1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
/*
* 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
*/
#include <float.h>
#include "libavutil/common.h"
#include "libavutil/float_dsp.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/xga_font_data.h"
#include "libavcodec/avfft.h"
#include "audio.h"
#include "avfilter.h"
#include "filters.h"
#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];
}
static int fir_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
{
AudioFIRContext *s = ctx->priv;
const float *src = (const float *)s->in[0]->extended_data[ch];
float *sum = (float *)s->seg.sum->extended_data[ch];
AVFrame *out = arg;
float *block, *dst, *ptr;
int n, i, j;
memset(sum, 0, sizeof(*sum) * s->seg.fft_length);
block = (float *)s->seg.block->extended_data[ch] + s->seg.part_index * s->seg.block_size;
memset(block, 0, sizeof(*block) * s->seg.fft_length);
s->fdsp->vector_fmul_scalar(block, src, s->dry_gain, FFALIGN(out->nb_samples, 4));
emms_c();
av_rdft_calc(s->seg.rdft[ch], block);
block[2 * s->seg.part_size] = block[1];
block[1] = 0;
j = s->seg.part_index;
for (i = 0; i < s->seg.nb_partitions; i++) {
const int coffset = i * s->seg.coeff_size;
const float *block = (const float *)s->seg.block->extended_data[ch] + j * s->seg.block_size;
const FFTComplex *coeff = s->seg.coeff[ch * !s->one2many] + coffset;
s->fcmul_add(sum, block, (const float *)coeff, s->seg.part_size);
if (j == 0)
j = s->seg.nb_partitions;
j--;
}
sum[1] = sum[2 * s->seg.part_size];
av_rdft_calc(s->seg.irdft[ch], sum);
dst = (float *)s->seg.buffer->extended_data[ch];
for (n = 0; n < s->seg.part_size; n++) {
dst[n] += sum[n];
}
ptr = (float *)out->extended_data[ch];
s->fdsp->vector_fmul_scalar(ptr, dst, s->wet_gain, FFALIGN(out->nb_samples, 4));
emms_c();
dst = (float *)s->seg.buffer->extended_data[ch];
memcpy(dst, sum + s->seg.part_size, s->seg.part_size * sizeof(*dst));
return 0;
}
static int fir_frame(AudioFIRContext *s, AVFrame *in, AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFrame *out = NULL;
out = ff_get_audio_buffer(outlink, in->nb_samples);
if (!out) {
av_frame_free(&in);
return AVERROR(ENOMEM);
}
if (s->pts == AV_NOPTS_VALUE)
s->pts = in->pts;
s->in[0] = in;
ctx->internal->execute(ctx, fir_channel, out, NULL, outlink->channels);
s->seg.part_index = (s->seg.part_index + 1) % s->seg.nb_partitions;
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);
av_frame_free(&in);
s->in[0] = NULL;
return ff_filter_frame(outlink, out);
}
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)
{
int dx = FFABS(x1-x0);
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;
x0--;
}
if (e2 < dy) {
err += dx;
y0 += sy;
}
}
}
static void draw_response(AVFilterContext *ctx, AVFrame *out)
{
AudioFIRContext *s = ctx->priv;
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;
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));
delay = av_malloc_array(s->w, sizeof(*delay));
if (!mag || !phase || !delay)
goto end;
channel = av_clip(s->ir_channel, 0, s->in[1]->channels - 1);
for (i = 0; i < s->w; i++) {
const float *src = (const float *)s->in[1]->extended_data[channel];
double w = i * M_PI / (s->w - 1);
double div, real_num = 0., imag_num = 0., real = 0., imag = 0.;
for (x = 0; x < s->nb_taps; x++) {
real += cos(-x * w) * src[x];
imag += sin(-x * w) * src[x];
real_num += cos(-x * w) * src[x] * x;
imag_num += sin(-x * w) * src[x] * x;
}
mag[i] = hypot(real, imag);
phase[i] = atan2(imag, real);
div = real * real + imag * imag;
delay[i] = (real_num * real + imag_num * imag) / div;
min = fminf(min, mag[i]);
max = fmaxf(max, mag[i]);
min_delay = fminf(min_delay, delay[i]);
max_delay = fmaxf(max_delay, delay[i]);
}
for (i = 0; i < s->w; i++) {
int ymag = mag[i] / max * (s->h - 1);
int ydelay = (delay[i] - min_delay) / (max_delay - min_delay) * (s->h - 1);
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);
ydelay = s->h - 1 - av_clip(ydelay, 0, s->h - 1);
if (prev_ymag < 0)
prev_ymag = ymag;
if (prev_yphase < 0)
prev_yphase = yphase;
if (prev_ydelay < 0)
prev_ydelay = ydelay;
draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
draw_line(out, i, ydelay, FFMAX(i - 1, 0), prev_ydelay, 0xFF00FFFF);
prev_ymag = ymag;
prev_yphase = yphase;
prev_ydelay = ydelay;
}
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);
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);
}
end:
av_free(delay);
av_free(phase);
av_free(mag);
}
static int init_segment(AVFilterContext *ctx, AudioFIRSegment *seg, int nb_partitions, int part_size)
{
seg->coeff = av_calloc(ctx->inputs[1]->channels, sizeof(*seg->coeff));
seg->rdft = av_calloc(ctx->inputs[0]->channels, sizeof(*seg->rdft));
seg->irdft = av_calloc(ctx->inputs[0]->channels, sizeof(*seg->irdft));
if (!seg->coeff || !seg->rdft || !seg->irdft)
return AVERROR(ENOMEM);
seg->fft_length = part_size * 4 + 1;
seg->part_size = part_size;
seg->block_size = FFALIGN(seg->fft_length, 32);
seg->coeff_size = FFALIGN(seg->part_size + 1, 32);
seg->nb_partitions = nb_partitions;
for (int ch = 0; ch < ctx->inputs[1]->channels; ch++) {
seg->coeff[ch] = av_calloc(seg->nb_partitions * seg->coeff_size, sizeof(**seg->coeff));
if (!seg->coeff[ch])
return AVERROR(ENOMEM);
}
for (int ch = 0; ch < ctx->inputs[0]->channels; ch++) {
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);
if (!seg->buffer || !seg->sum || !seg->block)
return AVERROR(ENOMEM);
return 0;
}
static int convert_coeffs(AVFilterContext *ctx)
{
AudioFIRContext *s = ctx->priv;
int ret, i, ch, n, N;
float power = 0;
s->nb_taps = ff_inlink_queued_samples(ctx->inputs[1]);
if (s->nb_taps <= 0)
return AVERROR(EINVAL);
for (n = av_log2(s->minp); (1 << n) < s->nb_taps; n++);
N = FFMIN(n, av_log2(s->maxp));
ret = init_segment(ctx, &s->seg, (s->nb_taps + (1 << N) - 1) / (1 << N), 1 << N);
if (ret < 0)
return ret;
ret = ff_inlink_consume_samples(ctx->inputs[1], s->nb_taps, s->nb_taps, &s->in[1]);
if (ret < 0)
return ret;
if (ret == 0)
return AVERROR_BUG;
if (s->response)
draw_response(ctx, s->video);
s->gain = 1;
switch (s->gtype) {
case -1:
/* nothing to do */
break;
case 0:
for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
for (i = 0; i < s->nb_taps; i++)
power += FFABS(time[i]);
}
s->gain = ctx->inputs[1]->channels / power;
break;
case 1:
for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
for (i = 0; i < s->nb_taps; i++)
power += time[i];
}
s->gain = ctx->inputs[1]->channels / power;
break;
case 2:
for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
for (i = 0; i < s->nb_taps; i++)
power += time[i] * time[i];
}
s->gain = sqrtf(ch / power);
break;
default:
return AVERROR_BUG;
}
s->gain = FFMIN(s->gain * s->ir_gain, 1.f);
av_log(ctx, AV_LOG_DEBUG, "power %f, gain %f\n", power, s->gain);
for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
s->fdsp->vector_fmul_scalar(time, time, s->gain, FFALIGN(s->nb_taps, 4));
}
for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
float *block = (float *)s->seg.block->extended_data[ch];
FFTComplex *coeff = s->seg.coeff[ch];
for (i = FFMAX(1, s->length * s->nb_taps); i < s->nb_taps; i++)
time[i] = 0;
for (i = 0; i < s->seg.nb_partitions; i++) {
const float scale = 1.f / s->seg.part_size;
const int toffset = i * s->seg.part_size;
const int coffset = i * s->seg.coeff_size;
const int remaining = s->nb_taps - (i * s->seg.part_size);
const int size = remaining >= s->seg.part_size ? s->seg.part_size : remaining;
memset(block, 0, sizeof(*block) * s->seg.fft_length);
memcpy(block, time + toffset, size * sizeof(*block));
av_rdft_calc(s->seg.rdft[0], block);
coeff[coffset].re = block[0] * scale;
coeff[coffset].im = 0;
for (n = 1; n < s->seg.part_size; n++) {
coeff[coffset + n].re = block[2 * n] * scale;
coeff[coffset + n].im = block[2 * n + 1] * scale;
}
coeff[coffset + s->seg.part_size].re = block[1] * scale;
coeff[coffset + s->seg.part_size].im = 0;
}
}
av_frame_free(&s->in[1]);
av_log(ctx, AV_LOG_DEBUG, "nb_taps: %d\n", s->nb_taps);
av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", s->seg.nb_partitions);
av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", s->seg.part_size);
av_log(ctx, AV_LOG_DEBUG, "fft_length: %d\n", s->seg.fft_length);
s->have_coeffs = 1;
return 0;
}
static int check_ir(AVFilterLink *link, AVFrame *frame)
{
AVFilterContext *ctx = link->dst;
AudioFIRContext *s = ctx->priv;
int nb_taps, max_nb_taps;
nb_taps = ff_inlink_queued_samples(link);
max_nb_taps = s->max_ir_len * ctx->outputs[0]->sample_rate;
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;
}
static int activate(AVFilterContext *ctx)
{
AudioFIRContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
AVFrame *in = NULL;
int ret, status;
int64_t pts;
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
if (s->response)
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[1], ctx);
if (!s->eof_coeffs) {
AVFrame *ir = NULL;
ret = check_ir(ctx->inputs[1], ir);
if (ret < 0)
return ret;
if (ff_outlink_get_status(ctx->inputs[1]) == AVERROR_EOF)
s->eof_coeffs = 1;
if (!s->eof_coeffs) {
if (ff_outlink_frame_wanted(ctx->outputs[0]))
ff_inlink_request_frame(ctx->inputs[1]);
else if (s->response && ff_outlink_frame_wanted(ctx->outputs[1]))
ff_inlink_request_frame(ctx->inputs[1]);
return 0;
}
}
if (!s->have_coeffs && s->eof_coeffs) {
ret = convert_coeffs(ctx);
if (ret < 0)
return ret;
}
ret = ff_inlink_consume_samples(ctx->inputs[0], s->seg.part_size, s->seg.part_size, &in);
if (ret > 0)
ret = fir_frame(s, in, outlink);
if (ret < 0)
return ret;
if (s->response && s->have_coeffs) {
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) {
s->video->pts = new_pts;
return ff_filter_frame(ctx->outputs[1], av_frame_clone(s->video));
}
}
if (ff_inlink_queued_samples(ctx->inputs[0]) >= s->seg.part_size) {
ff_filter_set_ready(ctx, 10);
return 0;
}
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;
}
}
if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
!ff_outlink_get_status(ctx->inputs[0])) {
ff_inlink_request_frame(ctx->inputs[0]);
return 0;
}
if (s->response &&
ff_outlink_frame_wanted(ctx->outputs[1]) &&
!ff_outlink_get_status(ctx->inputs[0])) {
ff_inlink_request_frame(ctx->inputs[0]);
return 0;
}
return FFERROR_NOT_READY;
}
static int query_formats(AVFilterContext *ctx)
{
AudioFIRContext *s = ctx->priv;
AVFilterFormats *formats;
AVFilterChannelLayouts *layouts;
static const enum AVSampleFormat sample_fmts[] = {
AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_NONE
};
static const enum AVPixelFormat pix_fmts[] = {
AV_PIX_FMT_RGB0,
AV_PIX_FMT_NONE
};
int ret;
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;
}
layouts = ff_all_channel_counts();
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;
ret = ff_add_channel_layout(&mono, AV_CH_LAYOUT_MONO);
if (ret)
return ret;
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;
if ((ret = ff_channel_layouts_ref(mono, &ctx->inputs[1]->out_channel_layouts)) < 0)
return ret;
}
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;
s->one2many = ctx->inputs[1]->channels == 1;
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;
s->nb_coef_channels = ctx->inputs[1]->channels;
s->pts = AV_NOPTS_VALUE;
return 0;
}
static void uninit_segment(AVFilterContext *ctx, AudioFIRSegment *seg)
{
AudioFIRContext *s = ctx->priv;
if (seg->coeff) {
for (int ch = 0; ch < s->nb_coef_channels; ch++) {
av_freep(&seg->coeff[ch]);
}
}
av_freep(&seg->coeff);
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_frame_free(&seg->block);
av_frame_free(&seg->sum);
av_frame_free(&seg->buffer);
}
static av_cold void uninit(AVFilterContext *ctx)
{
AudioFIRContext *s = ctx->priv;
uninit_segment(ctx, &s->seg);
av_freep(&s->fdsp);
av_frame_free(&s->in[1]);
for (int i = 0; i < ctx->nb_outputs; i++)
av_freep(&ctx->output_pads[i].name);
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;
outlink->frame_rate = s->frame_rate;
outlink->time_base = av_inv_q(outlink->frame_rate);
av_frame_free(&s->video);
s->video = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!s->video)
return AVERROR(ENOMEM);
return 0;
}
static av_cold int init(AVFilterContext *ctx)
{
AudioFIRContext *s = ctx->priv;
AVFilterPad pad, vpad;
int ret;
pad = (AVFilterPad){
.name = av_strdup("default"),
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_output,
};
if (!pad.name)
return AVERROR(ENOMEM);
if (s->response) {
vpad = (AVFilterPad){
.name = av_strdup("filter_response"),
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_video,
};
if (!vpad.name)
return AVERROR(ENOMEM);
}
ret = ff_insert_outpad(ctx, 0, &pad);
if (ret < 0) {
av_freep(&pad.name);
return ret;
}
if (s->response) {
ret = ff_insert_outpad(ctx, 1, &vpad);
if (ret < 0) {
av_freep(&vpad.name);
return ret;
}
}
s->fcmul_add = fcmul_add_c;
s->fdsp = avpriv_float_dsp_alloc(0);
if (!s->fdsp)
return AVERROR(ENOMEM);
if (ARCH_X86)
ff_afir_init_x86(s);
return 0;
}
static const AVFilterPad afir_inputs[] = {
{
.name = "main",
.type = AVMEDIA_TYPE_AUDIO,
},{
.name = "ir",
.type = AVMEDIA_TYPE_AUDIO,
},
{ NULL }
};
#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
#define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
#define OFFSET(x) offsetof(AudioFIRContext, x)
static const AVOption afir_options[] = {
{ "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 },
{ "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" },
{ "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 },
{ "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" },
{ "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 },
{ "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT32_MAX, VF },
{ "minp", "set min partition size", OFFSET(minp), AV_OPT_TYPE_INT, {.i64=16}, 16, 32768, AF },
{ "maxp", "set max partition size", OFFSET(maxp), AV_OPT_TYPE_INT, {.i64=8192}, 16, 32768, AF },
{ NULL }
};
AVFILTER_DEFINE_CLASS(afir);
AVFilter ff_af_afir = {
.name = "afir",
.description = NULL_IF_CONFIG_SMALL("Apply Finite Impulse Response filter with supplied coefficients in 2nd stream."),
.priv_size = sizeof(AudioFIRContext),
.priv_class = &afir_class,
.query_formats = query_formats,
.init = init,
.activate = activate,
.uninit = uninit,
.inputs = afir_inputs,
.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
AVFILTER_FLAG_SLICE_THREADS,
};
|