h264-parser 0.4.2

H.264 Annex B stream parser library
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
use crate::nal::{Nal, NalUnitType};
use crate::pps::Pps;
use crate::sei::{SeiMessage, SeiPayload};
use crate::slice::{PictureId, SliceHeader, SliceType};
use crate::sps::Sps;
use std::borrow::Cow;
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccessUnitKind {
    Idr,
    RecoveryPoint(u32),
    NonIdr,
}

#[derive(Debug, Clone)]
pub struct AccessUnit {
    pub nals: Vec<Nal>,
    pub is_keyframe: bool,
    pub kind: AccessUnitKind,
    pub sps: Option<Arc<Sps>>,
    pub pps: Option<Arc<Pps>>,
    pub picture_id: Option<PictureId>,
    parameter_sets: Vec<Nal>,
    first_slice_type: Option<SliceType>,
}

impl AccessUnit {
    pub fn new() -> Self {
        Self {
            nals: Vec::new(),
            is_keyframe: false,
            kind: AccessUnitKind::NonIdr,
            sps: None,
            pps: None,
            picture_id: None,
            parameter_sets: Vec::new(),
            first_slice_type: None,
        }
    }

    pub fn is_keyframe(&self) -> bool {
        self.is_keyframe
    }

    pub fn nals(&self) -> impl Iterator<Item = &Nal> {
        self.nals.iter()
    }

    pub fn to_annexb_bytes(&self) -> Cow<'_, [u8]> {
        let mut bytes = Vec::new();

        for nal in &self.nals {
            Self::push_annexb_nal(&mut bytes, nal);
        }

        Cow::Owned(bytes)
    }

    pub fn to_annexb_webcodec_bytes(&self) -> Cow<'_, [u8]> {
        let mut bytes = Vec::new();

        let include_parameter_sets =
            self.is_keyframe || matches!(self.first_slice_type, Some(SliceType::I));

        if include_parameter_sets {
            for nal in &self.parameter_sets {
                Self::push_annexb_nal_internal(&mut bytes, nal, true);
            }
        }

        for nal in &self.nals {
            let include_nal = if include_parameter_sets {
                !matches!(nal.nal_type, NalUnitType::Sps | NalUnitType::Pps)
            } else {
                true
            };

            if include_nal {
                Self::push_annexb_nal_internal(&mut bytes, nal, true);
            }
        }

        Cow::Owned(bytes)
    }

    pub fn add_nal(&mut self, nal: Nal) {
        if matches!(nal.nal_type, NalUnitType::Sps | NalUnitType::Pps) {
            self.add_parameter_set(nal.clone());
        }

        if nal.nal_type == NalUnitType::IdrSlice {
            self.kind = AccessUnitKind::Idr;
            self.is_keyframe = true;
        }

        self.nals.push(nal);
    }

    pub fn add_parameter_set(&mut self, nal: Nal) {
        if !self.parameter_sets.iter().any(|existing| existing == &nal) {
            self.parameter_sets.push(nal);
        }
    }

    pub(crate) fn note_slice_type(&mut self, slice_type: SliceType) {
        if self.first_slice_type.is_none() {
            self.first_slice_type = Some(slice_type);
        }
    }

    fn push_annexb_nal(bytes: &mut Vec<u8>, nal: &Nal) {
        Self::push_annexb_nal_internal(bytes, nal, false);
    }

    fn push_annexb_nal_internal(bytes: &mut Vec<u8>, nal: &Nal, force_long_start_code: bool) {
        let start_code = if force_long_start_code || nal.start_code_len == 4 {
            &[0x00, 0x00, 0x00, 0x01][..]
        } else {
            &[0x00, 0x00, 0x01][..]
        };

        bytes.extend_from_slice(start_code);

        let header = ((nal.ref_idc & 0b11) << 5) | (nal.nal_type.as_u8() & 0b11111);
        bytes.push(header);

        bytes.extend_from_slice(&nal.ebsp);
    }


    pub fn set_sps(&mut self, sps: Arc<Sps>) {
        self.sps = Some(sps);
    }

    pub fn set_pps(&mut self, pps: Arc<Pps>) {
        self.pps = Some(pps);
    }

    pub fn check_recovery_point(&mut self) {
        for nal in &self.nals {
            if nal.nal_type == NalUnitType::Sei {
                let rbsp = nal.to_rbsp();
                if let Ok(messages) = SeiMessage::parse(&rbsp) {
                    for msg in messages {
                        if let SeiPayload::RecoveryPoint {
                            recovery_frame_cnt, ..
                        } = msg.payload
                        {
                            if recovery_frame_cnt == 0 {
                                self.kind = AccessUnitKind::RecoveryPoint(0);
                                self.is_keyframe = true;
                            } else {
                                self.kind = AccessUnitKind::RecoveryPoint(recovery_frame_cnt);
                            }
                        }
                    }
                }
            }
        }
    }

    pub fn set_picture_id_from_slice(
        &mut self,
        slice_header: &SliceHeader,
        nal_type: NalUnitType,
        sps: &Sps,
    ) {
        self.picture_id = Some(PictureId::from_slice_header(slice_header, nal_type, sps));
    }
}

