LCOV - code coverage report
Current view: top level - libavfilter - af_pan.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 178 229 77.7 %
Date: 2018-05-20 11:54:08 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002 Anders Johansson <ajh@atri.curtin.edu.au>
       3             :  * Copyright (c) 2011 Clément Bœsch <u pkh me>
       4             :  * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  * GNU Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Audio panning filter (channels mixing)
      26             :  * Original code written by Anders Johansson for MPlayer,
      27             :  * reimplemented for FFmpeg.
      28             :  */
      29             : 
      30             : #include <stdio.h>
      31             : #include "libavutil/avstring.h"
      32             : #include "libavutil/channel_layout.h"
      33             : #include "libavutil/opt.h"
      34             : #include "libswresample/swresample.h"
      35             : #include "audio.h"
      36             : #include "avfilter.h"
      37             : #include "formats.h"
      38             : #include "internal.h"
      39             : 
      40             : #define MAX_CHANNELS 64
      41             : 
      42             : typedef struct PanContext {
      43             :     const AVClass *class;
      44             :     char *args;
      45             :     int64_t out_channel_layout;
      46             :     double gain[MAX_CHANNELS][MAX_CHANNELS];
      47             :     int64_t need_renorm;
      48             :     int need_renumber;
      49             :     int nb_output_channels;
      50             : 
      51             :     int pure_gains;
      52             :     /* channel mapping specific */
      53             :     int channel_map[MAX_CHANNELS];
      54             :     struct SwrContext *swr;
      55             : } PanContext;
      56             : 
      57          98 : static void skip_spaces(char **arg)
      58             : {
      59          98 :     int len = 0;
      60             : 
      61          98 :     sscanf(*arg, " %n", &len);
      62          98 :     *arg += len;
      63          98 : }
      64             : 
      65          49 : static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
      66             : {
      67             :     char buf[8];
      68          49 :     int len, i, channel_id = 0;
      69             :     int64_t layout, layout0;
      70             : 
      71          49 :     skip_spaces(arg);
      72             :     /* try to parse a channel name, e.g. "FL" */
      73          49 :     if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
      74           8 :         layout0 = layout = av_get_channel_layout(buf);
      75             :         /* channel_id <- first set bit in layout */
      76          56 :         for (i = 32; i > 0; i >>= 1) {
      77          48 :             if (layout >= (int64_t)1 << i) {
      78           4 :                 channel_id += i;
      79           4 :                 layout >>= i;
      80             :             }
      81             :         }
      82             :         /* reject layouts that are not a single channel */
      83           8 :         if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
      84           0 :             return AVERROR(EINVAL);
      85           8 :         *rchannel = channel_id;
      86           8 :         *rnamed = 1;
      87           8 :         *arg += len;
      88           8 :         return 0;
      89             :     }
      90             :     /* try to parse a channel number, e.g. "c2" */
      91          82 :     if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
      92          82 :         channel_id >= 0 && channel_id < MAX_CHANNELS) {
      93          41 :         *rchannel = channel_id;
      94          41 :         *rnamed = 0;
      95          41 :         *arg += len;
      96          41 :         return 0;
      97             :     }
      98           0 :     return AVERROR(EINVAL);
      99             : }
     100             : 
     101          12 : static av_cold int init(AVFilterContext *ctx)
     102             : {
     103          12 :     PanContext *const pan = ctx->priv;
     104          12 :     char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
     105          12 :     int out_ch_id, in_ch_id, len, named, ret, sign = 1;
     106          12 :     int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
     107          12 :     int used_out_ch[MAX_CHANNELS] = {0};
     108             :     double gain;
     109             : 
     110          12 :     if (!pan->args) {
     111           0 :         av_log(ctx, AV_LOG_ERROR,
     112             :                "pan filter needs a channel layout and a set "
     113             :                "of channel definitions as parameter\n");
     114           0 :         return AVERROR(EINVAL);
     115             :     }
     116          12 :     if (!args)
     117           0 :         return AVERROR(ENOMEM);
     118          12 :     arg = av_strtok(args, "|", &tokenizer);
     119          12 :     if (!arg) {
     120           0 :         av_log(ctx, AV_LOG_ERROR, "Channel layout not specified\n");
     121           0 :         ret = AVERROR(EINVAL);
     122           0 :         goto fail;
     123             :     }
     124          12 :     ret = ff_parse_channel_layout(&pan->out_channel_layout,
     125             :                                   &pan->nb_output_channels, arg, ctx);
     126          12 :     if (ret < 0)
     127           0 :         goto fail;
     128             : 
     129             :     /* parse channel specifications */
     130          45 :     while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
     131          21 :         int used_in_ch[MAX_CHANNELS] = {0};
     132             :         /* channel name */
     133          21 :         if (parse_channel_name(&arg, &out_ch_id, &named)) {
     134           0 :             av_log(ctx, AV_LOG_ERROR,
     135             :                    "Expected out channel name, got \"%.8s\"\n", arg);
     136           0 :             ret = AVERROR(EINVAL);
     137           0 :             goto fail;
     138             :         }
     139          21 :         if (named) {
     140           5 :             if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
     141           0 :                 av_log(ctx, AV_LOG_ERROR,
     142             :                        "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
     143           0 :                 ret = AVERROR(EINVAL);
     144           0 :                 goto fail;
     145             :             }
     146             :             /* get the channel number in the output channel layout:
     147             :              * out_channel_layout & ((1 << out_ch_id) - 1) are all the
     148             :              * channels that come before out_ch_id,
     149             :              * so their count is the index of out_ch_id */
     150           5 :             out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
     151             :         }
     152          21 :         if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
     153           0 :             av_log(ctx, AV_LOG_ERROR,
     154             :                    "Invalid out channel name \"%.8s\"\n", arg0);
     155           0 :             ret = AVERROR(EINVAL);
     156           0 :             goto fail;
     157             :         }
     158          21 :         if (used_out_ch[out_ch_id]) {
     159           0 :             av_log(ctx, AV_LOG_ERROR,
     160             :                    "Can not reference out channel %d twice\n", out_ch_id);
     161           0 :             ret = AVERROR(EINVAL);
     162           0 :             goto fail;
     163             :         }
     164          21 :         used_out_ch[out_ch_id] = 1;
     165          21 :         skip_spaces(&arg);
     166          21 :         if (*arg == '=') {
     167          19 :             arg++;
     168           2 :         } else if (*arg == '<') {
     169           2 :             pan->need_renorm |= (int64_t)1 << out_ch_id;
     170           2 :             arg++;
     171             :         } else {
     172           0 :             av_log(ctx, AV_LOG_ERROR,
     173             :                    "Syntax error after channel name in \"%.8s\"\n", arg0);
     174           0 :             ret = AVERROR(EINVAL);
     175           0 :             goto fail;
     176             :         }
     177             :         /* gains */
     178          21 :         sign = 1;
     179             :         while (1) {
     180          35 :             gain = 1;
     181          28 :             if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
     182           8 :                 arg += len;
     183          28 :             if (parse_channel_name(&arg, &in_ch_id, &named)){
     184           0 :                 av_log(ctx, AV_LOG_ERROR,
     185             :                        "Expected in channel name, got \"%.8s\"\n", arg);
     186           0 :                  ret = AVERROR(EINVAL);
     187           0 :                  goto fail;
     188             :             }
     189          28 :             nb_in_channels[named]++;
     190          28 :             if (nb_in_channels[!named]) {
     191           0 :                 av_log(ctx, AV_LOG_ERROR,
     192             :                        "Can not mix named and numbered channels\n");
     193           0 :                 ret = AVERROR(EINVAL);
     194           0 :                 goto fail;
     195             :             }
     196          28 :             if (used_in_ch[in_ch_id]) {
     197           0 :                 av_log(ctx, AV_LOG_ERROR,
     198             :                        "Can not reference in channel %d twice\n", in_ch_id);
     199           0 :                 ret = AVERROR(EINVAL);
     200           0 :                 goto fail;
     201             :             }
     202          28 :             used_in_ch[in_ch_id] = 1;
     203          28 :             pan->gain[out_ch_id][in_ch_id] = sign * gain;
     204          28 :             skip_spaces(&arg);
     205          28 :             if (!*arg)
     206          21 :                 break;
     207           7 :             if (*arg == '-') {
     208           3 :                 sign = -1;
     209           4 :             } else if (*arg != '+') {
     210           0 :                 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
     211           0 :                 ret = AVERROR(EINVAL);
     212           0 :                 goto fail;
     213             :             } else {
     214           4 :                 sign = 1;
     215             :             }
     216           7 :             arg++;
     217             :         }
     218             :     }
     219          12 :     pan->need_renumber = !!nb_in_channels[1];
     220             : 
     221          12 :     ret = 0;
     222          12 : fail:
     223          12 :     av_free(args);
     224          12 :     return ret;
     225             : }
     226             : 
     227          12 : static int are_gains_pure(const PanContext *pan)
     228             : {
     229             :     int i, j;
     230             : 
     231         524 :     for (i = 0; i < MAX_CHANNELS; i++) {
     232         516 :         int nb_gain = 0;
     233             : 
     234       33288 :         for (j = 0; j < MAX_CHANNELS; j++) {
     235       32776 :             double gain = pan->gain[i][j];
     236             : 
     237             :             /* channel mapping is effective only if 0% or 100% of a channel is
     238             :              * selected... */
     239       32776 :             if (gain != 0. && gain != 1.)
     240           3 :                 return 0;
     241             :             /* ...and if the output channel is only composed of one input */
     242       32773 :             if (gain && nb_gain++)
     243           1 :                 return 0;
     244             :         }
     245             :     }
     246           8 :     return 1;
     247             : }
     248             : 
     249          12 : static int query_formats(AVFilterContext *ctx)
     250             : {
     251          12 :     PanContext *pan = ctx->priv;
     252          12 :     AVFilterLink *inlink  = ctx->inputs[0];
     253          12 :     AVFilterLink *outlink = ctx->outputs[0];
     254          12 :     AVFilterFormats *formats = NULL;
     255             :     AVFilterChannelLayouts *layouts;
     256             :     int ret;
     257             : 
     258          12 :     pan->pure_gains = are_gains_pure(pan);
     259             :     /* libswr supports any sample and packing formats */
     260          12 :     if ((ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO))) < 0)
     261           0 :         return ret;
     262             : 
     263          12 :     formats = ff_all_samplerates();
     264          12 :     if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
     265           0 :         return ret;
     266             : 
     267             :     // inlink supports any channel layout
     268          12 :     layouts = ff_all_channel_counts();
     269          12 :     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
     270           0 :         return ret;
     271             : 
     272             :     // outlink supports only requested output channel layout
     273          12 :     layouts = NULL;
     274          24 :     if ((ret = ff_add_channel_layout(&layouts,
     275          22 :                           pan->out_channel_layout ? pan->out_channel_layout :
     276           2 :                           FF_COUNT2LAYOUT(pan->nb_output_channels))) < 0)
     277           0 :         return ret;
     278          12 :     return ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
     279             : }
     280             : 
     281          12 : static int config_props(AVFilterLink *link)
     282             : {
     283          12 :     AVFilterContext *ctx = link->dst;
     284          12 :     PanContext *pan = ctx->priv;
     285             :     char buf[1024], *cur;
     286             :     int i, j, k, r;
     287             :     double t;
     288             : 
     289          12 :     if (pan->need_renumber) {
     290             :         // input channels were given by their name: renumber them
     291         130 :         for (i = j = 0; i < MAX_CHANNELS; i++) {
     292         128 :             if ((link->channel_layout >> i) & 1) {
     293          13 :                 for (k = 0; k < pan->nb_output_channels; k++)
     294           8 :                     pan->gain[k][j] = pan->gain[k][i];
     295           5 :                 j++;
     296             :             }
     297             :         }
     298             :     }
     299             : 
     300             :     // sanity check; can't be done in query_formats since the inlink
     301             :     // channel layout is unknown at that time
     302          24 :     if (link->channels > MAX_CHANNELS ||
     303          12 :         pan->nb_output_channels > MAX_CHANNELS) {
     304           0 :         av_log(ctx, AV_LOG_ERROR,
     305             :                "af_pan supports a maximum of %d channels. "
     306             :                "Feel free to ask for a higher limit.\n", MAX_CHANNELS);
     307           0 :         return AVERROR_PATCHWELCOME;
     308             :     }
     309             : 
     310             :     // init libswresample context
     311          36 :     pan->swr = swr_alloc_set_opts(pan->swr,
     312          12 :                                   pan->out_channel_layout, link->format, link->sample_rate,
     313          12 :                                   link->channel_layout,    link->format, link->sample_rate,
     314             :                                   0, ctx);
     315          12 :     if (!pan->swr)
     316           0 :         return AVERROR(ENOMEM);
     317          12 :     if (!link->channel_layout) {
     318           1 :         if (av_opt_set_int(pan->swr, "ich", link->channels, 0) < 0)
     319           0 :             return AVERROR(EINVAL);
     320             :     }
     321          12 :     if (!pan->out_channel_layout) {
     322           2 :         if (av_opt_set_int(pan->swr, "och", pan->nb_output_channels, 0) < 0)
     323           0 :             return AVERROR(EINVAL);
     324             :     }
     325             : 
     326             :     // gains are pure, init the channel mapping
     327          12 :     if (pan->pure_gains) {
     328             : 
     329             :         // get channel map from the pure gains
     330          21 :         for (i = 0; i < pan->nb_output_channels; i++) {
     331          13 :             int ch_id = -1;
     332          21 :             for (j = 0; j < link->channels; j++) {
     333          19 :                 if (pan->gain[i][j]) {
     334          11 :                     ch_id = j;
     335          11 :                     break;
     336             :                 }
     337             :             }
     338          13 :             pan->channel_map[i] = ch_id;
     339             :         }
     340             : 
     341           8 :         av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
     342           8 :         av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
     343           8 :         swr_set_channel_mapping(pan->swr, pan->channel_map);
     344             :     } else {
     345             :         // renormalize
     346          13 :         for (i = 0; i < pan->nb_output_channels; i++) {
     347           9 :             if (!((pan->need_renorm >> i) & 1))
     348           7 :                 continue;
     349           2 :             t = 0;
     350           6 :             for (j = 0; j < link->channels; j++)
     351           4 :                 t += fabs(pan->gain[i][j]);
     352           2 :             if (t > -1E-5 && t < 1E-5) {
     353             :                 // t is almost 0 but not exactly, this is probably a mistake
     354           0 :                 if (t)
     355           0 :                     av_log(ctx, AV_LOG_WARNING,
     356             :                            "Degenerate coefficients while renormalizing\n");
     357           0 :                 continue;
     358             :             }
     359           6 :             for (j = 0; j < link->channels; j++)
     360           4 :                 pan->gain[i][j] /= t;
     361             :         }
     362           4 :         av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
     363           4 :         av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
     364           4 :         swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
     365             :     }
     366             : 
     367          12 :     r = swr_init(pan->swr);
     368          12 :     if (r < 0)
     369           0 :         return r;
     370             : 
     371             :     // summary
     372          34 :     for (i = 0; i < pan->nb_output_channels; i++) {
     373          22 :         cur = buf;
     374          86 :         for (j = 0; j < link->channels; j++) {
     375          64 :             r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
     376             :                          j ? " + " : "", pan->gain[i][j], j);
     377          64 :             cur += FFMIN(buf + sizeof(buf) - cur, r);
     378             :         }
     379          22 :         av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
     380             :     }
     381             :     // add channel mapping summary if possible
     382          12 :     if (pan->pure_gains) {
     383           8 :         av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
     384          21 :         for (i = 0; i < pan->nb_output_channels; i++)
     385          13 :             if (pan->channel_map[i] < 0)
     386           2 :                 av_log(ctx, AV_LOG_INFO, " M");
     387             :             else
     388          11 :                 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
     389           8 :         av_log(ctx, AV_LOG_INFO, "\n");
     390           8 :         return 0;
     391             :     }
     392           4 :     return 0;
     393             : }
     394             : 
     395        1996 : static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     396             : {
     397             :     int ret;
     398        1996 :     int n = insamples->nb_samples;
     399        1996 :     AVFilterLink *const outlink = inlink->dst->outputs[0];
     400        1996 :     AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
     401        1996 :     PanContext *pan = inlink->dst->priv;
     402             : 
     403        1996 :     if (!outsamples) {
     404           0 :         av_frame_free(&insamples);
     405           0 :         return AVERROR(ENOMEM);
     406             :     }
     407        1996 :     swr_convert(pan->swr, outsamples->extended_data, n,
     408        1996 :                 (void *)insamples->extended_data, n);
     409        1996 :     av_frame_copy_props(outsamples, insamples);
     410        1996 :     outsamples->channel_layout = outlink->channel_layout;
     411        1996 :     outsamples->channels = outlink->channels;
     412             : 
     413        1996 :     ret = ff_filter_frame(outlink, outsamples);
     414        1996 :     av_frame_free(&insamples);
     415        1996 :     return ret;
     416             : }
     417             : 
     418          12 : static av_cold void uninit(AVFilterContext *ctx)
     419             : {
     420          12 :     PanContext *pan = ctx->priv;
     421          12 :     swr_free(&pan->swr);
     422          12 : }
     423             : 
     424             : #define OFFSET(x) offsetof(PanContext, x)
     425             : 
     426             : static const AVOption pan_options[] = {
     427             :     { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
     428             :     { NULL }
     429             : };
     430             : 
     431             : AVFILTER_DEFINE_CLASS(pan);
     432             : 
     433             : static const AVFilterPad pan_inputs[] = {
     434             :     {
     435             :         .name         = "default",
     436             :         .type         = AVMEDIA_TYPE_AUDIO,
     437             :         .config_props = config_props,
     438             :         .filter_frame = filter_frame,
     439             :     },
     440             :     { NULL }
     441             : };
     442             : 
     443             : static const AVFilterPad pan_outputs[] = {
     444             :     {
     445             :         .name = "default",
     446             :         .type = AVMEDIA_TYPE_AUDIO,
     447             :     },
     448             :     { NULL }
     449             : };
     450             : 
     451             : AVFilter ff_af_pan = {
     452             :     .name          = "pan",
     453             :     .description   = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
     454             :     .priv_size     = sizeof(PanContext),
     455             :     .priv_class    = &pan_class,
     456             :     .init          = init,
     457             :     .uninit        = uninit,
     458             :     .query_formats = query_formats,
     459             :     .inputs        = pan_inputs,
     460             :     .outputs       = pan_outputs,
     461             : };

Generated by: LCOV version 1.13