[PR #1782] feat(rust): Add persistent DtvccRust context for CEA-708 decoder (Pha… #2510

Open
opened 2026-01-29 17:22:31 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/CCExtractor/ccextractor/pull/1782

State: closed
Merged: Yes


Fix CEA-708 Decoder State Persistence (Issue #1499)

This PR fully implements the fix for issue #1499. The Rust CEA-708 decoder was creating a new Dtvcc struct on every call to ccxr_process_cc_data(), causing all state to be reset and breaking stateful caption processing.

Based on #1618, which doesn't merge cleanly and contains implementations that differ from current code.

Changes by Phase

Phase 1: Rust Core

  • Added DtvccRust struct in decoder/mod.rs that owns its decoder state
  • Added CCX_DTVCC_MAX_SERVICES constant (63)
  • Added FFI functions in lib.rs:
    • ccxr_dtvcc_init(): Create persistent context
    • ccxr_dtvcc_free(): Free context and all owned memory
    • ccxr_dtvcc_set_encoder(): Set encoder (not available at init)
    • ccxr_dtvcc_process_data(): Process CC data on persistent context
    • ccxr_flush_active_decoders(): Flush all active decoders
    • ccxr_dtvcc_is_active(): Check if context is active
  • Used heap allocation for large structs to avoid stack overflow
  • Added unit tests for DtvccRust

Phase 2: C Headers

  • Added void *dtvcc_rust field to lib_cc_decode struct in ccx_decoders_structs.h
  • Added extern declarations in ccx_dtvcc.h for init/free/process_data/is_active
  • Added extern declaration in lib_ccx.h for ccxr_dtvcc_set_encoder
  • Added extern declaration in ccx_decoders_common.h for ccxr_flush_active_decoders

Phase 3: C Implementation

  • Modified ccx_decoders_common.c:
    • init_cc_decode(): Use ccxr_dtvcc_init() when Rust enabled
    • dinit_cc_decode(): Use ccxr_dtvcc_free() when Rust enabled
    • flush_cc_decode(): Use ccxr_flush_active_decoders() when Rust enabled
  • Modified general_loop.c: Set encoder via ccxr_dtvcc_set_encoder() at 3 locations
  • Modified mp4.c: Use ccxr_dtvcc_set_encoder() and ccxr_dtvcc_process_data()
  • All changes guarded with #ifndef DISABLE_RUST

Phase 4: Bug Fix & Testing

  • Fixed ccxr_process_cc_data() to use persistent DtvccRust from dec_ctx.dtvcc_rust instead of creating new Dtvcc from dec_ctx.dtvcc (which is NULL when Rust enabled)
  • Added do_cb_dtvcc_rust() function for processing with DtvccRust

Testing

Automated Tests

  • All 269 Rust unit tests pass
  • Cargo clippy passes with no errors
  • CI builds pass on Linux, Mac, Windows

Manual Testing

  • Tested with CEA-708 transport stream file (ANDE.ts):
    • ~10 minute file with 25,598 DTVCC packets
    • Successfully extracted 21KB of captions
    • No crashes, proper SRT output with timestamps
  • Tested with CEA-708 program stream file:
    • Processed without crashes
    • Verified state persistence across calls

Files Modified

File Changes
src/rust/src/decoder/mod.rs Added DtvccRust struct and methods
src/rust/src/lib.rs Added FFI functions, fixed ccxr_process_cc_data
src/lib_ccx/ccx_decoders_structs.h Added dtvcc_rust field
src/lib_ccx/ccx_dtvcc.h Added extern declarations
src/lib_ccx/lib_ccx.h Added ccxr_dtvcc_set_encoder declaration
src/lib_ccx/ccx_decoders_common.h Added ccxr_flush_active_decoders declaration
src/lib_ccx/ccx_decoders_common.c Use Rust init/free/flush
src/lib_ccx/general_loop.c Set encoder via Rust FFI
src/lib_ccx/mp4.c Use Rust FFI for encoder and processing

Fixes: #1499

**Original Pull Request:** https://github.com/CCExtractor/ccextractor/pull/1782 **State:** closed **Merged:** Yes --- ## Fix CEA-708 Decoder State Persistence (Issue #1499) This PR fully implements the fix for issue #1499. The Rust CEA-708 decoder was creating a new `Dtvcc` struct on every call to `ccxr_process_cc_data()`, causing all state to be reset and breaking stateful caption processing. Based on #1618, which doesn't merge cleanly and contains implementations that differ from current code. ## Changes by Phase ### Phase 1: Rust Core - Added `DtvccRust` struct in `decoder/mod.rs` that owns its decoder state - Added `CCX_DTVCC_MAX_SERVICES` constant (63) - Added FFI functions in `lib.rs`: - `ccxr_dtvcc_init()`: Create persistent context - `ccxr_dtvcc_free()`: Free context and all owned memory - `ccxr_dtvcc_set_encoder()`: Set encoder (not available at init) - `ccxr_dtvcc_process_data()`: Process CC data on persistent context - `ccxr_flush_active_decoders()`: Flush all active decoders - `ccxr_dtvcc_is_active()`: Check if context is active - Used heap allocation for large structs to avoid stack overflow - Added unit tests for `DtvccRust` ### Phase 2: C Headers - Added `void *dtvcc_rust` field to `lib_cc_decode` struct in `ccx_decoders_structs.h` - Added extern declarations in `ccx_dtvcc.h` for init/free/process_data/is_active - Added extern declaration in `lib_ccx.h` for `ccxr_dtvcc_set_encoder` - Added extern declaration in `ccx_decoders_common.h` for `ccxr_flush_active_decoders` ### Phase 3: C Implementation - Modified `ccx_decoders_common.c`: - `init_cc_decode()`: Use `ccxr_dtvcc_init()` when Rust enabled - `dinit_cc_decode()`: Use `ccxr_dtvcc_free()` when Rust enabled - `flush_cc_decode()`: Use `ccxr_flush_active_decoders()` when Rust enabled - Modified `general_loop.c`: Set encoder via `ccxr_dtvcc_set_encoder()` at 3 locations - Modified `mp4.c`: Use `ccxr_dtvcc_set_encoder()` and `ccxr_dtvcc_process_data()` - All changes guarded with `#ifndef DISABLE_RUST` ### Phase 4: Bug Fix & Testing - Fixed `ccxr_process_cc_data()` to use persistent `DtvccRust` from `dec_ctx.dtvcc_rust` instead of creating new `Dtvcc` from `dec_ctx.dtvcc` (which is NULL when Rust enabled) - Added `do_cb_dtvcc_rust()` function for processing with `DtvccRust` ## Testing ### Automated Tests - All 269 Rust unit tests pass - Cargo clippy passes with no errors - CI builds pass on Linux, Mac, Windows ### Manual Testing - Tested with CEA-708 transport stream file (ANDE.ts): - ~10 minute file with 25,598 DTVCC packets - Successfully extracted 21KB of captions - No crashes, proper SRT output with timestamps - Tested with CEA-708 program stream file: - Processed without crashes - Verified state persistence across calls ## Files Modified | File | Changes | |------|---------| | `src/rust/src/decoder/mod.rs` | Added `DtvccRust` struct and methods | | `src/rust/src/lib.rs` | Added FFI functions, fixed `ccxr_process_cc_data` | | `src/lib_ccx/ccx_decoders_structs.h` | Added `dtvcc_rust` field | | `src/lib_ccx/ccx_dtvcc.h` | Added extern declarations | | `src/lib_ccx/lib_ccx.h` | Added `ccxr_dtvcc_set_encoder` declaration | | `src/lib_ccx/ccx_decoders_common.h` | Added `ccxr_flush_active_decoders` declaration | | `src/lib_ccx/ccx_decoders_common.c` | Use Rust init/free/flush | | `src/lib_ccx/general_loop.c` | Set encoder via Rust FFI | | `src/lib_ccx/mp4.c` | Use Rust FFI for encoder and processing | Fixes: #1499
claunia added the pull-request label 2026-01-29 17:22:31 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#2510