dvb-si 11.0.1

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Video Depth Range Descriptor — ETSI EN 300 468 §6.4.16.1 (tag_extension 0x10).
use super::*;
use alloc::vec::Vec;

/// Range type — ETSI EN 300 468 §6.4.16.1 Table 161
/// (`dvb-si/docs/enums/en_300_468/161-range-type-coding.md`).
///
/// 8-bit field. Selects the interpretation of the `range_selector` bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum RangeType {
    /// `0x00` — production disparity hint (`production_disparity_hint_info()`).
    ProductionDisparityHint,
    /// `0x01` — multi-region disparity Supplemental Enhancement Information (SEI) present.
    MultiRegionSei,
    /// `0x02`–`0xFF` — reserved for future use.
    Reserved(u8),
}

impl RangeType {
    #[must_use]
    /// Creates a value from a wire byte, preserving every possible
    /// byte value for lossless round-trip.
    pub fn from_u8(v: u8) -> Self {
        match v {
            0x00 => Self::ProductionDisparityHint,
            0x01 => Self::MultiRegionSei,
            v => Self::Reserved(v),
        }
    }

    #[must_use]
    /// Returns the wire byte for this value.
    pub const fn to_u8(self) -> u8 {
        match self {
            Self::ProductionDisparityHint => 0x00,
            Self::MultiRegionSei => 0x01,
            Self::Reserved(v) => v,
        }
    }

    #[must_use]
    /// Returns the spec token for this value.
    pub fn name(self) -> &'static str {
        match self {
            Self::ProductionDisparityHint => "production disparity hint",
            Self::MultiRegionSei => "multi-region SEI",
            Self::Reserved(_) => "reserved",
        }
    }
}
broadcast_common::impl_spec_display!(RangeType, Reserved);

impl<'a> ExtensionBodyDef<'a> for VideoDepthRange<'a> {
    const TAG_EXTENSION: u8 = 0x10;
    const NAME: &'static str = "VIDEO_DEPTH_RANGE";
}

/// One depth range entry (Table 160 inner loop).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct DepthRange<'a> {
    /// range_type(8) — Table 161 — [`RangeType`].
    pub range_type: RangeType,
    /// Body interpreted by `range_type`.
    pub body: DepthRangeBody<'a>,
}

/// Body of a [`DepthRange`], keyed on `range_type` (Table 161).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
#[non_exhaustive]
pub enum DepthRangeBody<'a> {
    /// `0x00` — production_disparity_hint_info() (Table 162).
    /// Two 12-bit two's-complement signed values packed into 3 bytes.
    ProductionDisparityHint {
        /// video_max_disparity_hint (12 tcimsbf).
        max: i16,
        /// video_min_disparity_hint (12 tcimsbf).
        min: i16,
    },
    /// `0x01` — multi-region SEI present (empty body).
    MultiRegionSei,
    /// Any other `range_type`: raw `range_selector` bytes.
    #[cfg_attr(feature = "serde", serde(borrow))]
    Other(&'a [u8]),
}

/// video_depth_range body (Table 160) — fully typed loop.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct VideoDepthRange<'a> {
    /// Depth range entries in wire order.
    pub ranges: Vec<DepthRange<'a>>,
}

/// Sign-extend a 12-bit two's-complement value to `i16` (bit 11 is the sign).
fn sext12(v: u16) -> i16 {
    if v & 0x800 != 0 {
        (v | 0xF000) as i16
    } else {
        v as i16
    }
}

impl<'a> Parse<'a> for VideoDepthRange<'a> {
    type Error = crate::error::Error;
    fn parse(sel: &'a [u8]) -> Result<Self> {
        let mut pos = 0;
        let mut ranges = Vec::new();
        loop {
            if pos == sel.len() {
                break;
            }
            // Need at least range_type + range_length (Table 160).
            if sel.len() - pos < VD_RANGE_HDR_LEN {
                return Err(Error::BufferTooShort {
                    need: pos + VD_RANGE_HDR_LEN,
                    have: sel.len(),
                    what: "video_depth_range body",
                });
            }
            let range_type = RangeType::from_u8(sel[pos]);
            let range_length = sel[pos + 1] as usize;
            pos += VD_RANGE_HDR_LEN;
            if sel.len() < pos + range_length {
                return Err(Error::BufferTooShort {
                    need: pos + range_length,
                    have: sel.len(),
                    what: "video_depth_range body",
                });
            }
            let body = match range_type {
                // Table 161: production_disparity_hint_info() — Table 162.
                RangeType::ProductionDisparityHint => {
                    if range_length < VD_DISPARITY_LEN {
                        return Err(invalid(
                            "video_depth_range: production_disparity_hint requires 3+ bytes",
                        ));
                    }
                    // Two 12-bit tcimsbf values packed into 3 bytes (b0..b2):
                    let b0 = sel[pos];
                    let b1 = sel[pos + 1];
                    let b2 = sel[pos + 2];
                    let max = sext12((u16::from(b0) << 4) | (u16::from(b1) >> 4));
                    let min = sext12(((u16::from(b1) & 0x0F) << 8) | u16::from(b2));
                    DepthRangeBody::ProductionDisparityHint { max, min }
                }
                // Table 161: multi-region SEI present (no body).
                RangeType::MultiRegionSei => DepthRangeBody::MultiRegionSei,
                // Any other range_type: raw range_selector bytes.
                _ => DepthRangeBody::Other(&sel[pos..pos + range_length]),
            };
            ranges.push(DepthRange { range_type, body });
            pos += range_length;
        }
        Ok(VideoDepthRange { ranges })
    }
}

