dvb-si 3.1.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Event Information Table — ETSI EN 300 468 §5.2.4.
//!
//! EIT carries programme event metadata. Four variants distinguished by
//! table_id:
//! - `0x4E` — Present/Following for the actual TS
//! - `0x4F` — Present/Following for another TS
//! - `0x50..=0x5F` — Schedule sub-tables for the actual TS
//! - `0x60..=0x6F` — Schedule sub-tables for another TS

use crate::descriptors::DescriptorLoop;
use crate::error::{Error, Result};
use crate::traits::Table;
use dvb_common::{Parse, Serialize};

/// table_id for present/following on the actual TS.
pub const TABLE_ID_PF_ACTUAL: u8 = 0x4E;
/// table_id for present/following on other TSes.
pub const TABLE_ID_PF_OTHER: u8 = 0x4F;
/// First table_id in the schedule range for the actual TS.
pub const TABLE_ID_SCHEDULE_ACTUAL_FIRST: u8 = 0x50;
/// Last table_id in the schedule range for the actual TS (inclusive).
pub const TABLE_ID_SCHEDULE_ACTUAL_LAST: u8 = 0x5F;
/// First table_id in the schedule range for other TSes.
pub const TABLE_ID_SCHEDULE_OTHER_FIRST: u8 = 0x60;
/// Last table_id in the schedule range for other TSes (inclusive).
pub const TABLE_ID_SCHEDULE_OTHER_LAST: u8 = 0x6F;
/// Well-known PID on which EIT is carried.
pub const PID: u16 = 0x0012;

const MIN_HEADER_LEN: usize = 3;
const EXTENSION_HEADER_LEN: usize = 5;
/// transport_stream_id(2) + original_network_id(2) + segment_last_section_number(1)
/// + last_table_id(1) = 6 bytes between the section header and the first event.
const POST_EXTENSION_LEN: usize = 6;
const CRC_LEN: usize = 4;
const EVENT_HEADER_LEN: usize = 12;

/// EIT variant distinguished by table_id range.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum EitKind {
    /// Present/Following, actual TS.
    PresentFollowingActual,
    /// Present/Following, other TS.
    PresentFollowingOther,
    /// Schedule, actual TS — table_id `0x50..=0x5F`.
    ScheduleActual,
    /// Schedule, other TS — table_id `0x60..=0x6F`.
    ScheduleOther,
}

impl EitKind {
    /// Classify a table_id byte into a kind, if recognised.
    #[must_use]
    pub fn from_table_id(table_id: u8) -> Option<Self> {
        match table_id {
            TABLE_ID_PF_ACTUAL => Some(Self::PresentFollowingActual),
            TABLE_ID_PF_OTHER => Some(Self::PresentFollowingOther),
            TABLE_ID_SCHEDULE_ACTUAL_FIRST..=TABLE_ID_SCHEDULE_ACTUAL_LAST => {
                Some(Self::ScheduleActual)
            }
            TABLE_ID_SCHEDULE_OTHER_FIRST..=TABLE_ID_SCHEDULE_OTHER_LAST => {
                Some(Self::ScheduleOther)
            }
            _ => None,
        }
    }
}

/// One event in the EIT.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct EitEvent<'a> {
    /// 16-bit event_id.
    pub event_id: u16,
    /// 40-bit start_time: 16-bit MJD followed by 24-bit BCD UTC (HHMMSS).
    pub start_time_raw: [u8; 5],
    /// 24-bit BCD duration HHMMSS.
    pub duration_raw: [u8; 3],
    /// 3-bit running_status.
    pub running_status: u8,
    /// free_CA_mode flag.
    pub free_ca_mode: bool,
    /// Descriptor loop for this event. Serializes as the typed descriptor
    /// sequence; `.raw()` yields the wire bytes.
    pub descriptors: DescriptorLoop<'a>,
}

