~siretart/libav/trusty

« back to all changes in this revision

Viewing changes to libavfilter/avfiltergraph.c

  • Committer: Reinhard Tartler
  • Date: 2013-10-23 03:04:17 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: siretart@tauware.de-20131023030417-1o6mpkl1l0raifjt
mergeĀ fromĀ debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <ctype.h>
24
24
#include <string.h>
25
25
 
 
26
#include "libavutil/avassert.h"
26
27
#include "libavutil/avstring.h"
 
28
#include "libavutil/channel_layout.h"
 
29
#include "libavutil/common.h"
 
30
#include "libavutil/log.h"
27
31
#include "avfilter.h"
28
32
#include "avfiltergraph.h"
 
33
#include "formats.h"
29
34
#include "internal.h"
30
35
 
 
36
static const AVClass filtergraph_class = {
 
37
    .class_name = "AVFilterGraph",
 
38
    .item_name  = av_default_item_name,
 
39
    .version    = LIBAVUTIL_VERSION_INT,
 
40
};
 
41
 
31
42
AVFilterGraph *avfilter_graph_alloc(void)
32
43
{
33
 
    return av_mallocz(sizeof(AVFilterGraph));
 
44
    AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
 
45
    if (!ret)
 
46
        return NULL;
 
47
    ret->av_class = &filtergraph_class;
 
48
    return ret;
34
49
}
35
50
 
36
51
void avfilter_graph_free(AVFilterGraph **graph)
78
93
    return ret;
79
94
}
80
95
 
81
 
int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
 
96
/**
 
97
 * Check for the validity of graph.
 
98
 *
 
99
 * A graph is considered valid if all its input and output pads are
 
100
 * connected.
 
101
 *
 
102
 * @return 0 in case of success, a negative value otherwise
 
103
 */
 