/// 12-bit two's-complement bounds for disparity hints.
const DISPARITY_MIN: i16 = 2048;
const DISPARITY_MAX: i16 = 2047;

/// Check a 12-bit two's-complement disparity value fits its field.
fn disparity_bits(value: i16, field: &'static str) -> Result<u16> {
    if !(-DISPARITY_MIN..=DISPARITY_MAX).contains(&value) {
        return Err(Error::ValueOutOfRange {
            field,
            reason: "outside 12-bit two's-complement range -2048..=2047",
        });
    }
    Ok(value as u16 & 0x0FFF)
}

impl Serialize for VideoDepthRange<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        self.ranges
            .iter()
            .map(|r| {
                VD_RANGE_HDR_LEN
                    + match &r.body {
                        DepthRangeBody::ProductionDisparityHint { .. } => VD_DISPARITY_LEN,
                        DepthRangeBody::MultiRegionSei => 0,
                        DepthRangeBody::Other(s) => s.len(),
                    }
            })
            .sum()
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        if buf.len() < len {
            return Err(Error::OutputBufferTooSmall {
                need: len,
                have: buf.len(),
            });
        }
        let mut p = 0;
        for r in &self.ranges {
            buf[p] = r.range_type.to_u8();
            match &r.body {
                DepthRangeBody::ProductionDisparityHint { max, min } => {
                    // Table 162: two 12-bit tcimsbf values packed into 3 bytes.
                    buf[p + 1] = VD_DISPARITY_LEN as u8;
                    // Values are 12-bit two's-complement held in an i16;
                    // anything outside -2048..=2047 has no wire encoding
                    // (r03-W3).
                    let max_bits = disparity_bits(*max, "max_tcimsbf")?;
                    let min_bits = disparity_bits(*min, "min_tcimsbf")?;
                    buf[p + 2] = (max_bits >> 4) as u8;
                    buf[p + 3] = (((max_bits & 0x0F) << 4) | ((min_bits >> 8) & 0x0F)) as u8;
                    buf[p + 4] = min_bits as u8;
                    p += VD_RANGE_HDR_LEN + VD_DISPARITY_LEN;
                }
                DepthRangeBody::MultiRegionSei => {
                    buf[p + 1] = 0;
                    p += VD_RANGE_HDR_LEN;
                }
                DepthRangeBody::Other(s) => {
                    buf[p + 1] = broadcast_common::len::fit_u8(s.len(), "depth_range_length")?;
                    buf[p + 2..p + 2 + s.len()].copy_from_slice(s);
                    p += VD_RANGE_HDR_LEN + s.len();
                }
            }
        }
        Ok(len)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::descriptors::extension::test_support::*;
    use crate::descriptors::extension::{ExtensionBody, ExtensionDescriptor, ExtensionTag};

    #[test]
    fn parse_video_depth_range_two_entries_round_trip() {
        // Two depth ranges in one selector:
        //   entry 1: range_type 0x00, range_length 3, disparity max=100 min=-50
        //   entry 2: range_type 0x05, range_length 2, raw [0xAA, 0xBB]
        // max=100 -> 0x0074 bits -> sext12=100; min=-50 -> 0x0032 bits,
        // two's complement of 50 is 0x0FCE, sext12 -> -50.
        let max_val: i16 = 100;
        let min_val: i16 = -50;
        let max_b = max_val as u16 & 0x0FFF; // 0x0064
        let min_b = min_val as u16 & 0x0FFF; // 0x0FCE
        let sel = [
            0x00,
            0x03,
            (max_b >> 4) as u8,
            (((max_b & 0x0F) << 4) | ((min_b >> 8) & 0x0F)) as u8,
            min_b as u8,
            0x05,
            0x02,
            0xAA,
            0xBB,
        ];
        let bytes = wrap(0x10, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        assert_eq!(d.kind(), Some(ExtensionTag::VideoDepthRange));
        match &d.body {
            ExtensionBody::VideoDepthRange(b) => {
                assert_eq!(b.ranges.len(), 2);
                assert_eq!(b.ranges[0].range_type, RangeType::ProductionDisparityHint);
                match &b.ranges[0].body {
                    DepthRangeBody::ProductionDisparityHint { max, min } => {
                        assert_eq!(*max, 100);
                        assert_eq!(*min, -50);
                    }
                    _ => panic!("expected ProductionDisparityHint"),
                }
                assert_eq!(b.ranges[1].range_type, RangeType::Reserved(0x05));
                match &b.ranges[1].body {
                    DepthRangeBody::Other(s) => assert_eq!(s, &[0xAA, 0xBB]),
                    _ => panic!("expected Other"),
                }
            }
            other => panic!("expected VideoDepthRange, got {other:?}"),
        }
        // parse → serialize → parse round-trip (byte-identical)
        round_trip(&d);
    }

    #[test]
    fn parse_video_depth_range_negative_edge_round_trip() {
        // ProductionDisparityHint with max=-1 (0x0FFF), min=0
        let max_val: i16 = -1;
        let min_val: i16 = 0;
        let max_b = max_val as u16 & 0x0FFF;
        let min_b = min_val as u16 & 0x0FFF;
        let sel = [
            0x00,
            0x03,
            (max_b >> 4) as u8,
            (((max_b & 0x0F) << 4) | ((min_b >> 8) & 0x0F)) as u8,
            min_b as u8,
        ];
        let bytes = wrap(0x10, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::VideoDepthRange(b) => {
                assert_eq!(b.ranges.len(), 1);
                match &b.ranges[0].body {
                    DepthRangeBody::ProductionDisparityHint { max, min } => {
                        assert_eq!(*max, -1);
                        assert_eq!(*min, 0);
                    }
                    _ => panic!("expected ProductionDisparityHint"),
                }
            }
            other => panic!("expected VideoDepthRange, got {other:?}"),
        }
        round_trip(&d);
    }

    #[test]
    fn parse_video_depth_range_multi_region_sei_round_trip() {
        // range_type 0x01 with empty body
        let sel = [0x01, 0x00, 0x01, 0x00];
        let bytes = wrap(0x10, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::VideoDepthRange(b) => {
                assert_eq!(b.ranges.len(), 2);
                assert!(matches!(b.ranges[0].body, DepthRangeBody::MultiRegionSei));
                assert!(matches!(b.ranges[1].body, DepthRangeBody::MultiRegionSei));
            }
            other => panic!("expected VideoDepthRange, got {other:?}"),
        }
        round_trip(&d);
    }

    #[test]
    fn parse_video_depth_range_empty_selector() {
        let bytes = wrap(0x10, &[]);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::VideoDepthRange(b) => {
                assert!(b.ranges.is_empty());
            }
            other => panic!("expected VideoDepthRange, got {other:?}"),
        }
        round_trip(&d);
    }

    #[test]
    fn parse_video_depth_range_rejects_truncated() {
        // Only range_type byte, no range_length
        let sel = [0x00];
        let bytes = wrap(0x10, &sel);
        assert!(matches!(
            ExtensionDescriptor::parse(&bytes).unwrap_err(),
            crate::error::Error::BufferTooShort { .. }
        ));
    }

    #[test]
    fn parse_video_depth_range_rejects_overrun() {
        // range_length=5 but only 2 bytes follow
        let sel = [0x00, 0x05, 0xAA, 0xBB];
        let bytes = wrap(0x10, &sel);
        assert!(matches!(
            ExtensionDescriptor::parse(&bytes).unwrap_err(),
            crate::error::Error::BufferTooShort { .. }
        ));
    }

    #[test]
    fn range_type_full_range_round_trip() {
        for v in 0u8..=0xFF {
            let rt = RangeType::from_u8(v);
            assert_eq!(rt.to_u8(), v, "RangeType round-trip failed for 0x{v:02X}");
        }
    }

    #[test]
    fn range_type_known_values() {
        assert_eq!(RangeType::from_u8(0x00), RangeType::ProductionDisparityHint);
        assert_eq!(RangeType::from_u8(0x01), RangeType::MultiRegionSei);
        assert_eq!(RangeType::from_u8(0x02), RangeType::Reserved(0x02));
        assert_eq!(
            RangeType::ProductionDisparityHint.name(),
            "production disparity hint"
        );
        assert_eq!(RangeType::MultiRegionSei.name(), "multi-region SEI");
        assert_eq!(RangeType::Reserved(0xFF).name(), "reserved");
    }

    #[test]
    fn serialize_rejects_disparity_outside_12_bit_twos_complement() {
        // Pre-fix `*max as u16 & 0x0FFF` silently folded 2048 onto 0 (r03-W3).
        let d = VideoDepthRange {
            ranges: alloc::vec![DepthRange {
                range_type: RangeType::ProductionDisparityHint,
                body: DepthRangeBody::ProductionDisparityHint { max: 2048, min: 0 },
            }],
        };
        let mut buf = vec![0u8; d.serialized_len()];
        assert!(matches!(
            d.serialize_into(&mut buf).unwrap_err(),
            Error::ValueOutOfRange {
                field: "max_tcimsbf",
                ..
            }
        ));
        let d = VideoDepthRange {
            ranges: alloc::vec![DepthRange {
                range_type: RangeType::ProductionDisparityHint,
                body: DepthRangeBody::ProductionDisparityHint { max: 0, min: -2049 },
            }],
        };
        let mut buf = vec![0u8; d.serialized_len()];
        assert!(matches!(
            d.serialize_into(&mut buf).unwrap_err(),
            Error::ValueOutOfRange {
                field: "min_tcimsbf",
                ..
            }
        ));
    }
}