Skip to content
80 changes: 64 additions & 16 deletions src/lib_ccx/ccx_decoders_isdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,77 @@ typedef uint32_t rgba;
static rgba Default_clut[128] =
{
// 0-7
RGBA(0, 0, 0, 255), RGBA(255, 0, 0, 255), RGBA(0, 255, 0, 255), RGBA(255, 255, 0, 255),
RGBA(0, 0, 255, 255), RGBA(255, 0, 255, 255), RGBA(0, 255, 255, 255), RGBA(255, 255, 255, 255),
RGBA(0, 0, 0, 255),
RGBA(255, 0, 0, 255),
RGBA(0, 255, 0, 255),
RGBA(255, 255, 0, 255),
RGBA(0, 0, 255, 255),
RGBA(255, 0, 255, 255),
RGBA(0, 255, 255, 255),
RGBA(255, 255, 255, 255),
// 8-15
RGBA(0, 0, 0, 0), RGBA(170, 0, 0, 255), RGBA(0, 170, 0, 255), RGBA(170, 170, 0, 255),
RGBA(0, 0, 170, 255), RGBA(170, 0, 170, 255), RGBA(0, 170, 170, 255), RGBA(170, 170, 170, 255),
RGBA(0, 0, 0, 0),
RGBA(170, 0, 0, 255),
RGBA(0, 170, 0, 255),
RGBA(170, 170, 0, 255),
RGBA(0, 0, 170, 255),
RGBA(170, 0, 170, 255),
RGBA(0, 170, 170, 255),
RGBA(170, 170, 170, 255),
// 16-23
RGBA(0, 0, 85, 255), RGBA(0, 85, 0, 255), RGBA(0, 85, 85, 255), RGBA(0, 85, 170, 255),
RGBA(0, 85, 255, 255), RGBA(0, 170, 85, 255), RGBA(0, 170, 255, 255), RGBA(0, 255, 85, 255),
RGBA(0, 0, 85, 255),
RGBA(0, 85, 0, 255),
RGBA(0, 85, 85, 255),
RGBA(0, 85, 170, 255),
RGBA(0, 85, 255, 255),
RGBA(0, 170, 85, 255),
RGBA(0, 170, 255, 255),
RGBA(0, 255, 85, 255),
// 24-31
RGBA(0, 255, 170, 255), RGBA(85, 0, 0, 255), RGBA(85, 0, 85, 255), RGBA(85, 0, 170, 255),
RGBA(85, 0, 255, 255), RGBA(85, 85, 0, 255), RGBA(85, 85, 85, 255), RGBA(85, 85, 170, 255),
RGBA(0, 255, 170, 255),
RGBA(85, 0, 0, 255),
RGBA(85, 0, 85, 255),
RGBA(85, 0, 170, 255),
RGBA(85, 0, 255, 255),
RGBA(85, 85, 0, 255),
RGBA(85, 85, 85, 255),
RGBA(85, 85, 170, 255),
// 32-39
RGBA(85, 85, 255, 255), RGBA(85, 170, 0, 255), RGBA(85, 170, 85, 255), RGBA(85, 170, 170, 255),
RGBA(85, 170, 255, 255), RGBA(85, 255, 0, 255), RGBA(85, 255, 85, 255), RGBA(85, 255, 170, 255),
RGBA(85, 85, 255, 255),
RGBA(85, 170, 0, 255),
RGBA(85, 170, 85, 255),
RGBA(85, 170, 170, 255),
RGBA(85, 170, 255, 255),
RGBA(85, 255, 0, 255),
RGBA(85, 255, 85, 255),
RGBA(85, 255, 170, 255),
// 40-47
RGBA(85, 255, 255, 255), RGBA(170, 0, 85, 255), RGBA(170, 0, 255, 255), RGBA(170, 85, 0, 255),
RGBA(170, 85, 85, 255), RGBA(170, 85, 170, 255), RGBA(170, 85, 255, 255), RGBA(170, 170, 85, 255),
RGBA(85, 255, 255, 255),
RGBA(170, 0, 85, 255),
RGBA(170, 0, 255, 255),
RGBA(170, 85, 0, 255),
RGBA(170, 85, 85, 255),
RGBA(170, 85, 170, 255),
RGBA(170, 85, 255, 255),
RGBA(170, 170, 85, 255),
// 48-55
RGBA(170, 170, 255, 255), RGBA(170, 255, 0, 255), RGBA(170, 255, 85, 255), RGBA(170, 255, 170, 255),
RGBA(170, 255, 255, 255), RGBA(255, 0, 85, 255), RGBA(255, 0, 170, 255), RGBA(255, 85, 0, 255),
RGBA(170, 170, 255, 255),
RGBA(170, 255, 0, 255),
RGBA(170, 255, 85, 255),
RGBA(170, 255, 170, 255),
RGBA(170, 255, 255, 255),
RGBA(255, 0, 85, 255),
RGBA(255, 0, 170, 255),
RGBA(255, 85, 0, 255),
// 56-63
RGBA(255, 85, 85, 255), RGBA(255, 85, 170, 255), RGBA(255, 85, 255, 255), RGBA(255, 170, 0, 255),
RGBA(255, 170, 85, 255), RGBA(255, 170, 170, 255), RGBA(255, 170, 255, 255), RGBA(255, 255, 85, 255),
RGBA(255, 85, 85, 255),
RGBA(255, 85, 170, 255),
RGBA(255, 85, 255, 255),
RGBA(255, 170, 0, 255),
RGBA(255, 170, 85, 255),
RGBA(255, 170, 170, 255),
RGBA(255, 170, 255, 255),
RGBA(255, 255, 85, 255),
// 64
RGBA(255, 255, 170, 255),
// 65-127 are calculated later.
Expand Down
2 changes: 1 addition & 1 deletion src/lib_ccx/mp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ int processmp4(struct lib_ccx_ctx *ctx, struct ccx_s_mp4Cfg *cfg, char *file)
if (enc_ctx)
enc_ctx->timing = dec_ctx->timing;

// WARN: otherwise cea-708 will not work
// WARN: otherwise cea-708 will not work
#ifndef DISABLE_RUST
ccxr_dtvcc_set_encoder(dec_ctx->dtvcc_rust, enc_ctx);
#else
Expand Down
18 changes: 17 additions & 1 deletion src/lib_ccx/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

int temp_debug = 0; // This is a convenience variable used to enable/disable debug on variable conditions. Find references to understand.
volatile sig_atomic_t change_filename_requested = 0;

extern int ccxr_verify_crc32(uint8_t *buf, int len);
extern int ccxr_levenshtein_dist(const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len);
extern int ccxr_levenshtein_dist_char(const char *s1, const char *s2, unsigned s1len, unsigned s2len);
Expand Down Expand Up @@ -269,6 +268,13 @@ void sleep_secs(int secs)
#endif
}

