shim(rusty_h264): a whole Annex B stream decodes in one call
Browse files at this commite63caffdc30f980267da71cfec3f8b244dca0ce6Parent: 838fa631 changed file +32 -6
- shims/rusty_h264/src/lib.rs +32 -6
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 the | 113 | /// 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.push_nal(nal)? == NalOutcome::PictureBoundary { | 117 | + if self.inner.picture_open() { |
| 118 | - // Two pictures in one access unit: close the first one. | ||
| 119 | self.inner.end_picture()?; | 118 | self.inner.end_picture()?; |
| 120 | - self.inner.push_nal(nal)?; | ||
| 121 | } | 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); | ||
| 122 | } | 133 | } |
| 123 | - if self.inner.picture_open() { | 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) { | ||
| 140 | + if self.inner.push_nal(nal)? == NalOutcome::PictureBoundary { | ||
| 141 | + // The next picture starts here: close the one before it. | ||
| 124 | self.inner.end_picture()?; | 142 | self.inner.end_picture()?; |
| 143 | + self.inner.push_nal(nal)?; | ||
| 125 | } | 144 | } |
| 126 | - Ok(self.take()) | 145 | + } |
| 146 | + Ok(()) | ||
| 127 | } | 147 | } |
| 128 | 148 | ||
| 129 | /// Collect a picture the reordering delay is still holding, at end of | 149 | /// 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. |