~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavfilter/af_join.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * This file is part of Libav.
 
4
 *
 
5
 * Libav is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * Libav is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with Libav; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 */
 
19
 
 
20
/**
 
21
 * @file
 
22
 * Audio join filter
 
23
 *
 
24
 * Join multiple audio inputs as different channels in
 
25
 * a single output
 
26
 */
 
27
 
 
28
#include "libavutil/avassert.h"
 
29
#include "libavutil/channel_layout.h"
 
30
#include "libavutil/common.h"
 
31
#include "libavutil/opt.h"
 
32
 
 
33
#include "audio.h"
 
34
#include "avfilter.h"
 
35
#include "formats.h"
 
36
#include "internal.h"
 
37
 
 
38
typedef struct ChannelMap {
 
39
    int input;                ///< input stream index
 
40
    int       in_channel_idx; ///< index of in_channel in the input stream data
 
41
    uint64_t  in_channel;     ///< layout describing the input channel
 
42
    uint64_t out_channel;     ///< layout describing the output channel
 
43
} ChannelMap;
 
44
 
 
45
typedef struct JoinContext {
 
46
    const AVClass *class;
 
47
 
 
48
    int inputs;
 
49
    char *map;
 
50
    char    *channel_layout_str;
 
51
    uint64_t channel_layout;
 
52
 
 
53
    int      nb_channels;
 
54
    ChannelMap *channels;
 
55
 
 
56
    /**
 
57
     * Temporary storage for input frames, until we get one on each input.
 
58
     */
 
59
    AVFilterBufferRef **input_frames;
 
60
 
 
61
    /**
 
62
     *  Temporary storage for data pointers, for assembling the output buffer.
 
63
     */
 
64
    uint8_t **data;
 
65
} JoinContext;
 
66
 
 
67
/**
 
68
 * To avoid copying the data from input buffers, this filter creates
 
69
 * a custom output buffer that stores references to all inputs and
 
70
 * unrefs them on free.
 
71
 */
 
72
typedef struct JoinBufferPriv {
 
73
    AVFilterBufferRef **in_buffers;
 
74
    int              nb_in_buffers;
 
75
} JoinBufferPriv;
 
76
 
 
77
#define OFFSET(x) offsetof(JoinContext, x)
 
78
#define A AV_OPT_FLAG_AUDIO_PARAM
 
79
static const AVOption join_options[] = {
 
80
    { "inputs",         "Number of input streams.", OFFSET(inputs),             AV_OPT_TYPE_INT,    { .i64 = 2 }, 1, INT_MAX,       A },
 
81
    { "channel_layout", "Channel layout of the "
 
82
                        "output stream.",           OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A },
 
83
    { "map",            "A comma-separated list of channels maps in the format "
 
84
                        "'input_stream.input_channel-output_channel.",
 
85
                                                    OFFSET(map),                AV_OPT_TYPE_STRING,                 .flags = A },
 
86
    { NULL },
 
87
};
 
88
 
 
89
static const AVClass join_class = {
 
90
    .class_name = "join filter",
 
91
    .item_name  = av_default_item_name,
 
92
    .option     = join_options,
 
93
    .version    = LIBAVUTIL_VERSION_INT,
 
94
};
 
95
 
 
96
static int filter_frame(AVFilterLink *link, AVFilterBufferRef *buf)
 
97
{
 
98
    AVFilterContext *ctx = link->dst;
 
99
    JoinContext       *s = ctx->priv;
 
100
    int i;
 
101
 
 
102
    for (i = 0; i < ctx->nb_inputs; i++)
 
103
        if (link == ctx->inputs[i])
 
104
            break;
 
105
    av_assert0(i < ctx->nb_inputs);
 
106
    av_assert0(!s->input_frames[i]);
 
107
    s->input_frames[i] = buf;
 
108
 
 
109
    return 0;
 
110
}
 
111
 
 
112
static int parse_maps(AVFilterContext *ctx)
 
113
{
 
114
    JoinContext *s = ctx->priv;
 
115
    char *cur      = s->map;
 
116
 
 
117
    while (cur && *cur) {
 
118
        char *sep, *next, *p;
 
119
        uint64_t in_channel = 0, out_channel = 0;
 
120
        int input_idx, out_ch_idx, in_ch_idx;
 
121
 
 
122
        next = strchr(cur, ',');
 
123
        if (next)
 
124
            *next++ = 0;
 
125
 
 
126
        /* split the map into input and output parts */
 
127
        if (!(sep = strchr(cur, '-'))) {
 
128
            av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
 
129
                   "map '%s'\n", cur);
 
130
            return AVERROR(EINVAL);
 
131
        }
 
132
        *sep++ = 0;
 
133
 
 
134
#define PARSE_CHANNEL(str, var, inout)                                         \
 
135
        if (!(var = av_get_channel_layout(str))) {                             \
 
136
            av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
 
137
            return AVERROR(EINVAL);                                            \
 
138
        }                                                                      \
 
139
        if (av_get_channel_layout_nb_channels(var) != 1) {                     \
 
140
            av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one "   \
 
141
                   inout " channel.\n");                                       \
 
142
            return AVERROR(EINVAL);                                            \
 
143
        }
 
