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