int ffmpeg_demo1::init_filters(const char *filters_descr, AVFormatContext *fmt_ctx, AVCodecContext *dec_ctx)
{
char args[512];
int ret = 0;
const AVFilter *buffersrc[2];
buffersrc[0]= avfilter_get_by_name("buffer");
buffersrc[1]= avfilter_get_by_name("buffer");
const AVFilter *buffersink = avfilter_get_by_name("buffersink");
AVFilterInOut *outputs[2];
outputs[0]= avfilter_inout_alloc();
outputs[1]= avfilter_inout_alloc();
AVFilterInOut *inputs = avfilter_inout_alloc();
AVRational time_base = fmt_ctx->streams[0]->time_base;
enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_NV12, AV_PIX_FMT_NONE };
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. */
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
time_base.num, time_base.den,
dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
qDebug("video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
time_base.num, time_base.den,
dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
ret = avfilter_graph_create_filter(&buffersrc_ctx[0], buffersrc[0], "in0",
args, NULL, filter_graph);
qDebug("avfilter_graph_create_filter0:%d", ret);
ret = avfilter_graph_create_filter(&buffersrc_ctx[1], buffersrc[1], "in1",
args, NULL, filter_graph);
qDebug("avfilter_graph_create_filter1:%d", ret);
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);
qDebug("avfilter_graph_create_filterout:%d", ret);
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;
}
qDebug("av_opt_set_int_list:%d", ret);
/*
* Set the endpoints for the filter graph. The filter_graph will
* be linked to the graph described by filters_descr.
*/
/*
* The buffer source output must be connected to the input pad of
* the first filter described by filters_descr; since the first
* filter input label is not specified, it is set to "in" by
* default.
*/
outputs[0]->name = av_strdup("in0");
outputs[0]->filter_ctx = buffersrc_ctx[0];
outputs[0]->pad_idx = 0;
outputs[0]->next = outputs[1];
outputs[1]->name = av_strdup("in1");
outputs[1]->filter_ctx = buffersrc_ctx[1];
outputs[1]->pad_idx = 0;
outputs[1]->next = NULL;
/*
* The buffer sink input must be connected to the output pad of
* the last filter described by filters_descr; since the last
* filter output label is not specified, it is set to "out" by
* default.
*/
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)
{
qDebug("avfilter_graph_parse_ptr:%d", ret);
goto end;
}
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
{
qDebug("avfilter_graph_config:%d", ret);
goto end;
}
end:
av_make_error_string(errbuf,64,ret);
qDebug("av_make_error_string:%s", errbuf);
if (!errbuf)
{
free(errbuf);
}
avfilter_inout_free(&inputs);
avfilter_inout_free(outputs);
return ret;
}
08-28
08-28