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

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



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



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

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

/** 
 * 最簡(jiǎn)單的視音頻數(shù)據(jù)處理示例 
 * Simplest MediaData Test 
 * 
 * 雷霄驊 Lei Xiaohua 
 * leixiaohua1020@126.com 
 * 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù) 
 * Communication University of China / Digital TV Technology 
 * http://blog.csdn.net/leixiaohua1020 
 * 
 * 本項(xiàng)目包含如下幾種視音頻測(cè)試示例: 
 *  (1)像素?cái)?shù)據(jù)處理程序。包含RGB和YUV像素格式處理的函數(shù)。 
 *  (2)音頻采樣數(shù)據(jù)處理程序。包含PCM音頻采樣格式處理的函數(shù)。 
 *  (3)H.264碼流分析程序??梢苑蛛x并解析NALU。 
 *  (4)AAC碼流分析程序。可以分離并解析ADTS幀。 
 *  (5)FLV封裝格式分析程序??梢詫LV中的MP3音頻碼流分離出來(lái)。 
 *  (6)UDP-RTP協(xié)議分析程序??梢詫⒎治鯱DP/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語(yǔ)言標(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é)果

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



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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前兩篇文章介紹的YUV/RGB處理程序以及PCM處理程序都屬于視音頻原始數(shù)據(jù)的處理程序。從本文開始介紹視音頻碼流的...
    小魚兒喜歡花無(wú)缺閱讀 1,542評(píng)論 2 0
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評(píng)論 25 708
  • 在保證視頻圖像質(zhì)量的前提下,HEVC通過(guò)增加一定的計(jì)算復(fù)雜度,可以實(shí)現(xiàn)碼流在H.264/AVC的基礎(chǔ)上降低50%。...
    加劉景長(zhǎng)閱讀 8,312評(píng)論 0 6
  • 我們對(duì)于與己相關(guān)的人和事心里都有個(gè)預(yù)期,這個(gè)“預(yù)期”事實(shí)上起到了指導(dǎo)我們行為的作用。 比如說(shuō)我打算去買件湘繡小品,...
    山賊爺閱讀 614評(píng)論 0 0
  • 你有沒(méi)有在工作或生活中,被在意的人指責(zé)時(shí),情緒陷入陰暗的時(shí)刻? 其實(shí)你心里是知道的,在某些方面某些事上,你做的不好...
    SOSO_馨閱讀 560評(píng)論 0 0

友情鏈接更多精彩內(nèi)容