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