oxideav-ac4 0.0.7

Pure-Rust Dolby AC-4 audio decoder foundation for oxideav — sync, TOC, presentation and substream parsing
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
//! Dynamic Range Control (DRC) metadata parsing — §4.2.14.5..10 syntax,
//! §4.3.13 semantics. Pure parser: walks the bitstream and surfaces the
//! decoded fields, plus the per-(channel, subframe, band) DRC gain
//! matrix decoded via the Annex A.5 Huffman codebook
//! ([`crate::drc_huffman`]).
//!
//! Entry point: [`parse_drc_frame`] (§4.2.14.5 Table 70). It consumes
//! `b_drc_present`, optionally `drc_config()` on I-frames, and the
//! per-frame `drc_data()` payload that carries the gain stream.
//!
//! Out of scope here:
//! * Application of the gains to PCM samples (§5.7.9 DRC tool).
//! * E-AC-3 transcoding profile lookup beyond surfacing the
//!   [`DrcConfig::drc_eac3_profile`] field.
//! * Skipped `drc2_bits` payload (`drc_version > 0`) — bits are read
//!   and counted but their content is not interpreted.

use oxideav_core::bits::BitReader;
use oxideav_core::{Error, Result};

use crate::drc_huffman::drc_huff_decode_diff;
use crate::toc::variable_bits;

/// Maximum number of DRC decoder modes per `drc_decoder_nr_modes` (§4.3.13.2.1):
/// the field is 3 bits and the mode count is `value + 1`, so 1..=8.
pub const DRC_MAX_DECODER_MODES: usize = 8;

/// Decoded DRC compression curve (§4.2.14.8 Table 73, §4.3.13.4 semantics).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct DrcCompressionCurve {
    pub drc_lev_nullband_low: u8,
    pub drc_lev_nullband_high: u8,
    pub drc_gain_max_boost: u8,
    pub drc_lev_max_boost: Option<u8>,
    pub drc_nr_boost_sections: Option<u8>,
    pub drc_gain_section_boost: Option<u8>,
    pub drc_lev_section_boost: Option<u8>,
    pub drc_gain_max_cut: u8,
    pub drc_lev_max_cut: Option<u8>,
    pub drc_nr_cut_sections: Option<u8>,
    pub drc_gain_section_cut: Option<u8>,
    pub drc_lev_section_cut: Option<u8>,
    pub drc_tc_default_flag: bool,
    /// `Some(_)` when `!drc_tc_default_flag`: per-section time constants.
    pub time_constants: Option<DrcTimeConstants>,
}

/// Optional per-decoder-mode time-constant block (§4.3.13.4.14..21).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct DrcTimeConstants {
    pub drc_tc_attack: u8,
    pub drc_tc_release: u8,
    pub drc_tc_attack_fast: u8,
    pub drc_tc_release_fast: u8,
    pub drc_adaptive_smoothing_flag: bool,
    pub drc_attack_threshold: Option<u8>,
    pub drc_release_threshold: Option<u8>,
}

/// One `drc_decoder_mode_config()` element (§4.2.14.7 Table 72).
#[derive(Debug, Clone, PartialEq)]
pub struct DrcDecoderMode {
    pub drc_decoder_mode_id: u8,
    pub drc_output_level_from: Option<u8>,
    pub drc_output_level_to: Option<u8>,
    pub drc_repeat_profile_flag: bool,
    /// `Some(id)` when `drc_repeat_profile_flag` is set.
    pub drc_repeat_id: Option<u8>,
    /// `drc_default_profile_flag`. Only present when `!repeat_profile_flag`.
    pub drc_default_profile_flag: Option<bool>,
    /// True when this mode carries (or repeats) a compression curve;
    /// false when it carries gainset entries via `drc_gains_config`.
    pub drc_compression_curve_flag: bool,
    /// `Some(curve)` when this mode transmits its own compression curve.
    pub compression_curve: Option<DrcCompressionCurve>,
    /// `Some(value)` when `!compression_curve_flag` for this mode (gainset).
    pub drc_gains_config: Option<u8>,
}

/// Decoded `drc_config()` (§4.2.14.6 Table 71).
#[derive(Debug, Clone, PartialEq)]
pub struct DrcConfig {
    /// Per spec: number of decoder modes is `drc_decoder_nr_modes + 1`,
    /// so this field stores the *raw 3-bit value*. Use [`Self::nr_modes`]
    /// for the count.
    pub drc_decoder_nr_modes: u8,
    pub drc_eac3_profile: u8,
    pub modes: Vec<DrcDecoderMode>,
}

impl DrcConfig {
    /// Returns `drc_decoder_nr_modes + 1` per §4.3.13.2.1.
    pub fn nr_modes(&self) -> usize {
        self.drc_decoder_nr_modes as usize + 1
    }
}

/// Decoded `drc_gains()` payload (§4.2.14.10 Table 75).
///
/// `drc_gain[ch][sf][band]` flattened in `(ch, band, sf)` walk order
/// per the spec loop. Index helper: [`DrcGains::idx`].
#[derive(Debug, Clone, PartialEq)]
pub struct DrcGains {
    pub mode_id: u8,
    pub nr_drc_channels: u8,
    pub nr_drc_subframes: u8,
    pub nr_drc_bands: u8,
    /// 7-bit `drc_gain_val` (§4.3.13.6.1); `drc_gain[0][0][0] = val - 64 [dB]`.
    pub drc_gain_val: u8,
    /// All decoded gains as 7-bit unsigned values (-64 dB offset applies).
    /// Length = `nr_drc_channels * nr_drc_subframes * nr_drc_bands`.
    pub drc_gain: Vec<u8>,
}

impl DrcGains {
    /// Walk-order flattened index for `(ch, sf, band)` — matches the
    /// spec's three nested loops: `for ch { for band { for sf {} } }`.
    pub fn idx(&self, ch: usize, sf: usize, band: usize) -> usize {
        let bands = self.nr_drc_bands as usize;
        let sfs = self.nr_drc_subframes as usize;
        ch * bands * sfs + band * sfs + sf
    }

    /// `drc_gain[ch][sf][band]` in dB per §4.3.13.6.1 (`val - 64`).
    pub fn gain_db(&self, ch: usize, sf: usize, band: usize) -> i32 {
        self.drc_gain[self.idx(ch, sf, band)] as i32 - 64
    }
}

/// Decoded `drc_data()` (§4.2.14.9 Table 74) plus framing info that the
/// decoder needs in order to apply the gains.
#[derive(Debug, Clone, PartialEq)]
pub struct DrcData {
    /// One `DrcGains` per decoder mode that has `compression_curve_flag == 0`.
    /// Modes with curves don't carry per-frame gains.
    pub gainsets: Vec<DrcGains>,
    /// Set when at least one mode in this frame is curve-driven.
    pub curve_present: bool,
    /// Only meaningful when `curve_present`.
    pub drc_reset_flag: bool,
    /// Reserved 2 bits (§4.3.13.5.6) — surfaced for round-trip fidelity.
    pub drc_reserved: u8,
}

