Coverage Report

Created: 2025-10-10 07:38

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
379k
#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
94.8k
#define PLC_PITCH_LAG_MIN (100)
66
67
/**********************************************************************/
68
/*                                                                    */
69
/*                             DECODER                                */
70
/*                                                                    */
71
/**********************************************************************/
72
189k
#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
490k
{
133
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT)
134
260k
   celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL));
135
260k
   celt_assert(st->overlap == 120);
136
260k
   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
230k
   celt_assert(st->end <= 25);
143
230k
#endif
144
490k
   celt_assert(st->channels == 1 || st->channels == 2);
145
490k
   celt_assert(st->stream_channels == 1 || st->stream_channels == 2);
146
490k
   celt_assert(st->downsample > 0);
147
490k
   celt_assert(st->start == 0 || st->start == 17);
148
490k
   celt_assert(st->start < st->end);
149
490k
#ifdef OPUS_ARCHMASK
150
490k
   celt_assert(st->arch >= 0);
151
490k
   celt_assert(st->arch <= OPUS_ARCHMASK);
152
490k
#endif
153
#ifndef ENABLE_QEXT
154
260k
   celt_assert(st->last_pitch_index <= PLC_PITCH_LAG_MAX);
155
260k
   celt_assert(st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0);
156
260k
#endif
157
490k
   celt_assert(st->postfilter_period < MAX_PERIOD);
158
490k
   celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0);
159
490k
   celt_assert(st->postfilter_period_old < MAX_PERIOD);
160
490k
   celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0);
161
490k
   celt_assert(st->postfilter_tapset <= 2);
162
490k
   celt_assert(st->postfilter_tapset >= 0);
163
490k
   celt_assert(st->postfilter_tapset_old <= 2);
164
490k
   celt_assert(st->postfilter_tapset_old >= 0);
165
490k
}
validate_celt_decoder
Line
Count
Source
132
260k
{
133
260k
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT)
134
260k
   celt_assert(st->mode == opus_custom_mode_create(48000, 960, NULL));
135
260k
   celt_assert(st->overlap == 120);
136
260k
   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
260k
   celt_assert(st->channels == 1 || st->channels == 2);
145
260k
   celt_assert(st->stream_channels == 1 || st->stream_channels == 2);
146
260k
   celt_assert(st->downsample > 0);
147
260k
   celt_assert(st->start == 0 || st->start == 17);
148
260k
   celt_assert(st->start < st->end);
149
260k
#ifdef OPUS_ARCHMASK
150
260k
   celt_assert(st->arch >= 0);
151
260k
   celt_assert(st->arch <= OPUS_ARCHMASK);
152
260k
#endif
153
260k
#ifndef ENABLE_QEXT
154
260k
   celt_assert(st->last_pitch_index <= PLC_PITCH_LAG_MAX);
155
260k
   celt_assert(st->last_pitch_index >= PLC_PITCH_LAG_MIN || st->last_pitch_index == 0);
156
260k
#endif
157
260k
   celt_assert(st->postfilter_period < MAX_PERIOD);
158
260k
   celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0);
159
260k
   celt_assert(st->postfilter_period_old < MAX_PERIOD);
160
260k
   celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0);
161
260k
   celt_assert(st->postfilter_tapset <= 2);
162
260k
   celt_assert(st->postfilter_tapset >= 0);
163
260k
   celt_assert(st->postfilter_tapset_old <= 2);
164
260k
   celt_assert(st->postfilter_tapset_old >= 0);
165
260k
}
validate_celt_decoder
Line
Count
Source
132
230k
{
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
230k
   celt_assert(st->end <= 25);
143
230k
#endif
144
230k
   celt_assert(st->channels == 1 || st->channels == 2);
145
230k
   celt_assert(st->stream_channels == 1 || st->stream_channels == 2);
146
230k
   celt_assert(st->downsample > 0);
147
230k
   celt_assert(st->start == 0 || st->start == 17);
148
230k
   celt_assert(st->start < st->end);
149
230k
#ifdef OPUS_ARCHMASK
150
230k
   celt_assert(st->arch >= 0);
151
230k
   celt_assert(st->arch <= OPUS_ARCHMASK);
152
230k
#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
230k
   celt_assert(st->postfilter_period < MAX_PERIOD);
158
230k
   celt_assert(st->postfilter_period >= COMBFILTER_MINPERIOD || st->postfilter_period == 0);
159
230k
   celt_assert(st->postfilter_period_old < MAX_PERIOD);
160
230k
   celt_assert(st->postfilter_period_old >= COMBFILTER_MINPERIOD || st->postfilter_period_old == 0);
161
230k
   celt_assert(st->postfilter_tapset <= 2);
162
230k
   celt_assert(st->postfilter_tapset >= 0);
163
230k
   celt_assert(st->postfilter_tapset_old <= 2);
164
230k
   celt_assert(st->postfilter_tapset_old >= 0);
165
230k
}
166
#endif
167
168
int celt_decoder_get_size(int channels)
169
823k
{
170
#ifdef ENABLE_QEXT
171
   const CELTMode *mode = opus_custom_mode_create(96000, 960, NULL);
172
#else
173
823k
   const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
174
823k
#endif
175
823k
   return opus_custom_decoder_get_size(mode, channels);
176
823k
}
177
178
OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
179
1.64M
{
180
1.64M
   int size;
181
1.64M
   int extra=0;
182
#ifdef ENABLE_QEXT
183
   int qext_scale;
184
886k
   extra = 2*NB_QEXT_BANDS*sizeof(celt_glog);
185
886k
   if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) {
186
531k
      qext_scale = 2;
187
531k
   } else qext_scale = 1;
188
#endif
189
1.64M
   size = sizeof(struct CELTDecoder)
190
1.64M
            + (channels*(QEXT_SCALE(DECODE_BUFFER_SIZE)+mode->overlap)-1)*sizeof(celt_sig)
191
1.64M
            + channels*CELT_LPC_ORDER*sizeof(opus_val16)
192
1.64M
            + 4*2*mode->nbEBands*sizeof(celt_glog)
193
1.64M
            + extra;
194
1.64M
   return size;
195
1.64M
}
celt_decoder.c:opus_custom_decoder_get_size
Line
Count
Source
179
757k
{
180
757k
   int size;
181
757k
   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
757k
   size = sizeof(struct CELTDecoder)
190
757k
            + (channels*(QEXT_SCALE(DECODE_BUFFER_SIZE)+mode->overlap)-1)*sizeof(celt_sig)
191
757k
            + channels*CELT_LPC_ORDER*sizeof(opus_val16)
192
757k
            + 4*2*mode->nbEBands*sizeof(celt_glog)
193
757k
            + extra;
194
757k
   return size;
195
757k
}
celt_decoder.c:opus_custom_decoder_get_size
Line
Count
Source
179
886k
{
180
886k
   int size;
181
886k
   int extra=0;
182
886k
#ifdef ENABLE_QEXT
183
886k
   int qext_scale;
184
886k
   extra = 2*NB_QEXT_BANDS*sizeof(celt_glog);
185
886k
   if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) {
186
531k
      qext_scale = 2;
187
531k
   } else qext_scale = 1;
188
886k
#endif
189
886k
   size = sizeof(struct CELTDecoder)
190
886k
            + (channels*(QEXT_SCALE(DECODE_BUFFER_SIZE)+mode->overlap)-1)*sizeof(celt_sig)
191
886k
            + channels*CELT_LPC_ORDER*sizeof(opus_val16)
192
886k
            + 4*2*mode->nbEBands*sizeof(celt_glog)
193
886k
            + extra;
194
886k
   return size;
195
886k
}
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
187k
{
216
187k
   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
187k
   ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
223
187k
   if (ret != OPUS_OK)
224
0
      return ret;
225
187k
   st->downsample = resampling_factor(sampling_rate);
226
187k
   if (st->downsample==0)
227
0
      return OPUS_BAD_ARG;
228
187k
   else
229
187k
      return OPUS_OK;
230
187k
}
231
232
OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
233
187k
{
234
187k
   if (channels < 0 || channels > 2)
235
0
      return OPUS_BAD_ARG;
236
237
187k
   if (st==NULL)
238
0
      return OPUS_ALLOC_FAIL;
239
240
187k
   OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
241
242
187k
   st->mode = mode;
243
187k
   st->overlap = mode->overlap;
244
187k
   st->stream_channels = st->channels = channels;
245
246
187k
   st->downsample = 1;
247
187k
   st->start = 0;
248
187k
   st->end = st->mode->effEBands;
249
187k
   st->signalling = 1;
250
187k
#ifndef DISABLE_UPDATE_DRAFT
251
187k
   st->disable_inv = channels == 1;
252
#else
253
   st->disable_inv = 0;
254
#endif
255
187k
   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
187k
   opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
263
264
187k
   return OPUS_OK;
265
187k
}
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
34.2k
{
281
34.2k
   celt_sig * OPUS_RESTRICT x0;
282
34.2k
   celt_sig * OPUS_RESTRICT x1;
283
34.2k
   celt_sig m0, m1;
284
34.2k
   int j;
285
34.2k
   x0=in[0];
286
34.2k
   x1=in[1];
287
34.2k
   m0 = mem[0];
288
34.2k
   m1 = mem[1];
289
7.97M
   for (j=0;j<N;j++)
290
7.94M
   {
291
7.94M
      celt_sig tmp0, tmp1;
292
      /* Add VERY_SMALL to x[] first to reduce dependency chain. */
293
7.94M
      tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT);
294
7.94M
      tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT);
295
7.94M
      m0 = MULT16_32_Q15(coef0, tmp0);
296
7.94M
      m1 = MULT16_32_Q15(coef0, tmp1);
297
7.94M
      pcm[2*j  ] = SIG2RES(tmp0);
298
7.94M
      pcm[2*j+1] = SIG2RES(tmp1);
299
7.94M
   }
300
34.2k
   mem[0] = m0;
301
34.2k
   mem[1] = m1;
302
34.2k
}
celt_decoder.c:deemphasis_stereo_simple
Line
Count
Source
280
20.8k
{
281
20.8k
   celt_sig * OPUS_RESTRICT x0;
282
20.8k
   celt_sig * OPUS_RESTRICT x1;
283
20.8k
   celt_sig m0, m1;
284
20.8k
   int j;
285
20.8k
   x0=in[0];
286
20.8k
   x1=in[1];
287
20.8k
   m0 = mem[0];
288
20.8k
   m1 = mem[1];
289
4.21M
   for (j=0;j<N;j++)
290
4.19M
   {
291
4.19M
      celt_sig tmp0, tmp1;
292
      /* Add VERY_SMALL to x[] first to reduce dependency chain. */
293
4.19M
      tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT);
294
4.19M
      tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT);
295
4.19M
      m0 = MULT16_32_Q15(coef0, tmp0);
296
4.19M
      m1 = MULT16_32_Q15(coef0, tmp1);
297
4.19M
      pcm[2*j  ] = SIG2RES(tmp0);
298
4.19M
      pcm[2*j+1] = SIG2RES(tmp1);
299
4.19M
   }
300
20.8k
   mem[0] = m0;
301
20.8k
   mem[1] = m1;
302
20.8k
}
celt_decoder.c:deemphasis_stereo_simple
Line
Count
Source
280
13.4k
{
281
13.4k
   celt_sig * OPUS_RESTRICT x0;
282
13.4k
   celt_sig * OPUS_RESTRICT x1;
283
13.4k
   celt_sig m0, m1;
284
13.4k
   int j;
285
13.4k
   x0=in[0];
286
13.4k
   x1=in[1];
287
13.4k
   m0 = mem[0];
288
13.4k
   m1 = mem[1];
289
3.76M
   for (j=0;j<N;j++)
290
3.74M
   {
291
3.74M
      celt_sig tmp0, tmp1;
292
      /* Add VERY_SMALL to x[] first to reduce dependency chain. */
293
3.74M
      tmp0 = SATURATE(x0[j] + VERY_SMALL + m0, SIG_SAT);
294
3.74M
      tmp1 = SATURATE(x1[j] + VERY_SMALL + m1, SIG_SAT);
295
3.74M
      m0 = MULT16_32_Q15(coef0, tmp0);
296
3.74M
      m1 = MULT16_32_Q15(coef0, tmp1);
297
3.74M
      pcm[2*j  ] = SIG2RES(tmp0);
298
3.74M
      pcm[2*j+1] = SIG2RES(tmp1);
299
3.74M
   }
300
13.4k
   mem[0] = m0;
301
13.4k
   mem[1] = m1;
302
13.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
490k
{
311
490k
   int c;
312
490k
   int Nd;
313
490k
   int apply_downsampling=0;
314
490k
   opus_val16 coef0;
315
490k
   VARDECL(celt_sig, scratch);
316
490k
   SAVE_STACK;
317
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT)
318
   /* Short version for common case. */
319
260k
   if (downsample == 1 && C == 2 && !accum)
320
34.2k
   {
321
34.2k
      deemphasis_stereo_simple(in, pcm, N, coef[0], mem);
322
34.2k
      return;
323
34.2k
   }
324
226k
#endif
325
456k
   ALLOC(scratch, N, celt_sig);
326
226k
   coef0 = coef[0];
327
226k
   Nd = N/downsample;
328
670k
   c=0; do {
329
670k
      int j;
330
670k
      celt_sig * OPUS_RESTRICT x;
331
670k
      opus_res  * OPUS_RESTRICT y;
332
670k
      celt_sig m = mem[c];
333
670k
      x =in[c];
334
670k
      y = pcm+c;
335
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
336
330k
      if (coef[1] != 0)
337
94.0k
      {
338
94.0k
         opus_val16 coef1 = coef[1];
339
94.0k
         opus_val16 coef3 = coef[3];
340
45.8M
         for (j=0;j<N;j++)
341
45.7M
         {
342
45.7M
            celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
343
45.7M
            m = MULT16_32_Q15(coef0, tmp)
344
45.7M
                          - MULT16_32_Q15(coef1, x[j]);
345
45.7M
            tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
346
45.7M
            scratch[j] = tmp;
347
45.7M
         }
348
94.0k
         apply_downsampling=1;
349
94.0k
      } else
350
236k
#endif
351
576k
      if (downsample>1)
352
484k
      {
353
         /* Shortcut for the standard (non-custom modes) case */
354
137M
         for (j=0;j<N;j++)
355
136M
         {
356
136M
            celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
357
136M
            m = MULT16_32_Q15(coef0, tmp);
358
136M
            scratch[j] = tmp;
359
136M
         }
360
484k
         apply_downsampling=1;
361
484k
      } else {
362
         /* Shortcut for the standard (non-custom modes) case */
363
92.1k
         if (accum)
364
25.8k
         {
365
14.2M
            for (j=0;j<N;j++)
366
14.1M
            {
367
14.1M
               celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
368
14.1M
               m = MULT16_32_Q15(coef0, tmp);
369
14.1M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp));
370
14.1M
            }
371
25.8k
         } else
372
66.2k
         {
373
17.4M
            for (j=0;j<N;j++)
374
17.3M
            {
375
17.3M
               celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
376
17.3M
               m = MULT16_32_Q15(coef0, tmp);
377
17.3M
               y[j*C] = SIG2RES(tmp);
378
17.3M
            }
379
66.2k
         }
380
92.1k
      }
381
670k
      mem[c] = m;
382
383
670k
      if (apply_downsampling)
384
578k
      {
385
         /* Perform down-sampling */
386
578k
         if (accum)
387
72.1k
         {
388
18.1M
            for (j=0;j<Nd;j++)
389
18.0M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample]));
390
72.1k
         } else
391
506k
         {
392
69.9M
            for (j=0;j<Nd;j++)
393
69.4M
               y[j*C] = SIG2RES(scratch[j*downsample]);
394
506k
         }
395
578k
      }
396
670k
   } while (++c<C);
397
226k
   RESTORE_STACK;
398
226k
}
celt_decoder.c:deemphasis
Line
Count
Source
310
146k
{
311
146k
   int c;
312
146k
   int Nd;
313
146k
   int apply_downsampling=0;
314
146k
   opus_val16 coef0;
315
146k
   VARDECL(celt_sig, scratch);
316
146k
   SAVE_STACK;
317
146k
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT)
318
   /* Short version for common case. */
319
146k
   if (downsample == 1 && C == 2 && !accum)
320
20.8k
   {
321
20.8k
      deemphasis_stereo_simple(in, pcm, N, coef[0], mem);
322
20.8k
      return;
323
20.8k
   }
324
125k
#endif
325
125k
   ALLOC(scratch, N, celt_sig);
326
125k
   coef0 = coef[0];
327
125k
   Nd = N/downsample;
328
191k
   c=0; do {
329
191k
      int j;
330
191k
      celt_sig * OPUS_RESTRICT x;
331
191k
      opus_res  * OPUS_RESTRICT y;
332
191k
      celt_sig m = mem[c];
333
191k
      x =in[c];
334
191k
      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
191k
      if (downsample>1)
352
169k
      {
353
         /* Shortcut for the standard (non-custom modes) case */
354
43.4M
         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
169k
         apply_downsampling=1;
361
169k
      } else {
362
         /* Shortcut for the standard (non-custom modes) case */
363
21.9k
         if (accum)
364
9.07k
         {
365
4.90M
            for (j=0;j<N;j++)
366
4.89M
            {
367
4.89M
               celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
368
4.89M
               m = MULT16_32_Q15(coef0, tmp);
369
4.89M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp));
370
4.89M
            }
371
9.07k
         } else
372
12.8k
         {
373
3.87M
            for (j=0;j<N;j++)
374
3.86M
            {
375
3.86M
               celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
376
3.86M
               m = MULT16_32_Q15(coef0, tmp);
377
3.86M
               y[j*C] = SIG2RES(tmp);
378
3.86M
            }
379
12.8k
         }
380
21.9k
      }
381
191k
      mem[c] = m;
382
383
191k
      if (apply_downsampling)
384
169k
      {
385
         /* Perform down-sampling */
386
169k
         if (accum)
387
18.9k
         {
388
3.03M
            for (j=0;j<Nd;j++)
389
3.01M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample]));
390
18.9k
         } else
391
150k
         {
392
10.8M
            for (j=0;j<Nd;j++)
393
10.6M
               y[j*C] = SIG2RES(scratch[j*downsample]);
394
150k
         }
395
169k
      }
396
191k
   } while (++c<C);
397
125k
   RESTORE_STACK;
398
125k
}
celt_decoder.c:deemphasis
Line
Count
Source
310
135k
{
311
135k
   int c;
312
135k
   int Nd;
313
135k
   int apply_downsampling=0;
314
135k
   opus_val16 coef0;
315
135k
   VARDECL(celt_sig, scratch);
316
135k
   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
135k
   ALLOC(scratch, N, celt_sig);
326
135k
   coef0 = coef[0];
327
135k
   Nd = N/downsample;
328
188k
   c=0; do {
329
188k
      int j;
330
188k
      celt_sig * OPUS_RESTRICT x;
331
188k
      opus_res  * OPUS_RESTRICT y;
332
188k
      celt_sig m = mem[c];
333
188k
      x =in[c];
334
188k
      y = pcm+c;
335
188k
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
336
188k
      if (coef[1] != 0)
337
58.8k
      {
338
58.8k
         opus_val16 coef1 = coef[1];
339
58.8k
         opus_val16 coef3 = coef[3];
340
28.4M
         for (j=0;j<N;j++)
341
28.4M
         {
342
28.4M
            celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
343
28.4M
            m = MULT16_32_Q15(coef0, tmp)
344
28.4M
                          - MULT16_32_Q15(coef1, x[j]);
345
28.4M
            tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
346
28.4M
            scratch[j] = tmp;
347
28.4M
         }
348
58.8k
         apply_downsampling=1;
349
58.8k
      } else
350
129k
#endif
351
129k
      if (downsample>1)
352
96.6k
      {
353
         /* Shortcut for the standard (non-custom modes) case */
354
26.3M
         for (j=0;j<N;j++)
355
26.2M
         {
356
26.2M
            celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
357
26.2M
            m = MULT16_32_Q15(coef0, tmp);
358
26.2M
            scratch[j] = tmp;
359
26.2M
         }
360
96.6k
         apply_downsampling=1;
361
96.6k
      } else {
362
         /* Shortcut for the standard (non-custom modes) case */
363
33.0k
         if (accum)
364
4.36k
         {
365
2.44M
            for (j=0;j<N;j++)
366
2.43M
            {
367
2.43M
               celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
368
2.43M
               m = MULT16_32_Q15(coef0, tmp);
369
2.43M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp));
370
2.43M
            }
371
4.36k
         } else
372
28.7k
         {
373
6.58M
            for (j=0;j<N;j++)
374
6.56M
            {
375
6.56M
               celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
376
6.56M
               m = MULT16_32_Q15(coef0, tmp);
377
6.56M
               y[j*C] = SIG2RES(tmp);
378
6.56M
            }
379
28.7k
         }
380
33.0k
      }
381
188k
      mem[c] = m;
382
383
188k
      if (apply_downsampling)
384
155k
      {
385
         /* Perform down-sampling */
386
155k
         if (accum)
387
14.9k
         {
388
5.46M
            for (j=0;j<Nd;j++)
389
5.45M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample]));
390
14.9k
         } else
391
140k
         {
392
30.3M
            for (j=0;j<Nd;j++)
393
30.2M
               y[j*C] = SIG2RES(scratch[j*downsample]);
394
140k
         }
395
155k
      }
396
188k
   } while (++c<C);
397
135k
   RESTORE_STACK;
398
135k
}
celt_decoder.c:deemphasis
Line
Count
Source
310
94.8k
{
311
94.8k
   int c;
312
94.8k
   int Nd;
313
94.8k
   int apply_downsampling=0;
314
94.8k
   opus_val16 coef0;
315
94.8k
   VARDECL(celt_sig, scratch);
316
94.8k
   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
94.8k
   ALLOC(scratch, N, celt_sig);
326
94.8k
   coef0 = coef[0];
327
94.8k
   Nd = N/downsample;
328
141k
   c=0; do {
329
141k
      int j;
330
141k
      celt_sig * OPUS_RESTRICT x;
331
141k
      opus_res  * OPUS_RESTRICT y;
332
141k
      celt_sig m = mem[c];
333
141k
      x =in[c];
334
141k
      y = pcm+c;
335
141k
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
336
141k
      if (coef[1] != 0)
337
35.2k
      {
338
35.2k
         opus_val16 coef1 = coef[1];
339
35.2k
         opus_val16 coef3 = coef[3];
340
17.3M
         for (j=0;j<N;j++)
341
17.3M
         {
342
17.3M
            celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
343
17.3M
            m = MULT16_32_Q15(coef0, tmp)
344
17.3M
                          - MULT16_32_Q15(coef1, x[j]);
345
17.3M
            tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
346
17.3M
            scratch[j] = tmp;
347
17.3M
         }
348
35.2k
         apply_downsampling=1;
349
35.2k
      } else
350
106k
#endif
351
106k
      if (downsample>1)
352
88.7k
      {
353
         /* Shortcut for the standard (non-custom modes) case */
354
24.3M
         for (j=0;j<N;j++)
355
24.2M
         {
356
24.2M
            celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
357
24.2M
            m = MULT16_32_Q15(coef0, tmp);
358
24.2M
            scratch[j] = tmp;
359
24.2M
         }
360
88.7k
         apply_downsampling=1;
361
88.7k
      } else {
362
         /* Shortcut for the standard (non-custom modes) case */
363
17.7k
         if (accum)
364
3.73k
         {
365
2.26M
            for (j=0;j<N;j++)
366
2.25M
            {
367
2.25M
               celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
368
2.25M
               m = MULT16_32_Q15(coef0, tmp);
369
2.25M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp));
370
2.25M
            }
371
3.73k
         } else
372
13.9k
         {
373
3.44M
            for (j=0;j<N;j++)
374
3.42M
            {
375
3.42M
               celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
376
3.42M
               m = MULT16_32_Q15(coef0, tmp);
377
3.42M
               y[j*C] = SIG2RES(tmp);
378
3.42M
            }
379
13.9k
         }
380
17.7k
      }
381
141k
      mem[c] = m;
382
383
141k
      if (apply_downsampling)
384
124k
      {
385
         /* Perform down-sampling */
386
124k
         if (accum)
387
15.4k
         {
388
6.02M
            for (j=0;j<Nd;j++)
389
6.00M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample]));
390
15.4k
         } else
391
108k
         {
392
18.2M
            for (j=0;j<Nd;j++)
393
18.1M
               y[j*C] = SIG2RES(scratch[j*downsample]);
394
108k
         }
395
124k
      }
396
141k
   } while (++c<C);
397
94.8k
   RESTORE_STACK;
398
94.8k
}
celt_decoder.c:deemphasis
Line
Count
Source
310
114k
{
311
114k
   int c;
312
114k
   int Nd;
313
114k
   int apply_downsampling=0;
314
114k
   opus_val16 coef0;
315
114k
   VARDECL(celt_sig, scratch);
316
114k
   SAVE_STACK;
317
114k
#if !defined(CUSTOM_MODES) && !defined(ENABLE_OPUS_CUSTOM_API) && !defined(ENABLE_QEXT)
318
   /* Short version for common case. */
319
114k
   if (downsample == 1 && C == 2 && !accum)
320
13.4k
   {
321
13.4k
      deemphasis_stereo_simple(in, pcm, N, coef[0], mem);
322
13.4k
      return;
323
13.4k
   }
324
100k
#endif
325
100k
   ALLOC(scratch, N, celt_sig);
326
100k
   coef0 = coef[0];
327
100k
   Nd = N/downsample;
328
148k
   c=0; do {
329
148k
      int j;
330
148k
      celt_sig * OPUS_RESTRICT x;
331
148k
      opus_res  * OPUS_RESTRICT y;
332
148k
      celt_sig m = mem[c];
333
148k
      x =in[c];
334
148k
      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
148k
      if (downsample>1)
352
129k
      {
353
         /* Shortcut for the standard (non-custom modes) case */
354
42.9M
         for (j=0;j<N;j++)
355
42.8M
         {
356
42.8M
            celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
357
42.8M
            m = MULT16_32_Q15(coef0, tmp);
358
42.8M
            scratch[j] = tmp;
359
42.8M
         }
360
129k
         apply_downsampling=1;
361
129k
      } else {
362
         /* Shortcut for the standard (non-custom modes) case */
363
19.4k
         if (accum)
364
8.71k
         {
365
4.59M
            for (j=0;j<N;j++)
366
4.58M
            {
367
4.58M
               celt_sig tmp = SATURATE(x[j] + m + VERY_SMALL, SIG_SAT);
368
4.58M
               m = MULT16_32_Q15(coef0, tmp);
369
4.58M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(tmp));
370
4.58M
            }
371
8.71k
         } else
372
10.7k
         {
373
3.52M
            for (j=0;j<N;j++)
374
3.51M
            {
375
3.51M
               celt_sig tmp = SATURATE(x[j] + VERY_SMALL + m, SIG_SAT);
376
3.51M
               m = MULT16_32_Q15(coef0, tmp);
377
3.51M
               y[j*C] = SIG2RES(tmp);
378
3.51M
            }
379
10.7k
         }
380
19.4k
      }
381
148k
      mem[c] = m;
382
383
148k
      if (apply_downsampling)
384
129k
      {
385
         /* Perform down-sampling */
386
129k
         if (accum)
387
22.7k
         {
388
3.63M
            for (j=0;j<Nd;j++)
389
3.61M
               y[j*C] = ADD_RES(y[j*C], SIG2RES(scratch[j*downsample]));
390
22.7k
         } else
391
106k
         {
392
10.5M
            for (j=0;j<Nd;j++)
393
10.4M
               y[j*C] = SIG2RES(scratch[j*downsample]);
394
106k
         }
395
129k
      }
396
148k
   } while (++c<C);
397
100k
   RESTORE_STACK;
398
100k
}
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
330k
{
408
330k
   int c, i;
409
330k
   int M;
410
330k
   int b;
411
330k
   int B;
412
330k
   int N, NB;
413
330k
   int shift;
414
330k
   int nbEBands;
415
330k
   int overlap;
416
330k
   VARDECL(celt_sig, freq);
417
330k
   SAVE_STACK;
418
419
330k
   overlap = mode->overlap;
420
330k
   nbEBands = mode->nbEBands;
421
330k
   N = mode->shortMdctSize<<LM;
422
330k
   ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
423
330k
   M = 1<<LM;
424
#ifdef ENABLE_QEXT
425
156k
   if (mode->Fs != 96000) qext_end=2;
426
#endif
427
428
330k
   if (isTransient)
429
25.6k
   {
430
25.6k
      B = M;
431
25.6k
      NB = mode->shortMdctSize;
432
25.6k
      shift = mode->maxLM;
433
305k
   } else {
434
305k
      B = 1;
435
305k
      NB = mode->shortMdctSize<<LM;
436
305k
      shift = mode->maxLM-LM;
437
305k
   }
438
439
330k
   if (CC==2&&C==1)
440
100k
   {
441
      /* Copying a mono streams to two channels */
442
100k
      celt_sig *freq2;
443
100k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
444
100k
            downsample, silence);
445
#ifdef ENABLE_QEXT
446
42.6k
      if (qext_mode)
447
733
         denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M,
448
733
                        downsample, silence);
449
#endif
450
      /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */
451
100k
      freq2 = out_syn[1]+overlap/2;
452
100k
      OPUS_COPY(freq2, freq, N);
453
221k
      for (b=0;b<B;b++)
454
120k
         clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
455
221k
      for (b=0;b<B;b++)
456
120k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
457
229k
   } else if (CC==1&&C==2)
458
61.7k
   {
459
      /* Downmixing a stereo stream to mono */
460
61.7k
      celt_sig *freq2;
461
61.7k
      freq2 = out_syn[0]+overlap/2;
462
61.7k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
463
61.7k
            downsample, silence);
464
      /* Use the output buffer as temp array before downmixing. */
465
61.7k
      denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
466
61.7k
            downsample, silence);
467
#ifdef ENABLE_QEXT
468
31.3k
      if (qext_mode)
469
4.37k
      {
470
4.37k
         denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M,
471
4.37k
                        downsample, silence);
472
4.37k
         denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M,
473
4.37k
                        downsample, silence);
474
4.37k
      }
475
#endif
476
26.0M
      for (i=0;i<N;i++)
477
25.9M
         freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
478
150k
      for (b=0;b<B;b++)
479
89.0k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
480
168k
   } else {
481
      /* Normal case (mono or stereo) */
482
227k
      c=0; do {
483
227k
         denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
484
227k
               downsample, silence);
485
#ifdef ENABLE_QEXT
486
107k
         if (qext_mode)
487
4.33k
            denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M,
488
4.33k
                           downsample, silence);
489
#endif
490
504k
         for (b=0;b<B;b++)
491
277k
            clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
492
227k
      } while (++c<CC);
493
168k
   }
494
   /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
495
      or in the */
496
490k
   c=0; do {
497
174M
      for (i=0;i<N;i++)
498
173M
         out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT);
499
490k
   } while (++c<CC);
500
330k
   RESTORE_STACK;