#ifndef DISABLE_RUST
extern int ccxr_hex_to_int(char high, char low);
int hex_to_int(char high, char low)
{
return ccxr_hex_to_int(high, low);
}
#else
int hex_to_int(char high, char low)
{
unsigned char h, l;
Expand All @@ -286,6 +292,15 @@ int hex_to_int(char high, char low)
return -1;
return h * 16 + l;
}
#endif

#ifndef DISABLE_RUST
extern int ccxr_hex_string_to_int(const char *string, int len);
int hex_string_to_int(char *string, int len)
{
return ccxr_hex_string_to_int(string, len);
}
#else
int hex_string_to_int(char *string, int len)
{
int total_return = 0;
Expand All @@ -302,6 +317,7 @@ int hex_string_to_int(char *string, int len)
}
return total_return;
}
#endif

#ifndef _WIN32
void m_signal(int sig, void (*func)(int))
Expand Down
Empty file.
47 changes: 47 additions & 0 deletions src/rust/lib_ccxr/src/util/hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/// Converts a single lowercase hex character to its integer value.
/// Returns None if the character is not valid (0-9 or a-f).
fn hex_char_to_val(c: char) -> Option<i32> {
match c {
'0'..='9' => Some(c as i32 - '0' as i32),
'a'..='f' => Some(c as i32 - 'a' as i32 + 10),
_ => None,
}
}

