celt_decoder.c 42 KB

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