FFmpeg
Loading...
Searching...
No Matches
af_alimiter.c
Go to the documentation of this file.
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
28#include "libavutil/common.h"
29#include "libavutil/fifo.h"
30#include "libavutil/mem.h"
31#include "libavutil/opt.h"
32
33#include "audio.h"
34#include "avfilter.h"
35#include "filters.h"
36
37typedef struct MetaItem {
40} MetaItem;
41
42typedef 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;
53 double asc;
54 int asc_c;
56 double asc_coeff;
57
58 double *buffer;
60 int pos;
61 int *nextpos;
62 double *nextdelta;
63
69
71
72 double delta;
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
81static 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
95
97{
98 AudioLimiterContext *s = ctx->priv;
99
100 s->attack /= 1000.;
101 s->release /= 1000.;
102 s->att = 1.;
103 s->asc_pos = -1;
104 s->asc_coeff = pow(0.5, s->asc_coeff - 0.5) * 2 * -1;
105
106 return 0;
107}
108
109static double get_rdelta(AudioLimiterContext *s, double release, int sample_rate,
110 double peak, double limit, double patt, int asc)
111{
112 double rdelta = (1.0 - patt) / (sample_rate * release);
113
114 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 return rdelta;
126}
127
128static int filter_frame(AVFilterLink *inlink, AVFrame *in)
129{
130 AVFilterContext *ctx = inlink->dst;
131 AudioLimiterContext *s = ctx->priv;
132 AVFilterLink *outlink = ctx->outputs[0];
133 const double *src = (const double *)in->data[0];
134 const int channels = inlink->ch_layout.nb_channels;
135 const int buffer_size = s->buffer_size;
136 double *dst, *buffer = s->buffer;
137 const double release = s->release;
138 const double limit = s->limit;
139 double *nextdelta = s->nextdelta;
140 double level = s->auto_level ? 1 / limit : 1;
141 const double level_out = s->level_out;
142 const double level_in = s->level_in;
143 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 if (av_frame_is_writable(in)) {
154 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 }
162 }
163 dst = (double *)out->data[0];
164
165 for (n = 0; n < in->nb_samples; n++) {
166 double peak = 0;
167
168 for (c = 0; c < channels; c++) {
169 double sample = src[c] * level_in;
170
171 buffer[s->pos + c] = sample;
172 peak = FFMAX(peak, fabs(sample));
173 }
174
175 if (s->auto_release && peak > limit) {
176 s->asc += peak;
177 s->asc_c++;
178 }
179
180 if (peak > limit) {
181 double patt = FFMIN(limit / peak, 1.);
182 double rdelta = get_rdelta(s, release, inlink->sample_rate,
183 peak, limit, patt, 0);
184 double delta = (limit / peak - s->att) / buffer_size * channels;
185 int found = 0;
186
187 if (delta < s->delta) {
188 s->delta = delta;
189 nextpos[0] = s->pos;
190 nextpos[1] = -1;
191 nextdelta[0] = rdelta;
192 s->nextlen = 1;
193 s->nextiter= 0;
194 } else {
195 for (i = s->nextiter; i < s->nextiter + s->nextlen; i++) {
196 int j = i % buffer_size;
197 double ppeak = 0, pdelta;
198
199 if (nextpos[j] >= 0)
200 for (c = 0; c < channels; c++) {
201 ppeak = FFMAX(ppeak, fabs(buffer[nextpos[j] + c]));
202 }
203 pdelta = (limit / peak - limit / ppeak) / (((buffer_size - nextpos[j] + s->pos) % buffer_size) / channels);
204 if (pdelta < nextdelta[j]) {
205 nextdelta[j] = pdelta;
206 found = 1;
207 break;
208 }
209 }
210 if (found) {
211 s->nextlen = i - s->nextiter + 1;
212 nextpos[(s->nextiter + s->nextlen) % buffer_size] = s->pos;
213 nextdelta[(s->nextiter + s->nextlen) % buffer_size] = rdelta;
214 nextpos[(s->nextiter + s->nextlen + 1) % buffer_size] = -1;
215 s->nextlen++;
216 }
217 }
218 }
219
220 buf = &s->buffer[(s->pos + channels) % buffer_size];
221 peak = 0;
222 for (c = 0; c < channels; c++) {
223 double sample = buf[c];
224
225 peak = FFMAX(peak, fabs(sample));
226 }
227
228 if (s->pos == s->asc_pos && !s->asc_changed)
229 s->asc_pos = -1;
230
231 if (s->auto_release && s->asc_pos == -1 && peak > limit) {
232 s->asc -= peak;
233 s->asc_c--;
234 }
235
236 s->att += s->delta;
237
238 for (c = 0; c < channels; c++)
239 dst[c] = buf[c] * s->att;
240
241 if ((s->pos + channels) % buffer_size == nextpos[s->nextiter]) {
242 if (s->auto_release) {
243 s->delta = get_rdelta(s, release, inlink->sample_rate,
244 peak, limit, s->att, 1);
245 if (s->nextlen > 1) {
246 double ppeak = 0, pdelta;
247 int pnextpos = nextpos[(s->nextiter + 1) % buffer_size];
248
249 for (c = 0; c < channels; c++) {
250 ppeak = FFMAX(ppeak, fabs(buffer[pnextpos + c]));
251 }
252 pdelta = (limit / ppeak - s->att) /
253 (((buffer_size + pnextpos -
254 ((s->pos + channels) % buffer_size)) %
255 buffer_size) / channels);
256 if (pdelta < s->delta)
257 s->delta = pdelta;
258 }
259 } else {
260 s->delta = nextdelta[s->nextiter];
261 s->att = limit / peak;
262 }
263
264 s->nextlen -= 1;
265 nextpos[s->nextiter] = -1;
266 s->nextiter = (s->nextiter + 1) % buffer_size;
267 }
268
269 if (s->att > 1.) {
270 s->att = 1.;
271 s->delta = 0.;
272 s->nextiter = 0;
273 s->nextlen = 0;
274 nextpos[0] = -1;
275 }
276
277 if (s->att <= 0.) {
278 s->att = 0.0000000000001;
279 s->delta = (1.0 - s->att) / (inlink->sample_rate * release);
280 }
281
282 if (s->att != 1. && (1. - s->att) < 0.0000000000001)
283 s->att = 1.;
284
285 if (s->delta != 0. && fabs(s->delta) < 0.00000000000001)
286 s->delta = 0.;
287
288 for (c = 0; c < channels; c++)
289 dst[c] = av_clipd(dst[c], -limit, limit) * level * level_out;
290
291 s->pos = (s->pos + channels) % buffer_size;
292 src += channels;
293 dst += channels;
294 }
295
296 in_duration = av_rescale_q(in->nb_samples, inlink->time_base, av_make_q(1, in->sample_rate));
297 in_pts = in->pts;
298 meta = (MetaItem){ in->pts, in->nb_samples };
299 av_fifo_write(s->fifo, &meta, 1);
300 if (in != out)
301 av_frame_free(&in);
302
303 new_out_samples = out->nb_samples;
304 if (s->in_trim > 0) {
305 int trim = FFMIN(new_out_samples, s->in_trim);
306 new_out_samples -= trim;
307 s->in_trim -= trim;
308 }
309
310 if (new_out_samples <= 0) {
312 return 0;
313 } else if (new_out_samples < out->nb_samples) {
314 int offset = out->nb_samples - new_out_samples;
315 memmove(out->extended_data[0], out->extended_data[0] + sizeof(double) * offset * out->ch_layout.nb_channels,
316 sizeof(double) * new_out_samples * out->ch_layout.nb_channels);
317 out->nb_samples = new_out_samples;
318 s->in_trim = 0;
319 }
320
321 av_fifo_read(s->fifo, &meta, 1);
322
323 out_duration = av_rescale_q(out->nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
324 in_duration = av_rescale_q(meta.nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
325 in_pts = meta.pts;
326
327 if (s->next_out_pts != AV_NOPTS_VALUE && out->pts != s->next_out_pts &&
328 s->next_in_pts != AV_NOPTS_VALUE && in_pts == s->next_in_pts) {
329 out->pts = s->next_out_pts;
330 } else {
331 out->pts = in_pts;
332 }
333 s->next_in_pts = in_pts + in_duration;
334 s->next_out_pts = out->pts + out_duration;
335
336 return ff_filter_frame(outlink, out);
337}
338
339static int request_frame(AVFilterLink* outlink)
340{
341 AVFilterContext *ctx = outlink->src;
343 int ret;
344
345 ret = ff_request_frame(ctx->inputs[0]);
346
347 if (ret == AVERROR_EOF && s->out_pad > 0) {
348 AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(1024, s->out_pad));
349 if (!frame)
350 return AVERROR(ENOMEM);
351
352 s->out_pad -= frame->nb_samples;
353 frame->pts = s->next_in_pts;
354 return filter_frame(ctx->inputs[0], frame);
355 }
356 return ret;
357}
358
359static int config_input(AVFilterLink *inlink)
360{
361 AVFilterContext *ctx = inlink->dst;
362 AudioLimiterContext *s = ctx->priv;
363 int obuffer_size;
364
365 obuffer_size = inlink->sample_rate * inlink->ch_layout.nb_channels * 100 / 1000. + inlink->ch_layout.nb_channels;
366 if (obuffer_size < inlink->ch_layout.nb_channels)
367 return AVERROR(EINVAL);
368
369 s->buffer = av_calloc(obuffer_size, sizeof(*s->buffer));
370 s->nextdelta = av_calloc(obuffer_size, sizeof(*s->nextdelta));
371 s->nextpos = av_malloc_array(obuffer_size, sizeof(*s->nextpos));
372 if (!s->buffer || !s->nextdelta || !s->nextpos)
373 return AVERROR(ENOMEM);
374
375 memset(s->nextpos, -1, obuffer_size * sizeof(*s->nextpos));
376 s->buffer_size = inlink->sample_rate * s->attack * inlink->ch_layout.nb_channels;
377 s->buffer_size -= s->buffer_size % inlink->ch_layout.nb_channels;
378 if (s->latency)
379 s->in_trim = s->out_pad = s->buffer_size / inlink->ch_layout.nb_channels - 1;
380 s->next_out_pts = AV_NOPTS_VALUE;
381 s->next_in_pts = AV_NOPTS_VALUE;
382
383 s->fifo = av_fifo_alloc2(8, sizeof(MetaItem), AV_FIFO_FLAG_AUTO_GROW);
384 if (!s->fifo) {
385 return AVERROR(ENOMEM);
386 }
387
388 if (s->buffer_size <= 0) {
389 av_log(ctx, AV_LOG_ERROR, "Attack is too small.\n");
390 return AVERROR(EINVAL);
391 }
392
393 return 0;
394}
395
397{
398 AudioLimiterContext *s = ctx->priv;
399
400 av_freep(&s->buffer);
401 av_freep(&s->nextdelta);
402 av_freep(&s->nextpos);
403
404 av_fifo_freep2(&s->fifo);
405}
406
407static const AVFilterPad alimiter_inputs[] = {
408 {
409 .name = "main",
410 .type = AVMEDIA_TYPE_AUDIO,
411 .filter_frame = filter_frame,
412 .config_props = config_input,
413 },
414};
415
417 {
418 .name = "default",
419 .type = AVMEDIA_TYPE_AUDIO,
420 .request_frame = request_frame,
421 },
422};
423
425 .p.name = "alimiter",
426 .p.description = NULL_IF_CONFIG_SMALL("Audio lookahead limiter."),
427 .p.priv_class = &alimiter_class,
429 .priv_size = sizeof(AudioLimiterContext),
430 .init = init,
431 .uninit = uninit,
435 .process_command = ff_filter_process_command,
436};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
#define AF
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
static const AVFilterPad alimiter_inputs[]
static double get_rdelta(AudioLimiterContext *s, double release, int sample_rate, double peak, double limit, double patt, int asc)
static int config_input(AVFilterLink *inlink)
static const AVFilterPad alimiter_outputs[]
static int request_frame(AVFilterLink *outlink)
static const AVOption alimiter_options[]
Definition af_alimiter.c:81
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
Definition af_alimiter.c:78
const FFFilter ff_af_alimiter
channels
Definition aptx.h:31
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
common internal and external API header
#define av_clipd
Definition common.h:148
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabs(float a)
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
A generic FIFO API.
#define sample
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition fifo.c:47
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition fifo.h:63
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition fifo.c:240
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
static av_cold void uninit(AVBitStreamFilterContext *ctx)
unsigned offset
Definition libaomenc.c:763
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
Definition fifo.c:35
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int sample_rate
Sample rate of the audio data.
Definition frame.h:635
AVOption.
Definition opt.h:428
int nb_samples
Definition af_alimiter.c:39
int64_t pts
Definition af_alimiter.c:38
uint8_t level
Definition svq3.c:208
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
static const int8_t patt[4]
Definition vf_noise.c:67
static double limit(double x)
float delta
static double c[64]