104
static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
82
105
{
83
106
    AVFilterContext *filt;
84
107
    int i, j;
86
109
    for (i = 0; i < graph->filter_count; i++) {
87
110
        filt = graph->filters[i];
88
111
 
89
 
        for (j = 0; j < filt->input_count; j++) {
 
112
        for (j = 0; j < filt->nb_inputs; j++) {
90
113
            if (!filt->inputs[j] || !filt->inputs[j]->src) {
91
114
                av_log(log_ctx, AV_LOG_ERROR,
92
115
                       "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
95
118
            }
96
119
        }
97
120
 
98
 
        for (j = 0; j < filt->output_count; j++) {
 
121
        for (j = 0; j < filt->nb_outputs; j++) {
99
122
            if (!filt->outputs[j] || !filt->outputs[j]->dst) {
100
123
                av_log(log_ctx, AV_LOG_ERROR,
101
124
                       "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
108
131
    return 0;
109
132
}
110
133
 
111
 
int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
 
134
/**
 
135
 * Configure all the links of graphctx.
 
136
 *
 
137
 * @return 0 in case of success, a negative value otherwise
 
138
 */
 
139
static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
112
140
{
113
141
    AVFilterContext *filt;
114
142
    int i, ret;
116
144
    for (i=0; i < graph->filter_count; i++) {
117
145
        filt = graph->filters[i];
118
146
 
119
 
        if (!filt->output_count) {
 
147
        if (!filt->nb_outputs) {
120
148
            if ((ret = avfilter_config_links(filt)))
121
149
                return ret;
122
150
        }
139
167
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
140
168
{
141
169
    int i, j, ret;
142
 
    int scaler_count = 0;
143
 
    char inst_name[30];
 
170
    int scaler_count = 0, resampler_count = 0;
144
171
 
145
172
    /* ask all the sub-filters for their supported media formats */
146
173
    for (i = 0; i < graph->filter_count; i++) {
147
174
        if (graph->filters[i]->filter->query_formats)
148
175
            graph->filters[i]->filter->query_formats(graph->filters[i]);
149
176
        else
150
 
            avfilter_default_query_formats(graph->filters[i]);
 
177
            ff_default_query_formats(graph->filters[i]);
151
178
    }
152
179
 
153
180
    /* go through and merge as many format lists as possible */
154
181
    for (i = 0; i < graph->filter_count; i++) {
155
182
        AVFilterContext *filter = graph->filters[i];
156
183
 
157
 
        for (j = 0; j < filter->input_count; j++) {
 
184
        for (j = 0; j < filter->nb_inputs; j++) {
158
185
            AVFilterLink *link = filter->inputs[j];
159
 
            if (link && link->in_formats != link->out_formats) {
160
 
                if (!avfilter_merge_formats(link->in_formats,
161
 
                                            link->out_formats)) {
162
 
                    AVFilterContext *scale;
163
 
                    char scale_args[256];
164
 
                    /* couldn't merge format lists. auto-insert scale filter */
 
186
            int convert_needed = 0;
 
187
 
 
188
            if (!link)
 
189
                continue;
 
190
 
 
191
            if (link->in_formats != link->out_formats &&
 
192
                !ff_merge_formats(link->in_formats,
 
193
                                        link->out_formats))
 
194
                convert_needed = 1;
 
195
            if (link->type == AVMEDIA_TYPE_AUDIO) {
 
196
                if (link->in_channel_layouts != link->out_channel_layouts &&
 
197
                    !ff_merge_channel_layouts(link->in_channel_layouts,
 
198
                                              link->out_channel_layouts))
 
199
                    convert_needed = 1;
 
200
                if (link->in_samplerates != link->out_samplerates &&
 
201
                    !ff_merge_samplerates(link->in_samplerates,
 
202
                                          link->out_samplerates))
 
203
                    convert_needed = 1;
 
204
            }
 
205
 
 
206
            if (convert_needed) {
 
207
                AVFilterContext *convert;
 
208
                AVFilter *filter;
 
209
                AVFilterLink *inlink, *outlink;
 
210
                char scale_args[256];
 
211
                char inst_name[30];
 
212
 
 
213
                /* couldn't merge format lists. auto-insert conversion filter */
 
214
                switch (link->type) {
 
215
                case AVMEDIA_TYPE_VIDEO:
 
216
                    if (!(filter = avfilter_get_by_name("scale"))) {
 
217
                        av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
 
218
                               "not present, cannot convert pixel formats.\n");
 
219
                        return AVERROR(EINVAL);
 
220
                    }
 
221
 
165
222
                    snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
166
223
                             scaler_count++);
167
224
                    av_strlcpy(scale_args, "0:0", sizeof(scale_args));
169
226
                        av_strlcat(scale_args, ":", sizeof(scale_args));
170
227
                        av_strlcat(scale_args, graph->scale_sws_opts, sizeof(scale_args));
171
228
                    }
172
 
                    if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
173
 
                                                            inst_name, scale_args, NULL, graph)) < 0)
174
 
                        return ret;
175
 
                    if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
176
 
                        return ret;
177
 
 
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,
 
231
                                                            graph)) < 0)
 
232
                        return ret;
 
233
                    break;
 
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);
187
239
                    }
 
240
 
 
241
                    snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
 
242
                             resampler_count++);
 
243
                    if ((ret = avfilter_graph_create_filter(&convert, filter,
 
244
                                                            inst_name, NULL, NULL, graph)) < 0)
 
245
                        return ret;
 
246
                    break;
 
247
                default:
 
248
                    return AVERROR(EINVAL);
 
249
                }
 
250
 
 
251
                if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
 
252
                    return ret;
 
253
 
 
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);
 
272
 
 
273
                if (ret < 0) {
 
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);
 
277
                    return ret;
188
278
                }
189
279
            }
190
280
        }
193
283
    return 0;
194
284
}
195
285
 
196
 
static void pick_format(AVFilterLink *link)
 
286
static int pick_format(AVFilterLink *link)
197
287
{
198
288
    if (!link || !link->in_formats)
199
 
        return;
 
289
        return 0;
200
290
 
201
291
    link->in_formats->format_count = 1;
202
292
    link->format = link->in_formats->formats[0];
203
293
 
204
 
    avfilter_formats_unref(&link->in_formats);
205
 
    avfilter_formats_unref(&link->out_formats);
206
 
}
207
 
 
208
 
static void pick_formats(AVFilterGraph *graph)
209
 
{
210
 
    int i, j;
 
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,
 
298
                   link->dst->name);
 
299
            return AVERROR(EINVAL);
 
300
        }
 
