forge
Explore
Log in

Don't you dare go Hollow

h264: price inter chroma AC levels the same way

Browse files at this commit

e21a7646ba53d334b5abaa384d5e487fecc13867Ahmet Kaan GümüşParent: 47083d3
The RDOQ decision built for inter luma now also runs on the chroma AC
blocks: rdoq_4x4 takes the block category and the DC-skipping flag, and
block_ssd reconstructs through the AC-only unzigzag and dequant when it
is set.

The rounding that feeds the search had to be swept again rather than
inherited. Luma wanted the wider 1/2 rounding once a search could take
levels back down; chroma is monotonically worse for every widening and
keeps the 1/6 inter dead zone it already had, taking only the pruning
half. BD-PSNR against x264 on two real clips: film -0.628 -> -0.593,
screen capture -0.471 -> -0.468; the synthetic gate is unchanged at
-4.073 because its chroma planes are flat.
crates/ec-h264/src/enc/mb.rsmodified+56 -15
@@ -887,7 +887,15 @@ fn code_luma_4x4(
887887 quant_4x4(&d, qp, intra, false, &mut lv.luma[blk])
888888 };
889889 if rdoq && nz != 0 {
890- nz = rdoq_4x4(e, w, &source, &mut lv.luma[blk], qp);
890+ nz = rdoq_4x4(
891+ e,
892+ w,
893+ &source,
894+ &mut lv.luma[blk],
895+ qp,
896+ BlockCat::Luma4x4,
897+ false,
898+ );
891899 }
892900 lv.luma_nz[blk] = nz;
893901 if nz == 0 {
@@ -922,10 +930,14 @@ fn code_luma_4x4(
922930 }
923931
924932 /// Squared error of coding `levels`, in the pixels the block reconstructs to.
925-fn block_ssd(e: &MbEnc<'_>, source: &[i32; 16], levels: &[i32; 16], qp: i32) -> i64 {
933+fn block_ssd(e: &MbEnc<'_>, source: &[i32; 16], levels: &[i32; 16], qp: i32, skip_dc: bool) -> i64 {
926934 let mut raster = [0i32; 16];
927- unzigzag(levels, &mut raster);
928- dequant_4x4(&mut raster, &e.ls, qp, false);
935+ if skip_dc {
936+ unzigzag_ac15(levels, &mut raster);
937+ } else {
938+ unzigzag(levels, &mut raster);
939+ }
940+ dequant_4x4(&mut raster, &e.ls, qp, skip_dc);
929941 let mut resid = [0i32; 16];
930942 inverse_transform_4x4(&raster, &mut resid);
931943 let mut ssd = 0i64;
@@ -964,6 +976,17 @@ const RDOQ_DEAD_ZONE: i32 = 2;
964976 /// -4.065/-0.629/-0.477 dB on synthetic, film and screen capture.
965977 const RDOQ_MAX_LEVEL: i32 = 2;
966978
979+/// The rounding the chroma AC path keeps under the search: the inter dead zone
980+/// it already had. Luma wanted the wider 1/2 rounding once a search could take
981+/// levels back down (`RDOQ_DEAD_ZONE`); chroma does not. Measured on the two
982+/// clips at the settled luma constants, BD-PSNR against x264: 1/2 rounding
983+/// -0.653 film / -0.482 screen capture, 1/4 -0.623 / -0.472, 1/3 -0.644 /
984+/// -0.481, and the 1/6 kept here -0.593 / -0.468 -- against -0.628 / -0.471
985+/// with no chroma search at all. Chroma AC is sparse enough at these
986+/// quantisers that offering more levels costs more bits than the search takes
987+/// back; what pays is only the pruning.
988+const RDOQ_CHROMA_DEAD_ZONE: i32 = 6;
989+
967990 /// Rate-distortion quantisation of one inter luma block: each level is offered
968991 /// the two cheaper magnitudes below it, and takes one when the squared error it
969992 /// gives up costs less than the bits it saves.
@@ -974,22 +997,25 @@ const RDOQ_MAX_LEVEL: i32 = 2;
974997 /// is. Intra blocks stay out: their reconstruction is what the next block in
975998 /// the macroblock predicts from, so a level changed here would move a
976999 /// prediction the decision cannot see.
1000+#[allow(clippy::too_many_arguments)]
9771001 fn rdoq_4x4(
9781002 e: &MbEnc<'_>,
9791003 w: &EncEntropy,
9801004 source: &[i32; 16],
9811005 levels: &mut [i32; 16],
9821006 qp: i32,
1007+ cat: BlockCat,
1008+ skip_dc: bool,
9831009 ) -> u8 {
984- let Some(bits) = w.residual_block_cost(levels, BlockCat::Luma4x4, Some(1), Some(1)) else {
1010+ let Some(bits) = w.residual_block_cost(levels, cat, Some(1), Some(1)) else {
9851011 return levels.iter().filter(|&&l| l != 0).count() as u8;
9861012 };
9871013 let lambda = RDOQ_LAMBDA * lambda_ssd(qp) / 256.0;
988- let mut cost = block_ssd(e, source, levels, qp) as f64 + lambda * f64::from(bits);
1014+ let mut cost = block_ssd(e, source, levels, qp, skip_dc) as f64 + lambda * f64::from(bits);
9891015 // Highest scanning position first: dropping the last significant level is
9901016 // what shortens the significance map, so it is tried while the levels
9911017 // above it are still gone.
992- for pos in (0..16).rev() {
1018+ for pos in (0..cat.max_num_coeff()).rev() {
9931019 let level = levels[pos];
9941020 if level == 0 {
9951021 continue;
@@ -1006,11 +1032,10 @@ fn rdoq_4x4(
10061032 tried = candidate;
10071033 let keep = levels[pos];
10081034 levels[pos] = candidate * level.signum();
1009- let Some(bits) = w.residual_block_cost(levels, BlockCat::Luma4x4, Some(1), Some(1))
1010- else {
1035+ let Some(bits) = w.residual_block_cost(levels, cat, Some(1), Some(1)) else {
10111036 unreachable!("CAVLC returned early");
10121037 };
1013- let trial = block_ssd(e, source, levels, qp) as f64 + lambda * f64::from(bits);
1038+ let trial = block_ssd(e, source, levels, qp, skip_dc) as f64 + lambda * f64::from(bits);
10141039 if trial < cost {
10151040 cost = trial;
10161041 } else {
@@ -1023,7 +1048,7 @@ fn rdoq_4x4(
10231048 // the same decision `worth_coding` makes for the paths without an exact
10241049 // rate, made here with one.
10251050 let zero_bits = w
1026- .residual_block_cost(&[0; 16], BlockCat::Luma4x4, Some(1), Some(1))
1051+ .residual_block_cost(&[0; 16], cat, Some(1), Some(1))
10271052 .expect("CABAC");
10281053 let ssd_zero: i64 = source.iter().map(|&c| i64::from(c) * i64::from(c)).sum();
10291054 if ssd_zero as f64 + lambda * f64::from(zero_bits) <= cost {
@@ -1166,6 +1191,7 @@ fn code_luma_i16(
11661191 fn code_chroma(
11671192 pic: &mut Picture,
11681193 e: &MbEnc<'_>,
1194+ w: &EncEntropy,
11691195 mb_x: usize,
11701196 mb_y: usize,
11711197 qp_y: i32,
@@ -1196,8 +1222,23 @@ fn code_chroma(
11961222 let source = d;
11971223 forward_4x4(&mut d);
11981224 dc_lists[comp][blk] = d[0];
1199- let mut nz = quant_4x4(&d, qp_c, intra, true, &mut ac[comp][blk]);
1200- if nz > 0 {
1225+ let rdoq = !intra && w.is_cabac();
1226+ let mut nz = if rdoq {
1227+ quant_4x4_offset(&d, qp_c, RDOQ_CHROMA_DEAD_ZONE, true, &mut ac[comp][blk])
1228+ } else {
1229+ quant_4x4(&d, qp_c, intra, true, &mut ac[comp][blk])
1230+ };
1231+ if rdoq && nz > 0 {
1232+ nz = rdoq_4x4(
1233+ e,
1234+ w,
1235+ &source,
1236+ &mut ac[comp][blk],
1237+ qp_c,
1238+ BlockCat::ChromaAc,
1239+ true,
1240+ );
1241+ } else if nz > 0 {
12011242 // The same zero decision as luma, against the AC part alone:
12021243 // the DC of this block travels through the 2x2 Hadamard and is
12031244 // decided with the other three.
@@ -1980,7 +2021,7 @@ fn encode_intra_mb(
19802021 let mut use_i4 = false;
19812022 let chroma_mode = choose_chroma_mode(pic, e, &nbr, mb_x, mb_y);
19822023 let mut chroma_lv = Levels::default();
1983- code_chroma(pic, e, mb_x, mb_y, qp, true, &mut chroma_lv);
2024+ code_chroma(pic, e, w, mb_x, mb_y, qp, true, &mut chroma_lv);
19842025 let mut use_i8 = false;
19852026
19862027 if cost.allow_i4 {
@@ -2485,7 +2526,7 @@ fn encode_inter_mb(
24852526 let may_skip = choice.prefer_skip;
24862527 let ssd_skip = may_skip.then(|| ssd_mb(pic, e, mb_x, mb_y));
24872528 let mut chroma_lv = Levels::default();
2488- code_chroma(pic, e, mb_x, mb_y, qp, false, &mut chroma_lv);
2529+ code_chroma(pic, e, w, mb_x, mb_y, qp, false, &mut chroma_lv);
24892530 let mut lv = chroma_lv.clone();
24902531 if e.t8x8_inter(mb_y) {
24912532 // Both transform sizes on the same prediction; the cheaper
lanes/h264x264-r2.report.mdmodified+31 -0
@@ -352,3 +352,34 @@ candidates except for the whole-block-zero one.
352352
353353 x264's own trellis is worth 0.193 dB on the film clip and 0.208 on the screen
354354 capture (measured by ablation, r2 above). We now take 0.100 and 0.029 of that.
355+
356+## The same price, applied to inter chroma AC
357+
358+The luma decision was extended to the chroma AC blocks: they are quantised,
359+searched against the CABAC coder's own price for their levels, and given the
360+same exact whole-block-zero test, through the one `rdoq_4x4` the luma path
361+uses (it now takes the block category and the DC-skipping flag).
362+
363+The dead zone had to be re-swept there, because the class the luma work named
364+says a one-directional search measures whatever rounding feeds it. Chroma
365+answered the opposite way. BD-PSNR against x264, at the settled luma
366+constants:
367+
368+| chroma rounding under the search | film 3840x1608 | screen capture 2560x1440 |
369+|---|---|---|
370+| no chroma search (baseline) | -0.628 | -0.471 |
371+| 1/2 (what luma wanted) | -0.653 | -0.482 |
372+| 1/3 | -0.644 | -0.481 |
373+| 1/4 | -0.623 | -0.472 |
374+| 1/6 (the inter dead zone it had) | **-0.593** | **-0.468** |
375+
376+So chroma keeps its dead zone and takes only the pruning half of the search.
377+Chroma AC is sparse at these quantisers: offering more levels spends more bits
378+than the search wins back, and every widening is monotonically worse. The
379+synthetic gate does not move (-4.073 either way) -- its chroma planes are flat.
380+
381+Landed: film -0.628 -> -0.593 (0.035 dB), screen capture -0.471 -> -0.468.
382+x264's trellis is worth 0.193 dB on the film clip and 0.208 on the screen
383+capture, so the two slices together take 0.135 and 0.032 of it.
384+
385+The 8x8 luma transform path is still untouched.