Skip to content

Commit

Permalink
[FEATURE] Port 708 decoder encoding module to RUST (#1607)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IshanGrover2004 authored May 29, 2024
1 parent 8e3b145 commit d6ccf1b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/lib_ccx/ccx_decoders_708_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
9 changes: 8 additions & 1 deletion src/lib_ccx/ccx_decoders_708_encoding.h
Original file line number Diff line number Diff line change
@@ -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_*/
2 changes: 2 additions & 0 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions src/rust/src/decoder/encoding.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions src/rust/src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d6ccf1b

Please sign in to comment.