forge
Explore
Log in

Don't you dare go Hollow

B16: ec-mp4 mux casts checked: close/leaf return Result on box sizes over 4 GB; sample and chapter durations via u32::try_from (no silent wrap or 0); output unchanged for in-range values, checked: cargo check + clippy ec-mp4

Browse files at this commit

130fe27519911b075debaf633b75fe87f930fb7bAhmet Kaan GümüşParent: 22f0f3f
crates/ec-mp4/src/boxes.rsmodified+7 -6
@@ -228,16 +228,17 @@ pub fn open(out: &mut Vec<u8>, kind: &FourCc) -> usize {
228228 }
229229
230230 /// Finish the box [`open`] started at `at` by writing its size.
231-pub fn close(out: &mut [u8], at: usize) {
232- let size = (out.len() - at) as u32;
231+pub fn close(out: &mut [u8], at: usize) -> Result<()> {
232+ let size = u32::try_from(out.len() - at).map_err(|_| Error::corrupt("mp4: a box over 4 GB"))?;
233233 out[at..at + 4].copy_from_slice(&size.to_be_bytes());
234+ Ok(())
234235 }
235236
236237 /// A whole box in one call, for the leaves.
237-pub fn leaf(out: &mut Vec<u8>, kind: &FourCc, payload: &[u8]) {
238+pub fn leaf(out: &mut Vec<u8>, kind: &FourCc, payload: &[u8]) -> Result<()> {
238239 let at = open(out, kind);
239240 out.extend_from_slice(payload);
240- close(out, at);
241+ close(out, at)
241242 }
242243
243244 /// The `version`/`flags` word a FullBox starts with.
@@ -259,7 +260,7 @@ mod tests {
259260 #[test]
260261 fn walks_32_and_64_bit_sizes() {
261262 let mut out = Vec::new();
262- leaf(&mut out, b"ftyp", &[1, 2, 3, 4]);
263+ leaf(&mut out, b"ftyp", &[1, 2, 3, 4]).unwrap();
263264 // The same box written in the 64-bit form a big mdat uses.
264265 out.extend_from_slice(&1u32.to_be_bytes());
265266 out.extend_from_slice(b"mdat");
@@ -274,7 +275,7 @@ mod tests {
274275 #[test]
275276 fn a_size_that_does_not_fit_is_corrupt_not_a_panic() {
276277 let mut out = Vec::new();
277- leaf(&mut out, b"moov", &[0; 4]);
278+ leaf(&mut out, b"moov", &[0; 4]).unwrap();
278279 out[3] = 0xFF; // 255 bytes of moov inside 12
279280 assert!(types(&out).is_err());
280281
crates/ec-mp4/src/demux.rsmodified+1 -1
@@ -1950,7 +1950,7 @@ mod tests {
19501950 assert_eq!(unpack_language(0), None);
19511951
19521952 let mut udta = Vec::new();
1953- boxes::leaf(&mut udta, b"name", b"Turkish\0");
1953+ boxes::leaf(&mut udta, b"name", b"Turkish\0").unwrap();
19541954 assert_eq!(udta_name(&udta).unwrap().as_deref(), Some("Turkish"));
19551955 }
19561956
crates/ec-mp4/src/mux.rsmodified+116 -105
@@ -317,7 +317,7 @@ impl<W: Write + Seek> Mp4Muxer<W> {
317317 head.extend_from_slice(b"mp41");
318318 }
319319 }
320- close(&mut head, at);
320+ close(&mut head, at)?;
321321 self.mdat_at = head.len() as u64;
322322 // The 64-bit form of an `mdat` header, always: a two-hour 4K export goes
323323 // past 4 GB and the placeholder cannot grow later without moving every
@@ -333,7 +333,7 @@ impl<W: Write + Seek> Mp4Muxer<W> {
333333 /// Sample durations, resolved: a packet that stated one keeps it, and one
334334 /// that did not gets the step to the next sample — which is what the last
335335 /// sample has no next for, so it repeats the step before it.
336- fn resolve_durations(track: &mut Track) {
336+ fn resolve_durations(track: &mut Track) -> Result<()> {
337337 for i in 0..track.samples.len() {
338338 if track.samples[i].duration != 0 {
339339 continue;
@@ -344,8 +344,10 @@ impl<W: Write + Seek> Mp4Muxer<W> {
344344 .checked_sub(1)
345345 .map_or(0, |prev| i64::from(track.samples[prev].duration)),
346346 };
347- track.samples[i].duration = u32::try_from(step.max(0)).unwrap_or(0);
347+ track.samples[i].duration = u32::try_from(step.max(0))
348+ .map_err(|_| Error::corrupt("mp4: a sample duration over 32 bits"))?;
348349 }
350+ Ok(())
349351 }
350352
351353 /// The film's length in movie milliseconds: the longest track's presentation.
@@ -392,13 +394,15 @@ impl<W: Write + Seek> Mp4Muxer<W> {
392394 let mut samples = Vec::with_capacity(self.chapters.len());
393395 for (i, (_, title)) in self.chapters.iter().enumerate() {
394396 let next = starts.get(i + 1).copied().unwrap_or(end_ms);
395- let mut duration = (next - starts[i]).max(0) as u32;
396- if i + 1 == starts.len() {
397- // Readers that end the last chapter at the text track's media duration
398- // (ffmpeg) do not count the leading empty edit, so the media is padded by
399- // the first chapter's start; the edit list below still plays only to the end.
400- duration += first_ms.max(0) as u32;
401- }
397+ // Readers that end the last chapter at the text track's media duration
398+ // (ffmpeg) do not count the leading empty edit, so the media is padded by
399+ // the first chapter's start; the edit list below still plays only to the end.
400+ let padding = match i + 1 == starts.len() {
401+ true => first_ms.max(0),
402+ false => 0,
403+ };
404+ let duration = u32::try_from((next - starts[i]).max(0) + padding)
405+ .map_err(|_| Error::corrupt("mp4: a chapter over 32 bits of milliseconds"))?;
402406 let text = clip(title, usize::from(u16::MAX));
403407 payload.extend_from_slice(&(text.len() as u16).to_be_bytes());
404408 payload.extend_from_slice(text.as_bytes());
@@ -419,7 +423,7 @@ impl<W: Write + Seek> Mp4Muxer<W> {
419423 fn seal_mdat(&mut self) -> Result<Option<ChapterText>> {
420424 self.start()?;
421425 for track in &mut self.tracks {
422- Mp4Muxer::<W>::resolve_durations(track);
426+ Mp4Muxer::<W>::resolve_durations(track)?;
423427 }
424428 // The chapter text samples are media, so they go in the mdat before its
425429 // size is known.
@@ -457,7 +461,7 @@ impl<W: Write + Seek> Mp4Muxer<W> {
457461 moov.extend_from_slice(&[0; 24]); // pre_defined
458462 let next_id = chapter_id.unwrap_or(self.tracks.len() as u32) + 1;
459463 moov.extend_from_slice(&next_id.to_be_bytes());
460- close(&mut moov, mvhd);
464+ close(&mut moov, mvhd)?;
461465
462466 for i in 0..self.tracks.len() {
463467 let chapter_ref = match reference == Some(i) {
@@ -467,20 +471,20 @@ impl<W: Write + Seek> Mp4Muxer<W> {
467471 self.write_trak(&mut moov, i, duration, chapter_ref, shift)?;
468472 }
469473 if let (Some(text), Some(id)) = (chapters, chapter_id) {
470- write_chapter_trak(&mut moov, id, text, shift);
474+ write_chapter_trak(&mut moov, id, text, shift)?;
471475 }
472476 let chpl = (1..=255).contains(&self.chapters.len());
473477 if self.title.is_some() || chpl {
474478 let udta = open(&mut moov, b"udta");
475479 if let Some(title) = &self.title {
476- leaf(&mut moov, b"name", title.as_bytes());
480+ leaf(&mut moov, b"name", title.as_bytes())?;
477481 }
478482 if chpl {
479- write_chpl(&mut moov, &self.chapters);
483+ write_chpl(&mut moov, &self.chapters)?;
480484 }
481- close(&mut moov, udta);
485+ close(&mut moov, udta)?;
482486 }
483- close(&mut moov, at);
487+ close(&mut moov, at)?;
484488 Ok(moov)
485489 }
486490
@@ -532,14 +536,14 @@ impl<W: Write + Seek> Mp4Muxer<W> {
532536 };
533537 out.extend_from_slice(&(width << 16).to_be_bytes());
534538 out.extend_from_slice(&(height << 16).to_be_bytes());
535- close(out, tkhd);
539+ close(out, tkhd)?;
536540
537541 if let Some(id) = chapter_ref {
538542 let tref = open(out, b"tref");
539543 let chap = open(out, b"chap");
540544 out.extend_from_slice(&id.to_be_bytes());
541- close(out, chap);
542- close(out, tref);
545+ close(out, chap)?;
546+ close(out, tref)?;
543547 }
544548
545549 // An `elst`, where the first picture is not the first *decoded* one, or
@@ -558,8 +562,8 @@ impl<W: Write + Seek> Mp4Muxer<W> {
558562 out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes());
559563 out.extend_from_slice(&media_time.to_be_bytes());
560564 out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0
561- close(out, elst);
562- close(out, edts);
565+ close(out, elst)?;
566+ close(out, edts)?;
563567 }
564568 let priming = presentation_start(track)
565569 .max(track.audio_delay)
@@ -577,8 +581,8 @@ impl<W: Write + Seek> Mp4Muxer<W> {
577581 out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes());
578582 out.extend_from_slice(&(priming as u32).to_be_bytes());
579583 out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0
580- close(out, elst);
581- close(out, edts);
584+ close(out, elst)?;
585+ close(out, edts)?;
582586 }
583587
584588 let mdia = open(out, b"mdia");
@@ -590,7 +594,7 @@ impl<W: Write + Seek> Mp4Muxer<W> {
590594 out.extend_from_slice(&(track_ticks(track).max(0) as u32).to_be_bytes());
591595 out.extend_from_slice(&pack_language(track.info.language.as_deref()).to_be_bytes());
592596 out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined
593- close(out, mdhd);
597+ close(out, mdhd)?;
594598
595599 let (handler, handler_name) = match codec.media_type() {
596600 ec_core::MediaType::Video => (b"vide", "VideoHandler"),
@@ -604,7 +608,7 @@ impl<W: Write + Seek> Mp4Muxer<W> {
604608 out.extend_from_slice(&[0; 12]); // reserved
605609 out.extend_from_slice(handler_name.as_bytes());
606610 out.push(0);
607- close(out, hdlr);
611+ close(out, hdlr)?;
608612
609613 let minf = open(out, b"minf");
610614 match codec.media_type() {
@@ -612,18 +616,18 @@ impl<W: Write + Seek> Mp4Muxer<W> {
612616 let vmhd = open(out, b"vmhd");
613617 full_head(out, 0, 1);
614618 out.extend_from_slice(&[0; 8]); // graphicsmode + opcolor
615- close(out, vmhd);
619+ close(out, vmhd)?;
616620 }
617621 ec_core::MediaType::Audio => {
618622 let smhd = open(out, b"smhd");
619623 full_head(out, 0, 0);
620624 out.extend_from_slice(&[0; 4]); // balance + reserved
621- close(out, smhd);
625+ close(out, smhd)?;
622626 }
623627 ec_core::MediaType::Subtitle => {
624628 let nmhd = open(out, b"nmhd");
625629 full_head(out, 0, 0);
626- close(out, nmhd);
630+ close(out, nmhd)?;
627631 }
628632 }
629633 let dinf = open(out, b"dinf");
@@ -632,27 +636,27 @@ impl<W: Write + Seek> Mp4Muxer<W> {
632636 out.extend_from_slice(&1u32.to_be_bytes());
633637 let url = open(out, b"url ");
634638 full_head(out, 0, 1); // flag 1: the media is in this very file
635- close(out, url);
636- close(out, dref);
637- close(out, dinf);
639+ close(out, url)?;
640+ close(out, dref)?;
641+ close(out, dinf)?;
638642
639643 let stbl = open(out, b"stbl");
640644 let stsd = open(out, b"stsd");
641645 full_head(out, 0, 0);
642646 out.extend_from_slice(&1u32.to_be_bytes());
643647 sample_entry(out, &track.info, track.field_order, track.chan)?;
644- close(out, stsd);
645- sample_tables(out, track, shift);
646- close(out, stbl);
647- close(out, minf);
648- close(out, mdia);
648+ close(out, stsd)?;
649+ sample_tables(out, track, shift)?;
650+ close(out, stbl)?;
651+ close(out, minf)?;
652+ close(out, mdia)?;
649653
650654 if let Some(title) = &track.title {
651655 let udta = open(out, b"udta");
652- leaf(out, b"name", title.as_bytes());
653- close(out, udta);
656+ leaf(out, b"name", title.as_bytes())?;
657+ close(out, udta)?;
654658 }
655- close(out, at);
659+ close(out, at)?;
656660 Ok(())
657661 }
658662 }
@@ -718,7 +722,8 @@ impl<W: Write + Seek + Send> Muxer for Mp4Muxer<W> {
718722 size,
719723 dts,
720724 cts,
721- duration: packet.duration.map_or(0, |d| at(d).max(0) as u32),
725+ duration: u32::try_from(packet.duration.map_or(0, |d| at(d).max(0)))
726+ .map_err(|_| Error::corrupt("mp4: a sample duration over 32 bits"))?,
722727 sync: packet.flags.keyframe,
723728 });
724729 match track.chunk_end == pos {
@@ -812,7 +817,7 @@ impl<W: Read + Write + Seek + Send> Mp4Muxer<W> {
812817 /// clock (milliseconds), with a `co64` offset so it stays right past 4 GB. It is
813818 /// disabled, as a chapter track is in a QuickTime file, and its `edts` puts the
814819 /// first chapter at its own start when that is not zero.
815-fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64) {
820+fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64) -> Result<()> {
816821 let at = open(out, b"trak");
817822
818823 let tkhd = open(out, b"tkhd");
@@ -828,7 +833,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
828833 out.extend_from_slice(&0u16.to_be_bytes());
829834 out.extend_from_slice(&UNITY_MATRIX);
830835 out.extend_from_slice(&[0; 8]); // width, height
831- close(out, tkhd);
836+ close(out, tkhd)?;
832837
833838 if chap.first_ms > 0 {
834839 let edts = open(out, b"edts");
@@ -844,8 +849,8 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
844849 out.extend_from_slice(&length.to_be_bytes());
845850 out.extend_from_slice(&0u32.to_be_bytes());
846851 out.extend_from_slice(&0x0001_0000u32.to_be_bytes());
847- close(out, elst);
848- close(out, edts);
852+ close(out, elst)?;
853+ close(out, edts)?;
849854 }
850855
851856 let mdia = open(out, b"mdia");
@@ -858,7 +863,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
858863 out.extend_from_slice(&(total as u32).to_be_bytes());
859864 out.extend_from_slice(&pack_language(None).to_be_bytes());
860865 out.extend_from_slice(&0u16.to_be_bytes());
861- close(out, mdhd);
866+ close(out, mdhd)?;
862867
863868 let hdlr = open(out, b"hdlr");
864869 full_head(out, 0, 0);
@@ -867,21 +872,21 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
867872 out.extend_from_slice(&[0; 12]);
868873 out.extend_from_slice(b"ChapterHandler");
869874 out.push(0);
870- close(out, hdlr);
875+ close(out, hdlr)?;
871876
872877 let minf = open(out, b"minf");
873878 let nmhd = open(out, b"nmhd");
874879 full_head(out, 0, 0);
875- close(out, nmhd);
880+ close(out, nmhd)?;
876881 let dinf = open(out, b"dinf");
877882 let dref = open(out, b"dref");
878883 full_head(out, 0, 0);
879884 out.extend_from_slice(&1u32.to_be_bytes());
880885 let url = open(out, b"url ");
881886 full_head(out, 0, 1);
882- close(out, url);
883- close(out, dref);
884- close(out, dinf);
887+ close(out, url)?;
888+ close(out, dref)?;
889+ close(out, dinf)?;
885890
886891 let stbl = open(out, b"stbl");
887892 let stsd = open(out, b"stsd");
@@ -898,8 +903,8 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
898903 out.extend_from_slice(&0xFFFFu16.to_be_bytes()); // foreground: white
899904 }
900905 out.push(0); // empty font name
901- close(out, text);
902- close(out, stsd);
906+ close(out, text)?;
907+ close(out, stsd)?;
903908
904909 let stts = open(out, b"stts");
905910 full_head(out, 0, 0);
@@ -908,7 +913,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
908913 out.extend_from_slice(&1u32.to_be_bytes());
909914 out.extend_from_slice(&duration.to_be_bytes());
910915 }
911- close(out, stts);
916+ close(out, stts)?;
912917
913918 let stsc = open(out, b"stsc");
914919 full_head(out, 0, 0);
@@ -916,7 +921,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
916921 out.extend_from_slice(&1u32.to_be_bytes()); // first chunk
917922 out.extend_from_slice(&(chap.samples.len() as u32).to_be_bytes());
918923 out.extend_from_slice(&1u32.to_be_bytes()); // description index
919- close(out, stsc);
924+ close(out, stsc)?;
920925
921926 let stsz = open(out, b"stsz");
922927 full_head(out, 0, 0);
@@ -925,23 +930,24 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64
925930 for (_, size) in &chap.samples {
926931 out.extend_from_slice(&size.to_be_bytes());
927932 }
928- close(out, stsz);
933+ close(out, stsz)?;
929934
930935 let co64 = open(out, b"co64");
931936 full_head(out, 0, 0);
932937 out.extend_from_slice(&1u32.to_be_bytes());
933938 out.extend_from_slice(&(chap.offset + shift).to_be_bytes());
934- close(out, co64);
935- close(out, stbl);
936- close(out, minf);
937- close(out, mdia);
938- close(out, at);
939+ close(out, co64)?;
940+ close(out, stbl)?;
941+ close(out, minf)?;
942+ close(out, mdia)?;
943+ close(out, at)?;
944+ Ok(())
939945 }
940946
941947 /// The Nero `chpl` box in `moov/udta`: version 1 (its 4 extra bytes are
942948 /// reserved), start times in 100 ns units, and titles cut to 255 bytes. Written
943949 /// only for 1 to 255 chapters: the count is one byte.
944-fn write_chpl(out: &mut Vec<u8>, chapters: &[(u64, String)]) {
950+fn write_chpl(out: &mut Vec<u8>, chapters: &[(u64, String)]) -> Result<()> {
945951 let chpl = open(out, b"chpl");
946952 full_head(out, 1, 0);
947953 out.extend_from_slice(&0u32.to_be_bytes());
@@ -952,7 +958,8 @@ fn write_chpl(out: &mut Vec<u8>, chapters: &[(u64, String)]) {
952958 out.push(text.len() as u8);
953959 out.extend_from_slice(text.as_bytes());
954960 }
955- close(out, chpl);
961+ close(out, chpl)?;
962+ Ok(())
956963 }
957964
958965 /// `title` cut to at most `max` bytes, on a character boundary.
@@ -1043,7 +1050,7 @@ fn opus_pre_skip(track: &Track) -> i64 {
10431050 /// The tables of a QuickTime PCM track the way QuickTime and ffmpeg lay them
10441051 /// out: a *sample* is one frame (`frame_bytes`, one `stts` tick), and a chunk
10451052 /// holds as many samples as the packets written into it had frames.
1046-fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) {
1053+fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) -> Result<()> {
10471054 let frame_bytes = frame_bytes.max(1);
10481055 let total: u64 = track
10491056 .samples
@@ -1057,7 +1064,7 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) {
10571064 out.extend_from_slice(&(total as u32).to_be_bytes());
10581065 out.extend_from_slice(&1u32.to_be_bytes());
10591066 }
1060- close(out, stts);
1067+ close(out, stts)?;
10611068
10621069 // Frames per chunk: the chunk's packets are the next `chunk.1` samples.
10631070 let mut per_chunk = Vec::with_capacity(track.chunks.len());
@@ -1090,13 +1097,13 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) {
10901097 }
10911098 }
10921099 out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes());
1093- close(out, stsc);
1100+ close(out, stsc)?;
10941101
10951102 let stsz = open(out, b"stsz");
10961103 full_head(out, 0, 0);
10971104 out.extend_from_slice(&frame_bytes.to_be_bytes());
10981105 out.extend_from_slice(&(total as u32).to_be_bytes());
1099- close(out, stsz);
1106+ close(out, stsz)?;
11001107
11011108 let big = track
11021109 .chunks
@@ -1112,11 +1119,12 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) {
11121119 false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()),
11131120 }
11141121 }
1115- close(out, chunks);
1122+ close(out, chunks)?;
1123+ Ok(())
11161124 }
11171125
11181126 /// The `stts`/`ctts`/`stss`/`stsc`/`stsz` and chunk-offset tables of one track.
1119-fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
1127+fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) -> Result<()> {
11201128 if let (Some((bytes, _)), Some(audio)) =
11211129 (qt_pcm(track.info.params.codec), track.info.params.audio())
11221130 {
@@ -1125,8 +1133,8 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
11251133 track,
11261134 bytes * audio.layout.channel_count() as u32,
11271135 shift,
1128- );
1129- return;
1136+ )?;
1137+ return Ok(());
11301138 }
11311139 // stts: runs of equal durations, which for constant-rate video is one entry
11321140 // for the whole film.
@@ -1148,7 +1156,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
11481156 runs += 1;
11491157 }
11501158 out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes());
1151- close(out, stts);
1159+ close(out, stts)?;
11521160
11531161 // ctts, only where something is actually reordered.
11541162 let offsets: Vec<i64> = track.samples.iter().map(|s| s.cts - s.dts).collect();
@@ -1174,7 +1182,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
11741182 runs += 1;
11751183 }
11761184 out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes());
1177- close(out, ctts);
1185+ close(out, ctts)?;
11781186 }
11791187
11801188 // stss, only where some samples are not sync ones: no box at all means
@@ -1193,7 +1201,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
11931201 for n in syncs {
11941202 out.extend_from_slice(&n.to_be_bytes());
11951203 }
1196- close(out, stss);
1204+ close(out, stss)?;
11971205 }
11981206
11991207 // stsc: runs of chunks holding the same number of samples.
@@ -1215,7 +1223,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
12151223 }
12161224 }
12171225 out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes());
1218- close(out, stsc);
1226+ close(out, stsc)?;
12191227
12201228 // stsz: one size for all, where they really are all one size.
12211229 let stsz = open(out, b"stsz");
@@ -1233,7 +1241,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
12331241 out.extend_from_slice(&sample.size.to_be_bytes());
12341242 }
12351243 }
1236- close(out, stsz);
1244+ close(out, stsz)?;
12371245
12381246 // co64 where the file is big enough to need it, stco where it is not.
12391247 let big = track
@@ -1250,7 +1258,8 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) {
12501258 false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()),
12511259 }
12521260 }
1253- close(out, chunks);
1261+ close(out, chunks)?;
1262+ Ok(())
12541263 }
12551264
12561265 /// The sample entry name and configuration box of a video codec.
@@ -1323,22 +1332,22 @@ fn sample_entry(
13231332 out.extend_from_slice(&[0; 32]); // compressorname
13241333 out.extend_from_slice(&0x0018u16.to_be_bytes());
13251334 out.extend_from_slice(&(-1i16).to_be_bytes());
1326- colour_boxes(out, video.color, video.light);
1335+ colour_boxes(out, video.color, video.light)?;
13271336 if let Some(order) = field_order {
13281337 let (count, detail) = match order {
13291338 ec_core::Interlace::Progressive => (1u8, 0u8),
13301339 ec_core::Interlace::TopFieldFirst => (2, 1),
13311340 ec_core::Interlace::BottomFieldFirst => (2, 6),
13321341 };
1333- leaf(out, b"fiel", &[count, detail]);
1342+ leaf(out, b"fiel", &[count, detail])?;
13341343 }
13351344 let sar = video.sample_aspect_ratio;
13361345 let (n, d) = sar.map_or((1, 1), |s| (s.num() as u32, s.den() as u32));
13371346 let pasp = open(out, b"pasp");
13381347 out.extend_from_slice(&n.to_be_bytes());
13391348 out.extend_from_slice(&d.to_be_bytes());
1340- close(out, pasp);
1341- close(out, at);
1349+ close(out, pasp)?;
1350+ close(out, at)?;
13421351 return Ok(());
13431352 }
13441353 if let Some(name) = video_entry(codec) {
@@ -1364,13 +1373,13 @@ fn sample_entry(
13641373 out.extend_from_slice(&[0; 32]); // compressorname
13651374 out.extend_from_slice(&0x0018u16.to_be_bytes()); // depth: colour, no alpha
13661375 out.extend_from_slice(&(-1i16).to_be_bytes()); // pre_defined
1367- leaf(out, config_box(codec), config);
1368- colour_boxes(out, video.color, video.light);
1376+ leaf(out, config_box(codec), config)?;
1377+ colour_boxes(out, video.color, video.light)?;
13691378 if let Some(sar) = video.sample_aspect_ratio {
13701379 let pasp = open(out, b"pasp");
13711380 out.extend_from_slice(&(sar.num() as u32).to_be_bytes());
13721381 out.extend_from_slice(&(sar.den() as u32).to_be_bytes());
1373- close(out, pasp);
1382+ close(out, pasp)?;
13741383 }
13751384 // ffmpeg's order: after `colr`/`pasp`. field_count 1 / detail 0 is a
13761385 // progressive picture; 2 fields with detail 1 is top first (stored and
@@ -1382,9 +1391,9 @@ fn sample_entry(
13821391 ec_core::Interlace::TopFieldFirst => (2, 1),
13831392 ec_core::Interlace::BottomFieldFirst => (2, 6),
13841393 };
1385- leaf(out, b"fiel", &[count, detail]);
1394+ leaf(out, b"fiel", &[count, detail])?;
13861395 }
1387- close(out, at);
1396+ close(out, at)?;
13881397 return Ok(());
13891398 }
13901399 if codec == CodecId::Tx3g {
@@ -1411,10 +1420,10 @@ fn sample_entry(
14111420 out.extend_from_slice(&1u16.to_be_bytes());
14121421 out.push(5);
14131422 out.extend_from_slice(b"Serif");
1414- close(out, ftab);
1423+ close(out, ftab)?;
14151424 }
14161425 }
1417- close(out, at);
1426+ close(out, at)?;
14181427 return Ok(());
14191428 }
14201429
@@ -1477,10 +1486,10 @@ fn sample_entry(
14771486 out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame
14781487 out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant)
14791488 let wave = open(out, b"wave");
1480- leaf(out, b"frma", name);
1481- leaf(out, b"enda", &u16::from(little).to_be_bytes());
1489+ leaf(out, b"frma", name)?;
1490+ leaf(out, b"enda", &u16::from(little).to_be_bytes())?;
14821491 out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero
1483- close(out, wave);
1492+ close(out, wave)?;
14841493 }
14851494 None => {}
14861495 }
@@ -1494,7 +1503,7 @@ fn sample_entry(
14941503 })?;
14951504 let esds = open(out, b"esds");
14961505 out.extend_from_slice(&Esds::aac(asc).write());
1497- close(out, esds);
1506+ close(out, esds)?;
14981507 }
14991508 CodecId::Mp3 => {
15001509 let mut mp3 = Esds::aac(&[][..]);
@@ -1502,7 +1511,7 @@ fn sample_entry(
15021511 mp3.decoder_specific = None;
15031512 let esds = open(out, b"esds");
15041513 out.extend_from_slice(&mp3.write());
1505- close(out, esds);
1514+ close(out, esds)?;
15061515 }
15071516 CodecId::Ac3
15081517 | CodecId::EAc3
@@ -1522,23 +1531,23 @@ fn sample_entry(
15221531 CodecId::Alac if config.len() > 8 && &config[4..8] == b"alac" => {
15231532 out.extend_from_slice(config)
15241533 }
1525- CodecId::Alac => leaf(out, b"alac", config),
1526- CodecId::Opus => leaf(out, b"dOps", &dops(config)),
1527- _ => leaf(out, audio_config_box(codec), config),
1534+ CodecId::Alac => leaf(out, b"alac", config)?,
1535+ CodecId::Opus => leaf(out, b"dOps", &dops(config))?,
1536+ _ => leaf(out, audio_config_box(codec), config)?,
15281537 }
15291538 }
15301539 _ => {}
15311540 }
15321541 if chan || qt.is_some() {
1533- chan_box(out, &audio.layout);
1542+ chan_box(out, &audio.layout)?;
15341543 }
1535- close(out, at);
1544+ close(out, at)?;
15361545 Ok(())
15371546 }
15381547
15391548 /// `colr`, and the two HDR boxes where the grade stated them — metadata a remux
15401549 /// must not quietly drop, because nothing downstream can reconstruct it.
1541-fn colour_boxes(out: &mut Vec<u8>, color: ec_core::ColorInfo, light: ContentLight) {
1550+fn colour_boxes(out: &mut Vec<u8>, color: ec_core::ColorInfo, light: ContentLight) -> Result<()> {
15421551 if color != ec_core::ColorInfo::default() {
15431552 let colr = open(out, b"colr");
15441553 out.extend_from_slice(b"nclx");
@@ -1546,7 +1555,7 @@ fn colour_boxes(out: &mut Vec<u8>, color: ec_core::ColorInfo, light: ContentLigh
15461555 out.extend_from_slice(&u16::from(color.transfer).to_be_bytes());
15471556 out.extend_from_slice(&u16::from(color.matrix).to_be_bytes());
15481557 out.push(u8::from(color.full_range) << 7);
1549- close(out, colr);
1558+ close(out, colr)?;
15501559 }
15511560 if light.mastering_max.is_some()
15521561 || light.mastering_min.is_some()
@@ -1566,14 +1575,15 @@ fn colour_boxes(out: &mut Vec<u8>, color: ec_core::ColorInfo, light: ContentLigh
15661575 };
15671576 out.extend_from_slice(&nits(light.mastering_max));
15681577 out.extend_from_slice(&nits(light.mastering_min));
1569- close(out, mdcv);
1578+ close(out, mdcv)?;
15701579 }
15711580 if light.max_cll.is_some() || light.max_fall.is_some() {
15721581 let clli = open(out, b"clli");
15731582 out.extend_from_slice(&(light.max_cll.unwrap_or(0.0).round() as u16).to_be_bytes());
15741583 out.extend_from_slice(&(light.max_fall.unwrap_or(0.0).round() as u16).to_be_bytes());
1575- close(out, clli);
1584+ close(out, clli)?;
15761585 }
1586+ Ok(())
15771587 }
15781588
15791589 /// BT.2020 green, blue, red and D65, in the 0.00002 units an `mdcv` states them.
@@ -1603,21 +1613,22 @@ fn qt_pcm(codec: CodecId) -> Option<(u32, bool)> {
16031613
16041614 /// The QuickTime `chan` box for the layouts that have a stock one, as ffmpeg
16051615 /// writes it: a layout tag for mono and stereo, a channel bitmap for 5.1.
1606-fn chan_box(out: &mut Vec<u8>, layout: &ec_core::ChannelLayout) {
1616+fn chan_box(out: &mut Vec<u8>, layout: &ec_core::ChannelLayout) -> Result<()> {
16071617 use ec_core::ChannelLayout;
16081618 let (tag, bitmap) = match layout {
16091619 ChannelLayout::Mono => (0x0064_0001u32, 0u32),
16101620 ChannelLayout::Stereo => (0x0065_0002, 0),
16111621 // UseChannelBitmap; L R C LFE Ls Rs.
16121622 ChannelLayout::Surround5_1 => (0x0001_0000, 0x3f),
1613- _ => return,
1623+ _ => return Ok(()),
16141624 };
16151625 let at = open(out, b"chan");
16161626 full_head(out, 0, 0);
16171627 out.extend_from_slice(&tag.to_be_bytes());
16181628 out.extend_from_slice(&bitmap.to_be_bytes());
16191629 out.extend_from_slice(&0u32.to_be_bytes()); // number of descriptions
1620- close(out, at);
1630+ close(out, at)?;
1631+ Ok(())
16211632 }
16221633
16231634 fn audio_entry(codec: CodecId) -> Result<&'static [u8; 4]> {