301
        link->in_samplerates->format_count = 1;
 
302
        link->sample_rate = link->in_samplerates->formats[0];
 
303
 
 
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,
 
307
                   link->dst->name);
 
308
            return AVERROR(EINVAL);
 
309
        }
 
310
        link->in_channel_layouts->nb_channel_layouts = 1;
 
311
        link->channel_layout = link->in_channel_layouts->channel_layouts[0];
 
312
    }
 
313
 
 
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);
 
320
 
 
321
    return 0;
 
322
}
 
323
 
 
324
#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
 
325
do {                                                                   \
 
326
    for (i = 0; i < filter->nb_inputs; i++) {                          \
 
327
        AVFilterLink *link = filter->inputs[i];                        \
 
328
        fmt_type fmt;                                                  \
 
329
                                                                       \
 
330
        if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
 
331
            continue;                                                  \
 
332
        fmt = link->out_ ## list->var[0];                              \
 
333
                                                                       \
 
334
        for (j = 0; j < filter->nb_outputs; j++) {                     \
 
335
            AVFilterLink *out_link = filter->outputs[j];               \
 
336
            list_type *fmts;                                           \
 
337
                                                                       \
 
338
            if (link->type != out_link->type ||                        \
 
339
                out_link->in_ ## list->nb == 1)                        \
 
340
                continue;                                              \
 
341
            fmts = out_link->in_ ## list;                              \
 
342
                                                                       \
 
343
            if (!out_link->in_ ## list->nb) {                          \
 
344
                add_format(&out_link->in_ ##list, fmt);                \
 
345
                break;                                                 \
 
346
            }                                                          \
 
347
                                                                       \
 
348
            for (k = 0; k < out_link->in_ ## list->nb; k++)            \
 
349
                if (fmts->var[k] == fmt) {                             \
 
350
                    fmts->var[0]  = fmt;                               \
 
351
                    fmts->nb = 1;                                      \
 
352
                    ret = 1;                                           \
 
353
                    break;                                             \
 
354
                }                                                      \
 
355
        }                                                              \
 
356
    }                                                                  \
 
357
} while (0)
 
358
 
 
359
static int reduce_formats_on_filter(AVFilterContext *filter)
 
360
{
 
361
    int i, j, k, ret = 0;
 
362
 
 
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);
 
369
 
 
370
    return ret;
 
371
}
 
372
 
 
373
static void reduce_formats(AVFilterGraph *graph)
 
374
{
 
375
    int i, reduced;
 
376
 
 
377
    do {
 
378
        reduced = 0;
 
379
 
 
380
        for (i = 0; i < graph->filter_count; i++)
 
381
            reduced |= reduce_formats_on_filter(graph->filters[i]);
 
382
    } while (reduced);
 
383
}
 
384
 
 
385
static void swap_samplerates_on_filter(AVFilterContext *filter)
 
386
{
 
387
    AVFilterLink *link = NULL;
 
388
    int sample_rate;
 
389
    int i, j;
 
390
 
 
391
    for (i = 0; i < filter->nb_inputs; i++) {
 
392
        link = filter->inputs[i];
 
393
 
 
394
        if (link->type == AVMEDIA_TYPE_AUDIO &&
 
395
            link->out_samplerates->format_count == 1)
 
396
            break;
 
397
    }
 
398
    if (i == filter->nb_inputs)
 
399
        return;
 
400
 
 
401
    sample_rate = link->out_samplerates->formats[0];
 
402
 
 
403
    for (i = 0; i < filter->nb_outputs; i++) {
 
404
        AVFilterLink *outlink = filter->outputs[i];
 
405
        int best_idx, best_diff = INT_MAX;
 
406
 
 
407
        if (outlink->type != AVMEDIA_TYPE_AUDIO ||
 
408
            outlink->in_samplerates->format_count < 2)
 
409
            continue;
 
410
 
 
411
        for (j = 0; j < outlink->in_samplerates->format_count; j++) {
 
412
            int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
 
413
 
 
414
            if (diff < best_diff) {
 
415
                best_diff = diff;
 
416
                best_idx  = j;
 
417
            }
 
418
        }
 
419
        FFSWAP(int, outlink->in_samplerates->formats[0],
 
420
               outlink->in_samplerates->formats[best_idx]);
 
421
    }
 
422
}
 
423
 
 
424
static void swap_samplerates(AVFilterGraph *graph)
 
425
{
 
426
    int i;
 
427
 
 
428
    for (i = 0; i < graph->filter_count; i++)
 
429
        swap_samplerates_on_filter(graph->filters[i]);
 
430
}
 
431
 
 
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)
 
439
 
 
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       },
 
