FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_alimiter.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 147 202 72.8%
Functions: 6 6 100.0%
Branches: 65 122 53.3%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3 * Copyright (c) 2015 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
9 * License 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Lookahead limiter filter
25 */
26
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/fifo.h"
30 #include "libavutil/opt.h"
31
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36
37 typedef struct MetaItem {
38 int64_t pts;
39 int nb_samples;
40 } MetaItem;
41
42 typedef struct AudioLimiterContext {
43 const AVClass *class;
44
45 double limit;
46 double attack;
47 double release;
48 double att;
49 double level_in;
50 double level_out;
51 int auto_release;
52 int auto_level;
53 double asc;
54 int asc_c;
55 int asc_pos;
56 double asc_coeff;
57
58 double *buffer;
59 int buffer_size;
60 int pos;
61 int *nextpos;
62 double *nextdelta;
63
64 int in_trim;
65 int out_pad;
66 int64_t next_in_pts;
67 int64_t next_out_pts;
68 int latency;
69
70 AVFifo *fifo;
71
72 double delta;
73 int nextiter;
74 int nextlen;
75 int asc_changed;
76 } AudioLimiterContext;
77
78 #define OFFSET(x) offsetof(AudioLimiterContext, x)
79 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
80
81 static const AVOption alimiter_options[] = {
82 { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, AF },
83 { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, AF },
84 { "limit", "set limit", OFFSET(limit), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0625, 1, AF },
85 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=5}, 0.1, 80, AF },
86 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 8000, AF },
87 { "asc", "enable asc", OFFSET(auto_release), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AF },
88 { "asc_level", "set asc level", OFFSET(asc_coeff), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, AF },
89 { "level", "auto level", OFFSET(auto_level), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
90 { "latency", "compensate delay", OFFSET(latency), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AF },
91 { NULL }
92 };
93
94 AVFILTER_DEFINE_CLASS(alimiter);
95
96 1 static av_cold int init(AVFilterContext *ctx)
97 {
98 1 AudioLimiterContext *s = ctx->priv;
99
100 1 s->attack /= 1000.;
101 1 s->release /= 1000.;
102 1 s->att = 1.;
103 1 s->asc_pos = -1;
104 1 s->asc_coeff = pow(0.5, s->asc_coeff - 0.5) * 2 * -1;
105
106 1 return 0;
107 }
108
109 138816 static double get_rdelta(AudioLimiterContext *s, double release, int sample_rate,
110 double peak, double limit, double patt, int asc)
111 {
112 138816 double rdelta = (1.0 - patt) / (sample_rate * release);
113
114
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 138816 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
138816 if (asc && s->auto_release && s->asc_c > 0) {
115 double a_att = limit / (s->asc_coeff * s->asc) * (double)s->asc_c;
116
117 if (a_att > patt) {
118 double delta = FFMAX((a_att - patt) / (sample_rate * release), rdelta / 10);
119
120 if (delta < rdelta)
121 rdelta = delta;
122 }
123 }
124
125 138816 return rdelta;
126 }
127
128 259 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
129 {
130 259 AVFilterContext *ctx = inlink->dst;
131 259 AudioLimiterContext *s = ctx->priv;
132 259 AVFilterLink *outlink = ctx->outputs[0];
133 259 const double *src = (const double *)in->data[0];
134 259 const int channels = inlink->ch_layout.nb_channels;
135 259 const int buffer_size = s->buffer_size;
136 259 double *dst, *buffer = s->buffer;
137 259 const double release = s->release;
138 259 const double limit = s->limit;
139 259 double *nextdelta = s->nextdelta;
140
1/2
✓ Branch 0 taken 259 times.
✗ Branch 1 not taken.
259 double level = s->auto_level ? 1 / limit : 1;
141 259 const double level_out = s->level_out;
142 259 const double level_in = s->level_in;
143 259 int *nextpos = s->nextpos;
144 AVFrame *out;
145 double *buf;
146 int n, c, i;
147 int new_out_samples;
148 int64_t out_duration;
149 int64_t in_duration;
150 int64_t in_pts;
151 MetaItem meta;
152
153
1/2
✓ Branch 1 taken 259 times.
✗ Branch 2 not taken.
259 if (av_frame_is_writable(in)) {
154 259 out = in;
155 } else {
156 out = ff_get_audio_buffer(outlink, in->nb_samples);
157 if (!out) {
158 av_frame_free(&in);
159 return AVERROR(ENOMEM);
160 }
161 av_frame_copy_props(out, in);
162 }
163 259 dst = (double *)out->data[0];
164
165
2/2
✓ Branch 0 taken 264600 times.
✓ Branch 1 taken 259 times.
264859 for (n = 0; n < in->nb_samples; n++) {
166 264600 double peak = 0;
167
168
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 264600 times.
793800 for (c = 0; c < channels; c++) {
169 529200 double sample = src[c] * level_in;
170
171 529200 buffer[s->pos + c] = sample;
172
2/2
✓ Branch 0 taken 67140 times.
✓ Branch 1 taken 462060 times.
529200 peak = FFMAX(peak, fabs(sample));
173 }
174
175
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 264600 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
264600 if (s->auto_release && peak > limit) {
176 s->asc += peak;
177 s->asc_c++;
178 }
179
180
2/2
✓ Branch 0 taken 138816 times.
✓ Branch 1 taken 125784 times.
264600 if (peak > limit) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138816 times.
138816 double patt = FFMIN(limit / peak, 1.);
182 138816 double rdelta = get_rdelta(s, release, inlink->sample_rate,
183 peak, limit, patt, 0);
184 138816 double delta = (limit / peak - s->att) / buffer_size * channels;
185 138816 int found = 0;
186
187
2/2
✓ Branch 0 taken 1669 times.
✓ Branch 1 taken 137147 times.
138816 if (delta < s->delta) {
188 1669 s->delta = delta;
189 1669 nextpos[0] = s->pos;
190 1669 nextpos[1] = -1;
191 1669 nextdelta[0] = rdelta;
192 1669 s->nextlen = 1;
193 1669 s->nextiter= 0;
194 } else {
195
2/2
✓ Branch 0 taken 439864 times.
✓ Branch 1 taken 124511 times.
564375 for (i = s->nextiter; i < s->nextiter + s->nextlen; i++) {
196 439864 int j = i % buffer_size;
197 439864 double ppeak = 0, pdelta;
198
199
2/2
✓ Branch 0 taken 879728 times.
✓ Branch 1 taken 439864 times.
1319592 for (c = 0; c < channels; c++) {
200
2/2
✓ Branch 0 taken 121013 times.
✓ Branch 1 taken 758715 times.
879728 ppeak = FFMAX(ppeak, fabs(buffer[nextpos[j] + c]));
201 }
202 439864 pdelta = (limit / peak - limit / ppeak) / (((buffer_size - nextpos[j] + s->pos) % buffer_size) / channels);
203
2/2
✓ Branch 0 taken 12636 times.
✓ Branch 1 taken 427228 times.
439864 if (pdelta < nextdelta[j]) {
204 12636 nextdelta[j] = pdelta;
205 12636 found = 1;
206 12636 break;
207 }
208 }
209
2/2
✓ Branch 0 taken 12636 times.
✓ Branch 1 taken 124511 times.
137147 if (found) {
210 12636 s->nextlen = i - s->nextiter + 1;
211 12636 nextpos[(s->nextiter + s->nextlen) % buffer_size] = s->pos;
212 12636 nextdelta[(s->nextiter + s->nextlen) % buffer_size] = rdelta;
213 12636 nextpos[(s->nextiter + s->nextlen + 1) % buffer_size] = -1;
214 12636 s->nextlen++;
215 }
216 }
217 }
218
219 264600 buf = &s->buffer[(s->pos + channels) % buffer_size];
220 264600 peak = 0;
221
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 264600 times.
793800 for (c = 0; c < channels; c++) {
222 529200 double sample = buf[c];
223
224
2/2
✓ Branch 0 taken 67137 times.
✓ Branch 1 taken 462063 times.
529200 peak = FFMAX(peak, fabs(sample));
225 }
226
227
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 264600 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
264600 if (s->pos == s->asc_pos && !s->asc_changed)
228 s->asc_pos = -1;
229
230
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 264600 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
264600 if (s->auto_release && s->asc_pos == -1 && peak > limit) {
231 s->asc -= peak;
232 s->asc_c--;
233 }
234
235 264600 s->att += s->delta;
236
237
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 264600 times.
793800 for (c = 0; c < channels; c++)
238 529200 dst[c] = buf[c] * s->att;
239
240
2/2
✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 262581 times.
264600 if ((s->pos + channels) % buffer_size == nextpos[s->nextiter]) {
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2019 times.
2019 if (s->auto_release) {
242 s->delta = get_rdelta(s, release, inlink->sample_rate,
243 peak, limit, s->att, 1);
244 if (s->nextlen > 1) {
245 double ppeak = 0, pdelta;
246 int pnextpos = nextpos[(s->nextiter + 1) % buffer_size];
247
248 for (c = 0; c < channels; c++) {
249 ppeak = FFMAX(ppeak, fabs(buffer[pnextpos + c]));
250 }
251 pdelta = (limit / ppeak - s->att) /
252 (((buffer_size + pnextpos -
253 ((s->pos + channels) % buffer_size)) %
254 buffer_size) / channels);
255 if (pdelta < s->delta)
256 s->delta = pdelta;
257 }
258 } else {
259 2019 s->delta = nextdelta[s->nextiter];
260 2019 s->att = limit / peak;
261 }
262
263 2019 s->nextlen -= 1;
264 2019 nextpos[s->nextiter] = -1;
265 2019 s->nextiter = (s->nextiter + 1) % buffer_size;
266 }
267
268
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 264589 times.
264600 if (s->att > 1.) {
269 11 s->att = 1.;
270 11 s->delta = 0.;
271 11 s->nextiter = 0;
272 11 s->nextlen = 0;
273 11 nextpos[0] = -1;
274 }
275
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264600 times.
264600 if (s->att <= 0.) {
277 s->att = 0.0000000000001;
278 s->delta = (1.0 - s->att) / (inlink->sample_rate * release);
279 }
280
281
4/4
✓ Branch 0 taken 258924 times.
✓ Branch 1 taken 5676 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 258919 times.
264600 if (s->att != 1. && (1. - s->att) < 0.0000000000001)
282 5 s->att = 1.;
283
284
3/4
✓ Branch 0 taken 203760 times.
✓ Branch 1 taken 60840 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 203760 times.
264600 if (s->delta != 0. && fabs(s->delta) < 0.00000000000001)
285 s->delta = 0.;
286
287
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 264600 times.
793800 for (c = 0; c < channels; c++)
288 529200 dst[c] = av_clipd(dst[c], -limit, limit) * level * level_out;
289
290 264600 s->pos = (s->pos + channels) % buffer_size;
291 264600 src += channels;
292 264600 dst += channels;
293 }
294
295 259 in_duration = av_rescale_q(in->nb_samples, inlink->time_base, av_make_q(1, in->sample_rate));
296 259 in_pts = in->pts;
297 259 meta = (MetaItem){ in->pts, in->nb_samples };
298 259 av_fifo_write(s->fifo, &meta, 1);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (in != out)
300 av_frame_free(&in);
301
302 259 new_out_samples = out->nb_samples;
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (s->in_trim > 0) {
304 int trim = FFMIN(new_out_samples, s->in_trim);
305 new_out_samples -= trim;
306 s->in_trim -= trim;
307 }
308
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (new_out_samples <= 0) {
310 av_frame_free(&out);
311 return 0;
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 } else if (new_out_samples < out->nb_samples) {
313 int offset = out->nb_samples - new_out_samples;
314 memmove(out->extended_data[0], out->extended_data[0] + sizeof(double) * offset * out->ch_layout.nb_channels,
315 sizeof(double) * new_out_samples * out->ch_layout.nb_channels);
316 out->nb_samples = new_out_samples;
317 s->in_trim = 0;
318 }
319
320 259 av_fifo_read(s->fifo, &meta, 1);
321
322 259 out_duration = av_rescale_q(out->nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
323 259 in_duration = av_rescale_q(meta.nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
324 259 in_pts = meta.pts;
325
326
3/4
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 258 times.
259 if (s->next_out_pts != AV_NOPTS_VALUE && out->pts != s->next_out_pts &&
327 s->next_in_pts != AV_NOPTS_VALUE && in_pts == s->next_in_pts) {
328 out->pts = s->next_out_pts;
329 } else {
330 259 out->pts = in_pts;
331 }
332 259 s->next_in_pts = in_pts + in_duration;
333 259 s->next_out_pts = out->pts + out_duration;
334
335 259 return ff_filter_frame(outlink, out);
336 }
337
338 260 static int request_frame(AVFilterLink* outlink)
339 {
340 260 AVFilterContext *ctx = outlink->src;
341 260 AudioLimiterContext *s = (AudioLimiterContext*)ctx->priv;
342 int ret;
343
344 260 ret = ff_request_frame(ctx->inputs[0]);
345
346
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 259 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
260 if (ret == AVERROR_EOF && s->out_pad > 0) {
347 AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(1024, s->out_pad));
348 if (!frame)
349 return AVERROR(ENOMEM);
350
351 s->out_pad -= frame->nb_samples;
352 frame->pts = s->next_in_pts;
353 return filter_frame(ctx->inputs[0], frame);
354 }
355 260 return ret;
356 }
357
358 1 static int config_input(AVFilterLink *inlink)
359 {
360 1 AVFilterContext *ctx = inlink->dst;
361 1 AudioLimiterContext *s = ctx->priv;
362 int obuffer_size;
363
364 1 obuffer_size = inlink->sample_rate * inlink->ch_layout.nb_channels * 100 / 1000. + inlink->ch_layout.nb_channels;
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (obuffer_size < inlink->ch_layout.nb_channels)
366 return AVERROR(EINVAL);
367
368 1 s->buffer = av_calloc(obuffer_size, sizeof(*s->buffer));
369 1 s->nextdelta = av_calloc(obuffer_size, sizeof(*s->nextdelta));
370 1 s->nextpos = av_malloc_array(obuffer_size, sizeof(*s->nextpos));
371
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!s->buffer || !s->nextdelta || !s->nextpos)
372 return AVERROR(ENOMEM);
373
374 1 memset(s->nextpos, -1, obuffer_size * sizeof(*s->nextpos));
375 1 s->buffer_size = inlink->sample_rate * s->attack * inlink->ch_layout.nb_channels;
376 1 s->buffer_size -= s->buffer_size % inlink->ch_layout.nb_channels;
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->latency)
378 s->in_trim = s->out_pad = s->buffer_size / inlink->ch_layout.nb_channels - 1;
379 1 s->next_out_pts = AV_NOPTS_VALUE;
380 1 s->next_in_pts = AV_NOPTS_VALUE;
381
382 1 s->fifo = av_fifo_alloc2(8, sizeof(MetaItem), AV_FIFO_FLAG_AUTO_GROW);
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->fifo) {
384 return AVERROR(ENOMEM);
385 }
386
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->buffer_size <= 0) {
388 av_log(ctx, AV_LOG_ERROR, "Attack is too small.\n");
389 return AVERROR(EINVAL);
390 }
391
392 1 return 0;
393 }
394
395 1 static av_cold void uninit(AVFilterContext *ctx)
396 {
397 1 AudioLimiterContext *s = ctx->priv;
398
399 1 av_freep(&s->buffer);
400 1 av_freep(&s->nextdelta);
401 1 av_freep(&s->nextpos);
402
403 1 av_fifo_freep2(&s->fifo);
404 1 }
405
406 static const AVFilterPad alimiter_inputs[] = {
407 {
408 .name = "main",
409 .type = AVMEDIA_TYPE_AUDIO,
410 .filter_frame = filter_frame,
411 .config_props = config_input,
412 },
413 };
414
415 static const AVFilterPad alimiter_outputs[] = {
416 {
417 .name = "default",
418 .type = AVMEDIA_TYPE_AUDIO,
419 .request_frame = request_frame,
420 },
421 };
422
423 const AVFilter ff_af_alimiter = {
424 .name = "alimiter",
425 .description = NULL_IF_CONFIG_SMALL("Audio lookahead limiter."),
426 .priv_size = sizeof(AudioLimiterContext),
427 .priv_class = &alimiter_class,
428 .init = init,
429 .uninit = uninit,
430 FILTER_INPUTS(alimiter_inputs),
431 FILTER_OUTPUTS(alimiter_outputs),
432 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
433 .process_command = ff_filter_process_command,
434 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
435 };
436