Skip to content

Render in-band CEA-608 closed captions (#77) - #81

Merged
superuser404notfound merged 1 commit into
mainfrom
unknown repository
Jun 27, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jun 26, 2026

Copy link
Copy Markdown

Summary

A source whose only caption track is an embedded CEA-608 stream (eia_608, e.g. a QuickTime/MP4 c608 track) currently can't render. FFmpegBuild ships no ccaption decoder, so the side-demuxer EmbeddedSubtitleDecoder open fails and the track sits active-but-blank (subActive=true / subCues=0) — selectable but producing no cues for the whole title.

This PR decodes those captions in-house and renders them through the existing host-overlay subtitle path, without standing up a second demuxer, a second connection, or touching any existing pipeline. The caption bytes are read off the segment producer's source connection that the engine already holds.

Closes the rendering half of #77. First cut: 608 field-1 / channel CC1; CEA-708 (DTVCC) and field 2 are explicit follow-ons.

What it does

  • A track exposed as eia_608 now renders its captions on the host overlay (subtitleCues), exactly like every other side-decoded subtitle codec (SRT/ASS/PGS/DVB).
  • Works on local, remote (progressive MP4 over HTTP), and Jellyfin direct-stream sources.
  • Captions appear instantly when the track is selected (no side-demuxer open/seek/prewarm), and survive seek / reload / producer re-anchor (wedge).

Architecture

Three pieces, all additive; the caption bytes ride the connection the producer already reads.

  1. CCDataParser (new) — parses the bare cc_data triplet stream FFmpeg's MOV demuxer emits for a c608 track (it strips the cdat/cdt2 atom wrapper and hands downstream (cc_valid|cc_type, b0, b1) triplets). Pure byte parsing.
  2. CEA608Decoder (new) — an in-house line-21 state machine: pop-on / roll-up / paint-on, PAC row addressing, mid-row codes, and the basic / special / extended West-European character sets. Odd-parity validation, doubled-control suppression, the character tables and PAC bounds are all validated against FFmpeg's ccaption_dec.c (the decoder mpv/VLC mirror) and covered by unit tests.
  3. ClosedCaptionTap (new) — a read-only observer attached to the producer's source demuxer. The producer keeps the eia_608 stream in its keep-set, hands each of its packets to the tap, then drops it (never muxed). The tap decodes on the pump thread, owns the cue buffer, and publishes immutable snapshots to the MainActor, which mirrors them into subtitleCues only while the CC track is the active subtitle.

Because the tap owns the buffer and makeProducer re-threads the observer onto every producer (initial + restart), enabling CC mirrors the buffer instantly and seeks/reloads/wedges re-feed it automatically — no second connection, no per-seek re-arm. Snapshot delivery is sequence-guarded against out-of-order MainActor hops; the tap drops decoder + buffer state on an explicit seek (requestReset() from seek()).

Minimal interference — existing pipelines intact

  • Byte-identical output. The CC packet is observed then dropped in the pump's foreign-packet branch; it is never muxed. With no CC track, closedCaptionStreamIndex == -1 and every site is a no-op — the loopback-HLS segments are bit-for-bit unchanged.
  • Native subtitle rendition (AVMediaSelection / WebVTT) so subtitles survive PiP, AirPlay & external display #55 native mov_text rendition untouched. CC is excluded from the textTracks filter (a caption track can't become tx3g) and rendered on the overlay like the bitmap subtitle codecs. Consistent limitation: host-overlay only, so no PiP/AirPlay CC (same as PGS/DVB today).
  • All CC logic is externalised in the three new files. Edits to existing files are small, additive, and observational:
    • Demuxer.trackInfo — fall back to avcodec_get_name when no decoder exists, so the track is identifiable (eia_608) instead of "unknown". Cannot affect any track that has a decoder.
    • AetherEngine+Probe — add an isEmbeddedClosedCaptionCodec classifier beside isBitmapSubtitleCodec.
    • AetherEngine+Loading — exclude CC from the mov_text filter; arm the tap before start().
    • AetherEngine+Subtitles / AetherEngine — route CC selection to mirror the tap's snapshot; reset the tap on seek.
    • HLSVideoEngine — thread the CC stream index + observer onto every producer (same pattern as the Native subtitle rendition (AVMediaSelection / WebVTT) so subtitles survive PiP, AirPlay & external display #55 subtitleCueStores threading).
    • HLSSegmentProducer — one keep-set entry + one read-only observer call.

Scope / limitations (stated)

  • 608 field-1 / CC1 only; 708 (DTVCC packet reassembly) and field 2 are follow-ons.
  • Host-overlay rendering only (no native tx3g, so no PiP/AirPlay CC) — same as the existing bitmap subtitle path.
  • Handles the demuxable caption track carriage (eia_608 / c608). Captions carried only in the video bitstream SEI (no separate track) are out of scope here.

Extending this system (future work)

The architecture splits cleanly into producer → parser → decoder → subtitleCues overlay, and the overlay is the universal sink: anything that can emit timed text cues plugs in the same way. That makes several follow-ons cheap, in roughly increasing cost:

  • CEA-708 (DTVCC) and 608 field 2 / channels CC2–CC4 — same tap, no new I/O. The tap already receives all cc_data triplets from the caption stream and currently filters to field-1 (cc_type == 0); cc_type 2/3 (708) and cc_type 1 (field 2) flow through the exact same observer. Adding them is purely a decode-side change: reassemble the DTVCC packets (708) or extend the existing 608 channel handling (field 2 / CC2–CC4) and publish to the same buffer. No second connection, no producer changes.
  • In-band text streams with no FFmpeg decoder — reuse the tap pattern. Any demuxable caption/data stream the side-demuxer path can't open (a vendor caption codec, a teletext page, an ID3/timed_id3 text feed) can keep its stream in the producer keep-set, hand its packets to a small purpose-built parser, and publish cues — identical shape to ClosedCaptionTap, byte-identical output preserved.
  • Burned-in / "baked-in" / telesync hardsubs — same sink, different source. These live in the video pixels, not a data stream, so the packet-tap doesn't apply; they need a frame consumer + OCR (Vision on Apple platforms) running off the engine's existing software-decode / frame-extractor path, emitting recognized text as cues into subtitleCues. Heavier (it requires pixel decode), and best gated to on-demand, but it lands on the very same overlay — so the host UI, styling, positioning, and selection are unchanged.
  • Generated / external cue sources. Translation, auto-captioning, or a fetched sidecar (SRT/VTT/ASS) are all just cue producers: parse/produce timed text off-thread and publish snapshots, exactly as the CC tap does.

In short: subtitleCues is the extension point. For in-band data, copy the read-only producer-tap; for pixel-baked text, add a frame-OCR producer; either way the rendering, host overlay, seek/reload survival, and instant-enable behavior come for free.

Files

New: Decoder/CCDataParser.swift, Decoder/CEA608Decoder.swift, AetherEngine+ClosedCaptions.swift, plus Tests/AetherEngineTests/CCDataParserTests.swift and CEA608DecoderTests.swift.
Edited: Demuxer/Demuxer.swift, AetherEngine+Probe.swift, AetherEngine+Loading.swift, AetherEngine+Subtitles.swift, AetherEngine.swift, Video/HLSVideoEngine.swift, Video/HLSSegmentProducer.swift, CHANGELOG.md.

Test plan

  • Unit: 11 new tests — CCDataParser triplet parsing (cc_valid/cc_type, bounds) and CEA608Decoder (pop-on display/erase, roll-up on CR, doubled- and tripled-control suppression, odd-parity rejection, extended-glyph correctness, reset()). Full AetherEngineTests suite passes (221 tests, 0 failures) on macOS.
  • Manual (macOS): H.264 1080p + AAC + eia_608 MP4. Verified: captions render and stay in sync from cold start and on resume; survive short and long seeks; survive Jellyfin direct-stream reload; no A/V or audio regression. Selecting the track shows captions immediately.

Acknowledgements

Externalise-subtitles steer by DrHurt.


🤖 Generated with Claude Code

Decode embedded eia_608 / c608 caption tracks (which have no FFmpeg
ccaption decoder and previously sat active-but-blank) via a read-only
observer on the segment producer's source connection, an in-house
CEA-608 decoder, and the existing host-overlay subtitleCues path. No
second demuxer/connection; segment output byte-identical; #55 native
mov_text rendition untouched. First cut: 608 field-1 / CC1.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ghost

ghost commented Jun 26, 2026

Copy link
Copy Markdown
Author
Screen.Recording.2026-06-26.at.4.08.20.PM.mov

@superuser404notfound

Copy link
Copy Markdown
Owner

Merged and shipped in 4.6.0. Thanks @reckloon, this is a clean, well-tested implementation that lands exactly the externalised approach from the discussion: the read-only producer tap keeps the loopback segment output byte-identical, and the in-house CEA-608 decoder matches FFmpeg's ccaption_dec.c. I verified the parity handling, the doubled / tripled-control suppression (this FFmpeg revision does not self-clear prev_cmd in the dedup branch, so the decoder matches it exactly), and the full basic / special / extended character and PAC tables against the reference locally. Build is clean and all 226 tests pass on the merge.

Follow-ons noted for later: CEA-708 (DTVCC), 608 field 2 / channels CC2 to CC4, and the SEI-only carriage. Thanks also @DrHurt for the externalise-subtitles steer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants