LCOV - code coverage report
Current view: top level - src/libavfilter - af_amerge.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 141 174 81.0 %
Date: 2017-04-23 07:57:23 Functions: 7 7 100.0 %

          Line data    Source code
       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             : #define FF_INTERNAL_FIELDS 1
      27             : #include "framequeue.h"
      28             : 
      29             : #include "libavutil/avstring.h"
      30             : #include "libavutil/bprint.h"
      31             : #include "libavutil/channel_layout.h"
      32             : #include "libavutil/opt.h"
      33             : #include "avfilter.h"
      34             : #include "audio.h"
      35             : #include "bufferqueue.h"
      36             : #include "internal.h"
      37             : 
      38             : #define SWR_CH_MAX 64
      39             : 
      40             : typedef struct {
      41             :     const AVClass *class;
      42             :     int nb_inputs;
      43             :     int route[SWR_CH_MAX]; /**< channels routing, see copy_samples */
      44             :     int bps;
      45             :     struct amerge_input {
      46             :         struct FFBufQueue queue;
      47             :         int nb_ch;         /**< number of channels for the input */
      48             :         int nb_samples;
      49             :         int pos;
      50             :     } *in;
      51             : } AMergeContext;
      52             : 
      53             : #define OFFSET(x) offsetof(AMergeContext, x)
      54             : #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      55             : 
      56             : static const AVOption amerge_options[] = {
      57             :     { "inputs", "specify the number of inputs", OFFSET(nb_inputs),
      58             :       AV_OPT_TYPE_INT, { .i64 = 2 }, 1, SWR_CH_MAX, FLAGS },
      59             :     { NULL }
      60             : };
      61             : 
      62             : AVFILTER_DEFINE_CLASS(amerge);
      63             : 
      64           2 : static av_cold void uninit(AVFilterContext *ctx)
      65             : {
      66           2 :     AMergeContext *s = ctx->priv;
      67             :     int i;
      68             : 
      69           6 :     for (i = 0; i < s->nb_inputs; i++) {
      70           4 :         if (s->in)
      71           4 :             ff_bufqueue_discard_all(&s->in[i].queue);
      72           4 :         if (ctx->input_pads)
      73           4 :             av_freep(&ctx->input_pads[i].name);
      74             :     }
      75           2 :     av_freep(&s->in);
      76           2 : }
      77             : 
      78           2 : static int query_formats(AVFilterContext *ctx)
      79             : {
      80           2 :     AMergeContext *s = ctx->priv;
      81           2 :     int64_t inlayout[SWR_CH_MAX], outlayout = 0;
      82             :     AVFilterFormats *formats;
      83             :     AVFilterChannelLayouts *layouts;
      84           2 :     int i, ret, overlap = 0, nb_ch = 0;
      85             : 
      86           4 :     for (i = 0; i < s->nb_inputs; i++) {
      87           5 :         if (!ctx->inputs[i]->in_channel_layouts ||
      88           2 :             !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
      89           1 :             av_log(ctx, AV_LOG_WARNING,
      90             :                    "No channel layout for input %d\n", i + 1);
      91           1 :             return AVERROR(EAGAIN);
      92             :         }
      93           2 :         inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
      94           2 :         if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
      95             :             char buf[256];
      96           0 :             av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
      97           0 :             av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
      98             :         }
      99           2 :         s->in[i].nb_ch = FF_LAYOUT2COUNT(inlayout[i]);
     100           2 :         if (s->in[i].nb_ch) {
     101           0 :             overlap++;
     102             :         } else {
     103           2 :             s->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
     104           2 :             if (outlayout & inlayout[i])
     105           1 :                 overlap++;
     106           2 :             outlayout |= inlayout[i];
     107             :         }
     108           2 :         nb_ch += s->in[i].nb_ch;
     109             :     }
     110           1 :     if (nb_ch > SWR_CH_MAX) {
     111           0 :         av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
     112           0 :         return AVERROR(EINVAL);
     113             :     }
     114           1 :     if (overlap) {
     115           1 :         av_log(ctx, AV_LOG_WARNING,
     116             :                "Input channel layouts overlap: "
     117             :                "output layout will be determined by the number of distinct input channels\n");
     118           3 :         for (i = 0; i < nb_ch; i++)
     119           2 :             s->route[i] = i;
     120           1 :         outlayout = av_get_default_channel_layout(nb_ch);
     121           1 :         if (!outlayout && nb_ch)
     122           0 :             outlayout = 0xFFFFFFFFFFFFFFFFULL >> (64 - nb_ch);
     123             :     } else {
     124             :         int *route[SWR_CH_MAX];
     125           0 :         int c, out_ch_number = 0;
     126             : 
     127           0 :         route[0] = s->route;
     128           0 :         for (i = 1; i < s->nb_inputs; i++)
     129           0 :             route[i] = route[i - 1] + s->in[i - 1].nb_ch;
     130           0 :         for (c = 0; c < 64; c++)
     131           0 :             for (i = 0; i < s->nb_inputs; i++)
     132           0 :                 if ((inlayout[i] >> c) & 1)
     133           0 :                     *(route[i]++) = out_ch_number++;
     134             :     }
     135           1 :     formats = ff_make_format_list(ff_packed_sample_fmts_array);
     136           1 :     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
     137           0 :         return ret;
     138           3 :     for (i = 0; i < s->nb_inputs; i++) {
     139           2 :         layouts = NULL;
     140           2 :         if ((ret = ff_add_channel_layout(&layouts, inlayout[i])) < 0)
     141           0 :             return ret;
     142           2 :         if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
     143           0 :             return ret;
     144             :     }
     145           1 :     layouts = NULL;
     146           1 :     if ((ret = ff_add_channel_layout(&layouts, outlayout)) < 0)
     147           0 :         return ret;
     148           1 :     if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
     149           0 :         return ret;
     150             : 
     151           1 :     return ff_set_common_samplerates(ctx, ff_all_samplerates());
     152             : }
     153             : 
     154           1 : static int config_output(AVFilterLink *outlink)
     155             : {
     156           1 :     AVFilterContext *ctx = outlink->src;
     157           1 :     AMergeContext *s = ctx->priv;
     158             :     AVBPrint bp;
     159             :     int i;
     160             : 
     161           2 :     for (i = 1; i < s->nb_inputs; i++) {
     162           1 :         if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
     163           0 :             av_log(ctx, AV_LOG_ERROR,
     164             :                    "Inputs must have the same sample rate "
     165             :                    "%d for in%d vs %d\n",
     166           0 :                    ctx->inputs[i]->sample_rate, i, ctx->inputs[0]->sample_rate);
     167           0 :             return AVERROR(EINVAL);
     168             :         }
     169             :     }
     170           1 :     s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
     171           1 :     outlink->sample_rate = ctx->inputs[0]->sample_rate;
     172           1 :     outlink->time_base   = ctx->inputs[0]->time_base;
     173             : 
     174           1 :     av_bprint_init(&bp, 0, 1);
     175           3 :     for (i = 0; i < s->nb_inputs; i++) {
     176           2 :         av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
     177           2 :         av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
     178             :     }
     179           1 :     av_bprintf(&bp, " -> out:");
     180           1 :     av_bprint_channel_layout(&bp, -1, ctx->outputs[0]->channel_layout);
     181           1 :     av_log(ctx, AV_LOG_VERBOSE, "%s\n", bp.str);
     182             : 
     183           1 :     return 0;
     184             : }
     185             : 
     186         260 : static int request_frame(AVFilterLink *outlink)
     187             : {
     188         260 :     AVFilterContext *ctx = outlink->src;
     189         260 :     AMergeContext *s = ctx->priv;
     190             :     int i, ret;
     191             : 
     192         778 :     for (i = 0; i < s->nb_inputs; i++)
     193         648 :         if (!s->in[i].nb_samples ||
     194             :             /* detect EOF immediately */
     195         129 :             (ctx->inputs[i]->status_in && !ctx->inputs[i]->status_out))
     196         390 :             if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
     197           1 :                 return ret;
     198         259 :     return 0;
     199             : }
     200             : 
     201             : /**
     202             :  * Copy samples from several input streams to one output stream.
     203             :  * @param nb_inputs number of inputs
     204             :  * @param in        inputs; used only for the nb_ch field;
     205             :  * @param route     routing values;
     206             :  *                  input channel i goes to output channel route[i];
     207             :  *                  i <  in[0].nb_ch are the channels from the first output;
     208             :  *                  i >= in[0].nb_ch are the channels from the second output
     209             :  * @param ins       pointer to the samples of each inputs, in packed format;
     210             :  *                  will be left at the end of the copied samples
     211             :  * @param outs      pointer to the samples of the output, in packet format;
     212             :  *                  must point to a buffer big enough;
     213             :  *                  will be left at the end of the copied samples
     214             :  * @param ns        number of samples to copy
     215             :  * @param bps       bytes per sample
     216             :  */
     217         130 : static inline void copy_samples(int nb_inputs, struct amerge_input in[],
     218             :                                 int *route, uint8_t *ins[],
     219             :                                 uint8_t **outs, int ns, int bps)
     220             : {
     221             :     int *route_cur;
     222         130 :     int i, c, nb_ch = 0;
     223             : 
     224         390 :     for (i = 0; i < nb_inputs; i++)
     225         260 :         nb_ch += in[i].nb_ch;
     226      264860 :     while (ns--) {
     227      264600 :         route_cur = route;
     228      793800 :         for (i = 0; i < nb_inputs; i++) {
     229     1058400 :             for (c = 0; c < in[i].nb_ch; c++) {
     230      529200 :                 memcpy((*outs) + bps * *(route_cur++), ins[i], bps);
     231      529200 :                 ins[i] += bps;
     232             :             }
     233             :         }
     234      264600 :         *outs += nb_ch * bps;
     235             :     }
     236         130 : }
     237             : 
     238         260 : static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     239             : {
     240         260 :     AVFilterContext *ctx = inlink->dst;
     241         260 :     AMergeContext *s = ctx->priv;
     242         260 :     AVFilterLink *const outlink = ctx->outputs[0];
     243             :     int input_number;
     244             :     int nb_samples, ns, i;
     245             :     AVFrame *outbuf, *inbuf[SWR_CH_MAX];
     246             :     uint8_t *ins[SWR_CH_MAX], *outs;
     247             : 
     248         390 :     for (input_number = 0; input_number < s->nb_inputs; input_number++)
     249         390 :         if (inlink == ctx->inputs[input_number])
     250         260 :             break;
     251             :     av_assert1(input_number < s->nb_inputs);
     252         260 :     if (ff_bufqueue_is_full(&s->in[input_number].queue)) {
     253           0 :         av_frame_free(&insamples);
     254           0 :         return AVERROR(ENOMEM);
     255             :     }
     256         260 :     ff_bufqueue_add(ctx, &s->in[input_number].queue, av_frame_clone(insamples));
     257         260 :     s->in[input_number].nb_samples += insamples->nb_samples;
     258         260 :     av_frame_free(&insamples);
     259         260 :     nb_samples = s->in[0].nb_samples;
     260         520 :     for (i = 1; i < s->nb_inputs; i++)
     261         260 :         nb_samples = FFMIN(nb_samples, s->in[i].nb_samples);
     262         260 :     if (!nb_samples)
     263         130 :         return 0;
     264             : 
     265         130 :     outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
     266         130 :     if (!outbuf)
     267           0 :         return AVERROR(ENOMEM);
     268         130 :     outs = outbuf->data[0];
     269         390 :     for (i = 0; i < s->nb_inputs; i++) {
     270         260 :         inbuf[i] = ff_bufqueue_peek(&s->in[i].queue, 0);
     271         520 :         ins[i] = inbuf[i]->data[0] +
     272         260 :                  s->in[i].pos * s->in[i].nb_ch * s->bps;
     273             :     }
     274         130 :     av_frame_copy_props(outbuf, inbuf[0]);
     275         260 :     outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
     276         130 :                   inbuf[0]->pts +
     277         130 :                   av_rescale_q(s->in[0].pos,
     278         130 :                                av_make_q(1, ctx->inputs[0]->sample_rate),
     279         130 :                                ctx->outputs[0]->time_base);
     280             : 
     281         130 :     outbuf->nb_samples     = nb_samples;
     282         130 :     outbuf->channel_layout = outlink->channel_layout;
     283         130 :     av_frame_set_channels(outbuf, outlink->channels);
     284             : 
     285         390 :     while (nb_samples) {
     286         130 :         ns = nb_samples;
     287         390 :         for (i = 0; i < s->nb_inputs; i++)
     288         260 :             ns = FFMIN(ns, inbuf[i]->nb_samples - s->in[i].pos);
     289             :         /* Unroll the most common sample formats: speed +~350% for the loop,
     290             :            +~13% overall (including two common decoders) */
     291         130 :         switch (s->bps) {
     292             :             case 1:
     293           0 :                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 1);
     294           0 :                 break;
     295             :             case 2:
     296         130 :                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 2);
     297         130 :                 break;
     298             :             case 4:
     299           0 :                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 4);
     300           0 :                 break;
     301             :             default:
     302           0 :                 copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, s->bps);
     303           0 :                 break;
     304             :         }
     305             : 
     306         130 :         nb_samples -= ns;
     307         390 :         for (i = 0; i < s->nb_inputs; i++) {
     308         260 :             s->in[i].nb_samples -= ns;
     309         260 :             s->in[i].pos += ns;
     310         260 :             if (s->in[i].pos == inbuf[i]->nb_samples) {
     311         260 :                 s->in[i].pos = 0;
     312         260 :                 av_frame_free(&inbuf[i]);
     313         260 :                 ff_bufqueue_get(&s->in[i].queue);
     314         260 :                 inbuf[i] = ff_bufqueue_peek(&s->in[i].queue, 0);
     315         260 :                 ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
     316             :             }
     317             :         }
     318             :     }
     319         130 :     return ff_filter_frame(ctx->outputs[0], outbuf);
     320             : }
     321             : 
     322           2 : static av_cold int init(AVFilterContext *ctx)
     323             : {
     324           2 :     AMergeContext *s = ctx->priv;
     325             :     int i;
     326             : 
     327           2 :     s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
     328           2 :     if (!s->in)
     329           0 :         return AVERROR(ENOMEM);
     330           6 :     for (i = 0; i < s->nb_inputs; i++) {
     331           4 :         char *name = av_asprintf("in%d", i);
     332           4 :         AVFilterPad pad = {
     333             :             .name             = name,
     334             :             .type             = AVMEDIA_TYPE_AUDIO,
     335             :             .filter_frame     = filter_frame,
     336             :         };
     337           4 :         if (!name)
     338           0 :             return AVERROR(ENOMEM);
     339           4 :         ff_insert_inpad(ctx, i, &pad);
     340             :     }
     341           2 :     return 0;
     342             : }
     343             : 
     344             : static const AVFilterPad amerge_outputs[] = {
     345             :     {
     346             :         .name          = "default",
     347             :         .type          = AVMEDIA_TYPE_AUDIO,
     348             :         .config_props  = config_output,
     349             :         .request_frame = request_frame,
     350             :     },
     351             :     { NULL }
     352             : };
     353             : 
     354             : AVFilter ff_af_amerge = {
     355             :     .name          = "amerge",
     356             :     .description   = NULL_IF_CONFIG_SMALL("Merge two or more audio streams into "
     357             :                                           "a single multi-channel stream."),
     358             :     .priv_size     = sizeof(AMergeContext),
     359             :     .init          = init,
     360             :     .uninit        = uninit,
     361             :     .query_formats = query_formats,
     362             :     .inputs        = NULL,
     363             :     .outputs       = amerge_outputs,
     364             :     .priv_class    = &amerge_class,
     365             :     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
     366             : };

Generated by: LCOV version 1.13