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

Generated by: LCOV version 1.13