lavfi/formats: avoid using AV_{PIX,SAMPLE}_FMT_NB
[platform/upstream/libav.git] / libavfilter / af_compand.c
1 /*
2  * Copyright (c) 1999 Chris Bagwell
3  * Copyright (c) 1999 Nick Bailey
4  * Copyright (c) 2007 Rob Sykes <robs@users.sourceforge.net>
5  * Copyright (c) 2013 Paul B Mahol
6  * Copyright (c) 2014 Andrew Kelley
7  *
8  * This file is part of libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /**
26  * @file
27  * audio compand filter
28  */
29
30 #include <string.h>
31
32 #include "libavutil/avstring.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/common.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
38 #include "audio.h"
39 #include "avfilter.h"
40 #include "formats.h"
41 #include "internal.h"
42
43 typedef struct ChanParam {
44     float attack;
45     float decay;
46     float volume;
47 } ChanParam;
48
49 typedef struct CompandSegment {
50     float x, y;
51     float a, b;
52 } CompandSegment;
53
54 typedef struct CompandContext {
55     const AVClass *class;
56     int nb_channels;
57     int nb_segments;
58     char *attacks, *decays, *points;
59     CompandSegment *segments;
60     ChanParam *channels;
61     float in_min_lin;
62     float out_min_lin;
63     double curve_dB;
64     double gain_dB;
65     double initial_volume;
66     double delay;
67     AVFrame *delay_frame;
68     int delay_samples;
69     int delay_count;
70     int delay_index;
71     int64_t pts;
72
73     int (*compand)(AVFilterContext *ctx, AVFrame *frame);
74 } CompandContext;
75
76 #define OFFSET(x) offsetof(CompandContext, x)
77 #define A AV_OPT_FLAG_AUDIO_PARAM
78
79 static const AVOption compand_options[] = {
80     { "attacks", "set time over which increase of volume is determined", OFFSET(attacks), AV_OPT_TYPE_STRING, { .str = "0.3" }, 0, 0, A },
81     { "decays", "set time over which decrease of volume is determined", OFFSET(decays), AV_OPT_TYPE_STRING, { .str = "0.8" }, 0, 0, A },
82     { "points", "set points of transfer function", OFFSET(points), AV_OPT_TYPE_STRING, { .str = "-70/-70|-60/-20" }, 0, 0, A },
83     { "soft-knee", "set soft-knee", OFFSET(curve_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.01, 900, A },
84     { "gain", "set output gain", OFFSET(gain_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 900, A },
85     { "volume", "set initial volume", OFFSET(initial_volume), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 0, A },
86     { "delay", "set delay for samples before sending them to volume adjuster", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20, A },
87     { NULL }
88 };
89
90 static const AVClass compand_class = {
91     .class_name = "compand filter",
92     .item_name  = av_default_item_name,
93     .option     = compand_options,
94     .version    = LIBAVUTIL_VERSION_INT,
95 };
96
97 static av_cold int init(AVFilterContext *ctx)
98 {
99     CompandContext *s = ctx->priv;
100     s->pts            = AV_NOPTS_VALUE;
101     return 0;
102 }
103
104 static av_cold void uninit(AVFilterContext *ctx)
105 {
106     CompandContext *s = ctx->priv;
107
108     av_freep(&s->channels);
109     av_freep(&s->segments);
110     av_frame_free(&s->delay_frame);
111 }
112
113 static int query_formats(AVFilterContext *ctx)
114 {
115     AVFilterChannelLayouts *layouts;
116     AVFilterFormats *formats;
117     static const enum AVSampleFormat sample_fmts[] = {
118         AV_SAMPLE_FMT_FLTP,
119         AV_SAMPLE_FMT_NONE
120     };
121
122     layouts = ff_all_channel_layouts();
123     if (!layouts)
124         return AVERROR(ENOMEM);
125     ff_set_common_channel_layouts(ctx, layouts);
126
127     formats = ff_make_format_list(sample_fmts);
128     if (!formats)
129         return AVERROR(ENOMEM);
130     ff_set_common_formats(ctx, formats);
131
132     formats = ff_all_samplerates();
133     if (!formats)
134         return AVERROR(ENOMEM);
135     ff_set_common_samplerates(ctx, formats);
136
137     return 0;
138 }
139
140 static void count_items(char *item_str, int *nb_items)
141 {
142     char *p;
143
144     *nb_items = 1;
145     for (p = item_str; *p; p++) {
146         if (*p == '|')
147             (*nb_items)++;
148     }
149 }
150
151 static void update_volume(ChanParam *cp, float in)
152 {
153     float delta = in - cp->volume;
154
155     if (delta > 0.0)
156         cp->volume += delta * cp->attack;
157     else
158         cp->volume += delta * cp->decay;
159 }
160
161 static float get_volume(CompandContext *s, float in_lin)
162 {
163     CompandSegment *cs;
164     float in_log, out_log;
165     int i;
166
167     if (in_lin < s->in_min_lin)
168         return s->out_min_lin;
169
170     in_log = logf(in_lin);
171
172     for (i = 1; i < s->nb_segments; i++)
173         if (in_log <= s->segments[i].x)
174             break;
175     cs = &s->segments[i - 1];
176     in_log -= cs->x;
177     out_log = cs->y + in_log * (cs->a * in_log + cs->b);
178
179     return expf(out_log);
180 }
181
182 static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
183 {
184     CompandContext *s    = ctx->priv;
185     AVFilterLink *inlink = ctx->inputs[0];
186     const int channels   = s->nb_channels;
187     const int nb_samples = frame->nb_samples;
188     AVFrame *out_frame;
189     int chan, i;
190     int err;
191
192     if (av_frame_is_writable(frame)) {
193         out_frame = frame;
194     } else {
195         out_frame = ff_get_audio_buffer(inlink, nb_samples);
196         if (!out_frame) {
197             av_frame_free(&frame);
198             return AVERROR(ENOMEM);
199         }
200         err = av_frame_copy_props(out_frame, frame);
201         if (err < 0) {
202             av_frame_free(&out_frame);
203             av_frame_free(&frame);
204             return err;
205         }
206     }
207
208     for (chan = 0; chan < channels; chan++) {
209         const float *src = (float *)frame->extended_data[chan];
210         float *dst = (float *)out_frame->extended_data[chan];
211         ChanParam *cp = &s->channels[chan];
212
213         for (i = 0; i < nb_samples; i++) {
214             update_volume(cp, fabs(src[i]));
215
216             dst[i] = av_clipf(src[i] * get_volume(s, cp->volume), -1.0f, 1.0f);
217         }
218     }
219
220     if (frame != out_frame)
221         av_frame_free(&frame);
222
223     return ff_filter_frame(ctx->outputs[0], out_frame);
224 }
225
226 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
227
228 static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
229 {
230     CompandContext *s    = ctx->priv;
231     AVFilterLink *inlink = ctx->inputs[0];
232     const int channels   = s->nb_channels;
233     const int nb_samples = frame->nb_samples;
234     int chan, i, dindex  = 0, oindex, count = 0;
235     AVFrame *out_frame   = NULL;
236     int err;
237
238     if (s->pts == AV_NOPTS_VALUE) {
239         s->pts = (frame->pts == AV_NOPTS_VALUE) ? 0 : frame->pts;
240     }
241
242     for (chan = 0; chan < channels; chan++) {
243         AVFrame *delay_frame = s->delay_frame;
244         const float *src     = (float *)frame->extended_data[chan];
245         float *dbuf          = (float *)delay_frame->extended_data[chan];
246         ChanParam *cp        = &s->channels[chan];
247         float *dst;
248
249         count  = s->delay_count;
250         dindex = s->delay_index;
251         for (i = 0, oindex = 0; i < nb_samples; i++) {
252             const float in = src[i];
253             update_volume(cp, fabs(in));
254
255             if (count >= s->delay_samples) {
256                 if (!out_frame) {
257                     out_frame = ff_get_audio_buffer(inlink, nb_samples - i);
258                     if (!out_frame) {
259                         av_frame_free(&frame);
260                         return AVERROR(ENOMEM);
261                     }
262                     err = av_frame_copy_props(out_frame, frame);
263                     if (err < 0) {
264                         av_frame_free(&out_frame);
265                         av_frame_free(&frame);
266                         return err;
267                     }
268                     out_frame->pts = s->pts;
269                     s->pts += av_rescale_q(nb_samples - i,
270                         (AVRational){ 1, inlink->sample_rate },
271                         inlink->time_base);
272                 }
273
274                 dst = (float *)out_frame->extended_data[chan];
275                 dst[oindex++] = av_clipf(dbuf[dindex] *
276                         get_volume(s, cp->volume), -1.0f, 1.0f);
277             } else {
278                 count++;
279             }
280
281             dbuf[dindex] = in;
282             dindex = MOD(dindex + 1, s->delay_samples);
283         }
284     }
285
286     s->delay_count = count;
287     s->delay_index = dindex;
288
289     av_frame_free(&frame);
290     return out_frame ? ff_filter_frame(ctx->outputs[0], out_frame) : 0;
291 }
292
293 static int compand_drain(AVFilterLink *outlink)
294 {
295     AVFilterContext *ctx = outlink->src;
296     CompandContext *s    = ctx->priv;
297     const int channels   = s->nb_channels;
298     AVFrame *frame       = NULL;
299     int chan, i, dindex;
300
301     /* 2048 is to limit output frame size during drain */
302     frame = ff_get_audio_buffer(outlink, FFMIN(2048, s->delay_count));
303     if (!frame)
304         return AVERROR(ENOMEM);
305     frame->pts = s->pts;
306     s->pts += av_rescale_q(frame->nb_samples,
307             (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
308
309     for (chan = 0; chan < channels; chan++) {
310         AVFrame *delay_frame = s->delay_frame;
311         float *dbuf = (float *)delay_frame->extended_data[chan];
312         float *dst = (float *)frame->extended_data[chan];
313         ChanParam *cp = &s->channels[chan];
314
315         dindex = s->delay_index;
316         for (i = 0; i < frame->nb_samples; i++) {
317             dst[i] = av_clipf(dbuf[dindex] * get_volume(s, cp->volume),
318                     -1.0f, 1.0f);
319             dindex = MOD(dindex + 1, s->delay_samples);
320         }
321     }
322     s->delay_count -= frame->nb_samples;
323     s->delay_index = dindex;
324
325     return ff_filter_frame(outlink, frame);
326 }
327
328 static int config_output(AVFilterLink *outlink)
329 {
330     AVFilterContext *ctx  = outlink->src;
331     CompandContext *s     = ctx->priv;
332     const int sample_rate = outlink->sample_rate;
333     double radius         = s->curve_dB * M_LN10 / 20.0;
334     const char *p;
335     const int channels    =
336         av_get_channel_layout_nb_channels(outlink->channel_layout);
337     int nb_attacks, nb_decays, nb_points;
338     int new_nb_items, num;
339     int i;
340     int err;
341
342
343     count_items(s->attacks, &nb_attacks);
344     count_items(s->decays, &nb_decays);
345     count_items(s->points, &nb_points);
346
347     if (channels <= 0) {
348         av_log(ctx, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
349         return AVERROR(EINVAL);
350     }
351
352     if (nb_attacks > channels || nb_decays > channels) {
353         av_log(ctx, AV_LOG_ERROR,
354                 "Number of attacks/decays bigger than number of channels.\n");
355         return AVERROR(EINVAL);
356     }
357
358     uninit(ctx);
359
360     s->nb_channels = channels;
361     s->channels = av_mallocz_array(channels, sizeof(*s->channels));
362     s->nb_segments = (nb_points + 4) * 2;
363     s->segments = av_mallocz_array(s->nb_segments, sizeof(*s->segments));
364
365     if (!s->channels || !s->segments) {
366         uninit(ctx);
367         return AVERROR(ENOMEM);
368     }
369
370     p = s->attacks;
371     for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
372         char *tstr = av_get_token(&p, "|");
373         if (!tstr)
374             return AVERROR(ENOMEM);
375
376         new_nb_items += sscanf(tstr, "%f", &s->channels[i].attack) == 1;
377         av_freep(&tstr);
378         if (s->channels[i].attack < 0) {
379             uninit(ctx);
380             return AVERROR(EINVAL);
381         }
382         if (*p)
383             p++;
384     }
385     nb_attacks = new_nb_items;
386
387     p = s->decays;
388     for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
389         char *tstr = av_get_token(&p, "|");
390         if (!tstr)
391             return AVERROR(ENOMEM);
392         new_nb_items += sscanf(tstr, "%f", &s->channels[i].decay) == 1;
393         av_freep(&tstr);
394         if (s->channels[i].decay < 0) {
395             uninit(ctx);
396             return AVERROR(EINVAL);
397         }
398         if (*p)
399             p++;
400     }
401     nb_decays = new_nb_items;
402
403     if (nb_attacks != nb_decays) {
404         av_log(ctx, AV_LOG_ERROR,
405                 "Number of attacks %d differs from number of decays %d.\n",
406                 nb_attacks, nb_decays);
407         uninit(ctx);
408         return AVERROR(EINVAL);
409     }
410
411 #define S(x) s->segments[2 * ((x) + 1)]
412     p = s->points;
413     for (i = 0, new_nb_items = 0; i < nb_points; i++) {
414         char *tstr = av_get_token(&p, "|");
415         if (!tstr)
416             return AVERROR(ENOMEM);
417
418         err = sscanf(tstr, "%f/%f", &S(i).x, &S(i).y);
419         av_freep(&tstr);
420         if (err != 2) {
421             av_log(ctx, AV_LOG_ERROR,
422                     "Invalid and/or missing input/output value.\n");
423             uninit(ctx);
424             return AVERROR(EINVAL);
425         }
426         if (i && S(i - 1).x > S(i).x) {
427             av_log(ctx, AV_LOG_ERROR,
428                     "Transfer function input values must be increasing.\n");
429             uninit(ctx);
430             return AVERROR(EINVAL);
431         }
432         S(i).y -= S(i).x;
433         av_log(ctx, AV_LOG_DEBUG, "%d: x=%f y=%f\n", i, S(i).x, S(i).y);
434         new_nb_items++;
435         if (*p)
436             p++;
437     }
438     num = new_nb_items;
439
440     /* Add 0,0 if necessary */
441     if (num == 0 || S(num - 1).x)
442         num++;
443
444 #undef S
445 #define S(x) s->segments[2 * (x)]
446     /* Add a tail off segment at the start */
447     S(0).x = S(1).x - 2 * s->curve_dB;
448     S(0).y = S(1).y;
449     num++;
450
451     /* Join adjacent colinear segments */
452     for (i = 2; i < num; i++) {
453         double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x);
454         double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x);
455         int j;
456
457         /* here we purposefully lose precision so that we can compare floats */
458         if (fabs(g1 - g2))
459             continue;
460         num--;
461         for (j = --i; j < num; j++)
462             S(j) = S(j + 1);
463     }
464
465     for (i = 0; !i || s->segments[i - 2].x; i += 2) {
466         s->segments[i].y += s->gain_dB;
467         s->segments[i].x *= M_LN10 / 20;
468         s->segments[i].y *= M_LN10 / 20;
469     }
470
471 #define L(x) s->segments[i - (x)]
472     for (i = 4; s->segments[i - 2].x; i += 2) {
473         double x, y, cx, cy, in1, in2, out1, out2, theta, len, r;
474
475         L(4).a = 0;
476         L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x);
477
478         L(2).a = 0;
479         L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x);
480
481         theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x);
482         len = sqrt(pow(L(2).x - L(4).x, 2.) + pow(L(2).y - L(4).y, 2.));
483         r = FFMIN(radius, len);
484         L(3).x = L(2).x - r * cos(theta);
485         L(3).y = L(2).y - r * sin(theta);
486
487         theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x);
488         len = sqrt(pow(L(0).x - L(2).x, 2.) + pow(L(0).y - L(2).y, 2.));
489         r = FFMIN(radius, len / 2);
490         x = L(2).x + r * cos(theta);
491         y = L(2).y + r * sin(theta);
492
493         cx = (L(3).x + L(2).x + x) / 3;
494         cy = (L(3).y + L(2).y + y) / 3;
495
496         L(2).x = x;
497         L(2).y = y;
498
499         in1  = cx - L(3).x;
500         out1 = cy - L(3).y;
501         in2  = L(2).x - L(3).x;
502         out2 = L(2).y - L(3).y;
503         L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
504         L(3).b = out1 / in1 - L(3).a * in1;
505     }
506     L(3).x = 0;
507     L(3).y = L(2).y;
508
509     s->in_min_lin  = exp(s->segments[1].x);
510     s->out_min_lin = exp(s->segments[1].y);
511
512     for (i = 0; i < channels; i++) {
513         ChanParam *cp = &s->channels[i];
514
515         if (cp->attack > 1.0 / sample_rate)
516             cp->attack = 1.0 - exp(-1.0 / (sample_rate * cp->attack));
517         else
518             cp->attack = 1.0;
519         if (cp->decay > 1.0 / sample_rate)
520             cp->decay = 1.0 - exp(-1.0 / (sample_rate * cp->decay));
521         else
522             cp->decay = 1.0;
523         cp->volume = pow(10.0, s->initial_volume / 20);
524     }
525
526     s->delay_samples = s->delay * sample_rate;
527     if (s->delay_samples <= 0) {
528         s->compand = compand_nodelay;
529         return 0;
530     }
531
532     s->delay_frame = av_frame_alloc();
533     if (!s->delay_frame) {
534         uninit(ctx);
535         return AVERROR(ENOMEM);
536     }
537
538     s->delay_frame->format         = outlink->format;
539     s->delay_frame->nb_samples     = s->delay_samples;
540     s->delay_frame->channel_layout = outlink->channel_layout;
541
542     err = av_frame_get_buffer(s->delay_frame, 32);
543     if (err)
544         return err;
545
546     s->compand = compand_delay;
547     return 0;
548 }
549
550 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
551 {
552     AVFilterContext *ctx = inlink->dst;
553     CompandContext *s    = ctx->priv;
554
555     return s->compand(ctx, frame);
556 }
557
558 static int request_frame(AVFilterLink *outlink)
559 {
560     AVFilterContext *ctx = outlink->src;
561     CompandContext *s    = ctx->priv;
562     int ret;
563
564     ret = ff_request_frame(ctx->inputs[0]);
565
566     if (ret == AVERROR_EOF && s->delay_count)
567         ret = compand_drain(outlink);
568
569     return ret;
570 }
571
572 static const AVFilterPad compand_inputs[] = {
573     {
574         .name         = "default",
575         .type         = AVMEDIA_TYPE_AUDIO,
576         .filter_frame = filter_frame,
577     },
578     { NULL }
579 };
580
581 static const AVFilterPad compand_outputs[] = {
582     {
583         .name          = "default",
584         .request_frame = request_frame,
585         .config_props  = config_output,
586         .type          = AVMEDIA_TYPE_AUDIO,
587     },
588     { NULL }
589 };
590
591
592 AVFilter ff_af_compand = {
593     .name           = "compand",
594     .description    = NULL_IF_CONFIG_SMALL(
595             "Compress or expand audio dynamic range."),
596     .query_formats  = query_formats,
597     .priv_size      = sizeof(CompandContext),
598     .priv_class     = &compand_class,
599     .init           = init,
600     .uninit         = uninit,
601     .inputs         = compand_inputs,
602     .outputs        = compand_outputs,
603 };