2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/tx.h"
33 #define SAMPLE_FORMAT float
36 #define ctype AVComplexFloat
38 #define TX_TYPE AV_TX_FLOAT_RDFT
40 #define SAMPLE_FORMAT double
43 #define ctype AVComplexDouble
45 #define TX_TYPE AV_TX_DOUBLE_RDFT
48 #define fn3(a,b) a##_##b
49 #define fn2(a,b) fn3(a,b)
50 #define fn(a) fn2(a, SAMPLE_FORMAT)
52 static void fn(draw_response)(AVFilterContext *ctx, AVFrame *out)
54 AudioFIRContext *s = ctx->priv;
55 ftype *mag, *phase, *delay, min = FLT_MAX, max = FLT_MIN;
56 ftype min_delay = FLT_MAX, max_delay = FLT_MIN;
57 int prev_ymag = -1, prev_yphase = -1, prev_ydelay = -1;
61 for (int y = 0; y < s->h; y++)
62 memset(out->data[0] + y * out->linesize[0], 0, s->w * 4);
64 phase = av_malloc_array(s->w, sizeof(*phase));
65 mag = av_malloc_array(s->w, sizeof(*mag));
66 delay = av_malloc_array(s->w, sizeof(*delay));
67 if (!mag || !phase || !delay)
70 channel = av_clip(s->ir_channel, 0, s->ir[s->selir]->ch_layout.nb_channels - 1);
71 for (i = 0; i < s->w; i++) {
72 const ftype *src = (const ftype *)s->ir[s->selir]->extended_data[channel];
73 double w = i * M_PI / (s->w - 1);
74 double div, real_num = 0., imag_num = 0., real = 0., imag = 0.;
76 for (x = 0; x < s->nb_taps[s->selir]; x++) {
77 real += cos(-x * w) * src[x];
78 imag += sin(-x * w) * src[x];
79 real_num += cos(-x * w) * src[x] * x;
80 imag_num += sin(-x * w) * src[x] * x;
83 mag[i] = hypot(real, imag);
84 phase[i] = atan2(imag, real);
85 div = real * real + imag * imag;
86 delay[i] = (real_num * real + imag_num * imag) / div;
87 min = fminf(min, mag[i]);
88 max = fmaxf(max, mag[i]);
89 min_delay = fminf(min_delay, delay[i]);
90 max_delay = fmaxf(max_delay, delay[i]);
93 for (i = 0; i < s->w; i++) {
94 int ymag = mag[i] / max * (s->h - 1);
95 int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
96 int yphase = (0.5 * (1. + phase[i] / M_PI)) * (s->h - 1);
98 ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
99 yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
100 ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
105 prev_yphase = yphase;
107 prev_ydelay = ydelay;
109 draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
110 draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
111 draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
114 prev_yphase = yphase;
115 prev_ydelay = ydelay;
118 if (s->w > 400 && s->h > 100) {
119 drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
120 snprintf(text, sizeof(text), "%.2f", max);
121 drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
123 drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
124 snprintf(text, sizeof(text), "%.2f", min);
125 drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
127 drawtext(out, 2, 22, "Max Delay:", 0xDDDDDDDD);
128 snprintf(text, sizeof(text), "%.2f", max_delay);
129 drawtext(out, 11 * 8 + 2, 22, text, 0xDDDDDDDD);
131 drawtext(out, 2, 32, "Min Delay:", 0xDDDDDDDD);
132 snprintf(text, sizeof(text), "%.2f", min_delay);
133 drawtext(out, 11 * 8 + 2, 32, text, 0xDDDDDDDD);
142 static int fn(get_power)(AVFilterContext *ctx, AudioFIRContext *s,
143 int cur_nb_taps, int ch,
156 for (int i = 0; i < cur_nb_taps; i++)
157 sum += FFABS(time[i]);
165 for (int i = 0; i < cur_nb_taps; i++)
174 for (int i = 0; i < cur_nb_taps; i++)
175 sum += time[i] * time[i];
176 ch_gain = 1. / SQRT(sum);
182 ftype *inc, *outc, scale, power;
187 size = 1 << av_ceil_log2_c(cur_nb_taps);
188 inc = av_calloc(size + 2, sizeof(SAMPLE_FORMAT));
189 outc = av_calloc(size + 2, sizeof(SAMPLE_FORMAT));
197 ret = av_tx_init(&tx, &tx_fn, TX_TYPE, 0, size, &scale, 0);
205 memcpy(inc, time, cur_nb_taps * sizeof(SAMPLE_FORMAT));
206 tx_fn(tx, outc, inc, sizeof(SAMPLE_FORMAT));
210 for (int i = 0; i < size / 2 + 1; i++)
211 power = FFMAX(power, HYPOT(outc[i * 2], outc[i * 2 + 1]));
214 for (int i = 0; i < size / 2 + 1; i++)
215 sum += HYPOT(outc[i * 2], outc[i * 2 + 1]);
216 power = SQRT(sum / (size / 2 + 1));
219 ch_gain = 1. / power;
231 if (ch_gain != 1. || s->ir_gain != 1.) {
232 ftype gain = ch_gain * s->ir_gain;
234 av_log(ctx, AV_LOG_DEBUG, "ch%d gain %f\n", ch, gain);
236 s->fdsp->vector_fmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 4));
238 s->fdsp->vector_dmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 8));
245 static void fn(convert_channel)(AVFilterContext *ctx, AudioFIRContext *s, int ch,
246 AudioFIRSegment *seg, int coeff_partition, int selir)
248 const int coffset = coeff_partition * seg->coeff_size;
249 const int nb_taps = s->nb_taps[selir];
250 ftype *time = (ftype *)s->norm_ir[selir]->extended_data[ch];
251 ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
252 ftype *tempout = (ftype *)seg->tempout->extended_data[ch];
253 ctype *coeff = (ctype *)seg->coeff->extended_data[ch];
254 const int remaining = nb_taps - (seg->input_offset + coeff_partition * seg->part_size);
255 const int size = remaining >= seg->part_size ? seg->part_size : remaining;
257 memset(tempin + size, 0, sizeof(*tempin) * (seg->block_size - size));
258 memcpy(tempin, time + seg->input_offset + coeff_partition * seg->part_size,
259 size * sizeof(*tempin));
260 seg->ctx_fn(seg->ctx[ch], tempout, tempin, sizeof(*tempin));
261 memcpy(coeff + coffset, tempout, seg->coeff_size * sizeof(*coeff));
263 av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
264 av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", seg->nb_partitions);
265 av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", seg->part_size);
266 av_log(ctx, AV_LOG_DEBUG, "block size: %d\n", seg->block_size);
267 av_log(ctx, AV_LOG_DEBUG, "fft_length: %d\n", seg->fft_length);
268 av_log(ctx, AV_LOG_DEBUG, "coeff_size: %d\n", seg->coeff_size);
269 av_log(ctx, AV_LOG_DEBUG, "input_size: %d\n", seg->input_size);
270 av_log(ctx, AV_LOG_DEBUG, "input_offset: %d\n", seg->input_offset);
273 static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
275 if ((nb_samples & 15) == 0 && nb_samples >= 8) {
277 s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);
279 s->fdsp->vector_dmac_scalar(dst, src, 1.0, nb_samples);
282 for (int n = 0; n < nb_samples; n++)
287 static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int ioffset, int offset, int selir)
289 AudioFIRContext *s = ctx->priv;
290 const ftype *in = (const ftype *)s->in->extended_data[ch] + ioffset;
291 ftype *blockout, *ptr = (ftype *)out->extended_data[ch] + offset;
292 const int min_part_size = s->min_part_size;
293 const int nb_samples = FFMIN(min_part_size, out->nb_samples - offset);
294 const int nb_segments = s->nb_segments[selir];
295 const float dry_gain = s->dry_gain;
296 const float wet_gain = s->wet_gain;
298 for (int segment = 0; segment < nb_segments; segment++) {
299 AudioFIRSegment *seg = &s->seg[selir][segment];
300 ftype *src = (ftype *)seg->input->extended_data[ch];
301 ftype *dst = (ftype *)seg->output->extended_data[ch];
302 ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
303 ftype *sumout = (ftype *)seg->sumout->extended_data[ch];
304 ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
305 ftype *buf = (ftype *)seg->buffer->extended_data[ch];
306 int *output_offset = &seg->output_offset[ch];
307 const int nb_partitions = seg->nb_partitions;
308 const int input_offset = seg->input_offset;
309 const int part_size = seg->part_size;
312 seg->part_index[ch] = seg->part_index[ch] % nb_partitions;
313 if (dry_gain == 1.f) {
314 memcpy(src + input_offset, in, nb_samples * sizeof(*src));
315 } else if (min_part_size >= 8) {
317 s->fdsp->vector_fmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 4));
319 s->fdsp->vector_dmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 8));
322 ftype *src2 = src + input_offset;
323 for (int n = 0; n < nb_samples; n++)
324 src2[n] = in[n] * dry_gain;
327 output_offset[0] += min_part_size;
328 if (output_offset[0] >= part_size) {
329 output_offset[0] = 0;
331 memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
333 dst += output_offset[0];
334 fn(fir_fadd)(s, ptr, dst, nb_samples);
338 memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
340 blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;
341 memset(tempin + part_size, 0, sizeof(*tempin) * (seg->block_size - part_size));
342 memcpy(tempin, src, sizeof(*src) * part_size);
343 seg->tx_fn(seg->tx[ch], blockout, tempin, sizeof(ftype));
345 j = seg->part_index[ch];
346 for (int i = 0; i < nb_partitions; i++) {
347 const int input_partition = j;
348 const int coeff_partition = i;
349 const int coffset = coeff_partition * seg->coeff_size;
350 const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
351 const ctype *coeff = ((const ctype *)seg->coeff->extended_data[ch]) + coffset;
358 s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
360 s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
364 seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
366 fn(fir_fadd)(s, buf, sumout, part_size);
367 memcpy(dst, buf, part_size * sizeof(*dst));
368 memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
370 fn(fir_fadd)(s, ptr, dst, nb_samples);
372 if (part_size != min_part_size)
373 memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
375 seg->part_index[ch] = (seg->part_index[ch] + 1) % nb_partitions;
381 if (min_part_size >= 8) {
383 s->fdsp->vector_fmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 4));
385 s->fdsp->vector_dmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 8));
388 for (int n = 0; n < nb_samples; n++)