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"
29 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt2()
31 #include "avfiltergraph.h"
35 #include "libavutil/audioconvert.h"
36 #include "libavutil/avassert.h"
37 #include "libavutil/log.h"
39 static const AVClass filtergraph_class = {
40 .class_name = "AVFilterGraph",
41 .item_name = av_default_item_name,
42 .version = LIBAVUTIL_VERSION_INT,
43 .category = AV_CLASS_CATEGORY_FILTER,
46 AVFilterGraph *avfilter_graph_alloc(void)
48 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
51 ret->av_class = &filtergraph_class;
55 void avfilter_graph_free(AVFilterGraph **graph)
59 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
60 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
61 av_freep(&(*graph)->sink_links);
62 av_freep(&(*graph)->scale_sws_opts);
63 av_freep(&(*graph)->filters);
67 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
69 AVFilterContext **filters = av_realloc(graph->filters,
70 sizeof(AVFilterContext*) * (graph->filter_count+1));
72 return AVERROR(ENOMEM);
74 graph->filters = filters;
75 graph->filters[graph->filter_count++] = filter;
80 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
81 const char *name, const char *args, void *opaque,
82 AVFilterGraph *graph_ctx)
86 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
88 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
90 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
96 avfilter_free(*filt_ctx);
101 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
103 graph->disable_auto_convert = flags;
107 * Check for the validity of graph.
109 * A graph is considered valid if all its input and output pads are
112 * @return 0 in case of success, a negative value otherwise
114 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
116 AVFilterContext *filt;
119 for (i = 0; i < graph->filter_count; i++) {
120 filt = graph->filters[i];
122 for (j = 0; j < filt->nb_inputs; j++) {
123 if (!filt->inputs[j] || !filt->inputs[j]->src) {
124 av_log(log_ctx, AV_LOG_ERROR,
125 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
126 filt->input_pads[j].name, filt->name, filt->filter->name);
127 return AVERROR(EINVAL);
131 for (j = 0; j < filt->nb_outputs; j++) {
132 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
133 av_log(log_ctx, AV_LOG_ERROR,
134 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
135 filt->output_pads[j].name, filt->name, filt->filter->name);
136 return AVERROR(EINVAL);
145 * Configure all the links of graphctx.
147 * @return 0 in case of success, a negative value otherwise
149 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
151 AVFilterContext *filt;
154 for (i=0; i < graph->filter_count; i++) {
155 filt = graph->filters[i];
157 if (!filt->nb_outputs) {
158 if ((ret = avfilter_config_links(filt)))
166 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
170 for (i = 0; i < graph->filter_count; i++)
171 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
172 return graph->filters[i];
177 static int filter_query_formats(AVFilterContext *ctx)
180 AVFilterFormats *formats;
181 AVFilterChannelLayouts *chlayouts;
182 AVFilterFormats *samplerates;
183 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
184 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
187 if ((ret = ctx->filter->query_formats(ctx)) < 0)
190 formats = ff_all_formats(type);
192 return AVERROR(ENOMEM);
193 ff_set_common_formats(ctx, formats);
194 if (type == AVMEDIA_TYPE_AUDIO) {
195 samplerates = ff_all_samplerates();
197 return AVERROR(ENOMEM);
198 ff_set_common_samplerates(ctx, samplerates);
199 chlayouts = ff_all_channel_layouts();
201 return AVERROR(ENOMEM);
202 ff_set_common_channel_layouts(ctx, chlayouts);
207 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
208 const char *filt_name, const char *filt_args)
210 static int auto_count = 0, ret;
212 AVFilterContext *filt_ctx;
214 if (graph->disable_auto_convert) {
215 av_log(NULL, AV_LOG_ERROR,
216 "The filters '%s' and '%s' do not have a common format "
217 "and automatic conversion is disabled.\n",
218 link->src->name, link->dst->name);
219 return AVERROR(EINVAL);
222 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
223 filt_name, auto_count++);
225 if ((ret = avfilter_graph_create_filter(&filt_ctx,
226 avfilter_get_by_name(filt_name),
227 inst_name, filt_args, NULL, graph)) < 0)
229 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
232 filter_query_formats(filt_ctx);
234 if ( ((link = filt_ctx-> inputs[0]) &&
235 !ff_merge_formats(link->in_formats, link->out_formats)) ||
236 ((link = filt_ctx->outputs[0]) &&
237 !ff_merge_formats(link->in_formats, link->out_formats))
239 av_log(NULL, AV_LOG_ERROR,
240 "Impossible to convert between the formats supported by the filter "
241 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
242 return AVERROR(EINVAL);
245 if (link->type == AVMEDIA_TYPE_AUDIO &&
246 (((link = filt_ctx-> inputs[0]) &&
247 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
248 ((link = filt_ctx->outputs[0]) &&
249 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
251 av_log(NULL, AV_LOG_ERROR,
252 "Impossible to convert between the channel layouts formats supported by the filter "
253 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
254 return AVERROR(EINVAL);
260 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
264 AVFilterFormats *formats;
265 AVFilterChannelLayouts *chlayouts;
266 AVFilterFormats *samplerates;
267 int scaler_count = 0, resampler_count = 0;
269 for (j = 0; j < 2; j++) {
270 /* ask all the sub-filters for their supported media formats */
271 for (i = 0; i < graph->filter_count; i++) {
272 /* Call query_formats on sources first.
273 This is a temporary workaround for amerge,
274 until format renegociation is implemented. */
275 if (!graph->filters[i]->nb_inputs == j)
277 if (graph->filters[i]->filter->query_formats)
278 ret = filter_query_formats(graph->filters[i]);
280 ret = ff_default_query_formats(graph->filters[i]);
286 /* go through and merge as many format lists as possible */
287 for (i = 0; i < graph->filter_count; i++) {
288 AVFilterContext *filter = graph->filters[i];
290 for (j = 0; j < filter->nb_inputs; j++) {
291 AVFilterLink *link = filter->inputs[j];
295 if (!link->in_formats || !link->out_formats)
296 return AVERROR(EINVAL);
298 if (link->type == AVMEDIA_TYPE_VIDEO &&
299 !ff_merge_formats(link->in_formats, link->out_formats)) {
301 /* couldn't merge format lists, auto-insert scale filter */
302 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
303 graph->scale_sws_opts);
304 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
307 else if (link->type == AVMEDIA_TYPE_AUDIO) {
308 if (!link->in_channel_layouts || !link->out_channel_layouts)
309 return AVERROR(EINVAL);
311 /* Merge all three list before checking: that way, in all
312 * three categories, aconvert will use a common format
313 * whenever possible. */
314 formats = ff_merge_formats(link->in_formats, link->out_formats);
315 chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
316 samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
318 if (!formats || !chlayouts || !samplerates)
319 if (ret = insert_conv_filter(graph, link, "aresample", NULL))
322 int convert_needed = 0;
327 if (link->in_formats != link->out_formats &&
328 !ff_merge_formats(link->in_formats,
331 if (link->type == AVMEDIA_TYPE_AUDIO) {
332 if (link->in_channel_layouts != link->out_channel_layouts &&
333 !ff_merge_channel_layouts(link->in_channel_layouts,
334 link->out_channel_layouts))
336 if (link->in_samplerates != link->out_samplerates &&
337 !ff_merge_samplerates(link->in_samplerates,
338 link->out_samplerates))
342 if (convert_needed) {
343 AVFilterContext *convert;
345 AVFilterLink *inlink, *outlink;
346 char scale_args[256];
349 /* couldn't merge format lists. auto-insert conversion filter */
350 switch (link->type) {
351 case AVMEDIA_TYPE_VIDEO:
352 if (!(filter = avfilter_get_by_name("scale"))) {
353 av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
354 "not present, cannot convert pixel formats.\n");
355 return AVERROR(EINVAL);
358 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
360 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
361 if ((ret = avfilter_graph_create_filter(&convert, filter,
362 inst_name, scale_args, NULL,
366 case AVMEDIA_TYPE_AUDIO:
367 if (!(filter = avfilter_get_by_name("aresample"))) {
368 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
369 "not present, cannot convert audio formats.\n");
370 return AVERROR(EINVAL);
373 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
375 if ((ret = avfilter_graph_create_filter(&convert, filter,
376 inst_name, NULL, NULL, graph)) < 0)
380 return AVERROR(EINVAL);
383 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
386 filter_query_formats(convert);
387 inlink = convert->inputs[0];
388 outlink = convert->outputs[0];
389 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
390 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
391 ret |= AVERROR(ENOSYS);
392 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
393 (!ff_merge_samplerates(inlink->in_samplerates,
394 inlink->out_samplerates) ||
395 !ff_merge_channel_layouts(inlink->in_channel_layouts,
396 inlink->out_channel_layouts)))
397 ret |= AVERROR(ENOSYS);
398 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
399 (!ff_merge_samplerates(outlink->in_samplerates,
400 outlink->out_samplerates) ||
401 !ff_merge_channel_layouts(outlink->in_channel_layouts,
402 outlink->out_channel_layouts)))
403 ret |= AVERROR(ENOSYS);
406 av_log(log_ctx, AV_LOG_ERROR,
407 "Impossible to convert between the formats supported by the filter "
408 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
419 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
421 if (!link || !link->in_formats)
424 if (link->type == AVMEDIA_TYPE_VIDEO) {
425 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
426 int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
427 enum PixelFormat best= PIX_FMT_NONE;
429 for (i=0; i<link->in_formats->format_count; i++) {
430 enum PixelFormat p = link->in_formats->formats[i];
431 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
433 link->in_formats->formats[0] = best;
437 link->in_formats->format_count = 1;
438 link->format = link->in_formats->formats[0];
440 if (link->type == AVMEDIA_TYPE_AUDIO) {
441 if (!link->in_samplerates->format_count) {
442 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
443 " the link between filters %s and %s.\n", link->src->name,
445 return AVERROR(EINVAL);
447 link->in_samplerates->format_count = 1;
448 link->sample_rate = link->in_samplerates->formats[0];
450 if (!link->in_channel_layouts->nb_channel_layouts) {
451 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
452 "the link between filters %s and %s.\n", link->src->name,
454 return AVERROR(EINVAL);
456 link->in_channel_layouts->nb_channel_layouts = 1;
457 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
460 ff_formats_unref(&link->in_formats);
461 ff_formats_unref(&link->out_formats);
462 ff_formats_unref(&link->in_samplerates);
463 ff_formats_unref(&link->out_samplerates);
464 ff_channel_layouts_unref(&link->in_channel_layouts);
465 ff_channel_layouts_unref(&link->out_channel_layouts);
470 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
472 for (i = 0; i < filter->nb_inputs; i++) { \
473 AVFilterLink *link = filter->inputs[i]; \
476 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
478 fmt = link->out_ ## list->var[0]; \
480 for (j = 0; j < filter->nb_outputs; j++) { \
481 AVFilterLink *out_link = filter->outputs[j]; \
484 if (link->type != out_link->type || \
485 out_link->in_ ## list->nb == 1) \
487 fmts = out_link->in_ ## list; \
489 if (!out_link->in_ ## list->nb) { \
490 add_format(&out_link->in_ ##list, fmt); \
494 for (k = 0; k < out_link->in_ ## list->nb; k++) \
495 if (fmts->var[k] == fmt) { \
496 fmts->var[0] = fmt; \
505 static int reduce_formats_on_filter(AVFilterContext *filter)
507 int i, j, k, ret = 0;
509 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
510 format_count, ff_add_format);
511 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
512 format_count, ff_add_format);
513 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
514 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
519 static void reduce_formats(AVFilterGraph *graph)
526 for (i = 0; i < graph->filter_count; i++)
527 reduced |= reduce_formats_on_filter(graph->filters[i]);
531 static void swap_samplerates_on_filter(AVFilterContext *filter)
533 AVFilterLink *link = NULL;
537 for (i = 0; i < filter->nb_inputs; i++) {
538 link = filter->inputs[i];
540 if (link->type == AVMEDIA_TYPE_AUDIO &&
541 link->out_samplerates->format_count == 1)
544 if (i == filter->nb_inputs)
547 sample_rate = link->out_samplerates->formats[0];
549 for (i = 0; i < filter->nb_outputs; i++) {
550 AVFilterLink *outlink = filter->outputs[i];
551 int best_idx, best_diff = INT_MAX;
553 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
554 outlink->in_samplerates->format_count < 2)
557 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
558 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
560 if (diff < best_diff) {
565 FFSWAP(int, outlink->in_samplerates->formats[0],
566 outlink->in_samplerates->formats[best_idx]);
570 static void swap_samplerates(AVFilterGraph *graph)
574 for (i = 0; i < graph->filter_count; i++)
575 swap_samplerates_on_filter(graph->filters[i]);
578 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
580 AVFilterLink *link = NULL;
584 for (i = 0; i < filter->nb_inputs; i++) {
585 link = filter->inputs[i];
587 if (link->type == AVMEDIA_TYPE_AUDIO &&
588 link->out_channel_layouts->nb_channel_layouts == 1)
591 if (i == filter->nb_inputs)
594 chlayout = link->out_channel_layouts->channel_layouts[0];
596 for (i = 0; i < filter->nb_outputs; i++) {
597 AVFilterLink *outlink = filter->outputs[i];
598 int best_idx, best_score = INT_MIN;
600 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
601 outlink->in_channel_layouts->nb_channel_layouts < 2)
604 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
605 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
606 int matched_channels = av_get_channel_layout_nb_channels(chlayout &
608 int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
610 int score = matched_channels - extra_channels;
612 if (score > best_score) {
617 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
618 outlink->in_channel_layouts->channel_layouts[best_idx]);
623 static void swap_channel_layouts(AVFilterGraph *graph)
627 for (i = 0; i < graph->filter_count; i++)
628 swap_channel_layouts_on_filter(graph->filters[i]);
631 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
633 AVFilterLink *link = NULL;
637 for (i = 0; i < filter->nb_inputs; i++) {
638 link = filter->inputs[i];
640 if (link->type == AVMEDIA_TYPE_AUDIO &&
641 link->out_formats->format_count == 1)
644 if (i == filter->nb_inputs)
647 format = link->out_formats->formats[0];
648 bps = av_get_bytes_per_sample(format);
650 for (i = 0; i < filter->nb_outputs; i++) {
651 AVFilterLink *outlink = filter->outputs[i];
652 int best_idx = -1, best_score = INT_MIN;
654 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
655 outlink->in_formats->format_count < 2)
658 for (j = 0; j < outlink->in_formats->format_count; j++) {
659 int out_format = outlink->in_formats->formats[j];
660 int out_bps = av_get_bytes_per_sample(out_format);
663 if (av_get_packed_sample_fmt(out_format) == format ||
664 av_get_planar_sample_fmt(out_format) == format) {
669 /* for s32 and float prefer double to prevent loss of information */
670 if (bps == 4 && out_bps == 8) {
675 /* prefer closest higher or equal bps */
676 score = -abs(out_bps - bps);
680 if (score > best_score) {
685 av_assert0(best_idx >= 0);
686 FFSWAP(int, outlink->in_formats->formats[0],
687 outlink->in_formats->formats[best_idx]);
691 static void swap_sample_fmts(AVFilterGraph *graph)
695 for (i = 0; i < graph->filter_count; i++)
696 swap_sample_fmts_on_filter(graph->filters[i]);
700 static int pick_formats(AVFilterGraph *graph)
707 for (i = 0; i < graph->filter_count; i++) {
708 AVFilterContext *filter = graph->filters[i];
709 if (filter->nb_inputs){
710 for (j = 0; j < filter->nb_inputs; j++){
711 if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
712 pick_format(filter->inputs[j], NULL);
717 if (filter->nb_outputs){
718 for (j = 0; j < filter->nb_outputs; j++){
719 if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
720 pick_format(filter->outputs[j], NULL);
725 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
726 for (j = 0; j < filter->nb_outputs; j++) {
727 if(filter->outputs[j]->format<0) {
728 pick_format(filter->outputs[j], filter->inputs[0]);
736 for (i = 0; i < graph->filter_count; i++) {
737 AVFilterContext *filter = graph->filters[i];
739 for (j = 0; j < filter->nb_inputs; j++)
740 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
742 for (j = 0; j < filter->nb_outputs; j++)
743 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
750 * Configure the formats of all the links in the graph.
752 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
756 /* find supported formats from sub-filters, and merge along links */
757 if ((ret = query_formats(graph, log_ctx)) < 0)
760 /* Once everything is merged, it's possible that we'll still have
761 * multiple valid media format choices. We try to minimize the amount
762 * of format conversion inside filters */
763 reduce_formats(graph);
765 /* for audio filters, ensure the best format, sample rate and channel layout
767 swap_sample_fmts(graph);
768 swap_samplerates(graph);
769 swap_channel_layouts(graph);
771 if ((ret = pick_formats(graph)) < 0)
777 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
781 int sink_links_count = 0, n = 0;
783 AVFilterLink **sinks;
785 for (i = 0; i < graph->filter_count; i++) {
786 f = graph->filters[i];
787 for (j = 0; j < f->nb_inputs; j++) {
788 f->inputs[j]->graph = graph;
789 f->inputs[j]->age_index = -1;
791 for (j = 0; j < f->nb_outputs; j++) {
792 f->outputs[j]->graph = graph;
793 f->outputs[j]->age_index= -1;
795 if (!f->nb_outputs) {
796 if (f->nb_inputs > INT_MAX - sink_links_count)
797 return AVERROR(EINVAL);
798 sink_links_count += f->nb_inputs;
801 sinks = av_calloc(sink_links_count, sizeof(*sinks));
803 return AVERROR(ENOMEM);
804 for (i = 0; i < graph->filter_count; i++) {
805 f = graph->filters[i];
806 if (!f->nb_outputs) {
807 for (j = 0; j < f->nb_inputs; j++) {
808 sinks[n] = f->inputs[j];
809 f->inputs[j]->age_index = n++;
813 av_assert0(n == sink_links_count);
814 graph->sink_links = sinks;
815 graph->sink_links_count = sink_links_count;
819 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
825 for (i = 0; i < graph->filter_count; i++) {
826 f = graph->filters[i];
828 for (j = 0; j < f->nb_inputs; j++) {
829 AVFilterLink *link = f->inputs[j];
830 AVFilterContext *fifo_ctx;
834 if (!link->dstpad->needs_fifo)
837 fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
838 avfilter_get_by_name("fifo") :
839 avfilter_get_by_name("afifo");
841 snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
843 ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
848 ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
857 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
861 if ((ret = graph_check_validity(graphctx, log_ctx)))
863 if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
865 if ((ret = graph_config_formats(graphctx, log_ctx)))
867 if ((ret = graph_config_links(graphctx, log_ctx)))
869 if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
875 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
877 int i, r = AVERROR(ENOSYS);
882 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
883 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
884 if(r != AVERROR(ENOSYS))
891 for (i = 0; i < graph->filter_count; i++) {
892 AVFilterContext *filter = graph->filters[i];
893 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
894 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
895 if(r != AVERROR(ENOSYS)) {
896 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
905 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
912 for (i = 0; i < graph->filter_count; i++) {
913 AVFilterContext *filter = graph->filters[i];
914 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
915 AVFilterCommand **que = &filter->command_queue, *next;
916 while(*que && (*que)->time <= ts)
919 *que= av_mallocz(sizeof(AVFilterCommand));
920 (*que)->command = av_strdup(command);
921 (*que)->arg = av_strdup(arg);
923 (*que)->flags = flags;
925 if(flags & AVFILTER_CMD_FLAG_ONE)
933 static void heap_bubble_up(AVFilterGraph *graph,
934 AVFilterLink *link, int index)
936 AVFilterLink **links = graph->sink_links;
939 int parent = (index - 1) >> 1;
940 if (links[parent]->current_pts >= link->current_pts)
942 links[index] = links[parent];
943 links[index]->age_index = index;
947 link->age_index = index;
950 static void heap_bubble_down(AVFilterGraph *graph,
951 AVFilterLink *link, int index)
953 AVFilterLink **links = graph->sink_links;
956 int child = 2 * index + 1;
957 if (child >= graph->sink_links_count)
959 if (child + 1 < graph->sink_links_count &&
960 links[child + 1]->current_pts < links[child]->current_pts)
962 if (link->current_pts < links[child]->current_pts)
964 links[index] = links[child];
965 links[index]->age_index = index;
969 link->age_index = index;
972 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
974 heap_bubble_up (graph, link, link->age_index);
975 heap_bubble_down(graph, link, link->age_index);
979 int avfilter_graph_request_oldest(AVFilterGraph *graph)
981 while (graph->sink_links_count) {
982 AVFilterLink *oldest = graph->sink_links[0];
983 int r = ff_request_frame(oldest);
984 if (r != AVERROR_EOF)
986 /* EOF: remove the link from the heap */
987 if (oldest->age_index < --graph->sink_links_count)
988 heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
990 oldest->age_index = -1;