nanomp3-core 0.2.0

The no_std, allocation-free MPEG audio decoder behind nanomp3 (a bit-exact, safe Rust port of minimp3)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
//! Safe Rust port of the Layer III decoder from [minimp3](https://github.com/lieff/minimp3).
//!
//! The code deliberately mirrors the structure of `minimp3.h` (function names
//! are snake_case versions of the C ones) so the two can be read side by side.
//! Every floating-point expression is evaluated in the same order as in C, which
//! keeps the output bit-identical to upstream built with `MINIMP3_ONLY_MP3`,
//! `MINIMP3_FLOAT_OUTPUT` and `MINIMP3_NO_SIMD`; `parity/` checks this.

// Float literals are spelled exactly as in minimp3.h. Rounding them or swapping
// in `core::f32::consts` could change results and break bit-exactness.
#![allow(clippy::excessive_precision, clippy::approx_constant)]
// Index loops mirror the C and keep parallel arrays visibly in step.
#![allow(clippy::needless_range_loop)]

mod lanes;
#[cfg(feature = "layer12")]
mod layer12;
mod tables;
use lanes::{Lanes, F4};
use tables::*;

const HDR_SIZE: usize = 4;
const MAX_FREE_FORMAT_FRAME_SIZE: usize = 2304;
const MAX_FRAME_SYNC_MATCHES: usize = 10;
const MAX_BITRESERVOIR_BYTES: usize = 511;
const MAX_L3_FRAME_PAYLOAD_BYTES: usize = MAX_FREE_FORMAT_FRAME_SIZE;
const SHORT_BLOCK_TYPE: u8 = 2;
const STOP_BLOCK_TYPE: u8 = 3;
/// `MAX_SCFI` in C: `(255 + BITS_DEQUANTIZER_OUT*4 - 210 + 3) & ~3` with `BITS_DEQUANTIZER_OUT = -1`.
const MAX_SCFI: i32 = (255 - 4 - 210 + 3) & !3;

/// A PCM output sample format: `f32`, `i8`, `u8`, `i16`, `u16`, `i32` or `u32`.
///
/// The decoder synthesizes floating-point samples at 16-bit scale (full scale
/// is ±32768) and converts them as follows:
///
/// | type | conversion |
/// |---|---|
/// | `f32` | divided by 32768, so nominally in `[-1.0, 1.0]`; not clipped |
/// | `i16` | rounded half away from zero and clipped |
/// | `i8` | divided by 256, rounded half away from zero and clipped |
/// | `i32` | multiplied by 65536, rounded half away from zero and clipped |
/// | `u8`, `u16`, `u32` | as the signed type, offset by half the range (sign bit flipped) |
///
/// Integer formats carry at most about 24 significant bits, since that's the
/// precision of the synthesis.
///
/// With [`Decoder::new_minimp3_compat`](crate::Decoder::new_minimp3_compat),
/// `i16` uses minimp3's exact rounding instead, which maps samples in
/// (−1.5, −0.5] to 0 rather than −1. `f32` output is identical to minimp3's
/// either way; the other formats have no minimp3 counterpart.
pub trait Sample: Copy + sealed::Sealed {
    #[doc(hidden)]
    fn from_synth(x: f32, minimp3_compat: bool) -> Self;
}

mod sealed {
    pub trait Sealed {}
    impl Sealed for f32 {}
    impl Sealed for i8 {}
    impl Sealed for u8 {}
    impl Sealed for i16 {}
    impl Sealed for u16 {}
    impl Sealed for i32 {}
    impl Sealed for u32 {}
}

/// Rounds half away from zero, saturating to the `i32` range (NaN becomes 0).
/// `f32::round` isn't available in `core`, and the usual `(x + 0.5) as i32`
/// misrounds 0.49999997 (the sum rounds up to 1.0).
#[inline(always)]
fn round_i32(x: f32) -> i32 {
    let t = x as i32; // truncates toward zero and saturates
    let frac = x - t as f32; // exact: |x| < 2^24 or x is integral
    if frac >= 0.5 {
        t.saturating_add(1)
    } else if frac <= -0.5 {
        t.saturating_sub(1)
    } else {
        t
    }
}

impl Sample for f32 {
    #[inline(always)]
    fn from_synth(x: f32, _: bool) -> f32 {
        x * (1.0 / 32768.0)
    }
}

impl Sample for i16 {
    #[inline(always)]
    fn from_synth(x: f32, minimp3_compat: bool) -> i16 {
        if minimp3_compat {
            // minimp3's mp3d_scale_pcm, quirk included.
            if x >= 32766.5 {
                return 32767;
            }
            if x <= -32767.5 {
                return -32768;
            }
            let s = (x + 0.5) as i16;
            s - (s < 0) as i16
        } else {
            round_i32(x).clamp(i16::MIN.into(), i16::MAX.into()) as i16
        }
    }
}

impl Sample for i8 {
    #[inline(always)]
    fn from_synth(x: f32, _: bool) -> i8 {
        round_i32(x * (1.0 / 256.0)).clamp(i8::MIN.into(), i8::MAX.into()) as i8
    }
}

impl Sample for i32 {
    #[inline(always)]
    fn from_synth(x: f32, _: bool) -> i32 {
        round_i32(x * 65536.0)
    }
}

impl Sample for u8 {
    #[inline(always)]
    fn from_synth(x: f32, c: bool) -> u8 {
        i8::from_synth(x, c) as u8 ^ 0x80
    }
}

impl Sample for u16 {
    #[inline(always)]
    fn from_synth(x: f32, _: bool) -> u16 {
        i16::from_synth(x, false) as u16 ^ 0x8000
    }
}

impl Sample for u32 {
    #[inline(always)]
    fn from_synth(x: f32, c: bool) -> u32 {
        i32::from_synth(x, c) as u32 ^ 0x8000_0000
    }
}

/// Information about a decoded frame (`mp3dec_frame_info_t`).
#[derive(Copy, Clone, Default, Debug)]
pub struct FrameInfo {
    pub frame_bytes: usize,
    pub frame_offset: usize,
    pub channels: u8,
    pub hz: u32,
    pub layer: u8,
    pub bitrate_kbps: u32,
    /// Why the last call produced the samples it did. Not part of C's struct.
    pub status: Status,
}

/// The outcome of `mp3dec_decode_frame`, which C only conveys as "0 samples".
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub enum Status {
    /// Samples were decoded.
    Decoded,
    /// No frame was found; `frame_bytes` bytes of junk were skipped.
    #[default]
    NoFrame,
    /// Only the header was parsed (`pcm == None`).
    HeaderOnly,
    /// The frame's side info or bitstream is invalid; the decoder resyncs.
    Corrupt,
    /// The frame needs bit-reservoir bytes from frames that weren't decoded.
    ReservoirUnavailable,
    /// A Layer I/II frame, and the `layer12` feature is off.
    UnsupportedLayer,
}

/// Persistent decoder state (`mp3dec_t`).
#[derive(Clone)]
struct State {
    mdct_overlap: [[f32; 9 * 32]; 2],
    qmf_state: [f32; 15 * 2 * 32],
    reserv: i32,
    free_format_bytes: usize,
    header: [u8; 4],
    reserv_buf: [u8; MAX_BITRESERVOIR_BYTES],
}

impl State {
    const fn new() -> Self {
        Self {
            mdct_overlap: [[0.; 9 * 32]; 2],
            qmf_state: [0.; 15 * 2 * 32],
            reserv: 0,
            free_format_bytes: 0,
            header: [0; 4],
            reserv_buf: [0; MAX_BITRESERVOIR_BYTES],
        }
    }
}

/// Per-frame working memory (`mp3dec_scratch_t`). Unlike C, it lives in the
/// decoder rather than on the stack so it doesn't have to be re-initialized for
/// every frame; only the parts C might read before writing are cleared.
#[derive(Clone)]
struct Scratch {
    grbuf: [[f32; 576]; 2],
    scf: [f32; 40],
    syn: [[f32; 4]; SYN_ROWS],
    ist_pos: [[u8; 39]; 2],
}

