symphonia_bundle_mp3/layer3/requantize.rs
1// Symphonia
2// Copyright (c) 2019-2022 The Project Symphonia Developers.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
8use symphonia_core::errors::Result;
9use symphonia_core::io::ReadBitsLtr;
10
11use crate::common::FrameHeader;
12
13use super::{codebooks, common::*, GranuleChannel};
14
15use std::cmp::min;
16use std::{f32, f64};
17
18use lazy_static::lazy_static;
19
20use log::info;
21
22lazy_static! {
23 /// Lookup table for computing x(i) = s(i)^(4/3) where s(i) is a decoded Huffman sample. The
24 /// value of s(i) is bound between 0..8207.
25 static ref REQUANTIZE_POW43: [f32; 8207] = {
26 // It is wasteful to initialize to 0.. however, Symphonia policy is to limit unsafe code to
27 // only symphonia-core.
28 //
29 // TODO: Implement generic lookup table initialization in the core library.
30 let mut pow43 = [0f32; 8207];
31 for (i, pow43) in pow43.iter_mut().enumerate() {
32 *pow43 = f32::powf(i as f32, 4.0 / 3.0);
33 }
34 pow43
35 };
36}
37
38/// Zero a sample buffer.
39#[inline(always)]
40pub(super) fn zero(buf: &mut [f32; 576]) {
41 buf.fill(0.0);
42}
43
44/// Reads the Huffman coded spectral samples for a given channel in a granule from a `BitStream`
45/// into a provided sample buffer. Returns the number of decoded samples (the starting index of the
46/// rzero partition).
47///
48/// Note, each spectral sample is raised to the (4/3)-rd power. This is not actually part of the
49/// Huffman decoding process, but, by converting the integer sample to floating point here we don't
50/// need to do pointless casting or use an extra buffer.
51pub(super) fn read_huffman_samples<B: ReadBitsLtr>(
52 bs: &mut B,
53 channel: &GranuleChannel,
54 part3_bits: u32,
55 buf: &mut [f32; 576],
56) -> Result<usize> {
57 // If there are no Huffman code bits, zero all samples and return immediately.
58 if part3_bits == 0 {
59 buf.fill(0.0);
60 return Ok(0);
61 }
62
63 // Dereference the POW43 table once per granule since there is a tiny overhead each time a
64 // lazy_static is dereferenced that should be amortized over as many samples as possible.
65 let pow43_table: &[f32; 8207] = &REQUANTIZE_POW43;
66
67 let mut bits_read = 0;
68 let mut i = 0;
69
70 // There are two samples per big_value, therefore multiply big_values by 2 to get number of
71 // samples in the big_value partition.
72 let big_values_len = 2 * channel.big_values as usize;
73
74 // There are up-to 3 regions in the big_value partition. Determine the sample index denoting the
75 // end of each region (non-inclusive). Clamp to the end of the big_values partition.
76 let regions: [usize; 3] = [
77 min(channel.region1_start, big_values_len),
78 min(channel.region2_start, big_values_len),
79 min(576, big_values_len),
80 ];
81
82 // Iterate over each region in big_values.
83 for (region_idx, region_end) in regions.iter().enumerate() {
84 // Select the Huffman table based on the region's table select value.
85 let table_select = channel.table_select[region_idx] as usize;
86
87 // Tables 0..16 are all unique, while tables 16..24 and 24..32 each use one table but
88 // differ in the number of linbits to use.
89 let codebook = match table_select {
90 0..=15 => &codebooks::CODEBOOK_TABLES[table_select],
91 16..=23 => &codebooks::CODEBOOK_TABLES[16],
92 24..=31 => &codebooks::CODEBOOK_TABLES[17],
93 _ => unreachable!(),
94 };
95
96 let linbits = codebooks::CODEBOOK_LINBITS[table_select];
97
98 // If the table for a region is empty, fill the region with zeros and move on to the next
99 // region.
100 if codebook.is_empty() {
101 while i < *region_end {
102 buf[i] = 0.0;
103 i += 1;
104 buf[i] = 0.0;
105 i += 1;
106 }
107 continue;
108 }
109
110 // Otherwise, read the big_values.
111 while i < *region_end && bits_read < part3_bits {
112 // Decode the next Huffman code.
113 let (value, code_len) = bs.read_codebook(codebook)?;
114 bits_read += code_len;
115
116 // In the big_values partition, each Huffman code decodes to two sample, x and y. Each
117 // sample being 4-bits long.
118 let mut x = (value >> 4) as usize;
119 let mut y = (value & 0xf) as usize;
120
121 // If the first sample, x, is not 0, further process it.
122 if x > 0 {
123 // If x is saturated (it is at the maximum possible value), and the table specifies
124 // linbits, then read linbits more bits and add it to the sample.
125 if x == 15 && linbits > 0 {
126 x += bs.read_bits_leq32(linbits)? as usize;
127 bits_read += linbits;
128 }
129
130 // The next bit is the sign bit. If the sign bit is 1, then the sample should be
131 // negative. The value of the sample is raised to the (4/3) power.
132 buf[i] = (1.0 - 2.0 * bs.read_bit()? as f32) * pow43_table[x];
133 bits_read += 1;
134 }
135 else {
136 buf[i] = 0.0;
137 }
138
139 i += 1;
140
141 // Likewise, repeat the previous two steps for the second sample, y.
142 if y > 0 {
143 if y == 15 && linbits > 0 {
144 y += bs.read_bits_leq32(linbits)? as usize;
145 bits_read += linbits;
146 }
147
148 buf[i] = (1.0 - 2.0 * bs.read_bit()? as f32) * pow43_table[y];
149 bits_read += 1;
150 }
151 else {
152 buf[i] = 0.0;
153 }
154
155 i += 1;
156 }
157 }
158
159 let count1_codebook = &codebooks::QUADS_CODEBOOK_TABLE[usize::from(channel.count1table_select)];
160
161 // Read the count1 partition.
162 while i <= 572 && bits_read < part3_bits {
163 // In the count1 partition, each Huffman code decodes to 4 samples: v, w, x, and y.
164 // Each sample is 1-bit long (1 or 0).
165 //
166 // For each 1-bit sample, if it is 0, then the dequantized sample value is 0 as well. If
167 // the 1-bit sample is 1, then a sign bit is read. The dequantized sample is then either
168 // +/-1.0 depending on the sign bit.
169
170 // Decode the next Huffman code.
171 let (value, code_len) = bs.read_codebook(count1_codebook)?;
172 bits_read += code_len;
173
174 // The first 4 bits indicate if a sample if 0 or 1. Count the number of samples with a
175 // non-zero value.
176 let num_ones = (value & 0xf).count_ones();
177
178 // Read the sign bits for each non-zero sample in a single read.
179 let mut signs = bs.read_bits_leq32(num_ones)?;
180 bits_read += num_ones;
181
182 // Unpack the samples.
183 if value & 0x1 != 0 {
184 buf[i + 3] = 1.0 - 2.0 * (signs & 1) as f32;
185 signs >>= 1;
186 }
187 else {
188 buf[i + 3] = 0.0;
189 }
190
191 if value & 0x2 != 0 {
192 buf[i + 2] = 1.0 - 2.0 * (signs & 1) as f32;
193 signs >>= 1;
194 }
195 else {
196 buf[i + 2] = 0.0;
197 }
198
199 if value & 0x4 != 0 {
200 buf[i + 1] = 1.0 - 2.0 * (signs & 1) as f32;
201 signs >>= 1;
202 }
203 else {
204 buf[i + 1] = 0.0;
205 }
206
207 if value & 0x8 != 0 {
208 buf[i + 0] = 1.0 - 2.0 * (signs & 1) as f32;
209 }
210 else {
211 buf[i + 0] = 0.0;
212 }
213
214 i += 4;
215 }
216
217 // Ignore any extra "stuffing" bits.
218 if bits_read < part3_bits {
219 bs.ignore_bits(part3_bits - bits_read)?;
220 }
221 // Word on the street is that some encoders are poor at "stuffing" bits, resulting in part3_len
222 // being ever so slightly too large. This causes the Huffman decode loop to decode the next few
223 // bits as spectral samples. However, these bits are actually random data and are not real
224 // samples, therefore, undo them! The caller will be reponsible for re-aligning the bitstream
225 // reader. Candy Pop confirms this.
226 else if bits_read > part3_bits && i > big_values_len {
227 info!("count1 overrun, malformed bitstream");
228 i -= 4;
229 }
230 else if bits_read > part3_bits {
231 // It seems that most other decoders don't undo overruns of the big values. We'll just print
232 // a message for now.
233 info!("big_values overrun, malformed bitstream");
234 }
235
236 // The final partition after the count1 partition is the rzero partition. Samples in this
237 // partition are all 0.
238 buf[i..].fill(0.0);
239
240 Ok(i)
241}
242
243/// Requantize long block samples in `buf`.
244fn requantize_long(channel: &GranuleChannel, bands: &[usize], buf: &mut [f32; 576]) {
245 // For long blocks dequantization and scaling is governed by the following equation:
246 //
247 // xr(i) = s(i)^(4/3) * 2^(0.25*A) * 2^(-B)
248 // where:
249 // s(i) is the decoded Huffman sample
250 // xr(i) is the dequantized sample
251 // and:
252 // A = global_gain[gr] - 210
253 // B = scalefac_multiplier * (scalefacs[gr][ch][sfb] + (preflag[gr] * pretab[sfb]))
254 //
255 // Note: The samples in buf are the result of s(i)^(4/3) for each sample i.
256 debug_assert!(bands.len() <= 23);
257
258 // The preemphasis table is from table B.6 in ISO/IEC 11172-3.
259 const PRE_EMPHASIS: [u8; 22] =
260 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0];
261
262 // Calculate A, it is constant for the entire requantization.
263 let a = i32::from(channel.global_gain) - 210;
264
265 let scalefac_shift = if channel.scalefac_scale { 2 } else { 1 };
266
267 // Requantize each scale-factor band in buf.
268 for (i, (start, end)) in bands.iter().zip(&bands[1..]).enumerate() {
269 // Do not requantize bands starting after the rzero sample since all samples from there on
270 // are 0.
271 if *start >= channel.rzero {
272 break;
273 }
274
275 // Lookup the pre-emphasis amount if required.
276 let pre_emphasis = if channel.preflag { PRE_EMPHASIS[i] } else { 0 };
277
278 // Calculate B.
279 let b = i32::from((channel.scalefacs[i] + pre_emphasis) << scalefac_shift);
280
281 // Calculate 2^(0.25*A) * 2^(-B). This can be rewritten as 2^{ 0.25 * (A - 4 * B) }.
282 // Since scalefac_shift was multiplies by 4 above, the final equation becomes
283 // 2^{ 0.25 * (A - B) }.
284 let pow2ab = f64::powf(2.0, 0.25 * f64::from(a - b)) as f32;
285
286 // Calculate the ending sample index for the scale-factor band, clamping it to the length of
287 // the sample buffer.
288 let band_end = min(*end, channel.rzero);
289
290 // The sample buffer contains s(i)^(4/3), now multiply in 2^(0.25*A) * 2^(-B) to get xr(i).
291 for sample in &mut buf[*start..band_end] {
292 *sample *= pow2ab;
293 }
294 }
295}
296
297/// Requantize short block samples in `buf` starting at scale-factor band `sfb_init`.
298fn requantize_short(
299 channel: &GranuleChannel,
300 bands: &[usize],
301 switch: usize,
302 buf: &mut [f32; 576],
303) {
304 // For short blocks dequantization and scaling is governed by the following equation:
305 //
306 // xr(i) = s(i)^(4/3) * 2^(0.25*A) * 2^(-B)
307 // where:
308 // s(i) is the decoded Huffman sample
309 // xr(i) is the dequantized sample
310 // and:
311 // A = global_gain[gr] - 210 - (8 * subblock_gain[gr][win])
312 // B = scalefac_multiplier * scalefacs[gr][ch][sfb][win]
313 //
314 // Note: The samples in buf are the result of s(i)^(4/3) for each sample i.
315 debug_assert!(bands.len() <= 40);
316
317 // Calculate the window-independant part of A: global_gain[gr] - 210.
318 let gain = i32::from(channel.global_gain) - 210;
319
320 // Calculate A for each window.
321 let a = [
322 gain - 8 * i32::from(channel.subblock_gain[0]),
323 gain - 8 * i32::from(channel.subblock_gain[1]),
324 gain - 8 * i32::from(channel.subblock_gain[2]),
325 ];
326
327 // Likweise, the scalefac_multiplier is constant for the granule. The actual scale is multiplied
328 // by 4 to combine the two pow2 operations into one by adding the exponents. The sum of the
329 // exponent is multiplied by 0.25 so B must be multiplied by 4 to counter the quartering. A
330 // bitshift operation is used for the actual multiplication, so scalefac_multiplier is named
331 // scalefac_shift in this case.
332 let scalefac_shift = if channel.scalefac_scale { 2 } else { 1 };
333
334 for (i, (start, end)) in bands.iter().zip(&bands[1..]).enumerate() {
335 // Do not requantize bands starting after the rzero sample since all samples from there on
336 // are 0.
337 if *start >= channel.rzero {
338 break;
339 }
340
341 // Calculate B.
342 let b = i32::from(channel.scalefacs[switch + i] << scalefac_shift);
343
344 // Calculate 2^(0.25*A) * 2^(-B). This can be rewritten as 2^{ 0.25 * (A - 4 * B) }.
345 // Since scalefac_shift multiplies by 4 above, the final equation becomes
346 // 2^{ 0.25 * (A - B) }.
347 let pow2ab = f64::powf(2.0, 0.25 * f64::from(a[i % 3] - b)) as f32;
348
349 // Clamp the ending sample index to the rzero sample index. Since samples starting from
350 // rzero are 0, there is no point in requantizing them.
351 let win_end = min(*end, channel.rzero);
352
353 // The sample buffer contains s(i)^(4/3), now multiply in 2^(0.25*A) * 2^(-B) to get
354 // xr(i).
355 for sample in &mut buf[*start..win_end] {
356 *sample *= pow2ab;
357 }
358 }
359}
360
361/// Requantize samples in `buf` regardless of block type.
362pub(super) fn requantize(header: &FrameHeader, channel: &GranuleChannel, buf: &mut [f32; 576]) {
363 match channel.block_type {
364 BlockType::Short { is_mixed: false } => {
365 requantize_short(channel, &SFB_SHORT_BANDS[header.sample_rate_idx], 0, buf);
366 }
367 BlockType::Short { is_mixed: true } => {
368 // A mixed block is a combination of a long block and short blocks. The first few scale
369 // factor bands, and thus samples, belong to a single long block, while the remaining
370 // bands and samples belong to short blocks. Therefore, requantization for mixed blocks
371 // can be decomposed into short and long block requantizations.
372 //
373 // As per ISO/IEC 11172-3, the short scale factor band at which the long block ends and
374 // the short blocks begin is denoted by switch_point_s.
375 let bands = SFB_MIXED_BANDS[header.sample_rate_idx];
376 let switch = SFB_MIXED_SWITCH_POINT[header.sample_rate_idx];
377
378 requantize_long(channel, &bands[..switch], buf);
379 requantize_short(channel, &bands[switch..], switch, buf);
380 }
381 _ => {
382 requantize_long(channel, &SFB_LONG_BANDS[header.sample_rate_idx], buf);
383 }
384 }
385}