From d6ccf1bfcba606c633b40349dcab07dfc77f73a5 Mon Sep 17 00:00:00 2001 From: Ishan Grover <123376573+IshanGrover2004@users.noreply.github.com> Date: Wed, 29 May 2024 12:58:24 +0530 Subject: [PATCH] [FEATURE] Port 708 decoder encoding module to RUST (#1607) * feat: Add `decoder/encoding` new module This `decoder/encoding.rs` file will contain the content of `lib_ccx/ccx_708_decoder_encoding.c` file * feat: Add encoding functions * feat: Add conditional compilation to include Rust functions * fix: conditional compilation logic * refactor: Use of match statement instead of if-else * fix: Calling C function for rust * feat: Enable `derive_default` feature --- src/lib_ccx/ccx_decoders_708_encoding.c | 4 +++ src/lib_ccx/ccx_decoders_708_encoding.h | 9 ++++- src/rust/build.rs | 2 ++ src/rust/src/decoder/encoding.rs | 45 +++++++++++++++++++++++++ src/rust/src/decoder/mod.rs | 1 + 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/rust/src/decoder/encoding.rs diff --git a/src/lib_ccx/ccx_decoders_708_encoding.c b/src/lib_ccx/ccx_decoders_708_encoding.c index 5cf9c74f6..b3d9508d4 100644 --- a/src/lib_ccx/ccx_decoders_708_encoding.c +++ b/src/lib_ccx/ccx_decoders_708_encoding.c @@ -12,6 +12,8 @@ EIA-708, SO INTERNALLY WE USE THIS TABLE (FOR CONVENIENCE) A0-FF -> Group G1 as is - non-English characters and symbols */ +#if defined(DISABLE_RUST) + unsigned char dtvcc_get_internal_from_G0(unsigned char g0_char) { return g0_char; @@ -43,3 +45,5 @@ unsigned char dtvcc_get_internal_from_G3(unsigned char g3_char) // Rest unmapped, so we return a blank space return 0x20; } + +#endif diff --git a/src/lib_ccx/ccx_decoders_708_encoding.h b/src/lib_ccx/ccx_decoders_708_encoding.h index 0318a0f1c..ef7ca2a84 100644 --- a/src/lib_ccx/ccx_decoders_708_encoding.h +++ b/src/lib_ccx/ccx_decoders_708_encoding.h @@ -1,11 +1,18 @@ #ifndef _CCX_DECODERS_708_ENCODING_H_ #define _CCX_DECODERS_708_ENCODING_H_ -#define CCX_DTVCC_MUSICAL_NOTE_CHAR 9836 // Unicode Character 'BEAMED SIXTEENTH NOTES' +#define CCX_DTVCC_MUSICAL_NOTE_CHAR 9836 // Unicode Character 'BEAMED SIXTEENTH NOTES' +#ifndef DISABLE_RUST +extern unsigned char dtvcc_get_internal_from_G0(unsigned char g0_char); +extern unsigned char dtvcc_get_internal_from_G1(unsigned char g1_char); +extern unsigned char dtvcc_get_internal_from_G2(unsigned char g2_char); +extern unsigned char dtvcc_get_internal_from_G3(unsigned char g3_char); +#else unsigned char dtvcc_get_internal_from_G0(unsigned char g0_char); unsigned char dtvcc_get_internal_from_G1(unsigned char g1_char); unsigned char dtvcc_get_internal_from_G2(unsigned char g2_char); unsigned char dtvcc_get_internal_from_G3(unsigned char g3_char); +#endif #endif /*_CCX_DECODERS_708_ENCODING_H_*/ diff --git a/src/rust/build.rs b/src/rust/build.rs index f8ecc04c8..03e6157f6 100644 --- a/src/rust/build.rs +++ b/src/rust/build.rs @@ -59,6 +59,8 @@ fn main() { } let bindings = builder + .derive_default(true) + .no_default("dtvcc_pen_attribs|dtvcc_pen_color|dtvcc_symbol") // Finish the builder and generate the bindings. .generate() // Unwrap the Result and panic on failure. diff --git a/src/rust/src/decoder/encoding.rs b/src/rust/src/decoder/encoding.rs new file mode 100644 index 000000000..2671cedd9 --- /dev/null +++ b/src/rust/src/decoder/encoding.rs @@ -0,0 +1,45 @@ +/// 256 BYTES IS ENOUGH FOR ALL THE SUPPORTED CHARACTERS IN +/// EIA-708, SO INTERNALLY WE USE THIS TABLE (FOR CONVENIENCE) +/// +/// 00-1F -> Characters that are in the G2 group in 20-3F, +/// except for 06, which is used for the closed captions +/// sign "CC" which is defined in group G3 as 00. (this +/// is by the article 33). +/// 20-7F -> Group G0 as is - corresponds to the ASCII code +/// 80-9F -> Characters that are in the G2 group in 60-7F +/// (there are several blank characters here, that's OK) +/// A0-FF -> Group G1 as is - non-English characters and symbols + +// NOTE: Same as `lib_ccx/ccx_decoder_708_encoding.c` file + +#[no_mangle] +pub extern "C" fn dtvcc_get_internal_from_G0(g0_char: u8) -> u8 { + g0_char +} + +#[no_mangle] +pub extern "C" fn dtvcc_get_internal_from_G1(g1_char: u8) -> u8 { + g1_char +} + +/// G2: Extended Control Code Set 1 +#[no_mangle] +pub extern "C" fn dtvcc_get_internal_from_G2(g2_char: u8) -> u8 { + match g2_char { + 0x20..=0x3F => g2_char - 0x20, + 0x60..=0x7F => g2_char + 0x20, + _ => 0x20, + } +} + +/// G3: Future Characters and Icon Expansion +#[no_mangle] +pub extern "C" fn dtvcc_get_internal_from_G3(g3_char: u8) -> u8 { + if g3_char == 0xa0 { + // The "CC" (closed captions) sign + 0x06 + } else { + // Rest unmapped, so we return a blank space + 0x20 + } +} diff --git a/src/rust/src/decoder/mod.rs b/src/rust/src/decoder/mod.rs index e13c7ed2d..2ee5c26bc 100644 --- a/src/rust/src/decoder/mod.rs +++ b/src/rust/src/decoder/mod.rs @@ -3,6 +3,7 @@ //! Provides a CEA 708 decoder as defined by ANSI/CTA-708-E R-2018 mod commands; +mod encoding; mod output; mod service_decoder; mod timing;