=====================================================

视音频数据处理入门系列文章:

视音频数据处理入门:RGB、YUV像素数据处理

视音频数据处理入门:PCM音频采样数据处理

视音频数据处理入门:H.264视频码流解析

视音频数据处理入门:AAC音频码流解析

视音频数据处理入门:FLV封装格式解析

视音频数据处理入门:UDP-RTP协议解析

=====================================================

前两篇文章介绍的YUV/RGB处理程序以及PCM处理程序都属于视音频原始数据的处理程序。从本文开始介绍视音频码流的处理程序。本文介绍的程序是视频码流处理程序。视频码流在视频播放器中的位置如下所示。

本文中的程序是一个H.264码流解析程序。该程序可以从H.264码流中分析得到它的基本单元NALU,并且可以简单解析NALU首部的字段。通过修改该程序可以实现不同的H.264码流处理功能。

原理

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

其中每个NALU之间通过startcode(起始码)进行分隔,起始码分成两种:0x000001(3Byte)或者0x00000001(4Byte)。如果NALU对应的Slice为一帧的开始就用0x00000001,否则就用0x000001。

H.264码流解析的步骤就是首先从码流中搜索0x000001和0x00000001,分离出NALU;然后再分析NALU的各个字段。本文的程序即实现了上述的两个步骤。

代码

