History

avfilter/af_afir: improve output quality with small minp

Remove direct convolution implementation as its give worse results. Simplifies code too.

Paul B Mahol committed 3 years ago

Commit 85560061 parent 07e07322 files · +34 −69Browse files

Unified · Split

2 files
  1. libavfilter/af_afir.c +5 −3
  2. libavfilter/afir_template.c +29 −66
libavfilter/af_afir.c+5−3
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525
2626 #include <float.h>
2727
28+#include "libavutil/cpu.h"
2829 #include "libavutil/tx.h"
2930 #include "libavutil/avstring.h"
3031 #include "libavutil/channel_layout.h"
@@ -158,6 +159,7 @@ static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, · 127 unmodified lines
158159 int offset, int nb_partitions, int part_size)
159160 {
160161 AudioFIRContext *s = ctx->priv;
162+ const size_t cpu_align = av_cpu_max_align();
161163 int ret;
162164
163165 seg->tx = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->tx));
@@ -167,8 +169,8 @@ static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, · 3 unmodified lines
167169
168170 seg->fft_length = part_size * 2 + 1;
169171 seg->part_size = part_size;
170− seg->block_size = FFALIGN(seg->fft_length, 32);
171− seg->coeff_size = FFALIGN(seg->part_size + 1, 32);
172+ seg->block_size = FFALIGN(seg->fft_length, cpu_align);
173+ seg->coeff_size = FFALIGN(seg->part_size + 1, cpu_align);
172174 seg->nb_partitions = nb_partitions;
173175 seg->input_size = offset + s->min_part_size;
174176 seg->input_offset = offset;
@@ -178,7 +180,7 @@ static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, · 3 unmodified lines
178180 if (!seg->part_index || !seg->output_offset)
179181 return AVERROR(ENOMEM);
180182
181− for (int ch = 0; ch < ctx->inputs[0]->ch_layout.nb_channels && part_size >= 8; ch++) {
183+ for (int ch = 0; ch < ctx->inputs[0]->ch_layout.nb_channels && part_size >= 1; ch++) {
182184 union { double d; float f; } scale, iscale;
183185 enum AVTXType tx_type;
184186
libavfilter/afir_template.c+29−66
@@ -156,14 +156,6 @@ static void fn(convert_channels)(AVFilterContext *ctx, AudioFIRContext *s) · 155 unmodified lines
156156 const int remaining = s->nb_taps - toffset;
157157 const int size = remaining >= seg->part_size ? seg->part_size : remaining;
158158
159− if (size < 8) {
160− for (int n = 0; n < size; n++)
161− coeff[coffset + n].re = time[toffset + n];
162−
163− toffset += size;
164− continue;
165− }
166−
167159 memset(blockin, 0, sizeof(*blockin) * seg->fft_length);
168160 memcpy(blockin, time + toffset, size * sizeof(*blockin));
169161
@@ -245,16 +237,9 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s, int cur_nb_ta · 75 unmodified lines
245237 return 0;
246238 }
247239
248−static void fn(direct)(const ftype *in, const ctype *ir, int len, ftype *out)
249−{
250− for (int n = 0; n < len; n++)
251− for (int m = 0; m <= n; m++)
252− out[n] += ir[m].re * in[n - m];
253−}
254−
255240 static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
256241 {
257− if ((nb_samples & 15) == 0 && nb_samples >= 16) {
242+ if ((nb_samples & 15) == 0 && nb_samples >= 8) {
258243 #if DEPTH == 32
259244 s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);
260245 #else
@@ -272,7 +257,8 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 11 unmodified lines
272257 const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;
273258 ftype *blockin, *blockout, *buf, *ptr = (ftype *)out->extended_data[ch] + offset;
274259 const int nb_samples = FFMIN(s->min_part_size, out->nb_samples - offset);
275− int n, i, j;
260+ const int min_part_size = s->min_part_size;
261+ const float dry_gain = s->dry_gain;
276262
277263 for (int segment = 0; segment < s->nb_segments; segment++) {
278264 AudioFIRSegment *seg = &s->seg[segment];
@@ -280,76 +266,55 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 1 unmodified line
280266 ftype *dst = (ftype *)seg->output->extended_data[ch];
281267 ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
282268 ftype *sumout = (ftype *)seg->sumout->extended_data[ch];
269+ int *output_offset = &seg->output_offset[ch];
270+ const int input_offset = seg->input_offset;
271+ const int part_size = seg->part_size;
272+ int j;
283273
284− if (s->min_part_size >= 8) {
274+ if (min_part_size >= 8) {
285275 #if DEPTH == 32
286− s->fdsp->vector_fmul_scalar(src + seg->input_offset, in, s->dry_gain, FFALIGN(nb_samples, 4));
276+ s->fdsp->vector_fmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 4));
287277 #else
288− s->fdsp->vector_dmul_scalar(src + seg->input_offset, in, s->dry_gain, FFALIGN(nb_samples, 8));
278+ s->fdsp->vector_dmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 8));
289279 #endif
290280 emms_c();
291281 } else {
292− for (n = 0; n < nb_samples; n++)
293− src[seg->input_offset + n] = in[n] * s->dry_gain;
282+ ftype *src2 = src + input_offset;
283+ for (int n = 0; n < nb_samples; n++)
284+ src2[n] = in[n] * dry_gain;
294285 }
295286
296− seg->output_offset[ch] += s->min_part_size;
297− if (seg->output_offset[ch] == seg->part_size) {
298− seg->output_offset[ch] = 0;
287+ output_offset[0] += min_part_size;
288+ if (output_offset[0] == part_size) {
289+ output_offset[0] = 0;
299290 } else {
300− memmove(src, src + s->min_part_size, (seg->input_size - s->min_part_size) * sizeof(*src));
291+ memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
301292
302− dst += seg->output_offset[ch];
293+ dst += output_offset[0];
303294 fn(fir_fadd)(s, ptr, dst, nb_samples);
304295 continue;
305296 }
306297
307− if (seg->part_size < 8) {
308− memset(dst, 0, sizeof(*dst) * seg->part_size * seg->nb_partitions);
309−
310− j = seg->part_index[ch];
311−
312− for (i = 0; i < seg->nb_partitions; i++) {
313− const int coffset = j * seg->coeff_size;
314− const ctype *coeff = (const ctype *)seg->coeff->extended_data[ch * !s->one2many] + coffset;
315−
316− fn(direct)(src, coeff, nb_samples, dst);
317−
318− if (j == 0)
319− j = seg->nb_partitions;
320− j--;
321− }
322−
323− seg->part_index[ch] = (seg->part_index[ch] + 1) % seg->nb_partitions;
324−
325− memmove(src, src + s->min_part_size, (seg->input_size - s->min_part_size) * sizeof(*src));
326−
327− for (n = 0; n < nb_samples; n++) {
328− ptr[n] += dst[n];
329− }
330− continue;
331− }
332−
333298 memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
334299 blockin = (ftype *)seg->blockin->extended_data[ch] + seg->part_index[ch] * seg->block_size;
335300 blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;
336− memset(blockin + seg->part_size, 0, sizeof(*blockin) * (seg->fft_length - seg->part_size));
301+ memset(blockin + part_size, 0, sizeof(*blockin) * (seg->fft_length - part_size));
337302
338− memcpy(blockin, src, sizeof(*src) * seg->part_size);
303+ memcpy(blockin, src, sizeof(*src) * part_size);
339304
340305 seg->tx_fn(seg->tx[ch], blockout, blockin, sizeof(ftype));
341306
342307 j = seg->part_index[ch];
343308
344− for (i = 0; i < seg->nb_partitions; i++) {
309+ for (int i = 0; i < seg->nb_partitions; i++) {
345310 const int coffset = j * seg->coeff_size;
346311 const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + i * seg->block_size;
347312 const ctype *coeff = (const ctype *)seg->coeff->extended_data[ch * !s->one2many] + coffset;
348313
349314 #if DEPTH == 32
350− s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, seg->part_size);
315+ s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
351316 #else
352− s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, seg->part_size);
317+ s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
353318 #endif
354319
355320 if (j == 0)
@@ -360,21 +325,21 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 4 unmodified lines
360325 seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
361326
362327 buf = (ftype *)seg->buffer->extended_data[ch];
363− fn(fir_fadd)(s, buf, sumout, seg->part_size);
328+ fn(fir_fadd)(s, buf, sumout, part_size);
364329
365− memcpy(dst, buf, seg->part_size * sizeof(*dst));
330+ memcpy(dst, buf, part_size * sizeof(*dst));
366331
367332 buf = (ftype *)seg->buffer->extended_data[ch];
368− memcpy(buf, sumout + seg->part_size, seg->part_size * sizeof(*buf));
333+ memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
369334
370335 seg->part_index[ch] = (seg->part_index[ch] + 1) % seg->nb_partitions;
371336
372− memmove(src, src + s->min_part_size, (seg->input_size - s->min_part_size) * sizeof(*src));
337+ memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
373338
374339 fn(fir_fadd)(s, ptr, dst, nb_samples);
375340 }
376341
377− if (s->min_part_size >= 8) {
342+ if (min_part_size >= 8) {
378343 #if DEPTH == 32
379344 s->fdsp->vector_fmul_scalar(ptr, ptr, s->wet_gain, FFALIGN(nb_samples, 4));
380345 #else
@@ -382,11 +347,9 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 1 unmodified line
382347 #endif
383348 emms_c();
384349 } else {
385− for (n = 0; n < nb_samples; n++)
350+ for (int n = 0; n < nb_samples; n++)
386351 ptr[n] *= s->wet_gain;
387352 }
388353
389354 return 0;
390355 }
391−
392−

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