diff --git a/crates/ec-h264/Cargo.toml b/crates/ec-h264/Cargo.toml index 62f5ba9c..6e97d395 100644 --- a/crates/ec-h264/Cargo.toml +++ b/crates/ec-h264/Cargo.toml @@ -6,12 +6,6 @@ edition.workspace = true license.workspace = true authors.workspace = true -[features] -# Internal-only RD experiment switches, compiled only by the measurement harness. -rd-ablation = [] -# Per-stage timing counters in the macroblock coder (measurement builds only). -enc-profile = [] - [dependencies] ec-core = { workspace = true } ec-h264-syntax = { workspace = true } diff --git a/crates/ec-h264/src/enc/mb.rs b/crates/ec-h264/src/enc/mb.rs index 68e01846..d89f8afc 100644 --- a/crates/ec-h264/src/enc/mb.rs +++ b/crates/ec-h264/src/enc/mb.rs @@ -41,7 +41,7 @@ use super::quant::{ forward_4x4, forward_8x8, forward_hadamard_2x2, forward_hadamard_4x4, quant_4x4, quant_4x4_offset, quant_8x8, quant_8x8_offset, quant_chroma_dc, quant_luma_dc, }; -use super::timing::mb_timed; + use super::{ entropy::{EncEntropy, sub_block_4x4}, lambda_ssd, @@ -128,24 +128,12 @@ pub(crate) struct MbEnc<'a> { /// Lagrangian multiplier; it is in the squared-error domain when 8x8 /// transform decisions are enabled. pub lambda: f64, - /// Whether `lambda` is the standard squared-error multiplier. - pub lambda_standard: bool, - /// Extra rate saving an 8x8 trial must buy before it may replace 4x4. - pub t8x8_margin_bits: f64, pub preset: Preset, /// Fast-preset speed knobs (see [`FastKnobs`]). pub fast: FastKnobs, /// The PPS carries transform_8x8_mode_flag: transform_size_8x8_flag must /// be written for every eligible macroblock (7.3.5). pub transform_8x8: bool, - /// Whether intra macroblocks may select 8x8. The PPS can remain enabled - /// while an ablation measures the classes independently. - pub transform_8x8_intra: bool, - /// Whether inter macroblocks may select 8x8. - pub transform_8x8_inter: bool, - /// Whether the ablation restricts 8x8 eligibility to even macroblock - /// rows; odd rows then code exactly as they would with 8x8 disabled. - pub transform_8x8_roweven: bool, pub ls: LevelScale4x4, pub ls8: LevelScale8x8, /// Neighbourhood of the macroblock being written (CABAC). @@ -160,18 +148,6 @@ pub(crate) struct MbEnc<'a> { pub qp_delta_inc: u8, } -impl MbEnc<'_> { - /// Intra 8x8 eligibility of macroblock row `mb_y`. - fn t8x8_intra(&self, mb_y: usize) -> bool { - self.transform_8x8_intra && (mb_y.is_multiple_of(2) || !self.transform_8x8_roweven) - } - - /// Inter 8x8 eligibility of macroblock row `mb_y`. - fn t8x8_inter(&self, mb_y: usize) -> bool { - self.transform_8x8_inter && (mb_y.is_multiple_of(2) || !self.transform_8x8_roweven) - } -} - /// Quantised levels of one macroblock, scan order per block. #[derive(Clone)] struct Levels { @@ -399,7 +375,7 @@ fn motion_rate(e: &MbEnc<'_>, bits: f64) -> i32 { /// The multiplier `motion_rate` applies, for loops that price many vectors. #[inline] fn motion_lambda(e: &MbEnc<'_>) -> f64 { - if e.lambda_standard { + if e.transform_8x8 { e.lambda.sqrt() } else { e.lambda @@ -565,7 +541,7 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy let mut inter: Option> = None; if e.slice_type == SliceType::P { - inter = Some(mb_timed!(0, choose_inter(pic, e, mb_x, mb_y))); + inter = Some(choose_inter(pic, e, mb_x, mb_y)); } // Intra against inter is decided on real coded cost: both branches are @@ -597,21 +573,21 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy let gate = e.preset == Preset::Fast && e.fast.intra_gate > 0.0; let intra_cost = match inter.as_ref() { Some(c) if gate && e.fast.lazy_i16 => { - let dc = mb_timed!(1, intra_pre_cost(pic, e, &nbr, mb_x, mb_y, &[2])); + let dc = intra_pre_cost(pic, e, &nbr, mb_x, mb_y, &[2]); if f64::from(c[0].satd) <= e.fast.intra_gate * f64::from(dc.satd) { dc } else { - mb_timed!(1, intra_pre_cost(pic, e, &nbr, mb_x, mb_y, &[2, 0, 1, 3])) + intra_pre_cost(pic, e, &nbr, mb_x, mb_y, &[2, 0, 1, 3]) } } - _ => mb_timed!(1, intra_pre_cost(pic, e, &nbr, mb_x, mb_y, &[2, 0, 1, 3])), + _ => intra_pre_cost(pic, e, &nbr, mb_x, mb_y, &[2, 0, 1, 3]), }; let Some(choices) = inter else { - mb_timed!(3, encode_intra_mb(pic, e, w, mb_addr, nbr, intra_cost)); + encode_intra_mb(pic, e, w, mb_addr, nbr, intra_cost); return; }; - let lambda = if e.lambda_standard { + let lambda = if e.transform_8x8 { e.lambda } else { lambda_ssd(e.qp) @@ -622,10 +598,10 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy if gated { // Inter only and one candidate at Fast: nothing is compared, so the // macroblock is coded straight into the picture and the writer. - mb_timed!(2, encode_inter_mb(pic, e, w, mb_addr, nbr, choices[0])); + encode_inter_mb(pic, e, w, mb_addr, nbr, choices[0]); return; } - let before = mb_timed!(4, save_mb_state(pic, e, mb_addr)); + let before = save_mb_state(pic, e, mb_addr); // The shape and the skip flag are ranked the same way intra and inter are: // by coding each candidate and measuring it. The SATD total that ordered // them inside the search is a prediction-domain estimate that never sees @@ -638,26 +614,23 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy }; let mut best_inter: Option<(f64, MbState, EncEntropy)> = None; for &choice in &choices[..rd_cands.max(1)] { - mb_timed!(4, load_mb_state(pic, e, mb_addr, &before)); - let mut w_c = mb_timed!(5, w.probe_copy()); + load_mb_state(pic, e, mb_addr, &before); + let mut w_c = w.probe_copy(); let start = w_c.bit_len(); - mb_timed!(2, encode_inter_mb(pic, e, &mut w_c, mb_addr, nbr, choice)); + encode_inter_mb(pic, e, &mut w_c, mb_addr, nbr, choice); let bits = (w_c.bit_len() - start) as f64; let rd = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda * bits; if best_inter.as_ref().is_none_or(|b| rd < b.0) { - best_inter = Some((rd, mb_timed!(4, save_mb_state(pic, e, mb_addr)), w_c)); + best_inter = Some((rd, save_mb_state(pic, e, mb_addr), w_c)); } } let (inter_rd, after_inter, w_inter) = best_inter.expect("at least one inter candidate"); - mb_timed!(4, load_mb_state(pic, e, mb_addr, &before)); + load_mb_state(pic, e, mb_addr, &before); - let mut w_intra = mb_timed!(5, w.probe_copy()); + let mut w_intra = w.probe_copy(); let intra_bits = { let start = w_intra.bit_len(); - mb_timed!( - 3, - encode_intra_mb(pic, e, &mut w_intra, mb_addr, nbr, intra_cost) - ); + encode_intra_mb(pic, e, &mut w_intra, mb_addr, nbr, intra_cost); (w_intra.bit_len() - start) as f64 }; let intra_rd = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda * intra_bits; @@ -665,7 +638,7 @@ pub(crate) fn encode_mb(pic: &mut Picture, e: &mut MbEnc<'_>, w: &mut EncEntropy if intra_rd < inter_rd { w.adopt(w_intra); } else { - mb_timed!(4, load_mb_state(pic, e, mb_addr, &after_inter)); + load_mb_state(pic, e, mb_addr, &after_inter); w.adopt(w_inter); } } @@ -1097,18 +1070,15 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Vec, mb_x: usize, mb_y: usize) -> Vec= 4 { - let mut improved = true; - while improved { - improved = false; - passes += 1; - if fast && passes > FAST_ME_PASSES { - break; + while step >= 4 { + let mut improved = true; + while improved { + improved = false; + passes += 1; + if fast && passes > FAST_ME_PASSES { + break; + } + for (dx, dy) in ME_PATTERN { + let (dx, dy) = (dx * step, dy * step); + let cand = [best.0[0] + dx, best.0[1] + dy]; + if (cand[0] - mvp[0]).abs() > range * 4 + || (cand[1] - mvp[1]).abs() > range * 4 + { + continue; } - for (dx, dy) in ME_PATTERN { - let (dx, dy) = (dx * step, dy * step); - let cand = [best.0[0] + dx, best.0[1] + dy]; - if (cand[0] - mvp[0]).abs() > range * 4 - || (cand[1] - mvp[1]).abs() > range * 4 - { - continue; - } - let c = cost_of(cand, false); - if c < best.1 { - best = (cand, c); - improved = true; - } + let c = cost_of(cand, false); + if c < best.1 { + best = (cand, c); + improved = true; } } - step /= 2; } - ); + step /= 2; + } // Sub-sample refinement runs on SATD, not SAD: interpolation // smooths, and a sum of absolute differences reads the smoothing // as a better match where a transform-domain cost does not. Worth @@ -1157,7 +1124,7 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Vec, mb_x: usize, mb_y: usize) -> Vec false, Some(_) if empty => true, Some(ssd_skip) => { - let coded = if e.t8x8_inter(mb_y) { + let coded = if e.transform_8x8 { let bits = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv); ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 } else { diff --git a/crates/ec-h264/src/enc/mod.rs b/crates/ec-h264/src/enc/mod.rs index b00478fd..4c9e7a83 100644 --- a/crates/ec-h264/src/enc/mod.rs +++ b/crates/ec-h264/src/enc/mod.rs @@ -27,7 +27,6 @@ mod mb; mod pool; mod quant; mod rc; -pub(crate) mod timing; mod vlc; use ec_core::BitWriter; @@ -37,8 +36,6 @@ use ec_h264_syntax::{SliceType, Sps}; use crate::decoder::deblock_picture; use crate::dpb::{Picture, SliceParams as DeblockParams}; -#[cfg(feature = "rd-ablation")] -use crate::entropy::FLAG_SKIP; use crate::entropy::{FLAG_INTER, FLAG_TRANS8X8}; use crate::transform::{LevelScale4x4, LevelScale8x8}; @@ -48,8 +45,6 @@ use mb::{MbEnc, Source, encode_mb}; use pool::BandPool; use rc::RateControl; use std::sync::Arc; -use std::time::Instant; -pub use timing::EncodeTimings; pub use mb::Preset; @@ -87,16 +82,6 @@ pub struct EncoderConfig { /// Turning it off restricts the stream to the Baseline/Main-profile 4x4 /// path, which costs 0.7 dB BD-PSNR on film and 0.2 dB on screen capture. pub transform_8x8: bool, - #[cfg(feature = "rd-ablation")] - rd_lambda_standard: bool, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_intra: bool, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_inter: bool, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_roweven: bool, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_margin_bits: f64, } impl EncoderConfig { @@ -114,16 +99,6 @@ impl EncoderConfig { threads: 0, qp: 26, transform_8x8: true, - #[cfg(feature = "rd-ablation")] - rd_lambda_standard: false, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_intra: false, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_inter: false, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_roweven: false, - #[cfg(feature = "rd-ablation")] - rd_transform_8x8_margin_bits: 0.0, } } } @@ -169,7 +144,6 @@ pub struct Encoder { have_reference: bool, /// Macroblocks coded with transform_size_8x8_flag set: (intra, inter). t8x8_mbs: (u64, u64), - timings: timing::EncodeTimings, } /// A picture handed to the encoder: three planes, no padding assumptions. @@ -204,10 +178,6 @@ impl<'a> PictureView<'a> { impl Encoder { /// Build an encoder for `cfg`, refusing what it cannot code. pub fn new(cfg: EncoderConfig) -> Result { - #[cfg(feature = "rd-ablation")] - let mut cfg = cfg; - #[cfg(feature = "rd-ablation")] - apply_rd_ablation(&mut cfg); if cfg.width < 16 || cfg.height < 16 { return Err(Error::unsupported( "picture smaller than one macroblock", @@ -288,7 +258,6 @@ impl Encoder { next_id: 0, have_reference: false, t8x8_mbs: (0, 0), - timings: timing::EncodeTimings::default(), cfg, }) } @@ -304,11 +273,6 @@ impl Encoder { &self.cfg } - /// Where the encode time went so far; see [`EncodeTimings`]. - pub fn timings(&self) -> EncodeTimings { - self.timings - } - /// The reconstruction of the last coded picture, cropped, as I420 planes. /// This is what a conformant decoder produces for that picture — the tests /// compare it against this crate's decoder and against the oracle. @@ -351,9 +315,7 @@ impl Encoder { if frame.width != self.cfg.width || frame.height != self.cfg.height { return Err(Error::corrupt("frame size differs from the encoder's")); } - let t_pad = Instant::now(); self.pad_source(frame)?; - self.timings.pad += t_pad.elapsed().as_secs_f64(); if self.frames == 0 { self.cqo = chroma_qp_offset_for_source(&self.src, self.cfg.width, self.cfg.height); let pps_rbsp = write_pps(self.cfg.cabac, self.cfg.transform_8x8, self.cqo); @@ -399,9 +361,6 @@ impl Encoder { mb_w, }); - let mut busiest = 0.0f64; - let mut busy = 0.0f64; - let t_bands = Instant::now(); let mut answers = Vec::with_capacity(bands.len()); if self.threads == 1 { // One band: coded on this thread, with no workers. @@ -434,8 +393,6 @@ impl Encoder { } let mut outputs: Vec> = Vec::with_capacity(bands.len()); for (band, answer) in answers.into_iter().enumerate() { - busiest = busiest.max(answer.secs); - busy += answer.secs; outputs.push(answer.rbsp); self.workers[band] = answer.pic; } @@ -447,10 +404,6 @@ impl Encoder { if let Some(reference) = shared.reference { self.reference = reference; } - self.timings.bands_wall += t_bands.elapsed().as_secs_f64(); - self.timings.bands_busy += busy; - self.timings.bands_slowest += busiest; - let t_stitch = Instant::now(); // Stitch the bands into worker 0's picture, then filter the whole // picture the way a decoder does. @@ -469,7 +422,6 @@ impl Encoder { for (band, worker) in bands.iter().skip(1).zip(rest.iter()) { merge_band(master, worker, *band); } - self.timings.stitch += t_stitch.elapsed().as_secs_f64(); for &f in &master.mb_flags { if f & FLAG_TRANS8X8 != 0 { if f & FLAG_INTER != 0 { @@ -479,19 +431,7 @@ impl Encoder { } } } - // H5 measurement hook: the chosen candidate's trial SSD is the - // pre-deblock SSD of the macroblock in this picture (round 4 verified - // trial bits and SSD against the final picture), so capturing the - // per-macroblock SSD right before and right after the filter is the - // exact pre/post comparison with no hooks inside the RD trials. - #[cfg(feature = "rd-ablation")] - let rd_pre = rd_trace_capture(master, &self.src); - let t_deblock = Instant::now(); deblock_picture(master); - self.timings.deblock += t_deblock.elapsed().as_secs_f64(); - #[cfg(feature = "rd-ablation")] - rd_trace_write(master, &self.src, self.frames, rd_pre); - let t_output = Instant::now(); master.extend_borders(); master.complete = true; std::mem::swap(master, &mut self.reference); @@ -513,8 +453,6 @@ impl Encoder { } let bits = au.len() as u64 * 8; - self.timings.output += t_output.elapsed().as_secs_f64(); - self.timings.pictures += 1; self.rc.update(idr, qp, bits); self.frames += 1; self.frame_num = (self.frame_num + 1) % (1 << headers::LOG2_MAX_FRAME_NUM); @@ -682,7 +620,6 @@ struct BandMsg { struct BandOut { pic: Picture, rbsp: Vec, - secs: f64, } /// Code one band on a worker picture. @@ -693,7 +630,6 @@ fn run_band(msg: BandMsg) -> BandOut { band, picture_id, } = msg; - let t_band = Instant::now(); let (row0, row1) = shared.bands[band]; let mb_w = shared.mb_w; pic.start(&shared.sps); @@ -719,11 +655,7 @@ fn run_band(msg: BandMsg) -> BandOut { cqo: shared.cqo, }, ); - BandOut { - pic, - rbsp, - secs: t_band.elapsed().as_secs_f64(), - } + BandOut { pic, rbsp } } struct BandJob<'a> { @@ -776,15 +708,10 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec { slice_id: job.slice_id, qp: job.qp, target_qp: job.qp, - lambda: lambda_for(job.qp, rd_lambda_standard(job.cfg)), - t8x8_margin_bits: rd_transform_8x8_margin_bits(job.cfg), + lambda: lambda_for(job.qp, job.cfg.transform_8x8), preset: job.cfg.preset, fast: fast_knobs(), - lambda_standard: rd_lambda_standard(job.cfg), transform_8x8: job.cfg.transform_8x8, - transform_8x8_intra: rd_transform_8x8_intra(job.cfg), - transform_8x8_inter: rd_transform_8x8_inter(job.cfg), - transform_8x8_roweven: rd_transform_8x8_roweven(job.cfg), ls: LevelScale4x4::new(&[16; 16]), ls8: LevelScale8x8::new(&[16; 64]), mb_ctx: crate::entropy::MbCtx::default(), @@ -801,7 +728,7 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec { let expected = job.band_target * n as f64 / rows as f64; let delta = job.rc.row_delta(spent, expected); e.target_qp = (job.qp + delta).clamp(10, 51); - e.lambda = lambda_for(e.target_qp, rd_lambda_standard(job.cfg)); + e.lambda = lambda_for(e.target_qp, job.cfg.transform_8x8); } for mb_x in 0..mb_w { let addr = mb_y * mb_w + mb_x; @@ -878,167 +805,6 @@ fn lambda_for(qp: i32, standard: bool) -> f64 { } } -#[cfg(feature = "rd-ablation")] -fn apply_rd_ablation(cfg: &mut EncoderConfig) { - if let Ok(lambda) = std::env::var("EC_H264_RD_LAMBDA") { - cfg.rd_lambda_standard = match lambda.as_str() { - "ladder" => false, - "standard" => true, - _ => panic!("EC_H264_RD_LAMBDA must be ladder or standard"), - }; - } - if let Ok(transform) = std::env::var("EC_H264_RD_T8X8") { - let (intra, inter, roweven) = match transform.as_str() { - "off" => (false, false, false), - "on" => (true, true, false), - "roweven" => (true, true, true), - "intra" => (true, false, false), - "inter" => (false, true, false), - _ => panic!("EC_H264_RD_T8X8 must be off, on, roweven, intra, or inter"), - }; - cfg.rd_transform_8x8_intra = intra; - cfg.rd_transform_8x8_inter = inter; - cfg.rd_transform_8x8_roweven = roweven; - cfg.transform_8x8 = intra || inter; - } else if cfg.transform_8x8 { - cfg.rd_transform_8x8_intra = true; - cfg.rd_transform_8x8_inter = true; - } - if let Ok(margin) = std::env::var("EC_H264_RD_T8X8_MARGIN_BITS") { - cfg.rd_transform_8x8_margin_bits = margin - .parse() - .expect("EC_H264_RD_T8X8_MARGIN_BITS must be a number"); - } -} - -/// Luma and chroma SSD of every macroblock against the source, before the -/// deblocking filter — the distortion the RD decision actually paid. -#[cfg(feature = "rd-ablation")] -fn rd_trace_capture(pic: &Picture, src: &Source) -> Option> { - if std::env::var_os("EC_H264_RD_TRACE").is_none() { - return None; - } - let mut out = Vec::with_capacity(pic.mb_w * pic.mb_h); - for mb_y in 0..pic.mb_h { - for mb_x in 0..pic.mb_w { - out.push(rd_trace_ssd(pic, src, mb_x, mb_y)); - } - } - Some(out) -} - -/// One line per macroblock: `frame addr intra trans8 skip qp pre_luma -/// pre_chroma post_luma post_chroma`, appended to `$EC_H264_RD_TRACE`. -#[cfg(feature = "rd-ablation")] -fn rd_trace_write(pic: &Picture, src: &Source, frame: u64, pre: Option>) { - use std::io::Write; - let Some(pre) = pre else { return }; - let mut out = std::fs::OpenOptions::new() - .create(true) - .append(true) - .open(std::env::var_os("EC_H264_RD_TRACE").expect("checked in capture")) - .expect("open EC_H264_RD_TRACE"); - for (addr, &(pre_luma, pre_chroma)) in pre.iter().enumerate() { - let (post_luma, post_chroma) = rd_trace_ssd(pic, src, addr % pic.mb_w, addr / pic.mb_w); - let f = pic.mb_flags[addr]; - // A discarded write result here is a SILENTLY SHORT trace: the reader - // of `$EC_H264_RD_TRACE` attributes decisions from it and cannot tell a - // short line count from a real one. No length check is possible (append - // mode has no knowable total), so this is the ec-av1 `append_unknown` - // shape: the write error is fatal instead. - writeln!( - out, - "{frame} {addr} {} {} {} {} {pre_luma} {pre_chroma} {post_luma} {post_chroma}", - u8::from(f & FLAG_INTER == 0), - u8::from(f & FLAG_TRANS8X8 != 0), - u8::from(f & FLAG_SKIP != 0), - pic.mb_qp[addr] - ) - .expect("write EC_H264_RD_TRACE"); - } -} - -/// Luma and chroma SSD of one macroblock against the source planes. -#[cfg(feature = "rd-ablation")] -fn rd_trace_ssd(pic: &Picture, src: &Source, mb_x: usize, mb_y: usize) -> (i64, i64) { - let mut luma = 0i64; - let stride = pic.y.stride; - let origin = pic.y.at(mb_x * 16, mb_y * 16); - for row in 0..16 { - let s = (mb_y * 16 + row) * src.stride + mb_x * 16; - for x in 0..16 { - let d = i64::from(src.y[s + x]) - i64::from(pic.y.data[origin + row * stride + x]); - luma += d * d; - } - } - let mut chroma = 0i64; - for (plane, source) in [(&pic.cb, &src.u), (&pic.cr, &src.v)] { - let stride = plane.stride; - let origin = plane.at(mb_x * 8, mb_y * 8); - for row in 0..8 { - let s = (mb_y * 8 + row) * src.c_stride + mb_x * 8; - for x in 0..8 { - let d = i64::from(source[s + x]) - i64::from(plane.data[origin + row * stride + x]); - chroma += d * d; - } - } - } - (luma, chroma) -} - -#[cfg(feature = "rd-ablation")] -fn rd_lambda_standard(cfg: &EncoderConfig) -> bool { - cfg.rd_lambda_standard -} - -#[cfg(not(feature = "rd-ablation"))] -fn rd_lambda_standard(cfg: &EncoderConfig) -> bool { - cfg.transform_8x8 -} - -#[cfg(feature = "rd-ablation")] -fn rd_transform_8x8_intra(cfg: &EncoderConfig) -> bool { - cfg.rd_transform_8x8_intra -} - -#[cfg(not(feature = "rd-ablation"))] -fn rd_transform_8x8_intra(cfg: &EncoderConfig) -> bool { - cfg.transform_8x8 -} - -#[cfg(feature = "rd-ablation")] -fn rd_transform_8x8_inter(cfg: &EncoderConfig) -> bool { - cfg.rd_transform_8x8_inter -} - -#[cfg(not(feature = "rd-ablation"))] -fn rd_transform_8x8_inter(cfg: &EncoderConfig) -> bool { - cfg.transform_8x8 -} - -#[cfg(feature = "rd-ablation")] -fn rd_transform_8x8_roweven(cfg: &EncoderConfig) -> bool { - cfg.rd_transform_8x8_roweven -} - -#[cfg(not(feature = "rd-ablation"))] -fn rd_transform_8x8_roweven(_: &EncoderConfig) -> bool { - false -} - -#[cfg(not(feature = "rd-ablation"))] -const TRANSFORM_8X8_MARGIN_BITS: f64 = 0.0; - -#[cfg(feature = "rd-ablation")] -fn rd_transform_8x8_margin_bits(cfg: &EncoderConfig) -> f64 { - cfg.rd_transform_8x8_margin_bits -} - -#[cfg(not(feature = "rd-ablation"))] -fn rd_transform_8x8_margin_bits(_: &EncoderConfig) -> f64 { - TRANSFORM_8X8_MARGIN_BITS -} - /// Split `mb_h` macroblock rows into at most `n` contiguous bands. fn split_bands(mb_h: usize, n: usize) -> Vec<(usize, usize)> { let n = n.clamp(1, mb_h); @@ -1198,12 +964,9 @@ mod tests { fn production_lambda_tracks_transform_mode() { let mut cfg = EncoderConfig::new(640, 480); cfg.transform_8x8 = false; - assert_eq!( - lambda_for(26, rd_lambda_standard(&cfg)), - lambda_for(26, false) - ); + assert_eq!(lambda_for(26, cfg.transform_8x8), lambda_for(26, false)); cfg.transform_8x8 = true; - assert_eq!(lambda_for(26, rd_lambda_standard(&cfg)), lambda_ssd(26)); + assert_eq!(lambda_for(26, cfg.transform_8x8), lambda_ssd(26)); } /// Flat chroma over textured luma picks the negative chroma QP offset, and diff --git a/crates/ec-h264/src/enc/timing.rs b/crates/ec-h264/src/enc/timing.rs deleted file mode 100644 index ea0a0602..00000000 --- a/crates/ec-h264/src/enc/timing.rs +++ /dev/null @@ -1,81 +0,0 @@ -//! Stage timers for one encoder, read through [`Encoder::timings`]. They cost a -//! clock read per stage per picture, which is noise next to a picture's coding. -//! -//! [`Encoder::timings`]: super::Encoder::timings - -/// Wall-clock seconds spent in each stage of `encode`, summed over every -/// picture the encoder has coded. -#[derive(Debug, Clone, Copy, Default, PartialEq)] -pub struct EncodeTimings { - /// Pictures counted. - pub pictures: u64, - /// Copying and padding the source planes. - pub pad: f64, - /// The parallel band coding, wall clock. - pub bands_wall: f64, - /// Sum of every band's own coding time (the work the threads share). - pub bands_busy: f64, - /// Sum over pictures of the slowest band (the critical path). - pub bands_slowest: f64, - /// Stitching the bands into one picture. - pub stitch: f64, - /// The in-loop deblocking filter, which runs on one thread. - pub deblock: f64, - /// Border extension, the reference swap and assembling the access unit. - pub output: f64, -} - -/// Per-stage nanosecond counters for the macroblock coder, behind the -/// `enc-profile` feature so a normal build pays nothing. Shared atomics: the -/// bands add to them concurrently, which is fine for a profile. -#[cfg(feature = "enc-profile")] -pub mod mb_stages { - use std::sync::atomic::{AtomicU64, Ordering}; - - /// Stage names, in index order. - pub const NAMES: [&str; 16] = [ - "choose_inter", - "intra_pre_cost", - "inter_rd_candidates", - "intra_rd_total", - "state_save_load", - "entropy_clone", - "intra4x4_rd", - "intra8x8_rd", - "intra_bits_and_ssd", - "(unused)", - "chroma_mode_rd", - "intra16_rd", - "me_seeds", - "me_int", - "me_sub", - "me_chroma_skip", - ]; - pub(crate) static NANOS: [AtomicU64; 16] = [const { AtomicU64::new(0) }; 16]; - - /// Nanoseconds spent in each stage so far, in [`NAMES`] order. - pub fn snapshot() -> [u64; 16] { - std::array::from_fn(|i| NANOS[i].load(Ordering::Relaxed)) - } -} - -/// Time `$body` into stage `$idx` of [`mb_stages`] when profiling, else just run it. -#[cfg(feature = "enc-profile")] -macro_rules! mb_timed { - ($idx:expr, $body:expr) => {{ - let t = std::time::Instant::now(); - let r = $body; - $crate::enc::timing::mb_stages::NANOS[$idx].fetch_add( - t.elapsed().as_nanos() as u64, - std::sync::atomic::Ordering::Relaxed, - ); - r - }}; -} -#[cfg(not(feature = "enc-profile"))] -macro_rules! mb_timed { - ($idx:expr, $body:expr) => { - $body - }; -} -pub(crate) use mb_timed; diff --git a/crates/ec-h264/src/lib.rs b/crates/ec-h264/src/lib.rs index 67c8430f..8b28f6b4 100644 --- a/crates/ec-h264/src/lib.rs +++ b/crates/ec-h264/src/lib.rs @@ -46,8 +46,8 @@ mod deblock_mbaff; mod decoder; mod dpb; mod enc; -mod fmo; mod entropy; +mod fmo; mod inter; mod inter16; mod mbaff; @@ -61,6 +61,4 @@ pub use cabac::BitAccount; pub use codec::H264Decoder; pub use decoder::{Decoder, MbShapes, NalOutcome, OutputOrder}; pub use ec_core::error::{Error, Result}; -#[cfg(feature = "enc-profile")] -pub use enc::timing::mb_stages; -pub use enc::{EncodeTimings, EncodedPicture, Encoder, EncoderConfig, PictureView, Preset}; +pub use enc::{EncodedPicture, Encoder, EncoderConfig, PictureView, Preset};