ffmpeg 使用滤镜

来源:互联网 发布:win7软件停止运行 编辑:程序博客网 时间:2024/04/28 13:32

ffmpeg 中滤镜的实现其实并不高明,但是功能强大,先给出过滤镜的代码,再翻译我认为有的玩的滤镜。


一帧通过滤镜的函数:

int Filter_One_Frame(FilterArgs *filter_args,AVFrame *frame, AVFrame *filt_frame,const char * filter_descr){    int ret;    avcodec_register_all();    av_register_all();    avfilter_register_all();//注册,假如没有调用会报出不能创建滤镜链    if (filter_args == NULL || filter_descr == NULL ||frame == NULL ||filt_frame == NULL )    {            printf("%s\nLine %d:%s : input_mp4 == NULL or filter_descr == NULL\n", __FILE__, __LINE__,__func__);         goto end;    }    if ((ret = init_filters(filter_descr,filter_args)) < 0)//初始化滤镜链        goto end;        frame->pts = av_frame_get_best_effort_timestamp(frame);                /* push the decoded frame into the filtergraph */        if (av_buffersrc_add_frame(buffersrc_ctx, frame) < 0)    {                av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");            goto end;    }        /* pull filtered pictures from the filtergraph */     while (1)    {                ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);//过滤镜                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)                    break;                if (ret < 0)                    goto end;               }     ret = 0;end:    if (filter_graph != NULL)        avfilter_graph_free(&filter_graph);//释放    if (ret < 0 && ret != AVERROR_EOF) {        char buf[1024];        av_strerror(ret, buf, sizeof(buf));        fprintf(stderr, "Error occurred: %s\n", buf);        return -1;    }        return 0;}

FilterArgs 一个结构体:

typedef struct Filter_Args{    int width;    int height;     enum AVPixelFormat pix_fmt;    AVRational time_base;    AVRational sample_aspect_ratio;}FilterArgs;

这些参数在创建滤镜得时候比较重要,假如和过滤镜得帧不符和,或者参数不合法会导致通过滤镜失败。假如帧的yuv不是从视频中解码出来,也就是没有time_base 和sample_aspect_ratio,那么我一般会设置成{1,20};

函数init_filters:

int init_filters(const char *filters_descr,FilterArgs *filter_args){char args[512];int ret = 0;AVFilter *buffersrc  = avfilter_get_by_name("buffer");AVFilter *buffersink = avfilter_get_by_name("buffersink");AVFilterInOut *outputs = avfilter_inout_alloc();AVFilterInOut *inputs  = avfilter_inout_alloc();enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };//modify by elesos.com ?OEAE????oe??????if (filters_descr == NULL ){printf("%s\nLine %d:%s : filter_args == NULL or filter_descr == NULL\n", __FILE__, __LINE__,__func__); goto end;}filter_graph = avfilter_graph_alloc();if (!outputs || !inputs || !filter_graph ){    ret = AVERROR(ENOMEM);    goto end;}    /* buffer video source: the decoded frames from the decoder will be inserted here. */if (filter_args){  snprintf(args, sizeof(args),            "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",            filter_args->width, filter_args->height, filter_args->pix_fmt,            filter_args->time_base.num, filter_args->time_base.den,            filter_args->sample_aspect_ratio.num, filter_args->sample_aspect_ratio.den);}else{  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);}puts(args);ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",                                   args, NULL, filter_graph);if (ret < 0) {    av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");    goto end;} /* buffer video sink: to terminate the filter chain. */ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",                                 NULL, NULL, filter_graph);if (ret < 0) {    av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");    goto end;}ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);if (ret < 0) {    av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");    goto end;}/* Endpoints for the filter graph. */outputs->name       = av_strdup("in");outputs->filter_ctx = buffersrc_ctx;outputs->pad_idx    = 0;outputs->next       = NULL; 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, filters_descr,&inputs, &outputs, NULL)) < 0)    goto end;if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)    goto end;end:avfilter_inout_free(&inputs);avfilter_inout_free(&outputs);return ret;}

下篇给出我尝试过的滤镜及其效果:http://blog.csdn.net/dangxw_/article/details/49001413


0 0
原创粉丝点击