Skip to content

Reading the RTMP key frame type from the NAL header - #97

Open
erkin-io wants to merge 2 commits into
OpenIPC:masterfrom
erkin-io:fix/rtmp-keyframe-nal-header
Open

erkin-io wants to merge 2 commits into
OpenIPC:masterfrom
erkin-io:fix/rtmp-keyframe-nal-header

Conversation

@erkin-io

@erkin-io erkin-io commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What was wrong

RTMP sends no video on Fullhan V200: the camera connects, sends the metadata, and then never sends the video sequence header, so the server or player gives up ("could not find codec parameters"). Seen on a Ruision F8 board (FH8856V200) with FFmpeg as the RTMP listener.

Why

rtmp_ingest_video() (src/rtmp.c) waits for a slice whose type in the pack's NAL table is NalUnitType_CodedSliceIdr (5) before it sends the sequence header. The Fullhan V200 HAL fills that table from the encoder's own stream record, and the key-frame slice does not arrive as type 5 there, so the first key frame never comes and every slice is skipped.

What this changes

For H.264, the slice type is also read from the NAL header byte after the start code (& 0x1F). A NAL counts as a slice, and as a key frame, if either the table or the header says so. H.265 is unchanged.

How it was checked

  • Built with ./build.sh fhan-v2, no new warnings; ./test.sh -a passes.
  • On a Ruision F8 board, in the firmware that normally runs on the board, with this change in the same form: FFmpeg listening for RTMP received 2560x1440 H.264, 301 frames in 12 s with no decode error (and 300 frames with 332 MP3 packets in an MP3 run). Before, no frame at all.

Effect on other platforms

Shared code. On HALs whose table already says 5, the header says 5 as well, so nothing changes. The header is read only when the NAL starts with a 3- or 4-byte start code and is long enough.

Update after the automatic review

A second commit removes the Annex-B start code from slices before they are written to FLV, as flv.c already does for SPS and PPS. Before, every slice carried it (274 of 274 in a 12 s capture); after, none, with all 274 frames decoded.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Detect H.264 RTMP keyframes from NAL headers

🐞 Bug fix 🕐 10-20 Minutes

Grey Divider

AI Description

• Detect H.264 slices from NAL headers when HAL-provided types are unreliable.
• Recognize Fullhan V200 keyframes so RTMP can send the video sequence header and frames.
• Preserve the existing H.265 classification path.
Diagram

graph TD
  hal["HAL video pack"] --> classify["H.264 slice check"] --> gate{"IDR detected?"} -->|Yes| header["FLV sequence header"] --> output["RTMP output"]
  gate -->|No, before header| wait["Wait for IDR"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Correct types in the Fullhan V200 HAL
  • ➕ Contains the workaround within the affected platform.
  • ➖ Leaves RTMP dependent on every HAL reporting accurate slice types.

Recommendation: The shared RTMP fallback is preferable because it recognizes the encoded slice even when a HAL reports an unreliable type, while retaining the table-based behavior. Review the no-start-code path carefully: the implementation also interprets the first byte as a NAL header when no start code is found.

Files changed (1) +14 / -3

Bug fix (1) +14 / -3
rtmp.cRecognize H.264 RTMP slices using NAL header types +14/-3

Recognize H.264 RTMP slices using NAL header types

• The H.264 ingestion path checks the NAL header byte, accounting for three- or four-byte start codes, and treats either the header or HAL table as evidence of a slice or IDR. This allows a Fullhan V200 keyframe to trigger the RTMP video sequence header; H.265 logic is unchanged.

src/rtmp.c

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Some recovered frames carry invalid video data ✓ Resolved

Resolving commit: ce0869a

Resolution reason
Before passing the NAL to flv_set_slice, the code now strips a recognized 3- or 4-byte Annex-B start
code from nal_start and nal_len (guarded by nal_len > 4 and leading zero bytes), so the FLV writer
no longer embeds the start code in the length-prefixed payload.
Description
rtmp_ingest_video() accepts a slice identified by its header but later passes the original
nal_start and nal_len to flv_set_slice() without removing a detected Annex-B start code. When
a prefixed H.264 slice is newly accepted because its table type is wrong, the FLV writer includes
that start code inside its length-prefixed NAL payload, which can prevent a receiver from decoding
the frame.

Grey Divider

Tip of the day
💡 Did you know, you can enable the Remediation agent and Qodo fixes findings in a dedicated fix PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

FLV carries each NAL unit behind a 4-byte length. The slices were written with their Annex-B start code still in front (all 274 in a 12 s capture); they are now written without it, as the SPS and PPS already are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@erkin-io

Copy link
Copy Markdown
Contributor Author

Updated after the automatic review (second commit).

Start code inside the FLV payload: correct, and it was so for every slice before this change too: the slices went to flv_set_slice() with their Annex-B start code. Measured on a Ruision F8 board (FH8856V200) with FFmpeg as the RTMP listener, counting the NAL units in the FLV video tags: before, 274 of 274 began with 00 00 00 01; after, 0 of 274, with 274 frames decoded and no decode error. FFmpeg tolerated the old stream; stricter servers may not.

The second commit removes a 3- or 4-byte start code in front of the slice before it is written, in the same way the SPS and PPS setters in flv.c already do. It applies to H.264 and H.265 slices; the H.265 type checks are unchanged.

🤖 Generated with Claude Code

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