/// Converts two lowercase hex characters into a combined integer value.
/// Returns None if either character is invalid.
pub fn hex_to_int(high: char, low: char) -> Option<i32> {
let h = hex_char_to_val(high)?;
let l = hex_char_to_val(low)?;
Some(h * 16 + l)
}
pub fn hex_string_to_int(string: &str, len: usize) -> Option<i32> {
let mut result = 0;
for c in string.chars().take(len) {
result = result * 16 + hex_char_to_val(c)?;
}
Some(result)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_hex_to_int() {
assert_eq!(hex_to_int('4', 'f'), Some(79));
assert_eq!(hex_to_int('0', '0'), Some(0));
assert_eq!(hex_to_int('f', 'f'), Some(255));
assert_eq!(hex_to_int('z', '1'), None); // invalid
assert_eq!(hex_to_int('A', 'F'), None); // uppercase not supported
}
#[test]
fn test_hex_string_to_int() {
assert_eq!(hex_string_to_int("4f", 2), Some(79));
assert_eq!(hex_string_to_int("ff", 2), Some(255));
assert_eq!(hex_string_to_int("00", 2), Some(0));
assert_eq!(hex_string_to_int("ffff", 4), Some(65535));
assert_eq!(hex_string_to_int("4f", 1), Some(4)); // only takes first char
assert_eq!(hex_string_to_int("zz", 2), None); // invalid
}
}
15 changes: 2 additions & 13 deletions src/rust/lib_ccxr/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,29 @@
//! | `UNHAM_8_4` | [`decode_hamming_8_4`] |
//! | `unham_24_18` | [`decode_hamming_24_18`] |
//! | `levenshtein_dist`, levenshtein_dist_char` | [`levenshtein`](levenshtein()) |

pub mod bits;
pub mod encoders_helper;
pub mod encoding;
pub mod hex;
pub mod levenshtein;
pub mod log;
pub mod time;

use std::os::raw::c_char;

/// Helper function that converts a Rust-String (`string`) to C-String (`buffer`).
///
/// # Safety
///
/// `buffer` must have enough allocated space for `string` to fit.
pub fn write_string_into_pointer(buffer: *mut c_char, string: &str) {
let buffer = unsafe { std::slice::from_raw_parts_mut(buffer as *mut u8, string.len() + 1) };
buffer[..string.len()].copy_from_slice(string.as_bytes());
buffer[string.len()] = b'\0';
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_write_string_into_pointer() {
let test_string = "CCExtractor is the best";
let mut buffer = vec![0u8; test_string.len() + 1];
let buffer_ptr = buffer.as_mut_ptr() as *mut c_char;

write_string_into_pointer(buffer_ptr, test_string);

assert_eq!(&buffer[..test_string.len()], test_string.as_bytes());
assert_eq!(buffer[test_string.len()], 0); // Check null terminator
assert_eq!(buffer[test_string.len()], 0);
}
}
1 change: 1 addition & 0 deletions src/rust/src/libccxr_exports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod demuxer;
pub mod demuxerdata;
pub mod net;
pub mod time;
pub mod util;
use crate::ccx_options;
use lib_ccxr::util::log::*;
use lib_ccxr::util::{bits::*, levenshtein::*};
Expand Down
17 changes: 17 additions & 0 deletions src/rust/src/libccxr_exports/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::ffi::{c_char, c_int};

/// Rust equivalent for `hex_to_int` function in C. Uses C-native types as input and output.
///
/// # Safety
///
/// `high` and `low` must be valid ASCII characters. Invalid characters will
/// return -1 rather than causing undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn ccxr_hex_to_int(high: c_char, low: c_char) -> c_int {
lib_ccxr::util::hex::hex_to_int(high as u8 as char, low as u8 as char).unwrap_or(-1)
}
#[no_mangle]
pub unsafe extern "C" fn ccxr_hex_string_to_int(string: *const c_char, len: c_int) -> c_int {
let s = std::ffi::CStr::from_ptr(string).to_str().unwrap_or("");
lib_ccxr::util::hex::hex_string_to_int(s, len as usize).unwrap_or(-1)
}
Loading
Loading