2017-05-14 18:30:12 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/channel_layout.h"
|
2018-12-31 22:08:06 +02:00
|
|
|
#include "libavutil/ffmath.h"
|
2017-05-14 18:30:12 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "formats.h"
|
|
|
|
|
|
|
|
typedef struct CrossfeedContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
double range;
|
|
|
|
double strength;
|
2020-04-30 11:58:04 +02:00
|
|
|
double slope;
|
2017-05-14 18:30:12 +02:00
|
|
|
double level_in;
|
|
|
|
double level_out;
|
|
|
|
|
|
|
|
double a0, a1, a2;
|
|
|
|
double b0, b1, b2;
|
|
|
|
|
2020-10-20 13:32:25 +02:00
|
|
|
double w1, w2;
|
2017-05-14 18:30:12 +02:00
|
|
|
} CrossfeedContext;
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
AVFilterChannelLayouts *layout = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
|
|
|
|
(ret = ff_set_common_formats (ctx , formats )) < 0 ||
|
|
|
|
(ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
|
|
|
|
(ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
|
|
|
|
(ret = ff_set_common_samplerates (ctx , ff_all_samplerates())) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
CrossfeedContext *s = ctx->priv;
|
2018-12-31 22:08:06 +02:00
|
|
|
double A = ff_exp10(s->strength * -30 / 40);
|
2017-05-14 18:30:12 +02:00
|
|
|
double w0 = 2 * M_PI * (1. - s->range) * 2100 / inlink->sample_rate;
|
|
|
|
double alpha;
|
|
|
|
|
2020-04-30 11:58:04 +02:00
|
|
|
alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->slope - 1) + 2);
|
2017-05-14 18:30:12 +02:00
|
|
|
|
|
|
|
s->a0 = (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
|
|
|
|
s->a1 = -2 * ((A - 1) + (A + 1) * cos(w0));
|
|
|
|
s->a2 = (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
|
|
|
|
s->b0 = A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
|
|
|
|
s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
|
|
|
|
s->b2 = A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
|
|
|
|
|
|
|
|
s->a1 /= s->a0;
|
|
|
|
s->a2 /= s->a0;
|
|
|
|
s->b0 /= s->a0;
|
|
|
|
s->b1 /= s->a0;
|
|
|
|
s->b2 /= s->a0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
CrossfeedContext *s = ctx->priv;
|
|
|
|
const double *src = (const double *)in->data[0];
|
|
|
|
const double level_in = s->level_in;
|
|
|
|
const double level_out = s->level_out;
|
|
|
|
const double b0 = s->b0;
|
|
|
|
const double b1 = s->b1;
|
|
|
|
const double b2 = s->b2;
|
2020-10-20 13:32:25 +02:00
|
|
|
const double a1 = -s->a1;
|
|
|
|
const double a2 = -s->a2;
|
2017-05-14 18:30:12 +02:00
|
|
|
AVFrame *out;
|
|
|
|
double *dst;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (av_frame_is_writable(in)) {
|
|
|
|
out = in;
|
|
|
|
} else {
|
2018-01-03 23:47:40 +02:00
|
|
|
out = ff_get_audio_buffer(outlink, in->nb_samples);
|
2017-05-14 18:30:12 +02:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
}
|
|
|
|
dst = (double *)out->data[0];
|
|
|
|
|
|
|
|
for (n = 0; n < out->nb_samples; n++, src += 2, dst += 2) {
|
|
|
|
double mid = (src[0] + src[1]) * level_in * .5;
|
|
|
|
double side = (src[0] - src[1]) * level_in * .5;
|
2020-10-20 13:32:25 +02:00
|
|
|
double oside = side * b0 + s->w1;
|
2017-05-14 18:30:12 +02:00
|
|
|
|
2020-10-20 13:32:25 +02:00
|
|
|
s->w1 = b1 * side + s->w2 + a1 * oside;
|
|
|
|
s->w2 = b2 * side + a2 * oside;
|
2017-05-14 18:30:12 +02:00
|
|
|
|
2019-05-01 12:31:52 +02:00
|
|
|
if (ctx->is_disabled) {
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
} else {
|
|
|
|
dst[0] = (mid + oside) * level_out;
|
|
|
|
dst[1] = (mid - oside) * level_out;
|
|
|
|
}
|
2017-05-14 18:30:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (out != in)
|
|
|
|
av_frame_free(&in);
|
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:04:27 +02:00
|
|
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
|
|
|
char *res, int res_len, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return config_input(ctx->inputs[0]);
|
|
|
|
}
|
|
|
|
|
2017-05-14 18:30:12 +02:00
|
|
|
#define OFFSET(x) offsetof(CrossfeedContext, x)
|
2020-04-30 12:04:27 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2017-05-14 18:30:12 +02:00
|
|
|
|
|
|
|
static const AVOption crossfeed_options[] = {
|
|
|
|
{ "strength", "set crossfeed strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=.2}, 0, 1, FLAGS },
|
|
|
|
{ "range", "set soundstage wideness", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
|
2020-04-30 11:58:04 +02:00
|
|
|
{ "slope", "set curve slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .01, 1, FLAGS },
|
2017-05-14 18:30:12 +02:00
|
|
|
{ "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=.9}, 0, 1, FLAGS },
|
|
|
|
{ "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0, 1, FLAGS },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(crossfeed);
|
|
|
|
|
|
|
|
static const AVFilterPad inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_crossfeed = {
|
2017-05-14 18:30:12 +02:00
|
|
|
.name = "crossfeed",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply headphone crossfeed filter."),
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(CrossfeedContext),
|
|
|
|
.priv_class = &crossfeed_class,
|
|
|
|
.inputs = inputs,
|
|
|
|
.outputs = outputs,
|
2019-05-01 12:31:52 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
2020-04-30 12:04:27 +02:00
|
|
|
.process_command = process_command,
|
2017-05-14 18:30:12 +02:00
|
|
|
};
|