Skip to content

Commit 3a31433

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 3a31433

4 files changed

Lines changed: 558 additions & 3 deletions

File tree

src/lib_ccx/ccx_encoders_srt.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
#include "ocr.h"
77
#include "ccextractor.h"
88

9+
extern int ccxr_write_stringz_as_srt(const char *string, struct encoder_ctx *context, LLONG ms_start, LLONG ms_end);
10+
extern int ccxr_write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *context);
11+
extern int ccxr_write_cc_subtitle_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context);
12+
913
/* Helper function to write SRT to a specific output file (issue #665 - teletext multi-page)
1014
Takes output file descriptor and counter pointer as parameters */
15+
#if 0 /* C fallback — Rust is always available */
1116
static int write_stringz_as_srt_to_output(char *string, struct encoder_ctx *context, LLONG ms_start, LLONG ms_end,
1217
int out_fh, unsigned int *srt_counter)
1318
{
@@ -83,13 +88,12 @@ static int write_stringz_as_srt_to_output(char *string, struct encoder_ctx *cont
8388

8489
return 0;
8590
}
86-
91+
#endif
8792
/* The timing here is not PTS based, but output based, i.e. user delay must be accounted for
8893
if there is any */
8994
int write_stringz_as_srt(char *string, struct encoder_ctx *context, LLONG ms_start, LLONG ms_end)
9095
{
91-
return write_stringz_as_srt_to_output(string, context, ms_start, ms_end,
92-
context->out->fh, &context->srt_counter);
96+
return ccxr_write_stringz_as_srt(string, context, ms_start, ms_end);
9397
}
9498

9599
int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
@@ -156,6 +160,8 @@ int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
156160

157161
int write_cc_subtitle_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
158162
{
163+
return ccxr_write_cc_subtitle_as_srt(sub, context);
164+
#if 0 /* C fallback — Rust is always available */
159165
int ret = 0;
160166
struct cc_subtitle *osub = sub;
161167
struct cc_subtitle *lsub = sub;
@@ -191,10 +197,13 @@ int write_cc_subtitle_as_srt(struct cc_subtitle *sub, struct encoder_ctx *contex
191197
}
192198

193199
return ret;
200+
#endif
194201
}
195202

196203
int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *context)
197204
{
205+
return ccxr_write_cc_buffer_as_srt(data, context);
206+
#if 0 /* C fallback — Rust is always available */
198207
int used;
199208
unsigned h1, m1, s1, ms1;
200209
unsigned h2, m2, s2, ms2;
@@ -311,4 +320,5 @@ int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *conte
311320
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
312321
// printf("$ = %s\n",context->encoded_crlf);
313322
return wrote_something;
323+
#endif
314324
}

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)