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