Ignore:
Timestamp:
Jan 9, 2020 9:05:50 AM (4 years ago)
Author:
ming
Message:

Closed #589: Update Speex AEC to the latest version to get multichannel EC

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/third_party/speex/libspeex/resample.c

    r2002 r6129  
    11/* Copyright (C) 2007-2008 Jean-Marc Valin 
    22   Copyright (C) 2008      Thorvald Natvig 
    3        
     3 
    44   File: resample.c 
    55   Arbitrary resampling code 
     
    3939      - Good *perceptual* quality (and not best SNR) 
    4040 
    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 
    4242   all the major bugs and I don't expect the API to change anymore, there 
    4343   may be something I've missed. So use with caution. 
     
    4545   This algorithm is based on this original resampling algorithm: 
    4646   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), 
    4848   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 
    5252   interpolation instead of linear interpolation in the above paper. This 
    5353   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. 
    5757   The latter both reduces CPU time and makes the algorithm more SIMD-friendly. 
    5858*/ 
     
    6464#ifdef OUTSIDE_SPEEX 
    6565#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);} 
     66static void *speex_alloc(int size) {return calloc(size,1);} 
     67static void *speex_realloc(void *ptr, int size) {return realloc(ptr, size);} 
     68static void speex_free(void *ptr) {free(ptr);} 
     69#ifndef EXPORT 
     70#define EXPORT 
     71#endif 
    6972#include "speex_resampler.h" 
    7073#include "arch.h" 
    7174#else /* OUTSIDE_SPEEX */ 
    72                 
     75 
    7376#include "speex/speex_resampler.h" 
    7477#include "arch.h" 
     
    7679#endif /* OUTSIDE_SPEEX */ 
    7780 
    78 #include "stack_alloc.h" 
    7981#include <math.h> 
     82#include <limits.h> 
    8083 
    8184#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 
    9188#define IMAX(a,b) ((a) > (b) ? (a) : (b)) 
    9289#define IMIN(a,b) ((a) < (b) ? (a) : (b)) 
     
    9693#endif 
    9794 
    98 #ifdef _USE_SSE 
     95#ifndef UINT32_MAX 
     96#define UINT32_MAX 4294967295U 
     97#endif 
     98 
     99#ifdef USE_SSE 
    99100#include "resample_sse.h" 
     101#endif 
     102 
     103#ifdef USE_NEON 
     104#include "resample_neon.h" 
    100105#endif 
    101106 
     
    114119   spx_uint32_t num_rate; 
    115120   spx_uint32_t den_rate; 
    116     
     121 
    117122   int    quality; 
    118123   spx_uint32_t nb_channels; 
     
    126131   int          initialised; 
    127132   int          started; 
    128     
     133 
    129134   /* These are per-channel */ 
    130135   spx_int32_t  *last_sample; 
    131136   spx_uint32_t *samp_frac_num; 
    132137   spx_uint32_t *magic_samples; 
    133     
     138 
    134139   spx_word16_t *mem; 
    135140   spx_word16_t *sinc_table; 
    136141   spx_uint32_t sinc_table_length; 
    137142   resampler_basic_func resampler_ptr; 
    138           
     143 
    139144   int    in_stride; 
    140145   int    out_stride; 
    141146} ; 
    142147 
    143 static double kaiser12_table[68] = { 
     148static const double kaiser12_table[68] = { 
    144149   0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, 
    145150   0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, 
     
    155160   0.00001000, 0.00000000}; 
    156161/* 
    157 static double kaiser12_table[36] = { 
     162static const double kaiser12_table[36] = { 
    158163   0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741, 
    159164   0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762, 
     
    163168   0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000}; 
    164169*/ 
    165 static double kaiser10_table[36] = { 
     170static const double kaiser10_table[36] = { 
    166171   0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, 
    167172   0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, 
     
    171176   0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000}; 
    172177 
    173 static double kaiser8_table[36] = { 
     178static const double kaiser8_table[36] = { 
    174179   0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, 
    175180   0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, 
     
    178183   0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, 
    179184   0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000}; 
    180     
    181 static double kaiser6_table[36] = { 
     185 
     186static const double kaiser6_table[36] = { 
    182187   0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, 
    183188   0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, 
     
    188193 
    189194struct FuncDef { 
    190    double *table; 
     195   const double *table; 
    191196   int oversample; 
    192197}; 
    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 
     199static const struct FuncDef kaiser12_funcdef = {kaiser12_table, 64}; 
     200#define KAISER12 (&kaiser12_funcdef) 
     201static const struct FuncDef kaiser10_funcdef = {kaiser10_table, 32}; 
     202#define KAISER10 (&kaiser10_funcdef) 
     203static const struct FuncDef kaiser8_funcdef = {kaiser8_table, 32}; 
     204#define KAISER8 (&kaiser8_funcdef) 
     205static const struct FuncDef kaiser6_funcdef = {kaiser6_table, 32}; 
     206#define KAISER6 (&kaiser6_funcdef) 
    204207 
    205208struct QualityMapping { 
     
    208211   float downsample_bandwidth; 
    209212   float upsample_bandwidth; 
    210    struct FuncDef *window_func; 
     213   const struct FuncDef *window_func; 
    211214}; 
    212215 
    213216 
    214217/* 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 
    216219   down-sampling bandwidth: 
    217220   1) When up-sampling, we can assume that the spectrum is already attenuated 
     
    235238}; 
    236239/*8,24,40,56,80,104,128,160,200,256,320*/ 
    237 static double compute_func(float x, struct FuncDef *func) 
     240static double compute_func(float x, const struct FuncDef *func) 
    238241{ 
    239242   float y, frac; 
    240243   double interp[4]; 
    241    int ind;  
     244   int ind; 
    242245   y = x*func->oversample; 
    243246   ind = (int)floor(y); 
     
    250253   /* Just to make sure we don't have rounding problems */ 
    251254   interp[1] = 1.f-interp[3]-interp[2]-interp[0]; 
    252     
     255 
    253256   /*sum = frac*accum[1] + (1-frac)*accum[2];*/ 
    254257   return interp[0]*func->table[ind] + interp[1]*func->table[ind+1] + interp[2]*func->table[ind+2] + interp[3]*func->table[ind+3]; 
     
    270273#ifdef FIXED_POINT 
    271274/* 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) 
     275static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func) 
    273276{ 
    274277   /*fprintf (stderr, "%f ", x);*/ 
     
    283286#else 
    284287/* 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) 
     288static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func) 
    286289{ 
    287290   /*fprintf (stderr, "%f ", x);*/ 
     
    338341   const spx_uint32_t den_rate = st->den_rate; 
    339342   spx_word32_t sum; 
    340    int j; 
    341343 
    342344   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len)) 
    343345   { 
    344       const spx_word16_t *sinc = & sinc_table[samp_frac_num*N]; 
     346      const spx_word16_t *sinct = & sinc_table[samp_frac_num*N]; 
    345347      const spx_word16_t *iptr = & in[last_sample]; 
    346348 
    347349#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}; 
    350358      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]); 
    355363      } 
    356364      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; 
    362372      last_sample += int_advance; 
    363373      samp_frac_num += frac_advance; 
     
    389399   const spx_uint32_t den_rate = st->den_rate; 
    390400   double sum; 
    391    int j; 
    392401 
    393402   while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len)) 
    394403   { 
    395       const spx_word16_t *sinc = & sinc_table[samp_frac_num*N]; 
     404      const spx_word16_t *sinct = & sinc_table[samp_frac_num*N]; 
    396405      const spx_word16_t *iptr = & in[last_sample]; 
    397406 
    398407#ifndef OVERRIDE_INNER_PRODUCT_DOUBLE 
     408      int j; 
    399409      double accum[4] = {0,0,0,0}; 
    400410 
    401411      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]; 
    406416      } 
    407417      sum = accum[0] + accum[1] + accum[2] + accum[3]; 
    408418#else 
    409       sum = inner_product_double(sinc, iptr, N); 
     419      sum = inner_product_double(sinct, iptr, N); 
    410420#endif 
    411421 
     
    436446   const int frac_advance = st->frac_advance; 
    437447   const spx_uint32_t den_rate = st->den_rate; 
    438    int j; 
    439448   spx_word32_t sum; 
    440449 
     
    453462 
    454463#ifndef OVERRIDE_INTERPOLATE_PRODUCT_SINGLE 
     464      int j; 
    455465      spx_word32_t accum[4] = {0,0,0,0}; 
    456466 
     
    464474 
    465475      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); 
    467478#else 
    468479      cubic_coef(frac, interp); 
    469480      sum = interpolate_product_single(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp); 
    470481#endif 
    471        
    472       out[out_stride * out_sample++] = PSHR32(sum,15); 
     482 
     483      out[out_stride * out_sample++] = sum; 
    473484      last_sample += int_advance; 
    474485      samp_frac_num += frac_advance; 
     
    498509   const int frac_advance = st->frac_advance; 
    499510   const spx_uint32_t den_rate = st->den_rate; 
    500    int j; 
    501511   spx_word32_t sum; 
    502512 
     
    515525 
    516526#ifndef OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE 
     527      int j; 
    517528      double accum[4] = {0,0,0,0}; 
    518529 
     
    531542      sum = interpolate_product_double(iptr, st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, interp); 
    532543#endif 
    533        
     544 
    534545      out[out_stride * out_sample++] = PSHR32(sum,15); 
    535546      last_sample += int_advance; 
     
    548559#endif 
    549560 
    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. */ 
     565static 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 
     593static 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 
     605static 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; 
    555615   st->oversample = quality_map[st->quality].oversample; 
    556616   st->filt_len = quality_map[st->quality].base_length; 
    557     
     617 
    558618   if (st->num_rate > st->den_rate) 
    559619   { 
    560620      /* down-sampling */ 
    561621      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; 
    566626      if (2*st->den_rate < st->num_rate) 
    567627         st->oversample >>= 1; 
     
    578638      st->cutoff = quality_map[st->quality].upsample_bandwidth; 
    579639   } 
    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 
    581646   /* 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) 
    583669   { 
    584670      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       } 
    592671      for (i=0;i<st->den_rate;i++) 
    593672      { 
     
    609688   } else { 
    610689      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       } 
    618690      for (i=-4;i<(spx_int32_t)(st->oversample*st->filt_len+4);i++) 
    619691         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); 
     
    628700      /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/ 
    629701   } 
    630    st->int_advance = st->num_rate/st->den_rate; 
    631    st->frac_advance = st->num_rate%st->den_rate; 
    632  
    633     
     702 
    634703   /* Here's the place where we update the filter memory to take into account 
    635704      the change in filter length. It's probably the messiest part of the code 
    636705      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) 
    638722   { 
    639723      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)); 
    650724      for (i=0;i<st->nb_channels*st->mem_alloc_size;i++) 
    651725         st->mem[i] = 0; 
     
    653727   } else if (st->filt_len > old_length) 
    654728   { 
    655       spx_int32_t i; 
     729      spx_uint32_t i; 
    656730      /* Increase the filter length */ 
    657731      /*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--;) 
    660733      { 
    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; 
    667735         spx_uint32_t olen = old_length; 
    668736         /*if (st->magic_samples[i])*/ 
    669737         { 
    670738            /* Try and remove the magic samples as if nothing had happened */ 
    671              
     739 
    672740            /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */ 
    673741            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--;) 
    675743               st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]] = st->mem[i*old_alloc_size+j]; 
    676744            for (j=0;j<st->magic_samples[i];j++) 
     
    713781      } 
    714782   } 
    715  
     783   return RESAMPLER_ERR_SUCCESS; 
     784 
     785fail: 
     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; 
    716792} 
    717793 
     
    723799EXPORT 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) 
    724800{ 
    725    spx_uint32_t i; 
    726801   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) 
    728805   { 
    729806      if (err) 
     
    732809   } 
    733810   st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState)); 
     811   if (!st) 
     812   { 
     813      if (err) 
     814         *err = RESAMPLER_ERR_ALLOC_FAILED; 
     815      return NULL; 
     816   } 
    734817   st->initialised = 0; 
    735818   st->started = 0; 
     
    744827   st->mem = 0; 
    745828   st->resampler_ptr = 0; 
    746           
     829 
    747830   st->cutoff = 1.f; 
    748831   st->nb_channels = nb_channels; 
    749832   st->in_stride = 1; 
    750833   st->out_stride = 1; 
    751     
    752 #ifdef FIXED_POINT 
     834 
    753835   st->buffer_size = 160; 
    754 #else 
    755    st->buffer_size = 160; 
    756 #endif 
    757     
     836 
    758837   /* 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; 
    768844 
    769845   speex_resampler_set_quality(st, quality); 
    770846   speex_resampler_set_rate_frac(st, ratio_num, ratio_den, in_rate, out_rate); 
    771847 
    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   } 
    776856   if (err) 
    777       *err = RESAMPLER_ERR_SUCCESS; 
     857      *err = filter_err; 
    778858 
    779859   return st; 
     860 
     861fail: 
     862   if (err) 
     863      *err = RESAMPLER_ERR_ALLOC_FAILED; 
     864   speex_resampler_destroy(st); 
     865   return NULL; 
    780866} 
    781867 
     
    797883   spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; 
    798884   spx_uint32_t ilen; 
    799     
     885 
    800886   st->started = 1; 
    801     
     887 
    802888   /* Call the right resampler through the function ptr */ 
    803889   out_sample = st->resampler_ptr(st, channel_index, mem, in_len, out, out_len); 
    804     
     890 
    805891   if (st->last_sample[channel_index] < (spx_int32_t)*in_len) 
    806892      *in_len = st->last_sample[channel_index]; 
    807893   *out_len = out_sample; 
    808894   st->last_sample[channel_index] -= *in_len; 
    809     
     895 
    810896   ilen = *in_len; 
    811897 
     
    820906   spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; 
    821907   const int N = st->filt_len; 
    822     
     908 
    823909   speex_resampler_process_native(st, channel_index, &tmp_in_len, *out, &out_len); 
    824910 
    825911   st->magic_samples[channel_index] -= tmp_in_len; 
    826     
     912 
    827913   /* If we couldn't process all "magic" input samples, save the rest for next time */ 
    828914   if (st->magic_samples[channel_index]) 
     
    850936   const int istride = st->in_stride; 
    851937 
    852    if (st->magic_samples[channel_index])  
     938   if (st->magic_samples[channel_index]) 
    853939      olen -= speex_resampler_magic(st, channel_index, &out, olen); 
    854940   if (! st->magic_samples[channel_index]) { 
     
    856942        spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; 
    857943        spx_uint32_t ochunk = olen; 
    858   
     944 
    859945        if (in) { 
    860946           for(j=0;j<ichunk;++j) 
     
    874960   *in_len -= ilen; 
    875961   *out_len -= olen; 
    876    return RESAMPLER_ERR_SUCCESS; 
     962   return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; 
    877963} 
    878964 
     
    892978#ifdef VAR_ARRAYS 
    893979   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]; 
    896981#else 
    897982   const unsigned int ylen = FIXED_STACK_ALLOC; 
     
    900985 
    901986   st->out_stride = 1; 
    902     
     987 
    903988   while (ilen && olen) { 
    904989     spx_word16_t *y = ystack; 
     
    9371022        out[j*ostride_save] = WORD2INT(ystack[j]); 
    9381023#endif 
    939       
     1024 
    9401025     ilen -= ichunk; 
    9411026     olen -= ochunk; 
     
    9481033   *out_len -= olen; 
    9491034 
    950    return RESAMPLER_ERR_SUCCESS; 
     1035   return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; 
    9511036} 
    9521037 
     
    9551040   spx_uint32_t i; 
    9561041   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; 
    9581044   istride_save = st->in_stride; 
    9591045   ostride_save = st->out_stride; 
     
    9611047   for (i=0;i<st->nb_channels;i++) 
    9621048   { 
    963       *out_len = bak_len; 
     1049      *out_len = bak_out_len; 
     1050      *in_len = bak_in_len; 
    9641051      if (in != NULL) 
    9651052         speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len); 
     
    9691056   st->in_stride = istride_save; 
    9701057   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 
    9741061EXPORT 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) 
    9751062{ 
    9761063   spx_uint32_t i; 
    9771064   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; 
    9791067   istride_save = st->in_stride; 
    9801068   ostride_save = st->out_stride; 
     
    9821070   for (i=0;i<st->nb_channels;i++) 
    9831071   { 
    984       *out_len = bak_len; 
     1072      *out_len = bak_out_len; 
     1073      *in_len = bak_in_len; 
    9851074      if (in != NULL) 
    9861075         speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len); 
     
    9901079   st->in_stride = istride_save; 
    9911080   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; 
    9931082} 
    9941083 
     
    10021091   *in_rate = st->in_rate; 
    10031092   *out_rate = st->out_rate; 
     1093} 
     1094 
     1095static 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; 
    10041105} 
    10051106 
     
    10091110   spx_uint32_t old_den; 
    10101111   spx_uint32_t i; 
     1112 
     1113   if (ratio_num == 0 || ratio_den == 0) 
     1114      return RESAMPLER_ERR_INVALID_ARG; 
     1115 
    10111116   if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den) 
    10121117      return RESAMPLER_ERR_SUCCESS; 
    1013     
     1118 
    10141119   old_den = st->den_rate; 
    10151120   st->in_rate = in_rate; 
     
    10171122   st->num_rate = ratio_num; 
    10181123   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 
    10291130   if (old_den > 0) 
    10301131   { 
    10311132      for (i=0;i<st->nb_channels;i++) 
    10321133      { 
    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; 
    10341136         /* Safety net */ 
    10351137         if (st->samp_frac_num[i] >= st->den_rate) 
     
    10371139      } 
    10381140   } 
    1039     
     1141 
    10401142   if (st->initialised) 
    1041       update_filter(st); 
     1143      return update_filter(st); 
    10421144   return RESAMPLER_ERR_SUCCESS; 
    10431145} 
     
    10571159   st->quality = quality; 
    10581160   if (st->initialised) 
    1059       update_filter(st); 
     1161      return update_filter(st); 
    10601162   return RESAMPLER_ERR_SUCCESS; 
    10611163} 
     
    11071209{ 
    11081210   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   } 
    11091217   for (i=0;i<st->nb_channels*(st->filt_len-1);i++) 
    11101218      st->mem[i] = 0; 
Note: See TracChangeset for help on using the changeset viewer.