最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

来源:互联网 发布:软件测试兼职网站 编辑:程序博客网 时间:2024/05/11 01:15

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

最简单的基于FFmpeg的视频播放器系列文章列表:

100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)

最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)

最简单的基于FFmpeg的解码器-纯净版(不包含libavformat)

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


本文记录一个更加“纯净”的基于FFmpeg的视频解码器。此前记录过基于FFmpeg的视频播放器实际上就是一个解码器:

《最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)》

这个播放器调用了FFmpeg中的libavformat和libavcodec两个库完成了视频解码工作。但是这不是一个“纯净”的解码器。该解码器中libavformat完成封装格式的解析,而libavcodec完成解码工作。一个“纯净”的解码器,理论上说只需要使用libavcodec就足够了,并不需要使用libavformat。本文记录的解码器就是这样的一个“纯净”的解码器,它仅仅通过调用libavcodec将H.264/HEVC等格式的压缩视频码流解码成为YUV数据。

流程图

本文记录的纯净版本的基于FFmpeg的解码器的函数调用流程图如下图所示。需要注意的是,此解码器的输入必须是只包含视频编码数据“裸流”(例如H.264、HEVC码流文件),而不能是包含封装格式的媒体数据(例如AVI、MKV、MP4)。


流程图中关键函数的作用如下所列:
avcodec_register_all():注册所有的编解码器。
avcodec_find_decoder():查找解码器。
avcodec_alloc_context3():为AVCodecContext分配内存。
avcodec_open2():打开解码器。
avcodec_decode_video2():解码一帧数据。
有两个平时“不太常见”的函数:
av_parser_init():初始化AVCodecParserContext。
av_parser_parse2():解析获得一个Packet。

两个存储数据的结构体如下所列:
AVFrame:存储一帧解码后的像素数据
AVPacket:存储一帧(一般情况下)压缩编码数据

AVCodecParser

AVCodecParser用于解析输入的数据流并把它分成一帧一帧的压缩编码数据。比较形象的说法就是把长长的一段连续的数据“切割”成一段段的数据。他的核心函数是av_parser_parse2()。它的定义如下所示。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Parse a packet. 
  3.  * 
  4.  * @param s             parser context. 
  5.  * @param avctx         codec context. 
  6.  * @param poutbuf       set to pointer to parsed buffer or NULL if not yet finished. 
  7.  * @param poutbuf_size  set to size of parsed buffer or zero if not yet finished. 
  8.  * @param buf           input buffer. 
  9.  * @param buf_size      input length, to signal EOF, this should be 0 (so that the last frame can be output). 
  10.  * @param pts           input presentation timestamp. 
  11.  * @param dts           input decoding timestamp. 
  12.  * @param pos           input byte position in stream. 
  13.  * @return the number of bytes of the input bitstream used. 
  14.  * 
  15.  * Example: 
  16.  * @code 
  17.  *   while(in_len){ 
  18.  *       len = av_parser_parse2(myparser, AVCodecContext, &data, &size, 
  19.  *                                        in_data, in_len, 
  20.  *                                        pts, dts, pos); 
  21.  *       in_data += len; 
  22.  *       in_len  -= len; 
  23.  * 
  24.  *       if(size) 
  25.  *          decode_frame(data, size); 
  26.  *   } 
  27.  * @endcode 
  28.  */  
  29. int av_parser_parse2(AVCodecParserContext *s,  
  30.                      AVCodecContext *avctx,  
  31.                      uint8_t **poutbuf, int *poutbuf_size,  
  32.                      const uint8_t *buf, int buf_size,  
  33.                      int64_t pts, int64_t dts,  
  34.                      int64_t pos);  

其中poutbuf指向解析后输出的压缩编码数据帧,buf指向输入的压缩编码数据。如果函数执行完后输出数据为空(poutbuf_size为0),则代表解析还没有完成,还需要再次调用av_parser_parse2()解析一部分数据才可以得到解析后的数据帧。当函数执行完后输出数据不为空的时候,代表解析完成,可以将poutbuf中的这帧数据取出来做后续处理。

对比

简单记录一下这个只使用libavcodec的“纯净版”视频解码器和使用libavcodec+libavformat的视频解码器的不同。