/// The decoder: persistent state plus scratch memory.
#[derive(Clone)]
pub struct Mp3Dec {
    st: State,
    scratch: Scratch,
    maindata: [u8; MAX_BITRESERVOIR_BYTES + MAX_L3_FRAME_PAYLOAD_BYTES],
}

impl Mp3Dec {
    pub const fn new() -> Self {
        Self {
            st: State::new(),
            scratch: Scratch {
                grbuf: [[0.; 576]; 2],
                scf: [0.; 40],
                syn: [[0.; 4]; SYN_ROWS],
                ist_pos: [[0; 39]; 2],
            },
            maindata: [0; MAX_BITRESERVOIR_BYTES + MAX_L3_FRAME_PAYLOAD_BYTES],
        }
    }
}

#[derive(Copy, Clone, Default)]
struct GrInfo {
    sfbtab: &'static [u8],
    part_23_length: u16,
    big_values: u16,
    scalefac_compress: u16,
    global_gain: u8,
    block_type: u8,
    mixed_block_flag: u8,
    n_long_sfb: u8,
    n_short_sfb: u8,
    table_select: [u8; 3],
    region_count: [u8; 3],
    subblock_gain: [u8; 3],
    preflag: u8,
    scalefac_scale: u8,
    count1_table: u8,
    scfsi: u8,
}

/// Bit reader (`bs_t`). Positions are signed like in C: the limit of a
/// malformed tiny frame can be negative, and `pos` keeps advancing past the
/// limit so callers can detect overruns.
struct Bs<'a> {
    buf: &'a [u8],
    pos: i32,
    limit: i32,
}

impl<'a> Bs<'a> {
    fn new(buf: &'a [u8], bytes: i32) -> Self {
        Self { buf, pos: 0, limit: bytes * 8 }
    }

    fn get_bits(&mut self, n: u32) -> u32 {
        let s = (self.pos & 7) as u32;
        let mut p = (self.pos >> 3) as usize;
        self.pos += n as i32;
        if self.pos > self.limit {
            return 0;
        }
        // In bounds: every byte read lies below `limit`, and `limit` never
        // exceeds the buffer.
        let mut shl = (n + s) as i32;
        let mut next = u32::from(self.buf[p] & (255 >> s));
        p += 1;
        let mut cache = 0;
        loop {
            shl -= 8;
            if shl <= 0 {
                break;
            }
            cache |= next << shl;
            next = u32::from(self.buf[p]);
            p += 1;
        }
        cache | (next >> -shl)
    }
}

/// A 4-byte frame header.
#[derive(Copy, Clone)]
pub struct Header(pub [u8; 4]);

impl Header {
    pub fn read(buf: &[u8]) -> Self {
        Self([buf[0], buf[1], buf[2], buf[3]])
    }
    pub fn is_mono(self) -> bool {
        self.0[3] & 0xC0 == 0xC0
    }
    fn is_ms_stereo(self) -> bool {
        self.0[3] & 0xE0 == 0x60
    }
    fn is_free_format(self) -> bool {
        self.0[2] & 0xF0 == 0
    }
    pub fn is_crc(self) -> bool {
        self.0[1] & 1 == 0
    }
    fn test_padding(self) -> bool {
        self.0[2] & 0x2 != 0
    }
    fn test_mpeg1(self) -> bool {
        self.0[1] & 0x8 != 0
    }
    fn test_not_mpeg25(self) -> bool {
        self.0[1] & 0x10 != 0
    }
    fn test_i_stereo(self) -> bool {
        self.0[3] & 0x10 != 0
    }
    fn test_ms_stereo(self) -> bool {
        self.0[3] & 0x20 != 0
    }
    pub fn get_layer(self) -> u8 {
        (self.0[1] >> 1) & 3
    }
    fn get_bitrate(self) -> u8 {
        self.0[2] >> 4
    }
    fn get_sample_rate(self) -> u8 {
        (self.0[2] >> 2) & 3
    }
    fn get_my_sample_rate(self) -> usize {
        let h1 = self.0[1];
        (self.get_sample_rate() + (((h1 >> 3) & 1) + ((h1 >> 4) & 1)) * 3) as usize
    }
    fn is_frame_576(self) -> bool {
        self.0[1] & 14 == 2
    }
    fn is_layer_1(self) -> bool {
        self.0[1] & 6 == 6
    }

    pub fn valid(self) -> bool {
        let h = self.0;
        h[0] == 0xff
            && (h[1] & 0xF0 == 0xf0 || h[1] & 0xFE == 0xe2)
            && self.get_layer() != 0
            && self.get_bitrate() != 15
            && self.get_sample_rate() != 3
    }

    /// `hdr_compare(self, other)`: is `other` a valid header for the same stream?
    pub fn compare(self, other: Header) -> bool {
        let (h1, h2) = (self.0, other.0);
        other.valid()
            && (h1[1] ^ h2[1]) & 0xFE == 0
            && (h1[2] ^ h2[2]) & 0x0C == 0
            && self.is_free_format() == other.is_free_format()
    }

    pub fn bitrate_kbps(self) -> u32 {
        2 * u32::from(
            HDR_BITRATE_KBPS_HALFRATE[self.test_mpeg1() as usize][self.get_layer() as usize - 1]
                [self.get_bitrate() as usize],
        )
    }

    pub fn sample_rate_hz(self) -> u32 {
        HDR_SAMPLE_RATE_HZ_G_HZ[self.get_sample_rate() as usize]
            >> !self.test_mpeg1() as u32
            >> !self.test_not_mpeg25() as u32
    }

    pub fn frame_samples(self) -> u32 {
        if self.is_layer_1() {
            384
        } else {
            1152 >> self.is_frame_576() as u32
        }
    }

    pub fn frame_bytes(self, free_format_size: usize) -> usize {
        let mut frame_bytes =
            (self.frame_samples() * self.bitrate_kbps() * 125 / self.sample_rate_hz()) as usize;
        if self.is_layer_1() {
            frame_bytes &= !3; // slot align
        }
        if frame_bytes != 0 {
            frame_bytes
        } else {
            free_format_size
        }
    }

    pub fn padding(self) -> usize {
        if self.test_padding() {
            if self.is_layer_1() {
                4
            } else {
                1
            }
        } else {
            0
        }
    }
}

