diff --git a/crates/ec-h265/examples/hevc_gop.rs b/crates/ec-h265/examples/hevc_gop.rs index 041fb524..8b5ec1b9 100644 --- a/crates/ec-h265/examples/hevc_gop.rs +++ b/crates/ec-h265/examples/hevc_gop.rs @@ -93,13 +93,6 @@ fn run() -> Result<(), String> { cfg.keep_recon = true; // Optional trailing flags: `sh` turns sign data hiding on. cfg.sign_hiding = args.iter().skip(9).any(|a| a == "sh"); - // Sweep override of the P-mode decision lambda scale (experiments only). - if let Some(v) = std::env::var("EC_INTER_LAMBDA_SCALE") - .ok() - .and_then(|v| v.parse::().ok()) - { - cfg.inter_lambda_scale = v; - } // Experiments: worker threads and the P-picture wavefront (`0` or `off`). if let Some(v) = std::env::var("EC_H265_THREADS") .ok() diff --git a/crates/ec-h265/src/ctu.rs b/crates/ec-h265/src/ctu.rs index 06f28a8f..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, @@ -196,9 +192,6 @@ pub struct CtuEncoder<'a> { /// The skip decision for the 4x4 luma block currently in the scratch. ts_luma: bool, rqt: bool, - /// Whether a 64x64 coding tree block may stay one coding unit rather than - /// always splitting to 32x32. - cu64: bool, /// The counting CABAC engine that search prices its trials with. rdoq_enc: CabacEncoder, /// `log2` of the smallest coding unit the search will produce. @@ -232,15 +225,11 @@ 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, - rdoq_estimate: bool, transform_skip: TransformSkip, rqt: bool, - cu64: bool, min_cb_log2: u32, bit_depth: u32, ) -> CtuEncoder<'a> { @@ -265,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, @@ -274,10 +261,9 @@ impl<'a> CtuEncoder<'a> { ts_rd: transform_skip == TransformSkip::Rd, ts_luma: false, rqt, - cu64, rdoq_enc: { let mut enc = CabacEncoder::counter(Contexts::new(qp.clamp(0, 51))); - enc.set_estimating(rdoq_estimate); + enc.set_estimating(true); enc }, min_cb_log2: min_cb_log2.clamp(MIN_CB_LOG2, 5), @@ -439,16 +425,6 @@ impl<'a> CtuEncoder<'a> { return self.lambda * (enc.bit_count() - before) as f64 + self.code_cu(x, y, log2, depth, enc); } - if log2 > 5 && !self.cu64 { - // 64x64 coding units are not searched; the decision starts at 32x32. - let before = enc.bit_count(); - enc.encode_bin(split_ctx, 1); - let mut cost = self.lambda * (enc.bit_count() - before) as f64; - for (dx, dy) in [(0, 0), (half, 0), (0, half), (half, half)] { - cost += self.code_quadtree(x + dx, y + dy, log2 - 1, depth + 1, enc); - } - return cost; - } let start = enc.snapshot(); // The z-scan availability map has to go back with everything else: the @@ -646,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; @@ -742,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]; @@ -855,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); @@ -1811,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/dec/cu_syntax.rs b/crates/ec-h265/src/dec/cu_syntax.rs index c5783aa4..4869c4b1 100644 --- a/crates/ec-h265/src/dec/cu_syntax.rs +++ b/crates/ec-h265/src/dec/cu_syntax.rs @@ -184,7 +184,6 @@ pub fn parse_slice_into( motion: &mut SliceMotion<'_>, pic: &mut PicState, refs: Option<&InterRefs>, - sink: &mut dyn FnMut(&CodingUnit), rows: &mut dyn FnMut(&mut PicState, usize), ) -> Result { let min_cb_log2 = sps.log2_min_cb_size_minus3 + 3; @@ -310,7 +309,7 @@ pub fn parse_slice_into( if header.sao_luma || header.sao_chroma { p.sao(&mut dec, rs, rx, ry)?; } - p.coding_quadtree(&mut dec, x0, y0, ctb_log2, 0, sink)?; + p.coding_quadtree(&mut dec, x0, y0, ctb_log2, 0)?; // The row is complete with its last CTB. Single-tile pictures are in raster order. if raster && rx + 1 == cols { p.motion.row_done(p.pic, y0 + ctb_size); @@ -574,8 +573,7 @@ impl Parser<'_, '_> { y0: usize, log2: u32, depth: u32, - sink: &mut dyn FnMut(&CodingUnit), - ) -> Result<()> { + ) -> Result<()> { let n = 1usize << log2; let fits = x0 + n <= self.pic.width && y0 + n <= self.pic.height; let split = if fits && log2 > self.min_cb_log2 { @@ -604,12 +602,12 @@ impl Parser<'_, '_> { let half = n / 2; for (dx, dy) in [(0, 0), (half, 0), (0, half), (half, half)] { if x0 + dx < self.pic.width && y0 + dy < self.pic.height { - self.coding_quadtree(dec, x0 + dx, y0 + dy, log2 - 1, depth + 1, sink)?; + self.coding_quadtree(dec, x0 + dx, y0 + dy, log2 - 1, depth + 1)?; } } Ok(()) } else { - self.coding_unit(dec, x0, y0, log2, depth, sink) + self.coding_unit(dec, x0, y0, log2, depth) } } @@ -631,8 +629,7 @@ impl Parser<'_, '_> { y0: usize, log2: u32, depth: u32, - sink: &mut dyn FnMut(&CodingUnit), - ) -> Result<()> { + ) -> Result<()> { let n = 1usize << log2; self.stats.cus += 1; let is_i = self.header.slice_type == SliceType::I; @@ -771,7 +768,6 @@ impl Parser<'_, '_> { self.pic.update_info(x0, y0, n, n, |b| b.qp = qp); self.stats.pus += cu.pus.len(); self.stats.residual_blocks += cu.residuals.len(); - sink(&cu); Ok(()) } diff --git a/crates/ec-h265/src/dec/cu_syntax/wpp.rs b/crates/ec-h265/src/dec/cu_syntax/wpp.rs index d1c372c6..a9ae0dd3 100644 --- a/crates/ec-h265/src/dec/cu_syntax/wpp.rs +++ b/crates/ec-h265/src/dec/cu_syntax/wpp.rs @@ -359,7 +359,7 @@ fn parse_row( if sao { p.sao(&mut dec, rs, x, ry)?; } - p.coding_quadtree(&mut dec, x0, y0, ctb_log2, 0, &mut |_| {})?; + p.coding_quadtree(&mut dec, x0, y0, ctb_log2, 0)?; // 9.3.2.2: the wavefront storage after the second CTB of a row. let stored = (x == 1 && cols > 1).then(|| dec.contexts.clone()); sync.leave(&*p.pic, &mut *p.motion, x, stored.as_ref())?; diff --git a/crates/ec-h265/src/dec/picjob.rs b/crates/ec-h265/src/dec/picjob.rs index 461c4554..dc8dda57 100644 --- a/crates/ec-h265/src/dec/picjob.rs +++ b/crates/ec-h265/src/dec/picjob.rs @@ -171,7 +171,6 @@ impl PicJob { &mut source, pic, refs.as_ref(), - &mut |_| {}, &mut rows, )?; } diff --git a/crates/ec-h265/src/enc_p.rs b/crates/ec-h265/src/enc_p.rs index 6241ccde..54bfca45 100644 --- a/crates/ec-h265/src/enc_p.rs +++ b/crates/ec-h265/src/enc_p.rs @@ -624,7 +624,7 @@ impl<'a> PCoder<'a> { bd, qpd, qpc, - lambda: lambda_for(qpd) * enc.cfg.inter_lambda_scale, + lambda: lambda_for(qpd), // The search measures SAD on 8-bit samples, so its lambda is the 8-bit one. search_lambda: lambda_for(qp).sqrt(), sign_hiding: enc.cfg.sign_hiding, diff --git a/crates/ec-h265/src/encoder.rs b/crates/ec-h265/src/encoder.rs index 56261806..29b2ef67 100644 --- a/crates/ec-h265/src/encoder.rs +++ b/crates/ec-h265/src/encoder.rs @@ -126,39 +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 - /// +1.5 dB at QP 22 on a 1080p camera frame, at the same wall time), which - /// is why 8 is the default. - pub min_cu_size: usize, /// Whether an 8x8 coding unit may split into four 4x4 intra partitions /// (PART_NxN). On: measured at +2.14 dB BD-PSNR on a 2560x1440 screen /// capture (-2.619 dB against x265 without it, -0.479 dB with) and @@ -187,16 +154,6 @@ pub struct EncoderConfig { /// coder's own price for the whole block, so dropping a level is priced /// with the significance map and last position that follow from it. pub rdoq: bool, - /// Whether RDOQ's per-candidate bit cost comes from an estimated - /// per-context fractional-bit table (x265/HM `entropyBits`-style: the - /// entropy of the bin under its context's current probability state) - /// instead of replaying the exact adaptive-CABAC coder for every trial. - /// Changes output bytes (the estimate is not exact). On by default: - /// measured 1.6x faster on `ec-bench`'s h265 encode row and, on the - /// synthetic `bd_psnr_vs_x265` clip, a small BD-PSNR-YUV *gain* over the - /// exact-replay path (+7.948 vs +7.880 dB against x265) rather than a - /// loss. `false` restores exact per-trial CABAC replay for A/B. - pub rdoq_estimate: bool, /// Whether 4x4 TUs may skip the integer transform. /// /// `Rd` by default: the tool is the screen-content lever, and it wants a @@ -227,19 +184,6 @@ pub struct EncoderConfig { /// worth +0.010 dB BD-PSNR on film and +0.080 dB on screen capture, which /// is where our gap against x265 is (see `lanes/h265-rqt-r1.report.md`). pub rqt: bool, - /// Whether a 64x64 coding tree block may stay one coding unit instead of - /// always splitting to 32x32. Only reachable with `ctb_size` 64. - /// - /// A 64x64 intra coding unit still predicts and transforms in four 32x32 - /// blocks -- 32x32 is HEVC's largest transform -- so what it buys is three - /// split flags and three mode signallings, and what it costs is one luma - /// direction for the whole 64x64. Measured in `lanes/h265-cu64-r1.report.md`. - /// - /// On by default: against x265 over the four-point ladder it is worth - /// +0.007 dB BD-PSNR on film and +0.098 dB on screen capture over always - /// splitting a 64x64 tree block, and the default `ctb_size` of 32 leaves - /// it unreachable anyway. - pub cu64: bool, /// What the samples mean, written into the VUI. pub video_signal_type: Option, /// Sample aspect ratio. @@ -279,14 +223,6 @@ pub struct EncoderConfig { /// against off, the single continuous substream the sequential coder writes. /// Off also makes P pictures single-threaded. pub inter_wpp: bool, - /// Scale on the lambda the P-picture mode decisions use, against - /// `lambda_for(qp)` (the intra lambda, in squared-error units per bit). The - /// motion search uses `sqrt(lambda_for(qp))` instead, because it costs SAD. - /// - /// Measured with `scripts/hevc-bdrate.sh` (ffmpeg PSNR, bframes=0 x265 - /// anchor, 352x288, QPs 22/27/32/37), BD-rate against x265: +21.9% at scale 1, - /// +23.4% at 1/4, +28.9% at 1/16. Scale 1 is the default. - pub inter_lambda_scale: f64, /// Whether a sequence with P pictures (`keyint > 1`) codes its IDR pictures with the /// fast intra tool set: no RDOQ, one full-RD intra candidate, no NxN split, no transform /// skip trial, no transform-tree split. An IDR is one picture in `keyint`, so what @@ -330,16 +266,11 @@ 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, rdoq: true, - rdoq_estimate: true, transform_skip: TransformSkip::Rd, rqt: true, - cu64: true, video_signal_type: None, sample_aspect_ratio: None, timing: None, @@ -351,7 +282,6 @@ impl EncoderConfig { keep_recon: false, keyint: 1, inter_wpp: true, - inter_lambda_scale: 1.0, gop_fast_idr: true, bit_depth: 8, speed: Preset::Default, @@ -885,16 +815,12 @@ 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, - self.cfg.rdoq_estimate, self.cfg.transform_skip, self.cfg.rqt, - self.cfg.cu64, - self.cfg.min_cu_size.max(8).trailing_zeros(), + 3, u32::from(self.cfg.bit_depth), ); for col in 0..cols {