transmux 0.30.0

Any-to-any media container muxing hub: demux TS, fMP4/CMAF, MPEG-PS, WebM, FLV, or RTMP into one neutral IR and mux to CMAF/fMP4, progressive MP4, TS, DASH, low-latency DASH, HLS, low-latency HLS, Smooth Streaming, or RTMP. CENC/CBCS encrypt+decrypt, SSAI splice, RTP/RTCP, and an fMP4/CMAF conformance validator; parses codec config headers only, samples stay opaque. no_std + alloc, except the XML manifest paths (DASH/Smooth/PlayReady), which need the std feature.
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
//! H.264 slice-header prefix, PPS prefix and field-pair (PAFF) detection —
//! ITU-T H.264 (08/2021) §7.3.3 (slice header), §7.3.2.2 (PPS), §7.4.1.2.4
//! (first VCL NAL unit of a primary coded picture), §3.35-3.37 (complementary
//! field pairs). Transcription: `private/specs-verified/mpeg/transmux/codec/h264-slice-header.md`.
//!
//! Field-coded (PAFF) H.264 is broadcast as one coded picture per *field*, one
//! PES per field. ISO/IEC 14496-15 samples are whole access units, i.e. both
//! fields of a frame ([`AvcFieldInfo::pairs_with`]); the demuxer uses
//! [`AvcParamSets::classify_au`] to find them. Only the entropy-agnostic prefix
//! of the slice header (through `redundant_pic_cnt`) is parsed. MBAFF pictures
//! are *frames* (`field_pic_flag == 0`) and classify as [`AvcPicture::Frame`].

use crate::annexb::iter_annexb_nals;
use crate::bitreader::BitReader;
use crate::error::{Error, Result};
use crate::sps::{AvcSliceSps, decode_avc_slice_sps};
use alloc::collections::BTreeMap;
use broadcast_common::error::{BufferTooShort, InvalidValue};

/// `nal_unit_type` of a non-IDR coded slice (Table 7-1).
const NAL_SLICE: u8 = 1;
/// `nal_unit_type` of an IDR coded slice (Table 7-1).
const NAL_IDR: u8 = 5;
/// `nal_unit_type` of a sequence parameter set.
const NAL_SPS: u8 = 7;
/// `nal_unit_type` of a picture parameter set.
const NAL_PPS: u8 = 8;
/// `nal_unit_type` of an access unit delimiter.
pub(crate) const NAL_AUD: u8 = 9;
/// Low five bits of the NAL header byte.
const NAL_TYPE_MASK: u8 = 0x1F;
/// `nal_ref_idc` bits of the NAL header byte.
const NAL_REF_IDC_MASK: u8 = 0x60;
/// Shift of `nal_ref_idc` in the NAL header byte.
const NAL_REF_IDC_SHIFT: u8 = 5;
/// How many NAL bytes (after the header) a slice-header prefix is read from.
/// The prefix is a handful of Exp-Golomb codes; this bounds the unescape copy
/// so a multi-megabyte I slice is not duplicated per access unit.
const SLICE_PREFIX_BYTES: usize = 64;
/// Bits of `colour_plane_id` (§7.3.3).
const COLOUR_PLANE_ID_BITS: usize = 2;

/// The PPS fields a slice-header parser needs (§7.3.2.2).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AvcSlicePps {
    /// `pic_parameter_set_id`.
    pub pps_id: u8,
    /// `seq_parameter_set_id`.
    pub sps_id: u8,
    /// `entropy_coding_mode_flag`.
    pub entropy_coding_mode: bool,
    /// `bottom_field_pic_order_in_frame_present_flag`.
    pub bottom_field_pic_order_in_frame_present: bool,
    /// `redundant_pic_cnt_present_flag`.
    pub redundant_pic_cnt_present: bool,
}

