celt_decoder.c 37 KB

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