GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/af_amerge.c Lines: 125 165 75.8 %
Date: 2020-08-14 10:39:37 Branches: 74 108 68.5 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * Audio merging filter
24
 */
25
26
#include "libavutil/avstring.h"
27
#include "libavutil/bprint.h"
28
#include "libavutil/channel_layout.h"
29
#include "libavutil/opt.h"
30
#include "avfilter.h"
31
#include "filters.h"
32
#include "audio.h"
33
#include "internal.h"
34
35
#define SWR_CH_MAX 64
36
37
typedef struct AMergeContext {
38
    const AVClass *class;
39
    int nb_inputs;
40
    int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
41
    int bps;
42
    struct amerge_input {
43
        int nb_ch;         /**< number of channels for the input */
44
    } *in;
45
} AMergeContext;
46
47
#define OFFSET(x) offsetof(AMergeContext, x)
48
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49
50
static const AVOption amerge_options[] = {
51
    { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
52
      AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
53
    { NULL }
54
};
55
56
AVFILTER_DEFINE_CLASS(amerge);
57
58
2
static av_cold void uninit(AVFilterContext *ctx)
59
{
60
2
    AMergeContext *s = ctx->priv;
61
    int i;
62
63
6
    for (i = 0; i < s->nb_inputs; i++) {
64
4
        if (ctx->input_pads)
65
4
            av_freep(&ctx->input_pads[i].name);
66
    }
67
2
    av_freep(&s->in);
68
2
}
69
70
2
static int query_formats(AVFilterContext *ctx)
71
{
72
2
    AMergeContext *s = ctx->priv;
73
2
    int64_t inlayout[SWR_CH_MAX], outlayout = 0;
74
    AVFilterFormats *formats;
75
    AVFilterChannelLayouts *layouts;
76
2
    int i, ret, overlap = 0, nb_ch = 0;
77
78
4
    for (i = 0; i < s->nb_inputs; i++) {
79
3
        if (!ctx->inputs[i]->in_channel_layouts ||
80
2
            !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
81
1
            av_log(ctx, AV_LOG_WARNING,
82
                   "No channel layout for input %d\n", i + 1);
83
1
            return AVERROR(EAGAIN);
84
        }
85
2
        inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
86
2
        if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
87
            char buf[256];
88
            av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
89
            av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
90
        }
91
2
        s->in[i].nb_ch = FF_LAYOUT2COUNT(inlayout[i]);
92
2
        if (s->in[i].nb_ch) {
93
            overlap++;
94
        } else {
95
2
            s->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
96
2
            if (outlayout & inlayout[i])
97
1
                overlap++;
98
2
            outlayout |= inlayout[i];
99
        }
100
2
        nb_ch += s->in[i].nb_ch;
101
    }
102
1
    if (nb_ch > SWR_CH_MAX) {
103
        av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
104
        return AVERROR(EINVAL);
105
    }
106
1
    if (overlap) {
107
1
        av_log(ctx, AV_LOG_WARNING,
108
               "Input channel layouts overlap: "
109
               "output layout will be determined by the number of distinct input channels\n");
110
3
        for (i = 0; i < nb_ch; i++)
111
2
            s->route[i] = i;
112
1
        outlayout = av_get_default_channel_layout(nb_ch);
113

1
        if (!outlayout && nb_ch)
114
            outlayout = 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch);
115
    } else {
116
        int *route[SWR_CH_MAX];
117
        int c, out_ch_number = 0;
118
119
        route[0] = s->route;
120
        for (i = 1; i < s->nb_inputs; i++)
121
            route[i] = route[i - 1] + s->in[i - 1].nb_ch;
122
        for (c = 0; c < 64; c++)
123
            for (i = 0; i < s->nb_inputs; i++)
124
                if ((inlayout[i] >> c) & 1)
125
                    *(route[i]++) = out_ch_number++;
126
    }
127
1
    formats = ff_make_format_list(ff_packed_sample_fmts_array);
