blob: 390c5e8aeb36aa48b16d2ffa202554dc174da661 [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
Gregory Maxwell71d39ad2011-07-30 00:00:29 -04003 Copyright (c) 2008 Gregory Maxwell
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04004 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:
Gregory Maxwell71d39ad2011-07-30 00:00:29 -04009
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110010 - Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040012
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110013 - 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.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040016
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110017 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040020 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110022 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
Jean-Marc Valin02fa9132008-02-20 12:09:29 +110030#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
Jean-Marc Valin65d57e62008-02-18 15:49:37 +110034#include "celt.h"
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110035#include "modes.h"
Jean-Marc Valin472a5f02008-02-19 13:12:32 +110036#include "rate.h"
Jean-Marc Valin81a82952008-02-17 22:41:29 +110037#include "os_support.h"
Jean-Marc Valinf7cec832008-04-18 17:29:56 +100038#include "stack_alloc.h"
Jean-Marc Valin4ce92052008-04-23 13:42:10 +100039#include "quant_bands.h"
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -050040#include "cpu_support.h"
Jean-Marc Valinecb36a32007-12-05 01:31:49 +110041
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040042static const opus_int16 eband5ms[] = {
Jean-Marc Valin9cc56bf2011-02-01 22:03:26 -050043/*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 Valin8ccda882010-10-04 18:01:45 -040044 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 -040045};
46
Jean-Marc Valin54d84c02010-11-19 11:45:37 -050047/* Alternate tuning (partially derived from Vorbis) */
Timothy B. Terriberryce6d0902011-02-01 17:41:12 -080048#define BITALLOC_SIZE 11
Jean-Marc Valin94491652010-10-21 17:10:24 -040049/* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */
50static const unsigned char band_allocation[] = {
Jean-Marc Valin54d84c02010-11-19 11:45:37 -050051/*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 -040052 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 -050053 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 -050054110,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 -050055118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0,
56126,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 -050057134,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 -050058144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1,
59152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1,
60162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1,
61172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20,
Timothy B. Terriberry89039a32011-01-30 22:27:13 -080062200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,129,104,
Jean-Marc Valin94491652010-10-21 17:10:24 -040063};
Jean-Marc Valin78ea9fd2010-09-24 08:27:28 -040064
Jean-Marc Valin665da0b2011-01-30 12:15:12 -050065#ifndef CUSTOM_MODES_ONLY
66 #ifdef FIXED_POINT
Gregory Maxwell63fd63d2011-08-02 17:13:06 -040067 #include "static_modes_fixed.h"
Jean-Marc Valin665da0b2011-01-30 12:15:12 -050068 #else
Gregory Maxwell63fd63d2011-08-02 17:13:06 -040069 #include "static_modes_float.h"
Jean-Marc Valin665da0b2011-01-30 12:15:12 -050070 #endif
71#endif /* CUSTOM_MODES_ONLY */
Jean-Marc Valinf39e8692008-03-10 12:13:23 +110072
Jean-Marc Valind748cd52008-03-01 07:27:03 +110073#ifndef M_PI
74#define M_PI 3.141592653
75#endif
76
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -050077#ifdef CUSTOM_MODES
Jean-Marc Valin5588d522008-03-10 15:07:58 +110078
Jean-Marc Valin17683eb2008-02-18 21:45:19 +110079/* Defining 25 critical bands for the full 0-20 kHz audio bandwidth
80 Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */
Jean-Marc Valin81a82952008-02-17 22:41:29 +110081#define BARK_BANDS 25
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040082static const opus_int16 bark_freq[BARK_BANDS+1] = {
Jean-Marc Valin17683eb2008-02-18 21:45:19 +110083 0, 100, 200, 300, 400,
84 510, 630, 770, 920, 1080,
85 1270, 1480, 1720, 2000, 2320,
86 2700, 3150, 3700, 4400, 5300,
87 6400, 7700, 9500, 12000, 15500,
88 20000};
89
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040090static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *nbEBands)
Jean-Marc Valin81a82952008-02-17 22:41:29 +110091{
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040092 opus_int16 *eBands;
Timothy B. Terriberrycb8f3662011-02-01 12:32:34 -080093 int i, j, lin, low, high, nBark, offset=0;
Jean-Marc Valin81a82952008-02-17 22:41:29 +110094
Jean-Marc Valin3b0df0d2010-07-16 15:55:30 -040095 /* All modes that have 2.5 ms short blocks use the same definition */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040096 if (Fs == 400*(opus_int32)frame_size)
Jean-Marc Valin20639c42010-05-19 16:10:12 -040097 {
98 *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
Jean-Marc Valin07f88402011-08-29 15:08:51 -040099 eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+1));
Jean-Marc Valin8952c452010-07-16 21:48:44 -0400100 for (i=0;i<*nbEBands+1;i++)
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400101 eBands[i] = eband5ms[i];
Jean-Marc Valin20639c42010-05-19 16:10:12 -0400102 return eBands;
103 }
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400104 /* Find the number of critical bands supported by our sampling rate */
105 for (nBark=1;nBark<BARK_BANDS;nBark++)
106 if (bark_freq[nBark+1]*2 >= Fs)
107 break;
108
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100109 /* Find where the linear part ends (i.e. where the spacing is more than min_width */
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400110 for (lin=0;lin<nBark;lin++)
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400111 if (bark_freq[lin+1]-bark_freq[lin] >= res)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100112 break;
Jean-Marc Valin4fb95682010-04-20 23:30:22 -0400113
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400114 low = (bark_freq[lin]+res/2)/res;
Gregory Maxwellcbaf67e2008-09-28 04:19:19 -0400115 high = nBark-lin;
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100116 *nbEBands = low+high;
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400117 eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2));
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400118
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400119 if (eBands==NULL)
120 return NULL;
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400121
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100122 /* Linear spacing (min_width) */
123 for (i=0;i<low;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400124 eBands[i] = i;
Jean-Marc Valinbe8d1252010-04-21 18:09:07 -0400125 if (low>0)
126 offset = eBands[low-1]*res - bark_freq[lin-1];
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100127 /* Spacing follows critical bands */
128 for (i=0;i<high;i++)
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400129 {
130 int target = bark_freq[lin+i];
Jean-Marc Valin44203902011-01-12 09:22:29 -0500131 /* Round to an even value */
132 eBands[i+low] = (target+offset/2+res)/(2*res)*2;
Jean-Marc Valin39f68ac2009-10-03 23:27:52 -0400133 offset = eBands[i+low]*res - target;
134 }
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100135 /* Enforce the minimum spacing at the boundary */
136 for (i=0;i<*nbEBands;i++)
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400137 if (eBands[i] < i)
138 eBands[i] = i;
Jean-Marc Valin44203902011-01-12 09:22:29 -0500139 /* Round to an even value */
140 eBands[*nbEBands] = (bark_freq[nBark]+res)/(2*res)*2;
Jean-Marc Valin8952c452010-07-16 21:48:44 -0400141 if (eBands[*nbEBands] > frame_size)
142 eBands[*nbEBands] = frame_size;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400143 for (i=1;i<*nbEBands-1;i++)
144 {
145 if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
146 {
Jean-Marc Valin65ee67a2010-04-26 07:08:44 -0400147 eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400148 }
149 }
Timothy B. Terriberrycb8f3662011-02-01 12:32:34 -0800150 /* Remove any empty bands. */
151 for (i=j=0;i<*nbEBands;i++)
152 if(eBands[i+1]>eBands[j])
153 eBands[++j]=eBands[i+1];
154 *nbEBands=j;
155
Timothy B. Terriberry2799c292011-02-01 12:53:05 -0800156 for (i=1;i<*nbEBands;i++)
157 {
158 /* Every band must be smaller than the last band. */
159 celt_assert(eBands[i]-eBands[i-1]<=eBands[*nbEBands]-eBands[*nbEBands-1]);
160 /* Each band must be no larger than twice the size of the previous one. */
161 celt_assert(eBands[i+1]-eBands[i]<=2*(eBands[i]-eBands[i-1]));
162 }
163
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100164 return eBands;
165}
166
Jean-Marc Valine3e2c262011-01-26 13:09:53 -0500167static void compute_allocation_table(CELTMode *mode)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100168{
Jean-Marc Valin2f6c5fe2010-06-28 17:22:37 -0400169 int i, j;
Jean-Marc Valin01b54b92010-06-03 23:29:35 -0400170 unsigned char *allocVectors;
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400171 int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400172
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400173 mode->nbAllocVectors = BITALLOC_SIZE;
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400174 allocVectors = opus_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400175 if (allocVectors==NULL)
176 return;
177
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400178 /* Check for standard mode */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400179 if (mode->Fs == 400*(opus_int32)mode->shortMdctSize)
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400180 {
181 for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++)
Jean-Marc Valinffe50612010-06-04 00:13:19 -0400182 allocVectors[i] = band_allocation[i];
Jean-Marc Valin12e851d2010-06-03 08:12:11 -0400183 mode->allocVectors = allocVectors;
184 return;
185 }
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400186 /* If not the standard mode, interpolate */
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400187 /* Compute per-codec-band allocation from per-critical-band matrix */
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100188 for (i=0;i<BITALLOC_SIZE;i++)
189 {
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500190 for (j=0;j<mode->nbEBands;j++)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100191 {
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500192 int k;
193 for (k=0;k<maxBands;k++)
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100194 {
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400195 if (400*(opus_int32)eband5ms[k] > mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize)
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500196 break;
Jean-Marc Valin137f3362010-04-14 17:42:22 -0400197 }
Jean-Marc Valinfa74ae22011-02-11 21:56:11 -0500198 if (k>maxBands-1)
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500199 allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1];
200 else {
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400201 opus_int32 a0, a1;
202 a1 = mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize - 400*(opus_int32)eband5ms[k-1];
203 a0 = 400*(opus_int32)eband5ms[k] - mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize;
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500204 allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1]
205 + a1*band_allocation[i*maxBands+k])/(a0+a1);
206 }
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400207 }
Jean-Marc Valin9838fec2008-02-18 14:45:11 +1100208 }
Jean-Marc Valin1bfa18c2010-12-01 16:11:38 -0500209
Jean-Marc Valinc51e98b2010-06-03 22:11:41 -0400210 /*printf ("\n");
211 for (i=0;i<BITALLOC_SIZE;i++)
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400212 {
213 for (j=0;j<mode->nbEBands;j++)
214 printf ("%d ", allocVectors[i*mode->nbEBands+j]);
215 printf ("\n");
216 }
217 exit(0);*/
218
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400219 mode->allocVectors = allocVectors;
Jean-Marc Valinad637192008-05-07 13:44:39 +1000220}
221
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500222#endif /* CUSTOM_MODES */
Jean-Marc Valin36e6e012008-08-02 22:25:19 -0400223
Jean-Marc Valin06237d72011-09-01 13:20:40 -0400224CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100225{
Jean-Marc Valin47c248a2008-04-26 08:16:12 +1000226 int i;
Gregory Maxwell95becbe2011-02-03 21:06:43 -0500227#ifdef CUSTOM_MODES
Jean-Marc Valinff96b162011-03-21 11:32:50 -0400228 CELTMode *mode=NULL;
Gregory Maxwell95becbe2011-02-03 21:06:43 -0500229 int res;
Jean-Marc Valinff5f7222011-07-29 18:59:12 -0400230 opus_val16 *window;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400231 opus_int16 *logN;
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400232 int LM;
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -0500233 int arch = opus_select_arch();
Jean-Marc Valinf7cec832008-04-18 17:29:56 +1000234 ALLOC_STACK;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400235#if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
236 if (global_stack==NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400237 goto failure;
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400238#endif
Jean-Marc Valin7e983192011-02-01 18:00:29 -0500239#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100240
Jean-Marc Valin665da0b2011-01-30 12:15:12 -0500241#ifndef CUSTOM_MODES_ONLY
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500242 for (i=0;i<TOTAL_MODES;i++)
243 {
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500244 int j;
245 for (j=0;j<4;j++)
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500246 {
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500247 if (Fs == static_mode_list[i]->Fs &&
248 (frame_size<<j) == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
249 {
250 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400251 *error = OPUS_OK;
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500252 return (CELTMode*)static_mode_list[i];
253 }
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500254 }
255 }
Jean-Marc Valin665da0b2011-01-30 12:15:12 -0500256#endif /* CUSTOM_MODES_ONLY */
257
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500258#ifndef CUSTOM_MODES
259 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400260 *error = OPUS_BAD_ARG;
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500261 return NULL;
262#else
263
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100264 /* The good thing here is that permutation of the arguments will automatically be invalid */
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400265
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400266 if (Fs < 8000 || Fs > 96000)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100267 {
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100268 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400269 *error = OPUS_BAD_ARG;
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100270 return NULL;
271 }
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400272 if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0)
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100273 {
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100274 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400275 *error = OPUS_BAD_ARG;
Jean-Marc Valin75e9c862008-02-18 17:04:15 +1100276 return NULL;
277 }
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800278 /* Frames of less than 1ms are not supported. */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400279 if ((opus_int32)frame_size*1000 < Fs)
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800280 {
281 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400282 *error = OPUS_BAD_ARG;
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800283 return NULL;
284 }
285
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400286 if ((opus_int32)frame_size*75 >= Fs && (frame_size%16)==0)
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800287 {
288 LM = 3;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400289 } else if ((opus_int32)frame_size*150 >= Fs && (frame_size%8)==0)
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800290 {
291 LM = 2;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400292 } else if ((opus_int32)frame_size*300 >= Fs && (frame_size%4)==0)
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800293 {
294 LM = 1;
Timothy B. Terriberrycf5d3a82011-02-02 11:42:33 -0800295 } else
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800296 {
297 LM = 0;
298 }
Timothy B. Terriberrycf5d3a82011-02-02 11:42:33 -0800299
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800300 /* Shorts longer than 3.3ms are not supported. */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400301 if ((opus_int32)(frame_size>>LM)*300 > Fs)
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800302 {
303 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400304 *error = OPUS_BAD_ARG;
Timothy B. Terriberryaa6fec62011-02-01 15:36:59 -0800305 return NULL;
306 }
307
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400308 mode = opus_alloc(sizeof(CELTMode));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400309 if (mode==NULL)
310 goto failure;
Jean-Marc Valine6b74652008-02-20 18:01:08 +1100311 mode->Fs = Fs;
Gregory Maxwell23e654f2008-09-27 16:20:03 -0400312
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400313 /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis
314 is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should
315 approximate that. */
316 if(Fs < 12000) /* 8 kHz */
317 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500318 mode->preemph[0] = QCONST16(0.3500061035f, 15);
319 mode->preemph[1] = -QCONST16(0.1799926758f, 15);
Jean-Marc Valin09213de2011-01-27 21:43:24 -0500320 mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500321 mode->preemph[3] = QCONST16(3.6765136719f, 13);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400322 } else if(Fs < 24000) /* 16 kHz */
323 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500324 mode->preemph[0] = QCONST16(0.6000061035f, 15);
325 mode->preemph[1] = -QCONST16(0.1799926758f, 15);
Jean-Marc Valin09213de2011-01-27 21:43:24 -0500326 mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */
327 mode->preemph[3] = QCONST16(2.2598876953f, 13);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400328 } else if(Fs < 40000) /* 32 kHz */
329 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500330 mode->preemph[0] = QCONST16(0.7799987793f, 15);
331 mode->preemph[1] = -QCONST16(0.1000061035f, 15);
Jean-Marc Valin09213de2011-01-27 21:43:24 -0500332 mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500333 mode->preemph[3] = QCONST16(1.3333740234f, 13);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400334 } else /* 48 kHz */
335 {
Jean-Marc Valin49f76802011-01-27 17:01:59 -0500336 mode->preemph[0] = QCONST16(0.8500061035f, 15);
337 mode->preemph[1] = QCONST16(0.0f, 15);
Jean-Marc Valin223b69d2010-07-16 11:47:50 -0400338 mode->preemph[2] = QCONST16(1.f, SIG_SHIFT);
339 mode->preemph[3] = QCONST16(1.f, 13);
340 }
341
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400342 mode->maxLM = LM;
343 mode->nbShortMdcts = 1<<LM;
Jean-Marc Valin60ff9992010-06-27 13:49:38 -0400344 mode->shortMdctSize = frame_size/mode->nbShortMdcts;
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400345 res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
346
347 mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400348 if (mode->eBands==NULL)
349 goto failure;
Timothy B. Terriberry006273c2013-05-21 18:16:13 -0700350#if !defined(SMALL_FOOTPRINT)
351 /* Make sure we don't allocate a band larger than our PVQ table.
352 208 should be enough, but let's be paranoid. */
353 if ((mode->eBands[mode->nbEBands] - mode->eBands[mode->nbEBands-1])<<LM >
354 208) {
355 goto failure;
356 }
357#endif
Jean-Marc Valinfbfddf72009-07-19 21:07:12 -0400358
Jean-Marc Valin85f41b22010-07-16 18:12:45 -0400359 mode->effEBands = mode->nbEBands;
360 while (mode->eBands[mode->effEBands] > mode->shortMdctSize)
361 mode->effEBands--;
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400362
Jean-Marc Valin08192e32009-07-02 13:28:55 -0400363 /* Overlap must be divisible by 4 */
Jean-Marc Valin70d30ff2010-12-13 13:50:29 -0500364 mode->overlap = ((mode->shortMdctSize>>2)<<2);
Jean-Marc Valin7f1c9422010-04-29 11:24:11 -0400365
Jean-Marc Valine3e2c262011-01-26 13:09:53 -0500366 compute_allocation_table(mode);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400367 if (mode->allocVectors==NULL)
368 goto failure;
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400369
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400370 window = (opus_val16*)opus_alloc(mode->overlap*sizeof(opus_val16));
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400371 if (window==NULL)
372 goto failure;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100373
Jean-Marc Valinf28062f2008-03-03 13:24:01 +1100374#ifndef FIXED_POINT
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100375 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100376 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 +1100377#else
378 for (i=0;i<mode->overlap;i++)
Jean-Marc Valin8974f002010-03-20 00:41:39 -0400379 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 +1100380#endif
Jean-Marc Valin3dbc1d02008-03-08 15:21:24 +1100381 mode->window = window;
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100382
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400383 logN = (opus_int16*)opus_alloc(mode->nbEBands*sizeof(opus_int16));
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400384 if (logN==NULL)
385 goto failure;
386
387 for (i=0;i<mode->nbEBands;i++)
Jean-Marc Valinaead79b2010-05-11 07:34:24 -0400388 logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
Jean-Marc Valinf400a3c2010-04-05 23:58:44 -0400389 mode->logN = logN;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400390
391 compute_pulse_cache(mode, mode->maxLM);
Jean-Marc Valin640f7fd2009-06-21 09:47:51 -0400392
Jean-Marc Valin27169ca2011-05-16 14:10:04 -0400393 if (clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts,
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -0500394 mode->maxLM, arch) == 0)
Jean-Marc Valin72513f32010-07-07 21:26:38 -0400395 goto failure;
396
Jean-Marc Valin680a9ec2008-03-10 14:52:18 +1100397 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400398 *error = OPUS_OK;
Jean-Marc Valin40603b22010-08-25 23:02:49 -0400399
Jean-Marc Valin4991a562008-02-18 13:37:40 +1100400 return mode;
Jean-Marc Valin27169ca2011-05-16 14:10:04 -0400401failure:
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400402 if (error)
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -0400403 *error = OPUS_ALLOC_FAIL;
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400404 if (mode!=NULL)
Jean-Marc Valin06237d72011-09-01 13:20:40 -0400405 opus_custom_mode_destroy(mode);
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400406 return NULL;
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500407#endif /* !CUSTOM_MODES */
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100408}
Jean-Marc Valin2a8c3712008-02-18 12:16:41 +1100409
Gregory Maxwellafc8d532011-09-28 15:13:26 -0400410#ifdef CUSTOM_MODES
Jean-Marc Valin06237d72011-09-01 13:20:40 -0400411void opus_custom_mode_destroy(CELTMode *mode)
Jean-Marc Valin81a82952008-02-17 22:41:29 +1100412{
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -0500413 int arch = opus_select_arch();
414
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400415 if (mode == NULL)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400416 return;
Jean-Marc Valin665da0b2011-01-30 12:15:12 -0500417#ifndef CUSTOM_MODES_ONLY
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500418 {
Gregory Maxwell40f956e2011-09-01 19:42:37 -0400419 int i;
420 for (i=0;i<TOTAL_MODES;i++)
421 {
422 if (mode == static_mode_list[i])
423 {
424 return;
425 }
426 }
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -0500427 }
Jean-Marc Valin665da0b2011-01-30 12:15:12 -0500428#endif /* CUSTOM_MODES_ONLY */
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400429 opus_free((opus_int16*)mode->eBands);
Mark Harrisd6d70372017-02-20 19:51:40 -0800430 opus_free((unsigned char*)mode->allocVectors);
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400431
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400432 opus_free((opus_val16*)mode->window);
433 opus_free((opus_int16*)mode->logN);
Jean-Marc Valin81b38c22008-02-29 21:08:49 +1100434
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400435 opus_free((opus_int16*)mode->cache.index);
436 opus_free((unsigned char*)mode->cache.bits);
437 opus_free((unsigned char*)mode->cache.caps);
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -0500438 clt_mdct_clear(&mode->mdct, arch);
Jean-Marc Valin073d0bc2010-05-05 21:37:53 -0400439
Jean-Marc Valin07f88402011-08-29 15:08:51 -0400440 opus_free((CELTMode *)mode);
Jean-Marc Valin2ca8fc32008-02-18 16:27:49 +1100441}
Gregory Maxwellafc8d532011-09-28 15:13:26 -0400442#endif