2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
24 * Lookahead limiter filter
27 #include "libavutil/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
37 typedef struct AudioLimiterContext {
60 } AudioLimiterContext;
62 #define OFFSET(x) offsetof(AudioLimiterContext, x)
63 #define A AV_OPT_FLAG_AUDIO_PARAM
64 #define F AV_OPT_FLAG_FILTERING_PARAM
66 static const AVOption alimiter_options[] = {
67 { "limit", "set limit", OFFSET(limit), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0625, 1, A|F },
68 { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=5}, 0.1, 80, A|F },
69 { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 8000, A|F },
70 { "asc", "enable asc", OFFSET(auto_release), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A|F },
71 { "asc_level", "set asc level", OFFSET(asc_coeff), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, A|F },
75 AVFILTER_DEFINE_CLASS(alimiter);
77 static av_cold int init(AVFilterContext *ctx)
79 AudioLimiterContext *s = ctx->priv;
85 s->asc_coeff = pow(0.5, s->asc_coeff - 0.5) * 2 * -1;
90 static double get_rdelta(AudioLimiterContext *s, double release, int sample_rate,
91 double peak, double limit, double patt, int asc)
93 double rdelta = (1.0 - patt) / (sample_rate * release);
95 if (asc && s->auto_release && s->asc_c > 0) {
96 double a_att = limit / (s->asc_coeff * s->asc) * (double)s->asc_c;
99 double delta = FFMAX((a_att - patt) / (sample_rate * release), rdelta / 10);
109 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
111 AVFilterContext *ctx = inlink->dst;
112 AudioLimiterContext *s = ctx->priv;
113 AVFilterLink *outlink = ctx->outputs[0];
114 const double *src = (const double *)in->data[0];
115 const int channels = inlink->channels;
116 const int buffer_size = s->buffer_size;
117 double *dst, *buffer = s->buffer;
118 const double release = s->release;
119 const double limit = s->limit;
120 double *nextdelta = s->nextdelta;
121 int *nextpos = s->nextpos;
126 if (av_frame_is_writable(in)) {
129 out = ff_get_audio_buffer(inlink, in->nb_samples);
132 return AVERROR(ENOMEM);
134 av_frame_copy_props(out, in);
136 dst = (double *)out->data[0];
138 for (n = 0; n < in->nb_samples; n++) {
141 for (c = 0; c < channels; c++) {
142 double sample = src[c];
144 buffer[s->pos + c] = sample;
145 peak = FFMAX(peak, fabs(sample));
148 if (s->auto_release && peak > limit) {
154 double patt = FFMIN(limit / peak, 1.);
155 double rdelta = get_rdelta(s, release, inlink->sample_rate,
156 peak, limit, patt, 0);
157 double delta = (limit / peak - s->att) / buffer_size * channels;
160 if (delta < s->delta) {
164 nextdelta[0] = rdelta;
168 for (i = s->nextiter; i < s->nextiter + s->nextlen; i++) {
169 int j = i % buffer_size;
170 double ppeak, pdelta;
172 ppeak = fabs(buffer[nextpos[j]]) > fabs(buffer[nextpos[j] + 1]) ?
173 fabs(buffer[nextpos[j]]) : fabs(buffer[nextpos[j] + 1]);
174 pdelta = (limit / peak - limit / ppeak) / (((buffer_size - nextpos[j] + s->pos) % buffer_size) / channels);
175 if (pdelta < nextdelta[j]) {
176 nextdelta[j] = pdelta;
182 s->nextlen = i - s->nextiter + 1;
183 nextpos[(s->nextiter + s->nextlen) % buffer_size] = s->pos;
184 nextdelta[(s->nextiter + s->nextlen) % buffer_size] = rdelta;
185 nextpos[(s->nextiter + s->nextlen + 1) % buffer_size] = -1;
191 buf = &s->buffer[(s->pos + channels) % buffer_size];
193 for (c = 0; c < channels; c++) {
194 double sample = buf[c];
196 peak = FFMAX(peak, fabs(sample));
199 if (s->pos == s->asc_pos && !s->asc_changed)
202 if (s->auto_release && s->asc_pos == -1 && peak > limit) {
209 for (c = 0; c < channels; c++)
210 dst[c] = buf[c] * s->att;
212 if ((s->pos + channels) % buffer_size == nextpos[s->nextiter]) {
213 if (s->auto_release) {
214 s->delta = get_rdelta(s, release, inlink->sample_rate,
215 peak, limit, s->att, 1);
216 if (s->nextlen > 1) {
217 int pnextpos = nextpos[(s->nextiter + 1) % buffer_size];
218 double ppeak = fabs(buffer[pnextpos]) > fabs(buffer[pnextpos + 1]) ?
219 fabs(buffer[pnextpos]) :
220 fabs(buffer[pnextpos + 1]);
221 double pdelta = (limit / ppeak - s->att) /
222 (((buffer_size + pnextpos -
223 ((s->pos + channels) % buffer_size)) %
224 buffer_size) / channels);
225 if (pdelta < s->delta)
229 s->delta = nextdelta[s->nextiter];
230 s->att = limit / peak;
234 nextpos[s->nextiter] = -1;
235 s->nextiter = (s->nextiter + 1) % buffer_size;
247 s->att = 0.0000000000001;
248 s->delta = (1.0 - s->att) / (inlink->sample_rate * release);
251 if (s->att != 1. && (1. - s->att) < 0.0000000000001)
254 if (s->delta != 0. && fabs(s->delta) < 0.00000000000001)
257 for (c = 0; c < channels; c++)
258 dst[c] = av_clipd(dst[c], -limit, limit);
260 s->pos = (s->pos + channels) % buffer_size;
268 return ff_filter_frame(outlink, out);
271 static int query_formats(AVFilterContext *ctx)
273 AVFilterFormats *formats;
274 AVFilterChannelLayouts *layouts;
275 static const enum AVSampleFormat sample_fmts[] = {
281 layouts = ff_all_channel_counts();
283 return AVERROR(ENOMEM);
284 ret = ff_set_common_channel_layouts(ctx, layouts);
288 formats = ff_make_format_list(sample_fmts);
290 return AVERROR(ENOMEM);
291 ret = ff_set_common_formats(ctx, formats);
295 formats = ff_all_samplerates();
297 return AVERROR(ENOMEM);
298 return ff_set_common_samplerates(ctx, formats);
301 static int config_input(AVFilterLink *inlink)
303 AVFilterContext *ctx = inlink->dst;
304 AudioLimiterContext *s = ctx->priv;
307 obuffer_size = inlink->sample_rate * inlink->channels * 100 / 1000. + inlink->channels;
308 if (obuffer_size < inlink->channels)
309 return AVERROR(EINVAL);
311 s->buffer = av_calloc(obuffer_size, sizeof(*s->buffer));
312 s->nextdelta = av_calloc(obuffer_size, sizeof(*s->nextdelta));
313 s->nextpos = av_malloc_array(obuffer_size, sizeof(*s->nextpos));
314 if (!s->buffer || !s->nextdelta || !s->nextpos)
315 return AVERROR(ENOMEM);
317 memset(s->nextpos, -1, obuffer_size * sizeof(*s->nextpos));
318 s->buffer_size = inlink->sample_rate * s->attack * inlink->channels;
319 s->buffer_size -= s->buffer_size % inlink->channels;
324 static av_cold void uninit(AVFilterContext *ctx)
326 AudioLimiterContext *s = ctx->priv;
328 av_freep(&s->buffer);
329 av_freep(&s->nextdelta);
330 av_freep(&s->nextpos);
333 static const AVFilterPad alimiter_inputs[] = {
336 .type = AVMEDIA_TYPE_AUDIO,
337 .filter_frame = filter_frame,
338 .config_props = config_input,
343 static const AVFilterPad alimiter_outputs[] = {
346 .type = AVMEDIA_TYPE_AUDIO,
351 AVFilter ff_af_alimiter = {
353 .description = NULL_IF_CONFIG_SMALL("Lookahead limiter."),
354 .priv_size = sizeof(AudioLimiterContext),
355 .priv_class = &alimiter_class,
358 .query_formats = query_formats,
359 .inputs = alimiter_inputs,
360 .outputs = alimiter_outputs,