author | Karl Tomlinson <karlt+@karlt.net> |
Thu, 07 Aug 2014 18:13:12 +1200 | |
changeset 208140 | 25b9c248cbbd65a9d0d772585558758819fa5fbe |
parent 208139 | 87f53a100db001abcf645a2b941e52aa2fc1cddb |
child 208141 | 2260b0f9e4241503630f528158ef01e41d824172 |
push id | unknown |
push user | unknown |
push date | unknown |
reviewers | padenot |
bugs | 995075 |
milestone | 34.0a1 |
first release with | nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
|
last release without | nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
|
--- a/media/libspeex_resampler/README_MOZILLA +++ b/media/libspeex_resampler/README_MOZILLA @@ -1,5 +1,5 @@ This source is from the Speex DSP library -(http://git.xiph.org/?p=speexdsp.git), from commit 305e54ea. +(http://git.xiph.org/?p=speexdsp.git), from commit 769dc295. It consists in the audio resampling code (resampler.c) and its header files dependancies, imported into the tree using the update.sh script.
--- a/media/libspeex_resampler/hugemem.patch +++ b/media/libspeex_resampler/hugemem.patch @@ -15,29 +15,29 @@ diff --git a/media/libspeex_resampler/sr #ifdef OUTSIDE_SPEEX #include <stdlib.h> static void *speex_alloc (int size) {return calloc(size,1);} static void *speex_realloc (void *ptr, int size) {return realloc(ptr, size);} static void speex_free (void *ptr) {free(ptr);} #include "speex_resampler.h" #include "arch.h" #else /* OUTSIDE_SPEEX */ -@@ -613,18 +615,18 @@ static void update_filter(SpeexResampler +@@ -632,18 +634,18 @@ static int update_filter(SpeexResamplerS if (st->oversample < 1) st->oversample = 1; } else { /* up-sampling */ st->cutoff = quality_map[st->quality].upsample_bandwidth; } /* Choose the resampling type that requires the least amount of memory */ -#ifdef RESAMPLE_FULL_SINC_TABLE -- if (1) +- use_direct = 1; +#ifdef RESAMPLE_HUGEMEM -+ if (st->den_rate <= 16*(st->oversample+8)) ++ use_direct = st->den_rate <= 16*(st->oversample+8); #else - if (st->filt_len*st->den_rate <= st->filt_len*st->oversample+8) + use_direct = st->filt_len*st->den_rate <= st->filt_len*st->oversample+8; #endif + if (use_direct) { - spx_uint32_t i; - if (st->sinc_table_length < st->filt_len*st->den_rate) - { - st->sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,st->filt_len*st->den_rate*sizeof(spx_word16_t)); + min_sinc_table_length = st->filt_len*st->den_rate; + } else { + min_sinc_table_length = st->filt_len*st->oversample+8;
--- a/media/libspeex_resampler/src/resample.c +++ b/media/libspeex_resampler/src/resample.c @@ -135,84 +135,84 @@ struct SpeexResamplerState_ { spx_word16_t *sinc_table; spx_uint32_t sinc_table_length; resampler_basic_func resampler_ptr; int in_stride; int out_stride; } ; -static double kaiser12_table[68] = { +static const double kaiser12_table[68] = { 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601, 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014, 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490, 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546, 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178, 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947, 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058, 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438, 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734, 0.00001000, 0.00000000}; /* -static double kaiser12_table[36] = { +static const double kaiser12_table[36] = { 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741, 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762, 0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274, 0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466, 0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291, 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000}; */ -static double kaiser10_table[36] = { +static const double kaiser10_table[36] = { 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962, 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451, 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739, 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000}; -static double kaiser8_table[36] = { +static const double kaiser8_table[36] = { 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272, 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758, 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000}; -static double kaiser6_table[36] = { +static const double kaiser6_table[36] = { 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561, 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058, 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600, 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000}; struct FuncDef { - double *table; + const double *table; int oversample; }; -static struct FuncDef _KAISER12 = {kaiser12_table, 64}; +static const struct FuncDef _KAISER12 = {kaiser12_table, 64}; #define KAISER12 (&_KAISER12) /*static struct FuncDef _KAISER12 = {kaiser12_table, 32}; #define KAISER12 (&_KAISER12)*/ -static struct FuncDef _KAISER10 = {kaiser10_table, 32}; +static const struct FuncDef _KAISER10 = {kaiser10_table, 32}; #define KAISER10 (&_KAISER10) -static struct FuncDef _KAISER8 = {kaiser8_table, 32}; +static const struct FuncDef _KAISER8 = {kaiser8_table, 32}; #define KAISER8 (&_KAISER8) -static struct FuncDef _KAISER6 = {kaiser6_table, 32}; +static const struct FuncDef _KAISER6 = {kaiser6_table, 32}; #define KAISER6 (&_KAISER6) struct QualityMapping { int base_length; int oversample; float downsample_bandwidth; float upsample_bandwidth; - struct FuncDef *window_func; + const struct FuncDef *window_func; }; /* This table maps conversion quality to internal parameters. There are two reasons that explain why the up-sampling bandwidth is larger than the down-sampling bandwidth: 1) When up-sampling, we can assume that the spectrum is already attenuated close to the Nyquist rate (from an A/D or a previous resampling filter) @@ -229,17 +229,17 @@ static const struct QualityMapping quali { 80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 */ /* 89.1% cutoff (~100 dB stop) 10 */ { 96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 */ /* 91.5% cutoff (~100 dB stop) 10 */ {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 */ /* 93.1% cutoff (~100 dB stop) 10 */ {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 */ /* 94.5% cutoff (~100 dB stop) 10 */ {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 */ /* 95.5% cutoff (~100 dB stop) 10 */ {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 */ /* 96.6% cutoff (~100 dB stop) 10 */ }; /*8,24,40,56,80,104,128,160,200,256,320*/ -static double compute_func(float x, struct FuncDef *func) +static double compute_func(float x, const struct FuncDef *func) { float y, frac; double interp[4]; int ind; y = x*func->oversample; ind = (int)floor(y); frac = (y-ind); /* CSE with handle the repeated powers */ @@ -264,30 +264,30 @@ int main(int argc, char **argv) printf ("%f\n", compute_func(i/256., KAISER12)); } return 0; } #endif #ifdef FIXED_POINT /* The slow way of computing a sinc for the table. Should improve that some day */ -static spx_word16_t sinc(float cutoff, float x, int N, struct FuncDef *window_func) +static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func) { /*fprintf (stderr, "%f ", x);*/ float xx = x * cutoff; if (fabs(x)<1e-6f) return WORD2INT(32768.*cutoff); else if (fabs(x) > .5f*N) return 0; /*FIXME: Can it really be any slower than this? */ return WORD2INT(32768.*cutoff*sin(M_PI*xx)/(M_PI*xx) * compute_func(fabs(2.*x/N), window_func)); } #else /* The slow way of computing a sinc for the table. Should improve that some day */ -static spx_word16_t sinc(float cutoff, float x, int N, struct FuncDef *window_func) +static spx_word16_t sinc(float cutoff, float x, int N, const struct FuncDef *window_func) { /*fprintf (stderr, "%f ", x);*/ float xx = x * cutoff; if (fabs(x)<1e-6) return cutoff; else if (fabs(x) > .5*N) return 0; /*FIXME: Can it really be any slower than this? */ @@ -566,22 +566,57 @@ static int resampler_basic_interpolate_d } st->last_sample[channel_index] = last_sample; st->samp_frac_num[channel_index] = samp_frac_num; return out_sample; } #endif -static void update_filter(SpeexResamplerState *st) +/* This resampler is used to produce zero output in situations where memory + for the filter could not be allocated. The expected numbers of input and + output samples are still processed so that callers failing to check error + codes are not surprised, possibly getting into infinite loops. */ +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) +{ + int out_sample = 0; + int last_sample = st->last_sample[channel_index]; + spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; + const int out_stride = st->out_stride; + const int int_advance = st->int_advance; + const int frac_advance = st->frac_advance; + const spx_uint32_t den_rate = st->den_rate; + + while (!(last_sample >= (spx_int32_t)*in_len || out_sample >= (spx_int32_t)*out_len)) + { + out[out_stride * out_sample++] = 0; + last_sample += int_advance; + samp_frac_num += frac_advance; + if (samp_frac_num >= den_rate) + { + samp_frac_num -= den_rate; + last_sample++; + } + } + + st->last_sample[channel_index] = last_sample; + st->samp_frac_num[channel_index] = samp_frac_num; + return out_sample; +} + +static int update_filter(SpeexResamplerState *st) { spx_uint32_t old_length = st->filt_len; spx_uint32_t old_alloc_size = st->mem_alloc_size; + int use_direct; + spx_uint32_t min_sinc_table_length; spx_uint32_t min_alloc_size; + st->int_advance = st->num_rate/st->den_rate; + st->frac_advance = st->num_rate%st->den_rate; st->oversample = quality_map[st->quality].oversample; st->filt_len = quality_map[st->quality].base_length; if (st->num_rate > st->den_rate) { /* down-sampling */ st->cutoff = quality_map[st->quality].downsample_bandwidth * st->den_rate / st->num_rate; /* FIXME: divide the numerator and denominator by a certain amount if they're too large */ @@ -600,27 +635,38 @@ static void update_filter(SpeexResampler st->oversample = 1; } else { /* up-sampling */ st->cutoff = quality_map[st->quality].upsample_bandwidth; } /* Choose the resampling type that requires the least amount of memory */ #ifdef RESAMPLE_HUGEMEM - if (st->den_rate <= 16*(st->oversample+8)) + use_direct = st->den_rate <= 16*(st->oversample+8); #else - if (st->filt_len*st->den_rate <= st->filt_len*st->oversample+8) + use_direct = st->filt_len*st->den_rate <= st->filt_len*st->oversample+8; #endif + if (use_direct) + { + min_sinc_table_length = st->filt_len*st->den_rate; + } else { + min_sinc_table_length = st->filt_len*st->oversample+8; + } + if (st->sinc_table_length < min_sinc_table_length) + { + spx_word16_t *sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,min_sinc_table_length*sizeof(spx_word16_t)); + if (!sinc_table) + goto fail; + + st->sinc_table = sinc_table; + st->sinc_table_length = min_sinc_table_length; + } + if (use_direct) { spx_uint32_t i; - if (st->sinc_table_length < st->filt_len*st->den_rate) - { - st->sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,st->filt_len*st->den_rate*sizeof(spx_word16_t)); - st->sinc_table_length = st->filt_len*st->den_rate; - } for (i=0;i<st->den_rate;i++) { spx_int32_t j; for (j=0;j<st->filt_len;j++) { 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); } } @@ -630,44 +676,41 @@ static void update_filter(SpeexResampler if (st->quality>8) st->resampler_ptr = resampler_basic_direct_double; else st->resampler_ptr = resampler_basic_direct_single; #endif /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff);*/ } else { spx_int32_t i; - if (st->sinc_table_length < st->filt_len*st->oversample+8) - { - st->sinc_table = (spx_word16_t *)speex_realloc(st->sinc_table,(st->filt_len*st->oversample+8)*sizeof(spx_word16_t)); - st->sinc_table_length = st->filt_len*st->oversample+8; - } for (i=-4;i<(spx_int32_t)(st->oversample*st->filt_len+4);i++) 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); #ifdef FIXED_POINT st->resampler_ptr = resampler_basic_interpolate_single; #else if (st->quality>8) st->resampler_ptr = resampler_basic_interpolate_double; else st->resampler_ptr = resampler_basic_interpolate_single; #endif /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff);*/ } - st->int_advance = st->num_rate/st->den_rate; - st->frac_advance = st->num_rate%st->den_rate; /* Here's the place where we update the filter memory to take into account the change in filter length. It's probably the messiest part of the code due to handling of lots of corner cases. */ min_alloc_size = st->filt_len-1 + st->buffer_size; if (min_alloc_size > st->mem_alloc_size) { - st->mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*min_alloc_size * sizeof(spx_word16_t)); + spx_word16_t *mem = (spx_word16_t*)speex_realloc(st->mem, st->nb_channels*min_alloc_size * sizeof(spx_word16_t)); + if (!mem) + goto fail; + + st->mem = mem; st->mem_alloc_size = min_alloc_size; } if (!st->started) { spx_uint32_t i; for (i=0;i<st->nb_channels*st->mem_alloc_size;i++) st->mem[i] = 0; /*speex_warning("reinit filter");*/ @@ -722,28 +765,38 @@ static void update_filter(SpeexResampler st->magic_samples[i] = (old_length - st->filt_len)/2; /* We must copy some of the memory that's no longer used */ /* Copy data going backward */ for (j=0;j<st->filt_len-1+st->magic_samples[i]+old_magic;j++) st->mem[i*st->mem_alloc_size+j] = st->mem[i*st->mem_alloc_size+j+st->magic_samples[i]]; st->magic_samples[i] += old_magic; } } + return RESAMPLER_ERR_SUCCESS; +fail: + st->resampler_ptr = resampler_basic_zero; + /* st->mem may still contain consumed input samples for the filter. + Restore filt_len so that filt_len - 1 still points to the position after + the last of these samples. */ + st->filt_len = old_length; + return RESAMPLER_ERR_ALLOC_FAILED; } EXPORT SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err) { return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality, err); } 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) { spx_uint32_t i; SpeexResamplerState *st; + int filter_err; + if (quality > 10 || quality < 0) { if (err) *err = RESAMPLER_ERR_INVALID_ARG; return NULL; } st = (SpeexResamplerState *)speex_alloc(sizeof(SpeexResamplerState)); st->initialised = 0; @@ -775,22 +828,26 @@ EXPORT SpeexResamplerState *speex_resamp st->last_sample[i] = 0; st->magic_samples[i] = 0; st->samp_frac_num[i] = 0; } speex_resampler_set_quality(st, quality); speex_resampler_set_rate_frac(st, ratio_num, ratio_den, in_rate, out_rate); - - update_filter(st); - - st->initialised = 1; + filter_err = update_filter(st); + if (filter_err == RESAMPLER_ERR_SUCCESS) + { + st->initialised = 1; + } else { + speex_resampler_destroy(st); + st = NULL; + } if (err) - *err = RESAMPLER_ERR_SUCCESS; + *err = filter_err; return st; } EXPORT void speex_resampler_destroy(SpeexResamplerState *st) { speex_free(st->mem); speex_free(st->sinc_table); @@ -879,17 +936,17 @@ EXPORT int speex_resampler_process_float olen -= ochunk; out += ochunk * st->out_stride; if (in) in += ichunk * istride; } } *in_len -= ilen; *out_len -= olen; - return RESAMPLER_ERR_SUCCESS; + return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; } #ifdef FIXED_POINT 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) #else 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) #endif { @@ -953,17 +1010,17 @@ EXPORT int speex_resampler_process_int(S out += (ochunk+omagic) * ostride_save; if (in) in += ichunk * istride_save; } st->out_stride = ostride_save; *in_len -= ilen; *out_len -= olen; - return RESAMPLER_ERR_SUCCESS; + return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; } EXPORT int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len) { spx_uint32_t i; int istride_save, ostride_save; spx_uint32_t bak_out_len = *out_len; spx_uint32_t bak_in_len = *in_len; @@ -976,17 +1033,17 @@ EXPORT int speex_resampler_process_inter *in_len = bak_in_len; if (in != NULL) speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len); else speex_resampler_process_float(st, i, NULL, in_len, out+i, out_len); } st->in_stride = istride_save; st->out_stride = ostride_save; - return RESAMPLER_ERR_SUCCESS; + return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; } 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) { spx_uint32_t i; int istride_save, ostride_save; spx_uint32_t bak_out_len = *out_len; spx_uint32_t bak_in_len = *in_len; @@ -999,17 +1056,17 @@ EXPORT int speex_resampler_process_inter *in_len = bak_in_len; if (in != NULL) speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len); else speex_resampler_process_int(st, i, NULL, in_len, out+i, out_len); } st->in_stride = istride_save; st->out_stride = ostride_save; - return RESAMPLER_ERR_SUCCESS; + return st->resampler_ptr == resampler_basic_zero ? RESAMPLER_ERR_ALLOC_FAILED : RESAMPLER_ERR_SUCCESS; } EXPORT int speex_resampler_set_rate(SpeexResamplerState *st, spx_uint32_t in_rate, spx_uint32_t out_rate) { return speex_resampler_set_rate_frac(st, in_rate, out_rate, in_rate, out_rate); } EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, spx_uint32_t *out_rate) @@ -1048,17 +1105,17 @@ EXPORT int speex_resampler_set_rate_frac st->samp_frac_num[i]=st->samp_frac_num[i]*st->den_rate/old_den; /* Safety net */ if (st->samp_frac_num[i] >= st->den_rate) st->samp_frac_num[i] = st->den_rate-1; } } if (st->initialised) - update_filter(st); + return update_filter(st); return RESAMPLER_ERR_SUCCESS; } EXPORT void speex_resampler_get_ratio(SpeexResamplerState *st, spx_uint32_t *ratio_num, spx_uint32_t *ratio_den) { *ratio_num = st->num_rate; *ratio_den = st->den_rate; } @@ -1066,17 +1123,17 @@ EXPORT void speex_resampler_get_ratio(Sp EXPORT int speex_resampler_set_quality(SpeexResamplerState *st, int quality) { if (quality > 10 || quality < 0) return RESAMPLER_ERR_INVALID_ARG; if (st->quality == quality) return RESAMPLER_ERR_SUCCESS; st->quality = quality; if (st->initialised) - update_filter(st); + return update_filter(st); return RESAMPLER_ERR_SUCCESS; } EXPORT void speex_resampler_get_quality(SpeexResamplerState *st, int *quality) { *quality = st->quality; }