Jean-Marc Valin | 8b2a592 | 2008-02-29 00:32:51 +1100 | [diff] [blame] | 1 | /* (C) 2007-2008 Jean-Marc Valin, CSIRO |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 2 | */ |
| 3 | /* |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions |
| 6 | are met: |
| 7 | |
| 8 | - Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | - Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | |
| 15 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | contributors may be used to endorse or promote products derived from |
| 17 | this software without specific prior written permission. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include "config.h" |
| 34 | #endif |
| 35 | |
Jean-Marc Valin | 65d57e6 | 2008-02-18 15:49:37 +1100 | [diff] [blame] | 36 | #include "celt.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 37 | #include "modes.h" |
Jean-Marc Valin | 472a5f0 | 2008-02-19 13:12:32 +1100 | [diff] [blame] | 38 | #include "rate.h" |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 39 | #include "os_support.h" |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame^] | 40 | #include "stack_alloc.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 41 | |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 42 | #ifdef STATIC_MODES |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 43 | #include "static_modes.c" |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 44 | #endif |
| 45 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 46 | #define MODEVALID 0xa110ca7e |
| 47 | #define MODEFREED 0xb10cf8ee |
| 48 | |
Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 49 | #ifndef M_PI |
| 50 | #define M_PI 3.141592653 |
| 51 | #endif |
| 52 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 53 | |
Jean-Marc Valin | 7f38906 | 2008-03-12 17:46:04 +1100 | [diff] [blame] | 54 | int EXPORT celt_mode_info(const CELTMode *mode, int request, celt_int32_t *value) |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 55 | { |
| 56 | switch (request) |
| 57 | { |
| 58 | case CELT_GET_FRAME_SIZE: |
| 59 | *value = mode->mdctSize; |
| 60 | break; |
| 61 | case CELT_GET_LOOKAHEAD: |
| 62 | *value = mode->overlap; |
| 63 | break; |
Jean-Marc Valin | 4c6bc88 | 2008-01-31 17:34:27 +1100 | [diff] [blame] | 64 | case CELT_GET_NB_CHANNELS: |
| 65 | *value = mode->nbChannels; |
| 66 | break; |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 67 | default: |
| 68 | return CELT_BAD_ARG; |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 69 | } |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 70 | return CELT_OK; |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 71 | } |
| 72 | |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 73 | #ifndef STATIC_MODES |
| 74 | |
Jean-Marc Valin | 134492a | 2008-02-18 16:46:22 +1100 | [diff] [blame] | 75 | #define PBANDS 8 |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 76 | #define MIN_BINS 4 |
Jean-Marc Valin | 17683eb | 2008-02-18 21:45:19 +1100 | [diff] [blame] | 77 | /* Defining 25 critical bands for the full 0-20 kHz audio bandwidth |
| 78 | 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] | 79 | #define BARK_BANDS 25 |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 80 | static const celt_int16_t bark_freq[BARK_BANDS+1] = { |
Jean-Marc Valin | 17683eb | 2008-02-18 21:45:19 +1100 | [diff] [blame] | 81 | 0, 100, 200, 300, 400, |
| 82 | 510, 630, 770, 920, 1080, |
| 83 | 1270, 1480, 1720, 2000, 2320, |
| 84 | 2700, 3150, 3700, 4400, 5300, |
| 85 | 6400, 7700, 9500, 12000, 15500, |
| 86 | 20000}; |
| 87 | |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 88 | static const celt_int16_t pitch_freq[PBANDS+1] ={0, 345, 689, 1034, 1378, 2067, 3273, 5340, 6374}; |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 89 | |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 90 | /* This allocation table is per critical band. When creating a mode, the bits get added together |
| 91 | into the codec bands, which are sometimes larger than one critical band at low frequency */ |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 92 | #define BITALLOC_SIZE 10 |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 93 | static const int band_allocation[BARK_BANDS*BITALLOC_SIZE] = |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 94 | { 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Jean-Marc Valin | 5a4bbde | 2008-02-19 14:32:08 +1100 | [diff] [blame] | 95 | 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 5, 5, 7, 7, 7, 5, 4, 0, 0, 0, 0, 0, 0, |
| 96 | 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 3, 2, 6, 6, 8, 8, 8, 6, 5, 4, 0, 0, 0, 0, 0, |
| 97 | 3, 2, 2, 2, 3, 3, 2, 3, 2, 2, 4, 3, 7, 7, 9, 9, 9, 7, 6, 5, 5, 5, 0, 0, 0, |
| 98 | 3, 3, 2, 2, 3, 3, 3, 3, 3, 2, 4, 4, 9, 9, 10, 10, 10, 9, 6, 5, 5, 5, 5, 0, 0, |
| 99 | 3, 3, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 10, 10, 12, 12, 12, 10, 10, 10, 11, 10, 10, 5, 5, |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 100 | 4, 4, 4, 4, 5, 5, 5, 5, 5, 4, 7, 7, 14, 13, 13, 13, 13, 13, 15, 16, 17, 18, 20, 18, 11, |
| 101 | 7, 7, 6, 6, 9, 8, 8, 8, 8, 8, 11, 11, 20, 18, 19, 19, 25, 22, 25, 30, 30, 35, 35, 35, 35, |
| 102 | 8, 8, 8, 8, 10, 10, 10, 10, 9, 9, 19, 18, 25, 24, 23, 21, 29, 27, 35, 40, 42, 50, 59, 54, 51, |
| 103 | 11, 11, 10, 10, 14, 13, 13, 13, 13, 12, 19, 18, 35, 34, 33, 31, 39, 37, 45, 50, 52, 60, 60, 60, 60, |
| 104 | }; |
| 105 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 106 | |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 107 | static celt_int16_t *compute_ebands(celt_int32_t Fs, int frame_size, int *nbEBands) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 108 | { |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 109 | celt_int16_t *eBands; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 110 | int i, res, min_width, lin, low, high; |
| 111 | res = (Fs+frame_size)/(2*frame_size); |
| 112 | min_width = MIN_BINS*res; |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 113 | /*printf ("min_width = %d\n", min_width);*/ |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 114 | |
| 115 | /* Find where the linear part ends (i.e. where the spacing is more than min_width */ |
| 116 | for (lin=0;lin<BARK_BANDS;lin++) |
| 117 | if (bark_freq[lin+1]-bark_freq[lin] >= min_width) |
| 118 | break; |
| 119 | |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 120 | /*printf ("lin = %d (%d Hz)\n", lin, bark_freq[lin]);*/ |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 121 | low = ((bark_freq[lin]/res)+(MIN_BINS-1))/MIN_BINS; |
| 122 | high = BARK_BANDS-lin; |
| 123 | *nbEBands = low+high; |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 124 | eBands = celt_alloc(sizeof(celt_int16_t)*(*nbEBands+2)); |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 125 | |
| 126 | /* Linear spacing (min_width) */ |
| 127 | for (i=0;i<low;i++) |
| 128 | eBands[i] = MIN_BINS*i; |
| 129 | /* Spacing follows critical bands */ |
| 130 | for (i=0;i<high;i++) |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 131 | eBands[i+low] = (bark_freq[lin+i]+res/2)/res; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 132 | /* Enforce the minimum spacing at the boundary */ |
| 133 | for (i=0;i<*nbEBands;i++) |
| 134 | if (eBands[i] < MIN_BINS*i) |
| 135 | eBands[i] = MIN_BINS*i; |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 136 | eBands[*nbEBands] = (bark_freq[BARK_BANDS]+res/2)/res; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 137 | eBands[*nbEBands+1] = frame_size; |
| 138 | if (eBands[*nbEBands] > eBands[*nbEBands+1]) |
| 139 | eBands[*nbEBands] = eBands[*nbEBands+1]; |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 140 | |
| 141 | /* FIXME: Remove last band if too small */ |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 142 | /*for (i=0;i<*nbEBands+2;i++) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 143 | printf("%d ", eBands[i]); |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 144 | printf ("\n");*/ |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 145 | return eBands; |
| 146 | } |
| 147 | |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 148 | static void compute_pbands(CELTMode *mode, int res) |
| 149 | { |
| 150 | int i; |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 151 | celt_int16_t *pBands; |
| 152 | pBands=celt_alloc(sizeof(celt_int16_t)*(PBANDS+2)); |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 153 | mode->nbPBands = PBANDS; |
| 154 | for (i=0;i<PBANDS+1;i++) |
| 155 | { |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 156 | pBands[i] = (pitch_freq[i]+res/2)/res; |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 157 | if (pBands[i] < mode->eBands[i]) |
| 158 | pBands[i] = mode->eBands[i]; |
| 159 | } |
| 160 | pBands[PBANDS+1] = mode->eBands[mode->nbEBands+1]; |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 161 | for (i=1;i<mode->nbPBands+1;i++) |
| 162 | { |
| 163 | int j; |
| 164 | for (j=0;j<mode->nbEBands;j++) |
| 165 | if (mode->eBands[j] <= pBands[i] && mode->eBands[j+1] > pBands[i]) |
| 166 | break; |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 167 | /*printf ("%d %d\n", i, j);*/ |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 168 | if (mode->eBands[j] != pBands[i]) |
| 169 | { |
| 170 | if (pBands[i]-mode->eBands[j] < mode->eBands[j+1]-pBands[i] && |
| 171 | mode->eBands[j] != pBands[i-1]) |
| 172 | pBands[i] = mode->eBands[j]; |
| 173 | else |
| 174 | pBands[i] = mode->eBands[j+1]; |
| 175 | } |
| 176 | } |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 177 | /*for (i=0;i<mode->nbPBands+2;i++) |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 178 | printf("%d ", pBands[i]); |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 179 | printf ("\n");*/ |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 180 | mode->pBands = pBands; |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 181 | mode->pitchEnd = pBands[PBANDS]; |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 182 | } |
| 183 | |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 184 | static void compute_allocation_table(CELTMode *mode, int res) |
| 185 | { |
| 186 | int i, j, eband; |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 187 | celt_int16_t *allocVectors; |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 188 | |
| 189 | mode->nbAllocVectors = BITALLOC_SIZE; |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 190 | allocVectors = celt_alloc(sizeof(celt_int16_t)*(BITALLOC_SIZE*mode->nbEBands)); |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 191 | for (i=0;i<BITALLOC_SIZE;i++) |
| 192 | { |
| 193 | eband = 0; |
| 194 | for (j=0;j<BARK_BANDS;j++) |
| 195 | { |
| 196 | int edge, low; |
| 197 | edge = mode->eBands[eband+1]*res; |
| 198 | if (edge < bark_freq[j+1]) |
| 199 | { |
| 200 | int num, den; |
| 201 | num = band_allocation[i*BARK_BANDS+j] * (edge-bark_freq[j]); |
| 202 | den = bark_freq[j+1]-bark_freq[j]; |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 203 | low = (num+den/2)/den; |
| 204 | allocVectors[i*mode->nbEBands+eband] += low; |
| 205 | eband++; |
| 206 | allocVectors[i*mode->nbEBands+eband] += band_allocation[i*BARK_BANDS+j]-low; |
| 207 | } else { |
| 208 | allocVectors[i*mode->nbEBands+eband] += band_allocation[i*BARK_BANDS+j]; |
| 209 | } |
| 210 | } |
| 211 | } |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 212 | /*for (i=0;i<BITALLOC_SIZE;i++) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 213 | { |
| 214 | for (j=0;j<mode->nbEBands;j++) |
| 215 | printf ("%2d ", allocVectors[i*mode->nbEBands+j]); |
| 216 | printf ("\n"); |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 217 | }*/ |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 218 | mode->allocVectors = allocVectors; |
| 219 | } |
| 220 | |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 221 | #endif /* STATIC_MODES */ |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 222 | |
Jean-Marc Valin | 7f38906 | 2008-03-12 17:46:04 +1100 | [diff] [blame] | 223 | CELTMode EXPORT *celt_mode_create(celt_int32_t Fs, int channels, int frame_size, int lookahead, int *error) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 224 | { |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 225 | #ifdef STATIC_MODES |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 226 | const CELTMode *m = NULL; |
| 227 | CELTMode *mode=NULL; |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 228 | int i; |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame^] | 229 | ALLOC_STACK; |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 230 | for (i=0;i<TOTAL_MODES;i++) |
| 231 | { |
| 232 | if (Fs == static_mode_list[i]->Fs && |
| 233 | channels == static_mode_list[i]->nbChannels && |
| 234 | frame_size == static_mode_list[i]->mdctSize && |
| 235 | lookahead == static_mode_list[i]->overlap) |
| 236 | { |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 237 | m = static_mode_list[i]; |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | } |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 241 | if (m == NULL) |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 242 | { |
| 243 | celt_warning("Mode not included as part of the static modes"); |
| 244 | if (error) |
| 245 | *error = CELT_BAD_ARG; |
| 246 | return NULL; |
| 247 | } |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 248 | mode = (CELTMode*)celt_alloc(sizeof(CELTMode)); |
| 249 | CELT_COPY(mode, m, 1); |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 250 | #else |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 251 | int res; |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 252 | int i; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 253 | CELTMode *mode; |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 254 | celt_word16_t *window; |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame^] | 255 | ALLOC_STACK; |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 256 | |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 257 | /* The good thing here is that permutation of the arguments will automatically be invalid */ |
| 258 | |
| 259 | if (Fs < 32000 || Fs > 64000) |
| 260 | { |
| 261 | celt_warning("Sampling rate must be between 32 kHz and 64 kHz"); |
| 262 | if (error) |
| 263 | *error = CELT_BAD_ARG; |
| 264 | return NULL; |
| 265 | } |
| 266 | if (channels < 0 || channels > 2) |
| 267 | { |
| 268 | celt_warning("Only mono and stereo supported"); |
| 269 | if (error) |
| 270 | *error = CELT_BAD_ARG; |
| 271 | return NULL; |
| 272 | } |
| 273 | if (frame_size < 64 || frame_size > 256 || frame_size%2!=0) |
| 274 | { |
| 275 | celt_warning("Only even frame sizes between 64 and 256 are supported"); |
| 276 | if (error) |
| 277 | *error = CELT_BAD_ARG; |
| 278 | return NULL; |
| 279 | } |
| 280 | if (lookahead < 32 || lookahead > frame_size) |
| 281 | { |
| 282 | celt_warning("The overlap must be between 32 and the frame size"); |
| 283 | if (error) |
| 284 | *error = CELT_BAD_ARG; |
| 285 | return NULL; |
| 286 | } |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 287 | res = (Fs+frame_size)/(2*frame_size); |
| 288 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 289 | mode = celt_alloc(sizeof(CELTMode)); |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 290 | mode->Fs = Fs; |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 291 | mode->overlap = lookahead; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 292 | mode->mdctSize = frame_size; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 293 | mode->nbChannels = channels; |
| 294 | mode->eBands = compute_ebands(Fs, frame_size, &mode->nbEBands); |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 295 | compute_pbands(mode, res); |
Jean-Marc Valin | 8b2a592 | 2008-02-29 00:32:51 +1100 | [diff] [blame] | 296 | mode->ePredCoef = QCONST16(.8f,15); |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 297 | |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 298 | compute_allocation_table(mode, res); |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 299 | /*printf ("%d bands\n", mode->nbEBands);*/ |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 300 | |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 301 | window = (celt_word16_t*)celt_alloc(mode->overlap*sizeof(celt_word16_t)); |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 302 | |
Jean-Marc Valin | f28062f | 2008-03-03 13:24:01 +1100 | [diff] [blame] | 303 | #ifndef FIXED_POINT |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 304 | for (i=0;i<mode->overlap;i++) |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 305 | 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] | 306 | #else |
| 307 | for (i=0;i<mode->overlap;i++) |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 308 | window[i] = MIN32(32767,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] | 309 | #endif |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 310 | mode->window = window; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 311 | |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 312 | compute_alloc_cache(mode); |
| 313 | |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 314 | psydecay_init(&mode->psy, MAX_PERIOD/2, mode->Fs); |
| 315 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 316 | mode->marker_start = MODEVALID; |
| 317 | mode->marker_end = MODEVALID; |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 318 | #endif /* !STATIC_MODES */ |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 319 | mdct_init(&mode->mdct, 2*mode->mdctSize); |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 320 | if (error) |
| 321 | *error = CELT_OK; |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 322 | return mode; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 323 | } |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 324 | |
Jean-Marc Valin | 7f38906 | 2008-03-12 17:46:04 +1100 | [diff] [blame] | 325 | void EXPORT celt_mode_destroy(CELTMode *mode) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 326 | { |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 327 | #ifndef STATIC_MODES |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 328 | int i; |
Jean-Marc Valin | db0141e | 2008-03-10 16:37:37 +1100 | [diff] [blame] | 329 | const celt_int16_t *prevPtr = NULL; |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 330 | for (i=0;i<mode->nbEBands;i++) |
| 331 | { |
| 332 | if (mode->bits[i] != prevPtr) |
| 333 | { |
| 334 | prevPtr = mode->bits[i]; |
| 335 | celt_free((int*)mode->bits[i]); |
| 336 | } |
| 337 | } |
| 338 | celt_free((int**)mode->bits); |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 339 | if (check_mode(mode) != CELT_OK) |
| 340 | return; |
| 341 | celt_free((int*)mode->eBands); |
| 342 | celt_free((int*)mode->pBands); |
| 343 | celt_free((int*)mode->allocVectors); |
| 344 | |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 345 | celt_free((celt_word16_t*)mode->window); |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 346 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 347 | mode->marker_start = MODEFREED; |
| 348 | mode->marker_end = MODEFREED; |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 349 | psydecay_clear(&mode->psy); |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 350 | #endif |
Jean-Marc Valin | 9656ca0 | 2008-04-11 17:23:01 +1000 | [diff] [blame] | 351 | mdct_clear(&mode->mdct); |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 352 | celt_free((CELTMode *)mode); |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 353 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 354 | |
| 355 | int check_mode(const CELTMode *mode) |
| 356 | { |
| 357 | if (mode->marker_start == MODEVALID && mode->marker_end == MODEVALID) |
| 358 | return CELT_OK; |
| 359 | if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED) |
| 360 | celt_warning("Using a mode that has already been freed"); |
| 361 | else |
| 362 | celt_warning("This is not a valid CELT mode"); |
| 363 | return CELT_INVALID_MODE; |
| 364 | } |