本文中的程序是一個(gè)H.264碼流解析程序。該程序可以從H.264碼流中分析得到它的基本單元NALU随闺,并且可以簡(jiǎn)單解析NALU首部的字段。
原理
H.264原始碼流(又稱(chēng)為“裸流”)是由一個(gè)一個(gè)的NALU組成的。他們的結(jié)構(gòu)如下圖所示痴晦。
其中每個(gè)NALU之間通過(guò)startcode(起始碼)進(jìn)行分隔,起始碼分成兩種:0x000001(3Byte)或者0x00000001(4Byte)琳彩。如果NALU對(duì)應(yīng)的Slice為一幀的開(kāi)始就用0x00000001誊酌,否則就用0x000001。
H.264碼流解析的步驟就是首先從碼流中搜索0x000001和0x00000001露乏,分離出NALU碧浊;然后再分析NALU的各個(gè)字段。本文的程序即實(shí)現(xiàn)了上述的兩個(gè)步驟瘟仿。
『NALU』的格式如下圖3(引用H264 PDF)所示:
很明顯箱锐,『NALU』由頭和身體兩個(gè)部分組成:
頭:一般存儲(chǔ)標(biāo)志信息,譬如NALU的類(lèi)型猾骡。存儲(chǔ)了和編解碼信息相關(guān)的數(shù)據(jù)瑞躺;
身體:存儲(chǔ)了真正的數(shù)據(jù)。但實(shí)際上兴想,這塊也會(huì)相對(duì)比較復(fù)雜幢哨,過(guò)H264的一個(gè)目的是“網(wǎng)絡(luò)友好性”,說(shuō)白了就是能夠很好地適配各種傳輸格式嫂便。所以根據(jù)實(shí)際采用數(shù)據(jù)傳輸流格式捞镰,也會(huì)對(duì)這部分?jǐn)?shù)據(jù)格式再進(jìn)行處理。
代碼
#import "ViewController.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
NALU_TYPE_SLICE = 1,
NALU_TYPE_DPA = 2,
NALU_TYPE_DPB = 3,
NALU_TYPE_DPC = 4,
NALU_TYPE_IDR = 5,
NALU_TYPE_SEI = 6,
NALU_TYPE_SPS = 7,
NALU_TYPE_PPS = 8,
NALU_TYPE_AUD = 9,
NALU_TYPE_EOSEQ = 10,
NALU_TYPE_EOSTREAM = 11,
NALU_TYPE_FILL = 12,
} NaluType;
typedef enum {
NALU_PRIORITY_DISPOSABLE = 0,
NALU_PRIRITY_LOW = 1,
NALU_PRIORITY_HIGH = 2,
NALU_PRIORITY_HIGHEST = 3
} NaluPriority;
/**
NALU = NALU Header + NALU Body
1.NALU Header
首先毙替,『NALU Header』只占1個(gè)字節(jié)岸售,即8位,其組成如下:
| forbidden_zero_bit | nal_ref_idc | nal_unit_type |
`--------------------+-------------+---------------`
| 1 bit | 2 bit | 5 bit |
*/
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 這個(gè)值是自己設(shè)置的厂画,項(xiàng)目里設(shè)置是100000凸丸, 和nalu的結(jié)構(gòu)沒(méi)有關(guān)系
int forbidden_bit; //! should be always FALSE 占用1位
int nal_reference_idc; //! NALU_PRIORITY_xxxx 占用2位
int nal_unit_type; //! NALU_TYPE_xxxx 占用5位
char *buf; //! contains the first byte followed by the EBSP NALU的數(shù)據(jù)體部分?jǐn)?shù)據(jù)
} NALU_t;
FILE *h264bitstream = NULL; //!< the bit stream file
int info2=0, info3=0;
static int FindStartCode2 (unsigned char *Buf){
if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0; //查找0x000001?
else return 1;
}
static int FindStartCode3 (unsigned char *Buf){
if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) return 0;//查找0x00000001?
else return 1;
}
// 獲取Annexb格式的NALU,注意Annex B和 AVCC格式的區(qū)別袱院,可以自行百度
int GetAnnexbNALU (NALU_t *nalu){
int pos = 0;
int StartCodeFound, rewind;
unsigned char *Buf;
if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL)
printf ("GetAnnexbNALU: Could not allocate Buf memory\n");
nalu->startcodeprefix_len=3;
//成功讀取的元素總數(shù)會(huì)以 size_t 對(duì)象返回屎慢,size_t 對(duì)象是一個(gè)整型數(shù)據(jù)類(lèi)型。如果總數(shù)與 nmemb 參數(shù)不同忽洛,則可能發(fā)生了一個(gè)錯(cuò)誤或者到達(dá)了文件末尾腻惠。
if (3 != fread (Buf, 1, 3, h264bitstream)){// 讀取3個(gè)字節(jié)
free(Buf);
return 0;
}
info2 = FindStartCode2 (Buf);// 查找startCode 0x000001
if(info2 != 1) {// 沒(méi)找到
if(1 != fread(Buf+3, 1, 1, h264bitstream)){// 讀取一個(gè)字節(jié)
free(Buf);
return 0;
}
info3 = FindStartCode3 (Buf);// 查找startCode 0x00000001
if (info3 != 1){
free(Buf);
return -1;
}
else {// 找到了startCode 0x00000001
pos = 4;
nalu->startcodeprefix_len = 4;
}
}
else{//// 找到了startCode 0x000001
nalu->startcodeprefix_len = 3;
pos = 3;
}
StartCodeFound = 0;
info2 = 0;
info3 = 0;
while (!StartCodeFound){// 循環(huán)查找startCode
if (feof (h264bitstream)){
nalu->len = (pos-1)-nalu->startcodeprefix_len;
memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);
nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit 0x80的二進(jìn)制為10000000
nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit , 0x60的二進(jìn)制為1100000
nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit 0x1f的十進(jìn)制是31,31的二進(jìn)制為11111
free(Buf);
return pos-1;
}
/**
int fgetc(FILE *stream)
從指定的流 stream 獲取下一個(gè)字符(一個(gè)無(wú)符號(hào)字符)欲虚,并把位置標(biāo)識(shí)符往前移動(dòng)集灌。
*/
Buf[pos++] = fgetc (h264bitstream);// pos位置加1
// 查找startCode
info3 = FindStartCode3(&Buf[pos-4]);
if(info3 != 1)
info2 = FindStartCode2(&Buf[pos-3]);
StartCodeFound = (info2 == 1 || info3 == 1);
}
// Here, we have found another start code (and read length of startcode bytes more than we should
// have. Hence, go back in the file
rewind = (info3 == 1)? -4 : -3;
/**
int fseek(FILE *stream, long int offset, int whence)
設(shè)置流 stream 的文件位置為給定的偏移 offset,參數(shù) offset 意味著從給定的 whence 位置查找的字節(jié)數(shù)复哆。
*/
if (0 != fseek (h264bitstream, rewind, SEEK_CUR)){
free(Buf);
printf("GetAnnexbNALU: Cannot fseek in the bit stream file");
}
// Here the Start code, the complete NALU, and the next start code is in the Buf.
// The size of Buf is pos, pos+rewind are the number of bytes excluding the next
// start code, and (pos+rewind)-startcodeprefix_len is the size of the NALU excluding the start code
nalu->len = (pos+rewind)-nalu->startcodeprefix_len;
memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);//給nalu填充數(shù)據(jù)欣喧,填充的數(shù)據(jù)是不包含startCode(本身NALU的定義就是NALU header + NALU body腌零,并不包含startCode),但是包含NALU header 從Buf偏移startcode長(zhǎng)度的字節(jié)數(shù)位置開(kāi)始,填充長(zhǎng)度為nalu->len,
nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit 0x80的二進(jìn)制為10000000
nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit 0x60的二進(jìn)制為1100000
nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit 0x1f的十進(jìn)制是31续誉,31的二進(jìn)制為11111
free(Buf);
return (pos+rewind);
}
//H.264碼流解析的步驟就是首先從碼流中搜索0x000001和0x00000001莱没,分離出NALU;然后再分析NALU的各個(gè)字段酷鸦。本文的程序即實(shí)現(xiàn)了上述的兩個(gè)步驟。
/**
* Analysis H.264 Bitstream
* @param url Location of input H.264 bitstream file.
*/
int simplest_h264_parser(char *url){
NALU_t *n;
int buffersize=100000;
//FILE *myout=fopen("output_log.txt","wb+");
FILE *myout=stdout;
h264bitstream=fopen(url, "rb+");
if (h264bitstream==NULL){
printf("Open file error\n");
return 0;
}
n = (NALU_t*)calloc (1, sizeof (NALU_t));
if (n == NULL){
printf("Alloc NALU Error\n");
return 0;
}
n->max_size=buffersize;
n->buf = (char*)calloc (buffersize, sizeof (char));
if (n->buf == NULL){
free (n);
printf ("AllocNALU: n->buf");
return 0;
}
int data_offset=0;
int nal_num=0;
printf("-----+-------- NALU Table ------+---------+\n");
printf(" NUM | POS | IDC | TYPE | LEN |\n");
printf("-----+---------+--------+-------+---------+\n");
while(!feof(h264bitstream))
{
int data_lenth;
data_lenth=GetAnnexbNALU(n);// 讀取NALU
char type_str[20]={0};
switch(n->nal_unit_type){
case NALU_TYPE_SLICE:sprintf(type_str,"SLICE");break;
case NALU_TYPE_DPA:sprintf(type_str,"DPA");break;
case NALU_TYPE_DPB:sprintf(type_str,"DPB");break;
case NALU_TYPE_DPC:sprintf(type_str,"DPC");break;
case NALU_TYPE_IDR:sprintf(type_str,"IDR");break;
case NALU_TYPE_SEI:sprintf(type_str,"SEI");break;
case NALU_TYPE_SPS:sprintf(type_str,"SPS");break;
case NALU_TYPE_PPS:sprintf(type_str,"PPS");break;
case NALU_TYPE_AUD:sprintf(type_str,"AUD");break;
case NALU_TYPE_EOSEQ:sprintf(type_str,"EOSEQ");break;
case NALU_TYPE_EOSTREAM:sprintf(type_str,"EOSTREAM");break;
case NALU_TYPE_FILL:sprintf(type_str,"FILL");break;
}
char idc_str[20]={0};
switch(n->nal_reference_idc>>5){ //nal_reference_idc 占2位牙咏,指示當(dāng)前NALU的優(yōu)先級(jí)臼隔,或者說(shuō)重要性,數(shù)值越大表明越重要妄壶。所以取值范圍為0~3
case NALU_PRIORITY_DISPOSABLE:sprintf(idc_str,"DISPOS");break;
case NALU_PRIRITY_LOW:sprintf(idc_str,"LOW");break;
case NALU_PRIORITY_HIGH:sprintf(idc_str,"HIGH");break;
case NALU_PRIORITY_HIGHEST:sprintf(idc_str,"HIGHEST");break;
}
fprintf(myout,"%5d| %8d| %7s| %6s| %8d|\n",nal_num,data_offset,idc_str,type_str,n->len);
data_offset=data_offset+data_lenth;
nal_num++;
}
//Free
if (n){
if (n->buf){
free(n->buf);
n->buf=NULL;
}
free (n);
}
return 0;
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fileName = [[NSBundle mainBundle] pathForResource:@"sintel" ofType:@"h264"];
const char *filePath = [fileName cStringUsingEncoding:NSUTF8StringEncoding];
simplest_h264_parser(filePath);
}
@end
結(jié)果
本程序的輸入為一個(gè)H.264原始碼流(裸流)的文件路徑摔握,輸出為該碼流的NALU統(tǒng)計(jì)數(shù)據(jù),如下圖所示丁寄。
參考
視音頻數(shù)據(jù)處理入門(mén):H.264視頻碼流解析
https://blog.csdn.net/leixiaohua1020/article/details/50534369