fn l3_read_side_info(bs: &mut Bs, gr: &mut [GrInfo; 4], hdr: Header) -> i32 {
    let mut scfsi: u32 = 0;
    let mut part_23_sum: i32 = 0;
    let mut sr_idx = hdr.get_my_sample_rate();
    sr_idx -= (sr_idx != 0) as usize;
    let mut gr_count = if hdr.is_mono() { 1 } else { 2 };
    let main_data_begin;
    if hdr.test_mpeg1() {
        gr_count *= 2;
        main_data_begin = bs.get_bits(9) as i32;
        scfsi = bs.get_bits(7 + gr_count);
    } else {
        main_data_begin = (bs.get_bits(8 + gr_count) >> gr_count) as i32;
    }

    for gr in &mut gr[..gr_count as usize] {
        if hdr.is_mono() {
            scfsi <<= 4;
        }
        gr.part_23_length = bs.get_bits(12) as u16;
        part_23_sum += i32::from(gr.part_23_length);
        gr.big_values = bs.get_bits(9) as u16;
        if gr.big_values > 288 {
            return -1;
        }
        gr.global_gain = bs.get_bits(8) as u8;
        gr.scalefac_compress = bs.get_bits(if hdr.test_mpeg1() { 4 } else { 9 }) as u16;
        gr.sfbtab = &L3_READ_SIDE_INFO_G_SCF_LONG[sr_idx];
        gr.n_long_sfb = 22;
        gr.n_short_sfb = 0;
        let tables;
        if bs.get_bits(1) != 0 {
            gr.block_type = bs.get_bits(2) as u8;
            if gr.block_type == 0 {
                return -1;
            }
            gr.mixed_block_flag = bs.get_bits(1) as u8;
            gr.region_count[0] = 7;
            gr.region_count[1] = 255;
            if gr.block_type == SHORT_BLOCK_TYPE {
                scfsi &= 0x0F0F;
                if gr.mixed_block_flag == 0 {
                    gr.region_count[0] = 8;
                    gr.sfbtab = &L3_READ_SIDE_INFO_G_SCF_SHORT[sr_idx];
                    gr.n_long_sfb = 0;
                    gr.n_short_sfb = 39;
                } else {
                    gr.sfbtab = &L3_READ_SIDE_INFO_G_SCF_MIXED[sr_idx];
                    gr.n_long_sfb = if hdr.test_mpeg1() { 8 } else { 6 };
                    gr.n_short_sfb = 30;
                }
            }
            tables = bs.get_bits(10) << 5;
            gr.subblock_gain[0] = bs.get_bits(3) as u8;
            gr.subblock_gain[1] = bs.get_bits(3) as u8;
            gr.subblock_gain[2] = bs.get_bits(3) as u8;
        } else {
            gr.block_type = 0;
            gr.mixed_block_flag = 0;
            tables = bs.get_bits(15);
            gr.region_count[0] = bs.get_bits(4) as u8;
            gr.region_count[1] = bs.get_bits(3) as u8;
            gr.region_count[2] = 255;
        }
        gr.table_select[0] = (tables >> 10) as u8;
        gr.table_select[1] = ((tables >> 5) & 31) as u8;
        gr.table_select[2] = (tables & 31) as u8;
        gr.preflag = if hdr.test_mpeg1() {
            bs.get_bits(1) as u8
        } else {
            (gr.scalefac_compress >= 500) as u8
        };
        gr.scalefac_scale = bs.get_bits(1) as u8;
        gr.count1_table = bs.get_bits(1) as u8;
        gr.scfsi = ((scfsi >> 12) & 15) as u8;
        scfsi <<= 4;
    }

    if part_23_sum + bs.pos > bs.limit + main_data_begin * 8 {
        return -1;
    }
    main_data_begin
}

fn l3_read_scalefactors(
    scf: &mut [u8; 40],
    ist_pos: &mut [u8; 39],
    scf_size: &[u8; 4],
    scf_count: &[u8],
    bitbuf: &mut Bs,
    mut scfsi: i32,
) {
    let mut pos = 0;
    for i in 0..4 {
        let cnt = scf_count[i] as usize;
        if cnt == 0 {
            break;
        }
        let scf = &mut scf[pos..pos + cnt];
        let ist_pos = &mut ist_pos[pos..pos + cnt];
        if scfsi & 8 != 0 {
            scf.copy_from_slice(ist_pos);
        } else {
            let bits = u32::from(scf_size[i]);
            if bits == 0 {
                scf.fill(0);
                ist_pos.fill(0);
            } else {
                let max_scf = if scfsi < 0 { (1 << bits) - 1 } else { -1 };
                for (scf, ist_pos) in scf.iter_mut().zip(ist_pos) {
                    let s = bitbuf.get_bits(bits) as i32;
                    *ist_pos = if s == max_scf { -1i32 as u8 } else { s as u8 };
                    *scf = s as u8;
                }
            }
        }
        pos += cnt;
        scfsi *= 2;
    }
    scf[pos..pos + 3].fill(0);
}

fn l3_ldexp_q2(mut y: f32, mut exp_q2: i32) -> f32 {
    loop {
        let e = (30 * 4).min(exp_q2);
        y *= L3_LDEXP_Q2_G_EXPFRAC[(e & 3) as usize] * ((1 << 30 >> (e >> 2)) as f32);
        exp_q2 -= e;
        if exp_q2 <= 0 {
            return y;
        }
    }
}

fn l3_decode_scalefactors(
    hdr: Header,
    ist_pos: &mut [u8; 39],
    bs: &mut Bs,
    gr: &GrInfo,
    scf: &mut [f32; 40],
    ch: usize,
) {
    let mut scf_partition: &[u8] = &L3_DECODE_SCALEFACTORS_G_SCM_PARTITIONS
        [(gr.n_short_sfb != 0) as usize + (gr.n_long_sfb == 0) as usize];
    let mut scf_size = [0u8; 4];
    let mut iscf = [0u8; 40];
    let scf_shift = u32::from(gr.scalefac_scale) + 1;
    let mut scfsi = i32::from(gr.scfsi);

    if hdr.test_mpeg1() {
        let part = L3_DECODE_SCALEFACTORS_G_SCFC_DECODE[gr.scalefac_compress as usize];
        scf_size[0] = part >> 2;
        scf_size[1] = scf_size[0];
        scf_size[2] = part & 3;
        scf_size[3] = scf_size[2];
    } else {
        let ist = (hdr.test_i_stereo() && ch != 0) as usize;
        let mut sfc = i32::from(gr.scalefac_compress) >> ist;
        let mut k = ist * 3 * 4;
        while sfc >= 0 {
            let mut modprod = 1;
            for i in (0..4).rev() {
                let m = i32::from(L3_DECODE_SCALEFACTORS_G_MOD[k + i]);
                scf_size[i] = (sfc / modprod % m) as u8;
                modprod *= m;
            }
            sfc -= modprod;
            k += 4;
        }
        scf_partition = &scf_partition[k..];
        scfsi = -16;
    }
    l3_read_scalefactors(&mut iscf, ist_pos, &scf_size, scf_partition, bs, scfsi);

    let n_long = gr.n_long_sfb as usize;
    let n_short = gr.n_short_sfb as usize;
    if n_short != 0 {
        let sh = 3 - scf_shift;
        for i in (0..n_short).step_by(3) {
            for j in 0..3 {
                let v = &mut iscf[n_long + i + j];
                *v = v.wrapping_add(gr.subblock_gain[j] << sh);
            }
        }
    } else if gr.preflag != 0 {
        for (v, &p) in iscf[11..21].iter_mut().zip(&L3_DECODE_SCALEFACTORS_G_PREAMP) {
            *v = v.wrapping_add(p);
        }
    }

    let gain_exp = i32::from(gr.global_gain) - 4 - 210 - if hdr.is_ms_stereo() { 2 } else { 0 };
    let gain = l3_ldexp_q2((1 << (MAX_SCFI / 4)) as f32, MAX_SCFI - gain_exp);
    for (scf, &iscf) in scf.iter_mut().zip(&iscf).take(n_long + n_short) {
        *scf = l3_ldexp_q2(gain, i32::from(iscf) << scf_shift);
    }
}

fn l3_pow_43(mut x: i32) -> f32 {
    let mut mult = 256;
    if x < 129 {
        return G_POW43[(16 + x) as usize];
    }
    if x < 1024 {
        mult = 16;
        x <<= 3;
    }
    let sign = (2 * x) & 64;
    let frac = ((x & 63) - sign) as f32 / ((x & !63) + sign) as f32;
    G_POW43[(16 + ((x + sign) >> 6)) as usize]
        * (1.0 + frac * ((4.0 / 3.0) + frac * (2.0 / 9.0)))
        * mult as f32
}

/// The Huffman decoder's 32-bit look-ahead cache. Bytes past the end of the
/// buffer read as zero: a corrupt `big_values` can make the decoder run past
/// the granule's data, which C tolerates by reading neighbouring memory.
struct BitCache<'a> {
    buf: &'a [u8],
    next: usize,
    cache: u32,
    sh: i32,
}