整个程序位于simplest_h264_parser()函数中,如下所示。

  1. /**
  2. * 最简单的视音频数据处理示例
  3. * Simplest MediaData Test
  4. *
  5. * 雷霄骅 Lei Xiaohua
  6. * leixiaohua1020@126.com
  7. * 中国传媒大学/数字电视技术
  8. * Communication University of China / Digital TV Technology
  9. * http://blog.csdn.net/leixiaohua1020
  10. *
  11. * 本项目包含如下几种视音频测试示例:
  12. *  (1)像素数据处理程序。包含RGB和YUV像素格式处理的函数。
  13. *  (2)音频采样数据处理程序。包含PCM音频采样格式处理的函数。
  14. *  (3)H.264码流分析程序。可以分离并解析NALU。
  15. *  (4)AAC码流分析程序。可以分离并解析ADTS帧。
  16. *  (5)FLV封装格式分析程序。可以将FLV中的MP3音频码流分离出来。
  17. *  (6)UDP-RTP协议分析程序。可以将分析UDP/RTP/MPEG-TS数据包。
  18. *
  19. * This project contains following samples to handling multimedia data:
  20. *  (1) Video pixel data handling program. It contains several examples to handle RGB and YUV data.
  21. *  (2) Audio sample data handling program. It contains several examples to handle PCM data.
  22. *  (3) H.264 stream analysis program. It can parse H.264 bitstream and analysis NALU of stream.
  23. *  (4) AAC stream analysis program. It can parse AAC bitstream and analysis ADTS frame of stream.
  24. *  (5) FLV format analysis program. It can analysis FLV file and extract MP3 audio stream.
  25. *  (6) UDP-RTP protocol analysis program. It can analysis UDP/RTP/MPEG-TS Packet.
  26. *
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. typedef enum {
  32. NALU_TYPE_SLICE    = 1,
  33. NALU_TYPE_DPA      = 2,
  34. NALU_TYPE_DPB      = 3,
  35. NALU_TYPE_DPC      = 4,
  36. NALU_TYPE_IDR      = 5,
  37. NALU_TYPE_SEI      = 6,
  38. NALU_TYPE_SPS      = 7,
  39. NALU_TYPE_PPS      = 8,
  40. NALU_TYPE_AUD      = 9,
  41. NALU_TYPE_EOSEQ    = 10,
  42. NALU_TYPE_EOSTREAM = 11,
  43. NALU_TYPE_FILL     = 12,
  44. } NaluType;
  45. typedef enum {
  46. NALU_PRIORITY_DISPOSABLE = 0,
  47. NALU_PRIRITY_LOW         = 1,
  48. NALU_PRIORITY_HIGH       = 2,
  49. NALU_PRIORITY_HIGHEST    = 3
  50. } NaluPriority;
  51. typedef struct
  52. {
  53. int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
  54. unsigned len;                 //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
  55. unsigned max_size;            //! Nal Unit Buffer size
  56. int forbidden_bit;            //! should be always FALSE
  57. int nal_reference_idc;        //! NALU_PRIORITY_xxxx
  58. int nal_unit_type;            //! NALU_TYPE_xxxx
  59. char *buf;                    //! contains the first byte followed by the EBSP
  60. } NALU_t;
  61. FILE *h264bitstream = NULL;                //!< the bit stream file
  62. int info2=0, info3=0;
  63. static int FindStartCode2 (unsigned char *Buf){
  64. if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0; //0x000001?
  65. else return 1;
  66. }
  67. static int FindStartCode3 (unsigned char *Buf){
  68. if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) return 0;//0x00000001?
  69. else return 1;
  70. }
  71. int GetAnnexbNALU (NALU_t *nalu){
  72. int pos = 0;
  73. int StartCodeFound, rewind;
  74. unsigned char *Buf;
  75. if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL)
  76. printf ("GetAnnexbNALU: Could not allocate Buf memory\n");
  77. nalu->startcodeprefix_len=3;
  78. if (3 != fread (Buf, 1, 3, h264bitstream)){
  79. free(Buf);
  80. return 0;
  81. }
  82. info2 = FindStartCode2 (Buf);
  83. if(info2 != 1) {
  84. if(1 != fread(Buf+3, 1, 1, h264bitstream)){
  85. free(Buf);
  86. return 0;
  87. }
  88. info3 = FindStartCode3 (Buf);
  89. if (info3 != 1){
  90. free(Buf);
  91. return -1;
  92. }
  93. else {
  94. pos = 4;
  95. nalu->startcodeprefix_len = 4;
  96. }
  97. }
  98. else{
  99. nalu->startcodeprefix_len = 3;
  100. pos = 3;
  101. }
  102. StartCodeFound = 0;
  103. info2 = 0;
  104. info3 = 0;
  105. while (!StartCodeFound){
  106. if (feof (h264bitstream)){
  107. nalu->len = (pos-1)-nalu->startcodeprefix_len;
  108. memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);
  109. nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit
  110. nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit
  111. nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit
  112. free(Buf);
  113. return pos-1;
  114. }
  115. Buf[pos++] = fgetc (h264bitstream);
  116. info3 = FindStartCode3(&Buf[pos-4]);
  117. if(info3 != 1)
  118. info2 = FindStartCode2(&Buf[pos-3]);
  119. StartCodeFound = (info2 == 1 || info3 == 1);
  120. }
  121. // Here, we have found another start code (and read length of startcode bytes more than we should
  122. // have.  Hence, go back in the file
  123. rewind = (info3 == 1)? -4 : -3;
  124. if (0 != fseek (h264bitstream, rewind, SEEK_CUR)){
  125. free(Buf);
  126. printf("GetAnnexbNALU: Cannot fseek in the bit stream file");
  127. }
  128. // Here the Start code, the complete NALU, and the next start code is in the Buf.
  129. // The size of Buf is pos, pos+rewind are the number of bytes excluding the next
  130. // start code, and (pos+rewind)-startcodeprefix_len is the size of the NALU excluding the start code
  131. nalu->len = (pos+rewind)-nalu->startcodeprefix_len;
  132. memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);//
  133. nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit
  134. nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit
  135. nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit
  136. free(Buf);
  137. return (pos+rewind);
  138. }
  139. /**
  140. * Analysis H.264 Bitstream
  141. * @param url    Location of input H.264 bitstream file.
  142. */
  143. int simplest_h264_parser(char *url){
  144. NALU_t *n;
  145. int buffersize=100000;
  146. //FILE *myout=fopen("output_log.txt","wb+");
  147. FILE *myout=stdout;
  148. h264bitstream=fopen(url, "rb+");
  149. if (h264bitstream==NULL){
  150. printf("Open file error\n");
  151. return 0;
  152. }
  153. n = (NALU_t*)calloc (1, sizeof (NALU_t));
  154. if (n == NULL){
  155. printf("Alloc NALU Error\n");
  156. return 0;
  157. }
  158. n->max_size=buffersize;
  159. n->buf = (char*)calloc (buffersize, sizeof (char));
  160. if (n->buf == NULL){
  161. free (n);
  162. printf ("AllocNALU: n->buf");
  163. return 0;
  164. }
  165. int data_offset=0;
  166. int nal_num=0;
  167. printf("-----+-------- NALU Table ------+---------+\n");
  168. printf(" NUM |    POS  |    IDC |  TYPE |   LEN   |\n");
  169. printf("-----+---------+--------+-------+---------+\n");
  170. while(!feof(h264bitstream))
  171. {
  172. int data_lenth;
  173. data_lenth=GetAnnexbNALU(n);
  174. char type_str[20]={0};
  175. switch(n->nal_unit_type){
  176. case NALU_TYPE_SLICE:sprintf(type_str,"SLICE");break;
  177. case NALU_TYPE_DPA:sprintf(type_str,"DPA");break;
  178. case NALU_TYPE_DPB:sprintf(type_str,"DPB");break;
  179. case NALU_TYPE_DPC:sprintf(type_str,"DPC");break;
  180. case NALU_TYPE_IDR:sprintf(type_str,"IDR");break;
  181. case NALU_TYPE_SEI:sprintf(type_str,"SEI");break;
  182. case NALU_TYPE_SPS:sprintf(type_str,"SPS");break;
  183. case NALU_TYPE_PPS:sprintf(type_str,"PPS");break;
  184. case NALU_TYPE_AUD:sprintf(type_str,"AUD");break;
  185. case NALU_TYPE_EOSEQ:sprintf(type_str,"EOSEQ");break;
  186. case NALU_TYPE_EOSTREAM:sprintf(type_str,"EOSTREAM");break;
  187. case NALU_TYPE_FILL:sprintf(type_str,"FILL");break;
  188. }
  189. char idc_str[20]={0};
  190. switch(n->nal_reference_idc>>5){
  191. case NALU_PRIORITY_DISPOSABLE:sprintf(idc_str,"DISPOS");break;
  192. case NALU_PRIRITY_LOW:sprintf(idc_str,"LOW");break;
  193. case NALU_PRIORITY_HIGH:sprintf(idc_str,"HIGH");break;
  194. case NALU_PRIORITY_HIGHEST:sprintf(idc_str,"HIGHEST");break;
  195. }
  196. fprintf(myout,"%5d| %8d| %7s| %6s| %8d|\n",nal_num,data_offset,idc_str,type_str,n->len);
  197. data_offset=data_offset+data_lenth;
  198. nal_num++;
  199. }
  200. //Free
  201. if (n){
  202. if (n->buf){
  203. free(n->buf);
  204. n->buf=NULL;
  205. }
  206. free (n);
  207. }
  208. return 0;
  209. }

