2014-04-29 19:53:16 +03:00
|
|
|
/*
|
2014-05-01 15:14:10 +03:00
|
|
|
* This file is part of FFmpeg.
|
2014-04-29 19:53:16 +03:00
|
|
|
*
|
2014-05-01 15:14:10 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2014-04-29 19:53:16 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2014-05-01 15:14:10 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2014-04-29 19:53:16 +03:00
|
|
|
* 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
|
2014-05-01 15:14:10 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2014-04-29 19:53:16 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Bauer stereo-to-binaural filter
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <bs2b.h>
|
|
|
|
|
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
2024-08-12 15:29:00 +02:00
|
|
|
#include "filters.h"
|
2014-04-29 19:53:16 +03:00
|
|
|
#include "formats.h"
|
|
|
|
|
2017-06-12 22:12:17 +02:00
|
|
|
typedef void (*filter_func)(t_bs2bdp bs2bdp, uint8_t *sample, int n);
|
|
|
|
|
2014-04-29 19:53:16 +03:00
|
|
|
typedef struct Bs2bContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
int profile;
|
|
|
|
int fcut;
|
|
|
|
int feed;
|
|
|
|
|
|
|
|
t_bs2bdp bs2bp;
|
|
|
|
|
2017-06-12 22:12:17 +02:00
|
|
|
filter_func filter;
|
2014-04-29 19:53:16 +03:00
|
|
|
} Bs2bContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(Bs2bContext, x)
|
2017-05-16 19:16:50 +02:00
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
|
2014-04-29 19:53:16 +03:00
|
|
|
|
2015-07-02 11:49:26 +02:00
|
|
|
static const AVOption bs2b_options[] = {
|
2014-04-29 19:53:16 +03:00
|
|
|
{ "profile", "Apply a pre-defined crossfeed level",
|
2024-02-11 16:41:05 +02:00
|
|
|
OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, INT_MAX, A, .unit = "profile" },
|
|
|
|
{ "default", "default profile", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, 0, A, .unit = "profile" },
|
|
|
|
{ "cmoy", "Chu Moy circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_CMOY_CLEVEL }, 0, 0, A, .unit = "profile" },
|
|
|
|
{ "jmeier", "Jan Meier circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_JMEIER_CLEVEL }, 0, 0, A, .unit = "profile" },
|
2014-04-29 19:53:16 +03:00
|
|
|
{ "fcut", "Set cut frequency (in Hz)",
|
|
|
|
OFFSET(fcut), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFCUT, A },
|
|
|
|
{ "feed", "Set feed level (in Hz)",
|
|
|
|
OFFSET(feed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFEED, A },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2015-07-02 11:49:26 +02:00
|
|
|
AVFILTER_DEFINE_CLASS(bs2b);
|
2014-04-29 19:53:16 +03:00
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
Bs2bContext *bs2b = ctx->priv;
|
|
|
|
|
|
|
|
if (!(bs2b->bs2bp = bs2b_open()))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
bs2b_set_level(bs2b->bs2bp, bs2b->profile);
|
|
|
|
|
|
|
|
if (bs2b->fcut)
|
|
|
|
bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
|
|
|
|
|
|
|
|
if (bs2b->feed)
|
|
|
|
bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
Bs2bContext *bs2b = ctx->priv;
|
|
|
|
|
|
|
|
if (bs2b->bs2bp)
|
|
|
|
bs2b_close(bs2b->bs2bp);
|
|
|
|
}
|
|
|
|
|
2024-08-31 11:21:00 +02:00
|
|
|
static int query_formats(const AVFilterContext *ctx,
|
|
|
|
AVFilterFormatsConfig **cfg_in,
|
|
|
|
AVFilterFormatsConfig **cfg_out)
|
2014-04-29 19:53:16 +03:00
|
|
|
{
|
2024-08-31 11:21:00 +02:00
|
|
|
static const AVChannelLayout layouts[] = {
|
2024-09-05 17:35:04 +02:00
|
|
|
AV_CHANNEL_LAYOUT_STEREO,
|
|
|
|
{ .nb_channels = 0 },
|
2024-08-31 11:21:00 +02:00
|
|
|
};
|
2014-04-29 19:53:16 +03:00
|
|
|
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
|
|
AV_SAMPLE_FMT_U8,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
AV_SAMPLE_FMT_S32,
|
|
|
|
AV_SAMPLE_FMT_FLT,
|
|
|
|
AV_SAMPLE_FMT_DBL,
|
|
|
|
AV_SAMPLE_FMT_NONE,
|
|
|
|
};
|
2015-04-03 19:55:18 +02:00
|
|
|
int ret;
|
2014-04-29 19:53:16 +03:00
|
|
|
|
2024-08-31 11:21:00 +02:00
|
|
|
ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, layouts);
|
2015-04-03 19:55:18 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-29 19:53:16 +03:00
|
|
|
|
2024-08-31 11:21:00 +02:00
|
|
|
ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
|
2015-04-03 19:55:18 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-29 19:53:16 +03:00
|
|
|
|
2024-08-31 11:21:00 +02:00
|
|
|
return 0;
|
2014-04-29 19:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
AVFrame *out_frame;
|
|
|
|
|
|
|
|
Bs2bContext *bs2b = inlink->dst->priv;
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
|
|
|
|
|
|
|
if (av_frame_is_writable(frame)) {
|
|
|
|
out_frame = frame;
|
|
|
|
} else {
|
2018-01-03 23:47:40 +02:00
|
|
|
out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
|
2017-06-24 18:36:46 +02:00
|
|
|
if (!out_frame) {
|
|
|
|
av_frame_free(&frame);
|
2014-04-29 19:53:16 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2017-06-24 18:36:46 +02:00
|
|
|
}
|
2014-04-29 19:53:16 +03:00
|
|
|
av_frame_copy(out_frame, frame);
|
|
|
|
ret = av_frame_copy_props(out_frame, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&out_frame);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
|
|
|
|
|
|
|
|
if (frame != out_frame)
|
|
|
|
av_frame_free(&frame);
|
|
|
|
|
|
|
|
return ff_filter_frame(outlink, out_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
Bs2bContext *bs2b = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
|
|
|
|
int srate = inlink->sample_rate;
|
|
|
|
|
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_U8:
|
2017-06-12 22:12:17 +02:00
|
|
|
bs2b->filter = (filter_func) bs2b_cross_feed_u8;
|
2014-04-29 19:53:16 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
2017-06-12 22:12:17 +02:00
|
|
|
bs2b->filter = (filter_func) bs2b_cross_feed_s16;
|
2014-04-29 19:53:16 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S32:
|
2017-06-12 22:12:17 +02:00
|
|
|
bs2b->filter = (filter_func) bs2b_cross_feed_s32;
|
2014-04-29 19:53:16 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
2017-06-12 22:12:17 +02:00
|
|
|
bs2b->filter = (filter_func) bs2b_cross_feed_f;
|
2014-04-29 19:53:16 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBL:
|
2017-06-12 22:12:17 +02:00
|
|
|
bs2b->filter = (filter_func) bs2b_cross_feed_d;
|
2014-04-29 19:53:16 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
bs2b_set_srate(bs2b->bs2bp, srate);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad bs2b_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad bs2b_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_bs2b = {
|
2014-04-29 19:53:16 +03:00
|
|
|
.name = "bs2b",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
|
|
|
|
.priv_size = sizeof(Bs2bContext),
|
|
|
|
.priv_class = &bs2b_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(bs2b_inputs),
|
|
|
|
FILTER_OUTPUTS(bs2b_outputs),
|
2024-08-31 11:21:00 +02:00
|
|
|
FILTER_QUERY_FUNC2(query_formats),
|
2014-04-29 19:53:16 +03:00
|
|
|
};
|