Coverage Report

Created: 2026-10-28 08:13

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