blob: 9baf2fe8b35da1ed13a77ddeffa3333b621c3d7b [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* 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 Valinecb36a32007-12-05 01:31:49 +11005/*
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 Valin02fa9132008-02-20 12:09:29 +110034#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
Jean-Marc Valin65d57e62008-02-18 15:49:37 +110038#include "celt.h"
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110039#include "modes.h"
Jean-Marc Valin472a5f02008-02-19 13:12:32 +110040#include "rate.h"
Jean-Marc Valin81a82952008-02-17 22:41:29 +110041#include "os_support.h"
Jean-Marc Valinf7cec832008-04-18 17:29:56 +100042#include "stack_alloc.h"
Jean-Marc Valin4ce92052008-04-23 13:42:10 +100043#include "quant_bands.h"
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110044
Jean-Marc Valinf39e8692008-03-10 12:13:23 +110045#ifdef STATIC_MODES
Jean-Marc Valinb18ec0b2008-04-11 04:07:52 +100046#include "static_modes.c"
Jean-Marc Valinf39e8692008-03-10 12:13:23 +110047#endif
48
Gregory Maxwelldc67fa92009-06-04 17:17:35 -040049#define MODEVALID 0xa110ca7e
50#define MODEPARTIAL 0x7eca10a1
51#define MODEFREED 0xb10cf8ee
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +110052
Jean-Marc Valind748cd52008-03-01 07:27:03 +110053#ifndef M_PI
54#define M_PI 3.141592653
55#endif
56
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +110057
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040058int celt_mode_info(const CELTMode *mode, int request, celt_int32 *value)
Jean-Marc Valinf997ad52008-01-31 16:47:16 +110059{
Gregory Maxwelldc67fa92009-06-04 17:17:35 -040060 if (check_mode(mode) != CELT_OK)
61 return CELT_INVALID_MODE;
Jean-Marc Valinf997ad52008-01-31 16:47:16 +110062 switch (request)
63 {
Jean-Marc Valinf997ad52008-01-31 16:47:16 +110064 case CELT_GET_LOOKAHEAD:
65 *value = mode->overlap;
66 break;
Jean-Marc Valin59093c02008-05-15 21:53:27 +100067 case CELT_GET_BITSTREAM_VERSION:
68 *value = CELT_BITSTREAM_VERSION;
69 break;
Gregory Maxwelld9458cd2009-05-30 17:04:02 -040070 case CELT_GET_SAMPLE_RATE:
71 *value = mode->Fs;
72 break;
Jean-Marc Valincb7a2a32008-02-11 16:44:48 +110073 default:
Jean-Marc Valinb6f90612008-10-05 22:39:13 -040074 return CELT_UNIMPLEMENTED;
Jean-Marc Valinf997ad52008-01-31 16:47:16 +110075 }
Jean-Marc Valincb7a2a32008-02-11 16:44:48 +110076 return CELT_OK;
Jean-Marc Valinf997ad52008-01-31 16:47:16 +110077}
78
Jean-Marc Valin5588d522008-03-10 15:07:58 +110079#ifndef STATIC_MODES
80
Jean-Marc Valin17683eb2008-02-18 21:45:19 +110081/* 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 Valin81a82952008-02-17 22:41:29 +110083#define BARK_BANDS 25
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040084static const celt_int16 bark_freq[BARK_BANDS+1] = {
Jean-Marc Valin17683eb2008-02-18 21:45:19 +110085 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 Valin75e9c862008-02-18 17:04:15 +110092/* 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 Valin47c248a2008-04-26 08:16:12 +100094
Jean-Marc Valin9c709062008-08-03 22:07:06 -040095#define BITALLOC_SIZE 12
Jean-Marc Valin81a82952008-02-17 22:41:29 +110096
Jean-Marc Valin12e851d2010-06-03 08:12:11 -040097static 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 Valinffe50612010-06-04 00:13:19 -0400101static 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 Valin12e851d2010-06-03 08:12:11 -0400103 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 Valin20639c42010-05-19 16:10:12 -0400116
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400117static celt_int16 *compute_ebands(celt_int32 Fs, int frame_size, int res, int *nbEBands)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100118{
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400119 celt_int16 *eBands;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400120 int i, lin, low, high, nBark, offset=0;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100121
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400122 if (Fs == 400*(celt_int32)frame_size && Fs >= 40000)
123 {
124 *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
125 eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2));
126 for (i=0;i<*nbEBands+2;i++)
127 eBands[i] = eband5ms[i];
128 eBands[*nbEBands+1] = frame_size;
129 return eBands;
130 }
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400131 /* Find the number of critical bands supported by our sampling rate */
132 for (nBark=1;nBark<BARK_BANDS;nBark++)
133 if (bark_freq[nBark+1]*2 >= Fs)
134 break;
135
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100136 /* Find where the linear part ends (i.e. where the spacing is more than min_width */
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400137 for (lin=0;lin<nBark;lin++)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400138 if (bark_freq[lin+1]-bark_freq[lin] >= res)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100139 break;
Jean-Marc Valin4fb95682010-04-20 23:30:22 -0400140
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400141 low = (bark_freq[lin]+res/2)/res;
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400142 high = nBark-lin;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100143 *nbEBands = low+high;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400144 eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2));
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100145
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400146 if (eBands==NULL)
147 return NULL;
148
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100149 /* Linear spacing (min_width) */
150 for (i=0;i<low;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400151 eBands[i] = i;
Jean-Marc Valinbe8d1252010-04-21 18:09:07 -0400152 if (low>0)
153 offset = eBands[low-1]*res - bark_freq[lin-1];
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100154 /* Spacing follows critical bands */
155 for (i=0;i<high;i++)
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400156 {
157 int target = bark_freq[lin+i];
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400158 eBands[i+low] = (target+(offset+res)/2)/res;
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400159 offset = eBands[i+low]*res - target;
160 }
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100161 /* Enforce the minimum spacing at the boundary */
162 for (i=0;i<*nbEBands;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400163 if (eBands[i] < i)
164 eBands[i] = i;
165 eBands[*nbEBands] = (bark_freq[nBark]+res/2)/res;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100166 eBands[*nbEBands+1] = frame_size;
167 if (eBands[*nbEBands] > eBands[*nbEBands+1])
168 eBands[*nbEBands] = eBands[*nbEBands+1];
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400169 for (i=1;i<*nbEBands-1;i++)
170 {
171 if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
172 {
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400173 eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400174 }
175 }
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400176 /*for (i=0;i<=*nbEBands+1;i++)
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400177 printf ("%d ", eBands[i]);
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400178 printf ("\n");
179 exit(1);*/
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100180 /* FIXME: Remove last band if too small */
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100181 return eBands;
182}
183
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400184static void compute_allocation_table(CELTMode *mode, int res)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100185{
Jean-Marc Valin2f6c5fe2010-06-28 17:22:37 -0400186 int i, j;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400187 unsigned char *allocVectors;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400188 int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400189
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400190 mode->nbAllocVectors = BITALLOC_SIZE;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400191 allocVectors = celt_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400192 if (allocVectors==NULL)
193 return;
194
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400195 /* Check for standard mode */
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400196 if (mode->Fs == 400*(celt_int32)mode->shortMdctSize && mode->Fs >= 40000)
197 {
198 for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++)
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400199 allocVectors[i] = band_allocation[i];
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400200 mode->allocVectors = allocVectors;
201 return;
202 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400203 /* If not the standard mode, interpolate */
204
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400205 /* Compute per-codec-band allocation from per-critical-band matrix */
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100206 for (i=0;i<BITALLOC_SIZE;i++)
207 {
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400208 celt_int32 current = 0;
Gregory Maxwellec836da2009-05-04 14:55:40 -0400209 int eband = 0;
Jean-Marc Valin2f6c5fe2010-06-28 17:22:37 -0400210 /* We may be looping over too many bands, but eband will stop being
211 incremented once we reach the last band */
212 for (j=0;j<maxBands;j++)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100213 {
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400214 int edge, low, high;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400215 celt_int32 alloc;
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400216 alloc = band_allocation[i*maxBands + j]*(mode->eBands[eband+1]-mode->eBands[eband])<<4;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400217 low = eband5ms[j]*200;
218 high = eband5ms[j+1]*200;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400219 edge = mode->eBands[eband+1]*res;
Jean-Marc Valin3ff5e4c2010-04-14 18:02:09 -0400220 while (edge <= high && eband < mode->nbEBands)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100221 {
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400222 celt_int32 num;
223 int den, bits;
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400224 int N = (mode->eBands[eband+1]-mode->eBands[eband]);
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400225 num = alloc * (edge-low);
226 den = high-low;
227 /* Divide with rounding */
228 bits = (2*num+den)/(2*den);
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400229 allocVectors[i*mode->nbEBands+eband] = (2*(current+bits)+(N<<4))/(2*N<<4);
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400230 /* Remove the part of the band we just allocated */
231 low = edge;
232 alloc -= bits;
233
234 /* Move to next eband */
235 current = 0;
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100236 eband++;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400237 edge = mode->eBands[eband+1]*res;
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400238 }
239 current += alloc;
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100240 }
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400241 if (eband < mode->nbEBands)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400242 {
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400243 int N = (mode->eBands[eband+1]-mode->eBands[eband]);
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400244 allocVectors[i*mode->nbEBands+eband] = (2*current+(N<<4))/(2*N<<4);
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400245 }
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100246 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400247 /*printf ("\n");
248 for (i=0;i<BITALLOC_SIZE;i++)
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400249 {
250 for (j=0;j<mode->nbEBands;j++)
251 printf ("%d ", allocVectors[i*mode->nbEBands+j]);
252 printf ("\n");
253 }
254 exit(0);*/
255
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400256 mode->allocVectors = allocVectors;
Jean-Marc Valinad637192008-05-07 13:44:39 +1000257}
258
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400259#endif /* STATIC_MODES */
260
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400261CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100262{
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000263 int i;
264#ifdef STDIN_TUNING
265 scanf("%d ", &MIN_BINS);
266 scanf("%d ", &BITALLOC_SIZE);
267 band_allocation = celt_alloc(sizeof(int)*BARK_BANDS*BITALLOC_SIZE);
268 for (i=0;i<BARK_BANDS*BITALLOC_SIZE;i++)
269 {
270 scanf("%d ", band_allocation+i);
271 }
272#endif
Jean-Marc Valinf39e8692008-03-10 12:13:23 +1100273#ifdef STATIC_MODES
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100274 const CELTMode *m = NULL;
275 CELTMode *mode=NULL;
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000276 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400277#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
278 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400279 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400280#endif
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100281 for (i=0;i<TOTAL_MODES;i++)
282 {
283 if (Fs == static_mode_list[i]->Fs &&
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400284 frame_size == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100285 {
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100286 m = static_mode_list[i];
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100287 break;
288 }
289 }
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100290 if (m == NULL)
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100291 {
292 celt_warning("Mode not included as part of the static modes");
293 if (error)
294 *error = CELT_BAD_ARG;
295 return NULL;
296 }
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100297 mode = (CELTMode*)celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400298 if (mode==NULL)
299 goto failure;
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100300 CELT_COPY(mode, m, 1);
Jean-Marc Valinf477d582010-06-27 09:56:26 -0400301 mode->bits = mode->_bits+1;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400302 mode->marker_start = MODEPARTIAL;
Jean-Marc Valinf39e8692008-03-10 12:13:23 +1100303#else
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100304 int res;
Gregory Maxwell84966952009-06-30 01:06:04 -0400305 CELTMode *mode=NULL;
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400306 celt_word16 *window;
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400307 celt_int16 *logN;
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000308 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400309#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
310 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400311 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400312#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100313
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100314 /* The good thing here is that permutation of the arguments will automatically be invalid */
315
Gregory Maxwell8ed86582008-12-02 12:06:14 -0500316 if (Fs < 32000 || Fs > 96000)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100317 {
Gregory Maxwell8ed86582008-12-02 12:06:14 -0500318 celt_warning("Sampling rate must be between 32 kHz and 96 kHz");
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100319 if (error)
320 *error = CELT_BAD_ARG;
321 return NULL;
322 }
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400323 if (frame_size < 64 || frame_size > 1024 || frame_size%2!=0)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100324 {
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400325 celt_warning("Only even frame sizes from 64 to 1024 are supported");
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100326 if (error)
327 *error = CELT_BAD_ARG;
328 return NULL;
329 }
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100330
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100331 mode = celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400332 if (mode==NULL)
333 goto failure;
334 mode->marker_start = MODEPARTIAL;
Jean-Marc Valine6b74652008-02-20 18:01:08 +1100335 mode->Fs = Fs;
Jean-Marc Valin8b2a5922008-02-29 00:32:51 +1100336 mode->ePredCoef = QCONST16(.8f,15);
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400337
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400338 if (frame_size >= 640 && (frame_size%16)==0)
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400339 {
340 mode->nbShortMdcts = 8;
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400341 } else if (frame_size >= 320 && (frame_size%8)==0)
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000342 {
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400343 mode->nbShortMdcts = 4;
Jean-Marc Valinc5d00a02010-05-26 11:27:32 -0400344 } else if (frame_size >= 160 && (frame_size%4)==0)
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400345 {
346 mode->nbShortMdcts = 2;
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400347 } else
348 {
349 mode->nbShortMdcts = 1;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000350 }
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400351
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400352 mode->shortMdctSize = frame_size/mode->nbShortMdcts;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400353 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
354
355 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400356 if (mode->eBands==NULL)
357 goto failure;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400358
Jean-Marc Valine949b722010-05-07 07:48:07 -0400359 mode->pitchEnd = 4000*(celt_int32)mode->shortMdctSize/Fs;
Jean-Marc Valin4834c922009-09-28 19:17:34 -0400360
Jean-Marc Valin08192e32009-07-02 13:28:55 -0400361 /* Overlap must be divisible by 4 */
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000362 if (mode->nbShortMdcts > 1)
Jean-Marc Valince4dd362010-05-07 07:45:18 -0400363 mode->overlap = (mode->shortMdctSize>>2)<<2;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000364 else
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400365 mode->overlap = (frame_size>>3)<<2;
366
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400367
368 compute_allocation_table(mode, res);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400369 if (mode->allocVectors==NULL)
370 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100371
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400372 window = (celt_word16*)celt_alloc(mode->overlap*sizeof(celt_word16));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400373 if (window==NULL)
374 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100375
Jean-Marc Valinf28062f2008-03-03 13:24:01 +1100376#ifndef FIXED_POINT
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100377 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100378 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 Valinf28062f2008-03-03 13:24:01 +1100379#else
380 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin8974f002010-03-20 00:41:39 -0400381 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 Valinf28062f2008-03-03 13:24:01 +1100382#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100383 mode->window = window;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100384
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400385 mode->bits = mode->_bits+1;
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400386 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400387 {
388 mode->bits[i] = (const celt_int16 **)compute_alloc_cache(mode, 1<<i);
389 if (mode->bits[i]==NULL)
390 goto failure;
391 }
392 mode->bits[-1] = (const celt_int16 **)compute_alloc_cache(mode, 0);
393 if (mode->bits[-1]==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400394 goto failure;
Jean-Marc Valinb76ee702008-03-10 15:42:35 +1100395
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400396 logN = (celt_int16*)celt_alloc(mode->nbEBands*sizeof(celt_int16));
397 if (logN==NULL)
398 goto failure;
399
400 for (i=0;i<mode->nbEBands;i++)
Jean-Marc Valinaead79b2010-05-11 07:34:24 -0400401 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400402 mode->logN = logN;
Jean-Marc Valinb76ee702008-03-10 15:42:35 +1100403#endif /* !STATIC_MODES */
Jean-Marc Valin640f7fd2009-06-21 09:47:51 -0400404
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400405 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
406 {
407 clt_mdct_init(&mode->mdct[i], 2*mode->shortMdctSize<<i);
408 if ((mode->mdct[i].trig==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400409#ifndef ENABLE_TI_DSPLIB55
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400410 || (mode->mdct[i].kfft==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400411#endif
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400412 )
413 goto failure;
414 }
415 mode->prob = quant_prob_alloc(mode);
416 if (mode->prob==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400417 goto failure;
Jean-Marc Valinbf2d6482008-05-23 16:57:34 +1000418
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400419 mode->marker_start = MODEVALID;
420 mode->marker_end = MODEVALID;
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100421 if (error)
422 *error = CELT_OK;
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100423 return mode;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400424failure:
425 if (error)
426 *error = CELT_INVALID_MODE;
427 if (mode!=NULL)
428 celt_mode_destroy(mode);
429 return NULL;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100430}
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100431
Peter Kirk19f9dc92008-06-06 14:38:38 +0200432void celt_mode_destroy(CELTMode *mode)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100433{
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400434 int i, m;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400435 const celt_int16 *prevPtr = NULL;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400436 if (mode == NULL)
437 {
438 celt_warning("NULL passed to celt_mode_destroy");
439 return;
440 }
441
442 if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED)
443 {
444 celt_warning("Freeing a mode which has already been freed");
445 return;
446 }
447
448 if (mode->marker_start != MODEVALID && mode->marker_start != MODEPARTIAL)
449 {
450 celt_warning("This is not a valid CELT mode structure");
451 return;
452 }
453 mode->marker_start = MODEFREED;
Jean-Marc Valin5588d522008-03-10 15:07:58 +1100454#ifndef STATIC_MODES
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400455 for (m=0;(1<<m)<=mode->nbShortMdcts;m++)
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100456 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400457 if (mode->bits[m]!=NULL)
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100458 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400459 for (i=0;i<mode->nbEBands;i++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400460 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400461 if (mode->bits[m][i] != prevPtr)
462 {
463 prevPtr = mode->bits[m][i];
464 celt_free((int*)mode->bits[m][i]);
465 }
466 }
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100467 }
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400468 celt_free((celt_int16**)mode->bits[m]);
469 }
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400470 if (mode->bits[-1]!=NULL)
471 {
472 for (i=0;i<mode->nbEBands;i++)
473 {
474 if (mode->bits[-1][i] != prevPtr)
475 {
476 prevPtr = mode->bits[-1][i];
477 celt_free((int*)mode->bits[-1][i]);
478 }
479 }
480 }
481 celt_free((celt_int16**)mode->bits[-1]);
482
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400483 celt_free((celt_int16*)mode->eBands);
484 celt_free((celt_int16*)mode->allocVectors);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400485
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400486 celt_free((celt_word16*)mode->window);
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400487 celt_free((celt_int16*)mode->logN);
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100488
Jean-Marc Valin70720a32008-04-19 21:39:26 +1000489#endif
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400490 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
491 clt_mdct_clear(&mode->mdct[i]);
492
Jean-Marc Valin4ce92052008-04-23 13:42:10 +1000493 quant_prob_free(mode->prob);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400494 mode->marker_end = MODEFREED;
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100495 celt_free((CELTMode *)mode);
Jean-Marc Valin2ca8fc32008-02-18 16:27:49 +1100496}
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100497
498int check_mode(const CELTMode *mode)
499{
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400500 if (mode==NULL)
501 return CELT_INVALID_MODE;
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100502 if (mode->marker_start == MODEVALID && mode->marker_end == MODEVALID)
503 return CELT_OK;
504 if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED)
505 celt_warning("Using a mode that has already been freed");
506 else
507 celt_warning("This is not a valid CELT mode");
508 return CELT_INVALID_MODE;
509}