forked from CCExtractor/ccextractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
308 lines (267 loc) · 10.6 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//! CEA 708 decoder
//!
//! Provides a CEA 708 decoder as defined by ANSI/CTA-708-E R-2018
mod commands;
mod output;
mod service_decoder;
mod timing;
mod tv_screen;
mod window;
use crate::{
bindings::*,
utils::{is_false, is_true},
};
use log::{debug, warn};
const CCX_DTVCC_MAX_PACKET_LENGTH: u8 = 128;
const CCX_DTVCC_NO_LAST_SEQUENCE: i32 = -1;
const CCX_DTVCC_SCREENGRID_ROWS: u8 = 75;
const CCX_DTVCC_SCREENGRID_COLUMNS: u8 = 210;
const CCX_DTVCC_MAX_ROWS: u8 = 15;
const CCX_DTVCC_MAX_COLUMNS: u8 = 32 * 2;
const CCX_DTVCC_MAX_SERVICES: usize = 63;
// const CCX_DTVCC_MAX_WINDOWS: usize = 8;
/// Context required for processing 708 data
pub struct Dtvcc {
pub is_active: bool,
pub active_services_count: u8,
pub services_active: [i32; CCX_DTVCC_MAX_SERVICES],
pub report_enabled: bool,
pub report: *mut ccx_decoder_dtvcc_report,
pub decoders: [Option<Box<dtvcc_service_decoder>>; CCX_DTVCC_MAX_SERVICES],
pub packet: [u8; CCX_DTVCC_MAX_SERVICES],
pub packet_length: u8,
pub is_header_parsed: bool,
pub last_sequence: i32,
pub encoder: *mut encoder_ctx,
pub no_rollup: bool,
pub timing: *mut ccx_common_timing_ctx,
}
impl Dtvcc {
/// Create a new dtvcc context
pub fn new(opts: &ccx_decoder_dtvcc_settings) -> Self {
// closely follows `dtvcc_init` at `src/lib_ccx/ccx_dtvcc.c:76`
let report = opts.report;
unsafe {
(*report).reset_count = 0;
}
let is_active = false;
let no_rollup = is_true(opts.no_rollup);
let active_services_count = opts.active_services_count as u8;
let services_active = opts.services_enabled;
// `dtvcc_clear_packet` does the following
let packet_length = 0;
let is_header_parsed = false;
let packet = [0; CCX_DTVCC_MAX_SERVICES]; // unlike C, packet is allocated on the stack
let last_sequence = CCX_DTVCC_NO_LAST_SEQUENCE;
let report_enabled = is_true(opts.print_file_reports);
let timing = opts.timing;
// unlike C, here the decoders are allocated on the stack as an array.
let decoders = {
const INIT: Option<Box<dtvcc_service_decoder>> = None;
let mut decoders = [INIT; CCX_DTVCC_MAX_SERVICES];
decoders
.iter_mut()
.zip(opts.services_enabled)
.enumerate()
.for_each(|(i, (d, se))| {
if is_false(se) {
return;
}
let mut decoder = Box::new(dtvcc_service_decoder {
// we cannot allocate this on the stack as `dtvcc_service_decoder` is a C
// struct cannot be changed trivially
tv: Box::into_raw(Box::new(dtvcc_tv_screen {
cc_count: 0,
service_number: i as i32 + 1,
..dtvcc_tv_screen::default()
})),
..dtvcc_service_decoder::default()
});
decoder.windows.iter_mut().for_each(|w| {
w.memory_reserved = 0;
});
unsafe { dtvcc_windows_reset(decoder.as_mut()) };
*d = Some(decoder);
});
decoders
};
let encoder = std::ptr::null_mut(); // Unlike C, does not mention `encoder` and is initialised to `null` by default
Self {
report,
is_active,
no_rollup,
active_services_count,
services_active,
packet_length,
is_header_parsed,
packet,
last_sequence,
report_enabled,
timing,
decoders,
encoder,
}
}
/// Process cc data and add it to the dtvcc packet
pub fn process_cc_data(&mut self, cc_valid: u8, cc_type: u8, data1: u8, data2: u8) {
if !self.is_active && !self.report_enabled {
return;
}
match cc_type {
// type 0 and 1 are for CEA 608 data and are handled before calling this function
// valid types for CEA 708 data are only 2 and 3
2 => {
debug!("dtvcc_process_data: DTVCC Channel Packet Data");
if cc_valid == 1 && self.is_header_parsed {
if self.packet_length > 253 {
warn!("dtvcc_process_data: Warning: Legal packet size exceeded (1), data not added.");
} else {
self.add_data_to_packet(data1, data2);
let mut max_len = self.packet[0] & 0x3F;
if max_len == 0 {
// This is well defined in EIA-708; no magic.
max_len = 128;
} else {
max_len *= 2;
}
// If packet is complete then process the packet
if self.packet_length >= max_len {
self.process_current_packet(max_len);
}
}
}
}
3 => {
debug!("dtvcc_process_data: DTVCC Channel Packet Start");
if cc_valid == 1 {
if self.packet_length > (CCX_DTVCC_MAX_PACKET_LENGTH - 1) {
warn!("dtvcc_process_data: Warning: Legal packet size exceeded (2), data not added.");
} else {
if self.is_header_parsed {
warn!("dtvcc_process_data: Warning: Incorrect packet length specified. Packet will be skipped.");
self.clear_packet();
}
self.add_data_to_packet(data1, data2);
self.is_header_parsed = true;
}
}
}
_ => warn!(
"dtvcc_process_data: shouldn't be here - cc_type: {}",
cc_type
),
}
}
/// Add data to the packet
pub fn add_data_to_packet(&mut self, data1: u8, data2: u8) {
self.packet[self.packet_length as usize] = data1;
self.packet_length += 1;
self.packet[self.packet_length as usize] = data2;
self.packet_length += 1;
}
/// Process current packet into service blocks
pub fn process_current_packet(&mut self, len: u8) {
let seq = (self.packet[0] & 0xC0) >> 6;
debug!(
"dtvcc_process_current_packet: Sequence: {}, packet length: {}",
seq, len
);
if self.packet_length == 0 {
return;
}
// Check if current sequence is correct
// Sequence number is a 2 bit rolling sequence from (0-3)
if self.last_sequence != CCX_DTVCC_NO_LAST_SEQUENCE
&& (self.last_sequence + 1) % 4 != seq as i32
{
warn!("dtvcc_process_current_packet: Unexpected sequence number, it is {} but should be {}", seq, (self.last_sequence +1) % 4);
}
self.last_sequence = seq as i32;
let mut pos: u8 = 1;
while pos < len {
let mut service_number = (self.packet[pos as usize] & 0xE0) >> 5; // 3 more significant bits
let block_length = self.packet[pos as usize] & 0x1F; // 5 less significant bits
debug!("dtvcc_process_current_packet: Standard header Service number: {}, Block length: {}", service_number, block_length);
if service_number == 7 {
// There is an extended header
// CEA-708-E 6.2.2 Extended Service Block Header
pos += 1;
service_number = self.packet[pos as usize] & 0x3F; // 6 more significant bits
if service_number > 7 {
warn!("dtvcc_process_current_packet: Illegal service number in extended header: {}", service_number);
}
}
pos += 1;
if service_number == 0 && block_length != 0 {
// Illegal, but specs say what to do...
pos = len; // Move to end
break;
}
if block_length != 0 {
unsafe { (*self.report).services[service_number as usize] = 1 };
}
if service_number > 0 && is_true(self.services_active[(service_number - 1) as usize]) {
let decoder = &mut self.decoders[(service_number - 1) as usize];
decoder.as_mut().unwrap().process_service_block(
&self.packet[pos as usize..(pos + block_length) as usize],
unsafe { self.encoder.as_mut().unwrap() },
unsafe { self.timing.as_mut().unwrap() },
self.no_rollup,
);
}
pos += block_length // Skip data
}
self.clear_packet();
if len < 128 && self.packet[pos as usize] != 0 {
// Null header is mandatory if there is room
warn!("dtvcc_process_current_packet: Warning: Null header expected but not found.");
}
}
/// Clear current packet
pub fn clear_packet(&mut self) {
self.packet_length = 0;
self.is_header_parsed = false;
self.packet.iter_mut().for_each(|x| *x = 0);
}
}
impl Drop for Dtvcc {
fn drop(&mut self) {
// closely follows `dtvcc_free` at `src/lib_ccx/ccx_dtvcc.c:126`
for i in 0..CCX_DTVCC_MAX_SERVICES {
if let Some(decoder) = self.decoders[i].as_mut() {
if !is_true(self.services_active[i]) {
continue;
}
decoder.windows.iter_mut().for_each(|window| {
if is_false(window.memory_reserved) {
return;
}
window.rows.iter().for_each(|symbol_ptr| unsafe {
symbol_ptr.drop_in_place();
});
window.memory_reserved = 0;
});
unsafe { decoder.tv.drop_in_place() };
}
}
}
}
/// A single character symbol
///
/// sym stores the symbol
/// init is used to know if the symbol is initialized
impl dtvcc_symbol {
/// Create a new symbol
pub fn new(sym: u16) -> Self {
Self { init: 1, sym }
}
/// Create a new 16 bit symbol
pub fn new_16(data1: u8, data2: u8) -> Self {
let sym = (data1 as u16) << 8 | data2 as u16;
Self { init: 1, sym }
}
/// Check if symbol is initialized
pub fn is_set(&self) -> bool {
is_true(self.init)
}
}