• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 typedef struct TremoloContext {
27     const AVClass *class;
28     double freq;
29     double depth;
30     double *table;
31     int table_size;
32     int index;
33 } TremoloContext;
34 
35 #define OFFSET(x) offsetof(TremoloContext, x)
36 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
37 
38 static const AVOption tremolo_options[] = {
39     { "f", "set frequency in hertz",    OFFSET(freq),    AV_OPT_TYPE_DOUBLE,   {.dbl = 5.0},   0.1,   20000.0, FLAGS },
40     { "d", "set depth as percentage",   OFFSET(depth),   AV_OPT_TYPE_DOUBLE,   {.dbl = 0.5},   0.0,   1.0,     FLAGS },
41     { NULL }
42 };
43 
44 AVFILTER_DEFINE_CLASS(tremolo);
45 
filter_frame(AVFilterLink * inlink,AVFrame * in)46 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
47 {
48     AVFilterContext *ctx = inlink->dst;
49     AVFilterLink *outlink = ctx->outputs[0];
50     TremoloContext *s = ctx->priv;
51     const double *src = (const double *)in->data[0];
52     const int channels = inlink->ch_layout.nb_channels;
53     const int nb_samples = in->nb_samples;
54     AVFrame *out;
55     double *dst;
56     int n, c;
57 
58     if (av_frame_is_writable(in)) {
59         out = in;
60     } else {
61         out = ff_get_audio_buffer(outlink, in->nb_samples);
62         if (!out) {
63             av_frame_free(&in);
64             return AVERROR(ENOMEM);
65         }
66         av_frame_copy_props(out, in);
67     }
68     dst = (double *)out->data[0];
69 
70     for (n = 0; n < nb_samples; n++) {
71         for (c = 0; c < channels; c++)
72             dst[c] = src[c] * s->table[s->index];
73         dst += channels;
74         src += channels;
75         s->index++;
76         if (s->index >= s->table_size)
77             s->index = 0;
78     }
79 
80     if (in != out)
81         av_frame_free(&in);
82 
83     return ff_filter_frame(outlink, out);
84 }
85 
uninit(AVFilterContext * ctx)86 static av_cold void uninit(AVFilterContext *ctx)
87 {
88     TremoloContext *s = ctx->priv;
89     av_freep(&s->table);
90 }
91 
config_input(AVFilterLink * inlink)92 static int config_input(AVFilterLink *inlink)
93 {
94     AVFilterContext *ctx = inlink->dst;
95     TremoloContext *s = ctx->priv;
96     const double offset = 1. - s->depth / 2.;
97     int i;
98 
99     s->table_size = lrint(inlink->sample_rate / s->freq + 0.5);
100     s->table = av_malloc_array(s->table_size, sizeof(*s->table));
101     if (!s->table)
102         return AVERROR(ENOMEM);
103 
104     for (i = 0; i < s->table_size; i++) {
105         double env = s->freq * i / inlink->sample_rate;
106         env = sin(2 * M_PI * fmod(env + 0.25, 1.0));
107         s->table[i] = env * (1 - fabs(offset)) + offset;
108     }
109 
110     s->index = 0;
111 
112     return 0;
113 }
114 
115 static const AVFilterPad avfilter_af_tremolo_inputs[] = {
116     {
117         .name         = "default",
118         .type         = AVMEDIA_TYPE_AUDIO,
119         .config_props = config_input,
120         .filter_frame = filter_frame,
121     },
122 };
123 
124 static const AVFilterPad avfilter_af_tremolo_outputs[] = {
125     {
126         .name = "default",
127         .type = AVMEDIA_TYPE_AUDIO,
128     },
129 };
130 
131 const AVFilter ff_af_tremolo = {
132     .name          = "tremolo",
133     .description   = NULL_IF_CONFIG_SMALL("Apply tremolo effect."),
134     .priv_size     = sizeof(TremoloContext),
135     .priv_class    = &tremolo_class,
136     .uninit        = uninit,
137     FILTER_INPUTS(avfilter_af_tremolo_inputs),
138     FILTER_OUTPUTS(avfilter_af_tremolo_outputs),
139     FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
140     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
141 };
142