forge
Explore
Log in

Don't you dare go Hollow

ec-mp3 encoder: fix MPEG-2/2.5 LSF scalefac_compress coding and 8-bit main_data_begin reservoir cap

Browse files at this commit

8306e71c42e33eaa57f9a0155bb02282b35dc3e3Ahmet Kaan GümüşParent: 43912fe
crates/ec-mp3/src/encode.rsmodified+10 -6
@@ -995,10 +995,11 @@ impl Mp3Encode {
995 self.ready.push_back(frame);995 self.ready.push_back(frame);
996 self.frames += 1;996 self.frames += 1;
997 }997 }
998- // main_data_begin is nine bits, so the reservoir cannot reach further998+ // main_data_begin is nine bits (eight for LSF), so the reservoir cannot
999- // back than 511 bytes; anything older is dropped rather than pointed at.999+ // reach further back than that; anything older is dropped.
1000- if self.assigned > self.used + 511 {1000+ let reach = if self.version == Version::Mpeg1 { 511 } else { 255 };
1001- self.used = self.assigned - 511;1001+ if self.assigned > self.used + reach {
1002+ self.used = self.assigned - reach;
1002 }1003 }
1003 }1004 }
1004 1005
@@ -1319,7 +1320,7 @@ fn code_with(
1319 scalefac: &[u8; SFB_LONG],1320 scalefac: &[u8; SFB_LONG],
1320 sample_rate: u32,1321 sample_rate: u32,
1321 ) -> (CodedGranule, [f32; SFB_LONG]) {1322 ) -> (CodedGranule, [f32; SFB_LONG]) {
1322- let (slen1, slen2, compress) = scalefac_compress(scalefac, block_type);1323+ let (slen1, slen2, compress) = scalefac_compress(scalefac, block_type, sample_rate);
1323 // The scalefactors come out of the same budget as the spectrum does. A1324 // The scalefactors come out of the same budget as the spectrum does. A
1324 // short block transmits six bands of each length, once per window.1325 // short block transmits six bands of each length, once per window.
1325 let scalefac_bits = if block_type == 2 {1326 let scalefac_bits = if block_type == 2 {
@@ -1884,7 +1885,7 @@ fn best_table_cost(ix: &[i32], from: usize, to: usize) -> (u8, u32) {
1884 }1885 }
1885 1886
1886 /// The `scalefac_compress` index whose two lengths hold these scalefactors.1887 /// The `scalefac_compress` index whose two lengths hold these scalefactors.
1887-fn scalefac_compress(scalefac: &[u8; SFB_LONG], block_type: u8) -> (u32, u32, u32) {1888+fn scalefac_compress(scalefac: &[u8; SFB_LONG], block_type: u8, sample_rate: u32) -> (u32, u32, u32) {
1888 let bits_for = |slice: &[u8]| -> u32 {1889 let bits_for = |slice: &[u8]| -> u32 {
1889 let max = slice.iter().copied().max().unwrap_or(0);1890 let max = slice.iter().copied().max().unwrap_or(0);
1890 (0..=4u32).find(|n| max < (1 << n)).unwrap_or(4)1891 (0..=4u32).find(|n| max < (1 << n)).unwrap_or(4)
@@ -1900,6 +1901,9 @@ fn scalefac_compress(scalefac: &[u8; SFB_LONG], block_type: u8) -> (u32, u32, u3
1900 // that does not from silently coding a granule the decoder cannot rebuild.1901 // that does not from silently coding a granule the decoder cannot rebuild.
1901 let need1 = bits_for(first).min(4);1902 let need1 = bits_for(first).min(4);
1902 let need2 = bits_for(second).min(3);1903 let need2 = bits_for(second).min(3);
1904+ if sample_rate < 32000 {
1905+ return (need1, need2, ((need1 * 5 + need1) << 4) | (need2 << 2) | need2);
1906+ }
1903 let mut best = (u32::MAX, 4u32, 3u32, 15u32);1907 let mut best = (u32::MAX, 4u32, 3u32, 15u32);
1904 for (index, (slen1, slen2)) in SLEN.iter().enumerate() {1908 for (index, (slen1, slen2)) in SLEN.iter().enumerate() {
1905 if *slen1 >= need1 && *slen2 >= need2 {1909 if *slen1 >= need1 && *slen2 >= need2 {