• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
init(AVFilterContext * ctx)96 static av_cold int init(AVFilterContext *ctx)
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 
get_rdelta(AudioLimiterContext * s,double release,int sample_rate,double peak,double limit,double patt,int asc)109 static 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 
filter_frame(AVFilterLink * inlink,AVFrame * in)128 static 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         }
161         av_frame_copy_props(out, in);
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, pdelta;
198 
199                     ppeak = fabs(buffer[nextpos[j]]) > fabs(buffer[nextpos[j] + 1]) ?
200                             fabs(buffer[nextpos[j]]) : fabs(buffer[nextpos[j] + 1]);
201                     pdelta = (limit / peak - limit / ppeak) / (((buffer_size - nextpos[j] + s->pos) % buffer_size) / channels);
202                     if (pdelta < nextdelta[j]) {
203                         nextdelta[j] = pdelta;
204                         found = 1;
205                         break;
206                     }
207                 }
208                 if (found) {
209                     s->nextlen = i - s->nextiter + 1;
210                     nextpos[(s->nextiter + s->nextlen) % buffer_size] = s->pos;
211                     nextdelta[(s->nextiter + s->nextlen) % buffer_size] = rdelta;
212                     nextpos[(s->nextiter + s->nextlen + 1) % buffer_size] = -1;
213                     s->nextlen++;
214                 }
215             }
216         }
217 
218         buf = &s->buffer[(s->pos + channels) % buffer_size];
219         peak = 0;
220         for (c = 0; c < channels; c++) {
221             double sample = buf[c];
222 
223             peak = FFMAX(peak, fabs(sample));
224         }
225 
226         if (s->pos == s->asc_pos && !s->asc_changed)
227             s->asc_pos = -1;
228 
229         if (s->auto_release && s->asc_pos == -1 && peak > limit) {
230             s->asc -= peak;
231             s->asc_c--;
232         }
233 
234         s->att += s->delta;
235 
236         for (c = 0; c < channels; c++)
237             dst[c] = buf[c] * s->att;
238 
239         if ((s->pos + channels) % buffer_size == nextpos[s->nextiter]) {
240             if (s->auto_release) {
241                 s->delta = get_rdelta(s, release, inlink->sample_rate,
242                                       peak, limit, s->att, 1);
243                 if (s->nextlen > 1) {
244                     int pnextpos = nextpos[(s->nextiter + 1) % buffer_size];
245                     double ppeak = fabs(buffer[pnextpos]) > fabs(buffer[pnextpos + 1]) ?
246                                                             fabs(buffer[pnextpos]) :
247                                                             fabs(buffer[pnextpos + 1]);
248                     double pdelta = (limit / ppeak - s->att) /
249                                     (((buffer_size + pnextpos -
250                                     ((s->pos + channels) % buffer_size)) %
251                                     buffer_size) / channels);
252                     if (pdelta < s->delta)
253                         s->delta = pdelta;
254                 }
255             } else {
256                 s->delta = nextdelta[s->nextiter];
257                 s->att = limit / peak;
258             }
259 
260             s->nextlen -= 1;
261             nextpos[s->nextiter] = -1;
262             s->nextiter = (s->nextiter + 1) % buffer_size;
263         }
264 
265         if (s->att > 1.) {
266             s->att = 1.;
267             s->delta = 0.;
268             s->nextiter = 0;
269             s->nextlen = 0;
270             nextpos[0] = -1;
271         }
272 
273         if (s->att <= 0.) {
274             s->att = 0.0000000000001;
275             s->delta = (1.0 - s->att) / (inlink->sample_rate * release);
276         }
277 
278         if (s->att != 1. && (1. - s->att) < 0.0000000000001)
279             s->att = 1.;
280 
281         if (s->delta != 0. && fabs(s->delta) < 0.00000000000001)
282             s->delta = 0.;
283 
284         for (c = 0; c < channels; c++)
285             dst[c] = av_clipd(dst[c], -limit, limit) * level * level_out;
286 
287         s->pos = (s->pos + channels) % buffer_size;
288         src += channels;
289         dst += channels;
290     }
291 
292     in_duration = av_rescale_q(in->nb_samples,  inlink->time_base, av_make_q(1,  in->sample_rate));
293     in_pts = in->pts;
294     meta = (MetaItem){ in->pts, in->nb_samples };
295     av_fifo_write(s->fifo, &meta, 1);
296     if (in != out)
297         av_frame_free(&in);
298 
299     new_out_samples = out->nb_samples;
300     if (s->in_trim > 0) {
301         int trim = FFMIN(new_out_samples, s->in_trim);
302         new_out_samples -= trim;
303         s->in_trim -= trim;
304     }
305 
306     if (new_out_samples <= 0) {
307         av_frame_free(&out);
308         return 0;
309     } else if (new_out_samples < out->nb_samples) {
310         int offset = out->nb_samples - new_out_samples;
311         memmove(out->extended_data[0], out->extended_data[0] + sizeof(double) * offset * out->ch_layout.nb_channels,
312                 sizeof(double) * new_out_samples * out->ch_layout.nb_channels);
313         out->nb_samples = new_out_samples;
314         s->in_trim = 0;
315     }
316 
317     av_fifo_read(s->fifo, &meta, 1);
318 
319     out_duration = av_rescale_q(out->nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
320     in_duration  = av_rescale_q(meta.nb_samples, inlink->time_base, av_make_q(1, out->sample_rate));
321     in_pts       = meta.pts;
322 
323     if (s->next_out_pts != AV_NOPTS_VALUE && out->pts != s->next_out_pts &&
324         s->next_in_pts  != AV_NOPTS_VALUE && in_pts   == s->next_in_pts) {
325         out->pts = s->next_out_pts;
326     } else {
327         out->pts = in_pts;
328     }
329     s->next_in_pts  = in_pts   + in_duration;
330     s->next_out_pts = out->pts + out_duration;
331 
332     return ff_filter_frame(outlink, out);
333 }
334 
request_frame(AVFilterLink * outlink)335 static int request_frame(AVFilterLink* outlink)
336 {
337     AVFilterContext *ctx = outlink->src;
338     AudioLimiterContext *s = (AudioLimiterContext*)ctx->priv;
339     int ret;
340 
341     ret = ff_request_frame(ctx->inputs[0]);
342 
343     if (ret == AVERROR_EOF && s->out_pad > 0) {
344         AVFrame *frame = ff_get_audio_buffer(outlink, FFMIN(1024, s->out_pad));
345         if (!frame)
346             return AVERROR(ENOMEM);
347 
348         s->out_pad -= frame->nb_samples;
349         frame->pts = s->next_in_pts;
350         return filter_frame(ctx->inputs[0], frame);
351     }
352     return ret;
353 }
354 
config_input(AVFilterLink * inlink)355 static int config_input(AVFilterLink *inlink)
356 {
357     AVFilterContext *ctx = inlink->dst;
358     AudioLimiterContext *s = ctx->priv;
359     int obuffer_size;
360 
361     obuffer_size = inlink->sample_rate * inlink->ch_layout.nb_channels * 100 / 1000. + inlink->ch_layout.nb_channels;
362     if (obuffer_size < inlink->ch_layout.nb_channels)
363         return AVERROR(EINVAL);
364 
365     s->buffer = av_calloc(obuffer_size, sizeof(*s->buffer));
366     s->nextdelta = av_calloc(obuffer_size, sizeof(*s->nextdelta));
367     s->nextpos = av_malloc_array(obuffer_size, sizeof(*s->nextpos));
368     if (!s->buffer || !s->nextdelta || !s->nextpos)
369         return AVERROR(ENOMEM);
370 
371     memset(s->nextpos, -1, obuffer_size * sizeof(*s->nextpos));
372     s->buffer_size = inlink->sample_rate * s->attack * inlink->ch_layout.nb_channels;
373     s->buffer_size -= s->buffer_size % inlink->ch_layout.nb_channels;
374     if (s->latency)
375         s->in_trim = s->out_pad = s->buffer_size / inlink->ch_layout.nb_channels - 1;
376     s->next_out_pts = AV_NOPTS_VALUE;
377     s->next_in_pts  = AV_NOPTS_VALUE;
378 
379     s->fifo = av_fifo_alloc2(8, sizeof(MetaItem), AV_FIFO_FLAG_AUTO_GROW);
380     if (!s->fifo) {
381         return AVERROR(ENOMEM);
382     }
383 
384     if (s->buffer_size <= 0) {
385         av_log(ctx, AV_LOG_ERROR, "Attack is too small.\n");
386         return AVERROR(EINVAL);
387     }
388 
389     return 0;
390 }
391 
uninit(AVFilterContext * ctx)392 static av_cold void uninit(AVFilterContext *ctx)
393 {
394     AudioLimiterContext *s = ctx->priv;
395 
396     av_freep(&s->buffer);
397     av_freep(&s->nextdelta);
398     av_freep(&s->nextpos);
399 
400     av_fifo_freep2(&s->fifo);
401 }
402 
403 static const AVFilterPad alimiter_inputs[] = {
404     {
405         .name         = "main",
406         .type         = AVMEDIA_TYPE_AUDIO,
407         .filter_frame = filter_frame,
408         .config_props = config_input,
409     },
410 };
411 
412 static const AVFilterPad alimiter_outputs[] = {
413     {
414         .name = "default",
415         .type = AVMEDIA_TYPE_AUDIO,
416         .request_frame = request_frame,
417     },
418 };
419 
420 const AVFilter ff_af_alimiter = {
421     .name           = "alimiter",
422     .description    = NULL_IF_CONFIG_SMALL("Audio lookahead limiter."),
423     .priv_size      = sizeof(AudioLimiterContext),
424     .priv_class     = &alimiter_class,
425     .init           = init,
426     .uninit         = uninit,
427     FILTER_INPUTS(alimiter_inputs),
428     FILTER_OUTPUTS(alimiter_outputs),
429     FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
430     .process_command = ff_filter_process_command,
431     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
432 };
433