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

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

📋 Pull Request Information

Original PR: https://github.com/CCExtractor/ccextractor/pull/1857
Author: @cfsmp3
Created: 12/20/2025
Status: Merged
Merged: 12/20/2025
Merged by: @cfsmp3

Base: masterHead: fix/issue-1626-broken-avc-stream


📝 Commits (1)

  • 73db3a2 fix(avc): Handle streams that don't start with NAL start codes (#1626)

📊 Changes

1 file changed (+161 additions, -46 deletions)

View changed files

📝 src/rust/src/avc/core.rs (+161 -46)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/CCExtractor/ccextractor/pull/1857 **Author:** [@cfsmp3](https://github.com/cfsmp3) **Created:** 12/20/2025 **Status:** ✅ Merged **Merged:** 12/20/2025 **Merged by:** [@cfsmp3](https://github.com/cfsmp3) **Base:** `master` ← **Head:** `fix/issue-1626-broken-avc-stream` --- ### 📝 Commits (1) - [`73db3a2`](https://github.com/CCExtractor/ccextractor/commit/73db3a2c39f03006d89fc5d2f2a19fb27624e3f3) fix(avc): Handle streams that don't start with NAL start codes (#1626) ### 📊 Changes **1 file changed** (+161 additions, -46 deletions) <details> <summary>View changed files</summary> 📝 `src/rust/src/avc/core.rs` (+161 -46) </details> ### 📄 Description ## 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#2623