From e63caffdc30f980267da71cfec3f8b244dca0ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 18:56:59 +0300 Subject: [PATCH] shim(rusty_h264): a whole Annex B stream decodes in one call `Decoder::decode_stream` is the one piece of the incumbent's surface the shim had missed, and edith's `tests/hw_encode.rs` is what asks for it: it holds a hardware encoder's whole output in a `Vec` and decodes it back in software, with no demuxer to hand over one access unit at a time. The NAL loop `decode` already ran is now `feed`, shared by both: pictures close where a picture boundary says they do, and `decode_stream` collects after a `flush`, so the last picture -- and anything a reordering delay still holds -- comes back with the rest. --- shims/rusty_h264/src/lib.rs | 38 +++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/shims/rusty_h264/src/lib.rs b/shims/rusty_h264/src/lib.rs index ca44a20a..bb85095f 100644 --- a/shims/rusty_h264/src/lib.rs +++ b/shims/rusty_h264/src/lib.rs @@ -113,17 +113,37 @@ impl Decoder { /// Decode one access unit (Annex B, one coded picture), returning the /// picture it completed if any. pub fn decode(&mut self, au: &[u8]) -> Result, Error> { - for nal in ec_h264_syntax::AnnexBIter::new(au) { + self.feed(au)?; + if self.inner.picture_open() { + self.inner.end_picture()?; + } + Ok(self.take()) + } + + /// Decode a whole Annex B stream at once, returning every picture in it — + /// what a test that holds an encoder's output in memory asks for, where + /// [`Decoder::decode`] serves a demuxer handing over one unit at a time. + pub fn decode_stream(&mut self, stream: &[u8]) -> Result, Error> { + let mut out = Vec::new(); + self.feed(stream)?; + // The tail: whatever the last picture and the reordering delay hold. + self.inner.flush()?; + while let Some(frame) = self.take() { + out.push(frame); + } + Ok(out) + } + + /// Push every NAL in `bytes`, closing a picture wherever one ends. + fn feed(&mut self, bytes: &[u8]) -> Result<(), Error> { + for nal in ec_h264_syntax::AnnexBIter::new(bytes) { if self.inner.push_nal(nal)? == NalOutcome::PictureBoundary { - // Two pictures in one access unit: close the first one. + // The next picture starts here: close the one before it. self.inner.end_picture()?; self.inner.push_nal(nal)?; } } - if self.inner.picture_open() { - self.inner.end_picture()?; - } - Ok(self.take()) + Ok(()) } /// Collect a picture the reordering delay is still holding, at end of @@ -290,6 +310,12 @@ mod tests { / (w * h) as f64; assert!(mse < 25.0, "picture {t}: mean squared error {mse:.1}"); } + + // And the same units as one stream, which is how a test that kept the + // encoder's output in a `Vec` decodes it back. + let whole: Vec = units.concat(); + let frames = Decoder::new().decode_stream(&whole).expect("decode stream"); + assert_eq!(frames.len(), units.len(), "one picture per access unit"); } /// A frame whose geometry does not match the encoder is refused by name.