impl BitCache<'_> {
    #[inline(always)]
    fn byte(&self, i: usize) -> u32 {
        self.buf.get(i).copied().map_or(0, u32::from)
    }

    #[inline(always)]
    fn peek(&self, n: i32) -> u32 {
        self.cache >> (32 - n)
    }

    #[inline(always)]
    fn flush(&mut self, n: i32) {
        self.cache <<= n;
        self.sh += n;
    }

    #[inline(always)]
    fn check(&mut self) {
        while self.sh >= 0 {
            self.cache |= self.byte(self.next) << self.sh;
            self.next += 1;
            self.sh -= 8;
        }
    }

    /// Current bit position (`BSPOS` in C).
    fn pos(&self) -> i32 {
        self.next as i32 * 8 - 24 + self.sh
    }
}

/// Decodes Huffman pairs for one scalefactor band of the big-values region.
#[inline(always)]
fn l3_huffman_pairs<const LINBITS: bool>(
    out: &mut [f32],
    br: &mut BitCache,
    codebook: &[i16],
    linbits: i32,
    one: f32,
) {
    for pair in out.as_chunks_mut::<2>().0 {
        let mut w = 5;
        let mut leaf = i32::from(codebook[br.peek(w) as usize]);
        while leaf < 0 {
            br.flush(w);
            w = leaf & 7;
            leaf = i32::from(codebook[br.peek(w).wrapping_sub((leaf >> 3) as u32) as usize]);
        }
        br.flush(leaf >> 8);

        for v in pair {
            let mut lsb = leaf & 0x0F;
            if LINBITS && lsb == 15 {
                lsb += br.peek(linbits) as i32;
                br.flush(linbits);
                br.check();
                *v = one * l3_pow_43(lsb) * if (br.cache as i32) < 0 { -1.0 } else { 1.0 };
            } else {
                *v = G_POW43[(16 + lsb) as usize - 16 * (br.cache >> 31) as usize] * one;
            }
            br.flush((lsb != 0) as i32);
            leaf >>= 4;
        }
        br.check();
    }
}

fn l3_huffman(dst: &mut [f32; 576], bs: &mut Bs, gr_info: &GrInfo, scf: &[f32; 40], layer3gr_limit: i32) {
    let mut one = 0.0f32;
    let mut ireg = 0;
    let mut big_val_cnt = i32::from(gr_info.big_values);
    let sfbtab = gr_info.sfbtab;
    let mut sfb = 0;
    let mut scf_idx = 0;
    let mut d = 0;

    let start = (bs.pos / 8) as usize;
    let mut br = BitCache { buf: bs.buf, next: start + 4, cache: 0, sh: (bs.pos & 7) - 8 };
    br.cache = (((br.byte(start) * 256 + br.byte(start + 1)) * 256 + br.byte(start + 2)) * 256
        + br.byte(start + 3))
        << (bs.pos & 7);

    while big_val_cnt > 0 {
        let tab_num = gr_info.table_select[ireg] as usize;
        let mut sfb_cnt = i32::from(gr_info.region_count[ireg]);
        ireg += 1;
        let codebook = &L3_HUFFMAN_TABS[L3_HUFFMAN_TABINDEX[tab_num] as usize..];
        let linbits = i32::from(L3_HUFFMAN_G_LINBITS[tab_num]);
        loop {
            let np = i32::from(sfbtab[sfb]) / 2;
            sfb += 1;
            let pairs_to_decode = big_val_cnt.min(np) as usize;
            one = scf[scf_idx];
            scf_idx += 1;
            // C's do/while always decodes at least one pair; np is never 0 here
            // because big_values <= 288 and every table covers 288 pairs.
            let out = &mut dst[d..d + 2 * pairs_to_decode.max(1)];
            if linbits != 0 {
                l3_huffman_pairs::<true>(out, &mut br, codebook, linbits, one);
            } else {
                l3_huffman_pairs::<false>(out, &mut br, codebook, linbits, one);
            }
            d += out.len();
            big_val_cnt -= np;
            if big_val_cnt <= 0 {
                break;
            }
            sfb_cnt -= 1;
            if sfb_cnt < 0 {
                break;
            }
        }
    }

    let codebook_count1: &[u8] =
        if gr_info.count1_table != 0 { &L3_HUFFMAN_TAB33 } else { &L3_HUFFMAN_TAB32 };
    let mut np = 1 - big_val_cnt;
    'count1: loop {
        let mut leaf = i32::from(codebook_count1[br.peek(4) as usize]);
        if leaf & 8 == 0 {
            leaf = i32::from(
                codebook_count1[(leaf >> 3) as usize + (br.cache << 4 >> (32 - (leaf & 3))) as usize],
            );
        }
        br.flush(leaf & 7);
        if br.pos() > layer3gr_limit {
            break;
        }
        for half in 0..2 {
            np -= 1;
            if np == 0 {
                np = i32::from(sfbtab[sfb]) / 2;
                sfb += 1;
                if np == 0 {
                    break 'count1;
                }
                one = scf[scf_idx];
                scf_idx += 1;
            }
            for s in 2 * half..2 * half + 2 {
                if leaf & (128 >> s) != 0 {
                    dst[d + s] = if (br.cache as i32) < 0 { -one } else { one };
                    br.flush(1);
                }
            }
        }
        br.check();
        d += 4;
    }
    bs.pos = layer3gr_limit;
}

fn l3_midside_stereo(buf: &mut [f32; 1152], start: usize, n: usize) {
    let (left, right) = buf.split_at_mut(576);
    for (l, r) in left[start..start + n].iter_mut().zip(&mut right[start..start + n]) {
        let a = *l;
        let b = *r;
        *l = a + b;
        *r = a - b;
    }
}

fn l3_intensity_stereo_band(buf: &mut [f32; 1152], start: usize, n: usize, kl: f32, kr: f32) {
    let (left, right) = buf.split_at_mut(576);
    for (l, r) in left[start..start + n].iter_mut().zip(&mut right[start..start + n]) {
        *r = *l * kr;
        *l *= kl;
    }
}

fn l3_stereo_top_band(right: &[f32], sfb: &[u8], nbands: usize, max_band: &mut [i32; 3]) {
    *max_band = [-1; 3];
    let mut off = 0;
    for i in 0..nbands {
        let n = sfb[i] as usize;
        let band = &right[off..off + n];
        if band.as_chunks::<2>().0.iter().any(|p| p[0] != 0.0 || p[1] != 0.0) {
            max_band[i % 3] = i as i32;
        }
        off += n;
    }
}

fn l3_stereo_process(
    buf: &mut [f32; 1152],
    ist_pos: &[u8; 39],
    sfb: &[u8],
    hdr: Header,
    max_band: &[i32; 3],
    mpeg2_sh: u32,
) {
    let max_pos = if hdr.test_mpeg1() { 7 } else { 64 };
    let mut off = 0;
    for (i, &n) in sfb.iter().take_while(|&&n| n != 0).enumerate() {
        let n = n as usize;
        let ipos = u32::from(ist_pos[i]);
        if i as i32 > max_band[i % 3] && ipos < max_pos {
            let s = if hdr.test_ms_stereo() { 1.41421356 } else { 1.0 };
            let (kl, kr);
            if hdr.test_mpeg1() {
                kl = L3_STEREO_PROCESS_G_PAN[2 * ipos as usize];
                kr = L3_STEREO_PROCESS_G_PAN[2 * ipos as usize + 1];
            } else {
                let k = l3_ldexp_q2(1.0, (((ipos + 1) >> 1) << mpeg2_sh) as i32);
                (kl, kr) = if ipos & 1 != 0 { (k, 1.0) } else { (1.0, k) };
            }
            l3_intensity_stereo_band(buf, off, n, kl * s, kr * s);
        } else if hdr.test_ms_stereo() {
            l3_midside_stereo(buf, off, n);
        }
        off += n;
    }
}