501
330k
}
celt_decoder.c:celt_synthesis
Line
Count
Source
407
93.6k
{
408
93.6k
   int c, i;
409
93.6k
   int M;
410
93.6k
   int b;
411
93.6k
   int B;
412
93.6k
   int N, NB;
413
93.6k
   int shift;
414
93.6k
   int nbEBands;
415
93.6k
   int overlap;
416
93.6k
   VARDECL(celt_sig, freq);
417
93.6k
   SAVE_STACK;
418
419
93.6k
   overlap = mode->overlap;
420
93.6k
   nbEBands = mode->nbEBands;
421
93.6k
   N = mode->shortMdctSize<<LM;
422
93.6k
   ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
423
93.6k
   M = 1<<LM;
424
#ifdef ENABLE_QEXT
425
   if (mode->Fs != 96000) qext_end=2;
426
#endif
427
428
93.6k
   if (isTransient)
429
7.90k
   {
430
7.90k
      B = M;
431
7.90k
      NB = mode->shortMdctSize;
432
7.90k
      shift = mode->maxLM;
433
85.7k
   } else {
434
85.7k
      B = 1;
435
85.7k
      NB = mode->shortMdctSize<<LM;
436
85.7k
      shift = mode->maxLM-LM;
437
85.7k
   }
438
439
93.6k
   if (CC==2&&C==1)
440
34.3k
   {
441
      /* Copying a mono streams to two channels */
442
34.3k
      celt_sig *freq2;
443
34.3k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
444
34.3k
            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
34.3k
      freq2 = out_syn[1]+overlap/2;
452
34.3k
      OPUS_COPY(freq2, freq, N);
453
75.8k
      for (b=0;b<B;b++)
454
41.4k
         clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
455
75.8k
      for (b=0;b<B;b++)
456
41.4k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
457
59.2k
   } else if (CC==1&&C==2)
458
16.7k
   {
459
      /* Downmixing a stereo stream to mono */
460
16.7k
      celt_sig *freq2;
461
16.7k
      freq2 = out_syn[0]+overlap/2;
462
16.7k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
463
16.7k
            downsample, silence);
464
      /* Use the output buffer as temp array before downmixing. */
465
16.7k
      denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
466
16.7k
            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.06M
      for (i=0;i<N;i++)
477
6.04M
         freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
478
42.4k
      for (b=0;b<B;b++)
479
25.6k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
480
42.5k
   } else {
481
      /* Normal case (mono or stereo) */
482
59.7k
      c=0; do {
483
59.7k
         denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
484
59.7k
               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
59.7k
      } while (++c<CC);
493
42.5k
   }
494
   /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
495
      or in the */
496
145k
   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
145k
   } while (++c<CC);
500
93.6k
   RESTORE_STACK;
501
93.6k
}
celt_decoder.c:celt_synthesis
Line
Count
Source
407
87.9k
{
408
87.9k
   int c, i;
409
87.9k
   int M;
410
87.9k
   int b;
411
87.9k
   int B;
412
87.9k
   int N, NB;
413
87.9k
   int shift;
414
87.9k
   int nbEBands;
415
87.9k
   int overlap;
416
87.9k
   VARDECL(celt_sig, freq);
417
87.9k
   SAVE_STACK;
418
419
87.9k
   overlap = mode->overlap;
420
87.9k
   nbEBands = mode->nbEBands;
421
87.9k
   N = mode->shortMdctSize<<LM;
422
87.9k
   ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
423
87.9k
   M = 1<<LM;
424
87.9k
#ifdef ENABLE_QEXT
425
87.9k
   if (mode->Fs != 96000) qext_end=2;
426
87.9k
#endif
427
428
87.9k
   if (isTransient)
429
5.75k
   {
430
5.75k
      B = M;
431
5.75k
      NB = mode->shortMdctSize;
432
5.75k
      shift = mode->maxLM;
433
82.1k
   } else {
434
82.1k
      B = 1;
435
82.1k
      NB = mode->shortMdctSize<<LM;
436
82.1k
      shift = mode->maxLM-LM;
437
82.1k
   }
438
439
87.9k
   if (CC==2&&C==1)
440
20.7k
   {
441
      /* Copying a mono streams to two channels */
442
20.7k
      celt_sig *freq2;
443
20.7k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
444
20.7k
            downsample, silence);
445
20.7k
#ifdef ENABLE_QEXT
446
20.7k
      if (qext_mode)
447
337
         denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M,
448
337
                        downsample, silence);
449
20.7k
#endif
450
      /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */
451
20.7k
      freq2 = out_syn[1]+overlap/2;
452
20.7k
      OPUS_COPY(freq2, freq, N);
453
44.6k
      for (b=0;b<B;b++)
454
23.9k
         clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
455
44.6k
      for (b=0;b<B;b++)
456
23.9k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
457
67.1k
   } else if (CC==1&&C==2)
458
17.9k
   {
459
      /* Downmixing a stereo stream to mono */
460
17.9k
      celt_sig *freq2;
461
17.9k
      freq2 = out_syn[0]+overlap/2;
462
17.9k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
463
17.9k
            downsample, silence);
464
      /* Use the output buffer as temp array before downmixing. */
465
17.9k
      denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
466
17.9k
            downsample, silence);
467
17.9k
#ifdef ENABLE_QEXT
468
17.9k
      if (qext_mode)
469
3.10k
      {
470
3.10k
         denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M,
471
3.10k
                        downsample, silence);
472
3.10k
         denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M,
473
3.10k
                        downsample, silence);
474
3.10k
      }
475
17.9k
#endif
476
7.79M
      for (i=0;i<N;i++)
477
7.77M
         freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
478
42.0k
      for (b=0;b<B;b++)
479
24.0k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
480
49.2k
   } else {
481
      /* Normal case (mono or stereo) */
482
63.9k
      c=0; do {
483
63.9k
         denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
484
63.9k
               downsample, silence);
485
63.9k
#ifdef ENABLE_QEXT
486
63.9k
         if (qext_mode)
487
1.79k
            denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M,
488
1.79k
                           downsample, silence);
489
63.9k
#endif
490
140k
         for (b=0;b<B;b++)
491
76.0k
            clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
492
63.9k
      } while (++c<CC);
493
49.2k
   }
494
   /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
495
      or in the */
496
123k
   c=0; do {
497
47.6M
      for (i=0;i<N;i++)
498
47.5M
         out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT);
499
123k
   } while (++c<CC);
500
87.9k
   RESTORE_STACK;
501
87.9k
}
celt_decoder.c:celt_synthesis
Line
Count
Source
407
68.9k
{
408
68.9k
   int c, i;
409
68.9k
   int M;
410
68.9k
   int b;
411
68.9k
   int B;
412
68.9k
   int N, NB;
413
68.9k
   int shift;
414
68.9k
   int nbEBands;
415
68.9k
   int overlap;
416
68.9k
   VARDECL(celt_sig, freq);
417
68.9k
   SAVE_STACK;
418
419
68.9k
   overlap = mode->overlap;
420
68.9k
   nbEBands = mode->nbEBands;
421
68.9k
   N = mode->shortMdctSize<<LM;
422
68.9k
   ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
423
68.9k
   M = 1<<LM;
424
68.9k
#ifdef ENABLE_QEXT
425
68.9k
   if (mode->Fs != 96000) qext_end=2;
426
68.9k
#endif
427
428
68.9k
   if (isTransient)
429
5.49k
   {
430
5.49k
      B = M;
431
5.49k
      NB = mode->shortMdctSize;
432
5.49k
      shift = mode->maxLM;
433
63.4k
   } else {
434
63.4k
      B = 1;
435
63.4k
      NB = mode->shortMdctSize<<LM;
436
63.4k
      shift = mode->maxLM-LM;
437
63.4k
   }
438
439
68.9k
   if (CC==2&&C==1)
440
21.8k
   {
441
      /* Copying a mono streams to two channels */
442
21.8k
      celt_sig *freq2;
443
21.8k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
444
21.8k
            downsample, silence);
445
21.8k
#ifdef ENABLE_QEXT
446
21.8k
      if (qext_mode)
447
396
         denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M,
448
396
                        downsample, silence);
449
21.8k
#endif
450
      /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */
451
21.8k
      freq2 = out_syn[1]+overlap/2;
452
21.8k
      OPUS_COPY(freq2, freq, N);
453
46.1k
      for (b=0;b<B;b++)
454
24.2k
         clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
455
46.1k
      for (b=0;b<B;b++)
456
24.2k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
457
47.0k
   } else if (CC==1&&C==2)
458
13.4k
   {
459
      /* Downmixing a stereo stream to mono */
460
13.4k
      celt_sig *freq2;
461
13.4k
      freq2 = out_syn[0]+overlap/2;
462
13.4k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
463
13.4k
            downsample, silence);
464
      /* Use the output buffer as temp array before downmixing. */
465
13.4k
      denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
466
13.4k
            downsample, silence);
467
13.4k
#ifdef ENABLE_QEXT
468
13.4k
      if (qext_mode)
469
1.26k
      {
470
1.26k
         denormalise_bands(qext_mode, X, freq, qext_bandLogE, 0, qext_end, M,
471
1.26k
                        downsample, silence);
472
1.26k
         denormalise_bands(qext_mode, X+N, freq2, qext_bandLogE+NB_QEXT_BANDS, 0, qext_end, M,
473
1.26k
                        downsample, silence);
474
1.26k
      }
475
13.4k
#endif
476
6.52M
      for (i=0;i<N;i++)
477
6.50M
         freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
478
32.6k
      for (b=0;b<B;b++)
479
19.2k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
480
33.6k
   } else {
481
      /* Normal case (mono or stereo) */
482
43.4k
      c=0; do {
483
43.4k
         denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
484
43.4k
               downsample, silence);
485
43.4k
#ifdef ENABLE_QEXT
486
43.4k
         if (qext_mode)
487
2.54k
            denormalise_bands(qext_mode, X+c*N, freq, qext_bandLogE+c*NB_QEXT_BANDS, 0, qext_end, M,
488
2.54k
                           downsample, silence);
489
43.4k
#endif
490
95.1k
         for (b=0;b<B;b++)
491
51.7k
            clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
492
43.4k
      } while (++c<CC);
493
33.6k
   }
494
   /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
495
      or in the */
496
100k
   c=0; do {
497
39.1M
      for (i=0;i<N;i++)
498
39.0M
         out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT);
499
100k
   } while (++c<CC);
500
68.9k
   RESTORE_STACK;
501
68.9k
}
celt_decoder.c:celt_synthesis
Line
Count
Source
407
80.1k
{
408
80.1k
   int c, i;
409
80.1k
   int M;
410
80.1k
   int b;
411
80.1k
   int B;
412
80.1k
   int N, NB;
413
80.1k
   int shift;
414
80.1k
   int nbEBands;
415
80.1k
   int overlap;
416
80.1k
   VARDECL(celt_sig, freq);
417
80.1k
   SAVE_STACK;
418
419
80.1k
   overlap = mode->overlap;
420
80.1k
   nbEBands = mode->nbEBands;
421
80.1k
   N = mode->shortMdctSize<<LM;
422
80.1k
   ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
423
80.1k
   M = 1<<LM;
424
#ifdef ENABLE_QEXT
425
   if (mode->Fs != 96000) qext_end=2;
426
#endif
427
428
80.1k
   if (isTransient)
429
6.52k
   {
430
6.52k
      B = M;
431
6.52k
      NB = mode->shortMdctSize;
432
6.52k
      shift = mode->maxLM;
433
73.5k
   } else {
434
73.5k
      B = 1;
435
73.5k
      NB = mode->shortMdctSize<<LM;
436
73.5k
      shift = mode->maxLM-LM;
437
73.5k
   }
438
439
80.1k
   if (CC==2&&C==1)
440
23.8k
   {
441
      /* Copying a mono streams to two channels */
442
23.8k
      celt_sig *freq2;
443
23.8k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
444
23.8k
            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.8k
      freq2 = out_syn[1]+overlap/2;
452
23.8k
      OPUS_COPY(freq2, freq, N);
453
54.4k
      for (b=0;b<B;b++)
454
30.6k
         clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
455
54.4k
      for (b=0;b<B;b++)
456
30.6k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
457
56.2k
   } else if (CC==1&&C==2)
458
13.6k
   {
459
      /* Downmixing a stereo stream to mono */
460
13.6k
      celt_sig *freq2;
461
13.6k
      freq2 = out_syn[0]+overlap/2;
462
13.6k
      denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
463
13.6k
            downsample, silence);
464
      /* Use the output buffer as temp array before downmixing. */
465
13.6k
      denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
466
13.6k
            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.62M
      for (i=0;i<N;i++)
477
5.61M
         freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
478
33.6k
      for (b=0;b<B;b++)
479
20.0k
         clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
480
42.5k
   } else {
481
      /* Normal case (mono or stereo) */
482
60.2k
      c=0; do {
483
60.2k
         denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
484
60.2k
               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
133k
         for (b=0;b<B;b++)
491
72.8k
            clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
492
60.2k
      } while (++c<CC);
493
42.5k
   }
494
   /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
495
      or in the */
496
121k
   c=0; do {
497
43.7M
      for (i=0;i<N;i++)
498
43.6M
         out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT);
499
121k
   } while (++c<CC);
500
80.1k
   RESTORE_STACK;
501
80.1k
}
502
503
static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
504
294k
{
505
294k
   int i, curr, tf_select;
506
294k
   int tf_select_rsv;
507
294k
   int tf_changed;
508
294k
   int logp;
509
294k
   opus_uint32 budget;
510
294k
   opus_uint32 tell;
511
512
294k
   budget = dec->storage*8;
513
294k
   tell = ec_tell(dec);
514
294k
   logp = isTransient ? 2 : 4;
515
294k
   tf_select_rsv = LM>0 && tell+logp+1<=budget;
516
294k
   budget -= tf_select_rsv;
517
294k
   tf_changed = curr = 0;
518
5.06M
   for (i=start;i<end;i++)
519
4.77M
   {
520
4.77M
      if (tell+logp<=budget)
521
1.84M
      {
522
1.84M
         curr ^= ec_dec_bit_logp(dec, logp);
523
1.84M
         tell = ec_tell(dec);
524
1.84M
         tf_changed |= curr;
525
1.84M
      }
526
4.77M
      tf_res[i] = curr;
527
4.77M
      logp = isTransient ? 4 : 5;
528
4.77M
   }
529
294k
   tf_select = 0;
530
294k
   if (tf_select_rsv &&
531
92.4k
     tf_select_table[LM][4*isTransient+0+tf_changed] !=
532
92.4k
     tf_select_table[LM][4*isTransient+2+tf_changed])
533
31.7k
   {
534
31.7k
      tf_select = ec_dec_bit_logp(dec, 1);
535
31.7k
   }
536
5.06M
   for (i=start;i<end;i++)
537
4.77M
   {
538
4.77M
      tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
539
4.77M
   }
540
294k
}
541
542
static int celt_plc_pitch_search(CELTDecoder *st, celt_sig *decode_mem[2], int C, int arch)
543
94.8k
{
544
94.8k
   int pitch_index;
545
#ifdef ENABLE_QEXT
546
   int qext_scale;
547
#endif
548
94.8k
   VARDECL( opus_val16, lp_pitch_buf );
549
94.8k
   SAVE_STACK;
550
#ifdef ENABLE_QEXT
551
   qext_scale = st->qext_scale;
552
#endif
553
94.8k
   ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 );
554
94.8k
   pitch_downsample(decode_mem, lp_pitch_buf,
555
94.8k
         DECODE_BUFFER_SIZE>>1, C, QEXT_SCALE(2), arch);
556
94.8k
   pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf,
557
94.8k
         DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX,
558
94.8k
         PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, arch);
559
94.8k
   pitch_index = PLC_PITCH_LAG_MAX-pitch_index;
560
94.8k
   RESTORE_STACK;
561
94.8k
   return QEXT_SCALE(pitch_index);
562
94.8k
}
563
564
static void prefilter_and_fold(CELTDecoder * OPUS_RESTRICT st, int N)
565
119k
{
566
119k
   int c;
567
119k
   int CC;
568
119k
   int i;
569
119k
   int overlap;
570
119k
   celt_sig *decode_mem[2];
571
119k
   const OpusCustomMode *mode;
572
119k
   int decode_buffer_size;
573
#ifdef ENABLE_QEXT
574
   int qext_scale;
575
#endif
576
119k
   VARDECL(opus_val32, etmp);
577
119k
   SAVE_STACK
578
#ifdef ENABLE_QEXT
579
   qext_scale = st->qext_scale;
580
#endif
581
119k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
582
119k
   mode = st->mode;
583
119k
   overlap = st->overlap;
584
119k
   CC = st->channels;
585
119k
   ALLOC(etmp, overlap, opus_val32);
586
192k
   c=0; do {
587
192k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
588
192k
   } while (++c<CC);
589
590
192k
   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
192k
      comb_filter(etmp, decode_mem[c]+decode_buffer_size-N,
595
192k
         st->postfilter_period_old, st->postfilter_period, overlap,
596
192k
         -st->postfilter_gain_old, -st->postfilter_gain,
597
192k
         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
12.9M
      for (i=0;i<overlap/2;i++)
602
12.7M
      {
603
12.7M
         decode_mem[c][decode_buffer_size-N+i] =
604
12.7M
            MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i])
605
12.7M
            + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i]);
606
12.7M
      }
607
192k
   } while (++c<CC);
608
119k
   RESTORE_STACK;
609
119k
}
celt_decoder.c:prefilter_and_fold
Line
Count
Source
565
59.8k
{
566
59.8k
   int c;
567
59.8k
   int CC;
568
59.8k
   int i;
569
59.8k
   int overlap;
570
59.8k
   celt_sig *decode_mem[2];
571
59.8k
   const OpusCustomMode *mode;
572
59.8k
   int decode_buffer_size;
573
#ifdef ENABLE_QEXT
574
   int qext_scale;
575
#endif
576
59.8k
   VARDECL(opus_val32, etmp);
577
59.8k
   SAVE_STACK
578
#ifdef ENABLE_QEXT
579
   qext_scale = st->qext_scale;
580
#endif
581
59.8k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
582
59.8k
   mode = st->mode;
583
59.8k
   overlap = st->overlap;
584
59.8k
   CC = st->channels;
585
59.8k
   ALLOC(etmp, overlap, opus_val32);
586
96.0k
   c=0; do {
587
96.0k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
588
96.0k
   } while (++c<CC);
589
590
96.0k
   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
96.0k
      comb_filter(etmp, decode_mem[c]+decode_buffer_size-N,
595
96.0k
         st->postfilter_period_old, st->postfilter_period, overlap,
596
96.0k
         -st->postfilter_gain_old, -st->postfilter_gain,
597
96.0k
         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.48M
      for (i=0;i<overlap/2;i++)
602
6.39M
      {
603
6.39M
         decode_mem[c][decode_buffer_size-N+i] =
604
6.39M
            MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i])
605
6.39M
            + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i]);
606
6.39M
      }
607
96.0k
   } while (++c<CC);
608
59.8k
   RESTORE_STACK;
609
59.8k
}
celt_decoder.c:prefilter_and_fold
Line
Count
Source
565
59.8k
{
566
59.8k
   int c;
567
59.8k
   int CC;
568
59.8k
   int i;
569
59.8k
   int overlap;
570
59.8k
   celt_sig *decode_mem[2];
571
59.8k
   const OpusCustomMode *mode;
572
59.8k
   int decode_buffer_size;
573
59.8k
#ifdef ENABLE_QEXT
574
59.8k
   int qext_scale;
575
59.8k
#endif
576
59.8k
   VARDECL(opus_val32, etmp);
577
59.8k
   SAVE_STACK
578
59.8k
#ifdef ENABLE_QEXT
579
59.8k
   qext_scale = st->qext_scale;
580
59.8k
#endif
581
59.8k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
582
59.8k
   mode = st->mode;
583
59.8k
   overlap = st->overlap;
584
59.8k
   CC = st->channels;
585
59.8k
   ALLOC(etmp, overlap, opus_val32);
586
96.0k
   c=0; do {
587
96.0k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
588
96.0k
   } while (++c<CC);
589
590
96.0k
   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
96.0k
      comb_filter(etmp, decode_mem[c]+decode_buffer_size-N,
595
96.0k
         st->postfilter_period_old, st->postfilter_period, overlap,
596
96.0k
         -st->postfilter_gain_old, -st->postfilter_gain,
597
96.0k
         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.48M
      for (i=0;i<overlap/2;i++)
602
6.39M
      {
603
6.39M
         decode_mem[c][decode_buffer_size-N+i] =
604
6.39M
            MULT16_32_Q15(COEF2VAL16(mode->window[i]), etmp[overlap-1-i])
605
6.39M
            + MULT16_32_Q15 (COEF2VAL16(mode->window[overlap-i-1]), etmp[i]);
606
6.39M
      }
607
96.0k
   } while (++c<CC);
608
59.8k
   RESTORE_STACK;
609
59.8k
}
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
273k
{
669
273k
   int c;
670
273k
   int i;
671
273k
   const int C = st->channels;
672
273k
   celt_sig *decode_mem[2];
673
273k
   celt_sig *out_syn[2];
674
273k
   opus_val16 *lpc;
675
273k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
676
273k
   const OpusCustomMode *mode;
677
273k
   int nbEBands;
678
273k
   int overlap;
679
273k
   int start;
680
273k
   int loss_duration;
681
273k
   int noise_based;
682
273k
   const opus_int16 *eBands;
683
273k
   int decode_buffer_size;
684
273k
   int max_period;
685
#ifdef ENABLE_QEXT
686
   int qext_scale;
687
#endif
688
273k
   SAVE_STACK;
689
#ifdef ENABLE_QEXT
690
   qext_scale = st->qext_scale;
691
#endif
692
273k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
693
273k
   max_period = QEXT_SCALE(MAX_PERIOD);
694
273k
   mode = st->mode;
695
273k
   nbEBands = mode->nbEBands;
696
273k
   overlap = mode->overlap;
697
273k
   eBands = mode->eBands;
698
699
420k
   c=0; do {
700
420k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
701
420k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
702
420k
   } while (++c<C);
703
273k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C);
704
273k
   oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER);
705
273k
   oldLogE = oldBandE + 2*nbEBands;
706
273k
   oldLogE2 = oldLogE + 2*nbEBands;
707
273k
   backgroundLogE = oldLogE2  + 2*nbEBands;
708
709
273k
   loss_duration = st->loss_duration;
710
273k
   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
273k
   noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
716
273k
   if (noise_based)
717
53.6k
   {
718
      /* Noise-based PLC/CNG */
719
53.6k
      VARDECL(celt_norm, X);
720
53.6k
      opus_uint32 seed;
721
53.6k
      int end;
722
53.6k
      int effEnd;
723
53.6k
      celt_glog decay;
724
53.6k
      end = st->end;
725
53.6k
      effEnd = IMAX(start, IMIN(end, mode->effEBands));
726
727
53.6k
      ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
728
76.8k
      c=0; do {
729
76.8k
         OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
730
76.8k
               decode_buffer_size-N+overlap);
731
76.8k
      } while (++c<C);
732
733
53.6k
      if (st->prefilter_and_fold) {
734
877
         prefilter_and_fold(st, N);
735
877
      }
736
737
      /* Energy decay */
738
53.6k
      decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f);
739
53.6k
      c=0; do
740
76.8k
      {
741
366k
         for (i=start;i<end;i++)
742
289k
            oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
743
76.8k
      } while (++c<C);
744
53.6k
      seed = st->rng;
745
130k
      for (c=0;c<C;c++)
746
76.8k
      {
747
366k
         for (i=start;i<effEnd;i++)
748
289k
         {
749
289k
            int j;
750
289k
            int boffs;
751
289k
            int blen;
752
289k
            boffs = N*c+(eBands[i]<<LM);
753
289k
            blen = (eBands[i+1]-eBands[i])<<LM;
754
14.6M
            for (j=0;j<blen;j++)
755
14.3M
            {
756
14.3M
               seed = celt_lcg_rand(seed);
757
14.3M
               X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14);
758
14.3M
            }
759
289k
            renormalise_vector(X+boffs, blen, Q31ONE, st->arch);
760
289k
         }
761
76.8k
      }
762
53.6k
      st->rng = seed;
763
764
53.6k
      celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch ARG_QEXT(NULL) ARG_QEXT(NULL) ARG_QEXT(0));
765
53.6k
      st->prefilter_and_fold = 0;
766
      /* Skip regular PLC until we get two consecutive packets. */
767
53.6k
      st->skip_plc = 1;
768
219k
   } else {
769
219k
      int exc_length;
770
      /* Pitch-based PLC */
771
219k
      const celt_coef *window;
772
219k
      opus_val16 *exc;
773
219k
      opus_val16 fade = Q15ONE;
774
219k
      int pitch_index;
775
219k
      VARDECL(opus_val16, _exc);
776
219k
      VARDECL(opus_val16, fir_tmp);
777
778
219k
      if (loss_duration == 0)
779
130k
      {
780
#ifdef ENABLE_DEEP_PLC
781
        if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
782
#endif
783
130k
         st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch);
784
130k
      } else {
785
88.7k
         pitch_index = st->last_pitch_index;
786
88.7k
         fade = QCONST16(.8f,15);
787
88.7k
      }
788
789
      /* We want the excitation for 2 pitch periods in order to look for a
790
         decaying signal, but we can't get more than MAX_PERIOD. */
791
219k
      exc_length = IMIN(2*pitch_index, max_period);
792
793
219k
      ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16);
794
219k
      ALLOC(fir_tmp, exc_length, opus_val16);
795
219k
      exc = _exc+CELT_LPC_ORDER;
796
219k
      window = mode->window;
797
343k
      c=0; do {
798
343k
         opus_val16 decay;
799
343k
         opus_val16 attenuation;
800
343k
         opus_val32 S1=0;
801
343k
         celt_sig *buf;
802
343k
         int extrapolation_offset;
803
343k
         int extrapolation_len;
804
343k
         int j;
805
806
343k
         buf = decode_mem[c];
807
400M
         for (i=0;i<max_period+CELT_LPC_ORDER;i++)
808
400M
            exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT);
809
810
343k
         if (loss_duration == 0)
811
203k
         {
812
203k
            opus_val32 ac[CELT_LPC_ORDER+1];
813
            /* Compute LPC coefficients for the last MAX_PERIOD samples before
814
               the first loss so we can work in the excitation-filter domain. */
815
203k
            _celt_autocorr(exc, ac, window, overlap,
816
203k
                   CELT_LPC_ORDER, max_period, st->arch);
817
            /* Add a noise floor of -40 dB. */
818
#ifdef FIXED_POINT
819
89.3k
            ac[0] += SHR32(ac[0],13);
820
#else
821
            ac[0] *= 1.0001f;
822
#endif
823
            /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
824
5.07M
            for (i=1;i<=CELT_LPC_ORDER;i++)
825
4.87M
            {
826
               /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
827
#ifdef FIXED_POINT
828
2.14M
               ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
829
#else
830
               ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
831
#endif
832
4.87M
            }
833
203k
            _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER);
834
#ifdef FIXED_POINT
835
         /* For fixed-point, apply bandwidth expansion until we can guarantee that
836
            no overflow can happen in the IIR filter. This means:
837
            32768*sum(abs(filter)) < 2^31 */
838
114k
         while (1) {
839
114k
            opus_val16 tmp=Q15ONE;
840
114k
            opus_val32 sum=QCONST16(1., SIG_SHIFT);
841
2.87M
            for (i=0;i<CELT_LPC_ORDER;i++)
842
2.75M
               sum += ABS16(lpc[c*CELT_LPC_ORDER+i]);
843
114k
            if (sum < 65535) break;
844
637k
            for (i=0;i<CELT_LPC_ORDER;i++)
845
612k
            {
846
612k
               tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
847
612k
               lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp);
848
612k
            }
849
25.5k
         }
850
#endif
851
203k
         }
852
         /* Initialize the LPC history with the samples just before the start
853
            of the region for which we're computing the excitation. */
854
343k
         {
855
            /* Compute the excitation for exc_length samples before the loss. We need the copy
856
               because celt_fir() cannot filter in-place. */
857
343k
            celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER,
858
343k
                  fir_tmp, exc_length, CELT_LPC_ORDER, st->arch);
859
343k
            OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length);
860
343k
         }
861
862
         /* Check if the waveform is decaying, and if so how fast.
863
            We do this to avoid adding energy when concealing in a segment
864
            with decaying energy. */
865
343k
         {
866
343k
            opus_val32 E1=1, E2=1;
867
343k
            int decay_length;
868
#ifdef FIXED_POINT
869
153k
            int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20);
870
#ifdef ENABLE_QEXT
871
65.1k
            if (st->qext_scale==2) shift++;
872
#endif
873
#endif
874
343k
            decay_length = exc_length>>1;
875
92.7M
            for (i=0;i<decay_length;i++)
876
92.4M
            {
877
92.4M
               opus_val16 e;
878
92.4M
               e = exc[max_period-decay_length+i];
879
92.4M
               E1 += SHR32(MULT16_16(e, e), shift);
880
92.4M
               e = exc[max_period-2*decay_length+i];
881
92.4M
               E2 += SHR32(MULT16_16(e, e), shift);
882
92.4M
            }
883
343k
            E1 = MIN32(E1, E2);
884
343k
            decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
885
343k
         }
886
887
         /* Move the decoder memory one frame to the left to give us room to
888
            add the data for the new frame. We ignore the overlap that extends
889
            past the end of the buffer, because we aren't going to use it. */
890
343k
         OPUS_MOVE(buf, buf+N, decode_buffer_size-N);
891
892
         /* Extrapolate from the end of the excitation with a period of
893
            "pitch_index", scaling down each period by an additional factor of
894
            "decay". */
895
343k
         extrapolation_offset = max_period-pitch_index;
896
         /* We need to extrapolate enough samples to cover a complete MDCT
897
            window (including overlap/2 samples on both sides). */
898
343k
         extrapolation_len = N+overlap;
899
         /* We also apply fading if this is not the first loss. */
900
343k
         attenuation = MULT16_16_Q15(fade, decay);
901
125M
         for (i=j=0;i<extrapolation_len;i++,j++)
902
124M
         {
903
124M
            opus_val16 tmp;
904
124M
            if (j >= pitch_index) {
905
428k
               j -= pitch_index;
906
428k
               attenuation = MULT16_16_Q15(attenuation, decay);
907
428k
            }
908
124M
            buf[decode_buffer_size-N+i] =
909
124M
                  SHL32(EXTEND32(MULT16_16_Q15(attenuation,
910
124M
                        exc[extrapolation_offset+j])), SIG_SHIFT);
911
            /* Compute the energy of the previously decoded signal whose
912
               excitation we're copying. */
913
124M
            tmp = SROUND16(
914
124M
                  buf[decode_buffer_size-max_period-N+extrapolation_offset+j],
915
124M
                  SIG_SHIFT);
916
124M
            S1 += SHR32(MULT16_16(tmp, tmp), 11);
917
124M
         }
918
343k
         {
919
343k
            opus_val16 lpc_mem[CELT_LPC_ORDER];
920
            /* Copy the last decoded samples (prior to the overlap region) to
921
               synthesis filter memory so we can have a continuous signal. */
922
8.58M
            for (i=0;i<CELT_LPC_ORDER;i++)
923
8.23M
               lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT);
924
            /* Apply the synthesis filter to convert the excitation back into
925
               the signal domain. */
926
343k
            celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER,
927
343k
                  buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER,
928
343k
                  lpc_mem, st->arch);
929
#ifdef FIXED_POINT
930
53.6M
            for (i=0; i < extrapolation_len; i++)
931
53.4M
               buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT);
932
#endif
933
343k
         }
934
935
         /* Check if the synthesis energy is higher than expected, which can
936
            happen with the signal changes during our window. If so,
937
            attenuate. */
938
343k
         {
939
343k
            opus_val32 S2=0;
940
125M
            for (i=0;i<extrapolation_len;i++)
941
124M
            {
942
124M
               opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT);
943
124M
               S2 += SHR32(MULT16_16(tmp, tmp), 11);
944
124M
            }
945
            /* This checks for an "explosion" in the synthesis. */
946
#ifdef FIXED_POINT
947
153k
            if (!(S1 > SHR32(S2,2)))
948
#else
949
            /* The float test is written this way to catch NaNs in the output
950
               of the IIR filter at the same time. */
951
190k
            if (!(S1 > 0.2f*S2))
952
21.7k
#endif
953
111k
            {
954
38.0M
               for (i=0;i<extrapolation_len;i++)
955
37.9M
                  buf[decode_buffer_size-N+i] = 0;
956
231k
            } else if (S1 < S2)
