diff --git a/crates/video/README.md b/crates/video/README.md index 0413e658d..257b44369 100644 --- a/crates/video/README.md +++ b/crates/video/README.md @@ -75,6 +75,7 @@ is built with any of the decoder features below and decodes the codecs whose fea | DNxHD / DNxHR | `dnx` | ignored (frame headers carry the format) | | APV | `apv-decode` | ignored (access units carry the format) | | FFV1 | `ffv1` | required version 3 configuration record; container width and height are required too | +| Motion JPEG | `mjpeg` | ignored (each packet carries its own picture) | | VP8 | `vp8-decode` | ignored (key frames carry the format) | Configuration that holds picture data (a slice, a frame OBU, an MPEG-2 picture) is refused. diff --git a/crates/video/src/error.rs b/crates/video/src/error.rs index 44be75b66..bd8faa339 100644 --- a/crates/video/src/error.rs +++ b/crates/video/src/error.rs @@ -115,3 +115,13 @@ impl From for VideoError { VideoError::Malformed(format!("VP8: {e}")) } } + +#[cfg(feature = "mjpeg")] +impl From for VideoError { + fn from(e: crate::mjpeg::Error) -> Self { + match e { + crate::mjpeg::Error::Unsupported(_) => VideoError::Unsupported(format!("Motion JPEG: {e}")), + _ => VideoError::Malformed(format!("Motion JPEG: {e}")), + } + } +} diff --git a/crates/video/src/lib.rs b/crates/video/src/lib.rs index b17d58688..34f8da1c0 100644 --- a/crates/video/src/lib.rs +++ b/crates/video/src/lib.rs @@ -51,7 +51,8 @@ pub use error::VideoError; feature = "dnx", feature = "apv-decode", feature = "ffv1", - feature = "vp8-decode" + feature = "vp8-decode", + feature = "mjpeg" ))] mod track; #[cfg(any( @@ -64,7 +65,8 @@ mod track; feature = "dnx", feature = "apv-decode", feature = "ffv1", - feature = "vp8-decode" + feature = "vp8-decode", + feature = "mjpeg" ))] pub use track::{TrackDecoder, TrackDecoderOptions}; diff --git a/crates/video/src/track.rs b/crates/video/src/track.rs index fa7be7e51..fbd3bc118 100644 --- a/crates/video/src/track.rs +++ b/crates/video/src/track.rs @@ -41,6 +41,8 @@ enum Inner { Ffv1(Box), #[cfg(feature = "vp8-decode")] Vp8(Box), + #[cfg(feature = "mjpeg")] + Mjpeg(crate::mjpeg::Decoder), } /// A decoder for one video track, whatever its codec: built from the track's [`CodecParams`], @@ -57,6 +59,7 @@ enum Inner { /// | DNxHD / DNxHR | `dnx` | ignored (frame headers carry the format) | /// | APV | `apv-decode` | ignored (access units carry the format) | /// | FFV1 | `ffv1` | required version 3 configuration record; container width and height are required too | +/// | Motion JPEG | `mjpeg` | ignored (each packet carries its own picture) | /// | VP8 | `vp8-decode` | ignored (key frames carry the format) | /// /// With an `avcC` or `hvcC` record, packets are length-prefixed NAL units, as MP4 and Matroska @@ -67,10 +70,10 @@ enum Inner { /// metadata, and MPEG-2 `extradata` holds sequence headers, their extensions, user data and GOP /// headers. /// -/// ProRes, DNx, APV and FFV1 return one frame per packet. APV selects the first decodable +/// ProRes, DNx, APV, FFV1 and Motion JPEG return one frame per packet. APV selects the first decodable /// primary frame and its alpha, as the codec's default decoder does. FFV1 supports version 3, /// range-coded key frames. VP8 hidden frames update references without returning a picture. -/// These five decoders have no delayed output; reset keeps FFV1 configuration and dimensions. +/// These decoders have no delayed output; reset keeps FFV1 configuration and dimensions. /// /// Other codecs are [`VideoError::Unsupported`], as is a codec whose decoder feature is off. /// @@ -187,6 +190,8 @@ impl TrackDecoder { Inner::Ffv1(_) => {} #[cfg(feature = "vp8-decode")] Inner::Vp8(_) => {} + #[cfg(feature = "mjpeg")] + Inner::Mjpeg(_) => {} } } @@ -242,6 +247,11 @@ impl TrackDecoder { let pic = dec.decode(packet)?; self.single(pic, pts) } + #[cfg(feature = "mjpeg")] + Inner::Mjpeg(dec) => { + let pic = dec.decode(packet)?; + self.single(pic, pts) + } #[cfg(feature = "vp8-decode")] Inner::Vp8(dec) => { let pic = dec.decode(packet, pts)?; @@ -293,6 +303,8 @@ impl TrackDecoder { Inner::Ffv1(_) => Ok(std::mem::take(&mut self.held)), #[cfg(feature = "vp8-decode")] Inner::Vp8(_) => Ok(std::mem::take(&mut self.held)), + #[cfg(feature = "mjpeg")] + Inner::Mjpeg(_) => Ok(std::mem::take(&mut self.held)), } } @@ -348,6 +360,8 @@ impl TrackDecoder { Inner::Apv => Ok(()), #[cfg(feature = "ffv1")] Inner::Ffv1(_) => Ok(()), + #[cfg(feature = "mjpeg")] + Inner::Mjpeg(_) => Ok(()), #[cfg(feature = "vp8-decode")] Inner::Vp8(dec) => { dec.reset(); @@ -357,7 +371,7 @@ impl TrackDecoder { } /// Frame-only decoders do not take timestamps; attach the packet timestamp after conversion. - #[cfg(any(feature = "prores", feature = "dnx", feature = "apv-decode", feature = "ffv1"))] + #[cfg(any(feature = "prores", feature = "dnx", feature = "apv-decode", feature = "ffv1", feature = "mjpeg"))] fn single

