Drop the previous file's bytes when opening a file (#2394)

init_file_buffer(), called by every open(), rewound filebuffer_pos to
0 but kept bytesinbuffer when the buffer already existed. Everything
the previous input file left in the buffer - normally fully consumed -
thus counted as unread data of the newly opened file and was replayed.

With several inputs and binary concatenation this re-fed the start of
the previous file (its stream-detection bytes, up to 1 MB) into the
stream at every file boundary: split recordings produced repeated and
mistimed captions after the join, or aborted. The same replay made a
second RCWT input re-read the first.

Reset bytesinbuffer as well, in both the C and the Rust version.
Whatever the buffer holds at that point belongs to the previous file.
This commit is contained in:
Carlos Fernandez Sanz
2026-10-17 21:50:29 -07:00
committed by GitHub
parent 0bf2a94fe4
commit 6f5d0b5259
3 changed files with 15 additions and 5 deletions

View File

@@ -224,10 +224,13 @@ int init_file_buffer(struct ccx_demuxer *ctx)
{
ctx->filebuffer_start = 0;
ctx->filebuffer_pos = 0;
// Called for every newly opened file. Whatever the buffer still holds
// belongs to the previous file and has been consumed; keeping its byte
// count while rewinding the position would replay it as new data.
ctx->bytesinbuffer = 0;
if (ctx->filebuffer == NULL)
{
ctx->filebuffer = (unsigned char *)malloc(FILEBUFFERSIZE);
ctx->bytesinbuffer = 0;
}
if (ctx->filebuffer == NULL)
{

View File

@@ -556,12 +556,16 @@ mod tests {
let mut ctx = dummy_demuxer();
let buf = vec![1u8; unsafe { FILEBUFFERSIZE } as usize].into_boxed_slice();
ctx.filebuffer = Box::into_raw(buf) as *mut u8;
let original = ctx.filebuffer;
ctx.bytesinbuffer = 123;
ctx.filebuffer_pos = 123;
let res = init_file_buffer(&mut ctx);
assert_eq!(res, 0);
assert!(!ctx.filebuffer.is_null());
// bytesinbuffer remains unchanged.
assert_eq!(ctx.bytesinbuffer, 123);
assert_eq!(ctx.filebuffer, original);
// The bytes belong to the previously opened file: they must not be
// replayed as data of the new one.
assert_eq!(ctx.filebuffer_pos, 0);
assert_eq!(ctx.bytesinbuffer, 0);
// Clean up.
unsafe {
let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(

View File

@@ -40,11 +40,14 @@ pub const FILEBUFFERSIZE: usize = 1024 * 1024 * 16; // 16 Mbytes no less. Minimi
pub fn init_file_buffer(ctx: &mut CcxDemuxer) -> i32 {
ctx.filebuffer_start = 0;
ctx.filebuffer_pos = 0;
// Called for every newly opened file. Whatever the buffer still holds
// belongs to the previous file and has been consumed; keeping its byte
// count while rewinding the position would replay it as new data.
ctx.bytesinbuffer = 0;
if ctx.filebuffer.is_null() {
let mut buf = vec![0u8; FILEBUFFERSIZE].into_boxed_slice();
ctx.filebuffer = buf.as_mut_ptr();
mem::forget(buf);
ctx.bytesinbuffer = 0;
}
if ctx.filebuffer.is_null() {
return -1;