forge
Explore
Log in

Don't you dare go Hollow

shim(rusty_h264): a whole Annex B stream decodes in one call

Browse files at this commit

e63caffdc30f980267da71cfec3f8b244dca0ce6Ahmet Kaan GümüşParent: 838fa63
`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.rsmodified+32 -6
@@ -113,17 +113,37 @@ impl Decoder {
113 /// Decode one access unit (Annex B, one coded picture), returning the113 /// Decode one access unit (Annex B, one coded picture), returning the
114 /// picture it completed if any.114 /// picture it completed if any.
115 pub fn decode(&mut self, au: &[u8]) -> Result<Option<YuvFrame>, Error> {115 pub fn decode(&mut self, au: &[u8]) -> Result<Option<YuvFrame>, Error> {
116- for nal in ec_h264_syntax::AnnexBIter::new(au) {116+ self.feed(au)?;
117+ if self.inner.picture_open() {
118+ self.inner.end_picture()?;
119+ }
120+ Ok(self.take())
121+ }
122+
123+ /// Decode a whole Annex B stream at once, returning every picture in it —
124+ /// what a test that holds an encoder's output in memory asks for, where
125+ /// [`Decoder::decode`] serves a demuxer handing over one unit at a time.
126+ pub fn decode_stream(&mut self, stream: &[u8]) -> Result<Vec<YuvFrame>, Error> {
127+ let mut out = Vec::new();
128+ self.feed(stream)?;
129+ // The tail: whatever the last picture and the reordering delay hold.
130+ self.inner.flush()?;
131+ while let Some(frame) = self.take() {
132+ out.push(frame);
133+ }
134+ Ok(out)
135+ }
136+
137+ /// Push every NAL in `bytes`, closing a picture wherever one ends.
138+ fn feed(&mut self, bytes: &[u8]) -> Result<(), Error> {
139+ for nal in ec_h264_syntax::AnnexBIter::new(bytes) {
117 if self.inner.push_nal(nal)? == NalOutcome::PictureBoundary {140 if self.inner.push_nal(nal)? == NalOutcome::PictureBoundary {
118- // Two pictures in one access unit: close the first one.141+ // The next picture starts here: close the one before it.
119 self.inner.end_picture()?;142 self.inner.end_picture()?;
120 self.inner.push_nal(nal)?;143 self.inner.push_nal(nal)?;
121 }144 }
122 }145 }
123- if self.inner.picture_open() {146+ Ok(())
124- self.inner.end_picture()?;
125- }
126- Ok(self.take())
127 }147 }
128 148
129 /// Collect a picture the reordering delay is still holding, at end of149 /// Collect a picture the reordering delay is still holding, at end of
@@ -290,6 +310,12 @@ mod tests {
290 / (w * h) as f64;310 / (w * h) as f64;
291 assert!(mse < 25.0, "picture {t}: mean squared error {mse:.1}");311 assert!(mse < 25.0, "picture {t}: mean squared error {mse:.1}");
292 }312 }
313+
314+ // And the same units as one stream, which is how a test that kept the
315+ // encoder's output in a `Vec` decodes it back.
316+ let whole: Vec<u8> = units.concat();
317+ let frames = Decoder::new().decode_stream(&whole).expect("decode stream");
318+ assert_eq!(frames.len(), units.len(), "one picture per access unit");
293 }319 }
294 320
295 /// A frame whose geometry does not match the encoder is refused by name.321 /// A frame whose geometry does not match the encoder is refused by name.