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;
}