/// Top-level decoded `drc_frame()` (§4.2.14.5 Table 70).
#[derive(Debug, Clone, PartialEq)]
pub struct DrcFrame {
    pub b_drc_present: bool,
    /// I-frame configuration. `Some` when `b_drc_present && b_iframe`.
    pub config: Option<DrcConfig>,
    /// Per-frame data. `Some` when `b_drc_present`.
    pub data: Option<DrcData>,
}

/// Channel-config descriptor needed to decode `drc_data()` because the
/// gain-set walk depends on `nr_drc_channels` (§4.3.13.7.1 Table 168)
/// and `nr_drc_subframes` (§4.3.13.7.2 Table 169).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DrcChannelInfo {
    /// From Table 168 — depends on the speaker configuration of the frame.
    pub nr_drc_channels: u8,
    /// From Table 169 — depends on the AC-4 frame length.
    pub nr_drc_subframes: u8,
}

impl DrcChannelInfo {
    pub fn new(nr_drc_channels: u8, nr_drc_subframes: u8) -> Self {
        Self {
            nr_drc_channels,
            nr_drc_subframes,
        }
    }
}

/// Map an `nr_drc_subframes` value from the AC-4 frame length per
/// Table 169 (§4.3.13.7.2). Returns `None` for frame lengths not in
/// the table.
pub fn nr_drc_subframes(frame_length: u32) -> Option<u8> {
    match frame_length {
        384 => Some(1),
        512 => Some(2),
        768 | 960 => Some(3),
        1024 => Some(4),
        1536 | 1920 => Some(6),
        2048 => Some(8),
        _ => None,
    }
}

/// Map `nr_drc_channels` per Table 168 (§4.3.13.7.1). The `channel_mode`
/// here is the decoded `channel_mode` value from `ac4_substream_info()`
/// (mono = 0, stereo = 1, 5.1 = 4 .. etc per Annex H). Falls back to
/// the most common groups when the configuration is exotic; callers
/// expecting deterministic behaviour for unhandled modes should
/// override this.
pub fn nr_drc_channels(channel_mode: u32) -> u8 {
    // Table 168 maps Mono/Stereo to 1, the rest of the listed configs
    // (5.1 / 7.1 variants) to 3. The spec is silent about other
    // channel modes; for them we default to 1 wideband group and let
    // the bitstream's drc_gainset_size carry the truth.
    match channel_mode {
        0 | 1 => 1, // Mono, Stereo
        _ => 3,     // 5.1, 7.1 family per Table 168
    }
}

/// Map `drc_gains_config` value to `nr_drc_bands` per Table 163.
pub fn nr_drc_bands(drc_gains_config: u8) -> u8 {
    match drc_gains_config {
        0 | 1 => 1,
        2 => 2,
        3 => 4,
        _ => 1, // Reserved values are out of range for a 2-bit field.
    }
}

/// `drc_frame(b_iframe)` per §4.2.14.5 Table 70.
///
/// `b_iframe` controls whether `drc_config()` is present (only on
/// I-frames). `chan_info` provides the channel-configuration-dependent
/// `nr_drc_channels` / `nr_drc_subframes` constants needed to decode
/// the gainset payload. `prev_config` is the configuration carried by
/// a prior I-frame in this stream — when `b_drc_present` is true on a
/// non-I-frame, the parser falls back on `prev_config` to know how
/// many decoder modes (and which `compression_curve_flag` /
/// `drc_gains_config` they have) are in flight.
pub fn parse_drc_frame(
    br: &mut BitReader<'_>,
    b_iframe: bool,
    chan_info: DrcChannelInfo,
    prev_config: Option<&DrcConfig>,
) -> Result<DrcFrame> {
    let b_drc_present = br.read_bit()?;
    if !b_drc_present {
        return Ok(DrcFrame {
            b_drc_present: false,
            config: None,
            data: None,
        });
    }

    let config = if b_iframe {
        Some(parse_drc_config(br)?)
    } else {
        None
    };

    let active = config.as_ref().or(prev_config).ok_or_else(|| {
        Error::invalid(
            "ac4: drc_frame on non-I-frame with no prior drc_config — \
             cannot decode drc_data() without per-mode flags",
        )
    })?;

    let data = parse_drc_data(br, active, chan_info)?;

    Ok(DrcFrame {
        b_drc_present: true,
        config,
        data: Some(data),
    })
}

/// `drc_config()` per §4.2.14.6 Table 71.
pub fn parse_drc_config(br: &mut BitReader<'_>) -> Result<DrcConfig> {
    let drc_decoder_nr_modes = br.read_u32(3)? as u8;
    let mode_count = drc_decoder_nr_modes as usize + 1;
    let mut modes = Vec::with_capacity(mode_count);
    for _ in 0..mode_count {
        modes.push(parse_drc_decoder_mode_config(br, &modes)?);
    }
    let drc_eac3_profile = br.read_u32(3)? as u8;
    Ok(DrcConfig {
        drc_decoder_nr_modes,
        drc_eac3_profile,
        modes,
    })
}

/// `drc_decoder_mode_config()` per §4.2.14.7 Table 72.
///
/// The `drc_repeat_profile_flag` path duplicates the
/// `drc_compression_curve_flag` value from the referenced (already
/// decoded) mode, so callers must pass the modes decoded so far via
/// `prior_modes`. If the reference mode is missing the function
/// errors out — that's a malformed bitstream.
fn parse_drc_decoder_mode_config(
    br: &mut BitReader<'_>,
    prior_modes: &[DrcDecoderMode],
) -> Result<DrcDecoderMode> {
    let drc_decoder_mode_id = br.read_u32(3)? as u8;
    let (drc_output_level_from, drc_output_level_to) = if drc_decoder_mode_id > 3 {
        (Some(br.read_u32(5)? as u8), Some(br.read_u32(5)? as u8))
    } else {
        (None, None)
    };

    let drc_repeat_profile_flag = br.read_bit()?;
    if drc_repeat_profile_flag {
        let drc_repeat_id = br.read_u32(3)? as u8;
        // Per spec: drc_compression_curve_flag[mode_id]
        //          = drc_compression_curve_flag[drc_repeat_id].
        let referenced = prior_modes
            .iter()
            .find(|m| m.drc_decoder_mode_id == drc_repeat_id)
            .ok_or_else(|| Error::invalid("ac4: drc_repeat_id refers to undeclared mode"))?;
        Ok(DrcDecoderMode {
            drc_decoder_mode_id,
            drc_output_level_from,
            drc_output_level_to,
            drc_repeat_profile_flag: true,
            drc_repeat_id: Some(drc_repeat_id),
            drc_default_profile_flag: None,
            drc_compression_curve_flag: referenced.drc_compression_curve_flag,
            compression_curve: None, // Curve lives on the referenced mode.
            drc_gains_config: referenced.drc_gains_config,
        })
    } else {
        let drc_default_profile_flag = br.read_bit()?;
        if drc_default_profile_flag {
            // Default-profile mode: compression_curve_flag is implicitly
            // 1 per §4.2.14.7 last `else { ... = 1; }` branch.
            Ok(DrcDecoderMode {
                drc_decoder_mode_id,
                drc_output_level_from,
                drc_output_level_to,
                drc_repeat_profile_flag: false,
                drc_repeat_id: None,
                drc_default_profile_flag: Some(true),
                drc_compression_curve_flag: true,
                compression_curve: None,
                drc_gains_config: None,
            })
        } else {
            let drc_compression_curve_flag = br.read_bit()?;
            if drc_compression_curve_flag {
                let curve = parse_drc_compression_curve(br)?;
                Ok(DrcDecoderMode {
                    drc_decoder_mode_id,
                    drc_output_level_from,
                    drc_output_level_to,
                    drc_repeat_profile_flag: false,
                    drc_repeat_id: None,
                    drc_default_profile_flag: Some(false),
                    drc_compression_curve_flag: true,
                    compression_curve: Some(curve),
                    drc_gains_config: None,
                })
            } else {
                let drc_gains_config = br.read_u32(2)? as u8;
                Ok(DrcDecoderMode {
                    drc_decoder_mode_id,
                    drc_output_level_from,
                    drc_output_level_to,
                    drc_repeat_profile_flag: false,
                    drc_repeat_id: None,
                    drc_default_profile_flag: Some(false),
                    drc_compression_curve_flag: false,
                    compression_curve: None,
                    drc_gains_config: Some(drc_gains_config),
                })
            }
        }
    }
}

