NAL詳解2

1、RBSP 原始字節序列載荷-->在SODB的後面填加了結尾比特(RBSP trailing bits 一個bit“1”)若干比特“0”,以便字節對齊
完成兩件事:1、在原始數據流後加1位1;2、如果當前數據流位數不足一字節8位,則補零
void SODBtoRBSP(Bitstream *currStream)

{
  currStream->byte_buf <<= 1;
  currStream->byte_buf |= 1;   //在當前字符流後加一個1   1bit
  currStream->bits_to_go--; //剩下幾bit以組成一字節
  currStream->byte_buf <<= currStream->bits_to_go;
  currStream->streamBuffer[currStream->byte_pos++] = currStream->byte_buf; //在buff中添加一個字節以保存當前字符流
  currStream->bits_to_go = 8;
  currStream->byte_buf = 0;

}
2、EBSP 擴展字節序列載荷-->在RBSP基礎上填加了仿校驗字節(0X03)它的原因是: 在NALU加到Annexb上時,需要添加每組NALU之前的開始碼StartCodePrefix,如果該NALU對應的slice爲一幀的開始則用4位字節表示,ox00000001,否則用3位字節表示ox000001.爲了使NALU主體中不包括與開始碼相沖突的,在編碼時,每遇到兩個字節連續爲0,就插入一個字節的0x03。解碼時將0x03去掉。也稱爲脫殼操作。
int RBSPtoEBSP(byte *streamBuffer, int begin_bytepos, int end_bytepos, int min_num_bytes)
{
  
  int i, j, count;


  for(i = begin_bytepos; i < end_bytepos; i++)
    NAL_Payload_buffer[i] = streamBuffer[i];


  count = 0;
  j = begin_bytepos;
  for(i = begin_bytepos; i < end_bytepos; i++) 
  {
    if(count == ZEROBYTES_SHORTSTARTCODE && !(NAL_Payload_buffer[i] & 0xFC)) //0xFC-->0x1111 1100 取前3個字節
    {
      streamBuffer[j] = 0x03;//在最後一個字節前添加0x03
      j++;
      count = 0;   
    }
    streamBuffer[j] = NAL_Payload_buffer[i];
    if(NAL_Payload_buffer[i] == 0x00)      
      count++;
    else 
      count = 0;
    j++;
  }
  while (j < begin_bytepos+min_num_bytes) {
    streamBuffer[j] = 0x00; // cabac stuffing word
    streamBuffer[j+1] = 0x00;
    streamBuffer[j+2] = 0x03;
    j += 3;
    stat->bit_use_stuffingBits[img->type]+=16;
  }
  return j;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章