# lane/cea608: closed caption extraction and decoding New crate `crates/ec-cc` (depends on ec-core, ec-subs, ec-h264-syntax, ec-h265-syntax). No change to ec-h264, ec-h265, ec-mpeg2 or any decoder, so decoded pictures are untouched; extraction is a side channel over the elementary stream. No CodecId was added (nothing needed one). ## What is there - `ccdata`: `CcTriplet {valid, cc_type, data}`; parsers for ATSC A/53 `GA94` (SEI T.35 provider 0x31, DIRECTV 0x2F), MPEG-2 user data (`GA94` and DVD `CC 01 F8`), SMPTE 334 CDP, QuickTime `c608` (`cdat`/`cdt2`) and `c708` (`ccdp`) samples, raw pair tracks. - `extract`: `H264Extractor`, `HevcExtractor`, `Mpeg2Extractor`. `push(bytes, pts_us)` takes a whole Annex B stream or one packet (length-prefixed NALs with `set_extradata(avcC/hvcC)`). Every picture is recorded; `finish((fps_num, fps_den))` returns `Vec` in presentation order. Order comes from container pts when every picture has one, else from POC (H.264 type 0 / HEVC, epoch bumped at IDR/BLA, MSB tracking) or MPEG-2 temporal_reference per GOP. Field pairs of one frame (H.264 field pictures, MPEG-2 field pictures) fold into one entry. H.264 POC types 1/2 fall back to decode order. - `cea608::Cea608Decoder`: CC1/CC2 (field 1), CC3/CC4 (field 2). Pop-on, roll-up 2-4, paint-on, PACs (row, indent, colour, italics, underline), mid-row codes, special and both extended sets, tab offsets, BS, DER, EDM, ENM, EOC, doubled-control suppression, text mode ignored. Output `CaptionCue {channel, cue: ec_subs::SubtitleCue, lines: [row, col, text]}`; the cue has Italic/Underline/Color segments, LineBreak between rows and `positioning` x/y in percent of the 32x15 grid. A cue is one stable display state (new cue at EOC, EDM, CR, mode switch). - `cea708::Cea708Decoder`: DTVCC packet assembly, service 1: DF0-7, CW, CLW, DSW, HDW, TGW, DLW, RST, SPA, SPC, SPL, C0 text controls, G0/G1/G2/G3 text. Other services are refused by name: `unsupported_services()` lists them and 608 (cc_type 0/1) remains the fallback. Window pen colours are quantised to the 8 colour 608 palette; window fill/edge/justify/scroll/effects are ignored. - `decode_all(&[TimedCc]) -> DecodedCaptions {cea608, cea708, unsupported_708_services}`. - `synth`: 608 script builder, `cc_data` writer, `insert_sei` (H.264/HEVC), `insert_mpeg2_user_data` (ATSC and DVD forms). ## Verification (server, `EC_REQUIRE_FIXTURES=1 cargo test -p ec-cc`, clippy workspace -D warnings clean) - 9 unit-level tests of the 608/708 decoders and wrappers (pop-on, two-row, styles/special/extended, roll-up, paint-on, CC2, CC3, text mode, CDP/c608/c708/DVD parse, 708 window). - 4 stream tests: ffmpeg encodes 90 frames with B-frames (libx264, libx265, mpeg2video), captions are spliced in with `synth` in decode order, the extractors must return exactly the original per-frame triplets in display order at exact frame times, and the decoded text must equal ffmpeg's own `movie=...[out0+subcc]` extraction (text and start time within 0.1 s) after remux to mp4. The ffmpeg cross-check is skipped silently if ffmpeg's subcc path fails (older ffmpeg); the extraction asserts are always strict. Without ffmpeg/ffprobe the stream tests skip unless EC_REQUIRE_FIXTURES=1, which makes them fail. - Not verified against real broadcast material: DVD `CC` form (own writer only, layout from memory of ccextractor/ffmpeg), CDP and real `c608` mov tracks (hand-built samples), 708 beyond a single window. ccextractor was not available. - Known simplifications: 608 time is the picture time, not spread across multiple pairs per picture; paint-on cues commit at CR/EDM/mode change/end rather than per word; no flash, background attributes, or 608 text mode output. ## What edith needs to import captions 1. For H.264/HEVC/MPEG-2 video tracks: feed each demuxed packet to the matching extractor (`push(packet.data, Some(pts_us))`, call `set_extradata` with avcC/hvcC for mp4/mkv first). Calling it alongside normal decoding costs a header parse per packet. 2. For mov `c608`/`c708` tracks (ec-mp4 exposes the sample entry fourcc): per sample `ccdata::parse_c608_sample` / `parse_c708_sample`, push the triplets into a `CcStream`-like list, or directly `Cea608Decoder::push_pair(pts_us, cc_type, b1, b2)`. 3. `finish(fps)` then `decode_all` yields cues. Map `CaptionCue.cue` to the caption track IR (ec-subs `SubtitleCue`); `channel` tells CC1-CC4 for 608 and 1 for 708 service 1. Offer the channel(s) that have cues (usually CC1). 4. Add `unsupported_708_services` to the import log so an editor sees why 708 services 2+ are missing.