我如何解析H264文件和帧

时间:2022-06-14 21:22:07

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?

H264文件是NAL(网络抽象层)单元的流,每个单元编码帧(I,B或P)。解析此文件以及提取文件中每个NAL单元的大小和检测结束的最佳方法是什么,以及检测NAL单元包含的帧类型?

1 个解决方案

#1


20  

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.

如果您实际上没有尝试解码帧,可以通过读取h.264字节流并查找NAL单元签名来编写一个简单的“解析器”。

Here's what you need to know:

这是你需要知道的:

  • NAL Units start code: 00 00 01 X Y
  • NAL单位起始码:00 00 01 X Y.
  • X = IDR Picture NAL Units (e.g 25, 45, 65)
  • X = IDR图片NAL单位(例如25,45,65)
  • Y = Non IDR Picture NAL Units (e.g. 01, 21, 41, 61)
  • Y =非IDR图片NAL单位(例如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.

因此,如果您按顺序找到3个字节[00 00 01],很可能它是NAL单元的开头。然后,您需要解析接下来的两个字节[X Y]以找出帧的类型。有关详细信息,请参阅规范。

#1


20  

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.

如果您实际上没有尝试解码帧,可以通过读取h.264字节流并查找NAL单元签名来编写一个简单的“解析器”。

Here's what you need to know:

这是你需要知道的:

  • NAL Units start code: 00 00 01 X Y
  • NAL单位起始码:00 00 01 X Y.
  • X = IDR Picture NAL Units (e.g 25, 45, 65)
  • X = IDR图片NAL单位(例如25,45,65)
  • Y = Non IDR Picture NAL Units (e.g. 01, 21, 41, 61)
  • Y =非IDR图片NAL单位(例如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.

因此,如果您按顺序找到3个字节[00 00 01],很可能它是NAL单元的开头。然后,您需要解析接下来的两个字节[X Y]以找出帧的类型。有关详细信息,请参阅规范。