1use crate::cc_data::{CcTriplet, CcType};
20use alloc::string::String;
21use alloc::vec::Vec;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize))]
29#[non_exhaustive]
30pub enum Cea608Color {
31 #[default]
33 White,
34 Green,
36 Blue,
38 Cyan,
40 Red,
42 Yellow,
44 Magenta,
46}
47
48impl Cea608Color {
49 #[must_use]
51 pub fn name(&self) -> &'static str {
52 match self {
53 Self::White => "white",
54 Self::Green => "green",
55 Self::Blue => "blue",
56 Self::Cyan => "cyan",
57 Self::Red => "red",
58 Self::Yellow => "yellow",
59 Self::Magenta => "magenta",
60 }
61 }
62
63 #[must_use]
65 pub(super) fn from_idx(idx: u8) -> Self {
66 match idx & 0x07 {
67 0 => Self::White,
68 1 => Self::Green,
69 2 => Self::Blue,
70 3 => Self::Cyan,
71 4 => Self::Red,
72 5 => Self::Yellow,
73 6 => Self::Magenta,
74 _ => Self::White,
75 }
76 }
77}
78broadcast_common::impl_spec_display!(Cea608Color);
79
80const SCREEN_ROWS: usize = 15;
82const SCREEN_COLS: usize = 32;
84
85const MC_RCL: u8 = 0x20;
87const MC_BS: u8 = 0x21;
88const MC_DER: u8 = 0x24;
89const MC_RU2: u8 = 0x25;
90const MC_RU3: u8 = 0x26;
91const MC_RU4: u8 = 0x27;
92const MC_FON: u8 = 0x28;
93const MC_RDC: u8 = 0x29;
94const MC_TR: u8 = 0x2A;
95const MC_RTD: u8 = 0x2B;
96const MC_EDM: u8 = 0x2C;
97const MC_CR: u8 = 0x2D;
98const MC_ENM: u8 = 0x2E;
99const MC_EOC: u8 = 0x2F;
100
101const TAB_FIRST_C1: u8 = 0x17;
103const TAB_FIRST_C2: u8 = 0x1F;
104const TAB1: u8 = 0x21;
105const TAB3: u8 = 0x23;
106
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
109#[cfg_attr(feature = "serde", derive(serde::Serialize))]
110#[non_exhaustive]
111pub enum Cea608Mode {
112 #[default]
114 None,
115 PopOn,
117 RollUp(u8),
119 PaintOn,
121 Text,
123}
124
125impl Cea608Mode {
126 #[must_use]
128 pub fn name(&self) -> &'static str {
129 match self {
130 Self::None => "none",
131 Self::PopOn => "pop_on",
132 Self::RollUp(_) => "roll_up",
133 Self::PaintOn => "paint_on",
134 Self::Text => "text",
135 }
136 }
137}
138broadcast_common::impl_spec_display!(Cea608Mode);
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
143#[cfg_attr(feature = "serde", derive(serde::Serialize))]
144#[non_exhaustive]
145pub enum Cea608Channel {
146 Cc1,
148 Cc2,
150 Cc3,
152 Cc4,
154}
155
156impl Cea608Channel {
157 #[must_use]
159 pub fn number(self) -> u8 {
160 match self {
161 Self::Cc1 => 1,
162 Self::Cc2 => 2,
163 Self::Cc3 => 3,
164 Self::Cc4 => 4,
165 }
166 }
167
168 #[must_use]
170 fn index(self) -> usize {
171 match self {
172 Self::Cc1 => 0,
173 Self::Cc2 => 1,
174 Self::Cc3 => 2,
175 Self::Cc4 => 3,
176 }
177 }
178 #[must_use]
180 pub fn name(&self) -> &'static str {
181 match self {
182 Self::Cc1 => "cc1",
183 Self::Cc2 => "cc2",
184 Self::Cc3 => "cc3",
185 Self::Cc4 => "cc4",
186 }
187 }
188}
189broadcast_common::impl_spec_display!(Cea608Channel);
190
191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193#[cfg_attr(feature = "serde", derive(serde::Serialize))]
194pub struct Cea608StyledChar {
195 pub ch: char,
197 pub underline: bool,
199 pub italics: bool,
201 pub color: Cea608Color,
203}
204
205impl Default for Cea608StyledChar {
206 fn default() -> Self {
207 Cea608StyledChar {
208 ch: ' ',
209 underline: false,
210 italics: false,
211 color: Cea608Color::White,
212 }
213 }
214}
215
216#[derive(Debug, Clone, PartialEq, Eq, Default)]
218#[cfg_attr(feature = "serde", derive(serde::Serialize))]
219pub struct Cea608Row {
220 cells: Vec<(usize, Cea608StyledChar)>,
222}
223
224impl Cea608Row {
225 fn set(&mut self, col: usize, c: Cea608StyledChar) {
226 if let Some(slot) = self.cells.iter_mut().find(|(i, _)| *i == col) {
227 slot.1 = c;
228 } else {
229 self.cells.push((col, c));
230 }
231 }
232 fn clear_from(&mut self, col: usize) {
233 self.cells.retain(|(i, _)| *i < col);
234 }
235 fn remove(&mut self, col: usize) {
236 self.cells.retain(|(i, _)| *i != col);
237 }
238 #[must_use]
240 pub fn text(&self) -> String {
241 let mut sorted = self.cells.clone();
242 sorted.sort_by_key(|(i, _)| *i);
243 let mut out = String::new();
244 let mut last = None;
245 for (i, c) in sorted {
246 if let Some(prev) = last {
247 for _ in (prev + 1)..i {
248 out.push(' ');
249 }
250 }
251 out.push(c.ch);
252 last = Some(i);
253 }
254 out
255 }
256 #[must_use]
258 pub fn styled_cells(&self) -> Vec<(usize, Cea608StyledChar)> {
259 let mut sorted = self.cells.clone();
260 sorted.sort_by_key(|(i, _)| *i);
261 sorted
262 }
263}
264
265#[derive(Debug, Clone, PartialEq, Eq, Default)]
267#[cfg_attr(feature = "serde", derive(serde::Serialize))]
268pub struct Cea608Screen {
269 rows: [Cea608Row; SCREEN_ROWS],
270}
271
272impl Cea608Screen {
273 #[must_use]
275 pub fn text(&self) -> String {
276 let mut out = String::new();
277 for row in &self.rows {
278 let line = row.text();
279 let trimmed = line.trim_end();
280 if trimmed.is_empty() {
281 continue;
282 }
283 if !out.is_empty() {
284 out.push('\n');
285 }
286 out.push_str(trimmed);
287 }
288 out
289 }
290 #[must_use]
292 pub fn rows(&self) -> &[Cea608Row; SCREEN_ROWS] {
293 &self.rows
294 }
295}
296
297#[derive(Debug, Clone, Copy, PartialEq, Eq)]
299struct Pen {
300 underline: bool,
301 italics: bool,
302 color: Cea608Color,
303}
304
305impl Default for Pen {
306 fn default() -> Self {
307 Pen {
308 underline: false,
309 italics: false,
310 color: Cea608Color::White,
311 }
312 }
313}
314
315#[derive(Debug, Clone, PartialEq, Eq, Default)]
317struct ChannelState {
318 displayed: Cea608Screen,
319 nondisplayed: Cea608Screen,
320 mode: Cea608Mode,
321 rollup_rows: u8,
322 cursor_row: usize,
323 cursor_col: usize,
324 pen: Pen,
325}
326
327impl ChannelState {
328 fn active_mut(&mut self) -> &mut Cea608Screen {
331 match self.mode {
332 Cea608Mode::PopOn => &mut self.nondisplayed,
333 _ => &mut self.displayed,
334 }
335 }
336}
337
338#[derive(Debug, Clone, PartialEq, Eq)]
356pub struct Cea608Decoder {
357 channels: [ChannelState; 4],
358 last_control: [Option<(u8, u8)>; 2],
360 last_channel: [Cea608Channel; 2],
365 xds_active: bool,
366 caption_text_seen: u8,
369}
370
371impl Default for Cea608Decoder {
372 fn default() -> Self {
373 Self::new()
374 }
375}
376
377impl Cea608Decoder {
378 #[must_use]
380 pub fn new() -> Self {
381 Cea608Decoder {
382 channels: Default::default(),
383 last_control: [None, None],
384 last_channel: [Cea608Channel::Cc1, Cea608Channel::Cc3],
385 xds_active: false,
386 caption_text_seen: 0,
387 }
388 }
389
390 pub fn push_triplets<'a, I>(&mut self, triplets: I)
392 where
393 I: IntoIterator<Item = &'a CcTriplet>,
394 {
395 for t in triplets {
396 if !t.cc_valid {
397 continue;
398 }
399 let field2 = match t.cc_type {
400 CcType::Ntsc608Field1 => false,
401 CcType::Ntsc608Field2 => true,
402 _ => continue,
403 };
404 self.process_pair(field2, t.cc_data_1, t.cc_data_2);
405 }
406 }
407
408 pub fn push_pair(&mut self, field2: bool, b1: u8, b2: u8) {
411 self.process_pair(field2, b1, b2);
412 }
413
414 fn process_pair(&mut self, field2: bool, raw1: u8, raw2: u8) {
415 let b1 = raw1 & 0x7F;
416 let b2 = raw2 & 0x7F;
417 let field_idx = usize::from(field2);
418
419 if b1 == 0x00 && b2 == 0x00 {
421 return;
422 }
423
424 if field2 && (0x01..=0x0F).contains(&b1) {
428 self.xds_active = b1 != 0x0F;
431 return;
432 }
433
434 if (0x10..=0x1F).contains(&b1) {
440 if self.last_control[field_idx] == Some((b1, b2)) {
442 self.last_control[field_idx] = None; return;
444 }
445 self.last_control[field_idx] = Some((b1, b2));
446 self.xds_active = false;
447 self.handle_control(field2, b1, b2);
448 return;
449 }
450 if field2 && self.xds_active {
453 return;
454 }
455
456 self.last_control[field_idx] = None;
458 self.xds_active = false;
459 let ch = self.last_channel[field_idx];
462 if b1 >= 0x20 {
463 self.put_char(ch, Self::standard_char(b1));
464 }
465 if b2 >= 0x20 {
466 self.put_char(ch, Self::standard_char(b2));
467 }
468 }
469
470 fn handle_control(&mut self, field2: bool, b1: u8, b2: u8) {
472 let field2_misc = field2 && (0x20..=0x2F).contains(&b2) && matches!(b1, 0x15 | 0x1D);
484 let c2 = if field2_misc { b1 == 0x1D } else { b1 >= 0x18 };
485 let base1 = if field2_misc {
486 0x14
487 } else if c2 {
488 b1 - 0x08 } else {
490 b1
491 };
492 let ch = self.channel_for(field2, c2);
493 self.last_channel[usize::from(field2)] = ch;
496
497 if base1 == 0x14 && (0x20..=0x2F).contains(&b2) {
500 self.misc_control(ch, b2);
501 return;
502 }
503 if (b1 == TAB_FIRST_C1 || b1 == TAB_FIRST_C2) && (TAB1..=TAB3).contains(&b2) {
505 let n = (b2 - TAB1 + 1) as usize;
506 let st = &mut self.channels[ch.index()];
507 st.cursor_col = (st.cursor_col + n).min(SCREEN_COLS - 1);
508 return;
509 }
510 if base1 == 0x11 && (0x20..=0x2F).contains(&b2) {
512 self.mid_row(ch, b2);
513 return;
514 }
515 if base1 == 0x11 && (0x30..=0x3F).contains(&b2) {
517 self.put_char(ch, Self::special_char(b2));
518 return;
519 }
520 if base1 == 0x12 && (0x20..=0x3F).contains(&b2) {
522 self.put_extended(ch, Self::extended_char_block1(b2));
523 return;
524 }
525 if base1 == 0x13 && (0x20..=0x3F).contains(&b2) {
527 self.put_extended(ch, Self::extended_char_block2(b2));
528 return;
529 }
530 if (0x40..=0x7F).contains(&b2) {
532 self.pac(field2, ch, b1, b2);
533 }
534 }
537
538 fn misc_control(&mut self, ch: Cea608Channel, code: u8) {
540 let st = &mut self.channels[ch.index()];
541 match code {
542 MC_RCL => {
543 st.mode = Cea608Mode::PopOn;
549 st.cursor_row = 0;
550 st.cursor_col = 0;
551 }
552 MC_RU2 | MC_RU3 | MC_RU4 => {
553 let rows = match code {
554 MC_RU2 => 2,
555 MC_RU3 => 3,
556 _ => 4,
557 };
558 st.mode = Cea608Mode::RollUp(rows);
559 st.rollup_rows = rows;
560 st.cursor_row = SCREEN_ROWS - 1;
562 st.cursor_col = 0;
563 st.pen = Pen::default();
564 }
565 MC_RDC => {
566 st.mode = Cea608Mode::PaintOn;
567 }
568 MC_CR => {
569 self.carriage_return(ch);
570 }
571 MC_EDM => {
572 st.displayed = Cea608Screen::default();
573 }
574 MC_ENM => {
575 st.nondisplayed = Cea608Screen::default();
576 }
577 MC_EOC => {
578 core::mem::swap(&mut st.displayed, &mut st.nondisplayed);
580 }
581 MC_BS if st.cursor_col > 0 => {
582 st.cursor_col -= 1;
583 let row = st.cursor_row.min(SCREEN_ROWS - 1);
584 let col = st.cursor_col;
585 st.active_mut().rows[row].remove(col);
586 }
587 MC_DER => {
588 let row = st.cursor_row.min(SCREEN_ROWS - 1);
589 let col = st.cursor_col;
590 st.active_mut().rows[row].clear_from(col);
591 }
592 MC_TR => {
593 st.mode = Cea608Mode::Text;
594 st.displayed = Cea608Screen::default();
595 st.cursor_row = 0;
596 st.cursor_col = 0;
597 }
598 MC_RTD => {
599 st.mode = Cea608Mode::Text;
600 }
601 MC_FON => {}
602 _ => {}
603 }
604 }
605
606 fn carriage_return(&mut self, ch: Cea608Channel) {
608 let st = &mut self.channels[ch.index()];
609 if let Cea608Mode::RollUp(rows) = st.mode {
610 let base = st.cursor_row.min(SCREEN_ROWS - 1);
611 let rows = rows as usize;
612 let top = base.saturating_sub(rows - 1);
613 for r in top..base {
614 st.displayed.rows[r] = st.displayed.rows[r + 1].clone();
615 }
616 st.displayed.rows[base] = Cea608Row::default();
617 st.cursor_col = 0;
618 } else {
619 if st.cursor_row + 1 < SCREEN_ROWS {
621 st.cursor_row += 1;
622 }
623 st.cursor_col = 0;
624 }
625 }
626
627 fn mid_row(&mut self, ch: Cea608Channel, b2: u8) {
629 let idx = (b2 - 0x20) as usize; let underline = idx & 0x01 != 0;
631 let color_idx = (idx >> 1) as u8;
632 let (color, italics) = if color_idx <= 6 {
633 (Cea608Color::from_idx(color_idx), false)
634 } else {
635 (Cea608Color::White, true) };
637 let st = &mut self.channels[ch.index()];
638 st.pen = Pen {
639 underline,
640 italics,
641 color,
642 };
643 self.put_char(ch, ' ');
645 }
646
647 fn pac(&mut self, _field2: bool, ch: Cea608Channel, b1: u8, b2: u8) {
649 let f = if b1 >= 0x18 { b1 - 0x08 } else { b1 };
651 let row_pair = match f {
653 0x11 => 1, 0x12 => 3, 0x15 => 5, 0x16 => 7, 0x17 => 9, 0x10 => 11, 0x13 => 12, 0x14 => 14, _ => 1,
662 };
663 let second_of_pair = b2 >= 0x60;
666 let row = if f == 0x10 {
667 11 } else if second_of_pair {
669 row_pair + 1
670 } else {
671 row_pair
672 };
673 let row = row.clamp(1, SCREEN_ROWS); let attr = b2 & 0x1F; let underline = attr & 0x01 != 0;
676
677 let (color, italics, indent) = if attr >= 0x10 {
678 let indent = ((attr - 0x10) >> 1) as usize * 4;
680 (Cea608Color::White, false, indent)
681 } else {
682 let color_idx = attr >> 1;
684 let (c, it) = if color_idx <= 6 {
685 (Cea608Color::from_idx(color_idx), false)
686 } else {
687 (Cea608Color::White, true)
688 };
689 (c, it, 0)
690 };
691
692 let st = &mut self.channels[ch.index()];
693 st.cursor_row = (row - 1).min(SCREEN_ROWS - 1);
694 st.cursor_col = indent.min(SCREEN_COLS - 1);
695 st.pen = Pen {
696 underline,
697 italics,
698 color,
699 };
700 }
701
702 fn put_char(&mut self, ch: Cea608Channel, c: char) {
704 let st = &mut self.channels[ch.index()];
705 if st.mode != Cea608Mode::Text {
706 self.caption_text_seen |= 1 << ch.index();
707 }
708 let row = st.cursor_row.min(SCREEN_ROWS - 1);
709 let col = st.cursor_col;
710 if col >= SCREEN_COLS {
711 return;
712 }
713 let pen = st.pen;
714 let styled = Cea608StyledChar {
715 ch: c,
716 underline: pen.underline,
717 italics: pen.italics,
718 color: pen.color,
719 };
720 st.active_mut().rows[row].set(col, styled);
721 st.cursor_col = (col + 1).min(SCREEN_COLS);
722 }
723
724 fn put_extended(&mut self, ch: Cea608Channel, c: char) {
727 {
728 let st = &mut self.channels[ch.index()];
729 if st.cursor_col > 0 {
730 st.cursor_col -= 1;
731 let row = st.cursor_row.min(SCREEN_ROWS - 1);
732 let col = st.cursor_col;
733 st.active_mut().rows[row].remove(col);
734 }
735 }
736 self.put_char(ch, c);
737 }
738
739 fn channel_for(&self, field2: bool, c2: bool) -> Cea608Channel {
741 match (field2, c2) {
742 (false, false) => Cea608Channel::Cc1,
743 (false, true) => Cea608Channel::Cc2,
744 (true, false) => Cea608Channel::Cc3,
745 (true, true) => Cea608Channel::Cc4,
746 }
747 }
748
749 #[must_use]
753 pub fn has_caption_text(&self, channel: Cea608Channel) -> bool {
754 self.caption_text_seen & (1 << channel.index()) != 0
755 }
756
757 #[must_use]
759 pub fn screen(&self, channel: Cea608Channel) -> &Cea608Screen {
760 &self.channels[channel.index()].displayed
761 }
762
763 #[must_use]
765 pub fn mode(&self, channel: Cea608Channel) -> Cea608Mode {
766 self.channels[channel.index()].mode
767 }
768
769 #[must_use]
771 pub fn channel_text(&self, channel: Cea608Channel) -> String {
772 self.channels[channel.index()].displayed.text()
773 }
774
775 fn standard_char(b: u8) -> char {
779 match b {
780 0x2A => '\u{00E1}', 0x5C => '\u{00E9}', 0x5E => '\u{00ED}', 0x5F => '\u{00F3}', 0x60 => '\u{00FA}', 0x7B => '\u{00E7}', 0x7C => '\u{00F7}', 0x7D => '\u{00D1}', 0x7E => '\u{00F1}', 0x7F => '\u{25A0}', _ => char::from(b), }
792 }
793
794 fn special_char(b2: u8) -> char {
796 match b2 {
797 0x30 => '\u{00AE}', 0x31 => '\u{00B0}', 0x32 => '\u{00BD}', 0x33 => '\u{00BF}', 0x34 => '\u{2122}', 0x35 => '\u{00A2}', 0x36 => '\u{00A3}', 0x37 => '\u{266A}', 0x38 => '\u{00E0}', 0x39 => ' ', 0x3A => '\u{00E8}', 0x3B => '\u{00E2}', 0x3C => '\u{00EA}', 0x3D => '\u{00EE}', 0x3E => '\u{00F4}', 0x3F => '\u{00FB}', _ => '?',
814 }
815 }
816
817 fn extended_char_block1(b2: u8) -> char {
819 match b2 {
820 0x20 => '\u{00C1}', 0x21 => '\u{00C9}', 0x22 => '\u{00D3}', 0x23 => '\u{00DA}', 0x24 => '\u{00DC}', 0x25 => '\u{00FC}', 0x26 => '\u{2018}', 0x27 => '\u{00A1}', 0x28 => '*',
831 0x29 => '\'',
832 0x2A => '\u{2014}', 0x2B => '\u{00A9}', 0x2C => '\u{2120}', 0x2D => '\u{2022}', 0x2E => '\u{201C}', 0x2F => '\u{201D}', 0x30 => '\u{00C0}', 0x31 => '\u{00C2}', 0x32 => '\u{00C7}', 0x33 => '\u{00C8}', 0x34 => '\u{00CA}', 0x35 => '\u{00CB}', 0x36 => '\u{00EB}', 0x37 => '\u{00CE}', 0x38 => '\u{00CF}', 0x39 => '\u{00EF}', 0x3A => '\u{00D4}', 0x3B => '\u{00D9}', 0x3C => '\u{00F9}', 0x3D => '\u{00DB}', 0x3E => '\u{00AB}', 0x3F => '\u{00BB}', _ => '?',
856 }
857 }
858
859 fn extended_char_block2(b2: u8) -> char {
861 match b2 {
862 0x20 => '\u{00C3}', 0x21 => '\u{00E3}', 0x22 => '\u{00CD}', 0x23 => '\u{00CC}', 0x24 => '\u{00EC}', 0x25 => '\u{00D2}', 0x26 => '\u{00F2}', 0x27 => '\u{00D5}', 0x28 => '\u{00F5}', 0x29 => '{',
873 0x2A => '}',
874 0x2B => '\\',
875 0x2C => '^',
876 0x2D => '_',
877 0x2E => '|',
878 0x2F => '~',
879 0x30 => '\u{00C4}', 0x31 => '\u{00E4}', 0x32 => '\u{00D6}', 0x33 => '\u{00F6}', 0x34 => '\u{00DF}', 0x35 => '\u{00A5}', 0x36 => '\u{00A4}', 0x37 => '|',
888 0x38 => '\u{00C5}', 0x39 => '\u{00E5}', 0x3A => '\u{00D8}', 0x3B => '\u{00F8}', 0x3C => '\u{231C}', 0x3D => '\u{231D}', 0x3E => '\u{231E}', 0x3F => '\u{231F}', _ => '?',
898 }
899 }
900}
901
902#[cfg(test)]
903mod tests {
904 use super::*;
905 use broadcast_common::parity::set_odd_parity;
906
907 #[test]
909 fn pop_on_caption() {
910 let mut dec = Cea608Decoder::new();
911 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x70)); dec.push_pair(false, set_odd_parity(b'H'), set_odd_parity(b'I'));
914 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc1), "HI");
916 assert_eq!(dec.mode(Cea608Channel::Cc1), Cea608Mode::PopOn);
917 }
918
919 #[test]
924 fn rcl_does_not_erase_loaded_text() {
925 let mut dec = Cea608Decoder::new();
926 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x70)); dec.push_pair(false, set_odd_parity(b'H'), set_odd_parity(b'I'));
929 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2C)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc1), "HI");
933 }
934
935 #[test]
937 fn enm_erases_loaded_text() {
938 let mut dec = Cea608Decoder::new();
939 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x70));
941 dec.push_pair(false, set_odd_parity(b'H'), set_odd_parity(b'I'));
942 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2E)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc1), "");
945 }
946
947 #[test]
949 fn control_doubling() {
950 let mut dec = Cea608Decoder::new();
951 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x29)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc1), Cea608Mode::PaintOn);
954 dec.push_pair(false, set_odd_parity(b'X'), set_odd_parity(0x00));
956 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2C)); }
958
959 #[test]
961 fn roll_up_two_rows() {
962 let mut dec = Cea608Decoder::new();
963 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x25)); dec.push_pair(false, set_odd_parity(b'A'), set_odd_parity(b'B'));
965 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2D)); dec.push_pair(false, set_odd_parity(b'C'), set_odd_parity(b'D'));
967 let text = dec.channel_text(Cea608Channel::Cc1);
968 assert!(text.contains("AB"), "got {text:?}");
969 assert!(text.contains("CD"), "got {text:?}");
970 let ab = text.find("AB").unwrap();
972 let cd = text.find("CD").unwrap();
973 assert!(ab < cd, "AB should be above CD: {text:?}");
974 }
975
976 #[test]
978 fn mid_row_colour() {
979 let mut dec = Cea608Decoder::new();
980 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x70)); dec.push_pair(false, set_odd_parity(b'A'), set_odd_parity(0x00));
983 dec.push_pair(false, set_odd_parity(0x11), set_odd_parity(0x22)); dec.push_pair(false, set_odd_parity(b'B'), set_odd_parity(0x00));
985 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2F)); let screen = dec.screen(Cea608Channel::Cc1);
988 let mut found_green_b = false;
989 for row in screen.rows() {
990 for (_, c) in row.styled_cells() {
991 if c.ch == 'B' {
992 assert_eq!(c.color, Cea608Color::Green);
993 found_green_b = true;
994 }
995 }
996 }
997 assert!(found_green_b, "expected a green 'B'");
998 }
999
1000 #[test]
1001 fn caption_text_is_tracked_per_channel() {
1002 let mut dec = Cea608Decoder::new();
1003 dec.push_pair(false, set_odd_parity(0x1C), set_odd_parity(0x29));
1004 dec.push_pair(false, set_odd_parity(b'H'), set_odd_parity(b'I'));
1005 assert!(dec.has_caption_text(Cea608Channel::Cc2));
1006 assert!(!dec.has_caption_text(Cea608Channel::Cc1));
1007 assert!(!dec.has_caption_text(Cea608Channel::Cc3));
1008 assert!(!dec.has_caption_text(Cea608Channel::Cc4));
1009 }
1010
1011 #[test]
1012 fn control_codes_xds_and_text_mode_are_not_caption_text() {
1013 let mut dec = Cea608Decoder::new();
1014 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20));
1015 dec.push_pair(true, set_odd_parity(0x01), set_odd_parity(0x03));
1016 dec.push_pair(true, set_odd_parity(b'A'), set_odd_parity(b'B'));
1017 dec.push_pair(true, set_odd_parity(0x0F), set_odd_parity(0x2C));
1018 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2A));
1019 dec.push_pair(false, set_odd_parity(b'T'), set_odd_parity(b'X'));
1020 for ch in [
1021 Cea608Channel::Cc1,
1022 Cea608Channel::Cc2,
1023 Cea608Channel::Cc3,
1024 Cea608Channel::Cc4,
1025 ] {
1026 assert!(!dec.has_caption_text(ch), "{}", ch.name());
1027 }
1028 }
1029
1030 #[test]
1032 fn special_char_music_note() {
1033 let mut dec = Cea608Decoder::new();
1034 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x29)); dec.push_pair(false, set_odd_parity(0x11), set_odd_parity(0x37)); assert!(dec.channel_text(Cea608Channel::Cc1).contains('\u{266A}'));
1037 }
1038
1039 #[test]
1041 fn extended_char_backspace() {
1042 let mut dec = Cea608Decoder::new();
1043 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x29)); dec.push_pair(false, set_odd_parity(b'u'), set_odd_parity(0x00)); dec.push_pair(false, set_odd_parity(0x12), set_odd_parity(0x25)); let t = dec.channel_text(Cea608Channel::Cc1);
1047 assert_eq!(t, "\u{00FC}"); }
1049
1050 #[test]
1052 fn channel_cc2() {
1053 let mut dec = Cea608Decoder::new();
1054 dec.push_pair(false, set_odd_parity(0x1C), set_odd_parity(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc2), Cea608Mode::PaintOn);
1056 assert_eq!(dec.mode(Cea608Channel::Cc1), Cea608Mode::None);
1057 }
1058
1059 #[test]
1066 fn field2_cc3() {
1067 let mut dec = Cea608Decoder::new();
1068 dec.push_pair(true, set_odd_parity(0x15), set_odd_parity(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc3), Cea608Mode::PaintOn);
1070 }
1071
1072 #[test]
1075 fn field2_cc4() {
1076 let mut dec = Cea608Decoder::new();
1077 dec.push_pair(true, set_odd_parity(0x1D), set_odd_parity(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc4), Cea608Mode::PaintOn);
1079 assert_eq!(dec.mode(Cea608Channel::Cc3), Cea608Mode::None);
1080 }
1081
1082 #[test]
1084 fn xds_skipped() {
1085 let mut dec = Cea608Decoder::new();
1086 dec.push_pair(true, set_odd_parity(0x01), set_odd_parity(0x02)); dec.push_pair(true, set_odd_parity(0x20), set_odd_parity(0x21)); dec.push_pair(true, set_odd_parity(0x0F), set_odd_parity(0x40)); assert_eq!(dec.channel_text(Cea608Channel::Cc3), "");
1090 }
1091
1092 #[test]
1098 fn control_code_interrupts_open_xds() {
1099 let mut dec = Cea608Decoder::new();
1100 dec.push_pair(true, set_odd_parity(0x01), set_odd_parity(0x02)); dec.push_pair(true, set_odd_parity(0x15), set_odd_parity(0x20)); dec.push_pair(true, set_odd_parity(0x15), set_odd_parity(0x70)); dec.push_pair(true, set_odd_parity(b'H'), set_odd_parity(b'I'));
1104 dec.push_pair(true, set_odd_parity(0x15), set_odd_parity(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc3), "HI");
1106 }
1107
1108 #[test]
1114 fn standard_chars_follow_last_control_channel() {
1115 let mut dec = Cea608Decoder::new();
1116 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x70)); dec.push_pair(false, set_odd_parity(b'A'), set_odd_parity(b'B'));
1120 dec.push_pair(false, set_odd_parity(0x14), set_odd_parity(0x2F)); dec.push_pair(false, set_odd_parity(0x1C), set_odd_parity(0x20)); dec.push_pair(false, set_odd_parity(0x1C), set_odd_parity(0x70)); dec.push_pair(false, set_odd_parity(b'C'), set_odd_parity(b'D'));
1125 dec.push_pair(false, set_odd_parity(0x1C), set_odd_parity(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc1), "AB");
1127 assert_eq!(dec.channel_text(Cea608Channel::Cc2), "CD");
1128 }
1129
1130 #[test]
1131 fn standard_char_accents() {
1132 assert_eq!(Cea608Decoder::standard_char(0x2A), '\u{00E1}'); assert_eq!(Cea608Decoder::standard_char(b'A'), 'A');
1134 assert_eq!(Cea608Decoder::standard_char(0x7F), '\u{25A0}'); }
1136
1137 #[test]
1138 fn no_panic_on_arbitrary_input() {
1139 let mut dec = Cea608Decoder::new();
1140 let mut x: u32 = 0xDEAD_BEEF;
1141 for _ in 0..8192 {
1142 x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345);
1143 let b1 = (x >> 8) as u8;
1144 let b2 = (x >> 16) as u8;
1145 let f2 = (x & 1) != 0;
1146 dec.push_pair(f2, b1, b2);
1147 }
1148 let triplets = [
1150 CcTriplet {
1151 cc_valid: true,
1152 cc_type: CcType::Ntsc608Field1,
1153 cc_data_1: 0x14,
1154 cc_data_2: 0x2D,
1155 },
1156 CcTriplet {
1157 cc_valid: false,
1158 cc_type: CcType::Ntsc608Field2,
1159 cc_data_1: 0xFF,
1160 cc_data_2: 0xFF,
1161 },
1162 CcTriplet {
1163 cc_valid: true,
1164 cc_type: CcType::Ntsc608Field2,
1165 cc_data_1: 0x01,
1166 cc_data_2: 0x00,
1167 },
1168 ];
1169 dec.push_triplets(&triplets);
1170 }
1171}