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