From 06fedd5bf46b1a91dd35a5621654d5857ea8ab88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:12:42 +0300 Subject: [PATCH] S9: drop EncoderConfig::chroma_mode_search and chroma_rd_weight; default off, so the chroma RD search (choose_chroma_mode) was never called --- crates/ec-h265/src/ctu.rs | 124 +++------------------------------- crates/ec-h265/src/encoder.rs | 31 --------- 2 files changed, 9 insertions(+), 146 deletions(-) diff --git a/crates/ec-h265/src/ctu.rs b/crates/ec-h265/src/ctu.rs index 7d9319cb..6ec938a0 100644 --- a/crates/ec-h265/src/ctu.rs +++ b/crates/ec-h265/src/ctu.rs @@ -180,10 +180,6 @@ pub struct CtuEncoder<'a> { strong_smoothing: bool, /// How many of the 35 modes get a full rate-distortion trial. candidates: usize, - /// Whether the chroma mode is chosen by rate-distortion or always derived. - chroma_rd: bool, - /// Weight on chroma SSD in that decision, against luma's lambda. - chroma_rd_weight: f64, /// Whether an 8x8 coding unit may split into four 4x4 intra partitions. intra_nxn: bool, sign_hiding: bool, @@ -229,8 +225,6 @@ impl<'a> CtuEncoder<'a> { qp: i32, strong_smoothing: bool, candidates: usize, - chroma_rd: bool, - chroma_rd_weight: f64, intra_nxn: bool, sign_hiding: bool, rdoq: bool, @@ -260,8 +254,6 @@ impl<'a> CtuEncoder<'a> { lambda_satd: lambda_for(qp_bd).sqrt(), strong_smoothing, candidates: candidates.clamp(1, 35), - chroma_rd, - chroma_rd_weight, intra_nxn, sign_hiding, rdoq, @@ -630,12 +622,8 @@ impl<'a> CtuEncoder<'a> { // The chroma mode belongs to the coding unit and derives from the // first partition's luma mode (8.4.3). - let (chroma_mode, chroma_idx) = if self.chroma_rd { - self.choose_chroma_mode(x, y, 8, MIN_CB_LOG2, modes[0], self.transform_skip, enc) - } else { - (modes[0], None) - }; - self.write_chroma_mode_syntax(enc, chroma_idx); + let chroma_mode = modes[0]; + self.write_chroma_mode_syntax(enc); let cb_cbf = self .code_chroma(x, y, 8, chroma_mode, 1, self.transform_skip) .1; @@ -726,12 +714,8 @@ impl<'a> CtuEncoder<'a> { // levels in the luma scratch. let mode = self.choose_luma_mode(x, y, HALF, LOG2 - 1, &mpm, enc); self.write_mode_syntax(enc, mode, &mpm); - let (chroma_mode, chroma_idx) = if self.chroma_rd { - self.choose_chroma_mode(x, y, HALF, LOG2 - 1, mode, chroma_tskip, enc) - } else { - (mode, None) - }; - self.write_chroma_mode_syntax(enc, chroma_idx); + let chroma_mode = mode; + self.write_chroma_mode_syntax(enc); let mut child_levels = [[0i32; 1024]; 4]; let mut child_tskip = [self.transform_skip; 4]; @@ -839,12 +823,8 @@ impl<'a> CtuEncoder<'a> { let mpm = self.mpm_at(x, y); let mode = self.choose_luma_mode(x, y, n, log2, &mpm, enc); self.write_mode_syntax(enc, mode, &mpm); - let (chroma_mode, chroma_idx) = if self.chroma_rd { - self.choose_chroma_mode(x, y, n, log2, mode, chroma_tskip, enc) - } else { - (mode, None) - }; - self.write_chroma_mode_syntax(enc, chroma_idx); + let chroma_mode = mode; + self.write_chroma_mode_syntax(enc); // Luma was reconstructed by the mode search; chroma is coded now. let luma_cbf = self.scratch.luma_levels[..n * n].iter().any(|&v| v != 0); @@ -1795,95 +1775,9 @@ impl<'a> CtuEncoder<'a> { // ---- chroma mode decision -------------------------------------------- - /// Write `intra_chroma_pred_mode`: the single bin 0 for the derived mode, - /// or bin 1 and two bypass bits picking one of the four explicit modes. - fn write_chroma_mode_syntax(&self, enc: &mut CabacEncoder, idx: Option) { - match idx { - None => enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 0), - Some(i) => { - enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 1); - enc.encode_bypass_bits(i, 2); - } - } - } - - /// Pick the chroma prediction mode by rate-distortion over the five the - /// syntax allows: planar, vertical, horizontal and DC — each replaced by - /// angular 34 when it collides with the luma mode — plus the derived mode, - /// which is the luma mode itself and costs one bin instead of three. - /// - /// Returns the mode and the index to code, `None` meaning derived. Neither - /// the winner's reconstruction nor its levels survive the trial: the caller - /// codes the winner again for real. - #[allow(clippy::too_many_arguments)] - fn choose_chroma_mode( - &mut self, - x: usize, - y: usize, - n: usize, - log2: u32, - luma_mode: u8, - chroma_tskip: bool, - enc: &mut CabacEncoder, - ) -> (u8, Option) { - let mut explicit = [0u8, 26, 10, 1]; - for mode in explicit.iter_mut() { - if *mode == luma_mode { - *mode = 34; - } - } - let start = enc.snapshot(); - enc.set_counting(true); - let mut best = (f64::MAX, luma_mode, None); - for (i, mode) in explicit - .iter() - .copied() - .enumerate() - .map(|(i, m)| (Some(i as u32), m)) - .chain(std::iter::once((None, luma_mode))) - { - let bits_before = enc.bit_count(); - self.write_chroma_mode_syntax(enc, i); - let (cb_ssd, cb_cbf) = self.code_chroma(x, y, n, mode, 1, chroma_tskip); - let (cr_ssd, cr_cbf) = self.code_chroma(x, y, n, mode, 2, chroma_tskip); - enc.encode_bin(ctx::CBF_CHROMA, u32::from(cb_cbf)); - enc.encode_bin(ctx::CBF_CHROMA, u32::from(cr_cbf)); - let scan = intra::scan_index(mode, log2 - 1, false); - if cb_cbf { - let levels = std::mem::take(&mut self.scratch.cb_levels); - encode_residual( - enc, - &levels[..n * n / 4], - log2 - 1, - 1, - scan, - self.sign_hiding, - self.ts_flag(chroma_tskip), - ); - self.scratch.cb_levels = levels; - } - if cr_cbf { - let levels = std::mem::take(&mut self.scratch.cr_levels); - encode_residual( - enc, - &levels[..n * n / 4], - log2 - 1, - 2, - scan, - self.sign_hiding, - self.ts_flag(chroma_tskip), - ); - self.scratch.cr_levels = levels; - } - let bits = enc.bit_count() - bits_before; - enc.restore(&start); - let cost = self.chroma_rd_weight * (cb_ssd + cr_ssd) + self.lambda * bits as f64; - if cost < best.0 { - best = (cost, mode, i); - } - } - enc.set_counting(false); - (best.1, best.2) + /// Write `intra_chroma_pred_mode` for the derived mode: the single bin 0. + fn write_chroma_mode_syntax(&self, enc: &mut CabacEncoder) { + enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 0); } // ---- reference samples ----------------------------------------------- diff --git a/crates/ec-h265/src/encoder.rs b/crates/ec-h265/src/encoder.rs index 052beee2..9fd45520 100644 --- a/crates/ec-h265/src/encoder.rs +++ b/crates/ec-h265/src/encoder.rs @@ -126,33 +126,6 @@ pub struct EncoderConfig { /// knee is where the default sits. `EC_H265_RDO_CANDIDATES` in the gate /// test re-runs the sweep. pub rdo_candidates: usize, - /// Whether the chroma prediction mode is chosen by rate-distortion over the - /// five modes the syntax allows, instead of always taking the derived mode. - /// - /// Off by default: measured against x265 on four real clips it is a wash, - /// and on screen capture it is a loss (BD-PSNR luma / all-plane, derived - /// mode -> search, `chroma_rd_weight: 1.0`): - /// - /// | clip | derived | search | - /// |------|---------|--------| - /// | 4K film | -0.685 / -0.703 | -0.757 / **-0.606** | - /// | 1138x640 web | -0.507 / -0.556 | -0.584 / **-0.505** | - /// | 1440p screen capture | **-2.465 / -2.360** | -2.617 / -2.460 | - /// | 1080p phone | -0.366 / **-0.387** | -0.477 / -0.384 | - /// - /// Luma falls on every clip because the extra chroma bits come out of the - /// same budget at a fixed QP; all-plane gains 0.10 dB and 0.05 dB on the two - /// camera clips, is flat on the third and loses 0.10 dB on the screen - /// capture, for 10-15% more encode time. The average is +0.01 dB all-plane, - /// which is not a default. [`EncoderConfig::chroma_rd_weight`] was swept at - /// 0.25/0.5/1.0/2.0 and did not change the shape of that table: the screen - /// capture loses at every weight, so the cause is not the lambda. - pub chroma_mode_search: bool, - /// Weight on chroma SSD in that decision, against the luma lambda. Swept - /// at 0.25/0.5/1.0/2.0 on three clips; 1.0 is the best of them everywhere - /// the search wins at all (4K film all-plane: -0.606 at 1.0 against -0.649 - /// at 0.25, -0.619 at 0.5, -0.657 at 2.0). - pub chroma_rd_weight: f64, /// Smallest coding unit the search will produce, 8 or 16. Sixteen is a /// *coarser* search that is not always faster: on real pictures the 8x8 /// blocks pay for their search in residual bits (measured: -27% bits and @@ -299,8 +272,6 @@ impl EncoderConfig { threads: default_threads(), ctb_size: 32, rdo_candidates: 5, - chroma_mode_search: false, - chroma_rd_weight: 1.0, min_cu_size: 8, intra_nxn: true, sign_hiding: false, @@ -851,8 +822,6 @@ impl Encoder { slice_qp, true, self.cfg.rdo_candidates, - self.cfg.chroma_mode_search, - self.cfg.chroma_rd_weight, self.cfg.intra_nxn, self.cfg.sign_hiding, self.cfg.rdoq,