blob: b688f2a4e3fb089033182e762926590b8831b1b5 [file] [log] [blame]
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001/* 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
flimc91ee5b2016-01-26 14:33:44 +010054#if defined(SMALL_FOOTPRINT) && defined(FIXED_POINT)
55#define NORM_ALIASING_HACK
56#endif
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -080057/**********************************************************************/
58/* */
59/* DECODER */
60/* */
61/**********************************************************************/
62#define DECODE_BUFFER_SIZE 2048
63
64/** Decoder state
65 @brief Decoder state
66 */
67struct 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 arch;
77
78 /* Everything beyond this point gets cleared on a reset */
79#define DECODER_RESET_START rng
80
81 opus_uint32 rng;
82 int error;
83 int last_pitch_index;
84 int loss_count;
85 int postfilter_period;
86 int postfilter_period_old;
87 opus_val16 postfilter_gain;
88 opus_val16 postfilter_gain_old;
89 int postfilter_tapset;
90 int postfilter_tapset_old;
91
92 celt_sig preemph_memD[2];
93
94 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
95 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
96 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
97 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
98 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
99 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
100};
101
102int celt_decoder_get_size(int channels)
103{
104 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
105 return opus_custom_decoder_get_size(mode, channels);
106}
107
108OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
109{
110 int size = sizeof(struct CELTDecoder)
111 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
112 + channels*LPC_ORDER*sizeof(opus_val16)
113 + 4*2*mode->nbEBands*sizeof(opus_val16);
114 return size;
115}
116
117#ifdef CUSTOM_MODES
118CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
119{
120 int ret;
121 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
122 ret = opus_custom_decoder_init(st, mode, channels);
123 if (ret != OPUS_OK)
124 {
125 opus_custom_decoder_destroy(st);
126 st = NULL;
127 }
128 if (error)
129 *error = ret;
130 return st;
131}
132#endif /* CUSTOM_MODES */
133
134int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
135{
136 int ret;
137 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
138 if (ret != OPUS_OK)
139 return ret;
140 st->downsample = resampling_factor(sampling_rate);
141 if (st->downsample==0)
142 return OPUS_BAD_ARG;
143 else
144 return OPUS_OK;
145}
146
147OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
148{
149 if (channels < 0 || channels > 2)
150 return OPUS_BAD_ARG;
151
152 if (st==NULL)
153 return OPUS_ALLOC_FAIL;
154
155 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
156
157 st->mode = mode;
158 st->overlap = mode->overlap;
159 st->stream_channels = st->channels = channels;
160
161 st->downsample = 1;
162 st->start = 0;
163 st->end = st->mode->effEBands;
164 st->signalling = 1;
165 st->arch = opus_select_arch();
166
167 st->loss_count = 0;
168
169 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
170
171 return OPUS_OK;
172}
173
174#ifdef CUSTOM_MODES
175void opus_custom_decoder_destroy(CELTDecoder *st)
176{
177 opus_free(st);
178}
179#endif /* CUSTOM_MODES */
180
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800181
182#ifndef RESYNTH
183static
184#endif
flimc91ee5b2016-01-26 14:33:44 +0100185void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef,
186 celt_sig *mem, int accum)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800187{
188 int c;
189 int Nd;
190 int apply_downsampling=0;
191 opus_val16 coef0;
flimc91ee5b2016-01-26 14:33:44 +0100192 VARDECL(celt_sig, scratch);
193 SAVE_STACK;
194#ifndef FIXED_POINT
195 (void)accum;
196 celt_assert(accum==0);
197#endif
198 ALLOC(scratch, N, celt_sig);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800199 coef0 = coef[0];
200 Nd = N/downsample;
201 c=0; do {
202 int j;
203 celt_sig * OPUS_RESTRICT x;
204 opus_val16 * OPUS_RESTRICT y;
205 celt_sig m = mem[c];
206 x =in[c];
207 y = pcm+c;
208#ifdef CUSTOM_MODES
209 if (coef[1] != 0)
210 {
211 opus_val16 coef1 = coef[1];
212 opus_val16 coef3 = coef[3];
213 for (j=0;j<N;j++)
214 {
215 celt_sig tmp = x[j] + m + VERY_SMALL;
216 m = MULT16_32_Q15(coef0, tmp)
217 - MULT16_32_Q15(coef1, x[j]);
218 tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
219 scratch[j] = tmp;
220 }
221 apply_downsampling=1;
222 } else
223#endif
224 if (downsample>1)
225 {
226 /* Shortcut for the standard (non-custom modes) case */
227 for (j=0;j<N;j++)
228 {
229 celt_sig tmp = x[j] + m + VERY_SMALL;
230 m = MULT16_32_Q15(coef0, tmp);
231 scratch[j] = tmp;
232 }
233 apply_downsampling=1;
234 } else {
235 /* Shortcut for the standard (non-custom modes) case */
flimc91ee5b2016-01-26 14:33:44 +0100236#ifdef FIXED_POINT
237 if (accum)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800238 {
flimc91ee5b2016-01-26 14:33:44 +0100239 for (j=0;j<N;j++)
240 {
241 celt_sig tmp = x[j] + m + VERY_SMALL;
242 m = MULT16_32_Q15(coef0, tmp);
243 y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(tmp))));
244 }
245 } else
246#endif
247 {
248 for (j=0;j<N;j++)
249 {
250 celt_sig tmp = x[j] + m + VERY_SMALL;
251 m = MULT16_32_Q15(coef0, tmp);
252 y[j*C] = SCALEOUT(SIG2WORD16(tmp));
253 }
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800254 }
255 }
256 mem[c] = m;
257
258 if (apply_downsampling)
259 {
260 /* Perform down-sampling */
flimc91ee5b2016-01-26 14:33:44 +0100261#ifdef FIXED_POINT
262 if (accum)
263 {
264 for (j=0;j<Nd;j++)
265 y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(scratch[j*downsample]))));
266 } else
267#endif
268 {
269 for (j=0;j<Nd;j++)
270 y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample]));
271 }
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800272 }
273 } while (++c<C);
flimc91ee5b2016-01-26 14:33:44 +0100274 RESTORE_STACK;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800275}
276
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800277#ifndef RESYNTH
278static
279#endif
flimc91ee5b2016-01-26 14:33:44 +0100280void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[],
281 opus_val16 *oldBandE, int start, int effEnd, int C, int CC,
282 int isTransient, int LM, int downsample,
283 int silence, int arch)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800284{
flimc91ee5b2016-01-26 14:33:44 +0100285 int c, i;
286 int M;
287 int b;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800288 int B;
flimc91ee5b2016-01-26 14:33:44 +0100289 int N, NB;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800290 int shift;
flimc91ee5b2016-01-26 14:33:44 +0100291 int nbEBands;
292 int overlap;
293 VARDECL(celt_sig, freq);
294 SAVE_STACK;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800295
flimc91ee5b2016-01-26 14:33:44 +0100296 overlap = mode->overlap;
297 nbEBands = mode->nbEBands;
298 N = mode->shortMdctSize<<LM;
299 ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
300 M = 1<<LM;
301
302 if (isTransient)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800303 {
flimc91ee5b2016-01-26 14:33:44 +0100304 B = M;
305 NB = mode->shortMdctSize;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800306 shift = mode->maxLM;
307 } else {
308 B = 1;
flimc91ee5b2016-01-26 14:33:44 +0100309 NB = mode->shortMdctSize<<LM;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800310 shift = mode->maxLM-LM;
311 }
flimc91ee5b2016-01-26 14:33:44 +0100312
313 if (CC==2&&C==1)
314 {
315 /* Copying a mono streams to two channels */
316 celt_sig *freq2;
317 denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
318 downsample, silence);
319 /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */
320 freq2 = out_syn[1]+overlap/2;
321 OPUS_COPY(freq2, freq, N);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800322 for (b=0;b<B;b++)
flimc91ee5b2016-01-26 14:33:44 +0100323 clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
324 for (b=0;b<B;b++)
325 clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
326 } else if (CC==1&&C==2)
327 {
328 /* Downmixing a stereo stream to mono */
329 celt_sig *freq2;
330 freq2 = out_syn[0]+overlap/2;
331 denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
332 downsample, silence);
333 /* Use the output buffer as temp array before downmixing. */
334 denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
335 downsample, silence);
336 for (i=0;i<N;i++)
337 freq[i] = HALF32(ADD32(freq[i],freq2[i]));
338 for (b=0;b<B;b++)
339 clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
340 } else {
341 /* Normal case (mono or stereo) */
342 c=0; do {
343 denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
344 downsample, silence);
345 for (b=0;b<B;b++)
346 clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
347 } while (++c<CC);
348 }
349 RESTORE_STACK;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800350}
351
352static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
353{
354 int i, curr, tf_select;
355 int tf_select_rsv;
356 int tf_changed;
357 int logp;
358 opus_uint32 budget;
359 opus_uint32 tell;
360
361 budget = dec->storage*8;
362 tell = ec_tell(dec);
363 logp = isTransient ? 2 : 4;
364 tf_select_rsv = LM>0 && tell+logp+1<=budget;
365 budget -= tf_select_rsv;
366 tf_changed = curr = 0;
367 for (i=start;i<end;i++)
368 {
369 if (tell+logp<=budget)
370 {
371 curr ^= ec_dec_bit_logp(dec, logp);
372 tell = ec_tell(dec);
373 tf_changed |= curr;
374 }
375 tf_res[i] = curr;
376 logp = isTransient ? 4 : 5;
377 }
378 tf_select = 0;
379 if (tf_select_rsv &&
380 tf_select_table[LM][4*isTransient+0+tf_changed] !=
381 tf_select_table[LM][4*isTransient+2+tf_changed])
382 {
383 tf_select = ec_dec_bit_logp(dec, 1);
384 }
385 for (i=start;i<end;i++)
386 {
387 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
388 }
389}
390
391/* The maximum pitch lag to allow in the pitch-based PLC. It's possible to save
392 CPU time in the PLC pitch search by making this smaller than MAX_PERIOD. The
393 current value corresponds to a pitch of 66.67 Hz. */
394#define PLC_PITCH_LAG_MAX (720)
395/* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a
396 pitch of 480 Hz. */
397#define PLC_PITCH_LAG_MIN (100)
398
flimc91ee5b2016-01-26 14:33:44 +0100399static int celt_plc_pitch_search(celt_sig *decode_mem[2], int C, int arch)
400{
401 int pitch_index;
402 VARDECL( opus_val16, lp_pitch_buf );
403 SAVE_STACK;
404 ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 );
405 pitch_downsample(decode_mem, lp_pitch_buf,
406 DECODE_BUFFER_SIZE, C, arch);
407 pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf,
408 DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX,
409 PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, arch);
410 pitch_index = PLC_PITCH_LAG_MAX-pitch_index;
411 RESTORE_STACK;
412 return pitch_index;
413}
414
415static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800416{
417 int c;
418 int i;
419 const int C = st->channels;
420 celt_sig *decode_mem[2];
421 celt_sig *out_syn[2];
422 opus_val16 *lpc;
423 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
424 const OpusCustomMode *mode;
425 int nbEBands;
426 int overlap;
427 int start;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800428 int loss_count;
429 int noise_based;
430 const opus_int16 *eBands;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800431 SAVE_STACK;
432
433 mode = st->mode;
434 nbEBands = mode->nbEBands;
435 overlap = mode->overlap;
436 eBands = mode->eBands;
437
438 c=0; do {
439 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
440 out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N;
441 } while (++c<C);
442 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*C);
443 oldBandE = lpc+C*LPC_ORDER;
444 oldLogE = oldBandE + 2*nbEBands;
445 oldLogE2 = oldLogE + 2*nbEBands;
446 backgroundLogE = oldLogE2 + 2*nbEBands;
447
448 loss_count = st->loss_count;
449 start = st->start;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800450 noise_based = loss_count >= 5 || start != 0;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800451 if (noise_based)
452 {
453 /* Noise-based PLC/CNG */
flimc91ee5b2016-01-26 14:33:44 +0100454#ifdef NORM_ALIASING_HACK
455 celt_norm *X;
456#else
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800457 VARDECL(celt_norm, X);
flimc91ee5b2016-01-26 14:33:44 +0100458#endif
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800459 opus_uint32 seed;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800460 int end;
461 int effEnd;
flimc91ee5b2016-01-26 14:33:44 +0100462 opus_val16 decay;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800463 end = st->end;
464 effEnd = IMAX(start, IMIN(end, mode->effEBands));
465
flimc91ee5b2016-01-26 14:33:44 +0100466#ifdef NORM_ALIASING_HACK
467 /* This is an ugly hack that breaks aliasing rules and would be easily broken,
468 but it saves almost 4kB of stack. */
469 X = (celt_norm*)(out_syn[C-1]+overlap/2);
470#else
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800471 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
flimc91ee5b2016-01-26 14:33:44 +0100472#endif
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800473
flimc91ee5b2016-01-26 14:33:44 +0100474 /* Energy decay */
475 decay = loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
476 c=0; do
477 {
478 for (i=start;i<end;i++)
479 oldBandE[c*nbEBands+i] = MAX16(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
480 } while (++c<C);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800481 seed = st->rng;
482 for (c=0;c<C;c++)
483 {
484 for (i=start;i<effEnd;i++)
485 {
486 int j;
487 int boffs;
488 int blen;
489 boffs = N*c+(eBands[i]<<LM);
490 blen = (eBands[i+1]-eBands[i])<<LM;
491 for (j=0;j<blen;j++)
492 {
493 seed = celt_lcg_rand(seed);
494 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
495 }
flimc91ee5b2016-01-26 14:33:44 +0100496 renormalise_vector(X+boffs, blen, Q15ONE, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800497 }
498 }
499 st->rng = seed;
500
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800501 c=0; do {
502 OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
503 DECODE_BUFFER_SIZE-N+(overlap>>1));
504 } while (++c<C);
flimc91ee5b2016-01-26 14:33:44 +0100505
506 celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800507 } else {
508 /* Pitch-based PLC */
509 const opus_val16 *window;
510 opus_val16 fade = Q15ONE;
511 int pitch_index;
512 VARDECL(opus_val32, etmp);
513 VARDECL(opus_val16, exc);
514
515 if (loss_count == 0)
516 {
flimc91ee5b2016-01-26 14:33:44 +0100517 st->last_pitch_index = pitch_index = celt_plc_pitch_search(decode_mem, C, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800518 } else {
519 pitch_index = st->last_pitch_index;
520 fade = QCONST16(.8f,15);
521 }
522
523 ALLOC(etmp, overlap, opus_val32);
524 ALLOC(exc, MAX_PERIOD, opus_val16);
525 window = mode->window;
526 c=0; do {
527 opus_val16 decay;
528 opus_val16 attenuation;
529 opus_val32 S1=0;
530 celt_sig *buf;
531 int extrapolation_offset;
532 int extrapolation_len;
533 int exc_length;
534 int j;
535
536 buf = decode_mem[c];
537 for (i=0;i<MAX_PERIOD;i++) {
538 exc[i] = ROUND16(buf[DECODE_BUFFER_SIZE-MAX_PERIOD+i], SIG_SHIFT);
539 }
540
541 if (loss_count == 0)
542 {
543 opus_val32 ac[LPC_ORDER+1];
544 /* Compute LPC coefficients for the last MAX_PERIOD samples before
545 the first loss so we can work in the excitation-filter domain. */
546 _celt_autocorr(exc, ac, window, overlap,
547 LPC_ORDER, MAX_PERIOD, st->arch);
548 /* Add a noise floor of -40 dB. */
549#ifdef FIXED_POINT
550 ac[0] += SHR32(ac[0],13);
551#else
552 ac[0] *= 1.0001f;
553#endif
554 /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
555 for (i=1;i<=LPC_ORDER;i++)
556 {
557 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
558#ifdef FIXED_POINT
559 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
560#else
561 ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
562#endif
563 }
564 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
565 }
566 /* We want the excitation for 2 pitch periods in order to look for a
567 decaying signal, but we can't get more than MAX_PERIOD. */
568 exc_length = IMIN(2*pitch_index, MAX_PERIOD);
569 /* Initialize the LPC history with the samples just before the start
570 of the region for which we're computing the excitation. */
571 {
572 opus_val16 lpc_mem[LPC_ORDER];
573 for (i=0;i<LPC_ORDER;i++)
574 {
575 lpc_mem[i] =
576 ROUND16(buf[DECODE_BUFFER_SIZE-exc_length-1-i], SIG_SHIFT);
577 }
578 /* Compute the excitation for exc_length samples before the loss. */
579 celt_fir(exc+MAX_PERIOD-exc_length, lpc+c*LPC_ORDER,
flimc91ee5b2016-01-26 14:33:44 +0100580 exc+MAX_PERIOD-exc_length, exc_length, LPC_ORDER, lpc_mem, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800581 }
582
583 /* Check if the waveform is decaying, and if so how fast.
584 We do this to avoid adding energy when concealing in a segment
585 with decaying energy. */
586 {
587 opus_val32 E1=1, E2=1;
588 int decay_length;
589#ifdef FIXED_POINT
590 int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[MAX_PERIOD-exc_length], exc_length))-20);
591#endif
592 decay_length = exc_length>>1;
593 for (i=0;i<decay_length;i++)
594 {
595 opus_val16 e;
596 e = exc[MAX_PERIOD-decay_length+i];
597 E1 += SHR32(MULT16_16(e, e), shift);
598 e = exc[MAX_PERIOD-2*decay_length+i];
599 E2 += SHR32(MULT16_16(e, e), shift);
600 }
601 E1 = MIN32(E1, E2);
602 decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
603 }
604
605 /* Move the decoder memory one frame to the left to give us room to
606 add the data for the new frame. We ignore the overlap that extends
607 past the end of the buffer, because we aren't going to use it. */
608 OPUS_MOVE(buf, buf+N, DECODE_BUFFER_SIZE-N);
609
610 /* Extrapolate from the end of the excitation with a period of
611 "pitch_index", scaling down each period by an additional factor of
612 "decay". */
613 extrapolation_offset = MAX_PERIOD-pitch_index;
614 /* We need to extrapolate enough samples to cover a complete MDCT
615 window (including overlap/2 samples on both sides). */
616 extrapolation_len = N+overlap;
617 /* We also apply fading if this is not the first loss. */
618 attenuation = MULT16_16_Q15(fade, decay);
619 for (i=j=0;i<extrapolation_len;i++,j++)
620 {
621 opus_val16 tmp;
622 if (j >= pitch_index) {
623 j -= pitch_index;
624 attenuation = MULT16_16_Q15(attenuation, decay);
625 }
626 buf[DECODE_BUFFER_SIZE-N+i] =
627 SHL32(EXTEND32(MULT16_16_Q15(attenuation,
628 exc[extrapolation_offset+j])), SIG_SHIFT);
629 /* Compute the energy of the previously decoded signal whose
630 excitation we're copying. */
631 tmp = ROUND16(
632 buf[DECODE_BUFFER_SIZE-MAX_PERIOD-N+extrapolation_offset+j],
633 SIG_SHIFT);
634 S1 += SHR32(MULT16_16(tmp, tmp), 8);
635 }
636
637 {
638 opus_val16 lpc_mem[LPC_ORDER];
639 /* Copy the last decoded samples (prior to the overlap region) to
640 synthesis filter memory so we can have a continuous signal. */
641 for (i=0;i<LPC_ORDER;i++)
642 lpc_mem[i] = ROUND16(buf[DECODE_BUFFER_SIZE-N-1-i], SIG_SHIFT);
643 /* Apply the synthesis filter to convert the excitation back into
644 the signal domain. */
645 celt_iir(buf+DECODE_BUFFER_SIZE-N, lpc+c*LPC_ORDER,
646 buf+DECODE_BUFFER_SIZE-N, extrapolation_len, LPC_ORDER,
flimc91ee5b2016-01-26 14:33:44 +0100647 lpc_mem, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800648 }
649
650 /* Check if the synthesis energy is higher than expected, which can
651 happen with the signal changes during our window. If so,
652 attenuate. */
653 {
654 opus_val32 S2=0;
655 for (i=0;i<extrapolation_len;i++)
656 {
657 opus_val16 tmp = ROUND16(buf[DECODE_BUFFER_SIZE-N+i], SIG_SHIFT);
658 S2 += SHR32(MULT16_16(tmp, tmp), 8);
659 }
660 /* This checks for an "explosion" in the synthesis. */
661#ifdef FIXED_POINT
662 if (!(S1 > SHR32(S2,2)))
663#else
664 /* The float test is written this way to catch NaNs in the output
665 of the IIR filter at the same time. */
666 if (!(S1 > 0.2f*S2))
667#endif
668 {
669 for (i=0;i<extrapolation_len;i++)
670 buf[DECODE_BUFFER_SIZE-N+i] = 0;
671 } else if (S1 < S2)
672 {
673 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
674 for (i=0;i<overlap;i++)
675 {
676 opus_val16 tmp_g = Q15ONE
677 - MULT16_16_Q15(window[i], Q15ONE-ratio);
678 buf[DECODE_BUFFER_SIZE-N+i] =
679 MULT16_32_Q15(tmp_g, buf[DECODE_BUFFER_SIZE-N+i]);
680 }
681 for (i=overlap;i<extrapolation_len;i++)
682 {
683 buf[DECODE_BUFFER_SIZE-N+i] =
684 MULT16_32_Q15(ratio, buf[DECODE_BUFFER_SIZE-N+i]);
685 }
686 }
687 }
688
689 /* Apply the pre-filter to the MDCT overlap for the next frame because
690 the post-filter will be re-applied in the decoder after the MDCT
691 overlap. */
692 comb_filter(etmp, buf+DECODE_BUFFER_SIZE,
693 st->postfilter_period, st->postfilter_period, overlap,
694 -st->postfilter_gain, -st->postfilter_gain,
flimc91ee5b2016-01-26 14:33:44 +0100695 st->postfilter_tapset, st->postfilter_tapset, NULL, 0, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800696
697 /* Simulate TDAC on the concealed audio so that it blends with the
698 MDCT of the next frame. */
699 for (i=0;i<overlap/2;i++)
700 {
701 buf[DECODE_BUFFER_SIZE+i] =
702 MULT16_32_Q15(window[i], etmp[overlap-1-i])
703 + MULT16_32_Q15(window[overlap-i-1], etmp[i]);
704 }
705 } while (++c<C);
706 }
707
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800708 st->loss_count = loss_count+1;
709
710 RESTORE_STACK;
711}
712
flimc91ee5b2016-01-26 14:33:44 +0100713int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data,
714 int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800715{
716 int c, i, N;
717 int spread_decision;
718 opus_int32 bits;
719 ec_dec _dec;
flimc91ee5b2016-01-26 14:33:44 +0100720#ifdef NORM_ALIASING_HACK
721 celt_norm *X;
722#else
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800723 VARDECL(celt_norm, X);
flimc91ee5b2016-01-26 14:33:44 +0100724#endif
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800725 VARDECL(int, fine_quant);
726 VARDECL(int, pulses);
727 VARDECL(int, cap);
728 VARDECL(int, offsets);
729 VARDECL(int, fine_priority);
730 VARDECL(int, tf_res);
731 VARDECL(unsigned char, collapse_masks);
732 celt_sig *decode_mem[2];
733 celt_sig *out_syn[2];
734 opus_val16 *lpc;
735 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
736
737 int shortBlocks;
738 int isTransient;
739 int intra_ener;
740 const int CC = st->channels;
741 int LM, M;
flimc91ee5b2016-01-26 14:33:44 +0100742 int start;
743 int end;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800744 int effEnd;
745 int codedBands;
746 int alloc_trim;
747 int postfilter_pitch;
748 opus_val16 postfilter_gain;
749 int intensity=0;
750 int dual_stereo=0;
751 opus_int32 total_bits;
752 opus_int32 balance;
753 opus_int32 tell;
754 int dynalloc_logp;
755 int postfilter_tapset;
756 int anti_collapse_rsv;
757 int anti_collapse_on=0;
758 int silence;
759 int C = st->stream_channels;
760 const OpusCustomMode *mode;
761 int nbEBands;
762 int overlap;
763 const opus_int16 *eBands;
764 ALLOC_STACK;
765
766 mode = st->mode;
767 nbEBands = mode->nbEBands;
768 overlap = mode->overlap;
769 eBands = mode->eBands;
flimc91ee5b2016-01-26 14:33:44 +0100770 start = st->start;
771 end = st->end;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800772 frame_size *= st->downsample;
773
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800774 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC);
775 oldBandE = lpc+CC*LPC_ORDER;
776 oldLogE = oldBandE + 2*nbEBands;
777 oldLogE2 = oldLogE + 2*nbEBands;
778 backgroundLogE = oldLogE2 + 2*nbEBands;
779
780#ifdef CUSTOM_MODES
781 if (st->signalling && data!=NULL)
782 {
783 int data0=data[0];
784 /* Convert "standard mode" to Opus header */
785 if (mode->Fs==48000 && mode->shortMdctSize==120)
786 {
787 data0 = fromOpus(data0);
788 if (data0<0)
789 return OPUS_INVALID_PACKET;
790 }
flimc91ee5b2016-01-26 14:33:44 +0100791 st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800792 LM = (data0>>3)&0x3;
793 C = 1 + ((data0>>2)&0x1);
794 data++;
795 len--;
796 if (LM>mode->maxLM)
797 return OPUS_INVALID_PACKET;
798 if (frame_size < mode->shortMdctSize<<LM)
799 return OPUS_BUFFER_TOO_SMALL;
800 else
801 frame_size = mode->shortMdctSize<<LM;
802 } else {
803#else
804 {
805#endif
806 for (LM=0;LM<=mode->maxLM;LM++)
807 if (mode->shortMdctSize<<LM==frame_size)
808 break;
809 if (LM>mode->maxLM)
810 return OPUS_BAD_ARG;
811 }
812 M=1<<LM;
813
814 if (len<0 || len>1275 || pcm==NULL)
815 return OPUS_BAD_ARG;
816
817 N = M*mode->shortMdctSize;
flimc91ee5b2016-01-26 14:33:44 +0100818 c=0; do {
819 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
820 out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N;
821 } while (++c<CC);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800822
flimc91ee5b2016-01-26 14:33:44 +0100823 effEnd = end;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800824 if (effEnd > mode->effEBands)
825 effEnd = mode->effEBands;
826
827 if (data == NULL || len<=1)
828 {
flimc91ee5b2016-01-26 14:33:44 +0100829 celt_decode_lost(st, N, LM);
830 deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800831 RESTORE_STACK;
832 return frame_size/st->downsample;
833 }
834
835 if (dec == NULL)
836 {
837 ec_dec_init(&_dec,(unsigned char*)data,len);
838 dec = &_dec;
839 }
840
841 if (C==1)
842 {
843 for (i=0;i<nbEBands;i++)
844 oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]);
845 }
846
847 total_bits = len*8;
848 tell = ec_tell(dec);
849
850 if (tell >= total_bits)
851 silence = 1;
852 else if (tell==1)
853 silence = ec_dec_bit_logp(dec, 15);
854 else
855 silence = 0;
856 if (silence)
857 {
858 /* Pretend we've read all the remaining bits */
859 tell = len*8;
860 dec->nbits_total+=tell-ec_tell(dec);
861 }
862
863 postfilter_gain = 0;
864 postfilter_pitch = 0;
865 postfilter_tapset = 0;
flimc91ee5b2016-01-26 14:33:44 +0100866 if (start==0 && tell+16 <= total_bits)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800867 {
868 if(ec_dec_bit_logp(dec, 1))
869 {
870 int qg, octave;
871 octave = ec_dec_uint(dec, 6);
872 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
873 qg = ec_dec_bits(dec, 3);
874 if (ec_tell(dec)+2<=total_bits)
875 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
876 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
877 }
878 tell = ec_tell(dec);
879 }
880
881 if (LM > 0 && tell+3 <= total_bits)
882 {
883 isTransient = ec_dec_bit_logp(dec, 3);
884 tell = ec_tell(dec);
885 }
886 else
887 isTransient = 0;
888
889 if (isTransient)
890 shortBlocks = M;
891 else
892 shortBlocks = 0;
893
894 /* Decode the global flags (first symbols in the stream) */
895 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
896 /* Get band energies */
flimc91ee5b2016-01-26 14:33:44 +0100897 unquant_coarse_energy(mode, start, end, oldBandE,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800898 intra_ener, dec, C, LM);
899
900 ALLOC(tf_res, nbEBands, int);
flimc91ee5b2016-01-26 14:33:44 +0100901 tf_decode(start, end, isTransient, tf_res, LM, dec);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800902
903 tell = ec_tell(dec);
904 spread_decision = SPREAD_NORMAL;
905 if (tell+4 <= total_bits)
906 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
907
908 ALLOC(cap, nbEBands, int);
909
910 init_caps(mode,cap,LM,C);
911
912 ALLOC(offsets, nbEBands, int);
913
914 dynalloc_logp = 6;
915 total_bits<<=BITRES;
916 tell = ec_tell_frac(dec);
flimc91ee5b2016-01-26 14:33:44 +0100917 for (i=start;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800918 {
919 int width, quanta;
920 int dynalloc_loop_logp;
921 int boost;
922 width = C*(eBands[i+1]-eBands[i])<<LM;
923 /* quanta is 6 bits, but no more than 1 bit/sample
924 and no less than 1/8 bit/sample */
925 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
926 dynalloc_loop_logp = dynalloc_logp;
927 boost = 0;
928 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
929 {
930 int flag;
931 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
932 tell = ec_tell_frac(dec);
933 if (!flag)
934 break;
935 boost += quanta;
936 total_bits -= quanta;
937 dynalloc_loop_logp = 1;
938 }
939 offsets[i] = boost;
940 /* Making dynalloc more likely */
941 if (boost>0)
942 dynalloc_logp = IMAX(2, dynalloc_logp-1);
943 }
944
945 ALLOC(fine_quant, nbEBands, int);
946 alloc_trim = tell+(6<<BITRES) <= total_bits ?
947 ec_dec_icdf(dec, trim_icdf, 7) : 5;
948
949 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
950 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
951 bits -= anti_collapse_rsv;
952
953 ALLOC(pulses, nbEBands, int);
954 ALLOC(fine_priority, nbEBands, int);
955
flimc91ee5b2016-01-26 14:33:44 +0100956 codedBands = compute_allocation(mode, start, end, offsets, cap,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800957 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
958 fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
959
flimc91ee5b2016-01-26 14:33:44 +0100960 unquant_fine_energy(mode, start, end, oldBandE, fine_quant, dec, C);
961
962 c=0; do {
963 OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap/2);
964 } while (++c<CC);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800965
966 /* Decode fixed codebook */
967 ALLOC(collapse_masks, C*nbEBands, unsigned char);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800968
flimc91ee5b2016-01-26 14:33:44 +0100969#ifdef NORM_ALIASING_HACK
970 /* This is an ugly hack that breaks aliasing rules and would be easily broken,
971 but it saves almost 4kB of stack. */
972 X = (celt_norm*)(out_syn[CC-1]+overlap/2);
973#else
974 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
975#endif
976
977 quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800978 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
flimc91ee5b2016-01-26 14:33:44 +0100979 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800980
981 if (anti_collapse_rsv > 0)
982 {
983 anti_collapse_on = ec_dec_bits(dec, 1);
984 }
985
flimc91ee5b2016-01-26 14:33:44 +0100986 unquant_energy_finalise(mode, start, end, oldBandE,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800987 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
988
989 if (anti_collapse_on)
990 anti_collapse(mode, X, collapse_masks, LM, C, N,
flimc91ee5b2016-01-26 14:33:44 +0100991 start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800992
993 if (silence)
994 {
995 for (i=0;i<C*nbEBands;i++)
996 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800997 }
998
flimc91ee5b2016-01-26 14:33:44 +0100999 celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1000 C, CC, isTransient, LM, st->downsample, silence, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001001
1002 c=0; do {
1003 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1004 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1005 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1006 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
flimc91ee5b2016-01-26 14:33:44 +01001007 mode->window, overlap, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001008 if (LM!=0)
1009 comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1010 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
flimc91ee5b2016-01-26 14:33:44 +01001011 mode->window, overlap, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001012
1013 } while (++c<CC);
1014 st->postfilter_period_old = st->postfilter_period;
1015 st->postfilter_gain_old = st->postfilter_gain;
1016 st->postfilter_tapset_old = st->postfilter_tapset;
1017 st->postfilter_period = postfilter_pitch;
1018 st->postfilter_gain = postfilter_gain;
1019 st->postfilter_tapset = postfilter_tapset;
1020 if (LM!=0)
1021 {
1022 st->postfilter_period_old = st->postfilter_period;
1023 st->postfilter_gain_old = st->postfilter_gain;
1024 st->postfilter_tapset_old = st->postfilter_tapset;
1025 }
1026
flimc91ee5b2016-01-26 14:33:44 +01001027 if (C==1)
1028 OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001029
1030 /* In case start or end were to change */
1031 if (!isTransient)
1032 {
flimc91ee5b2016-01-26 14:33:44 +01001033 opus_val16 max_background_increase;
1034 OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1035 OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
1036 /* In normal circumstances, we only allow the noise floor to increase by
1037 up to 2.4 dB/second, but when we're in DTX, we allow up to 6 dB
1038 increase for each update.*/
1039 if (st->loss_count < 10)
1040 max_background_increase = M*QCONST16(0.001f,DB_SHIFT);
1041 else
1042 max_background_increase = QCONST16(1.f,DB_SHIFT);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001043 for (i=0;i<2*nbEBands;i++)
flimc91ee5b2016-01-26 14:33:44 +01001044 backgroundLogE[i] = MIN16(backgroundLogE[i] + max_background_increase, oldBandE[i]);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001045 } else {
1046 for (i=0;i<2*nbEBands;i++)
1047 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1048 }
1049 c=0; do
1050 {
flimc91ee5b2016-01-26 14:33:44 +01001051 for (i=0;i<start;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001052 {
1053 oldBandE[c*nbEBands+i]=0;
1054 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1055 }
flimc91ee5b2016-01-26 14:33:44 +01001056 for (i=end;i<nbEBands;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001057 {
1058 oldBandE[c*nbEBands+i]=0;
1059 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1060 }
1061 } while (++c<2);
1062 st->rng = dec->rng;
1063
flimc91ee5b2016-01-26 14:33:44 +01001064 deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001065 st->loss_count = 0;
1066 RESTORE_STACK;
1067 if (ec_tell(dec) > 8*len)
1068 return OPUS_INTERNAL_ERROR;
1069 if(ec_get_error(dec))
1070 st->error = 1;
1071 return frame_size/st->downsample;
1072}
1073
1074
1075#ifdef CUSTOM_MODES
1076
1077#ifdef FIXED_POINT
1078int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
1079{
flimc91ee5b2016-01-26 14:33:44 +01001080 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001081}
1082
1083#ifndef DISABLE_FLOAT_API
1084int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
1085{
1086 int j, ret, C, N;
1087 VARDECL(opus_int16, out);
1088 ALLOC_STACK;
1089
1090 if (pcm==NULL)
1091 return OPUS_BAD_ARG;
1092
1093 C = st->channels;
1094 N = frame_size;
1095
1096 ALLOC(out, C*N, opus_int16);
flimc91ee5b2016-01-26 14:33:44 +01001097 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001098 if (ret>0)
1099 for (j=0;j<C*ret;j++)
1100 pcm[j]=out[j]*(1.f/32768.f);
1101
1102 RESTORE_STACK;
1103 return ret;
1104}
1105#endif /* DISABLE_FLOAT_API */
1106
1107#else
1108
1109int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
1110{
flimc91ee5b2016-01-26 14:33:44 +01001111 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001112}
1113
1114int 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
1120 if (pcm==NULL)
1121 return OPUS_BAD_ARG;
1122
1123 C = st->channels;
1124 N = frame_size;
1125 ALLOC(out, C*N, celt_sig);
1126
flimc91ee5b2016-01-26 14:33:44 +01001127 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001128
1129 if (ret>0)
1130 for (j=0;j<C*ret;j++)
1131 pcm[j] = FLOAT2INT16 (out[j]);
1132
1133 RESTORE_STACK;
1134 return ret;
1135}
1136
1137#endif
1138#endif /* CUSTOM_MODES */
1139
1140int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
1141{
1142 va_list ap;
1143
1144 va_start(ap, request);
1145 switch (request)
1146 {
1147 case CELT_SET_START_BAND_REQUEST:
1148 {
1149 opus_int32 value = va_arg(ap, opus_int32);
1150 if (value<0 || value>=st->mode->nbEBands)
1151 goto bad_arg;
1152 st->start = value;
1153 }
1154 break;
1155 case CELT_SET_END_BAND_REQUEST:
1156 {
1157 opus_int32 value = va_arg(ap, opus_int32);
1158 if (value<1 || value>st->mode->nbEBands)
1159 goto bad_arg;
1160 st->end = value;
1161 }
1162 break;
1163 case CELT_SET_CHANNELS_REQUEST:
1164 {
1165 opus_int32 value = va_arg(ap, opus_int32);
1166 if (value<1 || value>2)
1167 goto bad_arg;
1168 st->stream_channels = value;
1169 }
1170 break;
1171 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
1172 {
1173 opus_int32 *value = va_arg(ap, opus_int32*);
1174 if (value==NULL)
1175 goto bad_arg;
1176 *value=st->error;
1177 st->error = 0;
1178 }
1179 break;
1180 case OPUS_GET_LOOKAHEAD_REQUEST:
1181 {
1182 opus_int32 *value = va_arg(ap, opus_int32*);
1183 if (value==NULL)
1184 goto bad_arg;
1185 *value = st->overlap/st->downsample;
1186 }
1187 break;
1188 case OPUS_RESET_STATE:
1189 {
1190 int i;
1191 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
1192 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
1193 oldBandE = lpc+st->channels*LPC_ORDER;
1194 oldLogE = oldBandE + 2*st->mode->nbEBands;
1195 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
1196 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
1197 opus_custom_decoder_get_size(st->mode, st->channels)-
1198 ((char*)&st->DECODER_RESET_START - (char*)st));
1199 for (i=0;i<2*st->mode->nbEBands;i++)
1200 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1201 }
1202 break;
1203 case OPUS_GET_PITCH_REQUEST:
1204 {
1205 opus_int32 *value = va_arg(ap, opus_int32*);
1206 if (value==NULL)
1207 goto bad_arg;
1208 *value = st->postfilter_period;
1209 }
1210 break;
1211 case CELT_GET_MODE_REQUEST:
1212 {
1213 const CELTMode ** value = va_arg(ap, const CELTMode**);
1214 if (value==0)
1215 goto bad_arg;
1216 *value=st->mode;
1217 }
1218 break;
1219 case CELT_SET_SIGNALLING_REQUEST:
1220 {
1221 opus_int32 value = va_arg(ap, opus_int32);
1222 st->signalling = value;
1223 }
1224 break;
1225 case OPUS_GET_FINAL_RANGE_REQUEST:
1226 {
1227 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1228 if (value==0)
1229 goto bad_arg;
1230 *value=st->rng;
1231 }
1232 break;
1233 default:
1234 goto bad_request;
1235 }
1236 va_end(ap);
1237 return OPUS_OK;
1238bad_arg:
1239 va_end(ap);
1240 return OPUS_BAD_ARG;
1241bad_request:
1242 va_end(ap);
1243 return OPUS_UNIMPLEMENTED;
1244}