YUV编码为HEVC(H.265)



流程

下面附上一张FFmpeg编码视频的流程图。通过该流程,不仅可以编码H.264/H.265的码流,而且可以编码MPEG4/MPEG2/VP9/VP8等多种码流。实际上使用FFmpeg编码视频的方式都是一样的。图中蓝色背景的函数是实际输出数据的函数。浅绿色的函数是视频编码的函数。


简单介绍一下流程中各个函数的意义
av_register_all():注册FFmpeg所有编解码器。
avformat_alloc_output_context2():初始化输出码流的AVFormatContext。
avio_open():打开输出文件。
av_new_stream():创建输出码流的AVStream。
avcodec_find_encoder():查找编码器。
avcodec_open2():打开编码器。
avformat_write_header():写文件头(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)。
avcodec_encode_video2():编码一帧视频。即将AVFrame(存储YUV像素数据)编码为AVPacket(存储H.264等格式的码流数据)。
av_write_frame():将编码后的视频码流写入文件。
flush_encoder():输入的像素数据读取完成后调用此函数。用于输出编码器中剩余的AVPacket。
av_write_trailer():写文件尾(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)。


代码

下面直接贴上代码

  1.  *  
  2.  * 本程序实现了YUV像素数据编码为视频码流(HEVC(H.265),H264,MPEG2,VP8等等)。 
  3.  * 是最简单的FFmpeg视频编码方面的教程。 
  4.  * 通过学习本例子可以了解FFmpeg的编码流程。 
  5.  * This software encode YUV420P data to HEVC(H.265) bitstream (or 
  6.  * H.264, MPEG2, VP8 etc.). 
  7.  * It's the simplest video encoding software based on FFmpeg.  
  8.  * Suitable for beginner of FFmpeg  
  9.  */  
  10.   
  11. #include <stdio.h>  
  12.   
  13. extern "C"  
  14. {  
  15. #include "libavutil\opt.h"  
  16. #include "libavcodec\avcodec.h"  
  17. #include "libavformat\avformat.h"  
  18. #include "libswscale\swscale.h"  
  19. };  
  20.   
  21.   
  22. int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)  
  23. {  
  24.     int ret;  
  25.     int got_frame;  
  26.     AVPacket enc_pkt;  
  27.     if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &  
  28.         CODEC_CAP_DELAY))  
  29.         return 0;  
  30.     while (1) {  
  31.         printf("Flushing stream #%u encoder\n", stream_index);  
  32.         //ret = encode_write_frame(NULL, stream_index, &got_frame);  
  33.         enc_pkt.data = NULL;  
  34.         enc_pkt.size = 0;  
  35.         av_init_packet(&enc_pkt);  
  36.         ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,  
  37.             NULL, &got_frame);  
  38.         av_frame_free(NULL);  
  39.         if (ret < 0)  
  40.             break;  
  41.         if (!got_frame){  
  42.             ret=0;  
  43.             break;  
  44.         }  
  45.         printf("Succeed to encode 1 frame! 编码成功1帧!\n");  
  46.         /* mux encoded frame */  
  47.         ret = av_write_frame(fmt_ctx, &enc_pkt);  
  48.         if (ret < 0)  
  49.             break;  
  50.     }  
  51.     return ret;  
  52. }  
  53.   
  54. int main(int argc, char* argv[])  
  55. {  
  56.     AVFormatContext* pFormatCtx;  
  57.     AVOutputFormat* fmt;  
  58.     AVStream* video_st;  
  59.     AVCodecContext* pCodecCtx;  
  60.     AVCodec* pCodec;  
  61.   
  62.     uint8_t* picture_buf;  
  63.     AVFrame* picture;  
  64.     int size;  
  65.   
  66.     //FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input YUV data 视频YUV源文件   
  67.     FILE *in_file = fopen("ds_480x272.yuv""rb");  //Input YUV data 视频YUV源文件   
  68.     int in_w=480,in_h=272;//宽高    
  69.     //Frames to encode  
  70.     int framenum=100;  
  71.     //const char* out_file = "src01.h264";  //Output Filepath 输出文件路径  
  72.     //const char* out_file = "src01.ts";  
  73.     //const char* out_file = "src01.hevc";  
  74.     const char* out_file = "ds.hevc";  
  75.   
  76.     av_register_all();  
  77.     //Method1 方法1.组合使用几个函数  
  78.     pFormatCtx = avformat_alloc_context();  
  79.     //Guess Format 猜格式  
  80.     fmt = av_guess_format(NULL, out_file, NULL);  
  81.     pFormatCtx->oformat = fmt;  
  82.       
  83.     //Method 2 方法2.更加自动化一些  
  84.     //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);  
  85.     //fmt = pFormatCtx->oformat;  
  86.   
  87.   
  88.     //Output Format 注意输出路径  
  89.     if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)  
  90.     {  
  91.         printf("Failed to open output file! 输出文件打开失败");  
  92.         return -1;  
  93.     }  
  94.   
  95.     video_st = avformat_new_stream(pFormatCtx, 0);  
  96.     video_st->time_base.num = 1;   
  97.     video_st->time_base.den = 25;    
  98.   
  99.     if (video_st==NULL)  
  100.     {  
  101.         return -1;  
  102.     }  
  103.     //Param that must set  
  104.     pCodecCtx = video_st->codec;  
  105.     //pCodecCtx->codec_id =AV_CODEC_ID_HEVC;  
  106.     pCodecCtx->codec_id = fmt->video_codec;  
  107.     pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;  
  108.     pCodecCtx->pix_fmt = PIX_FMT_YUV420P;  
  109.     pCodecCtx->width = in_w;    
  110.     pCodecCtx->height = in_h;  
  111.     pCodecCtx->time_base.num = 1;    
  112.     pCodecCtx->time_base.den = 25;    
  113.     pCodecCtx->bit_rate = 400000;    
  114.     pCodecCtx->gop_size=250;  
  115.     //H264  
  116.     //pCodecCtx->me_range = 16;  
  117.     //pCodecCtx->max_qdiff = 4;  
  118.     //pCodecCtx->qcompress = 0.6;  
  119.     pCodecCtx->qmin = 10;  
  120.     pCodecCtx->qmax = 51;  
  121.   
  122.     //Optional Param  
  123.     pCodecCtx->max_b_frames=3;  
  124.   
  125.     // Set Option  
  126.     AVDictionary *param = 0;  
  127.     //H.264  
  128.     if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {  
  129.         av_dict_set(?m, "preset""slow", 0);  
  130.         av_dict_set(?m, "tune""zerolatency", 0);  
  131.     }  
  132.     //H.265  
  133.     if(pCodecCtx->codec_id == AV_CODEC_ID_H265){  
  134.         av_dict_set(?m, "x265-params""qp=20", 0);  
  135.         av_dict_set(?m, "preset""ultrafast", 0);  
  136.         av_dict_set(?m, "tune""zero-latency", 0);  
  137.     }  
  138.   
  139.     //Dump Information 输出格式信息  
  140.     av_dump_format(pFormatCtx, 0, out_file, 1);  
  141.   
  142.     pCodec = avcodec_find_encoder(pCodecCtx->codec_id);  
  143.     if (!pCodec){  
  144.         printf("Can not find encoder! 没有找到合适的编码器!\n");  
  145.         return -1;  
  146.     }  
  147.     if (avcodec_open2(pCodecCtx, pCodec,?m) < 0){  
  148.         printf("Failed to open encoder! 编码器打开失败!\n");  
  149.         return -1;  
  150.     }  
  151.       
  152.   
  153.   
  154.     picture = avcodec_alloc_frame();  
  155.     size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);  
  156.     picture_buf = (uint8_t *)av_malloc(size);  
  157.     avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);  
  158.   
  159.     //Write File Header 写文件头  
  160.     avformat_write_header(pFormatCtx,NULL);  
  161.   
  162.     AVPacket pkt;  
  163.     int y_size = pCodecCtx->width * pCodecCtx->height;  
  164.     av_new_packet(&pkt,y_size*3);  
  165.   
  166.     for (int i=0; i<framenum; i++){  
  167.         //Read YUV 读入YUV  
  168.         if (fread(picture_buf, 1, y_size*3/2, in_file) < 0){  
  169.             printf("Failed to read YUV data! 文件读取错误\n");  
  170.             return -1;  
  171.         }else if(feof(in_file)){  
  172.             break;  
  173.         }  
  174.         picture->data[0] = picture_buf;  // 亮度Y  
  175.         picture->data[1] = picture_buf+ y_size;  // U   
  176.         picture->data[2] = picture_buf+ y_size*5/4; // V  
  177.         //PTS  
  178.         picture->pts=i;  
  179.         int got_picture=0;  
  180.         //Encode 编码  
  181.         int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);  
  182.         if(ret < 0){  
  183.             printf("Failed to encode! 编码错误!\n");  
  184.             return -1;  
  185.         }  
  186.         if (got_picture==1){  
  187.             printf("Succeed to encode 1 frame! 编码成功1帧!\n");  
  188.             pkt.stream_index = video_st->index;  
  189.             ret = av_write_frame(pFormatCtx, &pkt);  
  190.             av_free_packet(&pkt);  
  191.         }  
  192.     }  
  193.     //Flush Encoder  
  194.     int ret = flush_encoder(pFormatCtx,0);  
  195.     if (ret < 0) {  
  196.         printf("Flushing encoder failed\n");  
  197.         return -1;  
  198.     }  
  199.   
  200.     //Write file trailer 写文件尾  
  201.     av_write_trailer(pFormatCtx);  
  202.   
  203.     //Clean 清理  
  204.     if (video_st){  
  205.         avcodec_close(video_st->codec);  
  206.         av_free(picture);  
  207.         av_free(picture_buf);  
  208.     }  
  209.     avio_close(pFormatCtx->pb);  
  210.     avformat_free_context(pFormatCtx);  
  211.   
  212.     fclose(in_file);  
  213.   
  214.     return 0;  
  215. }  
  216. 雷霄骅 (Lei Xiaohua)
    [email protected]
    http://blog.csdn.net/leixiaohua1020


    版权声明:本文为博主原创文章,未经博主允许不得转载。



雷霄骅 (Lei Xiaohua)
[email protected]
http://blog.csdn.net/leixiaohua1020


猜你喜欢

转载自blog.csdn.net/ZH952016281/article/details/52769357