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>
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/float_dsp.h"
|
2018-05-29 18:24:22 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
#include "libavutil/opt.h"
|
2018-05-29 18:24:22 +02:00
|
|
|
#include "libavutil/xga_font_data.h"
|
2017-01-26 18:03:08 +02:00
|
|
|
#include "libavcodec/avfft.h"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "af_afir.h"
|
|
|
|
|
|
|
|
static void fcmul_add_c(float *sum, const float *t, const float *c, ptrdiff_t len)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < len; n++) {
|
|
|
|
const float cre = c[2 * n ];
|
|
|
|
const float cim = c[2 * n + 1];
|
|
|
|
const float tre = t[2 * n ];
|
|
|
|
const float tim = t[2 * n + 1];
|
|
|
|
|
|
|
|
sum[2 * n ] += tre * cre - tim * cim;
|
|
|
|
sum[2 * n + 1] += tre * cim + tim * cre;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum[2 * n] += t[2 * n] * c[2 * n];
|
|
|
|
}
|
|
|
|
|
2020-01-09 19:32:11 +02:00
|
|
|
static void direct(const float *in, const FFTComplex *ir, int len, float *out)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < len; n++)
|
|
|
|
for (int m = 0; m <= n; m++)
|
|
|
|
out[n] += ir[m].re * in[n - m];
|
|
|
|
}
|
|
|
|
|
2018-12-31 19:04:59 +02:00
|
|
|
static int fir_quantum(AVFilterContext *ctx, AVFrame *out, int ch, int offset)
|
2017-01-26 18:03:08 +02:00
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2020-01-08 20:23:45 +02:00
|
|
|
const float *in = (const float *)s->in->extended_data[ch] + offset;
|
2018-12-31 19:04:59 +02:00
|
|
|
float *block, *buf, *ptr = (float *)out->extended_data[ch] + offset;
|
|
|
|
const int nb_samples = FFMIN(s->min_part_size, out->nb_samples - offset);
|
2017-01-26 18:03:08 +02:00
|
|
|
int n, i, j;
|
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
for (int segment = 0; segment < s->nb_segments; segment++) {
|
|
|
|
AudioFIRSegment *seg = &s->seg[segment];
|
|
|
|
float *src = (float *)seg->input->extended_data[ch];
|
|
|
|
float *dst = (float *)seg->output->extended_data[ch];
|
|
|
|
float *sum = (float *)seg->sum->extended_data[ch];
|
|
|
|
|
2020-01-09 19:32:11 +02:00
|
|
|
if (s->min_part_size >= 8) {
|
|
|
|
s->fdsp->vector_fmul_scalar(src + seg->input_offset, in, s->dry_gain, FFALIGN(nb_samples, 4));
|
|
|
|
emms_c();
|
|
|
|
} else {
|
|
|
|
for (n = 0; n < nb_samples; n++)
|
|
|
|
src[seg->input_offset + n] = in[n] * s->dry_gain;
|
|
|
|
}
|
2018-12-29 12:40:13 +02:00
|
|
|
|
|
|
|
seg->output_offset[ch] += s->min_part_size;
|
|
|
|
if (seg->output_offset[ch] == seg->part_size) {
|
|
|
|
seg->output_offset[ch] = 0;
|
|
|
|
} else {
|
|
|
|
memmove(src, src + s->min_part_size, (seg->input_size - s->min_part_size) * sizeof(*src));
|
|
|
|
|
|
|
|
dst += seg->output_offset[ch];
|
2018-12-31 19:04:59 +02:00
|
|
|
for (n = 0; n < nb_samples; n++) {
|
2018-12-29 12:40:13 +02:00
|
|
|
ptr[n] += dst[n];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-09 19:32:11 +02:00
|
|
|
if (seg->part_size < 8) {
|
|
|
|
memset(dst, 0, sizeof(*dst) * seg->part_size * seg->nb_partitions);
|
|
|
|
|
|
|
|
j = seg->part_index[ch];
|
|
|
|
|
|
|
|
for (i = 0; i < seg->nb_partitions; i++) {
|
|
|
|
const int coffset = j * seg->coeff_size;
|
|
|
|
const FFTComplex *coeff = (const FFTComplex *)seg->coeff->extended_data[ch * !s->one2many] + coffset;
|
|
|
|
|
|
|
|
direct(src, coeff, nb_samples, dst);
|
|
|
|
|
|
|
|
if (j == 0)
|
|
|
|
j = seg->nb_partitions;
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
|
|
|
|
seg->part_index[ch] = (seg->part_index[ch] + 1) % seg->nb_partitions;
|
|
|
|
|
|
|
|
memmove(src, src + s->min_part_size, (seg->input_size - s->min_part_size) * sizeof(*src));
|
|
|
|
|
|
|
|
for (n = 0; n < nb_samples; n++) {
|
|
|
|
ptr[n] += dst[n];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
memset(sum, 0, sizeof(*sum) * seg->fft_length);
|
|
|
|
block = (float *)seg->block->extended_data[ch] + seg->part_index[ch] * seg->block_size;
|
|
|
|
memset(block + seg->part_size, 0, sizeof(*block) * (seg->fft_length - seg->part_size));
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
memcpy(block, src, sizeof(*src) * seg->part_size);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
av_rdft_calc(seg->rdft[ch], block);
|
|
|
|
block[2 * seg->part_size] = block[1];
|
|
|
|
block[1] = 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
j = seg->part_index[ch];
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
for (i = 0; i < seg->nb_partitions; i++) {
|
|
|
|
const int coffset = j * seg->coeff_size;
|
|
|
|
const float *block = (const float *)seg->block->extended_data[ch] + i * seg->block_size;
|
|
|
|
const FFTComplex *coeff = (const FFTComplex *)seg->coeff->extended_data[ch * !s->one2many] + coffset;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2019-01-03 01:54:18 +02:00
|
|
|
s->afirdsp.fcmul_add(sum, block, (const float *)coeff, seg->part_size);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
if (j == 0)
|
|
|
|
j = seg->nb_partitions;
|
|
|
|
j--;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
sum[1] = sum[2 * seg->part_size];
|
|
|
|
av_rdft_calc(seg->irdft[ch], sum);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
buf = (float *)seg->buffer->extended_data[ch];
|
|
|
|
for (n = 0; n < seg->part_size; n++) {
|
|
|
|
buf[n] += sum[n];
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-30 18:23:53 +02:00
|
|
|
memcpy(dst, buf, seg->part_size * sizeof(*dst));
|
2018-12-29 12:40:13 +02:00
|
|
|
|
|
|
|
buf = (float *)seg->buffer->extended_data[ch];
|
|
|
|
memcpy(buf, sum + seg->part_size, seg->part_size * sizeof(*buf));
|
|
|
|
|
|
|
|
seg->part_index[ch] = (seg->part_index[ch] + 1) % seg->nb_partitions;
|
|
|
|
|
|
|
|
memmove(src, src + s->min_part_size, (seg->input_size - s->min_part_size) * sizeof(*src));
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-31 19:04:59 +02:00
|
|
|
for (n = 0; n < nb_samples; n++) {
|
2018-12-29 12:40:13 +02:00
|
|
|
ptr[n] += dst[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 19:32:11 +02:00
|
|
|
if (s->min_part_size >= 8) {
|
|
|
|
s->fdsp->vector_fmul_scalar(ptr, ptr, s->wet_gain, FFALIGN(nb_samples, 4));
|
|
|
|
emms_c();
|
|
|
|
} else {
|
|
|
|
for (n = 0; n < nb_samples; n++)
|
|
|
|
ptr[n] *= s->wet_gain;
|
|
|
|
}
|
2018-12-29 11:46:25 +02:00
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-31 19:04:59 +02:00
|
|
|
static int fir_channel(AVFilterContext *ctx, AVFrame *out, int ch)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
|
|
|
|
|
|
|
for (int offset = 0; offset < out->nb_samples; offset += s->min_part_size) {
|
|
|
|
fir_quantum(ctx, out, ch, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
const int start = (out->channels * jobnr) / nb_jobs;
|
|
|
|
const int end = (out->channels * (jobnr+1)) / nb_jobs;
|
|
|
|
|
|
|
|
for (int ch = start; ch < end; ch++) {
|
|
|
|
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;
|
|
|
|
AVFrame *out = NULL;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
if (s->pts == AV_NOPTS_VALUE)
|
|
|
|
s->pts = in->pts;
|
2020-01-08 20:23:45 +02:00
|
|
|
s->in = in;
|
2018-12-30 18:40:31 +02:00
|
|
|
ctx->internal->execute(ctx, fir_channels, out, NULL, FFMIN(outlink->channels,
|
|
|
|
ff_filter_get_nb_threads(ctx)));
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-11-09 11:40:16 +02:00
|
|
|
out->pts = s->pts;
|
|
|
|
if (s->pts != AV_NOPTS_VALUE)
|
|
|
|
s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
|
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
|
|
|
}
|
|
|
|
|
2018-05-29 18:24:22 +02:00
|
|
|
static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
|
|
|
|
{
|
|
|
|
const uint8_t *font;
|
|
|
|
int font_height;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
font = avpriv_cga_font, font_height = 8;
|
|
|
|
|
|
|
|
for (i = 0; txt[i]; i++) {
|
|
|
|
int char_y, mask;
|
|
|
|
|
|
|
|
uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
|
|
|
|
for (char_y = 0; char_y < font_height; char_y++) {
|
|
|
|
for (mask = 0x80; mask; mask >>= 1) {
|
|
|
|
if (font[txt[i] * font_height + char_y] & mask)
|
|
|
|
AV_WL32(p, color);
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
p += pic->linesize[0] - 8 * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
|
|
|
|
{
|
2018-07-19 02:28:25 +02:00
|
|
|
int dx = FFABS(x1-x0);
|
2018-05-29 18:24:22 +02:00
|
|
|
int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
|
|
|
|
int err = (dx>dy ? dx : -dy) / 2, e2;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
|
|
|
|
|
|
|
|
if (x0 == x1 && y0 == y1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
e2 = err;
|
|
|
|
|
|
|
|
if (e2 >-dx) {
|
|
|
|
err -= dy;
|
2018-07-19 02:28:25 +02:00
|
|
|
x0--;
|
2018-05-29 18:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (e2 < dy) {
|
|
|
|
err += dx;
|
|
|
|
y0 += sy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_response(AVFilterContext *ctx, AVFrame *out)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2018-10-20 22:38:52 +02:00
|
|
|
float *mag, *phase, *delay, min = FLT_MAX, max = FLT_MIN;
|
|
|
|
float min_delay = FLT_MAX, max_delay = FLT_MIN;
|
|
|
|
int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
|
2018-05-29 18:24:22 +02:00
|
|
|
char text[32];
|
|
|
|
int channel, i, x;
|
|
|
|
|
|
|
|
memset(out->data[0], 0, s->h * out->linesize[0]);
|
|
|
|
|
|
|
|
phase = av_malloc_array(s->w, sizeof(*phase));
|
|
|
|
mag = av_malloc_array(s->w, sizeof(*mag));
|
2018-10-20 22:38:52 +02:00
|
|
|
delay = av_malloc_array(s->w, sizeof(*delay));
|
|
|
|
if (!mag || !phase || !delay)
|
2018-05-29 18:24:22 +02:00
|
|
|
goto end;
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
channel = av_clip(s->ir_channel, 0, s->ir[s->selir]->channels - 1);
|
2018-05-29 18:24:22 +02:00
|
|
|
for (i = 0; i < s->w; i++) {
|
2020-01-09 21:06:57 +02:00
|
|
|
const float *src = (const float *)s->ir[s->selir]->extended_data[channel];
|
2018-05-29 18:24:22 +02:00
|
|
|
double w = i * M_PI / (s->w - 1);
|
2018-10-20 22:38:52 +02:00
|
|
|
double div, real_num = 0., imag_num = 0., real = 0., imag = 0.;
|
2018-05-29 18:24:22 +02:00
|
|
|
|
|
|
|
for (x = 0; x < s->nb_taps; x++) {
|
|
|
|
real += cos(-x * w) * src[x];
|
|
|
|
imag += sin(-x * w) * src[x];
|
2018-10-20 22:38:52 +02:00
|
|
|
real_num += cos(-x * w) * src[x] * x;
|
|
|
|
imag_num += sin(-x * w) * src[x] * x;
|
2018-05-29 18:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mag[i] = hypot(real, imag);
|
|
|
|
phase[i] = atan2(imag, real);
|
2018-10-20 22:38:52 +02:00
|
|
|
div = real * real + imag * imag;
|
|
|
|
delay[i] = (real_num * real + imag_num * imag) / div;
|
2018-05-29 18:24:22 +02:00
|
|
|
min = fminf(min, mag[i]);
|
|
|
|
max = fmaxf(max, mag[i]);
|
2018-10-20 22:38:52 +02:00
|
|
|
min_delay = fminf(min_delay, delay[i]);
|
|
|
|
max_delay = fmaxf(max_delay, delay[i]);
|
2018-05-29 18:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < s->w; i++) {
|
|
|
|
int ymag = mag[i] / max * (s->h - 1);
|
2018-10-20 22:38:52 +02:00
|
|
|
int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
|
2018-05-29 18:24:22 +02:00
|
|
|
int yphase = (0.5 * (1. + phase[i] / M_PI)) * (s->h - 1);
|
|
|
|
|
|
|
|
ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
|
|
|
|
yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
|
2018-10-20 22:38:52 +02:00
|
|
|
ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
|
2018-05-29 18:24:22 +02:00
|
|
|
|
|
|
|
if (prev_ymag < 0)
|
|
|
|
prev_ymag = ymag;
|
|
|
|
if (prev_yphase < 0)
|
|
|
|
prev_yphase = yphase;
|
2018-10-20 22:38:52 +02:00
|
|
|
if (prev_ydelay < 0)
|
|
|
|
prev_ydelay = ydelay;
|
2018-05-29 18:24:22 +02:00
|
|
|
|
|
|
|
draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
|
|
|
|
draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
|
2018-10-20 22:38:52 +02:00
|
|
|
draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
|
2018-05-29 18:24:22 +02:00
|
|
|
|
|
|
|
prev_ymag = ymag;
|
|
|
|
prev_yphase = yphase;
|
2018-10-20 22:38:52 +02:00
|
|
|
prev_ydelay = ydelay;
|
2018-05-29 18:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->w > 400 && s->h > 100) {
|
|
|
|
drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
|
|
|
|
snprintf(text, sizeof(text), "%.2f", max);
|
|
|
|
drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
|
|
|
|
|
|
|
|
drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
|
|
|
|
snprintf(text, sizeof(text), "%.2f", min);
|
|
|
|
drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
|
2018-10-20 22:38:52 +02:00
|
|
|
|
|
|
|
drawtext(out, 2, 22, "Max Delay:", 0xDDDDDDDD);
|
|
|
|
snprintf(text, sizeof(text), "%.2f", max_delay);
|
|
|
|
drawtext(out, 11 * 8 + 2, 22, text, 0xDDDDDDDD);
|
|
|
|
|
|
|
|
drawtext(out, 2, 32, "Min Delay:", 0xDDDDDDDD);
|
|
|
|
snprintf(text, sizeof(text), "%.2f", min_delay);
|
|
|
|
drawtext(out, 11 * 8 + 2, 32, text, 0xDDDDDDDD);
|
2018-05-29 18:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2018-10-20 22:38:52 +02:00
|
|
|
av_free(delay);
|
2018-05-29 18:24:22 +02:00
|
|
|
av_free(phase);
|
|
|
|
av_free(mag);
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:40:13 +02:00
|
|
|
static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg,
|
|
|
|
int offset, int nb_partitions, int part_size)
|
2018-12-28 19:31:36 +02:00
|
|
|
{
|
2018-12-29 12:40:13 +02:00
|
|
|
AudioFIRContext *s = ctx->priv;
|
|
|
|
|
2018-12-28 19:31:36 +02:00
|
|
|
seg->rdft = av_calloc(ctx->inputs[0]->channels, sizeof(*seg->rdft));
|
|
|
|
seg->irdft = av_calloc(ctx->inputs[0]->channels, sizeof(*seg->irdft));
|
2018-12-29 11:17:39 +02:00
|
|
|
if (!seg->rdft || !seg->irdft)
|
2018-12-28 19:31:36 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2018-12-30 23:08:11 +02:00
|
|
|
seg->fft_length = part_size * 2 + 1;
|
2018-12-29 12:40:13 +02:00
|
|
|
seg->part_size = part_size;
|
|
|
|
seg->block_size = FFALIGN(seg->fft_length, 32);
|
|
|
|
seg->coeff_size = FFALIGN(seg->part_size + 1, 32);
|
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;
|
|
|
|
|
|
|
|
seg->part_index = av_calloc(ctx->inputs[0]->channels, sizeof(*seg->part_index));
|
|
|
|
seg->output_offset = av_calloc(ctx->inputs[0]->channels, sizeof(*seg->output_offset));
|
|
|
|
if (!seg->part_index || !seg->output_offset)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-12-28 19:31:36 +02:00
|
|
|
|
2020-01-09 19:32:11 +02:00
|
|
|
for (int ch = 0; ch < ctx->inputs[0]->channels && part_size >= 8; ch++) {
|
2018-12-28 19:31:36 +02:00
|
|
|
seg->rdft[ch] = av_rdft_init(av_log2(2 * part_size), DFT_R2C);
|
|
|
|
seg->irdft[ch] = av_rdft_init(av_log2(2 * part_size), IDFT_C2R);
|
|
|
|
if (!seg->rdft[ch] || !seg->irdft[ch])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
seg->sum = ff_get_audio_buffer(ctx->inputs[0], seg->fft_length);
|
|
|
|
seg->block = ff_get_audio_buffer(ctx->inputs[0], seg->nb_partitions * seg->block_size);
|
|
|
|
seg->buffer = ff_get_audio_buffer(ctx->inputs[0], seg->part_size);
|
2020-01-09 21:06:57 +02:00
|
|
|
seg->coeff = ff_get_audio_buffer(ctx->inputs[1 + s->selir], seg->nb_partitions * seg->coeff_size * 2);
|
2018-12-29 12:40:13 +02:00
|
|
|
seg->input = ff_get_audio_buffer(ctx->inputs[0], seg->input_size);
|
|
|
|
seg->output = ff_get_audio_buffer(ctx->inputs[0], seg->part_size);
|
|
|
|
if (!seg->buffer || !seg->sum || !seg->block || !seg->coeff || !seg->input || !seg->output)
|
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;
|
|
|
|
|
|
|
|
if (seg->rdft) {
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
av_rdft_end(seg->rdft[ch]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_freep(&seg->rdft);
|
|
|
|
|
|
|
|
if (seg->irdft) {
|
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
av_rdft_end(seg->irdft[ch]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_freep(&seg->irdft);
|
|
|
|
|
|
|
|
av_freep(&seg->output_offset);
|
|
|
|
av_freep(&seg->part_index);
|
|
|
|
|
|
|
|
av_frame_free(&seg->block);
|
|
|
|
av_frame_free(&seg->sum);
|
|
|
|
av_frame_free(&seg->buffer);
|
|
|
|
av_frame_free(&seg->coeff);
|
|
|
|
av_frame_free(&seg->input);
|
|
|
|
av_frame_free(&seg->output);
|
|
|
|
seg->input_size = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
static int convert_coeffs(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2020-01-09 21:06:57 +02:00
|
|
|
int ret, i, ch, n, cur_nb_taps;
|
2018-10-03 21:43:40 +02:00
|
|
|
float power = 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
if (!s->nb_taps) {
|
|
|
|
int part_size, max_part_size;
|
|
|
|
int left, offset = 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
s->nb_taps = ff_inlink_queued_samples(ctx->inputs[1 + s->selir]);
|
|
|
|
if (s->nb_taps <= 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
if (s->minp > s->maxp) {
|
|
|
|
s->maxp = s->minp;
|
|
|
|
}
|
|
|
|
|
|
|
|
left = s->nb_taps;
|
|
|
|
part_size = 1 << av_log2(s->minp);
|
|
|
|
max_part_size = 1 << av_log2(s->maxp);
|
2018-12-28 19:07:13 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
s->min_part_size = part_size;
|
2018-12-29 12:40:13 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
for (i = 0; left > 0; i++) {
|
|
|
|
int step = part_size == max_part_size ? INT_MAX : 1 + (i == 0);
|
|
|
|
int nb_partitions = FFMIN(step, (left + part_size - 1) / part_size);
|
2018-12-29 12:40:13 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
s->nb_segments = i + 1;
|
|
|
|
ret = init_segment(ctx, &s->seg[i], offset, nb_partitions, part_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
offset += nb_partitions * part_size;
|
|
|
|
left -= nb_partitions * part_size;
|
|
|
|
part_size *= 2;
|
|
|
|
part_size = FFMIN(part_size, max_part_size);
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 12:40:13 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
if (!s->ir[s->selir]) {
|
|
|
|
ret = ff_inlink_consume_samples(ctx->inputs[1 + s->selir], s->nb_taps, s->nb_taps, &s->ir[s->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
|
|
|
|
2018-05-29 18:24:22 +02:00
|
|
|
if (s->response)
|
|
|
|
draw_response(ctx, s->video);
|
|
|
|
|
2018-10-03 21:43:40 +02:00
|
|
|
s->gain = 1;
|
2020-01-09 21:06:57 +02:00
|
|
|
cur_nb_taps = s->ir[s->selir]->nb_samples;
|
2018-10-03 21:43:40 +02:00
|
|
|
|
2018-10-10 19:55:30 +02:00
|
|
|
switch (s->gtype) {
|
|
|
|
case -1:
|
2018-10-20 22:38:52 +02:00
|
|
|
/* nothing to do */
|
2018-10-10 19:55:30 +02:00
|
|
|
break;
|
|
|
|
case 0:
|
2020-01-09 21:06:57 +02:00
|
|
|
for (ch = 0; ch < ctx->inputs[1 + s->selir]->channels; ch++) {
|
|
|
|
float *time = (float *)s->ir[s->selir]->extended_data[!s->one2many * ch];
|
2018-10-10 19:55:30 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
for (i = 0; i < cur_nb_taps; i++)
|
2018-10-10 19:55:30 +02:00
|
|
|
power += FFABS(time[i]);
|
|
|
|
}
|
2020-01-09 21:06:57 +02:00
|
|
|
s->gain = ctx->inputs[1 + s->selir]->channels / power;
|
2018-10-10 19:55:30 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2020-01-09 21:06:57 +02:00
|
|
|
for (ch = 0; ch < ctx->inputs[1 + s->selir]->channels; ch++) {
|
|
|
|
float *time = (float *)s->ir[s->selir]->extended_data[!s->one2many * ch];
|
2018-10-10 19:55:30 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
for (i = 0; i < cur_nb_taps; i++)
|
2018-10-10 19:55:30 +02:00
|
|
|
power += time[i];
|
2017-12-31 20:09:07 +02:00
|
|
|
}
|
2020-01-09 21:06:57 +02:00
|
|
|
s->gain = ctx->inputs[1 + s->selir]->channels / power;
|
2018-10-10 19:55:30 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2020-01-09 21:06:57 +02:00
|
|
|
for (ch = 0; ch < ctx->inputs[1 + s->selir]->channels; ch++) {
|
|
|
|
float *time = (float *)s->ir[s->selir]->extended_data[!s->one2many * ch];
|
2018-10-10 19:55:30 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
for (i = 0; i < cur_nb_taps; i++)
|
2018-10-10 19:55:30 +02:00
|
|
|
power += time[i] * time[i];
|
|
|
|
}
|
|
|
|
s->gain = sqrtf(ch / power);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR_BUG;
|
2018-10-03 21:43:40 +02:00
|
|
|
}
|
2017-12-31 20:09:07 +02:00
|
|
|
|
2018-10-03 21:43:40 +02:00
|
|
|
s->gain = FFMIN(s->gain * s->ir_gain, 1.f);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "power %f, gain %f\n", power, s->gain);
|
2020-01-09 21:06:57 +02:00
|
|
|
for (ch = 0; ch < ctx->inputs[1 + s->selir]->channels; ch++) {
|
|
|
|
float *time = (float *)s->ir[s->selir]->extended_data[!s->one2many * ch];
|
2017-12-31 20:09:07 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
s->fdsp->vector_fmul_scalar(time, time, s->gain, FFALIGN(cur_nb_taps, 4));
|
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);
|
2018-12-29 11:39:19 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "nb_segments: %d\n", s->nb_segments);
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
for (ch = 0; ch < ctx->inputs[1 + s->selir]->channels; ch++) {
|
|
|
|
float *time = (float *)s->ir[s->selir]->extended_data[!s->one2many * ch];
|
2018-12-29 11:57:58 +02:00
|
|
|
int toffset = 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
|
|
|
for (i = FFMAX(1, s->length * s->nb_taps); i < s->nb_taps; i++)
|
|
|
|
time[i] = 0;
|
|
|
|
|
2018-12-29 11:39:19 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
|
|
|
|
|
|
|
|
for (int segment = 0; segment < s->nb_segments; segment++) {
|
|
|
|
AudioFIRSegment *seg = &s->seg[segment];
|
|
|
|
float *block = (float *)seg->block->extended_data[ch];
|
|
|
|
FFTComplex *coeff = (FFTComplex *)seg->coeff->extended_data[ch];
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "segment: %d\n", segment);
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 11:39:19 +02:00
|
|
|
for (i = 0; i < seg->nb_partitions; i++) {
|
|
|
|
const float scale = 1.f / seg->part_size;
|
|
|
|
const int coffset = i * seg->coeff_size;
|
2018-12-29 11:57:58 +02:00
|
|
|
const int remaining = s->nb_taps - toffset;
|
2018-12-29 11:39:19 +02:00
|
|
|
const int size = remaining >= seg->part_size ? seg->part_size : remaining;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-09 19:32:11 +02:00
|
|
|
if (size < 8) {
|
|
|
|
for (n = 0; n < size; n++)
|
|
|
|
coeff[coffset + n].re = time[toffset + n];
|
|
|
|
|
|
|
|
toffset += size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-29 11:39:19 +02:00
|
|
|
memset(block, 0, sizeof(*block) * seg->fft_length);
|
|
|
|
memcpy(block, time + toffset, size * sizeof(*block));
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-12-29 11:39:19 +02:00
|
|
|
av_rdft_calc(seg->rdft[0], block);
|
|
|
|
|
|
|
|
coeff[coffset].re = block[0] * scale;
|
|
|
|
coeff[coffset].im = 0;
|
|
|
|
for (n = 1; n < seg->part_size; n++) {
|
|
|
|
coeff[coffset + n].re = block[2 * n] * scale;
|
|
|
|
coeff[coffset + n].im = block[2 * n + 1] * scale;
|
|
|
|
}
|
|
|
|
coeff[coffset + seg->part_size].re = block[1] * scale;
|
|
|
|
coeff[coffset + seg->part_size].im = 0;
|
2018-12-29 11:57:58 +02:00
|
|
|
|
|
|
|
toffset += size;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
2018-12-29 11:39:19 +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);
|
2018-12-29 12:40:13 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "block size: %d\n", seg->block_size);
|
2018-12-29 11:39:19 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "fft_length: %d\n", seg->fft_length);
|
2018-12-29 12:40:13 +02:00
|
|
|
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);
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->have_coeffs = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:49:44 +02:00
|
|
|
static int check_ir(AVFilterLink *link, AVFrame *frame)
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (s->response)
|
|
|
|
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[1], ctx);
|
2020-01-09 21:06:57 +02:00
|
|
|
if (!s->eof_coeffs[s->selir]) {
|
2018-09-15 20:35:08 +02:00
|
|
|
AVFrame *ir = NULL;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
ret = check_ir(ctx->inputs[1 + s->selir], ir);
|
2018-09-15 20:35:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
if (ff_outlink_get_status(ctx->inputs[1 + s->selir]) == AVERROR_EOF)
|
|
|
|
s->eof_coeffs[s->selir] = 1;
|
2018-09-15 20:35:08 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
if (!s->eof_coeffs[s->selir]) {
|
2018-09-15 20:35:08 +02:00
|
|
|
if (ff_outlink_frame_wanted(ctx->outputs[0]))
|
2020-01-09 21:06:57 +02:00
|
|
|
ff_inlink_request_frame(ctx->inputs[1 + s->selir]);
|
2018-11-09 19:48:29 +02:00
|
|
|
else if (s->response && ff_outlink_frame_wanted(ctx->outputs[1]))
|
2020-01-09 21:06:57 +02:00
|
|
|
ff_inlink_request_frame(ctx->inputs[1 + s->selir]);
|
2018-09-15 20:35:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 14:25:39 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
if (!s->have_coeffs && s->eof_coeffs[s->selir]) {
|
2017-01-26 18:03:08 +02:00
|
|
|
ret = convert_coeffs(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-15 20:35:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (s->response && s->have_coeffs) {
|
2018-11-08 13:46:29 +02:00
|
|
|
int64_t old_pts = s->video->pts;
|
|
|
|
int64_t new_pts = av_rescale_q(s->pts, ctx->inputs[0]->time_base, ctx->outputs[1]->time_base);
|
|
|
|
|
|
|
|
if (ff_outlink_frame_wanted(ctx->outputs[1]) && old_pts < new_pts) {
|
2020-01-14 17:07:35 +02:00
|
|
|
AVFrame *clone;
|
2018-11-08 13:46:29 +02:00
|
|
|
s->video->pts = new_pts;
|
2020-01-14 17:07:35 +02:00
|
|
|
clone = av_frame_clone(s->video);
|
|
|
|
if (!clone)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_filter_frame(ctx->outputs[1], clone);
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (s->response)
|
|
|
|
ff_outlink_set_status(ctx->outputs[1], status, pts);
|
|
|
|
return 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:46:29 +02:00
|
|
|
if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
|
|
|
|
!ff_outlink_get_status(ctx->inputs[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
|
|
|
if (s->response &&
|
|
|
|
ff_outlink_frame_wanted(ctx->outputs[1]) &&
|
|
|
|
!ff_outlink_get_status(ctx->inputs[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-09-15 20:35:08 +02:00
|
|
|
|
2018-11-08 13:46:29 +02:00
|
|
|
return FFERROR_NOT_READY;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2018-05-29 18:24:22 +02:00
|
|
|
AudioFIRContext *s = ctx->priv;
|
2017-01-26 18:03:08 +02:00
|
|
|
AVFilterFormats *formats;
|
|
|
|
AVFilterChannelLayouts *layouts;
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
|
|
AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
2018-05-29 18:24:22 +02:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_RGB0,
|
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
2018-10-04 21:10:47 +02:00
|
|
|
int ret;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-05-29 18:24:22 +02:00
|
|
|
if (s->response) {
|
|
|
|
AVFilterLink *videolink = ctx->outputs[1];
|
|
|
|
formats = ff_make_format_list(pix_fmts);
|
|
|
|
if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
layouts = ff_all_channel_counts();
|
2018-10-04 21:10:47 +02:00
|
|
|
if (!layouts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (s->ir_format) {
|
|
|
|
ret = ff_set_common_channel_layouts(ctx, layouts);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
AVFilterChannelLayouts *mono = NULL;
|
2017-01-26 18:03:08 +02:00
|
|
|
|
2018-10-04 21:10:47 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts)) < 0)
|
|
|
|
return ret;
|
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
|
|
|
|
return ret;
|
2020-08-07 04:58:56 +02:00
|
|
|
|
|
|
|
ret = ff_add_channel_layout(&mono, AV_CH_LAYOUT_MONO);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-01-09 21:06:57 +02:00
|
|
|
for (int i = 1; i < ctx->nb_inputs; i++) {
|
|
|
|
if ((ret = ff_channel_layouts_ref(mono, &ctx->inputs[i]->out_channel_layouts)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
|
|
|
if ((ret = ff_set_common_formats(ctx, formats)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
|
|
|
return ff_set_common_samplerates(ctx, formats);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
s->one2many = ctx->inputs[1 + s->selir]->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;
|
|
|
|
outlink->channel_layout = ctx->inputs[0]->channel_layout;
|
|
|
|
outlink->channels = ctx->inputs[0]->channels;
|
|
|
|
|
|
|
|
s->nb_channels = outlink->channels;
|
2020-01-09 21:06:57 +02:00
|
|
|
s->nb_coef_channels = ctx->inputs[1 + s->selir]->channels;
|
2017-01-26 18:03:08 +02:00
|
|
|
s->pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-12-29 11:39:19 +02:00
|
|
|
for (int i = 0; i < s->nb_segments; i++) {
|
|
|
|
uninit_segment(ctx, &s->seg[i]);
|
|
|
|
}
|
2017-01-26 18:03:08 +02:00
|
|
|
|
|
|
|
av_freep(&s->fdsp);
|
2020-01-09 21:06:57 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < s->nb_irs; i++) {
|
|
|
|
av_frame_free(&s->ir[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ctx->nb_inputs; i++)
|
|
|
|
av_freep(&ctx->input_pads[i].name);
|
2018-05-29 18:24:22 +02:00
|
|
|
|
2018-09-15 20:03:40 +02:00
|
|
|
for (int i = 0; i < ctx->nb_outputs; i++)
|
|
|
|
av_freep(&ctx->output_pads[i].name);
|
2018-05-29 18:24:22 +02:00
|
|
|
av_frame_free(&s->video);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_video(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
|
|
|
|
|
|
|
outlink->sample_aspect_ratio = (AVRational){1,1};
|
|
|
|
outlink->w = s->w;
|
|
|
|
outlink->h = s->h;
|
2018-11-08 13:46:29 +02:00
|
|
|
outlink->frame_rate = s->frame_rate;
|
|
|
|
outlink->time_base = av_inv_q(outlink->frame_rate);
|
2018-05-29 18:24:22 +02:00
|
|
|
|
|
|
|
av_frame_free(&s->video);
|
|
|
|
s->video = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!s->video)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
2017-01-26 18:03:08 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 01:54:18 +02:00
|
|
|
void ff_afir_init(AudioFIRDSPContext *dsp)
|
|
|
|
{
|
|
|
|
dsp->fcmul_add = fcmul_add_c;
|
|
|
|
|
|
|
|
if (ARCH_X86)
|
|
|
|
ff_afir_init_x86(dsp);
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:03:08 +02:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioFIRContext *s = ctx->priv;
|
2018-05-29 18:24:22 +02:00
|
|
|
AVFilterPad pad, vpad;
|
2018-09-15 20:03:40 +02:00
|
|
|
int ret;
|
2018-05-29 18:24:22 +02:00
|
|
|
|
2020-01-09 21:06:57 +02:00
|
|
|
pad = (AVFilterPad) {
|
|
|
|
.name = av_strdup("main"),
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!pad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ret = ff_insert_inpad(ctx, 0, &pad);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&pad.name);
|
|
|
|
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);
|
|
|
|
|
|
|
|
ret = ff_insert_inpad(ctx, n + 1, &pad);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&pad.name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pad = (AVFilterPad) {
|
2018-05-29 18:24:22 +02:00
|
|
|
.name = av_strdup("default"),
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!pad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2020-05-25 02:48:12 +02:00
|
|
|
ret = ff_insert_outpad(ctx, 0, &pad);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&pad.name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-29 18:24:22 +02:00
|
|
|
if (s->response) {
|
|
|
|
vpad = (AVFilterPad){
|
|
|
|
.name = av_strdup("filter_response"),
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_video,
|
|
|
|
};
|
|
|
|
if (!vpad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2018-09-15 20:03:40 +02:00
|
|
|
ret = ff_insert_outpad(ctx, 1, &vpad);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&vpad.name);
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
int prev_ir = s->selir;
|
|
|
|
int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
s->selir = FFMIN(s->nb_irs - 1, s->selir);
|
|
|
|
|
|
|
|
if (prev_ir != s->selir) {
|
|
|
|
s->have_coeffs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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[] = {
|
2018-04-16 19:06:09 +02:00
|
|
|
{ "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, AF },
|
|
|
|
{ "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, AF },
|
|
|
|
{ "length", "set IR length", OFFSET(length), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
|
2018-10-10 19:55:30 +02:00
|
|
|
{ "gtype", "set IR auto gain type",OFFSET(gtype), AV_OPT_TYPE_INT, {.i64=0}, -1, 2, AF, "gtype" },
|
|
|
|
{ "none", "without auto gain", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, AF, "gtype" },
|
2018-10-03 21:43:40 +02:00
|
|
|
{ "peak", "peak gain", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "gtype" },
|
|
|
|
{ "dc", "DC gain", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "gtype" },
|
|
|
|
{ "gn", "gain to noise", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "gtype" },
|
|
|
|
{ "irgain", "set IR gain", OFFSET(ir_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
|
2018-10-04 21:10:47 +02:00
|
|
|
{ "irfmt", "set IR format", OFFSET(ir_format), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "irfmt" },
|
|
|
|
{ "mono", "single channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "irfmt" },
|
|
|
|
{ "input", "same as input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "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 },
|
|
|
|
{ "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
|
|
|
|
{ "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
|
|
|
|
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
|
2018-11-08 13:46:29 +02:00
|
|
|
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
|
2020-01-09 19:32:11 +02:00
|
|
|
{ "minp", "set min partition size", OFFSET(minp), AV_OPT_TYPE_INT, {.i64=8192}, 1, 32768, AF },
|
2019-01-05 10:38:27 +02:00
|
|
|
{ "maxp", "set max partition size", OFFSET(maxp), AV_OPT_TYPE_INT, {.i64=8192}, 8, 32768, 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 },
|
2017-01-26 18:03:08 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(afir);
|
|
|
|
|
|
|
|
AVFilter ff_af_afir = {
|
|
|
|
.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,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.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 |
|
|
|
|
AVFILTER_FLAG_DYNAMIC_OUTPUTS |
|
2018-05-29 18:24:22 +02:00
|
|
|
AVFILTER_FLAG_SLICE_THREADS,
|
2017-01-26 18:03:08 +02:00
|
|
|
};
|