Skip to content

Commit f9464e6

Browse files
committed
Rewriting Dtvcc::new() to instantiate
1 parent 0264e7d commit f9464e6

File tree

5 files changed

+64
-34
lines changed

5 files changed

+64
-34
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ windows/.vs/**
4444
*.opendb
4545
*.db
4646
*.vscode
47+
.cache
4748

4849
####
4950
# Ignore the header file that is updated upon build
@@ -153,4 +154,4 @@ src/rust/target/
153154
windows/ccx_rust.lib
154155
windows/*/debug/*
155156
windows/*/CACHEDIR.TAG
156-
windows/.rustc_info.json
157+
windows/.rustc_info.json

src/rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn main() {
5757
}
5858

5959
let bindings = builder
60+
.derive_default(true)
6061
// Finish the builder and generate the bindings.
6162
.generate()
6263
// Unwrap the Result and panic on failure.

src/rust/src/decoder/mod.rs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const CCX_DTVCC_SCREENGRID_ROWS: u8 = 75;
1919
const CCX_DTVCC_SCREENGRID_COLUMNS: u8 = 210;
2020
const CCX_DTVCC_MAX_ROWS: u8 = 15;
2121
const CCX_DTVCC_MAX_COLUMNS: u8 = 32 * 2;
22+
const CCX_DTVCC_MAX_SERVICES: usize = 63;
2223

2324
/// Context required for processing 708 data
2425
pub struct Dtvcc<'a> {
@@ -39,26 +40,60 @@ pub struct Dtvcc<'a> {
3940

