celt_decoder.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2010 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. #define CELT_DECODER_C
  30. #include "os_support.h"
  31. #include "mdct.h"
  32. #include <math.h>
  33. #include "celt.h"
  34. #include "pitch.h"
  35. #include "bands.h"
  36. #include "modes.h"
  37. #include "entcode.h"
  38. #include "quant_bands.h"
  39. #include "rate.h"
  40. #include "stack_alloc.h"
  41. #include "mathops.h"
  42. #include "float_cast.h"
  43. #include <stdarg.h>
  44. #include "celt_lpc.h"
  45. #include "vq.h"
  46. /**********************************************************************/
  47. /* */
  48. /* DECODER */
  49. /* */
  50. /**********************************************************************/
  51. #define DECODE_BUFFER_SIZE 2048
  52. /** Decoder state
  53. @brief Decoder state
  54. */
  55. struct OpusCustomDecoder {
  56. const OpusCustomMode *mode;
  57. int overlap;
  58. int channels;
  59. int stream_channels;
  60. int downsample;
  61. int start, end;
  62. int signalling;
  63. /* Everything beyond this point gets cleared on a reset */
  64. #define DECODER_RESET_START rng
  65. opus_uint32 rng;
  66. int error;
  67. int last_pitch_index;
  68. int loss_count;
  69. int postfilter_period;
  70. int postfilter_period_old;
  71. opus_val16 postfilter_gain;
  72. opus_val16 postfilter_gain_old;
  73. int postfilter_tapset;
  74. int postfilter_tapset_old;
  75. celt_sig preemph_memD[2];
  76. celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
  77. /* opus_val16 lpc[], Size = channels*LPC_ORDER */
  78. /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
  79. /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
  80. /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
  81. /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
  82. };
  83. int celt_decoder_get_size(int channels)
  84. {
  85. const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
  86. return opus_custom_decoder_get_size(mode, channels);
  87. }
  88. OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
  89. {
  90. int size = sizeof(struct CELTDecoder)
  91. + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
  92. + channels*LPC_ORDER*sizeof(opus_val16)
  93. + 4*2*mode->nbEBands*sizeof(opus_val16);
  94. return size;
  95. }
  96. #ifdef CUSTOM_MODES
  97. CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
  98. {
  99. int ret;
  100. CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
  101. ret = opus_custom_decoder_init(st, mode, channels);
  102. if (ret != OPUS_OK)
  103. {
  104. opus_custom_decoder_destroy(st);
  105. st = NULL;
  106. }
  107. if (error)
  108. *error = ret;
  109. return st;
  110. }
  111. #endif /* CUSTOM_MODES */
  112. int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
  113. {
  114. int ret;
  115. ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
  116. if (ret != OPUS_OK)
  117. return ret;
  118. st->downsample = resampling_factor(sampling_rate);
  119. if (st->downsample==0)
  120. return OPUS_BAD_ARG;
  121. else
  122. return OPUS_OK;
  123. }
  124. OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
  125. {
  126. if (channels < 0 || channels > 2)
  127. return OPUS_BAD_ARG;
  128. if (st==NULL)
  129. return OPUS_ALLOC_FAIL;
  130. OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
  131. st->mode = mode;
  132. st->overlap = mode->overlap;
  133. st->stream_channels = st->channels = channels;
  134. st->downsample = 1;
  135. st->start = 0;
  136. st->end = st->mode->effEBands;
  137. st->signalling = 1;
  138. st->loss_count = 0;
  139. opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
  140. return OPUS_OK;
  141. }
  142. #ifdef CUSTOM_MODES
  143. void opus_custom_decoder_destroy(CELTDecoder *st)
  144. {
  145. opus_free(st);
  146. }
  147. #endif /* CUSTOM_MODES */
  148. static inline opus_val16 SIG2WORD16(celt_sig x)
  149. {
  150. #ifdef FIXED_POINT
  151. x = PSHR32(x, SIG_SHIFT);
  152. x = MAX32(x, -32768);
  153. x = MIN32(x, 32767);
  154. return EXTRACT16(x);
  155. #else
  156. return (opus_val16)x;
  157. #endif
  158. }
  159. #ifndef RESYNTH
  160. static
  161. #endif
  162. void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, celt_sig * OPUS_RESTRICT scratch)
  163. {
  164. int c;
  165. int Nd;
  166. opus_val16 coef0, coef1;
  167. coef0 = coef[0];
  168. coef1 = coef[1];
  169. Nd = N/downsample;
  170. c=0; do {
  171. int j;
  172. celt_sig * OPUS_RESTRICT x;
  173. opus_val16 * OPUS_RESTRICT y;
  174. celt_sig m = mem[c];
  175. x =in[c];
  176. y = pcm+c;
  177. /* Shortcut for the standard (non-custom modes) case */
  178. if (coef1 == 0)
  179. {
  180. for (j=0;j<N;j++)
  181. {
  182. celt_sig tmp = x[j] + m;
  183. m = MULT16_32_Q15(coef0, tmp);
  184. scratch[j] = tmp;
  185. }
  186. } else {
  187. opus_val16 coef3 = coef[3];
  188. for (j=0;j<N;j++)
  189. {
  190. celt_sig tmp = x[j] + m;
  191. m = MULT16_32_Q15(coef0, tmp)
  192. - MULT16_32_Q15(coef1, x[j]);
  193. tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
  194. scratch[j] = tmp;
  195. }
  196. }
  197. mem[c] = m;
  198. /* Perform down-sampling */
  199. for (j=0;j<Nd;j++)
  200. y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample]));
  201. } while (++c<C);
  202. }
  203. /** Compute the IMDCT and apply window for all sub-frames and
  204. all channels in a frame */
  205. #ifndef RESYNTH
  206. static
  207. #endif
  208. void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
  209. celt_sig * OPUS_RESTRICT out_mem[], int C, int LM)
  210. {
  211. int b, c;
  212. int B;
  213. int N;
  214. int shift;
  215. const int overlap = OVERLAP(mode);
  216. if (shortBlocks)
  217. {
  218. B = shortBlocks;
  219. N = mode->shortMdctSize;
  220. shift = mode->maxLM;
  221. } else {
  222. B = 1;
  223. N = mode->shortMdctSize<<LM;
  224. shift = mode->maxLM-LM;
  225. }
  226. c=0; do {
  227. /* IMDCT on the interleaved the sub-frames, overlap-add is performed by the IMDCT */
  228. for (b=0;b<B;b++)
  229. clt_mdct_backward(&mode->mdct, &X[b+c*N*B], out_mem[c]+N*b, mode->window, overlap, shift, B);
  230. } while (++c<C);
  231. }
  232. static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
  233. {
  234. int i, curr, tf_select;
  235. int tf_select_rsv;
  236. int tf_changed;
  237. int logp;
  238. opus_uint32 budget;
  239. opus_uint32 tell;
  240. budget = dec->storage*8;
  241. tell = ec_tell(dec);
  242. logp = isTransient ? 2 : 4;
  243. tf_select_rsv = LM>0 && tell+logp+1<=budget;
  244. budget -= tf_select_rsv;
  245. tf_changed = curr = 0;
  246. for (i=start;i<end;i++)
  247. {
  248. if (tell+logp<=budget)
  249. {
  250. curr ^= ec_dec_bit_logp(dec, logp);
  251. tell = ec_tell(dec);
  252. tf_changed |= curr;
  253. }
  254. tf_res[i] = curr;
  255. logp = isTransient ? 4 : 5;
  256. }
  257. tf_select = 0;
  258. if (tf_select_rsv &&
  259. tf_select_table[LM][4*isTransient+0+tf_changed] !=
  260. tf_select_table[LM][4*isTransient+2+tf_changed])
  261. {
  262. tf_select = ec_dec_bit_logp(dec, 1);
  263. }
  264. for (i=start;i<end;i++)
  265. {
  266. tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
  267. }
  268. }
  269. static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM)
  270. {
  271. int c;
  272. int pitch_index;
  273. opus_val16 fade = Q15ONE;
  274. int i, len;
  275. const int C = st->channels;
  276. int offset;
  277. celt_sig *out_mem[2];
  278. celt_sig *decode_mem[2];
  279. opus_val16 *lpc;
  280. opus_val32 *out_syn[2];
  281. opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
  282. const OpusCustomMode *mode;
  283. int nbEBands;
  284. int overlap;
  285. const opus_int16 *eBands;
  286. VARDECL(celt_sig, scratch);
  287. SAVE_STACK;
  288. mode = st->mode;
  289. nbEBands = mode->nbEBands;
  290. overlap = mode->overlap;
  291. eBands = mode->eBands;
  292. c=0; do {
  293. decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
  294. out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
  295. } while (++c<C);
  296. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
  297. oldBandE = lpc+C*LPC_ORDER;
  298. oldLogE = oldBandE + 2*nbEBands;
  299. oldLogE2 = oldLogE + 2*nbEBands;
  300. backgroundLogE = oldLogE2 + 2*nbEBands;
  301. c=0; do {
  302. out_syn[c] = out_mem[c]+MAX_PERIOD-N;
  303. } while (++c<C);
  304. len = N+overlap;
  305. if (st->loss_count >= 5 || st->start!=0)
  306. {
  307. /* Noise-based PLC/CNG */
  308. VARDECL(celt_sig, freq);
  309. VARDECL(celt_norm, X);
  310. VARDECL(celt_ener, bandE);
  311. opus_uint32 seed;
  312. int effEnd;
  313. effEnd = st->end;
  314. if (effEnd > mode->effEBands)
  315. effEnd = mode->effEBands;
  316. ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
  317. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  318. ALLOC(bandE, nbEBands*C, celt_ener);
  319. if (st->loss_count >= 5)
  320. log2Amp(mode, st->start, st->end, bandE, backgroundLogE, C);
  321. else {
  322. /* Energy decay */
  323. opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
  324. c=0; do
  325. {
  326. for (i=st->start;i<st->end;i++)
  327. oldBandE[c*nbEBands+i] -= decay;
  328. } while (++c<C);
  329. log2Amp(mode, st->start, st->end, bandE, oldBandE, C);
  330. }
  331. seed = st->rng;
  332. for (c=0;c<C;c++)
  333. {
  334. for (i=st->start;i<mode->effEBands;i++)
  335. {
  336. int j;
  337. int boffs;
  338. int blen;
  339. boffs = N*c+(eBands[i]<<LM);
  340. blen = (eBands[i+1]-eBands[i])<<LM;
  341. for (j=0;j<blen;j++)
  342. {
  343. seed = celt_lcg_rand(seed);
  344. X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
  345. }
  346. renormalise_vector(X+boffs, blen, Q15ONE);
  347. }
  348. }
  349. st->rng = seed;
  350. denormalise_bands(mode, X, freq, bandE, st->start, mode->effEBands, C, 1<<LM);
  351. c=0; do {
  352. int bound = eBands[effEnd]<<LM;
  353. if (st->downsample!=1)
  354. bound = IMIN(bound, N/st->downsample);
  355. for (i=bound;i<N;i++)
  356. freq[c*N+i] = 0;
  357. } while (++c<C);
  358. c=0; do {
  359. OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap);
  360. } while (++c<C);
  361. compute_inv_mdcts(mode, 0, freq, out_syn, C, LM);
  362. } else {
  363. /* Pitch-based PLC */
  364. VARDECL(opus_val32, e);
  365. if (st->loss_count == 0)
  366. {
  367. opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
  368. /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
  369. search by using only part of the decode buffer */
  370. int poffset = 720;
  371. pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
  372. /* Max pitch is 100 samples (480 Hz) */
  373. pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
  374. poffset-100, &pitch_index);
  375. pitch_index = poffset-pitch_index;
  376. st->last_pitch_index = pitch_index;
  377. } else {
  378. pitch_index = st->last_pitch_index;
  379. fade = QCONST16(.8f,15);
  380. }
  381. ALLOC(e, MAX_PERIOD+2*overlap, opus_val32);
  382. c=0; do {
  383. opus_val16 exc[MAX_PERIOD];
  384. opus_val32 ac[LPC_ORDER+1];
  385. opus_val16 decay = 1;
  386. opus_val32 S1=0;
  387. opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  388. offset = MAX_PERIOD-pitch_index;
  389. for (i=0;i<MAX_PERIOD;i++)
  390. exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
  391. if (st->loss_count == 0)
  392. {
  393. _celt_autocorr(exc, ac, mode->window, overlap,
  394. LPC_ORDER, MAX_PERIOD);
  395. /* Noise floor -40 dB */
  396. #ifdef FIXED_POINT
  397. ac[0] += SHR32(ac[0],13);
  398. #else
  399. ac[0] *= 1.0001f;
  400. #endif
  401. /* Lag windowing */
  402. for (i=1;i<=LPC_ORDER;i++)
  403. {
  404. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  405. #ifdef FIXED_POINT
  406. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  407. #else
  408. ac[i] -= ac[i]*(.008f*i)*(.008f*i);
  409. #endif
  410. }
  411. _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
  412. }
  413. for (i=0;i<LPC_ORDER;i++)
  414. mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
  415. celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
  416. /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
  417. /* Check if the waveform is decaying (and if so how fast) */
  418. {
  419. opus_val32 E1=1, E2=1;
  420. int period;
  421. if (pitch_index <= MAX_PERIOD/2)
  422. period = pitch_index;
  423. else
  424. period = MAX_PERIOD/2;
  425. for (i=0;i<period;i++)
  426. {
  427. E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
  428. E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
  429. }
  430. if (E1 > E2)
  431. E1 = E2;
  432. decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
  433. }
  434. /* Copy excitation, taking decay into account */
  435. for (i=0;i<len+overlap;i++)
  436. {
  437. opus_val16 tmp;
  438. if (offset+i >= MAX_PERIOD)
  439. {
  440. offset -= pitch_index;
  441. decay = MULT16_16_Q15(decay, decay);
  442. }
  443. e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
  444. tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
  445. S1 += SHR32(MULT16_16(tmp,tmp),8);
  446. }
  447. for (i=0;i<LPC_ORDER;i++)
  448. mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
  449. for (i=0;i<len+overlap;i++)
  450. e[i] = MULT16_32_Q15(fade, e[i]);
  451. celt_iir(e, lpc+c*LPC_ORDER, e, len+overlap, LPC_ORDER, mem);
  452. {
  453. opus_val32 S2=0;
  454. for (i=0;i<len+overlap;i++)
  455. {
  456. opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
  457. S2 += SHR32(MULT16_16(tmp,tmp),8);
  458. }
  459. /* This checks for an "explosion" in the synthesis */
  460. #ifdef FIXED_POINT
  461. if (!(S1 > SHR32(S2,2)))
  462. #else
  463. /* Float test is written this way to catch NaNs at the same time */
  464. if (!(S1 > 0.2f*S2))
  465. #endif
  466. {
  467. for (i=0;i<len+overlap;i++)
  468. e[i] = 0;
  469. } else if (S1 < S2)
  470. {
  471. opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
  472. for (i=0;i<len+overlap;i++)
  473. e[i] = MULT16_32_Q15(ratio, e[i]);
  474. }
  475. }
  476. /* Apply post-filter to the MDCT overlap of the previous frame */
  477. comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
  478. st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
  479. NULL, 0);
  480. for (i=0;i<MAX_PERIOD+overlap-N;i++)
  481. out_mem[c][i] = out_mem[c][N+i];
  482. /* Apply TDAC to the concealed audio so that it blends with the
  483. previous and next frames */
  484. for (i=0;i<overlap/2;i++)
  485. {
  486. opus_val32 tmp;
  487. tmp = MULT16_32_Q15(mode->window[i], e[N+overlap-1-i]) +
  488. MULT16_32_Q15(mode->window[overlap-i-1], e[N+i ]);
  489. out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(mode->window[overlap-i-1], tmp);
  490. out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(mode->window[i], tmp);
  491. }
  492. for (i=0;i<N;i++)
  493. out_mem[c][MAX_PERIOD-N+i] = e[i];
  494. /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
  495. comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
  496. -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
  497. NULL, 0);
  498. for (i=0;i<overlap;i++)
  499. out_mem[c][MAX_PERIOD+i] = e[i];
  500. } while (++c<C);
  501. }
  502. ALLOC(scratch, N, celt_sig);
  503. deemphasis(out_syn, pcm, N, C, st->downsample, mode->preemph, st->preemph_memD, scratch);
  504. st->loss_count++;
  505. RESTORE_STACK;
  506. }
  507. int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec)
  508. {
  509. int c, i, N;
  510. int spread_decision;
  511. opus_int32 bits;
  512. ec_dec _dec;
  513. VARDECL(celt_sig, freq);
  514. VARDECL(celt_norm, X);
  515. VARDECL(celt_ener, bandE);
  516. VARDECL(int, fine_quant);
  517. VARDECL(int, pulses);
  518. VARDECL(int, cap);
  519. VARDECL(int, offsets);
  520. VARDECL(int, fine_priority);
  521. VARDECL(int, tf_res);
  522. VARDECL(unsigned char, collapse_masks);
  523. celt_sig *out_mem[2];
  524. celt_sig *decode_mem[2];
  525. celt_sig *out_syn[2];
  526. opus_val16 *lpc;
  527. opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
  528. int shortBlocks;
  529. int isTransient;
  530. int intra_ener;
  531. const int CC = st->channels;
  532. int LM, M;
  533. int effEnd;
  534. int codedBands;
  535. int alloc_trim;
  536. int postfilter_pitch;
  537. opus_val16 postfilter_gain;
  538. int intensity=0;
  539. int dual_stereo=0;
  540. opus_int32 total_bits;
  541. opus_int32 balance;
  542. opus_int32 tell;
  543. int dynalloc_logp;
  544. int postfilter_tapset;
  545. int anti_collapse_rsv;
  546. int anti_collapse_on=0;
  547. int silence;
  548. int C = st->stream_channels;
  549. const OpusCustomMode *mode;
  550. int nbEBands;
  551. int overlap;
  552. const opus_int16 *eBands;
  553. ALLOC_STACK;
  554. mode = st->mode;
  555. nbEBands = mode->nbEBands;
  556. overlap = mode->overlap;
  557. eBands = mode->eBands;
  558. frame_size *= st->downsample;
  559. c=0; do {
  560. decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
  561. out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
  562. } while (++c<CC);
  563. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC);
  564. oldBandE = lpc+CC*LPC_ORDER;
  565. oldLogE = oldBandE + 2*nbEBands;
  566. oldLogE2 = oldLogE + 2*nbEBands;
  567. backgroundLogE = oldLogE2 + 2*nbEBands;
  568. #ifdef CUSTOM_MODES
  569. if (st->signalling && data!=NULL)
  570. {
  571. int data0=data[0];
  572. /* Convert "standard mode" to Opus header */
  573. if (mode->Fs==48000 && mode->shortMdctSize==120)
  574. {
  575. data0 = fromOpus(data0);
  576. if (data0<0)
  577. return OPUS_INVALID_PACKET;
  578. }
  579. st->end = IMAX(1, mode->effEBands-2*(data0>>5));
  580. LM = (data0>>3)&0x3;
  581. C = 1 + ((data0>>2)&0x1);
  582. data++;
  583. len--;
  584. if (LM>mode->maxLM)
  585. return OPUS_INVALID_PACKET;
  586. if (frame_size < mode->shortMdctSize<<LM)
  587. return OPUS_BUFFER_TOO_SMALL;
  588. else
  589. frame_size = mode->shortMdctSize<<LM;
  590. } else {
  591. #else
  592. {
  593. #endif
  594. for (LM=0;LM<=mode->maxLM;LM++)
  595. if (mode->shortMdctSize<<LM==frame_size)
  596. break;
  597. if (LM>mode->maxLM)
  598. return OPUS_BAD_ARG;
  599. }
  600. M=1<<LM;
  601. if (len<0 || len>1275 || pcm==NULL)
  602. return OPUS_BAD_ARG;
  603. N = M*mode->shortMdctSize;
  604. effEnd = st->end;
  605. if (effEnd > mode->effEBands)
  606. effEnd = mode->effEBands;
  607. if (data == NULL || len<=1)
  608. {
  609. celt_decode_lost(st, pcm, N, LM);
  610. RESTORE_STACK;
  611. return frame_size/st->downsample;
  612. }
  613. if (dec == NULL)
  614. {
  615. ec_dec_init(&_dec,(unsigned char*)data,len);
  616. dec = &_dec;
  617. }
  618. if (C==1)
  619. {
  620. for (i=0;i<nbEBands;i++)
  621. oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]);
  622. }
  623. total_bits = len*8;
  624. tell = ec_tell(dec);
  625. if (tell >= total_bits)
  626. silence = 1;
  627. else if (tell==1)
  628. silence = ec_dec_bit_logp(dec, 15);
  629. else
  630. silence = 0;
  631. if (silence)
  632. {
  633. /* Pretend we've read all the remaining bits */
  634. tell = len*8;
  635. dec->nbits_total+=tell-ec_tell(dec);
  636. }
  637. postfilter_gain = 0;
  638. postfilter_pitch = 0;
  639. postfilter_tapset = 0;
  640. if (st->start==0 && tell+16 <= total_bits)
  641. {
  642. if(ec_dec_bit_logp(dec, 1))
  643. {
  644. int qg, octave;
  645. octave = ec_dec_uint(dec, 6);
  646. postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
  647. qg = ec_dec_bits(dec, 3);
  648. if (ec_tell(dec)+2<=total_bits)
  649. postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
  650. postfilter_gain = QCONST16(.09375f,15)*(qg+1);
  651. }
  652. tell = ec_tell(dec);
  653. }
  654. if (LM > 0 && tell+3 <= total_bits)
  655. {
  656. isTransient = ec_dec_bit_logp(dec, 3);
  657. tell = ec_tell(dec);
  658. }
  659. else
  660. isTransient = 0;
  661. if (isTransient)
  662. shortBlocks = M;
  663. else
  664. shortBlocks = 0;
  665. /* Decode the global flags (first symbols in the stream) */
  666. intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
  667. /* Get band energies */
  668. unquant_coarse_energy(mode, st->start, st->end, oldBandE,
  669. intra_ener, dec, C, LM);
  670. ALLOC(tf_res, nbEBands, int);
  671. tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
  672. tell = ec_tell(dec);
  673. spread_decision = SPREAD_NORMAL;
  674. if (tell+4 <= total_bits)
  675. spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
  676. ALLOC(cap, nbEBands, int);
  677. init_caps(mode,cap,LM,C);
  678. ALLOC(offsets, nbEBands, int);
  679. dynalloc_logp = 6;
  680. total_bits<<=BITRES;
  681. tell = ec_tell_frac(dec);
  682. for (i=st->start;i<st->end;i++)
  683. {
  684. int width, quanta;
  685. int dynalloc_loop_logp;
  686. int boost;
  687. width = C*(eBands[i+1]-eBands[i])<<LM;
  688. /* quanta is 6 bits, but no more than 1 bit/sample
  689. and no less than 1/8 bit/sample */
  690. quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
  691. dynalloc_loop_logp = dynalloc_logp;
  692. boost = 0;
  693. while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
  694. {
  695. int flag;
  696. flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
  697. tell = ec_tell_frac(dec);
  698. if (!flag)
  699. break;
  700. boost += quanta;
  701. total_bits -= quanta;
  702. dynalloc_loop_logp = 1;
  703. }
  704. offsets[i] = boost;
  705. /* Making dynalloc more likely */
  706. if (boost>0)
  707. dynalloc_logp = IMAX(2, dynalloc_logp-1);
  708. }
  709. ALLOC(fine_quant, nbEBands, int);
  710. alloc_trim = tell+(6<<BITRES) <= total_bits ?
  711. ec_dec_icdf(dec, trim_icdf, 7) : 5;
  712. bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
  713. anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
  714. bits -= anti_collapse_rsv;
  715. ALLOC(pulses, nbEBands, int);
  716. ALLOC(fine_priority, nbEBands, int);
  717. codedBands = compute_allocation(mode, st->start, st->end, offsets, cap,
  718. alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
  719. fine_quant, fine_priority, C, LM, dec, 0, 0);
  720. unquant_fine_energy(mode, st->start, st->end, oldBandE, fine_quant, dec, C);
  721. /* Decode fixed codebook */
  722. ALLOC(collapse_masks, C*nbEBands, unsigned char);
  723. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  724. quant_all_bands(0, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
  725. NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
  726. len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
  727. if (anti_collapse_rsv > 0)
  728. {
  729. anti_collapse_on = ec_dec_bits(dec, 1);
  730. }
  731. unquant_energy_finalise(mode, st->start, st->end, oldBandE,
  732. fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
  733. if (anti_collapse_on)
  734. anti_collapse(mode, X, collapse_masks, LM, C, N,
  735. st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
  736. ALLOC(bandE, nbEBands*C, celt_ener);
  737. log2Amp(mode, st->start, st->end, bandE, oldBandE, C);
  738. if (silence)
  739. {
  740. for (i=0;i<C*nbEBands;i++)
  741. {
  742. bandE[i] = 0;
  743. oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
  744. }
  745. }
  746. ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
  747. /* Synthesis */
  748. denormalise_bands(mode, X, freq, bandE, st->start, effEnd, C, M);
  749. c=0; do {
  750. OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap);
  751. } while (++c<CC);
  752. c=0; do {
  753. int bound = M*eBands[effEnd];
  754. if (st->downsample!=1)
  755. bound = IMIN(bound, N/st->downsample);
  756. for (i=bound;i<N;i++)
  757. freq[c*N+i] = 0;
  758. } while (++c<C);
  759. c=0; do {
  760. out_syn[c] = out_mem[c]+MAX_PERIOD-N;
  761. } while (++c<CC);
  762. if (CC==2&&C==1)
  763. {
  764. for (i=0;i<N;i++)
  765. freq[N+i] = freq[i];
  766. }
  767. if (CC==1&&C==2)
  768. {
  769. for (i=0;i<N;i++)
  770. freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
  771. }
  772. /* Compute inverse MDCTs */
  773. compute_inv_mdcts(mode, shortBlocks, freq, out_syn, CC, LM);
  774. c=0; do {
  775. st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
  776. st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
  777. comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
  778. st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
  779. mode->window, overlap);
  780. if (LM!=0)
  781. comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
  782. st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
  783. mode->window, overlap);
  784. } while (++c<CC);
  785. st->postfilter_period_old = st->postfilter_period;
  786. st->postfilter_gain_old = st->postfilter_gain;
  787. st->postfilter_tapset_old = st->postfilter_tapset;
  788. st->postfilter_period = postfilter_pitch;
  789. st->postfilter_gain = postfilter_gain;
  790. st->postfilter_tapset = postfilter_tapset;
  791. if (LM!=0)
  792. {
  793. st->postfilter_period_old = st->postfilter_period;
  794. st->postfilter_gain_old = st->postfilter_gain;
  795. st->postfilter_tapset_old = st->postfilter_tapset;
  796. }
  797. if (C==1) {
  798. for (i=0;i<nbEBands;i++)
  799. oldBandE[nbEBands+i]=oldBandE[i];
  800. }
  801. /* In case start or end were to change */
  802. if (!isTransient)
  803. {
  804. for (i=0;i<2*nbEBands;i++)
  805. oldLogE2[i] = oldLogE[i];
  806. for (i=0;i<2*nbEBands;i++)
  807. oldLogE[i] = oldBandE[i];
  808. for (i=0;i<2*nbEBands;i++)
  809. backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
  810. } else {
  811. for (i=0;i<2*nbEBands;i++)
  812. oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
  813. }
  814. c=0; do
  815. {
  816. for (i=0;i<st->start;i++)
  817. {
  818. oldBandE[c*nbEBands+i]=0;
  819. oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
  820. }
  821. for (i=st->end;i<nbEBands;i++)
  822. {
  823. oldBandE[c*nbEBands+i]=0;
  824. oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
  825. }
  826. } while (++c<2);
  827. st->rng = dec->rng;
  828. /* We reuse freq[] as scratch space for the de-emphasis */
  829. deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, freq);
  830. st->loss_count = 0;
  831. RESTORE_STACK;
  832. if (ec_tell(dec) > 8*len)
  833. return OPUS_INTERNAL_ERROR;
  834. if(ec_get_error(dec))
  835. st->error = 1;
  836. return frame_size/st->downsample;
  837. }
  838. #ifdef CUSTOM_MODES
  839. #ifdef FIXED_POINT
  840. int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
  841. {
  842. return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
  843. }
  844. #ifndef DISABLE_FLOAT_API
  845. int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
  846. {
  847. int j, ret, C, N;
  848. VARDECL(opus_int16, out);
  849. ALLOC_STACK;
  850. if (pcm==NULL)
  851. return OPUS_BAD_ARG;
  852. C = st->channels;
  853. N = frame_size;
  854. ALLOC(out, C*N, opus_int16);
  855. ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
  856. if (ret>0)
  857. for (j=0;j<C*ret;j++)
  858. pcm[j]=out[j]*(1.f/32768.f);
  859. RESTORE_STACK;
  860. return ret;
  861. }
  862. #endif /* DISABLE_FLOAT_API */
  863. #else
  864. int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
  865. {
  866. return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
  867. }
  868. int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
  869. {
  870. int j, ret, C, N;
  871. VARDECL(celt_sig, out);
  872. ALLOC_STACK;
  873. if (pcm==NULL)
  874. return OPUS_BAD_ARG;
  875. C = st->channels;
  876. N = frame_size;
  877. ALLOC(out, C*N, celt_sig);
  878. ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
  879. if (ret>0)
  880. for (j=0;j<C*ret;j++)
  881. pcm[j] = FLOAT2INT16 (out[j]);
  882. RESTORE_STACK;
  883. return ret;
  884. }
  885. #endif
  886. #endif /* CUSTOM_MODES */
  887. int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
  888. {
  889. va_list ap;
  890. va_start(ap, request);
  891. switch (request)
  892. {
  893. case CELT_SET_START_BAND_REQUEST:
  894. {
  895. opus_int32 value = va_arg(ap, opus_int32);
  896. if (value<0 || value>=st->mode->nbEBands)
  897. goto bad_arg;
  898. st->start = value;
  899. }
  900. break;
  901. case CELT_SET_END_BAND_REQUEST:
  902. {
  903. opus_int32 value = va_arg(ap, opus_int32);
  904. if (value<1 || value>st->mode->nbEBands)
  905. goto bad_arg;
  906. st->end = value;
  907. }
  908. break;
  909. case CELT_SET_CHANNELS_REQUEST:
  910. {
  911. opus_int32 value = va_arg(ap, opus_int32);
  912. if (value<1 || value>2)
  913. goto bad_arg;
  914. st->stream_channels = value;
  915. }
  916. break;
  917. case CELT_GET_AND_CLEAR_ERROR_REQUEST:
  918. {
  919. opus_int32 *value = va_arg(ap, opus_int32*);
  920. if (value==NULL)
  921. goto bad_arg;
  922. *value=st->error;
  923. st->error = 0;
  924. }
  925. break;
  926. case OPUS_GET_LOOKAHEAD_REQUEST:
  927. {
  928. opus_int32 *value = va_arg(ap, opus_int32*);
  929. if (value==NULL)
  930. goto bad_arg;
  931. *value = st->overlap/st->downsample;
  932. }
  933. break;
  934. case OPUS_RESET_STATE:
  935. {
  936. int i;
  937. opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
  938. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
  939. oldBandE = lpc+st->channels*LPC_ORDER;
  940. oldLogE = oldBandE + 2*st->mode->nbEBands;
  941. oldLogE2 = oldLogE + 2*st->mode->nbEBands;
  942. OPUS_CLEAR((char*)&st->DECODER_RESET_START,
  943. opus_custom_decoder_get_size(st->mode, st->channels)-
  944. ((char*)&st->DECODER_RESET_START - (char*)st));
  945. for (i=0;i<2*st->mode->nbEBands;i++)
  946. oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
  947. }
  948. break;
  949. case OPUS_GET_PITCH_REQUEST:
  950. {
  951. opus_int32 *value = va_arg(ap, opus_int32*);
  952. if (value==NULL)
  953. goto bad_arg;
  954. *value = st->postfilter_period;
  955. }
  956. break;
  957. #ifdef OPUS_BUILD
  958. case CELT_GET_MODE_REQUEST:
  959. {
  960. const CELTMode ** value = va_arg(ap, const CELTMode**);
  961. if (value==0)
  962. goto bad_arg;
  963. *value=st->mode;
  964. }
  965. break;
  966. case CELT_SET_SIGNALLING_REQUEST:
  967. {
  968. opus_int32 value = va_arg(ap, opus_int32);
  969. st->signalling = value;
  970. }
  971. break;
  972. case OPUS_GET_FINAL_RANGE_REQUEST:
  973. {
  974. opus_uint32 * value = va_arg(ap, opus_uint32 *);
  975. if (value==0)
  976. goto bad_arg;
  977. *value=st->rng;
  978. }
  979. break;
  980. #endif
  981. default:
  982. goto bad_request;
  983. }
  984. va_end(ap);
  985. return OPUS_OK;
  986. bad_arg:
  987. va_end(ap);
  988. return OPUS_BAD_ARG;
  989. bad_request:
  990. va_end(ap);
  991. return OPUS_UNIMPLEMENTED;
  992. }