Skip to content

Fix H.264 SPS and SEI dropped as HEVC access unit delimiters - #2656

Merged
AlexxIT merged 2 commits into
AlexxIT:masterfrom
gnacho:fix/h264-sps-nal-ref-idc-2
Nov 28, 2026
Merged

AlexxIT merged 2 commits into
AlexxIT:masterfrom
gnacho:fix/h264-sps-nal-ref-idc-2

Conversation

@gnacho

@gnacho gnacho commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #2655.

Problem

annexb.EncodeToAVCC drops any NAL unit whose first byte matches the HEVC AUD mask, b & 0x7e == 35 << 1. On an H.264 stream that mask also matches two legal headers:

  • 0x47: SPS with nal_ref_idc = 2
  • 0x46: SEI with nal_ref_idc = 2

Losing the SPS means the RTSP SDP is published without sprop-parameter-sets, and clients cannot decode the stream at all:

a=fmtp:96 packetization-mode=1,SO48gA==
[h264] sps_id 0 out of range
[h264] non-existing PPS 0 referenced

Fix

The NAL unit is now checked once its size is known instead of looking only at its first byte. A HEVC AUD is always three bytes long (two header bytes plus the picture type) and has a non zero nuh_temporal_id_plus1, which is what tells it apart from an H.264 SPS or SEI.

Dropping a parameter set breaks the whole stream, while keeping an AUD is harmless, so the check is deliberately strict: when in doubt the NAL unit is kept.

Result with the stream from the issue

Before:

avcc: 0000000448ee3c80
fmtp: packetization-mode=1,SO48gA==

After:

avcc: 00000013474d401e99a05825a100000303e800009c40840000000448ee3c80
fmtp: packetization-mode=1;profile-level-id=4d401e;sprop-parameter-sets=R01AHpmgWCWhAAADA+gAAJxAhA==,SO48gA==

Tests

Three new cases in pkg/h264/annexb/annexb_test.go:

  • TestH264SPSWithNalRefIDC2: the SPS and the PPS survive.
  • TestH264SEIWithNalRefIDC2: the SEI survives.
  • TestHEVCAUDIsStillDropped: a HEVC AUD is still removed.

They fail on master and pass with this change. The existing HEVC cases (TestFFmpegMPEGTSHEVC, TestReolink, TestDahua) keep passing, so AUD removal for HEVC sources is unchanged.

go test ./pkg/h264/... -count=1
ok  github.com/AlexxIT/go2rtc/pkg/h264
ok  github.com/AlexxIT/go2rtc/pkg/h264/annexb

go build ./... succeeds. go test ./... shows the same failures with and without this change (pkg/core, pkg/hap, pkg/ivideon, pkg/tuya do not compile, and internal/ffmpeg and internal/streams fail on this machine), so none of them is introduced here.

The same code is present in the dev branch, so the fix applies there too.

@AlexxIT AlexxIT added kind/bug Reports a defect or fixes one size/M PR with 30-99 lines changed, excluding generated files. area/codec/h264 H.264 (AVC) video parsing, packetization, and stream compatibility labels Nov 26, 2026
@AlexxIT AlexxIT added the ai Submission contains text or code generated by AI label Nov 28, 2026
@AlexxIT
AlexxIT merged commit 561419f into AlexxIT:master Nov 28, 2026
1 check passed
@AlexxIT

AlexxIT commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks for the idea! I've refined it a bit.

@AlexxIT AlexxIT added this to the master milestone Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Submission contains text or code generated by AI area/codec/h264 H.264 (AVC) video parsing, packetization, and stream compatibility kind/bug Reports a defect or fixes one size/M PR with 30-99 lines changed, excluding generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

H.264 SPS with nal_ref_idc 2 is dropped as if it were an HEVC AUD

2 participants