Browse code

lavfi: add compand filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/07/17 06:39:06
Showing 6 changed files
... ...
@@ -6,6 +6,7 @@ version <next>
6 6
 - aecho filter
7 7
 - perspective filter ported from libmpcodecs
8 8
 - ffprobe -show_programs option
9
+- compand filter
9 10
 
10 11
 
11 12
 version 2.0:
... ...
@@ -1176,6 +1176,83 @@ front_center.wav -map '[LFE]' lfe.wav -map '[SL]' side_left.wav -map '[SR]'
1176 1176
 side_right.wav
1177 1177
 @end example
1178 1178
 
1179
+@section compand
1180
+
1181
+Compress or expand audio dynamic range.
1182
+
1183
+A description of the accepted options follows.
1184
+
1185
+@table @option
1186
+@item attacks
1187
+@item decays
1188
+Set list of times in seconds for each channel over which the instantaneous
1189
+level of the input signal is averaged to determine its volume.
1190
+@option{attacks} refers to increase of volume and @option{decays} refers
1191
+to decrease of volume.
1192
+For most situations, the attack time (response to the audio getting louder)
1193
+should be shorter than the decay time because the human ear is more sensitive
1194
+to sudden loud audio than sudden soft audio.
1195
+Typical value for attack is @code{0.3} seconds and for decay @code{0.8}
1196
+seconds.
1197
+
1198
+@item points
1199
+Set list of points for transfer function, specified in dB relative to maximum
1200
+possible signal amplitude.
1201
+Each key points list need to be defined using the following syntax:
1202
+@code{x0/y0 x1/y1 x2/y2 ...}.
1203
+
1204
+The input values must be in strictly increasing order but the transfer
1205
+function does not have to me monotonically rising.
1206
+The point @code{0/0} is assumed but may be overridden (by @code{0/out-dBn}).
1207
+Typical values for the transfer function are @code{-70/-70 -60/-20}.
1208
+
1209
+@item soft-knee
1210
+Set amount for which the points at where adjacent line segments on the
1211
+transfer function meet will be rounded. Defaults is @code{0.01}.
1212
+
1213
+@item gain
1214
+Set additional gain in dB to be applied at all points on the transfer function
1215
+and allows easy adjustment of the overall gain.
1216
+Default is @code{0}.
1217
+
1218
+@item volume
1219
+Set initial volume in dB to be assumed for each channel when filtering starts.
1220
+This permits the user to supply a nominal level initially, so that,
1221
+for example, a very large gain is not applied to initial signal levels before
1222
+the companding has begun to operate. A typical value for audio which is
1223
+initially quiet is -90 dB. Default is @code{0}.
1224
+
1225
+@item delay
1226
+Set delay in seconds. Default is @code{0}. The input audio
1227
+is analysed immediately, but audio is delayed before being fed to the
1228
+volume adjuster. Specifying a delay approximately equal to the attack/decay
1229
+times allows the filter to effectively operate in predictive rather than
1230
+reactive mode.
1231
+@end table
1232
+
1233
+@subsection Examples
1234
+@itemize
1235
+@item
1236
+Make music with both quiet and loud passages suitable for listening
1237
+in a noisy environment:
1238
+@example
1239
+compand=.3 .3:1 1:-90/-60 -60/-40 -40/-30 -20/-20:6:0:-90:0.2
1240
+@end example
1241
+
1242
+@item
1243
+Noise-gate for when the noise is at a lower level than the signal:
1244
+@example
1245
+compand=.1 .1:.2 .2:-900/-900 -50.1/-900 -50/-50:.01:0:-90:.1
1246
+@end example
1247
+
1248
+@item
1249
+Here is another noise-gate, this time for when the noise is at a higher level
1250
+than the signal (making it, in some ways, similar to squelch):
1251
+@example
1252
+compand=.1 .1:.1 .1:-45.1/-45.1 -45/-900 0/-900:.01:45:-90:.1
1253
+@end example
1254
+@end itemize
1255
+
1179 1256
 @section earwax
1180 1257
 
1181 1258
 Make audio easier to listen to on headphones.
... ...
@@ -84,6 +84,7 @@ OBJS-$(CONFIG_BASS_FILTER)                   += af_biquads.o
84 84
 OBJS-$(CONFIG_BIQUAD_FILTER)                 += af_biquads.o
