Signed-off-by: Paul B Mahol <onemda@gmail.com>
Paul B Mahol authored on 2017/05/13 18:57:42... | ... |
@@ -3270,6 +3270,22 @@ lowpass=c=LFE |
3270 | 3270 |
@end example |
3271 | 3271 |
@end itemize |
3272 | 3272 |
|
3273 |
+@section mcompand |
|
3274 |
+Multiband Compress or expand the audio's dynamic range. |
|
3275 |
+ |
|
3276 |
+The input audio is divided into bands using 4th order Linkwitz-Riley IIRs. |
|
3277 |
+This is akin to the crossover of a loudspeaker, and results in flat frequency |
|
3278 |
+response when absent compander action. |
|
3279 |
+ |
|
3280 |
+It accepts the following parameters: |
|
3281 |
+ |
|
3282 |
+@table @option |
|
3283 |
+@item args |
|
3284 |
+This option syntax is: |
|
3285 |
+attack,decay,[attack,decay..] soft-knee points crossover_frequency [delay [initial_volume [gain]]] | attack,decay ... |
|
3286 |
+For explanation of each item refer to compand filter documentation. |
|
3287 |
+@end table |
|
3288 |
+ |
|
3273 | 3289 |
@anchor{pan} |
3274 | 3290 |
@section pan |
3275 | 3291 |
|
... | ... |
@@ -101,6 +101,7 @@ OBJS-$(CONFIG_JOIN_FILTER) += af_join.o |
101 | 101 |
OBJS-$(CONFIG_LADSPA_FILTER) += af_ladspa.o |
102 | 102 |
OBJS-$(CONFIG_LOUDNORM_FILTER) += af_loudnorm.o ebur128.o |
103 | 103 |
OBJS-$(CONFIG_LOWPASS_FILTER) += af_biquads.o |
104 |
+OBJS-$(CONFIG_MCOMPAND_FILTER) += af_mcompand.o |
|
104 | 105 |
OBJS-$(CONFIG_PAN_FILTER) += af_pan.o |
105 | 106 |
OBJS-$(CONFIG_REPLAYGAIN_FILTER) += af_replaygain.o |
106 | 107 |
OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o |
107 | 108 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,689 @@ |
0 |
+/* |
|
1 |
+ * COpyright (c) 2002 Daniel Pouzzner |
|
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 FFmpeg. |
|
9 |
+ * |
|
10 |
+ * FFmpeg 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 |
+ * FFmpeg 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 FFmpeg; 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 multiband compand filter |
|
28 |
+ */ |
|
29 |
+ |
|
30 |
+#include "libavutil/avassert.h" |
|
31 |
+#include "libavutil/avstring.h" |
|
32 |
+#include "libavutil/ffmath.h" |
|
33 |
+#include "libavutil/opt.h" |
|
34 |
+#include "libavutil/samplefmt.h" |
|
35 |
+#include "audio.h" |
|
36 |
+#include "avfilter.h" |
|
37 |
+#include "internal.h" |
|
38 |
+ |
|
39 |
+typedef struct CompandSegment { |
|
40 |
+ double x, y; |
|
41 |
+ double a, b; |
|
42 |
+} CompandSegment; |
|
43 |
+ |
|
44 |
+typedef struct CompandT { |
|
45 |
+ CompandSegment *segments; |
|
46 |
+ int nb_segments; |
|
47 |
+ double in_min_lin; |
|
48 |
+ double out_min_lin; |
|
49 |
+ double curve_dB; |
|
50 |
+ double gain_dB; |
|
51 |
+} CompandT; |
|
52 |
+ |
|
53 |
+#define N 4 |
|
54 |
+ |
|
55 |
+typedef struct PrevCrossover { |
|
56 |
+ double in; |
|
57 |
+ double out_low; |
|
58 |
+ double out_high; |
|
59 |
+} PrevCrossover[N * 2]; |
|
60 |
+ |
|
61 |
+typedef struct Crossover { |
|
62 |
+ PrevCrossover *previous; |
|
63 |
+ size_t pos; |
|
64 |
+ double coefs[3 *(N+1)]; |
|
65 |
+} Crossover; |
|
66 |
+ |
|
67 |
+typedef struct CompBand { |
|
68 |
+ CompandT transfer_fn; |
|
69 |
+ double *attack_rate; |
|
70 |
+ double *decay_rate; |
|
71 |
+ double *volume; |
|
72 |
+ double delay; |
|
73 |
+ double topfreq; |
|
74 |
+ Crossover filter; |
|
75 |
+ AVFrame *delay_buf; |
|
76 |
+ size_t delay_size; |
|
77 |
+ ptrdiff_t delay_buf_ptr; |
|
78 |
+ size_t delay_buf_cnt; |
|
79 |
+} CompBand; |
|
80 |
+ |
|
81 |
+typedef struct MCompandContext { |
|
82 |
+ const AVClass *class; |
|
83 |
+ |
|
84 |
+ char *args; |
|
85 |
+ |
|
86 |
+ int nb_bands; |
|
87 |
+ CompBand *bands; |
|
88 |
+ AVFrame *band_buf1, *band_buf2, *band_buf3; |
|
89 |
+ int band_samples; |
|
90 |
+ size_t delay_buf_size; |
|
91 |
+} MCompandContext; |
|
92 |
+ |
|
93 |
+#define OFFSET(x) offsetof(MCompandContext, x) |
|
94 |
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
95 |
+ |
|
96 |
+static const AVOption mcompand_options[] = { |
|
97 |
+ { "args", "set parameters for each band", OFFSET(args), AV_OPT_TYPE_STRING, { .str = "0.005,0.1 6 -47/-40,-34/-34,-17/-33 100 | 0.003,0.05 6 -47/-40,-34/-34,-17/-33 400 | 0.000625,0.0125 6 -47/-40,-34/-34,-15/-33 1600 | 0.0001,0.025 6 -47/-40,-34/-34,-31/-31,-0/-30 6400 | 0,0.025 6 -38/-31,-28/-28,-0/-25 22000" }, 0, 0, A }, |
|
98 |
+ { NULL } |
|
99 |
+}; |
|
100 |
+ |
|
101 |
+AVFILTER_DEFINE_CLASS(mcompand); |
|
102 |
+ |
|
103 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
104 |
+{ |
|
105 |
+ MCompandContext *s = ctx->priv; |
|
106 |
+ int i; |
|
107 |
+ |
|
108 |
+ av_frame_free(&s->band_buf1); |
|
109 |
+ av_frame_free(&s->band_buf2); |
|
110 |
+ av_frame_free(&s->band_buf3); |
|
111 |
+ |
|
112 |
+ if (s->bands) { |
|
113 |
+ for (i = 0; i < s->nb_bands; i++) { |
|
114 |
+ av_freep(&s->bands[i].attack_rate); |
|
115 |
+ av_freep(&s->bands[i].decay_rate); |
|
116 |
+ av_freep(&s->bands[i].volume); |
|
117 |
+ av_freep(&s->bands[i].transfer_fn.segments); |
|
118 |
+ av_freep(&s->bands[i].filter.previous); |
|
119 |
+ av_frame_free(&s->bands[i].delay_buf); |
|
120 |
+ } |
|
121 |
+ } |
|
122 |
+ av_freep(&s->bands); |
|
123 |
+} |
|
124 |
+ |
|
125 |
+static int query_formats(AVFilterContext *ctx) |
|
126 |
+{ |
|
127 |
+ AVFilterChannelLayouts *layouts; |
|
128 |
+ AVFilterFormats *formats; |
|
129 |
+ static const enum AVSampleFormat sample_fmts[] = { |
|
130 |
+ AV_SAMPLE_FMT_DBLP, |
|
131 |
+ AV_SAMPLE_FMT_NONE |
|
132 |
+ }; |
|
133 |
+ int ret; |
|
134 |
+ |
|
135 |
+ layouts = ff_all_channel_counts(); |
|
136 |
+ if (!layouts) |
|
137 |
+ return AVERROR(ENOMEM); |
|
138 |
+ ret = ff_set_common_channel_layouts(ctx, layouts); |
|
139 |
+ if (ret < 0) |
|
140 |
+ return ret; |
|
141 |
+ |
|
142 |
+ formats = ff_make_format_list(sample_fmts); |
|
143 |
+ if (!formats) |
|
144 |
+ return AVERROR(ENOMEM); |
|
145 |
+ ret = ff_set_common_formats(ctx, formats); |
|
146 |
+ if (ret < 0) |
|
147 |
+ return ret; |
|
148 |
+ |
|
149 |
+ formats = ff_all_samplerates(); |
|
150 |
+ if (!formats) |
|
151 |
+ return AVERROR(ENOMEM); |
|
152 |
+ return ff_set_common_samplerates(ctx, formats); |
|
153 |
+} |
|
154 |
+ |
|
155 |
+static void count_items(char *item_str, int *nb_items, char delimiter) |
|
156 |
+{ |
|
157 |
+ char *p; |
|
158 |
+ |
|
159 |
+ *nb_items = 1; |
|
160 |
+ for (p = item_str; *p; p++) { |
|
161 |
+ if (*p == delimiter) |
|
162 |
+ (*nb_items)++; |
|
163 |
+ } |
|
164 |
+} |
|
165 |
+ |
|
166 |
+static void update_volume(CompBand *cb, double in, int ch) |
|
167 |
+{ |
|
168 |
+ double delta = in - cb->volume[ch]; |
|
169 |
+ |
|
170 |
+ if (delta > 0.0) |
|
171 |
+ cb->volume[ch] += delta * cb->attack_rate[ch]; |
|
172 |
+ else |
|
173 |
+ cb->volume[ch] += delta * cb->decay_rate[ch]; |
|
174 |
+} |
|
175 |
+ |
|
176 |
+static double get_volume(CompandT *s, double in_lin) |
|
177 |
+{ |
|
178 |
+ CompandSegment *cs; |
|
179 |
+ double in_log, out_log; |
|
180 |
+ int i; |
|
181 |
+ |
|
182 |
+ if (in_lin <= s->in_min_lin) |
|
183 |
+ return s->out_min_lin; |
|
184 |
+ |
|
185 |
+ in_log = log(in_lin); |
|
186 |
+ |
|
187 |
+ for (i = 1; i < s->nb_segments; i++) |
|
188 |
+ if (in_log <= s->segments[i].x) |
|
189 |
+ break; |
|
190 |
+ cs = &s->segments[i - 1]; |
|
191 |
+ in_log -= cs->x; |
|
192 |
+ out_log = cs->y + in_log * (cs->a * in_log + cs->b); |
|
193 |
+ |
|
194 |
+ return exp(out_log); |
|
195 |
+} |
|
196 |
+ |
|
197 |
+static int parse_points(char *points, int nb_points, double radius, |
|
198 |
+ CompandT *s, AVFilterContext *ctx) |
|
199 |
+{ |
|
200 |
+ int new_nb_items, num; |
|
201 |
+ char *saveptr = NULL; |
|
202 |
+ char *p = points; |
|
203 |
+ int i; |
|
204 |
+ |
|
205 |
+#define S(x) s->segments[2 * ((x) + 1)] |
|
206 |
+ for (i = 0, new_nb_items = 0; i < nb_points; i++) { |
|
207 |
+ char *tstr = av_strtok(p, ",", &saveptr); |
|
208 |
+ p = NULL; |
|
209 |
+ if (!tstr || sscanf(tstr, "%lf/%lf", &S(i).x, &S(i).y) != 2) { |
|
210 |
+ av_log(ctx, AV_LOG_ERROR, |
|
211 |
+ "Invalid and/or missing input/output value.\n"); |
|
212 |
+ return AVERROR(EINVAL); |
|
213 |
+ } |
|
214 |
+ if (i && S(i - 1).x > S(i).x) { |
|
215 |
+ av_log(ctx, AV_LOG_ERROR, |
|
216 |
+ "Transfer function input values must be increasing.\n"); |
|
217 |
+ return AVERROR(EINVAL); |
|
218 |
+ } |
|
219 |
+ S(i).y -= S(i).x; |
|
220 |
+ av_log(ctx, AV_LOG_DEBUG, "%d: x=%f y=%f\n", i, S(i).x, S(i).y); |
|
221 |
+ new_nb_items++; |
|
222 |
+ } |
|
223 |
+ num = new_nb_items; |
|
224 |
+ |
|
225 |
+ /* Add 0,0 if necessary */ |
|
226 |
+ if (num == 0 || S(num - 1).x) |
|
227 |
+ num++; |
|
228 |
+ |
|
229 |
+#undef S |
|
230 |
+#define S(x) s->segments[2 * (x)] |
|
231 |
+ /* Add a tail off segment at the start */ |
|
232 |
+ S(0).x = S(1).x - 2 * s->curve_dB; |
|
233 |
+ S(0).y = S(1).y; |
|
234 |
+ num++; |
|
235 |
+ |
|
236 |
+ /* Join adjacent colinear segments */ |
|
237 |
+ for (i = 2; i < num; i++) { |
|
238 |
+ double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x); |
|
239 |
+ double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x); |
|
240 |
+ int j; |
|
241 |
+ |
|
242 |
+ if (fabs(g1 - g2)) |
|
243 |
+ continue; |
|
244 |
+ num--; |
|
245 |
+ for (j = --i; j < num; j++) |
|
246 |
+ S(j) = S(j + 1); |
|
247 |
+ } |
|
248 |
+ |
|
249 |
+ for (i = 0; i < s->nb_segments; i += 2) { |
|
250 |
+ s->segments[i].y += s->gain_dB; |
|
251 |
+ s->segments[i].x *= M_LN10 / 20; |
|
252 |
+ s->segments[i].y *= M_LN10 / 20; |
|
253 |
+ } |
|
254 |
+ |
|
255 |
+#define L(x) s->segments[i - (x)] |
|
256 |
+ for (i = 4; i < s->nb_segments; i += 2) { |
|
257 |
+ double x, y, cx, cy, in1, in2, out1, out2, theta, len, r; |
|
258 |
+ |
|
259 |
+ L(4).a = 0; |
|
260 |
+ L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x); |
|
261 |
+ |
|
262 |
+ L(2).a = 0; |
|
263 |
+ L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x); |
|
264 |
+ |
|
265 |
+ theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x); |
|
266 |
+ len = hypot(L(2).x - L(4).x, L(2).y - L(4).y); |
|
267 |
+ r = FFMIN(radius, len); |
|
268 |
+ L(3).x = L(2).x - r * cos(theta); |
|
269 |
+ L(3).y = L(2).y - r * sin(theta); |
|
270 |
+ |
|
271 |
+ theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x); |
|
272 |
+ len = hypot(L(0).x - L(2).x, L(0).y - L(2).y); |
|
273 |
+ r = FFMIN(radius, len / 2); |
|
274 |
+ x = L(2).x + r * cos(theta); |
|
275 |
+ y = L(2).y + r * sin(theta); |
|
276 |
+ |
|
277 |
+ cx = (L(3).x + L(2).x + x) / 3; |
|
278 |
+ cy = (L(3).y + L(2).y + y) / 3; |
|
279 |
+ |
|
280 |
+ L(2).x = x; |
|
281 |
+ L(2).y = y; |
|
282 |
+ |
|
283 |
+ in1 = cx - L(3).x; |
|
284 |
+ out1 = cy - L(3).y; |
|
285 |
+ in2 = L(2).x - L(3).x; |
|
286 |
+ out2 = L(2).y - L(3).y; |
|
287 |
+ L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1); |
|
288 |
+ L(3).b = out1 / in1 - L(3).a * in1; |
|
289 |
+ } |
|
290 |
+ L(3).x = 0; |
|
291 |
+ L(3).y = L(2).y; |
|
292 |
+ |
|
293 |
+ s->in_min_lin = exp(s->segments[1].x); |
|
294 |
+ s->out_min_lin = exp(s->segments[1].y); |
|
295 |
+ |
|
296 |
+ return 0; |
|
297 |
+} |
|
298 |
+ |
|
299 |
+static void square_quadratic(double const *x, double *y) |
|
300 |
+{ |
|
301 |
+ y[0] = x[0] * x[0]; |
|
302 |
+ y[1] = 2 * x[0] * x[1]; |
|
303 |
+ y[2] = 2 * x[0] * x[2] + x[1] * x[1]; |
|
304 |
+ y[3] = 2 * x[1] * x[2]; |
|
305 |
+ y[4] = x[2] * x[2]; |
|
306 |
+} |
|
307 |
+ |
|
308 |
+static int crossover_setup(AVFilterLink *outlink, Crossover *p, double frequency) |
|
309 |
+{ |
|
310 |
+ double w0 = 2 * M_PI * frequency / outlink->sample_rate; |
|
311 |
+ double Q = sqrt(.5), alpha = sin(w0) / (2*Q); |
|
312 |
+ double x[9], norm; |
|
313 |
+ int i; |
|
314 |
+ |
|
315 |
+ if (w0 > M_PI) |
|
316 |
+ return AVERROR(EINVAL); |
|
317 |
+ |
|
318 |
+ x[0] = (1 - cos(w0))/2; /* Cf. filter_LPF in biquads.c */ |
|
319 |
+ x[1] = 1 - cos(w0); |
|
320 |
+ x[2] = (1 - cos(w0))/2; |
|
321 |
+ x[3] = (1 + cos(w0))/2; /* Cf. filter_HPF in biquads.c */ |
|
322 |
+ x[4] = -(1 + cos(w0)); |
|
323 |
+ x[5] = (1 + cos(w0))/2; |
|
324 |
+ x[6] = 1 + alpha; |
|
325 |
+ x[7] = -2*cos(w0); |
|
326 |
+ x[8] = 1 - alpha; |
|
327 |
+ |
|
328 |
+ for (norm = x[6], i = 0; i < 9; ++i) |
|
329 |
+ x[i] /= norm; |
|
330 |
+ |
|
331 |
+ square_quadratic(x , p->coefs); |
|
332 |
+ square_quadratic(x + 3, p->coefs + 5); |
|
333 |
+ square_quadratic(x + 6, p->coefs + 10); |
|
334 |
+ |
|
335 |
+ p->previous = av_calloc(outlink->channels, sizeof(*p->previous)); |
|
336 |
+ if (!p->previous) |
|
337 |
+ return AVERROR(ENOMEM); |
|
338 |
+ |
|
339 |
+ return 0; |
|
340 |
+} |
|
341 |
+ |
|
342 |
+static int config_output(AVFilterLink *outlink) |
|
343 |
+{ |
|
344 |
+ AVFilterContext *ctx = outlink->src; |
|
345 |
+ MCompandContext *s = ctx->priv; |
|
346 |
+ int ret, ch, i, k, new_nb_items, nb_bands; |
|
347 |
+ char *p = s->args, *saveptr = NULL; |
|
348 |
+ int max_delay_size = 0; |
|
349 |
+ |
|
350 |
+ count_items(s->args, &nb_bands, '|'); |
|
351 |
+ s->nb_bands = FFMAX(1, nb_bands); |
|
352 |
+ |
|
353 |
+ s->bands = av_calloc(nb_bands, sizeof(*s->bands)); |
|
354 |
+ if (!s->bands) |
|
355 |
+ return AVERROR(ENOMEM); |
|
356 |
+ |
|
357 |
+ for (i = 0, new_nb_items = 0; i < nb_bands; i++) { |
|
358 |
+ int nb_points, nb_attacks, nb_items = 0; |
|
359 |
+ char *tstr2, *tstr = av_strtok(p, "|", &saveptr); |
|
360 |
+ char *p2, *p3, *saveptr2 = NULL, *saveptr3 = NULL; |
|
361 |
+ double radius; |
|
362 |
+ |
|
363 |
+ if (!tstr) { |
|
364 |
+ uninit(ctx); |
|
365 |
+ return AVERROR(EINVAL); |
|
366 |
+ } |
|
367 |
+ p = NULL; |
|
368 |
+ |
|
369 |
+ p2 = tstr; |
|
370 |
+ count_items(tstr, &nb_items, ' '); |
|
371 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
372 |
+ if (!tstr2) { |
|
373 |
+ av_log(ctx, AV_LOG_ERROR, "at least one attacks/decays rate is mandatory\n"); |
|
374 |
+ uninit(ctx); |
|
375 |
+ return AVERROR(EINVAL); |
|
376 |
+ } |
|
377 |
+ p2 = NULL; |
|
378 |
+ p3 = tstr2; |
|
379 |
+ |
|
380 |
+ count_items(tstr2, &nb_attacks, ','); |
|
381 |
+ if (!nb_attacks || nb_attacks & 1) { |
|
382 |
+ av_log(ctx, AV_LOG_ERROR, "number of attacks rate plus decays rate must be even\n"); |
|
383 |
+ uninit(ctx); |
|
384 |
+ return AVERROR(EINVAL); |
|
385 |
+ } |
|
386 |
+ |
|
387 |
+ s->bands[i].attack_rate = av_calloc(outlink->channels, sizeof(double)); |
|
388 |
+ s->bands[i].decay_rate = av_calloc(outlink->channels, sizeof(double)); |
|
389 |
+ s->bands[i].volume = av_calloc(outlink->channels, sizeof(double)); |
|
390 |
+ for (k = 0; k < FFMIN(nb_attacks / 2, outlink->channels); k++) { |
|
391 |
+ char *tstr3 = av_strtok(p3, ",", &saveptr3); |
|
392 |
+ |
|
393 |
+ p3 = NULL; |
|
394 |
+ sscanf(tstr3, "%lf", &s->bands[i].attack_rate[k]); |
|
395 |
+ tstr3 = av_strtok(p3, ",", &saveptr3); |
|
396 |
+ sscanf(tstr3, "%lf", &s->bands[i].decay_rate[k]); |
|
397 |
+ |
|
398 |
+ if (s->bands[i].attack_rate[k] > 1.0 / outlink->sample_rate) { |
|
399 |
+ s->bands[i].attack_rate[k] = 1.0 - exp(-1.0 / (outlink->sample_rate * s->bands[i].attack_rate[k])); |
|
400 |
+ } else { |
|
401 |
+ s->bands[i].attack_rate[k] = 1.0; |
|
402 |
+ } |
|
403 |
+ |
|
404 |
+ if (s->bands[i].decay_rate[k] > 1.0 / outlink->sample_rate) { |
|
405 |
+ s->bands[i].decay_rate[k] = 1.0 - exp(-1.0 / (outlink->sample_rate * s->bands[i].decay_rate[k])); |
|
406 |
+ } else { |
|
407 |
+ s->bands[i].decay_rate[k] = 1.0; |
|
408 |
+ } |
|
409 |
+ } |
|
410 |
+ |
|
411 |
+ for (ch = k; ch < outlink->channels; ch++) { |
|
412 |
+ s->bands[i].attack_rate[ch] = s->bands[i].attack_rate[k - 1]; |
|
413 |
+ s->bands[i].decay_rate[ch] = s->bands[i].decay_rate[k - 1]; |
|
414 |
+ } |
|
415 |
+ |
|
416 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
417 |
+ if (!tstr2) { |
|
418 |
+ av_log(ctx, AV_LOG_ERROR, "transfer function curve in dB must be set\n"); |
|
419 |
+ uninit(ctx); |
|
420 |
+ return AVERROR(EINVAL); |
|
421 |
+ } |
|
422 |
+ sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.curve_dB); |
|
423 |
+ |
|
424 |
+ radius = s->bands[i].transfer_fn.curve_dB * M_LN10 / 20.0; |
|
425 |
+ |
|
426 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
427 |
+ if (!tstr2) { |
|
428 |
+ av_log(ctx, AV_LOG_ERROR, "transfer points missing\n"); |
|
429 |
+ uninit(ctx); |
|
430 |
+ return AVERROR(EINVAL); |
|
431 |
+ } |
|
432 |
+ |
|
433 |
+ count_items(tstr2, &nb_points, ','); |
|
434 |
+ s->bands[i].transfer_fn.nb_segments = (nb_points + 4) * 2; |
|
435 |
+ s->bands[i].transfer_fn.segments = av_calloc(s->bands[i].transfer_fn.nb_segments, |
|
436 |
+ sizeof(CompandSegment)); |
|
437 |
+ if (!s->bands[i].transfer_fn.segments) { |
|
438 |
+ uninit(ctx); |
|
439 |
+ return AVERROR(ENOMEM); |
|
440 |
+ } |
|
441 |
+ |
|
442 |
+ ret = parse_points(tstr2, nb_points, radius, &s->bands[i].transfer_fn, ctx); |
|
443 |
+ if (ret < 0) { |
|
444 |
+ av_log(ctx, AV_LOG_ERROR, "transfer points parsing failed\n"); |
|
445 |
+ uninit(ctx); |
|
446 |
+ return ret; |
|
447 |
+ } |
|
448 |
+ |
|
449 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
450 |
+ if (!tstr2) { |
|
451 |
+ av_log(ctx, AV_LOG_ERROR, "crossover_frequency is missing\n"); |
|
452 |
+ uninit(ctx); |
|
453 |
+ return AVERROR(EINVAL); |
|
454 |
+ } |
|
455 |
+ |
|
456 |
+ new_nb_items += sscanf(tstr2, "%lf", &s->bands[i].topfreq) == 1; |
|
457 |
+ if (s->bands[i].topfreq < 0 || s->bands[i].topfreq >= outlink->sample_rate / 2) { |
|
458 |
+ av_log(ctx, AV_LOG_ERROR, "crossover_frequency should be >=0 and lower than half of sample rate\n"); |
|
459 |
+ uninit(ctx); |
|
460 |
+ return AVERROR(EINVAL); |
|
461 |
+ } |
|
462 |
+ |
|
463 |
+ if (s->bands[i].topfreq != 0) { |
|
464 |
+ ret = crossover_setup(outlink, &s->bands[i].filter, s->bands[i].topfreq); |
|
465 |
+ if (ret < 0) { |
|
466 |
+ uninit(ctx); |
|
467 |
+ return ret; |
|
468 |
+ } |
|
469 |
+ } |
|
470 |
+ |
|
471 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
472 |
+ if (tstr2) { |
|
473 |
+ sscanf(tstr2, "%lf", &s->bands[i].delay); |
|
474 |
+ max_delay_size = FFMAX(max_delay_size, s->bands[i].delay * outlink->sample_rate); |
|
475 |
+ |
|
476 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
477 |
+ if (tstr2) { |
|
478 |
+ double initial_volume; |
|
479 |
+ |
|
480 |
+ sscanf(tstr2, "%lf", &initial_volume); |
|
481 |
+ initial_volume = pow(10.0, initial_volume / 20); |
|
482 |
+ |
|
483 |
+ for (k = 0; k < outlink->channels; k++) { |
|
484 |
+ s->bands[i].volume[k] = initial_volume; |
|
485 |
+ } |
|
486 |
+ |
|
487 |
+ tstr2 = av_strtok(p2, " ", &saveptr2); |
|
488 |
+ if (tstr2) { |
|
489 |
+ sscanf(tstr2, "%lf", &s->bands[i].transfer_fn.gain_dB); |
|
490 |
+ } |
|
491 |
+ } |
|
492 |
+ } |
|
493 |
+ } |
|
494 |
+ s->nb_bands = new_nb_items; |
|
495 |
+ |
|
496 |
+ for (i = 0; max_delay_size > 0 && i < s->nb_bands; i++) { |
|
497 |
+ s->bands[i].delay_buf = ff_get_audio_buffer(outlink, max_delay_size); |
|
498 |
+ if (!s->bands[i].delay_buf) |
|
499 |
+ return AVERROR(ENOMEM); |
|
500 |
+ } |
|
501 |
+ s->delay_buf_size = max_delay_size; |
|
502 |
+ |
|
503 |
+ return 0; |
|
504 |
+} |
|
505 |
+ |
|
506 |
+#define CONVOLVE _ _ _ _ |
|
507 |
+ |
|
508 |
+static void crossover(int ch, Crossover *p, |
|
509 |
+ double *ibuf, double *obuf_low, |
|
510 |
+ double *obuf_high, size_t len) |
|
511 |
+{ |
|
512 |
+ double out_low, out_high; |
|
513 |
+ |
|
514 |
+ while (len--) { |
|
515 |
+ p->pos = p->pos ? p->pos - 1 : N - 1; |
|
516 |
+#define _ out_low += p->coefs[j] * p->previous[ch][p->pos + j].in \ |
|
517 |
+ - p->coefs[2*N+2 + j] * p->previous[ch][p->pos + j].out_low, j++; |
|
518 |
+ { |
|
519 |
+ int j = 1; |
|
520 |
+ out_low = p->coefs[0] * *ibuf; |
|
521 |
+ CONVOLVE |
|
522 |
+ *obuf_low++ = out_low; |
|
523 |
+ } |
|
524 |
+#undef _ |
|
525 |
+#define _ out_high += p->coefs[j+N+1] * p->previous[ch][p->pos + j].in \ |
|
526 |
+ - p->coefs[2*N+2 + j] * p->previous[ch][p->pos + j].out_high, j++; |
|
527 |
+ { |
|
528 |
+ int j = 1; |
|
529 |
+ out_high = p->coefs[N+1] * *ibuf; |
|
530 |
+ CONVOLVE |
|
531 |
+ *obuf_high++ = out_high; |
|
532 |
+ } |
|
533 |
+ p->previous[ch][p->pos + N].in = p->previous[ch][p->pos].in = *ibuf++; |
|
534 |
+ p->previous[ch][p->pos + N].out_low = p->previous[ch][p->pos].out_low = out_low; |
|
535 |
+ p->previous[ch][p->pos + N].out_high = p->previous[ch][p->pos].out_high = out_high; |
|
536 |
+ } |
|
537 |
+} |
|
538 |
+ |
|
539 |
+static int mcompand_channel(MCompandContext *c, CompBand *l, double *ibuf, double *obuf, int len, int ch) |
|
540 |
+{ |
|
541 |
+ int i; |
|
542 |
+ |
|
543 |
+ for (i = 0; i < len; i++) { |
|
544 |
+ double level_in_lin, level_out_lin, checkbuf; |
|
545 |
+ /* Maintain the volume fields by simulating a leaky pump circuit */ |
|
546 |
+ update_volume(l, fabs(ibuf[i]), ch); |
|
547 |
+ |
|
548 |
+ /* Volume memory is updated: perform compand */ |
|
549 |
+ level_in_lin = l->volume[ch]; |
|
550 |
+ level_out_lin = get_volume(&l->transfer_fn, level_in_lin); |
|
551 |
+ |
|
552 |
+ if (c->delay_buf_size <= 0) { |
|
553 |
+ checkbuf = ibuf[i] * level_out_lin; |
|
554 |
+ obuf[i] = checkbuf; |
|
555 |
+ } else { |
|
556 |
+ double *delay_buf = (double *)l->delay_buf->extended_data[ch]; |
|
557 |
+ |
|
558 |
+ /* FIXME: note that this lookahead algorithm is really lame: |
|
559 |
+ the response to a peak is released before the peak |
|
560 |
+ arrives. */ |
|
561 |
+ |
|
562 |
+ /* because volume application delays differ band to band, but |
|
563 |
+ total delay doesn't, the volume is applied in an iteration |
|
564 |
+ preceding that in which the sample goes to obuf, except in |
|
565 |
+ the band(s) with the longest vol app delay. |
|
566 |
+ |
|
567 |
+ the offset between delay_buf_ptr and the sample to apply |
|
568 |
+ vol to, is a constant equal to the difference between this |
|
569 |
+ band's delay and the longest delay of all the bands. */ |
|
570 |
+ |
|
571 |
+ if (l->delay_buf_cnt >= l->delay_size) { |
|
572 |
+ checkbuf = |
|
573 |
+ delay_buf[(l->delay_buf_ptr + |
|
574 |
+ c->delay_buf_size - |
|
575 |
+ l->delay_size) % c->delay_buf_size] * level_out_lin; |
|
576 |
+ delay_buf[(l->delay_buf_ptr + c->delay_buf_size - |
|
577 |
+ l->delay_size) % c->delay_buf_size] = checkbuf; |
|
578 |
+ } |
|
579 |
+ if (l->delay_buf_cnt >= c->delay_buf_size) { |
|
580 |
+ obuf[i] = delay_buf[l->delay_buf_ptr]; |
|
581 |
+ } else { |
|
582 |
+ l->delay_buf_cnt++; |
|
583 |
+ } |
|
584 |
+ delay_buf[l->delay_buf_ptr++] = ibuf[i]; |
|
585 |
+ l->delay_buf_ptr %= c->delay_buf_size; |
|
586 |
+ } |
|
587 |
+ } |
|
588 |
+ |
|
589 |
+ return 0; |
|
590 |
+} |
|
591 |
+ |
|
592 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
593 |
+{ |
|
594 |
+ AVFilterContext *ctx = inlink->dst; |
|
595 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
596 |
+ MCompandContext *s = ctx->priv; |
|
597 |
+ AVFrame *out, *abuf, *bbuf, *cbuf; |
|
598 |
+ int ch, band, i; |
|
599 |
+ |
|
600 |
+ out = ff_get_audio_buffer(outlink, in->nb_samples); |
|
601 |
+ if (!out) { |
|
602 |
+ av_frame_free(&in); |
|
603 |
+ return AVERROR(ENOMEM); |
|
604 |
+ } |
|
605 |
+ |
|
606 |
+ if (s->band_samples < in->nb_samples) { |
|
607 |
+ av_frame_free(&s->band_buf1); |
|
608 |
+ av_frame_free(&s->band_buf2); |
|
609 |
+ av_frame_free(&s->band_buf3); |
|
610 |
+ |
|
611 |
+ s->band_buf1 = ff_get_audio_buffer(outlink, in->nb_samples); |
|
612 |
+ s->band_buf2 = ff_get_audio_buffer(outlink, in->nb_samples); |
|
613 |
+ s->band_buf3 = ff_get_audio_buffer(outlink, in->nb_samples); |
|
614 |
+ s->band_samples = in->nb_samples; |
|
615 |
+ } |
|
616 |
+ |
|
617 |
+ for (ch = 0; ch < outlink->channels; ch++) { |
|
618 |
+ double *a, *dst = (double *)out->extended_data[ch]; |
|
619 |
+ |
|
620 |
+ for (band = 0, abuf = in, bbuf = s->band_buf2, cbuf = s->band_buf1; band < s->nb_bands; band++) { |
|
621 |
+ CompBand *b = &s->bands[band]; |
|
622 |
+ |
|
623 |
+ if (b->topfreq) { |
|
624 |
+ crossover(ch, &b->filter, (double *)abuf->extended_data[ch], |
|
625 |
+ (double *)bbuf->extended_data[ch], (double *)cbuf->extended_data[ch], in->nb_samples); |
|
626 |
+ } else { |
|
627 |
+ bbuf = abuf; |
|
628 |
+ abuf = cbuf; |
|
629 |
+ } |
|
630 |
+ |
|
631 |
+ if (abuf == in) |
|
632 |
+ abuf = s->band_buf3; |
|
633 |
+ mcompand_channel(s, b, (double *)bbuf->extended_data[ch], (double *)abuf->extended_data[ch], out->nb_samples, ch); |
|
634 |
+ a = (double *)abuf->extended_data[ch]; |
|
635 |
+ for (i = 0; i < out->nb_samples; i++) { |
|
636 |
+ dst[i] += a[i]; |
|
637 |
+ } |
|
638 |
+ |
|
639 |
+ FFSWAP(AVFrame *, abuf, cbuf); |
|
640 |
+ } |
|
641 |
+ } |
|
642 |
+ |
|
643 |
+ out->pts = in->pts; |
|
644 |
+ av_frame_free(&in); |
|
645 |
+ return ff_filter_frame(outlink, out); |
|
646 |
+} |
|
647 |
+ |
|
648 |
+static int request_frame(AVFilterLink *outlink) |
|
649 |
+{ |
|
650 |
+ AVFilterContext *ctx = outlink->src; |
|
651 |
+ int ret; |
|
652 |
+ |
|
653 |
+ ret = ff_request_frame(ctx->inputs[0]); |
|
654 |
+ |
|
655 |
+ return ret; |
|
656 |
+} |
|
657 |
+ |
|
658 |
+static const AVFilterPad mcompand_inputs[] = { |
|
659 |
+ { |
|
660 |
+ .name = "default", |
|
661 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
662 |
+ .filter_frame = filter_frame, |
|
663 |
+ }, |
|
664 |
+ { NULL } |
|
665 |
+}; |
|
666 |
+ |
|
667 |
+static const AVFilterPad mcompand_outputs[] = { |
|
668 |
+ { |
|
669 |
+ .name = "default", |
|
670 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
671 |
+ .request_frame = request_frame, |
|
672 |
+ .config_props = config_output, |
|
673 |
+ }, |
|
674 |
+ { NULL } |
|
675 |
+}; |
|
676 |
+ |
|
677 |
+ |
|
678 |
+AVFilter ff_af_mcompand = { |
|
679 |
+ .name = "mcompand", |
|
680 |
+ .description = NULL_IF_CONFIG_SMALL( |
|
681 |
+ "Multiband Compress or expand audio dynamic range."), |
|
682 |
+ .query_formats = query_formats, |
|
683 |
+ .priv_size = sizeof(MCompandContext), |
|
684 |
+ .priv_class = &mcompand_class, |
|
685 |
+ .uninit = uninit, |
|
686 |
+ .inputs = mcompand_inputs, |
|
687 |
+ .outputs = mcompand_outputs, |
|
688 |
+}; |
... | ... |
@@ -112,6 +112,7 @@ static void register_all(void) |
112 | 112 |
REGISTER_FILTER(LADSPA, ladspa, af); |
113 | 113 |
REGISTER_FILTER(LOUDNORM, loudnorm, af); |
114 | 114 |
REGISTER_FILTER(LOWPASS, lowpass, af); |
115 |
+ REGISTER_FILTER(MCOMPAND, mcompand, af); |
|
115 | 116 |
REGISTER_FILTER(PAN, pan, af); |
116 | 117 |
REGISTER_FILTER(REPLAYGAIN, replaygain, af); |
117 | 118 |
REGISTER_FILTER(RESAMPLE, resample, af); |
... | ... |
@@ -30,8 +30,8 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 7 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 0 |
|
34 |
-#define LIBAVFILTER_VERSION_MICRO 101 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 1 |
|
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, \ |