forge
Explore
Log in

Don't you dare go Hollow

S9: drop EncoderConfig::chroma_mode_search and chroma_rd_weight; default off, so the chroma RD search (choose_chroma_mode) was never called

Browse files at this commit

06fedd5bf46b1a91dd35a5621654d5857ea8ab88Ahmet Kaan GümüşParent: 6c44dc7
crates/ec-h265/src/ctu.rsmodified+9 -115
@@ -180,10 +180,6 @@ pub struct CtuEncoder<'a> {
180180 strong_smoothing: bool,
181181 /// How many of the 35 modes get a full rate-distortion trial.
182182 candidates: usize,
183- /// Whether the chroma mode is chosen by rate-distortion or always derived.
184- chroma_rd: bool,
185- /// Weight on chroma SSD in that decision, against luma's lambda.
186- chroma_rd_weight: f64,
187183 /// Whether an 8x8 coding unit may split into four 4x4 intra partitions.
188184 intra_nxn: bool,
189185 sign_hiding: bool,
@@ -229,8 +225,6 @@ impl<'a> CtuEncoder<'a> {
229225 qp: i32,
230226 strong_smoothing: bool,
231227 candidates: usize,
232- chroma_rd: bool,
233- chroma_rd_weight: f64,
234228 intra_nxn: bool,
235229 sign_hiding: bool,
236230 rdoq: bool,
@@ -260,8 +254,6 @@ impl<'a> CtuEncoder<'a> {
260254 lambda_satd: lambda_for(qp_bd).sqrt(),
261255 strong_smoothing,
262256 candidates: candidates.clamp(1, 35),
263- chroma_rd,
264- chroma_rd_weight,
265257 intra_nxn,
266258 sign_hiding,
267259 rdoq,
@@ -630,12 +622,8 @@ impl<'a> CtuEncoder<'a> {
630622
631623 // The chroma mode belongs to the coding unit and derives from the
632624 // first partition's luma mode (8.4.3).
633- let (chroma_mode, chroma_idx) = if self.chroma_rd {
634- self.choose_chroma_mode(x, y, 8, MIN_CB_LOG2, modes[0], self.transform_skip, enc)
635- } else {
636- (modes[0], None)
637- };
638- self.write_chroma_mode_syntax(enc, chroma_idx);
625+ let chroma_mode = modes[0];
626+ self.write_chroma_mode_syntax(enc);
639627 let cb_cbf = self
640628 .code_chroma(x, y, 8, chroma_mode, 1, self.transform_skip)
641629 .1;
@@ -726,12 +714,8 @@ impl<'a> CtuEncoder<'a> {
726714 // levels in the luma scratch.
727715 let mode = self.choose_luma_mode(x, y, HALF, LOG2 - 1, &mpm, enc);
728716 self.write_mode_syntax(enc, mode, &mpm);
729- let (chroma_mode, chroma_idx) = if self.chroma_rd {
730- self.choose_chroma_mode(x, y, HALF, LOG2 - 1, mode, chroma_tskip, enc)
731- } else {
732- (mode, None)
733- };
734- self.write_chroma_mode_syntax(enc, chroma_idx);
717+ let chroma_mode = mode;
718+ self.write_chroma_mode_syntax(enc);
735719
736720 let mut child_levels = [[0i32; 1024]; 4];
737721 let mut child_tskip = [self.transform_skip; 4];
@@ -839,12 +823,8 @@ impl<'a> CtuEncoder<'a> {
839823 let mpm = self.mpm_at(x, y);
840824 let mode = self.choose_luma_mode(x, y, n, log2, &mpm, enc);
841825 self.write_mode_syntax(enc, mode, &mpm);
842- let (chroma_mode, chroma_idx) = if self.chroma_rd {
843- self.choose_chroma_mode(x, y, n, log2, mode, chroma_tskip, enc)
844- } else {
845- (mode, None)
846- };
847- self.write_chroma_mode_syntax(enc, chroma_idx);
826+ let chroma_mode = mode;
827+ self.write_chroma_mode_syntax(enc);
848828
849829 // Luma was reconstructed by the mode search; chroma is coded now.
850830 let luma_cbf = self.scratch.luma_levels[..n * n].iter().any(|&v| v != 0);
@@ -1795,95 +1775,9 @@ impl<'a> CtuEncoder<'a> {
17951775
17961776 // ---- chroma mode decision --------------------------------------------
17971777
1798- /// Write `intra_chroma_pred_mode`: the single bin 0 for the derived mode,
1799- /// or bin 1 and two bypass bits picking one of the four explicit modes.
1800- fn write_chroma_mode_syntax(&self, enc: &mut CabacEncoder, idx: Option<u32>) {
1801- match idx {
1802- None => enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 0),
1803- Some(i) => {
1804- enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 1);
1805- enc.encode_bypass_bits(i, 2);
1806- }
1807- }
1808- }
1809-
1810- /// Pick the chroma prediction mode by rate-distortion over the five the
1811- /// syntax allows: planar, vertical, horizontal and DC — each replaced by
1812- /// angular 34 when it collides with the luma mode — plus the derived mode,
1813- /// which is the luma mode itself and costs one bin instead of three.
1814- ///
1815- /// Returns the mode and the index to code, `None` meaning derived. Neither
1816- /// the winner's reconstruction nor its levels survive the trial: the caller
1817- /// codes the winner again for real.
1818- #[allow(clippy::too_many_arguments)]
1819- fn choose_chroma_mode(
1820- &mut self,
1821- x: usize,
1822- y: usize,
1823- n: usize,
1824- log2: u32,
1825- luma_mode: u8,
1826- chroma_tskip: bool,
1827- enc: &mut CabacEncoder,
1828- ) -> (u8, Option<u32>) {
1829- let mut explicit = [0u8, 26, 10, 1];
1830- for mode in explicit.iter_mut() {
1831- if *mode == luma_mode {
1832- *mode = 34;
1833- }
1834- }
1835- let start = enc.snapshot();
1836- enc.set_counting(true);
1837- let mut best = (f64::MAX, luma_mode, None);
1838- for (i, mode) in explicit
1839- .iter()
1840- .copied()
1841- .enumerate()
1842- .map(|(i, m)| (Some(i as u32), m))
1843- .chain(std::iter::once((None, luma_mode)))
1844- {
1845- let bits_before = enc.bit_count();
1846- self.write_chroma_mode_syntax(enc, i);
1847- let (cb_ssd, cb_cbf) = self.code_chroma(x, y, n, mode, 1, chroma_tskip);
1848- let (cr_ssd, cr_cbf) = self.code_chroma(x, y, n, mode, 2, chroma_tskip);
1849- enc.encode_bin(ctx::CBF_CHROMA, u32::from(cb_cbf));
1850- enc.encode_bin(ctx::CBF_CHROMA, u32::from(cr_cbf));
1851- let scan = intra::scan_index(mode, log2 - 1, false);
1852- if cb_cbf {
1853- let levels = std::mem::take(&mut self.scratch.cb_levels);
1854- encode_residual(
1855- enc,
1856- &levels[..n * n / 4],
1857- log2 - 1,
1858- 1,
1859- scan,
1860- self.sign_hiding,
1861- self.ts_flag(chroma_tskip),
1862- );
1863- self.scratch.cb_levels = levels;
1864- }
1865- if cr_cbf {
1866- let levels = std::mem::take(&mut self.scratch.cr_levels);
1867- encode_residual(
1868- enc,
1869- &levels[..n * n / 4],
1870- log2 - 1,
1871- 2,
1872- scan,
1873- self.sign_hiding,
1874- self.ts_flag(chroma_tskip),
1875- );
1876- self.scratch.cr_levels = levels;
1877- }
1878- let bits = enc.bit_count() - bits_before;
1879- enc.restore(&start);
1880- let cost = self.chroma_rd_weight * (cb_ssd + cr_ssd) + self.lambda * bits as f64;
1881- if cost < best.0 {
1882- best = (cost, mode, i);
1883- }
1884- }
1885- enc.set_counting(false);
1886- (best.1, best.2)
1778+ /// Write `intra_chroma_pred_mode` for the derived mode: the single bin 0.
1779+ fn write_chroma_mode_syntax(&self, enc: &mut CabacEncoder) {
1780+ enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 0);
18871781 }
18881782
18891783 // ---- reference samples -----------------------------------------------
crates/ec-h265/src/encoder.rsmodified+0 -31
@@ -126,33 +126,6 @@ pub struct EncoderConfig {
126126 /// knee is where the default sits. `EC_H265_RDO_CANDIDATES` in the gate
127127 /// test re-runs the sweep.
128128 pub rdo_candidates: usize,
129- /// Whether the chroma prediction mode is chosen by rate-distortion over the
130- /// five modes the syntax allows, instead of always taking the derived mode.
131- ///
132- /// Off by default: measured against x265 on four real clips it is a wash,
133- /// and on screen capture it is a loss (BD-PSNR luma / all-plane, derived
134- /// mode -> search, `chroma_rd_weight: 1.0`):
135- ///
136- /// | clip | derived | search |
137- /// |------|---------|--------|
138- /// | 4K film | -0.685 / -0.703 | -0.757 / **-0.606** |
139- /// | 1138x640 web | -0.507 / -0.556 | -0.584 / **-0.505** |
140- /// | 1440p screen capture | **-2.465 / -2.360** | -2.617 / -2.460 |
141- /// | 1080p phone | -0.366 / **-0.387** | -0.477 / -0.384 |
142- ///
143- /// Luma falls on every clip because the extra chroma bits come out of the
144- /// same budget at a fixed QP; all-plane gains 0.10 dB and 0.05 dB on the two
145- /// camera clips, is flat on the third and loses 0.10 dB on the screen
146- /// capture, for 10-15% more encode time. The average is +0.01 dB all-plane,
147- /// which is not a default. [`EncoderConfig::chroma_rd_weight`] was swept at
148- /// 0.25/0.5/1.0/2.0 and did not change the shape of that table: the screen
149- /// capture loses at every weight, so the cause is not the lambda.
150- pub chroma_mode_search: bool,
151- /// Weight on chroma SSD in that decision, against the luma lambda. Swept
152- /// at 0.25/0.5/1.0/2.0 on three clips; 1.0 is the best of them everywhere
153- /// the search wins at all (4K film all-plane: -0.606 at 1.0 against -0.649
154- /// at 0.25, -0.619 at 0.5, -0.657 at 2.0).
155- pub chroma_rd_weight: f64,
156129 /// Smallest coding unit the search will produce, 8 or 16. Sixteen is a
157130 /// *coarser* search that is not always faster: on real pictures the 8x8
158131 /// blocks pay for their search in residual bits (measured: -27% bits and
@@ -299,8 +272,6 @@ impl EncoderConfig {
299272 threads: default_threads(),
300273 ctb_size: 32,
301274 rdo_candidates: 5,
302- chroma_mode_search: false,
303- chroma_rd_weight: 1.0,
304275 min_cu_size: 8,
305276 intra_nxn: true,
306277 sign_hiding: false,
@@ -851,8 +822,6 @@ impl Encoder {
851822 slice_qp,
852823 true,
853824 self.cfg.rdo_candidates,
854- self.cfg.chroma_mode_search,
855- self.cfg.chroma_rd_weight,
856825 self.cfg.intra_nxn,
857826 self.cfg.sign_hiding,
858827 self.cfg.rdoq,