85 85
 OBJS-$(CONFIG_CHANNELMAP_FILTER)             += af_channelmap.o
86 86
 OBJS-$(CONFIG_CHANNELSPLIT_FILTER)           += af_channelsplit.o
87
+OBJS-$(CONFIG_COMPAND_FILTER)                += af_compand.o
87 88
 OBJS-$(CONFIG_EARWAX_FILTER)                 += af_earwax.o
88 89
 OBJS-$(CONFIG_EBUR128_FILTER)                += f_ebur128.o
89 90
 OBJS-$(CONFIG_EQUALIZER_FILTER)              += af_biquads.o
90 91
new file mode 100644
... ...
@@ -0,0 +1,515 @@
0
+/*
1
+ * Copyright (c) 1999 Chris Bagwell
2
+ * Copyright (c) 1999 Nick Bailey
3
+ * Copyright (c) 2007 Rob Sykes <robs@users.sourceforge.net>
4
+ * Copyright (c) 2013 Paul B Mahol
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ *
22
+ */
23
+
24
+#include "libavutil/avstring.h"
25
+#include "libavutil/opt.h"
26
+#include "libavutil/samplefmt.h"
27
+#include "avfilter.h"
28
+#include "audio.h"
29
+#include "internal.h"
30
+
31
+typedef struct ChanParam {
32
+    double attack;
33
+    double decay;
34
+    double volume;
35
+} ChanParam;
36
+
37
+typedef struct CompandSegment {
38
+    double x, y;
39
+    double a, b;
40
+} CompandSegment;
41
+
42
+typedef struct CompandContext {
43
+    const AVClass *class;
44
+    char *attacks, *decays, *points;
45
+    CompandSegment *segments;
46
+    ChanParam *channels;
47
+    double in_min_lin;
48
+    double out_min_lin;
49
+    double curve_dB;
50
+    double gain_dB;
51
+    double initial_volume;
52
+    double delay;
53
+    uint8_t **delayptrs;
54
+    int delay_samples;
55
+    int delay_count;
56
+    int delay_index;
57
+    int64_t pts;
58
+
59
+    int (*compand)(AVFilterContext *ctx, AVFrame *frame);
60
+} CompandContext;
61
+
62
+#define OFFSET(x) offsetof(CompandContext, x)
63
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64
+
65
+static const AVOption compand_options[] = {
66
+    { "attacks", "set time over which increase of volume is determined", OFFSET(attacks), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
67
+    { "decays", "set time over which decrease of volume is determined", OFFSET(decays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
68
+    { "points", "set points of transfer function", OFFSET(points), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
69
+    { "soft-knee", "set soft-knee", OFFSET(curve_dB), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, 0.01, 900, A },
70
+    { "gain", "set output gain", OFFSET(gain_dB), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 900, A },
71
+    { "volume", "set initial volume", OFFSET(initial_volume), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -900, 0, A },
72
+    { "delay", "set delay for samples before sending them to volume adjuster", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 20, A },
73
+    { NULL },
74
+};
75
+
76
+AVFILTER_DEFINE_CLASS(compand);
77
+
78
+static av_cold int init(AVFilterContext *ctx)
79
+{
80
+    CompandContext *s = ctx->priv;
81
+
82
+    if (!s->attacks || !s->decays || !s->points) {
83
+        av_log(ctx, AV_LOG_ERROR, "Missing attacks and/or decays and/or points.\n");
84
+        return AVERROR(EINVAL);
85
+    }
86
+
87
+    return 0;
88
+}
89
+
90
+static av_cold void uninit(AVFilterContext *ctx)
91
+{
92
+    CompandContext *s = ctx->priv;
93
+
94
+    av_freep(&s->channels);
95
+    av_freep(&s->segments);
96
+    if (s->delayptrs)
97
+        av_freep(&s->delayptrs[0]);
98
+    av_freep(&s->delayptrs);
99
+}
100
+
101
+static int query_formats(AVFilterContext *ctx)
102
+{
103
+    AVFilterChannelLayouts *layouts;
104
+    AVFilterFormats *formats;
105
+    static const enum AVSampleFormat sample_fmts[] = {
106
+        AV_SAMPLE_FMT_DBLP,
107
+        AV_SAMPLE_FMT_NONE
108
+    };
109
+
110
+    layouts = ff_all_channel_layouts();
111
+    if (!layouts)
112
+        return AVERROR(ENOMEM);
113
+    ff_set_common_channel_layouts(ctx, layouts);
114
+
115
+    formats = ff_make_format_list(sample_fmts);
116
+    if (!formats)
117
+        return AVERROR(ENOMEM);
118
+    ff_set_common_formats(ctx, formats);
119
+
120
+    formats = ff_all_samplerates();
121
+    if (!formats)
122
+        return AVERROR(ENOMEM);
123
+    ff_set_common_samplerates(ctx, formats);
124
+
125
+    return 0;
126
+}
127
+
128
+static void count_items(char *item_str, int *nb_items)
129
+{
130
+    char *p;
131
+
132
+    *nb_items = 1;
133
+    for (p = item_str; *p; p++) {
134
+        if (*p == ' ')
135
+            (*nb_items)++;
136
+    }
137
+
138
+}
139
+
140
+static void update_volume(ChanParam *cp, double in)
141
+{
142
+    double delta = in - cp->volume;
143
+
144
+    if (delta > 0.0)
145
+        cp->volume += delta * cp->attack;
146
+    else
147
+        cp->volume += delta * cp->decay;
148
+}
149
+
150
+static double get_volume(CompandContext *s, double in_lin)
151
+{
152
+    CompandSegment *cs;
153
+    double in_log, out_log;
154
+    int i;
155
+
156
+    if (in_lin < s->in_min_lin)
157
+        return s->out_min_lin;
158
+
159
+    in_log = log(in_lin);
160
+
161
+    for (i = 1;; i++)
162
+        if (in_log <= s->segments[i + 1].x)
163
+            break;
164
+
165
+    cs = &s->segments[i];
166
+    in_log -= cs->x;
167
+    out_log = cs->y + in_log * (cs->a * in_log + cs->b);
168
+
169
+    return exp(out_log);
170
+}
171
+
172
+static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
173
+{
174
+    CompandContext *s = ctx->priv;
175
+    AVFilterLink *inlink = ctx->inputs[0];
176
+    const int channels = inlink->channels;
177
+    const int nb_samples = frame->nb_samples;
178
+    AVFrame *out_frame;
179
+    int chan, i;
180
+
181
+    if (av_frame_is_writable(frame)) {
182
+        out_frame = frame;
183
+    } else {
184
+        out_frame = ff_get_audio_buffer(inlink, nb_samples);
185
+        if (!out_frame)
186
+            return AVERROR(ENOMEM);
187
+        av_frame_copy_props(out_frame, frame);
188
+    }
189
+
190
+    for (chan = 0; chan < channels; chan++) {
191
+        const double *src = (double *)frame->data[chan];
192
+        double *dst = (double *)out_frame->data[chan];
193
+        ChanParam *cp = &s->channels[chan];
194
+
195
+        for (i = 0; i < nb_samples; i++) {
196
+            update_volume(cp, fabs(src[i]));
197
+
198
+            dst[i] = av_clipd(src[i] * get_volume(s, cp->volume), -1, 1);
199
+        }
200
+    }
201
+
202
+    if (frame != out_frame)
203
+        av_frame_free(&frame);
204
+
205
+    return ff_filter_frame(ctx->outputs[0], out_frame);
206
+}
207
+
208
+#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
209
+
210
+static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
211
+{
212
+    CompandContext *s = ctx->priv;
213
+    AVFilterLink *inlink = ctx->inputs[0];
214
+    const int channels = inlink->channels;
215
+    const int nb_samples = frame->nb_samples;
216
+    int chan, i, dindex, oindex, count;
217
+    AVFrame *out_frame = NULL;
218
+
219
+    for (chan = 0; chan < channels; chan++) {
220
+        const double *src = (double *)frame->data[chan];
221
+        double *dbuf = (double *)s->delayptrs[chan];
222
+        ChanParam *cp = &s->channels[chan];
223
+        double *dst;
224
+
225
+        count  = s->delay_count;
226
+        dindex = s->delay_index;
227
+        for (i = 0, oindex = 0; i < nb_samples; i++) {
228
+            const double in = src[i];
229
+            update_volume(cp, fabs(in));
230
+
231
+            if (count >= s->delay_samples) {
232
+                if (!out_frame) {
233
+                    out_frame = ff_get_audio_buffer(inlink, nb_samples - i);
234
+                    if (!out_frame)
235
+                        return AVERROR(ENOMEM);
236
+                    av_frame_copy_props(out_frame, frame);
237
+                    out_frame->pts = s->pts;
238
+                    s->pts += av_rescale_q(nb_samples - i, (AVRational){1, inlink->sample_rate}, inlink->time_base);
239
+                }
240
+
241
+                dst = (double *)out_frame->data[chan];
242
+                dst[oindex++] = av_clipd(dbuf[dindex] * get_volume(s, cp->volume), -1, 1);
243
+            } else {
244
+                count++;
245
+            }
246
+
247
+            dbuf[dindex] = in;
248
+            dindex = MOD(dindex + 1, s->delay_samples);
249
+        }
250
+    }
251
+
252
+    s->delay_count = count;
253
+    s->delay_index = dindex;
254
+
255
+    av_frame_free(&frame);
256
+    return out_frame ? ff_filter_frame(ctx->outputs[0], out_frame) : 0;
257
+}
258
+
259
+static int compand_drain(AVFilterLink *outlink)
260
+{
261
+    AVFilterContext *ctx = outlink->src;
262
+    CompandContext *s = ctx->priv;
263
+    const int channels = outlink->channels;
264
+    int chan, i, dindex;
265
+    AVFrame *frame = NULL;
266
+
267
+    frame = ff_get_audio_buffer(outlink, FFMIN(2048, s->delay_count));
268
+    if (!frame)
269
+        return AVERROR(ENOMEM);
270
+    frame->pts = s->pts;
271
+    s->pts += av_rescale_q(frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
272
+
273
+    for (chan = 0; chan < channels; chan++) {
274
+        double *dbuf = (double *)s->delayptrs[chan];
275
+        double *dst = (double *)frame->data[chan];
276
+        ChanParam *cp = &s->channels[chan];
277
+
278
+        dindex = s->delay_index;
279
+        for (i = 0; i < frame->nb_samples; i++) {
280
+            dst[i] = av_clipd(dbuf[dindex] * get_volume(s, cp->volume), -1, 1);
281
+            dindex = MOD(dindex + 1, s->delay_samples);
282
+        }
283
+    }
284
+    s->delay_count -= frame->nb_samples;
285
+    s->delay_index = dindex;
286
+
287
+    return ff_filter_frame(outlink, frame);
288
+}
289
+
290
+static int config_output(AVFilterLink *outlink)
291
+{
292
+    AVFilterContext *ctx = outlink->src;
293
+    CompandContext *s = ctx->priv;
294
+    const int sample_rate = outlink->sample_rate;
295
+    double radius = s->curve_dB * M_LN10 / 20;
296
+    int nb_attacks, nb_decays, nb_points;
297
+    char *p, *saveptr = NULL;
298
+    int new_nb_items, num;
299
+    int i;
300
+
301
+    count_items(s->attacks, &nb_attacks);
302
+    count_items(s->decays, &nb_decays);
303
+    count_items(s->points, &nb_points);
304
+
305
+    if ((nb_attacks > outlink->channels) || (nb_decays > outlink->channels)) {
306
+        av_log(ctx, AV_LOG_ERROR, "Number of attacks/decays bigger than number of channels.\n");
307
+        return AVERROR(EINVAL);
308
+    }
309
+
310
+    uninit(ctx);
311
+
312
+    s->channels = av_mallocz_array(outlink->channels, sizeof(*s->channels));
313
+    s->segments = av_mallocz_array((nb_points + 4) * 2, sizeof(*s->segments));
314
+
315
+    if (!s->channels || !s->segments)
316
+        return AVERROR(ENOMEM);
317
+
318
+    p = s->attacks;
319
+    for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
320
+        char *tstr = av_strtok(p, " ", &saveptr);
321
+        p = NULL;
322
+        new_nb_items += sscanf(tstr, "%lf", &s->channels[i].attack) == 1;
323
+        if (s->channels[i].attack < 0)
324
+            return AVERROR(EINVAL);
325
+    }
326
+    nb_attacks = new_nb_items;
327
+
328
+    p = s->decays;
329
+    for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
330
+        char *tstr = av_strtok(p, " ", &saveptr);
331
+        p = NULL;
332
+        new_nb_items += sscanf(tstr, "%lf", &s->channels[i].decay) == 1;
333
+        if (s->channels[i].decay < 0)
334
+            return AVERROR(EINVAL);
335
+    }
336
+    nb_decays = new_nb_items;
337
+
338
+    if (nb_attacks != nb_decays) {
339
+        av_log(ctx, AV_LOG_ERROR, "Number of attacks %d differs from number of decays %d.\n", nb_attacks, nb_decays);
340
+        return AVERROR(EINVAL);
341
+    }
342
+
343
+#define S(x) s->segments[2 * ((x) + 1)]
344
+    p = s->points;
345
+    for (i = 0, new_nb_items = 0; i < nb_points; i++) {
346
+        char *tstr = av_strtok(p, " ", &saveptr);
347
+        p = NULL;
348
+        if (sscanf(tstr, "%lf/%lf", &S(i).x, &S(i).y) != 2) {
349
+            av_log(ctx, AV_LOG_ERROR, "Invalid and/or missing input/output value.\n");
350
+            return AVERROR(EINVAL);
351
+        }
352
+        if (i && S(i - 1).x > S(i).x) {
353
+            av_log(ctx, AV_LOG_ERROR, "Transfer function input values must be increasing.\n");
354
+            return AVERROR(EINVAL);
355
+        }
356
+        S(i).y -= S(i).x;
357
+        av_log(ctx, AV_LOG_DEBUG, "%d: x=%lf y=%lf\n", i, S(i).x, S(i).y);
358
+        new_nb_items++;
359
+    }
360
+    num = new_nb_items;
361
+
362
+    /* Add 0,0 if necessary */
363
+    if (num == 0 || S(num - 1).x)
364
+        num++;
365
+
366
+#undef S
367
+#define S(x) s->segments[2 * (x)]
368
+    /* Add a tail off segment at the start */
369
+    S(0).x = S(1).x - 2 * s->curve_dB;
370
+    S(0).y = S(1).y;
371
+    num++;
372
+
373
+    /* Join adjacent colinear segments */
374
+    for (i = 2; i < num; i++) {
375
+        double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x);
376
+        double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x);
377
+        int j;
378
+
379
+        if (fabs(g1 - g2))
380
+            continue;
381
+        num--;
382
+        for (j = --i; j < num; j++)
383
+            S(j) = S(j + 1);
384
+    }
385
+
386
+    for (i = 0; !i || s->segments[i - 2].x; i += 2) {
387
+        s->segments[i].y += s->gain_dB;
388
+        s->segments[i].x *= M_LN10 / 20;
389
+        s->segments[i].y *= M_LN10 / 20;
390
+    }
391
+
392
+#define L(x) s->segments[i - (x)]
393
+    for (i = 4; s->segments[i - 2].x; i += 2) {
394
+        double x, y, cx, cy, in1, in2, out1, out2, theta, len, r;
395
+
396
+        L(4).a = 0;
397
+        L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x);
398
+
399
+        L(2).a = 0;
400
+        L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x);
401
+
402
+        theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x);
403
+        len = sqrt(pow(L(2).x - L(4).x, 2.) + pow(L(2).y - L(4).y, 2.));
404
+        r = FFMIN(radius, len);
405
+        L(3).x = L(2).x - r * cos(theta);
406
+        L(3).y = L(2).y - r * sin(theta);
407
+
408
+        theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x);
409
+        len = sqrt(pow(L(0).x - L(2).x, 2.) + pow(L(0).y - L(2).y, 2.));
410
+        r = FFMIN(radius, len / 2);
411
+        x = L(2).x + r * cos(theta);
412
+        y = L(2).y + r * sin(theta);
413
+
414
+        cx = (L(3).x + L(2).x + x) / 3;
415
+        cy = (L(3).y + L(2).y + y) / 3;
416
+
417
+        L(2).x = x;
418
+        L(2).y = y;
419
+
420
+        in1 = cx - L(3).x;
421
+        out1 = cy - L(3).y;
422
+        in2 = L(2).x - L(3).x;
423
+        out2 = L(2).y - L(3).y;
424
+        L(3).a = (out2 / in2 - out1 / in1) / (in2-in1);
425
+        L(3).b = out1 / in1 - L(3).a * in1;
426
+    }
427
+    L(3).x = 0;
428
+    L(3).y = L(2).y;
429
+
430
+    s->in_min_lin  = exp(s->segments[1].x);
431
+    s->out_min_lin = exp(s->segments[1].y);
432
+
433
+    for (i = 0; i < outlink->channels; i++) {
434
+        ChanParam *cp = &s->channels[i];
435
+
436
+        if (cp->attack > 1.0 / sample_rate)
437
+            cp->attack = 1.0 - exp(-1.0 / (sample_rate * cp->attack));
438
+        else
439
+            cp->attack = 1.0;
440
+        if (cp->decay > 1.0 / sample_rate)
441
+            cp->decay = 1.0 - exp(-1.0 / (sample_rate * cp->decay));
442
+        else
443
+            cp->decay = 1.0;
444
+        cp->volume = pow(10.0, s->initial_volume / 20);
445
+    }
446
+
447
+    s->delay_samples = s->delay * sample_rate;
448
+    if (s->delay_samples > 0) {
449
+        int ret;
450
+        if ((ret = av_samples_alloc_array_and_samples(&s->delayptrs, NULL,
451
+                                                      outlink->channels,
452
+                                                      s->delay_samples,
453
+                                                      outlink->format, 0)) < 0)
454
+            return ret;
455
+        s->compand = compand_delay;
456
+        outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
457
+    } else {
458
+        s->compand = compand_nodelay;
459
+    }
460
+    return 0;
461
+}
462
+
463
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
464
+{
465
+    AVFilterContext *ctx = inlink->dst;
466
+    CompandContext *s = ctx->priv;
467
+
468
+    return s->compand(ctx, frame);
469
+}
470
+
471
+static int request_frame(AVFilterLink *outlink)
472
+{
473
+    AVFilterContext *ctx = outlink->src;
474
+    CompandContext *s = ctx->priv;
475
+    int ret;
476
+
477
+    ret = ff_request_frame(ctx->inputs[0]);
478
+
479
+    if (ret == AVERROR_EOF && !ctx->is_disabled && s->delay_count)
480
+        ret = compand_drain(outlink);
481
+
482
+    return ret;
483
+}
484
+
485
+static const AVFilterPad compand_inputs[] = {
486
+    {
487
+        .name         = "default",
488
+        .type         = AVMEDIA_TYPE_AUDIO,
489
+        .filter_frame = filter_frame,
490
+    },
491
+    { NULL },
492
+};
493
+
494
+static const AVFilterPad compand_outputs[] = {
495
+    {
496
+        .name          = "default",
497
+        .request_frame = request_frame,
498
+        .config_props  = config_output,
499
+        .type          = AVMEDIA_TYPE_AUDIO,
500
+    },
501
+    { NULL },
502
+};
503
+
504
+AVFilter avfilter_af_compand = {
505
+    .name          = "compand",
506
+    .description   = NULL_IF_CONFIG_SMALL("Compress or expand audio dynamic range."),
507
+    .query_formats = query_formats,
508
+    .priv_size     = sizeof(CompandContext),
509
+    .priv_class    = &compand_class,
510
+    .init          = init,
511
+    .uninit        = uninit,
512
+    .inputs        = compand_inputs,
513
+    .outputs       = compand_outputs,
514
+};
... ...
@@ -80,6 +80,7 @@ void avfilter_register_all(void)
80 80
     REGISTER_FILTER(BIQUAD,         biquad,         af);
81 81
     REGISTER_FILTER(CHANNELMAP,     channelmap,     af);
82 82
     REGISTER_FILTER(CHANNELSPLIT,   channelsplit,   af);
83
+    REGISTER_FILTER(COMPAND,        compand,        af);
83 84
     REGISTER_FILTER(EARWAX,         earwax,         af);
84 85
     REGISTER_FILTER(EBUR128,        ebur128,        af);
85 86
     REGISTER_FILTER(EQUALIZER,      equalizer,      af);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  3
33
-#define LIBAVFILTER_VERSION_MINOR  81
34
-#define LIBAVFILTER_VERSION_MICRO 103
33
+#define LIBAVFILTER_VERSION_MINOR  82
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \