Skip to main content

dvb_si/descriptors/
auxiliary_video_stream.rs

1//! Auxiliary Video Stream Descriptor — ISO/IEC 13818-1 §2.6.74, Table 2-98 (tag 0x2F).
2//!
3//! Carries an aux_video_codedstreamtype byte followed by an si_rbsp()
4//! opaque payload from ISO/IEC 23002-3.
5
6use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10/// Descriptor tag for Auxiliary_video_stream_descriptor.
11pub const TAG: u8 = 0x2F;
12const HEADER_LEN: usize = 2;
13const FIXED_LEN: usize = 1;
14
15/// Auxiliary Video Stream Descriptor.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
19pub struct AuxiliaryVideoStreamDescriptor<'a> {
20    /// Auxiliary video coded stream type.
21    pub aux_video_codedstreamtype: u8,
22    /// si_rbsp() from ISO/IEC 23002-3 carried as opaque bytes.
23    #[cfg_attr(feature = "serde", serde(borrow))]
24    pub si_rbsp: &'a [u8],
25}
26
27impl<'a> Parse<'a> for AuxiliaryVideoStreamDescriptor<'a> {
28    type Error = crate::error::Error;
29
30    fn parse(bytes: &'a [u8]) -> Result<Self> {
31        let body = descriptor_body(
32            bytes,
33            TAG,
34            "AuxiliaryVideoStreamDescriptor",
35            "unexpected tag for Auxiliary_video_stream_descriptor",
36        )?;
37        if body.len() < FIXED_LEN {
38            return Err(Error::InvalidDescriptor {
39                tag: TAG,
40                reason: "Auxiliary_video_stream_descriptor length too short (need >= 1)",
41            });
42        }
43        Ok(Self {
44            aux_video_codedstreamtype: body[0],
45            si_rbsp: &body[FIXED_LEN..],
46        })
47    }
48}
49
50impl Serialize for AuxiliaryVideoStreamDescriptor<'_> {
51    type Error = crate::error::Error;
52
53    fn serialized_len(&self) -> usize {
54        HEADER_LEN + FIXED_LEN + self.si_rbsp.len()
55    }
56
57    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
58        let len = self.serialized_len();
59        if buf.len() < len {
60            return Err(Error::OutputBufferTooSmall {
61                need: len,
62                have: buf.len(),
63            });
64        }
65        buf[0] = TAG;
66        buf[1] = (len - HEADER_LEN) as u8;
67        buf[HEADER_LEN] = self.aux_video_codedstreamtype;
68        buf[HEADER_LEN + FIXED_LEN..len].copy_from_slice(self.si_rbsp);
69        Ok(len)
70    }
71}
72impl<'a> crate::traits::DescriptorDef<'a> for AuxiliaryVideoStreamDescriptor<'a> {
73    const TAG: u8 = TAG;
74    const NAME: &'static str = "AUXILIARY_VIDEO_STREAM";
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn parse_minimal() {
83        let bytes = [TAG, 1, 0x42];
84        let d = AuxiliaryVideoStreamDescriptor::parse(&bytes).unwrap();
85        assert_eq!(d.aux_video_codedstreamtype, 0x42);
86        assert!(d.si_rbsp.is_empty());
87    }
88
89    #[test]
90    fn parse_with_rbsp() {
91        let bytes = [TAG, 3, 0x01, 0xAA, 0xBB];
92        let d = AuxiliaryVideoStreamDescriptor::parse(&bytes).unwrap();
93        assert_eq!(d.aux_video_codedstreamtype, 0x01);
94        assert_eq!(d.si_rbsp, &[0xAA, 0xBB]);
95    }
96
97    #[test]
98    fn parse_rejects_wrong_tag() {
99        let err = AuxiliaryVideoStreamDescriptor::parse(&[0x02, 1, 0]).unwrap_err();
100        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x02, .. }));
101    }
102
103    #[test]
104    fn parse_rejects_too_short() {
105        let err = AuxiliaryVideoStreamDescriptor::parse(&[TAG, 0]).unwrap_err();
106        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
107    }
108
109    #[test]
110    fn serialize_round_trip() {
111        let d = AuxiliaryVideoStreamDescriptor {
112            aux_video_codedstreamtype: 0x07,
113            si_rbsp: &[0xDE, 0xAD, 0xBE, 0xEF],
114        };
115        let mut buf = vec![0u8; d.serialized_len()];
116        d.serialize_into(&mut buf).unwrap();
117        let reparsed = AuxiliaryVideoStreamDescriptor::parse(&buf).unwrap();
118        assert_eq!(d, reparsed);
119    }
120
121    #[test]
122    fn serialize_rejects_small_buffer() {
123        let d = AuxiliaryVideoStreamDescriptor {
124            aux_video_codedstreamtype: 0,
125            si_rbsp: &[],
126        };
127        let mut tiny = vec![0u8; 2];
128        let err = d.serialize_into(&mut tiny).unwrap_err();
129        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
130    }
131}