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
25 25
26#include <float.h>26#include <float.h>
27 27
28#include "libavutil/cpu.h"
28#include "libavutil/tx.h"29#include "libavutil/tx.h"
29#include "libavutil/avstring.h"30#include "libavutil/avstring.h"
30#include "libavutil/channel_layout.h"31#include "libavutil/channel_layout.h"
@@ -158,6 +159,7 @@ static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, · 127 unmodified lines
158 int offset, int nb_partitions, int part_size)159 int offset, int nb_partitions, int part_size)
159{160{
160 AudioFIRContext *s = ctx->priv;161 AudioFIRContext *s = ctx->priv;
162 const size_t cpu_align = av_cpu_max_align();
161 int ret;163 int ret;
162 164
163 seg->tx = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->tx));165 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
167 169
168 seg->fft_length = part_size * 2 + 1;170 seg->fft_length = part_size * 2 + 1;
169 seg->part_size = part_size;171 seg->part_size = part_size;
170 seg->block_size = FFALIGN(seg->fft_length, 32);172 seg->block_size = FFALIGN(seg->fft_length, cpu_align);
171 seg->coeff_size = FFALIGN(seg->part_size + 1, 32);173 seg->coeff_size = FFALIGN(seg->part_size + 1, cpu_align);
172 seg->nb_partitions = nb_partitions;174 seg->nb_partitions = nb_partitions;
173 seg->input_size = offset + s->min_part_size;175 seg->input_size = offset + s->min_part_size;
174 seg->input_offset = offset;176 seg->input_offset = offset;
@@ -178,7 +180,7 @@ static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, · 3 unmodified lines
178 if (!seg->part_index || !seg->output_offset)180 if (!seg->part_index || !seg->output_offset)
179 return AVERROR(ENOMEM);181 return AVERROR(ENOMEM);
180 182
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++) {
182 union { double d; float f; } scale, iscale;184 union { double d; float f; } scale, iscale;
183 enum AVTXType tx_type;185 enum AVTXType tx_type;
184 186
libavfilter/afir_template.c+29−66
@@ -156,14 +156,6 @@ static void fn(convert_channels)(AVFilterContext *ctx, AudioFIRContext *s) · 155 unmodified lines
156 const int remaining = s->nb_taps - toffset;156 const int remaining = s->nb_taps - toffset;
157 const int size = remaining >= seg->part_size ? seg->part_size : remaining;157 const int size = remaining >= seg->part_size ? seg->part_size : remaining;
158 158
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
167 memset(blockin, 0, sizeof(*blockin) * seg->fft_length);159 memset(blockin, 0, sizeof(*blockin) * seg->fft_length);
168 memcpy(blockin, time + toffset, size * sizeof(*blockin));160 memcpy(blockin, time + toffset, size * sizeof(*blockin));
169 161
@@ -245,16 +237,9 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s, int cur_nb_ta · 75 unmodified lines
245 return 0;237 return 0;
246}238}
247 239
248static 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
255static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)240static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
256{241{
257 if ((nb_samples & 15) == 0 && nb_samples >= 16) {242 if ((nb_samples & 15) == 0 && nb_samples >= 8) {
258#if DEPTH == 32243#if DEPTH == 32
259 s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);244 s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);
260#else245#else
@@ -272,7 +257,8 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 11 unmodified lines
272 const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;257 const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;
273 ftype *blockin, *blockout, *buf, *ptr = (ftype *)out->extended_data[ch] + offset;258 ftype *blockin, *blockout, *buf, *ptr = (ftype *)out->extended_data[ch] + offset;
274 const int nb_samples = FFMIN(s->min_part_size, out->nb_samples - offset);259 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;
276 262
277 for (int segment = 0; segment < s->nb_segments; segment++) {263 for (int segment = 0; segment < s->nb_segments; segment++) {
278 AudioFIRSegment *seg = &s->seg[segment];264 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
280 ftype *dst = (ftype *)seg->output->extended_data[ch];266 ftype *dst = (ftype *)seg->output->extended_data[ch];
281 ftype *sumin = (ftype *)seg->sumin->extended_data[ch];267 ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
282 ftype *sumout = (ftype *)seg->sumout->extended_data[ch];268 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;
283 273
284 if (s->min_part_size >= 8) {274 if (min_part_size >= 8) {
285#if DEPTH == 32275#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));
287#else277#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));
289#endif279#endif
290 emms_c();280 emms_c();
291 } else {281 } else {
292 for (n = 0; n < nb_samples; n++)282 ftype *src2 = src + input_offset;
293 src[seg->input_offset + n] = in[n] * s->dry_gain;283 for (int n = 0; n < nb_samples; n++)
284 src2[n] = in[n] * dry_gain;
294 }285 }
295 286
296 seg->output_offset[ch] += s->min_part_size;287 output_offset[0] += min_part_size;
297 if (seg->output_offset[ch] == seg->part_size) {288 if (output_offset[0] == part_size) {
298 seg->output_offset[ch] = 0;289 output_offset[0] = 0;
299 } else {290 } 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));
301 292
302 dst += seg->output_offset[ch];293 dst += output_offset[0];
303 fn(fir_fadd)(s, ptr, dst, nb_samples);294 fn(fir_fadd)(s, ptr, dst, nb_samples);
304 continue;295 continue;
305 }296 }
306 297
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
333 memset(sumin, 0, sizeof(*sumin) * seg->fft_length);298 memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
334 blockin = (ftype *)seg->blockin->extended_data[ch] + seg->part_index[ch] * seg->block_size;299 blockin = (ftype *)seg->blockin->extended_data[ch] + seg->part_index[ch] * seg->block_size;
335 blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;300 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));
337 302
338 memcpy(blockin, src, sizeof(*src) * seg->part_size);303 memcpy(blockin, src, sizeof(*src) * part_size);
339 304
340 seg->tx_fn(seg->tx[ch], blockout, blockin, sizeof(ftype));305 seg->tx_fn(seg->tx[ch], blockout, blockin, sizeof(ftype));
341 306
342 j = seg->part_index[ch];307 j = seg->part_index[ch];
343 308
344 for (i = 0; i < seg->nb_partitions; i++) {309 for (int i = 0; i < seg->nb_partitions; i++) {
345 const int coffset = j * seg->coeff_size;310 const int coffset = j * seg->coeff_size;
346 const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + i * seg->block_size;311 const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + i * seg->block_size;
347 const ctype *coeff = (const ctype *)seg->coeff->extended_data[ch * !s->one2many] + coffset;312 const ctype *coeff = (const ctype *)seg->coeff->extended_data[ch * !s->one2many] + coffset;
348 313
349#if DEPTH == 32314#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);
351#else316#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);
353#endif318#endif
354 319
355 if (j == 0)320 if (j == 0)
@@ -360,21 +325,21 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 4 unmodified lines
360 seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));325 seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
361 326
362 buf = (ftype *)seg->buffer->extended_data[ch];327 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);
364 329
365 memcpy(dst, buf, seg->part_size * sizeof(*dst));330 memcpy(dst, buf, part_size * sizeof(*dst));
366 331
367 buf = (ftype *)seg->buffer->extended_data[ch];332 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));
369 334
370 seg->part_index[ch] = (seg->part_index[ch] + 1) % seg->nb_partitions;335 seg->part_index[ch] = (seg->part_index[ch] + 1) % seg->nb_partitions;
371 336
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));
373 338
374 fn(fir_fadd)(s, ptr, dst, nb_samples);339 fn(fir_fadd)(s, ptr, dst, nb_samples);
375 }340 }
376 341
377 if (s->min_part_size >= 8) {342 if (min_part_size >= 8) {
378#if DEPTH == 32343#if DEPTH == 32
379 s->fdsp->vector_fmul_scalar(ptr, ptr, s->wet_gain, FFALIGN(nb_samples, 4));344 s->fdsp->vector_fmul_scalar(ptr, ptr, s->wet_gain, FFALIGN(nb_samples, 4));
380#else345#else
@@ -382,11 +347,9 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse · 1 unmodified line
382#endif347#endif
383 emms_c();348 emms_c();
384 } else {349 } else {
385 for (n = 0; n < nb_samples; n++)350 for (int n = 0; n < nb_samples; n++)
386 ptr[n] *= s->wet_gain;351 ptr[n] *= s->wet_gain;
387 }352 }
388 353
389 return 0;354 return 0;
390}355}
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