1 /*
2  * The Bauer stereophonic-to-binaural DSP using bs2b library:
3  * http://bs2b.sourceforge.net/
4  *
5  * Copyright (c) 2009 Andrew Savchenko
6  *
7  * This file is part of MPlayer.
8  *
9  * MPlayer is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * MPlayer is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #include <bs2b.h>
25 #include <inttypes.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "mp_msg.h"
30 #include "subopt-helper.h"
31 #include "af.h"
32 
33 /// Internal specific data of the filter
34 struct af_bs2b {
35     int fcut;           ///< cut frequency in Hz
36     int feed;           ///< feed level for low frequencies in 0.1*dB
37     char *profile;      ///< profile (available crossfeed presets)
38     t_bs2bdp filter;    ///< instance of a library filter
39 };
40 
41 #define PLAY(name, type) \
42 static af_data_t *play_##name(struct af_instance_s *af, af_data_t *data) \
43 { \
44     /* filter is called for all pairs of samples available in the buffer */ \
45     bs2b_cross_feed_##name(((struct af_bs2b*)(af->setup))->filter, \
46         (type*)(data->audio), data->len/data->bps/2); \
47 \
48     return data; \
49 }
50 
PLAY(f,float)51 PLAY(f, float)
52 PLAY(fbe, float)
53 PLAY(fle, float)
54 PLAY(s32be, int32_t)
55 PLAY(u32be, uint32_t)
56 PLAY(s32le, int32_t)
57 PLAY(u32le, uint32_t)
58 PLAY(s24be, bs2b_int24_t)
59 PLAY(u24be, bs2b_uint24_t)
60 PLAY(s24le, bs2b_int24_t)
61 PLAY(u24le, bs2b_uint24_t)
62 PLAY(s16be, int16_t)
63 PLAY(u16be, uint16_t)
64 PLAY(s16le, int16_t)
65 PLAY(u16le, uint16_t)
66 PLAY(s8, int8_t)
67 PLAY(u8, uint8_t)
68 
69 /// Sanity check for fcut value
70 static int test_fcut(void *par)
71 {
72     const int val = *(int*)par;
73     if (val >= BS2B_MINFCUT && val <= BS2B_MAXFCUT)
74         return 1;
75 
76     mp_msg(MSGT_AFILTER, MSGL_ERR,
77            "[bs2b] Cut frequency must be in range [%d..%d], but current value is %d.\n",
78            BS2B_MINFCUT, BS2B_MAXFCUT, val);
79     return 0;
80 }
81 
82 /// Sanity check for feed value
test_feed(void * par)83 static int test_feed(void *par)
84 {
85     const int val = *(int*)par;
86     if (val >= BS2B_MINFEED && val <= BS2B_MAXFEED)
87         return 1;
88 
89     mp_msg(MSGT_AFILTER, MSGL_ERR,
90            "[bs2b] Feed level must be in range [%d..%d], but current value is %d.\n",
91            BS2B_MINFEED, BS2B_MAXFEED, val);
92     return 0;
93 }
94 
95 /// Initialization and runtime control
control(struct af_instance_s * af,int cmd,void * arg)96 static int control(struct af_instance_s *af, int cmd, void *arg)
97 {
98     struct af_bs2b *s = af->setup;
99 
100     switch (cmd) {
101     case AF_CONTROL_REINIT: {
102         int format;
103         char buf[256];
104         // Sanity check
105         if (!arg) return AF_ERROR;
106 
107         format           = ((af_data_t*)arg)->format;
108         af->data->rate   = ((af_data_t*)arg)->rate;
109         af->data->nch    = 2;     // bs2b is useful only for 2ch audio
110         af->data->bps    = ((af_data_t*)arg)->bps;
111         af->data->format = format;
112 
113         /* check for formats supported by libbs2b
114            and assign corresponding handlers */
115         switch (format) {
116             case AF_FORMAT_FLOAT_BE:
117                 af->play = play_fbe;
118                 break;
119             case AF_FORMAT_FLOAT_LE:
120                 af->play = play_fle;
121                 break;
122             case AF_FORMAT_S32_BE:
123                 af->play = play_s32be;
124                 break;
125             case AF_FORMAT_U32_BE:
126                 af->play = play_u32be;
127                 break;
128             case AF_FORMAT_S32_LE:
129                 af->play = play_s32le;
130                 break;
131             case AF_FORMAT_U32_LE:
132                 af->play = play_u32le;
133                 break;
134             case AF_FORMAT_S24_BE:
135                 af->play = play_s24be;
136                 break;
137             case AF_FORMAT_U24_BE:
138                 af->play = play_u24be;
139                 break;
140             case AF_FORMAT_S24_LE:
141                 af->play = play_s24le;
142                 break;
143             case AF_FORMAT_U24_LE:
144                 af->play = play_u24le;
145                 break;
146             case AF_FORMAT_S16_BE:
147                 af->play = play_s16be;
148                 break;
149             case AF_FORMAT_U16_BE:
150                 af->play = play_u16be;
151                 break;
152             case AF_FORMAT_S16_LE:
153                 af->play = play_s16le;
154                 break;
155             case AF_FORMAT_U16_LE:
156                 af->play = play_u16le;
157                 break;
158             case AF_FORMAT_S8:
159                 af->play = play_s8;
160                 break;
161             case AF_FORMAT_U8:
162                 af->play = play_u8;
163                 break;
164             default:
165                 af->play = play_f;
166                 af->data->format = AF_FORMAT_FLOAT_NE;
167                 af->data->bps = 4;
168                 break;
169         }
170 
171         // bs2b have srate limits, try to resample if needed
172         if (af->data->rate > BS2B_MAXSRATE || af->data->rate < BS2B_MINSRATE) {
173             af->data->rate = BS2B_DEFAULT_SRATE;
174             mp_msg(MSGT_AFILTER, MSGL_WARN,
175                    "[bs2b] Requested sample rate %d Hz is out of bounds [%d..%d] Hz.\n"
176                    "[bs2b] Trying to resample to %d Hz.\n",
177                    af->data->rate, BS2B_MINSRATE, BS2B_MAXSRATE, BS2B_DEFAULT_SRATE);
178         }
179         bs2b_set_srate(s->filter, (long)af->data->rate);
180         mp_msg(MSGT_AFILTER, MSGL_V, "[bs2b] using format %s\n",
181                af_fmt2str(af->data->format,buf,256));
182 
183         return af_test_output(af,(af_data_t*)arg);
184     }
185     case AF_CONTROL_COMMAND_LINE: {
186         const opt_t subopts[] = {
187             {"fcut",    OPT_ARG_INT,   &s->fcut,    test_fcut},
188             {"feed",    OPT_ARG_INT,   &s->feed,    test_feed},
189             {"profile", OPT_ARG_MSTRZ, &s->profile, NULL},
190             {NULL}
191         };
192         if (subopt_parse(arg, subopts) != 0) {
193             mp_msg(MSGT_AFILTER, MSGL_ERR, "[bs2b] Invalid option specified.\n");
194             free(s->profile);
195             return AF_ERROR;
196         }
197         // parse profile if specified
198         if (s->profile) {
199             if (!strcmp(s->profile, "default"))
200                 bs2b_set_level(s->filter, BS2B_DEFAULT_CLEVEL);
201             else if (!strcmp(s->profile, "cmoy"))
202                 bs2b_set_level(s->filter, BS2B_CMOY_CLEVEL);
203             else if (!strcmp(s->profile, "jmeier"))
204                 bs2b_set_level(s->filter, BS2B_JMEIER_CLEVEL);
205             else {
206                 mp_msg(MSGT_AFILTER, MSGL_ERR,
207                        "[bs2b] Invalid profile specified: %s.\n"
208                        "[bs2b] Available profiles are: default, cmoy, jmeier.\n",
209                        s->profile);
210                 free(s->profile);
211                 return AF_ERROR;
212             }
213         }
214         // set fcut and feed only if specified, otherwise defaults will be used
215         if (s->fcut)
216             bs2b_set_level_fcut(s->filter, s->fcut);
217         if (s->feed)
218             bs2b_set_level_feed(s->filter, s->feed);
219 
220         mp_msg(MSGT_AFILTER, MSGL_V,
221                "[bs2b] using cut frequency %d, LF feed level %d\n",
222                bs2b_get_level_fcut(s->filter), bs2b_get_level_feed(s->filter));
223         free(s->profile);
224         return AF_OK;
225     }
226     }
227     return AF_UNKNOWN;
228 }
229 
230 /// Deallocate memory and close library
uninit(struct af_instance_s * af)231 static void uninit(struct af_instance_s *af)
232 {
233     struct af_bs2b *s = af->setup;
234     free(af->data);
235     if (s && s->filter)
236         bs2b_close(s->filter);
237     free(s);
238 }
239 
240 /// Allocate memory, set function pointers and init library
af_open(af_instance_t * af)241 static int af_open(af_instance_t *af)
242 {
243     struct af_bs2b *s;
244     af->control = control;
245     af->uninit  = uninit;
246     af->mul     = 1;
247     if (!(af->data = calloc(1, sizeof(af_data_t))))
248         return AF_ERROR;
249     if (!(af->setup = s = calloc(1, sizeof(struct af_bs2b)))) {
250         free(af->data);
251         return AF_ERROR;
252     }
253 
254     // NULL means failed initialization
255     if (!(s->filter = bs2b_open())) {
256         free(af->data);
257         free(af->setup);
258         return AF_ERROR;
259     }
260     // Set zero defaults indicating no option was specified.
261     s->profile = NULL;
262     s->fcut    = 0;
263     s->feed    = 0;
264     return AF_OK;
265 }
266 
267 /// Description of this filter
268 const af_info_t af_info_bs2b = {
269     "Bauer stereophonic-to-binaural audio filter",
270     "bs2b",
271     "Andrew Savchenko",
272     "",
273     AF_FLAGS_REENTRANT,
274     af_open
275 };
276