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"
29 AVFilterGraph *avfilter_graph_alloc(void)
31 return av_mallocz(sizeof(AVFilterGraph));
34 void avfilter_graph_free(AVFilterGraph *graph)
36 for(; graph->filter_count > 0; graph->filter_count --)
37 avfilter_free(graph->filters[graph->filter_count - 1]);
38 av_freep(&graph->scale_sws_opts);
39 av_freep(&graph->filters);
42 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
44 AVFilterContext **filters = av_realloc(graph->filters,
45 sizeof(AVFilterContext*) * (graph->filter_count+1));
47 return AVERROR(ENOMEM);
49 graph->filters = filters;
50 graph->filters[graph->filter_count++] = filter;
55 int avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
57 AVFilterContext *filt;
60 for (i=0; i < graph->filter_count; i++) {
61 filt = graph->filters[i];
63 for (j = 0; j < filt->input_count; j++) {
64 if (!filt->inputs[j] || !filt->inputs[j]->src) {
65 av_log(log_ctx, AV_LOG_ERROR,
66 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
67 filt->input_pads[j].name, filt->name, filt->filter->name);
72 for (j = 0; j < filt->output_count; j++) {
73 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
74 av_log(log_ctx, AV_LOG_ERROR,
75 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
76 filt->output_pads[j].name, filt->name, filt->filter->name);
85 int avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
87 AVFilterContext *filt;
90 for (i=0; i < graph->filter_count; i++) {
91 filt = graph->filters[i];
93 if (!filt->output_count) {
94 if ((ret = avfilter_config_links(filt)))
102 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
106 for(i = 0; i < graph->filter_count; i ++)
107 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
108 return graph->filters[i];
113 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
116 int scaler_count = 0;
119 /* ask all the sub-filters for their supported media formats */
120 for(i = 0; i < graph->filter_count; i ++) {
121 if(graph->filters[i]->filter->query_formats)
122 graph->filters[i]->filter->query_formats(graph->filters[i]);
124 avfilter_default_query_formats(graph->filters[i]);
127 /* go through and merge as many format lists as possible */
128 for(i = 0; i < graph->filter_count; i ++) {
129 AVFilterContext *filter = graph->filters[i];
131 for(j = 0; j < filter->input_count; j ++) {
132 AVFilterLink *link = filter->inputs[j];
133 if(link && link->in_formats != link->out_formats) {
134 if(!avfilter_merge_formats(link->in_formats,
135 link->out_formats)) {
136 AVFilterContext *scale;
137 char scale_args[256];
138 /* couldn't merge format lists. auto-insert scale filter */
139 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
141 avfilter_open(&scale, avfilter_get_by_name("scale"), inst_name);
143 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
144 if(!scale || scale->filter->init(scale, scale_args, NULL) ||
145 avfilter_insert_filter(link, scale, 0, 0)) {
146 avfilter_free(scale);
150 if (avfilter_graph_add_filter(graph, scale) < 0)
153 scale->filter->query_formats(scale);
154 if (((link = scale-> inputs[0]) &&
155 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
156 ((link = scale->outputs[0]) &&
157 !avfilter_merge_formats(link->in_formats, link->out_formats))) {
158 av_log(log_ctx, AV_LOG_ERROR,
159 "Impossible to convert between the formats supported by the filter "
160 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
171 static void pick_format(AVFilterLink *link)
173 if(!link || !link->in_formats)
176 link->in_formats->format_count = 1;
177 link->format = link->in_formats->formats[0];
179 avfilter_formats_unref(&link->in_formats);
180 avfilter_formats_unref(&link->out_formats);
183 static void pick_formats(AVFilterGraph *graph)
187 for(i = 0; i < graph->filter_count; i ++) {
188 AVFilterContext *filter = graph->filters[i];
190 for(j = 0; j < filter->input_count; j ++)
191 pick_format(filter->inputs[j]);
192 for(j = 0; j < filter->output_count; j ++)
193 pick_format(filter->outputs[j]);
197 int avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
199 /* find supported formats from sub-filters, and merge along links */
200 if(query_formats(graph, log_ctx))
203 /* Once everything is merged, it's possible that we'll still have
204 * multiple valid media format choices. We pick the first one. */
210 int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
214 if ((ret = avfilter_graph_check_validity(graphctx, log_ctx)))
216 if ((ret = avfilter_graph_config_formats(graphctx, log_ctx)))
218 if ((ret = avfilter_graph_config_links(graphctx, log_ctx)))