symphonia_bundle_mp3/layer3/
mod.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 std::fmt;
9
10use symphonia_core::audio::{AudioBuffer, Signal};
11use symphonia_core::errors::{decode_error, Error, Result};
12use symphonia_core::io::{BitReaderLtr, BufReader, ReadBitsLtr, ReadBytes};
13
14mod bitstream;
15mod codebooks;
16mod common;
17mod hybrid_synthesis;
18mod requantize;
19mod stereo;
20
21use crate::{common::*, synthesis};
22
23use common::BlockType;
24
25use log::warn;
26
27/// `BitResevoir` implements the bit resevoir mechanism for main_data. Since frames have a
28/// deterministic length based on the bit-rate, low-complexity portions of the audio may not need
29/// every byte allocated to the frame. The bit resevoir mechanism allows these unused portions of
30/// frames to be used by future frames.
31pub struct BitResevoir {
32    buf: Box<[u8]>,
33    len: usize,
34    consumed: usize,
35}
36
37impl BitResevoir {
38    pub fn new() -> Self {
39        BitResevoir { buf: vec![0u8; 2048].into_boxed_slice(), len: 0, consumed: 0 }
40    }
41
42    pub fn fill(&mut self, pkt_main_data: &[u8], main_data_begin: usize) -> Result<u32> {
43        let main_data_len = pkt_main_data.len();
44
45        // The value `main_data_begin` indicates the number of bytes from the previous frame(s) to
46        // reuse. It must always be less than or equal to maximum amount of bytes the resevoir can
47        // hold taking into account the additional data being added to the resevoir.
48        let main_data_end = main_data_begin + main_data_len;
49
50        if main_data_end > self.buf.len() {
51            return decode_error("mpa: invalid main_data length, will exceed resevoir buffer");
52        }
53
54        let unread = self.len - self.consumed;
55
56        // If the offset is less-than or equal to the amount of unread data in the resevoir, shift
57        // the re-used bytes to the beginning of the resevoir, then copy the main data of the
58        // current packet into the resevoir.
59        let underflow = if main_data_begin <= unread {
60            // Shift all the re-used bytes as indicated by main_data_begin to the front of the
61            // resevoir.
62            self.buf.copy_within(self.len - main_data_begin..self.len, 0);
63
64            // Copy the new main data from the packet buffer after the re-used bytes.
65            self.buf[main_data_begin..main_data_end].copy_from_slice(pkt_main_data);
66            self.len = main_data_end;
67
68            0
69        }
70        else {
71            // Shift all the unread bytes to the front of the resevoir. Since this is an underflow
72            // condition, all unread bytes will be unconditionally reused.
73            self.buf.copy_within(self.len - unread..self.len, 0);
74
75            // If the offset is greater than the amount of data in the resevoir, then the stream is
76            // malformed. This can occur if the decoder is starting in the middle of a stream. This
77            // is particularly common with online radio streams. In this case, copy the main data
78            // of the current packet into the resevoir, then return the number of bytes that are
79            // missing.
80            self.buf[unread..unread + main_data_len].copy_from_slice(pkt_main_data);
81            self.len = unread + main_data_len;
82
83            // The number of bytes that will be missing.
84            let underflow = (main_data_begin - unread) as u32;
85
86            warn!("mpa: invalid main_data_begin, underflow by {} bytes", underflow);
87
88            underflow
89        };
90
91        self.consumed = 0;
92
93        Ok(underflow)
94    }
95
96    pub fn consume(&mut self, len: usize) {
97        self.consumed = self.len.min(self.consumed + len);
98    }
99
100    pub fn bytes_ref(&self) -> &[u8] {
101        &self.buf[self.consumed..self.len]
102    }
103
104    pub fn clear(&mut self) {
105        self.len = 0;
106        self.consumed = 0;
107    }
108}
109
110/// `FrameData` contains the side_info and main_data portions of a MPEG audio frame.
111#[derive(Default, Debug)]
112struct FrameData {
113    /// The byte offset into the bit resevoir indicating the location of the first bit of main_data.
114    /// If 0, main_data begins after the side_info of this frame.
115    main_data_begin: u16,
116    /// Scale factor selector information, per channel. Each channel has 4 groups of bands that may
117    /// be scaled in each granule. Scale factors may optionally be used by both granules to save
118    /// bits. Bands that share scale factors for both granules are indicated by a true. Otherwise,
119    /// each granule must store its own set of scale factors.
120    ///
121    /// Mapping of array indicies to bands [0..6, 6..11, 11..16, 16..21].
122    scfsi: [[bool; 4]; 2],
123    /// The granules.
124    granules: [Granule; 2],
125}
126
127impl FrameData {
128    /// Get a mutable slice to the granule(s) in side_info. For MPEG1, a slice of 2 granules are
129    /// returned. For MPEG2/2.5, a single granule slice is returned.
130    #[inline(always)]
131    fn granules_mut(&mut self, version: MpegVersion) -> &mut [Granule] {
132        match version {
133            MpegVersion::Mpeg1 => &mut self.granules[..2],
134            _ => &mut self.granules[..1],
135        }
136    }
137}
138
139#[derive(Default, Debug)]
140struct Granule {
141    /// Channels in the granule.
142    channels: [GranuleChannel; 2],
143}
144
145struct GranuleChannel {
146    /// Total number of bits used for scale factors (part2) and Huffman encoded data (part3).
147    part2_3_length: u16,
148    /// HALF the number of samples in the big_values partition (sum of all samples in
149    /// `region[0..3]`).
150    big_values: u16,
151    /// Logarithmic quantization step size.
152    global_gain: u8,
153    /// Depending on the MPEG version, `scalefac_compress` determines how many bits are allocated
154    /// per scale factor.
155    ///
156    /// - For MPEG1 bitstreams, `scalefac_compress` is a 4-bit index into
157    ///  `SCALE_FACTOR_SLEN[0..16]` to obtain a number of bits per scale factor pair.
158    ///
159    /// - For MPEG2/2.5 bitstreams, `scalefac_compress` is a 9-bit value that decodes into
160    /// `slen[0..3]` (referred to as slen1-4 in the standard) for the number of bits per scale
161    /// factor, and depending on which range the value falls into, for which bands.
162    scalefac_compress: u16,
163    /// Indicates the block type (type of window) for the channel in the granule.
164    block_type: BlockType,
165    /// Gain factors for region[0..3] in big_values. Each gain factor has a maximum value of 7
166    /// (3 bits).
167    subblock_gain: [u8; 3],
168    /// The Huffman table to use for decoding `region[0..3]` of big_values.
169    table_select: [u8; 3],
170    /// The index of the first sample in region1 of big_values.
171    region1_start: usize,
172    /// The index of the first sample in region2 of big_values.
173    region2_start: usize,
174    /// Indicates if the pre-emphasis amount for each scale factor band should be added on to each
175    /// scale factor before requantization.
176    preflag: bool,
177    /// A 0.5x (false) or 1x (true) multiplier for scale factors.
178    scalefac_scale: bool,
179    /// Use Huffman Quads table A (0) or B (1), for decoding the count1 partition.
180    count1table_select: u8,
181    /// Long (scalefac_l) and short (scalefac_s) window scale factor bands. Must be interpreted
182    /// based on the block type of the granule.
183    ///
184    /// For `block_type == BlockType::Short { is_mixed: false }`:
185    ///   - `scalefac_s[0..36]` -> `scalefacs[0..36]`
186    ///
187    /// For `block_type == BlockType::Short { is_mixed: true }`:
188    ///   - `scalefac_l[0..8]`  -> `scalefacs[0..8]`
189    ///   - `scalefac_s[0..27]` -> `scalefacs[8..35]`
190    ///
191    /// For `block_type != BlockType::Short { .. }`:
192    ///   - `scalefac_l[0..21]` -> `scalefacs[0..21]`
193    ///
194    /// Note: The standard doesn't explicitly call it out, but for Short blocks, there are three
195    ///       additional scale factors, `scalefacs[36..39]`, that are always 0 and are not
196    ///       transmitted in the bitstream.
197    ///
198    /// For MPEG1, and MPEG2 without intensity stereo coding, a scale factor will not exceed 4 bits
199    /// in length (maximum value 15). For MPEG2 with intensity stereo, a scale factor will not
200    /// exceed 5 bits (maximum value 31) in length.
201    scalefacs: [u8; 39],
202    /// The starting sample index of the rzero partition, or the count of big_values and count1
203    /// samples.
204    rzero: usize,
205}
206
207impl Default for GranuleChannel {
208    fn default() -> Self {
209        GranuleChannel {
210            part2_3_length: 0,
211            big_values: 0,
212            global_gain: 0,
213            scalefac_compress: 0,
214            block_type: BlockType::Long,
215            subblock_gain: [0; 3],
216            table_select: [0; 3],
217            region1_start: 0,
218            region2_start: 0,
219            preflag: false,
220            scalefac_scale: false,
221            count1table_select: 0,
222            scalefacs: [0; 39],
223            rzero: 0,
224        }
225    }
226}
227
228impl fmt::Debug for GranuleChannel {
229    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230        writeln!(f, "GranuleChannel {{")?;
231        writeln!(f, "\tpart2_3_length={}", self.part2_3_length)?;
232        writeln!(f, "\tbig_values={}", self.big_values)?;
233        writeln!(f, "\tglobal_gain={}", self.global_gain)?;
234        writeln!(f, "\tscalefac_compress={}", self.scalefac_compress)?;
235        writeln!(f, "\tblock_type={:?}", self.block_type)?;
236        writeln!(f, "\tsubblock_gain={:?}", self.subblock_gain)?;
237        writeln!(f, "\ttable_select={:?}", self.table_select)?;
238        writeln!(f, "\tregion1_start={}", self.region1_start)?;
239        writeln!(f, "\tregion2_start={}", self.region2_start)?;
240        writeln!(f, "\tpreflag={}", self.preflag)?;
241        writeln!(f, "\tscalefac_scale={}", self.scalefac_scale)?;
242        writeln!(f, "\tcount1table_select={}", self.count1table_select)?;
243
244        write!(f, "\tscalefacs=[ ")?;
245        for sf in &self.scalefacs[..] {
246            write!(f, "{}, ", sf)?;
247        }
248        writeln!(f, "]")?;
249        writeln!(f, "\trzero={}", self.rzero)?;
250        writeln!(f, "}}")
251    }
252}
253
254pub struct Layer3 {
255    pub samples: [[[f32; 576]; 2]; 2],
256    pub overlap: [[[f32; 18]; 32]; 2],
257    pub synthesis: [synthesis::SynthesisState; 2],
258    pub resevoir: BitResevoir,
259}
260
261impl Layer3 {
262    pub fn new() -> Self {
263        Self {
264            samples: [[[0f32; 576]; 2]; 2],
265            overlap: [[[0f32; 18]; 32]; 2],
266            synthesis: Default::default(),
267            resevoir: BitResevoir::new(),
268        }
269    }
270
271    /// Reads the main_data portion of a MPEG audio frame from a `BitStream` into `FrameData`.
272    fn read_main_data(
273        &mut self,
274        header: &FrameHeader,
275        underflow_bits: u32,
276        frame_data: &mut FrameData,
277    ) -> Result<usize> {
278        let main_data = self.resevoir.bytes_ref();
279        let mut part2_3_begin = 0;
280        let mut part2_3_skipped = 0;
281
282        for gr in 0..header.n_granules() {
283            // If the resevoir underflowed (i.e., main_data_begin references bits not present in the
284            // resevoir) then skip the granule(s) the missing bits would belong to.
285            if part2_3_skipped < underflow_bits {
286                // Zero the samples in the granule channel(s) and sum the part2/3 bits that were
287                // skipped.
288                for ch in 0..header.n_channels() {
289                    requantize::zero(&mut self.samples[gr][ch]);
290                    part2_3_skipped +=
291                        u32::from(frame_data.granules[gr].channels[ch].part2_3_length);
292                }
293
294                // Adjust the start position of the next granule in the buffer of main data that is
295                // available.
296                if part2_3_skipped > underflow_bits {
297                    part2_3_begin = (part2_3_skipped - underflow_bits) as usize;
298                }
299
300                // Continue at the next granule.
301                continue;
302            }
303
304            for ch in 0..header.n_channels() {
305                let byte_index = part2_3_begin >> 3;
306
307                // Create a bit reader at the expected starting bit position.
308                let mut bs = if byte_index < main_data.len() {
309                    let mut bs = BitReaderLtr::new(&main_data[byte_index..]);
310
311                    let bit_index = part2_3_begin & 0x7;
312
313                    if bit_index > 0 {
314                        bs.ignore_bits(bit_index as u32)?;
315                    }
316
317                    bs
318                }
319                else {
320                    return decode_error("mpa: invalid main_data offset");
321                };
322
323                // Read the scale factors (part2) and get the number of bits read.
324                let part2_len = if header.is_mpeg1() {
325                    bitstream::read_scale_factors_mpeg1(&mut bs, gr, ch, frame_data)
326                }
327                else {
328                    bitstream::read_scale_factors_mpeg2(
329                        &mut bs,
330                        ch > 0 && header.is_intensity_stereo(),
331                        &mut frame_data.granules[gr].channels[ch],
332                    )
333                }?;
334
335                let part2_3_length = u32::from(frame_data.granules[gr].channels[ch].part2_3_length);
336
337                // The part2 length must be less than or equal to the part2_3_length.
338                if part2_len > part2_3_length {
339                    return decode_error("mpa: part2_3_length is not valid");
340                }
341
342                // The Huffman code length (part3).
343                let part3_len = part2_3_length - part2_len;
344
345                // Decode the Huffman coded spectral samples and get the starting index of the rzero
346                // partition.
347                let huffman_result = requantize::read_huffman_samples(
348                    &mut bs,
349                    &frame_data.granules[gr].channels[ch],
350                    part3_len,
351                    &mut self.samples[gr][ch],
352                );
353
354                // Huffman decoding errors are returned as an IO error by the bit reader. IO errors
355                // are unrecoverable, which is not the case for huffman decoding errors. Convert the
356                // IO error to a decode error.
357                frame_data.granules[gr].channels[ch].rzero = match huffman_result {
358                    Ok(rzero) => rzero,
359                    Err(Error::IoError(e)) if e.kind() == std::io::ErrorKind::Other => {
360                        return decode_error("mpa: huffman decode overrun");
361                    }
362                    Err(err) => return Err(err),
363                };
364
365                part2_3_begin += part2_3_length as usize;
366            }
367        }
368
369        Ok((part2_3_begin + 7) >> 3)
370    }
371}
372
373impl Layer for Layer3 {
374    fn decode(
375        &mut self,
376        reader: &mut BufReader<'_>,
377        header: &FrameHeader,
378        out: &mut AudioBuffer<f32>,
379    ) -> Result<()> {
380        // Initialize an empty FrameData to store the side_info and main_data portions of the
381        // frame.
382        let mut frame_data: FrameData = Default::default();
383
384        let _crc = if header.has_crc { Some(reader.read_be_u16()?) } else { None };
385
386        let buf = reader.read_buf_bytes_available_ref();
387
388        let mut bs = BitReaderLtr::new(buf);
389
390        // Read side_info into the frame data.
391        // TODO: Use a MonitorStream to compute the CRC.
392        let side_info_len = match bitstream::read_side_info(&mut bs, header, &mut frame_data) {
393            Ok(len) => len,
394            Err(e) => {
395                // A failure in reading this packet will cause a discontinuity in the codec
396                // bitstream. Therefore, clear the bit reservoir since it will not be valid for the
397                // next packet.
398                self.resevoir.clear();
399                return Err(e);
400            }
401        };
402
403        // Buffer main data into the bit resevoir.
404        let underflow =
405            self.resevoir.fill(&buf[side_info_len..], frame_data.main_data_begin as usize)?;
406
407        // Read the main data (scale factors and spectral samples).
408        match self.read_main_data(header, 8 * underflow, &mut frame_data) {
409            Ok(len) => {
410                // Consume the bytes of main data read from the resevoir.
411                self.resevoir.consume(len);
412            }
413            Err(e) => {
414                // The bit reservoir was likely filled with invalid data. Clear it for the next
415                // packet.
416                self.resevoir.clear();
417                return Err(e);
418            }
419        }
420
421        for gr in 0..header.n_granules() {
422            let granule = &mut frame_data.granules[gr];
423
424            // Requantize all non-zero (big_values and count1 partition) spectral samples.
425            requantize::requantize(header, &granule.channels[0], &mut self.samples[gr][0]);
426
427            // If there is a second channel...
428            if header.channel_mode != ChannelMode::Mono {
429                // Requantize all non-zero spectral samples in the second channel.
430                requantize::requantize(header, &granule.channels[1], &mut self.samples[gr][1]);
431
432                // Apply joint stereo processing if it is used.
433                stereo::stereo(header, granule, &mut self.samples[gr])?;
434            }
435
436            // Each granule will yield 576 samples. After reserving frames, all steps must be
437            // infalliable.
438            out.render_reserved(Some(576));
439
440            // The next steps are independant of channel count.
441            for ch in 0..header.n_channels() {
442                // Reorder the spectral samples in short blocks into sub-band order.
443                hybrid_synthesis::reorder(
444                    header,
445                    &mut granule.channels[ch],
446                    &mut self.samples[gr][ch],
447                );
448
449                // Apply the anti-aliasing filter to all block types other than short.
450                hybrid_synthesis::antialias(&mut granule.channels[ch], &mut self.samples[gr][ch]);
451
452                // Perform hybrid-synthesis (IMDCT and windowing). After this step, rzero is invalid
453                // due to the overlap-add operation.
454                hybrid_synthesis::hybrid_synthesis(
455                    &granule.channels[ch],
456                    &mut self.overlap[ch],
457                    &mut self.samples[gr][ch],
458                );
459
460                // Invert every second sample in every second sub-band to negate the frequency
461                // inversion of the polyphase filterbank.
462                hybrid_synthesis::frequency_inversion(&mut self.samples[gr][ch]);
463
464                // Perform polyphase synthesis and generate PCM samples.
465                let out_ch_samples = out.chan_mut(ch);
466
467                synthesis::synthesis(
468                    &mut self.synthesis[ch],
469                    18,
470                    &self.samples[gr][ch],
471                    &mut out_ch_samples[(gr * 576)..((gr + 1) * 576)],
472                );
473            }
474        }
475
476        Ok(())
477    }
478}