(&mut self, pic: P, pts: i64) -> Result, VideoError> where VideoFrame: TryFrom, @@ -458,6 +472,8 @@ impl Inner { CodecId::Apv => Ok(Inner::Apv), #[cfg(feature = "vp8-decode")] CodecId::Vp8 => Ok(Inner::Vp8(Box::default())), + #[cfg(feature = "mjpeg")] + CodecId::Mjpeg => Ok(Inner::Mjpeg(crate::mjpeg::Decoder::new())), #[cfg(feature = "ffv1")] CodecId::Ffv1 => { let (width, height) = params diff --git a/crates/video/tests/mjpeg.rs b/crates/video/tests/mjpeg.rs index fa64dd9f4..d5adf2bd6 100644 --- a/crates/video/tests/mjpeg.rs +++ b/crates/video/tests/mjpeg.rs @@ -248,3 +248,26 @@ fn adobe_transform_zero_keeps_numeric_id_components_as_rgb() { } } } + +#[test] +fn track_dispatch_preserves_mjpeg_frames_and_timestamps() { + use craft_media::{CodecId, CodecParams}; + use craft_video::{TrackDecoder, VideoError, frame::VideoFrame}; + + let enc = Encoder::default(); + let direct = Decoder::new(); + let mut track = TrackDecoder::new(&CodecParams::new(CodecId::Mjpeg)).expect("Motion JPEG track"); + let color = enc.encode(17, 11, 3, &picture(17, 11, 3)).expect("RGB packet"); + let gray = enc.encode(13, 9, 1, &picture(13, 9, 1)).expect("gray packet"); + let mut want_color = VideoFrame::try_from(direct.decode(&color).expect("RGB picture")).expect("RGB frame"); + want_color.pts = Some(71); + assert_eq!(track.decode(&color, 71).expect("RGB track picture"), [want_color]); + assert!(matches!(track.decode(&[], 72), Err(VideoError::Malformed(_)))); + assert!(track.flush().expect("flush").is_empty()); + + track.reset().expect("seek to an earlier packet"); + let mut want_gray = VideoFrame::try_from(direct.decode(&gray).expect("gray picture")).expect("gray frame"); + want_gray.pts = Some(-3); + assert_eq!(track.decode(&gray, -3).expect("gray track picture"), [want_gray]); + assert!(track.flush().expect("flush").is_empty()); +}