blob: 6e81a2a4be8c164f07e6762ddfb83a9e82601fe2 [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{
Gregory Maxwellec836da2009-05-04 14:55:40 -0400186 int i, j, nBark;
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
204 /* If not the standard mode, interpolate */
205
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400206 /* Find the number of critical bands supported by our sampling rate */
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400207 for (nBark=1;nBark<maxBands;nBark++)
208 if (eband5ms[j+1]*400 >= mode->Fs)
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400209 break;
210
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400211 /* Compute per-codec-band allocation from per-critical-band matrix */
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100212 for (i=0;i<BITALLOC_SIZE;i++)
213 {
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400214 celt_int32 current = 0;
Gregory Maxwellec836da2009-05-04 14:55:40 -0400215 int eband = 0;
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400216 for (j=0;j<nBark;j++)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100217 {
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400218 int edge, low, high;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400219 celt_int32 alloc;
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400220 alloc = band_allocation[i*maxBands + j]*(mode->eBands[eband+1]-mode->eBands[eband])<<4;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400221 low = eband5ms[j]*200;
222 high = eband5ms[j+1]*200;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400223 edge = mode->eBands[eband+1]*res;
Jean-Marc Valin3ff5e4c2010-04-14 18:02:09 -0400224 while (edge <= high && eband < mode->nbEBands)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100225 {
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400226 celt_int32 num;
227 int den, bits;
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400228 int N = (mode->eBands[eband+1]-mode->eBands[eband]);
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400229 num = alloc * (edge-low);
230 den = high-low;
231 /* Divide with rounding */
232 bits = (2*num+den)/(2*den);
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400233 allocVectors[i*mode->nbEBands+eband] = (2*(current+bits)+(N<<4))/(2*N<<4);
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400234 /* Remove the part of the band we just allocated */
235 low = edge;
236 alloc -= bits;
237
238 /* Move to next eband */
239 current = 0;
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100240 eband++;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400241 edge = mode->eBands[eband+1]*res;
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400242 }
243 current += alloc;
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100244 }
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400245 if (eband < mode->nbEBands)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400246 {
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400247 int N = (mode->eBands[eband+1]-mode->eBands[eband]);
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400248 allocVectors[i*mode->nbEBands+eband] = (2*current+(N<<4))/(2*N<<4);
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400249 }
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100250 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400251 /*printf ("\n");
252 for (i=0;i<BITALLOC_SIZE;i++)
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400253 {
254 for (j=0;j<mode->nbEBands;j++)
255 printf ("%d ", allocVectors[i*mode->nbEBands+j]);
256 printf ("\n");
257 }
258 exit(0);*/
259
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400260 mode->allocVectors = allocVectors;
Jean-Marc Valinad637192008-05-07 13:44:39 +1000261}
262
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400263#endif /* STATIC_MODES */
264
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400265CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100266{
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000267 int i;
268#ifdef STDIN_TUNING
269 scanf("%d ", &MIN_BINS);
270 scanf("%d ", &BITALLOC_SIZE);
271 band_allocation = celt_alloc(sizeof(int)*BARK_BANDS*BITALLOC_SIZE);
272 for (i=0;i<BARK_BANDS*BITALLOC_SIZE;i++)
273 {
274 scanf("%d ", band_allocation+i);
275 }
276#endif
Jean-Marc Valinf39e8692008-03-10 12:13:23 +1100277#ifdef STATIC_MODES
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100278 const CELTMode *m = NULL;
279 CELTMode *mode=NULL;
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000280 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400281#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
282 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400283 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400284#endif
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100285 for (i=0;i<TOTAL_MODES;i++)
286 {
287 if (Fs == static_mode_list[i]->Fs &&
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400288 frame_size == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100289 {
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100290 m = static_mode_list[i];
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100291 break;
292 }
293 }
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100294 if (m == NULL)
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100295 {
296 celt_warning("Mode not included as part of the static modes");
297 if (error)
298 *error = CELT_BAD_ARG;
299 return NULL;
300 }
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100301 mode = (CELTMode*)celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400302 if (mode==NULL)
303 goto failure;
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100304 CELT_COPY(mode, m, 1);
Jean-Marc Valinf477d582010-06-27 09:56:26 -0400305 mode->bits = mode->_bits+1;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400306 mode->marker_start = MODEPARTIAL;
Jean-Marc Valinf39e8692008-03-10 12:13:23 +1100307#else
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100308 int res;
Gregory Maxwell84966952009-06-30 01:06:04 -0400309 CELTMode *mode=NULL;
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400310 celt_word16 *window;
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400311 celt_int16 *logN;
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000312 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400313#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
314 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400315 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400316#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100317
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100318 /* The good thing here is that permutation of the arguments will automatically be invalid */
319
Gregory Maxwell8ed86582008-12-02 12:06:14 -0500320 if (Fs < 32000 || Fs > 96000)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100321 {
Gregory Maxwell8ed86582008-12-02 12:06:14 -0500322 celt_warning("Sampling rate must be between 32 kHz and 96 kHz");
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100323 if (error)
324 *error = CELT_BAD_ARG;
325 return NULL;
326 }
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400327 if (frame_size < 64 || frame_size > 1024 || frame_size%2!=0)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100328 {
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400329 celt_warning("Only even frame sizes from 64 to 1024 are supported");
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100330 if (error)
331 *error = CELT_BAD_ARG;
332 return NULL;
333 }
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100334
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100335 mode = celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400336 if (mode==NULL)
337 goto failure;
338 mode->marker_start = MODEPARTIAL;
Jean-Marc Valine6b74652008-02-20 18:01:08 +1100339 mode->Fs = Fs;
Jean-Marc Valin8b2a5922008-02-29 00:32:51 +1100340 mode->ePredCoef = QCONST16(.8f,15);
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400341
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400342 if (frame_size >= 640 && (frame_size%16)==0)
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400343 {
344 mode->nbShortMdcts = 8;
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400345 } else if (frame_size >= 320 && (frame_size%8)==0)
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000346 {
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400347 mode->nbShortMdcts = 4;
Jean-Marc Valinc5d00a02010-05-26 11:27:32 -0400348 } else if (frame_size >= 160 && (frame_size%4)==0)
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400349 {
350 mode->nbShortMdcts = 2;
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400351 } else
352 {
353 mode->nbShortMdcts = 1;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000354 }
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400355
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400356 mode->shortMdctSize = frame_size/mode->nbShortMdcts;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400357 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
358
359 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400360 if (mode->eBands==NULL)
361 goto failure;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400362
Jean-Marc Valine949b722010-05-07 07:48:07 -0400363 mode->pitchEnd = 4000*(celt_int32)mode->shortMdctSize/Fs;
Jean-Marc Valin4834c922009-09-28 19:17:34 -0400364
Jean-Marc Valin08192e32009-07-02 13:28:55 -0400365 /* Overlap must be divisible by 4 */
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000366 if (mode->nbShortMdcts > 1)
Jean-Marc Valince4dd362010-05-07 07:45:18 -0400367 mode->overlap = (mode->shortMdctSize>>2)<<2;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000368 else
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400369 mode->overlap = (frame_size>>3)<<2;
370
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400371
372 compute_allocation_table(mode, res);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400373 if (mode->allocVectors==NULL)
374 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100375
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400376 window = (celt_word16*)celt_alloc(mode->overlap*sizeof(celt_word16));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400377 if (window==NULL)
378 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100379
Jean-Marc Valinf28062f2008-03-03 13:24:01 +1100380#ifndef FIXED_POINT
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100381 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100382 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 +1100383#else
384 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin8974f002010-03-20 00:41:39 -0400385 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 +1100386#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100387 mode->window = window;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100388
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400389 mode->bits = mode->_bits+1;
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400390 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400391 {
392 mode->bits[i] = (const celt_int16 **)compute_alloc_cache(mode, 1<<i);
393 if (mode->bits[i]==NULL)
394 goto failure;
395 }
396 mode->bits[-1] = (const celt_int16 **)compute_alloc_cache(mode, 0);
397 if (mode->bits[-1]==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400398 goto failure;
Jean-Marc Valinb76ee702008-03-10 15:42:35 +1100399
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400400 logN = (celt_int16*)celt_alloc(mode->nbEBands*sizeof(celt_int16));
401 if (logN==NULL)
402 goto failure;
403
404 for (i=0;i<mode->nbEBands;i++)
Jean-Marc Valinaead79b2010-05-11 07:34:24 -0400405 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400406 mode->logN = logN;
Jean-Marc Valinb76ee702008-03-10 15:42:35 +1100407#endif /* !STATIC_MODES */
Jean-Marc Valin640f7fd2009-06-21 09:47:51 -0400408
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400409 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
410 {
411 clt_mdct_init(&mode->mdct[i], 2*mode->shortMdctSize<<i);
412 if ((mode->mdct[i].trig==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400413#ifndef ENABLE_TI_DSPLIB55
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400414 || (mode->mdct[i].kfft==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400415#endif
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400416 )
417 goto failure;
418 }
419 mode->prob = quant_prob_alloc(mode);
420 if (mode->prob==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400421 goto failure;
Jean-Marc Valinbf2d6482008-05-23 16:57:34 +1000422
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400423 mode->marker_start = MODEVALID;
424 mode->marker_end = MODEVALID;
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100425 if (error)
426 *error = CELT_OK;
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100427 return mode;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400428failure:
429 if (error)
430 *error = CELT_INVALID_MODE;
431 if (mode!=NULL)
432 celt_mode_destroy(mode);
433 return NULL;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100434}
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100435
Peter Kirk19f9dc92008-06-06 14:38:38 +0200436void celt_mode_destroy(CELTMode *mode)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100437{
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400438 int i, m;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400439 const celt_int16 *prevPtr = NULL;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400440 if (mode == NULL)
441 {
442 celt_warning("NULL passed to celt_mode_destroy");
443 return;
444 }
445
446 if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED)
447 {
448 celt_warning("Freeing a mode which has already been freed");
449 return;
450 }
451
452 if (mode->marker_start != MODEVALID && mode->marker_start != MODEPARTIAL)
453 {
454 celt_warning("This is not a valid CELT mode structure");
455 return;
456 }
457 mode->marker_start = MODEFREED;
Jean-Marc Valin5588d522008-03-10 15:07:58 +1100458#ifndef STATIC_MODES
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400459 for (m=0;(1<<m)<=mode->nbShortMdcts;m++)
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100460 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400461 if (mode->bits[m]!=NULL)
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100462 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400463 for (i=0;i<mode->nbEBands;i++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400464 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400465 if (mode->bits[m][i] != prevPtr)
466 {
467 prevPtr = mode->bits[m][i];
468 celt_free((int*)mode->bits[m][i]);
469 }
470 }
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100471 }
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400472 celt_free((celt_int16**)mode->bits[m]);
473 }
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400474 if (mode->bits[-1]!=NULL)
475 {
476 for (i=0;i<mode->nbEBands;i++)
477 {
478 if (mode->bits[-1][i] != prevPtr)
479 {
480 prevPtr = mode->bits[-1][i];
481 celt_free((int*)mode->bits[-1][i]);
482 }
483 }
484 }
485 celt_free((celt_int16**)mode->bits[-1]);
486
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400487 celt_free((celt_int16*)mode->eBands);
488 celt_free((celt_int16*)mode->allocVectors);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400489
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400490 celt_free((celt_word16*)mode->window);
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400491 celt_free((celt_int16*)mode->logN);
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100492
Jean-Marc Valin70720a32008-04-19 21:39:26 +1000493#endif
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400494 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
495 clt_mdct_clear(&mode->mdct[i]);
496
Jean-Marc Valin4ce92052008-04-23 13:42:10 +1000497 quant_prob_free(mode->prob);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400498 mode->marker_end = MODEFREED;
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100499 celt_free((CELTMode *)mode);
Jean-Marc Valin2ca8fc32008-02-18 16:27:49 +1100500}
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100501
502int check_mode(const CELTMode *mode)
503{
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400504 if (mode==NULL)
505 return CELT_INVALID_MODE;
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100506 if (mode->marker_start == MODEVALID && mode->marker_end == MODEVALID)
507 return CELT_OK;
508 if (mode->marker_start == MODEFREED || mode->marker_end == MODEFREED)
509 celt_warning("Using a mode that has already been freed");
510 else
511 celt_warning("This is not a valid CELT mode");
512 return CELT_INVALID_MODE;
513}