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(
887 quant_4x4(&d, qp, intra, false, &mut lv.luma[blk])887 quant_4x4(&d, qp, intra, false, &mut lv.luma[blk])
888 };888 };
889 if rdoq && nz != 0 {889 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+ );
891 }899 }
892 lv.luma_nz[blk] = nz;900 lv.luma_nz[blk] = nz;
893 if nz == 0 {901 if nz == 0 {
@@ -922,10 +930,14 @@ fn code_luma_4x4(
922 }930 }
923 931
924 /// Squared error of coding `levels`, in the pixels the block reconstructs to.932 /// 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 {
926 let mut raster = [0i32; 16];934 let mut raster = [0i32; 16];
927- unzigzag(levels, &mut raster);935+ if skip_dc {
928- dequant_4x4(&mut raster, &e.ls, qp, false);936+ unzigzag_ac15(levels, &mut raster);
937+ } else {
938+ unzigzag(levels, &mut raster);
939+ }
940+ dequant_4x4(&mut raster, &e.ls, qp, skip_dc);
929 let mut resid = [0i32; 16];941 let mut resid = [0i32; 16];
930 inverse_transform_4x4(&raster, &mut resid);942 inverse_transform_4x4(&raster, &mut resid);
931 let mut ssd = 0i64;943 let mut ssd = 0i64;
@@ -964,6 +976,17 @@ const RDOQ_DEAD_ZONE: i32 = 2;
964 /// -4.065/-0.629/-0.477 dB on synthetic, film and screen capture.976 /// -4.065/-0.629/-0.477 dB on synthetic, film and screen capture.
965 const RDOQ_MAX_LEVEL: i32 = 2;977 const RDOQ_MAX_LEVEL: i32 = 2;
966 978
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+
967 /// Rate-distortion quantisation of one inter luma block: each level is offered990 /// Rate-distortion quantisation of one inter luma block: each level is offered
968 /// the two cheaper magnitudes below it, and takes one when the squared error it991 /// the two cheaper magnitudes below it, and takes one when the squared error it
969 /// gives up costs less than the bits it saves.992 /// gives up costs less than the bits it saves.
@@ -974,22 +997,25 @@ const RDOQ_MAX_LEVEL: i32 = 2;
974 /// is. Intra blocks stay out: their reconstruction is what the next block in997 /// is. Intra blocks stay out: their reconstruction is what the next block in
975 /// the macroblock predicts from, so a level changed here would move a998 /// the macroblock predicts from, so a level changed here would move a
976 /// prediction the decision cannot see.999 /// prediction the decision cannot see.
1000+#[allow(clippy::too_many_arguments)]
977 fn rdoq_4x4(1001 fn rdoq_4x4(
978 e: &MbEnc<'_>,1002 e: &MbEnc<'_>,
979 w: &EncEntropy,1003 w: &EncEntropy,
980 source: &[i32; 16],1004 source: &[i32; 16],
981 levels: &mut [i32; 16],1005 levels: &mut [i32; 16],
982 qp: i32,1006 qp: i32,
1007+ cat: BlockCat,
1008+ skip_dc: bool,
983 ) -> u8 {1009 ) -> 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 {
985 return levels.iter().filter(|&&l| l != 0).count() as u8;1011 return levels.iter().filter(|&&l| l != 0).count() as u8;
986 };1012 };
987 let lambda = RDOQ_LAMBDA * lambda_ssd(qp) / 256.0;1013 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);
989 // Highest scanning position first: dropping the last significant level is1015 // Highest scanning position first: dropping the last significant level is
990 // what shortens the significance map, so it is tried while the levels1016 // what shortens the significance map, so it is tried while the levels
991 // above it are still gone.1017 // above it are still gone.
992- for pos in (0..16).rev() {1018+ for pos in (0..cat.max_num_coeff()).rev() {
993 let level = levels[pos];1019 let level = levels[pos];
994 if level == 0 {1020 if level == 0 {
995 continue;1021 continue;
@@ -1006,11 +1032,10 @@ fn rdoq_4x4(
1006 tried = candidate;1032 tried = candidate;
1007 let keep = levels[pos];1033 let keep = levels[pos];
1008 levels[pos] = candidate * level.signum();1034 levels[pos] = candidate * level.signum();
1009- let Some(bits) = w.residual_block_cost(levels, BlockCat::Luma4x4, Some(1), Some(1))1035+ let Some(bits) = w.residual_block_cost(levels, cat, Some(1), Some(1)) else {
1010- else {
1011 unreachable!("CAVLC returned early");1036 unreachable!("CAVLC returned early");
1012 };1037 };
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);
1014 if trial < cost {1039 if trial < cost {
1015 cost = trial;1040 cost = trial;
1016 } else {1041 } else {
@@ -1023,7 +1048,7 @@ fn rdoq_4x4(
1023 // the same decision `worth_coding` makes for the paths without an exact1048 // the same decision `worth_coding` makes for the paths without an exact
1024 // rate, made here with one.1049 // rate, made here with one.
1025 let zero_bits = w1050 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))
1027 .expect("CABAC");1052 .expect("CABAC");
1028 let ssd_zero: i64 = source.iter().map(|&c| i64::from(c) * i64::from(c)).sum();1053 let ssd_zero: i64 = source.iter().map(|&c| i64::from(c) * i64::from(c)).sum();
1029 if ssd_zero as f64 + lambda * f64::from(zero_bits) <= cost {1054 if ssd_zero as f64 + lambda * f64::from(zero_bits) <= cost {
@@ -1166,6 +1191,7 @@ fn code_luma_i16(
1166 fn code_chroma(1191 fn code_chroma(
1167 pic: &mut Picture,1192 pic: &mut Picture,
1168 e: &MbEnc<'_>,1193 e: &MbEnc<'_>,
1194+ w: &EncEntropy,
1169 mb_x: usize,1195 mb_x: usize,
1170 mb_y: usize,1196 mb_y: usize,
1171 qp_y: i32,1197 qp_y: i32,
@@ -1196,8 +1222,23 @@ fn code_chroma(
1196 let source = d;1222 let source = d;
1197 forward_4x4(&mut d);1223 forward_4x4(&mut d);
1198 dc_lists[comp][blk] = d[0];1224 dc_lists[comp][blk] = d[0];
1199- let mut nz = quant_4x4(&d, qp_c, intra, true, &mut ac[comp][blk]);1225+ let rdoq = !intra && w.is_cabac();
1200- if nz > 0 {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 {
1201 // The same zero decision as luma, against the AC part alone:1242 // The same zero decision as luma, against the AC part alone:
1202 // the DC of this block travels through the 2x2 Hadamard and is1243 // the DC of this block travels through the 2x2 Hadamard and is
1203 // decided with the other three.1244 // decided with the other three.
@@ -1980,7 +2021,7 @@ fn encode_intra_mb(
1980 let mut use_i4 = false;2021 let mut use_i4 = false;
1981 let chroma_mode = choose_chroma_mode(pic, e, &nbr, mb_x, mb_y);2022 let chroma_mode = choose_chroma_mode(pic, e, &nbr, mb_x, mb_y);
1982 let mut chroma_lv = Levels::default();2023 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);
1984 let mut use_i8 = false;2025 let mut use_i8 = false;
1985 2026
1986 if cost.allow_i4 {2027 if cost.allow_i4 {
@@ -2485,7 +2526,7 @@ fn encode_inter_mb(
2485 let may_skip = choice.prefer_skip;2526 let may_skip = choice.prefer_skip;
2486 let ssd_skip = may_skip.then(|| ssd_mb(pic, e, mb_x, mb_y));2527 let ssd_skip = may_skip.then(|| ssd_mb(pic, e, mb_x, mb_y));
2487 let mut chroma_lv = Levels::default();2528 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);
2489 let mut lv = chroma_lv.clone();2530 let mut lv = chroma_lv.clone();
2490 if e.t8x8_inter(mb_y) {2531 if e.t8x8_inter(mb_y) {
2491 // Both transform sizes on the same prediction; the cheaper2532 // 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.
352 352
353 x264's own trellis is worth 0.193 dB on the film clip and 0.208 on the screen353 x264's own trellis is worth 0.193 dB on the film clip and 0.208 on the screen
354 capture (measured by ablation, r2 above). We now take 0.100 and 0.029 of that.354 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.