15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Copyright (c) 2007-2008 CSIRO 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Copyright (c) 2007-2009 Xiph.Org Foundation 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Copyright (c) 2008 Gregory Maxwell 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Written by Jean-Marc Valin and Gregory Maxwell */ 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Redistribution and use in source and binary forms, with or without 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) modification, are permitted provided that the following conditions 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) are met: 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) - Redistributions of source code must retain the above copyright 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notice, this list of conditions and the following disclaimer. 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) - Redistributions in binary form must reproduce the above copyright 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notice, this list of conditions and the following disclaimer in the 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) documentation and/or other materials provided with the distribution. 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_CONFIG_H 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "config.h" 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "celt.h" 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "modes.h" 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "rate.h" 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "os_support.h" 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "stack_alloc.h" 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "quant_bands.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const opus_int16 eband5ms[] = { 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*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 */ 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Alternate tuning (partially derived from Vorbis) */ 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BITALLOC_SIZE 11 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */ 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const unsigned char band_allocation[] = { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*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 */ 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0, 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)110,100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0, 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0, 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)126,119,112,104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0, 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)134,127,120,114,103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1, 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1, 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1, 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1, 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20, 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,129,104, 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CUSTOM_MODES_ONLY 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) #ifdef FIXED_POINT 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) #include "static_modes_fixed.h" 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) #else 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) #include "static_modes_float.h" 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) #endif 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CUSTOM_MODES_ONLY */ 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef M_PI 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define M_PI 3.141592653 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CUSTOM_MODES 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Defining 25 critical bands for the full 0-20 kHz audio bandwidth 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */ 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BARK_BANDS 25 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const opus_int16 bark_freq[BARK_BANDS+1] = { 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 0, 100, 200, 300, 400, 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 510, 630, 770, 920, 1080, 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1270, 1480, 1720, 2000, 2320, 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2700, 3150, 3700, 4400, 5300, 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6400, 7700, 9500, 12000, 15500, 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 20000}; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *nbEBands) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) opus_int16 *eBands; 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int i, j, lin, low, high, nBark, offset=0; 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* All modes that have 2.5 ms short blocks use the same definition */ 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Fs == 400*(opus_int32)frame_size) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+1)); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (i=0;i<*nbEBands+1;i++) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) eBands[i] = eband5ms[i]; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return eBands; 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Find the number of critical bands supported by our sampling rate */ 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (nBark=1;nBark<BARK_BANDS;nBark++) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (bark_freq[nBark+1]*2 >= Fs) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Find where the linear part ends (i.e. where the spacing is more than min_width */ 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (lin=0;lin<nBark;lin++) 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (bark_freq[lin+1]-bark_freq[lin] >= res) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) low = (bark_freq[lin]+res/2)/res; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) high = nBark-lin; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *nbEBands = low+high; 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2)); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (eBands==NULL) 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return NULL; 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Linear spacing (min_width) */ 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (i=0;i<low;i++) 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) eBands[i] = i; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (low>0) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset = eBands[low-1]*res - bark_freq[lin-1]; 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Spacing follows critical bands */ 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (i=0;i<high;i++) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) { 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int target = bark_freq[lin+i]; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Round to an even value */ 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) eBands[i+low] = (target+offset/2+res)/(2*res)*2; 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) offset = eBands[i+low]*res - target; 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Enforce the minimum spacing at the boundary */ 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (i=0;i<*nbEBands;i++) 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (eBands[i] < i) 137 eBands[i] = i; 138 /* Round to an even value */ 139 eBands[*nbEBands] = (bark_freq[nBark]+res)/(2*res)*2; 140 if (eBands[*nbEBands] > frame_size) 141 eBands[*nbEBands] = frame_size; 142 for (i=1;i<*nbEBands-1;i++) 143 { 144 if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1]) 145 { 146 eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2; 147 } 148 } 149 /* Remove any empty bands. */ 150 for (i=j=0;i<*nbEBands;i++) 151 if(eBands[i+1]>eBands[j]) 152 eBands[++j]=eBands[i+1]; 153 *nbEBands=j; 154 155 for (i=1;i<*nbEBands;i++) 156 { 157 /* Every band must be smaller than the last band. */ 158 celt_assert(eBands[i]-eBands[i-1]<=eBands[*nbEBands]-eBands[*nbEBands-1]); 159 /* Each band must be no larger than twice the size of the previous one. */ 160 celt_assert(eBands[i+1]-eBands[i]<=2*(eBands[i]-eBands[i-1])); 161 } 162 163 return eBands; 164} 165 166static void compute_allocation_table(CELTMode *mode) 167{ 168 int i, j; 169 unsigned char *allocVectors; 170 int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; 171 172 mode->nbAllocVectors = BITALLOC_SIZE; 173 allocVectors = opus_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands)); 174 if (allocVectors==NULL) 175 return; 176 177 /* Check for standard mode */ 178 if (mode->Fs == 400*(opus_int32)mode->shortMdctSize) 179 { 180 for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++) 181 allocVectors[i] = band_allocation[i]; 182 mode->allocVectors = allocVectors; 183 return; 184 } 185 /* If not the standard mode, interpolate */ 186 /* Compute per-codec-band allocation from per-critical-band matrix */ 187 for (i=0;i<BITALLOC_SIZE;i++) 188 { 189 for (j=0;j<mode->nbEBands;j++) 190 { 191 int k; 192 for (k=0;k<maxBands;k++) 193 { 194 if (400*(opus_int32)eband5ms[k] > mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize) 195 break; 196 } 197 if (k>maxBands-1) 198 allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1]; 199 else { 200 opus_int32 a0, a1; 201 a1 = mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize - 400*(opus_int32)eband5ms[k-1]; 202 a0 = 400*(opus_int32)eband5ms[k] - mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize; 203 allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1] 204 + a1*band_allocation[i*maxBands+k])/(a0+a1); 205 } 206 } 207 } 208 209 /*printf ("\n"); 210 for (i=0;i<BITALLOC_SIZE;i++) 211 { 212 for (j=0;j<mode->nbEBands;j++) 213 printf ("%d ", allocVectors[i*mode->nbEBands+j]); 214 printf ("\n"); 215 } 216 exit(0);*/ 217 218 mode->allocVectors = allocVectors; 219} 220 221#endif /* CUSTOM_MODES */ 222 223CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error) 224{ 225 int i; 226#ifdef CUSTOM_MODES 227 CELTMode *mode=NULL; 228 int res; 229 opus_val16 *window; 230 opus_int16 *logN; 231 int LM; 232 ALLOC_STACK; 233#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA) 234 if (global_stack==NULL) 235 goto failure; 236#endif 237#endif 238 239#ifndef CUSTOM_MODES_ONLY 240 for (i=0;i<TOTAL_MODES;i++) 241 { 242 int j; 243 for (j=0;j<4;j++) 244 { 245 if (Fs == static_mode_list[i]->Fs && 246 (frame_size<<j) == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts) 247 { 248 if (error) 249 *error = OPUS_OK; 250 return (CELTMode*)static_mode_list[i]; 251 } 252 } 253 } 254#endif /* CUSTOM_MODES_ONLY */ 255 256#ifndef CUSTOM_MODES 257 if (error) 258 *error = OPUS_BAD_ARG; 259 return NULL; 260#else 261 262 /* The good thing here is that permutation of the arguments will automatically be invalid */ 263 264 if (Fs < 8000 || Fs > 96000) 265 { 266 if (error) 267 *error = OPUS_BAD_ARG; 268 return NULL; 269 } 270 if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0) 271 { 272 if (error) 273 *error = OPUS_BAD_ARG; 274 return NULL; 275 } 276 /* Frames of less than 1ms are not supported. */ 277 if ((opus_int32)frame_size*1000 < Fs) 278 { 279 if (error) 280 *error = OPUS_BAD_ARG; 281 return NULL; 282 } 283 284 if ((opus_int32)frame_size*75 >= Fs && (frame_size%16)==0) 285 { 286 LM = 3; 287 } else if ((opus_int32)frame_size*150 >= Fs && (frame_size%8)==0) 288 { 289 LM = 2; 290 } else if ((opus_int32)frame_size*300 >= Fs && (frame_size%4)==0) 291 { 292 LM = 1; 293 } else 294 { 295 LM = 0; 296 } 297 298 /* Shorts longer than 3.3ms are not supported. */ 299 if ((opus_int32)(frame_size>>LM)*300 > Fs) 300 { 301 if (error) 302 *error = OPUS_BAD_ARG; 303 return NULL; 304 } 305 306 mode = opus_alloc(sizeof(CELTMode)); 307 if (mode==NULL) 308 goto failure; 309 mode->Fs = Fs; 310 311 /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis 312 is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should 313 approximate that. */ 314 if(Fs < 12000) /* 8 kHz */ 315 { 316 mode->preemph[0] = QCONST16(0.3500061035f, 15); 317 mode->preemph[1] = -QCONST16(0.1799926758f, 15); 318 mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */ 319 mode->preemph[3] = QCONST16(3.6765136719f, 13); 320 } else if(Fs < 24000) /* 16 kHz */ 321 { 322 mode->preemph[0] = QCONST16(0.6000061035f, 15); 323 mode->preemph[1] = -QCONST16(0.1799926758f, 15); 324 mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */ 325 mode->preemph[3] = QCONST16(2.2598876953f, 13); 326 } else if(Fs < 40000) /* 32 kHz */ 327 { 328 mode->preemph[0] = QCONST16(0.7799987793f, 15); 329 mode->preemph[1] = -QCONST16(0.1000061035f, 15); 330 mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */ 331 mode->preemph[3] = QCONST16(1.3333740234f, 13); 332 } else /* 48 kHz */ 333 { 334 mode->preemph[0] = QCONST16(0.8500061035f, 15); 335 mode->preemph[1] = QCONST16(0.0f, 15); 336 mode->preemph[2] = QCONST16(1.f, SIG_SHIFT); 337 mode->preemph[3] = QCONST16(1.f, 13); 338 } 339 340 mode->maxLM = LM; 341 mode->nbShortMdcts = 1<<LM; 342 mode->shortMdctSize = frame_size/mode->nbShortMdcts; 343 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize); 344 345 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands); 346 if (mode->eBands==NULL) 347 goto failure; 348#if !defined(SMALL_FOOTPRINT) 349 /* Make sure we don't allocate a band larger than our PVQ table. 350 208 should be enough, but let's be paranoid. */ 351 if ((mode->eBands[mode->nbEBands] - mode->eBands[mode->nbEBands-1])<<LM > 352 208) { 353 goto failure; 354 } 355#endif 356 357 mode->effEBands = mode->nbEBands; 358 while (mode->eBands[mode->effEBands] > mode->shortMdctSize) 359 mode->effEBands--; 360 361 /* Overlap must be divisible by 4 */ 362 mode->overlap = ((mode->shortMdctSize>>2)<<2); 363 364 compute_allocation_table(mode); 365 if (mode->allocVectors==NULL) 366 goto failure; 367 368 window = (opus_val16*)opus_alloc(mode->overlap*sizeof(opus_val16)); 369 if (window==NULL) 370 goto failure; 371 372#ifndef FIXED_POINT 373 for (i=0;i<mode->overlap;i++) 374 window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap)); 375#else 376 for (i=0;i<mode->overlap;i++) 377 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)))); 378#endif 379 mode->window = window; 380 381 logN = (opus_int16*)opus_alloc(mode->nbEBands*sizeof(opus_int16)); 382 if (logN==NULL) 383 goto failure; 384 385 for (i=0;i<mode->nbEBands;i++) 386 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES); 387 mode->logN = logN; 388 389 compute_pulse_cache(mode, mode->maxLM); 390 391 if (clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, 392 mode->maxLM) == 0) 393 goto failure; 394 395 if (error) 396 *error = OPUS_OK; 397 398 return mode; 399failure: 400 if (error) 401 *error = OPUS_ALLOC_FAIL; 402 if (mode!=NULL) 403 opus_custom_mode_destroy(mode); 404 return NULL; 405#endif /* !CUSTOM_MODES */ 406} 407 408#ifdef CUSTOM_MODES 409void opus_custom_mode_destroy(CELTMode *mode) 410{ 411 if (mode == NULL) 412 return; 413#ifndef CUSTOM_MODES_ONLY 414 { 415 int i; 416 for (i=0;i<TOTAL_MODES;i++) 417 { 418 if (mode == static_mode_list[i]) 419 { 420 return; 421 } 422 } 423 } 424#endif /* CUSTOM_MODES_ONLY */ 425 opus_free((opus_int16*)mode->eBands); 426 opus_free((opus_int16*)mode->allocVectors); 427 428 opus_free((opus_val16*)mode->window); 429 opus_free((opus_int16*)mode->logN); 430 431 opus_free((opus_int16*)mode->cache.index); 432 opus_free((unsigned char*)mode->cache.bits); 433 opus_free((unsigned char*)mode->cache.caps); 434 clt_mdct_clear(&mode->mdct); 435 436 opus_free((CELTMode *)mode); 437} 438#endif 439