957
65.7k
            {
958
65.7k
               opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
959
8.91M
               for (i=0;i<overlap;i++)
960
8.85M
               {
961
8.85M
                  opus_val16 tmp_g = Q15ONE
962
8.85M
                        - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio);
963
8.85M
                  buf[decode_buffer_size-N+i] =
964
8.85M
                        MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]);
965
8.85M
               }
966
12.9M
               for (i=overlap;i<extrapolation_len;i++)
967
12.8M
               {
968
12.8M
                  buf[decode_buffer_size-N+i] =
969
12.8M
                        MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]);
970
12.8M
               }
971
65.7k
            }
972
343k
         }
973
974
343k
      } while (++c<C);
975
976
#ifdef ENABLE_DEEP_PLC
977
      if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
978
         float overlap_mem;
979
         int samples_needed16k;
980
         celt_sig *buf;
981
         VARDECL(float, buf_copy);
982
         buf = decode_mem[0];
983
         ALLOC(buf_copy, C*overlap, float);
984
         c=0; do {
985
            OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap);
986
         } while (++c<C);
987
988
         /* Need enough samples from the PLC to cover the frame size, resampling delay,
989
            and the overlap at the end. */
990
         samples_needed16k = (N+SINC_ORDER+overlap)/3;
991
         if (loss_duration == 0) {
992
            st->plc_fill = 0;
993
         }
994
         while (st->plc_fill < samples_needed16k) {
995
            lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]);
996
            st->plc_fill += FRAME_SIZE;
997
         }
998
         /* Resample to 48 kHz. */
999
         for (i=0;i<(N+overlap)/3;i++) {
1000
            int j;
1001
            float sum;
1002
            for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j];
1003
            buf[decode_buffer_size-N+3*i] = sum;
1004
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2];
1005
            buf[decode_buffer_size-N+3*i+1] = sum;
1006
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1];
1007
            buf[decode_buffer_size-N+3*i+2] = sum;
1008
         }
1009
         OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3);
1010
         st->plc_fill -= N/3;
1011
         for (i=0;i<N;i++) {
1012
            float tmp = buf[decode_buffer_size-N+i];
1013
            buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem;
1014
            st->plc_preemphasis_mem = tmp;
1015
         }
1016
         overlap_mem = st->plc_preemphasis_mem;
1017
         for (i=0;i<overlap;i++) {
1018
            float tmp = buf[decode_buffer_size+i];
1019
            buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem;
1020
            overlap_mem = tmp;
1021
         }
1022
         /* For now, we just do mono PLC. */
1023
         if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap);
1024
         c=0; do {
1025
            /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */
1026
            if (loss_duration == 0) {
1027
               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];
1028
            }
1029
         } while (++c<C);
1030
      }
1031
#endif
1032
219k
      st->prefilter_and_fold = 1;
1033
219k
   }
1034
1035
   /* Saturate to something large to avoid wrap-around. */
1036
273k
   st->loss_duration = IMIN(10000, loss_duration+(1<<LM));
1037
1038
273k
   RESTORE_STACK;
1039
273k
}
celt_decoder.c:celt_decode_lost
Line
Count
Source
668
64.6k
{
669
64.6k
   int c;
670
64.6k
   int i;
671
64.6k
   const int C = st->channels;
672
64.6k
   celt_sig *decode_mem[2];
673
64.6k
   celt_sig *out_syn[2];
674
64.6k
   opus_val16 *lpc;
675
64.6k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
676
64.6k
   const OpusCustomMode *mode;
677
64.6k
   int nbEBands;
678
64.6k
   int overlap;
679
64.6k
   int start;
680
64.6k
   int loss_duration;
681
64.6k
   int noise_based;
682
64.6k
   const opus_int16 *eBands;
683
64.6k
   int decode_buffer_size;
684
64.6k
   int max_period;
685
#ifdef ENABLE_QEXT
686
   int qext_scale;
687
#endif
688
64.6k
   SAVE_STACK;
689
#ifdef ENABLE_QEXT
690
   qext_scale = st->qext_scale;
691
#endif
692
64.6k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
693
64.6k
   max_period = QEXT_SCALE(MAX_PERIOD);
694
64.6k
   mode = st->mode;
695
64.6k
   nbEBands = mode->nbEBands;
696
64.6k
   overlap = mode->overlap;
697
64.6k
   eBands = mode->eBands;
698
699
105k
   c=0; do {
700
105k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
701
105k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
702
105k
   } while (++c<C);
703
64.6k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C);
704
64.6k
   oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER);
705
64.6k
   oldLogE = oldBandE + 2*nbEBands;
706
64.6k
   oldLogE2 = oldLogE + 2*nbEBands;
707
64.6k
   backgroundLogE = oldLogE2  + 2*nbEBands;
708
709
64.6k
   loss_duration = st->loss_duration;
710
64.6k
   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
64.6k
   noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
716
64.6k
   if (noise_based)
717
12.0k
   {
718
      /* Noise-based PLC/CNG */
719
12.0k
      VARDECL(celt_norm, X);
720
12.0k
      opus_uint32 seed;
721
12.0k
      int end;
722
12.0k
      int effEnd;
723
12.0k
      celt_glog decay;
724
12.0k
      end = st->end;
725
12.0k
      effEnd = IMAX(start, IMIN(end, mode->effEBands));
726
727
12.0k
      ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
728
17.3k
      c=0; do {
729
17.3k
         OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
730
17.3k
               decode_buffer_size-N+overlap);
731
17.3k
      } while (++c<C);
732
733
12.0k
      if (st->prefilter_and_fold) {
734
264
         prefilter_and_fold(st, N);
735
264
      }
736
737
      /* Energy decay */
738
12.0k
      decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f);
739
12.0k
      c=0; do
740
17.3k
      {
741
84.0k
         for (i=start;i<end;i++)
742
66.6k
            oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
743
17.3k
      } while (++c<C);
744
12.0k
      seed = st->rng;
745
29.4k
      for (c=0;c<C;c++)
746
17.3k
      {
747
84.0k
         for (i=start;i<effEnd;i++)
748
66.6k
         {
749
66.6k
            int j;
750
66.6k
            int boffs;
751
66.6k
            int blen;
752
66.6k
            boffs = N*c+(eBands[i]<<LM);
753
66.6k
            blen = (eBands[i+1]-eBands[i])<<LM;
754
3.07M
            for (j=0;j<blen;j++)
755
3.00M
            {
756
3.00M
               seed = celt_lcg_rand(seed);
757
3.00M
               X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14);
758
3.00M
            }
759
66.6k
            renormalise_vector(X+boffs, blen, Q31ONE, st->arch);
760
66.6k
         }
761
17.3k
      }
762
12.0k
      st->rng = seed;
763
764
12.0k
      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
12.0k
      st->prefilter_and_fold = 0;
766
      /* Skip regular PLC until we get two consecutive packets. */
767
12.0k
      st->skip_plc = 1;
768
52.6k
   } else {
769
52.6k
      int exc_length;
770
      /* Pitch-based PLC */
771
52.6k
      const celt_coef *window;
772
52.6k
      opus_val16 *exc;
773
52.6k
      opus_val16 fade = Q15ONE;
774
52.6k
      int pitch_index;
775
52.6k
      VARDECL(opus_val16, _exc);
776
52.6k
      VARDECL(opus_val16, fir_tmp);
777
778
52.6k
      if (loss_duration == 0)
779
31.2k
      {
780
#ifdef ENABLE_DEEP_PLC
781
        if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
782
#endif
783
31.2k
         st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch);
784
31.2k
      } else {
785
21.3k
         pitch_index = st->last_pitch_index;
786
21.3k
         fade = QCONST16(.8f,15);
787
21.3k
      }
788
789
      /* We want the excitation for 2 pitch periods in order to look for a
790
         decaying signal, but we can't get more than MAX_PERIOD. */
791
52.6k
      exc_length = IMIN(2*pitch_index, max_period);
792
793
52.6k
      ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16);
794
52.6k
      ALLOC(fir_tmp, exc_length, opus_val16);
795
52.6k
      exc = _exc+CELT_LPC_ORDER;
796
52.6k
      window = mode->window;
797
87.8k
      c=0; do {
798
87.8k
         opus_val16 decay;
799
87.8k
         opus_val16 attenuation;
800
87.8k
         opus_val32 S1=0;
801
87.8k
         celt_sig *buf;
802
87.8k
         int extrapolation_offset;
803
87.8k
         int extrapolation_len;
804
87.8k
         int j;
805
806
87.8k
         buf = decode_mem[c];
807
92.1M
         for (i=0;i<max_period+CELT_LPC_ORDER;i++)
808
92.0M
            exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT);
809
810
87.8k
         if (loss_duration == 0)
811
52.2k
         {
812
52.2k
            opus_val32 ac[CELT_LPC_ORDER+1];
813
            /* Compute LPC coefficients for the last MAX_PERIOD samples before
814
               the first loss so we can work in the excitation-filter domain. */
815
52.2k
            _celt_autocorr(exc, ac, window, overlap,
816
52.2k
                   CELT_LPC_ORDER, max_period, st->arch);
817
            /* Add a noise floor of -40 dB. */
818
52.2k
#ifdef FIXED_POINT
819
52.2k
            ac[0] += SHR32(ac[0],13);
820
#else
821
            ac[0] *= 1.0001f;
822
#endif
823
            /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
824
1.30M
            for (i=1;i<=CELT_LPC_ORDER;i++)
825
1.25M
            {
826
               /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
827
1.25M
#ifdef FIXED_POINT
828
1.25M
               ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
829
#else
830
               ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
831
#endif
832
1.25M
            }
833
52.2k
            _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER);
834
52.2k
#ifdef FIXED_POINT
835
         /* For fixed-point, apply bandwidth expansion until we can guarantee that
836
            no overflow can happen in the IIR filter. This means:
837
            32768*sum(abs(filter)) < 2^31 */
838
57.3k
         while (1) {
839
57.3k
            opus_val16 tmp=Q15ONE;
840
57.3k
            opus_val32 sum=QCONST16(1., SIG_SHIFT);
841
1.43M
            for (i=0;i<CELT_LPC_ORDER;i++)
842
1.37M
               sum += ABS16(lpc[c*CELT_LPC_ORDER+i]);
843
57.3k
            if (sum < 65535) break;
844
127k
            for (i=0;i<CELT_LPC_ORDER;i++)
845
122k
            {
846
122k
               tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
847
122k
               lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp);
848
122k
            }
849
5.10k
         }
850
52.2k
#endif
851
52.2k
         }
852
         /* Initialize the LPC history with the samples just before the start
853
            of the region for which we're computing the excitation. */
854
87.8k
         {
855
            /* Compute the excitation for exc_length samples before the loss. We need the copy
856
               because celt_fir() cannot filter in-place. */
857
87.8k
            celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER,
858
87.8k
                  fir_tmp, exc_length, CELT_LPC_ORDER, st->arch);
859
87.8k
            OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length);
860
87.8k
         }
861
862
         /* Check if the waveform is decaying, and if so how fast.
863
            We do this to avoid adding energy when concealing in a segment
864
            with decaying energy. */
865
87.8k
         {
866
87.8k
            opus_val32 E1=1, E2=1;
867
87.8k
            int decay_length;
868
87.8k
#ifdef FIXED_POINT
869
87.8k
            int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20);
870
#ifdef ENABLE_QEXT
871
            if (st->qext_scale==2) shift++;
872
#endif
873
87.8k
#endif
874
87.8k
            decay_length = exc_length>>1;
875
22.1M
            for (i=0;i<decay_length;i++)
876
22.0M
            {
877
22.0M
               opus_val16 e;
878
22.0M
               e = exc[max_period-decay_length+i];
879
22.0M
               E1 += SHR32(MULT16_16(e, e), shift);
880
22.0M
               e = exc[max_period-2*decay_length+i];
881
22.0M
               E2 += SHR32(MULT16_16(e, e), shift);
882
22.0M
            }
883
87.8k
            E1 = MIN32(E1, E2);
884
87.8k
            decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
885
87.8k
         }
886
887
         /* Move the decoder memory one frame to the left to give us room to
888
            add the data for the new frame. We ignore the overlap that extends
889
            past the end of the buffer, because we aren't going to use it. */
890
87.8k
         OPUS_MOVE(buf, buf+N, decode_buffer_size-N);
891
892
         /* Extrapolate from the end of the excitation with a period of
893
            "pitch_index", scaling down each period by an additional factor of
894
            "decay". */
895
87.8k
         extrapolation_offset = max_period-pitch_index;
896
         /* We need to extrapolate enough samples to cover a complete MDCT
897
            window (including overlap/2 samples on both sides). */
898
87.8k
         extrapolation_len = N+overlap;
899
         /* We also apply fading if this is not the first loss. */
900
87.8k
         attenuation = MULT16_16_Q15(fade, decay);
901
27.2M
         for (i=j=0;i<extrapolation_len;i++,j++)
902
27.2M
         {
903
27.2M
            opus_val16 tmp;
904
27.2M
            if (j >= pitch_index) {
905
97.8k
               j -= pitch_index;
906
97.8k
               attenuation = MULT16_16_Q15(attenuation, decay);
907
97.8k
            }
908
27.2M
            buf[decode_buffer_size-N+i] =
909
27.2M
                  SHL32(EXTEND32(MULT16_16_Q15(attenuation,
910
27.2M
                        exc[extrapolation_offset+j])), SIG_SHIFT);
911
            /* Compute the energy of the previously decoded signal whose
912
               excitation we're copying. */
913
27.2M
            tmp = SROUND16(
914
27.2M
                  buf[decode_buffer_size-max_period-N+extrapolation_offset+j],
915
27.2M
                  SIG_SHIFT);
916
27.2M
            S1 += SHR32(MULT16_16(tmp, tmp), 11);
917
27.2M
         }
918
87.8k
         {
919
87.8k
            opus_val16 lpc_mem[CELT_LPC_ORDER];
920
            /* Copy the last decoded samples (prior to the overlap region) to
921
               synthesis filter memory so we can have a continuous signal. */
922
2.19M
            for (i=0;i<CELT_LPC_ORDER;i++)
923
2.10M
               lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT);
924
            /* Apply the synthesis filter to convert the excitation back into
925
               the signal domain. */
926
87.8k
            celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER,
927
87.8k
                  buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER,
928
87.8k
                  lpc_mem, st->arch);
929
87.8k
#ifdef FIXED_POINT
930
27.2M
            for (i=0; i < extrapolation_len; i++)
931
27.2M
               buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT);
932
87.8k
#endif
933
87.8k
         }
934
935
         /* Check if the synthesis energy is higher than expected, which can
936
            happen with the signal changes during our window. If so,
937
            attenuate. */
938
87.8k
         {
939
87.8k
            opus_val32 S2=0;
940
27.2M
            for (i=0;i<extrapolation_len;i++)
941
27.2M
            {
942
27.2M
               opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT);
943
27.2M
               S2 += SHR32(MULT16_16(tmp, tmp), 11);
944
27.2M
            }
945
            /* This checks for an "explosion" in the synthesis. */
946
87.8k
#ifdef FIXED_POINT
947
87.8k
            if (!(S1 > SHR32(S2,2)))
948
#else
949
            /* The float test is written this way to catch NaNs in the output
950
               of the IIR filter at the same time. */
951
            if (!(S1 > 0.2f*S2))
952
#endif
953
50.0k
            {
954
14.4M
               for (i=0;i<extrapolation_len;i++)
955
14.3M
                  buf[decode_buffer_size-N+i] = 0;
956
50.0k
            } else if (S1 < S2)
957
19.7k
            {
958
19.7k
               opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
959
2.38M
               for (i=0;i<overlap;i++)
960
2.36M
               {
961
2.36M
                  opus_val16 tmp_g = Q15ONE
962
2.36M
                        - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio);
963
2.36M
                  buf[decode_buffer_size-N+i] =
964
2.36M
                        MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]);
965
2.36M
               }
966
3.91M
               for (i=overlap;i<extrapolation_len;i++)
967
3.89M
               {
968
3.89M
                  buf[decode_buffer_size-N+i] =
969
3.89M
                        MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]);
970
3.89M
               }
971
19.7k
            }
972
87.8k
         }
973
974
87.8k
      } while (++c<C);
975
976
#ifdef ENABLE_DEEP_PLC
977
      if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
978
         float overlap_mem;
979
         int samples_needed16k;
980
         celt_sig *buf;
981
         VARDECL(float, buf_copy);
982
         buf = decode_mem[0];
983
         ALLOC(buf_copy, C*overlap, float);
984
         c=0; do {
985
            OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap);
986
         } while (++c<C);
987
988
         /* Need enough samples from the PLC to cover the frame size, resampling delay,
989
            and the overlap at the end. */
990
         samples_needed16k = (N+SINC_ORDER+overlap)/3;
991
         if (loss_duration == 0) {
992
            st->plc_fill = 0;
993
         }
994
         while (st->plc_fill < samples_needed16k) {
995
            lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]);
996
            st->plc_fill += FRAME_SIZE;
997
         }
998
         /* Resample to 48 kHz. */
999
         for (i=0;i<(N+overlap)/3;i++) {
1000
            int j;
1001
            float sum;
1002
            for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j];
1003
            buf[decode_buffer_size-N+3*i] = sum;
1004
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2];
1005
            buf[decode_buffer_size-N+3*i+1] = sum;
1006
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1];
1007
            buf[decode_buffer_size-N+3*i+2] = sum;
1008
         }
1009
         OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3);
1010
         st->plc_fill -= N/3;
1011
         for (i=0;i<N;i++) {
1012
            float tmp = buf[decode_buffer_size-N+i];
1013
            buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem;
1014
            st->plc_preemphasis_mem = tmp;
1015
         }
1016
         overlap_mem = st->plc_preemphasis_mem;
1017
         for (i=0;i<overlap;i++) {
1018
            float tmp = buf[decode_buffer_size+i];
1019
            buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem;
1020
            overlap_mem = tmp;
1021
         }
1022
         /* For now, we just do mono PLC. */
1023
         if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap);
1024
         c=0; do {
1025
            /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */
1026
            if (loss_duration == 0) {
1027
               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];
1028
            }
1029
         } while (++c<C);
1030
      }
1031
#endif
1032
52.6k
      st->prefilter_and_fold = 1;
1033
52.6k
   }
1034
1035
   /* Saturate to something large to avoid wrap-around. */
1036
64.6k
   st->loss_duration = IMIN(10000, loss_duration+(1<<LM));
1037
1038
64.6k
   RESTORE_STACK;
1039
64.6k
}
celt_decoder.c:celt_decode_lost
Line
Count
Source
668
53.3k
{
669
53.3k
   int c;
670
53.3k
   int i;
671
53.3k
   const int C = st->channels;
672
53.3k
   celt_sig *decode_mem[2];
673
53.3k
   celt_sig *out_syn[2];
674
53.3k
   opus_val16 *lpc;
675
53.3k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
676
53.3k
   const OpusCustomMode *mode;
677
53.3k
   int nbEBands;
678
53.3k
   int overlap;
679
53.3k
   int start;
680
53.3k
   int loss_duration;
681
53.3k
   int noise_based;
682
53.3k
   const opus_int16 *eBands;
683
53.3k
   int decode_buffer_size;
684
53.3k
   int max_period;
685
53.3k
#ifdef ENABLE_QEXT
686
53.3k
   int qext_scale;
687
53.3k
#endif
688
53.3k
   SAVE_STACK;
689
53.3k
#ifdef ENABLE_QEXT
690
53.3k
   qext_scale = st->qext_scale;
691
53.3k
#endif
692
53.3k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
693
53.3k
   max_period = QEXT_SCALE(MAX_PERIOD);
694
53.3k
   mode = st->mode;
695
53.3k
   nbEBands = mode->nbEBands;
696
53.3k
   overlap = mode->overlap;
697
53.3k
   eBands = mode->eBands;
698
699
73.5k
   c=0; do {
700
73.5k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
701
73.5k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
702
73.5k
   } while (++c<C);
703
53.3k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C);
704
53.3k
   oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER);
705
53.3k
   oldLogE = oldBandE + 2*nbEBands;
706
53.3k
   oldLogE2 = oldLogE + 2*nbEBands;
707
53.3k
   backgroundLogE = oldLogE2  + 2*nbEBands;
708
709
53.3k
   loss_duration = st->loss_duration;
710
53.3k
   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
53.3k
   noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
716
53.3k
   if (noise_based)
717
5.91k
   {
718
      /* Noise-based PLC/CNG */
719
5.91k
      VARDECL(celt_norm, X);
720
5.91k
      opus_uint32 seed;
721
5.91k
      int end;
722
5.91k
      int effEnd;
723
5.91k
      celt_glog decay;
724
5.91k
      end = st->end;
725
5.91k
      effEnd = IMAX(start, IMIN(end, mode->effEBands));
726
727
5.91k
      ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
728
8.44k
      c=0; do {
729
8.44k
         OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
730
8.44k
               decode_buffer_size-N+overlap);
731
8.44k
      } while (++c<C);
732
733
5.91k
      if (st->prefilter_and_fold) {
734
95
         prefilter_and_fold(st, N);
735
95
      }
736
737
      /* Energy decay */
738
5.91k
      decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f);
739
5.91k
      c=0; do
740
8.44k
      {
741
38.2k
         for (i=start;i<end;i++)
742
29.8k
            oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
743
8.44k
      } while (++c<C);
744
5.91k
      seed = st->rng;
745
14.3k
      for (c=0;c<C;c++)
746
8.44k
      {
747
38.2k
         for (i=start;i<effEnd;i++)
748
29.8k
         {
749
29.8k
            int j;
750
29.8k
            int boffs;
751
29.8k
            int blen;
752
29.8k
            boffs = N*c+(eBands[i]<<LM);
753
29.8k
            blen = (eBands[i+1]-eBands[i])<<LM;
754
1.34M
            for (j=0;j<blen;j++)
755
1.31M
            {
756
1.31M
               seed = celt_lcg_rand(seed);
757
1.31M
               X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14);
758
1.31M
            }
759
29.8k
            renormalise_vector(X+boffs, blen, Q31ONE, st->arch);
760
29.8k
         }
761
8.44k
      }
762
5.91k
      st->rng = seed;
763
764
5.91k
      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
5.91k
      st->prefilter_and_fold = 0;
766
      /* Skip regular PLC until we get two consecutive packets. */
767
5.91k
      st->skip_plc = 1;
768
47.4k
   } else {
769
47.4k
      int exc_length;
770
      /* Pitch-based PLC */
771
47.4k
      const celt_coef *window;
772
47.4k
      opus_val16 *exc;
773
47.4k
      opus_val16 fade = Q15ONE;
774
47.4k
      int pitch_index;
775
47.4k
      VARDECL(opus_val16, _exc);
776
47.4k
      VARDECL(opus_val16, fir_tmp);
777
778
47.4k
      if (loss_duration == 0)
779
27.8k
      {
780
#ifdef ENABLE_DEEP_PLC
781
        if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
782
#endif
783
27.8k
         st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch);
784
27.8k
      } else {
785
19.6k
         pitch_index = st->last_pitch_index;
786
19.6k
         fade = QCONST16(.8f,15);
787
19.6k
      }
788
789
      /* We want the excitation for 2 pitch periods in order to look for a
790
         decaying signal, but we can't get more than MAX_PERIOD. */
791
47.4k
      exc_length = IMIN(2*pitch_index, max_period);
792
793
47.4k
      ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16);
794
47.4k
      ALLOC(fir_tmp, exc_length, opus_val16);
795
47.4k
      exc = _exc+CELT_LPC_ORDER;
796
47.4k
      window = mode->window;
797
65.1k
      c=0; do {
798
65.1k
         opus_val16 decay;
799
65.1k
         opus_val16 attenuation;
800
65.1k
         opus_val32 S1=0;
801
65.1k
         celt_sig *buf;
802
65.1k
         int extrapolation_offset;
803
65.1k
         int extrapolation_len;
804
65.1k
         int j;
805
806
65.1k
         buf = decode_mem[c];
807
88.1M
         for (i=0;i<max_period+CELT_LPC_ORDER;i++)
808
88.0M
            exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT);
809
810
65.1k
         if (loss_duration == 0)
811
37.0k
         {
812
37.0k
            opus_val32 ac[CELT_LPC_ORDER+1];
813
            /* Compute LPC coefficients for the last MAX_PERIOD samples before
814
               the first loss so we can work in the excitation-filter domain. */
815
37.0k
            _celt_autocorr(exc, ac, window, overlap,
816
37.0k
                   CELT_LPC_ORDER, max_period, st->arch);
817
            /* Add a noise floor of -40 dB. */
818
37.0k
#ifdef FIXED_POINT
819
37.0k
            ac[0] += SHR32(ac[0],13);
820
#else
821
            ac[0] *= 1.0001f;
822
#endif
823
            /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
824
926k
            for (i=1;i<=CELT_LPC_ORDER;i++)
825
889k
            {
826
               /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
827
889k
#ifdef FIXED_POINT
828
889k
               ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
829
#else
830
               ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
831
#endif
832
889k
            }
833
37.0k
            _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER);
834
37.0k
#ifdef FIXED_POINT
835
         /* For fixed-point, apply bandwidth expansion until we can guarantee that
836
            no overflow can happen in the IIR filter. This means:
837
            32768*sum(abs(filter)) < 2^31 */
838
57.4k
         while (1) {
839
57.4k
            opus_val16 tmp=Q15ONE;
840
57.4k
            opus_val32 sum=QCONST16(1., SIG_SHIFT);
841
1.43M
            for (i=0;i<CELT_LPC_ORDER;i++)
842
1.37M
               sum += ABS16(lpc[c*CELT_LPC_ORDER+i]);
843
57.4k
            if (sum < 65535) break;
844
510k
            for (i=0;i<CELT_LPC_ORDER;i++)
845
489k
            {
846
489k
               tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
847
489k
               lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp);
848
489k
            }
849
20.4k
         }
850
37.0k
#endif
851
37.0k
         }
852
         /* Initialize the LPC history with the samples just before the start
853
            of the region for which we're computing the excitation. */
854
65.1k
         {
855
            /* Compute the excitation for exc_length samples before the loss. We need the copy
856
               because celt_fir() cannot filter in-place. */
857
65.1k
            celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER,
858
65.1k
                  fir_tmp, exc_length, CELT_LPC_ORDER, st->arch);
859
65.1k
            OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length);
860
65.1k
         }
861
862
         /* Check if the waveform is decaying, and if so how fast.
863
            We do this to avoid adding energy when concealing in a segment
864
            with decaying energy. */
865
65.1k
         {
866
65.1k
            opus_val32 E1=1, E2=1;
867
65.1k
            int decay_length;
868
65.1k
#ifdef FIXED_POINT
869
65.1k
            int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20);
870
65.1k
#ifdef ENABLE_QEXT
871
65.1k
            if (st->qext_scale==2) shift++;
872
65.1k
#endif
873
65.1k
#endif
874
65.1k
            decay_length = exc_length>>1;
875
20.2M
            for (i=0;i<decay_length;i++)
876
20.2M
            {
877
20.2M
               opus_val16 e;
878
20.2M
               e = exc[max_period-decay_length+i];
879
20.2M
               E1 += SHR32(MULT16_16(e, e), shift);
880
20.2M
               e = exc[max_period-2*decay_length+i];
881
20.2M
               E2 += SHR32(MULT16_16(e, e), shift);
882
20.2M
            }
883
65.1k
            E1 = MIN32(E1, E2);
884
65.1k
            decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
885
65.1k
         }
886
887
         /* Move the decoder memory one frame to the left to give us room to
888
            add the data for the new frame. We ignore the overlap that extends
889
            past the end of the buffer, because we aren't going to use it. */
890
65.1k
         OPUS_MOVE(buf, buf+N, decode_buffer_size-N);
891
892
         /* Extrapolate from the end of the excitation with a period of
893
            "pitch_index", scaling down each period by an additional factor of
894
            "decay". */
895
65.1k
         extrapolation_offset = max_period-pitch_index;
896
         /* We need to extrapolate enough samples to cover a complete MDCT
897
            window (including overlap/2 samples on both sides). */
898
65.1k
         extrapolation_len = N+overlap;
899
         /* We also apply fading if this is not the first loss. */
900
65.1k
         attenuation = MULT16_16_Q15(fade, decay);
901
26.3M
         for (i=j=0;i<extrapolation_len;i++,j++)
902
26.2M
         {
903
26.2M
            opus_val16 tmp;
904
26.2M
            if (j >= pitch_index) {
905
81.6k
               j -= pitch_index;
906
81.6k
               attenuation = MULT16_16_Q15(attenuation, decay);
907
81.6k
            }
908
26.2M
            buf[decode_buffer_size-N+i] =
909
26.2M
                  SHL32(EXTEND32(MULT16_16_Q15(attenuation,
910
26.2M
                        exc[extrapolation_offset+j])), SIG_SHIFT);
911
            /* Compute the energy of the previously decoded signal whose
912
               excitation we're copying. */
913
26.2M
            tmp = SROUND16(
914
26.2M
                  buf[decode_buffer_size-max_period-N+extrapolation_offset+j],
915
26.2M
                  SIG_SHIFT);
916
26.2M
            S1 += SHR32(MULT16_16(tmp, tmp), 11);
917
26.2M
         }
918
65.1k
         {
919
65.1k
            opus_val16 lpc_mem[CELT_LPC_ORDER];
920
            /* Copy the last decoded samples (prior to the overlap region) to
921
               synthesis filter memory so we can have a continuous signal. */
922
1.62M
            for (i=0;i<CELT_LPC_ORDER;i++)
923
1.56M
               lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT);
924
            /* Apply the synthesis filter to convert the excitation back into
925
               the signal domain. */
926
65.1k
            celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER,
927
65.1k
                  buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER,
928
65.1k
                  lpc_mem, st->arch);
929
65.1k
#ifdef FIXED_POINT
930
26.3M
            for (i=0; i < extrapolation_len; i++)
931
26.2M
               buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT);
932
65.1k
#endif
933
65.1k
         }
934
935
         /* Check if the synthesis energy is higher than expected, which can
936
            happen with the signal changes during our window. If so,
937
            attenuate. */
938
65.1k
         {
939
65.1k
            opus_val32 S2=0;
940
26.3M
            for (i=0;i<extrapolation_len;i++)
941
26.2M
            {
942
26.2M
               opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT);
943
26.2M
               S2 += SHR32(MULT16_16(tmp, tmp), 11);
944
26.2M
            }
945
            /* This checks for an "explosion" in the synthesis. */
946
65.1k
#ifdef FIXED_POINT
947
65.1k
            if (!(S1 > SHR32(S2,2)))
948
#else
949
            /* The float test is written this way to catch NaNs in the output
950
               of the IIR filter at the same time. */
951
            if (!(S1 > 0.2f*S2))
952
#endif
953
39.7k
            {
954
15.1M
               for (i=0;i<extrapolation_len;i++)
955
15.1M
                  buf[decode_buffer_size-N+i] = 0;
956
39.7k
            } else if (S1 < S2)
957
11.5k
            {
958
11.5k
               opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
959
1.83M
               for (i=0;i<overlap;i++)
960
1.81M
               {
961
1.81M
                  opus_val16 tmp_g = Q15ONE
962
1.81M
                        - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio);
963
1.81M
                  buf[decode_buffer_size-N+i] =
964
1.81M
                        MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]);
965
1.81M
               }
966
2.78M
               for (i=overlap;i<extrapolation_len;i++)
967
2.76M
               {
968
2.76M
                  buf[decode_buffer_size-N+i] =
969
2.76M
                        MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]);
970
2.76M
               }
971
11.5k
            }
972
65.1k
         }
973
974
65.1k
      } while (++c<C);
975
976
#ifdef ENABLE_DEEP_PLC
977
      if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
