NALU:Coded H.264 data is stored or transmitted as a series of packets known as NetworkAbstraction LayerUnits. (NALU單元) app
RBSP :A NALU contains a Raw Byte Sequence Payload, a sequence of bytes containingsyntax elements.(原始數據字節流) 函數
SODB:String Of Data Bits (原始數據比特流, 長度不必定是8的倍數,故須要補齊) 編碼
邏輯關係: spa
SODB + RBSP trailing bits = RBSP .net
NAL header(1 byte) + RBSP = NALU code
Start Code Prefix(3 bytes) + NALU + Start Code Prefix(3 bytes) + NALU + ...+ = H.264BitsStream blog
說明: ip
1. SODB即編碼造成的真實碼流,爲了使一個RBSP爲整字節數,須要加trailing bits, 具體加的方法能夠看JM8.6中的SODBtoRBSP函數. ci
- 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函數來實現的. element
- typedef struct
- {
- int startcodeprefix_len; //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
- unsigned len; //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
- unsigned max_size; //! Nal Unit Buffer size
- int nal_unit_type; //! NALU_TYPE_xxxx
- int nal_reference_idc; //! NALU_PRIORITY_xxxx
- int forbidden_bit; //! should be always FALSE
- byte *buf; //! conjtains the first byte followed by the EBSP
- } 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進行斷言,很重要滴.
- 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;
-
- // 判斷是否在Start Code Prefix前面加Ox00
- nalu->startcodeprefix_len = UseAnnexbLongStartcode?4:3;
-
- // 對nalu->buf[i]進行賦值
- nalu->buf[0] =
- nalu->forbidden_bit << 7 |
- nalu->nal_reference_idc << 5 |
- nalu->nal_unit_type;
- memcpy (&nalu->buf[1], rbsp, rbsp_size);
-
- // printf ("First Byte %x\n", nalu->buf[0]);
- // 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]);
-
- len = 1 + RBSPtoEBSP (&nalu->buf[1], 0, rbsp_size, min_num_bytes);
-
- // 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]);
- // printf ("len %d\n\n", len);
- nalu->len = len;
-
- return len;
- }
3. Start Code Prefix爲3個字節. 可是,爲了尋址方便,要求數據流在長度上對齊,所以H.264建議在Start Code Prefix前面加若干個0.
4. 爲了簡便起見,上面的邏輯關係圖沒有考慮"防止競爭"機制.
編碼foreman_part_qcif.yuv的第一幀,碼流以下: (對照trace_enc.txt分析便可,因爲碼流太多,篇幅有限,故不一一分析)
0000000000000000000000000000000101100111010000100000000000011110111100010110000101100010011000100000000000000000000000000