rust_h264 0.3.0

Pure Rust H.264/AVC video decoder
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
//! H.264 NAL unit framing — Annex B and AVCC parsers.
//!
//! Provides two parser entry points:
//!
//! - [`parse_annex_b`] for start-code delimited bitstreams
//!   (`.h264` files, RTP payloads, broadcast TS).
//! - [`parse_avcc`] + [`parse_avcc_config`] for length-prefixed bitstreams
//!   (NAL units inside MP4/MKV containers).
//!
//! Both produce [`NalUnit`] values that can be fed directly to
//! [`Decoder::decode_nal`](crate::decoder::Decoder::decode_nal).

use std::borrow::Cow;

/// H.264 NAL unit type identifier (spec Table 7-1).
///
/// Only the types this decoder cares about are named explicitly; everything
/// else falls into [`NalUnitType::Other`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NalUnitType {
    Slice,               // 1
    SliceDataA,          // 2
    SliceDataB,          // 3
    SliceDataC,          // 4
    SliceIdr,            // 5
    Sei,                 // 6
    Sps,                 // 7
    Pps,                 // 8
    AccessUnitDelimiter, // 9
    EndOfSequence,       // 10
    EndOfStream,         // 11
    FillerData,          // 12
    Other(u8),
}

impl From<u8> for NalUnitType {
    fn from(val: u8) -> Self {
        match val {
            1 => NalUnitType::Slice,
            2 => NalUnitType::SliceDataA,
            3 => NalUnitType::SliceDataB,
            4 => NalUnitType::SliceDataC,
            5 => NalUnitType::SliceIdr,
            6 => NalUnitType::Sei,
            7 => NalUnitType::Sps,
            8 => NalUnitType::Pps,
            9 => NalUnitType::AccessUnitDelimiter,
            10 => NalUnitType::EndOfSequence,
            11 => NalUnitType::EndOfStream,
            12 => NalUnitType::FillerData,
            v => NalUnitType::Other(v),
        }
    }
}

/// A parsed H.264 NAL unit.
///
/// Contains the unit type, reference indicator, and the Raw Byte Sequence
/// Payload (RBSP) — which is the NAL payload with emulation prevention bytes
/// removed.
///
/// The `rbsp` field is a [`Cow`]: when no emulation prevention bytes are
/// present (the common case), it borrows directly from the input slice with
/// no allocation.
#[derive(Debug)]
pub struct NalUnit<'a> {
    /// `nal_ref_idc` from the NAL header (0..3). Non-zero means the NAL
    /// belongs to a reference picture.
    pub nal_ref_idc: u8,
    /// NAL unit type from the NAL header (5 bits).
    pub nal_unit_type: NalUnitType,
    /// RBSP payload (emulation prevention bytes removed, or borrowed directly
    /// from the input when no emulation prevention bytes are present).
    pub rbsp: Cow<'a, [u8]>,
}

/// Split an Annex B bytestream into NAL units.
///
/// Handles both 3-byte (`00 00 01`) and 4-byte (`00 00 00 01`) start codes
/// and removes emulation prevention bytes from each NAL's RBSP payload.
/// NALs with the `forbidden_zero_bit` set are skipped.
///
/// # Example
///
/// ```no_run
/// use rust_h264::nal::parse_annex_b;
///
/// let bitstream = std::fs::read("video.h264").unwrap();
/// let nals = parse_annex_b(&bitstream);
/// for nal in &nals {
///     println!("{:?}, ref_idc={}, {} bytes",
///              nal.nal_unit_type, nal.nal_ref_idc, nal.rbsp.len());
/// }
/// ```
pub fn parse_annex_b(data: &[u8]) -> Vec<NalUnit<'_>> {
    let mut nals = Vec::new();
    // Find first start code
    let mut i = match find_start_code(data, 0) {
        Some((pos, _)) => pos,
        None => return nals,
    };

    loop {
        // i points to first byte after start code (the NAL header byte)
        if i >= data.len() {
            break;
        }

        // Find the next start code to determine where this NAL ends
        let nal_end = match find_start_code(data, i) {
            Some((pos, sc_start)) => {
                let end = sc_start;
                // Strip trailing zeros before the start code
                let mut e = end;
                while e > i && data[e - 1] == 0 {
                    e -= 1;
                }
                (e, Some(pos))
            }
            None => (data.len(), None),
        };

        if let Some(nal) = parse_nal_bytes(&data[i..nal_end.0]) {
            nals.push(nal);
        }

        match nal_end.1 {
            Some(pos) => i = pos,
            None => break,
        }
    }

    nals
}

