关于H264中的startcode(干货)

startcode的两种形式

3字节的0x000001和4字节的0x00000001
引用一段H264标准(Annex B)的说明:

x264 uses 3 byte start codes where the spec says those start codes can be used.
4 byte start codes must be used at the start of SPS, PPS, and in the first NAL of a frame.
leading_zero_8bits is a byte equal to 0x00.
NOTE – The leading_zero_8bits syntax element can only be present in the first byte stream NAL unit of the bitstream, because(as shown in the syntax diagram of subclause B.1.1) any bytes equal to 0x00 that follow a NAL unit syntax structure and precede the four-byte sequence 0x00000001 (which is to be interpreted as a zero_byte followed by a start_code_prefix_one_3bytes) will be considered to be trailing_zero_8bits syntax elements that are part of the preceding byte stream NAL unit.

为什么需要startcode

       主要是为了将相邻两个NALU划分开,让他们有一个界线,方便解码,比如将h264的数据存储在一个文件当中,解码器无法从数据流中分别每个NALU的起始位置。

       在编码时,每个NALU前面添加startcode(占4字节0x00000001或者3字节0x000001),这里有人会想到万一中间出现0x000001怎么办呢,h264有个防止竞争的机制,在编码完一个NAL时,如果出现有连续两个0x00字节,就在后面插入一个0x03(解码的时候这个0x03会被丢弃)。

起始部分占4字节还是3字节?

       实际上startcode只占3字节,4字节的起始部分 = zero_byte + start_code_prefix_one_3bytes,就是说无论啥时候其实startcode都是3字节,关键就在于zero_byte。

1、包含sps,pps的NALU前面要加zero_byte(4字节)。
2、当一帧被分为多个slice时,首个NALU前面要加zero_byte(4字节)也就是,当一个完整的帧被编为多个slice的时候,除掉第一个NALU,剩下的都用3字节的,其余的都是4字节。
3、

SPS (4字节头)
PPS (4字节头)
SEI (4字节头)
I0(slice0) (4字节头)
I0(slice1) (3字节头)
P1(slice0) (4字节头)
P1(slice1) (3字节头)
P2(slice0) (4字节头)
P2(slice1) (3字节头)

       I0(slice0)是序列第一帧(I帧)的第一个slice,是当前Access Unit的首个nalu,所以是4字节头。
       而I0(slice1)表示第一帧的第二个slice,所以是3字节头。P1(slice0) 、P1(slice1)同理。

  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值