modes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* 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 */
  5. /*
  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. - Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. - Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "celt.h"
  30. #include "modes.h"
  31. #include "rate.h"
  32. #include "os_support.h"
  33. #include "stack_alloc.h"
  34. #include "quant_bands.h"
  35. static const opus_int16 eband5ms[] = {
  36. /*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 */
  37. 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100
  38. };
  39. /* Alternate tuning (partially derived from Vorbis) */
  40. #define BITALLOC_SIZE 11
  41. /* Bit allocation table in units of 1/32 bit/sample (0.1875 dB SNR) */
  42. static const unsigned char band_allocation[] = {
  43. /*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 */
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 110,100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0,
  47. 118,110,103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0,
  48. 126,119,112,104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0,
  49. 134,127,120,114,103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1,
  50. 144,137,130,124,113,107,101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1,
  51. 152,145,138,132,123,117,111,105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1,
  52. 162,155,148,142,133,127,121,115,108,102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1,
  53. 172,165,158,152,143,137,131,125,118,112,106,100, 94, 87, 81, 75, 69, 63, 56, 45, 20,
  54. 200,200,200,200,200,200,200,200,198,193,188,183,178,173,168,163,158,153,148,129,104,
  55. };
  56. #ifndef CUSTOM_MODES_ONLY
  57. #ifdef FIXED_POINT
  58. #include "static_modes_fixed.h"
  59. #else
  60. #include "static_modes_float.h"
  61. #endif
  62. #endif /* CUSTOM_MODES_ONLY */
  63. #ifndef M_PI
  64. #define M_PI 3.141592653
  65. #endif
  66. #ifdef CUSTOM_MODES
  67. /* Defining 25 critical bands for the full 0-20 kHz audio bandwidth
  68. Taken from http://ccrma.stanford.edu/~jos/bbt/Bark_Frequency_Scale.html */
  69. #define BARK_BANDS 25
  70. static const opus_int16 bark_freq[BARK_BANDS+1] = {
  71. 0, 100, 200, 300, 400,
  72. 510, 630, 770, 920, 1080,
  73. 1270, 1480, 1720, 2000, 2320,
  74. 2700, 3150, 3700, 4400, 5300,
  75. 6400, 7700, 9500, 12000, 15500,
  76. 20000};
  77. static opus_int16 *compute_ebands(opus_int32 Fs, int frame_size, int res, int *nbEBands)
  78. {
  79. opus_int16 *eBands;
  80. int i, j, lin, low, high, nBark, offset=0;
  81. /* All modes that have 2.5 ms short blocks use the same definition */
  82. if (Fs == 400*(opus_int32)frame_size)
  83. {
  84. *nbEBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
  85. eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+1));
  86. for (i=0;i<*nbEBands+1;i++)
  87. eBands[i] = eband5ms[i];
  88. return eBands;
  89. }
  90. /* Find the number of critical bands supported by our sampling rate */
  91. for (nBark=1;nBark<BARK_BANDS;nBark++)
  92. if (bark_freq[nBark+1]*2 >= Fs)
  93. break;
  94. /* Find where the linear part ends (i.e. where the spacing is more than min_width */
  95. for (lin=0;lin<nBark;lin++)
  96. if (bark_freq[lin+1]-bark_freq[lin] >= res)
  97. break;
  98. low = (bark_freq[lin]+res/2)/res;
  99. high = nBark-lin;
  100. *nbEBands = low+high;
  101. eBands = opus_alloc(sizeof(opus_int16)*(*nbEBands+2));
  102. if (eBands==NULL)
  103. return NULL;
  104. /* Linear spacing (min_width) */
  105. for (i=0;i<low;i++)
  106. eBands[i] = i;
  107. if (low>0)
  108. offset = eBands[low-1]*res - bark_freq[lin-1];
  109. /* Spacing follows critical bands */
  110. for (i=0;i<high;i++)
  111. {
  112. int target = bark_freq[lin+i];
  113. /* Round to an even value */
  114. eBands[i+low] = (target+offset/2+res)/(2*res)*2;
  115. offset = eBands[i+low]*res - target;
  116. }
  117. /* Enforce the minimum spacing at the boundary */
  118. for (i=0;i<*nbEBands;i++)
  119. if (eBands[i] < i)
  120. eBands[i] = i;
  121. /* Round to an even value */
  122. eBands[*nbEBands] = (bark_freq[nBark]+res)/(2*res)*2;
  123. if (eBands[*nbEBands] > frame_size)
  124. eBands[*nbEBands] = frame_size;
  125. for (i=1;i<*nbEBands-1;i++)
  126. {
  127. if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
  128. {
  129. eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
  130. }
  131. }
  132. /* Remove any empty bands. */
  133. for (i=j=0;i<*nbEBands;i++)
  134. if(eBands[i+1]>eBands[j])
  135. eBands[++j]=eBands[i+1];
  136. *nbEBands=j;
  137. for (i=1;i<*nbEBands;i++)
  138. {
  139. /* Every band must be smaller than the last band. */
  140. celt_assert(eBands[i]-eBands[i-1]<=eBands[*nbEBands]-eBands[*nbEBands-1]);
  141. /* Each band must be no larger than twice the size of the previous one. */
  142. celt_assert(eBands[i+1]-eBands[i]<=2*(eBands[i]-eBands[i-1]));
  143. }
  144. return eBands;
  145. }
  146. static void compute_allocation_table(CELTMode *mode)
  147. {
  148. int i, j;
  149. unsigned char *allocVectors;
  150. int maxBands = sizeof(eband5ms)/sizeof(eband5ms[0])-1;
  151. mode->nbAllocVectors = BITALLOC_SIZE;
  152. allocVectors = opus_alloc(sizeof(unsigned char)*(BITALLOC_SIZE*mode->nbEBands));
  153. if (allocVectors==NULL)
  154. return;
  155. /* Check for standard mode */
  156. if (mode->Fs == 400*(opus_int32)mode->shortMdctSize)
  157. {
  158. for (i=0;i<BITALLOC_SIZE*mode->nbEBands;i++)
  159. allocVectors[i] = band_allocation[i];
  160. mode->allocVectors = allocVectors;
  161. return;
  162. }
  163. /* If not the standard mode, interpolate */
  164. /* Compute per-codec-band allocation from per-critical-band matrix */
  165. for (i=0;i<BITALLOC_SIZE;i++)
  166. {
  167. for (j=0;j<mode->nbEBands;j++)
  168. {
  169. int k;
  170. for (k=0;k<maxBands;k++)
  171. {
  172. if (400*(opus_int32)eband5ms[k] > mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize)
  173. break;
  174. }
  175. if (k>maxBands-1)
  176. allocVectors[i*mode->nbEBands+j] = band_allocation[i*maxBands + maxBands-1];
  177. else {
  178. opus_int32 a0, a1;
  179. a1 = mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize - 400*(opus_int32)eband5ms[k-1];
  180. a0 = 400*(opus_int32)eband5ms[k] - mode->eBands[j]*(opus_int32)mode->Fs/mode->shortMdctSize;
  181. allocVectors[i*mode->nbEBands+j] = (a0*band_allocation[i*maxBands+k-1]
  182. + a1*band_allocation[i*maxBands+k])/(a0+a1);
  183. }
  184. }
  185. }
  186. /*printf ("\n");
  187. for (i=0;i<BITALLOC_SIZE;i++)
  188. {
  189. for (j=0;j<mode->nbEBands;j++)
  190. printf ("%d ", allocVectors[i*mode->nbEBands+j]);
  191. printf ("\n");
  192. }
  193. exit(0);*/
  194. mode->allocVectors = allocVectors;
  195. }
  196. #endif /* CUSTOM_MODES */
  197. CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error)
  198. {
  199. int i;
  200. #ifdef CUSTOM_MODES
  201. CELTMode *mode=NULL;
  202. int res;
  203. opus_val16 *window;
  204. opus_int16 *logN;
  205. int LM;
  206. ALLOC_STACK;
  207. #if !defined(VAR_ARRAYS) && !defined(USE_ALLOCA)
  208. if (global_stack==NULL)
  209. goto failure;
  210. #endif
  211. #endif
  212. #ifndef CUSTOM_MODES_ONLY
  213. for (i=0;i<TOTAL_MODES;i++)
  214. {
  215. int j;
  216. for (j=0;j<4;j++)
  217. {
  218. if (Fs == static_mode_list[i]->Fs &&
  219. (frame_size<<j) == static_mode_list[i]->shortMdctSize*static_mode_list[i]->nbShortMdcts)
  220. {
  221. if (error)
  222. *error = OPUS_OK;
  223. return (CELTMode*)static_mode_list[i];
  224. }
  225. }
  226. }
  227. #endif /* CUSTOM_MODES_ONLY */
  228. #ifndef CUSTOM_MODES
  229. if (error)
  230. *error = OPUS_BAD_ARG;
  231. return NULL;
  232. #else
  233. /* The good thing here is that permutation of the arguments will automatically be invalid */
  234. if (Fs < 8000 || Fs > 96000)
  235. {
  236. if (error)
  237. *error = OPUS_BAD_ARG;
  238. return NULL;
  239. }
  240. if (frame_size < 40 || frame_size > 1024 || frame_size%2!=0)
  241. {
  242. if (error)
  243. *error = OPUS_BAD_ARG;
  244. return NULL;
  245. }
  246. /* Frames of less than 1ms are not supported. */
  247. if ((opus_int32)frame_size*1000 < Fs)
  248. {
  249. if (error)
  250. *error = OPUS_BAD_ARG;
  251. return NULL;
  252. }
  253. if ((opus_int32)frame_size*75 >= Fs && (frame_size%16)==0)
  254. {
  255. LM = 3;
  256. } else if ((opus_int32)frame_size*150 >= Fs && (frame_size%8)==0)
  257. {
  258. LM = 2;
  259. } else if ((opus_int32)frame_size*300 >= Fs && (frame_size%4)==0)
  260. {
  261. LM = 1;
  262. } else
  263. {
  264. LM = 0;
  265. }
  266. /* Shorts longer than 3.3ms are not supported. */
  267. if ((opus_int32)(frame_size>>LM)*300 > Fs)
  268. {
  269. if (error)
  270. *error = OPUS_BAD_ARG;
  271. return NULL;
  272. }
  273. mode = opus_alloc(sizeof(CELTMode));
  274. if (mode==NULL)
  275. goto failure;
  276. mode->Fs = Fs;
  277. /* Pre/de-emphasis depends on sampling rate. The "standard" pre-emphasis
  278. is defined as A(z) = 1 - 0.85*z^-1 at 48 kHz. Other rates should
  279. approximate that. */
  280. if(Fs < 12000) /* 8 kHz */
  281. {
  282. mode->preemph[0] = QCONST16(0.3500061035f, 15);
  283. mode->preemph[1] = -QCONST16(0.1799926758f, 15);
  284. mode->preemph[2] = QCONST16(0.2719968125f, SIG_SHIFT); /* exact 1/preemph[3] */
  285. mode->preemph[3] = QCONST16(3.6765136719f, 13);
  286. } else if(Fs < 24000) /* 16 kHz */
  287. {
  288. mode->preemph[0] = QCONST16(0.6000061035f, 15);
  289. mode->preemph[1] = -QCONST16(0.1799926758f, 15);
  290. mode->preemph[2] = QCONST16(0.4424998650f, SIG_SHIFT); /* exact 1/preemph[3] */
  291. mode->preemph[3] = QCONST16(2.2598876953f, 13);
  292. } else if(Fs < 40000) /* 32 kHz */
  293. {
  294. mode->preemph[0] = QCONST16(0.7799987793f, 15);
  295. mode->preemph[1] = -QCONST16(0.1000061035f, 15);
  296. mode->preemph[2] = QCONST16(0.7499771125f, SIG_SHIFT); /* exact 1/preemph[3] */
  297. mode->preemph[3] = QCONST16(1.3333740234f, 13);
  298. } else /* 48 kHz */
  299. {
  300. mode->preemph[0] = QCONST16(0.8500061035f, 15);
  301. mode->preemph[1] = QCONST16(0.0f, 15);
  302. mode->preemph[2] = QCONST16(1.f, SIG_SHIFT);
  303. mode->preemph[3] = QCONST16(1.f, 13);
  304. }
  305. mode->maxLM = LM;
  306. mode->nbShortMdcts = 1<<LM;
  307. mode->shortMdctSize = frame_size/mode->nbShortMdcts;
  308. res = (mode->Fs+mode->shortMdctSize)/(2*mode->shortMdctSize);
  309. mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands);
  310. if (mode->eBands==NULL)
  311. goto failure;
  312. mode->effEBands = mode->nbEBands;
  313. while (mode->eBands[mode->effEBands] > mode->shortMdctSize)
  314. mode->effEBands--;
  315. /* Overlap must be divisible by 4 */
  316. mode->overlap = ((mode->shortMdctSize>>2)<<2);
  317. compute_allocation_table(mode);
  318. if (mode->allocVectors==NULL)
  319. goto failure;
  320. window = (opus_val16*)opus_alloc(mode->overlap*sizeof(opus_val16));
  321. if (window==NULL)
  322. goto failure;
  323. #ifndef FIXED_POINT
  324. for (i=0;i<mode->overlap;i++)
  325. window[i] = Q15ONE*sin(.5*M_PI* sin(.5*M_PI*(i+.5)/mode->overlap) * sin(.5*M_PI*(i+.5)/mode->overlap));
  326. #else
  327. for (i=0;i<mode->overlap;i++)
  328. 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))));
  329. #endif
  330. mode->window = window;
  331. logN = (opus_int16*)opus_alloc(mode->nbEBands*sizeof(opus_int16));
  332. if (logN==NULL)
  333. goto failure;
  334. for (i=0;i<mode->nbEBands;i++)
  335. logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
  336. mode->logN = logN;
  337. compute_pulse_cache(mode, mode->maxLM);
  338. if (clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts,
  339. mode->maxLM) == 0)
  340. goto failure;
  341. if (error)
  342. *error = OPUS_OK;
  343. return mode;
  344. failure:
  345. if (error)
  346. *error = OPUS_ALLOC_FAIL;
  347. if (mode!=NULL)
  348. opus_custom_mode_destroy(mode);
  349. return NULL;
  350. #endif /* !CUSTOM_MODES */
  351. }
  352. void opus_custom_mode_destroy(CELTMode *mode)
  353. {
  354. if (mode == NULL)
  355. return;
  356. #ifdef CUSTOM_MODES
  357. #ifndef CUSTOM_MODES_ONLY
  358. {
  359. int i;
  360. for (i=0;i<TOTAL_MODES;i++)
  361. {
  362. if (mode == static_mode_list[i])
  363. {
  364. return;
  365. }
  366. }
  367. }
  368. #endif /* CUSTOM_MODES_ONLY */
  369. opus_free((opus_int16*)mode->eBands);
  370. opus_free((opus_int16*)mode->allocVectors);
  371. opus_free((opus_val16*)mode->window);
  372. opus_free((opus_int16*)mode->logN);
  373. opus_free((opus_int16*)mode->cache.index);
  374. opus_free((unsigned char*)mode->cache.bits);
  375. opus_free((unsigned char*)mode->cache.caps);
  376. clt_mdct_clear(&mode->mdct);
  377. opus_free((CELTMode *)mode);
  378. #endif
  379. }