Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ pub enum Error {

#[error("invalid parameter combination: {0}")]
InvalidCombination(&'static str),

#[error("unknown codec in sample description box")]
UnknownCodec,
}

pub type Result<T> = std::result::Result<T, Error>;
Expand Down
32 changes: 20 additions & 12 deletions src/moov/trak/mdia/minf/stbl/stsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,18 @@ impl Decode for Codec {
impl Encode for Codec {
fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
match self {
Self::Unknown(..) => Err(Error::UnknownCodec),
Self::Unknown(kind, body) => {
let start = buf.len();
0u32.encode(buf)?; // size placeholder
kind.encode(buf)?;
buf.append_slice(body);

let size: u32 = (buf.len() - start)
.try_into()
.map_err(|_| Error::TooLarge(*kind))?;
buf.set_slice(start, &size.to_be_bytes());
Ok(())
}
Self::Avc1(atom) => atom.encode(buf),
Self::Hev1(atom) => atom.encode(buf),
Self::Hvc1(atom) => atom.encode(buf),
Expand Down Expand Up @@ -287,21 +298,18 @@ mod tests {
}

#[test]

fn unknown_codec_not_emitted() {
use crate::{Codec, Encode, FourCC, Stsd};
fn unknown_codec_roundtrip() {
use crate::{Codec, Decode, Encode, FourCC, Stsd};

let stsd = Stsd {
codecs: vec![Codec::Unknown(FourCC::new(b"dvh9"), vec![1, 2, 3, 4])],
};
assert_eq!(
stsd.codecs,
vec![Codec::Unknown(FourCC::new(b"dvh9"), vec![1, 2, 3, 4])]
);

let mut output = Vec::new();
assert!(matches!(
stsd.encode(&mut output),
Err(crate::Error::UnknownCodec)
));
stsd.encode(&mut output)
.expect("failed to encode unknown codec");

let decoded = Stsd::decode(&mut output.as_slice()).expect("failed to decode stsd");
assert_eq!(decoded, stsd);
}
}
Loading