978
         float overlap_mem;
979
         int samples_needed16k;
980
         celt_sig *buf;
981
         VARDECL(float, buf_copy);
982
         buf = decode_mem[0];
983
         ALLOC(buf_copy, C*overlap, float);
984
         c=0; do {
985
            OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap);
986
         } while (++c<C);
987
988
         /* Need enough samples from the PLC to cover the frame size, resampling delay,
989
            and the overlap at the end. */
990
         samples_needed16k = (N+SINC_ORDER+overlap)/3;
991
         if (loss_duration == 0) {
992
            st->plc_fill = 0;
993
         }
994
         while (st->plc_fill < samples_needed16k) {
995
            lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]);
996
            st->plc_fill += FRAME_SIZE;
997
         }
998
         /* Resample to 48 kHz. */
999
         for (i=0;i<(N+overlap)/3;i++) {
1000
            int j;
1001
            float sum;
1002
            for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j];
1003
            buf[decode_buffer_size-N+3*i] = sum;
1004
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2];
1005
            buf[decode_buffer_size-N+3*i+1] = sum;
1006
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1];
1007
            buf[decode_buffer_size-N+3*i+2] = sum;
1008
         }
1009
         OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3);
1010
         st->plc_fill -= N/3;
1011
         for (i=0;i<N;i++) {
1012
            float tmp = buf[decode_buffer_size-N+i];
1013
            buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem;
1014
            st->plc_preemphasis_mem = tmp;
1015
         }
1016
         overlap_mem = st->plc_preemphasis_mem;
1017
         for (i=0;i<overlap;i++) {
1018
            float tmp = buf[decode_buffer_size+i];
1019
            buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem;
1020
            overlap_mem = tmp;
1021
         }
1022
         /* For now, we just do mono PLC. */
1023
         if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap);
1024
         c=0; do {
1025
            /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */
1026
            if (loss_duration == 0) {
1027
               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];
1028
            }
1029
         } while (++c<C);
1030
      }
1031
#endif
1032
47.4k
      st->prefilter_and_fold = 1;
1033
47.4k
   }
1034
1035
   /* Saturate to something large to avoid wrap-around. */
1036
53.3k
   st->loss_duration = IMIN(10000, loss_duration+(1<<LM));
1037
1038
53.3k
   RESTORE_STACK;
1039
53.3k
}
celt_decoder.c:celt_decode_lost
Line
Count
Source
668
77.5k
{
669
77.5k
   int c;
670
77.5k
   int i;
671
77.5k
   const int C = st->channels;
672
77.5k
   celt_sig *decode_mem[2];
673
77.5k
   celt_sig *out_syn[2];
674
77.5k
   opus_val16 *lpc;
675
77.5k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
676
77.5k
   const OpusCustomMode *mode;
677
77.5k
   int nbEBands;
678
77.5k
   int overlap;
679
77.5k
   int start;
680
77.5k
   int loss_duration;
681
77.5k
   int noise_based;
682
77.5k
   const opus_int16 *eBands;
683
77.5k
   int decode_buffer_size;
684
77.5k
   int max_period;
685
77.5k
#ifdef ENABLE_QEXT
686
77.5k
   int qext_scale;
687
77.5k
#endif
688
77.5k
   SAVE_STACK;
689
77.5k
#ifdef ENABLE_QEXT
690
77.5k
   qext_scale = st->qext_scale;
691
77.5k
#endif
692
77.5k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
693
77.5k
   max_period = QEXT_SCALE(MAX_PERIOD);
694
77.5k
   mode = st->mode;
695
77.5k
   nbEBands = mode->nbEBands;
696
77.5k
   overlap = mode->overlap;
697
77.5k
   eBands = mode->eBands;
698
699
120k
   c=0; do {
700
120k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
701
120k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
702
120k
   } while (++c<C);
703
77.5k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C);
704
77.5k
   oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER);
705
77.5k
   oldLogE = oldBandE + 2*nbEBands;
706
77.5k
   oldLogE2 = oldLogE + 2*nbEBands;
707
77.5k
   backgroundLogE = oldLogE2  + 2*nbEBands;
708
709
77.5k
   loss_duration = st->loss_duration;
710
77.5k
   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
77.5k
   noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
716
77.5k
   if (noise_based)
717
17.8k
   {
718
      /* Noise-based PLC/CNG */
719
17.8k
      VARDECL(celt_norm, X);
720
17.8k
      opus_uint32 seed;
721
17.8k
      int end;
722
17.8k
      int effEnd;
723
17.8k
      celt_glog decay;
724
17.8k
      end = st->end;
725
17.8k
      effEnd = IMAX(start, IMIN(end, mode->effEBands));
726
727
17.8k
      ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
728
25.5k
      c=0; do {
729
25.5k
         OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
730
25.5k
               decode_buffer_size-N+overlap);
731
25.5k
      } while (++c<C);
732
733
17.8k
      if (st->prefilter_and_fold) {
734
259
         prefilter_and_fold(st, N);
735
259
      }
736
737
      /* Energy decay */
738
17.8k
      decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f);
739
17.8k
      c=0; do
740
25.5k
      {
741
122k
         for (i=start;i<end;i++)
742
96.6k
            oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
743
25.5k
      } while (++c<C);
744
17.8k
      seed = st->rng;
745
43.3k
      for (c=0;c<C;c++)
746
25.5k
      {
747
122k
         for (i=start;i<effEnd;i++)
748
96.6k
         {
749
96.6k
            int j;
750
96.6k
            int boffs;
751
96.6k
            int blen;
752
96.6k
            boffs = N*c+(eBands[i]<<LM);
753
96.6k
            blen = (eBands[i+1]-eBands[i])<<LM;
754
5.12M
            for (j=0;j<blen;j++)
755
5.03M
            {
756
5.03M
               seed = celt_lcg_rand(seed);
757
5.03M
               X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14);
758
5.03M
            }
759
96.6k
            renormalise_vector(X+boffs, blen, Q31ONE, st->arch);
760
96.6k
         }
761
25.5k
      }
762
17.8k
      st->rng = seed;
763
764
17.8k
      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
17.8k
      st->prefilter_and_fold = 0;
766
      /* Skip regular PLC until we get two consecutive packets. */
767
17.8k
      st->skip_plc = 1;
768
59.7k
   } else {
769
59.7k
      int exc_length;
770
      /* Pitch-based PLC */
771
59.7k
      const celt_coef *window;
772
59.7k
      opus_val16 *exc;
773
59.7k
      opus_val16 fade = Q15ONE;
774
59.7k
      int pitch_index;
775
59.7k
      VARDECL(opus_val16, _exc);
776
59.7k
      VARDECL(opus_val16, fir_tmp);
777
778
59.7k
      if (loss_duration == 0)
779
35.8k
      {
780
#ifdef ENABLE_DEEP_PLC
781
        if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
782
#endif
783
35.8k
         st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch);
784
35.8k
      } else {
785
23.8k
         pitch_index = st->last_pitch_index;
786
23.8k
         fade = QCONST16(.8f,15);
787
23.8k
      }
788
789
      /* We want the excitation for 2 pitch periods in order to look for a
790
         decaying signal, but we can't get more than MAX_PERIOD. */
791
59.7k
      exc_length = IMIN(2*pitch_index, max_period);
792
793
59.7k
      ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16);
794
59.7k
      ALLOC(fir_tmp, exc_length, opus_val16);
795
59.7k
      exc = _exc+CELT_LPC_ORDER;
796
59.7k
      window = mode->window;
797
95.1k
      c=0; do {
798
95.1k
         opus_val16 decay;
799
95.1k
         opus_val16 attenuation;
800
95.1k
         opus_val32 S1=0;
801
95.1k
         celt_sig *buf;
802
95.1k
         int extrapolation_offset;
803
95.1k
         int extrapolation_len;
804
95.1k
         int j;
805
806
95.1k
         buf = decode_mem[c];
807
110M
         for (i=0;i<max_period+CELT_LPC_ORDER;i++)
808
110M
            exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT);
809
810
95.1k
         if (loss_duration == 0)
811
56.8k
         {
812
56.8k
            opus_val32 ac[CELT_LPC_ORDER+1];
813
            /* Compute LPC coefficients for the last MAX_PERIOD samples before
814
               the first loss so we can work in the excitation-filter domain. */
815
56.8k
            _celt_autocorr(exc, ac, window, overlap,
816
56.8k
                   CELT_LPC_ORDER, max_period, st->arch);
817
            /* Add a noise floor of -40 dB. */
818
#ifdef FIXED_POINT
819
            ac[0] += SHR32(ac[0],13);
820
#else
821
56.8k
            ac[0] *= 1.0001f;
822
56.8k
#endif
823
            /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
824
1.42M
            for (i=1;i<=CELT_LPC_ORDER;i++)
825
1.36M
            {
826
               /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
827
#ifdef FIXED_POINT
828
               ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
829
#else
830
1.36M
               ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
831
1.36M
#endif
832
1.36M
            }
833
56.8k
            _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER);
834
#ifdef FIXED_POINT
835
         /* For fixed-point, apply bandwidth expansion until we can guarantee that
836
            no overflow can happen in the IIR filter. This means:
837
            32768*sum(abs(filter)) < 2^31 */
838
         while (1) {
839
            opus_val16 tmp=Q15ONE;
840
            opus_val32 sum=QCONST16(1., SIG_SHIFT);
841
            for (i=0;i<CELT_LPC_ORDER;i++)
842
               sum += ABS16(lpc[c*CELT_LPC_ORDER+i]);
843
            if (sum < 65535) break;
844
            for (i=0;i<CELT_LPC_ORDER;i++)
845
            {
846
               tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
847
               lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp);
848
            }
849
         }
850
#endif
851
56.8k
         }
852
         /* Initialize the LPC history with the samples just before the start
853
            of the region for which we're computing the excitation. */
854
95.1k
         {
855
            /* Compute the excitation for exc_length samples before the loss. We need the copy
856
               because celt_fir() cannot filter in-place. */
857
95.1k
            celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER,
858
95.1k
                  fir_tmp, exc_length, CELT_LPC_ORDER, st->arch);
859
95.1k
            OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length);
860
95.1k
         }
861
862
         /* Check if the waveform is decaying, and if so how fast.
863
            We do this to avoid adding energy when concealing in a segment
864
            with decaying energy. */
865
95.1k
         {
866
95.1k
            opus_val32 E1=1, E2=1;
867
95.1k
            int decay_length;
868
#ifdef FIXED_POINT
869
            int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20);
870
#ifdef ENABLE_QEXT
871
            if (st->qext_scale==2) shift++;
872
#endif
873
#endif
874
95.1k
            decay_length = exc_length>>1;
875
25.1M
            for (i=0;i<decay_length;i++)
876
25.0M
            {
877
25.0M
               opus_val16 e;
878
25.0M
               e = exc[max_period-decay_length+i];
879
25.0M
               E1 += SHR32(MULT16_16(e, e), shift);
880
25.0M
               e = exc[max_period-2*decay_length+i];
881
25.0M
               E2 += SHR32(MULT16_16(e, e), shift);
882
25.0M
            }
883
95.1k
            E1 = MIN32(E1, E2);
884
95.1k
            decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
885
95.1k
         }
886
887
         /* Move the decoder memory one frame to the left to give us room to
888
            add the data for the new frame. We ignore the overlap that extends
889
            past the end of the buffer, because we aren't going to use it. */
890
95.1k
         OPUS_MOVE(buf, buf+N, decode_buffer_size-N);
891
892
         /* Extrapolate from the end of the excitation with a period of
893
            "pitch_index", scaling down each period by an additional factor of
894
            "decay". */
895
95.1k
         extrapolation_offset = max_period-pitch_index;
896
         /* We need to extrapolate enough samples to cover a complete MDCT
897
            window (including overlap/2 samples on both sides). */
898
95.1k
         extrapolation_len = N+overlap;
899
         /* We also apply fading if this is not the first loss. */
900
95.1k
         attenuation = MULT16_16_Q15(fade, decay);
901
35.7M
         for (i=j=0;i<extrapolation_len;i++,j++)
902
35.6M
         {
903
35.6M
            opus_val16 tmp;
904
35.6M
            if (j >= pitch_index) {
905
124k
               j -= pitch_index;
906
124k
               attenuation = MULT16_16_Q15(attenuation, decay);
907
124k
            }
908
35.6M
            buf[decode_buffer_size-N+i] =
909
35.6M
                  SHL32(EXTEND32(MULT16_16_Q15(attenuation,
910
35.6M
                        exc[extrapolation_offset+j])), SIG_SHIFT);
911
            /* Compute the energy of the previously decoded signal whose
912
               excitation we're copying. */
913
35.6M
            tmp = SROUND16(
914
35.6M
                  buf[decode_buffer_size-max_period-N+extrapolation_offset+j],
915
35.6M
                  SIG_SHIFT);
916
35.6M
            S1 += SHR32(MULT16_16(tmp, tmp), 11);
917
35.6M
         }
918
95.1k
         {
919
95.1k
            opus_val16 lpc_mem[CELT_LPC_ORDER];
920
            /* Copy the last decoded samples (prior to the overlap region) to
921
               synthesis filter memory so we can have a continuous signal. */
922
2.37M
            for (i=0;i<CELT_LPC_ORDER;i++)
923
2.28M
               lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT);
924
            /* Apply the synthesis filter to convert the excitation back into
925
               the signal domain. */
926
95.1k
            celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER,
927
95.1k
                  buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER,
928
95.1k
                  lpc_mem, st->arch);
929
#ifdef FIXED_POINT
930
            for (i=0; i < extrapolation_len; i++)
931
               buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT);
932
#endif
933
95.1k
         }
934
935
         /* Check if the synthesis energy is higher than expected, which can
936
            happen with the signal changes during our window. If so,
937
            attenuate. */
938
95.1k
         {
939
95.1k
            opus_val32 S2=0;
940
35.7M
            for (i=0;i<extrapolation_len;i++)
941
35.6M
            {
942
35.6M
               opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT);
943
35.6M
               S2 += SHR32(MULT16_16(tmp, tmp), 11);
944
35.6M
            }
945
            /* This checks for an "explosion" in the synthesis. */
946
#ifdef FIXED_POINT
947
            if (!(S1 > SHR32(S2,2)))
948
#else
949
            /* The float test is written this way to catch NaNs in the output
950
               of the IIR filter at the same time. */
951
95.1k
            if (!(S1 > 0.2f*S2))
952
10.8k
#endif
953
10.8k
            {
954
4.22M
               for (i=0;i<extrapolation_len;i++)
955
4.21M
                  buf[decode_buffer_size-N+i] = 0;
956
84.2k
            } else if (S1 < S2)
957
17.2k
            {
958
17.2k
               opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
959
2.35M
               for (i=0;i<overlap;i++)
960
2.33M
               {
961
2.33M
                  opus_val16 tmp_g = Q15ONE
962
2.33M
                        - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio);
963
2.33M
                  buf[decode_buffer_size-N+i] =
964
2.33M
                        MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]);
965
2.33M
               }
966
3.11M
               for (i=overlap;i<extrapolation_len;i++)
967
3.10M
               {
968
3.10M
                  buf[decode_buffer_size-N+i] =
969
3.10M
                        MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]);
970
3.10M
               }
971
17.2k
            }
972
95.1k
         }
973
974
95.1k
      } while (++c<C);
975
976
#ifdef ENABLE_DEEP_PLC
977
      if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
978
         float overlap_mem;
979
         int samples_needed16k;
980
         celt_sig *buf;
981
         VARDECL(float, buf_copy);
982
         buf = decode_mem[0];
983
         ALLOC(buf_copy, C*overlap, float);
984
         c=0; do {
985
            OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap);
986
         } while (++c<C);
987
988
         /* Need enough samples from the PLC to cover the frame size, resampling delay,
989
            and the overlap at the end. */
990
         samples_needed16k = (N+SINC_ORDER+overlap)/3;
991
         if (loss_duration == 0) {
992
            st->plc_fill = 0;
993
         }
994
         while (st->plc_fill < samples_needed16k) {
995
            lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]);
996
            st->plc_fill += FRAME_SIZE;
997
         }
998
         /* Resample to 48 kHz. */
999
         for (i=0;i<(N+overlap)/3;i++) {
1000
            int j;
1001
            float sum;
1002
            for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j];
1003
            buf[decode_buffer_size-N+3*i] = sum;
1004
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2];
1005
            buf[decode_buffer_size-N+3*i+1] = sum;
1006
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1];
1007
            buf[decode_buffer_size-N+3*i+2] = sum;
1008
         }
1009
         OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3);
1010
         st->plc_fill -= N/3;
1011
         for (i=0;i<N;i++) {
1012
            float tmp = buf[decode_buffer_size-N+i];
1013
            buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem;
1014
            st->plc_preemphasis_mem = tmp;
1015
         }
1016
         overlap_mem = st->plc_preemphasis_mem;
1017
         for (i=0;i<overlap;i++) {
1018
            float tmp = buf[decode_buffer_size+i];
1019
            buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem;
1020
            overlap_mem = tmp;
1021
         }
1022
         /* For now, we just do mono PLC. */
1023
         if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap);
1024
         c=0; do {
1025
            /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */
1026
            if (loss_duration == 0) {
1027
               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];
1028
            }
1029
         } while (++c<C);
1030
      }
1031
#endif
1032
59.7k
      st->prefilter_and_fold = 1;
1033
59.7k
   }
1034
1035
   /* Saturate to something large to avoid wrap-around. */
1036
77.5k
   st->loss_duration = IMIN(10000, loss_duration+(1<<LM));
1037
1038
77.5k
   RESTORE_STACK;
1039
77.5k
}
celt_decoder.c:celt_decode_lost
Line
Count
Source
668
77.5k
{
669
77.5k
   int c;
670
77.5k
   int i;
671
77.5k
   const int C = st->channels;
672
77.5k
   celt_sig *decode_mem[2];
673
77.5k
   celt_sig *out_syn[2];
674
77.5k
   opus_val16 *lpc;
675
77.5k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
676
77.5k
   const OpusCustomMode *mode;
677
77.5k
   int nbEBands;
678
77.5k
   int overlap;
679
77.5k
   int start;
680
77.5k
   int loss_duration;
681
77.5k
   int noise_based;
682
77.5k
   const opus_int16 *eBands;
683
77.5k
   int decode_buffer_size;
684
77.5k
   int max_period;
685
#ifdef ENABLE_QEXT
686
   int qext_scale;
687
#endif
688
77.5k
   SAVE_STACK;
689
#ifdef ENABLE_QEXT
690
   qext_scale = st->qext_scale;
691
#endif
692
77.5k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
693
77.5k
   max_period = QEXT_SCALE(MAX_PERIOD);
694
77.5k
   mode = st->mode;
695
77.5k
   nbEBands = mode->nbEBands;
696
77.5k
   overlap = mode->overlap;
697
77.5k
   eBands = mode->eBands;
698
699
120k
   c=0; do {
700
120k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
701
120k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
702
120k
   } while (++c<C);
703
77.5k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*C);
704
77.5k
   oldBandE = (celt_glog*)(lpc+C*CELT_LPC_ORDER);
705
77.5k
   oldLogE = oldBandE + 2*nbEBands;
706
77.5k
   oldLogE2 = oldLogE + 2*nbEBands;
707
77.5k
   backgroundLogE = oldLogE2  + 2*nbEBands;
708
709
77.5k
   loss_duration = st->loss_duration;
710
77.5k
   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
77.5k
   noise_based = loss_duration >= 40 || start != 0 || st->skip_plc;
716
77.5k
   if (noise_based)
717
17.8k
   {
718
      /* Noise-based PLC/CNG */
719
17.8k
      VARDECL(celt_norm, X);
720
17.8k
      opus_uint32 seed;
721
17.8k
      int end;
722
17.8k
      int effEnd;
723
17.8k
      celt_glog decay;
724
17.8k
      end = st->end;
725
17.8k
      effEnd = IMAX(start, IMIN(end, mode->effEBands));
726
727
17.8k
      ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
728
25.5k
      c=0; do {
729
25.5k
         OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
730
25.5k
               decode_buffer_size-N+overlap);
731
25.5k
      } while (++c<C);
732
733
17.8k
      if (st->prefilter_and_fold) {
734
259
         prefilter_and_fold(st, N);
735
259
      }
736
737
      /* Energy decay */
738
17.8k
      decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f);
739
17.8k
      c=0; do
740
25.5k
      {
741
122k
         for (i=start;i<end;i++)
742
96.6k
            oldBandE[c*nbEBands+i] = MAXG(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
743
25.5k
      } while (++c<C);
744
17.8k
      seed = st->rng;
745
43.3k
      for (c=0;c<C;c++)
746
25.5k
      {
747
122k
         for (i=start;i<effEnd;i++)
748
96.6k
         {
749
96.6k
            int j;
750
96.6k
            int boffs;
751
96.6k
            int blen;
752
96.6k
            boffs = N*c+(eBands[i]<<LM);
753
96.6k
            blen = (eBands[i+1]-eBands[i])<<LM;
754
5.12M
            for (j=0;j<blen;j++)
755
5.03M
            {
756
5.03M
               seed = celt_lcg_rand(seed);
757
5.03M
               X[boffs+j] = SHL32((celt_norm)((opus_int32)seed>>20), NORM_SHIFT-14);
758
5.03M
            }
759
96.6k
            renormalise_vector(X+boffs, blen, Q31ONE, st->arch);
760
96.6k
         }
761
25.5k
      }
762
17.8k
      st->rng = seed;
763
764
17.8k
      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
17.8k
      st->prefilter_and_fold = 0;
766
      /* Skip regular PLC until we get two consecutive packets. */
767
17.8k
      st->skip_plc = 1;
768
59.7k
   } else {
769
59.7k
      int exc_length;
770
      /* Pitch-based PLC */
771
59.7k
      const celt_coef *window;
772
59.7k
      opus_val16 *exc;
773
59.7k
      opus_val16 fade = Q15ONE;
774
59.7k
      int pitch_index;
775
59.7k
      VARDECL(opus_val16, _exc);
776
59.7k
      VARDECL(opus_val16, fir_tmp);
777
778
59.7k
      if (loss_duration == 0)
779
35.8k
      {
780
#ifdef ENABLE_DEEP_PLC
781
        if (lpcnet != NULL && lpcnet->loaded) update_plc_state(lpcnet, decode_mem, &st->plc_preemphasis_mem, C);
782
#endif
783
35.8k
         st->last_pitch_index = pitch_index = celt_plc_pitch_search(st, decode_mem, C, st->arch);
784
35.8k
      } else {
785
23.8k
         pitch_index = st->last_pitch_index;
786
23.8k
         fade = QCONST16(.8f,15);
787
23.8k
      }
788
789
      /* We want the excitation for 2 pitch periods in order to look for a
790
         decaying signal, but we can't get more than MAX_PERIOD. */
791
59.7k
      exc_length = IMIN(2*pitch_index, max_period);
792
793
59.7k
      ALLOC(_exc, max_period+CELT_LPC_ORDER, opus_val16);
794
59.7k
      ALLOC(fir_tmp, exc_length, opus_val16);
795
59.7k
      exc = _exc+CELT_LPC_ORDER;
796
59.7k
      window = mode->window;
797
95.1k
      c=0; do {
798
95.1k
         opus_val16 decay;
799
95.1k
         opus_val16 attenuation;
800
95.1k
         opus_val32 S1=0;
801
95.1k
         celt_sig *buf;
802
95.1k
         int extrapolation_offset;
803
95.1k
         int extrapolation_len;
804
95.1k
         int j;
805
806
95.1k
         buf = decode_mem[c];
807
110M
         for (i=0;i<max_period+CELT_LPC_ORDER;i++)
808
110M
            exc[i-CELT_LPC_ORDER] = SROUND16(buf[decode_buffer_size-max_period-CELT_LPC_ORDER+i], SIG_SHIFT);
809
810
95.1k
         if (loss_duration == 0)
811
56.8k
         {
812
56.8k
            opus_val32 ac[CELT_LPC_ORDER+1];
813
            /* Compute LPC coefficients for the last MAX_PERIOD samples before
814
               the first loss so we can work in the excitation-filter domain. */
815
56.8k
            _celt_autocorr(exc, ac, window, overlap,
816
56.8k
                   CELT_LPC_ORDER, max_period, st->arch);
817
            /* Add a noise floor of -40 dB. */
818
#ifdef FIXED_POINT
819
            ac[0] += SHR32(ac[0],13);
820
#else
821
56.8k
            ac[0] *= 1.0001f;
822
56.8k
#endif
823
            /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
824
1.42M
            for (i=1;i<=CELT_LPC_ORDER;i++)
825
1.36M
            {
826
               /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
827
#ifdef FIXED_POINT
828
               ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
829
#else
830
1.36M
               ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
831
1.36M
#endif
832
1.36M
            }
833
56.8k
            _celt_lpc(lpc+c*CELT_LPC_ORDER, ac, CELT_LPC_ORDER);
834
#ifdef FIXED_POINT
835
         /* For fixed-point, apply bandwidth expansion until we can guarantee that
836
            no overflow can happen in the IIR filter. This means:
837
            32768*sum(abs(filter)) < 2^31 */
838
         while (1) {
839
            opus_val16 tmp=Q15ONE;
840
            opus_val32 sum=QCONST16(1., SIG_SHIFT);
841
            for (i=0;i<CELT_LPC_ORDER;i++)
842
               sum += ABS16(lpc[c*CELT_LPC_ORDER+i]);
843
            if (sum < 65535) break;
844
            for (i=0;i<CELT_LPC_ORDER;i++)
845
            {
846
               tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
847
               lpc[c*CELT_LPC_ORDER+i] = MULT16_16_Q15(lpc[c*CELT_LPC_ORDER+i], tmp);
848
            }
849
         }
850
#endif
851
56.8k
         }
852
         /* Initialize the LPC history with the samples just before the start
853
            of the region for which we're computing the excitation. */
854
95.1k
         {
855
            /* Compute the excitation for exc_length samples before the loss. We need the copy
856
               because celt_fir() cannot filter in-place. */
857
95.1k
            celt_fir(exc+max_period-exc_length, lpc+c*CELT_LPC_ORDER,
858
95.1k
                  fir_tmp, exc_length, CELT_LPC_ORDER, st->arch);
859
95.1k
            OPUS_COPY(exc+max_period-exc_length, fir_tmp, exc_length);
860
95.1k
         }
861
862
         /* Check if the waveform is decaying, and if so how fast.
863
            We do this to avoid adding energy when concealing in a segment
864
            with decaying energy. */
865
95.1k
         {
866
95.1k
            opus_val32 E1=1, E2=1;
867
95.1k
            int decay_length;
868
#ifdef FIXED_POINT
869
            int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[max_period-exc_length], exc_length))-20);
870
#ifdef ENABLE_QEXT
871
            if (st->qext_scale==2) shift++;
872
#endif
873
#endif
874
95.1k
            decay_length = exc_length>>1;
875
25.1M
            for (i=0;i<decay_length;i++)
876
25.0M
            {
877
25.0M
               opus_val16 e;
878
25.0M
               e = exc[max_period-decay_length+i];
879
25.0M
               E1 += SHR32(MULT16_16(e, e), shift);
880
25.0M
               e = exc[max_period-2*decay_length+i];
881
25.0M
               E2 += SHR32(MULT16_16(e, e), shift);
882
25.0M
            }
883
95.1k
            E1 = MIN32(E1, E2);
884
95.1k
            decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
885
95.1k
         }
886
887
         /* Move the decoder memory one frame to the left to give us room to
888
            add the data for the new frame. We ignore the overlap that extends
889
            past the end of the buffer, because we aren't going to use it. */
890
95.1k
         OPUS_MOVE(buf, buf+N, decode_buffer_size-N);
891
892
         /* Extrapolate from the end of the excitation with a period of
893
            "pitch_index", scaling down each period by an additional factor of
894
            "decay". */
895
95.1k
         extrapolation_offset = max_period-pitch_index;
896
         /* We need to extrapolate enough samples to cover a complete MDCT
897
            window (including overlap/2 samples on both sides). */
898
95.1k
         extrapolation_len = N+overlap;
899
         /* We also apply fading if this is not the first loss. */
900
95.1k
         attenuation = MULT16_16_Q15(fade, decay);
901
35.7M
         for (i=j=0;i<extrapolation_len;i++,j++)
902
35.6M
         {
903
35.6M
            opus_val16 tmp;
904
35.6M
            if (j >= pitch_index) {
905
124k
               j -= pitch_index;
906
124k
               attenuation = MULT16_16_Q15(attenuation, decay);
907
124k
            }
908
35.6M
            buf[decode_buffer_size-N+i] =
909
35.6M
                  SHL32(EXTEND32(MULT16_16_Q15(attenuation,
910
35.6M
                        exc[extrapolation_offset+j])), SIG_SHIFT);
911
            /* Compute the energy of the previously decoded signal whose
912
               excitation we're copying. */
913
35.6M
            tmp = SROUND16(
914
35.6M
                  buf[decode_buffer_size-max_period-N+extrapolation_offset+j],
915
35.6M
                  SIG_SHIFT);
916
35.6M
            S1 += SHR32(MULT16_16(tmp, tmp), 11);
917
35.6M
         }
918
95.1k
         {
919
95.1k
            opus_val16 lpc_mem[CELT_LPC_ORDER];
920
            /* Copy the last decoded samples (prior to the overlap region) to
921
               synthesis filter memory so we can have a continuous signal. */
922
2.37M
            for (i=0;i<CELT_LPC_ORDER;i++)
923
2.28M
               lpc_mem[i] = SROUND16(buf[decode_buffer_size-N-1-i], SIG_SHIFT);
924
            /* Apply the synthesis filter to convert the excitation back into
925
               the signal domain. */
926
95.1k
            celt_iir(buf+decode_buffer_size-N, lpc+c*CELT_LPC_ORDER,
927
95.1k
                  buf+decode_buffer_size-N, extrapolation_len, CELT_LPC_ORDER,
928
95.1k
                  lpc_mem, st->arch);
929
#ifdef FIXED_POINT
930
            for (i=0; i < extrapolation_len; i++)
931
               buf[decode_buffer_size-N+i] = SATURATE(buf[decode_buffer_size-N+i], SIG_SAT);
932
#endif
933
95.1k
         }
934
935
         /* Check if the synthesis energy is higher than expected, which can
936
            happen with the signal changes during our window. If so,
937
            attenuate. */
938
95.1k
         {
939
95.1k
            opus_val32 S2=0;
940
35.7M
            for (i=0;i<extrapolation_len;i++)
941
35.6M
            {
942
35.6M
               opus_val16 tmp = SROUND16(buf[decode_buffer_size-N+i], SIG_SHIFT);
943
35.6M
               S2 += SHR32(MULT16_16(tmp, tmp), 11);
944
35.6M
            }
945
            /* This checks for an "explosion" in the synthesis. */
946
#ifdef FIXED_POINT
947
            if (!(S1 > SHR32(S2,2)))
948
#else
949
            /* The float test is written this way to catch NaNs in the output
950
               of the IIR filter at the same time. */
951
95.1k
            if (!(S1 > 0.2f*S2))
952
10.8k
#endif
953
10.8k
            {
954
4.22M
               for (i=0;i<extrapolation_len;i++)
955
4.21M
                  buf[decode_buffer_size-N+i] = 0;
956
84.2k
            } else if (S1 < S2)
957
17.2k
            {
958
17.2k
               opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
959
2.35M
               for (i=0;i<overlap;i++)
960
2.33M
               {
961
2.33M
                  opus_val16 tmp_g = Q15ONE
962
2.33M
                        - MULT16_16_Q15(COEF2VAL16(window[i]), Q15ONE-ratio);
963
2.33M
                  buf[decode_buffer_size-N+i] =
964
2.33M
                        MULT16_32_Q15(tmp_g, buf[decode_buffer_size-N+i]);
965
2.33M
               }
966
3.11M
               for (i=overlap;i<extrapolation_len;i++)
967
3.10M
               {
968
3.10M
                  buf[decode_buffer_size-N+i] =
969
3.10M
                        MULT16_32_Q15(ratio, buf[decode_buffer_size-N+i]);
970
3.10M
               }
971
17.2k
            }
972
95.1k
         }