144
 
 
145
        /* parse output channel */
 
146
        PARSE_CHANNEL(sep, out_channel, "output");
 
147
        if (!(out_channel & s->channel_layout)) {
 
148
            av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
 
149
                   "requested channel layout.\n", sep);
 
150
            return AVERROR(EINVAL);
 
151
        }
 
152
 
 
153
        out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
 
154
                                                         out_channel);
 
155
        if (s->channels[out_ch_idx].input >= 0) {
 
156
            av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
 
157
                   "'%s'.\n", sep);
 
158
            return AVERROR(EINVAL);
 
159
        }
 
160
 
 
161
        /* parse input channel */
 
162
        input_idx = strtol(cur, &cur, 0);
 
163
        if (input_idx < 0 || input_idx >= s->inputs) {
 
164
            av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
 
165
                   input_idx);
 
166
            return AVERROR(EINVAL);
 
167
        }
 
168
 
 
169
        if (*cur)
 
170
            cur++;
 
171
 
 
172
        in_ch_idx = strtol(cur, &p, 0);
 
173
        if (p == cur) {
 
174
            /* channel specifier is not a number,
 
175
             * try to parse as channel name */
 
176
            PARSE_CHANNEL(cur, in_channel, "input");
 
177
        }
 
178
 
 
179
        s->channels[out_ch_idx].input      = input_idx;
 
180
        if (in_channel)
 
181
            s->channels[out_ch_idx].in_channel = in_channel;
 
182
        else
 
183
            s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
 
184
 
 
185
        cur = next;
 
186
    }
 
187
    return 0;
 
188
}
 
189
 
 
190
static int join_init(AVFilterContext *ctx, const char *args)
 
191
{
 
192
    JoinContext *s = ctx->priv;
 
193
    int ret, i;
 
194
 
 
195
    s->class = &join_class;
 
196
    av_opt_set_defaults(s);
 
197
    if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
 
198
        av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
 
199
        return ret;
 
200
    }
 
201
 
 
202
    if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
 
203
        av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
 
204
               s->channel_layout_str);
 
205
        ret = AVERROR(EINVAL);
 
206
        goto fail;
 
207
    }
 
208
 
 
209
    s->nb_channels  = av_get_channel_layout_nb_channels(s->channel_layout);
 
210
    s->channels     = av_mallocz(sizeof(*s->channels) * s->nb_channels);
 
211
    s->data         = av_mallocz(sizeof(*s->data)     * s->nb_channels);
 
212
    s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
 
213
    if (!s->channels || !s->data || !s->input_frames) {
 
214
        ret = AVERROR(ENOMEM);
 
215
        goto fail;
 
216
    }
 
217
 
 
218
    for (i = 0; i < s->nb_channels; i++) {
 
219
        s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
 
220
        s->channels[i].input       = -1;
 
221
    }
 
222
 
 
223
    if ((ret = parse_maps(ctx)) < 0)
 
224
        goto fail;
 
225
 
 
226
    for (i = 0; i < s->inputs; i++) {
 
227
        char name[32];
 
228
        AVFilterPad pad = { 0 };
 
229
 
 
230
        snprintf(name, sizeof(name), "input%d", i);
 
231
        pad.type           = AVMEDIA_TYPE_AUDIO;
 
232
        pad.name           = av_strdup(name);
 
233
        pad.filter_frame   = filter_frame;
 
234
 
 
235
        pad.needs_fifo = 1;
 
236
 
 
237
        ff_insert_inpad(ctx, i, &pad);
 
238
    }
 
239
 
 
240
fail:
 
241
    av_opt_free(s);
 
242
    return ret;
 
243
}
 
244
 
 
245
static void join_uninit(AVFilterContext *ctx)
 
246
{
 
247
    JoinContext *s = ctx->priv;
 
248
    int i;
 
249
 
 
250
    for (i = 0; i < ctx->nb_inputs; i++) {
 
251
        av_freep(&ctx->input_pads[i].name);
 
252
        avfilter_unref_bufferp(&s->input_frames[i]);
 
253
    }
 
254
 
 
255
    av_freep(&s->channels);
 
256
    av_freep(&s->data);
 
257
    av_freep(&s->input_frames);
 
258
}
 
259
 
 
260
static int join_query_formats(AVFilterContext *ctx)
 