PS:使用libavcodec+libavformat的解码器参考文章《》
(1)下列与libavformat相关的函数在“纯净版”视频解码器中都不存在。
av_register_all():注册所有的编解码器,复用/解复用器等等组件。其中调用了avcodec_register_all()注册所有编解码器相关的组件。
avformat_alloc_context():创建AVFormatContext结构体。
avformat_open_input():打开一个输入流(文件或者网络地址)。其中会调用avformat_new_stream()创建AVStream结构体。avformat_new_stream()中会调用avcodec_alloc_context3()创建AVCodecContext结构体。
avformat_find_stream_info():获取媒体的信息。
av_read_frame():获取媒体的一帧压缩编码数据。其中调用了av_parser_parse2()。
(2)新增了如下几个函数。
avcodec_register_all():只注册编解码器有关的组件。比如说编码器、解码器、比特流滤镜等,但是不注册复用/解复用器这些和编解码器无关的组件。
avcodec_alloc_context3():创建AVCodecContext结构体。
av_parser_init():初始化AVCodecParserContext结构体。
av_parser_parse2():使用AVCodecParser从输入的数据流中分离出一帧一帧的压缩编码数据。
(3)程序的流程发生了变化。
在“libavcodec+libavformat”的视频解码器中,使用avformat_open_input()和avformat_find_stream_info()就可以解析出输入视频的信息(例如视频的宽、高)并且赋值给相关的结构体。因此我们在初始化的时候就可以通过读取相应的字段获取到这些信息。
在“纯净”的解码器则不能这样,由于没有上述的函数,所以不能在初始化的时候获得视频的参数。“纯净”的解码器中,可以通过avcodec_decode_video2()获得这些信息。因此我们只有在成功解码第一帧之后,才能通过读取相应的字段获取到这些信息。


源代码