/// Event Information Table.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct Eit<'a> {
    /// Variant based on table_id.
    pub kind: EitKind,
    /// Raw table_id byte as parsed (for schedule sub-tables, identifies the slot).
    pub table_id: u8,
    /// service_id the events belong to (table_id_extension).
    pub service_id: u16,
    /// 5-bit version_number.
    pub version_number: u8,
    /// current_next_indicator bit.
    pub current_next_indicator: bool,
    /// section_number.
    pub section_number: u8,
    /// last_section_number.
    pub last_section_number: u8,
    /// transport_stream_id the events are carried on.
    pub transport_stream_id: u16,
    /// original_network_id.
    pub original_network_id: u16,
    /// segment_last_section_number.
    pub segment_last_section_number: u8,
    /// last_table_id (for schedule sub-table grouping).
    pub last_table_id: u8,
    /// Events in wire order.
    pub events: Vec<EitEvent<'a>>,
}

impl<'a> Parse<'a> for Eit<'a> {
    type Error = crate::error::Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let min_len = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + CRC_LEN;
        if bytes.len() < min_len {
            return Err(Error::BufferTooShort {
                need: min_len,
                have: bytes.len(),
                what: "Eit",
            });
        }

        let table_id = bytes[0];
        let kind = EitKind::from_table_id(table_id).ok_or(Error::UnexpectedTableId {
            table_id,
            what: "Eit",
            expected: &[
                TABLE_ID_PF_ACTUAL,
                TABLE_ID_PF_OTHER,
                TABLE_ID_SCHEDULE_ACTUAL_FIRST,
                TABLE_ID_SCHEDULE_OTHER_FIRST,
            ],
        })?;

        let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
        let total = MIN_HEADER_LEN + section_length as usize;
        if bytes.len() < total {
            return Err(Error::SectionLengthOverflow {
                declared: section_length as usize,
                available: bytes.len() - MIN_HEADER_LEN,
            });
        }

        let service_id = u16::from_be_bytes([bytes[3], bytes[4]]);
        let version_number = (bytes[5] >> 1) & 0x1F;
        let current_next_indicator = (bytes[5] & 0x01) != 0;
        let section_number = bytes[6];
        let last_section_number = bytes[7];

        let transport_stream_id = u16::from_be_bytes([bytes[8], bytes[9]]);
        let original_network_id = u16::from_be_bytes([bytes[10], bytes[11]]);
        let segment_last_section_number = bytes[12];
        let last_table_id = bytes[13];

        let events_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN;
        let events_end = total - CRC_LEN;
        let mut events = Vec::new();
        let mut pos = events_start;
        while pos + EVENT_HEADER_LEN <= events_end {
            let event_id = u16::from_be_bytes([bytes[pos], bytes[pos + 1]]);
            let start_time_raw = [
                bytes[pos + 2],
                bytes[pos + 3],
                bytes[pos + 4],
                bytes[pos + 5],
                bytes[pos + 6],
            ];
            let duration_raw = [bytes[pos + 7], bytes[pos + 8], bytes[pos + 9]];
            let status_and_len_hi = bytes[pos + 10];
            let running_status = (status_and_len_hi >> 5) & 0x07;
            let free_ca_mode = (status_and_len_hi & 0x10) != 0;
            let descriptors_loop_length =
                (((status_and_len_hi & 0x0F) as usize) << 8) | bytes[pos + 11] as usize;
            let desc_start = pos + EVENT_HEADER_LEN;
            let desc_end = desc_start + descriptors_loop_length;
            if desc_end > events_end {
                return Err(Error::SectionLengthOverflow {
                    declared: descriptors_loop_length,
                    available: events_end - desc_start,
                });
            }
            events.push(EitEvent {
                event_id,
                start_time_raw,
                duration_raw,
                running_status,
                free_ca_mode,
                descriptors: DescriptorLoop::new(&bytes[desc_start..desc_end]),
            });
            pos = desc_end;
        }

        Ok(Eit {
            kind,
            table_id,
            service_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            transport_stream_id,
            original_network_id,
            segment_last_section_number,
            last_table_id,
            events,
        })
    }
}