261
{
 
262
    JoinContext *s = ctx->priv;
 
263
    AVFilterChannelLayouts *layouts = NULL;
 
264
    int i;
 
265
 
 
266
    ff_add_channel_layout(&layouts, s->channel_layout);
 
267
    ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
 
268
 
 
269
    for (i = 0; i < ctx->nb_inputs; i++)
 
270
        ff_channel_layouts_ref(ff_all_channel_layouts(),
 
271
                               &ctx->inputs[i]->out_channel_layouts);
 
272
 
 
273
    ff_set_common_formats    (ctx, ff_planar_sample_fmts());
 
274
    ff_set_common_samplerates(ctx, ff_all_samplerates());
 
275
 
 
276
    return 0;
 
277
}
 
278
 
 
279
static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
 
280
                               uint64_t *inputs)
 
281
{
 
282
    int i;
 
283
 
 
284
    for (i = 0; i < ctx->nb_inputs; i++) {
 
285
        AVFilterLink *link = ctx->inputs[i];
 
286
 
 
287
        if (ch->out_channel & link->channel_layout &&
 
288
            !(ch->out_channel & inputs[i])) {
 
289
            ch->input      = i;
 
290
            ch->in_channel = ch->out_channel;
 
291
            inputs[i]     |= ch->out_channel;
 
292
            return;
 
293
        }
 
294
    }
 
295
}
 
296
 
 
297
static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
 
298
                          uint64_t *inputs)
 
299
{
 
300
    int i;
 
301
 
 
302
    for (i = 0; i < ctx->nb_inputs; i++) {
 
303
        AVFilterLink *link = ctx->inputs[i];
 
304
 
 
305
        if ((inputs[i] & link->channel_layout) != link->channel_layout) {
 
306
            uint64_t unused = link->channel_layout & ~inputs[i];
 
307
 
 
308
            ch->input      = i;
 
309
            ch->in_channel = av_channel_layout_extract_channel(unused, 0);
 
310
            inputs[i]     |= ch->in_channel;
 
311
            return;
 
312
        }
 
313
    }
 
314
}
 
315
 
 
316
static int join_config_output(AVFilterLink *outlink)
 
317
{
 
318
    AVFilterContext *ctx = outlink->src;
 
319
    JoinContext       *s = ctx->priv;
 
320
    uint64_t *inputs;   // nth element tracks which channels are used from nth input
 
321
    int i, ret = 0;
 
322
 
 
323
    /* initialize inputs to user-specified mappings */
 
324
    if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
 
325
        return AVERROR(ENOMEM);
 
326
    for (i = 0; i < s->nb_channels; i++) {
 
327
        ChannelMap *ch = &s->channels[i];
 
328
        AVFilterLink *inlink;
 
329
 
 
330
        if (ch->input < 0)
 
331
            continue;
 
332
 
 
333
        inlink = ctx->inputs[ch->input];
 
334
 
 
335
        if (!ch->in_channel)
 
336
            ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
 
337
                                                               ch->in_channel_idx);
 
338
 
 
339
        if (!(ch->in_channel & inlink->channel_layout)) {
 
340
            av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
 
341
                   "input stream #%d.\n", av_get_channel_name(ch->in_channel),
 
342
                   ch->input);
 
343
            ret = AVERROR(EINVAL);
 
344
            goto fail;
 
345
        }
 
346
 
 
347
        inputs[ch->input] |= ch->in_channel;
 
348
    }
 
349
 
 
350
    /* guess channel maps when not explicitly defined */
 
351
    /* first try unused matching channels */
 
352
    for (i = 0; i < s->nb_channels; i++) {
 
353
        ChannelMap *ch = &s->channels[i];
 
354
 
 
355
        if (ch->input < 0)
 
356
            guess_map_matching(ctx, ch, inputs);
 
357
    }
 
358
 
 
359
    /* if the above failed, try to find _any_ unused input channel */
 
360
    for (i = 0; i < s->nb_channels; i++) {
 
361
        ChannelMap *ch = &s->channels[i];
 
362
 
 
363
        if (ch->input < 0)
 
364
            guess_map_any(ctx, ch, inputs);
 
365
 
 
366
        if (ch->input < 0) {
 
367
            av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
 
368
                   "output channel '%s'.\n",
 
369
                   av_get_channel_name(ch->out_channel));
 
370
            goto fail;
 
371
        }
 
372
 
 
373
        ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
 
374
                                                                 ch->in_channel);
 
375
    }
 
376
 
 
377
    /* print mappings */
 
378
    av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
 
379
    for (i = 0; i < s->nb_channels; i++) {
 
380
        ChannelMap *ch = &s->channels[i];
 
381
        av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
 
382
               av_get_channel_name(ch->in_channel),
 
383
               av_get_channel_name(ch->out_channel));
 
384
    }
 
