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/avassert.h"
28 #include "libavutil/pixdesc.h"
30 #include "avfiltergraph.h"
33 #include "libavutil/log.h"
35 static const AVClass filtergraph_class = {
36 .class_name = "AVFilterGraph",
37 .item_name = av_default_item_name,
38 .version = LIBAVUTIL_VERSION_INT,
41 AVFilterGraph *avfilter_graph_alloc(void)
43 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
46 #if FF_API_GRAPH_AVCLASS
47 ret->av_class = &filtergraph_class;
52 void avfilter_graph_free(AVFilterGraph **graph)
56 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
57 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
58 av_freep(&(*graph)->sink_links);
59 av_freep(&(*graph)->scale_sws_opts);
60 av_freep(&(*graph)->filters);
64 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
66 AVFilterContext **filters = av_realloc(graph->filters,
67 sizeof(AVFilterContext*) * (graph->filter_count+1));
69 return AVERROR(ENOMEM);
71 graph->filters = filters;
72 graph->filters[graph->filter_count++] = filter;
77 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
78 const char *name, const char *args, void *opaque,
79 AVFilterGraph *graph_ctx)
83 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
85 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
87 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
93 avfilter_free(*filt_ctx);
98 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
100 AVFilterContext *filt;
103 for (i = 0; i < graph->filter_count; i++) {
104 filt = graph->filters[i];
106 for (j = 0; j < filt->input_count; j++) {
107 if (!filt->inputs[j] || !filt->inputs[j]->src) {
108 av_log(log_ctx, AV_LOG_ERROR,
109 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
110 filt->input_pads[j].name, filt->name, filt->filter->name);
111 return AVERROR(EINVAL);
115 for (j = 0; j < filt->output_count; j++) {
116 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
117 av_log(log_ctx, AV_LOG_ERROR,
118 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
119 filt->output_pads[j].name, filt->name, filt->filter->name);
120 return AVERROR(EINVAL);
128 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
130 AVFilterContext *filt;
133 for (i=0; i < graph->filter_count; i++) {
134 filt = graph->filters[i];
136 if (!filt->output_count) {
137 if ((ret = avfilter_config_links(filt)))
145 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
149 for (i = 0; i < graph->filter_count; i++)
150 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
151 return graph->filters[i];
156 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
157 const char *filt_name, const char *filt_args)
159 static int auto_count = 0, ret;
161 AVFilterContext *filt_ctx;
163 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
164 filt_name, auto_count++);
166 if ((ret = avfilter_graph_create_filter(&filt_ctx,
167 avfilter_get_by_name(filt_name),
168 inst_name, filt_args, NULL, graph)) < 0)
170 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
173 filt_ctx->filter->query_formats(filt_ctx);
175 if ( ((link = filt_ctx-> inputs[0]) &&
176 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
177 ((link = filt_ctx->outputs[0]) &&
178 !avfilter_merge_formats(link->in_formats, link->out_formats))
180 av_log(NULL, AV_LOG_ERROR,
181 "Impossible to convert between the formats supported by the filter "
182 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
183 return AVERROR(EINVAL);
186 if (link->type == AVMEDIA_TYPE_AUDIO &&
187 (((link = filt_ctx-> inputs[0]) &&
188 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
189 !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
190 ((link = filt_ctx->outputs[0]) &&
191 (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
192 !avfilter_merge_formats(link->in_packing, link->out_packing))))
194 av_log(NULL, AV_LOG_ERROR,
195 "Impossible to convert between the channel layouts/packing formats supported by the filter "
196 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
197 return AVERROR(EINVAL);
203 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
207 AVFilterFormats *formats, *chlayouts, *packing;
209 /* ask all the sub-filters for their supported media formats */
210 for (i = 0; i < graph->filter_count; i++) {
211 if (graph->filters[i]->filter->query_formats)
212 graph->filters[i]->filter->query_formats(graph->filters[i]);
214 avfilter_default_query_formats(graph->filters[i]);
217 /* go through and merge as many format lists as possible */
218 for (i = 0; i < graph->filter_count; i++) {
219 AVFilterContext *filter = graph->filters[i];
221 for (j = 0; j < filter->input_count; j++) {
222 AVFilterLink *link = filter->inputs[j];
225 if (!link->in_formats || !link->out_formats)
226 return AVERROR(EINVAL);
228 if (link->type == AVMEDIA_TYPE_VIDEO &&
229 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
231 /* couldn't merge format lists, auto-insert scale filter */
232 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
233 graph->scale_sws_opts);
234 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
237 else if (link->type == AVMEDIA_TYPE_AUDIO) {
238 if (!link->in_chlayouts || !link->out_chlayouts ||
239 !link->in_packing || !link->out_packing)
240 return AVERROR(EINVAL);
242 /* Merge all three list before checking: that way, in all
243 * three categories, aconvert will use a common format
244 * whenever possible. */
245 formats = avfilter_merge_formats(link->in_formats, link->out_formats);
246 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
247 packing = avfilter_merge_formats(link->in_packing, link->out_packing);
248 if (!formats || !chlayouts || !packing)
249 if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
258 static void pick_format(AVFilterLink *link, AVFilterLink *ref)
260 if (!link || !link->in_formats)
263 if (link->type == AVMEDIA_TYPE_VIDEO) {
264 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
265 int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
266 enum PixelFormat best= PIX_FMT_NONE;
268 for (i=0; i<link->in_formats->format_count; i++) {
269 enum PixelFormat p = link->in_formats->formats[i];
270 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
272 link->in_formats->formats[0] = best;
276 link->in_formats->format_count = 1;
277 link->format = link->in_formats->formats[0];
278 avfilter_formats_unref(&link->in_formats);
279 avfilter_formats_unref(&link->out_formats);
281 if (link->type == AVMEDIA_TYPE_AUDIO) {
282 link->in_chlayouts->format_count = 1;
283 link->channel_layout = link->in_chlayouts->formats[0];
284 avfilter_formats_unref(&link->in_chlayouts);
285 avfilter_formats_unref(&link->out_chlayouts);
287 link->in_packing->format_count = 1;
288 link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
289 avfilter_formats_unref(&link->in_packing);
290 avfilter_formats_unref(&link->out_packing);
294 static int reduce_formats_on_filter(AVFilterContext *filter)
296 int i, j, k, ret = 0;
298 for (i = 0; i < filter->input_count; i++) {
299 AVFilterLink *link = filter->inputs[i];
300 int format = link->out_formats->formats[0];
302 if (link->out_formats->format_count != 1)
305 for (j = 0; j < filter->output_count; j++) {
306 AVFilterLink *out_link = filter->outputs[j];
307 AVFilterFormats *fmts = out_link->in_formats;
309 if (link->type != out_link->type ||
310 out_link->in_formats->format_count == 1)
313 for (k = 0; k < out_link->in_formats->format_count; k++)
314 if (fmts->formats[k] == format) {
315 fmts->formats[0] = format;
316 fmts->format_count = 1;
325 static void reduce_formats(AVFilterGraph *graph)
332 for (i = 0; i < graph->filter_count; i++)
333 reduced |= reduce_formats_on_filter(graph->filters[i]);
337 static void pick_formats(AVFilterGraph *graph)
344 for (i = 0; i < graph->filter_count; i++) {
345 AVFilterContext *filter = graph->filters[i];
346 if (filter->input_count){
347 for (j = 0; j < filter->input_count; j++){
348 if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
349 pick_format(filter->inputs[j], NULL);
354 if (filter->output_count){
355 for (j = 0; j < filter->output_count; j++){
356 if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
357 pick_format(filter->outputs[j], NULL);
362 if (filter->input_count && filter->output_count && filter->inputs[0]->format>=0) {
363 for (j = 0; j < filter->output_count; j++) {
364 if(filter->outputs[j]->format<0) {
365 pick_format(filter->outputs[j], filter->inputs[0]);
373 for (i = 0; i < graph->filter_count; i++) {
374 AVFilterContext *filter = graph->filters[i];
376 for (j = 0; j < filter->input_count; j++)
377 pick_format(filter->inputs[j], NULL);
378 for (j = 0; j < filter->output_count; j++)
379 pick_format(filter->outputs[j], NULL);
384 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
388 /* find supported formats from sub-filters, and merge along links */
389 if ((ret = query_formats(graph, log_ctx)) < 0)
392 /* Once everything is merged, it's possible that we'll still have
393 * multiple valid media format choices. We try to minimize the amount
394 * of format conversion inside filters */
395 reduce_formats(graph);
402 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
406 int sink_links_count = 0, n = 0;
408 AVFilterLink **sinks;
410 for (i = 0; i < graph->filter_count; i++) {
411 f = graph->filters[i];
412 for (j = 0; j < f->input_count; j++) {
413 f->inputs[j]->graph = graph;
414 f->inputs[j]->age_index = -1;
416 for (j = 0; j < f->output_count; j++) {
417 f->outputs[j]->graph = graph;
418 f->outputs[j]->age_index= -1;
420 if (!f->output_count) {
421 if (f->input_count > INT_MAX - sink_links_count)
422 return AVERROR(EINVAL);
423 sink_links_count += f->input_count;
426 sinks = av_calloc(sink_links_count, sizeof(*sinks));
428 return AVERROR(ENOMEM);
429 for (i = 0; i < graph->filter_count; i++) {
430 f = graph->filters[i];
431 if (!f->output_count) {
432 for (j = 0; j < f->input_count; j++) {
433 sinks[n] = f->inputs[j];
434 f->inputs[j]->age_index = n++;
438 av_assert0(n == sink_links_count);
439 graph->sink_links = sinks;
440 graph->sink_links_count = sink_links_count;
444 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
448 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
450 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
452 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
454 if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
460 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
462 int i, r = AVERROR(ENOSYS);
467 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
468 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
469 if(r != AVERROR(ENOSYS))
476 for (i = 0; i < graph->filter_count; i++) {
477 AVFilterContext *filter = graph->filters[i];
478 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
479 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
480 if(r != AVERROR(ENOSYS)) {
481 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
490 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
497 for (i = 0; i < graph->filter_count; i++) {
498 AVFilterContext *filter = graph->filters[i];
499 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
500 AVFilterCommand **que = &filter->command_queue, *next;
501 while(*que && (*que)->time <= ts)
504 *que= av_mallocz(sizeof(AVFilterCommand));
505 (*que)->command = av_strdup(command);
506 (*que)->arg = av_strdup(arg);
508 (*que)->flags = flags;
510 if(flags & AVFILTER_CMD_FLAG_ONE)
518 static void heap_bubble_up(AVFilterGraph *graph,
519 AVFilterLink *link, int index)
521 AVFilterLink **links = graph->sink_links;
524 int parent = (index - 1) >> 1;
525 if (links[parent]->current_pts >= link->current_pts)
527 links[index] = links[parent];
528 links[index]->age_index = index;
532 link->age_index = index;
535 static void heap_bubble_down(AVFilterGraph *graph,
536 AVFilterLink *link, int index)
538 AVFilterLink **links = graph->sink_links;
541 int child = 2 * index + 1;
542 if (child >= graph->sink_links_count)
544 if (child + 1 < graph->sink_links_count &&
545 links[child + 1]->current_pts < links[child]->current_pts)
547 if (link->current_pts < links[child]->current_pts)
549 links[index] = links[child];
550 links[index]->age_index = index;
554 link->age_index = index;
557 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
559 heap_bubble_up (graph, link, link->age_index);
560 heap_bubble_down(graph, link, link->age_index);
564 int avfilter_graph_request_oldest(AVFilterGraph *graph)
566 while (graph->sink_links_count) {
567 AVFilterLink *oldest = graph->sink_links[0];
568 int r = avfilter_request_frame(oldest);
569 if (r != AVERROR_EOF)
571 /* EOF: remove the link from the heap */
572 if (oldest->age_index < --graph->sink_links_count)
573 heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
575 oldest->age_index = -1;