Macro qtTrIdx() replaced by tr() and QT_TRANSLATE_NOOP()
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
78 #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
79 #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
80
81 #define spx_int16_t gint16
82 #define spx_int32_t gint32
83 #define spx_uint16_t guint16
84 #define spx_uint32_t guint32
85       
86 #else /* OUTSIDE_SPEEX */
87
88 #ifdef _BUILD_SPEEX
89 # include "speex_types.h"
90 #else
91 # include <speex/speex_types.h>
92 #endif
93
94 #endif /* OUTSIDE_SPEEX */
95
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99
100 #define SPEEX_RESAMPLER_QUALITY_MAX 10
101 #define SPEEX_RESAMPLER_QUALITY_MIN 0
102 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
103 #define SPEEX_RESAMPLER_QUALITY_VOIP 3
104 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
105
106 enum {
107    RESAMPLER_ERR_SUCCESS         = 0,
108    RESAMPLER_ERR_ALLOC_FAILED    = 1,
109    RESAMPLER_ERR_BAD_STATE       = 2,
110    RESAMPLER_ERR_INVALID_ARG     = 3,
111    RESAMPLER_ERR_PTR_OVERLAP     = 4,
112    
113    RESAMPLER_ERR_MAX_ERROR
114 };
115
116 struct SpeexResamplerState_;
117 typedef struct SpeexResamplerState_ SpeexResamplerState;
118
119 /** Create a new resampler with integer input and output rates.
120  * @param nb_channels Number of channels to be processed
121  * @param in_rate Input sampling rate (integer number of Hz).
122  * @param out_rate Output sampling rate (integer number of Hz).
123  * @param quality Resampling quality between 0 and 10, where 0 has poor quality
124  * and 10 has very high quality.
125  * @return Newly created resampler state
126  * @retval NULL Error: not enough memory
127  */
128 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, 
129                                           spx_uint32_t in_rate, 
130                                           spx_uint32_t out_rate, 
131                                           int quality,
132                                           int *err);
133
134 /** Create a new resampler with fractional input/output rates. The sampling 
135  * rate ratio is an arbitrary rational number with both the numerator and 
136  * denominator being 32-bit integers.
137  * @param nb_channels Number of channels to be processed
138  * @param ratio_num Numerator of the sampling rate ratio
139  * @param ratio_den Denominator of the sampling rate ratio
140  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
141  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
142  * @param quality Resampling quality between 0 and 10, where 0 has poor quality
143  * and 10 has very high quality.
144  * @return Newly created resampler state
145  * @retval NULL Error: not enough memory
146  */
147 SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, 
148                                                spx_uint32_t ratio_num, 
149                                                spx_uint32_t ratio_den, 
150                                                spx_uint32_t in_rate, 
151                                                spx_uint32_t out_rate, 
152                                                int quality,
153                                                int *err);
154
155 /** Destroy a resampler state.
156  * @param st Resampler state
157  */
158 void speex_resampler_destroy(SpeexResamplerState *st);
159
160 /** Resample a float array. The input and output buffers must *not* overlap.
161  * @param st Resampler state
162  * @param channel_index Index of the channel to process for the multi-channel 
163  * base (0 otherwise)
164  * @param in Input buffer
165  * @param in_len Number of input samples in the input buffer. Returns the 
166  * number of samples processed
167  * @param out Output buffer
168  * @param out_len Size of the output buffer. Returns the number of samples written
169  */
170 #ifdef DOUBLE_PRECISION
171 int speex_resampler_process_float(SpeexResamplerState *st, 
172                                    spx_uint32_t channel_index, 
173                                    const double *in, 
174                                    spx_uint32_t *in_len, 
175                                    double *out, 
176                                    spx_uint32_t *out_len);
177 #else
178 int speex_resampler_process_float(SpeexResamplerState *st, 
179                                    spx_uint32_t channel_index, 
180                                    const float *in, 
181                                    spx_uint32_t *in_len, 
182                                    float *out, 
183                                    spx_uint32_t *out_len);
184 #endif
185
186 /** Resample an int array. The input and output buffers must *not* overlap.
187  * @param st Resampler state
188  * @param channel_index Index of the channel to process for the multi-channel 
189  * base (0 otherwise)
190  * @param in Input buffer
191  * @param in_len Number of input samples in the input buffer. Returns the number
192  * of samples processed
193  * @param out Output buffer
194  * @param out_len Size of the output buffer. Returns the number of samples written
195  */
196 int speex_resampler_process_int(SpeexResamplerState *st, 
197                                  spx_uint32_t channel_index, 
198                                  const spx_int16_t *in, 
199                                  spx_uint32_t *in_len, 
200                                  spx_int16_t *out, 
201                                  spx_uint32_t *out_len);
202
203 /** Resample an interleaved float array. The input and output buffers must *not* overlap.
204  * @param st Resampler state
205  * @param in Input buffer
206  * @param in_len Number of input samples in the input buffer. Returns the number
207  * of samples processed. This is all per-channel.
208  * @param out Output buffer
209  * @param out_len Size of the output buffer. Returns the number of samples written.
210  * This is all per-channel.
211  */
212 #ifdef DOUBLE_PRECISION
213 int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
214                                                const double *in, 
215                                                spx_uint32_t *in_len, 
216                                                double *out, 
217                                                spx_uint32_t *out_len);
218 #else
219 int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
220                                                const float *in, 
221                                                spx_uint32_t *in_len, 
222                                                float *out, 
223                                                spx_uint32_t *out_len);
224 #endif
225
226 /** Resample an interleaved int array. The input and output buffers must *not* overlap.
227  * @param st Resampler state
228  * @param in Input buffer
229  * @param in_len Number of input samples in the input buffer. Returns the number
230  * of samples processed. This is all per-channel.
231  * @param out Output buffer
232  * @param out_len Size of the output buffer. Returns the number of samples written.
233  * This is all per-channel.
234  */
235 int speex_resampler_process_interleaved_int(SpeexResamplerState *st, 
236                                              const spx_int16_t *in, 
237                                              spx_uint32_t *in_len, 
238                                              spx_int16_t *out, 
239                                              spx_uint32_t *out_len);
240
241 /** Set (change) the input/output sampling rates (integer value).
242  * @param st Resampler state
243  * @param in_rate Input sampling rate (integer number of Hz).
244  * @param out_rate Output sampling rate (integer number of Hz).
245  */
246 int speex_resampler_set_rate(SpeexResamplerState *st, 
247                               spx_uint32_t in_rate, 
248                               spx_uint32_t out_rate);
249
250 /** Get the current input/output sampling rates (integer value).
251  * @param st Resampler state
252  * @param in_rate Input sampling rate (integer number of Hz) copied.
253  * @param out_rate Output sampling rate (integer number of Hz) copied.
254  */
255 void speex_resampler_get_rate(SpeexResamplerState *st, 
256                               spx_uint32_t *in_rate, 
257                               spx_uint32_t *out_rate);
258
259 /** Set (change) the input/output sampling rates and resampling ratio 
260  * (fractional values in Hz supported).
261  * @param st Resampler state
262  * @param ratio_num Numerator of the sampling rate ratio
263  * @param ratio_den Denominator of the sampling rate ratio
264  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
265  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
266  */
267 int speex_resampler_set_rate_frac(SpeexResamplerState *st, 
268                                    spx_uint32_t ratio_num, 
269                                    spx_uint32_t ratio_den, 
270                                    spx_uint32_t in_rate, 
271                                    spx_uint32_t out_rate);
272
273 /** Get the current resampling ratio. This will be reduced to the least
274  * common denominator.
275  * @param st Resampler state
276  * @param ratio_num Numerator of the sampling rate ratio copied
277  * @param ratio_den Denominator of the sampling rate ratio copied
278  */
279 void speex_resampler_get_ratio(SpeexResamplerState *st, 
280                                spx_uint32_t *ratio_num, 
281                                spx_uint32_t *ratio_den);
282
283 /** Set (change) the conversion quality.
284  * @param st Resampler state
285  * @param quality Resampling quality between 0 and 10, where 0 has poor 
286  * quality and 10 has very high quality.
287  */
288 int speex_resampler_set_quality(SpeexResamplerState *st, 
289                                  int quality);
290
291 /** Get the conversion quality.
292  * @param st Resampler state
293  * @param quality Resampling quality between 0 and 10, where 0 has poor 
294  * quality and 10 has very high quality.
295  */
296 void speex_resampler_get_quality(SpeexResamplerState *st, 
297                                  int *quality);
298
299 /** Set (change) the input stride.
300  * @param st Resampler state
301  * @param stride Input stride
302  */
303 void speex_resampler_set_input_stride(SpeexResamplerState *st, 
304                                       spx_uint32_t stride);
305
306 /** Get the input stride.
307  * @param st Resampler state
308  * @param stride Input stride copied
309  */
310 void speex_resampler_get_input_stride(SpeexResamplerState *st, 
311                                       spx_uint32_t *stride);
312
313 /** Set (change) the output stride.
314  * @param st Resampler state
315  * @param stride Output stride
316  */
317 void speex_resampler_set_output_stride(SpeexResamplerState *st, 
318                                       spx_uint32_t stride);
319
320 /** Get the output stride.
321  * @param st Resampler state copied
322  * @param stride Output stride
323  */
324 void speex_resampler_get_output_stride(SpeexResamplerState *st, 
325                                       spx_uint32_t *stride);
326
327 /** Get the latency introduced by the resampler measured in input samples.
328  * @param st Resampler state
329  */
330 int speex_resampler_get_input_latency(SpeexResamplerState *st);
331
332 /** Get the latency introduced by the resampler measured in output samples.
333  * @param st Resampler state
334  */
335 int speex_resampler_get_output_latency(SpeexResamplerState *st);
336
337 /** Get the length of the filter in input samples.
338  * @param st Resampler state
339  */
340 int speex_resampler_get_filt_len(SpeexResamplerState *st);
341
342 /** Make sure that the first samples to go out of the resamplers don't have 
343  * leading zeros. This is only useful before starting to use a newly created 
344  * resampler. It is recommended to use that when resampling an audio file, as
345  * it will generate a file with the same length. For real-time processing,
346  * it is probably easier not to use this call (so that the output duration
347  * is the same for the first frame).
348  * @param st Resampler state
349  */
350 int speex_resampler_skip_zeros(SpeexResamplerState *st);
351
352 /** Reset a resampler so a new (unrelated) stream can be processed.
353  * @param st Resampler state
354  */
355 int speex_resampler_reset_mem(SpeexResamplerState *st);
356
357 /** Returns the English meaning for an error code
358  * @param err Error code
359  * @return English string
360  */
361 const char *speex_resampler_strerror(int err);
362
363 #ifdef __cplusplus
364 }
365 #endif
366
367 #endif