blob: 8a3b348597cbe80ad8e5a2c0be28eef8ea20479c [file] [log] [blame]
Jean-Marc Valin69062102012-11-08 09:42:27 -05001/* 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
Jean-Marc Valin1ecb7ea2012-11-08 11:25:20 -050034#define CELT_DECODER_C
Jean-Marc Valin69062102012-11-08 09:42:27 -050035
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020036#include "cpu_support.h"
Jean-Marc Valin69062102012-11-08 09:42:27 -050037#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
Jean-Marc Valin9134e962014-01-07 17:50:46 -050054#if defined(SMALL_FOOTPRINT) && defined(FIXED_POINT)
55#define NORM_ALIASING_HACK
56#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -050057/**********************************************************************/
58/* */
59/* DECODER */
60/* */
61/**********************************************************************/
62#define DECODE_BUFFER_SIZE 2048
63
64/** Decoder state
65 @brief Decoder state
66 */
67struct OpusCustomDecoder {
68 const OpusCustomMode *mode;
69 int overlap;
70 int channels;
71 int stream_channels;
72
73 int downsample;
74 int start, end;
75 int signalling;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020076 int arch;
Jean-Marc Valin69062102012-11-08 09:42:27 -050077
78 /* Everything beyond this point gets cleared on a reset */
79#define DECODER_RESET_START rng
80
81 opus_uint32 rng;
82 int error;
83 int last_pitch_index;
84 int loss_count;
Felicia Lim787eee22016-04-06 11:23:21 +020085 int skip_plc;
Jean-Marc Valin69062102012-11-08 09:42:27 -050086 int postfilter_period;
87 int postfilter_period_old;
88 opus_val16 postfilter_gain;
89 opus_val16 postfilter_gain_old;
90 int postfilter_tapset;
91 int postfilter_tapset_old;
92
93 celt_sig preemph_memD[2];
94
95 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
96 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
97 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
98 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
99 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
100 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
101};
102
103int celt_decoder_get_size(int channels)
104{
105 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
106 return opus_custom_decoder_get_size(mode, channels);
107}
108
109OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
110{
111 int size = sizeof(struct CELTDecoder)
112 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
113 + channels*LPC_ORDER*sizeof(opus_val16)
114 + 4*2*mode->nbEBands*sizeof(opus_val16);
115 return size;
116}
117
118#ifdef CUSTOM_MODES
119CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
120{
121 int ret;
122 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
123 ret = opus_custom_decoder_init(st, mode, channels);
124 if (ret != OPUS_OK)
125 {
126 opus_custom_decoder_destroy(st);
127 st = NULL;
128 }
129 if (error)
130 *error = ret;
131 return st;
132}
133#endif /* CUSTOM_MODES */
134
135int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
136{
137 int ret;
138 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
139 if (ret != OPUS_OK)
140 return ret;
141 st->downsample = resampling_factor(sampling_rate);
142 if (st->downsample==0)
143 return OPUS_BAD_ARG;
144 else
145 return OPUS_OK;
146}
147
148OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
149{
150 if (channels < 0 || channels > 2)
151 return OPUS_BAD_ARG;
152
153 if (st==NULL)
154 return OPUS_ALLOC_FAIL;
155
156 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
157
158 st->mode = mode;
159 st->overlap = mode->overlap;
160 st->stream_channels = st->channels = channels;
161
162 st->downsample = 1;
163 st->start = 0;
164 st->end = st->mode->effEBands;
165 st->signalling = 1;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +0200166 st->arch = opus_select_arch();
Jean-Marc Valin69062102012-11-08 09:42:27 -0500167
Jean-Marc Valin69062102012-11-08 09:42:27 -0500168 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
169
170 return OPUS_OK;
171}
172
173#ifdef CUSTOM_MODES
174void opus_custom_decoder_destroy(CELTDecoder *st)
175{
176 opus_free(st);
177}
178#endif /* CUSTOM_MODES */
179
Jean-Marc Valin69062102012-11-08 09:42:27 -0500180
181#ifndef RESYNTH
182static
183#endif
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500184void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef,
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500185 celt_sig *mem, int accum)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500186{
187 int c;
188 int Nd;
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400189 int apply_downsampling=0;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500190 opus_val16 coef0;
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500191 VARDECL(celt_sig, scratch);
192 SAVE_STACK;
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500193#ifndef FIXED_POINT
194 (void)accum;
195 celt_assert(accum==0);
196#endif
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500197 ALLOC(scratch, N, celt_sig);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500198 coef0 = coef[0];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500199 Nd = N/downsample;
200 c=0; do {
201 int j;
202 celt_sig * OPUS_RESTRICT x;
203 opus_val16 * OPUS_RESTRICT y;
204 celt_sig m = mem[c];
205 x =in[c];
206 y = pcm+c;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500207#ifdef CUSTOM_MODES
208 if (coef[1] != 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500209 {
Jean-Marc Valine368e622013-01-03 14:28:28 -0500210 opus_val16 coef1 = coef[1];
211 opus_val16 coef3 = coef[3];
212 for (j=0;j<N;j++)
213 {
Jean-Marc Valin96408b62013-12-02 20:02:37 -0500214 celt_sig tmp = x[j] + m + VERY_SMALL;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500215 m = MULT16_32_Q15(coef0, tmp)
216 - MULT16_32_Q15(coef1, x[j]);
217 tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2);
218 scratch[j] = tmp;
219 }
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400220 apply_downsampling=1;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500221 } else
Jean-Marc Valinebdfbfb2013-01-09 11:13:00 -0500222#endif
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400223 if (downsample>1)
Jean-Marc Valine368e622013-01-03 14:28:28 -0500224 {
225 /* Shortcut for the standard (non-custom modes) case */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500226 for (j=0;j<N;j++)
227 {
Jean-Marc Valin96408b62013-12-02 20:02:37 -0500228 celt_sig tmp = x[j] + m + VERY_SMALL;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500229 m = MULT16_32_Q15(coef0, tmp);
230 scratch[j] = tmp;
231 }
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400232 apply_downsampling=1;
233 } else {
234 /* Shortcut for the standard (non-custom modes) case */
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500235#ifdef FIXED_POINT
236 if (accum)
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400237 {
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500238 for (j=0;j<N;j++)
239 {
240 celt_sig tmp = x[j] + m + VERY_SMALL;
241 m = MULT16_32_Q15(coef0, tmp);
242 y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(tmp))));
243 }
244 } else
245#endif
246 {
247 for (j=0;j<N;j++)
248 {
249 celt_sig tmp = x[j] + m + VERY_SMALL;
250 m = MULT16_32_Q15(coef0, tmp);
251 y[j*C] = SCALEOUT(SIG2WORD16(tmp));
252 }
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400253 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500254 }
255 mem[c] = m;
256
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400257 if (apply_downsampling)
258 {
259 /* Perform down-sampling */
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500260#ifdef FIXED_POINT
261 if (accum)
262 {
263 for (j=0;j<Nd;j++)
264 y[j*C] = SAT16(ADD32(y[j*C], SCALEOUT(SIG2WORD16(scratch[j*downsample]))));
265 } else
266#endif
267 {
268 for (j=0;j<Nd;j++)
269 y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample]));
270 }
Jean-Marc Valin2fe47002013-05-26 18:50:13 -0400271 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500272 } while (++c<C);
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500273 RESTORE_STACK;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500274}
275
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500276#ifndef RESYNTH
277static
278#endif
279void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[],
Viswanath Puttagunta19c54062015-05-15 12:42:20 -0500280 opus_val16 *oldBandE, int start, int effEnd, int C, int CC,
281 int isTransient, int LM, int downsample,
282 int silence, int arch)
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500283{
284 int c, i;
Jean-Marc Valin14ca4ed2014-01-06 09:31:09 -0500285 int M;
286 int b;
287 int B;
288 int N, NB;
289 int shift;
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500290 int nbEBands;
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500291 int overlap;
292 VARDECL(celt_sig, freq);
293 SAVE_STACK;
294
295 overlap = mode->overlap;
296 nbEBands = mode->nbEBands;
297 N = mode->shortMdctSize<<LM;
298 ALLOC(freq, N, celt_sig); /**< Interleaved signal MDCTs */
299 M = 1<<LM;
Jean-Marc Valin14ca4ed2014-01-06 09:31:09 -0500300
301 if (isTransient)
302 {
303 B = M;
304 NB = mode->shortMdctSize;
305 shift = mode->maxLM;
306 } else {
307 B = 1;
308 NB = mode->shortMdctSize<<LM;
309 shift = mode->maxLM-LM;
310 }
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500311
312 if (CC==2&&C==1)
313 {
314 /* Copying a mono streams to two channels */
315 celt_sig *freq2;
316 denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
317 downsample, silence);
318 /* Store a temporary copy in the output buffer because the IMDCT destroys its input. */
319 freq2 = out_syn[1]+overlap/2;
320 OPUS_COPY(freq2, freq, N);
Jean-Marc Valin14ca4ed2014-01-06 09:31:09 -0500321 for (b=0;b<B;b++)
Viswanath Puttagunta19c54062015-05-15 12:42:20 -0500322 clt_mdct_backward(&mode->mdct, &freq2[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
Jean-Marc Valin14ca4ed2014-01-06 09:31:09 -0500323 for (b=0;b<B;b++)
Viswanath Puttagunta19c54062015-05-15 12:42:20 -0500324 clt_mdct_backward(&mode->mdct, &freq[b], out_syn[1]+NB*b, mode->window, overlap, shift, B, arch);
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500325 } else if (CC==1&&C==2)
326 {
327 /* Downmixing a stereo stream to mono */
328 celt_sig *freq2;
329 freq2 = out_syn[0]+overlap/2;
330 denormalise_bands(mode, X, freq, oldBandE, start, effEnd, M,
331 downsample, silence);
332 /* Use the output buffer as temp array before downmixing. */
333 denormalise_bands(mode, X+N, freq2, oldBandE+nbEBands, start, effEnd, M,
334 downsample, silence);
335 for (i=0;i<N;i++)
Jean-Marc Valinae332182016-07-22 14:44:24 -0400336 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq2[i]));
Jean-Marc Valin14ca4ed2014-01-06 09:31:09 -0500337 for (b=0;b<B;b++)
Viswanath Puttagunta19c54062015-05-15 12:42:20 -0500338 clt_mdct_backward(&mode->mdct, &freq[b], out_syn[0]+NB*b, mode->window, overlap, shift, B, arch);
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500339 } else {
340 /* Normal case (mono or stereo) */
341 c=0; do {
342 denormalise_bands(mode, X+c*N, freq, oldBandE+c*nbEBands, start, effEnd, M,
343 downsample, silence);
Jean-Marc Valin14ca4ed2014-01-06 09:31:09 -0500344 for (b=0;b<B;b++)
Viswanath Puttagunta19c54062015-05-15 12:42:20 -0500345 clt_mdct_backward(&mode->mdct, &freq[b], out_syn[c]+NB*b, mode->window, overlap, shift, B, arch);
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500346 } while (++c<CC);
347 }
Jean-Marc Valin1a37d4e2016-07-24 17:40:44 -0400348 /* Saturate IMDCT output so that we can't overflow in the pitch postfilter
349 or in the */
350 c=0; do {
351 for (i=0;i<N;i++)
352 out_syn[c][i] = SATURATE(out_syn[c][i], SIG_SAT);
353 } while (++c<CC);
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500354 RESTORE_STACK;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500355}
356
357static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
358{
359 int i, curr, tf_select;
360 int tf_select_rsv;
361 int tf_changed;
362 int logp;
363 opus_uint32 budget;
364 opus_uint32 tell;
365
366 budget = dec->storage*8;
367 tell = ec_tell(dec);
368 logp = isTransient ? 2 : 4;
369 tf_select_rsv = LM>0 && tell+logp+1<=budget;
370 budget -= tf_select_rsv;
371 tf_changed = curr = 0;
372 for (i=start;i<end;i++)
373 {
374 if (tell+logp<=budget)
375 {
376 curr ^= ec_dec_bit_logp(dec, logp);
377 tell = ec_tell(dec);
378 tf_changed |= curr;
379 }
380 tf_res[i] = curr;
381 logp = isTransient ? 4 : 5;
382 }
383 tf_select = 0;
384 if (tf_select_rsv &&
385 tf_select_table[LM][4*isTransient+0+tf_changed] !=
386 tf_select_table[LM][4*isTransient+2+tf_changed])
387 {
388 tf_select = ec_dec_bit_logp(dec, 1);
389 }
390 for (i=start;i<end;i++)
391 {
392 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
393 }
394}
395
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800396/* The maximum pitch lag to allow in the pitch-based PLC. It's possible to save
397 CPU time in the PLC pitch search by making this smaller than MAX_PERIOD. The
398 current value corresponds to a pitch of 66.67 Hz. */
399#define PLC_PITCH_LAG_MAX (720)
400/* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a
401 pitch of 480 Hz. */
402#define PLC_PITCH_LAG_MIN (100)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500403
Jean-Marc Valinb63e7112014-01-07 15:02:43 -0500404static int celt_plc_pitch_search(celt_sig *decode_mem[2], int C, int arch)
405{
406 int pitch_index;
Jean-Marc Valinb63e7112014-01-07 15:02:43 -0500407 VARDECL( opus_val16, lp_pitch_buf );
Jean-Marc Valine17ca252014-01-07 15:27:02 -0500408 SAVE_STACK;
Jean-Marc Valinb63e7112014-01-07 15:02:43 -0500409 ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 );
410 pitch_downsample(decode_mem, lp_pitch_buf,
411 DECODE_BUFFER_SIZE, C, arch);
412 pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf,
413 DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX,
414 PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, arch);
415 pitch_index = PLC_PITCH_LAG_MAX-pitch_index;
416 RESTORE_STACK;
417 return pitch_index;
418}
419
Jean-Marc Valin9d1b6fe2014-01-07 04:32:41 -0500420static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500421{
422 int c;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800423 int i;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500424 const int C = st->channels;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500425 celt_sig *decode_mem[2];
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800426 celt_sig *out_syn[2];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500427 opus_val16 *lpc;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500428 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
429 const OpusCustomMode *mode;
430 int nbEBands;
431 int overlap;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800432 int start;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800433 int loss_count;
434 int noise_based;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500435 const opus_int16 *eBands;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500436 SAVE_STACK;
437
438 mode = st->mode;
439 nbEBands = mode->nbEBands;
440 overlap = mode->overlap;
441 eBands = mode->eBands;
442
443 c=0; do {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800444 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
445 out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500446 } while (++c<C);
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800447 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*C);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500448 oldBandE = lpc+C*LPC_ORDER;
449 oldLogE = oldBandE + 2*nbEBands;
450 oldLogE2 = oldLogE + 2*nbEBands;
451 backgroundLogE = oldLogE2 + 2*nbEBands;
452
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800453 loss_count = st->loss_count;
454 start = st->start;
Felicia Lim787eee22016-04-06 11:23:21 +0200455 noise_based = loss_count >= 5 || start != 0 || st->skip_plc;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800456 if (noise_based)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500457 {
458 /* Noise-based PLC/CNG */
Jean-Marc Valin9134e962014-01-07 17:50:46 -0500459#ifdef NORM_ALIASING_HACK
Jean-Marc Valin5f807c12014-01-07 04:48:42 -0500460 celt_norm *X;
461#else
Jean-Marc Valin69062102012-11-08 09:42:27 -0500462 VARDECL(celt_norm, X);
Jean-Marc Valin5f807c12014-01-07 04:48:42 -0500463#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500464 opus_uint32 seed;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800465 int end;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500466 int effEnd;
Jean-Marc Valin1970d5b2015-12-03 13:32:10 -0500467 opus_val16 decay;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800468 end = st->end;
469 effEnd = IMAX(start, IMIN(end, mode->effEBands));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500470
Jean-Marc Valin9134e962014-01-07 17:50:46 -0500471#ifdef NORM_ALIASING_HACK
Jean-Marc Valin5f807c12014-01-07 04:48:42 -0500472 /* This is an ugly hack that breaks aliasing rules and would be easily broken,
473 but it saves almost 4kB of stack. */
474 X = (celt_norm*)(out_syn[C-1]+overlap/2);
475#else
Jean-Marc Valin69062102012-11-08 09:42:27 -0500476 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
Jean-Marc Valin5f807c12014-01-07 04:48:42 -0500477#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500478
Jean-Marc Valin1970d5b2015-12-03 13:32:10 -0500479 /* Energy decay */
480 decay = loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
481 c=0; do
482 {
483 for (i=start;i<end;i++)
484 oldBandE[c*nbEBands+i] = MAX16(backgroundLogE[c*nbEBands+i], oldBandE[c*nbEBands+i] - decay);
485 } while (++c<C);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500486 seed = st->rng;
487 for (c=0;c<C;c++)
488 {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800489 for (i=start;i<effEnd;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500490 {
491 int j;
492 int boffs;
493 int blen;
494 boffs = N*c+(eBands[i]<<LM);
495 blen = (eBands[i+1]-eBands[i])<<LM;
496 for (j=0;j<blen;j++)
497 {
498 seed = celt_lcg_rand(seed);
499 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
500 }
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800501 renormalise_vector(X+boffs, blen, Q15ONE, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500502 }
503 }
504 st->rng = seed;
505
Jean-Marc Valin69062102012-11-08 09:42:27 -0500506 c=0; do {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800507 OPUS_MOVE(decode_mem[c], decode_mem[c]+N,
508 DECODE_BUFFER_SIZE-N+(overlap>>1));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500509 } while (++c<C);
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500510
Jean-Marc Valin1970d5b2015-12-03 13:32:10 -0500511 celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd, C, C, 0, LM, st->downsample, 0, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500512 } else {
513 /* Pitch-based PLC */
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800514 const opus_val16 *window;
515 opus_val16 fade = Q15ONE;
516 int pitch_index;
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500517 VARDECL(opus_val32, etmp);
Timothy B. Terriberryc152d602013-05-08 10:32:37 -0700518 VARDECL(opus_val16, exc);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500519
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800520 if (loss_count == 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500521 {
Jean-Marc Valinb63e7112014-01-07 15:02:43 -0500522 st->last_pitch_index = pitch_index = celt_plc_pitch_search(decode_mem, C, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500523 } else {
524 pitch_index = st->last_pitch_index;
525 fade = QCONST16(.8f,15);
526 }
527
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500528 ALLOC(etmp, overlap, opus_val32);
Timothy B. Terriberryc152d602013-05-08 10:32:37 -0700529 ALLOC(exc, MAX_PERIOD, opus_val16);
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800530 window = mode->window;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500531 c=0; do {
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500532 opus_val16 decay;
533 opus_val16 attenuation;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500534 opus_val32 S1=0;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800535 celt_sig *buf;
536 int extrapolation_offset;
537 int extrapolation_len;
538 int exc_length;
539 int j;
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500540
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800541 buf = decode_mem[c];
542 for (i=0;i<MAX_PERIOD;i++) {
543 exc[i] = ROUND16(buf[DECODE_BUFFER_SIZE-MAX_PERIOD+i], SIG_SHIFT);
544 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500545
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800546 if (loss_count == 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500547 {
Timothy B. Terriberryc152d602013-05-08 10:32:37 -0700548 opus_val32 ac[LPC_ORDER+1];
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800549 /* Compute LPC coefficients for the last MAX_PERIOD samples before
550 the first loss so we can work in the excitation-filter domain. */
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500551 _celt_autocorr(exc, ac, window, overlap,
552 LPC_ORDER, MAX_PERIOD, st->arch);
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800553 /* Add a noise floor of -40 dB. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500554#ifdef FIXED_POINT
555 ac[0] += SHR32(ac[0],13);
556#else
557 ac[0] *= 1.0001f;
558#endif
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800559 /* Use lag windowing to stabilize the Levinson-Durbin recursion. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500560 for (i=1;i<=LPC_ORDER;i++)
561 {
562 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
563#ifdef FIXED_POINT
564 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
565#else
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800566 ac[i] -= ac[i]*(0.008f*0.008f)*i*i;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500567#endif
568 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500569 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
570 }
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800571 /* We want the excitation for 2 pitch periods in order to look for a
572 decaying signal, but we can't get more than MAX_PERIOD. */
573 exc_length = IMIN(2*pitch_index, MAX_PERIOD);
574 /* Initialize the LPC history with the samples just before the start
575 of the region for which we're computing the excitation. */
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800576 {
Timothy B. Terriberryc152d602013-05-08 10:32:37 -0700577 opus_val16 lpc_mem[LPC_ORDER];
578 for (i=0;i<LPC_ORDER;i++)
579 {
580 lpc_mem[i] =
581 ROUND16(buf[DECODE_BUFFER_SIZE-exc_length-1-i], SIG_SHIFT);
582 }
583 /* Compute the excitation for exc_length samples before the loss. */
584 celt_fir(exc+MAX_PERIOD-exc_length, lpc+c*LPC_ORDER,
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800585 exc+MAX_PERIOD-exc_length, exc_length, LPC_ORDER, lpc_mem, st->arch);
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800586 }
Jean-Marc Valin0094c882012-11-29 17:59:50 -0500587
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800588 /* Check if the waveform is decaying, and if so how fast.
Jean-Marc Valin0094c882012-11-29 17:59:50 -0500589 We do this to avoid adding energy when concealing in a segment
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800590 with decaying energy. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500591 {
592 opus_val32 E1=1, E2=1;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800593 int decay_length;
Jean-Marc Valinefdd3142013-05-18 02:14:24 -0400594#ifdef FIXED_POINT
595 int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[MAX_PERIOD-exc_length], exc_length))-20);
596#endif
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800597 decay_length = exc_length>>1;
598 for (i=0;i<decay_length;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500599 {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800600 opus_val16 e;
601 e = exc[MAX_PERIOD-decay_length+i];
Jean-Marc Valinefdd3142013-05-18 02:14:24 -0400602 E1 += SHR32(MULT16_16(e, e), shift);
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800603 e = exc[MAX_PERIOD-2*decay_length+i];
Jean-Marc Valinefdd3142013-05-18 02:14:24 -0400604 E2 += SHR32(MULT16_16(e, e), shift);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500605 }
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800606 E1 = MIN32(E1, E2);
607 decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500608 }
609
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800610 /* Move the decoder memory one frame to the left to give us room to
611 add the data for the new frame. We ignore the overlap that extends
612 past the end of the buffer, because we aren't going to use it. */
613 OPUS_MOVE(buf, buf+N, DECODE_BUFFER_SIZE-N);
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500614
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800615 /* Extrapolate from the end of the excitation with a period of
616 "pitch_index", scaling down each period by an additional factor of
617 "decay". */
618 extrapolation_offset = MAX_PERIOD-pitch_index;
619 /* We need to extrapolate enough samples to cover a complete MDCT
620 window (including overlap/2 samples on both sides). */
621 extrapolation_len = N+overlap;
622 /* We also apply fading if this is not the first loss. */
623 attenuation = MULT16_16_Q15(fade, decay);
624 for (i=j=0;i<extrapolation_len;i++,j++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500625 {
626 opus_val16 tmp;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800627 if (j >= pitch_index) {
628 j -= pitch_index;
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500629 attenuation = MULT16_16_Q15(attenuation, decay);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500630 }
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800631 buf[DECODE_BUFFER_SIZE-N+i] =
632 SHL32(EXTEND32(MULT16_16_Q15(attenuation,
633 exc[extrapolation_offset+j])), SIG_SHIFT);
Jean-Marc Valin027a2022012-12-01 22:00:53 -0500634 /* Compute the energy of the previously decoded signal whose
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800635 excitation we're copying. */
636 tmp = ROUND16(
637 buf[DECODE_BUFFER_SIZE-MAX_PERIOD-N+extrapolation_offset+j],
638 SIG_SHIFT);
Jean-Marc Valin52ded422016-07-24 17:48:40 -0400639 S1 += SHR32(MULT16_16(tmp, tmp), 10);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500640 }
Jean-Marc Valin3c60bae2016-07-22 11:05:19 -0400641#ifdef FIXED_POINT
642 /* For fixed-point, apply bandwidth expansion until we can guarantee that
643 no overflow can happen in the IIR filter. This means:
644 attenuation*32768*sum(abs(filter)) < 2^31 */
645 while (1) {
646 opus_val16 tmp=Q15ONE;
647 opus_val32 sum=QCONST16(1., SIG_SHIFT);
648 for (i=0;i<LPC_ORDER;i++)
649 sum += ABS16(lpc[c*LPC_ORDER+i]);
650 if (MULT16_32_Q15(attenuation, sum) < 65535) break;
651 for (i=0;i<LPC_ORDER;i++)
652 {
653 tmp = MULT16_16_Q15(QCONST16(.99f,15), tmp);
654 lpc[c*LPC_ORDER+i] = MULT16_16_Q15(lpc[c*LPC_ORDER+i], tmp);
655 }
656 }
657#endif
Timothy B. Terriberryc152d602013-05-08 10:32:37 -0700658 {
659 opus_val16 lpc_mem[LPC_ORDER];
660 /* Copy the last decoded samples (prior to the overlap region) to
661 synthesis filter memory so we can have a continuous signal. */
662 for (i=0;i<LPC_ORDER;i++)
663 lpc_mem[i] = ROUND16(buf[DECODE_BUFFER_SIZE-N-1-i], SIG_SHIFT);
664 /* Apply the synthesis filter to convert the excitation back into
665 the signal domain. */
666 celt_iir(buf+DECODE_BUFFER_SIZE-N, lpc+c*LPC_ORDER,
667 buf+DECODE_BUFFER_SIZE-N, extrapolation_len, LPC_ORDER,
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800668 lpc_mem, st->arch);
Jean-Marc Valin646fcc32016-07-22 11:47:01 -0400669#ifdef FIXED_POINT
670 for (i=0; i < extrapolation_len; i++)
671 buf[DECODE_BUFFER_SIZE-N+i] = SATURATE(buf[DECODE_BUFFER_SIZE-N+i], SIG_SAT);
672#endif
Timothy B. Terriberryc152d602013-05-08 10:32:37 -0700673 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500674
Jean-Marc Valin0094c882012-11-29 17:59:50 -0500675 /* Check if the synthesis energy is higher than expected, which can
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800676 happen with the signal changes during our window. If so,
677 attenuate. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500678 {
679 opus_val32 S2=0;
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800680 for (i=0;i<extrapolation_len;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500681 {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800682 opus_val16 tmp = ROUND16(buf[DECODE_BUFFER_SIZE-N+i], SIG_SHIFT);
Jean-Marc Valin52ded422016-07-24 17:48:40 -0400683 S2 += SHR32(MULT16_16(tmp, tmp), 10);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500684 }
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800685 /* This checks for an "explosion" in the synthesis. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500686#ifdef FIXED_POINT
687 if (!(S1 > SHR32(S2,2)))
688#else
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800689 /* The float test is written this way to catch NaNs in the output
690 of the IIR filter at the same time. */
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500691 if (!(S1 > 0.2f*S2))
Jean-Marc Valin69062102012-11-08 09:42:27 -0500692#endif
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500693 {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800694 for (i=0;i<extrapolation_len;i++)
695 buf[DECODE_BUFFER_SIZE-N+i] = 0;
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500696 } else if (S1 < S2)
697 {
698 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
699 for (i=0;i<overlap;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500700 {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800701 opus_val16 tmp_g = Q15ONE
702 - MULT16_16_Q15(window[i], Q15ONE-ratio);
703 buf[DECODE_BUFFER_SIZE-N+i] =
704 MULT16_32_Q15(tmp_g, buf[DECODE_BUFFER_SIZE-N+i]);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500705 }
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800706 for (i=overlap;i<extrapolation_len;i++)
707 {
708 buf[DECODE_BUFFER_SIZE-N+i] =
709 MULT16_32_Q15(ratio, buf[DECODE_BUFFER_SIZE-N+i]);
710 }
Jean-Marc Valine0491e72012-11-29 16:49:46 -0500711 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500712 }
713
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800714 /* Apply the pre-filter to the MDCT overlap for the next frame because
715 the post-filter will be re-applied in the decoder after the MDCT
716 overlap. */
717 comb_filter(etmp, buf+DECODE_BUFFER_SIZE,
718 st->postfilter_period, st->postfilter_period, overlap,
719 -st->postfilter_gain, -st->postfilter_gain,
Jonathan Lennox43120f02015-08-03 17:04:27 -0400720 st->postfilter_tapset, st->postfilter_tapset, NULL, 0, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500721
Jean-Marc Valin0094c882012-11-29 17:59:50 -0500722 /* Simulate TDAC on the concealed audio so that it blends with the
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800723 MDCT of the next frame. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500724 for (i=0;i<overlap/2;i++)
725 {
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800726 buf[DECODE_BUFFER_SIZE+i] =
727 MULT16_32_Q15(window[i], etmp[overlap-1-i])
728 + MULT16_32_Q15(window[overlap-i-1], etmp[i]);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500729 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500730 } while (++c<C);
731 }
732
Timothy B. Terriberry8eb9bb72012-12-07 14:07:22 -0800733 st->loss_count = loss_count+1;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500734
735 RESTORE_STACK;
736}
737
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500738int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data,
739 int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500740{
741 int c, i, N;
742 int spread_decision;
743 opus_int32 bits;
744 ec_dec _dec;
Jean-Marc Valin9134e962014-01-07 17:50:46 -0500745#ifdef NORM_ALIASING_HACK
Jean-Marc Valin32454dc2014-01-06 09:11:52 -0500746 celt_norm *X;
747#else
Jean-Marc Valin69062102012-11-08 09:42:27 -0500748 VARDECL(celt_norm, X);
Jean-Marc Valin32454dc2014-01-06 09:11:52 -0500749#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500750 VARDECL(int, fine_quant);
751 VARDECL(int, pulses);
752 VARDECL(int, cap);
753 VARDECL(int, offsets);
754 VARDECL(int, fine_priority);
755 VARDECL(int, tf_res);
756 VARDECL(unsigned char, collapse_masks);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500757 celt_sig *decode_mem[2];
758 celt_sig *out_syn[2];
759 opus_val16 *lpc;
760 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
761
762 int shortBlocks;
763 int isTransient;
764 int intra_ener;
765 const int CC = st->channels;
766 int LM, M;
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500767 int start;
768 int end;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500769 int effEnd;
770 int codedBands;
771 int alloc_trim;
772 int postfilter_pitch;
773 opus_val16 postfilter_gain;
774 int intensity=0;
775 int dual_stereo=0;
776 opus_int32 total_bits;
777 opus_int32 balance;
778 opus_int32 tell;
779 int dynalloc_logp;
780 int postfilter_tapset;
781 int anti_collapse_rsv;
782 int anti_collapse_on=0;
783 int silence;
784 int C = st->stream_channels;
785 const OpusCustomMode *mode;
786 int nbEBands;
787 int overlap;
788 const opus_int16 *eBands;
789 ALLOC_STACK;
790
791 mode = st->mode;
792 nbEBands = mode->nbEBands;
793 overlap = mode->overlap;
794 eBands = mode->eBands;
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500795 start = st->start;
796 end = st->end;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500797 frame_size *= st->downsample;
798
Jean-Marc Valin69062102012-11-08 09:42:27 -0500799 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC);
800 oldBandE = lpc+CC*LPC_ORDER;
801 oldLogE = oldBandE + 2*nbEBands;
802 oldLogE2 = oldLogE + 2*nbEBands;
803 backgroundLogE = oldLogE2 + 2*nbEBands;
804
805#ifdef CUSTOM_MODES
806 if (st->signalling && data!=NULL)
807 {
808 int data0=data[0];
809 /* Convert "standard mode" to Opus header */
810 if (mode->Fs==48000 && mode->shortMdctSize==120)
811 {
812 data0 = fromOpus(data0);
813 if (data0<0)
814 return OPUS_INVALID_PACKET;
815 }
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500816 st->end = end = IMAX(1, mode->effEBands-2*(data0>>5));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500817 LM = (data0>>3)&0x3;
818 C = 1 + ((data0>>2)&0x1);
819 data++;
820 len--;
821 if (LM>mode->maxLM)
822 return OPUS_INVALID_PACKET;
823 if (frame_size < mode->shortMdctSize<<LM)
824 return OPUS_BUFFER_TOO_SMALL;
825 else
826 frame_size = mode->shortMdctSize<<LM;
827 } else {
828#else
829 {
830#endif
831 for (LM=0;LM<=mode->maxLM;LM++)
832 if (mode->shortMdctSize<<LM==frame_size)
833 break;
834 if (LM>mode->maxLM)
835 return OPUS_BAD_ARG;
836 }
837 M=1<<LM;
838
839 if (len<0 || len>1275 || pcm==NULL)
840 return OPUS_BAD_ARG;
841
842 N = M*mode->shortMdctSize;
Jean-Marc Valin9d1b6fe2014-01-07 04:32:41 -0500843 c=0; do {
844 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap);
845 out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N;
846 } while (++c<CC);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500847
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500848 effEnd = end;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500849 if (effEnd > mode->effEBands)
850 effEnd = mode->effEBands;
851
852 if (data == NULL || len<=1)
853 {
Jean-Marc Valin9d1b6fe2014-01-07 04:32:41 -0500854 celt_decode_lost(st, N, LM);
855 deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500856 RESTORE_STACK;
857 return frame_size/st->downsample;
858 }
859
Felicia Lim787eee22016-04-06 11:23:21 +0200860 /* Check if there are at least two packets received consecutively before
861 * turning on the pitch-based PLC */
862 st->skip_plc = st->loss_count != 0;
863
Jean-Marc Valin69062102012-11-08 09:42:27 -0500864 if (dec == NULL)
865 {
866 ec_dec_init(&_dec,(unsigned char*)data,len);
867 dec = &_dec;
868 }
869
870 if (C==1)
871 {
872 for (i=0;i<nbEBands;i++)
873 oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]);
874 }
875
876 total_bits = len*8;
877 tell = ec_tell(dec);
878
879 if (tell >= total_bits)
880 silence = 1;
881 else if (tell==1)
882 silence = ec_dec_bit_logp(dec, 15);
883 else
884 silence = 0;
885 if (silence)
886 {
887 /* Pretend we've read all the remaining bits */
888 tell = len*8;
889 dec->nbits_total+=tell-ec_tell(dec);
890 }
891
892 postfilter_gain = 0;
893 postfilter_pitch = 0;
894 postfilter_tapset = 0;
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500895 if (start==0 && tell+16 <= total_bits)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500896 {
897 if(ec_dec_bit_logp(dec, 1))
898 {
899 int qg, octave;
900 octave = ec_dec_uint(dec, 6);
901 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
902 qg = ec_dec_bits(dec, 3);
903 if (ec_tell(dec)+2<=total_bits)
904 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
905 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
906 }
907 tell = ec_tell(dec);
908 }
909
910 if (LM > 0 && tell+3 <= total_bits)
911 {
912 isTransient = ec_dec_bit_logp(dec, 3);
913 tell = ec_tell(dec);
914 }
915 else
916 isTransient = 0;
917
918 if (isTransient)
919 shortBlocks = M;
920 else
921 shortBlocks = 0;
922
923 /* Decode the global flags (first symbols in the stream) */
924 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
925 /* Get band energies */
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500926 unquant_coarse_energy(mode, start, end, oldBandE,
Jean-Marc Valin69062102012-11-08 09:42:27 -0500927 intra_ener, dec, C, LM);
928
929 ALLOC(tf_res, nbEBands, int);
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500930 tf_decode(start, end, isTransient, tf_res, LM, dec);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500931
932 tell = ec_tell(dec);
933 spread_decision = SPREAD_NORMAL;
934 if (tell+4 <= total_bits)
935 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
936
937 ALLOC(cap, nbEBands, int);
938
939 init_caps(mode,cap,LM,C);
940
941 ALLOC(offsets, nbEBands, int);
942
943 dynalloc_logp = 6;
944 total_bits<<=BITRES;
945 tell = ec_tell_frac(dec);
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500946 for (i=start;i<end;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500947 {
948 int width, quanta;
949 int dynalloc_loop_logp;
950 int boost;
951 width = C*(eBands[i+1]-eBands[i])<<LM;
952 /* quanta is 6 bits, but no more than 1 bit/sample
953 and no less than 1/8 bit/sample */
954 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
955 dynalloc_loop_logp = dynalloc_logp;
956 boost = 0;
957 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
958 {
959 int flag;
960 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
961 tell = ec_tell_frac(dec);
962 if (!flag)
963 break;
964 boost += quanta;
965 total_bits -= quanta;
966 dynalloc_loop_logp = 1;
967 }
968 offsets[i] = boost;
969 /* Making dynalloc more likely */
970 if (boost>0)
971 dynalloc_logp = IMAX(2, dynalloc_logp-1);
972 }
973
974 ALLOC(fine_quant, nbEBands, int);
975 alloc_trim = tell+(6<<BITRES) <= total_bits ?
976 ec_dec_icdf(dec, trim_icdf, 7) : 5;
977
978 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
979 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
980 bits -= anti_collapse_rsv;
981
982 ALLOC(pulses, nbEBands, int);
983 ALLOC(fine_priority, nbEBands, int);
984
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500985 codedBands = compute_allocation(mode, start, end, offsets, cap,
Jean-Marc Valin69062102012-11-08 09:42:27 -0500986 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -0500987 fine_quant, fine_priority, C, LM, dec, 0, 0, 0);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500988
Jean-Marc Valin91f80102013-12-10 18:38:44 -0500989 unquant_fine_energy(mode, start, end, oldBandE, fine_quant, dec, C);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500990
Jean-Marc Valin32454dc2014-01-06 09:11:52 -0500991 c=0; do {
992 OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap/2);
Jean-Marc Valin32454dc2014-01-06 09:11:52 -0500993 } while (++c<CC);
994
Jean-Marc Valin69062102012-11-08 09:42:27 -0500995 /* Decode fixed codebook */
996 ALLOC(collapse_masks, C*nbEBands, unsigned char);
Jean-Marc Valin32454dc2014-01-06 09:11:52 -0500997
Jean-Marc Valin9134e962014-01-07 17:50:46 -0500998#ifdef NORM_ALIASING_HACK
Jean-Marc Valin32454dc2014-01-06 09:11:52 -0500999 /* This is an ugly hack that breaks aliasing rules and would be easily broken,
1000 but it saves almost 4kB of stack. */
1001 X = (celt_norm*)(out_syn[CC-1]+overlap/2);
1002#else
Jean-Marc Valin69062102012-11-08 09:42:27 -05001003 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
Jean-Marc Valin32454dc2014-01-06 09:11:52 -05001004#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -05001005
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001006 quant_all_bands(0, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
Jean-Marc Valin69062102012-11-08 09:42:27 -05001007 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
xiangmingzhuc95c9a02014-04-30 15:48:07 +08001008 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001009
1010 if (anti_collapse_rsv > 0)
1011 {
1012 anti_collapse_on = ec_dec_bits(dec, 1);
1013 }
1014
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001015 unquant_energy_finalise(mode, start, end, oldBandE,
Jean-Marc Valin69062102012-11-08 09:42:27 -05001016 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
1017
1018 if (anti_collapse_on)
1019 anti_collapse(mode, X, collapse_masks, LM, C, N,
xiangmingzhuc95c9a02014-04-30 15:48:07 +08001020 start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001021
Jean-Marc Valin69062102012-11-08 09:42:27 -05001022 if (silence)
1023 {
1024 for (i=0;i<C*nbEBands;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001025 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001026 }
Jean-Marc Valin4a6744a2014-01-05 21:40:02 -05001027
Viswanath Puttagunta19c54062015-05-15 12:42:20 -05001028 celt_synthesis(mode, X, out_syn, oldBandE, start, effEnd,
1029 C, CC, isTransient, LM, st->downsample, silence, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001030
1031 c=0; do {
1032 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
1033 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
1034 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize,
1035 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
Jonathan Lennox43120f02015-08-03 17:04:27 -04001036 mode->window, overlap, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001037 if (LM!=0)
1038 comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize,
1039 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
Jonathan Lennox43120f02015-08-03 17:04:27 -04001040 mode->window, overlap, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001041
1042 } while (++c<CC);
1043 st->postfilter_period_old = st->postfilter_period;
1044 st->postfilter_gain_old = st->postfilter_gain;
1045 st->postfilter_tapset_old = st->postfilter_tapset;
1046 st->postfilter_period = postfilter_pitch;
1047 st->postfilter_gain = postfilter_gain;
1048 st->postfilter_tapset = postfilter_tapset;
1049 if (LM!=0)
1050 {
1051 st->postfilter_period_old = st->postfilter_period;
1052 st->postfilter_gain_old = st->postfilter_gain;
1053 st->postfilter_tapset_old = st->postfilter_tapset;
1054 }
1055
Jean-Marc Valind5553e82013-12-10 02:32:26 -05001056 if (C==1)
1057 OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001058
1059 /* In case start or end were to change */
1060 if (!isTransient)
1061 {
Jean-Marc Valincfaf1472015-12-04 16:11:39 -05001062 opus_val16 max_background_increase;
Jean-Marc Valind5553e82013-12-10 02:32:26 -05001063 OPUS_COPY(oldLogE2, oldLogE, 2*nbEBands);
1064 OPUS_COPY(oldLogE, oldBandE, 2*nbEBands);
Jean-Marc Valincfaf1472015-12-04 16:11:39 -05001065 /* In normal circumstances, we only allow the noise floor to increase by
1066 up to 2.4 dB/second, but when we're in DTX, we allow up to 6 dB
1067 increase for each update.*/
1068 if (st->loss_count < 10)
1069 max_background_increase = M*QCONST16(0.001f,DB_SHIFT);
1070 else
1071 max_background_increase = QCONST16(1.f,DB_SHIFT);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001072 for (i=0;i<2*nbEBands;i++)
Jean-Marc Valincfaf1472015-12-04 16:11:39 -05001073 backgroundLogE[i] = MIN16(backgroundLogE[i] + max_background_increase, oldBandE[i]);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001074 } else {
1075 for (i=0;i<2*nbEBands;i++)
1076 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1077 }
1078 c=0; do
1079 {
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001080 for (i=0;i<start;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001081 {
1082 oldBandE[c*nbEBands+i]=0;
1083 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1084 }
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001085 for (i=end;i<nbEBands;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001086 {
1087 oldBandE[c*nbEBands+i]=0;
1088 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1089 }
1090 } while (++c<2);
1091 st->rng = dec->rng;
1092
Jean-Marc Valin4d07b132014-01-06 17:43:20 -05001093 deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, accum);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001094 st->loss_count = 0;
1095 RESTORE_STACK;
1096 if (ec_tell(dec) > 8*len)
1097 return OPUS_INTERNAL_ERROR;
1098 if(ec_get_error(dec))
1099 st->error = 1;
1100 return frame_size/st->downsample;
1101}
1102
1103
1104#ifdef CUSTOM_MODES
1105
1106#ifdef FIXED_POINT
1107int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
1108{
Jean-Marc Valin4d07b132014-01-06 17:43:20 -05001109 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001110}
1111
1112#ifndef DISABLE_FLOAT_API
1113int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
1114{
1115 int j, ret, C, N;
1116 VARDECL(opus_int16, out);
1117 ALLOC_STACK;
1118
1119 if (pcm==NULL)
1120 return OPUS_BAD_ARG;
1121
1122 C = st->channels;
1123 N = frame_size;
1124
1125 ALLOC(out, C*N, opus_int16);
Jean-Marc Valin4d07b132014-01-06 17:43:20 -05001126 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001127 if (ret>0)
1128 for (j=0;j<C*ret;j++)
1129 pcm[j]=out[j]*(1.f/32768.f);
1130
1131 RESTORE_STACK;
1132 return ret;
1133}
1134#endif /* DISABLE_FLOAT_API */
1135
1136#else
1137
1138int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
1139{
Jean-Marc Valin4d07b132014-01-06 17:43:20 -05001140 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL, 0);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001141}
1142
1143int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
1144{
1145 int j, ret, C, N;
1146 VARDECL(celt_sig, out);
1147 ALLOC_STACK;
1148
1149 if (pcm==NULL)
1150 return OPUS_BAD_ARG;
1151
1152 C = st->channels;
1153 N = frame_size;
1154 ALLOC(out, C*N, celt_sig);
1155
Jean-Marc Valin4d07b132014-01-06 17:43:20 -05001156 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL, 0);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001157
1158 if (ret>0)
1159 for (j=0;j<C*ret;j++)
1160 pcm[j] = FLOAT2INT16 (out[j]);
1161
1162 RESTORE_STACK;
1163 return ret;
1164}
1165
1166#endif
1167#endif /* CUSTOM_MODES */
1168
1169int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
1170{
1171 va_list ap;
1172
1173 va_start(ap, request);
1174 switch (request)
1175 {
1176 case CELT_SET_START_BAND_REQUEST:
1177 {
1178 opus_int32 value = va_arg(ap, opus_int32);
1179 if (value<0 || value>=st->mode->nbEBands)
1180 goto bad_arg;
1181 st->start = value;
1182 }
1183 break;
1184 case CELT_SET_END_BAND_REQUEST:
1185 {
1186 opus_int32 value = va_arg(ap, opus_int32);
1187 if (value<1 || value>st->mode->nbEBands)
1188 goto bad_arg;
1189 st->end = value;
1190 }
1191 break;
1192 case CELT_SET_CHANNELS_REQUEST:
1193 {
1194 opus_int32 value = va_arg(ap, opus_int32);
1195 if (value<1 || value>2)
1196 goto bad_arg;
1197 st->stream_channels = value;
1198 }
1199 break;
1200 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
1201 {
1202 opus_int32 *value = va_arg(ap, opus_int32*);
1203 if (value==NULL)
1204 goto bad_arg;
1205 *value=st->error;
1206 st->error = 0;
1207 }
1208 break;
1209 case OPUS_GET_LOOKAHEAD_REQUEST:
1210 {
1211 opus_int32 *value = va_arg(ap, opus_int32*);
1212 if (value==NULL)
1213 goto bad_arg;
1214 *value = st->overlap/st->downsample;
1215 }
1216 break;
1217 case OPUS_RESET_STATE:
1218 {
1219 int i;
1220 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
1221 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
1222 oldBandE = lpc+st->channels*LPC_ORDER;
1223 oldLogE = oldBandE + 2*st->mode->nbEBands;
1224 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
1225 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
1226 opus_custom_decoder_get_size(st->mode, st->channels)-
1227 ((char*)&st->DECODER_RESET_START - (char*)st));
1228 for (i=0;i<2*st->mode->nbEBands;i++)
1229 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
Felicia Lim787eee22016-04-06 11:23:21 +02001230 st->skip_plc = 1;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001231 }
1232 break;
1233 case OPUS_GET_PITCH_REQUEST:
1234 {
1235 opus_int32 *value = va_arg(ap, opus_int32*);
1236 if (value==NULL)
1237 goto bad_arg;
1238 *value = st->postfilter_period;
1239 }
1240 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001241 case CELT_GET_MODE_REQUEST:
1242 {
1243 const CELTMode ** value = va_arg(ap, const CELTMode**);
1244 if (value==0)
1245 goto bad_arg;
1246 *value=st->mode;
1247 }
1248 break;
1249 case CELT_SET_SIGNALLING_REQUEST:
1250 {
1251 opus_int32 value = va_arg(ap, opus_int32);
1252 st->signalling = value;
1253 }
1254 break;
1255 case OPUS_GET_FINAL_RANGE_REQUEST:
1256 {
1257 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1258 if (value==0)
1259 goto bad_arg;
1260 *value=st->rng;
1261 }
1262 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001263 default:
1264 goto bad_request;
1265 }
1266 va_end(ap);
1267 return OPUS_OK;
1268bad_arg:
1269 va_end(ap);
1270 return OPUS_BAD_ARG;
1271bad_request:
1272 va_end(ap);
1273 return OPUS_UNIMPLEMENTED;
1274}