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
32 changes: 29 additions & 3 deletions src/moov/trak/mdia/minf/stbl/stsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub enum Codec {
Samr(Samr),

// Unknown
Unknown(FourCC),
Unknown(FourCC, Vec<u8>),
}

impl Decode for Codec {
Expand Down Expand Up @@ -157,9 +157,23 @@ impl Decode for Codec {
Any::S16l(atom) => atom.into(),
Any::Wvtt(atom) => atom.into(),
Any::Samr(atom) => atom.into(),
unknown @ Any::Unknown(..) => {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf is this syntax?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://doc.rust-lang.org/book/ch19-03-pattern-syntax.html#using--bindings

I think the code is using unknown in two different ways, which probably means it needs a rewrite.

crate::decode_unknown(&unknown, Stsd::KIND)?;

let Any::Unknown(kind, body) = unknown else {
unreachable!()
};
Self::Unknown(kind, body)
}
unknown => {
crate::decode_unknown(&unknown, Stsd::KIND)?;
Self::Unknown(unknown.kind())

// The atom kind is known elsewhere in the hierarchy, but is not a supported
// sample entry. Re-encode its body so the unknown entry remains a valid box.
let kind = unknown.kind();
let mut encoded = Vec::new();
unknown.encode(&mut encoded)?;
Self::Unknown(kind, encoded.split_off(8))
}
})
}
Expand All @@ -168,7 +182,19 @@ impl Decode for Codec {
impl Encode for Codec {
fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
match self {
Self::Unknown(kind) => kind.encode(buf),
Self::Unknown(kind, body) => {
let start = buf.len();
0u32.encode(buf)?;
kind.encode(buf)?;
body.encode(buf)?;

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
20 changes: 20 additions & 0 deletions tests/stsd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg(not(feature = "strict"))]

use mp4_atom::{Codec, Decode, Encode, FourCC, Stsd};

#[test]
fn unknown_codec_round_trips() {
let input = b"\0\0\0\x1cstsd\0\0\0\0\0\0\0\x01\0\0\0\x0cdvh1\x01\x02\x03\x04";
let mut buf = input.as_slice();

let stsd = Stsd::decode(&mut buf).unwrap();
assert_eq!(
stsd.codecs,
vec![Codec::Unknown(FourCC::new(b"dvh1"), vec![1, 2, 3, 4])]
);

let mut output = Vec::new();
stsd.encode(&mut output).unwrap();

assert_eq!(output, input);
}
Loading