From 900d090647f2a68ed0f4215c39bb80c28d175567 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:41:46 +0100 Subject: [PATCH 01/24] codecs, export: decode and encode DNxHD / DNxHR with craft-video; remove crates/dnx FilmCraft's local VC-3 codec moved to craft-libs as craft-video's dnx module (craft-libs #702-#708, with the #4101 scan-line fix ported in #833). Turn on craft-video's dnx feature at the existing 156b491b pin and point the MOV / MXF probes, the DnxDecoder and the DNxHR export encoder at it. The shared decoder budgets 256 MiB of planes per coding unit by default, which would refuse 8K DNxHR 444 with alpha. Keep FilmCraft's previous 1 GiB whole-frame ceiling: check it on the first coding-unit header before anything is allocated, and raise the shared per-unit budget to match. Tests cover the 16384x16384 rejection (ported from the removed crate) and the 8K 444 + alpha admission. Refs storytold/craft-libs#778, storytold/craft-libs#781 --- Cargo.lock | 12 - Cargo.toml | 8 +- crates/codecs/Cargo.toml | 1 - crates/codecs/src/mp4.rs | 11 +- crates/codecs/src/mxf.rs | 4 +- crates/codecs/src/video.rs | 112 +- crates/dnx/Cargo.toml | 22 - crates/dnx/README.md | 141 -- crates/dnx/src/bits.rs | 127 -- crates/dnx/src/dct.rs | 160 -- crates/dnx/src/decode.rs | 560 ------- crates/dnx/src/encode.rs | 438 ------ crates/dnx/src/ffmpeg_probe.rs | 272 ---- crates/dnx/src/header.rs | 259 ---- crates/dnx/src/lib.rs | 235 --- crates/dnx/src/spec_tables.rs | 2115 -------------------------- crates/dnx/src/tables.rs | 301 ---- crates/dnx/tests/it/common/mod.rs | 303 ---- crates/dnx/tests/it/fuzz_hostile.rs | 183 --- crates/dnx/tests/it/main.rs | 6 - crates/dnx/tests/it/oracle_decode.rs | 83 - crates/dnx/tests/it/oracle_encode.rs | 76 - crates/dnx/tests/it/perf.rs | 59 - crates/dnx/tests/it/roundtrip.rs | 147 -- crates/dnx/tools/extract_tables.py | 133 -- crates/export/Cargo.toml | 1 - crates/export/src/encoders.rs | 14 +- docs/architecture.md | 2 +- 28 files changed, 124 insertions(+), 5661 deletions(-) delete mode 100644 crates/dnx/Cargo.toml delete mode 100644 crates/dnx/README.md delete mode 100644 crates/dnx/src/bits.rs delete mode 100644 crates/dnx/src/dct.rs delete mode 100644 crates/dnx/src/decode.rs delete mode 100644 crates/dnx/src/encode.rs delete mode 100644 crates/dnx/src/ffmpeg_probe.rs delete mode 100644 crates/dnx/src/header.rs delete mode 100644 crates/dnx/src/lib.rs delete mode 100644 crates/dnx/src/spec_tables.rs delete mode 100644 crates/dnx/src/tables.rs delete mode 100644 crates/dnx/tests/it/common/mod.rs delete mode 100644 crates/dnx/tests/it/fuzz_hostile.rs delete mode 100644 crates/dnx/tests/it/main.rs delete mode 100644 crates/dnx/tests/it/oracle_decode.rs delete mode 100644 crates/dnx/tests/it/oracle_encode.rs delete mode 100644 crates/dnx/tests/it/perf.rs delete mode 100644 crates/dnx/tests/it/roundtrip.rs delete mode 100644 crates/dnx/tools/extract_tables.py diff --git a/Cargo.lock b/Cargo.lock index f8036afe2..f592f8e61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2062,7 +2062,6 @@ dependencies = [ "craft-video", "filmcraft-bitstream", "filmcraft-color", - "filmcraft-dnx", "filmcraft-frame", "filmcraft-isobmff", "filmcraft-matroska", @@ -2098,16 +2097,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "filmcraft-dnx" -version = "0.20.1" -dependencies = [ - "filmcraft-bitstream", - "filmcraft-testkit", - "rayon", - "thiserror 2.0.21", -] - [[package]] name = "filmcraft-edit" version = "0.20.1" @@ -2169,7 +2158,6 @@ dependencies = [ "filmcraft-audio-dsp", "filmcraft-codecs", "filmcraft-color", - "filmcraft-dnx", "filmcraft-frame", "filmcraft-geom", "filmcraft-isobmff", diff --git a/Cargo.toml b/Cargo.toml index 5f2730c46..433c0788a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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-dnx = { path = "crates/dnx" } filmcraft-dng = { path = "crates/dng" } filmcraft-audio-dsp = { path = "crates/audio-dsp" } filmcraft-frame = { path = "crates/frame" } @@ -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, 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"] } +# The video codecs (including H.264 encode, APV encode, ProRes, DNxHD / DNxHR 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", "dnx", "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-dnx] -opt-level = 3 - [profile.dev.package.filmcraft-dng] opt-level = 3 diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml index cd13c8fbe..1649422d2 100644 --- a/crates/codecs/Cargo.toml +++ b/crates/codecs/Cargo.toml @@ -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-dnx = { workspace = true } craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "b577b219e96a1aa8cba5ed2966c6b32ea9cfc7b2" } rayon = { workspace = true } thiserror = { workspace = true } diff --git a/crates/codecs/src/mp4.rs b/crates/codecs/src/mp4.rs index 6c9daeea7..146e3265c 100644 --- a/crates/codecs/src/mp4.rs +++ b/crates/codecs/src/mp4.rs @@ -332,12 +332,13 @@ impl Mp4Source { if matches!(entry.codec, CodecConfig::Dnx { .. }) { // the sample entry doesn't say which VC-3 compression ID it is: 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_dnx::probe(&d).ok()) { - info.codec = filmcraft_dnx::cid_name(h.cid); + if let Some(h) = filmcraft_media::reader::read_range(&*bytes.0, s0.offset, s0.size as usize).ok().and_then(|d| craft_video::dnx::probe(&d).ok()) + { + info.codec = craft_video::dnx::cid_name(h.cid); let sub = match h.chroma { - filmcraft_dnx::ChromaFormat::Yuv420 => "YUV 4:2:0", - filmcraft_dnx::ChromaFormat::Yuv422 => "YUV 4:2:2", - filmcraft_dnx::ChromaFormat::Yuv444 => "4:4:4", + craft_video::dnx::ChromaFormat::Yuv420 => "YUV 4:2:0", + craft_video::dnx::ChromaFormat::Yuv422 => "YUV 4:2:2", + craft_video::dnx::ChromaFormat::Yuv444 => "4:4:4", }; info.pixel_format = format!("{sub} {}-bit", h.bit_depth); info.has_alpha = h.alpha; diff --git a/crates/codecs/src/mxf.rs b/crates/codecs/src/mxf.rs index 71cee6d8d..b2d62b229 100644 --- a/crates/codecs/src/mxf.rs +++ b/crates/codecs/src/mxf.rs @@ -293,8 +293,8 @@ impl MxfSource { Codec::Vc3 => file .read_sample(&bytes, vi, 0) .ok() - .and_then(|f| filmcraft_dnx::probe(&f).ok()) - .map(|h| filmcraft_dnx::cid_name(h.cid)) + .and_then(|f| craft_video::dnx::probe(&f).ok()) + .map(|h| craft_video::dnx::cid_name(h.cid)) .unwrap_or_else(|| "DNxHD/DNxHR".into()), Codec::ProRes { profile } => prores_name(profile).into(), c @ Codec::Avc { .. } => c.name().into(), diff --git a/crates/codecs/src/video.rs b/crates/codecs/src/video.rs index 3a9e1a8df..2dd20fd60 100644 --- a/crates/codecs/src/video.rs +++ b/crates/codecs/src/video.rs @@ -887,15 +887,15 @@ pub struct DnxDecoder; /// Convert a decoded VC-3 frame to a [`VideoFrame`]. RGB (4:4:4) frames are converted to /// BT.709 video-range Y'CbCr 4:4:4 at the coded depth. -pub fn dnx_to_video_frame(f: filmcraft_dnx::Frame) -> VideoFrame { +pub fn dnx_to_video_frame(f: craft_video::dnx::Frame) -> VideoFrame { use std::sync::Arc; let chroma = match f.chroma { - filmcraft_dnx::ChromaFormat::Yuv420 => filmcraft_frame::Chroma::C420, - filmcraft_dnx::ChromaFormat::Yuv422 => filmcraft_frame::Chroma::C422, - filmcraft_dnx::ChromaFormat::Yuv444 => filmcraft_frame::Chroma::C444, + craft_video::dnx::ChromaFormat::Yuv420 => filmcraft_frame::Chroma::C420, + craft_video::dnx::ChromaFormat::Yuv422 => filmcraft_frame::Chroma::C422, + craft_video::dnx::ChromaFormat::Yuv444 => filmcraft_frame::Chroma::C444, }; let mut color = filmcraft_color::ColorInfo::REC709; - if matches!(f.color_volume, filmcraft_dnx::ColorVolume::Bt2020Ncl | filmcraft_dnx::ColorVolume::Bt2020Cl) { + if matches!(f.color_volume, craft_video::dnx::ColorVolume::Bt2020Ncl | craft_video::dnx::ColorVolume::Bt2020Cl) { color.matrix = filmcraft_color::Matrix::Bt2020Ncl; color.primaries = filmcraft_color::Primaries::Bt2020; } @@ -935,7 +935,7 @@ pub fn dnx_to_video_frame(f: filmcraft_dnx::Frame) -> VideoFrame { impl VideoDecoder for DnxDecoder { fn decode(&mut self, sample: &[u8], pts: i64) -> Result> { - let f = filmcraft_dnx::decode_frame(sample).map_err(|e| CodecError::Decode(e.to_string()))?; + let f = decode_dnx_frame(sample).map_err(|e| CodecError::Decode(e.to_string()))?; Ok(vec![DecodedFrame { pts, frame: dnx_to_video_frame(f), draft: false }]) } fn flush(&mut self) -> Vec { @@ -1489,3 +1489,103 @@ mod prores_resource_tests { assert!(matches!(decode_prores_frame(&too_large), Err(craft_video::prores::Error::Invalid("frame dimensions exceed the decoder memory limit")))); } } + +/// FilmCraft's historical DNx ceiling: the decoded storage of a whole frame (the coded planes of +/// every coding unit plus the output picture) stays within 1 GiB. That covers 8K DNxHR 444 with +/// alpha (about 570 MB of 16-bit samples), which craft-video's default 256 MiB per-unit plane +/// budget would refuse. +const DNX_MAX_FRAME_BYTES: usize = 1 << 30; + +/// Decode one DNx frame with craft-video, keeping FilmCraft's 1 GiB whole-frame limit. +fn decode_dnx_frame(sample: &[u8]) -> craft_video::dnx::Result { + let h = craft_video::dnx::probe(sample)?; + check_dnx_frame_budget(&h)?; + craft_video::dnx::decode_frame_with(sample, &dnx_decode_options()) +} + +/// craft-video decode options with the per-unit plane budget raised to FilmCraft's frame ceiling +/// (the whole-frame check above is the binding limit). +fn dnx_decode_options() -> craft_video::dnx::DecodeOptions { + craft_video::dnx::DecodeOptions { max_decoded_plane_bytes: DNX_MAX_FRAME_BYTES, ..Default::default() } +} + +/// Reject a frame whose decoded storage would exceed [`DNX_MAX_FRAME_BYTES`] before anything is +/// allocated (the check the FilmCraft-local decoder ran on the first coding-unit header). +fn check_dnx_frame_budget(h: &craft_video::dnx::FrameHeader) -> craft_video::dnx::Result<()> { + use craft_video::dnx::{ChromaFormat, Error}; + const TOO_BIG: Error = Error::Invalid("frame dimensions exceed the decoder memory limit"); + let field_coded = h.interlaced && !h.frame_encoding; + let nw = h.mb_width() as usize; + let (cw, ch) = match h.chroma { + ChromaFormat::Yuv422 => (8, 16), + ChromaFormat::Yuv420 => (8, 8), + ChromaFormat::Yuv444 => (16, 16), + }; + let ns = h.mb_rows as usize; + let luma_rows = ns.checked_mul(16).ok_or(TOO_BIG)?; + let chroma_rows = ns.checked_mul(ch).ok_or(TOO_BIG)?; + let luma_plane = (nw * 16).checked_mul(luma_rows).ok_or(TOO_BIG)?; + let chroma_plane = (nw * cw).checked_mul(chroma_rows).ok_or(TOO_BIG)?; + let mut unit = chroma_plane.checked_mul(2).and_then(|n| n.checked_add(luma_plane)).ok_or(TOO_BIG)?; + if h.alpha { + unit = unit.checked_add(luma_plane).ok_or(TOO_BIG)?; + } + let units = if field_coded { 2 } else { 1 }; + let out_h = (h.lines as usize).checked_mul(units).ok_or(TOO_BIG)?; + let luma = (h.width as usize).checked_mul(out_h).ok_or(TOO_BIG)?; + let chroma = (h.chroma.chroma_width(h.width) as usize).checked_mul(h.chroma.chroma_height(out_h as u32) as usize).ok_or(TOO_BIG)?; + let out = chroma.checked_mul(2).and_then(|n| n.checked_add(luma)).and_then(|n| if h.alpha { n.checked_add(luma) } else { Some(n) }).ok_or(TOO_BIG)?; + let samples = unit.checked_mul(units).and_then(|n| n.checked_add(out)).ok_or(TOO_BIG)?; + if samples.checked_mul(std::mem::size_of::()).is_none_or(|n| n > DNX_MAX_FRAME_BYTES) { + return Err(TOO_BIG); + } + Ok(()) +} + +#[cfg(test)] +mod dnx_resource_tests { + use super::*; + use craft_video::dnx::{ChromaFormat, Encoder, Frame, FrameHeader, Profile, header}; + + /// A header-only frame (scan-line offsets increasing, no payload) claiming the given raster. + fn header_only(width: u32, lines: u32, alpha: bool) -> (FrameHeader, Vec) { + let mut enc = Encoder::new(Profile::R444, 64, 64).unwrap(); + let d = enc.encode(&Frame::new(64, 64, ChromaFormat::Yuv444, 10, false)).unwrap(); + let mut h = craft_video::dnx::probe(&d).unwrap(); + h.width = width; + h.lines = lines; + h.mb_rows = lines.div_ceil(16); + h.alpha = alpha; + h.header_size = header::header_size_for(h.lines); + let mut data = header::write(&h); + let hs = data.len(); + for s in 0..h.mb_rows as usize { + let o = header::SCAN_INDEX_OFFSET + 4 * s; + data[o..o + 4].copy_from_slice(&(s as u32).to_be_bytes()); + } + data.resize(hs + 4096, 0); + (h, data) + } + + #[test] + fn dnx_decoder_keeps_the_one_gibibyte_frame_limit() { + // Port of the FilmCraft-local `rejects_frames_over_the_memory_limit`: a 16384x16384 4:4:4 + // header with alpha is refused before any plane is allocated. + let (_, big) = header_only(16384, 16384, true); + assert!(craft_video::dnx::probe(&big).is_ok()); + let err = decode_dnx_frame(&big).unwrap_err().to_string(); + assert!(err.contains("memory limit"), "{err}"); + } + + #[test] + fn dnx_budget_admits_8k_444_with_alpha() { + // 8K DNxHR 444 with alpha is about 566 MB of decoded storage (one coding unit of four + // 16-bit planes is about 283 MB): inside FilmCraft's 1 GiB ceiling, but above + // craft-video's default per-unit budget, so the app must keep raising the shared limit. + let (h, _) = header_only(8192, 4320, true); + assert!(check_dnx_frame_budget(&h).is_ok()); + let unit_bytes = 8192 * 4320 * 4 * std::mem::size_of::(); + assert!(craft_video::dnx::DecodeOptions::default().max_decoded_plane_bytes < unit_bytes); + assert!(dnx_decode_options().max_decoded_plane_bytes >= unit_bytes); + } +} diff --git a/crates/dnx/Cargo.toml b/crates/dnx/Cargo.toml deleted file mode 100644 index 9b5d34db5..000000000 --- a/crates/dnx/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "filmcraft-dnx" -description = "Clean-room Avid DNxHD / DNxHR (SMPTE ST 2019-1 VC-3) 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 } diff --git a/crates/dnx/README.md b/crates/dnx/README.md deleted file mode 100644 index 0287c62c7..000000000 --- a/crates/dnx/README.md +++ /dev/null @@ -1,141 +0,0 @@ -# filmcraft-dnx - -Clean-room Avid DNxHD / DNxHR (VC-3) decoder and encoder in pure Rust, written from the public -**SMPTE ST 2019-1:2016** *VC-3 Picture Compression and Data Stream Format* and its -**Amendment 1:2023** (both free from `pub.smpte.org`). Layer L0: depends only on -`filmcraft-bitstream`, `thiserror` and (optional, feature `threads`, default on) `rayon`. Builds -for `wasm32-unknown-unknown`; no `unsafe`. - -The constant tables (Annex D quantisation weights, Annex E VLC tables) were extracted from the -standard's text by [`tools/extract_tables.py`](tools/extract_tables.py) into -`src/spec_tables.rs`; the script checks every table's size and that every VLC table is a -complete prefix code. No code was taken from FFmpeg or any other implementation; ffmpeg is used -only as an external fixture generator and test oracle. - -## Status - -| Area | Status | -|---|---| -| Header (§7.2): all fields, HVN 1–4, RI header growth above 1088 lines, time code | done | -| DNxHD (HD profile) CIDs 1235, 1237, 1238, 1241–1244, 1250–1253, 1258, 1259 | done, oracle-tested | -| CID 1256 (HD RGB 4:4:4) and 1260 (macroblock-adaptive field/frame, 1440×1080i) | done, not oracle-tested (ffmpeg can't encode them) | -| DNxHR (RI profile) 1270–1274: LB, SQ, HQ, HQX, 444; any raster 1…16384 | done, oracle-tested | -| 8-, 10-, 12-bit; 4:2:2, 4:4:4; Y′CbCr and RGB (per-macroblock ACF) | done (12-bit: our encoder's streams, compared with ffmpeg's decode) | -| 4:2:0 (RI only) | done, not oracle-tested (ffmpeg can't encode it) | -| Interlaced: field-coded (two coding units) and frame-coded MACF | done | -| Alpha: DCT-coded and lossless differential RLE (Amendment 1 for HD CIDs) | done, not oracle-tested (no encoder produces it) | -| CBR padding, VBR, scan-line indices, EOF signature / CRC flag | done | -| Encoder: DNxHR LB, SQ, HQ (8-bit), HQX (10/12-bit), 444 (10/12-bit); CBR | done | -| Encoder: DNxHD (HD CIDs), interlaced, alpha, VBR | not yet | - -## API - -```rust -use filmcraft_dnx::*; - -let frame: Frame = decode_frame(&sample)?; // one or two coding units -let f = decode_frame_with(&sample, &DecodeOptions { threads: false, ..Default::default() })?; -let hdr: FrameHeader = probe(&sample)?; // cid, raster, depth, chroma… -let name = cid_name(hdr.cid); // "DNxHR HQX", "DNxHD 1080i 8-bit (CID 1242)" - -let mut enc = Encoder::new(Profile::Hq, 1920, 1080)?; // or Encoder::with_config(EncoderConfig { .. }) -let sample: Vec = enc.encode(&frame)?; // exactly enc.frame_size() bytes (CBR) -``` - -`Frame { width, height, chroma, rgb, bit_depth, y, cb, cr: Vec, alpha, interlace, -color_volume, cid, par }` holds planar samples without row padding. Interlaced frames are woven -(field 1 = top field on even lines). RGB frames hold G, B, R in `y`, `cb`, `cr`. In MOV the -sample entry is `AVdn` (DNxHD) or `AVdh` (DNxHR); `filmcraft-codecs` reads the CID from the -first frame header to label the stream. - -## Decoder - -- Every coding unit's scan-line indices give each macroblock row's byte offset, so rows decode - 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 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. -- Robustness: every offset and size is validated, run lengths past coefficient 63 are rejected, - and corrupted or truncated streams return `Err` without panicking (`tests/it/roundtrip.rs`). - -### Where we follow ffmpeg rather than the letter of the standard - -ffmpeg's decoder is the reference everyone interoperates with (and the oracle of our tests). In -three places it differs from the text of ST 2019-1:2016; black-box probes (`src/ffmpeg_probe.rs`: -streams with a single coefficient, decoded by ffmpeg, measured with a forward DCT) pinned down -its behaviour, and we match it by default: - -1. **Reconstruction offset.** Equation 8.1 reconstructs a quantised amplitude `A` at - ≈ `(A + ½)·W·qsf/p`. ffmpeg reconstructs at `(A + ¾)·W·qsf/p` (fitted intercept 0.750 ± 0.01 - steps for every CID family and step size). The decoder does the same; - `DecodeOptions::spec_dequant` selects equation 8.1 instead. The encoder picks the amplitude - nearest to the `(A + ¾)` reconstruction. -2. **CID 1271 (DNxHR HQX) weights.** Table C.2 assigns Table D.1 to CID 1271. ffmpeg's decoder and - encoder use Table D.4 (the CID 1241 weights) — fitted RMS error 0.65 against D.4, > 2.6 - against every other table. We use D.4. -3. **4:4:4 signalling.** ffmpeg writes RGB DNxHR 444 with `CLF = 0`, and Y′CbCr 444 with `CLF = 1` - plus `ACF = 1` on every macroblock; its channel order is G, B, R (Ch1 = G). The decoder treats - every 4:4:4 stream as RGB except macroblocks flagged `ACF = 1` (a stream that is entirely - ACF = 1 is returned as Y′CbCr; mixed streams are converted to RGB macroblock by macroblock as - §8.1.1 describes), and the encoder writes the same signalling. - -Also noted: the RLE alpha pseudo code (Figure 50) tests the header bit the opposite way from -the prose of §7.3.1.2; we follow the prose (normative text takes precedence). - -## Encoder - -- DNxHR LB/SQ/HQ (8-bit 4:2:2), HQX (10- or 12-bit 4:2:2) and 444 (10- or 12-bit 4:4:4, - Y′CbCr or RGB), progressive, CBR at exactly the equation 7.1 frame size (e.g. 917 504 bytes - for 1080p HQ, 606 208 for SQ). Input at any depth 8–16 is rescaled to the coded depth. -- Every macroblock is transformed once (orthonormal FDCT) and stored pre-divided by its weights. - Rate control binary-searches the smallest uniform `qsf` whose frame fits the payload - (including the 4-byte scan-line alignment), starting from the previous frame's value, then - gives macroblocks one finer step, spread evenly over the frame, while the budget allows. -- `export::Format::DnxHr` (`file.exportMedia {"format":"dnxhr","dnxProfile":"lb|sq|hq|hqx"}`) - writes QuickTime `AVdh` + PCM. The Avid-specific `ACLR`/`ADHR` sample-entry atoms are not - written (ffmpeg and our reader don't need them). - -## Accuracy (vs ffmpeg as an external oracle) - -`tests/it/oracle_decode.rs` decodes 22 fixtures made by ffmpeg's VC-3 encoder: CIDs 1235, 1237, -1238, 1241, 1242, 1243, 1244, 1250, 1251, 1252, 1253, 1258, 1259 and DNxHR LB/SQ/HQ/HQX/444 -(Y′CbCr and RGB), with testsrc2, mandelbrot and heavy noise, at 1920×1080, 1280×720, 960×720, -1440×1080 (thin raster, interlaced), 1000×562, 640×360, 256×120 and 3840×2160. Compared with -ffmpeg's decode at the native depth, **every fixture is within ±2 LSB, with at most ~50 samples -per million at ±2** (typically 1–30 per fixture); 87–99% of samples are bit-identical and the -mean absolute error is 0.01–0.13 LSB. The remaining differences are ffmpeg's integer IDCT; this -decoder evaluates the exact IDCT of equation 8.3, so decode is **not bit-exact, but within IDCT -precision** of ffmpeg. (ffmpeg wraps rather than clips 10-bit reconstructions that overflow -1023, so the noise fixtures keep their samples inside 24…231 before encoding.) - -`tests/it/oracle_encode.rs` encodes 1920×1080 frames with every profile and writes them into a -minimal test-only MOV. ffmpeg decodes them with `-xerror`, without errors, and agrees with our -decoder within ±2 (90–99% identical). Luma PSNR against the source: HQ 58.8 dB (mandelbrot) and -66.3 dB (testsrc2), SQ 57.5 dB, LB 56.1 dB, HQX 10-bit 67.5 dB, HQX 12-bit 80.2 dB, 444 10-bit -79.7 dB — all above the 45 dB HQ target. - -## Performance - -`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 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: - -| Frame (busy synthetic content) | Decode, threaded | Decode, 1 thread | Encode | -|---|---|---|---| -| DNxHR HQ 1080p (896 KB) | 41 fps | 13.2 fps | 4.1 fps | -| DNxHR HQX 10-bit 1080p (896 KB) | 39 fps | 8.9 fps | 3.6 fps | -| DNxHR SQ 2160p (2352 KB) | 21 fps | 15.6 fps | 1.1 fps | -| DNxHR HQX 10-bit 2160p (3556 KB) | 16 fps | 12.2 fps | 1.1 fps | - -## Gaps - -- Decode is within IDCT precision of ffmpeg, not bit-exact (see above). -- Encoder: no DNxHD (HD CIDs), interlaced, alpha or VBR output; no RDO/trellis. -- CIDs 1256/1260 and alpha decoding are implemented from the standard but have no oracle - fixtures, because no available encoder produces them. -- No MXF; DNx in MOV only (via `filmcraft-isobmff` / `filmcraft-codecs`). diff --git a/crates/dnx/src/bits.rs b/crates/dnx/src/bits.rs deleted file mode 100644 index 9c3ae8fdf..000000000 --- a/crates/dnx/src/bits.rs +++ /dev/null @@ -1,127 +0,0 @@ -//! Fast MSB-first bit reader (64-bit cache) and writer for the macroblock data. - -use crate::tables::{LINK, Lut}; - -/// Bit reader over one macroblock scan line. Reads past the end yield zero bits; callers check -/// [`Reader::overrun`]. -pub(crate) struct Reader<'a> { - data: &'a [u8], - next: usize, - 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) { - self.cache <<= n; - self.avail -= n; - } - - #[inline] - pub fn consumed(&self) -> usize { - self.next * 8 - self.avail as usize - } - - #[inline] - pub fn overrun(&self) -> bool { - self.consumed() > self.data.len() * 8 - } - - /// Read `n` ≤ 32 bits. - #[inline(always)] - pub fn read(&mut self, n: u32) -> u32 { - if n == 0 { - return 0; - } - self.refill(); - let v = (self.cache >> (64 - n)) as u32; - self.consume(n); - v - } - - /// Decode one VLC symbol (value bits of the LUT entry). - #[inline(always)] - pub fn vlc(&mut self, t: &Lut) -> u32 { - self.refill(); - let w = self.cache; - let mut e = t.primary[(w >> (64 - t.bits)) as usize]; - if e & LINK != 0 { - let sb = (e >> 24) & 0x7f; - let off = (e & 0xff_ffff) as usize; - e = t.secondary[off + ((w << t.bits) >> (64 - sb)) as usize]; - } - // entries are always filled for complete prefix codes - self.consume(e >> 16); - e & 0xffff - } -} - -/// MSB-first bit writer. -#[derive(Default)] -pub(crate) struct Writer { - pub buf: Vec, - acc: u64, - n: u32, -} - -impl Writer { - pub fn new() -> Self { - Self::default() - } - - #[inline(always)] - pub fn put(&mut self, val: u32, bits: u32) { - 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; - } - - /// Pad with zero bits to a multiple of `align` bytes and return the bytes. - pub fn finish_aligned(mut self, align: usize) -> Vec { - if self.n > 0 { - let pad = 8 - self.n; - self.put(0, pad); - } - while !self.buf.len().is_multiple_of(align) { - self.buf.push(0); - } - self.buf - } -} diff --git a/crates/dnx/src/dct.rs b/crates/dnx/src/dct.rs deleted file mode 100644 index 53248290e..000000000 --- a/crates/dnx/src/dct.rs +++ /dev/null @@ -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 VC-3 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); - } - } -} diff --git a/crates/dnx/src/decode.rs b/crates/dnx/src/decode.rs deleted file mode 100644 index 3233b59df..000000000 --- a/crates/dnx/src/decode.rs +++ /dev/null @@ -1,560 +0,0 @@ -//! VC-3 decoding (SMPTE ST 2019-1 §8). - -use crate::bits::Reader; -use crate::dct::idct8x8; -use crate::header::{self, FieldCode, FrameHeader, SCAN_INDEX_OFFSET}; -use crate::tables::{AMP_EOB, AMP_FINDEX, AMP_FRUN, CidInfo, VlcSet, ZIGZAG, cid_info, vlc}; -use crate::{ChromaFormat, Error, Frame, Interlace, Result}; - -/// Maximum aggregate decoded storage for one frame: the coded planes of every coding unit plus -/// the output picture. 1 GiB covers 8K DNxHR 444 with alpha (about 570 MB of 16-bit samples). -const MAX_FRAME_BYTES: usize = 1 << 30; - -/// Decoder options. -#[derive(Debug, Clone, Copy)] -pub struct DecodeOptions { - /// Decode macroblock scan lines in parallel (needs the `threads` feature). - pub threads: bool, - /// Use the standard's integer inverse quantisation (equation 8.1) instead of the - /// ffmpeg-compatible reconstruction (default false; see the README). - pub spec_dequant: bool, -} - -impl Default for DecodeOptions { - fn default() -> Self { - DecodeOptions { threads: true, spec_dequant: false } - } -} - -/// Parse the header of the first coding unit of a frame. -pub fn probe(data: &[u8]) -> Result { - header::parse(data) -} - -/// Decode one compressed frame (one or two coding units). -pub fn decode_frame(data: &[u8]) -> Result { - decode_frame_with(data, &DecodeOptions::default()) -} - -/// Decode one compressed frame with options. -pub fn decode_frame_with(data: &[u8], opts: &DecodeOptions) -> Result { - let h1 = header::parse(data)?; - let field_coded = h1.interlaced && !h1.frame_encoding; - check_memory_budget(&h1, field_coded)?; - if !field_coded { - let planes = decode_unit(&h1, data, opts)?; - let mut f = Frame::new_like(&h1, h1.width, h1.lines); - f.interlace = if h1.interlaced { Interlace::TopFieldFirst } else { Interlace::Progressive }; - planes.copy_into(&mut f, 0, 1); - return Ok(f); - } - // field encoding: two coding units, field 1 (top) then field 2 - let off = second_unit_offset(&h1, data)?; - let h2 = header::parse(&data[off..])?; - if h2.cid != h1.cid || h2.width != h1.width || h2.lines != h1.lines || h2.chroma != h1.chroma || h2.alpha != h1.alpha { - return Err(Error::Invalid("field coding units differ")); - } - let (p1, p2) = if opts.threads { - join(|| decode_unit(&h1, &data[..off], opts), || decode_unit(&h2, &data[off..], opts)) - } else { - (decode_unit(&h1, &data[..off], opts), decode_unit(&h2, &data[off..], opts)) - }; - let (top, bottom) = if h1.field == FieldCode::Field2 { (p2?, p1?) } else { (p1?, p2?) }; - // Both field payloads must be valid before committing the full-frame - // allocation; a damaged header alone must not allocate an output picture. - let mut f = Frame::new_like(&h1, h1.width, h1.lines * 2); - f.interlace = Interlace::TopFieldFirst; - top.copy_into(&mut f, 0, 2); - bottom.copy_into(&mut f, 1, 2); - Ok(f) -} - -/// Coded-plane geometry of one coding unit: plane strides and chroma macroblock height. -fn coded_layout(h: &FrameHeader) -> ([usize; 4], usize) { - let nw = h.mb_width() as usize; - let cmb = match h.chroma { - ChromaFormat::Yuv422 => (8, 16), - ChromaFormat::Yuv420 => (8, 8), - ChromaFormat::Yuv444 => (16, 16), - }; - ([nw * 16, nw * cmb.0, nw * cmb.0, nw * 16], cmb.1) -} - -/// Reject a frame whose decoded storage (coded planes of each coding unit plus the output -/// picture) would exceed [`MAX_FRAME_BYTES`], before anything is allocated. -fn check_memory_budget(h: &FrameHeader, field_coded: bool) -> Result<()> { - const TOO_BIG: Error = Error::Invalid("frame dimensions exceed the decoder memory limit"); - let ns = h.mb_rows as usize; - let (strides, ch) = coded_layout(h); - let luma_rows = ns.checked_mul(16).ok_or(TOO_BIG)?; - let chroma_rows = ns.checked_mul(ch).ok_or(TOO_BIG)?; - let mut unit = strides[0].checked_mul(luma_rows).ok_or(TOO_BIG)?; - for s in [strides[1], strides[2]] { - unit = s.checked_mul(chroma_rows).and_then(|n| unit.checked_add(n)).ok_or(TOO_BIG)?; - } - if h.alpha { - unit = strides[3].checked_mul(luma_rows).and_then(|n| unit.checked_add(n)).ok_or(TOO_BIG)?; - } - let units = if field_coded { 2 } else { 1 }; - let out_h = (h.lines as usize).checked_mul(units).ok_or(TOO_BIG)?; - let luma = (h.width as usize).checked_mul(out_h).ok_or(TOO_BIG)?; - let chroma = (h.chroma.chroma_width(h.width) as usize).checked_mul(h.chroma.chroma_height(out_h as u32) as usize).ok_or(TOO_BIG)?; - let out = chroma.checked_mul(2).and_then(|n| n.checked_add(luma)).and_then(|n| if h.alpha { n.checked_add(luma) } else { Some(n) }).ok_or(TOO_BIG)?; - let samples = unit.checked_mul(units).and_then(|n| n.checked_add(out)).ok_or(TOO_BIG)?; - if samples.checked_mul(std::mem::size_of::()).is_none_or(|n| n > MAX_FRAME_BYTES) { - return Err(TOO_BIG); - } - Ok(()) -} - -#[cfg(feature = "threads")] -fn join(a: impl FnOnce() -> A + Send, b: impl FnOnce() -> B + Send) -> (A, B) { - rayon::join(a, b) -} - -#[cfg(not(feature = "threads"))] -fn join(a: impl FnOnce() -> A, b: impl FnOnce() -> B) -> (A, B) { - (a(), b()) -} - -/// Byte offset of the second coding unit of a field-coded frame. -fn second_unit_offset(h: &FrameHeader, data: &[u8]) -> Result { - if let Some(sz) = h.coding_unit_size() { - let sz = sz as usize; - if data.len() >= sz + 0x280 && data[sz..sz + 4] == data[0..4] && data[sz + 4] == data[4] { - return Ok(sz); - } - } - // VBR (or unexpected size): find the next header prefix after the first unit's scan lines - let prefix = &data[0..5]; - let start = h.header_size as usize; - data[start..] - .windows(5) - .position(|w| w == prefix) - .map(|p| p + start) - .filter(|&p| header::parse(&data[p..]).is_ok()) - .ok_or(Error::Invalid("second field coding unit not found")) -} - -/// Decoded planes of one coding unit at the coded (macroblock-aligned) size. -struct CodedPlanes { - /// Y / Cb / Cr / A (or R / G / B / A). - planes: [Vec; 4], - strides: [usize; 4], - /// An RGB-signalled stream whose macroblocks are all coded as Y'CbCr. - ycbcr: bool, -} - -impl CodedPlanes { - /// Crop into `f`, writing coded line `i` to frame line `first + i·step`. - fn copy_into(&self, f: &mut Frame, first: usize, step: usize) { - if self.ycbcr { - f.rgb = false; - } - let cw = f.chroma_width() as usize; - let w = f.width as usize; - let ch = f.chroma_height() as usize; - let h = f.height as usize; - let cfirst = first; - let mut targets: Vec<(&mut Vec, usize, usize, usize, usize)> = - vec![(&mut f.y, w, h, first, 0), (&mut f.cb, cw, ch, cfirst, 1), (&mut f.cr, cw, ch, cfirst, 2)]; - if let Some(a) = f.alpha.as_mut() { - targets.push((a, w, h, first, 3)); - } - for (dst, pw, ph, first, pi) in targets { - let src = &self.planes[pi]; - let ss = self.strides[pi]; - let mut line = 0; - let mut y = first; - while y < ph { - let s = &src[line * ss..line * ss + pw]; - dst[y * pw..y * pw + pw].copy_from_slice(s); - line += 1; - y += step; - } - } - } -} - -/// Block layout entry: plane, x offset and y offset (0 or 8) inside the macroblock. -type BlockPos = (u8, u8, u8); - -const LAYOUT_422: [BlockPos; 8] = [(0, 0, 0), (0, 8, 0), (1, 0, 0), (2, 0, 0), (0, 0, 8), (0, 8, 8), (1, 0, 8), (2, 0, 8)]; -const LAYOUT_420: [BlockPos; 6] = [(0, 0, 0), (0, 8, 0), (1, 0, 0), (2, 0, 0), (0, 0, 8), (0, 8, 8)]; -const LAYOUT_444: [BlockPos; 12] = - [(0, 0, 0), (0, 8, 0), (1, 0, 0), (1, 8, 0), (2, 0, 0), (2, 8, 0), (0, 0, 8), (0, 8, 8), (1, 0, 8), (1, 8, 8), (2, 0, 8), (2, 8, 8)]; -const LAYOUT_ALPHA: [BlockPos; 4] = [(3, 0, 0), (3, 8, 0), (3, 0, 8), (3, 8, 8)]; - -/// Per-coding-unit decoding context. -struct Ctx<'a> { - h: &'a FrameHeader, - info: &'static CidInfo, - vlc: &'static VlcSet, - nw: usize, - layout: &'static [BlockPos], - /// Plane macroblock sizes (width, height) for Y/Cb/Cr/A. - mb_size: [(usize, usize); 4], - strides: [usize; 4], - p_bits: u32, - p_shift: u32, - p_half: u32, - spec_dequant: bool, - /// `1 / (4p)` - inv_4p: f32, - /// IDCT output scale (4 at 12 bits, see README) and level offset. - scale: f32, - mid: f32, - max: f32, -} - -fn decode_unit(h: &FrameHeader, data: &[u8], opts: &DecodeOptions) -> Result { - let info = cid_info(h.cid).ok_or(Error::UnknownCid(h.cid))?; - let nw = h.mb_width() as usize; - let ns = h.mb_rows as usize; - let (layout, cmb): (&'static [BlockPos], (usize, usize)) = match h.chroma { - ChromaFormat::Yuv422 => (&LAYOUT_422, (8, 16)), - ChromaFormat::Yuv420 => (&LAYOUT_420, (8, 8)), - ChromaFormat::Yuv444 => (&LAYOUT_444, (16, 16)), - }; - let mb_size = [(16, 16), cmb, cmb, (16, 16)]; - let (strides, _) = coded_layout(h); - let ctx = Ctx { - h, - info, - vlc: vlc(info.vlc), - nw, - layout, - mb_size, - strides, - p_bits: if h.bit_depth == 8 { 4 } else { 6 }, - p_shift: info.p.trailing_zeros(), - p_half: info.p / 2, - spec_dequant: opts.spec_dequant, - inv_4p: 1.0 / (4 * info.p) as f32, - scale: if h.bit_depth == 12 { 4.0 } else { 1.0 }, - mid: (1u32 << (h.bit_depth - 1)) as f32, - max: ((1u32 << h.bit_depth) - 1) as f32, - }; - // scan line byte ranges - let hs = h.header_size as usize; - let mut starts = Vec::with_capacity(ns); - for s in 0..ns { - let o = SCAN_INDEX_OFFSET + 4 * s; - let idx = u32::from_be_bytes([data[o], data[o + 1], data[o + 2], data[o + 3]]) as usize; - let st = hs.checked_add(idx).filter(|&v| v < data.len()).ok_or(Error::Truncated)?; - starts.push(st); - } - // Each scan line owns its own, non-empty byte range: repeated or decreasing indices would let - // one coded row stand in for many, so a tiny payload could fill a huge raster. - if starts.windows(2).any(|w| w[1] <= w[0]) { - return Err(Error::Invalid("macroblock scan line indices are not increasing")); - } - let ranges: Vec<(usize, usize)> = (0..ns) - .map(|s| { - let st = starts[s]; - let end = starts.get(s + 1).copied().unwrap_or(data.len()); - (st, end) - }) - .collect(); - - let alpha = h.alpha; - let mut planes: [Vec; 4] = [ - vec![0; strides[0] * 16 * ns], - vec![0; strides[1] * cmb.1 * ns], - vec![0; strides[2] * cmb.1 * ns], - if alpha { vec![0; strides[3] * 16 * ns] } else { Vec::new() }, - ]; - let [py, pcb, pcr, pa] = &mut planes; - let band = [strides[0] * 16, strides[1] * cmb.1, strides[2] * cmb.1, strides[3] * 16]; - let mut alpha_bands: Vec<&mut [u16]> = if alpha { pa.chunks_mut(band[3]).collect() } else { (0..ns).map(|_| &mut [][..]).collect() }; - let rows: Vec<(usize, &mut [u16], &mut [u16], &mut [u16], &mut [u16])> = py - .chunks_mut(band[0]) - .zip(pcb.chunks_mut(band[1])) - .zip(pcr.chunks_mut(band[2])) - .zip(alpha_bands.drain(..)) - .enumerate() - .map(|(i, (((y, cb), cr), a))| (i, y, cb, cr, a)) - .collect(); - let run = |(i, y, cb, cr, a): (usize, &mut [u16], &mut [u16], &mut [u16], &mut [u16])| -> Result> { - let (st, end) = ranges[i]; - decode_row(&ctx, &data[st..end], [y, cb, cr, a]).map_err(|e| match e { - Error::Row(_) => Error::Row(i), - e => e, - }) - }; - #[cfg(feature = "threads")] - let acf: Vec> = if opts.threads { - use rayon::prelude::*; - rows.into_par_iter().map(run).collect::>()? - } else { - rows.into_iter().map(run).collect::>()? - }; - #[cfg(not(feature = "threads"))] - let acf: Vec> = { - let _ = opts; - rows.into_iter().map(run).collect::>()? - }; - // RGB streams (CLF = 1) choose RGB or Y'CbCr per macroblock (ACF). A stream whose - // macroblocks are all Y'CbCr is returned as Y'CbCr 4:4:4; mixed streams are converted to RGB - // macroblock by macroblock (ConvertMacroblockColor, 8.1.1). - let n_acf: usize = acf.iter().map(|r| r.iter().filter(|&&a| a).count()).sum(); - let ycbcr = h.rgb_planes() && n_acf == nw * ns; - if h.rgb_planes() && n_acf > 0 && !ycbcr && h.color_volume != crate::ColorVolume::OutOfBand { - let [py, pcb, pcr, _] = &mut planes; - for (s, row) in acf.iter().enumerate() { - for (mb, &a) in row.iter().enumerate() { - if a { - let o = s * band[0]; - convert_mb_to_rgb(&ctx, [&mut py[o..o + band[0]], &mut pcb[o..o + band[0]], &mut pcr[o..o + band[0]]], mb); - } - } - } - } - Ok(CodedPlanes { planes, strides, ycbcr }) -} - -/// Decode one macroblock scan line into the 16-line bands of each plane. -fn decode_row(ctx: &Ctx, data: &[u8], bands: [&mut [u16]; 4]) -> Result> { - let h = ctx.h; - let mut r = Reader::new(data); - // DC predictors: Y/Ch1, Cb, Cr, R, G, B, A - let mut pred = [0i32; 7]; - let rgb = h.rgb_planes(); - let mff_cid = ctx.info.cid == 1260; - let mut block = [0f32; 64]; - let mut acfs = Vec::new(); - for mb in 0..ctx.nw { - let (qsf, flag, mff) = if mff_cid { - let mff = r.read(1) == 1; - (r.read(10), r.read(1) == 1, mff) - } else { - (r.read(11), r.read(1) == 1, false) - }; - let acf = rgb && flag; - let wq = scaled_weights(ctx.info.weights, qsf); - for &(plane, xo, yo) in ctx.layout { - let comp = plane as usize; - let slot = if rgb && !acf { 3 + comp } else { comp }; - let w = &wq[(comp != 0) as usize]; - decode_block(ctx, &mut r, &mut pred[slot], w, &mut block)?; - store(ctx, bands[comp], BlockPosition { plane: comp, macroblock: mb, x_offset: xo as usize, y_offset: yo as usize, field: mff }, &mut block); - } - if rgb { - acfs.push(acf); - } - if r.overrun() { - return Err(Error::Row(0)); - } - } - if h.alpha { - if h.lossless_alpha { - for mb in 0..ctx.nw { - decode_rle_alpha(ctx, &mut r, bands[3], mb)?; - } - } else { - for mb in 0..ctx.nw { - let qsf = if mff_cid { - r.read(1); - let q = r.read(10); - r.read(1); - q - } else { - let q = r.read(11); - r.read(1); - q - }; - let wq = scaled_weights(ctx.info.weights, qsf); - for &(plane, xo, yo) in &LAYOUT_ALPHA { - decode_block(ctx, &mut r, &mut pred[6], &wq[0], &mut block)?; - store( - ctx, - bands[3], - BlockPosition { plane: plane as usize, macroblock: mb, x_offset: xo as usize, y_offset: yo as usize, field: false }, - &mut block, - ); - } - } - } - if r.overrun() { - return Err(Error::Row(0)); - } - } - Ok(acfs) -} - -/// `W(u,v)·qsf` for every AC raster position (index 0 unused), luma and chroma. -#[inline] -fn scaled_weights(w: &[[u8; 63]; 2], qsf: u32) -> [[(u32, u32); 64]; 2] { - let mut out = [[(0u32, 0u32); 64]; 2]; - for c in 0..2 { - for i in 0..63 { - let wi = w[c][i] as u32; - out[c][i + 1] = (wi * qsf, wi); - } - } - out -} - -/// Decode one DCT block into dequantised coefficients (raster order). -#[inline] -fn decode_block(ctx: &Ctx, r: &mut Reader, pred: &mut i32, wq: &[(u32, u32); 64], block: &mut [f32; 64]) -> Result<()> { - let v = ctx.vlc; - *block = [0.0; 64]; - // DC (8.2.4) - let eta = r.vlc(&v.dc); - let eps = if eta == 0 { - 0 - } else { - let rho = r.read(eta) as i32; - if rho >= 1 << (eta - 1) { rho } else { rho + 1 - (1 << eta) } - }; - let dc = pred.wrapping_add(eps); - *pred = dc; - block[0] = dc as f32; - // AC (8.2.5) - let mut pos = 0usize; - loop { - let s = r.vlc(&v.amp); - if s & AMP_EOB != 0 { - break; - } - let neg = r.read(1) == 1; - let mut a = s & 0xff; - if s & AMP_FINDEX != 0 { - a += r.read(ctx.p_bits) * 64; - } - pos += 1; - if s & AMP_FRUN != 0 { - pos += r.vlc(&v.run) as usize; - } - if pos > 63 { - return Err(Error::Row(0)); - } - let z = ZIGZAG[pos] as usize; - let (wqs, w) = wq[z]; - // inverse quantisation (8.2.7) - // Reconstruction at (|X̂| + 3/4)·W·qsf/p, as ffmpeg's decoder does (see README); the - // standard's equation 8.1 rounds to an integer near (|X̂| + 1/2)·W·qsf/p. - let mag = if ctx.spec_dequant { - let add = if w != ctx.info.p { ctx.p_half } else { 0 }; - ((a as u64 * wqs as u64 + (wqs >> 1) as u64 + add as u64) >> ctx.p_shift) as f32 - } else { - ((4 * a as u64 + 3) * wqs as u64) as f32 * ctx.inv_4p - }; - block[z] = if neg { -mag } else { mag }; - } - Ok(()) -} - -/// IDCT a block and store it (with level adjustment and clipping) into a plane band. -struct BlockPosition { - plane: usize, - macroblock: usize, - x_offset: usize, - y_offset: usize, - field: bool, -} - -#[inline] -fn store(ctx: &Ctx, band: &mut [u16], position: BlockPosition, block: &mut [f32; 64]) { - idct8x8(block); - let BlockPosition { plane, macroblock, x_offset, y_offset, field } = position; - let stride = ctx.strides[plane]; - let x0 = macroblock * ctx.mb_size[plane].0 + x_offset; - let (first, step) = if field { (y_offset / 8, 2) } else { (y_offset, 1) }; - let (scale, mid, max) = (ctx.scale, ctx.mid + 0.5, ctx.max); - for j in 0..8 { - let row = &mut band[(first + j * step) * stride + x0..][..8]; - let src = &block[j * 8..j * 8 + 8]; - for i in 0..8 { - row[i] = (src[i] * scale + mid).floor().clamp(0.0, max) as u16; - } - } -} - -/// `ConvertMacroblockColor` (8.1.1): BT.709 Y′CbCr → R′G′B′ on the quantised (video-range) -/// signals, in place over the macroblock's three planes. -fn convert_mb_to_rgb(ctx: &Ctx, bands: [&mut [u16]; 3], mb: usize) { - let stride = ctx.strides[0]; - let s = (1u32 << (ctx.h.bit_depth - 8)) as f32; - let (kr, kb) = match ctx.h.color_volume { - crate::ColorVolume::Bt709 => (0.2126f32, 0.0722f32), - _ => (0.2627, 0.0593), - }; - let kg = 1.0 - kr - kb; - let c = 219.0 / 224.0; - for y in 0..16 { - for x in 0..16 { - let i = y * stride + mb * 16 + x; - let yy = bands[0][i] as f32; - let cb = bands[1][i] as f32 - 128.0 * s; - let cr = bands[2][i] as f32 - 128.0 * s; - let r = yy + 2.0 * (1.0 - kr) * c * cr; - let b = yy + 2.0 * (1.0 - kb) * c * cb; - let g = (yy - kr * r - kb * b) / kg; - // RGB planes are stored in coded channel order G, B, R - bands[0][i] = (g + 0.5).floor().clamp(0.0, ctx.max) as u16; - bands[1][i] = (b + 0.5).floor().clamp(0.0, ctx.max) as u16; - bands[2][i] = (r + 0.5).floor().clamp(0.0, ctx.max) as u16; - } - } -} - -/// Lossless (differential RLE) alpha macroblock (§7.3.1.2, §8.3). -fn decode_rle_alpha(ctx: &Ctx, r: &mut Reader, band: &mut [u16], mb: usize) -> Result<()> { - let d = ctx.h.bit_depth as u32; - let mask = (1u32 << d) - 1; - let stride = ctx.strides[3]; - let pos = |count: usize| { - let y = count / 16; - let x = if y.is_multiple_of(2) { count % 16 } else { 15 - count % 16 }; - y * stride + mb * 16 + x - }; - // RL = 1: RLE coded; RL = 0: 256 raw samples (§7.3.1.2 prose; see README) - let rl = r.read(4) & 1; - if rl == 0 { - for count in 0..256 { - band[pos(count)] = r.read(d) as u16; - } - return Ok(()); - } - let rice = d / 2 - 2; - let (mut d1, mut d2) = (mask >> 1, mask >> 1); - let mut count = 0usize; - while count < 256 { - let p = r.read(1); - // Elias gamma - let mut n = 0; - while r.read(1) == 0 { - n += 1; - if n > 8 || r.overrun() { - return Err(Error::Row(0)); - } - } - let nrl = (1usize << n) + r.read(n) as usize; - let dcw = if r.read(1) == 1 { - let mut q = 0u32; - while r.read(1) == 1 { - q += 1; - if q > 1 << d || r.overrun() { - return Err(Error::Row(0)); - } - } - (q << rice) + r.read(rice) - } else { - r.read(d) - }; - let diff = if dcw & 1 == 1 { mask.wrapping_sub(dcw >> 1) } else { dcw >> 1 }; - if count + nrl > 256 { - return Err(Error::Row(0)); - } - for _ in 0..nrl { - let v = if p == 1 { (2 * d1).wrapping_sub(d2).wrapping_add(diff) & mask } else { d1.wrapping_add(diff) & mask }; - band[pos(count)] = v as u16; - d2 = d1; - d1 = v; - count += 1; - } - } - Ok(()) -} diff --git a/crates/dnx/src/encode.rs b/crates/dnx/src/encode.rs deleted file mode 100644 index 914191f1a..000000000 --- a/crates/dnx/src/encode.rs +++ /dev/null @@ -1,438 +0,0 @@ -//! DNxHR encoder (RI profile, CBR, progressive). -//! -//! Every macroblock is transformed once (orthonormal 8×8 FDCT). Quantisation follows equation -//! 8.2 exactly (`X̂ = ⌊p·|X| / (qsf·W)⌋`, which the decoder reconstructs at the interval -//! midpoint). Rate control: a binary search finds the smallest uniform quantisation scale factor -//! whose frame fits the CBR payload, then macroblocks get one finer step in scan order while the -//! budget allows. - -use crate::bits::Writer; -use crate::dct::fdct8x8; -use crate::header::{self, EOF_SIGNATURE, FieldCode, FrameHeader, SCAN_INDEX_OFFSET}; -use crate::tables::{CidInfo, VlcSet, ZIGZAG, cid_info, vlc}; -use crate::{ChromaFormat, ColorVolume, Error, Frame, Profile, Result}; - -/// Encoder configuration. -#[derive(Debug, Clone)] -pub struct EncoderConfig { - pub profile: Profile, - pub width: u32, - pub height: u32, - /// Coded sample depth: 8 for LB/SQ/HQ; 10 or 12 for HQX and 444. - pub bit_depth: u8, - pub color_volume: ColorVolume, - /// Pixel aspect ratio to signal (0/0 = not signalled). - pub par: (u16, u16), -} - -impl EncoderConfig { - pub fn new(profile: Profile, width: u32, height: u32) -> EncoderConfig { - let bit_depth = match profile { - Profile::Hqx | Profile::R444 => 10, - _ => 8, - }; - EncoderConfig { profile, width, height, bit_depth, color_volume: ColorVolume::Bt709, par: (0, 0) } - } - - pub fn chroma(&self) -> ChromaFormat { - if self.profile == Profile::R444 { ChromaFormat::Yuv444 } else { ChromaFormat::Yuv422 } - } -} - -/// DNxHR encoder. -pub struct Encoder { - cfg: EncoderConfig, - info: &'static CidInfo, - frame_size: u32, - header_size: u32, - last_q: u32, -} - -/// Transformed macroblock data: per block, the DC value and the AC magnitudes pre-divided by -/// the weights (`p·|X|/W`, sign in the float's sign) in bitstream (zig-zag) order. -struct Prepared { - nw: usize, - ns: usize, - nblk: usize, - /// `[mb * nblk + k][r]`; `[..][0]` is the DC value. - t: Vec<[f32; 64]>, - /// DC bits of each macroblock (independent of the quantiser). - dc_bits: Vec, -} - -impl Encoder { - /// An encoder with the default configuration; fails on rasters the profile can't code. - pub fn new(profile: Profile, width: u32, height: u32) -> Result { - Encoder::with_config(EncoderConfig::new(profile, width, height)) - } - - pub fn with_config(cfg: EncoderConfig) -> Result { - if cfg.width == 0 || cfg.height == 0 || cfg.width > 16384 || cfg.height > 16384 { - return Err(Error::Input(format!("unsupported raster {}x{}", cfg.width, cfg.height))); - } - if cfg.chroma() == ChromaFormat::Yuv422 && !cfg.width.is_multiple_of(2) { - return Err(Error::Input("4:2:2 needs an even width".into())); - } - let ok_depth = match cfg.profile { - Profile::Hqx | Profile::R444 => matches!(cfg.bit_depth, 10 | 12), - _ => cfg.bit_depth == 8, - }; - if !ok_depth { - return Err(Error::Input(format!("{} does not support {}-bit", cfg.profile.name(), cfg.bit_depth))); - } - let info = cid_info(cfg.profile.cid()).ok_or_else(|| Error::Input(format!("no compression table for {}", cfg.profile.name())))?; - let frame_size = cfg.profile.frame_size(cfg.width, cfg.height); - let header_size = header::header_size_for(cfg.height); - Ok(Encoder { cfg, info, frame_size, header_size, last_q: 8 }) - } - - pub fn config(&self) -> &EncoderConfig { - &self.cfg - } - - /// Size of every encoded frame in bytes (CBR). - pub fn frame_size(&self) -> u32 { - self.frame_size - } - - /// Encode one frame. Its geometry and chroma format must match the configuration; samples - /// at another depth are rescaled to the coded depth. - pub fn encode(&mut self, f: &Frame) -> Result> { - let cfg = &self.cfg; - if f.width != cfg.width || f.height != cfg.height { - return Err(Error::Input(format!("frame is {}x{}, encoder is {}x{}", f.width, f.height, cfg.width, cfg.height))); - } - if f.chroma != cfg.chroma() { - return Err(Error::Input(format!("{:?} input for {}", f.chroma, cfg.profile.name()))); - } - if !(8..=16).contains(&f.bit_depth) { - return Err(Error::Input(format!("unsupported input depth {}", f.bit_depth))); - } - let n = f.width as usize * f.height as usize; - let cn = f.chroma_width() as usize * f.chroma_height() as usize; - if f.y.len() != n || f.cb.len() != cn || f.cr.len() != cn { - return Err(Error::Input("plane sizes do not match the frame geometry".into())); - } - let prep = self.prepare(f); - let vlc = vlc(self.info.vlc); - let budget_bits = (self.frame_size as u64 - self.header_size as u64 - 4) * 8; - let q = self.rate_control(&prep, vlc, budget_bits); - self.write(&prep, vlc, &q, f.rgb) - } - - fn mb_layout(&self) -> &'static [(u8, u8, u8)] { - match self.cfg.chroma() { - ChromaFormat::Yuv444 => { - &[(0, 0, 0), (0, 8, 0), (1, 0, 0), (1, 8, 0), (2, 0, 0), (2, 8, 0), (0, 0, 8), (0, 8, 8), (1, 0, 8), (1, 8, 8), (2, 0, 8), (2, 8, 8)] - } - _ => &[(0, 0, 0), (0, 8, 0), (1, 0, 0), (2, 0, 0), (0, 0, 8), (0, 8, 8), (1, 0, 8), (2, 0, 8)], - } - } - - fn prepare(&self, f: &Frame) -> Prepared { - let cfg = &self.cfg; - let nw = cfg.width.div_ceil(16) as usize; - let ns = cfg.height.div_ceil(16) as usize; - let layout = self.mb_layout(); - let nblk = layout.len(); - let depth = cfg.bit_depth as i32; - let shift = depth - f.bit_depth as i32; - let mid = (1i32 << (depth - 1)) as f32; - let inv_scale = if depth == 12 { 0.25f32 } else { 1.0 }; - let p = self.info.p as f32; - let w = self.info.weights; - let cmbw = if cfg.chroma() == ChromaFormat::Yuv444 { 16 } else { 8 }; - let planes: [(&[u16], usize, usize, usize); 3] = [ - (&f.y, f.width as usize, f.height as usize, 16), - (&f.cb, f.chroma_width() as usize, f.chroma_height() as usize, cmbw), - (&f.cr, f.chroma_width() as usize, f.chroma_height() as usize, cmbw), - ]; - let sample = |pl: usize, x: usize, y: usize| -> f32 { - let (d, pw, ph, _) = planes[pl]; - let v = d[y.min(ph - 1) * pw + x.min(pw - 1)] as i32; - let v = if shift >= 0 { v << shift } else { (v + (1 << (-shift - 1))) >> -shift }; - v.min((1 << depth) - 1) as f32 - mid - }; - let do_row = |s: usize| -> Vec<[f32; 64]> { - let mut out = Vec::with_capacity(nw * nblk); - for mb in 0..nw { - for &(pl, xo, yo) in layout { - let pl = pl as usize; - let x0 = mb * planes[pl].3 + xo as usize; - let y0 = s * 16 + yo as usize; - let mut b = [0f32; 64]; - for j in 0..8 { - for i in 0..8 { - b[j * 8 + i] = sample(pl, x0 + i, y0 + j); - } - } - fdct8x8(&mut b); - let wt = &w[(pl != 0) as usize]; - let mut t = [0f32; 64]; - let dc_max = if depth == 8 { 1023.0 } else { 4095.0 }; - t[0] = (b[0] * inv_scale).round().clamp(-dc_max - 1.0, dc_max); - for r in 1..64 { - let z = ZIGZAG[r] as usize; - t[r] = b[z] * inv_scale * p / wt[z - 1] as f32; - } - out.push(t); - } - } - out - }; - #[cfg(feature = "threads")] - let rows: Vec> = { - use rayon::prelude::*; - (0..ns).into_par_iter().map(do_row).collect() - }; - #[cfg(not(feature = "threads"))] - let rows: Vec> = (0..ns).map(do_row).collect(); - let t: Vec<[f32; 64]> = rows.into_iter().flatten().collect(); - // DC bits with per-row, per-component prediction - let v = vlc(self.info.vlc); - let mut dc_bits = vec![0u32; nw * ns]; - for s in 0..ns { - let mut pred = [0i32; 3]; - for mb in 0..nw { - let mut bits = 0; - for (k, &(pl, _, _)) in layout.iter().enumerate() { - let dc = t[(s * nw + mb) * nblk + k][0] as i32; - let e = dc - pred[pl as usize]; - pred[pl as usize] = dc; - let eta = dc_eta(e); - bits += v.dc_code[eta as usize].1 as u32 + eta; - } - dc_bits[s * nw + mb] = bits; - } - } - Prepared { nw, ns, nblk, t, dc_bits } - } - - /// Per-macroblock qsf values whose coded frame fits `budget_bits`. - fn rate_control(&mut self, prep: &Prepared, vlc: &VlcSet, budget_bits: u64) -> Vec { - let p_bits = if self.cfg.bit_depth == 8 { 4 } else { 6 }; - let max_amp = if p_bits == 4 { 1024 } else { 4096 }; - let nmb = prep.nw * prep.ns; - let mb_bits = |mb: usize, q: u32| -> u32 { - let mut bits = 12 + prep.dc_bits[mb]; - for k in 0..prep.nblk { - bits += ac_bits(&prep.t[mb * prep.nblk + k], q as f32, vlc, p_bits, max_amp); - } - bits - }; - let frame_bits = |q: u32| -> u64 { - let row = |s: usize| -> u64 { row_bytes((0..prep.nw).map(|m| mb_bits(s * prep.nw + m, q) as u64).sum()) * 8 }; - #[cfg(feature = "threads")] - { - use rayon::prelude::*; - (0..prep.ns).into_par_iter().map(row).sum() - } - #[cfg(not(feature = "threads"))] - { - (0..prep.ns).map(row).sum() - } - }; - // smallest feasible uniform q (bits are non-increasing in q) - let (mut lo, mut hi) = (1u32, 2047u32); - if frame_bits(1) <= budget_bits { - hi = 1; - } else { - // bracket around the previous frame's choice first - let g = self.last_q.clamp(2, 2047); - if frame_bits(g) <= budget_bits { - hi = g; - let g2 = (g / 2).max(1); - if g2 > 1 && frame_bits(g2) > budget_bits { - lo = g2; - } - } else { - lo = g; - } - while hi - lo > 1 { - let m = (lo + hi) / 2; - if frame_bits(m) <= budget_bits { hi = m } else { lo = m } - } - } - let q = hi; - self.last_q = q; - let mut qs = vec![q; nmb]; - if q > 1 { - // refine: one finer step per macroblock while the budget allows - let mut row_bits: Vec = (0..prep.ns).map(|s| (0..prep.nw).map(|m| mb_bits(s * prep.nw + m, q) as u64).sum()).collect(); - let mut total: u64 = row_bits.iter().map(|&b| row_bytes(b) * 8).sum(); - let cur: Vec = (0..nmb).map(|mb| mb_bits(mb, q)).collect(); - let finer: Vec = (0..nmb).map(|mb| mb_bits(mb, q - 1)).collect(); - // spread the refinement evenly over the frame: visit macroblocks in a strided order - let stride = 7919usize; - let mut idx = 0usize; - for _ in 0..nmb { - idx = (idx + stride) % nmb; - let s = idx / prep.nw; - let extra = finer[idx] as u64 - cur[idx].min(finer[idx]) as u64; - let nb = row_bits[s] + extra; - let nt = total - row_bytes(row_bits[s]) * 8 + row_bytes(nb) * 8; - if nt <= budget_bits { - row_bits[s] = nb; - total = nt; - qs[idx] = q - 1; - } - } - } - qs - } - - fn write(&self, prep: &Prepared, vlc: &VlcSet, qs: &[u32], rgb: bool) -> Result> { - let cfg = &self.cfg; - let p_bits = if cfg.bit_depth == 8 { 4 } else { 6 }; - let max_amp = if p_bits == 4 { 1024 } else { 4096 }; - let layout = self.mb_layout(); - // 4:4:4 signalling as ffmpeg writes and reads it (see `FrameHeader::rgb_planes`): RGB is - // CLF = 0 with ACF = 0; Y'CbCr is CLF = 1 with ACF = 1 on every macroblock. - let ycbcr444 = cfg.chroma() == ChromaFormat::Yuv444 && !rgb; - let write_row = |s: usize| -> Vec { - let mut w = Writer::new(); - let mut pred = [0i32; 3]; - for mb in 0..prep.nw { - let m = s * prep.nw + mb; - let q = qs[m]; - w.put(q, 11); - w.put(ycbcr444 as u32, 1); - for (k, &(pl, _, _)) in layout.iter().enumerate() { - let t = &prep.t[m * prep.nblk + k]; - let dc = t[0] as i32; - let e = dc - pred[pl as usize]; - pred[pl as usize] = dc; - let eta = dc_eta(e); - let (c, l) = vlc.dc_code[eta as usize]; - w.put(c as u32, l as u32); - if eta > 0 { - let rho = if e > 0 { e } else { e + (1 << eta) - 1 }; - w.put(rho as u32, eta); - } - write_ac(&mut w, t, q as f32, vlc, p_bits, max_amp); - } - } - w.finish_aligned(4) - }; - #[cfg(feature = "threads")] - let rows: Vec> = { - use rayon::prelude::*; - (0..prep.ns).into_par_iter().map(write_row).collect() - }; - #[cfg(not(feature = "threads"))] - let rows: Vec> = (0..prep.ns).map(write_row).collect(); - - let h = FrameHeader { - header_size: self.header_size, - version: 3, - cid: self.info.cid, - vbr: false, - field: FieldCode::Frame, - macf: false, - crc: false, - alpha: false, - lossless_alpha: false, - premultiplied_alpha: false, - width: cfg.width, - lines: cfg.height, - par: cfg.par, - bit_depth: cfg.bit_depth, - interlaced: false, - frame_encoding: true, - chroma: cfg.chroma(), - rgb: ycbcr444, - color_volume: cfg.color_volume, - timecode: None, - mb_rows: prep.ns as u32, - }; - let mut out = header::write(&h); - out.reserve(self.frame_size as usize - out.len()); - let mut off = 0u32; - for (s, r) in rows.iter().enumerate() { - let o = SCAN_INDEX_OFFSET + 4 * s; - out[o..o + 4].copy_from_slice(&off.to_be_bytes()); - off += r.len() as u32; - } - for r in rows { - out.extend_from_slice(&r); - } - let end = self.frame_size as usize - 4; - if out.len() > end { - return Err(Error::Input("rate control overflow".into())); - } - out.resize(end, 0); - out.extend_from_slice(&EOF_SIGNATURE); - Ok(out) - } -} - -#[inline] -fn row_bytes(bits: u64) -> u64 { - bits.div_ceil(32) * 4 -} - -#[inline] -fn dc_eta(e: i32) -> u32 { - if e == 0 { 0 } else { 32 - e.unsigned_abs().leading_zeros() } -} - -/// Quantised amplitude (≥ 1) of a pre-divided magnitude `|t| ≥ q`: the nearest reconstruction -/// level for decoders that reconstruct at `(A + 3/4)·step` (ffmpeg and ours by default). -#[inline(always)] -fn quant(t: f32, q: f32, max_amp: u32) -> u32 { - ((t.abs() / q - 0.25) as u32).clamp(1, max_amp) -} - -/// Bits of a block's AC coefficients plus EOB at quantiser `q`. -#[inline] -fn ac_bits(t: &[f32; 64], q: f32, v: &VlcSet, p_bits: u32, max_amp: u32) -> u32 { - let mut bits = v.eob.1 as u32; - let mut run = 0usize; - for &x in &t[1..] { - if x.abs() < q { - run += 1; - continue; - } - let a = quant(x, q, max_amp); - let p = (a - 1) >> 6; - let base = ((a - 1) & 63) as usize; - let frun = (run > 0) as usize; - let findex = (p > 0) as usize; - bits += v.amp_code[frun][findex][base].1 as u32 + 1; - if findex == 1 { - bits += p_bits; - } - if frun == 1 { - bits += v.run_code[run].1 as u32; - } - run = 0; - } - bits -} - -fn write_ac(w: &mut Writer, t: &[f32; 64], q: f32, v: &VlcSet, p_bits: u32, max_amp: u32) { - let mut run = 0usize; - for &x in &t[1..] { - if x.abs() < q { - run += 1; - continue; - } - let a = quant(x, q, max_amp); - let p = (a - 1) >> 6; - let base = ((a - 1) & 63) as usize; - let frun = (run > 0) as usize; - let findex = (p > 0) as usize; - let (c, l) = v.amp_code[frun][findex][base]; - w.put(c as u32, l as u32); - w.put((x < 0.0) as u32, 1); - if findex == 1 { - w.put(p, p_bits); - } - if frun == 1 { - let (c, l) = v.run_code[run]; - w.put(c as u32, l as u32); - } - run = 0; - } - w.put(v.eob.0 as u32, v.eob.1 as u32); -} diff --git a/crates/dnx/src/ffmpeg_probe.rs b/crates/dnx/src/ffmpeg_probe.rs deleted file mode 100644 index 1bb57024b..000000000 --- a/crates/dnx/src/ffmpeg_probe.rs +++ /dev/null @@ -1,272 +0,0 @@ -//! Black-box probes of ffmpeg's VC-3 decoder (ignored tests, run by hand). -//! -//! Each probe writes a stream whose first DCT block holds one AC coefficient (all other blocks -//! empty), decodes it with ffmpeg as an external process, and measures the reconstructed -//! coefficient with a forward DCT of the output pixels. They established the two places where -//! ffmpeg's decoder differs from the text of SMPTE ST 2019-1 (see the README): the quantisation -//! weights it uses for CID 1271, and its reconstruction offset. -//! -//! `DNX_CID=1271 cargo test -p filmcraft-dnx --lib ffmpeg_probe -- --ignored --nocapture` -use crate::bits::Writer; -use crate::header::{self, EOF_SIGNATURE, FieldCode, FrameHeader, SCAN_INDEX_OFFSET}; -use crate::tables::{ZIGZAG, cid_info, vlc}; -use crate::{ChromaFormat, ColorVolume}; - -struct ProbeStream { - cid: u32, - width: u32, - height: u32, - depth: u8, - quantizer: u32, - coefficient_position: usize, - amplitude: u32, - negative: bool, - block: usize, -} - -fn stream_cid(input: ProbeStream) -> Vec { - let ProbeStream { cid, width: w, height: h, depth, quantizer: q, coefficient_position: pos, amplitude: amp, negative: neg, block: blk } = input; - let info = cid_info(cid).unwrap(); - let v = vlc(info.vlc); - let p_bits = if depth == 8 { 4 } else { 6 }; - let nw = w.div_ceil(16) as usize; - let ns = h.div_ceil(16) as usize; - let mut rows = Vec::new(); - for s in 0..ns { - let mut wr = Writer::new(); - for mb in 0..nw { - wr.put(q, 11); - wr.put(0, 1); - for k in 0..8 { - let (c, l) = v.dc_code[0]; - wr.put(c as u32, l as u32); - if s == 0 && mb == 0 && k == blk { - let p = (amp - 1) >> 6; - let base = ((amp - 1) & 63) as usize; - let frun = (pos > 1) as usize; - let (c, l) = v.amp_code[frun][(p > 0) as usize][base]; - wr.put(c as u32, l as u32); - wr.put(neg as u32, 1); - if p > 0 { - wr.put(p, p_bits); - } - if frun == 1 { - let (c, l) = v.run_code[pos - 1]; - wr.put(c as u32, l as u32); - } - } - wr.put(v.eob.0 as u32, v.eob.1 as u32); - } - } - rows.push(wr.finish_aligned(4)); - } - let hdr = FrameHeader { - header_size: 0x280, - version: if cid >= 1270 { 3 } else { 1 }, - cid, - vbr: false, - field: FieldCode::Frame, - macf: false, - crc: false, - alpha: false, - lossless_alpha: false, - premultiplied_alpha: false, - width: w, - lines: h, - par: (0, 0), - bit_depth: depth, - interlaced: false, - frame_encoding: true, - chroma: ChromaFormat::Yuv422, - rgb: false, - color_volume: ColorVolume::Bt709, - timecode: None, - mb_rows: ns as u32, - }; - let mut out = header::write(&hdr); - let mut off = 0u32; - for (s, r) in rows.iter().enumerate() { - out[SCAN_INDEX_OFFSET + 4 * s..SCAN_INDEX_OFFSET + 4 * s + 4].copy_from_slice(&off.to_be_bytes()); - off += r.len() as u32; - } - for r in rows { - out.extend_from_slice(&r); - } - let size = match info.kind { - crate::tables::Kind::Hd { frame_size, .. } => frame_size as usize, - crate::tables::Kind::Ri { c0 } => crate::ri_frame_size(w, h, c0, false) as usize, - }; - out.resize(size - 4, 0); - out.extend_from_slice(&EOF_SIGNATURE); - out -} - -#[test] -#[ignore] -fn probe_weights() { - let ff = filmcraft_testkit::ffmpeg().unwrap(); - let dir = filmcraft_testkit::fixtures_dir("dnx"); - let cid: u32 = std::env::var("DNX_CID").unwrap().parse().unwrap(); - let (w, h, depth): (u32, u32, u8) = match cid { - 1250 => (1280, 720, 10), - 1251 | 1252 => (1280, 720, 8), - 1235 => (1920, 1080, 10), - 1271 => (256, 128, 10), - 1272..=1274 => (256, 128, 8), - c => { - let i = cid_info(c).unwrap(); - match i.kind { - crate::tables::Kind::Hd { width, height, depth, .. } => (width as u32, height as u32, depth), - _ => panic!(), - } - } - }; - let pix = if depth == 10 { "yuv422p10le" } else { "yuv422p" }; - let prof = cid; - let amp = 20u32; - let q = 4u32; - for blk in [0usize, 2] { - let mut grid = [[0f64; 8]; 8]; - for pos in 1..64 { - let s = stream_cid(ProbeStream { - cid, - width: w, - height: h, - depth, - quantizer: q, - coefficient_position: pos, - amplitude: amp, - negative: false, - block: blk, - }); - let path = dir.join("explore.dnxhd"); - std::fs::write(&path, &s).unwrap(); - let out = std::process::Command::new(&ff) - .args(["-hide_banner", "-loglevel", "error", "-f", "dnxhd", "-i"]) - .arg(&path) - .args(["-f", "rawvideo", "-pix_fmt", pix, "-"]) - .output() - .unwrap(); - let px: Vec = if depth == 8 { - out.stdout.iter().map(|&b| b as f64).collect() - } else { - out.stdout.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]]) as f64).collect() - }; - let (off, stride) = if blk == 0 { (0, w as usize) } else { ((w * h) as usize, w as usize / 2) }; - let mid = (1 << (depth - 1)) as f64; - let z = ZIGZAG[pos] as usize; - let (u, vv) = (z % 8, z / 8); - let mut sum = 0f64; - for j in 0..8 { - for i in 0..8 { - let cu = if u == 0 { 0.5f64.sqrt() } else { 1.0 }; - let cv = if vv == 0 { 0.5f64.sqrt() } else { 1.0 }; - sum += cu * cv / 4.0 - * (px[off + j * stride + i] - mid) - * (((2 * i + 1) * u) as f64 * std::f64::consts::PI / 16.0).cos() - * (((2 * j + 1) * vv) as f64 * std::f64::consts::PI / 16.0).cos(); - } - } - grid[vv][u] = sum * cid_info(cid).unwrap().p as f64 / ((amp as f64 + 0.5) * q as f64); - } - { - use crate::spec_tables::*; - let all: [(&str, &[[u8; 63]; 2]); 11] = [ - ("D1", &W_D_1), - ("D2", &W_D_2), - ("D3", &W_D_3), - ("D4", &W_D_4), - ("D5", &W_D_5), - ("D6", &W_D_6), - ("D7", &W_D_7), - ("D8", &W_D_8), - ("D9", &W_D_9), - ("D10", &W_D_10), - ("D11", &W_D_11), - ]; - let mut res = vec![]; - for (n, t) in all { - for c in 0..2 { - let mut e = 0f64; - for i in 1..64 { - e += (grid[i / 8][i % 8] - t[c][i - 1] as f64).powi(2); - } - res.push(((e / 63.0).sqrt(), format!("{n}{}", if c == 0 { "L" } else { "C" }))); - } - } - res.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); - println!("best matches: {:?}", &res[..4]); - } - println!("{prof:?} block {blk} estimated W (spec table below):"); - let w = &cid_info(cid).unwrap().weights[(blk != 0) as usize]; - for v in 0..8 { - let mut l = String::new(); - for u in 0..8 { - l += &format!("{:6.1}", grid[v][u]); - } - l += " |"; - for u in 0..8 { - if v + u > 0 { - l += &format!("{:4}", w[v * 8 + u - 1]); - } else { - l += " -"; - } - } - println!("{l}"); - } - } -} - -#[test] -#[ignore] -fn probe_reconstruction() { - let ff = filmcraft_testkit::ffmpeg().unwrap(); - let dir = filmcraft_testkit::fixtures_dir("dnx"); - let cid: u32 = std::env::var("DNX_CID").unwrap().parse().unwrap(); - let (w, h, depth): (u32, u32, u8) = match cid { - 1250 => (1280, 720, 10), - 1251 | 1252 => (1280, 720, 8), - 1235 => (1920, 1080, 10), - 1271 => (256, 128, 10), - _ => (256, 128, 8), - }; - let pix = if depth == 10 { "yuv422p10le" } else { "yuv422p" }; - let p = cid_info(cid).unwrap().p as f64; - for q in [1u32, 2, 3, 4, 8, 16] { - let mut xs = vec![]; - for amp in 1..=40u32 { - let s = - stream_cid(ProbeStream { cid, width: w, height: h, depth, quantizer: q, coefficient_position: 1, amplitude: amp, negative: false, block: 0 }); - let path = dir.join("explore.dnxhd"); - std::fs::write(&path, &s).unwrap(); - let out = std::process::Command::new(&ff) - .args(["-hide_banner", "-loglevel", "error", "-f", "dnxhd", "-i"]) - .arg(&path) - .args(["-f", "rawvideo", "-pix_fmt", pix, "-"]) - .output() - .unwrap(); - let px: Vec = if depth == 8 { - out.stdout.iter().map(|&b| b as f64).collect() - } else { - out.stdout.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]]) as f64).collect() - }; - let mid = (1 << (depth - 1)) as f64; - let mut sum = 0f64; - for j in 0..8 { - for i in 0..8 { - sum += 0.25 * (px[j * w as usize + i] - mid) * ((2 * i + 1) as f64 * std::f64::consts::PI / 16.0).cos() * std::f64::consts::FRAC_1_SQRT_2; - } - } - xs.push((amp as f64, sum)); - } - let n = xs.len() as f64; - let sx: f64 = xs.iter().map(|x| x.0).sum(); - let sy: f64 = xs.iter().map(|x| x.1).sum(); - let sxx: f64 = xs.iter().map(|x| x.0 * x.0).sum(); - let sxy: f64 = xs.iter().map(|x| x.0 * x.1).sum(); - let a = (n * sxy - sx * sy) / (n * sxx - sx * sx); - let b = (sy - a * sx) / n; - let step = 32.0 * q as f64 / p; - println!("cid {cid} q {q}: step {step} slope {a:.4} ({:.4} steps) intercept {b:.3} ({:.4} steps)", a / step, b / step); - } -} diff --git a/crates/dnx/src/header.rs b/crates/dnx/src/header.rs deleted file mode 100644 index 245953809..000000000 --- a/crates/dnx/src/header.rs +++ /dev/null @@ -1,259 +0,0 @@ -//! Coding-unit header (SMPTE ST 2019-1 §7.2): parse and write. - -use crate::tables::{Kind, cid_info, ri_frame_size}; -use crate::{ChromaFormat, ColorVolume, Error, Result}; - -/// Offset of the macroblock scan indices payload. -pub const SCAN_INDEX_OFFSET: usize = 0x170; -/// EOF signature when the CRC flag is clear (§7.4, Figure 39). -pub const EOF_SIGNATURE: [u8; 4] = [0x60, 0x0D, 0xC0, 0xDE]; - -/// Field/frame code of a coding unit (`FFC`, §7.2.2). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum FieldCode { - /// A progressive frame, or an interlaced frame coded as one unit. - Frame, - /// Field 1 (the top field) of a field-coded interlaced frame. - Field1, - /// Field 2 (the bottom field). - Field2, -} - -/// A parsed coding-unit header. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FrameHeader { - /// Header size `HS` in bytes (the compressed payload starts here). - pub header_size: u32, - /// Header version number `HVN` (1, 2 = HD profile; 3 = RI profile; 4 = HD with alpha). - pub version: u8, - pub cid: u32, - /// Variable bitrate (no payload padding). - pub vbr: bool, - pub field: FieldCode, - /// Macroblock-adaptive field/frame coding (CID 1260). - pub macf: bool, - /// The EOF signature holds a CRC-32 instead of `0x600DC0DE`. - pub crc: bool, - pub alpha: bool, - /// Alpha is coded losslessly (differential RLE) instead of with the DCT. - pub lossless_alpha: bool, - pub premultiplied_alpha: bool, - /// Samples per line (`SPL`). - pub width: u32, - /// Active lines (`ALPF`) of this coding unit: lines per frame, or per field for a field - /// coding unit. - pub lines: u32, - /// Pixel aspect ratio `PARC / PARN` (0/0 = not signalled). - pub par: (u16, u16), - pub bit_depth: u8, - /// Source scan type is interlaced (`SST`). - pub interlaced: bool, - /// Frame encoding (`FFE`); false = each field is its own coding unit. - pub frame_encoding: bool, - pub chroma: ChromaFormat, - /// The bitstream codes RGB (`CLF`). - pub rgb: bool, - pub color_volume: ColorVolume, - /// SMPTE ST 12-1 time code bytes, if present. - pub timecode: Option<[u8; 8]>, - /// Number of macroblock scan lines `NS`. - pub mb_rows: u32, -} - -impl FrameHeader { - /// Whether the 4:4:4 planes carry R′G′B′ (macroblocks with ACF = 1 carry Y′CbCr). - /// - /// The standard ties RGB to `CLF = 1`. ffmpeg's encoder, the most common source of DNxHR 444 - /// files, writes RGB with `CLF = 0` (and Y′CbCr with `CLF = 1`, `ACF = 1` everywhere), and - /// its decoder reads them that way; we follow that interpretation: every 4:4:4 stream is RGB - /// except macroblocks flagged ACF = 1. - pub fn rgb_planes(&self) -> bool { - self.rgb || self.chroma == ChromaFormat::Yuv444 - } - - /// Macroblocks per scan line `NW`. - pub fn mb_width(&self) -> u32 { - self.width.div_ceil(16) - } - - /// Size of this coding unit for CBR streams (`None` for VBR streams), from Table C.1 or - /// equation 7.1. - pub fn coding_unit_size(&self) -> Option { - if self.vbr { - return None; - } - let info = cid_info(self.cid)?; - Some(match info.kind { - Kind::Hd { frame_size, interlaced, .. } => { - let f = if self.alpha { frame_size + frame_size / 2 } else { frame_size }; - if interlaced && !self.frame_encoding { f / 2 } else { f } - } - Kind::Ri { c0 } => ri_frame_size(self.width, self.lines, c0, self.alpha), - }) - } -} - -fn be16(d: &[u8], o: usize) -> u32 { - u32::from(d[o]) << 8 | u32::from(d[o + 1]) -} - -fn be32(d: &[u8], o: usize) -> u32 { - u32::from_be_bytes([d[o], d[o + 1], d[o + 2], d[o + 3]]) -} - -/// Parse a coding-unit header at the start of `d`. -pub fn parse(d: &[u8]) -> Result { - if d.len() < 0x280 { - return Err(Error::Truncated); - } - let header_size = be32(d, 0); - let version = d[4]; - if !(1..=4).contains(&version) { - return Err(Error::Invalid("unknown header version")); - } - if header_size < 0x280 || header_size as usize > d.len() { - return Err(if header_size as usize > d.len() { Error::Truncated } else { Error::Invalid("header size") }); - } - let cid = be32(d, 0x28); - let info = cid_info(cid).ok_or(Error::UnknownCid(cid))?; - let vbr = d[5] & 0x10 != 0; - let field = match d[5] & 3 { - 2 => FieldCode::Field1, - 3 => FieldCode::Field2, - _ => FieldCode::Frame, - }; - let macf = d[6] & 0x20 != 0; - let crc = d[6] & 0x10 != 0; - let alpha = d[7] & 1 != 0; - let lossless_alpha = d[7] & 2 != 0; - let premultiplied_alpha = d[7] & 4 != 0; - let lines = be16(d, 0x18); - let width = be16(d, 0x1A); - let parc = (u32::from(d[0x1C] >> 2 & 3) << 8 | u32::from(d[0x1F])) as u16; - let parn = (u32::from(d[0x1C] & 3) << 8 | u32::from(d[0x20])) as u16; - let bit_depth = match d[0x21] >> 5 { - 1 => 8, - 2 => 10, - 3 => 12, - _ => return Err(Error::Invalid("sample bit depth")), - }; - let interlaced = d[0x22] & 4 != 0; - let frame_encoding = d[0x2C] & 0x80 != 0; - let chroma = match d[0x2C] >> 5 & 3 { - 0 => ChromaFormat::Yuv422, - 1 => ChromaFormat::Yuv420, - 2 => ChromaFormat::Yuv444, - _ => return Err(Error::Invalid("sub sampling")), - }; - let rgb = d[0x2C] & 1 != 0; - let color_volume = match d[0x2C] >> 1 & 3 { - 0 => ColorVolume::Bt709, - 1 => ColorVolume::Bt2020Ncl, - 2 => ColorVolume::Bt2020Cl, - _ => ColorVolume::OutOfBand, - }; - let timecode = if d[0x30] & 0x80 != 0 { d.get(0x31..).and_then(|t| t.first_chunk::<8>()).copied() } else { None }; - let mb_rows = be16(d, 0x16C); - if width == 0 || lines == 0 || width > 16384 || lines > 16384 { - return Err(Error::Invalid("raster size")); - } - if let Kind::Hd { width: w, depth, .. } = info.kind - && (w as u32 != width || depth != bit_depth) - { - return Err(Error::Invalid("raster does not match the compression ID")); - } - if rgb && chroma != ChromaFormat::Yuv444 { - return Err(Error::Invalid("RGB requires 4:4:4")); - } - if chroma == ChromaFormat::Yuv444 && !matches!(cid, 1256 | 1270) { - return Err(Error::Invalid("4:4:4 requires CID 1256 or 1270")); - } - // macroblock scan lines must cover the coded raster, and their indices must fit the header - if mb_rows == 0 || SCAN_INDEX_OFFSET + 4 * mb_rows as usize > header_size as usize { - return Err(Error::Invalid("macroblock scan line count")); - } - if mb_rows != lines.div_ceil(16) { - return Err(Error::Invalid("macroblock scan lines do not match the raster")); - } - Ok(FrameHeader { - header_size, - version, - cid, - vbr, - field, - macf, - crc, - alpha, - lossless_alpha, - premultiplied_alpha, - width, - lines, - par: (parc, parn), - bit_depth, - interlaced, - frame_encoding, - chroma, - rgb, - color_volume, - timecode, - mb_rows, - }) -} - -/// Header size for a raster of `lines` lines (§7.2: 640 bytes up to 1088 lines, plus 4 bytes -/// per further 16 lines). -pub fn header_size_for(lines: u32) -> u32 { - if lines <= 1088 { 0x280 } else { 0x280 + 4 * (lines - 1088).div_ceil(16) } -} - -/// Write a header (without scan indices) for `h` into a zeroed buffer of `h.header_size` bytes. -pub fn write(h: &FrameHeader) -> Vec { - let mut d = vec![0u8; h.header_size as usize]; - d[0..4].copy_from_slice(&h.header_size.to_be_bytes()); - d[4] = h.version; - d[5] = (h.vbr as u8) << 4 - | match h.field { - FieldCode::Frame => 1, - FieldCode::Field1 => 2, - FieldCode::Field2 => 3, - }; - d[6] = 0x80 | (h.macf as u8) << 5 | (h.crc as u8) << 4; - d[7] = 0xA0 | (h.premultiplied_alpha as u8) << 2 | (h.lossless_alpha as u8) << 1 | h.alpha as u8; - d[0x18..0x1A].copy_from_slice(&(h.lines as u16).to_be_bytes()); - d[0x1A..0x1C].copy_from_slice(&(h.width as u16).to_be_bytes()); - d[0x1C] = ((h.par.0 >> 8) as u8 & 3) << 2 | ((h.par.1 >> 8) as u8 & 3); - d[0x1D..0x1F].copy_from_slice(&(h.lines as u16).to_be_bytes()); - d[0x1F] = h.par.0 as u8; - d[0x20] = h.par.1 as u8; - let sbd = match h.bit_depth { - 8 => 1, - 10 => 2, - _ => 3, - }; - d[0x21] = sbd << 5 | 0x18; - d[0x22] = 0x88 | (h.interlaced as u8) << 2; - d[0x28..0x2C].copy_from_slice(&h.cid.to_be_bytes()); - let ssc = match h.chroma { - ChromaFormat::Yuv422 => 0, - ChromaFormat::Yuv420 => 1, - ChromaFormat::Yuv444 => 2, - }; - let clv = match h.color_volume { - ColorVolume::Bt709 => 0, - ColorVolume::Bt2020Ncl => 1, - ColorVolume::Bt2020Cl => 2, - ColorVolume::OutOfBand => 3, - }; - d[0x2C] = (h.frame_encoding as u8) << 7 | ssc << 5 | clv << 1 | h.rgb as u8; - if let Some(tc) = h.timecode { - d[0x30] = 0x80; - d[0x31..0x39].copy_from_slice(&tc); - } - d[0x5F] = 0x01; - d[0x167] = 0x02; - let msips = (4 * h.mb_rows + 4) as u16; - d[0x16A..0x16C].copy_from_slice(&msips.to_be_bytes()); - d[0x16C..0x16E].copy_from_slice(&(h.mb_rows as u16).to_be_bytes()); - d[0x16F] = 0x10; - d -} diff --git a/crates/dnx/src/lib.rs b/crates/dnx/src/lib.rs deleted file mode 100644 index a5f2b8c4f..000000000 --- a/crates/dnx/src/lib.rs +++ /dev/null @@ -1,235 +0,0 @@ -//! Clean-room Avid DNxHD / DNxHR codec (VC-3, implemented from SMPTE ST 2019-1:2016 and its -//! Amendment 1:2023). -//! -//! - **Decoder** ([`decode_frame`]): every compression ID of the standard — the DNxHD (HD -//! profile) IDs 1235–1260 including interlaced field and macroblock-adaptive coding, thin -//! rasters and 4:4:4 RGB, and the resolution-independent DNxHR IDs 1270–1274 (LB, SQ, HQ, HQX, -//! 444) at 8, 10 and 12 bits in 4:2:0, 4:2:2 and 4:4:4, with DCT or lossless alpha. Macroblock -//! scan lines decode in parallel with the `threads` feature (rayon). -//! - **Encoder** ([`Encoder`]): DNxHR SQ / HQ (8-bit 4:2:2) and HQX (10- or 12-bit 4:2:2), CBR -//! at the standard's frame sizes, progressive. -//! -//! See the crate README for accuracy, performance 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 spec_tables; -mod tables; - -pub use decode::{DecodeOptions, decode_frame, decode_frame_with, probe}; -pub use encode::{Encoder, EncoderConfig}; -pub use header::{FieldCode, FrameHeader}; -pub use tables::ri_frame_size; - -/// Codec errors. -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -pub enum Error { - #[error("truncated VC-3 data")] - Truncated, - #[error("invalid VC-3 data: {0}")] - Invalid(&'static str), - #[error("unknown VC-3 compression ID {0}")] - UnknownCid(u32), - #[error("corrupt macroblock scan line {0}")] - Row(usize), - #[error("invalid encoder input: {0}")] - Input(String), -} - -pub type Result = std::result::Result; - -/// Chroma subsampling (`SSC`). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum ChromaFormat { - /// Half width, half height. - Yuv420, - /// Half width, full height. - Yuv422, - /// Full size (also RGB). - Yuv444, -} - -impl ChromaFormat { - pub fn chroma_width(self, width: u32) -> u32 { - match self { - ChromaFormat::Yuv444 => width, - _ => width.div_ceil(2), - } - } - pub fn chroma_height(self, height: u32) -> u32 { - match self { - ChromaFormat::Yuv420 => height.div_ceil(2), - _ => height, - } - } -} - -/// Colour volume (`CLV`). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] -pub enum ColorVolume { - #[default] - Bt709, - /// BT.2020 non-constant luminance. - Bt2020Ncl, - /// BT.2020 constant luminance. - Bt2020Cl, - /// Described out of band. - OutOfBand, -} - -/// Frame scan structure. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] -pub enum Interlace { - #[default] - Progressive, - /// Interlaced; field 1 (the top field, even lines) comes first. - TopFieldFirst, -} - -/// DNxHR profiles (resolution-independent compression IDs). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum Profile { - /// DNxHR LB (CID 1274), 8-bit. - Lb, - /// DNxHR SQ (CID 1273), 8-bit. - Sq, - /// DNxHR HQ (CID 1272), 8-bit. - Hq, - /// DNxHR HQX (CID 1271), 10- or 12-bit. - Hqx, - /// DNxHR 444 (CID 1270), 10- or 12-bit 4:4:4. - R444, -} - -impl Profile { - pub fn cid(self) -> u32 { - match self { - Profile::Lb => 1274, - Profile::Sq => 1273, - Profile::Hq => 1272, - Profile::Hqx => 1271, - Profile::R444 => 1270, - } - } - pub fn from_cid(cid: u32) -> Option { - [Profile::Lb, Profile::Sq, Profile::Hq, Profile::Hqx, Profile::R444].into_iter().find(|p| p.cid() == cid) - } - pub fn name(self) -> &'static str { - match self { - Profile::Lb => "DNxHR LB", - Profile::Sq => "DNxHR SQ", - Profile::Hq => "DNxHR HQ", - Profile::Hqx => "DNxHR HQX", - Profile::R444 => "DNxHR 444", - } - } - /// CBR frame size for a raster (equation 7.1). - pub fn frame_size(self, width: u32, height: u32) -> u32 { - let c0 = match self { - Profile::Lb => 188416, - Profile::Sq => 606208, - Profile::Hq | Profile::Hqx => 917504, - Profile::R444 => 1835008, - }; - ri_frame_size(width, height, c0, false) - } -} - -/// A human-readable name for a compression ID ("DNxHD 1080p 145/120 8-bit", "DNxHR HQ"…). -pub fn cid_name(cid: u32) -> String { - if let Some(p) = Profile::from_cid(cid) { - return p.name().into(); - } - match cid { - 1235 => "DNxHD 1080p 10-bit (CID 1235)".into(), - 1237 => "DNxHD 1080p 8-bit (CID 1237)".into(), - 1238 => "DNxHD 1080p 8-bit (CID 1238)".into(), - 1241 => "DNxHD 1080i 10-bit (CID 1241)".into(), - 1242 => "DNxHD 1080i 8-bit (CID 1242)".into(), - 1243 => "DNxHD 1080i 8-bit (CID 1243)".into(), - 1244 => "DNxHD 1080i thin raster (CID 1244)".into(), - 1250 => "DNxHD 720p 10-bit (CID 1250)".into(), - 1251 => "DNxHD 720p 8-bit (CID 1251)".into(), - 1252 => "DNxHD 720p 8-bit (CID 1252)".into(), - 1253 => "DNxHD 1080p 36 (CID 1253)".into(), - 1256 => "DNxHD 444 10-bit (CID 1256)".into(), - 1258 => "DNxHD 720p thin raster (CID 1258)".into(), - 1259 => "DNxHD 1080p thin raster (CID 1259)".into(), - 1260 => "DNxHD 1080i thin raster (CID 1260)".into(), - _ => format!("VC-3 (CID {cid})"), - } -} - -/// A decoded (or to-be-encoded) picture: planar `u16` samples, rows packed without padding. -/// -/// For RGB streams (`rgb == true`) the planes `y`, `cb`, `cr` hold G, B and R: the coded channel -/// order Ch1/Ch2/Ch3 as written by ffmpeg and read by its decoder (the standard's text names Ch1 -/// red; see the README). -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Frame { - pub width: u32, - pub height: u32, - pub chroma: ChromaFormat, - pub rgb: bool, - /// Significant bits per sample of every plane (8, 10 or 12). - pub bit_depth: u8, - pub y: Vec, - pub cb: Vec, - pub cr: Vec, - /// Full-range alpha (0 = transparent). - pub alpha: Option>, - pub interlace: Interlace, - pub color_volume: ColorVolume, - /// Compression ID of the stream. - pub cid: u32, - /// Pixel aspect ratio (0/0 = not signalled). - pub par: (u16, u16), -} - -impl Frame { - /// A black (video-range) frame. - 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 * chroma.chroma_height(height) as usize; - let s = bit_depth.saturating_sub(8) as u32; - Frame { - width, - height, - chroma, - rgb: false, - 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_volume: ColorVolume::Bt709, - cid: 0, - par: (0, 0), - } - } - - pub(crate) fn new_like(h: &FrameHeader, width: u32, height: u32) -> Frame { - let mut f = Frame::new(width, height, h.chroma, h.bit_depth, h.alpha); - f.rgb = h.rgb_planes(); - f.color_volume = h.color_volume; - f.cid = h.cid; - f.par = h.par; - f - } - - pub fn chroma_width(&self) -> u32 { - self.chroma.chroma_width(self.width) - } - - pub fn chroma_height(&self) -> u32 { - self.chroma.chroma_height(self.height) - } -} -#[cfg(test)] -mod ffmpeg_probe; diff --git a/crates/dnx/src/spec_tables.rs b/crates/dnx/src/spec_tables.rs deleted file mode 100644 index a22bf3b9e..000000000 --- a/crates/dnx/src/spec_tables.rs +++ /dev/null @@ -1,2115 +0,0 @@ -// Generated by tools/extract_tables.py from SMPTE ST 2019-1:2016 (VC-3), Annexes D and E. -// Do not edit by hand. - -pub const W_D_1: [[u8; 63]; 2] = [ - [ - 32, 32, 32, 33, 35, 38, 39, 32, 33, 32, 33, 36, 36, 39, 42, 32, 32, 33, 36, 35, 37, 41, 43, 31, 33, 34, 36, 36, 40, 42, 48, 32, 34, 36, 37, 39, 42, 46, - 51, 36, 37, 37, 39, 41, 46, 51, 55, 37, 39, 41, 41, 47, 50, 55, 56, 41, 42, 41, 44, 50, 53, 60, 60, - ], - [ - 32, 33, 34, 39, 41, 54, 59, 33, 34, 35, 38, 43, 49, 58, 84, 34, 37, 39, 44, 46, 55, 74, 87, 40, 42, 47, 48, 58, 70, 87, 86, 43, 50, 56, 63, 72, 94, 91, - 82, 55, 63, 65, 75, 93, 89, 85, 73, 61, 67, 82, 81, 83, 90, 79, 73, 74, 84, 75, 78, 90, 85, 73, 73, - ], -]; -pub const W_D_2: [[u8; 63]; 2] = [ - [ - 32, 36, 37, 41, 44, 54, 60, 33, 34, 36, 39, 43, 51, 62, 78, 34, 36, 38, 41, 49, 59, 73, 79, 37, 38, 40, 47, 55, 66, 80, 95, 38, 41, 46, 54, 63, 79, 93, - 96, 46, 47, 56, 64, 78, 90, 97, 98, 49, 58, 66, 78, 89, 97, 102, 98, 61, 65, 82, 87, 100, 104, 99, 99, - ], - [ - 32, 38, 39, 47, 51, 77, 83, 36, 39, 41, 48, 55, 74, 85, 95, 39, 45, 53, 58, 72, 83, 105, 89, 51, 58, 66, 73, 82, 109, 92, 95, 57, 75, 78, 89, 105, 95, - 93, 96, 81, 82, 99, 99, 94, 90, 97, 98, 83, 96, 97, 93, 89, 97, 102, 98, 90, 94, 92, 88, 100, 104, 99, 99, - ], -]; -pub const W_D_3: [[u8; 63]; 2] = [ - [ - 32, 33, 33, 37, 37, 41, 42, 32, 34, 33, 35, 36, 38, 41, 43, 33, 33, 33, 36, 37, 39, 41, 44, 33, 33, 35, 36, 38, 40, 44, 50, 33, 36, 35, 38, 39, 44, 49, - 51, 38, 36, 38, 39, 44, 47, 51, 58, 38, 39, 41, 44, 47, 51, 57, 59, 41, 42, 45, 47, 53, 55, 57, 57, - ], - [ - 32, 34, 34, 40, 41, 55, 61, 35, 35, 35, 39, 42, 52, 62, 75, 35, 39, 41, 44, 52, 58, 68, 77, 43, 45, 48, 53, 58, 65, 74, 80, 45, 55, 59, 63, 66, 74, 76, - 89, 59, 65, 66, 70, 77, 73, 90, 84, 63, 66, 74, 76, 73, 82, 86, 82, 65, 70, 73, 73, 77, 80, 82, 82, - ], -]; -pub const W_D_4: [[u8; 63]; 2] = [ - [ - 32, 35, 36, 39, 40, 45, 45, 33, 34, 37, 39, 40, 43, 46, 46, 34, 36, 38, 38, 42, 47, 46, 46, 37, 38, 38, 41, 46, 45, 47, 49, 38, 39, 41, 45, 45, 47, 48, - 50, 38, 39, 43, 44, 46, 47, 49, 48, 37, 39, 40, 44, 45, 48, 48, 49, 37, 37, 42, 43, 46, 47, 49, 49, - ], - [ - 32, 37, 40, 41, 41, 45, 45, 36, 37, 41, 41, 42, 45, 46, 48, 38, 40, 41, 43, 45, 47, 48, 46, 40, 42, 44, 44, 46, 48, 47, 49, 42, 44, 45, 45, 47, 47, 48, - 50, 45, 46, 44, 45, 46, 47, 49, 48, 46, 42, 45, 47, 45, 48, 49, 49, 41, 43, 46, 45, 46, 48, 49, 49, - ], -]; -pub const W_D_5: [[u8; 63]; 2] = [ - [ - 32, 35, 36, 38, 37, 40, 42, 33, 34, 35, 37, 37, 37, 45, 49, 33, 33, 37, 37, 36, 46, 48, 50, 33, 36, 36, 37, 44, 46, 52, 51, 35, 37, 39, 41, 48, 52, 51, - 50, 37, 38, 42, 50, 50, 50, 49, 49, 37, 44, 49, 49, 50, 49, 51, 47, 45, 47, 47, 48, 51, 52, 47, 47, - ], - [ - 32, 45, 45, 51, 47, 45, 47, 37, 45, 44, 49, 41, 47, 50, 51, 42, 38, 44, 40, 47, 51, 49, 51, 37, 42, 43, 51, 49, 48, 52, 54, 40, 44, 54, 46, 51, 52, 54, - 53, 46, 51, 47, 55, 54, 53, 53, 60, 48, 49, 55, 54, 52, 55, 62, 60, 47, 50, 49, 49, 59, 63, 60, 60, - ], -]; -pub const W_D_6: [[u8; 63]; 2] = [ - [ - 32, 35, 35, 38, 40, 44, 45, 32, 33, 35, 35, 39, 42, 44, 45, 33, 35, 34, 37, 41, 42, 45, 45, 35, 35, 37, 40, 41, 44, 45, 48, 35, 37, 38, 40, 43, 45, 47, - 48, 36, 36, 38, 40, 45, 47, 48, 47, 35, 36, 38, 41, 45, 46, 47, 48, 36, 37, 39, 41, 44, 45, 47, 47, - ], - [ - 32, 37, 39, 41, 42, 45, 45, 36, 36, 39, 41, 43, 45, 46, 45, 37, 41, 41, 43, 45, 44, 45, 46, 43, 42, 43, 46, 44, 45, 46, 48, 43, 44, 47, 45, 44, 46, 47, - 49, 44, 46, 44, 45, 45, 47, 48, 47, 44, 42, 46, 44, 45, 46, 47, 48, 41, 43, 45, 44, 45, 46, 47, 47, - ], -]; -pub const W_D_7: [[u8; 63]; 2] = [ - [ - 32, 37, 37, 40, 41, 52, 53, 33, 36, 36, 38, 40, 48, 49, 52, 34, 34, 37, 39, 44, 47, 49, 54, 33, 35, 38, 40, 45, 46, 54, 51, 34, 37, 37, 42, 44, 49, 52, - 48, 34, 34, 38, 43, 44, 51, 50, 50, 33, 36, 41, 44, 51, 52, 50, 54, 36, 38, 44, 47, 53, 53, 54, 54, - ], - [ - 32, 40, 38, 42, 40, 45, 45, 34, 42, 36, 43, 38, 46, 46, 49, 38, 35, 43, 39, 44, 47, 47, 49, 35, 42, 43, 42, 46, 47, 49, 52, 38, 43, 43, 44, 50, 49, 56, - 50, 42, 43, 44, 50, 51, 57, 52, 53, 41, 45, 46, 53, 53, 56, 53, 54, 46, 46, 51, 49, 56, 53, 58, 58, - ], -]; -pub const W_D_8: [[u8; 63]; 2] = [ - [ - 32, 35, 35, 36, 36, 41, 43, 32, 34, 35, 36, 37, 39, 43, 47, 33, 34, 36, 38, 38, 42, 42, 50, 34, 36, 38, 38, 41, 40, 47, 54, 35, 38, 39, 40, 39, 45, 49, - 58, 38, 39, 40, 39, 46, 47, 54, 60, 38, 39, 41, 46, 46, 48, 57, 62, 40, 41, 44, 45, 49, 54, 63, 63, - ], - [ - 32, 35, 36, 40, 42, 51, 51, 35, 36, 39, 39, 43, 51, 52, 55, 36, 41, 41, 43, 51, 53, 54, 56, 43, 44, 45, 50, 54, 54, 55, 57, 45, 48, 50, 51, 55, 58, 59, - 58, 49, 52, 49, 57, 58, 62, 58, 60, 51, 51, 56, 58, 62, 61, 59, 62, 52, 52, 60, 61, 59, 59, 63, 63, - ], -]; -pub const W_D_9: [[u8; 63]; 2] = [ - [ - 32, 34, 34, 35, 36, 41, 44, 32, 34, 35, 36, 38, 39, 43, 46, 34, 35, 36, 38, 38, 41, 43, 46, 35, 37, 38, 38, 40, 40, 44, 55, 36, 38, 38, 40, 39, 44, 50, - 58, 38, 38, 40, 40, 44, 48, 53, 61, 38, 40, 41, 42, 46, 48, 58, 62, 39, 40, 41, 43, 50, 55, 62, 62, - ], - [ - 32, 35, 36, 40, 42, 51, 51, 35, 36, 39, 39, 43, 51, 52, 55, 36, 41, 41, 43, 51, 53, 54, 56, 43, 44, 45, 50, 54, 54, 55, 57, 45, 48, 50, 51, 55, 58, 59, - 58, 48, 50, 49, 57, 58, 62, 58, 61, 48, 51, 56, 58, 62, 61, 59, 62, 52, 52, 60, 61, 59, 59, 62, 62, - ], -]; -pub const W_D_10: [[u8; 63]; 2] = [ - [ - 32, 36, 36, 40, 40, 55, 60, 34, 36, 37, 40, 41, 48, 57, 82, 35, 36, 41, 41, 46, 52, 73, 82, 37, 40, 42, 45, 50, 65, 80, 87, 39, 41, 44, 49, 62, 78, 88, - 90, 41, 44, 49, 58, 73, 90, 95, 95, 43, 52, 55, 68, 90, 100, 97, 93, 52, 53, 71, 82, 107, 103, 99, 99, - ], - [ - 32, 37, 38, 49, 53, 65, 66, 35, 37, 40, 49, 56, 64, 65, 82, 36, 42, 50, 56, 64, 67, 73, 85, 46, 50, 57, 63, 71, 72, 89, 87, 49, 58, 65, 72, 78, 88, 88, - 90, 60, 64, 74, 81, 84, 90, 95, 134, 62, 74, 77, 80, 90, 114, 129, 125, 74, 74, 90, 100, 128, 125, 116, 116, - ], -]; -pub const W_D_11: [[u8; 63]; 2] = [ - [ - 32, 32, 32, 33, 35, 38, 39, 32, 33, 32, 33, 36, 36, 39, 42, 32, 32, 33, 36, 35, 37, 41, 43, 31, 33, 34, 36, 36, 40, 42, 48, 32, 34, 36, 37, 39, 42, 46, - 51, 36, 37, 37, 39, 41, 46, 51, 55, 37, 39, 41, 41, 47, 50, 55, 56, 41, 42, 41, 44, 50, 53, 60, 60, - ], - [ - 32, 32, 32, 33, 35, 38, 39, 32, 33, 32, 33, 36, 36, 39, 42, 32, 32, 33, 36, 35, 37, 41, 43, 31, 33, 34, 36, 36, 40, 42, 48, 32, 34, 36, 37, 39, 42, 46, - 51, 36, 37, 37, 39, 41, 46, 51, 55, 37, 39, 41, 41, 47, 50, 55, 56, 41, 42, 41, 44, 50, 53, 60, 60, - ], -]; - -/// Table E.1: (codeword, length, amplitude (0 = EOB), Frun, Findex). -pub const AMP_E_1: [(u16, u8, u8, u8, u8); 257] = [ - (0b00, 2, 1, 0, 0), - (0b11111100111, 11, 37, 0, 0), - (0b01, 2, 1, 1, 0), - (0b11111101000, 11, 38, 0, 0), - (0b100, 3, 2, 0, 0), - (0b11111101001, 11, 10, 1, 0), - (0b1010, 4, 3, 0, 0), - (0b11111101010, 11, 11, 1, 0), - (0b1011, 4, 0, 0, 0), - (0b111111010110, 12, 39, 0, 0), - (0b11000, 5, 4, 0, 0), - (0b111111010111, 12, 40, 0, 0), - (0b11001, 5, 5, 0, 0), - (0b111111011000, 12, 41, 0, 0), - (0b11010, 5, 2, 1, 0), - (0b111111011001, 12, 42, 0, 0), - (0b110110, 6, 6, 0, 0), - (0b111111011010, 12, 43, 0, 0), - (0b110111, 6, 7, 0, 0), - (0b111111011011, 12, 44, 0, 0), - (0b111000, 6, 8, 0, 0), - (0b111111011100, 12, 45, 0, 0), - (0b111001, 6, 3, 1, 0), - (0b111111011101, 12, 46, 0, 0), - (0b1110100, 7, 9, 0, 0), - (0b111111011110, 12, 47, 0, 0), - (0b1110101, 7, 10, 0, 0), - (0b111111011111, 12, 48, 0, 0), - (0b1110110, 7, 11, 0, 0), - (0b111111100000, 12, 49, 0, 0), - (0b1110111, 7, 4, 1, 0), - (0b111111100001, 12, 50, 0, 0), - (0b11110000, 8, 12, 0, 0), - (0b111111100010, 12, 12, 1, 0), - (0b11110001, 8, 13, 0, 0), - (0b111111100011, 12, 13, 1, 0), - (0b11110010, 8, 14, 0, 0), - (0b111111100100, 12, 14, 1, 0), - (0b11110011, 8, 15, 0, 0), - (0b111111100101, 12, 15, 1, 0), - (0b11110100, 8, 16, 0, 0), - (0b1111111001100, 13, 51, 0, 0), - (0b11110101, 8, 5, 1, 0), - (0b1111111001101, 13, 52, 0, 0), - (0b111101100, 9, 17, 0, 0), - (0b1111111001110, 13, 53, 0, 0), - (0b111101101, 9, 18, 0, 0), - (0b1111111001111, 13, 54, 0, 0), - (0b111101110, 9, 19, 0, 0), - (0b1111111010000, 13, 55, 0, 0), - (0b111101111, 9, 20, 0, 0), - (0b1111111010001, 13, 56, 0, 0), - (0b111110000, 9, 21, 0, 0), - (0b1111111010010, 13, 57, 0, 0), - (0b111110001, 9, 6, 1, 0), - (0b1111111010011, 13, 58, 0, 0), - (0b111110010, 9, 7, 1, 0), - (0b1111111010100, 13, 59, 0, 0), - (0b1111100110, 10, 22, 0, 0), - (0b1111111010101, 13, 60, 0, 0), - (0b1111100111, 10, 23, 0, 0), - (0b1111111010110, 13, 61, 0, 0), - (0b1111101000, 10, 24, 0, 0), - (0b1111111010111, 13, 62, 0, 0), - (0b1111101001, 10, 25, 0, 0), - (0b1111111011000, 13, 63, 0, 0), - (0b1111101010, 10, 26, 0, 0), - (0b1111111011001, 13, 1, 0, 1), - (0b1111101011, 10, 27, 0, 0), - (0b1111111011010, 13, 16, 1, 0), - (0b1111101100, 10, 28, 0, 0), - (0b1111111011011, 13, 17, 1, 0), - (0b1111101101, 10, 29, 0, 0), - (0b1111111011100, 13, 18, 1, 0), - (0b1111101110, 10, 8, 1, 0), - (0b1111111011101, 13, 19, 1, 0), - (0b1111101111, 10, 9, 1, 0), - (0b11111110111100, 14, 64, 0, 0), - (0b11111100000, 11, 30, 0, 0), - (0b11111110111101, 14, 2, 0, 1), - (0b11111100001, 11, 31, 0, 0), - (0b11111110111110, 14, 3, 0, 1), - (0b11111100010, 11, 32, 0, 0), - (0b11111110111111, 14, 4, 0, 1), - (0b11111100011, 11, 33, 0, 0), - (0b11111111000000, 14, 5, 0, 1), - (0b11111100100, 11, 34, 0, 0), - (0b11111111000001, 14, 6, 0, 1), - (0b11111100101, 11, 35, 0, 0), - (0b11111111000010, 14, 7, 0, 1), - (0b11111100110, 11, 36, 0, 0), - (0b11111111000011, 14, 8, 0, 1), - (0b11111111000100, 14, 9, 0, 1), - (0b1111111110010101, 16, 54, 0, 1), - (0b11111111000101, 14, 10, 0, 1), - (0b1111111110010110, 16, 55, 0, 1), - (0b11111111000110, 14, 11, 0, 1), - (0b1111111110010111, 16, 56, 0, 1), - (0b11111111000111, 14, 12, 0, 1), - (0b1111111110011000, 16, 57, 0, 1), - (0b11111111001000, 14, 13, 0, 1), - (0b1111111110011001, 16, 58, 0, 1), - (0b11111111001001, 14, 14, 0, 1), - (0b1111111110011010, 16, 59, 0, 1), - (0b11111111001010, 14, 15, 0, 1), - (0b1111111110011011, 16, 60, 0, 1), - (0b11111111001011, 14, 16, 0, 1), - (0b1111111110011100, 16, 61, 0, 1), - (0b11111111001100, 14, 17, 0, 1), - (0b1111111110011101, 16, 62, 0, 1), - (0b11111111001101, 14, 20, 1, 0), - (0b1111111110011110, 16, 63, 0, 1), - (0b11111111001110, 14, 21, 1, 0), - (0b1111111110011111, 16, 64, 0, 1), - (0b11111111001111, 14, 22, 1, 0), - (0b1111111110100000, 16, 33, 1, 0), - (0b11111111010000, 14, 23, 1, 0), - (0b1111111110100001, 16, 34, 1, 0), - (0b11111111010001, 14, 24, 1, 0), - (0b1111111110100010, 16, 35, 1, 0), - (0b111111110100100, 15, 18, 0, 1), - (0b1111111110100011, 16, 36, 1, 0), - (0b111111110100101, 15, 19, 0, 1), - (0b1111111110100100, 16, 37, 1, 0), - (0b111111110100110, 15, 20, 0, 1), - (0b1111111110100101, 16, 38, 1, 0), - (0b111111110100111, 15, 21, 0, 1), - (0b1111111110100110, 16, 39, 1, 0), - (0b111111110101000, 15, 22, 0, 1), - (0b1111111110100111, 16, 40, 1, 0), - (0b111111110101001, 15, 23, 0, 1), - (0b1111111110101000, 16, 41, 1, 0), - (0b111111110101010, 15, 24, 0, 1), - (0b1111111110101001, 16, 42, 1, 0), - (0b111111110101011, 15, 25, 0, 1), - (0b1111111110101010, 16, 43, 1, 0), - (0b111111110101100, 15, 26, 0, 1), - (0b1111111110101011, 16, 44, 1, 0), - (0b111111110101101, 15, 27, 0, 1), - (0b1111111110101100, 16, 45, 1, 0), - (0b111111110101110, 15, 28, 0, 1), - (0b1111111110101101, 16, 46, 1, 0), - (0b111111110101111, 15, 29, 0, 1), - (0b1111111110101110, 16, 47, 1, 0), - (0b111111110110000, 15, 30, 0, 1), - (0b1111111110101111, 16, 48, 1, 0), - (0b111111110110001, 15, 31, 0, 1), - (0b1111111110110000, 16, 49, 1, 0), - (0b111111110110010, 15, 32, 0, 1), - (0b1111111110110001, 16, 50, 1, 0), - (0b111111110110011, 15, 33, 0, 1), - (0b1111111110110010, 16, 51, 1, 0), - (0b111111110110100, 15, 34, 0, 1), - (0b1111111110110011, 16, 52, 1, 0), - (0b111111110110101, 15, 35, 0, 1), - (0b1111111110110100, 16, 53, 1, 0), - (0b111111110110110, 15, 36, 0, 1), - (0b1111111110110101, 16, 54, 1, 0), - (0b111111110110111, 15, 37, 0, 1), - (0b1111111110110110, 16, 55, 1, 0), - (0b111111110111000, 15, 38, 0, 1), - (0b1111111110110111, 16, 56, 1, 0), - (0b111111110111001, 15, 39, 0, 1), - (0b1111111110111000, 16, 57, 1, 0), - (0b111111110111010, 15, 40, 0, 1), - (0b1111111110111001, 16, 58, 1, 0), - (0b111111110111011, 15, 41, 0, 1), - (0b1111111110111010, 16, 59, 1, 0), - (0b111111110111100, 15, 42, 0, 1), - (0b1111111110111011, 16, 60, 1, 0), - (0b111111110111101, 15, 25, 1, 0), - (0b1111111110111100, 16, 61, 1, 0), - (0b111111110111110, 15, 26, 1, 0), - (0b1111111110111101, 16, 62, 1, 0), - (0b111111110111111, 15, 27, 1, 0), - (0b1111111110111110, 16, 63, 1, 0), - (0b111111111000000, 15, 28, 1, 0), - (0b1111111110111111, 16, 64, 1, 0), - (0b111111111000001, 15, 29, 1, 0), - (0b1111111111000000, 16, 1, 1, 1), - (0b111111111000010, 15, 30, 1, 0), - (0b1111111111000001, 16, 2, 1, 1), - (0b111111111000011, 15, 31, 1, 0), - (0b1111111111000010, 16, 3, 1, 1), - (0b111111111000100, 15, 32, 1, 0), - (0b1111111111000011, 16, 4, 1, 1), - (0b1111111110001010, 16, 43, 0, 1), - (0b1111111111000100, 16, 5, 1, 1), - (0b1111111110001011, 16, 44, 0, 1), - (0b1111111111000101, 16, 6, 1, 1), - (0b1111111110001100, 16, 45, 0, 1), - (0b1111111111000110, 16, 7, 1, 1), - (0b1111111110001101, 16, 46, 0, 1), - (0b1111111111000111, 16, 8, 1, 1), - (0b1111111110001110, 16, 47, 0, 1), - (0b1111111111001000, 16, 9, 1, 1), - (0b1111111110001111, 16, 48, 0, 1), - (0b1111111111001001, 16, 10, 1, 1), - (0b1111111110010000, 16, 49, 0, 1), - (0b1111111111001010, 16, 11, 1, 1), - (0b1111111110010001, 16, 50, 0, 1), - (0b1111111111001011, 16, 12, 1, 1), - (0b1111111110010010, 16, 51, 0, 1), - (0b1111111111001100, 16, 13, 1, 1), - (0b1111111110010011, 16, 52, 0, 1), - (0b1111111111001101, 16, 14, 1, 1), - (0b1111111110010100, 16, 53, 0, 1), - (0b1111111111001110, 16, 15, 1, 1), - (0b1111111111001111, 16, 16, 1, 1), - (0b1111111111101001, 16, 42, 1, 1), - (0b1111111111010000, 16, 17, 1, 1), - (0b1111111111101010, 16, 43, 1, 1), - (0b1111111111010001, 16, 18, 1, 1), - (0b1111111111101011, 16, 44, 1, 1), - (0b1111111111010010, 16, 19, 1, 1), - (0b1111111111101100, 16, 45, 1, 1), - (0b1111111111010011, 16, 20, 1, 1), - (0b1111111111101101, 16, 46, 1, 1), - (0b1111111111010100, 16, 21, 1, 1), - (0b1111111111101110, 16, 47, 1, 1), - (0b1111111111010101, 16, 22, 1, 1), - (0b1111111111101111, 16, 48, 1, 1), - (0b1111111111010110, 16, 23, 1, 1), - (0b1111111111110000, 16, 49, 1, 1), - (0b1111111111010111, 16, 24, 1, 1), - (0b1111111111110001, 16, 50, 1, 1), - (0b1111111111011000, 16, 25, 1, 1), - (0b1111111111110010, 16, 51, 1, 1), - (0b1111111111011001, 16, 26, 1, 1), - (0b1111111111110011, 16, 52, 1, 1), - (0b1111111111011010, 16, 27, 1, 1), - (0b1111111111110100, 16, 53, 1, 1), - (0b1111111111011011, 16, 28, 1, 1), - (0b1111111111110101, 16, 54, 1, 1), - (0b1111111111011100, 16, 29, 1, 1), - (0b1111111111110110, 16, 55, 1, 1), - (0b1111111111011101, 16, 30, 1, 1), - (0b1111111111110111, 16, 56, 1, 1), - (0b1111111111011110, 16, 31, 1, 1), - (0b1111111111111000, 16, 57, 1, 1), - (0b1111111111011111, 16, 32, 1, 1), - (0b1111111111111001, 16, 58, 1, 1), - (0b1111111111100000, 16, 33, 1, 1), - (0b1111111111111010, 16, 59, 1, 1), - (0b1111111111100001, 16, 34, 1, 1), - (0b1111111111111011, 16, 60, 1, 1), - (0b1111111111100010, 16, 35, 1, 1), - (0b1111111111111100, 16, 61, 1, 1), - (0b1111111111100011, 16, 36, 1, 1), - (0b1111111111111101, 16, 62, 1, 1), - (0b1111111111100100, 16, 37, 1, 1), - (0b1111111111111110, 16, 63, 1, 1), - (0b1111111111100101, 16, 38, 1, 1), - (0b1111111111111111, 16, 64, 1, 1), - (0b1111111111100110, 16, 39, 1, 1), - (0b1111111111100111, 16, 40, 1, 1), - (0b1111111111101000, 16, 41, 1, 1), -]; -/// Table E.2: (codeword, length, run). -pub const RUN_E_1: [(u16, u8, u8); 62] = [ - (0b0, 1, 1), - (0b1111100001, 10, 32), - (0b100, 3, 2), - (0b1111100010, 10, 33), - (0b1010, 4, 3), - (0b1111100011, 10, 34), - (0b1011, 4, 4), - (0b1111100100, 10, 35), - (0b11000, 5, 5), - (0b1111100101, 10, 36), - (0b11001, 5, 6), - (0b1111100110, 10, 37), - (0b11010, 5, 7), - (0b1111100111, 10, 38), - (0b11011, 5, 8), - (0b1111101000, 10, 39), - (0b111000, 6, 9), - (0b1111101001, 10, 40), - (0b111001, 6, 10), - (0b1111101010, 10, 41), - (0b111010, 6, 11), - (0b1111101011, 10, 42), - (0b111011, 6, 12), - (0b1111101100, 10, 43), - (0b1111000, 7, 13), - (0b1111101101, 10, 44), - (0b11110010, 8, 14), - (0b1111101110, 10, 45), - (0b111100110, 9, 15), - (0b1111101111, 10, 46), - (0b111100111, 9, 16), - (0b1111110000, 10, 47), - (0b111101000, 9, 18), - (0b1111110001, 10, 48), - (0b111101001, 9, 20), - (0b1111110010, 10, 49), - (0b1111010100, 10, 17), - (0b1111110011, 10, 50), - (0b1111010101, 10, 19), - (0b1111110100, 10, 51), - (0b1111010110, 10, 21), - (0b1111110101, 10, 52), - (0b1111010111, 10, 22), - (0b1111110110, 10, 53), - (0b1111011000, 10, 23), - (0b1111110111, 10, 54), - (0b1111011001, 10, 24), - (0b1111111000, 10, 55), - (0b1111011010, 10, 25), - (0b1111111001, 10, 56), - (0b1111011011, 10, 26), - (0b1111111010, 10, 57), - (0b1111011100, 10, 27), - (0b1111111011, 10, 58), - (0b1111011101, 10, 28), - (0b1111111100, 10, 59), - (0b1111011110, 10, 29), - (0b1111111101, 10, 60), - (0b1111011111, 10, 30), - (0b1111111110, 10, 61), - (0b1111100000, 10, 31), - (0b1111111111, 10, 62), -]; -/// Table E.3: (codeword, length) indexed by the DC size (eta). -pub const DC_E_1: [(u16, u8); 14] = [ - (0b1010, 4), - (0b111110, 6), - (0b1011, 4), - (0b1100, 4), - (0b1101, 4), - (0b000, 3), - (0b001, 3), - (0b010, 3), - (0b011, 3), - (0b100, 3), - (0b1110, 4), - (0b11110, 5), - (0b1111110, 7), - (0b1111111, 7), -]; - -/// Table E.4: (codeword, length, amplitude (0 = EOB), Frun, Findex). -pub const AMP_E_4: [(u16, u8, u8, u8, u8); 257] = [ - (0b00, 2, 1, 0, 0), - (0b1111111011101, 13, 15, 1, 0), - (0b01, 2, 1, 1, 0), - (0b1111111011110, 13, 16, 1, 0), - (0b100, 3, 2, 0, 0), - (0b11111110111110, 14, 42, 0, 0), - (0b101, 3, 0, 0, 0), - (0b11111110111111, 14, 43, 0, 0), - (0b1100, 4, 3, 0, 0), - (0b11111111000000, 14, 44, 0, 0), - (0b11010, 5, 4, 0, 0), - (0b11111111000001, 14, 45, 0, 0), - (0b11011, 5, 2, 1, 0), - (0b11111111000010, 14, 46, 0, 0), - (0b111000, 6, 5, 0, 0), - (0b11111111000011, 14, 47, 0, 0), - (0b111001, 6, 6, 0, 0), - (0b11111111000100, 14, 48, 0, 0), - (0b111010, 6, 7, 0, 0), - (0b11111111000101, 14, 49, 0, 0), - (0b111011, 6, 3, 1, 0), - (0b11111111000110, 14, 50, 0, 0), - (0b1111000, 7, 8, 0, 0), - (0b11111111000111, 14, 51, 0, 0), - (0b1111001, 7, 9, 0, 0), - (0b11111111001000, 14, 52, 0, 0), - (0b11110100, 8, 10, 0, 0), - (0b11111111001001, 14, 17, 1, 0), - (0b11110101, 8, 11, 0, 0), - (0b11111111001010, 14, 18, 1, 0), - (0b11110110, 8, 12, 0, 0), - (0b11111111001011, 14, 19, 1, 0), - (0b11110111, 8, 4, 1, 0), - (0b11111111001100, 14, 20, 1, 0), - (0b11111000, 8, 5, 1, 0), - (0b11111111001101, 14, 21, 1, 0), - (0b111110010, 9, 13, 0, 0), - (0b111111110011100, 15, 53, 0, 0), - (0b111110011, 9, 14, 0, 0), - (0b111111110011101, 15, 54, 0, 0), - (0b111110100, 9, 15, 0, 0), - (0b111111110011110, 15, 55, 0, 0), - (0b111110101, 9, 16, 0, 0), - (0b111111110011111, 15, 56, 0, 0), - (0b111110110, 9, 6, 1, 0), - (0b111111110100000, 15, 57, 0, 0), - (0b1111101110, 10, 17, 0, 0), - (0b111111110100001, 15, 58, 0, 0), - (0b1111101111, 10, 18, 0, 0), - (0b111111110100010, 15, 59, 0, 0), - (0b1111110000, 10, 19, 0, 0), - (0b111111110100011, 15, 60, 0, 0), - (0b1111110001, 10, 20, 0, 0), - (0b111111110100100, 15, 61, 0, 0), - (0b1111110010, 10, 21, 0, 0), - (0b111111110100101, 15, 64, 0, 0), - (0b1111110011, 10, 7, 1, 0), - (0b111111110100110, 15, 1, 0, 1), - (0b11111101000, 11, 22, 0, 0), - (0b111111110100111, 15, 22, 1, 0), - (0b11111101001, 11, 23, 0, 0), - (0b111111110101000, 15, 23, 1, 0), - (0b11111101010, 11, 24, 0, 0), - (0b111111110101001, 15, 24, 1, 0), - (0b11111101011, 11, 25, 0, 0), - (0b111111110101010, 15, 25, 1, 0), - (0b11111101100, 11, 26, 0, 0), - (0b111111110101011, 15, 26, 1, 0), - (0b11111101101, 11, 27, 0, 0), - (0b111111110101100, 15, 27, 1, 0), - (0b11111101110, 11, 8, 1, 0), - (0b1111111101011010, 16, 62, 0, 0), - (0b11111101111, 11, 9, 1, 0), - (0b1111111101011011, 16, 63, 0, 0), - (0b111111100000, 12, 28, 0, 0), - (0b1111111101011100, 16, 2, 0, 1), - (0b111111100001, 12, 29, 0, 0), - (0b1111111101011101, 16, 3, 0, 1), - (0b111111100010, 12, 30, 0, 0), - (0b1111111101011110, 16, 4, 0, 1), - (0b111111100011, 12, 31, 0, 0), - (0b1111111101011111, 16, 5, 0, 1), - (0b111111100100, 12, 32, 0, 0), - (0b1111111101100000, 16, 6, 0, 1), - (0b111111100101, 12, 33, 0, 0), - (0b1111111101100001, 16, 7, 0, 1), - (0b111111100110, 12, 34, 0, 0), - (0b1111111101100010, 16, 8, 0, 1), - (0b111111100111, 12, 10, 1, 0), - (0b1111111101100011, 16, 9, 0, 1), - (0b111111101000, 12, 11, 1, 0), - (0b1111111101100100, 16, 10, 0, 1), - (0b111111101001, 12, 12, 1, 0), - (0b1111111101100101, 16, 11, 0, 1), - (0b1111111010100, 13, 35, 0, 0), - (0b1111111101100110, 16, 12, 0, 1), - (0b1111111010101, 13, 36, 0, 0), - (0b1111111101100111, 16, 13, 0, 1), - (0b1111111010110, 13, 37, 0, 0), - (0b1111111101101000, 16, 14, 0, 1), - (0b1111111010111, 13, 38, 0, 0), - (0b1111111101101001, 16, 15, 0, 1), - (0b1111111011000, 13, 39, 0, 0), - (0b1111111101101010, 16, 16, 0, 1), - (0b1111111011001, 13, 40, 0, 0), - (0b1111111101101011, 16, 17, 0, 1), - (0b1111111011010, 13, 41, 0, 0), - (0b1111111101101100, 16, 18, 0, 1), - (0b1111111011011, 13, 13, 1, 0), - (0b1111111101101101, 16, 19, 0, 1), - (0b1111111011100, 13, 14, 1, 0), - (0b1111111101101110, 16, 20, 0, 1), - (0b1111111101101111, 16, 21, 0, 1), - (0b1111111110101101, 16, 46, 1, 0), - (0b1111111101110000, 16, 22, 0, 1), - (0b1111111110101110, 16, 47, 1, 0), - (0b1111111101110001, 16, 23, 0, 1), - (0b1111111110101111, 16, 48, 1, 0), - (0b1111111101110010, 16, 24, 0, 1), - (0b1111111110110000, 16, 49, 1, 0), - (0b1111111101110011, 16, 25, 0, 1), - (0b1111111110110001, 16, 50, 1, 0), - (0b1111111101110100, 16, 26, 0, 1), - (0b1111111110110010, 16, 51, 1, 0), - (0b1111111101110101, 16, 27, 0, 1), - (0b1111111110110011, 16, 52, 1, 0), - (0b1111111101110110, 16, 28, 0, 1), - (0b1111111110110100, 16, 53, 1, 0), - (0b1111111101110111, 16, 29, 0, 1), - (0b1111111110110101, 16, 54, 1, 0), - (0b1111111101111000, 16, 30, 0, 1), - (0b1111111110110110, 16, 55, 1, 0), - (0b1111111101111001, 16, 31, 0, 1), - (0b1111111110110111, 16, 56, 1, 0), - (0b1111111101111010, 16, 32, 0, 1), - (0b1111111110111000, 16, 57, 1, 0), - (0b1111111101111011, 16, 33, 0, 1), - (0b1111111110111001, 16, 58, 1, 0), - (0b1111111101111100, 16, 34, 0, 1), - (0b1111111110111010, 16, 59, 1, 0), - (0b1111111101111101, 16, 35, 0, 1), - (0b1111111110111011, 16, 60, 1, 0), - (0b1111111101111110, 16, 36, 0, 1), - (0b1111111110111100, 16, 61, 1, 0), - (0b1111111101111111, 16, 37, 0, 1), - (0b1111111110111101, 16, 62, 1, 0), - (0b1111111110000000, 16, 38, 0, 1), - (0b1111111110111110, 16, 63, 1, 0), - (0b1111111110000001, 16, 39, 0, 1), - (0b1111111110111111, 16, 64, 1, 0), - (0b1111111110000010, 16, 40, 0, 1), - (0b1111111111000000, 16, 1, 1, 1), - (0b1111111110000011, 16, 41, 0, 1), - (0b1111111111000001, 16, 2, 1, 1), - (0b1111111110000100, 16, 42, 0, 1), - (0b1111111111000010, 16, 3, 1, 1), - (0b1111111110000101, 16, 43, 0, 1), - (0b1111111111000011, 16, 4, 1, 1), - (0b1111111110000110, 16, 44, 0, 1), - (0b1111111111000100, 16, 5, 1, 1), - (0b1111111110000111, 16, 45, 0, 1), - (0b1111111111000101, 16, 6, 1, 1), - (0b1111111110001000, 16, 46, 0, 1), - (0b1111111111000110, 16, 7, 1, 1), - (0b1111111110001001, 16, 47, 0, 1), - (0b1111111111000111, 16, 8, 1, 1), - (0b1111111110001010, 16, 48, 0, 1), - (0b1111111111001000, 16, 9, 1, 1), - (0b1111111110001011, 16, 49, 0, 1), - (0b1111111111001001, 16, 10, 1, 1), - (0b1111111110001100, 16, 50, 0, 1), - (0b1111111111001010, 16, 11, 1, 1), - (0b1111111110001101, 16, 51, 0, 1), - (0b1111111111001011, 16, 12, 1, 1), - (0b1111111110001110, 16, 52, 0, 1), - (0b1111111111001100, 16, 13, 1, 1), - (0b1111111110001111, 16, 53, 0, 1), - (0b1111111111001101, 16, 14, 1, 1), - (0b1111111110010000, 16, 54, 0, 1), - (0b1111111111001110, 16, 15, 1, 1), - (0b1111111110010001, 16, 55, 0, 1), - (0b1111111111001111, 16, 16, 1, 1), - (0b1111111110010010, 16, 56, 0, 1), - (0b1111111111010000, 16, 17, 1, 1), - (0b1111111110010011, 16, 57, 0, 1), - (0b1111111111010001, 16, 18, 1, 1), - (0b1111111110010100, 16, 58, 0, 1), - (0b1111111111010010, 16, 19, 1, 1), - (0b1111111110010101, 16, 59, 0, 1), - (0b1111111111010011, 16, 20, 1, 1), - (0b1111111110010110, 16, 60, 0, 1), - (0b1111111111010100, 16, 21, 1, 1), - (0b1111111110010111, 16, 61, 0, 1), - (0b1111111111010101, 16, 22, 1, 1), - (0b1111111110011000, 16, 62, 0, 1), - (0b1111111111010110, 16, 23, 1, 1), - (0b1111111110011001, 16, 63, 0, 1), - (0b1111111111010111, 16, 24, 1, 1), - (0b1111111110011010, 16, 64, 0, 1), - (0b1111111111011000, 16, 25, 1, 1), - (0b1111111110011011, 16, 28, 1, 0), - (0b1111111111011001, 16, 26, 1, 1), - (0b1111111110011100, 16, 29, 1, 0), - (0b1111111111011010, 16, 27, 1, 1), - (0b1111111110011101, 16, 30, 1, 0), - (0b1111111111011011, 16, 28, 1, 1), - (0b1111111110011110, 16, 31, 1, 0), - (0b1111111111011100, 16, 29, 1, 1), - (0b1111111110011111, 16, 32, 1, 0), - (0b1111111111011101, 16, 30, 1, 1), - (0b1111111110100000, 16, 33, 1, 0), - (0b1111111111011110, 16, 31, 1, 1), - (0b1111111110100001, 16, 34, 1, 0), - (0b1111111111011111, 16, 32, 1, 1), - (0b1111111110100010, 16, 35, 1, 0), - (0b1111111111100000, 16, 33, 1, 1), - (0b1111111110100011, 16, 36, 1, 0), - (0b1111111111100001, 16, 34, 1, 1), - (0b1111111110100100, 16, 37, 1, 0), - (0b1111111111100010, 16, 35, 1, 1), - (0b1111111110100101, 16, 38, 1, 0), - (0b1111111111100011, 16, 36, 1, 1), - (0b1111111110100110, 16, 39, 1, 0), - (0b1111111111100100, 16, 37, 1, 1), - (0b1111111110100111, 16, 40, 1, 0), - (0b1111111111100101, 16, 38, 1, 1), - (0b1111111110101000, 16, 41, 1, 0), - (0b1111111111100110, 16, 39, 1, 1), - (0b1111111110101001, 16, 42, 1, 0), - (0b1111111111100111, 16, 40, 1, 1), - (0b1111111110101010, 16, 43, 1, 0), - (0b1111111111101000, 16, 41, 1, 1), - (0b1111111110101011, 16, 44, 1, 0), - (0b1111111111101001, 16, 42, 1, 1), - (0b1111111110101100, 16, 45, 1, 0), - (0b1111111111101010, 16, 43, 1, 1), - (0b1111111111101011, 16, 44, 1, 1), - (0b1111111111110110, 16, 55, 1, 1), - (0b1111111111101100, 16, 45, 1, 1), - (0b1111111111110111, 16, 56, 1, 1), - (0b1111111111101101, 16, 46, 1, 1), - (0b1111111111111000, 16, 57, 1, 1), - (0b1111111111101110, 16, 47, 1, 1), - (0b1111111111111001, 16, 58, 1, 1), - (0b1111111111101111, 16, 48, 1, 1), - (0b1111111111111010, 16, 59, 1, 1), - (0b1111111111110000, 16, 49, 1, 1), - (0b1111111111111011, 16, 60, 1, 1), - (0b1111111111110001, 16, 50, 1, 1), - (0b1111111111111100, 16, 61, 1, 1), - (0b1111111111110010, 16, 51, 1, 1), - (0b1111111111111101, 16, 62, 1, 1), - (0b1111111111110011, 16, 52, 1, 1), - (0b1111111111111110, 16, 63, 1, 1), - (0b1111111111110100, 16, 53, 1, 1), - (0b1111111111111111, 16, 64, 1, 1), - (0b1111111111110101, 16, 54, 1, 1), -]; -/// Table E.5: (codeword, length, run). -pub const RUN_E_4: [(u16, u8, u8); 62] = [ - (0b0, 1, 1), - (0b1111100001, 10, 25), - (0b100, 3, 2), - (0b1111100010, 10, 26), - (0b1010, 4, 3), - (0b1111100011, 10, 27), - (0b1011, 4, 4), - (0b1111100100, 10, 28), - (0b11000, 5, 5), - (0b1111100101, 10, 29), - (0b11001, 5, 6), - (0b1111100110, 10, 30), - (0b11010, 5, 7), - (0b1111100111, 10, 31), - (0b110110, 6, 8), - (0b1111101000, 10, 32), - (0b110111, 6, 9), - (0b1111101001, 10, 33), - (0b111000, 6, 10), - (0b1111101010, 10, 34), - (0b111001, 6, 11), - (0b1111101011, 10, 35), - (0b111010, 6, 12), - (0b1111101100, 10, 36), - (0b1110110, 7, 13), - (0b1111101101, 10, 37), - (0b1110111, 7, 14), - (0b1111101110, 10, 38), - (0b11110000, 8, 15), - (0b1111101111, 10, 39), - (0b111100010, 9, 16), - (0b1111110000, 10, 40), - (0b111100011, 9, 17), - (0b1111110001, 10, 41), - (0b111100100, 9, 18), - (0b1111110010, 10, 42), - (0b111100101, 9, 19), - (0b1111110011, 10, 43), - (0b111100110, 9, 20), - (0b1111110100, 10, 44), - (0b111100111, 9, 21), - (0b1111110101, 10, 45), - (0b111101000, 9, 53), - (0b1111110110, 10, 46), - (0b111101001, 9, 57), - (0b1111110111, 10, 47), - (0b111101010, 9, 58), - (0b1111111000, 10, 48), - (0b111101011, 9, 59), - (0b1111111001, 10, 49), - (0b111101100, 9, 60), - (0b1111111010, 10, 50), - (0b111101101, 9, 61), - (0b1111111011, 10, 51), - (0b111101110, 9, 62), - (0b1111111100, 10, 52), - (0b1111011110, 10, 22), - (0b1111111101, 10, 54), - (0b1111011111, 10, 23), - (0b1111111110, 10, 55), - (0b1111100000, 10, 24), - (0b1111111111, 10, 56), -]; -/// Table E.6: (codeword, length) indexed by the DC size (eta). -pub const DC_E_4: [(u16, u8); 12] = - [(0b000, 3), (0b1100, 4), (0b1101, 4), (0b001, 3), (0b010, 3), (0b011, 3), (0b100, 3), (0b101, 3), (0b1110, 4), (0b11110, 5), (0b111110, 6), (0b111111, 6)]; - -/// Table E.7: (codeword, length, amplitude (0 = EOB), Frun, Findex). -pub const AMP_E_7: [(u16, u8, u8, u8, u8); 257] = [ - (0b00, 2, 1, 0, 0), - (0b111111011111, 12, 45, 0, 0), - (0b01, 2, 1, 1, 0), - (0b111111100000, 12, 46, 0, 0), - (0b100, 3, 2, 0, 0), - (0b111111100001, 12, 47, 0, 0), - (0b1010, 4, 3, 0, 0), - (0b111111100010, 12, 48, 0, 0), - (0b1011, 4, 0, 0, 0), - (0b111111100011, 12, 12, 1, 0), - (0b11000, 5, 4, 0, 0), - (0b111111100100, 12, 13, 1, 0), - (0b11001, 5, 5, 0, 0), - (0b111111100101, 12, 14, 1, 0), - (0b11010, 5, 2, 1, 0), - (0b1111111001100, 13, 49, 0, 0), - (0b110110, 6, 6, 0, 0), - (0b1111111001101, 13, 50, 0, 0), - (0b110111, 6, 7, 0, 0), - (0b1111111001110, 13, 51, 0, 0), - (0b111000, 6, 8, 0, 0), - (0b1111111001111, 13, 52, 0, 0), - (0b111001, 6, 3, 1, 0), - (0b1111111010000, 13, 53, 0, 0), - (0b1110100, 7, 9, 0, 0), - (0b1111111010001, 13, 54, 0, 0), - (0b1110101, 7, 10, 0, 0), - (0b1111111010010, 13, 55, 0, 0), - (0b1110110, 7, 11, 0, 0), - (0b1111111010011, 13, 56, 0, 0), - (0b1110111, 7, 4, 1, 0), - (0b1111111010100, 13, 57, 0, 0), - (0b11110000, 8, 12, 0, 0), - (0b1111111010101, 13, 58, 0, 0), - (0b11110001, 8, 13, 0, 0), - (0b1111111010110, 13, 59, 0, 0), - (0b11110010, 8, 14, 0, 0), - (0b1111111010111, 13, 60, 0, 0), - (0b11110011, 8, 15, 0, 0), - (0b1111111011000, 13, 61, 0, 0), - (0b11110100, 8, 16, 0, 0), - (0b1111111011001, 13, 15, 1, 0), - (0b11110101, 8, 5, 1, 0), - (0b1111111011010, 13, 16, 1, 0), - (0b111101100, 9, 17, 0, 0), - (0b1111111011011, 13, 17, 1, 0), - (0b111101101, 9, 18, 0, 0), - (0b1111111011100, 13, 18, 1, 0), - (0b111101110, 9, 19, 0, 0), - (0b11111110111010, 14, 62, 0, 0), - (0b111101111, 9, 20, 0, 0), - (0b11111110111011, 14, 63, 0, 0), - (0b111110000, 9, 21, 0, 0), - (0b11111110111100, 14, 64, 0, 0), - (0b111110001, 9, 22, 0, 0), - (0b11111110111101, 14, 1, 0, 1), - (0b111110010, 9, 6, 1, 0), - (0b11111110111110, 14, 2, 0, 1), - (0b111110011, 9, 7, 1, 0), - (0b11111110111111, 14, 3, 0, 1), - (0b1111101000, 10, 23, 0, 0), - (0b11111111000000, 14, 4, 0, 1), - (0b1111101001, 10, 24, 0, 0), - (0b11111111000001, 14, 5, 0, 1), - (0b1111101010, 10, 25, 0, 0), - (0b11111111000010, 14, 6, 0, 1), - (0b1111101011, 10, 26, 0, 0), - (0b11111111000011, 14, 7, 0, 1), - (0b1111101100, 10, 27, 0, 0), - (0b11111111000100, 14, 8, 0, 1), - (0b1111101101, 10, 28, 0, 0), - (0b11111111000101, 14, 9, 0, 1), - (0b1111101110, 10, 29, 0, 0), - (0b11111111000110, 14, 10, 0, 1), - (0b1111101111, 10, 8, 1, 0), - (0b11111111000111, 14, 11, 0, 1), - (0b1111110000, 10, 9, 1, 0), - (0b11111111001000, 14, 12, 0, 1), - (0b11111100010, 11, 30, 0, 0), - (0b11111111001001, 14, 13, 0, 1), - (0b11111100011, 11, 31, 0, 0), - (0b11111111001010, 14, 14, 0, 1), - (0b11111100100, 11, 32, 0, 0), - (0b11111111001011, 14, 15, 0, 1), - (0b11111100101, 11, 33, 0, 0), - (0b11111111001100, 14, 16, 0, 1), - (0b11111100110, 11, 34, 0, 0), - (0b11111111001101, 14, 19, 1, 0), - (0b11111100111, 11, 35, 0, 0), - (0b11111111001110, 14, 20, 1, 0), - (0b11111101000, 11, 36, 0, 0), - (0b11111111001111, 14, 21, 1, 0), - (0b11111101001, 11, 37, 0, 0), - (0b11111111010000, 14, 22, 1, 0), - (0b11111101010, 11, 10, 1, 0), - (0b11111111010001, 14, 23, 1, 0), - (0b11111101011, 11, 11, 1, 0), - (0b11111111010010, 14, 24, 1, 0), - (0b111111011000, 12, 38, 0, 0), - (0b111111110100110, 15, 17, 0, 1), - (0b111111011001, 12, 39, 0, 0), - (0b111111110100111, 15, 18, 0, 1), - (0b111111011010, 12, 40, 0, 0), - (0b111111110101000, 15, 19, 0, 1), - (0b111111011011, 12, 41, 0, 0), - (0b111111110101001, 15, 20, 0, 1), - (0b111111011100, 12, 42, 0, 0), - (0b111111110101010, 15, 21, 0, 1), - (0b111111011101, 12, 43, 0, 0), - (0b111111110101011, 15, 22, 0, 1), - (0b111111011110, 12, 44, 0, 0), - (0b111111110101100, 15, 23, 0, 1), - (0b111111110101101, 15, 24, 0, 1), - (0b1111111110101101, 16, 46, 1, 0), - (0b111111110101110, 15, 25, 0, 1), - (0b1111111110101110, 16, 47, 1, 0), - (0b111111110101111, 15, 26, 0, 1), - (0b1111111110101111, 16, 48, 1, 0), - (0b111111110110000, 15, 27, 0, 1), - (0b1111111110110000, 16, 49, 1, 0), - (0b111111110110001, 15, 28, 0, 1), - (0b1111111110110001, 16, 50, 1, 0), - (0b111111110110010, 15, 29, 0, 1), - (0b1111111110110010, 16, 51, 1, 0), - (0b111111110110011, 15, 30, 0, 1), - (0b1111111110110011, 16, 52, 1, 0), - (0b111111110110100, 15, 31, 0, 1), - (0b1111111110110100, 16, 53, 1, 0), - (0b111111110110101, 15, 32, 0, 1), - (0b1111111110110101, 16, 54, 1, 0), - (0b111111110110110, 15, 33, 0, 1), - (0b1111111110110110, 16, 55, 1, 0), - (0b111111110110111, 15, 34, 0, 1), - (0b1111111110110111, 16, 56, 1, 0), - (0b111111110111000, 15, 35, 0, 1), - (0b1111111110111000, 16, 57, 1, 0), - (0b111111110111001, 15, 36, 0, 1), - (0b1111111110111001, 16, 58, 1, 0), - (0b111111110111010, 15, 37, 0, 1), - (0b1111111110111010, 16, 59, 1, 0), - (0b111111110111011, 15, 40, 0, 1), - (0b1111111110111011, 16, 60, 1, 0), - (0b111111110111100, 15, 25, 1, 0), - (0b1111111110111100, 16, 61, 1, 0), - (0b111111110111101, 15, 26, 1, 0), - (0b1111111110111101, 16, 62, 1, 0), - (0b111111110111110, 15, 27, 1, 0), - (0b1111111110111110, 16, 63, 1, 0), - (0b111111110111111, 15, 28, 1, 0), - (0b1111111110111111, 16, 64, 1, 0), - (0b111111111000000, 15, 29, 1, 0), - (0b1111111111000000, 16, 1, 1, 1), - (0b111111111000001, 15, 30, 1, 0), - (0b1111111111000001, 16, 2, 1, 1), - (0b1111111110000100, 16, 38, 0, 1), - (0b1111111111000010, 16, 3, 1, 1), - (0b1111111110000101, 16, 39, 0, 1), - (0b1111111111000011, 16, 4, 1, 1), - (0b1111111110000110, 16, 41, 0, 1), - (0b1111111111000100, 16, 5, 1, 1), - (0b1111111110000111, 16, 42, 0, 1), - (0b1111111111000101, 16, 6, 1, 1), - (0b1111111110001000, 16, 43, 0, 1), - (0b1111111111000110, 16, 7, 1, 1), - (0b1111111110001001, 16, 44, 0, 1), - (0b1111111111000111, 16, 8, 1, 1), - (0b1111111110001010, 16, 45, 0, 1), - (0b1111111111001000, 16, 9, 1, 1), - (0b1111111110001011, 16, 46, 0, 1), - (0b1111111111001001, 16, 10, 1, 1), - (0b1111111110001100, 16, 47, 0, 1), - (0b1111111111001010, 16, 11, 1, 1), - (0b1111111110001101, 16, 48, 0, 1), - (0b1111111111001011, 16, 12, 1, 1), - (0b1111111110001110, 16, 49, 0, 1), - (0b1111111111001100, 16, 13, 1, 1), - (0b1111111110001111, 16, 50, 0, 1), - (0b1111111111001101, 16, 14, 1, 1), - (0b1111111110010000, 16, 51, 0, 1), - (0b1111111111001110, 16, 15, 1, 1), - (0b1111111110010001, 16, 52, 0, 1), - (0b1111111111001111, 16, 16, 1, 1), - (0b1111111110010010, 16, 53, 0, 1), - (0b1111111111010000, 16, 17, 1, 1), - (0b1111111110010011, 16, 54, 0, 1), - (0b1111111111010001, 16, 18, 1, 1), - (0b1111111110010100, 16, 55, 0, 1), - (0b1111111111010010, 16, 19, 1, 1), - (0b1111111110010101, 16, 56, 0, 1), - (0b1111111111010011, 16, 20, 1, 1), - (0b1111111110010110, 16, 57, 0, 1), - (0b1111111111010100, 16, 21, 1, 1), - (0b1111111110010111, 16, 58, 0, 1), - (0b1111111111010101, 16, 22, 1, 1), - (0b1111111110011000, 16, 59, 0, 1), - (0b1111111111010110, 16, 23, 1, 1), - (0b1111111110011001, 16, 60, 0, 1), - (0b1111111111010111, 16, 24, 1, 1), - (0b1111111110011010, 16, 61, 0, 1), - (0b1111111111011000, 16, 25, 1, 1), - (0b1111111110011011, 16, 62, 0, 1), - (0b1111111111011001, 16, 26, 1, 1), - (0b1111111110011100, 16, 63, 0, 1), - (0b1111111111011010, 16, 27, 1, 1), - (0b1111111110011101, 16, 64, 0, 1), - (0b1111111111011011, 16, 28, 1, 1), - (0b1111111110011110, 16, 31, 1, 0), - (0b1111111111011100, 16, 29, 1, 1), - (0b1111111110011111, 16, 32, 1, 0), - (0b1111111111011101, 16, 30, 1, 1), - (0b1111111110100000, 16, 33, 1, 0), - (0b1111111111011110, 16, 31, 1, 1), - (0b1111111110100001, 16, 34, 1, 0), - (0b1111111111011111, 16, 32, 1, 1), - (0b1111111110100010, 16, 35, 1, 0), - (0b1111111111100000, 16, 33, 1, 1), - (0b1111111110100011, 16, 36, 1, 0), - (0b1111111111100001, 16, 34, 1, 1), - (0b1111111110100100, 16, 37, 1, 0), - (0b1111111111100010, 16, 35, 1, 1), - (0b1111111110100101, 16, 38, 1, 0), - (0b1111111111100011, 16, 36, 1, 1), - (0b1111111110100110, 16, 39, 1, 0), - (0b1111111111100100, 16, 37, 1, 1), - (0b1111111110100111, 16, 40, 1, 0), - (0b1111111111100101, 16, 38, 1, 1), - (0b1111111110101000, 16, 41, 1, 0), - (0b1111111111100110, 16, 39, 1, 1), - (0b1111111110101001, 16, 42, 1, 0), - (0b1111111111100111, 16, 40, 1, 1), - (0b1111111110101010, 16, 43, 1, 0), - (0b1111111111101000, 16, 41, 1, 1), - (0b1111111110101011, 16, 44, 1, 0), - (0b1111111111101001, 16, 42, 1, 1), - (0b1111111110101100, 16, 45, 1, 0), - (0b1111111111101010, 16, 43, 1, 1), - (0b1111111111101011, 16, 44, 1, 1), - (0b1111111111110110, 16, 55, 1, 1), - (0b1111111111101100, 16, 45, 1, 1), - (0b1111111111110111, 16, 56, 1, 1), - (0b1111111111101101, 16, 46, 1, 1), - (0b1111111111111000, 16, 57, 1, 1), - (0b1111111111101110, 16, 47, 1, 1), - (0b1111111111111001, 16, 58, 1, 1), - (0b1111111111101111, 16, 48, 1, 1), - (0b1111111111111010, 16, 59, 1, 1), - (0b1111111111110000, 16, 49, 1, 1), - (0b1111111111111011, 16, 60, 1, 1), - (0b1111111111110001, 16, 50, 1, 1), - (0b1111111111111100, 16, 61, 1, 1), - (0b1111111111110010, 16, 51, 1, 1), - (0b1111111111111101, 16, 62, 1, 1), - (0b1111111111110011, 16, 52, 1, 1), - (0b1111111111111110, 16, 63, 1, 1), - (0b1111111111110100, 16, 53, 1, 1), - (0b1111111111111111, 16, 64, 1, 1), - (0b1111111111110101, 16, 54, 1, 1), -]; -/// Table E.8: (codeword, length, run). -pub const RUN_E_7: [(u16, u8, u8); 62] = [ - (0b0, 1, 1), - (0b1111100001, 10, 32), - (0b100, 3, 2), - (0b1111100010, 10, 33), - (0b1010, 4, 3), - (0b1111100011, 10, 34), - (0b1011, 4, 4), - (0b1111100100, 10, 35), - (0b11000, 5, 5), - (0b1111100101, 10, 36), - (0b11001, 5, 6), - (0b1111100110, 10, 37), - (0b11010, 5, 7), - (0b1111100111, 10, 38), - (0b11011, 5, 8), - (0b1111101000, 10, 39), - (0b111000, 6, 9), - (0b1111101001, 10, 40), - (0b111001, 6, 10), - (0b1111101010, 10, 41), - (0b111010, 6, 11), - (0b1111101011, 10, 42), - (0b111011, 6, 12), - (0b1111101100, 10, 43), - (0b1111000, 7, 13), - (0b1111101101, 10, 44), - (0b11110010, 8, 14), - (0b1111101110, 10, 45), - (0b111100110, 9, 15), - (0b1111101111, 10, 46), - (0b111100111, 9, 16), - (0b1111110000, 10, 47), - (0b111101000, 9, 20), - (0b1111110001, 10, 48), - (0b111101001, 9, 21), - (0b1111110010, 10, 49), - (0b1111010100, 10, 17), - (0b1111110011, 10, 50), - (0b1111010101, 10, 18), - (0b1111110100, 10, 51), - (0b1111010110, 10, 19), - (0b1111110101, 10, 52), - (0b1111010111, 10, 22), - (0b1111110110, 10, 53), - (0b1111011000, 10, 23), - (0b1111110111, 10, 54), - (0b1111011001, 10, 24), - (0b1111111000, 10, 55), - (0b1111011010, 10, 25), - (0b1111111001, 10, 56), - (0b1111011011, 10, 26), - (0b1111111010, 10, 57), - (0b1111011100, 10, 27), - (0b1111111011, 10, 58), - (0b1111011101, 10, 28), - (0b1111111100, 10, 59), - (0b1111011110, 10, 29), - (0b1111111101, 10, 60), - (0b1111011111, 10, 30), - (0b1111111110, 10, 61), - (0b1111100000, 10, 31), - (0b1111111111, 10, 62), -]; -/// Table E.9: (codeword, length) indexed by the DC size (eta). -pub const DC_E_7: [(u16, u8); 12] = - [(0b000, 3), (0b1100, 4), (0b1101, 4), (0b001, 3), (0b010, 3), (0b011, 3), (0b100, 3), (0b101, 3), (0b1110, 4), (0b11110, 5), (0b111110, 6), (0b111111, 6)]; - -/// Table E.10: (codeword, length, amplitude (0 = EOB), Frun, Findex). -pub const AMP_E_10: [(u16, u8, u8, u8, u8); 257] = [ - (0b00, 2, 1, 0, 0), - (0b111111011011, 12, 47, 0, 0), - (0b01, 2, 1, 1, 0), - (0b111111011100, 12, 48, 0, 0), - (0b100, 3, 2, 0, 0), - (0b111111011101, 12, 49, 0, 0), - (0b1010, 4, 3, 0, 0), - (0b111111011110, 12, 50, 0, 0), - (0b1011, 4, 0, 0, 0), - (0b111111011111, 12, 51, 0, 0), - (0b11000, 5, 4, 0, 0), - (0b111111100000, 12, 52, 0, 0), - (0b11001, 5, 5, 0, 0), - (0b111111100001, 12, 11, 1, 0), - (0b11010, 5, 2, 1, 0), - (0b111111100010, 12, 12, 1, 0), - (0b110110, 6, 6, 0, 0), - (0b111111100011, 12, 13, 1, 0), - (0b110111, 6, 7, 0, 0), - (0b1111111001000, 13, 53, 0, 0), - (0b111000, 6, 8, 0, 0), - (0b1111111001001, 13, 54, 0, 0), - (0b111001, 6, 3, 1, 0), - (0b1111111001010, 13, 55, 0, 0), - (0b1110100, 7, 9, 0, 0), - (0b1111111001011, 13, 56, 0, 0), - (0b1110101, 7, 10, 0, 0), - (0b1111111001100, 13, 57, 0, 0), - (0b1110110, 7, 11, 0, 0), - (0b1111111001101, 13, 58, 0, 0), - (0b1110111, 7, 4, 1, 0), - (0b1111111001110, 13, 59, 0, 0), - (0b11110000, 8, 12, 0, 0), - (0b1111111001111, 13, 60, 0, 0), - (0b11110001, 8, 13, 0, 0), - (0b1111111010000, 13, 61, 0, 0), - (0b11110010, 8, 14, 0, 0), - (0b1111111010001, 13, 62, 0, 0), - (0b11110011, 8, 15, 0, 0), - (0b1111111010010, 13, 63, 0, 0), - (0b11110100, 8, 16, 0, 0), - (0b1111111010011, 13, 64, 0, 0), - (0b11110101, 8, 5, 1, 0), - (0b1111111010100, 13, 1, 0, 1), - (0b111101100, 9, 17, 0, 0), - (0b1111111010101, 13, 2, 0, 1), - (0b111101101, 9, 18, 0, 0), - (0b1111111010110, 13, 3, 0, 1), - (0b111101110, 9, 19, 0, 0), - (0b1111111010111, 13, 4, 0, 1), - (0b111101111, 9, 20, 0, 0), - (0b1111111011000, 13, 5, 0, 1), - (0b111110000, 9, 21, 0, 0), - (0b1111111011001, 13, 14, 1, 0), - (0b111110001, 9, 22, 0, 0), - (0b1111111011010, 13, 15, 1, 0), - (0b111110010, 9, 6, 1, 0), - (0b1111111011011, 13, 16, 1, 0), - (0b1111100110, 10, 23, 0, 0), - (0b1111111011100, 13, 17, 1, 0), - (0b1111100111, 10, 24, 0, 0), - (0b11111110111010, 14, 6, 0, 1), - (0b1111101000, 10, 25, 0, 0), - (0b11111110111011, 14, 7, 0, 1), - (0b1111101001, 10, 26, 0, 0), - (0b11111110111100, 14, 8, 0, 1), - (0b1111101010, 10, 27, 0, 0), - (0b11111110111101, 14, 9, 0, 1), - (0b1111101011, 10, 28, 0, 0), - (0b11111110111110, 14, 10, 0, 1), - (0b1111101100, 10, 29, 0, 0), - (0b11111110111111, 14, 11, 0, 1), - (0b1111101101, 10, 7, 1, 0), - (0b11111111000000, 14, 12, 0, 1), - (0b1111101110, 10, 8, 1, 0), - (0b11111111000001, 14, 13, 0, 1), - (0b11111011110, 11, 30, 0, 0), - (0b11111111000010, 14, 14, 0, 1), - (0b11111011111, 11, 31, 0, 0), - (0b11111111000011, 14, 15, 0, 1), - (0b11111100000, 11, 32, 0, 0), - (0b11111111000100, 14, 16, 0, 1), - (0b11111100001, 11, 33, 0, 0), - (0b11111111000101, 14, 17, 0, 1), - (0b11111100010, 11, 34, 0, 0), - (0b11111111000110, 14, 18, 0, 1), - (0b11111100011, 11, 35, 0, 0), - (0b11111111000111, 14, 19, 0, 1), - (0b11111100100, 11, 36, 0, 0), - (0b11111111001000, 14, 20, 0, 1), - (0b11111100101, 11, 37, 0, 0), - (0b11111111001001, 14, 21, 0, 1), - (0b11111100110, 11, 38, 0, 0), - (0b11111111001010, 14, 22, 0, 1), - (0b11111100111, 11, 39, 0, 0), - (0b11111111001011, 14, 23, 0, 1), - (0b11111101000, 11, 9, 1, 0), - (0b11111111001100, 14, 24, 0, 1), - (0b11111101001, 11, 10, 1, 0), - (0b11111111001101, 14, 25, 0, 1), - (0b111111010100, 12, 40, 0, 0), - (0b11111111001110, 14, 26, 0, 1), - (0b111111010101, 12, 41, 0, 0), - (0b11111111001111, 14, 18, 1, 0), - (0b111111010110, 12, 42, 0, 0), - (0b11111111010000, 14, 19, 1, 0), - (0b111111010111, 12, 43, 0, 0), - (0b11111111010001, 14, 20, 1, 0), - (0b111111011000, 12, 44, 0, 0), - (0b11111111010010, 14, 21, 1, 0), - (0b111111011001, 12, 45, 0, 0), - (0b111111110100110, 15, 27, 0, 1), - (0b111111011010, 12, 46, 0, 0), - (0b111111110100111, 15, 28, 0, 1), - (0b111111110101000, 15, 29, 0, 1), - (0b1111111110101111, 16, 48, 1, 0), - (0b111111110101001, 15, 30, 0, 1), - (0b1111111110110000, 16, 49, 1, 0), - (0b111111110101010, 15, 31, 0, 1), - (0b1111111110110001, 16, 50, 1, 0), - (0b111111110101011, 15, 32, 0, 1), - (0b1111111110110010, 16, 51, 1, 0), - (0b111111110101100, 15, 33, 0, 1), - (0b1111111110110011, 16, 52, 1, 0), - (0b111111110101101, 15, 34, 0, 1), - (0b1111111110110100, 16, 53, 1, 0), - (0b111111110101110, 15, 35, 0, 1), - (0b1111111110110101, 16, 54, 1, 0), - (0b111111110101111, 15, 36, 0, 1), - (0b1111111110110110, 16, 55, 1, 0), - (0b111111110110000, 15, 37, 0, 1), - (0b1111111110110111, 16, 56, 1, 0), - (0b111111110110001, 15, 38, 0, 1), - (0b1111111110111000, 16, 57, 1, 0), - (0b111111110110010, 15, 39, 0, 1), - (0b1111111110111001, 16, 58, 1, 0), - (0b111111110110011, 15, 40, 0, 1), - (0b1111111110111010, 16, 59, 1, 0), - (0b111111110110100, 15, 41, 0, 1), - (0b1111111110111011, 16, 60, 1, 0), - (0b111111110110101, 15, 42, 0, 1), - (0b1111111110111100, 16, 61, 1, 0), - (0b111111110110110, 15, 43, 0, 1), - (0b1111111110111101, 16, 62, 1, 0), - (0b111111110110111, 15, 44, 0, 1), - (0b1111111110111110, 16, 63, 1, 0), - (0b111111110111000, 15, 45, 0, 1), - (0b1111111110111111, 16, 64, 1, 0), - (0b111111110111001, 15, 46, 0, 1), - (0b1111111111000000, 16, 1, 1, 1), - (0b111111110111010, 15, 47, 0, 1), - (0b1111111111000001, 16, 2, 1, 1), - (0b111111110111011, 15, 48, 0, 1), - (0b1111111111000010, 16, 3, 1, 1), - (0b111111110111100, 15, 49, 0, 1), - (0b1111111111000011, 16, 4, 1, 1), - (0b111111110111101, 15, 50, 0, 1), - (0b1111111111000100, 16, 5, 1, 1), - (0b111111110111110, 15, 51, 0, 1), - (0b1111111111000101, 16, 6, 1, 1), - (0b111111110111111, 15, 52, 0, 1), - (0b1111111111000110, 16, 7, 1, 1), - (0b111111111000000, 15, 53, 0, 1), - (0b1111111111000111, 16, 8, 1, 1), - (0b111111111000001, 15, 55, 0, 1), - (0b1111111111001000, 16, 9, 1, 1), - (0b111111111000010, 15, 56, 0, 1), - (0b1111111111001001, 16, 10, 1, 1), - (0b111111111000011, 15, 22, 1, 0), - (0b1111111111001010, 16, 11, 1, 1), - (0b111111111000100, 15, 23, 1, 0), - (0b1111111111001011, 16, 12, 1, 1), - (0b111111111000101, 15, 24, 1, 0), - (0b1111111111001100, 16, 13, 1, 1), - (0b111111111000110, 15, 25, 1, 0), - (0b1111111111001101, 16, 14, 1, 1), - (0b111111111000111, 15, 26, 1, 0), - (0b1111111111001110, 16, 15, 1, 1), - (0b111111111001000, 15, 27, 1, 0), - (0b1111111111001111, 16, 16, 1, 1), - (0b1111111110010010, 16, 54, 0, 1), - (0b1111111111010000, 16, 17, 1, 1), - (0b1111111110010011, 16, 57, 0, 1), - (0b1111111111010001, 16, 18, 1, 1), - (0b1111111110010100, 16, 58, 0, 1), - (0b1111111111010010, 16, 19, 1, 1), - (0b1111111110010101, 16, 59, 0, 1), - (0b1111111111010011, 16, 20, 1, 1), - (0b1111111110010110, 16, 60, 0, 1), - (0b1111111111010100, 16, 21, 1, 1), - (0b1111111110010111, 16, 61, 0, 1), - (0b1111111111010101, 16, 22, 1, 1), - (0b1111111110011000, 16, 62, 0, 1), - (0b1111111111010110, 16, 23, 1, 1), - (0b1111111110011001, 16, 63, 0, 1), - (0b1111111111010111, 16, 24, 1, 1), - (0b1111111110011010, 16, 64, 0, 1), - (0b1111111111011000, 16, 25, 1, 1), - (0b1111111110011011, 16, 28, 1, 0), - (0b1111111111011001, 16, 26, 1, 1), - (0b1111111110011100, 16, 29, 1, 0), - (0b1111111111011010, 16, 27, 1, 1), - (0b1111111110011101, 16, 30, 1, 0), - (0b1111111111011011, 16, 28, 1, 1), - (0b1111111110011110, 16, 31, 1, 0), - (0b1111111111011100, 16, 29, 1, 1), - (0b1111111110011111, 16, 32, 1, 0), - (0b1111111111011101, 16, 30, 1, 1), - (0b1111111110100000, 16, 33, 1, 0), - (0b1111111111011110, 16, 31, 1, 1), - (0b1111111110100001, 16, 34, 1, 0), - (0b1111111111011111, 16, 32, 1, 1), - (0b1111111110100010, 16, 35, 1, 0), - (0b1111111111100000, 16, 33, 1, 1), - (0b1111111110100011, 16, 36, 1, 0), - (0b1111111111100001, 16, 34, 1, 1), - (0b1111111110100100, 16, 37, 1, 0), - (0b1111111111100010, 16, 35, 1, 1), - (0b1111111110100101, 16, 38, 1, 0), - (0b1111111111100011, 16, 36, 1, 1), - (0b1111111110100110, 16, 39, 1, 0), - (0b1111111111100100, 16, 37, 1, 1), - (0b1111111110100111, 16, 40, 1, 0), - (0b1111111111100101, 16, 38, 1, 1), - (0b1111111110101000, 16, 41, 1, 0), - (0b1111111111100110, 16, 39, 1, 1), - (0b1111111110101001, 16, 42, 1, 0), - (0b1111111111100111, 16, 40, 1, 1), - (0b1111111110101010, 16, 43, 1, 0), - (0b1111111111101000, 16, 41, 1, 1), - (0b1111111110101011, 16, 44, 1, 0), - (0b1111111111101001, 16, 42, 1, 1), - (0b1111111110101100, 16, 45, 1, 0), - (0b1111111111101010, 16, 43, 1, 1), - (0b1111111110101101, 16, 46, 1, 0), - (0b1111111111101011, 16, 44, 1, 1), - (0b1111111110101110, 16, 47, 1, 0), - (0b1111111111101100, 16, 45, 1, 1), - (0b1111111111101101, 16, 46, 1, 1), - (0b1111111111110111, 16, 56, 1, 1), - (0b1111111111101110, 16, 47, 1, 1), - (0b1111111111111000, 16, 57, 1, 1), - (0b1111111111101111, 16, 48, 1, 1), - (0b1111111111111001, 16, 58, 1, 1), - (0b1111111111110000, 16, 49, 1, 1), - (0b1111111111111010, 16, 59, 1, 1), - (0b1111111111110001, 16, 50, 1, 1), - (0b1111111111111011, 16, 60, 1, 1), - (0b1111111111110010, 16, 51, 1, 1), - (0b1111111111111100, 16, 61, 1, 1), - (0b1111111111110011, 16, 52, 1, 1), - (0b1111111111111101, 16, 62, 1, 1), - (0b1111111111110100, 16, 53, 1, 1), - (0b1111111111111110, 16, 63, 1, 1), - (0b1111111111110101, 16, 54, 1, 1), - (0b1111111111111111, 16, 64, 1, 1), - (0b1111111111110110, 16, 55, 1, 1), -]; -/// Table E.11: (codeword, length, run). -pub const RUN_E_10: [(u16, u8, u8); 62] = [ - (0b0, 1, 1), - (0b1111100001, 10, 32), - (0b100, 3, 2), - (0b1111100010, 10, 33), - (0b101, 3, 3), - (0b1111100011, 10, 34), - (0b1100, 4, 4), - (0b1111100100, 10, 35), - (0b11010, 5, 5), - (0b1111100101, 10, 36), - (0b11011, 5, 6), - (0b1111100110, 10, 37), - (0b11100, 5, 7), - (0b1111100111, 10, 38), - (0b111010, 6, 8), - (0b1111101000, 10, 39), - (0b1110110, 7, 9), - (0b1111101001, 10, 40), - (0b1110111, 7, 10), - (0b1111101010, 10, 41), - (0b1111000, 7, 11), - (0b1111101011, 10, 42), - (0b11110010, 8, 12), - (0b1111101100, 10, 43), - (0b111100110, 9, 13), - (0b1111101101, 10, 44), - (0b111100111, 9, 14), - (0b1111101110, 10, 45), - (0b1111010000, 10, 15), - (0b1111101111, 10, 46), - (0b1111010001, 10, 16), - (0b1111110000, 10, 47), - (0b1111010010, 10, 17), - (0b1111110001, 10, 48), - (0b1111010011, 10, 18), - (0b1111110010, 10, 49), - (0b1111010100, 10, 19), - (0b1111110011, 10, 50), - (0b1111010101, 10, 20), - (0b1111110100, 10, 51), - (0b1111010110, 10, 21), - (0b1111110101, 10, 52), - (0b1111010111, 10, 22), - (0b1111110110, 10, 53), - (0b1111011000, 10, 23), - (0b1111110111, 10, 54), - (0b1111011001, 10, 24), - (0b1111111000, 10, 55), - (0b1111011010, 10, 25), - (0b1111111001, 10, 56), - (0b1111011011, 10, 26), - (0b1111111010, 10, 57), - (0b1111011100, 10, 27), - (0b1111111011, 10, 58), - (0b1111011101, 10, 28), - (0b1111111100, 10, 59), - (0b1111011110, 10, 29), - (0b1111111101, 10, 60), - (0b1111011111, 10, 30), - (0b1111111110, 10, 61), - (0b1111100000, 10, 31), - (0b1111111111, 10, 62), -]; -/// Table E.12: (codeword, length) indexed by the DC size (eta). -pub const DC_E_10: [(u16, u8); 14] = [ - (0b1010, 4), - (0b111110, 6), - (0b1011, 4), - (0b1100, 4), - (0b1101, 4), - (0b000, 3), - (0b001, 3), - (0b010, 3), - (0b011, 3), - (0b100, 3), - (0b1110, 4), - (0b11110, 5), - (0b1111110, 7), - (0b1111111, 7), -]; - -/// Table E.13: (codeword, length, amplitude (0 = EOB), Frun, Findex). -pub const AMP_E_13: [(u16, u8, u8, u8, u8); 257] = [ - (0b00, 2, 1, 0, 0), - (0b111111011000, 12, 45, 0, 0), - (0b01, 2, 1, 1, 0), - (0b111111011001, 12, 46, 0, 0), - (0b100, 3, 2, 0, 0), - (0b111111011010, 12, 47, 0, 0), - (0b1010, 4, 3, 0, 0), - (0b111111011011, 12, 48, 0, 0), - (0b1011, 4, 0, 0, 0), - (0b111111011100, 12, 49, 0, 0), - (0b11000, 5, 4, 0, 0), - (0b111111011101, 12, 50, 0, 0), - (0b11001, 5, 5, 0, 0), - (0b111111011110, 12, 51, 0, 0), - (0b11010, 5, 2, 1, 0), - (0b111111011111, 12, 52, 0, 0), - (0b110110, 6, 6, 0, 0), - (0b111111100000, 12, 12, 1, 0), - (0b110111, 6, 7, 0, 0), - (0b111111100001, 12, 13, 1, 0), - (0b111000, 6, 8, 0, 0), - (0b111111100010, 12, 14, 1, 0), - (0b111001, 6, 3, 1, 0), - (0b1111111000110, 13, 53, 0, 0), - (0b1110100, 7, 9, 0, 0), - (0b1111111000111, 13, 54, 0, 0), - (0b1110101, 7, 10, 0, 0), - (0b1111111001000, 13, 55, 0, 0), - (0b1110110, 7, 11, 0, 0), - (0b1111111001001, 13, 56, 0, 0), - (0b1110111, 7, 4, 1, 0), - (0b1111111001010, 13, 57, 0, 0), - (0b11110000, 8, 12, 0, 0), - (0b1111111001011, 13, 58, 0, 0), - (0b11110001, 8, 13, 0, 0), - (0b1111111001100, 13, 59, 0, 0), - (0b11110010, 8, 14, 0, 0), - (0b1111111001101, 13, 60, 0, 0), - (0b11110011, 8, 15, 0, 0), - (0b1111111001110, 13, 61, 0, 0), - (0b11110100, 8, 16, 0, 0), - (0b1111111001111, 13, 62, 0, 0), - (0b11110101, 8, 5, 1, 0), - (0b1111111010000, 13, 63, 0, 0), - (0b111101100, 9, 17, 0, 0), - (0b1111111010001, 13, 64, 0, 0), - (0b111101101, 9, 18, 0, 0), - (0b1111111010010, 13, 1, 0, 1), - (0b111101110, 9, 19, 0, 0), - (0b1111111010011, 13, 2, 0, 1), - (0b111101111, 9, 20, 0, 0), - (0b1111111010100, 13, 3, 0, 1), - (0b111110000, 9, 21, 0, 0), - (0b1111111010101, 13, 4, 0, 1), - (0b111110001, 9, 6, 1, 0), - (0b1111111010110, 13, 5, 0, 1), - (0b1111100100, 10, 22, 0, 0), - (0b1111111010111, 13, 6, 0, 1), - (0b1111100101, 10, 23, 0, 0), - (0b1111111011000, 13, 7, 0, 1), - (0b1111100110, 10, 24, 0, 0), - (0b1111111011001, 13, 8, 0, 1), - (0b1111100111, 10, 25, 0, 0), - (0b1111111011010, 13, 15, 1, 0), - (0b1111101000, 10, 26, 0, 0), - (0b1111111011011, 13, 16, 1, 0), - (0b1111101001, 10, 27, 0, 0), - (0b1111111011100, 13, 17, 1, 0), - (0b1111101010, 10, 28, 0, 0), - (0b11111110111010, 14, 9, 0, 1), - (0b1111101011, 10, 29, 0, 0), - (0b11111110111011, 14, 10, 0, 1), - (0b1111101100, 10, 7, 1, 0), - (0b11111110111100, 14, 11, 0, 1), - (0b1111101101, 10, 8, 1, 0), - (0b11111110111101, 14, 12, 0, 1), - (0b11111011100, 11, 30, 0, 0), - (0b11111110111110, 14, 13, 0, 1), - (0b11111011101, 11, 31, 0, 0), - (0b11111110111111, 14, 14, 0, 1), - (0b11111011110, 11, 32, 0, 0), - (0b11111111000000, 14, 15, 0, 1), - (0b11111011111, 11, 33, 0, 0), - (0b11111111000001, 14, 16, 0, 1), - (0b11111100000, 11, 34, 0, 0), - (0b11111111000010, 14, 17, 0, 1), - (0b11111100001, 11, 35, 0, 0), - (0b11111111000011, 14, 18, 0, 1), - (0b11111100010, 11, 36, 0, 0), - (0b11111111000100, 14, 19, 0, 1), - (0b11111100011, 11, 37, 0, 0), - (0b11111111000101, 14, 20, 0, 1), - (0b11111100100, 11, 38, 0, 0), - (0b11111111000110, 14, 21, 0, 1), - (0b11111100101, 11, 39, 0, 0), - (0b11111111000111, 14, 22, 0, 1), - (0b11111100110, 11, 40, 0, 0), - (0b11111111001000, 14, 23, 0, 1), - (0b11111100111, 11, 9, 1, 0), - (0b11111111001001, 14, 24, 0, 1), - (0b11111101000, 11, 10, 1, 0), - (0b11111111001010, 14, 25, 0, 1), - (0b11111101001, 11, 11, 1, 0), - (0b11111111001011, 14, 26, 0, 1), - (0b111111010100, 12, 41, 0, 0), - (0b11111111001100, 14, 27, 0, 1), - (0b111111010101, 12, 42, 0, 0), - (0b11111111001101, 14, 28, 0, 1), - (0b111111010110, 12, 43, 0, 0), - (0b11111111001110, 14, 29, 0, 1), - (0b111111010111, 12, 44, 0, 0), - (0b11111111001111, 14, 18, 1, 0), - (0b11111111010000, 14, 19, 1, 0), - (0b1111111110101101, 16, 46, 1, 0), - (0b11111111010001, 14, 20, 1, 0), - (0b1111111110101110, 16, 47, 1, 0), - (0b11111111010010, 14, 21, 1, 0), - (0b1111111110101111, 16, 48, 1, 0), - (0b11111111010011, 14, 22, 1, 0), - (0b1111111110110000, 16, 49, 1, 0), - (0b111111110101000, 15, 30, 0, 1), - (0b1111111110110001, 16, 50, 1, 0), - (0b111111110101001, 15, 31, 0, 1), - (0b1111111110110010, 16, 51, 1, 0), - (0b111111110101010, 15, 32, 0, 1), - (0b1111111110110011, 16, 52, 1, 0), - (0b111111110101011, 15, 33, 0, 1), - (0b1111111110110100, 16, 53, 1, 0), - (0b111111110101100, 15, 34, 0, 1), - (0b1111111110110101, 16, 54, 1, 0), - (0b111111110101101, 15, 35, 0, 1), - (0b1111111110110110, 16, 55, 1, 0), - (0b111111110101110, 15, 36, 0, 1), - (0b1111111110110111, 16, 56, 1, 0), - (0b111111110101111, 15, 37, 0, 1), - (0b1111111110111000, 16, 57, 1, 0), - (0b111111110110000, 15, 38, 0, 1), - (0b1111111110111001, 16, 58, 1, 0), - (0b111111110110001, 15, 39, 0, 1), - (0b1111111110111010, 16, 59, 1, 0), - (0b111111110110010, 15, 40, 0, 1), - (0b1111111110111011, 16, 60, 1, 0), - (0b111111110110011, 15, 41, 0, 1), - (0b1111111110111100, 16, 61, 1, 0), - (0b111111110110100, 15, 42, 0, 1), - (0b1111111110111101, 16, 62, 1, 0), - (0b111111110110101, 15, 43, 0, 1), - (0b1111111110111110, 16, 63, 1, 0), - (0b111111110110110, 15, 44, 0, 1), - (0b1111111110111111, 16, 64, 1, 0), - (0b111111110110111, 15, 45, 0, 1), - (0b1111111111000000, 16, 1, 1, 1), - (0b111111110111000, 15, 46, 0, 1), - (0b1111111111000001, 16, 2, 1, 1), - (0b111111110111001, 15, 47, 0, 1), - (0b1111111111000010, 16, 3, 1, 1), - (0b111111110111010, 15, 48, 0, 1), - (0b1111111111000011, 16, 4, 1, 1), - (0b111111110111011, 15, 49, 0, 1), - (0b1111111111000100, 16, 5, 1, 1), - (0b111111110111100, 15, 50, 0, 1), - (0b1111111111000101, 16, 6, 1, 1), - (0b111111110111101, 15, 51, 0, 1), - (0b1111111111000110, 16, 7, 1, 1), - (0b111111110111110, 15, 52, 0, 1), - (0b1111111111000111, 16, 8, 1, 1), - (0b111111110111111, 15, 53, 0, 1), - (0b1111111111001000, 16, 9, 1, 1), - (0b111111111000000, 15, 54, 0, 1), - (0b1111111111001001, 16, 10, 1, 1), - (0b111111111000001, 15, 55, 0, 1), - (0b1111111111001010, 16, 11, 1, 1), - (0b111111111000010, 15, 56, 0, 1), - (0b1111111111001011, 16, 12, 1, 1), - (0b111111111000011, 15, 57, 0, 1), - (0b1111111111001100, 16, 13, 1, 1), - (0b111111111000100, 15, 58, 0, 1), - (0b1111111111001101, 16, 14, 1, 1), - (0b111111111000101, 15, 23, 1, 0), - (0b1111111111001110, 16, 15, 1, 1), - (0b111111111000110, 15, 24, 1, 0), - (0b1111111111001111, 16, 16, 1, 1), - (0b111111111000111, 15, 25, 1, 0), - (0b1111111111010000, 16, 17, 1, 1), - (0b111111111001000, 15, 26, 1, 0), - (0b1111111111010001, 16, 18, 1, 1), - (0b111111111001001, 15, 27, 1, 0), - (0b1111111111010010, 16, 19, 1, 1), - (0b111111111001010, 15, 28, 1, 0), - (0b1111111111010011, 16, 20, 1, 1), - (0b1111111110010110, 16, 59, 0, 1), - (0b1111111111010100, 16, 21, 1, 1), - (0b1111111110010111, 16, 60, 0, 1), - (0b1111111111010101, 16, 22, 1, 1), - (0b1111111110011000, 16, 61, 0, 1), - (0b1111111111010110, 16, 23, 1, 1), - (0b1111111110011001, 16, 62, 0, 1), - (0b1111111111010111, 16, 24, 1, 1), - (0b1111111110011010, 16, 63, 0, 1), - (0b1111111111011000, 16, 25, 1, 1), - (0b1111111110011011, 16, 64, 0, 1), - (0b1111111111011001, 16, 26, 1, 1), - (0b1111111110011100, 16, 29, 1, 0), - (0b1111111111011010, 16, 27, 1, 1), - (0b1111111110011101, 16, 30, 1, 0), - (0b1111111111011011, 16, 28, 1, 1), - (0b1111111110011110, 16, 31, 1, 0), - (0b1111111111011100, 16, 29, 1, 1), - (0b1111111110011111, 16, 32, 1, 0), - (0b1111111111011101, 16, 30, 1, 1), - (0b1111111110100000, 16, 33, 1, 0), - (0b1111111111011110, 16, 31, 1, 1), - (0b1111111110100001, 16, 34, 1, 0), - (0b1111111111011111, 16, 32, 1, 1), - (0b1111111110100010, 16, 35, 1, 0), - (0b1111111111100000, 16, 33, 1, 1), - (0b1111111110100011, 16, 36, 1, 0), - (0b1111111111100001, 16, 34, 1, 1), - (0b1111111110100100, 16, 37, 1, 0), - (0b1111111111100010, 16, 35, 1, 1), - (0b1111111110100101, 16, 38, 1, 0), - (0b1111111111100011, 16, 36, 1, 1), - (0b1111111110100110, 16, 39, 1, 0), - (0b1111111111100100, 16, 37, 1, 1), - (0b1111111110100111, 16, 40, 1, 0), - (0b1111111111100101, 16, 38, 1, 1), - (0b1111111110101000, 16, 41, 1, 0), - (0b1111111111100110, 16, 39, 1, 1), - (0b1111111110101001, 16, 42, 1, 0), - (0b1111111111100111, 16, 40, 1, 1), - (0b1111111110101010, 16, 43, 1, 0), - (0b1111111111101000, 16, 41, 1, 1), - (0b1111111110101011, 16, 44, 1, 0), - (0b1111111111101001, 16, 42, 1, 1), - (0b1111111110101100, 16, 45, 1, 0), - (0b1111111111101010, 16, 43, 1, 1), - (0b1111111111101011, 16, 44, 1, 1), - (0b1111111111110110, 16, 55, 1, 1), - (0b1111111111101100, 16, 45, 1, 1), - (0b1111111111110111, 16, 56, 1, 1), - (0b1111111111101101, 16, 46, 1, 1), - (0b1111111111111000, 16, 57, 1, 1), - (0b1111111111101110, 16, 47, 1, 1), - (0b1111111111111001, 16, 58, 1, 1), - (0b1111111111101111, 16, 48, 1, 1), - (0b1111111111111010, 16, 59, 1, 1), - (0b1111111111110000, 16, 49, 1, 1), - (0b1111111111111011, 16, 60, 1, 1), - (0b1111111111110001, 16, 50, 1, 1), - (0b1111111111111100, 16, 61, 1, 1), - (0b1111111111110010, 16, 51, 1, 1), - (0b1111111111111101, 16, 62, 1, 1), - (0b1111111111110011, 16, 52, 1, 1), - (0b1111111111111110, 16, 63, 1, 1), - (0b1111111111110100, 16, 53, 1, 1), - (0b1111111111111111, 16, 64, 1, 1), - (0b1111111111110101, 16, 54, 1, 1), -]; -/// Table E.14: (codeword, length, run). -pub const RUN_E_13: [(u16, u8, u8); 62] = [ - (0b0, 1, 1), - (0b1111100001, 10, 32), - (0b100, 3, 2), - (0b1111100010, 10, 33), - (0b101, 3, 3), - (0b1111100011, 10, 34), - (0b1100, 4, 4), - (0b1111100100, 10, 35), - (0b11010, 5, 5), - (0b1111100101, 10, 36), - (0b11011, 5, 6), - (0b1111100110, 10, 37), - (0b11100, 5, 7), - (0b1111100111, 10, 38), - (0b111010, 6, 8), - (0b1111101000, 10, 39), - (0b1110110, 7, 9), - (0b1111101001, 10, 40), - (0b1110111, 7, 10), - (0b1111101010, 10, 41), - (0b1111000, 7, 11), - (0b1111101011, 10, 42), - (0b11110010, 8, 12), - (0b1111101100, 10, 43), - (0b111100110, 9, 13), - (0b1111101101, 10, 44), - (0b111100111, 9, 14), - (0b1111101110, 10, 45), - (0b1111010000, 10, 15), - (0b1111101111, 10, 46), - (0b1111010001, 10, 16), - (0b1111110000, 10, 47), - (0b1111010010, 10, 17), - (0b1111110001, 10, 48), - (0b1111010011, 10, 18), - (0b1111110010, 10, 49), - (0b1111010100, 10, 19), - (0b1111110011, 10, 50), - (0b1111010101, 10, 20), - (0b1111110100, 10, 51), - (0b1111010110, 10, 21), - (0b1111110101, 10, 52), - (0b1111010111, 10, 22), - (0b1111110110, 10, 53), - (0b1111011000, 10, 23), - (0b1111110111, 10, 54), - (0b1111011001, 10, 24), - (0b1111111000, 10, 55), - (0b1111011010, 10, 25), - (0b1111111001, 10, 56), - (0b1111011011, 10, 26), - (0b1111111010, 10, 57), - (0b1111011100, 10, 27), - (0b1111111011, 10, 58), - (0b1111011101, 10, 28), - (0b1111111100, 10, 59), - (0b1111011110, 10, 29), - (0b1111111101, 10, 60), - (0b1111011111, 10, 30), - (0b1111111110, 10, 61), - (0b1111100000, 10, 31), - (0b1111111111, 10, 62), -]; -/// Table E.15: (codeword, length) indexed by the DC size (eta). -pub const DC_E_13: [(u16, u8); 12] = - [(0b000, 3), (0b1100, 4), (0b1101, 4), (0b001, 3), (0b010, 3), (0b011, 3), (0b100, 3), (0b101, 3), (0b1110, 4), (0b11110, 5), (0b111110, 6), (0b111111, 6)]; - -/// Table E.16: (codeword, length, amplitude (0 = EOB), Frun, Findex). -pub const AMP_E_16: [(u16, u8, u8, u8, u8); 257] = [ - (0b00, 2, 1, 0, 0), - (0b1111111010001, 13, 43, 0, 0), - (0b01, 2, 1, 1, 0), - (0b1111111010010, 13, 44, 0, 0), - (0b100, 3, 2, 0, 0), - (0b1111111010011, 13, 45, 0, 0), - (0b1010, 4, 3, 0, 0), - (0b1111111010100, 13, 46, 0, 0), - (0b1011, 4, 2, 1, 0), - (0b1111111010101, 13, 47, 0, 0), - (0b1100, 4, 0, 0, 0), - (0b1111111010110, 13, 48, 0, 0), - (0b11010, 5, 4, 0, 0), - (0b1111111010111, 13, 49, 0, 0), - (0b11011, 5, 5, 0, 0), - (0b1111111011000, 13, 50, 0, 0), - (0b111000, 6, 6, 0, 0), - (0b1111111011001, 13, 51, 0, 0), - (0b111001, 6, 7, 0, 0), - (0b1111111011010, 13, 52, 0, 0), - (0b111010, 6, 3, 1, 0), - (0b1111111011011, 13, 53, 0, 0), - (0b1110110, 7, 8, 0, 0), - (0b1111111011100, 13, 14, 1, 0), - (0b1110111, 7, 9, 0, 0), - (0b1111111011101, 13, 15, 1, 0), - (0b1111000, 7, 10, 0, 0), - (0b1111111011110, 13, 16, 1, 0), - (0b11110010, 8, 11, 0, 0), - (0b11111110111110, 14, 54, 0, 0), - (0b11110011, 8, 12, 0, 0), - (0b11111110111111, 14, 55, 0, 0), - (0b11110100, 8, 13, 0, 0), - (0b11111111000000, 14, 56, 0, 0), - (0b11110101, 8, 14, 0, 0), - (0b11111111000001, 14, 57, 0, 0), - (0b11110110, 8, 4, 1, 0), - (0b11111111000010, 14, 58, 0, 0), - (0b11110111, 8, 5, 1, 0), - (0b11111111000011, 14, 59, 0, 0), - (0b111110000, 9, 15, 0, 0), - (0b11111111000100, 14, 60, 0, 0), - (0b111110001, 9, 16, 0, 0), - (0b11111111000101, 14, 61, 0, 0), - (0b111110010, 9, 17, 0, 0), - (0b11111111000110, 14, 62, 0, 0), - (0b111110011, 9, 18, 0, 0), - (0b11111111000111, 14, 63, 0, 0), - (0b111110100, 9, 6, 1, 0), - (0b11111111001000, 14, 64, 0, 0), - (0b1111101010, 10, 19, 0, 0), - (0b11111111001001, 14, 1, 0, 1), - (0b1111101011, 10, 20, 0, 0), - (0b11111111001010, 14, 2, 0, 1), - (0b1111101100, 10, 21, 0, 0), - (0b11111111001011, 14, 3, 0, 1), - (0b1111101101, 10, 22, 0, 0), - (0b11111111001100, 14, 17, 1, 0), - (0b1111101110, 10, 23, 0, 0), - (0b11111111001101, 14, 18, 1, 0), - (0b1111101111, 10, 24, 0, 0), - (0b11111111001110, 14, 19, 1, 0), - (0b1111110000, 10, 7, 1, 0), - (0b11111111001111, 14, 20, 1, 0), - (0b1111110001, 10, 8, 1, 0), - (0b111111110100000, 15, 4, 0, 1), - (0b11111100100, 11, 25, 0, 0), - (0b111111110100001, 15, 5, 0, 1), - (0b11111100101, 11, 26, 0, 0), - (0b111111110100010, 15, 6, 0, 1), - (0b11111100110, 11, 27, 0, 0), - (0b111111110100011, 15, 7, 0, 1), - (0b11111100111, 11, 28, 0, 0), - (0b111111110100100, 15, 8, 0, 1), - (0b11111101000, 11, 29, 0, 0), - (0b111111110100101, 15, 9, 0, 1), - (0b11111101001, 11, 30, 0, 0), - (0b111111110100110, 15, 10, 0, 1), - (0b11111101010, 11, 31, 0, 0), - (0b111111110100111, 15, 11, 0, 1), - (0b11111101011, 11, 32, 0, 0), - (0b111111110101000, 15, 12, 0, 1), - (0b11111101100, 11, 9, 1, 0), - (0b111111110101001, 15, 13, 0, 1), - (0b11111101101, 11, 10, 1, 0), - (0b111111110101010, 15, 14, 0, 1), - (0b111111011100, 12, 33, 0, 0), - (0b111111110101011, 15, 15, 0, 1), - (0b111111011101, 12, 34, 0, 0), - (0b111111110101100, 15, 16, 0, 1), - (0b111111011110, 12, 35, 0, 0), - (0b111111110101101, 15, 17, 0, 1), - (0b111111011111, 12, 36, 0, 0), - (0b111111110101110, 15, 18, 0, 1), - (0b111111100000, 12, 37, 0, 0), - (0b111111110101111, 15, 19, 0, 1), - (0b111111100001, 12, 38, 0, 0), - (0b111111110110000, 15, 20, 0, 1), - (0b111111100010, 12, 39, 0, 0), - (0b111111110110001, 15, 21, 0, 1), - (0b111111100011, 12, 40, 0, 0), - (0b111111110110010, 15, 21, 1, 0), - (0b111111100100, 12, 41, 0, 0), - (0b111111110110011, 15, 22, 1, 0), - (0b111111100101, 12, 11, 1, 0), - (0b111111110110100, 15, 23, 1, 0), - (0b111111100110, 12, 12, 1, 0), - (0b111111110110101, 15, 24, 1, 0), - (0b111111100111, 12, 13, 1, 0), - (0b111111110110110, 15, 25, 1, 0), - (0b1111111010000, 13, 42, 0, 0), - (0b1111111101101110, 16, 22, 0, 1), - (0b1111111101101111, 16, 23, 0, 1), - (0b1111111110101101, 16, 46, 1, 0), - (0b1111111101110000, 16, 24, 0, 1), - (0b1111111110101110, 16, 47, 1, 0), - (0b1111111101110001, 16, 25, 0, 1), - (0b1111111110101111, 16, 48, 1, 0), - (0b1111111101110010, 16, 26, 0, 1), - (0b1111111110110000, 16, 49, 1, 0), - (0b1111111101110011, 16, 27, 0, 1), - (0b1111111110110001, 16, 50, 1, 0), - (0b1111111101110100, 16, 28, 0, 1), - (0b1111111110110010, 16, 51, 1, 0), - (0b1111111101110101, 16, 29, 0, 1), - (0b1111111110110011, 16, 52, 1, 0), - (0b1111111101110110, 16, 30, 0, 1), - (0b1111111110110100, 16, 53, 1, 0), - (0b1111111101110111, 16, 31, 0, 1), - (0b1111111110110101, 16, 54, 1, 0), - (0b1111111101111000, 16, 32, 0, 1), - (0b1111111110110110, 16, 55, 1, 0), - (0b1111111101111001, 16, 33, 0, 1), - (0b1111111110110111, 16, 56, 1, 0), - (0b1111111101111010, 16, 34, 0, 1), - (0b1111111110111000, 16, 57, 1, 0), - (0b1111111101111011, 16, 35, 0, 1), - (0b1111111110111001, 16, 58, 1, 0), - (0b1111111101111100, 16, 36, 0, 1), - (0b1111111110111010, 16, 59, 1, 0), - (0b1111111101111101, 16, 37, 0, 1), - (0b1111111110111011, 16, 60, 1, 0), - (0b1111111101111110, 16, 38, 0, 1), - (0b1111111110111100, 16, 61, 1, 0), - (0b1111111101111111, 16, 39, 0, 1), - (0b1111111110111101, 16, 62, 1, 0), - (0b1111111110000000, 16, 40, 0, 1), - (0b1111111110111110, 16, 63, 1, 0), - (0b1111111110000001, 16, 41, 0, 1), - (0b1111111110111111, 16, 64, 1, 0), - (0b1111111110000010, 16, 42, 0, 1), - (0b1111111111000000, 16, 1, 1, 1), - (0b1111111110000011, 16, 43, 0, 1), - (0b1111111111000001, 16, 2, 1, 1), - (0b1111111110000100, 16, 44, 0, 1), - (0b1111111111000010, 16, 3, 1, 1), - (0b1111111110000101, 16, 45, 0, 1), - (0b1111111111000011, 16, 4, 1, 1), - (0b1111111110000110, 16, 46, 0, 1), - (0b1111111111000100, 16, 5, 1, 1), - (0b1111111110000111, 16, 47, 0, 1), - (0b1111111111000101, 16, 6, 1, 1), - (0b1111111110001000, 16, 48, 0, 1), - (0b1111111111000110, 16, 7, 1, 1), - (0b1111111110001001, 16, 49, 0, 1), - (0b1111111111000111, 16, 8, 1, 1), - (0b1111111110001010, 16, 50, 0, 1), - (0b1111111111001000, 16, 9, 1, 1), - (0b1111111110001011, 16, 51, 0, 1), - (0b1111111111001001, 16, 10, 1, 1), - (0b1111111110001100, 16, 52, 0, 1), - (0b1111111111001010, 16, 11, 1, 1), - (0b1111111110001101, 16, 53, 0, 1), - (0b1111111111001011, 16, 12, 1, 1), - (0b1111111110001110, 16, 54, 0, 1), - (0b1111111111001100, 16, 13, 1, 1), - (0b1111111110001111, 16, 55, 0, 1), - (0b1111111111001101, 16, 14, 1, 1), - (0b1111111110010000, 16, 56, 0, 1), - (0b1111111111001110, 16, 15, 1, 1), - (0b1111111110010001, 16, 57, 0, 1), - (0b1111111111001111, 16, 16, 1, 1), - (0b1111111110010010, 16, 58, 0, 1), - (0b1111111111010000, 16, 17, 1, 1), - (0b1111111110010011, 16, 59, 0, 1), - (0b1111111111010001, 16, 18, 1, 1), - (0b1111111110010100, 16, 60, 0, 1), - (0b1111111111010010, 16, 19, 1, 1), - (0b1111111110010101, 16, 61, 0, 1), - (0b1111111111010011, 16, 20, 1, 1), - (0b1111111110010110, 16, 62, 0, 1), - (0b1111111111010100, 16, 21, 1, 1), - (0b1111111110010111, 16, 63, 0, 1), - (0b1111111111010101, 16, 22, 1, 1), - (0b1111111110011000, 16, 64, 0, 1), - (0b1111111111010110, 16, 23, 1, 1), - (0b1111111110011001, 16, 26, 1, 0), - (0b1111111111010111, 16, 24, 1, 1), - (0b1111111110011010, 16, 27, 1, 0), - (0b1111111111011000, 16, 25, 1, 1), - (0b1111111110011011, 16, 28, 1, 0), - (0b1111111111011001, 16, 26, 1, 1), - (0b1111111110011100, 16, 29, 1, 0), - (0b1111111111011010, 16, 27, 1, 1), - (0b1111111110011101, 16, 30, 1, 0), - (0b1111111111011011, 16, 28, 1, 1), - (0b1111111110011110, 16, 31, 1, 0), - (0b1111111111011100, 16, 29, 1, 1), - (0b1111111110011111, 16, 32, 1, 0), - (0b1111111111011101, 16, 30, 1, 1), - (0b1111111110100000, 16, 33, 1, 0), - (0b1111111111011110, 16, 31, 1, 1), - (0b1111111110100001, 16, 34, 1, 0), - (0b1111111111011111, 16, 32, 1, 1), - (0b1111111110100010, 16, 35, 1, 0), - (0b1111111111100000, 16, 33, 1, 1), - (0b1111111110100011, 16, 36, 1, 0), - (0b1111111111100001, 16, 34, 1, 1), - (0b1111111110100100, 16, 37, 1, 0), - (0b1111111111100010, 16, 35, 1, 1), - (0b1111111110100101, 16, 38, 1, 0), - (0b1111111111100011, 16, 36, 1, 1), - (0b1111111110100110, 16, 39, 1, 0), - (0b1111111111100100, 16, 37, 1, 1), - (0b1111111110100111, 16, 40, 1, 0), - (0b1111111111100101, 16, 38, 1, 1), - (0b1111111110101000, 16, 41, 1, 0), - (0b1111111111100110, 16, 39, 1, 1), - (0b1111111110101001, 16, 42, 1, 0), - (0b1111111111100111, 16, 40, 1, 1), - (0b1111111110101010, 16, 43, 1, 0), - (0b1111111111101000, 16, 41, 1, 1), - (0b1111111110101011, 16, 44, 1, 0), - (0b1111111111101001, 16, 42, 1, 1), - (0b1111111110101100, 16, 45, 1, 0), - (0b1111111111101010, 16, 43, 1, 1), - (0b1111111111101011, 16, 44, 1, 1), - (0b1111111111110110, 16, 55, 1, 1), - (0b1111111111101100, 16, 45, 1, 1), - (0b1111111111110111, 16, 56, 1, 1), - (0b1111111111101101, 16, 46, 1, 1), - (0b1111111111111000, 16, 57, 1, 1), - (0b1111111111101110, 16, 47, 1, 1), - (0b1111111111111001, 16, 58, 1, 1), - (0b1111111111101111, 16, 48, 1, 1), - (0b1111111111111010, 16, 59, 1, 1), - (0b1111111111110000, 16, 49, 1, 1), - (0b1111111111111011, 16, 60, 1, 1), - (0b1111111111110001, 16, 50, 1, 1), - (0b1111111111111100, 16, 61, 1, 1), - (0b1111111111110010, 16, 51, 1, 1), - (0b1111111111111101, 16, 62, 1, 1), - (0b1111111111110011, 16, 52, 1, 1), - (0b1111111111111110, 16, 63, 1, 1), - (0b1111111111110100, 16, 53, 1, 1), - (0b1111111111111111, 16, 64, 1, 1), - (0b1111111111110101, 16, 54, 1, 1), -]; -/// Table E.17: (codeword, length, run). -pub const RUN_E_16: [(u16, u8, u8); 62] = [ - (0b0, 1, 1), - (0b1111100001, 10, 32), - (0b100, 3, 2), - (0b1111100010, 10, 33), - (0b101, 3, 3), - (0b1111100011, 10, 34), - (0b1100, 4, 4), - (0b1111100100, 10, 35), - (0b11010, 5, 5), - (0b1111100101, 10, 36), - (0b11011, 5, 6), - (0b1111100110, 10, 37), - (0b11100, 5, 7), - (0b1111100111, 10, 38), - (0b111010, 6, 8), - (0b1111101000, 10, 39), - (0b1110110, 7, 9), - (0b1111101001, 10, 40), - (0b1110111, 7, 10), - (0b1111101010, 10, 41), - (0b1111000, 7, 11), - (0b1111101011, 10, 42), - (0b11110010, 8, 12), - (0b1111101100, 10, 43), - (0b111100110, 9, 13), - (0b1111101101, 10, 44), - (0b111100111, 9, 14), - (0b1111101110, 10, 45), - (0b1111010000, 10, 15), - (0b1111101111, 10, 46), - (0b1111010001, 10, 16), - (0b1111110000, 10, 47), - (0b1111010010, 10, 17), - (0b1111110001, 10, 48), - (0b1111010011, 10, 18), - (0b1111110010, 10, 49), - (0b1111010100, 10, 19), - (0b1111110011, 10, 50), - (0b1111010101, 10, 20), - (0b1111110100, 10, 51), - (0b1111010110, 10, 21), - (0b1111110101, 10, 52), - (0b1111010111, 10, 22), - (0b1111110110, 10, 53), - (0b1111011000, 10, 23), - (0b1111110111, 10, 54), - (0b1111011001, 10, 24), - (0b1111111000, 10, 55), - (0b1111011010, 10, 25), - (0b1111111001, 10, 56), - (0b1111011011, 10, 26), - (0b1111111010, 10, 57), - (0b1111011100, 10, 27), - (0b1111111011, 10, 58), - (0b1111011101, 10, 28), - (0b1111111100, 10, 59), - (0b1111011110, 10, 29), - (0b1111111101, 10, 60), - (0b1111011111, 10, 30), - (0b1111111110, 10, 61), - (0b1111100000, 10, 31), - (0b1111111111, 10, 62), -]; -/// Table E.18: (codeword, length) indexed by the DC size (eta). -pub const DC_E_16: [(u16, u8); 12] = - [(0b000, 3), (0b1100, 4), (0b1101, 4), (0b001, 3), (0b010, 3), (0b011, 3), (0b100, 3), (0b101, 3), (0b1110, 4), (0b11110, 5), (0b111110, 6), (0b111111, 6)]; diff --git a/crates/dnx/src/tables.rs b/crates/dnx/src/tables.rs deleted file mode 100644 index e48acf832..000000000 --- a/crates/dnx/src/tables.rs +++ /dev/null @@ -1,301 +0,0 @@ -//! Compression-ID parameters (Annex C), zig-zag order (Figure 48) and VLC lookup tables built from -//! the Annex E codeword lists in [`crate::spec_tables`]. - -use crate::spec_tables::{ - AMP_E_1, AMP_E_4, AMP_E_7, AMP_E_10, AMP_E_13, AMP_E_16, DC_E_1, DC_E_4, DC_E_7, DC_E_10, DC_E_13, DC_E_16, RUN_E_1, RUN_E_4, RUN_E_7, RUN_E_10, RUN_E_13, - RUN_E_16, W_D_1, W_D_2, W_D_3, W_D_4, W_D_5, W_D_6, W_D_7, W_D_8, W_D_9, W_D_10, W_D_11, -}; -use std::sync::OnceLock; - -/// Bitstream coefficient index `r` → raster position `v*8 + u` (Figure 48). -pub(crate) const ZIGZAG: [u8; 64] = { - // Figure 48 printed as a grid: GRID[v][u] = r - const GRID: [[u8; 8]; 8] = [ - [0, 1, 5, 6, 14, 15, 27, 28], - [2, 4, 7, 13, 16, 26, 29, 42], - [3, 8, 12, 17, 25, 30, 41, 43], - [9, 11, 18, 24, 31, 40, 44, 53], - [10, 19, 23, 32, 39, 45, 52, 54], - [20, 22, 33, 38, 46, 51, 55, 60], - [21, 34, 37, 47, 50, 56, 59, 61], - [35, 36, 48, 49, 57, 58, 62, 63], - ]; - let mut z = [0u8; 64]; - let mut v = 0; - while v < 8 { - let mut u = 0; - while u < 8 { - z[GRID[v][u] as usize] = (v * 8 + u) as u8; - u += 1; - } - v += 1; - } - z -}; - -/// Raster profile of a compression ID. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum Kind { - /// HD profile: fixed raster and frame size (Table C.1). - Hd { width: u16, height: u16, interlaced: bool, depth: u8, frame_size: u32 }, - /// Resolution-independent profile (Table C.2): reference frame size C0 for 8160 macroblocks. - Ri { c0: u32 }, -} - -/// Parameters of one compression ID. -#[derive(Debug, Clone, Copy)] -pub(crate) struct CidInfo { - pub cid: u32, - pub kind: Kind, - /// Quantisation weights (Annex D), `[luma, chroma]`, raster order without the DC entry. - pub weights: &'static [[u8; 63]; 2], - /// VLC table set (index into [`vlc`]): 0 = E.1–E.3, 1 = E.4–E.6, 2 = E.7–E.9, 3 = E.10–E.12, - /// 4 = E.13–E.15, 5 = E.16–E.18. - pub vlc: usize, - /// Inverse quantisation parameter `p` (8.2.7): 8 or 32. - pub p: u32, -} - -const fn hd(cid: u32, kind: Kind, weights: &'static [[u8; 63]; 2], vlc: usize) -> CidInfo { - let p = if cid == 1235 || cid == 1241 || cid == 1250 { 8 } else { 32 }; - CidInfo { cid, kind, weights, vlc, p } -} - -const fn ri(cid: u32, c0: u32, weights: &'static [[u8; 63]; 2], vlc: usize) -> CidInfo { - CidInfo { cid, kind: Kind::Ri { c0 }, weights, vlc, p: 32 } -} - -/// Every compression ID of SMPTE ST 2019-1:2016 (Tables C.1 and C.2). -pub(crate) const CIDS: [CidInfo; 20] = [ - hd(1235, Kind::Hd { width: 1920, height: 1080, interlaced: false, depth: 10, frame_size: 917504 }, &W_D_1, 0), - hd(1237, Kind::Hd { width: 1920, height: 1080, interlaced: false, depth: 8, frame_size: 606208 }, &W_D_2, 1), - hd(1238, Kind::Hd { width: 1920, height: 1080, interlaced: false, depth: 8, frame_size: 917504 }, &W_D_3, 2), - hd(1241, Kind::Hd { width: 1920, height: 1080, interlaced: true, depth: 10, frame_size: 917504 }, &W_D_4, 0), - hd(1242, Kind::Hd { width: 1920, height: 1080, interlaced: true, depth: 8, frame_size: 606208 }, &W_D_5, 1), - hd(1243, Kind::Hd { width: 1920, height: 1080, interlaced: true, depth: 8, frame_size: 917504 }, &W_D_6, 2), - hd(1244, Kind::Hd { width: 1440, height: 1080, interlaced: true, depth: 8, frame_size: 606208 }, &W_D_7, 1), - hd(1250, Kind::Hd { width: 1280, height: 720, interlaced: false, depth: 10, frame_size: 458752 }, &W_D_8, 3), - hd(1251, Kind::Hd { width: 1280, height: 720, interlaced: false, depth: 8, frame_size: 458752 }, &W_D_9, 4), - hd(1252, Kind::Hd { width: 1280, height: 720, interlaced: false, depth: 8, frame_size: 303104 }, &W_D_10, 5), - hd(1253, Kind::Hd { width: 1920, height: 1080, interlaced: false, depth: 8, frame_size: 188416 }, &W_D_2, 1), - hd(1256, Kind::Hd { width: 1920, height: 1080, interlaced: false, depth: 10, frame_size: 1835008 }, &W_D_11, 0), - hd(1258, Kind::Hd { width: 960, height: 720, interlaced: false, depth: 8, frame_size: 212992 }, &W_D_10, 5), - hd(1259, Kind::Hd { width: 1440, height: 1080, interlaced: false, depth: 8, frame_size: 417792 }, &W_D_2, 1), - hd(1260, Kind::Hd { width: 1440, height: 1080, interlaced: true, depth: 8, frame_size: 417792 }, &W_D_7, 1), - ri(1270, 1835008, &W_D_11, 0), - ri(1271, 917504, &W_D_4, 0), - ri(1272, 917504, &W_D_3, 2), - ri(1273, 606208, &W_D_2, 1), - ri(1274, 188416, &W_D_2, 1), -]; - -pub(crate) fn cid_info(cid: u32) -> Option<&'static CidInfo> { - CIDS.iter().find(|c| c.cid == cid) -} - -/// Compressed frame size of an RI raster (equation 7.1 / the `RIsize` pseudo code), without the -/// alpha factor when `alpha` is false. -pub fn ri_frame_size(width: u32, height: u32, c0: u32, alpha: bool) -> u32 { - let nw = width.div_ceil(16) as u64; - let ns = height.div_ceil(16) as u64; - let mut size = c0 as u64 * nw * ns / 8160; - if alpha { - size += size / 2; - } - let rem = size % 4096; - if rem >= 2048 { - size += 4096 - rem; - } else { - size -= rem; - } - size.max(8192) as u32 -} - -/// Two-level VLC lookup table. `primary` is indexed by the next `PRIMARY_BITS` bits; an entry is -/// either a decoded symbol (`len << 16 | value`, len ≤ PRIMARY_BITS) or a link to a secondary -/// table (`LINK | sub_bits << 24 | offset`), indexed by the following `sub_bits` bits. -pub(crate) struct Lut { - pub primary: Vec, - pub secondary: Vec, - pub bits: u32, -} - -pub(crate) const LINK: u32 = 1 << 31; - -impl Lut { - /// `codes`: (codeword, length, value ≤ 0xFFFF). - fn build(codes: &[(u32, u32, u32)], bits: u32) -> Lut { - let max_len = codes.iter().map(|c| c.1).max().unwrap_or(1); - let bits = bits.min(max_len); - let mut primary = vec![0u32; 1 << bits]; - // longest code under each long prefix - let mut sub_len = vec![0u32; 1 << bits]; - for &(code, len, _) in codes { - if len > bits { - let pre = (code >> (len - bits)) as usize; - sub_len[pre] = sub_len[pre].max(len - bits); - } - } - let mut secondary = Vec::new(); - for pre in 0..1usize << bits { - if sub_len[pre] > 0 { - let off = secondary.len() as u32; - secondary.resize(secondary.len() + (1 << sub_len[pre]), 0); - primary[pre] = LINK | sub_len[pre] << 24 | off; - } - } - for &(code, len, value) in codes { - if len <= bits { - let shift = bits - len; - let base = (code << shift) as usize; - for e in &mut primary[base..base + (1 << shift)] { - *e = len << 16 | value; - } - } else { - let pre = (code >> (len - bits)) as usize; - let link = primary[pre]; - let sb = (link >> 24) & 0x7f; - let off = (link & 0xff_ffff) as usize; - let rest_len = len - bits; - let rest = code & ((1 << rest_len) - 1); - let shift = sb - rest_len; - let base = off + ((rest << shift) as usize); - for e in &mut secondary[base..base + (1 << shift)] { - *e = len << 16 | value; - } - } - } - Lut { primary, secondary, bits } - } -} - -/// Decoded amplitude-table symbol value: `amp | frun << 8 | findex << 9 | eob << 10`. -pub(crate) const AMP_FRUN: u32 = 1 << 8; -pub(crate) const AMP_FINDEX: u32 = 1 << 9; -pub(crate) const AMP_EOB: u32 = 1 << 10; - -/// One VLC table set (amplitude, run and DC tables) with decode LUTs and encode maps. -pub(crate) struct VlcSet { - pub amp: Lut, - pub run: Lut, - pub dc: Lut, - /// Encoder: `amp_code[frun][findex][amp-1]` = (codeword, length). - pub amp_code: [[[(u16, u8); 64]; 2]; 2], - pub eob: (u16, u8), - /// Encoder: `run_code[run]` (run 1..=62; index 0 unused). - pub run_code: [(u16, u8); 63], - pub dc_code: Vec<(u16, u8)>, -} - -fn build_set(amp: &[(u16, u8, u8, u8, u8)], run: &[(u16, u8, u8); 62], dc: &[(u16, u8)]) -> VlcSet { - let ac: Vec<(u32, u32, u32)> = amp - .iter() - .map(|&(c, l, a, fr, fi)| { - let v = if a == 0 { AMP_EOB } else { a as u32 | (fr as u32) << 8 | (fi as u32) << 9 }; - (c as u32, l as u32, v) - }) - .collect(); - let rc: Vec<(u32, u32, u32)> = run.iter().map(|&(c, l, r)| (c as u32, l as u32, r as u32)).collect(); - let dcc: Vec<(u32, u32, u32)> = dc.iter().enumerate().map(|(i, &(c, l))| (c as u32, l as u32, i as u32)).collect(); - let mut amp_code = [[[(0u16, 0u8); 64]; 2]; 2]; - let mut eob = (0, 0); - for &(c, l, a, fr, fi) in amp { - if a == 0 { - eob = (c, l); - } else { - amp_code[fr as usize][fi as usize][a as usize - 1] = (c, l); - } - } - let mut run_code = [(0u16, 0u8); 63]; - for &(c, l, r) in run { - run_code[r as usize] = (c, l); - } - VlcSet { amp: Lut::build(&ac, 11), run: Lut::build(&rc, 10), dc: Lut::build(&dcc, 8), amp_code, eob, run_code, dc_code: dc.to_vec() } -} - -/// The VLC table set `i` (see [`CidInfo::vlc`]), built on first use. -pub(crate) fn vlc(i: usize) -> &'static VlcSet { - static SETS: [OnceLock; 6] = [const { OnceLock::new() }; 6]; - SETS[i].get_or_init(|| match i { - 0 => build_set(&_E_1, &RUN_E_1, &DC_E_1), - 1 => build_set(&_E_4, &RUN_E_4, &DC_E_4), - 2 => build_set(&_E_7, &RUN_E_7, &DC_E_7), - 3 => build_set(&_E_10, &RUN_E_10, &DC_E_10), - 4 => build_set(&_E_13, &RUN_E_13, &DC_E_13), - _ => build_set(&_E_16, &RUN_E_16, &DC_E_16), - }) -} - -/// 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 zigzag_is_permutation() { - let mut seen = [false; 64]; - for &z in &ZIGZAG { - assert!(!seen[z as usize]); - seen[z as usize] = true; - } - assert_eq!(ZIGZAG[1], 1); - assert_eq!(ZIGZAG[2], 8); - assert_eq!(ZIGZAG[63], 63); - } - - #[test] - fn ri_size_matches_examples() { - // 1080p HQ (1272) has 8160 macroblocks: exactly C0 - assert_eq!(ri_frame_size(1920, 1080, 917504, false), 917504); - // tiny rasters clamp to 8192 - assert_eq!(ri_frame_size(16, 16, 188416, false), 8192); - } - - #[test] - fn luts_decode_every_code() { - for i in 0..6 { - let s = vlc(i); - for f in 0..2 { - for x in 0..2 { - for a in 0..64 { - let (c, l) = s.amp_code[f][x][a]; - assert!(l > 0); - let v = lookup(&s.amp, c as u32, l as u32); - assert_eq!(v, (a as u32 + 1) | (f as u32) << 8 | (x as u32) << 9); - } - } - } - assert_eq!(lookup(&s.amp, s.eob.0 as u32, s.eob.1 as u32), AMP_EOB); - for r in 1..63 { - let (c, l) = s.run_code[r]; - assert_eq!(lookup(&s.run, c as u32, l as u32), r as u32); - } - } - } - - fn lookup(t: &Lut, code: u32, len: u32) -> u32 { - // left-align the code in a 32-bit window - let w = code << (32 - len); - let e = t.primary[(w >> (32 - t.bits)) as usize]; - let e = if e & LINK != 0 { - let sb = (e >> 24) & 0x7f; - let off = (e & 0xff_ffff) as usize; - t.secondary[off + ((w << t.bits) >> (32 - sb)) as usize] - } else { - e - }; - assert_eq!(e >> 16, len); - e & 0xffff - } -} diff --git a/crates/dnx/tests/it/common/mod.rs b/crates/dnx/tests/it/common/mod.rs deleted file mode 100644 index 996c1ba91..000000000 --- a/crates/dnx/tests/it/common/mod.rs +++ /dev/null @@ -1,303 +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_dnx::{Frame, probe}; -use std::path::{Path, PathBuf}; -use std::process::Command; - -/// ffmpeg, or `None` after printing `SKIPPED` (see `filmcraft_testkit::oracle`). -pub fn ffmpeg() -> Option { - filmcraft_testkit::ffmpeg_or_skip("dnx oracle") -} - -pub fn fixture_dir() -> PathBuf { - filmcraft_testkit::fixtures_dir("dnx") -} - -pub 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 DNxHD/DNxHR fixture: a lavfi source encoded by ffmpeg's VC-3 encoder into a MOV. -pub struct Spec { - pub name: &'static str, - pub lavfi: String, - pub frames: u32, - /// Encoder arguments (bitrate / profile, pixel format, flags). - pub enc: Vec<&'static str>, - /// Raw pixel format of the reference decode. - pub pix_fmt: &'static str, -} - -impl Spec { - pub fn new(name: &'static str, lavfi: impl Into, frames: u32, enc: &[&'static str], pix_fmt: &'static str) -> Spec { - Spec { name, lavfi: lavfi.into(), frames, enc: enc.to_vec(), pix_fmt } - } -} - -/// 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(), "-c:v", "dnxhd"]; - args.extend(spec.enc.iter().copied()); - args.extend(["-f", "mov"]); - let t = tmp.to_str().unwrap().to_string(); - args.push(&t); - run(ff, &args); - filmcraft_testkit::fixtures::publish(&tmp, &path).unwrap(); - } - path -} - -/// The coded frames of a MOV (ffmpeg's `data` muxer concatenates packets; split them with the -/// coding-unit sizes from the headers). -pub fn packets(ff: &Path, mov: &Path) -> Vec> { - 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> { - let mut v = Vec::new(); - while d.len() >= 0x280 { - let h = probe(d).expect("header"); - let mut n = h.coding_unit_size().expect("CBR") as usize; - if h.interlaced && !h.frame_encoding { - n += probe(&d[n..]).expect("second field").coding_unit_size().unwrap() as usize; - } - v.push(d[..n].to_vec()); - d = &d[n..]; - } - v -} - -/// Reference-decode a MOV with ffmpeg; samples as u16 (8-bit formats widened). -pub fn reference(ff: &Path, mov: &Path, pix_fmt: &str) -> Vec { - 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)); - if pix_fmt.ends_with("le") { - out.stdout.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect() - } else { - out.stdout.iter().map(|&b| b as u16).collect() - } -} - -#[derive(Default, Clone, Copy, Debug)] -pub struct Stats { - pub max: u32, - pub sum: u64, - pub n: u64, - pub exact: u64, - /// Samples off by more than 1. - pub over1: 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; - self.over1 += (d > 1) 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; - self.over1 += o.over1; - } - 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 - } -} - -/// Planes in the order of ffmpeg's planar raw output. -pub fn planes(f: &Frame, pix_fmt: &str) -> Vec { - // RGB frames hold G, B, R in y, cb, cr: the same order as `gbrp` - let _ = pix_fmt; - let order: Vec<&Vec> = vec![&f.y, &f.cb, &f.cr]; - order.into_iter().chain(f.alpha.as_ref()).flat_map(|p| p.iter().copied()).collect() -} - -/// Samples per frame of a raw reference image. -pub fn frame_samples(f: &Frame) -> usize { - let n = f.width as usize * f.height as usize; - let c = f.chroma_width() as usize * f.chroma_height() as usize; - n + 2 * c + if f.alpha.is_some() { n } else { 0 } -} - -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::() / 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 { - 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 video track, all samples in one chunk. -pub fn write_mov(path: &Path, fourcc: &[u8; 4], width: u16, height: u16, depth: u16, frames: &[Vec]) { - 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 = frames.concat(); - let data_off = ftyp.len() as u32 + 8; - let mdat = bx(b"mdat", &mdat_payload); - let matrix: Vec = 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); raw samples as u16. -pub fn ffmpeg_decode(ff: &Path, mov: &Path, pix_fmt: &str) -> Vec { - 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() && err.trim().is_empty(), "ffmpeg failed: {err}"); - if pix_fmt.ends_with("le") { - out.stdout.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect() - } else { - out.stdout.iter().map(|&b| b as u16).collect() - } -} - -/// Render a lavfi source to raw 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: filmcraft_dnx::ChromaFormat, - pub depth: u8, -} - -pub fn source_frames(ff: &Path, spec: SourceFrameSpec<'_>) -> Vec { - let SourceFrameSpec { name, lavfi, frames, width: w, height: h, chroma, depth } = spec; - let pix = match (chroma, depth) { - (filmcraft_dnx::ChromaFormat::Yuv422, 8) => "yuv422p", - (filmcraft_dnx::ChromaFormat::Yuv422, 10) => "yuv422p10le", - (filmcraft_dnx::ChromaFormat::Yuv422, _) => "yuv422p12le", - (_, 10) => "yuv444p10le", - _ => "yuv444p12le", - }; - let path = fixture_dir().join(format!("{name}.{pix}.raw")); - if !path.exists() { - 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 = if depth == 8 { - bytes.iter().map(|&b| b as u16).collect() - } else { - bytes.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect() - }; - let proto = Frame::new(w, h, chroma, depth, false); - let per = frame_samples(&proto); - assert_eq!(s.len(), per * frames as usize); - s.chunks_exact(per) - .map(|p| { - let mut f = proto.clone(); - 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]); - f - }) - .collect() -} diff --git a/crates/dnx/tests/it/fuzz_hostile.rs b/crates/dnx/tests/it/fuzz_hostile.rs deleted file mode 100644 index a20e26a2b..000000000 --- a/crates/dnx/tests/it/fuzz_hostile.rs +++ /dev/null @@ -1,183 +0,0 @@ -//! Hostile-input robustness: every header byte set to extreme values, every bit flipped, -//! truncation at (nearly) every length and random mutation of encoder-made frames must never -//! panic or run away (errors are fine). No ffmpeg needed. - -use std::panic::{AssertUnwindSafe, catch_unwind}; - -use filmcraft_dnx::{ChromaFormat, DecodeOptions, Encoder, EncoderConfig, Frame, Profile, decode_frame_with, probe}; - -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> { - let mut v = Vec::new(); - for (profile, depth, w, h) in - [(Profile::Lb, 8, 64u32, 48u32), (Profile::Hq, 8, 48, 32), (Profile::Hqx, 10, 64, 32), (Profile::Hqx, 12, 48, 32), (Profile::R444, 10, 48, 32)] - { - let mut cfg = EncoderConfig::new(profile, w, h); - cfg.bit_depth = depth; - let chroma = cfg.chroma(); - let mut f = Frame::new(w, h, chroma, depth, false); - for (i, s) in f.y.iter_mut().enumerate() { - *s = ((16 + (i * 37) % 200) as u16) << (depth - 8); - } - if chroma != ChromaFormat::Yuv444 { - f.cb.fill(128 << (depth - 8)); - f.cr.fill(100 << (depth - 8)); - } - if let Ok(mut enc) = Encoder::with_config(cfg) - && let Ok(d) = enc.encode(&f) - { - v.push(d); - } - } - v -} - -fn run(d: &[u8], label: &str) { - filmcraft_testkit::set_progress(label); - let res = catch_unwind(AssertUnwindSafe(|| { - let _ = probe(d); - for opts in [DecodeOptions::default(), DecodeOptions { threads: true, spec_dequant: true }] { - let _ = decode_frame_with(d, &opts); - } - })); - 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; - -/// CI runs a rotating share of every part of the sweep; `FILMCRAFT_FUZZ_ITERS=1500` runs all of it (see [`Depth`]). -#[test] -fn hostile_frames_never_panic() { - filmcraft_testkit::with_deadline(DEADLINE_SECS, sweep); -} - -fn sweep() { - let depth = Depth::from_env(); - let seeds = seeds(); - assert!(!seeds.is_empty()); - for (si, seed) in seeds.iter().enumerate() { - for n in depth.truncations(seed.len()) { - run(&seed[..n], &format!("seed {si} truncate {n}")); - } - header_flips(&depth, seed, si); - big_fields(seed, si); - random_rounds(&depth, seed, si); - } -} - -/// The header (and the start of the scan-line index): every byte to extreme values, every bit. -fn header_flips(depth: &Depth, seed: &[u8], si: usize) { - for p in 0..seed.len().min(0x300) { - for (i, v) in VALUES.into_iter().enumerate() { - if !depth.value(p, i) { - continue; - } - let mut d = seed.to_vec(); - d[p] = v; - run(&d, &format!("seed {si} byte {p:#x}={v:#x}")); - } - for b in 0..8 { - if !depth.bit(p, b) { - continue; - } - let mut d = seed.to_vec(); - d[p] ^= 1 << b; - run(&d, &format!("seed {si} flip {p:#x}.{b}")); - } - } -} - -/// Big dimension fields with the rest intact (lines 0x18, width 0x1A, macroblock rows 0x16C). -fn big_fields(seed: &[u8], si: usize) { - for (off, v) in [(0x18usize, 0xffffu16), (0x1a, 0xffff), (0x18, 0x4000), (0x1a, 0x4000), (0x16c, 0xffff)] { - let mut d = seed.to_vec(); - if let Some(s) = d.get_mut(off..off + 2) { - s.copy_from_slice(&v.to_be_bytes()); - } - run(&d, &format!("seed {si} u16 {off:#x}={v:#x}")); - } -} - -fn random_rounds(depth: &Depth, seed: &[u8], si: usize) { - let mut rng = Rng(0x9e3779b97f4a7c15 ^ si as u64); - // Skipped rounds still draw their mutations, so round i matches round i of a full run. - for i in 0..depth.rounds { - let mut d = seed.to_vec(); - 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; - } - } - if depth.round(i) { - run(&d, &format!("seed {si} random {i}")); - } - } -} - -const VALUES: [u8; 5] = [0, 0xff, 0x80, 0x01, 0x7f]; - -/// How much of the sweep runs. `FILMCRAFT_FUZZ_ITERS=N` runs all of it (about 400 truncation -/// points spread over each seed, every header byte, overwrite value and bit flip) plus N random -/// rounds per seed: 1500 is the full depth, more hunts longer. Unset, CI runs a third of the -/// truncation points and of the full depth's random rounds and, for each header byte, one value -/// and two bits. A rotation offset shifts which share runs (`FILMCRAFT_FUZZ_SEED`, else the UTC -/// day number), so every case of the full sweep runs within a few days, and a random round's -/// mutations match the same round of a full run. -struct Depth { - full: bool, - truncations: usize, - rounds: usize, - rotation: usize, -} - -impl Depth { - fn from_env() -> Self { - match std::env::var("FILMCRAFT_FUZZ_ITERS").ok().and_then(|s| s.parse().ok()) { - Some(rounds) => Depth { full: true, truncations: 400, rounds, rotation: 0 }, - None => { - let rotation = rotation(); - eprintln!("fuzz subset rotation {rotation} (FILMCRAFT_FUZZ_SEED={rotation} replays it; FILMCRAFT_FUZZ_ITERS=1500 runs everything)"); - Depth { full: false, truncations: 400, rounds: 1500, rotation } - } - } - } - /// Truncation lengths for a `len`-byte input: every point of the full sweep, or every third one. - fn truncations(&self, len: usize) -> std::iter::StepBy> { - let stride = (len / self.truncations).max(1); - let (start, step) = if self.full { (0, stride) } else { ((self.rotation % 3) * stride, 3 * stride) }; - (start..len).step_by(step) - } - fn value(&self, p: usize, i: usize) -> bool { - self.full || i == (p + self.rotation % VALUES.len()) % VALUES.len() - } - fn bit(&self, p: usize, b: usize) -> bool { - self.full || (b + p + self.rotation % 4).is_multiple_of(4) - } - fn round(&self, i: usize) -> bool { - self.full || i % 3 == self.rotation % 3 - } -} - -/// The default subset's rotation offset: `FILMCRAFT_FUZZ_SEED`, else days since the Unix epoch (UTC). -fn rotation() -> usize { - std::env::var("FILMCRAFT_FUZZ_SEED").ok().and_then(|s| s.parse().ok()).unwrap_or_else(|| { - let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs()); - usize::try_from(secs / 86_400).unwrap_or(0) - }) -} diff --git a/crates/dnx/tests/it/main.rs b/crates/dnx/tests/it/main.rs deleted file mode 100644 index c12ebd099..000000000 --- a/crates/dnx/tests/it/main.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod common; -mod fuzz_hostile; -mod oracle_decode; -mod oracle_encode; -mod perf; -mod roundtrip; diff --git a/crates/dnx/tests/it/oracle_decode.rs b/crates/dnx/tests/it/oracle_decode.rs deleted file mode 100644 index a9da675c8..000000000 --- a/crates/dnx/tests/it/oracle_decode.rs +++ /dev/null @@ -1,83 +0,0 @@ -//! Decode ffmpeg-made DNxHD / DNxHR fixtures and compare with ffmpeg's own decode. - -use crate::common; -use common::*; -use filmcraft_dnx::{DecodeOptions, decode_frame_with}; - -fn specs() -> Vec { - let t = |s: &str, r: u32| format!("testsrc2=s={s}:r={r}"); - let m = |s: &str, r: u32| format!("mandelbrot=s={s}:r={r}"); - let n = |s: &str, r: u32| format!("testsrc2=s={s}:r={r},noise=alls=60:allf=t,lutyuv=y=clip(val\\,24\\,231):u=clip(val\\,24\\,231):v=clip(val\\,24\\,231)"); - vec![ - // DNxHD (HD profile) - Spec::new("hd1237_testsrc", t("1920x1080", 25), 2, &["-b:v", "120M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1235_mandel", m("1920x1080", 25), 2, &["-b:v", "185M", "-pix_fmt", "yuv422p10le"], "yuv422p10le"), - Spec::new("hd1238_noise", n("1920x1080", 25), 2, &["-b:v", "185M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1253_testsrc", t("1920x1080", 25), 2, &["-b:v", "36M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1250_noise", n("1280x720", 50), 2, &["-b:v", "90M", "-pix_fmt", "yuv422p10le"], "yuv422p10le"), - Spec::new("hd1251_testsrc", t("1280x720", 50), 2, &["-b:v", "90M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1252_mandel", m("1280x720", 50), 2, &["-b:v", "60M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1258_testsrc", t("960x720", 50), 2, &["-b:v", "60M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1259_testsrc", t("1440x1080", 25), 2, &["-b:v", "84M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1242_interlaced", t("1920x1080", 25), 2, &["-flags", "+ildct", "-b:v", "120M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1241_interlaced", n("1920x1080", 25), 2, &["-flags", "+ildct", "-b:v", "185M", "-pix_fmt", "yuv422p10le"], "yuv422p10le"), - Spec::new("hd1243_interlaced", m("1920x1080", 25), 2, &["-flags", "+ildct", "-b:v", "220M", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hd1244_interlaced", t("1440x1080", 25), 2, &["-flags", "+ildct", "-b:v", "120M", "-pix_fmt", "yuv422p"], "yuv422p"), - // DNxHR (RI profile) - Spec::new("hr_lb_testsrc", t("1920x1080", 25), 2, &["-profile:v", "dnxhr_lb", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hr_sq_mandel", m("1920x1080", 25), 2, &["-profile:v", "dnxhr_sq", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hr_hq_noise", n("1920x1080", 25), 2, &["-profile:v", "dnxhr_hq", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hr_hqx_mandel", m("1920x1080", 25), 2, &["-profile:v", "dnxhr_hqx", "-pix_fmt", "yuv422p10le"], "yuv422p10le"), - Spec::new("hr_hqx_noise_odd", n("1000x562", 25), 2, &["-profile:v", "dnxhr_hqx", "-pix_fmt", "yuv422p10le"], "yuv422p10le"), - Spec::new("hr_444_testsrc", t("1280x720", 25), 2, &["-profile:v", "dnxhr_444", "-pix_fmt", "yuv444p10le"], "yuv444p10le"), - Spec::new("hr_444_rgb_mandel", m("640x360", 25), 2, &["-profile:v", "dnxhr_444", "-pix_fmt", "gbrp10le"], "gbrp10le"), - Spec::new("hr_hq_small", t("256x120", 25), 2, &["-profile:v", "dnxhr_hq", "-pix_fmt", "yuv422p"], "yuv422p"), - Spec::new("hr_sq_uhd", t("3840x2160", 25), 1, &["-profile:v", "dnxhr_sq", "-pix_fmt", "yuv422p"], "yuv422p"), - ] -} - -/// Generate every fixture (used by `cargo xtask fixtures dnx`). -#[test] -#[ignore] -fn generate_fixtures() { - let Some(ff) = ffmpeg() else { return }; - for s in specs() { - make(&ff, &s); - } -} - -#[test] -fn decode_matches_ffmpeg() { - let Some(ff) = ffmpeg() else { return }; - let mut worst = 0; - for spec in specs() { - let mov = make(&ff, &spec); - let pk = packets(&ff, &mov); - assert_eq!(pk.len(), spec.frames as usize, "{}", spec.name); - let raw = reference(&ff, &mov, spec.pix_fmt); - let mut st = Stats::default(); - let mut off = 0; - for (i, p) in pk.iter().enumerate() { - let f = - decode_frame_with(p, &DecodeOptions { threads: i % 2 == 0, ..Default::default() }).unwrap_or_else(|e| panic!("{} frame {i}: {e}", spec.name)); - let ours = planes(&f, spec.pix_fmt); - let n = frame_samples(&f); - st.add(&ours, &raw[off..off + n]); - off += n; - } - assert_eq!(off, raw.len(), "{}: frame size mismatch", spec.name); - println!( - "{:<22} cid {} max {} mean {:.5} exact {:.3}% beyond ±1: {}", - spec.name, - filmcraft_dnx::probe(&pk[0]).unwrap().cid, - st.max, - st.mean(), - st.exact_pct(), - st.over1 - ); - worst = worst.max(st.max); - // the remaining differences are ffmpeg's integer IDCT: ±1, at most ~50 samples per million at ±2 - assert!(st.over1 * 10_000 <= st.n, "{}: {} samples beyond ±1", spec.name, st.over1); - } - assert!(worst <= 2, "max error {worst}"); -} diff --git a/crates/dnx/tests/it/oracle_encode.rs b/crates/dnx/tests/it/oracle_encode.rs deleted file mode 100644 index 6a60f1145..000000000 --- a/crates/dnx/tests/it/oracle_encode.rs +++ /dev/null @@ -1,76 +0,0 @@ -//! Encode DNxHR with our encoder, decode with ffmpeg (`-xerror`) and with our decoder. - -use crate::common; -use common::*; -use filmcraft_dnx::{ChromaFormat, Encoder, EncoderConfig, Profile, decode_frame}; - -struct Case { - name: &'static str, - profile: Profile, - depth: u8, - lavfi: &'static str, - min_psnr: f64, -} - -#[test] -fn encode_decodes_in_ffmpeg() { - let Some(ff) = ffmpeg() else { return }; - let (w, h) = (1920u32, 1080u32); - let cases = [ - Case { name: "enc_sq_mandel", profile: Profile::Sq, depth: 8, lavfi: "mandelbrot=s=1920x1080:r=25", min_psnr: 40.0 }, - Case { name: "enc_hq_mandel", profile: Profile::Hq, depth: 8, lavfi: "mandelbrot=s=1920x1080:r=25", min_psnr: 45.0 }, - Case { name: "enc_hq_testsrc", profile: Profile::Hq, depth: 8, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 45.0 }, - Case { name: "enc_hqx10_mandel", profile: Profile::Hqx, depth: 10, lavfi: "mandelbrot=s=1920x1080:r=25", min_psnr: 45.0 }, - Case { name: "enc_hqx12_testsrc", profile: Profile::Hqx, depth: 12, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 45.0 }, - Case { name: "enc_lb_testsrc", profile: Profile::Lb, depth: 8, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 30.0 }, - Case { name: "enc_444_testsrc", profile: Profile::R444, depth: 10, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 45.0 }, - ]; - for c in cases { - let mut cfg = EncoderConfig::new(c.profile, w, h); - cfg.bit_depth = c.depth; - let chroma = cfg.chroma(); - let src = source_frames(&ff, SourceFrameSpec { name: c.name, lavfi: c.lavfi, frames: 2, width: w, height: h, chroma, depth: c.depth }); - let mut enc = Encoder::with_config(cfg).unwrap(); - let t = std::time::Instant::now(); - let coded: Vec> = src.iter().map(|f| enc.encode(f).unwrap()).collect(); - let ms = t.elapsed().as_secs_f64() * 1000.0 / src.len() as f64; - for p in &coded { - assert_eq!(p.len() as u32, enc.frame_size(), "{}: CBR frame size", c.name); - } - let mov = fixture_dir().join(format!("{}.out.mov", c.name)); - write_mov(&mov, b"AVdh", w as u16, h as u16, 24, &coded); - let pix = match (chroma, c.depth) { - (ChromaFormat::Yuv444, 10) => "yuv444p10le", - (_, 8) => "yuv422p", - (_, 10) => "yuv422p10le", - _ => "yuv422p12le", - }; - // ffmpeg reads 4:4:4 Y'CbCr DNxHR as gbrp unless every macroblock is flagged - let ff_raw = ffmpeg_decode(&ff, &mov, pix); - let mut agree = Stats::default(); - let mut psnr_min = f64::MAX; - let per = frame_samples(&src[0]); - assert_eq!(ff_raw.len(), per * coded.len(), "{}: ffmpeg frame count", c.name); - for (i, p) in coded.iter().enumerate() { - let f = decode_frame(p).unwrap(); - let ours = planes(&f, pix); - agree.add(&ours, &ff_raw[i * per..(i + 1) * per]); - let peak = ((1u32 << c.depth) - 1) as f64; - let srcp = planes(&src[i], pix); - psnr_min = psnr_min.min(psnr(&srcp[..f.y.len()], &ours[..f.y.len()], peak)); - } - println!( - "{:<18} {} {}-bit: {} bytes/frame, {:.1} ms/frame, Y PSNR {:.2} dB, vs ffmpeg max {} exact {:.2}%", - c.name, - c.profile.name(), - c.depth, - enc.frame_size(), - ms, - psnr_min, - agree.max, - agree.exact_pct() - ); - assert!(agree.max <= 2, "{}: ffmpeg disagrees by {}", c.name, agree.max); - assert!(psnr_min >= c.min_psnr, "{}: PSNR {psnr_min:.2}", c.name); - } -} diff --git a/crates/dnx/tests/it/perf.rs b/crates/dnx/tests/it/perf.rs deleted file mode 100644 index befd20a48..000000000 --- a/crates/dnx/tests/it/perf.rs +++ /dev/null @@ -1,59 +0,0 @@ -//! Throughput: `cargo test --release -p filmcraft-dnx --test it perf:: -- --ignored --nocapture`. - -use filmcraft_dnx::{ChromaFormat, DecodeOptions, Encoder, EncoderConfig, Frame, Profile, decode_frame_with}; -use std::time::Instant; - -fn busy(w: u32, h: u32, chroma: ChromaFormat, depth: u8) -> Frame { - let mut f = Frame::new(w, h, chroma, depth, false); - let max = (1u32 << depth) - 1; - let mut s = 1u32; - for v in f.y.iter_mut().chain(f.cb.iter_mut()).chain(f.cr.iter_mut()) { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - *v = (((*v as u32) + (s % (24 << (depth - 8)))).min(max)) as u16; - } - for y in 0..h { - for x in 0..w { - let i = (y * w + x) as usize; - f.y[i] = (f.y[i] as u32 + (((x ^ y) % 160) << (depth - 8))).min(max) as u16; - } - } - f -} - -#[test] -#[ignore] -fn throughput() { - for (profile, depth, w, h) in [(Profile::Hq, 8, 1920, 1080), (Profile::Hqx, 10, 1920, 1080), (Profile::Sq, 8, 3840, 2160), (Profile::Hqx, 10, 3840, 2160)] { - let mut cfg = EncoderConfig::new(profile, w, h); - cfg.bit_depth = depth; - let src = busy(w, h, cfg.chroma(), depth); - let mut enc = Encoder::with_config(cfg).unwrap(); - let data = enc.encode(&src).unwrap(); - let n = if w > 1920 { 5 } else { 20 }; - let t = Instant::now(); - for _ in 0..n { - enc.encode(&src).unwrap(); - } - let enc_fps = n as f64 / t.elapsed().as_secs_f64(); - let mut fps = [0f64; 2]; - for (k, threads) in [true, false].into_iter().enumerate() { - let opts = DecodeOptions { threads, ..Default::default() }; - let m = if threads { n * 3 } else { n / 2 }; - let t = Instant::now(); - for _ in 0..m { - decode_frame_with(&data, &opts).unwrap(); - } - fps[k] = m as f64 / t.elapsed().as_secs_f64(); - } - println!( - "{} {depth}-bit {w}x{h} ({} KB/frame): decode {:.0} fps threaded, {:.1} fps single-thread; encode {:.1} fps", - profile.name(), - data.len() / 1024, - fps[0], - fps[1], - enc_fps - ); - } -} diff --git a/crates/dnx/tests/it/roundtrip.rs b/crates/dnx/tests/it/roundtrip.rs deleted file mode 100644 index fff8781d1..000000000 --- a/crates/dnx/tests/it/roundtrip.rs +++ /dev/null @@ -1,147 +0,0 @@ -//! Encoder → decoder round trips and robustness against corrupt data (no ffmpeg needed). - -use filmcraft_dnx::{ChromaFormat, DecodeOptions, Encoder, EncoderConfig, Frame, Profile, decode_frame, decode_frame_with, probe}; - -/// A synthetic picture: gradients, a ring pattern and some hashed texture. -fn picture(w: u32, h: u32, chroma: ChromaFormat, depth: u8, seed: u32) -> Frame { - let mut f = Frame::new(w, h, chroma, depth, false); - let max = (1u32 << depth) - 1; - let s = depth as u32 - 8; - let hash = |x: u32, y: u32| (x.wrapping_mul(73856093) ^ y.wrapping_mul(19349663) ^ seed.wrapping_mul(83492791)) % 17; - for y in 0..h { - for x in 0..w { - let r = (((x as f32 - w as f32 / 2.0).powi(2) + (y as f32 - h as f32 / 2.0).powi(2)).sqrt() / 6.0).sin(); - let v = (16 << s) as f32 + (219 << s) as f32 * (0.5 + 0.3 * r + 0.2 * (x as f32 / w as f32 - 0.5)) + (hash(x, y) << s) as f32; - f.y[(y * w + x) as usize] = (v as u32).min(max) as u16; - } - } - let cw = f.chroma_width(); - for y in 0..f.chroma_height() { - for x in 0..cw { - let i = (y * cw + x) as usize; - f.cb[i] = (((128 + (x * 90 / cw) as i32 - 45) as u32) << s) as u16; - f.cr[i] = (((128 + (y * 80 / f.chroma_height()) as i32 - 40) as u32) << s) as u16; - } - } - f -} - -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::() / a.len() as f64; - if mse == 0.0 { 99.0 } else { 10.0 * (peak * peak / mse).log10() } -} - -#[test] -fn roundtrip_all_profiles() { - for (profile, depth, w, h) in [ - (Profile::Lb, 8, 640, 360), - (Profile::Sq, 8, 1280, 720), - (Profile::Hq, 8, 720, 486), - (Profile::Hqx, 10, 1000, 562), - (Profile::Hqx, 12, 512, 288), - (Profile::R444, 10, 400, 300), - (Profile::R444, 12, 64, 48), - ] { - let mut cfg = EncoderConfig::new(profile, w, h); - cfg.bit_depth = depth; - let chroma = cfg.chroma(); - let mut enc = Encoder::with_config(cfg).unwrap(); - for seed in 0..2 { - let src = picture(w, h, chroma, depth, seed); - let data = enc.encode(&src).unwrap(); - assert_eq!(data.len() as u32, enc.frame_size()); - assert_eq!(&data[data.len() - 4..], &[0x60, 0x0D, 0xC0, 0xDE]); - let hdr = probe(&data).unwrap(); - assert_eq!((hdr.cid, hdr.width, hdr.lines, hdr.bit_depth), (profile.cid(), w, h, depth)); - let dec = decode_frame(&data).unwrap(); - let single = decode_frame_with(&data, &DecodeOptions { threads: false, ..Default::default() }).unwrap(); - assert_eq!(dec, single, "threaded and single-threaded decodes differ"); - assert_eq!((dec.width, dec.height, dec.chroma, dec.bit_depth, dec.rgb), (w, h, chroma, depth, false)); - let peak = ((1u32 << depth) - 1) as f64; - let p = psnr(&src.y, &dec.y, peak); - let pc = psnr(&src.cb, &dec.cb, peak); - let min = if profile == Profile::Lb { 30.0 } else { 38.0 }; - assert!(p > min && pc > min, "{profile:?} {depth}-bit {w}x{h}: PSNR {p:.1}/{pc:.1}"); - } - } -} - -#[test] -fn rejects_bad_input() { - let mut enc = Encoder::new(Profile::Hq, 64, 64).unwrap(); - assert!(enc.encode(&Frame::new(32, 64, ChromaFormat::Yuv422, 8, false)).is_err()); - assert!(enc.encode(&Frame::new(64, 64, ChromaFormat::Yuv444, 8, false)).is_err()); - assert!(Encoder::with_config(EncoderConfig { bit_depth: 10, ..EncoderConfig::new(Profile::Hq, 64, 64) }).is_err()); - assert!(decode_frame(&[0u8; 100]).is_err()); - assert!(decode_frame(&[0u8; 1000]).is_err()); - // An unsupported raster is an error, not a panic. - assert!(Encoder::new(Profile::Hq, 0, 0).is_err()); - assert!(Encoder::new(Profile::Hq, 20_000, 64).is_err()); -} - -#[test] -fn corrupt_streams_never_panic() { - let mut enc = Encoder::new(Profile::Hq, 256, 128).unwrap(); - let good = enc.encode(&picture(256, 128, ChromaFormat::Yuv422, 8, 1)).unwrap(); - let mut seed = 0x1234_5678u32; - let mut rnd = || { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - seed - }; - for i in 0..400 { - let mut d = good.clone(); - let n = 1 + rnd() % 8; - for _ in 0..n { - // mostly the payload, sometimes the header - let pos = if i % 4 == 0 { rnd() as usize % 0x280 } else { 0x280 + rnd() as usize % (d.len() - 0x280) }; - d[pos] ^= 1 << (rnd() % 8); - } - if i % 5 == 0 { - d.truncate(rnd() as usize % d.len()); - } - let _ = decode_frame(&d); - } -} - -#[test] -fn rejects_repeated_scan_line_indices() { - // Every scan index pointing at the first row's payload must not let one coded row fill the - // whole raster. - let mut enc = Encoder::new(Profile::Hq, 256, 128).unwrap(); - let mut d = enc.encode(&picture(256, 128, ChromaFormat::Yuv422, 8, 2)).unwrap(); - assert!(decode_frame(&d).is_ok()); - let h = filmcraft_dnx::probe(&d).unwrap(); - assert!(h.mb_rows > 1); - let first = filmcraft_dnx::header::SCAN_INDEX_OFFSET; - let idx: [u8; 4] = d[first..first + 4].try_into().unwrap(); - for s in 1..h.mb_rows as usize { - d[first + 4 * s..first + 4 * s + 4].copy_from_slice(&idx); - } - assert!(decode_frame(&d).is_err()); -} - -#[test] -fn rejects_frames_over_the_memory_limit() { - // A compact header-only frame claiming a 16384x16384 4:4:4 raster with alpha is refused before - // any plane is allocated. - let mut enc = Encoder::new(Profile::R444, 64, 64).unwrap(); - let d = enc.encode(&picture(64, 64, ChromaFormat::Yuv444, 10, 3)).unwrap(); - let mut h = filmcraft_dnx::probe(&d).unwrap(); - h.width = 16384; - h.lines = 16384; - h.mb_rows = 1024; - h.alpha = true; - h.header_size = filmcraft_dnx::header::header_size_for(h.lines); - let mut big = filmcraft_dnx::header::write(&h); - let hs = big.len() as u32; - for s in 0..h.mb_rows as usize { - let o = filmcraft_dnx::header::SCAN_INDEX_OFFSET + 4 * s; - big[o..o + 4].copy_from_slice(&(s as u32).to_be_bytes()); - } - big.resize(hs as usize + 4096, 0); - assert!(filmcraft_dnx::probe(&big).is_ok()); - let err = decode_frame(&big).unwrap_err().to_string(); - assert!(err.contains("memory limit"), "{err}"); -} diff --git a/crates/dnx/tools/extract_tables.py b/crates/dnx/tools/extract_tables.py deleted file mode 100644 index 2e120cc93..000000000 --- a/crates/dnx/tools/extract_tables.py +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/env python3 -"""Extract the VC-3 constant tables (Annex D quantization weights, Annex E VLC tables) from the -text of the public SMPTE ST 2019-1:2016 standard. - -Usage: - curl -LO https://pub.smpte.org/doc/st2019-1/20160518-pub/st2019-1-20160518-pub.zip - unzip st2019-1-20160518-pub.zip - pdftotext -layout st2019-1-2016.pdf vc3.txt - python3 extract_tables.py vc3.txt > ../src/spec_tables.rs - -Only the standard's text is read. Every table's size is checked (63 AC weights per matrix, 4*64+1 -amplitude codewords including EOB, 62 run codewords) and every VLC table must be a complete -prefix code. -""" -import re -import sys -from pathlib import Path - - -def _spec_path(arg): - """Resolve a CLI path argument and require it to be an existing regular file.""" - path = Path(arg).resolve() - if not path.is_file(): - sys.exit(f"not a readable file: {arg}") - return path - - -text = open(_spec_path(sys.argv[1]), encoding="utf-8").read() -lines = text.split("\n") - - -def section(start_pat, end_pat): - s = next(i for i, l in enumerate(lines) if re.search(start_pat, l)) - e = next(i for i in range(s + 1, len(lines)) if re.search(end_pat, lines[i])) - return lines[s + 1:e] - - -def weights(sec): - """The 63 AC weights of an 8x8 table printed in raster order with '-' for the DC entry.""" - nums = [] - for l in sec: - if "Page" in l or "SMPTE" in l or "Compression" in l: - continue - nums += [int(x) for x in re.findall(r"\b\d+\b", l)] - return nums - - -def check_prefix(codes): - """Kraft sum of a prefix code (1.0 when complete).""" - for a in codes: - for b in codes: - if a is not b and b.startswith(a): - raise SystemExit("not a prefix code: %s %s" % (a, b)) - return sum(2.0 ** -len(c) for c in codes) - - -out = ["// Generated by tools/extract_tables.py from SMPTE ST 2019-1:2016 (VC-3), Annexes D and E.", - "// Do not edit by hand.", ""] - -# ---- Annex D: weights in raster order (v*8+u), DC entry omitted ---- -dtables = [("D_1", r"Table D\.1 – Compression ID 1235"), ("D_2", r"Table D\.2 – Compression ID 1237"), - ("D_3", r"^Table D\.3 – Compression"), ("D_4", r"Table D\.4 – Compression"), - ("D_5", r"Table D\.5 – Compression"), ("D_6", r"Table D\.6 – Compression"), - ("D_7", r"^Table D\.7 – Compression"), ("D_8", r"Table D\.8 – Compression"), - ("D_9", r"Table D\.9 – Compression"), ("D_10", r"Table D\.10 – Compression"), - ("D_11", r"^Table D\.11 – Compression")] -for i, (name, pat) in enumerate(dtables): - end = dtables[i + 1][1] if i + 1 < len(dtables) else r"^Annex E VLC Tables" - sec = section(pat, end) - if name == "D_11": - sec = [l for l in sec if "Compression ID" not in l] - nums = weights(sec) - assert len(nums) == 63, (name, len(nums)) - out.append("pub const W_%s: [[u8; 63]; 2] = [%s, %s];" % (name, nums, nums)) - continue - li = next(j for j, l in enumerate(sec) if "Luma Quantization" in l) - ci = next(j for j, l in enumerate(sec) if "Chroma Quantization" in l) - luma = weights(sec[li + 1:ci]) - chroma = weights(sec[ci + 1:]) - assert len(luma) == 63 and len(chroma) == 63, (name, len(luma), len(chroma)) - out.append("pub const W_%s: [[u8; 63]; 2] = [%s, %s];" % (name, luma, chroma)) -out.append("") - -# ---- Annex E ---- -etables = ["E.1", "E.4", "E.7", "E.10", "E.13", "E.16"] -for n, t in enumerate(etables): - a = int(t[2:]) - amp = section(r"^\s*Table E\.%d – Amplitude" % a, r"Table E\.%d – Run" % (a + 1)) - run = section(r"Table E\.%d – Run" % (a + 1), r"Table E\.%d – DC" % (a + 2)) - nxt = r"Table E\.%d – Amplitude" % (a + 3) if n + 1 < len(etables) else r"^Annex F System" - dc = section(r"Table E\.%d – DC" % (a + 2), nxt) - ent = [] - for l in amp: - for m in re.finditer(r"\b([01]+)\s+(\d+)\s+(EOB1?|\d+)\s+([01])\s+([01])\b", l): - code, ln, am, fr, fi = m.groups() - assert len(code) == int(ln), m.group(0) - ent.append((code, 0 if am.startswith("EOB") else int(am), int(fr), int(fi), am.startswith("EOB"))) - k = check_prefix([e[0] for e in ent]) - assert abs(k - 1.0) < 1e-12, (t, k, len(ent)) - assert sum(e[4] for e in ent) == 1 and len(ent) == 4 * 64 + 1, (t, len(ent)) - rent = [] - for l in run: - for m in re.finditer(r"\b([01]+)\s+(\d+)\s+(\d+)\b", l): - code, ln, r = m.groups() - if len(code) != int(ln): - continue - rent.append((code, int(r))) - assert len(rent) == 62 and sorted(r for _, r in rent) == list(range(1, 63)), (t, len(rent)) - assert abs(check_prefix([c for c, _ in rent]) - 1.0) < 1e-12 - dent = [] - for l in dc: - m = re.match(r"^\s+([01]+)\s+(\d+)\s*$", l) - if m: - dent.append((m.group(1), int(m.group(2)))) - assert [e for _, e in dent] == list(range(len(dent))), (t, dent) - assert abs(check_prefix([c for c, _ in dent]) - 1.0) < 1e-12, (t, dent) - name = t.replace(".", "_") - out.append("/// Table %s: (codeword, length, amplitude (0 = EOB), Frun, Findex)." % t) - out.append("pub const AMP_%s: [(u16, u8, u8, u8, u8); %d] = [" % (name, len(ent))) - for c, a_, fr, fi, _ in ent: - out.append(" (0b%s, %d, %d, %d, %d)," % (c, len(c), a_, fr, fi)) - out.append("];") - out.append("/// Table E.%d: (codeword, length, run)." % (a + 1)) - out.append("pub const RUN_%s: [(u16, u8, u8); 62] = [" % name) - for c, r in rent: - out.append(" (0b%s, %d, %d)," % (c, len(c), r)) - out.append("];") - out.append("/// Table E.%d: (codeword, length) indexed by the DC size.", ) - out[-1] = "/// Table E.%d: (codeword, length) indexed by the DC size (eta)." % (a + 2) - out.append("pub const DC_%s: [(u16, u8); %d] = [%s];" % (name, len(dent), ", ".join("(0b%s, %d)" % (c, len(c)) for c, _ in dent))) - out.append("") - -print("\n".join(out)) diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index fa39b0b4e..47ecf1584 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -24,7 +24,6 @@ filmcraft-audio-dsp = { workspace = true } filmcraft-geom = { workspace = true } filmcraft-text = { workspace = true } filmcraft-isobmff = { workspace = true } -filmcraft-dnx = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/export/src/encoders.rs b/crates/export/src/encoders.rs index 6acccd4ec..ab6b5bfb7 100644 --- a/crates/export/src/encoders.rs +++ b/crates/export/src/encoders.rs @@ -199,8 +199,8 @@ pub(crate) fn prores_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: } /// The DNxHR profile named by [`ExportSettings::dnx_profile`]. -pub fn dnx_profile(name: &str) -> filmcraft_dnx::Profile { - use filmcraft_dnx::Profile; +pub fn dnx_profile(name: &str) -> craft_video::dnx::Profile { + use craft_video::dnx::Profile; match name.to_ascii_lowercase().as_str() { "lb" => Profile::Lb, "sq" => Profile::Sq, @@ -212,7 +212,7 @@ pub fn dnx_profile(name: &str) -> filmcraft_dnx::Profile { /// DNxHR encoder (RGBA8 or HDR floats → BT.709 / BT.2020 limited-range 4:2:2; 8-bit for /// LB/SQ/HQ, 10-bit for HQX). struct DnxEncoder { - enc: filmcraft_dnx::Encoder, + enc: craft_video::dnx::Encoder, w: u32, h: u32, rate: FrameRate, @@ -229,7 +229,7 @@ impl VideoEncoder for DnxEncoder { self.rate.num as u32 } fn encode(&mut self, f: &EncoderFrame) -> Result> { - let mut fr = filmcraft_dnx::Frame::new(f.width, f.height, filmcraft_dnx::ChromaFormat::Yuv422, 10, false); + let mut fr = craft_video::dnx::Frame::new(f.width, f.height, craft_video::dnx::ChromaFormat::Yuv422, 10, false); match f.hdr { Some(rgb) => { let (kr, kb) = self.signal.kr_kb(); @@ -248,11 +248,11 @@ impl VideoEncoder for DnxEncoder { pub(crate) fn dnx_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSettings) -> Option>> { (format == Format::DnxHr).then(|| { - let mut cfg = filmcraft_dnx::EncoderConfig::new(dnx_profile(&s.dnx_profile), w, h); + let mut cfg = craft_video::dnx::EncoderConfig::new(dnx_profile(&s.dnx_profile), w, h); if s.signal.primaries == 9 { - cfg.color_volume = filmcraft_dnx::ColorVolume::Bt2020Ncl; + cfg.color_volume = craft_video::dnx::ColorVolume::Bt2020Ncl; } - let enc = filmcraft_dnx::Encoder::with_config(cfg).map_err(|e| ExportError::Encode(e.to_string()))?; + let enc = craft_video::dnx::Encoder::with_config(cfg).map_err(|e| ExportError::Encode(e.to_string()))?; Ok(Box::new(DnxEncoder { enc, w, h, rate, signal: s.signal }) as Box) }) } diff --git a/docs/architecture.md b/docs/architecture.md index 07f461278..308d71c1f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -675,7 +675,7 @@ file.exportMedia {path, preset?, settings?, format?, range?, …} export.qui |---|---|---| | `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` | `craft_video::prores` (Proxy / LT / 422 / HQ) | MOV | -| `dnxhr` | `filmcraft-dnx` (LB / SQ / HQ / HQX) | MOV (`AVdh`) | +| `dnxhr` | `craft-video` `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` | | `mp3` | `craft_audio::mp3` (Layer III CBR, the audio settings' bitrate snapped to a Layer III one; 16-48 kHz, mono or stereo) | audio only: a plain `.mp3` stream of frames (no ID3 or gapless tag) | From 00de168f91444259b028fda5ef91d5040bbc055d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:09:50 +0000 Subject: [PATCH 02/24] export: use shared OpenEXR ZIP compression --- Cargo.lock | 10 + Cargo.toml | 2 + crates/export/Cargo.toml | 2 + crates/export/src/deflate.rs | 795 ----------------------------------- crates/export/src/lib.rs | 1 - crates/export/src/raster.rs | 11 +- 6 files changed, 22 insertions(+), 799 deletions(-) delete mode 100644 crates/export/src/deflate.rs diff --git a/Cargo.lock b/Cargo.lock index f8036afe2..eb8d21a9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1090,6 +1090,14 @@ name = "craft-docpath" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +[[package]] +name = "craft-exr" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f31e878c6e923748aebae75869e9dd88874d05d3#f31e878c6e923748aebae75869e9dd88874d05d3" +dependencies = [ + "serde_json", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -2164,6 +2172,7 @@ name = "filmcraft-export" version = "0.20.1" dependencies = [ "craft-audio", + "craft-exr", "craft-media", "craft-video", "filmcraft-audio-dsp", @@ -2179,6 +2188,7 @@ dependencies = [ "filmcraft-testkit", "filmcraft-text", "filmcraft-time", + "flate2", "image", "log", "rayon", diff --git a/Cargo.toml b/Cargo.toml index 5f2730c46..4839a91e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,8 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] } rayon = "1" proptest = "1" roxmltree = "0.21" +# Shared OpenEXR ZIP chunk compressor. +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f31e878c6e923748aebae75869e9dd88874d05d3" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index fa39b0b4e..f05cf310c 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] craft-audio = { workspace = true } +craft-exr = { workspace = true } craft-video = { workspace = true } filmcraft-time = { workspace = true } filmcraft-frame = { workspace = true } @@ -34,4 +35,5 @@ web-time = "1" image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "tiff", "bmp"] } [dev-dependencies] +flate2 = { version = "1", default-features = false, features = ["rust_backend"] } filmcraft-testkit = { workspace = true } diff --git a/crates/export/src/deflate.rs b/crates/export/src/deflate.rs deleted file mode 100644 index 4381c4ebe..000000000 --- a/crates/export/src/deflate.rs +++ /dev/null @@ -1,795 +0,0 @@ -//! A small DEFLATE encoder (RFC 1951) in a zlib wrapper (RFC 1950), written from the RFCs for the -//! ZIP-compressed OpenEXR chunks. -//! -//! Greedy LZ77 over a 32 KB window (hash chains of bounded length), then each block of tokens is -//! written as whichever of stored, fixed-Huffman or dynamic-Huffman is smallest. Huffman code -//! lengths are limited (15 bits, 7 for the code-length code) by halving the symbol frequencies -//! until the tree fits. Every code has at least two symbols, so all codes are complete. The -//! output favours simplicity and correctness over ratio; worst-case expansion is the stored-block -//! overhead (5 bytes per 64 KB). - -use std::cmp::Reverse; -use std::collections::BinaryHeap; - -const WINDOW: usize = 32 * 1024; -const MIN_MATCH: usize = 3; -const MAX_MATCH: usize = 258; -/// Hash chain candidates tried per position. -const MAX_CHAIN: usize = 48; -/// Tokens per block (a new set of Huffman codes after this many). -const BLOCK_TOKENS: usize = 1 << 15; -const MAX_STORED: usize = 65_535; - -const LEN_BASE: [u16; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]; -const LEN_EXTRA: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]; -const DIST_BASE: [u16; 30] = - [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]; -const DIST_EXTRA: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]; -/// Order in which the code-length code's lengths are sent. -const CL_ORDER: [usize; 19] = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; - -/// `data` compressed as a zlib stream (CMF 0x78: deflate, 32 KB window). `tables` are the match -/// finder's hash tables, kept by the caller across the chunks of a picture; the output does not -/// depend on what they held before. -pub(crate) fn zlib(data: &[u8], tables: &mut Tables) -> Vec { - let mut w = Bits { out: Vec::with_capacity(data.len() / 2 + 64), acc: 0, n: 0 }; - w.out.extend_from_slice(&[0x78, 0x9C]); // (0x78 << 8 | 0x9C) % 31 == 0 - deflate(data, &mut w, tables); - w.align(); - w.out.extend_from_slice(&adler32(data).to_be_bytes()); - w.out -} - -/// Adler-32 (RFC 1950 §9); 5552 bytes is the longest run that cannot overflow the sums. -fn adler32(data: &[u8]) -> u32 { - let (mut a, mut b) = (1_u32, 0_u32); - for c in data.chunks(5552) { - for &x in c { - a += u32::from(x); - b += a; - } - a %= 65_521; - b %= 65_521; - } - (b << 16) | a -} - -/// LSB-first bit writer. -struct Bits { - out: Vec, - acc: u64, - n: u32, -} - -impl Bits { - /// The low `n` bits of `v` (n ≤ 32). - fn put(&mut self, v: u32, n: u32) { - let mask = (1_u64 << n) - 1; - self.acc |= (u64::from(v) & mask) << self.n; - self.n += n; - while self.n >= 8 { - self.out.push(self.acc as u8); - self.acc >>= 8; - self.n -= 8; - } - } - - fn align(&mut self) { - if self.n > 0 { - self.out.push(self.acc as u8); - } - self.acc = 0; - self.n = 0; - } -} - -#[derive(Clone, Copy)] -enum Tok { - Lit(u8), - Match { len: u16, dist: u16 }, -} - -/// Index into [`LEN_BASE`] of a match length (3..=258). -fn len_code(len: u16) -> usize { - LEN_BASE.partition_point(|&b| b <= len).saturating_sub(1) -} - -/// Index into [`DIST_BASE`] of a distance (1..=32768). -fn dist_code(dist: u16) -> usize { - DIST_BASE.partition_point(|&b| b <= dist).saturating_sub(1) -} - -/// The match finder's hash heads and chains, reused from one input to the next. -/// -/// A slot holds `base + p + 1` for position `p` of the input that wrote it; anything at or below -/// the current input's `base` was left by an earlier input and reads as empty. So the tables are -/// never cleared between inputs: an OpenEXR ZIPS frame is one input per scan line, and zeroing -/// 256 KiB of tables for each 23 KB UHD line cost more memory traffic than the data itself. -#[derive(Default)] -pub(crate) struct Tables { - head: Vec, - prev: Vec, - /// Bytes compressed through these tables since they were last cleared. - base: u32, -} - -/// LZ77 match finder: hash heads and chains over the window. -struct Matcher<'a> { - data: &'a [u8], - head: &'a mut [u32], - prev: &'a mut [u32], - bits: u32, - /// `prev` slots used by this input, minus one (a power of two minus one). - mask: usize, - base: u32, -} - -impl<'a> Matcher<'a> { - /// A matcher over `data` (shorter than `u32::MAX` bytes), or None when it is not. - fn new(data: &'a [u8], t: &'a mut Tables) -> Option { - // hash width and chain span follow the input's size, as they did when every input had - // tables of its own (the output depends on them), so short chunks stay cheap to hash - let span = data.len().clamp(256, WINDOW).next_power_of_two(); - let bits = span.trailing_zeros().clamp(8, 15); - let n = u32::try_from(data.len()).ok().filter(|&n| n < u32::MAX)?; - if t.base.checked_add(n).and_then(|b| b.checked_add(1)).is_none() { - // positions would no longer fit after `base`: start over from empty tables - t.head.fill(0); - t.prev.fill(0); - t.base = 0; - } - // grown slots are 0, which is never above `base`, so they read as empty - if t.head.len() < 1 << bits { - t.head.resize(1 << bits, 0); - } - if t.prev.len() < span { - t.prev.resize(span, 0); - } - let base = t.base; - t.base += n; - let (head, prev) = (t.head.get_mut(..1 << bits)?, t.prev.get_mut(..span)?); - Some(Matcher { data, head, prev, bits, mask: span - 1, base }) - } - - /// Position `p` of this input from a stored slot, or None for an empty or earlier input's slot. - fn pos(&self, slot: u32) -> Option { - slot.checked_sub(self.base)?.checked_sub(1).map(|p| p as usize) - } - - fn hash(&self, p: usize) -> Option { - let b = self.data.get(p..p + MIN_MATCH)?; - let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); - Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize) - } - - /// Record position `p` (stored as `p + 1`; 0 = empty). - fn insert(&mut self, p: usize) { - let Some(h) = self.hash(p) else { return }; - let (Some(head), Ok(p32)) = (self.head.get(h).copied(), u32::try_from(p)) else { return }; - if let Some(slot) = self.prev.get_mut(p & self.mask) { - *slot = head; - } - if let Some(slot) = self.head.get_mut(h) { - // `new` reserved `base + len + 1` within u32 - *slot = self.base + p32 + 1; - } - } - - /// The longest earlier match at `p`: (length, distance), length 0 if none. - fn longest(&self, p: usize) -> (usize, usize) { - let Some(h) = self.hash(p) else { return (0, 0) }; - let max = self.data.len().saturating_sub(p).min(MAX_MATCH); - let Some(here) = self.data.get(p..p + max) else { return (0, 0) }; - let (mut best, mut best_dist) = (0, 0); - let mut cand = self.head.get(h).and_then(|&s| self.pos(s)); - for _ in 0..MAX_CHAIN { - let Some(c) = cand else { break }; - if c >= p || p - c > WINDOW { - break; - } - // a candidate that differs at `best` cannot be longer than `best`: skip comparing it - if best == 0 || self.data.get(c + best) == here.get(best) { - let l = self.data.get(c..).unwrap_or_default().iter().zip(here).take_while(|(a, b)| a == b).count(); - if l > best { - (best, best_dist) = (l, p - c); - if l == max { - break; - } - } - } - let Some(next) = self.prev.get(c & self.mask).and_then(|&s| self.pos(s)) else { break }; - // chains only go back; anything else is a stale slot - if next >= c { - break; - } - cand = Some(next); - } - (best, best_dist) - } -} - -/// The deflate blocks of `data`, the last one final. -fn deflate(data: &[u8], w: &mut Bits, tables: &mut Tables) { - if data.is_empty() { - // a final fixed-Huffman block holding only end-of-block (code 0000000) - w.put(1, 1); - w.put(1, 2); - w.put(0, 7); - return; - } - // positions are kept in u32; longer inputs (never an OpenEXR chunk) go out stored - let mut m = Matcher::new(data, tables); - let mut toks = Vec::with_capacity(BLOCK_TOKENS.min(data.len()) + 1); - let (mut start, mut i) = (0, 0); - while i < data.len() { - let (len, dist) = m.as_ref().map_or((0, 0), |m| m.longest(i)); - let step = if len >= MIN_MATCH { - toks.push(Tok::Match { len: len as u16, dist: dist as u16 }); - len - } else { - toks.push(Tok::Lit(data[i])); - 1 - }; - if let Some(m) = m.as_mut() { - for p in i..i + step { - m.insert(p); - } - } - i += step; - if toks.len() >= BLOCK_TOKENS || i == data.len() { - block(w, &toks, &data[start..i], i == data.len()); - toks.clear(); - start = i; - } - } -} - -/// Bits the tokens take with the given code lengths (EOB included). -fn data_bits(lf: &[u32], df: &[u32], ll: &[u8], dl: &[u8]) -> u64 { - let mut n = 0_u64; - for (s, (&f, &l)) in lf.iter().zip(ll).enumerate() { - let extra = s.checked_sub(257).and_then(|k| LEN_EXTRA.get(k)).copied().unwrap_or(0); - n += u64::from(f) * u64::from(l + extra); - } - for (d, (&f, &l)) in df.iter().zip(dl).enumerate() { - n += u64::from(f) * u64::from(l + DIST_EXTRA.get(d).copied().unwrap_or(0)); - } - n -} - -/// One block (split into several stored blocks when stored is smallest and `raw` is long). -fn block(w: &mut Bits, toks: &[Tok], raw: &[u8], last: bool) { - let mut lf = [0_u32; 286]; - let mut df = [0_u32; 30]; - for t in toks { - match *t { - Tok::Lit(b) => lf[b as usize] += 1, - Tok::Match { len, dist } => { - lf[257 + len_code(len)] += 1; - df[dist_code(dist)] += 1; - } - } - } - lf[256] += 1; - - // dynamic codes and their header - let ll = lengths(&lf, 15); - let dl = lengths(&df, 15); - let hlit = ll.iter().rposition(|&l| l > 0).map_or(257, |p| (p + 1).max(257)); - let hdist = dl.iter().rposition(|&l| l > 0).map_or(1, |p| p + 1); - let seq = rle_lengths(&[&ll[..hlit], &dl[..hdist]].concat()); - let mut cf = [0_u32; 19]; - for &(s, _) in &seq { - cf[s as usize] += 1; - } - let cl = lengths(&cf, 7); - let hclen = CL_ORDER.iter().rposition(|&s| cl[s] > 0).map_or(4, |p| (p + 1).max(4)); - let seq_bits: u64 = seq.iter().map(|&(s, _)| u64::from(cl[s as usize]) + [2_u64, 3, 7].get((s as usize).wrapping_sub(16)).copied().unwrap_or(0)).sum(); - let dyn_bits = 3 + 14 + 3 * hclen as u64 + seq_bits + data_bits(&lf, &df, &ll, &dl); - - let (fll, fdl) = fixed_lengths(); - let fixed_bits = 3 + data_bits(&lf, &df, &fll, &fdl); - // per stored piece: header, at most 7 padding bits, LEN and NLEN - let pieces = raw.len().div_ceil(MAX_STORED).max(1) as u64; - let stored_bits = pieces * (3 + 7 + 32) + 8 * raw.len() as u64; - - if stored_bits <= fixed_bits.min(dyn_bits) { - let n = raw.chunks(MAX_STORED).count(); - for (k, piece) in raw.chunks(MAX_STORED).enumerate() { - w.put(u32::from(last && k + 1 == n), 1); - w.put(0, 2); - w.align(); - let len = piece.len() as u16; - w.out.extend_from_slice(&len.to_le_bytes()); - w.out.extend_from_slice(&(!len).to_le_bytes()); - w.out.extend_from_slice(piece); - } - return; - } - w.put(u32::from(last), 1); - if fixed_bits <= dyn_bits { - w.put(1, 2); - write_tokens(w, toks, &fll, &fdl); - return; - } - w.put(2, 2); - w.put((hlit - 257) as u32, 5); - w.put((hdist - 1) as u32, 5); - w.put((hclen - 4) as u32, 4); - for &s in &CL_ORDER[..hclen] { - w.put(u32::from(cl[s]), 3); - } - let cc = codes(&cl); - for &(s, x) in &seq { - w.put(u32::from(cc[s as usize]), u32::from(cl[s as usize])); - match s { - 16 => w.put(u32::from(x), 2), - 17 => w.put(u32::from(x), 3), - 18 => w.put(u32::from(x), 7), - _ => {} - } - } - write_tokens(w, toks, &ll, &dl); -} - -/// The fixed Huffman code lengths (RFC 1951 §3.2.6). -fn fixed_lengths() -> ([u8; 288], [u8; 30]) { - let mut ll = [8_u8; 288]; - ll[144..256].fill(9); - ll[256..280].fill(7); - (ll, [5; 30]) -} - -fn write_tokens(w: &mut Bits, toks: &[Tok], ll: &[u8], dl: &[u8]) { - let lc = codes(ll); - let dc = codes(dl); - let sym = |w: &mut Bits, s: usize| w.put(u32::from(lc[s]), u32::from(ll[s])); - for t in toks { - match *t { - Tok::Lit(b) => sym(w, b as usize), - Tok::Match { len, dist } => { - let k = len_code(len); - sym(w, 257 + k); - w.put(u32::from(len - LEN_BASE[k]), u32::from(LEN_EXTRA[k])); - let d = dist_code(dist); - w.put(u32::from(dc[d]), u32::from(dl[d])); - w.put(u32::from(dist - DIST_BASE[d]), u32::from(DIST_EXTRA[d])); - } - } - } - sym(w, 256); -} - -/// Canonical Huffman codes for `lens`, bit-reversed for the LSB-first writer. -fn codes(lens: &[u8]) -> Vec { - let mut count = [0_u32; 16]; - for &l in lens { - if (1..16).contains(&l) { - count[l as usize] += 1; - } - } - let mut next = [0_u32; 16]; - let mut code = 0; - for bits in 1..16 { - code = (code + count[bits - 1]) << 1; - next[bits] = code; - } - lens.iter() - .map(|&l| { - if !(1..16).contains(&l) { - return 0; - } - let c = next[l as usize]; - next[l as usize] += 1; - (c.reverse_bits() >> (32 - u32::from(l))) as u16 - }) - .collect() -} - -/// Code lengths for `freq`, none longer than `limit`; at least two symbols always get a code. -fn lengths(freq: &[u32], limit: u8) -> Vec { - let mut f: Vec = freq.iter().map(|&x| u64::from(x)).collect(); - let mut used = f.iter().filter(|&&x| x > 0).count(); - for x in f.iter_mut() { - if used >= 2 { - break; - } - if *x == 0 { - *x = 1; - used += 1; - } - } - // halving converges: once every used frequency is 1 the tree is balanced (depth ≤ 9 here) - for _ in 0..66 { - let l = huffman(&f); - if l.iter().all(|&d| d <= limit) { - return l; - } - for x in f.iter_mut().filter(|x| **x > 0) { - *x = (*x >> 1).max(1); - } - } - huffman(&f) -} - -/// Huffman tree depths of the symbols with non-zero frequency (0 for the rest). -fn huffman(f: &[u64]) -> Vec { - let n = f.len(); - let mut heap: BinaryHeap> = f.iter().enumerate().filter(|(_, w)| **w > 0).map(|(i, &w)| Reverse((w, i))).collect(); - let mut parent = vec![usize::MAX; 2 * n]; - let mut next = n; - while heap.len() > 1 { - let (Some(Reverse((wa, a))), Some(Reverse((wb, b)))) = (heap.pop(), heap.pop()) else { break }; - parent[a] = next; - parent[b] = next; - heap.push(Reverse((wa + wb, next))); - next += 1; - } - // parents always come after their children, so walk down from the root - let mut depth = vec![0_u32; next]; - for i in (0..next).rev() { - if let Some(&p) = parent.get(i).filter(|&&p| p < next) { - depth[i] = depth[p].saturating_add(1); - } - } - depth[..n].iter().map(|&d| d.min(u32::from(u8::MAX)) as u8).collect() -} - -/// Code lengths run-length coded with the code-length alphabet: (symbol, extra-bits value). -fn rle_lengths(lens: &[u8]) -> Vec<(u8, u8)> { - let mut out = Vec::new(); - let mut i = 0; - while i < lens.len() { - let l = lens[i]; - let run = lens[i..].iter().take_while(|&&x| x == l).count(); - let mut r = run; - if l == 0 { - while r >= 11 { - let k = r.min(138); - out.push((18, (k - 11) as u8)); - r -= k; - } - if r >= 3 { - out.push((17, (r - 3) as u8)); - r = 0; - } - } else { - out.push((l, 0)); - r -= 1; - while r >= 3 { - let k = r.min(6); - out.push((16, (k - 3) as u8)); - r -= k; - } - } - out.extend(std::iter::repeat_n((l, 0), r)); - i += run; - } - out -} - -#[cfg(test)] -pub(crate) mod tests { - use super::*; - - /// A minimal inflater (RFC 1951), only to check the encoder; returns None on any error. - pub(crate) fn inflate(z: &[u8]) -> Option> { - let (cmf, flg) = (*z.first()?, *z.get(1)?); - if cmf & 0x0F != 8 || (u16::from(cmf) << 8 | u16::from(flg)) % 31 != 0 || flg & 0x20 != 0 { - return None; - } - let mut pos = 16_usize; // bit position - let bit = |pos: &mut usize| -> Option { - let b = (*z.get(*pos / 8)? >> (*pos % 8)) & 1; - *pos += 1; - Some(u32::from(b)) - }; - let bits = |pos: &mut usize, n: u32| -> Option { - let mut v = 0; - for k in 0..n { - v |= bit(pos)? << k; - } - Some(v) - }; - // decode one symbol with canonical lengths (slow but simple) - let decode = |pos: &mut usize, lens: &[u8]| -> Option { - let (mut code, mut first, mut index) = (0_i32, 0_i32, 0_i32); - let mut count = [0_i32; 16]; - for &l in lens { - count[l as usize] += 1; - } - let mut sorted: Vec = (0..lens.len()).filter(|&s| lens[s] > 0).collect(); - sorted.sort_by_key(|&s| (lens[s], s)); - for len in 1..16 { - code |= bit(pos)? as i32; - let c = count[len]; - if code - c < first { - return sorted.get((index + code - first) as usize).copied(); - } - index += c; - first = (first + c) << 1; - code <<= 1; - } - None - }; - let mut out = Vec::new(); - loop { - let last = bit(&mut pos)?; - match bits(&mut pos, 2)? { - 0 => { - pos = pos.div_ceil(8) * 8; - let at = pos / 8; - let len = u16::from_le_bytes([*z.get(at)?, *z.get(at + 1)?]); - let nlen = u16::from_le_bytes([*z.get(at + 2)?, *z.get(at + 3)?]); - if len != !nlen { - return None; - } - out.extend_from_slice(z.get(at + 4..at + 4 + len as usize)?); - pos = (at + 4 + len as usize) * 8; - } - t @ (1 | 2) => { - let (ll, dl) = if t == 1 { - let (a, b) = fixed_lengths(); - (a.to_vec(), b.to_vec()) - } else { - let hlit = bits(&mut pos, 5)? as usize + 257; - let hdist = bits(&mut pos, 5)? as usize + 1; - let hclen = bits(&mut pos, 4)? as usize + 4; - let mut cl = [0_u8; 19]; - for &s in &CL_ORDER[..hclen] { - cl[s] = bits(&mut pos, 3)? as u8; - } - let mut lens = Vec::new(); - while lens.len() < hlit + hdist { - match decode(&mut pos, &cl)? { - s @ 0..=15 => lens.push(s as u8), - 16 => { - let p = *lens.last()?; - lens.extend(std::iter::repeat_n(p, 3 + bits(&mut pos, 2)? as usize)); - } - 17 => lens.extend(std::iter::repeat_n(0, 3 + bits(&mut pos, 3)? as usize)), - _ => lens.extend(std::iter::repeat_n(0, 11 + bits(&mut pos, 7)? as usize)), - } - } - if lens.len() != hlit + hdist { - return None; - } - (lens[..hlit].to_vec(), lens[hlit..].to_vec()) - }; - loop { - let s = decode(&mut pos, &ll)?; - if s < 256 { - out.push(s as u8); - } else if s == 256 { - break; - } else { - let k = s - 257; - let len = *LEN_BASE.get(k)? as usize + bits(&mut pos, u32::from(LEN_EXTRA[k]))? as usize; - let d = decode(&mut pos, &dl)?; - let dist = *DIST_BASE.get(d)? as usize + bits(&mut pos, u32::from(DIST_EXTRA[d]))? as usize; - let from = out.len().checked_sub(dist)?; - for j in 0..len { - out.push(out[from + j]); - } - } - } - } - _ => return None, - } - if last == 1 { - break; - } - } - let at = pos.div_ceil(8); - let sum = u32::from_be_bytes(z.get(at..at + 4)?.try_into().ok()?); - (sum == adler32(&out) && at + 4 == z.len()).then_some(out) - } - - /// A deterministic pseudo-random byte stream. - fn noise(n: usize, seed: u32) -> Vec { - let mut s = seed | 1; - (0..n) - .map(|_| { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - s as u8 - }) - .collect() - } - - #[test] - fn round_trips() { - let mut cases: Vec> = vec![vec![], vec![7], vec![1, 2], vec![0; 1], vec![0; 300_000], b"abcabcabcabcabcabd".to_vec()]; - cases.push(noise(100_000, 9)); // incompressible: stored blocks - cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); // few symbols: dynamic - cases.push((0..200_000_u32).map(|i| ((i / 7) % 251) as u8).collect()); // long repeats - // text-like: a small vocabulary repeated at varying distances - let words: [&[u8]; 6] = [b"red ", b"green ", b"blue ", b"alpha ", b"half ", b"linear "]; - cases.push(noise(40_000, 5).iter().flat_map(|b| words[*b as usize % 6].iter().copied()).collect()); - // skewed frequencies: Huffman lengths beyond 15 before limiting - let mut fib = Vec::new(); - let (mut a, mut b) = (1_usize, 1_usize); - for s in 0..25_u8 { - fib.extend(std::iter::repeat_n(s.wrapping_mul(37), a.min(200_000))); - (a, b) = (b, a + b); - } - let r = noise(fib.len() * 4, 11); - for i in (1..fib.len()).rev() { - let j = u32::from_le_bytes(r[i * 4..i * 4 + 4].try_into().unwrap()) as usize % (i + 1); - fib.swap(i, j); - } - cases.push(fib); - for (k, data) in cases.iter().enumerate() { - let z = zlib(data, &mut Tables::default()); - assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "case {k}"); - // worst case: stored overhead only - assert!(z.len() <= data.len() + (data.len().div_ceil(BLOCK_TOKENS) + 1) * 5 + 6, "case {k}: {} > {}", z.len(), data.len()); - } - // repetitive data actually compresses - assert!(zlib(&cases[4], &mut Tables::default()).len() < 2_000); - assert!(zlib(&cases[8], &mut Tables::default()).len() < 20_000); - } - - #[test] - fn length_limits_hold() { - // Fibonacci frequencies give the deepest possible Huffman tree - let mut f = vec![0_u32; 286]; - let (mut a, mut b) = (1_u32, 1_u32); - for x in f.iter_mut().take(30) { - *x = a; - (a, b) = (b, a.saturating_add(b)); - } - let l = lengths(&f, 15); - assert!(l.iter().all(|&d| d <= 15)); - // a complete code: Kraft sum exactly 1 - let kraft: f64 = l.iter().filter(|&&d| d > 0).map(|&d| 0.5_f64.powi(i32::from(d))).sum(); - assert!((kraft - 1.0).abs() < 1e-12); - // one used symbol still gets a two-symbol code - let l = lengths(&[0, 5, 0], 7); - assert_eq!(l.iter().filter(|&&d| d == 1).count(), 2); - } - - #[test] - fn adler_matches_rfc_example() { - // the classic test vector for "Wikipedia" - assert_eq!(adler32(b"Wikipedia"), 0x11E6_0398); - assert_eq!(adler32(&[]), 1); - } - - /// The match finder before #4659: tables of its own per input, every chain candidate compared - /// from its first byte. - struct OldMatcher<'a> { - data: &'a [u8], - head: Vec, - prev: Vec, - bits: u32, - } - - impl<'a> OldMatcher<'a> { - /// Tables of its own, sized to `data`. - fn new(data: &'a [u8]) -> Self { - let span = data.len().clamp(256, WINDOW).next_power_of_two(); - let bits = span.trailing_zeros().clamp(8, 15); - OldMatcher { data, head: vec![0; 1 << bits], prev: vec![0; span], bits } - } - - /// The hash of the three bytes at `p`, None near the end. - fn hash(&self, p: usize) -> Option { - let b = self.data.get(p..p + MIN_MATCH)?; - let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); - Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize) - } - - /// Record position `p` (stored as `p + 1`; 0 = empty). - fn insert(&mut self, p: usize) { - let Some(h) = self.hash(p) else { return }; - let mask = self.prev.len() - 1; - self.prev[p & mask] = self.head[h]; - self.head[h] = p as u32 + 1; - } - - /// The longest earlier match at `p`: (length, distance), length 0 if none. - fn longest(&self, p: usize) -> (usize, usize) { - let Some(h) = self.hash(p) else { return (0, 0) }; - let max = (self.data.len() - p).min(MAX_MATCH); - let here = &self.data[p..p + max]; - let mask = self.prev.len() - 1; - let (mut best, mut best_dist) = (0, 0); - let mut cand = self.head[h] as usize; - for _ in 0..MAX_CHAIN { - let Some(c) = cand.checked_sub(1) else { break }; - if c >= p || p - c > WINDOW { - break; - } - let l = self.data[c..].iter().zip(here).take_while(|(a, b)| a == b).count(); - if l > best { - (best, best_dist) = (l, p - c); - if l == max { - break; - } - } - let next = self.prev[c & mask] as usize; - if next == 0 || next > c { - break; - } - cand = next; - } - (best, best_dist) - } - } - - /// Inputs shaped like OpenEXR chunks (predicted half-float scan lines, single and 16-line) - /// and the awkward cases: empty, tiny, incompressible, long runs, few symbols. - fn chunk_cases() -> Vec> { - let line: Vec = (0..23_040_u32).map(|i| (128 + ((i / 3) % 7) as i32 - 3 + (i % 5 == 0) as i32) as u8).collect(); - let mut cases = vec![line.clone(), line.iter().rev().copied().collect(), vec![], vec![9], b"abcabcabcabd".to_vec()]; - cases.push(line.iter().cycle().take(16 * 23_040).copied().collect()); // ZIP: 16 lines, longer than the window - cases.push(noise(23_040, 4)); - cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); - cases.push(vec![0; 300_000]); - cases.push(line.clone()); - cases.push(noise(300, 7)); - cases - } - - /// The matcher's greedy parse of `data`: what `deflate` codes. - fn parse (usize, usize), G: FnMut(usize)>(data: &[u8], mut longest: F, mut insert: G) -> Vec<(usize, usize)> { - let (mut out, mut i) = (Vec::new(), 0); - while i < data.len() { - let (len, dist) = longest(i); - let step = if len >= MIN_MATCH { len } else { 1 }; - out.push(if len >= MIN_MATCH { (len, dist) } else { (0, 0) }); - for p in i..i + step { - insert(p); - } - i += step; - } - out - } - - /// Shared tables and the candidate skip find exactly the matches the previous matcher found. - #[test] - fn shared_tables_find_the_matches_the_old_per_chunk_tables_found() { - let mut tables = Tables::default(); - for (k, data) in chunk_cases().iter().enumerate() { - let old = std::cell::RefCell::new(OldMatcher::new(data)); - let want = parse(data, |p| old.borrow().longest(p), |p| old.borrow_mut().insert(p)); - let new = std::cell::RefCell::new(Matcher::new(data, &mut tables).expect("fits")); - let got = parse(data, |p| new.borrow().longest(p), |p| new.borrow_mut().insert(p)); - assert!(got == want, "chunk {k}: the parse differs"); - } - } - - /// Shared tables compress to the bytes fresh ones give, also when positions run out and they start over. - #[test] - fn shared_tables_give_the_bytes_of_fresh_ones() { - let mut shared = Tables::default(); - for (k, data) in chunk_cases().iter().enumerate() { - let z = zlib(data, &mut shared); - assert!(z == zlib(data, &mut Tables::default()), "chunk {k}"); - assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "chunk {k}"); - } - // positions about to run past u32: the tables start over and the bytes stay the same - for gap in [0_u32, 1, 2, 23_040, 23_041, 1 << 20] { - shared.base = u32::MAX - gap; - for (k, data) in chunk_cases().iter().enumerate() { - assert!(zlib(data, &mut shared) == zlib(data, &mut Tables::default()), "gap {gap}, chunk {k}"); - } - } - } - - /// Later chunks use the same tables instead of allocating new ones. - #[test] - fn the_tables_are_reused_across_chunks() { - let mut tables = Tables::default(); - let line = &chunk_cases()[0]; - zlib(line, &mut tables); - let (head, prev) = (tables.head.as_ptr(), tables.prev.as_ptr()); - for _ in 0..4 { - zlib(line, &mut tables); - assert!(tables.head.as_ptr() == head && tables.prev.as_ptr() == prev, "a chunk allocated new tables"); - } - assert_eq!(tables.base as usize, 5 * line.len()); - } -} diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index d18e4ddac..413a46071 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -17,7 +17,6 @@ use std::io::Write; mod aac_out; mod audio_out; -mod deflate; mod job; mod mp3_out; mod mxf_out; diff --git a/crates/export/src/raster.rs b/crates/export/src/raster.rs index 34254da53..ed7d244ba 100644 --- a/crates/export/src/raster.rs +++ b/crates/export/src/raster.rs @@ -231,7 +231,7 @@ pub(crate) fn exr(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: ExrComp let mut px = vec![0_u16; wn * names.len()]; let mut raw = Vec::with_capacity(line * lines); // the deflate hash tables, shared by the frame's chunks instead of zeroed for each one - let mut tables = crate::deflate::Tables::default(); + let mut tables = craft_exr::zlib::Tables::default(); for (k, block) in rgba[..n].chunks(row * lines).enumerate() { raw.clear(); for src in block.chunks_exact(row) { @@ -255,7 +255,7 @@ pub(crate) fn exr(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: ExrComp let packed = match compression { ExrCompression::None => None, ExrCompression::Rle => Some(rle(&predict(&raw))), - ExrCompression::Zips | ExrCompression::Zip => Some(crate::deflate::zlib(&predict(&raw), &mut tables)), + ExrCompression::Zips | ExrCompression::Zip => Some(craft_exr::zlib::zlib(&predict(&raw), &mut tables)), }; // a chunk that does not shrink is stored as it is (readers compare the sizes) let data = packed.as_deref().filter(|p| p.len() < raw.len()).unwrap_or(&raw); @@ -508,7 +508,12 @@ mod tests { } else if c == ExrCompression::Rle { unpredict(&unrle(data)) } else { - unpredict(&crate::deflate::tests::inflate(data).unwrap()) + { + use std::io::Read as _; + let mut decoded = Vec::new(); + flate2::read::ZlibDecoder::new(data).read_to_end(&mut decoded).unwrap(); + unpredict(&decoded) + } }; assert_eq!(got, want, "{c:?} chunk {k}"); } From d0826946e386c68a1ac4c088da88fc6c6740a364 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:12:26 +0000 Subject: [PATCH 03/24] export: use shared OpenEXR scanline writer --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/export/src/raster.rs | 185 +++------------------------------- crates/export/src/settings.rs | 2 + 4 files changed, 16 insertions(+), 175 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb8d21a9e..043e3ea16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=f31e878c6e923748aebae75869e9dd88874d05d3#f31e878c6e923748aebae75869e9dd88874d05d3" +source = "git+https://github.com/storytold/craft-libs?rev=d55335423a9719102b2c2f3dfa2f6cbf05e915d3#d55335423a9719102b2c2f3dfa2f6cbf05e915d3" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 4839a91e5..9814ad634 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f31e878c6e923748aebae75869e9dd88874d05d3" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d55335423a9719102b2c2f3dfa2f6cbf05e915d3" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both diff --git a/crates/export/src/raster.rs b/crates/export/src/raster.rs index ed7d244ba..969115226 100644 --- a/crates/export/src/raster.rs +++ b/crates/export/src/raster.rs @@ -134,182 +134,21 @@ pub(crate) fn dpx(rgba: &[u8], w: u32, h: u32) -> Result> { Ok(out) } -/// IEEE 754 binary16 bits of `v`, rounded to nearest even (overflow → infinity, NaN → quiet NaN). -fn half(v: f32) -> u16 { - let b = v.to_bits(); - let sign = ((b >> 16) & 0x8000) as u16; - let exp = ((b >> 23) & 0xFF) as i32; - let man = b & 0x7F_FFFF; - if exp == 0xFF { - return sign | 0x7C00 | if man != 0 { 0x200 } else { 0 }; - } - // rounds `m >> shift` to nearest, ties to even (`shift` is 1..=24) - let round = |m: u32, shift: u32| { - let (q, rem, mid) = (m >> shift, m & ((1 << shift) - 1), 1 << (shift - 1)); - if rem > mid || (rem == mid && q & 1 == 1) { q + 1 } else { q } - }; - let e = exp - 127 + 15; - if e >= 0x1F { - sign | 0x7C00 - } else if e <= 0 { - if e < -10 { - return sign; - } - // subnormal half: the 24-bit significand scaled by 2^(e - 14); rounding up may carry - // into the smallest normal, which is the right encoding - sign | round(man | 0x80_0000, (14 - e) as u32) as u16 - } else { - // a carry out of the mantissa bumps the exponent (up to infinity), as it should - sign | round(((e as u32) << 23) | man, 13) as u16 - } -} +#[cfg(test)] +use craft_exr::rgba8::{half, rle}; -/// An OpenEXR file: half-float scene-linear Rec.709 RGB, or RGBA with `alpha` (premultiplied, as -/// EXR defines alpha; the frame's straight alpha is multiplied in after linearisation). +/// Write a scanline OpenEXR using FilmCraft's color conversion table. pub(crate) fn exr(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: ExrCompression) -> Result> { - let n = checked_len(rgba, w, h)?; - let too_large = || ExportError::Unsupported(format!("an OpenEXR frame of {w}×{h} is too large")); - let (Ok(x_max), Ok(y_max)) = (i32::try_from(w - 1), i32::try_from(h - 1)) else { - return Err(too_large()); + let compression = match compression { + ExrCompression::None => craft_exr::rgba8::Compression::None, + ExrCompression::Rle => craft_exr::rgba8::Compression::Rle, + ExrCompression::Zips => craft_exr::rgba8::Compression::Zips, + ExrCompression::Zip => craft_exr::rgba8::Compression::Zip, }; - // channels in the alphabetical order the file layout requires - let names: &[u8] = if alpha { b"ABGR" } else { b"BGR" }; - let line = (w as usize).checked_mul(names.len() * 2).ok_or_else(|| ExportError::Encode("frame size".into()))?; - // a chunk's byte count is a signed 32-bit field - let lines = compression.lines(); - if line.checked_mul(lines).and_then(|c| i32::try_from(c).ok()).is_none() { - return Err(too_large()); - } - - let mut hd = Vec::with_capacity(512); - hd.extend_from_slice(&[0x76, 0x2F, 0x31, 0x01]); // magic number 20000630 - hd.extend_from_slice(&[2, 0, 0, 0]); // version 2, single-part scan-line file, short names - let attr = |hd: &mut Vec, name: &str, ty: &str, value: &[u8]| { - for s in [name, ty] { - hd.extend_from_slice(s.as_bytes()); - hd.push(0); - } - hd.extend_from_slice(&(value.len() as i32).to_le_bytes()); - hd.extend_from_slice(value); - }; - let le = |v: &[f32]| v.iter().flat_map(|f| f.to_le_bytes()).collect::>(); - let mut channels = Vec::new(); - for &c in names { - channels.extend_from_slice(&[c, 0]); - channels.extend_from_slice(&1_i32.to_le_bytes()); // pixel type HALF - channels.extend_from_slice(&[0, 0, 0, 0]); // pLinear 0, reserved - channels.extend_from_slice(&1_i32.to_le_bytes()); // x sampling - channels.extend_from_slice(&1_i32.to_le_bytes()); // y sampling - } - channels.push(0); - let window: Vec = [0, 0, x_max, y_max].iter().flat_map(|v| v.to_le_bytes()).collect(); - attr(&mut hd, "channels", "chlist", &channels); - attr(&mut hd, "chromaticities", "chromaticities", &le(&[0.64, 0.33, 0.30, 0.60, 0.15, 0.06, 0.3127, 0.3290])); - attr(&mut hd, "compression", "compression", &[compression.code()]); - attr(&mut hd, "dataWindow", "box2i", &window); - attr(&mut hd, "displayWindow", "box2i", &window); - attr(&mut hd, "lineOrder", "lineOrder", &[0]); // INCREASING_Y - attr(&mut hd, "pixelAspectRatio", "float", &le(&[1.0])); - attr(&mut hd, "screenWindowCenter", "v2f", &le(&[0.0, 0.0])); - attr(&mut hd, "screenWindowWidth", "float", &le(&[1.0])); - hd.push(0); // end of header - - // offset table (one entry per chunk), then the chunks: first y, data size, data - let chunks = (h as usize).div_ceil(lines); - let table = chunks.checked_mul(8); - let body = (h as usize).checked_mul(line + 8); - let size = table.zip(body).and_then(|(t, b)| hd.len().checked_add(t)?.checked_add(b)).ok_or_else(|| ExportError::Encode("frame size".into()))?; - // the uncompressed size: compressed chunks only shrink it - let mut out = Vec::with_capacity(size); - out.extend_from_slice(&hd); - let table_at = out.len(); - out.resize(table_at + chunks * 8, 0); - let lin = filmcraft_color::srgb_u8_to_linear_table(); - let opaque: [u16; 256] = std::array::from_fn(|i| half(lin[i])); - let row = w as usize * 4; - let wn = w as usize; - let mut px = vec![0_u16; wn * names.len()]; - let mut raw = Vec::with_capacity(line * lines); - // the deflate hash tables, shared by the frame's chunks instead of zeroed for each one - let mut tables = craft_exr::zlib::Tables::default(); - for (k, block) in rgba[..n].chunks(row * lines).enumerate() { - raw.clear(); - for src in block.chunks_exact(row) { - // planar per line: every pixel of the first channel, then the next channel … - for (x, &[r, g, b, a]) in src.as_chunks::<4>().0.iter().enumerate() { - let rgb = [b, g, r]; - if alpha { - let k = f32::from(a) / 255.0; - px[x] = half(k); - for (c, &v) in rgb.iter().enumerate() { - px[(c + 1) * wn + x] = if a == 255 { opaque[v as usize] } else { half(lin[v as usize] * k) }; - } - } else { - for (c, &v) in rgb.iter().enumerate() { - px[c * wn + x] = opaque[v as usize]; - } - } - } - raw.extend(px.iter().flat_map(|v| v.to_le_bytes())); - } - let packed = match compression { - ExrCompression::None => None, - ExrCompression::Rle => Some(rle(&predict(&raw))), - ExrCompression::Zips | ExrCompression::Zip => Some(craft_exr::zlib::zlib(&predict(&raw), &mut tables)), - }; - // a chunk that does not shrink is stored as it is (readers compare the sizes) - let data = packed.as_deref().filter(|p| p.len() < raw.len()).unwrap_or(&raw); - let offset = (out.len() as u64).to_le_bytes(); - out[table_at + k * 8..table_at + k * 8 + 8].copy_from_slice(&offset); - out.extend_from_slice(&((k * lines) as i32).to_le_bytes()); - out.extend_from_slice(&(data.len() as i32).to_le_bytes()); - out.extend_from_slice(data); - } - Ok(out) -} - -/// The OpenEXR RLE / ZIP predictor: bytes split into even and odd halves, then each byte replaced -/// by its difference from the previous one, plus 128. -fn predict(raw: &[u8]) -> Vec { - let half = raw.len().div_ceil(2); - let mut t = vec![0_u8; raw.len()]; - for (i, &b) in raw.iter().enumerate() { - t[if i % 2 == 0 { i / 2 } else { half + i / 2 }] = b; - } - let mut p = t.first().copied().unwrap_or(0); - for v in t.iter_mut().skip(1) { - let d = v.wrapping_sub(p).wrapping_add(128); - p = *v; - *v = d; - } - t -} - -/// OpenEXR run-length coding: a signed count byte, then either `count + 1` copies of one byte -/// (count ≥ 0, runs of 3 to 128) or `-count` literal bytes (1 to 127). -fn rle(src: &[u8]) -> Vec { - let mut out = Vec::with_capacity(src.len() + src.len() / 127 + 1); - let mut i = 0; - while i < src.len() { - let b = src[i]; - let run = src[i..].iter().take(128).take_while(|&&x| x == b).count(); - if run >= 3 { - out.extend_from_slice(&[(run - 1) as u8, b]); - i += run; - continue; - } - // literals up to the next run of three (or 127 bytes) - let s = i; - while i < src.len() && i - s < 127 { - if src.get(i..i + 3).is_some_and(|t| t[0] == t[1] && t[1] == t[2]) { - break; - } - i += 1; - } - out.push(((i - s) as u8).wrapping_neg()); - out.extend_from_slice(&src[s..i]); - } - out + craft_exr::rgba8::write_rgba8(rgba, w, h, alpha, compression, filmcraft_color::srgb_u8_to_linear_table()).map_err(|e| match e { + craft_exr::rgba8::Error::FrameSize => ExportError::Encode("frame size".into()), + craft_exr::rgba8::Error::TooLarge => ExportError::Unsupported(format!("an OpenEXR frame of {w}×{h} is too large")), + }) } #[cfg(test)] diff --git a/crates/export/src/settings.rs b/crates/export/src/settings.rs index 34f72746d..854a08a7c 100644 --- a/crates/export/src/settings.rs +++ b/crates/export/src/settings.rs @@ -198,6 +198,7 @@ impl ExrCompression { } } /// The value of the OpenEXR `compression` attribute. + #[cfg(test)] pub(crate) fn code(self) -> u8 { match self { ExrCompression::None => 0, @@ -207,6 +208,7 @@ impl ExrCompression { } } /// Scan lines per chunk. + #[cfg(test)] pub(crate) fn lines(self) -> usize { if self == ExrCompression::Zip { 16 } else { 1 } } From 0c684a6c7640603c277439c222eb8947839064e8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:17:06 +0000 Subject: [PATCH 04/24] export: update shared EXR compressor lint fix --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb8d21a9e..73344da80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=f31e878c6e923748aebae75869e9dd88874d05d3#f31e878c6e923748aebae75869e9dd88874d05d3" +source = "git+https://github.com/storytold/craft-libs?rev=ab37b756cc86dfc552eeb351b0b407e546ec4d9e#ab37b756cc86dfc552eeb351b0b407e546ec4d9e" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 4839a91e5..8f74709c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f31e878c6e923748aebae75869e9dd88874d05d3" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ab37b756cc86dfc552eeb351b0b407e546ec4d9e" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From d7d9106ac7fc97181978d40ffa195bd6e6829403 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:17:07 +0000 Subject: [PATCH 05/24] export: update shared EXR compressor lint fix --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 043e3ea16..82ac44bc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d55335423a9719102b2c2f3dfa2f6cbf05e915d3#d55335423a9719102b2c2f3dfa2f6cbf05e915d3" +source = "git+https://github.com/storytold/craft-libs?rev=b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30#b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 9814ad634..39df04d1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d55335423a9719102b2c2f3dfa2f6cbf05e915d3" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From dac71afbd4256955cde9cea3eaf598a631de126e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:19:33 +0000 Subject: [PATCH 06/24] codecs: use shared Y4M stream parser --- Cargo.lock | 9 ++ Cargo.toml | 2 + crates/codecs/Cargo.toml | 1 + crates/codecs/src/y4m.rs | 237 +++++++++++---------------------------- 4 files changed, 79 insertions(+), 170 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8036afe2..b4b4bb9c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1230,6 +1230,14 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-y4m" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=25c7a8959cf70cf8c3f076d05baadb0990710569#25c7a8959cf70cf8c3f076d05baadb0990710569" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "crc32fast" version = "1.5.2" @@ -2060,6 +2068,7 @@ dependencies = [ "craft-jpeg", "craft-media", "craft-video", + "craft-y4m", "filmcraft-bitstream", "filmcraft-color", "filmcraft-dnx", diff --git a/Cargo.toml b/Cargo.toml index 5f2730c46..d9421344d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,8 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] } rayon = "1" proptest = "1" roxmltree = "0.21" +# Shared YUV4MPEG2 parser and frame index. +craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml index cd13c8fbe..224633558 100644 --- a/crates/codecs/Cargo.toml +++ b/crates/codecs/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-y4m = { workspace = true } craft-audio = { workspace = true } craft-video = { workspace = true } filmcraft-time = { workspace = true } diff --git a/crates/codecs/src/y4m.rs b/crates/codecs/src/y4m.rs index 51df7ead6..7f6d0355b 100644 --- a/crates/codecs/src/y4m.rs +++ b/crates/codecs/src/y4m.rs @@ -20,167 +20,54 @@ use filmcraft_time::{FrameRate, Tick}; use crate::CodecError; -const SIGNATURE: &[u8] = b"YUV4MPEG2"; -/// Longest stream header or `FRAME` line read. -const MAX_LINE: usize = 64 * 1024; -/// Largest picture (luma samples) accepted. -const MAX_PIXELS: u64 = 1 << 28; +pub use craft_y4m::sniff; -pub fn sniff(head: &[u8]) -> bool { - head.starts_with(SIGNATURE) && matches!(head.get(SIGNATURE.len()), Some(b' ' | b'\n')) -} - -/// The sample layout a `C` parameter names. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct Layout { - /// `None` for luma only (`mono`). - chroma: Option, - bits: u32, - alpha: bool, -} - -impl Layout { - fn parse(c: &str) -> Option { - let (base, bits) = match c.split_once('p') { - Some((base, bits)) if !bits.is_empty() && bits.bytes().all(|b| b.is_ascii_digit()) => (base, bits.parse().ok()?), - _ => (c, 8), - }; - let (base, bits) = match base.strip_prefix("mono") { - Some("") => ("mono", bits), - Some(digits) if bits == 8 => ("mono", digits.parse().ok()?), - _ => (base, bits), - }; - if !(8..=16).contains(&bits) { - return None; - } - let (chroma, alpha) = match base { - "420" | "420jpeg" | "420paldv" | "420mpeg2" => (Some(Chroma::C420), false), - "422" => (Some(Chroma::C422), false), - "444" => (Some(Chroma::C444), false), - "444alpha" if bits == 8 => (Some(Chroma::C444), true), - "mono" => (None, false), - _ => return None, - }; - Some(Layout { chroma, bits, alpha }) - } - - fn label(self) -> String { - let sub = match (self.chroma, self.alpha) { - (None, _) => "Monochrome", - (Some(_), true) => "YUVA 4:4:4:4", - (Some(c), false) => return format!("YUV {} {}-bit", c.label(), self.bits), - }; - format!("{sub} {}-bit", self.bits) +fn chroma(c: craft_y4m::Chroma) -> Chroma { + match c { + craft_y4m::Chroma::C420 => Chroma::C420, + craft_y4m::Chroma::C422 => Chroma::C422, + craft_y4m::Chroma::C444 => Chroma::C444, } } - -/// Sizes of one frame's planes in samples: luma, each chroma plane (0 for mono), alpha. -fn plane_samples(w: u32, h: u32, l: Layout) -> (usize, usize, usize) { - let luma = w as usize * h as usize; - let chroma = l.chroma.map_or(0, |c| { - let (sx, sy) = c.shifts(); - w.div_ceil(1 << sx) as usize * h.div_ceil(1 << sy) as usize - }); - (luma, chroma, if l.alpha { luma } else { 0 }) +fn layout_label(layout: craft_y4m::Layout) -> String { + let sub = match (layout.chroma, layout.alpha) { + (None, _) => "Monochrome", + (Some(_), true) => "YUVA 4:4:4:4", + (Some(c), false) => return format!("YUV {} {}-bit", chroma(c).label(), layout.bits), + }; + format!("{sub} {}-bit", layout.bits) } pub struct Y4mSource { info: MediaInfo, reader: filmcraft_media::SharedReader, - /// File offset of each frame's planes. - frames: Vec, - frame_bytes: usize, - layout: Layout, + stream: craft_y4m::Stream, rate: FrameRate, } -fn parse_ratio(s: &str) -> Option<(i64, i64)> { - let (n, d) = s.split_once(':')?; - Some((n.parse().ok()?, d.parse().ok()?)) -} - -/// The line starting at `pos` (without its newline) and the offset after it. -fn line_at(reader: &dyn filmcraft_media::reader::ByteReader, pos: u64) -> Option<(Vec, u64)> { - let mut want = 128usize; - loop { - let len = (reader.len().saturating_sub(pos)).min(want as u64) as usize; - let buf = filmcraft_media::reader::read_range(reader, pos, len).ok()?; - if let Some(end) = buf.iter().position(|&b| b == b'\n') { - let mut line = buf; - line.truncate(end); - return Some((line, pos + end as u64 + 1)); - } - if len < want || want >= MAX_LINE { - return None; - } - want = (want * 8).min(MAX_LINE); - } -} - impl Y4mSource { pub fn open(name: &str, bytes: Arc<[u8]>) -> crate::Result { Self::open_reader(name, Arc::new(filmcraft_media::reader::MemReader(bytes))) } pub fn open_reader(name: &str, reader: filmcraft_media::SharedReader) -> crate::Result { - let bad = |what: &str| CodecError::Container(format!("{name}: Y4M: {what}")); - let (header, mut pos) = line_at(&*reader, 0).ok_or_else(|| bad("no stream header"))?; - let header = String::from_utf8_lossy(&header).into_owned(); - let mut params = header.split(' '); - if params.next() != Some("YUV4MPEG2") { - return Err(bad("not a YUV4MPEG2 file")); - } - let (mut w, mut h, mut rate, mut par, mut colour, mut full) = (0u32, 0u32, None, (1u32, 1u32), "420jpeg".to_string(), false); - for p in params.filter(|p| !p.is_empty()) { - let (tag, v) = p.split_at(1); - match tag { - "W" => w = v.parse().map_err(|_| bad("invalid width"))?, - "H" => h = v.parse().map_err(|_| bad("invalid height"))?, - "F" => match parse_ratio(v) { - Some((n, d)) if n > 0 && d > 0 => rate = Some(FrameRate::new(n, d)), - _ => return Err(bad("invalid frame rate")), - }, - "A" => { - if let Some((n, d)) = parse_ratio(v).filter(|&(n, d)| n > 0 && d > 0) - && let (Ok(n), Ok(d)) = (u32::try_from(n), u32::try_from(d)) - { - par = (n, d); - } - } - "C" => colour = v.to_string(), - "X" => full |= v.eq_ignore_ascii_case("COLORRANGE=FULL"), - _ => {} - } - } - if w == 0 || h == 0 { - return Err(bad("missing or zero size")); - } - if u64::from(w) * u64::from(h) > MAX_PIXELS { - return Err(CodecError::Unsupported(format!("{name}: Y4M frames larger than 268 million pixels"))); - } - let layout = Layout::parse(&colour).ok_or_else(|| CodecError::Unsupported(format!("{name}: Y4M colour space {colour}")))?; - // ffmpeg's default when the file names no rate - let rate = rate.unwrap_or(FrameRate::FPS_25); - let (luma, chroma, alpha) = plane_samples(w, h, layout); - let per_sample = if layout.bits > 8 { 2 } else { 1 }; - let frame_bytes = (luma + 2 * chroma + alpha) * per_sample; - let len = reader.len(); - let mut frames = Vec::new(); - while pos < len { - let Some((line, data)) = line_at(&*reader, pos) else { break }; - if !line.starts_with(b"FRAME") { - break; - } - let Some(end) = data.checked_add(frame_bytes as u64).filter(|&e| e <= len) else { break }; - frames.push(data); - pos = end; - } - if frames.is_empty() { - return Err(bad("no complete frames")); - } + let stream = craft_y4m::Stream::open(len, |at, n| filmcraft_media::reader::read_range(&*reader, at, n).ok(), &craft_y4m::Limits::default()).map_err( + |e| match e { + craft_y4m::Error::UnsupportedColourSpace(c) => CodecError::Unsupported(format!("{name}: Y4M colour space {c}")), + craft_y4m::Error::LimitExceeded(_) => CodecError::Unsupported(format!("{name}: Y4M frames larger than 268 million pixels")), + craft_y4m::Error::Malformed(m) => CodecError::Container(format!("{name}: Y4M: {m}")), + }, + )?; + let hinfo = &stream.header; + let (w, h) = (hinfo.width, hinfo.height); + let rate = hinfo.frame_rate.map(|(n, d)| FrameRate::new(n, d)).unwrap_or(FrameRate::FPS_25); + let par = hinfo.pixel_aspect.unwrap_or((1, 1)); + let layout = hinfo.layout; + let full = hinfo.full_range; + let frame_bytes = stream.frame_bytes; let color = ColorInfo { matrix: filmcraft_frame::default_matrix(w, h), range: if full { Range::Full } else { Range::Limited }, ..ColorInfo::REC709 }; - let duration = Tick::from_rational(frames.len() as i64, rate.den, rate.num); + let duration = Tick::from_rational(stream.frames.len() as i64, rate.den, rate.num); let bitrate = (frame_bytes as u64 * 8).checked_mul(rate.num as u64).map(|b| b / rate.den as u64); let info = MediaInfo { name: name.to_string(), @@ -192,7 +79,7 @@ impl Y4mSource { frame_rate: rate, par, codec: "Uncompressed YUV".into(), - pixel_format: layout.label(), + pixel_format: layout_label(layout), color, has_alpha: layout.alpha, bitrate, @@ -204,40 +91,39 @@ impl Y4mSource { start_timecode: None, file_size: Some(len), }; - Ok(Self { info, reader, frames, frame_bytes, layout, rate }) + Ok(Self { info, reader, stream, rate }) } fn frame(&self, idx: usize) -> crate::Result { - let (Some(&at), Some(v)) = (self.frames.get(idx), self.info.video.as_ref()) else { + let (Some(&at), Some(v)) = (self.stream.frames.get(idx), self.info.video.as_ref()) else { return Err(CodecError::Container("Y4M: frame index out of range".into())); }; - let data = filmcraft_media::reader::read_range(&*self.reader, at, self.frame_bytes).map_err(|e| CodecError::Container(e.to_string()))?; - let (luma, chroma, alpha) = plane_samples(v.width, v.height, self.layout); - // mono: neutral chroma at 4:2:0 - let mono = Layout { chroma: Some(Chroma::C420), ..self.layout }; - let grey = plane_samples(v.width, v.height, mono).1; - let pixels = if self.layout.bits == 8 { + let data = filmcraft_media::reader::read_range(&*self.reader, at, self.stream.frame_bytes).map_err(|e| CodecError::Container(e.to_string()))?; + let header = &self.stream.header; + let layout = header.layout; + let (luma, chroma_samples, alpha) = header.plane_samples(); + let grey = header.chroma_samples(craft_y4m::Chroma::C420); + let pixels = if layout.bits == 8 { let plane = |i: usize, n: usize| Arc::new(data.get(i..i + n).map(<[u8]>::to_vec).unwrap_or_else(|| vec![0; n])); - let (y, cb, cr) = match self.layout.chroma { - Some(_) => (plane(0, luma), plane(luma, chroma), plane(luma + chroma, chroma)), + let (y, cb, cr) = match layout.chroma { + Some(_) => (plane(0, luma), plane(luma, chroma_samples), plane(luma + chroma_samples, chroma_samples)), None => (plane(0, luma), Arc::new(vec![128; grey]), Arc::new(vec![128; grey])), }; - let a = self.layout.alpha.then(|| plane(luma + 2 * chroma, alpha)); - PixelData::Yuv8 { planes: [y, cb, cr], chroma: self.layout.chroma.unwrap_or(Chroma::C420), alpha: a } + let a = layout.alpha.then(|| plane(luma + 2 * chroma_samples, alpha)); + PixelData::Yuv8 { planes: [y, cb, cr], chroma: layout.chroma.map(chroma).unwrap_or(Chroma::C420), alpha: a } } else { - let max = (1u32 << self.layout.bits) - 1; let plane = |i: usize, n: usize| { Arc::new(match data.get(i * 2..(i + n) * 2) { - Some(b) => b.as_chunks::<2>().0.iter().map(|&s| u32::from(u16::from_le_bytes(s)).min(max) as u16).collect(), + Some(b) => craft_y4m::samples_u16(b, layout.bits), None => vec![0; n], }) }; - let mid = 1u16 << (self.layout.bits - 1); - let (y, cb, cr) = match self.layout.chroma { - Some(_) => (plane(0, luma), plane(luma, chroma), plane(luma + chroma, chroma)), + let mid = 1u16 << (layout.bits - 1); + let (y, cb, cr) = match layout.chroma { + Some(_) => (plane(0, luma), plane(luma, chroma_samples), plane(luma + chroma_samples, chroma_samples)), None => (plane(0, luma), Arc::new(vec![mid; grey]), Arc::new(vec![mid; grey])), }; - PixelData::Yuv16 { planes: [y, cb, cr], chroma: self.layout.chroma.unwrap_or(Chroma::C420), bits: self.layout.bits, alpha: None } + PixelData::Yuv16 { planes: [y, cb, cr], chroma: layout.chroma.map(chroma).unwrap_or(Chroma::C420), bits: layout.bits, alpha: None } }; Ok(VideoFrame { width: v.width, @@ -256,7 +142,7 @@ impl MediaSource for Y4mSource { } fn video_frame(&self, req: FrameRequest) -> Result, MediaError> { - let last = self.frames.len().saturating_sub(1); + let last = self.stream.frames.len().saturating_sub(1); let idx = usize::try_from(self.rate.frame_at(req.time.max(Tick::ZERO))).unwrap_or(last).min(last); Ok(Arc::new(self.frame(idx)?)) } @@ -284,17 +170,28 @@ pub fn reader_opener(name: &str, head: &[u8], reader: &filmcraft_media::SharedRe mod tests { use super::*; + #[test] + fn non_ascii_unknown_header_tag_is_safe() { + let mut bytes = b"YUV4MPEG2 W2 H2 F25:1 Cmono ".to_vec(); + bytes.extend_from_slice("é".as_bytes()); + bytes.extend_from_slice(b"\nFRAME\n"); + bytes.extend_from_slice(&[10, 20, 30, 40]); + let source = Y4mSource::open("unicode.y4m", bytes.into()).unwrap(); + assert_eq!(source.info().video.as_ref().unwrap().width, 2); + assert!(source.frame(0).is_ok()); + } + #[test] fn colour_space_names() { - let l = |c: Option, bits, alpha| Some(Layout { chroma: c, bits, alpha }); - assert_eq!(Layout::parse("420jpeg"), l(Some(Chroma::C420), 8, false)); - assert_eq!(Layout::parse("420p10"), l(Some(Chroma::C420), 10, false)); - assert_eq!(Layout::parse("422p12"), l(Some(Chroma::C422), 12, false)); - assert_eq!(Layout::parse("444alpha"), l(Some(Chroma::C444), 8, true)); - assert_eq!(Layout::parse("mono"), l(None, 8, false)); - assert_eq!(Layout::parse("mono16"), l(None, 16, false)); + let l = |c: Option, bits, alpha| Some(craft_y4m::Layout { chroma: c, bits, alpha }); + assert_eq!(craft_y4m::Layout::parse("420jpeg"), l(Some(craft_y4m::Chroma::C420), 8, false)); + assert_eq!(craft_y4m::Layout::parse("420p10"), l(Some(craft_y4m::Chroma::C420), 10, false)); + assert_eq!(craft_y4m::Layout::parse("422p12"), l(Some(craft_y4m::Chroma::C422), 12, false)); + assert_eq!(craft_y4m::Layout::parse("444alpha"), l(Some(craft_y4m::Chroma::C444), 8, true)); + assert_eq!(craft_y4m::Layout::parse("mono"), l(None, 8, false)); + assert_eq!(craft_y4m::Layout::parse("mono16"), l(None, 16, false)); for bad in ["411", "420p", "420p7", "420p17", "mono10p12", "444alphap10", "", "p10"] { - assert_eq!(Layout::parse(bad), None, "{bad}"); + assert_eq!(craft_y4m::Layout::parse(bad), None, "{bad}"); } } } From 0a6d18840c5b493cd04e01a4284c5db0f8d7b511 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:23:12 +0000 Subject: [PATCH 07/24] export: update shared EXR writer lint fix --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82ac44bc7..b0bab0f26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30#b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30" +source = "git+https://github.com/storytold/craft-libs?rev=4c17e51335192345d8e508287c26f17fead930ac#4c17e51335192345d8e508287c26f17fead930ac" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 39df04d1f..f2c581123 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b2edb7ed80d8c8fac639050a3ea02f6e0ecaff30" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "4c17e51335192345d8e508287c26f17fead930ac" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From efc74da96fe6fa9b371aa6cc18f523b3a43da4b9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:27:23 +0000 Subject: [PATCH 08/24] codecs: use shared interleaved PCM decoder --- Cargo.lock | 19 ++++++++++++---- Cargo.toml | 6 ++--- crates/codecs/src/audio.rs | 46 ++++++++------------------------------ 3 files changed, 27 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8036afe2..cf45f8c36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1061,9 +1061,10 @@ dependencies = [ [[package]] name = "craft-audio" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=156b491b91f3693f4fce53bf69bb97791f51198f#156b491b91f3693f4fce53bf69bb97791f51198f" +source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" dependencies = [ "craft-media", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0)", "log", "rayon", "symphonia 0.6.1", @@ -1076,7 +1077,7 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "craft-timeline", - "craft-xml", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9)", "thiserror 2.0.21", ] @@ -1129,7 +1130,7 @@ dependencies = [ [[package]] name = "craft-media" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=156b491b91f3693f4fce53bf69bb97791f51198f#156b491b91f3693f4fce53bf69bb97791f51198f" +source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" dependencies = [ "chrono", "thiserror 2.0.21", @@ -1194,7 +1195,7 @@ dependencies = [ [[package]] name = "craft-video" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=156b491b91f3693f4fce53bf69bb97791f51198f#156b491b91f3693f4fce53bf69bb97791f51198f" +source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" dependencies = [ "craft-media", "rayon", @@ -1230,6 +1231,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "crc32fast" version = "1.5.2" diff --git a/Cargo.toml b/Cargo.toml index 5f2730c46..24d19f47e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,10 +59,10 @@ craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both # 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"] } +craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", default-features = false } +craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", 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, 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"] } +craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", 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" } diff --git a/crates/codecs/src/audio.rs b/crates/codecs/src/audio.rs index e6c18d109..8bccf4929 100644 --- a/crates/codecs/src/audio.rs +++ b/crates/codecs/src/audio.rs @@ -792,43 +792,9 @@ pub fn decode_dvd_lpcm(data: &[u8], channels: usize, bits: u32) -> Vec> /// Decode interleaved PCM bytes into planar f32. pub fn decode_pcm(data: &[u8], cfg: &filmcraft_isobmff::PcmConfig) -> Vec> { - let ch = cfg.channels.max(1) as usize; - // A hostile `ipcm`/`fpcm` sample entry can declare `bits == 0` (`a.sample_size` is - // applied unchecked, unlike the `sowt`/`twos` arms of `qt_pcm` which default it): `bps` - // would be 0 and the length division below would panic. There are no samples to decode. - if cfg.bits == 0 { - return vec![Vec::new(); ch]; - } - let bps = (cfg.bits as usize).div_ceil(8); - let n = data.len() / (bps * ch); - let mut out = vec![Vec::with_capacity(n); ch]; - for i in 0..n { - for (c, o) in out.iter_mut().enumerate() { - let off = (i * ch + c) * bps; - let b = &data[off..off + bps]; - let v = match (cfg.bits, cfg.float, cfg.big_endian) { - (32, true, false) => f32::from_le_bytes([b[0], b[1], b[2], b[3]]), - (32, true, true) => f32::from_be_bytes([b[0], b[1], b[2], b[3]]), - (64, true, false) => f64::from_le_bytes(b.try_into().unwrap_or([0; 8])) as f32, - (64, true, true) => f64::from_be_bytes(b.try_into().unwrap_or([0; 8])) as f32, - (8, _, _) => { - if cfg.signed { - b[0] as i8 as f32 / 128.0 - } else { - (b[0] as f32 - 128.0) / 128.0 - } - } - (16, _, false) => i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0, - (16, _, true) => i16::from_be_bytes([b[0], b[1]]) as f32 / 32768.0, - (24, _, false) => (i32::from_le_bytes([0, b[0], b[1], b[2]]) >> 8) as f32 / 8_388_608.0, - (24, _, true) => (i32::from_be_bytes([b[0], b[1], b[2], 0]) >> 8) as f32 / 8_388_608.0, - (32, false, false) => i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0, - (32, false, true) => i32::from_be_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0, - _ => 0.0, - }; - o.push(v); - } - } + let mut out = vec![Vec::new(); cfg.channels.max(1) as usize]; + let format = craft_audio::pcm_interleaved::Format { bits: cfg.bits, float: cfg.float, big_endian: cfg.big_endian, signed: cfg.signed }; + craft_audio::pcm_interleaved::decode_into(data, format, &mut out); out } @@ -1540,6 +1506,12 @@ mod gapless_tests { mod decode_pcm_tests { use super::*; + #[test] + fn pcm_adapter_decodes_interleaved_big_endian_samples() { + let cfg = filmcraft_isobmff::PcmConfig { bits: 16, float: false, big_endian: true, signed: true, channels: 2, sample_rate: 48_000.0 }; + assert_eq!(decode_pcm(&[0x40, 0, 0xc0, 0, 0xff], &cfg), [vec![0.5], vec![-0.5]]); + } + #[test] fn zero_bits_decode_to_empty_channels_without_panicking() { // A hostile `ipcm`/`fpcm` sample entry with `sample_size == 0` used to divide by zero From cecd67680bcb1f9791cb6d5ce11040fe1cb0c744 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:37:34 +0000 Subject: [PATCH 09/24] dng: use shared lossless JPEG decoder --- Cargo.lock | 21 ++ Cargo.toml | 2 + crates/dng/Cargo.toml | 1 + crates/dng/src/ljpeg.rs | 508 +--------------------------------------- 4 files changed, 32 insertions(+), 500 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8036afe2..9bd75d2d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1135,6 +1135,26 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-raw" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +dependencies = [ + "craft-tiff", + "rayon", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-timeline" version = "0.1.0" @@ -2090,6 +2110,7 @@ dependencies = [ name = "filmcraft-dng" version = "0.20.1" dependencies = [ + "craft-raw", "filmcraft-dng", "image", "rayon", diff --git a/Cargo.toml b/Cargo.toml index 5f2730c46..e052416f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,8 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] } rayon = "1" proptest = "1" roxmltree = "0.21" +# Shared lossless JPEG decoding for DNG. +craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d16ee7926c8403ded5ea3209bc2ab14b1570281a", default-features = false } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both diff --git a/crates/dng/Cargo.toml b/crates/dng/Cargo.toml index ca449ff43..17973f854 100644 --- a/crates/dng/Cargo.toml +++ b/crates/dng/Cargo.toml @@ -15,6 +15,7 @@ writer = [] serde = ["dep:serde"] [dependencies] +craft-raw = { workspace = true } rayon = { workspace = true, optional = true } # Baseline (lossy) JPEG for Compression 34892 tiles: the JPEG decoder the workspace already uses # (crates/codecs, crates/media), pure Rust. diff --git a/crates/dng/src/ljpeg.rs b/crates/dng/src/ljpeg.rs index 25cf4da8f..026782ce4 100644 --- a/crates/dng/src/ljpeg.rs +++ b/crates/dng/src/ljpeg.rs @@ -1,506 +1,14 @@ -//! Lossless JPEG decoding: ITU-T T.81 | ISO/IEC 10918-1 Annex H (process 14, Huffman coded, -//! "SOF3"), the compression DNG calls lossless JPEG (Compression = 7). -//! -//! - Frame header (B.2.2): 2–16-bit precision, 1–4 components, each sampled 1×1. -//! - Huffman tables (B.2.4.2, Annex C) decoded with a 9-bit lookup and the F.2.2.3 code-length -//! walk for longer codes. -//! - Scan header (B.2.3): one scan holding every component, predictor `Ss` 1–7 (Table H.1) and -//! point transform `Al`. -//! - Prediction (H.1.2.1): the first line (and the first line after a restart) predicts from the -//! left neighbour with 2^(P − Pt − 1) for its first sample; later lines predict their first -//! sample from the one above and the rest with the selected predictor. Differences are added -//! modulo 2^16 (H.1.2.2) and the SSSS = 16 category is 32768 with no extra bits. -//! - Restart intervals (DRI, RSTn) that span whole lines. +//! Lossless JPEG (T.81 process 14) decoding for DNG, using the shared line-restart decoder. use crate::{Error, Result}; +pub use craft_raw::ljpeg_lines::{Decoded, predict}; -/// A decoded lossless JPEG image. -#[derive(Debug)] -pub struct Decoded { - pub width: usize, - pub height: usize, - pub components: usize, - pub precision: u8, - pub predictor: u8, - /// Samples, interleaved by component, line after line, shifted back up by the point transform. - pub samples: Vec, -} - -const LOOKUP_BITS: u32 = 9; -/// Bits of the combined code + difference lookup. -const FULL_BITS: u32 = 13; -/// A combined lookup entry: bit 8 set, the bits used in 0–7, the difference in 16–31. -const FULL_VALID: u32 = 0x100; - -/// A Huffman table for decoding (T.81 Annex C and F.2.2.3). -#[derive(Clone)] -struct Huffman { - /// For codes of up to LOOKUP_BITS bits: (length, value) indexed by the next LOOKUP_BITS bits; - /// length 0 means "longer code". - fast: Vec<(u8, u8)>, - maxcode: [i32; 17], - valptr: [i32; 17], - mincode: [i32; 17], - values: Vec, - /// For a code and its extra bits that fit in FULL_BITS together (difference categories - /// 0–15): `FULL_VALID | used bits | difference << 16`, indexed by the next FULL_BITS bits; - /// 0 means "decode the slow way". - full: Vec, -} - -impl Huffman { - fn new(bits: &[u8; 16], values: Vec) -> Result { - // C.2: code sizes and codes - let mut sizes = Vec::new(); - for (l, &n) in bits.iter().enumerate() { - for _ in 0..n { - sizes.push(l as u8 + 1); - } - } - if sizes.len() != values.len() || sizes.is_empty() { - return Err(Error::Malformed("lossless JPEG: Huffman table counts")); - } - let mut codes = Vec::with_capacity(sizes.len()); - let mut code: u32 = 0; - let mut si = sizes[0]; - let mut k = 0; - while k < sizes.len() { - while k < sizes.len() && sizes[k] == si { - codes.push(code); - code += 1; - k += 1; - } - // the codes of one length must fit in that length - if code > (1u32 << si) { - return Err(Error::Malformed("lossless JPEG: Huffman table over-subscribed")); - } - code <<= 1; - si += 1; - } - // F.2.2.3 decoder tables - let mut maxcode = [-1i32; 17]; - let mut valptr = [0i32; 17]; - let mut mincode = [0i32; 17]; - let mut j = 0usize; - for l in 1..=16usize { - let n = usize::from(bits[l - 1]); - if n == 0 { - maxcode[l] = -1; - } else { - valptr[l] = j as i32; - mincode[l] = codes[j] as i32; - j += n; - maxcode[l] = codes[j - 1] as i32; - } - } - let mut fast = vec![(0u8, 0u8); 1 << LOOKUP_BITS]; - for (i, (&c, &s)) in codes.iter().zip(&sizes).enumerate() { - let s = u32::from(s); - if s <= LOOKUP_BITS { - let shift = LOOKUP_BITS - s; - let base = (c << shift) as usize; - for f in fast.iter_mut().skip(base).take(1 << shift) { - *f = (s as u8, values[i]); - } - } - } - let mut full = vec![0u32; 1 << FULL_BITS]; - for (&c, (&s, &ssss)) in codes.iter().zip(sizes.iter().zip(&values)) { - let (s, ssss) = (u32::from(s), u32::from(ssss)); - if ssss >= 16 || s + ssss > FULL_BITS { - continue; - } - let total = s + ssss; - for v in 0..1u32 << ssss { - let diff = match ssss { - 0 => 0, - k if v < 1 << (k - 1) => v as i32 - (1 << k) + 1, - _ => v as i32, - }; - let entry = FULL_VALID | total | (u32::from(diff as i16 as u16) << 16); - let shift = FULL_BITS - total; - let base = (((c << ssss) | v) << shift) as usize; - for f in full.iter_mut().skip(base).take(1 << shift) { - *f = entry; - } - } - } - Ok(Huffman { fast, maxcode, valptr, mincode, values, full }) - } -} - -/// Entropy-coded data reader: removes stuffed zero bytes and stops at markers (feeding zeros). -struct Bits<'a> { - data: &'a [u8], - pos: usize, - acc: u64, - n: u32, - /// The marker that ended the data (after 0xFF), if one was met. - marker: Option, -} - -impl<'a> Bits<'a> { - fn new(data: &'a [u8], pos: usize) -> Self { - Bits { data, pos, acc: 0, n: 0, marker: None } - } - - fn fill(&mut self) { - // fast path: eight bytes without a 0xFF (no stuffing, no marker) go in at once - if self.marker.is_none() - && let Some(b) = self.data.get(self.pos..self.pos + 8) - { - let chunk = u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]); - let inv = !chunk; - let has_ff = inv.wrapping_sub(0x0101_0101_0101_0101) & !inv & 0x8080_8080_8080_8080 != 0; - if !has_ff { - let take = (64 - self.n) / 8; - if take > 0 { - self.acc |= (chunk >> (64 - 8 * take)) << (64 - self.n - 8 * take); - self.pos += take as usize; - self.n += 8 * take; - return; - } - } - } - while self.n <= 56 { - let mut byte = 0u8; - if self.marker.is_none() { - match self.data.get(self.pos) { - Some(&0xFF) => match self.data.get(self.pos + 1) { - Some(&0x00) => { - byte = 0xFF; - self.pos += 2; - } - Some(&m) => self.marker = Some(m), - None => self.marker = Some(0xD9), - }, - Some(&b) => { - byte = b; - self.pos += 1; - } - None => self.marker = Some(0xD9), - } - } - self.acc |= u64::from(byte) << (56 - self.n); - self.n += 8; - } - } - - fn peek(&mut self, k: u32) -> u32 { - if self.n < k { - self.fill(); - } - (self.acc >> (64 - k)) as u32 - } - - fn skip(&mut self, k: u32) { - self.acc <<= k; - self.n -= k; - } - - fn bits(&mut self, k: u32) -> u32 { - if k == 0 { - return 0; - } - let v = self.peek(k); - self.skip(k); - v - } - - /// Decode one Huffman symbol (F.2.2.3). - fn decode(&mut self, h: &Huffman) -> Result { - let p = self.peek(LOOKUP_BITS) as usize; - if let Some(&(len, v)) = h.fast.get(p) - && len > 0 - { - self.skip(u32::from(len)); - return Ok(v); - } - let mut code = self.bits(1) as i32; - for l in 1..=16usize { - if code <= h.maxcode[l] { - let i = h.valptr[l] + code - h.mincode[l]; - return h.values.get(i as usize).copied().ok_or(Error::Malformed("lossless JPEG: bad Huffman code")); - } - code = (code << 1) | self.bits(1) as i32; - } - Err(Error::Malformed("lossless JPEG: bad Huffman code")) - } - - /// Skip to the byte after the next RSTn marker, starting a new bit stream there. - fn restart(&mut self) -> Result<()> { - // the decoder may have looked ahead into fill bits; find the marker in the bytes - let mut p = self.pos; - loop { - match (self.data.get(p), self.data.get(p + 1)) { - (Some(0xFF), Some(&m)) if (0xD0..=0xD7).contains(&m) => { - *self = Bits::new(self.data, p + 2); - return Ok(()); - } - (Some(_), Some(_)) => p += 1, - _ => return Err(Error::Malformed("lossless JPEG: missing restart marker")), - } - } - } -} - -struct Component { - id: u8, - table: usize, -} - -/// Decode a lossless JPEG stream. `max_samples` caps the picture (width × height × components). +/// Decode a lossless JPEG stream, capping the total number of samples. pub fn decode(data: &[u8], max_samples: usize) -> Result { - if data.get(0..2) != Some(&[0xFF, 0xD8]) { - return Err(Error::Malformed("lossless JPEG: no SOI marker")); - } - let mut pos = 2usize; - let mut tables: [Option; 4] = [None, None, None, None]; - let mut frame: Option<(u8, usize, usize, Vec)> = None; - let mut restart_interval = 0usize; - loop { - // markers may be preceded by fill bytes 0xFF - while data.get(pos) == Some(&0xFF) && data.get(pos + 1) == Some(&0xFF) { - pos += 1; - } - let (Some(&0xFF), Some(&m)) = (data.get(pos), data.get(pos + 1)) else { - return Err(Error::Malformed("lossless JPEG: expected a marker")); - }; - pos += 2; - if m == 0xD9 { - return Err(Error::Malformed("lossless JPEG: no scan")); - } - if (0xD0..=0xD8).contains(&m) || m == 0x01 { - continue; // markers without a segment - } - let len_b = data.get(pos..pos + 2).ok_or(Error::Truncated)?; - let len = usize::from(u16::from_be_bytes([len_b[0], len_b[1]])); - if len < 2 { - return Err(Error::Malformed("lossless JPEG: segment length")); - } - let seg = data.get(pos + 2..pos + len).ok_or(Error::Truncated)?; - match m { - 0xC3 => { - let [p, y0, y1, x0, x1, nf, rest @ ..] = seg else { - return Err(Error::Malformed("lossless JPEG: short frame header")); - }; - let (precision, nf) = (*p, usize::from(*nf)); - let height = usize::from(u16::from_be_bytes([*y0, *y1])); - let width = usize::from(u16::from_be_bytes([*x0, *x1])); - if !(2..=16).contains(&precision) { - return Err(Error::Unsupported(format!("lossless JPEG precision {precision}"))); - } - if height == 0 { - return Err(Error::Unsupported("lossless JPEG with the height in a DNL marker".into())); - } - if width == 0 || !(1..=4).contains(&nf) || rest.len() < nf * 3 { - return Err(Error::Malformed("lossless JPEG: frame header")); - } - let mut comps = Vec::with_capacity(nf); - for c in rest.as_chunks::<3>().0.iter().take(nf) { - if c[1] != 0x11 { - return Err(Error::Unsupported("subsampled lossless JPEG components".into())); - } - comps.push(Component { id: c[0], table: 0 }); - } - frame = Some((precision, width, height, comps)); - } - 0xC4 => { - let mut s = seg; - while !s.is_empty() { - let [tcth, counts @ ..] = s else { break }; - let bits: [u8; 16] = counts.get(..16).and_then(|b| b.try_into().ok()).ok_or(Error::Truncated)?; - let n: usize = bits.iter().map(|&b| usize::from(b)).sum(); - let values = counts.get(16..16 + n).ok_or(Error::Truncated)?.to_vec(); - let th = usize::from(tcth & 0x0F); - if tcth >> 4 != 0 || th > 3 { - return Err(Error::Malformed("lossless JPEG: Huffman table class or id")); - } - if values.iter().any(|&v| v > 16) { - return Err(Error::Malformed("lossless JPEG: difference category above 16")); - } - tables[th] = Some(Huffman::new(&bits, values)?); - s = counts.get(16 + n..).unwrap_or(&[]); - } - } - 0xDD => { - let r = seg.get(0..2).ok_or(Error::Truncated)?; - restart_interval = usize::from(u16::from_be_bytes([r[0], r[1]])); - } - 0xDA => { - let Some((precision, width, height, mut comps)) = frame.take() else { - return Err(Error::Malformed("lossless JPEG: scan before the frame header")); - }; - let ns = usize::from(*seg.first().ok_or(Error::Truncated)?); - if ns != comps.len() { - return Err(Error::Unsupported("lossless JPEG with more than one scan".into())); - } - for k in 0..ns { - let c = seg.get(1 + 2 * k..3 + 2 * k).ok_or(Error::Truncated)?; - let comp = comps.iter_mut().find(|x| x.id == c[0]).ok_or(Error::Malformed("lossless JPEG: unknown scan component"))?; - comp.table = usize::from(c[1] >> 4); - if comp.table > 3 { - return Err(Error::Malformed("lossless JPEG: Huffman table id")); - } - } - let tail = seg.get(1 + 2 * ns..4 + 2 * ns).ok_or(Error::Truncated)?; - let (predictor, pt) = (tail[0], tail[2] & 0x0F); - if !(1..=7).contains(&predictor) { - return Err(Error::Unsupported(format!("lossless JPEG predictor {predictor}"))); - } - if pt >= precision { - return Err(Error::Malformed("lossless JPEG: point transform")); - } - let n = width.checked_mul(height).and_then(|v| v.checked_mul(comps.len())).ok_or(Error::TooLarge)?; - if n > max_samples { - return Err(Error::TooLarge); - } - let mut hufs = Vec::with_capacity(comps.len()); - for c in &comps { - hufs.push(tables.get(c.table).and_then(Option::as_ref).ok_or(Error::Malformed("lossless JPEG: missing Huffman table"))?); - } - // every sample costs at least one bit of entropy-coded data (a Huffman code is at - // least one bit long), so a frame bigger than the data cannot be real - if n.div_ceil(8) > data.len().saturating_sub(pos + len) { - return Err(Error::Truncated); - } - let params = ScanParams { width, height, precision, predictor, pt, restart: restart_interval }; - let samples = scan(data, pos + len, &hufs, ¶ms)?; - return Ok(Decoded { width, height, components: comps.len(), precision, predictor, samples }); - } - 0xC0..=0xC2 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => { - return Err(Error::Unsupported("lossy (DCT) JPEG in a DNG".into())); - } - _ => {} // APPn, COM, DQT … are skipped - } - pos += len; - } -} - -/// Frame and scan parameters needed to decode one scan. -struct ScanParams { - width: usize, - height: usize, - precision: u8, - predictor: u8, - pt: u8, - /// Restart interval in samples per component (0 = none). - restart: usize, -} - -/// The next difference (F.2.2.1 and H.1.2.2): category and extra bits from the combined table -/// when they fit, else symbol by symbol. -#[inline(always)] -fn difference(bits: &mut Bits, h: &Huffman) -> Result { - let e = h.full.get(bits.peek(FULL_BITS) as usize).copied().unwrap_or(0); - if e & FULL_VALID != 0 { - bits.skip(e & 0xFF); - return Ok(i32::from((e >> 16) as u16 as i16)); - } - Ok(match u32::from(bits.decode(h)?) { - 0 => 0, - 16 => 32768, - s => { - let v = bits.bits(s) as i32; - if v < (1 << (s - 1)) { v - (1 << s) + 1 } else { v } - } + craft_raw::ljpeg_lines::decode(data, max_samples).map_err(|e| match e { + craft_raw::ljpeg_lines::Error::Truncated => Error::Truncated, + craft_raw::ljpeg_lines::Error::TooLarge => Error::TooLarge, + craft_raw::ljpeg_lines::Error::Malformed(s) => Error::Malformed(s), + craft_raw::ljpeg_lines::Error::Unsupported(s) => Error::Unsupported(s), }) } - -/// Decode the entropy-coded data of one scan (H.1.2). -fn scan(data: &[u8], start: usize, hufs: &[&Huffman], params: &ScanParams) -> Result> { - let ScanParams { width, height, precision, predictor, pt, restart } = *params; - let nc = hufs.len(); - let row_len = width * nc; - if restart != 0 && !restart.is_multiple_of(width) { - return Err(Error::Unsupported("lossless JPEG restart interval that splits a line".into())); - } - let lines_per_interval = if restart == 0 { usize::MAX } else { restart / width }; - let mut out = vec![0u16; row_len * height]; - let mut bits = Bits::new(data, start); - let initial = 1i32 << (precision - pt - 1); - let mut line_in_interval = 0usize; - for y in 0..height { - if line_in_interval == lines_per_interval { - bits.restart()?; - line_in_interval = 0; - } - let (above, cur) = out.split_at_mut(y * row_len); - let cur = cur.get_mut(..row_len).ok_or(Error::TooLarge)?; - let above = if line_in_interval == 0 { None } else { above.get((y - 1) * row_len..) }; - match predictor { - 1 => line::<1>(&mut bits, hufs, cur, above, initial)?, - 2 => line::<2>(&mut bits, hufs, cur, above, initial)?, - 3 => line::<3>(&mut bits, hufs, cur, above, initial)?, - 4 => line::<4>(&mut bits, hufs, cur, above, initial)?, - 5 => line::<5>(&mut bits, hufs, cur, above, initial)?, - 6 => line::<6>(&mut bits, hufs, cur, above, initial)?, - _ => line::<7>(&mut bits, hufs, cur, above, initial)?, - } - line_in_interval += 1; - } - if pt > 0 { - for s in &mut out { - *s <<= pt; - } - } - Ok(out) -} - -/// One line of samples (H.1.2.1): the first line of a restart interval (`above` is `None`) -/// predicts from the left with `initial` for its first sample; other lines predict their first -/// sample from above and the rest with predictor `P`. -#[inline(always)] -fn line(bits: &mut Bits, hufs: &[&Huffman], cur: &mut [u16], above: Option<&[u16]>, initial: i32) -> Result<()> { - let nc = hufs.len(); - let row_len = cur.len(); - let above = match above { - Some(a) => Some(a.get(..row_len).ok_or(Error::TooLarge)?), - None => None, - }; - for (c, h) in hufs.iter().enumerate() { - let d = difference(bits, h)?; - let px = match above { - Some(a) => i32::from(a[c]), - None => initial, - }; - cur[c] = ((px + d) & 0xFFFF) as u16; - } - let mut i = nc; - match above { - None => { - while i < row_len { - for h in hufs { - let d = difference(bits, h)?; - cur[i] = ((i32::from(cur[i - nc]) + d) & 0xFFFF) as u16; - i += 1; - } - } - } - Some(a) => { - while i < row_len { - for h in hufs { - let d = difference(bits, h)?; - let px = predict(P, i32::from(cur[i - nc]), i32::from(a[i]), i32::from(a[i - nc])); - cur[i] = ((px + d) & 0xFFFF) as u16; - i += 1; - } - } - } - } - Ok(()) -} - -/// Table H.1 predictors (`ra` left, `rb` above, `rc` above left). -#[inline(always)] -pub fn predict(p: u8, ra: i32, rb: i32, rc: i32) -> i32 { - match p { - 1 => ra, - 2 => rb, - 3 => rc, - 4 => ra + rb - rc, - 5 => ra + ((rb - rc) >> 1), - 6 => rb + ((ra - rc) >> 1), - _ => (ra + rb) >> 1, - } -} From 7cb6b736beae30c783b353fd0d06af722f916aac Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:40:51 +0000 Subject: [PATCH 10/24] dng: use shared bounded classic TIFF reader --- Cargo.lock | 12 +- Cargo.toml | 1 + crates/dng/Cargo.toml | 1 + crates/dng/src/tiff.rs | 407 ++++------------------------------------- 4 files changed, 53 insertions(+), 368 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9bd75d2d5..af4d5bc5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1140,12 +1140,21 @@ name = "craft-raw" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" dependencies = [ - "craft-tiff", + "craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a)", "rayon", "serde", "thiserror 2.0.21", ] +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8b9010ae5356b6588eb22c2e1ca4b4ae12647939#8b9010ae5356b6588eb22c2e1ca4b4ae12647939" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-tiff" version = "0.1.0" @@ -2111,6 +2120,7 @@ name = "filmcraft-dng" version = "0.20.1" dependencies = [ "craft-raw", + "craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8b9010ae5356b6588eb22c2e1ca4b4ae12647939)", "filmcraft-dng", "image", "rayon", diff --git a/Cargo.toml b/Cargo.toml index e052416f8..0dc83f28e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared lossless JPEG decoding for DNG. +craft-tiff = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "8b9010ae5356b6588eb22c2e1ca4b4ae12647939" } craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d16ee7926c8403ded5ea3209bc2ab14b1570281a", default-features = false } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } diff --git a/crates/dng/Cargo.toml b/crates/dng/Cargo.toml index 17973f854..4e8028274 100644 --- a/crates/dng/Cargo.toml +++ b/crates/dng/Cargo.toml @@ -16,6 +16,7 @@ serde = ["dep:serde"] [dependencies] craft-raw = { workspace = true } +craft-tiff = { workspace = true } rayon = { workspace = true, optional = true } # Baseline (lossy) JPEG for Compression 34892 tiles: the JPEG decoder the workspace already uses # (crates/codecs, crates/media), pure Rust. diff --git a/crates/dng/src/tiff.rs b/crates/dng/src/tiff.rs index 1dc5cba87..f0dcd7228 100644 --- a/crates/dng/src/tiff.rs +++ b/crates/dng/src/tiff.rs @@ -1,400 +1,73 @@ -//! The TIFF container (TIFF 6.0 §2, as DNG uses it): byte order, image file directories (IFDs), -//! their entries and values, and the SubIFD tree. -//! -//! Every offset and count comes from the file and is checked before use: an entry whose value lies -//! outside the file is an error when it is read, the walk visits at most [`MAX_IFDS`] directories, -//! never the same one twice (cyclic chains end), and goes at most [`MAX_DEPTH`] SubIFD levels deep. - -use std::collections::HashSet; +//! Bounded classic TIFF access for DNG, implemented by the shared TIFF library. use crate::{Error, Result}; +pub use craft_tiff::classic::{Entry, Ifd, MAX_DEPTH, MAX_IFDS, Type, tags}; -/// Most IFDs one file may have (main chain and SubIFDs together). -pub const MAX_IFDS: usize = 64; -/// Deepest SubIFD nesting followed. -pub const MAX_DEPTH: usize = 4; -/// Invalid SubIFD edges count too: otherwise a file can make failed offset probes quadratic. -const MAX_IFD_ATTEMPTS: usize = MAX_IFDS * 16; -/// SubIFD references (zero, repeated and invalid ones included) accepted over the whole walk, -/// counted before a list is read: many directories can name the same long SubIFD table. -const MAX_SUB_IFD_REFS: usize = MAX_IFD_ATTEMPTS; - -/// TIFF field types (TIFF 6.0 §2, plus type 13 IFD from TIFF Technical Note 1). -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Type { - Byte, - Ascii, - Short, - Long, - Rational, - SByte, - Undefined, - SShort, - SLong, - SRational, - Float, - Double, - Ifd, -} - -impl Type { - fn from_code(c: u16) -> Option { - Some(match c { - 1 => Type::Byte, - 2 => Type::Ascii, - 3 => Type::Short, - 4 => Type::Long, - 5 => Type::Rational, - 6 => Type::SByte, - 7 => Type::Undefined, - 8 => Type::SShort, - 9 => Type::SLong, - 10 => Type::SRational, - 11 => Type::Float, - 12 => Type::Double, - 13 => Type::Ifd, - _ => return None, - }) - } - - /// Bytes per value. - pub fn size(self) -> usize { - match self { - Type::Byte | Type::Ascii | Type::SByte | Type::Undefined => 1, - Type::Short | Type::SShort => 2, - Type::Long | Type::SLong | Type::Float | Type::Ifd => 4, - Type::Rational | Type::SRational | Type::Double => 8, - } +fn error(e: craft_tiff::classic::Error) -> Error { + match e { + craft_tiff::classic::Error::NotTiff => Error::NotDng, + craft_tiff::classic::Error::Malformed(s) => Error::Malformed(s), } } -/// One directory entry. -#[derive(Clone, Copy, Debug)] -pub struct Entry { - pub tag: u16, - pub ty: Type, - pub count: u32, - /// Where the value bytes start: inside the entry when they fit in four bytes, else the offset - /// the entry holds (not yet checked against the file length). - value_at: u64, -} - -/// One image file directory. -#[derive(Clone, Debug, Default)] -pub struct Ifd { - /// File offset of the directory. - pub offset: u64, - pub entries: Vec, - /// Nesting: 0 for the main chain, 1 for its SubIFDs, … - pub depth: usize, -} - -impl Ifd { - pub fn get(&self, tag: u16) -> Option<&Entry> { - self.entries.iter().find(|e| e.tag == tag) - } -} - -/// A TIFF file in memory. +/// A classic TIFF file in memory. #[derive(Clone, Copy)] pub struct Tiff<'a> { pub data: &'a [u8], pub big_endian: bool, } - impl<'a> Tiff<'a> { - /// Read the header. Classic TIFF only (BigTIFF, version 43, is not used by DNG). - pub fn new(data: &'a [u8]) -> Result<(Tiff<'a>, u64)> { - let big_endian = match data.get(0..4) { - Some(b"II*\0") => false, - Some(b"MM\0*") => true, - _ => return Err(Error::NotDng), - }; - let t = Tiff { data, big_endian }; - let first = u64::from(t.u32_at(4)?); - Ok((t, first)) + fn inner(&self) -> craft_tiff::classic::Tiff<'a> { + craft_tiff::classic::Tiff { data: self.data, big_endian: self.big_endian } } - - fn bytes_at(&self, off: u64, len: usize) -> Result<&'a [u8]> { - let start = usize::try_from(off).map_err(|_| Error::Malformed("offset outside the file"))?; - let end = start.checked_add(len).ok_or(Error::Malformed("offset outside the file"))?; - self.data.get(start..end).ok_or(Error::Malformed("offset outside the file")) + /// Read the classic TIFF header and first IFD offset. + pub fn new(data: &'a [u8]) -> Result<(Self, u64)> { + let (t, first) = craft_tiff::classic::Tiff::new(data).map_err(error)?; + Ok((Self { data: t.data, big_endian: t.big_endian }, first)) } - - pub fn u16_of(&self, b: [u8; 2]) -> u16 { - if self.big_endian { u16::from_be_bytes(b) } else { u16::from_le_bytes(b) } + /// Read an endian-aware 16-bit integer. + pub fn u16_of(&self, bytes: [u8; 2]) -> u16 { + self.inner().u16_of(bytes) } - - pub fn u32_of(&self, b: [u8; 4]) -> u32 { - if self.big_endian { u32::from_be_bytes(b) } else { u32::from_le_bytes(b) } + /// Read an endian-aware 32-bit integer. + pub fn u32_of(&self, bytes: [u8; 4]) -> u32 { + self.inner().u32_of(bytes) } - - fn u16_at(&self, off: u64) -> Result { - let b = self.bytes_at(off, 2)?; - Ok(self.u16_of([b[0], b[1]])) - } - - fn u32_at(&self, off: u64) -> Result { - let b = self.bytes_at(off, 4)?; - Ok(self.u32_of([b[0], b[1], b[2], b[3]])) - } - - /// Read the IFD at `off`: its entries and the offset of the next IFD (0 at the end of a chain). + /// Read one IFD and its next offset. pub fn read_ifd(&self, off: u64, depth: usize) -> Result<(Ifd, u64)> { - let n = self.u16_at(off)?; - let mut entries = Vec::with_capacity(usize::from(n)); - for i in 0..u64::from(n) { - let at = off + 2 + i * 12; - let e = self.bytes_at(at, 12)?; - let tag = self.u16_of([e[0], e[1]]); - let code = self.u16_of([e[2], e[3]]); - let count = self.u32_of([e[4], e[5], e[6], e[7]]); - // unknown field types are skipped (TIFF 6.0: readers ignore them) - let Some(ty) = Type::from_code(code) else { continue }; - let len = u64::from(count).saturating_mul(ty.size() as u64); - let value_at = if len <= 4 { at + 8 } else { u64::from(self.u32_of([e[8], e[9], e[10], e[11]])) }; - entries.push(Entry { tag, ty, count, value_at }); - } - let next = u64::from(self.u32_at(off + 2 + u64::from(n) * 12)?); - Ok((Ifd { offset: off, entries, depth }, next)) + self.inner().read_ifd(off, depth).map_err(error) } - - /// Every IFD of the file: the main chain from `first` and, depth first, the SubIFDs (tag 330) of - /// each. Bounded by [`MAX_IFDS`] and [`MAX_DEPTH`]; a directory reached twice ends that branch. + /// Walk the bounded main and SubIFD chains. pub fn all_ifds(&self, first: u64) -> Result> { - let mut out: Vec = Vec::new(); - let mut seen = HashSet::new(); - let mut attempts = 0usize; - let mut sub_refs = 0usize; - let mut stack: Vec<(u64, usize)> = vec![(first, 0)]; - while let Some((off, depth)) = stack.pop() { - let mut next = off; - while next != 0 { - if !seen.insert(next) { - break; - } - attempts += 1; - if attempts > MAX_IFD_ATTEMPTS { - return Err(Error::Malformed("too many IFD offsets")); - } - if out.len() >= MAX_IFDS { - return Err(Error::Malformed("too many IFDs")); - } - let (ifd, after) = match self.read_ifd(next, depth) { - Ok(x) => x, - // a broken directory ends the chain; the first one must be readable - Err(e) if out.is_empty() => return Err(e), - Err(_) => break, - }; - if depth < MAX_DEPTH - && let Some(sub) = ifd.get(tags::SUB_IFDS) - { - sub_refs = sub_refs.saturating_add(usize::try_from(sub.count).unwrap_or(usize::MAX)); - if sub_refs > MAX_SUB_IFD_REFS { - return Err(Error::Malformed("too many SubIFD references")); - } - if let Ok(offs) = self.uints(sub) { - for o in offs.into_iter().rev().filter(|o| *o != 0 && !seen.contains(o)) { - stack.push((o, depth + 1)); - } - } - } - out.push(ifd); - next = after; - } - } - Ok(out) + self.inner().all_ifds(first).map_err(error) } - - /// The value bytes of an entry. - pub fn value_bytes(&self, e: &Entry) -> Result<&'a [u8]> { - let len = (e.count as usize).checked_mul(e.ty.size()).ok_or(Error::Malformed("entry too large"))?; - self.bytes_at(e.value_at, len) + /// Borrow the bytes of an entry's value. + pub fn value_bytes(&self, entry: &Entry) -> Result<&'a [u8]> { + self.inner().value_bytes(entry).map_err(error) } - - /// The `i`-th value of a numeric entry as f64 (rationals divided; a zero denominator is an - /// error). - fn number(&self, e: &Entry, b: &[u8], i: usize) -> Result { - let s = e.ty.size(); - let v = b.get(i * s..i * s + s).ok_or(Error::Malformed("entry too short"))?; - let u16v = |o: usize| self.u16_of([v[o], v[o + 1]]); - let u32v = |o: usize| self.u32_of([v[o], v[o + 1], v[o + 2], v[o + 3]]); - Ok(match e.ty { - Type::Byte | Type::Undefined => f64::from(v[0]), - Type::SByte => f64::from(v[0] as i8), - Type::Short => f64::from(u16v(0)), - Type::SShort => f64::from(u16v(0) as i16), - Type::Long | Type::Ifd => f64::from(u32v(0)), - Type::SLong => f64::from(u32v(0) as i32), - Type::Rational => { - let d = u32v(4); - if d == 0 { - return Err(Error::Malformed("rational with a zero denominator")); - } - f64::from(u32v(0)) / f64::from(d) - } - Type::SRational => { - let d = u32v(4) as i32; - if d == 0 { - return Err(Error::Malformed("rational with a zero denominator")); - } - f64::from(u32v(0) as i32) / f64::from(d) - } - Type::Float => f64::from(f32::from_bits(u32v(0))), - Type::Double => { - let mut a = [0u8; 8]; - a.copy_from_slice(&v[..8]); - if self.big_endian { f64::from_be_bytes(a) } else { f64::from_le_bytes(a) } - } - Type::Ascii => return Err(Error::Malformed("text where a number belongs")), - }) + /// Read numeric values as floats. + pub fn floats(&self, entry: &Entry) -> Result> { + self.inner().floats(entry).map_err(error) } - - /// All values of a numeric entry as f64. Non-finite values are an error. - pub fn floats(&self, e: &Entry) -> Result> { - let b = self.value_bytes(e)?; - let mut out = Vec::with_capacity(e.count as usize); - for i in 0..e.count as usize { - let v = self.number(e, b, i)?; - if !v.is_finite() { - return Err(Error::Malformed("non-finite number")); - } - out.push(v); - } - Ok(out) + /// Read numeric values as unsigned integers. + pub fn uints(&self, entry: &Entry) -> Result> { + self.inner().uints(entry).map_err(error) } - - /// All values of an unsigned integer entry (BYTE, SHORT, LONG, IFD). - pub fn uints(&self, e: &Entry) -> Result> { - if !matches!(e.ty, Type::Byte | Type::Undefined | Type::Short | Type::Long | Type::Ifd) { - return Err(Error::Malformed("expected an unsigned integer field")); - } - let b = self.value_bytes(e)?; - let s = e.ty.size(); - Ok(b.chunks_exact(s) - .map(|v| match s { - 1 => u64::from(v[0]), - 2 => u64::from(self.u16_of([v[0], v[1]])), - _ => u64::from(self.u32_of([v[0], v[1], v[2], v[3]])), - }) - .collect()) + /// Read the first unsigned integer. + pub fn uint(&self, entry: &Entry) -> Result { + self.inner().uint(entry).map_err(error) } - - /// The first value of an unsigned integer entry. - pub fn uint(&self, e: &Entry) -> Result { - self.uints(e)?.first().copied().ok_or(Error::Malformed("empty field")) + /// Read an ASCII value. + pub fn ascii(&self, entry: &Entry) -> Result { + self.inner().ascii(entry).map_err(error) } - - /// An ASCII entry up to its first NUL, lossily decoded. - pub fn ascii(&self, e: &Entry) -> Result { - let b = self.value_bytes(e)?; - let end = b.iter().position(|&c| c == 0).unwrap_or(b.len()); - Ok(String::from_utf8_lossy(&b[..end]).trim().to_string()) - } -} - -/// Tag numbers (TIFF 6.0, TIFF/EP, DNG 1.7, CinemaDNG). -pub mod tags { - pub const NEW_SUBFILE_TYPE: u16 = 254; - pub const IMAGE_WIDTH: u16 = 256; - pub const IMAGE_LENGTH: u16 = 257; - pub const BITS_PER_SAMPLE: u16 = 258; - pub const COMPRESSION: u16 = 259; - pub const PHOTOMETRIC: u16 = 262; - pub const MAKE: u16 = 271; - pub const MODEL: u16 = 272; - pub const STRIP_OFFSETS: u16 = 273; - pub const SAMPLES_PER_PIXEL: u16 = 277; - pub const ROWS_PER_STRIP: u16 = 278; - pub const STRIP_BYTE_COUNTS: u16 = 279; - pub const PLANAR_CONFIGURATION: u16 = 284; - pub const PREDICTOR: u16 = 317; - pub const TILE_WIDTH: u16 = 322; - pub const TILE_LENGTH: u16 = 323; - pub const TILE_OFFSETS: u16 = 324; - pub const TILE_BYTE_COUNTS: u16 = 325; - pub const SUB_IFDS: u16 = 330; - pub const SAMPLE_FORMAT: u16 = 339; - pub const CFA_REPEAT_PATTERN_DIM: u16 = 33421; - pub const CFA_PATTERN: u16 = 33422; - pub const DNG_VERSION: u16 = 50706; - pub const UNIQUE_CAMERA_MODEL: u16 = 50708; - pub const CFA_PLANE_COLOR: u16 = 50710; - pub const CFA_LAYOUT: u16 = 50711; - pub const LINEARIZATION_TABLE: u16 = 50712; - pub const BLACK_LEVEL_REPEAT_DIM: u16 = 50713; - pub const BLACK_LEVEL: u16 = 50714; - pub const BLACK_LEVEL_DELTA_H: u16 = 50715; - pub const BLACK_LEVEL_DELTA_V: u16 = 50716; - pub const WHITE_LEVEL: u16 = 50717; - pub const DEFAULT_CROP_ORIGIN: u16 = 50719; - pub const DEFAULT_CROP_SIZE: u16 = 50720; - pub const COLOR_MATRIX_1: u16 = 50721; - pub const COLOR_MATRIX_2: u16 = 50722; - pub const CAMERA_CALIBRATION_1: u16 = 50723; - pub const CAMERA_CALIBRATION_2: u16 = 50724; - pub const ANALOG_BALANCE: u16 = 50727; - pub const AS_SHOT_NEUTRAL: u16 = 50728; - pub const AS_SHOT_WHITE_XY: u16 = 50729; - pub const BASELINE_EXPOSURE: u16 = 50730; - pub const CALIBRATION_ILLUMINANT_1: u16 = 50778; - pub const CALIBRATION_ILLUMINANT_2: u16 = 50779; - pub const ACTIVE_AREA: u16 = 50829; - pub const OPCODE_LIST_1: u16 = 51008; - pub const OPCODE_LIST_2: u16 = 51009; - pub const OPCODE_LIST_3: u16 = 51022; - pub const FORWARD_MATRIX_1: u16 = 50964; - pub const FORWARD_MATRIX_2: u16 = 50965; - /// CinemaDNG: SMPTE ST 12-1 time codes, 8 bytes each. - pub const TIME_CODES: u16 = 51043; - /// CinemaDNG: frames per second (SRATIONAL). - pub const FRAME_RATE: u16 = 51044; - /// CinemaDNG: the camera's reel name. - pub const REEL_NAME: u16 = 51081; } #[cfg(test)] mod tests { use super::*; - - /// A little-endian TIFF: a chain of `ifds` directories, each with one SubIFDs entry of - /// `count` LONGs, all naming the same table (of zeros, or of `target`). - fn chain(ifds: usize, count: u32, target: u32) -> Vec { - let mut b = b"II*\0".to_vec(); - b.extend_from_slice(&8u32.to_le_bytes()); - let table = 8 + ifds * 18; - for i in 0..ifds { - let next = if i + 1 < ifds { 8 + (i + 1) * 18 } else { 0 }; - b.extend_from_slice(&1u16.to_le_bytes()); - b.extend_from_slice(&tags::SUB_IFDS.to_le_bytes()); - b.extend_from_slice(&4u16.to_le_bytes()); - b.extend_from_slice(&count.to_le_bytes()); - b.extend_from_slice(&(table as u32).to_le_bytes()); - b.extend_from_slice(&(next as u32).to_le_bytes()); - } - for _ in 0..count.max(2) { - b.extend_from_slice(&target.to_le_bytes()); - } - b - } - #[test] - fn shared_sub_ifd_tables_are_budgeted_before_they_are_queued() { - // 60 directories each naming one 100 000-entry table: 6 million queued edges before - let b = chain(60, 100_000, 0); - let (t, first) = Tiff::new(&b).unwrap(); - assert!(matches!(t.all_ifds(first), Err(Error::Malformed("too many SubIFD references")))); - } - - #[test] - fn ordinary_sub_ifds_are_still_walked() { - // one directory whose SubIFD is the same directory: visited once - let b = chain(1, 1, 8); - let (t, first) = Tiff::new(&b).unwrap(); - assert_eq!(t.all_ifds(first).unwrap().len(), 1); - // a short chain with a few zero SubIFD offsets - let b = chain(3, 4, 0); - let (t, first) = Tiff::new(&b).unwrap(); - assert_eq!(t.all_ifds(first).unwrap().len(), 3); + fn malformed_header_preserves_filmcraft_error() { + assert!(matches!(Tiff::new(b"bad"), Err(Error::NotDng))); } } From 2d4cdcb22b42b0e5dc15c3966b75d04c8129494b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:41:43 +0000 Subject: [PATCH 11/24] codecs: fix shared Y4M layout test type --- crates/codecs/src/y4m.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/codecs/src/y4m.rs b/crates/codecs/src/y4m.rs index 7f6d0355b..eae5c7539 100644 --- a/crates/codecs/src/y4m.rs +++ b/crates/codecs/src/y4m.rs @@ -183,7 +183,7 @@ mod tests { #[test] fn colour_space_names() { - let l = |c: Option, bits, alpha| Some(craft_y4m::Layout { chroma: c, bits, alpha }); + let l = |c: Option, bits, alpha| Some(craft_y4m::Layout { chroma: c, bits, alpha }); assert_eq!(craft_y4m::Layout::parse("420jpeg"), l(Some(craft_y4m::Chroma::C420), 8, false)); assert_eq!(craft_y4m::Layout::parse("420p10"), l(Some(craft_y4m::Chroma::C420), 10, false)); assert_eq!(craft_y4m::Layout::parse("422p12"), l(Some(craft_y4m::Chroma::C422), 12, false)); From 1f44c9118c85dfe61c3e76fdf18ebf1cc41720ea Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:18:14 +0000 Subject: [PATCH 12/24] export: pin merged shared EXR deflate --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73344da80..c34b6033e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ab37b756cc86dfc552eeb351b0b407e546ec4d9e#ab37b756cc86dfc552eeb351b0b407e546ec4d9e" +source = "git+https://github.com/storytold/craft-libs?rev=1fdc42e5d68ef38799354bb906c2a45c5df25439#1fdc42e5d68ef38799354bb906c2a45c5df25439" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 8f74709c4..fe13c84d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ab37b756cc86dfc552eeb351b0b407e546ec4d9e" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1fdc42e5d68ef38799354bb906c2a45c5df25439" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From c2bd6036dd9c595a2fce4f2d92bfec9f340b39dd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:19:28 +0000 Subject: [PATCH 13/24] export: update shared EXR writer lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b0bab0f26..dd81bfd5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=4c17e51335192345d8e508287c26f17fead930ac#4c17e51335192345d8e508287c26f17fead930ac" +source = "git+https://github.com/storytold/craft-libs?rev=7a9b1c7b2865f50baeeca8eed951c071c80e818b#7a9b1c7b2865f50baeeca8eed951c071c80e818b" dependencies = [ "serde_json", ] From 7335542e67791d06aec9c6406913e6e7731d7696 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:34:53 +0000 Subject: [PATCH 14/24] Update shared PCM revision --- Cargo.lock | 10 +++++----- Cargo.toml | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf45f8c36..0138138d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1061,10 +1061,10 @@ dependencies = [ [[package]] name = "craft-audio" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" dependencies = [ "craft-media", - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d)", "log", "rayon", "symphonia 0.6.1", @@ -1130,7 +1130,7 @@ dependencies = [ [[package]] name = "craft-media" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" dependencies = [ "chrono", "thiserror 2.0.21", @@ -1195,7 +1195,7 @@ dependencies = [ [[package]] name = "craft-video" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" dependencies = [ "craft-media", "rayon", @@ -1234,7 +1234,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0#64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0" +source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" dependencies = [ "quick-xml 0.41.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index 24d19f47e..955391e82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,10 +59,10 @@ craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both # 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 = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", default-features = false } -craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", default-features = false, features = ["aac-decode", "aac-encode", "rayon", "ac3-decode", "mpa-decode", "mp3-decode", "decode-native", "symphonia", "opus-decode", "flac-encode", "mp3-encode"] } +craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a28cb617653255f5fe46624320b606bf9f692a8d", default-features = false } +craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a28cb617653255f5fe46624320b606bf9f692a8d", 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, ProRes and decoders), on the same commit as craft-media. -craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "64b7a7f21e5afa9be81966a6d6a21f6deb1f98c0", features = ["h264-decode", "h264-encode", "hevc-decode", "vp9-decode", "av1-decode", "vp8-decode", "mpeg2", "apv", "prores", "threads"] } +craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a28cb617653255f5fe46624320b606bf9f692a8d", 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" } From 793956a957660642a5253ed449868808cd6d3aa7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:36:52 +0000 Subject: [PATCH 15/24] Pin merged shared lossless JPEG --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9bd75d2d5..15010f74b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1138,7 +1138,7 @@ dependencies = [ [[package]] name = "craft-raw" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +source = "git+https://github.com/storytold/craft-libs?rev=7738cc6c689060475cb6947a6e3db6ed78fb3419#7738cc6c689060475cb6947a6e3db6ed78fb3419" dependencies = [ "craft-tiff", "rayon", @@ -1149,7 +1149,7 @@ dependencies = [ [[package]] name = "craft-tiff" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +source = "git+https://github.com/storytold/craft-libs?rev=7738cc6c689060475cb6947a6e3db6ed78fb3419#7738cc6c689060475cb6947a6e3db6ed78fb3419" dependencies = [ "serde", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index e052416f8..304b43368 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ rayon = "1" proptest = "1" roxmltree = "0.21" # Shared lossless JPEG decoding for DNG. -craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d16ee7926c8403ded5ea3209bc2ab14b1570281a", default-features = false } +craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7738cc6c689060475cb6947a6e3db6ed78fb3419", default-features = false } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From dcb85a09d9428f574f5df77603b7cb2940511386 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:52:18 +0000 Subject: [PATCH 16/24] Refresh TIFF dependency lockfile --- Cargo.lock | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index c21528484..456be08c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1140,7 +1140,7 @@ name = "craft-raw" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=7738cc6c689060475cb6947a6e3db6ed78fb3419#7738cc6c689060475cb6947a6e3db6ed78fb3419" dependencies = [ - "craft-tiff", + "craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7738cc6c689060475cb6947a6e3db6ed78fb3419)", "rayon", "serde", "thiserror 2.0.21", @@ -1155,6 +1155,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e79d9a8d5ff79d3e31c12748ecd45e90ee9d3168#e79d9a8d5ff79d3e31c12748ecd45e90ee9d3168" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-timeline" version = "0.1.0" @@ -2120,6 +2129,7 @@ name = "filmcraft-dng" version = "0.20.1" dependencies = [ "craft-raw", + "craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e79d9a8d5ff79d3e31c12748ecd45e90ee9d3168)", "filmcraft-dng", "image", "rayon", From ec87972c47799ca7393c7e357b1a1835a2fd7e67 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:52:46 +0000 Subject: [PATCH 17/24] Update shared PCM after main merge --- Cargo.lock | 10 +++++----- Cargo.toml | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0138138d9..1082378d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1061,10 +1061,10 @@ dependencies = [ [[package]] name = "craft-audio" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" +source = "git+https://github.com/storytold/craft-libs?rev=5dcddf5bf09c5250f95c89e8197c695f158b88f5#5dcddf5bf09c5250f95c89e8197c695f158b88f5" dependencies = [ "craft-media", - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5dcddf5bf09c5250f95c89e8197c695f158b88f5)", "log", "rayon", "symphonia 0.6.1", @@ -1130,7 +1130,7 @@ dependencies = [ [[package]] name = "craft-media" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" +source = "git+https://github.com/storytold/craft-libs?rev=5dcddf5bf09c5250f95c89e8197c695f158b88f5#5dcddf5bf09c5250f95c89e8197c695f158b88f5" dependencies = [ "chrono", "thiserror 2.0.21", @@ -1195,7 +1195,7 @@ dependencies = [ [[package]] name = "craft-video" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" +source = "git+https://github.com/storytold/craft-libs?rev=5dcddf5bf09c5250f95c89e8197c695f158b88f5#5dcddf5bf09c5250f95c89e8197c695f158b88f5" dependencies = [ "craft-media", "rayon", @@ -1234,7 +1234,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a28cb617653255f5fe46624320b606bf9f692a8d#a28cb617653255f5fe46624320b606bf9f692a8d" +source = "git+https://github.com/storytold/craft-libs?rev=5dcddf5bf09c5250f95c89e8197c695f158b88f5#5dcddf5bf09c5250f95c89e8197c695f158b88f5" dependencies = [ "quick-xml 0.41.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index 955391e82..e041f558a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,10 +59,10 @@ craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both # 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 = "a28cb617653255f5fe46624320b606bf9f692a8d", default-features = false } -craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a28cb617653255f5fe46624320b606bf9f692a8d", default-features = false, features = ["aac-decode", "aac-encode", "rayon", "ac3-decode", "mpa-decode", "mp3-decode", "decode-native", "symphonia", "opus-decode", "flac-encode", "mp3-encode"] } +craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5dcddf5bf09c5250f95c89e8197c695f158b88f5", default-features = false } +craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5dcddf5bf09c5250f95c89e8197c695f158b88f5", 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, ProRes and decoders), on the same commit as craft-media. -craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a28cb617653255f5fe46624320b606bf9f692a8d", features = ["h264-decode", "h264-encode", "hevc-decode", "vp9-decode", "av1-decode", "vp8-decode", "mpeg2", "apv", "prores", "threads"] } +craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5dcddf5bf09c5250f95c89e8197c695f158b88f5", 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" } From 6dd59ccaa766824be5280eb58495cc8620de94f9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:09:50 +0000 Subject: [PATCH 18/24] export: use shared OpenEXR ZIP compression --- Cargo.lock | 10 + Cargo.toml | 2 + crates/export/Cargo.toml | 2 + crates/export/src/deflate.rs | 795 ----------------------------------- crates/export/src/lib.rs | 1 - crates/export/src/raster.rs | 11 +- 6 files changed, 22 insertions(+), 799 deletions(-) delete mode 100644 crates/export/src/deflate.rs diff --git a/Cargo.lock b/Cargo.lock index b4b4bb9c2..c9bf41019 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1090,6 +1090,14 @@ name = "craft-docpath" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +[[package]] +name = "craft-exr" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f31e878c6e923748aebae75869e9dd88874d05d3#f31e878c6e923748aebae75869e9dd88874d05d3" +dependencies = [ + "serde_json", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -2173,6 +2181,7 @@ name = "filmcraft-export" version = "0.20.1" dependencies = [ "craft-audio", + "craft-exr", "craft-media", "craft-video", "filmcraft-audio-dsp", @@ -2188,6 +2197,7 @@ dependencies = [ "filmcraft-testkit", "filmcraft-text", "filmcraft-time", + "flate2", "image", "log", "rayon", diff --git a/Cargo.toml b/Cargo.toml index d9421344d..8f51d6ad9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,8 @@ proptest = "1" roxmltree = "0.21" # Shared YUV4MPEG2 parser and frame index. craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } +# Shared OpenEXR ZIP chunk compressor. +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f31e878c6e923748aebae75869e9dd88874d05d3" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index fa39b0b4e..f05cf310c 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] craft-audio = { workspace = true } +craft-exr = { workspace = true } craft-video = { workspace = true } filmcraft-time = { workspace = true } filmcraft-frame = { workspace = true } @@ -34,4 +35,5 @@ web-time = "1" image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "tiff", "bmp"] } [dev-dependencies] +flate2 = { version = "1", default-features = false, features = ["rust_backend"] } filmcraft-testkit = { workspace = true } diff --git a/crates/export/src/deflate.rs b/crates/export/src/deflate.rs deleted file mode 100644 index 4381c4ebe..000000000 --- a/crates/export/src/deflate.rs +++ /dev/null @@ -1,795 +0,0 @@ -//! A small DEFLATE encoder (RFC 1951) in a zlib wrapper (RFC 1950), written from the RFCs for the -//! ZIP-compressed OpenEXR chunks. -//! -//! Greedy LZ77 over a 32 KB window (hash chains of bounded length), then each block of tokens is -//! written as whichever of stored, fixed-Huffman or dynamic-Huffman is smallest. Huffman code -//! lengths are limited (15 bits, 7 for the code-length code) by halving the symbol frequencies -//! until the tree fits. Every code has at least two symbols, so all codes are complete. The -//! output favours simplicity and correctness over ratio; worst-case expansion is the stored-block -//! overhead (5 bytes per 64 KB). - -use std::cmp::Reverse; -use std::collections::BinaryHeap; - -const WINDOW: usize = 32 * 1024; -const MIN_MATCH: usize = 3; -const MAX_MATCH: usize = 258; -/// Hash chain candidates tried per position. -const MAX_CHAIN: usize = 48; -/// Tokens per block (a new set of Huffman codes after this many). -const BLOCK_TOKENS: usize = 1 << 15; -const MAX_STORED: usize = 65_535; - -const LEN_BASE: [u16; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]; -const LEN_EXTRA: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]; -const DIST_BASE: [u16; 30] = - [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]; -const DIST_EXTRA: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]; -/// Order in which the code-length code's lengths are sent. -const CL_ORDER: [usize; 19] = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; - -/// `data` compressed as a zlib stream (CMF 0x78: deflate, 32 KB window). `tables` are the match -/// finder's hash tables, kept by the caller across the chunks of a picture; the output does not -/// depend on what they held before. -pub(crate) fn zlib(data: &[u8], tables: &mut Tables) -> Vec { - let mut w = Bits { out: Vec::with_capacity(data.len() / 2 + 64), acc: 0, n: 0 }; - w.out.extend_from_slice(&[0x78, 0x9C]); // (0x78 << 8 | 0x9C) % 31 == 0 - deflate(data, &mut w, tables); - w.align(); - w.out.extend_from_slice(&adler32(data).to_be_bytes()); - w.out -} - -/// Adler-32 (RFC 1950 §9); 5552 bytes is the longest run that cannot overflow the sums. -fn adler32(data: &[u8]) -> u32 { - let (mut a, mut b) = (1_u32, 0_u32); - for c in data.chunks(5552) { - for &x in c { - a += u32::from(x); - b += a; - } - a %= 65_521; - b %= 65_521; - } - (b << 16) | a -} - -/// LSB-first bit writer. -struct Bits { - out: Vec, - acc: u64, - n: u32, -} - -impl Bits { - /// The low `n` bits of `v` (n ≤ 32). - fn put(&mut self, v: u32, n: u32) { - let mask = (1_u64 << n) - 1; - self.acc |= (u64::from(v) & mask) << self.n; - self.n += n; - while self.n >= 8 { - self.out.push(self.acc as u8); - self.acc >>= 8; - self.n -= 8; - } - } - - fn align(&mut self) { - if self.n > 0 { - self.out.push(self.acc as u8); - } - self.acc = 0; - self.n = 0; - } -} - -#[derive(Clone, Copy)] -enum Tok { - Lit(u8), - Match { len: u16, dist: u16 }, -} - -/// Index into [`LEN_BASE`] of a match length (3..=258). -fn len_code(len: u16) -> usize { - LEN_BASE.partition_point(|&b| b <= len).saturating_sub(1) -} - -/// Index into [`DIST_BASE`] of a distance (1..=32768). -fn dist_code(dist: u16) -> usize { - DIST_BASE.partition_point(|&b| b <= dist).saturating_sub(1) -} - -/// The match finder's hash heads and chains, reused from one input to the next. -/// -/// A slot holds `base + p + 1` for position `p` of the input that wrote it; anything at or below -/// the current input's `base` was left by an earlier input and reads as empty. So the tables are -/// never cleared between inputs: an OpenEXR ZIPS frame is one input per scan line, and zeroing -/// 256 KiB of tables for each 23 KB UHD line cost more memory traffic than the data itself. -#[derive(Default)] -pub(crate) struct Tables { - head: Vec, - prev: Vec, - /// Bytes compressed through these tables since they were last cleared. - base: u32, -} - -/// LZ77 match finder: hash heads and chains over the window. -struct Matcher<'a> { - data: &'a [u8], - head: &'a mut [u32], - prev: &'a mut [u32], - bits: u32, - /// `prev` slots used by this input, minus one (a power of two minus one). - mask: usize, - base: u32, -} - -impl<'a> Matcher<'a> { - /// A matcher over `data` (shorter than `u32::MAX` bytes), or None when it is not. - fn new(data: &'a [u8], t: &'a mut Tables) -> Option { - // hash width and chain span follow the input's size, as they did when every input had - // tables of its own (the output depends on them), so short chunks stay cheap to hash - let span = data.len().clamp(256, WINDOW).next_power_of_two(); - let bits = span.trailing_zeros().clamp(8, 15); - let n = u32::try_from(data.len()).ok().filter(|&n| n < u32::MAX)?; - if t.base.checked_add(n).and_then(|b| b.checked_add(1)).is_none() { - // positions would no longer fit after `base`: start over from empty tables - t.head.fill(0); - t.prev.fill(0); - t.base = 0; - } - // grown slots are 0, which is never above `base`, so they read as empty - if t.head.len() < 1 << bits { - t.head.resize(1 << bits, 0); - } - if t.prev.len() < span { - t.prev.resize(span, 0); - } - let base = t.base; - t.base += n; - let (head, prev) = (t.head.get_mut(..1 << bits)?, t.prev.get_mut(..span)?); - Some(Matcher { data, head, prev, bits, mask: span - 1, base }) - } - - /// Position `p` of this input from a stored slot, or None for an empty or earlier input's slot. - fn pos(&self, slot: u32) -> Option { - slot.checked_sub(self.base)?.checked_sub(1).map(|p| p as usize) - } - - fn hash(&self, p: usize) -> Option { - let b = self.data.get(p..p + MIN_MATCH)?; - let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); - Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize) - } - - /// Record position `p` (stored as `p + 1`; 0 = empty). - fn insert(&mut self, p: usize) { - let Some(h) = self.hash(p) else { return }; - let (Some(head), Ok(p32)) = (self.head.get(h).copied(), u32::try_from(p)) else { return }; - if let Some(slot) = self.prev.get_mut(p & self.mask) { - *slot = head; - } - if let Some(slot) = self.head.get_mut(h) { - // `new` reserved `base + len + 1` within u32 - *slot = self.base + p32 + 1; - } - } - - /// The longest earlier match at `p`: (length, distance), length 0 if none. - fn longest(&self, p: usize) -> (usize, usize) { - let Some(h) = self.hash(p) else { return (0, 0) }; - let max = self.data.len().saturating_sub(p).min(MAX_MATCH); - let Some(here) = self.data.get(p..p + max) else { return (0, 0) }; - let (mut best, mut best_dist) = (0, 0); - let mut cand = self.head.get(h).and_then(|&s| self.pos(s)); - for _ in 0..MAX_CHAIN { - let Some(c) = cand else { break }; - if c >= p || p - c > WINDOW { - break; - } - // a candidate that differs at `best` cannot be longer than `best`: skip comparing it - if best == 0 || self.data.get(c + best) == here.get(best) { - let l = self.data.get(c..).unwrap_or_default().iter().zip(here).take_while(|(a, b)| a == b).count(); - if l > best { - (best, best_dist) = (l, p - c); - if l == max { - break; - } - } - } - let Some(next) = self.prev.get(c & self.mask).and_then(|&s| self.pos(s)) else { break }; - // chains only go back; anything else is a stale slot - if next >= c { - break; - } - cand = Some(next); - } - (best, best_dist) - } -} - -/// The deflate blocks of `data`, the last one final. -fn deflate(data: &[u8], w: &mut Bits, tables: &mut Tables) { - if data.is_empty() { - // a final fixed-Huffman block holding only end-of-block (code 0000000) - w.put(1, 1); - w.put(1, 2); - w.put(0, 7); - return; - } - // positions are kept in u32; longer inputs (never an OpenEXR chunk) go out stored - let mut m = Matcher::new(data, tables); - let mut toks = Vec::with_capacity(BLOCK_TOKENS.min(data.len()) + 1); - let (mut start, mut i) = (0, 0); - while i < data.len() { - let (len, dist) = m.as_ref().map_or((0, 0), |m| m.longest(i)); - let step = if len >= MIN_MATCH { - toks.push(Tok::Match { len: len as u16, dist: dist as u16 }); - len - } else { - toks.push(Tok::Lit(data[i])); - 1 - }; - if let Some(m) = m.as_mut() { - for p in i..i + step { - m.insert(p); - } - } - i += step; - if toks.len() >= BLOCK_TOKENS || i == data.len() { - block(w, &toks, &data[start..i], i == data.len()); - toks.clear(); - start = i; - } - } -} - -/// Bits the tokens take with the given code lengths (EOB included). -fn data_bits(lf: &[u32], df: &[u32], ll: &[u8], dl: &[u8]) -> u64 { - let mut n = 0_u64; - for (s, (&f, &l)) in lf.iter().zip(ll).enumerate() { - let extra = s.checked_sub(257).and_then(|k| LEN_EXTRA.get(k)).copied().unwrap_or(0); - n += u64::from(f) * u64::from(l + extra); - } - for (d, (&f, &l)) in df.iter().zip(dl).enumerate() { - n += u64::from(f) * u64::from(l + DIST_EXTRA.get(d).copied().unwrap_or(0)); - } - n -} - -/// One block (split into several stored blocks when stored is smallest and `raw` is long). -fn block(w: &mut Bits, toks: &[Tok], raw: &[u8], last: bool) { - let mut lf = [0_u32; 286]; - let mut df = [0_u32; 30]; - for t in toks { - match *t { - Tok::Lit(b) => lf[b as usize] += 1, - Tok::Match { len, dist } => { - lf[257 + len_code(len)] += 1; - df[dist_code(dist)] += 1; - } - } - } - lf[256] += 1; - - // dynamic codes and their header - let ll = lengths(&lf, 15); - let dl = lengths(&df, 15); - let hlit = ll.iter().rposition(|&l| l > 0).map_or(257, |p| (p + 1).max(257)); - let hdist = dl.iter().rposition(|&l| l > 0).map_or(1, |p| p + 1); - let seq = rle_lengths(&[&ll[..hlit], &dl[..hdist]].concat()); - let mut cf = [0_u32; 19]; - for &(s, _) in &seq { - cf[s as usize] += 1; - } - let cl = lengths(&cf, 7); - let hclen = CL_ORDER.iter().rposition(|&s| cl[s] > 0).map_or(4, |p| (p + 1).max(4)); - let seq_bits: u64 = seq.iter().map(|&(s, _)| u64::from(cl[s as usize]) + [2_u64, 3, 7].get((s as usize).wrapping_sub(16)).copied().unwrap_or(0)).sum(); - let dyn_bits = 3 + 14 + 3 * hclen as u64 + seq_bits + data_bits(&lf, &df, &ll, &dl); - - let (fll, fdl) = fixed_lengths(); - let fixed_bits = 3 + data_bits(&lf, &df, &fll, &fdl); - // per stored piece: header, at most 7 padding bits, LEN and NLEN - let pieces = raw.len().div_ceil(MAX_STORED).max(1) as u64; - let stored_bits = pieces * (3 + 7 + 32) + 8 * raw.len() as u64; - - if stored_bits <= fixed_bits.min(dyn_bits) { - let n = raw.chunks(MAX_STORED).count(); - for (k, piece) in raw.chunks(MAX_STORED).enumerate() { - w.put(u32::from(last && k + 1 == n), 1); - w.put(0, 2); - w.align(); - let len = piece.len() as u16; - w.out.extend_from_slice(&len.to_le_bytes()); - w.out.extend_from_slice(&(!len).to_le_bytes()); - w.out.extend_from_slice(piece); - } - return; - } - w.put(u32::from(last), 1); - if fixed_bits <= dyn_bits { - w.put(1, 2); - write_tokens(w, toks, &fll, &fdl); - return; - } - w.put(2, 2); - w.put((hlit - 257) as u32, 5); - w.put((hdist - 1) as u32, 5); - w.put((hclen - 4) as u32, 4); - for &s in &CL_ORDER[..hclen] { - w.put(u32::from(cl[s]), 3); - } - let cc = codes(&cl); - for &(s, x) in &seq { - w.put(u32::from(cc[s as usize]), u32::from(cl[s as usize])); - match s { - 16 => w.put(u32::from(x), 2), - 17 => w.put(u32::from(x), 3), - 18 => w.put(u32::from(x), 7), - _ => {} - } - } - write_tokens(w, toks, &ll, &dl); -} - -/// The fixed Huffman code lengths (RFC 1951 §3.2.6). -fn fixed_lengths() -> ([u8; 288], [u8; 30]) { - let mut ll = [8_u8; 288]; - ll[144..256].fill(9); - ll[256..280].fill(7); - (ll, [5; 30]) -} - -fn write_tokens(w: &mut Bits, toks: &[Tok], ll: &[u8], dl: &[u8]) { - let lc = codes(ll); - let dc = codes(dl); - let sym = |w: &mut Bits, s: usize| w.put(u32::from(lc[s]), u32::from(ll[s])); - for t in toks { - match *t { - Tok::Lit(b) => sym(w, b as usize), - Tok::Match { len, dist } => { - let k = len_code(len); - sym(w, 257 + k); - w.put(u32::from(len - LEN_BASE[k]), u32::from(LEN_EXTRA[k])); - let d = dist_code(dist); - w.put(u32::from(dc[d]), u32::from(dl[d])); - w.put(u32::from(dist - DIST_BASE[d]), u32::from(DIST_EXTRA[d])); - } - } - } - sym(w, 256); -} - -/// Canonical Huffman codes for `lens`, bit-reversed for the LSB-first writer. -fn codes(lens: &[u8]) -> Vec { - let mut count = [0_u32; 16]; - for &l in lens { - if (1..16).contains(&l) { - count[l as usize] += 1; - } - } - let mut next = [0_u32; 16]; - let mut code = 0; - for bits in 1..16 { - code = (code + count[bits - 1]) << 1; - next[bits] = code; - } - lens.iter() - .map(|&l| { - if !(1..16).contains(&l) { - return 0; - } - let c = next[l as usize]; - next[l as usize] += 1; - (c.reverse_bits() >> (32 - u32::from(l))) as u16 - }) - .collect() -} - -/// Code lengths for `freq`, none longer than `limit`; at least two symbols always get a code. -fn lengths(freq: &[u32], limit: u8) -> Vec { - let mut f: Vec = freq.iter().map(|&x| u64::from(x)).collect(); - let mut used = f.iter().filter(|&&x| x > 0).count(); - for x in f.iter_mut() { - if used >= 2 { - break; - } - if *x == 0 { - *x = 1; - used += 1; - } - } - // halving converges: once every used frequency is 1 the tree is balanced (depth ≤ 9 here) - for _ in 0..66 { - let l = huffman(&f); - if l.iter().all(|&d| d <= limit) { - return l; - } - for x in f.iter_mut().filter(|x| **x > 0) { - *x = (*x >> 1).max(1); - } - } - huffman(&f) -} - -/// Huffman tree depths of the symbols with non-zero frequency (0 for the rest). -fn huffman(f: &[u64]) -> Vec { - let n = f.len(); - let mut heap: BinaryHeap> = f.iter().enumerate().filter(|(_, w)| **w > 0).map(|(i, &w)| Reverse((w, i))).collect(); - let mut parent = vec![usize::MAX; 2 * n]; - let mut next = n; - while heap.len() > 1 { - let (Some(Reverse((wa, a))), Some(Reverse((wb, b)))) = (heap.pop(), heap.pop()) else { break }; - parent[a] = next; - parent[b] = next; - heap.push(Reverse((wa + wb, next))); - next += 1; - } - // parents always come after their children, so walk down from the root - let mut depth = vec![0_u32; next]; - for i in (0..next).rev() { - if let Some(&p) = parent.get(i).filter(|&&p| p < next) { - depth[i] = depth[p].saturating_add(1); - } - } - depth[..n].iter().map(|&d| d.min(u32::from(u8::MAX)) as u8).collect() -} - -/// Code lengths run-length coded with the code-length alphabet: (symbol, extra-bits value). -fn rle_lengths(lens: &[u8]) -> Vec<(u8, u8)> { - let mut out = Vec::new(); - let mut i = 0; - while i < lens.len() { - let l = lens[i]; - let run = lens[i..].iter().take_while(|&&x| x == l).count(); - let mut r = run; - if l == 0 { - while r >= 11 { - let k = r.min(138); - out.push((18, (k - 11) as u8)); - r -= k; - } - if r >= 3 { - out.push((17, (r - 3) as u8)); - r = 0; - } - } else { - out.push((l, 0)); - r -= 1; - while r >= 3 { - let k = r.min(6); - out.push((16, (k - 3) as u8)); - r -= k; - } - } - out.extend(std::iter::repeat_n((l, 0), r)); - i += run; - } - out -} - -#[cfg(test)] -pub(crate) mod tests { - use super::*; - - /// A minimal inflater (RFC 1951), only to check the encoder; returns None on any error. - pub(crate) fn inflate(z: &[u8]) -> Option> { - let (cmf, flg) = (*z.first()?, *z.get(1)?); - if cmf & 0x0F != 8 || (u16::from(cmf) << 8 | u16::from(flg)) % 31 != 0 || flg & 0x20 != 0 { - return None; - } - let mut pos = 16_usize; // bit position - let bit = |pos: &mut usize| -> Option { - let b = (*z.get(*pos / 8)? >> (*pos % 8)) & 1; - *pos += 1; - Some(u32::from(b)) - }; - let bits = |pos: &mut usize, n: u32| -> Option { - let mut v = 0; - for k in 0..n { - v |= bit(pos)? << k; - } - Some(v) - }; - // decode one symbol with canonical lengths (slow but simple) - let decode = |pos: &mut usize, lens: &[u8]| -> Option { - let (mut code, mut first, mut index) = (0_i32, 0_i32, 0_i32); - let mut count = [0_i32; 16]; - for &l in lens { - count[l as usize] += 1; - } - let mut sorted: Vec = (0..lens.len()).filter(|&s| lens[s] > 0).collect(); - sorted.sort_by_key(|&s| (lens[s], s)); - for len in 1..16 { - code |= bit(pos)? as i32; - let c = count[len]; - if code - c < first { - return sorted.get((index + code - first) as usize).copied(); - } - index += c; - first = (first + c) << 1; - code <<= 1; - } - None - }; - let mut out = Vec::new(); - loop { - let last = bit(&mut pos)?; - match bits(&mut pos, 2)? { - 0 => { - pos = pos.div_ceil(8) * 8; - let at = pos / 8; - let len = u16::from_le_bytes([*z.get(at)?, *z.get(at + 1)?]); - let nlen = u16::from_le_bytes([*z.get(at + 2)?, *z.get(at + 3)?]); - if len != !nlen { - return None; - } - out.extend_from_slice(z.get(at + 4..at + 4 + len as usize)?); - pos = (at + 4 + len as usize) * 8; - } - t @ (1 | 2) => { - let (ll, dl) = if t == 1 { - let (a, b) = fixed_lengths(); - (a.to_vec(), b.to_vec()) - } else { - let hlit = bits(&mut pos, 5)? as usize + 257; - let hdist = bits(&mut pos, 5)? as usize + 1; - let hclen = bits(&mut pos, 4)? as usize + 4; - let mut cl = [0_u8; 19]; - for &s in &CL_ORDER[..hclen] { - cl[s] = bits(&mut pos, 3)? as u8; - } - let mut lens = Vec::new(); - while lens.len() < hlit + hdist { - match decode(&mut pos, &cl)? { - s @ 0..=15 => lens.push(s as u8), - 16 => { - let p = *lens.last()?; - lens.extend(std::iter::repeat_n(p, 3 + bits(&mut pos, 2)? as usize)); - } - 17 => lens.extend(std::iter::repeat_n(0, 3 + bits(&mut pos, 3)? as usize)), - _ => lens.extend(std::iter::repeat_n(0, 11 + bits(&mut pos, 7)? as usize)), - } - } - if lens.len() != hlit + hdist { - return None; - } - (lens[..hlit].to_vec(), lens[hlit..].to_vec()) - }; - loop { - let s = decode(&mut pos, &ll)?; - if s < 256 { - out.push(s as u8); - } else if s == 256 { - break; - } else { - let k = s - 257; - let len = *LEN_BASE.get(k)? as usize + bits(&mut pos, u32::from(LEN_EXTRA[k]))? as usize; - let d = decode(&mut pos, &dl)?; - let dist = *DIST_BASE.get(d)? as usize + bits(&mut pos, u32::from(DIST_EXTRA[d]))? as usize; - let from = out.len().checked_sub(dist)?; - for j in 0..len { - out.push(out[from + j]); - } - } - } - } - _ => return None, - } - if last == 1 { - break; - } - } - let at = pos.div_ceil(8); - let sum = u32::from_be_bytes(z.get(at..at + 4)?.try_into().ok()?); - (sum == adler32(&out) && at + 4 == z.len()).then_some(out) - } - - /// A deterministic pseudo-random byte stream. - fn noise(n: usize, seed: u32) -> Vec { - let mut s = seed | 1; - (0..n) - .map(|_| { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - s as u8 - }) - .collect() - } - - #[test] - fn round_trips() { - let mut cases: Vec> = vec![vec![], vec![7], vec![1, 2], vec![0; 1], vec![0; 300_000], b"abcabcabcabcabcabd".to_vec()]; - cases.push(noise(100_000, 9)); // incompressible: stored blocks - cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); // few symbols: dynamic - cases.push((0..200_000_u32).map(|i| ((i / 7) % 251) as u8).collect()); // long repeats - // text-like: a small vocabulary repeated at varying distances - let words: [&[u8]; 6] = [b"red ", b"green ", b"blue ", b"alpha ", b"half ", b"linear "]; - cases.push(noise(40_000, 5).iter().flat_map(|b| words[*b as usize % 6].iter().copied()).collect()); - // skewed frequencies: Huffman lengths beyond 15 before limiting - let mut fib = Vec::new(); - let (mut a, mut b) = (1_usize, 1_usize); - for s in 0..25_u8 { - fib.extend(std::iter::repeat_n(s.wrapping_mul(37), a.min(200_000))); - (a, b) = (b, a + b); - } - let r = noise(fib.len() * 4, 11); - for i in (1..fib.len()).rev() { - let j = u32::from_le_bytes(r[i * 4..i * 4 + 4].try_into().unwrap()) as usize % (i + 1); - fib.swap(i, j); - } - cases.push(fib); - for (k, data) in cases.iter().enumerate() { - let z = zlib(data, &mut Tables::default()); - assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "case {k}"); - // worst case: stored overhead only - assert!(z.len() <= data.len() + (data.len().div_ceil(BLOCK_TOKENS) + 1) * 5 + 6, "case {k}: {} > {}", z.len(), data.len()); - } - // repetitive data actually compresses - assert!(zlib(&cases[4], &mut Tables::default()).len() < 2_000); - assert!(zlib(&cases[8], &mut Tables::default()).len() < 20_000); - } - - #[test] - fn length_limits_hold() { - // Fibonacci frequencies give the deepest possible Huffman tree - let mut f = vec![0_u32; 286]; - let (mut a, mut b) = (1_u32, 1_u32); - for x in f.iter_mut().take(30) { - *x = a; - (a, b) = (b, a.saturating_add(b)); - } - let l = lengths(&f, 15); - assert!(l.iter().all(|&d| d <= 15)); - // a complete code: Kraft sum exactly 1 - let kraft: f64 = l.iter().filter(|&&d| d > 0).map(|&d| 0.5_f64.powi(i32::from(d))).sum(); - assert!((kraft - 1.0).abs() < 1e-12); - // one used symbol still gets a two-symbol code - let l = lengths(&[0, 5, 0], 7); - assert_eq!(l.iter().filter(|&&d| d == 1).count(), 2); - } - - #[test] - fn adler_matches_rfc_example() { - // the classic test vector for "Wikipedia" - assert_eq!(adler32(b"Wikipedia"), 0x11E6_0398); - assert_eq!(adler32(&[]), 1); - } - - /// The match finder before #4659: tables of its own per input, every chain candidate compared - /// from its first byte. - struct OldMatcher<'a> { - data: &'a [u8], - head: Vec, - prev: Vec, - bits: u32, - } - - impl<'a> OldMatcher<'a> { - /// Tables of its own, sized to `data`. - fn new(data: &'a [u8]) -> Self { - let span = data.len().clamp(256, WINDOW).next_power_of_two(); - let bits = span.trailing_zeros().clamp(8, 15); - OldMatcher { data, head: vec![0; 1 << bits], prev: vec![0; span], bits } - } - - /// The hash of the three bytes at `p`, None near the end. - fn hash(&self, p: usize) -> Option { - let b = self.data.get(p..p + MIN_MATCH)?; - let v = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); - Some((v.wrapping_mul(0x9E37_79B1) >> (32 - self.bits)) as usize) - } - - /// Record position `p` (stored as `p + 1`; 0 = empty). - fn insert(&mut self, p: usize) { - let Some(h) = self.hash(p) else { return }; - let mask = self.prev.len() - 1; - self.prev[p & mask] = self.head[h]; - self.head[h] = p as u32 + 1; - } - - /// The longest earlier match at `p`: (length, distance), length 0 if none. - fn longest(&self, p: usize) -> (usize, usize) { - let Some(h) = self.hash(p) else { return (0, 0) }; - let max = (self.data.len() - p).min(MAX_MATCH); - let here = &self.data[p..p + max]; - let mask = self.prev.len() - 1; - let (mut best, mut best_dist) = (0, 0); - let mut cand = self.head[h] as usize; - for _ in 0..MAX_CHAIN { - let Some(c) = cand.checked_sub(1) else { break }; - if c >= p || p - c > WINDOW { - break; - } - let l = self.data[c..].iter().zip(here).take_while(|(a, b)| a == b).count(); - if l > best { - (best, best_dist) = (l, p - c); - if l == max { - break; - } - } - let next = self.prev[c & mask] as usize; - if next == 0 || next > c { - break; - } - cand = next; - } - (best, best_dist) - } - } - - /// Inputs shaped like OpenEXR chunks (predicted half-float scan lines, single and 16-line) - /// and the awkward cases: empty, tiny, incompressible, long runs, few symbols. - fn chunk_cases() -> Vec> { - let line: Vec = (0..23_040_u32).map(|i| (128 + ((i / 3) % 7) as i32 - 3 + (i % 5 == 0) as i32) as u8).collect(); - let mut cases = vec![line.clone(), line.iter().rev().copied().collect(), vec![], vec![9], b"abcabcabcabd".to_vec()]; - cases.push(line.iter().cycle().take(16 * 23_040).copied().collect()); // ZIP: 16 lines, longer than the window - cases.push(noise(23_040, 4)); - cases.push(noise(70_000, 3).iter().map(|b| b % 4).collect()); - cases.push(vec![0; 300_000]); - cases.push(line.clone()); - cases.push(noise(300, 7)); - cases - } - - /// The matcher's greedy parse of `data`: what `deflate` codes. - fn parse (usize, usize), G: FnMut(usize)>(data: &[u8], mut longest: F, mut insert: G) -> Vec<(usize, usize)> { - let (mut out, mut i) = (Vec::new(), 0); - while i < data.len() { - let (len, dist) = longest(i); - let step = if len >= MIN_MATCH { len } else { 1 }; - out.push(if len >= MIN_MATCH { (len, dist) } else { (0, 0) }); - for p in i..i + step { - insert(p); - } - i += step; - } - out - } - - /// Shared tables and the candidate skip find exactly the matches the previous matcher found. - #[test] - fn shared_tables_find_the_matches_the_old_per_chunk_tables_found() { - let mut tables = Tables::default(); - for (k, data) in chunk_cases().iter().enumerate() { - let old = std::cell::RefCell::new(OldMatcher::new(data)); - let want = parse(data, |p| old.borrow().longest(p), |p| old.borrow_mut().insert(p)); - let new = std::cell::RefCell::new(Matcher::new(data, &mut tables).expect("fits")); - let got = parse(data, |p| new.borrow().longest(p), |p| new.borrow_mut().insert(p)); - assert!(got == want, "chunk {k}: the parse differs"); - } - } - - /// Shared tables compress to the bytes fresh ones give, also when positions run out and they start over. - #[test] - fn shared_tables_give_the_bytes_of_fresh_ones() { - let mut shared = Tables::default(); - for (k, data) in chunk_cases().iter().enumerate() { - let z = zlib(data, &mut shared); - assert!(z == zlib(data, &mut Tables::default()), "chunk {k}"); - assert_eq!(inflate(&z).as_deref(), Some(data.as_slice()), "chunk {k}"); - } - // positions about to run past u32: the tables start over and the bytes stay the same - for gap in [0_u32, 1, 2, 23_040, 23_041, 1 << 20] { - shared.base = u32::MAX - gap; - for (k, data) in chunk_cases().iter().enumerate() { - assert!(zlib(data, &mut shared) == zlib(data, &mut Tables::default()), "gap {gap}, chunk {k}"); - } - } - } - - /// Later chunks use the same tables instead of allocating new ones. - #[test] - fn the_tables_are_reused_across_chunks() { - let mut tables = Tables::default(); - let line = &chunk_cases()[0]; - zlib(line, &mut tables); - let (head, prev) = (tables.head.as_ptr(), tables.prev.as_ptr()); - for _ in 0..4 { - zlib(line, &mut tables); - assert!(tables.head.as_ptr() == head && tables.prev.as_ptr() == prev, "a chunk allocated new tables"); - } - assert_eq!(tables.base as usize, 5 * line.len()); - } -} diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index d18e4ddac..413a46071 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -17,7 +17,6 @@ use std::io::Write; mod aac_out; mod audio_out; -mod deflate; mod job; mod mp3_out; mod mxf_out; diff --git a/crates/export/src/raster.rs b/crates/export/src/raster.rs index 34254da53..ed7d244ba 100644 --- a/crates/export/src/raster.rs +++ b/crates/export/src/raster.rs @@ -231,7 +231,7 @@ pub(crate) fn exr(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: ExrComp let mut px = vec![0_u16; wn * names.len()]; let mut raw = Vec::with_capacity(line * lines); // the deflate hash tables, shared by the frame's chunks instead of zeroed for each one - let mut tables = crate::deflate::Tables::default(); + let mut tables = craft_exr::zlib::Tables::default(); for (k, block) in rgba[..n].chunks(row * lines).enumerate() { raw.clear(); for src in block.chunks_exact(row) { @@ -255,7 +255,7 @@ pub(crate) fn exr(rgba: &[u8], w: u32, h: u32, alpha: bool, compression: ExrComp let packed = match compression { ExrCompression::None => None, ExrCompression::Rle => Some(rle(&predict(&raw))), - ExrCompression::Zips | ExrCompression::Zip => Some(crate::deflate::zlib(&predict(&raw), &mut tables)), + ExrCompression::Zips | ExrCompression::Zip => Some(craft_exr::zlib::zlib(&predict(&raw), &mut tables)), }; // a chunk that does not shrink is stored as it is (readers compare the sizes) let data = packed.as_deref().filter(|p| p.len() < raw.len()).unwrap_or(&raw); @@ -508,7 +508,12 @@ mod tests { } else if c == ExrCompression::Rle { unpredict(&unrle(data)) } else { - unpredict(&crate::deflate::tests::inflate(data).unwrap()) + { + use std::io::Read as _; + let mut decoded = Vec::new(); + flate2::read::ZlibDecoder::new(data).read_to_end(&mut decoded).unwrap(); + unpredict(&decoded) + } }; assert_eq!(got, want, "{c:?} chunk {k}"); } From a95467d42bfcbd3c62d94a5824e0b17ced2f25ca Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:37:34 +0000 Subject: [PATCH 19/24] dng: use shared lossless JPEG decoder --- Cargo.lock | 21 ++ Cargo.toml | 2 + crates/dng/Cargo.toml | 1 + crates/dng/src/ljpeg.rs | 508 +--------------------------------------- 4 files changed, 32 insertions(+), 500 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b4b4bb9c2..742703e42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1135,6 +1135,26 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-raw" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +dependencies = [ + "craft-tiff", + "rayon", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-timeline" version = "0.1.0" @@ -2099,6 +2119,7 @@ dependencies = [ name = "filmcraft-dng" version = "0.20.1" dependencies = [ + "craft-raw", "filmcraft-dng", "image", "rayon", diff --git a/Cargo.toml b/Cargo.toml index d9421344d..aab9cea8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,8 @@ proptest = "1" roxmltree = "0.21" # Shared YUV4MPEG2 parser and frame index. craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } +# Shared lossless JPEG decoding for DNG. +craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d16ee7926c8403ded5ea3209bc2ab14b1570281a", default-features = false } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both diff --git a/crates/dng/Cargo.toml b/crates/dng/Cargo.toml index ca449ff43..17973f854 100644 --- a/crates/dng/Cargo.toml +++ b/crates/dng/Cargo.toml @@ -15,6 +15,7 @@ writer = [] serde = ["dep:serde"] [dependencies] +craft-raw = { workspace = true } rayon = { workspace = true, optional = true } # Baseline (lossy) JPEG for Compression 34892 tiles: the JPEG decoder the workspace already uses # (crates/codecs, crates/media), pure Rust. diff --git a/crates/dng/src/ljpeg.rs b/crates/dng/src/ljpeg.rs index 25cf4da8f..026782ce4 100644 --- a/crates/dng/src/ljpeg.rs +++ b/crates/dng/src/ljpeg.rs @@ -1,506 +1,14 @@ -//! Lossless JPEG decoding: ITU-T T.81 | ISO/IEC 10918-1 Annex H (process 14, Huffman coded, -//! "SOF3"), the compression DNG calls lossless JPEG (Compression = 7). -//! -//! - Frame header (B.2.2): 2–16-bit precision, 1–4 components, each sampled 1×1. -//! - Huffman tables (B.2.4.2, Annex C) decoded with a 9-bit lookup and the F.2.2.3 code-length -//! walk for longer codes. -//! - Scan header (B.2.3): one scan holding every component, predictor `Ss` 1–7 (Table H.1) and -//! point transform `Al`. -//! - Prediction (H.1.2.1): the first line (and the first line after a restart) predicts from the -//! left neighbour with 2^(P − Pt − 1) for its first sample; later lines predict their first -//! sample from the one above and the rest with the selected predictor. Differences are added -//! modulo 2^16 (H.1.2.2) and the SSSS = 16 category is 32768 with no extra bits. -//! - Restart intervals (DRI, RSTn) that span whole lines. +//! Lossless JPEG (T.81 process 14) decoding for DNG, using the shared line-restart decoder. use crate::{Error, Result}; +pub use craft_raw::ljpeg_lines::{Decoded, predict}; -/// A decoded lossless JPEG image. -#[derive(Debug)] -pub struct Decoded { - pub width: usize, - pub height: usize, - pub components: usize, - pub precision: u8, - pub predictor: u8, - /// Samples, interleaved by component, line after line, shifted back up by the point transform. - pub samples: Vec, -} - -const LOOKUP_BITS: u32 = 9; -/// Bits of the combined code + difference lookup. -const FULL_BITS: u32 = 13; -/// A combined lookup entry: bit 8 set, the bits used in 0–7, the difference in 16–31. -const FULL_VALID: u32 = 0x100; - -/// A Huffman table for decoding (T.81 Annex C and F.2.2.3). -#[derive(Clone)] -struct Huffman { - /// For codes of up to LOOKUP_BITS bits: (length, value) indexed by the next LOOKUP_BITS bits; - /// length 0 means "longer code". - fast: Vec<(u8, u8)>, - maxcode: [i32; 17], - valptr: [i32; 17], - mincode: [i32; 17], - values: Vec, - /// For a code and its extra bits that fit in FULL_BITS together (difference categories - /// 0–15): `FULL_VALID | used bits | difference << 16`, indexed by the next FULL_BITS bits; - /// 0 means "decode the slow way". - full: Vec, -} - -impl Huffman { - fn new(bits: &[u8; 16], values: Vec) -> Result { - // C.2: code sizes and codes - let mut sizes = Vec::new(); - for (l, &n) in bits.iter().enumerate() { - for _ in 0..n { - sizes.push(l as u8 + 1); - } - } - if sizes.len() != values.len() || sizes.is_empty() { - return Err(Error::Malformed("lossless JPEG: Huffman table counts")); - } - let mut codes = Vec::with_capacity(sizes.len()); - let mut code: u32 = 0; - let mut si = sizes[0]; - let mut k = 0; - while k < sizes.len() { - while k < sizes.len() && sizes[k] == si { - codes.push(code); - code += 1; - k += 1; - } - // the codes of one length must fit in that length - if code > (1u32 << si) { - return Err(Error::Malformed("lossless JPEG: Huffman table over-subscribed")); - } - code <<= 1; - si += 1; - } - // F.2.2.3 decoder tables - let mut maxcode = [-1i32; 17]; - let mut valptr = [0i32; 17]; - let mut mincode = [0i32; 17]; - let mut j = 0usize; - for l in 1..=16usize { - let n = usize::from(bits[l - 1]); - if n == 0 { - maxcode[l] = -1; - } else { - valptr[l] = j as i32; - mincode[l] = codes[j] as i32; - j += n; - maxcode[l] = codes[j - 1] as i32; - } - } - let mut fast = vec![(0u8, 0u8); 1 << LOOKUP_BITS]; - for (i, (&c, &s)) in codes.iter().zip(&sizes).enumerate() { - let s = u32::from(s); - if s <= LOOKUP_BITS { - let shift = LOOKUP_BITS - s; - let base = (c << shift) as usize; - for f in fast.iter_mut().skip(base).take(1 << shift) { - *f = (s as u8, values[i]); - } - } - } - let mut full = vec![0u32; 1 << FULL_BITS]; - for (&c, (&s, &ssss)) in codes.iter().zip(sizes.iter().zip(&values)) { - let (s, ssss) = (u32::from(s), u32::from(ssss)); - if ssss >= 16 || s + ssss > FULL_BITS { - continue; - } - let total = s + ssss; - for v in 0..1u32 << ssss { - let diff = match ssss { - 0 => 0, - k if v < 1 << (k - 1) => v as i32 - (1 << k) + 1, - _ => v as i32, - }; - let entry = FULL_VALID | total | (u32::from(diff as i16 as u16) << 16); - let shift = FULL_BITS - total; - let base = (((c << ssss) | v) << shift) as usize; - for f in full.iter_mut().skip(base).take(1 << shift) { - *f = entry; - } - } - } - Ok(Huffman { fast, maxcode, valptr, mincode, values, full }) - } -} - -/// Entropy-coded data reader: removes stuffed zero bytes and stops at markers (feeding zeros). -struct Bits<'a> { - data: &'a [u8], - pos: usize, - acc: u64, - n: u32, - /// The marker that ended the data (after 0xFF), if one was met. - marker: Option, -} - -impl<'a> Bits<'a> { - fn new(data: &'a [u8], pos: usize) -> Self { - Bits { data, pos, acc: 0, n: 0, marker: None } - } - - fn fill(&mut self) { - // fast path: eight bytes without a 0xFF (no stuffing, no marker) go in at once - if self.marker.is_none() - && let Some(b) = self.data.get(self.pos..self.pos + 8) - { - let chunk = u64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]); - let inv = !chunk; - let has_ff = inv.wrapping_sub(0x0101_0101_0101_0101) & !inv & 0x8080_8080_8080_8080 != 0; - if !has_ff { - let take = (64 - self.n) / 8; - if take > 0 { - self.acc |= (chunk >> (64 - 8 * take)) << (64 - self.n - 8 * take); - self.pos += take as usize; - self.n += 8 * take; - return; - } - } - } - while self.n <= 56 { - let mut byte = 0u8; - if self.marker.is_none() { - match self.data.get(self.pos) { - Some(&0xFF) => match self.data.get(self.pos + 1) { - Some(&0x00) => { - byte = 0xFF; - self.pos += 2; - } - Some(&m) => self.marker = Some(m), - None => self.marker = Some(0xD9), - }, - Some(&b) => { - byte = b; - self.pos += 1; - } - None => self.marker = Some(0xD9), - } - } - self.acc |= u64::from(byte) << (56 - self.n); - self.n += 8; - } - } - - fn peek(&mut self, k: u32) -> u32 { - if self.n < k { - self.fill(); - } - (self.acc >> (64 - k)) as u32 - } - - fn skip(&mut self, k: u32) { - self.acc <<= k; - self.n -= k; - } - - fn bits(&mut self, k: u32) -> u32 { - if k == 0 { - return 0; - } - let v = self.peek(k); - self.skip(k); - v - } - - /// Decode one Huffman symbol (F.2.2.3). - fn decode(&mut self, h: &Huffman) -> Result { - let p = self.peek(LOOKUP_BITS) as usize; - if let Some(&(len, v)) = h.fast.get(p) - && len > 0 - { - self.skip(u32::from(len)); - return Ok(v); - } - let mut code = self.bits(1) as i32; - for l in 1..=16usize { - if code <= h.maxcode[l] { - let i = h.valptr[l] + code - h.mincode[l]; - return h.values.get(i as usize).copied().ok_or(Error::Malformed("lossless JPEG: bad Huffman code")); - } - code = (code << 1) | self.bits(1) as i32; - } - Err(Error::Malformed("lossless JPEG: bad Huffman code")) - } - - /// Skip to the byte after the next RSTn marker, starting a new bit stream there. - fn restart(&mut self) -> Result<()> { - // the decoder may have looked ahead into fill bits; find the marker in the bytes - let mut p = self.pos; - loop { - match (self.data.get(p), self.data.get(p + 1)) { - (Some(0xFF), Some(&m)) if (0xD0..=0xD7).contains(&m) => { - *self = Bits::new(self.data, p + 2); - return Ok(()); - } - (Some(_), Some(_)) => p += 1, - _ => return Err(Error::Malformed("lossless JPEG: missing restart marker")), - } - } - } -} - -struct Component { - id: u8, - table: usize, -} - -/// Decode a lossless JPEG stream. `max_samples` caps the picture (width × height × components). +/// Decode a lossless JPEG stream, capping the total number of samples. pub fn decode(data: &[u8], max_samples: usize) -> Result { - if data.get(0..2) != Some(&[0xFF, 0xD8]) { - return Err(Error::Malformed("lossless JPEG: no SOI marker")); - } - let mut pos = 2usize; - let mut tables: [Option; 4] = [None, None, None, None]; - let mut frame: Option<(u8, usize, usize, Vec)> = None; - let mut restart_interval = 0usize; - loop { - // markers may be preceded by fill bytes 0xFF - while data.get(pos) == Some(&0xFF) && data.get(pos + 1) == Some(&0xFF) { - pos += 1; - } - let (Some(&0xFF), Some(&m)) = (data.get(pos), data.get(pos + 1)) else { - return Err(Error::Malformed("lossless JPEG: expected a marker")); - }; - pos += 2; - if m == 0xD9 { - return Err(Error::Malformed("lossless JPEG: no scan")); - } - if (0xD0..=0xD8).contains(&m) || m == 0x01 { - continue; // markers without a segment - } - let len_b = data.get(pos..pos + 2).ok_or(Error::Truncated)?; - let len = usize::from(u16::from_be_bytes([len_b[0], len_b[1]])); - if len < 2 { - return Err(Error::Malformed("lossless JPEG: segment length")); - } - let seg = data.get(pos + 2..pos + len).ok_or(Error::Truncated)?; - match m { - 0xC3 => { - let [p, y0, y1, x0, x1, nf, rest @ ..] = seg else { - return Err(Error::Malformed("lossless JPEG: short frame header")); - }; - let (precision, nf) = (*p, usize::from(*nf)); - let height = usize::from(u16::from_be_bytes([*y0, *y1])); - let width = usize::from(u16::from_be_bytes([*x0, *x1])); - if !(2..=16).contains(&precision) { - return Err(Error::Unsupported(format!("lossless JPEG precision {precision}"))); - } - if height == 0 { - return Err(Error::Unsupported("lossless JPEG with the height in a DNL marker".into())); - } - if width == 0 || !(1..=4).contains(&nf) || rest.len() < nf * 3 { - return Err(Error::Malformed("lossless JPEG: frame header")); - } - let mut comps = Vec::with_capacity(nf); - for c in rest.as_chunks::<3>().0.iter().take(nf) { - if c[1] != 0x11 { - return Err(Error::Unsupported("subsampled lossless JPEG components".into())); - } - comps.push(Component { id: c[0], table: 0 }); - } - frame = Some((precision, width, height, comps)); - } - 0xC4 => { - let mut s = seg; - while !s.is_empty() { - let [tcth, counts @ ..] = s else { break }; - let bits: [u8; 16] = counts.get(..16).and_then(|b| b.try_into().ok()).ok_or(Error::Truncated)?; - let n: usize = bits.iter().map(|&b| usize::from(b)).sum(); - let values = counts.get(16..16 + n).ok_or(Error::Truncated)?.to_vec(); - let th = usize::from(tcth & 0x0F); - if tcth >> 4 != 0 || th > 3 { - return Err(Error::Malformed("lossless JPEG: Huffman table class or id")); - } - if values.iter().any(|&v| v > 16) { - return Err(Error::Malformed("lossless JPEG: difference category above 16")); - } - tables[th] = Some(Huffman::new(&bits, values)?); - s = counts.get(16 + n..).unwrap_or(&[]); - } - } - 0xDD => { - let r = seg.get(0..2).ok_or(Error::Truncated)?; - restart_interval = usize::from(u16::from_be_bytes([r[0], r[1]])); - } - 0xDA => { - let Some((precision, width, height, mut comps)) = frame.take() else { - return Err(Error::Malformed("lossless JPEG: scan before the frame header")); - }; - let ns = usize::from(*seg.first().ok_or(Error::Truncated)?); - if ns != comps.len() { - return Err(Error::Unsupported("lossless JPEG with more than one scan".into())); - } - for k in 0..ns { - let c = seg.get(1 + 2 * k..3 + 2 * k).ok_or(Error::Truncated)?; - let comp = comps.iter_mut().find(|x| x.id == c[0]).ok_or(Error::Malformed("lossless JPEG: unknown scan component"))?; - comp.table = usize::from(c[1] >> 4); - if comp.table > 3 { - return Err(Error::Malformed("lossless JPEG: Huffman table id")); - } - } - let tail = seg.get(1 + 2 * ns..4 + 2 * ns).ok_or(Error::Truncated)?; - let (predictor, pt) = (tail[0], tail[2] & 0x0F); - if !(1..=7).contains(&predictor) { - return Err(Error::Unsupported(format!("lossless JPEG predictor {predictor}"))); - } - if pt >= precision { - return Err(Error::Malformed("lossless JPEG: point transform")); - } - let n = width.checked_mul(height).and_then(|v| v.checked_mul(comps.len())).ok_or(Error::TooLarge)?; - if n > max_samples { - return Err(Error::TooLarge); - } - let mut hufs = Vec::with_capacity(comps.len()); - for c in &comps { - hufs.push(tables.get(c.table).and_then(Option::as_ref).ok_or(Error::Malformed("lossless JPEG: missing Huffman table"))?); - } - // every sample costs at least one bit of entropy-coded data (a Huffman code is at - // least one bit long), so a frame bigger than the data cannot be real - if n.div_ceil(8) > data.len().saturating_sub(pos + len) { - return Err(Error::Truncated); - } - let params = ScanParams { width, height, precision, predictor, pt, restart: restart_interval }; - let samples = scan(data, pos + len, &hufs, ¶ms)?; - return Ok(Decoded { width, height, components: comps.len(), precision, predictor, samples }); - } - 0xC0..=0xC2 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => { - return Err(Error::Unsupported("lossy (DCT) JPEG in a DNG".into())); - } - _ => {} // APPn, COM, DQT … are skipped - } - pos += len; - } -} - -/// Frame and scan parameters needed to decode one scan. -struct ScanParams { - width: usize, - height: usize, - precision: u8, - predictor: u8, - pt: u8, - /// Restart interval in samples per component (0 = none). - restart: usize, -} - -/// The next difference (F.2.2.1 and H.1.2.2): category and extra bits from the combined table -/// when they fit, else symbol by symbol. -#[inline(always)] -fn difference(bits: &mut Bits, h: &Huffman) -> Result { - let e = h.full.get(bits.peek(FULL_BITS) as usize).copied().unwrap_or(0); - if e & FULL_VALID != 0 { - bits.skip(e & 0xFF); - return Ok(i32::from((e >> 16) as u16 as i16)); - } - Ok(match u32::from(bits.decode(h)?) { - 0 => 0, - 16 => 32768, - s => { - let v = bits.bits(s) as i32; - if v < (1 << (s - 1)) { v - (1 << s) + 1 } else { v } - } + craft_raw::ljpeg_lines::decode(data, max_samples).map_err(|e| match e { + craft_raw::ljpeg_lines::Error::Truncated => Error::Truncated, + craft_raw::ljpeg_lines::Error::TooLarge => Error::TooLarge, + craft_raw::ljpeg_lines::Error::Malformed(s) => Error::Malformed(s), + craft_raw::ljpeg_lines::Error::Unsupported(s) => Error::Unsupported(s), }) } - -/// Decode the entropy-coded data of one scan (H.1.2). -fn scan(data: &[u8], start: usize, hufs: &[&Huffman], params: &ScanParams) -> Result> { - let ScanParams { width, height, precision, predictor, pt, restart } = *params; - let nc = hufs.len(); - let row_len = width * nc; - if restart != 0 && !restart.is_multiple_of(width) { - return Err(Error::Unsupported("lossless JPEG restart interval that splits a line".into())); - } - let lines_per_interval = if restart == 0 { usize::MAX } else { restart / width }; - let mut out = vec![0u16; row_len * height]; - let mut bits = Bits::new(data, start); - let initial = 1i32 << (precision - pt - 1); - let mut line_in_interval = 0usize; - for y in 0..height { - if line_in_interval == lines_per_interval { - bits.restart()?; - line_in_interval = 0; - } - let (above, cur) = out.split_at_mut(y * row_len); - let cur = cur.get_mut(..row_len).ok_or(Error::TooLarge)?; - let above = if line_in_interval == 0 { None } else { above.get((y - 1) * row_len..) }; - match predictor { - 1 => line::<1>(&mut bits, hufs, cur, above, initial)?, - 2 => line::<2>(&mut bits, hufs, cur, above, initial)?, - 3 => line::<3>(&mut bits, hufs, cur, above, initial)?, - 4 => line::<4>(&mut bits, hufs, cur, above, initial)?, - 5 => line::<5>(&mut bits, hufs, cur, above, initial)?, - 6 => line::<6>(&mut bits, hufs, cur, above, initial)?, - _ => line::<7>(&mut bits, hufs, cur, above, initial)?, - } - line_in_interval += 1; - } - if pt > 0 { - for s in &mut out { - *s <<= pt; - } - } - Ok(out) -} - -/// One line of samples (H.1.2.1): the first line of a restart interval (`above` is `None`) -/// predicts from the left with `initial` for its first sample; other lines predict their first -/// sample from above and the rest with predictor `P`. -#[inline(always)] -fn line(bits: &mut Bits, hufs: &[&Huffman], cur: &mut [u16], above: Option<&[u16]>, initial: i32) -> Result<()> { - let nc = hufs.len(); - let row_len = cur.len(); - let above = match above { - Some(a) => Some(a.get(..row_len).ok_or(Error::TooLarge)?), - None => None, - }; - for (c, h) in hufs.iter().enumerate() { - let d = difference(bits, h)?; - let px = match above { - Some(a) => i32::from(a[c]), - None => initial, - }; - cur[c] = ((px + d) & 0xFFFF) as u16; - } - let mut i = nc; - match above { - None => { - while i < row_len { - for h in hufs { - let d = difference(bits, h)?; - cur[i] = ((i32::from(cur[i - nc]) + d) & 0xFFFF) as u16; - i += 1; - } - } - } - Some(a) => { - while i < row_len { - for h in hufs { - let d = difference(bits, h)?; - let px = predict(P, i32::from(cur[i - nc]), i32::from(a[i]), i32::from(a[i - nc])); - cur[i] = ((px + d) & 0xFFFF) as u16; - i += 1; - } - } - } - } - Ok(()) -} - -/// Table H.1 predictors (`ra` left, `rb` above, `rc` above left). -#[inline(always)] -pub fn predict(p: u8, ra: i32, rb: i32, rc: i32) -> i32 { - match p { - 1 => ra, - 2 => rb, - 3 => rc, - 4 => ra + rb - rc, - 5 => ra + ((rb - rc) >> 1), - 6 => rb + ((ra - rc) >> 1), - _ => (ra + rb) >> 1, - } -} From 50c87f598b42dc19cbdb4d3b20c20caf41d69674 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:17:06 +0000 Subject: [PATCH 20/24] export: update shared EXR compressor lint fix --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9bf41019..1a7de17a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=f31e878c6e923748aebae75869e9dd88874d05d3#f31e878c6e923748aebae75869e9dd88874d05d3" +source = "git+https://github.com/storytold/craft-libs?rev=ab37b756cc86dfc552eeb351b0b407e546ec4d9e#ab37b756cc86dfc552eeb351b0b407e546ec4d9e" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 8f51d6ad9..5478cce72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ roxmltree = "0.21" # Shared YUV4MPEG2 parser and frame index. craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f31e878c6e923748aebae75869e9dd88874d05d3" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ab37b756cc86dfc552eeb351b0b407e546ec4d9e" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From fd75cc68116e7552b8e668479435cbf39ef145de Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:36:52 +0000 Subject: [PATCH 21/24] Pin merged shared lossless JPEG --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 742703e42..c21528484 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1138,7 +1138,7 @@ dependencies = [ [[package]] name = "craft-raw" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +source = "git+https://github.com/storytold/craft-libs?rev=7738cc6c689060475cb6947a6e3db6ed78fb3419#7738cc6c689060475cb6947a6e3db6ed78fb3419" dependencies = [ "craft-tiff", "rayon", @@ -1149,7 +1149,7 @@ dependencies = [ [[package]] name = "craft-tiff" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d16ee7926c8403ded5ea3209bc2ab14b1570281a#d16ee7926c8403ded5ea3209bc2ab14b1570281a" +source = "git+https://github.com/storytold/craft-libs?rev=7738cc6c689060475cb6947a6e3db6ed78fb3419#7738cc6c689060475cb6947a6e3db6ed78fb3419" dependencies = [ "serde", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index aab9cea8a..d76b4706e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ roxmltree = "0.21" # Shared YUV4MPEG2 parser and frame index. craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } # Shared lossless JPEG decoding for DNG. -craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d16ee7926c8403ded5ea3209bc2ab14b1570281a", default-features = false } +craft-raw = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7738cc6c689060475cb6947a6e3db6ed78fb3419", default-features = false } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From 102e7851b3f6f9e902ae65822ab06ee11835999c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:18:14 +0000 Subject: [PATCH 22/24] export: pin merged shared EXR deflate --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a7de17a4..8efe4ca44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,7 +1093,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b4255 [[package]] name = "craft-exr" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ab37b756cc86dfc552eeb351b0b407e546ec4d9e#ab37b756cc86dfc552eeb351b0b407e546ec4d9e" +source = "git+https://github.com/storytold/craft-libs?rev=1fdc42e5d68ef38799354bb906c2a45c5df25439#1fdc42e5d68ef38799354bb906c2a45c5df25439" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 5478cce72..f5db190a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ roxmltree = "0.21" # Shared YUV4MPEG2 parser and frame index. craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } # Shared OpenEXR ZIP chunk compressor. -craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ab37b756cc86dfc552eeb351b0b407e546ec4d9e" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1fdc42e5d68ef38799354bb906c2a45c5df25439" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # The audio codecs (AAC, AC-3, FLAC, MP1/MP2, Opus) and the containers come from craft-libs. Both From 937a2f950d4e438afccab3df51cc365a7faebeb4 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:30:46 +0100 Subject: [PATCH 23/24] media: DPX stills from craft-dpx (craft-libs #778) (#4848) * media: read DPX and Targa stills with craft-dpx and craft-tga The DPX header and code-value unpacking and the whole Targa decoder move to craft-libs (craft-dpx, craft-tga at the existing 156b491b pin). The DPX colour mapping (BT.709 / BT.601 Y'CbCr, video or full range from the reference codes, linear RGB / luma to 8 bits) and the orientation flips stay in filmcraft-media, on top of the shared Info / codes. The existing DPX and Targa unit tests are kept and now exercise the shared readers; the image-sequence export test also checks that our import of the exported Targa and DPX frames matches ffmpeg within 1 LSB. Refs storytold/craft-libs#778 * export: compare the whole imported frame with ffmpeg's, not a matching prefix * media: leave Targa to #4847, keep the DPX cutover dylanmtaylor's #4847 (opened first) cuts TGA over to craft-tga together with the footer-metadata protections in storytold/craft-libs#1596, so this PR keeps FilmCraft's Targa reader as on main and only moves DPX to craft-dpx. --- Cargo.lock | 11 +- Cargo.toml | 2 + crates/export/src/settings_tests.rs | 8 + crates/media/Cargo.toml | 1 + crates/media/src/dpx.rs | 237 +++++++--------------------- 5 files changed, 77 insertions(+), 182 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f2af61c23..0819a4362 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1090,6 +1090,14 @@ name = "craft-docpath" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +[[package]] +name = "craft-dpx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=156b491b91f3693f4fce53bf69bb97791f51198f#156b491b91f3693f4fce53bf69bb97791f51198f" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -2296,6 +2304,7 @@ dependencies = [ name = "filmcraft-media" version = "0.20.1" dependencies = [ + "craft-dpx", "filmcraft-color", "filmcraft-dng", "filmcraft-frame", @@ -6696,7 +6705,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix 1.1.5", "windows-sys 0.61.2", diff --git a/Cargo.toml b/Cargo.toml index d0b5a9d79..58df1c9d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,8 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] } rayon = "1" proptest = "1" roxmltree = "0.21" +# DPX still reader, on the same commit as craft-media. +craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "156b491b91f3693f4fce53bf69bb97791f51198f" } # Shared YUV4MPEG2 parser and frame index. craft-y4m = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25c7a8959cf70cf8c3f076d05baadb0990710569" } craft-cfb = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "7f3fdc0dfebd03f33a241f91f916e9a78d3e5c93" } diff --git a/crates/export/src/settings_tests.rs b/crates/export/src/settings_tests.rs index 468dccf97..124cf2772 100644 --- a/crates/export/src/settings_tests.rs +++ b/crates/export/src/settings_tests.rs @@ -263,6 +263,14 @@ fn jpeg_targa_and_dpx_sequences() { } // the top-left pixel is the top row (Targa is stored bottom-up), and all of it is blue assert!(out.stdout.as_chunks::<4>().0.iter().all(|p| p[2] > 100 && p[0] < 20), "{first}"); + // FilmCraft's own import (craft-dpx, and the Targa reader) reads the frame as ffmpeg does + if matches!(fmt, Format::TgaSequence | Format::DpxSequence) { + let ours = filmcraft_media::still::decode_raster(&first, &bytes).unwrap(); + assert_eq!((ours.width, ours.height), (64, 36), "{first}"); + assert_eq!(ours.rgba.len(), out.stdout.len(), "{first}: import and ffmpeg frame sizes differ"); + let worst = ours.rgba.iter().zip(&out.stdout).map(|(&a, &b)| a.abs_diff(b)).max().unwrap_or(0); + assert!(worst <= 1, "{first}: import differs from ffmpeg by {worst}"); + } } assert!(jpeg_sizes[0] > jpeg_sizes[1], "quality 95 must be larger than quality 20: {jpeg_sizes:?}"); } diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index 3259e502f..f5ac5d0f5 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -14,6 +14,7 @@ filmcraft-time = { workspace = true } filmcraft-color = { workspace = true } filmcraft-frame = { workspace = true } filmcraft-dng = { workspace = true } +craft-dpx = { workspace = true } rayon = { workspace = true } serde = { workspace = true } thiserror = { workspace = true } diff --git a/crates/media/src/dpx.rs b/crates/media/src/dpx.rs index 31d208ac8..429faeb96 100644 --- a/crates/media/src/dpx.rs +++ b/crates/media/src/dpx.rs @@ -1,35 +1,24 @@ -//! DPX (`.dpx`) stills, decoded clean-room from SMPTE ST 268 (Digital Picture Exchange). +//! DPX (`.dpx`) stills: craft-dpx (SMPTE ST 268) reads the header and the code values; FilmCraft +//! turns them into RGBA8 for the still pipeline. //! //! The first image element is decoded: luma, RGB, RGBA, ABGR, or Y'CbCr (CbYCrY 4:2:2, CbYACrYA //! 4:2:2:4, CbYCr 4:4:4, CbYCrA 4:4:4:4), 8, 10, 12 or 16 bits per component, big-endian (`SDPX`) or -//! little-endian (`XPDS`) files. 10-bit components are filled into 32-bit words (packing 1, method -//! A: two padding bits at the bottom; packing 2, method B: at the top), 12-bit components fill -//! 16-bit words the same two ways; packed 10/12-bit components (packing 0) follow each other with no -//! padding, the first in the low bits of a 32-bit word, a component crossing into the next word -//! continuing in its low bits. Scan lines start on a 32-bit boundary and may carry end-of-line -//! padding. Orientations 0–3 (either scan direction, either line order) are honoured. RGB and luma -//! code values map linearly onto 8 bits (full range); a logarithmic (printing density) transfer is -//! shown as stored, without a film-log conversion. Y'CbCr is converted with the matrix the -//! colorimetric field names (BT.709, or BT.601 for the 601 and composite video codes; by picture -//! size when unspecified), in video range unless the reference low / high data codes span the -//! whole code range; 4:2:2 chroma is shared by the two pixels of a pair. +//! little-endian (`XPDS`) files, every packing craft-dpx reads. Orientations 0–3 (either scan +//! direction, either line order) are honoured. RGB and luma code values map linearly onto 8 bits +//! (full range); a logarithmic (printing density) transfer is shown as stored, without a film-log +//! conversion. Y'CbCr is converted with the matrix the colorimetric field names (BT.709, or BT.601 +//! for the 601 and composite video codes; by picture size when unspecified), in video range unless +//! the reference low / high data codes span the whole code range; 4:2:2 chroma is shared by the two +//! pixels of a pair. //! -//! Not decoded (an "unsupported" error): run-length encoded elements, 1-bit and floating-point -//! components, transposed orientations. +//! Not decoded (an "unsupported" error): run-length encoded elements, signed, 1-bit and +//! floating-point components, transposed orientations. Cineon files are not routed here. +use craft_dpx::{Error, Layout, Limits}; use filmcraft_color::{Matrix, Range, normalize_c, normalize_y, ycbcr_to_rgb}; use crate::{MediaError, Result, still::Raster}; -/// Generic file + image information header: everything we read lies below this. -const HEADER: usize = 1408; -/// First image element of the image information header (72 bytes each). -const ELEMENT: usize = 780; -/// Largest picture we decode (16384 × 16384): bounds the allocation a header can ask for. -const MAX_PIXELS: usize = 1 << 28; -/// "Undefined" value of numeric header fields. -const UNDEFINED: u32 = u32::MAX; - fn err(name: &str, what: &str) -> MediaError { MediaError::Decode(format!("{name}: DPX: {what}")) } @@ -38,82 +27,19 @@ fn unsupported(name: &str, what: String) -> MediaError { MediaError::Unsupported(format!("{name}: DPX {what}")) } +fn map_error(name: &str, e: Error) -> MediaError { + match e { + Error::Unsupported(what) => unsupported(name, what), + Error::NotDpx => err(name, "not a DPX file"), + Error::Malformed(what) => err(name, &what), + Error::LimitExceeded(_) => err(name, "picture too large"), + Error::Encode(what) => err(name, &what), + } +} + /// Whether `bytes` starts with the DPX magic number in either byte order. pub fn sniff(bytes: &[u8]) -> bool { - bytes.starts_with(b"SDPX") || bytes.starts_with(b"XPDS") -} - -/// Header and sample reads in the file's byte order. -#[derive(Clone, Copy)] -struct Order { - big: bool, -} - -impl Order { - fn u16(self, b: &[u8], at: usize) -> u16 { - let v = [b.get(at).copied().unwrap_or(0), b.get(at + 1).copied().unwrap_or(0)]; - if self.big { u16::from_be_bytes(v) } else { u16::from_le_bytes(v) } - } - - fn u32(self, b: &[u8], at: usize) -> u32 { - let v = core::array::from_fn(|i| b.get(at + i).copied().unwrap_or(0)); - if self.big { u32::from_be_bytes(v) } else { u32::from_le_bytes(v) } - } -} - -/// How components are stored in a scan line. -#[derive(Clone, Copy)] -enum Packing { - /// One byte per component. - Bytes, - /// One 16-bit word per component; the value is `word >> shift` (12-bit method A: 4). - Words { shift: u32 }, - /// Three 10-bit components per 32-bit word, the first in the high bits, above `pad` bits. - Ten { pad: u32 }, - /// `bits`-bit components back to back in 32-bit words, low bits first (packing 0). - Packed { bits: u32 }, -} - -impl Packing { - /// Bytes of a scan line holding `comps` components, before end-of-line padding (lines start on - /// a 32-bit boundary). - fn line_bytes(self, comps: usize) -> Option { - let raw = match self { - Packing::Bytes => comps, - Packing::Words { .. } => comps.checked_mul(2)?, - Packing::Ten { .. } => comps.div_ceil(3).checked_mul(4)?, - Packing::Packed { bits } => comps.checked_mul(bits as usize)?.div_ceil(32).checked_mul(4)?, - }; - raw.checked_next_multiple_of(4) - } - - /// The `comps` component values of one scan line. - fn unpack(self, line: &[u8], comps: usize, order: Order, out: &mut Vec) { - out.clear(); - match self { - Packing::Bytes => out.extend(line.iter().take(comps).map(|&b| u16::from(b))), - Packing::Words { shift } => out.extend((0..comps).map(|i| order.u16(line, i * 2) >> shift)), - Packing::Ten { pad } => { - for w in 0..comps.div_ceil(3) { - let word = order.u32(line, w * 4); - for s in [20 + pad, 10 + pad, pad] { - if out.len() < comps { - out.push(((word >> s) & 0x3FF) as u16); - } - } - } - } - Packing::Packed { bits } => { - let mask = (1u64 << bits) - 1; - for i in 0..comps { - let bit = i * bits as usize; - let at = bit / 32 * 4; - let pair = u64::from(order.u32(line, at)) | (u64::from(order.u32(line, at + 4)) << 32); - out.push(((pair >> (bit % 32)) & mask) as u16); - } - } - } - } + craft_dpx::sniff(bytes) == Some(craft_dpx::Kind::Dpx) } /// What the stored components of a pixel are. @@ -127,12 +53,6 @@ enum Components { } impl Components { - fn per_pixel(self) -> usize { - match self { - Components::Rgb { n, .. } | Components::YCbCr { n, .. } => n, - } - } - fn has_alpha(self) -> bool { match self { Components::Rgb { map, .. } => map[3].is_some(), @@ -140,7 +60,7 @@ impl Components { } } - /// RGBA8 of pixel `x` from the unpacked component values of its scan line (`bits` per value). + /// RGBA8 of pixel `x` from the component values of its scan line (`bits` per value). fn pixel(self, line: &[u16], x: usize, bits: u32) -> [u8; 4] { let max = (1u32 << bits) - 1; let to8 = |v: u32| ((v.min(max) * 255 + max / 2) / max) as u8; @@ -172,104 +92,59 @@ impl Components { /// Decode the first image element of a DPX file to straight-alpha RGBA8, top row first. pub fn decode(name: &str, bytes: &[u8]) -> Result { - let order = match bytes.get(..4) { - Some(b"SDPX") => Order { big: true }, - Some(b"XPDS") => Order { big: false }, - _ => return Err(err(name, "not a DPX file")), - }; - if bytes.len() < HEADER { - return Err(err(name, "truncated header")); + if !sniff(bytes) { + return Err(err(name, "not a DPX file")); } - let orientation = order.u16(bytes, 768); - let (w, h) = (order.u32(bytes, 772) as usize, order.u32(bytes, 776) as usize); - let el = ELEMENT; - let low_code = order.u32(bytes, el + 4); - let high_code = order.u32(bytes, el + 12); - let descriptor = bytes.get(el + 20).copied().unwrap_or(0); - let colorimetric = bytes.get(el + 22).copied().unwrap_or(0); - let bits = bytes.get(el + 23).copied().unwrap_or(0); - let packing = order.u16(bytes, el + 24); - let encoding = order.u16(bytes, el + 26); - let element_offset = order.u32(bytes, el + 28); - let eol_padding = order.u32(bytes, el + 32); - if w == 0 || h == 0 { - return Err(err(name, "empty picture")); - } - let pixels = w.checked_mul(h).filter(|&n| n <= MAX_PIXELS).ok_or_else(|| err(name, "picture too large"))?; - if encoding != 0 { - return Err(unsupported(name, "run-length encoding".into())); - } - let layout = match (bits, packing) { - (8, _) => Packing::Bytes, - (16, _) => Packing::Words { shift: 0 }, - (10, 1) => Packing::Ten { pad: 2 }, - (10, 2) => Packing::Ten { pad: 0 }, - (12, 1) => Packing::Words { shift: 4 }, - (12, 2) => Packing::Words { shift: 0 }, - (10 | 12, 0) => Packing::Packed { bits: u32::from(bits) }, - (b, p) => return Err(unsupported(name, format!("{b}-bit components with packing {p}"))), - }; - let (flip_x, flip_y) = match orientation { - 0 => (false, false), - 1 => (true, false), - 2 => (false, true), - 3 => (true, true), - o => return Err(unsupported(name, format!("orientation {o}"))), - }; - let max = (1u32 << bits) - 1; + let frame = craft_dpx::decode(bytes, &Limits::default()).map_err(|e| map_error(name, e))?; + let info = &frame.info; + let (w, h) = (info.width as usize, info.height as usize); + let (flip_x, flip_y) = info.flips().ok_or_else(|| unsupported(name, format!("orientation {}", info.orientation)))?; + let max = u32::from(info.max_code()); // video range unless the reference codes span every code value - let range = if low_code == 0 && high_code == max { Range::Full } else { Range::Limited }; - let matrix = match colorimetric { - 5 | 6 => Matrix::Bt709, - 7..=10 => Matrix::Bt601, - _ => filmcraft_frame::default_matrix(w as u32, h as u32), + let range = if info.reference_low_code == 0 && info.reference_high_code == max { Range::Full } else { Range::Limited }; + let matrix = match info.colorimetric { + Some(5 | 6) => Matrix::Bt709, + Some(7..=10) => Matrix::Bt601, + _ => filmcraft_frame::default_matrix(info.width, info.height), }; let ycbcr = |n, sub, alpha| Components::YCbCr { n, sub, alpha, matrix, range }; - let components = match descriptor { - 6 => Components::Rgb { n: 1, map: [Some(0), Some(0), Some(0), None] }, - 50 => Components::Rgb { n: 3, map: [Some(0), Some(1), Some(2), None] }, - 51 => Components::Rgb { n: 4, map: [Some(0), Some(1), Some(2), Some(3)] }, - 52 => Components::Rgb { n: 4, map: [Some(3), Some(2), Some(1), Some(0)] }, - 100 => ycbcr(2, true, false), - 101 => ycbcr(3, true, true), - 102 => ycbcr(3, false, false), - 103 => ycbcr(4, false, true), - d => return Err(unsupported(name, format!("descriptor {d}"))), + let components = match info.layout { + Layout::Luma => Components::Rgb { n: 1, map: [Some(0), Some(0), Some(0), None] }, + Layout::Rgb => Components::Rgb { n: 3, map: [Some(0), Some(1), Some(2), None] }, + // craft-dpx returns ABGR (descriptor 52) reordered to RGBA + Layout::Rgba => Components::Rgb { n: 4, map: [Some(0), Some(1), Some(2), Some(3)] }, + Layout::CbYCrY422 => ycbcr(2, true, false), + Layout::CbYACrYA4224 => ycbcr(3, true, true), + Layout::CbYCr444 => ycbcr(3, false, false), + Layout::CbYCrA4444 => ycbcr(4, false, true), }; - let n = components.per_pixel(); - let comps = w.checked_mul(n).ok_or_else(|| err(name, "picture too large"))?; - let pad = if eol_padding == UNDEFINED { 0 } else { eol_padding as usize }; - let stride = layout.line_bytes(comps).and_then(|l| l.checked_add(pad)).ok_or_else(|| err(name, "picture too large"))?; - let offset = match element_offset { - 0 | UNDEFINED => order.u32(bytes, 4) as usize, - o => o as usize, - }; - // the last line needs no end-of-line padding - let size = stride.checked_mul(h - 1).and_then(|s| s.checked_add(stride - pad)).ok_or_else(|| err(name, "picture too large"))?; - let data = offset.checked_add(size).and_then(|end| bytes.get(offset..end)).ok_or_else(|| err(name, "truncated image data"))?; - let bits = u32::from(bits); + let per_line = w * info.layout.components(); + let bits = u32::from(info.bits); + let pixels = w.checked_mul(h).ok_or_else(|| err(name, "picture too large"))?; let mut rgba = vec![0u8; pixels * 4]; - let mut line = Vec::with_capacity(comps); - for y in 0..h { - let src = data.get(y * stride..).unwrap_or_default(); - layout.unpack(src, comps, order, &mut line); + for (y, line) in frame.codes.chunks_exact(per_line.max(1)).take(h).enumerate() { let dy = if flip_y { h - 1 - y } else { y }; for x in 0..w { let dx = if flip_x { w - 1 - x } else { x }; let o = (dy * w + dx) * 4; if let Some(px) = rgba.get_mut(o..o + 4) { - px.copy_from_slice(&components.pixel(&line, x, bits)); + px.copy_from_slice(&components.pixel(line, x, bits)); } } } let has_alpha = components.has_alpha() && rgba.as_chunks::<4>().0.iter().any(|p| p[3] != 255); - Ok(Raster { width: w as u32, height: h as u32, rgba, has_alpha }) + Ok(Raster { width: info.width, height: info.height, rgba, has_alpha }) } #[cfg(test)] mod tests { use super::*; + /// First image element of the image information header. + const ELEMENT: usize = 780; + /// "Undefined" value of numeric header fields. + const UNDEFINED: u32 = u32::MAX; + /// A minimal DPX file (header + `data`) in the given byte order. /// Image element fields of a test DPX header. struct Element { From e8541904fccbb09f8da6fe648f885cf2bca540e5 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:53:44 +0100 Subject: [PATCH 24/24] codecs: HE-AAC through craft-audio's SBR decoder; drop local upsampler, ADTS and LATM glue (craft-libs #778) (#4849) * codecs: decode HE-AAC with craft-audio's SBR tool; drop the local upsampler, ADTS and LATM glue craft-libs #1130 gave craft-audio's AAC decoder the HE-AAC SBR high band (Decoder::set_sbr). PacketDecoder::aac now turns it on when the stream plays at twice its core rate, instead of interpolating the AAC-LC core with the local Upsample2x; a reset keeps the choice. The rest of the AAC glue moves to the same crate, at the existing 156b491b pin: aac_output_rate wraps craft_audio::aac::output_rate, adts_split and adts_frame_len read craft_audio::aac::AdtsHeader, and LatmConfig / latm_config / latm_split are craft-audio's (which also read a program config element and skip ascLen). Refs storytold/craft-libs#778 * codecs: reorder AAC with a 5.1-shaped program config element like configuration 6 Configuration 0 now takes the speakers its PCE spells out through craft_audio::aac::speaker_config before the WAV reorder, so an MP4 esds or LATM StreamMuxConfig carrying a centre-first 5.1 PCE no longer sends C, L, R, Ls, Rs, LFE to the wrong speakers. Layouts that cannot be told keep the decoded order. * codecs: keep the learned SBR header when a seek resets the AAC decoder --- crates/codecs/src/audio.rs | 368 +++++++------------------ crates/codecs/src/audio_panic_tests.rs | 2 +- 2 files changed, 93 insertions(+), 277 deletions(-) diff --git a/crates/codecs/src/audio.rs b/crates/codecs/src/audio.rs index e6c18d109..1fe4161b8 100644 --- a/crates/codecs/src/audio.rs +++ b/crates/codecs/src/audio.rs @@ -79,9 +79,10 @@ impl SymphoniaDecoder { } enum Inner { - /// Our own AAC-LC decoder. `up` interpolates HE-AAC's core to the output rate (see [`Upsample2x`]). + /// Our own AAC decoder (craft-audio): AAC-LC, and HE-AAC with its SBR high band when the + /// stream plays at twice the core rate (see [`craft_audio::aac::Decoder::set_sbr`]). /// Surround output is reordered to WAV/SMPTE order (see [`aac_to_wav_order`]). - Aac { dec: Box, asc: Vec, up: Option> }, + Aac { dec: Box, asc: Vec }, /// AAC whose configuration arrives with the first frame (ADTS / LATM). AacPending, /// MPEG audio at this rate whose layer arrives with the first frame (MP4 / QuickTime name the @@ -139,6 +140,14 @@ fn aac_to_wav_order(channel_config: u8) -> Option<&'static [usize]> { }) } +/// [`aac_to_wav_order`] for a stream's configuration. Configuration 0 takes the speakers its +/// program config element spells out (`craft_audio::aac::speaker_config`), so a PCE carried in an +/// MP4 `esds` or in LATM's `StreamMuxConfig` in the shape of 5.1 is reordered like configuration 6; +/// a layout that cannot be told keeps the decoded order. +fn aac_wav_order(asc: &craft_audio::aac::AudioSpecificConfig) -> Option<&'static [usize]> { + aac_to_wav_order(craft_audio::aac::speaker_config(asc)) +} + /// `channels` reordered by `order` (output channel → decoded channel). A decode whose channel count /// does not match the order is returned unchanged. fn reorder(mut channels: Vec>, order: Option<&[usize]>) -> Vec> { @@ -178,13 +187,14 @@ impl PacketDecoder { let dec = SymphoniaDecoder::new(&p)?; Ok(Self { inner: Inner::Symphonia(dec), channels: 0 }) } - /// AAC: our decoder for AAC-LC and the AAC-LC core of HE-AAC (v1/v2); symphonia for other + /// AAC: our decoder for AAC-LC and HE-AAC (v1; the mono core of v2); symphonia for other /// object types. `sample_rate` is the stream's output rate: when it is twice the core rate - /// (HE-AAC, see [`aac_output_rate`]) the decoded core is upsampled to it. + /// (HE-AAC, see [`aac_output_rate`]) the SBR high band is decoded at that rate. pub fn aac(asc: &[u8], sample_rate: u32) -> Result { - if let Ok(dec) = craft_audio::aac::Decoder::new(asc) { - let up = upsamples_core(dec.sample_rate(), sample_rate).then(|| Box::new(Upsample2x::new(dec.channels()))); - return Ok(Self { inner: Inner::Aac { dec: Box::new(dec), asc: asc.to_vec(), up }, channels: 0 }); + if let Ok(mut dec) = craft_audio::aac::Decoder::new(asc) { + let sbr = decodes_sbr(dec.sample_rate(), sample_rate); + dec.set_sbr(sbr); + return Ok(Self { inner: Inner::Aac { dec: Box::new(dec), asc: asc.to_vec() }, channels: 0 }); } Self::new(CODEC_TYPE_AAC, sample_rate, Some(asc.to_vec())) } @@ -224,7 +234,7 @@ impl PacketDecoder { return Ok(()); } let dec = craft_audio::aac::Decoder::new(asc).map_err(|e| CodecError::Unsupported(format!("AAC: {e}")))?; - self.inner = Inner::Aac { dec: Box::new(dec), asc: asc.to_vec(), up: None }; + self.inner = Inner::Aac { dec: Box::new(dec), asc: asc.to_vec() }; Ok(()) } /// AC-3 and E-AC-3 (ATSC A/52, Annex E). @@ -255,12 +265,9 @@ impl PacketDecoder { self.inner = Self::mpeg_audio(layer, rate)?.inner; } let dec = match &mut self.inner { - Inner::Aac { dec, up, .. } => { - let mut out = dec.decode(data).map_err(|e| CodecError::Decode(e.to_string()))?; - if let Some(up) = up { - out = up.process(out); - } - let out = reorder(out, aac_to_wav_order(dec.config().channel_config)); + Inner::Aac { dec, .. } => { + let out = dec.decode(data).map_err(|e| CodecError::Decode(e.to_string()))?; + let out = reorder(out, aac_wav_order(dec.config())); self.channels = out.len(); return Ok(out); } @@ -332,14 +339,10 @@ impl PacketDecoder { } pub fn reset(&mut self) { match &mut self.inner { - Inner::Aac { dec, asc, up } => { - if let Ok(d) = craft_audio::aac::Decoder::new(asc) { - **dec = d; - } - if let Some(up) = up { - up.reset(); - } - } + // craft-audio's reset clears the overlap, noise and SBR signal state but keeps the + // SBR header the stream last sent, so packets after a seek that carry no header still + // get their high band. Rebuilding the decoder would drop it until the next header. + Inner::Aac { dec, .. } => dec.reset(), Inner::Mpa(d) => d.reset(), Inner::Opus { dec, .. } => dec.reset(), Inner::Symphonia(d) => { @@ -367,29 +370,16 @@ pub fn mpeg_audio_layer(frame: &[u8]) -> Option { } } -/// The output rate of an AAC stream: twice the core rate for HE-AAC (ISO/IEC 14496-3 §1.6.5). -/// Explicit signalling names the rate in the `AudioSpecificConfig`; with implicit signalling (an -/// AAC-LC config at 24 kHz or less) SBR is only visible as fill-element data in the access units, -/// so the first ones are decoded to look for it. `None` if `asc` is not one our decoder reads. +/// The output rate of an AAC stream: twice the core rate for HE-AAC (ISO/IEC 14496-3 §1.6.5), +/// from the `AudioSpecificConfig` or, with implicit signalling, the first access units +/// ([`craft_audio::aac::output_rate`]). `None` if `asc` is not one our decoder reads. pub fn aac_output_rate<'a>(asc: &[u8], first_units: impl IntoIterator) -> Option { - let mut dec = craft_audio::aac::Decoder::new(asc).ok()?; - let core = dec.sample_rate(); - if let Some(ext) = dec.config().extension_sample_rate { - return Some(ext); - } - if core <= 24_000 { - for au in first_units { - let _ = dec.decode(au); - if dec.sbr() { - return Some(core * 2); - } - } - } - Some(core) + craft_audio::aac::output_rate(asc, first_units) } -/// Whether [`PacketDecoder::aac`] upsamples a decoded AAC core at `core` Hz to `rate` (HE-AAC). -fn upsamples_core(core: u32, rate: u32) -> bool { +/// Whether [`PacketDecoder::aac`] decodes the SBR high band of an AAC core at `core` Hz to play +/// it at `rate` (HE-AAC). +fn decodes_sbr(core: u32, rate: u32) -> bool { core.checked_mul(2) == Some(rate) } @@ -408,7 +398,7 @@ pub enum FixedFrames<'a> { } /// Samples each packet decodes to at the stream's output `rate`, for codecs where that is the same -/// for every packet: AAC 1024 (2048 when [`PacketDecoder::aac`] upsamples an HE-AAC core); MPEG +/// for every packet: AAC 1024 (2048 when [`PacketDecoder::aac`] decodes HE-AAC with SBR); MPEG /// audio and AC-3 from the frame header of `first`, the stream's first packet; E-AC-3 from the /// syncframes of the program we decode in `first`. `None` when it is not known: an AAC /// configuration our decoder does not read, a header that does not parse, or a header whose rate @@ -417,7 +407,7 @@ pub fn fixed_packet_samples(codec: FixedFrames, first: &[u8], rate: u32) -> Opti let mpegts = match codec { FixedFrames::Aac(asc) => { let core = craft_audio::aac::Decoder::new(asc).ok()?.sample_rate(); - return Some(if upsamples_core(core, rate) { 2048 } else { 1024 }); + return Some(if decodes_sbr(core, rate) { 2048 } else { 1024 }); } FixedFrames::Eac3 => { let mut samples = 0usize; @@ -477,73 +467,6 @@ pub fn contiguous_starts(packets: impl IntoIterator, toleranc .collect() } -/// Taps on each side of the interpolation point in [`Upsample2x`]. -const UPSAMPLE_HALF: usize = 12; - -/// 2× upsampler for HE-AAC. Our decoder reconstructs the AAC-LC core at half the output rate and -/// not yet the SBR high band, so the core is interpolated to the output rate: timing and pitch are -/// right and the band above the core's Nyquist stays empty. Even outputs are the input samples, -/// odd ones a Blackman-windowed sinc half-band interpolation over 2 × [`UPSAMPLE_HALF`] inputs. -/// The look-ahead delays the output by `UPSAMPLE_HALF` input samples (0.5 ms at 24 kHz); the -/// state carries across packets, so a seek primed with the preceding packet gives the same -/// samples as continuous decoding. -#[derive(Clone)] -pub struct Upsample2x { - taps: [f32; 2 * UPSAMPLE_HALF], - /// The last `2 × UPSAMPLE_HALF` input samples per channel. - hist: Vec>, -} - -impl Upsample2x { - pub fn new(channels: usize) -> Self { - let m = UPSAMPLE_HALF as f64; - let mut taps = [0f32; 2 * UPSAMPLE_HALF]; - let mut sum = 0.0; - for (k, t) in taps.iter_mut().enumerate() { - // distance from the interpolation point (halfway between two inputs) - let x = k as f64 - m + 0.5; - let sinc = (std::f64::consts::PI * x).sin() / (std::f64::consts::PI * x); - let w = 0.42 + 0.5 * (std::f64::consts::PI * x / m).cos() + 0.08 * (2.0 * std::f64::consts::PI * x / m).cos(); - *t = (sinc * w) as f32; - sum += sinc * w; - } - for t in &mut taps { - *t = (*t as f64 / sum) as f32; - } - Self { taps, hist: vec![vec![0.0; 2 * UPSAMPLE_HALF]; channels] } - } - - pub fn reset(&mut self) { - for h in &mut self.hist { - h.fill(0.0); - } - } - - pub fn process(&mut self, input: Vec>) -> Vec> { - if self.hist.len() < input.len() { - self.hist.resize(input.len(), vec![0.0; 2 * UPSAMPLE_HALF]); - } - let m = UPSAMPLE_HALF; - input - .into_iter() - .zip(&mut self.hist) - .map(|(x, hist)| { - let mut buf = std::mem::take(hist); - buf.extend_from_slice(&x); - let mut out = Vec::with_capacity(x.len() * 2); - // output pair i is centred on buf[i + m]: the sample itself, then the point halfway - // to the next one from buf[i + 1 ..= i + 2m] - for w in buf.windows(2 * m + 1).take(x.len()) { - out.push(w[m]); - out.push(w[1..].iter().zip(&self.taps).map(|(a, b)| a * b).sum()); - } - *hist = buf.split_off(buf.len() - 2 * m); - out - }) - .collect() - } -} - /// Whether AC-3 and E-AC-3 audio decode (our ATSC A/52 decoder). pub const AC3_DECODER: bool = true; @@ -554,147 +477,18 @@ fn asc_bytes(object_type: u8, sf_index: u8, channel_config: u8) -> Vec { v.to_be_bytes().to_vec() } -/// An ADTS frame (ISO/IEC 13818-7 §6.2): the equivalent AudioSpecificConfig and the raw data -/// block. Frames with several raw data blocks are not split (`None`). +/// An ADTS frame (ISO/IEC 13818-7 §6.2, parsed by [`craft_audio::aac::AdtsHeader`]): the +/// equivalent AudioSpecificConfig and the raw data block. Frames with several raw data blocks are +/// not split (`None`). pub fn adts_split(frame: &[u8]) -> Option<(Vec, &[u8])> { - if frame.len() < 7 || frame[0] != 0xFF || frame[1] & 0xF6 != 0xF0 { - return None; - } - let protection_absent = frame[1] & 1 != 0; - let profile = frame[2] >> 6; - let sf = (frame[2] >> 2) & 15; - let ch = ((frame[2] & 1) << 2) | (frame[3] >> 6); - let len = (((frame[3] & 3) as usize) << 11) | (frame[4] as usize) << 3 | (frame[5] as usize) >> 5; - if frame[6] & 3 != 0 { - return None; - } - let hdr = if protection_absent { 7 } else { 9 }; - let end = len.min(frame.len()); - (end > hdr).then(|| (asc_bytes(profile + 1, sf, ch), &frame[hdr..end])) + let h = craft_audio::aac::AdtsHeader::parse(frame).ok().filter(|h| h.raw_data_blocks == 1)?; + let end = h.frame_length.min(frame.len()); + (end > h.header_length).then(|| (asc_bytes(h.object_type, h.sf_index, h.channel_config), &frame[h.header_length..end])) } -/// The parts of a LATM StreamMuxConfig (ISO/IEC 14496-3 §1.7.3) we decode: one program, one -/// layer, AAC-LC, variable frame length (type 0). -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct LatmConfig { - pub asc: Vec, - pub sample_rate: u32, - pub channels: u32, -} - -const AAC_RATES: [u32; 13] = [96_000, 88_200, 64_000, 48_000, 44_100, 32_000, 24_000, 22_050, 16_000, 12_000, 11_025, 8_000, 7_350]; - -fn latm_value(r: &mut filmcraft_bitstream::BitReader) -> Option { - let n = r.read_bits(2).ok()? + 1; - r.read_bits(8 * n).ok() -} - -/// StreamMuxConfig after `audioMuxVersion`, leaving the reader after it. -fn stream_mux_config(r: &mut filmcraft_bitstream::BitReader) -> Option { - let version = r.read_bits(1).ok()?; - let version_a = if version == 1 { r.read_bits(1).ok()? } else { 0 }; - if version_a != 0 { - return None; - } - if version == 1 { - latm_value(r)?; // taraBufferFullness - } - let _all_same_framing = r.read_bits(1).ok()?; - let sub_frames = r.read_bits(6).ok()?; - let programs = r.read_bits(4).ok()?; - let layers = r.read_bits(3).ok()?; - if sub_frames != 0 || programs != 0 || layers != 0 { - return None; - } - if version == 1 { - latm_value(r)?; // ascLen - } - // AudioSpecificConfig - let aot = r.read_bits(5).ok()?; - let sf = r.read_bits(4).ok()?; - if aot != 2 || sf == 15 { - return None; - } - let ch = r.read_bits(4).ok()?; - let frame_length_flag = r.read_bits(1).ok()?; - let depends_on_core = r.read_bits(1).ok()?; - let _extension = r.read_bits(1).ok()?; - if frame_length_flag != 0 || depends_on_core != 0 { - return None; - } - let frame_length_type = r.read_bits(3).ok()?; - if frame_length_type != 0 { - return None; - } - r.read_bits(8).ok()?; // latmBufferFullness - if r.read_bits(1).ok()? == 1 { - // otherData - if version == 1 { - latm_value(r)?; - } else { - loop { - let esc = r.read_bits(1).ok()?; - r.read_bits(8).ok()?; - if esc == 0 { - break; - } - } - } - } - if r.read_bits(1).ok()? == 1 { - r.read_bits(8).ok()?; // crcCheckSum - } - Some(LatmConfig { asc: asc_bytes(2, sf as u8, ch as u8), sample_rate: *AAC_RATES.get(sf as usize)?, channels: if ch == 0 { 2 } else { ch } }) -} - -/// The configuration carried in a LOAS frame (`None` if it reuses an earlier one or is not -/// supported). -pub fn latm_config(frame: &[u8]) -> Option { - if frame.len() < 4 || frame[0] != 0x56 || frame[1] & 0xE0 != 0xE0 { - return None; - } - let mut r = filmcraft_bitstream::BitReader::new(&frame[3..]); - if r.read_bits(1).ok()? == 1 { - return None; // useSameStreamMux - } - stream_mux_config(&mut r) -} - -/// A LOAS/LATM frame: its AudioSpecificConfig and the AAC payload of the single sub-frame. -/// `last` is the configuration of earlier frames (used by frames that set useSameStreamMux, -/// updated by frames that carry one). -pub fn latm_split(frame: &[u8], last: &mut Option) -> Option<(Vec, Vec)> { - if frame.len() < 4 || frame[0] != 0x56 || frame[1] & 0xE0 != 0xE0 { - return None; - } - let mut r = filmcraft_bitstream::BitReader::new(&frame[3..]); - let same = r.read_bits(1).ok()? == 1; - let cfg = if same { - last.clone()? - } else { - let c = stream_mux_config(&mut r)?; - *last = Some(c.clone()); - c - }; - // PayloadLengthInfo (frameLengthType 0) then PayloadMux, bit-aligned - let mut len = 0usize; - loop { - let b = r.read_bits(8).ok()? as usize; - len += b; - if b != 255 { - break; - } - } - // Each payload byte is a byte of this frame: a longer claim is truncated, and must not size the buffer. - if len > frame.len() { - return None; - } - let mut payload = Vec::with_capacity(len); - for _ in 0..len { - payload.push(r.read_bits(8).ok()? as u8); - } - Some((cfg.asc, payload)) -} +/// LOAS / LATM (AAC in MPEG transport streams): craft-audio reads the StreamMuxConfig and the +/// payload of the single sub-frame. +pub use craft_audio::aac::{LatmConfig, latm_config, latm_split}; /// Blu-ray / AVCHD LPCM packet header (4 bytes before the samples). #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -1055,23 +849,18 @@ pub(crate) fn check_decoded_budget(retained: usize, add: usize) -> Result<()> { Ok(()) } -/// The length of the ADTS frame starting `b` (ISO/IEC 13818-7 §6.2: 12-bit sync word, layer `00`, -/// 13-bit `frame_length` including the header), when it starts with a header and fits in `b`. +/// The length of the ADTS frame starting `b` ([`craft_audio::aac::AdtsHeader`]: sync word, layer +/// `00`, 13-bit `frame_length` including the header), when it starts with a header and fits in `b`. fn adts_frame_len(b: &[u8]) -> Option { - let [0xFF, b1, _, b3, b4, b5, ..] = *b else { return None }; - if b1 & 0xF6 != 0xF0 { - return None; - } - let len = ((b3 as usize & 3) << 11) | (b4 as usize) << 3 | (b5 as usize) >> 5; - let hdr = if b1 & 1 != 0 { 7 } else { 9 }; - (len > hdr && len <= b.len()).then_some(len) + let h = craft_audio::aac::AdtsHeader::parse(b).ok()?; + (h.frame_length > h.header_length && h.frame_length <= b.len()).then_some(h.frame_length) } /// A raw ADTS stream (`.aac`) decoded with our AAC decoder: (output sample rate, planar samples). /// Frames follow each other from the first header (after an ID3v2 tag); damaged data is skipped /// up to the next header, frames with another configuration than the first are skipped, and a -/// frame that does not decode keeps its duration as silence. HE-AAC is upsampled to its output -/// rate. `Ok(None)` when no frame decodes. +/// frame that does not decode keeps its duration as silence. HE-AAC decodes with its SBR high band +/// at its output rate. `Ok(None)` when no frame decodes. fn decode_adts(bytes: &[u8]) -> Result>)>> { let mut frames: Vec<(Vec, &[u8])> = Vec::new(); let mut p = id3v2_len(bytes); @@ -1261,23 +1050,29 @@ mod he_aac_tests { assert_eq!(aac_output_rate(&[0x12, 0x10], [&SBR_FILL[..]]), Some(44_100)); } + /// HE-AAC played at its output rate decodes through craft-audio's SBR tool (2048 samples an + /// access unit, the core's band carried through the filter banks), not an interpolated core; + /// the same stream at its core rate stays AAC-LC. A reset keeps the SBR choice. #[test] - fn upsampler_interpolates_continuously_across_packets() { - let f = 1000.0 / 22_050.0; - let x: Vec = (0..4096).map(|i| (2.0 * std::f32::consts::PI * f * i as f32).sin()).collect(); - let mut up = Upsample2x::new(1); - let mut y = Vec::new(); - for chunk in x.chunks(1024) { - y.extend(up.process(vec![chunk.to_vec()]).remove(0)); - } - assert_eq!(y.len(), 8192); - // output 2k + 1 is input k − UPSAMPLE_HALF + ½ (the look-ahead delay), within 0.1 % - let d = UPSAMPLE_HALF as f32; - for k in 100..4000 { - let want = (2.0 * std::f32::consts::PI * f * (k as f32 - d + 0.5)).sin(); - assert!((y[2 * k + 1] - want).abs() < 1e-3, "{k}: {} vs {want}", y[2 * k + 1]); - assert_eq!(y[2 * k], x[k - UPSAMPLE_HALF]); + fn he_aac_decodes_its_sbr_band_at_the_output_rate() { + let mut enc = craft_audio::aac::Encoder::new(craft_audio::aac::EncoderConfig::cbr(22_050, 2, 64_000)).unwrap(); + let tone: Vec = (0..22_050).map(|i| (i as f32 * std::f32::consts::TAU * 440.0 / 22_050.0).sin() * 0.5).collect(); + let mut aus = enc.encode(&[&tone, &tone]); + aus.extend(enc.flush()); + // explicit signalling: AOT 5, 22.05 kHz core, stereo, 44.1 kHz output + let he = [0x2B, 0x92, 0x08, 0x00]; + let mut dec = PacketDecoder::aac(&he, 44_100).unwrap(); + let mut core = PacketDecoder::aac(&he, 22_050).unwrap(); + let mut energy = 0.0f32; + for (i, au) in aus.iter().enumerate() { + let out = dec.decode(au, 0).unwrap(); + assert_eq!((out.len(), out[0].len()), (2, 2048), "access unit {i}"); + energy += out[0].iter().map(|v| v * v).sum::(); + assert_eq!(core.decode(au, 0).unwrap()[0].len(), 1024); } + assert!(energy > 1.0, "the core's tone comes through the SBR filter banks"); + dec.reset(); + assert_eq!(dec.decode(&aus[1], 0).unwrap()[0].len(), 2048); } } @@ -1336,6 +1131,27 @@ mod surround_order_tests { assert_eq!(reorder(chans.clone(), aac_to_wav_order(6)), chans); assert_eq!(reorder(vec![vec![0.0], vec![1.0], vec![2.0]], aac_to_wav_order(3)), vec![vec![1.0], vec![2.0], vec![0.0]]); } + + /// Configuration 0 with a program config element listing centre, front pair, back pair and LFE + /// (5.1 as configuration 6 lays it out) is reordered to WAV order like configuration 6; a + /// pair-first PCE (ffmpeg's 5.1(side)) already decodes in WAV order and is left alone. + #[test] + fn a_program_config_element_in_the_shape_of_5_1_is_reordered_like_configuration_6() { + use craft_audio::aac::{AudioSpecificConfig, ProgramConfig}; + let with = |front: Vec<(bool, u8)>, side: Vec<(bool, u8)>, back: Vec<(bool, u8)>, lfe: Vec| { + let mut pce = ProgramConfig::default(); + (pce.front, pce.side, pce.back, pce.lfe) = (front, side, back, lfe); + let mut asc = AudioSpecificConfig::lc(3, 0); + asc.pce = Some(pce); + asc + }; + assert!(aac_to_wav_order(6).is_some()); + let centre_first = with(vec![(false, 0), (true, 0)], vec![], vec![(true, 1)], vec![0]); + assert_eq!(aac_wav_order(¢re_first), aac_to_wav_order(6)); + let pair_first = with(vec![(true, 0), (false, 0)], vec![(false, 1)], vec![(true, 1)], vec![]); + assert_eq!(aac_wav_order(&pair_first), None); + assert_eq!(aac_wav_order(&AudioSpecificConfig::lc(3, 6)), aac_to_wav_order(6)); + } } #[cfg(test)] @@ -1403,7 +1219,7 @@ mod packet_time_tests { #[test] fn fixed_frame_lengths() { - // AAC-LC 48 kHz stereo; HE-AAC (24 kHz core) played at 48 kHz is upsampled + // AAC-LC 48 kHz stereo; HE-AAC (24 kHz core) played at 48 kHz decodes its SBR band assert_eq!(fixed_packet_samples(FixedFrames::Aac(&[0x11, 0x90]), &[], 48_000), Some(1024)); assert_eq!(fixed_packet_samples(FixedFrames::Aac(&[0x13, 0x10]), &[], 48_000), Some(2048)); assert_eq!(fixed_packet_samples(FixedFrames::Aac(&[]), &[], 48_000), None); diff --git a/crates/codecs/src/audio_panic_tests.rs b/crates/codecs/src/audio_panic_tests.rs index 209819c9b..9d1f2f7e9 100644 --- a/crates/codecs/src/audio_panic_tests.rs +++ b/crates/codecs/src/audio_panic_tests.rs @@ -172,6 +172,6 @@ fn a_latm_payload_length_beyond_the_frame_is_refused_before_it_is_reserved() { let mut frame = vec![0x56, 0xE0, 0x00]; frame.extend([0xFF; 20]); frame.push(0x7F); - let mut last = Some(LatmConfig { asc: vec![0x11, 0x90], sample_rate: 48_000, channels: 2 }); + let mut last = Some(LatmConfig::new(vec![0x11, 0x90], 48_000, 2)); assert_eq!(latm_split(&frame, &mut last), None); }