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 AVFilterGraph *avfilter_graph_alloc(void)
33 return av_mallocz(sizeof(AVFilterGraph));
36 void avfilter_graph_free(AVFilterGraph **graph)
40 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
41 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
42 av_freep(&(*graph)->scale_sws_opts);
43 av_freep(&(*graph)->filters);
47 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
49 AVFilterContext **filters = av_realloc(graph->filters,
50 sizeof(AVFilterContext*) * (graph->filter_count+1));
52 return AVERROR(ENOMEM);
54 graph->filters = filters;
55 graph->filters[graph->filter_count++] = filter;
60 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
61 const char *name, const char *args, void *opaque,
62 AVFilterGraph *graph_ctx)
66 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
68 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
70 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
76 avfilter_free(*filt_ctx);
81 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
83 AVFilterContext *filt;
86 for (i = 0; i < graph->filter_count; i++) {
87 filt = graph->filters[i];
89 for (j = 0; j < filt->input_count; j++) {
90 if (!filt->inputs[j] || !filt->inputs[j]->src) {
91 av_log(log_ctx, AV_LOG_ERROR,
92 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
93 filt->input_pads[j].name, filt->name, filt->filter->name);
94 return AVERROR(EINVAL);
98 for (j = 0; j < filt->output_count; j++) {
99 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
100 av_log(log_ctx, AV_LOG_ERROR,
101 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
102 filt->output_pads[j].name, filt->name, filt->filter->name);
103 return AVERROR(EINVAL);
111 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
113 AVFilterContext *filt;
116 for (i=0; i < graph->filter_count; i++) {
117 filt = graph->filters[i];
119 if (!filt->output_count) {
120 if ((ret = avfilter_config_links(filt)))
128 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
132 for (i = 0; i < graph->filter_count; i++)
133 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
134 return graph->filters[i];
139 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
140 const char *filt_name, const char *filt_args)
142 static int auto_count = 0, ret;
144 AVFilterContext *filt_ctx;
146 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
147 filt_name, auto_count++);
149 if ((ret = avfilter_graph_create_filter(&filt_ctx,
150 avfilter_get_by_name(filt_name),
151 inst_name, filt_args, NULL, graph)) < 0)
153 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
156 filt_ctx->filter->query_formats(filt_ctx);
158 if ( ((link = filt_ctx-> inputs[0]) &&
159 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
160 ((link = filt_ctx->outputs[0]) &&
161 !avfilter_merge_formats(link->in_formats, link->out_formats))
163 av_log(NULL, AV_LOG_ERROR,
164 "Impossible to convert between the formats supported by the filter "
165 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
166 return AVERROR(EINVAL);
169 if (link->type == AVMEDIA_TYPE_AUDIO &&
170 (((link = filt_ctx-> inputs[0]) &&
171 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
172 !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
173 ((link = filt_ctx->outputs[0]) &&
174 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
175 !avfilter_merge_formats(link->in_packing, link->out_packing))))
177 av_log(NULL, AV_LOG_ERROR,
178 "Impossible to convert between the channel layouts/packing formats supported by the filter "
179 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
180 return AVERROR(EINVAL);
186 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
191 /* ask all the sub-filters for their supported media formats */
192 for (i = 0; i < graph->filter_count; i++) {
193 if (graph->filters[i]->filter->query_formats)
194 graph->filters[i]->filter->query_formats(graph->filters[i]);
196 avfilter_default_query_formats(graph->filters[i]);
199 /* go through and merge as many format lists as possible */
200 for (i = 0; i < graph->filter_count; i++) {
201 AVFilterContext *filter = graph->filters[i];
203 for (j = 0; j < filter->input_count; j++) {
204 AVFilterLink *link = filter->inputs[j];
207 if (!link->in_formats || !link->out_formats)
208 return AVERROR(EINVAL);
210 if (link->type == AVMEDIA_TYPE_VIDEO &&
211 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
213 /* couldn't merge format lists, auto-insert scale filter */
214 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
215 graph->scale_sws_opts);
216 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
219 else if (link->type == AVMEDIA_TYPE_AUDIO) {
220 if (!link->in_chlayouts || !link->out_chlayouts ||
221 !link->in_packing || !link->out_packing)
222 return AVERROR(EINVAL);
224 if (!avfilter_merge_formats(link->in_formats, link->out_formats) ||
225 !avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
226 !avfilter_merge_formats(link->in_packing, link->out_packing))
227 if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
236 static void pick_format(AVFilterLink *link)
238 if (!link || !link->in_formats)
241 link->in_formats->format_count = 1;
242 link->format = link->in_formats->formats[0];
243 avfilter_formats_unref(&link->in_formats);
244 avfilter_formats_unref(&link->out_formats);
246 if (link->type == AVMEDIA_TYPE_AUDIO) {
247 link->in_chlayouts->format_count = 1;
248 link->channel_layout = link->in_chlayouts->formats[0];
249 avfilter_formats_unref(&link->in_chlayouts);
250 avfilter_formats_unref(&link->out_chlayouts);
252 link->in_packing->format_count = 1;
253 link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
254 avfilter_formats_unref(&link->in_packing);
255 avfilter_formats_unref(&link->out_packing);
259 static void pick_formats(AVFilterGraph *graph)
263 for (i = 0; i < graph->filter_count; i++) {
264 AVFilterContext *filter = graph->filters[i];
266 for (j = 0; j < filter->input_count; j++)
267 pick_format(filter->inputs[j]);
268 for (j = 0; j < filter->output_count; j++)
269 pick_format(filter->outputs[j]);
273 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
277 /* find supported formats from sub-filters, and merge along links */
278 if ((ret = query_formats(graph, log_ctx)) < 0)
281 /* Once everything is merged, it's possible that we'll still have
282 * multiple valid media format choices. We pick the first one. */
288 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
292 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
294 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
296 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
302 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
304 int i, r = AVERROR(ENOSYS);
309 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
310 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
311 if(r != AVERROR(ENOSYS))
318 for (i = 0; i < graph->filter_count; i++) {
319 AVFilterContext *filter = graph->filters[i];
320 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
321 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
322 if(r != AVERROR(ENOSYS)) {
323 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
332 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
339 for (i = 0; i < graph->filter_count; i++) {
340 AVFilterContext *filter = graph->filters[i];
341 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
342 AVFilterCommand **que = &filter->command_queue, *next;
343 while(*que && (*que)->time <= ts)
346 *que= av_mallocz(sizeof(AVFilterCommand));
347 (*que)->command = av_strdup(command);
348 (*que)->arg = av_strdup(arg);
350 (*que)->flags = flags;
352 if(flags & AVFILTER_CMD_FLAG_ONE)