Skip to main content

dvb_si/descriptors/
target_background_grid.rs

1//! Target Background Grid Descriptor — ISO/IEC 13818-1 §2.6.12 (tag 0x07).
2//!
3//! Describes the target background grid size and aspect ratio.
4
5use super::descriptor_body;
6use crate::error::{Error, Result};
7use dvb_common::{Parse, Serialize};
8
9/// Descriptor tag for target_background_grid_descriptor.
10pub const TAG: u8 = 0x07;
11const HEADER_LEN: usize = 2;
12const BODY_LEN: u8 = 4;
13
14/// Target Background Grid Descriptor.
15#[derive(Debug, Clone, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize))]
17#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
18pub struct TargetBackgroundGridDescriptor {
19    /// Horizontal size (14 bits).
20    pub horizontal_size: u16,
21    /// Vertical size (14 bits).
22    pub vertical_size: u16,
23    /// Aspect ratio information (4 bits).
24    pub aspect_ratio_information: u8,
25}
26
27impl<'a> Parse<'a> for TargetBackgroundGridDescriptor {
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            "TargetBackgroundGridDescriptor",
35            "unexpected tag for target_background_grid_descriptor",
36        )?;
37        if body.len() != BODY_LEN as usize {
38            return Err(Error::InvalidDescriptor {
39                tag: TAG,
40                reason: "target_background_grid_descriptor length must equal 4",
41            });
42        }
43        let horizontal_size = (u16::from(body[0]) << 6) | (u16::from(body[1]) >> 2);
44        let vertical_size = ((u16::from(body[1]) & 0x03) << 12)
45            | (u16::from(body[2]) << 4)
46            | (u16::from(body[3]) >> 4);
47        let aspect_ratio_information = body[3] & 0x0F;
48        Ok(Self {
49            horizontal_size,
50            vertical_size,
51            aspect_ratio_information,
52        })
53    }
54}
55
56impl Serialize for TargetBackgroundGridDescriptor {
57    type Error = crate::error::Error;
58
59    fn serialized_len(&self) -> usize {
60        HEADER_LEN + (BODY_LEN as usize)
61    }
62
63    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
64        let len = self.serialized_len();
65        if buf.len() < len {
66            return Err(Error::OutputBufferTooSmall {
67                need: len,
68                have: buf.len(),
69            });
70        }
71        buf[0] = TAG;
72        buf[1] = BODY_LEN;
73        let hs = self.horizontal_size;
74        buf[HEADER_LEN] = (hs >> 6) as u8;
75        buf[HEADER_LEN + 1] = ((hs & 0x3F) << 2) as u8 | ((self.vertical_size >> 12) as u8 & 0x03);
76        buf[HEADER_LEN + 2] = (self.vertical_size >> 4) as u8;
77        buf[HEADER_LEN + 3] =
78            ((self.vertical_size & 0x0F) << 4) as u8 | (self.aspect_ratio_information & 0x0F);
79        Ok(len)
80    }
81}
82impl<'a> crate::traits::DescriptorDef<'a> for TargetBackgroundGridDescriptor {
83    const TAG: u8 = TAG;
84    const NAME: &'static str = "TARGET_BACKGROUND_GRID";
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn parse() {
93        let bytes = [
94            TAG,
95            4,
96            0b0001_0101,
97            0b0101_0000, // horizontal_size = 1364 (0x0554), low 2 bits = 0
98            0b1010_1010,
99            0b1010_0011, // vertical_size 14 bits = 0x0AAA (2730), aspect_ratio = 3
100        ];
101        let d = TargetBackgroundGridDescriptor::parse(&bytes).unwrap();
102        assert_eq!(d.horizontal_size, 0x0554);
103        // vertical_size: low 2 bits of byte1 (=0) | byte2 | high 4 bits of byte3
104        // 0b00_10101010_1010 = 0x0AAA = 2730
105        assert_eq!(d.vertical_size, 0x0AAA);
106        assert_eq!(d.aspect_ratio_information, 3);
107    }
108
109    #[test]
110    fn serialize_round_trip() {
111        let d = TargetBackgroundGridDescriptor {
112            horizontal_size: 1920,
113            vertical_size: 1080,
114            aspect_ratio_information: 0x0F,
115        };
116        let mut buf = vec![0u8; d.serialized_len()];
117        d.serialize_into(&mut buf).unwrap();
118        let reparsed = TargetBackgroundGridDescriptor::parse(&buf).unwrap();
119        assert_eq!(d, reparsed);
120    }
121
122    #[test]
123    fn horizontal_size_max() {
124        let d = TargetBackgroundGridDescriptor {
125            horizontal_size: 0x3FFF,
126            vertical_size: 0,
127            aspect_ratio_information: 0,
128        };
129        let mut buf = vec![0u8; d.serialized_len()];
130        d.serialize_into(&mut buf).unwrap();
131        let reparsed = TargetBackgroundGridDescriptor::parse(&buf).unwrap();
132        assert_eq!(reparsed.horizontal_size, 0x3FFF);
133    }
134
135    #[test]
136    fn parse_rejects_wrong_tag() {
137        let err = TargetBackgroundGridDescriptor::parse(&[0x08, 4, 0, 0, 0, 0]).unwrap_err();
138        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x08, .. }));
139    }
140
141    #[test]
142    fn parse_rejects_wrong_length() {
143        let err = TargetBackgroundGridDescriptor::parse(&[TAG, 3, 0, 0, 0]).unwrap_err();
144        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
145    }
146
147    #[test]
148    fn serialize_rejects_small_buffer() {
149        let d = TargetBackgroundGridDescriptor {
150            horizontal_size: 0,
151            vertical_size: 0,
152            aspect_ratio_information: 0,
153        };
154        let mut tiny = vec![0u8; 3];
155        let err = d.serialize_into(&mut tiny).unwrap_err();
156        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
157    }
158}