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