]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_bs2b.c
Merge commit '8a66fd40260b7aae6226d68c4dbad43b05a8e524'
[ffmpeg] / libavfilter / af_bs2b.c
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 struct Bs2bContext {
36     const AVClass *class;
37
38     int profile;
39     int fcut;
40     int feed;
41
42     t_bs2bdp bs2bp;
43
44     void (*filter)(t_bs2bdp bs2bdp, uint8_t *sample, int n);
45 } Bs2bContext;
46
47 #define OFFSET(x) offsetof(Bs2bContext, x)
48 #define A AV_OPT_FLAG_AUDIO_PARAM
49
50 static const AVOption options[] = {
51     { "profile", "Apply a pre-defined crossfeed level",
52             OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, INT_MAX, A, "profile" },
53         { "default", "default profile", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, 0, A, "profile" },
54         { "cmoy",    "Chu Moy circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_CMOY_CLEVEL    }, 0, 0, A, "profile" },
55         { "jmeier",  "Jan Meier circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_JMEIER_CLEVEL  }, 0, 0, A, "profile" },
56     { "fcut", "Set cut frequency (in Hz)",
57             OFFSET(fcut), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFCUT, A },
58     { "feed", "Set feed level (in Hz)",
59             OFFSET(feed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFEED, A },
60     { NULL },
61 };
62
63 static const AVClass bs2b_class = {
64     .class_name = "bs2b filter",
65     .item_name  = av_default_item_name,
66     .option     = options,
67     .version    = LIBAVUTIL_VERSION_INT,
68 };
69
70 static av_cold int init(AVFilterContext *ctx)
71 {
72     Bs2bContext *bs2b = ctx->priv;
73
74     if (!(bs2b->bs2bp = bs2b_open()))
75         return AVERROR(ENOMEM);
76
77     bs2b_set_level(bs2b->bs2bp, bs2b->profile);
78
79     if (bs2b->fcut)
80         bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
81
82     if (bs2b->feed)
83         bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
84
85     return 0;
86 }
87
88 static av_cold void uninit(AVFilterContext *ctx)
89 {
90     Bs2bContext *bs2b = ctx->priv;
91
92     if (bs2b->bs2bp)
93         bs2b_close(bs2b->bs2bp);
94 }
95
96 static int query_formats(AVFilterContext *ctx)
97 {
98     AVFilterFormats *formats = NULL;
99     AVFilterChannelLayouts *layouts = NULL;
100
101     static const enum AVSampleFormat sample_fmts[] = {
102         AV_SAMPLE_FMT_U8,
103         AV_SAMPLE_FMT_S16,
104         AV_SAMPLE_FMT_S32,
105         AV_SAMPLE_FMT_FLT,
106         AV_SAMPLE_FMT_DBL,
107         AV_SAMPLE_FMT_NONE,
108     };
109     int ret;
110
111     if (ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO) != 0)
112         return AVERROR(ENOMEM);
113     ret = ff_set_common_channel_layouts(ctx, layouts);
114     if (ret < 0)
115         return ret;
116
117     formats = ff_make_format_list(sample_fmts);
118     if (!formats)
119         return AVERROR(ENOMEM);
120     ret = ff_set_common_formats(ctx, formats);
121     if (ret < 0)
122         return ret;
123
124     formats = ff_all_samplerates();
125     if (!formats)
126         return AVERROR(ENOMEM);
127     return ff_set_common_samplerates(ctx, formats);
128 }
129
130 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
131 {
132     int ret;
133     AVFrame *out_frame;
134
135     Bs2bContext     *bs2b = inlink->dst->priv;
136     AVFilterLink *outlink = inlink->dst->outputs[0];
137
138     if (av_frame_is_writable(frame)) {
139         out_frame = frame;
140     } else {
141         out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
142         if (!out_frame)
143             return AVERROR(ENOMEM);
144         av_frame_copy(out_frame, frame);
145         ret = av_frame_copy_props(out_frame, frame);
146         if (ret < 0) {
147             av_frame_free(&out_frame);
148             av_frame_free(&frame);
149             return ret;
150         }
151     }
152
153     bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
154
155     if (frame != out_frame)
156         av_frame_free(&frame);
157
158     return ff_filter_frame(outlink, out_frame);
159 }
160
161 static int config_output(AVFilterLink *outlink)
162 {
163     AVFilterContext *ctx = outlink->src;
164     Bs2bContext    *bs2b = ctx->priv;
165     AVFilterLink *inlink = ctx->inputs[0];
166
167     int srate = inlink->sample_rate;
168
169     switch (inlink->format) {
170     case AV_SAMPLE_FMT_U8:
171         bs2b->filter = bs2b_cross_feed_u8;
172         break;
173     case AV_SAMPLE_FMT_S16:
174         bs2b->filter = (void*)bs2b_cross_feed_s16;
175         break;
176     case AV_SAMPLE_FMT_S32:
177         bs2b->filter = (void*)bs2b_cross_feed_s32;
178         break;
179     case AV_SAMPLE_FMT_FLT:
180         bs2b->filter = (void*)bs2b_cross_feed_f;
181         break;
182     case AV_SAMPLE_FMT_DBL:
183         bs2b->filter = (void*)bs2b_cross_feed_d;
184         break;
185     default:
186         return AVERROR_BUG;
187     }
188
189     if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
190         return AVERROR(ENOSYS);
191
192     bs2b_set_srate(bs2b->bs2bp, srate);
193
194     return 0;
195 }
196
197 static const AVFilterPad bs2b_inputs[] = {
198     {
199         .name           = "default",
200         .type           = AVMEDIA_TYPE_AUDIO,
201         .filter_frame   = filter_frame,
202     },
203     { NULL }
204 };
205
206 static const AVFilterPad bs2b_outputs[] = {
207     {
208         .name           = "default",
209         .type           = AVMEDIA_TYPE_AUDIO,
210         .config_props   = config_output,
211     },
212     { NULL }
213 };
214
215 AVFilter ff_af_bs2b = {
216     .name           = "bs2b",
217     .description    = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
218     .query_formats  = query_formats,
219     .priv_size      = sizeof(Bs2bContext),
220     .priv_class     = &bs2b_class,
221     .init           = init,
222     .uninit         = uninit,
223     .inputs         = bs2b_inputs,
224     .outputs        = bs2b_outputs,
225 };