169
226
av_strlcat(scale_args, ":", sizeof(scale_args));
170
227
av_strlcat(scale_args, graph->scale_sws_opts, sizeof(scale_args));
172
if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
173
inst_name, scale_args, NULL, graph)) < 0)
175
if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
178
scale->filter->query_formats(scale);
179
if (((link = scale-> inputs[0]) &&
180
!avfilter_merge_formats(link->in_formats, link->out_formats)) ||
181
((link = scale->outputs[0]) &&
182
!avfilter_merge_formats(link->in_formats, link->out_formats))) {
183
av_log(log_ctx, AV_LOG_ERROR,
184
"Impossible to convert between the formats supported by the filter "
185
"'%s' and the filter '%s'\n", link->src->name, link->dst->name);
229
if ((ret = avfilter_graph_create_filter(&convert, filter,
230
inst_name, scale_args, NULL,
234
case AVMEDIA_TYPE_AUDIO:
235
if (!(filter = avfilter_get_by_name("resample"))) {
236
av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
237
"not present, cannot convert audio formats.\n");
186
238
return AVERROR(EINVAL);
241
snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
243
if ((ret = avfilter_graph_create_filter(&convert, filter,
244
inst_name, NULL, NULL, graph)) < 0)
248
return AVERROR(EINVAL);
251
if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
254
convert->filter->query_formats(convert);
255
inlink = convert->inputs[0];
256
outlink = convert->outputs[0];
257
if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
258
!ff_merge_formats(outlink->in_formats, outlink->out_formats))
259
ret |= AVERROR(ENOSYS);
260
if (inlink->type == AVMEDIA_TYPE_AUDIO &&
261
(!ff_merge_samplerates(inlink->in_samplerates,
262
inlink->out_samplerates) ||
263
!ff_merge_channel_layouts(inlink->in_channel_layouts,
264
inlink->out_channel_layouts)))
265
ret |= AVERROR(ENOSYS);
266
if (outlink->type == AVMEDIA_TYPE_AUDIO &&
267
(!ff_merge_samplerates(outlink->in_samplerates,
268
outlink->out_samplerates) ||
269
!ff_merge_channel_layouts(outlink->in_channel_layouts,
270
outlink->out_channel_layouts)))
271
ret |= AVERROR(ENOSYS);
274
av_log(log_ctx, AV_LOG_ERROR,
275
"Impossible to convert between the formats supported by the filter "
276
"'%s' and the filter '%s'\n", link->src->name, link->dst->name);
196
static void pick_format(AVFilterLink *link)
286
static int pick_format(AVFilterLink *link)
198
288
if (!link || !link->in_formats)
201
291
link->in_formats->format_count = 1;
202
292
link->format = link->in_formats->formats[0];
204
avfilter_formats_unref(&link->in_formats);
205
avfilter_formats_unref(&link->out_formats);
208
static void pick_formats(AVFilterGraph *graph)
294
if (link->type == AVMEDIA_TYPE_AUDIO) {
295
if (!link->in_samplerates->format_count) {
296
av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
297
" the link between filters %s and %s.\n", link->src->name,
299
return AVERROR(EINVAL);
301
link->in_samplerates->format_count = 1;
302
link->sample_rate = link->in_samplerates->formats[0];
304
if (!link->in_channel_layouts->nb_channel_layouts) {
305
av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
306
"the link between filters %s and %s.\n", link->src->name,
308
return AVERROR(EINVAL);
310
link->in_channel_layouts->nb_channel_layouts = 1;
311
link->channel_layout = link->in_channel_layouts->channel_layouts[0];
314
ff_formats_unref(&link->in_formats);
315
ff_formats_unref(&link->out_formats);
316
ff_formats_unref(&link->in_samplerates);
317
ff_formats_unref(&link->out_samplerates);
318
ff_channel_layouts_unref(&link->in_channel_layouts);
319
ff_channel_layouts_unref(&link->out_channel_layouts);
324
#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
326
for (i = 0; i < filter->nb_inputs; i++) { \
327
AVFilterLink *link = filter->inputs[i]; \
330
if (!link->out_ ## list || link->out_ ## list->nb != 1) \
332
fmt = link->out_ ## list->var[0]; \
334
for (j = 0; j < filter->nb_outputs; j++) { \
335
AVFilterLink *out_link = filter->outputs[j]; \
338
if (link->type != out_link->type || \
339
out_link->in_ ## list->nb == 1) \
341
fmts = out_link->in_ ## list; \
343
if (!out_link->in_ ## list->nb) { \
344
add_format(&out_link->in_ ##list, fmt); \
348
for (k = 0; k < out_link->in_ ## list->nb; k++) \
349
if (fmts->var[k] == fmt) { \
350
fmts->var[0] = fmt; \
359
static int reduce_formats_on_filter(AVFilterContext *filter)
361
int i, j, k, ret = 0;
363
REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
364
format_count, ff_add_format);
365
REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
366
format_count, ff_add_format);
367
REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
368
channel_layouts, nb_channel_layouts, ff_add_channel_layout);
373
static void reduce_formats(AVFilterGraph *graph)
380
for (i = 0; i < graph->filter_count; i++)
381
reduced |= reduce_formats_on_filter(graph->filters[i]);
385
static void swap_samplerates_on_filter(AVFilterContext *filter)
387
AVFilterLink *link = NULL;
391
for (i = 0; i < filter->nb_inputs; i++) {
392
link = filter->inputs[i];
394
if (link->type == AVMEDIA_TYPE_AUDIO &&
395
link->out_samplerates->format_count == 1)
398
if (i == filter->nb_inputs)
401
sample_rate = link->out_samplerates->formats[0];
403
for (i = 0; i < filter->nb_outputs; i++) {
404
AVFilterLink *outlink = filter->outputs[i];
405
int best_idx, best_diff = INT_MAX;
407
if (outlink->type != AVMEDIA_TYPE_AUDIO ||
408
outlink->in_samplerates->format_count < 2)
411
for (j = 0; j < outlink->in_samplerates->format_count; j++) {
412
int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
414
if (diff < best_diff) {
419
FFSWAP(int, outlink->in_samplerates->formats[0],
420
outlink->in_samplerates->formats[best_idx]);
424
static void swap_samplerates(AVFilterGraph *graph)
428
for (i = 0; i < graph->filter_count; i++)
429
swap_samplerates_on_filter(graph->filters[i]);
432
#define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
433
#define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
434
#define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
435
#define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
436
#define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
437
#define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
438
#define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
440
/* allowable substitutions for channel pairs when comparing layouts,
441
* ordered by priority for both values */
442
static const uint64_t ch_subst[][2] = {
443
{ CH_FRONT_PAIR, CH_CENTER_PAIR },
444
{ CH_FRONT_PAIR, CH_WIDE_PAIR },
445
{ CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
446
{ CH_CENTER_PAIR, CH_FRONT_PAIR },
447
{ CH_CENTER_PAIR, CH_WIDE_PAIR },
448
{ CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
449
{ CH_WIDE_PAIR, CH_FRONT_PAIR },
450
{ CH_WIDE_PAIR, CH_CENTER_PAIR },
451
{ CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
452
{ AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
453
{ AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
454
{ AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
455
{ CH_SIDE_PAIR, CH_DIRECT_PAIR },
456
{ CH_SIDE_PAIR, CH_BACK_PAIR },
457
{ CH_SIDE_PAIR, AV_CH_BACK_CENTER },
458
{ CH_BACK_PAIR, CH_DIRECT_PAIR },
459
{ CH_BACK_PAIR, CH_SIDE_PAIR },
460
{ CH_BACK_PAIR, AV_CH_BACK_CENTER },
461
{ AV_CH_BACK_CENTER, CH_BACK_PAIR },
462
{ AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
463
{ AV_CH_BACK_CENTER, CH_SIDE_PAIR },
466
static void swap_channel_layouts_on_filter(AVFilterContext *filter)
468
AVFilterLink *link = NULL;
471
for (i = 0; i < filter->nb_inputs; i++) {
472
link = filter->inputs[i];
474
if (link->type == AVMEDIA_TYPE_AUDIO &&
475
link->out_channel_layouts->nb_channel_layouts == 1)
478
if (i == filter->nb_inputs)
481
for (i = 0; i < filter->nb_outputs; i++) {
482
AVFilterLink *outlink = filter->outputs[i];
483
int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
485
if (outlink->type != AVMEDIA_TYPE_AUDIO ||
486
outlink->in_channel_layouts->nb_channel_layouts < 2)
489
for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
490
uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
491
uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
492
int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
493
int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
494
int count_diff = out_channels - in_channels;
495
int matched_channels, extra_channels;
498
/* channel substitution */
499
for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
500
uint64_t cmp0 = ch_subst[k][0];
501
uint64_t cmp1 = ch_subst[k][1];
502
if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
503
(out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
504
in_chlayout &= ~cmp0;
505
out_chlayout &= ~cmp1;
506
/* add score for channel match, minus a deduction for
507
having to do the substitution */
508
score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
512
/* no penalty for LFE channel mismatch */
513
if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
514
(out_chlayout & AV_CH_LOW_FREQUENCY))
516
in_chlayout &= ~AV_CH_LOW_FREQUENCY;
517
out_chlayout &= ~AV_CH_LOW_FREQUENCY;
519
matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
521
extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
523
score += 10 * matched_channels - 5 * extra_channels;
525
if (score > best_score ||
526
(count_diff < best_count_diff && score == best_score)) {
529
best_count_diff = count_diff;
532
av_assert0(best_idx >= 0);
533
FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
534
outlink->in_channel_layouts->channel_layouts[best_idx]);
539
static void swap_channel_layouts(AVFilterGraph *graph)
543
for (i = 0; i < graph->filter_count; i++)
544
swap_channel_layouts_on_filter(graph->filters[i]);
547
static void swap_sample_fmts_on_filter(AVFilterContext *filter)
549
AVFilterLink *link = NULL;
553
for (i = 0; i < filter->nb_inputs; i++) {
554
link = filter->inputs[i];
556
if (link->type == AVMEDIA_TYPE_AUDIO &&
557
link->out_formats->format_count == 1)
560
if (i == filter->nb_inputs)
563
format = link->out_formats->formats[0];
564
bps = av_get_bytes_per_sample(format);
566
for (i = 0; i < filter->nb_outputs; i++) {
567
AVFilterLink *outlink = filter->outputs[i];
568
int best_idx = -1, best_score = INT_MIN;
570
if (outlink->type != AVMEDIA_TYPE_AUDIO ||
571
outlink->in_formats->format_count < 2)
574
for (j = 0; j < outlink->in_formats->format_count; j++) {
575
int out_format = outlink->in_formats->formats[j];
576
int out_bps = av_get_bytes_per_sample(out_format);
579
if (av_get_packed_sample_fmt(out_format) == format ||
580
av_get_planar_sample_fmt(out_format) == format) {
585
/* for s32 and float prefer double to prevent loss of information */
586
if (bps == 4 && out_bps == 8) {
591
/* prefer closest higher or equal bps */
592
score = -abs(out_bps - bps);
596
if (score > best_score) {
601
av_assert0(best_idx >= 0);
602
FFSWAP(int, outlink->in_formats->formats[0],
603
outlink->in_formats->formats[best_idx]);
607
static void swap_sample_fmts(AVFilterGraph *graph)
611
for (i = 0; i < graph->filter_count; i++)
612
swap_sample_fmts_on_filter(graph->filters[i]);
616
static int pick_formats(AVFilterGraph *graph)
212
620
for (i = 0; i < graph->filter_count; i++) {
213
621
AVFilterContext *filter = graph->filters[i];
215
for (j = 0; j < filter->input_count; j++)
216
pick_format(filter->inputs[j]);
217
for (j = 0; j < filter->output_count; j++)
218
pick_format(filter->outputs[j]);
623
for (j = 0; j < filter->nb_inputs; j++)
624
if ((ret = pick_format(filter->inputs[j])) < 0)
626
for (j = 0; j < filter->nb_outputs; j++)
627
if ((ret = pick_format(filter->outputs[j])) < 0)
222
int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
634
* Configure the formats of all the links in the graph.
636
static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)