ec-vp8 M4: public whole-stream API (ec-av1 decode_stream shape)
Browse files at this commit93dee5c1a63bbcf6f7737180880fc0e7298fd55aParent: 80115ee5 changed files +225 -0
- crates/ec-vp8/src/decode.rs +1 -0
- crates/ec-vp8/src/lib.rs +1 -0
- crates/ec-vp8/src/stream.rs +134 -0
- crates/ec-vp8/tests/ivf.rs +2 -0
- crates/ec-vp8/tests/stream_exact.rs +87 -0
crates/ec-vp8/src/decode.rsmodified+1 -0
| @@ -171,6 +171,7 @@ pub struct Decoder { | ||
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /// A decoded, displayable frame: cropped contiguous planes. |
| 174 | +#[derive(Clone)] | |
| 174 | 175 | pub struct Picture { |
| 175 | 176 | /// Luma plane, `stride`-spaced rows of `width` pixels. |
| 176 | 177 | pub y: Vec<u8>, |
crates/ec-vp8/src/lib.rsmodified+1 -0
| @@ -22,6 +22,7 @@ pub mod intra; | ||
| 22 | 22 | pub mod loopfilter; |
| 23 | 23 | mod mc; |
| 24 | 24 | pub mod modes; |
| 25 | +pub mod stream; | |
| 25 | 26 | pub mod tables; |
| 26 | 27 | pub mod tokens; |
| 27 | 28 | pub mod transform; |
crates/ec-vp8/src/stream.rsadded+134 -0
| @@ -0,0 +1,134 @@ | ||
| 1 | +//! Whole-stream decoding: an IVF file in, decoded pictures out. | |
| 2 | +//! | |
| 3 | +//! VP8 frames are not self-delimiting — a bare frame is a 3-byte | |
| 4 | +//! uncompressed tag followed by partitions whose total size only the | |
| 5 | +//! container knows — so the whole-stream API takes the stream in the | |
| 6 | +//! container VP8 actually ships in: IVF (the RFC's companion container, | |
| 7 | +//! `DKIF`/`VP80`; also what vpxenc, ffmpeg and the WebM toolchain emit | |
| 8 | +//! for raw VP8 elementaries). Single frames from any other demuxer feed | |
| 9 | +//! [`crate::decode::Decoder::decode`] directly. | |
| 10 | +//! | |
| 11 | +//! The shape mirrors `ec_av1::stream`: [`decode_stream`] collects every | |
| 12 | +//! shown picture into a `Vec`, and [`decode_stream_with`] hands each | |
| 13 | +//! picture to a sink the moment it exists. | |
| 14 | + | |
| 15 | +use crate::decode::{Decoder, Picture}; | |
| 16 | +use crate::{Error, Result}; | |
| 17 | + | |
| 18 | +/// The IVF file magic, `DKIF`. | |
| 19 | +const DKIF: &[u8; 4] = b"DKIF"; | |
| 20 | +/// The fourcc for VP8 video (`VP80`; `VP8L`/`VP8x` are WebP-lossless/alpha). | |
| 21 | +const VP80: &[u8; 4] = b"VP80"; | |
| 22 | + | |
| 23 | +/// One demuxed IVF frame: presentation timestamp plus the compressed | |
| 24 | +/// VP8 payload. | |
| 25 | +struct IvfFrame<'a> { | |
| 26 | + pts: u64, | |
| 27 | + data: &'a [u8], | |
| 28 | +} | |
| 29 | + | |
| 30 | +/// Split an IVF byte stream into its frames, validating the container | |
| 31 | +/// envelope (magic, fourcc, framing arithmetic) as [`Error`]s rather | |
| 32 | +/// than panics. | |
| 33 | +fn demux(data: &[u8]) -> Result<Vec<IvfFrame<'_>>> { | |
| 34 | + if data.len() < 32 || &data[0..4] != DKIF { | |
| 35 | + return Err(Error::corrupt("not an IVF stream (missing DKIF magic)")); | |
| 36 | + } | |
| 37 | + // Bytes 4..6 version (informational), 6..8 header length, 8..12 fourcc. | |
| 38 | + let hdr_len = u16::from_le_bytes([data[6], data[7]]) as usize; | |
| 39 | + if data.len() < hdr_len || hdr_len < 12 { | |
| 40 | + return Err(Error::corrupt("IVF header length out of range")); | |
| 41 | + } | |
| 42 | + if &data[8..12] != VP80 { | |
| 43 | + return Err(Error::unsupported( | |
| 44 | + "IVF fourcc", | |
| 45 | + "the stream is not VP80 (VP8 video)", | |
| 46 | + )); | |
| 47 | + } | |
| 48 | + // Bytes 12..16 carry the nominal width/height; VP8 streams set them | |
| 49 | + // to 0 and take the real dimensions from the key frame, so the | |
| 50 | + // decoder does not consult them. | |
| 51 | + let _ = u16::from_le_bytes([data[12], data[13]]); | |
| 52 | + let _ = u16::from_le_bytes([data[14], data[15]]); | |
| 53 | + let mut frames = Vec::new(); | |
| 54 | + let mut pos = hdr_len; | |
| 55 | + while pos + 12 <= data.len() { | |
| 56 | + let sz = | |
| 57 | + u32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]) | |
| 58 | + as usize; | |
| 59 | + let pts = u64::from_le_bytes([ | |
| 60 | + data[pos + 4], | |
| 61 | + data[pos + 5], | |
| 62 | + data[pos + 6], | |
| 63 | + data[pos + 7], | |
| 64 | + data[pos + 8], | |
| 65 | + data[pos + 9], | |
| 66 | + data[pos + 10], | |
| 67 | + data[pos + 11], | |
| 68 | + ]); | |
| 69 | + pos += 12; | |
| 70 | + if pos + sz > data.len() { | |
| 71 | + return Err(Error::corrupt(format!( | |
| 72 | + "IVF frame {} extends past end of stream (need {} bytes \ | |
| 73 | + at offset {})", | |
| 74 | + frames.len(), | |
| 75 | + sz, | |
| 76 | + pos | |
| 77 | + ))); | |
| 78 | + } | |
| 79 | + frames.push(IvfFrame { | |
| 80 | + pts, | |
| 81 | + data: &data[pos..pos + sz], | |
| 82 | + }); | |
| 83 | + pos += sz; | |
| 84 | + } | |
| 85 | + Ok(frames) | |
| 86 | +} | |
| 87 | + | |
| 88 | +/// Decode every frame in an IVF VP8 stream, in presentation order. | |
| 89 | +/// | |
| 90 | +/// The stream must start with a key frame; hidden frames (alternate | |
| 91 | +/// references, `show_frame == 0`) update the reference buffers but are | |
| 92 | +/// not part of the output, exactly as a player would not display them. | |
| 93 | +/// | |
| 94 | +/// # Errors | |
| 95 | +/// Returns an error when the container is truncated or not VP80 (as | |
| 96 | +/// [`demux`] reports it), when a frame is malformed (as | |
| 97 | +/// [`crate::decode::Decoder::decode`] reports it), or when an inter | |
| 98 | +/// frame appears before any key frame has supplied references. | |
| 99 | +pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |
| 100 | + let mut pictures: Vec<Picture> = Vec::new(); | |
| 101 | + decode_stream_with(data, |picture, _decode_idx, _pts| { | |
| 102 | + pictures.push(picture.clone()); | |
| 103 | + Ok(()) | |
| 104 | + })?; | |
| 105 | + Ok(pictures) | |
| 106 | +} | |
| 107 | + | |
| 108 | +/// Streaming form of [`decode_stream`]: hand every shown picture to | |
| 109 | +/// `sink` the moment it exists and drop it right after, so peak memory | |
| 110 | +/// is one output picture plus the reference frames instead of the whole | |
| 111 | +/// decoded stream. `decode_stream` itself is this function collecting | |
| 112 | +/// into a `Vec`. | |
| 113 | +/// | |
| 114 | +/// `sink` is called with the picture, the decode-order index of the | |
| 115 | +/// coded frame it came from, and the IVF presentation timestamp. A | |
| 116 | +/// hidden frame (`show_frame == 0`) updates the decoder state but | |
| 117 | +/// produces no picture, so the sink is not called for it. An `Err` the | |
| 118 | +/// sink returns aborts the decode and is returned unchanged. | |
| 119 | +/// | |
| 120 | +/// # Errors | |
| 121 | +/// As [`decode_stream`], plus whatever `sink` returns. | |
| 122 | +pub fn decode_stream_with( | |
| 123 | + data: &[u8], | |
| 124 | + mut sink: impl FnMut(&Picture, usize, u64) -> Result<()>, | |
| 125 | +) -> Result<()> { | |
| 126 | + let frames = demux(data)?; | |
| 127 | + let mut dec = Decoder::new(); | |
| 128 | + for (decode_idx, frame) in frames.iter().enumerate() { | |
| 129 | + if let Some(picture) = dec.decode(frame.data)? { | |
| 130 | + sink(&picture, decode_idx, frame.pts)?; | |
| 131 | + } | |
| 132 | + } | |
| 133 | + Ok(()) | |
| 134 | +} |
crates/ec-vp8/tests/ivf.rsmodified+2 -0
| @@ -7,6 +7,7 @@ pub struct IvfFrame { | ||
| 7 | 7 | #[allow(dead_code)] |
| 8 | 8 | pub pts: u64, |
| 9 | 9 | /// The compressed VP8 frame payload. |
| 10 | + #[allow(dead_code)] // pts-only consumers exist in the stream witnesses | |
| 10 | 11 | pub data: Vec<u8>, |
| 11 | 12 | } |
| 12 | 13 | |
| @@ -47,6 +48,7 @@ pub fn parse_ivf(bytes: &[u8]) -> (&str, u16, u16, Vec<IvfFrame>) { | ||
| 47 | 48 | |
| 48 | 49 | /// Extract the VP8 payload from a lossy WebP file (RIFF/WEBP with a VP8 |
| 49 | 50 | /// chunk) — the same VP8 key-frame bytes in a different container. |
| 51 | +#[allow(dead_code)] // only the keyframe_exact binary exercises WebP | |
| 50 | 52 | pub fn webp_vp8_chunk(bytes: &[u8]) -> Vec<u8> { |
| 51 | 53 | assert_eq!(&bytes[0..4], b"RIFF"); |
| 52 | 54 | assert_eq!(&bytes[8..12], b"WEBP"); |
crates/ec-vp8/tests/stream_exact.rsadded+87 -0
| @@ -0,0 +1,87 @@ | ||
| 1 | +//! M4 witness: the whole-stream public API (`ec_vp8::stream`) decodes | |
| 2 | +//! every fixture byte-exact against ffmpeg and refuses malformed | |
| 3 | +//! containers with errors, not panics. | |
| 4 | + | |
| 5 | +mod ivf; | |
| 6 | + | |
| 7 | +use ec_vp8::stream::decode_stream; | |
| 8 | + | |
| 9 | +/// Every IVF fixture in the witness set (the WebP still is covered by | |
| 10 | +/// `keyframe_exact::vp8_in_webp_still_matches_ffmpeg`). | |
| 11 | +const FIXTURES: &[&str] = &[ | |
| 12 | + "kf-16x16-black-q0.ivf", | |
| 13 | + "kf-32x16-q35.ivf", | |
| 14 | + "kf-76x52-q30.ivf", | |
| 15 | + "kf-96x80-q63.ivf", | |
| 16 | + "kf-160x96-q20.ivf", | |
| 17 | + "kf-160x96-q40.ivf", | |
| 18 | + "kf-172x144-q25.ivf", | |
| 19 | + "gop-96x80-v1.ivf", | |
| 20 | + "gop-160x96.ivf", | |
| 21 | + "mparts-160x96.ivf", | |
| 22 | + "altref-160x96.ivf", | |
| 23 | + "clip-obs-320x192.ivf", | |
| 24 | +]; | |
| 25 | + | |
| 26 | +#[test] | |
| 27 | +fn decode_stream_matches_ffmpeg_on_every_fixture() { | |
| 28 | + let Some(dir) = ivf::fixture_dir() else { | |
| 29 | + panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); | |
| 30 | + }; | |
| 31 | + for name in FIXTURES { | |
| 32 | + let path = dir.join(name); | |
| 33 | + let bytes = std::fs::read(&path).unwrap(); | |
| 34 | + let (_, w, h, frames) = ivf::parse_ivf(&bytes); | |
| 35 | + let all = ffmpeg_raw_yuv(&path); | |
| 36 | + let frame_len = usize::from(w) * usize::from(h) * 3 / 2; | |
| 37 | + assert_eq!(all.len(), frames.len() * frame_len, "{name}: ffmpeg output size"); | |
| 38 | + assert!(!frames[0].data.is_empty(), "{name}: empty first frame"); | |
| 39 | + | |
| 40 | + let pictures = | |
| 41 | + decode_stream(&bytes).unwrap_or_else(|e| panic!("{name}: decode failed: {e}")); | |
| 42 | + assert_eq!(pictures.len(), frames.len(), "{name}: picture count"); | |
| 43 | + for (i, pic) in pictures.iter().enumerate() { | |
| 44 | + let expect = &all[i * frame_len..(i + 1) * frame_len]; | |
| 45 | + let (cw, ch) = (usize::from(w + 1) / 2, usize::from(h + 1) / 2); | |
| 46 | + assert_eq!(pic.y, &expect[..usize::from(w) * usize::from(h)], "{name} frame {i}: Y"); | |
| 47 | + assert_eq!(pic.u, &expect[usize::from(w) * usize::from(h)..][..cw * ch], "{name} frame {i}: U"); | |
| 48 | + assert_eq!(pic.v, &expect[usize::from(w) * usize::from(h) + cw * ch..], "{name} frame {i}: V"); | |
| 49 | + } | |
| 50 | + } | |
| 51 | +} | |
| 52 | + | |
| 53 | +/// ffmpeg's decoded rawvideo byte stream for an IVF file. | |
| 54 | +fn ffmpeg_raw_yuv(path: &std::path::Path) -> Vec<u8> { | |
| 55 | + let out = std::process::Command::new("ffmpeg") | |
| 56 | + .args(["-v", "error", "-i"]) | |
| 57 | + .arg(path) | |
| 58 | + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) | |
| 59 | + .output() | |
| 60 | + .expect("ffmpeg must be on PATH for the witnesses"); | |
| 61 | + assert!(out.status.success(), "ffmpeg failed on {path:?}"); | |
| 62 | + out.stdout | |
| 63 | +} | |
| 64 | + | |
| 65 | +#[test] | |
| 66 | +fn decode_stream_is_error_not_panic_on_bad_containers() { | |
| 67 | + let Some(dir) = ivf::fixture_dir() else { | |
| 68 | + panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); | |
| 69 | + }; | |
| 70 | + let bytes = std::fs::read(dir.join("gop-160x96.ivf")).unwrap(); | |
| 71 | + | |
| 72 | + // Missing magic. | |
| 73 | + let mut bad = bytes.clone(); | |
| 74 | + bad[0..4].copy_from_slice(b"JUNK"); | |
| 75 | + assert!(decode_stream(&bad).is_err(), "non-DKIF accepted"); | |
| 76 | + | |
| 77 | + // Wrong fourcc (a WebP-lossless VP8L tag is not a VP8 video stream). | |
| 78 | + let mut bad = bytes.clone(); | |
| 79 | + bad[8..12].copy_from_slice(b"VP8L"); | |
| 80 | + assert!(decode_stream(&bad).is_err(), "non-VP80 fourcc accepted"); | |
| 81 | + | |
| 82 | + // A frame header claiming more payload than the stream carries. | |
| 83 | + let mut bad = bytes.clone(); | |
| 84 | + bad[32..36].copy_from_slice(&u32::MAX.to_le_bytes()); | |
| 85 | + assert!(decode_stream(&bad).is_err(), "truncated frame accepted"); | |
| 86 | +} | |
| 87 | + |