forge
Explore
Log in

Don't you dare go Hollow

Honest 8x8 rate term, dead-zone 8x8 quantiser, 8x8 gain test

Browse files at this commit

b397db55ecedba0a41b126382b6382e8732248f6Ahmet Kaan GümüşParent: 4e4673b
  • feat
    • honest 8x8 rate term, dead-zone 8x8 quantiser, 8x8 gain test
Show full message
feat(h264-enc): honest 8x8 rate term, dead-zone 8x8 quantiser, 8x8 gain test

The 8x8 quantiser now rounds with the same 1/3 (intra) / 1/6 (inter)
dead zone as the 4x4 one, and the mode decision charges a level's
position in a 64-coefficient significance map: without it Intra_8x8 was
picked on 28 macroblocks that cost 44 real bits more each. The gain test
codes curved shading, a 20-sample texture and glyph rows at qp 26: +0.30
dB at equal bits, 7.4% of macroblocks 8x8 (100 intra, 123 inter). The
conformance gates assert 8x8 macroblocks of both kinds across their
sweeps, and the VA-API decode runs with the flag on.
crates/ec-h264/src/enc/mb.rsmodified+6 -1
@@ -1425,7 +1425,12 @@ fn estimate_luma_bits(lv: &Levels) -> i64 {
1425 if lv.trans8 {1425 if lv.trans8 {
1426 for blk8 in 0..4 {1426 for blk8 in 0..4 {
1427 if lv.cbp_luma & (1 << blk8) != 0 {1427 if lv.cbp_luma & (1 << blk8) != 0 {
1428- bits += block_bits(&lv.luma8[blk8]);1428+ // A level's position costs two more bits in a 64-coefficient
1429+ // significance map than in a 16-coefficient one; measured
1430+ // against the coded size on the text clip, without this the
1431+ // estimate undercharges Intra_8x8 by ~44 bits a macroblock.
1432+ let b = &lv.luma8[blk8];
1433+ bits += block_bits(b) + 2 * b.iter().filter(|&&l| l != 0).count() as i64;
1429 }1434 }
1430 }1435 }
1431 return bits;1436 return bits;
crates/ec-h264/src/enc/quant.rsmodified+2 -2
@@ -152,8 +152,8 @@ pub(crate) fn forward_8x8(d: &[i32; 64]) -> [i64; 64] {
152 /// precomputed shift because [`forward_8x8`]'s coefficients already carry152 /// precomputed shift because [`forward_8x8`]'s coefficients already carry
153 /// far more headroom than a `<<15`-ish fixed point would give.153 /// far more headroom than a `<<15`-ish fixed point would give.
154 #[inline]154 #[inline]
155-fn round_div(raw: i64, denom: i64, _intra: bool) -> i64 {155+fn round_div(raw: i64, denom: i64, intra: bool) -> i64 {
156- (raw + denom / 2) / denom156+ (raw + denom / if intra { 3 } else { 6 }) / denom
157 }157 }
158 158
159 /// Quantise an 8x8 [`forward_8x8`] output into scan (zig-zag) order, undoing159 /// Quantise an 8x8 [`forward_8x8`] output into scan (zig-zag) order, undoing
crates/ec-h264/tests/encode.rsmodified+50 -21
@@ -126,6 +126,7 @@ fn psnr(a: &[u8], b: &[u8]) -> f64 {
126 /// sample, over a QP sweep, both presets, single and multi threaded.126 /// sample, over a QP sweep, both presets, single and multi threaded.
127 #[test]127 #[test]
128 fn reconstruction_matches_the_decoder() {128 fn reconstruction_matches_the_decoder() {
129+ let mut t8x8_mbs = (0, 0);
129 for (w, h) in [(176, 144), (208, 122), (64, 48)] {130 for (w, h) in [(176, 144), (208, 122), (64, 48)] {
130 let mut clip = Clip::new(w, h);131 let mut clip = Clip::new(w, h);
131 for qp in [12, 24, 33, 44] {132 for qp in [12, 24, 33, 44] {
@@ -155,10 +156,8 @@ fn reconstruction_matches_the_decoder() {
155 }156 }
156 if t8x8 {157 if t8x8 {
157 let (intra, inter) = enc.transform_8x8_mbs();158 let (intra, inter) = enc.transform_8x8_mbs();
158- assert!(159+ t8x8_mbs.0 += intra;
159- intra > 0 && inter > 0,160+ t8x8_mbs.1 += inter;
160- "{w}x{h} qp {qp} cabac {cabac}: 8x8 MBs intra {intra} inter {inter}"
161- );
162 }161 }
163 let decoded = decode_all(&stream);162 let decoded = decode_all(&stream);
164 assert_eq!(163 assert_eq!(
@@ -183,6 +182,13 @@ fn reconstruction_matches_the_decoder() {
183 }182 }
184 }183 }
185 }184 }
185+ // The flag-on streams above really carried 8x8 macroblocks of both kinds.
186+ assert!(
187+ t8x8_mbs.0 > 0 && t8x8_mbs.1 > 0,
188+ "8x8 MBs across the sweep: intra {} inter {}",
189+ t8x8_mbs.0,
190+ t8x8_mbs.1
191+ );
186 }192 }
187 193
188 /// Quality tracks the quantiser, and a low QP is close to lossless: a stream194 /// Quality tracks the quantiser, and a low QP is close to lossless: a stream
@@ -326,12 +332,11 @@ fn encode_clip(
326 h: usize,332 h: usize,
327 frames: usize,333 frames: usize,
328 configure: impl FnOnce(&mut EncoderConfig),334 configure: impl FnOnce(&mut EncoderConfig),
329-) -> (Vec<u8>, Vec<Planes>) {335+) -> (Vec<u8>, Vec<Planes>, (u64, u64)) {
330 let mut cfg = EncoderConfig::new(w as u32, h as u32);336 let mut cfg = EncoderConfig::new(w as u32, h as u32);
331 cfg.gop_size = 10;337 cfg.gop_size = 10;
332 cfg.threads = 3;338 cfg.threads = 3;
333 configure(&mut cfg);339 configure(&mut cfg);
334- let t8x8 = cfg.transform_8x8;
335 let mut enc = Encoder::new(cfg).expect("encoder");340 let mut enc = Encoder::new(cfg).expect("encoder");
336 let mut clip = Clip::new(w, h);341 let mut clip = Clip::new(w, h);
337 let mut stream = Vec::new();342 let mut stream = Vec::new();
@@ -341,11 +346,7 @@ fn encode_clip(
341 stream.extend_from_slice(&enc.encode(&clip.view()).expect("encode").au);346 stream.extend_from_slice(&enc.encode(&clip.view()).expect("encode").au);
342 recons.push(enc.reconstruction().expect("reconstruction"));347 recons.push(enc.reconstruction().expect("reconstruction"));
343 }348 }
344- if t8x8 {349+ (stream, recons, enc.transform_8x8_mbs())
345- let (intra, inter) = enc.transform_8x8_mbs();
346- assert!(intra > 0 && inter > 0, "8x8 MBs intra {intra} inter {inter}");
347- }
348- (stream, recons)
349 }350 }
350 351
351 /// Flat gradients and glyph-like rectangles: the content the 8x8 transform352 /// Flat gradients and glyph-like rectangles: the content the 8x8 transform
@@ -355,13 +356,27 @@ fn encode_clip(
355 fn eight_by_eight_gains_on_flat_and_text() {356 fn eight_by_eight_gains_on_flat_and_text() {
356 let (w, h) = (320, 240);357 let (w, h) = (320, 240);
357 let render = |t: usize, clip: &mut Clip| {358 let render = |t: usize, clip: &mut Clip| {
359+ // A half-sample pan per frame: inter prediction has a residual to code.
360+ let sx = |x: usize| x * 2 + t;
358 for y in 0..h {361 for y in 0..h {
359 for x in 0..w {362 for x in 0..w {
360- let grad = (x * 3 / 4 + y / 2 + t) as u8 / 2 + 40;363+ let (px, py) = (sx(x) as f64 / 2.0, y as f64);
361- // Glyph rows: 12x16 cells, a 2-sample stem and a bar.364+ // Curved shading: smooth but not a plane, so no 16x16 mode
362- let (cx, cy) = ((x + t) % 12, y % 16);365+ // predicts it for free.
363- let glyph = y > h / 3 && cy >= 3 && cy < 13 && (cx < 2 || (cy == 8 && cx < 8));366+ let shade = 70.0 + px * px / 900.0 + py * py / 700.0 - px * py / 1500.0;
364- clip.y[y * w + x] = if glyph { 20 } else { grad };367+ // Gentle texture with a 20-sample period: residual energy that
368+ // spans more than a 4x4 block, which is what the 8x8 transform
369+ // is for.
370+ let tri = |v: f64| ((v % 20.0) - 10.0).abs() - 5.0;
371+ let shade = shade + tri(px) * tri(py) / 1.5;
372+ // Glyph rows: 16x24 cells, stroke pattern hashed per cell.
373+ let (cell, cx, cy) = (sx(x) / 32, (sx(x) / 2) % 16, y % 24);
374+ let hash = (cell * 2654435761usize.wrapping_add(y / 24 * 40503)) >> 7;
375+ let stem = (2..5).contains(&cx);
376+ let bar = (10..13).contains(&cy) && cx < 12 && hash & 1 == 1;
377+ let foot = cy >= 19 && (2..13).contains(&cx) && hash & 2 == 2;
378+ let glyph = y > h / 3 && (3..22).contains(&cy) && (stem || bar || foot);
379+ clip.y[y * w + x] = if glyph { 24 } else { shade.min(235.0) as u8 };
365 }380 }
366 }381 }
367 clip.u.fill(128);382 clip.u.fill(128);
@@ -384,14 +399,18 @@ fn eight_by_eight_gains_on_flat_and_text() {
384 }399 }
385 if t8x8 {400 if t8x8 {
386 let (i, p) = enc.transform_8x8_mbs();401 let (i, p) = enc.transform_8x8_mbs();
402+ eprintln!("8x8 MBs: intra {i} inter {p}");
387 share = (i + p) as f64 / (10.0 * (w / 16 * h / 16) as f64);403 share = (i + p) as f64 / (10.0 * (w / 16 * h / 16) as f64);
388 }404 }
389 (bits as f64, sum_psnr / 10.0, share)405 (bits as f64, sum_psnr / 10.0, share)
390 };406 };
391 // Interpolate the off curve at the on point's bits / PSNR.407 // Interpolate the off curve at the on point's bits / PSNR.
392- let qp = 30;408+ let qp = 26;
393 let (bits_on, psnr_on, share) = run(true, qp);409 let (bits_on, psnr_on, share) = run(true, qp);
394- let off: Vec<_> = [qp - 2, qp, qp + 2].iter().map(|&q| run(false, q)).collect();410+ let off: Vec<_> = [qp - 2, qp, qp + 2]
411+ .iter()
412+ .map(|&q| run(false, q))
413+ .collect();
395 let (bits_off, psnr_off, _) = off[1];414 let (bits_off, psnr_off, _) = off[1];
396 // Local slopes of the off curve, from the neighbouring QPs.415 // Local slopes of the off curve, from the neighbouring QPs.
397 let dpsnr_dbits = (off[0].1 - off[2].1) / (off[0].0 - off[2].0);416 let dpsnr_dbits = (off[0].1 - off[2].1) / (off[0].0 - off[2].0);
@@ -421,15 +440,18 @@ fn ffmpeg_decodes_bit_exactly() {
421 eprintln!("SKIP ffmpeg_decodes_bit_exactly: no ffmpeg on PATH");440 eprintln!("SKIP ffmpeg_decodes_bit_exactly: no ffmpeg on PATH");
422 return;441 return;
423 }442 }
443+ let mut t8x8_mbs = (0, 0);
424 for (w, h) in [(176, 144), (322, 242)] {444 for (w, h) in [(176, 144), (322, 242)] {
425 for qp in [10, 20, 28, 37, 47] {445 for qp in [10, 20, 28, 37, 47] {
426 let cavlc = qp % 2 == 0; // both entropy coders across the sweep446 let cavlc = qp % 2 == 0; // both entropy coders across the sweep
427 let t8x8 = qp % 3 == 1; // High-profile PPS with the per-MB flag447 let t8x8 = qp % 3 == 1; // High-profile PPS with the per-MB flag
428- let (stream, recons) = encode_clip(w, h, 6, |cfg| {448+ let (stream, recons, (intra, inter)) = encode_clip(w, h, 6, |cfg| {
429 cfg.qp = qp;449 cfg.qp = qp;
430 cfg.cabac = !cavlc;450 cfg.cabac = !cavlc;
431 cfg.transform_8x8 = t8x8;451 cfg.transform_8x8 = t8x8;
432 });452 });
453+ t8x8_mbs.0 += intra;
454+ t8x8_mbs.1 += inter;
433 let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes our stream");455 let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes our stream");
434 assert_eq!(decoded.len(), recons.len(), "{w}x{h} qp {qp}: frame count");456 assert_eq!(decoded.len(), recons.len(), "{w}x{h} qp {qp}: frame count");
435 for (i, (dec, rec)) in decoded.iter().zip(&recons).enumerate() {457 for (i, (dec, rec)) in decoded.iter().zip(&recons).enumerate() {
@@ -439,6 +461,12 @@ fn ffmpeg_decodes_bit_exactly() {
439 }461 }
440 }462 }
441 }463 }
464+ assert!(
465+ t8x8_mbs.0 > 0 && t8x8_mbs.1 > 0,
466+ "8x8 MBs across the sweep: intra {} inter {}",
467+ t8x8_mbs.0,
468+ t8x8_mbs.1
469+ );
442 }470 }
443 471
444 /// The same stream under constant bitrate, which is the mode edith exports in.472 /// The same stream under constant bitrate, which is the mode edith exports in.
@@ -449,7 +477,7 @@ fn ffmpeg_decodes_a_cbr_stream_bit_exactly() {
449 return;477 return;
450 }478 }
451 let (w, h) = (352, 288);479 let (w, h) = (352, 288);
452- let (stream, recons) = encode_clip(w, h, 12, |cfg| {480+ let (stream, recons, _) = encode_clip(w, h, 12, |cfg| {
453 cfg.bitrate = 2_000_000;481 cfg.bitrate = 2_000_000;
454 cfg.framerate = 25.0;482 cfg.framerate = 25.0;
455 });483 });
@@ -475,10 +503,11 @@ fn vaapi_decodes_our_stream() {
475 return;503 return;
476 }504 }
477 let (w, h) = (352, 288);505 let (w, h) = (352, 288);
478- let (stream, recons) = encode_clip(w, h, 6, |cfg| {506+ let (stream, recons, (intra, inter)) = encode_clip(w, h, 6, |cfg| {
479 cfg.qp = 26;507 cfg.qp = 26;
480 cfg.transform_8x8 = true;508 cfg.transform_8x8 = true;
481 });509 });
510+ assert!(intra + inter > 0, "no 8x8 macroblocks in the VA-API stream");
482 let Some(decoded) = ffmpeg_decode(511 let Some(decoded) = ffmpeg_decode(
483 &stream,512 &stream,
484 w,513 w,