]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_chorus.c
vaapi: define a unique pixel format for VA-API (AV_PIX_FMT_VAAPI).
[ffmpeg] / libavfilter / af_chorus.c
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/opt.h"
34 #include "audio.h"
35 #include "avfilter.h"
36 #include "internal.h"
37 #include "generate_wave_table.h"
38
39 typedef struct ChorusContext {
40     const AVClass *class;
41     float in_gain, out_gain;
42     char *delays_str;
43     char *decays_str;
44     char *speeds_str;
45     char *depths_str;
46     float *delays;
47     float *decays;
48     float *speeds;
49     float *depths;
50     uint8_t **chorusbuf;
51     int **phase;
52     int *length;
53     int32_t **lookup_table;
54     int *counter;
55     int num_chorus;
56     int max_samples;
57     int channels;
58     int modulation;
59     int fade_out;
60     int64_t next_pts;
61 } ChorusContext;
62
63 #define OFFSET(x) offsetof(ChorusContext, x)
64 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
65
66 static const AVOption chorus_options[] = {
67     { "in_gain",  "set input gain",  OFFSET(in_gain),    AV_OPT_TYPE_FLOAT,  {.dbl=.4}, 0, 1, A },
68     { "out_gain", "set output gain", OFFSET(out_gain),   AV_OPT_TYPE_FLOAT,  {.dbl=.4}, 0, 1, A },
69     { "delays",   "set delays",      OFFSET(delays_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
70     { "decays",   "set decays",      OFFSET(decays_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
71     { "speeds",   "set speeds",      OFFSET(speeds_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
72     { "depths",   "set depths",      OFFSET(depths_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
73     { NULL }
74 };
75
76 AVFILTER_DEFINE_CLASS(chorus);
77
78 static void count_items(char *item_str, int *nb_items)
79 {
80     char *p;
81
82     *nb_items = 1;
83     for (p = item_str; *p; p++) {
84         if (*p == '|')
85             (*nb_items)++;
86     }
87
88 }
89
90 static void fill_items(char *item_str, int *nb_items, float *items)
91 {
92     char *p, *saveptr = NULL;
93     int i, new_nb_items = 0;
94
95     p = item_str;
96     for (i = 0; i < *nb_items; i++) {
97         char *tstr = av_strtok(p, "|", &saveptr);
98         p = NULL;
99         new_nb_items += sscanf(tstr, "%f", &items[i]) == 1;
100     }
101
102     *nb_items = new_nb_items;
103 }
104
105 static av_cold int init(AVFilterContext *ctx)
106 {
107     ChorusContext *s = ctx->priv;
108     int nb_delays, nb_decays, nb_speeds, nb_depths;
109
110     if (!s->delays_str || !s->decays_str || !s->speeds_str || !s->depths_str) {
111         av_log(ctx, AV_LOG_ERROR, "Both delays & decays & speeds & depths must be set.\n");
112         return AVERROR(EINVAL);
113     }
114
115     count_items(s->delays_str, &nb_delays);
116     count_items(s->decays_str, &nb_decays);
117     count_items(s->speeds_str, &nb_speeds);
118     count_items(s->depths_str, &nb_depths);
119
120     s->delays = av_realloc_f(s->delays, nb_delays, sizeof(*s->delays));
121     s->decays = av_realloc_f(s->decays, nb_decays, sizeof(*s->decays));
122     s->speeds = av_realloc_f(s->speeds, nb_speeds, sizeof(*s->speeds));
123     s->depths = av_realloc_f(s->depths, nb_depths, sizeof(*s->depths));
124
125     if (!s->delays || !s->decays || !s->speeds || !s->depths)
126         return AVERROR(ENOMEM);
127
128     fill_items(s->delays_str, &nb_delays, s->delays);
129     fill_items(s->decays_str, &nb_decays, s->decays);
130     fill_items(s->speeds_str, &nb_speeds, s->speeds);
131     fill_items(s->depths_str, &nb_depths, s->depths);
132
133     if (nb_delays != nb_decays && nb_delays != nb_speeds && nb_delays != nb_depths) {
134         av_log(ctx, AV_LOG_ERROR, "Number of delays & decays & speeds & depths given must be same.\n");
135         return AVERROR(EINVAL);
136     }
137
138     s->num_chorus = nb_delays;
139
140     if (s->num_chorus < 1) {
141         av_log(ctx, AV_LOG_ERROR, "At least one delay & decay & speed & depth must be set.\n");
142         return AVERROR(EINVAL);
143     }
144
145     s->length = av_calloc(s->num_chorus, sizeof(*s->length));
146     s->lookup_table = av_calloc(s->num_chorus, sizeof(*s->lookup_table));
147
148     if (!s->length || !s->lookup_table)
149         return AVERROR(ENOMEM);
150
151     s->next_pts = AV_NOPTS_VALUE;
152
153     return 0;
154 }
155
156 static int query_formats(AVFilterContext *ctx)
157 {
158     AVFilterFormats *formats;
159     AVFilterChannelLayouts *layouts;
160     static const enum AVSampleFormat sample_fmts[] = {
161         AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
162     };
163     int ret;
164
165     layouts = ff_all_channel_layouts();
166     if (!layouts)
167         return AVERROR(ENOMEM);
168     ret = ff_set_common_channel_layouts(ctx, layouts);
169     if (ret < 0)
170         return ret;
171
172     formats = ff_make_format_list(sample_fmts);
173     if (!formats)
174         return AVERROR(ENOMEM);
175     ret = ff_set_common_formats(ctx, formats);
176     if (ret < 0)
177         return ret;
178
179     formats = ff_all_samplerates();
180     if (!formats)
181         return AVERROR(ENOMEM);
182     return ff_set_common_samplerates(ctx, formats);
183 }
184
185 static int config_output(AVFilterLink *outlink)
186 {
187     AVFilterContext *ctx = outlink->src;
188     ChorusContext *s = ctx->priv;
189     float sum_in_volume = 1.0;
190     int n;
191
192     s->channels = outlink->channels;
193
194     for (n = 0; n < s->num_chorus; n++) {
195         int samples = (int) ((s->delays[n] + s->depths[n]) * outlink->sample_rate / 1000.0);
196         int depth_samples = (int) (s->depths[n] * outlink->sample_rate / 1000.0);
197
198         s->length[n] = outlink->sample_rate / s->speeds[n];
199
200         s->lookup_table[n] = av_malloc(sizeof(int32_t) * s->length[n]);
201         if (!s->lookup_table[n])
202             return AVERROR(ENOMEM);
203
204         ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_S32, s->lookup_table[n],
205                                s->length[n], 0., depth_samples, 0);
206         s->max_samples = FFMAX(s->max_samples, samples);
207     }
208
209     for (n = 0; n < s->num_chorus; n++)
210         sum_in_volume += s->decays[n];
211
212     if (s->in_gain * (sum_in_volume) > 1.0 / s->out_gain)
213         av_log(ctx, AV_LOG_WARNING, "output gain can cause saturation or clipping of output\n");
214
215     s->counter = av_calloc(outlink->channels, sizeof(*s->counter));
216     if (!s->counter)
217         return AVERROR(ENOMEM);
218
219     s->phase = av_calloc(outlink->channels, sizeof(*s->phase));
220     if (!s->phase)
221         return AVERROR(ENOMEM);
222
223     for (n = 0; n < outlink->channels; n++) {
224         s->phase[n] = av_calloc(s->num_chorus, sizeof(int));
225         if (!s->phase[n])
226             return AVERROR(ENOMEM);
227     }
228
229     s->fade_out = s->max_samples;
230
231     return av_samples_alloc_array_and_samples(&s->chorusbuf, NULL,
232                                               outlink->channels,
233                                               s->max_samples,
234                                               outlink->format, 0);
235 }
236
237 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
238
239 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
240 {
241     AVFilterContext *ctx = inlink->dst;
242     ChorusContext *s = ctx->priv;
243     AVFrame *out_frame;
244     int c, i, n;
245
246     if (av_frame_is_writable(frame)) {
247         out_frame = frame;
248     } else {
249         out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
250         if (!out_frame)
251             return AVERROR(ENOMEM);
252         av_frame_copy_props(out_frame, frame);
253     }
254
255     for (c = 0; c < inlink->channels; c++) {
256         const float *src = (const float *)frame->extended_data[c];
257         float *dst = (float *)out_frame->extended_data[c];
258         float *chorusbuf = (float *)s->chorusbuf[c];
259         int *phase = s->phase[c];
260
261         for (i = 0; i < frame->nb_samples; i++) {
262             float out, in = src[i];
263
264             out = in * s->in_gain;
265
266             for (n = 0; n < s->num_chorus; n++) {
267                 out += chorusbuf[MOD(s->max_samples + s->counter[c] -
268                                      s->lookup_table[n][phase[n]],
269                                      s->max_samples)] * s->decays[n];
270                 phase[n] = MOD(phase[n] + 1, s->length[n]);
271             }
272
273             out *= s->out_gain;
274
275             dst[i] = out;
276
277             chorusbuf[s->counter[c]] = in;
278             s->counter[c] = MOD(s->counter[c] + 1, s->max_samples);
279         }
280     }
281
282     s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
283
284     if (frame != out_frame)
285         av_frame_free(&frame);
286
287     return ff_filter_frame(ctx->outputs[0], out_frame);
288 }
289
290 static int request_frame(AVFilterLink *outlink)
291 {
292     AVFilterContext *ctx = outlink->src;
293     ChorusContext *s = ctx->priv;
294     int ret;
295
296     ret = ff_request_frame(ctx->inputs[0]);
297
298     if (ret == AVERROR_EOF && !ctx->is_disabled && s->fade_out) {
299         int nb_samples = FFMIN(s->fade_out, 2048);
300         AVFrame *frame;
301
302         frame = ff_get_audio_buffer(outlink, nb_samples);
303         if (!frame)
304             return AVERROR(ENOMEM);
305         s->fade_out -= nb_samples;
306
307         av_samples_set_silence(frame->extended_data, 0,
308                                frame->nb_samples,
309                                outlink->channels,
310                                frame->format);
311
312         frame->pts = s->next_pts;
313         if (s->next_pts != AV_NOPTS_VALUE)
314             s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
315
316         ret = filter_frame(ctx->inputs[0], frame);
317     }
318
319     return ret;
320 }
321
322 static av_cold void uninit(AVFilterContext *ctx)
323 {
324     ChorusContext *s = ctx->priv;
325     int n;
326
327     av_freep(&s->delays);
328     av_freep(&s->decays);
329     av_freep(&s->speeds);
330     av_freep(&s->depths);
331
332     if (s->chorusbuf)
333         av_freep(&s->chorusbuf[0]);
334     av_freep(&s->chorusbuf);
335
336     if (s->phase)
337         for (n = 0; n < s->channels; n++)
338             av_freep(&s->phase[n]);
339     av_freep(&s->phase);
340
341     av_freep(&s->counter);
342     av_freep(&s->length);
343
344     if (s->lookup_table)
345         for (n = 0; n < s->num_chorus; n++)
346             av_freep(&s->lookup_table[n]);
347     av_freep(&s->lookup_table);
348 }
349
350 static const AVFilterPad chorus_inputs[] = {
351     {
352         .name         = "default",
353         .type         = AVMEDIA_TYPE_AUDIO,
354         .filter_frame = filter_frame,
355     },
356     { NULL }
357 };
358
359 static const AVFilterPad chorus_outputs[] = {
360     {
361         .name          = "default",
362         .type          = AVMEDIA_TYPE_AUDIO,
363         .request_frame = request_frame,
364         .config_props  = config_output,
365     },
366     { NULL }
367 };
368
369 AVFilter ff_af_chorus = {
370     .name          = "chorus",
371     .description   = NULL_IF_CONFIG_SMALL("Add a chorus effect to the audio."),
372     .query_formats = query_formats,
373     .priv_size     = sizeof(ChorusContext),
374     .priv_class    = &chorus_class,
375     .init          = init,
376     .uninit        = uninit,
377     .inputs        = chorus_inputs,
378     .outputs       = chorus_outputs,
379 };