FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_acrusher.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 0 128 0.0%
Functions: 0 11 0.0%
Branches: 0 70 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) Markus Schmidt and Christian Holschuh
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/opt.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 #include "audio.h"
25
26 typedef struct LFOContext {
27 double freq;
28 double offset;
29 int srate;
30 double amount;
31 double pwidth;
32 double phase;
33 } LFOContext;
34
35 typedef struct SRContext {
36 double target;
37 double real;
38 double samples;
39 double last;
40 } SRContext;
41
42 typedef struct ACrusherContext {
43 const AVClass *class;
44
45 double level_in;
46 double level_out;
47 double bits;
48 double mix;
49 int mode;
50 double dc;
51 double idc;
52 double aa;
53 double samples;
54 int is_lfo;
55 double lforange;
56 double lforate;
57
58 double sqr;
59 double aa1;
60 double coeff;
61 int round;
62 double sov;
63 double smin;
64 double sdiff;
65
66 LFOContext lfo;
67 SRContext *sr;
68 } ACrusherContext;
69
70 #define OFFSET(x) offsetof(ACrusherContext, x)
71 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
72
73 static const AVOption acrusher_options[] = {
74 { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
75 { "level_out","set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
76 { "bits", "set bit reduction", OFFSET(bits), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 1, 64, A },
77 { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, A },
78 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "mode" },
79 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "mode" },
80 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "mode" },
81 { "dc", "set DC", OFFSET(dc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, .25, 4, A },
82 { "aa", "set anti-aliasing", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, A },
83 { "samples", "set sample reduction", OFFSET(samples), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 250, A },
84 { "lfo", "enable LFO", OFFSET(is_lfo), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
85 { "lforange", "set LFO depth", OFFSET(lforange), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 250, A },
86 { "lforate", "set LFO rate", OFFSET(lforate), AV_OPT_TYPE_DOUBLE, {.dbl=.3}, .01, 200, A },
87 { NULL }
88 };
89
90 AVFILTER_DEFINE_CLASS(acrusher);
91
92 static double samplereduction(ACrusherContext *s, SRContext *sr, double in)
93 {
94 sr->samples++;
95 if (sr->samples >= s->round) {
96 sr->target += s->samples;
97 sr->real += s->round;
98 if (sr->target + s->samples >= sr->real + 1) {
99 sr->last = in;
100 sr->target = 0;
101 sr->real = 0;
102 }
103 sr->samples = 0;
104 }
105 return sr->last;
106 }
107
108 static double add_dc(double s, double dc, double idc)
109 {
110 return s > 0 ? s * dc : s * idc;
111 }
112
113 static double remove_dc(double s, double dc, double idc)
114 {
115 return s > 0 ? s * idc : s * dc;
116 }
117
118 static inline double factor(double y, double k, double aa1, double aa)
119 {
120 return 0.5 * (sin(M_PI * (fabs(y - k) - aa1) / aa - M_PI_2) + 1);
121 }
122
123 static double bitreduction(ACrusherContext *s, double in)
124 {
125 const double sqr = s->sqr;
126 const double coeff = s->coeff;
127 const double aa = s->aa;
128 const double aa1 = s->aa1;
129 double y, k;
130
131 // add dc
132 in = add_dc(in, s->dc, s->idc);
133
134 // main rounding calculation depending on mode
135
136 // the idea for anti-aliasing:
137 // you need a function f which brings you to the scale, where
138 // you want to round and the function f_b (with f(f_b)=id) which
139 // brings you back to your original scale.
140 //
141 // then you can use the logic below in the following way:
142 // y = f(in) and k = roundf(y)
143 // if (y > k + aa1)
144 // k = f_b(k) + ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1)
145 // if (y < k + aa1)
146 // k = f_b(k) - ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1)
147 //
148 // whereas x = (fabs(f(in) - k) - aa1) * PI / aa
149 // for both cases.
150
151 switch (s->mode) {
152 case 0:
153 default:
154 // linear
155 y = in * coeff;
156 k = roundf(y);
157 if (k - aa1 <= y && y <= k + aa1) {
158 k /= coeff;
159 } else if (y > k + aa1) {
160 k = k / coeff + ((k + 1) / coeff - k / coeff) *
161 factor(y, k, aa1, aa);
162 } else {
163 k = k / coeff - (k / coeff - (k - 1) / coeff) *
164 factor(y, k, aa1, aa);
165 }
166 break;
167 case 1:
168 // logarithmic
169 y = sqr * log(fabs(in)) + sqr * sqr;
170 k = roundf(y);
171 if(!in) {
172 k = 0;
173 } else if (k - aa1 <= y && y <= k + aa1) {
174 k = in / fabs(in) * exp(k / sqr - sqr);
175 } else if (y > k + aa1) {
176 double x = exp(k / sqr - sqr);
177 k = FFSIGN(in) * (x + (exp((k + 1) / sqr - sqr) - x) *
178 factor(y, k, aa1, aa));
179 } else {
180 double x = exp(k / sqr - sqr);
181 k = in / fabs(in) * (x - (x - exp((k - 1) / sqr - sqr)) *
182 factor(y, k, aa1, aa));
183 }
184 break;
185 }
186
187 // mix between dry and wet signal
188 k += (in - k) * s->mix;
189
190 // remove dc
191 k = remove_dc(k, s->dc, s->idc);
192
193 return k;
194 }
195
196 static double lfo_get(LFOContext *lfo)
197 {
198 double phs = FFMIN(100., lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
199 double val;
200
201 if (phs > 1)
202 phs = fmod(phs, 1.);
203
204 val = sin((phs * 360.) * M_PI / 180);
205
206 return val * lfo->amount;
207 }
208
209 static void lfo_advance(LFOContext *lfo, unsigned count)
210 {
211 lfo->phase = fabs(lfo->phase + count * lfo->freq * (1. / lfo->srate));
212 if (lfo->phase >= 1.)
213 lfo->phase = fmod(lfo->phase, 1.);
214 }
215
216 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
217 {
218 AVFilterContext *ctx = inlink->dst;
219 ACrusherContext *s = ctx->priv;
220 AVFilterLink *outlink = ctx->outputs[0];
221 AVFrame *out;
222 const double *src = (const double *)in->data[0];
223 double *dst;
224 const double level_in = s->level_in;
225 const double level_out = s->level_out;
226 const double mix = s->mix;
227 int n, c;
228
229 if (av_frame_is_writable(in)) {
230 out = in;
231 } else {
232 out = ff_get_audio_buffer(inlink, in->nb_samples);
233 if (!out) {
234 av_frame_free(&in);
235 return AVERROR(ENOMEM);
236 }
237 av_frame_copy_props(out, in);
238 }
239
240 dst = (double *)out->data[0];
241 for (n = 0; n < in->nb_samples; n++) {
242 if (s->is_lfo) {
243 s->samples = s->smin + s->sdiff * (lfo_get(&s->lfo) + 0.5);
244 s->round = round(s->samples);
245 }
246
247 for (c = 0; c < inlink->ch_layout.nb_channels; c++) {
248 double sample = src[c] * level_in;
249
250 sample = mix * samplereduction(s, &s->sr[c], sample) + src[c] * (1. - mix) * level_in;
251 dst[c] = ctx->is_disabled ? src[c] : bitreduction(s, sample) * level_out;
252 }
253 src += c;
254 dst += c;
255
256 if (s->is_lfo)
257 lfo_advance(&s->lfo, 1);
258 }
259
260 if (in != out)
261 av_frame_free(&in);
262
263 return ff_filter_frame(outlink, out);
264 }
265
266 static av_cold void uninit(AVFilterContext *ctx)
267 {
268 ACrusherContext *s = ctx->priv;
269
270 av_freep(&s->sr);
271 }
272
273 static int config_input(AVFilterLink *inlink)
274 {
275 AVFilterContext *ctx = inlink->dst;
276 ACrusherContext *s = ctx->priv;
277 double rad, sunder, smax, sover;
278
279 s->idc = 1. / s->dc;
280 s->coeff = exp2(s->bits) - 1;
281 s->sqr = sqrt(s->coeff / 2);
282 s->aa1 = (1. - s->aa) / 2.;
283 s->round = round(s->samples);
284 rad = s->lforange / 2.;
285 s->smin = FFMAX(s->samples - rad, 1.);
286 sunder = s->samples - rad - s->smin;
287 smax = FFMIN(s->samples + rad, 250.);
288 sover = s->samples + rad - smax;
289 smax -= sunder;
290 s->smin -= sover;
291 s->sdiff = smax - s->smin;
292
293 s->lfo.freq = s->lforate;
294 s->lfo.pwidth = 1.;
295 s->lfo.srate = inlink->sample_rate;
296 s->lfo.amount = .5;
297
298 if (!s->sr)
299 s->sr = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->sr));
300 if (!s->sr)
301 return AVERROR(ENOMEM);
302
303 return 0;
304 }
305
306 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
307 char *res, int res_len, int flags)
308 {
309 AVFilterLink *inlink = ctx->inputs[0];
310 int ret;
311
312 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
313 if (ret < 0)
314 return ret;
315
316 return config_input(inlink);
317 }
318
319 static const AVFilterPad avfilter_af_acrusher_inputs[] = {
320 {
321 .name = "default",
322 .type = AVMEDIA_TYPE_AUDIO,
323 .config_props = config_input,
324 .filter_frame = filter_frame,
325 },
326 };
327
328 static const AVFilterPad avfilter_af_acrusher_outputs[] = {
329 {
330 .name = "default",
331 .type = AVMEDIA_TYPE_AUDIO,
332 },
333 };
334
335 const AVFilter ff_af_acrusher = {
336 .name = "acrusher",
337 .description = NULL_IF_CONFIG_SMALL("Reduce audio bit resolution."),
338 .priv_size = sizeof(ACrusherContext),
339 .priv_class = &acrusher_class,
340 .uninit = uninit,
341 FILTER_INPUTS(avfilter_af_acrusher_inputs),
342 FILTER_OUTPUTS(avfilter_af_acrusher_outputs),
343 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
344 .process_command = process_command,
345 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
346 };
347