From 76bf5bffbce222c65850717e4e3963ddf3b6f734 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 13:39:29 +0300 Subject: [PATCH] U7: drop pixel-decode-off, syntax-check and motion-checksum modes (has_pixels guards, Checksum, set_pixel_decode, set_syntax_check, motion_checksum, parse_slice) and the dead pub helpers (y_u8, bit_depth_of, grey, fill_plane, dequantize_with, chroma_mv_420, ref_pic_* accessors, hdr/SEI wrappers). Test-only helpers moved to tests/it/common.rs; no output change --- crates/ec-h265-syntax/src/sei.rs | 35 ++++----- crates/ec-h265/src/dec/cu_syntax.rs | 46 ++---------- crates/ec-h265/src/dec/cu_syntax/wpp.rs | 55 ++++++-------- crates/ec-h265/src/dec/deblock.rs | 5 +- crates/ec-h265/src/dec/inter.rs | 5 +- crates/ec-h265/src/dec/mod.rs | 88 +++-------------------- crates/ec-h265/src/dec/motion_field.rs | 57 +-------------- crates/ec-h265/src/dec/pic.rs | 68 +++++------------- crates/ec-h265/src/dec/picjob.rs | 25 +++---- crates/ec-h265/src/dec/recon.rs | 10 --- crates/ec-h265/src/dec/rowfilter.rs | 7 +- crates/ec-h265/src/dec/rps.rs | 32 +++------ crates/ec-h265/src/dec/runner.rs | 61 ++++------------ crates/ec-h265/src/dec/sao.rs | 5 +- crates/ec-h265/src/dec/shared.rs | 41 ++++------- crates/ec-h265/src/decode.rs | 29 +------- crates/ec-h265/src/enc_p.rs | 4 +- crates/ec-h265/src/encoder.rs | 6 +- crates/ec-h265/src/transform.rs | 7 -- crates/ec-h265/tests/it/common.rs | 11 ++- crates/ec-h265/tests/it/rext_generated.rs | 4 +- 21 files changed, 133 insertions(+), 468 deletions(-) diff --git a/crates/ec-h265-syntax/src/sei.rs b/crates/ec-h265-syntax/src/sei.rs index 815063dd..798ef4ca 100644 --- a/crates/ec-h265-syntax/src/sei.rs +++ b/crates/ec-h265-syntax/src/sei.rs @@ -11,7 +11,6 @@ //! RBSP, and refuses a malformed one rather than guessing. use crate::md5::Md5; -use crate::nal::{NalHeader, NalUnitType}; use crate::ps::rbsp_trailing_bits; use ec_core::bitio::BitWriter; use ec_core::color::ContentLight; @@ -48,19 +47,14 @@ fn sei_rbsp(messages: &[(u32, Vec)]) -> Vec { } /// The prefix SEI RBSP describing HDR mastering metadata, or `None` when there -/// is nothing to say. +/// is nothing to say. When `display` is given it is written verbatim (chromaticities +/// and luminances), and the mastering pair inside `light` is ignored. The content light +/// level comes from `light`. /// /// Both messages are written whole or not at all: a `mastering_display_colour_volume` /// with a zero luminance pair is what the oracle writes when it knows nothing, and a /// tone map that believes it maps the film to black. -pub fn hdr_metadata_rbsp(light: ContentLight) -> Option> { - hdr_metadata_rbsp_with(None, light) -} - -/// [`hdr_metadata_rbsp`] with an explicit mastering display: when `display` is -/// given it is written verbatim (chromaticities and luminances), and the mastering -/// pair inside `light` is ignored. The content light level comes from `light`. -pub fn hdr_metadata_rbsp_with( +pub fn hdr_metadata_rbsp( display: Option, light: ContentLight, ) -> Option> { @@ -126,16 +120,6 @@ pub fn decoded_picture_hash_rbsp(planes: &[(&[u8], usize, usize, usize)]) -> Vec sei_rbsp(&[(PAYLOAD_DECODED_PICTURE_HASH, payload)]) } -/// The NAL header a prefix SEI travels under. -pub fn prefix_sei_header() -> NalHeader { - NalHeader::new(NalUnitType::PrefixSei) -} - -/// The NAL header a suffix SEI (the picture hash) travels under. -pub fn suffix_sei_header() -> NalHeader { - NalHeader::new(NalUnitType::SuffixSei) -} - /// `payloadType` of the alternative transfer characteristics message (D.2.x). const PAYLOAD_ALTERNATIVE_TRANSFER: u32 = 147; @@ -373,16 +357,21 @@ mod tests { mastering_min: Some(0.005), ..ContentLight::default() }; - let rbsp = hdr_metadata_rbsp(light).unwrap(); + let rbsp = hdr_metadata_rbsp(None, light).unwrap(); let mut annex_b = Vec::new(); - crate::nal::write_annex_b(&mut annex_b, prefix_sei_header(), &rbsp, true); + crate::nal::write_annex_b( + &mut annex_b, + crate::nal::NalHeader::new(crate::nal::NalUnitType::PrefixSei), + &rbsp, + true, + ); // ec-core's reader is the consumer this exists for. let read = ec_core::color::hevc_sei_light(&annex_b); assert_eq!(read.max_cll, Some(1000.0)); assert_eq!(read.max_fall, Some(400.0)); assert_eq!(read.mastering_max, Some(1000.0)); assert_eq!(read.mastering_min, Some(0.005)); - assert!(hdr_metadata_rbsp(ContentLight::default()).is_none()); + assert!(hdr_metadata_rbsp(None, ContentLight::default()).is_none()); } #[test] diff --git a/crates/ec-h265/src/dec/cu_syntax.rs b/crates/ec-h265/src/dec/cu_syntax.rs index 299031fd..b30fda7b 100644 --- a/crates/ec-h265/src/dec/cu_syntax.rs +++ b/crates/ec-h265/src/dec/cu_syntax.rs @@ -244,35 +244,6 @@ pub struct SliceStats { pub trailing_bits_ok: bool, } -/// Parse one slice segment's data, from the byte after its header, to -/// `end_of_slice_segment_flag`. `sink` sees every coding unit in decoding order. -/// `motion` derives the motion of each prediction unit. -/// -/// A desync shows up as an `end_of_slice_segment_flag` at the wrong CTB, or as a -/// trailing-bit pattern that is not the stop bit; both are errors. This form -/// keeps only the syntax: no samples are produced. -pub fn parse_slice( - sps: &Sps, - pps: &Pps, - header: &SliceHeader, - data: &[u8], - motion: &mut dyn MotionSource, - sink: &mut dyn FnMut(&CodingUnit), -) -> Result { - let mut pic = PicState::for_sps_pps(sps, pps, false); - parse_slice_into( - sps, - pps, - header, - data, - motion, - &mut pic, - None, - sink, - &mut |_, _| {}, - ) -} - /// Parse one slice segment's data into `pic`. The block maps are always /// filled. When `pic` has sample planes (pixel decode), intra coding units are /// reconstructed in decoding order, and inter coding units are predicted from @@ -892,9 +863,6 @@ impl Parser<'_> { /// Motion-compensated prediction of one prediction unit (8.5.3.3) into the /// picture's samples. Nothing happens without pixel decode. fn predict_pu(&mut self, x: usize, y: usize, w: usize, h: usize, m: &Motion) -> Result<()> { - if !self.pic.has_pixels() { - return Ok(()); - } let Some(refs) = self.refs else { return Err(Error::corrupt( "HEVC decode: an inter prediction unit in a slice with no reference lists", @@ -1284,7 +1252,7 @@ impl Parser<'_> { { self.cu_chroma_qp_offset(dec); } - let recon = tx.intra && self.pic.has_pixels(); + let recon = tx.intra; let luma_mode = tx .intra .then(|| self.pic.info[self.pic.unit(x0, y0)].intra_mode); @@ -1680,13 +1648,11 @@ impl Parser<'_> { } else { (x0 >> sx, y0 >> sy) }; - if self.pic.has_pixels() { - let origin = self.pic.plane_origin(c); - let (stride, plane) = self.pic.plane_view_mut(c); - for j in 0..sh { - let start = (y + j - origin) * stride + x; - plane[start..start + sw].copy_from_slice(&samples[j * sw..j * sw + sw]); - } + let origin = self.pic.plane_origin(c); + let (stride, plane) = self.pic.plane_view_mut(c); + for j in 0..sh { + let start = (y + j - origin) * stride + x; + plane[start..start + sw].copy_from_slice(&samples[j * sw..j * sw + sw]); } } self.pic.update_info(x0, y0, n, n, |b| b.pcm = true); diff --git a/crates/ec-h265/src/dec/cu_syntax/wpp.rs b/crates/ec-h265/src/dec/cu_syntax/wpp.rs index efdf1792..1fa27ada 100644 --- a/crates/ec-h265/src/dec/cu_syntax/wpp.rs +++ b/crates/ec-h265/src/dec/cu_syntax/wpp.rs @@ -104,13 +104,11 @@ impl Board { let cw = self.width.div_ceil(1 << self.sub_x); let mut st = lock(&self.state); let slot = &mut st.slots[ry]; - if pic.has_pixels() { - let lr = (y0 + ctb - 1) - pic.origin_y; - slot.luma = pic.planes.y[lr * self.width..(lr + 1) * self.width].to_vec(); - let lc = ((y0 >> self.sub_y) + (ctb >> self.sub_y) - 1) - pic.origin_c; - slot.cb = pic.planes.cb[lc * cw..(lc + 1) * cw].to_vec(); - slot.cr = pic.planes.cr[lc * cw..(lc + 1) * cw].to_vec(); - } + let lr = (y0 + ctb - 1) - pic.origin_y; + slot.luma = pic.planes.y[lr * self.width..(lr + 1) * self.width].to_vec(); + let lc = ((y0 >> self.sub_y) + (ctb >> self.sub_y) - 1) - pic.origin_c; + slot.cb = pic.planes.cb[lc * cw..(lc + 1) * cw].to_vec(); + slot.cr = pic.planes.cr[lc * cw..(lc + 1) * cw].to_vec(); let ur = (y0 + ctb - 4) / 4; let band_row = ur - pic.origin_unit / 4; slot.units = pic.info[band_row * self.w4..(band_row + 1) * self.w4].to_vec(); @@ -157,11 +155,9 @@ impl Board { (k * ctb) >> self.sub_x, (((k + 1) * ctb) >> self.sub_x).min(cw), ); - if pic.has_pixels() { - pic.planes.y[a..b].copy_from_slice(&slot.luma[a..b]); - pic.planes.cb[ca..cb_end].copy_from_slice(&slot.cb[ca..cb_end]); - pic.planes.cr[ca..cb_end].copy_from_slice(&slot.cr[ca..cb_end]); - } + pic.planes.y[a..b].copy_from_slice(&slot.luma[a..b]); + pic.planes.cb[ca..cb_end].copy_from_slice(&slot.cb[ca..cb_end]); + pic.planes.cr[ca..cb_end].copy_from_slice(&slot.cr[ca..cb_end]); let (ua, ub) = (k * ctb / 4, ((k + 1) * ctb / 4).min(self.w4)); pic.info[ua..ub].copy_from_slice(&slot.units[ua..ub]); pic.ctb[(ry - 1) * cols + k] = slot.ctb[k]; @@ -216,14 +212,12 @@ impl Board { } if full { // Band rows: the sample rows and unit row of the CTB row's last rows. - if pic.has_pixels() { - let lr = (y0 + ctb - 1) - pic.origin_y; - slot.luma[a..b] - .copy_from_slice(&pic.planes.y[lr * self.width + a..lr * self.width + b]); - let lc = ((y0 >> self.sub_y) + (ctb >> self.sub_y) - 1) - pic.origin_c; - slot.cb[ca..cb_end].copy_from_slice(&pic.planes.cb[lc * cw + ca..lc * cw + cb_end]); - slot.cr[ca..cb_end].copy_from_slice(&pic.planes.cr[lc * cw + ca..lc * cw + cb_end]); - } + let lr = (y0 + ctb - 1) - pic.origin_y; + slot.luma[a..b] + .copy_from_slice(&pic.planes.y[lr * self.width + a..lr * self.width + b]); + let lc = ((y0 >> self.sub_y) + (ctb >> self.sub_y) - 1) - pic.origin_c; + slot.cb[ca..cb_end].copy_from_slice(&pic.planes.cb[lc * cw + ca..lc * cw + cb_end]); + slot.cr[ca..cb_end].copy_from_slice(&pic.planes.cr[lc * cw + ca..lc * cw + cb_end]); let ur = (y0 + ctb - 4) / 4; let band_row = ur - pic.origin_unit / 4; slot.units[ua..ub] @@ -501,7 +495,6 @@ pub(in crate::dec) fn parse_segment( if ry0 > 0 { board.prefill(ry0 - 1, pic, &*source.field); } - let pixels = pic.has_pixels(); let next = AtomicUsize::new(0); let lists = source.lists; let cur_poc = source.cur_poc; @@ -517,7 +510,7 @@ pub(in crate::dec) fn parse_segment( let y0 = ry << ctb_log2; let bound = usize::from(ry > 0); let own = ctb.min(global.height - y0); - let mut band = global.band(y0, own, pixels); + let mut band = global.band(y0, own); let mut field = field_global.band((y0 - 4 * bound) / 4, bound + own.div_ceil(4)); let row_start = if i == 0 { 0 } else { starts[i - 1] }; let row_len = (!last).then(|| starts[i] - row_start); @@ -617,16 +610,14 @@ pub(in crate::dec) fn parse_segment( let (sx, sy) = (pic.sub_x(), pic.sub_y()); let own_c = own.div_ceil(1 << sy); let cw = pic.width.div_ceil(1 << sx); - if pixels { - let w = pic.width; - pic.planes.y[y0 * w..(y0 + own) * w] - .copy_from_slice(&done.band.planes.y[bound * w..(bound + own) * w]); - let (c0, c1) = (y0 >> sy, (y0 >> sy) + own_c); - pic.planes.cb[c0 * cw..c1 * cw] - .copy_from_slice(&done.band.planes.cb[bound * cw..(bound + own_c) * cw]); - pic.planes.cr[c0 * cw..c1 * cw] - .copy_from_slice(&done.band.planes.cr[bound * cw..(bound + own_c) * cw]); - } + let w = pic.width; + pic.planes.y[y0 * w..(y0 + own) * w] + .copy_from_slice(&done.band.planes.y[bound * w..(bound + own) * w]); + let (c0, c1) = (y0 >> sy, (y0 >> sy) + own_c); + pic.planes.cb[c0 * cw..c1 * cw] + .copy_from_slice(&done.band.planes.cb[bound * cw..(bound + own_c) * cw]); + pic.planes.cr[c0 * cw..c1 * cw] + .copy_from_slice(&done.band.planes.cr[bound * cw..(bound + own_c) * cw]); let u0 = y0 / 4; let units = own.div_ceil(4); pic.info[u0 * pic.w4..(u0 + units) * pic.w4] diff --git a/crates/ec-h265/src/dec/deblock.rs b/crates/ec-h265/src/dec/deblock.rs index 45f838c2..6285fb7e 100644 --- a/crates/ec-h265/src/dec/deblock.rs +++ b/crates/ec-h265/src/dec/deblock.rs @@ -432,9 +432,6 @@ pub fn deblock_picture(pic: &mut PicState) { /// side is in the rows above are filtered after those rows' own vertical edges, which /// is the order 8.7.2 gives for the whole picture. pub fn deblock_rows(pic: &mut PicState, rows: Range) { - if !pic.has_pixels() { - return; - } let (w, h) = (pic.width, pic.height); let (sx, sy) = (pic.sub_x(), pic.sub_y()); let (cw, ch) = (w.div_ceil(1 << sx), h.div_ceil(1 << sy)); @@ -484,7 +481,7 @@ mod tests { /// A 16x16 picture whose luma column `x` holds `value(x)`, with flat chroma, /// every unit intra at QP 37 and an edge on every unit. fn picture(value: impl Fn(usize) -> u16) -> PicState { - let mut pic = PicState::new(16, 16, 4, 2, 8, 8, true); + let mut pic = PicState::new(16, 16, 4, 2, 8, 8); for y in 0..16 { for x in 0..16 { pic.planes.y[y * 16 + x] = value(x); diff --git a/crates/ec-h265/src/dec/inter.rs b/crates/ec-h265/src/dec/inter.rs index 7b9e33be..8052e86d 100644 --- a/crates/ec-h265/src/dec/inter.rs +++ b/crates/ec-h265/src/dec/inter.rs @@ -168,9 +168,6 @@ fn ref_picture(pic: &DecodedPicture) -> RefPicture<'_> { /// Add the residual blocks of an inter coding unit to the samples its prediction /// units already wrote. pub fn add_residuals(pic: &mut PicState, residuals: &[ResidualBlock]) { - if !pic.has_pixels() { - return; - } for block in residuals { pic.add_residual( block.component, @@ -196,7 +193,7 @@ mod tests { let (tx, rx) = mpsc::channel(); std::thread::spawn(move || { let (w, h) = (64usize, 64usize); - let pic = Arc::new(SharedPic::new(0, w, h, (8, 8), true, 1)); + let pic = Arc::new(SharedPic::new(0, w, h, (8, 8), 1)); let src = DecodedPicture { width: w, height: h, diff --git a/crates/ec-h265/src/dec/mod.rs b/crates/ec-h265/src/dec/mod.rs index 2ba7bbc8..0009cd6e 100644 --- a/crates/ec-h265/src/dec/mod.rs +++ b/crates/ec-h265/src/dec/mod.rs @@ -5,12 +5,7 @@ //! Scope is the base layer of Main, Main 10 and range extension streams. Every slice //! goes through [`cu_syntax::parse_slice_into`], which parses the slice data //! and pixel-decodes it. Inter slices write placeholder samples (see -//! [`inter::placeholder_cu`]) until motion compensation lands. With pixel -//! decode off ([`Decoder::set_pixel_decode`]) every slice is -//! parsed and every picture is managed, but no samples are produced; that is -//! the mode the picture-management tests use. [`Decoder::set_syntax_check`] -//! adds the slice-data parse ([`cu_syntax::parse_slice`]) to that mode, for -//! every slice type. +//! [`inter::placeholder_cu`]) until motion compensation lands. //! //! Decoded: Main, Main 10 and the range extension formats (4:0:0, 4:2:0, 4:2:2, 4:4:4 at //! 8 to 12 bits) with transform-skip extensions, implicit and explicit RDPCM, persistent @@ -64,9 +59,8 @@ pub use meta::PictureMeta; use picjob::{PicJob, SliceJob}; use rps::RpsInput; pub use rps::{RefEntry, RefPicLists, RefPicSet}; -use runner::{Budget, Checksum, Runner}; +use runner::{Budget, Runner}; use shared::SharedPic; -use std::sync::Mutex; use std::thread::JoinHandle; /// What [`Decoder::push_nal`] did with a NAL unit. @@ -99,8 +93,6 @@ struct CurPicture { runner: Runner, /// The published picture (the samples and collocated motion that later pictures read). shared: Arc, - /// The motion checksum, set when the picture finishes. - checksum: Arc, /// The header of the picture's latest independent slice segment; the /// dependent segments after it take their values from it. header: Option, @@ -142,13 +134,10 @@ pub struct Decoder { vps: Vec>, sps: Vec>>, pps: Vec>>, - pixel_decode: bool, /// Worker threads for the in-loop filters (see [`configured_threads`]). threads: usize, /// The worker threads the pictures and their wavefront rows share. budget: Arc, - /// Parse every slice's data (all types) with [`cu_syntax`] when pixel decode is off. - syntax_check: bool, dpb: Dpb, cur: Option, /// Set while the slices of a skipped RASL picture go by. @@ -167,23 +156,12 @@ pub struct Decoder { hdr_pending: PictureMeta, /// The HDR SEI in force in the current coded video sequence. hdr_persist: PictureMeta, - /// The motion checksum of the finished pictures (see [`Decoder::motion_checksum`]). - checksums: Mutex, /// Worker threads of the pictures that finished but may still run, oldest first. in_flight: VecDeque>, /// The most pictures in flight (see [`picture_limit`]). max_in_flight: usize, } -/// The motion checksum folded over the finished pictures in decoding order. -#[derive(Default)] -struct ChecksumFold { - /// The fold of every picture whose checksum has been waited for. - fold: u64, - /// The checksums of the finished pictures not yet folded, in decoding order. - pending: VecDeque>, -} - impl Default for Decoder { fn default() -> Self { Decoder::new() @@ -191,16 +169,14 @@ impl Default for Decoder { } impl Decoder { - /// A decoder with no parameter sets and pixel decode on. + /// A decoder with no parameter sets. pub fn new() -> Decoder { Decoder { vps: vec![None; 16], sps: vec![None; 16], pps: vec![None; 64], - pixel_decode: true, threads: configured_threads(), budget: Budget::new(configured_threads()), - syntax_check: false, dpb: Dpb::new(), cur: None, skipping: false, @@ -212,7 +188,6 @@ impl Decoder { out: VecDeque::new(), hdr_pending: PictureMeta::default(), hdr_persist: PictureMeta::default(), - checksums: Mutex::new(ChecksumFold::default()), in_flight: VecDeque::new(), max_in_flight: picture_limit(configured_threads()), } @@ -225,38 +200,6 @@ impl Decoder { self.budget = Budget::new(self.threads); } - /// Turn pixel decode on or off. Off parses every slice and manages every - /// picture (RPS, POC, bumping) without producing samples; inter slices are - /// accepted in that mode. - pub fn set_pixel_decode(&mut self, on: bool) { - self.pixel_decode = on; - } - - /// Whether pixel decode is on. - pub fn pixel_decode(&self) -> bool { - self.pixel_decode - } - - /// A hash of the motion field of every picture finished so far, in decoding order. - /// Two decodes of the same stream give the same value; a change in any derived - /// motion vector, reference index or prediction flag changes it. - pub fn motion_checksum(&self) -> u64 { - let mut fold = self.checksums.lock().unwrap_or_else(|e| e.into_inner()); - while let Some(slot) = fold.pending.pop_front() { - if let Some(sum) = slot.wait() { - fold.fold = (fold.fold ^ sum).wrapping_mul(0x0000_0100_0000_01b3); - } - } - fold.fold - } - - /// With pixel decode off, also parse every slice's data with the CU syntax - /// parser. A parse that does not reach `end_of_slice_segment_flag` at the - /// end of the slice is an error from [`Decoder::push_nal`]. - pub fn set_syntax_check(&mut self, on: bool) { - self.syntax_check = on; - } - /// The VPS stored under `id`. pub fn vps(&self, id: u32) -> Option<&Vps> { self.vps.get(id as usize)?.as_ref() @@ -521,10 +464,10 @@ impl Decoder { irap_no_rasl: irap && no_rasl, width: sps.pic_width_in_luma_samples as usize, height: sps.pic_height_in_luma_samples as usize, - fill: self.pixel_decode.then_some(( + fill: ( sps.bit_depth_luma_minus8 + 8, sps.bit_depth_chroma_minus8 + 8, - )), + ), chroma_format_idc: sps.chroma_format_idc, }; let rps = self.dpb.apply_rps(&input); @@ -559,7 +502,7 @@ impl Decoder { } // Frame threads: at most `threads` pictures in flight, so wait for the oldest. - let threaded = self.threads > 1 && self.pixel_decode; + let threaded = self.threads > 1; if threaded { while self.in_flight.len() >= self.max_in_flight { if let Some(handle) = self.in_flight.pop_front() { @@ -575,13 +518,10 @@ impl Decoder { 8 + sps.bit_depth_luma_minus8, 8 + sps.bit_depth_chroma_minus8, ), - self.pixel_decode, sps.chroma_format_idc, )); - let checksum = Arc::new(Checksum::default()); let runner = Runner::start( PicJob::new(Arc::clone(&shared), Arc::clone(&self.budget)), - Arc::clone(&checksum), threaded, ); self.cur = Some(CurPicture { @@ -594,7 +534,6 @@ impl Decoder { lists: RefPicLists::default(), runner, shared, - checksum, all_intra: true, meta, }); @@ -602,7 +541,7 @@ impl Decoder { } /// Build the slice's reference lists, then hand the slice data to the picture's - /// job. Nothing runs when pixel decode and the syntax check are both off. + /// job. fn decode_slice( &mut self, slice: &SliceHeader, @@ -612,7 +551,6 @@ impl Decoder { sps: &Arc, pps: &Arc, ) -> Result<()> { - let pixel = self.pixel_decode; let dpb = &self.dpb; let cur = self .cur @@ -624,9 +562,6 @@ impl Decoder { if slice.slice_type != SliceType::I { cur.all_intra = false; } - if !pixel && !self.syntax_check { - return Ok(()); - } let data_off = (pos.header_bits / 8) as usize; if data_off > rbsp.len() { return Err(Error::corrupt( @@ -648,7 +583,7 @@ impl Decoder { }) .collect() }; - let refs = if pixel && slice.slice_type != SliceType::I { + let refs = if slice.slice_type != SliceType::I { [lookup(&cur.lists.l0)?, lookup(&cur.lists.l1)?] } else { [Vec::new(), Vec::new()] @@ -664,7 +599,6 @@ impl Decoder { refs, col, cur_poc: cur.poc, - pixel, substreams, }); submitted @@ -688,17 +622,11 @@ impl Decoder { output, runner, shared, - checksum, all_intra, mut meta, .. } = cur; meta.intra_only = all_intra; - self.checksums - .lock() - .unwrap_or_else(|e| e.into_inner()) - .pending - .push_back(checksum); if let Some(handle) = runner.finish() { self.in_flight.push_back(handle); } diff --git a/crates/ec-h265/src/dec/motion_field.rs b/crates/ec-h265/src/dec/motion_field.rs index 69fa59b2..43133330 100644 --- a/crates/ec-h265/src/dec/motion_field.rs +++ b/crates/ec-h265/src/dec/motion_field.rs @@ -24,9 +24,6 @@ use super::{amvp, merge}; use ec_h265_syntax::slice::{SliceHeader, SliceType}; use ec_h265_syntax::{Pps, Sps}; -const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325; -const FNV_PRIME: u64 = 0x0000_0100_0000_01b3; - /// The motion of one prediction block: `predFlagLX`, `refIdxLX` and `mvLX` for both /// lists. An unused list has reference index -1 and a zero vector, so two blocks /// with the same motion compare equal. @@ -116,7 +113,6 @@ pub struct MotionField { cells: Vec>, /// The reference lists of each slice of the picture, indexed by `FieldCell::slice`. pub slices: Vec, - checksum: u64, /// `TileId` of each CTB, raster order; empty when the picture has one tile. tiles: Vec, /// `CtbLog2SizeY` and the CTBs per row, for the tile lookup. @@ -127,10 +123,6 @@ pub struct MotionField { /// The 4x4 unit row of `cells[0..width4]`: zero for a whole picture, and the row above /// the band for a wavefront row band (see [`MotionField::band`]). origin4: usize, - /// A wavefront band: its stores are kept in `words` for the picture's checksum, which - /// folds them in decoding order once the band is merged. - band: bool, - words: Vec, } impl MotionField { @@ -144,14 +136,11 @@ impl MotionField { width4, cells: vec![None; width4 * height4], slices: Vec::new(), - checksum: FNV_OFFSET, tiles: Vec::new(), ctb_log2: 0, ctb_cols: 0, cur_tile: 0, origin4: 0, - band: false, - words: Vec::new(), } } @@ -165,14 +154,11 @@ impl MotionField { width4: self.width4, cells: vec![None; unit_rows * self.width4], slices: Vec::new(), - checksum: FNV_OFFSET, tiles: self.tiles.clone(), ctb_log2: self.ctb_log2, ctb_cols: self.ctb_cols, cur_tile: 0, origin4, - band: true, - words: Vec::new(), } } @@ -198,18 +184,12 @@ impl MotionField { } /// Merge a finished band into this field: its cells from unit row `first_unit_row` on - /// are copied, and its stores are folded into the checksum in decoding order. Bands - /// merged in decoding order give the checksum a whole-picture decode would give. + /// are copied. pub fn merge_band(&mut self, band: &MotionField, first_unit_row: usize) { let from = (first_unit_row - band.origin4) * self.width4; let to = first_unit_row - self.origin4; let n = band.cells.len() - from; self.cells[to * self.width4..to * self.width4 + n].copy_from_slice(&band.cells[from..]); - for words in band.words.as_chunks::<8>().0 { - for &word in words { - self.checksum = (self.checksum ^ word).wrapping_mul(FNV_PRIME); - } - } } /// The tile of each CTB (raster order), so that a neighbour in another tile is @@ -313,30 +293,6 @@ impl MotionField { Some(FieldCell { motion, slice }); } } - let mut words = [0u64; 8]; - words[0] = x as u64; - words[1] = y as u64; - words[2] = w as u64; - words[3] = h as u64; - words[4] = u64::from(slice); - words[5] = u64::from(motion.pred[0]) | (u64::from(motion.pred[1]) << 1); - words[6] = (motion.ref_idx[0] as u8 as u64) | ((motion.ref_idx[1] as u8 as u64) << 8); - words[7] = (motion.mv[0][0] as u16 as u64) - | ((motion.mv[0][1] as u16 as u64) << 16) - | ((motion.mv[1][0] as u16 as u64) << 32) - | ((motion.mv[1][1] as u16 as u64) << 48); - if self.band { - self.words.extend_from_slice(&words); - return; - } - for word in words { - self.checksum = (self.checksum ^ word).wrapping_mul(FNV_PRIME); - } - } - - /// A hash of every stored block, in decoding order. Equal for equal derivations. - pub fn checksum(&self) -> u64 { - self.checksum } /// The motion of every 4x4 unit in the form the deblocking filter reads (8.7.2.4). @@ -638,10 +594,8 @@ impl MotionSource for SliceMotion<'_> { // The CTB row that ended is `row_end - ctb`; its motion is final. let ctb = 1usize << self.params.ctb_log2; let y0 = row_end.saturating_sub(ctb); - if pic.has_pixels() { - let (u0, u1) = (y0, row_end.min(self.field.height())); - self.field.deblock_rows(&mut pic.motion, u0, u1); - } + let (u0, u1) = (y0, row_end.min(self.field.height())); + self.field.deblock_rows(&mut pic.motion, u0, u1); if let Some(shared) = &self.publish { shared.publish_col(self.field, row_end); } @@ -682,11 +636,6 @@ pub fn scale_mv(mv: [i32; 2], td: i32, tb: i32) -> [i32; 2] { }) } -/// The chroma vector of 4:2:0 content, in 1/8 chroma sample units (8.5.3.2.10): -/// the luma quarter-sample vector, unchanged in value. -pub fn chroma_mv_420(mv: [i16; 2]) -> [i32; 2] { - to32(mv) -} /// A vector widened to `i32`. pub fn to32(mv: [i16; 2]) -> [i32; 2] { diff --git a/crates/ec-h265/src/dec/pic.rs b/crates/ec-h265/src/dec/pic.rs index 45ecdfff..b4d314e6 100644 --- a/crates/ec-h265/src/dec/pic.rs +++ b/crates/ec-h265/src/dec/pic.rs @@ -163,7 +163,7 @@ pub struct PicState { pub pps_cb_qp_offset: i32, /// See `pps_cb_qp_offset`. pub pps_cr_qp_offset: i32, - /// The samples. Empty planes when the decoder runs without pixel decode. + /// The samples. pub planes: DecodedPicture, /// Per 4x4 unit, `w4 * h4`. pub info: Vec, @@ -189,8 +189,7 @@ pub struct PicState { } impl PicState { - /// The state of a `width` x `height` picture. With `pixels` false the - /// sample planes are left empty and only the maps are kept. + /// The state of a `width` x `height` picture. #[allow(clippy::too_many_arguments)] // one value per picture-level SPS/PPS fact pub fn new( width: usize, @@ -199,27 +198,13 @@ impl PicState { min_tb_log2: u32, bit_depth_y: u32, bit_depth_c: u32, - pixels: bool, ) -> PicState { let ctb = 1usize << ctb_log2; let ctb_cols = width.div_ceil(ctb); let ctb_rows = height.div_ceil(ctb); let w4 = width.div_ceil(4); let h4 = height.div_ceil(4); - let planes = if pixels { - DecodedPicture::grey_planes(width, height, bit_depth_y, bit_depth_c) - } else { - DecodedPicture { - width, - height, - bit_depth: bit_depth_y, - bit_depth_c, - chroma_format_idc: 1, - y: Vec::new(), - cb: Vec::new(), - cr: Vec::new(), - } - }; + let planes = DecodedPicture::grey_planes(width, height, bit_depth_y, bit_depth_c, 1); PicState { width, height, @@ -262,11 +247,6 @@ impl PicState { crate::decode::chroma_shifts(self.chroma_format_idc).1 } - /// Whether the sample planes are allocated (pixel decode is on). - pub fn has_pixels(&self) -> bool { - !self.planes.y.is_empty() - } - /// The three writable sample planes, for motion compensation. pub fn planes_mut(&mut self) -> [PlaneMut<'_>; 3] { let (bd_y, bd_c) = (self.bit_depth_y, self.bit_depth_c); @@ -384,9 +364,8 @@ impl PicState { } } - /// The state of a picture of `sps` and `pps`. With `pixels` on, the sample - /// planes are allocated, grey. - pub fn for_sps_pps(sps: &Sps, pps: &Pps, pixels: bool) -> PicState { + /// The state of a picture of `sps` and `pps`, its sample planes grey. + pub fn for_sps_pps(sps: &Sps, pps: &Pps) -> PicState { let min_cb_log2 = sps.log2_min_cb_size_minus3 + 3; let ctb_log2 = min_cb_log2 + sps.log2_diff_max_min_cb_size; let min_tb_log2 = sps.log2_min_tb_size_minus2 + 2; @@ -397,19 +376,16 @@ impl PicState { min_tb_log2, 8 + sps.bit_depth_luma_minus8, 8 + sps.bit_depth_chroma_minus8, - pixels, ); if sps.chroma_format_idc != 1 { pic.chroma_format_idc = sps.chroma_format_idc; - if pixels { - pic.planes = DecodedPicture::grey_planes_fmt( - pic.width, - pic.height, - pic.bit_depth_y, - pic.bit_depth_c, - sps.chroma_format_idc, - ); - } + pic.planes = DecodedPicture::grey_planes( + pic.width, + pic.height, + pic.bit_depth_y, + pic.bit_depth_c, + sps.chroma_format_idc, + ); } pic.constrained_intra_pred = pps.constrained_intra_pred; pic.strong_intra_smoothing = sps.strong_intra_smoothing; @@ -426,7 +402,7 @@ impl PicState { /// first, so the band's first sample, unit and CTB rows are that row (copied in by /// the wavefront driver). The maps cover only the band; the CTB table is the /// whole picture's shape, and the tile scan and slice state are copied. - pub fn band(&self, y0: usize, own: usize, pixels: bool) -> PicState { + pub fn band(&self, y0: usize, own: usize) -> PicState { let bound = usize::from(y0 > 0); let own_units = own.div_ceil(4); let (sx, sy) = crate::decode::chroma_shifts(self.chroma_format_idc); @@ -438,21 +414,9 @@ impl PicState { bit_depth: self.bit_depth_y, bit_depth_c: self.bit_depth_c, chroma_format_idc: self.chroma_format_idc, - y: if pixels { - vec![0; (bound + own) * self.width] - } else { - Vec::new() - }, - cb: if pixels { - vec![0; (bound + own_c) * cw] - } else { - Vec::new() - }, - cr: if pixels { - vec![0; (bound + own_c) * cw] - } else { - Vec::new() - }, + y: vec![0; (bound + own) * self.width], + cb: vec![0; (bound + own_c) * cw], + cr: vec![0; (bound + own_c) * cw], }; PicState { width: self.width, diff --git a/crates/ec-h265/src/dec/picjob.rs b/crates/ec-h265/src/dec/picjob.rs index 5c9e19ff..461c4554 100644 --- a/crates/ec-h265/src/dec/picjob.rs +++ b/crates/ec-h265/src/dec/picjob.rs @@ -42,8 +42,6 @@ pub struct SliceJob { pub col: Option>, /// `PicOrderCntVal` of the picture. pub cur_poc: i32, - /// Reconstruct the samples (pixel decode). Off parses the syntax and the motion only. - pub pixel: bool, /// Where the substreams after the first start, relative to `data_off` (one per CTB row /// after the segment's first, from the entry points). Empty when there are none. pub substreams: Vec, @@ -95,7 +93,7 @@ impl PicJob { .. } = self; let (sps, pps) = (&*job.sps, &*job.pps); - let pic = pic.get_or_insert_with(|| PicState::for_sps_pps(sps, pps, job.pixel)); + let pic = pic.get_or_insert_with(|| PicState::for_sps_pps(sps, pps)); let (w, h) = ( sps.pic_width_in_luma_samples as usize, sps.pic_height_in_luma_samples as usize, @@ -112,7 +110,7 @@ impl PicJob { field.slices.push(SliceRefs::from_lists(&job.lists)); } let slice = u16::try_from(field.slices.len().saturating_sub(1)).unwrap_or(u16::MAX); - let refs = if job.pixel && job.header.slice_type != SliceType::I { + let refs = if job.header.slice_type != SliceType::I { Some(InterRefs::new(job.refs, &job.header, sps)?) } else { None @@ -181,10 +179,9 @@ impl PicJob { Ok(()) } - /// The picture is complete: run the rest of the in-loop filters, publish every - /// row and the collocated motion, and return the picture's motion checksum (`None` - /// when no slice ran). - pub fn finish(&mut self) -> Option { + /// The picture is complete: run the rest of the in-loop filters, and publish every + /// row and the collocated motion. + pub fn finish(&mut self) { let PicJob { shared, pic, @@ -194,22 +191,18 @@ impl PicJob { } = self; let Some(mut pic) = pic.take() else { shared.complete(); - return None; + return; }; let h = shared.height; // Slices that finished no CTB row (or several tiles) left the motion of the // units for the filter to read; fill every unit now. - let checksum = motion.take().map(|field| { - if pic.has_pixels() { - field.deblock_rows(&mut pic.motion, 0, h); - } + if let Some(field) = motion.take() { + field.deblock_rows(&mut pic.motion, 0, h); shared.publish_col(&field, h); - field.checksum() - }); + } let rows = pic.ctb_rows; let final_rows = filter.advance(&mut pic, rows); shared.publish_rows(&pic.planes, final_rows); shared.complete(); - checksum } } diff --git a/crates/ec-h265/src/dec/recon.rs b/crates/ec-h265/src/dec/recon.rs index c0905809..b9bb318d 100644 --- a/crates/ec-h265/src/dec/recon.rs +++ b/crates/ec-h265/src/dec/recon.rs @@ -118,16 +118,6 @@ impl PicState { } } - /// Write `value` into every sample of an `n` x `n` block of component `c`. - pub fn fill_plane(&mut self, c: usize, x: usize, y: usize, n: usize, value: u16) { - let origin = self.plane_origin(c); - let (stride, plane) = self.plane_view_mut(c); - for j in 0..n { - let start = (y + j - origin) * stride + x; - plane[start..start + n].fill(value); - } - } - /// The row stride of the chroma planes. fn chroma_stride(&self) -> usize { self.width.div_ceil(1 << self.sub_x()) diff --git a/crates/ec-h265/src/dec/rowfilter.rs b/crates/ec-h265/src/dec/rowfilter.rs index 423d85a6..6f7e7a47 100644 --- a/crates/ec-h265/src/dec/rowfilter.rs +++ b/crates/ec-h265/src/dec/rowfilter.rs @@ -53,9 +53,7 @@ impl RowFilter { let ctb = 1usize << pic.ctb_log2; while self.deblocked < rows && (self.deblocked + 2 <= complete || all) { let r = self.deblocked; - if pic.has_pixels() { - deblock::deblock_rows(pic, r * ctb..(r + 1) * ctb); - } + deblock::deblock_rows(pic, r * ctb..(r + 1) * ctb); self.deblocked += 1; } while self.filtered < rows @@ -74,9 +72,6 @@ impl RowFilter { /// SAO of CTB row `r` for the three planes, then remember its last deblocked line. fn sao_row(&mut self, pic: &mut PicState, r: usize) { - if !pic.has_pixels() { - return; - } let ctb = 1usize << pic.ctb_log2; // Does any CTB of the row use SAO? Without one the row is final as it is. let sao_on = (r * pic.ctb_cols..(r + 1) * pic.ctb_cols).any(|i| { diff --git a/crates/ec-h265/src/dec/rps.rs b/crates/ec-h265/src/dec/rps.rs index e2bfc751..2cd34392 100644 --- a/crates/ec-h265/src/dec/rps.rs +++ b/crates/ec-h265/src/dec/rps.rs @@ -27,9 +27,8 @@ pub struct RpsInput<'a> { /// Luma height of the picture, for generated pictures. pub height: usize, /// Bit depths (luma, chroma) of a generated (8.3.3) picture, whose samples are - /// grey. `None` generates a picture without samples, for a decoder that does - /// not decode pixels. - pub fill: Option<(u32, u32)>, + /// grey. + pub fill: (u32, u32), /// `chroma_format_idc` of the generated picture. pub chroma_format_idc: u32, } @@ -236,25 +235,14 @@ impl Dpb { /// Generate a missing reference picture (8.3.3.2): grey samples, no output. fn generate(&mut self, poc: i32, marking: Marking, input: &RpsInput<'_>) -> u32 { - let picture = match input.fill { - Some((bit_depth, bit_depth_c)) => DecodedPicture::grey_planes_fmt( - input.width, - input.height, - bit_depth, - bit_depth_c, - input.chroma_format_idc, - ), - None => DecodedPicture { - width: input.width, - height: input.height, - bit_depth: 8, - bit_depth_c: 8, - chroma_format_idc: input.chroma_format_idc, - y: Vec::new(), - cb: Vec::new(), - cr: Vec::new(), - }, - }; + let (bit_depth, bit_depth_c) = input.fill; + let picture = DecodedPicture::grey_planes( + input.width, + input.height, + bit_depth, + bit_depth_c, + input.chroma_format_idc, + ); self.insert( poc, marking, diff --git a/crates/ec-h265/src/dec/runner.rs b/crates/ec-h265/src/dec/runner.rs index 50ed779e..f3566f30 100644 --- a/crates/ec-h265/src/dec/runner.rs +++ b/crates/ec-h265/src/dec/runner.rs @@ -15,7 +15,7 @@ use std::panic::{AssertUnwindSafe, catch_unwind}; use std::sync::mpsc::{self, Receiver, Sender}; -use std::sync::{Arc, Condvar, Mutex, MutexGuard}; +use std::sync::{Arc, Mutex, MutexGuard}; use std::thread::JoinHandle; use super::picjob::{PicJob, SliceJob}; @@ -106,34 +106,6 @@ impl Drop for PictureSlot { } } -/// The motion checksum of one picture (see [`super::Decoder::motion_checksum`]), set when -/// the picture finishes. The value is `None` for a picture no slice ran in, and also for a -/// picture that failed. -#[derive(Debug, Default)] -pub struct Checksum { - value: Mutex>>, - changed: Condvar, -} - -impl Checksum { - /// Store the picture's value and wake the readers. - pub fn set(&self, value: Option) { - *lock(&self.value) = Some(value); - self.changed.notify_all(); - } - - /// Wait until the value is stored. - pub fn wait(&self) -> Option { - let mut value = lock(&self.value); - loop { - if let Some(v) = *value { - return v; - } - value = self.changed.wait(value).unwrap_or_else(|e| e.into_inner()); - } - } -} - /// A message to a picture's worker. pub enum Msg { /// Run this slice segment. @@ -148,8 +120,6 @@ pub enum Runner { Inline { /// The picture's job. job: Box, - /// Set when the picture finishes. - checksum: Arc, }, /// On a worker thread that owns the job. Worker { @@ -162,15 +132,14 @@ pub enum Runner { impl Runner { /// Start the decoding of the picture of `job`. `threaded` gives it a worker thread. - pub fn start(job: PicJob, checksum: Arc, threaded: bool) -> Runner { + pub fn start(job: PicJob, threaded: bool) -> Runner { if !threaded { return Runner::Inline { job: Box::new(job), - checksum, }; } let (tx, rx) = mpsc::channel(); - let handle = std::thread::spawn(move || run_worker(rx, job, checksum)); + let handle = std::thread::spawn(move || run_worker(rx, job)); Runner::Worker { tx, handle } } @@ -192,8 +161,8 @@ impl Runner { /// goes on in the background; its handle is returned to be joined later. pub fn finish(self) -> Option> { match self { - Runner::Inline { mut job, checksum } => { - checksum.set(job.finish()); + Runner::Inline { mut job } => { + job.finish(); None } Runner::Worker { tx, handle } => { @@ -206,17 +175,17 @@ impl Runner { /// The body of a picture's worker thread. A failure, a panic, or a picture dropped before /// its finish marks the picture as failed, so that nothing waits on it forever. -fn run_worker(rx: Receiver, mut job: PicJob, checksum: Arc) { +fn run_worker(rx: Receiver, mut job: PicJob) { let shared = Arc::clone(job.shared()); - let outcome = catch_unwind(AssertUnwindSafe(|| -> Result> { + let outcome = catch_unwind(AssertUnwindSafe(|| -> Result<()> { loop { let msg = rx.recv(); let Ok(msg) = msg else { break }; match msg { Msg::Slice(slice) => job.run_slice(*slice)?, Msg::Finish => { - let sum = job.finish(); - return Ok(sum); + job.finish(); + return Ok(()); } } } @@ -225,14 +194,8 @@ fn run_worker(rx: Receiver, mut job: PicJob, checksum: Arc) { )) })); match outcome { - Ok(Ok(sum)) => checksum.set(sum), - Ok(Err(e)) => { - shared.fail(&e.to_string()); - checksum.set(None); - } - Err(_) => { - shared.fail("HEVC decode: the picture's worker thread panicked"); - checksum.set(None); - } + Ok(Ok(())) => {} + Ok(Err(e)) => shared.fail(&e.to_string()), + Err(_) => shared.fail("HEVC decode: the picture's worker thread panicked"), } } diff --git a/crates/ec-h265/src/dec/sao.rs b/crates/ec-h265/src/dec/sao.rs index d075e09c..4fd34c23 100644 --- a/crates/ec-h265/src/dec/sao.rs +++ b/crates/ec-h265/src/dec/sao.rs @@ -40,9 +40,6 @@ pub fn sao_picture(pic: &mut PicState) { /// in parallel. A band reads the deblocked input (shared, read-only) and writes only /// its own rows of the output, so the bands never touch the same sample. pub fn sao_picture_threads(pic: &mut PicState, threads: usize) { - if !pic.has_pixels() { - return; - } let ro: &PicState = pic; let y = filter_plane(ro, 0, &ro.planes.y, threads); let cb = filter_plane(ro, 1, &ro.planes.cb, threads); @@ -362,7 +359,7 @@ mod tests { /// A 16x16 8-bit picture with one 16x16 CTB and no slices. fn picture() -> PicState { - PicState::new(16, 16, 4, 2, 8, 8, true) + PicState::new(16, 16, 4, 2, 8, 8) } /// SAO parameters for the luma component only. diff --git a/crates/ec-h265/src/dec/shared.rs b/crates/ec-h265/src/dec/shared.rs index 5fd0b252..c9159a10 100644 --- a/crates/ec-h265/src/dec/shared.rs +++ b/crates/ec-h265/src/dec/shared.rs @@ -55,41 +55,26 @@ fn lock(m: &Mutex) -> MutexGuard<'_, T> { } impl SharedPic { - /// The storage of a `width` by `height` picture of POC `poc`. With `pixels` - /// false no sample planes are allocated (the motion-only decode). + /// The storage of a `width` by `height` picture of POC `poc`. pub fn new( poc: i32, width: usize, height: usize, bit_depth: (u32, u32), - pixels: bool, chroma_format_idc: u32, ) -> SharedPic { - let planes = if pixels { - let (sx, sy) = crate::decode::chroma_shifts(chroma_format_idc); - let cw = width.div_ceil(1 << sx); - let ch = height.div_ceil(1 << sy); - DecodedPicture { - width, - height, - bit_depth: bit_depth.0, - bit_depth_c: bit_depth.1, - chroma_format_idc, - y: vec![0; width * height], - cb: vec![0; cw * ch], - cr: vec![0; cw * ch], - } - } else { - DecodedPicture { - width, - height, - bit_depth: 8, - bit_depth_c: 8, - chroma_format_idc, - y: Vec::new(), - cb: Vec::new(), - cr: Vec::new(), - } + let (sx, sy) = crate::decode::chroma_shifts(chroma_format_idc); + let cw = width.div_ceil(1 << sx); + let ch = height.div_ceil(1 << sy); + let planes = DecodedPicture { + width, + height, + bit_depth: bit_depth.0, + bit_depth_c: bit_depth.1, + chroma_format_idc, + y: vec![0; width * height], + cb: vec![0; cw * ch], + cr: vec![0; cw * ch], }; SharedPic { width, diff --git a/crates/ec-h265/src/decode.rs b/crates/ec-h265/src/decode.rs index 09316513..07b50c72 100644 --- a/crates/ec-h265/src/decode.rs +++ b/crates/ec-h265/src/decode.rs @@ -51,10 +51,7 @@ impl DecodedPicture { let (sx, sy) = self.chroma_shifts(); (self.width.div_ceil(1 << sx), self.height.div_ceil(1 << sy)) } - /// A picture of mid-grey samples (`1 << (bit_depth - 1)`). - pub fn grey(width: usize, height: usize, bit_depth: u32) -> DecodedPicture { - Self::grey_planes(width, height, bit_depth, bit_depth) - } + /// A picture of mid-grey samples, each plane at its own bit depth. pub fn grey_planes( @@ -62,16 +59,6 @@ impl DecodedPicture { height: usize, bit_depth: u32, bit_depth_c: u32, - ) -> DecodedPicture { - Self::grey_planes_fmt(width, height, bit_depth, bit_depth_c, 1) - } - - /// [`Self::grey_planes`] for any `chroma_format_idc`. - pub fn grey_planes_fmt( - width: usize, - height: usize, - bit_depth: u32, - bit_depth_c: u32, chroma_format_idc: u32, ) -> DecodedPicture { let mid = 1u16 << (bit_depth - 1); @@ -89,20 +76,6 @@ impl DecodedPicture { cr: vec![mid_c; cw * ch], } } - - /// The bit depth of component `c` (0 luma, 1 and 2 chroma). - pub fn bit_depth_of(&self, c: usize) -> u32 { - if c == 0 { - self.bit_depth - } else { - self.bit_depth_c - } - } - - /// The luma plane as 8-bit samples. Only meaningful for 8-bit pictures. - pub fn y_u8(&self) -> Vec { - self.y.iter().map(|&v| v as u8).collect() - } } /// Decode one IDR access unit: VPS/SPS/PPS/slice NALs in Annex-B framing, one diff --git a/crates/ec-h265/src/enc_p.rs b/crates/ec-h265/src/enc_p.rs index b8def5f7..1ea32d4d 100644 --- a/crates/ec-h265/src/enc_p.rs +++ b/crates/ec-h265/src/enc_p.rs @@ -577,7 +577,7 @@ impl<'a> PCoder<'a> { bit_depth: bd, }, ]; - let mut pic = PicState::new(w, h, ctb_log2, 2, bd, bd, true); + let mut pic = PicState::new(w, h, ctb_log2, 2, bd, bd); pic.strong_intra_smoothing = true; // as the sequence parameter set says pic.slices = vec![SliceFilter { deblock_disabled: !enc.cfg.deblock, @@ -1835,7 +1835,7 @@ pub(crate) fn code_p_picture( true, ); write_annex_b(&mut au, NalHeader::new(NalUnitType::TrailR), &rbsp, true); - let shared = Arc::new(SharedPic::new(poc, w, h, (8, 8), false, 1)); + let shared = Arc::new(SharedPic::new(poc, w, h, (8, 8), 1)); shared.publish_col(&field, h); Ok((au, recon, PrevPic { field, shared })) } diff --git a/crates/ec-h265/src/encoder.rs b/crates/ec-h265/src/encoder.rs index fe162e62..a2fb1cf2 100644 --- a/crates/ec-h265/src/encoder.rs +++ b/crates/ec-h265/src/encoder.rs @@ -989,9 +989,9 @@ impl Encoder { true, ); if let Some(rbsp) = - sei::hdr_metadata_rbsp_with(self.cfg.mastering_display, self.cfg.content_light) + sei::hdr_metadata_rbsp(self.cfg.mastering_display, self.cfg.content_light) { - write_annex_b(&mut au, sei::prefix_sei_header(), &rbsp, true); + write_annex_b(&mut au, NalHeader::new(NalUnitType::PrefixSei), &rbsp, true); } // Entry points count the bytes as they appear in the NAL, emulation @@ -1033,7 +1033,7 @@ impl Encoder { (&bcb, w / 2 * bps, w / 2 * bps, h / 2), (&bcr, w / 2 * bps, w / 2 * bps, h / 2), ]); - write_annex_b(&mut au, sei::suffix_sei_header(), &rbsp, false); + write_annex_b(&mut au, NalHeader::new(NalUnitType::SuffixSei), &rbsp, false); } au } diff --git a/crates/ec-h265/src/transform.rs b/crates/ec-h265/src/transform.rs index 946f704c..aa9ba7d5 100644 --- a/crates/ec-h265/src/transform.rs +++ b/crates/ec-h265/src/transform.rs @@ -686,13 +686,6 @@ pub fn dequantize_bd(levels: &[i32], scaled: &mut [i32], n: usize, qp: i32, bit_ dequantize_weighted(levels, scaled, n, qp, bit_depth, None); } -/// [`dequantize`] with a scaling matrix (8.6.4.2): `m` is the block's -/// `ScalingFactor` in raster order, or `None` for the flat 16 that a stream -/// without scaling lists (or a transform-skipped block larger than 4x4) uses. -pub fn dequantize_with(levels: &[i32], scaled: &mut [i32], n: usize, qp: i32, m: Option<&[u8]>) { - dequantize_weighted(levels, scaled, n, qp, 8, m); -} - /// The loop behind every dequantize entry point: one block at `bit_depth`, with /// the weight matrix `m` (or flat 16 for `None`). pub fn dequantize_weighted( diff --git a/crates/ec-h265/tests/it/common.rs b/crates/ec-h265/tests/it/common.rs index aca1526c..e4e33c84 100644 --- a/crates/ec-h265/tests/it/common.rs +++ b/crates/ec-h265/tests/it/common.rs @@ -192,9 +192,14 @@ pub fn pin_or_panic(site: &str, path: &std::path::Path, bytes: &[u8]) { pub const PLANE_NAMES: [&str; 3] = ["Y", "Cb", "Cr"]; +/// The bit depth of component `c` (0 luma, 1 and 2 chroma). +pub fn bit_depth_of(pic: &DecodedPicture, c: usize) -> u32 { + if c == 0 { pic.bit_depth } else { pic.bit_depth_c } +} + /// Bytes per sample of component `c`: 1 for 8-bit, 2 (little endian) above 8 bits. pub fn bytes_per_sample(pic: &DecodedPicture, c: usize) -> usize { - if pic.bit_depth_of(c) > 8 { 2 } else { 1 } + if bit_depth_of(pic, c) > 8 { 2 } else { 1 } } /// The samples of a picture cropped to its conformance window, as the bytes a YUV @@ -424,7 +429,7 @@ pub fn plane_matches(pic: &DecodedPicture, expected: &PictureHash) -> Vec .map(|(i, &w)| { planes .get(i) - .is_some_and(|p| plane_crc(p, pic.bit_depth_of(i)) == w) + .is_some_and(|p| plane_crc(p, bit_depth_of(pic, i)) == w) }) .collect(), PictureHash::Checksum(want) => want @@ -433,7 +438,7 @@ pub fn plane_matches(pic: &DecodedPicture, expected: &PictureHash) -> Vec .map(|(i, &w)| { planes .get(i) - .is_some_and(|p| plane_checksum(p, dims[i].0, pic.bit_depth_of(i)) == w) + .is_some_and(|p| plane_checksum(p, dims[i].0, bit_depth_of(pic, i)) == w) }) .collect(), } diff --git a/crates/ec-h265/tests/it/rext_generated.rs b/crates/ec-h265/tests/it/rext_generated.rs index 2701c1f3..1618bcda 100644 --- a/crates/ec-h265/tests/it/rext_generated.rs +++ b/crates/ec-h265/tests/it/rext_generated.rs @@ -7,6 +7,8 @@ use std::path::Path; use ec_h265::Decoder; use ec_h265_syntax::md5::Md5; +use crate::common::bit_depth_of; + fn hex(d: [u8; 16]) -> String { d.iter().map(|b| format!("{b:02x}")).collect() } @@ -67,7 +69,7 @@ fn output_md5(data: &[u8]) -> Result<(String, usize), String> { if plane.is_empty() { continue; } - let wide = p.bit_depth_of(c) > 8; + let wide = bit_depth_of(p, c) > 8; let mut bytes = Vec::new(); for y in y0..y1 { for x in x0..x1 {