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 {
14251425 if lv.trans8 {
14261426 for blk8 in 0..4 {
14271427 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;
14291434 }
14301435 }
14311436 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] {
152152 /// precomputed shift because [`forward_8x8`]'s coefficients already carry
153153 /// far more headroom than a `<<15`-ish fixed point would give.
154154 #[inline]
155-fn round_div(raw: i64, denom: i64, _intra: bool) -> i64 {
156- (raw + denom / 2) / denom
155+fn round_div(raw: i64, denom: i64, intra: bool) -> i64 {
156+ (raw + denom / if intra { 3 } else { 6 }) / denom
157157 }
158158
159159 /// 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 {
126126 /// sample, over a QP sweep, both presets, single and multi threaded.
127127 #[test]
128128 fn reconstruction_matches_the_decoder() {
129+ let mut t8x8_mbs = (0, 0);
129130 for (w, h) in [(176, 144), (208, 122), (64, 48)] {
130131 let mut clip = Clip::new(w, h);
131132 for qp in [12, 24, 33, 44] {
@@ -155,10 +156,8 @@ fn reconstruction_matches_the_decoder() {
155156 }
156157 if t8x8 {
157158 let (intra, inter) = enc.transform_8x8_mbs();
158- assert!(
159- intra > 0 && inter > 0,
160- "{w}x{h} qp {qp} cabac {cabac}: 8x8 MBs intra {intra} inter {inter}"
161- );
159+ t8x8_mbs.0 += intra;
160+ t8x8_mbs.1 += inter;
162161 }
163162 let decoded = decode_all(&stream);
164163 assert_eq!(
@@ -183,6 +182,13 @@ fn reconstruction_matches_the_decoder() {
183182 }
184183 }
185184 }
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+ );
186192 }
187193
188194 /// Quality tracks the quantiser, and a low QP is close to lossless: a stream
@@ -326,12 +332,11 @@ fn encode_clip(
326332 h: usize,
327333 frames: usize,
328334 configure: impl FnOnce(&mut EncoderConfig),
329-) -> (Vec<u8>, Vec<Planes>) {
335+) -> (Vec<u8>, Vec<Planes>, (u64, u64)) {
330336 let mut cfg = EncoderConfig::new(w as u32, h as u32);
331337 cfg.gop_size = 10;
332338 cfg.threads = 3;
333339 configure(&mut cfg);
334- let t8x8 = cfg.transform_8x8;
335340 let mut enc = Encoder::new(cfg).expect("encoder");
336341 let mut clip = Clip::new(w, h);
337342 let mut stream = Vec::new();
@@ -341,11 +346,7 @@ fn encode_clip(
341346 stream.extend_from_slice(&enc.encode(&clip.view()).expect("encode").au);
342347 recons.push(enc.reconstruction().expect("reconstruction"));
343348 }
344- if t8x8 {
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+ (stream, recons, enc.transform_8x8_mbs())
349350 }
350351
351352 /// Flat gradients and glyph-like rectangles: the content the 8x8 transform
@@ -355,13 +356,27 @@ fn encode_clip(
355356 fn eight_by_eight_gains_on_flat_and_text() {
356357 let (w, h) = (320, 240);
357358 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;
358361 for y in 0..h {
359362 for x in 0..w {
360- let grad = (x * 3 / 4 + y / 2 + t) as u8 / 2 + 40;
361- // Glyph rows: 12x16 cells, a 2-sample stem and a bar.
362- let (cx, cy) = ((x + t) % 12, y % 16);
363- let glyph = y > h / 3 && cy >= 3 && cy < 13 && (cx < 2 || (cy == 8 && cx < 8));
364- clip.y[y * w + x] = if glyph { 20 } else { grad };
363+ let (px, py) = (sx(x) as f64 / 2.0, y as f64);
364+ // Curved shading: smooth but not a plane, so no 16x16 mode
365+ // predicts it for free.
366+ let shade = 70.0 + px * px / 900.0 + py * py / 700.0 - px * py / 1500.0;
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 };
365380 }
366381 }
367382 clip.u.fill(128);
@@ -384,14 +399,18 @@ fn eight_by_eight_gains_on_flat_and_text() {
384399 }
385400 if t8x8 {
386401 let (i, p) = enc.transform_8x8_mbs();
402+ eprintln!("8x8 MBs: intra {i} inter {p}");
387403 share = (i + p) as f64 / (10.0 * (w / 16 * h / 16) as f64);
388404 }
389405 (bits as f64, sum_psnr / 10.0, share)
390406 };
391407 // Interpolate the off curve at the on point's bits / PSNR.
392- let qp = 30;
408+ let qp = 26;
393409 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();
395414 let (bits_off, psnr_off, _) = off[1];
396415 // Local slopes of the off curve, from the neighbouring QPs.
397416 let dpsnr_dbits = (off[0].1 - off[2].1) / (off[0].0 - off[2].0);
@@ -421,15 +440,18 @@ fn ffmpeg_decodes_bit_exactly() {
421440 eprintln!("SKIP ffmpeg_decodes_bit_exactly: no ffmpeg on PATH");
422441 return;
423442 }
443+ let mut t8x8_mbs = (0, 0);
424444 for (w, h) in [(176, 144), (322, 242)] {
425445 for qp in [10, 20, 28, 37, 47] {
426446 let cavlc = qp % 2 == 0; // both entropy coders across the sweep
427447 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| {
429449 cfg.qp = qp;
430450 cfg.cabac = !cavlc;
431451 cfg.transform_8x8 = t8x8;
432452 });
453+ t8x8_mbs.0 += intra;
454+ t8x8_mbs.1 += inter;
433455 let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes our stream");
434456 assert_eq!(decoded.len(), recons.len(), "{w}x{h} qp {qp}: frame count");
435457 for (i, (dec, rec)) in decoded.iter().zip(&recons).enumerate() {
@@ -439,6 +461,12 @@ fn ffmpeg_decodes_bit_exactly() {
439461 }
440462 }
441463 }
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+ );
442470 }
443471
444472 /// 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() {
449477 return;
450478 }
451479 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| {
453481 cfg.bitrate = 2_000_000;
454482 cfg.framerate = 25.0;
455483 });
@@ -475,10 +503,11 @@ fn vaapi_decodes_our_stream() {
475503 return;
476504 }
477505 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| {
479507 cfg.qp = 26;
480508 cfg.transform_8x8 = true;
481509 });
510+ assert!(intra + inter > 0, "no 8x8 macroblocks in the VA-API stream");
482511 let Some(decoded) = ffmpeg_decode(
483512 &stream,
484513 w,