celt_decoder.c 37 KB

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