forge
Explore
Log in

Don't you dare go Hollow

Encoder parameter-path fixes found by ffmpeg round-trip sweeps

Browse files at this commit

c561bfe77c7620676b06a7f1acb777d14358e004Ahmet Kaan GümüşParent: 8306e71
- ec-truehd: set nonrestart_substr on access units without major sync;
  ffmpeg rejected every AU after the first
- ec-mp3: write the Xing/Info frame at a bitrate that holds the whole tag;
  at low bitrates the truncated tag made ffmpeg read gapless fields from
  audio frames (16 kHz/8 kbps decoded as noise, others shifted)
- ec-aac: cap a frame at 6144 bits per channel; high bitrate per channel
  overflowed the decoder buffer and the 13-bit ADTS length
- ec-av1: validate tiles against the superblock size actually coded (128),
  no shift overflow on huge log2, refuse base_q_idx 0 (lossless) up front
- ec-ac3: 0 Hz no longer panics
- ec-alac: refuse frame_length outside 1..=2^20 (0 hung) and 0 Hz
crates/ec-aac/src/encode.rsmodified+3 -5
@@ -322,14 +322,12 @@ impl AacEncoder {
322 / u64::from(self.sample_rate.max(1))) as i32322 / u64::from(self.sample_rate.max(1))) as i32
323 - header_bits)323 - header_bits)
324 .max(0);324 .max(0);
325- let budget = target325+ let cap = RESERVOIR_MAX * self.channels as i32;
326- + self326+ let budget = (target + self.reservoir.clamp(0, cap)).min(cap);
327- .reservoir
328- .clamp(0, RESERVOIR_MAX * self.channels as i32);
329 let data = self.write_frame(&frames, seq, budget);327 let data = self.write_frame(&frames, seq, budget);
330 let used = data.len() as i32 * 8;328 let used = data.len() as i32 * 8;
331 self.reservoir =329 self.reservoir =
332- (self.reservoir + target - used).clamp(0, RESERVOIR_MAX * self.channels as i32);330+ (self.reservoir + target - used).clamp(0, cap);
333 let data = if self.config.adts {331 let data = if self.config.adts {
334 let header = AdtsHeader {332 let header = AdtsHeader {
335 object_type: 2,333 object_type: 2,
crates/ec-ac3/src/encode.rsmodified+1 -1
@@ -255,7 +255,7 @@ impl Ac3Encoder {
255 let (acmod, lfeon) = acmod_for(config.channels)?;255 let (acmod, lfeon) = acmod_for(config.channels)?;
256 let nfchans = acmod.nfchans();256 let nfchans = acmod.nfchans();
257 let coded = nfchans + usize::from(lfeon);257 let coded = nfchans + usize::from(lfeon);
258- let fscod = SAMPLE_RATE258+ let fscod = SAMPLE_RATE[..3]
259 .iter()259 .iter()
260 .position(|&r| r == config.sample_rate)260 .position(|&r| r == config.sample_rate)
261 .ok_or_else(|| {261 .ok_or_else(|| {
crates/ec-alac/src/encode.rsmodified+9 -0
@@ -491,6 +491,15 @@ impl AlacEncoder {
491 "this encoder writes mono and stereo only",491 "this encoder writes mono and stereo only",
492 ));492 ));
493 }493 }
494+ if !(1..=1 << 20).contains(&frame_length) {
495+ return Err(Error::unsupported(
496+ format!("ALAC frame length {frame_length}"),
497+ "frame length must be 1..=1048576",
498+ ));
499+ }
500+ if sample_rate == 0 {
501+ return Err(Error::unsupported("ALAC at 0 Hz", "sample rate must be non-zero"));
502+ }
494 let cookie = MagicCookie {503 let cookie = MagicCookie {
495 frame_length,504 frame_length,
496 compatible_version: 0,505 compatible_version: 0,
crates/ec-av1/src/encoder/mod.rsmodified+14 -5
@@ -1531,6 +1531,12 @@ impl Av1Encoder {
1531 if config.gop == 0 {1531 if config.gop == 0 {
1532 return Err(Error::unsupported("AV1 encode", "gop must be at least 1"));1532 return Err(Error::unsupported("AV1 encode", "gop must be at least 1"));
1533 }1533 }
1534+ if config.base_q_idx == 0 {
1535+ return Err(Error::unsupported(
1536+ "AV1 encode",
1537+ "base_q_idx 0 is lossless, which this encoder does not code",
1538+ ));
1539+ }
1534 // Validate the geometry the same way the picture-level entry points1540 // Validate the geometry the same way the picture-level entry points
1535 // do, before the first `encode()` call rather than only surfacing it1541 // do, before the first `encode()` call rather than only surfacing it
1536 // then.1542 // then.
@@ -1539,15 +1545,18 @@ impl Av1Encoder {
1539 // more tile columns (rows) than it has 64x64 superblocks across1545 // more tile columns (rows) than it has 64x64 superblocks across
1540 // (down) -- the header's own `max_log2_tile_cols` bound (spec1546 // (down) -- the header's own `max_log2_tile_cols` bound (spec
1541 // 5.9.15), refused here rather than at the first `encode()` call.1547 // 5.9.15), refused here rather than at the first `encode()` call.
1542- let sb_cols = config.width.div_ceil(64) as u32;1548+ let sb = if crate::encode::sb128_on() { 128 } else { 64 };
1543- let sb_rows = config.height.div_ceil(64) as u32;1549+ let sb_cols = config.width.div_ceil(sb) as u32;
1544- if (1u32 << config.tile_cols_log2) > sb_cols || (1u32 << config.tile_rows_log2) > sb_rows {1550+ let sb_rows = config.height.div_ceil(sb) as u32;
1551+ let tiles = |log2: u32| 1u32.checked_shl(log2).unwrap_or(u32::MAX);
1552+ let max_log2 = |n: u32| n.min(64).next_power_of_two().trailing_zeros();
1553+ if config.tile_cols_log2 > max_log2(sb_cols) || config.tile_rows_log2 > max_log2(sb_rows) {
1545 return Err(Error::unsupported(1554 return Err(Error::unsupported(
1546 "AV1 encode",1555 "AV1 encode",
1547 format!(1556 format!(
1548 "{}x{} tiles need more than the {sb_cols}x{sb_rows} superblocks this frame has",1557 "{}x{} tiles need more than the {sb_cols}x{sb_rows} superblocks this frame has",
1549- 1 << config.tile_cols_log2,1558+ tiles(config.tile_cols_log2),
1550- 1 << config.tile_rows_log21559+ tiles(config.tile_rows_log2)
1551 ),1560 ),
1552 ));1561 ));
1553 }1562 }
crates/ec-mp3/src/encode.rsmodified+17 -5
@@ -439,6 +439,8 @@ const DECODER_DELAY: u32 = 529;
439 /// tag that parses and is then ignored, which is worse than useless — the439 /// tag that parses and is then ignored, which is worse than useless — the
440 /// gapless information would be present and unusable. The rest of the440 /// gapless information would be present and unusable. The rest of the
441 /// extension is filled honestly or zeroed.441 /// extension is filled honestly or zeroed.
442+const INFO_TAG_LEN: usize = 52;
443+
442 const GAPLESS_VERSION: &[u8; 9] = &[0x4c, 0x41, 0x4d, 0x45, b'3', b'.', b'1', b'0', b'0'];444 const GAPLESS_VERSION: &[u8; 9] = &[0x4c, 0x41, 0x4d, 0x45, b'3', b'.', b'1', b'0', b'0'];
443 445
444 /// The Xing/Info header frame every player expects first: a silent frame whose446 /// The Xing/Info header frame every player expects first: a silent frame whose
@@ -453,7 +455,19 @@ const GAPLESS_VERSION: &[u8; 9] = &[0x4c, 0x41, 0x4d, 0x45, b'3', b'.', b'1', b'
453 /// counts as of the first pull.455 /// counts as of the first pull.
454 fn info_frame(core: &Mp3Encode, samples: u64, complete: bool) -> Vec<u8> {456 fn info_frame(core: &Mp3Encode, samples: u64, complete: bool) -> Vec<u8> {
455 let vbr = core.vbr_quality.is_some();457 let vbr = core.vbr_quality.is_some();
456- let header = core.header(core.bitrate_kbps);458+ let table: &[u32] = if core.version == Version::Mpeg1 {
459+ &BITRATES_V1
460+ } else {
461+ &BITRATES_V2
462+ };
463+ let side = core.header(core.bitrate_kbps).side_info_len();
464+ let kbps = table
465+ .iter()
466+ .copied()
467+ .filter(|&k| k >= core.bitrate_kbps)
468+ .find(|&k| core.header(k).frame_len().unwrap_or(0) >= 4 + side + INFO_TAG_LEN)
469+ .unwrap_or(table[table.len() - 1]);
470+ let header = core.header(kbps);
457 let frame_len = header.frame_len().unwrap_or(417);471 let frame_len = header.frame_len().unwrap_or(417);
458 let mut out = vec![0u8; frame_len];472 let mut out = vec![0u8; frame_len];
459 out[..4].copy_from_slice(&header.to_bytes());473 out[..4].copy_from_slice(&header.to_bytes());
@@ -511,10 +525,8 @@ fn info_frame(core: &Mp3Encode, samples: u64, complete: bool) -> Vec<u8> {
511 tag.extend_from_slice(&0u32.to_be_bytes()); // music length525 tag.extend_from_slice(&0u32.to_be_bytes()); // music length
512 tag.extend_from_slice(&0u16.to_be_bytes()); // music CRC526 tag.extend_from_slice(&0u16.to_be_bytes()); // music CRC
513 tag.extend_from_slice(&0u16.to_be_bytes()); // tag CRC527 tag.extend_from_slice(&0u16.to_be_bytes()); // tag CRC
514- // A frame too small to hold the whole tag keeps the fields that fit, in528+ debug_assert_eq!(tag.len(), INFO_TAG_LEN);
515- // order, which is how the parsers read it anyway.529+ let take = tag.len().min(frame_len - at);
516- let room = frame_len - at;
517- let take = tag.len().min(room);
518 out[at..at + take].copy_from_slice(&tag[..take]);530 out[at..at + take].copy_from_slice(&tag[..take]);
519 out531 out
520 }532 }
crates/ec-truehd/src/encode.rsmodified+1 -1
@@ -621,7 +621,7 @@ impl TrueHdEncoder {
621 let checksum = major_sync_checksum(&major_sync);621 let checksum = major_sync_checksum(&major_sync);
622 major_sync[26..28].copy_from_slice(&checksum.to_le_bytes());622 major_sync[26..28].copy_from_slice(&checksum.to_le_bytes());
623 let major_sync_len = if is_first { major_sync.len() } else { 0 };623 let major_sync_len = if is_first { major_sync.len() } else { 0 };
624- let dir_word = (seg_bytes.len() as u16 / 2) & 0x0FFF; // 1 substream, no extra word624+ let dir_word = (u16::from(!is_first) << 14) | ((seg_bytes.len() as u16 / 2) & 0x0FFF);
625 625
626 let total_len = 4 + major_sync_len + 2 + seg_bytes.len();626 let total_len = 4 + major_sync_len + 2 + seg_bytes.len();
627 let len_words = (total_len as u16 / 2) & 0x0FFF;627 let len_words = (total_len as u16 / 2) & 0x0FFF;