modes.c 14 KB

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