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