fn l3_intensity_stereo(buf: &mut [f32; 1152], ist_pos: &mut [u8; 39], gr: &[GrInfo], hdr: Header) {
    let mut max_band = [0i32; 3];
    let n_sfb = gr[0].n_long_sfb as usize + gr[0].n_short_sfb as usize;
    let max_blocks = if gr[0].n_short_sfb != 0 { 3 } else { 1 };

    l3_stereo_top_band(&buf[576..], gr[0].sfbtab, n_sfb, &mut max_band);
    if gr[0].n_long_sfb != 0 {
        max_band = [max_band[0].max(max_band[1]).max(max_band[2]); 3];
    }
    for i in 0..max_blocks {
        let default_pos = if hdr.test_mpeg1() { 3 } else { 0 };
        let itop = n_sfb - max_blocks + i;
        let prev = itop - max_blocks;
        ist_pos[itop] = if max_band[i] >= prev as i32 { default_pos } else { ist_pos[prev] };
    }
    l3_stereo_process(buf, ist_pos, gr[0].sfbtab, hdr, &max_band, u32::from(gr[1].scalefac_compress & 1));
}

fn l3_reorder(grbuf: &mut [f32], scratch: &mut [f32], sfb: &[u8]) {
    let mut src = 0;
    let mut dst = 0;
    for &len in sfb.iter().step_by(3).take_while(|&&len| len != 0) {
        let len = len as usize;
        for i in 0..len {
            scratch[dst] = grbuf[src + i];
            scratch[dst + 1] = grbuf[src + i + len];
            scratch[dst + 2] = grbuf[src + i + 2 * len];
            dst += 3;
        }
        src += 3 * len;
    }
    grbuf[..dst].copy_from_slice(&scratch[..dst]);
}

fn l3_antialias(grbuf: &mut [f32; 576], nbands: usize) {
    let aa = |k: usize, i: usize| F4::load(&L3_ANTIALIAS_G_AA[k][i..]);
    for b in 0..nbands {
        let (lo, hi) = grbuf[18 * b..18 * b + 36].split_at_mut(18);
        for i in [0, 4] {
            // hi[i..i+4] against lo[17-i..=14-i] (reversed).
            let u = F4::load(&hi[i..]);
            let d = F4::load(&lo[14 - i..]).rev();
            (u * aa(0, i) - d * aa(1, i)).store(&mut hi[i..]);
            (u * aa(1, i) + d * aa(0, i)).rev().store(&mut lo[14 - i..]);
        }
    }
}

#[inline(always)]
fn l3_dct3_9<T: Lanes>(y: &mut [T; 9]) {
    let c = T::splat;
    let mut s0 = y[0];
    let mut s2 = y[2];
    let mut s4 = y[4];
    let mut s6 = y[6];
    let mut s8 = y[8];
    let mut t0 = s0 + s6 * c(0.5);
    s0 -= s6;
    let mut t4 = (s4 + s2) * c(0.93969262);
    let mut t2 = (s8 + s2) * c(0.76604444);
    s6 = (s4 - s8) * c(0.17364818);
    s4 += s8 - s2;

    s2 = s0 - s4 * c(0.5);
    y[4] = s4 + s0;
    s8 = t0 - t2 + s6;
    s0 = t0 - t4 + t2;
    s4 = t0 + t4 - s6;

    let mut s1 = y[1];
    let mut s3 = y[3];
    let mut s5 = y[5];
    let mut s7 = y[7];

    s3 = s3 * c(0.86602540);
    t0 = (s5 + s1) * c(0.98480775);
    t4 = (s5 - s7) * c(0.34202014);
    t2 = (s1 + s7) * c(0.64278761);
    s1 = (s1 - s5 - s7) * c(0.86602540);

    s5 = t0 - s3 - t2;
    s7 = t4 - s3 - t0;
    s3 = t4 + s3 - t2;

    y[0] = s4 - s7;
    y[1] = s2 + s1;
    y[2] = s0 - s3;
    y[3] = s8 + s5;
    y[5] = s8 - s5;
    y[6] = s0 + s3;
    y[7] = s2 - s1;
    y[8] = s4 + s7;
}

/// IMDCT-36 of `T::N` adjacent bands at once (one band per lane).
#[inline(always)]
fn l3_imdct36_bands<T: Lanes>(grbuf: &mut [f32], overlap: &mut [f32], window: &[f32; 18]) {
    let c = T::splat;
    let g = |grbuf: &[f32], o: usize| T::gather(&grbuf[o..], 18);
    let mut co = [c(0.0); 9];
    let mut si = [c(0.0); 9];
    co[0] = -g(grbuf, 0);
    si[0] = g(grbuf, 17);
    for i in 0..4 {
        si[8 - 2 * i] = g(grbuf, 4 * i + 1) - g(grbuf, 4 * i + 2);
        co[1 + 2 * i] = g(grbuf, 4 * i + 1) + g(grbuf, 4 * i + 2);
        si[7 - 2 * i] = g(grbuf, 4 * i + 4) - g(grbuf, 4 * i + 3);
        co[2 + 2 * i] = -(g(grbuf, 4 * i + 3) + g(grbuf, 4 * i + 4));
    }
    l3_dct3_9(&mut co);
    l3_dct3_9(&mut si);

    si[1] = -si[1];
    si[3] = -si[3];
    si[5] = -si[5];
    si[7] = -si[7];

    for i in 0..9 {
        let ovl = T::gather(&overlap[i..], 9);
        let sum = co[i] * c(L3_IMDCT36_G_TWID9[9 + i]) + si[i] * c(L3_IMDCT36_G_TWID9[i]);
        (co[i] * c(L3_IMDCT36_G_TWID9[i]) - si[i] * c(L3_IMDCT36_G_TWID9[9 + i])).scatter(&mut overlap[i..], 9);
        (ovl * c(window[i]) - sum * c(window[9 + i])).scatter(&mut grbuf[i..], 18);
        (ovl * c(window[9 + i]) + sum * c(window[i])).scatter(&mut grbuf[17 - i..], 18);
    }
}

fn l3_imdct36(grbuf: &mut [f32], overlap: &mut [f32], window: &[f32; 18], nbands: usize) {
    let mut j = 0;
    while j + 4 <= nbands {
        l3_imdct36_bands::<F4>(&mut grbuf[18 * j..], &mut overlap[9 * j..], window);
        j += 4;
    }
    while j < nbands {
        l3_imdct36_bands::<f32>(&mut grbuf[18 * j..], &mut overlap[9 * j..], window);
        j += 1;
    }
}

fn l3_idct3(x0: f32, x1: f32, x2: f32, dst: &mut [f32; 3]) {
    let m1 = x1 * 0.86602540;
    let a1 = x0 - x2 * 0.5;
    dst[1] = x0 + x2;
    dst[0] = a1 + m1;
    dst[2] = a1 - m1;
}

fn l3_imdct12(x: &[f32], dst: &mut [f32], overlap: &mut [f32]) {
    let mut co = [0f32; 3];
    let mut si = [0f32; 3];
    l3_idct3(-x[0], x[6] + x[3], x[12] + x[9], &mut co);
    l3_idct3(x[15], x[12] - x[9], x[6] - x[3], &mut si);
    si[1] = -si[1];

    for i in 0..3 {
        let ovl = overlap[i];
        let sum = co[i] * L3_IMDCT12_G_TWID3[3 + i] + si[i] * L3_IMDCT12_G_TWID3[i];
        overlap[i] = co[i] * L3_IMDCT12_G_TWID3[i] - si[i] * L3_IMDCT12_G_TWID3[3 + i];
        dst[i] = ovl * L3_IMDCT12_G_TWID3[2 - i] - sum * L3_IMDCT12_G_TWID3[5 - i];
        dst[5 - i] = ovl * L3_IMDCT12_G_TWID3[5 - i] + sum * L3_IMDCT12_G_TWID3[2 - i];
    }
}

