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