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 commit130fe27519911b075debaf633b75fe87f930fb7bParent: 22f0f3f3 changed files +124 -112
- crates/ec-mp4/src/boxes.rs +7 -6
- crates/ec-mp4/src/demux.rs +1 -1
- crates/ec-mp4/src/mux.rs +116 -105
crates/ec-mp4/src/boxes.rsmodified+7 -6
| @@ -228,16 +228,17 @@ pub fn open(out: &mut Vec<u8>, kind: &FourCc) -> usize { | ||
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /// 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"))?; | |
| 233 | 233 | out[at..at + 4].copy_from_slice(&size.to_be_bytes()); |
| 234 | + Ok(()) | |
| 234 | 235 | } |
| 235 | 236 | |
| 236 | 237 | /// 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<()> { | |
| 238 | 239 | let at = open(out, kind); |
| 239 | 240 | out.extend_from_slice(payload); |
| 240 | - close(out, at); | |
| 241 | + close(out, at) | |
| 241 | 242 | } |
| 242 | 243 | |
| 243 | 244 | /// The `version`/`flags` word a FullBox starts with. |
| @@ -259,7 +260,7 @@ mod tests { | ||
| 259 | 260 | #[test] |
| 260 | 261 | fn walks_32_and_64_bit_sizes() { |
| 261 | 262 | 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(); | |
| 263 | 264 | // The same box written in the 64-bit form a big mdat uses. |
| 264 | 265 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 265 | 266 | out.extend_from_slice(b"mdat"); |
| @@ -274,7 +275,7 @@ mod tests { | ||
| 274 | 275 | #[test] |
| 275 | 276 | fn a_size_that_does_not_fit_is_corrupt_not_a_panic() { |
| 276 | 277 | let mut out = Vec::new(); |
| 277 | - leaf(&mut out, b"moov", &[0; 4]); | |
| 278 | + leaf(&mut out, b"moov", &[0; 4]).unwrap(); | |
| 278 | 279 | out[3] = 0xFF; // 255 bytes of moov inside 12 |
| 279 | 280 | assert!(types(&out).is_err()); |
| 280 | 281 |
crates/ec-mp4/src/demux.rsmodified+1 -1
| @@ -1950,7 +1950,7 @@ mod tests { | ||
| 1950 | 1950 | assert_eq!(unpack_language(0), None); |
| 1951 | 1951 | |
| 1952 | 1952 | 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(); | |
| 1954 | 1954 | assert_eq!(udta_name(&udta).unwrap().as_deref(), Some("Turkish")); |
| 1955 | 1955 | } |
| 1956 | 1956 |
crates/ec-mp4/src/mux.rsmodified+116 -105
| @@ -317,7 +317,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 317 | 317 | head.extend_from_slice(b"mp41"); |
| 318 | 318 | } |
| 319 | 319 | } |
| 320 | - close(&mut head, at); | |
| 320 | + close(&mut head, at)?; | |
| 321 | 321 | self.mdat_at = head.len() as u64; |
| 322 | 322 | // The 64-bit form of an `mdat` header, always: a two-hour 4K export goes |
| 323 | 323 | // past 4 GB and the placeholder cannot grow later without moving every |
| @@ -333,7 +333,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 333 | 333 | /// Sample durations, resolved: a packet that stated one keeps it, and one |
| 334 | 334 | /// that did not gets the step to the next sample — which is what the last |
| 335 | 335 | /// 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<()> { | |
| 337 | 337 | for i in 0..track.samples.len() { |
| 338 | 338 | if track.samples[i].duration != 0 { |
| 339 | 339 | continue; |
| @@ -344,8 +344,10 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 344 | 344 | .checked_sub(1) |
| 345 | 345 | .map_or(0, |prev| i64::from(track.samples[prev].duration)), |
| 346 | 346 | }; |
| 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"))?; | |
| 348 | 349 | } |
| 350 | + Ok(()) | |
| 349 | 351 | } |
| 350 | 352 | |
| 351 | 353 | /// The film's length in movie milliseconds: the longest track's presentation. |
| @@ -392,13 +394,15 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 392 | 394 | let mut samples = Vec::with_capacity(self.chapters.len()); |
| 393 | 395 | for (i, (_, title)) in self.chapters.iter().enumerate() { |
| 394 | 396 | 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"))?; | |
| 402 | 406 | let text = clip(title, usize::from(u16::MAX)); |
| 403 | 407 | payload.extend_from_slice(&(text.len() as u16).to_be_bytes()); |
| 404 | 408 | payload.extend_from_slice(text.as_bytes()); |
| @@ -419,7 +423,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 419 | 423 | fn seal_mdat(&mut self) -> Result<Option<ChapterText>> { |
| 420 | 424 | self.start()?; |
| 421 | 425 | for track in &mut self.tracks { |
| 422 | - Mp4Muxer::<W>::resolve_durations(track); | |
| 426 | + Mp4Muxer::<W>::resolve_durations(track)?; | |
| 423 | 427 | } |
| 424 | 428 | // The chapter text samples are media, so they go in the mdat before its |
| 425 | 429 | // size is known. |
| @@ -457,7 +461,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 457 | 461 | moov.extend_from_slice(&[0; 24]); // pre_defined |
| 458 | 462 | let next_id = chapter_id.unwrap_or(self.tracks.len() as u32) + 1; |
| 459 | 463 | moov.extend_from_slice(&next_id.to_be_bytes()); |
| 460 | - close(&mut moov, mvhd); | |
| 464 | + close(&mut moov, mvhd)?; | |
| 461 | 465 | |
| 462 | 466 | for i in 0..self.tracks.len() { |
| 463 | 467 | let chapter_ref = match reference == Some(i) { |
| @@ -467,20 +471,20 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 467 | 471 | self.write_trak(&mut moov, i, duration, chapter_ref, shift)?; |
| 468 | 472 | } |
| 469 | 473 | 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)?; | |
| 471 | 475 | } |
| 472 | 476 | let chpl = (1..=255).contains(&self.chapters.len()); |
| 473 | 477 | if self.title.is_some() || chpl { |
| 474 | 478 | let udta = open(&mut moov, b"udta"); |
| 475 | 479 | if let Some(title) = &self.title { |
| 476 | - leaf(&mut moov, b"name", title.as_bytes()); | |
| 480 | + leaf(&mut moov, b"name", title.as_bytes())?; | |
| 477 | 481 | } |
| 478 | 482 | if chpl { |
| 479 | - write_chpl(&mut moov, &self.chapters); | |
| 483 | + write_chpl(&mut moov, &self.chapters)?; | |
| 480 | 484 | } |
| 481 | - close(&mut moov, udta); | |
| 485 | + close(&mut moov, udta)?; | |
| 482 | 486 | } |
| 483 | - close(&mut moov, at); | |
| 487 | + close(&mut moov, at)?; | |
| 484 | 488 | Ok(moov) |
| 485 | 489 | } |
| 486 | 490 | |
| @@ -532,14 +536,14 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 532 | 536 | }; |
| 533 | 537 | out.extend_from_slice(&(width << 16).to_be_bytes()); |
| 534 | 538 | out.extend_from_slice(&(height << 16).to_be_bytes()); |
| 535 | - close(out, tkhd); | |
| 539 | + close(out, tkhd)?; | |
| 536 | 540 | |
| 537 | 541 | if let Some(id) = chapter_ref { |
| 538 | 542 | let tref = open(out, b"tref"); |
| 539 | 543 | let chap = open(out, b"chap"); |
| 540 | 544 | out.extend_from_slice(&id.to_be_bytes()); |
| 541 | - close(out, chap); | |
| 542 | - close(out, tref); | |
| 545 | + close(out, chap)?; | |
| 546 | + close(out, tref)?; | |
| 543 | 547 | } |
| 544 | 548 | |
| 545 | 549 | // An `elst`, where the first picture is not the first *decoded* one, or |
| @@ -558,8 +562,8 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 558 | 562 | out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); |
| 559 | 563 | out.extend_from_slice(&media_time.to_be_bytes()); |
| 560 | 564 | 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)?; | |
| 563 | 567 | } |
| 564 | 568 | let priming = presentation_start(track) |
| 565 | 569 | .max(track.audio_delay) |
| @@ -577,8 +581,8 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 577 | 581 | out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); |
| 578 | 582 | out.extend_from_slice(&(priming as u32).to_be_bytes()); |
| 579 | 583 | 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)?; | |
| 582 | 586 | } |
| 583 | 587 | |
| 584 | 588 | let mdia = open(out, b"mdia"); |
| @@ -590,7 +594,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 590 | 594 | out.extend_from_slice(&(track_ticks(track).max(0) as u32).to_be_bytes()); |
| 591 | 595 | out.extend_from_slice(&pack_language(track.info.language.as_deref()).to_be_bytes()); |
| 592 | 596 | out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined |
| 593 | - close(out, mdhd); | |
| 597 | + close(out, mdhd)?; | |
| 594 | 598 | |
| 595 | 599 | let (handler, handler_name) = match codec.media_type() { |
| 596 | 600 | ec_core::MediaType::Video => (b"vide", "VideoHandler"), |
| @@ -604,7 +608,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 604 | 608 | out.extend_from_slice(&[0; 12]); // reserved |
| 605 | 609 | out.extend_from_slice(handler_name.as_bytes()); |
| 606 | 610 | out.push(0); |
| 607 | - close(out, hdlr); | |
| 611 | + close(out, hdlr)?; | |
| 608 | 612 | |
| 609 | 613 | let minf = open(out, b"minf"); |
| 610 | 614 | match codec.media_type() { |
| @@ -612,18 +616,18 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 612 | 616 | let vmhd = open(out, b"vmhd"); |
| 613 | 617 | full_head(out, 0, 1); |
| 614 | 618 | out.extend_from_slice(&[0; 8]); // graphicsmode + opcolor |
| 615 | - close(out, vmhd); | |
| 619 | + close(out, vmhd)?; | |
| 616 | 620 | } |
| 617 | 621 | ec_core::MediaType::Audio => { |
| 618 | 622 | let smhd = open(out, b"smhd"); |
| 619 | 623 | full_head(out, 0, 0); |
| 620 | 624 | out.extend_from_slice(&[0; 4]); // balance + reserved |
| 621 | - close(out, smhd); | |
| 625 | + close(out, smhd)?; | |
| 622 | 626 | } |
| 623 | 627 | ec_core::MediaType::Subtitle => { |
| 624 | 628 | let nmhd = open(out, b"nmhd"); |
| 625 | 629 | full_head(out, 0, 0); |
| 626 | - close(out, nmhd); | |
| 630 | + close(out, nmhd)?; | |
| 627 | 631 | } |
| 628 | 632 | } |
| 629 | 633 | let dinf = open(out, b"dinf"); |
| @@ -632,27 +636,27 @@ impl<W: Write + Seek> Mp4Muxer<W> { | ||
| 632 | 636 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 633 | 637 | let url = open(out, b"url "); |
| 634 | 638 | 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)?; | |
| 638 | 642 | |
| 639 | 643 | let stbl = open(out, b"stbl"); |
| 640 | 644 | let stsd = open(out, b"stsd"); |
| 641 | 645 | full_head(out, 0, 0); |
| 642 | 646 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 643 | 647 | 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)?; | |
| 649 | 653 | |
| 650 | 654 | if let Some(title) = &track.title { |
| 651 | 655 | 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)?; | |
| 654 | 658 | } |
| 655 | - close(out, at); | |
| 659 | + close(out, at)?; | |
| 656 | 660 | Ok(()) |
| 657 | 661 | } |
| 658 | 662 | } |
| @@ -718,7 +722,8 @@ impl<W: Write + Seek + Send> Muxer for Mp4Muxer<W> { | ||
| 718 | 722 | size, |
| 719 | 723 | dts, |
| 720 | 724 | 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"))?, | |
| 722 | 727 | sync: packet.flags.keyframe, |
| 723 | 728 | }); |
| 724 | 729 | match track.chunk_end == pos { |
| @@ -812,7 +817,7 @@ impl<W: Read + Write + Seek + Send> Mp4Muxer<W> { | ||
| 812 | 817 | /// clock (milliseconds), with a `co64` offset so it stays right past 4 GB. It is |
| 813 | 818 | /// disabled, as a chapter track is in a QuickTime file, and its `edts` puts the |
| 814 | 819 | /// 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<()> { | |
| 816 | 821 | let at = open(out, b"trak"); |
| 817 | 822 | |
| 818 | 823 | let tkhd = open(out, b"tkhd"); |
| @@ -828,7 +833,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 828 | 833 | out.extend_from_slice(&0u16.to_be_bytes()); |
| 829 | 834 | out.extend_from_slice(&UNITY_MATRIX); |
| 830 | 835 | out.extend_from_slice(&[0; 8]); // width, height |
| 831 | - close(out, tkhd); | |
| 836 | + close(out, tkhd)?; | |
| 832 | 837 | |
| 833 | 838 | if chap.first_ms > 0 { |
| 834 | 839 | let edts = open(out, b"edts"); |
| @@ -844,8 +849,8 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 844 | 849 | out.extend_from_slice(&length.to_be_bytes()); |
| 845 | 850 | out.extend_from_slice(&0u32.to_be_bytes()); |
| 846 | 851 | 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)?; | |
| 849 | 854 | } |
| 850 | 855 | |
| 851 | 856 | let mdia = open(out, b"mdia"); |
| @@ -858,7 +863,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 858 | 863 | out.extend_from_slice(&(total as u32).to_be_bytes()); |
| 859 | 864 | out.extend_from_slice(&pack_language(None).to_be_bytes()); |
| 860 | 865 | out.extend_from_slice(&0u16.to_be_bytes()); |
| 861 | - close(out, mdhd); | |
| 866 | + close(out, mdhd)?; | |
| 862 | 867 | |
| 863 | 868 | let hdlr = open(out, b"hdlr"); |
| 864 | 869 | full_head(out, 0, 0); |
| @@ -867,21 +872,21 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 867 | 872 | out.extend_from_slice(&[0; 12]); |
| 868 | 873 | out.extend_from_slice(b"ChapterHandler"); |
| 869 | 874 | out.push(0); |
| 870 | - close(out, hdlr); | |
| 875 | + close(out, hdlr)?; | |
| 871 | 876 | |
| 872 | 877 | let minf = open(out, b"minf"); |
| 873 | 878 | let nmhd = open(out, b"nmhd"); |
| 874 | 879 | full_head(out, 0, 0); |
| 875 | - close(out, nmhd); | |
| 880 | + close(out, nmhd)?; | |
| 876 | 881 | let dinf = open(out, b"dinf"); |
| 877 | 882 | let dref = open(out, b"dref"); |
| 878 | 883 | full_head(out, 0, 0); |
| 879 | 884 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 880 | 885 | let url = open(out, b"url "); |
| 881 | 886 | 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)?; | |
| 885 | 890 | |
| 886 | 891 | let stbl = open(out, b"stbl"); |
| 887 | 892 | let stsd = open(out, b"stsd"); |
| @@ -898,8 +903,8 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 898 | 903 | out.extend_from_slice(&0xFFFFu16.to_be_bytes()); // foreground: white |
| 899 | 904 | } |
| 900 | 905 | out.push(0); // empty font name |
| 901 | - close(out, text); | |
| 902 | - close(out, stsd); | |
| 906 | + close(out, text)?; | |
| 907 | + close(out, stsd)?; | |
| 903 | 908 | |
| 904 | 909 | let stts = open(out, b"stts"); |
| 905 | 910 | full_head(out, 0, 0); |
| @@ -908,7 +913,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 908 | 913 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 909 | 914 | out.extend_from_slice(&duration.to_be_bytes()); |
| 910 | 915 | } |
| 911 | - close(out, stts); | |
| 916 | + close(out, stts)?; | |
| 912 | 917 | |
| 913 | 918 | let stsc = open(out, b"stsc"); |
| 914 | 919 | full_head(out, 0, 0); |
| @@ -916,7 +921,7 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 916 | 921 | out.extend_from_slice(&1u32.to_be_bytes()); // first chunk |
| 917 | 922 | out.extend_from_slice(&(chap.samples.len() as u32).to_be_bytes()); |
| 918 | 923 | out.extend_from_slice(&1u32.to_be_bytes()); // description index |
| 919 | - close(out, stsc); | |
| 924 | + close(out, stsc)?; | |
| 920 | 925 | |
| 921 | 926 | let stsz = open(out, b"stsz"); |
| 922 | 927 | full_head(out, 0, 0); |
| @@ -925,23 +930,24 @@ fn write_chapter_trak(out: &mut Vec<u8>, id: u32, chap: &ChapterText, shift: u64 | ||
| 925 | 930 | for (_, size) in &chap.samples { |
| 926 | 931 | out.extend_from_slice(&size.to_be_bytes()); |
| 927 | 932 | } |
| 928 | - close(out, stsz); | |
| 933 | + close(out, stsz)?; | |
| 929 | 934 | |
| 930 | 935 | let co64 = open(out, b"co64"); |
| 931 | 936 | full_head(out, 0, 0); |
| 932 | 937 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 933 | 938 | 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(()) | |
| 939 | 945 | } |
| 940 | 946 | |
| 941 | 947 | /// The Nero `chpl` box in `moov/udta`: version 1 (its 4 extra bytes are |
| 942 | 948 | /// reserved), start times in 100 ns units, and titles cut to 255 bytes. Written |
| 943 | 949 | /// 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<()> { | |
| 945 | 951 | let chpl = open(out, b"chpl"); |
| 946 | 952 | full_head(out, 1, 0); |
| 947 | 953 | out.extend_from_slice(&0u32.to_be_bytes()); |
| @@ -952,7 +958,8 @@ fn write_chpl(out: &mut Vec<u8>, chapters: &[(u64, String)]) { | ||
| 952 | 958 | out.push(text.len() as u8); |
| 953 | 959 | out.extend_from_slice(text.as_bytes()); |
| 954 | 960 | } |
| 955 | - close(out, chpl); | |
| 961 | + close(out, chpl)?; | |
| 962 | + Ok(()) | |
| 956 | 963 | } |
| 957 | 964 | |
| 958 | 965 | /// `title` cut to at most `max` bytes, on a character boundary. |
| @@ -1043,7 +1050,7 @@ fn opus_pre_skip(track: &Track) -> i64 { | ||
| 1043 | 1050 | /// The tables of a QuickTime PCM track the way QuickTime and ffmpeg lay them |
| 1044 | 1051 | /// out: a *sample* is one frame (`frame_bytes`, one `stts` tick), and a chunk |
| 1045 | 1052 | /// 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<()> { | |
| 1047 | 1054 | let frame_bytes = frame_bytes.max(1); |
| 1048 | 1055 | let total: u64 = track |
| 1049 | 1056 | .samples |
| @@ -1057,7 +1064,7 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) { | ||
| 1057 | 1064 | out.extend_from_slice(&(total as u32).to_be_bytes()); |
| 1058 | 1065 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 1059 | 1066 | } |
| 1060 | - close(out, stts); | |
| 1067 | + close(out, stts)?; | |
| 1061 | 1068 | |
| 1062 | 1069 | // Frames per chunk: the chunk's packets are the next `chunk.1` samples. |
| 1063 | 1070 | 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) { | ||
| 1090 | 1097 | } |
| 1091 | 1098 | } |
| 1092 | 1099 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); |
| 1093 | - close(out, stsc); | |
| 1100 | + close(out, stsc)?; | |
| 1094 | 1101 | |
| 1095 | 1102 | let stsz = open(out, b"stsz"); |
| 1096 | 1103 | full_head(out, 0, 0); |
| 1097 | 1104 | out.extend_from_slice(&frame_bytes.to_be_bytes()); |
| 1098 | 1105 | out.extend_from_slice(&(total as u32).to_be_bytes()); |
| 1099 | - close(out, stsz); | |
| 1106 | + close(out, stsz)?; | |
| 1100 | 1107 | |
| 1101 | 1108 | let big = track |
| 1102 | 1109 | .chunks |
| @@ -1112,11 +1119,12 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) { | ||
| 1112 | 1119 | false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()), |
| 1113 | 1120 | } |
| 1114 | 1121 | } |
| 1115 | - close(out, chunks); | |
| 1122 | + close(out, chunks)?; | |
| 1123 | + Ok(()) | |
| 1116 | 1124 | } |
| 1117 | 1125 | |
| 1118 | 1126 | /// 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<()> { | |
| 1120 | 1128 | if let (Some((bytes, _)), Some(audio)) = |
| 1121 | 1129 | (qt_pcm(track.info.params.codec), track.info.params.audio()) |
| 1122 | 1130 | { |
| @@ -1125,8 +1133,8 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | ||
| 1125 | 1133 | track, |
| 1126 | 1134 | bytes * audio.layout.channel_count() as u32, |
| 1127 | 1135 | shift, |
| 1128 | - ); | |
| 1129 | - return; | |
| 1136 | + )?; | |
| 1137 | + return Ok(()); | |
| 1130 | 1138 | } |
| 1131 | 1139 | // stts: runs of equal durations, which for constant-rate video is one entry |
| 1132 | 1140 | // for the whole film. |
| @@ -1148,7 +1156,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | ||
| 1148 | 1156 | runs += 1; |
| 1149 | 1157 | } |
| 1150 | 1158 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); |
| 1151 | - close(out, stts); | |
| 1159 | + close(out, stts)?; | |
| 1152 | 1160 | |
| 1153 | 1161 | // ctts, only where something is actually reordered. |
| 1154 | 1162 | 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) { | ||
| 1174 | 1182 | runs += 1; |
| 1175 | 1183 | } |
| 1176 | 1184 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); |
| 1177 | - close(out, ctts); | |
| 1185 | + close(out, ctts)?; | |
| 1178 | 1186 | } |
| 1179 | 1187 | |
| 1180 | 1188 | // 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) { | ||
| 1193 | 1201 | for n in syncs { |
| 1194 | 1202 | out.extend_from_slice(&n.to_be_bytes()); |
| 1195 | 1203 | } |
| 1196 | - close(out, stss); | |
| 1204 | + close(out, stss)?; | |
| 1197 | 1205 | } |
| 1198 | 1206 | |
| 1199 | 1207 | // 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) { | ||
| 1215 | 1223 | } |
| 1216 | 1224 | } |
| 1217 | 1225 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); |
| 1218 | - close(out, stsc); | |
| 1226 | + close(out, stsc)?; | |
| 1219 | 1227 | |
| 1220 | 1228 | // stsz: one size for all, where they really are all one size. |
| 1221 | 1229 | let stsz = open(out, b"stsz"); |
| @@ -1233,7 +1241,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | ||
| 1233 | 1241 | out.extend_from_slice(&sample.size.to_be_bytes()); |
| 1234 | 1242 | } |
| 1235 | 1243 | } |
| 1236 | - close(out, stsz); | |
| 1244 | + close(out, stsz)?; | |
| 1237 | 1245 | |
| 1238 | 1246 | // co64 where the file is big enough to need it, stco where it is not. |
| 1239 | 1247 | let big = track |
| @@ -1250,7 +1258,8 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | ||
| 1250 | 1258 | false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()), |
| 1251 | 1259 | } |
| 1252 | 1260 | } |
| 1253 | - close(out, chunks); | |
| 1261 | + close(out, chunks)?; | |
| 1262 | + Ok(()) | |
| 1254 | 1263 | } |
| 1255 | 1264 | |
| 1256 | 1265 | /// The sample entry name and configuration box of a video codec. |
| @@ -1323,22 +1332,22 @@ fn sample_entry( | ||
| 1323 | 1332 | out.extend_from_slice(&[0; 32]); // compressorname |
| 1324 | 1333 | out.extend_from_slice(&0x0018u16.to_be_bytes()); |
| 1325 | 1334 | out.extend_from_slice(&(-1i16).to_be_bytes()); |
| 1326 | - colour_boxes(out, video.color, video.light); | |
| 1335 | + colour_boxes(out, video.color, video.light)?; | |
| 1327 | 1336 | if let Some(order) = field_order { |
| 1328 | 1337 | let (count, detail) = match order { |
| 1329 | 1338 | ec_core::Interlace::Progressive => (1u8, 0u8), |
| 1330 | 1339 | ec_core::Interlace::TopFieldFirst => (2, 1), |
| 1331 | 1340 | ec_core::Interlace::BottomFieldFirst => (2, 6), |
| 1332 | 1341 | }; |
| 1333 | - leaf(out, b"fiel", &[count, detail]); | |
| 1342 | + leaf(out, b"fiel", &[count, detail])?; | |
| 1334 | 1343 | } |
| 1335 | 1344 | let sar = video.sample_aspect_ratio; |
| 1336 | 1345 | let (n, d) = sar.map_or((1, 1), |s| (s.num() as u32, s.den() as u32)); |
| 1337 | 1346 | let pasp = open(out, b"pasp"); |
| 1338 | 1347 | out.extend_from_slice(&n.to_be_bytes()); |
| 1339 | 1348 | out.extend_from_slice(&d.to_be_bytes()); |
| 1340 | - close(out, pasp); | |
| 1341 | - close(out, at); | |
| 1349 | + close(out, pasp)?; | |
| 1350 | + close(out, at)?; | |
| 1342 | 1351 | return Ok(()); |
| 1343 | 1352 | } |
| 1344 | 1353 | if let Some(name) = video_entry(codec) { |
| @@ -1364,13 +1373,13 @@ fn sample_entry( | ||
| 1364 | 1373 | out.extend_from_slice(&[0; 32]); // compressorname |
| 1365 | 1374 | out.extend_from_slice(&0x0018u16.to_be_bytes()); // depth: colour, no alpha |
| 1366 | 1375 | 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)?; | |
| 1369 | 1378 | if let Some(sar) = video.sample_aspect_ratio { |
| 1370 | 1379 | let pasp = open(out, b"pasp"); |
| 1371 | 1380 | out.extend_from_slice(&(sar.num() as u32).to_be_bytes()); |
| 1372 | 1381 | out.extend_from_slice(&(sar.den() as u32).to_be_bytes()); |
| 1373 | - close(out, pasp); | |
| 1382 | + close(out, pasp)?; | |
| 1374 | 1383 | } |
| 1375 | 1384 | // ffmpeg's order: after `colr`/`pasp`. field_count 1 / detail 0 is a |
| 1376 | 1385 | // progressive picture; 2 fields with detail 1 is top first (stored and |
| @@ -1382,9 +1391,9 @@ fn sample_entry( | ||
| 1382 | 1391 | ec_core::Interlace::TopFieldFirst => (2, 1), |
| 1383 | 1392 | ec_core::Interlace::BottomFieldFirst => (2, 6), |
| 1384 | 1393 | }; |
| 1385 | - leaf(out, b"fiel", &[count, detail]); | |
| 1394 | + leaf(out, b"fiel", &[count, detail])?; | |
| 1386 | 1395 | } |
| 1387 | - close(out, at); | |
| 1396 | + close(out, at)?; | |
| 1388 | 1397 | return Ok(()); |
| 1389 | 1398 | } |
| 1390 | 1399 | if codec == CodecId::Tx3g { |
| @@ -1411,10 +1420,10 @@ fn sample_entry( | ||
| 1411 | 1420 | out.extend_from_slice(&1u16.to_be_bytes()); |
| 1412 | 1421 | out.push(5); |
| 1413 | 1422 | out.extend_from_slice(b"Serif"); |
| 1414 | - close(out, ftab); | |
| 1423 | + close(out, ftab)?; | |
| 1415 | 1424 | } |
| 1416 | 1425 | } |
| 1417 | - close(out, at); | |
| 1426 | + close(out, at)?; | |
| 1418 | 1427 | return Ok(()); |
| 1419 | 1428 | } |
| 1420 | 1429 | |
| @@ -1477,10 +1486,10 @@ fn sample_entry( | ||
| 1477 | 1486 | out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame |
| 1478 | 1487 | out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) |
| 1479 | 1488 | 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())?; | |
| 1482 | 1491 | 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)?; | |
| 1484 | 1493 | } |
| 1485 | 1494 | None => {} |
| 1486 | 1495 | } |
| @@ -1494,7 +1503,7 @@ fn sample_entry( | ||
| 1494 | 1503 | })?; |
| 1495 | 1504 | let esds = open(out, b"esds"); |
| 1496 | 1505 | out.extend_from_slice(&Esds::aac(asc).write()); |
| 1497 | - close(out, esds); | |
| 1506 | + close(out, esds)?; | |
| 1498 | 1507 | } |
| 1499 | 1508 | CodecId::Mp3 => { |
| 1500 | 1509 | let mut mp3 = Esds::aac(&[][..]); |
| @@ -1502,7 +1511,7 @@ fn sample_entry( | ||
| 1502 | 1511 | mp3.decoder_specific = None; |
| 1503 | 1512 | let esds = open(out, b"esds"); |
| 1504 | 1513 | out.extend_from_slice(&mp3.write()); |
| 1505 | - close(out, esds); | |
| 1514 | + close(out, esds)?; | |
| 1506 | 1515 | } |
| 1507 | 1516 | CodecId::Ac3 |
| 1508 | 1517 | | CodecId::EAc3 |
| @@ -1522,23 +1531,23 @@ fn sample_entry( | ||
| 1522 | 1531 | CodecId::Alac if config.len() > 8 && &config[4..8] == b"alac" => { |
| 1523 | 1532 | out.extend_from_slice(config) |
| 1524 | 1533 | } |
| 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)?, | |
| 1528 | 1537 | } |
| 1529 | 1538 | } |
| 1530 | 1539 | _ => {} |
| 1531 | 1540 | } |
| 1532 | 1541 | if chan || qt.is_some() { |
| 1533 | - chan_box(out, &audio.layout); | |
| 1542 | + chan_box(out, &audio.layout)?; | |
| 1534 | 1543 | } |
| 1535 | - close(out, at); | |
| 1544 | + close(out, at)?; | |
| 1536 | 1545 | Ok(()) |
| 1537 | 1546 | } |
| 1538 | 1547 | |
| 1539 | 1548 | /// `colr`, and the two HDR boxes where the grade stated them — metadata a remux |
| 1540 | 1549 | /// 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<()> { | |
| 1542 | 1551 | if color != ec_core::ColorInfo::default() { |
| 1543 | 1552 | let colr = open(out, b"colr"); |
| 1544 | 1553 | out.extend_from_slice(b"nclx"); |
| @@ -1546,7 +1555,7 @@ fn colour_boxes(out: &mut Vec<u8>, color: ec_core::ColorInfo, light: ContentLigh | ||
| 1546 | 1555 | out.extend_from_slice(&u16::from(color.transfer).to_be_bytes()); |
| 1547 | 1556 | out.extend_from_slice(&u16::from(color.matrix).to_be_bytes()); |
| 1548 | 1557 | out.push(u8::from(color.full_range) << 7); |
| 1549 | - close(out, colr); | |
| 1558 | + close(out, colr)?; | |
| 1550 | 1559 | } |
| 1551 | 1560 | if light.mastering_max.is_some() |
| 1552 | 1561 | || light.mastering_min.is_some() |
| @@ -1566,14 +1575,15 @@ fn colour_boxes(out: &mut Vec<u8>, color: ec_core::ColorInfo, light: ContentLigh | ||
| 1566 | 1575 | }; |
| 1567 | 1576 | out.extend_from_slice(&nits(light.mastering_max)); |
| 1568 | 1577 | out.extend_from_slice(&nits(light.mastering_min)); |
| 1569 | - close(out, mdcv); | |
| 1578 | + close(out, mdcv)?; | |
| 1570 | 1579 | } |
| 1571 | 1580 | if light.max_cll.is_some() || light.max_fall.is_some() { |
| 1572 | 1581 | let clli = open(out, b"clli"); |
| 1573 | 1582 | out.extend_from_slice(&(light.max_cll.unwrap_or(0.0).round() as u16).to_be_bytes()); |
| 1574 | 1583 | 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)?; | |
| 1576 | 1585 | } |
| 1586 | + Ok(()) | |
| 1577 | 1587 | } |
| 1578 | 1588 | |
| 1579 | 1589 | /// 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)> { | ||
| 1603 | 1613 | |
| 1604 | 1614 | /// The QuickTime `chan` box for the layouts that have a stock one, as ffmpeg |
| 1605 | 1615 | /// 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<()> { | |
| 1607 | 1617 | use ec_core::ChannelLayout; |
| 1608 | 1618 | let (tag, bitmap) = match layout { |
| 1609 | 1619 | ChannelLayout::Mono => (0x0064_0001u32, 0u32), |
| 1610 | 1620 | ChannelLayout::Stereo => (0x0065_0002, 0), |
| 1611 | 1621 | // UseChannelBitmap; L R C LFE Ls Rs. |
| 1612 | 1622 | ChannelLayout::Surround5_1 => (0x0001_0000, 0x3f), |
| 1613 | - _ => return, | |
| 1623 | + _ => return Ok(()), | |
| 1614 | 1624 | }; |
| 1615 | 1625 | let at = open(out, b"chan"); |
| 1616 | 1626 | full_head(out, 0, 0); |
| 1617 | 1627 | out.extend_from_slice(&tag.to_be_bytes()); |
| 1618 | 1628 | out.extend_from_slice(&bitmap.to_be_bytes()); |
| 1619 | 1629 | out.extend_from_slice(&0u32.to_be_bytes()); // number of descriptions |
| 1620 | - close(out, at); | |
| 1630 | + close(out, at)?; | |
| 1631 | + Ok(()) | |
| 1621 | 1632 | } |
| 1622 | 1633 | |
| 1623 | 1634 | fn audio_entry(codec: CodecId) -> Result<&'static [u8; 4]> { |