4041
impl<'a> Dtvcc<'a> {
4142
/// Create a new dtvcc context
42-
pub fn new(ctx: &'a mut dtvcc_ctx) -> Self {
43-
let report = unsafe { &mut *ctx.report };
44-
let encoder = unsafe { &mut *(ctx.encoder as *mut encoder_ctx) };
45-
let timing = unsafe { &mut *ctx.timing };
43+
pub fn new(opts: &'a mut ccx_decoder_dtvcc_settings) -> Self {
44+
let report = unsafe { &mut *opts.report };
45+
report.reset_count = 0;
46+
47+
let decoders = (0..CCX_DTVCC_MAX_SERVICES)
48+
.map(|i| {
49+
if !is_true(opts.services_enabled[i]) {
50+
return unsafe { &mut *std::ptr::null_mut() };
51+
}
52+
53+
let decoder = Box::leak(Box::new(dtvcc_service_decoder {
54+
tv: Box::leak(Box::new(dtvcc_tv_screen {
55+
cc_count: 0,
56+
service_number: i as i32 + 1,
57+
..dtvcc_tv_screen::default()
58+
})),
59+
..dtvcc_service_decoder::default()
60+
}));
61+
62+
decoder.windows.iter_mut().for_each(|w| {
63+
w.memory_reserved = 0;
64+
});
65+
66+
unsafe { dtvcc_windows_reset(decoder) };
67+
68+
decoder
69+
})
70+
.collect();
71+
72+
for i in 0..CCX_DTVCC_MAX_SERVICES {
73+
if !is_true(opts.services_enabled[i]) {
74+
continue;
75+
}
76+
}
77+
78+
// dtvcc_clear_packet sets current_packet_length, is_current_packet_header_parsed, current_packet to 0
4679

4780
Self {
48-
is_active: is_true(ctx.is_active),
49-
active_services_count: ctx.active_services_count as u8,
50-
services_active: ctx.services_active.to_vec(),
51-
report_enabled: is_true(ctx.report_enabled),
5281
report,
53-
decoders: ctx.decoders.iter_mut().collect(),
54-
packet: ctx.current_packet.to_vec(),
55-
packet_length: ctx.current_packet_length as u8,
56-
is_header_parsed: is_true(ctx.is_current_packet_header_parsed),
57-
last_sequence: ctx.last_sequence,
58-
encoder,
59-
no_rollup: is_true(ctx.no_rollup),
60-
timing,
82+
decoders,
83+
is_active: false,
84+
no_rollup: is_true(opts.no_rollup),
85+
active_services_count: opts.active_services_count as u8,
86+
services_active: opts.services_enabled.to_vec(),
87+
packet_length: 0,
88+
is_header_parsed: false,
89+
packet: vec![0; CCX_DTVCC_MAX_SERVICES],
90+
last_sequence: CCX_DTVCC_NO_LAST_SEQUENCE,
91+
report_enabled: is_true(opts.print_file_reports),
92+
timing: unsafe { &mut *opts.timing },
93+
encoder: unsafe { &mut *std::ptr::null_mut() },
6194
}
95+
96+
// notes: import CCX_DTVCC_MAX_SERVICES, initialize the decoders
6297
}
6398
/// Process cc data and add it to the dtvcc packet
6499
pub fn process_cc_data(&mut self, cc_valid: u8, cc_type: u8, data1: u8, data2: u8) {
@@ -212,10 +247,3 @@ impl dtvcc_symbol {
212247
is_true(self.init)
213248
}
214249
}
215-
216-
impl Default for dtvcc_symbol {
217-
/// Create a blank uninitialized symbol
218-
fn default() -> Self {
219-
Self { sym: 0, init: 0 }
220-
}
221-
}

src/rust/src/decoder/tv_screen.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ impl dtvcc_tv_screen {
148148
use_colors: bool,
149149
) -> Result<(), String> {
150150
let mut buf = Vec::new();
151-
let mut pen_color = dtvcc_pen_color::default();
152-
let mut pen_attribs = dtvcc_pen_attribs::default();
151+
let mut pen_color = dtvcc_pen_color::new();
152+
let mut pen_attribs = dtvcc_pen_attribs::new();
153153
let (first, last) = self.get_write_interval(row_index);
154154
debug!("First: {}, Last: {}", first, last);
155155

@@ -418,7 +418,7 @@ impl dtvcc_tv_screen {
418418
return;
419419
}
420420
let new_pen_attribs = if col_index >= CCX_DTVCC_SCREENGRID_COLUMNS as usize {
421-
dtvcc_pen_attribs::default()
421+
dtvcc_pen_attribs::new()
422422
} else {
423423
self.pen_attribs[row_index][col_index]
424424
};
@@ -455,7 +455,7 @@ impl dtvcc_tv_screen {
455455
return;
456456
}
457457
let new_pen_color = if col_index >= CCX_DTVCC_SCREENGRID_COLUMNS as usize {
458-
dtvcc_pen_color::default()
458+
dtvcc_pen_color::new()
459459
} else {
460460
self.pen_colors[row_index][col_index]
461461
};

src/rust/src/decoder/window.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ impl dtvcc_window {
149149
/// Clear all text from the window
150150
pub fn clear_text(&mut self) {
151151
// Set pen color to default value
152-
self.pen_color_pattern = dtvcc_pen_color::default();
152+
self.pen_color_pattern = dtvcc_pen_color::new();
153153
// Set pen attributes to default value
154-
self.pen_attribs_pattern = dtvcc_pen_attribs::default();
154+
self.pen_attribs_pattern = dtvcc_pen_attribs::new();
155155
for row in 0..CCX_DTVCC_MAX_ROWS as usize {
156156
self.clear_row(row);
157157
}
@@ -521,9 +521,9 @@ enum Opacity {
521521
Transparent = 3,
522522
}
523523

524-
impl Default for dtvcc_pen_color {
524+
impl dtvcc_pen_color {
525525
/// Returns the default pen color
526-
fn default() -> Self {
526+
pub fn new() -> Self {
527527
Self {
528528
fg_color: 0x3F,
529529
fg_opacity: 0,
@@ -534,9 +534,9 @@ impl Default for dtvcc_pen_color {
534534
}
535535
}
536536

537-
impl Default for dtvcc_pen_attribs {
537+
impl dtvcc_pen_attribs {
538538
/// Returns the default pen attributes
539-
fn default() -> Self {
539+
pub fn new() -> Self {
540540
Self {
541541
pen_size: dtvcc_pen_size::DTVCC_PEN_SIZE_STANDART as i32,
542542
offset: 0,

0 commit comments

Comments
 (0)