video: dispatch Motion JPEG through TrackDecoder (#2874)

This commit is contained in:
Terra854
2026-11-21 12:56:32 -05:00
committed by GitHub
parent 5d25fa3d22
commit 69b7468cd3
5 changed files with 57 additions and 5 deletions
+1
View File
@@ -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.
+10
View File
@@ -115,3 +115,13 @@ impl From<crate::vp8::Error> for VideoError {
VideoError::Malformed(format!("VP8: {e}"))
}
}
#[cfg(feature = "mjpeg")]
impl From<crate::mjpeg::Error> 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}")),
}
}
}
+4 -2
View File
@@ -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};
+19 -3
View File
@@ -41,6 +41,8 @@ enum Inner {
Ffv1(Box<crate::ffv1::Decoder>),
#[cfg(feature = "vp8-decode")]
Vp8(Box<crate::vp8::Decoder>),
#[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<P>(&mut self, pic: P, pts: i64) -> Result<Vec<VideoFrame>, VideoError>
where
VideoFrame: TryFrom<P, Error = FrameError>,
@@ -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
+23
View File
@@ -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());
}