H.264中NALU、RBSP、SODB的关系 (弄清码流结构)

来源:互联网 发布:手机怎么设计软件 编辑:程序博客网 时间:2024/05/22 06:15

http://blog.csdn.net/stpeace/article/details/8221945


NALU:Coded H.264 data is stored or transmitted as a series of packets known as NetworkAbstraction LayerUnits. (NALU单元)

      RBSP :A NALU contains a Raw Byte Sequence Payload, a sequence of bytes containingsyntax elements.(原始数据字节流)

      SODB:String OData Bits (原始数据比特流, 长度不一定是8的倍数,故需要补齐)

 

      逻辑关系:

 

                                                  SODB  + RBSP trailing bits    =  RBSP

                                                       NAL header(1 byte)      +      RBSP   = NALU

     Start Code Prefix(3 bytes)  +   NALU  +  Start Code Prefix(3 bytes)  +   NALU   + ...+  = H.264BitsStream

 

     说明:

    1. SODB即编码形成的真实码流,为了使一个RBSP为整字节数,需要加trailing bits, 具体加的方法可以看JM8.6中的SODBtoRBSP函数.

[cpp] view plaincopy
  1. void SODBtoRBSP(Bitstream *currStream)  
  2. {  
  3.   currStream->byte_buf <<= 1;  
  4.   currStream->byte_buf |= 1;  
  5.   currStream->bits_to_go--;  
  6.   currStream->byte_buf <<= currStream->bits_to_go;  
  7.   currStream->streamBuffer[currStream->byte_pos++] = currStream->byte_buf;  
  8.   currStream->bits_to_go = 8;  
  9.   currStream->byte_buf = 0;  
  10. }  


 

      2. NALU header为一个字节,这8个比特分别对应forbidden_zero_bit, nal_ref_idc, nal_unit_type. NALU的body其实就是RBSP. 由RBSP转NALU是由RBSPtoNALU函数来实现的.

[cpp] view plaincopy
  1. typedef struct   
  2. {  
  3.   int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)  
  4.   unsigned len;                 //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)  
  5.   unsigned max_size;            //! Nal Unit Buffer size  
  6.   int nal_unit_type;            //! NALU_TYPE_xxxx  
  7.   int nal_reference_idc;        //! NALU_PRIORITY_xxxx  
  8.   int forbidden_bit;            //! should be always FALSE  
  9.   byte *buf;        //! conjtains the first byte followed by the EBSP  
  10. } NALU_t;  
[cpp] view plaincopy
  1. int RBSPtoNALU (char *rbsp, NALU_t *nalu, int rbsp_size, int nal_unit_type, int nal_reference_idc,   
  2.                 int min_num_bytes, int UseAnnexbLongStartcode)  
  3. {  
  4.   int len;  
  5.   
  6.   // 断言,以后要学会用assert进行断言,很重要滴.  
  7.   assert (nalu != NULL);  
  8.   assert (nal_reference_idc <=3 && nal_reference_idc >=0);  
  9.   assert (nal_unit_type > 0 && nal_unit_type <= 10);  
  10.   assert (rbsp_size < MAXRBSPSIZE);  
  11.   
  12.   // 下面这个是必须的,所以不需要通过参数传进来  
  13.   nalu->forbidden_bit = 0;  
  14.   // 下面两个通过参数传进来  
  15.   nalu->nal_reference_idc = nal_reference_idc;  
  16.   nalu->nal_unit_type = nal_unit_type;  
  17.   
  18.   // 判断是否在Start Code Prefix前面加Ox00  
  19.   nalu->startcodeprefix_len = UseAnnexbLongStartcode?4:3;  
  20.     
  21.   // 对nalu->buf[i]进行赋值  
  22.   nalu->buf[0] =  
  23.     nalu->forbidden_bit << 7      |  
  24.     nalu->nal_reference_idc << 5  |  
  25.     nalu->nal_unit_type;  
  26.   memcpy (&nalu->buf[1], rbsp, rbsp_size);  
  27.   
  28. // printf ("First Byte %x\n", nalu->buf[0]);  
  29. // printf ("RBSPtoNALU: Before: NALU len %d\t RBSP %x %x %x %x\n", rbsp_size, (unsigned) nalu->buf[1], (unsigned) nalu->buf[2], (unsigned) nalu->buf[3], (unsigned) nalu->buf[4]);  
  30.   
  31.   len = 1 + RBSPtoEBSP (&nalu->buf[1], 0, rbsp_size, min_num_bytes);  
  32.   
  33. // printf ("RBSPtoNALU: After : NALU len %d\t EBSP %x %x %x %x\n", rbsp_size, (unsigned) nalu->buf[1], (unsigned) nalu->buf[2], (unsigned) nalu->buf[3], (unsigned) nalu->buf[4]);  
  34. // printf ("len %d\n\n", len);  
  35.   nalu->len = len;  
  36.   
  37.   return len;  
  38. }  

 

      3. Start Code Prefix为3个字节. 但是,为了寻址方便,要求数据流在长度上对齐,因此H.264建议在Start Code Prefix前面加若干个0.    

      4. 为了简便起见,上面的逻辑关系图没有考虑"防止竞争"机制.

 

     编码foreman_part_qcif.yuv的第一帧,码流如下: (对照trace_enc.txt分析即可,由于码流太多,篇幅有限,故不一一分析)

000000000000000000000000000000010110011101000010000000000001111011110001011000010110001001100010000000000000000000000000


0 0
原创粉丝点击