fn l3_imdct_short(grbuf: &mut [f32], overlap: &mut [f32], nbands: usize) {
    for (grbuf, overlap) in grbuf.as_chunks_mut::<18>().0.iter_mut().zip(overlap.as_chunks_mut::<9>().0).take(nbands) {
        let mut tmp = [0f32; 18];
        tmp.copy_from_slice(grbuf);
        grbuf[..6].copy_from_slice(&overlap[..6]);
        let (ov_out, ov_state) = overlap.split_at_mut(6);
        l3_imdct12(&tmp, &mut grbuf[6..12], ov_state);
        l3_imdct12(&tmp[1..], &mut grbuf[12..18], ov_state);
        l3_imdct12(&tmp[2..], ov_out, ov_state);
    }
}

fn l3_change_sign(grbuf: &mut [f32; 576]) {
    for band in grbuf.as_chunks_mut::<18>().0.iter_mut().skip(1).step_by(2) {
        for x in band.iter_mut().skip(1).step_by(2) {
            *x = -*x;
        }
    }
}

fn l3_imdct_gr(grbuf: &mut [f32; 576], overlap: &mut [f32; 288], block_type: u8, n_long_bands: usize) {
    let (long_buf, rest_buf) = grbuf.split_at_mut(18 * n_long_bands);
    let (long_ovl, rest_ovl) = overlap.split_at_mut(9 * n_long_bands);
    if n_long_bands != 0 {
        l3_imdct36(long_buf, long_ovl, &L3_IMDCT_GR_G_MDCT_WINDOW[0], n_long_bands);
    }
    if block_type == SHORT_BLOCK_TYPE {
        l3_imdct_short(rest_buf, rest_ovl, 32 - n_long_bands);
    } else {
        let window = &L3_IMDCT_GR_G_MDCT_WINDOW[(block_type == STOP_BLOCK_TYPE) as usize];
        l3_imdct36(rest_buf, rest_ovl, window, 32 - n_long_bands);
    }
}

fn l3_save_reservoir(h: &mut State, bs: &Bs) {
    let mut pos = ((bs.pos + 7) as u32 / 8) as i32;
    let mut remains = (bs.limit as u32 / 8).wrapping_sub(pos as u32) as i32;
    if remains > MAX_BITRESERVOIR_BYTES as i32 {
        pos += remains - MAX_BITRESERVOIR_BYTES as i32;
        remains = MAX_BITRESERVOIR_BYTES as i32;
    }
    if remains > 0 {
        let (pos, remains) = (pos as usize, remains as usize);
        h.reserv_buf[..remains].copy_from_slice(&bs.buf[pos..pos + remains]);
    }
    h.reserv = remains;
}

fn l3_restore_reservoir<'a>(
    h: &State,
    bs: &Bs,
    maindata: &'a mut [u8; MAX_BITRESERVOIR_BYTES + MAX_L3_FRAME_PAYLOAD_BYTES],
    main_data_begin: i32,
) -> (Bs<'a>, bool) {
    let frame_bytes = ((bs.limit - bs.pos) / 8) as usize;
    let bytes_have = h.reserv.min(main_data_begin) as usize;
    let reserv_off = (h.reserv - main_data_begin).max(0) as usize;
    maindata[..bytes_have].copy_from_slice(&h.reserv_buf[reserv_off..reserv_off + bytes_have]);
    let frame_start = (bs.pos / 8) as usize;
    maindata[bytes_have..bytes_have + frame_bytes]
        .copy_from_slice(&bs.buf[frame_start..frame_start + frame_bytes]);
    // Bounding the reader to the valid bytes makes Huffman over-reads on corrupt
    // streams see zeros without having to clear the buffer.
    let len = bytes_have + frame_bytes;
    (Bs::new(&maindata[..len], len as i32), h.reserv >= main_data_begin)
}

fn l3_decode(h: &mut State, s: &mut Scratch, bs: &mut Bs, gr_info: &[GrInfo], nch: usize) {
    let hdr = Header(h.header);
    for ch in 0..nch {
        let layer3gr_limit = bs.pos + i32::from(gr_info[ch].part_23_length);
        l3_decode_scalefactors(hdr, &mut s.ist_pos[ch], bs, &gr_info[ch], &mut s.scf, ch);
        l3_huffman(&mut s.grbuf[ch], bs, &gr_info[ch], &s.scf, layer3gr_limit);
    }

    let stereo: &mut [f32; 1152] = s.grbuf.as_flattened_mut().try_into().unwrap();
    if hdr.test_i_stereo() {
        l3_intensity_stereo(stereo, &mut s.ist_pos[1], gr_info, hdr);
    } else if hdr.is_ms_stereo() {
        l3_midside_stereo(stereo, 0, 576);
    }

    for ch in 0..nch {
        let gr = &gr_info[ch];
        let mut aa_bands = 31;
        let n_long_bands: usize =
            (if gr.mixed_block_flag != 0 { 2 } else { 0 }) << (hdr.get_my_sample_rate() == 2) as u32;

        if gr.n_short_sfb != 0 {
            aa_bands = n_long_bands.saturating_sub(1);
            l3_reorder(
                &mut s.grbuf[ch][n_long_bands * 18..],
                s.syn.as_flattened_mut(),
                &gr.sfbtab[gr.n_long_sfb as usize..],
            );
        }

        l3_antialias(&mut s.grbuf[ch], aa_bands);
        l3_imdct_gr(&mut s.grbuf[ch], &mut h.mdct_overlap[ch], gr.block_type, n_long_bands);
        l3_change_sign(&mut s.grbuf[ch]);
    }
}

/// DCT-II over columns `k..k + T::N` of the 18x32 subband matrix.
#[inline(always)]
fn mp3d_dct_ii_cols<T: Lanes>(grbuf: &mut [f32; 576], k: usize) {
    let c = T::splat;
    let mut t = [[c(0.0); 8]; 4];
    for i in 0..8 {
        let x0 = T::load(&grbuf[k + i * 18..]);
        let x1 = T::load(&grbuf[k + (15 - i) * 18..]);
        let x2 = T::load(&grbuf[k + (16 + i) * 18..]);
        let x3 = T::load(&grbuf[k + (31 - i) * 18..]);
        let t0 = x0 + x3;
        let t1 = x1 + x2;
        let t2 = (x1 - x2) * c(MP3D_DCT_II_G_SEC[3 * i]);
        let t3 = (x0 - x3) * c(MP3D_DCT_II_G_SEC[3 * i + 1]);
        t[0][i] = t0 + t1;
        t[1][i] = (t0 - t1) * c(MP3D_DCT_II_G_SEC[3 * i + 2]);
        t[2][i] = t3 + t2;
        t[3][i] = (t3 - t2) * c(MP3D_DCT_II_G_SEC[3 * i + 2]);
    }
    for x in &mut t {
        let [mut x0, mut x1, mut x2, mut x3, mut x4, mut x5, mut x6, mut x7] = *x;
        let mut xt = x0 - x7;
        x0 += x7;
        x7 = x1 - x6;
        x1 += x6;
        x6 = x2 - x5;
        x2 += x5;
        x5 = x3 - x4;
        x3 += x4;
        x4 = x0 - x3;
        x0 += x3;
        x3 = x1 - x2;
        x1 += x2;
        x[0] = x0 + x1;
        x[4] = (x0 - x1) * c(0.70710677);
        x5 += x6;
        x6 = (x6 + x7) * c(0.70710677);
        x7 += xt;
        x3 = (x3 + x4) * c(0.70710677);
        x5 -= x7 * c(0.198912367); // rotate by PI/8
        x7 += x5 * c(0.382683432);
        x5 -= x7 * c(0.198912367);
        x0 = xt - x6;
        xt += x6;
        x[1] = (xt + x7) * c(0.50979561);
        x[2] = (x4 + x3) * c(0.54119611);
        x[3] = (x0 - x5) * c(0.60134488);
        x[5] = (x0 + x5) * c(0.89997619);
        x[6] = (x4 - x3) * c(1.30656302);
        x[7] = (xt - x7) * c(2.56291556);
    }
    for i in 0..7 {
        let y = k + i * 4 * 18;
        t[0][i].store(&mut grbuf[y..]);
        (t[2][i] + t[3][i] + t[3][i + 1]).store(&mut grbuf[y + 18..]);
        (t[1][i] + t[1][i + 1]).store(&mut grbuf[y + 2 * 18..]);
        (t[2][i + 1] + t[3][i] + t[3][i + 1]).store(&mut grbuf[y + 3 * 18..]);
    }
    let y = k + 7 * 4 * 18;
    t[0][7].store(&mut grbuf[y..]);
    (t[2][7] + t[3][7]).store(&mut grbuf[y + 18..]);
    t[1][7].store(&mut grbuf[y + 2 * 18..]);
    t[3][7].store(&mut grbuf[y + 3 * 18..]);
}

