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)
190 AVFilterFormats *formats, *chlayouts, *packing;
192 /* ask all the sub-filters for their supported media formats */
193 for (i = 0; i < graph->filter_count; i++) {
194 if (graph->filters[i]->filter->query_formats)
195 graph->filters[i]->filter->query_formats(graph->filters[i]);
197 avfilter_default_query_formats(graph->filters[i]);
200 /* go through and merge as many format lists as possible */
201 for (i = 0; i < graph->filter_count; i++) {
202 AVFilterContext *filter = graph->filters[i];
204 for (j = 0; j < filter->input_count; j++) {
205 AVFilterLink *link = filter->inputs[j];
208 if (!link->in_formats || !link->out_formats)
209 return AVERROR(EINVAL);
211 if (link->type == AVMEDIA_TYPE_VIDEO &&
212 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
214 /* couldn't merge format lists, auto-insert scale filter */
215 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
216 graph->scale_sws_opts);
217 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
220 else if (link->type == AVMEDIA_TYPE_AUDIO) {
221 if (!link->in_chlayouts || !link->out_chlayouts ||
222 !link->in_packing || !link->out_packing)
223 return AVERROR(EINVAL);
225 /* Merge all three list before checking: that way, in all
226 * three categories, aconvert will use a common format
227 * whenever possible. */
228 formats = avfilter_merge_formats(link->in_formats, link->out_formats);
229 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
230 packing = avfilter_merge_formats(link->in_packing, link->out_packing);
231 if (!formats || !chlayouts || !packing)
232 if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
241 static void pick_format(AVFilterLink *link)
243 if (!link || !link->in_formats)
246 link->in_formats->format_count = 1;
247 link->format = link->in_formats->formats[0];
248 avfilter_formats_unref(&link->in_formats);
249 avfilter_formats_unref(&link->out_formats);
251 if (link->type == AVMEDIA_TYPE_AUDIO) {
252 link->in_chlayouts->format_count = 1;
253 link->channel_layout = link->in_chlayouts->formats[0];
254 avfilter_formats_unref(&link->in_chlayouts);
255 avfilter_formats_unref(&link->out_chlayouts);
257 link->in_packing->format_count = 1;
258 link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
259 avfilter_formats_unref(&link->in_packing);
260 avfilter_formats_unref(&link->out_packing);
264 static void pick_formats(AVFilterGraph *graph)
268 for (i = 0; i < graph->filter_count; i++) {
269 AVFilterContext *filter = graph->filters[i];
271 for (j = 0; j < filter->input_count; j++)
272 pick_format(filter->inputs[j]);
273 for (j = 0; j < filter->output_count; j++)
274 pick_format(filter->outputs[j]);
278 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
282 /* find supported formats from sub-filters, and merge along links */
283 if ((ret = query_formats(graph, log_ctx)) < 0)
286 /* Once everything is merged, it's possible that we'll still have
287 * multiple valid media format choices. We pick the first one. */
293 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
297 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
299 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
301 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
307 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
309 int i, r = AVERROR(ENOSYS);
314 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
315 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
316 if(r != AVERROR(ENOSYS))
323 for (i = 0; i < graph->filter_count; i++) {
324 AVFilterContext *filter = graph->filters[i];
325 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
326 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
327 if(r != AVERROR(ENOSYS)) {
328 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
337 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
344 for (i = 0; i < graph->filter_count; i++) {
345 AVFilterContext *filter = graph->filters[i];
346 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
347 AVFilterCommand **que = &filter->command_queue, *next;
348 while(*que && (*que)->time <= ts)
351 *que= av_mallocz(sizeof(AVFilterCommand));
352 (*que)->command = av_strdup(command);
353 (*que)->arg = av_strdup(arg);
355 (*que)->flags = flags;
357 if(flags & AVFILTER_CMD_FLAG_ONE)