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)]
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 fn index(self) -> usize {
160 match self {
161 Self::Cc1 => 0,
162 Self::Cc2 => 1,
163 Self::Cc3 => 2,
164 Self::Cc4 => 3,
165 }
166 }
167 #[must_use]
169 pub fn name(&self) -> &'static str {
170 match self {
171 Self::Cc1 => "cc1",
172 Self::Cc2 => "cc2",
173 Self::Cc3 => "cc3",
174 Self::Cc4 => "cc4",
175 }
176 }
177}
178broadcast_common::impl_spec_display!(Cea608Channel);
179
180#[derive(Debug, Clone, Copy, PartialEq, Eq)]
182#[cfg_attr(feature = "serde", derive(serde::Serialize))]
183pub struct Cea608StyledChar {
184 pub ch: char,
186 pub underline: bool,
188 pub italics: bool,
190 pub color: Cea608Color,
192}
193
194impl Default for Cea608StyledChar {
195 fn default() -> Self {
196 Cea608StyledChar {
197 ch: ' ',
198 underline: false,
199 italics: false,
200 color: Cea608Color::White,
201 }
202 }
203}
204
205#[derive(Debug, Clone, PartialEq, Eq, Default)]
207#[cfg_attr(feature = "serde", derive(serde::Serialize))]
208pub struct Cea608Row {
209 cells: Vec<(usize, Cea608StyledChar)>,
211}
212
213impl Cea608Row {
214 fn set(&mut self, col: usize, c: Cea608StyledChar) {
215 if let Some(slot) = self.cells.iter_mut().find(|(i, _)| *i == col) {
216 slot.1 = c;
217 } else {
218 self.cells.push((col, c));
219 }
220 }
221 fn clear_from(&mut self, col: usize) {
222 self.cells.retain(|(i, _)| *i < col);
223 }
224 fn remove(&mut self, col: usize) {
225 self.cells.retain(|(i, _)| *i != col);
226 }
227 #[must_use]
229 pub fn text(&self) -> String {
230 let mut sorted = self.cells.clone();
231 sorted.sort_by_key(|(i, _)| *i);
232 let mut out = String::new();
233 let mut last = None;
234 for (i, c) in sorted {
235 if let Some(prev) = last {
236 for _ in (prev + 1)..i {
237 out.push(' ');
238 }
239 }
240 out.push(c.ch);
241 last = Some(i);
242 }
243 out
244 }
245 #[must_use]
247 pub fn styled_cells(&self) -> Vec<(usize, Cea608StyledChar)> {
248 let mut sorted = self.cells.clone();
249 sorted.sort_by_key(|(i, _)| *i);
250 sorted
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq, Default)]
256#[cfg_attr(feature = "serde", derive(serde::Serialize))]
257pub struct Cea608Screen {
258 rows: [Cea608Row; SCREEN_ROWS],
259}
260
261impl Cea608Screen {
262 #[must_use]
264 pub fn text(&self) -> String {
265 let mut out = String::new();
266 for row in &self.rows {
267 let line = row.text();
268 let trimmed = line.trim_end();
269 if trimmed.is_empty() {
270 continue;
271 }
272 if !out.is_empty() {
273 out.push('\n');
274 }
275 out.push_str(trimmed);
276 }
277 out
278 }
279 #[must_use]
281 pub fn rows(&self) -> &[Cea608Row; SCREEN_ROWS] {
282 &self.rows
283 }
284}
285
286#[derive(Debug, Clone, Copy, PartialEq, Eq)]
288struct Pen {
289 underline: bool,
290 italics: bool,
291 color: Cea608Color,
292}
293
294impl Default for Pen {
295 fn default() -> Self {
296 Pen {
297 underline: false,
298 italics: false,
299 color: Cea608Color::White,
300 }
301 }
302}
303
304#[derive(Debug, Clone, PartialEq, Eq, Default)]
306struct ChannelState {
307 displayed: Cea608Screen,
308 nondisplayed: Cea608Screen,
309 mode: Cea608Mode,
310 rollup_rows: u8,
311 cursor_row: usize,
312 cursor_col: usize,
313 pen: Pen,
314}
315
316impl ChannelState {
317 fn active_mut(&mut self) -> &mut Cea608Screen {
320 match self.mode {
321 Cea608Mode::PopOn => &mut self.nondisplayed,
322 _ => &mut self.displayed,
323 }
324 }
325}
326
327#[derive(Debug, Clone, PartialEq, Eq)]
345pub struct Cea608Decoder {
346 channels: [ChannelState; 4],
347 last_control: [Option<(u8, u8)>; 2],
349 last_channel: [Cea608Channel; 2],
354 xds_active: bool,
355}
356
357impl Default for Cea608Decoder {
358 fn default() -> Self {
359 Self::new()
360 }
361}
362
363impl Cea608Decoder {
364 #[must_use]
366 pub fn new() -> Self {
367 Cea608Decoder {
368 channels: Default::default(),
369 last_control: [None, None],
370 last_channel: [Cea608Channel::Cc1, Cea608Channel::Cc3],
371 xds_active: false,
372 }
373 }
374
375 pub fn push_triplets<'a, I>(&mut self, triplets: I)
377 where
378 I: IntoIterator<Item = &'a CcTriplet>,
379 {
380 for t in triplets {
381 if !t.cc_valid {
382 continue;
383 }
384 let field2 = match t.cc_type {
385 CcType::Ntsc608Field1 => false,
386 CcType::Ntsc608Field2 => true,
387 _ => continue,
388 };
389 self.process_pair(field2, t.cc_data_1, t.cc_data_2);
390 }
391 }
392
393 pub fn push_pair(&mut self, field2: bool, b1: u8, b2: u8) {
396 self.process_pair(field2, b1, b2);
397 }
398
399 fn process_pair(&mut self, field2: bool, raw1: u8, raw2: u8) {
400 let b1 = raw1 & 0x7F;
401 let b2 = raw2 & 0x7F;
402 let field_idx = usize::from(field2);
403
404 if b1 == 0x00 && b2 == 0x00 {
406 return;
407 }
408
409 if field2 && (0x01..=0x0F).contains(&b1) {
413 self.xds_active = b1 != 0x0F;
416 return;
417 }
418
419 if (0x10..=0x1F).contains(&b1) {
425 if self.last_control[field_idx] == Some((b1, b2)) {
427 self.last_control[field_idx] = None; return;
429 }
430 self.last_control[field_idx] = Some((b1, b2));
431 self.xds_active = false;
432 self.handle_control(field2, b1, b2);
433 return;
434 }
435 if field2 && self.xds_active {
438 return;
439 }
440
441 self.last_control[field_idx] = None;
443 self.xds_active = false;
444 let ch = self.last_channel[field_idx];
447 if b1 >= 0x20 {
448 self.put_char(ch, Self::standard_char(b1));
449 }
450 if b2 >= 0x20 {
451 self.put_char(ch, Self::standard_char(b2));
452 }
453 }
454
455 fn handle_control(&mut self, field2: bool, b1: u8, b2: u8) {
457 let field2_misc = field2 && (0x20..=0x2F).contains(&b2) && matches!(b1, 0x15 | 0x1D);
469 let c2 = if field2_misc { b1 == 0x1D } else { b1 >= 0x18 };
470 let base1 = if field2_misc {
471 0x14
472 } else if c2 {
473 b1 - 0x08 } else {
475 b1
476 };
477 let ch = self.channel_for(field2, c2);
478 self.last_channel[usize::from(field2)] = ch;
481
482 if base1 == 0x14 && (0x20..=0x2F).contains(&b2) {
485 self.misc_control(ch, b2);
486 return;
487 }
488 if (b1 == TAB_FIRST_C1 || b1 == TAB_FIRST_C2) && (TAB1..=TAB3).contains(&b2) {
490 let n = (b2 - TAB1 + 1) as usize;
491 let st = &mut self.channels[ch.index()];
492 st.cursor_col = (st.cursor_col + n).min(SCREEN_COLS - 1);
493 return;
494 }
495 if base1 == 0x11 && (0x20..=0x2F).contains(&b2) {
497 self.mid_row(ch, b2);
498 return;
499 }
500 if base1 == 0x11 && (0x30..=0x3F).contains(&b2) {
502 self.put_char(ch, Self::special_char(b2));
503 return;
504 }
505 if base1 == 0x12 && (0x20..=0x3F).contains(&b2) {
507 self.put_extended(ch, Self::extended_char_block1(b2));
508 return;
509 }
510 if base1 == 0x13 && (0x20..=0x3F).contains(&b2) {
512 self.put_extended(ch, Self::extended_char_block2(b2));
513 return;
514 }
515 if (0x40..=0x7F).contains(&b2) {
517 self.pac(field2, ch, b1, b2);
518 }
519 }
522
523 fn misc_control(&mut self, ch: Cea608Channel, code: u8) {
525 let st = &mut self.channels[ch.index()];
526 match code {
527 MC_RCL => {
528 st.mode = Cea608Mode::PopOn;
529 st.nondisplayed = Cea608Screen::default();
530 st.cursor_row = 0;
531 st.cursor_col = 0;
532 }
533 MC_RU2 | MC_RU3 | MC_RU4 => {
534 let rows = match code {
535 MC_RU2 => 2,
536 MC_RU3 => 3,
537 _ => 4,
538 };
539 st.mode = Cea608Mode::RollUp(rows);
540 st.rollup_rows = rows;
541 st.cursor_row = SCREEN_ROWS - 1;
543 st.cursor_col = 0;
544 st.pen = Pen::default();
545 }
546 MC_RDC => {
547 st.mode = Cea608Mode::PaintOn;
548 }
549 MC_CR => {
550 self.carriage_return(ch);
551 }
552 MC_EDM => {
553 st.displayed = Cea608Screen::default();
554 }
555 MC_ENM => {
556 st.nondisplayed = Cea608Screen::default();
557 }
558 MC_EOC => {
559 core::mem::swap(&mut st.displayed, &mut st.nondisplayed);
561 }
562 MC_BS if st.cursor_col > 0 => {
563 st.cursor_col -= 1;
564 let row = st.cursor_row.min(SCREEN_ROWS - 1);
565 let col = st.cursor_col;
566 st.active_mut().rows[row].remove(col);
567 }
568 MC_DER => {
569 let row = st.cursor_row.min(SCREEN_ROWS - 1);
570 let col = st.cursor_col;
571 st.active_mut().rows[row].clear_from(col);
572 }
573 MC_TR => {
574 st.mode = Cea608Mode::Text;
575 st.displayed = Cea608Screen::default();
576 st.cursor_row = 0;
577 st.cursor_col = 0;
578 }
579 MC_RTD => {
580 st.mode = Cea608Mode::Text;
581 }
582 MC_FON => {}
583 _ => {}
584 }
585 }
586
587 fn carriage_return(&mut self, ch: Cea608Channel) {
589 let st = &mut self.channels[ch.index()];
590 if let Cea608Mode::RollUp(rows) = st.mode {
591 let base = st.cursor_row.min(SCREEN_ROWS - 1);
592 let rows = rows as usize;
593 let top = base.saturating_sub(rows - 1);
594 for r in top..base {
596 st.displayed.rows[r] = st.displayed.rows[r + 1].clone();
597 }
598 st.displayed.rows[base] = Cea608Row::default();
599 st.cursor_col = 0;
600 } else {
601 if st.cursor_row + 1 < SCREEN_ROWS {
603 st.cursor_row += 1;
604 }
605 st.cursor_col = 0;
606 }
607 }
608
609 fn mid_row(&mut self, ch: Cea608Channel, b2: u8) {
611 let idx = (b2 - 0x20) as usize; let underline = idx & 0x01 != 0;
613 let color_idx = (idx >> 1) as u8;
614 let (color, italics) = if color_idx <= 6 {
615 (Cea608Color::from_idx(color_idx), false)
616 } else {
617 (Cea608Color::White, true) };
619 let st = &mut self.channels[ch.index()];
620 st.pen = Pen {
621 underline,
622 italics,
623 color,
624 };
625 self.put_char(ch, ' ');
627 }
628
629 fn pac(&mut self, _field2: bool, ch: Cea608Channel, b1: u8, b2: u8) {
631 let f = if b1 >= 0x18 { b1 - 0x08 } else { b1 };
633 let row_pair = match f {
635 0x11 => 1, 0x12 => 3, 0x15 => 5, 0x16 => 7, 0x17 => 9, 0x10 => 11, 0x13 => 12, 0x14 => 14, _ => 1,
644 };
645 let second_of_pair = b2 >= 0x60;
648 let row = if f == 0x10 {
649 11 } else if second_of_pair {
651 row_pair + 1
652 } else {
653 row_pair
654 };
655 let row = row.clamp(1, SCREEN_ROWS); let attr = b2 & 0x1F; let underline = attr & 0x01 != 0;
658
659 let (color, italics, indent) = if attr >= 0x10 {
660 let indent = ((attr - 0x10) >> 1) as usize * 4;
662 (Cea608Color::White, false, indent)
663 } else {
664 let color_idx = attr >> 1;
666 let (c, it) = if color_idx <= 6 {
667 (Cea608Color::from_idx(color_idx), false)
668 } else {
669 (Cea608Color::White, true)
670 };
671 (c, it, 0)
672 };
673
674 let st = &mut self.channels[ch.index()];
675 st.cursor_row = (row - 1).min(SCREEN_ROWS - 1);
676 st.cursor_col = indent.min(SCREEN_COLS - 1);
677 st.pen = Pen {
678 underline,
679 italics,
680 color,
681 };
682 }
683
684 fn put_char(&mut self, ch: Cea608Channel, c: char) {
686 let st = &mut self.channels[ch.index()];
687 let row = st.cursor_row.min(SCREEN_ROWS - 1);
688 let col = st.cursor_col;
689 if col >= SCREEN_COLS {
690 return;
691 }
692 let pen = st.pen;
693 let styled = Cea608StyledChar {
694 ch: c,
695 underline: pen.underline,
696 italics: pen.italics,
697 color: pen.color,
698 };
699 st.active_mut().rows[row].set(col, styled);
700 st.cursor_col = (col + 1).min(SCREEN_COLS);
701 }
702
703 fn put_extended(&mut self, ch: Cea608Channel, c: char) {
706 {
707 let st = &mut self.channels[ch.index()];
708 if st.cursor_col > 0 {
709 st.cursor_col -= 1;
710 let row = st.cursor_row.min(SCREEN_ROWS - 1);
711 let col = st.cursor_col;
712 st.active_mut().rows[row].remove(col);
713 }
714 }
715 self.put_char(ch, c);
716 }
717
718 fn channel_for(&self, field2: bool, c2: bool) -> Cea608Channel {
720 match (field2, c2) {
721 (false, false) => Cea608Channel::Cc1,
722 (false, true) => Cea608Channel::Cc2,
723 (true, false) => Cea608Channel::Cc3,
724 (true, true) => Cea608Channel::Cc4,
725 }
726 }
727
728 #[must_use]
730 pub fn screen(&self, channel: Cea608Channel) -> &Cea608Screen {
731 &self.channels[channel.index()].displayed
732 }
733
734 #[must_use]
736 pub fn mode(&self, channel: Cea608Channel) -> Cea608Mode {
737 self.channels[channel.index()].mode
738 }
739
740 #[must_use]
742 pub fn channel_text(&self, channel: Cea608Channel) -> String {
743 self.channels[channel.index()].displayed.text()
744 }
745
746 fn standard_char(b: u8) -> char {
750 match b {
751 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), }
763 }
764
765 fn special_char(b2: u8) -> char {
767 match b2 {
768 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}', _ => '?',
785 }
786 }
787
788 fn extended_char_block1(b2: u8) -> char {
790 match b2 {
791 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 => '*',
802 0x29 => '\'',
803 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}', _ => '?',
827 }
828 }
829
830 fn extended_char_block2(b2: u8) -> char {
832 match b2 {
833 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 => '{',
844 0x2A => '}',
845 0x2B => '\\',
846 0x2C => '^',
847 0x2D => '_',
848 0x2E => '|',
849 0x2F => '~',
850 0x30 => '\u{00C4}', 0x31 => '\u{00E4}', 0x32 => '\u{00D6}', 0x33 => '\u{00F6}', 0x34 => '\u{00DF}', 0x35 => '\u{00A5}', 0x36 => '\u{00A4}', 0x37 => '|',
859 0x38 => '\u{00C5}', 0x39 => '\u{00E5}', 0x3A => '\u{00D8}', 0x3B => '\u{00F8}', 0x3C => '\u{231C}', 0x3D => '\u{231D}', 0x3E => '\u{231E}', 0x3F => '\u{231F}', _ => '?',
869 }
870 }
871}
872
873#[cfg(test)]
874mod tests {
875 use super::*;
876
877 fn par(v: u8) -> u8 {
879 let ones = (v & 0x7F).count_ones();
880 if ones.is_multiple_of(2) {
881 v | 0x80
882 } else {
883 v & 0x7F
884 }
885 }
886
887 #[test]
889 fn pop_on_caption() {
890 let mut dec = Cea608Decoder::new();
891 dec.push_pair(false, par(0x14), par(0x20)); dec.push_pair(false, par(0x14), par(0x70)); dec.push_pair(false, par(b'H'), par(b'I'));
894 dec.push_pair(false, par(0x14), par(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc1), "HI");
896 assert_eq!(dec.mode(Cea608Channel::Cc1), Cea608Mode::PopOn);
897 }
898
899 #[test]
901 fn control_doubling() {
902 let mut dec = Cea608Decoder::new();
903 dec.push_pair(false, par(0x14), par(0x29)); dec.push_pair(false, par(0x14), par(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc1), Cea608Mode::PaintOn);
906 dec.push_pair(false, par(b'X'), par(0x00));
908 dec.push_pair(false, par(0x14), par(0x2C)); }
910
911 #[test]
913 fn roll_up_two_rows() {
914 let mut dec = Cea608Decoder::new();
915 dec.push_pair(false, par(0x14), par(0x25)); dec.push_pair(false, par(b'A'), par(b'B'));
917 dec.push_pair(false, par(0x14), par(0x2D)); dec.push_pair(false, par(b'C'), par(b'D'));
919 let text = dec.channel_text(Cea608Channel::Cc1);
920 assert!(text.contains("AB"), "got {text:?}");
921 assert!(text.contains("CD"), "got {text:?}");
922 let ab = text.find("AB").unwrap();
924 let cd = text.find("CD").unwrap();
925 assert!(ab < cd, "AB should be above CD: {text:?}");
926 }
927
928 #[test]
930 fn mid_row_colour() {
931 let mut dec = Cea608Decoder::new();
932 dec.push_pair(false, par(0x14), par(0x20)); dec.push_pair(false, par(0x14), par(0x70)); dec.push_pair(false, par(b'A'), par(0x00));
935 dec.push_pair(false, par(0x11), par(0x22)); dec.push_pair(false, par(b'B'), par(0x00));
937 dec.push_pair(false, par(0x14), par(0x2F)); let screen = dec.screen(Cea608Channel::Cc1);
940 let mut found_green_b = false;
941 for row in screen.rows() {
942 for (_, c) in row.styled_cells() {
943 if c.ch == 'B' {
944 assert_eq!(c.color, Cea608Color::Green);
945 found_green_b = true;
946 }
947 }
948 }
949 assert!(found_green_b, "expected a green 'B'");
950 }
951
952 #[test]
954 fn special_char_music_note() {
955 let mut dec = Cea608Decoder::new();
956 dec.push_pair(false, par(0x14), par(0x29)); dec.push_pair(false, par(0x11), par(0x37)); assert!(dec.channel_text(Cea608Channel::Cc1).contains('\u{266A}'));
959 }
960
961 #[test]
963 fn extended_char_backspace() {
964 let mut dec = Cea608Decoder::new();
965 dec.push_pair(false, par(0x14), par(0x29)); dec.push_pair(false, par(b'u'), par(0x00)); dec.push_pair(false, par(0x12), par(0x25)); let t = dec.channel_text(Cea608Channel::Cc1);
969 assert_eq!(t, "\u{00FC}"); }
971
972 #[test]
974 fn channel_cc2() {
975 let mut dec = Cea608Decoder::new();
976 dec.push_pair(false, par(0x1C), par(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc2), Cea608Mode::PaintOn);
978 assert_eq!(dec.mode(Cea608Channel::Cc1), Cea608Mode::None);
979 }
980
981 #[test]
988 fn field2_cc3() {
989 let mut dec = Cea608Decoder::new();
990 dec.push_pair(true, par(0x15), par(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc3), Cea608Mode::PaintOn);
992 }
993
994 #[test]
997 fn field2_cc4() {
998 let mut dec = Cea608Decoder::new();
999 dec.push_pair(true, par(0x1D), par(0x29)); assert_eq!(dec.mode(Cea608Channel::Cc4), Cea608Mode::PaintOn);
1001 assert_eq!(dec.mode(Cea608Channel::Cc3), Cea608Mode::None);
1002 }
1003
1004 #[test]
1006 fn xds_skipped() {
1007 let mut dec = Cea608Decoder::new();
1008 dec.push_pair(true, par(0x01), par(0x02)); dec.push_pair(true, par(0x20), par(0x21)); dec.push_pair(true, par(0x0F), par(0x40)); assert_eq!(dec.channel_text(Cea608Channel::Cc3), "");
1012 }
1013
1014 #[test]
1020 fn control_code_interrupts_open_xds() {
1021 let mut dec = Cea608Decoder::new();
1022 dec.push_pair(true, par(0x01), par(0x02)); dec.push_pair(true, par(0x15), par(0x20)); dec.push_pair(true, par(0x15), par(0x70)); dec.push_pair(true, par(b'H'), par(b'I'));
1026 dec.push_pair(true, par(0x15), par(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc3), "HI");
1028 }
1029
1030 #[test]
1036 fn standard_chars_follow_last_control_channel() {
1037 let mut dec = Cea608Decoder::new();
1038 dec.push_pair(false, par(0x14), par(0x20)); dec.push_pair(false, par(0x14), par(0x70)); dec.push_pair(false, par(b'A'), par(b'B'));
1042 dec.push_pair(false, par(0x14), par(0x2F)); dec.push_pair(false, par(0x1C), par(0x20)); dec.push_pair(false, par(0x1C), par(0x70)); dec.push_pair(false, par(b'C'), par(b'D'));
1047 dec.push_pair(false, par(0x1C), par(0x2F)); assert_eq!(dec.channel_text(Cea608Channel::Cc1), "AB");
1049 assert_eq!(dec.channel_text(Cea608Channel::Cc2), "CD");
1050 }
1051
1052 #[test]
1053 fn standard_char_accents() {
1054 assert_eq!(Cea608Decoder::standard_char(0x2A), '\u{00E1}'); assert_eq!(Cea608Decoder::standard_char(b'A'), 'A');
1056 assert_eq!(Cea608Decoder::standard_char(0x7F), '\u{25A0}'); }
1058
1059 #[test]
1060 fn no_panic_on_arbitrary_input() {
1061 let mut dec = Cea608Decoder::new();
1062 let mut x: u32 = 0xDEAD_BEEF;
1063 for _ in 0..8192 {
1064 x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345);
1065 let b1 = (x >> 8) as u8;
1066 let b2 = (x >> 16) as u8;
1067 let f2 = (x & 1) != 0;
1068 dec.push_pair(f2, b1, b2);
1069 }
1070 let triplets = [
1072 CcTriplet {
1073 cc_valid: true,
1074 cc_type: CcType::Ntsc608Field1,
1075 cc_data_1: 0x14,
1076 cc_data_2: 0x2D,
1077 },
1078 CcTriplet {
1079 cc_valid: false,
1080 cc_type: CcType::Ntsc608Field2,
1081 cc_data_1: 0xFF,
1082 cc_data_2: 0xFF,
1083 },
1084 CcTriplet {
1085 cc_valid: true,
1086 cc_type: CcType::Ntsc608Field2,
1087 cc_data_1: 0x01,
1088 cc_data_2: 0x00,
1089 },
1090 ];
1091 dec.push_triplets(&triplets);
1092 }
1093}