973
974
95.1k
      } while (++c<C);
975
976
#ifdef ENABLE_DEEP_PLC
977
      if (lpcnet != NULL && lpcnet->loaded && (st->complexity >= 5 || lpcnet->fec_fill_pos > 0)) {
978
         float overlap_mem;
979
         int samples_needed16k;
980
         celt_sig *buf;
981
         VARDECL(float, buf_copy);
982
         buf = decode_mem[0];
983
         ALLOC(buf_copy, C*overlap, float);
984
         c=0; do {
985
            OPUS_COPY(buf_copy+c*overlap, &decode_mem[c][decode_buffer_size-N], overlap);
986
         } while (++c<C);
987
988
         /* Need enough samples from the PLC to cover the frame size, resampling delay,
989
            and the overlap at the end. */
990
         samples_needed16k = (N+SINC_ORDER+overlap)/3;
991
         if (loss_duration == 0) {
992
            st->plc_fill = 0;
993
         }
994
         while (st->plc_fill < samples_needed16k) {
995
            lpcnet_plc_conceal(lpcnet, &st->plc_pcm[st->plc_fill]);
996
            st->plc_fill += FRAME_SIZE;
997
         }
998
         /* Resample to 48 kHz. */
999
         for (i=0;i<(N+overlap)/3;i++) {
1000
            int j;
1001
            float sum;
1002
            for (sum=0, j=0;j<17;j++) sum += 3*st->plc_pcm[i+j]*sinc_filter[3*j];
1003
            buf[decode_buffer_size-N+3*i] = sum;
1004
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+2];
1005
            buf[decode_buffer_size-N+3*i+1] = sum;
1006
            for (sum=0, j=0;j<16;j++) sum += 3*st->plc_pcm[i+j+1]*sinc_filter[3*j+1];
1007
            buf[decode_buffer_size-N+3*i+2] = sum;
1008
         }
1009
         OPUS_MOVE(st->plc_pcm, &st->plc_pcm[N/3], st->plc_fill-N/3);
1010
         st->plc_fill -= N/3;
1011
         for (i=0;i<N;i++) {
1012
            float tmp = buf[decode_buffer_size-N+i];
1013
            buf[decode_buffer_size-N+i] -= PREEMPHASIS*st->plc_preemphasis_mem;
1014
            st->plc_preemphasis_mem = tmp;
1015
         }
1016
         overlap_mem = st->plc_preemphasis_mem;
1017
         for (i=0;i<overlap;i++) {
1018
            float tmp = buf[decode_buffer_size+i];
1019
            buf[decode_buffer_size+i] -= PREEMPHASIS*overlap_mem;
1020
            overlap_mem = tmp;
1021
         }
1022
         /* For now, we just do mono PLC. */
1023
         if (C==2) OPUS_COPY(decode_mem[1], decode_mem[0], decode_buffer_size+overlap);
1024
         c=0; do {
1025
            /* Cross-fade with 48-kHz non-neural PLC for the first 2.5 ms to avoid a discontinuity. */
1026
            if (loss_duration == 0) {
1027
               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];
1028
            }
1029
         } while (++c<C);
1030
      }
1031
#endif
1032
59.7k
      st->prefilter_and_fold = 1;
1033
59.7k
   }
1034
1035
   /* Saturate to something large to avoid wrap-around. */
1036
77.5k
   st->loss_duration = IMIN(10000, loss_duration+(1<<LM));
1037
1038
77.5k
   RESTORE_STACK;
1039
77.5k
}
1040
1041
#ifdef ENABLE_QEXT
1042
5.95k
static void decode_qext_stereo_params(ec_dec *ec, int qext_end, int *qext_intensity, int *qext_dual_stereo) {
1043
5.95k
   *qext_intensity = ec_dec_uint(ec, qext_end+1);
1044
5.95k
   if (*qext_intensity != 0) *qext_dual_stereo = ec_dec_bit_logp(ec, 1);
1045
3.46k
   else *qext_dual_stereo = 0;
1046
5.95k
}
1047
#endif
1048
1049
int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data,
1050
      int len, opus_res * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum
1051
#ifdef ENABLE_DEEP_PLC
1052
      ,LPCNetPLCState *lpcnet
1053
#endif
1054
      ARG_QEXT(const unsigned char *qext_payload) ARG_QEXT(int qext_payload_len)
1055
      )
1056
980k
{
1057
980k
   int c, i, N;
1058
980k
   int spread_decision;
1059
980k
   opus_int32 bits;
1060
980k
   ec_dec _dec;
1061
980k
   VARDECL(celt_norm, X);
1062
980k
   VARDECL(int, fine_quant);
1063
980k
   VARDECL(int, pulses);
1064
980k
   VARDECL(int, cap);
1065
980k
   VARDECL(int, offsets);
1066
980k
   VARDECL(int, fine_priority);
1067
980k
   VARDECL(int, tf_res);
1068
980k
   VARDECL(unsigned char, collapse_masks);
1069
980k
   celt_sig *decode_mem[2];
1070
980k
   celt_sig *out_syn[2];
1071
980k
   opus_val16 *lpc;
1072
980k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
1073
1074
980k
   int shortBlocks;
1075
980k
   int isTransient;
1076
980k
   int intra_ener;
1077
980k
   const int CC = st->channels;
1078
980k
   int LM, M;
1079
980k
   int start;
1080
980k
   int end;
1081
980k
   int effEnd;
1082
980k
   int codedBands;
1083
980k
   int alloc_trim;
1084
980k
   int postfilter_pitch;
1085
980k
   opus_val16 postfilter_gain;
1086
980k
   int intensity=0;
1087
980k
   int dual_stereo=0;
1088
980k
   opus_int32 total_bits;
1089
980k
   opus_int32 balance;
1090
980k
   opus_int32 tell;
1091
980k
   int dynalloc_logp;
1092
980k
   int postfilter_tapset;
1093
980k
   int anti_collapse_rsv;
1094
980k
   int anti_collapse_on=0;
1095
980k
   int silence;
1096
980k
   int C = st->stream_channels;
1097
980k
   const OpusCustomMode *mode;
1098
980k
   int nbEBands;
1099
980k
   int overlap;
1100
980k
   const opus_int16 *eBands;
1101
980k
   celt_glog max_background_increase;
1102
980k
   int decode_buffer_size;
1103
#ifdef ENABLE_QEXT
1104
   opus_int32 qext_bits;
1105
   ec_dec ext_dec;
1106
   int qext_bytes=0;
1107
   int qext_end=0;
1108
   int qext_intensity=0;
1109
   int qext_dual_stereo=0;
1110
   VARDECL(int, extra_quant);
1111
   VARDECL(int, extra_pulses);
1112
   const CELTMode *qext_mode = NULL;
1113
460k
   CELTMode qext_mode_struct;
1114
   celt_glog *qext_oldBandE=NULL;
1115
   int qext_scale;
1116
#else
1117
298k
# define qext_bytes 0
1118
#endif
1119
980k
   ALLOC_STACK;
1120
#ifdef ENABLE_QEXT
1121
   qext_scale = st->qext_scale;
1122
#endif
1123
980k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1124
1125
980k
   VALIDATE_CELT_DECODER(st);
1126
980k
   mode = st->mode;
1127
980k
   nbEBands = mode->nbEBands;
1128
980k
   overlap = mode->overlap;
1129
980k
   eBands = mode->eBands;
1130
980k
   start = st->start;
1131
980k
   end = st->end;
1132
980k
   frame_size *= st->downsample;
1133
1134
980k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC);
1135
980k
   oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER);
1136
980k
   oldLogE = oldBandE + 2*nbEBands;
1137
980k
   oldLogE2 = oldLogE + 2*nbEBands;
1138
980k
   backgroundLogE = oldLogE2  + 2*nbEBands;
1139
1140
#ifdef ENABLE_QEXT
1141
460k
   if (qext_payload) {
1142
23.9k
      ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len);
1143
23.9k
      qext_bytes = qext_payload_len;
1144
436k
   } else {
1145
436k
      ec_dec_init(&ext_dec, NULL, 0);
1146
436k
   }
1147
#endif
1148
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1149
   if (st->signalling && data!=NULL)
1150
   {
1151
      int data0=data[0];
1152
      /* Convert "standard mode" to Opus header */
1153
# ifndef ENABLE_QEXT
1154
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1155
# endif
1156
      {
1157
         data0 = fromOpus(data0);
1158
         if (data0<0)
1159
            return OPUS_INVALID_PACKET;
1160
      }
1161
      st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
1162
      LM = (data0>>3)&0x3;
1163
      C = 1 + ((data0>>2)&0x1);
1164
      if ((data[0] & 0x03) == 0x03) {
1165
         data++;
1166
         len--;
1167
         if (len<=0)
1168
            return OPUS_INVALID_PACKET;
1169
         if (data[0] & 0x40) {
1170
            int p;
1171
            int padding=0;
1172
            data++;
1173
            len--;
1174
            do {
1175
               int tmp;
1176
               if (len<=0)
1177
                  return OPUS_INVALID_PACKET;
1178
               p = *data++;
1179
               len--;
1180
               tmp = p==255 ? 254: p;
1181
               len -= tmp;
1182
               padding += tmp;
1183
            } while (p==255);
1184
            padding--;
1185
            if (len <= 0 || padding<0) return OPUS_INVALID_PACKET;
1186
#ifdef ENABLE_QEXT
1187
            qext_bytes = padding;
1188
            if (data[len] != QEXT_EXTENSION_ID<<1)
1189
               qext_bytes=0;
1190
            ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes);
1191
#endif
1192
         }
1193
      } else
1194
      {
1195
         data++;
1196
         len--;
1197
      }
1198
      if (LM>mode->maxLM)
1199
         return OPUS_INVALID_PACKET;
1200
      if (frame_size < mode->shortMdctSize<<LM)
1201
         return OPUS_BUFFER_TOO_SMALL;
1202
      else
1203
         frame_size = mode->shortMdctSize<<LM;
1204
   } else {
1205
#else
1206
980k
   {
1207
980k
#endif
1208
1.82M
      for (LM=0;LM<=mode->maxLM;LM++)
1209
1.82M
         if (mode->shortMdctSize<<LM==frame_size)
1210
980k
            break;
1211
980k
      if (LM>mode->maxLM)
1212
0
         return OPUS_BAD_ARG;
1213
980k
   }
1214
980k
   M=1<<LM;
1215
1216
980k
   if (len<0 || len>1275 || pcm==NULL)
1217
0
      return OPUS_BAD_ARG;
1218
1219
980k
   N = M*mode->shortMdctSize;
1220
1.47M
   c=0; do {
1221
1.47M
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
1222
1.47M
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
1223
1.47M
   } while (++c<CC);
1224
1225
980k
   effEnd = end;
1226
980k
   if (effEnd > mode->effEBands)
1227
0
      effEnd = mode->effEBands;
1228
1229
980k
   if (data == NULL || len<=1)
1230
391k
   {
1231
391k
      celt_decode_lost(st, N, LM
1232
#ifdef ENABLE_DEEP_PLC
1233
      , lpcnet
1234
#endif
1235
391k
                      );
1236
391k
      deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1237
391k
      RESTORE_STACK;
1238
391k
      return frame_size/st->downsample;
1239
391k
   }
1240
#ifdef ENABLE_DEEP_PLC
1241
   else {
1242
      /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */
1243
      if (lpcnet) lpcnet->blend = 0;
1244
   }
1245
#endif
1246
1247
   /* Check if there are at least two packets received consecutively before
1248
    * turning on the pitch-based PLC */
1249
589k
   if (st->loss_duration == 0) st->skip_plc = 0;
1250
1251
589k
   if (dec == NULL)
1252
65.4k
   {
1253
65.4k
      ec_dec_init(&_dec,(unsigned char*)data,len);
1254
65.4k
      dec = &_dec;
1255
65.4k
   }
1256
1257
589k
   if (C==1)
1258
378k
   {
1259
8.32M
      for (i=0;i<nbEBands;i++)
1260
7.94M
         oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]);
1261
378k
   }
1262
1263
589k
   total_bits = len*8;
1264
589k
   tell = ec_tell(dec);
1265
1266
589k
   if (tell >= total_bits)
1267
44.4k
      silence = 1;
1268
545k
   else if (tell==1)
1269
521k
      silence = ec_dec_bit_logp(dec, 15);
1270
23.5k
   else
1271
23.5k
      silence = 0;
1272
589k
   if (silence)
1273
67.9k
   {
1274
      /* Pretend we've read all the remaining bits */
1275
67.9k
      tell = len*8;
1276
67.9k
      dec->nbits_total+=tell-ec_tell(dec);
1277
67.9k
   }
1278
1279
589k
   postfilter_gain = 0;
1280
589k
   postfilter_pitch = 0;
1281
589k
   postfilter_tapset = 0;
1282
589k
   if (start==0 && tell+16 <= total_bits)
1283
362k
   {
1284
362k
      if(ec_dec_bit_logp(dec, 1))
1285
104k
      {
1286
104k
         int qg, octave;
1287
104k
         octave = ec_dec_uint(dec, 6);
1288
104k
         postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
1289
104k
         qg = ec_dec_bits(dec, 3);
1290
104k
         if (ec_tell(dec)+2<=total_bits)
1291
104k
            postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
1292
104k
         postfilter_gain = QCONST16(.09375f,15)*(qg+1);
1293
104k
      }
1294
362k
      tell = ec_tell(dec);
1295
362k
   }
1296
1297
589k
   if (LM > 0 && tell+3 <= total_bits)
1298
280k
   {
1299
280k
      isTransient = ec_dec_bit_logp(dec, 3);
1300
280k
      tell = ec_tell(dec);
1301
280k
   }
1302
308k
   else
1303
308k
      isTransient = 0;
1304
1305
589k
   if (isTransient)
1306
51.3k
      shortBlocks = M;
1307
538k
   else
1308
538k
      shortBlocks = 0;
1309
1310
   /* Decode the global flags (first symbols in the stream) */
1311
589k
   intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
1312
   /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the
1313
      risk of getting loud artifacts. */
1314
589k
   if (!intra_ener && st->loss_duration != 0) {
1315
115k
      c=0; do
1316
231k
      {
1317
231k
         celt_glog safety = 0;
1318
231k
         int missing = IMIN(10, st->loss_duration>>LM);
1319
231k
         if (LM==0) safety = GCONST(1.5f);
1320
27.7k
         else if (LM==1) safety = GCONST(.5f);
1321
4.21M
         for (i=start;i<end;i++)
1322
3.98M
         {
1323
3.98M
            if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) {
1324
               /* If energy is going down already, continue the trend. */
1325
1.66M
               opus_val32 slope;
1326
1.66M
               opus_val32 E0, E1, E2;
1327
1.66M
               E0 = oldBandE[c*nbEBands+i];
1328
1.66M
               E1 = oldLogE[c*nbEBands+i];
1329
1.66M
               E2 = oldLogE2[c*nbEBands+i];
1330
1.66M
               slope = MAX32(E1 - E0, HALF32(E2 - E0));
1331
1.66M
               slope = MING(slope, GCONST(2.f));
1332
1.66M
               E0 -= MAX32(0, (1+missing)*slope);
1333
1.66M
               oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0);
1334
2.31M
            } else {
1335
               /* Otherwise take the min of the last frames. */
1336
2.31M
               oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]);
1337
2.31M
            }
1338
            /* Shorter frames have more natural fluctuations -- play it safe. */
1339
3.98M
            oldBandE[c*nbEBands+i] -= safety;
1340
3.98M
         }
1341
231k
      } while (++c<2);
1342
115k
   }
1343
   /* Get band energies */
1344
589k
   unquant_coarse_energy(mode, start, end, oldBandE,
1345
589k
         intra_ener, dec, C, LM);
1346
1347
589k
   ALLOC(tf_res, nbEBands, int);
1348
589k
   tf_decode(start, end, isTransient, tf_res, LM, dec);
1349
1350
589k
   tell = ec_tell(dec);
1351
589k
   spread_decision = SPREAD_NORMAL;
1352
589k
   if (tell+4 <= total_bits)
1353
237k
      spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
1354
1355
589k
   ALLOC(cap, nbEBands, int);
1356
1357
589k
   init_caps(mode,cap,LM,C);
1358
1359
589k
   ALLOC(offsets, nbEBands, int);
1360
1361
589k
   dynalloc_logp = 6;
1362
589k
   total_bits<<=BITRES;
1363
589k
   tell = ec_tell_frac(dec);
1364
10.1M
   for (i=start;i<end;i++)
1365
9.54M
   {
1366
9.54M
      int width, quanta;
1367
9.54M
      int dynalloc_loop_logp;
1368
9.54M
      int boost;
1369
9.54M
      width = C*(eBands[i+1]-eBands[i])<<LM;
1370
      /* quanta is 6 bits, but no more than 1 bit/sample
1371
         and no less than 1/8 bit/sample */
1372
9.54M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1373
9.54M
      dynalloc_loop_logp = dynalloc_logp;
1374
9.54M
      boost = 0;
1375
9.73M
      while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
1376
3.64M
      {
1377
3.64M
         int flag;
1378
3.64M
         flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
1379
3.64M
         tell = ec_tell_frac(dec);
1380
3.64M
         if (!flag)
1381
3.44M
            break;
1382
193k
         boost += quanta;
1383
193k
         total_bits -= quanta;
1384
193k
         dynalloc_loop_logp = 1;
1385
193k
      }
1386
9.54M
      offsets[i] = boost;
1387
      /* Making dynalloc more likely */
1388
9.54M
      if (boost>0)
1389
53.9k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
1390
9.54M
   }
1391
1392
589k
   ALLOC(fine_quant, nbEBands, int);
1393
589k
   alloc_trim = tell+(6<<BITRES) <= total_bits ?
1394
379k
         ec_dec_icdf(dec, trim_icdf, 7) : 5;
1395
1396
589k
   bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1397
589k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1398
589k
   bits -= anti_collapse_rsv;
1399
1400
589k
   ALLOC(pulses, nbEBands, int);
1401
589k
   ALLOC(fine_priority, nbEBands, int);
1402
1403
589k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
1404
589k
         alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1405
589k
         fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
1406
1407
589k
   unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C);
1408
1409
589k
   ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
1410
1411
#ifdef ENABLE_QEXT
1412
290k
   if (qext_bytes && end == nbEBands &&
1413
15.7k
         ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90))
1414
15.7k
       || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) {
1415
15.7k
      int qext_intra_ener;
1416
15.7k
      qext_oldBandE = backgroundLogE + 2*nbEBands;
1417
15.7k
      compute_qext_mode(&qext_mode_struct, mode);
1418
15.7k
      qext_mode = &qext_mode_struct;
1419
15.7k
      qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2;
1420
15.7k
      if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo);
1421
15.7k
      qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0;
1422
15.7k
      unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE,
1423
15.7k
            qext_intra_ener, &ext_dec, C, LM);
1424
15.7k
   }
1425
290k
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
1426
290k
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
1427
290k
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1428
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL,
1429
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0);
1430
290k
   if (qext_bytes > 0) {
1431
20.7k
      unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C);
1432
20.7k
   }
1433
#endif
1434
1435
879k
   c=0; do {
1436
879k
      OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap);
1437
879k
   } while (++c<CC);
1438
1439
   /* Decode fixed codebook */
1440
589k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
1441
1442
589k
   quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
1443
589k
         NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
1444
589k
         len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
1445
589k
         st->arch, st->disable_inv
1446
589k
         ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses)
1447
589k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
1448
1449
#ifdef ENABLE_QEXT
1450
290k
   if (qext_mode) {
1451
15.7k
      VARDECL(int, zeros);
1452
15.7k
      VARDECL(unsigned char, qext_collapse_masks);
1453
15.7k
      ec_dec dummy_dec;
1454
15.7k
      int ext_balance;
1455
15.7k
      ALLOC(zeros, nbEBands, int);
1456
15.7k
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
1457
15.7k
      ec_dec_init(&dummy_dec, NULL, 0);
1458
15.7k
      OPUS_CLEAR(zeros, end);
1459
15.7k
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec);
1460
95.6k
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
1461
15.7k
      unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C);
1462
15.7k
      quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
1463
15.7k
            NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros,
1464
15.7k
            qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0,
1465
15.7k
            st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL);
1466
15.7k
   }
1467
#endif
1468
1469
589k
   if (anti_collapse_rsv > 0)
1470
17.5k
   {
1471
17.5k
      anti_collapse_on = ec_dec_bits(dec, 1);
1472
17.5k
   }
1473
589k
   unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE,
1474
589k
         fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
1475
589k
   if (anti_collapse_on)
1476
13.1k
      anti_collapse(mode, X, collapse_masks, LM, C, N,
1477
13.1k
            start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch);
1478
1479
589k
   if (silence)
1480
67.9k
   {
1481
2.40M
      for (i=0;i<C*nbEBands;i++)
1482
2.33M
         oldBandE[i] = -GCONST(28.f);
1483
67.9k
   }
1484
589k
   if (st->prefilter_and_fold) {
1485
118k
      prefilter_and_fold(st, N);
1486
118k
   }
1487
589k
   celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1488
589k
                  C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
1489
1490
879k
   c=0; do {
1491
879k
      st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1492
879k
      st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1493
879k
      comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1494
879k
            st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
1495
879k
            mode->window, overlap, st->arch);
1496
879k
      if (LM!=0)
1497
479k
         comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1498
479k
               st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
1499
479k
               mode->window, overlap, st->arch);
1500
1501
879k
   } while (++c<CC);
1502
589k
   st->postfilter_period_old = st->postfilter_period;
1503
589k
   st->postfilter_gain_old = st->postfilter_gain;
1504
589k
   st->postfilter_tapset_old = st->postfilter_tapset;
1505
589k
   st->postfilter_period = postfilter_pitch;
1506
589k
   st->postfilter_gain = postfilter_gain;
1507
589k
   st->postfilter_tapset = postfilter_tapset;
1508
589k
   if (LM!=0)
1509
332k
   {
1510
332k
      st->postfilter_period_old = st->postfilter_period;
1511
332k
      st->postfilter_gain_old = st->postfilter_gain;
1512
332k
      st->postfilter_tapset_old = st->postfilter_tapset;
1513
332k
   }
1514
1515
589k
   if (C==1)
1516
378k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
1517
1518
589k
   if (!isTransient)
1519
538k
   {
1520
538k
      OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1521
538k
      OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
1522
538k
   } else {
1523
2.20M
      for (i=0;i<2*nbEBands;i++)
1524
2.15M
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
1525
51.3k
   }
1526
   /* In normal circumstances, we only allow the noise floor to increase by
1527
      up to 2.4 dB/second, but when we're in DTX we give the weight of
1528
      all missing packets to the update packet. */
1529
589k
   max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f);
1530
25.3M
   for (i=0;i<2*nbEBands;i++)
1531
24.7M
      backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]);
1532
   /* In case start or end were to change */
1533
589k
   c=0; do
1534
1.17M
   {
1535
3.49M
      for (i=0;i<start;i++)
1536
2.31M
      {
1537
2.31M
         oldBandE[c*nbEBands+i]=0;
1538
2.31M
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1539
2.31M
      }
1540
4.55M
      for (i=end;i<nbEBands;i++)
1541
3.37M
      {
1542
3.37M
         oldBandE[c*nbEBands+i]=0;
1543
3.37M
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1544
3.37M
      }
1545
1.17M
   } while (++c<2);
1546
589k
   st->rng = dec->rng;
1547
#ifdef ENABLE_QEXT
1548
290k
   if (qext_bytes) st->rng = st->rng ^ ext_dec.rng;
1549
#endif
1550
1551
589k
   deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1552
589k
   st->loss_duration = 0;
1553
589k
   st->prefilter_and_fold = 0;
1554
589k
   RESTORE_STACK;
1555
589k
   if (ec_tell(dec) > 8*len)
1556
8
      return OPUS_INTERNAL_ERROR;
1557
#ifdef ENABLE_QEXT
1558
290k
   if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes)
1559
0
      return OPUS_INTERNAL_ERROR;
1560
290k
#endif
1561
589k
   if(ec_get_error(dec))
1562
8.17k
      st->error = 1;
1563
589k
   return frame_size/st->downsample;
1564
290k
}
celt_decode_with_ec_dred
Line
Count
Source
1056
260k
{
1057
260k
   int c, i, N;
1058
260k
   int spread_decision;
1059
260k
   opus_int32 bits;
1060
260k
   ec_dec _dec;
1061
260k
   VARDECL(celt_norm, X);
1062
260k
   VARDECL(int, fine_quant);
1063
260k
   VARDECL(int, pulses);
1064
260k
   VARDECL(int, cap);
1065
260k
   VARDECL(int, offsets);
1066
260k
   VARDECL(int, fine_priority);
1067
260k
   VARDECL(int, tf_res);
1068
260k
   VARDECL(unsigned char, collapse_masks);
1069
260k
   celt_sig *decode_mem[2];
1070
260k
   celt_sig *out_syn[2];
1071
260k
   opus_val16 *lpc;
1072
260k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
1073
1074
260k
   int shortBlocks;
1075
260k
   int isTransient;
1076
260k
   int intra_ener;
1077
260k
   const int CC = st->channels;
1078
260k
   int LM, M;
1079
260k
   int start;
1080
260k
   int end;
1081
260k
   int effEnd;
1082
260k
   int codedBands;
1083
260k
   int alloc_trim;
1084
260k
   int postfilter_pitch;
1085
260k
   opus_val16 postfilter_gain;
1086
260k
   int intensity=0;
1087
260k
   int dual_stereo=0;
1088
260k
   opus_int32 total_bits;
1089
260k
   opus_int32 balance;
1090
260k
   opus_int32 tell;
1091
260k
   int dynalloc_logp;
1092
260k
   int postfilter_tapset;
1093
260k
   int anti_collapse_rsv;
1094
260k
   int anti_collapse_on=0;
1095
260k
   int silence;
1096
260k
   int C = st->stream_channels;
1097
260k
   const OpusCustomMode *mode;
1098
260k
   int nbEBands;
1099
260k
   int overlap;
1100
260k
   const opus_int16 *eBands;
1101
260k
   celt_glog max_background_increase;
1102
260k
   int decode_buffer_size;
1103
#ifdef ENABLE_QEXT
1104
   opus_int32 qext_bits;
1105
   ec_dec ext_dec;
1106
   int qext_bytes=0;
1107
   int qext_end=0;
1108
   int qext_intensity=0;
1109
   int qext_dual_stereo=0;
1110
   VARDECL(int, extra_quant);
1111
   VARDECL(int, extra_pulses);
1112
   const CELTMode *qext_mode = NULL;
1113
   CELTMode qext_mode_struct;
1114
   celt_glog *qext_oldBandE=NULL;
1115
   int qext_scale;
1116
#else
1117
260k
# define qext_bytes 0
1118
260k
#endif
1119
260k
   ALLOC_STACK;
1120
#ifdef ENABLE_QEXT
1121
   qext_scale = st->qext_scale;
1122
#endif
1123
260k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1124
1125
260k
   VALIDATE_CELT_DECODER(st);
1126
260k
   mode = st->mode;
1127
260k
   nbEBands = mode->nbEBands;
1128
260k
   overlap = mode->overlap;
1129
260k
   eBands = mode->eBands;
1130
260k
   start = st->start;
1131
260k
   end = st->end;
1132
260k
   frame_size *= st->downsample;
1133
1134
260k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC);
1135
260k
   oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER);
1136
260k
   oldLogE = oldBandE + 2*nbEBands;
1137
260k
   oldLogE2 = oldLogE + 2*nbEBands;
1138
260k
   backgroundLogE = oldLogE2  + 2*nbEBands;
1139
1140
#ifdef ENABLE_QEXT
1141
   if (qext_payload) {
1142
      ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len);
1143
      qext_bytes = qext_payload_len;
1144
   } else {
1145
      ec_dec_init(&ext_dec, NULL, 0);
1146
   }
1147
#endif
1148
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1149
   if (st->signalling && data!=NULL)
1150
   {
1151
      int data0=data[0];
1152
      /* Convert "standard mode" to Opus header */
1153
# ifndef ENABLE_QEXT
1154
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1155
# endif
1156
      {
1157
         data0 = fromOpus(data0);
1158
         if (data0<0)
1159
            return OPUS_INVALID_PACKET;
1160
      }
1161
      st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
1162
      LM = (data0>>3)&0x3;
1163
      C = 1 + ((data0>>2)&0x1);
1164
      if ((data[0] & 0x03) == 0x03) {
1165
         data++;
1166
         len--;
1167
         if (len<=0)
1168
            return OPUS_INVALID_PACKET;
1169
         if (data[0] & 0x40) {
1170
            int p;
1171
            int padding=0;
1172
            data++;
1173
            len--;
1174
            do {
1175
               int tmp;
1176
               if (len<=0)
1177
                  return OPUS_INVALID_PACKET;
1178
               p = *data++;
1179
               len--;
1180
               tmp = p==255 ? 254: p;
1181
               len -= tmp;
1182
               padding += tmp;
1183
            } while (p==255);
1184
            padding--;
1185
            if (len <= 0 || padding<0) return OPUS_INVALID_PACKET;
1186
#ifdef ENABLE_QEXT
1187
            qext_bytes = padding;
1188
            if (data[len] != QEXT_EXTENSION_ID<<1)
1189
               qext_bytes=0;
1190
            ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes);
1191
#endif
1192
         }
1193
      } else
1194
      {
1195
         data++;
1196
         len--;
1197
      }
1198
      if (LM>mode->maxLM)
1199
         return OPUS_INVALID_PACKET;
1200
      if (frame_size < mode->shortMdctSize<<LM)
1201
         return OPUS_BUFFER_TOO_SMALL;
1202
      else
1203
         frame_size = mode->shortMdctSize<<LM;
1204
   } else {
1205
#else
1206
260k
   {
1207
260k
#endif
1208
500k
      for (LM=0;LM<=mode->maxLM;LM++)
1209
500k
         if (mode->shortMdctSize<<LM==frame_size)
1210
260k
            break;
1211
260k
      if (LM>mode->maxLM)
1212
0
         return OPUS_BAD_ARG;
1213
260k
   }
1214
260k
   M=1<<LM;
1215
1216
260k
   if (len<0 || len>1275 || pcm==NULL)
1217
0
      return OPUS_BAD_ARG;
1218
1219
260k
   N = M*mode->shortMdctSize;
1220
408k
   c=0; do {
1221
408k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
1222
408k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
1223
408k
   } while (++c<CC);
1224
1225
260k
   effEnd = end;
1226
260k
   if (effEnd > mode->effEBands)
1227
0
      effEnd = mode->effEBands;
1228
1229
260k
   if (data == NULL || len<=1)
1230
110k
   {
1231
110k
      celt_decode_lost(st, N, LM
1232
#ifdef ENABLE_DEEP_PLC
1233
      , lpcnet
1234
#endif
1235
110k
                      );
1236
110k
      deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1237
110k
      RESTORE_STACK;
1238
110k
      return frame_size/st->downsample;
1239
110k
   }
1240
#ifdef ENABLE_DEEP_PLC
1241
   else {
1242
      /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */
1243
      if (lpcnet) lpcnet->blend = 0;
1244
   }
1245
#endif
1246
1247
   /* Check if there are at least two packets received consecutively before
1248
    * turning on the pitch-based PLC */
1249
149k
   if (st->loss_duration == 0) st->skip_plc = 0;
1250
1251
149k
   if (dec == NULL)
1252
13.8k
   {
1253
13.8k
      ec_dec_init(&_dec,(unsigned char*)data,len);
1254
13.8k
      dec = &_dec;
1255
13.8k
   }
1256
1257
149k
   if (C==1)
1258
95.3k
   {
1259
2.09M
      for (i=0;i<nbEBands;i++)
1260
2.00M
         oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]);