impl Serialize for Eit<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        let ev_bytes: usize = self
            .events
            .iter()
            .map(|e| EVENT_HEADER_LEN + e.descriptors.len())
            .sum();
        MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + ev_bytes + CRC_LEN
    }

    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 section_length: u16 = (len - MIN_HEADER_LEN) as u16;
        buf[0] = self.table_id;
        buf[1] = 0xB0 | ((section_length >> 8) as u8 & 0x0F);
        buf[2] = (section_length & 0xFF) as u8;
        buf[3..5].copy_from_slice(&self.service_id.to_be_bytes());
        buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
        buf[6] = self.section_number;
        buf[7] = self.last_section_number;
        buf[8..10].copy_from_slice(&self.transport_stream_id.to_be_bytes());
        buf[10..12].copy_from_slice(&self.original_network_id.to_be_bytes());
        buf[12] = self.segment_last_section_number;
        buf[13] = self.last_table_id;

        let mut pos = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN;
        for ev in &self.events {
            buf[pos..pos + 2].copy_from_slice(&ev.event_id.to_be_bytes());
            buf[pos + 2..pos + 7].copy_from_slice(&ev.start_time_raw);
            buf[pos + 7..pos + 10].copy_from_slice(&ev.duration_raw);
            let dll = ev.descriptors.len() as u16;
            buf[pos + 10] = ((ev.running_status & 0x07) << 5)
                | (u8::from(ev.free_ca_mode) << 4)
                | ((dll >> 8) as u8 & 0x0F);
            buf[pos + 11] = (dll & 0xFF) as u8;
            let desc_start = pos + EVENT_HEADER_LEN;
            buf[desc_start..desc_start + ev.descriptors.len()]
                .copy_from_slice(ev.descriptors.raw());
            pos = desc_start + ev.descriptors.len();
        }

        let crc_pos = len - CRC_LEN;
        let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
        buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
        Ok(len)
    }
}

impl<'a> Table<'a> for Eit<'a> {
    const TABLE_ID: u8 = TABLE_ID_PF_ACTUAL;
    const PID: u16 = PID;
}

impl<'a> crate::traits::TableDef<'a> for Eit<'a> {
    const TABLE_ID_RANGES: &'static [(u8, u8)] =
        &[(TABLE_ID_PF_ACTUAL, TABLE_ID_SCHEDULE_OTHER_LAST)];
    const NAME: &'static str = "EVENT_INFORMATION";
}

#[cfg(feature = "chrono")]
impl EitEvent<'_> {
    /// Decode `start_time_raw` (16-bit MJD + 24-bit BCD UTC) to a UTC datetime.
    /// Returns `None` if the BCD nibbles are out of range.
    ///
    /// MJD→calendar conversion per ETSI EN 300 468 Annex C.
    #[must_use]
    pub fn start_time(&self) -> Option<chrono::DateTime<chrono::Utc>> {
        use chrono::{NaiveDate, NaiveDateTime, TimeZone};
        let mjd = u16::from_be_bytes([self.start_time_raw[0], self.start_time_raw[1]]);
        let (y, m, d) = mjd_to_ymd(mjd);
        let h = bcd_byte(self.start_time_raw[2])?;
        let mi = bcd_byte(self.start_time_raw[3])?;
        let s = bcd_byte(self.start_time_raw[4])?;
        let date = NaiveDate::from_ymd_opt(y, m, d)?;
        let time = chrono::NaiveTime::from_hms_opt(u32::from(h), u32::from(mi), u32::from(s))?;
        let naive = NaiveDateTime::new(date, time);
        chrono::Utc.from_local_datetime(&naive).single()
    }
}

#[cfg(feature = "chrono")]
fn bcd_byte(b: u8) -> Option<u8> {
    let hi = b >> 4;
    let lo = b & 0x0F;
    if hi > 9 || lo > 9 {
        return None;
    }
    Some(hi * 10 + lo)
}

