Skip to content

Commit b23866f

Browse files
authored
feat(rust): Add persistent DtvccRust context for CEA-708 decoder
2 parents f2aeef1 + 2ec93c3 commit b23866f

File tree

12 files changed

+779
-30
lines changed

12 files changed

+779
-30
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ bazel*
145145
#Intellij IDEs
146146
.idea/
147147

148+
# Plans (local only)
149+
plans/
150+
148151
# Rust build and MakeFiles (and CMake files)
149152
src/rust/CMakeFiles/
150153
src/rust/CMakeCache.txt

src/lib_ccx/ccx_decoders_common.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,12 @@ int do_cb(struct lib_cc_decode *ctx, unsigned char *cc_block, struct cc_subtitle
224224
void dinit_cc_decode(struct lib_cc_decode **ctx)
225225
{
226226
struct lib_cc_decode *lctx = *ctx;
227+
#ifndef DISABLE_RUST
228+
ccxr_dtvcc_free(lctx->dtvcc_rust);
229+
lctx->dtvcc_rust = NULL;
230+
#else
227231
dtvcc_free(&lctx->dtvcc);
232+
#endif
228233
dinit_avc(&lctx->avc_ctx);
229234
ccx_decoder_608_dinit_library(&lctx->context_cc608_field_1);
230235
ccx_decoder_608_dinit_library(&lctx->context_cc608_field_2);
@@ -294,10 +299,16 @@ struct lib_cc_decode *init_cc_decode(struct ccx_decoders_common_settings_t *sett
294299
ctx->no_rollup = setting->no_rollup;
295300
ctx->noscte20 = setting->noscte20;
296301

302+
#ifndef DISABLE_RUST
303+
ctx->dtvcc_rust = ccxr_dtvcc_init(setting->settings_dtvcc);
304+
ctx->dtvcc = NULL; // Not used when Rust is enabled
305+
#else
297306
ctx->dtvcc = dtvcc_init(setting->settings_dtvcc);
298307
if (!ctx->dtvcc)
299308
fatal(EXIT_NOT_ENOUGH_MEMORY, "In init_cc_decode: Out of memory initializing dtvcc.");
300309
ctx->dtvcc->is_active = setting->settings_dtvcc->enabled;
310+
ctx->dtvcc_rust = NULL;
311+
#endif
301312

302313
if (setting->codec == CCX_CODEC_ATSC_CC)
303314
{
@@ -477,6 +488,13 @@ void flush_cc_decode(struct lib_cc_decode *ctx, struct cc_subtitle *sub)
477488
}
478489
}
479490
}
491+
#ifndef DISABLE_RUST
492+
if (ccxr_dtvcc_is_active(ctx->dtvcc_rust))
493+
{
494+
ctx->current_field = 3;
495+
ccxr_flush_active_decoders(ctx->dtvcc_rust);
496+
}
497+
#else
480498
if (ctx->dtvcc->is_active)
481499
{
482500
for (int i = 0; i < CCX_DTVCC_MAX_SERVICES; i++)
@@ -491,6 +509,7 @@ void flush_cc_decode(struct lib_cc_decode *ctx, struct cc_subtitle *sub)
491509
}
492510
}
493511
}
512+
#endif
494513
}
495514
struct encoder_ctx *copy_encoder_context(struct encoder_ctx *ctx)
496515
{

src/lib_ccx/ccx_decoders_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ struct cc_subtitle *copy_subtitle(struct cc_subtitle *sub);
3232
void free_encoder_context(struct encoder_ctx *ctx);
3333
void free_decoder_context(struct lib_cc_decode *ctx);
3434
void free_subtitle(struct cc_subtitle *sub);
35+
36+
#ifndef DISABLE_RUST
37+
// Rust FFI function to flush active CEA-708 service decoders
38+
extern void ccxr_flush_active_decoders(void *dtvcc_rust);
39+
#endif
40+
3541
#endif

src/lib_ccx/ccx_decoders_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ struct lib_cc_decode
208208
int false_pict_header;
209209

210210
dtvcc_ctx *dtvcc;
211+
void *dtvcc_rust; // Persistent Rust CEA-708 decoder context
211212
int current_field;
212213
// Analyse/use the picture information
213214
int maxtref; // Use to remember the temporal reference number

src/lib_ccx/ccx_dtvcc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ void dtvcc_process_data(struct dtvcc_ctx *dtvcc,
1010
dtvcc_ctx *dtvcc_init(ccx_decoder_dtvcc_settings *opts);
1111
void dtvcc_free(dtvcc_ctx **);
1212

13+
#ifndef DISABLE_RUST
14+
// Rust FFI functions for persistent CEA-708 decoder
15+
extern void *ccxr_dtvcc_init(struct ccx_decoder_dtvcc_settings *settings_dtvcc);
16+
extern void ccxr_dtvcc_free(void *dtvcc_rust);
17+
extern void ccxr_dtvcc_process_data(void *dtvcc_rust, const unsigned char cc_valid,
18+
const unsigned char cc_type, const unsigned char data1, const unsigned char data2);
19+
extern int ccxr_dtvcc_is_active(void *dtvcc_rust);
20+
#endif
21+
1322
#endif // CCEXTRACTOR_CCX_DTVCC_H

src/lib_ccx/general_loop.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,11 @@ int process_non_multiprogram_general_loop(struct lib_ccx_ctx *ctx,
10501050
cinfo = get_cinfo(ctx->demux_ctx, pid);
10511051
*enc_ctx = update_encoder_list_cinfo(ctx, cinfo);
10521052
*dec_ctx = update_decoder_list_cinfo(ctx, cinfo);
1053+
#ifndef DISABLE_RUST
1054+
ccxr_dtvcc_set_encoder((*dec_ctx)->dtvcc_rust, *enc_ctx);
1055+
#else
10531056
(*dec_ctx)->dtvcc->encoder = (void *)(*enc_ctx);
1057+
#endif
10541058

10551059
if ((*dec_ctx)->timing->min_pts == 0x01FFFFFFFFLL) // if we didn't set the min_pts of the program
10561060
{
@@ -1274,7 +1278,11 @@ int general_loop(struct lib_ccx_ctx *ctx)
12741278

12751279
enc_ctx = update_encoder_list_cinfo(ctx, cinfo);
12761280
dec_ctx = update_decoder_list_cinfo(ctx, cinfo);
1281+
#ifndef DISABLE_RUST
1282+
ccxr_dtvcc_set_encoder(dec_ctx->dtvcc_rust, enc_ctx);
1283+
#else
12771284
dec_ctx->dtvcc->encoder = (void *)enc_ctx; // WARN: otherwise cea-708 will not work
1285+
#endif
12781286

12791287
if (dec_ctx->timing->min_pts == 0x01FFFFFFFFLL) // if we didn't set the min_pts of the program
12801288
{
@@ -1484,7 +1492,11 @@ int rcwt_loop(struct lib_ccx_ctx *ctx)
14841492
}
14851493

14861494
dec_ctx = update_decoder_list(ctx);
1495+
#ifndef DISABLE_RUST
1496+
ccxr_dtvcc_set_encoder(dec_ctx->dtvcc_rust, enc_ctx);
1497+
#else
14871498
dec_ctx->dtvcc->encoder = (void *)enc_ctx; // WARN: otherwise cea-708 will not work
1499+
#endif
14881500
if (parsebuf[6] == 0 && parsebuf[7] == 2)
14891501
{
14901502
dec_ctx->codec = CCX_CODEC_TELETEXT;

src/lib_ccx/lib_ccx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,9 @@ int process_non_multiprogram_general_loop(struct lib_ccx_ctx *ctx,
341341
void segment_output_file(struct lib_ccx_ctx *ctx, struct lib_cc_decode *dec_ctx);
342342
int decode_vbi(struct lib_cc_decode *dec_ctx, uint8_t field, unsigned char *buffer, size_t len, struct cc_subtitle *sub);
343343

344+
#ifndef DISABLE_RUST
345+
// Rust FFI function to set encoder on persistent CEA-708 decoder
346+
void ccxr_dtvcc_set_encoder(void *dtvcc_rust, struct encoder_ctx *encoder);
347+
#endif
348+
344349
#endif

src/lib_ccx/mp4.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,11 @@ static int process_clcp(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx,
749749
dbg_print(CCX_DMT_PARSE, "MP4-708: atom skipped (cc_type < 2)\n");
750750
continue;
751751
}
752+
#ifndef DISABLE_RUST
753+
ccxr_dtvcc_process_data(dec_ctx->dtvcc_rust, cc_valid, cc_type, temp[2], temp[3]);
754+
#else
752755
dtvcc_process_data(dec_ctx->dtvcc, (unsigned char *)temp);
756+
#endif
753757
cb_708++;
754758
}
755759
if (ctx->write_format == CCX_OF_MCC)
@@ -887,8 +891,12 @@ int processmp4(struct lib_ccx_ctx *ctx, struct ccx_s_mp4Cfg *cfg, char *file)
887891
if (enc_ctx)
888892
enc_ctx->timing = dec_ctx->timing;
889893

890-
// WARN: otherwise cea-708 will not work
894+
// WARN: otherwise cea-708 will not work
895+
#ifndef DISABLE_RUST
896+
ccxr_dtvcc_set_encoder(dec_ctx->dtvcc_rust, enc_ctx);
897+
#else
891898
dec_ctx->dtvcc->encoder = (void *)enc_ctx;
899+
#endif
892900

893901
memset(&dec_sub, 0, sizeof(dec_sub));
894902
mprint("Opening \'%s\': ", file);

src/rust/Cargo.lock

Lines changed: 1 addition & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/src/avc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ pub fn hex_dump(data: &[u8]) {
455455

456456
// Print hex bytes
457457
for byte in chunk {
458-
print!("{:02X} ", byte);
458+
print!("{byte:02X} ");
459459
}
460460

461461
// Pad if less than 16 bytes

0 commit comments

Comments
 (0)