2017-01-26 18:03:08 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* An arbitrary audio FIR filter
|
|
|
|
*/
|
|
|
|
|
2018-05-29 18:24:22 +02:00
|
|
|
#include <float.h>
|
|
|
|
|
2024-06-10 14:53:44 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2022-12-11 19:29:31 +02:00
|
|
|
#include "libavutil/cpu.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2022-01-29 12:35:40 +02:00
|
|
|
#include "libavutil/tx.h"
|
2020-01-09 21:06:57 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2021-06-12 01:10:58 +02:00
|
|
|
#include "libavutil/channel_layout.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
#include "libavutil/float_dsp.h"
|
2022-05-03 05:37:15 +02:00
|
|
|
#include "libavutil/frame.h"
|
|
|
|
#include "libavutil/log.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
#include "libavutil/opt.h"
|
2024-05-24 10:12:12 +02:00
|
|
|
#include "libavutil/rational.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
2018-09-15 20:35:08 +02:00
|
|
|
#include "filters.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
#include "formats.h"
|
2022-05-03 05:37:15 +02:00
|
|
|
#include "af_afirdsp.h"
|
|
|
|
|
2024-05-24 10:12:12 +02:00
|
|
|
#define MAX_IR_STREAMS 32
|
|
|
|
|
|
|
|
typedef struct AudioFIRSegment {
|
|
|
|
int nb_partitions;
|
|
|
|
int part_size;
|
|
|
|
int block_size;
|
|
|
|
int fft_length;
|
|
|
|
int coeff_size;
|
|
|
|
int input_size;
|
|
|
|
int input_offset;
|
|
|
|
|
|
|
|
int *output_offset;
|
|
|
|
int *part_index;
|
|
|
|
|
|
|
|
AVFrame *sumin;
|
|
|
|
AVFrame *sumout;
|
|
|
|
AVFrame *blockout;
|
|
|
|
AVFrame *tempin;
|
|
|
|
AVFrame *tempout;
|
|
|
|
AVFrame *buffer;
|
|
|
|
AVFrame *coeff;
|
|
|
|
AVFrame *input;
|
|
|
|
AVFrame *output;
|
|
|
|
|
|
|
|
AVTXContext **ctx, **tx, **itx;
|
|
|
|
av_tx_fn ctx_fn, tx_fn, itx_fn;
|
|
|
|
} AudioFIRSegment;
|
|
|
|
|
|
|
|
typedef struct AudioFIRContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
float wet_gain;
|
|
|
|
float dry_gain;
|
|
|
|
float length;
|
|
|
|
int gtype;
|
|
|
|
float ir_norm;
|
|
|
|
float ir_link;
|
|
|
|
float ir_gain;
|
|
|
|
int ir_format;
|
|
|
|
int ir_load;
|
|
|
|
float max_ir_len;
|
|
|
|
int response;
|
|
|
|
int w, h;
|
|
|
|
AVRational frame_rate;
|
|
|
|
int ir_channel;
|
|
|
|
int minp;
|
|
|
|
int maxp;
|
|
|
|
int nb_irs;
|
|
|
|
int prev_selir;
|
|
|
|
int selir;
|
|
|
|
int precision;
|
|
|
|
int format;
|
|
|
|
|
|
|
|
int eof_coeffs[MAX_IR_STREAMS];
|
|
|
|
int have_coeffs[MAX_IR_STREAMS];
|
|
|
|
int nb_taps[MAX_IR_STREAMS];
|
|
|
|
int nb_segments[MAX_IR_STREAMS];
|
|
|
|
int max_offset[MAX_IR_STREAMS];
|
|
|
|
int nb_channels;
|
|
|
|
int one2many;
|
|
|
|
int prev_is_disabled;
|
|
|
|
int *loading;
|
|
|
|
double *ch_gain;
|
|
|
|
|
|
|
|
AudioFIRSegment seg[MAX_IR_STREAMS][1024];
|
|
|
|
|
|
|
|
AVFrame *in;
|
|
|
|
AVFrame *xfade[2];
|
|
|
|
AVFrame *fadein[2];
|
|
|
|
AVFrame *ir[MAX_IR_STREAMS];
|
|
|
|
AVFrame *norm_ir[MAX_IR_STREAMS];
|
|
|
|
int min_part_size;
|
|
|
|
int max_part_size;
|
|
|
|
int64_t pts;
|
|
|
|
|
|
|
|
AudioFIRDSPContext afirdsp;
|
|
|
|
AVFloatDSPContext *fdsp;
|
|
|
|
} AudioFIRContext;
|
|
|
|
|
2022-05-14 10:28:49 +02:00
|
|
|
#define DEPTH 32
|
|
|
|
#include "afir_template.c"
|
2018-12-29 11:46:25 +02:00
|
|
|
|
2022-05-14 10:28:49 +02:00
|
|
|
#undef DEPTH
|
|
|
|
#define DEPTH 64
|
|
|
|
#include "afir_template.c"
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-31 19:04:59 +02:00
|
|
|
static int fir_channel(AVFilterContext *ctx, AVFrame *out, int ch)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2022-12-13 12:46:02 +02:00
|
|
|
const int min_part_size = s->min_part_size;
|
2023-04-23 23:30:42 +02:00
|
|
|
const int prev_selir = s->prev_selir;
|
|
|
|
const int selir = s->selir;
|
2018-12-31 19:04:59 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
for (int offset = 0; offset < out->nb_samples; offset += min_part_size) {
|
2022-05-14 10:28:49 +02:00
|
|
|
switch (s->format) {
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2023-11-20 00:45:19 +02:00
|
|
|
fir_quantums_float(ctx, s, out, min_part_size, ch, offset, prev_selir, selir);
|
2022-05-14 10:28:49 +02:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
2023-11-20 00:45:19 +02:00
|
|
|
fir_quantums_double(ctx, s, out, min_part_size, ch, offset, prev_selir, selir);
|
2022-05-14 10:28:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-04-25 15:21:51 +02:00
|
|
|
|
2023-04-25 20:33:08 +02:00
|
|
|
if (selir != prev_selir && s->loading[ch] != 0)
|
|
|
|
s->loading[ch] += min_part_size;
|
2018-12-31 19:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:40:31 +02:00
|
|
|
static int fir_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
AVFrame *out = arg;
|
2021-08-31 16:03:14 +02:00
|
|
|
const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
|
|
|
|
const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
|
2018-12-30 18:40:31 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
for (int ch = start; ch < end; ch++)
|
2018-12-30 18:40:31 +02:00
|
|
|
fir_channel(ctx, out, ch);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
static int fir_frame(AudioFIRContext *s, AVFrame *in, AVFilterLink *outlink)
|
2017-01-26 18:03:08 +02:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2022-12-19 00:50:24 +02:00
|
|
|
AVFrame *out;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-27 15:11:12 +02:00
|
|
|
out = ff_get_audio_buffer(outlink, in->nb_samples);
|
2018-11-09 11:40:16 +02:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
2023-01-17 15:33:23 +02:00
|
|
|
av_frame_copy_props(out, in);
|
2022-12-13 12:46:02 +02:00
|
|
|
out->pts = s->pts = in->pts;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-08 20:23:45 +02:00
|
|
|
s->in = in;
|
2021-08-15 21:33:25 +02:00
|
|
|
ff_filter_execute(ctx, fir_channels, out, NULL,
|
2021-08-31 16:03:14 +02:00
|
|
|
FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
|
2023-11-19 22:26:00 +02:00
|
|
|
s->prev_is_disabled = ctx->is_disabled;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
av_frame_free(&in);
|
2020-01-08 20:23:45 +02:00
|
|
|
s->in = NULL;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-11-09 11:40:16 +02:00
|
|
|
return ff_filter_frame(outlink, out);
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, int selir,
|
2022-12-13 12:46:02 +02:00
|
|
|
int offset, int nb_partitions, int part_size, int index)
|
2018-12-28 19:31:36 +02:00
|
|
|
{
|
2018-12-29 12:40:13 +02:00
|
|
|
AudioFIRContext *s = ctx->priv;
|
2022-12-11 19:29:31 +02:00
|
|
|
const size_t cpu_align = av_cpu_max_align();
|
2022-12-12 02:55:06 +02:00
|
|
|
union { double d; float f; } cscale, scale, iscale;
|
|
|
|
enum AVTXType tx_type;
|
2022-11-09 17:58:04 +02:00
|
|
|
int ret;
|
2018-12-29 12:40:13 +02:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
seg->tx = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->tx));
|
2022-12-13 12:46:02 +02:00
|
|
|
seg->ctx = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->ctx));
|
2021-08-31 16:03:14 +02:00
|
|
|
seg->itx = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->itx));
|
2022-12-13 12:46:02 +02:00
|
|
|
if (!seg->tx || !seg->ctx || !seg->itx)
|
2018-12-28 19:31:36 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2022-12-25 11:21:47 +02:00
|
|
|
seg->fft_length = (part_size + 1) * 2;
|
2018-12-29 12:40:13 +02:00
|
|
|
seg->part_size = part_size;
|
2022-12-11 19:29:31 +02:00
|
|
|
seg->coeff_size = FFALIGN(seg->part_size + 1, cpu_align);
|
2023-07-24 10:34:17 +02:00
|
|
|
seg->block_size = FFMAX(seg->coeff_size * 2, FFALIGN(seg->fft_length, cpu_align));
|
2018-12-28 19:31:36 +02:00
|
|
|
seg->nb_partitions = nb_partitions;
|
2018-12-29 12:40:13 +02:00
|
|
|
seg->input_size = offset + s->min_part_size;
|
|
|
|
seg->input_offset = offset;
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
seg->part_index = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->part_index));
|
|
|
|
seg->output_offset = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*seg->output_offset));
|
2023-04-23 23:30:42 +02:00
|
|
|
if (!seg->part_index || !seg->output_offset)
|
2018-12-29 12:40:13 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2018-12-28 19:31:36 +02:00
|
|
|
|
2022-12-12 02:55:06 +02:00
|
|
|
switch (s->format) {
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
cscale.f = 1.f;
|
|
|
|
scale.f = 1.f / sqrtf(2.f * part_size);
|
|
|
|
iscale.f = 1.f / sqrtf(2.f * part_size);
|
|
|
|
tx_type = AV_TX_FLOAT_RDFT;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
cscale.d = 1.0;
|
|
|
|
scale.d = 1.0 / sqrt(2.0 * part_size);
|
|
|
|
iscale.d = 1.0 / sqrt(2.0 * part_size);
|
|
|
|
tx_type = AV_TX_DOUBLE_RDFT;
|
|
|
|
break;
|
2024-06-10 14:53:44 +02:00
|
|
|
default:
|
|
|
|
av_assert1(0);
|
2022-12-12 02:55:06 +02:00
|
|
|
}
|
2022-05-14 10:28:49 +02:00
|
|
|
|
2022-12-12 02:55:06 +02:00
|
|
|
for (int ch = 0; ch < ctx->inputs[0]->ch_layout.nb_channels && part_size >= 1; ch++) {
|
2022-12-13 12:46:02 +02:00
|
|
|
ret = av_tx_init(&seg->ctx[ch], &seg->ctx_fn, tx_type,
|
|
|
|
0, 2 * part_size, &cscale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-11-09 17:58:04 +02:00
|
|
|
ret = av_tx_init(&seg->tx[ch], &seg->tx_fn, tx_type,
|
|
|
|
0, 2 * part_size, &scale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ret = av_tx_init(&seg->itx[ch], &seg->itx_fn, tx_type,
|
|
|
|
1, 2 * part_size, &iscale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2018-12-28 19:31:36 +02:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:40 +02:00
|
|
|
seg->sumin = ff_get_audio_buffer(ctx->inputs[0], seg->fft_length);
|
|
|
|
seg->sumout = ff_get_audio_buffer(ctx->inputs[0], seg->fft_length);
|
2022-12-13 12:46:02 +02:00
|
|
|
seg->blockout = ff_get_audio_buffer(ctx->inputs[0], seg->block_size * seg->nb_partitions);
|
|
|
|
seg->tempin = ff_get_audio_buffer(ctx->inputs[0], seg->block_size);
|
|
|
|
seg->tempout = ff_get_audio_buffer(ctx->inputs[0], seg->block_size);
|
2018-12-28 19:31:36 +02:00
|
|
|
seg->buffer = ff_get_audio_buffer(ctx->inputs[0], seg->part_size);
|
2018-12-29 12:40:13 +02:00
|
|
|
seg->input = ff_get_audio_buffer(ctx->inputs[0], seg->input_size);
|
2023-01-01 00:31:31 +02:00
|
|
|
seg->output = ff_get_audio_buffer(ctx->inputs[0], seg->part_size * 5);
|
2022-12-25 01:42:17 +02:00
|
|
|
if (!seg->buffer || !seg->sumin || !seg->sumout || !seg->blockout ||
|
2023-01-01 00:31:31 +02:00
|
|
|
!seg->input || !seg->output || !seg->tempin || !seg->tempout)
|
2018-12-28 19:31:36 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
static void uninit_segment(AVFilterContext *ctx, AudioFIRSegment *seg)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
if (seg->ctx) {
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
|
|
|
av_tx_uninit(&seg->ctx[ch]);
|
|
|
|
}
|
|
|
|
av_freep(&seg->ctx);
|
2022-12-12 02:55:06 +02:00
|
|
|
|
2022-01-29 12:35:40 +02:00
|
|
|
if (seg->tx) {
|
2022-12-13 12:46:02 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
2022-01-29 12:35:40 +02:00
|
|
|
av_tx_uninit(&seg->tx[ch]);
|
2020-01-09 21:06:57 +02:00
|
|
|
}
|
2022-01-29 12:35:40 +02:00
|
|
|
av_freep(&seg->tx);
|
2020-01-09 21:06:57 +02:00
|
|
|
|
2022-01-29 12:35:40 +02:00
|
|
|
if (seg->itx) {
|
2022-12-13 12:46:02 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
2022-01-29 12:35:40 +02:00
|
|
|
av_tx_uninit(&seg->itx[ch]);
|
2020-01-09 21:06:57 +02:00
|
|
|
}
|
2022-01-29 12:35:40 +02:00
|
|
|
av_freep(&seg->itx);
|
2020-01-09 21:06:57 +02:00
|
|
|
|
|
|
|
av_freep(&seg->output_offset);
|
|
|
|
av_freep(&seg->part_index);
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
av_frame_free(&seg->tempin);
|
|
|
|
av_frame_free(&seg->tempout);
|
2022-01-29 12:35:40 +02:00
|
|
|
av_frame_free(&seg->blockout);
|
|
|
|
av_frame_free(&seg->sumin);
|
|
|
|
av_frame_free(&seg->sumout);
|
2020-01-09 21:06:57 +02:00
|
|
|
av_frame_free(&seg->buffer);
|
|
|
|
av_frame_free(&seg->input);
|
|
|
|
av_frame_free(&seg->output);
|
|
|
|
seg->input_size = 0;
|
2023-01-01 00:31:31 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < MAX_IR_STREAMS; i++)
|
2023-04-23 23:30:42 +02:00
|
|
|
av_frame_free(&seg->coeff);
|
2020-01-09 21:06:57 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
static int convert_coeffs(AVFilterContext *ctx, int selir)
|
2017-01-26 18:03:08 +02:00
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2023-01-01 00:31:31 +02:00
|
|
|
int ret, nb_taps, cur_nb_taps;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
if (!s->nb_taps[selir]) {
|
2020-01-09 21:06:57 +02:00
|
|
|
int part_size, max_part_size;
|
|
|
|
int left, offset = 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
s->nb_taps[selir] = ff_inlink_queued_samples(ctx->inputs[1 + selir]);
|
|
|
|
if (s->nb_taps[selir] <= 0)
|
2020-01-09 21:06:57 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
if (s->minp > s->maxp)
|
2020-01-09 21:06:57 +02:00
|
|
|
s->maxp = s->minp;
|
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
if (s->nb_segments[selir])
|
2022-12-13 12:46:02 +02:00
|
|
|
goto skip;
|
|
|
|
|
|
|
|
left = s->nb_taps[selir];
|
2020-01-09 21:06:57 +02:00
|
|
|
part_size = 1 << av_log2(s->minp);
|
|
|
|
max_part_size = 1 << av_log2(s->maxp);
|
2018-12-28 19:07:13 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
for (int i = 0; left > 0; i++) {
|
2023-04-25 15:21:51 +02:00
|
|
|
int step = (part_size == max_part_size) ? INT_MAX : 1 + (i == 0);
|
2020-01-09 21:06:57 +02:00
|
|
|
int nb_partitions = FFMIN(step, (left + part_size - 1) / part_size);
|
2018-12-29 12:40:13 +02:00
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
s->nb_segments[selir] = i + 1;
|
|
|
|
ret = init_segment(ctx, &s->seg[selir][i], selir, offset, nb_partitions, part_size, i);
|
2020-01-09 21:06:57 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
offset += nb_partitions * part_size;
|
2023-04-25 20:33:08 +02:00
|
|
|
s->max_offset[selir] = offset;
|
2020-01-09 21:06:57 +02:00
|
|
|
left -= nb_partitions * part_size;
|
|
|
|
part_size *= 2;
|
|
|
|
part_size = FFMIN(part_size, max_part_size);
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 12:40:13 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
skip:
|
|
|
|
if (!s->ir[selir]) {
|
|
|
|
ret = ff_inlink_consume_samples(ctx->inputs[1 + selir], s->nb_taps[selir], s->nb_taps[selir], &s->ir[selir]);
|
2018-12-29 12:40:13 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-01-09 21:06:57 +02:00
|
|
|
if (ret == 0)
|
|
|
|
return AVERROR_BUG;
|
2018-12-29 12:40:13 +02:00
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
cur_nb_taps = s->ir[selir]->nb_samples;
|
2023-01-01 00:31:31 +02:00
|
|
|
nb_taps = cur_nb_taps;
|
2018-10-03 21:43:40 +02:00
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
if (!s->norm_ir[selir] || s->norm_ir[selir]->nb_samples < nb_taps) {
|
|
|
|
av_frame_free(&s->norm_ir[selir]);
|
|
|
|
s->norm_ir[selir] = ff_get_audio_buffer(ctx->inputs[0], FFALIGN(nb_taps, 8));
|
|
|
|
if (!s->norm_ir[selir])
|
2022-12-13 12:46:02 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2018-10-03 21:43:40 +02:00
|
|
|
}
|
2017-12-31 20:09:07 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "nb_taps: %d\n", cur_nb_taps);
|
2023-04-23 23:30:42 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "nb_segments: %d\n", s->nb_segments[selir]);
|
2018-12-29 11:39:19 +02:00
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
switch (s->format) {
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2023-11-18 01:36:18 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
const float *tsrc = (const float *)s->ir[selir]->extended_data[!s->one2many * ch];
|
|
|
|
|
|
|
|
s->ch_gain[ch] = ir_gain_float(ctx, s, nb_taps, tsrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ir_link) {
|
|
|
|
float gain = +INFINITY;
|
|
|
|
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
|
|
|
gain = fminf(gain, s->ch_gain[ch]);
|
|
|
|
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
|
|
|
s->ch_gain[ch] = gain;
|
|
|
|
}
|
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
const float *tsrc = (const float *)s->ir[selir]->extended_data[!s->one2many * ch];
|
|
|
|
float *time = (float *)s->norm_ir[selir]->extended_data[ch];
|
|
|
|
|
|
|
|
memcpy(time, tsrc, sizeof(*time) * nb_taps);
|
|
|
|
for (int i = FFMAX(1, s->length * nb_taps); i < nb_taps; i++)
|
|
|
|
time[i] = 0;
|
|
|
|
|
2023-11-18 01:36:18 +02:00
|
|
|
ir_scale_float(ctx, s, nb_taps, ch, time, s->ch_gain[ch]);
|
2023-01-01 00:31:31 +02:00
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
for (int n = 0; n < s->nb_segments[selir]; n++) {
|
|
|
|
AudioFIRSegment *seg = &s->seg[selir][n];
|
2023-01-01 00:31:31 +02:00
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
if (!seg->coeff)
|
|
|
|
seg->coeff = ff_get_audio_buffer(ctx->inputs[0], seg->nb_partitions * seg->coeff_size * 2);
|
|
|
|
if (!seg->coeff)
|
2023-01-01 00:31:31 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (int i = 0; i < seg->nb_partitions; i++)
|
|
|
|
convert_channel_float(ctx, s, ch, seg, i, selir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
2023-11-18 01:36:18 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
const double *tsrc = (const double *)s->ir[selir]->extended_data[!s->one2many * ch];
|
|
|
|
|
|
|
|
s->ch_gain[ch] = ir_gain_double(ctx, s, nb_taps, tsrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->ir_link) {
|
|
|
|
double gain = +INFINITY;
|
|
|
|
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
|
|
|
gain = fmin(gain, s->ch_gain[ch]);
|
|
|
|
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
|
|
|
s->ch_gain[ch] = gain;
|
|
|
|
}
|
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
const double *tsrc = (const double *)s->ir[selir]->extended_data[!s->one2many * ch];
|
|
|
|
double *time = (double *)s->norm_ir[selir]->extended_data[ch];
|
|
|
|
|
|
|
|
memcpy(time, tsrc, sizeof(*time) * nb_taps);
|
|
|
|
for (int i = FFMAX(1, s->length * nb_taps); i < nb_taps; i++)
|
|
|
|
time[i] = 0;
|
|
|
|
|
2023-11-18 01:36:18 +02:00
|
|
|
ir_scale_double(ctx, s, nb_taps, ch, time, s->ch_gain[ch]);
|
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
for (int n = 0; n < s->nb_segments[selir]; n++) {
|
|
|
|
AudioFIRSegment *seg = &s->seg[selir][n];
|
2023-01-01 00:31:31 +02:00
|
|
|
|
2023-04-23 23:30:42 +02:00
|
|
|
if (!seg->coeff)
|
|
|
|
seg->coeff = ff_get_audio_buffer(ctx->inputs[0], seg->nb_partitions * seg->coeff_size * 2);
|
|
|
|
if (!seg->coeff)
|
2023-01-01 00:31:31 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (int i = 0; i < seg->nb_partitions; i++)
|
|
|
|
convert_channel_double(ctx, s, ch, seg, i, selir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
s->have_coeffs[selir] = 1;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-30 14:06:40 +02:00
|
|
|
static int check_ir(AVFilterLink *link, int selir)
|
2017-01-26 18:03:08 +02:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = link->dst;
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2018-10-03 12:49:44 +02:00
|
|
|
int nb_taps, max_nb_taps;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-10-03 12:49:44 +02:00
|
|
|
nb_taps = ff_inlink_queued_samples(link);
|
2018-04-16 19:06:09 +02:00
|
|
|
max_nb_taps = s->max_ir_len * ctx->outputs[0]->sample_rate;
|
2017-01-26 18:03:08 +02:00
|
|
|
if (nb_taps > max_nb_taps) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Too big number of coefficients: %d > %d.\n", nb_taps, max_nb_taps);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2023-05-30 14:06:40 +02:00
|
|
|
if (ff_inlink_check_available_samples(link, nb_taps + 1) == 1)
|
|
|
|
s->eof_coeffs[selir] = 1;
|
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2017-01-26 18:03:08 +02:00
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2018-12-31 19:04:59 +02:00
|
|
|
int ret, status, available, wanted;
|
2018-09-15 20:35:08 +02:00
|
|
|
AVFrame *in = NULL;
|
|
|
|
int64_t pts;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2023-04-25 15:42:44 +02:00
|
|
|
for (int i = 0; i < s->nb_irs; i++) {
|
|
|
|
const int selir = i;
|
2018-09-15 20:35:08 +02:00
|
|
|
|
2023-04-25 15:42:44 +02:00
|
|
|
if (s->ir_load && selir != s->selir)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!s->eof_coeffs[selir]) {
|
2023-05-30 14:06:40 +02:00
|
|
|
ret = check_ir(ctx->inputs[1 + selir], selir);
|
2023-04-25 15:42:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!s->eof_coeffs[selir]) {
|
|
|
|
if (ff_outlink_frame_wanted(ctx->outputs[0]))
|
|
|
|
ff_inlink_request_frame(ctx->inputs[1 + selir]);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-15 20:35:08 +02:00
|
|
|
}
|
2017-11-20 14:25:39 +02:00
|
|
|
|
2023-04-25 15:42:44 +02:00
|
|
|
if (!s->have_coeffs[selir] && s->eof_coeffs[selir]) {
|
|
|
|
ret = convert_coeffs(ctx, selir);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 19:04:59 +02:00
|
|
|
available = ff_inlink_queued_samples(ctx->inputs[0]);
|
|
|
|
wanted = FFMAX(s->min_part_size, (available / s->min_part_size) * s->min_part_size);
|
|
|
|
ret = ff_inlink_consume_samples(ctx->inputs[0], wanted, wanted, &in);
|
2018-11-08 12:14:04 +02:00
|
|
|
if (ret > 0)
|
2018-09-15 20:35:08 +02:00
|
|
|
ret = fir_frame(s, in, outlink);
|
2018-05-29 18:24:22 +02:00
|
|
|
|
2023-04-25 20:33:08 +02:00
|
|
|
if (s->selir != s->prev_selir && s->loading[0] == 0)
|
2023-04-23 23:30:42 +02:00
|
|
|
s->prev_selir = s->selir;
|
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
if (ff_inlink_queued_samples(ctx->inputs[0]) >= s->min_part_size) {
|
2018-11-08 13:46:29 +02:00
|
|
|
ff_filter_set_ready(ctx, 10);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
|
|
|
|
if (status == AVERROR_EOF) {
|
|
|
|
ff_outlink_set_status(ctx->outputs[0], status, pts);
|
|
|
|
return 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 14:06:40 +02:00
|
|
|
if (ff_outlink_frame_wanted(ctx->outputs[0])) {
|
2018-09-15 20:35:08 +02:00
|
|
|
ff_inlink_request_frame(ctx->inputs[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-11-08 13:46:29 +02:00
|
|
|
return FFERROR_NOT_READY;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
static int query_formats(const AVFilterContext *ctx,
|
|
|
|
AVFilterFormatsConfig **cfg_in,
|
|
|
|
AVFilterFormatsConfig **cfg_out)
|
2017-01-26 18:03:08 +02:00
|
|
|
{
|
2024-08-29 09:10:01 +02:00
|
|
|
const AudioFIRContext *s = ctx->priv;
|
2022-05-14 10:28:49 +02:00
|
|
|
static const enum AVSampleFormat sample_fmts[3][3] = {
|
|
|
|
{ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
|
|
|
|
{ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
|
|
|
|
{ AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
|
2017-01-26 18:03:08 +02:00
|
|
|
};
|
2018-10-04 21:10:47 +02:00
|
|
|
int ret;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
if (!s->ir_format) {
|
2018-10-04 21:10:47 +02:00
|
|
|
AVFilterChannelLayouts *mono = NULL;
|
2021-08-10 01:25:31 +02:00
|
|
|
AVFilterChannelLayouts *layouts = ff_all_channel_counts();
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &cfg_in[0]->channel_layouts)) < 0)
|
2018-10-04 21:10:47 +02:00
|
|
|
return ret;
|
2024-08-29 09:10:01 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &cfg_out[0]->channel_layouts)) < 0)
|
2018-10-04 21:10:47 +02:00
|
|
|
return ret;
|
2020-08-07 04:58:56 +02:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
ret = ff_add_channel_layout(&mono, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO);
|
2020-08-07 04:58:56 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-01-09 21:06:57 +02:00
|
|
|
for (int i = 1; i < ctx->nb_inputs; i++) {
|
2024-08-29 09:10:01 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(mono, &cfg_in[i]->channel_layouts)) < 0)
|
2020-01-09 21:06:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
if ((ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out,
|
|
|
|
sample_fmts[s->precision])) < 0)
|
2017-01-26 18:03:08 +02:00
|
|
|
return ret;
|
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
return 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2021-08-31 16:03:14 +02:00
|
|
|
int ret;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
s->one2many = ctx->inputs[1 + s->selir]->ch_layout.nb_channels == 1;
|
2017-01-26 18:03:08 +02:00
|
|
|
outlink->sample_rate = ctx->inputs[0]->sample_rate;
|
|
|
|
outlink->time_base = ctx->inputs[0]->time_base;
|
2021-08-31 16:03:14 +02:00
|
|
|
if ((ret = av_channel_layout_copy(&outlink->ch_layout, &ctx->inputs[0]->ch_layout)) < 0)
|
|
|
|
return ret;
|
|
|
|
outlink->ch_layout.nb_channels = ctx->inputs[0]->ch_layout.nb_channels;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2022-05-14 10:28:49 +02:00
|
|
|
s->format = outlink->format;
|
2022-12-13 12:46:02 +02:00
|
|
|
s->nb_channels = outlink->ch_layout.nb_channels;
|
2023-11-18 01:36:18 +02:00
|
|
|
s->ch_gain = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*s->ch_gain));
|
2023-04-23 23:30:42 +02:00
|
|
|
s->loading = av_calloc(ctx->inputs[0]->ch_layout.nb_channels, sizeof(*s->loading));
|
2023-11-18 01:36:18 +02:00
|
|
|
if (!s->loading || !s->ch_gain)
|
2023-04-23 23:30:42 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2023-04-25 15:21:51 +02:00
|
|
|
s->fadein[0] = ff_get_audio_buffer(outlink, s->min_part_size);
|
|
|
|
s->fadein[1] = ff_get_audio_buffer(outlink, s->min_part_size);
|
|
|
|
if (!s->fadein[0] || !s->fadein[1])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2023-04-24 17:31:18 +02:00
|
|
|
s->xfade[0] = ff_get_audio_buffer(outlink, s->min_part_size);
|
|
|
|
s->xfade[1] = ff_get_audio_buffer(outlink, s->min_part_size);
|
|
|
|
if (!s->xfade[0] || !s->xfade[1])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
switch (s->format) {
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
float *dst0 = (float *)s->xfade[0]->extended_data[ch];
|
|
|
|
float *dst1 = (float *)s->xfade[1]->extended_data[ch];
|
|
|
|
|
|
|
|
for (int n = 0; n < s->min_part_size; n++) {
|
|
|
|
dst0[n] = (n + 1.f) / s->min_part_size;
|
|
|
|
dst1[n] = 1.f - dst0[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
double *dst0 = (double *)s->xfade[0]->extended_data[ch];
|
|
|
|
double *dst1 = (double *)s->xfade[1]->extended_data[ch];
|
|
|
|
|
|
|
|
for (int n = 0; n < s->min_part_size; n++) {
|
|
|
|
dst0[n] = (n + 1.0) / s->min_part_size;
|
|
|
|
dst1[n] = 1.0 - dst0[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +02:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2018-12-28 18:57:24 +02:00
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
av_freep(&s->fdsp);
|
2023-11-18 01:36:18 +02:00
|
|
|
av_freep(&s->ch_gain);
|
2023-04-23 23:30:42 +02:00
|
|
|
av_freep(&s->loading);
|
2020-01-09 21:06:57 +02:00
|
|
|
|
2023-01-01 00:31:31 +02:00
|
|
|
for (int i = 0; i < s->nb_irs; i++) {
|
2023-04-23 23:30:42 +02:00
|
|
|
for (int j = 0; j < s->nb_segments[i]; j++)
|
|
|
|
uninit_segment(ctx, &s->seg[i][j]);
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
av_frame_free(&s->ir[i]);
|
2023-01-01 00:31:31 +02:00
|
|
|
av_frame_free(&s->norm_ir[i]);
|
|
|
|
}
|
2020-01-09 21:06:57 +02:00
|
|
|
|
2023-04-25 15:21:51 +02:00
|
|
|
av_frame_free(&s->fadein[0]);
|
|
|
|
av_frame_free(&s->fadein[1]);
|
|
|
|
|
2023-04-24 17:31:18 +02:00
|
|
|
av_frame_free(&s->xfade[0]);
|
|
|
|
av_frame_free(&s->xfade[1]);
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2023-11-19 14:22:31 +02:00
|
|
|
AVFilterPad pad;
|
2018-09-15 20:03:40 +02:00
|
|
|
int ret;
|
2018-05-29 18:24:22 +02:00
|
|
|
|
2022-12-13 12:46:02 +02:00
|
|
|
s->prev_selir = FFMIN(s->nb_irs - 1, s->selir);
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
pad = (AVFilterPad) {
|
2020-08-22 05:20:01 +02:00
|
|
|
.name = "main",
|
2020-01-09 21:06:57 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
};
|
|
|
|
|
2021-08-16 20:48:00 +02:00
|
|
|
ret = ff_append_inpad(ctx, &pad);
|
2020-08-22 05:20:01 +02:00
|
|
|
if (ret < 0)
|
2020-01-09 21:06:57 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (int n = 0; n < s->nb_irs; n++) {
|
|
|
|
pad = (AVFilterPad) {
|
|
|
|
.name = av_asprintf("ir%d", n),
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!pad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-08-16 21:39:50 +02:00
|
|
|
ret = ff_append_inpad_free_name(ctx, &pad);
|
|
|
|
if (ret < 0)
|
2020-01-09 21:06:57 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
s->fdsp = avpriv_float_dsp_alloc(0);
|
|
|
|
if (!s->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2019-01-03 01:54:18 +02:00
|
|
|
ff_afir_init(&s->afirdsp);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2023-04-24 17:31:18 +02:00
|
|
|
s->min_part_size = 1 << av_log2(s->minp);
|
2023-04-25 15:21:51 +02:00
|
|
|
s->max_part_size = 1 << av_log2(s->maxp);
|
2023-04-24 17:31:18 +02:00
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
static int process_command(AVFilterContext *ctx,
|
|
|
|
const char *cmd,
|
|
|
|
const char *arg,
|
|
|
|
char *res,
|
|
|
|
int res_len,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2022-12-25 19:07:47 +02:00
|
|
|
int prev_selir, ret;
|
2020-01-09 21:06:57 +02:00
|
|
|
|
2022-12-25 19:07:47 +02:00
|
|
|
prev_selir = s->selir;
|
2022-12-13 12:46:02 +02:00
|
|
|
ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
|
2020-01-09 21:06:57 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
s->selir = FFMIN(s->nb_irs - 1, s->selir);
|
2023-04-25 20:33:08 +02:00
|
|
|
if (s->selir != prev_selir) {
|
2022-12-25 19:07:47 +02:00
|
|
|
s->prev_selir = prev_selir;
|
2020-01-09 21:06:57 +02:00
|
|
|
|
2023-04-25 20:33:08 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++)
|
|
|
|
s->loading[ch] = 1;
|
|
|
|
}
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
|
|
|
#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2020-01-09 21:06:57 +02:00
|
|
|
#define AFR AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2018-05-29 18:24:22 +02:00
|
|
|
#define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2017-01-26 18:03:08 +02:00
|
|
|
#define OFFSET(x) offsetof(AudioFIRContext, x)
|
|
|
|
|
|
|
|
static const AVOption afir_options[] = {
|
2022-12-22 18:04:29 +02:00
|
|
|
{ "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, AFR },
|
|
|
|
{ "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, AFR },
|
2018-04-16 19:06:09 +02:00
|
|
|
{ "length", "set IR length", OFFSET(length), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "gtype", "set IR auto gain type",OFFSET(gtype), AV_OPT_TYPE_INT, {.i64=0}, -1, 4, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
|
|
|
{ "none", "without auto gain", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
|
|
|
{ "peak", "peak gain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
|
|
|
{ "dc", "DC gain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
|
|
|
{ "gn", "gain to noise", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
|
|
|
{ "ac", "AC gain", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
|
|
|
{ "rms", "RMS gain", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF|AV_OPT_FLAG_DEPRECATED, .unit = "gtype" },
|
2023-11-18 01:36:18 +02:00
|
|
|
{ "irnorm", "set IR norm", OFFSET(ir_norm), AV_OPT_TYPE_FLOAT, {.dbl=1}, -1, 2, AF },
|
|
|
|
{ "irlink", "set IR link", OFFSET(ir_link), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
|
2018-10-03 21:43:40 +02:00
|
|
|
{ "irgain", "set IR gain", OFFSET(ir_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "irfmt", "set IR format", OFFSET(ir_format), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, .unit = "irfmt" },
|
|
|
|
{ "mono", "single channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "irfmt" },
|
|
|
|
{ "input", "same as input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "irfmt" },
|
2018-05-29 18:24:22 +02:00
|
|
|
{ "maxir", "set max IR length", OFFSET(max_ir_len), AV_OPT_TYPE_FLOAT, {.dbl=30}, 0.1, 60, AF },
|
2023-11-19 14:22:31 +02:00
|
|
|
{ "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF|AV_OPT_FLAG_DEPRECATED },
|
|
|
|
{ "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF|AV_OPT_FLAG_DEPRECATED },
|
|
|
|
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF|AV_OPT_FLAG_DEPRECATED },
|
|
|
|
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF|AV_OPT_FLAG_DEPRECATED },
|
2022-12-25 11:21:47 +02:00
|
|
|
{ "minp", "set min partition size", OFFSET(minp), AV_OPT_TYPE_INT, {.i64=8192}, 1, 65536, AF },
|
|
|
|
{ "maxp", "set max partition size", OFFSET(maxp), AV_OPT_TYPE_INT, {.i64=8192}, 8, 65536, AF },
|
2020-01-09 21:06:57 +02:00
|
|
|
{ "nbirs", "set number of input IRs",OFFSET(nb_irs),AV_OPT_TYPE_INT, {.i64=1}, 1, 32, AF },
|
|
|
|
{ "ir", "select IR", OFFSET(selir), AV_OPT_TYPE_INT, {.i64=0}, 0, 31, AFR },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, .unit = "precision" },
|
|
|
|
{ "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision" },
|
|
|
|
{ "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision" },
|
|
|
|
{ "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision" },
|
|
|
|
{ "irload", "set IR loading type", OFFSET(ir_load), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AF, .unit = "irload" },
|
|
|
|
{ "init", "load all IRs on init", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "irload" },
|
|
|
|
{ "access", "load IR on access", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "irload" },
|
2017-01-26 18:03:08 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(afir);
|
|
|
|
|
2023-11-20 00:53:02 +02:00
|
|
|
static const AVFilterPad outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_afir = {
|
2017-01-26 18:03:08 +02:00
|
|
|
.name = "afir",
|
2020-01-09 21:06:57 +02:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply Finite Impulse Response filter with supplied coefficients in additional stream(s)."),
|
2017-01-26 18:03:08 +02:00
|
|
|
.priv_size = sizeof(AudioFIRContext),
|
|
|
|
.priv_class = &afir_class,
|
2024-08-29 09:10:01 +02:00
|
|
|
FILTER_QUERY_FUNC2(query_formats),
|
2023-11-20 00:53:02 +02:00
|
|
|
FILTER_OUTPUTS(outputs),
|
2017-01-26 18:03:08 +02:00
|
|
|
.init = init,
|
2018-09-15 20:35:08 +02:00
|
|
|
.activate = activate,
|
2017-01-26 18:03:08 +02:00
|
|
|
.uninit = uninit,
|
2020-01-09 21:06:57 +02:00
|
|
|
.process_command = process_command,
|
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS |
|
2023-11-19 22:26:00 +02:00
|
|
|
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
|
2018-05-29 18:24:22 +02:00
|
|
|
AVFILTER_FLAG_SLICE_THREADS,
|
2017-01-26 18:03:08 +02:00
|
|
|
};
|