FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_aphaser.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 0 61 0.0%
Functions: 0 12 0.0%
Branches: 0 113 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Paul B Mahol
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 /**
22 * @file
23 * phaser audio filter
24 */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "generate_wave_table.h"
33
34 typedef struct AudioPhaserContext {
35 const AVClass *class;
36 double in_gain, out_gain;
37 double delay;
38 double decay;
39 double speed;
40
41 int type;
42
43 int delay_buffer_length;
44 double *delay_buffer;
45
46 int modulation_buffer_length;
47 int32_t *modulation_buffer;
48
49 int delay_pos, modulation_pos;
50
51 void (*phaser)(struct AudioPhaserContext *s,
52 uint8_t * const *src, uint8_t **dst,
53 int nb_samples, int channels);
54 } AudioPhaserContext;
55
56 #define OFFSET(x) offsetof(AudioPhaserContext, x)
57 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58
59 static const AVOption aphaser_options[] = {
60 { "in_gain", "set input gain", OFFSET(in_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, 1, FLAGS },
61 { "out_gain", "set output gain", OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0, 1e9, FLAGS },
62 { "delay", "set delay in milliseconds", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=3.}, 0, 5, FLAGS },
63 { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=.4}, 0, .99, FLAGS },
64 { "speed", "set modulation speed", OFFSET(speed), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1, 2, FLAGS },
65 { "type", "set modulation type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, .unit = "type" },
66 { "triangular", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, .unit = "type" },
67 { "t", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_TRI}, 0, 0, FLAGS, .unit = "type" },
68 { "sinusoidal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, .unit = "type" },
69 { "s", NULL, 0, AV_OPT_TYPE_CONST, {.i64=WAVE_SIN}, 0, 0, FLAGS, .unit = "type" },
70 { NULL }
71 };
72
73 AVFILTER_DEFINE_CLASS(aphaser);
74
75 static av_cold int init(AVFilterContext *ctx)
76 {
77 AudioPhaserContext *s = ctx->priv;
78
79 if (s->in_gain > (1 - s->decay * s->decay))
80 av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
81 if (s->in_gain / (1 - s->decay) > 1 / s->out_gain)
82 av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
83
84 return 0;
85 }
86
87 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
88
89 #define PHASER_PLANAR(name, type) \
90 static void phaser_## name ##p(AudioPhaserContext *s, \
91 uint8_t * const *ssrc, uint8_t **ddst, \
92 int nb_samples, int channels) \
93 { \
94 int i, c, delay_pos, modulation_pos; \
95 \
96 av_assert0(channels > 0); \
97 for (c = 0; c < channels; c++) { \
98 type *src = (type *)ssrc[c]; \
99 type *dst = (type *)ddst[c]; \
100 double *buffer = s->delay_buffer + \
101 c * s->delay_buffer_length; \
102 \
103 delay_pos = s->delay_pos; \
104 modulation_pos = s->modulation_pos; \
105 \
106 for (i = 0; i < nb_samples; i++, src++, dst++) { \
107 double v = *src * s->in_gain + buffer[ \
108 MOD(delay_pos + s->modulation_buffer[ \
109 modulation_pos], \
110 s->delay_buffer_length)] * s->decay; \
111 \
112 modulation_pos = MOD(modulation_pos + 1, \
113 s->modulation_buffer_length); \
114 delay_pos = MOD(delay_pos + 1, s->delay_buffer_length); \
115 buffer[delay_pos] = v; \
116 \
117 *dst = v * s->out_gain; \
118 } \
119 } \
120 \
121 s->delay_pos = delay_pos; \
122 s->modulation_pos = modulation_pos; \
123 }
124
125 #define PHASER(name, type) \
126 static void phaser_## name (AudioPhaserContext *s, \
127 uint8_t * const *ssrc, uint8_t **ddst, \
128 int nb_samples, int channels) \
129 { \
130 int i, c, delay_pos, modulation_pos; \
131 type *src = (type *)ssrc[0]; \
132 type *dst = (type *)ddst[0]; \
133 double *buffer = s->delay_buffer; \
134 \
135 delay_pos = s->delay_pos; \
136 modulation_pos = s->modulation_pos; \
137 \
138 for (i = 0; i < nb_samples; i++) { \
139 int pos = MOD(delay_pos + s->modulation_buffer[modulation_pos], \
140 s->delay_buffer_length) * channels; \
141 int npos; \
142 \
143 delay_pos = MOD(delay_pos + 1, s->delay_buffer_length); \
144 npos = delay_pos * channels; \
145 for (c = 0; c < channels; c++, src++, dst++) { \
146 double v = *src * s->in_gain + buffer[pos + c] * s->decay; \
147 \
148 buffer[npos + c] = v; \
149 \
150 *dst = v * s->out_gain; \
151 } \
152 \
153 modulation_pos = MOD(modulation_pos + 1, \
154 s->modulation_buffer_length); \
155 } \
156 \
157 s->delay_pos = delay_pos; \
158 s->modulation_pos = modulation_pos; \
159 }
160
161 PHASER_PLANAR(dbl, double)
162 PHASER_PLANAR(flt, float)
163 PHASER_PLANAR(s16, int16_t)
164 PHASER_PLANAR(s32, int32_t)
165
166 PHASER(dbl, double)
167 PHASER(flt, float)
168 PHASER(s16, int16_t)
169 PHASER(s32, int32_t)
170
171 static int config_output(AVFilterLink *outlink)
172 {
173 AudioPhaserContext *s = outlink->src->priv;
174 AVFilterLink *inlink = outlink->src->inputs[0];
175
176 s->delay_buffer_length = s->delay * 0.001 * inlink->sample_rate + 0.5;
177 if (s->delay_buffer_length <= 0) {
178 av_log(outlink->src, AV_LOG_ERROR, "delay is too small\n");
179 return AVERROR(EINVAL);
180 }
181 s->delay_buffer = av_calloc(s->delay_buffer_length, sizeof(*s->delay_buffer) * inlink->ch_layout.nb_channels);
182 s->modulation_buffer_length = inlink->sample_rate / s->speed + 0.5;
183 s->modulation_buffer = av_malloc_array(s->modulation_buffer_length, sizeof(*s->modulation_buffer));
184
185 if (!s->modulation_buffer || !s->delay_buffer)
186 return AVERROR(ENOMEM);
187
188 ff_generate_wave_table(s->type, AV_SAMPLE_FMT_S32,
189 s->modulation_buffer, s->modulation_buffer_length,
190 1., s->delay_buffer_length, M_PI / 2.0);
191
192 s->delay_pos = s->modulation_pos = 0;
193
194 switch (inlink->format) {
195 case AV_SAMPLE_FMT_DBL: s->phaser = phaser_dbl; break;
196 case AV_SAMPLE_FMT_DBLP: s->phaser = phaser_dblp; break;
197 case AV_SAMPLE_FMT_FLT: s->phaser = phaser_flt; break;
198 case AV_SAMPLE_FMT_FLTP: s->phaser = phaser_fltp; break;
199 case AV_SAMPLE_FMT_S16: s->phaser = phaser_s16; break;
200 case AV_SAMPLE_FMT_S16P: s->phaser = phaser_s16p; break;
201 case AV_SAMPLE_FMT_S32: s->phaser = phaser_s32; break;
202 case AV_SAMPLE_FMT_S32P: s->phaser = phaser_s32p; break;
203 default: av_assert0(0);
204 }
205
206 return 0;
207 }
208
209 static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
210 {
211 AudioPhaserContext *s = inlink->dst->priv;
212 AVFilterLink *outlink = inlink->dst->outputs[0];
213 AVFrame *outbuf;
214
215 if (av_frame_is_writable(inbuf)) {
216 outbuf = inbuf;
217 } else {
218 outbuf = ff_get_audio_buffer(outlink, inbuf->nb_samples);
219 if (!outbuf) {
220 av_frame_free(&inbuf);
221 return AVERROR(ENOMEM);
222 }
223 av_frame_copy_props(outbuf, inbuf);
224 }
225
226 s->phaser(s, inbuf->extended_data, outbuf->extended_data,
227 outbuf->nb_samples, outbuf->ch_layout.nb_channels);
228
229 if (inbuf != outbuf)
230 av_frame_free(&inbuf);
231
232 return ff_filter_frame(outlink, outbuf);
233 }
234
235 static av_cold void uninit(AVFilterContext *ctx)
236 {
237 AudioPhaserContext *s = ctx->priv;
238
239 av_freep(&s->delay_buffer);
240 av_freep(&s->modulation_buffer);
241 }
242
243 static const AVFilterPad aphaser_inputs[] = {
244 {
245 .name = "default",
246 .type = AVMEDIA_TYPE_AUDIO,
247 .filter_frame = filter_frame,
248 },
249 };
250
251 static const AVFilterPad aphaser_outputs[] = {
252 {
253 .name = "default",
254 .type = AVMEDIA_TYPE_AUDIO,
255 .config_props = config_output,
256 },
257 };
258
259 const AVFilter ff_af_aphaser = {
260 .name = "aphaser",
261 .description = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
262 .priv_size = sizeof(AudioPhaserContext),
263 .init = init,
264 .uninit = uninit,
265 FILTER_INPUTS(aphaser_inputs),
266 FILTER_OUTPUTS(aphaser_outputs),
267 FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
268 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
269 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
270 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P),
271 .priv_class = &aphaser_class,
272 };
273