Skip to content

Commit b7dd34f

Browse files
authored
Always parse the extended avcC atom. (#30)
I don't want to allowlist every profile. If there's trailing bytes, parse them.
1 parent 90a3114 commit b7dd34f

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

src/moov/trak/mdia/minf/stbl/stsd/h264/avcc.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,30 @@ impl Atom for Avcc {
9090
picture_parameter_sets.push(nal);
9191
}
9292

93-
let ext = match avc_profile_indication {
94-
// NOTE: Many encoders/decoders skip this part, so it's not always present
95-
100 | 110 | 122 | 144 if buf.remaining() > 0 => {
96-
let chroma_format = u8::decode(buf)? & 0x3;
97-
let bit_depth_luma_minus8 = u8::decode(buf)? & 0x7;
98-
let bit_depth_chroma_minus8 = u8::decode(buf)? & 0x7;
99-
let num_of_sequence_parameter_set_exts = u8::decode(buf)? as usize;
100-
let mut sequence_parameter_sets_ext =
101-
Vec::with_capacity(num_of_sequence_parameter_set_exts);
102-
103-
for _ in 0..num_of_sequence_parameter_set_exts {
104-
let size = u16::decode(buf)? as usize;
105-
let nal = Vec::decode_exact(buf, size)?;
106-
sequence_parameter_sets_ext.push(nal);
107-
}
108-
109-
Some(AvccExt {
110-
chroma_format,
111-
bit_depth_luma: bit_depth_luma_minus8 + 8,
112-
bit_depth_chroma: bit_depth_chroma_minus8 + 8,
113-
sequence_parameter_sets_ext,
114-
})
93+
// NOTE: Many encoders/decoders skip this extended avcC part.
94+
// It's profile specific, but we don't really care and will parse it if present.
95+
let ext = if buf.has_remaining() {
96+
let chroma_format = u8::decode(buf)? & 0x3;
97+
let bit_depth_luma_minus8 = u8::decode(buf)? & 0x7;
98+
let bit_depth_chroma_minus8 = u8::decode(buf)? & 0x7;
99+
let num_of_sequence_parameter_set_exts = u8::decode(buf)? as usize;
100+
let mut sequence_parameter_sets_ext =
101+
Vec::with_capacity(num_of_sequence_parameter_set_exts);
102+
103+
for _ in 0..num_of_sequence_parameter_set_exts {
104+
let size = u16::decode(buf)? as usize;
105+
let nal = Vec::decode_exact(buf, size)?;
106+
sequence_parameter_sets_ext.push(nal);
115107
}
116-
_ => None,
108+
109+
Some(AvccExt {
110+
chroma_format,
111+
bit_depth_luma: bit_depth_luma_minus8 + 8,
112+
bit_depth_chroma: bit_depth_chroma_minus8 + 8,
113+
sequence_parameter_sets_ext,
114+
})
115+
} else {
116+
None
117117
};
118118

119119
Ok(Avcc {

src/test/h264.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,84 @@ fn avcc_ext() {
364364

365365
// assert_eq!(buf, ENCODED);
366366
}
367+
368+
#[test]
369+
fn avcc_ext_2() {
370+
const ENCODED: &[u8] = &[
371+
0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6F, 0x36, 0x00, 0x00, 0x02,
372+
0x00, 0x69, 0x73, 0x6F, 0x36, 0x63, 0x6D, 0x66, 0x63, 0x6D, 0x70, 0x34, 0x31, 0x00, 0x00,
373+
0x02, 0xE0, 0x6D, 0x6F, 0x6F, 0x76, 0x00, 0x00, 0x00, 0x6C, 0x6D, 0x76, 0x68, 0x64, 0x00,
374+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8,
375+
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
379+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0xE2, 0x74, 0x72,
381+
0x61, 0x6B, 0x00, 0x00, 0x00, 0x5C, 0x74, 0x6B, 0x68, 0x64, 0x00, 0x00, 0x00, 0x03, 0x00,
382+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
383+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
384+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00,
387+
0x04, 0x38, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7E, 0x6D, 0x64, 0x69, 0x61, 0x00, 0x00, 0x00,
388+
0x20, 0x6D, 0x64, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389+
0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xC4, 0x00, 0x00, 0x00,
390+
0x00, 0x00, 0x2D, 0x68, 0x64, 0x6C, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
391+
0x76, 0x69, 0x64, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392+
0x00, 0x56, 0x69, 0x64, 0x65, 0x6F, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00, 0x00,
393+
0x00, 0x01, 0x29, 0x6D, 0x69, 0x6E, 0x66, 0x00, 0x00, 0x00, 0x14, 0x76, 0x6D, 0x68, 0x64,
394+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395+
0x24, 0x64, 0x69, 0x6E, 0x66, 0x00, 0x00, 0x00, 0x1C, 0x64, 0x72, 0x65, 0x66, 0x00, 0x00,
396+
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0C, 0x75, 0x72, 0x6C, 0x20, 0x00,
397+
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xE9, 0x73, 0x74, 0x62, 0x6C, 0x00, 0x00, 0x00, 0x9D,
398+
0x73, 0x74, 0x73, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
399+
0x8D, 0x61, 0x76, 0x63, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
400+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
401+
0x80, 0x04, 0x38, 0x00, 0x48, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402+
0x00, 0x01, 0x15, 0x4C, 0x61, 0x76, 0x63, 0x36, 0x30, 0x2E, 0x33, 0x31, 0x2E, 0x31, 0x30,
403+
0x32, 0x20, 0x6C, 0x69, 0x62, 0x78, 0x32, 0x36, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
404+
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x37, 0x61, 0x76, 0x63,
405+
0x43, 0x01, 0xF4, 0x00, 0x28, 0xFF, 0xE1, 0x00, 0x1A, 0x67, 0xF4, 0x00, 0x28, 0x91, 0x9B,
406+
0x28, 0x0F, 0x00, 0x44, 0xFC, 0x4C, 0x20, 0x00, 0x00, 0x03, 0x00, 0x20, 0x00, 0x00, 0x07,
407+
0x81, 0xE3, 0x06, 0x32, 0xC0, 0x01, 0x00, 0x06, 0x68, 0xEF, 0x8F, 0x19, 0x21, 0x90, 0xFF,
408+
0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00,
409+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x73, 0x63, 0x00, 0x00, 0x00,
410+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x73, 0x74, 0x73, 0x7A, 0x00, 0x00,
411+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x73,
412+
0x74, 0x63, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28,
413+
0x6D, 0x76, 0x65, 0x78, 0x00, 0x00, 0x00, 0x20, 0x74, 0x72, 0x65, 0x78, 0x00, 0x00, 0x00,
414+
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
415+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x75, 0x64, 0x74, 0x61, 0x00,
416+
0x00, 0x00, 0x5A, 0x6D, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21,
417+
0x68, 0x64, 0x6C, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x64, 0x69,
418+
0x72, 0x61, 0x70, 0x70, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
419+
0x00, 0x00, 0x2D, 0x69, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, 0x25, 0xA9, 0x74, 0x6F, 0x6F,
420+
0x00, 0x00, 0x00, 0x1D, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
421+
0x00, 0x4C, 0x61, 0x76, 0x66, 0x36, 0x30, 0x2E, 0x31, 0x36, 0x2E, 0x31, 0x30, 0x30, 0x00,
422+
0x00, 0x00, 0x6C, 0x6D, 0x6F, 0x6F, 0x66, 0x00, 0x00, 0x00, 0x10, 0x6D, 0x66, 0x68, 0x64,
423+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0x74, 0x72, 0x61,
424+
0x66, 0x00, 0x00, 0x00, 0x20, 0x74, 0x66, 0x68, 0x64, 0x00, 0x02, 0x00, 0x3A, 0x00, 0x00,
425+
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x67, 0x1D, 0x01,
426+
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x74, 0x66, 0x64, 0x74, 0x01, 0x00, 0x00, 0x00,
427+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x74, 0x72, 0x75,
428+
0x6E, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x74, 0x02, 0x00,
429+
0x00,
430+
];
431+
432+
let buf = &mut std::io::Cursor::new(&ENCODED);
433+
434+
let ftyp = Ftyp::decode(buf).expect("failed to decode ftyp");
435+
436+
assert_eq!(
437+
ftyp,
438+
Ftyp {
439+
major_brand: b"iso6".into(),
440+
minor_version: 512,
441+
compatible_brands: vec![b"iso6".into(), b"cmfc".into(), b"mp41".into()],
442+
}
443+
);
444+
445+
// This was failing because the avcc atom was under decoded.
446+
let _ = Moov::decode(buf).expect("failed to decode moov");
447+
}

0 commit comments

Comments
 (0)