NALU:Coded H.264 data is stored or transmitted as a series of packet s known as N etworkA bstraction L ayerU nits. (NALU单元)
RBSP :A NALU contains a R aw B yte S equence P ayload, a sequence of byte s containingsyntax elements.(原始数据字节流)
SODB:S tring O f D ata 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函数.
void SODBtoRBSP(Bitstream *currStream) { currStream->byte_buf <<= 1; currStream->byte_buf |= 1; currStream->bits_to_go--; currStream->byte_buf <<= currStream->bits_to_go; currStream->streamBuffer[currStream->byte_pos++] = currStream->byte_buf; currStream->bits_to_go = 8; currStream->byte_buf = 0; }
2. NALU header为一个字节,这8个比特分别对应forbidden_zero_bit, nal_ref_idc, nal_unit_type. NALU的body其实就是RBSP. 由RBSP转NALU是由RBSPtoNALU函数来实现的.
typedef struct { int startcodeprefix_len; unsigned len; unsigned max_size; int nal_unit_type; int nal_reference_idc; int forbidden_bit; byte *buf; } NALU_t;
int RBSPtoNALU ( char *rbsp, NALU_t *nalu, int rbsp_size, int nal_unit_type, int nal_reference_idc, int min_num_bytes, int UseAnnexbLongStartcode) { int len; assert (nalu != NULL); assert (nal_reference_idc <=3 && nal_reference_idc >=0); assert (nal_unit_type > 0 && nal_unit_type <= 10); assert (rbsp_size < MAXRBSPSIZE); nalu->forbidden_bit = 0; nalu->nal_reference_idc = nal_reference_idc; nalu->nal_unit_type = nal_unit_type; nalu->startcodeprefix_len = UseAnnexbLongStartcode?4:3; nalu->buf[0] = nalu->forbidden_bit << 7 | nalu->nal_reference_idc << 5 | nalu->nal_unit_type; memcpy (&nalu->buf[1], rbsp, rbsp_size); len = 1 + RBSPtoEBSP (&nalu->buf[1], 0, rbsp_size, min_num_bytes); nalu->len = len; return len; }
3. Start Code Prefix为3个字节. 但是,为了寻址方便,要求数据流在长度上对齐,因此H.264建议在Start Code Prefix前面加若干个0.
4. 为了简便起见,上面的逻辑关系图没有考虑"防止竞争"机制.
编码foreman_part_qcif.yuv的第一帧,码流如下: (对照trace_enc.txt分析即可,由于码流太多,篇幅有限,故不一一分析)
000000000 00000000000000000000001 011001110100001000000000000111101111000101100001011000100110001000000000000000000000000000000