2022-05-14 10:28:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
#include "libavutil/tx.h"
|
2022-05-14 10:28:49 +02:00
|
|
|
#include "avfilter.h"
|
|
|
|
#include "audio.h"
|
|
|
|
|
|
|
|
#undef ctype
|
|
|
|
#undef ftype
|
|
|
|
#undef SQRT
|
2022-12-13 12:46:02 +02:00
|
|
|
#undef HYPOT
|
2022-05-14 10:28:49 +02:00
|
|
|
#undef SAMPLE_FORMAT
|
2022-12-13 12:46:02 +02:00
|
|
|
#undef TX_TYPE
|
2023-11-18 01:36:18 +02:00
|
|
|
#undef FABS
|
|
|
|
#undef POW
|
2022-05-14 10:28:49 +02:00
|
|
|
#if DEPTH == 32
|
|
|
|
#define SAMPLE_FORMAT float
|
|
|
|
#define SQRT sqrtf
|
2022-12-13 12:46:02 +02:00
|
|
|
#define HYPOT hypotf
|
2022-05-14 10:28:49 +02:00
|
|
|
#define ctype AVComplexFloat
|
|
|
|
#define ftype float
|
2022-12-13 12:46:02 +02:00
|
|
|
#define TX_TYPE AV_TX_FLOAT_RDFT
|
2023-11-18 01:36:18 +02:00
|
|
|
#define FABS fabsf
|
|
|
|
#define POW powf
|
2022-05-14 10:28:49 +02:00
|
|
|
#else
|
|
|
|
#define SAMPLE_FORMAT double
|
|
|
|
#define SQRT sqrt
|
2022-12-13 12:46:02 +02:00
|
|
|
#define HYPOT hypot
|
2022-05-14 10:28:49 +02:00
|
|
|
#define ctype AVComplexDouble
|
|
|
|
#define ftype double
|
2022-12-13 12:46:02 +02:00
|
|
|
#define TX_TYPE AV_TX_DOUBLE_RDFT
|
2023-11-18 01:36:18 +02:00
|
|
|
#define FABS fabs
|
|
|
|
#define POW pow
|
2022-05-14 10:28:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define fn3(a,b) a##_##b
|
|
|
|
#define fn2(a,b) fn3(a,b)
|
|
|
|
#define fn(a) fn2(a, SAMPLE_FORMAT)
|
|
|
|
|
2023-11-18 01:36:18 +02:00
|
|
|
static ftype fn(ir_gain)(AVFilterContext *ctx, AudioFIRContext *s,
|
|
|
|
int cur_nb_taps, const ftype *time)
|
2022-05-14 10:28:49 +02:00
|
|
|
{
|
2023-11-18 01:36:18 +02:00
|
|
|
ftype ch_gain, sum = 0;
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2023-11-18 01:36:18 +02:00
|
|
|
if (s->ir_norm < 0.f) {
|
2022-12-13 12:46:02 +02:00
|
|
|
ch_gain = 1;
|
2023-11-18 01:36:18 +02:00
|
|
|
} else if (s->ir_norm == 0.f) {
|
|
|
|
for (int i = 0; i < cur_nb_taps; i++)
|
|
|
|
sum += time[i];
|
|
|
|
ch_gain = 1. / sum;
|
|
|
|
} else {
|
|
|
|
ftype ir_norm = s->ir_norm;
|
|
|
|
|
|
|
|
for (int i = 0; i < cur_nb_taps; i++)
|
|
|
|
sum += POW(FABS(time[i]), ir_norm);
|
|
|
|
ch_gain = 1. / POW(sum, 1. / ir_norm);
|
2022-05-14 10:28:49 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 01:36:18 +02:00
|
|
|
return ch_gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fn(ir_scale)(AVFilterContext *ctx, AudioFIRContext *s,
|
|
|
|
int cur_nb_taps, int ch,
|
|
|
|
ftype *time, ftype ch_gain)
|
|
|
|
{
|
2022-12-13 12:46:02 +02:00
|
|
|
if (ch_gain != 1. || s->ir_gain != 1.) {
|
|
|
|
ftype gain = ch_gain * s->ir_gain;
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "ch%d gain %f\n", ch, gain);
|
2022-05-14 10:28:49 +02:00
|
|
|
#if DEPTH == 32
|
2022-12-13 12:46:02 +02:00
|
|
|
s->fdsp->vector_fmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 4));
|
2022-05-14 10:28:49 +02:00
|
|
|
#else
|
2022-12-13 12:46:02 +02:00
|
|
|
s->fdsp->vector_dmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 8));
|
2022-05-14 10:28:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
static void fn(convert_channel)(AVFilterContext *ctx, AudioFIRContext *s, int ch,
|
2023-01-01 00:31:31 +02:00
|
|
|
AudioFIRSegment *seg, int coeff_partition, int selir)
|
2022-12-13 12:46:02 +02:00
|
|
|
{
|
|
|
|
const int coffset = coeff_partition * seg->coeff_size;
|
|
|
|
const int nb_taps = s->nb_taps[selir];
|
2023-01-01 00:31:31 +02:00
|
|
|
ftype *time = (ftype *)s->norm_ir[selir]->extended_data[ch];
|
2022-12-13 12:46:02 +02:00
|
|
|
ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
|
|
|
|
ftype *tempout = (ftype *)seg->tempout->extended_data[ch];
|
2023-04-23 23:30:42 +02:00
|
|
|
ctype *coeff = (ctype *)seg->coeff->extended_data[ch];
|
2022-12-13 12:46:02 +02:00
|
|
|
const int remaining = nb_taps - (seg->input_offset + coeff_partition * seg->part_size);
|
|
|
|
const int size = remaining >= seg->part_size ? seg->part_size : remaining;
|
|
|
|
|
2022-12-25 11:24:07 +02:00
|
|
|
memset(tempin + size, 0, sizeof(*tempin) * (seg->block_size - size));
|
2022-12-13 12:46:02 +02:00
|
|
|
memcpy(tempin, time + seg->input_offset + coeff_partition * seg->part_size,
|
|
|
|
size * sizeof(*tempin));
|
|
|
|
seg->ctx_fn(seg->ctx[ch], tempout, tempin, sizeof(*tempin));
|
2022-12-25 11:24:07 +02:00
|
|
|
memcpy(coeff + coffset, tempout, seg->coeff_size * sizeof(*coeff));
|
2022-12-13 12:46:02 +02:00
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
|
2022-12-13 12:46:02 +02:00
|
|
|
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);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "fft_length: %d\n", seg->fft_length);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "coeff_size: %d\n", seg->coeff_size);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "input_size: %d\n", seg->input_size);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "input_offset: %d\n", seg->input_offset);
|
|
|
|
}
|
|
|
|
|
2022-05-14 10:28:49 +02:00
|
|
|
static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
|
|
|
|
{
|
2022-12-11 19:29:31 +02:00
|
|
|
if ((nb_samples & 15) == 0 && nb_samples >= 8) {
|
2022-05-14 10:28:49 +02:00
|
|
|
#if DEPTH == 32
|
|
|
|
s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);
|
|
|
|
#else
|
|
|
|
s->fdsp->vector_dmac_scalar(dst, src, 1.0, nb_samples);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
for (int n = 0; n < nb_samples; n++)
|
|
|
|
dst[n] += src[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:21:51 +02:00
|
|
|
static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int ioffset, int offset, int selir)
|
2022-05-14 10:28:49 +02:00
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2023-04-25 15:21:51 +02:00
|
|
|
const ftype *in = (const ftype *)s->in->extended_data[ch] + ioffset;
|
2023-01-01 00:31:31 +02:00
|
|
|
ftype *blockout, *ptr = (ftype *)out->extended_data[ch] + offset;
|
2022-12-11 19:29:31 +02:00
|
|
|
const int min_part_size = s->min_part_size;
|
2022-12-13 12:46:02 +02:00
|
|
|
const int nb_samples = FFMIN(min_part_size, out->nb_samples - offset);
|
2023-04-23 23:30:42 +02:00
|
|
|
const int nb_segments = s->nb_segments[selir];
|
2022-12-11 19:29:31 +02:00
|
|
|
const float dry_gain = s->dry_gain;
|
2023-04-27 22:47:31 +02:00
|
|
|
const float wet_gain = s->wet_gain;
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
for (int segment = 0; segment < nb_segments; segment++) {
|
2023-04-23 23:30:42 +02:00
|
|
|
AudioFIRSegment *seg = &s->seg[selir][segment];
|
2022-05-14 10:28:49 +02:00
|
|
|
ftype *src = (ftype *)seg->input->extended_data[ch];
|
|
|
|
ftype *dst = (ftype *)seg->output->extended_data[ch];
|
|
|
|
ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
|
|
|
|
ftype *sumout = (ftype *)seg->sumout->extended_data[ch];
|
2022-12-25 01:42:17 +02:00
|
|
|
ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
|
2023-01-01 00:31:31 +02:00
|
|
|
ftype *buf = (ftype *)seg->buffer->extended_data[ch];
|
2022-12-11 19:29:31 +02:00
|
|
|
int *output_offset = &seg->output_offset[ch];
|
2022-12-12 18:20:53 +02:00
|
|
|
const int nb_partitions = seg->nb_partitions;
|
2022-12-11 19:29:31 +02:00
|
|
|
const int input_offset = seg->input_offset;
|
|
|
|
const int part_size = seg->part_size;
|
|
|
|
int j;
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2022-12-19 00:50:24 +02:00
|
|
|
seg->part_index[ch] = seg->part_index[ch] % nb_partitions;
|
2023-04-27 22:47:31 +02:00
|
|
|
if (dry_gain == 1.f) {
|
|
|
|
memcpy(src + input_offset, in, nb_samples * sizeof(*src));
|
|
|
|
} else if (min_part_size >= 8) {
|
2022-05-14 10:28:49 +02:00
|
|
|
#if DEPTH == 32
|
2022-12-11 19:29:31 +02:00
|
|
|
s->fdsp->vector_fmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 4));
|
2022-05-14 10:28:49 +02:00
|
|
|
#else
|
2022-12-11 19:29:31 +02:00
|
|
|
s->fdsp->vector_dmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 8));
|
2022-05-14 10:28:49 +02:00
|
|
|
#endif
|
|
|
|
} else {
|
2022-12-11 19:29:31 +02:00
|
|
|
ftype *src2 = src + input_offset;
|
|
|
|
for (int n = 0; n < nb_samples; n++)
|
|
|
|
src2[n] = in[n] * dry_gain;
|
2022-05-14 10:28:49 +02:00
|
|
|
}
|
|
|
|
|
2022-12-11 19:29:31 +02:00
|
|
|
output_offset[0] += min_part_size;
|
2022-12-13 12:46:02 +02:00
|
|
|
if (output_offset[0] >= part_size) {
|
2022-12-11 19:29:31 +02:00
|
|
|
output_offset[0] = 0;
|
2022-05-14 10:28:49 +02:00
|
|
|
} else {
|
2022-12-11 19:29:31 +02:00
|
|
|
memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2022-12-11 19:29:31 +02:00
|
|
|
dst += output_offset[0];
|
2022-05-14 10:28:49 +02:00
|
|
|
fn(fir_fadd)(s, ptr, dst, nb_samples);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
|
2023-01-01 00:31:31 +02:00
|
|
|
|
2022-05-14 10:28:49 +02:00
|
|
|
blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;
|
2022-12-25 01:42:17 +02:00
|
|
|
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));
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
j = seg->part_index[ch];
|
2022-12-12 18:20:53 +02:00
|
|
|
for (int i = 0; i < nb_partitions; i++) {
|
2022-12-25 01:42:17 +02:00
|
|
|
const int input_partition = j;
|
|
|
|
const int coeff_partition = i;
|
2022-12-13 12:46:02 +02:00
|
|
|
const int coffset = coeff_partition * seg->coeff_size;
|
|
|
|
const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
|
2023-04-23 23:30:42 +02:00
|
|
|
const ctype *coeff = ((const ctype *)seg->coeff->extended_data[ch]) + coffset;
|
2022-12-13 12:46:02 +02:00
|
|
|
|
|
|
|
if (j == 0)
|
|
|
|
j = nb_partitions;
|
|
|
|
j--;
|
2022-05-14 10:28:49 +02:00
|
|
|
|
|
|
|
#if DEPTH == 32
|
2022-12-11 19:29:31 +02:00
|
|
|
s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
|
2022-05-14 10:28:49 +02:00
|
|
|
#else
|
2022-12-11 19:29:31 +02:00
|
|
|
s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
|
2022-05-14 10:28:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:04:06 +02:00
|
|
|
seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
fn(fir_fadd)(s, buf, sumout, part_size);
|
|
|
|
memcpy(dst, buf, part_size * sizeof(*dst));
|
|
|
|
memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
|
|
|
|
|
2023-04-25 15:21:51 +02:00
|
|
|
fn(fir_fadd)(s, ptr, dst, nb_samples);
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2022-12-21 19:05:20 +02:00
|
|
|
if (part_size != min_part_size)
|
|
|
|
memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
seg->part_index[ch] = (seg->part_index[ch] + 1) % nb_partitions;
|
2022-05-14 10:28:49 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 22:47:31 +02:00
|
|
|
if (wet_gain == 1.f)
|
2022-12-12 13:26:48 +02:00
|
|
|
return 0;
|
|
|
|
|
2022-12-11 19:29:31 +02:00
|
|
|
if (min_part_size >= 8) {
|
2022-05-14 10:28:49 +02:00
|
|
|
#if DEPTH == 32
|
2023-04-27 22:47:31 +02:00
|
|
|
s->fdsp->vector_fmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 4));
|
2022-05-14 10:28:49 +02:00
|
|
|
#else
|
2023-04-27 22:47:31 +02:00
|
|
|
s->fdsp->vector_dmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 8));
|
2022-05-14 10:28:49 +02:00
|
|
|
#endif
|
|
|
|
} else {
|
2022-12-11 19:29:31 +02:00
|
|
|
for (int n = 0; n < nb_samples; n++)
|
2023-04-27 22:47:31 +02:00
|
|
|
ptr[n] *= wet_gain;
|
2022-05-14 10:28:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-11-20 00:45:19 +02:00
|
|
|
|
|
|
|
static void fn(fir_quantums)(AVFilterContext *ctx, AudioFIRContext *s, AVFrame *out,
|
|
|
|
int min_part_size, int ch, int offset,
|
|
|
|
int prev_selir, int selir)
|
|
|
|
{
|
|
|
|
if (ctx->is_disabled || s->prev_is_disabled) {
|
|
|
|
const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;
|
|
|
|
const ftype *xfade0 = (const ftype *)s->xfade[0]->extended_data[ch];
|
|
|
|
const ftype *xfade1 = (const ftype *)s->xfade[1]->extended_data[ch];
|
|
|
|
ftype *src0 = (ftype *)s->fadein[0]->extended_data[ch];
|
|
|
|
ftype *src1 = (ftype *)s->fadein[1]->extended_data[ch];
|
|
|
|
ftype *dst = ((ftype *)out->extended_data[ch]) + offset;
|
|
|
|
|
|
|
|
if (ctx->is_disabled && !s->prev_is_disabled) {
|
|
|
|
memset(src0, 0, min_part_size * sizeof(ftype));
|
|
|
|
fn(fir_quantum)(ctx, s->fadein[0], ch, offset, 0, selir);
|
|
|
|
for (int n = 0; n < min_part_size; n++)
|
|
|
|
dst[n] = xfade1[n] * src0[n] + xfade0[n] * in[n];
|
|
|
|
} else if (!ctx->is_disabled && s->prev_is_disabled) {
|
|
|
|
memset(src1, 0, min_part_size * sizeof(ftype));
|
|
|
|
fn(fir_quantum)(ctx, s->fadein[1], ch, offset, 0, selir);
|
|
|
|
for (int n = 0; n < min_part_size; n++)
|
|
|
|
dst[n] = xfade1[n] * in[n] + xfade0[n] * src1[n];
|
|
|
|
} else {
|
|
|
|
memcpy(dst, in, sizeof(ftype) * min_part_size);
|
|
|
|
}
|
|
|
|
} else if (prev_selir != selir && s->loading[ch] != 0) {
|
|
|
|
const ftype *xfade0 = (const ftype *)s->xfade[0]->extended_data[ch];
|
|
|
|
const ftype *xfade1 = (const ftype *)s->xfade[1]->extended_data[ch];
|
|
|
|
ftype *src0 = (ftype *)s->fadein[0]->extended_data[ch];
|
|
|
|
ftype *src1 = (ftype *)s->fadein[1]->extended_data[ch];
|
|
|
|
ftype *dst = ((ftype *)out->extended_data[ch]) + offset;
|
|
|
|
|
|
|
|
memset(src0, 0, min_part_size * sizeof(ftype));
|
|
|
|
memset(src1, 0, min_part_size * sizeof(ftype));
|
|
|
|
|
|
|
|
fn(fir_quantum)(ctx, s->fadein[0], ch, offset, 0, prev_selir);
|
|
|
|
fn(fir_quantum)(ctx, s->fadein[1], ch, offset, 0, selir);
|
|
|
|
|
|
|
|
if (s->loading[ch] > s->max_offset[selir]) {
|
|
|
|
for (int n = 0; n < min_part_size; n++)
|
|
|
|
dst[n] = xfade1[n] * src0[n] + xfade0[n] * src1[n];
|
|
|
|
s->loading[ch] = 0;
|
|
|
|
} else {
|
|
|
|
memcpy(dst, src0, min_part_size * sizeof(ftype));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fn(fir_quantum)(ctx, out, ch, offset, offset, selir);
|
|
|
|
}
|
|
|
|
}
|