... | ... |
@@ -441,6 +441,64 @@ ffmpeg -i first.flac -i second.flac -filter_complex acrossfade=d=10:o=0:c1=exp:c |
441 | 441 |
@end example |
442 | 442 |
@end itemize |
443 | 443 |
|
444 |
+@section acrusher |
|
445 |
+ |
|
446 |
+Reduce audio bit resolution. |
|
447 |
+ |
|
448 |
+This filter is bit crusher with enhanced funcionality. A bit crusher |
|
449 |
+is used to audibly reduce number of bits an audio signal is sampled |
|
450 |
+with. This doesn't change the bit depth at all, it just produces the |
|
451 |
+effect. Material reduced in bit depth sounds more harsh and "digital". |
|
452 |
+This filter is able to even round to continous values instead of discrete |
|
453 |
+bit depths. |
|
454 |
+Additionally it has a D/C offset which results in different crushing of |
|
455 |
+the lower and the upper half of the signal. |
|
456 |
+An Anti-Aliasing setting is able to produce "softer" crushing sounds. |
|
457 |
+ |
|
458 |
+Another feature of this filter is the logarithmic mode. |
|
459 |
+This setting switches from linear distances between bits to logarithmic ones. |
|
460 |
+The result is a much more "natural" sounding crusher which doesn't gate low |
|
461 |
+signals for example. The human ear has a logarithmic perception, too |
|
462 |
+so this kind of crushing is much more pleasant. |
|
463 |
+Logarithmic crushing is also able to get anti-aliased. |
|
464 |
+ |
|
465 |
+The filter accepts the following options: |
|
466 |
+ |
|
467 |
+@table @option |
|
468 |
+@item level_in |
|
469 |
+Set level in. |
|
470 |
+ |
|
471 |
+@item level_out |
|
472 |
+Set level out. |
|
473 |
+ |
|
474 |
+@item bits |
|
475 |
+Set bit reduction. |
|
476 |
+ |
|
477 |
+@item mix |
|
478 |
+Set mixing ammount. |
|
479 |
+ |
|
480 |
+@item mode |
|
481 |
+Can be linear: @code{lin} or logarithmic: @code{log}. |
|
482 |
+ |
|
483 |
+@item dc |
|
484 |
+Set DC. |
|
485 |
+ |
|
486 |
+@item aa |
|
487 |
+Set anti-aliasing. |
|
488 |
+ |
|
489 |
+@item samples |
|
490 |
+Set sample reduction. |
|
491 |
+ |
|
492 |
+@item lfo |
|
493 |
+Enable LFO. By default disabled. |
|
494 |
+ |
|
495 |
+@item lforange |
|
496 |
+Set LFO range. |
|
497 |
+ |
|
498 |
+@item lforate |
|
499 |
+Set LFO rate. |
|
500 |
+@end table |
|
501 |
+ |
|
444 | 502 |
@section adelay |
445 | 503 |
|
446 | 504 |
Delay one or more audio channels. |
... | ... |
@@ -30,6 +30,7 @@ OBJS-$(HAVE_THREADS) += pthread.o |
30 | 30 |
OBJS-$(CONFIG_ABENCH_FILTER) += f_bench.o |
31 | 31 |
OBJS-$(CONFIG_ACOMPRESSOR_FILTER) += af_sidechaincompress.o |
32 | 32 |
OBJS-$(CONFIG_ACROSSFADE_FILTER) += af_afade.o |
33 |
+OBJS-$(CONFIG_ACRUSHER_FILTER) += af_acrusher.o |
|
33 | 34 |
OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o |
34 | 35 |
OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o |
35 | 36 |
OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o |
36 | 37 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,362 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) Markus Schmidt and Christian Holschuh |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+#include "libavutil/opt.h" |
|
21 |
+#include "avfilter.h" |
|
22 |
+#include "internal.h" |
|
23 |
+#include "audio.h" |
|
24 |
+ |
|
25 |
+typedef struct LFOContext { |
|
26 |
+ double freq; |
|
27 |
+ double offset; |
|
28 |
+ int srate; |
|
29 |
+ double amount; |
|
30 |
+ double pwidth; |
|
31 |
+ double phase; |
|
32 |
+} LFOContext; |
|
33 |
+ |
|
34 |
+typedef struct SRContext { |
|
35 |
+ double target; |
|
36 |
+ double real; |
|
37 |
+ double samples; |
|
38 |
+ double last; |
|
39 |
+} SRContext; |
|
40 |
+ |
|
41 |
+typedef struct ACrusherContext { |
|
42 |
+ const AVClass *class; |
|
43 |
+ |
|
44 |
+ double level_in; |
|
45 |
+ double level_out; |
|
46 |
+ double bits; |
|
47 |
+ double mix; |
|
48 |
+ int mode; |
|
49 |
+ double dc; |
|
50 |
+ double idc; |
|
51 |
+ double aa; |
|
52 |
+ double samples; |
|
53 |
+ int is_lfo; |
|
54 |
+ double lforange; |
|
55 |
+ double lforate; |
|
56 |
+ |
|
57 |
+ double sqr; |
|
58 |
+ double aa1; |
|
59 |
+ double coeff; |
|
60 |
+ int round; |
|
61 |
+ double sov; |
|
62 |
+ double smin; |
|
63 |
+ double sdiff; |
|
64 |
+ |
|
65 |
+ LFOContext lfo; |
|
66 |
+ SRContext *sr; |
|
67 |
+} ACrusherContext; |
|
68 |
+ |
|
69 |
+#define OFFSET(x) offsetof(ACrusherContext, x) |
|
70 |
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
71 |
+ |
|
72 |
+static const AVOption acrusher_options[] = { |
|
73 |
+ { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A }, |
|
74 |
+ { "level_out","set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A }, |
|
75 |
+ { "bits", "set bit reduction", OFFSET(bits), AV_OPT_TYPE_DOUBLE, {.dbl=8}, 1, 64, A }, |
|
76 |
+ { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, A }, |
|
77 |
+ { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "mode" }, |
|
78 |
+ { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "mode" }, |
|
79 |
+ { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "mode" }, |
|
80 |
+ { "dc", "set DC", OFFSET(dc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, .25, 4, A }, |
|
81 |
+ { "aa", "set anti-aliasing", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, A }, |
|
82 |
+ { "samples", "set sample reduction", OFFSET(samples), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 250, A }, |
|
83 |
+ { "lfo", "enable LFO", OFFSET(is_lfo), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A }, |
|
84 |
+ { "lforange", "set LFO depth", OFFSET(lforange), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 250, A }, |
|
85 |
+ { "lforate", "set LFO rate", OFFSET(lforate), AV_OPT_TYPE_DOUBLE, {.dbl=.3}, .01, 200, A }, |
|
86 |
+ { NULL } |
|
87 |
+}; |
|
88 |
+ |
|
89 |
+AVFILTER_DEFINE_CLASS(acrusher); |
|
90 |
+ |
|
91 |
+static double samplereduction(ACrusherContext *s, SRContext *sr, double in) |
|
92 |
+{ |
|
93 |
+ sr->samples++; |
|
94 |
+ if (sr->samples >= s->round) { |
|
95 |
+ sr->target += s->samples; |
|
96 |
+ sr->real += s->round; |
|
97 |
+ if (sr->target + s->samples >= sr->real + 1) { |
|
98 |
+ sr->last = in; |
|
99 |
+ sr->target = 0; |
|
100 |
+ sr->real = 0; |
|
101 |
+ } |
|
102 |
+ sr->samples = 0; |
|
103 |
+ } |
|
104 |
+ return sr->last; |
|
105 |
+} |
|
106 |
+ |
|
107 |
+static double add_dc(double s, double dc, double idc) |
|
108 |
+{ |
|
109 |
+ return s > 0 ? s * dc : s * idc; |
|
110 |
+} |
|
111 |
+ |
|
112 |
+static double remove_dc(double s, double dc, double idc) |
|
113 |
+{ |
|
114 |
+ return s > 0 ? s * idc : s * dc; |
|
115 |
+} |
|
116 |
+ |
|
117 |
+static inline double factor(double y, double k, double aa1, double aa) |
|
118 |
+{ |
|
119 |
+ return 0.5 * (sin(M_PI * (fabs(y - k) - aa1) / aa - M_PI_2) + 1); |
|
120 |
+} |
|
121 |
+ |
|
122 |
+static double bitreduction(ACrusherContext *s, double in) |
|
123 |
+{ |
|
124 |
+ const double sqr = s->sqr; |
|
125 |
+ const double coeff = s->coeff; |
|
126 |
+ const double aa = s->aa; |
|
127 |
+ const double aa1 = s->aa1; |
|
128 |
+ double y, k; |
|
129 |
+ |
|
130 |
+ // add dc |
|
131 |
+ in = add_dc(in, s->dc, s->idc); |
|
132 |
+ |
|
133 |
+ // main rounding calculation depending on mode |
|
134 |
+ |
|
135 |
+ // the idea for anti-aliasing: |
|
136 |
+ // you need a function f which brings you to the scale, where |
|
137 |
+ // you want to round and the function f_b (with f(f_b)=id) which |
|
138 |
+ // brings you back to your original scale. |
|
139 |
+ // |
|
140 |
+ // then you can use the logic below in the following way: |
|
141 |
+ // y = f(in) and k = roundf(y) |
|
142 |
+ // if (y > k + aa1) |
|
143 |
+ // k = f_b(k) + ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1) |
|
144 |
+ // if (y < k + aa1) |
|
145 |
+ // k = f_b(k) - ( f_b(k+1) - f_b(k) ) * 0.5 * (sin(x - PI/2) + 1) |
|
146 |
+ // |
|
147 |
+ // whereas x = (fabs(f(in) - k) - aa1) * PI / aa |
|
148 |
+ // for both cases. |
|
149 |
+ |
|
150 |
+ switch (s->mode) { |
|
151 |
+ case 0: |
|
152 |
+ default: |
|
153 |
+ // linear |
|
154 |
+ y = in * coeff; |
|
155 |
+ k = roundf(y); |
|
156 |
+ if (k - aa1 <= y && y <= k + aa1) { |
|
157 |
+ k /= coeff; |
|
158 |
+ } else if (y > k + aa1) { |
|
159 |
+ k = k / coeff + ((k + 1) / coeff - k / coeff) * |
|
160 |
+ factor(y, k, aa1, aa); |
|
161 |
+ } else { |
|
162 |
+ k = k / coeff - (k / coeff - (k - 1) / coeff) * |
|
163 |
+ factor(y, k, aa1, aa); |
|
164 |
+ } |
|
165 |
+ break; |
|
166 |
+ case 1: |
|
167 |
+ // logarithmic |
|
168 |
+ y = sqr * log(fabs(in)) + sqr * sqr; |
|
169 |
+ k = roundf(y); |
|
170 |
+ if(!in) { |
|
171 |
+ k = 0; |
|
172 |
+ } else if (k - aa1 <= y && y <= k + aa1) { |
|
173 |
+ k = in / fabs(in) * exp(k / sqr - sqr); |
|
174 |
+ } else if (y > k + aa1) { |
|
175 |
+ double x = exp(k / sqr - sqr); |
|
176 |
+ k = FFSIGN(in) * (x + (exp((k + 1) / sqr - sqr) - x) * |
|
177 |
+ factor(y, k, aa1, aa)); |
|
178 |
+ } else { |
|
179 |
+ double x = exp(k / sqr - sqr); |
|
180 |
+ k = in / fabs(in) * (x - (x - exp((k - 1) / sqr - sqr)) * |
|
181 |
+ factor(y, k, aa1, aa)); |
|
182 |
+ } |
|
183 |
+ break; |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ // mix between dry and wet signal |
|
187 |
+ k += (in - k) * s->mix; |
|
188 |
+ |
|
189 |
+ // remove dc |
|
190 |
+ k = remove_dc(k, s->dc, s->idc); |
|
191 |
+ |
|
192 |
+ return k; |
|
193 |
+} |
|
194 |
+ |
|
195 |
+static double lfo_get(LFOContext *lfo) |
|
196 |
+{ |
|
197 |
+ double phs = FFMIN(100., lfo->phase / FFMIN(1.99, FFMAX(0.01, lfo->pwidth)) + lfo->offset); |
|
198 |
+ double val; |
|
199 |
+ |
|
200 |
+ if (phs > 1) |
|
201 |
+ phs = fmod(phs, 1.); |
|
202 |
+ |
|
203 |
+ val = sin((phs * 360.) * M_PI / 180); |
|
204 |
+ |
|
205 |
+ return val * lfo->amount; |
|
206 |
+} |
|
207 |
+ |
|
208 |
+static void lfo_advance(LFOContext *lfo, unsigned count) |
|
209 |
+{ |
|
210 |
+ lfo->phase = fabs(lfo->phase + count * lfo->freq * (1. / lfo->srate)); |
|
211 |
+ if (lfo->phase >= 1.) |
|
212 |
+ lfo->phase = fmod(lfo->phase, 1.); |
|
213 |
+} |
|
214 |
+ |
|
215 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
216 |
+{ |
|
217 |
+ AVFilterContext *ctx = inlink->dst; |
|
218 |
+ ACrusherContext *s = ctx->priv; |
|
219 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
220 |
+ AVFrame *out; |
|
221 |
+ const double *src = (const double *)in->data[0]; |
|
222 |
+ double *dst; |
|
223 |
+ const double level_in = s->level_in; |
|
224 |
+ const double level_out = s->level_out; |
|
225 |
+ const double mix = s->mix; |
|
226 |
+ int n, c; |
|
227 |
+ |
|
228 |
+ if (av_frame_is_writable(in)) { |
|
229 |
+ out = in; |
|
230 |
+ } else { |
|
231 |
+ out = ff_get_audio_buffer(inlink, in->nb_samples); |
|
232 |
+ if (!out) { |
|
233 |
+ av_frame_free(&in); |
|
234 |
+ return AVERROR(ENOMEM); |
|
235 |
+ } |
|
236 |
+ av_frame_copy_props(out, in); |
|
237 |
+ } |
|
238 |
+ |
|
239 |
+ dst = (double *)out->data[0]; |
|
240 |
+ for (n = 0; n < in->nb_samples; n++) { |
|
241 |
+ if (s->is_lfo) { |
|
242 |
+ s->samples = s->smin + s->sdiff * (lfo_get(&s->lfo) + 0.5); |
|
243 |
+ s->round = round(s->samples); |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ for (c = 0; c < inlink->channels; c++) { |
|
247 |
+ double sample = src[c] * level_in; |
|
248 |
+ |
|
249 |
+ sample = mix * samplereduction(s, &s->sr[c], sample) + src[c] * (1. - mix) * level_in; |
|
250 |
+ dst[c] = bitreduction(s, sample) * level_out; |
|
251 |
+ } |
|
252 |
+ src += c; |
|
253 |
+ dst += c; |
|
254 |
+ |
|
255 |
+ if (s->is_lfo) |
|
256 |
+ lfo_advance(&s->lfo, 1); |
|
257 |
+ } |
|
258 |
+ |
|
259 |
+ if (in != out) |
|
260 |
+ av_frame_free(&in); |
|
261 |
+ |
|
262 |
+ return ff_filter_frame(outlink, out); |
|
263 |
+} |
|
264 |
+ |
|
265 |
+static int query_formats(AVFilterContext *ctx) |
|
266 |
+{ |
|
267 |
+ AVFilterFormats *formats; |
|
268 |
+ AVFilterChannelLayouts *layouts; |
|
269 |
+ static const enum AVSampleFormat sample_fmts[] = { |
|
270 |
+ AV_SAMPLE_FMT_DBL, |
|
271 |
+ AV_SAMPLE_FMT_NONE |
|
272 |
+ }; |
|
273 |
+ int ret; |
|
274 |
+ |
|
275 |
+ layouts = ff_all_channel_counts(); |
|
276 |
+ if (!layouts) |
|
277 |
+ return AVERROR(ENOMEM); |
|
278 |
+ ret = ff_set_common_channel_layouts(ctx, layouts); |
|
279 |
+ if (ret < 0) |
|
280 |
+ return ret; |
|
281 |
+ |
|
282 |
+ formats = ff_make_format_list(sample_fmts); |
|
283 |
+ if (!formats) |
|
284 |
+ return AVERROR(ENOMEM); |
|
285 |
+ ret = ff_set_common_formats(ctx, formats); |
|
286 |
+ if (ret < 0) |
|
287 |
+ return ret; |
|
288 |
+ |
|
289 |
+ formats = ff_all_samplerates(); |
|
290 |
+ if (!formats) |
|
291 |
+ return AVERROR(ENOMEM); |
|
292 |
+ return ff_set_common_samplerates(ctx, formats); |
|
293 |
+} |
|
294 |
+ |
|
295 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
296 |
+{ |
|
297 |
+ ACrusherContext *s = ctx->priv; |
|
298 |
+ |
|
299 |
+ av_freep(&s->sr); |
|
300 |
+} |
|
301 |
+ |
|
302 |
+static int config_input(AVFilterLink *inlink) |
|
303 |
+{ |
|
304 |
+ AVFilterContext *ctx = inlink->dst; |
|
305 |
+ ACrusherContext *s = ctx->priv; |
|
306 |
+ double rad, sun, smax, sov; |
|
307 |
+ |
|
308 |
+ s->idc = 1. / s->dc; |
|
309 |
+ s->coeff = exp2(s->bits) - 1; |
|
310 |
+ s->sqr = sqrt(s->coeff / 2); |
|
311 |
+ s->aa1 = (1. - s->aa) / 2.; |
|
312 |
+ s->round = round(s->samples); |
|
313 |
+ rad = s->lforange / 2.; |
|
314 |
+ s->smin = FFMAX(s->samples - rad, 1.); |
|
315 |
+ sun = s->samples - rad - s->smin; |
|
316 |
+ smax = FFMIN(s->samples + rad, 250.); |
|
317 |
+ sov = s->samples + rad - smax; |
|
318 |
+ smax -= sun; |
|
319 |
+ s->smin -= sov; |
|
320 |
+ s->sdiff = smax - s->smin; |
|
321 |
+ |
|
322 |
+ s->lfo.freq = s->lforate; |
|
323 |
+ s->lfo.pwidth = 1.; |
|
324 |
+ s->lfo.srate = inlink->sample_rate; |
|
325 |
+ s->lfo.amount = .5; |
|
326 |
+ |
|
327 |
+ s->sr = av_calloc(inlink->channels, sizeof(*s->sr)); |
|
328 |
+ if (!s->sr) |
|
329 |
+ return AVERROR(ENOMEM); |
|
330 |
+ |
|
331 |
+ return 0; |
|
332 |
+} |
|
333 |
+ |
|
334 |
+static const AVFilterPad avfilter_af_acrusher_inputs[] = { |
|
335 |
+ { |
|
336 |
+ .name = "default", |
|
337 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
338 |
+ .config_props = config_input, |
|
339 |
+ .filter_frame = filter_frame, |
|
340 |
+ }, |
|
341 |
+ { NULL } |
|
342 |
+}; |
|
343 |
+ |
|
344 |
+static const AVFilterPad avfilter_af_acrusher_outputs[] = { |
|
345 |
+ { |
|
346 |
+ .name = "default", |
|
347 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
348 |
+ }, |
|
349 |
+ { NULL } |
|
350 |
+}; |
|
351 |
+ |
|
352 |
+AVFilter ff_af_acrusher = { |
|
353 |
+ .name = "acrusher", |
|
354 |
+ .description = NULL_IF_CONFIG_SMALL("Reduce audio bit resolution."), |
|
355 |
+ .priv_size = sizeof(ACrusherContext), |
|
356 |
+ .priv_class = &acrusher_class, |
|
357 |
+ .uninit = uninit, |
|
358 |
+ .query_formats = query_formats, |
|
359 |
+ .inputs = avfilter_af_acrusher_inputs, |
|
360 |
+ .outputs = avfilter_af_acrusher_outputs, |
|
361 |
+}; |
... | ... |
@@ -48,6 +48,7 @@ void avfilter_register_all(void) |
48 | 48 |
REGISTER_FILTER(ABENCH, abench, af); |
49 | 49 |
REGISTER_FILTER(ACOMPRESSOR, acompressor, af); |
50 | 50 |
REGISTER_FILTER(ACROSSFADE, acrossfade, af); |
51 |
+ REGISTER_FILTER(ACRUSHER, acrusher, af); |
|
51 | 52 |
REGISTER_FILTER(ADELAY, adelay, af); |
52 | 53 |
REGISTER_FILTER(AECHO, aecho, af); |
53 | 54 |
REGISTER_FILTER(AEMPHASIS, aemphasis, af); |
... | ... |
@@ -30,7 +30,7 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 6 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 50 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 51 |
|
34 | 34 |
#define LIBAVFILTER_VERSION_MICRO 100 |
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |