edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | use ec_cc::ccdata::*; |
| 2 | use ec_cc::synth::*; |
| 3 | use ec_cc::{Cea608Decoder, Cea708Decoder}; |
| 4 | use ec_subs::{Segment, plain_text}; |
| 5 | |
| 6 | fn run(script: &Script608, field: u8) -> Vec<ec_cc::CaptionCue> { |
| 7 | let mut d = Cea608Decoder::new(); |
| 8 | for (i, p) in script.pairs.iter().enumerate() { |
| 9 | d.push_pair((i as i64 + 1) * 1000, field, p[0], p[1]); |
| 10 | } |
| 11 | d.finish((script.pairs.len() as i64 + 1) * 1000) |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn pop_on_basic() { |
| 16 | let mut s = Script608::new(); |
| 17 | s.rcl() |
| 18 | .enm() |
| 19 | .pac(15, 4, false) |
| 20 | .text("HELLO WORLD") |
| 21 | .eoc() |
| 22 | .idle(3) |
| 23 | .edm(); |
| 24 | let cues = run(&s, 0); |
| 25 | assert_eq!(cues.len(), 1); |
| 26 | let c = &cues[0]; |
| 27 | assert_eq!(c.channel, 1); |
| 28 | assert_eq!(plain_text(&c.cue.segments), "HELLO WORLD"); |
| 29 | assert_eq!((c.lines[0].row, c.lines[0].col), (14, 4)); |
| 30 | // EOC is the 13th pair (RCL x2, ENM x2, PAC x2, 6 text pairs) and its first copy sits at idx 12. |
| 31 | assert_eq!(c.cue.start_us, 13_000); |
| 32 | assert!(c.cue.end_us > c.cue.start_us); |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn pop_on_two_captions_and_two_rows() { |
| 37 | let mut s = Script608::new(); |
| 38 | s.rcl() |
| 39 | .enm() |
| 40 | .pac(14, 0, false) |
| 41 | .text("LINE A") |
| 42 | .pac(15, 0, false) |
| 43 | .text("LINE B") |
| 44 | .eoc(); |
| 45 | s.idle(5); |
| 46 | s.enm().pac(15, 8, false).text("NEXT").eoc().idle(5).edm(); |
| 47 | let cues = run(&s, 0); |
| 48 | assert_eq!(cues.len(), 2); |
| 49 | assert_eq!(plain_text(&cues[0].cue.segments), "LINE A\nLINE B"); |
| 50 | assert_eq!(plain_text(&cues[1].cue.segments), "NEXT"); |
| 51 | assert_eq!(cues[0].cue.end_us, cues[1].cue.start_us); |
| 52 | assert_eq!(cues[1].lines[0].col, 8); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn styles_and_char_sets() { |
| 57 | let mut s = Script608::new(); |
| 58 | s.rcl().enm().pac_style(15, 0, 4, false, false).text("RED "); |
| 59 | s.midrow(0, true, false) |
| 60 | .text("ITA ") |
| 61 | .midrow(0, false, true) |
| 62 | .text("UND"); |
| 63 | s.special(7).extended(0, 0).eoc().idle(2).edm(); |
| 64 | let cues = run(&s, 0); |
| 65 | assert_eq!(cues.len(), 1); |
| 66 | let t = plain_text(&cues[0].cue.segments); |
| 67 | assert!(t.starts_with("RED"), "{t}"); |
| 68 | assert!(t.contains('\u{266A}'), "{t}"); |
| 69 | // extended char replaces the filler '?' |
| 70 | assert!(t.contains('Á') && !t.contains('?'), "{t}"); |
| 71 | let has = |f: &dyn Fn(&Segment) -> bool| cues[0].cue.segments.iter().any(f); |
| 72 | assert!(has(&|g| matches!( |
| 73 | g, |
| 74 | Segment::Color { |
| 75 | rgb: (255, 0, 0), |
| 76 | .. |
| 77 | } |
| 78 | ))); |
| 79 | assert!(has( |
| 80 | &|g| matches!(g, Segment::Italic(_)) || matches!(g, Segment::Underline(_)) |
| 81 | )); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn roll_up_cr_makes_new_cue_and_doubled_cr_rolls_once() { |
| 86 | let mut s = Script608::new(); |
| 87 | s.roll_up(2) |
| 88 | .pac(15, 0, false) |
| 89 | .text("ONE") |
| 90 | .idle(2) |
| 91 | .cr() |
| 92 | .text("TWO") |
| 93 | .idle(2) |
| 94 | .cr() |
| 95 | .text("THREE") |
| 96 | .idle(2) |
| 97 | .edm(); |
| 98 | let cues = run(&s, 0); |
| 99 | let texts: Vec<String> = cues.iter().map(|c| plain_text(&c.cue.segments)).collect(); |
| 100 | assert_eq!(texts, vec!["ONE", "ONE\nTWO", "TWO\nTHREE"]); |
| 101 | assert_eq!(cues[0].cue.end_us, cues[1].cue.start_us); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn paint_on_and_channel_two() { |
| 106 | let mut s = Script608::new(); |
| 107 | s.rdc().pac(10, 0, false).text("PAINTED").idle(3).edm(); |
| 108 | let c = run(&s, 0); |
| 109 | assert_eq!(plain_text(&c[0].cue.segments), "PAINTED"); |
| 110 | // Channel 2 (CC2): the channel bit lives in the first byte of every control. |
| 111 | let mut s2 = Script608::new(); |
| 112 | s2.ctrl(0x1C, 0x20) |
| 113 | .ctrl(0x1C, 0x2E) |
| 114 | .ctrl(0x1C, 0x60) |
| 115 | .text("SECOND") |
| 116 | .ctrl(0x1C, 0x2F) |
| 117 | .idle(2) |
| 118 | .ctrl(0x1C, 0x2C); |
| 119 | let c2 = run(&s2, 0); |
| 120 | assert_eq!( |
| 121 | (c2[0].channel, plain_text(&c2[0].cue.segments)), |
| 122 | (2, "SECOND".to_string()) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn text_mode_is_not_captioned() { |
| 128 | let mut s = Script608::new(); |
| 129 | s.ctrl(0x14, 0x2A).text("TEXT MODE DATA").ctrl(0x14, 0x2C); |
| 130 | assert!(run(&s, 0).is_empty()); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn field_two_is_cc3() { |
| 135 | let mut s = Script608::new(); |
| 136 | s.rcl() |
| 137 | .enm() |
| 138 | .pac(15, 0, false) |
| 139 | .text("F2") |
| 140 | .eoc() |
| 141 | .idle(2) |
| 142 | .edm(); |
| 143 | let c = run(&s, 1); |
| 144 | assert_eq!(c[0].channel, 3); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn cc_data_and_wrappers() { |
| 149 | let t = [CcTriplet { |
| 150 | valid: true, |
| 151 | cc_type: 0, |
| 152 | data: [0x94, 0x2C], |
| 153 | }]; |
| 154 | let cc = cc_data_bytes_for_test(&t); |
| 155 | assert_eq!(parse_cc_data(&cc).unwrap(), t); |
| 156 | let mut ud = b"GA94\x03".to_vec(); |
| 157 | ud.extend(&cc); |
| 158 | assert_eq!(parse_mpeg2_user_data(&ud).unwrap(), t); |
| 159 | // DVD form |
| 160 | let dvd = [b'C', b'C', 0x01, 0xF8, 0xC2, 0xFF, 0x94, 0x2C]; |
| 161 | assert_eq!(parse_mpeg2_user_data(&dvd).unwrap(), t); |
| 162 | // c608 sample: cdat + cdt2 |
| 163 | let mut sample = Vec::new(); |
| 164 | sample.extend_from_slice(&10u32.to_be_bytes()); |
| 165 | sample.extend_from_slice(b"cdat\x94\x2c"); |
| 166 | sample.extend_from_slice(&10u32.to_be_bytes()); |
| 167 | sample.extend_from_slice(b"cdt2\x41\x42"); |
| 168 | let tr = parse_c608_sample(&sample); |
| 169 | assert_eq!(tr.len(), 2); |
| 170 | assert_eq!((tr[1].cc_type, tr[1].data), (1, [0x41, 0x42])); |
| 171 | // CDP |
| 172 | let mut cdp = vec![ |
| 173 | 0x96, 0x69, 0, 0x4F, 0x43, 0, 0, 0x72, 0xE1, 0xFC, 0x94, 0x2C, 0x74, 0, 0, 0, |
| 174 | ]; |
| 175 | cdp[2] = cdp.len() as u8; |
| 176 | assert_eq!(parse_cdp(&cdp).unwrap(), t); |
| 177 | let mut s708 = Vec::new(); |
| 178 | s708.extend_from_slice(&(8 + cdp.len() as u32).to_be_bytes()); |
| 179 | s708.extend_from_slice(b"ccdp"); |
| 180 | s708.extend(&cdp); |
| 181 | assert_eq!(parse_c708_sample(&s708), t); |
| 182 | } |
| 183 | |
| 184 | fn cc_data_bytes_for_test(t: &[CcTriplet]) -> Vec<u8> { |
| 185 | ec_cc::synth::cc_data_bytes(t) |
| 186 | } |
| 187 | |
| 188 | fn dtvcc_packet(seq: u8, svc_blocks: &[u8]) -> Vec<(u8, u8, u8)> { |
| 189 | // Header byte then data; total bytes padded to even. |
| 190 | let mut data = svc_blocks.to_vec(); |
| 191 | if !(data.len() + 1).is_multiple_of(2) { |
| 192 | data.push(0); |
| 193 | } |
| 194 | let size_code = data.len().div_ceil(2); |
| 195 | let mut all = vec![(seq << 6) | size_code as u8]; |
| 196 | all.extend(data); |
| 197 | let mut out = Vec::new(); |
| 198 | for (k, ch) in all.chunks(2).enumerate() { |
| 199 | out.push((if k == 0 { 3 } else { 2 }, ch[0], ch[1])); |
| 200 | } |
| 201 | out |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn cea708_service_one_window_text() { |
| 206 | let mut blk = vec![0x98, 0x20, 0x4A, 0x10, 0x0A, 0x1F, 0x00]; // DF0 visible, row_count 11? rows=(0x0A&15)+1 |
| 207 | blk.extend_from_slice(b"HI THERE"); |
| 208 | blk.push(0x03); |
| 209 | let mut block = vec![(1 << 5) | blk.len() as u8]; |
| 210 | block.extend(&blk); |
| 211 | let mut d = Cea708Decoder::new(); |
| 212 | for (ty, a, b) in dtvcc_packet(0, &block) { |
| 213 | d.push_triplet(1000, ty, a, b); |
| 214 | } |
| 215 | // service 2 block must be refused by name |
| 216 | let blk2 = [0x20, b'X']; |
| 217 | let mut b2 = vec![(2 << 5) | blk2.len() as u8]; |
| 218 | b2.extend(blk2); |
| 219 | for (ty, a, b) in dtvcc_packet(1, &b2) { |
| 220 | d.push_triplet(2000, ty, a, b); |
| 221 | } |
| 222 | // hide window 0 |
| 223 | let mut hide = vec![(1 << 5) | 2u8, 0x8A, 0x01]; |
| 224 | hide.push(0); |
| 225 | for (ty, a, b) in dtvcc_packet(2, &hide) { |
| 226 | d.push_triplet(5000, ty, a, b); |
| 227 | } |
| 228 | assert_eq!(d.unsupported_services(), vec![2]); |
| 229 | let cues = d.finish(6000); |
| 230 | assert_eq!(cues.len(), 1); |
| 231 | assert_eq!(plain_text(&cues[0].cue.segments), "HI THERE"); |
| 232 | assert_eq!((cues[0].cue.start_us, cues[0].cue.end_us), (1000, 5000)); |
| 233 | } |