Skip to content

Commit b842952

Browse files
committed
Modifying lib_cc_decode (C) to use the new Dtvcc (Rust)
1 parent 88c238a commit b842952

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

src/lib_ccx/ccx_decoders_common.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ made to reuse, not duplicate, as many functions as possible */
1818
#ifndef DISABLE_RUST
1919
extern int ccxr_process_cc_data(struct lib_cc_decode *dec_ctx, unsigned char *cc_data, int cc_count);
2020
extern void ccxr_flush_decoder(struct dtvcc_ctx *dtvcc, struct dtvcc_service_decoder *decoder);
21+
extern void *ccxr_dtvcc_init(struct ccx_decoder_dtvcc_settings *decoder_settings);
22+
extern void ccxr_dtvcc_free(void *dtvcc_rust);
2123
#endif
2224

2325
uint64_t utc_refvalue = UINT64_MAX; /* _UI64_MAX/UINT64_MAX means don't use UNIX, 0 = use current system time as reference, +1 use a specific reference */
@@ -232,6 +234,9 @@ int do_cb(struct lib_cc_decode *ctx, unsigned char *cc_block, struct cc_subtitle
232234
void dinit_cc_decode(struct lib_cc_decode **ctx)
233235
{
234236
struct lib_cc_decode *lctx = *ctx;
237+
#ifndef DISABLE_RUST
238+
ccxr_dtvcc_free(lctx->dtvcc_rust);
239+
#endif
235240
dtvcc_free(&lctx->dtvcc);
236241
dinit_avc(&lctx->avc_ctx);
237242
ccx_decoder_608_dinit_library(&lctx->context_cc608_field_1);
@@ -261,6 +266,9 @@ struct lib_cc_decode *init_cc_decode(struct ccx_decoders_common_settings_t *sett
261266
ctx->no_rollup = setting->no_rollup;
262267
ctx->noscte20 = setting->noscte20;
263268

269+
#ifndef DISABLE_RUST
270+
ctx->dtvcc = ccxr_dtvcc_init(setting->settings_dtvcc);
271+
#endif
264272
ctx->dtvcc = dtvcc_init(setting->settings_dtvcc);
265273
ctx->dtvcc->is_active = setting->settings_dtvcc->enabled;
266274

src/lib_ccx/ccx_decoders_structs.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
#define CCX_DECODER_608_SCREEN_ROWS 15
1212
#define CCX_DECODER_608_SCREEN_WIDTH 32
1313
#define MAXBFRAMES 50
14-
#define SORTBUF (2*MAXBFRAMES+1)
15-
14+
#define SORTBUF (2 * MAXBFRAMES + 1)
1615

1716
/* flag raised when end of display marker arrives in Dvb Subtitle */
18-
#define SUB_EOD_MARKER (1 << 0 )
17+
#define SUB_EOD_MARKER (1 << 0)
1918
struct cc_bitmap
2019
{
2120
int x;
@@ -77,22 +76,22 @@ enum ccx_decoder_608_color_code
7776
};
7877

7978
/**
80-
* This structure have fields which need to be ignored according to format,
81-
* for example if format is SFORMAT_XDS then all fields other then
82-
* xds related (xds_str, xds_len and cur_xds_packet_class) should be
83-
* ignored and not to be dereferenced.
84-
*
85-
* TODO use union inside struct for each kind of fields
86-
*/
79+
* This structure have fields which need to be ignored according to format,
80+
* for example if format is SFORMAT_XDS then all fields other then
81+
* xds related (xds_str, xds_len and cur_xds_packet_class) should be
82+
* ignored and not to be dereferenced.
83+
*
84+
* TODO use union inside struct for each kind of fields
85+
*/
8786
struct eia608_screen // A CC buffer
8887
{
8988
/** format of data inside this structure */
9089
enum ccx_eia608_format format;
9190
unsigned char characters[CCX_DECODER_608_SCREEN_ROWS][CCX_DECODER_608_SCREEN_WIDTH + 1];
9291
enum ccx_decoder_608_color_code colors[CCX_DECODER_608_SCREEN_ROWS][CCX_DECODER_608_SCREEN_WIDTH + 1];
9392
enum font_bits fonts[CCX_DECODER_608_SCREEN_ROWS][CCX_DECODER_608_SCREEN_WIDTH + 1]; // Extra char at the end for a 0
94-
int row_used[CCX_DECODER_608_SCREEN_ROWS]; // Any data in row?
95-
int empty; // Buffer completely empty?
93+
int row_used[CCX_DECODER_608_SCREEN_ROWS]; // Any data in row?
94+
int empty; // Buffer completely empty?
9695
/** start time of this CC buffer */
9796
LLONG start_time;
9897
/** end time of this CC buffer */
@@ -110,20 +109,20 @@ struct eia608_screen // A CC buffer
110109

111110
struct ccx_decoders_common_settings_t
112111
{
113-
LLONG subs_delay; // ms to delay (or advance) subs
114-
enum ccx_output_format output_format; // What kind of output format should be used?
115-
int fix_padding; // Replace 0000 with 8080 in HDTV (needed for some cards)
112+
LLONG subs_delay; // ms to delay (or advance) subs
113+
enum ccx_output_format output_format; // What kind of output format should be used?
114+
int fix_padding; // Replace 0000 with 8080 in HDTV (needed for some cards)
116115
struct ccx_boundary_time extraction_start, extraction_end; // Segment we actually process
117116
int cc_to_stdout;
118-
int extract; // Extract 1st, 2nd or both fields
119-
int fullbin; // Disable pruning of padding cc blocks
117+
int extract; // Extract 1st, 2nd or both fields
118+
int fullbin; // Disable pruning of padding cc blocks
120119
int no_rollup;
121120
int noscte20;
122-
struct ccx_decoder_608_settings *settings_608; // Contains the settings for the 608 decoder.
123-
ccx_decoder_dtvcc_settings *settings_dtvcc; // Same for cea 708 captions decoder (dtvcc)
124-
int cc_channel; // Channel we want to dump in srt mode
121+
struct ccx_decoder_608_settings *settings_608; // Contains the settings for the 608 decoder.
122+
ccx_decoder_dtvcc_settings *settings_dtvcc; // Same for cea 708 captions decoder (dtvcc)
123+
int cc_channel; // Channel we want to dump in srt mode
125124
unsigned send_to_srv;
126-
unsigned int hauppauge_mode; // If 1, use PID=1003, process specially and so on
125+
unsigned int hauppauge_mode; // If 1, use PID=1003, process specially and so on
127126
int program_number;
128127
enum ccx_code_type codec;
129128
int xds_write_to_file;
@@ -142,17 +141,17 @@ struct lib_cc_decode
142141
void *context_cc608_field_1;
143142
void *context_cc608_field_2;
144143

145-
int no_rollup; // If 1, write one line at a time
144+
int no_rollup; // If 1, write one line at a time
146145
int noscte20;
147-
int fix_padding; // Replace 0000 with 8080 in HDTV (needed for some cards)
148-
enum ccx_output_format write_format; // 0 = Raw, 1 = srt, 2 = SMI
146+
int fix_padding; // Replace 0000 with 8080 in HDTV (needed for some cards)
147+
enum ccx_output_format write_format; // 0 = Raw, 1 = srt, 2 = SMI
149148
struct ccx_boundary_time extraction_start, extraction_end; // Segment we actually process
150-
LLONG subs_delay; // ms to delay (or advance) subs
151-
int extract; // Extract 1st, 2nd or both fields
152-
int fullbin; // Disable pruning of padding cc blocks
149+
LLONG subs_delay; // ms to delay (or advance) subs
150+
int extract; // Extract 1st, 2nd or both fields
151+
int fullbin; // Disable pruning of padding cc blocks
153152
struct cc_subtitle dec_sub;
154153
enum ccx_bufferdata_type in_bufferdatatype;
155-
unsigned int hauppauge_mode; // If 1, use PID=1003, process specially and so on
154+
unsigned int hauppauge_mode; // If 1, use PID=1003, process specially and so on
156155

157156
int frames_since_last_gop;
158157
/* GOP-based timing */
@@ -185,7 +184,7 @@ struct lib_cc_decode
185184
int in_pic_data;
186185

187186
unsigned int current_progressive_sequence;
188-
unsigned int current_pulldownfields ;
187+
unsigned int current_pulldownfields;
189188

190189
int temporal_reference;
191190
enum ccx_frame_type picture_coding_type;
@@ -197,17 +196,18 @@ struct lib_cc_decode
197196
/* Required in es_function.c and es_userdata.c */
198197
unsigned top_field_first; // Needs to be global
199198

200-
/* Stats. Modified in es_userdata.c*/
201-
int stat_numuserheaders;
202-
int stat_dvdccheaders;
203-
int stat_scte20ccheaders;
204-
int stat_replay5000headers;
205-
int stat_replay4000headers;
206-
int stat_dishheaders;
207-
int stat_hdtv;
208-
int stat_divicom;
209-
int false_pict_header;
210-
199+
/* Stats. Modified in es_userdata.c*/
200+
int stat_numuserheaders;
201+
int stat_dvdccheaders;
202+
int stat_scte20ccheaders;
203+
int stat_replay5000headers;
204+
int stat_replay4000headers;
205+
int stat_dishheaders;
206+
int stat_hdtv;
207+
int stat_divicom;
208+
int false_pict_header;
209+
210+
void *dtvcc_rust;
211211
dtvcc_ctx *dtvcc;
212212
int current_field;
213213
// Analyse/use the picture information
@@ -217,7 +217,7 @@ struct lib_cc_decode
217217
// Store fts;
218218
LLONG cc_fts[SORTBUF];
219219
// Store HD CC packets
220-
unsigned char cc_data_pkts[SORTBUF][10*31*3+1]; // *10, because MP4 seems to have different limits
220+
unsigned char cc_data_pkts[SORTBUF][10 * 31 * 3 + 1]; // *10, because MP4 seems to have different limits
221221

222222
// The sequence number of the current anchor frame. All currently read
223223
// B-Frames belong to this I- or P-frame.
@@ -227,7 +227,7 @@ struct lib_cc_decode
227227

228228
int (*writedata)(const unsigned char *data, int length, void *private_data, struct cc_subtitle *sub);
229229

230-
//dvb subtitle related
230+
// dvb subtitle related
231231
int ocr_quantmode;
232232
struct lib_cc_decode *prev;
233233
};

src/rust/src/decoder/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ impl<'a> Dtvcc<'a> {
7070
// unlike C, here the decoders are allocated on the stack as an array.
7171
let decoders = {
7272
const INIT: Option<dtvcc_service_decoder> = None;
73-
let val = [INIT; CCX_DTVCC_MAX_SERVICES];
73+
let mut val = [INIT; CCX_DTVCC_MAX_SERVICES];
7474

7575
for i in 0..CCX_DTVCC_MAX_SERVICES {
7676
if is_false(opts.services_enabled[i]) {
7777
continue;
7878
}
7979

80-
let decoder = dtvcc_service_decoder {
80+
let mut decoder = dtvcc_service_decoder {
8181
// we cannot allocate this on the stack as `dtvcc_service_decoder` is a C
8282
// struct cannot be changed trivially
8383
tv: Box::into_raw(Box::new(dtvcc_tv_screen {
@@ -228,7 +228,7 @@ impl<'a> Dtvcc<'a> {
228228
let decoder = &mut self.decoders[(service_number - 1) as usize];
229229
decoder.unwrap().process_service_block(
230230
&self.packet[pos as usize..(pos + block_length) as usize],
231-
self.encoder.unwrap(),
231+
self.encoder.as_mut().unwrap(),
232232
self.timing,
233233
self.no_rollup,
234234
);
@@ -256,12 +256,12 @@ impl<'a> Drop for Dtvcc<'a> {
256256
fn drop(&mut self) {
257257
// closely follows `dtvcc_free` at `src/lib_ccx/ccx_dtvcc.c:126`
258258
for i in 0..CCX_DTVCC_MAX_SERVICES {
259-
if let Some(decoder) = self.decoders[i] {
259+
if let Some(mut decoder) = self.decoders[i] {
260260
if !is_true(self.services_active[i]) {
261261
continue;
262262
}
263263

264-
decoder.windows.iter().for_each(|window| {
264+
decoder.windows.iter_mut().for_each(|window| {
265265
if is_false(window.memory_reserved) {
266266
return;
267267
}

src/rust/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,11 @@ extern "C" fn ccxr_process_cc_data(
8686
.map(|x| unsafe { *data.add(x as usize) })
8787
.collect();
8888
let dec_ctx = unsafe { &mut *dec_ctx };
89-
let dtvcc_ctx = unsafe { &mut *dec_ctx.dtvcc };
90-
let mut dtvcc = Dtvcc::new(dtvcc_ctx);
9189
for cc_block in cc_data.chunks_exact_mut(3) {
9290
if !validate_cc_pair(cc_block) {
9391
continue;
9492
}
95-
let success = do_cb(dec_ctx, &mut dtvcc, cc_block);
93+
let success = do_cb(dec_ctx, cc_block);
9694
if success {
9795
ret = 0;
9896
}
@@ -136,7 +134,8 @@ pub fn verify_parity(data: u8) -> bool {
136134
}
137135

138136
/// Process CC data according to its type
139-
pub fn do_cb(ctx: &mut lib_cc_decode, dtvcc: &mut Dtvcc, cc_block: &[u8]) -> bool {
137+
pub fn do_cb(ctx: &mut lib_cc_decode, cc_block: &[u8]) -> bool {
138+
let dtvcc: &mut Dtvcc = unsafe { std::mem::transmute(ctx.dtvcc_rust) };
140139
let cc_valid = (cc_block[0] & 4) >> 2;
141140
let cc_type = cc_block[0] & 3;
142141
let mut timeok = true;

0 commit comments

Comments
 (0)