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