/// Decode the slice-header-relevant prefix of a PPS NAL unit (1-byte NAL
/// header included), walking the slice-group syntax to reach
/// `redundant_pic_cnt_present_flag` (§7.3.2.2).
pub fn decode_avc_slice_pps(pps_nal: &[u8]) -> Result<AvcSlicePps> {
    let body = pps_nal.get(1..).unwrap_or(&[]);
    let mut r = BitReader::with_unescape(body, "H.264 PPS")?;
    let pps_id = r.read_ue("pic_parameter_set_id")?;
    let sps_id = r.read_ue("seq_parameter_set_id")?;
    if pps_id > 255 || sps_id > 31 {
        return Err(Error::InvalidValue(InvalidValue {
            field: "pic_parameter_set_id/seq_parameter_set_id",
            value: pps_id.max(sps_id),
            reason: "out of range (H.264 §7.4.2.2: 0..=255 / 0..=31)",
        }));
    }
    let entropy_coding_mode = r.read_flag("entropy_coding_mode_flag")?;
    let bottom_field_pic_order_in_frame_present =
        r.read_flag("bottom_field_pic_order_in_frame_present_flag")?;
    let num_slice_groups_minus1 = r.read_ue("num_slice_groups_minus1")?;
    if num_slice_groups_minus1 > 7 {
        return Err(Error::InvalidValue(InvalidValue {
            field: "num_slice_groups_minus1",
            value: num_slice_groups_minus1,
            reason: "out of range 0..=7 (H.264 §7.4.2.2)",
        }));
    }
    if num_slice_groups_minus1 > 0 {
        let map_type = r.read_ue("slice_group_map_type")?;
        match map_type {
            0 => {
                for _ in 0..=num_slice_groups_minus1 {
                    r.read_ue("run_length_minus1")?;
                }
            }
            2 => {
                for _ in 0..num_slice_groups_minus1 {
                    r.read_ue("top_left")?;
                    r.read_ue("bottom_right")?;
                }
            }
            3..=5 => {
                r.read_flag("slice_group_change_direction_flag")?;
                r.read_ue("slice_group_change_rate_minus1")?;
            }
            6 => {
                let n = r.read_ue("pic_size_in_map_units_minus1")?;
                // Ceil(Log2(num_slice_groups_minus1 + 1)) bits per id (§7.4.2.2).
                let bits = (num_slice_groups_minus1 + 1)
                    .next_power_of_two()
                    .trailing_zeros();
                for _ in 0..=n {
                    r.read_bits(bits as usize, "slice_group_id")?;
                }
            }
            _ => {}
        }
    }
    r.read_ue("num_ref_idx_l0_default_active_minus1")?;
    r.read_ue("num_ref_idx_l1_default_active_minus1")?;
    r.read_flag("weighted_pred_flag")?;
    r.read_bits(2, "weighted_bipred_idc")?;
    r.read_se("pic_init_qp_minus26")?;
    r.read_se("pic_init_qs_minus26")?;
    r.read_se("chroma_qp_index_offset")?;
    r.read_flag("deblocking_filter_control_present_flag")?;
    r.read_flag("constrained_intra_pred_flag")?;
    let redundant_pic_cnt_present = r.read_flag("redundant_pic_cnt_present_flag")?;
    Ok(AvcSlicePps {
        pps_id: pps_id as u8,
        sps_id: sps_id as u8,
        entropy_coding_mode,
        bottom_field_pic_order_in_frame_present,
        redundant_pic_cnt_present,
    })
}

/// The slice-header prefix of one coded slice NAL (§7.3.3 through
/// `redundant_pic_cnt`), plus the NAL-header facts the §7.4.1.2.4 test uses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AvcSliceHeader {
    /// `nal_ref_idc` (0 = non-reference).
    pub nal_ref_idc: u8,
    /// `IdrPicFlag` (`nal_unit_type == 5`).
    pub idr: bool,
    /// `first_mb_in_slice`.
    pub first_mb_in_slice: u32,
    /// `slice_type` as coded (0..=9, Table 7-6).
    pub slice_type: u8,
    /// `pic_parameter_set_id`.
    pub pps_id: u8,
    /// `colour_plane_id` (only when `separate_colour_plane_flag`).
    pub colour_plane_id: Option<u8>,
    /// `frame_num`.
    pub frame_num: u32,
    /// `field_pic_flag` (inferred `false` when absent).
    pub field_pic_flag: bool,
    /// `bottom_field_flag` (inferred `false` when absent).
    pub bottom_field_flag: bool,
    /// `idr_pic_id` (IDR slices only).
    pub idr_pic_id: Option<u32>,
    /// `pic_order_cnt_lsb` (POC type 0 only).
    pub pic_order_cnt_lsb: Option<u32>,
    /// `delta_pic_order_cnt_bottom` (inferred 0).
    pub delta_pic_order_cnt_bottom: i32,
    /// `delta_pic_order_cnt[0..=1]` (inferred 0).
    pub delta_pic_order_cnt: [i32; 2],
    /// `redundant_pic_cnt` (inferred 0).
    pub redundant_pic_cnt: u32,
}

