FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Refactor dnx for strict Clippy checks
Dylan M. Taylor9e45a1af8ba6
diff --git a/crates/dnx/src/decode.rs b/crates/dnx/src/decode.rsindex ede98b0b3..66f99843a 100644--- a/crates/dnx/src/decode.rs+++ b/crates/dnx/src/decode.rs@@ -289,7 +289,7 @@ fn decode_row(ctx: &Ctx, data: &[u8], bands: [&mut [u16]; 4]) -> Result<Vec<boollet 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], comp, mb, xo as usize, yo as usize, mff, &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);@@ -318,7 +318,12 @@ fn decode_row(ctx: &Ctx, data: &[u8], bands: [&mut [u16]; 4]) -> Result<Vec<boollet 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], plane as usize, mb, xo as usize, yo as usize, false, &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,+ );}}}@@ -394,12 +399,21 @@ fn decode_block(ctx: &Ctx, r: &mut Reader, pred: &mut i32, wq: &[(u32, u32); 64]}/// 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], plane: usize, mb: usize, xo: usize, yo: usize, field: bool, block: &mut [f32; 64]) {+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 = mb * ctx.mb_size[plane].0 + xo;- let (first, step) = if field { (yo / 8, 2) } else { (yo, 1) };+ 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];diff --git a/crates/dnx/src/ffmpeg_probe.rs b/crates/dnx/src/ffmpeg_probe.rsindex 88cbaa818..1bb57024b 100644--- a/crates/dnx/src/ffmpeg_probe.rs+++ b/crates/dnx/src/ffmpeg_probe.rs@@ -12,7 +12,20 @@ use crate::header::{self, EOF_SIGNATURE, FieldCode, FrameHeader, SCAN_INDEX_OFFSuse crate::tables::{ZIGZAG, cid_info, vlc};use crate::{ChromaFormat, ColorVolume};-fn stream_cid(cid: u32, w: u32, h: u32, depth: u8, q: u32, pos: usize, amp: u32, neg: bool, blk: usize) -> Vec<u8> {+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<u8> {+ 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 };@@ -115,7 +128,17 @@ fn probe_weights() {for blk in [0usize, 2] {let mut grid = [[0f64; 8]; 8];for pos in 1..64 {- let s = stream_cid(cid, w, h, depth, q, pos, amp, false, blk);+ 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)@@ -212,7 +235,8 @@ fn probe_reconstruction() {for q in [1u32, 2, 3, 4, 8, 16] {let mut xs = vec![];for amp in 1..=40u32 {- let s = stream_cid(cid, w, h, depth, q, 1, amp, false, 0);+ 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)diff --git a/crates/dnx/src/tables.rs b/crates/dnx/src/tables.rsindex 905d808f1..e48acf832 100644--- a/crates/dnx/src/tables.rs+++ b/crates/dnx/src/tables.rs@@ -1,7 +1,10 @@//! 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::*;+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).@@ -53,9 +56,9 @@ pub(crate) struct CidInfo {pub p: u32,}-const fn hd(cid: u32, width: u16, height: u16, interlaced: bool, depth: u8, frame_size: u32, weights: &'static [[u8; 63]; 2], vlc: usize) -> CidInfo {+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: Kind::Hd { width, height, interlaced, depth, frame_size }, weights, vlc, p }+ CidInfo { cid, kind, weights, vlc, p }}const fn ri(cid: u32, c0: u32, weights: &'static [[u8; 63]; 2], vlc: usize) -> CidInfo {@@ -64,21 +67,21 @@ const fn ri(cid: u32, c0: u32, weights: &'static [[u8; 63]; 2], vlc: usize) -> C/// Every compression ID of SMPTE ST 2019-1:2016 (Tables C.1 and C.2).pub(crate) const CIDS: [CidInfo; 20] = [- hd(1235, 1920, 1080, false, 10, 917504, &W_D_1, 0),- hd(1237, 1920, 1080, false, 8, 606208, &W_D_2, 1),- hd(1238, 1920, 1080, false, 8, 917504, &W_D_3, 2),- hd(1241, 1920, 1080, true, 10, 917504, &W_D_4, 0),- hd(1242, 1920, 1080, true, 8, 606208, &W_D_5, 1),- hd(1243, 1920, 1080, true, 8, 917504, &W_D_6, 2),- hd(1244, 1440, 1080, true, 8, 606208, &W_D_7, 1),- hd(1250, 1280, 720, false, 10, 458752, &W_D_8, 3),- hd(1251, 1280, 720, false, 8, 458752, &W_D_9, 4),- hd(1252, 1280, 720, false, 8, 303104, &W_D_10, 5),- hd(1253, 1920, 1080, false, 8, 188416, &W_D_2, 1),- hd(1256, 1920, 1080, false, 10, 1835008, &W_D_11, 0),- hd(1258, 960, 720, false, 8, 212992, &W_D_10, 5),- hd(1259, 1440, 1080, false, 8, 417792, &W_D_2, 1),- hd(1260, 1440, 1080, true, 8, 417792, &W_D_7, 1),+ 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),diff --git a/crates/dnx/tests/it/common/mod.rs b/crates/dnx/tests/it/common/mod.rsindex ef0fef836..67858f079 100644--- a/crates/dnx/tests/it/common/mod.rs+++ b/crates/dnx/tests/it/common/mod.rs@@ -254,7 +254,18 @@ pub fn ffmpeg_decode(ff: &Path, mov: &Path, pix_fmt: &str) -> Vec<u16> {}/// Render a lavfi source to raw planar frames with ffmpeg and load them as [`Frame`]s.-pub fn source_frames(ff: &Path, name: &str, lavfi: &str, frames: u32, w: u32, h: u32, chroma: filmcraft_dnx::ChromaFormat, depth: u8) -> Vec<Frame> {+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<Frame> {+ 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",diff --git a/crates/dnx/tests/it/oracle_encode.rs b/crates/dnx/tests/it/oracle_encode.rsindex 4d5702698..6a60f1145 100644--- a/crates/dnx/tests/it/oracle_encode.rs+++ b/crates/dnx/tests/it/oracle_encode.rs@@ -29,7 +29,7 @@ fn encode_decodes_in_ffmpeg() {let mut cfg = EncoderConfig::new(c.profile, w, h);cfg.bit_depth = c.depth;let chroma = cfg.chroma();- let src = source_frames(&ff, c.name, c.lavfi, 2, w, h, chroma, c.depth);+ 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<Vec<u8>> = src.iter().map(|f| enc.encode(f).unwrap()).collect();