@@ -45,19 +45,41 @@ pub struct Dtvcc<'a> {
45
45
impl < ' a > Dtvcc < ' a > {
46
46
/// Create a new dtvcc context
47
47
pub fn new < ' b > ( opts : & ' b mut ccx_decoder_dtvcc_settings ) -> Self {
48
- let report = unsafe { & mut * opts. report } ;
48
+ // closely follows `dtvcc_init` at `src/lib_ccx/ccx_dtvcc.c:76`
49
+
50
+ let report = unsafe { opts. report . as_mut ( ) . unwrap ( ) } ;
49
51
report. reset_count = 0 ;
50
52
53
+ let is_active = false ;
54
+ let no_rollup = opts. no_rollup ;
55
+ let active_services_count = opts. active_services_count ;
56
+ let no_rollup = is_true ( opts. no_rollup ) ;
57
+ let active_services_count = opts. active_services_count as u8 ;
58
+ let services_active = opts. services_enabled . clone ( ) ;
59
+
60
+ // `dtvcc_clear_packet` does the following
61
+ let packet_length = 0 ;
62
+ let is_header_parsed = false ;
63
+ let packet = [ 0 ; CCX_DTVCC_MAX_SERVICES ] ; // unlike C, packet is allocated on the stack
64
+
65
+ let last_sequence = CCX_DTVCC_NO_LAST_SEQUENCE ;
66
+
67
+ let report_enabled = is_true ( opts. print_file_reports ) ;
68
+ let timing = unsafe { opts. timing . as_mut ( ) } . unwrap ( ) ;
69
+
70
+ // unlike C, here the decoders are allocated on the stack as an array.
51
71
let decoders = {
52
72
const INIT : Option < dtvcc_service_decoder > = None ;
53
73
let val = [ INIT ; CCX_DTVCC_MAX_SERVICES ] ;
54
74
55
75
for i in 0 ..CCX_DTVCC_MAX_SERVICES {
56
- if ! is_true ( opts. services_enabled [ i] ) {
76
+ if is_false ( opts. services_enabled [ i] ) {
57
77
continue ;
58
78
}
59
79
60
80
let decoder = dtvcc_service_decoder {
81
+ // we cannot allocate this on the stack as `dtvcc_service_decoder` is a C
82
+ // struct cannot be changed trivially
61
83
tv : Box :: into_raw ( Box :: new ( dtvcc_tv_screen {
62
84
cc_count : 0 ,
63
85
service_number : i as i32 + 1 ,
@@ -78,28 +100,22 @@ impl<'a> Dtvcc<'a> {
78
100
val
79
101
} ;
80
102
81
- for i in 0 ..CCX_DTVCC_MAX_SERVICES {
82
- if !is_true ( opts. services_enabled [ i] ) {
83
- continue ;
84
- }
85
- }
86
-
87
- // dtvcc_clear_packet sets current_packet_length, is_current_packet_header_parsed, current_packet to 0
103
+ let encoder = None ; // Unlike C, does not mention `encoder` and is initialised to `null` by default
88
104
89
105
Self {
90
106
report,
107
+ is_active,
108
+ no_rollup,
109
+ active_services_count,
110
+ services_active,
111
+ packet_length,
112
+ is_header_parsed,
113
+ packet,
114
+ last_sequence,
115
+ report_enabled,
116
+ timing,
91
117
decoders,
92
- is_active : false ,
93
- no_rollup : is_true ( opts. no_rollup ) ,
94
- active_services_count : opts. active_services_count as u8 ,
95
- services_active : opts. services_enabled . clone ( ) ,
96
- packet_length : 0 ,
97
- is_header_parsed : false ,
98
- packet : [ 0 ; CCX_DTVCC_MAX_SERVICES ] ,
99
- last_sequence : CCX_DTVCC_NO_LAST_SEQUENCE ,
100
- report_enabled : is_true ( opts. print_file_reports ) ,
101
- timing : unsafe { & mut * opts. timing } ,
102
- encoder : None ,
118
+ encoder,
103
119
}
104
120
}
105
121
@@ -238,30 +254,28 @@ impl<'a> Dtvcc<'a> {
238
254
239
255
impl < ' a > Drop for Dtvcc < ' a > {
240
256
fn drop ( & mut self ) {
257
+ // closely follows `dtvcc_free` at `src/lib_ccx/ccx_dtvcc.c:126`
241
258
for i in 0 ..CCX_DTVCC_MAX_SERVICES {
242
- match self . decoders [ i] {
243
- Some ( decoder) => {
244
- if !is_true ( self . services_active [ i] ) {
245
- continue ;
246
- }
247
-
248
- decoder. windows . iter ( ) . for_each ( |window| {
249
- if is_false ( window. memory_reserved ) {
250
- return ;
251
- }
259
+ if let Some ( decoder) = self . decoders [ i] {
260
+ if !is_true ( self . services_active [ i] ) {
261
+ continue ;
262
+ }
252
263
253
- window. rows . iter ( ) . for_each ( |symbol_ptr| unsafe {
254
- symbol_ptr. drop_in_place ( ) ;
255
- } ) ;
264
+ decoder. windows . iter ( ) . for_each ( |window| {
265
+ if is_false ( window. memory_reserved ) {
266
+ return ;
267
+ }
256
268
257
- window. memory_reserved = 0 ;
269
+ window. rows . iter ( ) . for_each ( |symbol_ptr| unsafe {
270
+ symbol_ptr. drop_in_place ( ) ;
258
271
} ) ;
259
272
260
- unsafe {
261
- decoder. tv . drop_in_place ( ) ;
262
- }
273
+ window. memory_reserved = 0 ;
274
+ } ) ;
275
+
276
+ unsafe {
277
+ decoder. tv . drop_in_place ( ) ;
263
278
}
264
- None => { }
265
279
}
266
280
}
267
281
}
0 commit comments