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//! # What it does / what it does not
23//!
24//! Parses and builds `cc_data()` byte-exactly, splits CEA-608 (`cc_type` 0/1)
25//! from CEA-708 (`cc_type` 2/3), and (with `decode`) interprets the pairs into
26//! caption text. It does **not** locate `cc_data()` in picture `user_data` / SEI,
27//! render pixels, or decode the XDS metadata catalogue (field-2 XDS is detected
28//! and skipped). Conversion to WebVTT / SRT is in `timed-metadata` and
29//! `caption-convert`.
30//!
31//! # Standards supported
32//!
33//! | Standard | Edition | Scope | Status |
34//! |---|---|---|---|
35//! | ETSI TS 101 154 | V2.6.1 (transcribed in `docs/ts_101_154/`) | §B.5 / Table B.9 `cc_data()` | Full |
36//! | 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 |
37//! | 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) |
38//! | 47 CFR §79.102 | public domain (`docs/decode/cea708-conformance.md`) | 708 semantic model | Full as a grounding source |
39//!
40//! # Feature flags
41//!
42//! | Feature | Default | Effect |
43//! |---|---|---|
44//! | `std` | on | link `std`; off = `no_std` + `alloc` |
45//! | `decode` | on | [`decode`](crate::decode) module: 608 / 708 decoders |
46//! | `serde` | off | `serde::Serialize` on public types |
47//!
48//! # Quick start
49//!
50//! ```
51//! use broadcast_common::{Parse, Serialize};
52//! use cc_data::{CcData, CcTriplet, CcType};
53//!
54//! // Build a cc_data() with one 608 field-1 pair and one 708 start triplet.
55//! let cc = CcData {
56//! process_em_data_flag: true,
57//! process_cc_data_flag: true,
58//! additional_data_flag: false,
59//! reserved_byte1: 0xFF,
60//! triplets: vec![
61//! CcTriplet {
62//! cc_valid: true,
63//! cc_type: CcType::Dtvcc708Start,
64//! cc_data_1: 0xC1,
65//! cc_data_2: 0x02,
66//! },
67//! CcTriplet {
68//! cc_valid: true,
69//! cc_type: CcType::Ntsc608Field1,
70//! cc_data_1: 0x94,
71//! cc_data_2: 0x2C,
72//! },
73//! ],
74//! };
75//!
76//! // Serialize, parse back byte-exactly, and split 608 from 708.
77//! let bytes = cc.try_to_bytes().unwrap();
78//! let back = CcData::parse(&bytes).unwrap();
79//! assert_eq!(back, cc);
80//! assert_eq!(back.cea608().count(), 1);
81//! assert_eq!(back.cea708().count(), 1);
82//! ```
83//!
84//! Decoding (feature `decode`):
85//!
86//! ```
87//! # #[cfg(feature = "decode")]
88//! # {
89//! use cc_data::decode::{Cea608Channel, Cea608Decoder};
90//!
91//! let mut dec = Cea608Decoder::new();
92//! // RCL, PAC row 15, "HI", EOC: a pop-on caption on CC1 (field 1).
93//! dec.push_pair(false, 0x14, 0x20); // RCL (field 1 -> CC1)
94//! dec.push_pair(false, 0x14, 0x20); // doubled control, ignored
95//! dec.push_pair(false, 0x14, 0x70); // PAC row 15 indent 0
96//! dec.push_pair(false, b'H', b'I');
97//! dec.push_pair(false, 0x14, 0x2F); // EOC -> flip
98//! assert_eq!(dec.channel_text(Cea608Channel::Cc1), "HI");
99//! # }
100//! ```
101//!
102//! # Design notes and related crates
103//!
104//! Symmetric `Parse` / `Serialize` with a byte-identical carriage round trip
105//! (flag bits and `reserved_byte1` preserved); [`Error`] is `#[non_exhaustive]`;
106//! both decoders are one-way and panic-free on hostile input. Depends only on
107//! `broadcast-common`; used by `timed-metadata` and `caption-convert`. MSRV
108//! 1.95.0; MIT OR Apache-2.0.
109#![no_std]
110#![cfg_attr(docsrs, feature(doc_cfg))]
111#![warn(missing_docs)]
112// Runnable examples, embedded so they render on docs.rs and stay in sync with
113// the actual `examples/*.rs` files (shown, not compiled).
114#![doc = "\n# Examples\n"]
115#![doc = "Runnable examples ship with this crate (`cargo run -p cc-data --example <name>`).\n"]
116#![doc = "\n## `parse_cc_data`\n\n```rust,ignore"]
117#![doc = include_str!("../examples/parse_cc_data.rs")]
118#![doc = "```\n\n## `build_cc_data`\n\n```rust,ignore"]
119#![doc = include_str!("../examples/build_cc_data.rs")]
120#![cfg_attr(
121 feature = "decode",
122 doc = "```\n\n## `decode_cea608`\n\n```rust,ignore"
123)]
124#![cfg_attr(feature = "decode", doc = include_str!("../examples/decode_cea608.rs"))]
125#![cfg_attr(
126 feature = "decode",
127 doc = "```\n\n## `decode_cea708`\n\n```rust,ignore"
128)]
129#![cfg_attr(feature = "decode", doc = include_str!("../examples/decode_cea708.rs"))]
130#![doc = "```"]
131
132extern crate alloc;
133
134mod cc_data;
135#[cfg(feature = "decode")]
136#[cfg_attr(docsrs, doc(cfg(feature = "decode")))]
137pub mod decode;
138mod error;
139
140pub use cc_data::{CcData, CcTriplet, CcType};
141pub use error::{Error, Result};