AI-friendly documentation for the h264nal project Last updated: 2025-12-29
h264nal is a production-grade C++ library and CLI tool for parsing H.264/AVC (Advanced Video Coding) NAL (Network Abstraction Layer) units. It accepts H.264 Annex B format files and produces human-readable dumps of parsed NAL unit contents.
- Version: 0.25
- License: BSD
- Copyright: Facebook, Inc. and its affiliates
- Repository: https://github.com/chemag/h264nal
- Companion Project: h265nal (for H.265/HEVC parsing)
- Finding a parser: All parsers are in
/include/h264_*_parser.hwith implementations in/src/h264_*_parser.cc - Running tests:
cd build && make test(16+ unit tests) - Building:
mkdir build && cd build && cmake .. && make - Main entry point:
/tools/h264nal.ccfor CLI tool - Bitstream parsing: Start with
/include/h264_bitstream_parser.h
// Level 1: Full Bitstream (Annex B format)
H264BitstreamParser::ParseBitstream()
// Level 2: Single NAL Unit
H264NalUnitParser::ParseNalUnit()
// Level 3: RTP Packets (RFC6184)
H264RtpParser::ParseRtp()Every parser follows this consistent pattern:
class H264[Component]Parser {
public:
// State structure (immutable - deleted copy/move constructors)
struct [Component]State {
[Component]State() = default;
[Component]State(const [Component]State&) = delete;
[Component]State& operator=(const [Component]State&) = delete;
// ... fields ...
};
// Static parsing method
static std::unique_ptr<[Component]State> Parse[Component](
rtc::BitBuffer* bit_buffer, ...);
// Optional formatted dump (if FDUMP_DEFINE)
static void fdump(const [Component]State& state, FILE* outfp);
};h264nal/
|-- include/ # 28 public header files
|-- src/ # 27 implementation files (.cc)
|-- tools/ # CLI tool (h264nal.cc)
|-- test/ # 23 unit test files
|-- fuzz/ # 18 auto-generated fuzzers + corpus
|-- media/ # Test video files and documentation
|-- ya_getopt/ # Windows getopt port
+-- build/ # CMake generated files
h264_nal_unit_parser.h- Master NAL unit parserh264_nal_unit_header_parser.h- NAL unit headersh264_nal_unit_payload_parser.h- Payload dispatcher
h264_sps_parser.h- Sequence Parameter Set (SPS)h264_pps_parser.h- Picture Parameter Set (PPS)h264_subset_sps_parser.h- Subset SPS (scalable coding)h264_sps_extension_parser.h- SPS extensionsh264_sps_svc_extension_parser.h- SVC SPS extensions
h264_slice_header_parser.h- Slice headersh264_slice_layer_without_partitioning_rbsp_parser.h- Slice data layerh264_slice_layer_extension_rbsp_parser.h- Slice extensionsh264_slice_header_in_scalable_extension_parser.h- SVC slice headers
h264_rtp_parser.h- RTP dispatcherh264_rtp_single_parser.h- Single NAL unit RTP packetsh264_rtp_stapa_parser.h- STAP-A (Single-Time Aggregation)h264_rtp_fua_parser.h- FU-A (Fragmentation Units)
h264_hrd_parameters_parser.h- Hypothetical Reference Decoder paramsh264_vui_parameters_parser.h- Video Usability Informationh264_ref_pic_list_modification_parser.h- Reference picture listsh264_pred_weight_table_parser.h- Weighted predictionh264_dec_ref_pic_marking_parser.h- Reference picture markingh264_prefix_nal_unit_parser.h- Prefix NAL units
h264_common.h- Enums, constants, type definitionsh264_utils.h- Utility functionsrtc_common.h- Bit buffer operations (from WebRTC)h264_bitstream_parser_state.h- Persistent SPS/PPS state
- Build System: CMake 3.10+
- C++ Standard: C++14 (Linux/Mac), C++20 (Windows)
- Main Files: Root
CMakeLists.txtplus subdirectory configs
-g -O0 -Wall -Wextra -Wunused-parameter -Wshadow -Wformat
-Wextra-semi -Wsign-conversion -Werror
-Wextra-semi-stmt (Clang only)FDUMP_DEFINE // Enable fdump() diagnostic output
RTP_DEFINE // Enable RTP parsing
FPRINT_ERRORS // Enable error printing
SMALL_FOOTPRINT // Minimal feature build- gtest-devel - Unit testing
- gmock-devel - Google Mock
- llvm-toolset/clang - Fuzzing support
- WebRTC - Bit buffer (integrated, no external dep)
# Standard build
mkdir build && cd build
cmake ..
make
make test
# With options
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
cmake -DBUILD_H264_TESTS=OFF ..
cmake -DBUILD_CLANG_FUZZER=OFF ..All tests in /test/ directory using Google Test framework.
Key Test Files:
h264_bitstream_parser_unittest.cc- End-to-end bitstream testsh264_sps_parser_unittest.cc- SPS parsingh264_pps_parser_unittest.cc- PPS parsingh264_slice_header_parser_unittest.cc- Slice headersh264_rtp_*_parser_unittest.cc- RTP variants- And 18 more component-specific tests
Running Tests:
cd build
make test # All tests
./test/h264_sps_parser_test # Individual test- Framework: libfuzzer with ASAN/UBSan
- Generation: Auto-generated from unit tests via
/fuzz/converter.py - Corpus: Available in
fuzz/corpus/submodule - Markers: Code tagged with
// fuzzer::conv: begin/end
Building Fuzzers:
cmake -DBUILD_CLANG_FUZZER=ON ..
make
./fuzz/h264_sps_parser_fuzzerExecutable: /tools/h264nal
# Simple parse
./h264nal file.264
# Formatted output with metadata
./h264nal file.264 --noas-one-line --add-offset --add-length --add-parsed-length
# Get resolution
./h264nal file.264 --noisy-parsing
# Explicit NAL length framing
./h264nal file.264 --nalu-length-size 4--noas-one-line- Multi-line formatted output--add-offset- Show byte offset--add-length- Show NAL unit length--add-parsed-length- Show parsed bytes--add-checksum- Add CRC32 checksum--noisy-parsing- Debug output
Immutable States:
struct SpsDataState {
SpsDataState() = default;
SpsDataState(const SpsDataState&) = delete; // No copy
SpsDataState& operator=(const SpsDataState&) = delete;
// Fields...
};Persistent State:
// Maintains SPS/PPS across NAL units
H264BitstreamParserState state;
H264BitstreamParser::ParseBitstream(data, len, &state);All code in h264nal namespace (flat, no nesting).
// WebRTC BitBuffer for bit-precise reading
rtc::BitBuffer bit_buffer(data, length);
// Exponential Golomb codes
bit_buffer.ReadExponentialGolomb(&value); // ue(v)
bit_buffer.ReadSignedExponentialGolomb(&value); // se(v)
// RBSP de-escaping handled automatically- Returns
nullptron parse errors - Optional error printing with
FPRINT_ERRORS - Validation against H.264 spec constraints
The code implements:
- ITU-T H.264 (08/2021)
- ISO/IEC 14496-10 (AVC specification)
- RFC 6184 (RTP Payload Format for H.264)
Comments reference specific sections, e.g., "// 7.3.2.1.1"
From README Section 8:
- No STAP-B, MTAP16, MTAP24, or FU-B RTP modes
- SEI (Supplemental Enhancement Information) parser not implemented
- Some auxiliary parsers missing (see TODO)
From git history and status.md:
- Fixed -Wextra-semi-stmt warnings (Clang)
- Resolved 242 format specifier warnings
- Fixed 138 + 10 extra semicolon warnings
- Sign conversion warnings addressed
- Centralized compiler flags in CMakeLists
- Create header in
/include/h264_new_parser.h - Implement in
/src/h264_new_parser.cc - Add unit test in
/test/h264_new_parser_unittest.cc - Add fuzzer markers to test
- Regenerate fuzzers:
cd fuzz && ./converter.py - Update CMakeLists.txt files
- Check current warnings:
make 2>&1 | tee warnings.txt - Update CMakeLists.txt compiler flags
- Fix code systematically by component
- Verify tests still pass
- Document in
status.mdorwarn.md
cd build
# Single parser test
./test/h264_sps_parser_test
# With verbose output
./test/h264_sps_parser_test --gtest_verbose
# Specific test case
./test/h264_sps_parser_test --gtest_filter=H264SpsParserTest.TestSample- Headers:
h264_component_parser.h - Sources:
h264_component_parser.cc - Tests:
h264_component_parser_unittest.cc - Fuzzers:
h264_component_parser_fuzzer.cc
All files use snake_case.
- Linux: Primary development platform
- macOS: Fully supported
- Windows: Supported with ya_getopt library
- Getopt: Conditional
ya_getopton Windows - Sockets:
winsock2.hon Windows - Designated initializers: C++20 required on Windows
- Comprehensive test coverage (23 unit tests)
- Fuzzing infrastructure for security
- Strict compiler warnings (
-Werror) - Production use (Facebook copyright)
- Active maintenance (recent v0.25)
- Clean API with multiple integration levels
- Standards-compliant implementation
# Full build and test
make clean && cmake .. && make && make test
# Check for warnings
make 2>&1 | grep -i warning
# Run fuzzer with corpus
./fuzz/h264_sps_parser_fuzzer fuzz/corpus/h264_sps_parser_fuzzer/
# Format check (if using clang-format)
find src include -name "*.cc" -o -name "*.h" | xargs clang-format -i
# Parse test file
./tools/h264nal media/test.264 --noas-one-lineREADME.md- Main project documentationmedia/README.md- Test video documentationfuzz/README.md- Fuzzing infrastructurestatus.md- Recent build improvementswarn.md- Warning analysis (23,081 warnings)LICENSE- BSD license
- Repository: https://github.com/chemag/h264nal
- Issues: File on GitHub
- Related: h265nal for HEVC/H.265
This document is designed to help AI assistants and developers quickly understand the h264nal codebase structure, patterns, and common tasks.