diff --git a/crates/ec-aac/src/encode.rs b/crates/ec-aac/src/encode.rs index b1d7c685..cbab94c1 100644 --- a/crates/ec-aac/src/encode.rs +++ b/crates/ec-aac/src/encode.rs @@ -322,14 +322,12 @@ impl AacEncoder { / u64::from(self.sample_rate.max(1))) as i32 - header_bits) .max(0); - let budget = target - + self - .reservoir - .clamp(0, RESERVOIR_MAX * self.channels as i32); + let cap = RESERVOIR_MAX * self.channels as i32; + let budget = (target + self.reservoir.clamp(0, cap)).min(cap); let data = self.write_frame(&frames, seq, budget); let used = data.len() as i32 * 8; self.reservoir = - (self.reservoir + target - used).clamp(0, RESERVOIR_MAX * self.channels as i32); + (self.reservoir + target - used).clamp(0, cap); let data = if self.config.adts { let header = AdtsHeader { object_type: 2, diff --git a/crates/ec-ac3/src/encode.rs b/crates/ec-ac3/src/encode.rs index aa709ac3..cad68fcf 100644 --- a/crates/ec-ac3/src/encode.rs +++ b/crates/ec-ac3/src/encode.rs @@ -255,7 +255,7 @@ impl Ac3Encoder { let (acmod, lfeon) = acmod_for(config.channels)?; let nfchans = acmod.nfchans(); let coded = nfchans + usize::from(lfeon); - let fscod = SAMPLE_RATE + let fscod = SAMPLE_RATE[..3] .iter() .position(|&r| r == config.sample_rate) .ok_or_else(|| { diff --git a/crates/ec-alac/src/encode.rs b/crates/ec-alac/src/encode.rs index 5fbe57db..2b5aa174 100644 --- a/crates/ec-alac/src/encode.rs +++ b/crates/ec-alac/src/encode.rs @@ -491,6 +491,15 @@ impl AlacEncoder { "this encoder writes mono and stereo only", )); } + if !(1..=1 << 20).contains(&frame_length) { + return Err(Error::unsupported( + format!("ALAC frame length {frame_length}"), + "frame length must be 1..=1048576", + )); + } + if sample_rate == 0 { + return Err(Error::unsupported("ALAC at 0 Hz", "sample rate must be non-zero")); + } let cookie = MagicCookie { frame_length, compatible_version: 0, diff --git a/crates/ec-av1/src/encoder/mod.rs b/crates/ec-av1/src/encoder/mod.rs index f64c29ab..f221b88a 100644 --- a/crates/ec-av1/src/encoder/mod.rs +++ b/crates/ec-av1/src/encoder/mod.rs @@ -1531,6 +1531,12 @@ impl Av1Encoder { if config.gop == 0 { return Err(Error::unsupported("AV1 encode", "gop must be at least 1")); } + if config.base_q_idx == 0 { + return Err(Error::unsupported( + "AV1 encode", + "base_q_idx 0 is lossless, which this encoder does not code", + )); + } // Validate the geometry the same way the picture-level entry points // do, before the first `encode()` call rather than only surfacing it // then. @@ -1539,15 +1545,18 @@ impl Av1Encoder { // more tile columns (rows) than it has 64x64 superblocks across // (down) -- the header's own `max_log2_tile_cols` bound (spec // 5.9.15), refused here rather than at the first `encode()` call. - let sb_cols = config.width.div_ceil(64) as u32; - let sb_rows = config.height.div_ceil(64) as u32; - if (1u32 << config.tile_cols_log2) > sb_cols || (1u32 << config.tile_rows_log2) > sb_rows { + let sb = if crate::encode::sb128_on() { 128 } else { 64 }; + let sb_cols = config.width.div_ceil(sb) as u32; + let sb_rows = config.height.div_ceil(sb) as u32; + let tiles = |log2: u32| 1u32.checked_shl(log2).unwrap_or(u32::MAX); + let max_log2 = |n: u32| n.min(64).next_power_of_two().trailing_zeros(); + if config.tile_cols_log2 > max_log2(sb_cols) || config.tile_rows_log2 > max_log2(sb_rows) { return Err(Error::unsupported( "AV1 encode", format!( "{}x{} tiles need more than the {sb_cols}x{sb_rows} superblocks this frame has", - 1 << config.tile_cols_log2, - 1 << config.tile_rows_log2 + tiles(config.tile_cols_log2), + tiles(config.tile_rows_log2) ), )); } diff --git a/crates/ec-mp3/src/encode.rs b/crates/ec-mp3/src/encode.rs index a5191fb5..a0544533 100644 --- a/crates/ec-mp3/src/encode.rs +++ b/crates/ec-mp3/src/encode.rs @@ -439,6 +439,8 @@ const DECODER_DELAY: u32 = 529; /// tag that parses and is then ignored, which is worse than useless — the /// gapless information would be present and unusable. The rest of the /// extension is filled honestly or zeroed. +const INFO_TAG_LEN: usize = 52; + const GAPLESS_VERSION: &[u8; 9] = &[0x4c, 0x41, 0x4d, 0x45, b'3', b'.', b'1', b'0', b'0']; /// 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' /// counts as of the first pull. fn info_frame(core: &Mp3Encode, samples: u64, complete: bool) -> Vec { let vbr = core.vbr_quality.is_some(); - let header = core.header(core.bitrate_kbps); + let table: &[u32] = if core.version == Version::Mpeg1 { + &BITRATES_V1 + } else { + &BITRATES_V2 + }; + let side = core.header(core.bitrate_kbps).side_info_len(); + let kbps = table + .iter() + .copied() + .filter(|&k| k >= core.bitrate_kbps) + .find(|&k| core.header(k).frame_len().unwrap_or(0) >= 4 + side + INFO_TAG_LEN) + .unwrap_or(table[table.len() - 1]); + let header = core.header(kbps); let frame_len = header.frame_len().unwrap_or(417); let mut out = vec![0u8; frame_len]; out[..4].copy_from_slice(&header.to_bytes()); @@ -511,10 +525,8 @@ fn info_frame(core: &Mp3Encode, samples: u64, complete: bool) -> Vec { tag.extend_from_slice(&0u32.to_be_bytes()); // music length tag.extend_from_slice(&0u16.to_be_bytes()); // music CRC tag.extend_from_slice(&0u16.to_be_bytes()); // tag CRC - // A frame too small to hold the whole tag keeps the fields that fit, in - // order, which is how the parsers read it anyway. - let room = frame_len - at; - let take = tag.len().min(room); + debug_assert_eq!(tag.len(), INFO_TAG_LEN); + let take = tag.len().min(frame_len - at); out[at..at + take].copy_from_slice(&tag[..take]); out } diff --git a/crates/ec-truehd/src/encode.rs b/crates/ec-truehd/src/encode.rs index c41dfa7d..aa6073ef 100644 --- a/crates/ec-truehd/src/encode.rs +++ b/crates/ec-truehd/src/encode.rs @@ -621,7 +621,7 @@ impl TrueHdEncoder { let checksum = major_sync_checksum(&major_sync); major_sync[26..28].copy_from_slice(&checksum.to_le_bytes()); let major_sync_len = if is_first { major_sync.len() } else { 0 }; - let dir_word = (seg_bytes.len() as u16 / 2) & 0x0FFF; // 1 substream, no extra word + let dir_word = (u16::from(!is_first) << 14) | ((seg_bytes.len() as u16 / 2) & 0x0FFF); let total_len = 4 + major_sync_len + 2 + seg_bytes.len(); let len_words = (total_len as u16 / 2) & 0x0FFF;