fn to_u32(v: u64, field: &'static str) -> Result<u32> {
    u32::try_from(v).map_err(|_| {
        Error::InvalidValue(InvalidValue {
            field,
            value: v,
            reason: "does not fit in u32",
        })
    })
}

fn to_i32(v: i64, field: &'static str) -> Result<i32> {
    i32::try_from(v).map_err(|_| {
        Error::InvalidValue(InvalidValue {
            field,
            value: v as u64,
            reason: "does not fit in i32",
        })
    })
}

/// Parse the §7.3.3 prefix of one slice NAL (`nal_unit_type` 1 or 5; 1-byte NAL
/// header included) against its SPS and PPS.
pub fn parse_avc_slice_header(
    nal: &[u8],
    sps: &AvcSliceSps,
    pps: &AvcSlicePps,
) -> Result<AvcSliceHeader> {
    let header = *nal.first().ok_or(Error::BufferTooShort(BufferTooShort {
        need: 1,
        have: 0,
        what: "H.264 slice NAL header",
    }))?;
    let nal_type = header & NAL_TYPE_MASK;
    if nal_type != NAL_SLICE && nal_type != NAL_IDR {
        return Err(Error::InvalidValue(InvalidValue {
            field: "nal_unit_type",
            value: nal_type as u64,
            reason: "not a non-partitioned coded slice (1 or 5)",
        }));
    }
    let idr = nal_type == NAL_IDR;
    let nal_ref_idc = (header & NAL_REF_IDC_MASK) >> NAL_REF_IDC_SHIFT;
    let end = nal.len().min(1 + SLICE_PREFIX_BYTES);
    let mut r = BitReader::with_unescape(&nal[1..end], "H.264 slice header")?;
    let first_mb_in_slice = to_u32(r.read_ue("first_mb_in_slice")?, "first_mb_in_slice")?;
    let slice_type = r.read_ue("slice_type")?;
    if slice_type > 9 {
        return Err(Error::InvalidValue(InvalidValue {
            field: "slice_type",
            value: slice_type,
            reason: "out of range 0..=9 (H.264 Table 7-6)",
        }));
    }
    let pps_id = r.read_ue("pic_parameter_set_id")?;
    if pps_id != pps.pps_id as u64 {
        return Err(Error::InvalidValue(InvalidValue {
            field: "pic_parameter_set_id",
            value: pps_id,
            reason: "slice refers to a different PPS than the one supplied",
        }));
    }
    let colour_plane_id = if sps.separate_colour_plane {
        Some(r.read_bits(COLOUR_PLANE_ID_BITS, "colour_plane_id")? as u8)
    } else {
        None
    };
    let frame_num = r.read_bits(sps.log2_max_frame_num as usize, "frame_num")? as u32;
    let (mut field_pic_flag, mut bottom_field_flag) = (false, false);
    if !sps.frame_mbs_only {
        field_pic_flag = r.read_flag("field_pic_flag")?;
        if field_pic_flag {
            bottom_field_flag = r.read_flag("bottom_field_flag")?;
        }
    }
    let idr_pic_id = if idr {
        Some(to_u32(r.read_ue("idr_pic_id")?, "idr_pic_id")?)
    } else {
        None
    };
    let bottom_poc_present = pps.bottom_field_pic_order_in_frame_present && !field_pic_flag;
    let mut pic_order_cnt_lsb = None;
    let mut delta_pic_order_cnt_bottom = 0;
    let mut delta_pic_order_cnt = [0i32; 2];
    if sps.pic_order_cnt_type == 0 {
        pic_order_cnt_lsb =
            Some(r.read_bits(sps.log2_max_poc_lsb as usize, "pic_order_cnt_lsb")? as u32);
        if bottom_poc_present {
            delta_pic_order_cnt_bottom = to_i32(
                r.read_se("delta_pic_order_cnt_bottom")?,
                "delta_pic_order_cnt_bottom",
            )?;
        }
    }
    if sps.pic_order_cnt_type == 1 && !sps.delta_pic_order_always_zero {
        delta_pic_order_cnt[0] = to_i32(
            r.read_se("delta_pic_order_cnt[0]")?,
            "delta_pic_order_cnt[0]",
        )?;
        if bottom_poc_present {
            delta_pic_order_cnt[1] = to_i32(
                r.read_se("delta_pic_order_cnt[1]")?,
                "delta_pic_order_cnt[1]",
            )?;
        }
    }
    let redundant_pic_cnt = if pps.redundant_pic_cnt_present {
        to_u32(r.read_ue("redundant_pic_cnt")?, "redundant_pic_cnt")?
    } else {
        0
    };
    Ok(AvcSliceHeader {
        nal_ref_idc,
        idr,
        first_mb_in_slice,
        slice_type: slice_type as u8,
        pps_id: pps_id as u8,
        colour_plane_id,
        frame_num,
        field_pic_flag,
        bottom_field_flag,
        idr_pic_id,
        pic_order_cnt_lsb,
        delta_pic_order_cnt_bottom,
        delta_pic_order_cnt,
        redundant_pic_cnt,
    })
}

