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, resampler_count = 0;
157 /* ask all the sub-filters for their supported media formats */
158 for (i = 0; i < graph->filter_count; i++) {
159 if (graph->filters[i]->filter->query_formats)
160 graph->filters[i]->filter->query_formats(graph->filters[i]);
162 avfilter_default_query_formats(graph->filters[i]);
165 /* go through and merge as many format lists as possible */
166 for (i = 0; i < graph->filter_count; i++) {
167 AVFilterContext *filter = graph->filters[i];
169 for (j = 0; j < filter->input_count; j++) {
170 AVFilterLink *link = filter->inputs[j];
171 if (link && link->in_formats != link->out_formats) {
172 if (!avfilter_merge_formats(link->in_formats,
173 link->out_formats)) {
174 AVFilterContext *convert;
176 AVFilterLink *inlink, *outlink;
177 char scale_args[256];
180 /* couldn't merge format lists. auto-insert conversion filter */
181 switch (link->type) {
182 case AVMEDIA_TYPE_VIDEO:
183 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
185 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
186 if ((ret = avfilter_graph_create_filter(&convert,
187 avfilter_get_by_name("scale"),
188 inst_name, scale_args, NULL,
192 case AVMEDIA_TYPE_AUDIO:
193 if (!(filter = avfilter_get_by_name("resample"))) {
194 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
195 "not present, cannot convert audio formats.\n");
196 return AVERROR(EINVAL);
199 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
201 if ((ret = avfilter_graph_create_filter(&convert,
202 avfilter_get_by_name("resample"),
203 inst_name, NULL, NULL, graph)) < 0)
207 return AVERROR(EINVAL);
210 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
213 convert->filter->query_formats(convert);
214 inlink = convert->inputs[0];
215 outlink = convert->outputs[0];
216 if (!avfilter_merge_formats( inlink->in_formats, inlink->out_formats) ||
217 !avfilter_merge_formats(outlink->in_formats, outlink->out_formats)) {
218 av_log(log_ctx, AV_LOG_ERROR,
219 "Impossible to convert between the formats supported by the filter "
220 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
221 return AVERROR(EINVAL);
231 static void pick_format(AVFilterLink *link)
233 if (!link || !link->in_formats)
236 link->in_formats->format_count = 1;
237 link->format = link->in_formats->formats[0];
239 avfilter_formats_unref(&link->in_formats);
240 avfilter_formats_unref(&link->out_formats);
243 static int reduce_formats_on_filter(AVFilterContext *filter)
245 int i, j, k, ret = 0;
247 for (i = 0; i < filter->input_count; i++) {
248 AVFilterLink *link = filter->inputs[i];
249 int format = link->out_formats->formats[0];
251 if (link->out_formats->format_count != 1)
254 for (j = 0; j < filter->output_count; j++) {
255 AVFilterLink *out_link = filter->outputs[j];
256 AVFilterFormats *fmts = out_link->in_formats;
258 if (link->type != out_link->type ||
259 out_link->in_formats->format_count == 1)
262 for (k = 0; k < out_link->in_formats->format_count; k++)
263 if (fmts->formats[k] == format) {
264 fmts->formats[0] = format;
265 fmts->format_count = 1;
274 static void reduce_formats(AVFilterGraph *graph)
281 for (i = 0; i < graph->filter_count; i++)
282 reduced |= reduce_formats_on_filter(graph->filters[i]);
286 static void pick_formats(AVFilterGraph *graph)
290 for (i = 0; i < graph->filter_count; i++) {
291 AVFilterContext *filter = graph->filters[i];
293 for (j = 0; j < filter->input_count; j++)
294 pick_format(filter->inputs[j]);
295 for (j = 0; j < filter->output_count; j++)
296 pick_format(filter->outputs[j]);
300 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
304 /* find supported formats from sub-filters, and merge along links */
305 if ((ret = query_formats(graph, log_ctx)) < 0)
308 /* Once everything is merged, it's possible that we'll still have
309 * multiple valid media format choices. We try to minimize the amount
310 * of format conversion inside filters */
311 reduce_formats(graph);
318 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
322 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
324 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
326 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))