ffmpeg---AVfilter滤波器模块如何初始化filter和如何使用?

本文介绍了FFmpeg中AVfilter滤波器模块的初始化步骤,包括调用`avfilter_register_all()`注册所有滤波器,`avfilter_graph_alloc()`分配FilterGraph内存,以及`avfilter_graph_create_filter()`和`avfilter_graph_parse_ptr()`创建并添加滤波器。同时,文章提供了使用滤波器的流程,如通过`av_buffersrc_add_frame()`添加输入帧,以及`av_buffersink_get_frame()`获取处理后的输出帧。此外,还提及了如何设置特效和初始化滤波器的详细过程。
摘要由CSDN通过智能技术生成

参考ffmpeg实例文档\doc\examples\ filtering_audio.c和filtering_video.c和 filter_audio.c
下面和雷霄骅–最简单的基于FFMPEG的转码程序均基于filtering_video.c

libavfilter的流程图、
在这里插入图片描述
1、初始化:
 avfilter_register_all():注册所有AVFilter。
 avfilter_graph_alloc():为FilterGraph分配内存。
 avfilter_graph_create_filter():创建并向FilterGraph中添加一个Filter。
 avfilter_graph_parse_ptr():将一串通过字符串描述的Graph添加到FilterGraph中。
 avfilter_graph_config():检查FilterGraph的配置。
2、使用:
 av_buffersrc_add_frame():向FilterGraph中加入AVFrame。
 av_buffersink_get_frame():从FilterGraph中取出(处理后的)AVFrame。

设置特效filters_descr 和准备结构体

#include "libavfilter/avfiltergraph.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/avutil.h"

//added by ws for AVfilter start
const char *filter_descr = "movie=my_logo.png[wm];[in][wm]overlay=5:5[out]";
		//需要叠加的水印为一张PNG(透明)图片(在这里是my_logo.png)。
//const char *filters_descr = "lutyuv='u=128:v=128'";
//const char *filters_descr = "hflip";
//const char *filters_descr = "hue='h=60:s=-3'";
//const char *filters_descr = "crop=2/3*in_w:2/3*in_h";
//const char *filters_descr = "drawbox=x=200:y=200:w=300:h=300:color=pink@0.5";
//const char *filters_descr = "movie=/storage/emulated/0/ws.jpg[wm];[in][wm]overlay=5:5[out]";
//const char *filters_descr="drawgrid=width=100:height=100:thickness=4:color=pink@0.9";

AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
//added by ws for AVfilter end

更多的特效使用,请到官网http://www.ffmpeg.org/ffmpeg-filters.html

初始化(不如直接抄filtering_video.c)

比如将下面的初始化过程单独写成一个函数:static int init_filters(const char *filters_descr)

 //init AVfilter
	avfilter_register_all();//初始化前要先注册
    char args[512];
    int ret;
    AVFilter *buffersrc  = avfilter_get_by_name("buffer");
    AVFilter *buffersink = avfilter_get_by_name("buffersink");//新版的ffmpeg库必须为buffersink
    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",
             pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
             pCodecCtx->time_base.num, pCodecCtx->time_base.den,
             pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);

    ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
                                       args, NULL, filter_graph);
    if (ret < 0) {
   LOGD("Cannot create buffer source\n");return ret;}

    /* buffer video sink: to terminate the filter chain. */
    buffersink_params = av_buffersink_params_alloc();
    buffersink_params
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值