464
};
 
465
 
 
466
static void swap_channel_layouts_on_filter(AVFilterContext *filter)
 
467
{
 
468
    AVFilterLink *link = NULL;
 
469
    int i, j, k;
 
470
 
 
471
    for (i = 0; i < filter->nb_inputs; i++) {
 
472
        link = filter->inputs[i];
 
473
 
 
474
        if (link->type == AVMEDIA_TYPE_AUDIO &&
 
475
            link->out_channel_layouts->nb_channel_layouts == 1)
 
476
            break;
 
477
    }
 
478
    if (i == filter->nb_inputs)
 
479
        return;
 
480
 
 
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;
 
484
 
 
485
        if (outlink->type != AVMEDIA_TYPE_AUDIO ||
 
486
            outlink->in_channel_layouts->nb_channel_layouts < 2)
 
487
            continue;
 
488
 
 
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;
 
496
            int score = 0;
 
497
 
 
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;
 
509
                }
 
510
            }
 
511
 
 
512
            /* no penalty for LFE channel mismatch */
 
513
            if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
 
514
                (out_chlayout & AV_CH_LOW_FREQUENCY))
 
515
                score += 10;
 
516
            in_chlayout  &= ~AV_CH_LOW_FREQUENCY;
 
517
            out_chlayout &= ~AV_CH_LOW_FREQUENCY;
 
518
 
 
519
            matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
 
520
                                                                 out_chlayout);
 
521
            extra_channels   = av_get_channel_layout_nb_channels(out_chlayout &
 
522
                                                                 (~in_chlayout));
 
523
            score += 10 * matched_channels - 5 * extra_channels;
 
524
 
 
525
            if (score > best_score ||
 
526
                (count_diff < best_count_diff && score == best_score)) {
 
527
                best_score = score;
 
528
                best_idx   = j;
 
529
                best_count_diff = count_diff;
 
530
            }
 
531
        }
 
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]);
 
535
    }
 
536
 
 
537
}
 
538
 
 
539
static void swap_channel_layouts(AVFilterGraph *graph)
 
540
{
 
541
    int i;
 
542
 
 
543
    for (i = 0; i < graph->filter_count; i++)
 
544
        swap_channel_layouts_on_filter(graph->filters[i]);
 
545
}
 
546
 
 
547
static void swap_sample_fmts_on_filter(AVFilterContext *filter)
 
548
{
 
549
    AVFilterLink *link = NULL;
 
550
    int format, bps;
 
551
    int i, j;
 
552
 
 
553
    for (i = 0; i < filter->nb_inputs; i++) {
 
554
        link = filter->inputs[i];
 
555
 
 
556
        if (link->type == AVMEDIA_TYPE_AUDIO &&
 
557
            link->out_formats->format_count == 1)
 
558
            break;
 
559
    }
 
560
    if (i == filter->nb_inputs)
 
561
        return;
 
562
 
 
563
    format = link->out_formats->formats[0];
 
564
    bps    = av_get_bytes_per_sample(format);
 
565
 
 
566
    for (i = 0; i < filter->nb_outputs; i++) {
 
567
        AVFilterLink *outlink = filter->outputs[i];
 
568
        int best_idx = -1, best_score = INT_MIN;
 
569
 
 
570
        if (outlink->type != AVMEDIA_TYPE_AUDIO ||
 
571
            outlink->in_formats->format_count < 2)
 
572
            continue;
 
573
 
 
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);
 
577
            int score;
 
578
 
 
579
            if (av_get_packed_sample_fmt(out_format) == format ||
 
580
                av_get_planar_sample_fmt(out_format) == format) {
 
581
                best_idx   = j;
 
582
                break;
 
583
            }
 
584
 
 
585
            /* for s32 and float prefer double to prevent loss of information */
 
586
            if (bps == 4 && out_bps == 8) {
 
587
                best_idx = j;
 
588
                break;
 
589
            }
 
590
 
 
591
            /* prefer closest higher or equal bps */
 
592
            score = -abs(out_bps - bps);
 
593
            if (out_bps >= bps)
 
594
                score += INT_MAX/2;
 
595
 
 
596
            if (score > best_score) {
 
597
                best_score = score;
 
598
                best_idx   = j;
 
599
            }
 
600
        }
 
