FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_apsyclip.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 323 0.0%
Functions: 0 16 0.0%
Branches: 0 186 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2014 - 2021 Jason Jang
3 * Copyright (c) 2021 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/tx.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29
30 typedef struct AudioPsyClipContext {
31 const AVClass *class;
32
33 double level_in;
34 double level_out;
35 double clip_level;
36 double adaptive;
37 int auto_level;
38 int diff_only;
39 int iterations;
40 char *protections_str;
41 double *protections;
42
43 int num_psy_bins;
44 int fft_size;
45 int overlap;
46 int channels;
47
48 int spread_table_rows;
49 int *spread_table_index;
50 int (*spread_table_range)[2];
51 float *window, *inv_window, *spread_table, *margin_curve;
52
53 AVFrame *in;
54 AVFrame *in_buffer;
55 AVFrame *in_frame;
56 AVFrame *out_dist_frame;
57 AVFrame *windowed_frame;
58 AVFrame *clipping_delta;
59 AVFrame *spectrum_buf;
60 AVFrame *mask_curve;
61
62 AVTXContext **tx_ctx;
63 av_tx_fn tx_fn;
64 AVTXContext **itx_ctx;
65 av_tx_fn itx_fn;
66 } AudioPsyClipContext;
67
68 #define OFFSET(x) offsetof(AudioPsyClipContext, x)
69 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
70
71 static const AVOption apsyclip_options[] = {
72 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, FLAGS },
73 { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, FLAGS },
74 { "clip", "set clip level", OFFSET(clip_level), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 1, FLAGS },
75 { "diff", "enable difference", OFFSET(diff_only), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
76 { "adaptive", "set adaptive distortion", OFFSET(adaptive), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, FLAGS },
77 { "iterations", "set iterations", OFFSET(iterations), AV_OPT_TYPE_INT, {.i64=10}, 1, 20, FLAGS },
78 { "level", "set auto level", OFFSET(auto_level), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
79 {NULL}
80 };
81
82 AVFILTER_DEFINE_CLASS(apsyclip);
83
84 static void generate_hann_window(float *window, float *inv_window, int size)
85 {
86 for (int i = 0; i < size; i++) {
87 float value = 0.5f * (1.f - cosf(2.f * M_PI * i / size));
88
89 window[i] = value;
90 // 1/window to calculate unwindowed peak.
91 inv_window[i] = value > 0.1f ? 1.f / value : 0.f;
92 }
93 }
94
95 static void set_margin_curve(AudioPsyClipContext *s,
96 const int (*points)[2], int num_points, int sample_rate)
97 {
98 int j = 0;
99
100 s->margin_curve[0] = points[0][1];
101
102 for (int i = 0; i < num_points - 1; i++) {
103 while (j < s->fft_size / 2 + 1 && j * sample_rate / s->fft_size < points[i + 1][0]) {
104 // linearly interpolate between points
105 int binHz = j * sample_rate / s->fft_size;
106 s->margin_curve[j] = points[i][1] + (binHz - points[i][0]) * (points[i + 1][1] - points[i][1]) / (points[i + 1][0] - points[i][0]);
107 j++;
108 }
109 }
110 // handle bins after the last point
111 while (j < s->fft_size / 2 + 1) {
112 s->margin_curve[j] = points[num_points - 1][1];
113 j++;
114 }
115
116 // convert margin curve to linear amplitude scale
117 for (j = 0; j < s->fft_size / 2 + 1; j++)
118 s->margin_curve[j] = powf(10.f, s->margin_curve[j] / 20.f);
119 }
120
121 static void generate_spread_table(AudioPsyClipContext *s)
122 {
123 // Calculate tent-shape function in log-log scale.
124
125 // As an optimization, only consider bins close to "bin"
126 // This reduces the number of multiplications needed in calculate_mask_curve
127 // The masking contribution at faraway bins is negligeable
128
129 // Another optimization to save memory and speed up the calculation of the
130 // spread table is to calculate and store only 2 spread functions per
131 // octave, and reuse the same spread function for multiple bins.
132 int table_index = 0;
133 int bin = 0;
134 int increment = 1;
135
136 while (bin < s->num_psy_bins) {
137 float sum = 0;
138 int base_idx = table_index * s->num_psy_bins;
139 int start_bin = bin * 3 / 4;
140 int end_bin = FFMIN(s->num_psy_bins, ((bin + 1) * 4 + 2) / 3);
141 int next_bin;
142
143 for (int j = start_bin; j < end_bin; j++) {
144 // add 0.5 so i=0 doesn't get log(0)
145 float rel_idx_log = FFABS(logf((j + 0.5f) / (bin + 0.5f)));
146 float value;
147 if (j >= bin) {
148 // mask up
149 value = expf(-rel_idx_log * 40.f);
150 } else {
151 // mask down
152 value = expf(-rel_idx_log * 80.f);
153 }
154 // the spreading function is centred in the row
155 sum += value;
156 s->spread_table[base_idx + s->num_psy_bins / 2 + j - bin] = value;
157 }
158 // now normalize it
159 for (int j = start_bin; j < end_bin; j++) {
160 s->spread_table[base_idx + s->num_psy_bins / 2 + j - bin] /= sum;
161 }
162
163 s->spread_table_range[table_index][0] = start_bin - bin;
164 s->spread_table_range[table_index][1] = end_bin - bin;
165
166 if (bin <= 1) {
167 next_bin = bin + 1;
168 } else {
169 if ((bin & (bin - 1)) == 0) {
170 // power of 2
171 increment = bin / 2;
172 }
173
174 next_bin = bin + increment;
175 }
176
177 // set bins between "bin" and "next_bin" to use this table_index
178 for (int i = bin; i < next_bin; i++)
179 s->spread_table_index[i] = table_index;
180
181 bin = next_bin;
182 table_index++;
183 }
184 }
185
186 static int config_input(AVFilterLink *inlink)
187 {
188 AVFilterContext *ctx = inlink->dst;
189 AudioPsyClipContext *s = ctx->priv;
190 static const int points[][2] = { {0,14}, {125,14}, {250,16}, {500,18}, {1000,20}, {2000,20}, {4000,20}, {8000,17}, {16000,14}, {20000,-10} };
191 static const int num_points = 10;
192 float scale = 1.f;
193 int ret;
194
195 s->fft_size = inlink->sample_rate > 100000 ? 1024 : inlink->sample_rate > 50000 ? 512 : 256;
196 s->overlap = s->fft_size / 4;
197
198 // The psy masking calculation is O(n^2),
199 // so skip it for frequencies not covered by base sampling rantes (i.e. 44k)
200 if (inlink->sample_rate <= 50000) {
201 s->num_psy_bins = s->fft_size / 2;
202 } else if (inlink->sample_rate <= 100000) {
203 s->num_psy_bins = s->fft_size / 4;
204 } else {
205 s->num_psy_bins = s->fft_size / 8;
206 }
207
208 s->window = av_calloc(s->fft_size, sizeof(*s->window));
209 s->inv_window = av_calloc(s->fft_size, sizeof(*s->inv_window));
210 if (!s->window || !s->inv_window)
211 return AVERROR(ENOMEM);
212
213 s->in_buffer = ff_get_audio_buffer(inlink, s->fft_size * 2);
214 s->in_frame = ff_get_audio_buffer(inlink, s->fft_size * 2);
215 s->out_dist_frame = ff_get_audio_buffer(inlink, s->fft_size * 2);
216 s->windowed_frame = ff_get_audio_buffer(inlink, s->fft_size * 2);
217 s->clipping_delta = ff_get_audio_buffer(inlink, s->fft_size * 2);
218 s->spectrum_buf = ff_get_audio_buffer(inlink, s->fft_size * 2);
219 s->mask_curve = ff_get_audio_buffer(inlink, s->fft_size / 2 + 1);
220 if (!s->in_buffer || !s->in_frame ||
221 !s->out_dist_frame || !s->windowed_frame ||
222 !s->clipping_delta || !s->spectrum_buf || !s->mask_curve)
223 return AVERROR(ENOMEM);
224
225 generate_hann_window(s->window, s->inv_window, s->fft_size);
226
227 s->margin_curve = av_calloc(s->fft_size / 2 + 1, sizeof(*s->margin_curve));
228 if (!s->margin_curve)
229 return AVERROR(ENOMEM);
230
231 s->spread_table_rows = av_log2(s->num_psy_bins) * 2;
232 s->spread_table = av_calloc(s->spread_table_rows * s->num_psy_bins, sizeof(*s->spread_table));
233 if (!s->spread_table)
234 return AVERROR(ENOMEM);
235
236 s->spread_table_range = av_calloc(s->spread_table_rows * 2, sizeof(*s->spread_table_range));
237 if (!s->spread_table_range)
238 return AVERROR(ENOMEM);
239
240 s->spread_table_index = av_calloc(s->num_psy_bins, sizeof(*s->spread_table_index));
241 if (!s->spread_table_index)
242 return AVERROR(ENOMEM);
243
244 set_margin_curve(s, points, num_points, inlink->sample_rate);
245
246 generate_spread_table(s);
247
248 s->channels = inlink->ch_layout.nb_channels;
249
250 s->tx_ctx = av_calloc(s->channels, sizeof(*s->tx_ctx));
251 s->itx_ctx = av_calloc(s->channels, sizeof(*s->itx_ctx));
252 if (!s->tx_ctx || !s->itx_ctx)
253 return AVERROR(ENOMEM);
254
255 for (int ch = 0; ch < s->channels; ch++) {
256 ret = av_tx_init(&s->tx_ctx[ch], &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0);
257 if (ret < 0)
258 return ret;
259
260 ret = av_tx_init(&s->itx_ctx[ch], &s->itx_fn, AV_TX_FLOAT_FFT, 1, s->fft_size, &scale, 0);
261 if (ret < 0)
262 return ret;
263 }
264
265 return 0;
266 }
267
268 static void apply_window(AudioPsyClipContext *s,
269 const float *in_frame, float *out_frame, const int add_to_out_frame)
270 {
271 const float *window = s->window;
272
273 for (int i = 0; i < s->fft_size; i++) {
274 if (add_to_out_frame) {
275 out_frame[i] += in_frame[i] * window[i];
276 } else {
277 out_frame[i] = in_frame[i] * window[i];
278 }
279 }
280 }
281
282 static void calculate_mask_curve(AudioPsyClipContext *s,
283 const float *spectrum, float *mask_curve)
284 {
285 for (int i = 0; i < s->fft_size / 2 + 1; i++)
286 mask_curve[i] = 0;
287
288 for (int i = 0; i < s->num_psy_bins; i++) {
289 int base_idx, start_bin, end_bin, table_idx;
290 float magnitude;
291 int range[2];
292
293 if (i == 0) {
294 magnitude = FFABS(spectrum[0]);
295 } else if (i == s->fft_size / 2) {
296 magnitude = FFABS(spectrum[s->fft_size]);
297 } else {
298 // Because the input signal is real, the + and - frequencies are redundant.
299 // Multiply the magnitude by 2 to simulate adding up the + and - frequencies.
300 magnitude = hypotf(spectrum[2 * i], spectrum[2 * i + 1]) * 2;
301 }
302
303 table_idx = s->spread_table_index[i];
304 range[0] = s->spread_table_range[table_idx][0];
305 range[1] = s->spread_table_range[table_idx][1];
306 base_idx = table_idx * s->num_psy_bins;
307 start_bin = FFMAX(0, i + range[0]);
308 end_bin = FFMIN(s->num_psy_bins, i + range[1]);
309
310 for (int j = start_bin; j < end_bin; j++)
311 mask_curve[j] += s->spread_table[base_idx + s->num_psy_bins / 2 + j - i] * magnitude;
312 }
313
314 // for ultrasonic frequencies, skip the O(n^2) spread calculation and just copy the magnitude
315 for (int i = s->num_psy_bins; i < s->fft_size / 2 + 1; i++) {
316 float magnitude;
317 if (i == s->fft_size / 2) {
318 magnitude = FFABS(spectrum[s->fft_size]);
319 } else {
320 // Because the input signal is real, the + and - frequencies are redundant.
321 // Multiply the magnitude by 2 to simulate adding up the + and - frequencies.
322 magnitude = hypotf(spectrum[2 * i], spectrum[2 * i + 1]) * 2;
323 }
324
325 mask_curve[i] = magnitude;
326 }
327
328 for (int i = 0; i < s->fft_size / 2 + 1; i++)
329 mask_curve[i] = mask_curve[i] / s->margin_curve[i];
330 }
331
332 static void clip_to_window(AudioPsyClipContext *s,
333 const float *windowed_frame, float *clipping_delta, float delta_boost)
334 {
335 const float *window = s->window;
336
337 for (int i = 0; i < s->fft_size; i++) {
338 const float limit = s->clip_level * window[i];
339 const float effective_value = windowed_frame[i] + clipping_delta[i];
340
341 if (effective_value > limit) {
342 clipping_delta[i] += (limit - effective_value) * delta_boost;
343 } else if (effective_value < -limit) {
344 clipping_delta[i] += (-limit - effective_value) * delta_boost;
345 }
346 }
347 }
348
349 static void limit_clip_spectrum(AudioPsyClipContext *s,
350 float *clip_spectrum, const float *mask_curve)
351 {
352 // bin 0
353 float relative_distortion_level = FFABS(clip_spectrum[0]) / mask_curve[0];
354
355 if (relative_distortion_level > 1.f)
356 clip_spectrum[0] /= relative_distortion_level;
357
358 // bin 1..N/2-1
359 for (int i = 1; i < s->fft_size / 2; i++) {
360 float real = clip_spectrum[i * 2];
361 float imag = clip_spectrum[i * 2 + 1];
362 // Because the input signal is real, the + and - frequencies are redundant.
363 // Multiply the magnitude by 2 to simulate adding up the + and - frequencies.
364 relative_distortion_level = hypotf(real, imag) * 2 / mask_curve[i];
365 if (relative_distortion_level > 1.0) {
366 clip_spectrum[i * 2] /= relative_distortion_level;
367 clip_spectrum[i * 2 + 1] /= relative_distortion_level;
368 clip_spectrum[s->fft_size * 2 - i * 2] /= relative_distortion_level;
369 clip_spectrum[s->fft_size * 2 - i * 2 + 1] /= relative_distortion_level;
370 }
371 }
372 // bin N/2
373 relative_distortion_level = FFABS(clip_spectrum[s->fft_size]) / mask_curve[s->fft_size / 2];
374 if (relative_distortion_level > 1.f)
375 clip_spectrum[s->fft_size] /= relative_distortion_level;
376 }
377
378 static void r2c(float *buffer, int size)
379 {
380 for (int i = size - 1; i >= 0; i--)
381 buffer[2 * i] = buffer[i];
382
383 for (int i = size - 1; i >= 0; i--)
384 buffer[2 * i + 1] = 0.f;
385 }
386
387 static void c2r(float *buffer, int size)
388 {
389 for (int i = 0; i < size; i++)
390 buffer[i] = buffer[2 * i];
391
392 for (int i = 0; i < size; i++)
393 buffer[i + size] = 0.f;
394 }
395
396 static void feed(AVFilterContext *ctx, int ch,
397 const float *in_samples, float *out_samples, int diff_only,
398 float *in_frame, float *out_dist_frame,
399 float *windowed_frame, float *clipping_delta,
400 float *spectrum_buf, float *mask_curve)
401 {
402 AudioPsyClipContext *s = ctx->priv;
403 const float clip_level_inv = 1.f / s->clip_level;
404 const float level_out = s->level_out;
405 float orig_peak = 0;
406 float peak;
407
408 // shift in/out buffers
409 for (int i = 0; i < s->fft_size - s->overlap; i++) {
410 in_frame[i] = in_frame[i + s->overlap];
411 out_dist_frame[i] = out_dist_frame[i + s->overlap];
412 }
413
414 for (int i = 0; i < s->overlap; i++) {
415 in_frame[i + s->fft_size - s->overlap] = in_samples[i];
416 out_dist_frame[i + s->fft_size - s->overlap] = 0.f;
417 }
418
419 apply_window(s, in_frame, windowed_frame, 0);
420 r2c(windowed_frame, s->fft_size);
421 s->tx_fn(s->tx_ctx[ch], spectrum_buf, windowed_frame, sizeof(AVComplexFloat));
422 c2r(windowed_frame, s->fft_size);
423 calculate_mask_curve(s, spectrum_buf, mask_curve);
424
425 // It would be easier to calculate the peak from the unwindowed input.
426 // This is just for consistency with the clipped peak calculateion
427 // because the inv_window zeros out samples on the edge of the window.
428 for (int i = 0; i < s->fft_size; i++)
429 orig_peak = FFMAX(orig_peak, FFABS(windowed_frame[i] * s->inv_window[i]));
430 orig_peak *= clip_level_inv;
431 peak = orig_peak;
432
433 // clear clipping_delta
434 for (int i = 0; i < s->fft_size * 2; i++)
435 clipping_delta[i] = 0.f;
436
437 // repeat clipping-filtering process a few times to control both the peaks and the spectrum
438 for (int i = 0; i < s->iterations; i++) {
439 float mask_curve_shift = 1.122f; // 1.122 is 1dB
440 // The last 1/3 of rounds have boosted delta to help reach the peak target faster
441 float delta_boost = 1.f;
442 if (i >= s->iterations - s->iterations / 3) {
443 // boosting the delta when largs peaks are still present is dangerous
444 if (peak < 2.f)
445 delta_boost = 2.f;
446 }
447
448 clip_to_window(s, windowed_frame, clipping_delta, delta_boost);
449
450 r2c(clipping_delta, s->fft_size);
451 s->tx_fn(s->tx_ctx[ch], spectrum_buf, clipping_delta, sizeof(AVComplexFloat));
452
453 limit_clip_spectrum(s, spectrum_buf, mask_curve);
454
455 s->itx_fn(s->itx_ctx[ch], clipping_delta, spectrum_buf, sizeof(AVComplexFloat));
456 c2r(clipping_delta, s->fft_size);
457
458 for (int i = 0; i < s->fft_size; i++)
459 clipping_delta[i] /= s->fft_size;
460
461 peak = 0;
462 for (int i = 0; i < s->fft_size; i++)
463 peak = FFMAX(peak, FFABS((windowed_frame[i] + clipping_delta[i]) * s->inv_window[i]));
464 peak *= clip_level_inv;
465
466 // Automatically adjust mask_curve as necessary to reach peak target
467 if (orig_peak > 1.f && peak > 1.f) {
468 float diff_achieved = orig_peak - peak;
469 if (i + 1 < s->iterations - s->iterations / 3 && diff_achieved > 0) {
470 float diff_needed = orig_peak - 1.f;
471 float diff_ratio = diff_needed / diff_achieved;
472 // If a good amount of peak reduction was already achieved,
473 // don't shift the mask_curve by the full peak value
474 // On the other hand, if only a little peak reduction was achieved,
475 // don't shift the mask_curve by the enormous diff_ratio.
476 diff_ratio = FFMIN(diff_ratio, peak);
477 mask_curve_shift = FFMAX(mask_curve_shift, diff_ratio);
478 } else {
479 // If the peak got higher than the input or we are in the last 1/3 rounds,
480 // go back to the heavy-handed peak heuristic.
481 mask_curve_shift = FFMAX(mask_curve_shift, peak);
482 }
483 }
484
485 mask_curve_shift = 1.f + (mask_curve_shift - 1.f) * s->adaptive;
486
487 // Be less strict in the next iteration.
488 // This helps with peak control.
489 for (int i = 0; i < s->fft_size / 2 + 1; i++)
490 mask_curve[i] *= mask_curve_shift;
491 }
492
493 // do overlap & add
494 apply_window(s, clipping_delta, out_dist_frame, 1);
495
496 for (int i = 0; i < s->overlap; i++) {
497 // 4 times overlap with squared hanning window results in 1.5 time increase in amplitude
498 if (!ctx->is_disabled) {
499 out_samples[i] = out_dist_frame[i] / 1.5f;
500 if (!diff_only)
501 out_samples[i] += in_frame[i];
502 if (s->auto_level)
503 out_samples[i] *= clip_level_inv;
504 out_samples[i] *= level_out;
505 } else {
506 out_samples[i] = in_frame[i];
507 }
508 }
509 }
510
511 static int psy_channel(AVFilterContext *ctx, AVFrame *in, AVFrame *out, int ch)
512 {
513 AudioPsyClipContext *s = ctx->priv;
514 const float *src = (const float *)in->extended_data[ch];
515 float *in_buffer = (float *)s->in_buffer->extended_data[ch];
516 float *dst = (float *)out->extended_data[ch];
517
518 for (int n = 0; n < s->overlap; n++)
519 in_buffer[n] = src[n] * s->level_in;
520
521 feed(ctx, ch, in_buffer, dst, s->diff_only,
522 (float *)(s->in_frame->extended_data[ch]),
523 (float *)(s->out_dist_frame->extended_data[ch]),
524 (float *)(s->windowed_frame->extended_data[ch]),
525 (float *)(s->clipping_delta->extended_data[ch]),
526 (float *)(s->spectrum_buf->extended_data[ch]),
527 (float *)(s->mask_curve->extended_data[ch]));
528
529 return 0;
530 }
531
532 static int psy_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
533 {
534 AudioPsyClipContext *s = ctx->priv;
535 AVFrame *out = arg;
536 const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
537 const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
538
539 for (int ch = start; ch < end; ch++)
540 psy_channel(ctx, s->in, out, ch);
541
542 return 0;
543 }
544
545 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
546 {
547 AVFilterContext *ctx = inlink->dst;
548 AVFilterLink *outlink = ctx->outputs[0];
549 AudioPsyClipContext *s = ctx->priv;
550 AVFrame *out;
551 int ret;
552
553 out = ff_get_audio_buffer(outlink, s->overlap);
554 if (!out) {
555 ret = AVERROR(ENOMEM);
556 goto fail;
557 }
558
559 s->in = in;
560 av_frame_copy_props(out, in);
561 ff_filter_execute(ctx, psy_channels, out, NULL,
562 FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
563
564 out->pts = in->pts;
565 out->nb_samples = in->nb_samples;
566 ret = ff_filter_frame(outlink, out);
567 fail:
568 av_frame_free(&in);
569 s->in = NULL;
570 return ret < 0 ? ret : 0;
571 }
572
573 static int activate(AVFilterContext *ctx)
574 {
575 AVFilterLink *inlink = ctx->inputs[0];
576 AVFilterLink *outlink = ctx->outputs[0];
577 AudioPsyClipContext *s = ctx->priv;
578 AVFrame *in = NULL;
579 int ret = 0, status;
580 int64_t pts;
581
582 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
583
584 ret = ff_inlink_consume_samples(inlink, s->overlap, s->overlap, &in);
585 if (ret < 0)
586 return ret;
587
588 if (ret > 0) {
589 return filter_frame(inlink, in);
590 } else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
591 ff_outlink_set_status(outlink, status, pts);
592 return 0;
593 } else {
594 if (ff_inlink_queued_samples(inlink) >= s->overlap) {
595 ff_filter_set_ready(ctx, 10);
596 } else if (ff_outlink_frame_wanted(outlink)) {
597 ff_inlink_request_frame(inlink);
598 }
599 return 0;
600 }
601 }
602
603 static av_cold void uninit(AVFilterContext *ctx)
604 {
605 AudioPsyClipContext *s = ctx->priv;
606
607 av_freep(&s->window);
608 av_freep(&s->inv_window);
609 av_freep(&s->spread_table);
610 av_freep(&s->spread_table_range);
611 av_freep(&s->spread_table_index);
612 av_freep(&s->margin_curve);
613
614 av_frame_free(&s->in_buffer);
615 av_frame_free(&s->in_frame);
616 av_frame_free(&s->out_dist_frame);
617 av_frame_free(&s->windowed_frame);
618 av_frame_free(&s->clipping_delta);
619 av_frame_free(&s->spectrum_buf);
620 av_frame_free(&s->mask_curve);
621
622 for (int ch = 0; ch < s->channels; ch++) {
623 if (s->tx_ctx)
624 av_tx_uninit(&s->tx_ctx[ch]);
625 if (s->itx_ctx)
626 av_tx_uninit(&s->itx_ctx[ch]);
627 }
628
629 av_freep(&s->tx_ctx);
630 av_freep(&s->itx_ctx);
631 }
632
633 static const AVFilterPad inputs[] = {
634 {
635 .name = "default",
636 .type = AVMEDIA_TYPE_AUDIO,
637 .config_props = config_input,
638 },
639 };
640
641 const AVFilter ff_af_apsyclip = {
642 .name = "apsyclip",
643 .description = NULL_IF_CONFIG_SMALL("Audio Psychoacoustic Clipper."),
644 .priv_size = sizeof(AudioPsyClipContext),
645 .priv_class = &apsyclip_class,
646 .uninit = uninit,
647 FILTER_INPUTS(inputs),
648 FILTER_OUTPUTS(ff_audio_default_filterpad),
649 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
650 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
651 AVFILTER_FLAG_SLICE_THREADS,
652 .activate = activate,
653 .process_command = ff_filter_process_command,
654 };
655