Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52867 views
1
/*
2
* Copyright (c) 2015 Kyle Swanson <[email protected]>.
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
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
54
{
55
AVFilterContext *ctx = inlink->dst;
56
VibratoContext *s = ctx->priv;
57
AVFilterLink *outlink = ctx->outputs[0];
58
AVFrame *out;
59
int n, c;
60
const double *src;
61
double *dst;
62
63
if (av_frame_is_writable(in)) {
64
out = in;
65
} else {
66
out = ff_get_audio_buffer(inlink, in->nb_samples);
67
if (!out) {
68
av_frame_free(&in);
69
return AVERROR(ENOMEM);
70
}
71
av_frame_copy_props(out, in);
72
}
73
74
75
for (n = 0; n < in->nb_samples; n++) {
76
double integer, decimal;
77
decimal = modf(s->depth * s->wave_table[s->wave_table_index], &integer);
78
79
s->wave_table_index++;
80
if (s->wave_table_index >= s->wave_table_size)
81
s->wave_table_index -= s->wave_table_size;
82
83
for (c = 0; c < inlink->channels; c++) {
84
int samp1_index, samp2_index;
85
double *buf;
86
double this_samp;
87
88
src = (const double *)in->extended_data[c];
89
dst = (double *)out->extended_data[c];
90
buf = s->buf[c];
91
92
samp1_index = s->buf_index + integer;
93
if (samp1_index >= s->buf_size)
94
samp1_index -= s->buf_size;
95
samp2_index = samp1_index + 1;
96
if (samp2_index >= s->buf_size)
97
samp2_index -= s->buf_size;
98
99
this_samp = src[n];
100
dst[n] = buf[samp1_index] + (decimal * (buf[samp2_index] - buf[samp1_index]));
101
buf[s->buf_index] = this_samp;
102
}
103
s->buf_index++;
104
if (s->buf_index >= s->buf_size)
105
s->buf_index -= s->buf_size;
106
}
107
108
if (in != out)
109
av_frame_free(&in);
110
111
return ff_filter_frame(outlink, out);
112
}
113
114
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
layouts = ff_all_channel_counts();
125
if (!layouts)
126
return AVERROR(ENOMEM);
127
ret = ff_set_common_channel_layouts(ctx, layouts);
128
if (ret < 0)
129
return ret;
130
131
formats = ff_make_format_list(sample_fmts);
132
if (!formats)
133
return AVERROR(ENOMEM);
134
ret = ff_set_common_formats(ctx, formats);
135
if (ret < 0)
136
return ret;
137
138
formats = ff_all_samplerates();
139
if (!formats)
140
return AVERROR(ENOMEM);
141
return ff_set_common_samplerates(ctx, formats);
142
}
143
144
static av_cold void uninit(AVFilterContext *ctx)
145
{
146
VibratoContext *s = ctx->priv;
147
int c;
148
149
av_freep(&s->wave_table);
150
for (c = 0; c < s->channels; c++)
151
av_freep(&s->buf[c]);
152
av_freep(&s->buf);
153
}
154
155
static int config_input(AVFilterLink *inlink)
156
{
157
int c;
158
AVFilterContext *ctx = inlink->dst;
159
VibratoContext *s = ctx->priv;
160
s->channels = inlink->channels;
161
162
s->buf = av_calloc(inlink->channels, sizeof(*s->buf));
163
if (!s->buf)
164
return AVERROR(ENOMEM);
165
s->buf_size = inlink->sample_rate * 0.005;
166
for (c = 0; c < s->channels; c++) {
167
s->buf[c] = av_malloc_array(s->buf_size, sizeof(*s->buf[c]));
168
if (!s->buf[c])
169
return AVERROR(ENOMEM);
170
}
171
s->buf_index = 0;
172
173
s->wave_table_size = inlink->sample_rate / s->freq;
174
s->wave_table = av_malloc_array(s->wave_table_size, sizeof(*s->wave_table));
175
if (!s->wave_table)
176
return AVERROR(ENOMEM);
177
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
s->wave_table_index = 0;
179
180
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
};
211
212