1261
95.3k
   }
1262
1263
149k
   total_bits = len*8;
1264
149k
   tell = ec_tell(dec);
1265
1266
149k
   if (tell >= total_bits)
1267
13.1k
      silence = 1;
1268
136k
   else if (tell==1)
1269
131k
      silence = ec_dec_bit_logp(dec, 15);
1270
4.82k
   else
1271
4.82k
      silence = 0;
1272
149k
   if (silence)
1273
19.9k
   {
1274
      /* Pretend we've read all the remaining bits */
1275
19.9k
      tell = len*8;
1276
19.9k
      dec->nbits_total+=tell-ec_tell(dec);
1277
19.9k
   }
1278
1279
149k
   postfilter_gain = 0;
1280
149k
   postfilter_pitch = 0;
1281
149k
   postfilter_tapset = 0;
1282
149k
   if (start==0 && tell+16 <= total_bits)
1283
76.2k
   {
1284
76.2k
      if(ec_dec_bit_logp(dec, 1))
1285
22.5k
      {
1286
22.5k
         int qg, octave;
1287
22.5k
         octave = ec_dec_uint(dec, 6);
1288
22.5k
         postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
1289
22.5k
         qg = ec_dec_bits(dec, 3);
1290
22.5k
         if (ec_tell(dec)+2<=total_bits)
1291
22.5k
            postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
1292
22.5k
         postfilter_gain = QCONST16(.09375f,15)*(qg+1);
1293
22.5k
      }
1294
76.2k
      tell = ec_tell(dec);
1295
76.2k
   }
1296
1297
149k
   if (LM > 0 && tell+3 <= total_bits)
1298
66.4k
   {
1299
66.4k
      isTransient = ec_dec_bit_logp(dec, 3);
1300
66.4k
      tell = ec_tell(dec);
1301
66.4k
   }
1302
83.0k
   else
1303
83.0k
      isTransient = 0;
1304
1305
149k
   if (isTransient)
1306
14.4k
      shortBlocks = M;
1307
135k
   else
1308
135k
      shortBlocks = 0;
1309
1310
   /* Decode the global flags (first symbols in the stream) */
1311
149k
   intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
1312
   /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the
1313
      risk of getting loud artifacts. */
1314
149k
   if (!intra_ener && st->loss_duration != 0) {
1315
29.1k
      c=0; do
1316
58.3k
      {
1317
58.3k
         celt_glog safety = 0;
1318
58.3k
         int missing = IMIN(10, st->loss_duration>>LM);
1319
58.3k
         if (LM==0) safety = GCONST(1.5f);
1320
6.79k
         else if (LM==1) safety = GCONST(.5f);
1321
1.06M
         for (i=start;i<end;i++)
1322
1.00M
         {
1323
1.00M
            if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) {
1324
               /* If energy is going down already, continue the trend. */
1325
421k
               opus_val32 slope;
1326
421k
               opus_val32 E0, E1, E2;
1327
421k
               E0 = oldBandE[c*nbEBands+i];
1328
421k
               E1 = oldLogE[c*nbEBands+i];
1329
421k
               E2 = oldLogE2[c*nbEBands+i];
1330
421k
               slope = MAX32(E1 - E0, HALF32(E2 - E0));
1331
421k
               slope = MING(slope, GCONST(2.f));
1332
421k
               E0 -= MAX32(0, (1+missing)*slope);
1333
421k
               oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0);
1334
583k
            } else {
1335
               /* Otherwise take the min of the last frames. */
1336
583k
               oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]);
1337
583k
            }
1338
            /* Shorter frames have more natural fluctuations -- play it safe. */
1339
1.00M
            oldBandE[c*nbEBands+i] -= safety;
1340
1.00M
         }
1341
58.3k
      } while (++c<2);
1342
29.1k
   }
1343
   /* Get band energies */
1344
149k
   unquant_coarse_energy(mode, start, end, oldBandE,
1345
149k
         intra_ener, dec, C, LM);
1346
1347
149k
   ALLOC(tf_res, nbEBands, int);
1348
149k
   tf_decode(start, end, isTransient, tf_res, LM, dec);
1349
1350
149k
   tell = ec_tell(dec);
1351
149k
   spread_decision = SPREAD_NORMAL;
1352
149k
   if (tell+4 <= total_bits)
1353
47.5k
      spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
1354
1355
149k
   ALLOC(cap, nbEBands, int);
1356
1357
149k
   init_caps(mode,cap,LM,C);
1358
1359
149k
   ALLOC(offsets, nbEBands, int);
1360
1361
149k
   dynalloc_logp = 6;
1362
149k
   total_bits<<=BITRES;
1363
149k
   tell = ec_tell_frac(dec);
1364
2.54M
   for (i=start;i<end;i++)
1365
2.39M
   {
1366
2.39M
      int width, quanta;
1367
2.39M
      int dynalloc_loop_logp;
1368
2.39M
      int boost;
1369
2.39M
      width = C*(eBands[i+1]-eBands[i])<<LM;
1370
      /* quanta is 6 bits, but no more than 1 bit/sample
1371
         and no less than 1/8 bit/sample */
1372
2.39M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1373
2.39M
      dynalloc_loop_logp = dynalloc_logp;
1374
2.39M
      boost = 0;
1375
2.44M
      while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
1376
702k
      {
1377
702k
         int flag;
1378
702k
         flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
1379
702k
         tell = ec_tell_frac(dec);
1380
702k
         if (!flag)
1381
656k
            break;
1382
46.1k
         boost += quanta;
1383
46.1k
         total_bits -= quanta;
1384
46.1k
         dynalloc_loop_logp = 1;
1385
46.1k
      }
1386
2.39M
      offsets[i] = boost;
1387
      /* Making dynalloc more likely */
1388
2.39M
      if (boost>0)
1389
12.0k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
1390
2.39M
   }
1391
1392
149k
   ALLOC(fine_quant, nbEBands, int);
1393
149k
   alloc_trim = tell+(6<<BITRES) <= total_bits ?
1394
108k
         ec_dec_icdf(dec, trim_icdf, 7) : 5;
1395
1396
149k
   bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1397
149k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1398
149k
   bits -= anti_collapse_rsv;
1399
1400
149k
   ALLOC(pulses, nbEBands, int);
1401
149k
   ALLOC(fine_priority, nbEBands, int);
1402
1403
149k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
1404
149k
         alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1405
149k
         fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
1406
1407
149k
   unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C);
1408
1409
149k
   ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
1410
1411
#ifdef ENABLE_QEXT
1412
   if (qext_bytes && end == nbEBands &&
1413
         ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90))
1414
       || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) {
1415
      int qext_intra_ener;
1416
      qext_oldBandE = backgroundLogE + 2*nbEBands;
1417
      compute_qext_mode(&qext_mode_struct, mode);
1418
      qext_mode = &qext_mode_struct;
1419
      qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2;
1420
      if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo);
1421
      qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0;
1422
      unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE,
1423
            qext_intra_ener, &ext_dec, C, LM);
1424
   }
1425
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
1426
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
1427
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1428
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL,
1429
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0);
1430
   if (qext_bytes > 0) {
1431
      unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C);
1432
   }
1433
#endif
1434
1435
231k
   c=0; do {
1436
231k
      OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap);
1437
231k
   } while (++c<CC);
1438
1439
   /* Decode fixed codebook */
1440
149k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
1441
1442
149k
   quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
1443
149k
         NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
1444
149k
         len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
1445
149k
         st->arch, st->disable_inv
1446
149k
         ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses)
1447
149k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
1448
1449
#ifdef ENABLE_QEXT
1450
   if (qext_mode) {
1451
      VARDECL(int, zeros);
1452
      VARDECL(unsigned char, qext_collapse_masks);
1453
      ec_dec dummy_dec;
1454
      int ext_balance;
1455
      ALLOC(zeros, nbEBands, int);
1456
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
1457
      ec_dec_init(&dummy_dec, NULL, 0);
1458
      OPUS_CLEAR(zeros, end);
1459
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec);
1460
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
1461
      unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C);
1462
      quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
1463
            NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros,
1464
            qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0,
1465
            st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL);
1466
   }
1467
#endif
1468
1469
149k
   if (anti_collapse_rsv > 0)
1470
4.09k
   {
1471
4.09k
      anti_collapse_on = ec_dec_bits(dec, 1);
1472
4.09k
   }
1473
149k
   unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE,
1474
149k
         fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
1475
149k
   if (anti_collapse_on)
1476
3.01k
      anti_collapse(mode, X, collapse_masks, LM, C, N,
1477
3.01k
            start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch);
1478
1479
149k
   if (silence)
1480
19.9k
   {
1481
682k
      for (i=0;i<C*nbEBands;i++)
1482
662k
         oldBandE[i] = -GCONST(28.f);
1483
19.9k
   }
1484
149k
   if (st->prefilter_and_fold) {
1485
30.4k
      prefilter_and_fold(st, N);
1486
30.4k
   }
1487
149k
   celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1488
149k
                  C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
1489
1490
231k
   c=0; do {
1491
231k
      st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1492
231k
      st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1493
231k
      comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1494
231k
            st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
1495
231k
            mode->window, overlap, st->arch);
1496
231k
      if (LM!=0)
1497
117k
         comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1498
117k
               st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
1499
117k
               mode->window, overlap, st->arch);
1500
1501
231k
   } while (++c<CC);
1502
149k
   st->postfilter_period_old = st->postfilter_period;
1503
149k
   st->postfilter_gain_old = st->postfilter_gain;
1504
149k
   st->postfilter_tapset_old = st->postfilter_tapset;
1505
149k
   st->postfilter_period = postfilter_pitch;
1506
149k
   st->postfilter_gain = postfilter_gain;
1507
149k
   st->postfilter_tapset = postfilter_tapset;
1508
149k
   if (LM!=0)
1509
81.8k
   {
1510
81.8k
      st->postfilter_period_old = st->postfilter_period;
1511
81.8k
      st->postfilter_gain_old = st->postfilter_gain;
1512
81.8k
      st->postfilter_tapset_old = st->postfilter_tapset;
1513
81.8k
   }
1514
1515
149k
   if (C==1)
1516
95.3k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
1517
1518
149k
   if (!isTransient)
1519
135k
   {
1520
135k
      OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1521
135k
      OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
1522
135k
   } else {
1523
620k
      for (i=0;i<2*nbEBands;i++)
1524
606k
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
1525
14.4k
   }
1526
   /* In normal circumstances, we only allow the noise floor to increase by
1527
      up to 2.4 dB/second, but when we're in DTX we give the weight of
1528
      all missing packets to the update packet. */
1529
149k
   max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f);
1530
6.42M
   for (i=0;i<2*nbEBands;i++)
1531
6.27M
      backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]);
1532
   /* In case start or end were to change */
1533
149k
   c=0; do
1534
298k
   {
1535
910k
      for (i=0;i<start;i++)
1536
611k
      {
1537
611k
         oldBandE[c*nbEBands+i]=0;
1538
611k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1539
611k
      }
1540
1.17M
      for (i=end;i<nbEBands;i++)
1541
873k
      {
1542
873k
         oldBandE[c*nbEBands+i]=0;
1543
873k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1544
873k
      }
1545
298k
   } while (++c<2);
1546
149k
   st->rng = dec->rng;
1547
#ifdef ENABLE_QEXT
1548
   if (qext_bytes) st->rng = st->rng ^ ext_dec.rng;
1549
#endif
1550
1551
149k
   deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1552
149k
   st->loss_duration = 0;
1553
149k
   st->prefilter_and_fold = 0;
1554
149k
   RESTORE_STACK;
1555
149k
   if (ec_tell(dec) > 8*len)
1556
4
      return OPUS_INTERNAL_ERROR;
1557
#ifdef ENABLE_QEXT
1558
   if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes)
1559
      return OPUS_INTERNAL_ERROR;
1560
#endif
1561
149k
   if(ec_get_error(dec))
1562
1.61k
      st->error = 1;
1563
149k
   return frame_size/st->downsample;
1564
149k
}
celt_decode_with_ec_dred
Line
Count
Source
1056
230k
{
1057
230k
   int c, i, N;
1058
230k
   int spread_decision;
1059
230k
   opus_int32 bits;
1060
230k
   ec_dec _dec;
1061
230k
   VARDECL(celt_norm, X);
1062
230k
   VARDECL(int, fine_quant);
1063
230k
   VARDECL(int, pulses);
1064
230k
   VARDECL(int, cap);
1065
230k
   VARDECL(int, offsets);
1066
230k
   VARDECL(int, fine_priority);
1067
230k
   VARDECL(int, tf_res);
1068
230k
   VARDECL(unsigned char, collapse_masks);
1069
230k
   celt_sig *decode_mem[2];
1070
230k
   celt_sig *out_syn[2];
1071
230k
   opus_val16 *lpc;
1072
230k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
1073
1074
230k
   int shortBlocks;
1075
230k
   int isTransient;
1076
230k
   int intra_ener;
1077
230k
   const int CC = st->channels;
1078
230k
   int LM, M;
1079
230k
   int start;
1080
230k
   int end;
1081
230k
   int effEnd;
1082
230k
   int codedBands;
1083
230k
   int alloc_trim;
1084
230k
   int postfilter_pitch;
1085
230k
   opus_val16 postfilter_gain;
1086
230k
   int intensity=0;
1087
230k
   int dual_stereo=0;
1088
230k
   opus_int32 total_bits;
1089
230k
   opus_int32 balance;
1090
230k
   opus_int32 tell;
1091
230k
   int dynalloc_logp;
1092
230k
   int postfilter_tapset;
1093
230k
   int anti_collapse_rsv;
1094
230k
   int anti_collapse_on=0;
1095
230k
   int silence;
1096
230k
   int C = st->stream_channels;
1097
230k
   const OpusCustomMode *mode;
1098
230k
   int nbEBands;
1099
230k
   int overlap;
1100
230k
   const opus_int16 *eBands;
1101
230k
   celt_glog max_background_increase;
1102
230k
   int decode_buffer_size;
1103
230k
#ifdef ENABLE_QEXT
1104
230k
   opus_int32 qext_bits;
1105
230k
   ec_dec ext_dec;
1106
230k
   int qext_bytes=0;
1107
230k
   int qext_end=0;
1108
230k
   int qext_intensity=0;
1109
230k
   int qext_dual_stereo=0;
1110
230k
   VARDECL(int, extra_quant);
1111
230k
   VARDECL(int, extra_pulses);
1112
230k
   const CELTMode *qext_mode = NULL;
1113
230k
   CELTMode qext_mode_struct;
1114
230k
   celt_glog *qext_oldBandE=NULL;
1115
230k
   int qext_scale;
1116
#else
1117
# define qext_bytes 0
1118
#endif
1119
230k
   ALLOC_STACK;
1120
230k
#ifdef ENABLE_QEXT
1121
230k
   qext_scale = st->qext_scale;
1122
230k
#endif
1123
230k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1124
1125
230k
   VALIDATE_CELT_DECODER(st);
1126
230k
   mode = st->mode;
1127
230k
   nbEBands = mode->nbEBands;
1128
230k
   overlap = mode->overlap;
1129
230k
   eBands = mode->eBands;
1130
230k
   start = st->start;
1131
230k
   end = st->end;
1132
230k
   frame_size *= st->downsample;
1133
1134
230k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC);
1135
230k
   oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER);
1136
230k
   oldLogE = oldBandE + 2*nbEBands;
1137
230k
   oldLogE2 = oldLogE + 2*nbEBands;
1138
230k
   backgroundLogE = oldLogE2  + 2*nbEBands;
1139
1140
230k
#ifdef ENABLE_QEXT
1141
230k
   if (qext_payload) {
1142
11.9k
      ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len);
1143
11.9k
      qext_bytes = qext_payload_len;
1144
218k
   } else {
1145
218k
      ec_dec_init(&ext_dec, NULL, 0);
1146
218k
   }
1147
230k
#endif
1148
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1149
   if (st->signalling && data!=NULL)
1150
   {
1151
      int data0=data[0];
1152
      /* Convert "standard mode" to Opus header */
1153
# ifndef ENABLE_QEXT
1154
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1155
# endif
1156
      {
1157
         data0 = fromOpus(data0);
1158
         if (data0<0)
1159
            return OPUS_INVALID_PACKET;
1160
      }
1161
      st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
1162
      LM = (data0>>3)&0x3;
1163
      C = 1 + ((data0>>2)&0x1);
1164
      if ((data[0] & 0x03) == 0x03) {
1165
         data++;
1166
         len--;
1167
         if (len<=0)
1168
            return OPUS_INVALID_PACKET;
1169
         if (data[0] & 0x40) {
1170
            int p;
1171
            int padding=0;
1172
            data++;
1173
            len--;
1174
            do {
1175
               int tmp;
1176
               if (len<=0)
1177
                  return OPUS_INVALID_PACKET;
1178
               p = *data++;
1179
               len--;
1180
               tmp = p==255 ? 254: p;
1181
               len -= tmp;
1182
               padding += tmp;
1183
            } while (p==255);
1184
            padding--;
1185
            if (len <= 0 || padding<0) return OPUS_INVALID_PACKET;
1186
#ifdef ENABLE_QEXT
1187
            qext_bytes = padding;
1188
            if (data[len] != QEXT_EXTENSION_ID<<1)
1189
               qext_bytes=0;
1190
            ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes);
1191
#endif
1192
         }
1193
      } else
1194
      {
1195
         data++;
1196
         len--;
1197
      }
1198
      if (LM>mode->maxLM)
1199
         return OPUS_INVALID_PACKET;
1200
      if (frame_size < mode->shortMdctSize<<LM)
1201
         return OPUS_BUFFER_TOO_SMALL;
1202
      else
1203
         frame_size = mode->shortMdctSize<<LM;
1204
   } else {
1205
#else
1206
230k
   {
1207
230k
#endif
1208
412k
      for (LM=0;LM<=mode->maxLM;LM++)
1209
412k
         if (mode->shortMdctSize<<LM==frame_size)
1210
230k
            break;
1211
230k
      if (LM>mode->maxLM)
1212
0
         return OPUS_BAD_ARG;
1213
230k
   }
1214
230k
   M=1<<LM;
1215
1216
230k
   if (len<0 || len>1275 || pcm==NULL)
1217
0
      return OPUS_BAD_ARG;
1218
1219
230k
   N = M*mode->shortMdctSize;
1220
330k
   c=0; do {
1221
330k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
1222
330k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
1223
330k
   } while (++c<CC);
1224
1225
230k
   effEnd = end;
1226
230k
   if (effEnd > mode->effEBands)
1227
0
      effEnd = mode->effEBands;
1228
1229
230k
   if (data == NULL || len<=1)
1230
84.7k
   {
1231
84.7k
      celt_decode_lost(st, N, LM
1232
#ifdef ENABLE_DEEP_PLC
1233
      , lpcnet
1234
#endif
1235
84.7k
                      );
1236
84.7k
      deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1237
84.7k
      RESTORE_STACK;
1238
84.7k
      return frame_size/st->downsample;
1239
84.7k
   }
1240
#ifdef ENABLE_DEEP_PLC
1241
   else {
1242
      /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */
1243
      if (lpcnet) lpcnet->blend = 0;
1244
   }
1245
#endif
1246
1247
   /* Check if there are at least two packets received consecutively before
1248
    * turning on the pitch-based PLC */
1249
145k
   if (st->loss_duration == 0) st->skip_plc = 0;
1250
1251
145k
   if (dec == NULL)
1252
18.8k
   {
1253
18.8k
      ec_dec_init(&_dec,(unsigned char*)data,len);
1254
18.8k
      dec = &_dec;
1255
18.8k
   }
1256
1257
145k
   if (C==1)
1258
93.9k
   {
1259
2.06M
      for (i=0;i<nbEBands;i++)
1260
1.97M
         oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]);
1261
93.9k
   }
1262
1263
145k
   total_bits = len*8;
1264
145k
   tell = ec_tell(dec);
1265
1266
145k
   if (tell >= total_bits)
1267
9.06k
      silence = 1;
1268
136k
   else if (tell==1)
1269
129k
      silence = ec_dec_bit_logp(dec, 15);
1270
6.93k
   else
1271
6.93k
      silence = 0;
1272
145k
   if (silence)
1273
14.0k
   {
1274
      /* Pretend we've read all the remaining bits */
1275
14.0k
      tell = len*8;
1276
14.0k
      dec->nbits_total+=tell-ec_tell(dec);
1277
14.0k
   }
1278
1279
145k
   postfilter_gain = 0;
1280
145k
   postfilter_pitch = 0;
1281
145k
   postfilter_tapset = 0;
1282
145k
   if (start==0 && tell+16 <= total_bits)
1283
104k
   {
1284
104k
      if(ec_dec_bit_logp(dec, 1))
1285
29.8k
      {
1286
29.8k
         int qg, octave;
1287
29.8k
         octave = ec_dec_uint(dec, 6);
1288
29.8k
         postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
1289
29.8k
         qg = ec_dec_bits(dec, 3);
1290
29.8k
         if (ec_tell(dec)+2<=total_bits)
1291
29.8k
            postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
1292
29.8k
         postfilter_gain = QCONST16(.09375f,15)*(qg+1);
1293
29.8k
      }
1294
104k
      tell = ec_tell(dec);
1295
104k
   }
1296
1297
145k
   if (LM > 0 && tell+3 <= total_bits)
1298
74.0k
   {
1299
74.0k
      isTransient = ec_dec_bit_logp(dec, 3);
1300
74.0k
      tell = ec_tell(dec);
1301
74.0k
   }
1302
71.4k
   else
1303
71.4k
      isTransient = 0;
1304
1305
145k
   if (isTransient)
1306
11.2k
      shortBlocks = M;
1307
134k
   else
1308
134k
      shortBlocks = 0;
1309
1310
   /* Decode the global flags (first symbols in the stream) */
1311
145k
   intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
1312
   /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the
1313
      risk of getting loud artifacts. */
1314
145k
   if (!intra_ener && st->loss_duration != 0) {
1315
28.7k
      c=0; do
1316
57.4k
      {
1317
57.4k
         celt_glog safety = 0;
1318
57.4k
         int missing = IMIN(10, st->loss_duration>>LM);
1319
57.4k
         if (LM==0) safety = GCONST(1.5f);
1320
7.06k
         else if (LM==1) safety = GCONST(.5f);
1321
1.04M
         for (i=start;i<end;i++)
1322
986k
         {
1323
986k
            if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) {
1324
               /* If energy is going down already, continue the trend. */
1325
412k
               opus_val32 slope;
1326
412k
               opus_val32 E0, E1, E2;
1327
412k
               E0 = oldBandE[c*nbEBands+i];
1328
412k
               E1 = oldLogE[c*nbEBands+i];
1329
412k
               E2 = oldLogE2[c*nbEBands+i];
1330
412k
               slope = MAX32(E1 - E0, HALF32(E2 - E0));
1331
412k
               slope = MING(slope, GCONST(2.f));
1332
412k
               E0 -= MAX32(0, (1+missing)*slope);
1333
412k
               oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0);
1334
574k
            } else {
1335
               /* Otherwise take the min of the last frames. */
1336
574k
               oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]);
1337
574k
            }
1338
            /* Shorter frames have more natural fluctuations -- play it safe. */
1339
986k
            oldBandE[c*nbEBands+i] -= safety;
1340
986k
         }
1341
57.4k
      } while (++c<2);
1342
28.7k
   }
1343
   /* Get band energies */
1344
145k
   unquant_coarse_energy(mode, start, end, oldBandE,
1345
145k
         intra_ener, dec, C, LM);
1346
1347
145k
   ALLOC(tf_res, nbEBands, int);
1348
145k
   tf_decode(start, end, isTransient, tf_res, LM, dec);
1349
1350
145k
   tell = ec_tell(dec);
1351
145k
   spread_decision = SPREAD_NORMAL;
1352
145k
   if (tell+4 <= total_bits)
1353
71.1k
      spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
1354
1355
145k
   ALLOC(cap, nbEBands, int);
1356
1357
145k
   init_caps(mode,cap,LM,C);
1358
1359
145k
   ALLOC(offsets, nbEBands, int);
1360
1361
145k
   dynalloc_logp = 6;
1362
145k
   total_bits<<=BITRES;
1363
145k
   tell = ec_tell_frac(dec);
1364
2.52M
   for (i=start;i<end;i++)
1365
2.37M
   {
1366
2.37M
      int width, quanta;
1367
2.37M
      int dynalloc_loop_logp;
1368
2.37M
      int boost;
1369
2.37M
      width = C*(eBands[i+1]-eBands[i])<<LM;
1370
      /* quanta is 6 bits, but no more than 1 bit/sample
1371
         and no less than 1/8 bit/sample */
1372
2.37M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1373
2.37M
      dynalloc_loop_logp = dynalloc_logp;
1374
2.37M
      boost = 0;
1375
2.42M
      while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
1376
1.11M
      {
1377
1.11M
         int flag;
1378
1.11M
         flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
1379
1.11M
         tell = ec_tell_frac(dec);
1380
1.11M
         if (!flag)
1381
1.06M
            break;
1382
50.4k
         boost += quanta;
1383
50.4k
         total_bits -= quanta;
1384
50.4k
         dynalloc_loop_logp = 1;
1385
50.4k
      }
1386
2.37M
      offsets[i] = boost;
1387
      /* Making dynalloc more likely */
1388
2.37M
      if (boost>0)
1389
14.9k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
1390
2.37M
   }
1391
1392
145k
   ALLOC(fine_quant, nbEBands, int);
1393
145k
   alloc_trim = tell+(6<<BITRES) <= total_bits ?
1394
81.1k
         ec_dec_icdf(dec, trim_icdf, 7) : 5;
1395
1396
145k
   bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1397
145k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1398
145k
   bits -= anti_collapse_rsv;
1399
1400
145k
   ALLOC(pulses, nbEBands, int);
1401
145k
   ALLOC(fine_priority, nbEBands, int);
1402
1403
145k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
1404
145k
         alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1405
145k
         fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
1406
1407
145k
   unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C);
1408
1409
145k
   ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
1410
1411
145k
#ifdef ENABLE_QEXT
1412
145k
   if (qext_bytes && end == nbEBands &&
1413
7.86k
         ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90))
1414
7.86k
       || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) {
1415
7.86k
      int qext_intra_ener;
1416
7.86k
      qext_oldBandE = backgroundLogE + 2*nbEBands;
1417
7.86k
      compute_qext_mode(&qext_mode_struct, mode);
1418
7.86k
      qext_mode = &qext_mode_struct;
1419
7.86k
      qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2;
1420
7.86k
      if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo);
1421
7.86k
      qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0;
1422
7.86k
      unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE,
1423
7.86k
            qext_intra_ener, &ext_dec, C, LM);
1424
7.86k
   }
1425
145k
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
1426
145k
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
1427
145k
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1428
145k
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL,
1429
145k
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0);
1430
145k
   if (qext_bytes > 0) {
1431
10.3k
      unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C);
1432
10.3k
   }
1433
145k
#endif
1434
1435
208k
   c=0; do {
1436
208k
      OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap);
1437
208k
   } while (++c<CC);
1438
1439
   /* Decode fixed codebook */
1440
145k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
1441
1442
145k
   quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
1443
145k
         NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
1444
145k
         len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
1445
145k
         st->arch, st->disable_inv
1446
145k
         ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses)
1447
145k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
1448
1449
145k
#ifdef ENABLE_QEXT
1450
145k
   if (qext_mode) {
1451
7.86k
      VARDECL(int, zeros);
1452
7.86k
      VARDECL(unsigned char, qext_collapse_masks);
1453
7.86k
      ec_dec dummy_dec;
1454
7.86k
      int ext_balance;
1455
7.86k
      ALLOC(zeros, nbEBands, int);
1456
7.86k
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
1457
7.86k
      ec_dec_init(&dummy_dec, NULL, 0);
1458
7.86k
      OPUS_CLEAR(zeros, end);
1459
7.86k
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec);
1460
47.8k
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
1461
7.86k
      unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C);
1462
7.86k
      quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
1463
7.86k
            NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros,
1464
7.86k
            qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0,
1465
7.86k
            st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL);
1466
7.86k
   }
1467
145k
#endif
1468
1469
145k
   if (anti_collapse_rsv > 0)
1470
4.67k
   {
1471
4.67k
      anti_collapse_on = ec_dec_bits(dec, 1);
1472
4.67k
   }
1473
145k
   unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE,
1474
145k
         fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
1475
145k
   if (anti_collapse_on)
1476
3.55k
      anti_collapse(mode, X, collapse_masks, LM, C, N,
1477
3.55k
            start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch);
1478
1479
145k
   if (silence)
1480
14.0k
   {
1481
521k
      for (i=0;i<C*nbEBands;i++)
1482
507k
         oldBandE[i] = -GCONST(28.f);
1483
14.0k
   }
1484
145k
   if (st->prefilter_and_fold) {
1485
28.7k
      prefilter_and_fold(st, N);
1486
28.7k
   }
1487
145k
   celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1488
145k
                  C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
1489
1490
208k
   c=0; do {
1491
208k
      st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1492
208k
      st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1493
208k
      comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1494
208k
            st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
1495
208k
            mode->window, overlap, st->arch);
1496
208k
      if (LM!=0)
1497
121k
         comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1498
121k
               st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
1499
121k
               mode->window, overlap, st->arch);
1500
1501
208k
   } while (++c<CC);
1502
145k
   st->postfilter_period_old = st->postfilter_period;
1503
145k
   st->postfilter_gain_old = st->postfilter_gain;
1504
145k
   st->postfilter_tapset_old = st->postfilter_tapset;
1505
145k
   st->postfilter_period = postfilter_pitch;
1506
145k
   st->postfilter_gain = postfilter_gain;
1507
145k
   st->postfilter_tapset = postfilter_tapset;
1508
145k
   if (LM!=0)
1509
84.2k
   {
1510
84.2k
      st->postfilter_period_old = st->postfilter_period;
1511
84.2k
      st->postfilter_gain_old = st->postfilter_gain;
1512
84.2k
      st->postfilter_tapset_old = st->postfilter_tapset;
1513
84.2k
   }
1514
1515
145k
   if (C==1)
1516
93.9k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
1517
1518
145k
   if (!isTransient)
1519
134k
   {
1520
134k
      OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1521
134k
      OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
1522
134k
   } else {
1523
483k
      for (i=0;i<2*nbEBands;i++)
1524
472k
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
1525
11.2k
   }
1526
   /* In normal circumstances, we only allow the noise floor to increase by
1527
      up to 2.4 dB/second, but when we're in DTX we give the weight of
1528
      all missing packets to the update packet. */
1529
145k
   max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f);
1530
6.25M
   for (i=0;i<2*nbEBands;i++)
1531
6.10M
      backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]);
1532
   /* In case start or end were to change */
1533
145k
   c=0; do
1534
290k
   {
1535
835k
      for (i=0;i<start;i++)
1536
544k
      {
1537
544k
         oldBandE[c*nbEBands+i]=0;
1538
544k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1539
544k
      }
1540
1.10M
      for (i=end;i<nbEBands;i++)
1541
814k
      {
1542
814k
         oldBandE[c*nbEBands+i]=0;
1543
814k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1544
814k
      }
1545
290k
   } while (++c<2);
1546
145k
   st->rng = dec->rng;
1547
145k
#ifdef ENABLE_QEXT
1548
145k
   if (qext_bytes) st->rng = st->rng ^ ext_dec.rng;
1549
145k
#endif
1550
1551
145k
   deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1552
145k
   st->loss_duration = 0;
1553
145k
   st->prefilter_and_fold = 0;
