3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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);
46 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
48 AVFilterContext **filters = av_realloc(graph->filters,
49 sizeof(AVFilterContext*) * (graph->filter_count+1));
51 return AVERROR(ENOMEM);
53 graph->filters = filters;
54 graph->filters[graph->filter_count++] = filter;
59 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
60 const char *name, const char *args, void *opaque,
61 AVFilterGraph *graph_ctx)
65 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
67 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
69 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
75 avfilter_free(*filt_ctx);
80 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
82 AVFilterContext *filt;
85 for (i = 0; i < graph->filter_count; i++) {
86 filt = graph->filters[i];
88 for (j = 0; j < filt->input_count; j++) {
89 if (!filt->inputs[j] || !filt->inputs[j]->src) {
90 av_log(log_ctx, AV_LOG_ERROR,
91 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
92 filt->input_pads[j].name, filt->name, filt->filter->name);
93 return AVERROR(EINVAL);
97 for (j = 0; j < filt->output_count; j++) {
98 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
99 av_log(log_ctx, AV_LOG_ERROR,
100 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
101 filt->output_pads[j].name, filt->name, filt->filter->name);
102 return AVERROR(EINVAL);
110 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
112 AVFilterContext *filt;
115 for (i=0; i < graph->filter_count; i++) {
116 filt = graph->filters[i];
118 if (!filt->output_count) {
119 if ((ret = avfilter_config_links(filt)))
127 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
131 for (i = 0; i < graph->filter_count; i++)
132 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
133 return graph->filters[i];
138 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
141 int scaler_count = 0;
144 /* ask all the sub-filters for their supported media formats */
145 for (i = 0; i < graph->filter_count; i++) {
146 if (graph->filters[i]->filter->query_formats)
147 graph->filters[i]->filter->query_formats(graph->filters[i]);
149 avfilter_default_query_formats(graph->filters[i]);
152 /* go through and merge as many format lists as possible */
153 for (i = 0; i < graph->filter_count; i++) {
154 AVFilterContext *filter = graph->filters[i];
156 for (j = 0; j < filter->input_count; j++) {
157 AVFilterLink *link = filter->inputs[j];
158 if (link && link->in_formats != link->out_formats) {
159 if (!avfilter_merge_formats(link->in_formats,
160 link->out_formats)) {
161 AVFilterContext *scale;
162 char scale_args[256];
163 /* couldn't merge format lists. auto-insert scale filter */
164 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
166 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
167 if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
168 inst_name, scale_args, NULL, graph)) < 0)
170 if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
173 scale->filter->query_formats(scale);
174 if (((link = scale-> inputs[0]) &&
175 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
176 ((link = scale->outputs[0]) &&
177 !avfilter_merge_formats(link->in_formats, link->out_formats))) {
178 av_log(log_ctx, AV_LOG_ERROR,
179 "Impossible to convert between the formats supported by the filter "
180 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
181 return AVERROR(EINVAL);
191 static void pick_format(AVFilterLink *link)
193 if (!link || !link->in_formats)
196 link->in_formats->format_count = 1;
197 link->format = link->in_formats->formats[0];
199 avfilter_formats_unref(&link->in_formats);
200 avfilter_formats_unref(&link->out_formats);
203 static void pick_formats(AVFilterGraph *graph)
207 for (i = 0; i < graph->filter_count; i++) {
208 AVFilterContext *filter = graph->filters[i];
210 for (j = 0; j < filter->input_count; j++)
211 pick_format(filter->inputs[j]);
212 for (j = 0; j < filter->output_count; j++)
213 pick_format(filter->outputs[j]);
217 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
221 /* find supported formats from sub-filters, and merge along links */
222 if ((ret = query_formats(graph, log_ctx)) < 0)
225 /* Once everything is merged, it's possible that we'll still have
226 * multiple valid media format choices. We pick the first one. */
232 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
236 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
238 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
240 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))