pub struct AccessUnitBuilder {
    current_au: Option<AccessUnit>,
    current_picture_id: Option<PictureId>,
}

impl AccessUnitBuilder {
    pub fn new() -> Self {
        Self {
            current_au: None,
            current_picture_id: None,
        }
    }

    pub fn is_au_boundary(
        &self,
        nal: &Nal,
        slice_header: Option<&SliceHeader>,
        sps: Option<&Sps>,
    ) -> bool {
        if nal.nal_type == NalUnitType::Aud {
            return true;
        }

        if !nal.is_vcl() {
            return false;
        }

        if self.current_picture_id.is_none() {
            return false;
        }

        if let (Some(header), Some(sps)) = (slice_header, sps) {
            let new_picture_id = PictureId::from_slice_header(header, nal.nal_type, sps);

            if let Some(ref current_id) = self.current_picture_id {
                return &new_picture_id != current_id;
            }
        }

        false
    }

    pub fn add_nal(
        &mut self,
        nal: Nal,
        slice_header: Option<SliceHeader>,
        sps: Option<Arc<Sps>>,
        pps: Option<Arc<Pps>>,
        mut extra_parameter_sets: Vec<Nal>,
    ) -> Option<AccessUnit> {
        let is_boundary = if let (Some(ref header), Some(ref sps_ref)) = (&slice_header, &sps) {
            self.is_au_boundary(&nal, Some(header), Some(sps_ref))
        } else {
            self.is_au_boundary(&nal, None, None)
        };

        let mut completed_au = None;

        if is_boundary && self.current_au.is_some() {
            if let Some(mut au) = self.current_au.take() {
                au.check_recovery_point();
                completed_au = Some(au);
            }
            self.current_picture_id = None;
        }

        if self.current_au.is_none() {
            self.current_au = Some(AccessUnit::new());
        }

        if let Some(ref mut au) = self.current_au {
            if let Some(sps) = sps {
                au.set_sps(sps);
            }

            if let Some(pps) = pps {
                au.set_pps(pps);
            }

            for parameter_set in extra_parameter_sets.drain(..) {
                au.add_parameter_set(parameter_set);
            }

            if let Some(header) = slice_header.as_ref() {
                if nal.is_vcl() {
                    au.note_slice_type(header.slice_type);
                }
            }

            if let (Some(header), Some(ref sps_ref)) = (slice_header.as_ref(), &au.sps) {
                let picture_id = PictureId::from_slice_header(header, nal.nal_type, sps_ref);
                self.current_picture_id = Some(picture_id.clone());
                au.picture_id = Some(picture_id);
            }

            au.add_nal(nal);
        }

        completed_au
    }