/// What an access unit's coded picture is, for field-pairing purposes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AvcPicture {
    /// A coded frame (progressive, or interlaced with `field_pic_flag == 0`,
    /// including MBAFF), or an AU holding more than one picture/parity — never
    /// merged with a neighbour.
    Frame,
    /// A single coded field.
    Field(AvcFieldInfo),
    /// The access unit carries no parseable slice (missing SPS/PPS, malformed
    /// header, non-VCL only): treated like [`AvcPicture::Frame`] by the demuxer.
    Unknown,
}

/// The identity of a coded field picture (the facts §3.36/§3.37 pair on).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AvcFieldInfo {
    /// `bottom_field_flag`.
    pub bottom: bool,
    /// `frame_num`.
    pub frame_num: u32,
    /// `nal_ref_idc != 0`.
    pub is_reference: bool,
    /// The field is an IDR picture.
    pub idr: bool,
}

impl AvcFieldInfo {
    /// Whether `second` (the next access unit in decoding order) completes
    /// `self` as a complementary field pair (§3.35-3.37): opposite parity, the
    /// same `frame_num`, both reference or both non-reference, and the second
    /// field not an IDR picture. (The mmco-5 exclusion of §3.37 is not
    /// checked — see the module docs. "First field not already paired" is the
    /// caller's state.)
    pub fn pairs_with(&self, second: &AvcFieldInfo) -> bool {
        self.bottom != second.bottom
            && self.frame_num == second.frame_num
            && self.is_reference == second.is_reference
            && !second.idr
    }
}

/// SPS/PPS cache for classifying access units, fed from the stream's in-band
/// parameter sets.
#[derive(Debug, Clone, Default)]
pub struct AvcParamSets {
    sps: BTreeMap<u8, AvcSliceSps>,
    pps: BTreeMap<u8, AvcSlicePps>,
}

impl AvcParamSets {
    /// An empty cache.
    pub fn new() -> Self {
        Self::default()
    }

    /// Record an SPS or PPS NAL unit (any other NAL type, or a malformed one,
    /// is ignored; the previous entry for that id is kept on a parse failure).
    pub fn observe_nal(&mut self, nal: &[u8]) {
        match nal.first().map(|b| b & NAL_TYPE_MASK) {
            Some(NAL_SPS) => {
                if let Ok(s) = decode_avc_slice_sps(nal) {
                    self.sps.insert(s.sps_id, s);
                }
            }
            Some(NAL_PPS) => {
                if let Ok(p) = decode_avc_slice_pps(nal) {
                    self.pps.insert(p.pps_id, p);
                }
            }
            _ => {}
        }
    }

    /// Update the cache from the parameter sets of an Annex B access unit and
    /// classify its coded picture ([`AvcPicture`]). Every VCL slice NAL is
    /// parsed; slices that disagree on field/frame, parity or `frame_num`
    /// make the access unit a [`AvcPicture::Frame`] (it already holds more
    /// than one picture, so it is a complete sample on its own).
    pub fn classify_au(&mut self, annexb_au: &[u8]) -> AvcPicture {
        self.classify_nals(iter_annexb_nals(annexb_au))
    }