#[cfg(feature = "chrono")]
fn mjd_to_ymd(mjd: u16) -> (i32, u32, u32) {
    // ETSI EN 300 468 Annex C: Y', M', K, Y, M, D via the Zeller-like formula.
    let mjd = i64::from(mjd);
    let y_prime = ((mjd as f64 - 15_078.2) / 365.25) as i64;
    let m_prime = ((mjd as f64 - 14_956.1 - (y_prime as f64 * 365.25).floor()) / 30.6001) as i64;
    let d = mjd
        - 14_956
        - (y_prime as f64 * 365.25).floor() as i64
        - (m_prime as f64 * 30.6001).floor() as i64;
    let k = if m_prime == 14 || m_prime == 15 { 1 } else { 0 };
    let y = y_prime + k + 1900;
    let m = m_prime - 1 - k * 12;
    (y as i32, m as u32, d as u32)
}

#[cfg(test)]
mod tests {
    use super::*;

    type TestEvent = (u16, [u8; 5], [u8; 3], u8, bool, Vec<u8>);

    fn build_eit(
        table_id: u8,
        service_id: u16,
        version: u8,
        tsid: u16,
        onid: u16,
        events: &[TestEvent],
    ) -> Vec<u8> {
        let ev_bytes: usize = events
            .iter()
            .map(|(_, _, _, _, _, d)| EVENT_HEADER_LEN + d.len())
            .sum();
        let section_length: u16 =
            (EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + ev_bytes + CRC_LEN) as u16;
        let mut v = Vec::new();
        v.push(table_id);
        v.push(0xB0 | ((section_length >> 8) as u8 & 0x0F));
        v.push((section_length & 0xFF) as u8);
        v.extend_from_slice(&service_id.to_be_bytes());
        v.push(0xC0 | ((version & 0x1F) << 1) | 0x01);
        v.push(0);
        v.push(0);
        v.extend_from_slice(&tsid.to_be_bytes());
        v.extend_from_slice(&onid.to_be_bytes());
        v.push(0);
        v.push(table_id);
        for (eid, start, dur, rs, fca, desc) in events {
            v.extend_from_slice(&eid.to_be_bytes());
            v.extend_from_slice(start);
            v.extend_from_slice(dur);
            let dll = desc.len() as u16;
            v.push(((*rs & 0x07) << 5) | (u8::from(*fca) << 4) | ((dll >> 8) as u8 & 0x0F));
            v.push((dll & 0xFF) as u8);
            v.extend_from_slice(desc);
        }
        v.extend_from_slice(&[0, 0, 0, 0]);
        v
    }

    #[test]
    fn parse_pf_actual_and_other_map_to_correct_kind() {
        for (tid, expected) in [
            (TABLE_ID_PF_ACTUAL, EitKind::PresentFollowingActual),
            (TABLE_ID_PF_OTHER, EitKind::PresentFollowingOther),
        ] {
            let bytes = build_eit(tid, 1, 0, 0x20, 0x30, &[]);
            assert_eq!(Eit::parse(&bytes).unwrap().kind, expected);
        }
    }

    #[test]
    fn schedule_tables_0x50_through_0x5f_all_decode_as_schedule_actual() {
        for tid in TABLE_ID_SCHEDULE_ACTUAL_FIRST..=TABLE_ID_SCHEDULE_ACTUAL_LAST {
            let bytes = build_eit(tid, 1, 0, 0x20, 0x30, &[]);
            assert_eq!(Eit::parse(&bytes).unwrap().kind, EitKind::ScheduleActual);
        }
    }

    #[test]
    fn schedule_tables_0x60_through_0x6f_all_decode_as_schedule_other() {
        for tid in TABLE_ID_SCHEDULE_OTHER_FIRST..=TABLE_ID_SCHEDULE_OTHER_LAST {
            let bytes = build_eit(tid, 1, 0, 0x20, 0x30, &[]);
            assert_eq!(Eit::parse(&bytes).unwrap().kind, EitKind::ScheduleOther);
        }
    }