    pub fn flush(mut self) -> Option<AccessUnit> {
        if let Some(mut au) = self.current_au.take() {
            au.check_recovery_point();
            Some(au)
        } else {
            None
        }
    }

    pub fn flush_pending(&mut self) -> Option<AccessUnit> {
        if let Some(mut au) = self.current_au.take() {
            au.check_recovery_point();
            self.current_picture_id = None;
            Some(au)
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_access_unit_keyframe_detection() {
        let mut au = AccessUnit::new();
        assert!(!au.is_keyframe());

        let idr_nal = Nal {
            start_code_len: 4,
            ref_idc: 3,
            nal_type: NalUnitType::IdrSlice,
            ebsp: Vec::new(),
        };

        au.add_nal(idr_nal);
        assert!(au.is_keyframe());
        assert_eq!(au.kind, AccessUnitKind::Idr);
    }

    #[test]
    fn test_to_annexb_bytes() {
        let mut au = AccessUnit::new();

        let nal = Nal {
            start_code_len: 3,
            ref_idc: 2,
            nal_type: NalUnitType::Sps,
            ebsp: vec![0x42, 0x00, 0x1f],
        };

        au.add_nal(nal);

        let bytes = au.to_annexb_bytes();
        assert_eq!(&bytes[0..3], &[0x00, 0x00, 0x01]);
        assert_eq!(bytes[3], 0x47);
        assert_eq!(&bytes[4..], &[0x42, 0x00, 0x1f]);
    }

    #[test]
    fn test_to_annexb_webcodec_bytes_includes_parameter_sets() {
        let mut au = AccessUnit::new();

        let sps = Nal {
            start_code_len: 4,
            ref_idc: 3,
            nal_type: NalUnitType::Sps,
            ebsp: vec![0x42, 0x00, 0x1f],
        };

        let pps = Nal {
            start_code_len: 4,
            ref_idc: 3,
            nal_type: NalUnitType::Pps,
            ebsp: vec![0xde, 0xad],
        };

        let slice = Nal {
            start_code_len: 3,
            ref_idc: 3,
            nal_type: NalUnitType::IdrSlice,
            ebsp: vec![0xaa, 0xbb],
        };

        au.add_parameter_set(sps.clone());
        au.add_parameter_set(pps.clone());
        au.add_parameter_set(sps.clone());

        au.add_nal(slice);

        let bytes = au.to_annexb_webcodec_bytes();

        let expected = vec![
            0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x01, 0x68, 0xde,
            0xad, 0x00, 0x00, 0x00, 0x01, 0x65, 0xaa, 0xbb,
        ];

        assert_eq!(bytes.as_ref(), &expected[..]);
    }

    #[test]
    fn test_to_annexb_webcodec_bytes_for_delta_frame_excludes_parameter_sets() {
        let mut au = AccessUnit::new();

        let sps = Nal {
            start_code_len: 4,
            ref_idc: 3,
            nal_type: NalUnitType::Sps,
            ebsp: vec![0x42, 0x00, 0x1f],
        };

        let pps = Nal {
            start_code_len: 4,
            ref_idc: 3,
            nal_type: NalUnitType::Pps,
            ebsp: vec![0xde, 0xad],
        };

        let slice = Nal {
            start_code_len: 4,
            ref_idc: 2,
            nal_type: NalUnitType::NonIdrSlice,
            ebsp: vec![0x11, 0x22],
        };

        au.add_parameter_set(sps);
        au.add_parameter_set(pps);
        au.note_slice_type(SliceType::P);
        au.add_nal(slice.clone());

        let bytes = au.to_annexb_webcodec_bytes();

        let expected = vec![0x00, 0x00, 0x00, 0x01, 0x41, 0x11, 0x22];
        assert_eq!(bytes.as_ref(), &expected[..]);
    }
}