media/libspeex_resampler/src/resample.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Copyright (C) 2007-2008 Jean-Marc Valin
michael@0 2 Copyright (C) 2008 Thorvald Natvig
michael@0 3
michael@0 4 File: resample.c
michael@0 5 Arbitrary resampling code
michael@0 6
michael@0 7 Redistribution and use in source and binary forms, with or without
michael@0 8 modification, are permitted provided that the following conditions are
michael@0 9 met:
michael@0 10
michael@0 11 1. Redistributions of source code must retain the above copyright notice,
michael@0 12 this list of conditions and the following disclaimer.
michael@0 13
michael@0 14 2. Redistributions in binary form must reproduce the above copyright
michael@0 15 notice, this list of conditions and the following disclaimer in the
michael@0 16 documentation and/or other materials provided with the distribution.
michael@0 17
michael@0 18 3. The name of the author may not be used to endorse or promote products
michael@0 19 derived from this software without specific prior written permission.
michael@0 20
michael@0 21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
michael@0 23 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
michael@0 24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
michael@0 25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
michael@0 27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
michael@0 30 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 31 POSSIBILITY OF SUCH DAMAGE.
michael@0 32 */
michael@0 33
michael@0 34 /*
michael@0 35 The design goals of this code are:
michael@0 36 - Very fast algorithm
michael@0 37 - SIMD-friendly algorithm
michael@0 38 - Low memory requirement
michael@0 39 - Good *perceptual* quality (and not best SNR)
michael@0 40
michael@0 41 Warning: This resampler is relatively new. Although I think I got rid of
michael@0 42 all the major bugs and I don't expect the API to change anymore, there
michael@0 43 may be something I've missed. So use with caution.
michael@0 44
michael@0 45 This algorithm is based on this original resampling algorithm:
michael@0 46 Smith, Julius O. Digital Audio Resampling Home Page
michael@0 47 Center for Computer Research in Music and Acoustics (CCRMA),
michael@0 48 Stanford University, 2007.
michael@0 49 Web published at http://www-ccrma.stanford.edu/~jos/resample/.
michael@0 50
michael@0 51 There is one main difference, though. This resampler uses cubic
michael@0 52 interpolation instead of linear interpolation in the above paper. This
michael@0 53 makes the table much smaller and makes it possible to compute that table
michael@0 54 on a per-stream basis. In turn, being able to tweak the table for each
michael@0 55 stream makes it possible to both reduce complexity on simple ratios
michael@0 56 (e.g. 2/3), and get rid of the rounding operations in the inner loop.
michael@0 57 The latter both reduces CPU time and makes the algorithm more SIMD-friendly.
michael@0 58 */
michael@0 59
michael@0 60 #ifdef HAVE_CONFIG_H
michael@0 61 # include "config.h"
michael@0 62 #endif
michael@0 63
michael@0 64 #define RESAMPLE_HUGEMEM 1
michael@0 65
michael@0 66 #ifdef OUTSIDE_SPEEX
michael@0 67 #include <stdlib.h>
michael@0 68 static void *speex_alloc (int size) {return calloc(size,1);}
michael@0 69 static void *speex_realloc (void *ptr, int size) {return realloc(ptr, size);}
michael@0 70 static void speex_free (void *ptr) {free(ptr);}
michael@0 71 #include "speex_resampler.h"
michael@0 72 #include "arch.h"
michael@0 73 #else /* OUTSIDE_SPEEX */
michael@0 74
michael@0 75 #include "../include/speex/speex_resampler.h"
michael@0 76 #include "arch.h"
michael@0 77 #include "os_support.h"
michael@0 78 #endif /* OUTSIDE_SPEEX */
michael@0 79
michael@0 80 #include "stack_alloc.h"
michael@0 81 #include <math.h>
michael@0 82
michael@0 83 #ifndef M_PI
michael@0 84 #define M_PI 3.14159263
michael@0 85 #endif
michael@0 86
michael@0 87 #ifdef FIXED_POINT
michael@0 88 #define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x)))
michael@0 89 #else
michael@0 90 #define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x))))
michael@0 91 #endif
michael@0 92
michael@0 93 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
michael@0 94 #define IMIN(a,b) ((a) < (b) ? (a) : (b))
michael@0 95
michael@0 96 #ifndef NULL
michael@0 97 #define NULL 0
michael@0 98 #endif
michael@0 99
michael@0 100 #include "sse_detect.h"
michael@0 101
michael@0 102 /* We compile SSE code on x86 all the time, but we only use it if we find at
michael@0 103 * runtime that the CPU supports it. */
michael@0 104 #if defined(FLOATING_POINT) && defined(__SSE__)
michael@0 105 #if defined(_MSC_VER)
michael@0 106 #define inline __inline
michael@0 107 #endif
michael@0 108 # include "resample_sse.h"
michael@0 109 #ifdef _MSC_VER
michael@0 110 #undef inline
michael@0 111 #endif
michael@0 112 #endif
michael@0 113
michael@0 114 /* Numer of elements to allocate on the stack */
michael@0 115 #ifdef VAR_ARRAYS
michael@0 116 #define FIXED_STACK_ALLOC 8192
michael@0 117 #else
michael@0 118 #define FIXED_STACK_ALLOC 1024
michael@0 119 #endif
michael@0 120
michael@0 121 typedef int (*resampler_basic_func)(SpeexResamplerState *, spx_uint32_t , const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *);
michael@0 122
michael@0 123 struct SpeexResamplerState_ {
michael@0 124 spx_uint32_t in_rate;
michael@0 125 spx_uint32_t out_rate;
michael@0 126 spx_uint32_t num_rate;
michael@0 127 spx_uint32_t den_rate;
michael@0 128
michael@0 129 int quality;
michael@0 130 spx_uint32_t nb_channels;
michael@0 131 spx_uint32_t filt_len;
michael@0 132 spx_uint32_t mem_alloc_size;
michael@0 133 spx_uint32_t buffer_size;
michael@0 134 int int_advance;
michael@0 135 int frac_advance;
michael@0 136 float cutoff;
michael@0 137 spx_uint32_t oversample;
michael@0 138 int initialised;
michael@0 139 int started;
michael@0 140
michael@0 141 /* These are per-channel */
michael@0 142 spx_int32_t *last_sample;
michael@0 143 spx_uint32_t *samp_frac_num;
michael@0 144 spx_uint32_t *magic_samples;
michael@0 145
michael@0 146 spx_word16_t *mem;
michael@0 147 spx_word16_t *sinc_table;
michael@0 148 spx_uint32_t sinc_table_length;
michael@0 149 resampler_basic_func resampler_ptr;
michael@0 150
michael@0 151 int in_stride;
michael@0 152 int out_stride;
michael@0 153 } ;
michael@0 154
michael@0 155 static double kaiser12_table[68] = {
michael@0 156 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076,
michael@0 157 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014,
michael@0 158 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601,
michael@0 159 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014,
michael@0 160 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490,
michael@0 161 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546,
michael@0 162 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178,
michael@0 163 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947,
michael@0 164 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058,
michael@0 165 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438,
michael@0 166 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734,
michael@0 167 0.00001000, 0.00000000};
michael@0 168 /*
michael@0 169 static double kaiser12_table[36] = {
michael@0 170 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741,
michael@0 171 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762,
michael@0 172 0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274,
michael@0 173 0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466,
michael@0 174 0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291,
michael@0 175 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000};
michael@0 176 */
michael@0 177 static double kaiser10_table[36] = {
michael@0 178 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446,
michael@0 179 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347,
michael@0 180 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962,
michael@0 181 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451,
michael@0 182 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739,
michael@0 183 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000};
michael@0 184
michael@0 185 static double kaiser8_table[36] = {
michael@0 186 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200,
michael@0 187 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126,
michael@0 188 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272,
michael@0 189 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758,
michael@0 190 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490,
michael@0 191 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000};
michael@0 192
michael@0 193 static double kaiser6_table[36] = {
michael@0 194 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003,
michael@0 195 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565,
michael@0 196 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561,
michael@0 197 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058,
michael@0 198 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600,
michael@0 199 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000};
michael@0 200
michael@0 201 struct FuncDef {
michael@0 202 double *table;
michael@0 203 int oversample;
michael@0 204 };
michael@0 205
michael@0 206 static struct FuncDef _KAISER12 = {kaiser12_table, 64};
michael@0 207 #define KAISER12 (&_KAISER12)
michael@0 208 /*static struct FuncDef _KAISER12 = {kaiser12_table, 32};
michael@0 209 #define KAISER12 (&_KAISER12)*/
michael@0 210 static struct FuncDef _KAISER10 = {kaiser10_table, 32};
michael@0 211 #define KAISER10 (&_KAISER10)
michael@0 212 static struct FuncDef _KAISER8 = {kaiser8_table, 32};
michael@0 213 #define KAISER8 (&_KAISER8)
michael@0 214 static struct FuncDef _KAISER6 = {kaiser6_table, 32};
michael@0 215 #define KAISER6 (&_KAISER6)
michael@0 216
michael@0 217 struct QualityMapping {
michael@0 218 int base_length;
michael@0 219 int oversample;
michael@0 220 float downsample_bandwidth;
michael@0 221 float upsample_bandwidth;
michael@0 222 struct FuncDef *window_func;
michael@0 223 };
michael@0 224
michael@0 225
michael@0 226 /* This table maps conversion quality to internal parameters. There are two
michael@0 227 reasons that explain why the up-sampling bandwidth is larger than the
michael@0 228 down-sampling bandwidth:
michael@0 229 1) When up-sampling, we can assume that the spectrum is already attenuated
michael@0 230 close to the Nyquist rate (from an A/D or a previous resampling filter)
michael@0 231 2) Any aliasing that occurs very close to the Nyquist rate will be masked
michael@0 232 by the sinusoids/noise just below the Nyquist rate (guaranteed only for
michael@0 233 up-sampling).
michael@0 234 */
michael@0 235 static const struct QualityMapping quality_map[11] = {
michael@0 236 { 8, 4, 0.830f, 0.860f, KAISER6 }, /* Q0 */
michael@0 237 { 16, 4, 0.850f, 0.880f, KAISER6 }, /* Q1 */
michael@0 238 { 32, 4, 0.882f, 0.910f, KAISER6 }, /* Q2 */ /* 82.3% cutoff ( ~60 dB stop) 6 */
michael@0 239 { 48, 8, 0.895f, 0.917f, KAISER8 }, /* Q3 */ /* 84.9% cutoff ( ~80 dB stop) 8 */
michael@0 240 { 64, 8, 0.921f, 0.940f, KAISER8 }, /* Q4 */ /* 88.7% cutoff ( ~80 dB stop) 8 */
michael@0 241 { 80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 */ /* 89.1% cutoff (~100 dB stop) 10 */
michael@0 242 { 96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 */ /* 91.5% cutoff (~100 dB stop) 10 */
michael@0 243 {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 */ /* 93.1% cutoff (~100 dB stop) 10 */
michael@0 244 {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 */ /* 94.5% cutoff (~100 dB stop) 10 */
michael@0 245 {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 */ /* 95.5% cutoff (~100 dB stop) 10 */
michael@0 246 {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 */ /* 96.6% cutoff (~100 dB stop) 10 */
michael@0 247 };
michael@0 248 /*8,24,40,56,80,104,128,160,200,256,320*/
michael@0 249 static double compute_func(float x, struct FuncDef *func)
michael@0 250 {
michael@0 251 float y, frac;
michael@0 252 double interp[4];
michael@0 253 int ind;
michael@0 254 y = x*func->oversample;
michael@0 255 ind = (int)floor(y);
michael@0 256 frac = (y-ind);
michael@0 257 /* CSE with handle the repeated powers */
michael@0 258 interp[3] = -0.1666666667*frac + 0.1666666667*(frac*frac*frac);
michael@0 259 interp[2] = frac + 0.5*(frac*frac) - 0.5*(frac*frac*frac);
michael@0 260 /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac;*/
michael@0 261 interp[0] = -0.3333333333*frac + 0.5*(frac*frac) - 0.1666666667*(frac*frac*frac);
michael@0 262 /* Just to make sure we don't have rounding problems */
michael@0 263 interp[1] = 1.f-interp[3]-interp[2]-interp[0];
michael@0 264
michael@0 265 /*sum = frac*accum[1] + (1-frac)*accum[2];*/
michael@0 266 return interp[0]*func->table[ind] + interp[1]*func->table[ind+1] + interp[2]*func->table[ind+2] + interp[3]*func->table[ind+3];
michael@0 267 }
michael@0 268
michael@0 269 #if 0
michael@0 270 #include <stdio.h>
michael@0 271 int main(int argc, char **argv)
michael@0 272 {
michael@0 273 int i;
michael@0 274 for (i=0;i<256;i++)
michael@0 275 {
michael@0 276 printf ("%f\n", compute_func(i/256., KAISER12));
michael@0 277 }
michael@0 278 return 0;
michael@0 279 }
michael@0 280 #endif
michael@0 281
michael@0 282 #ifdef FIXED_POINT
michael@0 283 /* The slow way of computing a sinc for the table. Should improve that some day */
michael@0 284 static spx_word16_t sinc(float cutoff, float x, int N, struct FuncDef *window_func)
michael@0 285 {
michael@0 286 /*fprintf (stderr, "%f ", x);*/
michael@0 287 float xx = x * cutoff;
michael@0 288 if (fabs(x)<1e-6f)
michael@0 289 return WORD2INT(32768.*cutoff);
michael@0 290 else if (fabs(x) > .5f*N)
michael@0 291 return 0;
michael@0 292 /*FIXME: Can it really be any slower than this? */
michael@0 293 return WORD2INT(32768.*cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func));
michael@0 294 }
michael@0 295 #else
michael@0 296 /* The slow way of computing a sinc for the table. Should improve that some day */
michael@0 297 static spx_word16_t sinc(float cutoff, float x, int N, struct FuncDef *window_func)
michael@0 298 {
michael@0 299 /*fprintf (stderr, "%f ", x);*/
michael@0 300 float xx = x * cutoff;
michael@0 301 if (fabs(x)<1e-6)
michael@0 302 return cutoff;
michael@0 303 else if (fabs(x) > .5*N)
michael@0 304 return 0;
michael@0 305 /*FIXME: Can it really be any slower than this? */
michael@0 306 return cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func);
michael@0 307 }
michael@0 308 #endif
michael@0 309
michael@0 310 #ifdef FIXED_POINT
michael@0 311 static void cubic_coef(spx_word16_t x, spx_word16_t interp[4])
michael@0 312 {
michael@0 313 /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation
michael@0 314 but I know it's MMSE-optimal on a sinc */
michael@0 315 spx_word16_t x2, x3;
michael@0 316 x2 = MULT16_16_P15(x, x);
michael@0 317 x3 = MULT16_16_P15(x, x2);
michael@0 318 interp[0] = PSHR32(MULT16_16(QCONST16(-0.16667f, 15),x) + MULT16_16(QCONST16(0.16667f, 15),x3),15);
michael@0 319 interp[1] = EXTRACT16(EXTEND32(x) + SHR32(SUB32(EXTEND32(x2),EXTEND32(x3)),1));
michael@0 320 interp[3] = PSHR32(MULT16_16(QCONST16(-0.33333f, 15),x) + MULT16_16(QCONST16(.5f,15),x2) - MULT16_16(QCONST16(0.16667f, 15),x3),15);
michael@0 321 /* Just to make sure we don't have rounding problems */
michael@0 322 interp[2] = Q15_ONE-interp[0]-interp[1]-interp[3];
michael@0 323 if (interp[2]<32767)
michael@0 324 interp[2]+=1;
michael@0 325 }
michael@0 326 #else
michael@0 327 static void cubic_coef(spx_word16_t frac, spx_word16_t interp[4])
michael@0 328 {
michael@0 329 /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation
michael@0 330 but I know it's MMSE-optimal on a sinc */
michael@0 331 interp[0] = -0.16667f*frac + 0.16667f*frac*frac*frac;
michael@0 332 interp[1] = frac + 0.5f*frac*frac - 0.5f*frac*frac*frac;
michael@0 333 /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac;*/
michael@0 334 interp[3] = -0.33333f*frac + 0.5f*frac*frac - 0.16667f*frac*frac*frac;
michael@0 335 /* Just to make sure we don't have rounding problems */
michael@0 336 interp[2] = 1.-interp[0]-interp[1]-interp[3];
michael@0 337 }
michael@0 338 #endif
michael@0 339
michael@0 340 static int resampler_basic_direct_single(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
michael@0 341 {
michael@0 342 const int N = st->filt_len;
michael@0 343 int out_sample = 0;
michael@0 344 int last_sample = st->last_sample[channel_index];
michael@0 345 spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
michael@0 346 const spx_word16_t *sinc_table = st->sinc_table;
michael@0 347 const int out_stride = st->out_stride;
michael@0 348 const int int_advance = st->int_advance;
michael@0 349 const int frac_advance = st->frac_advance;
michael@0 350 const spx_uint32_t den_rate = st->den_rate;
michael@0 351 spx_word32_t sum;
michael@0 352
michael@0 353 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
michael@0 354 {
michael@0 355 const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
michael@0 356 const spx_word16_t *iptr = & in[last_sample];
michael@0 357
michael@0 358 #ifdef OVERRIDE_INNER_PRODUCT_SINGLE
michael@0 359 if (moz_has_sse()) {
michael@0 360 sum = inner_product_single(sinct, iptr, N);
michael@0 361 } else {
michael@0 362 #endif
michael@0 363 int j;
michael@0 364 sum = 0;
michael@0 365 for(j=0;j<N;j++) sum += MULT16_16(sinct[j], iptr[j]);
michael@0 366
michael@0 367 /* This code is slower on most DSPs which have only 2 accumulators.
michael@0 368 Plus this this forces truncation to 32 bits and you lose the HW guard bits.
michael@0 369 I think we can trust the compiler and let it vectorize and/or unroll itself.
michael@0 370 spx_word32_t accum[4] = {0,0,0,0};
michael@0 371 for(j=0;j<N;j+=4) {
michael@0 372 accum[0] += MULT16_16(sinct[j], iptr[j]);
michael@0 373 accum[1] += MULT16_16(sinct[j+1], iptr[j+1]);
michael@0 374 accum[2] += MULT16_16(sinct[j+2], iptr[j+2]);
michael@0 375 accum[3] += MULT16_16(sinct[j+3], iptr[j+3]);
michael@0 376 }
michael@0 377 sum = accum[0] + accum[1] + accum[2] + accum[3];
michael@0 378 */
michael@0 379 #ifdef OVERRIDE_INNER_PRODUCT_SINGLE
michael@0 380 }
michael@0 381 #endif
michael@0 382
michael@0 383 out[out_stride * out_sample++] = SATURATE32(PSHR32(sum, 15), 32767);
michael@0 384 last_sample += int_advance;
michael@0 385 samp_frac_num += frac_advance;
michael@0 386 if (samp_frac_num >= den_rate)
michael@0 387 {
michael@0 388 samp_frac_num -= den_rate;
michael@0 389 last_sample++;
michael@0 390 }
michael@0 391 }
michael@0 392
michael@0 393 st->last_sample[channel_index] = last_sample;
michael@0 394 st->samp_frac_num[channel_index] = samp_frac_num;
michael@0 395 return out_sample;
michael@0 396 }
michael@0 397
michael@0 398 #ifdef FIXED_POINT
michael@0 399 #else
michael@0 400 /* This is the same as the previous function, except with a double-precision accumulator */
michael@0 401 static int resampler_basic_direct_double(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
michael@0 402 {
michael@0 403 const int N = st->filt_len;
michael@0 404 int out_sample = 0;
michael@0 405 int last_sample = st->last_sample[channel_index];
michael@0 406 spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
michael@0 407 const spx_word16_t *sinc_table = st->sinc_table;
michael@0 408 const int out_stride = st->out_stride;
michael@0 409 const int int_advance = st->int_advance;
michael@0 410 const int frac_advance = st->frac_advance;
michael@0 411 const spx_uint32_t den_rate = st->den_rate;
michael@0 412 double sum;
michael@0 413
michael@0 414 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
michael@0 415 {
michael@0 416 const spx_word16_t *sinct = & sinc_table[samp_frac_num*N];
michael@0 417 const spx_word16_t *iptr = & in[last_sample];
michael@0 418
michael@0 419 #ifdef OVERRIDE_INNER_PRODUCT_DOUBLE
michael@0 420 if(moz_has_sse2()) {
michael@0 421 sum = inner_product_double(sinct, iptr, N);
michael@0 422 } else {
michael@0 423 #endif
michael@0 424 int j;
michael@0 425 double accum[4] = {0,0,0,0};
michael@0 426
michael@0 427 for(j=0;j<N;j+=4) {
michael@0 428 accum[0] += sinct[j]*iptr[j];
michael@0 429 accum[1] += sinct[j+1]*iptr[j+1];
michael@0 430 accum[2] += sinct[j+2]*iptr[j+2];
michael@0 431 accum[3] += sinct[j+3]*iptr[j+3];
michael@0 432 }
michael@0 433 sum = accum[0] + accum[1] + accum[2] + accum[3];
michael@0 434 #ifdef OVERRIDE_INNER_PRODUCT_DOUBLE
michael@0 435 }
michael@0 436 #endif
michael@0 437
michael@0 438 out[out_stride * out_sample++] = PSHR32(sum, 15);
michael@0 439 last_sample += int_advance;
michael@0 440 samp_frac_num += frac_advance;
michael@0 441 if (samp_frac_num >= den_rate)
michael@0 442 {
michael@0 443 samp_frac_num -= den_rate;
michael@0 444 last_sample++;
michael@0 445 }
michael@0 446 }
michael@0 447
michael@0 448 st->last_sample[channel_index] = last_sample;
michael@0 449 st->samp_frac_num[channel_index] = samp_frac_num;
michael@0 450 return out_sample;
michael@0 451 }
michael@0 452 #endif
michael@0 453
michael@0 454 static int resampler_basic_interpolate_single(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
michael@0 455 {
michael@0 456 const int N = st->filt_len;
michael@0 457 int out_sample = 0;
michael@0 458 int last_sample = st->last_sample[channel_index];
michael@0 459 spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
michael@0 460 const int out_stride = st->out_stride;
michael@0 461 const int int_advance = st->int_advance;
michael@0 462 const int frac_advance = st->frac_advance;
michael@0 463 const spx_uint32_t den_rate = st->den_rate;
michael@0 464 spx_word32_t sum;
michael@0 465
michael@0 466 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
michael@0 467 {
michael@0 468 const spx_word16_t *iptr = & in[last_sample];
michael@0 469
michael@0 470 const int offset = samp_frac_num*st->oversample/st->den_rate;
michael@0 471 #ifdef FIXED_POINT
michael@0 472 const spx_word16_t frac = PDIV32(SHL32((samp_frac_num*st->oversample) % st->den_rate,15),st->den_rate);
michael@0 473 #else
michael@0 474 const spx_word16_t frac = ((float)((samp_frac_num*st->oversample) % st->den_rate))/st->den_rate;
michael@0 475 #endif
michael@0 476 spx_word16_t interp[4];
michael@0 477
michael@0 478
michael@0 479 #ifdef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
michael@0 480 if (moz_has_sse()) {
michael@0 481 cubic_coef(frac, interp);
michael@0 482 sum = interpolate_product_single(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp);
michael@0 483 } else {
michael@0 484 #endif
michael@0 485 int j;
michael@0 486 spx_word32_t accum[4] = {0,0,0,0};
michael@0 487
michael@0 488 for(j=0;j<N;j++) {
michael@0 489 const spx_word16_t curr_in=iptr[j];
michael@0 490 accum[0] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-2]);
michael@0 491 accum[1] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-1]);
michael@0 492 accum[2] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset]);
michael@0 493 accum[3] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset+1]);
michael@0 494 }
michael@0 495 cubic_coef(frac, interp);
michael@0 496 sum = MULT16_32_Q15(interp[0],SHR32(accum[0], 1)) + MULT16_32_Q15(interp[1],SHR32(accum[1], 1)) + MULT16_32_Q15(interp[2],SHR32(accum[2], 1)) + MULT16_32_Q15(interp[3],SHR32(accum[3], 1));
michael@0 497 #ifdef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
michael@0 498 }
michael@0 499 #endif
michael@0 500
michael@0 501 out[out_stride * out_sample++] = SATURATE32(PSHR32(sum, 14), 32767);
michael@0 502 last_sample += int_advance;
michael@0 503 samp_frac_num += frac_advance;
michael@0 504 if (samp_frac_num >= den_rate)
michael@0 505 {
michael@0 506 samp_frac_num -= den_rate;
michael@0 507 last_sample++;
michael@0 508 }
michael@0 509 }
michael@0 510
michael@0 511 st->last_sample[channel_index] = last_sample;
michael@0 512 st->samp_frac_num[channel_index] = samp_frac_num;
michael@0 513 return out_sample;
michael@0 514 }
michael@0 515
michael@0 516 #ifdef FIXED_POINT
michael@0 517 #else
michael@0 518 /* This is the same as the previous function, except with a double-precision accumulator */
michael@0 519 static int resampler_basic_interpolate_double(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
michael@0 520 {
michael@0 521 const int N = st->filt_len;
michael@0 522 int out_sample = 0;
michael@0 523 int last_sample = st->last_sample[channel_index];
michael@0 524 spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
michael@0 525 const int out_stride = st->out_stride;
michael@0 526 const int int_advance = st->int_advance;
michael@0 527 const int frac_advance = st->frac_advance;
michael@0 528 const spx_uint32_t den_rate = st->den_rate;
michael@0 529 spx_word32_t sum;
michael@0 530
michael@0 531 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
michael@0 532 {
michael@0 533 const spx_word16_t *iptr = & in[last_sample];
michael@0 534
michael@0 535 const int offset = samp_frac_num*st->oversample/st->den_rate;
michael@0 536 #ifdef FIXED_POINT
michael@0 537 const spx_word16_t frac = PDIV32(SHL32((samp_frac_num*st->oversample) % st->den_rate,15),st->den_rate);
michael@0 538 #else
michael@0 539 const spx_word16_t frac = ((float)((samp_frac_num*st->oversample) % st->den_rate))/st->den_rate;
michael@0 540 #endif
michael@0 541 spx_word16_t interp[4];
michael@0 542
michael@0 543
michael@0 544 #ifdef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
michael@0 545 if (moz_has_sse2()) {
michael@0 546 cubic_coef(frac, interp);
michael@0 547 sum = interpolate_product_double(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp);
michael@0 548 } else {
michael@0 549 #endif
michael@0 550 int j;
michael@0 551 double accum[4] = {0,0,0,0};
michael@0 552
michael@0 553 for(j=0;j<N;j++) {
michael@0 554 const double curr_in=iptr[j];
michael@0 555 accum[0] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-2]);
michael@0 556 accum[1] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset-1]);
michael@0 557 accum[2] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset]);
michael@0 558 accum[3] += MULT16_16(curr_in,st->sinc_table[4+(j+1)*st->oversample-offset+1]);
michael@0 559 }
michael@0 560
michael@0 561 cubic_coef(frac, interp);
michael@0 562 sum = MULT16_32_Q15(interp[0],accum[0]) + MULT16_32_Q15(interp[1],accum[1]) + MULT16_32_Q15(interp[2],accum[2]) + MULT16_32_Q15(interp[3],accum[3]);
michael@0 563 #ifdef OVERRIDE_INNER_PRODUCT_DOUBLE
michael@0 564 }
michael@0 565 #endif
michael@0 566 out[out_stride * out_sample++] = PSHR32(sum,15);
michael@0 567 last_sample += int_advance;
michael@0 568 samp_frac_num += frac_advance;
michael@0 569 if (samp_frac_num >= den_rate)
michael@0 570 {
michael@0 571 samp_frac_num -= den_rate;
michael@0 572 last_sample++;
michael@0 573 }
michael@0 574 }
michael@0 575
michael@0 576 st->last_sample[channel_index] = last_sample;
michael@0 577 st->samp_frac_num[channel_index] = samp_frac_num;
michael@0 578 return out_sample;
michael@0 579 }
michael@0 580 #endif
michael@0 581
michael@0 582 /* This resampler is used to produce zero output in situations where memory
michael@0 583 for the filter could not be allocated. The expected numbers of input and
michael@0 584 output samples are still processed so that callers failing to check error
michael@0 585 codes are not surprised, possibly getting into infinite loops. */
michael@0 586 static int resampler_basic_zero(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_word16_t *in, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
michael@0 587 {
michael@0 588 int out_sample = 0;
michael@0 589 int last_sample = st->last_sample[channel_index];
michael@0 590 spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index];
michael@0 591 const int out_stride = st->out_stride;
michael@0 592 const int int_advance = st->int_advance;
michael@0 593 const int frac_advance = st->frac_advance;
michael@0 594 const spx_uint32_t den_rate = st->den_rate;
michael@0 595
michael@0 596 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len))
michael@0 597 {
michael@0 598 out[out_stride * out_sample++] = 0;
michael@0 599 last_sample += int_advance;
michael@0 600 samp_frac_num += frac_advance;
michael@0 601 if (samp_frac_num >= den_rate)
michael@0 602 {
michael@0 603 samp_frac_num -= den_rate;
michael@0 604 last_sample++;
michael@0 605 }
michael@0 606 }
michael@0 607
michael@0 608 st->last_sample[channel_index] = last_sample;
michael@0 609 st->samp_frac_num[channel_index] = samp_frac_num;
michael@0 610 return out_sample;
michael@0 611 }
michael@0 612
michael@0 613 static int update_filter(SpeexResamplerState *st)
michael@0 614 {
michael@0 615 spx_uint32_t old_length = st->filt_len;
michael@0 616 spx_uint32_t old_alloc_size = st->mem_alloc_size;
michael@0 617 int use_direct;
michael@0 618 spx_uint32_t min_sinc_table_length;
michael@0 619 spx_uint32_t min_alloc_size;
michael@0 620
michael@0 621 st->int_advance = st->num_rate/st->den_rate;
michael@0 622 st->frac_advance = st->num_rate%st->den_rate;
michael@0 623 st->oversample = quality_map[st->quality].oversample;
michael@0 624 st->filt_len = quality_map[st->quality].base_length;
michael@0 625
michael@0 626 if (st->num_rate > st->den_rate)
michael@0 627 {
michael@0 628 /* down-sampling */
michael@0 629 st->cutoff = quality_map[st->quality].downsample_bandwidth * st->den_rate / st->num_rate;
michael@0 630 /* FIXME: divide the numerator and denominator by a certain amount if they're too large */
michael@0 631 st->filt_len = st->filt_len*st->num_rate / st->den_rate;
michael@0 632 /* Round up to make sure we have a multiple of 8 */
michael@0 633 st->filt_len = ((st->filt_len-1)&(~0x7))+8;
michael@0 634 if (2*st->den_rate < st->num_rate)
michael@0 635 st->oversample >>= 1;
michael@0 636 if (4*st->den_rate < st->num_rate)
michael@0 637 st->oversample >>= 1;
michael@0 638 if (8*st->den_rate < st->num_rate)
michael@0 639 st->oversample >>= 1;
michael@0 640 if (16*st->den_rate < st->num_rate)
michael@0 641 st->oversample >>= 1;
michael@0 642 if (st->oversample < 1)
michael@0 643 st->oversample = 1;
michael@0 644 } else {
michael@0 645 /* up-sampling */
michael@0 646 st->cutoff = quality_map[st->quality].upsample_bandwidth;
michael@0 647 }
michael@0 648
michael@0 649 #ifdef RESAMPLE_HUGEMEM
michael@0 650 use_direct = st->den_rate <= 16*(st->oversample+8);
michael@0 651 #else
michael@0 652 /* Choose the resampling type that requires the least amount of memory */
michael@0 653 use_direct = st->filt_len*st->den_rate <= st->filt_len*st->oversample+8;
michael@0 654 #endif
michael@0 655 if (use_direct)
michael@0 656 {
michael@0 657 min_sinc_table_length = st->filt_len*st->den_rate;
michael@0 658 } else {
michael@0 659 min_sinc_table_length = st->filt_len*st->oversample+8;
michael@0 660 }
michael@0 661 if (st->sinc_table_length < min_sinc_table_length)
michael@0 662 {
michael@0 663 spx_word16_t *sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,min_sinc_table_length*sizeof(spx_word16_t));
michael@0 664 if (!sinc_table)
michael@0 665 goto fail;
michael@0 666
michael@0 667 st->sinc_table = sinc_table;
michael@0 668 st->sinc_table_length = min_sinc_table_length;
michael@0 669 }
michael@0 670 if (use_direct)
michael@0 671 {
michael@0 672 spx_uint32_t i;
michael@0 673 for (i=0;i<st->den_rate;i++)
michael@0 674 {
michael@0 675 spx_int32_t j;
michael@0 676 for (j=0;j<st->filt_len;j++)
michael@0 677 {
michael@0 678 st->sinc_table[i*st->filt_len+j] = sinc(st->cutoff,((j-(spx_int32_t)st->filt_len/2+1)-((float)i)/st->den_rate), st->filt_len, quality_map[st->quality].window_func);
michael@0 679 }
michael@0 680 }
michael@0 681 #ifdef FIXED_POINT
michael@0 682 st->resampler_ptr = resampler_basic_direct_single;
michael@0 683 #else
michael@0 684 if (st->quality>8)
michael@0 685 st->resampler_ptr = resampler_basic_direct_double;
michael@0 686 else
michael@0 687 st->resampler_ptr = resampler_basic_direct_single;
michael@0 688 #endif
michael@0 689 /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff);*/
michael@0 690 } else {
michael@0 691 spx_int32_t i;
michael@0 692 for (i=-4;i<(spx_int32_t)(st->oversample*st->filt_len+4);i++)
michael@0 693 st->sinc_table[i+4] = sinc(st->cutoff,(i/(float)st->oversample - st->filt_len/2), st->filt_len, quality_map[st->quality].window_func);
michael@0 694 #ifdef FIXED_POINT
michael@0 695 st->resampler_ptr = resampler_basic_interpolate_single;
michael@0 696 #else
michael@0 697 if (st->quality>8)
michael@0 698 st->resampler_ptr = resampler_basic_interpolate_double;
michael@0 699 else
michael@0 700 st->resampler_ptr = resampler_basic_interpolate_single;
michael@0 701 #endif
michael@0 702 /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/
michael@0 703 }
michael@0 704
michael@0 705
michael@0 706 /* Here's the place where we update the filter memory to take into account
michael@0 707 the change in filter length. It's probably the messiest part of the code
michael@0 708 due to handling of lots of corner cases. */
michael@0 709 min_alloc_size = st->filt_len-1 + st->buffer_size;
michael@0 710 if (min_alloc_size > st->mem_alloc_size)
michael@0 711 {
michael@0 712 spx_word16_t *mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*min_alloc_size * sizeof(spx_word16_t));
michael@0 713 if (!mem)
michael@0 714 goto fail;
michael@0 715
michael@0 716 st->mem = mem;
michael@0 717 st->mem_alloc_size = min_alloc_size;
michael@0 718 }
michael@0 719 if (!st->started)
michael@0 720 {
michael@0 721 spx_uint32_t i;
michael@0 722 for (i=0;i<st->nb_channels*st->mem_alloc_size;i++)
michael@0 723 st->mem[i] = 0;
michael@0 724 /*speex_warning("reinit filter");*/
michael@0 725 } else if (st->filt_len > old_length)
michael@0 726 {
michael@0 727 spx_uint32_t i;
michael@0 728 /* Increase the filter length */
michael@0 729 /*speex_warning("increase filter size");*/
michael@0 730 for (i=st->nb_channels;i--;)
michael@0 731 {
michael@0 732 spx_uint32_t j;
michael@0 733 spx_uint32_t olen = old_length;
michael@0 734 /*if (st->magic_samples[i])*/
michael@0 735 {
michael@0 736 /* Try and remove the magic samples as if nothing had happened */
michael@0 737
michael@0 738 /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */
michael@0 739 olen = old_length + 2*st->magic_samples[i];
michael@0 740 for (j=old_length-1+st->magic_samples[i];j--;)
michael@0 741 st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]] = st->mem[i*old_alloc_size+j];
michael@0 742 for (j=0;j<st->magic_samples[i];j++)
michael@0 743 st->mem[i*st->mem_alloc_size+j] = 0;
michael@0 744 st->magic_samples[i] = 0;
michael@0 745 }
michael@0 746 if (st->filt_len > olen)
michael@0 747 {
michael@0 748 /* If the new filter length is still bigger than the "augmented" length */
michael@0 749 /* Copy data going backward */
michael@0 750 for (j=0;j<olen-1;j++)
michael@0 751 st->mem[i*st->mem_alloc_size+(st->filt_len-2-j)] = st->mem[i*st->mem_alloc_size+(olen-2-j)];
michael@0 752 /* Then put zeros for lack of anything better */
michael@0 753 for (;j<st->filt_len-1;j++)
michael@0 754 st->mem[i*st->mem_alloc_size+(st->filt_len-2-j)] = 0;
michael@0 755 /* Adjust last_sample */
michael@0 756 st->last_sample[i] += (st->filt_len - olen)/2;
michael@0 757 } else {
michael@0 758 /* Put back some of the magic! */
michael@0 759 st->magic_samples[i] = (olen - st->filt_len)/2;
michael@0 760 for (j=0;j<st->filt_len-1+st->magic_samples[i];j++)
michael@0 761 st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]];
michael@0 762 }
michael@0 763 }
michael@0 764 } else if (st->filt_len < old_length)
michael@0 765 {
michael@0 766 spx_uint32_t i;
michael@0 767 /* Reduce filter length, this a bit tricky. We need to store some of the memory as "magic"
michael@0 768 samples so they can be used directly as input the next time(s) */
michael@0 769 for (i=0;i<st->nb_channels;i++)
michael@0 770 {
michael@0 771 spx_uint32_t j;
michael@0 772 spx_uint32_t old_magic = st->magic_samples[i];
michael@0 773 st->magic_samples[i] = (old_length - st->filt_len)/2;
michael@0 774 /* We must copy some of the memory that's no longer used */
michael@0 775 /* Copy data going backward */
michael@0 776 for (j=0;j<st->filt_len-1+st->magic_samples[i]+old_magic;j++)
michael@0 777 st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]];
michael@0 778 st->magic_samples[i] += old_magic;
michael@0 779 }
michael@0 780 }
michael@0 781 return RESAMPLER_ERR_SUCCESS;
michael@0 782
michael@0 783 fail:
michael@0 784 st->resampler_ptr = resampler_basic_zero;
michael@0 785 /* st->mem may still contain consumed input samples for the filter.
michael@0 786 Restore filt_len so that filt_len - 1 still points to the position after
michael@0 787 the last of these samples. */
michael@0 788 st->filt_len = old_length;
michael@0 789 return RESAMPLER_ERR_ALLOC_FAILED;
michael@0 790 }
michael@0 791
michael@0 792 SPX_RESAMPLE_EXPORT SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
michael@0 793 {
michael@0 794 return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality, err);
michael@0 795 }
michael@0 796
michael@0 797 SPX_RESAMPLE_EXPORT SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
michael@0 798 {
michael@0 799 spx_uint32_t i;
michael@0 800 SpeexResamplerState *st;
michael@0 801 int filter_err;
michael@0 802
michael@0 803 if (quality > 10 || quality < 0)
michael@0 804 {
michael@0 805 if (err)
michael@0 806 *err = RESAMPLER_ERR_INVALID_ARG;
michael@0 807 return NULL;
michael@0 808 }
michael@0 809 st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState));
michael@0 810 st->initialised = 0;
michael@0 811 st->started = 0;
michael@0 812 st->in_rate = 0;
michael@0 813 st->out_rate = 0;
michael@0 814 st->num_rate = 0;
michael@0 815 st->den_rate = 0;
michael@0 816 st->quality = -1;
michael@0 817 st->sinc_table_length = 0;
michael@0 818 st->mem_alloc_size = 0;
michael@0 819 st->filt_len = 0;
michael@0 820 st->mem = 0;
michael@0 821 st->resampler_ptr = 0;
michael@0 822
michael@0 823 st->cutoff = 1.f;
michael@0 824 st->nb_channels = nb_channels;
michael@0 825 st->in_stride = 1;
michael@0 826 st->out_stride = 1;
michael@0 827
michael@0 828 #ifdef FIXED_POINT
michael@0 829 st->buffer_size = 160;
michael@0 830 #else
michael@0 831 st->buffer_size = 160;
michael@0 832 #endif
michael@0 833
michael@0 834 /* Per channel data */
michael@0 835 st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t));
michael@0 836 st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
michael@0 837 st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t));
michael@0 838 for (i=0;i<nb_channels;i++)
michael@0 839 {
michael@0 840 st->last_sample[i] = 0;
michael@0 841 st->magic_samples[i] = 0;
michael@0 842 st->samp_frac_num[i] = 0;
michael@0 843 }
michael@0 844
michael@0 845 speex_resampler_set_quality(st, quality);
michael@0 846 speex_resampler_set_rate_frac(st, ratio_num, ratio_den, in_rate, out_rate);
michael@0 847
michael@0 848 filter_err = update_filter(st);
michael@0 849 if (filter_err == RESAMPLER_ERR_SUCCESS)
michael@0 850 {
michael@0 851 st->initialised = 1;
michael@0 852 } else {
michael@0 853 speex_resampler_destroy(st);
michael@0 854 st = NULL;
michael@0 855 }
michael@0 856 if (err)
michael@0 857 *err = filter_err;
michael@0 858
michael@0 859 return st;
michael@0 860 }
michael@0 861
michael@0 862 SPX_RESAMPLE_EXPORT void speex_resampler_destroy(SpeexResamplerState *st)
michael@0 863 {
michael@0 864 speex_free(st->mem);
michael@0 865 speex_free(st->sinc_table);
michael@0 866 speex_free(st->last_sample);
michael@0 867 speex_free(st->magic_samples);
michael@0 868 speex_free(st->samp_frac_num);
michael@0 869 speex_free(st);
michael@0 870 }
michael@0 871
michael@0 872 static int speex_resampler_process_native(SpeexResamplerState *st, spx_uint32_t channel_index, spx_uint32_t *in_len, spx_word16_t *out, spx_uint32_t *out_len)
michael@0 873 {
michael@0 874 int j=0;
michael@0 875 const int N = st->filt_len;
michael@0 876 int out_sample = 0;
michael@0 877 spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size;
michael@0 878 spx_uint32_t ilen;
michael@0 879
michael@0 880 st->started = 1;
michael@0 881
michael@0 882 /* Call the right resampler through the function ptr */
michael@0 883 out_sample = st->resampler_ptr(st, channel_index, mem, in_len, out, out_len);
michael@0 884
michael@0 885 if (st->last_sample[channel_index] < (spx_int32_t)*in_len)
michael@0 886 *in_len = st->last_sample[channel_index];
michael@0 887 *out_len = out_sample;
michael@0 888 st->last_sample[channel_index] -= *in_len;
michael@0 889
michael@0 890 ilen = *in_len;
michael@0 891
michael@0 892 for(j=0;j<N-1;++j)
michael@0 893 mem[j] = mem[j+ilen];
michael@0 894
michael@0 895 return RESAMPLER_ERR_SUCCESS;
michael@0 896 }
michael@0 897
michael@0 898 static int speex_resampler_magic(SpeexResamplerState *st, spx_uint32_t channel_index, spx_word16_t **out, spx_uint32_t out_len) {
michael@0 899 spx_uint32_t tmp_in_len = st->magic_samples[channel_index];
michael@0 900 spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size;
michael@0 901 const int N = st->filt_len;
michael@0 902
michael@0 903 speex_resampler_process_native(st, channel_index, &tmp_in_len, *out, &out_len);
michael@0 904
michael@0 905 st->magic_samples[channel_index] -= tmp_in_len;
michael@0 906
michael@0 907 /* If we couldn't process all "magic" input samples, save the rest for next time */
michael@0 908 if (st->magic_samples[channel_index])
michael@0 909 {
michael@0 910 spx_uint32_t i;
michael@0 911 for (i=0;i<st->magic_samples[channel_index];i++)
michael@0 912 mem[N-1+i]=mem[N-1+i+tmp_in_len];
michael@0 913 }
michael@0 914 *out += out_len*st->out_stride;
michael@0 915 return out_len;
michael@0 916 }
michael@0 917
michael@0 918 #ifdef FIXED_POINT
michael@0 919 SPX_RESAMPLE_EXPORT int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
michael@0 920 #else
michael@0 921 SPX_RESAMPLE_EXPORT int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
michael@0 922 #endif
michael@0 923 {
michael@0 924 int j;
michael@0 925 spx_uint32_t ilen = *in_len;
michael@0 926 spx_uint32_t olen = *out_len;
michael@0 927 spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size;
michael@0 928 const int filt_offs = st->filt_len - 1;
michael@0 929 const spx_uint32_t xlen = st->mem_alloc_size - filt_offs;
michael@0 930 const int istride = st->in_stride;
michael@0 931
michael@0 932 if (st->magic_samples[channel_index])
michael@0 933 olen -= speex_resampler_magic(st, channel_index, &out, olen);
michael@0 934 if (! st->magic_samples[channel_index]) {
michael@0 935 while (ilen && olen) {
michael@0 936 spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
michael@0 937 spx_uint32_t ochunk = olen;
michael@0 938
michael@0 939 if (in) {
michael@0 940 for(j=0;j<ichunk;++j)
michael@0 941 x[j+filt_offs]=in[j*istride];
michael@0 942 } else {
michael@0 943 for(j=0;j<ichunk;++j)
michael@0 944 x[j+filt_offs]=0;
michael@0 945 }
michael@0 946 speex_resampler_process_native(st, channel_index, &ichunk, out, &ochunk);
michael@0 947 ilen -= ichunk;
michael@0 948 olen -= ochunk;
michael@0 949 out += ochunk * st->out_stride;
michael@0 950 if (in)
michael@0 951 in += ichunk * istride;
michael@0 952 }
michael@0 953 }
michael@0 954 *in_len -= ilen;
michael@0 955 *out_len -= olen;
michael@0 956 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
michael@0 957 }
michael@0 958
michael@0 959 #ifdef FIXED_POINT
michael@0 960 SPX_RESAMPLE_EXPORT int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
michael@0 961 #else
michael@0 962 SPX_RESAMPLE_EXPORT int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
michael@0 963 #endif
michael@0 964 {
michael@0 965 int j;
michael@0 966 const int istride_save = st->in_stride;
michael@0 967 const int ostride_save = st->out_stride;
michael@0 968 spx_uint32_t ilen = *in_len;
michael@0 969 spx_uint32_t olen = *out_len;
michael@0 970 spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size;
michael@0 971 const spx_uint32_t xlen = st->mem_alloc_size - (st->filt_len - 1);
michael@0 972 #ifdef VAR_ARRAYS
michael@0 973 const unsigned int ylen = (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC;
michael@0 974 VARDECL(spx_word16_t *ystack);
michael@0 975 ALLOC(ystack, ylen, spx_word16_t);
michael@0 976 #else
michael@0 977 const unsigned int ylen = FIXED_STACK_ALLOC;
michael@0 978 spx_word16_t ystack[FIXED_STACK_ALLOC];
michael@0 979 #endif
michael@0 980
michael@0 981 st->out_stride = 1;
michael@0 982
michael@0 983 while (ilen && olen) {
michael@0 984 spx_word16_t *y = ystack;
michael@0 985 spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen;
michael@0 986 spx_uint32_t ochunk = (olen > ylen) ? ylen : olen;
michael@0 987 spx_uint32_t omagic = 0;
michael@0 988
michael@0 989 if (st->magic_samples[channel_index]) {
michael@0 990 omagic = speex_resampler_magic(st, channel_index, &y, ochunk);
michael@0 991 ochunk -= omagic;
michael@0 992 olen -= omagic;
michael@0 993 }
michael@0 994 if (! st->magic_samples[channel_index]) {
michael@0 995 if (in) {
michael@0 996 for(j=0;j<ichunk;++j)
michael@0 997 #ifdef FIXED_POINT
michael@0 998 x[j+st->filt_len-1]=WORD2INT(in[j*istride_save]);
michael@0 999 #else
michael@0 1000 x[j+st->filt_len-1]=in[j*istride_save];
michael@0 1001 #endif
michael@0 1002 } else {
michael@0 1003 for(j=0;j<ichunk;++j)
michael@0 1004 x[j+st->filt_len-1]=0;
michael@0 1005 }
michael@0 1006
michael@0 1007 speex_resampler_process_native(st, channel_index, &ichunk, y, &ochunk);
michael@0 1008 } else {
michael@0 1009 ichunk = 0;
michael@0 1010 ochunk = 0;
michael@0 1011 }
michael@0 1012
michael@0 1013 for (j=0;j<ochunk+omagic;++j)
michael@0 1014 #ifdef FIXED_POINT
michael@0 1015 out[j*ostride_save] = ystack[j];
michael@0 1016 #else
michael@0 1017 out[j*ostride_save] = WORD2INT(ystack[j]);
michael@0 1018 #endif
michael@0 1019
michael@0 1020 ilen -= ichunk;
michael@0 1021 olen -= ochunk;
michael@0 1022 out += (ochunk+omagic) * ostride_save;
michael@0 1023 if (in)
michael@0 1024 in += ichunk * istride_save;
michael@0 1025 }
michael@0 1026 st->out_stride = ostride_save;
michael@0 1027 *in_len -= ilen;
michael@0 1028 *out_len -= olen;
michael@0 1029
michael@0 1030 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
michael@0 1031 }
michael@0 1032
michael@0 1033 SPX_RESAMPLE_EXPORT int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len)
michael@0 1034 {
michael@0 1035 spx_uint32_t i;
michael@0 1036 int istride_save, ostride_save;
michael@0 1037 spx_uint32_t bak_out_len = *out_len;
michael@0 1038 spx_uint32_t bak_in_len = *in_len;
michael@0 1039 istride_save = st->in_stride;
michael@0 1040 ostride_save = st->out_stride;
michael@0 1041 st->in_stride = st->out_stride = st->nb_channels;
michael@0 1042 for (i=0;i<st->nb_channels;i++)
michael@0 1043 {
michael@0 1044 *out_len = bak_out_len;
michael@0 1045 *in_len = bak_in_len;
michael@0 1046 if (in != NULL)
michael@0 1047 speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len);
michael@0 1048 else
michael@0 1049 speex_resampler_process_float(st, i, NULL, in_len, out+i, out_len);
michael@0 1050 }
michael@0 1051 st->in_stride = istride_save;
michael@0 1052 st->out_stride = ostride_save;
michael@0 1053 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
michael@0 1054 }
michael@0 1055
michael@0 1056 SPX_RESAMPLE_EXPORT int speex_resampler_process_interleaved_int(SpeexResamplerState *st, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
michael@0 1057 {
michael@0 1058 spx_uint32_t i;
michael@0 1059 int istride_save, ostride_save;
michael@0 1060 spx_uint32_t bak_out_len = *out_len;
michael@0 1061 spx_uint32_t bak_in_len = *in_len;
michael@0 1062 istride_save = st->in_stride;
michael@0 1063 ostride_save = st->out_stride;
michael@0 1064 st->in_stride = st->out_stride = st->nb_channels;
michael@0 1065 for (i=0;i<st->nb_channels;i++)
michael@0 1066 {
michael@0 1067 *out_len = bak_out_len;
michael@0 1068 *in_len = bak_in_len;
michael@0 1069 if (in != NULL)
michael@0 1070 speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len);
michael@0 1071 else
michael@0 1072 speex_resampler_process_int(st, i, NULL, in_len, out+i, out_len);
michael@0 1073 }
michael@0 1074 st->in_stride = istride_save;
michael@0 1075 st->out_stride = ostride_save;
michael@0 1076 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS;
michael@0 1077 }
michael@0 1078
michael@0 1079 SPX_RESAMPLE_EXPORT int speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate)
michael@0 1080 {
michael@0 1081 return speex_resampler_set_rate_frac(st, in_rate, out_rate, in_rate, out_rate);
michael@0 1082 }
michael@0 1083
michael@0 1084 SPX_RESAMPLE_EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, spx_uint32_t *out_rate)
michael@0 1085 {
michael@0 1086 *in_rate = st->in_rate;
michael@0 1087 *out_rate = st->out_rate;
michael@0 1088 }
michael@0 1089
michael@0 1090 SPX_RESAMPLE_EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate)
michael@0 1091 {
michael@0 1092 spx_uint32_t fact;
michael@0 1093 spx_uint32_t old_den;
michael@0 1094 spx_uint32_t i;
michael@0 1095 if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den)
michael@0 1096 return RESAMPLER_ERR_SUCCESS;
michael@0 1097
michael@0 1098 old_den = st->den_rate;
michael@0 1099 st->in_rate = in_rate;
michael@0 1100 st->out_rate = out_rate;
michael@0 1101 st->num_rate = ratio_num;
michael@0 1102 st->den_rate = ratio_den;
michael@0 1103 /* FIXME: This is terribly inefficient, but who cares (at least for now)? */
michael@0 1104 for (fact=2;fact<=IMIN(st->num_rate, st->den_rate);fact++)
michael@0 1105 {
michael@0 1106 while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0))
michael@0 1107 {
michael@0 1108 st->num_rate /= fact;
michael@0 1109 st->den_rate /= fact;
michael@0 1110 }
michael@0 1111 }
michael@0 1112
michael@0 1113 if (old_den > 0)
michael@0 1114 {
michael@0 1115 for (i=0;i<st->nb_channels;i++)
michael@0 1116 {
michael@0 1117 st->samp_frac_num[i]=st->samp_frac_num[i]*st->den_rate/old_den;
michael@0 1118 /* Safety net */
michael@0 1119 if (st->samp_frac_num[i] >= st->den_rate)
michael@0 1120 st->samp_frac_num[i] = st->den_rate-1;
michael@0 1121 }
michael@0 1122 }
michael@0 1123
michael@0 1124 if (st->initialised)
michael@0 1125 return update_filter(st);
michael@0 1126 return RESAMPLER_ERR_SUCCESS;
michael@0 1127 }
michael@0 1128
michael@0 1129 SPX_RESAMPLE_EXPORT void speex_resampler_get_ratio(SpeexResamplerState *st, spx_uint32_t *ratio_num, spx_uint32_t *ratio_den)
michael@0 1130 {
michael@0 1131 *ratio_num = st->num_rate;
michael@0 1132 *ratio_den = st->den_rate;
michael@0 1133 }
michael@0 1134
michael@0 1135 SPX_RESAMPLE_EXPORT int speex_resampler_set_quality(SpeexResamplerState *st, int quality)
michael@0 1136 {
michael@0 1137 if (quality > 10 || quality < 0)
michael@0 1138 return RESAMPLER_ERR_INVALID_ARG;
michael@0 1139 if (st->quality == quality)
michael@0 1140 return RESAMPLER_ERR_SUCCESS;
michael@0 1141 st->quality = quality;
michael@0 1142 if (st->initialised)
michael@0 1143 return update_filter(st);
michael@0 1144 return RESAMPLER_ERR_SUCCESS;
michael@0 1145 }
michael@0 1146
michael@0 1147 SPX_RESAMPLE_EXPORT void speex_resampler_get_quality(SpeexResamplerState *st, int *quality)
michael@0 1148 {
michael@0 1149 *quality = st->quality;
michael@0 1150 }
michael@0 1151
michael@0 1152 SPX_RESAMPLE_EXPORT void speex_resampler_set_input_stride(SpeexResamplerState *st, spx_uint32_t stride)
michael@0 1153 {
michael@0 1154 st->in_stride = stride;
michael@0 1155 }
michael@0 1156
michael@0 1157 SPX_RESAMPLE_EXPORT void speex_resampler_get_input_stride(SpeexResamplerState *st, spx_uint32_t *stride)
michael@0 1158 {
michael@0 1159 *stride = st->in_stride;
michael@0 1160 }
michael@0 1161
michael@0 1162 SPX_RESAMPLE_EXPORT void speex_resampler_set_output_stride(SpeexResamplerState *st, spx_uint32_t stride)
michael@0 1163 {
michael@0 1164 st->out_stride = stride;
michael@0 1165 }
michael@0 1166
michael@0 1167 SPX_RESAMPLE_EXPORT void speex_resampler_get_output_stride(SpeexResamplerState *st, spx_uint32_t *stride)
michael@0 1168 {
michael@0 1169 *stride = st->out_stride;
michael@0 1170 }
michael@0 1171
michael@0 1172 SPX_RESAMPLE_EXPORT int speex_resampler_get_input_latency(SpeexResamplerState *st)
michael@0 1173 {
michael@0 1174 return st->filt_len / 2;
michael@0 1175 }
michael@0 1176
michael@0 1177 SPX_RESAMPLE_EXPORT int speex_resampler_get_output_latency(SpeexResamplerState *st)
michael@0 1178 {
michael@0 1179 return ((st->filt_len / 2) * st->den_rate + (st->num_rate >> 1)) / st->num_rate;
michael@0 1180 }
michael@0 1181
michael@0 1182 SPX_RESAMPLE_EXPORT int speex_resampler_skip_zeros(SpeexResamplerState *st)
michael@0 1183 {
michael@0 1184 spx_uint32_t i;
michael@0 1185 for (i=0;i<st->nb_channels;i++)
michael@0 1186 st->last_sample[i] = st->filt_len/2;
michael@0 1187 return RESAMPLER_ERR_SUCCESS;
michael@0 1188 }
michael@0 1189
michael@0 1190 SPX_RESAMPLE_EXPORT int speex_resampler_set_skip_frac_num(SpeexResamplerState *st, spx_uint32_t skip_frac_num)
michael@0 1191 {
michael@0 1192 spx_uint32_t i;
michael@0 1193 spx_uint32_t last_sample = skip_frac_num / st->den_rate;
michael@0 1194 spx_uint32_t samp_frac_num = skip_frac_num % st->den_rate;
michael@0 1195 for (i=0;i<st->nb_channels;i++) {
michael@0 1196 st->last_sample[i] = last_sample;
michael@0 1197 st->samp_frac_num[i] = samp_frac_num;
michael@0 1198 }
michael@0 1199 return RESAMPLER_ERR_SUCCESS;
michael@0 1200 }
michael@0 1201
michael@0 1202 SPX_RESAMPLE_EXPORT int speex_resampler_reset_mem(SpeexResamplerState *st)
michael@0 1203 {
michael@0 1204 spx_uint32_t i;
michael@0 1205 for (i=0;i<st->nb_channels;i++)
michael@0 1206 {
michael@0 1207 st->last_sample[i] = 0;
michael@0 1208 st->magic_samples[i] = 0;
michael@0 1209 st->samp_frac_num[i] = 0;
michael@0 1210 }
michael@0 1211 for (i=0;i<st->nb_channels*(st->filt_len-1);i++)
michael@0 1212 st->mem[i] = 0;
michael@0 1213 return RESAMPLER_ERR_SUCCESS;
michael@0 1214 }
michael@0 1215
michael@0 1216 SPX_RESAMPLE_EXPORT const char *speex_resampler_strerror(int err)
michael@0 1217 {
michael@0 1218 switch (err)
michael@0 1219 {
michael@0 1220 case RESAMPLER_ERR_SUCCESS:
michael@0 1221 return "Success.";
michael@0 1222 case RESAMPLER_ERR_ALLOC_FAILED:
michael@0 1223 return "Memory allocation failed.";
michael@0 1224 case RESAMPLER_ERR_BAD_STATE:
michael@0 1225 return "Bad resampler state.";
michael@0 1226 case RESAMPLER_ERR_INVALID_ARG:
michael@0 1227 return "Invalid argument.";
michael@0 1228 case RESAMPLER_ERR_PTR_OVERLAP:
michael@0 1229 return "Input and output buffers overlap.";
michael@0 1230 default:
michael@0 1231 return "Unknown error. Bad error code or strange version mismatch.";
michael@0 1232 }
michael@0 1233 }

mercurial