最简单的基于FFMPEG的视频编码器(YUV编码为H.264)

来源:互联网 发布:网络教育与电大哪个好 编辑:程序博客网 时间:2024/04/20 07:47

http://blog.csdn.net/leixiaohua1020/article/details/25430425

本文介绍一个最简单的基于FFMPEG的视频编码器。该编码器实现了YUV420P的像素数据编码为H.264的压缩编码数据。编码器代码十分简单,但是每一行代码都很重要,适合好好研究一下。弄清楚了本代码也就基本弄清楚了FFMPEG的编码流程。目前我虽然已经调通了程序,但是还是有些地方没有完全搞明白,需要下一步继续探究然后补充内容。

本程序使用最新版的类库(编译时间为2014.5.6),开发平台为VC2010。所有的配置都已经做好,只需要运行就可以了。

下面直接上代码:

 

[cpp] view plaincopy
  1. /*  
  2.  *最简单的基于FFmpeg的视频编码器 
  3.  *Simplest FFmpeg Video Encoder 
  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.  *本程序实现了YUV像素数据编码为视频码流(H264,MPEG2,VP8等等)。 
  12.  *是最简单的FFmpeg视频编码方面的教程。 
  13.  *通过学习本例子可以了解FFmpeg的编码流程。 
  14.  *This software encode YUV420P data to H.264 bitstream. 
  15.  *It's the simplest video encoding software based on FFmpeg.  
  16.  *Suitable for beginner of FFmpeg  
  17.  */  
  18.   
  19. #include "stdafx.h"  
  20.   
  21. extern "C"  
  22. {  
  23. #include "libavcodec\avcodec.h"  
  24. #include "libavformat\avformat.h"  
  25. #include "libswscale\swscale.h"  
  26. };  
  27.   
  28.   
  29. int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)  
  30. {  
  31.     int ret;  
  32.     int got_frame;  
  33.     AVPacket enc_pkt;  
  34.     if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &  
  35.         CODEC_CAP_DELAY))  
  36.         return 0;  
  37.     while (1) {  
  38.         printf("Flushing stream #%u encoder\n", stream_index);  
  39.         //ret = encode_write_frame(NULL, stream_index, &got_frame);  
  40.         enc_pkt.data = NULL;  
  41.         enc_pkt.size = 0;  
  42.         av_init_packet(&enc_pkt);  
  43.         ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,  
  44.             NULL, &got_frame);  
  45.         av_frame_free(NULL);  
  46.         if (ret < 0)  
  47.             break;  
  48.         if (!got_frame)  
  49.         {ret=0;break;}  
  50.         printf("编码成功1帧!\n");  
  51.         /* mux encoded frame */  
  52.         ret = av_write_frame(fmt_ctx, &enc_pkt);  
  53.         if (ret < 0)  
  54.             break;  
  55.     }  
  56.     return ret;  
  57. }  
  58.   
  59. int _tmain(int argc, _TCHAR* argv[])  
  60. {  
  61.     AVFormatContext* pFormatCtx;  
  62.     AVOutputFormat* fmt;  
  63.     AVStream* video_st;  
  64.     AVCodecContext* pCodecCtx;  
  65.     AVCodec* pCodec;  
  66.   
  67.     uint8_t* picture_buf;  
  68.     AVFrame* picture;  
  69.     int size;  
  70.   
  71.     FILE *in_file = fopen("src01_480x272.yuv""rb");   //视频YUV源文件   
  72.     int in_w=480,in_h=272;//宽高    
  73.     int framenum=50;  
  74.     const char* out_file = "src01.h264";                    //输出文件路径  
  75.   
  76.     av_register_all();  
  77.     //方法1.组合使用几个函数  
  78.     pFormatCtx = avformat_alloc_context();  
  79.     //猜格式  
  80.     fmt = av_guess_format(NULL, out_file, NULL);  
  81.     pFormatCtx->oformat = fmt;  
  82.       
  83.     //方法2.更加自动化一些  
  84.     //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);  
  85.     //fmt = pFormatCtx->oformat;  
  86.   
  87.   
  88.     //注意输出路径  
  89.     if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)  
  90.     {  
  91.         printf("输出文件打开失败");  
  92.         return -1;  
  93.     }  
  94.   
  95.     video_st = av_new_stream(pFormatCtx, 0);  
  96.     if (video_st==NULL)  
  97.     {  
  98.         return -1;  
  99.     }  
  100.     pCodecCtx = video_st->codec;  
  101.     pCodecCtx->codec_id = fmt->video_codec;  
  102.     pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;  
  103.     pCodecCtx->pix_fmt = PIX_FMT_YUV420P;  
  104.     pCodecCtx->width = in_w;    
  105.     pCodecCtx->height = in_h;  
  106.     pCodecCtx->time_base.num = 1;    
  107.     pCodecCtx->time_base.den = 25;    
  108.     pCodecCtx->bit_rate = 400000;    
  109.     pCodecCtx->gop_size=250;  
  110.     //H264  
  111.     //pCodecCtx->me_range = 16;  
  112.     //pCodecCtx->max_qdiff = 4;  
  113.     pCodecCtx->qmin = 10;  
  114.     pCodecCtx->qmax = 51;  
  115.     //pCodecCtx->qcompress = 0.6;  
  116.     //输出格式信息  
  117.     av_dump_format(pFormatCtx, 0, out_file, 1);  
  118.   
  119.     pCodec = avcodec_find_encoder(pCodecCtx->codec_id);  
  120.     if (!pCodec)  
  121.     {  
  122.         printf("没有找到合适的编码器!\n");  
  123.         return -1;  
  124.     }  
  125.     if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0)  
  126.     {  
  127.         printf("编码器打开失败!\n");  
  128.         return -1;  
  129.     }  
  130.     picture = avcodec_alloc_frame();  
  131.     size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);  
  132.     picture_buf = (uint8_t *)av_malloc(size);  
  133.     avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);  
  134.   
  135.     //写文件头  
  136.     avformat_write_header(pFormatCtx,NULL);  
  137.   
  138.     AVPacket pkt;  
  139.     int y_size = pCodecCtx->width * pCodecCtx->height;  
  140.     av_new_packet(&pkt,y_size*3);  
  141.   
  142.     for (int i=0; i<framenum; i++){  
  143.         //读入YUV  
  144.         if (fread(picture_buf, 1, y_size*3/2, in_file) < 0)  
  145.         {  
  146.             printf("文件读取错误\n");  
  147.             return -1;  
  148.         }else if(feof(in_file)){  
  149.             break;  
  150.         }  
  151.         picture->data[0] = picture_buf;  // 亮度Y  
  152.         picture->data[1] = picture_buf+ y_size;  // U   
  153.         picture->data[2] = picture_buf+ y_size*5/4; // V  
  154.         //PTS  
  155.         picture->pts=i;  
  156.         int got_picture=0;  
  157.         //编码  
  158.         int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);  
  159.         if(ret < 0)  
  160.         {  
  161.             printf("编码错误!\n");  
  162.             return -1;  
  163.         }  
  164.         if (got_picture==1)  
  165.         {  
  166.             printf("编码成功1帧!\n");  
  167.             pkt.stream_index = video_st->index;  
  168.             ret = av_write_frame(pFormatCtx, &pkt);  
  169.             av_free_packet(&pkt);  
  170.         }  
  171.     }  
  172.     //Flush Encoder  
  173.     int ret = flush_encoder(pFormatCtx,0);  
  174.     if (ret < 0) {  
  175.         printf("Flushing encoder failed\n");  
  176.         return -1;  
  177.     }  
  178.   
  179.     //写文件尾  
  180.     av_write_trailer(pFormatCtx);  
  181.   
  182.     //清理  
  183.     if (video_st)  
  184.     {  
  185.         avcodec_close(video_st->codec);  
  186.         av_free(picture);  
  187.         av_free(picture_buf);  
  188.     }  
  189.     avio_close(pFormatCtx->pb);  
  190.     avformat_free_context(pFormatCtx);  
  191.   
  192.     fclose(in_file);  
  193.   
  194.     return 0;  
  195. }  

软件运行截图(受限于文件体积,原始YUV帧数很少):


编码前的YUV序列:


编码后的H.264码流:


下载地址:

http://download.csdn.net/detail/leixiaohua1020/7324115

修正记录:

之前发现编码后的H.264码流与YUV输入的帧数不同。经过观察对比其他程序后发现需要调用flush_encoder()将编码器中剩余的视频帧输出。已经将该问题修正。

下载地址(修正后):

http://download.csdn.net/detail/leixiaohua1020/7466649

SourceForge上已经更新。


SourceForge项目地址:

https://sourceforge.net/projects/simplestffmpegvideoencoder/

0 0
原创粉丝点击