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"
27 #include "libavutil/pixdesc.h"
29 #include "avfiltergraph.h"
32 #include "libavutil/log.h"
34 static const AVClass filtergraph_class = {
35 .class_name = "AVFilterGraph",
36 .item_name = av_default_item_name,
37 .version = LIBAVUTIL_VERSION_INT,
40 AVFilterGraph *avfilter_graph_alloc(void)
42 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
45 #if FF_API_GRAPH_AVCLASS
46 ret->av_class = &filtergraph_class;
51 void avfilter_graph_free(AVFilterGraph **graph)
55 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
56 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
57 av_freep(&(*graph)->scale_sws_opts);
58 av_freep(&(*graph)->filters);
62 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
64 AVFilterContext **filters = av_realloc(graph->filters,
65 sizeof(AVFilterContext*) * (graph->filter_count+1));
67 return AVERROR(ENOMEM);
69 graph->filters = filters;
70 graph->filters[graph->filter_count++] = filter;
75 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
76 const char *name, const char *args, void *opaque,
77 AVFilterGraph *graph_ctx)
81 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
83 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
85 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
91 avfilter_free(*filt_ctx);
96 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
98 AVFilterContext *filt;
101 for (i = 0; i < graph->filter_count; i++) {
102 filt = graph->filters[i];
104 for (j = 0; j < filt->input_count; j++) {
105 if (!filt->inputs[j] || !filt->inputs[j]->src) {
106 av_log(log_ctx, AV_LOG_ERROR,
107 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
108 filt->input_pads[j].name, filt->name, filt->filter->name);
109 return AVERROR(EINVAL);
113 for (j = 0; j < filt->output_count; j++) {
114 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
115 av_log(log_ctx, AV_LOG_ERROR,
116 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
117 filt->output_pads[j].name, filt->name, filt->filter->name);
118 return AVERROR(EINVAL);
126 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
128 AVFilterContext *filt;
131 for (i=0; i < graph->filter_count; i++) {
132 filt = graph->filters[i];
134 if (!filt->output_count) {
135 if ((ret = avfilter_config_links(filt)))
143 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
147 for (i = 0; i < graph->filter_count; i++)
148 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
149 return graph->filters[i];
154 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
155 const char *filt_name, const char *filt_args)
157 static int auto_count = 0, ret;
159 AVFilterContext *filt_ctx;
161 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
162 filt_name, auto_count++);
164 if ((ret = avfilter_graph_create_filter(&filt_ctx,
165 avfilter_get_by_name(filt_name),
166 inst_name, filt_args, NULL, graph)) < 0)
168 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
171 filt_ctx->filter->query_formats(filt_ctx);
173 if ( ((link = filt_ctx-> inputs[0]) &&
174 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
175 ((link = filt_ctx->outputs[0]) &&
176 !avfilter_merge_formats(link->in_formats, link->out_formats))
178 av_log(NULL, AV_LOG_ERROR,
179 "Impossible to convert between the formats supported by the filter "
180 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
181 return AVERROR(EINVAL);
184 if (link->type == AVMEDIA_TYPE_AUDIO &&
185 (((link = filt_ctx-> inputs[0]) &&
186 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
187 !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
188 ((link = filt_ctx->outputs[0]) &&
189 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
190 !avfilter_merge_formats(link->in_packing, link->out_packing))))
192 av_log(NULL, AV_LOG_ERROR,
193 "Impossible to convert between the channel layouts/packing formats supported by the filter "
194 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
195 return AVERROR(EINVAL);
201 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
205 AVFilterFormats *formats, *chlayouts, *packing;
207 /* ask all the sub-filters for their supported media formats */
208 for (i = 0; i < graph->filter_count; i++) {
209 if (graph->filters[i]->filter->query_formats)
210 graph->filters[i]->filter->query_formats(graph->filters[i]);
212 avfilter_default_query_formats(graph->filters[i]);
215 /* go through and merge as many format lists as possible */
216 for (i = 0; i < graph->filter_count; i++) {
217 AVFilterContext *filter = graph->filters[i];
219 for (j = 0; j < filter->input_count; j++) {
220 AVFilterLink *link = filter->inputs[j];
223 if (!link->in_formats || !link->out_formats)
224 return AVERROR(EINVAL);
226 if (link->type == AVMEDIA_TYPE_VIDEO &&
227 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
229 /* couldn't merge format lists, auto-insert scale filter */
230 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
231 graph->scale_sws_opts);
232 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
235 else if (link->type == AVMEDIA_TYPE_AUDIO) {
236 if (!link->in_chlayouts || !link->out_chlayouts ||
237 !link->in_packing || !link->out_packing)
238 return AVERROR(EINVAL);
240 /* Merge all three list before checking: that way, in all
241 * three categories, aconvert will use a common format
242 * whenever possible. */
243 formats = avfilter_merge_formats(link->in_formats, link->out_formats);
244 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
245 packing = avfilter_merge_formats(link->in_packing, link->out_packing);
246 if (!formats || !chlayouts || !packing)
247 if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
256 static void pick_format(AVFilterLink *link, AVFilterLink *ref)
258 if (!link || !link->in_formats)
261 if (link->type == AVMEDIA_TYPE_VIDEO) {
262 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
263 int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
264 enum PixelFormat best= PIX_FMT_NONE;
266 for (i=0; i<link->in_formats->format_count; i++) {
267 enum PixelFormat p = link->in_formats->formats[i];
268 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
272 link->format = link->in_formats->formats[0];
275 link->in_formats->format_count = 1;
276 avfilter_formats_unref(&link->in_formats);
277 avfilter_formats_unref(&link->out_formats);
279 if (link->type == AVMEDIA_TYPE_AUDIO) {
280 link->in_chlayouts->format_count = 1;
281 link->channel_layout = link->in_chlayouts->formats[0];
282 avfilter_formats_unref(&link->in_chlayouts);
283 avfilter_formats_unref(&link->out_chlayouts);
285 link->in_packing->format_count = 1;
286 link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
287 avfilter_formats_unref(&link->in_packing);
288 avfilter_formats_unref(&link->out_packing);
292 static int reduce_formats_on_filter(AVFilterContext *filter)
294 int i, j, k, ret = 0;
296 for (i = 0; i < filter->input_count; i++) {
297 AVFilterLink *link = filter->inputs[i];
298 int format = link->out_formats->formats[0];
300 if (link->out_formats->format_count != 1)
303 for (j = 0; j < filter->output_count; j++) {
304 AVFilterLink *out_link = filter->outputs[j];
305 AVFilterFormats *fmts = out_link->in_formats;
307 if (link->type != out_link->type ||
308 out_link->in_formats->format_count == 1)
311 for (k = 0; k < out_link->in_formats->format_count; k++)
312 if (fmts->formats[k] == format) {
313 fmts->formats[0] = format;
314 fmts->format_count = 1;
323 static void reduce_formats(AVFilterGraph *graph)
330 for (i = 0; i < graph->filter_count; i++)
331 reduced |= reduce_formats_on_filter(graph->filters[i]);
335 static void pick_formats(AVFilterGraph *graph)
339 for (i = 0; i < graph->filter_count; i++) {
340 AVFilterContext *filter = graph->filters[i];
341 if (filter->input_count && filter->output_count) {
342 for (j = 0; j < filter->input_count; j++)
343 pick_format(filter->inputs[j], NULL);
344 for (j = 0; j < filter->output_count; j++)
345 pick_format(filter->outputs[j], filter->inputs[0]);
348 for (i = 0; i < graph->filter_count; i++) {
349 AVFilterContext *filter = graph->filters[i];
350 if (!(filter->input_count && filter->output_count)) {
351 for (j = 0; j < filter->input_count; j++)
352 pick_format(filter->inputs[j], NULL);
353 for (j = 0; j < filter->output_count; j++)
354 pick_format(filter->outputs[j], NULL);
359 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
363 /* find supported formats from sub-filters, and merge along links */
364 if ((ret = query_formats(graph, log_ctx)) < 0)
367 /* Once everything is merged, it's possible that we'll still have
368 * multiple valid media format choices. We try to minimize the amount
369 * of format conversion inside filters */
370 reduce_formats(graph);
377 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
381 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
383 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
385 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
391 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
393 int i, r = AVERROR(ENOSYS);
398 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
399 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
400 if(r != AVERROR(ENOSYS))
407 for (i = 0; i < graph->filter_count; i++) {
408 AVFilterContext *filter = graph->filters[i];
409 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
410 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
411 if(r != AVERROR(ENOSYS)) {
412 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
421 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
428 for (i = 0; i < graph->filter_count; i++) {
429 AVFilterContext *filter = graph->filters[i];
430 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
431 AVFilterCommand **que = &filter->command_queue, *next;
432 while(*que && (*que)->time <= ts)
435 *que= av_mallocz(sizeof(AVFilterCommand));
436 (*que)->command = av_strdup(command);
437 (*que)->arg = av_strdup(arg);
439 (*que)->flags = flags;
441 if(flags & AVFILTER_CMD_FLAG_ONE)