2 * Copyright (c) Markus Schmidt and Christian Holschuh
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/opt.h"
26 typedef struct LFOContext {
35 typedef struct SRContext {
42 typedef struct ACrusherContext {
70 #define OFFSET(x) offsetof(ACrusherContext, x)
71 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
90 AVFILTER_DEFINE_CLASS(acrusher);
92 static double samplereduction(ACrusherContext *s, SRContext *sr, double in)
95 if (sr->samples >= s->round) {
96 sr->target += s->samples;
98 if (sr->target + s->samples >= sr->real + 1) {
108 static double add_dc(double s, double dc, double idc)
110 return s > 0 ? s * dc : s * idc;
113 static double remove_dc(double s, double dc, double idc)
115 return s > 0 ? s * idc : s * dc;
118 static inline double factor(double y, double k, double aa1, double aa)
120 return 0.5 * (sin(M_PI * (fabs(y - k) - aa1) / aa - M_PI_2) + 1);
123 static double bitreduction(ACrusherContext *s, double in)
125 const double sqr = s->sqr;
126 const double coeff = s->coeff;
127 const double aa = s->aa;
128 const double aa1 = s->aa1;
132 in = add_dc(in, s->dc, s->idc);
134 // main rounding calculation depending on mode
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.
141 // then you can use the logic below in the following way:
142 // y = f(in) and k = roundf(y)
144 // k = f_b(k) + ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1)
146 // k = f_b(k) - ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1)
148 // whereas x = (fabs(f(in) - k) - aa1) * PI / aa
157 if (k - aa1 <= y && y <= k + aa1) {
159 } else if (y > k + aa1) {
160 k = k / coeff + ((k + 1) / coeff - k / coeff) *
161 factor(y, k, aa1, aa);
163 k = k / coeff - (k / coeff - (k - 1) / coeff) *
164 factor(y, k, aa1, aa);
169 y = sqr * log(fabs(in)) + sqr * sqr;
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));
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));
187 // mix between dry and wet signal
188 k += (in - k) * s->mix;
191 k = remove_dc(k, s->dc, s->idc);
196 static double lfo_get(LFOContext *lfo)
198 double phs = FFMIN(100., lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset);
204 val = sin((phs * 360.) * M_PI / 180);
206 return val * lfo->amount;
209 static void lfo_advance(LFOContext *lfo, unsigned count)
211 lfo->phase = fabs(lfo->phase + count * lfo->freq * (1. / lfo->srate));
212 if (lfo->phase >= 1.)
213 lfo->phase = fmod(lfo->phase, 1.);
216 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
218 AVFilterContext *ctx = inlink->dst;
219 ACrusherContext *s = ctx->priv;
220 AVFilterLink *outlink = ctx->outputs[0];
222 const double *src = (const double *)in->data[0];
224 const double level_in = s->level_in;
225 const double level_out = s->level_out;
226 const double mix = s->mix;
229 if (av_frame_is_writable(in)) {
232 out = ff_get_audio_buffer(inlink, in->nb_samples);
235 return AVERROR(ENOMEM);
237 av_frame_copy_props(out, in);
240 dst = (double *)out->data[0];
241 for (n = 0; n < in->nb_samples; n++) {
243 s->samples = s->smin + s->sdiff * (lfo_get(&s->lfo) + 0.5);
244 s->round = round(s->samples);
247 for (c = 0; c < inlink->channels; c++) {
248 double sample = src[c] * level_in;
250 sample = mix * samplereduction(s, &s->sr[c], sample) + src[c] * (1. - mix) * level_in;
251 dst[c] = bitreduction(s, sample) * level_out;
257 lfo_advance(&s->lfo, 1);
263 return ff_filter_frame(outlink, out);
266 static int query_formats(AVFilterContext *ctx)
268 AVFilterFormats *formats;
269 AVFilterChannelLayouts *layouts;
270 static const enum AVSampleFormat sample_fmts[] = {
276 layouts = ff_all_channel_counts();
278 return AVERROR(ENOMEM);
279 ret = ff_set_common_channel_layouts(ctx, layouts);
283 formats = ff_make_format_list(sample_fmts);
285 return AVERROR(ENOMEM);
286 ret = ff_set_common_formats(ctx, formats);
290 formats = ff_all_samplerates();
292 return AVERROR(ENOMEM);
293 return ff_set_common_samplerates(ctx, formats);
296 static av_cold void uninit(AVFilterContext *ctx)
298 ACrusherContext *s = ctx->priv;
303 static int config_input(AVFilterLink *inlink)
305 AVFilterContext *ctx = inlink->dst;
306 ACrusherContext *s = ctx->priv;
307 double rad, sunder, smax, sover;
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;
321 s->sdiff = smax - s->smin;
323 s->lfo.freq = s->lforate;
325 s->lfo.srate = inlink->sample_rate;
328 s->sr = av_calloc(inlink->channels, sizeof(*s->sr));
330 return AVERROR(ENOMEM);
335 static const AVFilterPad avfilter_af_acrusher_inputs[] = {
338 .type = AVMEDIA_TYPE_AUDIO,
339 .config_props = config_input,
340 .filter_frame = filter_frame,
345 static const AVFilterPad avfilter_af_acrusher_outputs[] = {
348 .type = AVMEDIA_TYPE_AUDIO,
353 AVFilter ff_af_acrusher = {
355 .description = NULL_IF_CONFIG_SMALL("Reduce audio bit resolution."),
356 .priv_size = sizeof(ACrusherContext),
357 .priv_class = &acrusher_class,
359 .query_formats = query_formats,
360 .inputs = avfilter_af_acrusher_inputs,
361 .outputs = avfilter_af_acrusher_outputs,