Skip to main content

dvb_si/descriptors/
ibp.rs

1//! IBP Descriptor — ISO/IEC 13818-1 §2.6.34 (tag 0x12).
2//!
3//! Describes the GOP structure of the associated video elementary stream.
4//! `max_gop_length` value 0 is forbidden (§2.6.35).
5
6use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10/// Descriptor tag for ibp_descriptor.
11pub const TAG: u8 = 0x12;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 2;
14
15/// IBP 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 IbpDescriptor {
20    /// Closed GOP flag.
21    pub closed_gop_flag: bool,
22    /// Identical GOP flag.
23    pub identical_gop_flag: bool,
24    /// Maximum GOP length in pictures (14 bits). Value 0 is forbidden.
25    pub max_gop_length: u16,
26}
27
28impl<'a> Parse<'a> for IbpDescriptor {
29    type Error = crate::error::Error;
30
31    fn parse(bytes: &'a [u8]) -> Result<Self> {
32        let body = descriptor_body(
33            bytes,
34            TAG,
35            "IbpDescriptor",
36            "unexpected tag for ibp_descriptor",
37        )?;
38        if body.len() != BODY_LEN as usize {
39            return Err(Error::InvalidDescriptor {
40                tag: TAG,
41                reason: "ibp_descriptor length must equal 2",
42            });
43        }
44        let closed_gop_flag = (body[0] & 0x80) != 0;
45        let identical_gop_flag = (body[0] & 0x40) != 0;
46        let max_gop_length = ((u16::from(body[0]) & 0x3F) << 8) | u16::from(body[1]);
47        Ok(Self {
48            closed_gop_flag,
49            identical_gop_flag,
50            max_gop_length,
51        })
52    }
53}
54
55impl Serialize for IbpDescriptor {
56    type Error = crate::error::Error;
57
58    fn serialized_len(&self) -> usize {
59        HEADER_LEN + (BODY_LEN as usize)
60    }
61
62    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
63        let len = self.serialized_len();
64        if buf.len() < len {
65            return Err(Error::OutputBufferTooSmall {
66                need: len,
67                have: buf.len(),
68            });
69        }
70        buf[0] = TAG;
71        buf[1] = BODY_LEN;
72        buf[HEADER_LEN] = ((self.closed_gop_flag as u8) << 7)
73            | ((self.identical_gop_flag as u8) << 6)
74            | ((self.max_gop_length >> 8) as u8 & 0x3F);
75        buf[HEADER_LEN + 1] = (self.max_gop_length & 0xFF) as u8;
76        Ok(len)
77    }
78}
79impl<'a> crate::traits::DescriptorDef<'a> for IbpDescriptor {
80    const TAG: u8 = TAG;
81    const NAME: &'static str = "IBP";
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn parse() {
90        let bytes = [
91            TAG,
92            2,
93            0b1111_1111,
94            0xAB, // closed=1, identical=1, max_gop=0x3FAB
95        ];
96        let d = IbpDescriptor::parse(&bytes).unwrap();
97        assert!(d.closed_gop_flag);
98        assert!(d.identical_gop_flag);
99        assert_eq!(d.max_gop_length, 0x3FAB);
100    }
101
102    #[test]
103    fn serialize_round_trip() {
104        let d = IbpDescriptor {
105            closed_gop_flag: false,
106            identical_gop_flag: true,
107            max_gop_length: 12,
108        };
109        let mut buf = vec![0u8; d.serialized_len()];
110        d.serialize_into(&mut buf).unwrap();
111        let reparsed = IbpDescriptor::parse(&buf).unwrap();
112        assert_eq!(d, reparsed);
113    }
114
115    #[test]
116    fn max_gop_allowed_max() {
117        let d = IbpDescriptor {
118            closed_gop_flag: false,
119            identical_gop_flag: false,
120            max_gop_length: 0x3FFF,
121        };
122        let mut buf = vec![0u8; d.serialized_len()];
123        d.serialize_into(&mut buf).unwrap();
124        let reparsed = IbpDescriptor::parse(&buf).unwrap();
125        assert_eq!(reparsed.max_gop_length, 0x3FFF);
126    }
127
128    #[test]
129    fn parse_rejects_wrong_tag() {
130        let err = IbpDescriptor::parse(&[0x13, 2, 0, 0]).unwrap_err();
131        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x13, .. }));
132    }
133
134    #[test]
135    fn parse_rejects_wrong_length() {
136        let err = IbpDescriptor::parse(&[TAG, 3, 0, 0, 0]).unwrap_err();
137        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
138    }
139
140    #[test]
141    fn serialize_rejects_small_buffer() {
142        let d = IbpDescriptor {
143            closed_gop_flag: false,
144            identical_gop_flag: false,
145            max_gop_length: 1,
146        };
147        let mut tiny = vec![0u8; 2];
148        let err = d.serialize_into(&mut tiny).unwrap_err();
149        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
150    }
151}