forge
Explore
Log in

Don't you dare go Hollow

Let the inter decision see chroma

Browse files at this commit

5f9d4c96834a7822d5bab233641c7ae5f6165985Ahmet Kaan GümüşParent: 8adad23
  • feat
    • let the inter decision see chroma
Show full message
feat(h264): let the inter decision see chroma

Every decision in the P path was made on luma alone: the motion search, the
partition shape, the skip flag. Chroma was then coded against whatever vector
came out, having never been asked about. Add the two chroma planes to the cost
of the candidates that survive to the shape decision -- one motion
compensation and one SATD per candidate, which the search proper could not
afford but a handful of finalists can.

Weight swept at 0, 0.25, 0.5, 1 and 2 on two contents, read on BD-PSNR-YUV;
half is the optimum. Worth 0.041 dB on the film clip and nothing on the screen
capture, which is small enough to be the useful part of the result: our chroma
trails by 0.5-0.9 dB per plane at every QP, and this rules out the vector as
the reason. Weighting chroma higher does lift chroma PSNR, monotonically, and
past a half it spends more bits than the chroma is worth.
crates/ec-h264/src/enc/mb.rsmodified+74 -2
@@ -698,6 +698,22 @@ impl PShape {
698 }698 }
699 }699 }
700 700
701+/// How much the two chroma planes weigh against luma in the inter decision.
702+///
703+/// Swept at 0, 0.25, 0.5, 1 and 2, read on BD-PSNR-YUV because a luma-only
704+/// metric cannot price a term whose whole purpose is chroma: the film clip
705+/// reads -0.653/-0.622/-0.612/-0.615/-0.626 dB and the screen capture
706+/// -0.517/-0.521/-0.515/-0.521/-0.546. A half is film's optimum and inside
707+/// screen capture's flat part; zero reproduces the chroma-blind decision
708+/// exactly, which is how the sweep proved the term was reaching the search.
709+///
710+/// It is worth 0.041 dB on the film clip and nothing on the screen capture --
711+/// small enough to name what it rules out. Our chroma trails x264 by 0.5-0.9 dB
712+/// per plane at every QP, and it is not because the vector was chosen without
713+/// looking: weighting chroma up lifts chroma PSNR monotonically, and past a
714+/// half the bits it spends cost more than the chroma it buys.
715+const CHROMA_ME_WEIGHT: f64 = 0.5;
716+
701 /// The inter decision for a P macroblock.717 /// The inter decision for a P macroblock.
702 struct InterChoice {718 struct InterChoice {
703 shape: PShape,719 shape: PShape,
@@ -741,6 +757,60 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter
741 };757 };
742 let (sx, sy) = (mb_x * 16, mb_y * 16);758 let (sx, sy) = (mb_x * 16, mb_y * 16);
743 let sw = e.src.stride;759 let sw = e.src.stride;
760+
761+ // What the two chroma planes cost under a candidate vector, in the same
762+ // SATD domain the luma search ranks in. The search itself cannot afford
763+ // this -- it runs on SAD over a whole neighbourhood -- but the handful of
764+ // candidates that survive to the shape decision can, and until they did,
765+ // no decision in this encoder had ever looked at chroma: the vector, the
766+ // shape and the skip flag were all chosen on luma alone and chroma was
767+ // coded against whatever came out. Measured per plane against x264 that
768+ // showed up as a uniform 0.5-0.9 dB chroma deficit at every QP while we
769+ // spent more bits, which is a prediction that was never asked about.
770+ let cplanes = [&reference.cb, &reference.cr];
771+ let chroma_satd = |px: usize, py: usize, w: usize, h: usize, mv: [i16; 2]| -> i32 {
772+ if CHROMA_ME_WEIGHT == 0.0 {
773+ return 0;
774+ }
775+ let mut buf = [0u8; 64];
776+ let mut sum = 0;
777+ for comp in 0..2 {
778+ let src = cplanes[comp];
779+ let rp = RefPlane {
780+ data: &src.data,
781+ stride: src.stride,
782+ origin: src.origin,
783+ width: src.width,
784+ height: src.height,
785+ pad: src.pad,
786+ };
787+ let (cw, ch) = (w / 2, h / 2);
788+ mc_chroma(
789+ &rp,
790+ (sx + px) as i32 / 2,
791+ (sy + py) as i32 / 2,
792+ mv,
793+ cw,
794+ ch,
795+ cw,
796+ &mut buf,
797+ );
798+ let plane = if comp == 0 { &e.src.u } else { &e.src.v };
799+ sum += satd_block(
800+ plane,
801+ e.src.c_stride,
802+ (sx + px) / 2,
803+ (sy + py) / 2,
804+ &buf,
805+ cw,
806+ 0,
807+ cw,
808+ ch,
809+ );
810+ }
811+ (f64::from(sum) * CHROMA_ME_WEIGHT) as i32
812+ };
813+
744 let range = match e.preset {814 let range = match e.preset {
745 Preset::Fast => 16i16,815 Preset::Fast => 16i16,
746 Preset::Balanced => 48,816 Preset::Balanced => 48,
@@ -858,6 +928,7 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter
858 928
859 let seeds = [mvp, skip_mv, [0, 0], n[0].mv, n[1].mv, n[2].mv];929 let seeds = [mvp, skip_mv, [0, 0], n[0].mv, n[1].mv, n[2].mv];
860 let (mv16, cost16) = search(0, 0, 16, 16, mvp, &seeds);930 let (mv16, cost16) = search(0, 0, 16, 16, mvp, &seeds);
931+ let cost16 = cost16 + chroma_satd(0, 0, 16, 16, mv16);
861 let skip_cost = {932 let skip_cost = {
862 let mut buf = [0u8; 256];933 let mut buf = [0u8; 256];
863 mc_luma(&plane, sx as i32, sy as i32, skip_mv, 16, 16, 16, &mut buf);934 mc_luma(&plane, sx as i32, sy as i32, skip_mv, 16, 16, 16, &mut buf);
@@ -871,7 +942,8 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter
871 // read -1.216/-1.154/-1.182/-1.373 on film and -0.970/-0.696/-0.591/942 // read -1.216/-1.154/-1.182/-1.373 on film and -0.970/-0.696/-0.591/
872 // -0.562 on screen capture, so three quarters is film's optimum and943 // -0.562 on screen capture, so three quarters is film's optimum and
873 // inside screen capture's flat part.944 // inside screen capture's flat part.
874- satd_block(&e.src.y, sw, sx, sy, &buf, 16, 0, 16, 16) - motion_rate(e, 2.0)945+ satd_block(&e.src.y, sw, sx, sy, &buf, 16, 0, 16, 16) + chroma_satd(0, 0, 16, 16, skip_mv)
946+ - motion_rate(e, 2.0)
875 };947 };
876 948
877 let mut best = (PShape::Whole, [mv16; 4], cost16);949 let mut best = (PShape::Whole, [mv16; 4], cost16);
@@ -932,7 +1004,7 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter
932 let seeds = [mv16, part_mvp, mvp, skip_mv, [0, 0]];1004 let seeds = [mv16, part_mvp, mvp, skip_mv, [0, 0]];
933 let (mv, cost) = search(bx * 4, by * 4, w, h, part_mvp, &seeds);1005 let (mv, cost) = search(bx * 4, by * 4, w, h, part_mvp, &seeds);
934 mvs[part] = mv;1006 mvs[part] = mv;
935- total += cost;1007+ total += cost + chroma_satd(bx * 4, by * 4, w, h, mv);
936 }1008 }
937 if total < best.2 {1009 if total < best.2 {
938 best = (shape, mvs, total);1010 best = (shape, mvs, total);