H.264视频码流解析

本文中的程序是一个H.264码流解析程序。该程序可以从H.264码流中分析得到它的基本单元NALU,并且可以简单解析NALU首部的字段。

原理

H.264原始码流(又称为“裸流”)是由一个一个的NALU组成的。他们的结构如下图所示。

image.png

其中每个NALU之间通过startcode(起始码)进行分隔,起始码分成两种:0x000001(3Byte)或者0x00000001(4Byte)。如果NALU对应的Slice为一帧的开始就用0x00000001,否则就用0x000001。
H.264码流解析的步骤就是首先从码流中搜索0x000001和0x00000001,分离出NALU;然后再分析NALU的各个字段。本文的程序即实现了上述的两个步骤。

『NALU』的格式如下图3(引用H264 PDF)所示:

image.png

很明显,『NALU』由头和身体两个部分组成:

  • 头:一般存储标志信息,譬如NALU的类型。存储了和编解码信息相关的数据;

  • 身体:存储了真正的数据。但实际上,这块也会相对比较复杂,过H264的一个目的是“网络友好性”,说白了就是能够很好地适配各种传输格式。所以根据实际采用数据传输流格式,也会对这部分数据格式再进行处理。

代码

#import "ViewController.h"
#include 
#include 
#include 

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个字节,即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  这个值是自己设置的,项目里设置是100000, 和nalu的结构没有关系
    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的数据体部分数据
} 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格式的区别,可以自行百度
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;

    //成功读取的元素总数会以 size_t 对象返回,size_t 对象是一个整型数据类型。如果总数与 nmemb 参数不同,则可能发生了一个错误或者到达了文件末尾。
    if (3 != fread (Buf, 1, 3, h264bitstream)){// 读取3个字节
        free(Buf);
        return 0;
    }
    info2 = FindStartCode2 (Buf);// 查找startCode 0x000001
    if(info2 != 1) {// 没找到
        if(1 != fread(Buf+3, 1, 1, h264bitstream)){// 读取一个字节
            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){// 循环查找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的二进制为10000000
            nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit , 0x60的二进制为1100000
            nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit 0x1f的十进制是31,31的二进制为11111
            free(Buf);
            return pos-1;
        }
        /**
         int fgetc(FILE *stream)
         从指定的流 stream 获取下一个字符(一个无符号字符),并把位置标识符往前移动。
         */
        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)
     设置流 stream 的文件位置为给定的偏移 offset,参数 offset 意味着从给定的 whence 位置查找的字节数。
     */
    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填充数据,填充的数据是不包含startCode(本身NALU的定义就是NALU header + NALU body,并不包含startCode),但是包含NALU header 从Buf偏移startcode长度的字节数位置开始,填充长度为nalu->len,
    nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit 0x80的二进制为10000000
    nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit 0x60的二进制为1100000
    nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit 0x1f的十进制是31,31的二进制为11111
    free(Buf);

    return (pos+rewind);
}

//H.264码流解析的步骤就是首先从码流中搜索0x000001和0x00000001,分离出NALU;然后再分析NALU的各个字段。本文的程序即实现了上述的两个步骤。
/**
 * 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位,指示当前NALU的优先级,或者说重要性,数值越大表明越重要。所以取值范围为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

结果

本程序的输入为一个H.264原始码流(裸流)的文件路径,输出为该码流的NALU统计数据,如下图所示。

image.png

参考

视音频数据处理入门:H.264视频码流解析
https://blog.csdn.net/leixiaohua1020/article/details/50534369

你可能感兴趣的:(H.264视频码流解析)