Repository navigation
Add native NTSC DV25 decoding for MOV imports #2092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andymenard
wants to merge
7
commits into
storytold:main
Choose a base branch
from
andymenard:codex/ntsc-dv25-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
30a96f7
codecs: add native NTSC DV25 MOV decoding
andymenard e4adc69
dv: run the DV oracle tests through filmcraft-testkit and cover 2-4-8
andymenard d5c7cf0
dv: look up the scan order instead of searching it
andymenard 6c32670
docs: date the DV entries in ROADMAP, codec-parity and gaps
andymenard 8609540
dv: conceal damaged segments instead of failing the frame
andymenard 38ae816
dv: check both chroma planes in the concealment test
andymenard f1aa1c3
dv: test every kind of segment damage, and payload flips on real frames
andymenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //! NTSC DV25 QuickTime (`dvc `) with big-endian PCM against ffmpeg: frames requested out of order | ||
| //! and then sequentially are the right frames within the `filmcraft-dv` oracle tolerance, the | ||
| //! 4:1:1 chroma reaches the compositor replicated (not resampled) into 4:2:2, and the audio is | ||
| //! sample-exact. | ||
|
|
||
| use crate::common; | ||
|
|
||
| use common::*; | ||
| use filmcraft_media::FrameRequest; | ||
|
|
||
| const W: usize = 720; | ||
| const H: usize = 480; | ||
|
|
||
| #[test] | ||
| fn native_dv_mov_with_big_endian_pcm_and_random_seeking() { | ||
| let ff = filmcraft_testkit::require_ffmpeg!(); | ||
| let name = "native_dv_ntsc_pcm.mov"; | ||
| let args = [ | ||
| "-f", | ||
| "lavfi", | ||
| "-i", | ||
| "testsrc2=size=720x480:rate=30000/1001:duration=1", | ||
| "-f", | ||
| "lavfi", | ||
| "-i", | ||
| "sine=frequency=440:sample_rate=48000:duration=1", | ||
| "-c:v", | ||
| "dvvideo", | ||
| "-pix_fmt", | ||
| "yuv411p", | ||
| "-threads", | ||
| "1", | ||
| "-c:a", | ||
| "pcm_s16be", | ||
| "-ac", | ||
| "2", | ||
| "-shortest", | ||
| ]; | ||
| let f = fixture(&ff, name, &args).unwrap_or_else(|| panic!("could not write {name}")); | ||
| let src = filmcraft_codecs::open_bytes(name, bytes(&f)).unwrap(); | ||
| let video = src.info().video.clone().unwrap(); | ||
| assert_eq!(video.codec, "DV / DVCPRO NTSC"); | ||
| assert_eq!(video.pixel_format, "YUV 4:1:1 8-bit"); | ||
| assert_eq!(video.frame_rate, filmcraft_time::FrameRate::FPS_29_97); | ||
| assert_eq!((video.width, video.height), (W as u32, H as u32)); | ||
|
|
||
| let reference = ffmpeg_frames(&ff, &f, "yuv411p"); | ||
| let frame_len = W * H + 2 * (W / 4) * H; | ||
| // 29 frames: `-shortest` ends the 29.97 fps video with the one-second sine. | ||
| let frames = reference.len() / frame_len; | ||
| assert_eq!((frames, reference.len() % frame_len), (29, 0)); | ||
| // Back and forth, then sequential playback. testsrc2 changes every frame, so a seek that lands | ||
| // on a neighbouring frame is far outside the tolerance. | ||
| for k in [frames - 1, 1, 18, 0, 15].into_iter().chain(20..26) { | ||
| let frame = src.video_frame(FrameRequest::full(video.frame_rate.tick_of(k as i64))).unwrap(); | ||
| assert_eq!((frame.width, frame.height), (W as u32, H as u32)); | ||
| let ours = planes(&frame); | ||
| for c in &ours[1..] { | ||
| assert_eq!(c.len(), (W / 2) * H, "frame {k}: chroma is not 4:2:2"); | ||
| assert!(c.as_chunks::<2>().0.iter().all(|[a, b]| a == b), "frame {k}: chroma is not replicated"); | ||
| } | ||
| let ours = [ours[0].clone(), ours[1].iter().step_by(2).copied().collect(), ours[2].iter().step_by(2).copied().collect()]; | ||
| let want = raw_planes(&reference[k * frame_len..(k + 1) * frame_len], W, H, W / 4, H, 1); | ||
| let max = max_diff(&ours, &want); | ||
| assert!(max <= 3, "frame {k}: max difference {max} against ffmpeg"); | ||
| } | ||
|
|
||
| let want = ffmpeg_audio_f32(&ff, &f, &[]); | ||
| let got = src.audio(0, want.len() / 2, 48_000).unwrap(); | ||
| assert_eq!(got.channels.len(), 2); | ||
| let interleaved: Vec<f32> = (0..want.len() / 2).flat_map(|i| [got.channels[0][i], got.channels[1][i]]).collect(); | ||
| assert_eq!(interleaved, want, "PCM differs from ffmpeg"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "filmcraft-dv" | ||
| description = "Clean-room SMPTE 314M DV25 / DVCPRO25 video decoder" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| rust-version.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [dev-dependencies] | ||
| filmcraft-testkit = { workspace = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Native DV25 / DVCPRO25 decoding | ||
|
|
||
| Clean-room pure-Rust decoder for the 525/60, 25 Mb/s, 4:1:1 video used by | ||
| NTSC DV/DVCPRO QuickTime (`dvc `) footage. Implemented from **SMPTE 314M-2005** | ||
| (stabilized 2013), sections 4.7 and 5, tables 23–25 and figures 20, 22, 26–28, 30, 32. | ||
| Specification: https://pub.smpte.org/pub/st314/st0314-2005_stable2013.pdf | ||
|
|
||
| Implements DIF indexing, three-pass AC bit allocation, VLC and escape codes, | ||
| inverse quantization/weighting, 8x8 and 2-4-8 inverse DCT, macroblock unshuffling, | ||
| and the clipped rightmost chroma blocks. Four abbreviated printed VLC words | ||
| (run 7 and 8 with amplitude 1; run 1 with amplitudes 16 and 17) are expanded to | ||
| Table 24's lengths to obtain prefix-free codes. FFmpeg's encoder emits all four, and | ||
| they are frequent in camera footage, which decodes within the same tolerance of FFmpeg | ||
| (not committed). All frame sizes, coefficient runs and block locations are bounded. | ||
|
|
||
| Returns original 720x480, 8-bit planar 4:1:1 pictures with the original field order. | ||
| FilmCraft's adapter represents the chroma in its existing 4:2:2 compositor by | ||
| replicating samples; the MOV source continues handling clean aperture, pixel aspect, | ||
| timecode, audio and seeking. No external decoder, transcoded file, or FFmpeg runtime | ||
| dependency is used. PCM audio uses FilmCraft's existing native decoder. | ||
|
|
||
| Currently scoped to NTSC DV25/DVCPRO25 MOV. PAL DV, DV50/DVCPRO50, DVCPRO HD, | ||
| raw DV audio demultiplexing and AVI DV are not implemented. Unsupported packets and | ||
| frames with a wrong size, header or DIF IDs return a decoding error without changing the | ||
| input or crashing. Damaged coded data (a macroblock flagged as damaged, an overlong | ||
| coefficient run or a missing end of block) conceals only its video segment, which keeps | ||
| black luma and neutral chroma, and the rest of the frame is decoded. With its escapes the | ||
| VLC table covers every bit pattern, so corrupt AC bits surface as one of the latter two. | ||
|
|
||
| `cargo test -p filmcraft-dv` covers spatial coverage, malformed inputs, generated | ||
| progressive/interlaced patterns versus an external FFmpeg oracle (maximum sample | ||
| difference 3, mean square error below 0.5, over the whole picture and over the | ||
| clipped rightmost chroma columns), and bit-flip/truncation mutation tests. | ||
| FFmpeg's encoder never codes chroma with the 2-4-8 DCT, so two patterns set the | ||
| mode bit of every chroma block before both decoders read them; the interlaced | ||
| pattern is encoded with `+ildct` so luma 2-4-8 blocks are exercised too. The | ||
| fixtures are generated into `target/fixtures/dv/` (`cargo xtask fixtures dv`), and | ||
| `FILMCRAFT_REQUIRE_ORACLES=1` turns a missing ffmpeg into a failure. | ||
| The standalone example decodes DIF test fixtures to planar YUV for oracle comparisons. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| use std::{ | ||
| fs::File, | ||
| io::{BufReader, BufWriter, Read, Write}, | ||
| }; | ||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let mut args = std::env::args_os().skip(1); | ||
| let mut input = BufReader::new(File::open(args.next().ok_or("input required")?)?); | ||
| let mut out = BufWriter::new(File::create_new(args.next().ok_or("output required")?)?); | ||
| let mut data = vec![0; 120000]; | ||
| loop { | ||
| if input.read(&mut data[..1])? == 0 { | ||
| break; | ||
| } | ||
| input.read_exact(&mut data[1..])?; | ||
| let f = filmcraft_dv::decode_frame(&data)?; | ||
| out.write_all(&f.y)?; | ||
| out.write_all(&f.cb)?; | ||
| out.write_all(&f.cr)?; | ||
| } | ||
| out.flush()?; | ||
| Ok(()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.