FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_chorus.c
Date: 2026-09-20 08:09:24
Exec Total Coverage
Lines: 120 156 76.9%
Functions: 7 7 100.0%
Branches: 58 96 60.4%

Line Branch Exec Source
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
40 typedef struct ChorusContext {
41 const AVClass *class;
42 float in_gain, out_gain;
43 char *delays_str;
44 char *decays_str;
45 char *speeds_str;
46 char *depths_str;
47 float *delays;
48 float *decays;
49 float *speeds;
50 float *depths;
51 uint8_t **chorusbuf;
52 int **phase;
53 int *length;
54 int32_t **lookup_table;
55 int *counter;
56 int num_chorus;
57 int max_samples;
58 int channels;
59 int modulation;
60 int fade_out;
61 int64_t next_pts;
62 } ChorusContext;
63
64 #define OFFSET(x) offsetof(ChorusContext, x)
65 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
66
67 static 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
77 AVFILTER_DEFINE_CLASS(chorus);
78
79 8 static void count_items(char *item_str, int *nb_items)
80 {
81 char *p;
82
83 8 *nb_items = 1;
84
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 8 times.
58 for (p = item_str; *p; p++) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (*p == '|')
86 (*nb_items)++;
87 }
88
89 8 }
90
91 8 static void fill_items(char *item_str, int *nb_items, float *items)
92 {
93 8 char *p, *saveptr = NULL;
94 8 int i, new_nb_items = 0;
95
96 8 p = item_str;
97
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 for (i = 0; i < *nb_items; i++) {
98 8 char *tstr = av_strtok(p, "|", &saveptr);
99 8 p = NULL;
100
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (tstr)
101 8 new_nb_items += sscanf(tstr, "%f", &items[new_nb_items]) == 1;
102 }
103
104 8 *nb_items = new_nb_items;
105 8 }
106
107 2 static av_cold int init(AVFilterContext *ctx)
108 {
109 2 ChorusContext *s = ctx->priv;
110 int nb_delays, nb_decays, nb_speeds, nb_depths;
111
112
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 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 2 count_items(s->delays_str, &nb_delays);
118 2 count_items(s->decays_str, &nb_decays);
119 2 count_items(s->speeds_str, &nb_speeds);
120 2 count_items(s->depths_str, &nb_depths);
121
122 2 s->delays = av_realloc_f(s->delays, nb_delays, sizeof(*s->delays));
123 2 s->decays = av_realloc_f(s->decays, nb_decays, sizeof(*s->decays));
124 2 s->speeds = av_realloc_f(s->speeds, nb_speeds, sizeof(*s->speeds));
125 2 s->depths = av_realloc_f(s->depths, nb_depths, sizeof(*s->depths));
126
127
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 if (!s->delays || !s->decays || !s->speeds || !s->depths)
128 return AVERROR(ENOMEM);
129
130 2 fill_items(s->delays_str, &nb_delays, s->delays);
131 2 fill_items(s->decays_str, &nb_decays, s->decays);
132 2 fill_items(s->speeds_str, &nb_speeds, s->speeds);
133 2 fill_items(s->depths_str, &nb_depths, s->depths);
134
135
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 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 2 s->num_chorus = nb_delays;
141
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int n = 0; n < s->num_chorus; n++) {
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(s->speeds[n] > 0)) {
149 av_log(ctx, AV_LOG_ERROR, "Speeds must be positive.\n");
150 return AVERROR(EINVAL);
151 }
152 }
153
154 2 s->length = av_calloc(s->num_chorus, sizeof(*s->length));
155 2 s->lookup_table = av_calloc(s->num_chorus, sizeof(*s->lookup_table));
156
157
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!s->length || !s->lookup_table)
158 return AVERROR(ENOMEM);
159
160 2 s->next_pts = AV_NOPTS_VALUE;
161
162 2 return 0;
163 }
164
165 1 static int config_output(AVFilterLink *outlink)
166 {
167 1 AVFilterContext *ctx = outlink->src;
168 1 ChorusContext *s = ctx->priv;
169 1 float sum_in_volume = 1.0;
170 int n;
171
172 1 s->channels = outlink->ch_layout.nb_channels;
173
174
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (n = 0; n < s->num_chorus; n++) {
175 1 int samples = (int) ((s->delays[n] + s->depths[n]) * outlink->sample_rate / 1000.0);
176 1 int depth_samples = (int) (s->depths[n] * outlink->sample_rate / 1000.0);
177
178 1 s->length[n] = av_clipd(outlink->sample_rate / s->speeds[n], 0, INT_MAX);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->length[n] < 1) {
180 av_log(ctx, AV_LOG_ERROR, "Speed %g is above the sample rate of %d.\n",
181 s->speeds[n], outlink->sample_rate);
182 return AVERROR(EINVAL);
183 }
184
185 1 s->lookup_table[n] = av_malloc_array(s->length[n], sizeof(*s->lookup_table[n]));
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->lookup_table[n])
187 return AVERROR(ENOMEM);
188
189 1 ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_S32, s->lookup_table[n],
190 1 s->length[n], 0., depth_samples, 0);
191 1 s->max_samples = FFMAX(s->max_samples, samples);
192 }
193
194
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (n = 0; n < s->num_chorus; n++)
195 1 sum_in_volume += s->decays[n];
196
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->in_gain * (sum_in_volume) > 1.0 / s->out_gain)
198 av_log(ctx, AV_LOG_WARNING, "output gain can cause saturation or clipping of output\n");
199
200 1 s->counter = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->counter));
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->counter)
202 return AVERROR(ENOMEM);
203
204 1 s->phase = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->phase));
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->phase)
206 return AVERROR(ENOMEM);
207
208
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (n = 0; n < outlink->ch_layout.nb_channels; n++) {
209 1 s->phase[n] = av_calloc(s->num_chorus, sizeof(int));
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->phase[n])
211 return AVERROR(ENOMEM);
212 }
213
214 1 s->fade_out = s->max_samples;
215
216 1 return av_samples_alloc_array_and_samples(&s->chorusbuf, NULL,
217 outlink->ch_layout.nb_channels,
218 s->max_samples,
219 1 outlink->format, 0);
220 }
221
222 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
223
224 11 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
225 {
226 11 AVFilterContext *ctx = inlink->dst;
227 11 ChorusContext *s = ctx->priv;
228 AVFrame *out_frame;
229 int c, i, n;
230
231
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 if (av_frame_is_writable(frame)) {
232 11 out_frame = frame;
233 } else {
234 out_frame = ff_get_audio_buffer(ctx->outputs[0], frame->nb_samples);
235 if (!out_frame) {
236 av_frame_free(&frame);
237 return AVERROR(ENOMEM);
238 }
239 av_frame_copy_props(out_frame, frame);
240 }
241
242
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
243 11 const float *src = (const float *)frame->extended_data[c];
244 11 float *dst = (float *)out_frame->extended_data[c];
245 11 float *chorusbuf = (float *)s->chorusbuf[c];
246 11 int *phase = s->phase[c];
247
248
2/2
✓ Branch 0 taken 22528 times.
✓ Branch 1 taken 11 times.
22539 for (i = 0; i < frame->nb_samples; i++) {
249 22528 float out, in = src[i];
250
251 22528 out = in * s->in_gain;
252
253
2/2
✓ Branch 0 taken 22528 times.
✓ Branch 1 taken 22528 times.
45056 for (n = 0; n < s->num_chorus; n++) {
254
2/2
✓ Branch 0 taken 22149 times.
✓ Branch 1 taken 379 times.
22528 out += chorusbuf[MOD(s->max_samples + s->counter[c] -
255 s->lookup_table[n][phase[n]],
256 22528 s->max_samples)] * s->decays[n];
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22528 times.
22528 phase[n] = MOD(phase[n] + 1, s->length[n]);
258 }
259
260 22528 out *= s->out_gain;
261
262 22528 dst[i] = out;
263
264 22528 chorusbuf[s->counter[c]] = in;
265
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 22513 times.
22528 s->counter[c] = MOD(s->counter[c] + 1, s->max_samples);
266 }
267 }
268
269 11 s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (frame != out_frame)
272 av_frame_free(&frame);
273
274 11 return ff_filter_frame(ctx->outputs[0], out_frame);
275 }
276
277 10 static int request_frame(AVFilterLink *outlink)
278 {
279 10 AVFilterContext *ctx = outlink->src;
280 10 ChorusContext *s = ctx->priv;
281 int ret;
282
283 10 ret = ff_request_frame(ctx->inputs[0]);
284
285
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
10 if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
286 int nb_samples = FFMIN(s->fade_out, 2048);
287 AVFrame *frame;
288
289 frame = ff_get_audio_buffer(outlink, nb_samples);
290 if (!frame)
291 return AVERROR(ENOMEM);
292 s->fade_out -= nb_samples;
293
294 av_samples_set_silence(frame->extended_data, 0,
295 frame->nb_samples,
296 outlink->ch_layout.nb_channels,
297 frame->format);
298
299 frame->pts = s->next_pts;
300 if (s->next_pts != AV_NOPTS_VALUE)
301 s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
302
303 ret = filter_frame(ctx->inputs[0], frame);
304 }
305
306 10 return ret;
307 }
308
309 2 static av_cold void uninit(AVFilterContext *ctx)
310 {
311 2 ChorusContext *s = ctx->priv;
312 int n;
313
314 2 av_freep(&s->delays);
315 2 av_freep(&s->decays);
316 2 av_freep(&s->speeds);
317 2 av_freep(&s->depths);
318
319
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->chorusbuf)
320 1 av_freep(&s->chorusbuf[0]);
321 2 av_freep(&s->chorusbuf);
322
323
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->phase)
324
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (n = 0; n < s->channels; n++)
325 1 av_freep(&s->phase[n]);
326 2 av_freep(&s->phase);
327
328 2 av_freep(&s->counter);
329 2 av_freep(&s->length);
330
331
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->lookup_table)
332
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (n = 0; n < s->num_chorus; n++)
333 2 av_freep(&s->lookup_table[n]);
334 2 av_freep(&s->lookup_table);
335 2 }
336
337 static const AVFilterPad chorus_inputs[] = {
338 {
339 .name = "default",
340 .type = AVMEDIA_TYPE_AUDIO,
341 .filter_frame = filter_frame,
342 },
343 };
344
345 static const AVFilterPad chorus_outputs[] = {
346 {
347 .name = "default",
348 .type = AVMEDIA_TYPE_AUDIO,
349 .request_frame = request_frame,
350 .config_props = config_output,
351 },
352 };
353
354 const FFFilter ff_af_chorus = {
355 .p.name = "chorus",
356 .p.description = NULL_IF_CONFIG_SMALL("Add a chorus effect to the audio."),
357 .p.priv_class = &chorus_class,
358 .priv_size = sizeof(ChorusContext),
359 .init = init,
360 .uninit = uninit,
361 FILTER_INPUTS(chorus_inputs),
362 FILTER_OUTPUTS(chorus_outputs),
363 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
364 };
365