Coverage Report

Created: 2026-05-16 07:49

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