385
    av_log(ctx, AV_LOG_VERBOSE, "\n");
 
386
 
 
387
    for (i = 0; i < ctx->nb_inputs; i++) {
 
388
        if (!inputs[i])
 
389
            av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
 
390
                   "stream %d.\n", i);
 
391
    }
 
392
 
 
393
fail:
 
394
    av_freep(&inputs);
 
395
    return ret;
 
396
}
 
397
 
 
398
static void join_free_buffer(AVFilterBuffer *buf)
 
399
{
 
400
    JoinBufferPriv *priv = buf->priv;
 
401
 
 
402
    if (priv) {
 
403
        int i;
 
404
 
 
405
        for (i = 0; i < priv->nb_in_buffers; i++)
 
406
            avfilter_unref_bufferp(&priv->in_buffers[i]);
 
407
 
 
408
        av_freep(&priv->in_buffers);
 
409
        av_freep(&buf->priv);
 
410
    }
 
411
 
 
412
    if (buf->extended_data != buf->data)
 
413
        av_freep(&buf->extended_data);
 
414
    av_freep(&buf);
 
415
}
 
416
 
 
417
static int join_request_frame(AVFilterLink *outlink)
 
418
{
 
419
    AVFilterContext *ctx = outlink->src;
 
420
    JoinContext *s       = ctx->priv;
 
421
    AVFilterBufferRef *buf;
 
422
    JoinBufferPriv *priv;
 
423
    int linesize   = INT_MAX;
 
424
    int perms      = ~0;
 
425
    int nb_samples = 0;
 
426
    int i, j, ret;
 
427
 
 
428
    /* get a frame on each input */
 
429
    for (i = 0; i < ctx->nb_inputs; i++) {
 
430
        AVFilterLink *inlink = ctx->inputs[i];
 
431
 
 
432
        if (!s->input_frames[i] &&
 
433
            (ret = ff_request_frame(inlink)) < 0)
 
434
            return ret;
 
435
 
 
436
        /* request the same number of samples on all inputs */
 
437
        if (i == 0) {
 
438
            nb_samples = s->input_frames[0]->audio->nb_samples;
 
439
 
 
440
            for (j = 1; !i && j < ctx->nb_inputs; j++)
 
441
                ctx->inputs[j]->request_samples = nb_samples;
 
442
        }
 
443
    }
 
444
 
 
445
    for (i = 0; i < s->nb_channels; i++) {
 
446
        ChannelMap *ch = &s->channels[i];
 
447
        AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
 
448
 
 
449
        s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
 
450
        linesize   = FFMIN(linesize, cur_buf->linesize[0]);
 
451
        perms     &= cur_buf->perms;
 
452
    }
 
453
 
 
454
    av_assert0(nb_samples > 0);
 
455
    buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
 
456
                                                    nb_samples, outlink->format,
 
457
                                                    outlink->channel_layout);
 
458
    if (!buf)
 
459
        return AVERROR(ENOMEM);
 
460
 
 
461
    buf->buf->free = join_free_buffer;
 
462
    buf->pts       = s->input_frames[0]->pts;
 
463
 
 
464
    if (!(priv = av_mallocz(sizeof(*priv))))
 
465
        goto fail;
 
466
    if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
 
467
        goto fail;
 
468
 
 
469
    for (i = 0; i < ctx->nb_inputs; i++)
 
470
        priv->in_buffers[i] = s->input_frames[i];
 
471
    priv->nb_in_buffers = ctx->nb_inputs;
 
472
    buf->buf->priv      = priv;
 
473
 
 
474
    ret = ff_filter_frame(outlink, buf);
 
475
 
 
476
    memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
 
477
 
 
478
    return ret;
 
479
 
 
480
fail:
 
481
    avfilter_unref_buffer(buf);
 
482
    if (priv)
 
483
        av_freep(&priv->in_buffers);
 
484
    av_freep(&priv);
 
485
    return AVERROR(ENOMEM);
 
486
}
 
487
 
 
488
static const AVFilterPad avfilter_af_join_outputs[] = {
 
489
    {
 
490
        .name          = "default",
 
491
        .type          = AVMEDIA_TYPE_AUDIO,
 
492
        .config_props  = join_config_output,
 
493
        .request_frame = join_request_frame,
 
494
    },
 
495
    { NULL }
 
496
};
 
497
 
 
498
AVFilter avfilter_af_join = {
 
499
    .name           = "join",
 
500
    .description    = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
 
501
                                           "multi-channel output"),
 
502
    .priv_size      = sizeof(JoinContext),
 
503
 
 
504
    .init           = join_init,
 
505
    .uninit         = join_uninit,
 
506
    .query_formats  = join_query_formats,
 
507
 
 
508
    .inputs  = NULL,
 
509
    .outputs = avfilter_af_join_outputs,
 
510
};