Chris@69: /* Copyright (c) 2007-2008 CSIRO Chris@69: Copyright (c) 2007-2009 Xiph.Org Foundation Chris@69: Copyright (c) 2008 Gregory Maxwell Chris@69: Written by Jean-Marc Valin and Gregory Maxwell */ Chris@69: /* Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: Chris@69: - Redistributions of source code must retain the above copyright Chris@69: notice, this list of conditions and the following disclaimer. Chris@69: Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@69: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@69: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@69: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER Chris@69: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@69: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@69: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@69: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@69: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@69: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@69: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@69: */ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "celt.h" Chris@69: #include "modes.h" Chris@69: #include "rate.h" Chris@69: #include "os_support.h" Chris@69: #include "stack_alloc.h" Chris@69: #include "quant_bands.h" Chris@69: #include "cpu_support.h" Chris@69: Chris@69: static const opus_int16 eband5ms[] = { Chris@69: /*0 200 400 600 800 1k 1.2 1.4 1.6 2k 2.4 2.8 3.2 4k 4.8 5.6 6.8 8k 9.6 12k 15.6 */ Chris@69: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 Chris@69: }; Chris@69: Chris@69: /* Alternate tuning (partially derived from Vorbis) */ Chris@69: #define BITALLOC_SIZE 11 Chris@69: /* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */ Chris@69: static const unsigned char band_allocation[] = { Chris@69: /*0 200 400 600 800 1k 1.2 1.4 1.6 2k 2.4 2.8 3.2 4k 4.8 5.6 6.8 8k 9.6 12k 15.6 */ Chris@69: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Chris@69: 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0, Chris@69: 110,100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0, Chris@69: 118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0, Chris@69: 126,119,112,104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0, Chris@69: 134,127,120,114,103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1, Chris@69: 144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1, Chris@69: 152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1, Chris@69: 162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1, Chris@69: 172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20, Chris@69: 200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,129,104, Chris@69: }; Chris@69: Chris@69: #ifndef CUSTOM_MODES_ONLY Chris@69: #ifdef FIXED_POINT Chris@69: #include "static_modes_fixed.h" Chris@69: #else Chris@69: #include "static_modes_float.h" Chris@69: #endif Chris@69: #endif /* CUSTOM_MODES_ONLY */ Chris@69: Chris@69: #ifndef M_PI Chris@69: #define M_PI 3.141592653 Chris@69: #endif Chris@69: Chris@69: #ifdef CUSTOM_MODES Chris@69: Chris@69: /* Defining 25 critical bands for the full 0-20 kHz audio bandwidth Chris@69: Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */ Chris@69: #define BARK_BANDS 25 Chris@69: static const opus_int16 bark_freq[BARK_BANDS+1] = { Chris@69: 0, 100, 200, 300, 400, Chris@69: 510, 630, 770, 920, 1080, Chris@69: 1270, 1480, 1720, 2000, 2320, Chris@69: 2700, 3150, 3700, 4400, 5300, Chris@69: 6400, 7700, 9500, 12000, 15500, Chris@69: 20000}; Chris@69: Chris@69: static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *nbEBands) Chris@69: { Chris@69: opus_int16 *eBands; Chris@69: int i, j, lin, low, high, nBark, offset=0; Chris@69: Chris@69: /* All modes that have 2.5 ms short blocks use the same definition */ Chris@69: if (Fs == 400*(opus_int32)frame_size) Chris@69: { Chris@69: *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; Chris@69: eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+1)); Chris@69: for (i=0;i<*nbEBands+1;i++) Chris@69: eBands[i] = eband5ms[i]; Chris@69: return eBands; Chris@69: } Chris@69: /* Find the number of critical bands supported by our sampling rate */ Chris@69: for (nBark=1;nBark= Fs) Chris@69: break; Chris@69: Chris@69: /* Find where the linear part ends (i.e. where the spacing is more than min_width */ Chris@69: for (lin=0;lin= res) Chris@69: break; Chris@69: Chris@69: low = (bark_freq[lin]+res/2)/res; Chris@69: high = nBark-lin; Chris@69: *nbEBands = low+high; Chris@69: eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2)); Chris@69: Chris@69: if (eBands==NULL) Chris@69: return NULL; Chris@69: Chris@69: /* Linear spacing (min_width) */ Chris@69: for (i=0;i0) Chris@69: offset = eBands[low-1]*res - bark_freq[lin-1]; Chris@69: /* Spacing follows critical bands */ Chris@69: for (i=0;i frame_size) Chris@69: eBands[*nbEBands] = frame_size; Chris@69: for (i=1;i<*nbEBands-1;i++) Chris@69: { Chris@69: if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1]) Chris@69: { Chris@69: eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2; Chris@69: } Chris@69: } Chris@69: /* Remove any empty bands. */ Chris@69: for (i=j=0;i<*nbEBands;i++) Chris@69: if(eBands[i+1]>eBands[j]) Chris@69: eBands[++j]=eBands[i+1]; Chris@69: *nbEBands=j; Chris@69: Chris@69: for (i=1;i<*nbEBands;i++) Chris@69: { Chris@69: /* Every band must be smaller than the last band. */ Chris@69: celt_assert(eBands[i]-eBands[i-1]<=eBands[*nbEBands]-eBands[*nbEBands-1]); Chris@69: /* Each band must be no larger than twice the size of the previous one. */ Chris@69: celt_assert(eBands[i+1]-eBands[i]<=2*(eBands[i]-eBands[i-1])); Chris@69: } Chris@69: Chris@69: return eBands; Chris@69: } Chris@69: Chris@69: static void compute_allocation_table(CELTMode *mode) Chris@69: { Chris@69: int i, j; Chris@69: unsigned char *allocVectors; Chris@69: int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; Chris@69: Chris@69: mode->nbAllocVectors = BITALLOC_SIZE; Chris@69: allocVectors = opus_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands)); Chris@69: if (allocVectors==NULL) Chris@69: return; Chris@69: Chris@69: /* Check for standard mode */ Chris@69: if (mode->Fs == 400*(opus_int32)mode->shortMdctSize) Chris@69: { Chris@69: for (i=0;inbEBands;i++) Chris@69: allocVectors[i] = band_allocation[i]; Chris@69: mode->allocVectors = allocVectors; Chris@69: return; Chris@69: } Chris@69: /* If not the standard mode, interpolate */ Chris@69: /* Compute per-codec-band allocation from per-critical-band matrix */ Chris@69: for (i=0;inbEBands;j++) Chris@69: { Chris@69: int k; Chris@69: for (k=0;k mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize) Chris@69: break; Chris@69: } Chris@69: if (k>maxBands-1) Chris@69: allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1]; Chris@69: else { Chris@69: opus_int32 a0, a1; Chris@69: a1 = mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize - 400*(opus_int32)eband5ms[k-1]; Chris@69: a0 = 400*(opus_int32)eband5ms[k] - mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize; Chris@69: allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1] Chris@69: + a1*band_allocation[i*maxBands+k])/(a0+a1); Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: /*printf ("\n"); Chris@69: for (i=0;inbEBands;j++) Chris@69: printf ("%d ", allocVectors[i*mode->nbEBands+j]); Chris@69: printf ("\n"); Chris@69: } Chris@69: exit(0);*/ Chris@69: Chris@69: mode->allocVectors = allocVectors; Chris@69: } Chris@69: Chris@69: #endif /* CUSTOM_MODES */ Chris@69: Chris@69: CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error) Chris@69: { Chris@69: int i; Chris@69: #ifdef CUSTOM_MODES Chris@69: CELTMode *mode=NULL; Chris@69: int res; Chris@69: opus_val16 *window; Chris@69: opus_int16 *logN; Chris@69: int LM; Chris@69: int arch = opus_select_arch(); Chris@69: ALLOC_STACK; Chris@69: #if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA) Chris@69: if (global_stack==NULL) Chris@69: goto failure; Chris@69: #endif Chris@69: #endif Chris@69: Chris@69: #ifndef CUSTOM_MODES_ONLY Chris@69: for (i=0;iFs && Chris@69: (frame_size<shortMdctSize*static_mode_list[i]->nbShortMdcts) Chris@69: { Chris@69: if (error) Chris@69: *error = OPUS_OK; Chris@69: return (CELTMode*)static_mode_list[i]; Chris@69: } Chris@69: } Chris@69: } Chris@69: #endif /* CUSTOM_MODES_ONLY */ Chris@69: Chris@69: #ifndef CUSTOM_MODES Chris@69: if (error) Chris@69: *error = OPUS_BAD_ARG; Chris@69: return NULL; Chris@69: #else Chris@69: Chris@69: /* The good thing here is that permutation of the arguments will automatically be invalid */ Chris@69: Chris@69: if (Fs < 8000 || Fs > 96000) Chris@69: { Chris@69: if (error) Chris@69: *error = OPUS_BAD_ARG; Chris@69: return NULL; Chris@69: } Chris@69: if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0) Chris@69: { Chris@69: if (error) Chris@69: *error = OPUS_BAD_ARG; Chris@69: return NULL; Chris@69: } Chris@69: /* Frames of less than 1ms are not supported. */ Chris@69: if ((opus_int32)frame_size*1000 < Fs) Chris@69: { Chris@69: if (error) Chris@69: *error = OPUS_BAD_ARG; Chris@69: return NULL; Chris@69: } Chris@69: Chris@69: if ((opus_int32)frame_size*75 >= Fs && (frame_size%16)==0) Chris@69: { Chris@69: LM = 3; Chris@69: } else if ((opus_int32)frame_size*150 >= Fs && (frame_size%8)==0) Chris@69: { Chris@69: LM = 2; Chris@69: } else if ((opus_int32)frame_size*300 >= Fs && (frame_size%4)==0) Chris@69: { Chris@69: LM = 1; Chris@69: } else Chris@69: { Chris@69: LM = 0; Chris@69: } Chris@69: Chris@69: /* Shorts longer than 3.3ms are not supported. */ Chris@69: if ((opus_int32)(frame_size>>LM)*300 > Fs) Chris@69: { Chris@69: if (error) Chris@69: *error = OPUS_BAD_ARG; Chris@69: return NULL; Chris@69: } Chris@69: Chris@69: mode = opus_alloc(sizeof(CELTMode)); Chris@69: if (mode==NULL) Chris@69: goto failure; Chris@69: mode->Fs = Fs; Chris@69: Chris@69: /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis Chris@69: is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should Chris@69: approximate that. */ Chris@69: if(Fs < 12000) /* 8 kHz */ Chris@69: { Chris@69: mode->preemph[0] = QCONST16(0.3500061035f, 15); Chris@69: mode->preemph[1] = -QCONST16(0.1799926758f, 15); Chris@69: mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */ Chris@69: mode->preemph[3] = QCONST16(3.6765136719f, 13); Chris@69: } else if(Fs < 24000) /* 16 kHz */ Chris@69: { Chris@69: mode->preemph[0] = QCONST16(0.6000061035f, 15); Chris@69: mode->preemph[1] = -QCONST16(0.1799926758f, 15); Chris@69: mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */ Chris@69: mode->preemph[3] = QCONST16(2.2598876953f, 13); Chris@69: } else if(Fs < 40000) /* 32 kHz */ Chris@69: { Chris@69: mode->preemph[0] = QCONST16(0.7799987793f, 15); Chris@69: mode->preemph[1] = -QCONST16(0.1000061035f, 15); Chris@69: mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */ Chris@69: mode->preemph[3] = QCONST16(1.3333740234f, 13); Chris@69: } else /* 48 kHz */ Chris@69: { Chris@69: mode->preemph[0] = QCONST16(0.8500061035f, 15); Chris@69: mode->preemph[1] = QCONST16(0.0f, 15); Chris@69: mode->preemph[2] = QCONST16(1.f, SIG_SHIFT); Chris@69: mode->preemph[3] = QCONST16(1.f, 13); Chris@69: } Chris@69: Chris@69: mode->maxLM = LM; Chris@69: mode->nbShortMdcts = 1<shortMdctSize = frame_size/mode->nbShortMdcts; Chris@69: res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize); Chris@69: Chris@69: mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands); Chris@69: if (mode->eBands==NULL) Chris@69: goto failure; Chris@69: #if !defined(SMALL_FOOTPRINT) Chris@69: /* Make sure we don't allocate a band larger than our PVQ table. Chris@69: 208 should be enough, but let's be paranoid. */ Chris@69: if ((mode->eBands[mode->nbEBands] - mode->eBands[mode->nbEBands-1])< Chris@69: 208) { Chris@69: goto failure; Chris@69: } Chris@69: #endif Chris@69: Chris@69: mode->effEBands = mode->nbEBands; Chris@69: while (mode->eBands[mode->effEBands] > mode->shortMdctSize) Chris@69: mode->effEBands--; Chris@69: Chris@69: /* Overlap must be divisible by 4 */ Chris@69: mode->overlap = ((mode->shortMdctSize>>2)<<2); Chris@69: Chris@69: compute_allocation_table(mode); Chris@69: if (mode->allocVectors==NULL) Chris@69: goto failure; Chris@69: Chris@69: window = (opus_val16*)opus_alloc(mode->overlap*sizeof(opus_val16)); Chris@69: if (window==NULL) Chris@69: goto failure; Chris@69: Chris@69: #ifndef FIXED_POINT Chris@69: for (i=0;ioverlap;i++) Chris@69: window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap)); Chris@69: #else Chris@69: for (i=0;ioverlap;i++) Chris@69: window[i] = MIN32(32767,floor(.5+32768.*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap)))); Chris@69: #endif Chris@69: mode->window = window; Chris@69: Chris@69: logN = (opus_int16*)opus_alloc(mode->nbEBands*sizeof(opus_int16)); Chris@69: if (logN==NULL) Chris@69: goto failure; Chris@69: Chris@69: for (i=0;inbEBands;i++) Chris@69: logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES); Chris@69: mode->logN = logN; Chris@69: Chris@69: compute_pulse_cache(mode, mode->maxLM); Chris@69: Chris@69: if (clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, Chris@69: mode->maxLM, arch) == 0) Chris@69: goto failure; Chris@69: Chris@69: if (error) Chris@69: *error = OPUS_OK; Chris@69: Chris@69: return mode; Chris@69: failure: Chris@69: if (error) Chris@69: *error = OPUS_ALLOC_FAIL; Chris@69: if (mode!=NULL) Chris@69: opus_custom_mode_destroy(mode); Chris@69: return NULL; Chris@69: #endif /* !CUSTOM_MODES */ Chris@69: } Chris@69: Chris@69: #ifdef CUSTOM_MODES Chris@69: void opus_custom_mode_destroy(CELTMode *mode) Chris@69: { Chris@69: int arch = opus_select_arch(); Chris@69: Chris@69: if (mode == NULL) Chris@69: return; Chris@69: #ifndef CUSTOM_MODES_ONLY Chris@69: { Chris@69: int i; Chris@69: for (i=0;ieBands); Chris@69: opus_free((unsigned char*)mode->allocVectors); Chris@69: Chris@69: opus_free((opus_val16*)mode->window); Chris@69: opus_free((opus_int16*)mode->logN); Chris@69: Chris@69: opus_free((opus_int16*)mode->cache.index); Chris@69: opus_free((unsigned char*)mode->cache.bits); Chris@69: opus_free((unsigned char*)mode->cache.caps); Chris@69: clt_mdct_clear(&mode->mdct, arch); Chris@69: Chris@69: opus_free((CELTMode *)mode); Chris@69: } Chris@69: #endif