    /// [`Self::classify_au`] over an access unit already split into NAL units
    /// (for example from a length-prefixed sample).
    pub fn classify_nals<'a>(&mut self, nals: impl IntoIterator<Item = &'a [u8]>) -> AvcPicture {
        let mut result: Option<AvcPicture> = None;
        for nal in nals {
            let t = nal.first().map(|b| b & NAL_TYPE_MASK);
            match t {
                Some(NAL_SPS) | Some(NAL_PPS) => self.observe_nal(nal),
                Some(NAL_SLICE) | Some(NAL_IDR) => {
                    let this = self.classify_slice(nal);
                    result = Some(match (result, this) {
                        (None, p) => p,
                        (Some(a), b) if a == b => a,
                        _ => AvcPicture::Frame,
                    });
                }
                _ => {}
            }
        }
        result.unwrap_or(AvcPicture::Unknown)
    }

    /// Parse a slice NAL's header against the cached parameter sets, returning
    /// it with its SPS, or `None` when a parameter set is missing or the header
    /// is malformed.
    pub(crate) fn parse_slice(&self, nal: &[u8]) -> Option<(AvcSliceHeader, AvcSliceSps)> {
        let pps = self.pps.get(&peek_pps_id(nal)?)?;
        let sps = self.sps.get(&pps.sps_id)?;
        parse_avc_slice_header(nal, sps, pps)
            .ok()
            .map(|h| (h, *sps))
    }

    fn classify_slice(&self, nal: &[u8]) -> AvcPicture {
        // pic_parameter_set_id is the third ue(v); peek it to select the PPS.
        let Some(pps_id) = peek_pps_id(nal) else {
            return AvcPicture::Unknown;
        };
        let Some(pps) = self.pps.get(&pps_id) else {
            return AvcPicture::Unknown;
        };
        let Some(sps) = self.sps.get(&pps.sps_id) else {
            return AvcPicture::Unknown;
        };
        match parse_avc_slice_header(nal, sps, pps) {
            Ok(h) if h.field_pic_flag => AvcPicture::Field(AvcFieldInfo {
                bottom: h.bottom_field_flag,
                frame_num: h.frame_num,
                is_reference: h.nal_ref_idc != 0,
                idr: h.idr,
            }),
            Ok(_) => AvcPicture::Frame,
            Err(_) => AvcPicture::Unknown,
        }
    }
}

fn peek_pps_id(nal: &[u8]) -> Option<u8> {
    let end = nal.len().min(1 + SLICE_PREFIX_BYTES);
    let mut r = BitReader::with_unescape(nal.get(1..end)?, "H.264 slice header").ok()?;
    r.read_ue("first_mb_in_slice").ok()?;
    r.read_ue("slice_type").ok()?;
    u8::try_from(r.read_ue("pic_parameter_set_id").ok()?).ok()
}

#[cfg(test)]
mod tests {
    use broadcast_common::error::InvalidValue;

    use super::*;
    use crate::test_bits::BitWriter;
    use alloc::vec;
    use alloc::vec::Vec;

    fn nal(ref_idc: u8, t: u8, rbsp: Vec<u8>) -> Vec<u8> {
        let mut v = vec![(ref_idc << 5) | t];
        v.extend(rbsp);
        v
    }

    fn sps_nal(poc_type: u32, frame_mbs_only: bool) -> Vec<u8> {
        let mut w = BitWriter::new();
        w.u(8, 77);
        w.u(8, 0);
        w.u(8, 30);
        w.ue(0); // sps id
        w.ue(0); // log2_max_frame_num_minus4 -> 4 bits
        w.ue(u64::from(poc_type));
        if poc_type == 0 {
            w.ue(2); // log2_max_poc_lsb_minus4 -> 6 bits
        } else if poc_type == 1 {
            w.u(1, 0); // delta_pic_order_always_zero_flag
            w.se(0);
            w.se(0);
            w.ue(0);
        }
        w.ue(1);
        w.u(1, 0);
        w.ue(3);
        w.ue(1);
        w.u(1, u64::from(frame_mbs_only as u32));
        if !frame_mbs_only {
            w.u(1, 0);
        }
        w.u(1, 1);
        w.u(1, 0);
        w.u(1, 0);
        nal(3, 7, w.trailing())
    }

