LCOV - code coverage report
Current view: top level - src/libavfilter - af_vibrato.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 79 0.0 %
Date: 2017-01-24 04:42:20 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
       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             : #include "generate_wave_table.h"
      26             : 
      27             : typedef struct VibratoContext {
      28             :     const AVClass *class;
      29             :     double freq;
      30             :     double depth;
      31             :     int channels;
      32             : 
      33             :     double **buf;
      34             :     int buf_index;
      35             :     int buf_size;
      36             : 
      37             :     double *wave_table;
      38             :     int wave_table_index;
      39             :     int wave_table_size;
      40             : } VibratoContext;
      41             : 
      42             : #define OFFSET(x) offsetof(VibratoContext, x)
      43             : #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      44             : 
      45             : static const AVOption vibrato_options[] = {
      46             :     { "f", "set frequency in hertz",    OFFSET(freq),    AV_OPT_TYPE_DOUBLE,   {.dbl = 5.0},   0.1,   20000.0, FLAGS },
      47             :     { "d", "set depth as percentage",   OFFSET(depth),   AV_OPT_TYPE_DOUBLE,   {.dbl = 0.5},   0.00,  1.0,     FLAGS },
      48             :     { NULL }
      49             : };
      50             : 
      51             : AVFILTER_DEFINE_CLASS(vibrato);
      52             : 
      53           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
      54             : {
      55           0 :     AVFilterContext *ctx = inlink->dst;
      56           0 :     VibratoContext *s = ctx->priv;
      57           0 :     AVFilterLink *outlink = ctx->outputs[0];
      58             :     AVFrame *out;
      59             :     int n, c;
      60             :     const double *src;
      61             :     double *dst;
      62             : 
      63           0 :     if (av_frame_is_writable(in)) {
      64           0 :         out = in;
      65             :     } else {
      66           0 :         out = ff_get_audio_buffer(inlink, in->nb_samples);
      67           0 :         if (!out) {
      68           0 :             av_frame_free(&in);
      69           0 :             return AVERROR(ENOMEM);
      70             :         }
      71           0 :         av_frame_copy_props(out, in);
      72             :     }
      73             : 
      74             : 
      75           0 :     for (n = 0; n < in->nb_samples; n++) {
      76             :         double integer, decimal;
      77           0 :         decimal = modf(s->depth * s->wave_table[s->wave_table_index], &integer);
      78             : 
      79           0 :         s->wave_table_index++;
      80           0 :         if (s->wave_table_index >= s->wave_table_size)
      81           0 :             s->wave_table_index -= s->wave_table_size;
      82             : 
      83           0 :         for (c = 0; c < inlink->channels; c++) {
      84             :             int samp1_index, samp2_index;
      85             :             double *buf;
      86             :             double this_samp;
      87             : 
      88           0 :             src = (const double *)in->extended_data[c];
      89           0 :             dst = (double *)out->extended_data[c];
      90           0 :             buf = s->buf[c];
      91             : 
      92           0 :             samp1_index = s->buf_index + integer;
      93           0 :             if (samp1_index >= s->buf_size)
      94           0 :                 samp1_index -= s->buf_size;
      95           0 :             samp2_index = samp1_index + 1;
      96           0 :             if (samp2_index >= s->buf_size)
      97           0 :                 samp2_index -= s->buf_size;
      98             : 
      99           0 :             this_samp = src[n];
     100           0 :             dst[n] = buf[samp1_index] + (decimal * (buf[samp2_index] - buf[samp1_index]));
     101           0 :             buf[s->buf_index] = this_samp;
     102             :         }
     103           0 :         s->buf_index++;
     104           0 :         if (s->buf_index >= s->buf_size)
     105           0 :             s->buf_index -= s->buf_size;
     106             :     }
     107             : 
     108           0 :     if (in != out)
     109           0 :         av_frame_free(&in);
     110             : 
     111           0 :     return ff_filter_frame(outlink, out);
     112             : }
     113             : 
     114           0 : static int query_formats(AVFilterContext *ctx)
     115             : {
     116             :     AVFilterFormats *formats;
     117             :     AVFilterChannelLayouts *layouts;
     118             :     static const enum AVSampleFormat sample_fmts[] = {
     119             :         AV_SAMPLE_FMT_DBLP,
     120             :         AV_SAMPLE_FMT_NONE
     121             :     };
     122             :     int ret;
     123             : 
     124           0 :     layouts = ff_all_channel_counts();
     125           0 :     if (!layouts)
     126           0 :         return AVERROR(ENOMEM);
     127           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
     128           0 :     if (ret < 0)
     129           0 :         return ret;
     130             : 
     131           0 :     formats = ff_make_format_list(sample_fmts);
     132           0 :     if (!formats)
     133           0 :         return AVERROR(ENOMEM);
     134           0 :     ret = ff_set_common_formats(ctx, formats);
     135           0 :     if (ret < 0)
     136           0 :         return ret;
     137             : 
     138           0 :     formats = ff_all_samplerates();
     139           0 :     if (!formats)
     140           0 :         return AVERROR(ENOMEM);
     141           0 :     return ff_set_common_samplerates(ctx, formats);
     142             : }
     143             : 
     144           0 : static av_cold void uninit(AVFilterContext *ctx)
     145             : {
     146           0 :     VibratoContext *s = ctx->priv;
     147             :     int c;
     148             : 
     149           0 :     av_freep(&s->wave_table);
     150           0 :     for (c = 0; c < s->channels; c++)
     151           0 :         av_freep(&s->buf[c]);
     152           0 :     av_freep(&s->buf);
     153           0 : }
     154             : 
     155           0 : static int config_input(AVFilterLink *inlink)
     156             : {
     157             :     int c;
     158           0 :     AVFilterContext *ctx = inlink->dst;
     159           0 :     VibratoContext *s = ctx->priv;
     160           0 :     s->channels = inlink->channels;
     161             : 
     162           0 :     s->buf = av_calloc(inlink->channels, sizeof(*s->buf));
     163           0 :     if (!s->buf)
     164           0 :         return AVERROR(ENOMEM);
     165           0 :     s->buf_size = inlink->sample_rate * 0.005;
     166           0 :     for (c = 0; c < s->channels; c++) {
     167           0 :         s->buf[c] = av_malloc_array(s->buf_size, sizeof(*s->buf[c]));
     168           0 :         if (!s->buf[c])
     169           0 :             return AVERROR(ENOMEM);
     170             :     }
     171           0 :     s->buf_index = 0;
     172             : 
     173           0 :     s->wave_table_size = inlink->sample_rate / s->freq;
     174           0 :     s->wave_table = av_malloc_array(s->wave_table_size, sizeof(*s->wave_table));
     175           0 :     if (!s->wave_table)
     176           0 :         return AVERROR(ENOMEM);
     177           0 :     ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_DBL, s->wave_table, s->wave_table_size, 0.0, s->buf_size - 1, 3.0 * M_PI_2);
     178           0 :     s->wave_table_index = 0;
     179             : 
     180           0 :     return 0;
     181             : }
     182             : 
     183             : static const AVFilterPad avfilter_af_vibrato_inputs[] = {
     184             :     {
     185             :         .name         = "default",
     186             :         .type         = AVMEDIA_TYPE_AUDIO,
     187             :         .config_props = config_input,
     188             :         .filter_frame = filter_frame,
     189             :     },
     190             :     { NULL }
     191             : };
     192             : 
     193             : static const AVFilterPad avfilter_af_vibrato_outputs[] = {
     194             :     {
     195             :         .name = "default",
     196             :         .type = AVMEDIA_TYPE_AUDIO,
     197             :     },
     198             :     { NULL }
     199             : };
     200             : 
     201             : AVFilter ff_af_vibrato = {
     202             :     .name          = "vibrato",
     203             :     .description   = NULL_IF_CONFIG_SMALL("Apply vibrato effect."),
     204             :     .priv_size     = sizeof(VibratoContext),
     205             :     .priv_class    = &vibrato_class,
     206             :     .uninit        = uninit,
     207             :     .query_formats = query_formats,
     208             :     .inputs        = avfilter_af_vibrato_inputs,
     209             :     .outputs       = avfilter_af_vibrato_outputs,
     210             : };

Generated by: LCOV version 1.12