Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Address review: AV1 area cap is exactly level 6.3 (16384 x 8704); document new limits; report skipped seeds

Logan Jenningsbd3b2097bbb2
diff --git a/crates/av1/README.md b/crates/av1/README.md
index 354484158..a98ad74b8 100644
--- a/crates/av1/README.md
+++ b/crates/av1/README.md
@@ -125,6 +125,12 @@ motion vectors nor CDFs reach another frame — skip deblocking, CDEF and loop r
(super-resolution still runs, it sets the output size) and are flagged `Picture::draft`. In
SVT-AV1's hierarchical GOPs that is the top layer, half of the frames.
+## Limits
+
+Frames larger than AV1 level 6.3 allows (16384 x 8704 luma samples, and at most 16384 per side) are refused
+with `Error::Invalid("frame size")`. The sequence header can code up to 65536 x 65536, which would ask for
+gigabytes of buffers from a few bytes of hostile input; no real stream comes close to level 6.3.
+
## API
```rust
diff --git a/crates/av1/src/decoder.rs b/crates/av1/src/decoder.rs
index d10068fcc..33c4e9a6c 100644
--- a/crates/av1/src/decoder.rs
+++ b/crates/av1/src/decoder.rs
@@ -23,9 +23,15 @@ use crate::stats::{DecodeStats, Stage, Timer};
use crate::tile::TileDecoder;
use crate::{Error, Picture, Result};
-/// Largest accepted frame side and area: AV1 level 6.3 stops at 16384 x 8704; the sequence header allows 65536 x 65536 (gigabytes of buffers).
+/// Largest accepted frame side and area: AV1 level 6.3 stops at 16384 x 8704 (the area limit is exactly that
+/// product, so a conforming maximum-size frame is accepted); the sequence header allows 65536 x 65536 (gigabytes of buffers).
const MAX_FRAME_DIM: u32 = 16384;
-const MAX_FRAME_AREA: u64 = 1 << 27;
+const MAX_FRAME_AREA: u64 = 16384 * 8704;
+
+/// Whether a frame of `width` x `height` luma samples is within the decoder's resource limits.
+fn frame_size_allowed(width: u32, height: u32) -> bool {
+ width <= MAX_FRAME_DIM && height <= MAX_FRAME_DIM && u64::from(width) * u64::from(height) <= MAX_FRAME_AREA
+}
const OBU_SEQUENCE_HEADER_T: u32 = 1;
const OBU_TEMPORAL_DELIMITER_T: u32 = 2;
const OBU_FRAME_HEADER_T: u32 = 3;
@@ -508,7 +514,7 @@ impl Decoder {
}
// Encoders exist that code frames slightly larger than the sequence maximum (e.g. a
// 270-line stream coded as 272 lines); like other decoders we accept them.
- if fh.upscaled_width > MAX_FRAME_DIM || fh.frame_height > MAX_FRAME_DIM || u64::from(fh.upscaled_width) * u64::from(fh.frame_height) > MAX_FRAME_AREA {
+ if !frame_size_allowed(fh.upscaled_width, fh.frame_height) {
return Err(Error::Invalid("frame size"));
}
if !fh.frame_is_intra {
@@ -950,3 +956,22 @@ fn project(fs: &mut FrameState, src: usize, dst_sign: i32) -> bool {
}
true
}
+
+#[cfg(test)]
+mod tests {
+ use super::frame_size_allowed;
+
+ #[test]
+ fn level_6_3_maximum_frame_is_allowed() {
+ assert!(frame_size_allowed(16384, 8704));
+ assert!(frame_size_allowed(8704, 16384));
+ }
+
+ #[test]
+ fn frames_beyond_the_limits_are_refused() {
+ assert!(!frame_size_allowed(16384, 8705));
+ assert!(!frame_size_allowed(16385, 1));
+ assert!(!frame_size_allowed(1, 16385));
+ assert!(!frame_size_allowed(65536, 65536));
+ }
+}
diff --git a/crates/av1/tests/fuzz_hostile.rs b/crates/av1/tests/fuzz_hostile.rs
index 482bb844c..47476b941 100644
--- a/crates/av1/tests/fuzz_hostile.rs
+++ b/crates/av1/tests/fuzz_hostile.rs
@@ -95,7 +95,10 @@ fn iters() -> usize {
fn hostile_streams_never_panic() {
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("AV1 fuzz") else { return };
for &(name, args, pix) in SEEDS {
- let Some(seed) = make(&ff, name, args, pix) else { continue };
+ let Some(seed) = make(&ff, name, args, pix) else {
+ filmcraft_testkit::skip("AV1 fuzz", &format!("ffmpeg could not encode the {name} seed (encoder missing?)"));
+ continue;
+ };
if seed.is_empty() {
continue;
}
diff --git a/crates/av1/tests/hostile_dims.rs b/crates/av1/tests/hostile_dims.rs
index 166b69d5e..41a5adb2f 100644
--- a/crates/av1/tests/hostile_dims.rs
+++ b/crates/av1/tests/hostile_dims.rs
@@ -112,6 +112,20 @@ fn huge_frames_are_refused_without_allocating() {
refused(30000, 30000);
}
+/// A conforming level 6.3 maximum-size frame is past the size check (this stream's tiles are junk, so decoding
+/// may fail later, but not with the "frame size" error); one row more is refused.
+#[test]
+fn level_6_3_maximum_frame_passes_the_size_check() {
+ let mut dec = Decoder::new();
+ let r = dec.decode(&stream(16384, 8704)).and_then(|_| dec.flush_result());
+ assert!(
+ !matches!(&r, Err(Error::Invalid(m)) if m.contains("frame size")),
+ "16384x8704 must not be refused as too large, got {:?}",
+ r.as_ref().map(|p| p.len())
+ );
+ refused(16384, 8705);
+}
+
/// A 64x48 4:2:0 key frame whose sequence profile bit was flipped to 2 (4:2:2): blocks such as 4x16 then have no
/// chroma size (BLOCK_INVALID) and used to index the size tables out of bounds.
const PROFILE_FLIPPED: &[u8] = &[
diff --git a/crates/h264/tests/fuzz_hostile.rs b/crates/h264/tests/fuzz_hostile.rs
index b85dcc458..63d1af19c 100644
--- a/crates/h264/tests/fuzz_hostile.rs
+++ b/crates/h264/tests/fuzz_hostile.rs
@@ -85,7 +85,10 @@ fn run_all(d: &[u8], label: &str) {
fn hostile_streams_never_panic() {
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("h264 fuzz") else { return };
for &(name, args, pix) in SEEDS {
- let Some(seed) = make(&ff, name, args, pix) else { continue };
+ let Some(seed) = make(&ff, name, args, pix) else {
+ filmcraft_testkit::skip("h264 fuzz", &format!("ffmpeg could not encode the {name} seed (encoder missing?)"));
+ continue;
+ };
let stride = (seed.len() / 300).max(1);
for n in (0..seed.len()).step_by(stride) {
run_all(&seed[..n], &format!("{name} truncate {n}"));
diff --git a/crates/hevc/tests/fuzz_hostile.rs b/crates/hevc/tests/fuzz_hostile.rs
index 7eac8128b..f44bcebc6 100644
--- a/crates/hevc/tests/fuzz_hostile.rs
+++ b/crates/hevc/tests/fuzz_hostile.rs
@@ -85,7 +85,10 @@ fn run_all(d: &[u8], label: &str) {
fn hostile_streams_never_panic() {
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("hevc fuzz") else { return };
for &(name, args, pix) in SEEDS {
- let Some(seed) = make(&ff, name, args, pix) else { continue };
+ let Some(seed) = make(&ff, name, args, pix) else {
+ filmcraft_testkit::skip("hevc fuzz", &format!("ffmpeg could not encode the {name} seed (encoder missing?)"));
+ continue;
+ };
let stride = (seed.len() / 300).max(1);
for n in (0..seed.len()).step_by(stride) {
run_all(&seed[..n], &format!("{name} truncate {n}"));
diff --git a/crates/mpeg2v/README.md b/crates/mpeg2v/README.md
index 0562ffed3..90d6d758d 100644
--- a/crates/mpeg2v/README.md
+++ b/crates/mpeg2v/README.md
@@ -38,6 +38,9 @@ parameters, so a random-access point without its own sequence header still decod
- Main Profile at Low/Main/High-1440/High Level, 4:2:2 Profile (IMX / D-10, XDCAM HD422),
Simple Profile, MPEG-1 (including D pictures). 4:4:4 is refused (no profile in use for it).
+- Pictures above 2^25 luma samples (8192 x 4096; 8K UHD at 7680 x 4320 fits, and the largest MPEG-2 level,
+ High, is 1920 x 1152) are refused as `Unsupported`: the 14-bit header fields allow 16383 x 16383, which
+ would ask for gigabytes per frame from hostile input.
- Frame pictures with frame and field motion compensation, dual prime, frame and field DCT;
field pictures with field, 16x8 and dual-prime motion compensation, the second field of a
P frame predicted from the first; open and closed GOPs; B pictures without a forward
diff --git a/crates/mpeg2v/tests/fuzz_hostile.rs b/crates/mpeg2v/tests/fuzz_hostile.rs
index 93710a980..500d95977 100644
--- a/crates/mpeg2v/tests/fuzz_hostile.rs
+++ b/crates/mpeg2v/tests/fuzz_hostile.rs
@@ -85,7 +85,10 @@ fn run_all(d: &[u8], label: &str) {
fn hostile_streams_never_panic() {
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("mpeg2v fuzz") else { return };
for spec in &SPECS {
- let Some(path) = common::make(&ff, spec) else { continue };
+ let Some(path) = common::make(&ff, spec) else {
+ filmcraft_testkit::skip("mpeg2v fuzz", &format!("ffmpeg could not encode the {} seed", spec.name));
+ continue;
+ };
fuzz(&std::fs::read(path).unwrap());
}
}
diff --git a/crates/vp8/tests/fuzz_hostile.rs b/crates/vp8/tests/fuzz_hostile.rs
index 52f418e50..8befebf41 100644
--- a/crates/vp8/tests/fuzz_hostile.rs
+++ b/crates/vp8/tests/fuzz_hostile.rs
@@ -97,7 +97,10 @@ fn iters() -> usize {
fn hostile_streams_never_panic() {
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("VP8 fuzz") else { return };
for &(name, args, pix) in SEEDS {
- let Some(seed) = make(&ff, name, args, pix) else { continue };
+ let Some(seed) = make(&ff, name, args, pix) else {
+ filmcraft_testkit::skip("VP8 fuzz", &format!("ffmpeg could not encode the {name} seed (encoder missing?)"));
+ continue;
+ };
if seed.is_empty() {
continue;
}
diff --git a/crates/vp9/tests/fuzz_hostile.rs b/crates/vp9/tests/fuzz_hostile.rs
index 1a383b542..91516d132 100644
--- a/crates/vp9/tests/fuzz_hostile.rs
+++ b/crates/vp9/tests/fuzz_hostile.rs
@@ -100,7 +100,10 @@ fn iters() -> usize {
fn hostile_streams_never_panic() {
let Some(ff) = filmcraft_testkit::ffmpeg_or_skip("VP9 fuzz") else { return };
for &(name, args, pix) in SEEDS {
- let Some(seed) = make(&ff, name, args, pix) else { continue };
+ let Some(seed) = make(&ff, name, args, pix) else {
+ filmcraft_testkit::skip("VP9 fuzz", &format!("ffmpeg could not encode the {name} seed (encoder missing?)"));
+ continue;
+ };
if seed.is_empty() {
continue;
}