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"
34 #include "libavutil/audioconvert.h"
35 #include "libavutil/log.h"
37 static const AVClass filtergraph_class = {
38 .class_name = "AVFilterGraph",
39 .item_name = av_default_item_name,
40 .version = LIBAVUTIL_VERSION_INT,
43 AVFilterGraph *avfilter_graph_alloc(void)
45 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
48 #if FF_API_GRAPH_AVCLASS
49 ret->av_class = &filtergraph_class;
54 void avfilter_graph_free(AVFilterGraph **graph)
58 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
59 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
60 av_freep(&(*graph)->sink_links);
61 av_freep(&(*graph)->scale_sws_opts);
62 av_freep(&(*graph)->filters);
66 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
68 AVFilterContext **filters = av_realloc(graph->filters,
69 sizeof(AVFilterContext*) * (graph->filter_count+1));
71 return AVERROR(ENOMEM);
73 graph->filters = filters;
74 graph->filters[graph->filter_count++] = filter;
79 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
80 const char *name, const char *args, void *opaque,
81 AVFilterGraph *graph_ctx)
85 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
87 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
89 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
95 avfilter_free(*filt_ctx);
100 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
102 graph->disable_auto_convert = flags;
105 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
107 AVFilterContext *filt;
110 for (i = 0; i < graph->filter_count; i++) {
111 filt = graph->filters[i];
113 for (j = 0; j < filt->input_count; j++) {
114 if (!filt->inputs[j] || !filt->inputs[j]->src) {
115 av_log(log_ctx, AV_LOG_ERROR,
116 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
117 filt->input_pads[j].name, filt->name, filt->filter->name);
118 return AVERROR(EINVAL);
122 for (j = 0; j < filt->output_count; j++) {
123 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
124 av_log(log_ctx, AV_LOG_ERROR,
125 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
126 filt->output_pads[j].name, filt->name, filt->filter->name);
127 return AVERROR(EINVAL);
135 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
137 AVFilterContext *filt;
140 for (i=0; i < graph->filter_count; i++) {
141 filt = graph->filters[i];
143 if (!filt->output_count) {
144 if ((ret = avfilter_config_links(filt)))
152 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
156 for (i = 0; i < graph->filter_count; i++)
157 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
158 return graph->filters[i];
163 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
164 const char *filt_name, const char *filt_args)
166 static int auto_count = 0, ret;
168 AVFilterContext *filt_ctx;
170 if (graph->disable_auto_convert) {
171 av_log(NULL, AV_LOG_ERROR,
172 "The filters '%s' and '%s' do not have a common format "
173 "and automatic conversion is disabled.\n",
174 link->src->name, link->dst->name);
175 return AVERROR(EINVAL);
178 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
179 filt_name, auto_count++);
181 if ((ret = avfilter_graph_create_filter(&filt_ctx,
182 avfilter_get_by_name(filt_name),
183 inst_name, filt_args, NULL, graph)) < 0)
185 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
188 filt_ctx->filter->query_formats(filt_ctx);
190 if ( ((link = filt_ctx-> inputs[0]) &&
191 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
192 ((link = filt_ctx->outputs[0]) &&
193 !avfilter_merge_formats(link->in_formats, link->out_formats))
195 av_log(NULL, AV_LOG_ERROR,
196 "Impossible to convert between the formats supported by the filter "
197 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
198 return AVERROR(EINVAL);
201 if (link->type == AVMEDIA_TYPE_AUDIO &&
202 (((link = filt_ctx-> inputs[0]) &&
203 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
204 ((link = filt_ctx->outputs[0]) &&
205 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
207 av_log(NULL, AV_LOG_ERROR,
208 "Impossible to convert between the channel layouts formats supported by the filter "
209 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
210 return AVERROR(EINVAL);
216 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
220 AVFilterFormats *formats;
221 AVFilterChannelLayouts *chlayouts;
222 AVFilterFormats *samplerates;
223 int scaler_count = 0, resampler_count = 0;
225 /* ask all the sub-filters for their supported media formats */
226 for (i = 0; i < graph->filter_count; i++) {
227 if (graph->filters[i]->filter->query_formats)
228 graph->filters[i]->filter->query_formats(graph->filters[i]);
230 ff_default_query_formats(graph->filters[i]);
233 /* go through and merge as many format lists as possible */
234 for (i = 0; i < graph->filter_count; i++) {
235 AVFilterContext *filter = graph->filters[i];
237 for (j = 0; j < filter->input_count; j++) {
238 AVFilterLink *link = filter->inputs[j];
242 if (!link->in_formats || !link->out_formats)
243 return AVERROR(EINVAL);
245 if (link->type == AVMEDIA_TYPE_VIDEO &&
246 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
248 /* couldn't merge format lists, auto-insert scale filter */
249 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
250 graph->scale_sws_opts);
251 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
254 else if (link->type == AVMEDIA_TYPE_AUDIO) {
255 if (!link->in_channel_layouts || !link->out_channel_layouts)
256 return AVERROR(EINVAL);
258 /* Merge all three list before checking: that way, in all
259 * three categories, aconvert will use a common format
260 * whenever possible. */
261 formats = avfilter_merge_formats(link->in_formats, link->out_formats);
262 chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
263 samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
265 if (!formats || !chlayouts || !samplerates)
266 if (ret = insert_conv_filter(graph, link, "aresample", NULL))
269 int convert_needed = 0;
274 if (link->in_formats != link->out_formats &&
275 !avfilter_merge_formats(link->in_formats,
278 if (link->type == AVMEDIA_TYPE_AUDIO) {
279 if (link->in_channel_layouts != link->out_channel_layouts &&
280 !ff_merge_channel_layouts(link->in_channel_layouts,
281 link->out_channel_layouts))
283 if (link->in_samplerates != link->out_samplerates &&
284 !ff_merge_samplerates(link->in_samplerates,
285 link->out_samplerates))
289 if (convert_needed) {
290 AVFilterContext *convert;
292 AVFilterLink *inlink, *outlink;
293 char scale_args[256];
296 /* couldn't merge format lists. auto-insert conversion filter */
297 switch (link->type) {
298 case AVMEDIA_TYPE_VIDEO:
299 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
301 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
302 if ((ret = avfilter_graph_create_filter(&convert,
303 avfilter_get_by_name("scale"),
304 inst_name, scale_args, NULL,
308 case AVMEDIA_TYPE_AUDIO:
309 if (!(filter = avfilter_get_by_name("aresample"))) {
310 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
311 "not present, cannot convert audio formats.\n");
312 return AVERROR(EINVAL);
315 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
317 if ((ret = avfilter_graph_create_filter(&convert,
318 avfilter_get_by_name("aresample"),
319 inst_name, NULL, NULL, graph)) < 0)
323 return AVERROR(EINVAL);
326 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
329 convert->filter->query_formats(convert);
330 inlink = convert->inputs[0];
331 outlink = convert->outputs[0];
332 if (!avfilter_merge_formats( inlink->in_formats, inlink->out_formats) ||
333 !avfilter_merge_formats(outlink->in_formats, outlink->out_formats))
334 ret |= AVERROR(ENOSYS);
335 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
336 (!ff_merge_samplerates(inlink->in_samplerates,
337 inlink->out_samplerates) ||
338 !ff_merge_channel_layouts(inlink->in_channel_layouts,
339 inlink->out_channel_layouts)))
340 ret |= AVERROR(ENOSYS);
341 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
342 (!ff_merge_samplerates(outlink->in_samplerates,
343 outlink->out_samplerates) ||
344 !ff_merge_channel_layouts(outlink->in_channel_layouts,
345 outlink->out_channel_layouts)))
346 ret |= AVERROR(ENOSYS);
349 av_log(log_ctx, AV_LOG_ERROR,
350 "Impossible to convert between the formats supported by the filter "
351 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
362 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
364 if (!link || !link->in_formats)
367 if (link->type == AVMEDIA_TYPE_VIDEO) {
368 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
369 int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
370 enum PixelFormat best= PIX_FMT_NONE;
372 for (i=0; i<link->in_formats->format_count; i++) {
373 enum PixelFormat p = link->in_formats->formats[i];
374 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
376 link->in_formats->formats[0] = best;
380 link->in_formats->format_count = 1;
381 link->format = link->in_formats->formats[0];
383 if (link->type == AVMEDIA_TYPE_AUDIO) {
384 if (!link->in_samplerates->format_count) {
385 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
386 " the link between filters %s and %s.\n", link->src->name,
388 return AVERROR(EINVAL);
390 link->in_samplerates->format_count = 1;
391 link->sample_rate = link->in_samplerates->formats[0];
393 if (!link->in_channel_layouts->nb_channel_layouts) {
394 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
395 "the link between filters %s and %s.\n", link->src->name,
397 return AVERROR(EINVAL);
399 link->in_channel_layouts->nb_channel_layouts = 1;
400 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
403 avfilter_formats_unref(&link->in_formats);
404 avfilter_formats_unref(&link->out_formats);
405 avfilter_formats_unref(&link->in_samplerates);
406 avfilter_formats_unref(&link->out_samplerates);
407 ff_channel_layouts_unref(&link->in_channel_layouts);
408 ff_channel_layouts_unref(&link->out_channel_layouts);
413 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
415 for (i = 0; i < filter->input_count; i++) { \
416 AVFilterLink *link = filter->inputs[i]; \
419 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
421 fmt = link->out_ ## list->var[0]; \
423 for (j = 0; j < filter->output_count; j++) { \
424 AVFilterLink *out_link = filter->outputs[j]; \
427 if (link->type != out_link->type || \
428 out_link->in_ ## list->nb == 1) \
430 fmts = out_link->in_ ## list; \
432 if (!out_link->in_ ## list->nb) { \
433 add_format(&out_link->in_ ##list, fmt); \
437 for (k = 0; k < out_link->in_ ## list->nb; k++) \
438 if (fmts->var[k] == fmt) { \
439 fmts->var[0] = fmt; \
448 static int reduce_formats_on_filter(AVFilterContext *filter)
450 int i, j, k, ret = 0;
452 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
453 format_count, avfilter_add_format);
454 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
455 format_count, avfilter_add_format);
456 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
457 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
462 static void reduce_formats(AVFilterGraph *graph)
469 for (i = 0; i < graph->filter_count; i++)
470 reduced |= reduce_formats_on_filter(graph->filters[i]);
474 static void swap_samplerates_on_filter(AVFilterContext *filter)
476 AVFilterLink *link = NULL;
480 for (i = 0; i < filter->input_count; i++) {
481 link = filter->inputs[i];
483 if (link->type == AVMEDIA_TYPE_AUDIO &&
484 link->out_samplerates->format_count == 1)
487 if (i == filter->input_count)
490 sample_rate = link->out_samplerates->formats[0];
492 for (i = 0; i < filter->output_count; i++) {
493 AVFilterLink *outlink = filter->outputs[i];
494 int best_idx, best_diff = INT_MAX;
496 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
497 outlink->in_samplerates->format_count < 2)
500 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
501 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
503 if (diff < best_diff) {
508 FFSWAP(int, outlink->in_samplerates->formats[0],
509 outlink->in_samplerates->formats[best_idx]);
513 static void swap_samplerates(AVFilterGraph *graph)
517 for (i = 0; i < graph->filter_count; i++)
518 swap_samplerates_on_filter(graph->filters[i]);
521 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
523 AVFilterLink *link = NULL;
527 for (i = 0; i < filter->input_count; i++) {
528 link = filter->inputs[i];
530 if (link->type == AVMEDIA_TYPE_AUDIO &&
531 link->out_channel_layouts->nb_channel_layouts == 1)
534 if (i == filter->input_count)
537 chlayout = link->out_channel_layouts->channel_layouts[0];
539 for (i = 0; i < filter->output_count; i++) {
540 AVFilterLink *outlink = filter->outputs[i];
541 int best_idx, best_score = INT_MIN;
543 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
544 outlink->in_channel_layouts->nb_channel_layouts < 2)
547 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
548 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
549 int matched_channels = av_get_channel_layout_nb_channels(chlayout &
551 int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
553 int score = matched_channels - extra_channels;
555 if (score > best_score) {
560 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
561 outlink->in_channel_layouts->channel_layouts[best_idx]);
566 static void swap_channel_layouts(AVFilterGraph *graph)
570 for (i = 0; i < graph->filter_count; i++)
571 swap_channel_layouts_on_filter(graph->filters[i]);
574 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
576 AVFilterLink *link = NULL;
580 for (i = 0; i < filter->input_count; i++) {
581 link = filter->inputs[i];
583 if (link->type == AVMEDIA_TYPE_AUDIO &&
584 link->out_formats->format_count == 1)
587 if (i == filter->input_count)
590 format = link->out_formats->formats[0];
591 bps = av_get_bytes_per_sample(format);
593 for (i = 0; i < filter->output_count; i++) {
594 AVFilterLink *outlink = filter->outputs[i];
595 int best_idx, best_score = INT_MIN;
597 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
598 outlink->in_formats->format_count < 2)
601 for (j = 0; j < outlink->in_formats->format_count; j++) {
602 int out_format = outlink->in_formats->formats[j];
603 int out_bps = av_get_bytes_per_sample(out_format);
606 if (av_get_packed_sample_fmt(out_format) == format ||
607 av_get_planar_sample_fmt(out_format) == format) {
612 /* for s32 and float prefer double to prevent loss of information */
613 if (bps == 4 && out_bps == 8) {
618 /* prefer closest higher or equal bps */
619 score = -abs(out_bps - bps);
623 if (score > best_score) {
628 FFSWAP(int, outlink->in_formats->formats[0],
629 outlink->in_formats->formats[best_idx]);
633 static void swap_sample_fmts(AVFilterGraph *graph)
637 for (i = 0; i < graph->filter_count; i++)
638 swap_sample_fmts_on_filter(graph->filters[i]);
642 static int pick_formats(AVFilterGraph *graph)
649 for (i = 0; i < graph->filter_count; i++) {
650 AVFilterContext *filter = graph->filters[i];
651 if (filter->input_count){
652 for (j = 0; j < filter->input_count; j++){
653 if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
654 pick_format(filter->inputs[j], NULL);
659 if (filter->output_count){
660 for (j = 0; j < filter->output_count; j++){
661 if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
662 pick_format(filter->outputs[j], NULL);
667 if (filter->input_count && filter->output_count && filter->inputs[0]->format>=0) {
668 for (j = 0; j < filter->output_count; j++) {
669 if(filter->outputs[j]->format<0) {
670 pick_format(filter->outputs[j], filter->inputs[0]);
678 for (i = 0; i < graph->filter_count; i++) {
679 AVFilterContext *filter = graph->filters[i];
681 for (j = 0; j < filter->input_count; j++)
682 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
684 for (j = 0; j < filter->output_count; j++)
685 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
691 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
695 /* find supported formats from sub-filters, and merge along links */
696 if ((ret = query_formats(graph, log_ctx)) < 0)
699 /* Once everything is merged, it's possible that we'll still have
700 * multiple valid media format choices. We try to minimize the amount
701 * of format conversion inside filters */
702 reduce_formats(graph);
704 /* for audio filters, ensure the best format, sample rate and channel layout
706 swap_sample_fmts(graph);
707 swap_samplerates(graph);
708 swap_channel_layouts(graph);
710 if ((ret = pick_formats(graph)) < 0)
716 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
720 int sink_links_count = 0, n = 0;
722 AVFilterLink **sinks;
724 for (i = 0; i < graph->filter_count; i++) {
725 f = graph->filters[i];
726 for (j = 0; j < f->input_count; j++) {
727 f->inputs[j]->graph = graph;
728 f->inputs[j]->age_index = -1;
730 for (j = 0; j < f->output_count; j++) {
731 f->outputs[j]->graph = graph;
732 f->outputs[j]->age_index= -1;
734 if (!f->output_count) {
735 if (f->input_count > INT_MAX - sink_links_count)
736 return AVERROR(EINVAL);
737 sink_links_count += f->input_count;
740 sinks = av_calloc(sink_links_count, sizeof(*sinks));
742 return AVERROR(ENOMEM);
743 for (i = 0; i < graph->filter_count; i++) {
744 f = graph->filters[i];
745 if (!f->output_count) {
746 for (j = 0; j < f->input_count; j++) {
747 sinks[n] = f->inputs[j];
748 f->inputs[j]->age_index = n++;
752 av_assert0(n == sink_links_count);
753 graph->sink_links = sinks;
754 graph->sink_links_count = sink_links_count;
758 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
762 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
764 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
766 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
768 if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
774 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
776 int i, r = AVERROR(ENOSYS);
781 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
782 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
783 if(r != AVERROR(ENOSYS))
790 for (i = 0; i < graph->filter_count; i++) {
791 AVFilterContext *filter = graph->filters[i];
792 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
793 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
794 if(r != AVERROR(ENOSYS)) {
795 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
804 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
811 for (i = 0; i < graph->filter_count; i++) {
812 AVFilterContext *filter = graph->filters[i];
813 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
814 AVFilterCommand **que = &filter->command_queue, *next;
815 while(*que && (*que)->time <= ts)
818 *que= av_mallocz(sizeof(AVFilterCommand));
819 (*que)->command = av_strdup(command);
820 (*que)->arg = av_strdup(arg);
822 (*que)->flags = flags;
824 if(flags & AVFILTER_CMD_FLAG_ONE)
832 static void heap_bubble_up(AVFilterGraph *graph,
833 AVFilterLink *link, int index)
835 AVFilterLink **links = graph->sink_links;
838 int parent = (index - 1) >> 1;
839 if (links[parent]->current_pts >= link->current_pts)
841 links[index] = links[parent];
842 links[index]->age_index = index;
846 link->age_index = index;
849 static void heap_bubble_down(AVFilterGraph *graph,
850 AVFilterLink *link, int index)
852 AVFilterLink **links = graph->sink_links;
855 int child = 2 * index + 1;
856 if (child >= graph->sink_links_count)
858 if (child + 1 < graph->sink_links_count &&
859 links[child + 1]->current_pts < links[child]->current_pts)
861 if (link->current_pts < links[child]->current_pts)
863 links[index] = links[child];
864 links[index]->age_index = index;
868 link->age_index = index;
871 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
873 heap_bubble_up (graph, link, link->age_index);
874 heap_bubble_down(graph, link, link->age_index);
878 int avfilter_graph_request_oldest(AVFilterGraph *graph)
880 while (graph->sink_links_count) {
881 AVFilterLink *oldest = graph->sink_links[0];
882 int r = avfilter_request_frame(oldest);
883 if (r != AVERROR_EOF)
885 /* EOF: remove the link from the heap */
886 if (oldest->age_index < --graph->sink_links_count)
887 heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
889 oldest->age_index = -1;