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)
37 for(; graph->filter_count > 0; graph->filter_count --)
38 avfilter_free(graph->filters[graph->filter_count - 1]);
39 av_freep(&graph->scale_sws_opts);
40 av_freep(&graph->filters);
43 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
45 AVFilterContext **filters = av_realloc(graph->filters,
46 sizeof(AVFilterContext*) * (graph->filter_count+1));
48 return AVERROR(ENOMEM);
50 graph->filters = filters;
51 graph->filters[graph->filter_count++] = filter;
56 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
58 AVFilterContext *filt;
61 for (i=0; i < graph->filter_count; i++) {
62 filt = graph->filters[i];
64 for (j = 0; j < filt->input_count; j++) {
65 if (!filt->inputs[j] || !filt->inputs[j]->src) {
66 av_log(log_ctx, AV_LOG_ERROR,
67 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
68 filt->input_pads[j].name, filt->name, filt->filter->name);
73 for (j = 0; j < filt->output_count; j++) {
74 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
75 av_log(log_ctx, AV_LOG_ERROR,
76 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
77 filt->output_pads[j].name, filt->name, filt->filter->name);
86 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
88 AVFilterContext *filt;
91 for (i=0; i < graph->filter_count; i++) {
92 filt = graph->filters[i];
94 if (!filt->output_count) {
95 if ((ret = avfilter_config_links(filt)))
103 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
107 for(i = 0; i < graph->filter_count; i ++)
108 if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
109 return graph->filters[i];
114 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
117 int scaler_count = 0;
120 /* ask all the sub-filters for their supported media formats */
121 for(i = 0; i < graph->filter_count; i ++) {
122 if(graph->filters[i]->filter->query_formats)
123 graph->filters[i]->filter->query_formats(graph->filters[i]);
125 avfilter_default_query_formats(graph->filters[i]);
128 /* go through and merge as many format lists as possible */
129 for(i = 0; i < graph->filter_count; i ++) {
130 AVFilterContext *filter = graph->filters[i];
132 for(j = 0; j < filter->input_count; j ++) {
133 AVFilterLink *link = filter->inputs[j];
134 if(link && link->in_formats != link->out_formats) {
135 if(!avfilter_merge_formats(link->in_formats,
136 link->out_formats)) {
137 AVFilterContext *scale;
138 char scale_args[256];
139 /* couldn't merge format lists. auto-insert scale filter */
140 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
142 avfilter_open(&scale, avfilter_get_by_name("scale"), inst_name);
144 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
145 if(!scale || scale->filter->init(scale, scale_args, NULL) ||
146 avfilter_insert_filter(link, scale, 0, 0)) {
147 avfilter_free(scale);
151 if (avfilter_graph_add_filter(graph, scale) < 0)
154 scale->filter->query_formats(scale);
155 if (((link = scale-> inputs[0]) &&
156 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
157 ((link = scale->outputs[0]) &&
158 !avfilter_merge_formats(link->in_formats, link->out_formats))) {
159 av_log(log_ctx, AV_LOG_ERROR,
160 "Impossible to convert between the formats supported by the filter "
161 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
172 static void pick_format(AVFilterLink *link)
174 if(!link || !link->in_formats)
177 link->in_formats->format_count = 1;
178 link->format = link->in_formats->formats[0];
180 avfilter_formats_unref(&link->in_formats);
181 avfilter_formats_unref(&link->out_formats);
184 static void pick_formats(AVFilterGraph *graph)
188 for(i = 0; i < graph->filter_count; i ++) {
189 AVFilterContext *filter = graph->filters[i];
191 for(j = 0; j < filter->input_count; j ++)
192 pick_format(filter->inputs[j]);
193 for(j = 0; j < filter->output_count; j ++)
194 pick_format(filter->outputs[j]);
198 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
200 /* find supported formats from sub-filters, and merge along links */
201 if(query_formats(graph, log_ctx))
204 /* Once everything is merged, it's possible that we'll still have
205 * multiple valid media format choices. We pick the first one. */
211 int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
215 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
217 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
219 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))