上文中的函数调用方法如下所示。

  1. simplest_h264_parser("sintel.h264");

结果

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

下载

Simplest mediadata test

项目主页

SourceForge:https://sourceforge.net/projects/simplest-mediadata-test/

Github:https://github.com/leixiaohua1020/simplest_mediadata_test

开源中国:http://git.oschina.net/leixiaohua1020/simplest_mediadata_test

CSDN下载地址:http://download.csdn.net/detail/leixiaohua1020/9422409

本项目包含如下几种视音频数据解析示例:

(1)像素数据处理程序。包含RGB和YUV像素格式处理的函数。
 (2)音频采样数据处理程序。包含PCM音频采样格式处理的函数。
 (3)H.264码流分析程序。可以分离并解析NALU。
 (4)AAC码流分析程序。可以分离并解析ADTS帧。
 (5)FLV封装格式分析程序。可以将FLV中的MP3音频码流分离出来。
 (6)UDP-RTP协议分析程序。可以将分析UDP/RTP/MPEG-TS数据包。

视音频数据处理入门:H.264视频码流解析的更多相关文章

  1. H.264/H265码流解析

    H.264/H265码流解析 一.H.264码流解析 一个原始的H.264 NALU 单元常由 [StartCode] [NALU Header] [NALU Payload] 三部分组成 一个原始的 ...

  2. 视音频数据处理入门:AAC音频码流解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  3. 视音频数据处理入门:UDP-RTP协议解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  4. 视音频数据处理入门:FLV封装格式解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  5. 视音频数据处理入门:PCM音频采样数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  6. 视音频数据处理入门:RGB、YUV像素数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  7. [转载] 视音频数据处理入门:RGB、YUV像素数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  8. 视音频数据处理入门:RGB、YUV像素数据处理【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...

  9. 【视频编解码·学习笔记】4. H.264的码流封装格式

    一.码流封装格式简单介绍: H.264的语法元素进行编码后,生成的输出数据都封装为NAL Unit进行传递,多个NAL Unit的数据组合在一起形成总的输出码流.对于不同的应用场景,NAL规定了一种通 ...

随机推荐

  1. 2018软工实践—Beta冲刺(1)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 调试服务器性能 展示GitHub当日代码/文档签入记录(组内 ...

  2. 【Coursera】支持向量机

    一.最大间隔分类器 1. 函数间隔:\(γ^{i} = y^{i}(w^{T} x + b)\), 改变w和b的量级,对分类结果不会产生任何影响,但是会改变函数间隔的大小.因此,直接对函数间隔求最大值 ...

  3. VS提示“无法启动IIS Express Web服务器”的解决方法

    有时在使用Visual Studio运行项目时,会提示“无法启动IIS Express Web服务器”,如图: 可以依次尝试以下方法(我的情况使用第一种就解决了): 1.可能原因:误操作执行了:Sc ...

  4. AWS EC2安装docker时的问题

    在AWS EC2的实例(Ubuntu)里面安装docker时,使用通常的安装步骤 :~$ sudo apt-get update :~$ sudo apt-get install docker 安装完 ...

  5. 使用node-webkit包装浏览器

    node-webkit简称nwjs:开源地址 https://github.com/nwjs/nw.js 参考博客 https://www.cnblogs.com/soaringEveryday/p/ ...

  6. Git查看与修改用户名、邮箱(转载)

    用户名和邮箱的作用: 用户名和邮箱地址相当于你的身份标识,是本地Git客户端的一个变量,不会随着Git库而改变. 每次commit都会用用户名和邮箱纪录. github的contributions跟你 ...

  7. 6/10 sprint2 看板和燃尽图的更新

  8. 【第十周】psp

    代码累计 300+575+475+353+620+703=2926 随笔字数 1700+3000+3785+4210+4333+3032=20727 知识点 机器学习,支持向量机 数据库技术 Acm刷 ...

  9. HDU 2061 Treasure the new start, freshmen!

    http://acm.hdu.edu.cn/showproblem.php?pid=2061 Problem Description background:A new semester comes , ...

  10. Delphi函数的out、var等关键字的作用,和使用场景

    问题描述 Delphi函数的out.var等关键字的作用,和使用场景 Delphi函数的out.var等关键字的作用,和使用场景,我知道var是作为传值调用,但是像out这个关键字又是什么作用呢? 解 ...