symphonia_bundle_flac/
decoder.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::cmp;
9use std::convert::TryInto;
10use std::num::Wrapping;
11
12use symphonia_core::audio::{AsAudioBufferRef, AudioBuffer, AudioBufferRef};
13use symphonia_core::audio::{Signal, SignalSpec};
14use symphonia_core::codecs::{
15    CodecDescriptor, CodecParameters, VerificationCheck, CODEC_TYPE_FLAC,
16};
17use symphonia_core::codecs::{Decoder, DecoderOptions, FinalizeResult};
18use symphonia_core::errors::{decode_error, unsupported_error, Result};
19use symphonia_core::formats::Packet;
20use symphonia_core::io::{BitReaderLtr, BufReader, ReadBitsLtr};
21use symphonia_core::support_codec;
22use symphonia_core::units::TimeBase;
23use symphonia_core::util::bits::sign_extend_leq32_to_i32;
24use symphonia_utils_xiph::flac::metadata::StreamInfo;
25
26use log::{debug, log_enabled, warn};
27
28use super::frame::*;
29use super::validate::Validator;
30
31fn decorrelate_left_side(left: &[i32], side: &mut [i32]) {
32    for (s, l) in side.iter_mut().zip(left) {
33        *s = *l - *s;
34    }
35}
36
37fn decorrelate_mid_side(mid: &mut [i32], side: &mut [i32]) {
38    for (m, s) in mid.iter_mut().zip(side) {
39        // Mid (M) is given as M = L/2 + R/2, while Side (S) is given as S = L - R.
40        //
41        // To calculate the individual channels, the following equations can be used:
42        //      - L = S/2 + M
43        //      - R = M - S/2
44        //
45        // Ideally, this would work, but since samples are represented as integers, division yields
46        // the floor of the divided value. Therefore, the channel restoration equations actually
47        // yield:
48        //      - L = floor(S/2) + M
49        //      - R = M - floor(S/2)
50        //
51        // This will produce incorrect samples whenever the sample S is odd. For example:
52        //      - 2/2 = 1
53        //      - 3/2 = 1 (should be 2 if rounded!)
54        //
55        // To get the proper rounding behaviour, the solution is to add one to the result if S is
56        // odd:
57        //      - L = floor(S/2) + M + (S%2) = M + (S%2) + floor(S/2)
58        //      - R = M - floor(S/2) + (S%2) = M + (S%2) - floor(S/2)
59        //
60        // Further, to prevent loss of accuracy, instead of dividing S/2 and adding or subtracting
61        // it from M, multiply M*2, then add or subtract S, and then divide the whole result by 2.
62        // This gives one extra bit of precision for the intermediate computations.
63        //
64        // Conveniently, since M should be doubled, the LSB will always be 0. This allows S%2 to
65        // be added simply by bitwise ORing S&1 to M<<1.
66        //
67        // Therefore the final equations yield:
68        //      - L = (2*M + (S%2) + S) / 2
69        //      - R = (2*M + (S%2) - S) / 2
70        let mid = (*m << 1) | (*s & 1);
71        let side = *s;
72        *m = (mid + side) >> 1;
73        *s = (mid - side) >> 1;
74    }
75}
76
77fn decorrelate_right_side(right: &[i32], side: &mut [i32]) {
78    for (s, r) in side.iter_mut().zip(right) {
79        *s += *r;
80    }
81}
82
83/// Free Lossless Audio Codec (FLAC) decoder.
84pub struct FlacDecoder {
85    params: CodecParameters,
86    is_validating: bool,
87    validator: Validator,
88    buf: AudioBuffer<i32>,
89}
90
91impl FlacDecoder {
92    fn decode_inner(&mut self, packet: &Packet) -> Result<()> {
93        let mut reader = packet.as_buf_reader();
94
95        // Synchronize to a frame and get the synchronization code.
96        let sync = sync_frame(&mut reader)?;
97
98        let header = read_frame_header(&mut reader, sync)?;
99
100        // Use the bits per sample and sample rate as stated in the frame header, falling back to
101        // the stream information if provided. If neither are available, return an error.
102        let bits_per_sample = if let Some(bps) = header.bits_per_sample {
103            bps
104        }
105        else if let Some(bps) = self.params.bits_per_sample {
106            bps
107        }
108        else {
109            return decode_error("flac: bits per sample not provided");
110        };
111
112        // trace!("frame: [{:?}] strategy={:?}, n_samples={}, bps={}, channels={:?}",
113        //     header.block_sequence,
114        //     header.blocking_strategy,
115        //     header.block_num_samples,
116        //     bits_per_sample,
117        //     &header.channel_assignment);
118
119        // Reserve a writeable chunk in the buffer equal to the number of samples in the block.
120        self.buf.clear();
121        self.buf.render_reserved(Some(header.block_num_samples as usize));
122
123        // Only Bitstream reading for subframes.
124        {
125            // Sub-frames don't have any byte-aligned content, so use a BitReader.
126            let mut bs = BitReaderLtr::new(reader.read_buf_bytes_available_ref());
127
128            // Read each subframe based on the channel assignment into a planar buffer.
129            match header.channel_assignment {
130                ChannelAssignment::Independant(channels) => {
131                    for i in 0..channels as usize {
132                        read_subframe(&mut bs, bits_per_sample, self.buf.chan_mut(i))?;
133                    }
134                }
135                // For Left/Side, Mid/Side, and Right/Side channel configurations, the Side
136                // (Difference) channel requires an extra bit per sample.
137                ChannelAssignment::LeftSide => {
138                    let (left, side) = self.buf.chan_pair_mut(0, 1);
139
140                    read_subframe(&mut bs, bits_per_sample, left)?;
141                    read_subframe(&mut bs, bits_per_sample + 1, side)?;
142
143                    decorrelate_left_side(left, side);
144                }
145                ChannelAssignment::MidSide => {
146                    let (mid, side) = self.buf.chan_pair_mut(0, 1);
147
148                    read_subframe(&mut bs, bits_per_sample, mid)?;
149                    read_subframe(&mut bs, bits_per_sample + 1, side)?;
150
151                    decorrelate_mid_side(mid, side);
152                }
153                ChannelAssignment::RightSide => {
154                    let (side, right) = self.buf.chan_pair_mut(0, 1);
155
156                    read_subframe(&mut bs, bits_per_sample + 1, side)?;
157                    read_subframe(&mut bs, bits_per_sample, right)?;
158
159                    decorrelate_right_side(right, side);
160                }
161            }
162        }
163
164        // Feed the validator if validation is enabled.
165        if self.is_validating {
166            self.validator.update(&self.buf, bits_per_sample);
167        }
168
169        // The decoder uses a 32bit sample format as a common denominator, but that doesn't mean
170        // the encoded audio samples are actually 32bit. Shift all samples in the output buffer
171        // so that regardless the encoded bits/sample, the output is always 32bits/sample.
172        if bits_per_sample < 32 {
173            let shift = 32 - bits_per_sample;
174            self.buf.transform(|sample| sample << shift);
175        }
176
177        Ok(())
178    }
179}
180
181impl Decoder for FlacDecoder {
182    fn try_new(params: &CodecParameters, options: &DecoderOptions) -> Result<Self> {
183        // This decoder only supports FLAC.
184        if params.codec != CODEC_TYPE_FLAC {
185            return unsupported_error("flac: invalid codec type");
186        }
187
188        // Obtain the extra data.
189        let extra_data = match params.extra_data.as_ref() {
190            Some(buf) => buf,
191            _ => return unsupported_error("flac: missing extra data"),
192        };
193
194        // Read the stream information block.
195        let info = StreamInfo::read(&mut BufReader::new(extra_data))?;
196
197        // Clone the codec parameters so that the parameters can be supplemented and/or amended.
198        let mut params = params.clone();
199
200        // Amend the provided codec parameters with information from the stream information block.
201        params
202            .with_sample_rate(info.sample_rate)
203            .with_time_base(TimeBase::new(1, info.sample_rate))
204            .with_bits_per_sample(info.bits_per_sample)
205            .with_max_frames_per_packet(u64::from(info.block_len_max))
206            .with_channels(info.channels);
207
208        if let Some(md5) = info.md5 {
209            params.with_verification_code(VerificationCheck::Md5(md5));
210        }
211
212        if let Some(n_frames) = info.n_samples {
213            params.with_n_frames(n_frames);
214        }
215
216        let spec = SignalSpec::new(info.sample_rate, info.channels);
217        let buf = AudioBuffer::new(u64::from(info.block_len_max), spec);
218
219        // TODO: Verify packet integrity if the demuxer is not.
220        // if !params.packet_data_integrity {
221        //     return unsupported_error("flac: packet integrity is required");
222        // }
223
224        Ok(FlacDecoder {
225            params,
226            is_validating: options.verify,
227            validator: Default::default(),
228            buf,
229        })
230    }
231
232    fn supported_codecs() -> &'static [CodecDescriptor] {
233        &[support_codec!(CODEC_TYPE_FLAC, "flac", "Free Lossless Audio Codec")]
234    }
235
236    fn reset(&mut self) {
237        // No state is stored between packets, therefore do nothing.
238    }
239
240    fn codec_params(&self) -> &CodecParameters {
241        &self.params
242    }
243
244    fn decode(&mut self, packet: &Packet) -> Result<AudioBufferRef<'_>> {
245        if let Err(e) = self.decode_inner(packet) {
246            self.buf.clear();
247            Err(e)
248        }
249        else {
250            Ok(self.buf.as_audio_buffer_ref())
251        }
252    }
253
254    fn finalize(&mut self) -> FinalizeResult {
255        let mut result: FinalizeResult = Default::default();
256
257        // If verifying...
258        if self.is_validating {
259            // Try to get the expected MD5 checksum and compare it against the decoded checksum.
260            if let Some(VerificationCheck::Md5(expected)) = self.params.verification_check {
261                let decoded = self.validator.md5();
262
263                // Only generate the expected and decoded MD5 checksum strings if logging is
264                // enabled at the debug level.
265                if log_enabled!(log::Level::Debug) {
266                    use std::fmt::Write;
267
268                    let mut expected_s = String::with_capacity(32);
269                    let mut decoded_s = String::with_capacity(32);
270
271                    expected.iter().for_each(|b| write!(expected_s, "{:02x}", b).unwrap());
272                    decoded.iter().for_each(|b| write!(decoded_s, "{:02x}", b).unwrap());
273
274                    debug!("verification: expected md5 = {}", expected_s);
275                    debug!("verification: decoded md5  = {}", decoded_s);
276                }
277
278                result.verify_ok = Some(decoded == expected)
279            }
280            else {
281                warn!("verification requested but the expected md5 checksum was not provided");
282            }
283        }
284
285        result
286    }
287
288    fn last_decoded(&self) -> AudioBufferRef<'_> {
289        self.buf.as_audio_buffer_ref()
290    }
291}
292
293// Subframe business
294
295#[derive(Debug)]
296enum SubFrameType {
297    Constant,
298    Verbatim,
299    FixedLinear(u32),
300    Linear(u32),
301}
302
303fn read_subframe<B: ReadBitsLtr>(bs: &mut B, frame_bps: u32, buf: &mut [i32]) -> Result<()> {
304    // First sub-frame bit must always 0.
305    if bs.read_bool()? {
306        return decode_error("flac: subframe padding is not 0");
307    }
308
309    // Next 6 bits designate the sub-frame type.
310    let subframe_type_enc = bs.read_bits_leq32(6)?;
311
312    let subframe_type = match subframe_type_enc {
313        0x00 => SubFrameType::Constant,
314        0x01 => SubFrameType::Verbatim,
315        0x08..=0x0f => {
316            let order = subframe_type_enc & 0x07;
317            // The Fixed Predictor only supports orders between 0 and 4.
318            if order > 4 {
319                return decode_error("flac: fixed predictor orders of greater than 4 are invalid");
320            }
321            SubFrameType::FixedLinear(order)
322        }
323        0x20..=0x3f => SubFrameType::Linear((subframe_type_enc & 0x1f) + 1),
324        _ => {
325            return decode_error("flac: subframe type set to reserved value");
326        }
327    };
328
329    // Bit 7 of the sub-frame header designates if there are any dropped (wasted in FLAC terms)
330    // bits per sample in the audio sub-block. If the bit is set, unary decode the number of
331    // dropped bits per sample.
332    let dropped_bps = if bs.read_bool()? { bs.read_unary_zeros()? + 1 } else { 0 };
333
334    // The bits per sample stated in the frame header is for the decoded audio sub-block samples.
335    // However, it is likely that the lower order bits of all the samples are simply 0. Therefore,
336    // the encoder will truncate `dropped_bps` of lower order bits for every sample in a sub-block.
337    // The decoder simply needs to shift left all samples by `dropped_bps` after decoding the
338    // sub-frame and obtaining the truncated audio sub-block samples.
339    let bps = frame_bps - dropped_bps;
340
341    // trace!("\tsubframe: type={:?}, bps={}, dropped_bps={}",
342    //     &subframe_type,
343    //     bps,
344    //     dropped_bps);
345
346    match subframe_type {
347        SubFrameType::Constant => decode_constant(bs, bps, buf)?,
348        SubFrameType::Verbatim => decode_verbatim(bs, bps, buf)?,
349        SubFrameType::FixedLinear(order) => decode_fixed_linear(bs, bps, order, buf)?,
350        SubFrameType::Linear(order) => decode_linear(bs, bps, order, buf)?,
351    };
352
353    // Shift the samples to account for the dropped bits.
354    samples_shl(dropped_bps, buf);
355
356    Ok(())
357}
358
359#[inline(always)]
360fn samples_shl(shift: u32, buf: &mut [i32]) {
361    if shift > 0 {
362        for sample in buf.iter_mut() {
363            *sample = sample.wrapping_shl(shift);
364        }
365    }
366}
367
368fn decode_constant<B: ReadBitsLtr>(bs: &mut B, bps: u32, buf: &mut [i32]) -> Result<()> {
369    let const_sample = sign_extend_leq32_to_i32(bs.read_bits_leq32(bps)?, bps);
370
371    for sample in buf.iter_mut() {
372        *sample = const_sample;
373    }
374
375    Ok(())
376}
377
378fn decode_verbatim<B: ReadBitsLtr>(bs: &mut B, bps: u32, buf: &mut [i32]) -> Result<()> {
379    for sample in buf.iter_mut() {
380        *sample = sign_extend_leq32_to_i32(bs.read_bits_leq32(bps)?, bps);
381    }
382
383    Ok(())
384}
385
386fn decode_fixed_linear<B: ReadBitsLtr>(
387    bs: &mut B,
388    bps: u32,
389    order: u32,
390    buf: &mut [i32],
391) -> Result<()> {
392    // The first `order` samples are encoded verbatim to warm-up the LPC decoder.
393    decode_verbatim(bs, bps, &mut buf[..order as usize])?;
394
395    // Decode the residuals for the predicted samples.
396    decode_residual(bs, order, buf)?;
397
398    // Run the Fixed predictor (appends to residuals).
399    //
400    // TODO: The fixed predictor uses 64-bit accumulators by default to support bps > 26. On 64-bit
401    // machines, this is preferable, but on 32-bit machines if bps <= 26, run a 32-bit predictor,
402    // and fallback to the 64-bit predictor if necessary (which is basically never).
403    fixed_predict(order, buf);
404
405    Ok(())
406}
407
408fn decode_linear<B: ReadBitsLtr>(bs: &mut B, bps: u32, order: u32, buf: &mut [i32]) -> Result<()> {
409    // The order of the Linear Predictor should be between 1 and 32.
410    debug_assert!(order > 0 && order <= 32);
411
412    // The first `order` samples are encoded verbatim to warm-up the LPC decoder.
413    decode_verbatim(bs, bps, &mut buf[0..order as usize])?;
414
415    // Quantized linear predictor (QLP) coefficients precision in bits (1-16).
416    let qlp_precision = bs.read_bits_leq32(4)? + 1;
417
418    if qlp_precision > 15 {
419        return decode_error("flac: qlp precision set to reserved value");
420    }
421
422    // QLP coefficients bit shift [-16, 15].
423    let qlp_coeff_shift = sign_extend_leq32_to_i32(bs.read_bits_leq32(5)?, 5);
424
425    if qlp_coeff_shift >= 0 {
426        let mut qlp_coeffs = [0i32; 32];
427
428        for c in qlp_coeffs.iter_mut().rev().take(order as usize) {
429            *c = sign_extend_leq32_to_i32(bs.read_bits_leq32(qlp_precision)?, qlp_precision);
430        }
431
432        decode_residual(bs, order, buf)?;
433
434        // Helper function to dispatch to a predictor with a maximum order of N.
435        #[inline(always)]
436        fn lpc<const N: usize>(order: u32, coeffs: &[i32; 32], coeff_shift: i32, buf: &mut [i32]) {
437            let coeffs_n = (&coeffs[32 - N..32]).try_into().unwrap();
438            lpc_predict::<N>(order as usize, coeffs_n, coeff_shift as u32, buf);
439        }
440
441        // Pick the best length linear predictor to use based on the order. Most FLAC streams use
442        // the subset format and have an order <= 12. Therefore, for orders <= 12, dispatch to
443        // predictors that roughly match the order. If a predictor is too long for a given order,
444        // then there will be wasted computations. On the other hand, it is not worth the code bloat
445        // to specialize for every order <= 12.
446        match order {
447            0..=4 => lpc::<4>(order, &qlp_coeffs, qlp_coeff_shift, buf),
448            5..=6 => lpc::<6>(order, &qlp_coeffs, qlp_coeff_shift, buf),
449            7..=8 => lpc::<8>(order, &qlp_coeffs, qlp_coeff_shift, buf),
450            9..=10 => lpc::<10>(order, &qlp_coeffs, qlp_coeff_shift, buf),
451            11..=12 => lpc::<12>(order, &qlp_coeffs, qlp_coeff_shift, buf),
452            _ => lpc::<32>(order, &qlp_coeffs, qlp_coeff_shift, buf),
453        };
454    }
455    else {
456        return unsupported_error("flac: lpc shifts less than 0 are not supported");
457    }
458
459    Ok(())
460}
461
462fn decode_residual<B: ReadBitsLtr>(
463    bs: &mut B,
464    n_prelude_samples: u32,
465    buf: &mut [i32],
466) -> Result<()> {
467    let method_enc = bs.read_bits_leq32(2)?;
468
469    // The FLAC specification defines two residual coding methods: Rice and Rice2. The
470    // only difference between the two is the bit width of the Rice parameter. Note the
471    // bit width based on the residual encoding method and use the same code path for
472    // both cases.
473    let param_bit_width = match method_enc {
474        0x0 => 4,
475        0x1 => 5,
476        _ => {
477            return decode_error("flac: residual method set to reserved value");
478        }
479    };
480
481    // Read the partition order.
482    let order = bs.read_bits_leq32(4)?;
483
484    // The number of partitions is equal to 2^order.
485    let n_partitions = 1usize << order;
486
487    // In general, all partitions have the same number of samples such that the sum of all partition
488    // lengths equal the block length. The number of samples in a partition can therefore be
489    // calculated with block_size / 2^order *in general*. However, since there are warm-up samples
490    // stored verbatim, the first partition has n_prelude_samples less samples. Likewise, if there
491    // is only one partition, then it too has n_prelude_samples less samples.
492    let n_partition_samples = buf.len() >> order;
493
494    // The size of the first (and/or only) partition as per the specification is n_partition_samples
495    // minus the number of warm-up samples (which is the predictor order). Ensure the number of
496    // samples in these types of partitions cannot be negative.
497    if n_prelude_samples as usize > n_partition_samples {
498        return decode_error("flac: residual partition too small for given predictor order");
499    }
500
501    // Ensure that the sum of all partition lengths equal the block size.
502    if n_partitions * n_partition_samples != buf.len() {
503        return decode_error("flac: block size is not same as encoded residual");
504    }
505
506    // trace!("\t\tresidual: n_partitions={}, n_partition_samples={}, n_prelude_samples={}",
507    //     n_partitions,
508    //     n_partition_samples,
509    //     n_prelude_samples);
510
511    // Decode the first partition as it may have less than n_partition_samples samples.
512    decode_rice_partition(
513        bs,
514        param_bit_width,
515        &mut buf[n_prelude_samples as usize..n_partition_samples],
516    )?;
517
518    // Decode the remaining partitions.
519    for buf_chunk in buf[n_partition_samples..].chunks_mut(n_partition_samples) {
520        decode_rice_partition(bs, param_bit_width, buf_chunk)?;
521    }
522
523    Ok(())
524}
525
526fn decode_rice_partition<B: ReadBitsLtr>(
527    bs: &mut B,
528    param_bit_width: u32,
529    buf: &mut [i32],
530) -> Result<()> {
531    // Read the encoding parameter, generally the Rice parameter.
532    let rice_param = bs.read_bits_leq32(param_bit_width)?;
533
534    // If the Rice parameter is all 1s (e.g., 0xf for a 4bit parameter, 0x1f for a 5bit parameter),
535    // then it indicates that residuals in this partition are not Rice encoded, rather they are
536    // binary encoded. Conversely, if the parameter is less than this value, the residuals are Rice
537    // encoded.
538    if rice_param < (1 << param_bit_width) - 1 {
539        // println!("\t\t\tPartition (Rice): n_residuals={}, rice_param={}", buf.len(), rice_param);
540
541        // Read each rice encoded residual and store in buffer.
542        for sample in buf.iter_mut() {
543            let q = bs.read_unary_zeros()?;
544            let r = bs.read_bits_leq32(rice_param)?;
545            *sample = rice_signed_to_i32((q << rice_param) | r);
546        }
547    }
548    else {
549        let residual_bits = bs.read_bits_leq32(5)?;
550
551        // trace!(
552        //     "\t\t\tpartition (Binary): n_residuals={}, residual_bits={}",
553        //     buf.len(),
554        //     residual_bits
555        // );
556
557        // Read each binary encoded residual and store in buffer.
558        for sample in buf.iter_mut() {
559            *sample = sign_extend_leq32_to_i32(bs.read_bits_leq32(residual_bits)?, residual_bits);
560        }
561    }
562
563    Ok(())
564}
565
566#[inline(always)]
567fn rice_signed_to_i32(word: u32) -> i32 {
568    // Input  => 0  1  2  3  4  5  6  7  8  9  10
569    // Output => 0 -1  1 -2  2 -3  3 -4  4 -5   5
570    //
571    //  - If even: output = input / 2
572    //  - If odd:  output = -(input + 1) / 2
573    //                    =  (input / 2) - 1
574
575    // Divide the input by 2 and convert to signed.
576    let div2 = (word >> 1) as i32;
577
578    // Using the LSB of the input, create a new signed integer that's either
579    // -1 (0b1111_11110) or 0 (0b0000_0000). For odd inputs, this will be -1, for even
580    // inputs it'll be 0.
581    let sign = -((word & 0x1) as i32);
582
583    // XOR the div2 result with the sign. If sign is 0, the XOR produces div2. If sign is -1, then
584    // -div2 - 1 is returned.
585    //
586    // Example:  input = 9 => div2 = 0b0000_0100, sign = 0b1111_11110
587    //
588    //           div2 ^ sign =   0b0000_0100
589    //                         ^ 0b1111_1110
590    //                           -----------
591    //                           0b1111_1011  (-5)
592    div2 ^ sign
593}
594
595#[test]
596fn verify_rice_signed_to_i32() {
597    assert_eq!(rice_signed_to_i32(0), 0);
598    assert_eq!(rice_signed_to_i32(1), -1);
599    assert_eq!(rice_signed_to_i32(2), 1);
600    assert_eq!(rice_signed_to_i32(3), -2);
601    assert_eq!(rice_signed_to_i32(4), 2);
602    assert_eq!(rice_signed_to_i32(5), -3);
603    assert_eq!(rice_signed_to_i32(6), 3);
604    assert_eq!(rice_signed_to_i32(7), -4);
605    assert_eq!(rice_signed_to_i32(8), 4);
606    assert_eq!(rice_signed_to_i32(9), -5);
607    assert_eq!(rice_signed_to_i32(10), 5);
608
609    assert_eq!(rice_signed_to_i32(u32::max_value()), -2_147_483_648);
610}
611
612fn fixed_predict(order: u32, buf: &mut [i32]) {
613    debug_assert!(order <= 4);
614
615    // The Fixed Predictor is just a hard-coded version of the Linear Predictor up to order 4 and
616    // with fixed coefficients. Some cases may be simplified such as orders 0 and 1. For orders 2
617    // through 4, use the same IIR-style algorithm as the Linear Predictor.
618    match order {
619        // A 0th order predictor always predicts 0, and therefore adds nothing to any of the samples
620        // in buf. Do nothing.
621        0 => (),
622        // A 1st order predictor always returns the previous sample since the polynomial is:
623        // s(i) = 1*s(i),
624        1 => {
625            for i in 1..buf.len() {
626                buf[i] += buf[i - 1];
627            }
628        }
629        // A 2nd order predictor uses the polynomial: s(i) = 2*s(i-1) - 1*s(i-2).
630        2 => {
631            for i in 2..buf.len() {
632                let a = Wrapping(-1) * Wrapping(i64::from(buf[i - 2]));
633                let b = Wrapping(2) * Wrapping(i64::from(buf[i - 1]));
634                buf[i] += (a + b).0 as i32;
635            }
636        }
637        // A 3rd order predictor uses the polynomial: s(i) = 3*s(i-1) - 3*s(i-2) + 1*s(i-3).
638        3 => {
639            for i in 3..buf.len() {
640                let a = Wrapping(1) * Wrapping(i64::from(buf[i - 3]));
641                let b = Wrapping(-3) * Wrapping(i64::from(buf[i - 2]));
642                let c = Wrapping(3) * Wrapping(i64::from(buf[i - 1]));
643                buf[i] += (a + b + c).0 as i32;
644            }
645        }
646        // A 4th order predictor uses the polynomial:
647        // s(i) = 4*s(i-1) - 6*s(i-2) + 4*s(i-3) - 1*s(i-4).
648        4 => {
649            for i in 4..buf.len() {
650                let a = Wrapping(-1) * Wrapping(i64::from(buf[i - 4]));
651                let b = Wrapping(4) * Wrapping(i64::from(buf[i - 3]));
652                let c = Wrapping(-6) * Wrapping(i64::from(buf[i - 2]));
653                let d = Wrapping(4) * Wrapping(i64::from(buf[i - 1]));
654                buf[i] += (a + b + c + d).0 as i32;
655            }
656        }
657        _ => unreachable!(),
658    };
659}
660
661/// Generalized Linear Predictive Coding (LPC) decoder. The exact number of coefficients given is
662/// specified by `order`. Coefficients must be stored in reverse order in `coeffs` with the first
663/// coefficient at index 31. Coefficients at indices less than 31 - `order` must be 0.
664/// It is expected that the first `order` samples in `buf` are warm-up samples.
665fn lpc_predict<const N: usize>(order: usize, coeffs: &[i32; N], coeff_shift: u32, buf: &mut [i32]) {
666    // Order must be less than or equal to the number of coefficients.
667    debug_assert!(order <= coeffs.len());
668
669    // Order must be less than to equal to the number of samples the buffer can hold.
670    debug_assert!(order <= buf.len());
671
672    // The main, efficient, predictor loop needs N previous samples to run. Since order <= N,
673    // calculate enough samples to reach N.
674    let n_prefill = cmp::min(N, buf.len()) - order;
675
676    for i in order..order + n_prefill {
677        let predicted = coeffs[N - order..N]
678            .iter()
679            .zip(&buf[i - order..i])
680            .map(|(&c, &sample)| c as i64 * sample as i64)
681            .sum::<i64>();
682
683        buf[i] += (predicted >> coeff_shift) as i32;
684    }
685
686    // If the pre-fill operation filled the entire sample buffer, return immediately.
687    if buf.len() <= N {
688        return;
689    }
690
691    // Main predictor loop. Calculate each sample by applying what is essentially an IIR filter.
692    for i in N..buf.len() {
693        let predicted = coeffs
694            .iter()
695            .zip(&buf[i - N..i])
696            .map(|(&c, &s)| i64::from(c) * i64::from(s))
697            .sum::<i64>();
698
699        buf[i] += (predicted >> coeff_shift) as i32;
700    }
701}