fn mp3d_dct_ii(grbuf: &mut [f32; 576], n: usize) {
    let mut k = 0;
    while k + 4 <= n {
        mp3d_dct_ii_cols::<F4>(grbuf, k);
        k += 4;
    }
    while k < n {
        mp3d_dct_ii_cols::<f32>(grbuf, k);
        k += 1;
    }
}

/// `mp3d_synth_pair`: the two samples C writes to `pcm[0]` and `pcm[16*nch]`,
/// read from `lane` (and `lane + 2`) of every 16th row starting at `row`.
#[inline(always)]
fn mp3d_synth_pair(z: &[[f32; 4]; SYNTH_ROWS], row: usize, lane: usize) -> (f32, f32) {
    let x = |m: usize| z[row + 16 * m][lane];
    let mut a;
    a = (x(14) - x(0)) * 29.0;
    a += (x(1) + x(13)) * 213.0;
    a += (x(12) - x(2)) * 459.0;
    a += (x(3) + x(11)) * 2037.0;
    a += (x(10) - x(4)) * 5153.0;
    a += (x(5) + x(9)) * 6574.0;
    a += (x(8) - x(6)) * 37489.0;
    a += x(7) * 75038.0;
    let first = a;

    let x = |m: usize| z[row + 16 * m][lane + 2];
    a = x(14) * 104.0;
    a += x(12) * 1567.0;
    a += x(10) * 9727.0;
    a += x(8) * 64019.0;
    a += x(6) * -9975.0;
    a += x(4) * -45.0;
    a += x(2) * 146.0;
    a += x(0) * -5.0;
    (first, a)
}

// The polyphase filterbank history (`lins` in C) is viewed as rows of 4 floats:
// [left even, right even, left odd, right odd]. In C terms, row `r` lane `l` is
// `lins[4*r + l]`, and `zlin = lins + 15*64` starts at row `ZLIN`.
const ZLIN: usize = 15 * 16;
/// Rows of `lins` touched by one call to `mp3d_synth`.
const SYNTH_ROWS: usize = ZLIN + 2 * 16;

fn mp3d_synth<S: Sample, const NCH: usize>(
    xl: &[f32],
    xr: &[f32],
    dst: &mut [S],
    lins: &mut [[f32; 4]; SYNTH_ROWS],
    compat: bool,
) {
    let xl: &[f32; 560] = xl[..560].try_into().unwrap();
    let xr: &[f32; 560] = xr[..560].try_into().unwrap();
    let dst = &mut dst[..64 * NCH];
    let r = NCH - 1;

    lins[ZLIN + 15] = [xl[18 * 16], xr[18 * 16], xl[0], xr[0]];
    lins[ZLIN + 31] = [xl[1 + 18 * 16], xr[1 + 18 * 16], xl[1], xr[1]];

    // For mono, C also computes "right" samples into the same slots and then
    // overwrites them with the left ones; skipping them gives identical output.
    let mut pair = |at: usize, row: usize, lane: usize| {
        let (a, b) = mp3d_synth_pair(lins, row, lane);
        dst[at] = S::from_synth(a, compat);
        dst[at + 16 * NCH] = S::from_synth(b, compat);
    };
    if NCH == 2 {
        pair(r, 15, 1);
        pair(r + 32 * NCH, 31, 1);
    }
    pair(0, 15, 0);
    pair(32 * NCH, 31, 0);

    for i in (0..15).rev() {
        lins[ZLIN + i] = [xl[18 * (31 - i)], xr[18 * (31 - i)], xl[1 + 18 * (31 - i)], xr[1 + 18 * (31 - i)]];
        lins[ZLIN + 16 + i][0] = xl[1 + 18 * (1 + i)];
        lins[ZLIN + 16 + i][1] = xr[1 + 18 * (1 + i)];
        lins[ZLIN - 16 + i][2] = xl[18 * (1 + i)];
        lins[ZLIN - 16 + i][3] = xr[18 * (1 + i)];

        let w: &[f32; 16] = MP3D_SYNTH_G_WIN[(14 - i) * 16..][..16].try_into().unwrap();
        let vz = |k: usize| F4::load(&lins[ZLIN + i - 16 * k]);
        let vy = |k: usize| F4::load(&lins[i + 16 * k]);
        let w0 = |k: usize| F4::splat(w[2 * k]);
        let w1 = |k: usize| F4::splat(w[2 * k + 1]);
        let mut b = vz(0) * w1(0) + vy(0) * w0(0);
        let mut a = vz(0) * w0(0) - vy(0) * w1(0);
        for k in [1, 3, 5, 7] {
            b += vz(k) * w1(k) + vy(k) * w0(k);
            a += vy(k) * w1(k) - vz(k) * w0(k);
            if k < 7 {
                let k = k + 1;
                b += vz(k) * w1(k) + vy(k) * w0(k);
                a += vz(k) * w0(k) - vy(k) * w1(k);
            }
        }
        let (a, b) = (a.to_array(), b.to_array());

        if NCH == 2 {
            dst[r + (15 - i) * NCH] = S::from_synth(a[1], compat);
            dst[r + (17 + i) * NCH] = S::from_synth(b[1], compat);
        }
        dst[(15 - i) * NCH] = S::from_synth(a[0], compat);
        dst[(17 + i) * NCH] = S::from_synth(b[0], compat);
        if NCH == 2 {
            dst[r + (47 - i) * NCH] = S::from_synth(a[3], compat);
            dst[r + (49 + i) * NCH] = S::from_synth(b[3], compat);
        }
        dst[(47 - i) * NCH] = S::from_synth(a[2], compat);
        dst[(49 + i) * NCH] = S::from_synth(b[2], compat);
    }
}

fn mp3d_synth_all<S: Sample, const NCH: usize>(
    grbuf: &[f32],
    nbands: usize,
    pcm: &mut [S],
    lins: &mut [[f32; 4]; SYN_ROWS],
    compat: bool,
) {
    for i in (0..nbands).step_by(2) {
        mp3d_synth::<S, NCH>(
            &grbuf[i..],
            &grbuf[576 * (NCH - 1) + i..],
            &mut pcm[32 * NCH * i..],
            (&mut lins[16 * i..16 * i + SYNTH_ROWS]).try_into().unwrap(),
            compat,
        );
    }
}

/// Rows in the whole synthesis buffer (`syn` in C: `(18 + 15) * 2 * 32` floats).
const SYN_ROWS: usize = (18 + 15) * 2 * 32 / 4;

fn mp3d_synth_granule<S: Sample>(
    qmf_state: &mut [f32; 960],
    grbuf: &mut [[f32; 576]; 2],
    nbands: usize,
    nch: usize,
    pcm: &mut [S],
    lins: &mut [[f32; 4]; SYN_ROWS],
    compat: bool,
) {
    for ch in &mut grbuf[..nch] {
        mp3d_dct_ii(ch, nbands);
    }

    lins.as_flattened_mut()[..15 * 64].copy_from_slice(qmf_state);

    let grbuf = grbuf.as_flattened();
    if nch == 1 {
        mp3d_synth_all::<S, 1>(grbuf, nbands, pcm, lins, compat);
    } else {
        mp3d_synth_all::<S, 2>(grbuf, nbands, pcm, lins, compat);
    }

    let tail = &lins.as_flattened()[nbands * 64..nbands * 64 + 15 * 64];
    if nch == 1 {
        // Standard (not MINIMP3_NONSTANDARD_BUT_LOGICAL) behavior: a mono frame
        // only advances the left channel's filterbank history, so a later
        // switch to stereo starts the right channel from its old state.
        for (q, l) in qmf_state.as_chunks_mut::<2>().0.iter_mut().zip(tail.as_chunks::<2>().0) {
            q[0] = l[0];
        }
    } else {
        qmf_state.copy_from_slice(tail);
    }
}

