h264: price inter chroma AC levels the same way
Browse files at this commite21a7646ba53d334b5abaa384d5e487fecc13867Parent: 47083d32 changed files +87 -15
- crates/ec-h264/src/enc/mb.rs +56 -15
- lanes/h264x264-r2.report.md +31 -0
crates/ec-h264/src/enc/mb.rsmodified+56 -15
| @@ -887,7 +887,15 @@ fn code_luma_4x4( | ||
| 887 | 887 | quant_4x4(&d, qp, intra, false, &mut lv.luma[blk]) |
| 888 | 888 | }; |
| 889 | 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 | 900 | lv.luma_nz[blk] = nz; |
| 893 | 901 | if nz == 0 { |
| @@ -922,10 +930,14 @@ fn code_luma_4x4( | ||
| 922 | 930 | } |
| 923 | 931 | |
| 924 | 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 | 934 | 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); | |
| 929 | 941 | let mut resid = [0i32; 16]; |
| 930 | 942 | inverse_transform_4x4(&raster, &mut resid); |
| 931 | 943 | let mut ssd = 0i64; |
| @@ -964,6 +976,17 @@ const RDOQ_DEAD_ZONE: i32 = 2; | ||
| 964 | 976 | /// -4.065/-0.629/-0.477 dB on synthetic, film and screen capture. |
| 965 | 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 | 990 | /// Rate-distortion quantisation of one inter luma block: each level is offered |
| 968 | 991 | /// the two cheaper magnitudes below it, and takes one when the squared error it |
| 969 | 992 | /// gives up costs less than the bits it saves. |
| @@ -974,22 +997,25 @@ const RDOQ_MAX_LEVEL: i32 = 2; | ||
| 974 | 997 | /// is. Intra blocks stay out: their reconstruction is what the next block in |
| 975 | 998 | /// the macroblock predicts from, so a level changed here would move a |
| 976 | 999 | /// prediction the decision cannot see. |
| 1000 | +#[allow(clippy::too_many_arguments)] | |
| 977 | 1001 | fn rdoq_4x4( |
| 978 | 1002 | e: &MbEnc<'_>, |
| 979 | 1003 | w: &EncEntropy, |
| 980 | 1004 | source: &[i32; 16], |
| 981 | 1005 | levels: &mut [i32; 16], |
| 982 | 1006 | qp: i32, |
| 1007 | + cat: BlockCat, | |
| 1008 | + skip_dc: bool, | |
| 983 | 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 | 1011 | return levels.iter().filter(|&&l| l != 0).count() as u8; |
| 986 | 1012 | }; |
| 987 | 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 | 1015 | // Highest scanning position first: dropping the last significant level is |
| 990 | 1016 | // what shortens the significance map, so it is tried while the levels |
| 991 | 1017 | // above it are still gone. |
| 992 | - for pos in (0..16).rev() { | |
| 1018 | + for pos in (0..cat.max_num_coeff()).rev() { | |
| 993 | 1019 | let level = levels[pos]; |
| 994 | 1020 | if level == 0 { |
| 995 | 1021 | continue; |
| @@ -1006,11 +1032,10 @@ fn rdoq_4x4( | ||
| 1006 | 1032 | tried = candidate; |
| 1007 | 1033 | let keep = levels[pos]; |
| 1008 | 1034 | 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 { | |
| 1011 | 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 | 1039 | if trial < cost { |
| 1015 | 1040 | cost = trial; |
| 1016 | 1041 | } else { |
| @@ -1023,7 +1048,7 @@ fn rdoq_4x4( | ||
| 1023 | 1048 | // the same decision `worth_coding` makes for the paths without an exact |
| 1024 | 1049 | // rate, made here with one. |
| 1025 | 1050 | 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 | 1052 | .expect("CABAC"); |
| 1028 | 1053 | let ssd_zero: i64 = source.iter().map(|&c| i64::from(c) * i64::from(c)).sum(); |
| 1029 | 1054 | if ssd_zero as f64 + lambda * f64::from(zero_bits) <= cost { |
| @@ -1166,6 +1191,7 @@ fn code_luma_i16( | ||
| 1166 | 1191 | fn code_chroma( |
| 1167 | 1192 | pic: &mut Picture, |
| 1168 | 1193 | e: &MbEnc<'_>, |
| 1194 | + w: &EncEntropy, | |
| 1169 | 1195 | mb_x: usize, |
| 1170 | 1196 | mb_y: usize, |
| 1171 | 1197 | qp_y: i32, |
| @@ -1196,8 +1222,23 @@ fn code_chroma( | ||
| 1196 | 1222 | let source = d; |
| 1197 | 1223 | forward_4x4(&mut d); |
| 1198 | 1224 | 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 { | |
| 1201 | 1242 | // The same zero decision as luma, against the AC part alone: |
| 1202 | 1243 | // the DC of this block travels through the 2x2 Hadamard and is |
| 1203 | 1244 | // decided with the other three. |
| @@ -1980,7 +2021,7 @@ fn encode_intra_mb( | ||
| 1980 | 2021 | let mut use_i4 = false; |
| 1981 | 2022 | let chroma_mode = choose_chroma_mode(pic, e, &nbr, mb_x, mb_y); |
| 1982 | 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 | 2025 | let mut use_i8 = false; |
| 1985 | 2026 | |
| 1986 | 2027 | if cost.allow_i4 { |
| @@ -2485,7 +2526,7 @@ fn encode_inter_mb( | ||
| 2485 | 2526 | let may_skip = choice.prefer_skip; |
| 2486 | 2527 | let ssd_skip = may_skip.then(|| ssd_mb(pic, e, mb_x, mb_y)); |
| 2487 | 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 | 2530 | let mut lv = chroma_lv.clone(); |
| 2490 | 2531 | if e.t8x8_inter(mb_y) { |
| 2491 | 2532 | // 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 | 353 | x264's own trellis is worth 0.193 dB on the film clip and 0.208 on the screen |
| 354 | 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. |