mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-23 02:51:42 +01:00
Merge pull request #2351 from alehel/codex/video-track-remaining-decoders
craft-video: TrackDecoder for ProRes, DNx, APV, FFV1 and VP8 (#1231)
This commit is contained in:
9 files changed
+356
-11
No files matched your search
@@ -258,6 +258,7 @@ impl Stream {
|
||||
b"HEVC" | b"H265" | b"HEV1" | b"HVC1" | b"X265" => Id::Hevc,
|
||||
b"DIVX" | b"DX50" | b"XVID" | b"FMP4" | b"MP4V" | b"M4S2" | b"3IV2" => Id::Mpeg4Video,
|
||||
b"MPG2" | b"MPEG" | b"MPG1" => Id::Mpeg2Video,
|
||||
b"FFV1" => Id::Ffv1,
|
||||
b"VP80" => Id::Vp8,
|
||||
b"VP90" => Id::Vp9,
|
||||
b"AV01" => Id::Av1,
|
||||
|
||||
@@ -546,6 +546,7 @@ fn codec_params_map_fourccs_and_format_tags() {
|
||||
(b"XVID", CodecId::Mpeg4Video),
|
||||
(b"apch", CodecId::ProRes),
|
||||
(b"AVdn", CodecId::Dnxhd),
|
||||
(b"FFV1", CodecId::Ffv1),
|
||||
(b"YUY2", CodecId::Unknown),
|
||||
(&[0; 4], CodecId::Unknown),
|
||||
] {
|
||||
|
||||
@@ -121,6 +121,8 @@ pub enum CodecId {
|
||||
Vp8,
|
||||
/// VP9.
|
||||
Vp9,
|
||||
/// FFV1 lossless video.
|
||||
Ffv1,
|
||||
/// Apple ProRes.
|
||||
ProRes,
|
||||
/// Motion JPEG.
|
||||
@@ -168,6 +170,7 @@ impl CodecId {
|
||||
CodecId::Av1 => "AV1",
|
||||
CodecId::Vp8 => "VP8",
|
||||
CodecId::Vp9 => "VP9",
|
||||
CodecId::Ffv1 => "FFV1",
|
||||
CodecId::ProRes => "ProRes",
|
||||
CodecId::Mjpeg => "Motion JPEG",
|
||||
CodecId::Mpeg2Video => "MPEG-2",
|
||||
@@ -214,6 +217,7 @@ impl CodecId {
|
||||
| CodecId::Av1
|
||||
| CodecId::Vp8
|
||||
| CodecId::Vp9
|
||||
| CodecId::Ffv1
|
||||
| CodecId::ProRes
|
||||
| CodecId::Mjpeg
|
||||
| CodecId::Mpeg2Video
|
||||
@@ -271,6 +275,7 @@ impl PcmFormat {
|
||||
/// | H.264 / HEVC / AV1 | the `avcC` / `hvcC` / `av1C` record |
|
||||
/// | VP9 | the MP4 `vpcC` payload, or Matroska's codec feature metadata |
|
||||
/// | APV | the `apvC` record |
|
||||
/// | FFV1 | the version 3 configuration record (dimensions are required separately) |
|
||||
/// | VMD video, VMD audio | the 816-byte VMD file header |
|
||||
///
|
||||
/// `sample_rate` and `channels` are what the container declares (for HE-AAC with implicit
|
||||
|
||||
@@ -258,6 +258,11 @@ impl Track {
|
||||
Codec::Mp3 => (Id::Mp3, Vec::new()),
|
||||
Codec::Mp2 => (Id::Mp2, Vec::new()),
|
||||
Codec::Mp1 => (Id::Mp1, Vec::new()),
|
||||
Codec::Other(id) if id == "V_FFV1" => (Id::Ffv1, self.codec_private.clone()),
|
||||
// VfW CodecPrivate starts with the 40-byte BITMAPINFOHEADER.
|
||||
Codec::VfwFourcc { fourcc: [b'F', b'F', b'V', b'1'], bitmap_info_header } => {
|
||||
(Id::Ffv1, bitmap_info_header.get(40..).unwrap_or_default().to_vec())
|
||||
}
|
||||
// the ALACSpecificConfig, as MP4's `alac` box holds it after its FullBox header
|
||||
Codec::Other(id) if id == "A_ALAC" => (Id::Alac, self.codec_private.clone()),
|
||||
Codec::VfwFourcc { fourcc: [b'A', b'V', b'd', b'n' | b'h'], .. } => (Id::Dnxhd, Vec::new()),
|
||||
@@ -439,6 +444,36 @@ mod codec_params_tests {
|
||||
use crate::matroska::mux::{MkvWriter, MuxOptions, TrackSpec};
|
||||
use crate::matroska::{Demuxer, TrackKind};
|
||||
|
||||
#[test]
|
||||
fn ffv1_native_and_vfw_preserve_configuration_and_dimensions() {
|
||||
let config = vec![1, 2, 3, 4, 5];
|
||||
let mut native = TrackSpec::new(TrackKind::Video, "V_FFV1");
|
||||
native.codec_private = config.clone();
|
||||
native.video_size = Some((32, 24));
|
||||
let mut vfw = TrackSpec::new(TrackKind::Video, "V_MS/VFW/FOURCC");
|
||||
let mut bih = vec![0; 40];
|
||||
bih[..4].copy_from_slice(&40u32.to_le_bytes());
|
||||
bih[16..20].copy_from_slice(b"FFV1");
|
||||
bih.extend_from_slice(&config);
|
||||
vfw.codec_private = bih;
|
||||
vfw.video_size = native.video_size;
|
||||
let mut writer = MkvWriter::new(std::io::Cursor::new(Vec::new()), vec![native, vfw], MuxOptions::default()).unwrap();
|
||||
for track in 0..2 {
|
||||
writer.write_frame(track, 0, true, &[0; 4], None).unwrap();
|
||||
}
|
||||
let bytes = writer.finish().unwrap().into_inner();
|
||||
let demux = Demuxer::from_slice(&bytes).unwrap();
|
||||
for track in demux.tracks() {
|
||||
let params = track.codec_params();
|
||||
assert_eq!(params.codec, CodecId::Ffv1);
|
||||
assert_eq!(params.extradata, config);
|
||||
assert_eq!((params.width, params.height), (Some(32), Some(24)));
|
||||
assert!(params.codec.is_video());
|
||||
assert!(!params.codec.is_audio());
|
||||
assert_eq!(params.codec.name(), "FFV1");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alac_dnx_and_opus_match_mp4() {
|
||||
let cookie: Vec<u8> = (0..24).collect();
|
||||
|
||||
@@ -71,6 +71,11 @@ is built with any of the decoder features below and decodes the codecs whose fea
|
||||
| VP9 | `vp9-decode` | ignored |
|
||||
| AV1 | `av1-decode` | an `av1C` record, or empty |
|
||||
| MPEG-2 and MPEG-1 video | `mpeg2-decode` | a sequence header and its extensions, or empty |
|
||||
| ProRes | `prores` | ignored (frame headers carry the format) |
|
||||
| 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 |
|
||||
| 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.
|
||||
Errors are `VideoError`: `Unsupported`, `Malformed`, or a picture that did not convert.
|
||||
@@ -83,3 +88,9 @@ them first. When the codec itself fails, the pictures it had finished are up to
|
||||
- VP9 keeps those of earlier packets.
|
||||
- MPEG-2 returns them with the call, unless the packet made too many pictures.
|
||||
- AV1 drops those the failing `decode` or `flush` had finished.
|
||||
|
||||
ProRes, DNx, APV and FFV1 return one frame per successful packet, with its packet timestamp;
|
||||
VP8 returns no picture for a hidden reference frame. These codecs have no delayed output.
|
||||
APV uses the codec's default selection of the first decodable primary frame and its alpha.
|
||||
FFV1 has the underlying decoder's version 3, range-coded, key-frame-only support. Reset keeps
|
||||
its configuration and container dimensions; VP8 reset discards its reference pictures.
|
||||
@@ -71,3 +71,47 @@ impl From<crate::mpeg2::Error> for VideoError {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "prores")]
|
||||
impl From<crate::prores::Error> for VideoError {
|
||||
fn from(e: crate::prores::Error) -> Self {
|
||||
match e {
|
||||
crate::prores::Error::Unsupported(_) => VideoError::Unsupported(format!("ProRes: {e}")),
|
||||
_ => VideoError::Malformed(format!("ProRes: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dnx")]
|
||||
impl From<crate::dnx::Error> for VideoError {
|
||||
fn from(e: crate::dnx::Error) -> Self {
|
||||
match e {
|
||||
crate::dnx::Error::UnknownCid(_) => VideoError::Unsupported(format!("DNx: {e}")),
|
||||
_ => VideoError::Malformed(format!("DNx: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "apv-decode")]
|
||||
impl From<crate::apv::Error> for VideoError {
|
||||
fn from(e: crate::apv::Error) -> Self {
|
||||
VideoError::Malformed(format!("APV: {e}"))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffv1")]
|
||||
impl From<crate::ffv1::Error> for VideoError {
|
||||
fn from(e: crate::ffv1::Error) -> Self {
|
||||
match e {
|
||||
crate::ffv1::Error::Unsupported(_) => VideoError::Unsupported(format!("FFV1: {e}")),
|
||||
_ => VideoError::Malformed(format!("FFV1: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
impl From<crate::vp8::Error> for VideoError {
|
||||
fn from(e: crate::vp8::Error) -> Self {
|
||||
VideoError::Malformed(format!("VP8: {e}"))
|
||||
}
|
||||
}
|
||||
+26
-3
@@ -29,7 +29,8 @@
|
||||
//! they all convert into (`VideoFrame::try_from`).
|
||||
//!
|
||||
//! `TrackDecoder` picks the decoder for a track from its `craft_media::CodecParams` and returns
|
||||
//! `VideoFrame`s, for H.264, HEVC, VP9, AV1 and MPEG-2 (built with any of their decoder features).
|
||||
//! `VideoFrame`s, for H.264, HEVC, VP9, AV1, MPEG-2, ProRes, DNx, APV, FFV1 and VP8
|
||||
//! (built with any of their decoder features).
|
||||
//!
|
||||
//! Containers and bit-level I/O are in `craft-media`; the codecs here share its `bitstream`
|
||||
//! module and describe their streams with `craft_media::CodecParams`.
|
||||
@@ -40,9 +41,31 @@ pub mod frame;
|
||||
|
||||
pub use error::VideoError;
|
||||
|
||||
#[cfg(any(feature = "h264-decode", feature = "hevc-decode", feature = "vp9-decode", feature = "av1-decode", feature = "mpeg2-decode"))]
|
||||
#[cfg(any(
|
||||
feature = "h264-decode",
|
||||
feature = "hevc-decode",
|
||||
feature = "vp9-decode",
|
||||
feature = "av1-decode",
|
||||
feature = "mpeg2-decode",
|
||||
feature = "prores",
|
||||
feature = "dnx",
|
||||
feature = "apv-decode",
|
||||
feature = "ffv1",
|
||||
feature = "vp8-decode"
|
||||
))]
|
||||
mod track;
|
||||
#[cfg(any(feature = "h264-decode", feature = "hevc-decode", feature = "vp9-decode", feature = "av1-decode", feature = "mpeg2-decode"))]
|
||||
#[cfg(any(
|
||||
feature = "h264-decode",
|
||||
feature = "hevc-decode",
|
||||
feature = "vp9-decode",
|
||||
feature = "av1-decode",
|
||||
feature = "mpeg2-decode",
|
||||
feature = "prores",
|
||||
feature = "dnx",
|
||||
feature = "apv-decode",
|
||||
feature = "ffv1",
|
||||
feature = "vp8-decode"
|
||||
))]
|
||||
pub use track::TrackDecoder;
|
||||
|
||||
#[cfg(any(feature = "apv-decode", feature = "apv-encode"))]
|
||||
|
||||
+107
-7
@@ -31,6 +31,16 @@ enum Inner {
|
||||
Av1 { dec: Box<av1::decode::Decoder>, config_obus: Vec<u8> },
|
||||
#[cfg(feature = "mpeg2-decode")]
|
||||
Mpeg2(Box<mpeg2::Decoder>),
|
||||
#[cfg(feature = "prores")]
|
||||
ProRes,
|
||||
#[cfg(feature = "dnx")]
|
||||
Dnx,
|
||||
#[cfg(feature = "apv-decode")]
|
||||
Apv,
|
||||
#[cfg(feature = "ffv1")]
|
||||
Ffv1(Box<crate::ffv1::Decoder>),
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
Vp8(Box<crate::vp8::Decoder>),
|
||||
}
|
||||
|
||||
/// A decoder for one video track, whatever its codec: built from the track's [`CodecParams`],
|
||||
@@ -43,6 +53,11 @@ enum Inner {
|
||||
/// | VP9 | `vp9-decode` | ignored (the frame headers repeat the `vpcC` values) |
|
||||
/// | AV1 | `av1-decode` | an `av1C` record, or empty |
|
||||
/// | MPEG-2 and MPEG-1 video | `mpeg2-decode` | a sequence header and its extensions, or empty |
|
||||
/// | ProRes | `prores` | ignored (frame headers carry the format) |
|
||||
/// | 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 |
|
||||
/// | 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
|
||||
/// store them. Otherwise they are Annex B, as MPEG-TS and AVI store them, and the parameter sets
|
||||
@@ -52,6 +67,11 @@ 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
|
||||
/// 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.
|
||||
///
|
||||
/// Other codecs are [`VideoError::Unsupported`], as is a codec whose decoder feature is off.
|
||||
///
|
||||
/// A packet carries its presentation timestamp, in any unit; each frame returns with the timestamp
|
||||
@@ -86,13 +106,15 @@ impl std::fmt::Debug for TrackDecoder {
|
||||
}
|
||||
|
||||
impl TrackDecoder {
|
||||
/// A decoder for the track `params` describes. Only `codec` and `extradata` are read; the
|
||||
/// picture size and format come from the stream.
|
||||
/// A decoder for the track `params` describes. `codec` and `extradata` select and configure it;
|
||||
/// FFV1 also requires nonzero `width` and `height` from the container. Other codecs get the
|
||||
/// picture size and format from the stream.
|
||||
///
|
||||
/// Configuration that the codec refuses, or that holds picture data, is
|
||||
/// [`VideoError::Malformed`].
|
||||
/// Invalid configuration, or configuration containing picture data where only headers are
|
||||
/// allowed, is [`VideoError::Malformed`]. Unsupported codec features are
|
||||
/// [`VideoError::Unsupported`].
|
||||
pub fn new(params: &CodecParams) -> Result<TrackDecoder, VideoError> {
|
||||
Ok(TrackDecoder { inner: Inner::new(params.codec, ¶ms.extradata)?, codec: params.codec, held: Vec::new() })
|
||||
Ok(TrackDecoder { inner: Inner::new(params)?, codec: params.codec, held: Vec::new() })
|
||||
}
|
||||
|
||||
/// The track's codec, as `params` gave it.
|
||||
@@ -141,6 +163,22 @@ impl TrackDecoder {
|
||||
let pics = dec.decode(packet, pts)?;
|
||||
self.output(pics, None)
|
||||
}
|
||||
#[cfg(feature = "prores")]
|
||||
Inner::ProRes => self.single(crate::prores::decode_frame(packet)?, pts),
|
||||
#[cfg(feature = "dnx")]
|
||||
Inner::Dnx => self.single(crate::dnx::decode_frame(packet)?, pts),
|
||||
#[cfg(feature = "apv-decode")]
|
||||
Inner::Apv => self.single(crate::apv::decode_frame(packet)?, pts),
|
||||
#[cfg(feature = "ffv1")]
|
||||
Inner::Ffv1(dec) => {
|
||||
let pic = dec.decode(packet)?;
|
||||
self.single(pic, pts)
|
||||
}
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
Inner::Vp8(dec) => {
|
||||
let pic = dec.decode(packet, pts)?;
|
||||
self.output(pic.into_iter().collect(), None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,6 +215,16 @@ impl TrackDecoder {
|
||||
let pics = dec.flush();
|
||||
self.output(pics, None)
|
||||
}
|
||||
#[cfg(feature = "prores")]
|
||||
Inner::ProRes => Ok(std::mem::take(&mut self.held)),
|
||||
#[cfg(feature = "dnx")]
|
||||
Inner::Dnx => Ok(std::mem::take(&mut self.held)),
|
||||
#[cfg(feature = "apv-decode")]
|
||||
Inner::Apv => Ok(std::mem::take(&mut self.held)),
|
||||
#[cfg(feature = "ffv1")]
|
||||
Inner::Ffv1(_) => Ok(std::mem::take(&mut self.held)),
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
Inner::Vp8(_) => Ok(std::mem::take(&mut self.held)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,11 +270,45 @@ impl TrackDecoder {
|
||||
dec.reset();
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(feature = "prores")]
|
||||
Inner::ProRes => Ok(()),
|
||||
#[cfg(feature = "dnx")]
|
||||
Inner::Dnx => Ok(()),
|
||||
#[cfg(feature = "apv-decode")]
|
||||
Inner::Apv => Ok(()),
|
||||
#[cfg(feature = "ffv1")]
|
||||
Inner::Ffv1(_) => Ok(()),
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
Inner::Vp8(dec) => {
|
||||
dec.reset();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Frame-only decoders do not take timestamps; attach the packet timestamp after conversion.
|
||||
#[cfg(any(feature = "prores", feature = "dnx", feature = "apv-decode", feature = "ffv1"))]
|
||||
fn single<P>(&mut self, pic: P, pts: i64) -> Result<Vec<VideoFrame>, VideoError>
|
||||
where
|
||||
VideoFrame: TryFrom<P, Error = FrameError>,
|
||||
{
|
||||
let mut frame = VideoFrame::try_from(pic)?;
|
||||
frame.pts = Some(pts);
|
||||
let mut frames = std::mem::take(&mut self.held);
|
||||
frames.push(frame);
|
||||
Ok(frames)
|
||||
}
|
||||
|
||||
/// The frames kept from a failed call, then `pics` converted. On `err`, or on a picture that
|
||||
/// does not convert, the frames are kept for the next call and the (first) error returned.
|
||||
#[cfg(any(
|
||||
feature = "h264-decode",
|
||||
feature = "hevc-decode",
|
||||
feature = "vp9-decode",
|
||||
feature = "av1-decode",
|
||||
feature = "mpeg2-decode",
|
||||
feature = "vp8-decode"
|
||||
))]
|
||||
fn output<P>(&mut self, pics: Vec<P>, err: Option<VideoError>) -> Result<Vec<VideoFrame>, VideoError>
|
||||
where
|
||||
VideoFrame: TryFrom<P, Error = FrameError>,
|
||||
@@ -253,8 +335,9 @@ impl TrackDecoder {
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
fn new(codec: CodecId, extradata: &[u8]) -> Result<Inner, VideoError> {
|
||||
#[cfg(not(any(feature = "h264-decode", feature = "hevc-decode", feature = "av1-decode", feature = "mpeg2-decode")))]
|
||||
fn new(params: &CodecParams) -> Result<Inner, VideoError> {
|
||||
let codec = params.codec;
|
||||
let extradata = ¶ms.extradata;
|
||||
let _ = extradata;
|
||||
match codec {
|
||||
#[cfg(feature = "h264-decode")]
|
||||
@@ -287,6 +370,23 @@ impl Inner {
|
||||
}
|
||||
Ok(Inner::Mpeg2(dec))
|
||||
}
|
||||
#[cfg(feature = "prores")]
|
||||
CodecId::ProRes => Ok(Inner::ProRes),
|
||||
#[cfg(feature = "dnx")]
|
||||
CodecId::Dnxhd => Ok(Inner::Dnx),
|
||||
#[cfg(feature = "apv-decode")]
|
||||
CodecId::Apv => Ok(Inner::Apv),
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
CodecId::Vp8 => Ok(Inner::Vp8(Box::default())),
|
||||
#[cfg(feature = "ffv1")]
|
||||
CodecId::Ffv1 => {
|
||||
let (width, height) = params
|
||||
.width
|
||||
.zip(params.height)
|
||||
.filter(|&(w, h)| w > 0 && h > 0)
|
||||
.ok_or_else(|| VideoError::Malformed("FFV1: container dimensions are required".into()))?;
|
||||
Ok(Inner::Ffv1(Box::new(crate::ffv1::Decoder::new(extradata, width, height)?)))
|
||||
}
|
||||
other => Err(VideoError::Unsupported(format!("no {} decoder in this build", other.name()))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,16 @@ fn a_codec_without_a_decoder_is_unsupported() {
|
||||
CodecId::Theora,
|
||||
CodecId::Aac,
|
||||
CodecId::Unknown,
|
||||
#[cfg(not(feature = "prores"))]
|
||||
CodecId::ProRes,
|
||||
#[cfg(not(feature = "dnx"))]
|
||||
CodecId::Dnxhd,
|
||||
#[cfg(not(feature = "apv-decode"))]
|
||||
CodecId::Apv,
|
||||
#[cfg(not(feature = "ffv1"))]
|
||||
CodecId::Ffv1,
|
||||
#[cfg(not(feature = "vp8-decode"))]
|
||||
CodecId::Vp8,
|
||||
#[cfg(not(feature = "h264-decode"))]
|
||||
CodecId::H264,
|
||||
#[cfg(not(feature = "hevc-decode"))]
|
||||
@@ -39,8 +49,16 @@ fn a_codec_without_a_decoder_is_unsupported() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_built_decoder_starts_without_configuration() {
|
||||
fn decoders_other_than_ffv1_start_without_configuration() {
|
||||
let on = [
|
||||
#[cfg(feature = "prores")]
|
||||
CodecId::ProRes,
|
||||
#[cfg(feature = "dnx")]
|
||||
CodecId::Dnxhd,
|
||||
#[cfg(feature = "apv-decode")]
|
||||
CodecId::Apv,
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
CodecId::Vp8,
|
||||
#[cfg(feature = "h264-decode")]
|
||||
CodecId::H264,
|
||||
#[cfg(feature = "hevc-decode")]
|
||||
@@ -264,3 +282,110 @@ mod held {
|
||||
assert!(dec.flush().unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "prores", feature = "dnx", feature = "apv", feature = "ffv1", feature = "vp8"))]
|
||||
fn check_packet(params: CodecParams, packet: &[u8], mut expected: VideoFrame) {
|
||||
let mut dec = TrackDecoder::new(¶ms).unwrap();
|
||||
assert_eq!(dec.codec(), params.codec);
|
||||
for pts in [-17, 91] {
|
||||
expected.pts = Some(pts);
|
||||
assert_eq!(dec.decode(packet, pts).unwrap(), [expected.clone()]);
|
||||
assert!(dec.flush().unwrap().is_empty());
|
||||
assert!(dec.flush().unwrap().is_empty());
|
||||
assert!(matches!(dec.decode(&[], 0), Err(VideoError::Malformed(_))));
|
||||
dec.reset().unwrap();
|
||||
assert!(dec.flush().unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "prores")]
|
||||
#[test]
|
||||
fn prores_packet_timestamps_and_reset() {
|
||||
use crate::prores::*;
|
||||
let frame = Frame::new(32, 32, ChromaFormat::Yuv422, 10, false).unwrap();
|
||||
let mut enc = Encoder::new(Profile::Proxy, 32, 32);
|
||||
let packet = enc.encode(&frame).unwrap();
|
||||
check_packet(enc.codec_params(), &packet, decode_frame(&packet).unwrap().try_into().unwrap());
|
||||
}
|
||||
|
||||
#[cfg(feature = "dnx")]
|
||||
#[test]
|
||||
fn dnx_packet_timestamps_and_reset() {
|
||||
use crate::dnx::*;
|
||||
let frame = Frame::new(64, 64, ChromaFormat::Yuv422, 8, false);
|
||||
let mut enc = Encoder::new(Profile::Lb, 64, 64).unwrap();
|
||||
let packet = enc.encode(&frame).unwrap();
|
||||
check_packet(enc.codec_params(), &packet, decode_frame(&packet).unwrap().try_into().unwrap());
|
||||
}
|
||||
|
||||
#[cfg(feature = "apv")]
|
||||
#[test]
|
||||
fn apv_packet_timestamps_and_reset() {
|
||||
use crate::apv::*;
|
||||
let frame = Frame::new(32, 32, ChromaFormat::Yuv422, 10, false);
|
||||
let mut enc = Encoder::new(Profile::P422_10, 32, 32).unwrap();
|
||||
let packet = enc.encode(&frame).unwrap();
|
||||
check_packet(params(CodecId::Apv, &enc.decoder_config_record()), &packet, decode_frame(&packet).unwrap().try_into().unwrap());
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffv1")]
|
||||
#[test]
|
||||
fn ffv1_configuration_dimensions_timestamps_and_reset() {
|
||||
use crate::ffv1::*;
|
||||
let frame = Frame {
|
||||
width: 16,
|
||||
height: 16,
|
||||
colorspace: Colorspace::Rgb,
|
||||
bit_depth: 10,
|
||||
chroma_shift: (0, 0),
|
||||
planes: vec![vec![128; 256], vec![512; 256], vec![768; 256], vec![1023; 256]],
|
||||
};
|
||||
let enc = Encoder::new(&frame, EncoderConfig::default()).unwrap();
|
||||
let config = enc.config_record();
|
||||
let packet = enc.encode(&frame).unwrap();
|
||||
let p = params(CodecId::Ffv1, &config).with_size(16, 16);
|
||||
check_packet(p.clone(), &packet, frame.try_into().unwrap());
|
||||
for (width, height) in [(None, Some(16)), (Some(16), None), (Some(0), Some(16)), (Some(16), Some(0))] {
|
||||
let mut bad = p.clone();
|
||||
bad.width = width;
|
||||
bad.height = height;
|
||||
assert!(matches!(TrackDecoder::new(&bad), Err(VideoError::Malformed(_))));
|
||||
}
|
||||
for config in [&[][..], &config[..config.len() - 1]] {
|
||||
assert!(matches!(TrackDecoder::new(¶ms(CodecId::Ffv1, config).with_size(16, 16)), Err(VideoError::Malformed(_))));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "vp8")]
|
||||
#[test]
|
||||
fn vp8_hidden_frames_timestamps_and_reset() {
|
||||
use crate::vp8::*;
|
||||
let packet = encode_rgba(&vec![128; 32 * 32 * 4], 32, 32, &Vp8Params::default()).unwrap().data;
|
||||
let expected = Decoder::new().decode(&packet, 0).unwrap().unwrap().try_into().unwrap();
|
||||
check_packet(params(CodecId::Vp8, &[]), &packet, expected);
|
||||
let mut dec = TrackDecoder::new(¶ms(CodecId::Vp8, &[])).unwrap();
|
||||
let mut hidden = packet.clone();
|
||||
hidden[0] &= !0x10; // show_frame = 0: updates references, produces no displayed picture.
|
||||
assert!(dec.decode(&hidden, 33).unwrap().is_empty());
|
||||
assert!(dec.flush().unwrap().is_empty());
|
||||
dec.reset().unwrap();
|
||||
// An inter-frame tag with one byte of first partition must fail for missing references
|
||||
// before its payload is parsed. Keeping the previous key frame would give a syntax error.
|
||||
let err = dec.decode(&[0x31, 0, 0, 0], 34).unwrap_err();
|
||||
assert!(err.to_string().contains("missing reference frame"), "{err}");
|
||||
assert_eq!(dec.decode(&packet, 35).unwrap()[0].pts, Some(35));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_codec_errors_keep_their_category() {
|
||||
#[cfg(feature = "prores")]
|
||||
assert!(matches!(VideoError::from(crate::prores::Error::Unsupported("profile")), VideoError::Unsupported(_)));
|
||||
#[cfg(feature = "dnx")]
|
||||
assert!(matches!(VideoError::from(crate::dnx::Error::UnknownCid(0)), VideoError::Unsupported(_)));
|
||||
#[cfg(feature = "apv-decode")]
|
||||
assert!(matches!(VideoError::from(crate::apv::Error::Truncated), VideoError::Malformed(_)));
|
||||
#[cfg(feature = "ffv1")]
|
||||
assert!(matches!(VideoError::from(crate::ffv1::Error::Unsupported("inter frames")), VideoError::Unsupported(_)));
|
||||
#[cfg(feature = "vp8-decode")]
|
||||
assert!(matches!(VideoError::from(crate::vp8::Error::Invalid("header".into())), VideoError::Malformed(_)));
|
||||
}
|
||||
Reference in new issue
Block a user