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 #include "libavutil/log.h"
32 static const AVClass filtergraph_class = {
33 .class_name = "AVFilterGraph",
34 .item_name = av_default_item_name,
35 .version = LIBAVUTIL_VERSION_INT,
38 AVFilterGraph *avfilter_graph_alloc(void)
40 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
43 #if FF_API_GRAPH_AVCLASS
44 ret->av_class = &filtergraph_class;
49 void avfilter_graph_free(AVFilterGraph **graph)
53 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
54 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
55 av_freep(&(*graph)->scale_sws_opts);
56 av_freep(&(*graph)->filters);
60 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
62 AVFilterContext **filters = av_realloc(graph->filters,
63 sizeof(AVFilterContext*) * (graph->filter_count+1));
65 return AVERROR(ENOMEM);
67 graph->filters = filters;
68 graph->filters[graph->filter_count++] = filter;
73 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
74 const char *name, const char *args, void *opaque,
75 AVFilterGraph *graph_ctx)
79 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
81 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
83 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
89 avfilter_free(*filt_ctx);
94 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
96 AVFilterContext *filt;
99 for (i = 0; i < graph->filter_count; i++) {
100 filt = graph->filters[i];
102 for (j = 0; j < filt->input_count; j++) {
103 if (!filt->inputs[j] || !filt->inputs[j]->src) {
104 av_log(log_ctx, AV_LOG_ERROR,
105 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
106 filt->input_pads[j].name, filt->name, filt->filter->name);
107 return AVERROR(EINVAL);
111 for (j = 0; j < filt->output_count; j++) {
112 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
113 av_log(log_ctx, AV_LOG_ERROR,
114 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
115 filt->output_pads[j].name, filt->name, filt->filter->name);
116 return AVERROR(EINVAL);
124 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
126 AVFilterContext *filt;
129 for (i=0; i < graph->filter_count; i++) {
130 filt = graph->filters[i];
132 if (!filt->output_count) {
133 if ((ret = avfilter_config_links(filt)))
141 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
145 for (i = 0; i < graph->filter_count; i++)
146 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
147 return graph->filters[i];
152 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
155 int scaler_count = 0;
158 /* ask all the sub-filters for their supported media formats */
159 for (i = 0; i < graph->filter_count; i++) {
160 if (graph->filters[i]->filter->query_formats)
161 graph->filters[i]->filter->query_formats(graph->filters[i]);
163 avfilter_default_query_formats(graph->filters[i]);
166 /* go through and merge as many format lists as possible */
167 for (i = 0; i < graph->filter_count; i++) {
168 AVFilterContext *filter = graph->filters[i];
170 for (j = 0; j < filter->input_count; j++) {
171 AVFilterLink *link = filter->inputs[j];
172 if (link && link->in_formats != link->out_formats) {
173 if (!avfilter_merge_formats(link->in_formats,
174 link->out_formats)) {
175 AVFilterContext *scale;
176 char scale_args[256];
177 /* couldn't merge format lists. auto-insert scale filter */
178 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
180 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
181 if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
182 inst_name, scale_args, NULL, graph)) < 0)
184 if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
187 scale->filter->query_formats(scale);
188 if (((link = scale-> inputs[0]) &&
189 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
190 ((link = scale->outputs[0]) &&
191 !avfilter_merge_formats(link->in_formats, link->out_formats))) {
192 av_log(log_ctx, AV_LOG_ERROR,
193 "Impossible to convert between the formats supported by the filter "
194 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
195 return AVERROR(EINVAL);
205 static void pick_format(AVFilterLink *link)
207 if (!link || !link->in_formats)
210 link->in_formats->format_count = 1;
211 link->format = link->in_formats->formats[0];
213 avfilter_formats_unref(&link->in_formats);
214 avfilter_formats_unref(&link->out_formats);
217 static int reduce_formats_on_filter(AVFilterContext *filter)
219 int i, j, k, ret = 0;
221 for (i = 0; i < filter->input_count; i++) {
222 AVFilterLink *link = filter->inputs[i];
223 int format = link->out_formats->formats[0];
225 if (link->out_formats->format_count != 1)
228 for (j = 0; j < filter->output_count; j++) {
229 AVFilterLink *out_link = filter->outputs[j];
230 AVFilterFormats *fmts = out_link->in_formats;
232 if (link->type != out_link->type ||
233 out_link->in_formats->format_count == 1)
236 for (k = 0; k < out_link->in_formats->format_count; k++)
237 if (fmts->formats[k] == format) {
238 fmts->formats[0] = format;
239 fmts->format_count = 1;
248 static void reduce_formats(AVFilterGraph *graph)
255 for (i = 0; i < graph->filter_count; i++)
256 reduced |= reduce_formats_on_filter(graph->filters[i]);
260 static void pick_formats(AVFilterGraph *graph)
264 for (i = 0; i < graph->filter_count; i++) {
265 AVFilterContext *filter = graph->filters[i];
267 for (j = 0; j < filter->input_count; j++)
268 pick_format(filter->inputs[j]);
269 for (j = 0; j < filter->output_count; j++)
270 pick_format(filter->outputs[j]);
274 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
278 /* find supported formats from sub-filters, and merge along links */
279 if ((ret = query_formats(graph, log_ctx)) < 0)
282 /* Once everything is merged, it's possible that we'll still have
283 * multiple valid media format choices. We try to minimize the amount
284 * of format conversion inside filters */
285 reduce_formats(graph);
292 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
296 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
298 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
300 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))