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
samplereduction(ACrusherContext * s,SRContext * sr,double in)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
add_dc(double s,double dc,double idc)108 static double add_dc(double s, double dc, double idc)
109 {
110 return s > 0 ? s * dc : s * idc;
111 }
112
remove_dc(double s,double dc,double idc)113 static double remove_dc(double s, double dc, double idc)
114 {
115 return s > 0 ? s * idc : s * dc;
116 }
117
factor(double y,double k,double aa1,double aa)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
bitreduction(ACrusherContext * s,double in)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
lfo_get(LFOContext * lfo)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
lfo_advance(LFOContext * lfo,unsigned count)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
filter_frame(AVFilterLink * inlink,AVFrame * in)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->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] = 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
query_formats(AVFilterContext * ctx)266 static int query_formats(AVFilterContext *ctx)
267 {
268 AVFilterFormats *formats;
269 AVFilterChannelLayouts *layouts;
270 static const enum AVSampleFormat sample_fmts[] = {
271 AV_SAMPLE_FMT_DBL,
272 AV_SAMPLE_FMT_NONE
273 };
274 int ret;
275
276 layouts = ff_all_channel_counts();
277 if (!layouts)
278 return AVERROR(ENOMEM);
279 ret = ff_set_common_channel_layouts(ctx, layouts);
280 if (ret < 0)
281 return ret;
282
283 formats = ff_make_format_list(sample_fmts);
284 if (!formats)
285 return AVERROR(ENOMEM);
286 ret = ff_set_common_formats(ctx, formats);
287 if (ret < 0)
288 return ret;
289
290 formats = ff_all_samplerates();
291 if (!formats)
292 return AVERROR(ENOMEM);
293 return ff_set_common_samplerates(ctx, formats);
294 }
295
uninit(AVFilterContext * ctx)296 static av_cold void uninit(AVFilterContext *ctx)
297 {
298 ACrusherContext *s = ctx->priv;
299
300 av_freep(&s->sr);
301 }
302
config_input(AVFilterLink * inlink)303 static int config_input(AVFilterLink *inlink)
304 {
305 AVFilterContext *ctx = inlink->dst;
306 ACrusherContext *s = ctx->priv;
307 double rad, sunder, smax, sover;
308
309 s->idc = 1. / s->dc;
310 s->coeff = exp2(s->bits) - 1;
311 s->sqr = sqrt(s->coeff / 2);
312 s->aa1 = (1. - s->aa) / 2.;
313 s->round = round(s->samples);
314 rad = s->lforange / 2.;
315 s->smin = FFMAX(s->samples - rad, 1.);
316 sunder = s->samples - rad - s->smin;
317 smax = FFMIN(s->samples + rad, 250.);
318 sover = s->samples + rad - smax;
319 smax -= sunder;
320 s->smin -= sover;
321 s->sdiff = smax - s->smin;
322
323 s->lfo.freq = s->lforate;
324 s->lfo.pwidth = 1.;
325 s->lfo.srate = inlink->sample_rate;
326 s->lfo.amount = .5;
327
328 if (!s->sr)
329 s->sr = av_calloc(inlink->channels, sizeof(*s->sr));
330 if (!s->sr)
331 return AVERROR(ENOMEM);
332
333 return 0;
334 }
335
process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)336 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
337 char *res, int res_len, int flags)
338 {
339 AVFilterLink *inlink = ctx->inputs[0];
340 int ret;
341
342 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
343 if (ret < 0)
344 return ret;
345
346 return config_input(inlink);
347 }
348
349 static const AVFilterPad avfilter_af_acrusher_inputs[] = {
350 {
351 .name = "default",
352 .type = AVMEDIA_TYPE_AUDIO,
353 .config_props = config_input,
354 .filter_frame = filter_frame,
355 },
356 { NULL }
357 };
358
359 static const AVFilterPad avfilter_af_acrusher_outputs[] = {
360 {
361 .name = "default",
362 .type = AVMEDIA_TYPE_AUDIO,
363 },
364 { NULL }
365 };
366
367 AVFilter ff_af_acrusher = {
368 .name = "acrusher",
369 .description = NULL_IF_CONFIG_SMALL("Reduce audio bit resolution."),
370 .priv_size = sizeof(ACrusherContext),
371 .priv_class = &acrusher_class,
372 .uninit = uninit,
373 .query_formats = query_formats,
374 .inputs = avfilter_af_acrusher_inputs,
375 .outputs = avfilter_af_acrusher_outputs,
376 .process_command = process_command,
377 };
378