601
        av_assert0(best_idx >= 0);
 
602
        FFSWAP(int, outlink->in_formats->formats[0],
 
603
               outlink->in_formats->formats[best_idx]);
 
604
    }
 
605
}
 
606
 
 
607
static void swap_sample_fmts(AVFilterGraph *graph)
 
608
{
 
609
    int i;
 
610
 
 
611
    for (i = 0; i < graph->filter_count; i++)
 
612
        swap_sample_fmts_on_filter(graph->filters[i]);
 
613
 
 
614
}
 
615
 
 
616
static int pick_formats(AVFilterGraph *graph)
 
617
{
 
618
    int i, j, ret;
211
619
 
212
620
    for (i = 0; i < graph->filter_count; i++) {
213
621
        AVFilterContext *filter = graph->filters[i];
214
622
 
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)
 
625
                return ret;
 
626
        for (j = 0; j < filter->nb_outputs; j++)
 
627
            if ((ret = pick_format(filter->outputs[j])) < 0)
 
628
                return ret;
219
629
    }
 
630
    return 0;
220
631
}
221
632
 
222
 
int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
 
633
/**
 
634
 * Configure the formats of all the links in the graph.
 
635
 */
 
636
static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
223
637
{
224
638
    int ret;
225
639
 
228
642
        return ret;
229
643
 
230
644
    /* Once everything is merged, it's possible that we'll still have
231
 
     * multiple valid media format choices. We pick the first one. */
232
 
    pick_formats(graph);
 
645
     * multiple valid media format choices. We try to minimize the amount
 
646
     * of format conversion inside filters */
 
647
    reduce_formats(graph);
 
648
 
 
649
    /* for audio filters, ensure the best format, sample rate and channel layout
 
650
     * is selected */
 
651
    swap_sample_fmts(graph);
 
652
    swap_samplerates(graph);
 
653
    swap_channel_layouts(graph);
 
654
 
 
655
    if ((ret = pick_formats(graph)) < 0)
 
656
        return ret;
 
657
 
 
658
    return 0;
 
659
}
 
660
 
 
661
static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
 
662
{
 
663
    AVFilterContext *f;
 
664
    int i, j, ret;
 
665
    int fifo_count = 0;
 
666
 
 
667
    for (i = 0; i < graph->filter_count; i++) {
 
668
        f = graph->filters[i];
 
669
 
 
670
        for (j = 0; j < f->nb_inputs; j++) {
 
671
            AVFilterLink *link = f->inputs[j];
 
672
            AVFilterContext *fifo_ctx;
 
673
            AVFilter *fifo;
 
674
            char name[32];
 
675
 
 
676
            if (!link->dstpad->needs_fifo)
 
677
                continue;
 
678
 
 
679
            fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
 
680
                   avfilter_get_by_name("fifo") :
 
681
                   avfilter_get_by_name("afifo");
 
682
 
 
683
            snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
 
684
 
 
685
            ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
 
686
                                               NULL, graph);
 
687
            if (ret < 0)
 
688
                return ret;
 
689
 
 
690
            ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
 
691
            if (ret < 0)
 
692
                return ret;
 
693
        }
 
694
    }
233
695
 
234
696
    return 0;
235
697
}
238
700
{
239
701
    int ret;
240
702
 
241
 
    if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
242
 
        return ret;
243
 
    if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
244
 
        return ret;
245
 
    if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
 
703
    if ((ret = graph_check_validity(graphctx, log_ctx)))
 
704
        return ret;
 
705
    if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
 
706
        return ret;
 
707
    if ((ret = graph_config_formats(graphctx, log_ctx)))
 
708
        return ret;
 
709
    if ((ret = graph_config_links(graphctx, log_ctx)))
246
710
        return ret;
247
711
 
248
712
    return 0;