blob: 5f801ccc221bd21e405d64a43ae5daa74f9277b3 [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 Valine0c25452010-09-03 11:52:38 -040045static const celt_int16 eband5ms[] = {
Jean-Marc Valin8ccda882010-10-04 18:01:45 -040046 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100
Jean-Marc Valine0c25452010-09-03 11:52:38 -040047};
48
Jean-Marc Valinc992a0d2010-11-19 14:06:10 -050049#if 0
Jean-Marc Valin78ea9fd2010-09-24 08:27:28 -040050
Jean-Marc Valine0c25452010-09-03 11:52:38 -040051#define BITALLOC_SIZE 9
52/* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */
53static const unsigned char band_allocation[] = {
54/*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 */
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jean-Marc Valin35095c62010-11-04 13:24:44 -040056 95, 90, 80, 75, 65, 60, 55, 50, 44, 40, 35, 30, 15, 1, 0, 0, 0, 0, 0, 0, 0,
57100, 95, 90, 88, 85, 82, 78, 74, 70, 65, 60, 54, 45, 35, 25, 15, 1, 0, 0, 0, 0,
Jean-Marc Valine0c25452010-09-03 11:52:38 -040058120,110,110,110,100, 96, 90, 88, 84, 76, 70, 65, 60, 45, 35, 25, 20, 1, 1, 0, 0,
59135,125,125,125,115,112,104,104,100, 96, 83, 78, 70, 55, 46, 36, 32, 28, 20, 8, 0,
Jean-Marc Valin8ccda882010-10-04 18:01:45 -040060170,165,157,155,149,145,143,138,138,138,129,124,108, 96, 88, 83, 72, 56, 44, 28, 2,
61192,192,160,160,160,160,160,160,160,160,150,140,120,110,100, 90, 80, 70, 60, 50, 20,
62224,224,192,192,192,192,192,192,192,160,160,160,160,160,160,160,160,120, 80, 64, 40,
63255,255,224,224,224,224,224,224,224,192,192,192,192,192,192,192,192,192,192,192,120,
Jean-Marc Valine0c25452010-09-03 11:52:38 -040064};
65
Jean-Marc Valin78ea9fd2010-09-24 08:27:28 -040066#else
Jean-Marc Valin94491652010-10-21 17:10:24 -040067
Jean-Marc Valin54d84c02010-11-19 11:45:37 -050068/* Alternate tuning (partially derived from Vorbis) */
Jean-Marc Valin59858632010-12-21 11:28:23 -050069#define BITALLOC_SIZE 12
Jean-Marc Valin94491652010-10-21 17:10:24 -040070/* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */
71static const unsigned char band_allocation[] = {
Jean-Marc Valin54d84c02010-11-19 11:45:37 -050072/*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 Valin94491652010-10-21 17:10:24 -040073 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jean-Marc Valinc992a0d2010-11-19 14:06:10 -050074 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0,
Jean-Marc Valin54d84c02010-11-19 11:45:37 -050075110,100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0,
Jean-Marc Valinc992a0d2010-11-19 14:06:10 -050076118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0,
77126,119,112,104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0,
Jean-Marc Valin54d84c02010-11-19 11:45:37 -050078134,127,120,114,103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1,
Jean-Marc Valindd4bf632010-11-30 15:31:45 -050079144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1,
80152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1,
81162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1,
82172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20,
Jean-Marc Valine6d7c082010-11-19 14:43:05 -050083200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,143,120,
Jean-Marc Valin59858632010-12-21 11:28:23 -050084255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
Jean-Marc Valin94491652010-10-21 17:10:24 -040085};
Jean-Marc Valin78ea9fd2010-09-24 08:27:28 -040086#endif
87
Jean-Marc Valinf39e8692008-03-10 12:13:23 +110088#ifdef STATIC_MODES
Jean-Marc Valin8f6d12b2010-11-06 12:11:18 -040089#ifdef FIXED_POINT
90#include "static_modes_fixed.c"
91#else
92#include "static_modes_float.c"
93#endif
Jean-Marc Valinf39e8692008-03-10 12:13:23 +110094#endif
95
Jean-Marc Valind748cd52008-03-01 07:27:03 +110096#ifndef M_PI
97#define M_PI 3.141592653
98#endif
99
Jean-Marc Valin44ffd5a2008-02-22 00:39:25 +1100100
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400101int celt_mode_info(const CELTMode *mode, int request, celt_int32 *value)
Jean-Marc Valinf997ad52008-01-31 16:47:16 +1100102{
103 switch (request)
104 {
Jean-Marc Valinf997ad52008-01-31 16:47:16 +1100105 case CELT_GET_LOOKAHEAD:
106 *value = mode->overlap;
107 break;
Jean-Marc Valin59093c02008-05-15 21:53:27 +1000108 case CELT_GET_BITSTREAM_VERSION:
109 *value = CELT_BITSTREAM_VERSION;
110 break;
Gregory Maxwelld9458cd2009-05-30 17:04:02 -0400111 case CELT_GET_SAMPLE_RATE:
112 *value = mode->Fs;
113 break;
Jean-Marc Valincb7a2a32008-02-11 16:44:48 +1100114 default:
Jean-Marc Valinb6f90612008-10-05 22:39:13 -0400115 return CELT_UNIMPLEMENTED;
Jean-Marc Valinf997ad52008-01-31 16:47:16 +1100116 }
Jean-Marc Valincb7a2a32008-02-11 16:44:48 +1100117 return CELT_OK;
Jean-Marc Valinf997ad52008-01-31 16:47:16 +1100118}
119
Jean-Marc Valin5588d522008-03-10 15:07:58 +1100120#ifndef STATIC_MODES
121
Jean-Marc Valin17683eb2008-02-18 21:45:19 +1100122/* Defining 25 critical bands for the full 0-20 kHz audio bandwidth
123 Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100124#define BARK_BANDS 25
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400125static const celt_int16 bark_freq[BARK_BANDS+1] = {
Jean-Marc Valin17683eb2008-02-18 21:45:19 +1100126 0, 100, 200, 300, 400,
127 510, 630, 770, 920, 1080,
128 1270, 1480, 1720, 2000, 2320,
129 2700, 3150, 3700, 4400, 5300,
130 6400, 7700, 9500, 12000, 15500,
131 20000};
132
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400133static celt_int16 *compute_ebands(celt_int32 Fs, int frame_size, int res, int *nbEBands)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100134{
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400135 celt_int16 *eBands;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400136 int i, lin, low, high, nBark, offset=0;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100137
Jean-Marc Valin3b0df0d2010-07-16 15:55:30 -0400138 /* All modes that have 2.5 ms short blocks use the same definition */
139 if (Fs == 400*(celt_int32)frame_size)
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400140 {
141 *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
Jean-Marc Valin8952c452010-07-16 21:48:44 -0400142 eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+1));
143 for (i=0;i<*nbEBands+1;i++)
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400144 eBands[i] = eband5ms[i];
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400145 return eBands;
146 }
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400147 /* Find the number of critical bands supported by our sampling rate */
148 for (nBark=1;nBark<BARK_BANDS;nBark++)
149 if (bark_freq[nBark+1]*2 >= Fs)
150 break;
151
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100152 /* Find where the linear part ends (i.e. where the spacing is more than min_width */
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400153 for (lin=0;lin<nBark;lin++)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400154 if (bark_freq[lin+1]-bark_freq[lin] >= res)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100155 break;
Jean-Marc Valin4fb95682010-04-20 23:30:22 -0400156
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400157 low = (bark_freq[lin]+res/2)/res;
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400158 high = nBark-lin;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100159 *nbEBands = low+high;
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400160 eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2));
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100161
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400162 if (eBands==NULL)
163 return NULL;
164
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100165 /* Linear spacing (min_width) */
166 for (i=0;i<low;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400167 eBands[i] = i;
Jean-Marc Valinbe8d1252010-04-21 18:09:07 -0400168 if (low>0)
169 offset = eBands[low-1]*res - bark_freq[lin-1];
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100170 /* Spacing follows critical bands */
171 for (i=0;i<high;i++)
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400172 {
173 int target = bark_freq[lin+i];
Jean-Marc Valin44203902011-01-12 09:22:29 -0500174 /* Round to an even value */
175 eBands[i+low] = (target+offset/2+res)/(2*res)*2;
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400176 offset = eBands[i+low]*res - target;
177 }
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100178 /* Enforce the minimum spacing at the boundary */
179 for (i=0;i<*nbEBands;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400180 if (eBands[i] < i)
181 eBands[i] = i;
Jean-Marc Valin44203902011-01-12 09:22:29 -0500182 /* Round to an even value */
183 eBands[*nbEBands] = (bark_freq[nBark]+res)/(2*res)*2;
Jean-Marc Valin8952c452010-07-16 21:48:44 -0400184 if (eBands[*nbEBands] > frame_size)
185 eBands[*nbEBands] = frame_size;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400186 for (i=1;i<*nbEBands-1;i++)
187 {
188 if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
189 {
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400190 eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400191 }
192 }
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400193 /*for (i=0;i<=*nbEBands+1;i++)
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400194 printf ("%d ", eBands[i]);
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400195 printf ("\n");
196 exit(1);*/
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100197 /* FIXME: Remove last band if too small */
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100198 return eBands;
199}
200
Jean-Marc Valine3e2c262011-01-26 13:09:53 -0500201static void compute_allocation_table(CELTMode *mode)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100202{
Jean-Marc Valin2f6c5fe2010-06-28 17:22:37 -0400203 int i, j;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400204 unsigned char *allocVectors;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400205 int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400206
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400207 mode->nbAllocVectors = BITALLOC_SIZE;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400208 allocVectors = celt_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400209 if (allocVectors==NULL)
210 return;
211
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400212 /* Check for standard mode */
Jean-Marc Valin8e31ab32010-12-16 16:45:35 -0500213 if (mode->Fs == 400*(celt_int32)mode->shortMdctSize)
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400214 {
215 for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++)
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400216 allocVectors[i] = band_allocation[i];
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400217 mode->allocVectors = allocVectors;
218 return;
219 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400220 /* If not the standard mode, interpolate */
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400221 /* Compute per-codec-band allocation from per-critical-band matrix */
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100222 for (i=0;i<BITALLOC_SIZE;i++)
223 {
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500224 for (j=0;j<mode->nbEBands;j++)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100225 {
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500226 int k;
227 for (k=0;k<maxBands;k++)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100228 {
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500229 if (400*(celt_int32)eband5ms[k] > mode->eBands[j]*(celt_int32)mode->Fs/mode->shortMdctSize)
230 break;
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400231 }
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500232 if (k>mode->nbEBands-1)
233 allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1];
234 else {
235 celt_int32 a0, a1;
236 a1 = mode->eBands[j]*(celt_int32)mode->Fs/mode->shortMdctSize - 400*(celt_int32)eband5ms[k-1];
237 a0 = 400*(celt_int32)eband5ms[k] - mode->eBands[j]*(celt_int32)mode->Fs/mode->shortMdctSize;
238 allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1]
239 + a1*band_allocation[i*maxBands+k])/(a0+a1);
240 }
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400241 }
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100242 }
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500243
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400244 /*printf ("\n");
245 for (i=0;i<BITALLOC_SIZE;i++)
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400246 {
247 for (j=0;j<mode->nbEBands;j++)
248 printf ("%d ", allocVectors[i*mode->nbEBands+j]);
249 printf ("\n");
250 }
251 exit(0);*/
252
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400253 mode->allocVectors = allocVectors;
Jean-Marc Valinad637192008-05-07 13:44:39 +1000254}
255
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400256#endif /* STATIC_MODES */
257
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400258CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100259{
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000260 int i;
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400261#ifdef STATIC_MODES
262 for (i=0;i<TOTAL_MODES;i++)
263 {
264 if (Fs == static_mode_list[i]->Fs &&
265 frame_size == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
266 {
267 return (CELTMode*)static_mode_list[i];
268 }
269 }
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400270 if (error)
271 *error = CELT_BAD_ARG;
272 return NULL;
273#else
274 int res;
275 CELTMode *mode=NULL;
276 celt_word16 *window;
277 celt_int16 *logN;
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400278 int LM;
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000279#ifdef STDIN_TUNING
280 scanf("%d ", &MIN_BINS);
281 scanf("%d ", &BITALLOC_SIZE);
282 band_allocation = celt_alloc(sizeof(int)*BARK_BANDS*BITALLOC_SIZE);
283 for (i=0;i<BARK_BANDS*BITALLOC_SIZE;i++)
284 {
285 scanf("%d ", band_allocation+i);
286 }
287#endif
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000288 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400289#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
290 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400291 goto failure;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400292#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100293
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100294 /* The good thing here is that permutation of the arguments will automatically be invalid */
295
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400296 if (Fs < 8000 || Fs > 96000)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100297 {
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100298 if (error)
299 *error = CELT_BAD_ARG;
300 return NULL;
301 }
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400302 if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100303 {
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100304 if (error)
305 *error = CELT_BAD_ARG;
306 return NULL;
307 }
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100308
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100309 mode = celt_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400310 if (mode==NULL)
311 goto failure;
Jean-Marc Valine6b74652008-02-20 18:01:08 +1100312 mode->Fs = Fs;
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400313
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400314 /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis
315 is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should
316 approximate that. */
317 if(Fs < 12000) /* 8 kHz */
318 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500319 mode->preemph[0] = QCONST16(0.3500061035f, 15);
320 mode->preemph[1] = -QCONST16(0.1799926758f, 15);
Jean-Marc Valin09213de2011-01-27 21:43:24 -0500321 mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500322 mode->preemph[3] = QCONST16(3.6765136719f, 13);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400323 } else if(Fs < 24000) /* 16 kHz */
324 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500325 mode->preemph[0] = QCONST16(0.6000061035f, 15);
326 mode->preemph[1] = -QCONST16(0.1799926758f, 15);
Jean-Marc Valin09213de2011-01-27 21:43:24 -0500327 mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */
328 mode->preemph[3] = QCONST16(2.2598876953f, 13);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400329 } else if(Fs < 40000) /* 32 kHz */
330 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500331 mode->preemph[0] = QCONST16(0.7799987793f, 15);
332 mode->preemph[1] = -QCONST16(0.1000061035f, 15);
Jean-Marc Valin09213de2011-01-27 21:43:24 -0500333 mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500334 mode->preemph[3] = QCONST16(1.3333740234f, 13);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400335 } else /* 48 kHz */
336 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500337 mode->preemph[0] = QCONST16(0.8500061035f, 15);
338 mode->preemph[1] = QCONST16(0.0f, 15);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400339 mode->preemph[2] = QCONST16(1.f, SIG_SHIFT);
340 mode->preemph[3] = QCONST16(1.f, 13);
341 }
342
Jean-Marc Valina2546bb2010-07-16 12:00:00 -0400343 if ((celt_int32)frame_size*75 >= Fs && (frame_size%16)==0)
Jean-Marc Valine6d832a2009-07-08 22:21:31 -0400344 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400345 LM = 3;
Jean-Marc Valina2546bb2010-07-16 12:00:00 -0400346 } else if ((celt_int32)frame_size*150 >= Fs && (frame_size%8)==0)
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000347 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400348 LM = 2;
Jean-Marc Valina2546bb2010-07-16 12:00:00 -0400349 } else if ((celt_int32)frame_size*300 >= Fs && (frame_size%4)==0)
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400350 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400351 LM = 1;
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400352 } else
353 {
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400354 LM = 0;
Jean-Marc Valin16ca18b2008-06-18 23:44:48 +1000355 }
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400356
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400357 mode->maxLM = LM;
358 mode->nbShortMdcts = 1<<LM;
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400359 mode->shortMdctSize = frame_size/mode->nbShortMdcts;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400360 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
361
362 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400363 if (mode->eBands==NULL)
364 goto failure;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400365
Jean-Marc Valin85f41b22010-07-16 18:12:45 -0400366 mode->effEBands = mode->nbEBands;
367 while (mode->eBands[mode->effEBands] > mode->shortMdctSize)
368 mode->effEBands--;
Jean-Marc Valin4834c922009-09-28 19:17:34 -0400369
Jean-Marc Valin08192e32009-07-02 13:28:55 -0400370 /* Overlap must be divisible by 4 */
Jean-Marc Valin70d30ff2010-12-13 13:50:29 -0500371 mode->overlap = ((mode->shortMdctSize>>2)<<2);
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400372
Jean-Marc Valine3e2c262011-01-26 13:09:53 -0500373 compute_allocation_table(mode);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400374 if (mode->allocVectors==NULL)
375 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100376
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400377 window = (celt_word16*)celt_alloc(mode->overlap*sizeof(celt_word16));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400378 if (window==NULL)
379 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100380
Jean-Marc Valinf28062f2008-03-03 13:24:01 +1100381#ifndef FIXED_POINT
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100382 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100383 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 +1100384#else
385 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin8974f002010-03-20 00:41:39 -0400386 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 +1100387#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100388 mode->window = window;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100389
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400390 logN = (celt_int16*)celt_alloc(mode->nbEBands*sizeof(celt_int16));
391 if (logN==NULL)
392 goto failure;
393
394 for (i=0;i<mode->nbEBands;i++)
Jean-Marc Valinaead79b2010-05-11 07:34:24 -0400395 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400396 mode->logN = logN;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400397
398 compute_pulse_cache(mode, mode->maxLM);
Jean-Marc Valin640f7fd2009-06-21 09:47:51 -0400399
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400400 clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, mode->maxLM);
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400401 if ((mode->mdct.trig==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400402#ifndef ENABLE_TI_DSPLIB55
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400403 || (mode->mdct.kfft==NULL)
Jean-Marc Valin9714f662009-08-12 20:29:57 -0400404#endif
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400405 )
406 goto failure;
407
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100408 if (error)
409 *error = CELT_OK;
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400410
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100411 return mode;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400412failure:
413 if (error)
414 *error = CELT_INVALID_MODE;
415 if (mode!=NULL)
416 celt_mode_destroy(mode);
417 return NULL;
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400418#endif /* !STATIC_MODES */
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100419}
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100420
Peter Kirk19f9dc92008-06-06 14:38:38 +0200421void celt_mode_destroy(CELTMode *mode)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100422{
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400423#ifndef STATIC_MODES
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400424 if (mode == NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400425 return;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400426
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400427 celt_free((celt_int16*)mode->eBands);
428 celt_free((celt_int16*)mode->allocVectors);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400429
Jean-Marc Valin234969c2009-10-17 22:12:42 -0400430 celt_free((celt_word16*)mode->window);
Jean-Marc Valin31bec962010-04-07 18:30:28 -0400431 celt_free((celt_int16*)mode->logN);
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100432
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400433 celt_free((celt_int16*)mode->cache.index);
434 celt_free((unsigned char*)mode->cache.bits);
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400435 clt_mdct_clear(&mode->mdct);
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400436
Jean-Marc Valin949902f2008-03-11 10:43:06 +1100437 celt_free((CELTMode *)mode);
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400438#endif
Jean-Marc Valin2ca8fc32008-02-18 16:27:49 +1100439}