[cpp] view plaincopy
  1. /** 
  2.  * 最简单的基于FFmpeg的视频解码器(纯净版) 
  3.  * Simplest FFmpeg Decoder Pure 
  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.  * 本程序实现了视频码流(支持HEVC,H.264,MPEG2等)解码为YUV数据。 
  13.  * 它仅仅使用了libavcodec(而没有使用libavformat)。 
  14.  * 是最简单的FFmpeg视频解码方面的教程。 
  15.  * 通过学习本例子可以了解FFmpeg的解码流程。 
  16.  * This software is a simplest decoder based on FFmpeg. 
  17.  * It decode bitstreams to YUV pixel data. 
  18.  * It just use libavcodec (do not contains libavformat). 
  19.  * Suitable for beginner of FFmpeg. 
  20.  */  
  21.   
  22. #include <stdio.h>  
  23.   
  24. #define __STDC_CONSTANT_MACROS  
  25.   
  26. #ifdef _WIN32  
  27. //Windows  
  28. extern "C"  
  29. {  
  30. #include "libavcodec/avcodec.h"  
  31. #include "libswscale/swscale.h"  
  32. };  
  33. #else  
  34. //Linux...  
  35. #ifdef __cplusplus  
  36. extern "C"  
  37. {  
  38. #endif  
  39. #include <libavcodec/avcodec.h>  
  40. #include <libswscale/swscale.h>  
  41. #ifdef __cplusplus  
  42. };  
  43. #endif  
  44. #endif  
  45.   
  46.   
  47. //test different codec  
  48. #define TEST_H264  0  
  49. #define TEST_HEVC  0  
  50.   
  51. int main(int argc, char* argv[])  
  52. {  
  53.     AVCodec *pCodec;  
  54.     AVCodecContext *pCodecCtx= NULL;  
  55.     AVCodecParserContext *pCodecParserCtx=NULL;  
  56.   
  57.     int frame_count;  
  58.     FILE *fp_in;  
  59.     FILE *fp_out;  
  60.     AVFrame *pFrame,*pFrameYUV;  
  61.     uint8_t *out_buffer;  
  62.     const int in_buffer_size=4096;  
  63.     uint8_t in_buffer[in_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE]={0};  
  64.     uint8_t *cur_ptr;  
  65.     int cur_size;  
  66.   
  67.     AVPacket packet;  
  68.     int ret, got_picture;  
  69.       
  70.     int y_size;  
  71.   
  72. #if TEST_HEVC  
  73.     enum AVCodecID codec_id=AV_CODEC_ID_HEVC;  
  74.     char filepath_in[]="bigbuckbunny_480x272.hevc";  
  75. #elif TEST_H264  
  76.     AVCodecID codec_id=AV_CODEC_ID_H264;  
  77.     char filepath_in[]="bigbuckbunny_480x272.h264";  
  78. #else  
  79.     AVCodecID codec_id=AV_CODEC_ID_MPEG2VIDEO;  
  80.     char filepath_in[]="bigbuckbunny_480x272.m2v";  
  81. #endif  
  82.   
  83.     char filepath_out[]="bigbuckbunny_480x272.yuv";  
  84.     int first_time=1;  
  85.   
  86.     struct SwsContext *img_convert_ctx;  
  87.   
  88.     //av_log_set_level(AV_LOG_DEBUG);  
  89.       
  90.     avcodec_register_all();  
  91.   
  92.     pCodec = avcodec_find_decoder(codec_id);  
  93.     if (!pCodec) {  
  94.         printf("Codec not found\n");  
  95.         return -1;  
  96.     }  
  97.     pCodecCtx = avcodec_alloc_context3(pCodec);  
  98.     if (!pCodecCtx){  
  99.         printf("Could not allocate video codec context\n");  
  100.         return -1;  
  101.     }  
  102.   
  103.     pCodecParserCtx=av_parser_init(codec_id);  
  104.     if (!pCodecParserCtx){  
  105.         printf("Could not allocate video parser context\n");  
  106.         return -1;  
  107.     }  
  108.   
  109.     //if(pCodec->capabilities&CODEC_CAP_TRUNCATED)  
  110.     //    pCodecCtx->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */  
  111.       
  112.     if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {  
  113.         printf("Could not open codec\n");  
  114.         return -1;  
  115.     }  
  116.     //Input File  
  117.     fp_in = fopen(filepath_in, "rb");  
  118.     if (!fp_in) {  
  119.         printf("Could not open input stream\n");  
  120.         return -1;  
  121.     }  
  122.     //Output File  
  123.     fp_out = fopen(filepath_out, "wb");  
  124.     if (!fp_out) {  
  125.         printf("Could not open output YUV file\n");  
  126.         return -1;  
  127.     }  
  128.   
  129.     pFrame = av_frame_alloc();  
  130.     av_init_packet(&packet);  
  131.   
  132.   
  133.     while (1) {  
  134.         cur_size = fread(in_buffer, 1, in_buffer_size, fp_in);  
  135.         if (cur_size == 0)  
  136.             break;  
  137.         cur_ptr=in_buffer;  
  138.   
  139.         while (cur_size>0){  
  140.   
  141.             int len = av_parser_parse2(  
  142.                 pCodecParserCtx, pCodecCtx,  
  143.                 &packet.data, &packet.size,  
  144.                 cur_ptr , cur_size ,  
  145.                 AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);  
  146.   
  147.             cur_ptr += len;  
  148.             cur_size -= len;  
  149.   
  150.             if(packet.size==0)  
  151.                 continue;  
  152.   
  153.             //Some Info from AVCodecParserContext  
  154.             printf("Packet Size:%6d\t",packet.size);  
  155.             switch(pCodecParserCtx->pict_type){  
  156.                 case AV_PICTURE_TYPE_I: printf("Type: I\t");break;  
  157.                 case AV_PICTURE_TYPE_P: printf("Type: P\t");break;  
  158.                 case AV_PICTURE_TYPE_B: printf("Type: B\t");break;  
  159.                 default: printf("Type: Other\t");break;  
  160.             }  
  161.             printf("Output Number:%4d\t",pCodecParserCtx->output_picture_number);  
  162.             printf("Offset:%lld\n",pCodecParserCtx->cur_offset);  
  163.   
  164.             ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);  
  165.             if (ret < 0) {  
  166.                 printf("Decode Error.\n");  
  167.                 return ret;  
  168.             }  
  169.             if (got_picture) {  
  170.                 if(first_time){  
  171.                     printf("\nCodec Full Name:%s\n",pCodecCtx->codec->long_name);  
  172.                     printf("width:%d\nheight:%d\n\n",pCodecCtx->width,pCodecCtx->height);  
  173.                     //SwsContext  
  174.                     img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,   
  175.                         pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  176.                       
  177.                     pFrameYUV=av_frame_alloc();  
  178.                     out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
  179.                     avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  180.                       
  181.                     y_size=pCodecCtx->width*pCodecCtx->height;  
  182.   
  183.                     first_time=0;  
  184.                 }  
  185.   
  186.                 printf("Succeed to decode 1 frame!\n");  
  187.                 sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,   
  188.                     pFrameYUV->data, pFrameYUV->linesize);  
  189.   
  190.                 fwrite(pFrameYUV->data[0],1,y_size,fp_out);     //Y   
  191.                 fwrite(pFrameYUV->data[1],1,y_size/4,fp_out);   //U  
  192.                 fwrite(pFrameYUV->data[2],1,y_size/4,fp_out);   //V  
  193.             }  
  194.         }  
  195.   
  196.     }  
  197.   
  198.     //Flush Decoder  
  199.     packet.data = NULL;  
  200.     packet.size = 0;  
  201.     while(1){  
  202.         ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet);  
  203.         if (ret < 0) {  
  204.             printf("Decode Error.\n");  
  205.             return ret;  
  206.         }  
  207.         if (!got_picture)  
  208.             break;  
  209.         if (got_picture) {  
  210.             printf("Flush Decoder: Succeed to decode 1 frame!\n");  
  211.             sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,   
  212.                 pFrameYUV->data, pFrameYUV->linesize);  
  213.   
  214.             fwrite(pFrameYUV->data[0],1,y_size,fp_out);     //Y  
  215.             fwrite(pFrameYUV->data[1],1,y_size/4,fp_out);   //U  
  216.             fwrite(pFrameYUV->data[2],1,y_size/4,fp_out);   //V  
  217.         }  
  218.     }  
  219.   
  220.     fclose(fp_in);  
  221.     fclose(fp_out);  
  222.       
  223.     sws_freeContext(img_convert_ctx);  
  224.     av_parser_close(pCodecParserCtx);  
  225.   
  226.     av_frame_free(&pFrameYUV);  
  227.     av_frame_free(&pFrame);  
  228.     avcodec_close(pCodecCtx);  
  229.     av_free(pCodecCtx);  
  230.   
  231.     return 0;  
  232. }  



 

