ssaparse: ignore invalid UTF-8 in init section
[platform/upstream/gstreamer.git] / gst / audioresample / speex_resampler.h
1 /* Copyright (C) 2007 Jean-Marc Valin
2       
3    File: speex_resampler.h
4    Resampling code
5       
6    The design goals of this code are:
7       - Very fast algorithm
8       - Low memory requirement
9       - Good *perceptual* quality (and not best SNR)
10
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions are
13    met:
14
15    1. Redistributions of source code must retain the above copyright notice,
16    this list of conditions and the following disclaimer.
17
18    2. Redistributions in binary form must reproduce the above copyright
19    notice, this list of conditions and the following disclaimer in the
20    documentation and/or other materials provided with the distribution.
21
22    3. The name of the author may not be used to endorse or promote products
23    derived from this software without specific prior written permission.
24
25    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28    DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35    POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39 #ifndef SPEEX_RESAMPLER_H
40 #define SPEEX_RESAMPLER_H
41
42 #ifdef OUTSIDE_SPEEX
43
44 /********* WARNING: MENTAL SANITY ENDS HERE *************/
45
46 /* If the resampler is defined outside of Speex, we change the symbol names so that 
47    there won't be any clash if linking with Speex later on. */
48
49 /* #define RANDOM_PREFIX your software name here */
50 #ifndef RANDOM_PREFIX
51 #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
52 #endif
53
54 #define CAT_PREFIX2(a,b) a ## b
55 #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
56       
57 #define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
58 #define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
59 #define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
60 #define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
61 #define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
62 #define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
63 #define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
64 #define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
65 #define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
66 #define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
67 #define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
68 #define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
69 #define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
70 #define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
71 #define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
72 #define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
73 #define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
74 #define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)
75 #define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)
76 #define speex_resampler_get_filt_len CAT_PREFIX(RANDOM_PREFIX,_resampler_get_filt_len)
77 #define speex_resampler_get_sinc_filter_mode CAT_PREFIX(RANDOM_PREFIX,_resampler_get_sinc_filter_mode)
78 #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
79 #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
80 #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
81
82 #define spx_int16_t gint16
83 #define spx_int32_t gint32
84 #define spx_uint16_t guint16
85 #define spx_uint32_t guint32
86       
87 #else /* OUTSIDE_SPEEX */
88
89 #ifdef _BUILD_SPEEX
90 # include "speex_types.h"
91 #else
92 # include <speex/speex_types.h>
93 #endif
94
95 #endif /* OUTSIDE_SPEEX */
96
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100
101 #define SPEEX_RESAMPLER_QUALITY_MAX 10
102 #define SPEEX_RESAMPLER_QUALITY_MIN 0
103 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
104 #define SPEEX_RESAMPLER_QUALITY_VOIP 3
105 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
106
107 enum {
108    RESAMPLER_ERR_SUCCESS         = 0,
109    RESAMPLER_ERR_ALLOC_FAILED    = 1,
110    RESAMPLER_ERR_BAD_STATE       = 2,
111    RESAMPLER_ERR_INVALID_ARG     = 3,
112    RESAMPLER_ERR_PTR_OVERLAP     = 4,
113    
114    RESAMPLER_ERR_MAX_ERROR
115 };
116
117 typedef enum {
118    RESAMPLER_SINC_FILTER_INTERPOLATED   = 0,
119    RESAMPLER_SINC_FILTER_FULL           = 1,
120    RESAMPLER_SINC_FILTER_AUTO           = 2
121 } SpeexResamplerSincFilterMode;
122
123 #define RESAMPLER_SINC_FILTER_DEFAULT RESAMPLER_SINC_FILTER_INTERPOLATED
124 #define RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT (1 * 1048576)
125
126 struct SpeexResamplerState_;
127 typedef struct SpeexResamplerState_ SpeexResamplerState;
128
129 /** Create a new resampler with integer input and output rates.
130  * @param nb_channels Number of channels to be processed
131  * @param in_rate Input sampling rate (integer number of Hz).
132  * @param out_rate Output sampling rate (integer number of Hz).
133  * @param quality Resampling quality between 0 and 10, where 0 has poor quality
134  * and 10 has very high quality.
135  * @param sinc_filter_mode Sinc filter table mode to use
136  * @param sinc_filter_auto_threshold Threshold to use if sinc filter mode is auto, in bytes
137  * @return Newly created resampler state
138  * @retval NULL Error: not enough memory
139  *
140  * If a full filter table would be larger than the auto threshold, and sinc_filter_mode is AUTO,
141  * the resample uses the interpolated mode instead
142  *
143  * @note A full sinc table can significantly improve the resampler's performance, but calculating the table
144  * takes longer, as opposed to the interpolated variant
145  */
146 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, 
147                                           spx_uint32_t in_rate, 
148                                           spx_uint32_t out_rate, 
149                                           int quality,
150                                           SpeexResamplerSincFilterMode sinc_filter_mode,
151                                           spx_uint32_t sinc_filter_auto_threshold,
152                                           int *err);
153
154 /** Create a new resampler with fractional input/output rates. The sampling 
155  * rate ratio is an arbitrary rational number with both the numerator and 
156  * denominator being 32-bit integers.
157  * @param nb_channels Number of channels to be processed
158  * @param ratio_num Numerator of the sampling rate ratio
159  * @param ratio_den Denominator of the sampling rate ratio
160  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
161  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
162  * @param quality Resampling quality between 0 and 10, where 0 has poor quality
163  * and 10 has very high quality.
164  * @param sinc_filter_mode Sinc filter table mode to use
165  * @param sinc_filter_auto_threshold Threshold to use if sinc filter mode is auto, in bytes
166  * @return Newly created resampler state
167  * @retval NULL Error: not enough memory
168  *
169  * If a full filter table would be larger than the auto threshold, and sinc_filter_mode is AUTO,
170  * the resample uses the interpolated mode instead
171  *
172  * @note A full sinc table can significantly improve the resampler's performance, but calculating the table
173  * takes longer, as opposed to the interpolated variant
174  */
175 SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, 
176                                                spx_uint32_t ratio_num, 
177                                                spx_uint32_t ratio_den, 
178                                                spx_uint32_t in_rate, 
179                                                spx_uint32_t out_rate, 
180                                                int quality,
181                                                SpeexResamplerSincFilterMode sinc_filter_mode,
182                                                spx_uint32_t sinc_filter_auto_threshold,
183                                                int *err);
184
185 /** Destroy a resampler state.
186  * @param st Resampler state
187  */
188 void speex_resampler_destroy(SpeexResamplerState *st);
189
190 /** Resample a float array. The input and output buffers must *not* overlap.
191  * @param st Resampler state
192  * @param channel_index Index of the channel to process for the multi-channel 
193  * base (0 otherwise)
194  * @param in Input buffer
195  * @param in_len Number of input samples in the input buffer. Returns the 
196  * number of samples processed
197  * @param out Output buffer
198  * @param out_len Size of the output buffer. Returns the number of samples written
199  */
200 #ifdef DOUBLE_PRECISION
201 int speex_resampler_process_float(SpeexResamplerState *st, 
202                                    spx_uint32_t channel_index, 
203                                    const double *in, 
204                                    spx_uint32_t *in_len, 
205                                    double *out, 
206                                    spx_uint32_t *out_len);
207 #else
208 int speex_resampler_process_float(SpeexResamplerState *st, 
209                                    spx_uint32_t channel_index, 
210                                    const float *in, 
211                                    spx_uint32_t *in_len, 
212                                    float *out, 
213                                    spx_uint32_t *out_len);
214 #endif
215
216 /** Resample an int array. The input and output buffers must *not* overlap.
217  * @param st Resampler state
218  * @param channel_index Index of the channel to process for the multi-channel 
219  * base (0 otherwise)
220  * @param in Input buffer
221  * @param in_len Number of input samples in the input buffer. Returns the number
222  * of samples processed
223  * @param out Output buffer
224  * @param out_len Size of the output buffer. Returns the number of samples written
225  */
226 int speex_resampler_process_int(SpeexResamplerState *st, 
227                                  spx_uint32_t channel_index, 
228                                  const spx_int16_t *in, 
229                                  spx_uint32_t *in_len, 
230                                  spx_int16_t *out, 
231                                  spx_uint32_t *out_len);
232
233 /** Resample an interleaved float array. The input and output buffers must *not* overlap.
234  * @param st Resampler state
235  * @param in Input buffer
236  * @param in_len Number of input samples in the input buffer. Returns the number
237  * of samples processed. This is all per-channel.
238  * @param out Output buffer
239  * @param out_len Size of the output buffer. Returns the number of samples written.
240  * This is all per-channel.
241  */
242 #ifdef DOUBLE_PRECISION
243 int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
244                                                const double *in, 
245                                                spx_uint32_t *in_len, 
246                                                double *out, 
247                                                spx_uint32_t *out_len);
248 #else
249 int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
250                                                const float *in, 
251                                                spx_uint32_t *in_len, 
252                                                float *out, 
253                                                spx_uint32_t *out_len);
254 #endif
255
256 /** Resample an interleaved int array. The input and output buffers must *not* overlap.
257  * @param st Resampler state
258  * @param in Input buffer
259  * @param in_len Number of input samples in the input buffer. Returns the number
260  * of samples processed. This is all per-channel.
261  * @param out Output buffer
262  * @param out_len Size of the output buffer. Returns the number of samples written.
263  * This is all per-channel.
264  */
265 int speex_resampler_process_interleaved_int(SpeexResamplerState *st, 
266                                              const spx_int16_t *in, 
267                                              spx_uint32_t *in_len, 
268                                              spx_int16_t *out, 
269                                              spx_uint32_t *out_len);
270
271 /** Set (change) the input/output sampling rates (integer value).
272  * @param st Resampler state
273  * @param in_rate Input sampling rate (integer number of Hz).
274  * @param out_rate Output sampling rate (integer number of Hz).
275  */
276 int speex_resampler_set_rate(SpeexResamplerState *st, 
277                               spx_uint32_t in_rate, 
278                               spx_uint32_t out_rate);
279
280 /** Get the current input/output sampling rates (integer value).
281  * @param st Resampler state
282  * @param in_rate Input sampling rate (integer number of Hz) copied.
283  * @param out_rate Output sampling rate (integer number of Hz) copied.
284  */
285 void speex_resampler_get_rate(SpeexResamplerState *st, 
286                               spx_uint32_t *in_rate, 
287                               spx_uint32_t *out_rate);
288
289 /** Set (change) the input/output sampling rates and resampling ratio 
290  * (fractional values in Hz supported).
291  * @param st Resampler state
292  * @param ratio_num Numerator of the sampling rate ratio
293  * @param ratio_den Denominator of the sampling rate ratio
294  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
295  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
296  */
297 int speex_resampler_set_rate_frac(SpeexResamplerState *st, 
298                                    spx_uint32_t ratio_num, 
299                                    spx_uint32_t ratio_den, 
300                                    spx_uint32_t in_rate, 
301                                    spx_uint32_t out_rate);
302
303 /** Get the current resampling ratio. This will be reduced to the least
304  * common denominator.
305  * @param st Resampler state
306  * @param ratio_num Numerator of the sampling rate ratio copied
307  * @param ratio_den Denominator of the sampling rate ratio copied
308  */
309 void speex_resampler_get_ratio(SpeexResamplerState *st, 
310                                spx_uint32_t *ratio_num, 
311                                spx_uint32_t *ratio_den);
312
313 /** Set (change) the conversion quality.
314  * @param st Resampler state
315  * @param quality Resampling quality between 0 and 10, where 0 has poor 
316  * quality and 10 has very high quality.
317  */
318 int speex_resampler_set_quality(SpeexResamplerState *st, 
319                                  int quality);
320
321 /** Get the conversion quality.
322  * @param st Resampler state
323  * @param quality Resampling quality between 0 and 10, where 0 has poor 
324  * quality and 10 has very high quality.
325  */
326 void speex_resampler_get_quality(SpeexResamplerState *st, 
327                                  int *quality);
328
329 /** Set (change) the input stride.
330  * @param st Resampler state
331  * @param stride Input stride
332  */
333 void speex_resampler_set_input_stride(SpeexResamplerState *st, 
334                                       spx_uint32_t stride);
335
336 /** Get the input stride.
337  * @param st Resampler state
338  * @param stride Input stride copied
339  */
340 void speex_resampler_get_input_stride(SpeexResamplerState *st, 
341                                       spx_uint32_t *stride);
342
343 /** Set (change) the output stride.
344  * @param st Resampler state
345  * @param stride Output stride
346  */
347 void speex_resampler_set_output_stride(SpeexResamplerState *st, 
348                                       spx_uint32_t stride);
349
350 /** Get the output stride.
351  * @param st Resampler state copied
352  * @param stride Output stride
353  */
354 void speex_resampler_get_output_stride(SpeexResamplerState *st, 
355                                       spx_uint32_t *stride);
356
357 /** Get the latency introduced by the resampler measured in input samples.
358  * @param st Resampler state
359  */
360 int speex_resampler_get_input_latency(SpeexResamplerState *st);
361
362 /** Get the latency introduced by the resampler measured in output samples.
363  * @param st Resampler state
364  */
365 int speex_resampler_get_output_latency(SpeexResamplerState *st);
366
367 /** Get the length of the filter in input samples.
368  * @param st Resampler state
369  */
370 int speex_resampler_get_filt_len(SpeexResamplerState *st);
371
372 /** Returns 1 if the full sinc filter table is used, 0 if the interpolated one is used
373  * @param st Resampler state
374  * @return Sinc filter mode
375  */
376 int speex_resampler_get_sinc_filter_mode(SpeexResamplerState *st);
377
378 /** Make sure that the first samples to go out of the resamplers don't have 
379  * leading zeros. This is only useful before starting to use a newly created 
380  * resampler. It is recommended to use that when resampling an audio file, as
381  * it will generate a file with the same length. For real-time processing,
382  * it is probably easier not to use this call (so that the output duration
383  * is the same for the first frame).
384  * @param st Resampler state
385  */
386 int speex_resampler_skip_zeros(SpeexResamplerState *st);
387
388 /** Reset a resampler so a new (unrelated) stream can be processed.
389  * @param st Resampler state
390  */
391 int speex_resampler_reset_mem(SpeexResamplerState *st);
392
393 /** Returns the English meaning for an error code
394  * @param err Error code
395  * @return English string
396  */
397 const char *speex_resampler_strerror(int err);
398
399 #ifdef __cplusplus
400 }
401 #endif
402
403 #endif