blob: af2d0077df97c4fd23a4a3f648031e22915d84ac [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 Valin3b0df0d2010-07-16 15:55:30 -0400122 /* All modes that have 2.5 ms short blocks use the same definition */
123 if (Fs == 400*(celt_int32)frame_size)
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400124 {
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 Maxwellcbaf67e2008-09-28 04:19:19 -0400132 /* 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 Valin81a82952008-02-17 22:41:29 +1100137 /* Find where the linear part ends (i.e. where the spacing is more than min_width */
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400138 for (lin=0;lin<nBark;lin++)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400139 if (bark_freq[lin+1]-bark_freq[lin] >= res)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100140 break;
Jean-Marc Valin4fb95682010-04-20 23:30:22 -0400141
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400142 low = (bark_freq[lin]+res/2)/res;
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400143 high = nBark-lin;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100144 *nbEBands = low+high;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400145 eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2));
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100146
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400147 if (eBands==NULL)
148 return NULL;
149
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100150 /* Linear spacing (min_width) */
151 for (i=0;i<low;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400152 eBands[i] = i;
Jean-Marc Valinbe8d1252010-04-21 18:09:07 -0400153 if (low>0)
154 offset = eBands[low-1]*res - bark_freq[lin-1];
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100155 /* Spacing follows critical bands */
156 for (i=0;i<high;i++)
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400157 {
158 int target = bark_freq[lin+i];
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400159 eBands[i+low] = (target+(offset+res)/2)/res;
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400160 offset = eBands[i+low]*res - target;
161 }
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100162 /* Enforce the minimum spacing at the boundary */
163 for (i=0;i<*nbEBands;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400164 if (eBands[i] < i)
165 eBands[i] = i;
166 eBands[*nbEBands] = (bark_freq[nBark]+res/2)/res;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100167 eBands[*nbEBands+1] = frame_size;
168 if (eBands[*nbEBands] > eBands[*nbEBands+1])
169 eBands[*nbEBands] = eBands[*nbEBands+1];
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400170 for (i=1;i<*nbEBands-1;i++)
171 {
172 if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
173 {
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400174 eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400175 }
176 }
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400177 /*for (i=0;i<=*nbEBands+1;i++)
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400178 printf ("%d ", eBands[i]);
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400179 printf ("\n");
180 exit(1);*/
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100181 /* FIXME: Remove last band if too small */
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100182 return eBands;
183}
184
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400185static void compute_allocation_table(CELTMode *mode, int res)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100186{
Jean-Marc Valin2f6c5fe2010-06-28 17:22:37 -0400187 int i, j;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400188 unsigned char *allocVectors;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400189 int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400190
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400191 mode->nbAllocVectors = BITALLOC_SIZE;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400192 allocVectors = celt_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400193 if (allocVectors==NULL)
194 return;
195
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400196 /* Check for standard mode */
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400197 if (mode->Fs == 400*(celt_int32)mode->shortMdctSize && mode->Fs >= 40000)
198 {
199 for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++)
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400200 allocVectors[i] = band_allocation[i];
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400201 mode->allocVectors = allocVectors;
202 return;
203 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400204 /* If not the standard mode, interpolate */
205
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400206 /* Compute per-codec-band allocation from per-critical-band matrix */
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100207 for (i=0;i<BITALLOC_SIZE;i++)
208 {
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400209 celt_int32 current = 0;
Gregory Maxwellec836da2009-05-04 14:55:40 -0400210 int eband = 0;
Jean-Marc Valin2f6c5fe2010-06-28 17:22:37 -0400211 /* 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 Valin9838fec2008-02-18 14:45:11 +1100214 {
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400215 int edge, low, high;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400216 celt_int32 alloc;
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400217 alloc = band_allocation[i*maxBands + j]*(mode->eBands[eband+1]-mode->eBands[eband])<<4;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400218 low = eband5ms[j]*200;
219 high = eband5ms[j+1]*200;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400220 edge = mode->eBands[eband+1]*res;
Jean-Marc Valin3ff5e4c2010-04-14 18:02:09 -0400221 while (edge <= high && eband < mode->nbEBands)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100222 {
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400223 celt_int32 num;
224 int den, bits;
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400225 int N = (mode->eBands[eband+1]-mode->eBands[eband]);
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400226 num = alloc * (edge-low);
227 den = high-low;
228 /* Divide with rounding */
229 bits = (2*num+den)/(2*den);
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400230 allocVectors[i*mode->nbEBands+eband] = (2*(current+bits)+(N<<4))/(2*N<<4);
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400231 /* 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 Valin9838fec2008-02-18 14:45:11 +1100237 eband++;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400238 edge = mode->eBands[eband+1]*res;
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400239 }
240 current += alloc;
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100241 }
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400242 if (eband < mode->nbEBands)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400243 {
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400244 int N = (mode->eBands[eband+1]-mode->eBands[eband]);
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400245 allocVectors[i*mode->nbEBands+eband] = (2*current+(N<<4))/(2*N<<4);
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400246 }
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100247 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400248 /*printf ("\n");
249 for (i=0;i<BITALLOC_SIZE;i++)
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400250 {
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 Valin36e6e012008-08-02 22:25:19 -0400257 mode->allocVectors = allocVectors;
Jean-Marc Valinad637192008-05-07 13:44:39 +1000258}
259
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400260#endif /* STATIC_MODES */
261
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400262CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100263{
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000264 int i;
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400265 int LM;
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000266#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 Valinf39e8692008-03-10 12:13:23 +1100275#ifdef STATIC_MODES
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100276 const CELTMode *m = NULL;
277 CELTMode *mode=NULL;
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000278 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400279#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
280 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400281 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400282#endif
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100283 for (i=0;i<TOTAL_MODES;i++)
284 {
285 if (Fs == static_mode_list[i]->Fs &&
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400286 frame_size == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100287 {
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100288 m = static_mode_list[i];
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100289 break;
290 }
291 }
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100292 if (m == NULL)
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100293 {
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 Valin949902f2008-03-11 10:43:06 +1100299 mode = (CELTMode*)celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400300 if (mode==NULL)
301 goto failure;
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100302 CELT_COPY(mode, m, 1);
Jean-Marc Valinf477d582010-06-27 09:56:26 -0400303 mode->bits = mode->_bits+1;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400304 mode->marker_start = MODEPARTIAL;
Jean-Marc Valinf39e8692008-03-10 12:13:23 +1100305#else
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100306 int res;
Gregory Maxwell84966952009-06-30 01:06:04 -0400307 CELTMode *mode=NULL;
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400308 celt_word16 *window;
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400309 celt_int16 *logN;
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000310 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400311#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
312 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400313 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400314#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100315
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100316 /* The good thing here is that permutation of the arguments will automatically be invalid */
317
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400318 if (Fs < 8000 || Fs > 96000)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100319 {
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400320 celt_warning("Sampling rate must be between 8 kHz and 96 kHz");
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100321 if (error)
322 *error = CELT_BAD_ARG;
323 return NULL;
324 }
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400325 if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100326 {
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400327 celt_warning("Only even frame sizes from 40 to 1024 are supported");
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100328 if (error)
329 *error = CELT_BAD_ARG;
330 return NULL;
331 }
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100332
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100333 mode = celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400334 if (mode==NULL)
335 goto failure;
336 mode->marker_start = MODEPARTIAL;
Jean-Marc Valine6b74652008-02-20 18:01:08 +1100337 mode->Fs = Fs;
Jean-Marc Valin8b2a5922008-02-29 00:32:51 +1100338 mode->ePredCoef = QCONST16(.8f,15);
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400339
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400340 /* 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 Valina2546bb2010-07-16 12:00:00 -0400369 if ((celt_int32)frame_size*75 >= Fs && (frame_size%16)==0)
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400370 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400371 LM = 3;
Jean-Marc Valina2546bb2010-07-16 12:00:00 -0400372 } else if ((celt_int32)frame_size*150 >= Fs && (frame_size%8)==0)
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000373 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400374 LM = 2;
Jean-Marc Valina2546bb2010-07-16 12:00:00 -0400375 } else if ((celt_int32)frame_size*300 >= Fs && (frame_size%4)==0)
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400376 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400377 LM = 1;
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400378 } else
379 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400380 LM = 0;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000381 }
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400382
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400383 mode->maxLM = LM;
384 mode->nbShortMdcts = 1<<LM;
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400385 mode->shortMdctSize = frame_size/mode->nbShortMdcts;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400386 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
387
388 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400389 if (mode->eBands==NULL)
390 goto failure;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400391
Jean-Marc Valine949b722010-05-07 07:48:07 -0400392 mode->pitchEnd = 4000*(celt_int32)mode->shortMdctSize/Fs;
Jean-Marc Valin4834c922009-09-28 19:17:34 -0400393
Jean-Marc Valin08192e32009-07-02 13:28:55 -0400394 /* Overlap must be divisible by 4 */
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000395 if (mode->nbShortMdcts > 1)
Jean-Marc Valince4dd362010-05-07 07:45:18 -0400396 mode->overlap = (mode->shortMdctSize>>2)<<2;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000397 else
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400398 mode->overlap = (frame_size>>3)<<2;
399
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400400
401 compute_allocation_table(mode, res);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400402 if (mode->allocVectors==NULL)
403 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100404
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400405 window = (celt_word16*)celt_alloc(mode->overlap*sizeof(celt_word16));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400406 if (window==NULL)
407 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100408
Jean-Marc Valinf28062f2008-03-03 13:24:01 +1100409#ifndef FIXED_POINT
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100410 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100411 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 +1100412#else
413 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin8974f002010-03-20 00:41:39 -0400414 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 +1100415#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100416 mode->window = window;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100417
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400418 mode->bits = mode->_bits+1;
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400419 for (i=0;(1<<i)<=mode->nbShortMdcts;i++)
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400420 {
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 Maxwelldc67fa92009-06-04 17:17:35 -0400427 goto failure;
Jean-Marc Valinb76ee702008-03-10 15:42:35 +1100428
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400429 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 Valinaead79b2010-05-11 07:34:24 -0400434 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400435 mode->logN = logN;
Jean-Marc Valinb76ee702008-03-10 15:42:35 +1100436#endif /* !STATIC_MODES */
Jean-Marc Valin640f7fd2009-06-21 09:47:51 -0400437
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400438 clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, LM);
439 if ((mode->mdct.trig==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400440#ifndef ENABLE_TI_DSPLIB55
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400441 || (mode->mdct.kfft==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400442#endif
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400443 )
444 goto failure;
445
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400446 mode->prob = quant_prob_alloc(mode);
447 if (mode->prob==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400448 goto failure;
Jean-Marc Valinbf2d6482008-05-23 16:57:34 +1000449
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400450 mode->marker_start = MODEVALID;
451 mode->marker_end = MODEVALID;
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100452 if (error)
453 *error = CELT_OK;
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100454 return mode;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400455failure:
456 if (error)
457 *error = CELT_INVALID_MODE;
458 if (mode!=NULL)
459 celt_mode_destroy(mode);
460 return NULL;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100461}
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100462
Peter Kirk19f9dc92008-06-06 14:38:38 +0200463void celt_mode_destroy(CELTMode *mode)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100464{
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400465 int i, m;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400466 const celt_int16 *prevPtr = NULL;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400467 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 Valin5588d522008-03-10 15:07:58 +1100485#ifndef STATIC_MODES
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400486 for (m=0;(1<<m)<=mode->nbShortMdcts;m++)
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100487 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400488 if (mode->bits[m]!=NULL)
Jean-Marc Valin25358cd2008-02-19 12:21:32 +1100489 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400490 for (i=0;i<mode->nbEBands;i++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400491 {
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400492 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 Valin25358cd2008-02-19 12:21:32 +1100498 }
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400499 celt_free((celt_int16**)mode->bits[m]);
500 }
Jean-Marc Valincae30df2010-05-21 00:26:03 -0400501 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 Valin31bec962010-04-07 18:30:28 -0400514 celt_free((celt_int16*)mode->eBands);
515 celt_free((celt_int16*)mode->allocVectors);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400516
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400517 celt_free((celt_word16*)mode->window);
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400518 celt_free((celt_int16*)mode->logN);
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100519
Jean-Marc Valin70720a32008-04-19 21:39:26 +1000520#endif
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400521 clt_mdct_clear(&mode->mdct);
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400522
Jean-Marc Valin4ce92052008-04-23 13:42:10 +1000523 quant_prob_free(mode->prob);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400524 mode->marker_end = MODEFREED;
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100525 celt_free((CELTMode *)mode);
Jean-Marc Valin2ca8fc32008-02-18 16:27:49 +1100526}
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100527
528int check_mode(const CELTMode *mode)
529{
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400530 if (mode==NULL)
531 return CELT_INVALID_MODE;
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100532 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}