目录
- 参考
- 示例说明
- 示例代码
1. 参考
- [1] ffmpeg.org/libavfilter documentation
- [2] ffmpeg.org/Filters Documentation
- [3] 雷霄骅/最简单的基于FFmpeg的AVfilter的例子-纯净版
- [4] Kernel-Tomposon/最简单的基于FFmpeg的AVfilter的例子-修正版
- [5] FFmpeg/doc/examples/filtering_video.c
2. 示例说明
FFmpeg中的libavfilter提供了一个通用的音视频filter框架。使用avfilter可以对音视频数据做一些效果处理如去色调、模糊、水平翻转、裁剪、加方框、叠加文字等功能。
本示例为对YUV视频数据做一些特效处理。输入为一个YUV文件,输出也是一个YUV文件。
程序的流程图如下所示。
AVFilter的初始化比较复杂,需要调用avfilter_graph_config()等一系列函数。数据处理时候的调用比较简单,只有两个函数:av_buffersrc_add_frame()和av_buffersink_get_frame()。
流程中的关键函数如下所示:
- avfilter_graph_alloc():为AVFilterGraph分配内存。
- avfilter_graph_create_filter():创建并向AVFilterGraph中添加一个AVFilterContext。
- avfilter_graph_parse_ptr():将一串通过字符串描述的Graph添加到AVFilterGraph中。
- avfilter_graph_config():检查AVFilterGraph的配置有效性,并配置其中的所有连接和格式。
- av_buffersrc_add_frame():添加一个AVFrame到source filter。
- av_buffersink_get_frame():从sink filter中获取一个AVFrame。
3. 示例代码
#include <stdio.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
char *progname = "";
void usage() {
fprintf(stdout, "usage: %s <input> <width> <height> <filter_descr> <output>\n", progname);
exit(1);
}
int main(int argc, char* argv[])
{
progname = argv[0];
if (argc < 6)
usage();
const char *in_filename = argv[1];
const char *out_filename = argv[5];
int in_width = atoi(argv[2]);
int in_height = atoi(argv[3]);
const char *filter_descr = argv[4];
//const char *filter_descr = "lutyuv='u=128:v=128'";
if (in_width <= 0 || in_height <= 0) {
fprintf(stderr, "invalid parameters.\n");
usage();
}
int ret;
AVFrame *frame_in;
AVFrame *frame_out;
unsigned char *frame_buffer_in;
unsigned char *frame_buffer_out;
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
static int video_stream_index = -1;
FILE *in_fp = fopen(in_filename, "rb+");
if (!in_fp) {
printf("Error open input file.\n");
return -1;
}
FILE *out_fp = fopen(out_filename, "wb+");
if(!out_fp){
printf("Error open output file.\n");
return -1;
}
char args[512];
const AVFilter *buffersrc = avfilter_get_by_name("buffer");
if (!buffersrc) {
fprintf(stderr, "Can't find source fiter 'buffer'\n");
return -1;
}
const AVFilter *buffersink = avfilter_get_by_name("buffersink");
if (!buffersink) {
fprintf(stderr, "Can't find sink fiter 'buffersink'\n");
return -1;
}
AVFilterInOut *outputs = avfilter_inout_alloc();
AVFilterInOut *inputs = avfilter_inout_alloc();
enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
AVBufferSinkParams *buffersink_params;
filter_graph = avfilter_graph_alloc();
/* buffer video source: the decoded frames from the decoder will be inserted here. */
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
in_width,in_height,AV_PIX_FMT_YUV420P,
1, 25,1,1);
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
args, NULL, filter_graph);
if (ret < 0) {
printf("Cannot create buffer source\n");
return ret;
}
/* buffer video sink: to terminate the filter chain. */
buffersink_params = av_buffersink_params_alloc();
buffersink_params->pixel_fmts = pix_fmts;
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
NULL, buffersink_params, filter_graph);
av_free(buffersink_params);
if (ret < 0) {
fprintf(stderr, "Cannot create buffer sink, error:%s\n", av_err2str(ret));
return ret;
}
/*
* Set the endpoints for the filter graph. The filter_graph will
* be linked to the graph described by filters_descr.
*/
/*
* The buffer source output must be connected to the input pad of
* the first filter described by filters_descr; since the first
* filter input label is not specified, it is set to "in" by
* default.
*/
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
/*
* The buffer sink input must be connected to the output pad of
* the last filter described by filters_descr; since the last
* filter output label is not specified, it is set to "out" by
* default.
*/
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx;
inputs->pad_idx = 0;
inputs->next = NULL;
if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_descr,
&inputs, &outputs, NULL)) < 0) {
fprintf(stderr, "avfilter_graph_parse_ptr failed, error:%s\n", av_err2str(ret));
return ret;
}
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) {
fprintf(stderr, "avfilter_graph_config failed, error:%s\n", av_err2str(ret));
return ret;
}
frame_in = av_frame_alloc();
frame_buffer_in = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, in_width,in_height,1));
av_image_fill_arrays(frame_in->data, frame_in->linesize, frame_buffer_in,
AV_PIX_FMT_YUV420P, in_width, in_height, 1);
frame_out = av_frame_alloc();
frame_buffer_out = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, in_width,in_height,1));
av_image_fill_arrays(frame_out->data, frame_out->linesize, frame_buffer_out,
AV_PIX_FMT_YUV420P, in_width, in_height, 1);
frame_in->width = in_width;
frame_in->height = in_height;
frame_in->format = AV_PIX_FMT_YUV420P;
while (1) {
if(fread(frame_buffer_in, 1, in_width*in_height*3/2, in_fp) != in_width * in_height * 3/2){
break;
}
//input Y,U,V
frame_in->data[0] = frame_buffer_in;
frame_in->data[1] = frame_buffer_in + in_width * in_height;
frame_in->data[2] = frame_buffer_in + in_width * in_height * 5/4;
if (av_buffersrc_add_frame(buffersrc_ctx, frame_in) < 0) {
printf( "Error while add frame.\n");
break;
}
/* pull filtered pictures from the filtergraph */
ret = av_buffersink_get_frame(buffersink_ctx, frame_out);
if (ret < 0)
break;
//output Y,U,V
if(frame_out->format == AV_PIX_FMT_YUV420P){
for(int i = 0; i < frame_out->height; i++){
fwrite(frame_out->data[0] + frame_out->linesize[0] * i, 1, frame_out->width, out_fp);
}
for(int i = 0; i < frame_out->height/2; i++){
fwrite(frame_out->data[1] + frame_out->linesize[1] * i, 1, frame_out->width / 2, out_fp);
}
for(int i = 0; i < frame_out->height/2; i++){
fwrite(frame_out->data[2] + frame_out->linesize[2] * i, 1, frame_out->width / 2, out_fp);
}
}
fprintf(stdout, "Process 1 frame!\n");
av_frame_unref(frame_out);
}
fclose(in_fp);
fclose(out_fp);
av_frame_free(&frame_in);
av_frame_free(&frame_out);
avfilter_graph_free(&filter_graph);
return 0;
}
以上是基于[2]中给出的代码进行的修改,说明:
- 调用avfilter_get_by_name("ffbuffersink")时在新版本的ffmpeg要修改为avfilter_get_by_name("buffersink"),否则返回指针为空,调用avfilter_graph_create_filter返回-12,在以前的版本则没有这个问题。
- filter_descr传入"boxblur"时,返回"Invalid argument"的错误,并打印“No such filter: 'boxblur'”的错误信息。
- filter_descr参数通过传入不同的值实现不同的特效。
lutyuv='u=128:v=128' //去色调
boxblur //模糊
hflip //水平翻转
crop=2/3*in_w:2/3*in_h //裁剪