Skip to content

Commit 5d8dc3b

Browse files
authored
Rust updates:- Add writers for transcripts and SAMI (#1372)
* add rust-iconv * Add writer for transcipts and SAMI * consistent import ordering
1 parent a42e847 commit 5d8dc3b

File tree

9 files changed

+222
-73
lines changed

9 files changed

+222
-73
lines changed

src/rust/Cargo.lock

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ crate-type = ["cdylib"]
1313
[dependencies]
1414
log = "0.4.0"
1515
env_logger = "0.8.4"
16+
iconv = "0.1.1"
1617

1718
[build-dependencies]
1819
bindgen = "0.58.1"

src/rust/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ const ALLOWLIST_FUNCTIONS: &[&str] = &[
4444
"writercwtdata",
4545
];
4646
const ALLOWLIST_TYPES: &[&str] = &[".*(?i)_?dtvcc_.*", "encoder_ctx", "lib_cc_decode"];
47-
const RUSTIFIED_ENUMS: &[&str] = &["dtvcc_(window|pen)_.*"];
47+
const RUSTIFIED_ENUMS: &[&str] = &["dtvcc_(window|pen)_.*", "ccx_output_format"];

src/rust/src/decoder/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ mod timing;
99
mod tv_screen;
1010
mod window;
1111

12-
use log::{debug, warn};
13-
1412
use crate::{bindings::*, utils::is_true};
1513

14+
use log::{debug, warn};
15+
1616
const CCX_DTVCC_MAX_PACKET_LENGTH: u8 = 128;
1717
const CCX_DTVCC_NO_LAST_SEQUENCE: i32 = -1;
1818
const CCX_DTVCC_SCREENGRID_ROWS: u8 = 75;

src/rust/src/decoder/output.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1+
#[cfg(windows)]
2+
use std::os::windows::io::{FromRawHandle, IntoRawHandle, RawHandle};
13
use std::{
24
fs::File,
35
io::Write,
46
os::unix::prelude::{FromRawFd, IntoRawFd},
57
};
68

7-
use log::debug;
8-
9-
#[cfg(windows)]
10-
use std::os::windows::io::{FromRawHandle, IntoRawHandle, RawHandle};
11-
129
use crate::{bindings::*, utils::is_true};
1310

11+
use log::debug;
12+
1413
// Context for writing subtitles to file
1514
pub struct Writer<'a> {
1615
pub cea_708_counter: &'a mut u32,
1716
pub subs_delay: LLONG,
1817
pub crlf: String,
19-
pub write_format: u32,
18+
pub write_format: ccx_output_format,
2019
pub writer_ctx: &'a mut dtvcc_writer_ctx,
2120
pub no_font_color: bool,
21+
pub transcript_settings: &'a ccx_encoders_transcript_format,
22+
pub no_bom: i32,
2223
}
2324

2425
impl<'a> Writer<'a> {
2526
pub fn new(
2627
cea_708_counter: &'a mut u32,
2728
subs_delay: LLONG,
28-
write_format: u32,
29+
write_format: ccx_output_format,
2930
writer_ctx: &'a mut dtvcc_writer_ctx,
3031
no_font_color: i32,
32+
transcript_settings: &'a ccx_encoders_transcript_format,
33+
no_bom: i32,
3134
) -> Self {
3235
Self {
3336
cea_708_counter,
@@ -36,17 +39,31 @@ impl<'a> Writer<'a> {
3639
write_format,
3740
writer_ctx,
3841
no_font_color: is_true(no_font_color),
42+
transcript_settings,
43+
no_bom,
3944
}
4045
}
46+
pub fn write_to_file(&mut self, buf: &[u8]) -> Result<(), String> {
47+
#[cfg(unix)]
48+
{
49+
let mut file = unsafe { File::from_raw_fd(self.writer_ctx.fd) };
50+
file.write_all(buf).map_err(|err| err.to_string())?;
51+
self.writer_ctx.fd = file.into_raw_fd();
52+
}
53+
#[cfg(windows)]
54+
{
55+
let mut file = unsafe { File::from_raw_handle(self.writer_ctx.fhandle) };
56+
file.write_all(buf).map_err(|err| err.to_string())?;
57+
self.writer_ctx.fhandle = file.into_raw_handle();
58+
}
59+
Ok(())
60+
}
4161
}
4262

4363
pub fn write_char(sym: &dtvcc_symbol, buf: &mut Vec<u8>) {
4464
if sym.sym >> 8 != 0 {
4565
buf.push((sym.sym >> 8) as u8);
4666
buf.push((sym.sym & 0xff) as u8);
47-
for x in buf.iter().rev().take(2) {
48-
println!("Chars:- {:02X}", x);
49-
}
5067
} else {
5168
buf.push(sym.sym as u8);
5269
}
@@ -62,19 +79,3 @@ pub fn color_to_hex(color: u8) -> (u8, u8, u8) {
6279
);
6380
(red, green, blue)
6481
}
65-
66-
pub fn write_to_file(writer: &mut Writer, buf: &[u8]) -> std::io::Result<()> {
67-
#[cfg(unix)]
68-
{
69-
let mut file = unsafe { File::from_raw_fd(writer.writer_ctx.fd) };
70-
file.write_all(buf)?;
71-
writer.writer_ctx.fd = file.into_raw_fd();
72-
}
73-
#[cfg(windows)]
74-
{
75-
let mut file = unsafe { File::from_raw_handle(writer.writer_ctx.fhandle) };
76-
file.write_all(buf)?;
77-
writer.writer_ctx.fhandle = file.into_raw_handle();
78-
}
79-
Ok(())
80-
}

src/rust/src/decoder/service_decoder.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ use std::{
33
os::raw::c_uchar,
44
};
55

6-
use log::{debug, error, warn};
7-
6+
use super::commands::{self, C0CodeSet, C0Command, C1CodeSet, C1Command};
7+
use super::window::{PenPreset, WindowPreset};
88
use super::{
9-
commands::{self, C0CodeSet, C0Command, C1CodeSet, C1Command},
10-
window::{PenPreset, WindowPreset},
119
CCX_DTVCC_MAX_COLUMNS, CCX_DTVCC_MAX_ROWS, CCX_DTVCC_SCREENGRID_COLUMNS,
1210
CCX_DTVCC_SCREENGRID_ROWS,
1311
};
@@ -17,6 +15,8 @@ use crate::{
1715
utils::{is_false, is_true},
1816
};
1917

18+
use log::{debug, error, warn};
19+
2020
const CCX_DTVCC_MUSICAL_NOTE_CHAR: u16 = 9836;
2121
const CCX_DTVCC_MAX_WINDOWS: u8 = 8;
2222
const DTVCC_COMMANDS_C0_CODES_DTVCC_C0_EXT1: u8 = 16;
@@ -949,6 +949,8 @@ impl dtvcc_service_decoder {
949949
encoder.write_format,
950950
writer_ctx,
951951
encoder.no_font_color,
952+
&*encoder.transcript_settings,
953+
encoder.no_bom,
952954
);
953955
tv.writer_output(&mut writer).unwrap();
954956
tv.clear();

0 commit comments

Comments
 (0)