LCOV - code coverage report
Current view: top level - src/libavfilter - af_pan.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 126 214 58.9 %
Date: 2017-03-25 17:02:41 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          20 : static void skip_spaces(char **arg)
      58             : {
      59          20 :     int len = 0;
      60             : 
      61          20 :     sscanf(*arg, " %n", &len);
      62          20 :     *arg += len;
      63          20 : }
      64             : 
      65          10 : static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
      66             : {
      67             :     char buf[8];
      68          10 :     int len, i, channel_id = 0;
      69             :     int64_t layout, layout0;
      70             : 
      71          10 :     skip_spaces(arg);
      72             :     /* try to parse a channel name, e.g. "FL" */
      73          10 :     if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
      74           0 :         layout0 = layout = av_get_channel_layout(buf);
      75             :         /* channel_id <- first set bit in layout */
      76           0 :         for (i = 32; i > 0; i >>= 1) {
      77           0 :             if (layout >= (int64_t)1 << i) {
      78           0 :                 channel_id += i;
      79           0 :                 layout >>= i;
      80             :             }
      81             :         }
      82             :         /* reject layouts that are not a single channel */
      83           0 :         if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
      84           0 :             return AVERROR(EINVAL);
      85           0 :         *rchannel = channel_id;
      86           0 :         *rnamed = 1;
      87           0 :         *arg += len;
      88           0 :         return 0;
      89             :     }
      90             :     /* try to parse a channel number, e.g. "c2" */
      91          20 :     if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
      92          20 :         channel_id >= 0 && channel_id < MAX_CHANNELS) {
      93          10 :         *rchannel = channel_id;
      94          10 :         *rnamed = 0;
      95          10 :         *arg += len;
      96          10 :         return 0;
      97             :     }
      98           0 :     return AVERROR(EINVAL);
      99             : }
     100             : 
     101           4 : static av_cold int init(AVFilterContext *ctx)
     102             : {
     103           4 :     PanContext *const pan = ctx->priv;
     104           4 :     char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
     105           4 :     int out_ch_id, in_ch_id, len, named, ret, sign = 1;
     106           4 :     int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
     107             :     double gain;
     108             : 
     109           4 :     if (!pan->args) {
     110           0 :         av_log(ctx, AV_LOG_ERROR,
     111             :                "pan filter needs a channel layout and a set "
     112             :                "of channel definitions as parameter\n");
     113           0 :         return AVERROR(EINVAL);
     114             :     }
     115           4 :     if (!args)
     116           0 :         return AVERROR(ENOMEM);
     117           4 :     arg = av_strtok(args, "|", &tokenizer);
     118           4 :     if (!arg) {
     119           0 :         av_log(ctx, AV_LOG_ERROR, "Channel layout not specified\n");
     120           0 :         ret = AVERROR(EINVAL);
     121           0 :         goto fail;
     122             :     }
     123           4 :     ret = ff_parse_channel_layout(&pan->out_channel_layout,
     124             :                                   &pan->nb_output_channels, arg, ctx);
     125           4 :     if (ret < 0)
     126           0 :         goto fail;
     127             : 
     128             :     /* parse channel specifications */
     129          13 :     while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
     130             :         /* channel name */
     131           5 :         if (parse_channel_name(&arg, &out_ch_id, &named)) {
     132           0 :             av_log(ctx, AV_LOG_ERROR,
     133             :                    "Expected out channel name, got \"%.8s\"\n", arg);
     134           0 :             ret = AVERROR(EINVAL);
     135           0 :             goto fail;
     136             :         }
     137           5 :         if (named) {
     138           0 :             if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
     139           0 :                 av_log(ctx, AV_LOG_ERROR,
     140             :                        "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
     141           0 :                 ret = AVERROR(EINVAL);
     142           0 :                 goto fail;
     143             :             }
     144             :             /* get the channel number in the output channel layout:
     145             :              * out_channel_layout & ((1 << out_ch_id) - 1) are all the
     146             :              * channels that come before out_ch_id,
     147             :              * so their count is the index of out_ch_id */
     148           0 :             out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
     149             :         }
     150           5 :         if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
     151           0 :             av_log(ctx, AV_LOG_ERROR,
     152             :                    "Invalid out channel name \"%.8s\"\n", arg0);
     153           0 :             ret = AVERROR(EINVAL);
     154           0 :             goto fail;
     155             :         }
     156           5 :         skip_spaces(&arg);
     157           5 :         if (*arg == '=') {
     158           5 :             arg++;
     159           0 :         } else if (*arg == '<') {
     160           0 :             pan->need_renorm |= (int64_t)1 << out_ch_id;
     161           0 :             arg++;
     162             :         } else {
     163           0 :             av_log(ctx, AV_LOG_ERROR,
     164             :                    "Syntax error after channel name in \"%.8s\"\n", arg0);
     165           0 :             ret = AVERROR(EINVAL);
     166           0 :             goto fail;
     167             :         }
     168             :         /* gains */
     169             :         while (1) {
     170           5 :             gain = 1;
     171           5 :             if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
     172           0 :                 arg += len;
     173           5 :             if (parse_channel_name(&arg, &in_ch_id, &named)){
     174           0 :                 av_log(ctx, AV_LOG_ERROR,
     175             :                        "Expected in channel name, got \"%.8s\"\n", arg);
     176           0 :                  ret = AVERROR(EINVAL);
     177           0 :                  goto fail;
     178             :             }
     179           5 :             nb_in_channels[named]++;
     180           5 :             if (nb_in_channels[!named]) {
     181           0 :                 av_log(ctx, AV_LOG_ERROR,
     182             :                        "Can not mix named and numbered channels\n");
     183           0 :                 ret = AVERROR(EINVAL);
     184           0 :                 goto fail;
     185             :             }
     186           5 :             pan->gain[out_ch_id][in_ch_id] = sign * gain;
     187           5 :             skip_spaces(&arg);
     188           5 :             if (!*arg)
     189           5 :                 break;
     190           0 :             if (*arg == '-') {
     191           0 :                 sign = -1;
     192           0 :             } else if (*arg != '+') {
     193           0 :                 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
     194           0 :                 ret = AVERROR(EINVAL);
     195           0 :                 goto fail;
     196             :             } else {
     197           0 :                 sign = 1;
     198             :             }
     199           0 :             arg++;
     200             :         }
     201             :     }
     202           4 :     pan->need_renumber = !!nb_in_channels[1];
     203             : 
     204           4 :     ret = 0;
     205             : fail:
     206           4 :     av_free(args);
     207           4 :     return ret;
     208             : }
     209             : 
     210           4 : static int are_gains_pure(const PanContext *pan)
     211             : {
     212             :     int i, j;
     213             : 
     214         260 :     for (i = 0; i < MAX_CHANNELS; i++) {
     215         256 :         int nb_gain = 0;
     216             : 
     217       16640 :         for (j = 0; j < MAX_CHANNELS; j++) {
     218       16384 :             double gain = pan->gain[i][j];
     219             : 
     220             :             /* channel mapping is effective only if 0% or 100% of a channel is
     221             :              * selected... */
     222       16384 :             if (gain != 0. && gain != 1.)
     223           0 :                 return 0;
     224             :             /* ...and if the output channel is only composed of one input */
     225       16384 :             if (gain && nb_gain++)
     226           0 :                 return 0;
     227             :         }
     228             :     }
     229           4 :     return 1;
     230             : }
     231             : 
     232           4 : static int query_formats(AVFilterContext *ctx)
     233             : {
     234           4 :     PanContext *pan = ctx->priv;
     235           4 :     AVFilterLink *inlink  = ctx->inputs[0];
     236           4 :     AVFilterLink *outlink = ctx->outputs[0];
     237           4 :     AVFilterFormats *formats = NULL;
     238             :     AVFilterChannelLayouts *layouts;
     239             :     int ret;
     240             : 
     241           4 :     pan->pure_gains = are_gains_pure(pan);
     242             :     /* libswr supports any sample and packing formats */
     243           4 :     if ((ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO))) < 0)
     244           0 :         return ret;
     245             : 
     246           4 :     formats = ff_all_samplerates();
     247           4 :     if ((ret = ff_set_common_samplerates(ctx, formats)) < 0)
     248           0 :         return ret;
     249             : 
     250             :     // inlink supports any channel layout
     251           4 :     layouts = ff_all_channel_counts();
     252           4 :     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
     253           0 :         return ret;
     254             : 
     255             :     // outlink supports only requested output channel layout
     256           4 :     layouts = NULL;
     257           8 :     if ((ret = ff_add_channel_layout(&layouts,
     258           8 :                           pan->out_channel_layout ? pan->out_channel_layout :
     259           0 :                           FF_COUNT2LAYOUT(pan->nb_output_channels))) < 0)
     260           0 :         return ret;
     261           4 :     return ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
     262             : }
     263             : 
     264           4 : static int config_props(AVFilterLink *link)
     265             : {
     266           4 :     AVFilterContext *ctx = link->dst;
     267           4 :     PanContext *pan = ctx->priv;
     268             :     char buf[1024], *cur;
     269             :     int i, j, k, r;
     270             :     double t;
     271             : 
     272           4 :     if (pan->need_renumber) {
     273             :         // input channels were given by their name: renumber them
     274           0 :         for (i = j = 0; i < MAX_CHANNELS; i++) {
     275           0 :             if ((link->channel_layout >> i) & 1) {
     276           0 :                 for (k = 0; k < pan->nb_output_channels; k++)
     277           0 :                     pan->gain[k][j] = pan->gain[k][i];
     278           0 :                 j++;
     279             :             }
     280             :         }
     281             :     }
     282             : 
     283             :     // sanity check; can't be done in query_formats since the inlink
     284             :     // channel layout is unknown at that time
     285           8 :     if (link->channels > MAX_CHANNELS ||
     286           4 :         pan->nb_output_channels > MAX_CHANNELS) {
     287           0 :         av_log(ctx, AV_LOG_ERROR,
     288             :                "af_pan supports a maximum of %d channels. "
     289             :                "Feel free to ask for a higher limit.\n", MAX_CHANNELS);
     290           0 :         return AVERROR_PATCHWELCOME;
     291             :     }
     292             : 
     293             :     // init libswresample context
     294          12 :     pan->swr = swr_alloc_set_opts(pan->swr,
     295           4 :                                   pan->out_channel_layout, link->format, link->sample_rate,
     296           4 :                                   link->channel_layout,    link->format, link->sample_rate,
     297             :                                   0, ctx);
     298           4 :     if (!pan->swr)
     299           0 :         return AVERROR(ENOMEM);
     300           4 :     if (!link->channel_layout) {
     301           0 :         if (av_opt_set_int(pan->swr, "ich", link->channels, 0) < 0)
     302           0 :             return AVERROR(EINVAL);
     303             :     }
     304           4 :     if (!pan->out_channel_layout) {
     305           0 :         if (av_opt_set_int(pan->swr, "och", pan->nb_output_channels, 0) < 0)
     306           0 :             return AVERROR(EINVAL);
     307             :     }
     308             : 
     309             :     // gains are pure, init the channel mapping
     310           4 :     if (pan->pure_gains) {
     311             : 
     312             :         // get channel map from the pure gains
     313          10 :         for (i = 0; i < pan->nb_output_channels; i++) {
     314           6 :             int ch_id = -1;
     315           9 :             for (j = 0; j < link->channels; j++) {
     316           8 :                 if (pan->gain[i][j]) {
     317           5 :                     ch_id = j;
     318           5 :                     break;
     319             :                 }
     320             :             }
     321           6 :             pan->channel_map[i] = ch_id;
     322             :         }
     323             : 
     324           4 :         av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
     325           4 :         av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
     326           4 :         swr_set_channel_mapping(pan->swr, pan->channel_map);
     327             :     } else {
     328             :         // renormalize
     329           0 :         for (i = 0; i < pan->nb_output_channels; i++) {
     330           0 :             if (!((pan->need_renorm >> i) & 1))
     331           0 :                 continue;
     332           0 :             t = 0;
     333           0 :             for (j = 0; j < link->channels; j++)
     334           0 :                 t += fabs(pan->gain[i][j]);
     335           0 :             if (t > -1E-5 && t < 1E-5) {
     336             :                 // t is almost 0 but not exactly, this is probably a mistake
     337           0 :                 if (t)
     338           0 :                     av_log(ctx, AV_LOG_WARNING,
     339             :                            "Degenerate coefficients while renormalizing\n");
     340           0 :                 continue;
     341             :             }
     342           0 :             for (j = 0; j < link->channels; j++)
     343           0 :                 pan->gain[i][j] /= t;
     344             :         }
     345           0 :         av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
     346           0 :         av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
     347           0 :         swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
     348             :     }
     349             : 
     350           4 :     r = swr_init(pan->swr);
     351           4 :     if (r < 0)
     352           0 :         return r;
     353             : 
     354             :     // summary
     355          10 :     for (i = 0; i < pan->nb_output_channels; i++) {
     356           6 :         cur = buf;
     357          32 :         for (j = 0; j < link->channels; j++) {
     358          26 :             r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
     359             :                          j ? " + " : "", pan->gain[i][j], j);
     360          26 :             cur += FFMIN(buf + sizeof(buf) - cur, r);
     361             :         }
     362           6 :         av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
     363             :     }
     364             :     // add channel mapping summary if possible
     365           4 :     if (pan->pure_gains) {
     366           4 :         av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
     367          10 :         for (i = 0; i < pan->nb_output_channels; i++)
     368           6 :             if (pan->channel_map[i] < 0)
     369           1 :                 av_log(ctx, AV_LOG_INFO, " M");
     370             :             else
     371           5 :                 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
     372           4 :         av_log(ctx, AV_LOG_INFO, "\n");
     373           4 :         return 0;
     374             :     }
     375           0 :     return 0;
     376             : }
     377             : 
     378        1229 : static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     379             : {
     380             :     int ret;
     381        1229 :     int n = insamples->nb_samples;
     382        1229 :     AVFilterLink *const outlink = inlink->dst->outputs[0];
     383        1229 :     AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
     384        1229 :     PanContext *pan = inlink->dst->priv;
     385             : 
     386        1229 :     if (!outsamples)
     387           0 :         return AVERROR(ENOMEM);
     388        1229 :     swr_convert(pan->swr, outsamples->extended_data, n,
     389        1229 :                 (void *)insamples->extended_data, n);
     390        1229 :     av_frame_copy_props(outsamples, insamples);
     391        1229 :     outsamples->channel_layout = outlink->channel_layout;
     392        1229 :     av_frame_set_channels(outsamples, outlink->channels);
     393             : 
     394        1229 :     ret = ff_filter_frame(outlink, outsamples);
     395        1229 :     av_frame_free(&insamples);
     396        1229 :     return ret;
     397             : }
     398             : 
     399           4 : static av_cold void uninit(AVFilterContext *ctx)
     400             : {
     401           4 :     PanContext *pan = ctx->priv;
     402           4 :     swr_free(&pan->swr);
     403           4 : }
     404             : 
     405             : #define OFFSET(x) offsetof(PanContext, x)
     406             : 
     407             : static const AVOption pan_options[] = {
     408             :     { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
     409             :     { NULL }
     410             : };
     411             : 
     412             : AVFILTER_DEFINE_CLASS(pan);
     413             : 
     414             : static const AVFilterPad pan_inputs[] = {
     415             :     {
     416             :         .name         = "default",
     417             :         .type         = AVMEDIA_TYPE_AUDIO,
     418             :         .config_props = config_props,
     419             :         .filter_frame = filter_frame,
     420             :     },
     421             :     { NULL }
     422             : };
     423             : 
     424             : static const AVFilterPad pan_outputs[] = {
     425             :     {
     426             :         .name = "default",
     427             :         .type = AVMEDIA_TYPE_AUDIO,
     428             :     },
     429             :     { NULL }
     430             : };
     431             : 
     432             : AVFilter ff_af_pan = {
     433             :     .name          = "pan",
     434             :     .description   = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
     435             :     .priv_size     = sizeof(PanContext),
     436             :     .priv_class    = &pan_class,
     437             :     .init          = init,
     438             :     .uninit        = uninit,
     439             :     .query_formats = query_formats,
     440             :     .inputs        = pan_inputs,
     441             :     .outputs       = pan_outputs,
     442             : };

Generated by: LCOV version 1.13