fn mp3d_match_frame(hdr: &[u8], frame_bytes: usize) -> bool {
    let first = Header::read(hdr);
    let mut i = 0;
    for nmatch in 0..MAX_FRAME_SYNC_MATCHES {
        let h = Header::read(&hdr[i..]);
        i += h.frame_bytes(frame_bytes) + h.padding();
        if i + HDR_SIZE > hdr.len() {
            return nmatch > 0;
        }
        if !first.compare(Header::read(&hdr[i..])) {
            return false;
        }
    }
    true
}

/// Returns `(offset, frame_bytes)`; `frame_bytes` is 0 when no frame was found.
pub fn mp3d_find_frame(mp3: &[u8], free_format_bytes: &mut usize) -> (usize, usize) {
    let mp3_bytes = mp3.len();
    let mut i = 0;
    while i + HDR_SIZE < mp3_bytes {
        let buf = &mp3[i..];
        let h = Header::read(buf);
        if h.valid() {
            let mut frame_bytes = h.frame_bytes(*free_format_bytes);
            let mut frame_and_padding = frame_bytes + h.padding();

            let mut k = HDR_SIZE;
            while frame_bytes == 0 && k < MAX_FREE_FORMAT_FRAME_SIZE && i + 2 * k + HDR_SIZE < mp3_bytes {
                let next = Header::read(&buf[k..]);
                if h.compare(next) {
                    let fb = k - h.padding();
                    let nextfb = fb + next.padding();
                    if i + k + nextfb + HDR_SIZE <= mp3_bytes && h.compare(Header::read(&buf[k + nextfb..])) {
                        frame_and_padding = k;
                        frame_bytes = fb;
                        *free_format_bytes = fb;
                    }
                }
                k += 1;
            }

            if (frame_bytes != 0 && i + frame_and_padding <= mp3_bytes && mp3d_match_frame(buf, frame_bytes))
                || (i == 0 && frame_and_padding == mp3_bytes)
            {
                return (i, frame_and_padding);
            }
            *free_format_bytes = 0;
        }
        i += 1;
    }
    (mp3_bytes, 0)
}

/// `mp3dec_init`: forces a full resynchronization (and state reset) on the
/// next decode.
pub fn mp3dec_init(dec: &mut Mp3Dec) {
    dec.st.header[0] = 0;
}

/// Parses the Layer III side info of `frame` (header included) as a frame of
/// `frame_size` bytes, like minimp3_ex does for VBR tags and seeking. Returns
/// the bit reader's `(pos, limit)` afterwards, relative to the end of the
/// header, or `None` if the side info is invalid.
pub fn l3_side_info(frame: &[u8], frame_size: usize) -> Option<(i32, i32)> {
    let hdr = Header::read(frame);
    let body = &frame[HDR_SIZE..];
    // C trusts frame_size; clamp it so a truncated frame can't be over-read.
    let mut bs = Bs::new(body, (frame_size as i32 - HDR_SIZE as i32).min(body.len() as i32));
    if hdr.is_crc() {
        bs.get_bits(16);
    }
    let mut gr_info = [GrInfo::default(); 4];
    (l3_read_side_info(&mut bs, &mut gr_info, hdr) >= 0).then_some((bs.pos, bs.limit))
}

/// `mp3dec_decode_frame`. With `pcm == None`, only parses the frame header and
/// returns the number of samples the frame would produce (per channel).
/// `compat` selects minimp3's exact sample conversion (see [`Sample`]).
pub fn mp3dec_decode_frame<S: Sample>(
    dec: &mut Mp3Dec,
    mp3: &[u8],
    pcm: Option<&mut [S]>,
    info: &mut FrameInfo,
    compat: bool,
) -> usize {
    let Mp3Dec { st: dec, scratch, maindata } = dec;
    let mp3_bytes = mp3.len();
    let mut i = 0;
    let mut frame_size = 0;

    if mp3_bytes > 4 && dec.header[0] == 0xff && Header(dec.header).compare(Header::read(mp3)) {
        let h = Header::read(mp3);
        frame_size = h.frame_bytes(dec.free_format_bytes) + h.padding();
        if frame_size != mp3_bytes
            && (frame_size + HDR_SIZE > mp3_bytes || !h.compare(Header::read(&mp3[frame_size..])))
        {
            frame_size = 0;
        }
    }
    if frame_size == 0 {
        *dec = State::new();
        (i, frame_size) = mp3d_find_frame(mp3, &mut dec.free_format_bytes);
        if frame_size == 0 || i + frame_size > mp3_bytes {
            info.frame_bytes = i;
            info.status = Status::NoFrame;
            return 0;
        }
    }

    let hdr = Header::read(&mp3[i..]);
    dec.header = hdr.0;
    info.frame_bytes = i + frame_size;
    info.frame_offset = i;
    info.channels = if hdr.is_mono() { 1 } else { 2 };
    info.hz = hdr.sample_rate_hz();
    info.layer = 4 - hdr.get_layer();
    info.bitrate_kbps = hdr.bitrate_kbps();

    let Some(pcm) = pcm else {
        info.status = Status::HeaderOnly;
        return hdr.frame_samples() as usize;
    };

    let frame = mp3.get(i + HDR_SIZE..i + frame_size).unwrap_or(&[]);
    let mut bs_frame = Bs::new(frame, frame_size as i32 - HDR_SIZE as i32);
    if hdr.is_crc() {
        bs_frame.get_bits(16);
    }

    if info.layer != 3 {
        #[cfg(feature = "layer12")]
        {
            let n = layer12::decode_frame(dec, scratch, hdr, &mut bs_frame, pcm, info.channels as usize, compat);
            info.status = if n == 0 { Status::Corrupt } else { Status::Decoded };
            return n;
        }
        #[cfg(not(feature = "layer12"))]
        {
            info.status = Status::UnsupportedLayer;
            return 0;
        }
    }

    let nch = info.channels as usize;
    let mut gr_info = [GrInfo::default(); 4];
    let main_data_begin = l3_read_side_info(&mut bs_frame, &mut gr_info, hdr);
    if main_data_begin < 0 || bs_frame.pos > bs_frame.limit {
        dec.header[0] = 0; // mp3dec_init
        info.status = Status::Corrupt;
        return 0;
    }

    let (mut bs, success) = l3_restore_reservoir(dec, &bs_frame, maindata, main_data_begin);
    if success {
        // C leaves these uninitialized and corrupt streams can read them before
        // writing; clearing them keeps results deterministic.
        scratch.scf = [0.; 40];
        scratch.ist_pos = [[0; 39]; 2];
        let granules = if hdr.test_mpeg1() { 2 } else { 1 };
        for (igr, pcm) in pcm.chunks_exact_mut(576 * nch).take(granules).enumerate() {
            scratch.grbuf = [[0.; 576]; 2];
            l3_decode(dec, scratch, &mut bs, &gr_info[igr * nch..], nch);
            mp3d_synth_granule(&mut dec.qmf_state, &mut scratch.grbuf, 18, nch, pcm, &mut scratch.syn, compat);
        }
    }
    l3_save_reservoir(dec, &bs);
    info.status = if success { Status::Decoded } else { Status::ReservoirUnavailable };
    success as usize * Header(dec.header).frame_samples() as usize
}