FFmpeg
Loading...
Searching...
No Matches
af_crossfeed.c
Go to the documentation of this file.
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
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
28typedef struct CrossfeedContext {
29 const AVClass *class;
30
31 double range;
32 double strength;
33 double slope;
34 double level_in;
35 double level_out;
38
39 double a0, a1, a2;
40 double b0, b1, b2;
41
42 double w1, w2;
43
46
47 double *mid;
48 double *side[3];
50
52 AVFilterFormatsConfig **cfg_in,
53 AVFilterFormatsConfig **cfg_out)
54{
55 static const enum AVSampleFormat formats[] = {
58 };
59 static const AVChannelLayout layouts[] = {
61 { .nb_channels = 0 },
62 };
63
64 int ret;
65
66 ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, formats);
67 if (ret < 0)
68 return ret;
69
71 if (ret < 0)
72 return ret;
73
74 return 0;
75}
76
77static 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
113static 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
120static 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
143static 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 }
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 {
281 return 0;
282 }
283}
284
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) {
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
331static 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
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
356static 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
367
368static const AVFilterPad inputs[] = {
369 {
370 .name = "default",
371 .type = AVMEDIA_TYPE_AUDIO,
372 .config_props = config_input,
373 },
374};
375
377 .p.name = "crossfeed",
378 .p.description = NULL_IF_CONFIG_SMALL("Apply headphone crossfeed filter."),
379 .p.priv_class = &crossfeed_class,
381 .priv_size = sizeof(CrossfeedContext),
383 .uninit = uninit,
387 .process_command = process_command,
388};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
#define AF
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static void filter_samples(double *dst, const double *src, int nb_samples, double b0, double b1, double b2, double a1, double a2, double *sw1, double *sw2)
static const AVOption crossfeed_options[]
static int config_input(AVFilterLink *inlink)
const FFFilter ff_af_crossfeed
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int filter_frame(AVFilterLink *inlink, AVFrame *in, int eof)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static void reverse_samples(double *dst, const double *src, int nb_samples)
#define OFFSET(x)
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1540
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:229
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1495
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition ffmath.h:42
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition formats.c:1026
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AV_CHANNEL_LAYOUT_STEREO
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
static const int16_t alpha[]
Definition ilbcdata.h:55
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define M_PI
Definition mathematics.h:67
enum AVColorRange range
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
AVOptions.
formats
Definition signature.h:47
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
double * side[3]
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int64_t pts
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029