1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (audio_resampler.h).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __LIBRETRO_SDK_AUDIO_RESAMPLER_DRIVER_H
24 #define __LIBRETRO_SDK_AUDIO_RESAMPLER_DRIVER_H
25 
26 #include <stdint.h>
27 #include <stddef.h>
28 
29 #include <boolean.h>
30 #include <retro_common_api.h>
31 
32 RETRO_BEGIN_DECLS
33 
34 #define RESAMPLER_SIMD_SSE      (1 << 0)
35 #define RESAMPLER_SIMD_SSE2     (1 << 1)
36 #define RESAMPLER_SIMD_VMX      (1 << 2)
37 #define RESAMPLER_SIMD_VMX128   (1 << 3)
38 #define RESAMPLER_SIMD_AVX      (1 << 4)
39 #define RESAMPLER_SIMD_NEON     (1 << 5)
40 #define RESAMPLER_SIMD_SSE3     (1 << 6)
41 #define RESAMPLER_SIMD_SSSE3    (1 << 7)
42 #define RESAMPLER_SIMD_MMX      (1 << 8)
43 #define RESAMPLER_SIMD_MMXEXT   (1 << 9)
44 #define RESAMPLER_SIMD_SSE4     (1 << 10)
45 #define RESAMPLER_SIMD_SSE42    (1 << 11)
46 #define RESAMPLER_SIMD_AVX2     (1 << 12)
47 #define RESAMPLER_SIMD_VFPU     (1 << 13)
48 #define RESAMPLER_SIMD_PS       (1 << 14)
49 
50 enum resampler_quality
51 {
52    RESAMPLER_QUALITY_DONTCARE = 0,
53    RESAMPLER_QUALITY_LOWEST,
54    RESAMPLER_QUALITY_LOWER,
55    RESAMPLER_QUALITY_NORMAL,
56    RESAMPLER_QUALITY_HIGHER,
57    RESAMPLER_QUALITY_HIGHEST
58 };
59 
60 /* A bit-mask of all supported SIMD instruction sets.
61  * Allows an implementation to pick different
62  * resampler_implementation structs.
63  */
64 typedef unsigned resampler_simd_mask_t;
65 
66 #define RESAMPLER_API_VERSION 1
67 
68 struct resampler_data
69 {
70    const float *data_in;
71    float *data_out;
72 
73    size_t input_frames;
74    size_t output_frames;
75 
76    double ratio;
77 };
78 
79 /* Returns true if config key was found. Otherwise,
80  * returns false, and sets value to default value.
81  */
82 typedef int (*resampler_config_get_float_t)(void *userdata,
83       const char *key, float *value, float default_value);
84 
85 typedef int (*resampler_config_get_int_t)(void *userdata,
86       const char *key, int *value, int default_value);
87 
88 /* Allocates an array with values. free() with resampler_config_free_t. */
89 typedef int (*resampler_config_get_float_array_t)(void *userdata,
90       const char *key, float **values, unsigned *out_num_values,
91       const float *default_values, unsigned num_default_values);
92 
93 typedef int (*resampler_config_get_int_array_t)(void *userdata,
94       const char *key, int **values, unsigned *out_num_values,
95       const int *default_values, unsigned num_default_values);
96 
97 typedef int (*resampler_config_get_string_t)(void *userdata,
98       const char *key, char **output, const char *default_output);
99 
100 /* Calls free() in host runtime. Sometimes needed on Windows.
101  * free() on NULL is fine. */
102 typedef void (*resampler_config_free_t)(void *ptr);
103 
104 struct resampler_config
105 {
106    resampler_config_get_float_t get_float;
107    resampler_config_get_int_t get_int;
108 
109    resampler_config_get_float_array_t get_float_array;
110    resampler_config_get_int_array_t get_int_array;
111 
112    resampler_config_get_string_t get_string;
113    /* Avoid problems where resampler plug and host are
114     * linked against different C runtimes. */
115    resampler_config_free_t free;
116 };
117 
118 /* Bandwidth factor. Will be < 1.0 for downsampling, > 1.0 for upsampling.
119  * Corresponds to expected resampling ratio. */
120 typedef void *(*resampler_init_t)(const struct resampler_config *config,
121       double bandwidth_mod, enum resampler_quality quality,
122       resampler_simd_mask_t mask);
123 
124 /* Frees the handle. */
125 typedef void (*resampler_free_t)(void *data);
126 
127 /* Processes input data. */
128 typedef void (*resampler_process_t)(void *_data, struct resampler_data *data);
129 
130 typedef struct retro_resampler
131 {
132    resampler_init_t     init;
133    resampler_process_t  process;
134    resampler_free_t     free;
135 
136    /* Must be RESAMPLER_API_VERSION */
137    unsigned api_version;
138 
139    /* Human readable identifier of implementation. */
140    const char *ident;
141 
142    /* Computer-friendly short version of ident.
143     * Lower case, no spaces and special characters, etc. */
144    const char *short_ident;
145 } retro_resampler_t;
146 
147 typedef struct audio_frame_float
148 {
149    float l;
150    float r;
151 } audio_frame_float_t;
152 
153 extern retro_resampler_t sinc_resampler;
154 #ifdef HAVE_CC_RESAMPLER
155 extern retro_resampler_t CC_resampler;
156 #endif
157 extern retro_resampler_t nearest_resampler;
158 
159 /**
160  * audio_resampler_driver_find_handle:
161  * @index              : index of driver to get handle to.
162  *
163  * Returns: handle to audio resampler driver at index. Can be NULL
164  * if nothing found.
165  **/
166 const void *audio_resampler_driver_find_handle(int index);
167 
168 /**
169  * audio_resampler_driver_find_ident:
170  * @index              : index of driver to get handle to.
171  *
172  * Returns: Human-readable identifier of audio resampler driver at index.
173  * Can be NULL if nothing found.
174  **/
175 const char *audio_resampler_driver_find_ident(int index);
176 
177 /**
178  * retro_resampler_realloc:
179  * @re                         : Resampler handle
180  * @backend                    : Resampler backend that is about to be set.
181  * @ident                      : Identifier name for resampler we want.
182  * @bw_ratio                   : Bandwidth ratio.
183  *
184  * Reallocates resampler. Will free previous handle before
185  * allocating a new one. If ident is NULL, first resampler will be used.
186  *
187  * Returns: true (1) if successful, otherwise false (0).
188  **/
189 bool retro_resampler_realloc(void **re, const retro_resampler_t **backend,
190       const char *ident, enum resampler_quality quality, double bw_ratio);
191 
192 RETRO_END_DECLS
193 
194 #endif
195