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)。
代码
下面直接贴上代码
- *
- * 本程序实现了YUV像素数据编码为视频码流(HEVC(H.265),H264,MPEG2,VP8等等)。
- * 是最简单的FFmpeg视频编码方面的教程。
- * 通过学习本例子可以了解FFmpeg的编码流程。
- * This software encode YUV420P data to HEVC(H.265) bitstream (or
- * H.264, MPEG2, VP8 etc.).
- * It's the simplest video encoding software based on FFmpeg.
- * Suitable for beginner of FFmpeg
- */
- #include <stdio.h>
- extern "C"
- {
- #include "libavutil\opt.h"
- #include "libavcodec\avcodec.h"
- #include "libavformat\avformat.h"
- #include "libswscale\swscale.h"
- };
- int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)
- {
- int ret;
- int got_frame;
- AVPacket enc_pkt;
- if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
- CODEC_CAP_DELAY))
- return 0;
- while (1) {
- printf("Flushing stream #%u encoder\n", stream_index);
- //ret = encode_write_frame(NULL, stream_index, &got_frame);
- enc_pkt.data = NULL;
- enc_pkt.size = 0;
- av_init_packet(&enc_pkt);
- ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
- NULL, &got_frame);
- av_frame_free(NULL);
- if (ret < 0)
- break;
- if (!got_frame){
- ret=0;
- break;
- }
- printf("Succeed to encode 1 frame! 编码成功1帧!\n");
- /* mux encoded frame */
- ret = av_write_frame(fmt_ctx, &enc_pkt);
- if (ret < 0)
- break;
- }
- return ret;
- }
- int main(int argc, char* argv[])
- {
- AVFormatContext* pFormatCtx;
- AVOutputFormat* fmt;
- AVStream* video_st;
- AVCodecContext* pCodecCtx;
- AVCodec* pCodec;
- uint8_t* picture_buf;
- AVFrame* picture;
- int size;
- //FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input YUV data 视频YUV源文件
- FILE *in_file = fopen("ds_480x272.yuv", "rb"); //Input YUV data 视频YUV源文件
- int in_w=480,in_h=272;//宽高
- //Frames to encode
- int framenum=100;
- //const char* out_file = "src01.h264"; //Output Filepath 输出文件路径
- //const char* out_file = "src01.ts";
- //const char* out_file = "src01.hevc";
- const char* out_file = "ds.hevc";
- av_register_all();
- //Method1 方法1.组合使用几个函数
- pFormatCtx = avformat_alloc_context();
- //Guess Format 猜格式
- fmt = av_guess_format(NULL, out_file, NULL);
- pFormatCtx->oformat = fmt;
- //Method 2 方法2.更加自动化一些
- //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
- //fmt = pFormatCtx->oformat;
- //Output Format 注意输出路径
- if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)
- {
- printf("Failed to open output file! 输出文件打开失败");
- return -1;
- }
- video_st = avformat_new_stream(pFormatCtx, 0);
- video_st->time_base.num = 1;
- video_st->time_base.den = 25;
- if (video_st==NULL)
- {
- return -1;
- }
- //Param that must set
- pCodecCtx = video_st->codec;
- //pCodecCtx->codec_id =AV_CODEC_ID_HEVC;
- pCodecCtx->codec_id = fmt->video_codec;
- pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
- pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
- pCodecCtx->width = in_w;
- pCodecCtx->height = in_h;
- pCodecCtx->time_base.num = 1;
- pCodecCtx->time_base.den = 25;
- pCodecCtx->bit_rate = 400000;
- pCodecCtx->gop_size=250;
- //H264
- //pCodecCtx->me_range = 16;
- //pCodecCtx->max_qdiff = 4;
- //pCodecCtx->qcompress = 0.6;
- pCodecCtx->qmin = 10;
- pCodecCtx->qmax = 51;
- //Optional Param
- pCodecCtx->max_b_frames=3;
- // Set Option
- AVDictionary *param = 0;
- //H.264
- if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
- av_dict_set(?m, "preset", "slow", 0);
- av_dict_set(?m, "tune", "zerolatency", 0);
- }
- //H.265
- if(pCodecCtx->codec_id == AV_CODEC_ID_H265){
- av_dict_set(?m, "x265-params", "qp=20", 0);
- av_dict_set(?m, "preset", "ultrafast", 0);
- av_dict_set(?m, "tune", "zero-latency", 0);
- }
- //Dump Information 输出格式信息
- av_dump_format(pFormatCtx, 0, out_file, 1);
- pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
- if (!pCodec){
- printf("Can not find encoder! 没有找到合适的编码器!\n");
- return -1;
- }
- if (avcodec_open2(pCodecCtx, pCodec,?m) < 0){
- printf("Failed to open encoder! 编码器打开失败!\n");
- return -1;
- }
- picture = avcodec_alloc_frame();
- size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
- picture_buf = (uint8_t *)av_malloc(size);
- avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
- //Write File Header 写文件头
- avformat_write_header(pFormatCtx,NULL);
- AVPacket pkt;
- int y_size = pCodecCtx->width * pCodecCtx->height;
- av_new_packet(&pkt,y_size*3);
- for (int i=0; i<framenum; i++){
- //Read YUV 读入YUV
- if (fread(picture_buf, 1, y_size*3/2, in_file) < 0){
- printf("Failed to read YUV data! 文件读取错误\n");
- return -1;
- }else if(feof(in_file)){
- break;
- }
- picture->data[0] = picture_buf; // 亮度Y
- picture->data[1] = picture_buf+ y_size; // U
- picture->data[2] = picture_buf+ y_size*5/4; // V
- //PTS
- picture->pts=i;
- int got_picture=0;
- //Encode 编码
- int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
- if(ret < 0){
- printf("Failed to encode! 编码错误!\n");
- return -1;
- }
- if (got_picture==1){
- printf("Succeed to encode 1 frame! 编码成功1帧!\n");
- pkt.stream_index = video_st->index;
- ret = av_write_frame(pFormatCtx, &pkt);
- av_free_packet(&pkt);
- }
- }
- //Flush Encoder
- int ret = flush_encoder(pFormatCtx,0);
- if (ret < 0) {
- printf("Flushing encoder failed\n");
- return -1;
- }
- //Write file trailer 写文件尾
- av_write_trailer(pFormatCtx);
- //Clean 清理
- if (video_st){
- avcodec_close(video_st->codec);
- av_free(picture);
- av_free(picture_buf);
- }
- avio_close(pFormatCtx->pb);
- avformat_free_context(pFormatCtx);
- fclose(in_file);
- return 0;
- }
雷霄骅 (Lei Xiaohua)
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020版权声明:本文为博主原创文章,未经博主允许不得转载。
YUV编码为HEVC(H.265)相关推荐
- 高效视频编码 (HEVC) -H.265(结构解析)
版本 HEVC (H.265) 规范的第一个版本于 2013 年 4 月发布.该标准的版本如下: ITU-T H.265 (V1) (04/2013) http://handle.itu.int/11 ...
- HEVC/H.265编码HM码率控制
HEVC/H.265编码标准HM平台码率控制流程 研究生期间了解过HM平台的码率控制过程,现在时隔2年多又回顾了一下HM平台的码控流程,发现相较之前多了CpbSaturationEnabled这么个机 ...
- 二、对HEVC/H.265视频编解码器进行隐写的基本思路
二.对HEVC/H.265视频编解码器进行隐写的基本思路 概述 1.视频隐写的基本思路 2.视频隐写的举例说明 3.结尾 概述 其实对视频隐写.图像隐写或是音频隐写,基本的思路都是一样的:读取原始图像 ...
- SONY索尼A7S3相机HEVC|H.265视频RSV损坏修复MP4
继佳能和松下相机HEVC|H.265断电视频文件成功修复后,终于迎来索尼HEVC|H.265编码损坏视频修复.回想以往索尼微单的MP4视频文件,使用的都是H.264视频编码技术,从A7S3微单开始支持 ...
- AV1比HEVC/H.265简单对比
AV1是由开放媒体视频联盟(Alliance of Open Media Video)开发的开放.免版税的下一代视频编码格式.它被设计为取代谷歌的VP9,并与H.265/HEVC竞争.AV1的目标是在 ...
- 修改Chromium源码实现HEVC/H.265 4K视频播放
本文作者:蔡斯杰,字节跳动互娱前端业务负责人 公司内容生产端最近(2019/10)在推广 HEVC/H.265 的使用,这种视频编码格式对比H.264更加先进且节省带宽,虽然先进但是因为专利费的问题, ...
- HEVC/H.265(1)——入门初步了解
一.引子 说到H.265,个人的初始印象就是比H.264高端了那么一些的编码标准,再就是在学校的BT上下的那个好多播放器都播放不了,只有potplayer等一小部分未来播放器能播放的权利的游戏HEVC ...
- 【miscellaneous】最新HEVC/H.265 4K视频,显卡解码测试
转载自:http://bbs.zol.com.cn/diybbs/d34441_76103.html 4K这个概念也在最近几年开始流行了起来,无论是4K显示器.4K电视盒子,还是4K游戏对硬件的要求也 ...
- HEVC (H.265)介绍(转)
[Liupin]: 这是一篇简单介绍H.265文章,我接触和开发H.265二年来,H.265技术在行业内接收速度比H.264快多了,现在国际和国内各大公司都在进行H.265应用,不管是IC设计还是H. ...
- HEVC/H.265硬件编码器实现杂谈
国际视频编码标准HEVC已经发布两年有余,市场上关于支持HEVC的硬件也日益涌现,本文借鉴了各方面资源做了综合与概述,给出了HEVC硬件编码器实现的基本方法等重要网络资源. 一.系统设计要点 对于HE ...
最新文章
- 关于 TApplication 详解 三 ---- TComponent
- Linux完全兼容POSIX1.0标准的特性
- TensorFlow官方文档中的sub 和mul中的函数已经在API中改名了
- Thymeleaf 常用属性
- GAN做图像翻译的一点总结
- 逆向工程核心原理学习笔记(十三):分析abex' crackme #1 的延伸:将参数压入栈
- php读取客户机本地时间,PHP如何获取客户端时区以及准确显示所在地时间
- Eclipse中自动创建set、get方法
- php include 导航栏,PHP全栈开发(八):CSS Ⅹ 导航栏制作
- mvc+EF实现简单的登陆功能
- 从CarLife音乐切换回蓝牙音乐音量变小
- Excel黑科技——含合并单元格的同行求和并下拉自动填充
- 安装Dev c++后,编译文件出现未编译的解决方法
- 菲尔博士的三十六交际方式
- transform 属性 实现3D立体相册
- rsa public key not find与Generate First a serial的解决方案-6.13日摸索总结
- [c#]喜马拉雅、蜻蜓、荔枝FM音频批量下载器V1.3 by Levme开发手记
- [安洵杯 2019]不是文件上传
- CAD杀毒V2.6 正式版
- Java自动生成数据库设计文档(Word)