1554
145k
   RESTORE_STACK;
1555
145k
   if (ec_tell(dec) > 8*len)
1556
0
      return OPUS_INTERNAL_ERROR;
1557
145k
#ifdef ENABLE_QEXT
1558
145k
   if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes)
1559
0
      return OPUS_INTERNAL_ERROR;
1560
145k
#endif
1561
145k
   if(ec_get_error(dec))
1562
2.47k
      st->error = 1;
1563
145k
   return frame_size/st->downsample;
1564
145k
}
celt_decode_with_ec_dred
Line
Count
Source
1056
230k
{
1057
230k
   int c, i, N;
1058
230k
   int spread_decision;
1059
230k
   opus_int32 bits;
1060
230k
   ec_dec _dec;
1061
230k
   VARDECL(celt_norm, X);
1062
230k
   VARDECL(int, fine_quant);
1063
230k
   VARDECL(int, pulses);
1064
230k
   VARDECL(int, cap);
1065
230k
   VARDECL(int, offsets);
1066
230k
   VARDECL(int, fine_priority);
1067
230k
   VARDECL(int, tf_res);
1068
230k
   VARDECL(unsigned char, collapse_masks);
1069
230k
   celt_sig *decode_mem[2];
1070
230k
   celt_sig *out_syn[2];
1071
230k
   opus_val16 *lpc;
1072
230k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
1073
1074
230k
   int shortBlocks;
1075
230k
   int isTransient;
1076
230k
   int intra_ener;
1077
230k
   const int CC = st->channels;
1078
230k
   int LM, M;
1079
230k
   int start;
1080
230k
   int end;
1081
230k
   int effEnd;
1082
230k
   int codedBands;
1083
230k
   int alloc_trim;
1084
230k
   int postfilter_pitch;
1085
230k
   opus_val16 postfilter_gain;
1086
230k
   int intensity=0;
1087
230k
   int dual_stereo=0;
1088
230k
   opus_int32 total_bits;
1089
230k
   opus_int32 balance;
1090
230k
   opus_int32 tell;
1091
230k
   int dynalloc_logp;
1092
230k
   int postfilter_tapset;
1093
230k
   int anti_collapse_rsv;
1094
230k
   int anti_collapse_on=0;
1095
230k
   int silence;
1096
230k
   int C = st->stream_channels;
1097
230k
   const OpusCustomMode *mode;
1098
230k
   int nbEBands;
1099
230k
   int overlap;
1100
230k
   const opus_int16 *eBands;
1101
230k
   celt_glog max_background_increase;
1102
230k
   int decode_buffer_size;
1103
230k
#ifdef ENABLE_QEXT
1104
230k
   opus_int32 qext_bits;
1105
230k
   ec_dec ext_dec;
1106
230k
   int qext_bytes=0;
1107
230k
   int qext_end=0;
1108
230k
   int qext_intensity=0;
1109
230k
   int qext_dual_stereo=0;
1110
230k
   VARDECL(int, extra_quant);
1111
230k
   VARDECL(int, extra_pulses);
1112
230k
   const CELTMode *qext_mode = NULL;
1113
230k
   CELTMode qext_mode_struct;
1114
230k
   celt_glog *qext_oldBandE=NULL;
1115
230k
   int qext_scale;
1116
#else
1117
# define qext_bytes 0
1118
#endif
1119
230k
   ALLOC_STACK;
1120
230k
#ifdef ENABLE_QEXT
1121
230k
   qext_scale = st->qext_scale;
1122
230k
#endif
1123
230k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1124
1125
230k
   VALIDATE_CELT_DECODER(st);
1126
230k
   mode = st->mode;
1127
230k
   nbEBands = mode->nbEBands;
1128
230k
   overlap = mode->overlap;
1129
230k
   eBands = mode->eBands;
1130
230k
   start = st->start;
1131
230k
   end = st->end;
1132
230k
   frame_size *= st->downsample;
1133
1134
230k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC);
1135
230k
   oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER);
1136
230k
   oldLogE = oldBandE + 2*nbEBands;
1137
230k
   oldLogE2 = oldLogE + 2*nbEBands;
1138
230k
   backgroundLogE = oldLogE2  + 2*nbEBands;
1139
1140
230k
#ifdef ENABLE_QEXT
1141
230k
   if (qext_payload) {
1142
11.9k
      ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len);
1143
11.9k
      qext_bytes = qext_payload_len;
1144
218k
   } else {
1145
218k
      ec_dec_init(&ext_dec, NULL, 0);
1146
218k
   }
1147
230k
#endif
1148
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1149
   if (st->signalling && data!=NULL)
1150
   {
1151
      int data0=data[0];
1152
      /* Convert "standard mode" to Opus header */
1153
# ifndef ENABLE_QEXT
1154
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1155
# endif
1156
      {
1157
         data0 = fromOpus(data0);
1158
         if (data0<0)
1159
            return OPUS_INVALID_PACKET;
1160
      }
1161
      st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
1162
      LM = (data0>>3)&0x3;
1163
      C = 1 + ((data0>>2)&0x1);
1164
      if ((data[0] & 0x03) == 0x03) {
1165
         data++;
1166
         len--;
1167
         if (len<=0)
1168
            return OPUS_INVALID_PACKET;
1169
         if (data[0] & 0x40) {
1170
            int p;
1171
            int padding=0;
1172
            data++;
1173
            len--;
1174
            do {
1175
               int tmp;
1176
               if (len<=0)
1177
                  return OPUS_INVALID_PACKET;
1178
               p = *data++;
1179
               len--;
1180
               tmp = p==255 ? 254: p;
1181
               len -= tmp;
1182
               padding += tmp;
1183
            } while (p==255);
1184
            padding--;
1185
            if (len <= 0 || padding<0) return OPUS_INVALID_PACKET;
1186
#ifdef ENABLE_QEXT
1187
            qext_bytes = padding;
1188
            if (data[len] != QEXT_EXTENSION_ID<<1)
1189
               qext_bytes=0;
1190
            ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes);
1191
#endif
1192
         }
1193
      } else
1194
      {
1195
         data++;
1196
         len--;
1197
      }
1198
      if (LM>mode->maxLM)
1199
         return OPUS_INVALID_PACKET;
1200
      if (frame_size < mode->shortMdctSize<<LM)
1201
         return OPUS_BUFFER_TOO_SMALL;
1202
      else
1203
         frame_size = mode->shortMdctSize<<LM;
1204
   } else {
1205
#else
1206
230k
   {
1207
230k
#endif
1208
412k
      for (LM=0;LM<=mode->maxLM;LM++)
1209
412k
         if (mode->shortMdctSize<<LM==frame_size)
1210
230k
            break;
1211
230k
      if (LM>mode->maxLM)
1212
0
         return OPUS_BAD_ARG;
1213
230k
   }
1214
230k
   M=1<<LM;
1215
1216
230k
   if (len<0 || len>1275 || pcm==NULL)
1217
0
      return OPUS_BAD_ARG;
1218
1219
230k
   N = M*mode->shortMdctSize;
1220
330k
   c=0; do {
1221
330k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
1222
330k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
1223
330k
   } while (++c<CC);
1224
1225
230k
   effEnd = end;
1226
230k
   if (effEnd > mode->effEBands)
1227
0
      effEnd = mode->effEBands;
1228
1229
230k
   if (data == NULL || len<=1)
1230
84.7k
   {
1231
84.7k
      celt_decode_lost(st, N, LM
1232
#ifdef ENABLE_DEEP_PLC
1233
      , lpcnet
1234
#endif
1235
84.7k
                      );
1236
84.7k
      deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1237
84.7k
      RESTORE_STACK;
1238
84.7k
      return frame_size/st->downsample;
1239
84.7k
   }
1240
#ifdef ENABLE_DEEP_PLC
1241
   else {
1242
      /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */
1243
      if (lpcnet) lpcnet->blend = 0;
1244
   }
1245
#endif
1246
1247
   /* Check if there are at least two packets received consecutively before
1248
    * turning on the pitch-based PLC */
1249
145k
   if (st->loss_duration == 0) st->skip_plc = 0;
1250
1251
145k
   if (dec == NULL)
1252
18.8k
   {
1253
18.8k
      ec_dec_init(&_dec,(unsigned char*)data,len);
1254
18.8k
      dec = &_dec;
1255
18.8k
   }
1256
1257
145k
   if (C==1)
1258
93.9k
   {
1259
2.06M
      for (i=0;i<nbEBands;i++)
1260
1.97M
         oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]);
1261
93.9k
   }
1262
1263
145k
   total_bits = len*8;
1264
145k
   tell = ec_tell(dec);
1265
1266
145k
   if (tell >= total_bits)
1267
9.06k
      silence = 1;
1268
136k
   else if (tell==1)
1269
129k
      silence = ec_dec_bit_logp(dec, 15);
1270
6.93k
   else
1271
6.93k
      silence = 0;
1272
145k
   if (silence)
1273
14.0k
   {
1274
      /* Pretend we've read all the remaining bits */
1275
14.0k
      tell = len*8;
1276
14.0k
      dec->nbits_total+=tell-ec_tell(dec);
1277
14.0k
   }
1278
1279
145k
   postfilter_gain = 0;
1280
145k
   postfilter_pitch = 0;
1281
145k
   postfilter_tapset = 0;
1282
145k
   if (start==0 && tell+16 <= total_bits)
1283
104k
   {
1284
104k
      if(ec_dec_bit_logp(dec, 1))
1285
29.8k
      {
1286
29.8k
         int qg, octave;
1287
29.8k
         octave = ec_dec_uint(dec, 6);
1288
29.8k
         postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
1289
29.8k
         qg = ec_dec_bits(dec, 3);
1290
29.8k
         if (ec_tell(dec)+2<=total_bits)
1291
29.8k
            postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
1292
29.8k
         postfilter_gain = QCONST16(.09375f,15)*(qg+1);
1293
29.8k
      }
1294
104k
      tell = ec_tell(dec);
1295
104k
   }
1296
1297
145k
   if (LM > 0 && tell+3 <= total_bits)
1298
74.0k
   {
1299
74.0k
      isTransient = ec_dec_bit_logp(dec, 3);
1300
74.0k
      tell = ec_tell(dec);
1301
74.0k
   }
1302
71.4k
   else
1303
71.4k
      isTransient = 0;
1304
1305
145k
   if (isTransient)
1306
11.2k
      shortBlocks = M;
1307
134k
   else
1308
134k
      shortBlocks = 0;
1309
1310
   /* Decode the global flags (first symbols in the stream) */
1311
145k
   intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
1312
   /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the
1313
      risk of getting loud artifacts. */
1314
145k
   if (!intra_ener && st->loss_duration != 0) {
1315
28.7k
      c=0; do
1316
57.4k
      {
1317
57.4k
         celt_glog safety = 0;
1318
57.4k
         int missing = IMIN(10, st->loss_duration>>LM);
1319
57.4k
         if (LM==0) safety = GCONST(1.5f);
1320
7.06k
         else if (LM==1) safety = GCONST(.5f);
1321
1.04M
         for (i=start;i<end;i++)
1322
986k
         {
1323
986k
            if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) {
1324
               /* If energy is going down already, continue the trend. */
1325
412k
               opus_val32 slope;
1326
412k
               opus_val32 E0, E1, E2;
1327
412k
               E0 = oldBandE[c*nbEBands+i];
1328
412k
               E1 = oldLogE[c*nbEBands+i];
1329
412k
               E2 = oldLogE2[c*nbEBands+i];
1330
412k
               slope = MAX32(E1 - E0, HALF32(E2 - E0));
1331
412k
               slope = MING(slope, GCONST(2.f));
1332
412k
               E0 -= MAX32(0, (1+missing)*slope);
1333
412k
               oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0);
1334
574k
            } else {
1335
               /* Otherwise take the min of the last frames. */
1336
574k
               oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]);
1337
574k
            }
1338
            /* Shorter frames have more natural fluctuations -- play it safe. */
1339
986k
            oldBandE[c*nbEBands+i] -= safety;
1340
986k
         }
1341
57.4k
      } while (++c<2);
1342
28.7k
   }
1343
   /* Get band energies */
1344
145k
   unquant_coarse_energy(mode, start, end, oldBandE,
1345
145k
         intra_ener, dec, C, LM);
1346
1347
145k
   ALLOC(tf_res, nbEBands, int);
1348
145k
   tf_decode(start, end, isTransient, tf_res, LM, dec);
1349
1350
145k
   tell = ec_tell(dec);
1351
145k
   spread_decision = SPREAD_NORMAL;
1352
145k
   if (tell+4 <= total_bits)
1353
71.1k
      spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
1354
1355
145k
   ALLOC(cap, nbEBands, int);
1356
1357
145k
   init_caps(mode,cap,LM,C);
1358
1359
145k
   ALLOC(offsets, nbEBands, int);
1360
1361
145k
   dynalloc_logp = 6;
1362
145k
   total_bits<<=BITRES;
1363
145k
   tell = ec_tell_frac(dec);
1364
2.52M
   for (i=start;i<end;i++)
1365
2.37M
   {
1366
2.37M
      int width, quanta;
1367
2.37M
      int dynalloc_loop_logp;
1368
2.37M
      int boost;
1369
2.37M
      width = C*(eBands[i+1]-eBands[i])<<LM;
1370
      /* quanta is 6 bits, but no more than 1 bit/sample
1371
         and no less than 1/8 bit/sample */
1372
2.37M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1373
2.37M
      dynalloc_loop_logp = dynalloc_logp;
1374
2.37M
      boost = 0;
1375
2.42M
      while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
1376
1.11M
      {
1377
1.11M
         int flag;
1378
1.11M
         flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
1379
1.11M
         tell = ec_tell_frac(dec);
1380
1.11M
         if (!flag)
1381
1.06M
            break;
1382
50.4k
         boost += quanta;
1383
50.4k
         total_bits -= quanta;
1384
50.4k
         dynalloc_loop_logp = 1;
1385
50.4k
      }
1386
2.37M
      offsets[i] = boost;
1387
      /* Making dynalloc more likely */
1388
2.37M
      if (boost>0)
1389
14.9k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
1390
2.37M
   }
1391
1392
145k
   ALLOC(fine_quant, nbEBands, int);
1393
145k
   alloc_trim = tell+(6<<BITRES) <= total_bits ?
1394
81.1k
         ec_dec_icdf(dec, trim_icdf, 7) : 5;
1395
1396
145k
   bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1397
145k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1398
145k
   bits -= anti_collapse_rsv;
1399
1400
145k
   ALLOC(pulses, nbEBands, int);
1401
145k
   ALLOC(fine_priority, nbEBands, int);
1402
1403
145k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
1404
145k
         alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1405
145k
         fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
1406
1407
145k
   unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C);
1408
1409
145k
   ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
1410
1411
145k
#ifdef ENABLE_QEXT
1412
145k
   if (qext_bytes && end == nbEBands &&
1413
7.86k
         ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90))
1414
7.86k
       || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) {
1415
7.86k
      int qext_intra_ener;
1416
7.86k
      qext_oldBandE = backgroundLogE + 2*nbEBands;
1417
7.86k
      compute_qext_mode(&qext_mode_struct, mode);
1418
7.86k
      qext_mode = &qext_mode_struct;
1419
7.86k
      qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2;
1420
7.86k
      if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo);
1421
7.86k
      qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0;
1422
7.86k
      unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE,
1423
7.86k
            qext_intra_ener, &ext_dec, C, LM);
1424
7.86k
   }
1425
145k
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
1426
145k
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
1427
145k
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1428
145k
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL,
1429
145k
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0);
1430
145k
   if (qext_bytes > 0) {
1431
10.3k
      unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C);
1432
10.3k
   }
1433
145k
#endif
1434
1435
208k
   c=0; do {
1436
208k
      OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap);
1437
208k
   } while (++c<CC);
1438
1439
   /* Decode fixed codebook */
1440
145k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
1441
1442
145k
   quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
1443
145k
         NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
1444
145k
         len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
1445
145k
         st->arch, st->disable_inv
1446
145k
         ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses)
1447
145k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
1448
1449
145k
#ifdef ENABLE_QEXT
1450
145k
   if (qext_mode) {
1451
7.86k
      VARDECL(int, zeros);
1452
7.86k
      VARDECL(unsigned char, qext_collapse_masks);
1453
7.86k
      ec_dec dummy_dec;
1454
7.86k
      int ext_balance;
1455
7.86k
      ALLOC(zeros, nbEBands, int);
1456
7.86k
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
1457
7.86k
      ec_dec_init(&dummy_dec, NULL, 0);
1458
7.86k
      OPUS_CLEAR(zeros, end);
1459
7.86k
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec);
1460
47.8k
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
1461
7.86k
      unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C);
1462
7.86k
      quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
1463
7.86k
            NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros,
1464
7.86k
            qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0,
1465
7.86k
            st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL);
1466
7.86k
   }
1467
145k
#endif
1468
1469
145k
   if (anti_collapse_rsv > 0)
1470
4.67k
   {
1471
4.67k
      anti_collapse_on = ec_dec_bits(dec, 1);
1472
4.67k
   }
1473
145k
   unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE,
1474
145k
         fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
1475
145k
   if (anti_collapse_on)
1476
3.55k
      anti_collapse(mode, X, collapse_masks, LM, C, N,
1477
3.55k
            start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch);
1478
1479
145k
   if (silence)
1480
14.0k
   {
1481
521k
      for (i=0;i<C*nbEBands;i++)
1482
507k
         oldBandE[i] = -GCONST(28.f);
1483
14.0k
   }
1484
145k
   if (st->prefilter_and_fold) {
1485
28.7k
      prefilter_and_fold(st, N);
1486
28.7k
   }
1487
145k
   celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1488
145k
                  C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
1489
1490
208k
   c=0; do {
1491
208k
      st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1492
208k
      st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1493
208k
      comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1494
208k
            st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
1495
208k
            mode->window, overlap, st->arch);
1496
208k
      if (LM!=0)
1497
121k
         comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1498
121k
               st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
1499
121k
               mode->window, overlap, st->arch);
1500
1501
208k
   } while (++c<CC);
1502
145k
   st->postfilter_period_old = st->postfilter_period;
1503
145k
   st->postfilter_gain_old = st->postfilter_gain;
1504
145k
   st->postfilter_tapset_old = st->postfilter_tapset;
1505
145k
   st->postfilter_period = postfilter_pitch;
1506
145k
   st->postfilter_gain = postfilter_gain;
1507
145k
   st->postfilter_tapset = postfilter_tapset;
1508
145k
   if (LM!=0)
1509
84.2k
   {
1510
84.2k
      st->postfilter_period_old = st->postfilter_period;
1511
84.2k
      st->postfilter_gain_old = st->postfilter_gain;
1512
84.2k
      st->postfilter_tapset_old = st->postfilter_tapset;
1513
84.2k
   }
1514
1515
145k
   if (C==1)
1516
93.9k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
1517
1518
145k
   if (!isTransient)
1519
134k
   {
1520
134k
      OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1521
134k
      OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
1522
134k
   } else {
1523
483k
      for (i=0;i<2*nbEBands;i++)
1524
472k
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
1525
11.2k
   }
1526
   /* In normal circumstances, we only allow the noise floor to increase by
1527
      up to 2.4 dB/second, but when we're in DTX we give the weight of
1528
      all missing packets to the update packet. */
1529
145k
   max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f);
1530
6.25M
   for (i=0;i<2*nbEBands;i++)
1531
6.10M
      backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]);
1532
   /* In case start or end were to change */
1533
145k
   c=0; do
1534
290k
   {
1535
835k
      for (i=0;i<start;i++)
1536
544k
      {
1537
544k
         oldBandE[c*nbEBands+i]=0;
1538
544k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1539
544k
      }
1540
1.10M
      for (i=end;i<nbEBands;i++)
1541
814k
      {
1542
814k
         oldBandE[c*nbEBands+i]=0;
1543
814k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1544
814k
      }
1545
290k
   } while (++c<2);
1546
145k
   st->rng = dec->rng;
1547
145k
#ifdef ENABLE_QEXT
1548
145k
   if (qext_bytes) st->rng = st->rng ^ ext_dec.rng;
1549
145k
#endif
1550
1551
145k
   deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1552
145k
   st->loss_duration = 0;
1553
145k
   st->prefilter_and_fold = 0;
1554
145k
   RESTORE_STACK;
1555
145k
   if (ec_tell(dec) > 8*len)
1556
0
      return OPUS_INTERNAL_ERROR;
1557
145k
#ifdef ENABLE_QEXT
1558
145k
   if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes)
1559
0
      return OPUS_INTERNAL_ERROR;
1560
145k
#endif
1561
145k
   if(ec_get_error(dec))
1562
2.47k
      st->error = 1;
1563
145k
   return frame_size/st->downsample;
1564
145k
}
celt_decode_with_ec_dred
Line
Count
Source
1056
260k
{
1057
260k
   int c, i, N;
1058
260k
   int spread_decision;
1059
260k
   opus_int32 bits;
1060
260k
   ec_dec _dec;
1061
260k
   VARDECL(celt_norm, X);
1062
260k
   VARDECL(int, fine_quant);
1063
260k
   VARDECL(int, pulses);
1064
260k
   VARDECL(int, cap);
1065
260k
   VARDECL(int, offsets);
1066
260k
   VARDECL(int, fine_priority);
1067
260k
   VARDECL(int, tf_res);
1068
260k
   VARDECL(unsigned char, collapse_masks);
1069
260k
   celt_sig *decode_mem[2];
1070
260k
   celt_sig *out_syn[2];
1071
260k
   opus_val16 *lpc;
1072
260k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
1073
1074
260k
   int shortBlocks;
1075
260k
   int isTransient;
1076
260k
   int intra_ener;
1077
260k
   const int CC = st->channels;
1078
260k
   int LM, M;
1079
260k
   int start;
1080
260k
   int end;
1081
260k
   int effEnd;
1082
260k
   int codedBands;
1083
260k
   int alloc_trim;
1084
260k
   int postfilter_pitch;
1085
260k
   opus_val16 postfilter_gain;
1086
260k
   int intensity=0;
1087
260k
   int dual_stereo=0;
1088
260k
   opus_int32 total_bits;
1089
260k
   opus_int32 balance;
1090
260k
   opus_int32 tell;
1091
260k
   int dynalloc_logp;
1092
260k
   int postfilter_tapset;
1093
260k
   int anti_collapse_rsv;
1094
260k
   int anti_collapse_on=0;
1095
260k
   int silence;
1096
260k
   int C = st->stream_channels;
1097
260k
   const OpusCustomMode *mode;
1098
260k
   int nbEBands;
1099
260k
   int overlap;
1100
260k
   const opus_int16 *eBands;
1101
260k
   celt_glog max_background_increase;
1102
260k
   int decode_buffer_size;
1103
#ifdef ENABLE_QEXT
1104
   opus_int32 qext_bits;
1105
   ec_dec ext_dec;
1106
   int qext_bytes=0;
1107
   int qext_end=0;
1108
   int qext_intensity=0;
1109
   int qext_dual_stereo=0;
1110
   VARDECL(int, extra_quant);
1111
   VARDECL(int, extra_pulses);
1112
   const CELTMode *qext_mode = NULL;
1113
   CELTMode qext_mode_struct;
1114
   celt_glog *qext_oldBandE=NULL;
1115
   int qext_scale;
1116
#else
1117
260k
# define qext_bytes 0
1118
260k
#endif
1119
260k
   ALLOC_STACK;
1120
#ifdef ENABLE_QEXT
1121
   qext_scale = st->qext_scale;
1122
#endif
1123
260k
   decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1124
1125
260k
   VALIDATE_CELT_DECODER(st);
1126
260k
   mode = st->mode;
1127
260k
   nbEBands = mode->nbEBands;
1128
260k
   overlap = mode->overlap;
1129
260k
   eBands = mode->eBands;
1130
260k
   start = st->start;
1131
260k
   end = st->end;
1132
260k
   frame_size *= st->downsample;
1133
1134
260k
   lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+overlap)*CC);
1135
260k
   oldBandE = (celt_glog*)(lpc+CC*CELT_LPC_ORDER);
1136
260k
   oldLogE = oldBandE + 2*nbEBands;
1137
260k
   oldLogE2 = oldLogE + 2*nbEBands;
1138
260k
   backgroundLogE = oldLogE2  + 2*nbEBands;
1139
1140
#ifdef ENABLE_QEXT
1141
   if (qext_payload) {
1142
      ec_dec_init(&ext_dec, (unsigned char*)qext_payload, qext_payload_len);
1143
      qext_bytes = qext_payload_len;
1144
   } else {
1145
      ec_dec_init(&ext_dec, NULL, 0);
1146
   }
1147
#endif
1148
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1149
   if (st->signalling && data!=NULL)
1150
   {
1151
      int data0=data[0];
1152
      /* Convert "standard mode" to Opus header */
1153
# ifndef ENABLE_QEXT
1154
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1155
# endif
1156
      {
1157
         data0 = fromOpus(data0);
1158
         if (data0<0)
1159
            return OPUS_INVALID_PACKET;
1160
      }
1161
      st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
1162
      LM = (data0>>3)&0x3;
1163
      C = 1 + ((data0>>2)&0x1);
1164
      if ((data[0] & 0x03) == 0x03) {
1165
         data++;
1166
         len--;
1167
         if (len<=0)
1168
            return OPUS_INVALID_PACKET;
1169
         if (data[0] & 0x40) {
1170
            int p;
1171
            int padding=0;
1172
            data++;
1173
            len--;
1174
            do {
1175
               int tmp;
1176
               if (len<=0)
1177
                  return OPUS_INVALID_PACKET;
1178
               p = *data++;
1179
               len--;
1180
               tmp = p==255 ? 254: p;
1181
               len -= tmp;
1182
               padding += tmp;
1183
            } while (p==255);
1184
            padding--;
1185
            if (len <= 0 || padding<0) return OPUS_INVALID_PACKET;
1186
#ifdef ENABLE_QEXT
1187
            qext_bytes = padding;
1188
            if (data[len] != QEXT_EXTENSION_ID<<1)
1189
               qext_bytes=0;
1190
            ec_dec_init(&ext_dec, (unsigned char*)data+len+1, qext_bytes);
1191
#endif
1192
         }
1193
      } else
1194
      {
1195
         data++;
1196
         len--;
1197
      }
1198
      if (LM>mode->maxLM)
1199
         return OPUS_INVALID_PACKET;
1200
      if (frame_size < mode->shortMdctSize<<LM)
1201
         return OPUS_BUFFER_TOO_SMALL;
1202
      else
1203
         frame_size = mode->shortMdctSize<<LM;
1204
   } else {
1205
#else
1206
260k
   {
1207
260k
#endif
1208
500k
      for (LM=0;LM<=mode->maxLM;LM++)
1209
500k
         if (mode->shortMdctSize<<LM==frame_size)
1210
260k
            break;
1211
260k
      if (LM>mode->maxLM)
1212
0
         return OPUS_BAD_ARG;
1213
260k
   }
1214
260k
   M=1<<LM;
1215
1216
260k
   if (len<0 || len>1275 || pcm==NULL)
1217
0
      return OPUS_BAD_ARG;
1218
1219
260k
   N = M*mode->shortMdctSize;
1220
408k
   c=0; do {
1221
408k
      decode_mem[c] = st->_decode_mem + c*(decode_buffer_size+overlap);
1222
408k
      out_syn[c] = decode_mem[c]+decode_buffer_size-N;
1223
408k
   } while (++c<CC);
1224
1225
260k
   effEnd = end;
1226
260k
   if (effEnd > mode->effEBands)
1227
0
      effEnd = mode->effEBands;
1228
1229
260k
   if (data == NULL || len<=1)
1230
110k
   {
1231
110k
      celt_decode_lost(st, N, LM
1232
#ifdef ENABLE_DEEP_PLC
1233
      , lpcnet
1234
#endif
1235
110k
                      );
1236
110k
      deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1237
110k
      RESTORE_STACK;
1238
110k
      return frame_size/st->downsample;
1239
110k
   }
1240
#ifdef ENABLE_DEEP_PLC
1241
   else {
1242
      /* FIXME: This is a bit of a hack just to make sure opus_decode_native() knows we're no longer in PLC. */
1243
      if (lpcnet) lpcnet->blend = 0;
1244
   }
1245
#endif
1246
1247
   /* Check if there are at least two packets received consecutively before
1248
    * turning on the pitch-based PLC */
1249
149k
   if (st->loss_duration == 0) st->skip_plc = 0;
1250
1251
149k
   if (dec == NULL)
1252
13.8k
   {
1253
13.8k
      ec_dec_init(&_dec,(unsigned char*)data,len);
1254
13.8k
      dec = &_dec;
1255
13.8k
   }
1256
1257
149k
   if (C==1)
1258
95.3k
   {
1259
2.09M
      for (i=0;i<nbEBands;i++)
1260
2.00M
         oldBandE[i]=MAXG(oldBandE[i],oldBandE[nbEBands+i]);
1261
95.3k
   }
1262
1263
149k
   total_bits = len*8;
1264
149k
   tell = ec_tell(dec);
1265
1266
149k
   if (tell >= total_bits)
1267
13.1k
      silence = 1;
1268
136k
   else if (tell==1)
1269
131k
      silence = ec_dec_bit_logp(dec, 15);
1270
4.82k
   else
1271
4.82k
      silence = 0;
1272
149k
   if (silence)
1273
19.9k
   {
1274
      /* Pretend we've read all the remaining bits */
1275
19.9k
      tell = len*8;
1276
19.9k
      dec->nbits_total+=tell-ec_tell(dec);
1277
19.9k
   }
1278
1279
149k
   postfilter_gain = 0;
1280
149k
   postfilter_pitch = 0;
1281
149k
   postfilter_tapset = 0;
1282
149k
   if (start==0 && tell+16 <= total_bits)
1283
76.2k
   {
1284
76.2k
      if(ec_dec_bit_logp(dec, 1))
1285
22.5k
      {
1286
22.5k
         int qg, octave;
1287
22.5k
         octave = ec_dec_uint(dec, 6);
1288
22.5k
         postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
1289
22.5k
         qg = ec_dec_bits(dec, 3);
1290
22.5k
         if (ec_tell(dec)+2<=total_bits)
1291
22.5k
            postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
1292
22.5k
         postfilter_gain = QCONST16(.09375f,15)*(qg+1);
1293
22.5k
      }
1294
76.2k
      tell = ec_tell(dec);
1295
76.2k
   }
1296
1297
149k
   if (LM > 0 && tell+3 <= total_bits)
1298
66.4k
   {
1299
66.4k
      isTransient = ec_dec_bit_logp(dec, 3);
1300
66.4k
      tell = ec_tell(dec);
1301
66.4k
   }
1302
83.0k
   else
1303
83.0k
      isTransient = 0;
1304
1305
149k
   if (isTransient)
1306
14.4k
      shortBlocks = M;
1307
135k
   else
1308
135k
      shortBlocks = 0;
1309
1310
   /* Decode the global flags (first symbols in the stream) */
1311
149k
   intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
1312
   /* If recovering from packet loss, make sure we make the energy prediction safe to reduce the
1313
      risk of getting loud artifacts. */
1314
149k
   if (!intra_ener && st->loss_duration != 0) {
1315
29.1k
      c=0; do
1316
58.3k
      {
1317
58.3k
         celt_glog safety = 0;
1318
58.3k
         int missing = IMIN(10, st->loss_duration>>LM);
1319
58.3k
         if (LM==0) safety = GCONST(1.5f);
1320
6.79k
         else if (LM==1) safety = GCONST(.5f);
1321
1.06M
         for (i=start;i<end;i++)
1322
1.00M
         {
1323
1.00M
            if (oldBandE[c*nbEBands+i] < MAXG(oldLogE[c*nbEBands+i], oldLogE2[c*nbEBands+i])) {
1324
               /* If energy is going down already, continue the trend. */
1325
421k
               opus_val32 slope;
1326
421k
               opus_val32 E0, E1, E2;
1327
421k
               E0 = oldBandE[c*nbEBands+i];
1328
421k
               E1 = oldLogE[c*nbEBands+i];
1329
421k
               E2 = oldLogE2[c*nbEBands+i];
1330
421k
               slope = MAX32(E1 - E0, HALF32(E2 - E0));
1331
421k
               slope = MING(slope, GCONST(2.f));
1332
421k
               E0 -= MAX32(0, (1+missing)*slope);
1333
421k
               oldBandE[c*nbEBands+i] = MAX32(-GCONST(20.f), E0);
1334
583k
            } else {
1335
               /* Otherwise take the min of the last frames. */
1336
583k
               oldBandE[c*nbEBands+i] = MING(MING(oldBandE[c*nbEBands+i], oldLogE[c*nbEBands+i]), oldLogE2[c*nbEBands+i]);
1337
583k
            }
1338
            /* Shorter frames have more natural fluctuations -- play it safe. */
1339
1.00M
            oldBandE[c*nbEBands+i] -= safety;
1340
1.00M
         }
1341
58.3k
      } while (++c<2);
1342
29.1k
   }
1343
   /* Get band energies */
1344
149k
   unquant_coarse_energy(mode, start, end, oldBandE,
1345
149k
         intra_ener, dec, C, LM);
1346
1347
149k
   ALLOC(tf_res, nbEBands, int);
1348
149k
   tf_decode(start, end, isTransient, tf_res, LM, dec);
1349
1350
149k
   tell = ec_tell(dec);
1351
149k
   spread_decision = SPREAD_NORMAL;
1352
149k
   if (tell+4 <= total_bits)
1353
47.5k
      spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
1354
1355
149k
   ALLOC(cap, nbEBands, int);
1356
1357
149k
   init_caps(mode,cap,LM,C);
1358
1359
149k
   ALLOC(offsets, nbEBands, int);
1360
1361
149k
   dynalloc_logp = 6;
1362
149k
   total_bits<<=BITRES;
1363
149k
   tell = ec_tell_frac(dec);
1364
2.54M
   for (i=start;i<end;i++)
1365
2.39M
   {
1366
2.39M
      int width, quanta;
1367
2.39M
      int dynalloc_loop_logp;
1368
2.39M
      int boost;
1369
2.39M
      width = C*(eBands[i+1]-eBands[i])<<LM;
1370
      /* quanta is 6 bits, but no more than 1 bit/sample
1371
         and no less than 1/8 bit/sample */
1372
2.39M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1373
2.39M
      dynalloc_loop_logp = dynalloc_logp;
1374
2.39M
      boost = 0;
1375
2.44M
      while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
1376
702k
      {
1377
702k
         int flag;
1378
702k
         flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
1379
702k
         tell = ec_tell_frac(dec);
1380
702k
         if (!flag)
1381
656k
            break;
1382
46.1k
         boost += quanta;
1383
46.1k
         total_bits -= quanta;
1384
46.1k
         dynalloc_loop_logp = 1;
1385
46.1k
      }
1386
2.39M
      offsets[i] = boost;
1387
      /* Making dynalloc more likely */
1388
2.39M
      if (boost>0)
1389
12.0k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
1390
2.39M
   }
1391
1392
149k
   ALLOC(fine_quant, nbEBands, int);
1393
149k
   alloc_trim = tell+(6<<BITRES) <= total_bits ?
1394
108k
         ec_dec_icdf(dec, trim_icdf, 7) : 5;
1395
1396
149k
   bits = (((opus_int32)len*8)<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1397
149k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1398
149k
   bits -= anti_collapse_rsv;
1399
1400
149k
   ALLOC(pulses, nbEBands, int);
1401
149k
   ALLOC(fine_priority, nbEBands, int);
1402
1403
149k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
1404
149k
         alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1405
149k
         fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
1406
1407
149k
   unquant_fine_energy(mode, start, end, oldBandE, NULL, fine_quant, dec, C);
1408
1409
149k
   ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
1410
1411
#ifdef ENABLE_QEXT
1412
   if (qext_bytes && end == nbEBands &&
1413
         ((mode->Fs == 48000 && (mode->shortMdctSize==120 || mode->shortMdctSize==90))
1414
       || (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)))) {
1415
      int qext_intra_ener;
1416
      qext_oldBandE = backgroundLogE + 2*nbEBands;
1417
      compute_qext_mode(&qext_mode_struct, mode);
1418
      qext_mode = &qext_mode_struct;
1419
      qext_end = ec_dec_bit_logp(&ext_dec, 1) ? NB_QEXT_BANDS : 2;
1420
      if (C==2) decode_qext_stereo_params(&ext_dec, qext_end, &qext_intensity, &qext_dual_stereo);
1421
      qext_intra_ener = ec_tell(&ext_dec)+3<=qext_bytes*8 ? ec_dec_bit_logp(&ext_dec, 3) : 0;
1422
      unquant_coarse_energy(qext_mode, 0, qext_end, qext_oldBandE,
1423
            qext_intra_ener, &ext_dec, C, LM);
1424
   }
1425
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
1426
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
1427
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(dec) - 1;
1428
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, NULL, NULL,
1429
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_dec, 0, 0, 0);
1430
   if (qext_bytes > 0) {
1431
      unquant_fine_energy(mode, start, end, oldBandE, fine_quant, extra_quant, &ext_dec, C);
1432
   }
