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 | /// Finish the box [`open`] started at `at` by writing its size. | 230 | /// Finish the box [`open`] started at `at` by writing its size. |
| 231 | -pub fn close(out: &mut [u8], at: usize) { | 231 | +pub fn close(out: &mut [u8], at: usize) -> Result<()> { |
| 232 | - let size = (out.len() - at) as u32; | 232 | + let size = u32::try_from(out.len() - at).map_err(|_| Error::corrupt("mp4: a box over 4 GB"))?; |
| 233 | out[at..at + 4].copy_from_slice(&size.to_be_bytes()); | 233 | out[at..at + 4].copy_from_slice(&size.to_be_bytes()); |
| 234 | + Ok(()) | ||
| 234 | } | 235 | } |
| 235 | 236 | ||
| 236 | /// A whole box in one call, for the leaves. | 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 | let at = open(out, kind); | 239 | let at = open(out, kind); |
| 239 | out.extend_from_slice(payload); | 240 | out.extend_from_slice(payload); |
| 240 | - close(out, at); | 241 | + close(out, at) |
| 241 | } | 242 | } |
| 242 | 243 | ||
| 243 | /// The `version`/`flags` word a FullBox starts with. | 244 | /// The `version`/`flags` word a FullBox starts with. |
| @@ -259,7 +260,7 @@ mod tests { | |||
| 259 | #[test] | 260 | #[test] |
| 260 | fn walks_32_and_64_bit_sizes() { | 261 | fn walks_32_and_64_bit_sizes() { |
| 261 | let mut out = Vec::new(); | 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 | // The same box written in the 64-bit form a big mdat uses. | 264 | // The same box written in the 64-bit form a big mdat uses. |
| 264 | out.extend_from_slice(&1u32.to_be_bytes()); | 265 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 265 | out.extend_from_slice(b"mdat"); | 266 | out.extend_from_slice(b"mdat"); |
| @@ -274,7 +275,7 @@ mod tests { | |||
| 274 | #[test] | 275 | #[test] |
| 275 | fn a_size_that_does_not_fit_is_corrupt_not_a_panic() { | 276 | fn a_size_that_does_not_fit_is_corrupt_not_a_panic() { |
| 276 | let mut out = Vec::new(); | 277 | let mut out = Vec::new(); |
| 277 | - leaf(&mut out, b"moov", &[0; 4]); | 278 | + leaf(&mut out, b"moov", &[0; 4]).unwrap(); |
| 278 | out[3] = 0xFF; // 255 bytes of moov inside 12 | 279 | out[3] = 0xFF; // 255 bytes of moov inside 12 |
| 279 | assert!(types(&out).is_err()); | 280 | assert!(types(&out).is_err()); |
| 280 | 281 | ||
crates/ec-mp4/src/demux.rsmodified+1 -1
| @@ -1950,7 +1950,7 @@ mod tests { | |||
| 1950 | assert_eq!(unpack_language(0), None); | 1950 | assert_eq!(unpack_language(0), None); |
| 1951 | 1951 | ||
| 1952 | let mut udta = Vec::new(); | 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 | assert_eq!(udta_name(&udta).unwrap().as_deref(), Some("Turkish")); | 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 | head.extend_from_slice(b"mp41"); | 317 | head.extend_from_slice(b"mp41"); |
| 318 | } | 318 | } |
| 319 | } | 319 | } |
| 320 | - close(&mut head, at); | 320 | + close(&mut head, at)?; |
| 321 | self.mdat_at = head.len() as u64; | 321 | self.mdat_at = head.len() as u64; |
| 322 | // The 64-bit form of an `mdat` header, always: a two-hour 4K export goes | 322 | // The 64-bit form of an `mdat` header, always: a two-hour 4K export goes |
| 323 | // past 4 GB and the placeholder cannot grow later without moving every | 323 | // past 4 GB and the placeholder cannot grow later without moving every |
| @@ -333,7 +333,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 333 | /// Sample durations, resolved: a packet that stated one keeps it, and one | 333 | /// Sample durations, resolved: a packet that stated one keeps it, and one |
| 334 | /// that did not gets the step to the next sample — which is what the last | 334 | /// that did not gets the step to the next sample — which is what the last |
| 335 | /// sample has no next for, so it repeats the step before it. | 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 | for i in 0..track.samples.len() { | 337 | for i in 0..track.samples.len() { |
| 338 | if track.samples[i].duration != 0 { | 338 | if track.samples[i].duration != 0 { |
| 339 | continue; | 339 | continue; |
| @@ -344,8 +344,10 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 344 | .checked_sub(1) | 344 | .checked_sub(1) |
| 345 | .map_or(0, |prev| i64::from(track.samples[prev].duration)), | 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 | /// The film's length in movie milliseconds: the longest track's presentation. | 353 | /// The film's length in movie milliseconds: the longest track's presentation. |
| @@ -392,13 +394,15 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 392 | let mut samples = Vec::with_capacity(self.chapters.len()); | 394 | let mut samples = Vec::with_capacity(self.chapters.len()); |
| 393 | for (i, (_, title)) in self.chapters.iter().enumerate() { | 395 | for (i, (_, title)) in self.chapters.iter().enumerate() { |
| 394 | let next = starts.get(i + 1).copied().unwrap_or(end_ms); | 396 | let next = starts.get(i + 1).copied().unwrap_or(end_ms); |
| 395 | - let mut duration = (next - starts[i]).max(0) as u32; | 397 | + // Readers that end the last chapter at the text track's media duration |
| 396 | - if i + 1 == starts.len() { | 398 | + // (ffmpeg) do not count the leading empty edit, so the media is padded by |
| 397 | - // Readers that end the last chapter at the text track's media duration | 399 | + // the first chapter's start; the edit list below still plays only to the end. |
| 398 | - // (ffmpeg) do not count the leading empty edit, so the media is padded by | 400 | + let padding = match i + 1 == starts.len() { |
| 399 | - // the first chapter's start; the edit list below still plays only to the end. | 401 | + true => first_ms.max(0), |
| 400 | - duration += first_ms.max(0) as u32; | 402 | + false => 0, |
| 401 | - } | 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 | let text = clip(title, usize::from(u16::MAX)); | 406 | let text = clip(title, usize::from(u16::MAX)); |
| 403 | payload.extend_from_slice(&(text.len() as u16).to_be_bytes()); | 407 | payload.extend_from_slice(&(text.len() as u16).to_be_bytes()); |
| 404 | payload.extend_from_slice(text.as_bytes()); | 408 | payload.extend_from_slice(text.as_bytes()); |
| @@ -419,7 +423,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 419 | fn seal_mdat(&mut self) -> Result<Option<ChapterText>> { | 423 | fn seal_mdat(&mut self) -> Result<Option<ChapterText>> { |
| 420 | self.start()?; | 424 | self.start()?; |
| 421 | for track in &mut self.tracks { | 425 | for track in &mut self.tracks { |
| 422 | - Mp4Muxer::<W>::resolve_durations(track); | 426 | + Mp4Muxer::<W>::resolve_durations(track)?; |
| 423 | } | 427 | } |
| 424 | // The chapter text samples are media, so they go in the mdat before its | 428 | // The chapter text samples are media, so they go in the mdat before its |
| 425 | // size is known. | 429 | // size is known. |
| @@ -457,7 +461,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 457 | moov.extend_from_slice(&[0; 24]); // pre_defined | 461 | moov.extend_from_slice(&[0; 24]); // pre_defined |
| 458 | let next_id = chapter_id.unwrap_or(self.tracks.len() as u32) + 1; | 462 | let next_id = chapter_id.unwrap_or(self.tracks.len() as u32) + 1; |
| 459 | moov.extend_from_slice(&next_id.to_be_bytes()); | 463 | moov.extend_from_slice(&next_id.to_be_bytes()); |
| 460 | - close(&mut moov, mvhd); | 464 | + close(&mut moov, mvhd)?; |
| 461 | 465 | ||
| 462 | for i in 0..self.tracks.len() { | 466 | for i in 0..self.tracks.len() { |
| 463 | let chapter_ref = match reference == Some(i) { | 467 | let chapter_ref = match reference == Some(i) { |
| @@ -467,20 +471,20 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 467 | self.write_trak(&mut moov, i, duration, chapter_ref, shift)?; | 471 | self.write_trak(&mut moov, i, duration, chapter_ref, shift)?; |
| 468 | } | 472 | } |
| 469 | if let (Some(text), Some(id)) = (chapters, chapter_id) { | 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 | let chpl = (1..=255).contains(&self.chapters.len()); | 476 | let chpl = (1..=255).contains(&self.chapters.len()); |
| 473 | if self.title.is_some() || chpl { | 477 | if self.title.is_some() || chpl { |
| 474 | let udta = open(&mut moov, b"udta"); | 478 | let udta = open(&mut moov, b"udta"); |
| 475 | if let Some(title) = &self.title { | 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 | if chpl { | 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 | Ok(moov) | 488 | Ok(moov) |
| 485 | } | 489 | } |
| 486 | 490 | ||
| @@ -532,14 +536,14 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 532 | }; | 536 | }; |
| 533 | out.extend_from_slice(&(width << 16).to_be_bytes()); | 537 | out.extend_from_slice(&(width << 16).to_be_bytes()); |
| 534 | out.extend_from_slice(&(height << 16).to_be_bytes()); | 538 | out.extend_from_slice(&(height << 16).to_be_bytes()); |
| 535 | - close(out, tkhd); | 539 | + close(out, tkhd)?; |
| 536 | 540 | ||
| 537 | if let Some(id) = chapter_ref { | 541 | if let Some(id) = chapter_ref { |
| 538 | let tref = open(out, b"tref"); | 542 | let tref = open(out, b"tref"); |
| 539 | let chap = open(out, b"chap"); | 543 | let chap = open(out, b"chap"); |
| 540 | out.extend_from_slice(&id.to_be_bytes()); | 544 | out.extend_from_slice(&id.to_be_bytes()); |
| 541 | - close(out, chap); | 545 | + close(out, chap)?; |
| 542 | - close(out, tref); | 546 | + close(out, tref)?; |
| 543 | } | 547 | } |
| 544 | 548 | ||
| 545 | // An `elst`, where the first picture is not the first *decoded* one, or | 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 | out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); | 562 | out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); |
| 559 | out.extend_from_slice(&media_time.to_be_bytes()); | 563 | out.extend_from_slice(&media_time.to_be_bytes()); |
| 560 | out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0 | 564 | out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0 |
| 561 | - close(out, elst); | 565 | + close(out, elst)?; |
| 562 | - close(out, edts); | 566 | + close(out, edts)?; |
| 563 | } | 567 | } |
| 564 | let priming = presentation_start(track) | 568 | let priming = presentation_start(track) |
| 565 | .max(track.audio_delay) | 569 | .max(track.audio_delay) |
| @@ -577,8 +581,8 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 577 | out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); | 581 | out.extend_from_slice(&(segment.max(0) as u32).to_be_bytes()); |
| 578 | out.extend_from_slice(&(priming as u32).to_be_bytes()); | 582 | out.extend_from_slice(&(priming as u32).to_be_bytes()); |
| 579 | out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0 | 583 | out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // media rate 1.0 |
| 580 | - close(out, elst); | 584 | + close(out, elst)?; |
| 581 | - close(out, edts); | 585 | + close(out, edts)?; |
| 582 | } | 586 | } |
| 583 | 587 | ||
| 584 | let mdia = open(out, b"mdia"); | 588 | let mdia = open(out, b"mdia"); |
| @@ -590,7 +594,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 590 | out.extend_from_slice(&(track_ticks(track).max(0) as u32).to_be_bytes()); | 594 | out.extend_from_slice(&(track_ticks(track).max(0) as u32).to_be_bytes()); |
| 591 | out.extend_from_slice(&pack_language(track.info.language.as_deref()).to_be_bytes()); | 595 | out.extend_from_slice(&pack_language(track.info.language.as_deref()).to_be_bytes()); |
| 592 | out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined | 596 | out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined |
| 593 | - close(out, mdhd); | 597 | + close(out, mdhd)?; |
| 594 | 598 | ||
| 595 | let (handler, handler_name) = match codec.media_type() { | 599 | let (handler, handler_name) = match codec.media_type() { |
| 596 | ec_core::MediaType::Video => (b"vide", "VideoHandler"), | 600 | ec_core::MediaType::Video => (b"vide", "VideoHandler"), |
| @@ -604,7 +608,7 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 604 | out.extend_from_slice(&[0; 12]); // reserved | 608 | out.extend_from_slice(&[0; 12]); // reserved |
| 605 | out.extend_from_slice(handler_name.as_bytes()); | 609 | out.extend_from_slice(handler_name.as_bytes()); |
| 606 | out.push(0); | 610 | out.push(0); |
| 607 | - close(out, hdlr); | 611 | + close(out, hdlr)?; |
| 608 | 612 | ||
| 609 | let minf = open(out, b"minf"); | 613 | let minf = open(out, b"minf"); |
| 610 | match codec.media_type() { | 614 | match codec.media_type() { |
| @@ -612,18 +616,18 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 612 | let vmhd = open(out, b"vmhd"); | 616 | let vmhd = open(out, b"vmhd"); |
| 613 | full_head(out, 0, 1); | 617 | full_head(out, 0, 1); |
| 614 | out.extend_from_slice(&[0; 8]); // graphicsmode + opcolor | 618 | out.extend_from_slice(&[0; 8]); // graphicsmode + opcolor |
| 615 | - close(out, vmhd); | 619 | + close(out, vmhd)?; |
| 616 | } | 620 | } |
| 617 | ec_core::MediaType::Audio => { | 621 | ec_core::MediaType::Audio => { |
| 618 | let smhd = open(out, b"smhd"); | 622 | let smhd = open(out, b"smhd"); |
| 619 | full_head(out, 0, 0); | 623 | full_head(out, 0, 0); |
| 620 | out.extend_from_slice(&[0; 4]); // balance + reserved | 624 | out.extend_from_slice(&[0; 4]); // balance + reserved |
| 621 | - close(out, smhd); | 625 | + close(out, smhd)?; |
| 622 | } | 626 | } |
| 623 | ec_core::MediaType::Subtitle => { | 627 | ec_core::MediaType::Subtitle => { |
| 624 | let nmhd = open(out, b"nmhd"); | 628 | let nmhd = open(out, b"nmhd"); |
| 625 | full_head(out, 0, 0); | 629 | full_head(out, 0, 0); |
| 626 | - close(out, nmhd); | 630 | + close(out, nmhd)?; |
| 627 | } | 631 | } |
| 628 | } | 632 | } |
| 629 | let dinf = open(out, b"dinf"); | 633 | let dinf = open(out, b"dinf"); |
| @@ -632,27 +636,27 @@ impl<W: Write + Seek> Mp4Muxer<W> { | |||
| 632 | out.extend_from_slice(&1u32.to_be_bytes()); | 636 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 633 | let url = open(out, b"url "); | 637 | let url = open(out, b"url "); |
| 634 | full_head(out, 0, 1); // flag 1: the media is in this very file | 638 | full_head(out, 0, 1); // flag 1: the media is in this very file |
| 635 | - close(out, url); | 639 | + close(out, url)?; |
| 636 | - close(out, dref); | 640 | + close(out, dref)?; |
| 637 | - close(out, dinf); | 641 | + close(out, dinf)?; |
| 638 | 642 | ||
| 639 | let stbl = open(out, b"stbl"); | 643 | let stbl = open(out, b"stbl"); |
| 640 | let stsd = open(out, b"stsd"); | 644 | let stsd = open(out, b"stsd"); |
| 641 | full_head(out, 0, 0); | 645 | full_head(out, 0, 0); |
| 642 | out.extend_from_slice(&1u32.to_be_bytes()); | 646 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 643 | sample_entry(out, &track.info, track.field_order, track.chan)?; | 647 | sample_entry(out, &track.info, track.field_order, track.chan)?; |
| 644 | - close(out, stsd); | 648 | + close(out, stsd)?; |
| 645 | - sample_tables(out, track, shift); | 649 | + sample_tables(out, track, shift)?; |
| 646 | - close(out, stbl); | 650 | + close(out, stbl)?; |
| 647 | - close(out, minf); | 651 | + close(out, minf)?; |
| 648 | - close(out, mdia); | 652 | + close(out, mdia)?; |
| 649 | 653 | ||
| 650 | if let Some(title) = &track.title { | 654 | if let Some(title) = &track.title { |
| 651 | let udta = open(out, b"udta"); | 655 | let udta = open(out, b"udta"); |
| 652 | - leaf(out, b"name", title.as_bytes()); | 656 | + leaf(out, b"name", title.as_bytes())?; |
| 653 | - close(out, udta); | 657 | + close(out, udta)?; |
| 654 | } | 658 | } |
| 655 | - close(out, at); | 659 | + close(out, at)?; |
| 656 | Ok(()) | 660 | Ok(()) |
| 657 | } | 661 | } |
| 658 | } | 662 | } |
| @@ -718,7 +722,8 @@ impl<W: Write + Seek + Send> Muxer for Mp4Muxer<W> { | |||
| 718 | size, | 722 | size, |
| 719 | dts, | 723 | dts, |
| 720 | cts, | 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 | sync: packet.flags.keyframe, | 727 | sync: packet.flags.keyframe, |
| 723 | }); | 728 | }); |
| 724 | match track.chunk_end == pos { | 729 | match track.chunk_end == pos { |
| @@ -812,7 +817,7 @@ impl<W: Read + Write + Seek + Send> Mp4Muxer<W> { | |||
| 812 | /// clock (milliseconds), with a `co64` offset so it stays right past 4 GB. It is | 817 | /// clock (milliseconds), with a `co64` offset so it stays right past 4 GB. It is |
| 813 | /// disabled, as a chapter track is in a QuickTime file, and its `edts` puts the | 818 | /// disabled, as a chapter track is in a QuickTime file, and its `edts` puts the |
| 814 | /// first chapter at its own start when that is not zero. | 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 | let at = open(out, b"trak"); | 821 | let at = open(out, b"trak"); |
| 817 | 822 | ||
| 818 | let tkhd = open(out, b"tkhd"); | 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 | out.extend_from_slice(&0u16.to_be_bytes()); | 833 | out.extend_from_slice(&0u16.to_be_bytes()); |
| 829 | out.extend_from_slice(&UNITY_MATRIX); | 834 | out.extend_from_slice(&UNITY_MATRIX); |
| 830 | out.extend_from_slice(&[0; 8]); // width, height | 835 | out.extend_from_slice(&[0; 8]); // width, height |
| 831 | - close(out, tkhd); | 836 | + close(out, tkhd)?; |
| 832 | 837 | ||
| 833 | if chap.first_ms > 0 { | 838 | if chap.first_ms > 0 { |
| 834 | let edts = open(out, b"edts"); | 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 | out.extend_from_slice(&length.to_be_bytes()); | 849 | out.extend_from_slice(&length.to_be_bytes()); |
| 845 | out.extend_from_slice(&0u32.to_be_bytes()); | 850 | out.extend_from_slice(&0u32.to_be_bytes()); |
| 846 | out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); | 851 | out.extend_from_slice(&0x0001_0000u32.to_be_bytes()); |
| 847 | - close(out, elst); | 852 | + close(out, elst)?; |
| 848 | - close(out, edts); | 853 | + close(out, edts)?; |
| 849 | } | 854 | } |
| 850 | 855 | ||
| 851 | let mdia = open(out, b"mdia"); | 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 | out.extend_from_slice(&(total as u32).to_be_bytes()); | 863 | out.extend_from_slice(&(total as u32).to_be_bytes()); |
| 859 | out.extend_from_slice(&pack_language(None).to_be_bytes()); | 864 | out.extend_from_slice(&pack_language(None).to_be_bytes()); |
| 860 | out.extend_from_slice(&0u16.to_be_bytes()); | 865 | out.extend_from_slice(&0u16.to_be_bytes()); |
| 861 | - close(out, mdhd); | 866 | + close(out, mdhd)?; |
| 862 | 867 | ||
| 863 | let hdlr = open(out, b"hdlr"); | 868 | let hdlr = open(out, b"hdlr"); |
| 864 | full_head(out, 0, 0); | 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 | out.extend_from_slice(&[0; 12]); | 872 | out.extend_from_slice(&[0; 12]); |
| 868 | out.extend_from_slice(b"ChapterHandler"); | 873 | out.extend_from_slice(b"ChapterHandler"); |
| 869 | out.push(0); | 874 | out.push(0); |
| 870 | - close(out, hdlr); | 875 | + close(out, hdlr)?; |
| 871 | 876 | ||
| 872 | let minf = open(out, b"minf"); | 877 | let minf = open(out, b"minf"); |
| 873 | let nmhd = open(out, b"nmhd"); | 878 | let nmhd = open(out, b"nmhd"); |
| 874 | full_head(out, 0, 0); | 879 | full_head(out, 0, 0); |
| 875 | - close(out, nmhd); | 880 | + close(out, nmhd)?; |
| 876 | let dinf = open(out, b"dinf"); | 881 | let dinf = open(out, b"dinf"); |
| 877 | let dref = open(out, b"dref"); | 882 | let dref = open(out, b"dref"); |
| 878 | full_head(out, 0, 0); | 883 | full_head(out, 0, 0); |
| 879 | out.extend_from_slice(&1u32.to_be_bytes()); | 884 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 880 | let url = open(out, b"url "); | 885 | let url = open(out, b"url "); |
| 881 | full_head(out, 0, 1); | 886 | full_head(out, 0, 1); |
| 882 | - close(out, url); | 887 | + close(out, url)?; |
| 883 | - close(out, dref); | 888 | + close(out, dref)?; |
| 884 | - close(out, dinf); | 889 | + close(out, dinf)?; |
| 885 | 890 | ||
| 886 | let stbl = open(out, b"stbl"); | 891 | let stbl = open(out, b"stbl"); |
| 887 | let stsd = open(out, b"stsd"); | 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 | out.extend_from_slice(&0xFFFFu16.to_be_bytes()); // foreground: white | 903 | out.extend_from_slice(&0xFFFFu16.to_be_bytes()); // foreground: white |
| 899 | } | 904 | } |
| 900 | out.push(0); // empty font name | 905 | out.push(0); // empty font name |
| 901 | - close(out, text); | 906 | + close(out, text)?; |
| 902 | - close(out, stsd); | 907 | + close(out, stsd)?; |
| 903 | 908 | ||
| 904 | let stts = open(out, b"stts"); | 909 | let stts = open(out, b"stts"); |
| 905 | full_head(out, 0, 0); | 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 | out.extend_from_slice(&1u32.to_be_bytes()); | 913 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 909 | out.extend_from_slice(&duration.to_be_bytes()); | 914 | out.extend_from_slice(&duration.to_be_bytes()); |
| 910 | } | 915 | } |
| 911 | - close(out, stts); | 916 | + close(out, stts)?; |
| 912 | 917 | ||
| 913 | let stsc = open(out, b"stsc"); | 918 | let stsc = open(out, b"stsc"); |
| 914 | full_head(out, 0, 0); | 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 | out.extend_from_slice(&1u32.to_be_bytes()); // first chunk | 921 | out.extend_from_slice(&1u32.to_be_bytes()); // first chunk |
| 917 | out.extend_from_slice(&(chap.samples.len() as u32).to_be_bytes()); | 922 | out.extend_from_slice(&(chap.samples.len() as u32).to_be_bytes()); |
| 918 | out.extend_from_slice(&1u32.to_be_bytes()); // description index | 923 | out.extend_from_slice(&1u32.to_be_bytes()); // description index |
| 919 | - close(out, stsc); | 924 | + close(out, stsc)?; |
| 920 | 925 | ||
| 921 | let stsz = open(out, b"stsz"); | 926 | let stsz = open(out, b"stsz"); |
| 922 | full_head(out, 0, 0); | 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 | for (_, size) in &chap.samples { | 930 | for (_, size) in &chap.samples { |
| 926 | out.extend_from_slice(&size.to_be_bytes()); | 931 | out.extend_from_slice(&size.to_be_bytes()); |
| 927 | } | 932 | } |
| 928 | - close(out, stsz); | 933 | + close(out, stsz)?; |
| 929 | 934 | ||
| 930 | let co64 = open(out, b"co64"); | 935 | let co64 = open(out, b"co64"); |
| 931 | full_head(out, 0, 0); | 936 | full_head(out, 0, 0); |
| 932 | out.extend_from_slice(&1u32.to_be_bytes()); | 937 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 933 | out.extend_from_slice(&(chap.offset + shift).to_be_bytes()); | 938 | out.extend_from_slice(&(chap.offset + shift).to_be_bytes()); |
| 934 | - close(out, co64); | 939 | + close(out, co64)?; |
| 935 | - close(out, stbl); | 940 | + close(out, stbl)?; |
| 936 | - close(out, minf); | 941 | + close(out, minf)?; |
| 937 | - close(out, mdia); | 942 | + close(out, mdia)?; |
| 938 | - close(out, at); | 943 | + close(out, at)?; |
| 944 | + Ok(()) | ||
| 939 | } | 945 | } |
| 940 | 946 | ||
| 941 | /// The Nero `chpl` box in `moov/udta`: version 1 (its 4 extra bytes are | 947 | /// The Nero `chpl` box in `moov/udta`: version 1 (its 4 extra bytes are |
| 942 | /// reserved), start times in 100 ns units, and titles cut to 255 bytes. Written | 948 | /// reserved), start times in 100 ns units, and titles cut to 255 bytes. Written |
| 943 | /// only for 1 to 255 chapters: the count is one byte. | 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 | let chpl = open(out, b"chpl"); | 951 | let chpl = open(out, b"chpl"); |
| 946 | full_head(out, 1, 0); | 952 | full_head(out, 1, 0); |
| 947 | out.extend_from_slice(&0u32.to_be_bytes()); | 953 | out.extend_from_slice(&0u32.to_be_bytes()); |
| @@ -952,7 +958,8 @@ fn write_chpl(out: &mut Vec<u8>, chapters: &[(u64, String)]) { | |||
| 952 | out.push(text.len() as u8); | 958 | out.push(text.len() as u8); |
| 953 | out.extend_from_slice(text.as_bytes()); | 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 | /// `title` cut to at most `max` bytes, on a character boundary. | 965 | /// `title` cut to at most `max` bytes, on a character boundary. |
| @@ -1043,7 +1050,7 @@ fn opus_pre_skip(track: &Track) -> i64 { | |||
| 1043 | /// The tables of a QuickTime PCM track the way QuickTime and ffmpeg lay them | 1050 | /// The tables of a QuickTime PCM track the way QuickTime and ffmpeg lay them |
| 1044 | /// out: a *sample* is one frame (`frame_bytes`, one `stts` tick), and a chunk | 1051 | /// out: a *sample* is one frame (`frame_bytes`, one `stts` tick), and a chunk |
| 1045 | /// holds as many samples as the packets written into it had frames. | 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 | let frame_bytes = frame_bytes.max(1); | 1054 | let frame_bytes = frame_bytes.max(1); |
| 1048 | let total: u64 = track | 1055 | let total: u64 = track |
| 1049 | .samples | 1056 | .samples |
| @@ -1057,7 +1064,7 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) { | |||
| 1057 | out.extend_from_slice(&(total as u32).to_be_bytes()); | 1064 | out.extend_from_slice(&(total as u32).to_be_bytes()); |
| 1058 | out.extend_from_slice(&1u32.to_be_bytes()); | 1065 | out.extend_from_slice(&1u32.to_be_bytes()); |
| 1059 | } | 1066 | } |
| 1060 | - close(out, stts); | 1067 | + close(out, stts)?; |
| 1061 | 1068 | ||
| 1062 | // Frames per chunk: the chunk's packets are the next `chunk.1` samples. | 1069 | // Frames per chunk: the chunk's packets are the next `chunk.1` samples. |
| 1063 | let mut per_chunk = Vec::with_capacity(track.chunks.len()); | 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 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); | 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 | let stsz = open(out, b"stsz"); | 1102 | let stsz = open(out, b"stsz"); |
| 1096 | full_head(out, 0, 0); | 1103 | full_head(out, 0, 0); |
| 1097 | out.extend_from_slice(&frame_bytes.to_be_bytes()); | 1104 | out.extend_from_slice(&frame_bytes.to_be_bytes()); |
| 1098 | out.extend_from_slice(&(total as u32).to_be_bytes()); | 1105 | out.extend_from_slice(&(total as u32).to_be_bytes()); |
| 1099 | - close(out, stsz); | 1106 | + close(out, stsz)?; |
| 1100 | 1107 | ||
| 1101 | let big = track | 1108 | let big = track |
| 1102 | .chunks | 1109 | .chunks |
| @@ -1112,11 +1119,12 @@ fn pcm_tables(out: &mut Vec<u8>, track: &Track, frame_bytes: u32, shift: u64) { | |||
| 1112 | false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()), | 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 | /// The `stts`/`ctts`/`stss`/`stsc`/`stsz` and chunk-offset tables of one track. | 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 | if let (Some((bytes, _)), Some(audio)) = | 1128 | if let (Some((bytes, _)), Some(audio)) = |
| 1121 | (qt_pcm(track.info.params.codec), track.info.params.audio()) | 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 | track, | 1133 | track, |
| 1126 | bytes * audio.layout.channel_count() as u32, | 1134 | bytes * audio.layout.channel_count() as u32, |
| 1127 | shift, | 1135 | shift, |
| 1128 | - ); | 1136 | + )?; |
| 1129 | - return; | 1137 | + return Ok(()); |
| 1130 | } | 1138 | } |
| 1131 | // stts: runs of equal durations, which for constant-rate video is one entry | 1139 | // stts: runs of equal durations, which for constant-rate video is one entry |
| 1132 | // for the whole film. | 1140 | // for the whole film. |
| @@ -1148,7 +1156,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | |||
| 1148 | runs += 1; | 1156 | runs += 1; |
| 1149 | } | 1157 | } |
| 1150 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); | 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 | // ctts, only where something is actually reordered. | 1161 | // ctts, only where something is actually reordered. |
| 1154 | let offsets: Vec<i64> = track.samples.iter().map(|s| s.cts - s.dts).collect(); | 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 | runs += 1; | 1182 | runs += 1; |
| 1175 | } | 1183 | } |
| 1176 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); | 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 | // stss, only where some samples are not sync ones: no box at all means | 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 | for n in syncs { | 1201 | for n in syncs { |
| 1194 | out.extend_from_slice(&n.to_be_bytes()); | 1202 | out.extend_from_slice(&n.to_be_bytes()); |
| 1195 | } | 1203 | } |
| 1196 | - close(out, stss); | 1204 | + close(out, stss)?; |
| 1197 | } | 1205 | } |
| 1198 | 1206 | ||
| 1199 | // stsc: runs of chunks holding the same number of samples. | 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 | out[count_at..count_at + 4].copy_from_slice(&runs.to_be_bytes()); | 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 | // stsz: one size for all, where they really are all one size. | 1228 | // stsz: one size for all, where they really are all one size. |
| 1221 | let stsz = open(out, b"stsz"); | 1229 | let stsz = open(out, b"stsz"); |
| @@ -1233,7 +1241,7 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | |||
| 1233 | out.extend_from_slice(&sample.size.to_be_bytes()); | 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 | // co64 where the file is big enough to need it, stco where it is not. | 1246 | // co64 where the file is big enough to need it, stco where it is not. |
| 1239 | let big = track | 1247 | let big = track |
| @@ -1250,7 +1258,8 @@ fn sample_tables(out: &mut Vec<u8>, track: &Track, shift: u64) { | |||
| 1250 | false => out.extend_from_slice(&((at + shift) as u32).to_be_bytes()), | 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 | /// The sample entry name and configuration box of a video codec. | 1265 | /// The sample entry name and configuration box of a video codec. |
| @@ -1323,22 +1332,22 @@ fn sample_entry( | |||
| 1323 | out.extend_from_slice(&[0; 32]); // compressorname | 1332 | out.extend_from_slice(&[0; 32]); // compressorname |
| 1324 | out.extend_from_slice(&0x0018u16.to_be_bytes()); | 1333 | out.extend_from_slice(&0x0018u16.to_be_bytes()); |
| 1325 | out.extend_from_slice(&(-1i16).to_be_bytes()); | 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 | if let Some(order) = field_order { | 1336 | if let Some(order) = field_order { |
| 1328 | let (count, detail) = match order { | 1337 | let (count, detail) = match order { |
| 1329 | ec_core::Interlace::Progressive => (1u8, 0u8), | 1338 | ec_core::Interlace::Progressive => (1u8, 0u8), |
| 1330 | ec_core::Interlace::TopFieldFirst => (2, 1), | 1339 | ec_core::Interlace::TopFieldFirst => (2, 1), |
| 1331 | ec_core::Interlace::BottomFieldFirst => (2, 6), | 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 | let sar = video.sample_aspect_ratio; | 1344 | let sar = video.sample_aspect_ratio; |
| 1336 | let (n, d) = sar.map_or((1, 1), |s| (s.num() as u32, s.den() as u32)); | 1345 | let (n, d) = sar.map_or((1, 1), |s| (s.num() as u32, s.den() as u32)); |
| 1337 | let pasp = open(out, b"pasp"); | 1346 | let pasp = open(out, b"pasp"); |
| 1338 | out.extend_from_slice(&n.to_be_bytes()); | 1347 | out.extend_from_slice(&n.to_be_bytes()); |
| 1339 | out.extend_from_slice(&d.to_be_bytes()); | 1348 | out.extend_from_slice(&d.to_be_bytes()); |
| 1340 | - close(out, pasp); | 1349 | + close(out, pasp)?; |
| 1341 | - close(out, at); | 1350 | + close(out, at)?; |
| 1342 | return Ok(()); | 1351 | return Ok(()); |
| 1343 | } | 1352 | } |
| 1344 | if let Some(name) = video_entry(codec) { | 1353 | if let Some(name) = video_entry(codec) { |
| @@ -1364,13 +1373,13 @@ fn sample_entry( | |||
| 1364 | out.extend_from_slice(&[0; 32]); // compressorname | 1373 | out.extend_from_slice(&[0; 32]); // compressorname |
| 1365 | out.extend_from_slice(&0x0018u16.to_be_bytes()); // depth: colour, no alpha | 1374 | out.extend_from_slice(&0x0018u16.to_be_bytes()); // depth: colour, no alpha |
| 1366 | out.extend_from_slice(&(-1i16).to_be_bytes()); // pre_defined | 1375 | out.extend_from_slice(&(-1i16).to_be_bytes()); // pre_defined |
| 1367 | - leaf(out, config_box(codec), config); | 1376 | + leaf(out, config_box(codec), config)?; |
| 1368 | - colour_boxes(out, video.color, video.light); | 1377 | + colour_boxes(out, video.color, video.light)?; |
| 1369 | if let Some(sar) = video.sample_aspect_ratio { | 1378 | if let Some(sar) = video.sample_aspect_ratio { |
| 1370 | let pasp = open(out, b"pasp"); | 1379 | let pasp = open(out, b"pasp"); |
| 1371 | out.extend_from_slice(&(sar.num() as u32).to_be_bytes()); | 1380 | out.extend_from_slice(&(sar.num() as u32).to_be_bytes()); |
| 1372 | out.extend_from_slice(&(sar.den() as u32).to_be_bytes()); | 1381 | out.extend_from_slice(&(sar.den() as u32).to_be_bytes()); |
| 1373 | - close(out, pasp); | 1382 | + close(out, pasp)?; |
| 1374 | } | 1383 | } |
| 1375 | // ffmpeg's order: after `colr`/`pasp`. field_count 1 / detail 0 is a | 1384 | // ffmpeg's order: after `colr`/`pasp`. field_count 1 / detail 0 is a |
| 1376 | // progressive picture; 2 fields with detail 1 is top first (stored and | 1385 | // progressive picture; 2 fields with detail 1 is top first (stored and |
| @@ -1382,9 +1391,9 @@ fn sample_entry( | |||
| 1382 | ec_core::Interlace::TopFieldFirst => (2, 1), | 1391 | ec_core::Interlace::TopFieldFirst => (2, 1), |
| 1383 | ec_core::Interlace::BottomFieldFirst => (2, 6), | 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 | return Ok(()); | 1397 | return Ok(()); |
| 1389 | } | 1398 | } |
| 1390 | if codec == CodecId::Tx3g { | 1399 | if codec == CodecId::Tx3g { |
| @@ -1411,10 +1420,10 @@ fn sample_entry( | |||
| 1411 | out.extend_from_slice(&1u16.to_be_bytes()); | 1420 | out.extend_from_slice(&1u16.to_be_bytes()); |
| 1412 | out.push(5); | 1421 | out.push(5); |
| 1413 | out.extend_from_slice(b"Serif"); | 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 | return Ok(()); | 1427 | return Ok(()); |
| 1419 | } | 1428 | } |
| 1420 | 1429 | ||
| @@ -1477,10 +1486,10 @@ fn sample_entry( | |||
| 1477 | out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame | 1486 | out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame |
| 1478 | out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) | 1487 | out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) |
| 1479 | let wave = open(out, b"wave"); | 1488 | let wave = open(out, b"wave"); |
| 1480 | - leaf(out, b"frma", name); | 1489 | + leaf(out, b"frma", name)?; |
| 1481 | - leaf(out, b"enda", &u16::from(little).to_be_bytes()); | 1490 | + leaf(out, b"enda", &u16::from(little).to_be_bytes())?; |
| 1482 | out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero | 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 | None => {} | 1494 | None => {} |
| 1486 | } | 1495 | } |
| @@ -1494,7 +1503,7 @@ fn sample_entry( | |||
| 1494 | })?; | 1503 | })?; |
| 1495 | let esds = open(out, b"esds"); | 1504 | let esds = open(out, b"esds"); |
| 1496 | out.extend_from_slice(&Esds::aac(asc).write()); | 1505 | out.extend_from_slice(&Esds::aac(asc).write()); |
| 1497 | - close(out, esds); | 1506 | + close(out, esds)?; |
| 1498 | } | 1507 | } |
| 1499 | CodecId::Mp3 => { | 1508 | CodecId::Mp3 => { |
| 1500 | let mut mp3 = Esds::aac(&[][..]); | 1509 | let mut mp3 = Esds::aac(&[][..]); |
| @@ -1502,7 +1511,7 @@ fn sample_entry( | |||
| 1502 | mp3.decoder_specific = None; | 1511 | mp3.decoder_specific = None; |
| 1503 | let esds = open(out, b"esds"); | 1512 | let esds = open(out, b"esds"); |
| 1504 | out.extend_from_slice(&mp3.write()); | 1513 | out.extend_from_slice(&mp3.write()); |
| 1505 | - close(out, esds); | 1514 | + close(out, esds)?; |
| 1506 | } | 1515 | } |
| 1507 | CodecId::Ac3 | 1516 | CodecId::Ac3 |
| 1508 | | CodecId::EAc3 | 1517 | | CodecId::EAc3 |
| @@ -1522,23 +1531,23 @@ fn sample_entry( | |||
| 1522 | CodecId::Alac if config.len() > 8 && &config[4..8] == b"alac" => { | 1531 | CodecId::Alac if config.len() > 8 && &config[4..8] == b"alac" => { |
| 1523 | out.extend_from_slice(config) | 1532 | out.extend_from_slice(config) |
| 1524 | } | 1533 | } |
| 1525 | - CodecId::Alac => leaf(out, b"alac", config), | 1534 | + CodecId::Alac => leaf(out, b"alac", config)?, |
| 1526 | - CodecId::Opus => leaf(out, b"dOps", &dops(config)), | 1535 | + CodecId::Opus => leaf(out, b"dOps", &dops(config))?, |
| 1527 | - _ => leaf(out, audio_config_box(codec), config), | 1536 | + _ => leaf(out, audio_config_box(codec), config)?, |
| 1528 | } | 1537 | } |
| 1529 | } | 1538 | } |
| 1530 | _ => {} | 1539 | _ => {} |
| 1531 | } | 1540 | } |
| 1532 | if chan || qt.is_some() { | 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 | Ok(()) | 1545 | Ok(()) |
| 1537 | } | 1546 | } |
| 1538 | 1547 | ||
| 1539 | /// `colr`, and the two HDR boxes where the grade stated them — metadata a remux | 1548 | /// `colr`, and the two HDR boxes where the grade stated them — metadata a remux |
| 1540 | /// must not quietly drop, because nothing downstream can reconstruct it. | 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 | if color != ec_core::ColorInfo::default() { | 1551 | if color != ec_core::ColorInfo::default() { |
| 1543 | let colr = open(out, b"colr"); | 1552 | let colr = open(out, b"colr"); |
| 1544 | out.extend_from_slice(b"nclx"); | 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 | out.extend_from_slice(&u16::from(color.transfer).to_be_bytes()); | 1555 | out.extend_from_slice(&u16::from(color.transfer).to_be_bytes()); |
| 1547 | out.extend_from_slice(&u16::from(color.matrix).to_be_bytes()); | 1556 | out.extend_from_slice(&u16::from(color.matrix).to_be_bytes()); |
| 1548 | out.push(u8::from(color.full_range) << 7); | 1557 | out.push(u8::from(color.full_range) << 7); |
| 1549 | - close(out, colr); | 1558 | + close(out, colr)?; |
| 1550 | } | 1559 | } |
| 1551 | if light.mastering_max.is_some() | 1560 | if light.mastering_max.is_some() |
| 1552 | || light.mastering_min.is_some() | 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 | out.extend_from_slice(&nits(light.mastering_max)); | 1576 | out.extend_from_slice(&nits(light.mastering_max)); |
| 1568 | out.extend_from_slice(&nits(light.mastering_min)); | 1577 | out.extend_from_slice(&nits(light.mastering_min)); |
| 1569 | - close(out, mdcv); | 1578 | + close(out, mdcv)?; |
| 1570 | } | 1579 | } |
| 1571 | if light.max_cll.is_some() || light.max_fall.is_some() { | 1580 | if light.max_cll.is_some() || light.max_fall.is_some() { |
| 1572 | let clli = open(out, b"clli"); | 1581 | let clli = open(out, b"clli"); |
| 1573 | out.extend_from_slice(&(light.max_cll.unwrap_or(0.0).round() as u16).to_be_bytes()); | 1582 | out.extend_from_slice(&(light.max_cll.unwrap_or(0.0).round() as u16).to_be_bytes()); |
| 1574 | out.extend_from_slice(&(light.max_fall.unwrap_or(0.0).round() as u16).to_be_bytes()); | 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 | /// BT.2020 green, blue, red and D65, in the 0.00002 units an `mdcv` states them. | 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 | /// The QuickTime `chan` box for the layouts that have a stock one, as ffmpeg | 1614 | /// The QuickTime `chan` box for the layouts that have a stock one, as ffmpeg |
| 1605 | /// writes it: a layout tag for mono and stereo, a channel bitmap for 5.1. | 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 | use ec_core::ChannelLayout; | 1617 | use ec_core::ChannelLayout; |
| 1608 | let (tag, bitmap) = match layout { | 1618 | let (tag, bitmap) = match layout { |
| 1609 | ChannelLayout::Mono => (0x0064_0001u32, 0u32), | 1619 | ChannelLayout::Mono => (0x0064_0001u32, 0u32), |
| 1610 | ChannelLayout::Stereo => (0x0065_0002, 0), | 1620 | ChannelLayout::Stereo => (0x0065_0002, 0), |
| 1611 | // UseChannelBitmap; L R C LFE Ls Rs. | 1621 | // UseChannelBitmap; L R C LFE Ls Rs. |
| 1612 | ChannelLayout::Surround5_1 => (0x0001_0000, 0x3f), | 1622 | ChannelLayout::Surround5_1 => (0x0001_0000, 0x3f), |
| 1613 | - _ => return, | 1623 | + _ => return Ok(()), |
| 1614 | }; | 1624 | }; |
| 1615 | let at = open(out, b"chan"); | 1625 | let at = open(out, b"chan"); |
| 1616 | full_head(out, 0, 0); | 1626 | full_head(out, 0, 0); |
| 1617 | out.extend_from_slice(&tag.to_be_bytes()); | 1627 | out.extend_from_slice(&tag.to_be_bytes()); |
| 1618 | out.extend_from_slice(&bitmap.to_be_bytes()); | 1628 | out.extend_from_slice(&bitmap.to_be_bytes()); |
| 1619 | out.extend_from_slice(&0u32.to_be_bytes()); // number of descriptions | 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 | fn audio_entry(codec: CodecId) -> Result<&'static [u8; 4]> { | 1634 | fn audio_entry(codec: CodecId) -> Result<&'static [u8; 4]> { |