    fn pps_nal(id: u32, bottom_poc: bool, redundant: bool, slice_groups: u32) -> Vec<u8> {
        let mut w = BitWriter::new();
        w.ue(u64::from(id));
        w.ue(0);
        w.u(1, 0);
        w.u(1, u64::from(bottom_poc as u32));
        w.ue(u64::from(slice_groups));
        if slice_groups > 0 {
            w.ue(6); // map type 6
            w.ue(3); // pic_size_in_map_units_minus1 -> 4 ids
            for _ in 0..4 {
                w.u(2, 1); // ceil(log2(3)) = 2 bits
            }
        }
        w.ue(0);
        w.ue(0);
        w.u(1, 0);
        w.u(2, 0);
        w.se(0);
        w.se(0);
        w.se(0);
        w.u(1, 1);
        w.u(1, 0);
        w.u(1, u64::from(redundant as u32));
        nal(3, 8, w.trailing())
    }

    #[allow(clippy::too_many_arguments)]
    fn slice(
        ref_idc: u8,
        idr: bool,
        frame_num: u32,
        field: Option<bool>,
        poc_lsb: u32,
        extra_after_poc: &dyn Fn(&mut BitWriter),
    ) -> Vec<u8> {
        let mut w = BitWriter::new();
        w.ue(0); // first_mb
        w.ue(7); // slice_type I
        w.ue(0); // pps id
        w.u(4, u64::from(frame_num));
        if let Some(bottom) = field {
            w.u(1, 1);
            w.u(1, u64::from(bottom as u32));
        } else {
            w.u(1, 0);
        }
        if idr {
            w.ue(9);
        }
        w.u(6, u64::from(poc_lsb));
        extra_after_poc(&mut w);
        w.ue(1); // trailing slice data stand-in
        nal(ref_idc, if idr { 5 } else { 1 }, w.trailing())
    }

    fn annexb(nals: &[Vec<u8>]) -> Vec<u8> {
        let mut v = Vec::new();
        for n in nals {
            v.extend([0, 0, 0, 1]);
            v.extend(n);
        }
        v
    }

    #[test]
    fn parses_every_prefix_field_bit_exact() {
        let sps = decode_avc_slice_sps(&sps_nal(0, false)).unwrap();
        assert_eq!(sps.log2_max_frame_num, 4);
        assert_eq!(sps.log2_max_poc_lsb, 6);
        assert!(!sps.frame_mbs_only);
        let pps = decode_avc_slice_pps(&pps_nal(0, true, true, 0)).unwrap();
        assert!(pps.bottom_field_pic_order_in_frame_present && pps.redundant_pic_cnt_present);
        // Frame picture (field_pic_flag = 0): delta_pic_order_cnt_bottom + redundant_pic_cnt present.
        let n = slice(2, true, 9, None, 37, &|w| {
            w.se(-5);
            w.ue(3);
        });
        let h = parse_avc_slice_header(&n, &sps, &pps).unwrap();
        assert_eq!(h.nal_ref_idc, 2);
        assert!(h.idr);
        assert_eq!((h.first_mb_in_slice, h.slice_type, h.frame_num), (0, 7, 9));
        assert!(!h.field_pic_flag && !h.bottom_field_flag);
        assert_eq!(h.idr_pic_id, Some(9));
        assert_eq!(h.pic_order_cnt_lsb, Some(37));
        assert_eq!(h.delta_pic_order_cnt_bottom, -5);
        assert_eq!(h.redundant_pic_cnt, 3);
        // Field picture: no delta_pic_order_cnt_bottom even though the PPS flag is set.
        let n = slice(2, false, 15, Some(true), 63, &|w| {
            w.ue(2);
        });
        let h = parse_avc_slice_header(&n, &sps, &pps).unwrap();
        assert!(h.field_pic_flag && h.bottom_field_flag);
        assert_eq!((h.frame_num, h.pic_order_cnt_lsb), (15, Some(63)));
        assert_eq!((h.delta_pic_order_cnt_bottom, h.redundant_pic_cnt), (0, 2));
        assert_eq!(h.idr_pic_id, None);
    }