/// `drc_compression_curve()` per §4.2.14.8 Table 73.
pub fn parse_drc_compression_curve(br: &mut BitReader<'_>) -> Result<DrcCompressionCurve> {
    let drc_lev_nullband_low = br.read_u32(4)? as u8;
    let drc_lev_nullband_high = br.read_u32(4)? as u8;
    let drc_gain_max_boost = br.read_u32(4)? as u8;
    let (drc_lev_max_boost, drc_nr_boost_sections, drc_gain_section_boost, drc_lev_section_boost) =
        if drc_gain_max_boost > 0 {
            let lmb = br.read_u32(5)? as u8;
            let nbs = br.read_u32(1)? as u8;
            if nbs > 0 {
                (
                    Some(lmb),
                    Some(nbs),
                    Some(br.read_u32(4)? as u8),
                    Some(br.read_u32(5)? as u8),
                )
            } else {
                (Some(lmb), Some(nbs), None, None)
            }
        } else {
            (None, None, None, None)
        };

    let drc_gain_max_cut = br.read_u32(5)? as u8;
    let (drc_lev_max_cut, drc_nr_cut_sections, drc_gain_section_cut, drc_lev_section_cut) =
        if drc_gain_max_cut > 0 {
            let lmc = br.read_u32(6)? as u8;
            let ncs = br.read_u32(1)? as u8;
            if ncs > 0 {
                (
                    Some(lmc),
                    Some(ncs),
                    Some(br.read_u32(5)? as u8),
                    Some(br.read_u32(5)? as u8),
                )
            } else {
                (Some(lmc), Some(ncs), None, None)
            }
        } else {
            (None, None, None, None)
        };

    let drc_tc_default_flag = br.read_bit()?;
    let time_constants = if !drc_tc_default_flag {
        let drc_tc_attack = br.read_u32(8)? as u8;
        let drc_tc_release = br.read_u32(8)? as u8;
        let drc_tc_attack_fast = br.read_u32(8)? as u8;
        let drc_tc_release_fast = br.read_u32(8)? as u8;
        let drc_adaptive_smoothing_flag = br.read_bit()?;
        let (drc_attack_threshold, drc_release_threshold) = if drc_adaptive_smoothing_flag {
            (Some(br.read_u32(5)? as u8), Some(br.read_u32(5)? as u8))
        } else {
            (None, None)
        };
        Some(DrcTimeConstants {
            drc_tc_attack,
            drc_tc_release,
            drc_tc_attack_fast,
            drc_tc_release_fast,
            drc_adaptive_smoothing_flag,
            drc_attack_threshold,
            drc_release_threshold,
        })
    } else {
        None
    };

    Ok(DrcCompressionCurve {
        drc_lev_nullband_low,
        drc_lev_nullband_high,
        drc_gain_max_boost,
        drc_lev_max_boost,
        drc_nr_boost_sections,
        drc_gain_section_boost,
        drc_lev_section_boost,
        drc_gain_max_cut,
        drc_lev_max_cut,
        drc_nr_cut_sections,
        drc_gain_section_cut,
        drc_lev_section_cut,
        drc_tc_default_flag,
        time_constants,
    })
}

/// `drc_data()` per §4.2.14.9 Table 74.
pub fn parse_drc_data(
    br: &mut BitReader<'_>,
    config: &DrcConfig,
    chan_info: DrcChannelInfo,
) -> Result<DrcData> {
    let mut curve_present = false;
    let mut gainsets = Vec::new();

    for mode in &config.modes {
        let mode_id = mode.drc_decoder_mode_id;
        if !mode.drc_compression_curve_flag {
            // Gainset payload: drc_gainset_size (6 bits, optionally
            // extended via variable_bits(2)), drc_version (2 bits),
            // then drc_gains() if version <= 1, then optional
            // drc2_bits filler.
            let mut drc_gainset_size = br.read_u32(6)?;
            let b_more_bits = br.read_bit()?;
            if b_more_bits {
                drc_gainset_size += variable_bits(br, 2)? << 6;
            }
            let drc_version = br.read_u32(2)?;
            let bit_pos_before = br.bit_position();
            let gainset = if drc_version <= 1 {
                let gains =
                    parse_drc_gains(br, mode_id, mode.drc_gains_config.unwrap_or(0), chan_info)?;
                Some(gains)
            } else {
                None
            };
            let used_bits = (br.bit_position() - bit_pos_before) as u32;
            // drc_gainset_size is the bit-count of the drc_gains payload
            // *plus* the drc_version bits per the inner equation
            // `bits_left = drc_gainset_size - 2 - used_bits`. Skip any
            // residual drc2_bits.
            if drc_version >= 1 {
                let bits_left = drc_gainset_size
                    .checked_sub(2)
                    .and_then(|v| v.checked_sub(used_bits))
                    .ok_or_else(|| {
                        Error::invalid(
                            "ac4: drc_gainset_size accounting underflow \
                             (drc_gains used more bits than declared)",
                        )
                    })?;
                if bits_left > 0 {
                    br.skip(bits_left)?;
                }
            }
            if let Some(g) = gainset {
                gainsets.push(g);
            }
        } else {
            curve_present = true;
        }
    }

    let (drc_reset_flag, drc_reserved) = if curve_present {
        let reset = br.read_bit()?;
        let reserved = br.read_u32(2)? as u8;
        (reset, reserved)
    } else {
        (false, 0)
    };

    Ok(DrcData {
        gainsets,
        curve_present,
        drc_reset_flag,
        drc_reserved,
    })
}

