C++ – How to i parse H264 file and frames

c++h.264visual-c++

An H264 file is a stream of NAL (Network Abstraction Layer) units, each encoding a frame (I, B, or P). What is the best way to parse this file and to extract sizes and detect ends of each NAL unit in the file, as well as detect the type of frame the NAL unit contains?

Best Solution

If you're not actually trying to decode the frames, you can write a simple 'parser' by reading the h.264 byte stream and looking for NAL unit signature.

Here's what you need to know:

  • NAL Units start code: 00 00 01 X Y
  • X = IDR Picture NAL Units (e.g 25, 45, 65)
  • Y = Non IDR Picture NAL Units (e.g. 01, 21, 41, 61)

So, if you find 3 bytes [00 00 01] in sequence, very likely it's the beginning of the NAL unit. Then you will need to parse the the next two bytes [X Y] to find out the type of frame. Please refer to the spec for more details.