[PR #1857] fix(avc): Handle streams that don't start with NAL start codes #2626

Closed
opened 2026-01-29 17:23:08 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/CCExtractor/ccextractor/pull/1857

State: closed
Merged: Yes


Summary

  • Fixes #1626 - [BUG] [EIA-608] Can't extract subtitles in TS file with "Broken AVC stream" error
  • Fixes #1582 - [BUG] Error: Broken AVC stream on Twitch.tv stream file
  • Fixes #1597 - [BUG] Subtitles not extracted from Twitch stream

Problem

The AVC parser would fail with "Leading bytes are non-zero" error when processing HLS/Twitch stream segments. These segments often start mid-stream without proper NAL unit headers at the beginning.

Root cause: When process_avc encountered non-zero leading bytes, it returned an error with 0 bytes processed. The C code would not remove any bytes from the buffer, causing subsequent data to accumulate with the corrupt beginning, leading to infinite errors and no caption extraction.

Solution

  • Add find_nal_start_code() function to search for valid NAL start codes (3-byte or 4-byte)
  • If buffer doesn't start with 0x00 0x00, search for first NAL start code
  • Skip garbage data before first valid NAL unit
  • Return full buffer length when no NAL found (clears the buffer, prevents accumulation)
  • Change forbidden_zero_bit error from fatal to skip-and-continue

Test plan

  • Tested with all 6 Twitch HLS sample files from #1626 - all now process correctly
  • Tested with sample from #1582 - processes to 100% with no AVC errors
  • Captions extracted successfully (see output below)
  • All 299 Rust tests pass (292 existing + 7 new unit tests for find_nal_start_code)

Before fix:

Error in process_avc: BrokenStream("Leading bytes are non-zero")
Error in process_avc: BrokenStream("Leading bytes are non-zero")
... (repeated for every PES packet)

After fix:

Number of NAL_type_7: 5
Total frames time:    00:00:10:000  (600 frames at 60.00fps)
Done, processing time = 0 seconds

Extracted captions:

1
00:00:04,066 --> 00:00:06,332
congratulations to papa plot and
their community for winning ■k71
million in a TV contest through 

2
00:00:06,334 --> 00:00:09,998
their community for winning ■k71
million in a TV contest through 
Twitch. I mean, just wild. Um

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/CCExtractor/ccextractor/pull/1857 **State:** closed **Merged:** Yes --- ## Summary - Fixes #1626 - [BUG] [EIA-608] Can't extract subtitles in TS file with "Broken AVC stream" error - Fixes #1582 - [BUG] Error: Broken AVC stream on Twitch.tv stream file - Fixes #1597 - [BUG] Subtitles not extracted from Twitch stream ## Problem The AVC parser would fail with "Leading bytes are non-zero" error when processing HLS/Twitch stream segments. These segments often start mid-stream without proper NAL unit headers at the beginning. **Root cause:** When `process_avc` encountered non-zero leading bytes, it returned an error with 0 bytes processed. The C code would not remove any bytes from the buffer, causing subsequent data to accumulate with the corrupt beginning, leading to infinite errors and no caption extraction. ## Solution - Add `find_nal_start_code()` function to search for valid NAL start codes (3-byte or 4-byte) - If buffer doesn't start with `0x00 0x00`, search for first NAL start code - Skip garbage data before first valid NAL unit - Return full buffer length when no NAL found (clears the buffer, prevents accumulation) - Change forbidden_zero_bit error from fatal to skip-and-continue ## Test plan - [x] Tested with all 6 Twitch HLS sample files from #1626 - all now process correctly - [x] Tested with sample from #1582 - processes to 100% with no AVC errors - [x] Captions extracted successfully (see output below) - [x] All 299 Rust tests pass (292 existing + 7 new unit tests for `find_nal_start_code`) **Before fix:** ``` Error in process_avc: BrokenStream("Leading bytes are non-zero") Error in process_avc: BrokenStream("Leading bytes are non-zero") ... (repeated for every PES packet) ``` **After fix:** ``` Number of NAL_type_7: 5 Total frames time: 00:00:10:000 (600 frames at 60.00fps) Done, processing time = 0 seconds ``` **Extracted captions:** ``` 1 00:00:04,066 --> 00:00:06,332 congratulations to papa plot and their community for winning ■k71 million in a TV contest through 2 00:00:06,334 --> 00:00:09,998 their community for winning ■k71 million in a TV contest through Twitch. I mean, just wild. Um ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code)
claunia added the pull-request label 2026-01-29 17:23:08 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#2626