128
1
    if ((ret = ff_set_common_formats(ctx, formats)) < 0)
129
        return ret;
130
3
    for (i = 0; i < s->nb_inputs; i++) {
131
2
        layouts = NULL;
132
2
        if ((ret = ff_add_channel_layout(&layouts, inlayout[i])) < 0)
133
            return ret;
134
2
        if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
135
            return ret;
136
    }
137
1
    layouts = NULL;
138
1
    if ((ret = ff_add_channel_layout(&layouts, outlayout)) < 0)
139
        return ret;
140
1
    if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
141
        return ret;
142
143
1
    return ff_set_common_samplerates(ctx, ff_all_samplerates());
144
}
145
146
1
static int config_output(AVFilterLink *outlink)
147
{
148
1
    AVFilterContext *ctx = outlink->src;
149
1
    AMergeContext *s = ctx->priv;
150
    AVBPrint bp;
151
    int i;
152
153
2
    for (i = 1; i < s->nb_inputs; i++) {
154
1
        if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
155
            av_log(ctx, AV_LOG_ERROR,
156
                   "Inputs must have the same sample rate "
157
                   "%d for in%d vs %d\n",
158
                   ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
159
            return AVERROR(EINVAL);
160
        }
161
    }
162
1
    s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
163
1
    outlink->sample_rate = ctx->inputs[0]->sample_rate;
164
1
    outlink->time_base   = ctx->inputs[0]->time_base;
165
166
1
    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
167
3
    for (i = 0; i < s->nb_inputs; i++) {
168
2
        av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
169
2
        av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
170
    }
171
1
    av_bprintf(&bp, " -> out:");
172
1
    av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
173
1
    av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
174
175
1
    return 0;
176
}
177
178
/**
179
 * Copy samples from several input streams to one output stream.
180
 * @param nb_inputs number of inputs
181
 * @param in        inputs; used only for the nb_ch field;
182
 * @param route     routing values;
183
 *                  input channel i goes to output channel route[i];
184
 *                  i <  in[0].nb_ch are the channels from the first output;
185
 *                  i >= in[0].nb_ch are the channels from the second output
186
 * @param ins       pointer to the samples of each inputs, in packed format;
187
 *                  will be left at the end of the copied samples
188
 * @param outs      pointer to the samples of the output, in packet format;
189
 *                  must point to a buffer big enough;
190
 *                  will be left at the end of the copied samples
191
 * @param ns        number of samples to copy
192
 * @param bps       bytes per sample
193
 */
194
130
static inline void copy_samples(int nb_inputs, struct amerge_input in[],
195
                                int *route, uint8_t *ins[],
196
                                uint8_t **outs, int ns, int bps)
197
{
198
    int *route_cur;
199
130
    int i, c, nb_ch = 0;
200
201
390
    for (i = 0; i < nb_inputs; i++)
202
260
        nb_ch += in[i].nb_ch;
203
264730
    while (ns--) {
204
264600
        route_cur = route;
205
793800
        for (i = 0; i < nb_inputs; i++) {
206
1058400
            for (c = 0; c < in[i].nb_ch; c++) {
207
529200
                memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
208
529200
                ins[i] += bps;
209
            }
210
        }
211
264600
        *outs += nb_ch * bps;
212
    }
213
130
}
214
215
130
static void free_frames(int nb_inputs, AVFrame **input_frames)
216
{
217
    int i;
218
390
    for (i = 0; i < nb_inputs; i++)
219
260
        av_frame_free(&input_frames[i]);
220
130
}
221
222
130
static int try_push_frame(AVFilterContext *ctx, int nb_samples)
223
{
224
130
    AMergeContext *s = ctx->priv;
225
130
    AVFilterLink *outlink = ctx->outputs[0];
226
    int i, ret;
227
130
    AVFrame *outbuf, *inbuf[SWR_CH_MAX] = { NULL };
228
    uint8_t *outs, *ins[SWR_CH_MAX];
229
230
390
    for (i = 0; i < ctx->nb_inputs; i++) {
231
260
        ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &inbuf[i]);
232
260
        if (ret < 0) {
233
            free_frames(i, inbuf);
234
            return ret;
235
        }
236
260
        ins[i] = inbuf[i]->data[0];
237
    }
