视频解码
// ffmpegdemo.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
};
#else//Linux...
#ifdef __cplusplus
extern "C"
{
#endif#include
#include
#include
#include
#ifdef __cplusplus
};
#endif
#endifint main()
{//文件格式上下文AVFormatContext *pFormatCtx;int i &#61; 0, videoindex;AVCodecContext *pCodecCtx;AVCodec *pCodec;AVFrame *pFrame, *pFrameYUV;unsigned char *out_buffer;AVPacket *packet;int y_size;int ret, got_picture;struct SwsContext *img_convert_ctx;char filepath[] &#61; "input.mkv";FILE *fp_yuv &#61; fopen("output.yuv", "wb&#43;");av_register_all();avformat_network_init();pFormatCtx &#61; avformat_alloc_context();if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) !&#61; 0) {printf("Couldn&#39;t open input stream.\n");return -1;}//读取一部分视音频数据并且获得一些相关的信息if (avformat_find_stream_info(pFormatCtx, NULL) <0) {printf("Couldn&#39;t find stream information.\n");return -1;}//查找视频编码索引videoindex &#61; -1;for (i &#61; 0; i
}
视频编码
// ffmpegdemo.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
};
#else//Linux...
#ifdef __cplusplus
extern "C"
{
#endif#include
#include
#include
#include
#ifdef __cplusplus
};
#endif
#endifint 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 (true) {enc_pkt.data &#61; NULL;enc_pkt.size &#61; 0;av_init_packet(&enc_pkt);ret &#61; 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 &#61; 0;break;}printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size);/* mux encoded frame */ret &#61; av_write_frame(fmt_ctx, &enc_pkt);if (ret <0){break;}}return ret;
}int main()
{AVFormatContext* pFormatCtx;AVOutputFormat* pOutputFmt;AVStream* video_st;AVCodecContext* pCodecCtx;AVCodec* pCodec;AVPacket pkt;uint8_t* picture_buf;AVFrame* pFrame;int picture_size;int y_size;int framecnt &#61; 0;int in_w &#61; 640, in_h &#61; 272; //视频图像宽高const char* out_file &#61; "output.h264";int i, j;int num;int index_y, index_u, index_v;uint8_t *y_, *u_, *v_, *in;int got_picture &#61; 0;int ret;av_register_all();//alloc avformat contextpFormatCtx &#61; avformat_alloc_context();//猜测类型,返回输出类型。pOutputFmt &#61; av_guess_format(NULL, out_file, NULL);pFormatCtx->oformat &#61; pOutputFmt;//打开FFmpeg的输入输出文件,成功之后创建的AVIOContext结构体。if (avio_open2(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE,NULL,NULL) <0) {//Failed to open output filereturn -1;}//除了以下方法&#xff0c;另外还可以使用avcodec_find_encoder_by_name()来获取AVCodec pCodec &#61; avcodec_find_encoder(pOutputFmt->video_codec);//获取编码器 if (!pCodec) {//cannot find encoder return -1;}pCodecCtx &#61; avcodec_alloc_context3(pCodec);//申请AVCodecContext&#xff0c;并初始化。 if (!pCodecCtx) {//failed get AVCodecContext return -1;}FILE * in_file &#61; fopen("640-272.yuv", "rb"); //打开原始yuv数据文件/* 创建输出码流的AVStream */video_st &#61; avformat_new_stream(pFormatCtx, 0);video_st->time_base.num &#61; 1;video_st->time_base.den &#61; 25;if (video_st &#61;&#61; NULL) {return -1;}//Param that must set pCodecCtx &#61; video_st->codec;//pCodecCtx->codec_id &#61;AV_CODEC_ID_HEVC; //H265pCodecCtx->codec_id &#61; AV_CODEC_ID_H264;pCodecCtx->codec_type &#61; AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt &#61; AV_PIX_FMT_YUV420P;pCodecCtx->width &#61; in_w;pCodecCtx->height &#61; in_h;pCodecCtx->b_frame_strategy &#61; true;/*码率bit_rate/-bt tolerance 设置视频码率容忍度kbit/s &#xff08;固定误差&#xff09;rc_max_rate/-maxrate bitrate设置最大视频码率容忍度 &#xff08;可变误差&#xff09;rc_min_rate/-minrate bitreate 设置最小视频码率容忍度&#xff08;可变误差&#xff09;rc_buffer_size/-bufsize size 设置码率控制缓冲区大小如何设置固定码率编码 ?bit_rate是平均码率&#xff0c;不一定能控制住c->bit_rate &#61; 400000;c->rc_max_rate &#61; 400000;c->rc_min_rate &#61; 400000;提示 [libx264 &#64; 00c70be0] VBV maxrate specified, but no bufsize, ignored再设置 c->rc_buffer_size &#61; 200000; 即可。如此控制后编码质量明显差了。*/pCodecCtx->bit_rate &#61; 400000; //采样码率越大&#xff0c;目标文件越大//pCodecCtx->bit_rate_tolerance &#61; 8000000; // 码率误差&#xff0c;允许的误差越大&#xff0c;视频越小//两个I帧之间的间隔pCodecCtx->gop_size &#61; 15;//编码帧率&#xff0c;每秒多少帧。下面表示1秒25帧pCodecCtx->time_base.num &#61; 1;pCodecCtx->time_base.den &#61; 25;//最小的量化因子pCodecCtx->qmin &#61; 10;//最大的量化因子pCodecCtx->qmax &#61; 30;//最大B帧数pCodecCtx->max_b_frames &#61; 3;// Set Option AVDictionary *param &#61; 0;//H.264 if (pCodecCtx->codec_id &#61;&#61; AV_CODEC_ID_H264) {/*preset的参数主要调节编码速度和质量的平衡&#xff0c;有ultrafast、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo这10个选项&#xff0c;从快到慢。*/av_dict_set(¶m, "preset", "fast", 0);/* tune的参数主要配合视频类型和视觉优化的参数。tune的值有&#xff1a; film&#xff1a; 电影、真人类型&#xff1b;animation&#xff1a; 动画&#xff1b;grain&#xff1a; 需要保留大量的grain时用&#xff1b;stillimage&#xff1a; 静态图像编码时使用&#xff1b;psnr&#xff1a; 为提高psnr做了优化的参数&#xff1b;ssim&#xff1a; 为提高ssim做了优化的参数&#xff1b;fastdecode&#xff1a; 可以快速解码的参数&#xff1b;zerolatency&#xff1a;零延迟&#xff0c;用在需要非常低的延迟的情况下&#xff0c;比如电视电话会议的编码。*/av_dict_set(¶m, "tune", "zerolatency", 0);/*画质,分别是baseline, extended, main, high1、Baseline Profile&#xff1a;基本画质。支持I/P 帧&#xff0c;只支持无交错&#xff08;Progressive&#xff09;和CAVLC&#xff1b;2、Extended profile&#xff1a;进阶画质。支持I/P/B/SP/SI 帧&#xff0c;只支持无交错&#xff08;Progressive&#xff09;和CAVLC&#xff1b;(用的少)3、Main profile&#xff1a;主流画质。提供I/P/B 帧&#xff0c;支持无交错&#xff08;Progressive&#xff09;和交错&#xff08;Interlaced&#xff09;&#xff0c; 也支持CAVLC 和CABAC 的支持&#xff1b;4、High profile&#xff1a;高级画质。在main Profile 的基础上增加了8x8内部预测、自定义量化、 无损视频编码和更多的YUV 格式&#xff1b;H.264 Baseline profile、Extended profile和Main profile都是针对8位样本数据、4:2:0格式(YUV)的视频序列。在相同配置情况下&#xff0c;High profile&#xff08;HP&#xff09;可以比Main profile&#xff08;MP&#xff09;降低10%的码率。 根据应用领域的不同&#xff0c;Baseline profile多应用于实时通信领域&#xff0c;Main profile多应用于流媒体领域&#xff0c;High profile则多应用于广电和存储领域。*///av_dict_set(¶m, "profile", "main", 0); }else if (pCodecCtx->codec_id &#61;&#61; AV_CODEC_ID_H265 || pCodecCtx->codec_id &#61;&#61; AV_CODEC_ID_HEVC){av_dict_set(¶m, "preset", "fast", 0);av_dict_set(¶m, "tune", "zerolatency", 0);}//Output Info-----------------------------printf("--------------- out_file Information ----------------\n");//手工调试函数&#xff0c;输出tbn、tbc、tbr、PAR、DAR的含义av_dump_format(pFormatCtx, 0, out_file, 1); //最后一个参数&#xff0c;如果是输出文件时&#xff0c;该值为1&#xff1b;如果是输入文件时&#xff0c;该值为0printf("-----------------------------------------------------\n");/* 查找编码器 */pCodec &#61; 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;}pFrame &#61; av_frame_alloc();picture_size &#61; avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);picture_buf &#61; (uint8_t *)av_malloc(picture_size);avpicture_fill((AVPicture *)pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);//Write File Header /* 写文件头&#xff08;对于某些没有文件头的封装格式&#xff0c;不需要此函数。比如说MPEG2TS&#xff09; */avformat_write_header(pFormatCtx, NULL);/* Allocate the payload of a packet and initialize its fields with default values. */av_new_packet(&pkt, picture_size);j &#61; 1;y_size &#61; pCodecCtx->width * pCodecCtx->height;unsigned int uiReadSize &#61; 0;while (true){//Read raw YUV dataif (feof(in_file)) //文件结束{break;}else{uiReadSize &#61; fread(picture_buf, 1, y_size * 3 / 2, in_file);if (uiReadSize <&#61; 0){printf("Failed to read raw data! \n");break;}else if (uiReadSize
}