Skip to content

Commit

Permalink
Rust updates: Added srt writer (#1368)
Browse files Browse the repository at this point in the history
* pass -DENABLE_RUST to clang

* impl Default

* Update time str format

* Add SRT writer

* fmt
  • Loading branch information
PunitLodha committed Aug 3, 2021
1 parent d58f078 commit b18e696
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 23 deletions.
13 changes: 13 additions & 0 deletions src/lib_ccx/ccx_decoders_708_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "utility.h"
#include "ccx_common_common.h"

#if defined(ENABLE_RUST) && defined(WIN32)
extern void ccxr_close_handle(void *handle);
#endif

int dtvcc_is_row_empty(dtvcc_tv_screen *tv, int row_index)
{
for (int j = 0; j < CCX_DTVCC_SCREENGRID_COLUMNS; j++)
Expand Down Expand Up @@ -434,6 +438,11 @@ void dtvcc_writer_init(dtvcc_writer_ctx *writer,

char *charset = cfg->all_services_charset ? cfg->all_services_charset : cfg->services_charsets[service_number - 1];

#ifdef ENABLE_RUST
writer->fhandle = NULL;
writer->charset = charset;
#endif

if (charset)
{
writer->cd = iconv_open("UTF-8", charset);
Expand All @@ -450,6 +459,10 @@ void dtvcc_writer_cleanup(dtvcc_writer_ctx *writer)
{
if (writer->fd >= 0 && writer->fd != STDOUT_FILENO)
close(writer->fd);
#if defined(ENABLE_RUST) && defined(WIN32)
ccxr_close_handle(writer->fhandle);
writer->charset = NULL;
#endif
free(writer->filename);
if (writer->cd == (iconv_t)-1)
{
Expand Down
8 changes: 8 additions & 0 deletions src/lib_ccx/ccx_encoders_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,10 @@ static int init_output_ctx(struct encoder_ctx *ctx, struct encoder_cfg *cfg)
if (!cfg->services_enabled[i])
{
ctx->dtvcc_writers[i].fd = -1;
#ifdef ENABLE_RUST
ctx->dtvcc_writers[i].fhandle = NULL;
ctx->dtvcc_writers[i].charset = NULL;
#endif
ctx->dtvcc_writers[i].filename = NULL;
ctx->dtvcc_writers[i].cd = (iconv_t)-1;
continue;
Expand All @@ -877,6 +881,10 @@ static int init_output_ctx(struct encoder_ctx *ctx, struct encoder_cfg *cfg)
if (cfg->cc_to_stdout)
{
ctx->dtvcc_writers[i].fd = STDOUT_FILENO;
#ifdef ENABLE_RUST
ctx->dtvcc_writers[i].fhandle = NULL;
ctx->dtvcc_writers[i].charset = NULL;
#endif
ctx->dtvcc_writers[i].filename = NULL;
ctx->dtvcc_writers[i].cd = (iconv_t)-1;
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib_ccx/ccx_encoders_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ if (ctx->buffer == NULL) { fatal(EXIT_NOT_ENOUGH_MEMORY, "Not enough memory for
typedef struct dtvcc_writer_ctx
{
int fd;
#ifdef ENABLE_RUST
// File handle used to work with files on windows
void *fhandle;
// Charset of the subtitle
char *charset;
#endif
char *filename;
iconv_t cd;
} dtvcc_writer_ctx;
Expand Down
1 change: 1 addition & 0 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn main() {
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
.clang_arg("-DENABLE_RUST")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks));
Expand Down
10 changes: 9 additions & 1 deletion src/rust/src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! This module provides a CEA 708 decoder as defined by ANSI/CTA-708-E R-2018

mod commands;
mod output;
mod service_decoder;
mod timing;
mod tv_screen;
Expand Down Expand Up @@ -198,8 +199,15 @@ impl dtvcc_symbol {
let sym = (data1 as u16) << 8 | data2 as u16;
Self { init: 1, sym }
}
/// Check if symbol is initialized
pub fn is_set(&self) -> bool {
is_true(self.init)
}
}

impl Default for dtvcc_symbol {
/// Create a blank uninitialized symbol
pub fn default() -> Self {
fn default() -> Self {
Self { sym: 0, init: 0 }
}
}
80 changes: 80 additions & 0 deletions src/rust/src/decoder/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::{
fs::File,
io::Write,
os::unix::prelude::{FromRawFd, IntoRawFd},
};

use log::debug;

#[cfg(windows)]
use std::os::windows::io::{FromRawHandle, IntoRawHandle, RawHandle};

use crate::{bindings::*, utils::is_true};

// Context for writing subtitles to file
pub struct Writer<'a> {
pub cea_708_counter: &'a mut u32,
pub subs_delay: LLONG,
pub crlf: String,
pub write_format: u32,
pub writer_ctx: &'a mut dtvcc_writer_ctx,
pub no_font_color: bool,
}

impl<'a> Writer<'a> {
pub fn new(
cea_708_counter: &'a mut u32,
subs_delay: LLONG,
write_format: u32,
writer_ctx: &'a mut dtvcc_writer_ctx,
no_font_color: i32,
) -> Self {
Self {
cea_708_counter,
subs_delay,
crlf: "\r\n".to_owned(),
write_format,
writer_ctx,
no_font_color: is_true(no_font_color),
}
}
}

pub fn write_char(sym: &dtvcc_symbol, buf: &mut Vec<u8>) {
if sym.sym >> 8 != 0 {
buf.push((sym.sym >> 8) as u8);
buf.push((sym.sym & 0xff) as u8);
for x in buf.iter().rev().take(2) {
println!("Chars:- {:02X}", x);
}
} else {
buf.push(sym.sym as u8);
}
}

pub fn color_to_hex(color: u8) -> (u8, u8, u8) {
let red = color >> 4;
let green = (color >> 2) & 0x3;
let blue = color & 0x3;
debug!(
"Color: {} [{:06x}] {} {} {}",
color, color, red, green, blue
);
(red, green, blue)
}

pub fn write_to_file(writer: &mut Writer, buf: &[u8]) -> std::io::Result<()> {
#[cfg(unix)]
{
let mut file = unsafe { File::from_raw_fd(writer.writer_ctx.fd) };
file.write_all(buf)?;
writer.writer_ctx.fd = file.into_raw_fd();
}
#[cfg(windows)]
{
let mut file = unsafe { File::from_raw_handle(writer.writer_ctx.fhandle) };
file.write_all(buf)?;
writer.writer_ctx.fhandle = file.into_raw_handle();
}
Ok(())
}
12 changes: 10 additions & 2 deletions src/rust/src/decoder/service_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{
};
use crate::{
bindings::*,
decoder::output::Writer,
utils::{is_false, is_true},
};

Expand Down Expand Up @@ -939,10 +940,17 @@ impl dtvcc_service_decoder {
let tv = &mut (*self.tv);
tv.cc_count += 1;
let sn = tv.service_number;
let writer = &mut encoder.dtvcc_writers[(sn - 1) as usize];
let writer_ctx = &mut encoder.dtvcc_writers[(sn - 1) as usize];

tv.update_time_hide(timing.get_visible_end(3));
dtvcc_writer_output(writer, self, encoder);
let mut writer = Writer::new(
&mut encoder.cea_708_counter,
encoder.subs_delay,
encoder.write_format,
writer_ctx,
encoder.no_font_color,
);
tv.writer_output(&mut writer).unwrap();
tv.clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/decoder/timing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ pub fn get_time_str(time: LLONG) -> String {
let mm = time / 1000 / 60 - 60 * hh;
let ss = time / 1000 - 60 * (mm + 60 * hh);
let ms = time - 1000 * (ss + 60 * (mm + 60 * hh));
format!("{:02}:{:02}:{:02}:{:03}", hh, mm, ss, ms)
format!("{:02}:{:02}:{:02},{:03}", hh, mm, ss, ms)
}
Loading

0 comments on commit b18e696

Please sign in to comment.