LCOV - code coverage report
Current view: top level - libavfilter - af_sidechaincompress.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 171 0.0 %
Date: 2017-12-17 16:07:53 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
       3             :  * Copyright (c) 2015 Paul B Mahol
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * Audio (Sidechain) Compressor filter
      25             :  */
      26             : 
      27             : #include "libavutil/audio_fifo.h"
      28             : #include "libavutil/avassert.h"
      29             : #include "libavutil/channel_layout.h"
      30             : #include "libavutil/common.h"
      31             : #include "libavutil/opt.h"
      32             : 
      33             : #include "audio.h"
      34             : #include "avfilter.h"
      35             : #include "filters.h"
      36             : #include "formats.h"
      37             : #include "hermite.h"
      38             : #include "internal.h"
      39             : 
      40             : typedef struct SidechainCompressContext {
      41             :     const AVClass *class;
      42             : 
      43             :     double level_in;
      44             :     double level_sc;
      45             :     double attack, attack_coeff;
      46             :     double release, release_coeff;
      47             :     double lin_slope;
      48             :     double ratio;
      49             :     double threshold;
      50             :     double makeup;
      51             :     double mix;
      52             :     double thres;
      53             :     double knee;
      54             :     double knee_start;
      55             :     double knee_stop;
      56             :     double lin_knee_start;
      57             :     double adj_knee_start;
      58             :     double compressed_knee_stop;
      59             :     int link;
      60             :     int detection;
      61             : 
      62             :     AVAudioFifo *fifo[2];
      63             :     int64_t pts;
      64             : } SidechainCompressContext;
      65             : 
      66             : #define OFFSET(x) offsetof(SidechainCompressContext, x)
      67             : #define A AV_OPT_FLAG_AUDIO_PARAM
      68             : #define F AV_OPT_FLAG_FILTERING_PARAM
      69             : 
      70             : static const AVOption options[] = {
      71             :     { "level_in",  "set input gain",     OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},        0.015625,   64, A|F },
      72             :     { "threshold", "set threshold",      OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563,    1, A|F },
      73             :     { "ratio",     "set ratio",          OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   20, A|F },
      74             :     { "attack",    "set attack",         OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},           0.01, 2000, A|F },
      75             :     { "release",   "set release",        OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},          0.01, 9000, A|F },
      76             :     { "makeup",    "set make up gain",   OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=1},               1,   64, A|F },
      77             :     { "knee",      "set knee",           OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.82843},         1,    8, A|F },
      78             :     { "link",      "set link type",      OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},               0,    1, A|F, "link" },
      79             :     {   "average", 0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "link" },
      80             :     {   "maximum", 0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "link" },
      81             :     { "detection", "set detection",      OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=1},               0,    1, A|F, "detection" },
      82             :     {   "peak",    0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "detection" },
      83             :     {   "rms",     0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "detection" },
      84             :     { "level_sc",  "set sidechain gain", OFFSET(level_sc),  AV_OPT_TYPE_DOUBLE, {.dbl=1},        0.015625,   64, A|F },
      85             :     { "mix",       "set mix",            OFFSET(mix),       AV_OPT_TYPE_DOUBLE, {.dbl=1},               0,    1, A|F },
      86             :     { NULL }
      87             : };
      88             : 
      89             : #define sidechaincompress_options options
      90             : AVFILTER_DEFINE_CLASS(sidechaincompress);
      91             : 
      92             : // A fake infinity value (because real infinity may break some hosts)
      93             : #define FAKE_INFINITY (65536.0 * 65536.0)
      94             : 
      95             : // Check for infinity (with appropriate-ish tolerance)
      96             : #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
      97             : 
      98           0 : static double output_gain(double lin_slope, double ratio, double thres,
      99             :                           double knee, double knee_start, double knee_stop,
     100             :                           double compressed_knee_stop, int detection)
     101             : {
     102           0 :     double slope = log(lin_slope);
     103           0 :     double gain = 0.0;
     104           0 :     double delta = 0.0;
     105             : 
     106           0 :     if (detection)
     107           0 :         slope *= 0.5;
     108             : 
     109           0 :     if (IS_FAKE_INFINITY(ratio)) {
     110           0 :         gain = thres;
     111           0 :         delta = 0.0;
     112             :     } else {
     113           0 :         gain = (slope - thres) / ratio + thres;
     114           0 :         delta = 1.0 / ratio;
     115             :     }
     116             : 
     117           0 :     if (knee > 1.0 && slope < knee_stop)
     118           0 :         gain = hermite_interpolation(slope, knee_start, knee_stop,
     119             :                                      knee_start, compressed_knee_stop,
     120             :                                      1.0, delta);
     121             : 
     122           0 :     return exp(gain - slope);
     123             : }
     124             : 
     125           0 : static int compressor_config_output(AVFilterLink *outlink)
     126             : {
     127           0 :     AVFilterContext *ctx = outlink->src;
     128           0 :     SidechainCompressContext *s = ctx->priv;
     129             : 
     130           0 :     s->thres = log(s->threshold);
     131           0 :     s->lin_knee_start = s->threshold / sqrt(s->knee);
     132           0 :     s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
     133           0 :     s->knee_start = log(s->lin_knee_start);
     134           0 :     s->knee_stop = log(s->threshold * sqrt(s->knee));
     135           0 :     s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
     136             : 
     137           0 :     s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
     138           0 :     s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
     139             : 
     140           0 :     return 0;
     141             : }
     142             : 
     143           0 : static void compressor(SidechainCompressContext *s,
     144             :                        const double *src, double *dst, const double *scsrc, int nb_samples,
     145             :                        double level_in, double level_sc,
     146             :                        AVFilterLink *inlink, AVFilterLink *sclink)
     147             : {
     148           0 :     const double makeup = s->makeup;
     149           0 :     const double mix = s->mix;
     150             :     int i, c;
     151             : 
     152           0 :     for (i = 0; i < nb_samples; i++) {
     153           0 :         double abs_sample, gain = 1.0;
     154             : 
     155           0 :         abs_sample = fabs(scsrc[0] * level_sc);
     156             : 
     157           0 :         if (s->link == 1) {
     158           0 :             for (c = 1; c < sclink->channels; c++)
     159           0 :                 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
     160             :         } else {
     161           0 :             for (c = 1; c < sclink->channels; c++)
     162           0 :                 abs_sample += fabs(scsrc[c] * level_sc);
     163             : 
     164           0 :             abs_sample /= sclink->channels;
     165             :         }
     166             : 
     167           0 :         if (s->detection)
     168           0 :             abs_sample *= abs_sample;
     169             : 
     170           0 :         s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
     171             : 
     172           0 :         if (s->lin_slope > 0.0 && s->lin_slope > (s->detection ? s->adj_knee_start : s->lin_knee_start))
     173           0 :             gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
     174             :                                s->knee_start, s->knee_stop,
     175             :                                s->compressed_knee_stop, s->detection);
     176             : 
     177           0 :         for (c = 0; c < inlink->channels; c++)
     178           0 :             dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
     179             : 
     180           0 :         src += inlink->channels;
     181           0 :         dst += inlink->channels;
     182           0 :         scsrc += sclink->channels;
     183             :     }
     184           0 : }
     185             : 
     186             : #if CONFIG_SIDECHAINCOMPRESS_FILTER
     187           0 : static int activate(AVFilterContext *ctx)
     188             : {
     189           0 :     SidechainCompressContext *s = ctx->priv;
     190           0 :     AVFrame *out = NULL, *in[2] = { NULL };
     191             :     int ret, i, nb_samples;
     192             :     double *dst;
     193             : 
     194           0 :     FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
     195           0 :     if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
     196           0 :         av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
     197           0 :                             in[0]->nb_samples);
     198           0 :         av_frame_free(&in[0]);
     199             :     }
     200           0 :     if (ret < 0)
     201           0 :         return ret;
     202           0 :     if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
     203           0 :         av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
     204           0 :                             in[1]->nb_samples);
     205           0 :         av_frame_free(&in[1]);
     206             :     }
     207           0 :     if (ret < 0)
     208           0 :         return ret;
     209             : 
     210           0 :     nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
     211           0 :     if (nb_samples) {
     212           0 :         out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
     213           0 :         if (!out)
     214           0 :             return AVERROR(ENOMEM);
     215           0 :         for (i = 0; i < 2; i++) {
     216           0 :             in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
     217           0 :             if (!in[i]) {
     218           0 :                 av_frame_free(&in[0]);
     219           0 :                 av_frame_free(&in[1]);
     220           0 :                 av_frame_free(&out);
     221           0 :                 return AVERROR(ENOMEM);
     222             :             }
     223           0 :             av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
     224             :         }
     225             : 
     226           0 :         dst = (double *)out->data[0];
     227           0 :         out->pts = s->pts;
     228           0 :         s->pts += nb_samples;
     229             : 
     230           0 :         compressor(s, (double *)in[0]->data[0], dst,
     231           0 :                    (double *)in[1]->data[0], nb_samples,
     232             :                    s->level_in, s->level_sc,
     233           0 :                    ctx->inputs[0], ctx->inputs[1]);
     234             : 
     235           0 :         av_frame_free(&in[0]);
     236           0 :         av_frame_free(&in[1]);
     237             : 
     238           0 :         ret = ff_filter_frame(ctx->outputs[0], out);
     239           0 :         if (ret < 0)
     240           0 :             return ret;
     241             :     }
     242           0 :     FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
     243           0 :     FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
     244           0 :     if (ff_outlink_frame_wanted(ctx->outputs[0])) {
     245           0 :         if (!av_audio_fifo_size(s->fifo[0]))
     246           0 :             ff_inlink_request_frame(ctx->inputs[0]);
     247           0 :         if (!av_audio_fifo_size(s->fifo[1]))
     248           0 :             ff_inlink_request_frame(ctx->inputs[1]);
     249             :     }
     250           0 :     return 0;
     251             : }
     252             : 
     253           0 : static int query_formats(AVFilterContext *ctx)
     254             : {
     255             :     AVFilterFormats *formats;
     256           0 :     AVFilterChannelLayouts *layouts = NULL;
     257             :     static const enum AVSampleFormat sample_fmts[] = {
     258             :         AV_SAMPLE_FMT_DBL,
     259             :         AV_SAMPLE_FMT_NONE
     260             :     };
     261             :     int ret, i;
     262             : 
     263           0 :     if (!ctx->inputs[0]->in_channel_layouts ||
     264           0 :         !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
     265           0 :         av_log(ctx, AV_LOG_WARNING,
     266             :                "No channel layout for input 1\n");
     267           0 :             return AVERROR(EAGAIN);
     268             :     }
     269             : 
     270           0 :     if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
     271           0 :         (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
     272           0 :         return ret;
     273             : 
     274           0 :     for (i = 0; i < 2; i++) {
     275           0 :         layouts = ff_all_channel_counts();
     276           0 :         if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
     277           0 :             return ret;
     278             :     }
     279             : 
     280           0 :     formats = ff_make_format_list(sample_fmts);
     281           0 :     if ((ret = ff_set_common_formats(ctx, formats)) < 0)
     282           0 :         return ret;
     283             : 
     284           0 :     formats = ff_all_samplerates();
     285           0 :     return ff_set_common_samplerates(ctx, formats);
     286             : }
     287             : 
     288           0 : static int config_output(AVFilterLink *outlink)
     289             : {
     290           0 :     AVFilterContext *ctx = outlink->src;
     291           0 :     SidechainCompressContext *s = ctx->priv;
     292             : 
     293           0 :     if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
     294           0 :         av_log(ctx, AV_LOG_ERROR,
     295             :                "Inputs must have the same sample rate "
     296             :                "%d for in0 vs %d for in1\n",
     297           0 :                ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
     298           0 :         return AVERROR(EINVAL);
     299             :     }
     300             : 
     301           0 :     outlink->sample_rate = ctx->inputs[0]->sample_rate;
     302           0 :     outlink->time_base   = ctx->inputs[0]->time_base;
     303           0 :     outlink->channel_layout = ctx->inputs[0]->channel_layout;
     304           0 :     outlink->channels = ctx->inputs[0]->channels;
     305             : 
     306           0 :     s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
     307           0 :     s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
     308           0 :     if (!s->fifo[0] || !s->fifo[1])
     309           0 :         return AVERROR(ENOMEM);
     310             : 
     311           0 :     compressor_config_output(outlink);
     312             : 
     313           0 :     return 0;
     314             : }
     315             : 
     316           0 : static av_cold void uninit(AVFilterContext *ctx)
     317             : {
     318           0 :     SidechainCompressContext *s = ctx->priv;
     319             : 
     320           0 :     av_audio_fifo_free(s->fifo[0]);
     321           0 :     av_audio_fifo_free(s->fifo[1]);
     322           0 : }
     323             : 
     324             : static const AVFilterPad sidechaincompress_inputs[] = {
     325             :     {
     326             :         .name           = "main",
     327             :         .type           = AVMEDIA_TYPE_AUDIO,
     328             :     },{
     329             :         .name           = "sidechain",
     330             :         .type           = AVMEDIA_TYPE_AUDIO,
     331             :     },
     332             :     { NULL }
     333             : };
     334             : 
     335             : static const AVFilterPad sidechaincompress_outputs[] = {
     336             :     {
     337             :         .name          = "default",
     338             :         .type          = AVMEDIA_TYPE_AUDIO,
     339             :         .config_props  = config_output,
     340             :     },
     341             :     { NULL }
     342             : };
     343             : 
     344             : AVFilter ff_af_sidechaincompress = {
     345             :     .name           = "sidechaincompress",
     346             :     .description    = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
     347             :     .priv_size      = sizeof(SidechainCompressContext),
     348             :     .priv_class     = &sidechaincompress_class,
     349             :     .query_formats  = query_formats,
     350             :     .activate       = activate,
     351             :     .uninit         = uninit,
     352             :     .inputs         = sidechaincompress_inputs,
     353             :     .outputs        = sidechaincompress_outputs,
     354             : };
     355             : #endif  /* CONFIG_SIDECHAINCOMPRESS_FILTER */
     356             : 
     357             : #if CONFIG_ACOMPRESSOR_FILTER
     358           0 : static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
     359             : {
     360           0 :     const double *src = (const double *)in->data[0];
     361           0 :     AVFilterContext *ctx = inlink->dst;
     362           0 :     SidechainCompressContext *s = ctx->priv;
     363           0 :     AVFilterLink *outlink = ctx->outputs[0];
     364             :     AVFrame *out;
     365             :     double *dst;
     366             : 
     367           0 :     if (av_frame_is_writable(in)) {
     368           0 :         out = in;
     369             :     } else {
     370           0 :         out = ff_get_audio_buffer(inlink, in->nb_samples);
     371           0 :         if (!out) {
     372           0 :             av_frame_free(&in);
     373           0 :             return AVERROR(ENOMEM);
     374             :         }
     375           0 :         av_frame_copy_props(out, in);
     376             :     }
     377           0 :     dst = (double *)out->data[0];
     378             : 
     379           0 :     compressor(s, src, dst, src, in->nb_samples,
     380             :                s->level_in, s->level_in,
     381             :                inlink, inlink);
     382             : 
     383           0 :     if (out != in)
     384           0 :         av_frame_free(&in);
     385           0 :     return ff_filter_frame(outlink, out);
     386             : }
     387             : 
     388           0 : static int acompressor_query_formats(AVFilterContext *ctx)
     389             : {
     390             :     AVFilterFormats *formats;
     391             :     AVFilterChannelLayouts *layouts;
     392             :     static const enum AVSampleFormat sample_fmts[] = {
     393             :         AV_SAMPLE_FMT_DBL,
     394             :         AV_SAMPLE_FMT_NONE
     395             :     };
     396             :     int ret;
     397             : 
     398           0 :     layouts = ff_all_channel_counts();
     399           0 :     if (!layouts)
     400           0 :         return AVERROR(ENOMEM);
     401           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
     402           0 :     if (ret < 0)
     403           0 :         return ret;
     404             : 
     405           0 :     formats = ff_make_format_list(sample_fmts);
     406           0 :     if (!formats)
     407           0 :         return AVERROR(ENOMEM);
     408           0 :     ret = ff_set_common_formats(ctx, formats);
     409           0 :     if (ret < 0)
     410           0 :         return ret;
     411             : 
     412           0 :     formats = ff_all_samplerates();
     413           0 :     if (!formats)
     414           0 :         return AVERROR(ENOMEM);
     415           0 :     return ff_set_common_samplerates(ctx, formats);
     416             : }
     417             : 
     418             : #define acompressor_options options
     419             : AVFILTER_DEFINE_CLASS(acompressor);
     420             : 
     421             : static const AVFilterPad acompressor_inputs[] = {
     422             :     {
     423             :         .name           = "default",
     424             :         .type           = AVMEDIA_TYPE_AUDIO,
     425             :         .filter_frame   = acompressor_filter_frame,
     426             :     },
     427             :     { NULL }
     428             : };
     429             : 
     430             : static const AVFilterPad acompressor_outputs[] = {
     431             :     {
     432             :         .name          = "default",
     433             :         .type          = AVMEDIA_TYPE_AUDIO,
     434             :         .config_props  = compressor_config_output,
     435             :     },
     436             :     { NULL }
     437             : };
     438             : 
     439             : AVFilter ff_af_acompressor = {
     440             :     .name           = "acompressor",
     441             :     .description    = NULL_IF_CONFIG_SMALL("Audio compressor."),
     442             :     .priv_size      = sizeof(SidechainCompressContext),
     443             :     .priv_class     = &acompressor_class,
     444             :     .query_formats  = acompressor_query_formats,
     445             :     .inputs         = acompressor_inputs,
     446             :     .outputs        = acompressor_outputs,
     447             : };
     448             : #endif  /* CONFIG_ACOMPRESSOR_FILTER */

Generated by: LCOV version 1.13