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 /**
20 * @file
21 * Bauer stereo-to-binaural filter
22 */
23
24 #include <bs2b.h>
25
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29
30 #include "audio.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34
35 typedef void (*filter_func)(t_bs2bdp bs2bdp, uint8_t *sample, int n);
36
37 typedef struct Bs2bContext {
38 const AVClass *class;
39
40 int profile;
41 int fcut;
42 int feed;
43
44 t_bs2bdp bs2bp;
45
46 filter_func filter;
47 } Bs2bContext;
48
49 #define OFFSET(x) offsetof(Bs2bContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
51
52 static const AVOption bs2b_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 },
62 { NULL },
63 };
64
65 AVFILTER_DEFINE_CLASS(bs2b);
66
init(AVFilterContext * ctx)67 static av_cold int init(AVFilterContext *ctx)
68 {
69 Bs2bContext *bs2b = ctx->priv;
70
71 if (!(bs2b->bs2bp = bs2b_open()))
72 return AVERROR(ENOMEM);
73
74 bs2b_set_level(bs2b->bs2bp, bs2b->profile);
75
76 if (bs2b->fcut)
77 bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
78
79 if (bs2b->feed)
80 bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
81
82 return 0;
83 }
84
uninit(AVFilterContext * ctx)85 static av_cold void uninit(AVFilterContext *ctx)
86 {
87 Bs2bContext *bs2b = ctx->priv;
88
89 if (bs2b->bs2bp)
90 bs2b_close(bs2b->bs2bp);
91 }
92
query_formats(AVFilterContext * ctx)93 static int query_formats(AVFilterContext *ctx)
94 {
95 AVFilterChannelLayouts *layouts = NULL;
96
97 static const enum AVSampleFormat sample_fmts[] = {
98 AV_SAMPLE_FMT_U8,
99 AV_SAMPLE_FMT_S16,
100 AV_SAMPLE_FMT_S32,
101 AV_SAMPLE_FMT_FLT,
102 AV_SAMPLE_FMT_DBL,
103 AV_SAMPLE_FMT_NONE,
104 };
105 int ret;
106
107 if (ff_add_channel_layout(&layouts, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) != 0)
108 return AVERROR(ENOMEM);
109 ret = ff_set_common_channel_layouts(ctx, layouts);
110 if (ret < 0)
111 return ret;
112
113 ret = ff_set_common_formats_from_list(ctx, sample_fmts);
114 if (ret < 0)
115 return ret;
116
117 return ff_set_common_all_samplerates(ctx);
118 }
119
filter_frame(AVFilterLink * inlink,AVFrame * frame)120 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
121 {
122 int ret;
123 AVFrame *out_frame;
124
125 Bs2bContext *bs2b = inlink->dst->priv;
126 AVFilterLink *outlink = inlink->dst->outputs[0];
127
128 if (av_frame_is_writable(frame)) {
129 out_frame = frame;
130 } else {
131 out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
132 if (!out_frame) {
133 av_frame_free(&frame);
134 return AVERROR(ENOMEM);
135 }
136 av_frame_copy(out_frame, frame);
137 ret = av_frame_copy_props(out_frame, frame);
138 if (ret < 0) {
139 av_frame_free(&out_frame);
140 av_frame_free(&frame);
141 return ret;
142 }
143 }
144
145 bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
146
147 if (frame != out_frame)
148 av_frame_free(&frame);
149
150 return ff_filter_frame(outlink, out_frame);
151 }
152
config_output(AVFilterLink * outlink)153 static int config_output(AVFilterLink *outlink)
154 {
155 AVFilterContext *ctx = outlink->src;
156 Bs2bContext *bs2b = ctx->priv;
157 AVFilterLink *inlink = ctx->inputs[0];
158
159 int srate = inlink->sample_rate;
160
161 switch (inlink->format) {
162 case AV_SAMPLE_FMT_U8:
163 bs2b->filter = (filter_func) bs2b_cross_feed_u8;
164 break;
165 case AV_SAMPLE_FMT_S16:
166 bs2b->filter = (filter_func) bs2b_cross_feed_s16;
167 break;
168 case AV_SAMPLE_FMT_S32:
169 bs2b->filter = (filter_func) bs2b_cross_feed_s32;
170 break;
171 case AV_SAMPLE_FMT_FLT:
172 bs2b->filter = (filter_func) bs2b_cross_feed_f;
173 break;
174 case AV_SAMPLE_FMT_DBL:
175 bs2b->filter = (filter_func) bs2b_cross_feed_d;
176 break;
177 default:
178 return AVERROR_BUG;
179 }
180
181 if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
182 return AVERROR(ENOSYS);
183
184 bs2b_set_srate(bs2b->bs2bp, srate);
185
186 return 0;
187 }
188
189 static const AVFilterPad bs2b_inputs[] = {
190 {
191 .name = "default",
192 .type = AVMEDIA_TYPE_AUDIO,
193 .filter_frame = filter_frame,
194 },
195 };
196
197 static const AVFilterPad bs2b_outputs[] = {
198 {
199 .name = "default",
200 .type = AVMEDIA_TYPE_AUDIO,
201 .config_props = config_output,
202 },
203 };
204
205 const AVFilter ff_af_bs2b = {
206 .name = "bs2b",
207 .description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
208 .priv_size = sizeof(Bs2bContext),
209 .priv_class = &bs2b_class,
210 .init = init,
211 .uninit = uninit,
212 FILTER_INPUTS(bs2b_inputs),
213 FILTER_OUTPUTS(bs2b_outputs),
214 FILTER_QUERY_FUNC(query_formats),
215 };
216