Coverage Report

Created: 2025-11-16 07:20

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