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
//! Shared primitives of the rust-broadcast workspace, one layer above the
//! [`broadcast_core`] wire contract: the CRC-32/MPEG-2 that protects PSI/SI
//! sections, the BCD and MJD time codecs of ETSI EN 300 468, the 33-bit
//! PTS/DTS and 27 MHz PCR clock arithmetic of ITU-T H.222.0, wrap-safe
//! counters for RTP and SRT sequence numbers, BER lengths and SMPTE ST 336
//! KLV triplets, odd parity, hex, the SDP media-section model the RTP crates
//! exchange, bounded whole-document reads, and (behind features) base64,
//! ISO 639 to BCP 47 language tags and the bounded XML reader every
//! XML-reading crate shares. Each of these is defined here exactly once so
//! the format crates above (`mpeg-ts`, `mpeg-pes`, `dvb-si`, `scte35-splice`,
//! `srt-runtime`, `transmux`, ...) cannot disagree about it.
//!
//! It depends on `broadcast-core` and re-exports nothing from it: import the
//! `Parse`/`Serialize` traits, bit I/O and error kinds as
//! `broadcast_core::...`. The crate is `no_std` + `alloc` without the `std`
//! feature, forbids `unsafe`, does no I/O outside [`bounded`], and has no
//! shared error enum: each module returns its own small typed error. HTTP and
//! runtime helpers live in `broadcast-net`.
//!
//! # Quickstart
//!
//! Check the CRC of a real PAT section, decode a BCD byte and take a
//! wrap-safe PTS distance:
//!
//! ```
//! use broadcast_common::{bcd, clock33, crc32_mpeg2};
//!
//! let pat = [
//! 0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x04, 0x01, 0xE0, 0x64,
//! 0xF9, 0xB4, 0x63, 0xEF,
//! ];
//! assert_eq!(crc32_mpeg2::verify_trailer(&pat, "PAT"), Some(Ok(())));
//! assert_eq!(crc32_mpeg2::compute(&pat[..pat.len() - 4]), 0xF9B4_63EF);
//!
//! assert_eq!(bcd::from_bcd_byte(0x42), Some(42));
//!
//! let just_before_wrap = clock33::WRAP_33BIT - 10;
//! assert_eq!(clock33::signed_distance(just_before_wrap, 5), 15);
//! assert_eq!(clock33::add(just_before_wrap, 15), 5);
//! ```
//!
//! # How do I...
//!
//! - Compute a CRC-32/MPEG-2: [`crc32_mpeg2::compute`],
//! `examples/crc_and_bcd.rs`.
//! - Check a section's trailing CRC or write one in place:
//! [`crc32_mpeg2::verify_trailer`], [`crc32_mpeg2::seal`]; the Quickstart
//! above checks one.
//! - Decode or encode a BCD byte: [`bcd::from_bcd_byte`],
//! [`bcd::to_bcd_byte`], `examples/crc_and_bcd.rs`.
//! - Decode or encode a wider BCD field: [`bcd::bcd_to_decimal`],
//! [`bcd::decimal_to_bcd`]; no example yet.
//! - Decode a DVB UTC time (MJD + BCD) or BCD duration:
//! [`time::decode_mjd_bcd`], [`time::encode_mjd_bcd`],
//! [`time::decode_bcd_duration`], `examples/dvb_time.rs`.
//! - Read or write a 5-byte PTS/DTS marker-bit field:
//! [`clock33::unpack_marked`], [`clock33::pack_marked`],
//! `examples/timestamps.rs`.
//! - Compare or step PTS/DTS values across the 33-bit wrap:
//! [`clock33::Clock33`], `examples/timestamps.rs`.
//! - Do the same on raw `u64` ticks: [`clock33::signed_distance`],
//! [`clock33::add`]; the Quickstart above.
//! - Build or split a 27 MHz PCR: [`clock33::Clock27`],
//! [`clock33::pcr_split`], `examples/timestamps.rs`.
//! - Unwrap a 16-bit RTP or 31-bit SRT counter onto a timeline:
//! [`modular::Modulus`], [`modular::Unwrapper`], `examples/timestamps.rs`.
//! - Walk or build KLV triplets: [`klv::KlvItem`], [`klv::walk_klv_items`],
//! `examples/klv_items.rs`.
//! - Decode or encode a BER length on its own: [`ber::decode_ber_length`],
//! [`ber::encode_ber_length`]; no example yet.
//! - Check or set odd parity on teletext and CEA-608 bytes:
//! [`parity::has_odd_parity`], [`parity::strip_parity`]; no example yet.
//! - Hex-encode or decode an opaque field: [`hex::hex_encode`],
//! [`hex::hex_decode_into`]; no example yet.
//! - Describe an RTP media section without an SDP library:
//! [`sdp::SdpMedia`], [`sdp::SdpAttribute`]; no example yet.
//! - Read a small document whole under a named size limit:
//! [`bounded::read_bounded`], [`bounded::Limit`]; no example yet.
//! - Read a bounded, DOCTYPE-free XML document into a tree (`xml` feature):
//! [`xml::read_tree`], [`xml_chars::XmlLimits`]; no example yet.
//! - Declare a `#[repr]` spec enum with checked conversions:
//! [`wire_enum!`]; see its documentation.
//!
//! # Choosing limits
//!
//! No limit struct here has a `Default`. Each one offers
//! `for_profile(broadcast_core::Profile)`, a complete preset whose every
//! number is a documented constant, and a typed `with_*` setter per field:
//!
//! | Profile | Sized for |
//! |---|---|
//! | `Embedded` | one program, a few hundred MB of memory, 32-bit or wasm32, few elementary streams (values checked against the 256 MiB budget where exercised, #4834) |
//! | `Device` | single-service playback on a phone, tablet, TV or desktop, about 30 s of read-ahead |
//! | `Server` | a live origin or HLS server carrying several programs |
//! | `Headend` | a whole multiplex with full SI/EPG, up to 2048 PIDs |
//!
//! The presets: `xml_chars::XmlLimits`.
//!
//! # Modules
//!
//! Wire codecs: [`crc32_mpeg2`] (CRC-32/MPEG-2), [`bcd`] (binary-coded
//! decimal), [`time`] (MJD + BCD UTC time and BCD durations), [`parity`]
//! (odd-parity bytes), [`ber`] (BER definite-form lengths), [`klv`] (SMPTE
//! ST 336 Key-Length-Value triplets), [`hex`](mod@hex) (hex text of byte
//! fields), [`base64`](mod@base64) (RFC 4648, feature `base64`).
//!
//! Clocks and counters: [`clock33`] (33-bit PTS/DTS, 27 MHz PCR and the
//! marker-bit timestamp field), [`modular`] (wrap-safe counters of any width
//! up to 63 bits).
//!
//! Session and language vocabulary: [`sdp`] (the SDP media-section model),
//! [`lang`] (ISO 639 to BCP 47, feature `lang`).
//!
//! Bounded input: [`bounded`] (size-limited whole-document reads, `std`),
//! [`xml`] (the bounded namespace-aware XML tree reader, feature `xml`),
//! [`xml_chars`] (XML 1.0 character-data checks, limits and the writer,
//! feature `xml`).
//!
//! Registry support: [`erased`] (the type-erased value face behind the
//! `dvb-si` and `dvb-t2mi` runtime registries, with
//! [`declare_object_trait!`]).
//!
//! # Feature flags
//!
//! - `std` (default): links `std` and adds [`bounded`]; off gives `no_std` +
//! `alloc`.
//! - `jiff`: `jiff::Timestamp` faces of the [`time`] codecs.
//! - `serde`: `serde::Serialize` on the shared types that carry one, and the
//! serde half of [`declare_object_trait!`].
//! - `xml`: [`xml`] and [`xml_chars`] (implies `std`).
//! - `lang`: [`lang`] and the root [`bcp47_language`] (implies `std`).
//! - `base64`: [`base64`](mod@base64).
//!
//! # Spec citations
//!
//! - ITU-T H.222.0 v9 (08/2023) / ISO/IEC 13818-1: the CRC-32 decoder model
//! (Annex A), the 33-bit PTS/DTS and 27 MHz PCR clocks and the marker-bit
//! timestamp field (§2.4.3.4, §2.4.3.7).
//! - ETSI EN 300 468 V1.20.1: the CRC-32 (Annex A), MJD + BCD UTC time and
//! BCD durations (Annex C).
//! - ETSI TS 102 773 V1.4.1 Annex A: the same CRC-32 over T2-MI packets.
//! - ETSI EN 300 706 V1.2.1 §8.1: the odd-parity protected byte.
//! - SMPTE ST 336, as constrained for MXF by SMPTE ST 377-1:2019 §6.3: KLV
//! triplets and BER lengths.
//! - RFC 4566 §5.13, §5.14: the SDP media section.
//! - RFC 4648: base64. RFC 5646: BCP 47 language tags. XML 1.0 §2.2: the
//! `Char` production.
//!
//! # Reserved-bit and trailing-byte policy
//!
//! One workspace-wide rule for every wire crate (issue #1139):
//!
//! - **Parse is tolerant.** A non-conformant reserved bit, or trailing data
//! that real senders emit, is accepted rather than rejected (a crate may
//! choose stricter validation, as `dvb-t2mi` does for RFU bits, but must
//! document it).
//! - **Serialize preserves what was parsed.** Reserved bits are captured on
//! parse in typed, spec-named fields and re-emitted verbatim, so a stream
//! from a sender that deviates from the governing edition re-serializes
//! **byte-identically** (`dvb-si` descriptors, `mpeg-ps`, `rmt-flute`,
//! `cc-data` fixed bits, `rtp-packet`'s `rtcp` `profile_extensions`, `isobmff`
//! flags). There is no raw passthrough blob.
//! - **Constructed values default to the edition value**: a struct built by
//! hand (`new(..)` / `Default`) carries `1` for `reserved_future_use` and
//! H.222.0 "reserved", `0` for `reserved_zero_future_use`, with any explicit
//! per-field exception documented on the field. (A crate that has not
//! adopted preserve-on-parse documents which fields it normalizes to the
//! spec value on serialize; `mpeg-ts`, `mpeg-pes` and `mpeg-ps` preserve.)
//! - A caller that needs byte-for-byte preservation of reserved bits in a
//! not-yet-migrated crate **keeps the original bytes** rather than
//! re-serializing.
//! - Trailing bytes inside a descriptor or section body are never silently
//! dropped without a documented decision: hold them in a typed field, or
//! document the field as "ignored".
//! - Reserved bytes are always **written explicitly** by `serialize_into`;
//! never rely on a pre-zeroed output buffer.
//!
//! The round-trip gate is byte-exact against the original fixture slice
//! (see `docs/CRATE-ACCEPTANCE.md`).
;
extern crate alloc;
pub use bcp47_language;