FFmpeg
Loading...
Searching...
No Matches
af_chorus.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1998 Juergen Mueller And Sundry Contributors
3 * This source code is freely redistributable and may be used for
4 * any purpose. This copyright notice must be maintained.
5 * Juergen Mueller And Sundry Contributors are not responsible for
6 * the consequences of using this software.
7 *
8 * Copyright (c) 2015 Paul B Mahol
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27/**
28 * @file
29 * chorus audio filter
30 */
31
32#include "libavutil/avstring.h"
33#include "libavutil/mem.h"
34#include "libavutil/opt.h"
35#include "audio.h"
36#include "avfilter.h"
37#include "filters.h"
38#include "generate_wave_table.h"
39
63
64#define OFFSET(x) offsetof(ChorusContext, x)
65#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
66
67static const AVOption chorus_options[] = {
68 { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_FLOAT, {.dbl=.4}, 0, 1, A },
69 { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_FLOAT, {.dbl=.4}, 0, 1, A },
70 { "delays", "set delays", OFFSET(delays_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
71 { "decays", "set decays", OFFSET(decays_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
72 { "speeds", "set speeds", OFFSET(speeds_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
73 { "depths", "set depths", OFFSET(depths_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
74 { NULL }
75};
76
78
79static void count_items(char *item_str, int *nb_items)
80{
81 char *p;
82
83 *nb_items = 1;
84 for (p = item_str; *p; p++) {
85 if (*p == '|')
86 (*nb_items)++;
87 }
88
89}
90
91static void fill_items(char *item_str, int *nb_items, float *items)
92{
93 char *p, *saveptr = NULL;
94 int i, new_nb_items = 0;
95
96 p = item_str;
97 for (i = 0; i < *nb_items; i++) {
98 char *tstr = av_strtok(p, "|", &saveptr);
99 p = NULL;
100 if (tstr)
101 new_nb_items += sscanf(tstr, "%f", &items[new_nb_items]) == 1;
102 }
103
104 *nb_items = new_nb_items;
105}
106
108{
109 ChorusContext *s = ctx->priv;
110 int nb_delays, nb_decays, nb_speeds, nb_depths;
111
112 if (!s->delays_str || !s->decays_str || !s->speeds_str || !s->depths_str) {
113 av_log(ctx, AV_LOG_ERROR, "Both delays & decays & speeds & depths must be set.\n");
114 return AVERROR(EINVAL);
115 }
116
117 count_items(s->delays_str, &nb_delays);
118 count_items(s->decays_str, &nb_decays);
119 count_items(s->speeds_str, &nb_speeds);
120 count_items(s->depths_str, &nb_depths);
121
122 s->delays = av_realloc_f(s->delays, nb_delays, sizeof(*s->delays));
123 s->decays = av_realloc_f(s->decays, nb_decays, sizeof(*s->decays));
124 s->speeds = av_realloc_f(s->speeds, nb_speeds, sizeof(*s->speeds));
125 s->depths = av_realloc_f(s->depths, nb_depths, sizeof(*s->depths));
126
127 if (!s->delays || !s->decays || !s->speeds || !s->depths)
128 return AVERROR(ENOMEM);
129
130 fill_items(s->delays_str, &nb_delays, s->delays);
131 fill_items(s->decays_str, &nb_decays, s->decays);
132 fill_items(s->speeds_str, &nb_speeds, s->speeds);
133 fill_items(s->depths_str, &nb_depths, s->depths);
134
135 if (nb_delays != nb_decays && nb_delays != nb_speeds && nb_delays != nb_depths) {
136 av_log(ctx, AV_LOG_ERROR, "Number of delays & decays & speeds & depths given must be same.\n");
137 return AVERROR(EINVAL);
138 }
139
140 s->num_chorus = nb_delays;
141
142 if (s->num_chorus < 1) {
143 av_log(ctx, AV_LOG_ERROR, "At least one delay & decay & speed & depth must be set.\n");
144 return AVERROR(EINVAL);
145 }
146
147 s->length = av_calloc(s->num_chorus, sizeof(*s->length));
148 s->lookup_table = av_calloc(s->num_chorus, sizeof(*s->lookup_table));
149
150 if (!s->length || !s->lookup_table)
151 return AVERROR(ENOMEM);
152
153 s->next_pts = AV_NOPTS_VALUE;
154
155 return 0;
156}
157
158static int config_output(AVFilterLink *outlink)
159{
160 AVFilterContext *ctx = outlink->src;
161 ChorusContext *s = ctx->priv;
162 float sum_in_volume = 1.0;
163 int n;
164
165 s->channels = outlink->ch_layout.nb_channels;
166
167 for (n = 0; n < s->num_chorus; n++) {
168 int samples = (int) ((s->delays[n] + s->depths[n]) * outlink->sample_rate / 1000.0);
169 int depth_samples = (int) (s->depths[n] * outlink->sample_rate / 1000.0);
170
171 s->length[n] = outlink->sample_rate / s->speeds[n];
172
173 s->lookup_table[n] = av_malloc(sizeof(int32_t) * s->length[n]);
174 if (!s->lookup_table[n])
175 return AVERROR(ENOMEM);
176
178 s->length[n], 0., depth_samples, 0);
179 s->max_samples = FFMAX(s->max_samples, samples);
180 }
181
182 for (n = 0; n < s->num_chorus; n++)
183 sum_in_volume += s->decays[n];
184
185 if (s->in_gain * (sum_in_volume) > 1.0 / s->out_gain)
186 av_log(ctx, AV_LOG_WARNING, "output gain can cause saturation or clipping of output\n");
187
188 s->counter = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->counter));
189 if (!s->counter)
190 return AVERROR(ENOMEM);
191
192 s->phase = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->phase));
193 if (!s->phase)
194 return AVERROR(ENOMEM);
195
196 for (n = 0; n < outlink->ch_layout.nb_channels; n++) {
197 s->phase[n] = av_calloc(s->num_chorus, sizeof(int));
198 if (!s->phase[n])
199 return AVERROR(ENOMEM);
200 }
201
202 s->fade_out = s->max_samples;
203
204 return av_samples_alloc_array_and_samples(&s->chorusbuf, NULL,
205 outlink->ch_layout.nb_channels,
206 s->max_samples,
207 outlink->format, 0);
208}
209
210#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
211
213{
214 AVFilterContext *ctx = inlink->dst;
215 ChorusContext *s = ctx->priv;
216 AVFrame *out_frame;
217 int c, i, n;
218
220 out_frame = frame;
221 } else {
222 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
223 if (!out_frame) {
225 return AVERROR(ENOMEM);
226 }
227 av_frame_copy_props(out_frame, frame);
228 }
229
230 for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
231 const float *src = (const float *)frame->extended_data[c];
232 float *dst = (float *)out_frame->extended_data[c];
233 float *chorusbuf = (float *)s->chorusbuf[c];
234 int *phase = s->phase[c];
235
236 for (i = 0; i < frame->nb_samples; i++) {
237 float out, in = src[i];
238
239 out = in * s->in_gain;
240
241 for (n = 0; n < s->num_chorus; n++) {
242 out += chorusbuf[MOD(s->max_samples + s->counter[c] -
243 s->lookup_table[n][phase[n]],
244 s->max_samples)] * s->decays[n];
245 phase[n] = MOD(phase[n] + 1, s->length[n]);
246 }
247
248 out *= s->out_gain;
249
250 dst[i] = out;
251
252 chorusbuf[s->counter[c]] = in;
253 s->counter[c] = MOD(s->counter[c] + 1, s->max_samples);
254 }
255 }
256
257 s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
258
259 if (frame != out_frame)
261
262 return ff_filter_frame(ctx->outputs[0], out_frame);
263}
264
265static int request_frame(AVFilterLink *outlink)
266{
267 AVFilterContext *ctx = outlink->src;
268 ChorusContext *s = ctx->priv;
269 int ret;
270
271 ret = ff_request_frame(ctx->inputs[0]);
272
273 if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
274 int nb_samples = FFMIN(s->fade_out, 2048);
275 AVFrame *frame;
276
277 frame = ff_get_audio_buffer(outlink, nb_samples);
278 if (!frame)
279 return AVERROR(ENOMEM);
280 s->fade_out -= nb_samples;
281
282 av_samples_set_silence(frame->extended_data, 0,
283 frame->nb_samples,
284 outlink->ch_layout.nb_channels,
285 frame->format);
286
287 frame->pts = s->next_pts;
288 if (s->next_pts != AV_NOPTS_VALUE)
289 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
290
291 ret = filter_frame(ctx->inputs[0], frame);
292 }
293
294 return ret;
295}
296
298{
299 ChorusContext *s = ctx->priv;
300 int n;
301
302 av_freep(&s->delays);
303 av_freep(&s->decays);
304 av_freep(&s->speeds);
305 av_freep(&s->depths);
306
307 if (s->chorusbuf)
308 av_freep(&s->chorusbuf[0]);
309 av_freep(&s->chorusbuf);
310
311 if (s->phase)
312 for (n = 0; n < s->channels; n++)
313 av_freep(&s->phase[n]);
314 av_freep(&s->phase);
315
316 av_freep(&s->counter);
317 av_freep(&s->length);
318
319 if (s->lookup_table)
320 for (n = 0; n < s->num_chorus; n++)
321 av_freep(&s->lookup_table[n]);
322 av_freep(&s->lookup_table);
323}
324
325static const AVFilterPad chorus_inputs[] = {
326 {
327 .name = "default",
328 .type = AVMEDIA_TYPE_AUDIO,
329 .filter_frame = filter_frame,
330 },
331};
332
333static const AVFilterPad chorus_outputs[] = {
334 {
335 .name = "default",
336 .type = AVMEDIA_TYPE_AUDIO,
337 .request_frame = request_frame,
338 .config_props = config_output,
339 },
340};
341
343 .p.name = "chorus",
344 .p.description = NULL_IF_CONFIG_SMALL("Add a chorus effect to the audio."),
345 .p.priv_class = &chorus_class,
346 .priv_size = sizeof(ChorusContext),
347 .init = init,
348 .uninit = uninit,
352};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition af_chorus.c:212
static int request_frame(AVFilterLink *outlink)
Definition af_chorus.c:265
static const AVFilterPad chorus_inputs[]
Definition af_chorus.c:325
#define MOD(a, b)
Definition af_chorus.c:210
static const AVFilterPad chorus_outputs[]
Definition af_chorus.c:333
static av_cold void uninit(AVFilterContext *ctx)
Definition af_chorus.c:297
static const AVOption chorus_options[]
Definition af_chorus.c:67
#define OFFSET(x)
Definition af_chorus.c:64
static int config_output(AVFilterLink *outlink)
Definition af_chorus.c:158
const FFFilter ff_af_chorus
Definition af_chorus.c:342
static void count_items(char *item_str, int *nb_items)
Definition af_chorus.c:79
static void fill_items(char *item_str, int *nb_items, float *items)
Definition af_chorus.c:91
#define A(x)
Definition vpx_arith.h:28
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int32_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
void ff_generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt, void *table, int table_size, double min, double max, double phase)
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:246
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a data pointers array, samples buffer for nb_samples samples, and fill data pointers and lin...
Definition samplefmt.c:207
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
float in_gain
Definition af_chorus.c:42
int * counter
Definition af_chorus.c:55
int64_t next_pts
Definition af_chorus.c:61
float * delays
Definition af_chorus.c:47
float out_gain
Definition af_chorus.c:42
int ** phase
Definition af_chorus.c:52
char * depths_str
Definition af_chorus.c:46
char * delays_str
Definition af_chorus.c:43
float * decays
Definition af_chorus.c:48
float * speeds
Definition af_chorus.c:49
char * speeds_str
Definition af_chorus.c:45
char * decays_str
Definition af_chorus.c:44
uint8_t ** chorusbuf
Definition af_chorus.c:51
int32_t ** lookup_table
Definition af_chorus.c:54
float * depths
Definition af_chorus.c:50
#define av_realloc_f(p, o, n)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static double c[64]