forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB84fa44cTahinli2 months ago3331 commits

stream.rs99 lines, 3.4 KiBHistoryRawDownload
1//! Whole-stream decoding: an IVF file in, decoded pictures out.
2//!
3//! VP9 frames are not self-delimiting — a frame's total size only the
4//! container knows (and one IVF chunk may hold a superframe with hidden
5//! ALTREF frames) — so the whole-stream API takes the stream in the
6//! container VP9 actually ships in: IVF (`DKIF`/`VP90`). Single frames
7//! or chunks from any other demuxer feed
8//! [`crate::decode::Decoder::decode`] directly, which splits superframes
9//! itself.
10//!
11//! The shape mirrors `ec-vp8`'s: [`decode_stream`] collects every shown
12//! picture into a `Vec`, and [`decode_stream_with`] hands each picture
13//! to a sink the moment it exists.
14
15use crate::decode::{Decoder, Picture};
16use crate::tables::ensure;
17use crate::{Error, Result};
18
19/// The IVF file magic, `DKIF`.
20const DKIF: &[u8; 4] = b"DKIF";
21/// The fourcc for VP9 video (`VP90`).
22const VP90: &[u8; 4] = b"VP90";
23
24/// One demuxed IVF frame: presentation timestamp plus the compressed
25/// VP9 payload (possibly a superframe chunk).
26struct IvfFrame<'a> {
27 pts: u64,
28 data: &'a [u8],
29}
30
31/// Split an IVF byte stream into its frames, validating the container
32/// envelope as [`Error`]s rather than panics.
33fn demux(data: &[u8]) -> Result<Vec<IvfFrame<'_>>> {
34 let mut frames = Vec::new();
35 let err = |what: &str| Error::corrupt(format!("ivf: {what}"));
36 ensure(data.len() >= 32, "truncated IVF header")?;
37 if &data[0..4] != DKIF {
38 return Err(err("not DKIF"));
39 }
40 if &data[8..12] != VP90 {
41 return Err(err("fourcc is not VP90"));
42 }
43 let hdr_len = u16::from_le_bytes([data[6], data[7]]) as usize;
44 let mut pos = hdr_len.max(32);
45 while pos + 12 <= data.len() {
46 let sz = u32::from_le_bytes(data[pos..pos + 4].try_into().unwrap()) as usize;
47 let pts = u64::from_le_bytes(data[pos + 4..pos + 12].try_into().unwrap());
48 pos += 12;
49 if sz == 0 {
50 return Err(err("zero-length frame"));
51 }
52 if pos + sz > data.len() {
53 return Err(err("truncated frame payload"));
54 }
55 frames.push(IvfFrame {
56 pts,
57 data: &data[pos..pos + sz],
58 });
59 pos += sz;
60 }
61 Ok(frames)
62}
63
64/// Decode every shown frame in an IVF VP9 stream, in presentation order.
65///
66/// The stream must start with a key frame; hidden frames
67/// (`show_frame == 0`) update the reference buffers but are not part of
68/// the output.
69///
70/// # Errors
71/// As [`demux`] reports for the container, [`Decoder::decode`] for the
72/// frames, plus `vp9 inter` refusals once a lane-ineligible frame shows
73/// up.
74pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
75 let mut out = Vec::new();
76 decode_stream_with(data, |pic, _, _| {
77 out.push(pic.clone());
78 Ok(())
79 })?;
80 Ok(out)
81}
82
83/// Streaming form of [`decode_stream`]: hand every shown picture to
84/// `sink` the moment it exists. `sink` receives the picture, the
85/// decode-order index of the IVF chunk it came from, and the IVF
86/// presentation timestamp. An `Err` from `sink` aborts the decode.
87pub fn decode_stream_with(
88 data: &[u8],
89 mut sink: impl FnMut(&Picture, usize, u64) -> Result<()>,
90) -> Result<()> {
91 let frames = demux(data)?;
92 let mut decoder = Decoder::new();
93 for (i, frame) in frames.iter().enumerate() {
94 if let Some(pic) = decoder.decode(frame.data)? {
95 sink(&pic, i, frame.pts)?;
96 }
97 }
98 Ok(())
99}