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