Coverage Report

Created: 2026-01-09 07:33

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