238
239
130
    outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
240
130
    if (!outbuf) {
241
        free_frames(s->nb_inputs, inbuf);
242
        return AVERROR(ENOMEM);
243
    }
244
245
130
    outs = outbuf->data[0];
246
130
    outbuf->pts = inbuf[0]->pts;
247
248
130
    outbuf->nb_samples     = nb_samples;
249
130
    outbuf->channel_layout = outlink->channel_layout;
250
130
    outbuf->channels       = outlink->channels;
251
252
260
    while (nb_samples) {
253
        /* Unroll the most common sample formats: speed +~350% for the loop,
254
           +~13% overall (including two common decoders) */
255

130
        switch (s->bps) {
256
            case 1:
257
                copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 1);
258
                break;
259
130
            case 2:
260
130
                copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 2);
261
130
                break;
262
            case 4:
263
                copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, 4);
264
                break;
265
            default:
266
                copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, nb_samples, s->bps);
267
                break;
268
        }
269
270
130
        nb_samples = 0;
271
    }
272
273
130
    free_frames(s->nb_inputs, inbuf);
274
130
    return ff_filter_frame(ctx->outputs[0], outbuf);
275
}
276
277
391
static int activate(AVFilterContext *ctx)
278
{
279
    int i, status;
280
    int ret, nb_samples;
281
    int64_t pts;
282
283

393
    FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
284
285
390
    nb_samples = ff_inlink_queued_samples(ctx->inputs[0]);
286

649
    for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
287
259
        nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[i]), nb_samples);
288
    }
289
290
390
    if (nb_samples) {
291
130
        ret = try_push_frame(ctx, nb_samples);
292
130
        if (ret < 0)
293
            return ret;
294
    }
295
296
779
    for (i = 0; i < ctx->nb_inputs; i++) {
297
649
        if (ff_inlink_queued_samples(ctx->inputs[i]))
298
129
            continue;
299
300
520
        if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
301
1
            ff_outlink_set_status(ctx->outputs[0], status, pts);
302
1
            return 0;
303
519
        } else if (ff_outlink_frame_wanted(ctx->outputs[0])) {
304
259
            ff_inlink_request_frame(ctx->inputs[i]);
305
259
            return 0;
306
        }
307
    }
308
309
130
    return 0;
310
}
311
312
2
static av_cold int init(AVFilterContext *ctx)
313
{
314
2
    AMergeContext *s = ctx->priv;
315
    int i, ret;
316
317
2
    s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
318
2
    if (!s->in)
319
        return AVERROR(ENOMEM);
320
6
    for (i = 0; i < s->nb_inputs; i++) {
321
4
        char *name = av_asprintf("in%d", i);
322
4
        AVFilterPad pad = {
323
            .name             = name,
324
            .type             = AVMEDIA_TYPE_AUDIO,
325
        };
326
4
        if (!name)
327
            return AVERROR(ENOMEM);
328
4
        if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
329
            av_freep(&pad.name);
330
            return ret;
331
        }
332
    }
333
2
    return 0;
334
}
335
336
static const AVFilterPad amerge_outputs[] = {
337
    {
338
        .name          = "default",
339
        .type          = AVMEDIA_TYPE_AUDIO,
340
        .config_props  = config_output,
341
    },
342
    { NULL }
343
};
344
345
AVFilter ff_af_amerge = {
346
    .name          = "amerge",
347
    .description   = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
348
                                          "a single multi-channel stream."),
349
    .priv_size     = sizeof(AMergeContext),
350
    .init          = init,
351
    .uninit        = uninit,
352
    .query_formats = query_formats,
353
    .activate      = activate,
354
    .inputs        = NULL,
355
    .outputs       = amerge_outputs,
356
    .priv_class    = &amerge_class,
357
    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
358
};