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 | |
| 17 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 18 | contributors may be used to endorse or promote products derived from |
| 19 | this software without specific prior written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 34 | #ifdef HAVE_CONFIG_H |
| 35 | #include "config.h" |
| 36 | #endif |
| 37 | |
Jean-Marc Valin | 65d57e6 | 2008-02-18 15:49:37 +1100 | [diff] [blame] | 38 | #include "celt.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 39 | #include "modes.h" |
Jean-Marc Valin | 472a5f0 | 2008-02-19 13:12:32 +1100 | [diff] [blame] | 40 | #include "rate.h" |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 41 | #include "os_support.h" |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame] | 42 | #include "stack_alloc.h" |
Jean-Marc Valin | 4ce9205 | 2008-04-23 13:42:10 +1000 | [diff] [blame] | 43 | #include "quant_bands.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 44 | |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 45 | #ifdef STATIC_MODES |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 46 | #include "static_modes.c" |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 47 | #endif |
| 48 | |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 49 | #define MODEVALID 0xa110ca7e |
| 50 | #define MODEPARTIAL 0x7eca10a1 |
| 51 | #define MODEFREED 0xb10cf8ee |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 52 | |
Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 53 | #ifndef M_PI |
| 54 | #define M_PI 3.141592653 |
| 55 | #endif |
| 56 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 57 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 58 | int celt_mode_info(const CELTMode *mode, int request, celt_int32 *value) |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 59 | { |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 60 | if (check_mode(mode) != CELT_OK) |
| 61 | return CELT_INVALID_MODE; |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 62 | switch (request) |
| 63 | { |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 64 | case CELT_GET_LOOKAHEAD: |
| 65 | *value = mode->overlap; |
| 66 | break; |
Jean-Marc Valin | 59093c0 | 2008-05-15 21:53:27 +1000 | [diff] [blame] | 67 | case CELT_GET_BITSTREAM_VERSION: |
| 68 | *value = CELT_BITSTREAM_VERSION; |
| 69 | break; |
Gregory Maxwell | d9458cd | 2009-05-30 17:04:02 -0400 | [diff] [blame] | 70 | case CELT_GET_SAMPLE_RATE: |
| 71 | *value = mode->Fs; |
| 72 | break; |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 73 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 74 | return CELT_UNIMPLEMENTED; |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 75 | } |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 76 | return CELT_OK; |
Jean-Marc Valin | f997ad5 | 2008-01-31 16:47:16 +1100 | [diff] [blame] | 77 | } |
| 78 | |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 79 | #ifndef STATIC_MODES |
| 80 | |
Jean-Marc Valin | 17683eb | 2008-02-18 21:45:19 +1100 | [diff] [blame] | 81 | /* Defining 25 critical bands for the full 0-20 kHz audio bandwidth |
| 82 | 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] | 83 | #define BARK_BANDS 25 |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 84 | static const celt_int16 bark_freq[BARK_BANDS+1] = { |
Jean-Marc Valin | 17683eb | 2008-02-18 21:45:19 +1100 | [diff] [blame] | 85 | 0, 100, 200, 300, 400, |
| 86 | 510, 630, 770, 920, 1080, |
| 87 | 1270, 1480, 1720, 2000, 2320, |
| 88 | 2700, 3150, 3700, 4400, 5300, |
| 89 | 6400, 7700, 9500, 12000, 15500, |
| 90 | 20000}; |
| 91 | |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 92 | /* This allocation table is per critical band. When creating a mode, the bits get added together |
| 93 | into the codec bands, which are sometimes larger than one critical band at low frequency */ |
Jean-Marc Valin | 47c248a | 2008-04-26 08:16:12 +1000 | [diff] [blame] | 94 | |
Jean-Marc Valin | 9c70906 | 2008-08-03 22:07:06 -0400 | [diff] [blame] | 95 | #define BITALLOC_SIZE 12 |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 96 | |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 97 | static const celt_int16 eband5ms[] = { |
| 98 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 |
| 99 | }; |
| 100 | |
Jean-Marc Valin | ffe5061 | 2010-06-04 00:13:19 -0400 | [diff] [blame] | 101 | static const unsigned char band_allocation[] = { |
| 102 | /* 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 | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 103 | 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 104 | 10, 3, 8, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 105 | 10, 6, 8, 6, 5, 4, 3, 2, 7, 10, 11, 9, 7, 3, 1, 0, 0, 0, 0, 0, 0, |
| 106 | 10, 10, 14, 11, 10, 8, 6, 5, 10, 12, 13, 11, 8, 4, 2, 1, 0, 0, 0, 0, 0, |
| 107 | 13, 10, 17, 16, 14, 12, 10, 8, 12, 14, 14, 12, 9, 5, 3, 2, 2, 1, 0, 0, 0, |
| 108 | 17, 21, 23, 26, 24, 20, 17, 16, 17, 18, 16, 14, 11, 6, 3, 2, 2, 1, 1, 0, 0, |
| 109 | 21, 21, 36, 32, 28, 24, 23, 23, 22, 18, 18, 14, 11, 7, 5, 5, 5, 3, 3, 0, 0, |
| 110 | 31, 35, 40, 32, 30, 28, 26, 26, 25, 24, 19, 15, 15, 13, 9, 9, 8, 7, 5, 2, 0, |
| 111 | 42, 46, 46, 37, 35, 34, 33, 32, 34, 35, 32, 31, 27, 24, 23, 23, 18, 14, 11, 7, 0, |
| 112 | 46, 49, 46, 46, 42, 43, 44, 47, 50, 52, 51, 48, 39, 32, 27, 24, 22, 19, 17, 11, 5, |
| 113 | 53, 53, 49, 48, 55, 66, 71, 71, 71, 65, 64, 64, 56, 47, 41, 37, 31, 24, 20, 16, 10, |
| 114 | 60, 64, 74, 74, 87,103,106,102,101,100,101, 95, 80, 69, 63, 55, 47, 36, 26, 21, 15, |
| 115 | }; |
Jean-Marc Valin | 20639c4 | 2010-05-19 16:10:12 -0400 | [diff] [blame] | 116 | |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 117 | 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] | 118 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 119 | celt_int16 *eBands; |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 120 | int i, lin, low, high, nBark, offset=0; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 121 | |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame^] | 122 | /* All modes that have 2.5 ms short blocks use the same definition */ |
| 123 | if (Fs == 400*(celt_int32)frame_size) |
Jean-Marc Valin | 20639c4 | 2010-05-19 16:10:12 -0400 | [diff] [blame] | 124 | { |
| 125 | *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; |
| 126 | eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2)); |
| 127 | for (i=0;i<*nbEBands+2;i++) |
| 128 | eBands[i] = eband5ms[i]; |
| 129 | eBands[*nbEBands+1] = frame_size; |
| 130 | return eBands; |
| 131 | } |
Gregory Maxwell | cbaf67e | 2008-09-28 04:19:19 -0400 | [diff] [blame] | 132 | /* Find the number of critical bands supported by our sampling rate */ |
| 133 | for (nBark=1;nBark<BARK_BANDS;nBark++) |
| 134 | if (bark_freq[nBark+1]*2 >= Fs) |
| 135 | break; |
| 136 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 137 | /* 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] | 138 | for (lin=0;lin<nBark;lin++) |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 139 | if (bark_freq[lin+1]-bark_freq[lin] >= res) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 140 | break; |
Jean-Marc Valin | 4fb9568 | 2010-04-20 23:30:22 -0400 | [diff] [blame] | 141 | |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 142 | low = (bark_freq[lin]+res/2)/res; |
Gregory Maxwell | cbaf67e | 2008-09-28 04:19:19 -0400 | [diff] [blame] | 143 | high = nBark-lin; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 144 | *nbEBands = low+high; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 145 | eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2)); |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 146 | |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 147 | if (eBands==NULL) |
| 148 | return NULL; |
| 149 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 150 | /* Linear spacing (min_width) */ |
| 151 | for (i=0;i<low;i++) |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 152 | eBands[i] = i; |
Jean-Marc Valin | be8d125 | 2010-04-21 18:09:07 -0400 | [diff] [blame] | 153 | if (low>0) |
| 154 | offset = eBands[low-1]*res - bark_freq[lin-1]; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 155 | /* Spacing follows critical bands */ |
| 156 | for (i=0;i<high;i++) |
Jean-Marc Valin | 39f68ac | 2009-10-03 23:27:52 -0400 | [diff] [blame] | 157 | { |
| 158 | int target = bark_freq[lin+i]; |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 159 | eBands[i+low] = (target+(offset+res)/2)/res; |
Jean-Marc Valin | 39f68ac | 2009-10-03 23:27:52 -0400 | [diff] [blame] | 160 | offset = eBands[i+low]*res - target; |
| 161 | } |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 162 | /* Enforce the minimum spacing at the boundary */ |
| 163 | for (i=0;i<*nbEBands;i++) |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 164 | if (eBands[i] < i) |
| 165 | eBands[i] = i; |
| 166 | eBands[*nbEBands] = (bark_freq[nBark]+res/2)/res; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 167 | eBands[*nbEBands+1] = frame_size; |
| 168 | if (eBands[*nbEBands] > eBands[*nbEBands+1]) |
| 169 | eBands[*nbEBands] = eBands[*nbEBands+1]; |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 170 | for (i=1;i<*nbEBands-1;i++) |
| 171 | { |
| 172 | if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1]) |
| 173 | { |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 174 | 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] | 175 | } |
| 176 | } |
Jean-Marc Valin | 65ee67a | 2010-04-26 07:08:44 -0400 | [diff] [blame] | 177 | /*for (i=0;i<=*nbEBands+1;i++) |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 178 | printf ("%d ", eBands[i]); |
Jean-Marc Valin | 39f68ac | 2009-10-03 23:27:52 -0400 | [diff] [blame] | 179 | printf ("\n"); |
| 180 | exit(1);*/ |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 181 | /* FIXME: Remove last band if too small */ |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 182 | return eBands; |
| 183 | } |
| 184 | |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 185 | static void compute_allocation_table(CELTMode *mode, int res) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 186 | { |
Jean-Marc Valin | 2f6c5fe | 2010-06-28 17:22:37 -0400 | [diff] [blame] | 187 | int i, j; |
Jean-Marc Valin | 01b54b9 | 2010-06-03 23:29:35 -0400 | [diff] [blame] | 188 | unsigned char *allocVectors; |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 189 | int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1; |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 190 | |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 191 | mode->nbAllocVectors = BITALLOC_SIZE; |
Jean-Marc Valin | 01b54b9 | 2010-06-03 23:29:35 -0400 | [diff] [blame] | 192 | allocVectors = celt_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands)); |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 193 | if (allocVectors==NULL) |
| 194 | return; |
| 195 | |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 196 | /* Check for standard mode */ |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 197 | if (mode->Fs == 400*(celt_int32)mode->shortMdctSize && mode->Fs >= 40000) |
| 198 | { |
| 199 | for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++) |
Jean-Marc Valin | ffe5061 | 2010-06-04 00:13:19 -0400 | [diff] [blame] | 200 | allocVectors[i] = band_allocation[i]; |
Jean-Marc Valin | 12e851d | 2010-06-03 08:12:11 -0400 | [diff] [blame] | 201 | mode->allocVectors = allocVectors; |
| 202 | return; |
| 203 | } |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 204 | /* If not the standard mode, interpolate */ |
| 205 | |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 206 | /* Compute per-codec-band allocation from per-critical-band matrix */ |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 207 | for (i=0;i<BITALLOC_SIZE;i++) |
| 208 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 209 | celt_int32 current = 0; |
Gregory Maxwell | ec836da | 2009-05-04 14:55:40 -0400 | [diff] [blame] | 210 | int eband = 0; |
Jean-Marc Valin | 2f6c5fe | 2010-06-28 17:22:37 -0400 | [diff] [blame] | 211 | /* We may be looping over too many bands, but eband will stop being |
| 212 | incremented once we reach the last band */ |
| 213 | for (j=0;j<maxBands;j++) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 214 | { |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 215 | int edge, low, high; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 216 | celt_int32 alloc; |
Jean-Marc Valin | ffe5061 | 2010-06-04 00:13:19 -0400 | [diff] [blame] | 217 | alloc = band_allocation[i*maxBands + j]*(mode->eBands[eband+1]-mode->eBands[eband])<<4; |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 218 | low = eband5ms[j]*200; |
| 219 | high = eband5ms[j+1]*200; |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 220 | edge = mode->eBands[eband+1]*res; |
Jean-Marc Valin | 3ff5e4c | 2010-04-14 18:02:09 -0400 | [diff] [blame] | 221 | while (edge <= high && eband < mode->nbEBands) |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 222 | { |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 223 | celt_int32 num; |
| 224 | int den, bits; |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 225 | int N = (mode->eBands[eband+1]-mode->eBands[eband]); |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 226 | num = alloc * (edge-low); |
| 227 | den = high-low; |
| 228 | /* Divide with rounding */ |
| 229 | bits = (2*num+den)/(2*den); |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 230 | allocVectors[i*mode->nbEBands+eband] = (2*(current+bits)+(N<<4))/(2*N<<4); |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 231 | /* Remove the part of the band we just allocated */ |
| 232 | low = edge; |
| 233 | alloc -= bits; |
| 234 | |
| 235 | /* Move to next eband */ |
| 236 | current = 0; |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 237 | eband++; |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 238 | edge = mode->eBands[eband+1]*res; |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 239 | } |
| 240 | current += alloc; |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 241 | } |
Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 242 | if (eband < mode->nbEBands) |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 243 | { |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 244 | int N = (mode->eBands[eband+1]-mode->eBands[eband]); |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 245 | allocVectors[i*mode->nbEBands+eband] = (2*current+(N<<4))/(2*N<<4); |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 246 | } |
Jean-Marc Valin | 9838fec | 2008-02-18 14:45:11 +1100 | [diff] [blame] | 247 | } |
Jean-Marc Valin | c51e98b | 2010-06-03 22:11:41 -0400 | [diff] [blame] | 248 | /*printf ("\n"); |
| 249 | for (i=0;i<BITALLOC_SIZE;i++) |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 250 | { |
| 251 | for (j=0;j<mode->nbEBands;j++) |
| 252 | printf ("%d ", allocVectors[i*mode->nbEBands+j]); |
| 253 | printf ("\n"); |
| 254 | } |
| 255 | exit(0);*/ |
| 256 | |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 257 | mode->allocVectors = allocVectors; |
Jean-Marc Valin | ad63719 | 2008-05-07 13:44:39 +1000 | [diff] [blame] | 258 | } |
| 259 | |
Jean-Marc Valin | 36e6e01 | 2008-08-02 22:25:19 -0400 | [diff] [blame] | 260 | #endif /* STATIC_MODES */ |
| 261 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 262 | 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] | 263 | { |
Jean-Marc Valin | 47c248a | 2008-04-26 08:16:12 +1000 | [diff] [blame] | 264 | int i; |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 265 | int LM; |
Jean-Marc Valin | 47c248a | 2008-04-26 08:16:12 +1000 | [diff] [blame] | 266 | #ifdef STDIN_TUNING |
| 267 | scanf("%d ", &MIN_BINS); |
| 268 | scanf("%d ", &BITALLOC_SIZE); |
| 269 | band_allocation = celt_alloc(sizeof(int)*BARK_BANDS*BITALLOC_SIZE); |
| 270 | for (i=0;i<BARK_BANDS*BITALLOC_SIZE;i++) |
| 271 | { |
| 272 | scanf("%d ", band_allocation+i); |
| 273 | } |
| 274 | #endif |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 275 | #ifdef STATIC_MODES |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 276 | const CELTMode *m = NULL; |
| 277 | CELTMode *mode=NULL; |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame] | 278 | ALLOC_STACK; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 279 | #if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA) |
| 280 | if (global_stack==NULL) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 281 | goto failure; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 282 | #endif |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 283 | for (i=0;i<TOTAL_MODES;i++) |
| 284 | { |
| 285 | if (Fs == static_mode_list[i]->Fs && |
Jean-Marc Valin | 60ff999 | 2010-06-27 13:49:38 -0400 | [diff] [blame] | 286 | frame_size == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts) |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 287 | { |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 288 | m = static_mode_list[i]; |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | } |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 292 | if (m == NULL) |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 293 | { |
| 294 | celt_warning("Mode not included as part of the static modes"); |
| 295 | if (error) |
| 296 | *error = CELT_BAD_ARG; |
| 297 | return NULL; |
| 298 | } |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 299 | mode = (CELTMode*)celt_alloc(sizeof(CELTMode)); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 300 | if (mode==NULL) |
| 301 | goto failure; |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 302 | CELT_COPY(mode, m, 1); |
Jean-Marc Valin | f477d58 | 2010-06-27 09:56:26 -0400 | [diff] [blame] | 303 | mode->bits = mode->_bits+1; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 304 | mode->marker_start = MODEPARTIAL; |
Jean-Marc Valin | f39e869 | 2008-03-10 12:13:23 +1100 | [diff] [blame] | 305 | #else |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 306 | int res; |
Gregory Maxwell | 8496695 | 2009-06-30 01:06:04 -0400 | [diff] [blame] | 307 | CELTMode *mode=NULL; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 308 | celt_word16 *window; |
Jean-Marc Valin | f400a3c | 2010-04-05 23:58:44 -0400 | [diff] [blame] | 309 | celt_int16 *logN; |
Jean-Marc Valin | f7cec83 | 2008-04-18 17:29:56 +1000 | [diff] [blame] | 310 | ALLOC_STACK; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 311 | #if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA) |
| 312 | if (global_stack==NULL) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 313 | goto failure; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 314 | #endif |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 315 | |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 316 | /* The good thing here is that permutation of the arguments will automatically be invalid */ |
| 317 | |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 318 | if (Fs < 8000 || Fs > 96000) |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 319 | { |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 320 | celt_warning("Sampling rate must be between 8 kHz and 96 kHz"); |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 321 | if (error) |
| 322 | *error = CELT_BAD_ARG; |
| 323 | return NULL; |
| 324 | } |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 325 | if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0) |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 326 | { |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 327 | celt_warning("Only even frame sizes from 40 to 1024 are supported"); |
Jean-Marc Valin | 75e9c86 | 2008-02-18 17:04:15 +1100 | [diff] [blame] | 328 | if (error) |
| 329 | *error = CELT_BAD_ARG; |
| 330 | return NULL; |
| 331 | } |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 332 | |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 333 | mode = celt_alloc(sizeof(CELTMode)); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 334 | if (mode==NULL) |
| 335 | goto failure; |
| 336 | mode->marker_start = MODEPARTIAL; |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 337 | mode->Fs = Fs; |
Jean-Marc Valin | 8b2a592 | 2008-02-29 00:32:51 +1100 | [diff] [blame] | 338 | mode->ePredCoef = QCONST16(.8f,15); |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 339 | |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 340 | /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis |
| 341 | is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should |
| 342 | approximate that. */ |
| 343 | if(Fs < 12000) /* 8 kHz */ |
| 344 | { |
| 345 | mode->preemph[0] = QCONST16(.35f, 15); |
| 346 | mode->preemph[1] = -QCONST16(.18f, 15); |
| 347 | mode->preemph[2] = QCONST16(.272f, SIG_SHIFT); |
| 348 | mode->preemph[3] = QCONST16(3.6765f, 13); |
| 349 | } else if(Fs < 24000) /* 16 kHz */ |
| 350 | { |
| 351 | mode->preemph[0] = QCONST16(.6f, 15); |
| 352 | mode->preemph[1] = -QCONST16(.18f, 15); |
| 353 | mode->preemph[2] = QCONST16(.4425f, SIG_SHIFT); |
| 354 | mode->preemph[3] = QCONST16(2.259887f, 13); |
| 355 | } else if(Fs < 40000) /* 32 kHz */ |
| 356 | { |
| 357 | mode->preemph[0] = QCONST16(.78f, 15); |
| 358 | mode->preemph[1] = -QCONST16(.1f, 15); |
| 359 | mode->preemph[2] = QCONST16(.75f, SIG_SHIFT); |
| 360 | mode->preemph[3] = QCONST16(1.33333333f, 13); |
| 361 | } else /* 48 kHz */ |
| 362 | { |
| 363 | mode->preemph[0] = QCONST16(.85f, 15); |
| 364 | mode->preemph[1] = QCONST16(.0f, 15); |
| 365 | mode->preemph[2] = QCONST16(1.f, SIG_SHIFT); |
| 366 | mode->preemph[3] = QCONST16(1.f, 13); |
| 367 | } |
| 368 | |
Jean-Marc Valin | a2546bb | 2010-07-16 12:00:00 -0400 | [diff] [blame] | 369 | if ((celt_int32)frame_size*75 >= Fs && (frame_size%16)==0) |
Jean-Marc Valin | e6d832a | 2009-07-08 22:21:31 -0400 | [diff] [blame] | 370 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 371 | LM = 3; |
Jean-Marc Valin | a2546bb | 2010-07-16 12:00:00 -0400 | [diff] [blame] | 372 | } else if ((celt_int32)frame_size*150 >= Fs && (frame_size%8)==0) |
Jean-Marc Valin | 16ca18b | 2008-06-18 23:44:48 +1000 | [diff] [blame] | 373 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 374 | LM = 2; |
Jean-Marc Valin | a2546bb | 2010-07-16 12:00:00 -0400 | [diff] [blame] | 375 | } else if ((celt_int32)frame_size*300 >= Fs && (frame_size%4)==0) |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 376 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 377 | LM = 1; |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 378 | } else |
| 379 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 380 | LM = 0; |
Jean-Marc Valin | 16ca18b | 2008-06-18 23:44:48 +1000 | [diff] [blame] | 381 | } |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 382 | |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 383 | mode->maxLM = LM; |
| 384 | mode->nbShortMdcts = 1<<LM; |
Jean-Marc Valin | 60ff999 | 2010-06-27 13:49:38 -0400 | [diff] [blame] | 385 | mode->shortMdctSize = frame_size/mode->nbShortMdcts; |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 386 | res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize); |
| 387 | |
| 388 | mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands); |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 389 | if (mode->eBands==NULL) |
| 390 | goto failure; |
Jean-Marc Valin | fbfddf7 | 2009-07-19 21:07:12 -0400 | [diff] [blame] | 391 | |
Jean-Marc Valin | e949b72 | 2010-05-07 07:48:07 -0400 | [diff] [blame] | 392 | mode->pitchEnd = 4000*(celt_int32)mode->shortMdctSize/Fs; |
Jean-Marc Valin | 4834c92 | 2009-09-28 19:17:34 -0400 | [diff] [blame] | 393 | |
Jean-Marc Valin | 08192e3 | 2009-07-02 13:28:55 -0400 | [diff] [blame] | 394 | /* Overlap must be divisible by 4 */ |
Jean-Marc Valin | 16ca18b | 2008-06-18 23:44:48 +1000 | [diff] [blame] | 395 | if (mode->nbShortMdcts > 1) |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 396 | mode->overlap = (mode->shortMdctSize>>2)<<2; |
Jean-Marc Valin | 16ca18b | 2008-06-18 23:44:48 +1000 | [diff] [blame] | 397 | else |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 398 | mode->overlap = (frame_size>>3)<<2; |
| 399 | |
Jean-Marc Valin | 7f1c942 | 2010-04-29 11:24:11 -0400 | [diff] [blame] | 400 | |
| 401 | compute_allocation_table(mode, res); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 402 | if (mode->allocVectors==NULL) |
| 403 | goto failure; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 404 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 405 | window = (celt_word16*)celt_alloc(mode->overlap*sizeof(celt_word16)); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 406 | if (window==NULL) |
| 407 | goto failure; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 408 | |
Jean-Marc Valin | f28062f | 2008-03-03 13:24:01 +1100 | [diff] [blame] | 409 | #ifndef FIXED_POINT |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 410 | for (i=0;i<mode->overlap;i++) |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 411 | 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] | 412 | #else |
| 413 | for (i=0;i<mode->overlap;i++) |
Jean-Marc Valin | 8974f00 | 2010-03-20 00:41:39 -0400 | [diff] [blame] | 414 | 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] | 415 | #endif |
Jean-Marc Valin | 3dbc1d0 | 2008-03-08 15:21:24 +1100 | [diff] [blame] | 416 | mode->window = window; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 417 | |
Jean-Marc Valin | cae30df | 2010-05-21 00:26:03 -0400 | [diff] [blame] | 418 | mode->bits = mode->_bits+1; |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 419 | for (i=0;(1<<i)<=mode->nbShortMdcts;i++) |
Jean-Marc Valin | cae30df | 2010-05-21 00:26:03 -0400 | [diff] [blame] | 420 | { |
| 421 | mode->bits[i] = (const celt_int16 **)compute_alloc_cache(mode, 1<<i); |
| 422 | if (mode->bits[i]==NULL) |
| 423 | goto failure; |
| 424 | } |
| 425 | mode->bits[-1] = (const celt_int16 **)compute_alloc_cache(mode, 0); |
| 426 | if (mode->bits[-1]==NULL) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 427 | goto failure; |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 428 | |
Jean-Marc Valin | f400a3c | 2010-04-05 23:58:44 -0400 | [diff] [blame] | 429 | logN = (celt_int16*)celt_alloc(mode->nbEBands*sizeof(celt_int16)); |
| 430 | if (logN==NULL) |
| 431 | goto failure; |
| 432 | |
| 433 | for (i=0;i<mode->nbEBands;i++) |
Jean-Marc Valin | aead79b | 2010-05-11 07:34:24 -0400 | [diff] [blame] | 434 | 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] | 435 | mode->logN = logN; |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 436 | #endif /* !STATIC_MODES */ |
Jean-Marc Valin | 640f7fd | 2009-06-21 09:47:51 -0400 | [diff] [blame] | 437 | |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 438 | clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, LM); |
| 439 | if ((mode->mdct.trig==NULL) |
Jean-Marc Valin | 9714f66 | 2009-08-12 20:29:57 -0400 | [diff] [blame] | 440 | #ifndef ENABLE_TI_DSPLIB55 |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 441 | || (mode->mdct.kfft==NULL) |
Jean-Marc Valin | 9714f66 | 2009-08-12 20:29:57 -0400 | [diff] [blame] | 442 | #endif |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 443 | ) |
| 444 | goto failure; |
| 445 | |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 446 | mode->prob = quant_prob_alloc(mode); |
| 447 | if (mode->prob==NULL) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 448 | goto failure; |
Jean-Marc Valin | bf2d648 | 2008-05-23 16:57:34 +1000 | [diff] [blame] | 449 | |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 450 | mode->marker_start = MODEVALID; |
| 451 | mode->marker_end = MODEVALID; |
Jean-Marc Valin | 680a9ec | 2008-03-10 14:52:18 +1100 | [diff] [blame] | 452 | if (error) |
| 453 | *error = CELT_OK; |
Jean-Marc Valin | 4991a56 | 2008-02-18 13:37:40 +1100 | [diff] [blame] | 454 | return mode; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 455 | failure: |
| 456 | if (error) |
| 457 | *error = CELT_INVALID_MODE; |
| 458 | if (mode!=NULL) |
| 459 | celt_mode_destroy(mode); |
| 460 | return NULL; |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 461 | } |
Jean-Marc Valin | 2a8c371 | 2008-02-18 12:16:41 +1100 | [diff] [blame] | 462 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 463 | void celt_mode_destroy(CELTMode *mode) |
Jean-Marc Valin | 81a8295 | 2008-02-17 22:41:29 +1100 | [diff] [blame] | 464 | { |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 465 | int i, m; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 466 | const celt_int16 *prevPtr = NULL; |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 467 | if (mode == NULL) |
| 468 | { |
| 469 | celt_warning("NULL passed to celt_mode_destroy"); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED) |
| 474 | { |
| 475 | celt_warning("Freeing a mode which has already been freed"); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | if (mode->marker_start != MODEVALID && mode->marker_start != MODEPARTIAL) |
| 480 | { |
| 481 | celt_warning("This is not a valid CELT mode structure"); |
| 482 | return; |
| 483 | } |
| 484 | mode->marker_start = MODEFREED; |
Jean-Marc Valin | 5588d52 | 2008-03-10 15:07:58 +1100 | [diff] [blame] | 485 | #ifndef STATIC_MODES |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 486 | for (m=0;(1<<m)<=mode->nbShortMdcts;m++) |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 487 | { |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 488 | if (mode->bits[m]!=NULL) |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 489 | { |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 490 | for (i=0;i<mode->nbEBands;i++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 491 | { |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 492 | if (mode->bits[m][i] != prevPtr) |
| 493 | { |
| 494 | prevPtr = mode->bits[m][i]; |
| 495 | celt_free((int*)mode->bits[m][i]); |
| 496 | } |
| 497 | } |
Jean-Marc Valin | 25358cd | 2008-02-19 12:21:32 +1100 | [diff] [blame] | 498 | } |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 499 | celt_free((celt_int16**)mode->bits[m]); |
| 500 | } |
Jean-Marc Valin | cae30df | 2010-05-21 00:26:03 -0400 | [diff] [blame] | 501 | if (mode->bits[-1]!=NULL) |
| 502 | { |
| 503 | for (i=0;i<mode->nbEBands;i++) |
| 504 | { |
| 505 | if (mode->bits[-1][i] != prevPtr) |
| 506 | { |
| 507 | prevPtr = mode->bits[-1][i]; |
| 508 | celt_free((int*)mode->bits[-1][i]); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | celt_free((celt_int16**)mode->bits[-1]); |
| 513 | |
Jean-Marc Valin | 31bec96 | 2010-04-07 18:30:28 -0400 | [diff] [blame] | 514 | celt_free((celt_int16*)mode->eBands); |
| 515 | celt_free((celt_int16*)mode->allocVectors); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 516 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 517 | celt_free((celt_word16*)mode->window); |
Jean-Marc Valin | 31bec96 | 2010-04-07 18:30:28 -0400 | [diff] [blame] | 518 | celt_free((celt_int16*)mode->logN); |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 519 | |
Jean-Marc Valin | 70720a3 | 2008-04-19 21:39:26 +1000 | [diff] [blame] | 520 | #endif |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 521 | clt_mdct_clear(&mode->mdct); |
Jean-Marc Valin | 073d0bc | 2010-05-05 21:37:53 -0400 | [diff] [blame] | 522 | |
Jean-Marc Valin | 4ce9205 | 2008-04-23 13:42:10 +1000 | [diff] [blame] | 523 | quant_prob_free(mode->prob); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 524 | mode->marker_end = MODEFREED; |
Jean-Marc Valin | 949902f | 2008-03-11 10:43:06 +1100 | [diff] [blame] | 525 | celt_free((CELTMode *)mode); |
Jean-Marc Valin | 2ca8fc3 | 2008-02-18 16:27:49 +1100 | [diff] [blame] | 526 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 527 | |
| 528 | int check_mode(const CELTMode *mode) |
| 529 | { |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 530 | if (mode==NULL) |
| 531 | return CELT_INVALID_MODE; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 532 | if (mode->marker_start == MODEVALID && mode->marker_end == MODEVALID) |
| 533 | return CELT_OK; |
| 534 | if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED) |
| 535 | celt_warning("Using a mode that has already been freed"); |
| 536 | else |
| 537 | celt_warning("This is not a valid CELT mode"); |
| 538 | return CELT_INVALID_MODE; |
| 539 | } |