summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/celt/celt.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/celt/celt.c')
-rw-r--r--lib/rbcodec/codecs/libopus/celt/celt.c2870
1 files changed, 2870 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libopus/celt/celt.c b/lib/rbcodec/codecs/libopus/celt/celt.c
new file mode 100644
index 0000000000..8d42cc95b3
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/celt.c
@@ -0,0 +1,2870 @@
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 "opus_config.h"
32#endif
33
34#define CELT_C
35
36#include "os_support.h"
37#include "mdct.h"
38#include <math.h>
39#include "celt.h"
40#include "pitch.h"
41#include "bands.h"
42#include "modes.h"
43#include "entcode.h"
44#include "quant_bands.h"
45#include "rate.h"
46#include "stack_alloc.h"
47#include "mathops.h"
48#include "float_cast.h"
49#include <stdarg.h>
50#include "celt_lpc.h"
51#include "vq.h"
52
53#ifndef OPUS_VERSION
54#define OPUS_VERSION "unknown"
55#endif
56
57#ifdef CUSTOM_MODES
58#define OPUS_CUSTOM_NOSTATIC
59#else
60#define OPUS_CUSTOM_NOSTATIC static inline
61#endif
62
63static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
64/* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
65static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
66
67static const unsigned char tapset_icdf[3]={2,1,0};
68
69#ifdef CUSTOM_MODES
70static const unsigned char toOpusTable[20] = {
71 0xE0, 0xE8, 0xF0, 0xF8,
72 0xC0, 0xC8, 0xD0, 0xD8,
73 0xA0, 0xA8, 0xB0, 0xB8,
74 0x00, 0x00, 0x00, 0x00,
75 0x80, 0x88, 0x90, 0x98,
76};
77
78static const unsigned char fromOpusTable[16] = {
79 0x80, 0x88, 0x90, 0x98,
80 0x40, 0x48, 0x50, 0x58,
81 0x20, 0x28, 0x30, 0x38,
82 0x00, 0x08, 0x10, 0x18
83};
84
85static inline int toOpus(unsigned char c)
86{
87 int ret=0;
88 if (c<0xA0)
89 ret = toOpusTable[c>>3];
90 if (ret == 0)
91 return -1;
92 else
93 return ret|(c&0x7);
94}
95
96static inline int fromOpus(unsigned char c)
97{
98 if (c<0x80)
99 return -1;
100 else
101 return fromOpusTable[(c>>3)-16] | (c&0x7);
102}
103#endif /* CUSTOM_MODES */
104
105#define COMBFILTER_MAXPERIOD 1024
106#define COMBFILTER_MINPERIOD 15
107
108static int resampling_factor(opus_int32 rate)
109{
110 int ret;
111 switch (rate)
112 {
113 case 48000:
114 ret = 1;
115 break;
116 case 24000:
117 ret = 2;
118 break;
119 case 16000:
120 ret = 3;
121 break;
122 case 12000:
123 ret = 4;
124 break;
125 case 8000:
126 ret = 6;
127 break;
128 default:
129#ifndef CUSTOM_MODES
130 celt_assert(0);
131#endif
132 ret = 0;
133 break;
134 }
135 return ret;
136}
137
138/** Encoder state
139 @brief Encoder state
140 */
141struct OpusCustomEncoder {
142 const OpusCustomMode *mode; /**< Mode used by the encoder */
143 int overlap;
144 int channels;
145 int stream_channels;
146
147 int force_intra;
148 int clip;
149 int disable_pf;
150 int complexity;
151 int upsample;
152 int start, end;
153
154 opus_int32 bitrate;
155 int vbr;
156 int signalling;
157 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
158 int loss_rate;
159 int lsb_depth;
160
161 /* Everything beyond this point gets cleared on a reset */
162#define ENCODER_RESET_START rng
163
164 opus_uint32 rng;
165 int spread_decision;
166 opus_val32 delayedIntra;
167 int tonal_average;
168 int lastCodedBands;
169 int hf_average;
170 int tapset_decision;
171
172 int prefilter_period;
173 opus_val16 prefilter_gain;
174 int prefilter_tapset;
175#ifdef RESYNTH
176 int prefilter_period_old;
177 opus_val16 prefilter_gain_old;
178 int prefilter_tapset_old;
179#endif
180 int consec_transient;
181
182 opus_val32 preemph_memE[2];
183 opus_val32 preemph_memD[2];
184
185 /* VBR-related parameters */
186 opus_int32 vbr_reservoir;
187 opus_int32 vbr_drift;
188 opus_int32 vbr_offset;
189 opus_int32 vbr_count;
190
191#ifdef RESYNTH
192 celt_sig syn_mem[2][2*MAX_PERIOD];
193#endif
194
195 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
196 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
197 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
198 /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
199};
200
201int celt_encoder_get_size(int channels)
202{
203 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
204 return opus_custom_encoder_get_size(mode, channels);
205}
206
207OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
208{
209 int size = sizeof(struct CELTEncoder)
210 + (2*channels*mode->overlap-1)*sizeof(celt_sig)
211 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
212 + 3*channels*mode->nbEBands*sizeof(opus_val16);
213 return size;
214}
215
216#ifdef CUSTOM_MODES
217CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
218{
219 int ret;
220 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
221 /* init will handle the NULL case */
222 ret = opus_custom_encoder_init(st, mode, channels);
223 if (ret != OPUS_OK)
224 {
225 opus_custom_encoder_destroy(st);
226 st = NULL;
227 }
228 if (error)
229 *error = ret;
230 return st;
231}
232#endif /* CUSTOM_MODES */
233
234int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
235{
236 int ret;
237 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
238 if (ret != OPUS_OK)
239 return ret;
240 st->upsample = resampling_factor(sampling_rate);
241 return OPUS_OK;
242}
243
244OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
245{
246 if (channels < 0 || channels > 2)
247 return OPUS_BAD_ARG;
248
249 if (st==NULL || mode==NULL)
250 return OPUS_ALLOC_FAIL;
251
252 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
253
254 st->mode = mode;
255 st->overlap = mode->overlap;
256 st->stream_channels = st->channels = channels;
257
258 st->upsample = 1;
259 st->start = 0;
260 st->end = st->mode->effEBands;
261 st->signalling = 1;
262
263 st->constrained_vbr = 1;
264 st->clip = 1;
265
266 st->bitrate = OPUS_BITRATE_MAX;
267 st->vbr = 0;
268 st->force_intra = 0;
269 st->complexity = 5;
270 st->lsb_depth=24;
271
272 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
273
274 return OPUS_OK;
275}
276
277#ifdef CUSTOM_MODES
278void opus_custom_encoder_destroy(CELTEncoder *st)
279{
280 opus_free(st);
281}
282#endif /* CUSTOM_MODES */
283
284static inline opus_val16 SIG2WORD16(celt_sig x)
285{
286#ifdef FIXED_POINT
287 x = PSHR32(x, SIG_SHIFT);
288 x = MAX32(x, -32768);
289 x = MIN32(x, 32767);
290 return EXTRACT16(x);
291#else
292 return (opus_val16)x;
293#endif
294}
295
296static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
297 int overlap)
298{
299 int i;
300 VARDECL(opus_val16, tmp);
301 opus_val32 mem0=0,mem1=0;
302 int is_transient = 0;
303 int block;
304 int N;
305 VARDECL(opus_val16, bins);
306 SAVE_STACK;
307 ALLOC(tmp, len, opus_val16);
308
309 block = overlap/2;
310 N=len/block;
311 ALLOC(bins, N, opus_val16);
312 if (C==1)
313 {
314 for (i=0;i<len;i++)
315 tmp[i] = SHR32(in[i],SIG_SHIFT);
316 } else {
317 for (i=0;i<len;i++)
318 tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
319 }
320
321 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
322 for (i=0;i<len;i++)
323 {
324 opus_val32 x,y;
325 x = tmp[i];
326 y = ADD32(mem0, x);
327#ifdef FIXED_POINT
328 mem0 = mem1 + y - SHL32(x,1);
329 mem1 = x - SHR32(y,1);
330#else
331 mem0 = mem1 + y - 2*x;
332 mem1 = x - .5f*y;
333#endif
334 tmp[i] = EXTRACT16(SHR32(y,2));
335 }
336 /* First few samples are bad because we don't propagate the memory */
337 for (i=0;i<12;i++)
338 tmp[i] = 0;
339
340 for (i=0;i<N;i++)
341 {
342 int j;
343 opus_val16 max_abs=0;
344 for (j=0;j<block;j++)
345 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
346 bins[i] = max_abs;
347 }
348 for (i=0;i<N;i++)
349 {
350 int j;
351 int conseq=0;
352 opus_val16 t1, t2, t3;
353
354 t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
355 t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
356 t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
357 for (j=0;j<i;j++)
358 {
359 if (bins[j] < t1)
360 conseq++;
361 if (bins[j] < t2)
362 conseq++;
363 else
364 conseq = 0;
365 }
366 if (conseq>=3)
367 is_transient=1;
368 conseq = 0;
369 for (j=i+1;j<N;j++)
370 {
371 if (bins[j] < t3)
372 conseq++;
373 else
374 conseq = 0;
375 }
376 if (conseq>=7)
377 is_transient=1;
378 }
379 RESTORE_STACK;
380#ifdef FUZZING
381 is_transient = rand()&0x1;
382#endif
383 return is_transient;
384}
385
386/** Apply window and compute the MDCT for all sub-frames and
387 all channels in a frame */
388static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in, celt_sig * OPUS_RESTRICT out, int C, int LM)
389{
390 if (C==1 && !shortBlocks)
391 {
392 const int overlap = OVERLAP(mode);
393 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
394 } else {
395 const int overlap = OVERLAP(mode);
396 int N = mode->shortMdctSize<<LM;
397 int B = 1;
398 int b, c;
399 if (shortBlocks)
400 {
401 N = mode->shortMdctSize;
402 B = shortBlocks;
403 }
404 c=0; do {
405 for (b=0;b<B;b++)
406 {
407 /* Interleaving the sub-frames while doing the MDCTs */
408 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
409 }
410 } while (++c<C);
411 }
412}
413
414/** Compute the IMDCT and apply window for all sub-frames and
415 all channels in a frame */
416static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
417 celt_sig * OPUS_RESTRICT out_mem[],
418 celt_sig * OPUS_RESTRICT overlap_mem[], int C, int LM)
419{
420 int c;
421 const int N = mode->shortMdctSize<<LM;
422 const int overlap = OVERLAP(mode);
423 VARDECL(opus_val32, x);
424 SAVE_STACK;
425
426 ALLOC(x, N+overlap, opus_val32);
427 c=0; do {
428 int j;
429 int b;
430 int N2 = N;
431 int B = 1;
432
433 if (shortBlocks)
434 {
435 N2 = mode->shortMdctSize;
436 B = shortBlocks;
437 }
438 /* Prevents problems from the imdct doing the overlap-add */
439 OPUS_CLEAR(x, overlap);
440
441 for (b=0;b<B;b++)
442 {
443 /* IMDCT on the interleaved the sub-frames */
444 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
445 }
446
447 for (j=0;j<overlap;j++)
448 out_mem[c][j] = x[j] + overlap_mem[c][j];
449 for (;j<N;j++)
450 out_mem[c][j] = x[j];
451 for (j=0;j<overlap;j++)
452 overlap_mem[c][j] = x[N+j];
453 } while (++c<C);
454 RESTORE_STACK;
455}
456
457static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
458{
459 int c;
460 int count=0;
461 c=0; do {
462 int j;
463 celt_sig * OPUS_RESTRICT x;
464 opus_val16 * OPUS_RESTRICT y;
465 celt_sig m = mem[c];
466 x =in[c];
467 y = pcm+c;
468 for (j=0;j<N;j++)
469 {
470 celt_sig tmp = *x + m;
471 m = MULT16_32_Q15(coef[0], tmp)
472 - MULT16_32_Q15(coef[1], *x);
473 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
474 x++;
475 /* Technically the store could be moved outside of the if because
476 the stores we don't want will just be overwritten */
477 if (count==0)
478 *y = SCALEOUT(SIG2WORD16(tmp));
479 if (++count==downsample)
480 {
481 y+=C;
482 count=0;
483 }
484 }
485 mem[c] = m;
486 } while (++c<C);
487}
488
489static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
490 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
491 const opus_val16 *window, int overlap)
492{
493 int i;
494 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
495 opus_val16 g00, g01, g02, g10, g11, g12;
496 static const opus_val16 gains[3][3] = {
497 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
498 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
499 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
500 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
501 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
502 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
503 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
504 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
505 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
506 for (i=0;i<overlap;i++)
507 {
508 opus_val16 f;
509 f = MULT16_16_Q15(window[i],window[i]);
510 y[i] = x[i]
511 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
512 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
513 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
514 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
515 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
516 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
517 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
518 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
519 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
520 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
521
522 }
523 for (i=overlap;i<N;i++)
524 y[i] = x[i]
525 + MULT16_32_Q15(g10,x[i-T1])
526 + MULT16_32_Q15(g11,x[i-T1-1])
527 + MULT16_32_Q15(g11,x[i-T1+1])
528 + MULT16_32_Q15(g12,x[i-T1-2])
529 + MULT16_32_Q15(g12,x[i-T1+2]);
530}
531
532static const signed char tf_select_table[4][8] = {
533 {0, -1, 0, -1, 0,-1, 0,-1},
534 {0, -1, 0, -2, 1, 0, 1,-1},
535 {0, -2, 0, -3, 2, 0, 1,-1},
536 {0, -2, 0, -3, 3, 0, 1,-1},
537};
538
539static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width)
540{
541 int i, j;
542 static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)};
543 opus_val32 L1;
544 opus_val16 bias;
545 L1=0;
546 for (i=0;i<1<<LM;i++)
547 {
548 opus_val32 L2 = 0;
549 for (j=0;j<N>>LM;j++)
550 L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]);
551 L1 += celt_sqrt(L2);
552 }
553 L1 = MULT16_32_Q15(sqrtM_1[LM], L1);
554 if (width==1)
555 bias = QCONST16(.12f,15)*LM;
556 else if (width==2)
557 bias = QCONST16(.05f,15)*LM;
558 else
559 bias = QCONST16(.02f,15)*LM;
560 L1 = MAC16_32_Q15(L1, bias, L1);
561 return L1;
562}
563
564static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
565 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
566 int *tf_sum)
567{
568 int i;
569 VARDECL(int, metric);
570 int cost0;
571 int cost1;
572 VARDECL(int, path0);
573 VARDECL(int, path1);
574 VARDECL(celt_norm, tmp);
575 int lambda;
576 int tf_select=0;
577 SAVE_STACK;
578
579 if (nbCompressedBytes<15*C)
580 {
581 *tf_sum = 0;
582 for (i=0;i<len;i++)
583 tf_res[i] = isTransient;
584 return 0;
585 }
586 if (nbCompressedBytes<40)
587 lambda = 12;
588 else if (nbCompressedBytes<60)
589 lambda = 6;
590 else if (nbCompressedBytes<100)
591 lambda = 4;
592 else
593 lambda = 3;
594
595 ALLOC(metric, len, int);
596 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
597 ALLOC(path0, len, int);
598 ALLOC(path1, len, int);
599
600 *tf_sum = 0;
601 for (i=0;i<len;i++)
602 {
603 int j, k, N;
604 opus_val32 L1, best_L1;
605 int best_level=0;
606 N = (m->eBands[i+1]-m->eBands[i])<<LM;
607 for (j=0;j<N;j++)
608 tmp[j] = X[j+(m->eBands[i]<<LM)];
609 /* Just add the right channel if we're in stereo */
610 if (C==2)
611 for (j=0;j<N;j++)
612 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));
613 L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM);
614 best_L1 = L1;
615 /*printf ("%f ", L1);*/
616 for (k=0;k<LM;k++)
617 {
618 int B;
619
620 if (isTransient)
621 B = (LM-k-1);
622 else
623 B = k+1;
624
625 if (isTransient)
626 haar1(tmp, N>>(LM-k), 1<<(LM-k));
627 else
628 haar1(tmp, N>>k, 1<<k);
629
630 L1 = l1_metric(tmp, N, B, N>>LM);
631
632 if (L1 < best_L1)
633 {
634 best_L1 = L1;
635 best_level = k+1;
636 }
637 }
638 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
639 if (isTransient)
640 metric[i] = best_level;
641 else
642 metric[i] = -best_level;
643 *tf_sum += metric[i];
644 }
645 /*printf("\n");*/
646 /* NOTE: Future optimized implementations could detect extreme transients and set
647 tf_select = 1 but so far we have not found a reliable way of making this useful */
648 tf_select = 0;
649
650 cost0 = 0;
651 cost1 = isTransient ? 0 : lambda;
652 /* Viterbi forward pass */
653 for (i=1;i<len;i++)
654 {
655 int curr0, curr1;
656 int from0, from1;
657
658 from0 = cost0;
659 from1 = cost1 + lambda;
660 if (from0 < from1)
661 {
662 curr0 = from0;
663 path0[i]= 0;
664 } else {
665 curr0 = from1;
666 path0[i]= 1;
667 }
668
669 from0 = cost0 + lambda;
670 from1 = cost1;
671 if (from0 < from1)
672 {
673 curr1 = from0;
674 path1[i]= 0;
675 } else {
676 curr1 = from1;
677 path1[i]= 1;
678 }
679 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
680 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
681 }
682 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
683 /* Viterbi backward pass to check the decisions */
684 for (i=len-2;i>=0;i--)
685 {
686 if (tf_res[i+1] == 1)
687 tf_res[i] = path1[i+1];
688 else
689 tf_res[i] = path0[i+1];
690 }
691 RESTORE_STACK;
692#ifdef FUZZING
693 tf_select = rand()&0x1;
694 tf_res[0] = rand()&0x1;
695 for (i=1;i<len;i++)
696 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
697#endif
698 return tf_select;
699}
700
701static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
702{
703 int curr, i;
704 int tf_select_rsv;
705 int tf_changed;
706 int logp;
707 opus_uint32 budget;
708 opus_uint32 tell;
709 budget = enc->storage*8;
710 tell = ec_tell(enc);
711 logp = isTransient ? 2 : 4;
712 /* Reserve space to code the tf_select decision. */
713 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
714 budget -= tf_select_rsv;
715 curr = tf_changed = 0;
716 for (i=start;i<end;i++)
717 {
718 if (tell+logp<=budget)
719 {
720 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
721 tell = ec_tell(enc);
722 curr = tf_res[i];
723 tf_changed |= curr;
724 }
725 else
726 tf_res[i] = curr;
727 logp = isTransient ? 4 : 5;
728 }
729 /* Only code tf_select if it would actually make a difference. */
730 if (tf_select_rsv &&
731 tf_select_table[LM][4*isTransient+0+tf_changed]!=
732 tf_select_table[LM][4*isTransient+2+tf_changed])
733 ec_enc_bit_logp(enc, tf_select, 1);
734 else
735 tf_select = 0;
736 for (i=start;i<end;i++)
737 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
738 /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
739}
740
741static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
742{
743 int i, curr, tf_select;
744 int tf_select_rsv;
745 int tf_changed;
746 int logp;
747 opus_uint32 budget;
748 opus_uint32 tell;
749
750 budget = dec->storage*8;
751 tell = ec_tell(dec);
752 logp = isTransient ? 2 : 4;
753 tf_select_rsv = LM>0 && tell+logp+1<=budget;
754 budget -= tf_select_rsv;
755 tf_changed = curr = 0;
756 for (i=start;i<end;i++)
757 {
758 if (tell+logp<=budget)
759 {
760 curr ^= ec_dec_bit_logp(dec, logp);
761 tell = ec_tell(dec);
762 tf_changed |= curr;
763 }
764 tf_res[i] = curr;
765 logp = isTransient ? 4 : 5;
766 }
767 tf_select = 0;
768 if (tf_select_rsv &&
769 tf_select_table[LM][4*isTransient+0+tf_changed] !=
770 tf_select_table[LM][4*isTransient+2+tf_changed])
771 {
772 tf_select = ec_dec_bit_logp(dec, 1);
773 }
774 for (i=start;i<end;i++)
775 {
776 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
777 }
778}
779
780static void init_caps(const CELTMode *m,int *cap,int LM,int C)
781{
782 int i;
783 for (i=0;i<m->nbEBands;i++)
784 {
785 int N;
786 N=(m->eBands[i+1]-m->eBands[i])<<LM;
787 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
788 }
789}
790
791static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
792 const opus_val16 *bandLogE, int end, int LM, int C, int N0)
793{
794 int i;
795 opus_val32 diff=0;
796 int c;
797 int trim_index = 5;
798 if (C==2)
799 {
800 opus_val16 sum = 0; /* Q10 */
801 /* Compute inter-channel correlation for low frequencies */
802 for (i=0;i<8;i++)
803 {
804 int j;
805 opus_val32 partial = 0;
806 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
807 partial = MAC16_16(partial, X[j], X[N0+j]);
808 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
809 }
810 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
811 /*printf ("%f\n", sum);*/
812 if (sum > QCONST16(.995f,10))
813 trim_index-=4;
814 else if (sum > QCONST16(.92f,10))
815 trim_index-=3;
816 else if (sum > QCONST16(.85f,10))
817 trim_index-=2;
818 else if (sum > QCONST16(.8f,10))
819 trim_index-=1;
820 }
821
822 /* Estimate spectral tilt */
823 c=0; do {
824 for (i=0;i<end-1;i++)
825 {
826 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
827 }
828 } while (++c<C);
829 /* We divide by two here to avoid making the tilt larger for stereo as a
830 result of a bug in the loop above */
831 diff /= 2*C*(end-1);
832 /*printf("%f\n", diff);*/
833 if (diff > QCONST16(2.f, DB_SHIFT))
834 trim_index--;
835 if (diff > QCONST16(8.f, DB_SHIFT))
836 trim_index--;
837 if (diff < -QCONST16(4.f, DB_SHIFT))
838 trim_index++;
839 if (diff < -QCONST16(10.f, DB_SHIFT))
840 trim_index++;
841
842 if (trim_index<0)
843 trim_index = 0;
844 if (trim_index>10)
845 trim_index = 10;
846#ifdef FUZZING
847 trim_index = rand()%11;
848#endif
849 return trim_index;
850}
851
852static int stereo_analysis(const CELTMode *m, const celt_norm *X,
853 int LM, int N0)
854{
855 int i;
856 int thetas;
857 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
858
859 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
860 for (i=0;i<13;i++)
861 {
862 int j;
863 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
864 {
865 opus_val32 L, R, M, S;
866 /* We cast to 32-bit first because of the -32768 case */
867 L = EXTEND32(X[j]);
868 R = EXTEND32(X[N0+j]);
869 M = ADD32(L, R);
870 S = SUB32(L, R);
871 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
872 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
873 }
874 }
875 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
876 thetas = 13;
877 /* We don't need thetas for lower bands with LM<=1 */
878 if (LM<=1)
879 thetas -= 8;
880 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
881 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
882}
883
884int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
885{
886 int i, c, N;
887 opus_int32 bits;
888 ec_enc _enc;
889 VARDECL(celt_sig, in);
890 VARDECL(celt_sig, freq);
891 VARDECL(celt_norm, X);
892 VARDECL(celt_ener, bandE);
893 VARDECL(opus_val16, bandLogE);
894 VARDECL(int, fine_quant);
895 VARDECL(opus_val16, error);
896 VARDECL(int, pulses);
897 VARDECL(int, cap);
898 VARDECL(int, offsets);
899 VARDECL(int, fine_priority);
900 VARDECL(int, tf_res);
901 VARDECL(unsigned char, collapse_masks);
902 celt_sig *prefilter_mem;
903 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
904 int shortBlocks=0;
905 int isTransient=0;
906 const int CC = st->channels;
907 const int C = st->stream_channels;
908 int LM, M;
909 int tf_select;
910 int nbFilledBytes, nbAvailableBytes;
911 int effEnd;
912 int codedBands;
913 int tf_sum;
914 int alloc_trim;
915 int pitch_index=COMBFILTER_MINPERIOD;
916 opus_val16 gain1 = 0;
917 int intensity=0;
918 int dual_stereo=0;
919 int effectiveBytes;
920 opus_val16 pf_threshold;
921 int dynalloc_logp;
922 opus_int32 vbr_rate;
923 opus_int32 total_bits;
924 opus_int32 total_boost;
925 opus_int32 balance;
926 opus_int32 tell;
927 int prefilter_tapset=0;
928 int pf_on;
929 int anti_collapse_rsv;
930 int anti_collapse_on=0;
931 int silence=0;
932 ALLOC_STACK;
933
934 if (nbCompressedBytes<2 || pcm==NULL)
935 return OPUS_BAD_ARG;
936
937 frame_size *= st->upsample;
938 for (LM=0;LM<=st->mode->maxLM;LM++)
939 if (st->mode->shortMdctSize<<LM==frame_size)
940 break;
941 if (LM>st->mode->maxLM)
942 return OPUS_BAD_ARG;
943 M=1<<LM;
944 N = M*st->mode->shortMdctSize;
945
946 prefilter_mem = st->in_mem+CC*(st->overlap);
947 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
948 oldLogE = oldBandE + CC*st->mode->nbEBands;
949 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
950
951 if (enc==NULL)
952 {
953 tell=1;
954 nbFilledBytes=0;
955 } else {
956 tell=ec_tell(enc);
957 nbFilledBytes=(tell+4)>>3;
958 }
959
960#ifdef CUSTOM_MODES
961 if (st->signalling && enc==NULL)
962 {
963 int tmp = (st->mode->effEBands-st->end)>>1;
964 st->end = IMAX(1, st->mode->effEBands-tmp);
965 compressed[0] = tmp<<5;
966 compressed[0] |= LM<<3;
967 compressed[0] |= (C==2)<<2;
968 /* Convert "standard mode" to Opus header */
969 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
970 {
971 int c0 = toOpus(compressed[0]);
972 if (c0<0)
973 return OPUS_BAD_ARG;
974 compressed[0] = c0;
975 }
976 compressed++;
977 nbCompressedBytes--;
978 }
979#else
980 celt_assert(st->signalling==0);
981#endif
982
983 /* Can't produce more than 1275 output bytes */
984 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
985 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
986
987 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
988 {
989 opus_int32 den=st->mode->Fs>>BITRES;
990 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
991#ifdef CUSTOM_MODES
992 if (st->signalling)
993 vbr_rate -= 8<<BITRES;
994#endif
995 effectiveBytes = vbr_rate>>(3+BITRES);
996 } else {
997 opus_int32 tmp;
998 vbr_rate = 0;
999 tmp = st->bitrate*frame_size;
1000 if (tell>1)
1001 tmp += tell;
1002 if (st->bitrate!=OPUS_BITRATE_MAX)
1003 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1004 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1005 effectiveBytes = nbCompressedBytes;
1006 }
1007
1008 if (enc==NULL)
1009 {
1010 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1011 enc = &_enc;
1012 }
1013
1014 if (vbr_rate>0)
1015 {
1016 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1017 target rate and buffering.
1018 We must do this up front so that bust-prevention logic triggers
1019 correctly if we don't have enough bits. */
1020 if (st->constrained_vbr)
1021 {
1022 opus_int32 vbr_bound;
1023 opus_int32 max_allowed;
1024 /* We could use any multiple of vbr_rate as bound (depending on the
1025 delay).
1026 This is clamped to ensure we use at least two bytes if the encoder
1027 was entirely empty, but to allow 0 in hybrid mode. */
1028 vbr_bound = vbr_rate;
1029 max_allowed = IMIN(IMAX(tell==1?2:0,
1030 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1031 nbAvailableBytes);
1032 if(max_allowed < nbAvailableBytes)
1033 {
1034 nbCompressedBytes = nbFilledBytes+max_allowed;
1035 nbAvailableBytes = max_allowed;
1036 ec_enc_shrink(enc, nbCompressedBytes);
1037 }
1038 }
1039 }
1040 total_bits = nbCompressedBytes*8;
1041
1042 effEnd = st->end;
1043 if (effEnd > st->mode->effEBands)
1044 effEnd = st->mode->effEBands;
1045
1046 ALLOC(in, CC*(N+st->overlap), celt_sig);
1047
1048 /* Find pitch period and gain */
1049 {
1050 VARDECL(celt_sig, _pre);
1051 celt_sig *pre[2];
1052 SAVE_STACK;
1053 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1054
1055 pre[0] = _pre;
1056 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1057
1058 silence = 1;
1059 c=0; do {
1060 int count = 0;
1061 const opus_val16 * OPUS_RESTRICT pcmp = pcm+c;
1062 celt_sig * OPUS_RESTRICT inp = in+c*(N+st->overlap)+st->overlap;
1063
1064 for (i=0;i<N;i++)
1065 {
1066 celt_sig x, tmp;
1067
1068 x = SCALEIN(*pcmp);
1069#ifndef FIXED_POINT
1070 if (!(x==x))
1071 x = 0;
1072 if (st->clip)
1073 x = MAX32(-65536.f, MIN32(65536.f,x));
1074#endif
1075 if (++count==st->upsample)
1076 {
1077 count=0;
1078 pcmp+=CC;
1079 } else {
1080 x = 0;
1081 }
1082 /* Apply pre-emphasis */
1083 tmp = MULT16_16(st->mode->preemph[2], x);
1084 *inp = tmp + st->preemph_memE[c];
1085 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1086 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1087 silence = silence && *inp == 0;
1088 inp++;
1089 }
1090 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1091 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1092 } while (++c<CC);
1093
1094#ifdef FUZZING
1095 if ((rand()&0x3F)==0)
1096 silence = 1;
1097#endif
1098 if (tell==1)
1099 ec_enc_bit_logp(enc, silence, 15);
1100 else
1101 silence=0;
1102 if (silence)
1103 {
1104 /*In VBR mode there is no need to send more than the minimum. */
1105 if (vbr_rate>0)
1106 {
1107 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1108 total_bits=nbCompressedBytes*8;
1109 nbAvailableBytes=2;
1110 ec_enc_shrink(enc, nbCompressedBytes);
1111 }
1112 /* Pretend we've filled all the remaining bits with zeros
1113 (that's what the initialiser did anyway) */
1114 tell = nbCompressedBytes*8;
1115 enc->nbits_total+=tell-ec_tell(enc);
1116 }
1117 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1118 {
1119 VARDECL(opus_val16, pitch_buf);
1120 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1121
1122 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1123 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1124 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1125 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1126
1127 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1128 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1129 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1130 pitch_index = COMBFILTER_MAXPERIOD-2;
1131 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1132 if (st->loss_rate>2)
1133 gain1 = HALF32(gain1);
1134 if (st->loss_rate>4)
1135 gain1 = HALF32(gain1);
1136 if (st->loss_rate>8)
1137 gain1 = 0;
1138 prefilter_tapset = st->tapset_decision;
1139 } else {
1140 gain1 = 0;
1141 }
1142
1143 /* Gain threshold for enabling the prefilter/postfilter */
1144 pf_threshold = QCONST16(.2f,15);
1145
1146 /* Adjusting the threshold based on rate and continuity */
1147 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1148 pf_threshold += QCONST16(.2f,15);
1149 if (nbAvailableBytes<25)
1150 pf_threshold += QCONST16(.1f,15);
1151 if (nbAvailableBytes<35)
1152 pf_threshold += QCONST16(.1f,15);
1153 if (st->prefilter_gain > QCONST16(.4f,15))
1154 pf_threshold -= QCONST16(.1f,15);
1155 if (st->prefilter_gain > QCONST16(.55f,15))
1156 pf_threshold -= QCONST16(.1f,15);
1157
1158 /* Hard threshold at 0.2 */
1159 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1160 if (gain1<pf_threshold)
1161 {
1162 if(st->start==0 && tell+16<=total_bits)
1163 ec_enc_bit_logp(enc, 0, 1);
1164 gain1 = 0;
1165 pf_on = 0;
1166 } else {
1167 /*This block is not gated by a total bits check only because
1168 of the nbAvailableBytes check above.*/
1169 int qg;
1170 int octave;
1171
1172 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1173 gain1=st->prefilter_gain;
1174
1175#ifdef FIXED_POINT
1176 qg = ((gain1+1536)>>10)/3-1;
1177#else
1178 qg = (int)floor(.5f+gain1*32/3)-1;
1179#endif
1180 qg = IMAX(0, IMIN(7, qg));
1181 ec_enc_bit_logp(enc, 1, 1);
1182 pitch_index += 1;
1183 octave = EC_ILOG(pitch_index)-5;
1184 ec_enc_uint(enc, octave, 6);
1185 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1186 pitch_index -= 1;
1187 ec_enc_bits(enc, qg, 3);
1188 if (ec_tell(enc)+2<=total_bits)
1189 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1190 else
1191 prefilter_tapset = 0;
1192 gain1 = QCONST16(0.09375f,15)*(qg+1);
1193 pf_on = 1;
1194 }
1195 /*printf("%d %f\n", pitch_index, gain1);*/
1196
1197 c=0; do {
1198 int offset = st->mode->shortMdctSize-st->mode->overlap;
1199 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1200 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1201 if (offset)
1202 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1203 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1204 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1205
1206 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1207 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1208 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1209 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1210
1211 if (N>COMBFILTER_MAXPERIOD)
1212 {
1213 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1214 } else {
1215 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1216 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1217 }
1218 } while (++c<CC);
1219
1220 RESTORE_STACK;
1221 }
1222
1223 isTransient = 0;
1224 shortBlocks = 0;
1225 if (LM>0 && ec_tell(enc)+3<=total_bits)
1226 {
1227 if (st->complexity > 1)
1228 {
1229 isTransient = transient_analysis(in, N+st->overlap, CC,
1230 st->overlap);
1231 if (isTransient)
1232 shortBlocks = M;
1233 }
1234 ec_enc_bit_logp(enc, isTransient, 3);
1235 }
1236
1237 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1238 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1239 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1240 /* Compute MDCTs */
1241 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1242
1243 if (CC==2&&C==1)
1244 {
1245 for (i=0;i<N;i++)
1246 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1247 }
1248 if (st->upsample != 1)
1249 {
1250 c=0; do
1251 {
1252 int bound = N/st->upsample;
1253 for (i=0;i<bound;i++)
1254 freq[c*N+i] *= st->upsample;
1255 for (;i<N;i++)
1256 freq[c*N+i] = 0;
1257 } while (++c<C);
1258 }
1259 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1260
1261 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1262
1263 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1264
1265 /* Band normalisation */
1266 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1267
1268 ALLOC(tf_res, st->mode->nbEBands, int);
1269 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1270 for (i=effEnd;i<st->end;i++)
1271 tf_res[i] = tf_res[effEnd-1];
1272
1273 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1274 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1275 oldBandE, total_bits, error, enc,
1276 C, LM, nbAvailableBytes, st->force_intra,
1277 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1278
1279 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1280
1281 st->spread_decision = SPREAD_NORMAL;
1282 if (ec_tell(enc)+4<=total_bits)
1283 {
1284 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1285 {
1286 if (st->complexity == 0)
1287 st->spread_decision = SPREAD_NONE;
1288 } else {
1289 st->spread_decision = spreading_decision(st->mode, X,
1290 &st->tonal_average, st->spread_decision, &st->hf_average,
1291 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1292 }
1293 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1294 }
1295
1296 ALLOC(cap, st->mode->nbEBands, int);
1297 ALLOC(offsets, st->mode->nbEBands, int);
1298
1299 init_caps(st->mode,cap,LM,C);
1300 for (i=0;i<st->mode->nbEBands;i++)
1301 offsets[i] = 0;
1302 /* Dynamic allocation code */
1303 /* Make sure that dynamic allocation can't make us bust the budget */
1304 if (effectiveBytes > 50 && LM>=1)
1305 {
1306 int t1, t2;
1307 if (LM <= 1)
1308 {
1309 t1 = 3;
1310 t2 = 5;
1311 } else {
1312 t1 = 2;
1313 t2 = 4;
1314 }
1315 for (i=st->start+1;i<st->end-1;i++)
1316 {
1317 opus_val32 d2;
1318 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1319 if (C==2)
1320 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1321 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1322#ifdef FUZZING
1323 if((rand()&0xF)==0)
1324 {
1325 offsets[i] += 1;
1326 if((rand()&0x3)==0)
1327 offsets[i] += 1+(rand()&0x3);
1328 }
1329#else
1330 if (d2 > SHL16(t1,DB_SHIFT))
1331 offsets[i] += 1;
1332 if (d2 > SHL16(t2,DB_SHIFT))
1333 offsets[i] += 1;
1334#endif
1335 }
1336 }
1337 dynalloc_logp = 6;
1338 total_bits<<=BITRES;
1339 total_boost = 0;
1340 tell = ec_tell_frac(enc);
1341 for (i=st->start;i<st->end;i++)
1342 {
1343 int width, quanta;
1344 int dynalloc_loop_logp;
1345 int boost;
1346 int j;
1347 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1348 /* quanta is 6 bits, but no more than 1 bit/sample
1349 and no less than 1/8 bit/sample */
1350 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1351 dynalloc_loop_logp = dynalloc_logp;
1352 boost = 0;
1353 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1354 && boost < cap[i]; j++)
1355 {
1356 int flag;
1357 flag = j<offsets[i];
1358 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1359 tell = ec_tell_frac(enc);
1360 if (!flag)
1361 break;
1362 boost += quanta;
1363 total_boost += quanta;
1364 dynalloc_loop_logp = 1;
1365 }
1366 /* Making dynalloc more likely */
1367 if (j)
1368 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1369 offsets[i] = boost;
1370 }
1371 alloc_trim = 5;
1372 if (tell+(6<<BITRES) <= total_bits - total_boost)
1373 {
1374 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1375 st->end, LM, C, N);
1376 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1377 tell = ec_tell_frac(enc);
1378 }
1379
1380 /* Variable bitrate */
1381 if (vbr_rate>0)
1382 {
1383 opus_val16 alpha;
1384 opus_int32 delta;
1385 /* The target rate in 8th bits per frame */
1386 opus_int32 target;
1387 opus_int32 min_allowed;
1388 int lm_diff = st->mode->maxLM - LM;
1389
1390 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1391 The CELT allocator will just not be able to use more than that anyway. */
1392 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1393 target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES);
1394
1395 /* Shortblocks get a large boost in bitrate, but since they
1396 are uncommon long blocks are not greatly affected */
1397 if (shortBlocks || tf_sum < -2*(st->end-st->start))
1398 target = 7*target/4;
1399 else if (tf_sum < -(st->end-st->start))
1400 target = 3*target/2;
1401 else if (M > 1)
1402 target-=(target+14)/28;
1403
1404 /* The current offset is removed from the target and the space used
1405 so far is added*/
1406 target=target+tell;
1407
1408 /* In VBR mode the frame size must not be reduced so much that it would
1409 result in the encoder running out of bits.
1410 The margin of 2 bytes ensures that none of the bust-prevention logic
1411 in the decoder will have triggered so far. */
1412 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1413
1414 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1415 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1416 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1417
1418 /* By how much did we "miss" the target on that frame */
1419 delta = target - vbr_rate;
1420
1421 target=nbAvailableBytes<<(BITRES+3);
1422
1423 /*If the frame is silent we don't adjust our drift, otherwise
1424 the encoder will shoot to very high rates after hitting a
1425 span of silence, but we do allow the bitres to refill.
1426 This means that we'll undershoot our target in CVBR/VBR modes
1427 on files with lots of silence. */
1428 if(silence)
1429 {
1430 nbAvailableBytes = 2;
1431 target = 2*8<<BITRES;
1432 delta = 0;
1433 }
1434
1435 if (st->vbr_count < 970)
1436 {
1437 st->vbr_count++;
1438 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1439 } else
1440 alpha = QCONST16(.001f,15);
1441 /* How many bits have we used in excess of what we're allowed */
1442 if (st->constrained_vbr)
1443 st->vbr_reservoir += target - vbr_rate;
1444 /*printf ("%d\n", st->vbr_reservoir);*/
1445
1446 /* Compute the offset we need to apply in order to reach the target */
1447 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1448 st->vbr_offset = -st->vbr_drift;
1449 /*printf ("%d\n", st->vbr_drift);*/
1450
1451 if (st->constrained_vbr && st->vbr_reservoir < 0)
1452 {
1453 /* We're under the min value -- increase rate */
1454 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1455 /* Unless we're just coding silence */
1456 nbAvailableBytes += silence?0:adjust;
1457 st->vbr_reservoir = 0;
1458 /*printf ("+%d\n", adjust);*/
1459 }
1460 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1461 /* This moves the raw bits to take into account the new compressed size */
1462 ec_enc_shrink(enc, nbCompressedBytes);
1463 }
1464 if (C==2)
1465 {
1466 int effectiveRate;
1467
1468 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1469 if (LM!=0)
1470 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1471
1472 /* Account for coarse energy */
1473 effectiveRate = (8*effectiveBytes - 80)>>LM;
1474
1475 /* effectiveRate in kb/s */
1476 effectiveRate = 2*effectiveRate/5;
1477 if (effectiveRate<35)
1478 intensity = 8;
1479 else if (effectiveRate<50)
1480 intensity = 12;
1481 else if (effectiveRate<68)
1482 intensity = 16;
1483 else if (effectiveRate<84)
1484 intensity = 18;
1485 else if (effectiveRate<102)
1486 intensity = 19;
1487 else if (effectiveRate<130)
1488 intensity = 20;
1489 else
1490 intensity = 100;
1491 intensity = IMIN(st->end,IMAX(st->start, intensity));
1492 }
1493
1494 /* Bit allocation */
1495 ALLOC(fine_quant, st->mode->nbEBands, int);
1496 ALLOC(pulses, st->mode->nbEBands, int);
1497 ALLOC(fine_priority, st->mode->nbEBands, int);
1498
1499 /* bits = packet size - where we are - safety*/
1500 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1501 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1502 bits -= anti_collapse_rsv;
1503 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1504 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1505 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1506 st->lastCodedBands = codedBands;
1507
1508 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1509
1510#ifdef MEASURE_NORM_MSE
1511 float X0[3000];
1512 float bandE0[60];
1513 c=0; do
1514 for (i=0;i<N;i++)
1515 X0[i+c*N] = X[i+c*N];
1516 while (++c<C);
1517 for (i=0;i<C*st->mode->nbEBands;i++)
1518 bandE0[i] = bandE[i];
1519#endif
1520
1521 /* Residual quantisation */
1522 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1523 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1524 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1525 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1526
1527 if (anti_collapse_rsv > 0)
1528 {
1529 anti_collapse_on = st->consec_transient<2;
1530#ifdef FUZZING
1531 anti_collapse_on = rand()&0x1;
1532#endif
1533 ec_enc_bits(enc, anti_collapse_on, 1);
1534 }
1535 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1536
1537 if (silence)
1538 {
1539 for (i=0;i<C*st->mode->nbEBands;i++)
1540 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1541 }
1542
1543#ifdef RESYNTH
1544 /* Re-synthesis of the coded audio if required */
1545 {
1546 celt_sig *out_mem[2];
1547 celt_sig *overlap_mem[2];
1548
1549 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1550 if (silence)
1551 {
1552 for (i=0;i<C*st->mode->nbEBands;i++)
1553 bandE[i] = 0;
1554 }
1555
1556#ifdef MEASURE_NORM_MSE
1557 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1558#endif
1559 if (anti_collapse_on)
1560 {
1561 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1562 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1563 }
1564
1565 /* Synthesis */
1566 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1567
1568 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1569 if (CC==2)
1570 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1571
1572 c=0; do
1573 for (i=0;i<M*st->mode->eBands[st->start];i++)
1574 freq[c*N+i] = 0;
1575 while (++c<C);
1576 c=0; do
1577 for (i=M*st->mode->eBands[st->end];i<N;i++)
1578 freq[c*N+i] = 0;
1579 while (++c<C);
1580
1581 if (CC==2&&C==1)
1582 {
1583 for (i=0;i<N;i++)
1584 freq[N+i] = freq[i];
1585 }
1586
1587 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1588 if (CC==2)
1589 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1590
1591 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1592 if (CC==2)
1593 overlap_mem[1] = overlap_mem[0] + st->overlap;
1594
1595 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1596
1597 c=0; do {
1598 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1599 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1600 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1601 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1602 st->mode->window, st->overlap);
1603 if (LM!=0)
1604 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1605 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1606 st->mode->window, st->mode->overlap);
1607 } while (++c<CC);
1608
1609 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1610 st->prefilter_period_old = st->prefilter_period;
1611 st->prefilter_gain_old = st->prefilter_gain;
1612 st->prefilter_tapset_old = st->prefilter_tapset;
1613 }
1614#endif
1615
1616 st->prefilter_period = pitch_index;
1617 st->prefilter_gain = gain1;
1618 st->prefilter_tapset = prefilter_tapset;
1619#ifdef RESYNTH
1620 if (LM!=0)
1621 {
1622 st->prefilter_period_old = st->prefilter_period;
1623 st->prefilter_gain_old = st->prefilter_gain;
1624 st->prefilter_tapset_old = st->prefilter_tapset;
1625 }
1626#endif
1627
1628 if (CC==2&&C==1) {
1629 for (i=0;i<st->mode->nbEBands;i++)
1630 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1631 }
1632
1633 if (!isTransient)
1634 {
1635 for (i=0;i<CC*st->mode->nbEBands;i++)
1636 oldLogE2[i] = oldLogE[i];
1637 for (i=0;i<CC*st->mode->nbEBands;i++)
1638 oldLogE[i] = oldBandE[i];
1639 } else {
1640 for (i=0;i<CC*st->mode->nbEBands;i++)
1641 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1642 }
1643 /* In case start or end were to change */
1644 c=0; do
1645 {
1646 for (i=0;i<st->start;i++)
1647 {
1648 oldBandE[c*st->mode->nbEBands+i]=0;
1649 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1650 }
1651 for (i=st->end;i<st->mode->nbEBands;i++)
1652 {
1653 oldBandE[c*st->mode->nbEBands+i]=0;
1654 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1655 }
1656 } while (++c<CC);
1657
1658 if (isTransient)
1659 st->consec_transient++;
1660 else
1661 st->consec_transient=0;
1662 st->rng = enc->rng;
1663
1664 /* If there's any room left (can only happen for very high rates),
1665 it's already filled with zeros */
1666 ec_enc_done(enc);
1667
1668#ifdef CUSTOM_MODES
1669 if (st->signalling)
1670 nbCompressedBytes++;
1671#endif
1672
1673 RESTORE_STACK;
1674 if (ec_get_error(enc))
1675 return OPUS_INTERNAL_ERROR;
1676 else
1677 return nbCompressedBytes;
1678}
1679
1680
1681#ifdef CUSTOM_MODES
1682
1683#ifdef FIXED_POINT
1684int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1685{
1686 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1687}
1688
1689#ifndef DISABLE_FLOAT_API
1690int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1691{
1692 int j, ret, C, N;
1693 VARDECL(opus_int16, in);
1694 ALLOC_STACK;
1695
1696 if (pcm==NULL)
1697 return OPUS_BAD_ARG;
1698
1699 C = st->channels;
1700 N = frame_size;
1701 ALLOC(in, C*N, opus_int16);
1702
1703 for (j=0;j<C*N;j++)
1704 in[j] = FLOAT2INT16(pcm[j]);
1705
1706 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1707#ifdef RESYNTH
1708 for (j=0;j<C*N;j++)
1709 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1710#endif
1711 RESTORE_STACK;
1712 return ret;
1713}
1714#endif /* DISABLE_FLOAT_API */
1715#else
1716
1717int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1718{
1719 int j, ret, C, N;
1720 VARDECL(celt_sig, in);
1721 ALLOC_STACK;
1722
1723 if (pcm==NULL)
1724 return OPUS_BAD_ARG;
1725
1726 C=st->channels;
1727 N=frame_size;
1728 ALLOC(in, C*N, celt_sig);
1729 for (j=0;j<C*N;j++) {
1730 in[j] = SCALEOUT(pcm[j]);
1731 }
1732
1733 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1734#ifdef RESYNTH
1735 for (j=0;j<C*N;j++)
1736 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1737#endif
1738 RESTORE_STACK;
1739 return ret;
1740}
1741
1742int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1743{
1744 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1745}
1746
1747#endif
1748
1749#endif /* CUSTOM_MODES */
1750
1751int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
1752{
1753 va_list ap;
1754
1755 va_start(ap, request);
1756 switch (request)
1757 {
1758 case OPUS_SET_COMPLEXITY_REQUEST:
1759 {
1760 int value = va_arg(ap, opus_int32);
1761 if (value<0 || value>10)
1762 goto bad_arg;
1763 st->complexity = value;
1764 }
1765 break;
1766 case CELT_SET_START_BAND_REQUEST:
1767 {
1768 opus_int32 value = va_arg(ap, opus_int32);
1769 if (value<0 || value>=st->mode->nbEBands)
1770 goto bad_arg;
1771 st->start = value;
1772 }
1773 break;
1774 case CELT_SET_END_BAND_REQUEST:
1775 {
1776 opus_int32 value = va_arg(ap, opus_int32);
1777 if (value<1 || value>st->mode->nbEBands)
1778 goto bad_arg;
1779 st->end = value;
1780 }
1781 break;
1782 case CELT_SET_PREDICTION_REQUEST:
1783 {
1784 int value = va_arg(ap, opus_int32);
1785 if (value<0 || value>2)
1786 goto bad_arg;
1787 st->disable_pf = value<=1;
1788 st->force_intra = value==0;
1789 }
1790 break;
1791 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1792 {
1793 int value = va_arg(ap, opus_int32);
1794 if (value<0 || value>100)
1795 goto bad_arg;
1796 st->loss_rate = value;
1797 }
1798 break;
1799 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1800 {
1801 opus_int32 value = va_arg(ap, opus_int32);
1802 st->constrained_vbr = value;
1803 }
1804 break;
1805 case OPUS_SET_VBR_REQUEST:
1806 {
1807 opus_int32 value = va_arg(ap, opus_int32);
1808 st->vbr = value;
1809 }
1810 break;
1811 case OPUS_SET_BITRATE_REQUEST:
1812 {
1813 opus_int32 value = va_arg(ap, opus_int32);
1814 if (value<=500 && value!=OPUS_BITRATE_MAX)
1815 goto bad_arg;
1816 value = IMIN(value, 260000*st->channels);
1817 st->bitrate = value;
1818 }
1819 break;
1820 case CELT_SET_CHANNELS_REQUEST:
1821 {
1822 opus_int32 value = va_arg(ap, opus_int32);
1823 if (value<1 || value>2)
1824 goto bad_arg;
1825 st->stream_channels = value;
1826 }
1827 break;
1828 case OPUS_SET_LSB_DEPTH_REQUEST:
1829 {
1830 opus_int32 value = va_arg(ap, opus_int32);
1831 if (value<8 || value>24)
1832 goto bad_arg;
1833 st->lsb_depth=value;
1834 }
1835 break;
1836 case OPUS_GET_LSB_DEPTH_REQUEST:
1837 {
1838 opus_int32 *value = va_arg(ap, opus_int32*);
1839 *value=st->lsb_depth;
1840 }
1841 break;
1842 case OPUS_RESET_STATE:
1843 {
1844 int i;
1845 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1846 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
1847 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
1848 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
1849 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
1850 opus_custom_encoder_get_size(st->mode, st->channels)-
1851 ((char*)&st->ENCODER_RESET_START - (char*)st));
1852 for (i=0;i<st->channels*st->mode->nbEBands;i++)
1853 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1854 st->vbr_offset = 0;
1855 st->delayedIntra = 1;
1856 st->spread_decision = SPREAD_NORMAL;
1857 st->tonal_average = 256;
1858 st->hf_average = 0;
1859 st->tapset_decision = 0;
1860 }
1861 break;
1862#ifdef CUSTOM_MODES
1863 case CELT_SET_INPUT_CLIPPING_REQUEST:
1864 {
1865 opus_int32 value = va_arg(ap, opus_int32);
1866 st->clip = value;
1867 }
1868 break;
1869#endif
1870 case CELT_SET_SIGNALLING_REQUEST:
1871 {
1872 opus_int32 value = va_arg(ap, opus_int32);
1873 st->signalling = value;
1874 }
1875 break;
1876 case CELT_GET_MODE_REQUEST:
1877 {
1878 const CELTMode ** value = va_arg(ap, const CELTMode**);
1879 if (value==0)
1880 goto bad_arg;
1881 *value=st->mode;
1882 }
1883 break;
1884 case OPUS_GET_FINAL_RANGE_REQUEST:
1885 {
1886 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1887 if (value==0)
1888 goto bad_arg;
1889 *value=st->rng;
1890 }
1891 break;
1892 default:
1893 goto bad_request;
1894 }
1895 va_end(ap);
1896 return OPUS_OK;
1897bad_arg:
1898 va_end(ap);
1899 return OPUS_BAD_ARG;
1900bad_request:
1901 va_end(ap);
1902 return OPUS_UNIMPLEMENTED;
1903}
1904
1905/**********************************************************************/
1906/* */
1907/* DECODER */
1908/* */
1909/**********************************************************************/
1910#define DECODE_BUFFER_SIZE 2048
1911
1912/** Decoder state
1913 @brief Decoder state
1914 */
1915struct OpusCustomDecoder {
1916 const OpusCustomMode *mode;
1917 int overlap;
1918 int channels;
1919 int stream_channels;
1920
1921 int downsample;
1922 int start, end;
1923 int signalling;
1924
1925 /* Everything beyond this point gets cleared on a reset */
1926#define DECODER_RESET_START rng
1927
1928 opus_uint32 rng;
1929 int error;
1930 int last_pitch_index;
1931 int loss_count;
1932 int postfilter_period;
1933 int postfilter_period_old;
1934 opus_val16 postfilter_gain;
1935 opus_val16 postfilter_gain_old;
1936 int postfilter_tapset;
1937 int postfilter_tapset_old;
1938
1939 celt_sig preemph_memD[2];
1940
1941 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
1942 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
1943 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
1944 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
1945 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
1946 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
1947};
1948
1949int celt_decoder_get_size(int channels)
1950{
1951 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
1952 return opus_custom_decoder_get_size(mode, channels);
1953}
1954
1955OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
1956{
1957 int size = sizeof(struct CELTDecoder)
1958 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
1959 + channels*LPC_ORDER*sizeof(opus_val16)
1960 + 4*2*mode->nbEBands*sizeof(opus_val16);
1961 return size;
1962}
1963
1964#ifdef CUSTOM_MODES
1965CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
1966{
1967 int ret;
1968 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
1969 ret = opus_custom_decoder_init(st, mode, channels);
1970 if (ret != OPUS_OK)
1971 {
1972 opus_custom_decoder_destroy(st);
1973 st = NULL;
1974 }
1975 if (error)
1976 *error = ret;
1977 return st;
1978}
1979#endif /* CUSTOM_MODES */
1980
1981int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
1982{
1983 int ret;
1984 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
1985 if (ret != OPUS_OK)
1986 return ret;
1987 st->downsample = resampling_factor(sampling_rate);
1988 if (st->downsample==0)
1989 return OPUS_BAD_ARG;
1990 else
1991 return OPUS_OK;
1992}
1993
1994OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
1995{
1996 if (channels < 0 || channels > 2)
1997 return OPUS_BAD_ARG;
1998
1999 if (st==NULL)
2000 return OPUS_ALLOC_FAIL;
2001
2002 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2003
2004 st->mode = mode;
2005 st->overlap = mode->overlap;
2006 st->stream_channels = st->channels = channels;
2007
2008 st->downsample = 1;
2009 st->start = 0;
2010 st->end = st->mode->effEBands;
2011 st->signalling = 1;
2012
2013 st->loss_count = 0;
2014
2015 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2016
2017 return OPUS_OK;
2018}
2019
2020#ifdef CUSTOM_MODES
2021void opus_custom_decoder_destroy(CELTDecoder *st)
2022{
2023 opus_free(st);
2024}
2025#endif /* CUSTOM_MODES */
2026
2027static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM)
2028{
2029 int c;
2030 int pitch_index;
2031 int overlap = st->mode->overlap;
2032 opus_val16 fade = Q15ONE;
2033 int i, len;
2034 const int C = st->channels;
2035 int offset;
2036 celt_sig *out_mem[2];
2037 celt_sig *decode_mem[2];
2038 celt_sig *overlap_mem[2];
2039 opus_val16 *lpc;
2040 opus_val32 *out_syn[2];
2041 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2042 SAVE_STACK;
2043
2044 c=0; do {
2045 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2046 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2047 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2048 } while (++c<C);
2049 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2050 oldBandE = lpc+C*LPC_ORDER;
2051 oldLogE = oldBandE + 2*st->mode->nbEBands;
2052 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2053 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2054
2055 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2056 if (C==2)
2057 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2058
2059 len = N+st->mode->overlap;
2060
2061 if (st->loss_count >= 5 || st->start!=0)
2062 {
2063 /* Noise-based PLC/CNG */
2064 VARDECL(celt_sig, freq);
2065 VARDECL(celt_norm, X);
2066 VARDECL(celt_ener, bandE);
2067 opus_uint32 seed;
2068 int effEnd;
2069
2070 effEnd = st->end;
2071 if (effEnd > st->mode->effEBands)
2072 effEnd = st->mode->effEBands;
2073
2074 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2075 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2076 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2077
2078 if (st->loss_count >= 5)
2079 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2080 else {
2081 /* Energy decay */
2082 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2083 c=0; do
2084 {
2085 for (i=st->start;i<st->end;i++)
2086 oldBandE[c*st->mode->nbEBands+i] -= decay;
2087 } while (++c<C);
2088 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2089 }
2090 seed = st->rng;
2091 for (c=0;c<C;c++)
2092 {
2093 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2094 X[c*N+i] = 0;
2095 for (i=st->start;i<st->mode->effEBands;i++)
2096 {
2097 int j;
2098 int boffs;
2099 int blen;
2100 boffs = N*c+(st->mode->eBands[i]<<LM);
2101 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2102 for (j=0;j<blen;j++)
2103 {
2104 seed = celt_lcg_rand(seed);
2105 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2106 }
2107 renormalise_vector(X+boffs, blen, Q15ONE);
2108 }
2109 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2110 X[c*N+i] = 0;
2111 }
2112 st->rng = seed;
2113
2114 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2115
2116 c=0; do
2117 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2118 freq[c*N+i] = 0;
2119 while (++c<C);
2120 c=0; do {
2121 int bound = st->mode->eBands[effEnd]<<LM;
2122 if (st->downsample!=1)
2123 bound = IMIN(bound, N/st->downsample);
2124 for (i=bound;i<N;i++)
2125 freq[c*N+i] = 0;
2126 } while (++c<C);
2127 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2128 } else {
2129 /* Pitch-based PLC */
2130 if (st->loss_count == 0)
2131 {
2132 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2133 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2134 search by using only part of the decode buffer */
2135 int poffset = 720;
2136 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2137 /* Max pitch is 100 samples (480 Hz) */
2138 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2139 poffset-100, &pitch_index);
2140 pitch_index = poffset-pitch_index;
2141 st->last_pitch_index = pitch_index;
2142 } else {
2143 pitch_index = st->last_pitch_index;
2144 fade = QCONST16(.8f,15);
2145 }
2146
2147 c=0; do {
2148 VARDECL(opus_val32, e);
2149 opus_val16 exc[MAX_PERIOD];
2150 opus_val32 ac[LPC_ORDER+1];
2151 opus_val16 decay = 1;
2152 opus_val32 S1=0;
2153 opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2154
2155 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2156
2157 offset = MAX_PERIOD-pitch_index;
2158 for (i=0;i<MAX_PERIOD;i++)
2159 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2160
2161 if (st->loss_count == 0)
2162 {
2163 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2164 LPC_ORDER, MAX_PERIOD);
2165
2166 /* Noise floor -40 dB */
2167#ifdef FIXED_POINT
2168 ac[0] += SHR32(ac[0],13);
2169#else
2170 ac[0] *= 1.0001f;
2171#endif
2172 /* Lag windowing */
2173 for (i=1;i<=LPC_ORDER;i++)
2174 {
2175 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2176#ifdef FIXED_POINT
2177 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2178#else
2179 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2180#endif
2181 }
2182
2183 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2184 }
2185 for (i=0;i<LPC_ORDER;i++)
2186 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2187 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2188 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2189 /* Check if the waveform is decaying (and if so how fast) */
2190 {
2191 opus_val32 E1=1, E2=1;
2192 int period;
2193 if (pitch_index <= MAX_PERIOD/2)
2194 period = pitch_index;
2195 else
2196 period = MAX_PERIOD/2;
2197 for (i=0;i<period;i++)
2198 {
2199 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2200 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2201 }
2202 if (E1 > E2)
2203 E1 = E2;
2204 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2205 }
2206
2207 /* Copy excitation, taking decay into account */
2208 for (i=0;i<len+st->mode->overlap;i++)
2209 {
2210 opus_val16 tmp;
2211 if (offset+i >= MAX_PERIOD)
2212 {
2213 offset -= pitch_index;
2214 decay = MULT16_16_Q15(decay, decay);
2215 }
2216 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2217 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2218 S1 += SHR32(MULT16_16(tmp,tmp),8);
2219 }
2220 for (i=0;i<LPC_ORDER;i++)
2221 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2222 for (i=0;i<len+st->mode->overlap;i++)
2223 e[i] = MULT16_32_Q15(fade, e[i]);
2224 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2225
2226 {
2227 opus_val32 S2=0;
2228 for (i=0;i<len+overlap;i++)
2229 {
2230 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2231 S2 += SHR32(MULT16_16(tmp,tmp),8);
2232 }
2233 /* This checks for an "explosion" in the synthesis */
2234#ifdef FIXED_POINT
2235 if (!(S1 > SHR32(S2,2)))
2236#else
2237 /* Float test is written this way to catch NaNs at the same time */
2238 if (!(S1 > 0.2f*S2))
2239#endif
2240 {
2241 for (i=0;i<len+overlap;i++)
2242 e[i] = 0;
2243 } else if (S1 < S2)
2244 {
2245 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2246 for (i=0;i<len+overlap;i++)
2247 e[i] = MULT16_32_Q15(ratio, e[i]);
2248 }
2249 }
2250
2251 /* Apply post-filter to the MDCT overlap of the previous frame */
2252 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2253 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2254 NULL, 0);
2255
2256 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2257 out_mem[c][i] = out_mem[c][N+i];
2258
2259 /* Apply TDAC to the concealed audio so that it blends with the
2260 previous and next frames */
2261 for (i=0;i<overlap/2;i++)
2262 {
2263 opus_val32 tmp;
2264 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2265 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2266 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2267 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2268 }
2269 for (i=0;i<N;i++)
2270 out_mem[c][MAX_PERIOD-N+i] = e[i];
2271
2272 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2273 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2274 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2275 NULL, 0);
2276 for (i=0;i<overlap;i++)
2277 out_mem[c][MAX_PERIOD+i] = e[i];
2278 } while (++c<C);
2279 }
2280
2281 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2282
2283 st->loss_count++;
2284
2285 RESTORE_STACK;
2286}
2287
2288int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec)
2289{
2290 int c, i, N;
2291 int spread_decision;
2292 opus_int32 bits;
2293 ec_dec _dec;
2294 VARDECL(celt_sig, freq);
2295 VARDECL(celt_norm, X);
2296 VARDECL(celt_ener, bandE);
2297 VARDECL(int, fine_quant);
2298 VARDECL(int, pulses);
2299 VARDECL(int, cap);
2300 VARDECL(int, offsets);
2301 VARDECL(int, fine_priority);
2302 VARDECL(int, tf_res);
2303 VARDECL(unsigned char, collapse_masks);
2304 celt_sig *out_mem[2];
2305 celt_sig *decode_mem[2];
2306 celt_sig *overlap_mem[2];
2307 celt_sig *out_syn[2];
2308 opus_val16 *lpc;
2309 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2310
2311 int shortBlocks;
2312 int isTransient;
2313 int intra_ener;
2314 const int CC = st->channels;
2315 int LM, M;
2316 int effEnd;
2317 int codedBands;
2318 int alloc_trim;
2319 int postfilter_pitch;
2320 opus_val16 postfilter_gain;
2321 int intensity=0;
2322 int dual_stereo=0;
2323 opus_int32 total_bits;
2324 opus_int32 balance;
2325 opus_int32 tell;
2326 int dynalloc_logp;
2327 int postfilter_tapset;
2328 int anti_collapse_rsv;
2329 int anti_collapse_on=0;
2330 int silence;
2331 int C = st->stream_channels;
2332 ALLOC_STACK;
2333
2334 frame_size *= st->downsample;
2335
2336 c=0; do {
2337 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2338 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2339 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2340 } while (++c<CC);
2341 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2342 oldBandE = lpc+CC*LPC_ORDER;
2343 oldLogE = oldBandE + 2*st->mode->nbEBands;
2344 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2345 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2346
2347#ifdef CUSTOM_MODES
2348 if (st->signalling && data!=NULL)
2349 {
2350 int data0=data[0];
2351 /* Convert "standard mode" to Opus header */
2352 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2353 {
2354 data0 = fromOpus(data0);
2355 if (data0<0)
2356 return OPUS_INVALID_PACKET;
2357 }
2358 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2359 LM = (data0>>3)&0x3;
2360 C = 1 + ((data0>>2)&0x1);
2361 data++;
2362 len--;
2363 if (LM>st->mode->maxLM)
2364 return OPUS_INVALID_PACKET;
2365 if (frame_size < st->mode->shortMdctSize<<LM)
2366 return OPUS_BUFFER_TOO_SMALL;
2367 else
2368 frame_size = st->mode->shortMdctSize<<LM;
2369 } else {
2370#else
2371 {
2372#endif
2373 for (LM=0;LM<=st->mode->maxLM;LM++)
2374 if (st->mode->shortMdctSize<<LM==frame_size)
2375 break;
2376 if (LM>st->mode->maxLM)
2377 return OPUS_BAD_ARG;
2378 }
2379 M=1<<LM;
2380
2381 if (len<0 || len>1275 || pcm==NULL)
2382 return OPUS_BAD_ARG;
2383
2384 N = M*st->mode->shortMdctSize;
2385
2386 effEnd = st->end;
2387 if (effEnd > st->mode->effEBands)
2388 effEnd = st->mode->effEBands;
2389
2390 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2391 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2392 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2393 c=0; do
2394 for (i=0;i<M*st->mode->eBands[st->start];i++)
2395 X[c*N+i] = 0;
2396 while (++c<C);
2397 c=0; do
2398 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2399 X[c*N+i] = 0;
2400 while (++c<C);
2401
2402 if (data == NULL || len<=1)
2403 {
2404 celt_decode_lost(st, pcm, N, LM);
2405 RESTORE_STACK;
2406 return frame_size/st->downsample;
2407 }
2408
2409 if (dec == NULL)
2410 {
2411 ec_dec_init(&_dec,(unsigned char*)data,len);
2412 dec = &_dec;
2413 }
2414
2415 if (C==1)
2416 {
2417 for (i=0;i<st->mode->nbEBands;i++)
2418 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2419 }
2420
2421 total_bits = len*8;
2422 tell = ec_tell(dec);
2423
2424 if (tell >= total_bits)
2425 silence = 1;
2426 else if (tell==1)
2427 silence = ec_dec_bit_logp(dec, 15);
2428 else
2429 silence = 0;
2430 if (silence)
2431 {
2432 /* Pretend we've read all the remaining bits */
2433 tell = len*8;
2434 dec->nbits_total+=tell-ec_tell(dec);
2435 }
2436
2437 postfilter_gain = 0;
2438 postfilter_pitch = 0;
2439 postfilter_tapset = 0;
2440 if (st->start==0 && tell+16 <= total_bits)
2441 {
2442 if(ec_dec_bit_logp(dec, 1))
2443 {
2444 int qg, octave;
2445 octave = ec_dec_uint(dec, 6);
2446 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2447 qg = ec_dec_bits(dec, 3);
2448 if (ec_tell(dec)+2<=total_bits)
2449 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2450 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2451 }
2452 tell = ec_tell(dec);
2453 }
2454
2455 if (LM > 0 && tell+3 <= total_bits)
2456 {
2457 isTransient = ec_dec_bit_logp(dec, 3);
2458 tell = ec_tell(dec);
2459 }
2460 else
2461 isTransient = 0;
2462
2463 if (isTransient)
2464 shortBlocks = M;
2465 else
2466 shortBlocks = 0;
2467
2468 /* Decode the global flags (first symbols in the stream) */
2469 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2470 /* Get band energies */
2471 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2472 intra_ener, dec, C, LM);
2473
2474 ALLOC(tf_res, st->mode->nbEBands, int);
2475 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2476
2477 tell = ec_tell(dec);
2478 spread_decision = SPREAD_NORMAL;
2479 if (tell+4 <= total_bits)
2480 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2481
2482 ALLOC(pulses, st->mode->nbEBands, int);
2483 ALLOC(cap, st->mode->nbEBands, int);
2484 ALLOC(offsets, st->mode->nbEBands, int);
2485 ALLOC(fine_priority, st->mode->nbEBands, int);
2486
2487 init_caps(st->mode,cap,LM,C);
2488
2489 dynalloc_logp = 6;
2490 total_bits<<=BITRES;
2491 tell = ec_tell_frac(dec);
2492 for (i=st->start;i<st->end;i++)
2493 {
2494 int width, quanta;
2495 int dynalloc_loop_logp;
2496 int boost;
2497 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2498 /* quanta is 6 bits, but no more than 1 bit/sample
2499 and no less than 1/8 bit/sample */
2500 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2501 dynalloc_loop_logp = dynalloc_logp;
2502 boost = 0;
2503 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2504 {
2505 int flag;
2506 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2507 tell = ec_tell_frac(dec);
2508 if (!flag)
2509 break;
2510 boost += quanta;
2511 total_bits -= quanta;
2512 dynalloc_loop_logp = 1;
2513 }
2514 offsets[i] = boost;
2515 /* Making dynalloc more likely */
2516 if (boost>0)
2517 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2518 }
2519
2520 ALLOC(fine_quant, st->mode->nbEBands, int);
2521 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2522 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2523
2524 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2525 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2526 bits -= anti_collapse_rsv;
2527 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2528 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2529 fine_quant, fine_priority, C, LM, dec, 0, 0);
2530
2531 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2532
2533 /* Decode fixed codebook */
2534 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2535 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2536 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2537 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2538
2539 if (anti_collapse_rsv > 0)
2540 {
2541 anti_collapse_on = ec_dec_bits(dec, 1);
2542 }
2543
2544 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2545 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2546
2547 if (anti_collapse_on)
2548 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2549 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2550
2551 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2552
2553 if (silence)
2554 {
2555 for (i=0;i<C*st->mode->nbEBands;i++)
2556 {
2557 bandE[i] = 0;
2558 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2559 }
2560 }
2561 /* Synthesis */
2562 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2563
2564 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2565 if (CC==2)
2566 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2567
2568 c=0; do
2569 for (i=0;i<M*st->mode->eBands[st->start];i++)
2570 freq[c*N+i] = 0;
2571 while (++c<C);
2572 c=0; do {
2573 int bound = M*st->mode->eBands[effEnd];
2574 if (st->downsample!=1)
2575 bound = IMIN(bound, N/st->downsample);
2576 for (i=bound;i<N;i++)
2577 freq[c*N+i] = 0;
2578 } while (++c<C);
2579
2580 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2581 if (CC==2)
2582 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2583
2584 if (CC==2&&C==1)
2585 {
2586 for (i=0;i<N;i++)
2587 freq[N+i] = freq[i];
2588 }
2589 if (CC==1&&C==2)
2590 {
2591 for (i=0;i<N;i++)
2592 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2593 }
2594
2595 /* Compute inverse MDCTs */
2596 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2597
2598 c=0; do {
2599 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2600 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2601 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2602 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2603 st->mode->window, st->overlap);
2604 if (LM!=0)
2605 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2606 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2607 st->mode->window, st->mode->overlap);
2608
2609 } while (++c<CC);
2610 st->postfilter_period_old = st->postfilter_period;
2611 st->postfilter_gain_old = st->postfilter_gain;
2612 st->postfilter_tapset_old = st->postfilter_tapset;
2613 st->postfilter_period = postfilter_pitch;
2614 st->postfilter_gain = postfilter_gain;
2615 st->postfilter_tapset = postfilter_tapset;
2616 if (LM!=0)
2617 {
2618 st->postfilter_period_old = st->postfilter_period;
2619 st->postfilter_gain_old = st->postfilter_gain;
2620 st->postfilter_tapset_old = st->postfilter_tapset;
2621 }
2622
2623 if (C==1) {
2624 for (i=0;i<st->mode->nbEBands;i++)
2625 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2626 }
2627
2628 /* In case start or end were to change */
2629 if (!isTransient)
2630 {
2631 for (i=0;i<2*st->mode->nbEBands;i++)
2632 oldLogE2[i] = oldLogE[i];
2633 for (i=0;i<2*st->mode->nbEBands;i++)
2634 oldLogE[i] = oldBandE[i];
2635 for (i=0;i<2*st->mode->nbEBands;i++)
2636 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2637 } else {
2638 for (i=0;i<2*st->mode->nbEBands;i++)
2639 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2640 }
2641 c=0; do
2642 {
2643 for (i=0;i<st->start;i++)
2644 {
2645 oldBandE[c*st->mode->nbEBands+i]=0;
2646 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2647 }
2648 for (i=st->end;i<st->mode->nbEBands;i++)
2649 {
2650 oldBandE[c*st->mode->nbEBands+i]=0;
2651 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2652 }
2653 } while (++c<2);
2654 st->rng = dec->rng;
2655
2656 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2657 st->loss_count = 0;
2658 RESTORE_STACK;
2659 if (ec_tell(dec) > 8*len)
2660 return OPUS_INTERNAL_ERROR;
2661 if(ec_get_error(dec))
2662 st->error = 1;
2663 return frame_size/st->downsample;
2664}
2665
2666
2667#ifdef CUSTOM_MODES
2668
2669#ifdef FIXED_POINT
2670int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
2671{
2672 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2673}
2674
2675#ifndef DISABLE_FLOAT_API
2676int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
2677{
2678 int j, ret, C, N;
2679 VARDECL(opus_int16, out);
2680 ALLOC_STACK;
2681
2682 if (pcm==NULL)
2683 return OPUS_BAD_ARG;
2684
2685 C = st->channels;
2686 N = frame_size;
2687
2688 ALLOC(out, C*N, opus_int16);
2689 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2690 if (ret>0)
2691 for (j=0;j<C*ret;j++)
2692 pcm[j]=out[j]*(1.f/32768.f);
2693
2694 RESTORE_STACK;
2695 return ret;
2696}
2697#endif /* DISABLE_FLOAT_API */
2698
2699#else
2700
2701int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
2702{
2703 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2704}
2705
2706int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
2707{
2708 int j, ret, C, N;
2709 VARDECL(celt_sig, out);
2710 ALLOC_STACK;
2711
2712 if (pcm==NULL)
2713 return OPUS_BAD_ARG;
2714
2715 C = st->channels;
2716 N = frame_size;
2717 ALLOC(out, C*N, celt_sig);
2718
2719 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2720
2721 if (ret>0)
2722 for (j=0;j<C*ret;j++)
2723 pcm[j] = FLOAT2INT16 (out[j]);
2724
2725 RESTORE_STACK;
2726 return ret;
2727}
2728
2729#endif
2730#endif /* CUSTOM_MODES */
2731
2732int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
2733{
2734 va_list ap;
2735
2736 va_start(ap, request);
2737 switch (request)
2738 {
2739 case CELT_SET_START_BAND_REQUEST:
2740 {
2741 opus_int32 value = va_arg(ap, opus_int32);
2742 if (value<0 || value>=st->mode->nbEBands)
2743 goto bad_arg;
2744 st->start = value;
2745 }
2746 break;
2747 case CELT_SET_END_BAND_REQUEST:
2748 {
2749 opus_int32 value = va_arg(ap, opus_int32);
2750 if (value<1 || value>st->mode->nbEBands)
2751 goto bad_arg;
2752 st->end = value;
2753 }
2754 break;
2755 case CELT_SET_CHANNELS_REQUEST:
2756 {
2757 opus_int32 value = va_arg(ap, opus_int32);
2758 if (value<1 || value>2)
2759 goto bad_arg;
2760 st->stream_channels = value;
2761 }
2762 break;
2763 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
2764 {
2765 opus_int32 *value = va_arg(ap, opus_int32*);
2766 if (value==NULL)
2767 goto bad_arg;
2768 *value=st->error;
2769 st->error = 0;
2770 }
2771 break;
2772 case OPUS_GET_LOOKAHEAD_REQUEST:
2773 {
2774 opus_int32 *value = va_arg(ap, opus_int32*);
2775 if (value==NULL)
2776 goto bad_arg;
2777 *value = st->overlap/st->downsample;
2778 }
2779 break;
2780 case OPUS_RESET_STATE:
2781 {
2782 int i;
2783 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
2784 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
2785 oldBandE = lpc+st->channels*LPC_ORDER;
2786 oldLogE = oldBandE + 2*st->mode->nbEBands;
2787 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2788 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
2789 opus_custom_decoder_get_size(st->mode, st->channels)-
2790 ((char*)&st->DECODER_RESET_START - (char*)st));
2791 for (i=0;i<2*st->mode->nbEBands;i++)
2792 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2793 }
2794 break;
2795 case OPUS_GET_PITCH_REQUEST:
2796 {
2797 opus_int32 *value = va_arg(ap, opus_int32*);
2798 if (value==NULL)
2799 goto bad_arg;
2800 *value = st->postfilter_period;
2801 }
2802 break;
2803#ifdef OPUS_BUILD
2804 case CELT_GET_MODE_REQUEST:
2805 {
2806 const CELTMode ** value = va_arg(ap, const CELTMode**);
2807 if (value==0)
2808 goto bad_arg;
2809 *value=st->mode;
2810 }
2811 break;
2812 case CELT_SET_SIGNALLING_REQUEST:
2813 {
2814 opus_int32 value = va_arg(ap, opus_int32);
2815 st->signalling = value;
2816 }
2817 break;
2818 case OPUS_GET_FINAL_RANGE_REQUEST:
2819 {
2820 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2821 if (value==0)
2822 goto bad_arg;
2823 *value=st->rng;
2824 }
2825 break;
2826#endif
2827 default:
2828 goto bad_request;
2829 }
2830 va_end(ap);
2831 return OPUS_OK;
2832bad_arg:
2833 va_end(ap);
2834 return OPUS_BAD_ARG;
2835bad_request:
2836 va_end(ap);
2837 return OPUS_UNIMPLEMENTED;
2838}
2839
2840
2841
2842const char *opus_strerror(int error)
2843{
2844 static const char * const error_strings[8] = {
2845 "success",
2846 "invalid argument",
2847 "buffer too small",
2848 "internal error",
2849 "corrupted stream",
2850 "request not implemented",
2851 "invalid state",
2852 "memory allocation failed"
2853 };
2854 if (error > 0 || error < -7)
2855 return "unknown error";
2856 else
2857 return error_strings[-error];
2858}
2859
2860const char *opus_get_version_string(void)
2861{
2862 return "libopus " OPUS_VERSION
2863#ifdef FIXED_POINT
2864 "-fixed"
2865#endif
2866#ifdef FUZZING
2867 "-fuzzing"
2868#endif
2869 ;
2870}