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
27 #include "libavutil/avassert.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
37 #include "avfilter_internal.h"
38 #include "buffersink.h"
41 #include "framequeue.h"
44 #define OFFSET(x) offsetof(AVFilterGraph, x)
45 #define F AV_OPT_FLAG_FILTERING_PARAM
46 #define V AV_OPT_FLAG_VIDEO_PARAM
47 #define A AV_OPT_FLAG_AUDIO_PARAM
48 static const AVOption filtergraph_options
[] = {
49 { "thread_type", "Allowed thread types", OFFSET(thread_type
), AV_OPT_TYPE_FLAGS
,
50 { .i64
= AVFILTER_THREAD_SLICE
}, 0, INT_MAX
, F
|V
|A
, .unit
= "thread_type" },
51 { "slice", NULL
, 0, AV_OPT_TYPE_CONST
, { .i64
= AVFILTER_THREAD_SLICE
}, .flags
= F
|V
|A
, .unit
= "thread_type" },
52 { "threads", "Maximum number of threads", OFFSET(nb_threads
), AV_OPT_TYPE_INT
,
53 { .i64
= 0 }, 0, INT_MAX
, F
|V
|A
, .unit
= "threads"},
54 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST
, {.i64
= 0 }, .flags
= F
|V
|A
, .unit
= "threads"},
55 {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts
) ,
56 AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, F
|V
},
57 {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts
) ,
58 AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, F
|A
},
59 {"max_buffered_frames" , "maximum number of buffered frames allowed", OFFSET(max_buffered_frames
),
60 AV_OPT_TYPE_UINT
, {.i64
= 0}, 0, UINT_MAX
, F
|V
|A
},
64 static const AVClass filtergraph_class
= {
65 .class_name
= "AVFilterGraph",
66 .item_name
= av_default_item_name
,
67 .version
= LIBAVUTIL_VERSION_INT
,
68 .option
= filtergraph_options
,
69 .category
= AV_CLASS_CATEGORY_FILTER
,
73 void ff_graph_thread_free(FFFilterGraph
*graph
)
77 int ff_graph_thread_init(FFFilterGraph
*graph
)
79 graph
->p
.thread_type
= 0;
80 graph
->p
.nb_threads
= 1;
85 AVFilterGraph
*avfilter_graph_alloc(void)
87 FFFilterGraph
*graph
= av_mallocz(sizeof(*graph
));
94 ret
->av_class
= &filtergraph_class
;
95 av_opt_set_defaults(ret
);
96 ff_framequeue_global_init(&graph
->frame_queues
);
101 void ff_filter_graph_remove_filter(AVFilterGraph
*graph
, AVFilterContext
*filter
)
104 for (i
= 0; i
< graph
->nb_filters
; i
++) {
105 if (graph
->filters
[i
] == filter
) {
106 FFSWAP(AVFilterContext
*, graph
->filters
[i
],
107 graph
->filters
[graph
->nb_filters
- 1]);
109 filter
->graph
= NULL
;
110 for (j
= 0; j
<filter
->nb_outputs
; j
++)
111 if (filter
->outputs
[j
])
112 ff_filter_link(filter
->outputs
[j
])->graph
= NULL
;
119 void avfilter_graph_free(AVFilterGraph
**graphp
)
121 AVFilterGraph
*graph
= *graphp
;
122 FFFilterGraph
*graphi
= fffiltergraph(graph
);
127 while (graph
->nb_filters
)
128 avfilter_free(graph
->filters
[0]);
130 ff_graph_thread_free(graphi
);
132 av_freep(&graphi
->sink_links
);
136 av_freep(&graph
->filters
);
140 int avfilter_graph_create_filter(AVFilterContext
**filt_ctx
, const AVFilter
*filt
,
141 const char *name
, const char *args
, void *opaque
,
142 AVFilterGraph
*graph_ctx
)
146 *filt_ctx
= avfilter_graph_alloc_filter(graph_ctx
, filt
, name
);
148 return AVERROR(ENOMEM
);
150 ret
= avfilter_init_str(*filt_ctx
, args
);
157 avfilter_free(*filt_ctx
);
162 void avfilter_graph_set_auto_convert(AVFilterGraph
*graph
, unsigned flags
)
164 fffiltergraph(graph
)->disable_auto_convert
= flags
;
167 AVFilterContext
*avfilter_graph_alloc_filter(AVFilterGraph
*graph
,
168 const AVFilter
*filter
,
171 AVFilterContext
**filters
, *s
;
172 FFFilterGraph
*graphi
= fffiltergraph(graph
);
174 if (graph
->thread_type
&& !graphi
->thread_execute
) {
175 if (graph
->execute
) {
176 graphi
->thread_execute
= graph
->execute
;
178 int ret
= ff_graph_thread_init(graphi
);
180 av_log(graph
, AV_LOG_ERROR
, "Error initializing threading: %s.\n", av_err2str(ret
));
186 filters
= av_realloc_array(graph
->filters
, graph
->nb_filters
+ 1, sizeof(*filters
));
189 graph
->filters
= filters
;
191 s
= ff_filter_alloc(filter
, name
);
195 graph
->filters
[graph
->nb_filters
++] = s
;
203 * Check for the validity of graph.
205 * A graph is considered valid if all its input and output pads are
208 * @return >= 0 in case of success, a negative value otherwise
210 static int graph_check_validity(AVFilterGraph
*graph
, void *log_ctx
)
212 AVFilterContext
*filt
;
215 for (i
= 0; i
< graph
->nb_filters
; i
++) {
216 const AVFilterPad
*pad
;
217 filt
= graph
->filters
[i
];
219 for (j
= 0; j
< filt
->nb_inputs
; j
++) {
220 if (!filt
->inputs
[j
] || !filt
->inputs
[j
]->src
) {
221 pad
= &filt
->input_pads
[j
];
222 av_log(log_ctx
, AV_LOG_ERROR
,
223 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
224 pad
->name
, av_get_media_type_string(pad
->type
), filt
->name
, filt
->filter
->name
);
225 return AVERROR(EINVAL
);
229 for (j
= 0; j
< filt
->nb_outputs
; j
++) {
230 if (!filt
->outputs
[j
] || !filt
->outputs
[j
]->dst
) {
231 pad
= &filt
->output_pads
[j
];
232 av_log(log_ctx
, AV_LOG_ERROR
,
233 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
234 pad
->name
, av_get_media_type_string(pad
->type
), filt
->name
, filt
->filter
->name
);
235 return AVERROR(EINVAL
);
244 * Configure all the links of graphctx.
246 * @return >= 0 in case of success, a negative value otherwise
248 static int graph_config_links(AVFilterGraph
*graph
, void *log_ctx
)
250 AVFilterContext
*filt
;
253 for (i
= 0; i
< graph
->nb_filters
; i
++) {
254 filt
= graph
->filters
[i
];
256 if (!filt
->nb_outputs
) {
257 if ((ret
= ff_filter_config_links(filt
)))
265 static int graph_check_links(AVFilterGraph
*graph
, void *log_ctx
)
272 for (i
= 0; i
< graph
->nb_filters
; i
++) {
273 f
= graph
->filters
[i
];
274 for (j
= 0; j
< f
->nb_outputs
; j
++) {
276 if (l
->type
== AVMEDIA_TYPE_VIDEO
) {
277 ret
= av_image_check_size2(l
->w
, l
->h
, INT64_MAX
, l
->format
, 0, f
);
286 AVFilterContext
*avfilter_graph_get_filter(AVFilterGraph
*graph
, const char *name
)
290 for (i
= 0; i
< graph
->nb_filters
; i
++)
291 if (graph
->filters
[i
]->name
&& !strcmp(name
, graph
->filters
[i
]->name
))
292 return graph
->filters
[i
];
297 static int filter_link_check_formats(void *log
, AVFilterLink
*link
, AVFilterFormatsConfig
*cfg
)
301 switch (link
->type
) {
303 case AVMEDIA_TYPE_VIDEO
:
304 if ((ret
= ff_formats_check_pixel_formats(log
, cfg
->formats
)) < 0 ||
305 (ret
= ff_formats_check_color_spaces(log
, cfg
->color_spaces
)) < 0 ||
306 (ret
= ff_formats_check_color_ranges(log
, cfg
->color_ranges
)) < 0 ||
307 (ret
= ff_formats_check_alpha_modes(log
, cfg
->alpha_modes
)) < 0)
311 case AVMEDIA_TYPE_AUDIO
:
312 if ((ret
= ff_formats_check_sample_formats(log
, cfg
->formats
)) < 0 ||
313 (ret
= ff_formats_check_sample_rates(log
, cfg
->samplerates
)) < 0 ||
314 (ret
= ff_formats_check_channel_layouts(log
, cfg
->channel_layouts
)) < 0)
319 av_assert0(!"reached");
325 * Check the validity of the formats / etc. lists set by query_formats().
327 * In particular, check they do not contain any redundant element.
329 static int filter_check_formats(AVFilterContext
*ctx
)
334 for (i
= 0; i
< ctx
->nb_inputs
; i
++) {
335 ret
= filter_link_check_formats(ctx
, ctx
->inputs
[i
], &ctx
->inputs
[i
]->outcfg
);
339 for (i
= 0; i
< ctx
->nb_outputs
; i
++) {
340 ret
= filter_link_check_formats(ctx
, ctx
->outputs
[i
], &ctx
->outputs
[i
]->incfg
);
347 static int filter_query_formats(AVFilterContext
*ctx
)
349 const FFFilter
*const filter
= fffilter(ctx
->filter
);
352 if (filter
->formats_state
== FF_FILTER_FORMATS_QUERY_FUNC
) {
353 if ((ret
= filter
->formats
.query_func(ctx
)) < 0) {
354 if (ret
!= AVERROR(EAGAIN
))
355 av_log(ctx
, AV_LOG_ERROR
, "Query format failed for '%s': %s\n",
356 ctx
->name
, av_err2str(ret
));
359 } else if (filter
->formats_state
== FF_FILTER_FORMATS_QUERY_FUNC2
) {
360 AVFilterFormatsConfig
*cfg_in_stack
[64], *cfg_out_stack
[64];
361 AVFilterFormatsConfig
**cfg_in_dyn
= NULL
, **cfg_out_dyn
= NULL
;
362 AVFilterFormatsConfig
**cfg_in
, **cfg_out
;
364 if (ctx
->nb_inputs
> FF_ARRAY_ELEMS(cfg_in_stack
)) {
365 cfg_in_dyn
= av_malloc_array(ctx
->nb_inputs
, sizeof(*cfg_in_dyn
));
367 return AVERROR(ENOMEM
);
370 cfg_in
= ctx
->nb_inputs
? cfg_in_stack
: NULL
;
372 for (unsigned i
= 0; i
< ctx
->nb_inputs
; i
++) {
373 AVFilterLink
*l
= ctx
->inputs
[i
];
374 cfg_in
[i
] = &l
->outcfg
;
377 if (ctx
->nb_outputs
> FF_ARRAY_ELEMS(cfg_out_stack
)) {
378 cfg_out_dyn
= av_malloc_array(ctx
->nb_outputs
, sizeof(*cfg_out_dyn
));
380 av_freep(&cfg_in_dyn
);
381 return AVERROR(ENOMEM
);
383 cfg_out
= cfg_out_dyn
;
385 cfg_out
= ctx
->nb_outputs
? cfg_out_stack
: NULL
;
387 for (unsigned i
= 0; i
< ctx
->nb_outputs
; i
++) {
388 AVFilterLink
*l
= ctx
->outputs
[i
];
389 cfg_out
[i
] = &l
->incfg
;
392 ret
= filter
->formats
.query_func2(ctx
, cfg_in
, cfg_out
);
393 av_freep(&cfg_in_dyn
);
394 av_freep(&cfg_out_dyn
);
396 if (ret
!= AVERROR(EAGAIN
))
397 av_log(ctx
, AV_LOG_ERROR
, "Query format failed for '%s': %s\n",
398 ctx
->name
, av_err2str(ret
));
403 if (filter
->formats_state
== FF_FILTER_FORMATS_QUERY_FUNC
||
404 filter
->formats_state
== FF_FILTER_FORMATS_QUERY_FUNC2
) {
405 ret
= filter_check_formats(ctx
);
410 return ff_default_query_formats(ctx
);
413 static int formats_declared(AVFilterContext
*f
)
417 for (i
= 0; i
< f
->nb_inputs
; i
++) {
418 if (!f
->inputs
[i
]->outcfg
.formats
)
420 if (f
->inputs
[i
]->type
== AVMEDIA_TYPE_VIDEO
&&
421 !(f
->inputs
[i
]->outcfg
.color_ranges
&&
422 f
->inputs
[i
]->outcfg
.color_spaces
&&
423 f
->inputs
[i
]->outcfg
.alpha_modes
))
425 if (f
->inputs
[i
]->type
== AVMEDIA_TYPE_AUDIO
&&
426 !(f
->inputs
[i
]->outcfg
.samplerates
&&
427 f
->inputs
[i
]->outcfg
.channel_layouts
))
430 for (i
= 0; i
< f
->nb_outputs
; i
++) {
431 if (!f
->outputs
[i
]->incfg
.formats
)
433 if (f
->outputs
[i
]->type
== AVMEDIA_TYPE_VIDEO
&&
434 !(f
->outputs
[i
]->incfg
.color_ranges
&&
435 f
->outputs
[i
]->incfg
.color_spaces
&&
436 f
->outputs
[i
]->incfg
.alpha_modes
))
438 if (f
->outputs
[i
]->type
== AVMEDIA_TYPE_AUDIO
&&
439 !(f
->outputs
[i
]->incfg
.samplerates
&&
440 f
->outputs
[i
]->incfg
.channel_layouts
))
446 static void print_link_formats(void *log_ctx
, int level
, const AVFilterLink
*l
,
447 const AVFilterFormatsMerger
*mergers
[],
450 if (av_log_get_level() < level
)
454 av_bprint_init(&bp
, 0, AV_BPRINT_SIZE_UNLIMITED
);
456 av_log(log_ctx
, level
, "Link '%s.%s' -> '%s.%s':\n",
457 l
->src
->name
, l
->srcpad
->name
, l
->dst
->name
, l
->dstpad
->name
);
459 for (unsigned i
= 0; i
< nb_mergers
; i
++) {
460 const AVFilterFormatsMerger
*m
= mergers
[i
];
461 av_log(log_ctx
, level
, " %s:\n", m
->name
);
462 m
->print_list(&bp
, FF_FIELD_AT(void *, m
->offset
, l
->incfg
));
463 if (av_bprint_is_complete(&bp
))
464 av_log(log_ctx
, level
, " src: %s\n", bp
.str
);
465 av_bprint_clear(&bp
);
467 m
->print_list(&bp
, FF_FIELD_AT(void *, m
->offset
, l
->outcfg
));
468 if (av_bprint_is_complete(&bp
))
469 av_log(log_ctx
, level
, " dst: %s\n", bp
.str
);
470 av_bprint_clear(&bp
);
473 av_bprint_finalize(&bp
, NULL
);
476 static void print_filter_formats(void *log_ctx
, int level
, const AVFilterContext
*f
)
478 if (av_log_get_level() < level
)
482 av_bprint_init(&bp
, 0, AV_BPRINT_SIZE_UNLIMITED
);
484 av_log(log_ctx
, level
, "Filter '%s' formats:\n", f
->name
);
485 for (int i
= 0; i
< f
->nb_inputs
; i
++) {
486 const AVFilterLink
*in
= f
->inputs
[i
];
487 const AVFilterNegotiation
*neg
= ff_filter_get_negotiation(in
);
488 av_log(log_ctx
, level
, " in[%d] '%s':", i
, f
->input_pads
[i
].name
);
490 for (unsigned i
= 0; i
< neg
->nb_mergers
; i
++) {
491 const AVFilterFormatsMerger
*m
= &neg
->mergers
[i
];
492 m
->print_list(&bp
, FF_FIELD_AT(void *, m
->offset
, in
->outcfg
));
493 if (av_bprint_is_complete(&bp
))
494 av_log(log_ctx
, level
, " %s: %s", m
->name
, bp
.str
);
495 av_bprint_clear(&bp
);
499 for (int i
= 0; i
< f
->nb_outputs
; i
++) {
500 const AVFilterLink
*out
= f
->outputs
[i
];
501 const AVFilterNegotiation
*neg
= ff_filter_get_negotiation(out
);
502 av_log(log_ctx
, level
, " out[%d] '%s':", i
, f
->output_pads
[i
].name
);
504 for (unsigned i
= 0; i
< neg
->nb_mergers
; i
++) {
505 const AVFilterFormatsMerger
*m
= &neg
->mergers
[i
];
506 m
->print_list(&bp
, FF_FIELD_AT(void *, m
->offset
, out
->incfg
));
507 if (av_bprint_is_complete(&bp
))
508 av_log(log_ctx
, level
, " %s: %s", m
->name
, bp
.str
);
509 av_bprint_clear(&bp
);
513 av_bprint_finalize(&bp
, NULL
);
517 * Perform one round of query_formats() and merging formats lists on the
519 * @return >=0 if all links formats lists could be queried and merged;
520 * AVERROR(EAGAIN) some progress was made in the queries or merging
521 * and a later call may succeed;
522 * AVERROR(EIO) (may be changed) plus a log message if no progress
523 * was made and the negotiation is stuck;
524 * a negative error code if some other error happened
526 static int query_formats(AVFilterGraph
*graph
, void *log_ctx
)
529 int converter_count
= 0;
530 int count_queried
= 0; /* successful calls to query_formats() */
531 int count_merged
= 0; /* successful merge of formats lists */
532 int count_already_merged
= 0; /* lists already merged */
533 int count_delayed
= 0; /* lists that need to be merged later */
535 for (i
= 0; i
< graph
->nb_filters
; i
++) {
536 AVFilterContext
*f
= graph
->filters
[i
];
537 if (formats_declared(f
))
539 ret
= filter_query_formats(f
);
540 if (ret
< 0 && ret
!= AVERROR(EAGAIN
))
542 /* note: EAGAIN could indicate a partial success, not counted yet */
544 print_filter_formats(log_ctx
, AV_LOG_DEBUG
, f
);
549 /* go through and merge as many format lists as possible */
551 for (i
= 0; i
< graph
->nb_filters
; i
++) {
552 AVFilterContext
*filter
= graph
->filters
[i
];
554 for (j
= 0; j
< filter
->nb_inputs
; j
++) {
555 AVFilterLink
*link
= filter
->inputs
[j
];
556 const AVFilterNegotiation
*neg
;
557 AVFilterContext
*conv
[4];
558 const AVFilterFormatsMerger
*mergers
[4]; /* triggered mergers */
559 const char *conv_filters
[4], *conv_opts
[4] = {0};
560 unsigned neg_step
, num_conv
= 0, num_mergers
= 0;
565 neg
= ff_filter_get_negotiation(link
);
567 for (neg_step
= 0; neg_step
< neg
->nb_mergers
; neg_step
++) {
568 const AVFilterFormatsMerger
*m
= &neg
->mergers
[neg_step
];
569 void *a
= FF_FIELD_AT(void *, m
->offset
, link
->incfg
);
570 void *b
= FF_FIELD_AT(void *, m
->offset
, link
->outcfg
);
571 if (a
&& b
&& a
!= b
&& !m
->can_merge(a
, b
)) {
572 for (k
= 0; k
< num_conv
; k
++) {
573 if (!strcmp(conv_filters
[k
], m
->conversion_filter
))
577 av_assert1(num_conv
< FF_ARRAY_ELEMS(conv_filters
));
578 conv_filters
[num_conv
] = m
->conversion_filter
;
579 if (m
->conversion_opts_offset
)
580 conv_opts
[num_conv
] = FF_FIELD_AT(char *, m
->conversion_opts_offset
, *graph
);
583 av_assert1(num_mergers
< FF_ARRAY_ELEMS(mergers
));
584 mergers
[num_mergers
++] = m
;
587 for (neg_step
= 0; neg_step
< neg
->nb_mergers
; neg_step
++) {
588 const AVFilterFormatsMerger
*m
= &neg
->mergers
[neg_step
];
589 void *a
= FF_FIELD_AT(void *, m
->offset
, link
->incfg
);
590 void *b
= FF_FIELD_AT(void *, m
->offset
, link
->outcfg
);
594 count_already_merged
++;
595 } else if (!num_conv
) {
597 ret
= m
->merge(a
, b
);
601 mergers
[num_mergers
++] = m
;
602 conv_filters
[num_conv
] = m
->conversion_filter
;
603 if (m
->conversion_opts_offset
)
604 conv_opts
[num_conv
] = FF_FIELD_AT(char *, m
->conversion_opts_offset
, *graph
);
611 * Couldn't merge format lists; auto-insert conversion filters
612 * in reverse order to keep the order consistent with the list
613 * of mergers, since they are prepended onto the existing link
615 for (k
= num_conv
- 1; k
>= 0; k
--) {
616 const AVFilter
*filter
;
619 if (fffiltergraph(graph
)->disable_auto_convert
) {
620 av_log(log_ctx
, AV_LOG_ERROR
,
621 "The filters '%s' and '%s' do not have a common format "
622 "and automatic conversion is disabled.\n",
623 link
->src
->name
, link
->dst
->name
);
624 print_link_formats(log_ctx
, AV_LOG_ERROR
, link
, mergers
, num_mergers
);
625 return AVERROR(EINVAL
);
628 if (!(filter
= avfilter_get_by_name(conv_filters
[k
]))) {
629 av_log(log_ctx
, AV_LOG_ERROR
,
630 "'%s' filter not present, cannot convert formats.\n",
632 print_link_formats(log_ctx
, AV_LOG_ERROR
, link
, mergers
, num_mergers
);
633 return AVERROR(EINVAL
);
635 snprintf(inst_name
, sizeof(inst_name
), "auto_%s_%d",
636 conv_filters
[k
], converter_count
++);
637 ret
= avfilter_graph_create_filter(&conv
[k
], filter
, inst_name
,
638 conv_opts
[k
], NULL
, graph
);
641 if ((ret
= avfilter_insert_filter(link
, conv
[k
], 0, 0)) < 0)
644 if ((ret
= filter_query_formats(conv
[k
])) < 0)
648 /* preemptively settle formats of auto filters */
649 for (k
= 0; k
< num_conv
; k
++) {
650 AVFilterLink
*inlink
= conv
[k
]->inputs
[0];
651 AVFilterLink
*outlink
= conv
[k
]->outputs
[0];
652 av_assert0( inlink
->incfg
.formats
->refcount
> 0);
653 av_assert0( inlink
->outcfg
.formats
->refcount
> 0);
654 av_assert0(outlink
->incfg
.formats
->refcount
> 0);
655 av_assert0(outlink
->outcfg
.formats
->refcount
> 0);
656 if (outlink
->type
== AVMEDIA_TYPE_VIDEO
) {
657 av_assert0( inlink
-> incfg
.color_spaces
->refcount
> 0);
658 av_assert0( inlink
->outcfg
.color_spaces
->refcount
> 0);
659 av_assert0(outlink
-> incfg
.color_spaces
->refcount
> 0);
660 av_assert0(outlink
->outcfg
.color_spaces
->refcount
> 0);
661 av_assert0( inlink
-> incfg
.color_ranges
->refcount
> 0);
662 av_assert0( inlink
->outcfg
.color_ranges
->refcount
> 0);
663 av_assert0(outlink
-> incfg
.color_ranges
->refcount
> 0);
664 av_assert0(outlink
->outcfg
.color_ranges
->refcount
> 0);
665 av_assert0( inlink
-> incfg
.alpha_modes
->refcount
> 0);
666 av_assert0( inlink
->outcfg
.alpha_modes
->refcount
> 0);
667 av_assert0(outlink
-> incfg
.alpha_modes
->refcount
> 0);
668 av_assert0(outlink
->outcfg
.alpha_modes
->refcount
> 0);
669 } else if (outlink
->type
== AVMEDIA_TYPE_AUDIO
) {
670 av_assert0( inlink
-> incfg
.samplerates
->refcount
> 0);
671 av_assert0( inlink
->outcfg
.samplerates
->refcount
> 0);
672 av_assert0(outlink
-> incfg
.samplerates
->refcount
> 0);
673 av_assert0(outlink
->outcfg
.samplerates
->refcount
> 0);
674 av_assert0( inlink
-> incfg
.channel_layouts
->refcount
> 0);
675 av_assert0( inlink
->outcfg
.channel_layouts
->refcount
> 0);
676 av_assert0(outlink
-> incfg
.channel_layouts
->refcount
> 0);
677 av_assert0(outlink
->outcfg
.channel_layouts
->refcount
> 0);
680 #define MERGE(merger, link) \
681 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
682 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
684 for (neg_step
= 0; neg_step
< neg
->nb_mergers
; neg_step
++) {
685 const AVFilterFormatsMerger
*m
= &neg
->mergers
[neg_step
];
686 if (strcmp(m
->conversion_filter
, conv_filters
[k
]))
688 if ((ret
= MERGE(m
, inlink
)) <= 0 ||
689 (ret
= MERGE(m
, outlink
)) <= 0) {
692 av_log(log_ctx
, AV_LOG_ERROR
,
693 "Impossible to convert between the formats supported by the filter "
694 "'%s' and the filter '%s'\n", link
->src
->name
, link
->dst
->name
);
695 print_link_formats(log_ctx
, AV_LOG_ERROR
, inlink
, &m
, 1);
696 print_link_formats(log_ctx
, AV_LOG_ERROR
, outlink
, &m
, 1);
697 return AVERROR(ENOSYS
);
704 /* if there is more than one auto filter, we may need another round
705 * to fully settle formats due to possible cross-incompatibilities
706 * between the auto filters themselves */
712 av_log(graph
, AV_LOG_DEBUG
, "query_formats: "
713 "%d queried, %d merged, %d already done, %d delayed\n",
714 count_queried
, count_merged
, count_already_merged
, count_delayed
);
718 /* if count_queried > 0, one filter at least did set its formats,
719 that will give additional information to its neighbour;
720 if count_merged > 0, one pair of formats lists at least was merged,
721 that will give additional information to all connected filters;
722 in both cases, progress was made and a new round must be done */
723 if (count_queried
|| count_merged
)
724 return AVERROR(EAGAIN
);
725 av_bprint_init(&bp
, 0, AV_BPRINT_SIZE_AUTOMATIC
);
726 for (i
= 0; i
< graph
->nb_filters
; i
++)
727 if (!formats_declared(graph
->filters
[i
]))
728 av_bprintf(&bp
, "%s%s", bp
.len
? ", " : "",
729 graph
->filters
[i
]->name
);
730 av_log(graph
, AV_LOG_ERROR
,
731 "The following filters could not choose their formats: %s\n"
732 "Consider inserting the (a)format filter near their input or "
733 "output.\n", bp
.str
);
739 static int get_fmt_score(enum AVSampleFormat dst_fmt
, enum AVSampleFormat src_fmt
)
743 if (av_sample_fmt_is_planar(dst_fmt
) != av_sample_fmt_is_planar(src_fmt
))
746 if (av_get_bytes_per_sample(dst_fmt
) < av_get_bytes_per_sample(src_fmt
)) {
747 score
+= 100 * (av_get_bytes_per_sample(src_fmt
) - av_get_bytes_per_sample(dst_fmt
));
749 score
+= 10 * (av_get_bytes_per_sample(dst_fmt
) - av_get_bytes_per_sample(src_fmt
));
751 if (av_get_packed_sample_fmt(dst_fmt
) == AV_SAMPLE_FMT_S32
&&
752 av_get_packed_sample_fmt(src_fmt
) == AV_SAMPLE_FMT_FLT
)
755 if (av_get_packed_sample_fmt(dst_fmt
) == AV_SAMPLE_FMT_FLT
&&
756 av_get_packed_sample_fmt(src_fmt
) == AV_SAMPLE_FMT_S32
)
762 static enum AVSampleFormat
find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1
, enum AVSampleFormat dst_fmt2
,
763 enum AVSampleFormat src_fmt
)
767 score1
= get_fmt_score(dst_fmt1
, src_fmt
);
768 score2
= get_fmt_score(dst_fmt2
, src_fmt
);
770 return score1
< score2
? dst_fmt1
: dst_fmt2
;
773 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt
)
775 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(fmt
);
778 if (desc
->nb_components
< 3)
779 return 0; /* Grayscale is explicitly full-range in swscale */
780 av_assert1(!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
));
781 return !(desc
->flags
& (AV_PIX_FMT_FLAG_RGB
| AV_PIX_FMT_FLAG_PAL
|
782 AV_PIX_FMT_FLAG_XYZ
| AV_PIX_FMT_FLAG_FLOAT
));
786 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt
)
789 case AV_PIX_FMT_YUVJ420P
:
790 case AV_PIX_FMT_YUVJ422P
:
791 case AV_PIX_FMT_YUVJ444P
:
792 case AV_PIX_FMT_YUVJ440P
:
793 case AV_PIX_FMT_YUVJ411P
:
800 static int pick_format(AVFilterLink
*link
, AVFilterLink
*ref
)
802 if (!link
|| !link
->incfg
.formats
)
805 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
806 if(ref
&& ref
->type
== AVMEDIA_TYPE_VIDEO
){
807 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
808 int has_alpha
= av_pix_fmt_desc_get(ref
->format
)->nb_components
% 2 == 0;
809 enum AVPixelFormat best
= AV_PIX_FMT_NONE
;
811 for (i
= 0; i
< link
->incfg
.formats
->nb_formats
; i
++) {
812 enum AVPixelFormat p
= link
->incfg
.formats
->formats
[i
];
813 best
= av_find_best_pix_fmt_of_2(best
, p
, ref
->format
, has_alpha
, NULL
);
815 av_log(link
->src
,AV_LOG_DEBUG
, "picking %s out of %d ref:%s alpha:%d\n",
816 av_get_pix_fmt_name(best
), link
->incfg
.formats
->nb_formats
,
817 av_get_pix_fmt_name(ref
->format
), has_alpha
);
818 link
->incfg
.formats
->formats
[0] = best
;
820 } else if (link
->type
== AVMEDIA_TYPE_AUDIO
) {
821 if(ref
&& ref
->type
== AVMEDIA_TYPE_AUDIO
){
822 enum AVSampleFormat best
= AV_SAMPLE_FMT_NONE
;
824 for (i
= 0; i
< link
->incfg
.formats
->nb_formats
; i
++) {
825 enum AVSampleFormat p
= link
->incfg
.formats
->formats
[i
];
826 best
= find_best_sample_fmt_of_2(best
, p
, ref
->format
);
828 av_log(link
->src
,AV_LOG_DEBUG
, "picking %s out of %d ref:%s\n",
829 av_get_sample_fmt_name(best
), link
->incfg
.formats
->nb_formats
,
830 av_get_sample_fmt_name(ref
->format
));
831 link
->incfg
.formats
->formats
[0] = best
;
835 link
->incfg
.formats
->nb_formats
= 1;
836 link
->format
= link
->incfg
.formats
->formats
[0];
838 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
839 enum AVPixelFormat swfmt
= link
->format
;
840 if (av_pix_fmt_desc_get(swfmt
)->flags
& AV_PIX_FMT_FLAG_HWACCEL
) {
841 // FIXME: this is a hack - we'd like to use the sw_format of
842 // link->hw_frames_ctx here, but it is not yet available.
843 // To make this work properly we will need to either reorder
844 // things so that it is available here or somehow negotiate
845 // sw_format separately.
846 swfmt
= AV_PIX_FMT_YUV420P
;
849 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(swfmt
);
850 if (!ff_fmt_is_regular_yuv(swfmt
)) {
851 /* These fields are explicitly documented as affecting YUV only,
852 * so set them to sane values for other formats. */
853 if (desc
->flags
& AV_PIX_FMT_FLAG_FLOAT
)
854 link
->color_range
= AVCOL_RANGE_UNSPECIFIED
;
856 link
->color_range
= AVCOL_RANGE_JPEG
;
857 if (desc
->flags
& (AV_PIX_FMT_FLAG_RGB
| AV_PIX_FMT_FLAG_XYZ
)) {
858 link
->colorspace
= AVCOL_SPC_RGB
;
860 link
->colorspace
= AVCOL_SPC_UNSPECIFIED
;
863 if (!link
->incfg
.color_spaces
->nb_formats
) {
864 av_log(link
->src
, AV_LOG_ERROR
, "Cannot select color space for"
865 " the link between filters %s and %s.\n", link
->src
->name
,
867 return AVERROR(EINVAL
);
869 link
->incfg
.color_spaces
->nb_formats
= 1;
870 link
->colorspace
= link
->incfg
.color_spaces
->formats
[0];
872 if (ff_fmt_is_forced_full_range(swfmt
)) {
873 link
->color_range
= AVCOL_RANGE_JPEG
;
875 if (!link
->incfg
.color_ranges
->nb_formats
) {
876 av_log(link
->src
, AV_LOG_ERROR
, "Cannot select color range for"
877 " the link between filters %s and %s.\n", link
->src
->name
,
879 return AVERROR(EINVAL
);
881 link
->incfg
.color_ranges
->nb_formats
= 1;
882 link
->color_range
= link
->incfg
.color_ranges
->formats
[0];
886 if (desc
->flags
& AV_PIX_FMT_FLAG_ALPHA
) {
887 if (!link
->incfg
.alpha_modes
->nb_formats
) {
888 av_log(link
->src
, AV_LOG_ERROR
, "Cannot select alpha mode for"
889 " the link between filters %s and %s.\n", link
->src
->name
,
891 return AVERROR(EINVAL
);
893 link
->incfg
.alpha_modes
->nb_formats
= 1;
894 link
->alpha_mode
= link
->incfg
.alpha_modes
->formats
[0];
896 link
->alpha_mode
= AVALPHA_MODE_UNSPECIFIED
;
898 } else if (link
->type
== AVMEDIA_TYPE_AUDIO
) {
901 if (!link
->incfg
.samplerates
->nb_formats
) {
902 av_log(link
->src
, AV_LOG_ERROR
, "Cannot select sample rate for"
903 " the link between filters %s and %s.\n", link
->src
->name
,
905 return AVERROR(EINVAL
);
907 link
->incfg
.samplerates
->nb_formats
= 1;
908 link
->sample_rate
= link
->incfg
.samplerates
->formats
[0];
910 if (link
->incfg
.channel_layouts
->all_layouts
) {
911 av_log(link
->src
, AV_LOG_ERROR
, "Cannot select channel layout for"
912 " the link between filters %s and %s.\n", link
->src
->name
,
914 if (!link
->incfg
.channel_layouts
->all_counts
)
915 av_log(link
->src
, AV_LOG_ERROR
, "Unknown channel layouts not "
916 "supported, try specifying a channel layout using "
917 "'aformat=channel_layouts=something'.\n");
918 return AVERROR(EINVAL
);
920 link
->incfg
.channel_layouts
->nb_channel_layouts
= 1;
921 ret
= av_channel_layout_copy(&link
->ch_layout
, &link
->incfg
.channel_layouts
->channel_layouts
[0]);
926 ff_formats_unref(&link
->incfg
.formats
);
927 ff_formats_unref(&link
->outcfg
.formats
);
928 ff_formats_unref(&link
->incfg
.samplerates
);
929 ff_formats_unref(&link
->outcfg
.samplerates
);
930 ff_channel_layouts_unref(&link
->incfg
.channel_layouts
);
931 ff_channel_layouts_unref(&link
->outcfg
.channel_layouts
);
932 ff_formats_unref(&link
->incfg
.color_spaces
);
933 ff_formats_unref(&link
->outcfg
.color_spaces
);
934 ff_formats_unref(&link
->incfg
.color_ranges
);
935 ff_formats_unref(&link
->outcfg
.color_ranges
);
936 ff_formats_unref(&link
->incfg
.alpha_modes
);
937 ff_formats_unref(&link
->outcfg
.alpha_modes
);
942 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
944 for (i = 0; i < filter->nb_inputs; i++) { \
945 AVFilterLink *link = filter->inputs[i]; \
948 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
950 fmt = link->outcfg.list->var[0]; \
952 for (j = 0; j < filter->nb_outputs; j++) { \
953 AVFilterLink *out_link = filter->outputs[j]; \
956 if (link->type != out_link->type || \
957 out_link->incfg.list->nb == 1) \
959 fmts = out_link->incfg.list; \
961 if (!out_link->incfg.list->nb) { \
962 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
968 for (k = 0; k < out_link->incfg.list->nb; k++) \
969 if (fmts->var[k] == fmt) { \
970 fmts->var[0] = fmt; \
979 static int reduce_formats_on_filter(AVFilterContext
*filter
)
981 int i
, j
, k
, ret
= 0;
983 REDUCE_FORMATS(int, AVFilterFormats
, formats
, formats
,
984 nb_formats
, ff_add_format
);
985 REDUCE_FORMATS(int, AVFilterFormats
, samplerates
, formats
,
986 nb_formats
, ff_add_format
);
987 REDUCE_FORMATS(int, AVFilterFormats
, color_spaces
, formats
,
988 nb_formats
, ff_add_format
);
989 REDUCE_FORMATS(int, AVFilterFormats
, color_ranges
, formats
,
990 nb_formats
, ff_add_format
);
991 REDUCE_FORMATS(int, AVFilterFormats
, alpha_modes
, formats
,
992 nb_formats
, ff_add_format
);
994 /* reduce channel layouts */
995 for (i
= 0; i
< filter
->nb_inputs
; i
++) {
996 AVFilterLink
*inlink
= filter
->inputs
[i
];
997 const AVChannelLayout
*fmt
;
999 if (!inlink
->outcfg
.channel_layouts
||
1000 inlink
->outcfg
.channel_layouts
->nb_channel_layouts
!= 1)
1002 fmt
= &inlink
->outcfg
.channel_layouts
->channel_layouts
[0];
1004 for (j
= 0; j
< filter
->nb_outputs
; j
++) {
1005 AVFilterLink
*outlink
= filter
->outputs
[j
];
1006 AVFilterChannelLayouts
*fmts
;
1008 fmts
= outlink
->incfg
.channel_layouts
;
1009 if (inlink
->type
!= outlink
->type
|| fmts
->nb_channel_layouts
== 1)
1012 if (fmts
->all_layouts
&&
1013 (KNOWN(fmt
) || fmts
->all_counts
)) {
1014 /* Turn the infinite list into a singleton */
1015 fmts
->all_layouts
= fmts
->all_counts
= 0;
1016 ret
= ff_add_channel_layout(&outlink
->incfg
.channel_layouts
, fmt
);
1023 for (k
= 0; k
< outlink
->incfg
.channel_layouts
->nb_channel_layouts
; k
++) {
1024 if (!av_channel_layout_compare(&fmts
->channel_layouts
[k
], fmt
)) {
1025 ret
= av_channel_layout_copy(&fmts
->channel_layouts
[0], fmt
);
1028 fmts
->nb_channel_layouts
= 1;
1039 static int reduce_formats(AVFilterGraph
*graph
)
1041 int i
, reduced
, ret
;
1046 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1047 if ((ret
= reduce_formats_on_filter(graph
->filters
[i
])) < 0)
1056 static void swap_samplerates_on_filter(AVFilterContext
*filter
)
1058 AVFilterLink
*link
= NULL
;
1062 for (i
= 0; i
< filter
->nb_inputs
; i
++) {
1063 link
= filter
->inputs
[i
];
1065 if (link
->type
== AVMEDIA_TYPE_AUDIO
&&
1066 link
->outcfg
.samplerates
->nb_formats
== 1)
1069 if (i
== filter
->nb_inputs
)
1072 sample_rate
= link
->outcfg
.samplerates
->formats
[0];
1074 for (i
= 0; i
< filter
->nb_outputs
; i
++) {
1075 AVFilterLink
*outlink
= filter
->outputs
[i
];
1076 int best_idx
, best_diff
= INT_MAX
;
1078 if (outlink
->type
!= AVMEDIA_TYPE_AUDIO
||
1079 outlink
->incfg
.samplerates
->nb_formats
< 2)
1082 for (j
= 0; j
< outlink
->incfg
.samplerates
->nb_formats
; j
++) {
1083 int diff
= abs(sample_rate
- outlink
->incfg
.samplerates
->formats
[j
]);
1085 av_assert0(diff
< INT_MAX
); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
1087 if (diff
< best_diff
) {
1092 FFSWAP(int, outlink
->incfg
.samplerates
->formats
[0],
1093 outlink
->incfg
.samplerates
->formats
[best_idx
]);
1097 static void swap_samplerates(AVFilterGraph
*graph
)
1101 for (i
= 0; i
< graph
->nb_filters
; i
++)
1102 swap_samplerates_on_filter(graph
->filters
[i
]);
1105 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
1106 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
1107 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
1108 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
1109 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
1110 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
1111 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
1113 /* allowable substitutions for channel pairs when comparing layouts,
1114 * ordered by priority for both values */
1115 static const uint64_t ch_subst
[][2] = {
1116 { CH_FRONT_PAIR
, CH_CENTER_PAIR
},
1117 { CH_FRONT_PAIR
, CH_WIDE_PAIR
},
1118 { CH_FRONT_PAIR
, AV_CH_FRONT_CENTER
},
1119 { CH_CENTER_PAIR
, CH_FRONT_PAIR
},
1120 { CH_CENTER_PAIR
, CH_WIDE_PAIR
},
1121 { CH_CENTER_PAIR
, AV_CH_FRONT_CENTER
},
1122 { CH_WIDE_PAIR
, CH_FRONT_PAIR
},
1123 { CH_WIDE_PAIR
, CH_CENTER_PAIR
},
1124 { CH_WIDE_PAIR
, AV_CH_FRONT_CENTER
},
1125 { AV_CH_FRONT_CENTER
, CH_FRONT_PAIR
},
1126 { AV_CH_FRONT_CENTER
, CH_CENTER_PAIR
},
1127 { AV_CH_FRONT_CENTER
, CH_WIDE_PAIR
},
1128 { CH_SIDE_PAIR
, CH_DIRECT_PAIR
},
1129 { CH_SIDE_PAIR
, CH_BACK_PAIR
},
1130 { CH_SIDE_PAIR
, AV_CH_BACK_CENTER
},
1131 { CH_BACK_PAIR
, CH_DIRECT_PAIR
},
1132 { CH_BACK_PAIR
, CH_SIDE_PAIR
},
1133 { CH_BACK_PAIR
, AV_CH_BACK_CENTER
},
1134 { AV_CH_BACK_CENTER
, CH_BACK_PAIR
},
1135 { AV_CH_BACK_CENTER
, CH_DIRECT_PAIR
},
1136 { AV_CH_BACK_CENTER
, CH_SIDE_PAIR
},
1139 static void swap_channel_layouts_on_filter(AVFilterContext
*filter
)
1141 AVFilterLink
*link
= NULL
;
1144 for (i
= 0; i
< filter
->nb_inputs
; i
++) {
1145 link
= filter
->inputs
[i
];
1147 if (link
->type
== AVMEDIA_TYPE_AUDIO
&&
1148 link
->outcfg
.channel_layouts
->nb_channel_layouts
== 1)
1151 if (i
== filter
->nb_inputs
)
1154 for (i
= 0; i
< filter
->nb_outputs
; i
++) {
1155 AVFilterLink
*outlink
= filter
->outputs
[i
];
1156 int best_idx
= -1, best_score
= INT_MIN
, best_count_diff
= INT_MAX
;
1158 if (outlink
->type
!= AVMEDIA_TYPE_AUDIO
||
1159 outlink
->incfg
.channel_layouts
->nb_channel_layouts
< 2)
1162 for (j
= 0; j
< outlink
->incfg
.channel_layouts
->nb_channel_layouts
; j
++) {
1163 AVChannelLayout in_chlayout
= { 0 }, out_chlayout
= { 0 };
1167 int matched_channels
, extra_channels
;
1170 av_channel_layout_copy(&in_chlayout
, &link
->outcfg
.channel_layouts
->channel_layouts
[0]);
1171 av_channel_layout_copy(&out_chlayout
, &outlink
->incfg
.channel_layouts
->channel_layouts
[j
]);
1172 in_channels
= in_chlayout
.nb_channels
;
1173 out_channels
= out_chlayout
.nb_channels
;
1174 count_diff
= out_channels
- in_channels
;
1175 if (!KNOWN(&in_chlayout
) || !KNOWN(&out_chlayout
)) {
1176 /* Compute score in case the input or output layout encodes
1177 a channel count; in this case the score is not altered by
1178 the computation afterwards, as in_chlayout and
1179 out_chlayout have both been set to 0 */
1180 if (!KNOWN(&in_chlayout
))
1181 in_channels
= FF_LAYOUT2COUNT(&in_chlayout
);
1182 if (!KNOWN(&out_chlayout
))
1183 out_channels
= FF_LAYOUT2COUNT(&out_chlayout
);
1184 score
-= 10000 + FFABS(out_channels
- in_channels
) +
1185 (in_channels
> out_channels
? 10000 : 0);
1186 av_channel_layout_uninit(&in_chlayout
);
1187 av_channel_layout_uninit(&out_chlayout
);
1188 /* Let the remaining computation run, even if the score
1189 value is not altered */
1192 /* channel substitution */
1193 for (k
= 0; k
< FF_ARRAY_ELEMS(ch_subst
); k
++) {
1194 uint64_t cmp0
= ch_subst
[k
][0];
1195 uint64_t cmp1
= ch_subst
[k
][1];
1196 if ( av_channel_layout_subset(& in_chlayout
, cmp0
) &&
1197 !av_channel_layout_subset(&out_chlayout
, cmp0
) &&
1198 av_channel_layout_subset(&out_chlayout
, cmp1
) &&
1199 !av_channel_layout_subset(& in_chlayout
, cmp1
)) {
1200 av_channel_layout_from_mask(&in_chlayout
, av_channel_layout_subset(& in_chlayout
, ~cmp0
));
1201 av_channel_layout_from_mask(&out_chlayout
, av_channel_layout_subset(&out_chlayout
, ~cmp1
));
1202 /* add score for channel match, minus a deduction for
1203 having to do the substitution */
1204 score
+= 10 * av_popcount64(cmp1
) - 2;
1208 /* no penalty for LFE channel mismatch */
1209 if (av_channel_layout_index_from_channel(&in_chlayout
, AV_CHAN_LOW_FREQUENCY
) >= 0 &&
1210 av_channel_layout_index_from_channel(&out_chlayout
, AV_CHAN_LOW_FREQUENCY
) >= 0)
1212 av_channel_layout_from_mask(&in_chlayout
, av_channel_layout_subset(&in_chlayout
, ~AV_CH_LOW_FREQUENCY
));
1213 av_channel_layout_from_mask(&out_chlayout
, av_channel_layout_subset(&out_chlayout
, ~AV_CH_LOW_FREQUENCY
));
1215 matched_channels
= av_popcount64(in_chlayout
.u
.mask
& out_chlayout
.u
.mask
);
1216 extra_channels
= av_popcount64(out_chlayout
.u
.mask
& (~in_chlayout
.u
.mask
));
1217 score
+= 10 * matched_channels
- 5 * extra_channels
;
1219 if (score
> best_score
||
1220 (count_diff
< best_count_diff
&& score
== best_score
)) {
1223 best_count_diff
= count_diff
;
1226 av_assert0(best_idx
>= 0);
1227 FFSWAP(AVChannelLayout
, outlink
->incfg
.channel_layouts
->channel_layouts
[0],
1228 outlink
->incfg
.channel_layouts
->channel_layouts
[best_idx
]);
1233 static void swap_channel_layouts(AVFilterGraph
*graph
)
1237 for (i
= 0; i
< graph
->nb_filters
; i
++)
1238 swap_channel_layouts_on_filter(graph
->filters
[i
]);
1241 static void swap_sample_fmts_on_filter(AVFilterContext
*filter
)
1243 AVFilterLink
*link
= NULL
;
1247 for (i
= 0; i
< filter
->nb_inputs
; i
++) {
1248 link
= filter
->inputs
[i
];
1250 if (link
->type
== AVMEDIA_TYPE_AUDIO
&&
1251 link
->outcfg
.formats
->nb_formats
== 1)
1254 if (i
== filter
->nb_inputs
)
1257 format
= link
->outcfg
.formats
->formats
[0];
1258 bps
= av_get_bytes_per_sample(format
);
1260 for (i
= 0; i
< filter
->nb_outputs
; i
++) {
1261 AVFilterLink
*outlink
= filter
->outputs
[i
];
1262 int best_idx
= -1, best_score
= INT_MIN
;
1264 if (outlink
->type
!= AVMEDIA_TYPE_AUDIO
||
1265 outlink
->incfg
.formats
->nb_formats
< 2)
1268 for (j
= 0; j
< outlink
->incfg
.formats
->nb_formats
; j
++) {
1269 int out_format
= outlink
->incfg
.formats
->formats
[j
];
1270 int out_bps
= av_get_bytes_per_sample(out_format
);
1273 if (av_get_packed_sample_fmt(out_format
) == format
||
1274 av_get_planar_sample_fmt(out_format
) == format
) {
1279 /* for s32 and float prefer double to prevent loss of information */
1280 if (bps
== 4 && out_bps
== 8) {
1285 /* prefer closest higher or equal bps */
1286 score
= -abs(out_bps
- bps
);
1290 if (score
> best_score
) {
1295 av_assert0(best_idx
>= 0);
1296 FFSWAP(int, outlink
->incfg
.formats
->formats
[0],
1297 outlink
->incfg
.formats
->formats
[best_idx
]);
1301 static void swap_sample_fmts(AVFilterGraph
*graph
)
1305 for (i
= 0; i
< graph
->nb_filters
; i
++)
1306 swap_sample_fmts_on_filter(graph
->filters
[i
]);
1310 static int pick_formats(AVFilterGraph
*graph
)
1317 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1318 AVFilterContext
*filter
= graph
->filters
[i
];
1319 if (filter
->nb_inputs
){
1320 for (j
= 0; j
< filter
->nb_inputs
; j
++){
1321 if (filter
->inputs
[j
]->incfg
.formats
&& filter
->inputs
[j
]->incfg
.formats
->nb_formats
== 1) {
1322 if ((ret
= pick_format(filter
->inputs
[j
], NULL
)) < 0)
1328 if (filter
->nb_outputs
){
1329 for (j
= 0; j
< filter
->nb_outputs
; j
++){
1330 if (filter
->outputs
[j
]->incfg
.formats
&& filter
->outputs
[j
]->incfg
.formats
->nb_formats
== 1) {
1331 if ((ret
= pick_format(filter
->outputs
[j
], NULL
)) < 0)
1337 if (filter
->nb_inputs
&& filter
->nb_outputs
&& filter
->inputs
[0]->format
>=0) {
1338 for (j
= 0; j
< filter
->nb_outputs
; j
++) {
1339 if (filter
->outputs
[j
]->format
<0) {
1340 if ((ret
= pick_format(filter
->outputs
[j
], filter
->inputs
[0])) < 0)
1349 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1350 AVFilterContext
*filter
= graph
->filters
[i
];
1352 for (j
= 0; j
< filter
->nb_inputs
; j
++)
1353 if ((ret
= pick_format(filter
->inputs
[j
], NULL
)) < 0)
1355 for (j
= 0; j
< filter
->nb_outputs
; j
++)
1356 if ((ret
= pick_format(filter
->outputs
[j
], NULL
)) < 0)
1363 * Configure the formats of all the links in the graph.
1365 static int graph_config_formats(AVFilterGraph
*graph
, void *log_ctx
)
1369 /* find supported formats from sub-filters, and merge along links */
1370 while ((ret
= query_formats(graph
, log_ctx
)) == AVERROR(EAGAIN
))
1371 av_log(graph
, AV_LOG_DEBUG
, "query_formats not finished\n");
1375 /* Once everything is merged, it's possible that we'll still have
1376 * multiple valid media format choices. We try to minimize the amount
1377 * of format conversion inside filters */
1378 if ((ret
= reduce_formats(graph
)) < 0)
1381 /* for audio filters, ensure the best format, sample rate and channel layout
1383 swap_sample_fmts(graph
);
1384 swap_samplerates(graph
);
1385 swap_channel_layouts(graph
);
1387 if ((ret
= pick_formats(graph
)) < 0)
1393 static int graph_config_pointers(AVFilterGraph
*graph
, void *log_ctx
)
1396 int sink_links_count
= 0, n
= 0;
1398 FilterLinkInternal
**sinks
;
1400 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1401 f
= graph
->filters
[i
];
1402 for (j
= 0; j
< f
->nb_inputs
; j
++) {
1403 ff_link_internal(f
->inputs
[j
])->age_index
= -1;
1405 for (j
= 0; j
< f
->nb_outputs
; j
++) {
1406 ff_link_internal(f
->outputs
[j
])->age_index
= -1;
1408 if (!f
->nb_outputs
) {
1409 if (f
->nb_inputs
> INT_MAX
- sink_links_count
)
1410 return AVERROR(EINVAL
);
1411 sink_links_count
+= f
->nb_inputs
;
1414 sinks
= av_calloc(sink_links_count
, sizeof(*sinks
));
1416 return AVERROR(ENOMEM
);
1417 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1418 f
= graph
->filters
[i
];
1419 if (!f
->nb_outputs
) {
1420 for (j
= 0; j
< f
->nb_inputs
; j
++) {
1421 sinks
[n
] = ff_link_internal(f
->inputs
[j
]);
1422 sinks
[n
]->age_index
= n
;
1427 av_assert0(n
== sink_links_count
);
1428 fffiltergraph(graph
)->sink_links
= sinks
;
1429 fffiltergraph(graph
)->sink_links_count
= sink_links_count
;
1433 int avfilter_graph_config(AVFilterGraph
*graphctx
, void *log_ctx
)
1437 if (graphctx
->max_buffered_frames
)
1438 fffiltergraph(graphctx
)->frame_queues
.max_queued
= graphctx
->max_buffered_frames
;
1439 if ((ret
= graph_check_validity(graphctx
, log_ctx
)))
1441 if ((ret
= graph_config_formats(graphctx
, log_ctx
)))
1443 if ((ret
= graph_config_links(graphctx
, log_ctx
)))
1445 if ((ret
= graph_check_links(graphctx
, log_ctx
)))
1447 if ((ret
= graph_config_pointers(graphctx
, log_ctx
)))
1453 int avfilter_graph_send_command(AVFilterGraph
*graph
, const char *target
, const char *cmd
, const char *arg
, char *res
, int res_len
, int flags
)
1455 int i
, r
= AVERROR(ENOSYS
);
1460 if ((flags
& AVFILTER_CMD_FLAG_ONE
) && !(flags
& AVFILTER_CMD_FLAG_FAST
)) {
1461 r
= avfilter_graph_send_command(graph
, target
, cmd
, arg
, res
, res_len
, flags
| AVFILTER_CMD_FLAG_FAST
);
1462 if (r
!= AVERROR(ENOSYS
))
1469 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1470 AVFilterContext
*filter
= graph
->filters
[i
];
1471 if (!strcmp(target
, "all") || (filter
->name
&& !strcmp(target
, filter
->name
)) || !strcmp(target
, filter
->filter
->name
)) {
1472 r
= avfilter_process_command(filter
, cmd
, arg
, res
, res_len
, flags
);
1473 if (r
!= AVERROR(ENOSYS
)) {
1474 if ((flags
& AVFILTER_CMD_FLAG_ONE
) || r
< 0)
1483 int avfilter_graph_queue_command(AVFilterGraph
*graph
, const char *target
, const char *command
, const char *arg
, int flags
, double ts
)
1490 for (i
= 0; i
< graph
->nb_filters
; i
++) {
1491 AVFilterContext
*filter
= graph
->filters
[i
];
1492 FFFilterContext
*ctxi
= fffilterctx(filter
);
1493 if(filter
&& (!strcmp(target
, "all") || !strcmp(target
, filter
->name
) || !strcmp(target
, filter
->filter
->name
))){
1494 AVFilterCommand
**queue
= &ctxi
->command_queue
, *next
;
1495 while (*queue
&& (*queue
)->time
<= ts
)
1496 queue
= &(*queue
)->next
;
1498 *queue
= av_mallocz(sizeof(AVFilterCommand
));
1500 return AVERROR(ENOMEM
);
1502 (*queue
)->command
= av_strdup(command
);
1503 (*queue
)->arg
= av_strdup(arg
);
1504 (*queue
)->time
= ts
;
1505 (*queue
)->flags
= flags
;
1506 (*queue
)->next
= next
;
1507 if(flags
& AVFILTER_CMD_FLAG_ONE
)
1515 static void heap_bubble_up(FFFilterGraph
*graph
,
1516 FilterLinkInternal
*li
, int index
)
1518 FilterLinkInternal
**links
= graph
->sink_links
;
1520 av_assert0(index
>= 0);
1523 int parent
= (index
- 1) >> 1;
1524 if (links
[parent
]->l
.current_pts_us
>= li
->l
.current_pts_us
)
1526 links
[index
] = links
[parent
];
1527 links
[index
]->age_index
= index
;
1531 li
->age_index
= index
;
1534 static void heap_bubble_down(FFFilterGraph
*graph
,
1535 FilterLinkInternal
*li
, int index
)
1537 FilterLinkInternal
**links
= graph
->sink_links
;
1539 av_assert0(index
>= 0);
1542 int child
= 2 * index
+ 1;
1543 if (child
>= graph
->sink_links_count
)
1545 if (child
+ 1 < graph
->sink_links_count
&&
1546 links
[child
+ 1]->l
.current_pts_us
< links
[child
]->l
.current_pts_us
)
1548 if (li
->l
.current_pts_us
< links
[child
]->l
.current_pts_us
)
1550 links
[index
] = links
[child
];
1551 links
[index
]->age_index
= index
;
1555 li
->age_index
= index
;
1558 void ff_avfilter_graph_update_heap(AVFilterGraph
*graph
, FilterLinkInternal
*li
)
1560 FFFilterGraph
*graphi
= fffiltergraph(graph
);
1562 heap_bubble_up (graphi
, li
, li
->age_index
);
1563 heap_bubble_down(graphi
, li
, li
->age_index
);
1566 int avfilter_graph_request_oldest(AVFilterGraph
*graph
)
1568 FFFilterGraph
*graphi
= fffiltergraph(graph
);
1569 FilterLinkInternal
*oldesti
= graphi
->sink_links
[0];
1570 AVFilterLink
*oldest
= &oldesti
->l
.pub
;
1571 int64_t frame_count
;
1574 while (graphi
->sink_links_count
) {
1575 oldesti
= graphi
->sink_links
[0];
1576 oldest
= &oldesti
->l
.pub
;
1577 if (fffilter(oldest
->dst
->filter
)->activate
) {
1578 r
= av_buffersink_get_frame_flags(oldest
->dst
, NULL
,
1579 AV_BUFFERSINK_FLAG_PEEK
);
1580 if (r
!= AVERROR_EOF
)
1583 r
= ff_request_frame(oldest
);
1585 if (r
!= AVERROR_EOF
)
1587 av_log(oldest
->dst
, AV_LOG_DEBUG
, "EOF on sink link %s:%s.\n",
1589 oldest
->dstpad
->name
);
1590 /* EOF: remove the link from the heap */
1591 if (oldesti
->age_index
< --graphi
->sink_links_count
)
1592 heap_bubble_down(graphi
, graphi
->sink_links
[graphi
->sink_links_count
],
1593 oldesti
->age_index
);
1594 oldesti
->age_index
= -1;
1596 if (!graphi
->sink_links_count
)
1598 av_assert1(!fffilter(oldest
->dst
->filter
)->activate
);
1599 av_assert1(oldesti
->age_index
>= 0);
1600 frame_count
= oldesti
->l
.frame_count_out
;
1601 while (frame_count
== oldesti
->l
.frame_count_out
) {
1602 r
= ff_filter_graph_run_once(graph
);
1603 if (r
== FFERROR_BUFFERSRC_EMPTY
)
1605 if (r
== AVERROR(EAGAIN
) &&
1606 !oldesti
->frame_wanted_out
&& !oldesti
->frame_blocked_in
&&
1607 !oldesti
->status_in
)
1608 (void)ff_request_frame(oldest
);
1615 int ff_filter_graph_run_once(AVFilterGraph
*graph
)
1617 FFFilterContext
*ctxi
;
1620 av_assert0(graph
->nb_filters
);
1621 ctxi
= fffilterctx(graph
->filters
[0]);
1622 for (i
= 1; i
< graph
->nb_filters
; i
++) {
1623 FFFilterContext
*ctxi_other
= fffilterctx(graph
->filters
[i
]);
1625 if (ctxi_other
->ready
> ctxi
->ready
)
1630 return AVERROR(EAGAIN
);
1631 return ff_filter_activate(&ctxi
->p
);