Skip to content

[FIX] Drop the previous file's bytes when opening a file - #2394

Merged
cfsmp3 merged 1 commit into
masterfrom
fix/concat-timing
Oct 18, 2026
Merged

cfsmp3 merged 1 commit into
masterfrom
fix/concat-timing

Conversation

@cfsmp3

@cfsmp3 cfsmp3 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

In raising this pull request, I confirm the following (please check boxes):

Reason for this PR:

  • This PR adds new functionality.
  • This PR fixes a bug that I have personally experienced or that a real user has reported and for which a sample exists.
  • This PR is porting code from C to Rust.

Sanity check:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • If the PR adds new functionality, I've added it to the changelog. If it's just a bug fix, I have NOT added it to the changelog.
  • I am NOT adding new C code unless it's to fix an existing, reproducible bug.

Repro instructions:

Split any captioned TS at a 188-byte-aligned offset and pass both parts (binary concatenation is the default):
head -c $OFF in.ts > a.ts; tail -c +$((OFF+1)) in.ts > b.ts; ccextractor --autoprogram --out=srt a.ts b.ts -o split.srt
and compare with ccextractor --autoprogram --out=srt in.ts -o whole.srt. On master, captions after the join are repeated or mistimed (one 3 GB sample: every timestamp ~465 ms late), and some splits abort.


init_file_buffer(), called by every open(), rewound filebuffer_pos to 0 but kept bytesinbuffer when the buffer already existed. Whatever the previous input file left in the buffer — already consumed — therefore counted as unread data of the newly opened file. With binary concatenation, stream detection on the new file re-read the start of the previous file (its detection bytes, up to 1 MB) and fed it back into the stream at the join.

This resets bytesinbuffer too, in both the C (file_functions.c) and Rust (file.rs) versions; both are only called from the demuxer's open(). The existing Rust test that asserted the old behaviour now checks that the buffer is reused but both counters are reset; it fails without the fix.

Verified:

  • 4 local TS samples × splits at 30/50/70%: master differs from the whole-file output in 11 of 12 (one aborts with SIGABRT); with this PR all 12 are byte-identical to the whole file, including the original 3 GB 50/50 case (608 and 708 outputs). Same with the C-only -min-rust build.
  • 30 varied single-file samples: no output or exit-code changes.
  • cargo test --lib (418), fmt and clippy clean; valgrind shows no invalid accesses on the concat path.

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.
@cfsmp3
cfsmp3 merged commit 6f5d0b5 into master Oct 18, 2026
26 of 28 checks passed
@cfsmp3
cfsmp3 deleted the fix/concat-timing branch October 18, 2026 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant