symphonia_codec_pcm/
lib.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
8#![warn(rust_2018_idioms)]
9#![forbid(unsafe_code)]
10// The following lints are allowed in all Symphonia crates. Please see clippy.toml for their
11// justification.
12#![allow(clippy::comparison_chain)]
13#![allow(clippy::excessive_precision)]
14#![allow(clippy::identity_op)]
15#![allow(clippy::manual_range_contains)]
16
17use symphonia_core::support_codec;
18
19use symphonia_core::audio::{AsAudioBufferRef, AudioBuffer, AudioBufferRef, Signal, SignalSpec};
20use symphonia_core::codecs::{CodecDescriptor, CodecParameters, CodecType};
21use symphonia_core::codecs::{Decoder, DecoderOptions, FinalizeResult};
22// Signed Int PCM codecs
23use symphonia_core::codecs::{CODEC_TYPE_PCM_S16BE, CODEC_TYPE_PCM_S24BE, CODEC_TYPE_PCM_S32BE};
24use symphonia_core::codecs::{CODEC_TYPE_PCM_S16LE, CODEC_TYPE_PCM_S8};
25use symphonia_core::codecs::{CODEC_TYPE_PCM_S24LE, CODEC_TYPE_PCM_S32LE};
26// Unsigned Int PCM codecs
27use symphonia_core::codecs::{CODEC_TYPE_PCM_U16BE, CODEC_TYPE_PCM_U24BE, CODEC_TYPE_PCM_U32BE};
28use symphonia_core::codecs::{CODEC_TYPE_PCM_U16LE, CODEC_TYPE_PCM_U8};
29use symphonia_core::codecs::{CODEC_TYPE_PCM_U24LE, CODEC_TYPE_PCM_U32LE};
30// Floating point PCM codecs
31use symphonia_core::codecs::{CODEC_TYPE_PCM_F32BE, CODEC_TYPE_PCM_F32LE};
32use symphonia_core::codecs::{CODEC_TYPE_PCM_F64BE, CODEC_TYPE_PCM_F64LE};
33// G711 ALaw and MuLaw PCM codecs
34use symphonia_core::codecs::{CODEC_TYPE_PCM_ALAW, CODEC_TYPE_PCM_MULAW};
35use symphonia_core::conv::IntoSample;
36use symphonia_core::errors::{decode_error, unsupported_error, Result};
37use symphonia_core::formats::Packet;
38use symphonia_core::io::ReadBytes;
39use symphonia_core::sample::{i24, u24, SampleFormat};
40use symphonia_core::units::Duration;
41
42macro_rules! impl_generic_audio_buffer_func {
43    ($generic:expr, $buf:ident, $expr:expr) => {
44        match $generic {
45            GenericAudioBuffer::U8($buf) => $expr,
46            GenericAudioBuffer::U16($buf) => $expr,
47            GenericAudioBuffer::U24($buf) => $expr,
48            GenericAudioBuffer::U32($buf) => $expr,
49            GenericAudioBuffer::S8($buf) => $expr,
50            GenericAudioBuffer::S16($buf) => $expr,
51            GenericAudioBuffer::S24($buf) => $expr,
52            GenericAudioBuffer::S32($buf) => $expr,
53            GenericAudioBuffer::F32($buf) => $expr,
54            GenericAudioBuffer::F64($buf) => $expr,
55        }
56    };
57}
58
59/// A generic audio buffer.
60/// TODO: Move to core library with a full API.
61pub enum GenericAudioBuffer {
62    U8(AudioBuffer<u8>),
63    U16(AudioBuffer<u16>),
64    U24(AudioBuffer<u24>),
65    U32(AudioBuffer<u32>),
66    S8(AudioBuffer<i8>),
67    S16(AudioBuffer<i16>),
68    S24(AudioBuffer<i24>),
69    S32(AudioBuffer<i32>),
70    F32(AudioBuffer<f32>),
71    F64(AudioBuffer<f64>),
72}
73
74impl GenericAudioBuffer {
75    fn new(format: SampleFormat, duration: Duration, spec: SignalSpec) -> Self {
76        match format {
77            SampleFormat::U8 => GenericAudioBuffer::U8(AudioBuffer::new(duration, spec)),
78            SampleFormat::U16 => GenericAudioBuffer::U16(AudioBuffer::new(duration, spec)),
79            SampleFormat::U24 => GenericAudioBuffer::U24(AudioBuffer::new(duration, spec)),
80            SampleFormat::U32 => GenericAudioBuffer::U32(AudioBuffer::new(duration, spec)),
81            SampleFormat::S8 => GenericAudioBuffer::S8(AudioBuffer::new(duration, spec)),
82            SampleFormat::S16 => GenericAudioBuffer::S16(AudioBuffer::new(duration, spec)),
83            SampleFormat::S24 => GenericAudioBuffer::S24(AudioBuffer::new(duration, spec)),
84            SampleFormat::S32 => GenericAudioBuffer::S32(AudioBuffer::new(duration, spec)),
85            SampleFormat::F32 => GenericAudioBuffer::F32(AudioBuffer::new(duration, spec)),
86            SampleFormat::F64 => GenericAudioBuffer::F64(AudioBuffer::new(duration, spec)),
87        }
88    }
89
90    fn clear(&mut self) {
91        impl_generic_audio_buffer_func!(self, buf, buf.clear());
92    }
93}
94
95impl AsAudioBufferRef for GenericAudioBuffer {
96    fn as_audio_buffer_ref(&self) -> AudioBufferRef<'_> {
97        impl_generic_audio_buffer_func!(self, buf, buf.as_audio_buffer_ref())
98    }
99}
100
101macro_rules! read_pcm_signed {
102    ($buf:expr, $fmt:tt, $read:expr, $width:expr, $coded_width:expr) => {
103        // Get buffer of the correct sample format.
104        match $buf {
105            GenericAudioBuffer::$fmt(ref mut buf) => {
106                // Read samples.
107                let shift = $width - $coded_width;
108                buf.fill(|audio_planes, idx| -> Result<()> {
109                    for plane in audio_planes.planes() {
110                        plane[idx] = ($read << shift).into_sample();
111                    }
112                    Ok(())
113                })
114            }
115            _ => unreachable!(),
116        }
117    };
118}
119
120macro_rules! read_pcm_unsigned {
121    ($buf:expr, $fmt:tt, $read:expr, $width:expr, $coded_width:expr) => {
122        // Get buffer of the correct sample format.
123        match $buf {
124            GenericAudioBuffer::$fmt(ref mut buf) => {
125                // Read samples.
126                let shift = $width - $coded_width;
127                buf.fill(|audio_planes, idx| -> Result<()> {
128                    for plane in audio_planes.planes() {
129                        plane[idx] = ($read << shift).into_sample();
130                    }
131                    Ok(())
132                })
133            }
134            _ => unreachable!(),
135        }
136    };
137}
138
139macro_rules! read_pcm_floating {
140    ($buf:expr, $fmt:tt, $read:expr) => {
141        // Get buffer of the correct sample format.
142        match $buf {
143            GenericAudioBuffer::$fmt(ref mut buf) => {
144                // Read samples.
145                buf.fill(|audio_planes, idx| -> Result<()> {
146                    for plane in audio_planes.planes() {
147                        plane[idx] = $read;
148                    }
149                    Ok(())
150                })
151            }
152            _ => unreachable!(),
153        }
154    };
155}
156
157macro_rules! read_pcm_transfer_func {
158    ($buf:expr, $fmt:tt, $func:expr) => {
159        // Get buffer of the correct sample format.
160        match $buf {
161            GenericAudioBuffer::$fmt(ref mut buf) => {
162                // Read samples.
163                buf.fill(|audio_planes, idx| -> Result<()> {
164                    for plane in audio_planes.planes() {
165                        plane[idx] = $func;
166                    }
167                    Ok(())
168                })
169            }
170            _ => unreachable!(),
171        }
172    };
173}
174
175// alaw_to_linear and mulaw_to_linear are adaptations of alaw2linear and ulaw2linear from g711.c by
176// SUN Microsystems (unrestricted use license), license text for these parts follow below.
177//
178// ---
179//
180// This source code is a product of Sun Microsystems, Inc. and is provided for
181// unrestricted use.  Users may copy or modify this source code without
182// charge.
183//
184// SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
185// WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
186// PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
187//
188// Sun source code is provided with no support and without any obligation on the
189// part of Sun Microsystems, Inc. to assist in its use, correction,
190// modification or enhancement.
191//
192// SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
193// INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
194// OR ANY PART THEREOF.
195//
196// In no event will Sun Microsystems, Inc. be liable for any lost revenue
197// or profits or other special, indirect and consequential damages, even if
198// Sun has been advised of the possibility of such damages.
199//
200// Sun Microsystems, Inc.
201// 2550 Garcia Avenue
202// Mountain View, California  94043
203const XLAW_QUANT_MASK: u8 = 0x0f;
204const XLAW_SEG_MASK: u8 = 0x70;
205const XLAW_SEG_SHIFT: u32 = 4;
206
207fn alaw_to_linear(mut a_val: u8) -> i16 {
208    a_val ^= 0x55;
209
210    let mut t = i16::from((a_val & XLAW_QUANT_MASK) << 4);
211    let seg = (a_val & XLAW_SEG_MASK) >> XLAW_SEG_SHIFT;
212
213    match seg {
214        0 => t += 0x8,
215        1 => t += 0x108,
216        _ => t = (t + 0x108) << (seg - 1),
217    }
218
219    if a_val & 0x80 == 0x80 {
220        t
221    }
222    else {
223        -t
224    }
225}
226
227fn mulaw_to_linear(mut mu_val: u8) -> i16 {
228    const BIAS: i16 = 0x84;
229
230    // Complement to obtain normal u-law value.
231    mu_val = !mu_val;
232
233    // Extract and bias the quantization bits. Then shift up by the segment number and subtract out
234    // the bias.
235    let mut t = i16::from((mu_val & XLAW_QUANT_MASK) << 3) + BIAS;
236    t <<= (mu_val & XLAW_SEG_MASK) >> XLAW_SEG_SHIFT;
237
238    if mu_val & 0x80 == 0x80 {
239        BIAS - t
240    }
241    else {
242        t - BIAS
243    }
244}
245
246fn is_supported_pcm_codec(codec_type: CodecType) -> bool {
247    matches!(
248        codec_type,
249        CODEC_TYPE_PCM_S32LE
250            | CODEC_TYPE_PCM_S32BE
251            | CODEC_TYPE_PCM_S24LE
252            | CODEC_TYPE_PCM_S24BE
253            | CODEC_TYPE_PCM_S16LE
254            | CODEC_TYPE_PCM_S16BE
255            | CODEC_TYPE_PCM_S8
256            | CODEC_TYPE_PCM_U32LE
257            | CODEC_TYPE_PCM_U32BE
258            | CODEC_TYPE_PCM_U24LE
259            | CODEC_TYPE_PCM_U24BE
260            | CODEC_TYPE_PCM_U16LE
261            | CODEC_TYPE_PCM_U16BE
262            | CODEC_TYPE_PCM_U8
263            | CODEC_TYPE_PCM_F32LE
264            | CODEC_TYPE_PCM_F32BE
265            | CODEC_TYPE_PCM_F64LE
266            | CODEC_TYPE_PCM_F64BE
267            | CODEC_TYPE_PCM_ALAW
268            | CODEC_TYPE_PCM_MULAW
269    )
270}
271
272/// Pulse Code Modulation (PCM) decoder for all raw PCM, and log-PCM codecs.
273pub struct PcmDecoder {
274    params: CodecParameters,
275    coded_width: u32,
276    buf: GenericAudioBuffer,
277}
278
279impl PcmDecoder {
280    fn decode_inner(&mut self, packet: &Packet) -> Result<()> {
281        let mut reader = packet.as_buf_reader();
282
283        let _ = match self.params.codec {
284            CODEC_TYPE_PCM_S32LE => {
285                read_pcm_signed!(self.buf, S32, reader.read_i32()?, 32, self.coded_width)
286            }
287            CODEC_TYPE_PCM_S32BE => {
288                read_pcm_signed!(self.buf, S32, reader.read_be_i32()?, 32, self.coded_width)
289            }
290            CODEC_TYPE_PCM_S24LE => {
291                read_pcm_signed!(self.buf, S24, reader.read_i24()? << 8, 24, self.coded_width)
292            }
293            CODEC_TYPE_PCM_S24BE => {
294                read_pcm_signed!(self.buf, S24, reader.read_be_i24()? << 8, 24, self.coded_width)
295            }
296            CODEC_TYPE_PCM_S16LE => {
297                read_pcm_signed!(self.buf, S16, reader.read_i16()?, 16, self.coded_width)
298            }
299            CODEC_TYPE_PCM_S16BE => {
300                read_pcm_signed!(self.buf, S16, reader.read_be_i16()?, 16, self.coded_width)
301            }
302            CODEC_TYPE_PCM_S8 => {
303                read_pcm_signed!(self.buf, S8, reader.read_i8()?, 8, self.coded_width)
304            }
305            CODEC_TYPE_PCM_U32LE => {
306                read_pcm_unsigned!(self.buf, U32, reader.read_u32()?, 32, self.coded_width)
307            }
308            CODEC_TYPE_PCM_U32BE => {
309                read_pcm_unsigned!(self.buf, U32, reader.read_be_u32()?, 32, self.coded_width)
310            }
311            CODEC_TYPE_PCM_U24LE => {
312                read_pcm_unsigned!(self.buf, U24, reader.read_u24()? << 8, 24, self.coded_width)
313            }
314            CODEC_TYPE_PCM_U24BE => {
315                read_pcm_unsigned!(self.buf, U24, reader.read_be_u24()? << 8, 24, self.coded_width)
316            }
317            CODEC_TYPE_PCM_U16LE => {
318                read_pcm_unsigned!(self.buf, U16, reader.read_u16()?, 16, self.coded_width)
319            }
320            CODEC_TYPE_PCM_U16BE => {
321                read_pcm_unsigned!(self.buf, U16, reader.read_be_u16()?, 16, self.coded_width)
322            }
323            CODEC_TYPE_PCM_U8 => {
324                read_pcm_unsigned!(self.buf, U8, reader.read_u8()?, 8, self.coded_width)
325            }
326            CODEC_TYPE_PCM_F32LE => {
327                read_pcm_floating!(self.buf, F32, reader.read_f32()?)
328            }
329            CODEC_TYPE_PCM_F32BE => {
330                read_pcm_floating!(self.buf, F32, reader.read_be_f32()?)
331            }
332            CODEC_TYPE_PCM_F64LE => {
333                read_pcm_floating!(self.buf, F64, reader.read_f64()?)
334            }
335            CODEC_TYPE_PCM_F64BE => {
336                read_pcm_floating!(self.buf, F64, reader.read_be_f64()?)
337            }
338            CODEC_TYPE_PCM_ALAW => {
339                read_pcm_transfer_func!(self.buf, S16, alaw_to_linear(reader.read_u8()?))
340            }
341            CODEC_TYPE_PCM_MULAW => {
342                read_pcm_transfer_func!(self.buf, S16, mulaw_to_linear(reader.read_u8()?))
343            }
344            // CODEC_TYPE_PCM_S32LE_PLANAR =>
345            // CODEC_TYPE_PCM_S32BE_PLANAR =>
346            // CODEC_TYPE_PCM_S24LE_PLANAR =>
347            // CODEC_TYPE_PCM_S24BE_PLANAR =>
348            // CODEC_TYPE_PCM_S16LE_PLANAR =>
349            // CODEC_TYPE_PCM_S16BE_PLANAR =>
350            // CODEC_TYPE_PCM_S8_PLANAR    =>
351            // CODEC_TYPE_PCM_U32LE_PLANAR =>
352            // CODEC_TYPE_PCM_U32BE_PLANAR =>
353            // CODEC_TYPE_PCM_U24LE_PLANAR =>
354            // CODEC_TYPE_PCM_U24BE_PLANAR =>
355            // CODEC_TYPE_PCM_U16LE_PLANAR =>
356            // CODEC_TYPE_PCM_U16BE_PLANAR =>
357            // CODEC_TYPE_PCM_U8_PLANAR    =>
358            // CODEC_TYPE_PCM_F32LE_PLANAR =>
359            // CODEC_TYPE_PCM_F32BE_PLANAR =>
360            // CODEC_TYPE_PCM_F64LE_PLANAR =>
361            // CODEC_TYPE_PCM_F64BE_PLANAR =>
362            _ => unsupported_error("pcm: codec is unsupported"),
363        };
364
365        Ok(())
366    }
367}
368
369impl Decoder for PcmDecoder {
370    fn try_new(params: &CodecParameters, _options: &DecoderOptions) -> Result<Self> {
371        // This decoder only supports certain PCM codecs.
372        if !is_supported_pcm_codec(params.codec) {
373            return unsupported_error("pcm: invalid codec type");
374        }
375
376        let frames = match params.max_frames_per_packet {
377            Some(frames) => frames,
378            _ => return unsupported_error("pcm: maximum frames per packet is required"),
379        };
380
381        let rate = match params.sample_rate {
382            Some(rate) => rate,
383            _ => return unsupported_error("pcm: sample rate is required"),
384        };
385
386        let spec = if let Some(channels) = params.channels {
387            // Atleast one channel is required.
388            if channels.count() < 1 {
389                return unsupported_error("pcm: number of channels cannot be 0");
390            }
391
392            SignalSpec::new(rate, channels)
393        }
394        else if let Some(layout) = params.channel_layout {
395            SignalSpec::new_with_layout(rate, layout)
396        }
397        else {
398            return unsupported_error("pcm: channels or channel_layout is required");
399        };
400
401        // Determine the sample format for the audio buffer based on the codec type.
402        let (sample_format, sample_format_width) = match params.codec {
403            CODEC_TYPE_PCM_S32LE | CODEC_TYPE_PCM_S32BE => (SampleFormat::S32, 32),
404            CODEC_TYPE_PCM_S24LE | CODEC_TYPE_PCM_S24BE => (SampleFormat::S24, 24),
405            CODEC_TYPE_PCM_S16LE | CODEC_TYPE_PCM_S16BE => (SampleFormat::S16, 16),
406            CODEC_TYPE_PCM_S8 => (SampleFormat::S8, 8),
407            CODEC_TYPE_PCM_U32LE | CODEC_TYPE_PCM_U32BE => (SampleFormat::U32, 32),
408            CODEC_TYPE_PCM_U24LE | CODEC_TYPE_PCM_U24BE => (SampleFormat::U24, 24),
409            CODEC_TYPE_PCM_U16LE | CODEC_TYPE_PCM_U16BE => (SampleFormat::U16, 16),
410            CODEC_TYPE_PCM_U8 => (SampleFormat::U8, 8),
411            CODEC_TYPE_PCM_F32LE | CODEC_TYPE_PCM_F32BE => (SampleFormat::F32, 32),
412            CODEC_TYPE_PCM_F64LE | CODEC_TYPE_PCM_F64BE => (SampleFormat::F64, 64),
413            CODEC_TYPE_PCM_ALAW => (SampleFormat::S16, 16),
414            CODEC_TYPE_PCM_MULAW => (SampleFormat::S16, 16),
415            _ => unreachable!(),
416        };
417
418        // Signed and unsigned integer PCM codecs may code fewer bits per sample than the actual
419        // sample format. The number of coded bits is therefore required to shift the decoded
420        // samples into the range of the sample format. Try to get the bits per coded sample
421        // parameter, or, if failing that, the bits per sample parameter.
422        let coded_width =
423            params.bits_per_coded_sample.unwrap_or_else(|| params.bits_per_sample.unwrap_or(0));
424
425        // If the coded sample width is unknown, then the bits per coded sample may be constant and
426        // implicit to the codec.
427        if coded_width == 0 {
428            // A-Law, Mu-Law, and floating point codecs have an implicit coded sample bit-width. If
429            // the codec is none of those, then decoding is not possible.
430            match params.codec {
431                CODEC_TYPE_PCM_F32LE | CODEC_TYPE_PCM_F32BE => (),
432                CODEC_TYPE_PCM_F64LE | CODEC_TYPE_PCM_F64BE => (),
433                CODEC_TYPE_PCM_ALAW | CODEC_TYPE_PCM_MULAW => (),
434                _ => return unsupported_error("pcm: unknown bits per (coded) sample"),
435            }
436        }
437        else if coded_width > sample_format_width {
438            // If the coded sample width is greater than the width of the sample format, then the
439            // stream has incorrect parameters.
440            return decode_error("pcm: coded bits per sample is greater than the sample format");
441        }
442
443        // Create an audio buffer of the correct format.
444        let buf = GenericAudioBuffer::new(sample_format, frames, spec);
445
446        Ok(PcmDecoder { params: params.clone(), coded_width, buf })
447    }
448
449    fn supported_codecs() -> &'static [CodecDescriptor] {
450        &[
451            support_codec!(
452                CODEC_TYPE_PCM_S32LE,
453                "pcm_s32le",
454                "PCM Signed 32-bit Little-Endian Interleaved"
455            ),
456            support_codec!(
457                CODEC_TYPE_PCM_S32BE,
458                "pcm_s32be",
459                "PCM Signed 32-bit Big-Endian Interleaved"
460            ),
461            support_codec!(
462                CODEC_TYPE_PCM_S24LE,
463                "pcm_s24le",
464                "PCM Signed 24-bit Little-Endian Interleaved"
465            ),
466            support_codec!(
467                CODEC_TYPE_PCM_S24BE,
468                "pcm_s24be",
469                "PCM Signed 24-bit Big-Endian Interleaved"
470            ),
471            support_codec!(
472                CODEC_TYPE_PCM_S16LE,
473                "pcm_s16le",
474                "PCM Signed 16-bit Little-Endian Interleaved"
475            ),
476            support_codec!(
477                CODEC_TYPE_PCM_S16BE,
478                "pcm_s16be",
479                "PCM Signed 16-bit Big-Endian Interleaved"
480            ),
481            support_codec!(CODEC_TYPE_PCM_S8, "pcm_s8", "PCM Signed 8-bit Interleaved"),
482            support_codec!(
483                CODEC_TYPE_PCM_U32LE,
484                "pcm_u32le",
485                "PCM Unsigned 32-bit Little-Endian Interleaved"
486            ),
487            support_codec!(
488                CODEC_TYPE_PCM_U32BE,
489                "pcm_u32be",
490                "PCM Unsigned 32-bit Big-Endian Interleaved"
491            ),
492            support_codec!(
493                CODEC_TYPE_PCM_U24LE,
494                "pcm_u24le",
495                "PCM Unsigned 24-bit Little-Endian Interleaved"
496            ),
497            support_codec!(
498                CODEC_TYPE_PCM_U24BE,
499                "pcm_u24be",
500                "PCM Unsigned 24-bit Big-Endian Interleaved"
501            ),
502            support_codec!(
503                CODEC_TYPE_PCM_U16LE,
504                "pcm_u16le",
505                "PCM Unsigned 16-bit Little-Endian Interleaved"
506            ),
507            support_codec!(
508                CODEC_TYPE_PCM_U16BE,
509                "pcm_u16be",
510                "PCM Unsigned 16-bit Big-Endian Interleaved"
511            ),
512            support_codec!(CODEC_TYPE_PCM_U8, "pcm_u8", "PCM Unsigned 8-bit Interleaved"),
513            support_codec!(
514                CODEC_TYPE_PCM_F32LE,
515                "pcm_f32le",
516                "PCM 32-bit Little-Endian Floating Point Interleaved"
517            ),
518            support_codec!(
519                CODEC_TYPE_PCM_F32BE,
520                "pcm_f32be",
521                "PCM 32-bit Big-Endian Floating Point Interleaved"
522            ),
523            support_codec!(
524                CODEC_TYPE_PCM_F64LE,
525                "pcm_f64le",
526                "PCM 64-bit Little-Endian Floating Point Interleaved"
527            ),
528            support_codec!(
529                CODEC_TYPE_PCM_F64BE,
530                "pcm_f64be",
531                "PCM 64-bit Big-Endian Floating Point Interleaved"
532            ),
533            support_codec!(CODEC_TYPE_PCM_ALAW, "pcm_alaw", "PCM A-law"),
534            support_codec!(CODEC_TYPE_PCM_MULAW, "pcm_mulaw", "PCM Mu-law"),
535            // support_codec!(
536            //     CODEC_TYPE_PCM_S32LE_PLANAR,
537            //     "pcm_s32le_planar",
538            //     "PCM Signed 32-bit Little-Endian Planar"
539            // ),
540            // support_codec!(
541            //     CODEC_TYPE_PCM_S32BE_PLANAR,
542            //     "pcm_s32be_planar",
543            //     "PCM Signed 32-bit Big-Endian Planar"
544            // ),
545            // support_codec!(
546            //     CODEC_TYPE_PCM_S24LE_PLANAR,
547            //     "pcm_s24le_planar",
548            //     "PCM Signed 24-bit Little-Endian Planar"
549            // ),
550            // support_codec!(
551            //     CODEC_TYPE_PCM_S24BE_PLANAR,
552            //     "pcm_s24be_planar",
553            //     "PCM Signed 24-bit Big-Endian Planar"
554            // ),
555            // support_codec!(
556            //     CODEC_TYPE_PCM_S16LE_PLANAR,
557            //     "pcm_s16le_planar",
558            //     "PCM Signed 16-bit Little-Endian Planar"
559            // ),
560            // support_codec!(
561            //     CODEC_TYPE_PCM_S16BE_PLANAR,
562            //     "pcm_s16be_planar",
563            //     "PCM Signed 16-bit Big-Endian Planar"
564            // ),
565            // support_codec!(
566            //     CODEC_TYPE_PCM_S8_PLANAR   ,
567            //     "pcm_s8_planar"   ,
568            //     "PCM Signed 8-bit Planar"
569            // ),
570            // support_codec!(
571            //     CODEC_TYPE_PCM_U32LE_PLANAR,
572            //     "pcm_u32le_planar",
573            //     "PCM Unsigned 32-bit Little-Endian Planar"
574            // ),
575            // support_codec!(
576            //     CODEC_TYPE_PCM_U32BE_PLANAR,
577            //     "pcm_u32be_planar",
578            //     "PCM Unsigned 32-bit Big-Endian Planar"
579            // ),
580            // support_codec!(
581            //     CODEC_TYPE_PCM_U24LE_PLANAR,
582            //     "pcm_u24le_planar",
583            //     "PCM Unsigned 24-bit Little-Endian Planar"
584            // ),
585            // support_codec!(
586            //     CODEC_TYPE_PCM_U24BE_PLANAR,
587            //     "pcm_u24be_planar",
588            //     "PCM Unsigned 24-bit Big-Endian Planar"
589            // ),
590            // support_codec!(
591            //     CODEC_TYPE_PCM_U16LE_PLANAR,
592            //     "pcm_u16le_planar",
593            //     "PCM Unsigned 16-bit Little-Endian Planar"
594            // ),
595            // support_codec!(
596            //     CODEC_TYPE_PCM_U16BE_PLANAR,
597            //     "pcm_u16be_planar",
598            //     "PCM Unsigned 16-bit Big-Endian Planar"
599            // ),
600            // support_codec!(
601            //     CODEC_TYPE_PCM_U8_PLANAR   ,
602            //     "pcm_u8_planar"   ,
603            //     "PCM Unsigned 8-bit Planar"
604            // ),
605            // support_codec!(
606            //     CODEC_TYPE_PCM_F32LE_PLANAR,
607            //     "pcm_f32le_planar",
608            //     "PCM 32-bit Little-Endian Floating Point Planar"
609            // ),
610            // support_codec!(
611            //     CODEC_TYPE_PCM_F32BE_PLANAR,
612            //     "pcm_f32be_planar",
613            //     "PCM 32-bit Big-Endian Floating Point Planar"
614            // ),
615            // support_codec!(
616            //     CODEC_TYPE_PCM_F64LE_PLANAR,
617            //     "pcm_f64le_planar",
618            //     "PCM 64-bit Little-Endian Floating Point Planar"
619            // ),
620            // support_codec!(
621            //     CODEC_TYPE_PCM_F64BE_PLANAR,
622            //     "pcm_f64be_planar",
623            //     "PCM 64-bit Big-Endian Floating Point Planar"
624            // ),
625        ]
626    }
627
628    fn reset(&mut self) {
629        // No state is stored between packets, therefore do nothing.
630    }
631
632    fn codec_params(&self) -> &CodecParameters {
633        &self.params
634    }
635
636    fn decode(&mut self, packet: &Packet) -> Result<AudioBufferRef<'_>> {
637        if let Err(e) = self.decode_inner(packet) {
638            self.buf.clear();
639            Err(e)
640        }
641        else {
642            Ok(self.buf.as_audio_buffer_ref())
643        }
644    }
645
646    fn finalize(&mut self) -> FinalizeResult {
647        Default::default()
648    }
649
650    fn last_decoded(&self) -> AudioBufferRef<'_> {
651        self.buf.as_audio_buffer_ref()
652    }
653}