    #[test]
    fn event_loop_with_descriptor_bytes_preserved() {
        let desc = vec![0x4D, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05];
        let bytes = build_eit(
            TABLE_ID_PF_ACTUAL,
            1,
            0,
            0x20,
            0x30,
            &[(
                42,
                [0xDF, 0xA1, 0x12, 0x34, 0x56],
                [0x00, 0x30, 0x00],
                4,
                false,
                desc.clone(),
            )],
        );
        let eit = Eit::parse(&bytes).unwrap();
        assert_eq!(eit.events.len(), 1);
        assert_eq!(eit.events[0].event_id, 42);
        assert_eq!(eit.events[0].descriptors.raw(), &desc[..]);
    }

    #[test]
    fn running_status_extracted() {
        let bytes = build_eit(
            TABLE_ID_PF_ACTUAL,
            1,
            0,
            0x20,
            0x30,
            &[(1, [0; 5], [0; 3], 2, false, vec![])],
        );
        assert_eq!(Eit::parse(&bytes).unwrap().events[0].running_status, 2);
    }

    #[test]
    fn free_ca_mode_flag_extracted() {
        let bytes = build_eit(
            TABLE_ID_PF_ACTUAL,
            1,
            0,
            0x20,
            0x30,
            &[(1, [0; 5], [0; 3], 0, true, vec![])],
        );
        assert!(Eit::parse(&bytes).unwrap().events[0].free_ca_mode);
    }

    #[test]
    fn serialize_round_trip_preserves_all_events() {
        let desc1: [u8; 2] = [0x54, 0x00];
        let eit = Eit {
            kind: EitKind::PresentFollowingActual,
            table_id: TABLE_ID_PF_ACTUAL,
            service_id: 0x0100,
            version_number: 3,
            current_next_indicator: true,
            section_number: 0,
            last_section_number: 0,
            transport_stream_id: 0x1234,
            original_network_id: 0x0020,
            segment_last_section_number: 0,
            last_table_id: TABLE_ID_PF_ACTUAL,
            events: vec![
                EitEvent {
                    event_id: 1,
                    start_time_raw: [0xDF, 0xA1, 0x12, 0x34, 0x56],
                    duration_raw: [0x00, 0x30, 0x00],
                    running_status: 4,
                    free_ca_mode: false,
                    descriptors: DescriptorLoop::new(&desc1),
                },
                EitEvent {
                    event_id: 2,
                    start_time_raw: [0xDF, 0xA1, 0x13, 0x00, 0x00],
                    duration_raw: [0x01, 0x00, 0x00],
                    running_status: 1,
                    free_ca_mode: true,
                    descriptors: DescriptorLoop::new(&[]),
                },
            ],
        };
        let mut buf = vec![0u8; eit.serialized_len()];
        eit.serialize_into(&mut buf).unwrap();
        let re = Eit::parse(&buf).unwrap();
        assert_eq!(eit, re);
    }

    #[test]
    fn zero_events_is_valid() {
        let bytes = build_eit(TABLE_ID_PF_ACTUAL, 1, 0, 0x20, 0x30, &[]);
        let eit = Eit::parse(&bytes).unwrap();
        assert_eq!(eit.events.len(), 0);
    }

    #[test]
    #[cfg(feature = "chrono")]
    fn event_start_time_decodes_to_utc_datetime() {
        // MJD 59945 is 2023-01-01 per ETSI EN 300 468 Annex C; BCD time 12:34:56.
        let mjd: u16 = 59945;
        let ev = EitEvent {
            event_id: 1,
            start_time_raw: [(mjd >> 8) as u8, (mjd & 0xFF) as u8, 0x12, 0x34, 0x56],
            duration_raw: [0, 0, 0],
            running_status: 0,
            free_ca_mode: false,
            descriptors: DescriptorLoop::new(&[]),
        };
        let dt = ev.start_time().unwrap();
        use chrono::Datelike;
        assert_eq!(dt.year(), 2023);
        assert_eq!(dt.month(), 1);
        assert_eq!(dt.day(), 1);
        use chrono::Timelike;
        assert_eq!(dt.hour(), 12);
        assert_eq!(dt.minute(), 34);
        assert_eq!(dt.second(), 56);
    }
}