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
26 #include "libavutil/audioconvert.h"
28 #include "avfiltergraph.h"
31 #include "libavutil/log.h"
33 static const AVClass filtergraph_class = {
34 .class_name = "AVFilterGraph",
35 .item_name = av_default_item_name,
36 .version = LIBAVUTIL_VERSION_INT,
39 AVFilterGraph *avfilter_graph_alloc(void)
41 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
44 #if FF_API_GRAPH_AVCLASS
45 ret->av_class = &filtergraph_class;
50 void avfilter_graph_free(AVFilterGraph **graph)
54 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
55 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
56 av_freep(&(*graph)->scale_sws_opts);
57 av_freep(&(*graph)->filters);
61 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
63 AVFilterContext **filters = av_realloc(graph->filters,
64 sizeof(AVFilterContext*) * (graph->filter_count+1));
66 return AVERROR(ENOMEM);
68 graph->filters = filters;
69 graph->filters[graph->filter_count++] = filter;
74 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
75 const char *name, const char *args, void *opaque,
76 AVFilterGraph *graph_ctx)
80 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
82 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
84 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
90 avfilter_free(*filt_ctx);
95 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
97 AVFilterContext *filt;
100 for (i = 0; i < graph->filter_count; i++) {
101 filt = graph->filters[i];
103 for (j = 0; j < filt->input_count; j++) {
104 if (!filt->inputs[j] || !filt->inputs[j]->src) {
105 av_log(log_ctx, AV_LOG_ERROR,
106 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
107 filt->input_pads[j].name, filt->name, filt->filter->name);
108 return AVERROR(EINVAL);
112 for (j = 0; j < filt->output_count; j++) {
113 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
114 av_log(log_ctx, AV_LOG_ERROR,
115 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
116 filt->output_pads[j].name, filt->name, filt->filter->name);
117 return AVERROR(EINVAL);
125 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
127 AVFilterContext *filt;
130 for (i=0; i < graph->filter_count; i++) {
131 filt = graph->filters[i];
133 if (!filt->output_count) {
134 if ((ret = avfilter_config_links(filt)))
142 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
146 for (i = 0; i < graph->filter_count; i++)
147 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
148 return graph->filters[i];
153 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
154 const char *filt_name, const char *filt_args)
156 static int auto_count = 0, ret;
158 AVFilterContext *filt_ctx;
160 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
161 filt_name, auto_count++);
163 if ((ret = avfilter_graph_create_filter(&filt_ctx,
164 avfilter_get_by_name(filt_name),
165 inst_name, filt_args, NULL, graph)) < 0)
167 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
170 filt_ctx->filter->query_formats(filt_ctx);
172 if ( ((link = filt_ctx-> inputs[0]) &&
173 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
174 ((link = filt_ctx->outputs[0]) &&
175 !avfilter_merge_formats(link->in_formats, link->out_formats))
177 av_log(NULL, AV_LOG_ERROR,
178 "Impossible to convert between the formats supported by the filter "
179 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
180 return AVERROR(EINVAL);
183 if (link->type == AVMEDIA_TYPE_AUDIO &&
184 (((link = filt_ctx-> inputs[0]) &&
185 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
186 !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
187 ((link = filt_ctx->outputs[0]) &&
188 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
189 !avfilter_merge_formats(link->in_packing, link->out_packing))))
191 av_log(NULL, AV_LOG_ERROR,
192 "Impossible to convert between the channel layouts/packing formats supported by the filter "
193 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
194 return AVERROR(EINVAL);
200 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
204 AVFilterFormats *formats, *chlayouts, *packing;
206 /* ask all the sub-filters for their supported media formats */
207 for (i = 0; i < graph->filter_count; i++) {
208 if (graph->filters[i]->filter->query_formats)
209 graph->filters[i]->filter->query_formats(graph->filters[i]);
211 avfilter_default_query_formats(graph->filters[i]);
214 /* go through and merge as many format lists as possible */
215 for (i = 0; i < graph->filter_count; i++) {
216 AVFilterContext *filter = graph->filters[i];
218 for (j = 0; j < filter->input_count; j++) {
219 AVFilterLink *link = filter->inputs[j];
222 if (!link->in_formats || !link->out_formats)
223 return AVERROR(EINVAL);
225 if (link->type == AVMEDIA_TYPE_VIDEO &&
226 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
228 /* couldn't merge format lists, auto-insert scale filter */
229 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
230 graph->scale_sws_opts);
231 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
234 else if (link->type == AVMEDIA_TYPE_AUDIO) {
235 if (!link->in_chlayouts || !link->out_chlayouts ||
236 !link->in_packing || !link->out_packing)
237 return AVERROR(EINVAL);
239 /* Merge all three list before checking: that way, in all
240 * three categories, aconvert will use a common format
241 * whenever possible. */
242 formats = avfilter_merge_formats(link->in_formats, link->out_formats);
243 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
244 packing = avfilter_merge_formats(link->in_packing, link->out_packing);
245 if (!formats || !chlayouts || !packing)
246 if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
255 static void pick_format(AVFilterLink *link)
257 if (!link || !link->in_formats)
260 link->in_formats->format_count = 1;
261 link->format = link->in_formats->formats[0];
262 avfilter_formats_unref(&link->in_formats);
263 avfilter_formats_unref(&link->out_formats);
265 if (link->type == AVMEDIA_TYPE_AUDIO) {
266 link->in_chlayouts->format_count = 1;
267 link->channel_layout = link->in_chlayouts->formats[0];
268 avfilter_formats_unref(&link->in_chlayouts);
269 avfilter_formats_unref(&link->out_chlayouts);
271 link->in_packing->format_count = 1;
272 link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
273 avfilter_formats_unref(&link->in_packing);
274 avfilter_formats_unref(&link->out_packing);
278 static int reduce_formats_on_filter(AVFilterContext *filter)
280 int i, j, k, ret = 0;
282 for (i = 0; i < filter->input_count; i++) {
283 AVFilterLink *link = filter->inputs[i];
284 int format = link->out_formats->formats[0];
286 if (link->out_formats->format_count != 1)
289 for (j = 0; j < filter->output_count; j++) {
290 AVFilterLink *out_link = filter->outputs[j];
291 AVFilterFormats *fmts = out_link->in_formats;
293 if (link->type != out_link->type ||
294 out_link->in_formats->format_count == 1)
297 for (k = 0; k < out_link->in_formats->format_count; k++)
298 if (fmts->formats[k] == format) {
299 fmts->formats[0] = format;
300 fmts->format_count = 1;
309 static void reduce_formats(AVFilterGraph *graph)
316 for (i = 0; i < graph->filter_count; i++)
317 reduced |= reduce_formats_on_filter(graph->filters[i]);
321 static void pick_formats(AVFilterGraph *graph)
325 for (i = 0; i < graph->filter_count; i++) {
326 AVFilterContext *filter = graph->filters[i];
328 for (j = 0; j < filter->input_count; j++)
329 pick_format(filter->inputs[j]);
330 for (j = 0; j < filter->output_count; j++)
331 pick_format(filter->outputs[j]);
335 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
339 /* find supported formats from sub-filters, and merge along links */
340 if ((ret = query_formats(graph, log_ctx)) < 0)
343 /* Once everything is merged, it's possible that we'll still have
344 * multiple valid media format choices. We try to minimize the amount
345 * of format conversion inside filters */
346 reduce_formats(graph);
353 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
357 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
359 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
361 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
367 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
369 int i, r = AVERROR(ENOSYS);
374 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
375 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
376 if(r != AVERROR(ENOSYS))
383 for (i = 0; i < graph->filter_count; i++) {
384 AVFilterContext *filter = graph->filters[i];
385 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
386 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
387 if(r != AVERROR(ENOSYS)) {
388 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
397 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
404 for (i = 0; i < graph->filter_count; i++) {
405 AVFilterContext *filter = graph->filters[i];
406 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
407 AVFilterCommand **que = &filter->command_queue, *next;
408 while(*que && (*que)->time <= ts)
411 *que= av_mallocz(sizeof(AVFilterCommand));
412 (*que)->command = av_strdup(command);
413 (*que)->arg = av_strdup(arg);
415 (*que)->flags = flags;
417 if(flags & AVFILTER_CMD_FLAG_ONE)