2 * This file is part of Libav.
4 * Libav 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.
9 * Libav 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Bauer stereo-to-binaural filter
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
35 typedef void (*filter_func
)(t_bs2bdp bs2bdp
, uint8_t *sample
, int n
);
37 typedef struct Bs2bContext
{
49 #define OFFSET(x) offsetof(Bs2bContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM
52 static const AVOption options
[] = {
53 { "profile", "Apply a pre-defined crossfeed level",
54 OFFSET(profile
), AV_OPT_TYPE_INT
, { .i64
= BS2B_DEFAULT_CLEVEL
}, 0, INT_MAX
, A
, "profile" },
55 { "default", "default profile", 0, AV_OPT_TYPE_CONST
, { .i64
= BS2B_DEFAULT_CLEVEL
}, 0, 0, A
, "profile" },
56 { "cmoy", "Chu Moy circuit", 0, AV_OPT_TYPE_CONST
, { .i64
= BS2B_CMOY_CLEVEL
}, 0, 0, A
, "profile" },
57 { "jmeier", "Jan Meier circuit", 0, AV_OPT_TYPE_CONST
, { .i64
= BS2B_JMEIER_CLEVEL
}, 0, 0, A
, "profile" },
58 { "fcut", "Set cut frequency (in Hz)",
59 OFFSET(fcut
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, BS2B_MAXFCUT
, A
},
60 { "feed", "Set feed level (in Hz)",
61 OFFSET(feed
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, BS2B_MAXFEED
, A
},
65 static const AVClass bs2b_class
= {
66 .class_name
= "bs2b filter",
67 .item_name
= av_default_item_name
,
69 .version
= LIBAVUTIL_VERSION_INT
,
72 static av_cold
int init(AVFilterContext
*ctx
)
74 Bs2bContext
*bs2b
= ctx
->priv
;
76 if (!(bs2b
->bs2bp
= bs2b_open()))
77 return AVERROR(ENOMEM
);
79 bs2b_set_level(bs2b
->bs2bp
, bs2b
->profile
);
82 bs2b_set_level_fcut(bs2b
->bs2bp
, bs2b
->fcut
);
85 bs2b_set_level_feed(bs2b
->bs2bp
, bs2b
->feed
);
90 static av_cold
void uninit(AVFilterContext
*ctx
)
92 Bs2bContext
*bs2b
= ctx
->priv
;
95 bs2b_close(bs2b
->bs2bp
);
98 static int query_formats(AVFilterContext
*ctx
)
100 AVFilterFormats
*formats
= NULL
;
101 AVFilterChannelLayouts
*layouts
= NULL
;
103 static const enum AVSampleFormat sample_fmts
[] = {
112 if (ff_add_channel_layout(&layouts
, AV_CH_LAYOUT_STEREO
) != 0)
113 return AVERROR(ENOMEM
);
114 ff_set_common_channel_layouts(ctx
, layouts
);
116 formats
= ff_make_format_list(sample_fmts
);
118 return AVERROR(ENOMEM
);
119 ff_set_common_formats(ctx
, formats
);
121 formats
= ff_all_samplerates();
123 return AVERROR(ENOMEM
);
124 ff_set_common_samplerates(ctx
, formats
);
129 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*frame
)
134 Bs2bContext
*bs2b
= inlink
->dst
->priv
;
135 AVFilterLink
*outlink
= inlink
->dst
->outputs
[0];
137 if (av_frame_is_writable(frame
)) {
140 out_frame
= ff_get_audio_buffer(inlink
, frame
->nb_samples
);
142 return AVERROR(ENOMEM
);
143 av_frame_copy(out_frame
, frame
);
144 ret
= av_frame_copy_props(out_frame
, frame
);
146 av_frame_free(&out_frame
);
147 av_frame_free(&frame
);
152 bs2b
->filter(bs2b
->bs2bp
, out_frame
->extended_data
[0], out_frame
->nb_samples
);
154 if (frame
!= out_frame
)
155 av_frame_free(&frame
);
157 return ff_filter_frame(outlink
, out_frame
);
160 static int config_output(AVFilterLink
*outlink
)
162 AVFilterContext
*ctx
= outlink
->src
;
163 Bs2bContext
*bs2b
= ctx
->priv
;
164 AVFilterLink
*inlink
= ctx
->inputs
[0];
166 int srate
= inlink
->sample_rate
;
168 switch (inlink
->format
) {
169 case AV_SAMPLE_FMT_U8
:
170 bs2b
->filter
= (filter_func
) bs2b_cross_feed_u8
;
172 case AV_SAMPLE_FMT_S16
:
173 bs2b
->filter
= (filter_func
) bs2b_cross_feed_s16
;
175 case AV_SAMPLE_FMT_S32
:
176 bs2b
->filter
= (filter_func
) bs2b_cross_feed_s32
;
178 case AV_SAMPLE_FMT_FLT
:
179 bs2b
->filter
= (filter_func
) bs2b_cross_feed_f
;
181 case AV_SAMPLE_FMT_DBL
:
182 bs2b
->filter
= (filter_func
) bs2b_cross_feed_d
;
188 if ((srate
< BS2B_MINSRATE
) || (srate
> BS2B_MAXSRATE
))
189 return AVERROR(ENOSYS
);
191 bs2b_set_srate(bs2b
->bs2bp
, srate
);
196 static const AVFilterPad bs2b_inputs
[] = {
199 .type
= AVMEDIA_TYPE_AUDIO
,
200 .filter_frame
= filter_frame
,
205 static const AVFilterPad bs2b_outputs
[] = {
208 .type
= AVMEDIA_TYPE_AUDIO
,
209 .config_props
= config_output
,
214 AVFilter ff_af_bs2b
= {
216 .description
= NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
217 .query_formats
= query_formats
,
218 .priv_size
= sizeof(Bs2bContext
),
219 .priv_class
= &bs2b_class
,
222 .inputs
= bs2b_inputs
,
223 .outputs
= bs2b_outputs
,