diff --git a/crates/ec-mp4/src/boxes.rs b/crates/ec-mp4/src/boxes.rs index 5d07fc18..d9435a6f 100644 --- a/crates/ec-mp4/src/boxes.rs +++ b/crates/ec-mp4/src/boxes.rs @@ -228,16 +228,17 @@ pub fn open(out: &mut Vec, kind: &FourCc) -> usize { } /// Finish the box [`open`] started at `at` by writing its size. -pub fn close(out: &mut [u8], at: usize) { - let size = (out.len() - at) as u32; +pub fn close(out: &mut [u8], at: usize) -> Result<()> { + let size = u32::try_from(out.len() - at).map_err(|_| Error::corrupt("mp4: a box over 4 GB"))?; out[at..at + 4].copy_from_slice(&size.to_be_bytes()); + Ok(()) } /// A whole box in one call, for the leaves. -pub fn leaf(out: &mut Vec, kind: &FourCc, payload: &[u8]) { +pub fn leaf(out: &mut Vec, kind: &FourCc, payload: &[u8]) -> Result<()> { let at = open(out, kind); out.extend_from_slice(payload); - close(out, at); + close(out, at) } /// The `version`/`flags` word a FullBox starts with. @@ -259,7 +260,7 @@ mod tests { #[test] fn walks_32_and_64_bit_sizes() { let mut out = Vec::new(); - leaf(&mut out, b"ftyp", &[1, 2, 3, 4]); + leaf(&mut out, b"ftyp", &[1, 2, 3, 4]).unwrap(); // The same box written in the 64-bit form a big mdat uses. out.extend_from_slice(&1u32.to_be_bytes()); out.extend_from_slice(b"mdat"); @@ -274,7 +275,7 @@ mod tests { #[test] fn a_size_that_does_not_fit_is_corrupt_not_a_panic() { let mut out = Vec::new(); - leaf(&mut out, b"moov", &[0; 4]); + leaf(&mut out, b"moov", &[0; 4]).unwrap(); out[3] = 0xFF; // 255 bytes of moov inside 12 assert!(types(&out).is_err()); diff --git a/crates/ec-mp4/src/demux.rs b/crates/ec-mp4/src/demux.rs index 708bf2f7..6a86a114 100644 --- a/crates/ec-mp4/src/demux.rs +++ b/crates/ec-mp4/src/demux.rs @@ -1950,7 +1950,7 @@ mod tests { assert_eq!(unpack_language(0), None); let mut udta = Vec::new(); - boxes::leaf(&mut udta, b"name", b"Turkish\0"); + boxes::leaf(&mut udta, b"name", b"Turkish\0").unwrap(); assert_eq!(udta_name(&udta).unwrap().as_deref(), Some("Turkish")); } diff --git a/crates/ec-mp4/src/mux.rs b/crates/ec-mp4/src/mux.rs index 165d1686..36a00fa9 100644 --- a/crates/ec-mp4/src/mux.rs +++ b/crates/ec-mp4/src/mux.rs @@ -317,7 +317,7 @@ impl Mp4Muxer { head.extend_from_slice(b"mp41"); } } - close(&mut head, at); + close(&mut head, at)?; self.mdat_at = head.len() as u64; // The 64-bit form of an `mdat` header, always: a two-hour 4K export goes // past 4 GB and the placeholder cannot grow later without moving every @@ -333,7 +333,7 @@ impl Mp4Muxer { /// Sample durations, resolved: a packet that stated one keeps it, and one /// that did not gets the step to the next sample — which is what the last /// sample has no next for, so it repeats the step before it. - fn resolve_durations(track: &mut Track) { + fn resolve_durations(track: &mut Track) -> Result<()> { for i in 0..track.samples.len() { if track.samples[i].duration != 0 { continue; @@ -344,8 +344,10 @@ impl Mp4Muxer { .checked_sub(1) .map_or(0, |prev| i64::from(track.samples[prev].duration)), }; - track.samples[i].duration = u32::try_from(step.max(0)).unwrap_or(0); + track.samples[i].duration = u32::try_from(step.max(0)) + .map_err(|_| Error::corrupt("mp4: a sample duration over 32 bits"))?; } + Ok(()) } /// The film's length in movie milliseconds: the longest track's presentation. @@ -392,13 +394,15 @@ impl Mp4Muxer { let mut samples = Vec::with_capacity(self.chapters.len()); for (i, (_, title)) in self.chapters.iter().enumerate() { let next = starts.get(i + 1).copied().unwrap_or(end_ms); - let mut duration = (next - starts[i]).max(0) as u32; - if i + 1 == starts.len() { - // Readers that end the last chapter at the text track's media duration - // (ffmpeg) do not count the leading empty edit, so the media is padded by - // the first chapter's start; the edit list below still plays only to the end. - duration += first_ms.max(0) as u32; - } + // Readers that end the last chapter at the text track's media duration + // (ffmpeg) do not count the leading empty edit, so the media is padded by + // the first chapter's start; the edit list below still plays only to the end. + let padding = match i + 1 == starts.len() { + true => first_ms.max(0), + false => 0, + }; + let duration = u32::try_from((next - starts[i]).max(0) + padding) + .map_err(|_| Error::corrupt("mp4: a chapter over 32 bits of milliseconds"))?; let text = clip(title, usize::from(u16::MAX)); payload.extend_from_slice(&(text.len() as u16).to_be_bytes()); payload.extend_from_slice(text.as_bytes()); @@ -419,7 +423,7 @@ impl Mp4Muxer { fn seal_mdat(&mut self) -> Result> { self.start()?; for track in &mut self.tracks { - Mp4Muxer::::resolve_durations(track); + Mp4Muxer::::resolve_durations(track)?; } // The chapter text samples are media, so they go in the mdat before its // size is known. @@ -457,7 +461,7 @@ impl Mp4Muxer { moov.extend_from_slice(&[0; 24]); // pre_defined let next_id = chapter_id.unwrap_or(self.tracks.len() as u32) + 1; moov.extend_from_slice(&next_id.to_be_bytes()); - close(&mut moov, mvhd); + close(&mut moov, mvhd)?; for i in 0..self.tracks.len() { let chapter_ref = match reference == Some(i) { @@ -467,20 +471,20 @@ impl Mp4Muxer { self.write_trak(&mut moov, i, duration, chapter_ref, shift)?; } if let (Some(text), Some(id)) = (chapters, chapter_id) { - write_chapter_trak(&mut moov, id, text, shift); + write_chapter_trak(&mut moov, id, text, shift)?; } let chpl = (1..=255).contains(&self.chapters.len()); if self.title.is_some() || chpl { let udta = open(&mut moov, b"udta"); if let Some(title) = &self.title { - leaf(&mut moov, b"name", title.as_bytes()); + leaf(&mut moov, b"name", title.as_bytes())?; } if chpl { - write_chpl(&mut moov, &self.chapters); + write_chpl(&mut moov, &self.chapters)?; } - close(&mut moov, udta); + close(&mut moov, udta)?; } - close(&mut moov, at); + close(&mut moov, at)?; Ok(moov) } @@ -532,14 +536,14 @@ impl Mp4Muxer { }; out.extend_from_slice(&(width << 16).to_be_bytes()); out.extend_from_slice(&(height << 16).to_be_bytes()); - close(out, tkhd); + close(out, tkhd)?; if let Some(id) = chapter_ref { let tref = open(out, b"tref"); let chap = open(out, b"chap"); out.extend_from_slice(&id.to_be_bytes()); - close(out, chap); - close(out, tref); + close(out, chap)?; + close(out, tref)?; } // An `elst`, where the first picture is not the first *decoded* one, or @@ -558,8 +562,8 @@ impl Mp4Muxer { out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); out.extend_from_slice(&media_time.to_be_bytes()); out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0 - close(out, elst); - close(out, edts); + close(out, elst)?; + close(out, edts)?; } let priming = presentation_start(track) .max(track.audio_delay) @@ -577,8 +581,8 @@ impl Mp4Muxer { out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); out.extend_from_slice(&(priming as u32).to_be_bytes()); out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0 - close(out, elst); - close(out, edts); + close(out, elst)?; + close(out, edts)?; } let mdia = open(out, b"mdia"); @@ -590,7 +594,7 @@ impl Mp4Muxer { out.extend_from_slice(&(track_ticks(track).max(0) as u32).to_be_bytes()); out.extend_from_slice(&pack_language(track.info.language.as_deref()).to_be_bytes()); out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined - close(out, mdhd); + close(out, mdhd)?; let (handler, handler_name) = match codec.media_type() { ec_core::MediaType::Video => (b"vide", "VideoHandler"), @@ -604,7 +608,7 @@ impl Mp4Muxer { out.extend_from_slice(&[0; 12]); // reserved out.extend_from_slice(handler_name.as_bytes()); out.push(0); - close(out, hdlr); + close(out, hdlr)?; let minf = open(out, b"minf"); match codec.media_type() { @@ -612,18 +616,18 @@ impl Mp4Muxer { let vmhd = open(out, b"vmhd"); full_head(out, 0, 1); out.extend_from_slice(&[0; 8]); // graphicsmode + opcolor - close(out, vmhd); + close(out, vmhd)?; } ec_core::MediaType::Audio => { let smhd = open(out, b"smhd"); full_head(out, 0, 0); out.extend_from_slice(&[0; 4]); // balance + reserved - close(out, smhd); + close(out, smhd)?; } ec_core::MediaType::Subtitle => { let nmhd = open(out, b"nmhd"); full_head(out, 0, 0); - close(out, nmhd); + close(out, nmhd)?; } } let dinf = open(out, b"dinf"); @@ -632,27 +636,27 @@ impl Mp4Muxer { out.extend_from_slice(&1u32.to_be_bytes()); let url = open(out, b"url "); full_head(out, 0, 1); // flag 1: the media is in this very file - close(out, url); - close(out, dref); - close(out, dinf); + close(out, url)?; + close(out, dref)?; + close(out, dinf)?; let stbl = open(out, b"stbl"); let stsd = open(out, b"stsd"); full_head(out, 0, 0); out.extend_from_slice(&1u32.to_be_bytes()); sample_entry(out, &track.info, track.field_order, track.chan)?; - close(out, stsd); - sample_tables(out, track, shift); - close(out, stbl); - close(out, minf); - close(out, mdia); + close(out, stsd)?; + sample_tables(out, track, shift)?; + close(out, stbl)?; + close(out, minf)?; + close(out, mdia)?; if let Some(title) = &track.title { let udta = open(out, b"udta"); - leaf(out, b"name", title.as_bytes()); - close(out, udta); + leaf(out, b"name", title.as_bytes())?; + close(out, udta)?; } - close(out, at); + close(out, at)?; Ok(()) } } @@ -718,7 +722,8 @@ impl Muxer for Mp4Muxer { size, dts, cts, - duration: packet.duration.map_or(0, |d| at(d).max(0) as u32), + duration: u32::try_from(packet.duration.map_or(0, |d| at(d).max(0))) + .map_err(|_| Error::corrupt("mp4: a sample duration over 32 bits"))?, sync: packet.flags.keyframe, }); match track.chunk_end == pos { @@ -812,7 +817,7 @@ impl Mp4Muxer { /// clock (milliseconds), with a `co64` offset so it stays right past 4 GB. It is /// disabled, as a chapter track is in a QuickTime file, and its `edts` puts the /// first chapter at its own start when that is not zero. -fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64) { +fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64) -> Result<()> { let at = open(out, b"trak"); let tkhd = open(out, b"tkhd"); @@ -828,7 +833,7 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&0u16.to_be_bytes()); out.extend_from_slice(&UNITY_MATRIX); out.extend_from_slice(&[0; 8]); // width, height - close(out, tkhd); + close(out, tkhd)?; if chap.first_ms > 0 { let edts = open(out, b"edts"); @@ -844,8 +849,8 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&length.to_be_bytes()); out.extend_from_slice(&0u32.to_be_bytes()); out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); - close(out, elst); - close(out, edts); + close(out, elst)?; + close(out, edts)?; } let mdia = open(out, b"mdia"); @@ -858,7 +863,7 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&(total as u32).to_be_bytes()); out.extend_from_slice(&pack_language(None).to_be_bytes()); out.extend_from_slice(&0u16.to_be_bytes()); - close(out, mdhd); + close(out, mdhd)?; let hdlr = open(out, b"hdlr"); full_head(out, 0, 0); @@ -867,21 +872,21 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&[0; 12]); out.extend_from_slice(b"ChapterHandler"); out.push(0); - close(out, hdlr); + close(out, hdlr)?; let minf = open(out, b"minf"); let nmhd = open(out, b"nmhd"); full_head(out, 0, 0); - close(out, nmhd); + close(out, nmhd)?; let dinf = open(out, b"dinf"); let dref = open(out, b"dref"); full_head(out, 0, 0); out.extend_from_slice(&1u32.to_be_bytes()); let url = open(out, b"url "); full_head(out, 0, 1); - close(out, url); - close(out, dref); - close(out, dinf); + close(out, url)?; + close(out, dref)?; + close(out, dinf)?; let stbl = open(out, b"stbl"); let stsd = open(out, b"stsd"); @@ -898,8 +903,8 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&0xFFFFu16.to_be_bytes()); // foreground: white } out.push(0); // empty font name - close(out, text); - close(out, stsd); + close(out, text)?; + close(out, stsd)?; let stts = open(out, b"stts"); full_head(out, 0, 0); @@ -908,7 +913,7 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&1u32.to_be_bytes()); out.extend_from_slice(&duration.to_be_bytes()); } - close(out, stts); + close(out, stts)?; let stsc = open(out, b"stsc"); full_head(out, 0, 0); @@ -916,7 +921,7 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 out.extend_from_slice(&1u32.to_be_bytes()); // first chunk out.extend_from_slice(&(chap.samples.len() as u32).to_be_bytes()); out.extend_from_slice(&1u32.to_be_bytes()); // description index - close(out, stsc); + close(out, stsc)?; let stsz = open(out, b"stsz"); full_head(out, 0, 0); @@ -925,23 +930,24 @@ fn write_chapter_trak(out: &mut Vec, id: u32, chap: &ChapterText, shift: u64 for (_, size) in &chap.samples { out.extend_from_slice(&size.to_be_bytes()); } - close(out, stsz); + close(out, stsz)?; let co64 = open(out, b"co64"); full_head(out, 0, 0); out.extend_from_slice(&1u32.to_be_bytes()); out.extend_from_slice(&(chap.offset + shift).to_be_bytes()); - close(out, co64); - close(out, stbl); - close(out, minf); - close(out, mdia); - close(out, at); + close(out, co64)?; + close(out, stbl)?; + close(out, minf)?; + close(out, mdia)?; + close(out, at)?; + Ok(()) } /// The Nero `chpl` box in `moov/udta`: version 1 (its 4 extra bytes are /// reserved), start times in 100 ns units, and titles cut to 255 bytes. Written /// only for 1 to 255 chapters: the count is one byte. -fn write_chpl(out: &mut Vec, chapters: &[(u64, String)]) { +fn write_chpl(out: &mut Vec, chapters: &[(u64, String)]) -> Result<()> { let chpl = open(out, b"chpl"); full_head(out, 1, 0); out.extend_from_slice(&0u32.to_be_bytes()); @@ -952,7 +958,8 @@ fn write_chpl(out: &mut Vec, chapters: &[(u64, String)]) { out.push(text.len() as u8); out.extend_from_slice(text.as_bytes()); } - close(out, chpl); + close(out, chpl)?; + Ok(()) } /// `title` cut to at most `max` bytes, on a character boundary. @@ -1043,7 +1050,7 @@ fn opus_pre_skip(track: &Track) -> i64 { /// The tables of a QuickTime PCM track the way QuickTime and ffmpeg lay them /// out: a *sample* is one frame (`frame_bytes`, one `stts` tick), and a chunk /// holds as many samples as the packets written into it had frames. -fn pcm_tables(out: &mut Vec, track: &Track, frame_bytes: u32, shift: u64) { +fn pcm_tables(out: &mut Vec, track: &Track, frame_bytes: u32, shift: u64) -> Result<()> { let frame_bytes = frame_bytes.max(1); let total: u64 = track .samples @@ -1057,7 +1064,7 @@ fn pcm_tables(out: &mut Vec, track: &Track, frame_bytes: u32, shift: u64) { out.extend_from_slice(&(total as u32).to_be_bytes()); out.extend_from_slice(&1u32.to_be_bytes()); } - close(out, stts); + close(out, stts)?; // Frames per chunk: the chunk's packets are the next `chunk.1` samples. let mut per_chunk = Vec::with_capacity(track.chunks.len()); @@ -1090,13 +1097,13 @@ fn pcm_tables(out: &mut Vec, track: &Track, frame_bytes: u32, shift: u64) { } } out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); - close(out, stsc); + close(out, stsc)?; let stsz = open(out, b"stsz"); full_head(out, 0, 0); out.extend_from_slice(&frame_bytes.to_be_bytes()); out.extend_from_slice(&(total as u32).to_be_bytes()); - close(out, stsz); + close(out, stsz)?; let big = track .chunks @@ -1112,11 +1119,12 @@ fn pcm_tables(out: &mut Vec, track: &Track, frame_bytes: u32, shift: u64) { false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()), } } - close(out, chunks); + close(out, chunks)?; + Ok(()) } /// The `stts`/`ctts`/`stss`/`stsc`/`stsz` and chunk-offset tables of one track. -fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { +fn sample_tables(out: &mut Vec, track: &Track, shift: u64) -> Result<()> { if let (Some((bytes, _)), Some(audio)) = (qt_pcm(track.info.params.codec), track.info.params.audio()) { @@ -1125,8 +1133,8 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { track, bytes * audio.layout.channel_count() as u32, shift, - ); - return; + )?; + return Ok(()); } // stts: runs of equal durations, which for constant-rate video is one entry // for the whole film. @@ -1148,7 +1156,7 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { runs += 1; } out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); - close(out, stts); + close(out, stts)?; // ctts, only where something is actually reordered. let offsets: Vec = track.samples.iter().map(|s| s.cts - s.dts).collect(); @@ -1174,7 +1182,7 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { runs += 1; } out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); - close(out, ctts); + close(out, ctts)?; } // stss, only where some samples are not sync ones: no box at all means @@ -1193,7 +1201,7 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { for n in syncs { out.extend_from_slice(&n.to_be_bytes()); } - close(out, stss); + close(out, stss)?; } // stsc: runs of chunks holding the same number of samples. @@ -1215,7 +1223,7 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { } } out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); - close(out, stsc); + close(out, stsc)?; // stsz: one size for all, where they really are all one size. let stsz = open(out, b"stsz"); @@ -1233,7 +1241,7 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { out.extend_from_slice(&sample.size.to_be_bytes()); } } - close(out, stsz); + close(out, stsz)?; // co64 where the file is big enough to need it, stco where it is not. let big = track @@ -1250,7 +1258,8 @@ fn sample_tables(out: &mut Vec, track: &Track, shift: u64) { false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()), } } - close(out, chunks); + close(out, chunks)?; + Ok(()) } /// The sample entry name and configuration box of a video codec. @@ -1323,22 +1332,22 @@ fn sample_entry( out.extend_from_slice(&[0; 32]); // compressorname out.extend_from_slice(&0x0018u16.to_be_bytes()); out.extend_from_slice(&(-1i16).to_be_bytes()); - colour_boxes(out, video.color, video.light); + colour_boxes(out, video.color, video.light)?; if let Some(order) = field_order { let (count, detail) = match order { ec_core::Interlace::Progressive => (1u8, 0u8), ec_core::Interlace::TopFieldFirst => (2, 1), ec_core::Interlace::BottomFieldFirst => (2, 6), }; - leaf(out, b"fiel", &[count, detail]); + leaf(out, b"fiel", &[count, detail])?; } let sar = video.sample_aspect_ratio; let (n, d) = sar.map_or((1, 1), |s| (s.num() as u32, s.den() as u32)); let pasp = open(out, b"pasp"); out.extend_from_slice(&n.to_be_bytes()); out.extend_from_slice(&d.to_be_bytes()); - close(out, pasp); - close(out, at); + close(out, pasp)?; + close(out, at)?; return Ok(()); } if let Some(name) = video_entry(codec) { @@ -1364,13 +1373,13 @@ fn sample_entry( out.extend_from_slice(&[0; 32]); // compressorname out.extend_from_slice(&0x0018u16.to_be_bytes()); // depth: colour, no alpha out.extend_from_slice(&(-1i16).to_be_bytes()); // pre_defined - leaf(out, config_box(codec), config); - colour_boxes(out, video.color, video.light); + leaf(out, config_box(codec), config)?; + colour_boxes(out, video.color, video.light)?; if let Some(sar) = video.sample_aspect_ratio { let pasp = open(out, b"pasp"); out.extend_from_slice(&(sar.num() as u32).to_be_bytes()); out.extend_from_slice(&(sar.den() as u32).to_be_bytes()); - close(out, pasp); + close(out, pasp)?; } // ffmpeg's order: after `colr`/`pasp`. field_count 1 / detail 0 is a // progressive picture; 2 fields with detail 1 is top first (stored and @@ -1382,9 +1391,9 @@ fn sample_entry( ec_core::Interlace::TopFieldFirst => (2, 1), ec_core::Interlace::BottomFieldFirst => (2, 6), }; - leaf(out, b"fiel", &[count, detail]); + leaf(out, b"fiel", &[count, detail])?; } - close(out, at); + close(out, at)?; return Ok(()); } if codec == CodecId::Tx3g { @@ -1411,10 +1420,10 @@ fn sample_entry( out.extend_from_slice(&1u16.to_be_bytes()); out.push(5); out.extend_from_slice(b"Serif"); - close(out, ftab); + close(out, ftab)?; } } - close(out, at); + close(out, at)?; return Ok(()); } @@ -1477,10 +1486,10 @@ fn sample_entry( out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) let wave = open(out, b"wave"); - leaf(out, b"frma", name); - leaf(out, b"enda", &u16::from(little).to_be_bytes()); + leaf(out, b"frma", name)?; + leaf(out, b"enda", &u16::from(little).to_be_bytes())?; out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero - close(out, wave); + close(out, wave)?; } None => {} } @@ -1494,7 +1503,7 @@ fn sample_entry( })?; let esds = open(out, b"esds"); out.extend_from_slice(&Esds::aac(asc).write()); - close(out, esds); + close(out, esds)?; } CodecId::Mp3 => { let mut mp3 = Esds::aac(&[][..]); @@ -1502,7 +1511,7 @@ fn sample_entry( mp3.decoder_specific = None; let esds = open(out, b"esds"); out.extend_from_slice(&mp3.write()); - close(out, esds); + close(out, esds)?; } CodecId::Ac3 | CodecId::EAc3 @@ -1522,23 +1531,23 @@ fn sample_entry( CodecId::Alac if config.len() > 8 && &config[4..8] == b"alac" => { out.extend_from_slice(config) } - CodecId::Alac => leaf(out, b"alac", config), - CodecId::Opus => leaf(out, b"dOps", &dops(config)), - _ => leaf(out, audio_config_box(codec), config), + CodecId::Alac => leaf(out, b"alac", config)?, + CodecId::Opus => leaf(out, b"dOps", &dops(config))?, + _ => leaf(out, audio_config_box(codec), config)?, } } _ => {} } if chan || qt.is_some() { - chan_box(out, &audio.layout); + chan_box(out, &audio.layout)?; } - close(out, at); + close(out, at)?; Ok(()) } /// `colr`, and the two HDR boxes where the grade stated them — metadata a remux /// must not quietly drop, because nothing downstream can reconstruct it. -fn colour_boxes(out: &mut Vec, color: ec_core::ColorInfo, light: ContentLight) { +fn colour_boxes(out: &mut Vec, color: ec_core::ColorInfo, light: ContentLight) -> Result<()> { if color != ec_core::ColorInfo::default() { let colr = open(out, b"colr"); out.extend_from_slice(b"nclx"); @@ -1546,7 +1555,7 @@ fn colour_boxes(out: &mut Vec, color: ec_core::ColorInfo, light: ContentLigh out.extend_from_slice(&u16::from(color.transfer).to_be_bytes()); out.extend_from_slice(&u16::from(color.matrix).to_be_bytes()); out.push(u8::from(color.full_range) << 7); - close(out, colr); + close(out, colr)?; } if light.mastering_max.is_some() || light.mastering_min.is_some() @@ -1566,14 +1575,15 @@ fn colour_boxes(out: &mut Vec, color: ec_core::ColorInfo, light: ContentLigh }; out.extend_from_slice(&nits(light.mastering_max)); out.extend_from_slice(&nits(light.mastering_min)); - close(out, mdcv); + close(out, mdcv)?; } if light.max_cll.is_some() || light.max_fall.is_some() { let clli = open(out, b"clli"); out.extend_from_slice(&(light.max_cll.unwrap_or(0.0).round() as u16).to_be_bytes()); out.extend_from_slice(&(light.max_fall.unwrap_or(0.0).round() as u16).to_be_bytes()); - close(out, clli); + close(out, clli)?; } + Ok(()) } /// 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)> { /// The QuickTime `chan` box for the layouts that have a stock one, as ffmpeg /// writes it: a layout tag for mono and stereo, a channel bitmap for 5.1. -fn chan_box(out: &mut Vec, layout: &ec_core::ChannelLayout) { +fn chan_box(out: &mut Vec, layout: &ec_core::ChannelLayout) -> Result<()> { use ec_core::ChannelLayout; let (tag, bitmap) = match layout { ChannelLayout::Mono => (0x0064_0001u32, 0u32), ChannelLayout::Stereo => (0x0065_0002, 0), // UseChannelBitmap; L R C LFE Ls Rs. ChannelLayout::Surround5_1 => (0x0001_0000, 0x3f), - _ => return, + _ => return Ok(()), }; let at = open(out, b"chan"); full_head(out, 0, 0); out.extend_from_slice(&tag.to_be_bytes()); out.extend_from_slice(&bitmap.to_be_bytes()); out.extend_from_slice(&0u32.to_be_bytes()); // number of descriptions - close(out, at); + close(out, at)?; + Ok(()) } fn audio_entry(codec: CodecId) -> Result<&'static [u8; 4]> {