Skip to content

Commit af58374

Browse files
author
Dhanush Varma
committed
feat: port SRT encoder to Rust
Implement ccxr_write_stringz_as_srt and ccxr_write_cc_buffer_as_srt in src/rust/src/encoder/srt.rs. Covers: - Subtitle counter and timestamp formatting with -1ms overlap prevention - \n unescape handling for multi-line subtitles - Encoding conversion (UTF-8, Latin1, UCS-2) - Autodash detection for CEA-608 screen buffers - Speaker name detection (colon-based) Uses existing Rust encoder infrastructure (encode_line, write_wrapped) and calls C get_decoder_line_encoded for CEA-608 line encoding until that function is also ported. Exported as #[no_mangle] extern C functions ready to replace the C versions in ccx_encoders_srt.c.
1 parent 9f250b1 commit af58374

File tree

3 files changed

+548
-1
lines changed

3 files changed

+548
-1
lines changed

src/lib_ccx/ccx_encoders_srt.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
#include "ocr.h"
77
#include "ccextractor.h"
88

9+
#ifndef DISABLE_RUST
10+
extern int ccxr_write_stringz_as_srt(const char *string, struct encoder_ctx *context, LLONG ms_start, LLONG ms_end);
11+
extern int ccxr_write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *context);
12+
extern int ccxr_write_cc_subtitle_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context);
13+
#endif
14+
915
/* Helper function to write SRT to a specific output file (issue #665 - teletext multi-page)
1016
Takes output file descriptor and counter pointer as parameters */
17+
#ifdef DISABLE_RUST
1118
static int write_stringz_as_srt_to_output(char *string, struct encoder_ctx *context, LLONG ms_start, LLONG ms_end,
1219
int out_fh, unsigned int *srt_counter)
1320
{
@@ -83,13 +90,17 @@ static int write_stringz_as_srt_to_output(char *string, struct encoder_ctx *cont
8390

8491
return 0;
8592
}
86-
93+
#endif
8794
/* The timing here is not PTS based, but output based, i.e. user delay must be accounted for
8895
if there is any */
8996
int write_stringz_as_srt(char *string, struct encoder_ctx *context, LLONG ms_start, LLONG ms_end)
9097
{
98+
#ifndef DISABLE_RUST
99+
return ccxr_write_stringz_as_srt(string, context, ms_start, ms_end);
100+
#else
91101
return write_stringz_as_srt_to_output(string, context, ms_start, ms_end,
92102
context->out->fh, &context->srt_counter);
103+
#endif
93104
}
94105

95106
int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
@@ -156,6 +167,9 @@ int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
156167

157168
int write_cc_subtitle_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
158169
{
170+
#ifndef DISABLE_RUST
171+
return ccxr_write_cc_subtitle_as_srt(sub, context);
172+
#else
159173
int ret = 0;
160174
struct cc_subtitle *osub = sub;
161175
struct cc_subtitle *lsub = sub;
@@ -191,10 +205,14 @@ int write_cc_subtitle_as_srt(struct cc_subtitle *sub, struct encoder_ctx *contex
191205
}
192206

193207
return ret;
208+
#endif
194209
}
195210

196211
int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *context)
197212
{
213+
#ifndef DISABLE_RUST
214+
return ccxr_write_cc_buffer_as_srt(data, context);
215+
#else
198216
int used;
199217
unsigned h1, m1, s1, ms1;
200218
unsigned h2, m2, s2, ms2;
@@ -311,4 +329,5 @@ int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *conte
311329
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
312330
// printf("$ = %s\n",context->encoded_crlf);
313331
return wrote_something;
332+
#endif
314333
}

src/rust/src/encoder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::os::raw::{c_int, c_uchar};
77
pub mod common;
88
pub mod g608;
99
pub mod simplexml;
10+
pub mod srt;
1011
/// # Safety
1112
/// This function is unsafe because it deferences to raw pointers and performs operations on pointer slices.
1213
#[no_mangle]

0 commit comments

Comments
 (0)