視音頻數(shù)據(jù)處理入門:H.264視頻碼流解析

本文介紹的程序是視頻碼流處理程序障斋。視頻碼流在視頻播放器中的位置如下所示。



本文中的程序是一個H.264碼流解析程序徐鹤。該程序可以從H.264碼流中分析得到它的基本單元NALU垃环,并且可以簡單解析NALU首部的字段。通過修改該程序可以實現(xiàn)不同的H.264碼流處理功能返敬。
原理
H.264原始碼流(又稱為“裸流”)是由一個一個的NALU組成的遂庄。他們的結(jié)構(gòu)如下圖所示。



其中每個NALU之間通過startcode(起始碼)進(jìn)行分隔救赐,起始碼分成兩種:0x000001(3Byte)或者0x00000001(4Byte)涧团。如果NALU對應(yīng)的Slice為一幀的開始就用0x00000001,否則就用0x000001经磅。
H.264碼流解析的步驟就是首先從碼流中搜索0x000001和0x00000001泌绣,分離出NALU;然后再分析NALU的各個字段预厌。本文的程序即實現(xiàn)了上述的兩個步驟阿迈。
代碼

整個程序位于simplest_h264_parser()函數(shù)中,如下所示轧叽。

/** 
 * 最簡單的視音頻數(shù)據(jù)處理示例 
 * Simplest MediaData Test 
 * 
 * 雷霄驊 Lei Xiaohua 
 * leixiaohua1020@126.com 
 * 中國傳媒大學(xué)/數(shù)字電視技術(shù) 
 * Communication University of China / Digital TV Technology 
 * http://blog.csdn.net/leixiaohua1020 
 * 
 * 本項目包含如下幾種視音頻測試示例: 
 *  (1)像素數(shù)據(jù)處理程序苗沧。包含RGB和YUV像素格式處理的函數(shù)。 
 *  (2)音頻采樣數(shù)據(jù)處理程序炭晒。包含PCM音頻采樣格式處理的函數(shù)待逞。 
 *  (3)H.264碼流分析程序⊥希可以分離并解析NALU识樱。 
 *  (4)AAC碼流分析程序。可以分離并解析ADTS幀怜庸。 
 *  (5)FLV封裝格式分析程序当犯。可以將FLV中的MP3音頻碼流分離出來割疾。 
 *  (6)UDP-RTP協(xié)議分析程序嚎卫。可以將分析UDP/RTP/MPEG-TS數(shù)據(jù)包宏榕。 
 * 
 * This project contains following samples to handling multimedia data: 
 *  (1) Video pixel data handling program. It contains several examples to handle RGB and YUV data. 
 *  (2) Audio sample data handling program. It contains several examples to handle PCM data. 
 *  (3) H.264 stream analysis program. It can parse H.264 bitstream and analysis NALU of stream. 
 *  (4) AAC stream analysis program. It can parse AAC bitstream and analysis ADTS frame of stream. 
 *  (5) FLV format analysis program. It can analysis FLV file and extract MP3 audio stream. 
 *  (6) UDP-RTP protocol analysis program. It can analysis UDP/RTP/MPEG-TS Packet. 
 * 
 */  
#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_PRIORITY_LOW         = 1,  
    NALU_PRIORITY_HIGH       = 2,  
    NALU_PRIORITY_HIGHEST    = 3  
} NaluPriority;  
  
  
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 forbidden_bit;            //! should be always FALSE  
    int nal_reference_idc;        //! NALU_PRIORITY_xxxx  
    int nal_unit_type;            //! NALU_TYPE_xxxx      
    char *buf;                    //! contains the first byte followed by the EBSP  
} NALU_t;  
  
FILE *h264bitstream = NULL;                //!< the bit stream file  
  
int info2=0, info3=0;  
//判斷是否為0x000001
static int FindStartCode2 (unsigned char *Buf){  
    if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0; 
    else return 1;  
}  
//判斷是否為0x00000001  
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;  
}  
  
  
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");  
  //判斷開頭代碼0x000001還是0x00000001
    nalu->startcodeprefix_len=3;  
  
    if (3 != fread (Buf, 1, 3, h264bitstream)){  
        free(Buf);  
        return 0;  
    }  
    info2 = FindStartCode2 (Buf);  
    if(info2 != 1) {  
        if(1 != fread(Buf+3, 1, 1, h264bitstream)){  
            free(Buf);  
            return 0;  
        }  
        info3 = FindStartCode3 (Buf);  
        if (info3 != 1){   
            free(Buf);  
            return -1;  
        }  
        else {  
            pos = 4;  
            nalu->startcodeprefix_len = 4;  
        }  
    }  
    else{  
        nalu->startcodeprefix_len = 3;  
        pos = 3;  
    }  
    StartCodeFound = 0;  
    info2 = 0;  
    info3 = 0;  
  
    while (!StartCodeFound){  
        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  
            nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit  
            nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit  
            free(Buf);  
            return pos-1;  
        }  
        Buf[pos++] = fgetc (h264bitstream);  
        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;  
  
    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->forbidden_bit = nalu->buf[0] & 0x80; //1 bit  
    nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit  
    nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit  
    free(Buf);  
  
    return (pos+rewind);  
}  
  
/** 
 * 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;  //C語言標(biāo)準(zhǔn)話輸出
  
    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);  
  
        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){  
            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;  
} 
結(jié)果

本程序的輸入為一個H.264原始碼流(裸流)的文件路徑拓诸,輸出為該碼流的NALU統(tǒng)計數(shù)據(jù),如下圖所示担扑。



Ps:非原創(chuàng),原作者雷霄驊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末恰响,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子涌献,更是在濱河造成了極大的恐慌胚宦,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件燕垃,死亡現(xiàn)場離奇詭異枢劝,居然都是意外死亡,警方通過查閱死者的電腦和手機卜壕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門您旁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人轴捎,你說我怎么就攤上這事鹤盒。” “怎么了侦副?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵侦锯,是天一觀的道長。 經(jīng)常有香客問我秦驯,道長尺碰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任译隘,我火速辦了婚禮亲桥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘固耘。我一直安慰自己题篷,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布厅目。 她就那樣靜靜地躺著悼凑,像睡著了一般偿枕。 火紅的嫁衣襯著肌膚如雪璧瞬。 梳的紋絲不亂的頭發(fā)上户辫,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音嗤锉,去河邊找鬼渔欢。 笑死,一個胖子當(dāng)著我的面吹牛瘟忱,可吹牛的內(nèi)容都是我干的奥额。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼访诱,長吁一口氣:“原來是場噩夢啊……” “哼垫挨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起触菜,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤九榔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后涡相,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哲泊,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年催蝗,在試婚紗的時候發(fā)現(xiàn)自己被綠了切威。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡丙号,死狀恐怖先朦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情犬缨,我是刑警寧澤喳魏,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站遍尺,受9級特大地震影響截酷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜乾戏,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一迂苛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鼓择,春花似錦三幻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽抑堡。三九已至,卻和暖如春朗徊,著一層夾襖步出監(jiān)牢的瞬間首妖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工爷恳, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留有缆,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓温亲,卻偏偏與公主長得像棚壁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子栈虚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容