forge
Explore
Log in

Don't you dare go Hollow

Choose the intra chroma mode by rate-distortion

Browse files at this commit

495e4503980d5f7115512fb1677eeed746980fd5Ahmet Kaan GümüşParent: 59070a1
  • perf
    • choose the intra chroma mode by rate-distortion
Show full message
perf(h264): choose the intra chroma mode by rate-distortion

The chooser scored the four chroma prediction modes on SATD of the
prediction plus the bits naming the mode, deciding before the quantiser
had a say. Every available mode now goes through the real chroma coding
path and is scored on reconstructed SSD plus what its levels and its name
actually cost, which is the same measurement the rest of the macroblock
decisions already make.

Measured with bd_psnr_vs_x264 against x264 -preset medium, BD-PSNR-YUV,
GOP 10, 24 consecutive pictures: 3840x1608 film -0.500 -> -0.443 dB,
2560x1440 screen capture -0.220 -> -0.146 dB, its second scene -0.571 ->
-0.538 dB, a 1080p phone clip -1.122 -> -1.084 dB, a 720p web clip
-0.606 -> -0.572 dB. Five clips of five improve; luma is unmoved
(film -0.065 -> -0.070 dB) and encode time does not change measurably,
since only intra macroblocks pay and only four chroma trials each.
crates/ec-h264/src/enc/mb.rsmodified+122 -89
@@ -1852,93 +1852,6 @@ fn code_chroma(
1852 }1852 }
1853 }1853 }
1854 1854
1855-/// Pick the chroma prediction mode, leaving the winner in the planes.
1856-///
1857-/// SATD plus the mode's own signalling, not SAD: the transform is what the
1858-/// residual actually goes through, and the four modes cost between one and five
1859-/// bits to name. Measured with `bd_psnr_vs_x264` against x264 `-preset medium`,
1860-/// SAD-with-no-rate-term -> this, at no measurable encode time:
1861-///
1862-/// | clip | all-intra | GOP 10 |
1863-/// |---|---|---|
1864-/// | 3840x1608 film | -0.620 -> -0.535 dB | -1.495 -> -1.404 dB |
1865-/// | 2560x1440 screen capture | -0.231 -> -0.197 dB | -0.062 -> -0.033 dB |
1866-/// | 352x288 synthetic ramp | - | -5.297 -> -5.291 dB |
1867-///
1868-/// SATD alone accounts for about two thirds of that and the rate term the rest
1869-/// (film all-intra: -0.620 -> -0.568 -> -0.535 dB).
1870-fn choose_chroma_mode(
1871- pic: &mut Picture,
1872- e: &MbEnc<'_>,
1873- nbr: &MbNeighbors,
1874- mb_x: usize,
1875- mb_y: usize,
1876-) -> u8 {
1877- let mut best = (i32::MAX, 0u8);
1878- for mode in [0u8, 1, 2, 3] {
1879- let ok = match mode {
1880- 1 => nbr.a,
1881- 2 => nbr.b,
1882- 3 => nbr.a && nbr.b && nbr.d,
1883- _ => true,
1884- };
1885- if !ok {
1886- continue;
1887- }
1888- let mut cost = 0;
1889- for comp in 0..2 {
1890- let (plane, src) = if comp == 0 {
1891- (&mut pic.cb, &e.src.u)
1892- } else {
1893- (&mut pic.cr, &e.src.v)
1894- };
1895- let stride = plane.stride;
1896- let origin = plane.at(mb_x * 8, mb_y * 8);
1897- let mut win = PlaneWindow {
1898- data: &mut plane.data,
1899- stride,
1900- origin,
1901- };
1902- pred_chroma_8x8(mode, &mut win, nbr.b, nbr.a);
1903- cost += satd_block(
1904- src,
1905- e.src.c_stride,
1906- mb_x * 8,
1907- mb_y * 8,
1908- &plane.data,
1909- stride,
1910- origin,
1911- 8,
1912- 8,
1913- );
1914- }
1915- // What signalling the mode costs: intra_chroma_pred_mode is ue(v), so
1916- // DC is one bit and the plane mode five.
1917- let bits = match mode {
1918- 0 => 1.0,
1919- 3 => 5.0,
1920- _ => 3.0,
1921- };
1922- cost += motion_rate(e, bits);
1923- if cost < best.0 {
1924- best = (cost, mode);
1925- }
1926- }
1927- // Leave the chosen mode's prediction in place.
1928- for comp in 0..2 {
1929- let plane = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
1930- let stride = plane.stride;
1931- let origin = plane.at(mb_x * 8, mb_y * 8);
1932- let mut win = PlaneWindow {
1933- data: &mut plane.data,
1934- stride,
1935- origin,
1936- };
1937- pred_chroma_8x8(best.1, &mut win, nbr.b, nbr.a);
1938- }
1939- best.1
1940-}
1941-
1942 /// Code the luma of an Intra_4x4 macroblock: per block, choose the prediction1855 /// Code the luma of an Intra_4x4 macroblock: per block, choose the prediction
1943 /// mode against the *reconstructed* neighbours and reconstruct it immediately,1856 /// mode against the *reconstructed* neighbours and reconstruct it immediately,
1944 /// because the next block predicts from it. Returns the chosen modes and the1857 /// because the next block predicts from it. Returns the chosen modes and the
@@ -2638,9 +2551,8 @@ fn encode_intra_mb(
2638 let mut modes8 = [2u8; 4];2551 let mut modes8 = [2u8; 4];
2639 let mut pred_modes8 = [2u8; 4];2552 let mut pred_modes8 = [2u8; 4];
2640 let mut use_i4 = false;2553 let mut use_i4 = false;
2641- let chroma_mode = choose_chroma_mode(pic, e, &nbr, mb_x, mb_y);
2642 let mut chroma_lv = Levels::default();2554 let mut chroma_lv = Levels::default();
2643- code_chroma(pic, e, w, mb_x, mb_y, qp, true, &mut chroma_lv);2555+ let chroma_mode = choose_chroma_mode_rd(pic, e, w, &nbr, mb_x, mb_y, qp, &mut chroma_lv);
2644 let mut use_i8 = false;2556 let mut use_i8 = false;
2645 2557
2646 if cost.allow_i4 {2558 if cost.allow_i4 {
@@ -3412,3 +3324,124 @@ fn finish_mb(
3412 }3324 }
3413 pic.mb_dc_cbf[mb_addr] = dc_cbf;3325 pic.mb_dc_cbf[mb_addr] = dc_cbf;
3414 }3326 }
3327+
3328+/// Chroma prediction mode by rate-distortion: every available mode is coded
3329+/// through the real chroma path and scored on reconstructed SSD plus the bits
3330+/// its levels and its own name actually cost. The SATD-plus-mode-bits chooser
3331+/// this replaced picked the mode before the quantiser had a say, which reads
3332+/// as a chroma deficit at matched luma: measured with `bd_psnr_vs_x264`
3333+/// against x264 `-preset medium`, BD-PSNR-YUV, GOP 10, 24 pictures:
3334+///
3335+/// | clip | SATD | RD |
3336+/// |---|---|---|
3337+/// | 3840x1608 film | -0.500 dB | -0.443 dB |
3338+/// | 2560x1440 screen capture | -0.220 dB | -0.146 dB |
3339+/// | 2560x1440 screen capture, second scene | -0.571 dB | -0.538 dB |
3340+/// | 1080p phone clip | -1.122 dB | -1.084 dB |
3341+/// | 720p web clip | -0.606 dB | -0.572 dB |
3342+///
3343+/// Luma is unmoved (film -0.065 -> -0.070 dB) and the encode time does not
3344+/// change measurably: only intra macroblocks pay, and only four trials of the
3345+/// chroma path each.
3346+#[allow(clippy::too_many_arguments)]
3347+fn choose_chroma_mode_rd(
3348+ pic: &mut Picture,
3349+ e: &MbEnc<'_>,
3350+ w: &EncEntropy,
3351+ nbr: &MbNeighbors,
3352+ mb_x: usize,
3353+ mb_y: usize,
3354+ qp: i32,
3355+ out: &mut Levels,
3356+) -> u8 {
3357+ let qp_c = chroma_qp(qp, e.cqo);
3358+ let lambda = lambda_ssd(qp_c);
3359+ let mut best: Option<(f64, u8, Levels, [[u8; 64]; 2])> = None;
3360+ for mode in [0u8, 1, 2, 3] {
3361+ let ok = match mode {
3362+ 1 => nbr.a,
3363+ 2 => nbr.b,
3364+ 3 => nbr.a && nbr.b && nbr.d,
3365+ _ => true,
3366+ };
3367+ if !ok {
3368+ continue;
3369+ }
3370+ for comp in 0..2 {
3371+ let plane = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
3372+ let stride = plane.stride;
3373+ let origin = plane.at(mb_x * 8, mb_y * 8);
3374+ let mut win = PlaneWindow {
3375+ data: &mut plane.data,
3376+ stride,
3377+ origin,
3378+ };
3379+ pred_chroma_8x8(mode, &mut win, nbr.b, nbr.a);
3380+ }
3381+ let mut lv = Levels::default();
3382+ code_chroma(pic, e, w, mb_x, mb_y, qp, true, &mut lv);
3383+ let mut ssd = 0i64;
3384+ let mut recon = [[0u8; 64]; 2];
3385+ for comp in 0..2 {
3386+ let (plane, src) = if comp == 0 {
3387+ (&pic.cb, &e.src.u)
3388+ } else {
3389+ (&pic.cr, &e.src.v)
3390+ };
3391+ let origin = plane.at(mb_x * 8, mb_y * 8);
3392+ for ry in 0..8 {
3393+ for rx in 0..8 {
3394+ let r = plane.data[origin + ry * plane.stride + rx];
3395+ recon[comp][ry * 8 + rx] = r;
3396+ let d = i64::from(src[(mb_y * 8 + ry) * e.src.c_stride + mb_x * 8 + rx])
3397+ - i64::from(r);
3398+ ssd += d * d;
3399+ }
3400+ }
3401+ }
3402+ let mut bits = match mode {
3403+ 0 => 1.0,
3404+ 3 => 5.0,
3405+ _ => 3.0,
3406+ };
3407+ for comp in 0..2 {
3408+ bits += match w.residual_block_cost(
3409+ &lv.chroma_dc[comp],
3410+ BlockCat::ChromaDc(comp as u8),
3411+ Some(1),
3412+ Some(1),
3413+ ) {
3414+ Some(b) => f64::from(b) / 256.0,
3415+ None => block_bits(&lv.chroma_dc[comp]) as f64,
3416+ };
3417+ for blk in 0..4 {
3418+ bits += match w.residual_block_cost(
3419+ &lv.chroma[comp][blk],
3420+ BlockCat::ChromaAc,
3421+ Some(1),
3422+ Some(1),
3423+ ) {
3424+ Some(b) => f64::from(b) / 256.0,
3425+ None => block_bits(&lv.chroma[comp][blk]) as f64,
3426+ };
3427+ }
3428+ }
3429+ let cost = ssd as f64 + lambda * bits;
3430+ if best.as_ref().is_none_or(|b| cost < b.0) {
3431+ best = Some((cost, mode, lv, recon));
3432+ }
3433+ }
3434+ let (_, mode, lv, recon) = best.expect("chroma DC prediction is always available");
3435+ for comp in 0..2 {
3436+ let plane = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
3437+ let stride = plane.stride;
3438+ let origin = plane.at(mb_x * 8, mb_y * 8);
3439+ for ry in 0..8 {
3440+ for rx in 0..8 {
3441+ plane.data[origin + ry * stride + rx] = recon[comp][ry * 8 + rx];
3442+ }
3443+ }
3444+ }
3445+ *out = lv;
3446+ mode
3447+}