FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_apulsator.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 0 97 0.0%
Functions: 0 5 0.0%
Branches: 0 50 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
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/avassert.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "audio.h"
27
28 enum PulsatorModes { SINE, TRIANGLE, SQUARE, SAWUP, SAWDOWN, NB_MODES };
29 enum PulsatorTimings { UNIT_BPM, UNIT_MS, UNIT_HZ, NB_TIMINGS };
30
31 typedef struct SimpleLFO {
32 double phase;
33 double freq;
34 double offset;
35 double amount;
36 double pwidth;
37 int mode;
38 int srate;
39 } SimpleLFO;
40
41 typedef struct AudioPulsatorContext {
42 const AVClass *class;
43 int mode;
44 double level_in;
45 double level_out;
46 double amount;
47 double offset_l;
48 double offset_r;
49 double pwidth;
50 double bpm;
51 double hertz;
52 int ms;
53 int timing;
54
55 SimpleLFO lfoL, lfoR;
56 } AudioPulsatorContext;
57
58 #define OFFSET(x) offsetof(AudioPulsatorContext, x)
59 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60
61 static const AVOption apulsator_options[] = {
62 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
63 { "level_out", "set output gain", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, FLAGS, },
64 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=SINE}, SINE, NB_MODES-1, FLAGS, "mode" },
65 { "sine", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SINE}, 0, 0, FLAGS, "mode" },
66 { "triangle", NULL, 0, AV_OPT_TYPE_CONST, {.i64=TRIANGLE},0, 0, FLAGS, "mode" },
67 { "square", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SQUARE}, 0, 0, FLAGS, "mode" },
68 { "sawup", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWUP}, 0, 0, FLAGS, "mode" },
69 { "sawdown", NULL, 0, AV_OPT_TYPE_CONST, {.i64=SAWDOWN}, 0, 0, FLAGS, "mode" },
70 { "amount", "set modulation", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
71 { "offset_l", "set offset L", OFFSET(offset_l), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
72 { "offset_r", "set offset R", OFFSET(offset_r), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
73 { "width", "set pulse width", OFFSET(pwidth), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 2, FLAGS },
74 { "timing", "set timing", OFFSET(timing), AV_OPT_TYPE_INT, {.i64=2}, 0, NB_TIMINGS-1, FLAGS, "timing" },
75 { "bpm", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_BPM}, 0, 0, FLAGS, "timing" },
76 { "ms", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_MS}, 0, 0, FLAGS, "timing" },
77 { "hz", NULL, 0, AV_OPT_TYPE_CONST, {.i64=UNIT_HZ}, 0, 0, FLAGS, "timing" },
78 { "bpm", "set BPM", OFFSET(bpm), AV_OPT_TYPE_DOUBLE, {.dbl=120}, 30, 300, FLAGS },
79 { "ms", "set ms", OFFSET(ms), AV_OPT_TYPE_INT, {.i64=500}, 10, 2000, FLAGS },
80 { "hz", "set frequency", OFFSET(hertz), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0.01, 100, FLAGS },
81 { NULL }
82 };
83
84 AVFILTER_DEFINE_CLASS(apulsator);
85
86 static void lfo_advance(SimpleLFO *lfo, unsigned count)
87 {
88 lfo->phase = fabs(lfo->phase + count * lfo->freq / lfo->srate);
89 if (lfo->phase >= 1)
90 lfo->phase = fmod(lfo->phase, 1);
91 }
92
93 static double lfo_get_value(SimpleLFO *lfo)
94 {
95 double phs = FFMIN(100, lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
96 double val;
97
98 if (phs > 1)
99 phs = fmod(phs, 1.);
100
101 switch (lfo->mode) {
102 case SINE:
103 val = sin(phs * 2 * M_PI);
104 break;
105 case TRIANGLE:
106 if (phs > 0.75)
107 val = (phs - 0.75) * 4 - 1;
108 else if (phs > 0.25)
109 val = -4 * phs + 2;
110 else
111 val = phs * 4;
112 break;
113 case SQUARE:
114 val = phs < 0.5 ? -1 : +1;
115 break;
116 case SAWUP:
117 val = phs * 2 - 1;
118 break;
119 case SAWDOWN:
120 val = 1 - phs * 2;
121 break;
122 default: av_assert0(0);
123 }
124
125 return val * lfo->amount;
126 }
127
128 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
129 {
130 AVFilterContext *ctx = inlink->dst;
131 AVFilterLink *outlink = ctx->outputs[0];
132 AudioPulsatorContext *s = ctx->priv;
133 const double *src = (const double *)in->data[0];
134 const int nb_samples = in->nb_samples;
135 const double level_out = s->level_out;
136 const double level_in = s->level_in;
137 const double amount = s->amount;
138 AVFrame *out;
139 double *dst;
140 int n;
141
142 if (av_frame_is_writable(in)) {
143 out = in;
144 } else {
145 out = ff_get_audio_buffer(inlink, in->nb_samples);
146 if (!out) {
147 av_frame_free(&in);
148 return AVERROR(ENOMEM);
149 }
150 av_frame_copy_props(out, in);
151 }
152 dst = (double *)out->data[0];
153
154 for (n = 0; n < nb_samples; n++) {
155 double outL;
156 double outR;
157 double inL = src[0] * level_in;
158 double inR = src[1] * level_in;
159 double procL = inL;
160 double procR = inR;
161
162 procL *= lfo_get_value(&s->lfoL) * 0.5 + amount / 2;
163 procR *= lfo_get_value(&s->lfoR) * 0.5 + amount / 2;
164
165 outL = procL + inL * (1 - amount);
166 outR = procR + inR * (1 - amount);
167
168 outL *= level_out;
169 outR *= level_out;
170
171 dst[0] = outL;
172 dst[1] = outR;
173
174 lfo_advance(&s->lfoL, 1);
175 lfo_advance(&s->lfoR, 1);
176
177 dst += 2;
178 src += 2;
179 }
180
181 if (in != out)
182 av_frame_free(&in);
183
184 return ff_filter_frame(outlink, out);
185 }
186
187 static int query_formats(AVFilterContext *ctx)
188 {
189 AVFilterChannelLayouts *layout = NULL;
190 AVFilterFormats *formats = NULL;
191 int ret;
192
193 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
194 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
195 (ret = ff_add_channel_layout (&layout , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
196 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
197 return ret;
198
199 return ff_set_common_all_samplerates(ctx);
200 }
201
202 static int config_input(AVFilterLink *inlink)
203 {
204 AVFilterContext *ctx = inlink->dst;
205 AudioPulsatorContext *s = ctx->priv;
206 double freq;
207
208 switch (s->timing) {
209 case UNIT_BPM: freq = s->bpm / 60; break;
210 case UNIT_MS: freq = 1 / (s->ms / 1000.); break;
211 case UNIT_HZ: freq = s->hertz; break;
212 default: av_assert0(0);
213 }
214
215 s->lfoL.freq = freq;
216 s->lfoR.freq = freq;
217 s->lfoL.mode = s->mode;
218 s->lfoR.mode = s->mode;
219 s->lfoL.offset = s->offset_l;
220 s->lfoR.offset = s->offset_r;
221 s->lfoL.srate = inlink->sample_rate;
222 s->lfoR.srate = inlink->sample_rate;
223 s->lfoL.amount = s->amount;
224 s->lfoR.amount = s->amount;
225 s->lfoL.pwidth = s->pwidth;
226 s->lfoR.pwidth = s->pwidth;
227
228 return 0;
229 }
230
231 static const AVFilterPad inputs[] = {
232 {
233 .name = "default",
234 .type = AVMEDIA_TYPE_AUDIO,
235 .config_props = config_input,
236 .filter_frame = filter_frame,
237 },
238 };
239
240 static const AVFilterPad outputs[] = {
241 {
242 .name = "default",
243 .type = AVMEDIA_TYPE_AUDIO,
244 },
245 };
246
247 const AVFilter ff_af_apulsator = {
248 .name = "apulsator",
249 .description = NULL_IF_CONFIG_SMALL("Audio pulsator."),
250 .priv_size = sizeof(AudioPulsatorContext),
251 .priv_class = &apulsator_class,
252 FILTER_INPUTS(inputs),
253 FILTER_OUTPUTS(outputs),
254 FILTER_QUERY_FUNC(query_formats),
255 };
256