/// `drc_gains(mode)` per §4.2.14.10 Table 75.
///
/// The first gain `drc_gain[0][0][0]` is sent as a 7-bit
/// `drc_gain_val` (offset by -64 dB at use-time). All subsequent
/// (ch, band, sf) gains are deltas decoded via the Annex A.5 codebook
/// (`huff_decode_diff(DRC_HCB, drc_gain_code)`). The spec resets
/// `ref_drc_gain` to `drc_gain[ch][0][band]` between bands and to
/// `drc_gain[ch][0][0]` between channels.
pub fn parse_drc_gains(
    br: &mut BitReader<'_>,
    mode_id: u8,
    drc_gains_config_value: u8,
    chan_info: DrcChannelInfo,
) -> Result<DrcGains> {
    let drc_gain_val = br.read_u32(7)? as u8;
    let nr_drc_bands = nr_drc_bands(drc_gains_config_value);

    // `drc_gains_config == 0` means a single wideband gain shared by
    // all channels (just the 7-bit drc_gain_val).
    if drc_gains_config_value == 0 {
        return Ok(DrcGains {
            mode_id,
            nr_drc_channels: 1,
            nr_drc_subframes: 1,
            nr_drc_bands: 1,
            drc_gain_val,
            drc_gain: vec![drc_gain_val],
        });
    }

    let nr_ch = chan_info.nr_drc_channels as usize;
    let nr_sf = chan_info.nr_drc_subframes as usize;
    let nr_bd = nr_drc_bands as usize;
    if nr_ch == 0 || nr_sf == 0 || nr_bd == 0 {
        return Err(Error::invalid(
            "ac4: drc_gains() invalid (ch, sf, band) dimensions",
        ));
    }

    let mut gains = vec![0u8; nr_ch * nr_bd * nr_sf];
    // gain[ch][sf][band] = ref + diff
    // Walk order matches the spec: ch outer, band middle, sf inner.
    let mut ref_gain: i32 = drc_gain_val as i32;
    let mut first = true;
    let idx =
        |ch: usize, sf: usize, band: usize| -> usize { ch * nr_bd * nr_sf + band * nr_sf + sf };

    for ch in 0..nr_ch {
        // Reset reference between channels per spec — see the trailing
        // `ref_drc_gain = drc_gain[ch][0][0];` after the channel loop.
        // For ch == 0 this is the seed value drc_gain_val.
        for band in 0..nr_bd {
            // Reset reference between bands per spec — see the trailing
            // `ref_drc_gain = drc_gain[ch][0][band];` after the band
            // loop. For band == 0 we keep the channel reset.
            for sf in 0..nr_sf {
                if first {
                    // First slot is the seed drc_gain[0][0][0].
                    gains[idx(ch, sf, band)] = drc_gain_val;
                    first = false;
                } else {
                    let diff = drc_huff_decode_diff(br)?;
                    let g = ref_gain + diff;
                    if !(0..=127).contains(&g) {
                        return Err(Error::invalid(
                            "ac4: DRC gain out of 7-bit range after diff",
                        ));
                    }
                    gains[idx(ch, sf, band)] = g as u8;
                }
                ref_gain = gains[idx(ch, sf, band)] as i32;
            }
            // Spec reset at end of band: ref = drc_gain[ch][0][band].
            ref_gain = gains[idx(ch, 0, band)] as i32;
        }
        // Spec reset at end of channel: ref = drc_gain[ch][0][0].
        ref_gain = gains[idx(ch, 0, 0)] as i32;
    }

    Ok(DrcGains {
        mode_id,
        nr_drc_channels: chan_info.nr_drc_channels,
        nr_drc_subframes: chan_info.nr_drc_subframes,
        nr_drc_bands,
        drc_gain_val,
        drc_gain: gains,
    })
}

// ---------------------------------------------------------------------
// §5.7.9.3.3 PCM gain application
// ---------------------------------------------------------------------

/// Convert a 7-bit `drc_gain[ch][sf][band]` raw value into a linear
/// per-sample multiplier per §5.7.9.3.3:
///
/// `g = 2^(drc_gain_dB / 6)`
///
/// where `drc_gain_dB = drc_gain - 64` (the -64 dB offset from
/// §4.3.13.6.1 — `drc_gain_val` is unsigned 0..=127, mapping to
/// -64..=+63 dB).
#[inline]
pub fn drc_raw_to_linear(drc_gain_raw: u8) -> f32 {
    let db = drc_gain_raw as i32 - 64;
    // 2^(dB/6). The factor of 1/6 vs the more common 1/20 is because
    // AC-4 transmits gains in 6 dB power steps.
    libm_pow2(db as f32 / 6.0)
}

/// Convert a (Lout - Lin) dB difference (dialnorm correction) into the
/// linear scale factor `2^((Lout - Lin) / 6)` per §5.7.9.3.3.
#[inline]
pub fn dialnorm_correction_linear(lout_db: i32, lin_db: i32) -> f32 {
    libm_pow2((lout_db - lin_db) as f32 / 6.0)
}

/// `2^(x)` without pulling in the `libm` crate. Uses `f32::powi` for
/// the integer-exponent fast path, falling back to the IEEE-754 exp /
/// ln path via `f32::exp` for fractional exponents.
#[inline]
fn libm_pow2(x: f32) -> f32 {
    // exp2 isn't in core; ln(2) * x then exp.
    const LN2: f32 = std::f32::consts::LN_2;
    (x * LN2).exp()
}

/// Layout descriptor for [`apply_drc_gains_to_pcm`]: maps the bitstream
/// `nr_drc_channels` channel-group ordering onto the PCM channel
/// indices. `chg_for_pcm_channel[i]` returns which DRC channel-group
/// index drives PCM channel `i`. Per §5.7.9.3.2, for a 5_X channel
/// element with 3 channel groups (`[Lf, Rf, LFE]`, `[Cf]`, `[Ls, Rs]`)
/// the natural mapping is `[0, 0, 1, 0, 2, 2]` (L=0, R=0, C=1, LFE=0,
/// Ls=2, Rs=2).
#[derive(Debug, Clone)]
pub struct DrcChannelMap {
    pub chg_for_pcm_channel: Vec<u8>,
}

impl DrcChannelMap {
    /// Default mapping for mono / stereo where `nr_drc_channels == 1`:
    /// every PCM channel maps to channel-group 0.
    pub fn wideband_single_group(nr_pcm_channels: usize) -> Self {
        Self {
            chg_for_pcm_channel: vec![0; nr_pcm_channels],
        }
    }

    /// 5.0 / 5.1 default: `[L, R, C, LFE?, Ls, Rs]` (LFE optional)
    /// mapped to channel-groups `[0, 0, 1, 0, 2, 2]` per the spec
    /// example in §5.7.9.3.2.
    pub fn five_one_default(includes_lfe: bool) -> Self {
        if includes_lfe {
            Self {
                chg_for_pcm_channel: vec![0, 0, 1, 0, 2, 2],
            }
        } else {
            Self {
                chg_for_pcm_channel: vec![0, 0, 1, 2, 2],
            }
        }
    }
}

