forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

lib.rs63 lines, 2.5 KiBHistoryRawDownload
1//! Closed captions: carriage extraction and CEA-608 / CEA-708 decoding.
2//!
3//! * [`ccdata`]: the `cc_data` triplet type and the wrappers it travels in (ATSC A/53 `GA94`,
4//! the DVD `CC` variant, CDP packets, QuickTime `c608`/`c708` samples).
5//! * [`extract`]: side-channel extractors for H.264, HEVC and MPEG-2 elementary streams. They never
6//! touch the decoders: a light parse of the SEI / user data plus just enough of the slice header
7//! (H.264/HEVC) or picture header (MPEG-2) to restore presentation order.
8//! * [`cea608`]: the field 1 / field 2 line-21 decoder (CC1 to CC4) with pop-on, roll-up and
9//! paint-on modes.
10//! * [`cea708`]: DTVCC service 1 (windows plus text commands); other services are refused by name.
11//! * [`synth`]: cc_data generator and SEI / user-data writers used by tests and tools.
12
13#![forbid(unsafe_code)]
14
15pub mod ccdata;
16pub mod cea608;
17pub mod cea708;
18pub mod extract;
19pub mod synth;
20
21pub use ccdata::CcTriplet;
22pub use cea608::{CaptionCue, CaptionLine, Cea608Decoder};
23pub use cea708::Cea708Decoder;
24pub use extract::{CcStream, H264Extractor, HevcExtractor, Mpeg2Extractor, TimedCc};
25
26/// Decode a presentation-ordered triplet list into 608 cues and 708 service 1 cues.
27///
28/// Convenience for callers that just want captions: field 1/2 pairs (`cc_type` 0 and 1) go to the
29/// 608 decoder, DTVCC bytes (types 2 and 3) to the 708 decoder.
30pub fn decode_all(timed: &[TimedCc]) -> DecodedCaptions {
31 let mut d608 = Cea608Decoder::new();
32 let mut d708 = Cea708Decoder::new();
33 let mut end = 0;
34 for t in timed {
35 end = t.time_us;
36 for tr in &t.triplets {
37 if !tr.valid {
38 continue;
39 }
40 match tr.cc_type {
41 0 | 1 => d608.push_pair(t.time_us, tr.cc_type, tr.data[0], tr.data[1]),
42 2 | 3 => d708.push_triplet(t.time_us, tr.cc_type, tr.data[0], tr.data[1]),
43 _ => {}
44 }
45 }
46 }
47 DecodedCaptions {
48 cea608: d608.finish(end),
49 cea708: d708.finish(end),
50 unsupported_708_services: d708.unsupported_services(),
51 }
52}
53
54/// Result of [`decode_all`].
55#[derive(Debug, Clone, Default)]
56pub struct DecodedCaptions {
57 /// CEA-608 cues, all four channels, ordered by start time.
58 pub cea608: Vec<CaptionCue>,
59 /// CEA-708 service 1 cues (channel field is 1).
60 pub cea708: Vec<CaptionCue>,
61 /// DTVCC service numbers seen but not decoded (everything except service 1).
62 pub unsupported_708_services: Vec<u8>,
63}