/// Parse a single NAL unit from raw bytes (header byte + payload).
/// Returns `None` if the slice is empty or has the forbidden_zero_bit set.
fn parse_nal_bytes(nal_data: &[u8]) -> Option<NalUnit<'_>> {
    if nal_data.is_empty() {
        return None;
    }
    let header = nal_data[0];
    // forbidden_zero_bit (MSB) must be 0
    if header & 0x80 != 0 {
        return None;
    }
    let nal_ref_idc = (header >> 5) & 0x03;
    let nal_unit_type = NalUnitType::from(header & 0x1F);
    let rbsp = remove_emulation_prevention(&nal_data[1..]);
    Some(NalUnit {
        nal_ref_idc,
        nal_unit_type,
        rbsp,
    })
}

/// Configuration parsed from an MP4 `avcC` (AVCDecoderConfigurationRecord) box.
///
/// In MP4/MKV containers, the SPS and PPS parameter sets are stored
/// out-of-band in the `avcC` configuration box rather than inline with the
/// sample data. After parsing the box, callers must feed the SPS/PPS NALs
/// to the decoder once before decoding any samples, then use [`parse_avcc`]
/// (with [`length_size`](Self::length_size)) to parse the length-prefixed
/// NALs from each sample.
///
/// # Example
///
/// ```no_run
/// use rust_h264::decoder::Decoder;
/// use rust_h264::nal::{parse_avcc, parse_avcc_config};
///
/// // Get this from your MP4 demuxer's `avcC` box
/// let avcc_box: &[u8] = unimplemented!();
/// let cfg = parse_avcc_config(avcc_box).unwrap();
///
/// let mut decoder = Decoder::new();
/// // Feed parameter sets once at startup
/// for nal in cfg.sps_nals.iter().chain(cfg.pps_nals.iter()) {
///     decoder.decode_nal(nal).unwrap();
/// }
///
/// // For each MP4 sample, decode its NALs
/// let sample: &[u8] = unimplemented!();
/// for nal in parse_avcc(sample, cfg.length_size) {
///     if let Ok(Some(frame)) = decoder.decode_nal(&nal) {
///         // handle frame
///     }
/// }
/// ```
#[derive(Debug)]
pub struct AvccConfig<'a> {
    /// Number of bytes used for length prefixes in sample data (1, 2, or 4).
    /// Pass this value to [`parse_avcc`] when parsing samples.
    pub length_size: usize,
    /// SPS NAL units extracted from the configuration record. Feed these to
    /// the decoder before any sample data.
    pub sps_nals: Vec<NalUnit<'a>>,
    /// PPS NAL units extracted from the configuration record. Feed these to
    /// the decoder before any sample data.
    pub pps_nals: Vec<NalUnit<'a>>,
}

/// Parse an MP4 `avcC` (AVCDecoderConfigurationRecord) box per ISO/IEC 14496-15.
///
/// The input is the raw box payload (not including the box header). Returns
/// the SPS/PPS NAL units and the length-field size needed by `parse_avcc`.
///
/// Layout:
/// ```text
/// configurationVersion         u8 (must be 1)
/// AVCProfileIndication         u8
/// profile_compatibility        u8
/// AVCLevelIndication           u8
/// reserved (6 bits) | lengthSizeMinusOne (2 bits)  u8
/// reserved (3 bits) | numOfSequenceParameterSets (5 bits)  u8
/// for each SPS:
///   sequenceParameterSetLength u16 (big-endian)
///   sequenceParameterSetNALUnit
/// numOfPictureParameterSets    u8
/// for each PPS:
///   pictureParameterSetLength  u16 (big-endian)
///   pictureParameterSetNALUnit
/// ```
pub fn parse_avcc_config(data: &[u8]) -> Result<AvccConfig<'_>, &'static str> {
    if data.len() < 7 {
        return Err("avcC: too short");
    }
    if data[0] != 1 {
        return Err("avcC: unsupported configurationVersion");
    }
    // data[1..4] are profile/compat/level — informational, not needed here
    let length_size = ((data[4] & 0x03) + 1) as usize;
    if length_size != 1 && length_size != 2 && length_size != 4 {
        return Err("avcC: invalid lengthSizeMinusOne");
    }
    let num_sps = (data[5] & 0x1F) as usize;

    let mut off = 6;
    let mut sps_nals = Vec::with_capacity(num_sps);
    for _ in 0..num_sps {
        if off + 2 > data.len() {
            return Err("avcC: truncated SPS length");
        }
        let len = u16::from_be_bytes([data[off], data[off + 1]]) as usize;
        off += 2;
        if off + len > data.len() {
            return Err("avcC: truncated SPS data");
        }
        if let Some(nal) = parse_nal_bytes(&data[off..off + len]) {
            sps_nals.push(nal);
        }
        off += len;
    }

    if off >= data.len() {
        return Err("avcC: missing PPS count");
    }
    let num_pps = data[off] as usize;
    off += 1;

    let mut pps_nals = Vec::with_capacity(num_pps);
    for _ in 0..num_pps {
        if off + 2 > data.len() {
            return Err("avcC: truncated PPS length");
        }
        let len = u16::from_be_bytes([data[off], data[off + 1]]) as usize;
        off += 2;
        if off + len > data.len() {
            return Err("avcC: truncated PPS data");
        }
        if let Some(nal) = parse_nal_bytes(&data[off..off + len]) {
            pps_nals.push(nal);
        }
        off += len;
    }

    Ok(AvccConfig {
        length_size,
        sps_nals,
        pps_nals,
    })
}