    #[test]
    fn poc_type_1_and_progressive_sps() {
        let sps = decode_avc_slice_sps(&sps_nal(1, true)).unwrap();
        assert!(sps.frame_mbs_only);
        assert_eq!(sps.pic_order_cnt_type, 1);
        let pps = decode_avc_slice_pps(&pps_nal(0, false, false, 0)).unwrap();
        // frame_mbs_only: no field_pic_flag bit at all; POC type 1: delta_pic_order_cnt[0].
        let mut w = BitWriter::new();
        w.ue(0);
        w.ue(0);
        w.ue(0);
        w.u(4, 3);
        w.se(7);
        let n = nal(1, 1, w.trailing());
        let h = parse_avc_slice_header(&n, &sps, &pps).unwrap();
        assert!(!h.field_pic_flag);
        assert_eq!((h.frame_num, h.delta_pic_order_cnt), (3, [7, 0]));
    }

    #[test]
    fn pps_slice_group_type_6_walked() {
        let pps = decode_avc_slice_pps(&pps_nal(0, false, true, 2)).unwrap();
        assert!(pps.redundant_pic_cnt_present);
    }

    #[test]
    fn truncated_or_wrong_pps_is_an_error_not_a_panic() {
        let sps = decode_avc_slice_sps(&sps_nal(0, false)).unwrap();
        let pps = decode_avc_slice_pps(&pps_nal(0, false, false, 0)).unwrap();
        let n = slice(1, false, 1, Some(false), 1, &|_| {});
        for cut in 1..4 {
            assert!(matches!(
                parse_avc_slice_header(&n[..cut], &sps, &pps),
                Err(Error::BufferTooShort(..))
            ));
        }
        let other = AvcSlicePps { pps_id: 4, ..pps };
        assert!(matches!(
            parse_avc_slice_header(&n, &sps, &other),
            Err(Error::InvalidValue(InvalidValue {
                field: "pic_parameter_set_id",
                ..
            }))
        ));
        assert!(matches!(
            parse_avc_slice_header(&[0x67, 0x01], &sps, &pps),
            Err(Error::InvalidValue(InvalidValue {
                field: "nal_unit_type",
                value: 7,
                ..
            }))
        ));
    }

    fn params() -> (Vec<u8>, Vec<u8>) {
        (sps_nal(0, false), pps_nal(0, false, false, 0))
    }

    #[test]
    fn classify_field_frame_and_unknown() {
        let (s, p) = params();
        let mut ps = AvcParamSets::new();
        // No parameter sets seen yet: Unknown.
        let f = slice(1, false, 2, Some(false), 4, &|_| {});
        assert_eq!(
            ps.classify_au(&annexb(std::slice::from_ref(&f))),
            AvcPicture::Unknown
        );
        // With them: a field...
        let au = annexb(&[s, p, f.clone()]);
        assert_eq!(
            ps.classify_au(&au),
            AvcPicture::Field(AvcFieldInfo {
                bottom: false,
                frame_num: 2,
                is_reference: true,
                idr: false
            })
        );
        // ...and a frame picture (field_pic_flag = 0, MBAFF-like) is not a field.
        let fr = slice(1, false, 2, None, 4, &|_| {});
        assert_eq!(ps.classify_au(&annexb(&[fr])), AvcPicture::Frame);
        // An AU carrying both parities (two pictures in one PES) is a Frame.
        let b = slice(1, false, 2, Some(true), 5, &|_| {});
        assert_eq!(ps.classify_au(&annexb(&[f, b])), AvcPicture::Frame);
    }

    #[test]
    fn pairing_rules() {
        let a = AvcFieldInfo {
            bottom: false,
            frame_num: 3,
            is_reference: true,
            idr: true,
        };
        let b = AvcFieldInfo {
            bottom: true,
            frame_num: 3,
            is_reference: true,
            idr: false,
        };
        assert!(a.pairs_with(&b)); // IDR first field + non-IDR second
        assert!(!a.pairs_with(&AvcFieldInfo { bottom: false, ..b })); // same parity
        assert!(!a.pairs_with(&AvcFieldInfo { frame_num: 4, ..b })); // frame_num
        assert!(!a.pairs_with(&AvcFieldInfo {
            is_reference: false,
            ..b
        })); // ref/non-ref mix
        assert!(!a.pairs_with(&AvcFieldInfo { idr: true, ..b })); // second field IDR
    }
}