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