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. }  

發佈了126 篇原創文章 · 獲贊 61 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章