cc_data/lib.rs
1//! Closed-caption `cc_data()` carriage (CEA-608/708), per ETSI TS 101 154 §B.5, Table B.9.
2//!
3//! Parses the closed-caption carriage structure carried in MPEG-2 / AVC / HEVC
4//! picture `user_data` (the DVB-native, normative form of the ATSC/CEA `cc_data()`).
5//! Exposes the typed caption triplets (`cc_valid`, `cc_type`, `cc_data_1/2`) and a
6//! CEA-608 vs CEA-708 split by `cc_type`.
7//!
8//! Feed it the `cc_data()` bytes (the caller extracts them from the picture
9//! user_data / SEI). Depends only on `broadcast-common`, `#![no_std]` (+ `alloc`).
10//!
11//! # Caption decode (`decode` feature)
12//!
13//! With the default `decode` feature, this crate also *interprets* the demuxed
14//! caption byte pairs — the [`decode`] module's [`Cea608Decoder`](decode::Cea608Decoder)
15//! (line-21 pop-on / roll-up / paint-on, PACs, mid-row codes, the standard /
16//! special / extended character sets, channels CC1–CC4) and
17//! [`Cea708Decoder`](decode::Cea708Decoder) (DTVCC packet / service-block
18//! reassembly + the C0/C1/G0/G1/G2/G3 window / pen command interpreter, services
19//! 1–6) — and exposes the decoded caption screen / window text. Grounded in
20//! ANSI/CTA-608-E, ANSI/CTA-708-E and 47 CFR §79.102 (see `cc-data/docs/decode/`).
21//!
22//! Caption-detection helpers (issue #1428): `Cea608Decoder::has_caption_text`,
23//! `Cea708Decoder::services_seen` and `Cea608Channel::number` (1 to 4).
24//!
25//! # What it does / what it does not
26//!
27//! Parses and builds `cc_data()` byte-exactly, splits CEA-608 (`cc_type` 0/1)
28//! from CEA-708 (`cc_type` 2/3), and (with `decode`) interprets the pairs into
29//! caption text. It does **not** locate `cc_data()` in picture `user_data` / SEI,
30//! render pixels, or decode the XDS metadata catalogue (field-2 XDS is detected
31//! and skipped). Conversion to WebVTT / SRT is in `timed-metadata` and
32//! `caption-convert`.
33//!
34//! # Standards supported
35//!
36//! | Standard | Edition | Scope | Status |
37//! |---|---|---|---|
38//! | ETSI TS 101 154 | V2.6.1 (transcribed in `docs/ts_101_154/`) | §B.5 / Table B.9 `cc_data()` | Full |
39//! | ANSI/CTA-608-E | -E; CTA-608-E is not vendored (no copy in `private/specs` or `specs/`), so byte tables cannot be re-verified against it | line-21 decode, CC1-CC4, PACs, character sets | Partial (decode); XDS skipped |
40//! | ANSI/CTA-708-E | -E; CTA-708-E is not vendored; cross-source: GY/T 270-2013 (vendored, a Chinese adaptation; differences from CTA must be checked) | DTVCC packets, service blocks, windows, pens; services 1-6 | Partial (decode) |
41//! | 47 CFR §79.102 | public domain (`docs/decode/cea708-conformance.md`) | 708 semantic model | Full as a grounding source |
42//!
43//! # Feature flags
44//!
45//! | Feature | Default | Effect |
46//! |---|---|---|
47//! | `std` | on | link `std`; off = `no_std` + `alloc` |
48//! | `decode` | on | [`decode`](crate::decode) module: 608 / 708 decoders |
49//! | `serde` | off | `serde::Serialize` on public types |
50//!
51//! # Quick start
52//!
53//! ```
54//! use broadcast_common::{Parse, Serialize};
55//! use cc_data::{CcData, CcTriplet, CcType};
56//!
57//! // Build a cc_data() with one 608 field-1 pair and one 708 start triplet.
58//! let cc = CcData {
59//! process_em_data_flag: true,
60//! process_cc_data_flag: true,
61//! additional_data_flag: false,
62//! reserved_byte1: 0xFF,
63//! triplets: vec![
64//! CcTriplet {
65//! cc_valid: true,
66//! cc_type: CcType::Dtvcc708Start,
67//! cc_data_1: 0xC1,
68//! cc_data_2: 0x02,
69//! },
70//! CcTriplet {
71//! cc_valid: true,
72//! cc_type: CcType::Ntsc608Field1,
73//! cc_data_1: 0x94,
74//! cc_data_2: 0x2C,
75//! },
76//! ],
77//! };
78//!
79//! // Serialize, parse back byte-exactly, and split 608 from 708.
80//! let bytes = cc.try_to_bytes().unwrap();
81//! let back = CcData::parse(&bytes).unwrap();
82//! assert_eq!(back, cc);
83//! assert_eq!(back.cea608().count(), 1);
84//! assert_eq!(back.cea708().count(), 1);
85//! ```
86//!
87//! Decoding (feature `decode`):
88//!
89//! ```
90//! # #[cfg(feature = "decode")]
91//! # {
92//! use cc_data::decode::{Cea608Channel, Cea608Decoder};
93//!
94//! let mut dec = Cea608Decoder::new();
95//! // RCL, PAC row 15, "HI", EOC: a pop-on caption on CC1 (field 1).
96//! dec.push_pair(false, 0x14, 0x20); // RCL (field 1 -> CC1)
97//! dec.push_pair(false, 0x14, 0x20); // doubled control, ignored
98//! dec.push_pair(false, 0x14, 0x70); // PAC row 15 indent 0
99//! dec.push_pair(false, b'H', b'I');
100//! dec.push_pair(false, 0x14, 0x2F); // EOC -> flip
101//! assert_eq!(dec.channel_text(Cea608Channel::Cc1), "HI");
102//! # }
103//! ```
104//!
105//! # Design notes and related crates
106//!
107//! Symmetric `Parse` / `Serialize` with a byte-identical carriage round trip
108//! (flag bits and `reserved_byte1` preserved); [`Error`] is `#[non_exhaustive]`;
109//! both decoders are one-way and panic-free on hostile input. Depends only on
110//! `broadcast-common`; used by `timed-metadata` and `caption-convert`. MSRV
111//! 1.95.0; MIT OR Apache-2.0.
112#![no_std]
113#![cfg_attr(docsrs, feature(doc_cfg))]
114#![warn(missing_docs)]
115// Runnable examples, embedded so they render on docs.rs and stay in sync with
116// the actual `examples/*.rs` files (shown, not compiled).
117#![doc = "\n# Examples\n"]
118#![doc = "Runnable examples ship with this crate (`cargo run -p cc-data --example <name>`).\n"]
119#![doc = "\n## `parse_cc_data`\n\n```rust,ignore"]
120#![doc = include_str!("../examples/parse_cc_data.rs")]
121#![doc = "```\n\n## `build_cc_data`\n\n```rust,ignore"]
122#![doc = include_str!("../examples/build_cc_data.rs")]
123#![cfg_attr(
124 feature = "decode",
125 doc = "```\n\n## `decode_cea608`\n\n```rust,ignore"
126)]
127#![cfg_attr(feature = "decode", doc = include_str!("../examples/decode_cea608.rs"))]
128#![cfg_attr(
129 feature = "decode",
130 doc = "```\n\n## `decode_cea708`\n\n```rust,ignore"
131)]
132#![cfg_attr(feature = "decode", doc = include_str!("../examples/decode_cea708.rs"))]
133#![doc = "```"]
134
135extern crate alloc;
136
137mod cc_data;
138#[cfg(feature = "decode")]
139#[cfg_attr(docsrs, doc(cfg(feature = "decode")))]
140pub mod decode;
141mod error;
142
143pub use cc_data::{CcData, CcTriplet, CcType};
144pub use error::{Error, Result};