Changeset 6129 for pjproject/trunk/third_party/speex/libspeex/resample.c
- Timestamp:
- Jan 9, 2020 9:05:50 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/third_party/speex/libspeex/resample.c
r2002 r6129 1 1 /* Copyright (C) 2007-2008 Jean-Marc Valin 2 2 Copyright (C) 2008 Thorvald Natvig 3 3 4 4 File: resample.c 5 5 Arbitrary resampling code … … 39 39 - Good *perceptual* quality (and not best SNR) 40 40 41 Warning: This resampler is relatively new. Although I think I got rid of 41 Warning: This resampler is relatively new. Although I think I got rid of 42 42 all the major bugs and I don't expect the API to change anymore, there 43 43 may be something I've missed. So use with caution. … … 45 45 This algorithm is based on this original resampling algorithm: 46 46 Smith, Julius O. Digital Audio Resampling Home Page 47 Center for Computer Research in Music and Acoustics (CCRMA), 47 Center for Computer Research in Music and Acoustics (CCRMA), 48 48 Stanford University, 2007. 49 Web published at http ://www-ccrma.stanford.edu/~jos/resample/.50 51 There is one main difference, though. This resampler uses cubic 49 Web published at https://ccrma.stanford.edu/~jos/resample/. 50 51 There is one main difference, though. This resampler uses cubic 52 52 interpolation instead of linear interpolation in the above paper. This 53 53 makes the table much smaller and makes it possible to compute that table 54 on a per-stream basis. In turn, being able to tweak the table for each 55 stream makes it possible to both reduce complexity on simple ratios 56 (e.g. 2/3), and get rid of the rounding operations in the inner loop. 54 on a per-stream basis. In turn, being able to tweak the table for each 55 stream makes it possible to both reduce complexity on simple ratios 56 (e.g. 2/3), and get rid of the rounding operations in the inner loop. 57 57 The latter both reduces CPU time and makes the algorithm more SIMD-friendly. 58 58 */ … … 64 64 #ifdef OUTSIDE_SPEEX 65 65 #include <stdlib.h> 66 static void *speex_alloc (int size) {return calloc(size,1);} 67 static void *speex_realloc (void *ptr, int size) {return realloc(ptr, size);} 68 static void speex_free (void *ptr) {free(ptr);} 66 static void *speex_alloc(int size) {return calloc(size,1);} 67 static void *speex_realloc(void *ptr, int size) {return realloc(ptr, size);} 68 static void speex_free(void *ptr) {free(ptr);} 69 #ifndef EXPORT 70 #define EXPORT 71 #endif 69 72 #include "speex_resampler.h" 70 73 #include "arch.h" 71 74 #else /* OUTSIDE_SPEEX */ 72 75 73 76 #include "speex/speex_resampler.h" 74 77 #include "arch.h" … … 76 79 #endif /* OUTSIDE_SPEEX */ 77 80 78 #include "stack_alloc.h"79 81 #include <math.h> 82 #include <limits.h> 80 83 81 84 #ifndef M_PI 82 #define M_PI 3.14159263 83 #endif 84 85 #ifdef FIXED_POINT 86 #define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x))) 87 #else 88 #define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x)))) 89 #endif 90 85 #define M_PI 3.14159265358979323846 86 #endif 87 91 88 #define IMAX(a,b) ((a) > (b) ? (a) : (b)) 92 89 #define IMIN(a,b) ((a) < (b) ? (a) : (b)) … … 96 93 #endif 97 94 98 #ifdef _USE_SSE 95 #ifndef UINT32_MAX 96 #define UINT32_MAX 4294967295U 97 #endif 98 99 #ifdef USE_SSE 99 100 #include "resample_sse.h" 101 #endif 102 103 #ifdef USE_NEON 104 #include "resample_neon.h" 100 105 #endif 101 106 … … 114 119 spx_uint32_t num_rate; 115 120 spx_uint32_t den_rate; 116 121 117 122 int quality; 118 123 spx_uint32_t nb_channels; … … 126 131 int initialised; 127 132 int started; 128 133 129 134 /* These are per-channel */ 130 135 spx_int32_t *last_sample; 131 136 spx_uint32_t *samp_frac_num; 132 137 spx_uint32_t *magic_samples; 133 138 134 139 spx_word16_t *mem; 135 140 spx_word16_t *sinc_table; 136 141 spx_uint32_t sinc_table_length; 137 142 resampler_basic_func resampler_ptr; 138 143 139 144 int in_stride; 140 145 int out_stride; 141 146 } ; 142 147 143 static double kaiser12_table[68] = {148 static const double kaiser12_table[68] = { 144 149 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, 145 150 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, … … 155 160 0.00001000, 0.00000000}; 156 161 /* 157 static double kaiser12_table[36] = {162 static const double kaiser12_table[36] = { 158 163 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741, 159 164 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762, … … 163 168 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000}; 164 169 */ 165 static double kaiser10_table[36] = {170 static const double kaiser10_table[36] = { 166 171 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, 167 172 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, … … 171 176 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000}; 172 177 173 static double kaiser8_table[36] = {178 static const double kaiser8_table[36] = { 174 179 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, 175 180 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, … … 178 183 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, 179 184 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000}; 180 181 static double kaiser6_table[36] = {185 186 static const double kaiser6_table[36] = { 182 187 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, 183 188 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, … … 188 193 189 194 struct FuncDef { 190 double *table;195 const double *table; 191 196 int oversample; 192 197 }; 193 194 static struct FuncDef _KAISER12 = {kaiser12_table, 64}; 195 #define KAISER12 (&_KAISER12) 196 /*static struct FuncDef _KAISER12 = {kaiser12_table, 32}; 197 #define KAISER12 (&_KAISER12)*/ 198 static struct FuncDef _KAISER10 = {kaiser10_table, 32}; 199 #define KAISER10 (&_KAISER10) 200 static struct FuncDef _KAISER8 = {kaiser8_table, 32}; 201 #define KAISER8 (&_KAISER8) 202 static struct FuncDef _KAISER6 = {kaiser6_table, 32}; 203 #define KAISER6 (&_KAISER6) 198 199 static const struct FuncDef kaiser12_funcdef = {kaiser12_table, 64}; 200 #define KAISER12 (&kaiser12_funcdef) 201 static const struct FuncDef kaiser10_funcdef = {kaiser10_table, 32}; 202 #define KAISER10 (&kaiser10_funcdef) 203 static const struct FuncDef kaiser8_funcdef = {kaiser8_table, 32}; 204 #define KAISER8 (&kaiser8_funcdef) 205 static const struct FuncDef kaiser6_funcdef = {kaiser6_table, 32}; 206 #define KAISER6 (&kaiser6_funcdef) 204 207 205 208 struct QualityMapping { … … 208 211 float downsample_bandwidth; 209 212 float upsample_bandwidth; 210 struct FuncDef *window_func;213 const struct FuncDef *window_func; 211 214 }; 212 215 213 216 214 217 /* This table maps conversion quality to internal parameters. There are two 215 reasons that explain why the up-sampling bandwidth is larger than the 218 reasons that explain why the up-sampling bandwidth is larger than the 216 219 down-sampling bandwidth: 217 220 1) When up-sampling, we can assume that the spectrum is already attenuated … … 235 238 }; 236 239 /*8,24,40,56,80,104,128,160,200,256,320*/ 237 static double compute_func(float x, struct FuncDef *func)240 static double compute_func(float x, const struct FuncDef *func) 238 241 { 239 242 float y, frac; 240 243 double interp[4]; 241 int ind; 244 int ind; 242 245 y = x*func->oversample; 243 246 ind = (int)floor(y); … … 250 253 /* Just to make sure we don't have rounding problems */ 251 254 interp[1] = 1.f-interp[3]-interp[2]-interp[0]; 252 255 253 256 /*sum = frac*accum[1] + (1-frac)*accum[2];*/ 254 257 return interp[0]*func->table[ind] + interp[1]*func->table[ind+1] + interp[2]*func->table[ind+2] + interp[3]*func->table[ind+3]; … … 270 273 #ifdef FIXED_POINT 271 274 /* The slow way of computing a sinc for the table. Should improve that some day */ 272 static spx_word16_t sinc(float cutoff, float x, int N, struct FuncDef *window_func)275 static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func) 273 276 { 274 277 /*fprintf (stderr, "%f ", x);*/ … … 283 286 #else 284 287 /* The slow way of computing a sinc for the table. Should improve that some day */ 285 static spx_word16_t sinc(float cutoff, float x, int N, struct FuncDef *window_func)288 static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func) 286 289 { 287 290 /*fprintf (stderr, "%f ", x);*/ … … 338 341 const spx_uint32_t den_rate = st->den_rate; 339 342 spx_word32_t sum; 340 int j;341 343 342 344 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len)) 343 345 { 344 const spx_word16_t *sinc = & sinc_table[samp_frac_num*N];346 const spx_word16_t *sinct = & sinc_table[samp_frac_num*N]; 345 347 const spx_word16_t *iptr = & in[last_sample]; 346 348 347 349 #ifndef OVERRIDE_INNER_PRODUCT_SINGLE 348 float accum[4] = {0,0,0,0}; 349 350 int j; 351 sum = 0; 352 for(j=0;j<N;j++) sum += MULT16_16(sinct[j], iptr[j]); 353 354 /* This code is slower on most DSPs which have only 2 accumulators. 355 Plus this this forces truncation to 32 bits and you lose the HW guard bits. 356 I think we can trust the compiler and let it vectorize and/or unroll itself. 357 spx_word32_t accum[4] = {0,0,0,0}; 350 358 for(j=0;j<N;j+=4) { 351 accum[0] += sinc[j]*iptr[j];352 accum[1] += sinc[j+1]*iptr[j+1];353 accum[2] += sinc[j+2]*iptr[j+2];354 accum[3] += sinc[j+3]*iptr[j+3];359 accum[0] += MULT16_16(sinct[j], iptr[j]); 360 accum[1] += MULT16_16(sinct[j+1], iptr[j+1]); 361 accum[2] += MULT16_16(sinct[j+2], iptr[j+2]); 362 accum[3] += MULT16_16(sinct[j+3], iptr[j+3]); 355 363 } 356 364 sum = accum[0] + accum[1] + accum[2] + accum[3]; 357 #else 358 sum = inner_product_single(sinc, iptr, N); 359 #endif 360 361 out[out_stride * out_sample++] = PSHR32(sum, 15); 365 */ 366 sum = SATURATE32PSHR(sum, 15, 32767); 367 #else 368 sum = inner_product_single(sinct, iptr, N); 369 #endif 370 371 out[out_stride * out_sample++] = sum; 362 372 last_sample += int_advance; 363 373 samp_frac_num += frac_advance; … … 389 399 const spx_uint32_t den_rate = st->den_rate; 390 400 double sum; 391 int j;392 401 393 402 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len)) 394 403 { 395 const spx_word16_t *sinc = & sinc_table[samp_frac_num*N];404 const spx_word16_t *sinct = & sinc_table[samp_frac_num*N]; 396 405 const spx_word16_t *iptr = & in[last_sample]; 397 406 398 407 #ifndef OVERRIDE_INNER_PRODUCT_DOUBLE 408 int j; 399 409 double accum[4] = {0,0,0,0}; 400 410 401 411 for(j=0;j<N;j+=4) { 402 accum[0] += sinc [j]*iptr[j];403 accum[1] += sinc [j+1]*iptr[j+1];404 accum[2] += sinc [j+2]*iptr[j+2];405 accum[3] += sinc [j+3]*iptr[j+3];412 accum[0] += sinct[j]*iptr[j]; 413 accum[1] += sinct[j+1]*iptr[j+1]; 414 accum[2] += sinct[j+2]*iptr[j+2]; 415 accum[3] += sinct[j+3]*iptr[j+3]; 406 416 } 407 417 sum = accum[0] + accum[1] + accum[2] + accum[3]; 408 418 #else 409 sum = inner_product_double(sinc , iptr, N);419 sum = inner_product_double(sinct, iptr, N); 410 420 #endif 411 421 … … 436 446 const int frac_advance = st->frac_advance; 437 447 const spx_uint32_t den_rate = st->den_rate; 438 int j;439 448 spx_word32_t sum; 440 449 … … 453 462 454 463 #ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE 464 int j; 455 465 spx_word32_t accum[4] = {0,0,0,0}; 456 466 … … 464 474 465 475 cubic_coef(frac, interp); 466 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]); 476 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)); 477 sum = SATURATE32PSHR(sum, 15, 32767); 467 478 #else 468 479 cubic_coef(frac, interp); 469 480 sum = interpolate_product_single(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp); 470 481 #endif 471 472 out[out_stride * out_sample++] = PSHR32(sum,15);482 483 out[out_stride * out_sample++] = sum; 473 484 last_sample += int_advance; 474 485 samp_frac_num += frac_advance; … … 498 509 const int frac_advance = st->frac_advance; 499 510 const spx_uint32_t den_rate = st->den_rate; 500 int j;501 511 spx_word32_t sum; 502 512 … … 515 525 516 526 #ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE 527 int j; 517 528 double accum[4] = {0,0,0,0}; 518 529 … … 531 542 sum = interpolate_product_double(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp); 532 543 #endif 533 544 534 545 out[out_stride * out_sample++] = PSHR32(sum,15); 535 546 last_sample += int_advance; … … 548 559 #endif 549 560 550 static void update_filter(SpeexResamplerState *st) 551 { 552 spx_uint32_t old_length; 553 554 old_length = st->filt_len; 561 /* This resampler is used to produce zero output in situations where memory 562 for the filter could not be allocated. The expected numbers of input and 563 output samples are still processed so that callers failing to check error 564 codes are not surprised, possibly getting into infinite loops. */ 565 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) 566 { 567 int out_sample = 0; 568 int last_sample = st->last_sample[channel_index]; 569 spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; 570 const int out_stride = st->out_stride; 571 const int int_advance = st->int_advance; 572 const int frac_advance = st->frac_advance; 573 const spx_uint32_t den_rate = st->den_rate; 574 575 (void)in; 576 while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len)) 577 { 578 out[out_stride * out_sample++] = 0; 579 last_sample += int_advance; 580 samp_frac_num += frac_advance; 581 if (samp_frac_num >= den_rate) 582 { 583 samp_frac_num -= den_rate; 584 last_sample++; 585 } 586 } 587 588 st->last_sample[channel_index] = last_sample; 589 st->samp_frac_num[channel_index] = samp_frac_num; 590 return out_sample; 591 } 592 593 static int multiply_frac(spx_uint32_t *result, spx_uint32_t value, spx_uint32_t num, spx_uint32_t den) 594 { 595 spx_uint32_t major = value / den; 596 spx_uint32_t remain = value % den; 597 /* TODO: Could use 64 bits operation to check for overflow. But only guaranteed in C99+ */ 598 if (remain > UINT32_MAX / num || major > UINT32_MAX / num 599 || major * num > UINT32_MAX - remain * num / den) 600 return RESAMPLER_ERR_OVERFLOW; 601 *result = remain * num / den + major * num; 602 return RESAMPLER_ERR_SUCCESS; 603 } 604 605 static int update_filter(SpeexResamplerState *st) 606 { 607 spx_uint32_t old_length = st->filt_len; 608 spx_uint32_t old_alloc_size = st->mem_alloc_size; 609 int use_direct; 610 spx_uint32_t min_sinc_table_length; 611 spx_uint32_t min_alloc_size; 612 613 st->int_advance = st->num_rate/st->den_rate; 614 st->frac_advance = st->num_rate%st->den_rate; 555 615 st->oversample = quality_map[st->quality].oversample; 556 616 st->filt_len = quality_map[st->quality].base_length; 557 617 558 618 if (st->num_rate > st->den_rate) 559 619 { 560 620 /* down-sampling */ 561 621 st->cutoff = quality_map[st->quality].downsample_bandwidth * st->den_rate / st->num_rate; 562 /* FIXME: divide the numerator and denominator by a certain amount if they're too large */563 st->filt_len = st->filt_len*st->num_rate / st->den_rate;564 /* Round down to make sure we have a multiple of 4*/565 st->filt_len &= (~0x3);622 if (multiply_frac(&st->filt_len,st->filt_len,st->num_rate,st->den_rate) != RESAMPLER_ERR_SUCCESS) 623 goto fail; 624 /* Round up to make sure we have a multiple of 8 for SSE */ 625 st->filt_len = ((st->filt_len-1)&(~0x7))+8; 566 626 if (2*st->den_rate < st->num_rate) 567 627 st->oversample >>= 1; … … 578 638 st->cutoff = quality_map[st->quality].upsample_bandwidth; 579 639 } 580 640 641 #ifdef RESAMPLE_FULL_SINC_TABLE 642 use_direct = 1; 643 if (INT_MAX/sizeof(spx_word16_t)/st->den_rate < st->filt_len) 644 goto fail; 645 #else 581 646 /* Choose the resampling type that requires the least amount of memory */ 582 if (st->den_rate <= st->oversample) 647 use_direct = st->filt_len*st->den_rate <= st->filt_len*st->oversample+8 648 && INT_MAX/sizeof(spx_word16_t)/st->den_rate >= st->filt_len; 649 #endif 650 if (use_direct) 651 { 652 min_sinc_table_length = st->filt_len*st->den_rate; 653 } else { 654 if ((INT_MAX/sizeof(spx_word16_t)-8)/st->oversample < st->filt_len) 655 goto fail; 656 657 min_sinc_table_length = st->filt_len*st->oversample+8; 658 } 659 if (st->sinc_table_length < min_sinc_table_length) 660 { 661 spx_word16_t *sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,min_sinc_table_length*sizeof(spx_word16_t)); 662 if (!sinc_table) 663 goto fail; 664 665 st->sinc_table = sinc_table; 666 st->sinc_table_length = min_sinc_table_length; 667 } 668 if (use_direct) 583 669 { 584 670 spx_uint32_t i; 585 if (!st->sinc_table)586 st->sinc_table = (spx_word16_t *)speex_alloc(st->filt_len*st->den_rate*sizeof(spx_word16_t));587 else if (st->sinc_table_length < st->filt_len*st->den_rate)588 {589 st->sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,st->filt_len*st->den_rate*sizeof(spx_word16_t));590 st->sinc_table_length = st->filt_len*st->den_rate;591 }592 671 for (i=0;i<st->den_rate;i++) 593 672 { … … 609 688 } else { 610 689 spx_int32_t i; 611 if (!st->sinc_table)612 st->sinc_table = (spx_word16_t *)speex_alloc((st->filt_len*st->oversample+8)*sizeof(spx_word16_t));613 else if (st->sinc_table_length < st->filt_len*st->oversample+8)614 {615 st->sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,(st->filt_len*st->oversample+8)*sizeof(spx_word16_t));616 st->sinc_table_length = st->filt_len*st->oversample+8;617 }618 690 for (i=-4;i<(spx_int32_t)(st->oversample*st->filt_len+4);i++) 619 691 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); … … 628 700 /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/ 629 701 } 630 st->int_advance = st->num_rate/st->den_rate; 631 st->frac_advance = st->num_rate%st->den_rate; 632 633 702 634 703 /* Here's the place where we update the filter memory to take into account 635 704 the change in filter length. It's probably the messiest part of the code 636 705 due to handling of lots of corner cases. */ 637 if (!st->mem) 706 707 /* Adding buffer_size to filt_len won't overflow here because filt_len 708 could be multiplied by sizeof(spx_word16_t) above. */ 709 min_alloc_size = st->filt_len-1 + st->buffer_size; 710 if (min_alloc_size > st->mem_alloc_size) 711 { 712 spx_word16_t *mem; 713 if (INT_MAX/sizeof(spx_word16_t)/st->nb_channels < min_alloc_size) 714 goto fail; 715 else if (!(mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*min_alloc_size * sizeof(*mem)))) 716 goto fail; 717 718 st->mem = mem; 719 st->mem_alloc_size = min_alloc_size; 720 } 721 if (!st->started) 638 722 { 639 723 spx_uint32_t i; 640 st->mem_alloc_size = st->filt_len-1 + st->buffer_size;641 st->mem = (spx_word16_t*)speex_alloc(st->nb_channels*st->mem_alloc_size * sizeof(spx_word16_t));642 for (i=0;i<st->nb_channels*st->mem_alloc_size;i++)643 st->mem[i] = 0;644 /*speex_warning("init filter");*/645 } else if (!st->started)646 {647 spx_uint32_t i;648 st->mem_alloc_size = st->filt_len-1 + st->buffer_size;649 st->mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*st->mem_alloc_size * sizeof(spx_word16_t));650 724 for (i=0;i<st->nb_channels*st->mem_alloc_size;i++) 651 725 st->mem[i] = 0; … … 653 727 } else if (st->filt_len > old_length) 654 728 { 655 spx_ int32_t i;729 spx_uint32_t i; 656 730 /* Increase the filter length */ 657 731 /*speex_warning("increase filter size");*/ 658 int old_alloc_size = st->mem_alloc_size; 659 if ((st->filt_len-1 + st->buffer_size) > st->mem_alloc_size) 732 for (i=st->nb_channels;i--;) 660 733 { 661 st->mem_alloc_size = st->filt_len-1 + st->buffer_size; 662 st->mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*st->mem_alloc_size * sizeof(spx_word16_t)); 663 } 664 for (i=st->nb_channels-1;i>=0;i--) 665 { 666 spx_int32_t j; 734 spx_uint32_t j; 667 735 spx_uint32_t olen = old_length; 668 736 /*if (st->magic_samples[i])*/ 669 737 { 670 738 /* Try and remove the magic samples as if nothing had happened */ 671 739 672 740 /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */ 673 741 olen = old_length + 2*st->magic_samples[i]; 674 for (j=old_length- 2+st->magic_samples[i];j>=0;j--)742 for (j=old_length-1+st->magic_samples[i];j--;) 675 743 st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]] = st->mem[i*old_alloc_size+j]; 676 744 for (j=0;j<st->magic_samples[i];j++) … … 713 781 } 714 782 } 715 783 return RESAMPLER_ERR_SUCCESS; 784 785 fail: 786 st->resampler_ptr = resampler_basic_zero; 787 /* st->mem may still contain consumed input samples for the filter. 788 Restore filt_len so that filt_len - 1 still points to the position after 789 the last of these samples. */ 790 st->filt_len = old_length; 791 return RESAMPLER_ERR_ALLOC_FAILED; 716 792 } 717 793 … … 723 799 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) 724 800 { 725 spx_uint32_t i;726 801 SpeexResamplerState *st; 727 if (quality > 10 || quality < 0) 802 int filter_err; 803 804 if (nb_channels == 0 || ratio_num == 0 || ratio_den == 0 || quality > 10 || quality < 0) 728 805 { 729 806 if (err) … … 732 809 } 733 810 st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState)); 811 if (!st) 812 { 813 if (err) 814 *err = RESAMPLER_ERR_ALLOC_FAILED; 815 return NULL; 816 } 734 817 st->initialised = 0; 735 818 st->started = 0; … … 744 827 st->mem = 0; 745 828 st->resampler_ptr = 0; 746 829 747 830 st->cutoff = 1.f; 748 831 st->nb_channels = nb_channels; 749 832 st->in_stride = 1; 750 833 st->out_stride = 1; 751 752 #ifdef FIXED_POINT 834 753 835 st->buffer_size = 160; 754 #else 755 st->buffer_size = 160; 756 #endif 757 836 758 837 /* Per channel data */ 759 st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(int)); 760 st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(int)); 761 st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(int)); 762 for (i=0;i<nb_channels;i++) 763 { 764 st->last_sample[i] = 0; 765 st->magic_samples[i] = 0; 766 st->samp_frac_num[i] = 0; 767 } 838 if (!(st->last_sample = (spx_int32_t*)speex_alloc(nb_channels*sizeof(spx_int32_t)))) 839 goto fail; 840 if (!(st->magic_samples = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t)))) 841 goto fail; 842 if (!(st->samp_frac_num = (spx_uint32_t*)speex_alloc(nb_channels*sizeof(spx_uint32_t)))) 843 goto fail; 768 844 769 845 speex_resampler_set_quality(st, quality); 770 846 speex_resampler_set_rate_frac(st, ratio_num, ratio_den, in_rate, out_rate); 771 847 772 773 update_filter(st); 774 775 st->initialised = 1; 848 filter_err = update_filter(st); 849 if (filter_err == RESAMPLER_ERR_SUCCESS) 850 { 851 st->initialised = 1; 852 } else { 853 speex_resampler_destroy(st); 854 st = NULL; 855 } 776 856 if (err) 777 *err = RESAMPLER_ERR_SUCCESS;857 *err = filter_err; 778 858 779 859 return st; 860 861 fail: 862 if (err) 863 *err = RESAMPLER_ERR_ALLOC_FAILED; 864 speex_resampler_destroy(st); 865 return NULL; 780 866 } 781 867 … … 797 883 spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; 798 884 spx_uint32_t ilen; 799 885 800 886 st->started = 1; 801 887 802 888 /* Call the right resampler through the function ptr */ 803 889 out_sample = st->resampler_ptr(st, channel_index, mem, in_len, out, out_len); 804 890 805 891 if (st->last_sample[channel_index] < (spx_int32_t)*in_len) 806 892 *in_len = st->last_sample[channel_index]; 807 893 *out_len = out_sample; 808 894 st->last_sample[channel_index] -= *in_len; 809 895 810 896 ilen = *in_len; 811 897 … … 820 906 spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; 821 907 const int N = st->filt_len; 822 908 823 909 speex_resampler_process_native(st, channel_index, &tmp_in_len, *out, &out_len); 824 910 825 911 st->magic_samples[channel_index] -= tmp_in_len; 826 912 827 913 /* If we couldn't process all "magic" input samples, save the rest for next time */ 828 914 if (st->magic_samples[channel_index]) … … 850 936 const int istride = st->in_stride; 851 937 852 if (st->magic_samples[channel_index]) 938 if (st->magic_samples[channel_index]) 853 939 olen -= speex_resampler_magic(st, channel_index, &out, olen); 854 940 if (! st->magic_samples[channel_index]) { … … 856 942 spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; 857 943 spx_uint32_t ochunk = olen; 858 944 859 945 if (in) { 860 946 for(j=0;j<ichunk;++j) … … 874 960 *in_len -= ilen; 875 961 *out_len -= olen; 876 return RESAMPLER_ERR_SUCCESS;962 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; 877 963 } 878 964 … … 892 978 #ifdef VAR_ARRAYS 893 979 const unsigned int ylen = (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC; 894 VARDECL(spx_word16_t *ystack); 895 ALLOC(ystack, ylen, spx_word16_t); 980 spx_word16_t ystack[ylen]; 896 981 #else 897 982 const unsigned int ylen = FIXED_STACK_ALLOC; … … 900 985 901 986 st->out_stride = 1; 902 987 903 988 while (ilen && olen) { 904 989 spx_word16_t *y = ystack; … … 937 1022 out[j*ostride_save] = WORD2INT(ystack[j]); 938 1023 #endif 939 1024 940 1025 ilen -= ichunk; 941 1026 olen -= ochunk; … … 948 1033 *out_len -= olen; 949 1034 950 return RESAMPLER_ERR_SUCCESS;1035 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; 951 1036 } 952 1037 … … 955 1040 spx_uint32_t i; 956 1041 int istride_save, ostride_save; 957 spx_uint32_t bak_len = *out_len; 1042 spx_uint32_t bak_out_len = *out_len; 1043 spx_uint32_t bak_in_len = *in_len; 958 1044 istride_save = st->in_stride; 959 1045 ostride_save = st->out_stride; … … 961 1047 for (i=0;i<st->nb_channels;i++) 962 1048 { 963 *out_len = bak_len; 1049 *out_len = bak_out_len; 1050 *in_len = bak_in_len; 964 1051 if (in != NULL) 965 1052 speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len); … … 969 1056 st->in_stride = istride_save; 970 1057 st->out_stride = ostride_save; 971 return RESAMPLER_ERR_SUCCESS;972 } 973 1058 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; 1059 } 1060 974 1061 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) 975 1062 { 976 1063 spx_uint32_t i; 977 1064 int istride_save, ostride_save; 978 spx_uint32_t bak_len = *out_len; 1065 spx_uint32_t bak_out_len = *out_len; 1066 spx_uint32_t bak_in_len = *in_len; 979 1067 istride_save = st->in_stride; 980 1068 ostride_save = st->out_stride; … … 982 1070 for (i=0;i<st->nb_channels;i++) 983 1071 { 984 *out_len = bak_len; 1072 *out_len = bak_out_len; 1073 *in_len = bak_in_len; 985 1074 if (in != NULL) 986 1075 speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len); … … 990 1079 st->in_stride = istride_save; 991 1080 st->out_stride = ostride_save; 992 return RESAMPLER_ERR_SUCCESS;1081 return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; 993 1082 } 994 1083 … … 1002 1091 *in_rate = st->in_rate; 1003 1092 *out_rate = st->out_rate; 1093 } 1094 1095 static inline spx_uint32_t compute_gcd(spx_uint32_t a, spx_uint32_t b) 1096 { 1097 while (b != 0) 1098 { 1099 spx_uint32_t temp = a; 1100 1101 a = b; 1102 b = temp % b; 1103 } 1104 return a; 1004 1105 } 1005 1106 … … 1009 1110 spx_uint32_t old_den; 1010 1111 spx_uint32_t i; 1112 1113 if (ratio_num == 0 || ratio_den == 0) 1114 return RESAMPLER_ERR_INVALID_ARG; 1115 1011 1116 if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den) 1012 1117 return RESAMPLER_ERR_SUCCESS; 1013 1118 1014 1119 old_den = st->den_rate; 1015 1120 st->in_rate = in_rate; … … 1017 1122 st->num_rate = ratio_num; 1018 1123 st->den_rate = ratio_den; 1019 /* FIXME: This is terribly inefficient, but who cares (at least for now)? */ 1020 for (fact=2;fact<=IMIN(st->num_rate, st->den_rate);fact++) 1021 { 1022 while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0)) 1023 { 1024 st->num_rate /= fact; 1025 st->den_rate /= fact; 1026 } 1027 } 1028 1124 1125 fact = compute_gcd(st->num_rate, st->den_rate); 1126 1127 st->num_rate /= fact; 1128 st->den_rate /= fact; 1129 1029 1130 if (old_den > 0) 1030 1131 { 1031 1132 for (i=0;i<st->nb_channels;i++) 1032 1133 { 1033 st->samp_frac_num[i]=st->samp_frac_num[i]*st->den_rate/old_den; 1134 if (multiply_frac(&st->samp_frac_num[i],st->samp_frac_num[i],st->den_rate,old_den) != RESAMPLER_ERR_SUCCESS) 1135 return RESAMPLER_ERR_OVERFLOW; 1034 1136 /* Safety net */ 1035 1137 if (st->samp_frac_num[i] >= st->den_rate) … … 1037 1139 } 1038 1140 } 1039 1141 1040 1142 if (st->initialised) 1041 update_filter(st);1143 return update_filter(st); 1042 1144 return RESAMPLER_ERR_SUCCESS; 1043 1145 } … … 1057 1159 st->quality = quality; 1058 1160 if (st->initialised) 1059 update_filter(st);1161 return update_filter(st); 1060 1162 return RESAMPLER_ERR_SUCCESS; 1061 1163 } … … 1107 1209 { 1108 1210 spx_uint32_t i; 1211 for (i=0;i<st->nb_channels;i++) 1212 { 1213 st->last_sample[i] = 0; 1214 st->magic_samples[i] = 0; 1215 st->samp_frac_num[i] = 0; 1216 } 1109 1217 for (i=0;i<st->nb_channels*(st->filt_len-1);i++) 1110 1218 st->mem[i] = 0;
Note: See TracChangeset
for help on using the changeset viewer.