less_avc/
sei.rs

1// Copyright 2022-2023 Andrew D. Straw.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT
5// or http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Supplemental Enhancement Information (SEI) encoding
9
10use alloc::{vec, vec::Vec};
11
12use super::RbspData;
13
14/// User data unregistered [SupplementalEnhancementInformation] message
15#[derive(Debug, PartialEq, Eq)]
16pub struct UserDataUnregistered {
17    pub uuid: [u8; 16],
18    pub payload: Vec<u8>,
19}
20
21impl UserDataUnregistered {
22    pub fn new(uuid: [u8; 16], payload: Vec<u8>) -> Self {
23        Self { uuid, payload }
24    }
25    fn to_sei_payload(&self) -> Vec<u8> {
26        let mut result = self.uuid.to_vec();
27        result.extend(self.payload.clone());
28        result
29    }
30}
31
32/// Supplemental Enhancement Information
33#[derive(Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub enum SupplementalEnhancementInformation {
36    /// User data unregistered message
37    UserDataUnregistered(UserDataUnregistered),
38}
39
40impl SupplementalEnhancementInformation {
41    /// Encode into raw byte sequence payload
42    pub fn to_rbsp(&self) -> RbspData {
43        let (payload_type, payload) = match &self {
44            Self::UserDataUnregistered(udr) => (5u8, udr.to_sei_payload()),
45        };
46        let mut payload_size = payload.len();
47        let mut num_ff_bytes = 0;
48        while payload_size > 255 {
49            num_ff_bytes += 1;
50            payload_size -= 0xff;
51        }
52        let mut result = vec![0xff; num_ff_bytes + 2];
53        let size_idx = result.len() - 1;
54        result[0] = payload_type;
55        result[size_idx] = payload_size.try_into().unwrap();
56
57        result.extend(payload);
58        result.push(0x80); // rbsp_trailing_bits
59        RbspData { data: result }
60    }
61}