1433
#endif
1434
1435
231k
   c=0; do {
1436
231k
      OPUS_MOVE(decode_mem[c], decode_mem[c]+N, decode_buffer_size-N+overlap);
1437
231k
   } while (++c<CC);
1438
1439
   /* Decode fixed codebook */
1440
149k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
1441
1442
149k
   quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
1443
149k
         NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
1444
149k
         len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
1445
149k
         st->arch, st->disable_inv
1446
149k
         ARG_QEXT(&ext_dec) ARG_QEXT(extra_pulses)
1447
149k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
1448
1449
#ifdef ENABLE_QEXT
1450
   if (qext_mode) {
1451
      VARDECL(int, zeros);
1452
      VARDECL(unsigned char, qext_collapse_masks);
1453
      ec_dec dummy_dec;
1454
      int ext_balance;
1455
      ALLOC(zeros, nbEBands, int);
1456
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
1457
      ec_dec_init(&dummy_dec, NULL, 0);
1458
      OPUS_CLEAR(zeros, end);
1459
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_dec);
1460
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
1461
      unquant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, NULL, &extra_quant[nbEBands], &ext_dec, C);
1462
      quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
1463
            NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros,
1464
            qext_bytes*(8<<BITRES), ext_balance, &ext_dec, LM, qext_end, &st->rng, 0,
1465
            st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL);
1466
   }
1467
#endif
1468
1469
149k
   if (anti_collapse_rsv > 0)
1470
4.09k
   {
1471
4.09k
      anti_collapse_on = ec_dec_bits(dec, 1);
1472
4.09k
   }
1473
149k
   unquant_energy_finalise(mode, start, end, (qext_bytes > 0) ? NULL : oldBandE,
1474
149k
         fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
1475
149k
   if (anti_collapse_on)
1476
3.01k
      anti_collapse(mode, X, collapse_masks, LM, C, N,
1477
3.01k
            start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 0, st->arch);
1478
1479
149k
   if (silence)
1480
19.9k
   {
1481
682k
      for (i=0;i<C*nbEBands;i++)
1482
662k
         oldBandE[i] = -GCONST(28.f);
1483
19.9k
   }
1484
149k
   if (st->prefilter_and_fold) {
1485
30.4k
      prefilter_and_fold(st, N);
1486
30.4k
   }
1487
149k
   celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1488
149k
                  C, CC, isTransient, LM, st->downsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
1489
1490
231k
   c=0; do {
1491
231k
      st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1492
231k
      st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1493
231k
      comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1494
231k
            st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
1495
231k
            mode->window, overlap, st->arch);
1496
231k
      if (LM!=0)
1497
117k
         comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1498
117k
               st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
1499
117k
               mode->window, overlap, st->arch);
1500
1501
231k
   } while (++c<CC);
1502
149k
   st->postfilter_period_old = st->postfilter_period;
1503
149k
   st->postfilter_gain_old = st->postfilter_gain;
1504
149k
   st->postfilter_tapset_old = st->postfilter_tapset;
1505
149k
   st->postfilter_period = postfilter_pitch;
1506
149k
   st->postfilter_gain = postfilter_gain;
1507
149k
   st->postfilter_tapset = postfilter_tapset;
1508
149k
   if (LM!=0)
1509
81.8k
   {
1510
81.8k
      st->postfilter_period_old = st->postfilter_period;
1511
81.8k
      st->postfilter_gain_old = st->postfilter_gain;
1512
81.8k
      st->postfilter_tapset_old = st->postfilter_tapset;
1513
81.8k
   }
1514
1515
149k
   if (C==1)
1516
95.3k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
1517
1518
149k
   if (!isTransient)
1519
135k
   {
1520
135k
      OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1521
135k
      OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
1522
135k
   } else {
1523
620k
      for (i=0;i<2*nbEBands;i++)
1524
606k
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
1525
14.4k
   }
1526
   /* In normal circumstances, we only allow the noise floor to increase by
1527
      up to 2.4 dB/second, but when we're in DTX we give the weight of
1528
      all missing packets to the update packet. */
1529
149k
   max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f);
1530
6.42M
   for (i=0;i<2*nbEBands;i++)
1531
6.27M
      backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]);
1532
   /* In case start or end were to change */
1533
149k
   c=0; do
1534
298k
   {
1535
910k
      for (i=0;i<start;i++)
1536
611k
      {
1537
611k
         oldBandE[c*nbEBands+i]=0;
1538
611k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1539
611k
      }
1540
1.17M
      for (i=end;i<nbEBands;i++)
1541
873k
      {
1542
873k
         oldBandE[c*nbEBands+i]=0;
1543
873k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
1544
873k
      }
1545
298k
   } while (++c<2);
1546
149k
   st->rng = dec->rng;
1547
#ifdef ENABLE_QEXT
1548
   if (qext_bytes) st->rng = st->rng ^ ext_dec.rng;
1549
#endif
1550
1551
149k
   deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
1552
149k
   st->loss_duration = 0;
1553
149k
   st->prefilter_and_fold = 0;
1554
149k
   RESTORE_STACK;
1555
149k
   if (ec_tell(dec) > 8*len)
1556
4
      return OPUS_INTERNAL_ERROR;
1557
#ifdef ENABLE_QEXT
1558
   if (qext_bytes != 0 && ec_tell(&ext_dec) > 8*qext_bytes)
1559
      return OPUS_INTERNAL_ERROR;
1560
#endif
1561
149k
   if(ec_get_error(dec))
1562
1.61k
      st->error = 1;
1563
149k
   return frame_size/st->downsample;
1564
149k
}
1565
1566
int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data,
1567
      int len, opus_res * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum)
1568
65.4k
{
1569
65.4k
   return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum
1570
#ifdef ENABLE_DEEP_PLC
1571
       , NULL
1572
#endif
1573
65.4k
       ARG_QEXT(NULL) ARG_QEXT(0)
1574
65.4k
       );
1575
65.4k
}
celt_decode_with_ec
Line
Count
Source
1568
32.7k
{
1569
32.7k
   return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum
1570
#ifdef ENABLE_DEEP_PLC
1571
       , NULL
1572
#endif
1573
32.7k
       ARG_QEXT(NULL) ARG_QEXT(0)
1574
32.7k
       );
1575
32.7k
}
celt_decode_with_ec
Line
Count
Source
1568
32.7k
{
1569
32.7k
   return celt_decode_with_ec_dred(st, data, len, pcm, frame_size, dec, accum
1570
#ifdef ENABLE_DEEP_PLC
1571
       , NULL
1572
#endif
1573
32.7k
       ARG_QEXT(NULL) ARG_QEXT(0)
1574
32.7k
       );
1575
32.7k
}
1576
1577
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1578
1579
#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
1580
int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
1581
{
1582
   return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
1583
}
1584
#else
1585
int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
1586
{
1587
   int j, ret, C, N;
1588
   VARDECL(opus_res, out);
1589
   ALLOC_STACK;
1590
1591
   if (pcm==NULL)
1592
      return OPUS_BAD_ARG;
1593
1594
   C = st->channels;
1595
   N = frame_size;
1596
1597
   ALLOC(out, C*N, opus_res);
1598
   ret = celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
1599
   if (ret>0)
1600
      for (j=0;j<C*ret;j++)
1601
         pcm[j]=RES2INT16(out[j]);
1602
1603
   RESTORE_STACK;
1604
   return ret;
1605
}
1606
#endif
1607
1608
#if defined(FIXED_POINT) && defined(ENABLE_RES24)
1609
int opus_custom_decode24(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int32 * OPUS_RESTRICT pcm, int frame_size)
1610
{
1611
   return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
1612
}
1613
#else
1614
int opus_custom_decode24(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int32 * OPUS_RESTRICT pcm, int frame_size)
1615
{
1616
   int j, ret, C, N;
1617
   VARDECL(opus_res, out);
1618
   ALLOC_STACK;
1619
1620
   if (pcm==NULL)
1621
      return OPUS_BAD_ARG;
1622
1623
   C = st->channels;
1624
   N = frame_size;
1625
1626
   ALLOC(out, C*N, opus_res);
1627
   ret = celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
1628
   if (ret>0)
1629
      for (j=0;j<C*ret;j++)
1630
         pcm[j]=RES2INT24(out[j]);
1631
1632
   RESTORE_STACK;
1633
   return ret;
1634
}
1635
#endif
1636
1637
1638
#ifndef DISABLE_FLOAT_API
1639
1640
# if !defined(FIXED_POINT)
1641
int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
1642
{
1643
   return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
1644
}
1645
# else
1646
int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
1647
{
1648
   int j, ret, C, N;
1649
   VARDECL(opus_res, out);
1650
   ALLOC_STACK;
1651
1652
   if (pcm==NULL)
1653
      return OPUS_BAD_ARG;
1654
1655
   C = st->channels;
1656
   N = frame_size;
1657
1658
   ALLOC(out, C*N, opus_res);
1659
   ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
1660
   if (ret>0)
1661
      for (j=0;j<C*ret;j++)
1662
         pcm[j]=RES2FLOAT(out[j]);
1663
1664
   RESTORE_STACK;
1665
   return ret;
1666
}
1667
# endif
1668
1669
#endif
1670
1671
#endif /* CUSTOM_MODES */
1672
1673
int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
1674
7.43M
{
1675
7.43M
   va_list ap;
1676
1677
7.43M
   va_start(ap, request);
1678
7.43M
   switch (request)
1679
7.43M
   {
1680
0
      case OPUS_SET_COMPLEXITY_REQUEST:
1681
0
      {
1682
0
          opus_int32 value = va_arg(ap, opus_int32);
1683
0
          if(value<0 || value>10)
1684
0
          {
1685
0
             goto bad_arg;
1686
0
          }
1687
0
          st->complexity = value;
1688
0
      }
1689
0
      break;
1690
0
      case OPUS_GET_COMPLEXITY_REQUEST:
1691
0
      {
1692
0
          opus_int32 *value = va_arg(ap, opus_int32*);
1693
0
          if (!value)
1694
0
          {
1695
0
             goto bad_arg;
1696
0
          }
1697
0
          *value = st->complexity;
1698
0
      }
1699
0
      break;
1700
1.36M
      case CELT_SET_START_BAND_REQUEST:
1701
1.36M
      {
1702
1.36M
         opus_int32 value = va_arg(ap, opus_int32);
1703
1.36M
         if (value<0 || value>=st->mode->nbEBands)
1704
0
            goto bad_arg;
1705
1.36M
         st->start = value;
1706
1.36M
      }
1707
0
      break;
1708
841k
      case CELT_SET_END_BAND_REQUEST:
1709
841k
      {
1710
841k
         opus_int32 value = va_arg(ap, opus_int32);
1711
841k
         if (value<1 || value>st->mode->nbEBands)
1712
0
            goto bad_arg;
1713
841k
         st->end = value;
1714
841k
      }
1715
0
      break;
1716
1.30M
      case CELT_SET_CHANNELS_REQUEST:
1717
1.30M
      {
1718
1.30M
         opus_int32 value = va_arg(ap, opus_int32);
1719
1.30M
         if (value<1 || value>2)
1720
0
            goto bad_arg;
1721
1.30M
         st->stream_channels = value;
1722
1.30M
      }
1723
0
      break;
1724
0
      case CELT_GET_AND_CLEAR_ERROR_REQUEST:
1725
0
      {
1726
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1727
0
         if (value==NULL)
1728
0
            goto bad_arg;
1729
0
         *value=st->error;
1730
0
         st->error = 0;
1731
0
      }
1732
0
      break;
1733
0
      case OPUS_GET_LOOKAHEAD_REQUEST:
1734
0
      {
1735
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1736
0
         if (value==NULL)
1737
0
            goto bad_arg;
1738
0
         *value = st->overlap/st->downsample;
1739
0
      }
1740
0
      break;
1741
834k
      case OPUS_RESET_STATE:
1742
834k
      {
1743
834k
         int i;
1744
834k
         opus_val16 *lpc;
1745
834k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
1746
834k
         int decode_buffer_size;
1747
#ifdef ENABLE_QEXT
1748
         int qext_scale = st->qext_scale;
1749
#endif
1750
834k
         decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1751
834k
         lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+st->overlap)*st->channels);
1752
834k
         oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER);
1753
834k
         oldLogE = oldBandE + 2*st->mode->nbEBands;
1754
834k
         oldLogE2 = oldLogE + 2*st->mode->nbEBands;
1755
834k
         OPUS_CLEAR((char*)&st->DECODER_RESET_START,
1756
834k
               opus_custom_decoder_get_size(st->mode, st->channels)-
1757
834k
               ((char*)&st->DECODER_RESET_START - (char*)st));
1758
35.8M
         for (i=0;i<2*st->mode->nbEBands;i++)
1759
35.0M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
1760
834k
         st->skip_plc = 1;
1761
834k
      }
1762
834k
      break;
1763
0
      case OPUS_GET_PITCH_REQUEST:
1764
0
      {
1765
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1766
0
         if (value==NULL)
1767
0
            goto bad_arg;
1768
0
         *value = st->postfilter_period;
1769
0
      }
1770
0
      break;
1771
1.30M
      case CELT_GET_MODE_REQUEST:
1772
1.30M
      {
1773
1.30M
         const CELTMode ** value = va_arg(ap, const CELTMode**);
1774
1.30M
         if (value==0)
1775
0
            goto bad_arg;
1776
1.30M
         *value=st->mode;
1777
1.30M
      }
1778
0
      break;
1779
807k
      case CELT_SET_SIGNALLING_REQUEST:
1780
807k
      {
1781
807k
         opus_int32 value = va_arg(ap, opus_int32);
1782
807k
         st->signalling = value;
1783
807k
      }
1784
807k
      break;
1785
980k
      case OPUS_GET_FINAL_RANGE_REQUEST:
1786
980k
      {
1787
980k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
1788
980k
         if (value==0)
1789
0
            goto bad_arg;
1790
980k
         *value=st->rng;
1791
980k
      }
1792
0
      break;
1793
0
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
1794
0
      {
1795
0
          opus_int32 value = va_arg(ap, opus_int32);
1796
0
          if(value<0 || value>1)
1797
0
          {
1798
0
             goto bad_arg;
1799
0
          }
1800
0
          st->disable_inv = value;
1801
0
      }
1802
0
      break;
1803
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
1804
0
      {
1805
0
          opus_int32 *value = va_arg(ap, opus_int32*);
1806
0
          if (!value)
1807
0
          {
1808
0
             goto bad_arg;
1809
0
          }
1810
0
          *value = st->disable_inv;
1811
0
      }
1812
0
      break;
1813
0
      default:
1814
0
         goto bad_request;
1815
7.43M
   }
1816
7.43M
   va_end(ap);
1817
7.43M
   return OPUS_OK;
1818
0
bad_arg:
1819
0
   va_end(ap);
1820
0
   return OPUS_BAD_ARG;
1821
0
bad_request:
1822
0
      va_end(ap);
1823
0
  return OPUS_UNIMPLEMENTED;
1824
7.43M
}
opus_custom_decoder_ctl
Line
Count
Source
1674
3.71M
{
1675
3.71M
   va_list ap;
1676
1677
3.71M
   va_start(ap, request);
1678
3.71M
   switch (request)
1679
3.71M
   {
1680
0
      case OPUS_SET_COMPLEXITY_REQUEST:
1681
0
      {
1682
0
          opus_int32 value = va_arg(ap, opus_int32);
1683
0
          if(value<0 || value>10)
1684
0
          {
1685
0
             goto bad_arg;
1686
0
          }
1687
0
          st->complexity = value;
1688
0
      }
1689
0
      break;
1690
0
      case OPUS_GET_COMPLEXITY_REQUEST:
1691
0
      {
1692
0
          opus_int32 *value = va_arg(ap, opus_int32*);
1693
0
          if (!value)
1694
0
          {
1695
0
             goto bad_arg;
1696
0
          }
1697
0
          *value = st->complexity;
1698
0
      }
1699
0
      break;
1700
683k
      case CELT_SET_START_BAND_REQUEST:
1701
683k
      {
1702
683k
         opus_int32 value = va_arg(ap, opus_int32);
1703
683k
         if (value<0 || value>=st->mode->nbEBands)
1704
0
            goto bad_arg;
1705
683k
         st->start = value;
1706
683k
      }
1707
0
      break;
1708
420k
      case CELT_SET_END_BAND_REQUEST:
1709
420k
      {
1710
420k
         opus_int32 value = va_arg(ap, opus_int32);
1711
420k
         if (value<1 || value>st->mode->nbEBands)
1712
0
            goto bad_arg;
1713
420k
         st->end = value;
1714
420k
      }
1715
0
      break;
1716
650k
      case CELT_SET_CHANNELS_REQUEST:
1717
650k
      {
1718
650k
         opus_int32 value = va_arg(ap, opus_int32);
1719
650k
         if (value<1 || value>2)
1720
0
            goto bad_arg;
1721
650k
         st->stream_channels = value;
1722
650k
      }
1723
0
      break;
1724
0
      case CELT_GET_AND_CLEAR_ERROR_REQUEST:
1725
0
      {
1726
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1727
0
         if (value==NULL)
1728
0
            goto bad_arg;
1729
0
         *value=st->error;
1730
0
         st->error = 0;
1731
0
      }
1732
0
      break;
1733
0
      case OPUS_GET_LOOKAHEAD_REQUEST:
1734
0
      {
1735
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1736
0
         if (value==NULL)
1737
0
            goto bad_arg;
1738
0
         *value = st->overlap/st->downsample;
1739
0
      }
1740
0
      break;
1741
417k
      case OPUS_RESET_STATE:
1742
417k
      {
1743
417k
         int i;
1744
417k
         opus_val16 *lpc;
1745
417k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
1746
417k
         int decode_buffer_size;
1747
#ifdef ENABLE_QEXT
1748
         int qext_scale = st->qext_scale;
1749
#endif
1750
417k
         decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1751
417k
         lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+st->overlap)*st->channels);
1752
417k
         oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER);
1753
417k
         oldLogE = oldBandE + 2*st->mode->nbEBands;
1754
417k
         oldLogE2 = oldLogE + 2*st->mode->nbEBands;
1755
417k
         OPUS_CLEAR((char*)&st->DECODER_RESET_START,
1756
417k
               opus_custom_decoder_get_size(st->mode, st->channels)-
1757
417k
               ((char*)&st->DECODER_RESET_START - (char*)st));
1758
17.9M
         for (i=0;i<2*st->mode->nbEBands;i++)
1759
17.5M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
1760
417k
         st->skip_plc = 1;
1761
417k
      }
1762
417k
      break;
1763
0
      case OPUS_GET_PITCH_REQUEST:
1764
0
      {
1765
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1766
0
         if (value==NULL)
1767
0
            goto bad_arg;
1768
0
         *value = st->postfilter_period;
1769
0
      }
1770
0
      break;
1771
650k
      case CELT_GET_MODE_REQUEST:
1772
650k
      {
1773
650k
         const CELTMode ** value = va_arg(ap, const CELTMode**);
1774
650k
         if (value==0)
1775
0
            goto bad_arg;
1776
650k
         *value=st->mode;
1777
650k
      }
1778
0
      break;
1779
403k
      case CELT_SET_SIGNALLING_REQUEST:
1780
403k
      {
1781
403k
         opus_int32 value = va_arg(ap, opus_int32);
1782
403k
         st->signalling = value;
1783
403k
      }
1784
403k
      break;
1785
490k
      case OPUS_GET_FINAL_RANGE_REQUEST:
1786
490k
      {
1787
490k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
1788
490k
         if (value==0)
1789
0
            goto bad_arg;
1790
490k
         *value=st->rng;
1791
490k
      }
1792
0
      break;
1793
0
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
1794
0
      {
1795
0
          opus_int32 value = va_arg(ap, opus_int32);
1796
0
          if(value<0 || value>1)
1797
0
          {
1798
0
             goto bad_arg;
1799
0
          }
1800
0
          st->disable_inv = value;
1801
0
      }
1802
0
      break;
1803
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
1804
0
      {
1805
0
          opus_int32 *value = va_arg(ap, opus_int32*);
1806
0
          if (!value)
1807
0
          {
1808
0
             goto bad_arg;
1809
0
          }
1810
0
          *value = st->disable_inv;
1811
0
      }
1812
0
      break;
1813
0
      default:
1814
0
         goto bad_request;
1815
3.71M
   }
1816
3.71M
   va_end(ap);
1817
3.71M
   return OPUS_OK;
1818
0
bad_arg:
1819
0
   va_end(ap);
1820
0
   return OPUS_BAD_ARG;
1821
0
bad_request:
1822
0
      va_end(ap);
1823
0
  return OPUS_UNIMPLEMENTED;
1824
3.71M
}
opus_custom_decoder_ctl
Line
Count
Source
1674
3.71M
{
1675
3.71M
   va_list ap;
1676
1677
3.71M
   va_start(ap, request);
1678
3.71M
   switch (request)
1679
3.71M
   {
1680
0
      case OPUS_SET_COMPLEXITY_REQUEST:
1681
0
      {
1682
0
          opus_int32 value = va_arg(ap, opus_int32);
1683
0
          if(value<0 || value>10)
1684
0
          {
1685
0
             goto bad_arg;
1686
0
          }
1687
0
          st->complexity = value;
1688
0
      }
1689
0
      break;
1690
0
      case OPUS_GET_COMPLEXITY_REQUEST:
1691
0
      {
1692
0
          opus_int32 *value = va_arg(ap, opus_int32*);
1693
0
          if (!value)
1694
0
          {
1695
0
             goto bad_arg;
1696
0
          }
1697
0
          *value = st->complexity;
1698
0
      }
1699
0
      break;
1700
683k
      case CELT_SET_START_BAND_REQUEST:
1701
683k
      {
1702
683k
         opus_int32 value = va_arg(ap, opus_int32);
1703
683k
         if (value<0 || value>=st->mode->nbEBands)
1704
0
            goto bad_arg;
1705
683k
         st->start = value;
1706
683k
      }
1707
0
      break;
1708
420k
      case CELT_SET_END_BAND_REQUEST:
1709
420k
      {
1710
420k
         opus_int32 value = va_arg(ap, opus_int32);
1711
420k
         if (value<1 || value>st->mode->nbEBands)
1712
0
            goto bad_arg;
1713
420k
         st->end = value;
1714
420k
      }
1715
0
      break;
1716
650k
      case CELT_SET_CHANNELS_REQUEST:
1717
650k
      {
1718
650k
         opus_int32 value = va_arg(ap, opus_int32);
1719
650k
         if (value<1 || value>2)
1720
0
            goto bad_arg;
1721
650k
         st->stream_channels = value;
1722
650k
      }
1723
0
      break;
1724
0
      case CELT_GET_AND_CLEAR_ERROR_REQUEST:
1725
0
      {
1726
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1727
0
         if (value==NULL)
1728
0
            goto bad_arg;
1729
0
         *value=st->error;
1730
0
         st->error = 0;
1731
0
      }
1732
0
      break;
1733
0
      case OPUS_GET_LOOKAHEAD_REQUEST:
1734
0
      {
1735
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1736
0
         if (value==NULL)
1737
0
            goto bad_arg;
1738
0
         *value = st->overlap/st->downsample;
1739
0
      }
1740
0
      break;
1741
417k
      case OPUS_RESET_STATE:
1742
417k
      {
1743
417k
         int i;
1744
417k
         opus_val16 *lpc;
1745
417k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
1746
417k
         int decode_buffer_size;
1747
417k
#ifdef ENABLE_QEXT
1748
417k
         int qext_scale = st->qext_scale;
1749
417k
#endif
1750
417k
         decode_buffer_size = QEXT_SCALE(DECODE_BUFFER_SIZE);
1751
417k
         lpc = (opus_val16*)(st->_decode_mem+(decode_buffer_size+st->overlap)*st->channels);
1752
417k
         oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER);
1753
417k
         oldLogE = oldBandE + 2*st->mode->nbEBands;
1754
417k
         oldLogE2 = oldLogE + 2*st->mode->nbEBands;
1755
417k
         OPUS_CLEAR((char*)&st->DECODER_RESET_START,
1756
417k
               opus_custom_decoder_get_size(st->mode, st->channels)-
1757
417k
               ((char*)&st->DECODER_RESET_START - (char*)st));
1758
17.9M
         for (i=0;i<2*st->mode->nbEBands;i++)
1759
17.5M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
1760
417k
         st->skip_plc = 1;
1761
417k
      }
1762
417k
      break;
1763
0
      case OPUS_GET_PITCH_REQUEST:
1764
0
      {
1765
0
         opus_int32 *value = va_arg(ap, opus_int32*);
1766
0
         if (value==NULL)
1767
0
            goto bad_arg;
1768
0
         *value = st->postfilter_period;
1769
0
      }
1770
0
      break;
1771
650k
      case CELT_GET_MODE_REQUEST:
1772
650k
      {
1773
650k
         const CELTMode ** value = va_arg(ap, const CELTMode**);
1774
650k
         if (value==0)
1775
0
            goto bad_arg;
1776
650k
         *value=st->mode;
1777
650k
      }
1778
0
      break;
1779
403k
      case CELT_SET_SIGNALLING_REQUEST:
1780
403k
      {
1781
403k
         opus_int32 value = va_arg(ap, opus_int32);
1782
403k
         st->signalling = value;
1783
403k
      }
1784
403k
      break;
1785
490k
      case OPUS_GET_FINAL_RANGE_REQUEST:
1786
490k
      {
1787
490k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
1788
490k
         if (value==0)
1789
0
            goto bad_arg;
1790
490k
         *value=st->rng;
1791
490k
      }
1792
0
      break;
1793
0
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
1794
0
      {
1795
0
          opus_int32 value = va_arg(ap, opus_int32);
1796
0
          if(value<0 || value>1)
1797
0
          {
1798
0
             goto bad_arg;
1799
0
          }
1800
0
          st->disable_inv = value;
1801
0
      }
1802
0
      break;
1803
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
1804
0
      {
1805
0
          opus_int32 *value = va_arg(ap, opus_int32*);
1806
0
          if (!value)
1807
0
          {
1808
0
             goto bad_arg;
1809
0
          }
1810
0
          *value = st->disable_inv;
1811
0
      }
1812
0
      break;
1813
0
      default:
1814
0
         goto bad_request;
1815
3.71M
   }
1816
3.71M
   va_end(ap);
1817
3.71M
   return OPUS_OK;
1818
0
bad_arg:
1819
0
   va_end(ap);
1820
0
   return OPUS_BAD_ARG;
1821
0
bad_request:
1822
0
      va_end(ap);
1823
0
  return OPUS_UNIMPLEMENTED;
1824
3.71M
}