From fc4dddd7c70cfd21a70fbb5ea10d60f419bd37d1 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:23:42 +0100 Subject: [PATCH] video: decode Motion JPEG with craft-video's shared mjpeg codec Movie frames from MJPEG tracks were decoded with the image crate's JPEG decoder, and the synthetic MJPEG fixtures were encoded with its encoder. Both now go through craft_video::mjpeg (storytold/craft-libs#1757), a frame-level wrapper over craft-jpeg. The picture size is still checked from the header against MAX_PIXELS before any pixel is decoded, and grey and 12-bit pictures map to RGBA8. soundcraft-video no longer depends on the image crate. craft-video is pinned at craft-libs main 341020b2 with the mjpeg feature. Refs storytold/craft-libs#778, storytold/craft-libs#781. --- Cargo.lock | 354 ++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- crates/video/Cargo.toml | 1 - crates/video/src/movie.rs | 43 +++-- crates/video/src/synth.rs | 8 +- crates/video/src/tests.rs | 27 ++- 6 files changed, 401 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 994543817..9efe85db6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,6 +138,24 @@ dependencies = [ "memchr", ] +[[package]] +name = "aligned" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4508988c62edf04abd8d92897fca0c2995d907ce1dfeaf369dac3716a40685" +dependencies = [ + "as-slice", +] + +[[package]] +name = "aligned-vec" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b" +dependencies = [ + "equator", +] + [[package]] name = "allocator-api2" version = "0.2.21" @@ -256,6 +274,12 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + [[package]] name = "arboard" version = "3.7.0" @@ -277,6 +301,17 @@ dependencies = [ "x11rb", ] +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "arrayref" version = "0.3.9" @@ -295,6 +330,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "ash" version = "0.38.0+1.3.281" @@ -497,6 +541,40 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "av-scenechange" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f321d77c20e19b92c39e7471cf986812cbb46659d2af674adc4331ef3f18394" +dependencies = [ + "aligned", + "anyhow", + "arg_enum_proc_macro", + "arrayvec", + "log", + "num-rational", + "num-traits", + "pastey", + "rayon", + "thiserror 2.0.21", + "v_frame", + "y4m", +] + +[[package]] +name = "av1-grain" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cfddb07216410377231960af4fcab838eaa12e013417781b78bd95ee22077f8" +dependencies = [ + "anyhow", + "arrayvec", + "log", + "nom 8.0.0", + "num-rational", + "v_frame", +] + [[package]] name = "base64" version = "0.23.1" @@ -569,6 +647,15 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "bitstream-io" +version = "4.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eff00be299a18769011411c9def0d827e8f2d7bf0c3dbf53633147a8867fd1f" +dependencies = [ + "no_std_io2", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -633,6 +720,12 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "built" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0e531d93d39c34eef561e929e8a7f86d77a5af08aac4f6d6e39976c51858e9" + [[package]] name = "bumpalo" version = "3.20.3" @@ -771,7 +864,7 @@ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", "cpufeatures 0.3.1", - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -1066,7 +1159,7 @@ name = "craft-audio" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c" dependencies = [ - "craft-media", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c)", "log", "serde", "symphonia", @@ -1111,6 +1204,19 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c#341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-media" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c#341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c" + [[package]] name = "craft-media" version = "0.1.0" @@ -1213,9 +1319,11 @@ dependencies = [ [[package]] name = "craft-video" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c#45a8e16669e70dadb780dfbc759c882dd5b7ea1c" +source = "git+https://github.com/storytold/craft-libs?rev=341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c#341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c" dependencies = [ - "craft-media", + "craft-jpeg", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c)", + "rav1e", "rayon", "thiserror 2.0.21", ] @@ -1664,6 +1772,26 @@ name = "epaint_default_fonts" version = "0.37.0" source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" +[[package]] +name = "equator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc" +dependencies = [ + "equator-macro", +] + +[[package]] +name = "equator-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "equivalent" version = "1.0.3" @@ -1981,7 +2109,7 @@ dependencies = [ "js-sys", "libc", "r-efi 6.0.0", - "rand_core", + "rand_core 0.10.1", "wasm-bindgen", ] @@ -2416,8 +2544,6 @@ dependencies = [ "num-traits", "png", "tiff", - "zune-core", - "zune-jpeg", ] [[package]] @@ -2430,6 +2556,17 @@ dependencies = [ "hashbrown 0.17.1", ] +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "is-docker" version = "0.2.0" @@ -2464,6 +2601,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.15.0" @@ -2619,6 +2765,16 @@ version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" +[[package]] +name = "libfuzzer-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2" +dependencies = [ + "arbitrary", + "cc", +] + [[package]] name = "libloading" version = "0.8.9" @@ -2711,6 +2867,16 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if", + "rayon", +] + [[package]] name = "memchr" version = "2.8.3" @@ -2863,6 +3029,12 @@ dependencies = [ "jni-sys 0.3.1", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nix" version = "0.31.3" @@ -2875,6 +3047,15 @@ dependencies = [ "libc", ] +[[package]] +name = "no_std_io2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003" +dependencies = [ + "memchr", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -2900,6 +3081,22 @@ dependencies = [ "memchr", ] +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-complex" version = "0.4.6" @@ -2935,6 +3132,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -3426,6 +3634,18 @@ dependencies = [ "windows-link", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pastey" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec" + [[package]] name = "peniko" version = "0.6.1" @@ -3651,6 +3871,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "presser" version = "0.3.1" @@ -3699,6 +3928,19 @@ name = "profiling" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +dependencies = [ + "profiling-procmacros", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb" +dependencies = [ + "quote", + "syn 2.0.119", +] [[package]] name = "proptest" @@ -3712,7 +3954,7 @@ dependencies = [ "chacha20", "core_detect", "num-traits", - "rand", + "rand 0.10.3", "rand_xorshift", "regex-syntax", "rusty-fork", @@ -3777,6 +4019,16 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha", + "rand_core 0.9.5", +] + [[package]] name = "rand" version = "0.10.3" @@ -3784,7 +4036,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ "getrandom 0.4.3", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", ] [[package]] @@ -3799,7 +4070,7 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" dependencies = [ - "rand_core", + "rand_core 0.10.1", ] [[package]] @@ -3808,6 +4079,40 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" +[[package]] +name = "rav1e" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b6dd56e85d9483277cde964fd1bdb0428de4fec5ebba7540995639a21cb32b" +dependencies = [ + "aligned-vec", + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av-scenechange", + "av1-grain", + "bitstream-io", + "built", + "cfg-if", + "interpolate_name", + "itertools 0.14.0", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "new_debug_unreachable", + "noop_proc_macro", + "num-derive", + "num-traits", + "paste", + "profiling", + "rand 0.9.5", + "rand_chacha", + "simd_helpers", + "thiserror 2.0.21", + "v_frame", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -4323,6 +4628,15 @@ dependencies = [ "simdutf8", ] +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + [[package]] name = "simdutf8" version = "0.1.5" @@ -4641,9 +4955,8 @@ dependencies = [ name = "soundcraft-video" version = "0.20.1" dependencies = [ - "craft-media", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=45a8e16669e70dadb780dfbc759c882dd5b7ea1c)", "craft-video", - "image", "proptest", "serde", "thiserror 2.0.21", @@ -5393,6 +5706,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "v_frame" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2" +dependencies = [ + "aligned-vec", + "num-traits", + "wasm-bindgen", +] + [[package]] name = "vello_common" version = "0.2.0" @@ -6320,6 +6644,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "y4m" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" + [[package]] name = "yoke" version = "0.8.4" diff --git a/Cargo.toml b/Cargo.toml index d08b3af36..5c6345b47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l # Video containers and bitstreams, shared with the other craft apps. craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c" } # H.264 and ProRes codecs shared with the other Craft apps. -craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "45a8e16669e70dadb780dfbc759c882dd5b7ea1c", default-features = false, features = ["h264-decode", "prores"] } +craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "341020b2c1c8b5ebc62c7dc5c1c99a37cfa4d43c", default-features = false, features = ["h264-decode", "prores", "mjpeg"] } # Audio file formats, codecs and waveform overviews, shared with the other craft apps. It shares # types with craft-media, so both stay on one rev. # Standard MIDI Files and the note/controller model, shared with the other craft apps. diff --git a/crates/video/Cargo.toml b/crates/video/Cargo.toml index 4e588876c..7d3d5c08f 100644 --- a/crates/video/Cargo.toml +++ b/crates/video/Cargo.toml @@ -18,7 +18,6 @@ craft-media.workspace = true craft-video.workspace = true thiserror.workspace = true serde.workspace = true -image = { workspace = true, features = ["jpeg"] } [dev-dependencies] proptest.workspace = true diff --git a/crates/video/src/movie.rs b/crates/video/src/movie.rs index 789eccbb3..b71b88fda 100644 --- a/crates/video/src/movie.rs +++ b/crates/video/src/movie.rs @@ -418,20 +418,14 @@ impl Movie { Ok(Frame { width: f.width, height: f.height, rgba, index, time }) } Codec::Jpeg => { - // The frame's own size, from its header, before a pixel is decoded: a frame may differ from - // the track's size (as before), but not exceed `MAX_PIXELS`. - let reader = image::ImageReader::with_format(std::io::Cursor::new(&data), image::ImageFormat::Jpeg); - let (w, h) = reader.into_dimensions().map_err(|e| VideoError::Decode(e.to_string()))?; - if u64::from(w) * u64::from(h) > MAX_PIXELS { - return Err(VideoError::Decode("picture too large".into())); - } - let img = image::load_from_memory_with_format(&data, image::ImageFormat::Jpeg).map_err(|e| VideoError::Decode(e.to_string()))?; - let rgba = img.to_rgba8(); - let (width, height) = rgba.dimensions(); - if u64::from(width) * u64::from(height) > MAX_PIXELS { - return Err(VideoError::Decode("picture too large".into())); - } - Ok(Frame { width, height, rgba: rgba.into_raw(), index, time }) + // The frame's own size is checked from its header before a pixel is decoded: a frame may + // differ from the track's size (as before), but not exceed `MAX_PIXELS`. + let decoded = craft_video::mjpeg::Decoder::new().max_pixels(MAX_PIXELS).decode(&data).map_err(|e| match e { + craft_video::mjpeg::Error::Limit(_) => VideoError::Decode("picture too large".into()), + e => VideoError::Decode(e.to_string()), + })?; + let rgba = mjpeg_rgba(&decoded)?; + Ok(Frame { width: decoded.width, height: decoded.height, rgba, index, time }) } _ => Err(VideoError::Unsupported("not an intra-frame codec".into())), } @@ -563,3 +557,24 @@ impl Movie { Err(VideoError::Decode(last_err.unwrap_or_else(|| format!("frame {index} was not produced")))) } } + +/// A decoded Motion JPEG picture as RGBA8: grey is spread over R, G and B, and 12-bit samples +/// (scaled to 16 bits by the decoder) keep their top 8 bits. +pub(crate) fn mjpeg_rgba(frame: &craft_video::mjpeg::Frame) -> Result> { + let channels = usize::from(frame.channels); + if channels != 1 && channels != 3 { + return Err(VideoError::Decode(format!("{channels}-channel Motion JPEG picture"))); + } + let samples: Vec = match &frame.samples { + craft_video::mjpeg::Samples::U8(p) => p.clone(), + craft_video::mjpeg::Samples::U16(p) => p.iter().map(|&v| (v >> 8) as u8).collect(), + }; + Ok(samples + .chunks_exact(channels) + .flat_map(|px| match *px { + [g] => [g, g, g, 255], + [r, g, b] => [r, g, b, 255], + _ => [0, 0, 0, 255], + }) + .collect()) +} diff --git a/crates/video/src/synth.rs b/crates/video/src/synth.rs index 2661a5141..753ed7948 100644 --- a/crates/video/src/synth.rs +++ b/crates/video/src/synth.rs @@ -1,6 +1,6 @@ //! Tiny synthetic movies, generated in code (for tests, demos and screenshots; nothing binary is //! committed). H.264 pictures are made of I_PCM macroblocks, so their decoded samples are known -//! exactly; ProRes comes from the bundled encoder and Motion JPEG from the `image` encoder. All +//! exactly; ProRes comes from the bundled encoder and Motion JPEG from craft-video's `mjpeg` encoder. All //! are muxed with the bundled MP4/MOV writer. use crate::bitstream::{BitWriter, escape_rbsp}; @@ -174,10 +174,10 @@ pub fn mjpeg_mov(n: usize) -> Result> { let (w, h) = (32u32, 24u32); let mut mw = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).map_err(err)?; let t = mw.add_track(TrackConfig::new(SampleEntry::jpeg(w as u16, h as u16), 10)).map_err(err)?; + let encoder = craft_video::mjpeg::Encoder::new(craft_video::mjpeg::EncoderConfig { quality: 95, ..Default::default() }).map_err(err)?; for i in 0..n.min(10_000) { - let img = image::RgbImage::from_pixel(w, h, image::Rgb([(30 * (i % 9)) as u8; 3])); - let mut jpg = Vec::new(); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpg, 95).encode_image(&img).map_err(err)?; + let pixels = vec![(30 * (i % 9)) as u8; (w * h * 3) as usize]; + let jpg = encoder.encode(w, h, 3, &pixels).map_err(err)?; mw.write_sample(t, WriteSample { data: &jpg, duration: 1, composition_offset: 0, is_sync: true }).map_err(err)?; } Ok(mw.finish().map_err(err)?.into_inner()) diff --git a/crates/video/src/tests.rs b/crates/video/src/tests.rs index 85bd573bc..27d8c790f 100644 --- a/crates/video/src/tests.rs +++ b/crates/video/src/tests.rs @@ -1,6 +1,6 @@ //! Movie tests on fixtures generated in process (no binary fixtures): H.264 made of I_PCM -//! macroblocks (exact, known colours), ProRes from the bundled encoder and Motion JPEG from the -//! `image` encoder, muxed with the bundled MP4/MOV writer. When `ffmpeg` with libx264 is on PATH, +//! macroblocks (exact, known colours), ProRes from the bundled encoder and Motion JPEG from craft-video's +//! `mjpeg` encoder, muxed with the bundled MP4/MOV writer. When `ffmpeg` with libx264 is on PATH, //! real High-profile streams with B-frames are checked too (skipped otherwise). use crate::isobmff::{Brand, ByteSource, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions}; @@ -345,3 +345,26 @@ fn test_pattern_moves() { let f17 = m.frame_at(17.5 / 24.0).unwrap(); assert_eq!(white_x(&f17), Some(17)); } + +#[test] +fn mjpeg_grey_twelve_bit_and_odd_channel_pictures_map_to_rgba() { + use craft_video::mjpeg::{Frame as Jpeg, Samples}; + let grey = Jpeg { width: 2, height: 1, channels: 1, samples: Samples::U8(vec![7, 200]), damaged: false }; + assert_eq!(crate::movie::mjpeg_rgba(&grey).unwrap(), [7, 7, 7, 255, 200, 200, 200, 255]); + // 12-bit samples arrive scaled to 16 bits; the top 8 bits are kept. + let deep = Jpeg { width: 1, height: 1, channels: 3, samples: Samples::U16(vec![0xFFFF, 0x8000, 0x00FF]), damaged: false }; + assert_eq!(crate::movie::mjpeg_rgba(&deep).unwrap(), [255, 128, 0, 255]); + let odd = Jpeg { width: 1, height: 1, channels: 2, samples: Samples::U8(vec![1, 2]), damaged: false }; + assert!(matches!(crate::movie::mjpeg_rgba(&odd), Err(VideoError::Decode(e)) if e.contains("2-channel"))); +} + +#[test] +fn grey_mjpeg_packets_decode_through_the_shared_decoder() { + // A one-channel packet, as some cameras write, decodes to equal R, G and B. + let enc = craft_video::mjpeg::Encoder::new(craft_video::mjpeg::EncoderConfig { quality: 95, ..Default::default() }).unwrap(); + let packet = enc.encode(16, 8, 1, &[90; 16 * 8]).unwrap(); + let frame = craft_video::mjpeg::Decoder::new().decode(&packet).unwrap(); + let rgba = crate::movie::mjpeg_rgba(&frame).unwrap(); + assert_eq!(rgba.len(), 16 * 8 * 4); + assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[0] == p[1] && p[1] == p[2] && p[0].abs_diff(90) <= 2 && p[3] == 255)); +}