dvb-si 17.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
//! Sub-table identity beyond the generic long-form header.
//!
//! A sub_table is told apart by `table_id`, `table_id_extension` and
//! `current_next_indicator` (ISO/IEC 13818-1 §2.4.4, EN 300 468 §5.2); two
//! tables carry further identifying fields outside that header, so the header
//! alone merges distinct sub_tables of them:
//!
//! - the service_description_section (EN 300 468 §5.2.3 Table 5,
//!   `private/specs-verified/dvb/dvb-si/tables/en_300_468/5-service-description-section.md`)
//!   puts `transport_stream_id` in the extension and `original_network_id`
//!   after it;
//! - the event_information_section (§5.2.4 Table 7,
//!   `private/specs-verified/dvb/dvb-si/tables/en_300_468/7-event-information-section.md`)
//!   puts `service_id` in the extension, followed by `transport_stream_id` and
//!   `original_network_id`.
//!
//! The NIT (`network_id`), BAT (`bouquet_id`) and PMT (`program_number`) are
//! identified by their extension alone.

use crate::table_id::TableId;

/// The identifying fields a section carries after its generic header.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct SubTableQualifier {
    /// `transport_stream_id`, where the table carries it beyond the extension
    /// (the EIT).
    pub transport_stream_id: Option<u16>,
    /// `original_network_id`.
    pub original_network_id: u16,
}

const ONID_AT: usize = 0;
const EIT_TSID_AT: usize = 0;
const EIT_ONID_AT: usize = 2;
const EIT_FIRST_TABLE_ID: u8 = TableId::EventInformationPfActual as u8;
const EIT_LAST_TABLE_ID: u8 = 0x6F;

impl SubTableQualifier {
    /// The qualifier of a long-form section of `table_id` whose payload (the
    /// bytes after the 8-byte long-form header, before the CRC) is `payload`:
    /// `None` for a table the extension alone identifies, and for a payload
    /// too short to hold the fields its table carries: such a section is
    /// keyed as best it can be rather than refused.
    #[must_use]
    pub fn of(table_id: u8, payload: &[u8]) -> Option<Self> {
        let be16 = |at: usize| crate::wire::be_u16(payload, at, "sub_table qualifier").ok();
        if table_id == TableId::ServiceDescriptionActual as u8
            || table_id == TableId::ServiceDescriptionOther as u8
        {
            return Some(Self {
                transport_stream_id: None,
                original_network_id: be16(ONID_AT)?,
            });
        }
        if (EIT_FIRST_TABLE_ID..=EIT_LAST_TABLE_ID).contains(&table_id) {
            return Some(Self {
                transport_stream_id: Some(be16(EIT_TSID_AT)?),
                original_network_id: be16(EIT_ONID_AT)?,
            });
        }
        None
    }
}

/// The sub_table a long-form section belongs to, within its `table_id`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct SubTableId {
    /// `table_id_extension`.
    pub table_id_extension: u16,
    /// `current_next_indicator`: a `next` sub_table is not the current one.
    pub current_next_indicator: bool,
    /// The identifying fields outside the generic header, if the table has
    /// any.
    pub qualifier: Option<SubTableQualifier>,
}

impl SubTableId {
    /// The sub_table an extension and `current_next_indicator` name, for a
    /// table with no qualifier.
    #[must_use]
    pub const fn new(table_id_extension: u16, current_next_indicator: bool) -> Self {
        Self {
            table_id_extension,
            current_next_indicator,
            qualifier: None,
        }
    }

    /// The sub_table of the long-form section `section`.
    #[must_use]
    pub fn of_section(section: &mpeg_ts::section::Section<'_>) -> Self {
        Self {
            table_id_extension: section.extension_id,
            current_next_indicator: section.current_next_indicator,
            qualifier: SubTableQualifier::of(section.table_id, section.payload),
        }
    }

    /// This sub_table with `qualifier`.
    #[must_use]
    pub const fn with_qualifier(mut self, qualifier: SubTableQualifier) -> Self {
        self.qualifier = Some(qualifier);
        self
    }
}