网站首页 > 物联资讯 > 技术分享

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

2016-09-28 00:00:00 广州睿丰德信息科技有限公司 阅读
睿丰德科技 专注RFID识别技术和条码识别技术与管理软件的集成项目。质量追溯系统、MES系统、金蝶与条码系统对接、用友与条码系统对接

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 Of Data 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分析即可,由于码流太多,篇幅有限,故不一一分析)

000000000000000000000000000000010110011101000010000000000001111011110001011000010110001001100010000000000000000000000000000000010110100011001000101000010100001110001000000000000000000000000000000000010110010110001000100001000000001001100011011000010111110000000000000111010110111011111100000000000111001111100000110000010111011100110000111100100001010000111011011001011010010001001001011101010011111000010110000001101001100000000111111111111001111100101111100101100000000010101001101010010111010011100110011011100000100101010101000110100000010110010001011001011100100001001010110011100010000001001111110000000100001000001011001001001100011010101100001001001111000001110000000010010000011101001000110111111001000100011101101010011111010100001010011011110100001110101101011111100110010100110010011110001011000000111001001000001100000000101110100011011010010011111001011100111000000100000101000111010100001000101000111001011111111100011100011101110111011101111110010011100101111000000110110100110110111000000010110101001111010000101100101100100001010010111101101111011111000001011010101011000000110000001001010110101101110001000000000111001001100110111000110010011010101010010011110100000110111110011101011010101110111001110010010000011100101100010111010000101000101101001101011111111111100000000100000010000100100100110010001111111011111000110010011111010001111111101110111110111100111111111011101111010100110000100011111001010001111111111110001100011010010001111101011000001101000110001111011011101011001010111010100101110000101100100110010100000010110010110100100110111000110000100100011110000011011000111001100001010010011011001110101011001100111100000111010001000101111111101110111011100000111101110111110111101011110101000000010000000010011000001011000111111111000100111011010011111111010000001000110100001010001110111000101100000011001110001111111101000100110010111111111111001111101100100010011110010100010100001001111010101111010111111110110010101111010000011110001010110011011100111101111011101111101110111111001111111101010000000100100000100110001011001110010111100000110001001110100010101101111111111100100011011110100100000001111100100111110000010000010011000001101111100001100011010010000100011101010010010111111000110011110001011010100111110000011101110010110000111100001011001111101100001010011110000010111110001101111101111100010101111111111000010110101011100101111101111101110001111001101110011111011010011000111110011100001101001110111111101010011110011001110101111010100111100110111000000000011101111011010110110000100111001001100011011001110101010011001101110011101101000000110011011001010000100111100000100011100001001101111001000001010101100101101111101100001000111111001101011010111100010001111101011001001011011110110001100011011110011010011010011111111001110101011111001111111000000101000101011000011011010100110101110010010110111110011001111011110100101101101101110110010100100101011011011001011010010000110110111101101111010100100010100011011011001110101011010011100101010001100101110011001111001111001110100101001110111000011101010010100000111001011100000101110110011101111100101100100011000000000001000011011000011001011101011110110111010100100000110001010110011011001100111011100101111100010101110101111000110010000100010111111110000111110001001110001011101111000111000101111001111010010011110010100110011110111000000000101000001000101001000010101100000111110110100111110010011100010011100001000100000001111001001011111010011000101111011000000101000000010010100010001001110110011101000001001101010100011101001011111111110101111000110011111000001000110011100110110101010111100000001100010011101100101110010011100110011111001111110011110100101010111011111001000011011000111010010111110110000010110111010101000101110010011010110011100100110100111101011100100000111110100000000010011001010101000110000010000001011011011010100111100100110010110001000101000001000111100000001001101111010001111000010100100111010110011010110111011000101111010001111011001111101000111110010110111011001011010010100110000010000100110011110001111111100101100101010100100111101011010111101100000101000000110111100111000010000011111011101100110101100011001001101101100001100100000001000011101101111001001101010001011011000001111110110111110111111011011111010011111110011010000110110110000000010001001101101001000011010010110110000110100011111110001010011111011011110111000000001000010101101100000010110001111111101000100110001100001100010111000000100001100010111111001000001000101111100101111110000111011001001110011001010110101111011110111011101110111111010101010100111000000010000000010110000101100010111111100010000000010100010110001011001011001100110011000011111000101101001000111101100000111111111111000101010011110011111010000110100010011010010011001100110011110011001001100101011100000001101111001110111010000011110001010110101011101000010101101011100101101110111011110000011100011111111111001011101111001001111010100011100111110100011011110000011000011011110010010000000111110010011111000101111111010101100001111010111000010001000001011001101011111000000001100111110011111001101110111101011011100111101011111111011010000101000101111000001101011111111111000010111101011110000000001100000011011001100010100011100111010001101111111111111111111111000010111101100101111101011111011110101101110010001010100111010010011011011111010011110100110101101011010110100011101111111001000011110000110010101011110001110000100110100010100111010001010101011101111101001011101001111011110001010011000101101010011001111100100011010001001111100001001101101111110101111000110010010001110101110110101111000000111011011111101101111000011111001001100110110111110101000110111011011111010110010101100110110101001100101110111001100111001111111110011000001001010011101110010100010110001011100100111111100000011101110001010101000010010101101111110111101011111101001010010110101110111010001011101111000110001111001101100111000011101011100111101000110000010001111110001111110101111001101001111010010101111100001001110111000000000110001001010011111001110011111110111101011111101100100001000001000100001111000111101111000111100000011000111100111001101011011111001011101001010110011100101001100111010001111000010100101111000001001011110111110011010111100001000010000001001111011010011100111111100000001000000010111001000101000010000111101111001101011001001100111000100111101011000111011010100010000100001111011111011111000001110101111100000001100001001010010111001110111001001000111100000011110111110000000100100011110100110111111001001001000100101101111101110000111111111111111111101110111011101111000101101011011111111000011101010011110011000111000000001010001011010100011100110010011001100111000000111011111011011101110111110100100001101000111011100000100001100101011100101101101010001100000111110110100101000101010011111011110111011110111101111110010110101011110000100001010011000000101100011111111000011001101000100010000000000101000111010100111001100000110101011111000101001110111111000011010101001010001110000011010010100111000100000100011000010010000111110100011111100001001010100100010011100010011011101101000111111111111001110101111101100101101111110001110100101101110110001110110111011101111011011111010110110000011001110100110111110111001100111001010100010001010000100011111101101001101000011111011111011001111111101111110011110101111111000011011110101011101111111111111110111011111011101111000011101111000101111011111110010100011110100011101111100000010011100010100111101001010110110000110001011000011110011100110010110100110001011011110110110110111111111110000000011000000001101000001010111000000011101000000001000110100010010001110111001110011110110100100011111111110000000011000000001101000001110111001101011000100111100001111000100100000110111000011101100011001001111010101011100101110111111110001110000101001000000001101011100000100000100001010111111010011100110010100010000010111001010100000101111000110101111000100010110010111100101000111100010100000110101101101111111010111001111110101001010101111101101111011101001001110101010110011001000101101101101111000110111010111100001011001010011101011001110010111100100001010110101111010010100110010010000101011001011001111100111111100101001110001001100110000100111011101011100001100001010111010110011101110111011000011000001001110110100010001101000110010110100011010111010101001110001111110111000110111001110000100101011011101000111110011111011110010011001000110100001110011100111110110011101000100100001101110110011011010011110000100010011110011100110011101110000101100110110000101111001111100010001001101001100001100110101011110111100111111000111000101100110010111011010100111101010100100110011110001110000111111110101101101011111001101000101100111011100011100101010101100110001000011111011110111111111100100000111000010111101111000001110000100101111001011000000001110001110001001000111010100100011111000101000011110100011101010010111011101000100110100010011101010111100001011011100110101110101101111101100111010001110001101000010101100101010111010101010000001100001110111111000000011000001100011101110000101011100000110101111001110010111111101111111111111010000010110101101111001001000000111110110111111000100100001001101110011110011011101111111001111011111101110011111110111111000101111111111111111110100000101011111011101110010111110000000100010111010110110000110011000011001010111011100100010011000001011001101010111100101110000000001001000000011100001000100000111001011110000000011011100001110100010010010100001000000101100110011100101100011110101001111101001001010101111011000111011110011101100111001010001111110010111100001010000000101010010100010111100000000010001000011000001011001100110101010010011101011011100111111111000001111011111111010000010101011110111111101000111110011110000110010111001000111001111011011001001111001110000011011110111010011011010111011011110010011011001000111011111010000100100101111000000111110011011110011111100010111010010001001100101110101111111111111111101011100010001000110001111001011110111101101010110110101101110101001011110110101011111010011011101100111010110001100101000011011111110101011110001110110101001111010010101100101100100110000110010010111111100001100110110111110001100100101000100101010011101110001000011110100100000001111110110101111111101011000110011010101011010100111010001010100101110110011000110111111011110011100111100100110011101000010011111110010110110011010011011101011101100010111001011111111010100010110110011011101000111010100000100000111000111111011000111011101010110001011011110111111110001011010101110011101111111001111011011110011010001111010111110000000010110110010011010000101000000101110010111110100001011000001101111000100100110110010100011100001110011000010101100100110100001011010110100100101100110111101010101111110110110001101110001101101111100110100011010111110101010010101111101100111010101111010000111010110011111100011110010111111001110100110010101100011110111101011001011001110011001101011100001101101111001101000000101100101100110000010111100001010110011011011000000110100100111100111000010011101110010001100110110110011101001101100111110101001111101001010111110010101000111011010010111001111111000111011010000100001011110011001110101110101011110001111011111001100100001011010011001111011000100100110111011001110000010110110000001000000010010001011101111010100100001000010101111000001110000000101110111100011001111110111110000100010111101001000000010100010001101101001110111100001101001010111000000100001110000110100011000101100101110011101001011110100101001110000100000000110111011100000111010010000010011110110010000000010111000001111110111011000000011101101111101111100000111011100100111010101111110100111110000000010100010110010100011100110010000010101101011111110001111100000010000101101011100110010011001110011100110011101001001011001110111001100100000110010000111001000010110000110011010010011000101100011010001110000100110110110011100010000111111101101101110000111111011110000111000111100000111011110111000011111000111110100011011000011010001011001110111000100111000111001010111000110110101111010010110010010010110101001001000001011010011110000101111101010101010101111110110011001101010101011010000110100100111100111010010110111000000011010000001011111101011000111100110010010001101001001110100100111100111110101110110000101011010101011110101101100010001100011110111100100010110101110010000111111010110100011101000011011110010101101101111010111001100001000000010101011010110111100000111101111100111000011100001011010010111000111001111101111000011011011010011110000001010001111000101011101101111011011000101010010001101010011100001011110111011010111011001001010011100111000111001100101011101000011111110101010101110110010101101110110111110101011101010011010110101101010110110011011001010101111110100101000011101011010011110001100101010101000101001000001100011000011111001101010101110010111001011111101000110000110010100101110001001111101010111111010001010101101111101001101111001101010100101110000000101110110011111100110011010010101001011000111010111101100011111011011000100111010110010001101100001110111000010011111101001011101000100001100110101000110110010000111110011011010111010110011101100000001100110110001110100100000110010010011100110110010100011110100010010011110010111100111111111111011000100111110011010111111110000010101010100110100111100001001011111110111111001110000100111010111000010011010111011110100011110110010101100001011101101000011001111001100101010000100111101111111011000111110000110010101111001110011110110100010101100011101010011111000010101100100000101100110100011011001001110001000011001111111011101010110011000011000011110000110011100000011001111001111111000110010000000001111111101010100110011101001100100000011010000011100100000001101000110100110111010111100011001001101011111100110011000100000111000011011011000010000110010011000101010110011001110100100011000110001110011000000011101101110010001000110101111011001111001101110101000111100011110101101010101000011111111100101110010010110010011111010010011110011000001001010001001101110111100111001110100001110010111000110001111011110010010000000111110110001000100101001111101101101001111111100001011101010101010110101110101101001111010101011001111011111100011100111101100101110000110111011101110001001111001011001101100000101011101101001010101101010000100111101111110101100100001010010010011101111100001000111010111100000101110101110001011100110110100000100011101111111110100111001010110010111101100001111011001011001011000100101010111100010000001111111000001001100111101110101111111101011111110000001011101101010101001101100111001111101010101111101110111101110111011101001101111001011001010000001011001101010101100011010110100101011101001011000010111111000011100010000111101101010010000001011010100011110000110000111001001110111011111010011110111001110111000000100100001101010001100010111001101110101010110000001010100010110000000101100000101110100010010111000010101001010010011110001101010010110110100101000110001011010110101000010110010100110110010010101011000101100101111010101000101000011111101011010011111011100001000001011010110011010111110001011011111010111000111111110110101101010111010000110111000010001111110111001011110101101101110011000110110101111001111001111001110000001111100111010110111000010001001101101000111111101110101011111011001110101010100000010110000110010100001111011001011111101111111011010010011111000000011101011001110111000110010101001010101101100010000011101011100011111110100000110111001110111001011100111000000111000101001010010101111101111110111110111100010001111110000001101111101101001101110111010101110111100001101101101010000111111111101011001111001111110011100010011101110010001010111100111100001001000100110011001110011000001011001010110100011011010100100111100110001110010101011010111000110010101100101010110111101011001100111010000111110100110010101101000010111110000111001000000111010001101101010100100001000011100101000001100000001011010111011111101111010100101101011001101111010111101000001111100010011000011010101011100010001111110000010011010100111110111001101101111001101001010001001110011110010010010100101001110010111111100100111100111110011110100111010011010001100100110010000101010100011001101001101011010010100000010101010110011011101101010101001101000001100111001011000001101110110101000011100111100000010101110111101110101110011111001011010001110111100111111111001101001000010011011100001110100001101100000100100110101010111110100111110000011110101101001100101110111001010100110110001011101110111110011111100011000001010011101001000000101101010101110101010110000001101110000111110001100110010111011100110101110101101011010100011110000111010000010010000001001111111101101101101110010000100111100110111010000000000010111111110010011100010000011001000110001001111000001000001001111001111001111101010111001110110110011011010101011010001011101111101011010101111100111011011001111010001011000110101000010101110001100100111011010000010110010111011000001010011100001110110100111110010001010001100111010001110001001100111111101000001010100111010100001111111101111100111000110011110101101101001101010000101010110001001011010000100010100110110100001011111010011011100110011001011101011011111011111100001110001010100001101000101111011000111000110010011001110111111111100111001111100011101011100010001111011110111111001101010000101100010110100100111001101010111111011100111100111100111001111110101010110001010110100101111101100000000011101100011100100001101111101101101011110001100100110110001111110110000000111010101011101110001100101010010000100101001111110100011101000010010110111101111101011111111110011000011001110101001111001111010110111111011110111101100011000000110111110110010000110111011101010111011110100001100010000000111101100011101010000111111000011101111101110011011101111111110000100001011011100001000010011111100001011010001100101000001110111010010011100001000111101111010110111111001010100001000011001101101111110011001010011111111111101000010110011110100111100000000000101001000001000111100011001010101110111011101101111010001001011101010101010010100010010100111111110100010101101101101011001101010001100000110110011111010110101100110111111111000111011011100111110101110010001101000100101000000001111010101000111100010100111001001100010101100010010110101101100011100111110011011110011110010111000110110011100111001010111010001101111110101000100101101101011010000101111101010100101110100111001101110100001110000110111100000001001100110111001101000001101010110000111101100001101010001010000001110001001000000100101101000110110110010011111110110111010011001111010111001101000100101100101101000101101011001010101111000100111110110010001111001110000111100111001111000101010100010100011011001010101011110101110100100010011010001110001100100001111110110110110011010011110001101110110011001111100111011101100001001110101111100010011011100001111010101001100100110101011101010011101001000011110000111110001010001101000010101011011100000001011000111111100110101011010110110110011100010011100000001101011101011011010101100111110011111100011010111101111010111101010101100101111011101001001110111100111000000011010100111101111100010000011011111101011110101110111110100101011011111010110001010100111010101101111010111010000011111111100011001100011001001000110100110011010111011101101111000101001110111110110100101110000001101000110101110001100101011001000010111010011110101011101110011010010111110110100000111110101110111010001011111110010111101011000000101001010011101011110100001001110000100101110010010011011101101111111100100111110111011110000101011100111100000001010110011111101110011110011111101001010010110101010010011100000101001100000011111110011011110111110101000100111100001101011111100010000001101101111110000011101111011011000011111000110101001011010101001011111001100010000111000101011101100000010111010000010000111011101101111111100110100010101000001010000111001110010011001011000111110000111100110101110100111101101110101101110000101101101111111110111100110100010010001100100110001011010111110011011110010010101011001111010000010001110010110010100110110000001011000111011000101001000011110111110011010111101010001101110000110110011010100100101001100001000110000100111111101011100001111000001011011100011110101101011110010111110100010111111011100011111000110010010110111001110011110110100110110100011010011111010010101110000001010111001111111010011000011011000101110010100110010100111000111100101111101001010001100111101010000001001110100001101010011111110010010100111111100011111101111011001101011011000111111010110010010111110010010011110101001110011001011011001110011011101011001001010001100110101001001011011000111100111111110010011001000101110101111001101001101001010000000110001000011110001110010111110011011101101111101100000111011001110101110011011011100100100011000010110100110010100011101100011000000011111101001110011111000101101010010010010101000010110111001100000100101100101100111010001011000110010111000000001100000000001000010001011001100000000000100111001101110111110001001011000000000101100001010110110000001010110110101011010011010100010101111100100101000000000101001101010101011110011010100010100001000010000011001001010011010111101001010101000100111010000011001111010000011110100101110010110101001011110110110011101000101101000001101100101011011110100100011100100111000101110000110101110010010000011011100111100000110001001010011101101001011111000010111111110010010000110101100011010111000100111010111011111111101010101011110001001111001001111111100111101100001001110010100111110011001111010111110000111110100101101000000100101100110011100100000011011011001010000110100110110000000101110001110000001111111101101001100101111000011000010001011010000010101000111001011111111110011101110111010010111011111111001000010101110011011011100110111010001101010000011101001001101010010110000100011010010011100011011111101110010011100101100001001000001100101010000110011011010010001110011010111110101100000111000111100000100110011111110101001010000010100000011001101000001100110011111101011101000101100111010110000000101100000101010011010000101001111010101110100111000000110101101000110010110001110000110000011111110111000110110011000001110011101100

 

RFID管理系统集成商 RFID中间件 条码系统中间层 物联网软件集成