/// Apply per-subframe DRC gains to a planar PCM buffer per §5.7.9.3.3.
///
/// `pcm_planar[ch][sample]` is updated in-place. The frame is divided
/// into `gains.nr_drc_subframes` equal subframes; within each subframe
/// the same per-channel-group gain is applied across all bands.
///
/// Multi-band gains are applied as a wideband approximation when
/// `gains.nr_drc_bands > 1`: the per-band gains are averaged in the
/// linear domain before scaling. A full per-band split-band filterbank
/// would belong in the QMF synthesis pipeline; this helper offers a
/// conservative drop-in for callers that just need the time-domain
/// loudness correction.
///
/// Returns the number of samples that were processed (sum across all
/// subframes / channels). Errors when:
/// - `pcm_planar.len() != map.chg_for_pcm_channel.len()`
/// - any `chg_for_pcm_channel[i] >= gains.nr_drc_channels`
/// - any per-channel sample buffer length doesn't divide cleanly into
///   `gains.nr_drc_subframes` (loose tail tolerated — final subframe
///   gets the remainder)
pub fn apply_drc_gains_to_pcm(
    pcm_planar: &mut [Vec<f32>],
    gains: &DrcGains,
    map: &DrcChannelMap,
    dialnorm_correction: f32,
) -> Result<usize> {
    if pcm_planar.len() != map.chg_for_pcm_channel.len() {
        return Err(Error::invalid(
            "ac4: apply_drc_gains_to_pcm: pcm channel count mismatches DrcChannelMap",
        ));
    }
    let nr_chg = gains.nr_drc_channels as usize;
    let nr_sf = gains.nr_drc_subframes as usize;
    let nr_bands = gains.nr_drc_bands as usize;
    if nr_sf == 0 || nr_chg == 0 || nr_bands == 0 {
        return Ok(0);
    }
    for &chg in &map.chg_for_pcm_channel {
        if chg as usize >= nr_chg {
            return Err(Error::invalid(
                "ac4: DrcChannelMap entry out of bounds for DrcGains.nr_drc_channels",
            ));
        }
    }

    // Pre-compute per-(chg, sf) wideband-equivalent linear gains.
    let mut gain_per_chg_sf = vec![0f32; nr_chg * nr_sf];
    for chg in 0..nr_chg {
        for sf in 0..nr_sf {
            let mut acc = 0f32;
            for band in 0..nr_bands {
                let raw = gains.drc_gain[gains.idx(chg, sf, band)];
                acc += drc_raw_to_linear(raw);
            }
            let avg = acc / nr_bands as f32;
            gain_per_chg_sf[chg * nr_sf + sf] = avg * dialnorm_correction;
        }
    }

    let mut total_samples = 0usize;
    for (pcm_ch, ch_buf) in pcm_planar.iter_mut().enumerate() {
        let chg = map.chg_for_pcm_channel[pcm_ch] as usize;
        let n_total = ch_buf.len();
        if n_total == 0 {
            continue;
        }
        let sf_len = n_total / nr_sf;
        if sf_len == 0 {
            // Less than one sample per subframe: just apply the
            // first-subframe gain wideband.
            let g = gain_per_chg_sf[chg * nr_sf];
            for s in ch_buf.iter_mut() {
                *s *= g;
            }
            total_samples += n_total;
            continue;
        }
        for sf in 0..nr_sf {
            let g = gain_per_chg_sf[chg * nr_sf + sf];
            let start = sf * sf_len;
            let end = if sf + 1 == nr_sf {
                n_total
            } else {
                start + sf_len
            };
            for s in ch_buf[start..end].iter_mut() {
                *s *= g;
            }
            total_samples += end - start;
        }
    }

    Ok(total_samples)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::drc_huffman::{DRC_HCB_CW, DRC_HCB_LEN};
    use oxideav_core::bits::BitWriter;

    fn write_zero_diff(bw: &mut BitWriter) {
        // index 127 -> diff 0
        bw.write_u32(DRC_HCB_CW[127], DRC_HCB_LEN[127] as u32);
    }

    fn write_plus_one_diff(bw: &mut BitWriter) {
        // index 128 -> diff +1
        bw.write_u32(DRC_HCB_CW[128], DRC_HCB_LEN[128] as u32);
    }

    fn write_minus_one_diff(bw: &mut BitWriter) {
        // index 126 -> diff -1
        bw.write_u32(DRC_HCB_CW[126], DRC_HCB_LEN[126] as u32);
    }

    #[test]
    fn nr_drc_subframes_table_169() {
        assert_eq!(nr_drc_subframes(384), Some(1));
        assert_eq!(nr_drc_subframes(512), Some(2));
        assert_eq!(nr_drc_subframes(768), Some(3));
        assert_eq!(nr_drc_subframes(960), Some(3));
        assert_eq!(nr_drc_subframes(1024), Some(4));
        assert_eq!(nr_drc_subframes(1536), Some(6));
        assert_eq!(nr_drc_subframes(1920), Some(6));
        assert_eq!(nr_drc_subframes(2048), Some(8));
        assert_eq!(nr_drc_subframes(999), None);
    }

    #[test]
    fn nr_drc_bands_table_163() {
        assert_eq!(nr_drc_bands(0), 1);
        assert_eq!(nr_drc_bands(1), 1);
        assert_eq!(nr_drc_bands(2), 2);
        assert_eq!(nr_drc_bands(3), 4);
    }

    #[test]
    fn parse_drc_frame_absent() {
        // b_drc_present = 0 -> no payload at all.
        let mut bw = BitWriter::new();
        bw.write_bit(false);
        bw.align_to_byte();
        let bytes = bw.finish();
        let mut br = BitReader::new(&bytes);
        let info = DrcChannelInfo::new(1, 1);
        let frame = parse_drc_frame(&mut br, true, info, None).unwrap();
        assert!(!frame.b_drc_present);
        assert!(frame.config.is_none());
        assert!(frame.data.is_none());
    }

    #[test]
    fn parse_drc_frame_one_mode_wideband() {
        // I-frame with one decoder mode, default profile (so curve flag
        // implicitly 1, no gainset payload), no curve transmitted.
        let mut bw = BitWriter::new();
        bw.write_bit(true); // b_drc_present
                            // drc_config():
        bw.write_u32(0, 3); // drc_decoder_nr_modes = 0 -> 1 mode
                            // mode 0:
        bw.write_u32(2, 3); // drc_decoder_mode_id = 2 (Portable Speakers)
                            // mode_id <= 3 so no output_level_from/to.
        bw.write_bit(false); // drc_repeat_profile_flag = 0
        bw.write_bit(true); // drc_default_profile_flag = 1 -> implicit curve flag
        bw.write_u32(1, 3); // drc_eac3_profile = 1 (Film standard)
                            // drc_data(): mode 0 has compression_curve_flag = 1 ->
                            // curve_present = 1, no gainsets, then drc_reset_flag (1b) +
                            // drc_reserved (2b).
        bw.write_bit(false); // drc_reset_flag
        bw.write_u32(0, 2); // drc_reserved
        bw.align_to_byte();
        let bytes = bw.finish();
        let mut br = BitReader::new(&bytes);
        let info = DrcChannelInfo::new(1, 1);
        let frame = parse_drc_frame(&mut br, true, info, None).unwrap();
        assert!(frame.b_drc_present);
        let cfg = frame.config.as_ref().expect("config present");
        assert_eq!(cfg.nr_modes(), 1);
        assert_eq!(cfg.modes[0].drc_decoder_mode_id, 2);
        assert_eq!(
            cfg.modes[0].drc_default_profile_flag,
            Some(true),
            "default profile"
        );
        assert!(cfg.modes[0].drc_compression_curve_flag);
        assert_eq!(cfg.drc_eac3_profile, 1);
        let data = frame.data.as_ref().expect("data present");
        assert!(data.curve_present);
        assert!(data.gainsets.is_empty());
        assert!(!data.drc_reset_flag);
    }

    #[test]
    fn parse_drc_frame_with_gainset_wideband() {
        // I-frame with one decoder mode that carries gainset
        // (drc_gains_config = 1 -> 1 wideband band, channel-dependent
        // gains). Channel info: 1 DRC channel, 2 subframes -> 1*1*2 = 2
        // gain entries. First is drc_gain_val = 64 (=> 0 dB), then one
        // diff.
        let drc_gain_val: u8 = 64;
        // drc_gainset_size accounting: drc_version (2b) + the gain
        // payload bits = drc_gain_val (7) + 1 diff (3 bits for +1) = 10.
        // So drc_gainset_size = 12 (= 2 + 10 inner bits, but the spec
        // formulation is `bits_left = drc_gainset_size - 2 - used_bits`,
        // so size includes drc_version's 2 bits). Thus we set size to
        // 12 (under 64 so single 6-bit field). Total bits = 12, version
        // skipped because 0.
        let drc_gainset_size: u32 = 2 /* drc_version */ + 7 /* drc_gain_val */ + 3 /* +1 diff */;

        let mut bw = BitWriter::new();
        bw.write_bit(true); // b_drc_present
                            // drc_config:
        bw.write_u32(0, 3); // drc_decoder_nr_modes = 0 -> 1 mode
        bw.write_u32(0, 3); // drc_decoder_mode_id = 0 (Home Theatre)
        bw.write_bit(false); // drc_repeat_profile_flag
        bw.write_bit(false); // drc_default_profile_flag = 0
        bw.write_bit(false); // drc_compression_curve_flag = 0 -> gainset
        bw.write_u32(1, 2); // drc_gains_config = 1 (wideband, ch-dep)
        bw.write_u32(0, 3); // drc_eac3_profile = 0 (None)
                            // drc_data:
        bw.write_u32(drc_gainset_size, 6); // drc_gainset_size_value
        bw.write_bit(false); // b_more_bits = 0
        bw.write_u32(0, 2); // drc_version = 0
                            // drc_gains payload starts here:
        bw.write_u32(drc_gain_val as u32, 7);
        write_plus_one_diff(&mut bw);
        bw.align_to_byte();
        let bytes = bw.finish();

        let mut br = BitReader::new(&bytes);
        // Frame length 512 -> 2 subframes (Table 169).
        let info = DrcChannelInfo::new(1, 2);
        let frame = parse_drc_frame(&mut br, true, info, None).unwrap();
        let data = frame.data.as_ref().expect("data");
        assert!(!data.curve_present);
        assert_eq!(data.gainsets.len(), 1);
        let g = &data.gainsets[0];
        assert_eq!(g.nr_drc_channels, 1);
        assert_eq!(g.nr_drc_subframes, 2);
        assert_eq!(g.nr_drc_bands, 1);
        assert_eq!(g.drc_gain_val, 64);
        assert_eq!(g.drc_gain.len(), 2);
        // Gain[0][0][0] = drc_gain_val = 64 -> 0 dB.
        assert_eq!(g.gain_db(0, 0, 0), 0);
        // Gain[0][1][0] = ref(64) + diff(+1) = 65 -> +1 dB.
        assert_eq!(g.gain_db(0, 1, 0), 1);
    }

    #[test]
    fn parse_drc_frame_gainset_2band_2ch_2sf() {
        // 2 channels x 2 subframes x 2 bands = 8 slots, 7 deltas after
        // the seed.
        let drc_gain_val: u8 = 64;
        // Walk order: ch0 band0 sf0 (seed), ch0 band0 sf1 (+1),
        // ch0 band1 sf0 (-1), ch0 band1 sf1 (0), ch1 band0 sf0 (+1),
        // ch1 band0 sf1 (-1), ch1 band1 sf0 (0), ch1 band1 sf1 (+1).
        // After ch0 band1, ref resets to ch0 band1 sf0 = 63.
        // After ch1 (final), no further reset needed.
        let total_diff_bits = 3 + 3 + 2 + 3 + 3 + 2 + 3; // 19 bits

        let mut bw = BitWriter::new();
        bw.write_bit(true); // b_drc_present
        bw.write_u32(0, 3); // drc_decoder_nr_modes = 0 -> 1 mode
        bw.write_u32(0, 3); // drc_decoder_mode_id = 0
        bw.write_bit(false); // drc_repeat_profile_flag
        bw.write_bit(false); // drc_default_profile_flag = 0
        bw.write_bit(false); // drc_compression_curve_flag = 0
        bw.write_u32(2, 2); // drc_gains_config = 2 (2 freq bands)
        bw.write_u32(0, 3); // drc_eac3_profile

        let drc_gainset_size: u32 = 2 + 7 + total_diff_bits;
        bw.write_u32(drc_gainset_size, 6);
        bw.write_bit(false); // b_more_bits
        bw.write_u32(0, 2); // drc_version = 0
        bw.write_u32(drc_gain_val as u32, 7);
        // 7 diffs in walk order:
        write_plus_one_diff(&mut bw);
        write_minus_one_diff(&mut bw);
        write_zero_diff(&mut bw);
        write_plus_one_diff(&mut bw);
        write_minus_one_diff(&mut bw);
        write_zero_diff(&mut bw);
        write_plus_one_diff(&mut bw);
        bw.align_to_byte();
        let bytes = bw.finish();

        let mut br = BitReader::new(&bytes);
        let info = DrcChannelInfo::new(2, 2);
        let frame = parse_drc_frame(&mut br, true, info, None).unwrap();
        let g = &frame.data.as_ref().unwrap().gainsets[0];
        assert_eq!(g.nr_drc_bands, 2);
        assert_eq!(g.drc_gain.len(), 8);

        // Reproduce the expected walk in software:
        //   ref starts at drc_gain_val = 64.
        //   ch=0 band=0 sf=0 -> seed 64
        //   ch=0 band=0 sf=1 -> 64 + 1 = 65
        //   reset ref to gain[0][0][0] = 64 after band 0.
        //   ch=0 band=1 sf=0 -> 64 + (-1) = 63
        //   ch=0 band=1 sf=1 -> 63 + 0  = 63
        //   reset ref to gain[0][0][1] = 63 after band 1.
        //   reset ref to gain[0][0][0] = 64 after channel.
        //   ch=1 band=0 sf=0 -> 64 + 1 = 65
        //   ch=1 band=0 sf=1 -> 65 + (-1) = 64
        //   reset ref to gain[1][0][0] = 65 after band 0.
        //   ch=1 band=1 sf=0 -> 65 + 0 = 65
        //   ch=1 band=1 sf=1 -> 65 + 1 = 66
        assert_eq!(g.gain_db(0, 0, 0), 0); // 64 -> 0 dB
        assert_eq!(g.gain_db(0, 1, 0), 1); // 65
        assert_eq!(g.gain_db(0, 0, 1), -1); // 63
        assert_eq!(g.gain_db(0, 1, 1), -1); // 63
        assert_eq!(g.gain_db(1, 0, 0), 1); // 65
        assert_eq!(g.gain_db(1, 1, 0), 0); // 64
        assert_eq!(g.gain_db(1, 0, 1), 1); // 65
        assert_eq!(g.gain_db(1, 1, 1), 2); // 66
    }

    #[test]
    fn parse_drc_compression_curve_minimal() {
        // Minimal compression curve: nullband_low=0, nullband_high=0,
        // gain_max_boost=0 (no boost section), gain_max_cut=0 (no cut
        // section), tc_default_flag=1 (no time-constants block).
        let mut bw = BitWriter::new();
        bw.write_u32(0, 4); // drc_lev_nullband_low
        bw.write_u32(0, 4); // drc_lev_nullband_high
        bw.write_u32(0, 4); // drc_gain_max_boost
        bw.write_u32(0, 5); // drc_gain_max_cut
        bw.write_bit(true); // drc_tc_default_flag
        bw.align_to_byte();
        let bytes = bw.finish();
        let mut br = BitReader::new(&bytes);
        let curve = parse_drc_compression_curve(&mut br).unwrap();
        assert_eq!(curve.drc_lev_nullband_low, 0);
        assert_eq!(curve.drc_gain_max_boost, 0);
        assert!(curve.drc_lev_max_boost.is_none());
        assert!(curve.time_constants.is_none());
        assert!(curve.drc_tc_default_flag);
    }

    #[test]
    fn parse_drc_compression_curve_full_with_boost_and_cut() {
        // Boost section + cut section + adaptive smoothing.
        let mut bw = BitWriter::new();
        bw.write_u32(3, 4); // nullband_low
        bw.write_u32(5, 4); // nullband_high
        bw.write_u32(7, 4); // gain_max_boost > 0
        bw.write_u32(15, 5); // lev_max_boost
        bw.write_u32(1, 1); // nr_boost_sections > 0
        bw.write_u32(9, 4); // gain_section_boost
        bw.write_u32(20, 5); // lev_section_boost
        bw.write_u32(11, 5); // gain_max_cut > 0
        bw.write_u32(33, 6); // lev_max_cut
        bw.write_u32(1, 1); // nr_cut_sections > 0
        bw.write_u32(13, 5); // gain_section_cut
        bw.write_u32(17, 5); // lev_section_cut
        bw.write_bit(false); // tc_default_flag = 0
        bw.write_u32(20, 8); // tc_attack
        bw.write_u32(75, 8); // tc_release
        bw.write_u32(2, 8); // tc_attack_fast
        bw.write_u32(50, 8); // tc_release_fast
        bw.write_bit(true); // adaptive_smoothing_flag
        bw.write_u32(15, 5); // attack_threshold
        bw.write_u32(20, 5); // release_threshold
        bw.align_to_byte();
        let bytes = bw.finish();
        let mut br = BitReader::new(&bytes);
        let curve = parse_drc_compression_curve(&mut br).unwrap();
        assert_eq!(curve.drc_lev_nullband_low, 3);
        assert_eq!(curve.drc_gain_max_boost, 7);
        assert_eq!(curve.drc_lev_max_boost, Some(15));
        assert_eq!(curve.drc_nr_boost_sections, Some(1));
        assert_eq!(curve.drc_gain_section_boost, Some(9));
        assert_eq!(curve.drc_lev_section_boost, Some(20));
        assert_eq!(curve.drc_gain_max_cut, 11);
        assert_eq!(curve.drc_lev_max_cut, Some(33));
        let tc = curve.time_constants.as_ref().unwrap();
        assert_eq!(tc.drc_tc_attack, 20);
        assert!(tc.drc_adaptive_smoothing_flag);
        assert_eq!(tc.drc_attack_threshold, Some(15));
        assert_eq!(tc.drc_release_threshold, Some(20));
    }

    #[test]
    fn parse_drc_decoder_mode_repeat_profile() {
        // Two-mode config: mode 0 carries a curve, mode 1 repeats it
        // via drc_repeat_profile_flag.
        let mut bw = BitWriter::new();
        bw.write_bit(true); // b_drc_present
                            // drc_config:
        bw.write_u32(1, 3); // drc_decoder_nr_modes = 1 -> 2 modes

        // Mode 0: id=0, default profile (implicit curve flag).
        bw.write_u32(0, 3); // mode_id
        bw.write_bit(false); // drc_repeat_profile_flag
        bw.write_bit(true); // drc_default_profile_flag

        // Mode 1: id=1, repeat profile of mode 0.
        bw.write_u32(1, 3); // mode_id
        bw.write_bit(true); // drc_repeat_profile_flag
        bw.write_u32(0, 3); // drc_repeat_id = 0 -> mode 0

        bw.write_u32(0, 3); // drc_eac3_profile
                            // drc_data: both modes have curve_flag = 1 (implicit), so
                            // gainsets are empty; just the curve_present trailer.
        bw.write_bit(true); // drc_reset_flag
        bw.write_u32(2, 2); // drc_reserved
        bw.align_to_byte();
        let bytes = bw.finish();

        let mut br = BitReader::new(&bytes);
        let info = DrcChannelInfo::new(1, 1);
        let frame = parse_drc_frame(&mut br, true, info, None).unwrap();
        let cfg = frame.config.unwrap();
        assert_eq!(cfg.nr_modes(), 2);
        assert!(cfg.modes[0].drc_compression_curve_flag);
        assert!(cfg.modes[1].drc_repeat_profile_flag);
        assert_eq!(cfg.modes[1].drc_repeat_id, Some(0));
        assert!(cfg.modes[1].drc_compression_curve_flag); // inherited
        let data = frame.data.unwrap();
        assert!(data.curve_present);
        assert!(data.drc_reset_flag);
        assert_eq!(data.drc_reserved, 2);
    }

    #[test]
    fn parse_drc_frame_non_iframe_uses_prev_config() {
        // Non-I-frame: drc_frame skips drc_config(). Caller supplies
        // prev_config so the parser knows which modes carry gainsets.
        let drc_gain_val: u8 = 50;
        let drc_gainset_size: u32 = 2 + 7;

        let mut bw = BitWriter::new();
        bw.write_bit(true); // b_drc_present
                            // No drc_config() because !b_iframe.
        bw.write_u32(drc_gainset_size, 6); // drc_gainset_size_value
        bw.write_bit(false); // b_more_bits
        bw.write_u32(0, 2); // drc_version
        bw.write_u32(drc_gain_val as u32, 7); // drc_gain_val
        bw.align_to_byte();
        let bytes = bw.finish();

        let prev = DrcConfig {
            drc_decoder_nr_modes: 0,
            drc_eac3_profile: 0,
            modes: vec![DrcDecoderMode {
                drc_decoder_mode_id: 0,
                drc_output_level_from: None,
                drc_output_level_to: None,
                drc_repeat_profile_flag: false,
                drc_repeat_id: None,
                drc_default_profile_flag: Some(false),
                drc_compression_curve_flag: false,
                compression_curve: None,
                drc_gains_config: Some(0),
            }],
        };

        let mut br = BitReader::new(&bytes);
        let info = DrcChannelInfo::new(1, 1);
        let frame = parse_drc_frame(&mut br, false, info, Some(&prev)).unwrap();
        assert!(frame.config.is_none(), "no config on non-I-frame");
        let data = frame.data.unwrap();
        assert_eq!(data.gainsets.len(), 1);
        let g = &data.gainsets[0];
        assert_eq!(g.drc_gain_val, 50);
        // gain[0][0][0] = 50 - 64 = -14 dB.
        assert_eq!(g.gain_db(0, 0, 0), -14);
    }

    #[test]
    fn parse_drc_frame_non_iframe_without_prev_config_errors() {
        let mut bw = BitWriter::new();
        bw.write_bit(true);
        bw.align_to_byte();
        let bytes = bw.finish();
        let mut br = BitReader::new(&bytes);
        let info = DrcChannelInfo::new(1, 1);
        assert!(parse_drc_frame(&mut br, false, info, None).is_err());
    }

    // ------------------------------------------------------------------
    // §5.7.9.3.3 PCM gain application
    // ------------------------------------------------------------------

    #[test]
    fn drc_raw_to_linear_unity_at_64() {
        // raw == 64 -> dB == 0 -> linear == 1.0.
        let g = drc_raw_to_linear(64);
        assert!((g - 1.0).abs() < 1e-6, "expected ~1.0, got {g}");
    }

    #[test]
    fn drc_raw_to_linear_plus_six_db_doubles() {
        // 6 dB power = 2x linear in the AC-4 (1/6) convention.
        let g_pos = drc_raw_to_linear(64 + 6);
        assert!((g_pos - 2.0).abs() < 1e-5, "+6 dB: got {g_pos}");
        let g_neg = drc_raw_to_linear(64 - 6);
        assert!((g_neg - 0.5).abs() < 1e-5, "-6 dB: got {g_neg}");
    }

    #[test]
    fn dialnorm_correction_zero_is_unity() {
        let c = dialnorm_correction_linear(-31, -31);
        assert!((c - 1.0).abs() < 1e-6, "got {c}");
    }

    #[test]
    fn drc_channel_map_five_one_default_with_lfe() {
        let map = DrcChannelMap::five_one_default(true);
        assert_eq!(map.chg_for_pcm_channel, vec![0, 0, 1, 0, 2, 2]);
    }

    #[test]
    fn drc_channel_map_wideband_single_group() {
        let map = DrcChannelMap::wideband_single_group(2);
        assert_eq!(map.chg_for_pcm_channel, vec![0, 0]);
    }

    #[test]
    fn apply_drc_gains_to_pcm_uniform_subframes_apply_per_subframe_gain() {
        // 1 channel-group, 4 subframes, 1 band. gain[0][sf][0] = 64 + sf*6
        // → linear gains 1, 2, 4, 8.
        let gains = DrcGains {
            mode_id: 0,
            nr_drc_channels: 1,
            nr_drc_subframes: 4,
            nr_drc_bands: 1,
            drc_gain_val: 64,
            drc_gain: vec![64, 70, 76, 82], // (ch=0, sf=0..3, band=0)
        };
        let map = DrcChannelMap::wideband_single_group(1);
        // 16 samples, all 1.0; expect [1,1,1,1, 2,2,2,2, 4,4,4,4, 8,8,8,8].
        let mut pcm = vec![vec![1.0f32; 16]];
        let n = apply_drc_gains_to_pcm(&mut pcm, &gains, &map, 1.0).unwrap();
        assert_eq!(n, 16);
        let expected: Vec<f32> = [1.0, 2.0, 4.0, 8.0]
            .iter()
            .flat_map(|&g| std::iter::repeat(g).take(4))
            .collect();
        for (got, want) in pcm[0].iter().zip(expected.iter()) {
            assert!((got - want).abs() < 1e-4, "got {got}, want {want}");
        }
    }

    #[test]
    fn apply_drc_gains_to_pcm_dialnorm_correction_is_multiplicative() {
        // 1 group, 1 sf, 1 band, drc_gain == 64 (unity). dialnorm_corr =
        // 0.5 → output = input * 0.5.
        let gains = DrcGains {
            mode_id: 0,
            nr_drc_channels: 1,
            nr_drc_subframes: 1,
            nr_drc_bands: 1,
            drc_gain_val: 64,
            drc_gain: vec![64],
        };
        let map = DrcChannelMap::wideband_single_group(1);
        let mut pcm = vec![vec![1.0f32; 4]];
        let dialnorm = dialnorm_correction_linear(-37, -31); // -6 dB → 0.5
        apply_drc_gains_to_pcm(&mut pcm, &gains, &map, dialnorm).unwrap();
        for s in &pcm[0] {
            assert!((s - 0.5).abs() < 1e-5, "got {s}");
        }
    }

    #[test]
    fn apply_drc_gains_to_pcm_5_1_routes_to_correct_channel_group() {
        // 3 groups (5_X-style), 1 sf, 1 band.
        // group 0 -> +6dB (g=2), group 1 -> 0dB (g=1), group 2 -> -6dB (g=0.5).
        let gains = DrcGains {
            mode_id: 0,
            nr_drc_channels: 3,
            nr_drc_subframes: 1,
            nr_drc_bands: 1,
            drc_gain_val: 70,
            // gain[ch][sf=0][band=0] for ch in 0..3.
            drc_gain: vec![70, 64, 58],
        };
        let map = DrcChannelMap::five_one_default(true); // [0,0,1,0,2,2]
        let mut pcm: Vec<Vec<f32>> = (0..6).map(|_| vec![1.0f32; 4]).collect();
        apply_drc_gains_to_pcm(&mut pcm, &gains, &map, 1.0).unwrap();
        // L (chg 0) = 2, R (chg 0) = 2, C (chg 1) = 1, LFE (chg 0) = 2,
        // Ls (chg 2) = 0.5, Rs (chg 2) = 0.5.
        let want = [2.0, 2.0, 1.0, 2.0, 0.5, 0.5];
        for (i, expected) in want.iter().enumerate() {
            for s in &pcm[i] {
                assert!(
                    (s - expected).abs() < 1e-4,
                    "ch {i}: got {s}, want {expected}"
                );
            }
        }
    }

    #[test]
    fn apply_drc_gains_to_pcm_invalid_chg_rejected() {
        let gains = DrcGains {
            mode_id: 0,
            nr_drc_channels: 1,
            nr_drc_subframes: 1,
            nr_drc_bands: 1,
            drc_gain_val: 64,
            drc_gain: vec![64],
        };
        let map = DrcChannelMap {
            chg_for_pcm_channel: vec![5], // out of range
        };
        let mut pcm = vec![vec![1.0f32; 4]];
        let err = apply_drc_gains_to_pcm(&mut pcm, &gains, &map, 1.0).unwrap_err();
        assert!(err.to_string().contains("out of bounds"));
    }

    #[test]
    fn apply_drc_gains_to_pcm_pcm_channel_count_mismatch_rejected() {
        let gains = DrcGains {
            mode_id: 0,
            nr_drc_channels: 1,
            nr_drc_subframes: 1,
            nr_drc_bands: 1,
            drc_gain_val: 64,
            drc_gain: vec![64],
        };
        let map = DrcChannelMap::wideband_single_group(2);
        let mut pcm: Vec<Vec<f32>> = vec![vec![1.0f32; 4]]; // only 1 channel
        let err = apply_drc_gains_to_pcm(&mut pcm, &gains, &map, 1.0).unwrap_err();
        assert!(err.to_string().contains("mismatches"));
    }
}