symphonia_bundle_mp3/layer3/bitstream.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::{decode_error, Result};
9use symphonia_core::io::ReadBitsLtr;
10
11use crate::common::{ChannelMode, FrameHeader};
12
13use super::{common::*, FrameData, Granule, GranuleChannel};
14
15/// Pairs of bit lengths for MPEG version 1 scale factors. For MPEG version 1, there are two
16/// possible bit lengths for scale factors: slen1 and slen2. The first N of bands have scale factors
17/// of bit length slen1, while the remaining bands have length slen2. The value of the switch point,
18/// N, is determined by block type.
19///
20/// This table is indexed by scalefac_compress.
21const SCALE_FACTOR_SLEN: [(u32, u32); 16] = [
22 (0, 0),
23 (0, 1),
24 (0, 2),
25 (0, 3),
26 (3, 0),
27 (1, 1),
28 (1, 2),
29 (1, 3),
30 (2, 1),
31 (2, 2),
32 (2, 3),
33 (3, 1),
34 (3, 2),
35 (3, 3),
36 (4, 2),
37 (4, 3),
38];
39
40/// For MPEG version 2, each scale factor band has a different scale factor. The length in bits of
41/// a scale factor (slen) can be one of 4 values. The values in this table indicate the number of
42/// scale factors that have length slen[0..4]. Slen[0..4] is calculated from scalefac_compress.
43///
44/// This table is indexed by channel_mode, scalefac_compress, and block_type.
45const SCALE_FACTOR_MPEG2_NSFB: [[[usize; 4]; 3]; 6] = [
46 // Intensity stereo channel modes.
47 [[7, 7, 7, 0], [12, 12, 12, 0], [6, 15, 12, 0]],
48 [[6, 6, 6, 3], [12, 9, 9, 6], [6, 12, 9, 6]],
49 [[8, 8, 5, 0], [15, 12, 9, 0], [6, 18, 9, 0]],
50 // Other channel modes.
51 [[6, 5, 5, 5], [9, 9, 9, 9], [6, 9, 9, 9]],
52 [[6, 5, 7, 3], [9, 9, 12, 6], [6, 9, 12, 6]],
53 [[11, 10, 0, 0], [18, 18, 0, 0], [15, 18, 0, 0]],
54];
55
56/// Reads the side_info for a single channel in a granule from a `BitStream`.
57fn read_granule_channel_side_info<B: ReadBitsLtr>(
58 bs: &mut B,
59 channel: &mut GranuleChannel,
60 header: &FrameHeader,
61) -> Result<()> {
62 channel.part2_3_length = bs.read_bits_leq32(12)? as u16;
63 channel.big_values = bs.read_bits_leq32(9)? as u16;
64
65 // The maximum number of samples in a granule is 576. One big_value decodes to 2 samples,
66 // therefore there can be no more than 288 (576/2) big_values.
67 if channel.big_values > 288 {
68 return decode_error("mpa: granule big_values > 288");
69 }
70
71 channel.global_gain = bs.read_bits_leq32(8)? as u8;
72
73 channel.scalefac_compress =
74 if header.is_mpeg1() { bs.read_bits_leq32(4) } else { bs.read_bits_leq32(9) }? as u16;
75
76 let window_switching = bs.read_bool()?;
77
78 if window_switching {
79 let block_type_enc = bs.read_bits_leq32(2)?;
80
81 let is_mixed = bs.read_bool()?;
82
83 channel.block_type = match block_type_enc {
84 // Only transitional Long blocks (Start, End) are allowed with window switching.
85 0b00 => return decode_error("mpa: invalid block_type"),
86 0b01 => BlockType::Start,
87 0b10 => BlockType::Short { is_mixed },
88 0b11 => BlockType::End,
89 _ => unreachable!(),
90 };
91
92 // When window switching is used, there are only two regions, therefore there are only
93 // two table selectors.
94 for i in 0..2 {
95 channel.table_select[i] = bs.read_bits_leq32(5)? as u8;
96 }
97
98 for i in 0..3 {
99 channel.subblock_gain[i] = bs.read_bits_leq32(3)? as u8;
100 }
101
102 // When using window switching, the boundaries of region[0..3] are set implicitly according
103 // to the MPEG version and block type. Below, the boundaries to set as per the applicable
104 // standard.
105 //
106 // If MPEG version 2.5 specifically...
107 if header.is_mpeg2p5() {
108 // For MPEG2.5, the number of scale-factor bands in region0 depends on the block type.
109 // The standard indicates these values as 1 less than the actual value, therefore 1 is
110 // added here to both values.
111 let region0_count = match channel.block_type {
112 BlockType::Short { is_mixed: false } => 5 + 1,
113 _ => 7 + 1,
114 };
115
116 channel.region1_start = SFB_LONG_BANDS[header.sample_rate_idx][region0_count];
117 }
118 // If MPEG version 1, OR the block type is Short...
119 else if header.is_mpeg1() || block_type_enc == 0b10 {
120 // For MPEG1 with transitional LONG blocks, the first 8 LONG scale-factor bands are used
121 // for region0. These bands are always [4, 4, 4, 4, 4, 4, 6, 6, ...] regardless of
122 // sample rate. These bands sum to 36 samples.
123 //
124 // For MPEG1 with SHORT blocks, the first 9 SHORT scale-factor bands are used for
125 // region0. These band are always [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless of sample
126 // rate. These bands also sum to 36 samples.
127 //
128 // Finally, for MPEG2 with SHORT blocks, the first 9 short scale-factor bands are used
129 // for region0. These bands are also always [4, 4, 4, 4, 4, 4, 4, 4, 4, ...] regardless
130 // of sample and thus sum to 36 samples.
131 //
132 // In all cases, the region0_count is 36.
133 //
134 // TODO: This is not accurate for MPEG2.5 at 8kHz.
135 channel.region1_start = 36;
136 }
137 // If MPEG version 2 AND the block type is not Short...
138 else {
139 // For MPEG2 and transitional LONG blocks, the first 8 LONG scale-factor bands are used
140 // for region0. These bands are always [6, 6, 6, 6, 6, 6, 8, 10, ...] regardless of
141 // sample rate. These bands sum to 54.
142 channel.region1_start = 54;
143 }
144
145 // The second region, region1, spans the remaining samples. Therefore the third region,
146 // region2, isn't used.
147 channel.region2_start = 576;
148 }
149 else {
150 // If window switching is not used, the block type is always Long.
151 channel.block_type = BlockType::Long;
152
153 for i in 0..3 {
154 channel.table_select[i] = bs.read_bits_leq32(5)? as u8;
155 }
156
157 // When window switching is not used, only LONG scale-factor bands are used for each region.
158 // The number of bands in region0 and region1 are defined in side_info. The stored value is
159 // 1 less than the actual value.
160 let region0_count = bs.read_bits_leq32(4)? as usize + 1;
161 let region0_1_count = bs.read_bits_leq32(3)? as usize + region0_count + 1;
162
163 channel.region1_start = SFB_LONG_BANDS[header.sample_rate_idx][region0_count];
164
165 // The count in region0_1_count may exceed the last band (22) in the LONG bands table.
166 // Protect against this.
167 channel.region2_start = match region0_1_count {
168 0..=22 => SFB_LONG_BANDS[header.sample_rate_idx][region0_1_count],
169 _ => 576,
170 };
171 }
172
173 // For MPEG2, preflag is determined implicitly when reading the scale factors.
174 channel.preflag = if header.is_mpeg1() { bs.read_bool()? } else { false };
175
176 channel.scalefac_scale = bs.read_bool()?;
177 channel.count1table_select = bs.read_bit()? as u8;
178
179 Ok(())
180}
181
182/// Reads the side_info for all channels in a granule from a `BitStream`.
183fn read_granule_side_info<B: ReadBitsLtr>(
184 bs: &mut B,
185 granule: &mut Granule,
186 header: &FrameHeader,
187) -> Result<()> {
188 // Read the side_info for each channel in the granule.
189 for channel in &mut granule.channels[..header.channel_mode.count()] {
190 read_granule_channel_side_info(bs, channel, header)?;
191 }
192 Ok(())
193}
194
195/// Reads the side_info of a MPEG audio frame from a `BitStream` into `FrameData`.
196pub(super) fn read_side_info<B: ReadBitsLtr>(
197 bs: &mut B,
198 header: &FrameHeader,
199 frame_data: &mut FrameData,
200) -> Result<usize> {
201 // For MPEG version 1...
202 if header.is_mpeg1() {
203 // First 9 bits is main_data_begin.
204 frame_data.main_data_begin = bs.read_bits_leq32(9)? as u16;
205
206 // Next 3 (>1 channel) or 5 (1 channel) bits are private and should be ignored.
207 match header.channel_mode {
208 ChannelMode::Mono => bs.ignore_bits(5)?,
209 _ => bs.ignore_bits(3)?,
210 };
211
212 // Next four (or 8, if more than one channel) are the SCFSI bits.
213 for scfsi in &mut frame_data.scfsi[..header.n_channels()] {
214 for band in scfsi.iter_mut() {
215 *band = bs.read_bool()?;
216 }
217 }
218 }
219 // For MPEG version 2...
220 else {
221 // First 8 bits is main_data_begin.
222 frame_data.main_data_begin = bs.read_bits_leq32(8)? as u16;
223
224 // Next 1 (1 channel) or 2 (>1 channel) bits are private and should be ignored.
225 match header.channel_mode {
226 ChannelMode::Mono => bs.ignore_bits(1)?,
227 _ => bs.ignore_bits(2)?,
228 }
229 }
230
231 // Read the side_info for each granule.
232 for granule in frame_data.granules_mut(header.version) {
233 read_granule_side_info(bs, granule, header)?;
234 }
235
236 Ok(header.side_info_len())
237}
238
239/// Reads the scale factors for a single channel in a granule in a MPEG version 1 audio frame.
240pub(super) fn read_scale_factors_mpeg1<B: ReadBitsLtr>(
241 bs: &mut B,
242 gr: usize,
243 ch: usize,
244 frame_data: &mut FrameData,
245) -> Result<u32> {
246 let mut bits_read = 0;
247
248 let channel = &mut frame_data.granules[gr].channels[ch];
249
250 // For MPEG1, scalefac_compress is a 4-bit index into a scale factor bit length lookup table.
251 let (slen1, slen2) = SCALE_FACTOR_SLEN[channel.scalefac_compress as usize];
252
253 // Short or Mixed windows...
254 if let BlockType::Short { is_mixed } = channel.block_type {
255 // If the block is mixed, there are three total scale factor partitions. The first is a long
256 // scale factor partition for bands 0..8 (scalefacs[0..8] with each scale factor being slen1
257 // bits long. Following this is a short scale factor partition covering bands 8..11 with a
258 // window of 3 (scalefacs[8..17]) and each scale factoring being slen1 bits long.
259 //
260 // If a block is not mixed, then there are a total of two scale factor partitions. The first
261 // is a short scale factor partition for bands 0..6 with a window length of 3
262 // (scalefacs[0..18]) and each scale factor being slen1 bits long.
263 let n_sfb = if is_mixed { 8 + 3 * 3 } else { 6 * 3 };
264
265 if slen1 > 0 {
266 for sfb in 0..n_sfb {
267 channel.scalefacs[sfb] = bs.read_bits_leq32(slen1)? as u8;
268 }
269 bits_read += n_sfb * slen1 as usize;
270 }
271
272 // The final scale factor partition is always a a short scale factor window. It covers bands
273 // 11..17 (scalefacs[17..35]) if the block is mixed, or bands 6..12 (scalefacs[18..36]) if
274 // not. Each band has a window of 3 with each scale factor being slen2 bits long.
275 if slen2 > 0 {
276 for sfb in n_sfb..(n_sfb + (6 * 3)) {
277 channel.scalefacs[sfb] = bs.read_bits_leq32(slen2)? as u8;
278 }
279 bits_read += 6 * 3 * slen2 as usize;
280 }
281 }
282 // Normal (long, start, end) windows...
283 else {
284 // For normal windows there are 21 scale factor bands. These bands are divivided into four
285 // band ranges. Scale factors in the first two band ranges: [0..6], [6..11], have scale
286 // factors that are slen1 bits long, while the last two band ranges: [11..16], [16..21] have
287 // scale factors that are slen2 bits long.
288 const SCALE_FACTOR_BANDS: [(usize, usize); 4] = [(0, 6), (6, 11), (11, 16), (16, 21)];
289
290 for (i, (start, end)) in SCALE_FACTOR_BANDS.iter().enumerate() {
291 let slen = if i < 2 { slen1 } else { slen2 };
292
293 // If this is the second granule, and the scale factor selection information for this
294 // channel indicates that the scale factors should be copied from the first granule,
295 // do so.
296 if gr > 0 && frame_data.scfsi[ch][i] {
297 let (granule0, granule1) = frame_data.granules.split_first_mut().unwrap();
298
299 granule1[0].channels[ch].scalefacs[*start..*end]
300 .copy_from_slice(&granule0.channels[ch].scalefacs[*start..*end]);
301 }
302 // Otherwise, read the scale factors from the bitstream. Since scale factors are already
303 // zeroed out by default, don't do anything if slen is 0.
304 else if slen > 0 {
305 for sfb in *start..*end {
306 frame_data.granules[gr].channels[ch].scalefacs[sfb] =
307 bs.read_bits_leq32(slen)? as u8;
308 }
309 bits_read += slen as usize * (end - start);
310 }
311 }
312 }
313
314 Ok(bits_read as u32)
315}
316
317/// Reads the scale factors for a single channel in a granule in a MPEG version 2 audio frame.
318pub(super) fn read_scale_factors_mpeg2<B: ReadBitsLtr>(
319 bs: &mut B,
320 is_intensity_stereo: bool,
321 channel: &mut GranuleChannel,
322) -> Result<u32> {
323 let mut bits_read = 0;
324
325 let block_index = match channel.block_type {
326 BlockType::Short { is_mixed: true } => 2,
327 BlockType::Short { is_mixed: false } => 1,
328 _ => 0,
329 };
330
331 let (slen_table, nsfb_table) = if is_intensity_stereo {
332 // The actual value of scalefac_compress is a 9-bit unsigned integer (0..512) for MPEG2. A
333 // left shift reduces it to an 8-bit value (0..256).
334 let sfc = u32::from(channel.scalefac_compress) >> 1;
335
336 match sfc {
337 0..=179 => (
338 [
339 (sfc / 36), //
340 (sfc % 36) / 6, //
341 (sfc % 36) % 6, //
342 0, //
343 ],
344 &SCALE_FACTOR_MPEG2_NSFB[0][block_index],
345 ),
346 180..=243 => (
347 [
348 ((sfc - 180) % 64) >> 4, //
349 ((sfc - 180) % 16) >> 2, //
350 ((sfc - 180) % 4), //
351 0, //
352 ],
353 &SCALE_FACTOR_MPEG2_NSFB[1][block_index],
354 ),
355 244..=255 => (
356 [
357 (sfc - 244) / 3, //
358 (sfc - 244) % 3, //
359 0, //
360 0, //
361 ],
362 &SCALE_FACTOR_MPEG2_NSFB[2][block_index],
363 ),
364 _ => unreachable!(),
365 }
366 }
367 else {
368 // The actual value of scalefac_compress is a 9-bit unsigned integer (0..512) for MPEG2.
369 let sfc = u32::from(channel.scalefac_compress);
370
371 // Preflag is set only if scalefac_compress >= 500 and this is not the intensity stereo
372 // channel. See ISO/IEC 13818-3 section 2.4.3.4.
373 channel.preflag = sfc >= 500;
374
375 match sfc {
376 0..=399 => (
377 [
378 (sfc >> 4) / 5, //
379 (sfc >> 4) % 5, //
380 (sfc % 16) >> 2, //
381 (sfc % 4), //
382 ],
383 &SCALE_FACTOR_MPEG2_NSFB[3][block_index],
384 ),
385 400..=499 => (
386 [
387 ((sfc - 400) >> 2) / 5, //
388 ((sfc - 400) >> 2) % 5, //
389 (sfc - 400) % 4, //
390 0, //
391 ],
392 &SCALE_FACTOR_MPEG2_NSFB[4][block_index],
393 ),
394 500..=512 => (
395 [
396 (sfc - 500) / 3, //
397 (sfc - 500) % 3, //
398 0, //
399 0, //
400 ],
401 &SCALE_FACTOR_MPEG2_NSFB[5][block_index],
402 ),
403 _ => unreachable!(),
404 }
405 };
406
407 let mut start = 0;
408
409 for (&slen, &n_sfb) in slen_table.iter().zip(nsfb_table.iter()) {
410 // If slen > 0, read n_sfb scale factors with each scale factor being slen bits long. If
411 // slen == 0, but n_sfb > 0, then the those scale factors should be set to 0. Since all
412 // scalefacs are preinitialized to 0, this process may be skipped.
413 if slen > 0 {
414 for sfb in start..(start + n_sfb) {
415 channel.scalefacs[sfb] = bs.read_bits_leq32(slen)? as u8;
416 }
417 bits_read += slen * n_sfb as u32;
418 }
419
420 start += n_sfb;
421 }
422
423 Ok(bits_read)
424}