celt_decoder.c 42 KB

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