3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "avfiltergraph.h"
30 AVFilterGraph *avfilter_graph_alloc(void)
32 return av_mallocz(sizeof(AVFilterGraph));
35 void avfilter_graph_free(AVFilterGraph *graph)
39 for (; graph->filter_count > 0; graph->filter_count --)
40 avfilter_free(graph->filters[graph->filter_count - 1]);
41 av_freep(&graph->scale_sws_opts);
42 av_freep(&graph->filters);
45 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
47 AVFilterContext **filters = av_realloc(graph->filters,
48 sizeof(AVFilterContext*) * (graph->filter_count+1));
50 return AVERROR(ENOMEM);
52 graph->filters = filters;
53 graph->filters[graph->filter_count++] = filter;
58 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
59 const char *name, const char *args, void *opaque,
60 AVFilterGraph *graph_ctx)
64 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
66 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
68 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
74 avfilter_free(*filt_ctx);
79 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
81 AVFilterContext *filt;
84 for (i = 0; i < graph->filter_count; i++) {
85 filt = graph->filters[i];
87 for (j = 0; j < filt->input_count; j++) {
88 if (!filt->inputs[j] || !filt->inputs[j]->src) {
89 av_log(log_ctx, AV_LOG_ERROR,
90 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
91 filt->input_pads[j].name, filt->name, filt->filter->name);
96 for (j = 0; j < filt->output_count; j++) {
97 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
98 av_log(log_ctx, AV_LOG_ERROR,
99 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
100 filt->output_pads[j].name, filt->name, filt->filter->name);
109 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
111 AVFilterContext *filt;
114 for (i=0; i < graph->filter_count; i++) {
115 filt = graph->filters[i];
117 if (!filt->output_count) {
118 if ((ret = avfilter_config_links(filt)))
126 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
130 for (i = 0; i < graph->filter_count; i++)
131 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
132 return graph->filters[i];
137 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
140 int scaler_count = 0;
143 /* ask all the sub-filters for their supported media formats */
144 for (i = 0; i < graph->filter_count; i++) {
145 if (graph->filters[i]->filter->query_formats)
146 graph->filters[i]->filter->query_formats(graph->filters[i]);
148 avfilter_default_query_formats(graph->filters[i]);
151 /* go through and merge as many format lists as possible */
152 for (i = 0; i < graph->filter_count; i++) {
153 AVFilterContext *filter = graph->filters[i];
155 for (j = 0; j < filter->input_count; j++) {
156 AVFilterLink *link = filter->inputs[j];
157 if (link && link->in_formats != link->out_formats) {
158 if (!avfilter_merge_formats(link->in_formats,
159 link->out_formats)) {
160 AVFilterContext *scale;
161 char scale_args[256];
162 /* couldn't merge format lists. auto-insert scale filter */
163 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
165 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
166 if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
167 inst_name, scale_args, NULL, graph)) < 0)
169 if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
172 scale->filter->query_formats(scale);
173 if (((link = scale-> inputs[0]) &&
174 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
175 ((link = scale->outputs[0]) &&
176 !avfilter_merge_formats(link->in_formats, link->out_formats))) {
177 av_log(log_ctx, AV_LOG_ERROR,
178 "Impossible to convert between the formats supported by the filter "
179 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
190 static void pick_format(AVFilterLink *link)
192 if (!link || !link->in_formats)
195 link->in_formats->format_count = 1;
196 link->format = link->in_formats->formats[0];
198 avfilter_formats_unref(&link->in_formats);
199 avfilter_formats_unref(&link->out_formats);
202 static void pick_formats(AVFilterGraph *graph)
206 for (i = 0; i < graph->filter_count; i++) {
207 AVFilterContext *filter = graph->filters[i];
209 for (j = 0; j < filter->input_count; j++)
210 pick_format(filter->inputs[j]);
211 for (j = 0; j < filter->output_count; j++)
212 pick_format(filter->outputs[j]);
216 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
218 /* find supported formats from sub-filters, and merge along links */
219 if (query_formats(graph, log_ctx))
222 /* Once everything is merged, it's possible that we'll still have
223 * multiple valid media format choices. We pick the first one. */
229 int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
233 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
235 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
237 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))