Coverage Report

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