【H.264】H.264详解(二)—— H264视频码流解析示例源码 ...

打印 上一主题 下一主题

主题 1963|帖子 1963|积分 5889

声明:此篇示例源码非原创,原作者雷霄骅。雷霄骅,中国传媒大学通信与信息系统专业博士生,在此向雷霄骅雷神致敬。
代码原文地址:视音频数据处理入门:H.264视频码流解析
一、媒介

示例源码是一个H.264码流解析程序。该程序可以从H.264码流中分析得到它的根本单元NALU,分离出NALU,然后再分析NALU的各个字段。
关于NALU的相关内容可参考文章【H.264】H.264详解(一)—— 一文看懂H.264协议
二、示例源码

【1】目录结构

  1. project/
  2. ├── Makefile
  3. ├── h264parser.c
  4. ├── sintel.h264
复制代码
【2】Makefile源码

  1. app:h264parser.c
  2.         gcc -o app h264parser.c
  3. clean:
  4.         rm -f app
复制代码
【3】h264parser.c源码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef enum {
  5.         NALU_TYPE_SLICE    = 1,
  6.         NALU_TYPE_DPA      = 2,
  7.         NALU_TYPE_DPB      = 3,
  8.         NALU_TYPE_DPC      = 4,
  9.         NALU_TYPE_IDR      = 5,
  10.         NALU_TYPE_SEI      = 6,
  11.         NALU_TYPE_SPS      = 7,
  12.         NALU_TYPE_PPS      = 8,
  13.         NALU_TYPE_AUD      = 9,
  14.         NALU_TYPE_EOSEQ    = 10,
  15.         NALU_TYPE_EOSTREAM = 11,
  16.         NALU_TYPE_FILL     = 12,
  17. } NaluType;
  18. typedef enum {
  19.         NALU_PRIORITY_DISPOSABLE = 0,
  20.         NALU_PRIRITY_LOW         = 1,
  21.         NALU_PRIORITY_HIGH       = 2,
  22.         NALU_PRIORITY_HIGHEST    = 3
  23. } NaluPriority;
  24. typedef struct{
  25.         int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
  26.         unsigned len;                 //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
  27.         unsigned max_size;            //! Nal Unit Buffer size
  28.         int forbidden_bit;            //! should be always FALSE
  29.         int nal_reference_idc;        //! NALU_PRIORITY_xxxx
  30.         int nal_unit_type;            //! NALU_TYPE_xxxx   
  31.         char *buf;                    //! contains the first byte followed by the EBSP
  32. } NALU_t;
  33. FILE *h264bitstream = NULL;                //!< the bit stream file
  34. int info2=0, info3=0;
  35. static int FindStartCode2 (unsigned char *Buf){
  36.         if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0; //0x000001?
  37.         else return 1;
  38. }
  39. static int FindStartCode3 (unsigned char *Buf){
  40.         if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) return 0;//0x00000001?
  41.         else return 1;
  42. }
  43. int GetAnnexbNALU (NALU_t *nalu){
  44.         int pos = 0;
  45.         int StartCodeFound, rewind;
  46.         unsigned char *Buf;
  47.         if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL)
  48.                 printf ("GetAnnexbNALU: Could not allocate Buf memory\n");
  49.         nalu->startcodeprefix_len=3;
  50.         if (3 != fread (Buf, 1, 3, h264bitstream)){
  51.                 free(Buf);
  52.                 return 0;
  53.         }
  54.         info2 = FindStartCode2 (Buf);
  55.         if(info2 != 1) {
  56.                 if(1 != fread(Buf+3, 1, 1, h264bitstream)){
  57.                         free(Buf);
  58.                         return 0;
  59.                 }
  60.                 info3 = FindStartCode3 (Buf);
  61.                 if (info3 != 1){
  62.                         free(Buf);
  63.                         return -1;
  64.                 }
  65.                 else {
  66.                         pos = 4;
  67.                         nalu->startcodeprefix_len = 4;
  68.                 }
  69.         }
  70.         else{
  71.                 nalu->startcodeprefix_len = 3;
  72.                 pos = 3;
  73.         }
  74.         StartCodeFound = 0;
  75.         info2 = 0;
  76.         info3 = 0;
  77.         while (!StartCodeFound){
  78.                 if (feof (h264bitstream)){
  79.                         nalu->len = (pos-1)-nalu->startcodeprefix_len;
  80.                         memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);     
  81.                         nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit
  82.                         nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit
  83.                         nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit
  84.                         free(Buf);
  85.                         return pos-1;
  86.                 }
  87.                 Buf[pos++] = fgetc (h264bitstream);
  88.                 info3 = FindStartCode3(&Buf[pos-4]);
  89.                 if(info3 != 1)
  90.                         info2 = FindStartCode2(&Buf[pos-3]);
  91.                 StartCodeFound = (info2 == 1 || info3 == 1);
  92.         }
  93.         // Here, we have found another start code (and read length of startcode bytes more than we should
  94.         // have.  Hence, go back in the file
  95.         rewind = (info3 == 1)? -4 : -3;
  96.         if (0 != fseek (h264bitstream, rewind, SEEK_CUR)){
  97.                 free(Buf);
  98.                 printf("GetAnnexbNALU: Cannot fseek in the bit stream file");
  99.         }
  100.         // Here the Start code, the complete NALU, and the next start code is in the Buf.  
  101.         // The size of Buf is pos, pos+rewind are the number of bytes excluding the next
  102.         // start code, and (pos+rewind)-startcodeprefix_len is the size of the NALU excluding the start code
  103.         nalu->len = (pos+rewind)-nalu->startcodeprefix_len;
  104.         memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);//
  105.         nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit
  106.         nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit
  107.         nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit
  108.         free(Buf);
  109.         return (pos+rewind);
  110. }
  111. /**
  112. * Analysis H.264 Bitstream
  113. * @param url    Location of input H.264 bitstream file.
  114. */
  115. int simplest_h264_parser(char *url){
  116.         NALU_t *n = NULL;
  117.         int buffersize=100000;
  118.         //FILE *myout=fopen("output_log.txt","wb+");
  119.         FILE *myout=stdout;
  120.         h264bitstream=fopen(url, "rb+");
  121.         if (h264bitstream==NULL){
  122.                 printf("Open file error\n");
  123.                 return 0;
  124.         }
  125.         n = (NALU_t*)calloc (1, sizeof(NALU_t));
  126.         if (n == NULL){
  127.                 printf("Alloc NALU Error\n");
  128.                 return 0;
  129.         }
  130.         n->max_size=buffersize;
  131.         n->buf = (char*)calloc (buffersize, sizeof(char));
  132.         if (n->buf == NULL){
  133.                 free (n);
  134.                 printf ("AllocNALU: n->buf");
  135.                 return 0;
  136.         }
  137.         int data_offset=0;
  138.         int nal_num=0;
  139.         printf("-----+-------- NALU Table ------+---------+\n");
  140.         printf(" NUM |   POS   |   IDC  |  TYPE |   LEN   |\n");
  141.         printf("-----+---------+--------+-------+---------+\n");
  142.         while(!feof(h264bitstream)){
  143.                 int data_lenth;
  144.                 data_lenth=GetAnnexbNALU(n);
  145.                 char type_str[20]={0};
  146.                 switch(n->nal_unit_type){
  147.                         case NALU_TYPE_SLICE:sprintf(type_str,"SLICE");break;
  148.                         case NALU_TYPE_DPA:sprintf(type_str,"DPA");break;
  149.                         case NALU_TYPE_DPB:sprintf(type_str,"DPB");break;
  150.                         case NALU_TYPE_DPC:sprintf(type_str,"DPC");break;
  151.                         case NALU_TYPE_IDR:sprintf(type_str,"IDR");break;
  152.                         case NALU_TYPE_SEI:sprintf(type_str,"SEI");break;
  153.                         case NALU_TYPE_SPS:sprintf(type_str,"SPS");break;
  154.                         case NALU_TYPE_PPS:sprintf(type_str,"PPS");break;
  155.                         case NALU_TYPE_AUD:sprintf(type_str,"AUD");break;
  156.                         case NALU_TYPE_EOSEQ:sprintf(type_str,"EOSEQ");break;
  157.                         case NALU_TYPE_EOSTREAM:sprintf(type_str,"EOSTREAM");break;
  158.                         case NALU_TYPE_FILL:sprintf(type_str,"FILL");break;
  159.                 }
  160.                 char idc_str[20]={0};
  161.                 switch(n->nal_reference_idc>>5){
  162.                         case NALU_PRIORITY_DISPOSABLE:sprintf(idc_str,"DISPOS");break;
  163.                         case NALU_PRIRITY_LOW:sprintf(idc_str,"LOW");break;
  164.                         case NALU_PRIORITY_HIGH:sprintf(idc_str,"HIGH");break;
  165.                         case NALU_PRIORITY_HIGHEST:sprintf(idc_str,"HIGHEST");break;
  166.                 }
  167.                 fprintf(myout,"%5d| %8d| %7s| %6s| %8d|\n",nal_num,data_offset,idc_str,type_str,n->len);
  168.                 data_offset=data_offset+data_lenth;
  169.                 nal_num++;
  170.         }
  171.         //Free
  172.         if (n){
  173.                 if (n->buf){
  174.                         free(n->buf);
  175.                         n->buf=NULL;
  176.                 }
  177.                 free (n);
  178.         }
  179.         return 0;
  180. }
  181. int main(int argc, char* argv[]){
  182.         simplest_h264_parser("sintel.h264");
  183.         return 0;
  184. }
复制代码
【4】编译运行


【5】源码下载地址

H.264详解(二)- H264视频码流解析示例源码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表