History

avfilter/af_asoftclip: rewrite oversampling

Fixes most aliasing issues.

Paul B Mahol committed 5 years ago

Commit 94d4cc21 parent 3e127b52 files · +171 −135Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/af_asoftclip.c+171−134
@@ -21,11 +21,12 @@ · 20 unmodified lines
2121 #include "libavutil/avassert.h"
2222 #include "libavutil/channel_layout.h"
2323 #include "libavutil/opt.h"
24−#include "libswresample/swresample.h"
2524 #include "avfilter.h"
2625 #include "audio.h"
2726 #include "formats.h"
2827
28+#define MAX_OVERSAMPLE 64
29+
2930 enum ASoftClipTypes {
3031 ASC_HARD = -1,
3132 ASC_TANH,
@@ -39,6 +40,14 @@ enum ASoftClipTypes { · 7 unmodified lines
3940 NB_TYPES,
4041 };
4142
43+typedef struct Lowpass {
44+ float fb0, fb1, fb2;
45+ float fa0, fa1, fa2;
46+
47+ double db0, db1, db2;
48+ double da0, da1, da2;
49+} Lowpass;
50+
4251 typedef struct ASoftClipContext {
4352 const AVClass *class;
4453
@@ -49,10 +58,8 @@ typedef struct ASoftClipContext { · 4 unmodified lines
4958 double output;
5059 double param;
5160
52− SwrContext *up_ctx;
53− SwrContext *down_ctx;
54−
55− AVFrame *frame;
61+ Lowpass lowpass[MAX_OVERSAMPLE];
62+ AVFrame *frame[2];
5663
5764 void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
5865 int nb_samples, int channels, int start, int end);
@@ -60,7 +67,6 @@ typedef struct ASoftClipContext { · 1 unmodified line
6067
6168 #define OFFSET(x) offsetof(ASoftClipContext, x)
6269 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
63−#define F AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
6470
6571 static const AVOption asoftclip_options[] = {
6672 { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
@@ -76,7 +82,7 @@ static const AVOption asoftclip_options[] = { · 9 unmodified lines
7682 { "threshold", "set softclip threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.000001, 1, A },
7783 { "output", "set softclip output gain", OFFSET(output), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.000001, 16, A },
7884 { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
79− { "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, 32, F },
85+ { "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, MAX_OVERSAMPLE, A },
8086 { NULL }
8187 };
8288
@@ -85,8 +91,7 @@ AVFILTER_DEFINE_CLASS(asoftclip); · 2 unmodified lines
8591 static int query_formats(AVFilterContext *ctx)
8692 {
8793 static const enum AVSampleFormat sample_fmts[] = {
88− AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
89− AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
94+ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
9095 AV_SAMPLE_FMT_NONE
9196 };
9297 int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
@@ -100,42 +105,103 @@ static int query_formats(AVFilterContext *ctx) · 7 unmodified lines
100105 return ff_set_common_all_samplerates(ctx);
101106 }
102107
108+static void get_lowpass(Lowpass *s,
109+ double frequency,
110+ double sample_rate)
111+{
112+ double w0 = 2 * M_PI * frequency / sample_rate;
113+ double alpha = sin(w0) / (2 * 0.8);
114+ double factor;
115+
116+ s->da0 = 1 + alpha;
117+ s->da1 = -2 * cos(w0);
118+ s->da2 = 1 - alpha;
119+ s->db0 = (1 - cos(w0)) / 2;
120+ s->db1 = 1 - cos(w0);
121+ s->db2 = (1 - cos(w0)) / 2;
122+
123+ s->da1 /= s->da0;
124+ s->da2 /= s->da0;
125+ s->db0 /= s->da0;
126+ s->db1 /= s->da0;
127+ s->db2 /= s->da0;
128+ s->da0 /= s->da0;
129+
130+ factor = (s->da0 + s->da1 + s->da2) / (s->db0 + s->db1 + s->db2);
131+ s->db0 *= factor;
132+ s->db1 *= factor;
133+ s->db2 *= factor;
134+
135+ s->fa0 = s->da0;
136+ s->fa1 = s->da1;
137+ s->fa2 = s->da2;
138+ s->fb0 = s->db0;
139+ s->fb1 = s->db1;
140+ s->fb2 = s->db2;
141+}
142+
143+static inline float run_lowpassf(const Lowpass *const s,
144+ float src, float *w)
145+{
146+ float dst;
147+
148+ dst = src * s->fb0 + w[0];
149+ w[0] = s->fb1 * src + w[1] - s->fa1 * dst;
150+ w[1] = s->fb2 * src - s->fa2 * dst;
151+
152+ return dst;
153+}
154+
103155 static void filter_flt(ASoftClipContext *s,
104156 void **dptr, const void **sptr,
105157 int nb_samples, int channels,
106158 int start, int end)
107159 {
160+ const int oversample = s->oversample;
161+ const int nb_osamples = nb_samples * oversample;
162+ const float scale = oversample > 1 ? oversample * 0.5f : 1.f;
108163 float threshold = s->threshold;
109164 float gain = s->output * threshold;
110165 float factor = 1.f / threshold;
111166 float param = s->param;
112167
113168 for (int c = start; c < end; c++) {
169+ float *w = (float *)(s->frame[0]->extended_data[c]) + 2 * (oversample - 1);
114170 const float *src = sptr[c];
115171 float *dst = dptr[c];
116172
173+ for (int n = 0; n < nb_samples; n++) {
174+ dst[oversample * n] = src[n];
175+
176+ for (int m = 1; m < oversample; m++)
177+ dst[oversample * n + m] = 0.f;
178+ }
179+
180+ for (int n = 0; n < nb_osamples && oversample > 1; n++)
181+ dst[n] = run_lowpassf(&s->lowpass[oversample - 1], dst[n], w);
182+
117183 switch (s->type) {
118184 case ASC_HARD:
119− for (int n = 0; n < nb_samples; n++) {
120− dst[n] = av_clipf(src[n] * factor, -1.f, 1.f);
185+ for (int n = 0; n < nb_osamples; n++) {
186+ dst[n] = av_clipf(dst[n] * factor, -1.f, 1.f);
121187 dst[n] *= gain;
122188 }
123189 break;
124190 case ASC_TANH:
125− for (int n = 0; n < nb_samples; n++) {
126− dst[n] = tanhf(src[n] * factor * param);
191+ for (int n = 0; n < nb_osamples; n++) {
192+ dst[n] = tanhf(dst[n] * factor * param);
127193 dst[n] *= gain;
128194 }
129195 break;
130196 case ASC_ATAN:
131− for (int n = 0; n < nb_samples; n++) {
132− dst[n] = 2.f / M_PI * atanf(src[n] * factor * param);
197+ for (int n = 0; n < nb_osamples; n++) {
198+ dst[n] = 2.f / M_PI * atanf(dst[n] * factor * param);
133199 dst[n] *= gain;
134200 }
135201 break;
136202 case ASC_CUBIC:
137− for (int n = 0; n < nb_samples; n++) {
138− float sample = src[n] * factor;
203+ for (int n = 0; n < nb_osamples; n++) {
204+ float sample = dst[n] * factor;
139205
140206 if (FFABS(sample) >= 1.5f)
141207 dst[n] = FFSIGN(sample);
@@ -145,22 +211,22 @@ static void filter_flt(ASoftClipContext *s, · 3 unmodified lines
145211 }
146212 break;
147213 case ASC_EXP:
148− for (int n = 0; n < nb_samples; n++) {
149− dst[n] = 2.f / (1.f + expf(-2.f * src[n] * factor)) - 1.;
214+ for (int n = 0; n < nb_osamples; n++) {
215+ dst[n] = 2.f / (1.f + expf(-2.f * dst[n] * factor)) - 1.;
150216 dst[n] *= gain;
151217 }
152218 break;
153219 case ASC_ALG:
154− for (int n = 0; n < nb_samples; n++) {
155− float sample = src[n] * factor;
220+ for (int n = 0; n < nb_osamples; n++) {
221+ float sample = dst[n] * factor;
156222
157223 dst[n] = sample / (sqrtf(param + sample * sample));
158224 dst[n] *= gain;
159225 }
160226 break;
161227 case ASC_QUINTIC:
162− for (int n = 0; n < nb_samples; n++) {
163− float sample = src[n] * factor;
228+ for (int n = 0; n < nb_osamples; n++) {
229+ float sample = dst[n] * factor;
164230
165231 if (FFABS(sample) >= 1.25)
166232 dst[n] = FFSIGN(sample);
@@ -170,8 +236,8 @@ static void filter_flt(ASoftClipContext *s, · 3 unmodified lines
170236 }
171237 break;
172238 case ASC_SIN:
173− for (int n = 0; n < nb_samples; n++) {
174− float sample = src[n] * factor;
239+ for (int n = 0; n < nb_osamples; n++) {
240+ float sample = dst[n] * factor;
175241
176242 if (FFABS(sample) >= M_PI_2)
177243 dst[n] = FFSIGN(sample);
@@ -181,53 +247,86 @@ static void filter_flt(ASoftClipContext *s, · 3 unmodified lines
181247 }
182248 break;
183249 case ASC_ERF:
184− for (int n = 0; n < nb_samples; n++) {
185− dst[n] = erff(src[n] * factor);
250+ for (int n = 0; n < nb_osamples; n++) {
251+ dst[n] = erff(dst[n] * factor);
186252 dst[n] *= gain;
187253 }
188254 break;
189255 default:
190256 av_assert0(0);
191257 }
258+
259+ w = (float *)(s->frame[1]->extended_data[c]) + 2 * (oversample - 1);
260+ for (int n = 0; n < nb_osamples && oversample > 1; n++)
261+ dst[n] = run_lowpassf(&s->lowpass[oversample - 1], dst[n], w);
262+
263+ for (int n = 0; n < nb_samples; n++)
264+ dst[n] = dst[n * oversample] * scale;
192265 }
193266 }
194267
268+static inline double run_lowpassd(const Lowpass *const s,
269+ double src, double *w)
270+{
271+ double dst;
272+
273+ dst = src * s->db0 + w[0];
274+ w[0] = s->db1 * src + w[1] - s->da1 * dst;
275+ w[1] = s->db2 * src - s->da2 * dst;
276+
277+ return dst;
278+}
279+
195280 static void filter_dbl(ASoftClipContext *s,
196281 void **dptr, const void **sptr,
197282 int nb_samples, int channels,
198283 int start, int end)
199284 {
285+ const int oversample = s->oversample;
286+ const int nb_osamples = nb_samples * oversample;
287+ const double scale = oversample > 1 ? oversample * 0.5 : 1.;
200288 double threshold = s->threshold;
201289 double gain = s->output * threshold;
202290 double factor = 1. / threshold;
203291 double param = s->param;
204292
205293 for (int c = start; c < end; c++) {
294+ double *w = (double *)(s->frame[0]->extended_data[c]) + 2 * (oversample - 1);
206295 const double *src = sptr[c];
207296 double *dst = dptr[c];
208297
298+ for (int n = 0; n < nb_samples; n++) {
299+ dst[oversample * n] = src[n];
300+
301+ for (int m = 1; m < oversample; m++)
302+ dst[oversample * n + m] = 0.f;
303+ }
304+
305+ for (int n = 0; n < nb_osamples && oversample > 1; n++)
306+ dst[n] = run_lowpassd(&s->lowpass[oversample - 1], dst[n], w);
307+
209308 switch (s->type) {
210309 case ASC_HARD:
211− for (int n = 0; n < nb_samples; n++) {
212− dst[n] = av_clipd(src[n] * factor, -1., 1.);
310+ for (int n = 0; n < nb_osamples; n++) {
311+ dst[n] = av_clipd(dst[n] * factor, -1., 1.);
213312 dst[n] *= gain;
214313 }
215314 break;
216315 case ASC_TANH:
217− for (int n = 0; n < nb_samples; n++) {
218− dst[n] = tanh(src[n] * factor * param);
316+ for (int n = 0; n < nb_osamples; n++) {
317+ dst[n] = tanh(dst[n] * factor * param);
219318 dst[n] *= gain;
220319 }
221320 break;
222321 case ASC_ATAN:
223− for (int n = 0; n < nb_samples; n++) {
224− dst[n] = 2. / M_PI * atan(src[n] * factor * param);
322+ for (int n = 0; n < nb_osamples; n++) {
323+ dst[n] = 2. / M_PI * atan(dst[n] * factor * param);
225324 dst[n] *= gain;
226325 }
227326 break;
228327 case ASC_CUBIC:
229− for (int n = 0; n < nb_samples; n++) {
230− double sample = src[n] * factor;
328+ for (int n = 0; n < nb_osamples; n++) {
329+ double sample = dst[n] * factor;
231330
232331 if (FFABS(sample) >= 1.5)
233332 dst[n] = FFSIGN(sample);
@@ -237,22 +336,22 @@ static void filter_dbl(ASoftClipContext *s, · 3 unmodified lines
237336 }
238337 break;
239338 case ASC_EXP:
240− for (int n = 0; n < nb_samples; n++) {
241− dst[n] = 2. / (1. + exp(-2. * src[n] * factor)) - 1.;
339+ for (int n = 0; n < nb_osamples; n++) {
340+ dst[n] = 2. / (1. + exp(-2. * dst[n] * factor)) - 1.;
242341 dst[n] *= gain;
243342 }
244343 break;
245344 case ASC_ALG:
246− for (int n = 0; n < nb_samples; n++) {
247− double sample = src[n] * factor;
345+ for (int n = 0; n < nb_osamples; n++) {
346+ double sample = dst[n] * factor;
248347
249348 dst[n] = sample / (sqrt(param + sample * sample));
250349 dst[n] *= gain;
251350 }
252351 break;
253352 case ASC_QUINTIC:
254− for (int n = 0; n < nb_samples; n++) {
255− double sample = src[n] * factor;
353+ for (int n = 0; n < nb_osamples; n++) {
354+ double sample = dst[n] * factor;
256355
257356 if (FFABS(sample) >= 1.25)
258357 dst[n] = FFSIGN(sample);
@@ -262,8 +361,8 @@ static void filter_dbl(ASoftClipContext *s, · 3 unmodified lines
262361 }
263362 break;
264363 case ASC_SIN:
265− for (int n = 0; n < nb_samples; n++) {
266− double sample = src[n] * factor;
364+ for (int n = 0; n < nb_osamples; n++) {
365+ double sample = dst[n] * factor;
267366
268367 if (FFABS(sample) >= M_PI_2)
269368 dst[n] = FFSIGN(sample);
@@ -273,14 +372,21 @@ static void filter_dbl(ASoftClipContext *s, · 3 unmodified lines
273372 }
274373 break;
275374 case ASC_ERF:
276− for (int n = 0; n < nb_samples; n++) {
277− dst[n] = erf(src[n] * factor);
375+ for (int n = 0; n < nb_osamples; n++) {
376+ dst[n] = erf(dst[n] * factor);
278377 dst[n] *= gain;
279378 }
280379 break;
281380 default:
282381 av_assert0(0);
283382 }
383+
384+ w = (double *)(s->frame[1]->extended_data[c]) + 2 * (oversample - 1);
385+ for (int n = 0; n < nb_osamples && oversample > 1; n++)
386+ dst[n] = run_lowpassd(&s->lowpass[oversample - 1], dst[n], w);
387+
388+ for (int n = 0; n < nb_samples; n++)
389+ dst[n] = dst[n * oversample] * scale;
284390 }
285391 }
286392
@@ -288,47 +394,21 @@ static int config_input(AVFilterLink *inlink) · 1 unmodified line
288394 {
289395 AVFilterContext *ctx = inlink->dst;
290396 ASoftClipContext *s = ctx->priv;
291− int ret;
292397
293398 switch (inlink->format) {
294− case AV_SAMPLE_FMT_FLT:
295399 case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
296− case AV_SAMPLE_FMT_DBL:
297400 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
298401 default: av_assert0(0);
299402 }
300403
301− if (s->oversample <= 1)
302− return 0;
303−
304− s->up_ctx = swr_alloc();
305− s->down_ctx = swr_alloc();
306− if (!s->up_ctx || !s->down_ctx)
404+ s->frame[0] = ff_get_audio_buffer(inlink, 2 * MAX_OVERSAMPLE);
405+ s->frame[1] = ff_get_audio_buffer(inlink, 2 * MAX_OVERSAMPLE);
406+ if (!s->frame[0] || !s->frame[1])
307407 return AVERROR(ENOMEM);
308408
309− av_opt_set_int(s->up_ctx, "in_channel_layout", inlink->channel_layout, 0);
310− av_opt_set_int(s->up_ctx, "in_sample_rate", inlink->sample_rate, 0);
311− av_opt_set_sample_fmt(s->up_ctx, "in_sample_fmt", inlink->format, 0);
312−
313− av_opt_set_int(s->up_ctx, "out_channel_layout", inlink->channel_layout, 0);
314− av_opt_set_int(s->up_ctx, "out_sample_rate", inlink->sample_rate * s->oversample, 0);
315− av_opt_set_sample_fmt(s->up_ctx, "out_sample_fmt", inlink->format, 0);
316−
317− av_opt_set_int(s->down_ctx, "in_channel_layout", inlink->channel_layout, 0);
318− av_opt_set_int(s->down_ctx, "in_sample_rate", inlink->sample_rate * s->oversample, 0);
319− av_opt_set_sample_fmt(s->down_ctx, "in_sample_fmt", inlink->format, 0);
320−
321− av_opt_set_int(s->down_ctx, "out_channel_layout", inlink->channel_layout, 0);
322− av_opt_set_int(s->down_ctx, "out_sample_rate", inlink->sample_rate, 0);
323− av_opt_set_sample_fmt(s->down_ctx, "out_sample_fmt", inlink->format, 0);
324−
325− ret = swr_init(s->up_ctx);
326− if (ret < 0)
327− return ret;
328−
329− ret = swr_init(s->down_ctx);
330− if (ret < 0)
331− return ret;
409+ for (int i = 0; i < MAX_OVERSAMPLE; i++) {
410+ get_lowpass(&s->lowpass[i], inlink->sample_rate / 2, inlink->sample_rate * (i + 1));
411+ }
332412
333413 return 0;
334414 }
@@ -361,14 +441,14 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 26 unmodified lines
361441 AVFilterContext *ctx = inlink->dst;
362442 ASoftClipContext *s = ctx->priv;
363443 AVFilterLink *outlink = ctx->outputs[0];
364− int ret, nb_samples, channels;
444+ int nb_samples, channels;
365445 ThreadData td;
366446 AVFrame *out;
367447
368− if (av_frame_is_writable(in)) {
448+ if (av_frame_is_writable(in) && s->oversample == 1) {
369449 out = in;
370450 } else {
371− out = ff_get_audio_buffer(outlink, in->nb_samples);
451+ out = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
372452 if (!out) {
373453 av_frame_free(&in);
374454 return AVERROR(ENOMEM);
@@ -376,72 +456,29 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 1 unmodified line
376456 av_frame_copy_props(out, in);
377457 }
378458
379− if (av_sample_fmt_is_planar(in->format)) {
380− nb_samples = in->nb_samples;
381− channels = in->channels;
382− } else {
383− nb_samples = in->channels * in->nb_samples;
384− channels = 1;
385− }
386−
387− if (s->oversample > 1) {
388− s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
389− if (!s->frame) {
390− ret = AVERROR(ENOMEM);
391− goto fail;
392− }
459+ nb_samples = in->nb_samples;
460+ channels = in->channels;
393461
394− ret = swr_convert(s->up_ctx, (uint8_t**)s->frame->extended_data, in->nb_samples * s->oversample,
395− (const uint8_t **)in->extended_data, in->nb_samples);
396− if (ret < 0)
397− goto fail;
398−
399− td.in = s->frame;
400− td.out = s->frame;
401− td.nb_samples = av_sample_fmt_is_planar(in->format) ? ret : ret * in->channels;
402− td.channels = channels;
403− ff_filter_execute(ctx, filter_channels, &td, NULL,
404− FFMIN(channels, ff_filter_get_nb_threads(ctx)));
405−
406− ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
407− (const uint8_t **)s->frame->extended_data, ret);
408− if (ret < 0)
409− goto fail;
410−
411− if (out->pts)
412− out->pts -= s->delay;
413− s->delay += in->nb_samples - ret;
414− out->nb_samples = ret;
415−
416− av_frame_free(&s->frame);
417− } else {
418− td.in = in;
419− td.out = out;
420− td.nb_samples = nb_samples;
421− td.channels = channels;
422− ff_filter_execute(ctx, filter_channels, &td, NULL,
423− FFMIN(channels, ff_filter_get_nb_threads(ctx)));
424− }
462+ td.in = in;
463+ td.out = out;
464+ td.nb_samples = nb_samples;
465+ td.channels = channels;
466+ ff_filter_execute(ctx, filter_channels, &td, NULL,
467+ FFMIN(channels, ff_filter_get_nb_threads(ctx)));
425468
426469 if (out != in)
427470 av_frame_free(&in);
428471
472+ out->nb_samples /= s->oversample;
429473 return ff_filter_frame(outlink, out);
430−fail:
431− if (out != in)
432− av_frame_free(&out);
433− av_frame_free(&in);
434− av_frame_free(&s->frame);
435−
436− return ret;
437474 }
438475
439476 static av_cold void uninit(AVFilterContext *ctx)
440477 {
441478 ASoftClipContext *s = ctx->priv;
442479
443− swr_free(&s->up_ctx);
444− swr_free(&s->down_ctx);
480+ av_frame_free(&s->frame[0]);
481+ av_frame_free(&s->frame[1]);
445482 }
446483
447484 static const AVFilterPad inputs[] = {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t