Skip to content

Conversation

@DhanushVarma-2
Copy link
Contributor

@DhanushVarma-2 DhanushVarma-2 commented Nov 10, 2025

Problem
When processing TS files containing no subtitles, ccextractor enters an infinite loop instead of exiting gracefully (Issue #1754).

Root Cause
The ts_readstream function had a do-while (!gotpes) loop that would run indefinitely when no subtitle streams were detected, because gotpes never became true in files without subtitles.

Solution
Instead of using an arbitrary packet limit, this fix properly detects when no subtitle streams exist by:

  1. Tracking PMT analysis: Monitors when all Program Map Tables have been analyzed
  2. Checking for caption streams: After PMT analysis, checks if any caption PIDs were registered (ctx->num_of_PIDs == 0)
  3. Grace period: Waits 1000 packets after PMT analysis to ensure all streams are detected
  4. Clean exit: Returns CCX_EOF with a clear debug message when no subtitles are found

Why This Approach is Better

  • PMT-based detection: Uses actual stream metadata instead of arbitrary limits
  • Deterministic: Based on MPEG-TS structure, not guesswork
  • Faster: Exits as soon as it's confirmed no caption streams exist (~1000 packets vs 50,000)
  • Maintains compatibility: Works for files with subtitles exactly as before
  • No magic numbers: Logic is based on actual stream parsing

Changes Made

  • src/lib_ccx/ts_functions.c: Added PMT analysis tracking and caption stream detection in ts_readstream function

Testing

  • Code compiles successfully without errors
  • Maintains backward compatibility for files with subtitles

Fixes #1754

@cfsmp3
Copy link
Contributor

cfsmp3 commented Dec 8, 2025

Why would no subtitles cause an infinite loop? Do you have an example file?

I don't like the heuristics solution TBH.

@cfsmp3 cfsmp3 self-requested a review December 8, 2025 02:04
Copy link
Contributor

@cfsmp3 cfsmp3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please submit an example video to shows the problem.
There must be a way to fix it that doesn't consist on a random limit.

@DhanushVarma-2
Copy link
Contributor Author

Example TS file (no subtitles):
no_subs.ts
This is a valid MPEG-TS containing video + audio only, with no subtitle streams
Verified via:
ffprobe no_subs.ts | grep -i subtitle
(no output)

@DhanushVarma-2
Copy link
Contributor Author

A hard packet limit is not ideal and was added only as a safeguard to prevent the infinite loop.
The fix can instead terminate cleanly based on EOF / PMT parsing when no subtitle streams exist

- Check if all PMTs have been analyzed
- After PMT analysis, verify if any caption streams (PIDs) were found
- Exit after 1000 packets if num_of_PIDs == 0 (no caption streams)
- Uses PMT-based detection instead of arbitrary packet limits

This fix properly uses the Program Map Table data to determine if
subtitle streams exist, rather than relying on arbitrary packet counts.

Fixes CCExtractor#1754
@DhanushVarma-2 DhanushVarma-2 force-pushed the fix-ts-infinite-loop-fresh branch from 8e5f4d2 to 8d4d84b Compare December 17, 2025 06:53
@rboy1
Copy link
Contributor

rboy1 commented Dec 21, 2025

What's the root cause of the problem? what if subtitles begin half way through the video (e.g. when a new segment or show starts)?

@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on linux. Below is a summary of the test results, when compared to test for commit 45ee03a...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 7/7
DVD 3/3
DVR-MS 2/2
General 24/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 80/86
Teletext 21/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=ttxt --latin1 1974a299f0...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --fullbin --out=srt c83f765c66...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...

It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on windows. Below is a summary of the test results, when compared to test for commit f6e9d55...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 6/7
DVD 3/3
DVR-MS 2/2
General 24/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 80/86
Teletext 21/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...
  • ccextractor --autoprogram --out=ttxt --latin1 1974a299f0...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --out=spupng c83f765c66...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...

It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

@rboy1
Copy link
Contributor

rboy1 commented Dec 26, 2025

Shouldn’t the loop terminate automatically in ts_readpacket when it encounters a EOF? If it’s running in an infinite loop won’t the mean there’a an issue with getting to EOF?

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.

[BUG] Infinite binary output when processing .ts files with no subtitles

4 participants