aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/afir_template.c
diff options
context:
space:
mode:
authorPaul B Mahol <onemda@gmail.com>2022-12-31 23:31:31 +0100
committerPaul B Mahol <onemda@gmail.com>2023-01-02 15:33:57 +0100
commit3879555cd540f7df02ef527fcbc0fda4c68fbfa9 (patch)
tree3a45b52c2a17815762ceb34d1a389ee1fa6c2e36 /libavfilter/afir_template.c
parent62da0b4a741a064f118a0eece496d6bcc437ec91 (diff)
downloadffmpeg-3879555cd540f7df02ef527fcbc0fda4c68fbfa9.tar.gz
avfilter/afir_template: make IR transitions artifacts free
Diffstat (limited to 'libavfilter/afir_template.c')
-rw-r--r--libavfilter/afir_template.c137
1 files changed, 91 insertions, 46 deletions
diff --git a/libavfilter/afir_template.c b/libavfilter/afir_template.c
index d42ff882d3..3f3778c675 100644
--- a/libavfilter/afir_template.c
+++ b/libavfilter/afir_template.c
@@ -141,7 +141,8 @@ end:
}
static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
- int cur_nb_taps, int ch)
+ int cur_nb_taps, int ch,
+ ftype *time)
{
ftype ch_gain = 1;
@@ -151,7 +152,6 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
break;
case 0:
{
- ftype *time = (ftype *)s->norm_ir->extended_data[ch];
ftype sum = 0;
for (int i = 0; i < cur_nb_taps; i++)
@@ -161,7 +161,6 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
break;
case 1:
{
- ftype *time = (ftype *)s->norm_ir->extended_data[ch];
ftype sum = 0;
for (int i = 0; i < cur_nb_taps; i++)
@@ -171,7 +170,6 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
break;
case 2:
{
- ftype *time = (ftype *)s->norm_ir->extended_data[ch];
ftype sum = 0;
for (int i = 0; i < cur_nb_taps; i++)
@@ -182,7 +180,7 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
case 3:
case 4:
{
- ftype *inc, *outc, scale;
+ ftype *inc, *outc, scale, power;
AVTXContext *tx;
av_tx_fn tx_fn;
int ret, size;
@@ -205,7 +203,6 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
}
{
- ftype power, *time = (ftype *)s->norm_ir->extended_data[ch];
memcpy(inc, time, cur_nb_taps * sizeof(SAMPLE_FORMAT));
tx_fn(tx, outc, inc, sizeof(SAMPLE_FORMAT));
@@ -233,7 +230,6 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
}
if (ch_gain != 1. || s->ir_gain != 1.) {
- ftype *time = (ftype *)s->norm_ir->extended_data[ch];
ftype gain = ch_gain * s->ir_gain;
av_log(ctx, AV_LOG_DEBUG, "ch%d gain %f\n", ch, gain);
@@ -248,45 +244,24 @@ static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
}
static void fn(convert_channel)(AVFilterContext *ctx, AudioFIRContext *s, int ch,
- AudioFIRSegment *seg)
+ AudioFIRSegment *seg, int coeff_partition, int selir)
{
- const int coeff_partition = seg->loading[ch];
const int coffset = coeff_partition * seg->coeff_size;
- const int selir = s->selir;
const int nb_taps = s->nb_taps[selir];
- ftype *tsrc = (ftype *)s->ir[selir]->extended_data[!s->one2many * ch];
- ftype *time = (ftype *)s->norm_ir->extended_data[ch];
+ ftype *time = (ftype *)s->norm_ir[selir]->extended_data[ch];
ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
ftype *tempout = (ftype *)seg->tempout->extended_data[ch];
- ctype *coeff = (ctype *)seg->coeff->extended_data[ch];
- int *loaded = (int *)seg->loaded->extended_data[ch];
+ ctype *coeff = (ctype *)seg->coeff[selir]->extended_data[ch];
const int remaining = nb_taps - (seg->input_offset + coeff_partition * seg->part_size);
const int size = remaining >= seg->part_size ? seg->part_size : remaining;
- if (loaded[coeff_partition] == selir + 1)
- return;
- loaded[coeff_partition] = selir + 1;
-
- memcpy(time, tsrc, sizeof(*time) * nb_taps);
- for (int i = FFMAX(1, s->length * nb_taps); i < nb_taps; i++)
- time[i] = 0;
-
-#if DEPTH == 32
- get_power_float(ctx, s, nb_taps, ch);
-#else
- get_power_double(ctx, s, nb_taps, ch);
-#endif
-
- av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
-
memset(tempin + size, 0, sizeof(*tempin) * (seg->block_size - size));
memcpy(tempin, time + seg->input_offset + coeff_partition * seg->part_size,
size * sizeof(*tempin));
-
seg->ctx_fn(seg->ctx[ch], tempout, tempin, sizeof(*tempin));
-
memcpy(coeff + coffset, tempout, seg->coeff_size * sizeof(*coeff));
+ av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", seg->nb_partitions);
av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", seg->part_size);
av_log(ctx, AV_LOG_DEBUG, "block size: %d\n", seg->block_size);
@@ -314,11 +289,12 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse
{
AudioFIRContext *s = ctx->priv;
const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;
- ftype *blockout, *buf, *ptr = (ftype *)out->extended_data[ch] + offset;
+ ftype *blockout, *ptr = (ftype *)out->extended_data[ch] + offset;
const int min_part_size = s->min_part_size;
const int nb_samples = FFMIN(min_part_size, out->nb_samples - offset);
const int nb_segments = s->nb_segments;
const float dry_gain = s->dry_gain;
+ const int selir = s->selir;
for (int segment = 0; segment < nb_segments; segment++) {
AudioFIRSegment *seg = &s->seg[segment];
@@ -327,6 +303,7 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse
ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
ftype *sumout = (ftype *)seg->sumout->extended_data[ch];
ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
+ ftype *buf = (ftype *)seg->buffer->extended_data[ch];
int *output_offset = &seg->output_offset[ch];
const int nb_partitions = seg->nb_partitions;
const int input_offset = seg->input_offset;
@@ -359,28 +336,71 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse
}
memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
+
+ if (seg->loading[ch] < nb_partitions) {
+ j = seg->part_index[ch] <= 0 ? nb_partitions - 1 : seg->part_index[ch] - 1;
+ for (int i = 0; i < nb_partitions; i++) {
+ const int input_partition = j;
+ const int coeff_partition = i;
+ const int coffset = coeff_partition * seg->coeff_size;
+ const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
+ const ctype *coeff = ((const ctype *)seg->coeff[selir]->extended_data[ch]) + coffset;
+
+ if (j == 0)
+ j = nb_partitions;
+ j--;
+
+#if DEPTH == 32
+ s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
+#else
+ s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
+#endif
+ }
+
+ seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
+ memcpy(dst + part_size, sumout + part_size, part_size * sizeof(*buf));
+ memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
+ }
+
blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;
memset(tempin + part_size, 0, sizeof(*tempin) * (seg->block_size - part_size));
memcpy(tempin, src, sizeof(*src) * part_size);
-
seg->tx_fn(seg->tx[ch], blockout, tempin, sizeof(ftype));
- j = seg->part_index[ch];
if (seg->loading[ch] < nb_partitions) {
+ const int selir = s->prev_selir;
+
+ j = seg->part_index[ch];
+ for (int i = 0; i < nb_partitions; i++) {
+ const int input_partition = j;
+ const int coeff_partition = i;
+ const int coffset = coeff_partition * seg->coeff_size;
+ const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
+ const ctype *coeff = ((const ctype *)seg->coeff[selir]->extended_data[ch]) + coffset;
+
+ if (j == 0)
+ j = nb_partitions;
+ j--;
+
#if DEPTH == 32
- convert_channel_float(ctx, s, ch, seg);
+ s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
#else
- convert_channel_double(ctx, s, ch, seg);
+ s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
#endif
- seg->loading[ch]++;
+ }
+
+ seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
+ memcpy(dst + 2 * part_size, sumout, 2 * part_size * sizeof(*dst));
+ memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
}
+ j = seg->part_index[ch];
for (int i = 0; i < nb_partitions; i++) {
const int input_partition = j;
const int coeff_partition = i;
const int coffset = coeff_partition * seg->coeff_size;
const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
- const ctype *coeff = ((const ctype *)seg->coeff->extended_data[ch]) + coffset;
+ const ctype *coeff = ((const ctype *)seg->coeff[selir]->extended_data[ch]) + coffset;
if (j == 0)
j = nb_partitions;
@@ -395,18 +415,43 @@ static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int offse
seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
- buf = (ftype *)seg->buffer->extended_data[ch];
- fn(fir_fadd)(s, buf, sumout, part_size);
-
- memcpy(dst, buf, part_size * sizeof(*dst));
- memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
+ if (seg->loading[ch] < nb_partitions) {
+ ftype *ptr1 = dst + part_size;
+ ftype *ptr2 = dst + part_size * 2;
+ ftype *ptr3 = dst + part_size * 3;
+ ftype *ptr4 = dst + part_size * 4;
+ if (seg->loading[ch] == 0)
+ memcpy(ptr4, buf, sizeof(*ptr4) * part_size);
+ for (int n = 0; n < part_size; n++)
+ ptr2[n] += ptr4[n];
+
+ if (seg->loading[ch] < nb_partitions - 1)
+ memcpy(ptr4, ptr3, part_size * sizeof(*dst));
+ for (int n = 0; n < part_size; n++)
+ ptr1[n] += sumout[n];
+
+ if (seg->loading[ch] == nb_partitions - 1)
+ memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
+
+ for (int i = 0; i < part_size; i++) {
+ const ftype factor = (part_size * seg->loading[ch] + i) / (ftype)(part_size * nb_partitions);
+ const ftype ifactor = 1 - factor;
+ dst[i] = ptr1[i] * factor + ptr2[i] * ifactor;
+ }
+ } else {
+ fn(fir_fadd)(s, buf, sumout, part_size);
+ memcpy(dst, buf, part_size * sizeof(*dst));
+ memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
+ }
- seg->part_index[ch] = (seg->part_index[ch] + 1) % nb_partitions;
+ fn(fir_fadd)(s, ptr, dst, nb_samples);
if (part_size != min_part_size)
memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
- fn(fir_fadd)(s, ptr, dst, nb_samples);
+ seg->part_index[ch] = (seg->part_index[ch] + 1) % nb_partitions;
+ if (seg->loading[ch] < nb_partitions)
+ seg->loading[ch]++;
}
if (s->wet_gain == 1.f)