当前位置:网站首页>Annexb and avcc are two methods of data segmentation in decoding

Annexb and avcc are two methods of data segmentation in decoding

2022-07-07 15:41:00 When can Xiaobai advance to success

at present H.264 There are two popular ways of packaging , One is called AnnexB, One is called avcC. For both formats , The degree of support of each family is also different , for example ,Android Hard decoding MediaCodec We only accept AnnexB Formatted data , and Apple Of VideoToolBox, Only support avcC The format of . So this requires our practitioners to have an understanding of both formats . This chapter , Let's introduce AnnexB

1、AnnexB

1.1 Introduce

If we put more NALU Write it in a file , Multiple NALU The first one is connected and threaded into a string , because NALU Its length is different , There is no specific identifier to indicate that you are an independent NALU, When we read this file, there is no way to write it together NALU Effectively distinguish . To solve this problem , We have to give NALU Add some data , Will all NALU Segmentation . AnnexB It's used to deal with NALU A format in which layers are packed .

AnnexB The principle of format is very simple , It's in one NALU Add three or four bytes to the front , The contents of these bytes are 0 0 0 1 perhaps 0 0 1. When we read a H264 When it's flowing , Once encountered 0 0 0 1 perhaps 0 0 1, We think of a new NALU Here we go , therefore , These bytes used as separators , It is also commonly known as start code, Start code .

1.2 Anti contention byte (Emulation Prevention Bytes)

But only in NALU Adding the start code in front of it will cause problems , Because in the original code stream , It is possible that 0 0 0 1 perhaps 0 0 1 Of , This will cause the reader to send a NALU Wrong segmentation into multiple NALU. To prevent this from happening ,AnnexB Anti contention bytes are introduced (Emulation Prevention Bytes) The concept of .

The so-called anti contention byte (Emulation Prevention Bytes), It's just for you NALU Before adding the start code , First traverse the code stream , Find the existing in the code stream 0 0 0,0 0 1,0 0 2,0 0 3 Bytes of , Then modify it as follows

0 0 0 => 0 0 3 0
0 0 1 => 0 0 3 1
0 0 2 => 0 0 3 2
0 0 3 => 0 0 3 3
 Copy code 

That is, above 4 Under different circumstances , stay 0 0 after , Insert a byte , The content is 3. The code stream processed in this way , No longer with the start code (0 0 1, 0 0 0 1) Conflict due to repetition .

Of course , During decoding , Successfully split through the start code NALU After the data , Also remove the anti contention bytes .

0 0 3 0 => 0 0 0
0 0 3 1 => 0 0 1
0 0 3 2 => 0 0 2
0 0 3 3 => 0 0 3
 Copy code 

Only in this way can we get real NALU stream .

Two 、avcC

AnnexB The principle is in every NALU Write a special start code in front , Use this starting code as NALU The delimiter , Thus dividing each NALU. and avcC In another way . That is in NALU Write a few bytes in front , These bytes form an integer ( Big endian byte order ) This integer represents the whole NALU The length of . When reading , First read out this integer , Get this NALU The length of , Then read the whole by length NALU.

2.1 avcC Detailed explanation

Introducing avcC Before the format , Let's first introduce two special NALU, these two items. NALU Namely SPS and PPS,SPS and PPS Stored decoding all the way H.264 Necessary parameter information of code stream , in other words , You want to decode all the way H.264, You must first get SPS and PPS. In a later lesson , We will introduce in detail SPS and PPS, Now all you need to know is ,SPS and PPS Are two special and important NALU.

stay AnnexB in ,SPS and PPS It is regarded as ordinary NALU To deal with ; And in the avcC in ,SPS and PPS Information is treated as special information .

Adopt in one way avcC packaged H.264 In the stream , The first thing we will see is a passage called extradata The data of , This data defines this H.264 Basic attribute data of flow , Of course , Also includes SPS and PPS data .

Let's take a look extradata data format

bits      
8   version ( always 0x01 )  
8   avc profile ( sps[0][1] )  
8   avc compatibility ( sps[0][2] )  
8   avc level ( sps[0][3] )  
6   reserved ( all bits on )  
2   NALULengthSizeMinusOne    //  This value is ( Prefix length -1)
3   reserved ( all bits on )  
5   number of SPS NALUs (usually 1)  
        repeated once per SPS:  
16  SPS size  
        variable SPS NALU data  
8   number of PPS NALUs (usually 1)  
        repeated once per PPS  
16  PPS size  
        variable PPS NALU data 
 Copy code 

  Let's take a look at this value NALULengthSizeMinusOne, By adding this value to 1 , We've come to the conclusion that each of the following NALU Prefix before ( It's an integer that represents the length ) Bytes of

for example , This NALULengthSizeMinusOne yes 3, Then each NALU The length of the prefix is 4 Bytes . When we read subsequent data , You can read it first 4 Bytes , And then turn these four bytes into integers , This is this. NALU The length of the , Be careful , This length does not include the starting 4 Bytes , It's simple NALU The length of .

原网站

版权声明
本文为[When can Xiaobai advance to success]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130611524519.html