/// Parse a length-prefixed AVCC sample into NAL units.
///
/// `data` is the raw sample payload from an MP4 `mdat` chunk. `length_size`
/// is the number of bytes used for each length prefix (1, 2, or 4 — typically
/// 4, taken from `AvccConfig::length_size`).
///
/// Note: this only parses the per-sample NAL units. The SPS/PPS configuration
/// is stored separately in the MP4 `avcC` box and must be parsed with
/// `parse_avcc_config` and fed to the decoder before any sample NALs.
pub fn parse_avcc(data: &[u8], length_size: usize) -> Vec<NalUnit<'_>> {
    let mut nals = Vec::new();
    if length_size != 1 && length_size != 2 && length_size != 4 {
        return nals;
    }
    let mut i = 0;
    while i + length_size <= data.len() {
        let len = match length_size {
            1 => data[i] as usize,
            2 => u16::from_be_bytes([data[i], data[i + 1]]) as usize,
            4 => u32::from_be_bytes([data[i], data[i + 1], data[i + 2], data[i + 3]]) as usize,
            _ => unreachable!(),
        };
        i += length_size;
        if i + len > data.len() {
            // Truncated NAL — stop parsing
            break;
        }
        if let Some(nal) = parse_nal_bytes(&data[i..i + len]) {
            nals.push(nal);
        }
        i += len;
    }
    nals
}

/// Find the next start code starting from `offset`.
/// Returns (position after start code, position of start code beginning).
fn find_start_code(data: &[u8], offset: usize) -> Option<(usize, usize)> {
    let mut i = offset;
    while i + 2 < data.len() {
        if data[i] == 0 && data[i + 1] == 0 {
            if data[i + 2] == 1 {
                return Some((i + 3, i));
            }
            if i + 3 < data.len() && data[i + 2] == 0 && data[i + 3] == 1 {
                return Some((i + 4, i));
            }
        }
        i += 1;
    }
    None
}

