LCOV - code coverage report
Current view: top level - src/libavfilter - af_sidechaincompress.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 166 0.0 %
Date: 2017-06-22 08:51:55 Functions: 0 10 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 "formats.h"
      36             : #include "hermite.h"
      37             : #include "internal.h"
      38             : 
      39             : typedef struct SidechainCompressContext {
      40             :     const AVClass *class;
      41             : 
      42             :     double level_in;
      43             :     double level_sc;
      44             :     double attack, attack_coeff;
      45             :     double release, release_coeff;
      46             :     double lin_slope;
      47             :     double ratio;
      48             :     double threshold;
      49             :     double makeup;
      50             :     double mix;
      51             :     double thres;
      52             :     double knee;
      53             :     double knee_start;
      54             :     double knee_stop;
      55             :     double lin_knee_start;
      56             :     double adj_knee_start;
      57             :     double compressed_knee_stop;
      58             :     int link;
      59             :     int detection;
      60             : 
      61             :     AVAudioFifo *fifo[2];
      62             :     int64_t pts;
      63             : } SidechainCompressContext;
      64             : 
      65             : #define OFFSET(x) offsetof(SidechainCompressContext, x)
      66             : #define A AV_OPT_FLAG_AUDIO_PARAM
      67             : #define F AV_OPT_FLAG_FILTERING_PARAM
      68             : 
      69             : static const AVOption options[] = {
      70             :     { "level_in",  "set input gain",     OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},        0.015625,   64, A|F },
      71             :     { "threshold", "set threshold",      OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563,    1, A|F },
      72             :     { "ratio",     "set ratio",          OFFSET(ratio),     AV_OPT_TYPE_DOUBLE, {.dbl=2},               1,   20, A|F },
      73             :     { "attack",    "set attack",         OFFSET(attack),    AV_OPT_TYPE_DOUBLE, {.dbl=20},           0.01, 2000, A|F },
      74             :     { "release",   "set release",        OFFSET(release),   AV_OPT_TYPE_DOUBLE, {.dbl=250},          0.01, 9000, A|F },
      75             :     { "makeup",    "set make up gain",   OFFSET(makeup),    AV_OPT_TYPE_DOUBLE, {.dbl=1},               1,   64, A|F },
      76             :     { "knee",      "set knee",           OFFSET(knee),      AV_OPT_TYPE_DOUBLE, {.dbl=2.82843},         1,    8, A|F },
      77             :     { "link",      "set link type",      OFFSET(link),      AV_OPT_TYPE_INT,    {.i64=0},               0,    1, A|F, "link" },
      78             :     {   "average", 0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "link" },
      79             :     {   "maximum", 0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "link" },
      80             :     { "detection", "set detection",      OFFSET(detection), AV_OPT_TYPE_INT,    {.i64=1},               0,    1, A|F, "detection" },
      81             :     {   "peak",    0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=0},               0,    0, A|F, "detection" },
      82             :     {   "rms",     0,                    0,                 AV_OPT_TYPE_CONST,  {.i64=1},               0,    0, A|F, "detection" },
      83             :     { "level_sc",  "set sidechain gain", OFFSET(level_sc),  AV_OPT_TYPE_DOUBLE, {.dbl=1},        0.015625,   64, A|F },
      84             :     { "mix",       "set mix",            OFFSET(mix),       AV_OPT_TYPE_DOUBLE, {.dbl=1},               0,    1, A|F },
      85             :     { NULL }
      86             : };
      87             : 
      88             : #define sidechaincompress_options options
      89             : AVFILTER_DEFINE_CLASS(sidechaincompress);
      90             : 
      91             : // A fake infinity value (because real infinity may break some hosts)
      92             : #define FAKE_INFINITY (65536.0 * 65536.0)
      93             : 
      94             : // Check for infinity (with appropriate-ish tolerance)
      95             : #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
      96             : 
      97           0 : static double output_gain(double lin_slope, double ratio, double thres,
      98             :                           double knee, double knee_start, double knee_stop,
      99             :                           double compressed_knee_stop, int detection)
     100             : {
     101           0 :     double slope = log(lin_slope);
     102           0 :     double gain = 0.0;
     103           0 :     double delta = 0.0;
     104             : 
     105           0 :     if (detection)
     106           0 :         slope *= 0.5;
     107             : 
     108           0 :     if (IS_FAKE_INFINITY(ratio)) {
     109           0 :         gain = thres;
     110           0 :         delta = 0.0;
     111             :     } else {
     112           0 :         gain = (slope - thres) / ratio + thres;
     113           0 :         delta = 1.0 / ratio;
     114             :     }
     115             : 
     116           0 :     if (knee > 1.0 && slope < knee_stop)
     117           0 :         gain = hermite_interpolation(slope, knee_start, knee_stop,
     118             :                                      knee_start, compressed_knee_stop,
     119             :                                      1.0, delta);
     120             : 
     121           0 :     return exp(gain - slope);
     122             : }
     123             : 
     124           0 : static int compressor_config_output(AVFilterLink *outlink)
     125             : {
     126           0 :     AVFilterContext *ctx = outlink->src;
     127           0 :     SidechainCompressContext *s = ctx->priv;
     128             : 
     129           0 :     s->thres = log(s->threshold);
     130           0 :     s->lin_knee_start = s->threshold / sqrt(s->knee);
     131           0 :     s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
     132           0 :     s->knee_start = log(s->lin_knee_start);
     133           0 :     s->knee_stop = log(s->threshold * sqrt(s->knee));
     134           0 :     s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
     135             : 
     136           0 :     s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
     137           0 :     s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
     138             : 
     139           0 :     return 0;
     140             : }
     141             : 
     142           0 : static void compressor(SidechainCompressContext *s,
     143             :                        const double *src, double *dst, const double *scsrc, int nb_samples,
     144             :                        double level_in, double level_sc,
     145             :                        AVFilterLink *inlink, AVFilterLink *sclink)
     146             : {
     147           0 :     const double makeup = s->makeup;
     148           0 :     const double mix = s->mix;
     149             :     int i, c;
     150             : 
     151           0 :     for (i = 0; i < nb_samples; i++) {
     152           0 :         double abs_sample, gain = 1.0;
     153             : 
     154           0 :         abs_sample = fabs(scsrc[0] * level_sc);
     155             : 
     156           0 :         if (s->link == 1) {
     157           0 :             for (c = 1; c < sclink->channels; c++)
     158           0 :                 abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
     159             :         } else {
     160           0 :             for (c = 1; c < sclink->channels; c++)
     161           0 :                 abs_sample += fabs(scsrc[c] * level_sc);
     162             : 
     163           0 :             abs_sample /= sclink->channels;
     164             :         }
     165             : 
     166           0 :         if (s->detection)
     167           0 :             abs_sample *= abs_sample;
     168             : 
     169           0 :         s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
     170             : 
     171           0 :         if (s->lin_slope > 0.0 && s->lin_slope > (s->detection ? s->adj_knee_start : s->lin_knee_start))
     172           0 :             gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
     173             :                                s->knee_start, s->knee_stop,
     174             :                                s->compressed_knee_stop, s->detection);
     175             : 
     176           0 :         for (c = 0; c < inlink->channels; c++)
     177           0 :             dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
     178             : 
     179           0 :         src += inlink->channels;
     180           0 :         dst += inlink->channels;
     181           0 :         scsrc += sclink->channels;
     182             :     }
     183           0 : }
     184             : 
     185             : #if CONFIG_SIDECHAINCOMPRESS_FILTER
     186           0 : static int filter_frame(AVFilterLink *link, AVFrame *frame)
     187             : {
     188           0 :     AVFilterContext *ctx = link->dst;
     189           0 :     SidechainCompressContext *s = ctx->priv;
     190           0 :     AVFilterLink *outlink = ctx->outputs[0];
     191           0 :     AVFrame *out = NULL, *in[2] = { NULL };
     192             :     double *dst;
     193             :     int nb_samples;
     194             :     int i;
     195             : 
     196           0 :     for (i = 0; i < 2; i++)
     197           0 :         if (link == ctx->inputs[i])
     198           0 :             break;
     199           0 :     av_assert0(i < 2);
     200           0 :     av_audio_fifo_write(s->fifo[i], (void **)frame->extended_data,
     201           0 :                         frame->nb_samples);
     202           0 :     av_frame_free(&frame);
     203             : 
     204           0 :     nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
     205           0 :     if (!nb_samples)
     206           0 :         return 0;
     207             : 
     208           0 :     out = ff_get_audio_buffer(outlink, nb_samples);
     209           0 :     if (!out)
     210           0 :         return AVERROR(ENOMEM);
     211           0 :     for (i = 0; i < 2; i++) {
     212           0 :         in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
     213           0 :         if (!in[i]) {
     214           0 :             av_frame_free(&in[0]);
     215           0 :             av_frame_free(&in[1]);
     216           0 :             av_frame_free(&out);
     217           0 :             return AVERROR(ENOMEM);
     218             :         }
     219           0 :         av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
     220             :     }
     221             : 
     222           0 :     dst = (double *)out->data[0];
     223           0 :     out->pts = s->pts;
     224           0 :     s->pts += nb_samples;
     225             : 
     226           0 :     compressor(s, (double *)in[0]->data[0], dst,
     227           0 :                (double *)in[1]->data[0], nb_samples,
     228             :                s->level_in, s->level_sc,
     229           0 :                ctx->inputs[0], ctx->inputs[1]);
     230             : 
     231           0 :     av_frame_free(&in[0]);
     232           0 :     av_frame_free(&in[1]);
     233             : 
     234           0 :     return ff_filter_frame(outlink, out);
     235             : }
     236             : 
     237           0 : static int request_frame(AVFilterLink *outlink)
     238             : {
     239           0 :     AVFilterContext *ctx = outlink->src;
     240           0 :     SidechainCompressContext *s = ctx->priv;
     241             :     int i;
     242             : 
     243             :     /* get a frame on each input */
     244           0 :     for (i = 0; i < 2; i++) {
     245           0 :         AVFilterLink *inlink = ctx->inputs[i];
     246           0 :         if (!av_audio_fifo_size(s->fifo[i]))
     247           0 :             return ff_request_frame(inlink);
     248             :     }
     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             :         .filter_frame   = filter_frame,
     329             :     },{
     330             :         .name           = "sidechain",
     331             :         .type           = AVMEDIA_TYPE_AUDIO,
     332             :         .filter_frame   = filter_frame,
     333             :     },
     334             :     { NULL }
     335             : };
     336             : 
     337             : static const AVFilterPad sidechaincompress_outputs[] = {
     338             :     {
     339             :         .name          = "default",
     340             :         .type          = AVMEDIA_TYPE_AUDIO,
     341             :         .config_props  = config_output,
     342             :         .request_frame = request_frame,
     343             :     },
     344             :     { NULL }
     345             : };
     346             : 
     347             : AVFilter ff_af_sidechaincompress = {
     348             :     .name           = "sidechaincompress",
     349             :     .description    = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
     350             :     .priv_size      = sizeof(SidechainCompressContext),
     351             :     .priv_class     = &sidechaincompress_class,
     352             :     .query_formats  = query_formats,
     353             :     .uninit         = uninit,
     354             :     .inputs         = sidechaincompress_inputs,
     355             :     .outputs        = sidechaincompress_outputs,
     356             : };
     357             : #endif  /* CONFIG_SIDECHAINCOMPRESS_FILTER */
     358             : 
     359             : #if CONFIG_ACOMPRESSOR_FILTER
     360           0 : static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
     361             : {
     362           0 :     const double *src = (const double *)in->data[0];
     363           0 :     AVFilterContext *ctx = inlink->dst;
     364           0 :     SidechainCompressContext *s = ctx->priv;
     365           0 :     AVFilterLink *outlink = ctx->outputs[0];
     366             :     AVFrame *out;
     367             :     double *dst;
     368             : 
     369           0 :     if (av_frame_is_writable(in)) {
     370           0 :         out = in;
     371             :     } else {
     372           0 :         out = ff_get_audio_buffer(inlink, in->nb_samples);
     373           0 :         if (!out) {
     374           0 :             av_frame_free(&in);
     375           0 :             return AVERROR(ENOMEM);
     376             :         }
     377           0 :         av_frame_copy_props(out, in);
     378             :     }
     379           0 :     dst = (double *)out->data[0];
     380             : 
     381           0 :     compressor(s, src, dst, src, in->nb_samples,
     382             :                s->level_in, s->level_in,
     383             :                inlink, inlink);
     384             : 
     385           0 :     if (out != in)
     386           0 :         av_frame_free(&in);
     387           0 :     return ff_filter_frame(outlink, out);
     388             : }
     389             : 
     390           0 : static int acompressor_query_formats(AVFilterContext *ctx)
     391             : {
     392             :     AVFilterFormats *formats;
     393             :     AVFilterChannelLayouts *layouts;
     394             :     static const enum AVSampleFormat sample_fmts[] = {
     395             :         AV_SAMPLE_FMT_DBL,
     396             :         AV_SAMPLE_FMT_NONE
     397             :     };
     398             :     int ret;
     399             : 
     400           0 :     layouts = ff_all_channel_counts();
     401           0 :     if (!layouts)
     402           0 :         return AVERROR(ENOMEM);
     403           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
     404           0 :     if (ret < 0)
     405           0 :         return ret;
     406             : 
     407           0 :     formats = ff_make_format_list(sample_fmts);
     408           0 :     if (!formats)
     409           0 :         return AVERROR(ENOMEM);
     410           0 :     ret = ff_set_common_formats(ctx, formats);
     411           0 :     if (ret < 0)
     412           0 :         return ret;
     413             : 
     414           0 :     formats = ff_all_samplerates();
     415           0 :     if (!formats)
     416           0 :         return AVERROR(ENOMEM);
     417           0 :     return ff_set_common_samplerates(ctx, formats);
     418             : }
     419             : 
     420             : #define acompressor_options options
     421             : AVFILTER_DEFINE_CLASS(acompressor);
     422             : 
     423             : static const AVFilterPad acompressor_inputs[] = {
     424             :     {
     425             :         .name           = "default",
     426             :         .type           = AVMEDIA_TYPE_AUDIO,
     427             :         .filter_frame   = acompressor_filter_frame,
     428             :     },
     429             :     { NULL }
     430             : };
     431             : 
     432             : static const AVFilterPad acompressor_outputs[] = {
     433             :     {
     434             :         .name          = "default",
     435             :         .type          = AVMEDIA_TYPE_AUDIO,
     436             :         .config_props  = compressor_config_output,
     437             :     },
     438             :     { NULL }
     439             : };
     440             : 
     441             : AVFilter ff_af_acompressor = {
     442             :     .name           = "acompressor",
     443             :     .description    = NULL_IF_CONFIG_SMALL("Audio compressor."),
     444             :     .priv_size      = sizeof(SidechainCompressContext),
     445             :     .priv_class     = &acompressor_class,
     446             :     .query_formats  = acompressor_query_formats,
     447             :     .inputs         = acompressor_inputs,
     448             :     .outputs        = acompressor_outputs,
     449             : };
     450             : #endif  /* CONFIG_ACOMPRESSOR_FILTER */

Generated by: LCOV version 1.13