Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* This file is part of FFmpeg. |
3 |
|
|
* |
4 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
5 |
|
|
* modify it under the terms of the GNU Lesser General Public |
6 |
|
|
* License as published by the Free Software Foundation; either |
7 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
8 |
|
|
* |
9 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
* Lesser General Public License for more details. |
13 |
|
|
* |
14 |
|
|
* You should have received a copy of the GNU Lesser General Public |
15 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
16 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include "libavutil/channel_layout.h" |
20 |
|
|
#include "libavutil/ffmath.h" |
21 |
|
|
#include "libavutil/mem.h" |
22 |
|
|
#include "libavutil/opt.h" |
23 |
|
|
#include "avfilter.h" |
24 |
|
|
#include "audio.h" |
25 |
|
|
#include "filters.h" |
26 |
|
|
#include "formats.h" |
27 |
|
|
|
28 |
|
|
typedef struct CrossfeedContext { |
29 |
|
|
const AVClass *class; |
30 |
|
|
|
31 |
|
|
double range; |
32 |
|
|
double strength; |
33 |
|
|
double slope; |
34 |
|
|
double level_in; |
35 |
|
|
double level_out; |
36 |
|
|
int block_samples; |
37 |
|
|
int block_size; |
38 |
|
|
|
39 |
|
|
double a0, a1, a2; |
40 |
|
|
double b0, b1, b2; |
41 |
|
|
|
42 |
|
|
double w1, w2; |
43 |
|
|
|
44 |
|
|
int64_t pts; |
45 |
|
|
int nb_samples; |
46 |
|
|
|
47 |
|
|
double *mid; |
48 |
|
|
double *side[3]; |
49 |
|
|
} CrossfeedContext; |
50 |
|
|
|
51 |
|
✗ |
static int query_formats(const AVFilterContext *ctx, |
52 |
|
|
AVFilterFormatsConfig **cfg_in, |
53 |
|
|
AVFilterFormatsConfig **cfg_out) |
54 |
|
|
{ |
55 |
|
|
static const enum AVSampleFormat formats[] = { |
56 |
|
|
AV_SAMPLE_FMT_DBL, |
57 |
|
|
AV_SAMPLE_FMT_NONE, |
58 |
|
|
}; |
59 |
|
|
static const AVChannelLayout layouts[] = { |
60 |
|
|
AV_CHANNEL_LAYOUT_STEREO, |
61 |
|
|
{ .nb_channels = 0 }, |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
int ret; |
65 |
|
|
|
66 |
|
✗ |
ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats); |
67 |
|
✗ |
if (ret < 0) |
68 |
|
✗ |
return ret; |
69 |
|
|
|
70 |
|
✗ |
ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, layouts); |
71 |
|
✗ |
if (ret < 0) |
72 |
|
✗ |
return ret; |
73 |
|
|
|
74 |
|
✗ |
return 0; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
78 |
|
|
{ |
79 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
80 |
|
✗ |
CrossfeedContext *s = ctx->priv; |
81 |
|
✗ |
double A = ff_exp10(s->strength * -30 / 40); |
82 |
|
✗ |
double w0 = 2 * M_PI * (1. - s->range) * 2100 / inlink->sample_rate; |
83 |
|
|
double alpha; |
84 |
|
|
|
85 |
|
✗ |
alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->slope - 1) + 2); |
86 |
|
|
|
87 |
|
✗ |
s->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha; |
88 |
|
✗ |
s->a1 = -2 * ((A - 1) + (A + 1) * cos(w0)); |
89 |
|
✗ |
s->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha; |
90 |
|
✗ |
s->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha); |
91 |
|
✗ |
s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0)); |
92 |
|
✗ |
s->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha); |
93 |
|
|
|
94 |
|
✗ |
s->a1 /= s->a0; |
95 |
|
✗ |
s->a2 /= s->a0; |
96 |
|
✗ |
s->b0 /= s->a0; |
97 |
|
✗ |
s->b1 /= s->a0; |
98 |
|
✗ |
s->b2 /= s->a0; |
99 |
|
|
|
100 |
|
✗ |
if (s->block_samples == 0 && s->block_size > 0) { |
101 |
|
✗ |
s->block_samples = s->block_size; |
102 |
|
✗ |
s->mid = av_calloc(s->block_samples * 2, sizeof(*s->mid)); |
103 |
|
✗ |
for (int i = 0; i < 3; i++) { |
104 |
|
✗ |
s->side[i] = av_calloc(s->block_samples * 2, sizeof(*s->side[0])); |
105 |
|
✗ |
if (!s->side[i]) |
106 |
|
✗ |
return AVERROR(ENOMEM); |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
✗ |
return 0; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
✗ |
static void reverse_samples(double *dst, const double *src, |
114 |
|
|
int nb_samples) |
115 |
|
|
{ |
116 |
|
✗ |
for (int i = 0, j = nb_samples - 1; i < nb_samples; i++, j--) |
117 |
|
✗ |
dst[i] = src[j]; |
118 |
|
✗ |
} |
119 |
|
|
|
120 |
|
✗ |
static void filter_samples(double *dst, const double *src, |
121 |
|
|
int nb_samples, |
122 |
|
|
double b0, double b1, double b2, |
123 |
|
|
double a1, double a2, |
124 |
|
|
double *sw1, double *sw2) |
125 |
|
|
{ |
126 |
|
✗ |
double w1 = *sw1; |
127 |
|
✗ |
double w2 = *sw2; |
128 |
|
|
|
129 |
|
✗ |
for (int n = 0; n < nb_samples; n++) { |
130 |
|
✗ |
double side = src[n]; |
131 |
|
✗ |
double oside = side * b0 + w1; |
132 |
|
|
|
133 |
|
✗ |
w1 = b1 * side + w2 + a1 * oside; |
134 |
|
✗ |
w2 = b2 * side + a2 * oside; |
135 |
|
|
|
136 |
|
✗ |
dst[n] = oside; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
✗ |
*sw1 = w1; |
140 |
|
✗ |
*sw2 = w2; |
141 |
|
✗ |
} |
142 |
|
|
|
143 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in, int eof) |
144 |
|
|
{ |
145 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
146 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
147 |
|
✗ |
CrossfeedContext *s = ctx->priv; |
148 |
|
✗ |
const double *src = (const double *)in->data[0]; |
149 |
|
✗ |
const double level_in = s->level_in; |
150 |
|
✗ |
const double level_out = s->level_out; |
151 |
|
✗ |
const double b0 = s->b0; |
152 |
|
✗ |
const double b1 = s->b1; |
153 |
|
✗ |
const double b2 = s->b2; |
154 |
|
✗ |
const double a1 = -s->a1; |
155 |
|
✗ |
const double a2 = -s->a2; |
156 |
|
|
AVFrame *out; |
157 |
|
✗ |
int drop = 0; |
158 |
|
|
double *dst; |
159 |
|
|
|
160 |
|
✗ |
if (av_frame_is_writable(in) && s->block_samples == 0) { |
161 |
|
✗ |
out = in; |
162 |
|
|
} else { |
163 |
|
✗ |
out = ff_get_audio_buffer(outlink, s->block_samples > 0 ? s->block_samples : in->nb_samples); |
164 |
|
✗ |
if (!out) { |
165 |
|
✗ |
av_frame_free(&in); |
166 |
|
✗ |
return AVERROR(ENOMEM); |
167 |
|
|
} |
168 |
|
✗ |
av_frame_copy_props(out, in); |
169 |
|
|
} |
170 |
|
✗ |
dst = (double *)out->data[0]; |
171 |
|
|
|
172 |
|
✗ |
if (s->block_samples > 0 && s->pts == AV_NOPTS_VALUE) |
173 |
|
✗ |
drop = 1; |
174 |
|
|
|
175 |
|
✗ |
if (s->block_samples == 0) { |
176 |
|
✗ |
double w1 = s->w1; |
177 |
|
✗ |
double w2 = s->w2; |
178 |
|
|
|
179 |
|
✗ |
for (int n = 0; n < out->nb_samples; n++, src += 2, dst += 2) { |
180 |
|
✗ |
double mid = (src[0] + src[1]) * level_in * .5; |
181 |
|
✗ |
double side = (src[0] - src[1]) * level_in * .5; |
182 |
|
✗ |
double oside = side * b0 + w1; |
183 |
|
|
|
184 |
|
✗ |
w1 = b1 * side + w2 + a1 * oside; |
185 |
|
✗ |
w2 = b2 * side + a2 * oside; |
186 |
|
|
|
187 |
|
✗ |
if (ctx->is_disabled) { |
188 |
|
✗ |
dst[0] = src[0]; |
189 |
|
✗ |
dst[1] = src[1]; |
190 |
|
|
} else { |
191 |
|
✗ |
dst[0] = (mid + oside) * level_out; |
192 |
|
✗ |
dst[1] = (mid - oside) * level_out; |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
|
196 |
|
✗ |
s->w1 = w1; |
197 |
|
✗ |
s->w2 = w2; |
198 |
|
✗ |
} else if (eof) { |
199 |
|
✗ |
const double *src = (const double *)in->data[0]; |
200 |
|
✗ |
double *ssrc = s->side[1] + s->block_samples; |
201 |
|
✗ |
double *msrc = s->mid; |
202 |
|
|
|
203 |
|
✗ |
for (int n = 0; n < out->nb_samples; n++, src += 2, dst += 2) { |
204 |
|
✗ |
if (ctx->is_disabled) { |
205 |
|
✗ |
dst[0] = src[0]; |
206 |
|
✗ |
dst[1] = src[1]; |
207 |
|
|
} else { |
208 |
|
✗ |
dst[0] = (msrc[n] + ssrc[n]) * level_out; |
209 |
|
✗ |
dst[1] = (msrc[n] - ssrc[n]) * level_out; |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
} else { |
213 |
|
✗ |
double *mdst = s->mid + s->block_samples; |
214 |
|
✗ |
double *sdst = s->side[0] + s->block_samples; |
215 |
|
✗ |
double *ssrc = s->side[0]; |
216 |
|
✗ |
double *msrc = s->mid; |
217 |
|
✗ |
double w1 = s->w1; |
218 |
|
✗ |
double w2 = s->w2; |
219 |
|
|
|
220 |
|
✗ |
for (int n = 0; n < out->nb_samples; n++, src += 2) { |
221 |
|
✗ |
mdst[n] = (src[0] + src[1]) * level_in * .5; |
222 |
|
✗ |
sdst[n] = (src[0] - src[1]) * level_in * .5; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
✗ |
sdst = s->side[1]; |
226 |
|
✗ |
filter_samples(sdst, ssrc, s->block_samples, |
227 |
|
|
b0, b1, b2, a1, a2, |
228 |
|
|
&w1, &w2); |
229 |
|
✗ |
s->w1 = w1; |
230 |
|
✗ |
s->w2 = w2; |
231 |
|
|
|
232 |
|
✗ |
ssrc = s->side[0] + s->block_samples; |
233 |
|
✗ |
sdst = s->side[1] + s->block_samples; |
234 |
|
✗ |
filter_samples(sdst, ssrc, s->block_samples, |
235 |
|
|
b0, b1, b2, a1, a2, |
236 |
|
|
&w1, &w2); |
237 |
|
|
|
238 |
|
✗ |
reverse_samples(s->side[2], s->side[1], s->block_samples * 2); |
239 |
|
✗ |
w1 = w2 = 0.; |
240 |
|
✗ |
filter_samples(s->side[2], s->side[2], s->block_samples * 2, |
241 |
|
|
b0, b1, b2, a1, a2, |
242 |
|
|
&w1, &w2); |
243 |
|
|
|
244 |
|
✗ |
reverse_samples(s->side[1], s->side[2], s->block_samples * 2); |
245 |
|
|
|
246 |
|
✗ |
src = (const double *)in->data[0]; |
247 |
|
✗ |
ssrc = s->side[1]; |
248 |
|
✗ |
for (int n = 0; n < out->nb_samples; n++, src += 2, dst += 2) { |
249 |
|
✗ |
if (ctx->is_disabled) { |
250 |
|
✗ |
dst[0] = src[0]; |
251 |
|
✗ |
dst[1] = src[1]; |
252 |
|
|
} else { |
253 |
|
✗ |
dst[0] = (msrc[n] + ssrc[n]) * level_out; |
254 |
|
✗ |
dst[1] = (msrc[n] - ssrc[n]) * level_out; |
255 |
|
|
} |
256 |
|
|
} |
257 |
|
|
|
258 |
|
✗ |
memmove(s->mid, s->mid + s->block_samples, |
259 |
|
✗ |
s->block_samples * sizeof(*s->mid)); |
260 |
|
✗ |
memmove(s->side[0], s->side[0] + s->block_samples, |
261 |
|
✗ |
s->block_samples * sizeof(*s->side[0])); |
262 |
|
|
} |
263 |
|
|
|
264 |
|
✗ |
if (s->block_samples > 0) { |
265 |
|
✗ |
int nb_samples = in->nb_samples; |
266 |
|
✗ |
int64_t pts = in->pts; |
267 |
|
|
|
268 |
|
✗ |
out->pts = s->pts; |
269 |
|
✗ |
out->nb_samples = s->nb_samples; |
270 |
|
✗ |
s->pts = pts; |
271 |
|
✗ |
s->nb_samples = nb_samples; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
✗ |
if (out != in) |
275 |
|
✗ |
av_frame_free(&in); |
276 |
|
✗ |
if (!drop) { |
277 |
|
✗ |
return ff_filter_frame(outlink, out); |
278 |
|
|
} else { |
279 |
|
✗ |
av_frame_free(&out); |
280 |
|
✗ |
ff_filter_set_ready(ctx, 10); |
281 |
|
✗ |
return 0; |
282 |
|
|
} |
283 |
|
|
} |
284 |
|
|
|
285 |
|
✗ |
static int activate(AVFilterContext *ctx) |
286 |
|
|
{ |
287 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
288 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
289 |
|
✗ |
CrossfeedContext *s = ctx->priv; |
290 |
|
✗ |
AVFrame *in = NULL; |
291 |
|
|
int64_t pts; |
292 |
|
|
int status; |
293 |
|
|
int ret; |
294 |
|
|
|
295 |
|
✗ |
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
296 |
|
|
|
297 |
|
✗ |
if (s->block_samples > 0) { |
298 |
|
✗ |
ret = ff_inlink_consume_samples(inlink, s->block_samples, s->block_samples, &in); |
299 |
|
|
} else { |
300 |
|
✗ |
ret = ff_inlink_consume_frame(inlink, &in); |
301 |
|
|
} |
302 |
|
✗ |
if (ret < 0) |
303 |
|
✗ |
return ret; |
304 |
|
✗ |
if (ret > 0) |
305 |
|
✗ |
return filter_frame(inlink, in, 0); |
306 |
|
|
|
307 |
|
✗ |
if (s->block_samples > 0 && ff_inlink_queued_samples(inlink) >= s->block_samples) { |
308 |
|
✗ |
ff_filter_set_ready(ctx, 10); |
309 |
|
✗ |
return 0; |
310 |
|
|
} |
311 |
|
|
|
312 |
|
✗ |
if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
313 |
|
✗ |
if (s->block_samples > 0) { |
314 |
|
✗ |
AVFrame *in = ff_get_audio_buffer(outlink, s->block_samples); |
315 |
|
✗ |
if (!in) |
316 |
|
✗ |
return AVERROR(ENOMEM); |
317 |
|
|
|
318 |
|
✗ |
ret = filter_frame(inlink, in, 1); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
✗ |
ff_outlink_set_status(outlink, status, pts); |
322 |
|
|
|
323 |
|
✗ |
return ret; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
✗ |
FF_FILTER_FORWARD_WANTED(outlink, inlink); |
327 |
|
|
|
328 |
|
✗ |
return FFERROR_NOT_READY; |
329 |
|
|
} |
330 |
|
|
|
331 |
|
✗ |
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, |
332 |
|
|
char *res, int res_len, int flags) |
333 |
|
|
{ |
334 |
|
|
int ret; |
335 |
|
|
|
336 |
|
✗ |
ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); |
337 |
|
✗ |
if (ret < 0) |
338 |
|
✗ |
return ret; |
339 |
|
|
|
340 |
|
✗ |
return config_input(ctx->inputs[0]); |
341 |
|
|
} |
342 |
|
|
|
343 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
344 |
|
|
{ |
345 |
|
✗ |
CrossfeedContext *s = ctx->priv; |
346 |
|
|
|
347 |
|
✗ |
av_freep(&s->mid); |
348 |
|
✗ |
for (int i = 0; i < 3; i++) |
349 |
|
✗ |
av_freep(&s->side[i]); |
350 |
|
✗ |
} |
351 |
|
|
|
352 |
|
|
#define OFFSET(x) offsetof(CrossfeedContext, x) |
353 |
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
354 |
|
|
#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
355 |
|
|
|
356 |
|
|
static const AVOption crossfeed_options[] = { |
357 |
|
|
{ "strength", "set crossfeed strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=.2}, 0, 1, FLAGS }, |
358 |
|
|
{ "range", "set soundstage wideness", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS }, |
359 |
|
|
{ "slope", "set curve slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .01, 1, FLAGS }, |
360 |
|
|
{ "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=.9}, 0, 1, FLAGS }, |
361 |
|
|
{ "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0, 1, FLAGS }, |
362 |
|
|
{ "block_size", "set the block size", OFFSET(block_size),AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF }, |
363 |
|
|
{ NULL } |
364 |
|
|
}; |
365 |
|
|
|
366 |
|
|
AVFILTER_DEFINE_CLASS(crossfeed); |
367 |
|
|
|
368 |
|
|
static const AVFilterPad inputs[] = { |
369 |
|
|
{ |
370 |
|
|
.name = "default", |
371 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
372 |
|
|
.config_props = config_input, |
373 |
|
|
}, |
374 |
|
|
}; |
375 |
|
|
|
376 |
|
|
const AVFilter ff_af_crossfeed = { |
377 |
|
|
.name = "crossfeed", |
378 |
|
|
.description = NULL_IF_CONFIG_SMALL("Apply headphone crossfeed filter."), |
379 |
|
|
.priv_size = sizeof(CrossfeedContext), |
380 |
|
|
.priv_class = &crossfeed_class, |
381 |
|
|
.activate = activate, |
382 |
|
|
.uninit = uninit, |
383 |
|
|
FILTER_INPUTS(inputs), |
384 |
|
|
FILTER_OUTPUTS(ff_audio_default_filterpad), |
385 |
|
|
FILTER_QUERY_FUNC2(query_formats), |
386 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, |
387 |
|
|
.process_command = process_command, |
388 |
|
|
}; |
389 |
|
|
|