/// Remove emulation prevention bytes (0x03 in 00 00 03 sequences).
/// Returns a borrowed slice when no emulation prevention bytes are found
/// (the common case), avoiding allocation entirely.
fn remove_emulation_prevention(data: &[u8]) -> Cow<'_, [u8]> {
    // Fast path: scan for 00 00 03. If none found, return borrowed slice.
    let has_epb = data.windows(3).any(|w| w[0] == 0 && w[1] == 0 && w[2] == 3);
    if !has_epb {
        return Cow::Borrowed(data);
    }

    // Slow path: copy with emulation prevention removal.
    let mut rbsp = Vec::with_capacity(data.len());
    let mut i = 0;
    while i < data.len() {
        if i + 2 < data.len() && data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 3 {
            rbsp.push(0);
            rbsp.push(0);
            i += 3; // skip the 0x03 byte
        } else {
            rbsp.push(data[i]);
            i += 1;
        }
    }
    Cow::Owned(rbsp)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_annex_b_single_frame() {
        let data = std::fs::read(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/testdata/single_frame.h264"
        ))
        .unwrap();
        let nals = parse_annex_b(&data);
        assert_eq!(nals.len(), 4);
        assert_eq!(nals[0].nal_unit_type, NalUnitType::Sps);
        assert_eq!(nals[1].nal_unit_type, NalUnitType::Pps);
        assert_eq!(nals[2].nal_unit_type, NalUnitType::Sei);
        assert_eq!(nals[3].nal_unit_type, NalUnitType::SliceIdr);
    }

    #[test]
    fn test_emulation_prevention_removal() {
        let input = [0x00, 0x00, 0x03, 0x01, 0xAB];
        let rbsp = remove_emulation_prevention(&input);
        assert_eq!(&*rbsp, &[0x00, 0x00, 0x01, 0xAB]);
        assert!(
            matches!(rbsp, Cow::Owned(_)),
            "should allocate when EPB present"
        );
    }

    #[test]
    fn test_emulation_prevention_zero_copy() {
        // No emulation prevention bytes → should return borrowed slice (no allocation)
        let input = [0x01, 0x02, 0x03, 0x04];
        let rbsp = remove_emulation_prevention(&input);
        assert_eq!(&*rbsp, &input);
        assert!(
            matches!(rbsp, Cow::Borrowed(_)),
            "should borrow when no EPB"
        );
    }

    #[test]
    fn test_parse_avcc_two_nals_4byte_length() {
        // Two NAL units with 4-byte length prefixes:
        // SliceIdr (header byte 0x65 = nal_ref_idc=3, type=5) with payload [0xAA]
        // Sps (header byte 0x67 = nal_ref_idc=3, type=7) with payload [0xBB, 0xCC]
        let data = [
            0x00, 0x00, 0x00, 0x02, // length = 2
            0x65, 0xAA,             // IDR
            0x00, 0x00, 0x00, 0x03, // length = 3
            0x67, 0xBB, 0xCC,       // SPS
        ];
        let nals = parse_avcc(&data, 4);
        assert_eq!(nals.len(), 2);
        assert_eq!(nals[0].nal_unit_type, NalUnitType::SliceIdr);
        assert_eq!(nals[0].nal_ref_idc, 3);
        assert_eq!(&*nals[0].rbsp, &[0xAA]);
        assert_eq!(nals[1].nal_unit_type, NalUnitType::Sps);
        assert_eq!(&*nals[1].rbsp, &[0xBB, 0xCC]);
    }

    #[test]
    fn test_parse_avcc_truncated() {
        // Length says 10 bytes but only 2 follow → stop parsing
        let data = [0x00, 0x00, 0x00, 0x0A, 0x65, 0xAA];
        let nals = parse_avcc(&data, 4);
        assert_eq!(nals.len(), 0);
    }

    #[test]
    fn test_parse_avcc_2byte_length() {
        let data = [
            0x00, 0x02, 0x65, 0xAA, // length=2, IDR + payload
            0x00, 0x01, 0x67,       // length=1, SPS header only
        ];
        let nals = parse_avcc(&data, 2);
        assert_eq!(nals.len(), 2);
        assert_eq!(nals[0].nal_unit_type, NalUnitType::SliceIdr);
        assert_eq!(nals[1].nal_unit_type, NalUnitType::Sps);
    }

    #[test]
    fn test_parse_avcc_config_minimal() {
        // Minimal avcC: 1 SPS, 1 PPS, length_size=4
        // Bytes: version=1, profile=66, compat=0, level=30,
        //        reserved+lengthSize: 0xFF (lengthSizeMinusOne=3 → length_size=4)
        //        reserved+numSPS: 0xE1 (numSPS=1)
        //        sps_len=4, sps=[0x67, 0x42, 0x00, 0x1E]
        //        numPPS=1
        //        pps_len=2, pps=[0x68, 0xCE]
        let data = [
            0x01, 0x42, 0x00, 0x1E,
            0xFF,                       // lengthSizeMinusOne = 3
            0xE1,                       // numOfSequenceParameterSets = 1
            0x00, 0x04,                 // sps length
            0x67, 0x42, 0x00, 0x1E,     // sps NAL (header + 3 bytes RBSP)
            0x01,                       // numOfPictureParameterSets = 1
            0x00, 0x02,                 // pps length
            0x68, 0xCE,                 // pps NAL
        ];
        let cfg = parse_avcc_config(&data).unwrap();
        assert_eq!(cfg.length_size, 4);
        assert_eq!(cfg.sps_nals.len(), 1);
        assert_eq!(cfg.sps_nals[0].nal_unit_type, NalUnitType::Sps);
        assert_eq!(&*cfg.sps_nals[0].rbsp, &[0x42, 0x00, 0x1E]);
        assert_eq!(cfg.pps_nals.len(), 1);
        assert_eq!(cfg.pps_nals[0].nal_unit_type, NalUnitType::Pps);
        assert_eq!(&*cfg.pps_nals[0].rbsp, &[0xCE]);
    }

    #[test]
    fn test_parse_avcc_config_invalid_version() {
        let data = [0x02, 0x42, 0x00, 0x1E, 0xFF, 0xE0, 0x00];
        assert!(parse_avcc_config(&data).is_err());
    }

    #[test]
    fn test_parse_avcc_config_truncated() {
        let data = [0x01, 0x42];
        assert!(parse_avcc_config(&data).is_err());
    }
}