Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
| 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
| 3 | Copyright (c) 2008 Gregory Maxwell |
| 4 | Written by Jean-Marc Valin and Gregory Maxwell */ |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 5 | /* |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions |
| 8 | are met: |
| 9 | |
| 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | |
| 13 | - Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the |
| 15 | documentation and/or other materials provided with the distribution. |
| 16 | |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include "config.h" |
| 32 | #endif |
| 33 | |
Jean-Marc Valin | 65d57e6 | 2008-02-18 15:49:37 +1100 | [diff] [blame] | 34 | #include "celt.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 35 | #include "modes.h" |
Jean-Marc Valin | 472a5f0 | 2008-02-19 13:12:32 +1100 | [diff] [blame] | 36 | #include "rate.h" |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 37 | #include "os_support.h" |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame] | 38 | #include "stack_alloc.h" |
Jean-Marc Valin | 4ce9205 | 2008-04-23 13:42:10 +1000 | [diff] [blame] | 39 | #include "quant_bands.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 40 | |
Jean-Marc Valin | e0c2545 | 2010-09-03 11:52:38 -0400 | [diff] [blame] | 41 | static const celt_int16 eband5ms[] = { |
Jean-Marc Valin | 9cc56bf | 2011-02-01 22:03:26 -0500 | [diff] [blame] | 42 | /*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 */ |
Jean-Marc Valin | 8ccda88 | 2010-10-04 18:01:45 -0400 | [diff] [blame] | 43 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 |
Jean-Marc Valin | e0c2545 | 2010-09-03 11:52:38 -0400 | [diff] [blame] | 44 | }; |
| 45 | |
Jean-Marc Valin | 54d84c0 | 2010-11-19 11:45:37 -0500 | [diff] [blame] | 46 | /* Alternate tuning (partially derived from Vorbis) */ |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 47 | #define BITALLOC_SIZE 11 |
Jean-Marc Valin | 9449165 | 2010-10-21 17:10:24 -0400 | [diff] [blame] | 48 | /* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */ |
| 49 | static const unsigned char band_allocation[] = { |
Jean-Marc Valin | 54d84c0 | 2010-11-19 11:45:37 -0500 | [diff] [blame] | 50 | /*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 */ |
Jean-Marc Valin | 9449165 | 2010-10-21 17:10:24 -0400 | [diff] [blame] | 51 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Jean-Marc Valin | c992a0d | 2010-11-19 14:06:10 -0500 | [diff] [blame] | 52 | 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0, |
Jean-Marc Valin | 54d84c0 | 2010-11-19 11:45:37 -0500 | [diff] [blame] | 53 | 110,100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0, |
Jean-Marc Valin | c992a0d | 2010-11-19 14:06:10 -0500 | [diff] [blame] | 54 | 118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0, |
| 55 | 126,119,112,104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0, |
Jean-Marc Valin | 54d84c0 | 2010-11-19 11:45:37 -0500 | [diff] [blame] | 56 | 134,127,120,114,103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1, |
Jean-Marc Valin | dd4bf63 | 2010-11-30 15:31:45 -0500 | [diff] [blame] | 57 | 144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1, |
| 58 | 152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1, |
| 59 | 162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1, |
| 60 | 172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20, |
Timothy B. Terriberry | 89039a3 | 2011-01-30 22:27:13 -0800 | [diff] [blame] | 61 | 200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,129,104, |
Jean-Marc Valin | 9449165 | 2010-10-21 17:10:24 -0400 | [diff] [blame] | 62 | }; |
Jean-Marc Valin | 78ea9fd | 2010-09-24 08:27:28 -0400 | [diff] [blame] | 63 | |
Jean-Marc Valin | 665da0b | 2011-01-30 12:15:12 -0500 | [diff] [blame] | 64 | #ifndef CUSTOM_MODES_ONLY |
| 65 | #ifdef FIXED_POINT |
| 66 | #include "static_modes_fixed.c" |
| 67 | #else |
| 68 | #include "static_modes_float.c" |
| 69 | #endif |
| 70 | #endif /* CUSTOM_MODES_ONLY */ |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 71 | |
Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 72 | #ifndef M_PI |
| 73 | #define M_PI 3.141592653 |
| 74 | #endif |
| 75 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 76 | |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 77 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 78 | |
Jean-Marc Valin | 17683eb | 2008-02-18 21:45:19 +1100 | [diff] [blame] | 79 | /* Defining 25 critical bands for the full 0-20 kHz audio bandwidth |
| 80 | Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */ |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 81 | #define BARK_BANDS 25 |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 82 | static const celt_int16 bark_freq[BARK_BANDS+1] = { |
Jean-Marc Valin | 17683eb | 2008-02-18 21:45:19 +1100 | [diff] [blame] | 83 | 0, 100, 200, 300, 400, |
| 84 | 510, 630, 770, 920, 1080, |
| 85 | 1270, 1480, 1720, 2000, 2320, |
| 86 | 2700, 3150, 3700, 4400, 5300, |
| 87 | 6400, 7700, 9500, 12000, 15500, |
| 88 | 20000}; |
| 89 | |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 90 | static celt_int16 *compute_ebands(celt_int32 Fs, int frame_size, int res, int *nbEBands) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 91 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 92 | celt_int16 *eBands; |
Timothy B. Terriberry | cb8f366 | 2011-02-01 12:32:34 -0800 | [diff] [blame] | 93 | int i, j, lin, low, high, nBark, offset=0; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 94 | |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 95 | /* All modes that have 2.5 ms short blocks use the same definition */ |
| 96 | if (Fs == 400*(celt_int32)frame_size) |
Jean-Marc Valin | 20639c4 | 2010-05-19 16:10:12 -0400 | [diff] [blame] | 97 | { |
| 98 | *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 99 | eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+1)); |
| 100 | for (i=0;i<*nbEBands+1;i++) |
Jean-Marc Valin | 20639c4 | 2010-05-19 16:10:12 -0400 | [diff] [blame] | 101 | eBands[i] = eband5ms[i]; |
Jean-Marc Valin | 20639c4 | 2010-05-19 16:10:12 -0400 | [diff] [blame] | 102 | return eBands; |
| 103 | } |
Gregory Maxwell | cbaf67e | 2008-09-28 04:19:19 -0400 | [diff] [blame] | 104 | /* Find the number of critical bands supported by our sampling rate */ |
| 105 | for (nBark=1;nBark<BARK_BANDS;nBark++) |
| 106 | if (bark_freq[nBark+1]*2 >= Fs) |
| 107 | break; |
| 108 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 109 | /* Find where the linear part ends (i.e. where the spacing is more than min_width */ |
Gregory Maxwell | cbaf67e | 2008-09-28 04:19:19 -0400 | [diff] [blame] | 110 | for (lin=0;lin<nBark;lin++) |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 111 | if (bark_freq[lin+1]-bark_freq[lin] >= res) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 112 | break; |
Jean-Marc Valin | 4fb9568 | 2010-04-20 23:30:22 -0400 | [diff] [blame] | 113 | |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 114 | low = (bark_freq[lin]+res/2)/res; |
Gregory Maxwell | cbaf67e | 2008-09-28 04:19:19 -0400 | [diff] [blame] | 115 | high = nBark-lin; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 116 | *nbEBands = low+high; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 117 | eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2)); |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 118 | |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 119 | if (eBands==NULL) |
| 120 | return NULL; |
| 121 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 122 | /* Linear spacing (min_width) */ |
| 123 | for (i=0;i<low;i++) |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 124 | eBands[i] = i; |
Jean-Marc Valin | be8d125 | 2010-04-21 18:09:07 -0400 | [diff] [blame] | 125 | if (low>0) |
| 126 | offset = eBands[low-1]*res - bark_freq[lin-1]; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 127 | /* Spacing follows critical bands */ |
| 128 | for (i=0;i<high;i++) |
Jean-Marc Valin | 39f68ac | 2009-10-03 23:27:52 -0400 | [diff] [blame] | 129 | { |
| 130 | int target = bark_freq[lin+i]; |
Jean-Marc Valin | 4420390 | 2011-01-12 09:22:29 -0500 | [diff] [blame] | 131 | /* Round to an even value */ |
| 132 | eBands[i+low] = (target+offset/2+res)/(2*res)*2; |
Jean-Marc Valin | 39f68ac | 2009-10-03 23:27:52 -0400 | [diff] [blame] | 133 | offset = eBands[i+low]*res - target; |
| 134 | } |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 135 | /* Enforce the minimum spacing at the boundary */ |
| 136 | for (i=0;i<*nbEBands;i++) |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 137 | if (eBands[i] < i) |
| 138 | eBands[i] = i; |
Jean-Marc Valin | 4420390 | 2011-01-12 09:22:29 -0500 | [diff] [blame] | 139 | /* Round to an even value */ |
| 140 | eBands[*nbEBands] = (bark_freq[nBark]+res)/(2*res)*2; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 141 | if (eBands[*nbEBands] > frame_size) |
| 142 | eBands[*nbEBands] = frame_size; |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 143 | for (i=1;i<*nbEBands-1;i++) |
| 144 | { |
| 145 | if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1]) |
| 146 | { |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 147 | eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2; |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
Timothy B. Terriberry | cb8f366 | 2011-02-01 12:32:34 -0800 | [diff] [blame] | 150 | /* Remove any empty bands. */ |
| 151 | for (i=j=0;i<*nbEBands;i++) |
| 152 | if(eBands[i+1]>eBands[j]) |
| 153 | eBands[++j]=eBands[i+1]; |
| 154 | *nbEBands=j; |
| 155 | |
Timothy B. Terriberry | 2799c29 | 2011-02-01 12:53:05 -0800 | [diff] [blame] | 156 | for (i=1;i<*nbEBands;i++) |
| 157 | { |
| 158 | /* Every band must be smaller than the last band. */ |
| 159 | celt_assert(eBands[i]-eBands[i-1]<=eBands[*nbEBands]-eBands[*nbEBands-1]); |
| 160 | /* Each band must be no larger than twice the size of the previous one. */ |
| 161 | celt_assert(eBands[i+1]-eBands[i]<=2*(eBands[i]-eBands[i-1])); |
| 162 | } |
| 163 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 164 | return eBands; |
| 165 | } |
| 166 | |
Jean-Marc Valin | e3e2c26 | 2011-01-26 13:09:53 -0500 | [diff] [blame] | 167 | static void compute_allocation_table(CELTMode *mode) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 168 | { |
Jean-Marc Valin | 2f6c5fe | 2010-06-28 17:22:37 -0400 | [diff] [blame] | 169 | int i, j; |
Jean-Marc Valin | 01b54b9 | 2010-06-03 23:29:35 -0400 | [diff] [blame] | 170 | unsigned char *allocVectors; |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 171 | int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 172 | |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 173 | mode->nbAllocVectors = BITALLOC_SIZE; |
Jean-Marc Valin | 01b54b9 | 2010-06-03 23:29:35 -0400 | [diff] [blame] | 174 | allocVectors = celt_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands)); |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 175 | if (allocVectors==NULL) |
| 176 | return; |
| 177 | |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 178 | /* Check for standard mode */ |
Jean-Marc Valin | 8e31ab3 | 2010-12-16 16:45:35 -0500 | [diff] [blame] | 179 | if (mode->Fs == 400*(celt_int32)mode->shortMdctSize) |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 180 | { |
| 181 | for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++) |
Jean-Marc Valin | ffe5061 | 2010-06-04 00:13:19 -0400 | [diff] [blame] | 182 | allocVectors[i] = band_allocation[i]; |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 183 | mode->allocVectors = allocVectors; |
| 184 | return; |
| 185 | } |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 186 | /* If not the standard mode, interpolate */ |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 187 | /* Compute per-codec-band allocation from per-critical-band matrix */ |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 188 | for (i=0;i<BITALLOC_SIZE;i++) |
| 189 | { |
Jean-Marc Valin | 1bfa18c | 2010-12-01 16:11:38 -0500 | [diff] [blame] | 190 | for (j=0;j<mode->nbEBands;j++) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 191 | { |
Jean-Marc Valin | 1bfa18c | 2010-12-01 16:11:38 -0500 | [diff] [blame] | 192 | int k; |
| 193 | for (k=0;k<maxBands;k++) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 194 | { |
Jean-Marc Valin | 1bfa18c | 2010-12-01 16:11:38 -0500 | [diff] [blame] | 195 | if (400*(celt_int32)eband5ms[k] > mode->eBands[j]*(celt_int32)mode->Fs/mode->shortMdctSize) |
| 196 | break; |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 197 | } |
Jean-Marc Valin | fa74ae2 | 2011-02-11 21:56:11 -0500 | [diff] [blame] | 198 | if (k>maxBands-1) |
Jean-Marc Valin | 1bfa18c | 2010-12-01 16:11:38 -0500 | [diff] [blame] | 199 | allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1]; |
| 200 | else { |
| 201 | celt_int32 a0, a1; |
| 202 | a1 = mode->eBands[j]*(celt_int32)mode->Fs/mode->shortMdctSize - 400*(celt_int32)eband5ms[k-1]; |
| 203 | a0 = 400*(celt_int32)eband5ms[k] - mode->eBands[j]*(celt_int32)mode->Fs/mode->shortMdctSize; |
| 204 | allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1] |
| 205 | + a1*band_allocation[i*maxBands+k])/(a0+a1); |
| 206 | } |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 207 | } |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 208 | } |
Jean-Marc Valin | 1bfa18c | 2010-12-01 16:11:38 -0500 | [diff] [blame] | 209 | |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 210 | /*printf ("\n"); |
| 211 | for (i=0;i<BITALLOC_SIZE;i++) |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 212 | { |
| 213 | for (j=0;j<mode->nbEBands;j++) |
| 214 | printf ("%d ", allocVectors[i*mode->nbEBands+j]); |
| 215 | printf ("\n"); |
| 216 | } |
| 217 | exit(0);*/ |
| 218 | |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 219 | mode->allocVectors = allocVectors; |
Jean-Marc Valin | ad63719 | 2008-05-07 13:44:39 +1000 | [diff] [blame] | 220 | } |
| 221 | |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 222 | #endif /* CUSTOM_MODES */ |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 223 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 224 | CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 225 | { |
Jean-Marc Valin | 47c248a | 2008-04-26 08:16:12 +1000 | [diff] [blame] | 226 | int i; |
Gregory Maxwell | 95becbe | 2011-02-03 21:06:43 -0500 | [diff] [blame] | 227 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | ff96b16 | 2011-03-21 11:32:50 -0400 | [diff] [blame^] | 228 | CELTMode *mode=NULL; |
Gregory Maxwell | 95becbe | 2011-02-03 21:06:43 -0500 | [diff] [blame] | 229 | int res; |
Jean-Marc Valin | 40603b2 | 2010-08-25 23:02:49 -0400 | [diff] [blame] | 230 | celt_word16 *window; |
| 231 | celt_int16 *logN; |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 232 | int LM; |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame] | 233 | ALLOC_STACK; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 234 | #if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA) |
| 235 | if (global_stack==NULL) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 236 | goto failure; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 237 | #endif |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 238 | #endif |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 239 | |
Jean-Marc Valin | 665da0b | 2011-01-30 12:15:12 -0500 | [diff] [blame] | 240 | #ifndef CUSTOM_MODES_ONLY |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 241 | for (i=0;i<TOTAL_MODES;i++) |
| 242 | { |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 243 | int j; |
| 244 | for (j=0;j<4;j++) |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 245 | { |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 246 | if (Fs == static_mode_list[i]->Fs && |
| 247 | (frame_size<<j) == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts) |
| 248 | { |
| 249 | if (error) |
| 250 | *error = CELT_OK; |
| 251 | return (CELTMode*)static_mode_list[i]; |
| 252 | } |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
Jean-Marc Valin | 665da0b | 2011-01-30 12:15:12 -0500 | [diff] [blame] | 255 | #endif /* CUSTOM_MODES_ONLY */ |
| 256 | |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 257 | #ifndef CUSTOM_MODES |
| 258 | if (error) |
| 259 | *error = CELT_BAD_ARG; |
| 260 | return NULL; |
| 261 | #else |
| 262 | |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 263 | /* The good thing here is that permutation of the arguments will automatically be invalid */ |
| 264 | |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 265 | if (Fs < 8000 || Fs > 96000) |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 266 | { |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 267 | if (error) |
| 268 | *error = CELT_BAD_ARG; |
| 269 | return NULL; |
| 270 | } |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 271 | if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0) |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 272 | { |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 273 | if (error) |
| 274 | *error = CELT_BAD_ARG; |
| 275 | return NULL; |
| 276 | } |
Timothy B. Terriberry | aa6fec6 | 2011-02-01 15:36:59 -0800 | [diff] [blame] | 277 | /* Frames of less than 1ms are not supported. */ |
| 278 | if ((celt_int32)frame_size*1000 < Fs) |
| 279 | { |
| 280 | if (error) |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 281 | *error = CELT_BAD_ARG; |
Timothy B. Terriberry | aa6fec6 | 2011-02-01 15:36:59 -0800 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | if ((celt_int32)frame_size*75 >= Fs && (frame_size%16)==0) |
| 286 | { |
| 287 | LM = 3; |
| 288 | } else if ((celt_int32)frame_size*150 >= Fs && (frame_size%8)==0) |
| 289 | { |
| 290 | LM = 2; |
| 291 | } else if ((celt_int32)frame_size*300 >= Fs && (frame_size%4)==0) |
| 292 | { |
| 293 | LM = 1; |
Timothy B. Terriberry | cf5d3a8 | 2011-02-02 11:42:33 -0800 | [diff] [blame] | 294 | } else |
Timothy B. Terriberry | aa6fec6 | 2011-02-01 15:36:59 -0800 | [diff] [blame] | 295 | { |
| 296 | LM = 0; |
| 297 | } |
Timothy B. Terriberry | cf5d3a8 | 2011-02-02 11:42:33 -0800 | [diff] [blame] | 298 | |
Timothy B. Terriberry | aa6fec6 | 2011-02-01 15:36:59 -0800 | [diff] [blame] | 299 | /* Shorts longer than 3.3ms are not supported. */ |
Timothy B. Terriberry | cf5d3a8 | 2011-02-02 11:42:33 -0800 | [diff] [blame] | 300 | if ((celt_int32)(frame_size>>LM)*300 > Fs) |
Timothy B. Terriberry | aa6fec6 | 2011-02-01 15:36:59 -0800 | [diff] [blame] | 301 | { |
| 302 | if (error) |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 303 | *error = CELT_BAD_ARG; |
Timothy B. Terriberry | aa6fec6 | 2011-02-01 15:36:59 -0800 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
| 306 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 307 | mode = celt_alloc(sizeof(CELTMode)); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 308 | if (mode==NULL) |
| 309 | goto failure; |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 310 | mode->Fs = Fs; |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 311 | |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 312 | /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis |
| 313 | is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should |
| 314 | approximate that. */ |
| 315 | if(Fs < 12000) /* 8 kHz */ |
| 316 | { |
Jean-Marc Valin | 49f7680 | 2011-01-27 17:01:59 -0500 | [diff] [blame] | 317 | mode->preemph[0] = QCONST16(0.3500061035f, 15); |
| 318 | mode->preemph[1] = -QCONST16(0.1799926758f, 15); |
Jean-Marc Valin | 09213de | 2011-01-27 21:43:24 -0500 | [diff] [blame] | 319 | mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */ |
Jean-Marc Valin | 49f7680 | 2011-01-27 17:01:59 -0500 | [diff] [blame] | 320 | mode->preemph[3] = QCONST16(3.6765136719f, 13); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 321 | } else if(Fs < 24000) /* 16 kHz */ |
| 322 | { |
Jean-Marc Valin | 49f7680 | 2011-01-27 17:01:59 -0500 | [diff] [blame] | 323 | mode->preemph[0] = QCONST16(0.6000061035f, 15); |
| 324 | mode->preemph[1] = -QCONST16(0.1799926758f, 15); |
Jean-Marc Valin | 09213de | 2011-01-27 21:43:24 -0500 | [diff] [blame] | 325 | mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */ |
| 326 | mode->preemph[3] = QCONST16(2.2598876953f, 13); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 327 | } else if(Fs < 40000) /* 32 kHz */ |
| 328 | { |
Jean-Marc Valin | 49f7680 | 2011-01-27 17:01:59 -0500 | [diff] [blame] | 329 | mode->preemph[0] = QCONST16(0.7799987793f, 15); |
| 330 | mode->preemph[1] = -QCONST16(0.1000061035f, 15); |
Jean-Marc Valin | 09213de | 2011-01-27 21:43:24 -0500 | [diff] [blame] | 331 | mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */ |
Jean-Marc Valin | 49f7680 | 2011-01-27 17:01:59 -0500 | [diff] [blame] | 332 | mode->preemph[3] = QCONST16(1.3333740234f, 13); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 333 | } else /* 48 kHz */ |
| 334 | { |
Jean-Marc Valin | 49f7680 | 2011-01-27 17:01:59 -0500 | [diff] [blame] | 335 | mode->preemph[0] = QCONST16(0.8500061035f, 15); |
| 336 | mode->preemph[1] = QCONST16(0.0f, 15); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 337 | mode->preemph[2] = QCONST16(1.f, SIG_SHIFT); |
| 338 | mode->preemph[3] = QCONST16(1.f, 13); |
| 339 | } |
| 340 | |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 341 | mode->maxLM = LM; |
| 342 | mode->nbShortMdcts = 1<<LM; |
Jean-Marc Valin | 60ff999 | 2010-06-27 13:49:38 -0400 | [diff] [blame] | 343 | mode->shortMdctSize = frame_size/mode->nbShortMdcts; |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 344 | res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize); |
| 345 | |
| 346 | mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands); |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 347 | if (mode->eBands==NULL) |
| 348 | goto failure; |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 349 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 350 | mode->effEBands = mode->nbEBands; |
| 351 | while (mode->eBands[mode->effEBands] > mode->shortMdctSize) |
| 352 | mode->effEBands--; |
Jean-Marc Valin | 4834c92 | 2009-09-28 19:17:34 -0400 | [diff] [blame] | 353 | |
Jean-Marc Valin | 08192e3 | 2009-07-02 13:28:55 -0400 | [diff] [blame] | 354 | /* Overlap must be divisible by 4 */ |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 355 | mode->overlap = ((mode->shortMdctSize>>2)<<2); |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 356 | |
Jean-Marc Valin | e3e2c26 | 2011-01-26 13:09:53 -0500 | [diff] [blame] | 357 | compute_allocation_table(mode); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 358 | if (mode->allocVectors==NULL) |
| 359 | goto failure; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 360 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 361 | window = (celt_word16*)celt_alloc(mode->overlap*sizeof(celt_word16)); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 362 | if (window==NULL) |
| 363 | goto failure; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 364 | |
Jean-Marc Valin | f28062f | 2008-03-03 13:24:01 +1100 | [diff] [blame] | 365 | #ifndef FIXED_POINT |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 366 | for (i=0;i<mode->overlap;i++) |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 367 | window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap)); |
Jean-Marc Valin | f28062f | 2008-03-03 13:24:01 +1100 | [diff] [blame] | 368 | #else |
| 369 | for (i=0;i<mode->overlap;i++) |
Jean-Marc Valin | 8974f00 | 2010-03-20 00:41:39 -0400 | [diff] [blame] | 370 | 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)))); |
Jean-Marc Valin | f28062f | 2008-03-03 13:24:01 +1100 | [diff] [blame] | 371 | #endif |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 372 | mode->window = window; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 373 | |
Jean-Marc Valin | f400a3c | 2010-04-05 23:58:44 -0400 | [diff] [blame] | 374 | logN = (celt_int16*)celt_alloc(mode->nbEBands*sizeof(celt_int16)); |
| 375 | if (logN==NULL) |
| 376 | goto failure; |
| 377 | |
| 378 | for (i=0;i<mode->nbEBands;i++) |
Jean-Marc Valin | aead79b | 2010-05-11 07:34:24 -0400 | [diff] [blame] | 379 | logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES); |
Jean-Marc Valin | f400a3c | 2010-04-05 23:58:44 -0400 | [diff] [blame] | 380 | mode->logN = logN; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 381 | |
| 382 | compute_pulse_cache(mode, mode->maxLM); |
Jean-Marc Valin | 640f7fd | 2009-06-21 09:47:51 -0400 | [diff] [blame] | 383 | |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 384 | clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, mode->maxLM); |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 385 | if ((mode->mdct.trig==NULL) |
Jean-Marc Valin | 9714f66 | 2009-08-12 20:29:57 -0400 | [diff] [blame] | 386 | #ifndef ENABLE_TI_DSPLIB55 |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 387 | || (mode->mdct.kfft==NULL) |
Jean-Marc Valin | 9714f66 | 2009-08-12 20:29:57 -0400 | [diff] [blame] | 388 | #endif |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 389 | ) |
| 390 | goto failure; |
| 391 | |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 392 | if (error) |
| 393 | *error = CELT_OK; |
Jean-Marc Valin | 40603b2 | 2010-08-25 23:02:49 -0400 | [diff] [blame] | 394 | |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 395 | return mode; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 396 | failure: |
| 397 | if (error) |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 398 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 399 | if (mode!=NULL) |
| 400 | celt_mode_destroy(mode); |
| 401 | return NULL; |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 402 | #endif /* !CUSTOM_MODES */ |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 403 | } |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 404 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 405 | void celt_mode_destroy(CELTMode *mode) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 406 | { |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 407 | #ifdef CUSTOM_MODES |
| 408 | int i; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 409 | if (mode == NULL) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 410 | return; |
Jean-Marc Valin | 665da0b | 2011-01-30 12:15:12 -0500 | [diff] [blame] | 411 | #ifndef CUSTOM_MODES_ONLY |
Jean-Marc Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 412 | for (i=0;i<TOTAL_MODES;i++) |
| 413 | { |
| 414 | if (mode == static_mode_list[i]) |
| 415 | { |
| 416 | return; |
| 417 | } |
| 418 | } |
Jean-Marc Valin | 665da0b | 2011-01-30 12:15:12 -0500 | [diff] [blame] | 419 | #endif /* CUSTOM_MODES_ONLY */ |
Jean-Marc Valin | 31bec96 | 2010-04-07 18:30:28 -0400 | [diff] [blame] | 420 | celt_free((celt_int16*)mode->eBands); |
| 421 | celt_free((celt_int16*)mode->allocVectors); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 422 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 423 | celt_free((celt_word16*)mode->window); |
Jean-Marc Valin | 31bec96 | 2010-04-07 18:30:28 -0400 | [diff] [blame] | 424 | celt_free((celt_int16*)mode->logN); |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 425 | |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 426 | celt_free((celt_int16*)mode->cache.index); |
| 427 | celt_free((unsigned char*)mode->cache.bits); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 428 | celt_free((unsigned char*)mode->cache.caps); |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 429 | clt_mdct_clear(&mode->mdct); |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 430 | |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 431 | celt_free((CELTMode *)mode); |
Jean-Marc Valin | 40603b2 | 2010-08-25 23:02:49 -0400 | [diff] [blame] | 432 | #endif |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 433 | } |