运行结果

通过设定定义在程序开始的宏,确定需要使用的解码器。
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //test different codec  
  2. #define TEST_H264  0  
  3. #define TEST_HEVC  1  

当TEST_H264设置为1的时候,解码H.264文件“bigbuckbunny_480x272.h264”。
当TEST_HEVC设置为1的时候,解码HEVC文件“bigbuckbunny_480x272.hevc”。

解码后的数据保存成YUV420P格式的文件“bigbuckbunny_480x272.yuv”。

此外,程序在运行的过程中,会打印出AVCodecParserContext中的一些信息,比如说帧类型等等,如下图所示。


输入H.264码流如下所示。


输出YUV420P像素数据如下图所示。



下载


Simplest ffmpeg decoder pure工程被作为子工程添加到了simplest ffmpeg player 2工程中。新版的simplest ffmpeg player 2工程的信息如下。


Simplest ffmpeg player 2


项目主页

SourceForge:https://sourceforge.net/projects/simplestffmpegplayer/

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

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


本程序实现了视频文件的解码和显示(支持HEVC,H.264,MPEG2等)。
是最简单的FFmpeg视频解码方面的教程。
通过学习本例子可以了解FFmpeg的解码流程。
项目包含3个工程:
simplest_ffmpeg_player:标准版,FFmpeg学习的开始。
simplest_ffmpeg_player_su:SU(SDL Update)版,加入了简单的SDL的Event。
simplest_ffmpeg_decoder_pure:一个纯净的解码器。


version 2.3========================================

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


更新-2.4(2015.2.13)===============================

这次考虑到了跨平台的要求,调整了源代码。经过这次调整之后,源代码可以在以下平台编译通过:

VC++:打开sln文件即可编译,无需配置。

cl.exe:打开compile_cl.bat即可命令行下使用cl.exe进行编译,注意可能需要按照VC的安装路径调整脚本里面的参数。编译命令如下。

[plain] view plaincopy
  1. ::VS2010 Environment  
  2. call "D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"  
  3. ::include  
  4. @set INCLUDE=include;%INCLUDE%  
  5. ::lib  
  6. @set LIB=lib;%LIB%  
  7. ::compile and link  
  8. cl simplest_ffmpeg_decoder_pure.cpp /link avcodec.lib avutil.lib swscale.lib ^  
  9. /OPT:NOREF  

MinGW:MinGW命令行下运行compile_mingw.sh即可使用MinGW的g++进行编译。编译命令如下。

[plain] view plaincopy
  1. g++ simplest_ffmpeg_decoder_pure.cpp -g -o simplest_ffmpeg_decoder_pure.exe \  
  2. -I /usr/local/include -L /usr/local/lib -lavcodec -lavutil -lswscale  

GCC:Linux或者MacOS命令行下运行compile_gcc.sh即可使用GCC进行编译。编译命令如下。

[plain] view plaincopy
  1. gcc simplest_ffmpeg_decoder_pure.cpp -g -o simplest_ffmpeg_decoder_pure.out -I /usr/local/include -L /usr/local/lib \  
  2. -lavcodec -lavutil -lswscale  

PS:相关的编译命令已经保存到了工程文件夹中

CSDN项目下载地址:http://download.csdn.net/detail/leixiaohua1020/8443943
SourceForge上已经更新。

0 0