celt_decoder.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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 "cpu_support.h"
  31. #include "os_support.h"
  32. #include "mdct.h"
  33. #include <math.h>
  34. #include "celt.h"
  35. #include "pitch.h"
  36. #include "bands.h"
  37. #include "modes.h"
  38. #include "entcode.h"
  39. #include "quant_bands.h"
  40. #include "rate.h"
  41. #include "stack_alloc.h"
  42. #include "mathops.h"
  43. #include "float_cast.h"
  44. #include <stdarg.h>
  45. #include "celt_lpc.h"
  46. #include "vq.h"
  47. #if defined(SMALL_FOOTPRINT) && defined(FIXED_POINT)
  48. #define NORM_ALIASING_HACK
  49. #endif
  50. /**********************************************************************/
  51. /* */
  52. /* DECODER */
  53. /* */
  54. /**********************************************************************/
  55. #define DECODE_BUFFER_SIZE 2048
  56. /** Decoder state
  57. @brief Decoder state
  58. */
  59. struct OpusCustomDecoder {
  60. const OpusCustomMode *mode;
  61. int overlap;
  62. int channels;
  63. int stream_channels;
  64. int downsample;
  65. int start, end;
  66. int signalling;
  67. int disable_inv;
  68. int arch;
  69. /* Everything beyond this point gets cleared on a reset */
  70. #define DECODER_RESET_START rng
  71. opus_uint32 rng;
  72. int error;
  73. int last_pitch_index;
  74. int loss_count;
  75. int skip_plc;
  76. int postfilter_period;
  77. int postfilter_period_old;
  78. opus_val16 postfilter_gain;
  79. opus_val16 postfilter_gain_old;
  80. int postfilter_tapset;
  81. int postfilter_tapset_old;
  82. celt_sig preemph_memD[2];
  83. celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
  84. /* opus_val16 lpc[], Size = channels*LPC_ORDER */
  85. /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
  86. /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
  87. /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
  88. /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
  89. };
  90. int celt_decoder_get_size(int channels)
  91. {
  92. const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
  93. return opus_custom_decoder_get_size(mode, channels);
  94. }
  95. OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
  96. {
  97. int size = sizeof(struct CELTDecoder)
  98. + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
  99. + channels*LPC_ORDER*sizeof(opus_val16)
  100. + 4*2*mode->nbEBands*sizeof(opus_val16);
  101. return size;
  102. }
  103. #ifdef CUSTOM_MODES
  104. CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
  105. {
  106. int ret;
  107. CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
  108. ret = opus_custom_decoder_init(st, mode, channels);
  109. if (ret != OPUS_OK)
  110. {
  111. opus_custom_decoder_destroy(st);
  112. st = NULL;
  113. }
  114. if (error)
  115. *error = ret;
  116. return st;
  117. }
  118. #endif /* CUSTOM_MODES */
  119. int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
  120. {
  121. int ret;
  122. ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
  123. if (ret != OPUS_OK)
  124. return ret;
  125. st->downsample = resampling_factor(sampling_rate);
  126. if (st->downsample==0)
  127. return OPUS_BAD_ARG;
  128. else
  129. return OPUS_OK;
  130. }
  131. OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
  132. {
  133. if (channels < 0 || channels > 2)
  134. return OPUS_BAD_ARG;
  135. if (st==NULL)
  136. return OPUS_ALLOC_FAIL;
  137. OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
  138. st->mode = mode;
  139. st->overlap = mode->overlap;
  140. st->stream_channels = st->channels = channels;
  141. st->downsample = 1;
  142. st->start = 0;
  143. st->end = st->mode->effEBands;
  144. st->signalling = 1;
  145. #ifdef ENABLE_UPDATE_DRAFT
  146. st->disable_inv = channels == 1;
  147. #else
  148. st->disable_inv = 0;
  149. #endif
  150. st->arch = opus_select_arch();
  151. opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
  152. return OPUS_OK;
  153. }
  154. #ifdef CUSTOM_MODES
  155. void opus_custom_decoder_destroy(CELTDecoder *st)
  156. {
  157. opus_free(st);
  158. }
  159. #endif /* CUSTOM_MODES */
  160. #ifndef CUSTOM_MODES
  161. /* Special case for stereo with no downsampling and no accumulation. This is
  162. quite common and we can make it faster by processing both channels in the
  163. same loop, reducing overhead due to the dependency loop in the IIR filter. */
  164. static void deemphasis_stereo_simple(celt_sig *in[], opus_val16 *pcm, int N, const opus_val16 coef0,
  165. celt_sig *mem)
  166. {
  167. celt_sig * OPUS_RESTRICT x0;
  168. celt_sig * OPUS_RESTRICT x1;
  169. celt_sig m0, m1;
  170. int j;
  171. x0=in[0];
  172. x1=in[1];
  173. m0 = mem[0];
  174. m1 = mem[1];
  175. for (j=0;j<N;j++)
  176. {
  177. celt_sig tmp0, tmp1;
  178. /* Add VERY_SMALL to x[] first to reduce dependency chain. */
  179. tmp0 = x0[j] + VERY_SMALL + m0;
  180. tmp1 = x1[j] + VERY_SMALL + m1;
  181. m0 = MULT16_32_Q15(coef0, tmp0);
  182. m1 = MULT16_32_Q15(coef0, tmp1);
  183. pcm[2*j ] = SCALEOUT(SIG2WORD16(tmp0));
  184. pcm[2*j+1] = SCALEOUT(SIG2WORD16(tmp1));
  185. }
  186. mem[0] = m0;
  187. mem[1] = m1;
  188. }
  189. #endif
  190. #ifndef RESYNTH
  191. static
  192. #endif
  193. void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef,
  194. celt_sig *mem, int accum)
  195. {
  196. int c;
  197. int Nd;
  198. int apply_downsampling=0;
  199. opus_val16 coef0;
  200. VARDECL(celt_sig, scratch);
  201. SAVE_STACK;
  202. #ifndef CUSTOM_MODES
  203. /* Short version for common case. */
  204. if (downsample == 1 && C == 2 && !accum)
  205. {
  206. deemphasis_stereo_simple(in, pcm, N, coef[0], mem);
  207. return;
  208. }
  209. #endif
  210. #ifndef FIXED_POINT
  211. (void)accum;
  212. celt_assert(accum==0);
  213. #endif
  214. ALLOC(scratch, N, celt_sig);
  215. coef0 = coef[0];
  216. Nd = N/downsample;
  217. c=0; do {
  218. int j;
  219. celt_sig * OPUS_RESTRICT x;
  220. opus_val16 * OPUS_RESTRICT y;
  221. celt_sig m = mem[c];
  222. x =in[c];
  223. y = pcm+c;
  224. #ifdef CUSTOM_MODES
  225. if (coef[1] != 0)
  226. {
  227. opus_val16 coef1 = coef[1];
  228. opus_val16 coef3 = coef[3];
  229. for (j=0;j<N;j++)
  230. {
  231. celt_sig tmp = x[j] + m + VERY_SMALL;
  232. m = MULT16_32_Q15(coef0, tmp)
  233. - MULT16_32_Q15(coef1, x[j]);
  234. tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
  235. scratch[j] = tmp;
  236. }
  237. apply_downsampling=1;
  238. } else
  239. #endif
  240. if (downsample>1)
  241. {
  242. /* Shortcut for the standard (non-custom modes) case */
  243. for (j=0;j<N;j++)
  244. {
  245. celt_sig tmp = x[j] + VERY_SMALL + m;
  246. m = MULT16_32_Q15(coef0, tmp);
  247. scratch[j] = tmp;
  248. }
  249. apply_downsampling=1;
  250. } else {
  251. /* Shortcut for the standard (non-custom modes) case */
  252. #ifdef FIXED_POINT
  253. if (accum)
  254. {
  255. for (j=0;j<N;j++)
  256. {
  257. celt_sig tmp = x[j] + m + VERY_SMALL;
  258. m = MULT16_32_Q15(coef0, tmp);
  259. y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(tmp))));
  260. }
  261. } else
  262. #endif
  263. {
  264. for (j=0;j<N;j++)
  265. {
  266. celt_sig tmp = x[j] + VERY_SMALL + m;
  267. m = MULT16_32_Q15(coef0, tmp);
  268. y[j*C] = SCALEOUT(SIG2WORD16(tmp));
  269. }
  270. }
  271. }
  272. mem[c] = m;
  273. if (apply_downsampling)
  274. {
  275. /* Perform down-sampling */
  276. #ifdef FIXED_POINT
  277. if (accum)
  278. {
  279. for (j=0;j<Nd;j++)
  280. y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(scratch[j*downsample]))));
  281. } else
  282. #endif
  283. {
  284. for (j=0;j<Nd;j++)
  285. y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample]));
  286. }
  287. }
  288. } while (++c<C);
  289. RESTORE_STACK;
  290. }
  291. #ifndef RESYNTH
  292. static
  293. #endif
  294. void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[],
  295. opus_val16 *oldBandE, int start, int effEnd, int C, int CC,
  296. int isTransient, int LM, int downsample,
  297. int silence, int arch)
  298. {
  299. int c, i;
  300. int M;
  301. int b;
  302. int B;
  303. int N, NB;
  304. int shift;
  305. int nbEBands;
  306. int overlap;
  307. VARDECL(celt_sig, freq);
  308. SAVE_STACK;
  309. overlap = mode->overlap;
  310. nbEBands = mode->nbEBands;
  311. N = mode->shortMdctSize<<LM;
  312. ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
  313. M = 1<<LM;
  314. if (isTransient)
  315. {
  316. B = M;
  317. NB = mode->shortMdctSize;
  318. shift = mode->maxLM;
  319. } else {
  320. B = 1;
  321. NB = mode->shortMdctSize<<LM;
  322. shift = mode->maxLM-LM;
  323. }
  324. if (CC==2&&C==1)
  325. {
  326. /* Copying a mono streams to two channels */
  327. celt_sig *freq2;
  328. denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
  329. downsample, silence);
  330. /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */
  331. freq2 = out_syn[1]+overlap/2;
  332. OPUS_COPY(freq2, freq, N);
  333. for (b=0;b<B;b++)
  334. clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
  335. for (b=0;b<B;b++)
  336. clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
  337. } else if (CC==1&&C==2)
  338. {
  339. /* Downmixing a stereo stream to mono */
  340. celt_sig *freq2;
  341. freq2 = out_syn[0]+overlap/2;
  342. denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
  343. downsample, silence);
  344. /* Use the output buffer as temp array before downmixing. */
  345. denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
  346. downsample, silence);
  347. for (i=0;i<N;i++)
  348. freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
  349. for (b=0;b<B;b++)
  350. clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
  351. } else {
  352. /* Normal case (mono or stereo) */
  353. c=0; do {
  354. denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
  355. downsample, silence);
  356. for (b=0;b<B;b++)
  357. clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
  358. } while (++c<CC);
  359. }
  360. /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
  361. or in the */
  362. c=0; do {
  363. for (i=0;i<N;i++)
  364. out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT);
  365. } while (++c<CC);
  366. RESTORE_STACK;
  367. }
  368. static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
  369. {
  370. int i, curr, tf_select;
  371. int tf_select_rsv;
  372. int tf_changed;
  373. int logp;
  374. opus_uint32 budget;
  375. opus_uint32 tell;
  376. budget = dec->storage*8;
  377. tell = ec_tell(dec);
  378. logp = isTransient ? 2 : 4;
  379. tf_select_rsv = LM>0 && tell+logp+1<=budget;
  380. budget -= tf_select_rsv;
  381. tf_changed = curr = 0;
  382. for (i=start;i<end;i++)
  383. {
  384. if (tell+logp<=budget)
  385. {
  386. curr ^= ec_dec_bit_logp(dec, logp);
  387. tell = ec_tell(dec);
  388. tf_changed |= curr;
  389. }
  390. tf_res[i] = curr;
  391. logp = isTransient ? 4 : 5;
  392. }
  393. tf_select = 0;
  394. if (tf_select_rsv &&
  395. tf_select_table[LM][4*isTransient+0+tf_changed] !=
  396. tf_select_table[LM][4*isTransient+2+tf_changed])
  397. {
  398. tf_select = ec_dec_bit_logp(dec, 1);
  399. }
  400. for (i=start;i<end;i++)
  401. {
  402. tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
  403. }
  404. }
  405. /* The maximum pitch lag to allow in the pitch-based PLC. It's possible to save
  406. CPU time in the PLC pitch search by making this smaller than MAX_PERIOD. The
  407. current value corresponds to a pitch of 66.67 Hz. */
  408. #define PLC_PITCH_LAG_MAX (720)
  409. /* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a
  410. pitch of 480 Hz. */
  411. #define PLC_PITCH_LAG_MIN (100)
  412. static int celt_plc_pitch_search(celt_sig *decode_mem[2], int C, int arch)
  413. {
  414. int pitch_index;
  415. VARDECL( opus_val16, lp_pitch_buf );
  416. SAVE_STACK;
  417. ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 );
  418. pitch_downsample(decode_mem, lp_pitch_buf,
  419. DECODE_BUFFER_SIZE, C, arch);
  420. pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf,
  421. DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX,
  422. PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, arch);
  423. pitch_index = PLC_PITCH_LAG_MAX-pitch_index;
  424. RESTORE_STACK;
  425. return pitch_index;
  426. }
  427. static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM)
  428. {
  429. int c;
  430. int i;
  431. const int C = st->channels;
  432. celt_sig *decode_mem[2];
  433. celt_sig *out_syn[2];
  434. opus_val16 *lpc;
  435. opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
  436. const OpusCustomMode *mode;
  437. int nbEBands;
  438. int overlap;
  439. int start;
  440. int loss_count;
  441. int noise_based;
  442. const opus_int16 *eBands;
  443. SAVE_STACK;
  444. mode = st->mode;
  445. nbEBands = mode->nbEBands;
  446. overlap = mode->overlap;
  447. eBands = mode->eBands;
  448. c=0; do {
  449. decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
  450. out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N;
  451. } while (++c<C);
  452. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*C);
  453. oldBandE = lpc+C*LPC_ORDER;
  454. oldLogE = oldBandE + 2*nbEBands;
  455. oldLogE2 = oldLogE + 2*nbEBands;
  456. backgroundLogE = oldLogE2 + 2*nbEBands;
  457. loss_count = st->loss_count;
  458. start = st->start;
  459. noise_based = loss_count >= 5 || start != 0 || st->skip_plc;
  460. if (noise_based)
  461. {
  462. /* Noise-based PLC/CNG */
  463. #ifdef NORM_ALIASING_HACK
  464. celt_norm *X;
  465. #else
  466. VARDECL(celt_norm, X);
  467. #endif
  468. opus_uint32 seed;
  469. int end;
  470. int effEnd;
  471. opus_val16 decay;
  472. end = st->end;
  473. effEnd = IMAX(start, IMIN(end, mode->effEBands));
  474. #ifdef NORM_ALIASING_HACK
  475. /* This is an ugly hack that breaks aliasing rules and would be easily broken,
  476. but it saves almost 4kB of stack. */
  477. X = (celt_norm*)(out_syn[C-1]+overlap/2);
  478. #else
  479. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  480. #endif
  481. /* Energy decay */
  482. decay = loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
  483. c=0; do
  484. {
  485. for (i=start;i<end;i++)
  486. oldBandE[c*nbEBands+i] = MAX16(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
  487. } while (++c<C);
  488. seed = st->rng;
  489. for (c=0;c<C;c++)
  490. {
  491. for (i=start;i<effEnd;i++)
  492. {
  493. int j;
  494. int boffs;
  495. int blen;
  496. boffs = N*c+(eBands[i]<<LM);
  497. blen = (eBands[i+1]-eBands[i])<<LM;
  498. for (j=0;j<blen;j++)
  499. {
  500. seed = celt_lcg_rand(seed);
  501. X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
  502. }
  503. renormalise_vector(X+boffs, blen, Q15ONE, st->arch);
  504. }
  505. }
  506. st->rng = seed;
  507. c=0; do {
  508. OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
  509. DECODE_BUFFER_SIZE-N+(overlap>>1));
  510. } while (++c<C);
  511. celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch);
  512. } else {
  513. /* Pitch-based PLC */
  514. const opus_val16 *window;
  515. opus_val16 *exc;
  516. opus_val16 fade = Q15ONE;
  517. int pitch_index;
  518. VARDECL(opus_val32, etmp);
  519. VARDECL(opus_val16, _exc);
  520. if (loss_count == 0)
  521. {
  522. st->last_pitch_index = pitch_index = celt_plc_pitch_search(decode_mem, C, st->arch);
  523. } else {
  524. pitch_index = st->last_pitch_index;
  525. fade = QCONST16(.8f,15);
  526. }
  527. ALLOC(etmp, overlap, opus_val32);
  528. ALLOC(_exc, MAX_PERIOD+LPC_ORDER, opus_val16);
  529. exc = _exc+LPC_ORDER;
  530. window = mode->window;
  531. c=0; do {
  532. opus_val16 decay;
  533. opus_val16 attenuation;
  534. opus_val32 S1=0;
  535. celt_sig *buf;
  536. int extrapolation_offset;
  537. int extrapolation_len;
  538. int exc_length;
  539. int j;
  540. buf = decode_mem[c];
  541. for (i=0;i<MAX_PERIOD;i++) {
  542. exc[i] = ROUND16(buf[DECODE_BUFFER_SIZE-MAX_PERIOD+i], SIG_SHIFT);
  543. }
  544. if (loss_count == 0)
  545. {
  546. opus_val32 ac[LPC_ORDER+1];
  547. /* Compute LPC coefficients for the last MAX_PERIOD samples before
  548. the first loss so we can work in the excitation-filter domain. */
  549. _celt_autocorr(exc, ac, window, overlap,
  550. LPC_ORDER, MAX_PERIOD, st->arch);
  551. /* Add a noise floor of -40 dB. */
  552. #ifdef FIXED_POINT
  553. ac[0] += SHR32(ac[0],13);
  554. #else
  555. ac[0] *= 1.0001f;
  556. #endif
  557. /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
  558. for (i=1;i<=LPC_ORDER;i++)
  559. {
  560. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  561. #ifdef FIXED_POINT
  562. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  563. #else
  564. ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
  565. #endif
  566. }
  567. _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
  568. #ifdef FIXED_POINT
  569. /* For fixed-point, apply bandwidth expansion until we can guarantee that
  570. no overflow can happen in the IIR filter. This means:
  571. 32768*sum(abs(filter)) < 2^31 */
  572. while (1) {
  573. opus_val16 tmp=Q15ONE;
  574. opus_val32 sum=QCONST16(1., SIG_SHIFT);
  575. for (i=0;i<LPC_ORDER;i++)
  576. sum += ABS16(lpc[c*LPC_ORDER+i]);
  577. if (sum < 65535) break;
  578. for (i=0;i<LPC_ORDER;i++)
  579. {
  580. tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
  581. lpc[c*LPC_ORDER+i] = MULT16_16_Q15(lpc[c*LPC_ORDER+i], tmp);
  582. }
  583. }
  584. #endif
  585. }
  586. /* We want the excitation for 2 pitch periods in order to look for a
  587. decaying signal, but we can't get more than MAX_PERIOD. */
  588. exc_length = IMIN(2*pitch_index, MAX_PERIOD);
  589. /* Initialize the LPC history with the samples just before the start
  590. of the region for which we're computing the excitation. */
  591. {
  592. for (i=0;i<LPC_ORDER;i++)
  593. {
  594. exc[MAX_PERIOD-exc_length-LPC_ORDER+i] =
  595. ROUND16(buf[DECODE_BUFFER_SIZE-exc_length-LPC_ORDER+i], SIG_SHIFT);
  596. }
  597. /* Compute the excitation for exc_length samples before the loss. */
  598. celt_fir(exc+MAX_PERIOD-exc_length, lpc+c*LPC_ORDER,
  599. exc+MAX_PERIOD-exc_length, exc_length, LPC_ORDER, st->arch);
  600. }
  601. /* Check if the waveform is decaying, and if so how fast.
  602. We do this to avoid adding energy when concealing in a segment
  603. with decaying energy. */
  604. {
  605. opus_val32 E1=1, E2=1;
  606. int decay_length;
  607. #ifdef FIXED_POINT
  608. int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[MAX_PERIOD-exc_length], exc_length))-20);
  609. #endif
  610. decay_length = exc_length>>1;
  611. for (i=0;i<decay_length;i++)
  612. {
  613. opus_val16 e;
  614. e = exc[MAX_PERIOD-decay_length+i];
  615. E1 += SHR32(MULT16_16(e, e), shift);
  616. e = exc[MAX_PERIOD-2*decay_length+i];
  617. E2 += SHR32(MULT16_16(e, e), shift);
  618. }
  619. E1 = MIN32(E1, E2);
  620. decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
  621. }
  622. /* Move the decoder memory one frame to the left to give us room to
  623. add the data for the new frame. We ignore the overlap that extends
  624. past the end of the buffer, because we aren't going to use it. */
  625. OPUS_MOVE(buf, buf+N, DECODE_BUFFER_SIZE-N);
  626. /* Extrapolate from the end of the excitation with a period of
  627. "pitch_index", scaling down each period by an additional factor of
  628. "decay". */
  629. extrapolation_offset = MAX_PERIOD-pitch_index;
  630. /* We need to extrapolate enough samples to cover a complete MDCT
  631. window (including overlap/2 samples on both sides). */
  632. extrapolation_len = N+overlap;
  633. /* We also apply fading if this is not the first loss. */
  634. attenuation = MULT16_16_Q15(fade, decay);
  635. for (i=j=0;i<extrapolation_len;i++,j++)
  636. {
  637. opus_val16 tmp;
  638. if (j >= pitch_index) {
  639. j -= pitch_index;
  640. attenuation = MULT16_16_Q15(attenuation, decay);
  641. }
  642. buf[DECODE_BUFFER_SIZE-N+i] =
  643. SHL32(EXTEND32(MULT16_16_Q15(attenuation,
  644. exc[extrapolation_offset+j])), SIG_SHIFT);
  645. /* Compute the energy of the previously decoded signal whose
  646. excitation we're copying. */
  647. tmp = ROUND16(
  648. buf[DECODE_BUFFER_SIZE-MAX_PERIOD-N+extrapolation_offset+j],
  649. SIG_SHIFT);
  650. S1 += SHR32(MULT16_16(tmp, tmp), 10);
  651. }
  652. {
  653. opus_val16 lpc_mem[LPC_ORDER];
  654. /* Copy the last decoded samples (prior to the overlap region) to
  655. synthesis filter memory so we can have a continuous signal. */
  656. for (i=0;i<LPC_ORDER;i++)
  657. lpc_mem[i] = ROUND16(buf[DECODE_BUFFER_SIZE-N-1-i], SIG_SHIFT);
  658. /* Apply the synthesis filter to convert the excitation back into
  659. the signal domain. */
  660. celt_iir(buf+DECODE_BUFFER_SIZE-N, lpc+c*LPC_ORDER,
  661. buf+DECODE_BUFFER_SIZE-N, extrapolation_len, LPC_ORDER,
  662. lpc_mem, st->arch);
  663. #ifdef FIXED_POINT
  664. for (i=0; i < extrapolation_len; i++)
  665. buf[DECODE_BUFFER_SIZE-N+i] = SATURATE(buf[DECODE_BUFFER_SIZE-N+i], SIG_SAT);
  666. #endif
  667. }
  668. /* Check if the synthesis energy is higher than expected, which can
  669. happen with the signal changes during our window. If so,
  670. attenuate. */
  671. {
  672. opus_val32 S2=0;
  673. for (i=0;i<extrapolation_len;i++)
  674. {
  675. opus_val16 tmp = ROUND16(buf[DECODE_BUFFER_SIZE-N+i], SIG_SHIFT);
  676. S2 += SHR32(MULT16_16(tmp, tmp), 10);
  677. }
  678. /* This checks for an "explosion" in the synthesis. */
  679. #ifdef FIXED_POINT
  680. if (!(S1 > SHR32(S2,2)))
  681. #else
  682. /* The float test is written this way to catch NaNs in the output
  683. of the IIR filter at the same time. */
  684. if (!(S1 > 0.2f*S2))
  685. #endif
  686. {
  687. for (i=0;i<extrapolation_len;i++)
  688. buf[DECODE_BUFFER_SIZE-N+i] = 0;
  689. } else if (S1 < S2)
  690. {
  691. opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
  692. for (i=0;i<overlap;i++)
  693. {
  694. opus_val16 tmp_g = Q15ONE
  695. - MULT16_16_Q15(window[i], Q15ONE-ratio);
  696. buf[DECODE_BUFFER_SIZE-N+i] =
  697. MULT16_32_Q15(tmp_g, buf[DECODE_BUFFER_SIZE-N+i]);
  698. }
  699. for (i=overlap;i<extrapolation_len;i++)
  700. {
  701. buf[DECODE_BUFFER_SIZE-N+i] =
  702. MULT16_32_Q15(ratio, buf[DECODE_BUFFER_SIZE-N+i]);
  703. }
  704. }
  705. }
  706. /* Apply the pre-filter to the MDCT overlap for the next frame because
  707. the post-filter will be re-applied in the decoder after the MDCT
  708. overlap. */
  709. comb_filter(etmp, buf+DECODE_BUFFER_SIZE,
  710. st->postfilter_period, st->postfilter_period, overlap,
  711. -st->postfilter_gain, -st->postfilter_gain,
  712. st->postfilter_tapset, st->postfilter_tapset, NULL, 0, st->arch);
  713. /* Simulate TDAC on the concealed audio so that it blends with the
  714. MDCT of the next frame. */
  715. for (i=0;i<overlap/2;i++)
  716. {
  717. buf[DECODE_BUFFER_SIZE+i] =
  718. MULT16_32_Q15(window[i], etmp[overlap-1-i])
  719. + MULT16_32_Q15(window[overlap-i-1], etmp[i]);
  720. }
  721. } while (++c<C);
  722. }
  723. st->loss_count = loss_count+1;
  724. RESTORE_STACK;
  725. }
  726. int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data,
  727. int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum)
  728. {
  729. int c, i, N;
  730. int spread_decision;
  731. opus_int32 bits;
  732. ec_dec _dec;
  733. #ifdef NORM_ALIASING_HACK
  734. celt_norm *X;
  735. #else
  736. VARDECL(celt_norm, X);
  737. #endif
  738. VARDECL(int, fine_quant);
  739. VARDECL(int, pulses);
  740. VARDECL(int, cap);
  741. VARDECL(int, offsets);
  742. VARDECL(int, fine_priority);
  743. VARDECL(int, tf_res);
  744. VARDECL(unsigned char, collapse_masks);
  745. celt_sig *decode_mem[2];
  746. celt_sig *out_syn[2];
  747. opus_val16 *lpc;
  748. opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
  749. int shortBlocks;
  750. int isTransient;
  751. int intra_ener;
  752. const int CC = st->channels;
  753. int LM, M;
  754. int start;
  755. int end;
  756. int effEnd;
  757. int codedBands;
  758. int alloc_trim;
  759. int postfilter_pitch;
  760. opus_val16 postfilter_gain;
  761. int intensity=0;
  762. int dual_stereo=0;
  763. opus_int32 total_bits;
  764. opus_int32 balance;
  765. opus_int32 tell;
  766. int dynalloc_logp;
  767. int postfilter_tapset;
  768. int anti_collapse_rsv;
  769. int anti_collapse_on=0;
  770. int silence;
  771. int C = st->stream_channels;
  772. const OpusCustomMode *mode;
  773. int nbEBands;
  774. int overlap;
  775. const opus_int16 *eBands;
  776. ALLOC_STACK;
  777. mode = st->mode;
  778. nbEBands = mode->nbEBands;
  779. overlap = mode->overlap;
  780. eBands = mode->eBands;
  781. start = st->start;
  782. end = st->end;
  783. frame_size *= st->downsample;
  784. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC);
  785. oldBandE = lpc+CC*LPC_ORDER;
  786. oldLogE = oldBandE + 2*nbEBands;
  787. oldLogE2 = oldLogE + 2*nbEBands;
  788. backgroundLogE = oldLogE2 + 2*nbEBands;
  789. #ifdef CUSTOM_MODES
  790. if (st->signalling && data!=NULL)
  791. {
  792. int data0=data[0];
  793. /* Convert "standard mode" to Opus header */
  794. if (mode->Fs==48000 && mode->shortMdctSize==120)
  795. {
  796. data0 = fromOpus(data0);
  797. if (data0<0)
  798. return OPUS_INVALID_PACKET;
  799. }
  800. st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
  801. LM = (data0>>3)&0x3;
  802. C = 1 + ((data0>>2)&0x1);
  803. data++;
  804. len--;
  805. if (LM>mode->maxLM)
  806. return OPUS_INVALID_PACKET;
  807. if (frame_size < mode->shortMdctSize<<LM)
  808. return OPUS_BUFFER_TOO_SMALL;
  809. else
  810. frame_size = mode->shortMdctSize<<LM;
  811. } else {
  812. #else
  813. {
  814. #endif
  815. for (LM=0;LM<=mode->maxLM;LM++)
  816. if (mode->shortMdctSize<<LM==frame_size)
  817. break;
  818. if (LM>mode->maxLM)
  819. return OPUS_BAD_ARG;
  820. }
  821. M=1<<LM;
  822. if (len<0 || len>1275 || pcm==NULL)
  823. return OPUS_BAD_ARG;
  824. N = M*mode->shortMdctSize;
  825. c=0; do {
  826. decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
  827. out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N;
  828. } while (++c<CC);
  829. effEnd = end;
  830. if (effEnd > mode->effEBands)
  831. effEnd = mode->effEBands;
  832. if (data == NULL || len<=1)
  833. {
  834. celt_decode_lost(st, N, LM);
  835. deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
  836. RESTORE_STACK;
  837. return frame_size/st->downsample;
  838. }
  839. /* Check if there are at least two packets received consecutively before
  840. * turning on the pitch-based PLC */
  841. st->skip_plc = st->loss_count != 0;
  842. if (dec == NULL)
  843. {
  844. ec_dec_init(&_dec,(unsigned char*)data,len);
  845. dec = &_dec;
  846. }
  847. if (C==1)
  848. {
  849. for (i=0;i<nbEBands;i++)
  850. oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]);
  851. }
  852. total_bits = len*8;
  853. tell = ec_tell(dec);
  854. if (tell >= total_bits)
  855. silence = 1;
  856. else if (tell==1)
  857. silence = ec_dec_bit_logp(dec, 15);
  858. else
  859. silence = 0;
  860. if (silence)
  861. {
  862. /* Pretend we've read all the remaining bits */
  863. tell = len*8;
  864. dec->nbits_total+=tell-ec_tell(dec);
  865. }
  866. postfilter_gain = 0;
  867. postfilter_pitch = 0;
  868. postfilter_tapset = 0;
  869. if (start==0 && tell+16 <= total_bits)
  870. {
  871. if(ec_dec_bit_logp(dec, 1))
  872. {
  873. int qg, octave;
  874. octave = ec_dec_uint(dec, 6);
  875. postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
  876. qg = ec_dec_bits(dec, 3);
  877. if (ec_tell(dec)+2<=total_bits)
  878. postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
  879. postfilter_gain = QCONST16(.09375f,15)*(qg+1);
  880. }
  881. tell = ec_tell(dec);
  882. }
  883. if (LM > 0 && tell+3 <= total_bits)
  884. {
  885. isTransient = ec_dec_bit_logp(dec, 3);
  886. tell = ec_tell(dec);
  887. }
  888. else
  889. isTransient = 0;
  890. if (isTransient)
  891. shortBlocks = M;
  892. else
  893. shortBlocks = 0;
  894. /* Decode the global flags (first symbols in the stream) */
  895. intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
  896. /* Get band energies */
  897. unquant_coarse_energy(mode, start, end, oldBandE,
  898. intra_ener, dec, C, LM);
  899. ALLOC(tf_res, nbEBands, int);
  900. tf_decode(start, end, isTransient, tf_res, LM, dec);
  901. tell = ec_tell(dec);
  902. spread_decision = SPREAD_NORMAL;
  903. if (tell+4 <= total_bits)
  904. spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
  905. ALLOC(cap, nbEBands, int);
  906. init_caps(mode,cap,LM,C);
  907. ALLOC(offsets, nbEBands, int);
  908. dynalloc_logp = 6;
  909. total_bits<<=BITRES;
  910. tell = ec_tell_frac(dec);
  911. for (i=start;i<end;i++)
  912. {
  913. int width, quanta;
  914. int dynalloc_loop_logp;
  915. int boost;
  916. width = C*(eBands[i+1]-eBands[i])<<LM;
  917. /* quanta is 6 bits, but no more than 1 bit/sample
  918. and no less than 1/8 bit/sample */
  919. quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
  920. dynalloc_loop_logp = dynalloc_logp;
  921. boost = 0;
  922. while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
  923. {
  924. int flag;
  925. flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
  926. tell = ec_tell_frac(dec);
  927. if (!flag)
  928. break;
  929. boost += quanta;
  930. total_bits -= quanta;
  931. dynalloc_loop_logp = 1;
  932. }
  933. offsets[i] = boost;
  934. /* Making dynalloc more likely */
  935. if (boost>0)
  936. dynalloc_logp = IMAX(2, dynalloc_logp-1);
  937. }
  938. ALLOC(fine_quant, nbEBands, int);
  939. alloc_trim = tell+(6<<BITRES) <= total_bits ?
  940. ec_dec_icdf(dec, trim_icdf, 7) : 5;
  941. bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
  942. anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
  943. bits -= anti_collapse_rsv;
  944. ALLOC(pulses, nbEBands, int);
  945. ALLOC(fine_priority, nbEBands, int);
  946. codedBands = compute_allocation(mode, start, end, offsets, cap,
  947. alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
  948. fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
  949. unquant_fine_energy(mode, start, end, oldBandE, fine_quant, dec, C);
  950. c=0; do {
  951. OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap/2);
  952. } while (++c<CC);
  953. /* Decode fixed codebook */
  954. ALLOC(collapse_masks, C*nbEBands, unsigned char);
  955. #ifdef NORM_ALIASING_HACK
  956. /* This is an ugly hack that breaks aliasing rules and would be easily broken,
  957. but it saves almost 4kB of stack. */
  958. X = (celt_norm*)(out_syn[CC-1]+overlap/2);
  959. #else
  960. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  961. #endif
  962. quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
  963. NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
  964. len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
  965. st->arch, st->disable_inv);
  966. if (anti_collapse_rsv > 0)
  967. {
  968. anti_collapse_on = ec_dec_bits(dec, 1);
  969. }
  970. unquant_energy_finalise(mode, start, end, oldBandE,
  971. fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
  972. if (anti_collapse_on)
  973. anti_collapse(mode, X, collapse_masks, LM, C, N,
  974. start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, st->arch);
  975. if (silence)
  976. {
  977. for (i=0;i<C*nbEBands;i++)
  978. oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
  979. }
  980. celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
  981. C, CC, isTransient, LM, st->downsample, silence, st->arch);
  982. c=0; do {
  983. st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
  984. st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
  985. comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
  986. st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
  987. mode->window, overlap, st->arch);
  988. if (LM!=0)
  989. comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
  990. st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
  991. mode->window, overlap, st->arch);
  992. } while (++c<CC);
  993. st->postfilter_period_old = st->postfilter_period;
  994. st->postfilter_gain_old = st->postfilter_gain;
  995. st->postfilter_tapset_old = st->postfilter_tapset;
  996. st->postfilter_period = postfilter_pitch;
  997. st->postfilter_gain = postfilter_gain;
  998. st->postfilter_tapset = postfilter_tapset;
  999. if (LM!=0)
  1000. {
  1001. st->postfilter_period_old = st->postfilter_period;
  1002. st->postfilter_gain_old = st->postfilter_gain;
  1003. st->postfilter_tapset_old = st->postfilter_tapset;
  1004. }
  1005. if (C==1)
  1006. OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
  1007. /* In case start or end were to change */
  1008. if (!isTransient)
  1009. {
  1010. opus_val16 max_background_increase;
  1011. OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
  1012. OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
  1013. /* In normal circumstances, we only allow the noise floor to increase by
  1014. up to 2.4 dB/second, but when we're in DTX, we allow up to 6 dB
  1015. increase for each update.*/
  1016. if (st->loss_count < 10)
  1017. max_background_increase = M*QCONST16(0.001f,DB_SHIFT);
  1018. else
  1019. max_background_increase = QCONST16(1.f,DB_SHIFT);
  1020. for (i=0;i<2*nbEBands;i++)
  1021. backgroundLogE[i] = MIN16(backgroundLogE[i] + max_background_increase, oldBandE[i]);
  1022. } else {
  1023. for (i=0;i<2*nbEBands;i++)
  1024. oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
  1025. }
  1026. c=0; do
  1027. {
  1028. for (i=0;i<start;i++)
  1029. {
  1030. oldBandE[c*nbEBands+i]=0;
  1031. oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
  1032. }
  1033. for (i=end;i<nbEBands;i++)
  1034. {
  1035. oldBandE[c*nbEBands+i]=0;
  1036. oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
  1037. }
  1038. } while (++c<2);
  1039. st->rng = dec->rng;
  1040. deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
  1041. st->loss_count = 0;
  1042. RESTORE_STACK;
  1043. if (ec_tell(dec) > 8*len)
  1044. return OPUS_INTERNAL_ERROR;
  1045. if(ec_get_error(dec))
  1046. st->error = 1;
  1047. return frame_size/st->downsample;
  1048. }
  1049. #ifdef CUSTOM_MODES
  1050. #ifdef FIXED_POINT
  1051. int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
  1052. {
  1053. return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
  1054. }
  1055. #ifndef DISABLE_FLOAT_API
  1056. int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
  1057. {
  1058. int j, ret, C, N;
  1059. VARDECL(opus_int16, out);
  1060. ALLOC_STACK;
  1061. if (pcm==NULL)
  1062. return OPUS_BAD_ARG;
  1063. C = st->channels;
  1064. N = frame_size;
  1065. ALLOC(out, C*N, opus_int16);
  1066. ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
  1067. if (ret>0)
  1068. for (j=0;j<C*ret;j++)
  1069. pcm[j]=out[j]*(1.f/32768.f);
  1070. RESTORE_STACK;
  1071. return ret;
  1072. }
  1073. #endif /* DISABLE_FLOAT_API */
  1074. #else
  1075. int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
  1076. {
  1077. return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
  1078. }
  1079. int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
  1080. {
  1081. int j, ret, C, N;
  1082. VARDECL(celt_sig, out);
  1083. ALLOC_STACK;
  1084. if (pcm==NULL)
  1085. return OPUS_BAD_ARG;
  1086. C = st->channels;
  1087. N = frame_size;
  1088. ALLOC(out, C*N, celt_sig);
  1089. ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
  1090. if (ret>0)
  1091. for (j=0;j<C*ret;j++)
  1092. pcm[j] = FLOAT2INT16 (out[j]);
  1093. RESTORE_STACK;
  1094. return ret;
  1095. }
  1096. #endif
  1097. #endif /* CUSTOM_MODES */
  1098. int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
  1099. {
  1100. va_list ap;
  1101. va_start(ap, request);
  1102. switch (request)
  1103. {
  1104. case CELT_SET_START_BAND_REQUEST:
  1105. {
  1106. opus_int32 value = va_arg(ap, opus_int32);
  1107. if (value<0 || value>=st->mode->nbEBands)
  1108. goto bad_arg;
  1109. st->start = value;
  1110. }
  1111. break;
  1112. case CELT_SET_END_BAND_REQUEST:
  1113. {
  1114. opus_int32 value = va_arg(ap, opus_int32);
  1115. if (value<1 || value>st->mode->nbEBands)
  1116. goto bad_arg;
  1117. st->end = value;
  1118. }
  1119. break;
  1120. case CELT_SET_CHANNELS_REQUEST:
  1121. {
  1122. opus_int32 value = va_arg(ap, opus_int32);
  1123. if (value<1 || value>2)
  1124. goto bad_arg;
  1125. st->stream_channels = value;
  1126. }
  1127. break;
  1128. case CELT_GET_AND_CLEAR_ERROR_REQUEST:
  1129. {
  1130. opus_int32 *value = va_arg(ap, opus_int32*);
  1131. if (value==NULL)
  1132. goto bad_arg;
  1133. *value=st->error;
  1134. st->error = 0;
  1135. }
  1136. break;
  1137. case OPUS_GET_LOOKAHEAD_REQUEST:
  1138. {
  1139. opus_int32 *value = va_arg(ap, opus_int32*);
  1140. if (value==NULL)
  1141. goto bad_arg;
  1142. *value = st->overlap/st->downsample;
  1143. }
  1144. break;
  1145. case OPUS_RESET_STATE:
  1146. {
  1147. int i;
  1148. opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
  1149. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
  1150. oldBandE = lpc+st->channels*LPC_ORDER;
  1151. oldLogE = oldBandE + 2*st->mode->nbEBands;
  1152. oldLogE2 = oldLogE + 2*st->mode->nbEBands;
  1153. OPUS_CLEAR((char*)&st->DECODER_RESET_START,
  1154. opus_custom_decoder_get_size(st->mode, st->channels)-
  1155. ((char*)&st->DECODER_RESET_START - (char*)st));
  1156. for (i=0;i<2*st->mode->nbEBands;i++)
  1157. oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
  1158. st->skip_plc = 1;
  1159. }
  1160. break;
  1161. case OPUS_GET_PITCH_REQUEST:
  1162. {
  1163. opus_int32 *value = va_arg(ap, opus_int32*);
  1164. if (value==NULL)
  1165. goto bad_arg;
  1166. *value = st->postfilter_period;
  1167. }
  1168. break;
  1169. case CELT_GET_MODE_REQUEST:
  1170. {
  1171. const CELTMode ** value = va_arg(ap, const CELTMode**);
  1172. if (value==0)
  1173. goto bad_arg;
  1174. *value=st->mode;
  1175. }
  1176. break;
  1177. case CELT_SET_SIGNALLING_REQUEST:
  1178. {
  1179. opus_int32 value = va_arg(ap, opus_int32);
  1180. st->signalling = value;
  1181. }
  1182. break;
  1183. case OPUS_GET_FINAL_RANGE_REQUEST:
  1184. {
  1185. opus_uint32 * value = va_arg(ap, opus_uint32 *);
  1186. if (value==0)
  1187. goto bad_arg;
  1188. *value=st->rng;
  1189. }
  1190. break;
  1191. case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
  1192. {
  1193. opus_int32 value = va_arg(ap, opus_int32);
  1194. if(value<0 || value>1)
  1195. {
  1196. goto bad_arg;
  1197. }
  1198. st->disable_inv = value;
  1199. }
  1200. break;
  1201. case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
  1202. {
  1203. opus_int32 *value = va_arg(ap, opus_int32*);
  1204. if (!value)
  1205. {
  1206. goto bad_arg;
  1207. }
  1208. *value = st->disable_inv;
  1209. }
  1210. break;
  1211. default:
  1212. goto bad_request;
  1213. }
  1214. va_end(ap);
  1215. return OPUS_OK;
  1216. bad_arg:
  1217. va_end(ap);
  1218. return OPUS_BAD_ARG;
  1219. bad_request:
  1220. va_end(ap);
  1221. return OPUS_UNIMPLEMENTED;
  1222. }