mirror of
https://github.com/storytold/filmcraft.git
synced 2026-11-19 08:53:21 +01:00
Merge commit 'd1e64ecfcc5e6f21e30f571e6fe3edbe86fdcafa' into codex/filmcraft-h264-search-policy
# Conflicts: # Cargo.toml # docs/architecture.md # docs/testing.md
This commit is contained in:
commit
a2ff6f860b
34 files changed
+132
-3624
No files matched your search
Generated
-12
@@ -2059,7 +2059,6 @@ dependencies = [
|
||||
"filmcraft-isobmff",
|
||||
"filmcraft-matroska",
|
||||
"filmcraft-media",
|
||||
"filmcraft-prores",
|
||||
"filmcraft-testkit",
|
||||
"filmcraft-time",
|
||||
"image",
|
||||
@@ -2168,7 +2167,6 @@ dependencies = [
|
||||
"filmcraft-isobmff",
|
||||
"filmcraft-media",
|
||||
"filmcraft-project",
|
||||
"filmcraft-prores",
|
||||
"filmcraft-render",
|
||||
"filmcraft-testkit",
|
||||
"filmcraft-text",
|
||||
@@ -2346,16 +2344,6 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filmcraft-prores"
|
||||
version = "0.20.1"
|
||||
dependencies = [
|
||||
"filmcraft-bitstream",
|
||||
"filmcraft-testkit",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filmcraft-render"
|
||||
version = "0.20.1"
|
||||
|
||||
+2
-6
@@ -19,7 +19,6 @@ filmcraft-bitstream = { path = "crates/bitstream" }
|
||||
filmcraft-isobmff = { path = "crates/isobmff" }
|
||||
filmcraft-matroska = { path = "crates/matroska" }
|
||||
filmcraft-cfb = { path = "crates/cfb" }
|
||||
filmcraft-prores = { path = "crates/prores" }
|
||||
filmcraft-dnx = { path = "crates/dnx" }
|
||||
filmcraft-dng = { path = "crates/dng" }
|
||||
filmcraft-audio-dsp = { path = "crates/audio-dsp" }
|
||||
@@ -61,8 +60,8 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l
|
||||
# are on one commit, so the types they share (CodecParams, ByteSource) are the same.
|
||||
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "156b491b91f3693f4fce53bf69bb97791f51198f", default-features = false }
|
||||
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "156b491b91f3693f4fce53bf69bb97791f51198f", default-features = false, features = ["aac-decode", "aac-encode", "rayon", "ac3-decode", "mpa-decode", "mp3-decode", "decode-native", "symphonia", "opus-decode", "flac-encode", "mp3-encode"] }
|
||||
# The video codecs (including H.264 encode, APV encode and decoders), on the same commit as craft-media.
|
||||
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "156b491b91f3693f4fce53bf69bb97791f51198f", features = ["h264-decode", "h264-encode", "hevc-decode", "vp9-decode", "av1-decode", "vp8-decode", "mpeg2", "apv", "vp8-decode", "threads"] }
|
||||
# The video codecs (including H.264 encode, APV encode, ProRes and decoders), on the same commit as craft-media.
|
||||
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "156b491b91f3693f4fce53bf69bb97791f51198f", features = ["h264-decode", "h264-encode", "hevc-decode", "vp9-decode", "av1-decode", "vp8-decode", "mpeg2", "apv", "prores", "threads"] }
|
||||
# Exact time, EDL / ALE and the interchange report; serde for the project file's time fields.
|
||||
craft-timeline = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["serde"] }
|
||||
craft-captions = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
|
||||
@@ -105,9 +104,6 @@ opt-level = 1
|
||||
debug = "line-tables-only"
|
||||
|
||||
# Codec hot loops: debug-build tests (RFC/oracle vectors) are minutes slower below opt-level 3.
|
||||
[profile.dev.package.filmcraft-prores]
|
||||
opt-level = 3
|
||||
|
||||
[profile.dev.package.filmcraft-dnx]
|
||||
opt-level = 3
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@ A layered Cargo workspace:
|
||||
|
||||
| Layer | Crates |
|
||||
|---|---|
|
||||
| Codecs and containers | `h264`, `h264enc`, `hevc`, `vp9`, `prores`, `aac`, `opus`, `isobmff`, `matroska`, `avi`, `bitstream` |
|
||||
| Codecs and containers | `h264`, `hevc`, `vp9`, `prores`, `aac`, `opus`, `isobmff`, `matroska`, `avi`, `bitstream` |
|
||||
| Foundations | `time`, `geom`, `color`, `frame`, `media`, `text`, `project`, `audio-dsp` |
|
||||
| Editing and interchange | `edit`, `codecs`, `captions`, `format` (project files), `interchange` |
|
||||
| Rendering and output | `render`, `gpu`, `export` |
|
||||
|
||||
@@ -20,7 +20,6 @@ filmcraft-isobmff = { workspace = true }
|
||||
filmcraft-matroska = { workspace = true }
|
||||
craft-media = { workspace = true, features = ["avi", "mpegts", "mxf", "ogg"] }
|
||||
filmcraft-bitstream = { workspace = true }
|
||||
filmcraft-prores = { workspace = true }
|
||||
filmcraft-dnx = { workspace = true }
|
||||
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "b577b219e96a1aa8cba5ed2966c6b32ea9cfc7b2" }
|
||||
rayon = { workspace = true }
|
||||
|
||||
@@ -346,12 +346,13 @@ impl Mp4Source {
|
||||
if matches!(entry.codec, CodecConfig::ProRes { .. }) {
|
||||
// a 4444 fourcc doesn't mean the frames carry alpha (alpha_channel_type does): read the first frame header
|
||||
let s0 = &t.samples[0];
|
||||
if let Some(h) = filmcraft_media::reader::read_range(&*bytes.0, s0.offset, s0.size as usize).ok().and_then(|d| filmcraft_prores::probe(&d).ok())
|
||||
if let Some(h) =
|
||||
filmcraft_media::reader::read_range(&*bytes.0, s0.offset, s0.size as usize).ok().and_then(|d| craft_video::prores::probe(&d).ok())
|
||||
{
|
||||
info.has_alpha = h.alpha != filmcraft_prores::AlphaType::None;
|
||||
info.has_alpha = h.alpha != craft_video::prores::AlphaType::None;
|
||||
let sub = match h.chroma {
|
||||
filmcraft_prores::ChromaFormat::Yuv422 => "4:2:2 10-bit",
|
||||
filmcraft_prores::ChromaFormat::Yuv444 => "4:4:4 12-bit",
|
||||
craft_video::prores::ChromaFormat::Yuv422 => "4:2:2 10-bit",
|
||||
craft_video::prores::ChromaFormat::Yuv444 => "4:4:4 12-bit",
|
||||
};
|
||||
info.pixel_format = format!("{} {sub}", if info.has_alpha { "YUVA" } else { "YUV" });
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ use filmcraft_media::FrameRequest;
|
||||
/// replaced by `matrix` — built here, no media files.
|
||||
fn rotated_mov(matrix: [i32; 9]) -> Arc<[u8]> {
|
||||
let (w, h) = (64u32, 32u32);
|
||||
let mut fr = filmcraft_prores::Frame::new(w, h, filmcraft_prores::ChromaFormat::Yuv422, 10, false);
|
||||
let mut fr = craft_video::prores::Frame::new(w, h, craft_video::prores::ChromaFormat::Yuv422, 10, false).expect("valid ProRes test frame");
|
||||
for (i, y) in fr.y.iter_mut().enumerate() {
|
||||
*y = if (i as u32 % w) < w / 2 { 800 } else { 100 };
|
||||
}
|
||||
let data = filmcraft_prores::Encoder::new(filmcraft_prores::Profile::Hq, w, h).encode(&fr).expect("encode");
|
||||
let data = craft_video::prores::Encoder::new(craft_video::prores::Profile::Hq, w, h).encode(&fr).expect("encode");
|
||||
let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).expect("writer");
|
||||
let t = mux.add_track(TrackConfig::new(SampleEntry::prores(FourCc(*b"apch"), w as u16, h as u16), 25)).expect("track");
|
||||
mux.write_sample(t, WriteSample { data: &data, duration: 1, composition_offset: 0, is_sync: true }).expect("sample");
|
||||
@@ -33,8 +33,8 @@ fn mov_with_edits(edits: Vec<filmcraft_isobmff::Edit>) -> Arc<[u8]> {
|
||||
/// An `n`-frame ProRes MOV (64×32) at `fps` (track timescale `fps`, one tick per frame) with movie
|
||||
/// timescale `movie`, whose video track carries `edits`.
|
||||
fn mov_at(fps: u32, movie: u32, n: usize, edits: Vec<filmcraft_isobmff::Edit>) -> Arc<[u8]> {
|
||||
let fr = filmcraft_prores::Frame::new(64, 32, filmcraft_prores::ChromaFormat::Yuv422, 10, false);
|
||||
let data = filmcraft_prores::Encoder::new(filmcraft_prores::Profile::Hq, 64, 32).encode(&fr).expect("encode");
|
||||
let fr = craft_video::prores::Frame::new(64, 32, craft_video::prores::ChromaFormat::Yuv422, 10, false).expect("valid ProRes test frame");
|
||||
let data = craft_video::prores::Encoder::new(craft_video::prores::Profile::Hq, 64, 32).encode(&fr).expect("encode");
|
||||
let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions { movie_timescale: movie, ..WriterOptions::new(Brand::Mov) }).expect("writer");
|
||||
let mut cfg = TrackConfig::new(SampleEntry::prores(FourCc(*b"apch"), 64, 32), fps);
|
||||
cfg.edits = edits;
|
||||
@@ -133,9 +133,9 @@ fn unedited_composition_delay_starts_at_the_first_picture() {
|
||||
let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).expect("writer");
|
||||
let t = mux.add_track(TrackConfig::new(SampleEntry::prores(FourCc(*b"apch"), 64, 32), 25)).expect("track");
|
||||
for k in 0..4u16 {
|
||||
let mut fr = filmcraft_prores::Frame::new(64, 32, filmcraft_prores::ChromaFormat::Yuv422, 10, false);
|
||||
let mut fr = craft_video::prores::Frame::new(64, 32, craft_video::prores::ChromaFormat::Yuv422, 10, false).expect("valid ProRes test frame");
|
||||
fr.y.fill(100 + 200 * k);
|
||||
let data = filmcraft_prores::Encoder::new(filmcraft_prores::Profile::Hq, 64, 32).encode(&fr).expect("encode");
|
||||
let data = craft_video::prores::Encoder::new(craft_video::prores::Profile::Hq, 64, 32).encode(&fr).expect("encode");
|
||||
mux.write_sample(t, WriteSample { data: &data, duration: 1, composition_offset: 2, is_sync: true }).expect("sample");
|
||||
}
|
||||
let src = Mp4Source::open("delay.mov", mux.finish().expect("finish").into_inner().into()).expect("open");
|
||||
@@ -219,11 +219,11 @@ fn display_matrix_rotates_frames_and_reported_size() {
|
||||
/// A one-frame 25 fps ProRes MOV (64×32, left half bright) with clean aperture `clap`.
|
||||
fn clap_mov(clap: filmcraft_isobmff::CleanAperture) -> Arc<[u8]> {
|
||||
let (w, h) = (64u32, 32u32);
|
||||
let mut fr = filmcraft_prores::Frame::new(w, h, filmcraft_prores::ChromaFormat::Yuv422, 10, false);
|
||||
let mut fr = craft_video::prores::Frame::new(w, h, craft_video::prores::ChromaFormat::Yuv422, 10, false).expect("valid ProRes test frame");
|
||||
for (i, y) in fr.y.iter_mut().enumerate() {
|
||||
*y = if (i as u32 % w) < w / 2 { 800 } else { 100 };
|
||||
}
|
||||
let data = filmcraft_prores::Encoder::new(filmcraft_prores::Profile::Hq, w, h).encode(&fr).expect("encode");
|
||||
let data = craft_video::prores::Encoder::new(craft_video::prores::Profile::Hq, w, h).encode(&fr).expect("encode");
|
||||
let mut entry = SampleEntry::prores(FourCc(*b"apch"), w as u16, h as u16);
|
||||
entry.video.as_mut().expect("video").clean_aperture = Some(clap);
|
||||
let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).expect("writer");
|
||||
@@ -415,10 +415,11 @@ fn hevc_pixel_format_prefers_the_sps_over_the_hvcc_fields() {
|
||||
}
|
||||
|
||||
/// A one-frame 32×16 ProRes MOV with the given profile, chroma format and fourcc.
|
||||
fn prores_mov(profile: filmcraft_prores::Profile, fourcc: &[u8; 4], chroma: filmcraft_prores::ChromaFormat, alpha: bool) -> Arc<[u8]> {
|
||||
fn prores_mov(profile: craft_video::prores::Profile, fourcc: &[u8; 4], chroma: craft_video::prores::ChromaFormat, alpha: bool) -> Arc<[u8]> {
|
||||
let (w, h) = (32u32, 16u32);
|
||||
let fr = filmcraft_prores::Frame::new(w, h, chroma, if chroma == filmcraft_prores::ChromaFormat::Yuv444 { 12 } else { 10 }, alpha);
|
||||
let data = filmcraft_prores::Encoder::new(profile, w, h).encode(&fr).expect("encode");
|
||||
let fr = craft_video::prores::Frame::new(w, h, chroma, if chroma == craft_video::prores::ChromaFormat::Yuv444 { 12 } else { 10 }, alpha)
|
||||
.expect("valid ProRes test frame");
|
||||
let data = craft_video::prores::Encoder::new(profile, w, h).encode(&fr).expect("encode");
|
||||
let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).expect("writer");
|
||||
let t = mux.add_track(TrackConfig::new(SampleEntry::prores(FourCc(*fourcc), w as u16, h as u16), 25)).expect("track");
|
||||
mux.write_sample(t, WriteSample { data: &data, duration: 1, composition_offset: 0, is_sync: true }).expect("sample");
|
||||
@@ -428,7 +429,7 @@ fn prores_mov(profile: filmcraft_prores::Profile, fourcc: &[u8; 4], chroma: film
|
||||
/// `ap4h` / `ap4x` don't imply alpha: the frame header's alpha_channel_type does.
|
||||
#[test]
|
||||
fn prores_alpha_comes_from_the_frame_header() {
|
||||
use filmcraft_prores::{ChromaFormat, Profile};
|
||||
use craft_video::prores::{ChromaFormat, Profile};
|
||||
for (profile, fourcc, chroma, alpha, want) in [
|
||||
(Profile::P4444, b"ap4h", ChromaFormat::Yuv444, false, "YUV 4:4:4 12-bit"),
|
||||
(Profile::P4444, b"ap4h", ChromaFormat::Yuv444, true, "YUVA 4:4:4 12-bit"),
|
||||
@@ -444,7 +445,7 @@ fn prores_alpha_comes_from_the_frame_header() {
|
||||
/// [`prores_mov`] with its sample entry retyped `icod`: an Apple Intermediate Codec MOV as far as
|
||||
/// the demuxer can tell (there is no AIC encoder to make a real one).
|
||||
fn aic_mov() -> Arc<[u8]> {
|
||||
let mut b = prores_mov(filmcraft_prores::Profile::Hq, b"apch", filmcraft_prores::ChromaFormat::Yuv422, false).to_vec();
|
||||
let mut b = prores_mov(craft_video::prores::Profile::Hq, b"apch", craft_video::prores::ChromaFormat::Yuv422, false).to_vec();
|
||||
// stsd (ISO/IEC 14496-12 §8.5.2): type, version/flags, entry_count, then the first entry's size and type
|
||||
let k = b.windows(4).position(|x| x == b"stsd").expect("stsd") + 16;
|
||||
assert_eq!(&b[k..k + 4], b"apch");
|
||||
|
||||
@@ -771,10 +771,10 @@ pub struct ProResDecoder;
|
||||
impl VideoDecoder for ProResDecoder {
|
||||
fn decode(&mut self, sample: &[u8], pts: i64) -> Result<Vec<DecodedFrame>> {
|
||||
use std::sync::Arc;
|
||||
let f = filmcraft_prores::decode_frame(sample).map_err(|e| CodecError::Decode(e.to_string()))?;
|
||||
let f = decode_prores_frame(sample).map_err(|e| CodecError::Decode(e.to_string()))?;
|
||||
let chroma = match f.chroma {
|
||||
filmcraft_prores::ChromaFormat::Yuv422 => filmcraft_frame::Chroma::C422,
|
||||
filmcraft_prores::ChromaFormat::Yuv444 => filmcraft_frame::Chroma::C444,
|
||||
craft_video::prores::ChromaFormat::Yuv422 => filmcraft_frame::Chroma::C422,
|
||||
craft_video::prores::ChromaFormat::Yuv444 => filmcraft_frame::Chroma::C444,
|
||||
};
|
||||
let mut color = filmcraft_color::ColorInfo::REC709;
|
||||
if let Some(m) = filmcraft_color::Matrix::from_code(f.color.matrix) {
|
||||
@@ -1434,3 +1434,58 @@ impl VideoDecoder for RawVideoDecoder {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode with FilmCraft's historical 1 GiB ProRes output-plane ceiling.
|
||||
fn decode_prores_frame(sample: &[u8]) -> std::result::Result<craft_video::prores::Frame, craft_video::prores::Error> {
|
||||
let limits = craft_video::prores::DecodeLimits { max_pixels: u64::MAX, max_frame_bytes: 1 << 30 };
|
||||
craft_video::prores::decode_frame_with_limits(sample, &craft_video::prores::DecodeOptions::default(), &limits)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod prores_resource_tests {
|
||||
use super::*;
|
||||
use craft_video::prores::{AlphaType, ChromaFormat, ColorInfo, FrameHeader, Interlace};
|
||||
|
||||
fn header(width: u16, height: u16) -> Vec<u8> {
|
||||
let h = FrameHeader {
|
||||
header_size: 20,
|
||||
bitstream_version: 1,
|
||||
encoder_id: *b"test",
|
||||
width,
|
||||
height,
|
||||
chroma: ChromaFormat::Yuv444,
|
||||
interlace: Interlace::Progressive,
|
||||
aspect_ratio: 0,
|
||||
frame_rate_code: 0,
|
||||
color: ColorInfo::default(),
|
||||
alpha: AlphaType::Bits16,
|
||||
qmat_luma: [4; 64],
|
||||
qmat_chroma: [4; 64],
|
||||
luma_matrix_present: false,
|
||||
chroma_matrix_present: false,
|
||||
}
|
||||
.write();
|
||||
let size = 8 + h.len();
|
||||
let mut data = Vec::with_capacity(size);
|
||||
data.extend_from_slice(&(size as u32).to_be_bytes());
|
||||
data.extend_from_slice(b"icpf");
|
||||
data.extend_from_slice(&h);
|
||||
data
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prores_decoder_preserves_the_one_gibibyte_plane_limit() {
|
||||
// 4:4:4 + 16-bit alpha: four u16 planes. This is 960,000,000 output bytes,
|
||||
// above craft-video's default 300 MiB but below FilmCraft's prior 1 GiB ceiling.
|
||||
let data = header(12_000, 10_000);
|
||||
assert!(matches!(decode_prores_frame(&data), Err(craft_video::prores::Error::Truncated)));
|
||||
assert!(matches!(
|
||||
craft_video::prores::decode_frame(&data),
|
||||
Err(craft_video::prores::Error::Invalid("frame dimensions exceed the decoder memory limit"))
|
||||
));
|
||||
|
||||
// 1,120,000,000 bytes is beyond the old ceiling and must fail before allocation.
|
||||
let too_large = header(14_000, 10_000);
|
||||
assert!(matches!(decode_prores_frame(&too_large), Err(craft_video::prores::Error::Invalid("frame dimensions exceed the decoder memory limit"))));
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ first frame header to label the stream.
|
||||
independently and in parallel, writing straight into their 16-line bands.
|
||||
- VLCs are decoded with two-level lookup tables (11-bit primary) built from the Annex E lists.
|
||||
- Inverse quantisation with the CID's Annex D weights; IDCT: the exact orthonormal 8×8 inverse DCT
|
||||
of equation 8.3, evaluated separably in `f32` (shared design with `filmcraft-prores`).
|
||||
of equation 8.3, evaluated separably in `f32` (shared design with craft-video ProRes).
|
||||
Output: `floor(x·s + 2^(b−1) + ½)` clipped to `[0, 2^b − 1]`, where `s = 4` at 12 bits (the
|
||||
12-bit coefficient domain is ¼ of the orthonormal DCT, as Table 14's 13-bit IDCT input range
|
||||
for 12-bit samples implies) and 1 otherwise.
|
||||
@@ -121,7 +121,7 @@ decoder within ±2 (90–99% identical). Luma PSNR against the source: HQ 58.8 d
|
||||
|
||||
`cargo test --release -p filmcraft-dnx --test it perf:: -- --ignored --nocapture` on an M4 Pro
|
||||
(14 cores) while the machine was **heavily overloaded** by other jobs (load average ≈ 190).
|
||||
Under the same load `filmcraft-prores`' perf test measured 30 fps threaded / 11.8 fps single
|
||||
Under the same load the former FilmCraft-local ProRes codec measured 30 fps threaded / 11.8 fps single
|
||||
for a 1080p HQ frame, against 318 / 53 fps in its README on a less loaded machine, so these are
|
||||
lower bounds by a factor of roughly 4–10:
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ filmcraft-audio-dsp = { workspace = true }
|
||||
filmcraft-geom = { workspace = true }
|
||||
filmcraft-text = { workspace = true }
|
||||
filmcraft-isobmff = { workspace = true }
|
||||
filmcraft-prores = { workspace = true }
|
||||
filmcraft-dnx = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -129,8 +129,8 @@ pub(crate) fn aac_factory(_format: Format, sample_rate: u32, channels: u32, s: &
|
||||
|
||||
/// ProRes 422 encoder (HQ unless the settings pick another flavour): sRGB/709 RGBA8 → 10-bit limited-range BT.709 4:2:2.
|
||||
struct ProResEncoder {
|
||||
enc: filmcraft_prores::Encoder,
|
||||
profile: filmcraft_prores::Profile,
|
||||
enc: craft_video::prores::Encoder,
|
||||
profile: craft_video::prores::Profile,
|
||||
/// Code the frames' alpha channel (4444 profiles with Include Alpha Channel).
|
||||
alpha: bool,
|
||||
w: u32,
|
||||
@@ -150,10 +150,10 @@ impl VideoEncoder for ProResEncoder {
|
||||
}
|
||||
fn encode(&mut self, f: &EncoderFrame) -> Result<Vec<EncodedPacket>> {
|
||||
let chroma = self.profile.chroma();
|
||||
let mut fr = filmcraft_prores::Frame::new(f.width, f.height, chroma, 10, self.alpha);
|
||||
let mut fr = craft_video::prores::Frame::new(f.width, f.height, chroma, 10, self.alpha).map_err(|e| ExportError::Encode(e.to_string()))?;
|
||||
let (w, h) = (f.width as usize, f.height as usize);
|
||||
timed(Stage::Convert, || match (chroma, f.hdr) {
|
||||
(filmcraft_prores::ChromaFormat::Yuv444, hdr) => {
|
||||
(craft_video::prores::ChromaFormat::Yuv444, hdr) => {
|
||||
let (kr, kb) = if hdr.is_some() { self.signal.kr_kb() } else { (0.2126, 0.0722) };
|
||||
to_yuv444_10(f.rgba, hdr, w, h, Matrix::new(kr, kb), Planes444 { y: &mut fr.y, cb: &mut fr.cb, cr: &mut fr.cr, alpha: fr.alpha.as_deref_mut() })
|
||||
}
|
||||
@@ -172,8 +172,8 @@ impl VideoEncoder for ProResEncoder {
|
||||
}
|
||||
|
||||
/// The ProRes profile named by [`ExportSettings::prores_profile`].
|
||||
pub fn prores_profile(name: &str) -> filmcraft_prores::Profile {
|
||||
use filmcraft_prores::Profile;
|
||||
pub fn prores_profile(name: &str) -> craft_video::prores::Profile {
|
||||
use craft_video::prores::Profile;
|
||||
match name.to_ascii_lowercase().as_str() {
|
||||
"proxy" => Profile::Proxy,
|
||||
"lt" => Profile::Lt,
|
||||
@@ -187,13 +187,13 @@ pub fn prores_profile(name: &str) -> filmcraft_prores::Profile {
|
||||
pub(crate) fn prores_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option<Result<Box<dyn VideoEncoder>>> {
|
||||
(format == Format::ProRes).then(|| {
|
||||
let profile = prores_profile(&s.prores_profile);
|
||||
let mut cfg = filmcraft_prores::EncoderConfig::new(profile, w, h);
|
||||
let mut cfg = craft_video::prores::EncoderConfig::new(profile, w, h);
|
||||
if s.signal.is_hdr() {
|
||||
cfg.color = filmcraft_prores::ColorInfo { primaries: s.signal.primaries, transfer: s.signal.transfer, matrix: s.signal.matrix };
|
||||
cfg.color = craft_video::prores::ColorInfo { primaries: s.signal.primaries, transfer: s.signal.transfer, matrix: s.signal.matrix };
|
||||
}
|
||||
let alpha = s.keeps_alpha();
|
||||
cfg.encode_alpha = alpha;
|
||||
let enc = filmcraft_prores::Encoder::with_config(cfg);
|
||||
let enc = craft_video::prores::Encoder::with_config(cfg);
|
||||
Ok(Box::new(ProResEncoder { enc, profile, alpha, w, h, rate, signal: s.signal }) as Box<dyn VideoEncoder>)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ fn io(e: std::io::Error) -> ExportError {
|
||||
fn picture(settings: &ExportSettings, w: u32, h: u32) -> PictureDesc {
|
||||
let coding = match settings.video_format() {
|
||||
Format::ProRes => {
|
||||
use filmcraft_prores::Profile;
|
||||
use craft_video::prores::Profile;
|
||||
PictureCoding::ProRes {
|
||||
profile: match crate::prores_profile(&settings.prores_profile) {
|
||||
Profile::Proxy => 1,
|
||||
|
||||
@@ -523,7 +523,7 @@ impl ExportSettings {
|
||||
pub fn supports_alpha(&self) -> bool {
|
||||
match self.format {
|
||||
Format::PngSequence | Format::TiffSequence | Format::TgaSequence | Format::ExrSequence | Format::Gif => true,
|
||||
Format::ProRes => crate::prores_profile(&self.prores_profile).chroma() == filmcraft_prores::ChromaFormat::Yuv444,
|
||||
Format::ProRes => crate::prores_profile(&self.prores_profile).chroma() == craft_video::prores::ChromaFormat::Yuv444,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -736,7 +736,7 @@ impl ExportSettings {
|
||||
v += &format!(", keyframe every {} frames", r.keyint);
|
||||
}
|
||||
Format::ProRes => {
|
||||
use filmcraft_prores::Profile;
|
||||
use craft_video::prores::Profile;
|
||||
v += match crate::prores_profile(&self.prores_profile) {
|
||||
Profile::Proxy => ", ProRes 422 Proxy",
|
||||
Profile::Lt => ", ProRes 422 LT",
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
[package]
|
||||
name = "filmcraft-prores"
|
||||
description = "Clean-room Apple ProRes (SMPTE RDD 36) decoder and encoder"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
default = ["threads"]
|
||||
threads = ["dep:rayon"]
|
||||
|
||||
[dependencies]
|
||||
filmcraft-bitstream = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
rayon = { workspace = true, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
filmcraft-testkit = { workspace = true }
|
||||
@@ -1,143 +0,0 @@
|
||||
# filmcraft-prores
|
||||
|
||||
Clean-room Apple ProRes decoder and encoder in pure Rust, written from the public [SMPTE RDD 36:2022](https://pub.smpte.org/doc/rdd36/20220909-pub/rdd36-2022.pdf)
|
||||
bitstream description. Layer L0: depends only on `filmcraft-bitstream`, `thiserror` and (optional,
|
||||
feature `threads`, default on) `rayon`. Builds for `wasm32-unknown-unknown`; no `unsafe`.
|
||||
|
||||
## API
|
||||
|
||||
```rust
|
||||
use filmcraft_prores::*;
|
||||
|
||||
// Decode one QuickTime sample (one ProRes frame).
|
||||
let frame: Frame = decode_frame(&sample)?; // 10-bit 4:2:2 / 12-bit 4:4:4
|
||||
let f10 = decode_frame_with(&sample, &DecodeOptions { bit_depth: Some(10), threads: true })?;
|
||||
decode_frame_into(&sample, &DecodeOptions::default(), &mut frame_buf)?; // reuses planes
|
||||
let hdr: FrameHeader = probe(&sample)?; // header only
|
||||
|
||||
// Encode (progressive).
|
||||
let mut enc = Encoder::new(Profile::Hq, 1920, 1080); // or Encoder::with_config(EncoderConfig { .. })
|
||||
let sample: Vec<u8> = enc.encode(&frame)?; // the QuickTime sample for fourcc Profile::fourcc()
|
||||
```
|
||||
|
||||
`Frame { width, height, chroma: ChromaFormat::{Yuv422, Yuv444}, bit_depth, y, cb, cr: Vec<u16>,
|
||||
alpha: Option<Vec<u16>>, interlace: Interlace, color: ColorInfo { primaries, transfer, matrix },
|
||||
aspect_ratio, frame_rate_code }` holds planar samples with no row padding (`chroma_width()` is
|
||||
`ceil(width/2)` for 4:2:2). The colour codes are H.273 code points straight from the frame header.
|
||||
|
||||
`Profile` = `Proxy` (`apco`), `Lt` (`apcs`), `Standard` (`apcn`), `Hq` (`apch`), `P4444` (`ap4h`),
|
||||
`P4444Xq` (`ap4x`). The frame itself doesn't say which profile it is (only the container fourcc
|
||||
does), so the decoder doesn't need to know.
|
||||
|
||||
## Decoder
|
||||
|
||||
- Frame header (dimensions, chroma format, interlace mode, aspect/frame-rate codes, colour
|
||||
description, alpha type, optional luma/chroma quantisation matrices), picture header(s), slice
|
||||
table, slices.
|
||||
- Entropy decoding: RDD 36's adaptive Golomb-Rice/Exp-Golomb codebooks. DC is coded as the first
|
||||
value plus sign-predicted differences. AC is run/level coded, interleaved across all blocks of a
|
||||
slice component.
|
||||
- Progressive and interlaced scan orders. Interlaced frames are two field pictures (top or bottom
|
||||
field first) of `ceil(height/32)` macroblock rows each.
|
||||
- Dequantisation `F = level × qmat[i] × qscale(qidx)` (qscale = qidx up to 128, then
|
||||
`128 + 4·(qidx−128)`).
|
||||
- IDCT: the exact 2-D orthonormal inverse DCT, evaluated separably in `f32`. It uses even/odd
|
||||
symmetry, skips zero rows and fills DC-only blocks directly. Its error is below 2⁻⁸ in the
|
||||
12-bit domain. The IDCT output `f` is the 12-bit-domain sample minus 2048. The output at depth
|
||||
`d` is `floor(f·2^(d−12) + 2^(d−1) + ½)`, clipped to `[4, 2^d − 5]` for d ≥ 10 (the four
|
||||
lowest/highest codes are SDI reserved values; this matches common decoders at 10 and 12 bits)
|
||||
and to `[1, 2^d − 2]` below 10 bits.
|
||||
- 4:2:2 chroma blocks per macroblock are top/bottom. 4:4:4 chroma blocks run TL, BL, TR, BR,
|
||||
while luma runs TL, TR, BL, BR.
|
||||
- Alpha (8- or 16-bit coding): lossless difference/run coding in raster order over each slice's
|
||||
16 lines. The starting value is all-ones. Output is scaled to the output depth: 8-bit coding is
|
||||
bit-replicated, 16-bit coding is truncated.
|
||||
- Default output depth: 10 bits for 4:2:2 and 12 bits for 4:4:4. Any depth from 8 to 16 is
|
||||
available on request.
|
||||
- Slice-parallel: each macroblock row, covering both fields of interlaced frames, is decoded by a
|
||||
rayon task directly into the output planes.
|
||||
- Robustness: every size field is validated before allocation, codewords longer than 56 bits are
|
||||
rejected, and arithmetic is overflow-safe. Mutated streams return `Err`, never panic (see
|
||||
`tests/it/fuzz.rs`).
|
||||
|
||||
## Encoder
|
||||
|
||||
- Progressive ProRes 422 (Proxy/LT/Standard/HQ) and 4444/4444 XQ. For 4444 the frame's alpha
|
||||
plane is coded with 16-bit alpha coding by default (`EncoderConfig::encode_alpha`).
|
||||
- Input: planar `u16` at any depth from 8 to 16 (normally 10). The chroma format must match the
|
||||
profile. Edge macroblocks are padded by replicating the edge.
|
||||
- Flat quantisation matrices (all 4, not transmitted), 8 macroblocks per slice by default
|
||||
(`log2_slice_mbs`), encoder id `fmcr`.
|
||||
- Rate control targets Apple's published nominal rates (Mbit/s at 1920×1080, 29.97 fps: 45, 102,
|
||||
147, 220, 330, 500). These are converted to bits per macroblock (184, 417, 601, 900, 1349,
|
||||
2045), so frame targets scale with frame area (`Encoder::target_frame_bytes`). Alpha comes on
|
||||
top of the target. Every slice is transformed once. Then a frame-level binary search finds the
|
||||
smallest uniform quantiser index whose frame fits the budget, and as many slices as the rest of
|
||||
the budget allows get one finer step. Detailed content lands within 0.1% of the target. Simple
|
||||
content uses qidx 1 and comes in under the target. Every slice is kept within the 16-bit
|
||||
size fields.
|
||||
|
||||
## Accuracy (vs ffmpeg as an external oracle)
|
||||
|
||||
`tests/it/oracle_decode.rs` decodes 29 fixtures made by ffmpeg's `prores_ks` and `prores_aw`
|
||||
encoders. They cover every profile, testsrc2/mandelbrot/SMPTE bars at 1920×1080, 1918×1080,
|
||||
1280×720, 640×360, 352×288, 70×38 and 333×201, interlaced 720×486 TFF/BFF and 1080i, 2-MB slices,
|
||||
and 4444 with 8- and 16-bit alpha. The results are compared with ffmpeg's decode at the native
|
||||
depth (10-bit 4:2:2, 12-bit 4:4:4). **Every fixture is within ±1 LSB.** Mean absolute error is
|
||||
0.0001–0.03 LSB for 422 and up to 0.15 LSB for 12-bit XQ, and 97–99.99% of samples are
|
||||
bit-identical (86% for 12-bit XQ mandelbrot). Alpha is always bit-exact. The remaining
|
||||
differences are the reference decoder's integer-IDCT rounding. This decoder is closer to the
|
||||
exact IDCT and does not reproduce that approximation.
|
||||
|
||||
`tests/it/oracle_encode.rs` encodes 1920×1080 frames with every profile (testsrc2, mandelbrot and
|
||||
noisy content; 4444 with an alpha disc) and writes them into a minimal test-only MOV. ffmpeg then
|
||||
decodes them with `-xerror`, with no errors, and agrees with this decoder within ±1. HQ PSNR is
|
||||
69.9/69.2 dB (Y/C) on mandelbrot at 100% of the target rate. Busy content lands at 99.9–100.4%
|
||||
of the target.
|
||||
|
||||
## Alpha bitstream conformance
|
||||
|
||||
Every alpha difference is followed by a run code, including a final singleton
|
||||
(RDD 36:2022 §5.3.3, Tables 12–14). Omitting the final run produces a truncated
|
||||
alpha payload: FFmpeg may accept it, while macOS native HD decoding fails with AVFoundation `-11821` / underlying `-12137`. This affected
|
||||
both 4444 and 4444 XQ exports, including EffectCraft's alpha output
|
||||
([effectcraft#293](https://github.com/storytold/effectcraft/issues/293)).
|
||||
|
||||
The encoder tests check exact singleton codewords at 8 and 16 bits, run-length
|
||||
boundaries through 2048 and split runs, final-value bit alignments, and complete
|
||||
encoded slices for both profiles and all four slice widths. A separate,
|
||||
bounds-checked parser follows the published syntax and requires every run and
|
||||
zero alignment bit; round-tripping through a permissive decoder alone cannot
|
||||
catch this defect.
|
||||
|
||||
Native validation on Apple M5 / macOS 27.0.1 used 14 synthetic 1920×1080 clips:
|
||||
opaque, transparent, gradient, changing last pixel, moving block, noisy alpha,
|
||||
and RGB-only, for each 4444 profile. Before the fix, the gradient, last-pixel and
|
||||
noise cases failed in AVAssetReader (six clips); afterwards all 14 decoded all
|
||||
six frames. FFmpeg's decoded frame hashes are unchanged by the fix. Small
|
||||
128×32 clips passed even before the fix. Resolve itself and the reporter's
|
||||
M1 Pro / macOS 15.8 environment were not available for this check.
|
||||
|
||||
EffectCraft 0.16.0's complete export pipeline was also tested using an animated
|
||||
synthetic PNG with varying alpha: 4444 / XQ × straight / premultiplied × motion
|
||||
blur off / on (eight six-frame HD movies). All eight failed native decoding with
|
||||
the pinned encoder, and all eight passed with this codec patched in. FFmpeg's
|
||||
decoded frame hashes remained identical for every before/after pair.
|
||||
|
||||
## Performance
|
||||
|
||||
Measured with `cargo test --release -p filmcraft-prores --test it perf:: -- --ignored --nocapture` on
|
||||
an M4 Pro (14 cores) that was heavily loaded by other jobs, so these are lower bounds. A 1080p HQ
|
||||
frame at the full nominal rate (918 KB) decoded at up to 318 fps threaded and 53 fps on one
|
||||
thread. A prores_ks HQ mandelbrot frame (657 KB) decoded at 390 fps threaded. Encoding a 1080p
|
||||
HQ frame takes about 40 ms threaded.
|
||||
|
||||
## Gaps
|
||||
|
||||
- The encoder is progressive-only (the decoder handles interlaced), uses flat quantisation
|
||||
matrices (no perceptual matrices), and has no trellis/RDO.
|
||||
- Output is not bit-exact with ffmpeg's integer IDCT; it stays within ±1 LSB of it (see above).
|
||||
- The 4:2:2 decoder output is fixed at the coded 10-bit precision unless another depth is
|
||||
requested; there is no direct output of packed formats (v210, etc.).
|
||||
- No container handling: frames are QuickTime samples, and demuxing/muxing lives in
|
||||
`filmcraft-isobmff`.
|
||||
@@ -1,280 +0,0 @@
|
||||
//! Fast MSB-first bit reader/writer for slice payloads (the hot path of the codec).
|
||||
//!
|
||||
//! Frame and picture headers use `filmcraft_bitstream`; the entropy-coded slice data uses these
|
||||
//! specialised helpers, which read 64-bit windows and decode a whole codeword per call.
|
||||
|
||||
use crate::tables::Codebook;
|
||||
|
||||
/// Bit reader over one slice component with a 64-bit MSB-aligned cache. Reads past the end
|
||||
/// yield zero bits; callers check [`Reader::overrun`] once a component has been consumed.
|
||||
pub(crate) struct Reader<'a> {
|
||||
data: &'a [u8],
|
||||
/// Next byte of `data` to load into the cache (may run past the end: zeros are loaded).
|
||||
next: usize,
|
||||
/// Cached bits, MSB first; bits below `avail` are zero.
|
||||
cache: u64,
|
||||
avail: u32,
|
||||
}
|
||||
|
||||
impl<'a> Reader<'a> {
|
||||
#[inline]
|
||||
pub fn new(data: &'a [u8]) -> Self {
|
||||
let mut r = Self { data, next: 0, cache: 0, avail: 0 };
|
||||
r.refill();
|
||||
r
|
||||
}
|
||||
|
||||
/// Ensure at least 56 valid bits are cached.
|
||||
#[inline(always)]
|
||||
fn refill(&mut self) {
|
||||
if self.avail >= 56 {
|
||||
return;
|
||||
}
|
||||
if let Some(s) = self.data.get(self.next..self.next + 8) {
|
||||
let w = u64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]);
|
||||
self.cache |= w >> self.avail;
|
||||
self.next += ((63 - self.avail) >> 3) as usize;
|
||||
self.avail |= 56;
|
||||
} else {
|
||||
while self.avail <= 56 {
|
||||
let b = self.data.get(self.next).copied().unwrap_or(0) as u64;
|
||||
self.cache |= b << (56 - self.avail);
|
||||
self.avail += 8;
|
||||
self.next += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn consume(&mut self, n: u32) {
|
||||
// n <= 56 <= avail
|
||||
self.cache = if n >= 64 { 0 } else { self.cache << n };
|
||||
self.avail -= n;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn consumed(&self) -> usize {
|
||||
self.next * 8 - self.avail as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn bits_left(&self) -> isize {
|
||||
(self.data.len() * 8) as isize - self.consumed() as isize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn overrun(&self) -> bool {
|
||||
self.consumed() > self.data.len() * 8
|
||||
}
|
||||
|
||||
/// True when every remaining bit is zero (the component's padding), or nothing is left.
|
||||
#[inline(always)]
|
||||
pub fn only_padding_left(&mut self) -> bool {
|
||||
let left = self.bits_left();
|
||||
if left > 56 {
|
||||
return false;
|
||||
}
|
||||
if left <= 0 {
|
||||
return true;
|
||||
}
|
||||
self.refill();
|
||||
self.cache >> (64 - left as u32) == 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn read(&mut self, n: u32) -> u32 {
|
||||
debug_assert!(n <= 32);
|
||||
if n == 0 {
|
||||
return 0;
|
||||
}
|
||||
self.refill();
|
||||
let v = (self.cache >> (64 - n)) as u32;
|
||||
self.consume(n);
|
||||
v
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_bit(&mut self) -> bool {
|
||||
if self.avail == 0 {
|
||||
self.refill();
|
||||
}
|
||||
let b = self.cache >> 63 == 1;
|
||||
self.consume(1);
|
||||
b
|
||||
}
|
||||
|
||||
/// Decode one codeword; `None` for codewords longer than 56 bits (never produced by valid
|
||||
/// streams).
|
||||
#[inline(always)]
|
||||
pub fn read_cw(&mut self, cb: Codebook) -> Option<u32> {
|
||||
self.refill();
|
||||
let w = self.cache;
|
||||
let q = w.leading_zeros();
|
||||
let rq = cb.rice_q_max as u32;
|
||||
if q <= rq {
|
||||
let rice = cb.rice as u32;
|
||||
let len = q + 1 + rice;
|
||||
let low = if rice == 0 { 0 } else { ((w << (q + 1)) >> (64 - rice)) as u32 };
|
||||
self.consume(len);
|
||||
Some((q << rice) | low)
|
||||
} else {
|
||||
let exp = cb.exp as u32;
|
||||
let len = 2 * q - rq + exp;
|
||||
if len > 56 {
|
||||
return None;
|
||||
}
|
||||
let code = w >> (64 - len);
|
||||
self.consume(len);
|
||||
let v = code - (1u64 << exp) + (((rq + 1) as u64) << cb.rice);
|
||||
u32::try_from(v).ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Number of bits of the codeword for `val` in codebook `cb`.
|
||||
#[inline]
|
||||
pub(crate) fn cw_len(cb: Codebook, val: u32) -> u32 {
|
||||
let rq = cb.rice_q_max as u32;
|
||||
let rice = cb.rice as u32;
|
||||
let rice_span = (rq + 1) << rice;
|
||||
if val < rice_span {
|
||||
(val >> rice) + 1 + rice
|
||||
} else {
|
||||
let exp = cb.exp as u32;
|
||||
let v = (val - rice_span) as u64 + (1u64 << exp);
|
||||
let nbits = 64 - v.leading_zeros(); // n + exp + 1
|
||||
let n = nbits - exp - 1;
|
||||
(rq + 1 + n) + nbits
|
||||
}
|
||||
}
|
||||
|
||||
/// A bit sink: either a real writer or a bit counter (for rate control trials).
|
||||
pub(crate) trait Sink {
|
||||
fn put(&mut self, val: u32, bits: u32);
|
||||
fn put_cw(&mut self, cb: Codebook, val: u32);
|
||||
}
|
||||
|
||||
/// Counts bits without storing them.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Counter(pub u64);
|
||||
|
||||
impl Sink for Counter {
|
||||
#[inline]
|
||||
fn put(&mut self, _val: u32, bits: u32) {
|
||||
self.0 += bits as u64;
|
||||
}
|
||||
#[inline]
|
||||
fn put_cw(&mut self, cb: Codebook, val: u32) {
|
||||
self.0 += cw_len(cb, val) as u64;
|
||||
}
|
||||
}
|
||||
|
||||
/// MSB-first bit writer.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Writer {
|
||||
pub buf: Vec<u8>,
|
||||
acc: u64,
|
||||
n: u32,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
/// Pad with zero bits to a byte boundary and return the bytes.
|
||||
pub fn finish(mut self) -> Vec<u8> {
|
||||
if self.n > 0 {
|
||||
let pad = 8 - self.n;
|
||||
self.put(0, pad);
|
||||
}
|
||||
self.buf
|
||||
}
|
||||
}
|
||||
|
||||
impl Sink for Writer {
|
||||
#[inline]
|
||||
fn put(&mut self, val: u32, bits: u32) {
|
||||
debug_assert!(bits <= 32);
|
||||
if bits == 0 {
|
||||
return;
|
||||
}
|
||||
let v = (val as u64) & ((1u64 << bits) - 1);
|
||||
self.acc = (self.acc << bits) | v;
|
||||
self.n += bits;
|
||||
while self.n >= 8 {
|
||||
self.n -= 8;
|
||||
self.buf.push((self.acc >> self.n) as u8);
|
||||
}
|
||||
self.acc &= (1u64 << self.n) - 1;
|
||||
}
|
||||
|
||||
fn put_cw(&mut self, cb: Codebook, val: u32) {
|
||||
let rq = cb.rice_q_max as u32;
|
||||
let rice = cb.rice as u32;
|
||||
let rice_span = (rq + 1) << rice;
|
||||
if val < rice_span {
|
||||
let q = val >> rice;
|
||||
// q zeros, a one, then `rice` low bits
|
||||
self.put(0, q);
|
||||
self.put(1, 1);
|
||||
self.put(val & ((1 << rice) - 1), rice);
|
||||
} else {
|
||||
let exp = cb.exp as u32;
|
||||
let v = (val - rice_span) as u64 + (1u64 << exp);
|
||||
let nbits = 64 - v.leading_zeros();
|
||||
let n = nbits - exp - 1;
|
||||
let zeros = rq + 1 + n;
|
||||
self.put(0, zeros.min(32));
|
||||
if zeros > 32 {
|
||||
self.put(0, zeros - 32);
|
||||
}
|
||||
if nbits > 32 {
|
||||
self.put((v >> 32) as u32, nbits - 32);
|
||||
self.put(v as u32, 32);
|
||||
} else {
|
||||
self.put(v as u32, nbits);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tables::*;
|
||||
|
||||
#[test]
|
||||
fn codeword_roundtrip_all_codebooks() {
|
||||
let mut books = vec![FIRST_DC_CB];
|
||||
books.extend_from_slice(&DC_CB);
|
||||
books.extend_from_slice(&RUN_CB);
|
||||
books.extend_from_slice(&LEVEL_CB);
|
||||
let vals: Vec<u32> = (0..300).chain([1000, 4095, 65535, 1 << 20]).collect();
|
||||
for cb in books {
|
||||
let mut w = Writer::new();
|
||||
let mut total = 0;
|
||||
for &v in &vals {
|
||||
w.put_cw(cb, v);
|
||||
total += cw_len(cb, v);
|
||||
}
|
||||
let bits = w.buf.len() as u32 * 8 + w.n;
|
||||
assert_eq!(bits, total);
|
||||
let data = w.finish();
|
||||
let mut r = Reader::new(&data);
|
||||
for &v in &vals {
|
||||
assert_eq!(r.read_cw(cb), Some(v), "{cb:?}");
|
||||
}
|
||||
assert!(!r.overrun());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exp_golomb5_equivalence() {
|
||||
// FIRST_DC_CB is Exp-Golomb order 5: value 32 is `01` + 6 bits (000000)
|
||||
assert_eq!(cw_len(FIRST_DC_CB, 0), 6);
|
||||
assert_eq!(cw_len(FIRST_DC_CB, 31), 6);
|
||||
assert_eq!(cw_len(FIRST_DC_CB, 32), 8);
|
||||
assert_eq!(cw_len(FIRST_DC_CB, 95), 8);
|
||||
assert_eq!(cw_len(FIRST_DC_CB, 96), 10);
|
||||
}
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
//! 8×8 orthonormal DCT-II / DCT-III (IDCT) in single precision.
|
||||
//!
|
||||
//! The reconstruction IDCT is defined as the exact real-valued 2-D inverse DCT
|
||||
//! `f(x,y) = Σ_u Σ_v C(u)C(v)/4 · F(v,u) · cos((2x+1)uπ/16) · cos((2y+1)vπ/16)`
|
||||
//! evaluated separably (rows then columns) in `f32`. For ProRes coefficient ranges the `f32`
|
||||
//! evaluation error is below 2⁻⁸ in the 12-bit sample domain, so the rounded output equals the
|
||||
//! rounded exact IDCT except when the exact value lies within that distance of a rounding tie.
|
||||
|
||||
use crate::tables::BASIS;
|
||||
|
||||
/// In-place inverse DCT. `block` holds dequantised coefficients in raster order
|
||||
/// (`block[v*8 + u]`, `v` = vertical frequency) and receives samples (`block[y*8 + x]`).
|
||||
///
|
||||
/// Each 1-D pass uses the even/odd symmetry of the basis (`B[u][7-x] = ±B[u][x]` for even/odd
|
||||
/// `u`) and skips all-zero rows; DC-only blocks are filled directly.
|
||||
#[inline]
|
||||
pub(crate) fn idct8x8(block: &mut [f32; 64]) {
|
||||
let mut t = [[0f32; 8]; 8];
|
||||
let mut nz = [false; 8];
|
||||
let mut dc_only = true;
|
||||
// Horizontal pass: t[r][x] = Σ_u F[r][u] · B[u][x]
|
||||
for (r, c) in block.as_chunks::<8>().0.iter().enumerate() {
|
||||
if c[1..].iter().all(|&v| v == 0.0) {
|
||||
if c[0] != 0.0 {
|
||||
t[r] = [c[0] * BASIS[0][0]; 8];
|
||||
nz[r] = true;
|
||||
dc_only &= r == 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
dc_only = false;
|
||||
nz[r] = true;
|
||||
let mut out = [0f32; 8];
|
||||
for x in 0..4 {
|
||||
let e = c[0] * BASIS[0][x] + c[2] * BASIS[2][x] + c[4] * BASIS[4][x] + c[6] * BASIS[6][x];
|
||||
let o = c[1] * BASIS[1][x] + c[3] * BASIS[3][x] + c[5] * BASIS[5][x] + c[7] * BASIS[7][x];
|
||||
out[x] = e + o;
|
||||
out[7 - x] = e - o;
|
||||
}
|
||||
t[r] = out;
|
||||
}
|
||||
if dc_only {
|
||||
*block = [t[0][0] * BASIS[0][0]; 64];
|
||||
return;
|
||||
}
|
||||
// Vertical pass: f[y][x] = Σ_v t[v][x] · B[v][y]
|
||||
for y in 0..4 {
|
||||
let mut e = [0f32; 8];
|
||||
let mut o = [0f32; 8];
|
||||
for v in [0, 2, 4, 6] {
|
||||
if nz[v] {
|
||||
let b = BASIS[v][y];
|
||||
for x in 0..8 {
|
||||
e[x] += t[v][x] * b;
|
||||
}
|
||||
}
|
||||
}
|
||||
for v in [1, 3, 5, 7] {
|
||||
if nz[v] {
|
||||
let b = BASIS[v][y];
|
||||
for x in 0..8 {
|
||||
o[x] += t[v][x] * b;
|
||||
}
|
||||
}
|
||||
}
|
||||
for x in 0..8 {
|
||||
block[y * 8 + x] = e[x] + o[x];
|
||||
block[(7 - y) * 8 + x] = e[x] - o[x];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In-place forward DCT (samples in, coefficients out; same layout as [`idct8x8`]).
|
||||
pub(crate) fn fdct8x8(block: &mut [f32; 64]) {
|
||||
let mut t = [[0f32; 8]; 8];
|
||||
// Horizontal: t[y][u] = Σ_x f[y][x] · B[u][x]
|
||||
for y in 0..8 {
|
||||
for u in 0..8 {
|
||||
let mut s = 0f32;
|
||||
for x in 0..8 {
|
||||
s += block[y * 8 + x] * BASIS[u][x];
|
||||
}
|
||||
t[y][u] = s;
|
||||
}
|
||||
}
|
||||
// Vertical: F[v][u] = Σ_y t[y][u] · B[v][y]
|
||||
for v in 0..8 {
|
||||
let mut acc = [0f32; 8];
|
||||
for y in 0..8 {
|
||||
let b = BASIS[v][y];
|
||||
for u in 0..8 {
|
||||
acc[u] += t[y][u] * b;
|
||||
}
|
||||
}
|
||||
block[v * 8..v * 8 + 8].copy_from_slice(&acc);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn reference_idct(c: &[f32; 64]) -> [f64; 64] {
|
||||
let mut out = [0f64; 64];
|
||||
for y in 0..8 {
|
||||
for x in 0..8 {
|
||||
let mut s = 0f64;
|
||||
for v in 0..8 {
|
||||
for u in 0..8 {
|
||||
let cu = if u == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };
|
||||
let cv = if v == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };
|
||||
s += cu * cv / 4.0
|
||||
* c[v * 8 + u] as f64
|
||||
* (((2 * x + 1) * u) as f64 * std::f64::consts::PI / 16.0).cos()
|
||||
* (((2 * y + 1) * v) as f64 * std::f64::consts::PI / 16.0).cos();
|
||||
}
|
||||
}
|
||||
out[y * 8 + x] = s;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idct_matches_double_reference() {
|
||||
let mut seed = 12345u32;
|
||||
let mut rnd = || {
|
||||
seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
|
||||
seed >> 8
|
||||
};
|
||||
for _ in 0..200 {
|
||||
let mut c = [0f32; 64];
|
||||
for v in c.iter_mut() {
|
||||
if rnd() % 3 == 0 {
|
||||
*v = ((rnd() % 8192) as i32 - 4096) as f32;
|
||||
}
|
||||
}
|
||||
let r = reference_idct(&c);
|
||||
let mut b = c;
|
||||
idct8x8(&mut b);
|
||||
for i in 0..64 {
|
||||
assert!((b[i] as f64 - r[i]).abs() < 4e-3, "{} vs {}", b[i], r[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fdct_inverts_idct() {
|
||||
let mut b = [0f32; 64];
|
||||
for (i, v) in b.iter_mut().enumerate() {
|
||||
*v = ((i * 37) % 200) as f32 - 100.0;
|
||||
}
|
||||
let orig = b;
|
||||
fdct8x8(&mut b);
|
||||
idct8x8(&mut b);
|
||||
for i in 0..64 {
|
||||
assert!((b[i] - orig[i]).abs() < 1e-3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,520 +0,0 @@
|
||||
//! ProRes frame decoding.
|
||||
|
||||
use crate::bits::Reader;
|
||||
use crate::dct::idct8x8;
|
||||
use crate::header::{FrameHeader, PictureHeader, row_slice_widths};
|
||||
use crate::tables::{DC_CB, DC_CTX_INIT, FIRST_DC_CB, LEVEL_CB, LEVEL_CTX_INIT, RUN_CB, RUN_CTX_INIT, SCAN_INTERLACED, SCAN_PROGRESSIVE, qscale};
|
||||
use crate::{AlphaType, ChromaFormat, Error, Frame, Interlace, Result};
|
||||
|
||||
/// Maximum aggregate decoded plane storage for one ProRes frame (256 MiB).
|
||||
// 1 GiB: 8K ProRes 4444 with alpha is 283 MB of 16-bit planes
|
||||
const MAX_FRAME_BYTES: usize = 1 << 30;
|
||||
|
||||
/// Decoder options.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct DecodeOptions {
|
||||
/// Output bit depth (8..=16). `None`: 10 for 4:2:2, 12 for 4:4:4 (the precision ProRes
|
||||
/// codes each format at).
|
||||
pub bit_depth: Option<u8>,
|
||||
/// Decode slices in parallel (requires the `threads` feature).
|
||||
pub threads: bool,
|
||||
}
|
||||
|
||||
impl Default for DecodeOptions {
|
||||
fn default() -> Self {
|
||||
DecodeOptions { bit_depth: None, threads: true }
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse only the frame header (cheap; for probing geometry and format).
|
||||
pub fn probe(data: &[u8]) -> Result<FrameHeader> {
|
||||
FrameHeader::parse(data).map(|(h, _, _)| h)
|
||||
}
|
||||
|
||||
/// Decode one ProRes frame (the contents of one QuickTime sample) with default options.
|
||||
pub fn decode_frame(data: &[u8]) -> Result<Frame> {
|
||||
decode_frame_with(data, &DecodeOptions::default())
|
||||
}
|
||||
|
||||
/// Maps IDCT output (12-bit-domain signed samples) to the output bit depth.
|
||||
#[derive(Clone, Copy)]
|
||||
struct OutConv {
|
||||
scale: f32,
|
||||
bias: f32,
|
||||
min: i32,
|
||||
max: i32,
|
||||
}
|
||||
|
||||
impl OutConv {
|
||||
fn new(depth: u8) -> OutConv {
|
||||
// The IDCT produces `f`, where the 12-bit sample is `f + 2048`.
|
||||
let scale = (2f32).powi(depth as i32 - 12);
|
||||
// Samples are clipped away from the extremes: the four lowest and highest codes are
|
||||
// excluded at 10 bits and above (0..=3 and 1020..=1023 are the SDI timing-reference codes
|
||||
// at 10 bits; the same margin is kept at 12 bits, matching common decoders), and the
|
||||
// single lowest/highest code at 8 and 9 bits.
|
||||
let reserved = if depth >= 10 { 4 } else { 1 };
|
||||
OutConv { scale, bias: (1u32 << (depth - 1)) as f32 + 0.5, min: reserved, max: (1i32 << depth) - 1 - reserved }
|
||||
}
|
||||
#[inline(always)]
|
||||
fn conv(&self, v: f32) -> u16 {
|
||||
// Truncation equals floor here: negative values clamp to `min` (> 0) either way.
|
||||
((v * self.scale + self.bias) as i32).clamp(self.min, self.max) as u16
|
||||
}
|
||||
}
|
||||
|
||||
struct Ctx {
|
||||
width: usize,
|
||||
cwidth: usize,
|
||||
chroma444: bool,
|
||||
qmat_luma: [u8; 64],
|
||||
qmat_chroma: [u8; 64],
|
||||
scan: &'static [u8; 64],
|
||||
alpha: AlphaType,
|
||||
conv: OutConv,
|
||||
depth: u8,
|
||||
}
|
||||
|
||||
/// Output rows belonging to one macroblock row of every coded picture.
|
||||
struct Band<'a> {
|
||||
y: &'a mut [u16],
|
||||
cb: &'a mut [u16],
|
||||
cr: &'a mut [u16],
|
||||
a: Option<&'a mut [u16]>,
|
||||
rows: usize,
|
||||
}
|
||||
|
||||
struct Picture {
|
||||
/// Line parity within the frame (0 = even lines) for field pictures.
|
||||
parity: usize,
|
||||
/// Byte ranges of each slice, row-major.
|
||||
slices: Vec<(usize, usize)>,
|
||||
}
|
||||
|
||||
struct Scratch {
|
||||
coeffs: Vec<[f32; 64]>,
|
||||
alpha: Vec<u16>,
|
||||
}
|
||||
|
||||
impl Scratch {
|
||||
fn new() -> Scratch {
|
||||
Scratch { coeffs: vec![[0f32; 64]; 32], alpha: vec![0; 16 * 128] }
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode one ProRes frame.
|
||||
pub fn decode_frame_with(data: &[u8], opts: &DecodeOptions) -> Result<Frame> {
|
||||
let mut f = Frame::new(0, 0, ChromaFormat::Yuv422, 10, false);
|
||||
decode_frame_into(data, opts, &mut f)?;
|
||||
Ok(f)
|
||||
}
|
||||
|
||||
/// Decode one ProRes frame into `out`, reusing its plane allocations (the fastest way to decode
|
||||
/// a sequence). On error the contents of `out` are unspecified.
|
||||
pub fn decode_frame_into(data: &[u8], opts: &DecodeOptions, out: &mut Frame) -> Result<()> {
|
||||
let (hdr, mut off, frame_size) = FrameHeader::parse(data)?;
|
||||
let data = &data[..frame_size];
|
||||
let depth = opts.bit_depth.unwrap_or(match hdr.chroma {
|
||||
ChromaFormat::Yuv422 => 10,
|
||||
ChromaFormat::Yuv444 => 12,
|
||||
});
|
||||
if !(8..=16).contains(&depth) {
|
||||
return Err(Error::Unsupported("output bit depth"));
|
||||
}
|
||||
let width = hdr.width as usize;
|
||||
let height = hdr.height as usize;
|
||||
let cwidth = hdr.chroma.chroma_width(hdr.width as u32) as usize;
|
||||
let luma = width.checked_mul(height).ok_or(Error::Invalid("frame dimensions overflow"))?;
|
||||
let chroma = cwidth.checked_mul(height).ok_or(Error::Invalid("chroma dimensions overflow"))?;
|
||||
let samples = luma
|
||||
.checked_add(chroma.checked_mul(2).ok_or(Error::Invalid("chroma plane count overflows"))?)
|
||||
.and_then(|n| if hdr.alpha == AlphaType::None { Some(n) } else { n.checked_add(luma) })
|
||||
.ok_or(Error::Invalid("frame plane count overflows"))?;
|
||||
if samples.checked_mul(std::mem::size_of::<u16>()).is_none_or(|n| n > MAX_FRAME_BYTES) {
|
||||
return Err(Error::Invalid("frame dimensions exceed the decoder memory limit"));
|
||||
}
|
||||
let interlaced = hdr.interlace != Interlace::Progressive;
|
||||
let mb_width = width.div_ceil(16);
|
||||
let mb_height = if interlaced { height.div_ceil(32) } else { height.div_ceil(16) };
|
||||
|
||||
let npics = if interlaced { 2 } else { 1 };
|
||||
let mut pics = Vec::with_capacity(npics);
|
||||
for p in 0..npics {
|
||||
let ph = PictureHeader::parse(data.get(off..).ok_or(Error::Truncated)?)?;
|
||||
let pic_end = off.checked_add(ph.picture_size).ok_or(Error::Truncated)?;
|
||||
if pic_end > data.len() || ph.picture_size < ph.header_size {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
let widths = row_slice_widths(mb_width, ph.log2_slice_mbs);
|
||||
let nslices = widths.len() * mb_height;
|
||||
if ph.num_slices != 0 && ph.num_slices != nslices {
|
||||
return Err(Error::Invalid("slice count does not match frame geometry"));
|
||||
}
|
||||
let table = off + ph.header_size;
|
||||
let mut pos = table + 2 * nslices;
|
||||
if pos > pic_end {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
let mut slices = Vec::with_capacity(nslices);
|
||||
for i in 0..nslices {
|
||||
let sz = u16::from_be_bytes([data[table + 2 * i], data[table + 2 * i + 1]]) as usize;
|
||||
// Every slice carries at least its 6-byte header.
|
||||
if sz < 6 || pos + sz > pic_end {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
slices.push((pos, sz));
|
||||
pos += sz;
|
||||
}
|
||||
let parity = match (hdr.interlace, p) {
|
||||
(Interlace::BottomFieldFirst, 0) | (Interlace::TopFieldFirst, 1) => 1,
|
||||
_ => 0,
|
||||
};
|
||||
pics.push((Picture { parity, slices }, widths));
|
||||
off = pic_end;
|
||||
}
|
||||
|
||||
let ctx = Ctx {
|
||||
width,
|
||||
cwidth,
|
||||
chroma444: hdr.chroma == ChromaFormat::Yuv444,
|
||||
qmat_luma: hdr.qmat_luma,
|
||||
qmat_chroma: hdr.qmat_chroma,
|
||||
scan: if interlaced { &SCAN_INTERLACED } else { &SCAN_PROGRESSIVE },
|
||||
alpha: hdr.alpha,
|
||||
conv: OutConv::new(depth),
|
||||
depth,
|
||||
};
|
||||
|
||||
let take = |v: &mut Vec<u16>, n: usize| {
|
||||
let mut v = std::mem::take(v);
|
||||
v.resize(n, 0);
|
||||
v
|
||||
};
|
||||
let mut y = take(&mut out.y, width * height);
|
||||
let mut cb = take(&mut out.cb, cwidth * height);
|
||||
let mut cr = take(&mut out.cr, cwidth * height);
|
||||
let mut alpha = match (hdr.alpha, out.alpha.take()) {
|
||||
(AlphaType::None, _) => None,
|
||||
(_, a) => Some(take(&mut a.unwrap_or_default(), width * height)),
|
||||
};
|
||||
|
||||
let band_h = if interlaced { 32 } else { 16 };
|
||||
let mut bands: Vec<Band> = Vec::with_capacity(mb_height);
|
||||
{
|
||||
let mut yc = y.chunks_mut(band_h * width);
|
||||
let mut cbc = cb.chunks_mut(band_h * cwidth);
|
||||
let mut crc = cr.chunks_mut(band_h * cwidth);
|
||||
let mut ac = alpha.as_mut().map(|a| a.chunks_mut(band_h * width));
|
||||
while let (Some(y), Some(cb), Some(cr)) = (yc.next(), cbc.next(), crc.next()) {
|
||||
let rows = y.len() / width;
|
||||
let a = ac.as_mut().and_then(|it| it.next());
|
||||
bands.push(Band { y, cb, cr, a, rows });
|
||||
}
|
||||
}
|
||||
|
||||
let step = if interlaced { 2 } else { 1 };
|
||||
let run_band = |(row, band): (usize, &mut Band), scratch: &mut Scratch| -> Result<()> {
|
||||
for (pi, (pic, widths)) in pics.iter().enumerate() {
|
||||
let mut mb_x = 0;
|
||||
for (k, &w) in widths.iter().enumerate() {
|
||||
let si = row * widths.len() + k;
|
||||
let (start, len) = pic.slices[si];
|
||||
decode_slice(&ctx, &data[start..start + len], mb_x, w, band, Lines { step, parity: pic.parity }, scratch)
|
||||
.map_err(|_| Error::Slice { picture: pi, slice: si })?;
|
||||
mb_x += w;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
|
||||
#[cfg(feature = "threads")]
|
||||
if opts.threads {
|
||||
use rayon::prelude::*;
|
||||
bands.par_iter_mut().enumerate().try_for_each_init(Scratch::new, |s, b| run_band(b, s))?;
|
||||
} else {
|
||||
let mut s = Scratch::new();
|
||||
bands.iter_mut().enumerate().try_for_each(|b| run_band(b, &mut s))?;
|
||||
}
|
||||
#[cfg(not(feature = "threads"))]
|
||||
{
|
||||
let mut s = Scratch::new();
|
||||
bands.iter_mut().enumerate().try_for_each(|b| run_band(b, &mut s))?;
|
||||
}
|
||||
drop(bands);
|
||||
|
||||
*out = Frame {
|
||||
width: hdr.width as u32,
|
||||
height: hdr.height as u32,
|
||||
chroma: hdr.chroma,
|
||||
bit_depth: depth,
|
||||
y,
|
||||
cb,
|
||||
cr,
|
||||
alpha,
|
||||
interlace: hdr.interlace,
|
||||
color: hdr.color,
|
||||
aspect_ratio: hdr.aspect_ratio,
|
||||
frame_rate_code: hdr.frame_rate_code,
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn be16(d: &[u8], i: usize) -> usize {
|
||||
u16::from_be_bytes([d[i], d[i + 1]]) as usize
|
||||
}
|
||||
|
||||
/// Dequantisation factors (matrix × scale) for a slice.
|
||||
fn qfactors(qmat: &[u8; 64], qs: u32) -> [f32; 64] {
|
||||
let mut q = [0f32; 64];
|
||||
for i in 0..64 {
|
||||
q[i] = (qmat[i] as u32 * qs) as f32;
|
||||
}
|
||||
q
|
||||
}
|
||||
|
||||
/// Line stepping of the picture inside its frame: `step` is 2 for field pictures, `parity` the first line.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Lines {
|
||||
step: usize,
|
||||
parity: usize,
|
||||
}
|
||||
|
||||
fn decode_slice(ctx: &Ctx, d: &[u8], mb_x: usize, mbs: usize, band: &mut Band, il: Lines, s: &mut Scratch) -> std::result::Result<(), ()> {
|
||||
let Lines { step, parity } = il;
|
||||
if d.len() < 6 {
|
||||
return Err(());
|
||||
}
|
||||
let hsize = (d[0] >> 3) as usize;
|
||||
let has_alpha = ctx.alpha != AlphaType::None;
|
||||
if hsize < if has_alpha { 8 } else { 6 } || hsize > d.len() {
|
||||
return Err(());
|
||||
}
|
||||
let qs = qscale(d[1]);
|
||||
let ysize = be16(d, 2);
|
||||
let cbsize = be16(d, 4);
|
||||
let body = d.len() - hsize;
|
||||
let crsize = if has_alpha { be16(d, 6) } else { body.checked_sub(ysize + cbsize).ok_or(())? };
|
||||
if ysize + cbsize + crsize > body {
|
||||
return Err(());
|
||||
}
|
||||
let mut p = hsize;
|
||||
let ydata = &d[p..p + ysize];
|
||||
p += ysize;
|
||||
let cbdata = &d[p..p + cbsize];
|
||||
p += cbsize;
|
||||
let crdata = &d[p..p + crsize];
|
||||
p += crsize;
|
||||
let adata = &d[p..];
|
||||
|
||||
let log2_mbs = mbs.trailing_zeros();
|
||||
let x0 = mb_x * 16;
|
||||
|
||||
// Luma
|
||||
let ql = qfactors(&ctx.qmat_luma, qs);
|
||||
let nb = 4 * mbs;
|
||||
decode_component(ydata, log2_mbs + 2, ctx.scan, &ql, &mut s.coeffs[..nb])?;
|
||||
for (b, blk) in s.coeffs[..nb].iter_mut().enumerate() {
|
||||
idct8x8(blk);
|
||||
let (mb, i) = (b >> 2, b & 3);
|
||||
put_block(blk, band.y, ctx.width, (x0 + mb * 16 + (i & 1) * 8, (i >> 1) * 8), band.rows, il, ctx.conv);
|
||||
}
|
||||
|
||||
// Chroma
|
||||
let qc = qfactors(&ctx.qmat_chroma, qs);
|
||||
let (cnb, clog2) = if ctx.chroma444 { (4 * mbs, log2_mbs + 2) } else { (2 * mbs, log2_mbs + 1) };
|
||||
for (data, plane) in [(cbdata, &mut *band.cb), (crdata, &mut *band.cr)] {
|
||||
decode_component(data, clog2, ctx.scan, &qc, &mut s.coeffs[..cnb])?;
|
||||
for (b, blk) in s.coeffs[..cnb].iter_mut().enumerate() {
|
||||
idct8x8(blk);
|
||||
let (bx, by) = if ctx.chroma444 {
|
||||
// 4:4:4 chroma blocks run down each 8-wide column pair: TL, BL, TR, BR.
|
||||
let (mb, i) = (b >> 2, b & 3);
|
||||
(x0 + mb * 16 + (i >> 1) * 8, (i & 1) * 8)
|
||||
} else {
|
||||
let (mb, i) = (b >> 1, b & 1);
|
||||
(mb_x * 8 + mb * 8, i * 8)
|
||||
};
|
||||
put_block(blk, plane, ctx.cwidth, (bx, by), band.rows, il, ctx.conv);
|
||||
}
|
||||
}
|
||||
|
||||
// Alpha
|
||||
if let Some(a) = band.a.as_deref_mut() {
|
||||
let w = mbs * 16;
|
||||
let n = w * 16;
|
||||
let buf = &mut s.alpha[..n];
|
||||
decode_alpha(adata, ctx.alpha, buf)?;
|
||||
let depth = ctx.depth as u32;
|
||||
let conv = |v: u16| -> u16 {
|
||||
let v = v as u32;
|
||||
(match ctx.alpha {
|
||||
AlphaType::Bits8 => {
|
||||
if depth >= 8 {
|
||||
let r = (v << (depth - 8)) | (v >> (16 - depth).min(8));
|
||||
r & ((1 << depth) - 1)
|
||||
} else {
|
||||
v
|
||||
}
|
||||
}
|
||||
_ => v >> (16 - depth),
|
||||
}) as u16
|
||||
};
|
||||
for py in 0..16 {
|
||||
let line = py * step + parity;
|
||||
if line >= band.rows {
|
||||
break;
|
||||
}
|
||||
let row = &mut a[line * ctx.width..(line + 1) * ctx.width];
|
||||
let xe = (x0 + w).min(ctx.width);
|
||||
if x0 >= xe {
|
||||
continue;
|
||||
}
|
||||
for (o, &v) in row[x0..xe].iter_mut().zip(&buf[py * w..py * w + (xe - x0)]) {
|
||||
*o = conv(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn to_signed(code: u32) -> i64 {
|
||||
let c = code as i64;
|
||||
(c >> 1) ^ -(c & 1)
|
||||
}
|
||||
|
||||
/// Entropy-decode and dequantise one slice component into `blocks` (raster-order coefficients).
|
||||
fn decode_component(data: &[u8], log2_blocks: u32, scan: &[u8; 64], q: &[f32; 64], blocks: &mut [[f32; 64]]) -> std::result::Result<(), ()> {
|
||||
for b in blocks.iter_mut() {
|
||||
*b = [0f32; 64];
|
||||
}
|
||||
let n = blocks.len();
|
||||
let mut r = Reader::new(data);
|
||||
|
||||
// DC coefficients
|
||||
let code = r.read_cw(FIRST_DC_CB).ok_or(())?;
|
||||
let mut dc = to_signed(code);
|
||||
blocks[0][0] = dc as f32 * q[0];
|
||||
let mut code = DC_CTX_INIT;
|
||||
let mut sign = 0i64;
|
||||
for blk in blocks.iter_mut().skip(1) {
|
||||
code = r.read_cw(DC_CB[code.min(6) as usize]).ok_or(())?;
|
||||
if code == 0 {
|
||||
sign = 0;
|
||||
} else {
|
||||
sign ^= -((code & 1) as i64);
|
||||
}
|
||||
let mag = (code as i64 + 1) >> 1;
|
||||
dc = (dc + ((mag ^ sign) - sign)).clamp(-(1 << 24), 1 << 24);
|
||||
blk[0] = dc as f32 * q[0];
|
||||
}
|
||||
|
||||
// AC coefficients, interleaved across blocks: position = scan_index * n + block
|
||||
let mask = n - 1;
|
||||
let total = 64 * n;
|
||||
let mut pos = mask;
|
||||
let mut run_ctx = RUN_CTX_INIT;
|
||||
let mut lev_ctx = LEVEL_CTX_INIT;
|
||||
while !r.only_padding_left() {
|
||||
let run = r.read_cw(RUN_CB[run_ctx.min(15) as usize]).ok_or(())?;
|
||||
run_ctx = run;
|
||||
pos += run as usize + 1;
|
||||
if pos >= total {
|
||||
return Err(());
|
||||
}
|
||||
let lev = r.read_cw(LEVEL_CB[lev_ctx.min(9) as usize]).ok_or(())?.saturating_add(1);
|
||||
lev_ctx = lev;
|
||||
let neg = r.read_bit();
|
||||
let i = scan[pos >> log2_blocks] as usize;
|
||||
let v = lev as f32 * q[i];
|
||||
blocks[pos & mask][i] = if neg { -v } else { v };
|
||||
}
|
||||
if r.overrun() {
|
||||
return Err(());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decode a slice's alpha plane (raster order over the slice's 16 lines) into `out`
|
||||
/// (native 8- or 16-bit values).
|
||||
fn decode_alpha(data: &[u8], ty: AlphaType, out: &mut [u16]) -> std::result::Result<(), ()> {
|
||||
let bits = if ty == AlphaType::Bits16 { 16 } else { 8 };
|
||||
let short_bits = if bits == 16 { 7 } else { 4 };
|
||||
let mask: u32 = (1 << bits) - 1;
|
||||
let mut r = Reader::new(data);
|
||||
let mut val: u32 = mask;
|
||||
let n = out.len();
|
||||
let mut idx = 0;
|
||||
'outer: loop {
|
||||
loop {
|
||||
let d = if r.read_bit() {
|
||||
r.read(bits)
|
||||
} else {
|
||||
let c = r.read(short_bits);
|
||||
let m = (c + 2) >> 1;
|
||||
if c & 1 == 1 { m.wrapping_neg() } else { m }
|
||||
};
|
||||
val = val.wrapping_add(d) & mask;
|
||||
out[idx] = val as u16;
|
||||
idx += 1;
|
||||
if idx >= n {
|
||||
break 'outer;
|
||||
}
|
||||
if r.bits_left() <= 0 || !r.read_bit() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut run = r.read(4) as usize;
|
||||
if run == 0 {
|
||||
run = r.read(11) as usize;
|
||||
}
|
||||
let run = run.min(n - idx);
|
||||
out[idx..idx + run].fill(val as u16);
|
||||
idx += run;
|
||||
if idx >= n {
|
||||
break;
|
||||
}
|
||||
if r.bits_left() <= 0 {
|
||||
// Streams may end early; the remainder repeats the last value.
|
||||
out[idx..].fill(val as u16);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if r.bits_left() < -64 {
|
||||
return Err(());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn put_block(f: &[f32; 64], plane: &mut [u16], stride: usize, (x0, y0): (usize, usize), rows: usize, il: Lines, conv: OutConv) {
|
||||
let Lines { step, parity } = il;
|
||||
if x0 >= stride {
|
||||
return;
|
||||
}
|
||||
let xn = (stride - x0).min(8);
|
||||
if xn == 8 && (y0 + 7) * step + parity < rows {
|
||||
for yy in 0..8 {
|
||||
let o = ((y0 + yy) * step + parity) * stride + x0;
|
||||
let (Some(row), Some(src)) = (plane.get_mut(o..).and_then(|p| p.first_chunk_mut::<8>()), f.as_chunks::<8>().0.get(yy)) else {
|
||||
return;
|
||||
};
|
||||
for x in 0..8 {
|
||||
row[x] = conv.conv(src[x]);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
for yy in 0..8 {
|
||||
let line = (y0 + yy) * step + parity;
|
||||
if line >= rows {
|
||||
break;
|
||||
}
|
||||
let row = &mut plane[line * stride + x0..line * stride + x0 + xn];
|
||||
let src = &f[yy * 8..yy * 8 + xn];
|
||||
for (o, &v) in row.iter_mut().zip(src) {
|
||||
*o = conv.conv(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,772 +0,0 @@
|
||||
//! ProRes frame encoding (progressive) with per-slice quantiser rate control.
|
||||
|
||||
use crate::bits::{Counter, Sink, Writer};
|
||||
use crate::dct::fdct8x8;
|
||||
use crate::header::{FRAME_ID, FrameHeader, PictureHeader, row_slice_widths};
|
||||
use crate::tables::{DC_CB, DC_CTX_INIT, FIRST_DC_CB, LEVEL_CB, LEVEL_CTX_INIT, RUN_CB, RUN_CTX_INIT, SCAN_PROGRESSIVE, qscale};
|
||||
use crate::{AlphaType, ChromaFormat, ColorInfo, Error, Frame, Interlace, Profile, Result};
|
||||
|
||||
/// Encoder configuration.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EncoderConfig {
|
||||
pub profile: Profile,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
/// Coded-bits budget per macroblock; `None` uses [`Profile::nominal_bits_per_mb`].
|
||||
pub bits_per_mb: Option<u32>,
|
||||
/// log2 of the macroblocks per slice (0..=3; 3 = 8 macroblocks, the common choice).
|
||||
pub log2_slice_mbs: u8,
|
||||
pub color: ColorInfo,
|
||||
pub aspect_ratio: u8,
|
||||
pub frame_rate_code: u8,
|
||||
/// Encode the frame's alpha plane (4444 profiles only; 16-bit alpha coding).
|
||||
pub encode_alpha: bool,
|
||||
/// Encode slices in parallel (requires the `threads` feature).
|
||||
pub threads: bool,
|
||||
/// Four-character encoder identifier written into the frame header.
|
||||
pub encoder_id: [u8; 4],
|
||||
}
|
||||
|
||||
impl EncoderConfig {
|
||||
pub fn new(profile: Profile, width: u32, height: u32) -> EncoderConfig {
|
||||
EncoderConfig {
|
||||
profile,
|
||||
width,
|
||||
height,
|
||||
bits_per_mb: None,
|
||||
log2_slice_mbs: 3,
|
||||
color: ColorInfo::default(),
|
||||
aspect_ratio: 0,
|
||||
frame_rate_code: 0,
|
||||
encode_alpha: true,
|
||||
threads: true,
|
||||
encoder_id: *b"fmcr",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ProRes encoder for one stream of same-sized progressive frames.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Encoder {
|
||||
cfg: EncoderConfig,
|
||||
/// The previous frame's uniform quantiser: where the next frame's search starts.
|
||||
last_q: u8,
|
||||
}
|
||||
|
||||
/// Quantiser search range (RDD 36 allows 1..=224).
|
||||
const Q_MIN: u8 = 1;
|
||||
const Q_MAX: u8 = 224;
|
||||
|
||||
impl Encoder {
|
||||
pub fn new(profile: Profile, width: u32, height: u32) -> Encoder {
|
||||
Encoder::with_config(EncoderConfig::new(profile, width, height))
|
||||
}
|
||||
|
||||
pub fn with_config(cfg: EncoderConfig) -> Encoder {
|
||||
Encoder { cfg, last_q: Q_MIN }
|
||||
}
|
||||
|
||||
pub fn config(&self) -> &EncoderConfig {
|
||||
&self.cfg
|
||||
}
|
||||
|
||||
pub fn bits_per_mb(&self) -> u32 {
|
||||
self.cfg.bits_per_mb.unwrap_or_else(|| self.cfg.profile.nominal_bits_per_mb())
|
||||
}
|
||||
|
||||
/// The rate-control target for one frame in bytes (excluding alpha).
|
||||
pub fn target_frame_bytes(&self) -> usize {
|
||||
let mbs = (self.cfg.width as usize).div_ceil(16) * (self.cfg.height as usize).div_ceil(16);
|
||||
mbs * self.bits_per_mb() as usize / 8
|
||||
}
|
||||
|
||||
/// Encode one frame. Input planes may be at any bit depth 8..=16 (10 is typical); the frame's
|
||||
/// chroma format must match the profile.
|
||||
pub fn encode(&mut self, frame: &Frame) -> Result<Vec<u8>> {
|
||||
let cfg = &self.cfg;
|
||||
if frame.width != cfg.width || frame.height != cfg.height {
|
||||
return Err(Error::Input(format!("frame is {}x{}, encoder is {}x{}", frame.width, frame.height, cfg.width, cfg.height)));
|
||||
}
|
||||
if cfg.width == 0 || cfg.height == 0 || cfg.width > 65535 || cfg.height > 65535 {
|
||||
return Err(Error::Input("unsupported frame size".into()));
|
||||
}
|
||||
let chroma = cfg.profile.chroma();
|
||||
if frame.chroma != chroma {
|
||||
return Err(Error::Input(format!("{:?} input for a {:?} profile", frame.chroma, chroma)));
|
||||
}
|
||||
if !(8..=16).contains(&frame.bit_depth) {
|
||||
return Err(Error::Input("bit depth must be 8..=16".into()));
|
||||
}
|
||||
let (w, h) = (cfg.width as usize, cfg.height as usize);
|
||||
let cw = chroma.chroma_width(cfg.width) as usize;
|
||||
if frame.y.len() < w * h || frame.cb.len() < cw * h || frame.cr.len() < cw * h {
|
||||
return Err(Error::Input("plane too small".into()));
|
||||
}
|
||||
let with_alpha = cfg.encode_alpha && chroma == ChromaFormat::Yuv444 && frame.alpha.is_some();
|
||||
if let Some(a) = &frame.alpha
|
||||
&& with_alpha
|
||||
&& a.len() < w * h
|
||||
{
|
||||
return Err(Error::Input("alpha plane too small".into()));
|
||||
}
|
||||
if cfg.log2_slice_mbs > 3 {
|
||||
return Err(Error::Input("log2_slice_mbs must be 0..=3".into()));
|
||||
}
|
||||
|
||||
let mb_width = w.div_ceil(16);
|
||||
let mb_height = h.div_ceil(16);
|
||||
let widths = row_slice_widths(mb_width, cfg.log2_slice_mbs);
|
||||
let mut jobs = Vec::with_capacity(widths.len() * mb_height);
|
||||
for row in 0..mb_height {
|
||||
let mut x = 0;
|
||||
for &sw in &widths {
|
||||
jobs.push((x, row, sw));
|
||||
x += sw;
|
||||
}
|
||||
}
|
||||
let src = Source {
|
||||
frame,
|
||||
w,
|
||||
h,
|
||||
cw,
|
||||
chroma444: chroma == ChromaFormat::Yuv444,
|
||||
scale: (2f32).powi(12 - frame.bit_depth as i32),
|
||||
alpha: with_alpha,
|
||||
bits_per_mb: self.bits_per_mb(),
|
||||
};
|
||||
let threads = cfg.threads;
|
||||
let prepared: Vec<SliceData> = par_map(threads, &jobs, |&(x, row, sw)| prepare_slice(&src, x, row, sw));
|
||||
let (qs, qg) = choose_quantisers(&prepared, &src, mb_width * mb_height, threads, self.last_q);
|
||||
self.last_q = qg;
|
||||
let items: Vec<(&SliceData, u8)> = prepared.iter().zip(qs).collect();
|
||||
let slices: Vec<Vec<u8>> = par_map(threads, &items, |&(sd, q)| write_slice(sd, q));
|
||||
|
||||
let hdr = FrameHeader {
|
||||
header_size: 0,
|
||||
bitstream_version: if chroma == ChromaFormat::Yuv444 { 1 } else { 0 },
|
||||
encoder_id: cfg.encoder_id,
|
||||
width: cfg.width as u16,
|
||||
height: cfg.height as u16,
|
||||
chroma,
|
||||
interlace: Interlace::Progressive,
|
||||
aspect_ratio: cfg.aspect_ratio & 15,
|
||||
frame_rate_code: cfg.frame_rate_code & 15,
|
||||
color: cfg.color,
|
||||
alpha: if with_alpha { AlphaType::Bits16 } else { AlphaType::None },
|
||||
qmat_luma: [4; 64],
|
||||
qmat_chroma: [4; 64],
|
||||
luma_matrix_present: false,
|
||||
chroma_matrix_present: false,
|
||||
};
|
||||
let fh = hdr.write();
|
||||
let slice_bytes: usize = slices.iter().map(Vec::len).sum();
|
||||
let pic_size = 8 + 2 * slices.len() + slice_bytes;
|
||||
let total = 8 + fh.len() + pic_size;
|
||||
let mut out = Vec::with_capacity(total);
|
||||
out.extend_from_slice(&(total as u32).to_be_bytes());
|
||||
out.extend_from_slice(&FRAME_ID);
|
||||
out.extend_from_slice(&fh);
|
||||
PictureHeader { header_size: 8, picture_size: pic_size, num_slices: slices.len(), log2_slice_mbs: cfg.log2_slice_mbs }.write(&mut out);
|
||||
for s in &slices {
|
||||
out.extend_from_slice(&(s.len() as u16).to_be_bytes());
|
||||
}
|
||||
for s in &slices {
|
||||
out.extend_from_slice(s);
|
||||
}
|
||||
debug_assert_eq!(out.len(), total);
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
struct Source<'a> {
|
||||
frame: &'a Frame,
|
||||
w: usize,
|
||||
h: usize,
|
||||
cw: usize,
|
||||
chroma444: bool,
|
||||
/// Multiplier from input sample values to the 12-bit domain.
|
||||
scale: f32,
|
||||
alpha: bool,
|
||||
bits_per_mb: u32,
|
||||
}
|
||||
|
||||
/// Load an 8×8 block (edge-replicated) into the 12-bit signed domain and transform it.
|
||||
fn load_block(plane: &[u16], stride: usize, height: usize, x0: usize, y0: usize, scale: f32) -> [f32; 64] {
|
||||
let mut b = [0f32; 64];
|
||||
for yy in 0..8 {
|
||||
let y = (y0 + yy).min(height - 1);
|
||||
let row = &plane[y * stride..(y + 1) * stride];
|
||||
for xx in 0..8 {
|
||||
let x = (x0 + xx).min(stride - 1);
|
||||
b[yy * 8 + xx] = row[x] as f32 * scale - 2048.0;
|
||||
}
|
||||
}
|
||||
fdct8x8(&mut b);
|
||||
b
|
||||
}
|
||||
|
||||
/// Rounding offsets for quantisation: nearest for DC, a slight dead zone for AC.
|
||||
const AC_ROUND: f32 = 0.45;
|
||||
|
||||
fn quantise(coeffs: &[[f32; 64]], qmat_inv: &[f32; 64], out: &mut Vec<[i32; 64]>) {
|
||||
out.clear();
|
||||
for c in coeffs {
|
||||
let mut l = [0i32; 64];
|
||||
let dc = c[0] * qmat_inv[0];
|
||||
l[0] = dc.round() as i32;
|
||||
for i in 1..64 {
|
||||
let v = c[i] * qmat_inv[i];
|
||||
let a = (v.abs() + AC_ROUND) as i32;
|
||||
l[i] = if v < 0.0 { -a } else { a };
|
||||
}
|
||||
out.push(l);
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_component<S: Sink>(s: &mut S, levels: &[[i32; 64]], scan: &[u8; 64]) {
|
||||
let n = levels.len();
|
||||
// DC
|
||||
let first = levels[0][0];
|
||||
let zz = if first >= 0 { (first as u32) << 1 } else { ((-(first as i64)) as u32) * 2 - 1 };
|
||||
s.put_cw(FIRST_DC_CB, zz);
|
||||
let mut prev = first;
|
||||
let mut ctx = DC_CTX_INIT;
|
||||
let mut sign = false;
|
||||
for blk in &levels[1..] {
|
||||
let dc = blk[0];
|
||||
let delta = dc - prev;
|
||||
prev = dc;
|
||||
let code = if delta == 0 {
|
||||
sign = false;
|
||||
0
|
||||
} else {
|
||||
let neg = delta < 0;
|
||||
let mag = delta.unsigned_abs();
|
||||
let c = if neg != sign { 2 * mag - 1 } else { 2 * mag };
|
||||
sign = neg;
|
||||
c
|
||||
};
|
||||
s.put_cw(DC_CB[ctx.min(6) as usize], code);
|
||||
ctx = code;
|
||||
}
|
||||
// AC, interleaved across blocks
|
||||
let mut run = 0u32;
|
||||
let mut run_ctx = RUN_CTX_INIT;
|
||||
let mut lev_ctx = LEVEL_CTX_INIT;
|
||||
for &nat in &scan[1..] {
|
||||
let nat = nat as usize;
|
||||
for blk in levels.iter().take(n) {
|
||||
let l = blk[nat];
|
||||
if l == 0 {
|
||||
run += 1;
|
||||
} else {
|
||||
s.put_cw(RUN_CB[run_ctx.min(15) as usize], run);
|
||||
run_ctx = run;
|
||||
let a = l.unsigned_abs();
|
||||
s.put_cw(LEVEL_CB[lev_ctx.min(9) as usize], a - 1);
|
||||
lev_ctx = a;
|
||||
s.put((l < 0) as u32, 1);
|
||||
run = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn component_bits(levels: &[[i32; 64]]) -> u64 {
|
||||
let mut c = Counter::default();
|
||||
encode_component(&mut c, levels, &SCAN_PROGRESSIVE);
|
||||
c.0.div_ceil(8) * 8
|
||||
}
|
||||
|
||||
/// A slice's transformed blocks (Y, Cb, Cr) and its coded alpha.
|
||||
struct SliceData {
|
||||
comps: [Vec<[f32; 64]>; 3],
|
||||
alpha: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl SliceData {
|
||||
fn header_size(&self) -> usize {
|
||||
if self.alpha.is_some() { 8 } else { 6 }
|
||||
}
|
||||
/// Largest DCT payload (bits) that keeps every 16-bit size field valid.
|
||||
fn limit_bits(&self) -> u64 {
|
||||
(65535 - self.header_size() - self.alpha.as_ref().map_or(0, Vec::len)) as u64 * 8
|
||||
}
|
||||
}
|
||||
|
||||
fn par_map<T: Sync, R: Send>(threads: bool, items: &[T], f: impl Fn(&T) -> R + Sync + Send) -> Vec<R> {
|
||||
#[cfg(feature = "threads")]
|
||||
if threads {
|
||||
use rayon::prelude::*;
|
||||
return items.par_iter().map(f).collect();
|
||||
}
|
||||
let _ = threads;
|
||||
items.iter().map(f).collect()
|
||||
}
|
||||
|
||||
fn prepare_slice(src: &Source, mb_x: usize, mb_y: usize, mbs: usize) -> SliceData {
|
||||
let f = src.frame;
|
||||
let x0 = mb_x * 16;
|
||||
let y0 = mb_y * 16;
|
||||
let mut luma = Vec::with_capacity(4 * mbs);
|
||||
for mb in 0..mbs {
|
||||
for i in 0..4 {
|
||||
luma.push(load_block(&f.y, src.w, src.h, x0 + mb * 16 + (i & 1) * 8, y0 + (i >> 1) * 8, src.scale));
|
||||
}
|
||||
}
|
||||
let mut chroma = [Vec::with_capacity(4 * mbs), Vec::with_capacity(4 * mbs)];
|
||||
for (k, plane) in [&f.cb, &f.cr].into_iter().enumerate() {
|
||||
for mb in 0..mbs {
|
||||
if src.chroma444 {
|
||||
// 4:4:4 chroma block order within a macroblock: TL, BL, TR, BR.
|
||||
for i in 0..4 {
|
||||
chroma[k].push(load_block(plane, src.cw, src.h, x0 + mb * 16 + (i >> 1) * 8, y0 + (i & 1) * 8, src.scale));
|
||||
}
|
||||
} else {
|
||||
for i in 0..2 {
|
||||
chroma[k].push(load_block(plane, src.cw, src.h, mb_x * 8 + mb * 8, y0 + i * 8, src.scale));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let [cb, cr] = chroma;
|
||||
let alpha = src.alpha.then(|| encode_alpha_slice(src, x0, y0, mbs));
|
||||
SliceData { comps: [luma, cb, cr], alpha }
|
||||
}
|
||||
|
||||
fn quantise_all(sd: &SliceData, q: u8, lv: &mut [Vec<[i32; 64]>; 3]) {
|
||||
let inv = [1.0 / (4.0 * qscale(q) as f32); 64];
|
||||
for (c, l) in sd.comps.iter().zip(lv.iter_mut()) {
|
||||
quantise(c, &inv, l);
|
||||
}
|
||||
}
|
||||
|
||||
/// Coded DCT payload bits of a slice at quantiser `q` (components padded to bytes).
|
||||
fn slice_bits(sd: &SliceData, q: u8) -> u64 {
|
||||
let mut lv = [Vec::new(), Vec::new(), Vec::new()];
|
||||
quantise_all(sd, q, &mut lv);
|
||||
lv.iter().map(|l| component_bits(l)).sum()
|
||||
}
|
||||
|
||||
/// Per-slice coded bits at the quantisers tried so far, so no quantiser is measured twice.
|
||||
struct Probes<'a> {
|
||||
slices: &'a [SliceData],
|
||||
threads: bool,
|
||||
tried: Vec<(u8, Vec<u64>, u64)>,
|
||||
}
|
||||
|
||||
impl Probes<'_> {
|
||||
/// The frame's coded bits at `q` (the sum over slices).
|
||||
fn total(&mut self, q: u8) -> u64 {
|
||||
if let Some((_, _, t)) = self.tried.iter().find(|(tq, _, _)| *tq == q) {
|
||||
return *t;
|
||||
}
|
||||
let v = par_map(self.threads, self.slices, |sd| slice_bits(sd, q));
|
||||
let t = v.iter().sum();
|
||||
self.tried.push((q, v, t));
|
||||
t
|
||||
}
|
||||
|
||||
/// The per-slice bits at `q`, measuring them if `q` was not tried yet.
|
||||
fn take(&mut self, q: u8) -> Vec<u64> {
|
||||
match self.tried.iter().position(|(tq, _, _)| *tq == q) {
|
||||
Some(i) => self.tried.swap_remove(i).1,
|
||||
None => par_map(self.threads, self.slices, |sd| slice_bits(sd, q)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The smallest quantiser in `Q_MIN..=Q_MAX` whose frame fits `budget` (`Q_MAX` when none does),
|
||||
/// assuming bits fall as the quantiser grows. The search starts at `hint` (the previous frame's
|
||||
/// quantiser) and widens its steps away from it until the answer is bracketed, then bisects: the
|
||||
/// frames of a shot need the same quantiser or a neighbour, which takes two measurements instead of
|
||||
/// the eight of a bisection over the whole range.
|
||||
fn smallest_fitting(total: &mut impl FnMut(u8) -> u64, hint: u8, budget: u64) -> u8 {
|
||||
let h = hint.clamp(Q_MIN, Q_MAX);
|
||||
// `lo` does not fit, `hi` does: the answer is in `lo + 1..=hi`.
|
||||
let (mut lo, mut hi) = if total(h) <= budget {
|
||||
let (mut hi, mut step) = (h, 1u8);
|
||||
loop {
|
||||
if hi == Q_MIN {
|
||||
return Q_MIN;
|
||||
}
|
||||
let q = hi.saturating_sub(step).max(Q_MIN);
|
||||
if total(q) > budget {
|
||||
break (q, hi);
|
||||
}
|
||||
hi = q;
|
||||
step = step.saturating_mul(2);
|
||||
}
|
||||
} else {
|
||||
let (mut lo, mut step) = (h, 1u8);
|
||||
loop {
|
||||
if lo == Q_MAX {
|
||||
return Q_MAX;
|
||||
}
|
||||
let q = lo.saturating_add(step).min(Q_MAX);
|
||||
if total(q) <= budget {
|
||||
break (lo, q);
|
||||
}
|
||||
lo = q;
|
||||
step = step.saturating_mul(2);
|
||||
}
|
||||
};
|
||||
while hi - lo > 1 {
|
||||
let mid = lo + (hi - lo) / 2;
|
||||
if total(mid) <= budget {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
hi
|
||||
}
|
||||
|
||||
/// Frame-level rate control: the smallest uniform quantiser whose frame fits the budget, then
|
||||
/// one step finer on as many slices as the remaining budget allows. Uniform quantisation
|
||||
/// approximately minimises frame MSE for a given size. Returns the slices' quantisers and the
|
||||
/// uniform one (the next frame's `hint`).
|
||||
fn choose_quantisers(slices: &[SliceData], src: &Source, total_mbs: usize, threads: bool, hint: u8) -> (Vec<u8>, u8) {
|
||||
let n = slices.len();
|
||||
let hsize = slices.first().map_or(6, SliceData::header_size);
|
||||
let fixed = 8 * (8 + 20 + 8 + n * (2 + hsize)) as u64;
|
||||
let budget = (src.bits_per_mb as u64 * total_mbs as u64).saturating_sub(fixed);
|
||||
let mut probes = Probes { slices, threads, tried: Vec::new() };
|
||||
let qg = smallest_fitting(&mut |q| probes.total(q), hint, budget);
|
||||
let mut qs = vec![qg; n];
|
||||
let mut cur = probes.take(qg);
|
||||
let total = cur.iter().sum::<u64>();
|
||||
if qg > Q_MIN && total < budget {
|
||||
// usually measured already: the search's last quantiser that did not fit
|
||||
let finer = probes.take(qg - 1);
|
||||
let mut left = budget - total;
|
||||
for (((sd, f), c), q) in slices.iter().zip(&finer).zip(cur.iter_mut()).zip(qs.iter_mut()) {
|
||||
let d = f.saturating_sub(*c);
|
||||
if d <= left && *f <= sd.limit_bits() {
|
||||
left -= d;
|
||||
*q = qg - 1;
|
||||
*c = *f;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Keep every slice within its 16-bit size fields.
|
||||
for ((sd, q), c) in slices.iter().zip(qs.iter_mut()).zip(cur.iter_mut()) {
|
||||
while *c > sd.limit_bits() && *q < Q_MAX {
|
||||
*q += 1;
|
||||
*c = slice_bits(sd, *q);
|
||||
}
|
||||
}
|
||||
(qs, qg)
|
||||
}
|
||||
|
||||
fn write_slice(sd: &SliceData, q: u8) -> Vec<u8> {
|
||||
let mut lv = [Vec::new(), Vec::new(), Vec::new()];
|
||||
quantise_all(sd, q, &mut lv);
|
||||
let comps: Vec<Vec<u8>> = lv
|
||||
.iter()
|
||||
.map(|l| {
|
||||
let mut w = Writer::new();
|
||||
encode_component(&mut w, l, &SCAN_PROGRESSIVE);
|
||||
w.finish()
|
||||
})
|
||||
.collect();
|
||||
let hsize = sd.header_size();
|
||||
let alpha = sd.alpha.as_deref().unwrap_or(&[]);
|
||||
let mut out = Vec::with_capacity(hsize + comps.iter().map(Vec::len).sum::<usize>() + alpha.len());
|
||||
out.push((hsize as u8) << 3);
|
||||
out.push(q);
|
||||
out.extend_from_slice(&(comps[0].len() as u16).to_be_bytes());
|
||||
out.extend_from_slice(&(comps[1].len() as u16).to_be_bytes());
|
||||
if sd.alpha.is_some() {
|
||||
out.extend_from_slice(&(comps[2].len() as u16).to_be_bytes());
|
||||
}
|
||||
for c in &comps {
|
||||
out.extend_from_slice(c);
|
||||
}
|
||||
out.extend_from_slice(alpha);
|
||||
out
|
||||
}
|
||||
|
||||
/// Encode the slice's alpha samples (16-bit coding) in raster order over its 16 lines.
|
||||
fn encode_alpha_slice(src: &Source, x0: usize, y0: usize, mbs: usize) -> Vec<u8> {
|
||||
let Some(a) = src.frame.alpha.as_ref() else { return Vec::new() };
|
||||
let d = src.frame.bit_depth as u32;
|
||||
let to16 = |v: u16| -> u32 {
|
||||
let v = v as u32 & ((1 << d) - 1);
|
||||
if d >= 16 { v } else { ((v << (16 - d)) | (v >> (2 * d).saturating_sub(16).min(d))) & 0xffff }
|
||||
};
|
||||
let sw = mbs * 16;
|
||||
let mut vals = Vec::with_capacity(sw * 16);
|
||||
for yy in 0..16 {
|
||||
let y = (y0 + yy).min(src.h - 1);
|
||||
for xx in 0..sw {
|
||||
let x = (x0 + xx).min(src.w - 1);
|
||||
vals.push(to16(a[y * src.w + x]));
|
||||
}
|
||||
}
|
||||
let mut w = Writer::new();
|
||||
encode_alpha(&mut w, &vals, 16);
|
||||
w.finish()
|
||||
}
|
||||
|
||||
/// Alpha plane coding: a value (difference from the previous value, short or full width), then
|
||||
/// either a one bit (another value follows) or a zero bit and a run of repeats of the value.
|
||||
pub(crate) fn encode_alpha(w: &mut Writer, vals: &[u32], bits: u32) {
|
||||
let mask = (1u32 << bits) - 1;
|
||||
let (short_bits, short_max) = if bits == 16 { (7, 64i32) } else { (4, 8i32) };
|
||||
let n = vals.len();
|
||||
let mut prev = mask;
|
||||
let mut idx = 0;
|
||||
while idx < n {
|
||||
let v = vals[idx];
|
||||
let diff = v.wrapping_sub(prev) & mask;
|
||||
// signed difference in (-2^(bits-1), 2^(bits-1)]
|
||||
let sd = if diff > mask >> 1 { diff as i32 - (mask as i32 + 1) } else { diff as i32 };
|
||||
if sd != 0 && sd.abs() <= short_max {
|
||||
w.put(0, 1);
|
||||
let c = (2 * sd.unsigned_abs() - 2) | (sd < 0) as u32;
|
||||
w.put(c, short_bits);
|
||||
} else {
|
||||
w.put(1, 1);
|
||||
w.put(diff, bits);
|
||||
}
|
||||
prev = v;
|
||||
idx += 1;
|
||||
// RDD 36:2022 §5.3.3 requires a run after every alpha difference,
|
||||
// including a singleton at the end of the slice. Do not replace that
|
||||
// final `1` with byte-alignment zeros: native decoders reject it.
|
||||
let mut run = 0;
|
||||
while idx + run < n && vals[idx + run] == prev && run < 2047 {
|
||||
run += 1;
|
||||
}
|
||||
if run == 0 {
|
||||
w.put(1, 1);
|
||||
continue;
|
||||
}
|
||||
w.put(0, 1);
|
||||
if run < 16 {
|
||||
w.put(run as u32, 4);
|
||||
} else {
|
||||
w.put(0, 4);
|
||||
w.put(run as u32, 11);
|
||||
}
|
||||
idx += run;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod rate_tests {
|
||||
use super::*;
|
||||
|
||||
/// `take` gives a quantiser's measured per-slice bits whether or not `total` tried it: an
|
||||
/// empty list would skip the slices' 16-bit size checks in `choose_quantisers`.
|
||||
#[test]
|
||||
fn take_measures_an_untried_quantiser() {
|
||||
let block = |v: f32| std::array::from_fn::<f32, 64, _>(|i| v * (64 - i) as f32);
|
||||
let slices: Vec<SliceData> =
|
||||
(1..=3).map(|k| SliceData { comps: [vec![block(40.0 * k as f32); 4], vec![block(10.0); 2], vec![block(5.0); 2]], alpha: None }).collect();
|
||||
let want = |q| slices.iter().map(|sd| slice_bits(sd, q)).collect::<Vec<_>>();
|
||||
assert!(want(4).iter().all(|&b| b > 0));
|
||||
let mut probes = Probes { slices: &slices, threads: false, tried: Vec::new() };
|
||||
assert_eq!(probes.take(4), want(4), "untried");
|
||||
probes.total(6);
|
||||
assert_eq!(probes.take(6), want(6), "tried");
|
||||
assert!(probes.tried.is_empty());
|
||||
}
|
||||
|
||||
/// Frame bits falling with the quantiser (steeply at first, then flat), as real content does.
|
||||
fn bits(q: u8) -> u64 {
|
||||
4_000_000 / (q as u64 + 3) + 1_000
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hinted_search_finds_the_smallest_fitting_quantiser() {
|
||||
for budget in (0..1_200_000).step_by(997).chain([0, 1_000, 2_000, bits(Q_MIN), bits(Q_MAX), u64::MAX]) {
|
||||
// the reference: a scan from the finest quantiser
|
||||
let want = (Q_MIN..=Q_MAX).find(|&q| bits(q) <= budget).unwrap_or(Q_MAX);
|
||||
for hint in [0, Q_MIN, 2, 7, 64, 128, 223, Q_MAX, 255] {
|
||||
let mut calls = 0;
|
||||
let got = smallest_fitting(
|
||||
&mut |q| {
|
||||
assert!((Q_MIN..=Q_MAX).contains(&q), "measured an invalid quantiser {q}");
|
||||
calls += 1;
|
||||
bits(q)
|
||||
},
|
||||
hint,
|
||||
budget,
|
||||
);
|
||||
assert_eq!(got, want, "budget {budget}, hint {hint}");
|
||||
assert!(calls <= 16, "budget {budget}, hint {hint}: {calls} measurements");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The common case (the previous frame's quantiser, or a neighbour) takes two measurements.
|
||||
#[test]
|
||||
fn a_good_hint_takes_two_measurements() {
|
||||
for want in [Q_MIN + 1, 9, 40, 150, Q_MAX] {
|
||||
let budget = bits(want);
|
||||
for hint in [want - 1, want] {
|
||||
let mut calls = 0;
|
||||
let got = smallest_fitting(
|
||||
&mut |q| {
|
||||
calls += 1;
|
||||
bits(q)
|
||||
},
|
||||
hint,
|
||||
budget,
|
||||
);
|
||||
assert_eq!((got, calls), (want, 2), "hint {hint}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Two frames, the second encoded after very different content: the encoder's memory of the
|
||||
/// previous quantiser changes the work, not the stream.
|
||||
#[test]
|
||||
fn previous_frames_do_not_change_the_stream() {
|
||||
let (w, h) = (256, 64);
|
||||
let frame = |seed: u32, detail: u32| {
|
||||
let mut f = Frame::new(w, h, ChromaFormat::Yuv422, 10, false);
|
||||
let mut s = seed;
|
||||
let cw = f.chroma_width() as usize;
|
||||
for y in 0..h as usize {
|
||||
for x in 0..w as usize {
|
||||
s ^= s << 13;
|
||||
s ^= s >> 17;
|
||||
s ^= s << 5;
|
||||
f.y[y * w as usize + x] = (400 + (x * 3 + y) as u32 % 200 + s % detail) as u16;
|
||||
}
|
||||
for x in 0..cw {
|
||||
f.cb[y * cw + x] = (500 + (x + y) as u32 % 40) as u16;
|
||||
f.cr[y * cw + x] = (520 + (x * 5) as u32 % 30) as u16;
|
||||
}
|
||||
}
|
||||
f
|
||||
};
|
||||
let (busy, plain) = (frame(7, 400), frame(9, 2));
|
||||
for profile in [Profile::Proxy, Profile::Standard, Profile::Hq] {
|
||||
let alone = |f: &Frame| Encoder::new(profile, w, h).encode(f).unwrap();
|
||||
let mut enc = Encoder::new(profile, w, h);
|
||||
enc.encode(&plain).unwrap();
|
||||
assert!(enc.encode(&busy).unwrap() == alone(&busy), "{profile:?}: busy after plain");
|
||||
assert!(enc.encode(&plain).unwrap() == alone(&plain), "{profile:?}: plain after busy");
|
||||
assert!(enc.encode(&plain).unwrap() == alone(&plain), "{profile:?}: plain after plain");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod alpha_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn alpha_final_singleton_has_a_run_code() {
|
||||
// RDD 36:2022 §5.3.3: every difference is followed by a run, including
|
||||
// the final one. Tables 13/14 encode -1; Table 12 encodes run 1 as `1`.
|
||||
// With 16-bit alpha the difference occupies exactly one byte, so the
|
||||
// required final run must start another byte, followed by zero padding.
|
||||
for (bits, expected) in [(16, vec![0x01, 0x80]), (8, vec![0x0c])] {
|
||||
let mut w = Writer::new();
|
||||
encode_alpha(&mut w, &[(1 << bits) - 2], bits);
|
||||
assert_eq!(w.finish(), expected, "{bits}-bit final singleton");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod alpha_conformance_tests {
|
||||
use super::*;
|
||||
use filmcraft_bitstream::BitReader;
|
||||
|
||||
/// RDD 36:2022 §5.3.3 and Tables 12–14, using a bounds-checked reader.
|
||||
/// Unlike the production decoder's zero-padded hot path, this checks every
|
||||
/// final run and all alignment bits, so a truncated run cannot be accepted.
|
||||
fn read_alpha(data: &[u8], bits: u32, count: usize) -> Vec<u32> {
|
||||
let mut r = BitReader::new(data);
|
||||
let mask = (1u32 << bits) - 1;
|
||||
let mut prev = mask;
|
||||
let mut out = Vec::new();
|
||||
while out.len() < count {
|
||||
let diff = if r.read_bits(1).unwrap() == 1 {
|
||||
r.read_bits(bits).unwrap()
|
||||
} else {
|
||||
let code = r.read_bits(if bits == 16 { 7 } else { 4 }).unwrap();
|
||||
let magnitude = (code >> 1) + 1;
|
||||
if code & 1 == 0 { magnitude } else { magnitude.wrapping_neg() }
|
||||
};
|
||||
prev = prev.wrapping_add(diff) & mask;
|
||||
let run = if r.read_bits(1).unwrap() == 1 {
|
||||
1
|
||||
} else {
|
||||
let short = r.read_bits(4).unwrap();
|
||||
if short == 0 { r.read_bits(11).unwrap() + 1 } else { short + 1 }
|
||||
} as usize;
|
||||
assert!(run <= count - out.len(), "alpha run exceeds the slice");
|
||||
out.extend(std::iter::repeat_n(prev, run));
|
||||
}
|
||||
assert!(r.bits_left() < 8, "only byte-alignment padding may remain");
|
||||
while r.bits_left() > 0 {
|
||||
assert_eq!(r.read_bits(1).unwrap(), 0, "alignment padding must be zero");
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_runs_cover_boundaries_and_final_values() {
|
||||
for bits in [8, 16] {
|
||||
let mask = (1 << bits) - 1;
|
||||
for run in [1, 2, 15, 16, 17, 2047, 2048, 2049, 4096] {
|
||||
for tail in 0..=8 {
|
||||
let mut values = vec![mask - 1; run];
|
||||
// Short negative/positive deltas, modulo transitions and
|
||||
// escaped full-width deltas, ending on every bit alignment.
|
||||
values.extend([0, mask, 1, 7, 8, 64, 65, mask / 2].into_iter().take(tail));
|
||||
let mut w = Writer::new();
|
||||
encode_alpha(&mut w, &values, bits);
|
||||
assert_eq!(read_alpha(&w.finish(), bits, values.len()), values, "bits={bits}, run={run}, tail={tail}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded_alpha_slices_have_complete_final_runs() {
|
||||
for profile in [Profile::P4444, Profile::P4444Xq] {
|
||||
for log2_slice_mbs in 0..=3 {
|
||||
let (width, height) = (128, 32);
|
||||
let mut frame = Frame::new(width, height, ChromaFormat::Yuv444, 16, true);
|
||||
frame.y.fill(32768);
|
||||
frame.cb.fill(32768);
|
||||
frame.cr.fill(32768);
|
||||
let alpha = frame.alpha.as_mut().unwrap();
|
||||
for (i, a) in alpha.iter_mut().enumerate() {
|
||||
*a = (i as u16).wrapping_mul(257);
|
||||
}
|
||||
let mut cfg = EncoderConfig::new(profile, width, height);
|
||||
cfg.log2_slice_mbs = log2_slice_mbs;
|
||||
let bytes = Encoder::with_config(cfg).encode(&frame).unwrap();
|
||||
let (_, start, _) = FrameHeader::parse(&bytes).unwrap();
|
||||
let picture = &bytes[start..];
|
||||
let header = PictureHeader::parse(picture).unwrap();
|
||||
let table = &picture[header.header_size..][..2 * header.num_slices];
|
||||
let mut offset = header.header_size + table.len();
|
||||
let sw = 16 << log2_slice_mbs;
|
||||
for (i, size) in table.as_chunks::<2>().0.iter().enumerate() {
|
||||
let size = u16::from_be_bytes([size[0], size[1]]) as usize;
|
||||
let slice = &picture[offset..offset + size];
|
||||
offset += size;
|
||||
let color_bytes: usize = slice[2..8].as_chunks::<2>().0.iter().map(|b| u16::from_be_bytes([b[0], b[1]]) as usize).sum();
|
||||
let payload = &slice[(slice[0] >> 3) as usize + color_bytes..];
|
||||
let actual = read_alpha(payload, 16, sw * 16);
|
||||
let x0 = i % (width as usize / sw) * sw;
|
||||
let y0 = i / (width as usize / sw) * 16;
|
||||
let alpha = frame.alpha.as_ref().unwrap();
|
||||
let expected: Vec<u32> = (y0..y0 + 16).flat_map(|y| (x0..x0 + sw).map(move |x| alpha[y * width as usize + x] as u32)).collect();
|
||||
assert_eq!(actual, expected, "{profile:?}, slice {i}, log2={log2_slice_mbs}");
|
||||
}
|
||||
assert_eq!(offset, picture.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,234 +0,0 @@
|
||||
//! Frame and picture headers (RDD 36 §5).
|
||||
|
||||
use crate::{AlphaType, ChromaFormat, ColorInfo, Error, Interlace, Result};
|
||||
use filmcraft_bitstream::BitReader;
|
||||
|
||||
/// `'icpf'`: the frame identifier following the 32-bit frame size.
|
||||
pub const FRAME_ID: [u8; 4] = *b"icpf";
|
||||
|
||||
/// A parsed ProRes frame header.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FrameHeader {
|
||||
/// Size of the frame header in bytes (from its own first field).
|
||||
pub header_size: u16,
|
||||
pub bitstream_version: u8,
|
||||
/// Four-character encoder identifier (e.g. `apl0`, `Lavc`, `fmcr`).
|
||||
pub encoder_id: [u8; 4],
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub chroma: ChromaFormat,
|
||||
pub interlace: Interlace,
|
||||
/// `aspect_ratio_information` (0 = unknown/square, 1 = 1:1, 2 = 4:3, 3 = 16:9).
|
||||
pub aspect_ratio: u8,
|
||||
/// `frame_rate_code` (0 = unknown).
|
||||
pub frame_rate_code: u8,
|
||||
pub color: ColorInfo,
|
||||
pub alpha: AlphaType,
|
||||
/// Luma quantisation matrix in raster order (all 4 when not transmitted).
|
||||
pub qmat_luma: [u8; 64],
|
||||
/// Chroma quantisation matrix in raster order (the luma matrix when not transmitted).
|
||||
pub qmat_chroma: [u8; 64],
|
||||
pub luma_matrix_present: bool,
|
||||
pub chroma_matrix_present: bool,
|
||||
}
|
||||
|
||||
impl FrameHeader {
|
||||
/// Parse the 8-byte frame prefix and frame header. Returns the header, the byte offset of the
|
||||
/// first picture, and the frame size (bytes of `data` that belong to this frame).
|
||||
pub fn parse(data: &[u8]) -> Result<(FrameHeader, usize, usize)> {
|
||||
if data.len() < 8 + 20 {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
let frame_size = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as usize;
|
||||
if data[4..8] != FRAME_ID {
|
||||
return Err(Error::Invalid("missing 'icpf' frame identifier"));
|
||||
}
|
||||
if frame_size < 28 || frame_size > data.len() {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
let data = &data[..frame_size];
|
||||
let h = &data[8..];
|
||||
let mut r = BitReader::new(h);
|
||||
let rd = |r: &mut BitReader, n: u32| r.read_bits(n).map_err(|_| Error::Truncated);
|
||||
let header_size = rd(&mut r, 16)? as u16;
|
||||
if (header_size as usize) < 20 || 8 + header_size as usize > data.len() {
|
||||
return Err(Error::Invalid("bad frame header size"));
|
||||
}
|
||||
let _reserved = rd(&mut r, 8)?;
|
||||
let bitstream_version = rd(&mut r, 8)? as u8;
|
||||
let encoder_id = [h[4], h[5], h[6], h[7]];
|
||||
r.skip(32).map_err(|_| Error::Truncated)?;
|
||||
let width = rd(&mut r, 16)? as u16;
|
||||
let height = rd(&mut r, 16)? as u16;
|
||||
let chroma = match rd(&mut r, 2)? {
|
||||
2 => ChromaFormat::Yuv422,
|
||||
3 => ChromaFormat::Yuv444,
|
||||
_ => return Err(Error::Unsupported("chroma format")),
|
||||
};
|
||||
let _ = rd(&mut r, 2)?;
|
||||
let interlace = match rd(&mut r, 2)? {
|
||||
0 => Interlace::Progressive,
|
||||
1 => Interlace::TopFieldFirst,
|
||||
2 => Interlace::BottomFieldFirst,
|
||||
_ => return Err(Error::Invalid("interlace mode")),
|
||||
};
|
||||
let _ = rd(&mut r, 2)?;
|
||||
let aspect_ratio = rd(&mut r, 4)? as u8;
|
||||
let frame_rate_code = rd(&mut r, 4)? as u8;
|
||||
let color = ColorInfo { primaries: rd(&mut r, 8)? as u8, transfer: rd(&mut r, 8)? as u8, matrix: rd(&mut r, 8)? as u8 };
|
||||
let _ = rd(&mut r, 4)?;
|
||||
let alpha = match rd(&mut r, 4)? {
|
||||
0 => AlphaType::None,
|
||||
1 => AlphaType::Bits8,
|
||||
2 => AlphaType::Bits16,
|
||||
_ => return Err(Error::Unsupported("alpha channel type")),
|
||||
};
|
||||
let _ = rd(&mut r, 14)?;
|
||||
let luma_matrix_present = rd(&mut r, 1)? == 1;
|
||||
let chroma_matrix_present = rd(&mut r, 1)? == 1;
|
||||
let mut pos = 20usize;
|
||||
let mut qmat_luma = [4u8; 64];
|
||||
if luma_matrix_present {
|
||||
let m = h.get(pos..pos + 64).ok_or(Error::Truncated)?;
|
||||
qmat_luma.copy_from_slice(m);
|
||||
pos += 64;
|
||||
}
|
||||
let mut qmat_chroma = qmat_luma;
|
||||
if chroma_matrix_present {
|
||||
let m = h.get(pos..pos + 64).ok_or(Error::Truncated)?;
|
||||
qmat_chroma.copy_from_slice(m);
|
||||
pos += 64;
|
||||
}
|
||||
if pos > header_size as usize {
|
||||
return Err(Error::Invalid("quantisation matrices exceed frame header"));
|
||||
}
|
||||
if qmat_luma.contains(&0) || qmat_chroma.contains(&0) {
|
||||
return Err(Error::Invalid("zero quantisation matrix entry"));
|
||||
}
|
||||
if width == 0 || height == 0 {
|
||||
return Err(Error::Invalid("zero frame dimension"));
|
||||
}
|
||||
let hdr = FrameHeader {
|
||||
header_size,
|
||||
bitstream_version,
|
||||
encoder_id,
|
||||
width,
|
||||
height,
|
||||
chroma,
|
||||
interlace,
|
||||
aspect_ratio,
|
||||
frame_rate_code,
|
||||
color,
|
||||
alpha,
|
||||
qmat_luma,
|
||||
qmat_chroma,
|
||||
luma_matrix_present,
|
||||
chroma_matrix_present,
|
||||
};
|
||||
Ok((hdr, 8 + header_size as usize, frame_size))
|
||||
}
|
||||
|
||||
/// Serialise the frame header (without the 8-byte size/identifier prefix).
|
||||
pub fn write(&self) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(148);
|
||||
let size = 20 + 64 * (self.luma_matrix_present as usize + self.chroma_matrix_present as usize);
|
||||
v.extend_from_slice(&(size as u16).to_be_bytes());
|
||||
v.push(0);
|
||||
v.push(self.bitstream_version);
|
||||
v.extend_from_slice(&self.encoder_id);
|
||||
v.extend_from_slice(&self.width.to_be_bytes());
|
||||
v.extend_from_slice(&self.height.to_be_bytes());
|
||||
let cf = match self.chroma {
|
||||
ChromaFormat::Yuv422 => 2u8,
|
||||
ChromaFormat::Yuv444 => 3,
|
||||
};
|
||||
let il = match self.interlace {
|
||||
Interlace::Progressive => 0u8,
|
||||
Interlace::TopFieldFirst => 1,
|
||||
Interlace::BottomFieldFirst => 2,
|
||||
};
|
||||
v.push((cf << 6) | (il << 2));
|
||||
v.push((self.aspect_ratio << 4) | (self.frame_rate_code & 15));
|
||||
v.push(self.color.primaries);
|
||||
v.push(self.color.transfer);
|
||||
v.push(self.color.matrix);
|
||||
v.push(match self.alpha {
|
||||
AlphaType::None => 0,
|
||||
AlphaType::Bits8 => 1,
|
||||
AlphaType::Bits16 => 2,
|
||||
});
|
||||
v.push(0);
|
||||
v.push(((self.luma_matrix_present as u8) << 1) | self.chroma_matrix_present as u8);
|
||||
if self.luma_matrix_present {
|
||||
v.extend_from_slice(&self.qmat_luma);
|
||||
}
|
||||
if self.chroma_matrix_present {
|
||||
v.extend_from_slice(&self.qmat_chroma);
|
||||
}
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
/// A parsed picture header (one per progressive frame, two per interlaced frame).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct PictureHeader {
|
||||
pub header_size: usize,
|
||||
/// Total picture size in bytes including this header, the slice table and the slices.
|
||||
pub picture_size: usize,
|
||||
pub num_slices: usize,
|
||||
pub log2_slice_mbs: u8,
|
||||
}
|
||||
|
||||
impl PictureHeader {
|
||||
pub fn parse(data: &[u8]) -> Result<PictureHeader> {
|
||||
if data.len() < 8 {
|
||||
return Err(Error::Truncated);
|
||||
}
|
||||
let header_size = (data[0] >> 3) as usize;
|
||||
if header_size < 8 || header_size > data.len() {
|
||||
return Err(Error::Invalid("bad picture header size"));
|
||||
}
|
||||
let picture_size = u32::from_be_bytes([data[1], data[2], data[3], data[4]]) as usize;
|
||||
let num_slices = u16::from_be_bytes([data[5], data[6]]) as usize;
|
||||
let log2_slice_mbs = (data[7] >> 4) & 3;
|
||||
Ok(PictureHeader { header_size, picture_size, num_slices, log2_slice_mbs })
|
||||
}
|
||||
|
||||
pub fn write(&self, out: &mut Vec<u8>) {
|
||||
out.push(8 << 3);
|
||||
out.extend_from_slice(&(self.picture_size as u32).to_be_bytes());
|
||||
out.extend_from_slice(&(self.num_slices as u16).to_be_bytes());
|
||||
out.push((self.log2_slice_mbs & 3) << 4);
|
||||
}
|
||||
}
|
||||
|
||||
/// Slice widths (in macroblocks) of one macroblock row: as many `1 << log2` slices as fit, then
|
||||
/// the remainder split into decreasing powers of two.
|
||||
pub fn row_slice_widths(mb_width: usize, log2_slice_mbs: u8) -> Vec<usize> {
|
||||
let size = 1usize << log2_slice_mbs;
|
||||
let mut v = vec![size; mb_width / size];
|
||||
let mut rem = mb_width % size;
|
||||
let mut s = size >> 1;
|
||||
while rem > 0 {
|
||||
if rem >= s {
|
||||
v.push(s);
|
||||
rem -= s;
|
||||
}
|
||||
s >>= 1;
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn slice_widths() {
|
||||
assert_eq!(row_slice_widths(120, 3), vec![8; 15]);
|
||||
assert_eq!(row_slice_widths(45, 3), vec![8, 8, 8, 8, 8, 4, 1]);
|
||||
assert_eq!(row_slice_widths(7, 3), vec![4, 2, 1]);
|
||||
assert_eq!(row_slice_widths(2, 3), vec![2]);
|
||||
assert_eq!(row_slice_widths(5, 0), vec![1; 5]);
|
||||
}
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
//! Clean-room Apple ProRes codec (implemented from SMPTE RDD 36).
|
||||
//!
|
||||
//! - **Decoder** ([`decode_frame`], [`decode_frame_with`]): ProRes 422 Proxy/LT/Standard/HQ and
|
||||
//! 4444/4444 XQ including the alpha channel; progressive and interlaced frames; slice-parallel
|
||||
//! with the `threads` feature (rayon). Output is planar `u16` at 10 bits for 4:2:2 and 12 bits
|
||||
//! for 4:4:4 by default (any depth 8..=16 on request).
|
||||
//! - **Encoder** ([`Encoder`]): ProRes 422 (all four flavours) and 4444/4444 XQ (optionally with
|
||||
//! alpha), progressive, with per-slice quantiser rate control targeting Apple's nominal rates.
|
||||
//!
|
||||
//! See the crate README for bitstream details, accuracy and limitations.
|
||||
|
||||
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
|
||||
|
||||
mod bits;
|
||||
mod dct;
|
||||
mod decode;
|
||||
mod encode;
|
||||
pub mod header;
|
||||
mod tables;
|
||||
|
||||
pub use decode::{DecodeOptions, decode_frame, decode_frame_into, decode_frame_with, probe};
|
||||
pub use encode::{Encoder, EncoderConfig};
|
||||
pub use header::{FrameHeader, PictureHeader};
|
||||
|
||||
/// Codec errors.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("truncated ProRes data")]
|
||||
Truncated,
|
||||
#[error("invalid ProRes data: {0}")]
|
||||
Invalid(&'static str),
|
||||
#[error("unsupported ProRes feature: {0}")]
|
||||
Unsupported(&'static str),
|
||||
#[error("corrupt slice {slice} in picture {picture}")]
|
||||
Slice { picture: usize, slice: usize },
|
||||
#[error("invalid encoder input: {0}")]
|
||||
Input(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Chroma subsampling of a frame.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ChromaFormat {
|
||||
/// Chroma planes are half width, full height.
|
||||
Yuv422,
|
||||
/// Chroma planes are full size.
|
||||
Yuv444,
|
||||
}
|
||||
|
||||
impl ChromaFormat {
|
||||
/// Width of a chroma plane for a luma width.
|
||||
pub fn chroma_width(self, width: u32) -> u32 {
|
||||
match self {
|
||||
ChromaFormat::Yuv422 => width.div_ceil(2),
|
||||
ChromaFormat::Yuv444 => width,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Frame scan structure.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
pub enum Interlace {
|
||||
#[default]
|
||||
Progressive,
|
||||
/// Two fields; the first coded picture is the top field (even lines).
|
||||
TopFieldFirst,
|
||||
/// Two fields; the first coded picture is the bottom field (odd lines).
|
||||
BottomFieldFirst,
|
||||
}
|
||||
|
||||
/// Alpha channel coding in the bitstream.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
pub enum AlphaType {
|
||||
#[default]
|
||||
None,
|
||||
Bits8,
|
||||
Bits16,
|
||||
}
|
||||
|
||||
/// Colour description codes from the frame header (ITU-T H.273 / ISO 23091-2 code points;
|
||||
/// 0 = unspecified in ProRes, 2 = unspecified in H.273).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ColorInfo {
|
||||
pub primaries: u8,
|
||||
pub transfer: u8,
|
||||
pub matrix: u8,
|
||||
}
|
||||
|
||||
impl Default for ColorInfo {
|
||||
/// BT.709 primaries, transfer and matrix.
|
||||
fn default() -> Self {
|
||||
ColorInfo { primaries: 1, transfer: 1, matrix: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
/// ProRes profiles (flavours).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Profile {
|
||||
/// `apco`
|
||||
Proxy,
|
||||
/// `apcs`
|
||||
Lt,
|
||||
/// `apcn`
|
||||
Standard,
|
||||
/// `apch`
|
||||
Hq,
|
||||
/// `ap4h`
|
||||
P4444,
|
||||
/// `ap4x`
|
||||
P4444Xq,
|
||||
}
|
||||
|
||||
impl Profile {
|
||||
pub const ALL: [Profile; 6] = [Profile::Proxy, Profile::Lt, Profile::Standard, Profile::Hq, Profile::P4444, Profile::P4444Xq];
|
||||
|
||||
/// The QuickTime sample-entry four-character code.
|
||||
pub fn fourcc(self) -> [u8; 4] {
|
||||
*match self {
|
||||
Profile::Proxy => b"apco",
|
||||
Profile::Lt => b"apcs",
|
||||
Profile::Standard => b"apcn",
|
||||
Profile::Hq => b"apch",
|
||||
Profile::P4444 => b"ap4h",
|
||||
Profile::P4444Xq => b"ap4x",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_fourcc(f: &[u8; 4]) -> Option<Profile> {
|
||||
Profile::ALL.into_iter().find(|p| &p.fourcc() == f)
|
||||
}
|
||||
|
||||
pub fn chroma(self) -> ChromaFormat {
|
||||
match self {
|
||||
Profile::P4444 | Profile::P4444Xq => ChromaFormat::Yuv444,
|
||||
_ => ChromaFormat::Yuv422,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apple's published nominal data rate at 1920×1080, 29.97 fps, in Mbit/s.
|
||||
pub fn nominal_mbps_1080p30(self) -> f64 {
|
||||
match self {
|
||||
Profile::Proxy => 45.0,
|
||||
Profile::Lt => 102.0,
|
||||
Profile::Standard => 147.0,
|
||||
Profile::Hq => 220.0,
|
||||
Profile::P4444 => 330.0,
|
||||
Profile::P4444Xq => 500.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Nominal coded bits per 16×16 macroblock (the nominal rate spread over the 8160
|
||||
/// macroblocks of a 1920×1080 frame at 30000/1001 fps). Frame targets scale with area.
|
||||
pub fn nominal_bits_per_mb(self) -> u32 {
|
||||
(self.nominal_mbps_1080p30() * 1e6 * 1001.0 / 30000.0 / 8160.0).round() as u32
|
||||
}
|
||||
}
|
||||
|
||||
/// A decoded (or to-be-encoded) picture: planar `u16` samples, one sample per element,
|
||||
/// rows packed without padding.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Frame {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub chroma: ChromaFormat,
|
||||
/// Significant bits per sample of every plane (including alpha).
|
||||
pub bit_depth: u8,
|
||||
/// `width × height`
|
||||
pub y: Vec<u16>,
|
||||
/// `chroma_width × height`
|
||||
pub cb: Vec<u16>,
|
||||
/// `chroma_width × height`
|
||||
pub cr: Vec<u16>,
|
||||
/// `width × height` (full range: 0 = transparent, max = opaque)
|
||||
pub alpha: Option<Vec<u16>>,
|
||||
pub interlace: Interlace,
|
||||
pub color: ColorInfo,
|
||||
/// `aspect_ratio_information` from the frame header.
|
||||
pub aspect_ratio: u8,
|
||||
/// `frame_rate_code` from the frame header.
|
||||
pub frame_rate_code: u8,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
/// A black (video-range) frame with the given geometry.
|
||||
pub fn new(width: u32, height: u32, chroma: ChromaFormat, bit_depth: u8, with_alpha: bool) -> Frame {
|
||||
let n = width as usize * height as usize;
|
||||
let cn = chroma.chroma_width(width) as usize * height as usize;
|
||||
let s = bit_depth.saturating_sub(8) as u32;
|
||||
Frame {
|
||||
width,
|
||||
height,
|
||||
chroma,
|
||||
bit_depth,
|
||||
y: vec![(16u32 << s) as u16; n],
|
||||
cb: vec![(128u32 << s) as u16; cn],
|
||||
cr: vec![(128u32 << s) as u16; cn],
|
||||
alpha: with_alpha.then(|| vec![((1u32 << bit_depth) - 1) as u16; n]),
|
||||
interlace: Interlace::Progressive,
|
||||
color: ColorInfo::default(),
|
||||
aspect_ratio: 0,
|
||||
frame_rate_code: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn chroma_width(&self) -> u32 {
|
||||
self.chroma.chroma_width(self.width)
|
||||
}
|
||||
|
||||
pub fn is_interlaced(&self) -> bool {
|
||||
self.interlace != Interlace::Progressive
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
//! Constant tables defined by the ProRes bitstream format (SMPTE RDD 36).
|
||||
|
||||
/// A ProRes variable-length codebook: a Golomb-Rice code for small values that switches to an
|
||||
/// Exp-Golomb code for large ones.
|
||||
///
|
||||
/// A codeword starts with `q` zero bits and a one bit.
|
||||
/// - If `q <= rice_q_max` it is a Rice codeword: `value = (q << rice) | next(rice bits)`.
|
||||
/// - Otherwise it is an Exp-Golomb codeword of order `exp` whose prefix is `q - rice_q_max - 1`
|
||||
/// zeros, offset by the number of values the Rice part covers: `(rice_q_max + 1) << rice`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) struct Codebook {
|
||||
pub rice: u8,
|
||||
pub exp: u8,
|
||||
pub rice_q_max: u8,
|
||||
}
|
||||
|
||||
const fn cb(rice: u8, exp: u8, rice_q_max: u8) -> Codebook {
|
||||
Codebook { rice, exp, rice_q_max }
|
||||
}
|
||||
|
||||
/// Codebook of the first DC coefficient in a slice component (equivalent to Exp-Golomb order 5).
|
||||
pub(crate) const FIRST_DC_CB: Codebook = cb(5, 6, 0);
|
||||
|
||||
/// DC difference codebooks, selected by the previous DC codeword value (clamped to 6).
|
||||
pub(crate) const DC_CB: [Codebook; 7] = [cb(0, 1, 0), cb(1, 2, 0), cb(1, 2, 0), cb(2, 3, 1), cb(2, 3, 1), cb(3, 4, 0), cb(3, 4, 0)];
|
||||
|
||||
/// AC run codebooks, selected by the previous run (clamped to 15).
|
||||
pub(crate) const RUN_CB: [Codebook; 16] = [
|
||||
cb(0, 1, 2),
|
||||
cb(0, 1, 2),
|
||||
cb(0, 1, 1),
|
||||
cb(0, 1, 1),
|
||||
cb(0, 1, 0),
|
||||
cb(1, 2, 1),
|
||||
cb(1, 2, 1),
|
||||
cb(1, 2, 1),
|
||||
cb(1, 2, 1),
|
||||
cb(1, 2, 0),
|
||||
cb(1, 2, 0),
|
||||
cb(1, 2, 0),
|
||||
cb(1, 2, 0),
|
||||
cb(1, 2, 0),
|
||||
cb(1, 2, 0),
|
||||
cb(2, 3, 0),
|
||||
];
|
||||
|
||||
/// AC level codebooks (level magnitude minus one), selected by the previous level magnitude (clamped to 9).
|
||||
pub(crate) const LEVEL_CB: [Codebook; 10] =
|
||||
[cb(0, 1, 0), cb(0, 2, 2), cb(0, 1, 1), cb(0, 1, 2), cb(0, 1, 0), cb(1, 2, 0), cb(1, 2, 0), cb(1, 2, 0), cb(1, 2, 0), cb(2, 3, 0)];
|
||||
|
||||
/// Initial DC codebook context for the second DC of a component.
|
||||
pub(crate) const DC_CTX_INIT: u32 = 5;
|
||||
/// Initial AC run / level codebook contexts.
|
||||
pub(crate) const RUN_CTX_INIT: u32 = 4;
|
||||
pub(crate) const LEVEL_CTX_INIT: u32 = 2;
|
||||
|
||||
/// Progressive-frame coefficient scan: scan position → raster index within the 8×8 block.
|
||||
pub(crate) const SCAN_PROGRESSIVE: [u8; 64] = [
|
||||
0, 1, 8, 9, 2, 3, 10, 11, //
|
||||
16, 17, 24, 25, 18, 19, 26, 27, //
|
||||
4, 5, 12, 20, 13, 6, 7, 14, //
|
||||
21, 28, 29, 22, 15, 23, 30, 31, //
|
||||
32, 33, 40, 48, 41, 34, 35, 42, //
|
||||
49, 56, 57, 50, 43, 36, 37, 44, //
|
||||
51, 58, 59, 52, 45, 38, 39, 46, //
|
||||
53, 60, 61, 54, 47, 55, 62, 63,
|
||||
];
|
||||
|
||||
/// Interlaced-field coefficient scan: scan position → raster index within the 8×8 block.
|
||||
pub(crate) const SCAN_INTERLACED: [u8; 64] = [
|
||||
0, 8, 1, 9, 16, 24, 17, 25, //
|
||||
2, 10, 3, 11, 18, 26, 19, 27, //
|
||||
32, 40, 33, 34, 41, 48, 56, 49, //
|
||||
42, 35, 43, 50, 57, 58, 51, 59, //
|
||||
4, 12, 5, 6, 13, 20, 28, 21, //
|
||||
14, 7, 15, 22, 29, 36, 44, 37, //
|
||||
30, 23, 31, 38, 45, 52, 60, 53, //
|
||||
46, 39, 47, 54, 61, 62, 55, 63,
|
||||
];
|
||||
|
||||
/// Quantisation scale for a slice `quantization_index` (1..=224).
|
||||
#[inline]
|
||||
pub(crate) fn qscale(qidx: u8) -> u32 {
|
||||
let q = qidx.max(1) as u32;
|
||||
if q <= 128 { q } else { 128 + ((q - 128) << 2) }
|
||||
}
|
||||
|
||||
/// Orthonormal 1-D DCT-II basis: `BASIS[u][x] = C(u)/2 · cos((2x+1)uπ/16)`, `C(0) = 1/√2`.
|
||||
pub(crate) const BASIS: [[f32; 8]; 8] = [
|
||||
[0.353_553_39, 0.353_553_39, 0.353_553_39, 0.353_553_39, 0.353_553_39, 0.353_553_39, 0.353_553_39, 0.353_553_39],
|
||||
[0.49039264, 0.415_734_8, 0.277_785_12, 0.097_545_16, -0.097_545_16, -0.277_785_12, -0.415_734_8, -0.49039264],
|
||||
[0.461_939_77, 0.191_341_72, -0.191_341_72, -0.461_939_77, -0.461_939_77, -0.191_341_72, 0.191_341_72, 0.461_939_77],
|
||||
[0.415_734_8, -0.097_545_16, -0.49039264, -0.277_785_12, 0.277_785_12, 0.49039264, 0.097_545_16, -0.415_734_8],
|
||||
[0.353_553_39, -0.353_553_39, -0.353_553_39, 0.353_553_39, 0.353_553_39, -0.353_553_39, -0.353_553_39, 0.353_553_39],
|
||||
[0.277_785_12, -0.49039264, 0.097_545_16, 0.415_734_8, -0.415_734_8, -0.097_545_16, 0.49039264, -0.277_785_12],
|
||||
[0.191_341_72, -0.461_939_77, 0.461_939_77, -0.191_341_72, -0.191_341_72, 0.461_939_77, -0.461_939_77, 0.191_341_72],
|
||||
[0.097_545_16, -0.277_785_12, 0.415_734_8, -0.49039264, 0.49039264, -0.415_734_8, 0.277_785_12, -0.097_545_16],
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn scans_are_permutations() {
|
||||
for scan in [&SCAN_PROGRESSIVE, &SCAN_INTERLACED] {
|
||||
let mut seen = [false; 64];
|
||||
for &i in scan.iter() {
|
||||
assert!(!seen[i as usize]);
|
||||
seen[i as usize] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basis_matches_cosines() {
|
||||
for u in 0..8 {
|
||||
for x in 0..8 {
|
||||
let c = if u == 0 { (0.125f64).sqrt() } else { 0.5 };
|
||||
let v = c * (((2 * x + 1) * u) as f64 * std::f64::consts::PI / 16.0).cos();
|
||||
assert!((v - BASIS[u][x] as f64).abs() < 1e-7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qscale_mapping() {
|
||||
assert_eq!(qscale(1), 1);
|
||||
assert_eq!(qscale(128), 128);
|
||||
assert_eq!(qscale(129), 132);
|
||||
assert_eq!(qscale(224), 512);
|
||||
}
|
||||
}
|
||||
@@ -1,297 +0,0 @@
|
||||
//! Shared helpers for oracle tests. ffmpeg is used only as an external fixture generator and
|
||||
//! reference decoder (never linked).
|
||||
#![allow(dead_code)]
|
||||
|
||||
use filmcraft_prores::{ChromaFormat, Frame};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
/// ffmpeg, or `None` after printing `SKIPPED` (see `filmcraft_testkit::oracle`).
|
||||
pub fn ffmpeg() -> Option<PathBuf> {
|
||||
filmcraft_testkit::ffmpeg_or_skip("prores oracle")
|
||||
}
|
||||
|
||||
pub fn fixture_dir() -> PathBuf {
|
||||
filmcraft_testkit::fixtures_dir("prores")
|
||||
}
|
||||
|
||||
fn run(ff: &Path, args: &[&str]) {
|
||||
let out = Command::new(ff).args(["-hide_banner", "-loglevel", "error", "-y"]).args(args).output().expect("run ffmpeg");
|
||||
assert!(out.status.success(), "ffmpeg {:?} failed: {}", args, String::from_utf8_lossy(&out.stderr));
|
||||
}
|
||||
|
||||
/// A ProRes fixture: a lavfi source encoded by an ffmpeg ProRes encoder.
|
||||
pub struct Spec {
|
||||
pub name: &'static str,
|
||||
/// lavfi graph (source plus filters, including the input pixel format).
|
||||
pub lavfi: String,
|
||||
pub frames: u32,
|
||||
/// Encoder arguments (codec, profile, flags...).
|
||||
pub enc: Vec<&'static str>,
|
||||
}
|
||||
|
||||
impl Spec {
|
||||
pub fn new(name: &'static str, lavfi: impl Into<String>, frames: u32, enc: &[&'static str]) -> Spec {
|
||||
Spec { name, lavfi: lavfi.into(), frames, enc: enc.to_vec() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate (or reuse) the fixture MOV.
|
||||
pub fn make(ff: &Path, spec: &Spec) -> PathBuf {
|
||||
let path = fixture_dir().join(format!("{}.mov", spec.name));
|
||||
if !path.exists() {
|
||||
let tmp = filmcraft_testkit::temp_path(&path);
|
||||
let frames = spec.frames.to_string();
|
||||
let mut args = vec!["-f", "lavfi", "-i", spec.lavfi.as_str(), "-frames:v", frames.as_str()];
|
||||
args.extend(spec.enc.iter().copied());
|
||||
let t = tmp.to_str().unwrap().to_string();
|
||||
args.push(&t);
|
||||
run(ff, &args);
|
||||
filmcraft_testkit::fixtures::publish(&tmp, &path).unwrap();
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
/// Extract the coded ProRes frames of a MOV (ffmpeg's `data` muxer concatenates packets; ProRes
|
||||
/// frames are self-delimiting through their leading 32-bit size).
|
||||
pub fn packets(ff: &Path, mov: &Path) -> Vec<Vec<u8>> {
|
||||
let out = Command::new(ff)
|
||||
.args(["-hide_banner", "-loglevel", "error", "-i"])
|
||||
.arg(mov)
|
||||
.args(["-map", "0:v", "-c", "copy", "-f", "data", "-"])
|
||||
.output()
|
||||
.expect("run ffmpeg");
|
||||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
split_frames(&out.stdout)
|
||||
}
|
||||
|
||||
pub fn split_frames(mut d: &[u8]) -> Vec<Vec<u8>> {
|
||||
let mut v = Vec::new();
|
||||
while d.len() >= 8 {
|
||||
let n = u32::from_be_bytes([d[0], d[1], d[2], d[3]]) as usize;
|
||||
assert!(n >= 8 && n <= d.len(), "bad frame size");
|
||||
v.push(d[..n].to_vec());
|
||||
d = &d[n..];
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// Reference-decode a MOV with ffmpeg to raw planar little-endian 16-bit samples.
|
||||
pub fn reference(ff: &Path, mov: &Path, pix_fmt: &str) -> Vec<u16> {
|
||||
let out = Command::new(ff)
|
||||
.args(["-hide_banner", "-loglevel", "error", "-i"])
|
||||
.arg(mov)
|
||||
.args(["-f", "rawvideo", "-pix_fmt", pix_fmt, "-"])
|
||||
.output()
|
||||
.expect("run ffmpeg");
|
||||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
out.stdout.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect()
|
||||
}
|
||||
|
||||
/// Number of u16 samples per frame of a raw planar image.
|
||||
pub fn frame_samples(w: usize, h: usize, chroma: ChromaFormat, alpha: bool) -> usize {
|
||||
let cw = if chroma == ChromaFormat::Yuv422 { w.div_ceil(2) } else { w };
|
||||
w * h * (1 + alpha as usize) + 2 * cw * h
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Copy, Debug)]
|
||||
pub struct Stats {
|
||||
pub max: u32,
|
||||
pub sum: u64,
|
||||
pub n: u64,
|
||||
pub exact: u64,
|
||||
}
|
||||
|
||||
impl Stats {
|
||||
pub fn add(&mut self, a: &[u16], b: &[u16]) {
|
||||
assert_eq!(a.len(), b.len());
|
||||
for (&x, &y) in a.iter().zip(b) {
|
||||
let d = (x as i32 - y as i32).unsigned_abs();
|
||||
self.max = self.max.max(d);
|
||||
self.sum += d as u64;
|
||||
self.n += 1;
|
||||
self.exact += (d == 0) as u64;
|
||||
}
|
||||
}
|
||||
pub fn merge(&mut self, o: &Stats) {
|
||||
self.max = self.max.max(o.max);
|
||||
self.sum += o.sum;
|
||||
self.n += o.n;
|
||||
self.exact += o.exact;
|
||||
}
|
||||
pub fn mean(&self) -> f64 {
|
||||
self.sum as f64 / self.n.max(1) as f64
|
||||
}
|
||||
pub fn exact_pct(&self) -> f64 {
|
||||
100.0 * self.exact as f64 / self.n.max(1) as f64
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare a decoded frame against a raw reference frame (planes in Y, Cb, Cr, A order).
|
||||
pub fn compare(f: &Frame, raw: &[u16]) -> (Stats, Stats) {
|
||||
let n = f.y.len();
|
||||
let c = f.cb.len();
|
||||
let mut luma = Stats::default();
|
||||
let mut rest = Stats::default();
|
||||
luma.add(&f.y, &raw[..n]);
|
||||
rest.add(&f.cb, &raw[n..n + c]);
|
||||
rest.add(&f.cr, &raw[n + c..n + 2 * c]);
|
||||
if let Some(a) = &f.alpha {
|
||||
rest.add(a, &raw[n + 2 * c..n + 2 * c + n]);
|
||||
}
|
||||
(luma, rest)
|
||||
}
|
||||
|
||||
/// PSNR over given peak.
|
||||
pub fn psnr(a: &[u16], b: &[u16], peak: f64) -> f64 {
|
||||
let mse: f64 = a.iter().zip(b).map(|(&x, &y)| (x as f64 - y as f64).powi(2)).sum::<f64>() / a.len() as f64;
|
||||
if mse == 0.0 { 99.0 } else { 10.0 * (peak * peak / mse).log10() }
|
||||
}
|
||||
|
||||
fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(payload.len() + 8);
|
||||
v.extend_from_slice(&((payload.len() + 8) as u32).to_be_bytes());
|
||||
v.extend_from_slice(kind);
|
||||
v.extend_from_slice(payload);
|
||||
v
|
||||
}
|
||||
|
||||
const MATRIX: [u32; 9] = [0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x4000_0000];
|
||||
|
||||
/// Minimal QuickTime writer (test-only): one ProRes video track, all samples in one chunk.
|
||||
pub fn write_mov(path: &Path, fourcc: &[u8; 4], width: u16, height: u16, depth: u16, frames: &[Vec<u8>]) {
|
||||
let n = frames.len() as u32;
|
||||
let be32 = |v: u32| v.to_be_bytes();
|
||||
let ftyp = bx(b"ftyp", &[b"qt ".as_slice(), &be32(0x200), b"qt "].concat());
|
||||
let mdat_payload: Vec<u8> = frames.concat();
|
||||
let data_off = ftyp.len() as u32 + 8;
|
||||
let mdat = bx(b"mdat", &mdat_payload);
|
||||
let matrix: Vec<u8> = MATRIX.iter().flat_map(|v| v.to_be_bytes()).collect();
|
||||
|
||||
let mut mvhd = vec![0u8; 4 + 8];
|
||||
mvhd.extend_from_slice(&be32(25));
|
||||
mvhd.extend_from_slice(&be32(n));
|
||||
mvhd.extend_from_slice(&be32(0x10000));
|
||||
mvhd.extend_from_slice(&[1, 0]);
|
||||
mvhd.extend_from_slice(&[0; 10]);
|
||||
mvhd.extend_from_slice(&matrix);
|
||||
mvhd.extend_from_slice(&[0; 24]);
|
||||
mvhd.extend_from_slice(&be32(2));
|
||||
|
||||
let mut tkhd = vec![0, 0, 0, 0xf];
|
||||
tkhd.extend_from_slice(&[0; 8]);
|
||||
tkhd.extend_from_slice(&be32(1));
|
||||
tkhd.extend_from_slice(&[0; 4]);
|
||||
tkhd.extend_from_slice(&be32(n));
|
||||
tkhd.extend_from_slice(&[0; 8]);
|
||||
tkhd.extend_from_slice(&[0; 8]); // layer, alternate group, volume, reserved
|
||||
tkhd.extend_from_slice(&matrix);
|
||||
tkhd.extend_from_slice(&be32((width as u32) << 16));
|
||||
tkhd.extend_from_slice(&be32((height as u32) << 16));
|
||||
|
||||
let mut mdhd = vec![0u8; 4 + 8];
|
||||
mdhd.extend_from_slice(&be32(25));
|
||||
mdhd.extend_from_slice(&be32(n));
|
||||
mdhd.extend_from_slice(&[0; 4]);
|
||||
let hdlr = [&[0u8; 4][..], b"mhlr", b"vide", &[0; 12], &[0]].concat();
|
||||
let vmhd = [0u8, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||
let dref = bx(b"dref", &[&[0u8; 4][..], &be32(1), &bx(b"alis", &[0, 0, 0, 1])].concat());
|
||||
let dinf = bx(b"dinf", &dref);
|
||||
|
||||
let mut entry = vec![0u8; 6];
|
||||
entry.extend_from_slice(&[0, 1]);
|
||||
entry.extend_from_slice(&[0; 4]); // version, revision
|
||||
entry.extend_from_slice(b"fmcr");
|
||||
entry.extend_from_slice(&be32(0));
|
||||
entry.extend_from_slice(&be32(0x400));
|
||||
entry.extend_from_slice(&width.to_be_bytes());
|
||||
entry.extend_from_slice(&height.to_be_bytes());
|
||||
entry.extend_from_slice(&be32(72 << 16));
|
||||
entry.extend_from_slice(&be32(72 << 16));
|
||||
entry.extend_from_slice(&be32(0));
|
||||
entry.extend_from_slice(&[0, 1]);
|
||||
entry.extend_from_slice(&[0; 32]);
|
||||
entry.extend_from_slice(&depth.to_be_bytes());
|
||||
entry.extend_from_slice(&[0xff, 0xff]);
|
||||
let stsd = bx(b"stsd", &[&[0u8; 4][..], &be32(1), &bx(fourcc, &entry)].concat());
|
||||
let stts = bx(b"stts", &[&[0u8; 4][..], &be32(1), &be32(n), &be32(1)].concat());
|
||||
let stsc = bx(b"stsc", &[&[0u8; 4][..], &be32(1), &be32(1), &be32(n), &be32(1)].concat());
|
||||
let mut stsz = vec![0u8; 4];
|
||||
stsz.extend_from_slice(&be32(0));
|
||||
stsz.extend_from_slice(&be32(n));
|
||||
for f in frames {
|
||||
stsz.extend_from_slice(&be32(f.len() as u32));
|
||||
}
|
||||
let stsz = bx(b"stsz", &stsz);
|
||||
let stco = bx(b"stco", &[&[0u8; 4][..], &be32(1), &be32(data_off)].concat());
|
||||
let stbl = bx(b"stbl", &[stsd, stts, stsc, stsz, stco].concat());
|
||||
let minf = bx(b"minf", &[bx(b"vmhd", &vmhd), dinf, stbl].concat());
|
||||
let mdia = bx(b"mdia", &[bx(b"mdhd", &mdhd), bx(b"hdlr", &hdlr), minf].concat());
|
||||
let trak = bx(b"trak", &[bx(b"tkhd", &tkhd), mdia].concat());
|
||||
let moov = bx(b"moov", &[bx(b"mvhd", &mvhd), trak].concat());
|
||||
std::fs::write(path, [ftyp, mdat, moov].concat()).unwrap();
|
||||
}
|
||||
|
||||
/// Decode a MOV with ffmpeg (failing on any decode error); returns (raw samples, stderr text).
|
||||
pub fn ffmpeg_decode(ff: &Path, mov: &Path, pix_fmt: &str) -> (Vec<u16>, String) {
|
||||
let out = Command::new(ff)
|
||||
.args(["-hide_banner", "-loglevel", "error", "-xerror", "-i"])
|
||||
.arg(mov)
|
||||
.args(["-f", "rawvideo", "-pix_fmt", pix_fmt, "-"])
|
||||
.output()
|
||||
.expect("run ffmpeg");
|
||||
let err = String::from_utf8_lossy(&out.stderr).to_string();
|
||||
assert!(out.status.success(), "ffmpeg failed: {err}");
|
||||
(out.stdout.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect(), err)
|
||||
}
|
||||
|
||||
/// Render a lavfi source to raw 10-bit planar frames with ffmpeg and load them as [`Frame`]s.
|
||||
pub struct SourceFrameSpec<'a> {
|
||||
pub name: &'a str,
|
||||
pub lavfi: &'a str,
|
||||
pub frames: u32,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub chroma: ChromaFormat,
|
||||
pub alpha: bool,
|
||||
}
|
||||
|
||||
pub fn source_frames(ff: &Path, spec: SourceFrameSpec<'_>) -> Vec<Frame> {
|
||||
let SourceFrameSpec { name, lavfi, frames, width, height, chroma, alpha } = spec;
|
||||
let path = fixture_dir().join(format!("{name}.raw"));
|
||||
if !path.exists() {
|
||||
let pix = match (chroma, alpha) {
|
||||
(ChromaFormat::Yuv422, _) => "yuv422p10le",
|
||||
(ChromaFormat::Yuv444, false) => "yuv444p10le",
|
||||
(ChromaFormat::Yuv444, true) => "yuva444p10le",
|
||||
};
|
||||
let tmp = filmcraft_testkit::temp_path(&path);
|
||||
let f = frames.to_string();
|
||||
run(ff, &["-f", "lavfi", "-i", lavfi, "-frames:v", &f, "-f", "rawvideo", "-pix_fmt", pix, tmp.to_str().unwrap()]);
|
||||
filmcraft_testkit::fixtures::publish(&tmp, &path).unwrap();
|
||||
}
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
let s: Vec<u16> = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect();
|
||||
let (w, h) = (width as usize, height as usize);
|
||||
let per = frame_samples(w, h, chroma, alpha);
|
||||
assert_eq!(s.len(), per * frames as usize);
|
||||
s.chunks_exact(per).map(|p| frame_from_raw(p, w as u32, h as u32, chroma, 10, alpha)).collect()
|
||||
}
|
||||
|
||||
pub fn frame_from_raw(p: &[u16], w: u32, h: u32, chroma: ChromaFormat, depth: u8, alpha: bool) -> Frame {
|
||||
let mut f = Frame::new(w, h, chroma, depth, alpha);
|
||||
let n = f.y.len();
|
||||
let c = f.cb.len();
|
||||
f.y.copy_from_slice(&p[..n]);
|
||||
f.cb.copy_from_slice(&p[n..n + c]);
|
||||
f.cr.copy_from_slice(&p[n + c..n + 2 * c]);
|
||||
if let Some(a) = f.alpha.as_mut() {
|
||||
a.copy_from_slice(&p[n + 2 * c..n + 2 * c + n]);
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
/// Frame planes concatenated (Y, Cb, Cr, A).
|
||||
pub fn planes(f: &Frame) -> Vec<u16> {
|
||||
[&f.y, &f.cb, &f.cr].into_iter().chain(f.alpha.as_ref()).flat_map(|p| p.iter().copied()).collect()
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
//! Robustness: mutated and random inputs must never panic (errors are fine).
|
||||
|
||||
use crate::common;
|
||||
use filmcraft_prores::*;
|
||||
|
||||
struct Rng(u64);
|
||||
impl Rng {
|
||||
fn next(&mut self) -> u64 {
|
||||
self.0 ^= self.0 << 13;
|
||||
self.0 ^= self.0 >> 7;
|
||||
self.0 ^= self.0 << 17;
|
||||
self.0
|
||||
}
|
||||
fn below(&mut self, n: usize) -> usize {
|
||||
(self.next() % n.max(1) as u64) as usize
|
||||
}
|
||||
}
|
||||
|
||||
fn seeds() -> Vec<Vec<u8>> {
|
||||
let mut v = Vec::new();
|
||||
for (p, w, h) in [(Profile::Proxy, 96u32, 48u32), (Profile::Hq, 70, 38), (Profile::P4444, 64, 32), (Profile::P4444Xq, 48, 20)] {
|
||||
let chroma = p.chroma();
|
||||
let mut f = Frame::new(w, h, chroma, 10, chroma == ChromaFormat::Yuv444);
|
||||
for (i, s) in f.y.iter_mut().enumerate() {
|
||||
*s = (64 + (i * 37) % 800) as u16;
|
||||
}
|
||||
if let Some(a) = f.alpha.as_mut() {
|
||||
for (i, s) in a.iter_mut().enumerate() {
|
||||
*s = if i % 5 == 0 { 1023 } else { (i % 1024) as u16 };
|
||||
}
|
||||
}
|
||||
let mut cfg = EncoderConfig::new(p, w, h);
|
||||
cfg.log2_slice_mbs = (w % 4) as u8;
|
||||
v.push(Encoder::with_config(cfg).encode(&f).unwrap());
|
||||
}
|
||||
// An interlaced ffmpeg stream, when fixtures from the oracle tests are present.
|
||||
if let Some(ff) = common::ffmpeg() {
|
||||
let spec = common::Spec::new(
|
||||
"fuzz_seed_interlaced",
|
||||
"testsrc2=s=64x48:r=25,setfield=tff,format=yuv422p10le",
|
||||
1,
|
||||
&["-c:v", "prores_ks", "-profile:v", "2", "-flags", "+ildct+ilme"],
|
||||
);
|
||||
let mov = common::make(&ff, &spec);
|
||||
v.extend(common::packets(&ff, &mov));
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutated_streams_never_panic() {
|
||||
let seeds = seeds();
|
||||
for s in &seeds {
|
||||
decode_frame(s).expect("seed decodes");
|
||||
}
|
||||
let mut rng = Rng(0x9e3779b97f4a7c15);
|
||||
let mut decoded_ok = 0;
|
||||
let iters = 3000;
|
||||
for i in 0..iters {
|
||||
let mut d = seeds[i % seeds.len()].clone();
|
||||
match rng.below(6) {
|
||||
0 => {
|
||||
for _ in 0..1 + rng.below(8) {
|
||||
let p = rng.below(d.len());
|
||||
d[p] ^= 1 << rng.below(8);
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
for _ in 0..1 + rng.below(4) {
|
||||
let p = rng.below(d.len());
|
||||
d[p] = rng.next() as u8;
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
let n = rng.below(d.len());
|
||||
d.truncate(n);
|
||||
if d.len() >= 4 && rng.below(2) == 0 {
|
||||
// keep the frame size field consistent with the truncation
|
||||
let n = d.len() as u32;
|
||||
d[..4].copy_from_slice(&n.to_be_bytes());
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
// header fields
|
||||
let p = 8 + rng.below(20.min(d.len() - 8));
|
||||
d[p] = rng.next() as u8;
|
||||
}
|
||||
4 => {
|
||||
// slice region garbage
|
||||
let start = 150.min(d.len() - 1) + rng.below(d.len().saturating_sub(150).max(1));
|
||||
for b in d.iter_mut().skip(start).take(1 + rng.below(64)) {
|
||||
*b = rng.next() as u8;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let p = rng.below(d.len());
|
||||
let extra: Vec<u8> = (0..1 + rng.below(16)).map(|_| rng.next() as u8).collect();
|
||||
d.splice(p..p, extra);
|
||||
}
|
||||
}
|
||||
for depth in [None, Some(8), Some(16)] {
|
||||
let opts = DecodeOptions { bit_depth: depth, threads: i % 2 == 0 };
|
||||
if let Ok(f) = decode_frame_with(&d, &opts) {
|
||||
decoded_ok += 1;
|
||||
assert_eq!(f.y.len(), f.width as usize * f.height as usize);
|
||||
}
|
||||
}
|
||||
let _ = probe(&d);
|
||||
}
|
||||
println!("{decoded_ok} of {} mutated decodes succeeded", iters * 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_bytes_never_panic() {
|
||||
let mut rng = Rng(12345);
|
||||
for _ in 0..2000 {
|
||||
let n = rng.below(400);
|
||||
let mut d: Vec<u8> = (0..n).map(|_| rng.next() as u8).collect();
|
||||
if d.len() >= 8 && rng.below(2) == 0 {
|
||||
d[..4].copy_from_slice(&(n as u32).to_be_bytes());
|
||||
d[4..8].copy_from_slice(b"icpf");
|
||||
}
|
||||
let _ = decode_frame(&d);
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
//! Hostile-input robustness: every header byte set to extreme values, every bit flipped,
|
||||
//! truncation at (nearly) every length must never panic or run away (errors are fine).
|
||||
|
||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||
|
||||
use filmcraft_prores::*;
|
||||
|
||||
struct Rng(u64);
|
||||
impl Rng {
|
||||
fn next(&mut self) -> u64 {
|
||||
self.0 ^= self.0 << 13;
|
||||
self.0 ^= self.0 >> 7;
|
||||
self.0 ^= self.0 << 17;
|
||||
self.0
|
||||
}
|
||||
fn below(&mut self, n: usize) -> usize {
|
||||
(self.next() % n.max(1) as u64) as usize
|
||||
}
|
||||
}
|
||||
|
||||
fn seeds() -> Vec<Vec<u8>> {
|
||||
let mut v = Vec::new();
|
||||
for (p, w, h) in [(Profile::Proxy, 96u32, 48u32), (Profile::Hq, 70, 38), (Profile::P4444, 64, 32), (Profile::P4444Xq, 48, 20)] {
|
||||
let chroma = p.chroma();
|
||||
let mut f = Frame::new(w, h, chroma, 10, chroma == ChromaFormat::Yuv444);
|
||||
for (i, s) in f.y.iter_mut().enumerate() {
|
||||
*s = (64 + (i * 37) % 800) as u16;
|
||||
}
|
||||
if let Some(a) = f.alpha.as_mut() {
|
||||
for (i, s) in a.iter_mut().enumerate() {
|
||||
*s = if i % 5 == 0 { 1023 } else { (i % 1024) as u16 };
|
||||
}
|
||||
}
|
||||
let mut cfg = EncoderConfig::new(p, w, h);
|
||||
cfg.log2_slice_mbs = (w % 4) as u8;
|
||||
if let Ok(d) = Encoder::with_config(cfg).encode(&f) {
|
||||
v.push(d);
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
fn run(d: &[u8], label: &str) {
|
||||
filmcraft_testkit::set_progress(label);
|
||||
let res = catch_unwind(AssertUnwindSafe(|| {
|
||||
for depth in [None, Some(8), Some(16)] {
|
||||
let _ = decode_frame_with(d, &DecodeOptions { bit_depth: depth, threads: depth.is_none() });
|
||||
}
|
||||
let _ = probe(d);
|
||||
}));
|
||||
assert!(res.is_ok(), "panic: {label}");
|
||||
}
|
||||
|
||||
/// Seconds the whole sweep may take before it counts as a hang (override: FILMCRAFT_HOSTILE_TIMEOUT_SECS).
|
||||
const DEADLINE_SECS: u64 = 600;
|
||||
|
||||
#[test]
|
||||
fn hostile_frames_never_panic() {
|
||||
filmcraft_testkit::with_deadline(DEADLINE_SECS, sweep);
|
||||
}
|
||||
|
||||
fn sweep() {
|
||||
let seeds = seeds();
|
||||
assert!(!seeds.is_empty());
|
||||
for (si, seed) in seeds.iter().enumerate() {
|
||||
let stride = (seed.len() / 400).max(1);
|
||||
for n in (0..seed.len()).step_by(stride) {
|
||||
run(&seed[..n], &format!("seed {si} truncate {n}"));
|
||||
}
|
||||
for p in 0..seed.len().min(256) {
|
||||
for v in [0u8, 0xff, 0x80, 0x01, 0x7f] {
|
||||
let mut d = seed.clone();
|
||||
d[p] = v;
|
||||
run(&d, &format!("seed {si} byte {p}={v:#x}"));
|
||||
}
|
||||
for b in 0..8 {
|
||||
let mut d = seed.clone();
|
||||
d[p] ^= 1 << b;
|
||||
run(&d, &format!("seed {si} flip {p}.{b}"));
|
||||
}
|
||||
}
|
||||
// Whole big-endian 32-bit words of the header set to 0 / huge values (sizes, counts, dimensions).
|
||||
for p in 0..seed.len().min(128) {
|
||||
for v in [0u32, 0xffff_ffff, 0x0001_0000, 0x7fff_ffff] {
|
||||
let mut d = seed.clone();
|
||||
if let Some(s) = d.get_mut(p..p + 4) {
|
||||
s.copy_from_slice(&v.to_be_bytes());
|
||||
}
|
||||
run(&d, &format!("seed {si} u32 {p}={v:#x}"));
|
||||
}
|
||||
}
|
||||
let mut rng = Rng(0x9e3779b97f4a7c15 ^ si as u64);
|
||||
let iters = std::env::var("FILMCRAFT_FUZZ_ITERS").ok().and_then(|s| s.parse().ok()).unwrap_or(1000);
|
||||
for i in 0..iters {
|
||||
let mut d = seed.clone();
|
||||
for _ in 0..1 + rng.below(8) {
|
||||
let p = rng.below(d.len());
|
||||
if rng.below(2) == 0 {
|
||||
d[p] ^= 1 << rng.below(8);
|
||||
} else {
|
||||
d[p] = rng.next() as u8;
|
||||
}
|
||||
}
|
||||
run(&d, &format!("seed {si} random {i}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
mod common;
|
||||
mod fuzz;
|
||||
mod fuzz_hostile;
|
||||
mod oracle_decode;
|
||||
mod oracle_encode;
|
||||
mod perf;
|
||||
mod roundtrip;
|
||||
@@ -1,153 +0,0 @@
|
||||
//! Decoder accuracy against ffmpeg's ProRes decoder (oracle only; skipped without ffmpeg).
|
||||
|
||||
use crate::common;
|
||||
use common::*;
|
||||
use filmcraft_prores::{AlphaType, ChromaFormat, DecodeOptions, Interlace, decode_frame, decode_frame_with, probe};
|
||||
|
||||
fn specs() -> Vec<Spec> {
|
||||
let p422 = "format=yuv422p10le";
|
||||
let p444 = "format=yuv444p10le";
|
||||
let alpha = "format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='mod(X*7+Y*3,1024)'";
|
||||
let ks = |p: &'static str| -> Vec<&'static str> { vec!["-c:v", "prores_ks", "-profile:v", p] };
|
||||
let aw = |p: &'static str| -> Vec<&'static str> { vec!["-c:v", "prores_aw", "-profile:v", p] };
|
||||
let mut v = Vec::new();
|
||||
for (i, p) in ["0", "1", "2", "3"].into_iter().enumerate() {
|
||||
let names = [
|
||||
["ks_proxy_testsrc2_1080", "ks_lt_testsrc2_1080", "ks_std_testsrc2_1080", "ks_hq_testsrc2_1080"],
|
||||
["ks_proxy_mandel_1918", "ks_lt_mandel_1918", "ks_std_mandel_1918", "ks_hq_mandel_1918"],
|
||||
["ks_proxy_bars_720", "ks_lt_bars_720", "ks_std_bars_720", "ks_hq_bars_720"],
|
||||
["aw_proxy_testsrc2_640", "aw_lt_testsrc2_640", "aw_std_testsrc2_640", "aw_hq_testsrc2_640"],
|
||||
];
|
||||
v.push(Spec::new(names[0][i], format!("testsrc2=s=1920x1080:r=25,{p422}"), 2, &ks(p)));
|
||||
v.push(Spec::new(names[1][i], format!("mandelbrot=s=1918x1080:r=25,{p422}"), 2, &ks(p)));
|
||||
v.push(Spec::new(names[2][i], format!("smptehdbars=s=1280x720:r=25,{p422}"), 1, &ks(p)));
|
||||
v.push(Spec::new(names[3][i], format!("testsrc2=s=640x360:r=25,{p422}"), 2, &aw(p)));
|
||||
}
|
||||
v.push(Spec::new("ks_hq_odd_70x38", format!("testsrc2=s=70x38:r=25,{p422}"), 2, &ks("3")));
|
||||
v.push(Spec::new("ks_hq_small_slices", format!("mandelbrot=s=352x288:r=25,{p422}"), 1, &[&ks("3")[..], &["-mbs_per_slice", "2"]].concat()));
|
||||
let il = ["-flags", "+ildct+ilme"];
|
||||
v.push(Spec::new("ks_hq_ntsc_tff", format!("testsrc2=s=720x486:r=30000/1001,setfield=tff,{p422}"), 2, &[&ks("3")[..], &il].concat()));
|
||||
v.push(Spec::new("ks_std_ntsc_bff", format!("mandelbrot=s=720x486:r=30000/1001,setfield=bff,{p422}"), 2, &[&ks("2")[..], &il].concat()));
|
||||
v.push(Spec::new("aw_hq_1080i", format!("testsrc2=s=1920x1080:r=30000/1001,setfield=tff,{p422}"), 1, &[&aw("3")[..], &il].concat()));
|
||||
v.push(Spec::new("ks_4444_testsrc2_1080", format!("testsrc2=s=1920x1080:r=25,{p444}"), 2, &ks("4")));
|
||||
v.push(Spec::new("ks_4444xq_mandel_1918", format!("mandelbrot=s=1918x1080:r=25,{p444}"), 1, &ks("5")));
|
||||
v.push(Spec::new("ks_4444_alpha16_640", format!("testsrc2=s=640x360:r=25,{alpha}"), 2, &[&ks("4")[..], &["-alpha_bits", "16"]].concat()));
|
||||
v.push(Spec::new("ks_4444_alpha8_640", format!("testsrc2=s=640x360:r=25,{alpha}"), 1, &[&ks("4")[..], &["-alpha_bits", "8"]].concat()));
|
||||
v.push(Spec::new("ks_4444xq_alpha_odd", format!("mandelbrot=s=333x201:r=25,{alpha}"), 1, &ks("5")));
|
||||
v.push(Spec::new("aw_4444_640", format!("testsrc2=s=640x360:r=25,{p444}"), 1, &aw("4")));
|
||||
v.push(Spec::new("aw_4444_alpha_640", format!("testsrc2=s=640x360:r=25,{alpha}"), 1, &aw("4")));
|
||||
v.push(Spec::new("ks_4444_ntsc_tff", format!("testsrc2=s=720x486:r=30000/1001,setfield=tff,{p444}"), 1, &[&ks("4")[..], &il].concat()));
|
||||
v
|
||||
}
|
||||
|
||||
fn native_pix_fmt(chroma: ChromaFormat, alpha: bool) -> &'static str {
|
||||
match (chroma, alpha) {
|
||||
(ChromaFormat::Yuv422, _) => "yuv422p10le",
|
||||
(ChromaFormat::Yuv444, false) => "yuv444p12le",
|
||||
(ChromaFormat::Yuv444, true) => "yuva444p12le",
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_matches_ffmpeg() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
let filter = std::env::var("PRORES_FIXTURE").ok();
|
||||
let mut report = vec![format!("{:<26} {:>9} {:>5} {:>8} {:>8} {:>8} {:>8}", "fixture", "format", "maxY", "meanY", "maxC/A", "meanC/A", "exact%")];
|
||||
let mut worst = 0;
|
||||
for spec in specs() {
|
||||
if let Some(f) = &filter
|
||||
&& !spec.name.contains(f.as_str())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let mov = make(&ff, &spec);
|
||||
let pkts = packets(&ff, &mov);
|
||||
assert_eq!(pkts.len(), spec.frames as usize, "{}", spec.name);
|
||||
let hdr = probe(&pkts[0]).unwrap();
|
||||
let has_alpha = hdr.alpha != AlphaType::None;
|
||||
let fmt = native_pix_fmt(hdr.chroma, has_alpha);
|
||||
let raw = reference(&ff, &mov, fmt);
|
||||
let per = frame_samples(hdr.width as usize, hdr.height as usize, hdr.chroma, has_alpha);
|
||||
assert_eq!(raw.len(), per * pkts.len());
|
||||
let (mut sl, mut sc) = (Stats::default(), Stats::default());
|
||||
for (i, p) in pkts.iter().enumerate() {
|
||||
let f = decode_frame(p).unwrap_or_else(|e| panic!("{}: frame {i}: {e}", spec.name));
|
||||
if spec.enc.contains(&"+ildct+ilme") {
|
||||
assert_ne!(f.interlace, Interlace::Progressive);
|
||||
}
|
||||
let (l, c) = compare(&f, &raw[i * per..(i + 1) * per]);
|
||||
sl.merge(&l);
|
||||
sc.merge(&c);
|
||||
}
|
||||
let mut all = sl;
|
||||
all.merge(&sc);
|
||||
worst = worst.max(all.max);
|
||||
report.push(format!(
|
||||
"{:<26} {:>9} {:>5} {:>8.5} {:>8} {:>8.5} {:>8.3}",
|
||||
spec.name,
|
||||
&fmt[..fmt.len() - 2],
|
||||
sl.max,
|
||||
sl.mean(),
|
||||
sc.max,
|
||||
sc.mean(),
|
||||
all.exact_pct()
|
||||
));
|
||||
}
|
||||
let text = report.join("\n");
|
||||
println!("{text}");
|
||||
std::fs::write(fixture_dir().join("decode_report.txt"), &text).unwrap();
|
||||
assert!(worst <= 1, "decoder deviates from reference by up to {worst} LSB\n{text}");
|
||||
}
|
||||
|
||||
/// 10-bit output of 4444 streams stays within ±1 of ffmpeg's 12-bit output rounded to 10 bits.
|
||||
#[test]
|
||||
fn decode_4444_at_10_bits() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
let spec = Spec::new("ks_4444_alpha16_640", "", 2, &[]);
|
||||
let spec = specs().into_iter().find(|s| s.name == spec.name).unwrap();
|
||||
let mov = make(&ff, &spec);
|
||||
let pkts = packets(&ff, &mov);
|
||||
let raw12 = reference(&ff, &mov, "yuva444p12le");
|
||||
let per = frame_samples(640, 360, ChromaFormat::Yuv444, true);
|
||||
let f = decode_frame_with(&pkts[0], &DecodeOptions { bit_depth: Some(10), threads: true }).unwrap();
|
||||
assert_eq!(f.bit_depth, 10);
|
||||
let r10: Vec<u16> = raw12[..per].iter().map(|&v| ((v as u32 + 2) >> 2).min(1023) as u16).collect();
|
||||
let (l, c) = compare(&f, &r10);
|
||||
assert!(l.max <= 1 && c.max <= 1, "{l:?} {c:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_threaded_matches_threaded() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
let spec = specs().into_iter().find(|s| s.name == "ks_hq_ntsc_tff").unwrap();
|
||||
let mov = make(&ff, &spec);
|
||||
let pkts = packets(&ff, &mov);
|
||||
let a = decode_frame_with(&pkts[0], &DecodeOptions { bit_depth: None, threads: false }).unwrap();
|
||||
let b = decode_frame(&pkts[0]).unwrap();
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_metadata_is_reported() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
let spec = Spec::new(
|
||||
"ks_hq_bt2020_pq",
|
||||
"testsrc2=s=128x64:r=25,format=yuv422p10le,setparams=color_primaries=bt2020:color_trc=smpte2084:colorspace=bt2020nc",
|
||||
1,
|
||||
&["-c:v", "prores_ks", "-profile:v", "3"],
|
||||
);
|
||||
let mov = make(&ff, &spec);
|
||||
let f = decode_frame(&packets(&ff, &mov)[0]).unwrap();
|
||||
assert_eq!((f.color.primaries, f.color.transfer, f.color.matrix), (9, 16, 9));
|
||||
}
|
||||
|
||||
/// Pre-generate every decode fixture (`cargo xtask fixtures`).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn generate_fixtures() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
for spec in specs() {
|
||||
let out = [fixture_dir().join(format!("{}.mov", spec.name))];
|
||||
filmcraft_testkit::fixtures::generate_and_report(&format!("prores/{}", spec.name), &out, || Some(make(&ff, &spec)));
|
||||
}
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
//! Encoder conformance and quality against ffmpeg's ProRes decoder (oracle only).
|
||||
|
||||
use crate::common;
|
||||
use common::*;
|
||||
use filmcraft_prores::{ChromaFormat, DecodeOptions, Encoder, Frame, Profile, decode_frame, decode_frame_with};
|
||||
|
||||
struct Content {
|
||||
name: &'static str,
|
||||
lavfi: &'static str,
|
||||
/// Detailed enough that every profile's rate control is budget-bound.
|
||||
busy: bool,
|
||||
}
|
||||
|
||||
const W: u32 = 1920;
|
||||
const H: u32 = 1080;
|
||||
|
||||
fn contents(chroma: ChromaFormat) -> Vec<Content> {
|
||||
match chroma {
|
||||
ChromaFormat::Yuv422 => vec![
|
||||
Content { name: "src422_testsrc2", lavfi: "testsrc2=s=1920x1080:r=25", busy: false },
|
||||
Content { name: "src422_mandel", lavfi: "mandelbrot=s=1920x1080:r=25", busy: false },
|
||||
Content { name: "src422_noise", lavfi: "testsrc2=s=1920x1080:r=25,noise=alls=40:allf=t", busy: true },
|
||||
],
|
||||
ChromaFormat::Yuv444 => vec![
|
||||
Content {
|
||||
name: "src444a_testsrc2_disc",
|
||||
lavfi: "testsrc2=s=1920x1080:r=25,format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='1023*lt(hypot(X-W/2,Y-H/2),400)'",
|
||||
busy: false,
|
||||
},
|
||||
Content { name: "src444a_noise", lavfi: "testsrc2=s=1920x1080:r=25,noise=alls=40:allf=t,format=yuva444p10le", busy: true },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
fn native_fmt(chroma: ChromaFormat, alpha: bool) -> &'static str {
|
||||
match (chroma, alpha) {
|
||||
(ChromaFormat::Yuv422, _) => "yuv422p10le",
|
||||
(ChromaFormat::Yuv444, false) => "yuv444p12le",
|
||||
(ChromaFormat::Yuv444, true) => "yuva444p12le",
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoder_output_decodes_in_ffmpeg() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
let mut report = vec![format!(
|
||||
"{:<9} {:<18} {:>9} {:>9} {:>6} {:>8} {:>8} {:>7} {:>7}",
|
||||
"profile", "content", "target", "avg", "ratio", "psnrY", "psnrC", "ffΔmax", "ffΔ=0%"
|
||||
)];
|
||||
let mut failures = Vec::new();
|
||||
for profile in Profile::ALL {
|
||||
let chroma = profile.chroma();
|
||||
for c in contents(chroma) {
|
||||
let alpha = chroma == ChromaFormat::Yuv444;
|
||||
let src = source_frames(&ff, SourceFrameSpec { name: c.name, lavfi: c.lavfi, frames: 2, width: W, height: H, chroma, alpha });
|
||||
let mut enc = Encoder::new(profile, W, H);
|
||||
let coded: Vec<Vec<u8>> = src.iter().map(|f| enc.encode(f).unwrap()).collect();
|
||||
let target = enc.target_frame_bytes() as f64;
|
||||
let avg = coded.iter().map(|c| c.len()).sum::<usize>() as f64 / coded.len() as f64;
|
||||
let mov = fixture_dir().join(format!("enc_{}_{}.mov", String::from_utf8_lossy(&profile.fourcc()), c.name));
|
||||
write_mov(&mov, &profile.fourcc(), W as u16, H as u16, if alpha { 32 } else { 24 }, &coded);
|
||||
let fmt = native_fmt(chroma, alpha);
|
||||
let (ffraw, err) = ffmpeg_decode(&ff, &mov, fmt);
|
||||
if !err.trim().is_empty() {
|
||||
failures.push(format!("{profile:?}/{}: ffmpeg reported: {err}", c.name));
|
||||
}
|
||||
let per = frame_samples(W as usize, H as usize, chroma, alpha);
|
||||
assert_eq!(ffraw.len(), per * coded.len(), "{profile:?}/{}", c.name);
|
||||
let (mut ffd, mut py, mut pc) = (Stats::default(), 0.0, 0.0);
|
||||
for (i, (cf, s)) in coded.iter().zip(&src).enumerate() {
|
||||
// Our decode of our stream agrees with ffmpeg's decode of it.
|
||||
let ours = decode_frame(cf).unwrap();
|
||||
let (l, cc) = compare(&ours, &ffraw[i * per..(i + 1) * per]);
|
||||
ffd.merge(&l);
|
||||
ffd.merge(&cc);
|
||||
// Quality against the 10-bit source.
|
||||
let d10 = decode_frame_with(cf, &DecodeOptions { bit_depth: Some(10), threads: true }).unwrap();
|
||||
py += psnr(&d10.y, &s.y, 1023.0) / coded.len() as f64;
|
||||
pc += psnr(&[d10.cb.clone(), d10.cr.clone()].concat(), &[s.cb.clone(), s.cr.clone()].concat(), 1023.0) / coded.len() as f64;
|
||||
if let (Some(a), Some(b)) = (&d10.alpha, &s.alpha) {
|
||||
assert_eq!(a, b, "alpha is lossless");
|
||||
}
|
||||
}
|
||||
let ratio = avg / target;
|
||||
report.push(format!(
|
||||
"{:<9} {:<18} {:>9.0} {:>9.0} {:>6.3} {:>8.2} {:>8.2} {:>7} {:>7.2}",
|
||||
format!("{profile:?}"),
|
||||
c.name,
|
||||
target,
|
||||
avg,
|
||||
ratio,
|
||||
py,
|
||||
pc,
|
||||
ffd.max,
|
||||
ffd.exact_pct()
|
||||
));
|
||||
if ffd.max > 1 {
|
||||
failures.push(format!("{profile:?}/{}: ffmpeg decode differs from ours by {}", c.name, ffd.max));
|
||||
}
|
||||
if ratio > 1.15 || (c.busy && ratio < 0.85) {
|
||||
failures.push(format!("{profile:?}/{}: frame size {avg:.0} vs target {target:.0}", c.name));
|
||||
}
|
||||
if profile == Profile::Hq && !c.busy && py.min(pc) < 45.0 {
|
||||
failures.push(format!("HQ/{}: PSNR {py:.2}/{pc:.2} dB below 45", c.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
let text = report.join("\n");
|
||||
println!("{text}");
|
||||
std::fs::write(fixture_dir().join("encode_report.txt"), &text).unwrap();
|
||||
assert!(failures.is_empty(), "{}\n{text}", failures.join("\n"));
|
||||
}
|
||||
|
||||
/// Odd sizes and small slices round-trip through ffmpeg too.
|
||||
#[test]
|
||||
fn odd_geometry_encodes() {
|
||||
let Some(ff) = ffmpeg() else { return };
|
||||
for (profile, w, h) in [(Profile::Standard, 1918u32, 1080u32), (Profile::Proxy, 70, 38), (Profile::P4444, 333, 201)] {
|
||||
let chroma = profile.chroma();
|
||||
let alpha = chroma == ChromaFormat::Yuv444;
|
||||
let lav = if alpha {
|
||||
format!("mandelbrot=s={w}x{h}:r=25,format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='mod(3*X+Y,1024)'")
|
||||
} else {
|
||||
format!("mandelbrot=s={w}x{h}:r=25")
|
||||
};
|
||||
let name = format!("src_odd_{w}x{h}");
|
||||
let src = source_frames(&ff, SourceFrameSpec { name: &name, lavfi: &lav, frames: 1, width: w, height: h, chroma, alpha });
|
||||
let mut enc = Encoder::new(profile, w, h);
|
||||
let coded = vec![enc.encode(&src[0]).unwrap()];
|
||||
let mov = fixture_dir().join(format!("enc_odd_{w}x{h}.mov"));
|
||||
write_mov(&mov, &profile.fourcc(), w as u16, h as u16, 24, &coded);
|
||||
let (ffraw, err) = ffmpeg_decode(&ff, &mov, native_fmt(chroma, alpha));
|
||||
assert!(err.trim().is_empty(), "{err}");
|
||||
let ours: Frame = decode_frame(&coded[0]).unwrap();
|
||||
let (l, c) = compare(&ours, &ffraw);
|
||||
assert!(l.max <= 1 && c.max <= 1, "{profile:?} {w}x{h}: {l:?} {c:?}");
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
//! Decode/encode throughput. Run with
|
||||
//! `cargo test --release -p filmcraft-prores --test it perf:: -- --ignored --nocapture`.
|
||||
|
||||
use crate::common;
|
||||
use filmcraft_prores::*;
|
||||
use std::time::Instant;
|
||||
|
||||
/// A 1080p frame whose HQ encoding uses the full nominal rate (detailed, noisy content).
|
||||
fn busy_1080p(chroma: ChromaFormat) -> Frame {
|
||||
let mut f = Frame::new(1920, 1080, chroma, 10, false);
|
||||
let mut s = 0x1234_5678u32;
|
||||
let mut rnd = move || {
|
||||
s ^= s << 13;
|
||||
s ^= s >> 17;
|
||||
s ^= s << 5;
|
||||
s
|
||||
};
|
||||
let cw = f.chroma_width() as usize;
|
||||
for y in 0..1080usize {
|
||||
for x in 0..1920usize {
|
||||
let base = 200 + ((x * 3 + y * 2) % 600) as u32;
|
||||
f.y[y * 1920 + x] = (base + rnd() % 48) as u16;
|
||||
}
|
||||
for x in 0..cw {
|
||||
f.cb[y * cw + x] = (480 + (x + y) as u32 % 64 + rnd() % 16) as u16;
|
||||
f.cr[y * cw + x] = (500 + (x * 7) as u32 % 50 + rnd() % 16) as u16;
|
||||
}
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
fn fps(frame: &[u8], threads: bool, secs: f64) -> f64 {
|
||||
let opts = DecodeOptions { bit_depth: None, threads };
|
||||
// Reuse the output planes across frames, as a player would.
|
||||
let mut out = decode_frame_with(frame, &opts).unwrap();
|
||||
let t = Instant::now();
|
||||
let mut n = 0;
|
||||
while t.elapsed().as_secs_f64() < secs {
|
||||
decode_frame_into(frame, &opts, &mut out).unwrap();
|
||||
std::hint::black_box(&out);
|
||||
n += 1;
|
||||
}
|
||||
n as f64 / t.elapsed().as_secs_f64()
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn decode_1080p_fps() {
|
||||
let mut streams: Vec<(String, Vec<u8>)> = Vec::new();
|
||||
for p in [Profile::Hq, Profile::P4444] {
|
||||
let src = busy_1080p(p.chroma());
|
||||
let mut enc = Encoder::new(p, 1920, 1080);
|
||||
let t = Instant::now();
|
||||
let coded = enc.encode(&src).unwrap();
|
||||
println!("encode {p:?} 1080p: {:.1} ms, {} bytes", t.elapsed().as_secs_f64() * 1e3, coded.len());
|
||||
streams.push((format!("ours {p:?} (nominal rate)"), coded));
|
||||
}
|
||||
if let Some(ff) = common::ffmpeg() {
|
||||
let spec = common::Spec::new("perf_ks_hq_mandel_1080", "mandelbrot=s=1920x1080:r=25,format=yuv422p10le", 1, &["-c:v", "prores_ks", "-profile:v", "3"]);
|
||||
let mov = common::make(&ff, &spec);
|
||||
streams.push(("prores_ks HQ mandelbrot".into(), common::packets(&ff, &mov).remove(0)));
|
||||
}
|
||||
for (name, s) in &streams {
|
||||
let mt = fps(s, true, 2.0);
|
||||
let st = fps(s, false, 2.0);
|
||||
println!("{name:<32} {:>8} bytes: {mt:7.1} fps threaded, {st:6.1} fps single-threaded", s.len());
|
||||
}
|
||||
}
|
||||
|
||||
/// A run of similar 3840×2160 frames (busy content drifting by a pixel per frame), as a preview
|
||||
/// render or export of footage hands them to one encoder in order.
|
||||
fn busy_2160p_run(frames: usize) -> Vec<Frame> {
|
||||
let one = busy_1080p(ChromaFormat::Yuv422);
|
||||
(0..frames)
|
||||
.map(|k| {
|
||||
let mut f = Frame::new(3840, 2160, ChromaFormat::Yuv422, 10, false);
|
||||
let (cw, scw) = (f.chroma_width() as usize, one.chroma_width() as usize);
|
||||
for y in 0..2160usize {
|
||||
for x in 0..3840usize {
|
||||
f.y[y * 3840 + x] = one.y[(y % 1080) * 1920 + (x + k) % 1920];
|
||||
}
|
||||
for x in 0..cw {
|
||||
f.cb[y * cw + x] = one.cb[(y % 1080) * scw + (x + k) % scw];
|
||||
f.cr[y * cw + x] = one.cr[(y % 1080) * scw + (x + k) % scw];
|
||||
}
|
||||
}
|
||||
f
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn encode_2160p_run_fps() {
|
||||
let run = busy_2160p_run(12);
|
||||
for p in [Profile::Standard, Profile::Hq] {
|
||||
let mut enc = Encoder::new(p, 3840, 2160);
|
||||
let t = Instant::now();
|
||||
let bytes: usize = run.iter().map(|f| enc.encode(f).unwrap().len()).sum();
|
||||
let s = t.elapsed().as_secs_f64();
|
||||
println!(
|
||||
"encode {p:?} 2160p run of {}: {:.1} ms/frame ({:.1} fps), {:.1}% of the target",
|
||||
run.len(),
|
||||
s * 1e3 / run.len() as f64,
|
||||
run.len() as f64 / s,
|
||||
bytes as f64 * 100.0 / (enc.target_frame_bytes() * run.len()) as f64
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
//! Encoder → decoder round trips on synthetic content (no external tools needed).
|
||||
|
||||
use crate::common;
|
||||
use common::psnr;
|
||||
use filmcraft_prores::*;
|
||||
|
||||
/// Deterministic test picture: gradients, a sharp-edged grid, a disc and mild noise.
|
||||
pub fn synth(w: u32, h: u32, chroma: ChromaFormat, depth: u8, alpha: bool, seed: u32) -> Frame {
|
||||
let mut f = Frame::new(w, h, chroma, depth, alpha);
|
||||
let max = ((1u32 << depth) - 1) as f32;
|
||||
let mut s = seed.wrapping_mul(2654435761).wrapping_add(1);
|
||||
let mut rnd = move || {
|
||||
s ^= s << 13;
|
||||
s ^= s >> 17;
|
||||
s ^= s << 5;
|
||||
(s % 1000) as f32 / 1000.0 - 0.5
|
||||
};
|
||||
let cw = f.chroma_width() as usize;
|
||||
let (wu, hu) = (w as usize, h as usize);
|
||||
for y in 0..hu {
|
||||
for x in 0..wu {
|
||||
let fx = x as f32 / w as f32;
|
||||
let fy = y as f32 / h as f32;
|
||||
let grid = if (x / 24 + y / 24) % 2 == 0 { 0.15 } else { 0.0 };
|
||||
let disc = if (fx - 0.6).powi(2) + (fy - 0.4).powi(2) < 0.04 { 0.2 } else { 0.0 };
|
||||
let v = 0.1 + 0.5 * fx * (1.0 - fy) + grid + disc + 0.01 * rnd();
|
||||
f.y[y * wu + x] = (v.clamp(0.0, 1.0) * max) as u16;
|
||||
if let Some(a) = f.alpha.as_mut() {
|
||||
a[y * wu + x] = if disc > 0.0 { max as u16 } else { ((fx * max) as u16) & !7 };
|
||||
}
|
||||
}
|
||||
for x in 0..cw {
|
||||
let fx = x as f32 / cw as f32;
|
||||
f.cb[y * cw + x] = ((0.5 + 0.2 * (fx * 6.0).sin()) * max) as u16;
|
||||
f.cr[y * cw + x] = ((0.5 - 0.15 * (y as f32 / 40.0).cos()) * max) as u16;
|
||||
}
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
fn quality(profile: Profile, w: u32, h: u32) -> (f64, f64, usize, usize) {
|
||||
let chroma = profile.chroma();
|
||||
let src = synth(w, h, chroma, 10, chroma == ChromaFormat::Yuv444, 7);
|
||||
let mut enc = Encoder::new(profile, w, h);
|
||||
let coded = enc.encode(&src).unwrap();
|
||||
let hdr = probe(&coded).unwrap();
|
||||
assert_eq!((hdr.width as u32, hdr.height as u32, hdr.chroma), (w, h, chroma));
|
||||
let dec = decode_frame_with(&coded, &DecodeOptions { bit_depth: Some(10), threads: true }).unwrap();
|
||||
assert_eq!(dec.alpha, src.alpha, "alpha is lossless");
|
||||
let py = psnr(&dec.y, &src.y, 1023.0);
|
||||
let pc = psnr(&[dec.cb, dec.cr].concat(), &[src.cb, src.cr].concat(), 1023.0);
|
||||
(py, pc, coded.len(), enc.target_frame_bytes())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_profile_round_trips() {
|
||||
let mut last = 0.0;
|
||||
for p in [Profile::Proxy, Profile::Lt, Profile::Standard, Profile::Hq] {
|
||||
let (py, pc, size, target) = quality(p, 640, 360);
|
||||
println!("{p:?}: Y {py:.2} dB, C {pc:.2} dB, {size} / {target} bytes");
|
||||
assert!(size as f64 <= target as f64 * 1.15);
|
||||
assert!(py >= last - 0.5, "quality should not drop with a higher profile");
|
||||
assert!(py > 38.0 && pc > 38.0);
|
||||
last = py;
|
||||
}
|
||||
let (py, pc, _, _) = quality(Profile::Hq, 640, 360);
|
||||
assert!(py >= 45.0 && pc >= 45.0);
|
||||
for p in [Profile::P4444, Profile::P4444Xq] {
|
||||
let (py, pc, size, target) = quality(p, 320, 240);
|
||||
println!("{p:?}: Y {py:.2} dB, C {pc:.2} dB, {size} / {target} bytes");
|
||||
assert!(py >= 45.0 && pc >= 45.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn odd_sizes_and_slice_widths() {
|
||||
for (w, h) in [(1u32, 1u32), (17, 9), (70, 38), (1918, 34), (720, 486)] {
|
||||
for log2 in [0u8, 1, 3] {
|
||||
let src = synth(w, h, ChromaFormat::Yuv422, 10, false, w);
|
||||
let mut cfg = EncoderConfig::new(Profile::Hq, w, h);
|
||||
cfg.log2_slice_mbs = log2;
|
||||
let coded = Encoder::with_config(cfg).encode(&src).unwrap();
|
||||
let dec = decode_frame(&coded).unwrap();
|
||||
assert_eq!((dec.width, dec.height, dec.bit_depth), (w, h, 10));
|
||||
assert_eq!(dec.cb.len(), w.div_ceil(2) as usize * h as usize);
|
||||
assert!(psnr(&dec.y, &src.y, 1023.0) > 40.0, "{w}x{h} log2 {log2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twelve_bit_input_and_output() {
|
||||
let src = synth(256, 128, ChromaFormat::Yuv444, 12, true, 3);
|
||||
let coded = Encoder::new(Profile::P4444Xq, 256, 128).encode(&src).unwrap();
|
||||
let dec = decode_frame(&coded).unwrap();
|
||||
assert_eq!(dec.bit_depth, 12);
|
||||
assert!(psnr(&dec.y, &src.y, 4095.0) > 50.0);
|
||||
assert_eq!(dec.alpha, src.alpha);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_round_trips() {
|
||||
let src = synth(64, 32, ChromaFormat::Yuv422, 10, false, 1);
|
||||
let mut cfg = EncoderConfig::new(Profile::Standard, 64, 32);
|
||||
cfg.color = ColorInfo { primaries: 9, transfer: 16, matrix: 9 };
|
||||
cfg.aspect_ratio = 1;
|
||||
cfg.frame_rate_code = 3;
|
||||
let coded = Encoder::with_config(cfg).encode(&src).unwrap();
|
||||
let hdr = probe(&coded).unwrap();
|
||||
assert_eq!(&hdr.encoder_id, b"fmcr");
|
||||
let dec = decode_frame(&coded).unwrap();
|
||||
assert_eq!(dec.color, ColorInfo { primaries: 9, transfer: 16, matrix: 9 });
|
||||
assert_eq!((dec.aspect_ratio, dec.frame_rate_code, dec.interlace), (1, 3, Interlace::Progressive));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_input() {
|
||||
let mut enc = Encoder::new(Profile::Hq, 64, 32);
|
||||
assert!(enc.encode(&Frame::new(32, 32, ChromaFormat::Yuv422, 10, false)).is_err());
|
||||
assert!(enc.encode(&Frame::new(64, 32, ChromaFormat::Yuv444, 10, false)).is_err());
|
||||
let mut short = Frame::new(64, 32, ChromaFormat::Yuv422, 10, false);
|
||||
short.cb.truncate(10);
|
||||
assert!(enc.encode(&short).is_err());
|
||||
assert!(decode_frame(&[]).is_err());
|
||||
assert!(decode_frame(b"\0\0\0\x20icpfxxxxxxxxxxxxxxxxxxxxxxxx").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_identifiers() {
|
||||
for p in Profile::ALL {
|
||||
assert_eq!(Profile::from_fourcc(&p.fourcc()), Some(p));
|
||||
}
|
||||
assert_eq!(Profile::Hq.nominal_bits_per_mb(), 900);
|
||||
assert_eq!(Profile::Proxy.chroma(), ChromaFormat::Yuv422);
|
||||
assert_eq!(Profile::P4444Xq.chroma(), ChromaFormat::Yuv444);
|
||||
}
|
||||
@@ -31,7 +31,13 @@ fn perf_24_tracks_3_effects_realtime_factor() {
|
||||
});
|
||||
let rt = secs / el;
|
||||
eprintln!("mixer perf: 24 tracks x 3 inserts + submix, {secs} s of 48 kHz stereo in {el:.3} s of CPU on one core = {rt:.1}x realtime");
|
||||
let min = if cfg!(debug_assertions) { 1.0 } else { 4.0 };
|
||||
let min = if std::env::var_os("LLVM_PROFILE_FILE").is_some() {
|
||||
0.5
|
||||
} else if cfg!(debug_assertions) {
|
||||
1.0
|
||||
} else {
|
||||
4.0
|
||||
};
|
||||
assert!(rt > min, "realtime factor {rt:.2}");
|
||||
}
|
||||
|
||||
|
||||
@@ -442,16 +442,22 @@ fn offline_badges_proxy_toggle_and_dialogs() {
|
||||
assert!(d.app().ui.create_proxies.is_some());
|
||||
d.snapshot("media-create-proxies");
|
||||
d.click("proxies.preset.prores_proxy_quarter");
|
||||
let prior_jobs = d.app().session.jobs.len();
|
||||
d.click("proxies.ok");
|
||||
for _ in 0..400 {
|
||||
let proxy_jobs = d.app().session.jobs[prior_jobs..].to_vec();
|
||||
assert!(!proxy_jobs.is_empty(), "creating proxies starts a media job");
|
||||
// Media jobs share a queue with other UI tests; allow a queued transcode up to 60 seconds.
|
||||
for _ in 0..1200 {
|
||||
d.frames(1);
|
||||
let attached = d.app().session.project.items.values().filter(|i| i.as_media().is_some_and(|m| m.proxy.is_some())).count();
|
||||
if attached == 2 {
|
||||
let finished = proxy_jobs.iter().all(|job| job.result.lock().unwrap_or_else(|e| e.into_inner()).is_some());
|
||||
if attached == 2 || finished {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
}
|
||||
assert_eq!(d.app().session.project.items.values().filter(|i| i.as_media().is_some_and(|m| m.proxy.is_some())).count(), 2, "proxies attached");
|
||||
let jobs = d.exec("jobs.list", json!({}));
|
||||
assert_eq!(d.app().session.project.items.values().filter(|i| i.as_media().is_some_and(|m| m.proxy.is_some())).count(), 2, "proxies attached; jobs: {jobs}");
|
||||
d.click("program.transport.media.toggleProxies");
|
||||
assert!(d.app().session.prefs.media.enable_proxies);
|
||||
assert_eq!(d.ids("project.item.").into_iter().filter(|i| i.ends_with(".proxy")).count(), 2, "proxy badges");
|
||||
|
||||
@@ -336,13 +336,21 @@ fn playback_at_a_newer_revision_keeps_the_frame_being_rendered() {
|
||||
let d = Driver::demo();
|
||||
let app = d.harness.state();
|
||||
let idle = || {
|
||||
for _ in 0..200_000 {
|
||||
if app.frames.queue_len() == 0 {
|
||||
return;
|
||||
if std::env::var_os("LLVM_PROFILE_FILE").is_some() {
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
|
||||
while app.frames.queue_len() != 0 {
|
||||
assert!(std::time::Instant::now() < deadline, "the frame queue never emptied ({} queued)", app.frames.queue_len());
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_micros(50));
|
||||
} else {
|
||||
for _ in 0..200_000 {
|
||||
if app.frames.queue_len() == 0 {
|
||||
return;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_micros(50));
|
||||
}
|
||||
panic!("the frame queue never emptied ({} queued)", app.frames.queue_len());
|
||||
}
|
||||
panic!("the frame queue never emptied");
|
||||
};
|
||||
// the clip with Color Grading at full resolution: tens of milliseconds on the CPU
|
||||
let seq = app.session.state.active_sequence.unwrap();
|
||||
|
||||
@@ -29,7 +29,7 @@ Design principles:
|
||||
L2 edit · codecs · interchange · captions · speech · tts · tts-text
|
||||
L1 frame · media · project · audio-dsp · text
|
||||
L0 foundation: time · geom · color · bitstream · testkit (dev-dependency only)
|
||||
codecs/containers: isobmff · matroska · cfb · h264enc · prores · dnx · dng · craft-media (avi · mpegts · mxf · ogg) · craft-video (h264 · hevc · vp9 · av1 · mpeg2 · apv) · craft-audio (aac · ac3 · mpa · opus · flac · mp3 encoder)
|
||||
codecs/containers: isobmff · matroska · cfb · dnx · dng · craft-media (avi · mpegts · mxf · ogg) · craft-video (h264 decoder and encoder · hevc · vp9 · av1 · mpeg2 · apv · prores decoder and encoder) · craft-audio (aac · ac3 · mpa · opus · flac · mp3 encoder)
|
||||
```
|
||||
|
||||
Crates are named `filmcraft-<dir>` (`crates/time` is `filmcraft-time`). The apps are `filmcraft`
|
||||
@@ -44,13 +44,11 @@ and `filmcraft-cli`.
|
||||
| `isobmff` | L0 | MP4/MOV demux and mux |
|
||||
| `matroska` | L0 | MKV/WebM demux |
|
||||
| `cfb` | L0 | Compound File Binary ([MS-CFB] structured storage) reader and writer, the container of AAF ([README](../crates/cfb/README.md)) |
|
||||
| `h264enc` | L0 | H.264 encoder |
|
||||
| `prores` | L0 | ProRes decoder and encoder |
|
||||
| `dnx` | L0 | DNxHD / DNxHR (SMPTE ST 2019-1 VC-3) decoder and DNxHR encoder |
|
||||
| `dng` | L0 | DNG / CinemaDNG camera RAW decoder: TIFF container, uncompressed and lossless JPEG raw data, DNG processing to linear Rec. 709 ([README](../crates/dng/README.md)) |
|
||||
| `craft-audio` | shared | the audio codecs, from [craft-libs](https://github.com/storytold/craft-libs/tree/main/crates/audio): AAC-LC decoder and encoder, AC-3 / E-AC-3 decoder, MPEG-1/2 Audio Layer I and II decoder, Opus decoder, FLAC encoder, MPEG-1/2 Layer III (MP3) encoder. They moved there from this repository; fixes go to craft-libs |
|
||||
| `craft-media` | shared | containers from [craft-libs](https://github.com/storytold/craft-libs/tree/main/crates/media): ISO BMFF, Matroska, AVI / OpenDML, MPEG-2 transport and program streams, MXF (OP1a / OP-Atom demux and writer), and the Ogg demuxer with Ogg Opus timing and Vorbis headers |
|
||||
| `craft-video` | shared | video codecs from [craft-libs](https://github.com/storytold/craft-libs/tree/main/crates/video): the H.264, H.265 (Main / Main 10), VP9, AV1 and MPEG-2 / MPEG-1 decoders and the APV decoder and encoder. They moved there from this repository; fixes, their READMEs and their conformance tests are in craft-libs |
|
||||
| `craft-video` | shared | video codecs from [craft-libs](https://github.com/storytold/craft-libs/tree/main/crates/video): H.264, H.265 (Main / Main 10), VP9, AV1 and MPEG-2 / MPEG-1 decoders; APV decoder and encoder; ProRes decoder and encoder. They moved there from this repository; fixes, their READMEs and their conformance tests are in craft-libs |
|
||||
| `testkit` | L0 | test-only helpers, used only as a dev-dependency: ffmpeg/ffprobe discovery, fixture dirs, golden images ([testing.md](testing.md)) |
|
||||
| `frame` | L1 | `VideoFrame` (planar YUV / RGBA8 / linear RGBA f32, colour metadata), `AudioBuffer` |
|
||||
| `media` | L1 | `MediaSource` trait, probing/openers, frame cache, generators, stills, image sequences, WAV / Broadcast WAV |
|
||||
@@ -676,7 +674,7 @@ file.exportMedia {path, preset?, settings?, format?, range?, …} export.qui
|
||||
| Format | Encoder | Container |
|
||||
|---|---|---|
|
||||
| `h264` | `craft_video::h264::Encoder` (profile, level, CBR / VBR 1-pass / VBR 2-pass, keyframe distance) + `craft_audio::aac` | MP4 or QuickTime (`isobmff`, the Multiplexer setting) |
|
||||
| `prores` | `filmcraft-prores` (Proxy / LT / 422 / HQ) | MOV |
|
||||
| `prores` | `craft_video::prores` (Proxy / LT / 422 / HQ) | MOV |
|
||||
| `dnxhr` | `filmcraft-dnx` (LB / SQ / HQ / HQX) | MOV (`AVdh`) |
|
||||
| `mjpeg` | built in | MOV |
|
||||
| `aac` | `craft_audio::aac` (AAC-LC, the audio settings' bitrate) | audio only: MPEG-4 audio `.m4a` (`isobmff`, priming in an edit list), or raw ADTS when the path ends in `.aac` |
|
||||
|
||||
+5
-5
@@ -4,7 +4,7 @@
|
||||
cargo test --workspace # everything; oracle tests skip without ffmpeg
|
||||
cargo nextest run --workspace # the same in parallel processes (doctests: cargo test --doc)
|
||||
cargo test -p filmcraft-codecs # one crate
|
||||
cargo test --release -p filmcraft-prores # codec tests are much faster in release
|
||||
cargo test --release -p craft-video --features prores,threads --test prores # run from the craft-libs workspace
|
||||
cargo xtask ci # all gates (fmt, clippy, tests, layers, assets, wasm)
|
||||
cargo xtask fixtures # pre-generate the ffmpeg fixture matrix
|
||||
FILMCRAFT_REQUIRE_ORACLES=1 cargo test --workspace # CI: missing ffmpeg fails instead of skipping
|
||||
@@ -117,8 +117,8 @@ are never linked or shipped ([AGENTS.md](../AGENTS.md) §2).
|
||||
parallel test run or on a fresh machine). It runs each crate's ignored `generate_fixtures` test —
|
||||
the same generators the oracle tests call — for `h264`, `hevc`, `isobmff`, `matroska`, `dnx`,
|
||||
`prores`, `mpeg2v`, `ac3` and `codecs` (MXF, Ogg and MPEG TS / PS), and prints one `made` / `cached` / `skipped` line per fixture plus a summary. The
|
||||
`aac`, `opus` and `h264enc` oracles generate small per-test signals on demand and are not part of
|
||||
the matrix.
|
||||
`aac` and `opus` oracles here generate small per-test signals on demand, and craft-libs H.264
|
||||
encoder tests generate their own signals; none are part of the fixture matrix.
|
||||
- Fixture sources are synthetic: `testsrc2`, `mandelbrot`, SMPTE bars, noise and fades, sine tones.
|
||||
H.264 and HEVC fixtures need ffmpeg built with libx264 and libx265. VideoToolbox fixtures are
|
||||
generated only on macOS. The Media Foundation parity tests (`crates/platform/tests/it/media_foundation.rs`,
|
||||
@@ -148,7 +148,7 @@ anything is "fixed" to match) and add asserts for anything new.
|
||||
|---|---|---|
|
||||
| `h264` | decode every fixture single-threaded and frame-threaded; compare with `ffmpeg -f rawvideo -pix_fmt yuv420p` | **bit-exact**, every frame |
|
||||
| `hevc` | same, `yuv420p` / `yuv420p10le` | **bit-exact** (8- and 10-bit) |
|
||||
| `h264enc` | `ffmpeg -v warning -err_detect +crccheck+bitstream+buffer+explode` decodes our stream | no output at all (a `corrupt decoded frame` warning fails; a negative control checks dropped and truncated slices are reported), exact frame count, **bit-identical to the encoder's reconstruction**; B-frame order checked with ffprobe; rate-control targets |
|
||||
| craft-video H.264 encoder (`h264-encode`) | `ffmpeg -v warning -err_detect +crccheck+bitstream+buffer+explode` decodes the shared encoder stream | no output at all (a `corrupt decoded frame` warning fails; a negative control checks dropped and truncated slices are reported), exact frame count, **bit-identical to the encoder's reconstruction**; B-frame order checked with ffprobe; rate-control targets |
|
||||
| `prores` decode | ffmpeg `prores_ks` / `prores_aw` fixtures, compared at native depth | within **±1 LSB** (different integer IDCT); alpha bit-exact |
|
||||
| `prores` encode | ffmpeg decodes with `-xerror` | no errors; agrees with our decoder within ±1 |
|
||||
| `dnx` decode | ffmpeg VC-3 fixtures (13 DNxHD CIDs, DNxHR LB/SQ/HQ/HQX/444), compared at native depth | within **±2 LSB**, ≤ 100 samples per million beyond ±1 (ffmpeg's integer IDCT; we evaluate the exact IDCT) |
|
||||
@@ -310,7 +310,7 @@ engine behaviour without a window.
|
||||
|---|---|
|
||||
| H.264 decode, 1080p, threads sweep | in craft-libs: `cargo test --release -p craft-video --features h264-decode,threads --test h264_decode perf:: -- --ignored --nocapture` |
|
||||
| HEVC decode, 1080p / 2160p | in craft-libs: `cargo test --release -p craft-video --features hevc-decode,threads --test hevc_decode perf:: -- --ignored --nocapture` |
|
||||
| ProRes decode/encode | `cargo test --release -p filmcraft-prores --test it perf:: -- --ignored --nocapture` |
|
||||
| ProRes decode/encode | in craft-libs: `cargo test --release -p craft-video --features prores,threads --test prores perf:: -- --ignored --nocapture` |
|
||||
| Encoder speed / PSNR / bitrate | in craft-libs: `cargo run --release -p craft-video --features h264-encode --example h264_h264enc_bench -- …` |
|
||||
| Export to H.264 + AAC on the CPU (1080p clip through `file.exportMedia`, wall time per run, `--keep out.mp4` to compare files) | `cargo run --release -p filmcraft-engine --example bench_export -- --runs 7` |
|
||||
| Decode a real file through the media stack | `cargo run --release -p filmcraft-cli -- bench-decode file.mp4 --frames 120` |
|
||||
|
||||
Reference in new issue
Block a user