diff --git a/crates/ec-h265/src/dec/dpb.rs b/crates/ec-h265/src/dec/dpb.rs index 80bed595..7d05a503 100644 --- a/crates/ec-h265/src/dec/dpb.rs +++ b/crates/ec-h265/src/dec/dpb.rs @@ -93,6 +93,35 @@ pub struct Frame { } impl Frame { + /// The picture cropped to the conformance window, at the display size. + pub fn cropped(&self) -> DecodedPicture { + let p = &self.picture; + let (sx, sy) = crate::decode::chroma_shifts(p.chroma_format_idc); + let (left, top) = ((self.window.left << sx) as usize, (self.window.top << sy) as usize); + let width = p.width - left - ((self.window.right << sx) as usize); + let height = p.height - top - ((self.window.bottom << sy) as usize); + let crop = |plane: &[u16], stride: usize, x: usize, y: usize, w: usize, h: usize| { + (y..y + h) + .flat_map(|r| plane[r * stride + x..r * stride + x + w].iter().copied()) + .collect::>() + }; + let cw = p.width >> sx; + let chroma = |plane: &[u16]| match plane.is_empty() { + true => Vec::new(), + false => crop(plane, cw, left >> sx, top >> sy, width >> sx, height >> sy), + }; + DecodedPicture { + width, + height, + bit_depth: p.bit_depth, + bit_depth_c: p.bit_depth_c, + chroma_format_idc: p.chroma_format_idc, + y: crop(&p.y, p.width, left, top, width, height), + cb: chroma(&p.cb), + cr: chroma(&p.cr), + } + } + /// The mastering display colour volume in force for this frame (SEI 137). pub fn mastering_display(&self) -> Option<&MasteringDisplay> { self.meta.mastering_display.as_ref() diff --git a/crates/ec-h265/src/dec/mod.rs b/crates/ec-h265/src/dec/mod.rs index 314ffdb6..cb92670f 100644 --- a/crates/ec-h265/src/dec/mod.rs +++ b/crates/ec-h265/src/dec/mod.rs @@ -310,6 +310,12 @@ impl Decoder { Ok(()) } + /// End of the current access unit: finish the open picture now instead of + /// at the next picture's first slice, so a no-reorder stream outputs it at once. + pub fn end_picture(&mut self) { + self.finish_picture(); + } + /// End of stream: finish the open picture and output everything pending. pub fn flush(&mut self) -> Result<()> { self.finish_picture(); diff --git a/crates/ec-h265/tests/it/encode_threads.rs b/crates/ec-h265/tests/it/encode_threads.rs index 0f11cb69..8624d39b 100644 --- a/crates/ec-h265/tests/it/encode_threads.rs +++ b/crates/ec-h265/tests/it/encode_threads.rs @@ -174,3 +174,25 @@ fn sequence_encoder_matches_encode_sequence_and_forces_idrs() { "forced-IDR stream does not decode to the reconstruction" ); } + +#[test] +fn end_picture_outputs_each_access_unit_cropped() { + let frames = moving_clip(4, 200, 120); + let mut cfg = EncoderConfig::new(200, 120); + cfg.rate_control = RateControl::ConstantQp(30); + cfg.keyint = 4; + cfg.keep_recon = true; + cfg.threads = 1; + let mut seq = ec_h265::SequenceEncoder::new(Encoder::new(cfg).expect("encoder")).expect("sequence"); + let mut dec = Decoder::new(); + for f in &frames { + let pic = seq.encode(f, false).expect("encode"); + dec.decode_annex_b(&pic.au).expect("decode"); + dec.end_picture(); + let out = dec.next_frame().expect("a frame per access unit").cropped(); + assert_eq!((out.width, out.height), (200, 120)); + let recon = pic.recon.expect("recon"); + let y: Vec = out.y.iter().map(|&v| v as u8).collect(); + assert!(*y == *recon.planes[0].data, "cropped luma differs from the reconstruction"); + } +}