LCOV - code coverage report
Current view: top level - libavfilter - af_acrusher.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 140 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 11 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) Markus Schmidt and Christian Holschuh
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "libavutil/opt.h"
      22             : #include "avfilter.h"
      23             : #include "internal.h"
      24             : #include "audio.h"
      25             : 
      26             : typedef struct LFOContext {
      27             :     double freq;
      28             :     double offset;
      29             :     int srate;
      30             :     double amount;
      31             :     double pwidth;
      32             :     double phase;
      33             : } LFOContext;
      34             : 
      35             : typedef struct SRContext {
      36             :     double target;
      37             :     double real;
      38             :     double samples;
      39             :     double last;
      40             : } SRContext;
      41             : 
      42             : typedef struct ACrusherContext {
      43             :     const AVClass *class;
      44             : 
      45             :     double level_in;
      46             :     double level_out;
      47             :     double bits;
      48             :     double mix;
      49             :     int mode;
      50             :     double dc;
      51             :     double idc;
      52             :     double aa;
      53             :     double samples;
      54             :     int is_lfo;
      55             :     double lforange;
      56             :     double lforate;
      57             : 
      58             :     double sqr;
      59             :     double aa1;
      60             :     double coeff;
      61             :     int    round;
      62             :     double sov;
      63             :     double smin;
      64             :     double sdiff;
      65             : 
      66             :     LFOContext lfo;
      67             :     SRContext *sr;
      68             : } ACrusherContext;
      69             : 
      70             : #define OFFSET(x) offsetof(ACrusherContext, x)
      71             : #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      72             : 
      73             : static const AVOption acrusher_options[] = {
      74             :     { "level_in", "set level in",         OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=1},    0.015625, 64, A },
      75             :     { "level_out","set level out",        OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},    0.015625, 64, A },
      76             :     { "bits",     "set bit reduction",    OFFSET(bits),      AV_OPT_TYPE_DOUBLE, {.dbl=8},    1,        64, A },
      77             :     { "mix",      "set mix",              OFFSET(mix),       AV_OPT_TYPE_DOUBLE, {.dbl=.5},   0,         1, A },
      78             :     { "mode",     "set mode",             OFFSET(mode),      AV_OPT_TYPE_INT,    {.i64=0},    0,         1, A, "mode" },
      79             :     {   "lin",    "linear",               0,                 AV_OPT_TYPE_CONST,  {.i64=0},    0,         0, A, "mode" },
      80             :     {   "log",    "logarithmic",          0,                 AV_OPT_TYPE_CONST,  {.i64=1},    0,         0, A, "mode" },
      81             :     { "dc",       "set DC",               OFFSET(dc),        AV_OPT_TYPE_DOUBLE, {.dbl=1},  .25,         4, A },
      82             :     { "aa",       "set anti-aliasing",    OFFSET(aa),        AV_OPT_TYPE_DOUBLE, {.dbl=.5},   0,         1, A },
      83             :     { "samples",  "set sample reduction", OFFSET(samples),   AV_OPT_TYPE_DOUBLE, {.dbl=1},    1,       250, A },
      84             :     { "lfo",      "enable LFO",           OFFSET(is_lfo),    AV_OPT_TYPE_BOOL,   {.i64=0},    0,         1, A },
      85             :     { "lforange", "set LFO depth",        OFFSET(lforange),  AV_OPT_TYPE_DOUBLE, {.dbl=20},   1,       250, A },
      86             :     { "lforate",  "set LFO rate",         OFFSET(lforate),   AV_OPT_TYPE_DOUBLE, {.dbl=.3}, .01,       200, A },
      87             :     { NULL }
      88             : };
      89             : 
      90             : AVFILTER_DEFINE_CLASS(acrusher);
      91             : 
      92           0 : static double samplereduction(ACrusherContext *s, SRContext *sr, double in)
      93             : {
      94           0 :     sr->samples++;
      95           0 :     if (sr->samples >= s->round) {
      96           0 :         sr->target += s->samples;
      97           0 :         sr->real += s->round;
      98           0 :         if (sr->target + s->samples >= sr->real + 1) {
      99           0 :             sr->last = in;
     100           0 :             sr->target = 0;
     101           0 :             sr->real   = 0;
     102             :         }
     103           0 :         sr->samples = 0;
     104             :     }
     105           0 :     return sr->last;
     106             : }
     107             : 
     108           0 : static double add_dc(double s, double dc, double idc)
     109             : {
     110           0 :     return s > 0 ? s * dc : s * idc;
     111             : }
     112             : 
     113           0 : static double remove_dc(double s, double dc, double idc)
     114             : {
     115           0 :     return s > 0 ? s * idc : s * dc;
     116             : }
     117             : 
     118           0 : static inline double factor(double y, double k, double aa1, double aa)
     119             : {
     120           0 :     return 0.5 * (sin(M_PI * (fabs(y - k) - aa1) / aa - M_PI_2) + 1);
     121             : }
     122             : 
     123           0 : static double bitreduction(ACrusherContext *s, double in)
     124             : {
     125           0 :     const double sqr = s->sqr;
     126           0 :     const double coeff = s->coeff;
     127           0 :     const double aa = s->aa;
     128           0 :     const double aa1 = s->aa1;
     129             :     double y, k;
     130             : 
     131             :     // add dc
     132           0 :     in = add_dc(in, s->dc, s->idc);
     133             : 
     134             :     // main rounding calculation depending on mode
     135             : 
     136             :     // the idea for anti-aliasing:
     137             :     // you need a function f which brings you to the scale, where
     138             :     // you want to round and the function f_b (with f(f_b)=id) which
     139             :     // brings you back to your original scale.
     140             :     //
     141             :     // then you can use the logic below in the following way:
     142             :     // y = f(in) and k = roundf(y)
     143             :     // if (y > k + aa1)
     144             :     //      k = f_b(k) + ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1)
     145             :     // if (y < k + aa1)
     146             :     //      k = f_b(k) - ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1)
     147             :     //
     148             :     // whereas x = (fabs(f(in) - k) - aa1) * PI / aa
     149             :     // for both cases.
     150             : 
     151           0 :     switch (s->mode) {
     152           0 :     case 0:
     153             :     default:
     154             :         // linear
     155           0 :         y = in * coeff;
     156           0 :         k = roundf(y);
     157           0 :         if (k - aa1 <= y && y <= k + aa1) {
     158           0 :             k /= coeff;
     159           0 :         } else if (y > k + aa1) {
     160           0 :             k = k / coeff + ((k + 1) / coeff - k / coeff) *
     161           0 :                 factor(y, k, aa1, aa);
     162             :         } else {
     163           0 :             k = k / coeff - (k / coeff - (k - 1) / coeff) *
     164           0 :                 factor(y, k, aa1, aa);
     165             :         }
     166           0 :         break;
     167           0 :     case 1:
     168             :         // logarithmic
     169           0 :         y = sqr * log(fabs(in)) + sqr * sqr;
     170           0 :         k = roundf(y);
     171           0 :         if(!in) {
     172           0 :             k = 0;
     173           0 :         } else if (k - aa1 <= y && y <= k + aa1) {
     174           0 :             k = in / fabs(in) * exp(k / sqr - sqr);
     175           0 :         } else if (y > k + aa1) {
     176           0 :             double x = exp(k / sqr - sqr);
     177           0 :             k = FFSIGN(in) * (x + (exp((k + 1) / sqr - sqr) - x) *
     178           0 :                 factor(y, k, aa1, aa));
     179             :         } else {
     180           0 :             double x = exp(k / sqr - sqr);
     181           0 :             k = in / fabs(in) * (x - (x - exp((k - 1) / sqr - sqr)) *
     182           0 :                 factor(y, k, aa1, aa));
     183             :         }
     184           0 :         break;
     185             :     }
     186             : 
     187             :     // mix between dry and wet signal
     188           0 :     k += (in - k) * s->mix;
     189             : 
     190             :     // remove dc
     191           0 :     k = remove_dc(k, s->dc, s->idc);
     192             : 
     193           0 :     return k;
     194             : }
     195             : 
     196           0 : static double lfo_get(LFOContext *lfo)
     197             : {
     198           0 :     double phs = FFMIN(100., lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
     199             :     double val;
     200             : 
     201           0 :     if (phs > 1)
     202           0 :         phs = fmod(phs, 1.);
     203             : 
     204           0 :     val = sin((phs * 360.) * M_PI / 180);
     205             : 
     206           0 :     return val * lfo->amount;
     207             : }
     208             : 
     209           0 : static void lfo_advance(LFOContext *lfo, unsigned count)
     210             : {
     211           0 :     lfo->phase = fabs(lfo->phase + count * lfo->freq * (1. / lfo->srate));
     212           0 :     if (lfo->phase >= 1.)
     213           0 :         lfo->phase = fmod(lfo->phase, 1.);
     214           0 : }
     215             : 
     216           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     217             : {
     218           0 :     AVFilterContext *ctx = inlink->dst;
     219           0 :     ACrusherContext *s = ctx->priv;
     220           0 :     AVFilterLink *outlink = ctx->outputs[0];
     221             :     AVFrame *out;
     222           0 :     const double *src = (const double *)in->data[0];
     223             :     double *dst;
     224           0 :     const double level_in = s->level_in;
     225           0 :     const double level_out = s->level_out;
     226           0 :     const double mix = s->mix;
     227             :     int n, c;
     228             : 
     229           0 :     if (av_frame_is_writable(in)) {
     230           0 :         out = in;
     231             :     } else {
     232           0 :         out = ff_get_audio_buffer(inlink, in->nb_samples);
     233           0 :         if (!out) {
     234           0 :             av_frame_free(&in);
     235           0 :             return AVERROR(ENOMEM);
     236             :         }
     237           0 :         av_frame_copy_props(out, in);
     238             :     }
     239             : 
     240           0 :     dst = (double *)out->data[0];
     241           0 :     for (n = 0; n < in->nb_samples; n++) {
     242           0 :         if (s->is_lfo) {
     243           0 :             s->samples = s->smin + s->sdiff * (lfo_get(&s->lfo) + 0.5);
     244           0 :             s->round = round(s->samples);
     245             :         }
     246             : 
     247           0 :         for (c = 0; c < inlink->channels; c++) {
     248           0 :             double sample = src[c] * level_in;
     249             : 
     250           0 :             sample = mix * samplereduction(s, &s->sr[c], sample) + src[c] * (1. - mix) * level_in;
     251           0 :             dst[c] = bitreduction(s, sample) * level_out;
     252             :         }
     253           0 :         src += c;
     254           0 :         dst += c;
     255             : 
     256           0 :         if (s->is_lfo)
     257           0 :             lfo_advance(&s->lfo, 1);
     258             :     }
     259             : 
     260           0 :     if (in != out)
     261           0 :         av_frame_free(&in);
     262             : 
     263           0 :     return ff_filter_frame(outlink, out);
     264             : }
     265             : 
     266           0 : static int query_formats(AVFilterContext *ctx)
     267             : {
     268             :     AVFilterFormats *formats;
     269             :     AVFilterChannelLayouts *layouts;
     270             :     static const enum AVSampleFormat sample_fmts[] = {
     271             :         AV_SAMPLE_FMT_DBL,
     272             :         AV_SAMPLE_FMT_NONE
     273             :     };
     274             :     int ret;
     275             : 
     276           0 :     layouts = ff_all_channel_counts();
     277           0 :     if (!layouts)
     278           0 :         return AVERROR(ENOMEM);
     279           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
     280           0 :     if (ret < 0)
     281           0 :         return ret;
     282             : 
     283           0 :     formats = ff_make_format_list(sample_fmts);
     284           0 :     if (!formats)
     285           0 :         return AVERROR(ENOMEM);
     286           0 :     ret = ff_set_common_formats(ctx, formats);
     287           0 :     if (ret < 0)
     288           0 :         return ret;
     289             : 
     290           0 :     formats = ff_all_samplerates();
     291           0 :     if (!formats)
     292           0 :         return AVERROR(ENOMEM);
     293           0 :     return ff_set_common_samplerates(ctx, formats);
     294             : }
     295             : 
     296           0 : static av_cold void uninit(AVFilterContext *ctx)
     297             : {
     298           0 :     ACrusherContext *s = ctx->priv;
     299             : 
     300           0 :     av_freep(&s->sr);
     301           0 : }
     302             : 
     303           0 : static int config_input(AVFilterLink *inlink)
     304             : {
     305           0 :     AVFilterContext *ctx = inlink->dst;
     306           0 :     ACrusherContext *s = ctx->priv;
     307             :     double rad, sunder, smax, sover;
     308             : 
     309           0 :     s->idc = 1. / s->dc;
     310           0 :     s->coeff = exp2(s->bits) - 1;
     311           0 :     s->sqr = sqrt(s->coeff / 2);
     312           0 :     s->aa1 = (1. - s->aa) / 2.;
     313           0 :     s->round = round(s->samples);
     314           0 :     rad = s->lforange / 2.;
     315           0 :     s->smin = FFMAX(s->samples - rad, 1.);
     316           0 :     sunder   = s->samples - rad - s->smin;
     317           0 :     smax = FFMIN(s->samples + rad, 250.);
     318           0 :     sover    = s->samples + rad - smax;
     319           0 :     smax    -= sunder;
     320           0 :     s->smin -= sover;
     321           0 :     s->sdiff = smax - s->smin;
     322             : 
     323           0 :     s->lfo.freq = s->lforate;
     324           0 :     s->lfo.pwidth = 1.;
     325           0 :     s->lfo.srate = inlink->sample_rate;
     326           0 :     s->lfo.amount = .5;
     327             : 
     328           0 :     s->sr = av_calloc(inlink->channels, sizeof(*s->sr));
     329           0 :     if (!s->sr)
     330           0 :         return AVERROR(ENOMEM);
     331             : 
     332           0 :     return 0;
     333             : }
     334             : 
     335             : static const AVFilterPad avfilter_af_acrusher_inputs[] = {
     336             :     {
     337             :         .name         = "default",
     338             :         .type         = AVMEDIA_TYPE_AUDIO,
     339             :         .config_props = config_input,
     340             :         .filter_frame = filter_frame,
     341             :     },
     342             :     { NULL }
     343             : };
     344             : 
     345             : static const AVFilterPad avfilter_af_acrusher_outputs[] = {
     346             :     {
     347             :         .name = "default",
     348             :         .type = AVMEDIA_TYPE_AUDIO,
     349             :     },
     350             :     { NULL }
     351             : };
     352             : 
     353             : AVFilter ff_af_acrusher = {
     354             :     .name          = "acrusher",
     355             :     .description   = NULL_IF_CONFIG_SMALL("Reduce audio bit resolution."),
     356             :     .priv_size     = sizeof(ACrusherContext),
     357             :     .priv_class    = &acrusher_class,
     358             :     .uninit        = uninit,
     359             :     .query_formats = query_formats,
     360             :     .inputs        = avfilter_af_acrusher_inputs,
     361             :     .outputs       = avfilter_af_acrusher_outputs,
     362             : };

Generated by: LCOV version 1.13