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