modes.c 14 KB

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