1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 #include "os_support.h"
40 #include "stack_alloc.h"
43 #include "static_modes.c"
46 #define MODEVALID 0xa110ca7e
47 #define MODEFREED 0xb10cf8ee
50 #define M_PI 3.141592653
54 int EXPORT celt_mode_info(const CELTMode *mode, int request, celt_int32_t *value)
58 case CELT_GET_FRAME_SIZE:
59 *value = mode->mdctSize;
61 case CELT_GET_LOOKAHEAD:
62 *value = mode->overlap;
64 case CELT_GET_NB_CHANNELS:
65 *value = mode->nbChannels;
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 */
80 static const celt_int16_t bark_freq[BARK_BANDS+1] = {
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,
88 static const celt_int16_t pitch_freq[PBANDS+1] ={0, 345, 689, 1034, 1378, 2067, 3273, 5340, 6374};
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 */
92 #define BITALLOC_SIZE 10
93 static const int band_allocation[BARK_BANDS*BITALLOC_SIZE] =
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,
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,
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,
107 static celt_int16_t *compute_ebands(celt_int32_t Fs, int frame_size, int *nbEBands)
109 celt_int16_t *eBands;
110 int i, res, min_width, lin, low, high;
111 res = (Fs+frame_size)/(2*frame_size);
112 min_width = MIN_BINS*res;
113 /*printf ("min_width = %d\n", min_width);*/
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)
120 /*printf ("lin = %d (%d Hz)\n", lin, bark_freq[lin]);*/
121 low = ((bark_freq[lin]/res)+(MIN_BINS-1))/MIN_BINS;
122 high = BARK_BANDS-lin;
123 *nbEBands = low+high;
124 eBands = celt_alloc(sizeof(celt_int16_t)*(*nbEBands+2));
126 /* Linear spacing (min_width) */
128 eBands[i] = MIN_BINS*i;
129 /* Spacing follows critical bands */
131 eBands[i+low] = (bark_freq[lin+i]+res/2)/res;
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;
136 eBands[*nbEBands] = (bark_freq[BARK_BANDS]+res/2)/res;
137 eBands[*nbEBands+1] = frame_size;
138 if (eBands[*nbEBands] > eBands[*nbEBands+1])
139 eBands[*nbEBands] = eBands[*nbEBands+1];
141 /* FIXME: Remove last band if too small */
142 /*for (i=0;i<*nbEBands+2;i++)
143 printf("%d ", eBands[i]);
148 static void compute_pbands(CELTMode *mode, int res)
151 celt_int16_t *pBands;
152 pBands=celt_alloc(sizeof(celt_int16_t)*(PBANDS+2));
153 mode->nbPBands = PBANDS;
154 for (i=0;i<PBANDS+1;i++)
156 pBands[i] = (pitch_freq[i]+res/2)/res;
157 if (pBands[i] < mode->eBands[i])
158 pBands[i] = mode->eBands[i];
160 pBands[PBANDS+1] = mode->eBands[mode->nbEBands+1];
161 for (i=1;i<mode->nbPBands+1;i++)
164 for (j=0;j<mode->nbEBands;j++)
165 if (mode->eBands[j] <= pBands[i] && mode->eBands[j+1] > pBands[i])
167 /*printf ("%d %d\n", i, j);*/
168 if (mode->eBands[j] != pBands[i])
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];
174 pBands[i] = mode->eBands[j+1];
177 /*for (i=0;i<mode->nbPBands+2;i++)
178 printf("%d ", pBands[i]);
180 mode->pBands = pBands;
181 mode->pitchEnd = pBands[PBANDS];
184 static void compute_allocation_table(CELTMode *mode, int res)
187 celt_int16_t *allocVectors;
189 mode->nbAllocVectors = BITALLOC_SIZE;
190 allocVectors = celt_alloc(sizeof(celt_int16_t)*(BITALLOC_SIZE*mode->nbEBands));
191 for (i=0;i<BITALLOC_SIZE;i++)
194 for (j=0;j<BARK_BANDS;j++)
197 edge = mode->eBands[eband+1]*res;
198 if (edge < bark_freq[j+1])
201 num = band_allocation[i*BARK_BANDS+j] * (edge-bark_freq[j]);
202 den = bark_freq[j+1]-bark_freq[j];
203 low = (num+den/2)/den;
204 allocVectors[i*mode->nbEBands+eband] += low;
206 allocVectors[i*mode->nbEBands+eband] += band_allocation[i*BARK_BANDS+j]-low;
208 allocVectors[i*mode->nbEBands+eband] += band_allocation[i*BARK_BANDS+j];
212 /*for (i=0;i<BITALLOC_SIZE;i++)
214 for (j=0;j<mode->nbEBands;j++)
215 printf ("%2d ", allocVectors[i*mode->nbEBands+j]);
218 mode->allocVectors = allocVectors;
221 #endif /* STATIC_MODES */
223 CELTMode EXPORT *celt_mode_create(celt_int32_t Fs, int channels, int frame_size, int lookahead, int *error)
226 const CELTMode *m = NULL;
230 for (i=0;i<TOTAL_MODES;i++)
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)
237 m = static_mode_list[i];
243 celt_warning("Mode not included as part of the static modes");
245 *error = CELT_BAD_ARG;
248 mode = (CELTMode*)celt_alloc(sizeof(CELTMode));
249 CELT_COPY(mode, m, 1);
254 celt_word16_t *window;
257 /* The good thing here is that permutation of the arguments will automatically be invalid */
259 if (Fs < 32000 || Fs > 64000)
261 celt_warning("Sampling rate must be between 32 kHz and 64 kHz");
263 *error = CELT_BAD_ARG;
266 if (channels < 0 || channels > 2)
268 celt_warning("Only mono and stereo supported");
270 *error = CELT_BAD_ARG;
273 if (frame_size < 64 || frame_size > 256 || frame_size%2!=0)
275 celt_warning("Only even frame sizes between 64 and 256 are supported");
277 *error = CELT_BAD_ARG;
280 if (lookahead < 32 || lookahead > frame_size)
282 celt_warning("The overlap must be between 32 and the frame size");
284 *error = CELT_BAD_ARG;
287 res = (Fs+frame_size)/(2*frame_size);
289 mode = celt_alloc(sizeof(CELTMode));
291 mode->overlap = lookahead;
292 mode->mdctSize = frame_size;
293 mode->nbChannels = channels;
294 mode->eBands = compute_ebands(Fs, frame_size, &mode->nbEBands);
295 compute_pbands(mode, res);
296 mode->ePredCoef = QCONST16(.8f,15);
298 compute_allocation_table(mode, res);
299 /*printf ("%d bands\n", mode->nbEBands);*/
301 window = (celt_word16_t*)celt_alloc(mode->overlap*sizeof(celt_word16_t));
304 for (i=0;i<mode->overlap;i++)
305 window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap));
307 for (i=0;i<mode->overlap;i++)
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)));
310 mode->window = window;
312 compute_alloc_cache(mode);
314 psydecay_init(&mode->psy, MAX_PERIOD/2, mode->Fs);
316 mode->marker_start = MODEVALID;
317 mode->marker_end = MODEVALID;
318 #endif /* !STATIC_MODES */
319 mdct_init(&mode->mdct, 2*mode->mdctSize);
325 void EXPORT celt_mode_destroy(CELTMode *mode)
329 const celt_int16_t *prevPtr = NULL;
330 for (i=0;i<mode->nbEBands;i++)
332 if (mode->bits[i] != prevPtr)
334 prevPtr = mode->bits[i];
335 celt_free((int*)mode->bits[i]);
338 celt_free((int**)mode->bits);
339 if (check_mode(mode) != CELT_OK)
341 celt_free((int*)mode->eBands);
342 celt_free((int*)mode->pBands);
343 celt_free((int*)mode->allocVectors);
345 celt_free((celt_word16_t*)mode->window);
347 mode->marker_start = MODEFREED;
348 mode->marker_end = MODEFREED;
349 psydecay_clear(&mode->psy);
351 mdct_clear(&mode->mdct);
352 celt_free((CELTMode *)mode);
355 int check_mode(const CELTMode *mode)
357 if (mode->marker_start == MODEVALID && mode->marker_end == MODEVALID)
359 if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED)
360 celt_warning("Using a mode that has already been freed");
362 celt_warning("This is not a valid CELT mode");
363 return CELT_INVALID_MODE;