ffmpeg 使用滤镜

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


一帧通过滤镜的函数:

[cpp]   view plain  copy
  1. int Filter_One_Frame(FilterArgs *filter_args,AVFrame *frame, AVFrame *filt_frame,const char * filter_descr)  
  2. {  
  3.     int ret;  
  4.     avcodec_register_all();  
  5.     av_register_all();  
  6.     avfilter_register_all();//注册,假如没有调用会报出不能创建滤镜链  
  7.   
  8.     if (filter_args == NULL || filter_descr == NULL ||frame == NULL ||filt_frame == NULL )  
  9.     {      
  10.         printf("%s\nLine %d:%s : input_mp4 == NULL or filter_descr == NULL\n", __FILE__, __LINE__,__func__);  
  11.          goto end;  
  12.     }  
  13.     if ((ret = init_filters(filter_descr,filter_args)) < 0)//初始化滤镜链  
  14.         goto end;  
  15.   
  16.         frame->pts = av_frame_get_best_effort_timestamp(frame);          
  17.         /* push the decoded frame into the filtergraph */  
  18.         if (av_buffersrc_add_frame(buffersrc_ctx, frame) < 0)  
  19.     {  
  20.                 av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");  
  21.             goto end;  
  22.     }  
  23.         /* pull filtered pictures from the filtergraph */  
  24.      while (1)  
  25.     {  
  26.                 ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);//过滤镜  
  27.                 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)  
  28.                     break;  
  29.                 if (ret < 0)  
  30.                     goto end;          
  31.        }  
  32.      ret = 0;  
  33. end:  
  34.   
  35.     if (filter_graph != NULL)  
  36.         avfilter_graph_free(&filter_graph);//释放  
  37.     if (ret < 0 && ret != AVERROR_EOF) {  
  38.         char buf[1024];  
  39.         av_strerror(ret, buf, sizeof(buf));  
  40.         fprintf(stderr, "Error occurred: %s\n", buf);  
  41.         return -1;  
  42.     }  
  43.         return 0;  
  44. }  

FilterArgs 一个结构体:

[cpp]   view plain  copy
  1. typedef struct Filter_Args  
  2. {  
  3.     int width;  
  4.     int height;  
  5.      enum AVPixelFormat pix_fmt;  
  6.     AVRational time_base;  
  7.     AVRational sample_aspect_ratio;  
  8. }FilterArgs;  

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

函数init_filters:

[cpp]   view plain  copy
  1. int init_filters(const char *filters_descr,FilterArgs *filter_args)  
  2.   
  3. {  
  4.   
  5.     char args[512];  
  6.   
  7.     int ret = 0;  
  8.   
  9.     AVFilter *buffersrc  = avfilter_get_by_name("buffer");  
  10.     AVFilter *buffersink = avfilter_get_by_name("buffersink");  
  11.     AVFilterInOut *outputs = avfilter_inout_alloc();  
  12.     AVFilterInOut *inputs  = avfilter_inout_alloc();  
  13.   
  14.     enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };//modify by elesos.com ?OEAE????oe??????  
  15.     if (filters_descr == NULL )  
  16.     {     
  17.         printf("%s\nLine %d:%s : filter_args == NULL or filter_descr == NULL\n", __FILE__, __LINE__,__func__);  
  18.          goto end;  
  19.     }  
  20.     filter_graph = avfilter_graph_alloc();  
  21.     if (!outputs || !inputs || !filter_graph )  
  22.     {  
  23.         ret = AVERROR(ENOMEM);  
  24.         goto end;  
  25.     }  
  26.     /* buffer video source: the decoded frames from the decoder will be inserted here. */  
  27.     if (filter_args)  
  28.     {  
  29.           snprintf(args, sizeof(args),  
  30.                     "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",  
  31.                     filter_args->width, filter_args->height, filter_args->pix_fmt,  
  32.                     filter_args->time_base.num, filter_args->time_base.den,  
  33.                     filter_args->sample_aspect_ratio.num, filter_args->sample_aspect_ratio.den);  
  34.     }  
  35.     else  
  36.     {  
  37.           snprintf(args, sizeof(args),  
  38.                     "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",  
  39.                     pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,  
  40.                     pCodecCtx->time_base.num, pCodecCtx->time_base.den,  
  41.                     pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);  
  42.     }  
  43.     puts(args);  
  44.     ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",  
  45.   
  46.                                        args, NULL, filter_graph);  
  47.     if (ret < 0) {  
  48.   
  49.         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");  
  50.   
  51.         goto end;  
  52.   
  53.     }  
  54.      /* buffer video sink: to terminate the filter chain. */  
  55.     ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",  
  56.                                      NULL, NULL, filter_graph);  
  57.     if (ret < 0) {  
  58.         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");  
  59.         goto end;  
  60.   
  61.     }  
  62.     ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);  
  63.     if (ret < 0) {  
  64.         av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");  
  65.         goto end;  
  66.     }  
  67.   
  68.     /* Endpoints for the filter graph. */  
  69.     outputs->name       = av_strdup("in");  
  70.     outputs->filter_ctx = buffersrc_ctx;  
  71.     outputs->pad_idx    = 0;  
  72.     outputs->next       = NULL;   
  73.   
  74.     inputs->name       = av_strdup("out");  
  75.     inputs->filter_ctx = buffersink_ctx;  
  76.     inputs->pad_idx    = 0;  
  77.     inputs->next       = NULL;  
  78.   
  79.     if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,&inputs, &outputs, NULL)) < 0)  
  80.               goto end;  
  81.   
  82.     if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)  
  83.   
  84.         goto end;  
  85.     end:  
  86.     avfilter_inout_free(&inputs);  
  87.     avfilter_inout_free(&outputs);  
  88.     return ret;  
  89.   
  90. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值