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 */
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 - Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
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.
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.
36 #include "os_support.h"
44 #include "quant_bands.h"
46 #include "stack_alloc.h"
48 #include "float_cast.h"
54 #define OPUS_VERSION "unknown"
58 #define OPUS_CUSTOM_NOSTATIC
60 #define OPUS_CUSTOM_NOSTATIC static inline
63 static 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% */
65 static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
67 static const unsigned char tapset_icdf[3]={2,1,0};
70 static 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,
78 static 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
85 static inline int toOpus(unsigned char c)
89 ret = toOpusTable[c>>3];
96 static inline int fromOpus(unsigned char c)
101 return fromOpusTable[(c>>3)-16] | (c&0x7);
103 #endif /* CUSTOM_MODES */
105 #define COMBFILTER_MAXPERIOD 1024
106 #define COMBFILTER_MINPERIOD 15
108 static int resampling_factor(opus_int32 rate)
141 struct OpusCustomEncoder {
142 const OpusCustomMode *mode; /**< Mode used by the encoder */
157 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
160 /* Everything beyond this point gets cleared on a reset */
161 #define ENCODER_RESET_START rng
165 opus_val32 delayedIntra;
171 int prefilter_period;
172 opus_val16 prefilter_gain;
173 int prefilter_tapset;
175 int prefilter_period_old;
176 opus_val16 prefilter_gain_old;
177 int prefilter_tapset_old;
179 int consec_transient;
181 opus_val32 preemph_memE[2];
182 opus_val32 preemph_memD[2];
184 /* VBR-related parameters */
185 opus_int32 vbr_reservoir;
186 opus_int32 vbr_drift;
187 opus_int32 vbr_offset;
188 opus_int32 vbr_count;
191 celt_sig syn_mem[2][2*MAX_PERIOD];
194 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
195 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
196 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
197 /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
200 int celt_encoder_get_size(int channels)
202 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
203 return opus_custom_encoder_get_size(mode, channels);
206 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
208 int size = sizeof(struct CELTEncoder)
209 + (2*channels*mode->overlap-1)*sizeof(celt_sig)
210 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
211 + 3*channels*mode->nbEBands*sizeof(opus_val16);
216 CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
219 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
220 /* init will handle the NULL case */
221 ret = opus_custom_encoder_init(st, mode, channels);
224 opus_custom_encoder_destroy(st);
231 #endif /* CUSTOM_MODES */
233 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
236 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
239 st->upsample = resampling_factor(sampling_rate);
243 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
245 if (channels < 0 || channels > 2)
248 if (st==NULL || mode==NULL)
249 return OPUS_ALLOC_FAIL;
251 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
254 st->overlap = mode->overlap;
255 st->stream_channels = st->channels = channels;
259 st->end = st->mode->effEBands;
262 st->constrained_vbr = 1;
265 st->bitrate = OPUS_BITRATE_MAX;
270 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
276 void opus_custom_encoder_destroy(CELTEncoder *st)
280 #endif /* CUSTOM_MODES */
282 static inline opus_val16 SIG2WORD16(celt_sig x)
285 x = PSHR32(x, SIG_SHIFT);
286 x = MAX32(x, -32768);
290 return (opus_val16)x;
294 static int transient_analysis(const opus_val32 * restrict in, int len, int C,
298 VARDECL(opus_val16, tmp);
299 opus_val32 mem0=0,mem1=0;
300 int is_transient = 0;
303 VARDECL(opus_val16, bins);
305 ALLOC(tmp, len, opus_val16);
309 ALLOC(bins, N, opus_val16);
313 tmp[i] = SHR32(in[i],SIG_SHIFT);
316 tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
319 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
326 mem0 = mem1 + y - SHL32(x,1);
327 mem1 = x - SHR32(y,1);
329 mem0 = mem1 + y - 2*x;
332 tmp[i] = EXTRACT16(SHR32(y,2));
334 /* First few samples are bad because we don't propagate the memory */
341 opus_val16 max_abs=0;
342 for (j=0;j<block;j++)
343 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
350 opus_val16 t1, t2, t3;
352 t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
353 t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
354 t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
379 is_transient = rand()&0x1;
384 /** Apply window and compute the MDCT for all sub-frames and
385 all channels in a frame */
386 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int C, int LM)
388 if (C==1 && !shortBlocks)
390 const int overlap = OVERLAP(mode);
391 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
393 const int overlap = OVERLAP(mode);
394 int N = mode->shortMdctSize<<LM;
399 N = mode->shortMdctSize;
405 /* Interleaving the sub-frames while doing the MDCTs */
406 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 /** Compute the IMDCT and apply window for all sub-frames and
413 all channels in a frame */
414 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
415 celt_sig * restrict out_mem[],
416 celt_sig * restrict overlap_mem[], int C, int LM)
419 const int N = mode->shortMdctSize<<LM;
420 const int overlap = OVERLAP(mode);
421 VARDECL(opus_val32, x);
424 ALLOC(x, N+overlap, opus_val32);
433 N2 = mode->shortMdctSize;
436 /* Prevents problems from the imdct doing the overlap-add */
437 OPUS_CLEAR(x, overlap);
441 /* IMDCT on the interleaved the sub-frames */
442 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
445 for (j=0;j<overlap;j++)
446 out_mem[c][j] = x[j] + overlap_mem[c][j];
448 out_mem[c][j] = x[j];
449 for (j=0;j<overlap;j++)
450 overlap_mem[c][j] = x[N+j];
455 static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
461 celt_sig * restrict x;
462 opus_val16 * restrict y;
468 celt_sig tmp = *x + m;
469 m = MULT16_32_Q15(coef[0], tmp)
470 - MULT16_32_Q15(coef[1], *x);
471 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
473 /* Technically the store could be moved outside of the if because
474 the stores we don't want will just be overwritten */
476 *y = SCALEOUT(SIG2WORD16(tmp));
477 if (++count==downsample)
487 static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
488 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
489 const opus_val16 *window, int overlap)
492 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
493 opus_val16 g00, g01, g02, g10, g11, g12;
494 static const opus_val16 gains[3][3] = {
495 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
496 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
497 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
498 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
499 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
500 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
501 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
502 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
503 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
504 for (i=0;i<overlap;i++)
507 f = MULT16_16_Q15(window[i],window[i]);
509 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
510 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
511 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
512 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
513 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
514 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
515 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
516 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
517 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
518 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
521 for (i=overlap;i<N;i++)
523 + MULT16_32_Q15(g10,x[i-T1])
524 + MULT16_32_Q15(g11,x[i-T1-1])
525 + MULT16_32_Q15(g11,x[i-T1+1])
526 + MULT16_32_Q15(g12,x[i-T1-2])
527 + MULT16_32_Q15(g12,x[i-T1+2]);
530 static const signed char tf_select_table[4][8] = {
531 {0, -1, 0, -1, 0,-1, 0,-1},
532 {0, -1, 0, -2, 1, 0, 1,-1},
533 {0, -2, 0, -3, 2, 0, 1,-1},
534 {0, -2, 0, -3, 3, 0, 1,-1},
537 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width)
540 static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)};
544 for (i=0;i<1<<LM;i++)
547 for (j=0;j<N>>LM;j++)
548 L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]);
551 L1 = MULT16_32_Q15(sqrtM_1[LM], L1);
553 bias = QCONST16(.12f,15)*LM;
555 bias = QCONST16(.05f,15)*LM;
557 bias = QCONST16(.02f,15)*LM;
558 L1 = MAC16_32_Q15(L1, bias, L1);
562 static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
563 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
567 VARDECL(int, metric);
572 VARDECL(celt_norm, tmp);
577 if (nbCompressedBytes<15*C)
581 tf_res[i] = isTransient;
584 if (nbCompressedBytes<40)
586 else if (nbCompressedBytes<60)
588 else if (nbCompressedBytes<100)
593 ALLOC(metric, len, int);
594 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
595 ALLOC(path0, len, int);
596 ALLOC(path1, len, int);
602 opus_val32 L1, best_L1;
604 N = (m->eBands[i+1]-m->eBands[i])<<LM;
606 tmp[j] = X[j+(m->eBands[i]<<LM)];
607 /* Just add the right channel if we're in stereo */
610 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));
611 L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM);
613 /*printf ("%f ", L1);*/
624 haar1(tmp, N>>(LM-k), 1<<(LM-k));
626 haar1(tmp, N>>k, 1<<k);
628 L1 = l1_metric(tmp, N, B, N>>LM);
636 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
638 metric[i] = best_level;
640 metric[i] = -best_level;
641 *tf_sum += metric[i];
644 /* NOTE: Future optimized implementations could detect extreme transients and set
645 tf_select = 1 but so far we have not found a reliable way of making this useful */
649 cost1 = isTransient ? 0 : lambda;
650 /* Viterbi forward pass */
657 from1 = cost1 + lambda;
667 from0 = cost0 + lambda;
677 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
678 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
680 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
681 /* Viterbi backward pass to check the decisions */
682 for (i=len-2;i>=0;i--)
684 if (tf_res[i+1] == 1)
685 tf_res[i] = path1[i+1];
687 tf_res[i] = path0[i+1];
691 tf_select = rand()&0x1;
692 tf_res[0] = rand()&0x1;
694 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
699 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
707 budget = enc->storage*8;
709 logp = isTransient ? 2 : 4;
710 /* Reserve space to code the tf_select decision. */
711 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
712 budget -= tf_select_rsv;
713 curr = tf_changed = 0;
714 for (i=start;i<end;i++)
716 if (tell+logp<=budget)
718 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
725 logp = isTransient ? 4 : 5;
727 /* Only code tf_select if it would actually make a difference. */
729 tf_select_table[LM][4*isTransient+0+tf_changed]!=
730 tf_select_table[LM][4*isTransient+2+tf_changed])
731 ec_enc_bit_logp(enc, tf_select, 1);
734 for (i=start;i<end;i++)
735 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
736 /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
739 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
741 int i, curr, tf_select;
748 budget = dec->storage*8;
750 logp = isTransient ? 2 : 4;
751 tf_select_rsv = LM>0 && tell+logp+1<=budget;
752 budget -= tf_select_rsv;
753 tf_changed = curr = 0;
754 for (i=start;i<end;i++)
756 if (tell+logp<=budget)
758 curr ^= ec_dec_bit_logp(dec, logp);
763 logp = isTransient ? 4 : 5;
767 tf_select_table[LM][4*isTransient+0+tf_changed] !=
768 tf_select_table[LM][4*isTransient+2+tf_changed])
770 tf_select = ec_dec_bit_logp(dec, 1);
772 for (i=start;i<end;i++)
774 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
778 static void init_caps(const CELTMode *m,int *cap,int LM,int C)
781 for (i=0;i<m->nbEBands;i++)
784 N=(m->eBands[i+1]-m->eBands[i])<<LM;
785 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
789 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
790 const opus_val16 *bandLogE, int end, int LM, int C, int N0)
798 opus_val16 sum = 0; /* Q10 */
799 /* Compute inter-channel correlation for low frequencies */
803 opus_val32 partial = 0;
804 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
805 partial = MAC16_16(partial, X[j], X[N0+j]);
806 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
808 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
809 /*printf ("%f\n", sum);*/
810 if (sum > QCONST16(.995f,10))
812 else if (sum > QCONST16(.92f,10))
814 else if (sum > QCONST16(.85f,10))
816 else if (sum > QCONST16(.8f,10))
820 /* Estimate spectral tilt */
822 for (i=0;i<end-1;i++)
824 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
827 /* We divide by two here to avoid making the tilt larger for stereo as a
828 result of a bug in the loop above */
830 /*printf("%f\n", diff);*/
831 if (diff > QCONST16(2.f, DB_SHIFT))
833 if (diff > QCONST16(8.f, DB_SHIFT))
835 if (diff < -QCONST16(4.f, DB_SHIFT))
837 if (diff < -QCONST16(10.f, DB_SHIFT))
845 trim_index = rand()%11;
850 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
855 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
857 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
861 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
863 opus_val32 L, R, M, S;
864 /* We cast to 32-bit first because of the -32768 case */
866 R = EXTEND32(X[N0+j]);
869 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
870 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
873 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
875 /* We don't need thetas for lower bands with LM<=1 */
878 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
879 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
882 int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
887 VARDECL(celt_sig, in);
888 VARDECL(celt_sig, freq);
889 VARDECL(celt_norm, X);
890 VARDECL(celt_ener, bandE);
891 VARDECL(opus_val16, bandLogE);
892 VARDECL(int, fine_quant);
893 VARDECL(opus_val16, error);
894 VARDECL(int, pulses);
896 VARDECL(int, offsets);
897 VARDECL(int, fine_priority);
898 VARDECL(int, tf_res);
899 VARDECL(unsigned char, collapse_masks);
900 celt_sig *prefilter_mem;
901 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
904 const int CC = st->channels;
905 const int C = st->stream_channels;
908 int nbFilledBytes, nbAvailableBytes;
913 int pitch_index=COMBFILTER_MINPERIOD;
914 opus_val16 gain1 = 0;
918 opus_val16 pf_threshold;
921 opus_int32 total_bits;
922 opus_int32 total_boost;
925 int prefilter_tapset=0;
927 int anti_collapse_rsv;
928 int anti_collapse_on=0;
932 if (nbCompressedBytes<2 || pcm==NULL)
935 frame_size *= st->upsample;
936 for (LM=0;LM<=st->mode->maxLM;LM++)
937 if (st->mode->shortMdctSize<<LM==frame_size)
939 if (LM>st->mode->maxLM)
942 N = M*st->mode->shortMdctSize;
944 prefilter_mem = st->in_mem+CC*(st->overlap);
945 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
946 oldLogE = oldBandE + CC*st->mode->nbEBands;
947 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
955 nbFilledBytes=(tell+4)>>3;
959 if (st->signalling && enc==NULL)
961 int tmp = (st->mode->effEBands-st->end)>>1;
962 st->end = IMAX(1, st->mode->effEBands-tmp);
963 compressed[0] = tmp<<5;
964 compressed[0] |= LM<<3;
965 compressed[0] |= (C==2)<<2;
966 /* Convert "standard mode" to Opus header */
967 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
969 int c0 = toOpus(compressed[0]);
978 celt_assert(st->signalling==0);
981 /* Can't produce more than 1275 output bytes */
982 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
983 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
985 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
987 opus_int32 den=st->mode->Fs>>BITRES;
988 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
991 vbr_rate -= 8<<BITRES;
993 effectiveBytes = vbr_rate>>(3+BITRES);
997 tmp = st->bitrate*frame_size;
1000 if (st->bitrate!=OPUS_BITRATE_MAX)
1001 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1002 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1003 effectiveBytes = nbCompressedBytes;
1008 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1014 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1015 target rate and buffering.
1016 We must do this up front so that bust-prevention logic triggers
1017 correctly if we don't have enough bits. */
1018 if (st->constrained_vbr)
1020 opus_int32 vbr_bound;
1021 opus_int32 max_allowed;
1022 /* We could use any multiple of vbr_rate as bound (depending on the
1024 This is clamped to ensure we use at least two bytes if the encoder
1025 was entirely empty, but to allow 0 in hybrid mode. */
1026 vbr_bound = vbr_rate;
1027 max_allowed = IMIN(IMAX(tell==1?2:0,
1028 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1030 if(max_allowed < nbAvailableBytes)
1032 nbCompressedBytes = nbFilledBytes+max_allowed;
1033 nbAvailableBytes = max_allowed;
1034 ec_enc_shrink(enc, nbCompressedBytes);
1038 total_bits = nbCompressedBytes*8;
1041 if (effEnd > st->mode->effEBands)
1042 effEnd = st->mode->effEBands;
1044 ALLOC(in, CC*(N+st->overlap), celt_sig);
1046 /* Find pitch period and gain */
1048 VARDECL(celt_sig, _pre);
1051 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1054 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1059 const opus_val16 * restrict pcmp = pcm+c;
1060 celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
1071 x = MAX32(-65536.f, MIN32(65536.f,x));
1073 if (++count==st->upsample)
1080 /* Apply pre-emphasis */
1081 tmp = MULT16_16(st->mode->preemph[2], x);
1082 *inp = tmp + st->preemph_memE[c];
1083 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1084 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1085 silence = silence && *inp == 0;
1088 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1089 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1093 if ((rand()&0x3F)==0)
1097 ec_enc_bit_logp(enc, silence, 15);
1102 /*In VBR mode there is no need to send more than the minimum. */
1105 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1106 total_bits=nbCompressedBytes*8;
1108 ec_enc_shrink(enc, nbCompressedBytes);
1110 /* Pretend we've filled all the remaining bits with zeros
1111 (that's what the initialiser did anyway) */
1112 tell = nbCompressedBytes*8;
1113 enc->nbits_total+=tell-ec_tell(enc);
1115 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1117 VARDECL(opus_val16, pitch_buf);
1118 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1120 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1121 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1122 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1123 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1125 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1126 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1127 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1128 pitch_index = COMBFILTER_MAXPERIOD-2;
1129 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1130 if (st->loss_rate>2)
1131 gain1 = HALF32(gain1);
1132 if (st->loss_rate>4)
1133 gain1 = HALF32(gain1);
1134 if (st->loss_rate>8)
1136 prefilter_tapset = st->tapset_decision;
1141 /* Gain threshold for enabling the prefilter/postfilter */
1142 pf_threshold = QCONST16(.2f,15);
1144 /* Adjusting the threshold based on rate and continuity */
1145 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1146 pf_threshold += QCONST16(.2f,15);
1147 if (nbAvailableBytes<25)
1148 pf_threshold += QCONST16(.1f,15);
1149 if (nbAvailableBytes<35)
1150 pf_threshold += QCONST16(.1f,15);
1151 if (st->prefilter_gain > QCONST16(.4f,15))
1152 pf_threshold -= QCONST16(.1f,15);
1153 if (st->prefilter_gain > QCONST16(.55f,15))
1154 pf_threshold -= QCONST16(.1f,15);
1156 /* Hard threshold at 0.2 */
1157 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1158 if (gain1<pf_threshold)
1160 if(st->start==0 && tell+16<=total_bits)
1161 ec_enc_bit_logp(enc, 0, 1);
1165 /*This block is not gated by a total bits check only because
1166 of the nbAvailableBytes check above.*/
1170 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1171 gain1=st->prefilter_gain;
1174 qg = ((gain1+1536)>>10)/3-1;
1176 qg = (int)floor(.5f+gain1*32/3)-1;
1178 qg = IMAX(0, IMIN(7, qg));
1179 ec_enc_bit_logp(enc, 1, 1);
1181 octave = EC_ILOG(pitch_index)-5;
1182 ec_enc_uint(enc, octave, 6);
1183 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1185 ec_enc_bits(enc, qg, 3);
1186 if (ec_tell(enc)+2<=total_bits)
1187 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1189 prefilter_tapset = 0;
1190 gain1 = QCONST16(0.09375f,15)*(qg+1);
1193 /*printf("%d %f\n", pitch_index, gain1);*/
1196 int offset = st->mode->shortMdctSize-st->mode->overlap;
1197 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1198 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1200 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1201 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1202 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1204 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1205 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1206 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1207 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1209 if (N>COMBFILTER_MAXPERIOD)
1211 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1213 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1214 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1223 if (LM>0 && ec_tell(enc)+3<=total_bits)
1225 if (st->complexity > 1)
1227 isTransient = transient_analysis(in, N+st->overlap, CC,
1232 ec_enc_bit_logp(enc, isTransient, 3);
1235 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1236 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1237 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1239 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1244 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1246 if (st->upsample != 1)
1250 int bound = N/st->upsample;
1251 for (i=0;i<bound;i++)
1252 freq[c*N+i] *= st->upsample;
1257 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1259 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1261 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1263 /* Band normalisation */
1264 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1266 ALLOC(tf_res, st->mode->nbEBands, int);
1267 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1268 for (i=effEnd;i<st->end;i++)
1269 tf_res[i] = tf_res[effEnd-1];
1271 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1272 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1273 oldBandE, total_bits, error, enc,
1274 C, LM, nbAvailableBytes, st->force_intra,
1275 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1277 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1279 st->spread_decision = SPREAD_NORMAL;
1280 if (ec_tell(enc)+4<=total_bits)
1282 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1284 if (st->complexity == 0)
1285 st->spread_decision = SPREAD_NONE;
1287 st->spread_decision = spreading_decision(st->mode, X,
1288 &st->tonal_average, st->spread_decision, &st->hf_average,
1289 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1291 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1294 ALLOC(cap, st->mode->nbEBands, int);
1295 ALLOC(offsets, st->mode->nbEBands, int);
1297 init_caps(st->mode,cap,LM,C);
1298 for (i=0;i<st->mode->nbEBands;i++)
1300 /* Dynamic allocation code */
1301 /* Make sure that dynamic allocation can't make us bust the budget */
1302 if (effectiveBytes > 50 && LM>=1)
1313 for (i=st->start+1;i<st->end-1;i++)
1316 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1318 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1319 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1325 offsets[i] += 1+(rand()&0x3);
1328 if (d2 > SHL16(t1,DB_SHIFT))
1330 if (d2 > SHL16(t2,DB_SHIFT))
1336 total_bits<<=BITRES;
1338 tell = ec_tell_frac(enc);
1339 for (i=st->start;i<st->end;i++)
1342 int dynalloc_loop_logp;
1345 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1346 /* quanta is 6 bits, but no more than 1 bit/sample
1347 and no less than 1/8 bit/sample */
1348 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1349 dynalloc_loop_logp = dynalloc_logp;
1351 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1352 && boost < cap[i]; j++)
1355 flag = j<offsets[i];
1356 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1357 tell = ec_tell_frac(enc);
1361 total_boost += quanta;
1362 dynalloc_loop_logp = 1;
1364 /* Making dynalloc more likely */
1366 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1370 if (tell+(6<<BITRES) <= total_bits - total_boost)
1372 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1374 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1375 tell = ec_tell_frac(enc);
1378 /* Variable bitrate */
1383 /* The target rate in 8th bits per frame */
1385 opus_int32 min_allowed;
1386 int lm_diff = st->mode->maxLM - LM;
1388 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1389 The CELT allocator will just not be able to use more than that anyway. */
1390 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1391 target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES);
1393 /* Shortblocks get a large boost in bitrate, but since they
1394 are uncommon long blocks are not greatly affected */
1395 if (shortBlocks || tf_sum < -2*(st->end-st->start))
1396 target = 7*target/4;
1397 else if (tf_sum < -(st->end-st->start))
1398 target = 3*target/2;
1400 target-=(target+14)/28;
1402 /* The current offset is removed from the target and the space used
1406 /* In VBR mode the frame size must not be reduced so much that it would
1407 result in the encoder running out of bits.
1408 The margin of 2 bytes ensures that none of the bust-prevention logic
1409 in the decoder will have triggered so far. */
1410 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1412 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1413 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1414 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1416 /* By how much did we "miss" the target on that frame */
1417 delta = target - vbr_rate;
1419 target=nbAvailableBytes<<(BITRES+3);
1421 /*If the frame is silent we don't adjust our drift, otherwise
1422 the encoder will shoot to very high rates after hitting a
1423 span of silence, but we do allow the bitres to refill.
1424 This means that we'll undershoot our target in CVBR/VBR modes
1425 on files with lots of silence. */
1428 nbAvailableBytes = 2;
1429 target = 2*8<<BITRES;
1433 if (st->vbr_count < 970)
1436 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1438 alpha = QCONST16(.001f,15);
1439 /* How many bits have we used in excess of what we're allowed */
1440 if (st->constrained_vbr)
1441 st->vbr_reservoir += target - vbr_rate;
1442 /*printf ("%d\n", st->vbr_reservoir);*/
1444 /* Compute the offset we need to apply in order to reach the target */
1445 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1446 st->vbr_offset = -st->vbr_drift;
1447 /*printf ("%d\n", st->vbr_drift);*/
1449 if (st->constrained_vbr && st->vbr_reservoir < 0)
1451 /* We're under the min value -- increase rate */
1452 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1453 /* Unless we're just coding silence */
1454 nbAvailableBytes += silence?0:adjust;
1455 st->vbr_reservoir = 0;
1456 /*printf ("+%d\n", adjust);*/
1458 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1459 /* This moves the raw bits to take into account the new compressed size */
1460 ec_enc_shrink(enc, nbCompressedBytes);
1466 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1468 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1470 /* Account for coarse energy */
1471 effectiveRate = (8*effectiveBytes - 80)>>LM;
1473 /* effectiveRate in kb/s */
1474 effectiveRate = 2*effectiveRate/5;
1475 if (effectiveRate<35)
1477 else if (effectiveRate<50)
1479 else if (effectiveRate<68)
1481 else if (effectiveRate<84)
1483 else if (effectiveRate<102)
1485 else if (effectiveRate<130)
1489 intensity = IMIN(st->end,IMAX(st->start, intensity));
1492 /* Bit allocation */
1493 ALLOC(fine_quant, st->mode->nbEBands, int);
1494 ALLOC(pulses, st->mode->nbEBands, int);
1495 ALLOC(fine_priority, st->mode->nbEBands, int);
1497 /* bits = packet size - where we are - safety*/
1498 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1499 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1500 bits -= anti_collapse_rsv;
1501 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1502 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1503 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1504 st->lastCodedBands = codedBands;
1506 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1508 #ifdef MEASURE_NORM_MSE
1513 X0[i+c*N] = X[i+c*N];
1515 for (i=0;i<C*st->mode->nbEBands;i++)
1516 bandE0[i] = bandE[i];
1519 /* Residual quantisation */
1520 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1521 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1522 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1523 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1525 if (anti_collapse_rsv > 0)
1527 anti_collapse_on = st->consec_transient<2;
1529 anti_collapse_on = rand()&0x1;
1531 ec_enc_bits(enc, anti_collapse_on, 1);
1533 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1537 for (i=0;i<C*st->mode->nbEBands;i++)
1538 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1542 /* Re-synthesis of the coded audio if required */
1544 celt_sig *out_mem[2];
1545 celt_sig *overlap_mem[2];
1547 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1550 for (i=0;i<C*st->mode->nbEBands;i++)
1554 #ifdef MEASURE_NORM_MSE
1555 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1557 if (anti_collapse_on)
1559 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1560 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1564 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1566 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1568 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1571 for (i=0;i<M*st->mode->eBands[st->start];i++)
1575 for (i=M*st->mode->eBands[st->end];i<N;i++)
1582 freq[N+i] = freq[i];
1585 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1587 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1589 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1591 overlap_mem[1] = overlap_mem[0] + st->overlap;
1593 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1596 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1597 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1598 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1599 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1600 st->mode->window, st->overlap);
1602 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1603 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1604 st->mode->window, st->mode->overlap);
1607 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1608 st->prefilter_period_old = st->prefilter_period;
1609 st->prefilter_gain_old = st->prefilter_gain;
1610 st->prefilter_tapset_old = st->prefilter_tapset;
1614 st->prefilter_period = pitch_index;
1615 st->prefilter_gain = gain1;
1616 st->prefilter_tapset = prefilter_tapset;
1620 st->prefilter_period_old = st->prefilter_period;
1621 st->prefilter_gain_old = st->prefilter_gain;
1622 st->prefilter_tapset_old = st->prefilter_tapset;
1627 for (i=0;i<st->mode->nbEBands;i++)
1628 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1633 for (i=0;i<CC*st->mode->nbEBands;i++)
1634 oldLogE2[i] = oldLogE[i];
1635 for (i=0;i<CC*st->mode->nbEBands;i++)
1636 oldLogE[i] = oldBandE[i];
1638 for (i=0;i<CC*st->mode->nbEBands;i++)
1639 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1641 /* In case start or end were to change */
1644 for (i=0;i<st->start;i++)
1646 oldBandE[c*st->mode->nbEBands+i]=0;
1647 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1649 for (i=st->end;i<st->mode->nbEBands;i++)
1651 oldBandE[c*st->mode->nbEBands+i]=0;
1652 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1657 st->consec_transient++;
1659 st->consec_transient=0;
1662 /* If there's any room left (can only happen for very high rates),
1663 it's already filled with zeros */
1668 nbCompressedBytes++;
1672 if (ec_get_error(enc))
1673 return OPUS_INTERNAL_ERROR;
1675 return nbCompressedBytes;
1682 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1684 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1687 #ifndef DISABLE_FLOAT_API
1688 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1691 VARDECL(opus_int16, in);
1695 return OPUS_BAD_ARG;
1699 ALLOC(in, C*N, opus_int16);
1702 in[j] = FLOAT2INT16(pcm[j]);
1704 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1707 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1712 #endif /* DISABLE_FLOAT_API */
1715 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1718 VARDECL(celt_sig, in);
1722 return OPUS_BAD_ARG;
1726 ALLOC(in, C*N, celt_sig);
1727 for (j=0;j<C*N;j++) {
1728 in[j] = SCALEOUT(pcm[j]);
1731 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1734 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1740 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1742 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1747 #endif /* CUSTOM_MODES */
1749 int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1753 va_start(ap, request);
1756 case OPUS_SET_COMPLEXITY_REQUEST:
1758 int value = va_arg(ap, opus_int32);
1759 if (value<0 || value>10)
1761 st->complexity = value;
1764 case CELT_SET_START_BAND_REQUEST:
1766 opus_int32 value = va_arg(ap, opus_int32);
1767 if (value<0 || value>=st->mode->nbEBands)
1772 case CELT_SET_END_BAND_REQUEST:
1774 opus_int32 value = va_arg(ap, opus_int32);
1775 if (value<1 || value>st->mode->nbEBands)
1780 case CELT_SET_PREDICTION_REQUEST:
1782 int value = va_arg(ap, opus_int32);
1783 if (value<0 || value>2)
1785 st->disable_pf = value<=1;
1786 st->force_intra = value==0;
1789 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1791 int value = va_arg(ap, opus_int32);
1792 if (value<0 || value>100)
1794 st->loss_rate = value;
1797 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1799 opus_int32 value = va_arg(ap, opus_int32);
1800 st->constrained_vbr = value;
1803 case OPUS_SET_VBR_REQUEST:
1805 opus_int32 value = va_arg(ap, opus_int32);
1809 case OPUS_SET_BITRATE_REQUEST:
1811 opus_int32 value = va_arg(ap, opus_int32);
1812 if (value<=500 && value!=OPUS_BITRATE_MAX)
1814 value = IMIN(value, 260000*st->channels);
1815 st->bitrate = value;
1818 case CELT_SET_CHANNELS_REQUEST:
1820 opus_int32 value = va_arg(ap, opus_int32);
1821 if (value<1 || value>2)
1823 st->stream_channels = value;
1826 case OPUS_RESET_STATE:
1829 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1830 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
1831 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
1832 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
1833 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
1834 opus_custom_encoder_get_size(st->mode, st->channels)-
1835 ((char*)&st->ENCODER_RESET_START - (char*)st));
1836 for (i=0;i<st->channels*st->mode->nbEBands;i++)
1837 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1839 st->delayedIntra = 1;
1840 st->spread_decision = SPREAD_NORMAL;
1841 st->tonal_average = 256;
1843 st->tapset_decision = 0;
1847 case CELT_SET_INPUT_CLIPPING_REQUEST:
1849 opus_int32 value = va_arg(ap, opus_int32);
1854 case CELT_SET_SIGNALLING_REQUEST:
1856 opus_int32 value = va_arg(ap, opus_int32);
1857 st->signalling = value;
1860 case CELT_GET_MODE_REQUEST:
1862 const CELTMode ** value = va_arg(ap, const CELTMode**);
1868 case OPUS_GET_FINAL_RANGE_REQUEST:
1870 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1883 return OPUS_BAD_ARG;
1886 return OPUS_UNIMPLEMENTED;
1889 /**********************************************************************/
1893 /**********************************************************************/
1894 #define DECODE_BUFFER_SIZE 2048
1897 @brief Decoder state
1899 struct OpusCustomDecoder {
1900 const OpusCustomMode *mode;
1903 int stream_channels;
1909 /* Everything beyond this point gets cleared on a reset */
1910 #define DECODER_RESET_START rng
1914 int last_pitch_index;
1916 int postfilter_period;
1917 int postfilter_period_old;
1918 opus_val16 postfilter_gain;
1919 opus_val16 postfilter_gain_old;
1920 int postfilter_tapset;
1921 int postfilter_tapset_old;
1923 celt_sig preemph_memD[2];
1925 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
1926 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
1927 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
1928 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
1929 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
1930 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
1933 int celt_decoder_get_size(int channels)
1935 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
1936 return opus_custom_decoder_get_size(mode, channels);
1939 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
1941 int size = sizeof(struct CELTDecoder)
1942 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
1943 + channels*LPC_ORDER*sizeof(opus_val16)
1944 + 4*2*mode->nbEBands*sizeof(opus_val16);
1949 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
1952 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
1953 ret = opus_custom_decoder_init(st, mode, channels);
1956 opus_custom_decoder_destroy(st);
1963 #endif /* CUSTOM_MODES */
1965 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
1968 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
1971 st->downsample = resampling_factor(sampling_rate);
1972 if (st->downsample==0)
1973 return OPUS_BAD_ARG;
1978 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
1980 if (channels < 0 || channels > 2)
1981 return OPUS_BAD_ARG;
1984 return OPUS_ALLOC_FAIL;
1986 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
1989 st->overlap = mode->overlap;
1990 st->stream_channels = st->channels = channels;
1994 st->end = st->mode->effEBands;
1999 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2005 void opus_custom_decoder_destroy(CELTDecoder *st)
2009 #endif /* CUSTOM_MODES */
2011 static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)
2015 int overlap = st->mode->overlap;
2016 opus_val16 fade = Q15ONE;
2018 const int C = st->channels;
2020 celt_sig *out_mem[2];
2021 celt_sig *decode_mem[2];
2022 celt_sig *overlap_mem[2];
2024 opus_val32 *out_syn[2];
2025 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2029 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2030 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2031 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2033 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2034 oldBandE = lpc+C*LPC_ORDER;
2035 oldLogE = oldBandE + 2*st->mode->nbEBands;
2036 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2037 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2039 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2041 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2043 len = N+st->mode->overlap;
2045 if (st->loss_count >= 5 || st->start!=0)
2047 /* Noise-based PLC/CNG */
2048 VARDECL(celt_sig, freq);
2049 VARDECL(celt_norm, X);
2050 VARDECL(celt_ener, bandE);
2055 if (effEnd > st->mode->effEBands)
2056 effEnd = st->mode->effEBands;
2058 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2059 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2060 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2062 if (st->loss_count >= 5)
2063 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2066 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2069 for (i=st->start;i<st->end;i++)
2070 oldBandE[c*st->mode->nbEBands+i] -= decay;
2072 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2077 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2079 for (i=st->start;i<st->mode->effEBands;i++)
2084 boffs = N*c+(st->mode->eBands[i]<<LM);
2085 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2086 for (j=0;j<blen;j++)
2088 seed = celt_lcg_rand(seed);
2089 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2091 renormalise_vector(X+boffs, blen, Q15ONE);
2093 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2098 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2101 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2105 int bound = st->mode->eBands[effEnd]<<LM;
2106 if (st->downsample!=1)
2107 bound = IMIN(bound, N/st->downsample);
2108 for (i=bound;i<N;i++)
2111 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2113 /* Pitch-based PLC */
2114 if (st->loss_count == 0)
2116 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2117 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2118 search by using only part of the decode buffer */
2120 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2121 /* Max pitch is 100 samples (480 Hz) */
2122 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2123 poffset-100, &pitch_index);
2124 pitch_index = poffset-pitch_index;
2125 st->last_pitch_index = pitch_index;
2127 pitch_index = st->last_pitch_index;
2128 fade = QCONST16(.8f,15);
2132 VARDECL(opus_val32, e);
2133 opus_val16 exc[MAX_PERIOD];
2134 opus_val32 ac[LPC_ORDER+1];
2135 opus_val16 decay = 1;
2137 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};
2139 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2141 offset = MAX_PERIOD-pitch_index;
2142 for (i=0;i<MAX_PERIOD;i++)
2143 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2145 if (st->loss_count == 0)
2147 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2148 LPC_ORDER, MAX_PERIOD);
2150 /* Noise floor -40 dB */
2152 ac[0] += SHR32(ac[0],13);
2157 for (i=1;i<=LPC_ORDER;i++)
2159 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2161 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2163 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2167 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2169 for (i=0;i<LPC_ORDER;i++)
2170 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2171 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2172 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2173 /* Check if the waveform is decaying (and if so how fast) */
2175 opus_val32 E1=1, E2=1;
2177 if (pitch_index <= MAX_PERIOD/2)
2178 period = pitch_index;
2180 period = MAX_PERIOD/2;
2181 for (i=0;i<period;i++)
2183 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2184 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2188 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2191 /* Copy excitation, taking decay into account */
2192 for (i=0;i<len+st->mode->overlap;i++)
2195 if (offset+i >= MAX_PERIOD)
2197 offset -= pitch_index;
2198 decay = MULT16_16_Q15(decay, decay);
2200 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2201 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2202 S1 += SHR32(MULT16_16(tmp,tmp),8);
2204 for (i=0;i<LPC_ORDER;i++)
2205 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2206 for (i=0;i<len+st->mode->overlap;i++)
2207 e[i] = MULT16_32_Q15(fade, e[i]);
2208 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2212 for (i=0;i<len+overlap;i++)
2214 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2215 S2 += SHR32(MULT16_16(tmp,tmp),8);
2217 /* This checks for an "explosion" in the synthesis */
2219 if (!(S1 > SHR32(S2,2)))
2221 /* Float test is written this way to catch NaNs at the same time */
2222 if (!(S1 > 0.2f*S2))
2225 for (i=0;i<len+overlap;i++)
2229 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2230 for (i=0;i<len+overlap;i++)
2231 e[i] = MULT16_32_Q15(ratio, e[i]);
2235 /* Apply post-filter to the MDCT overlap of the previous frame */
2236 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2237 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2240 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2241 out_mem[c][i] = out_mem[c][N+i];
2243 /* Apply TDAC to the concealed audio so that it blends with the
2244 previous and next frames */
2245 for (i=0;i<overlap/2;i++)
2248 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2249 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2250 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2251 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2254 out_mem[c][MAX_PERIOD-N+i] = e[i];
2256 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2257 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2258 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2260 for (i=0;i<overlap;i++)
2261 out_mem[c][MAX_PERIOD+i] = e[i];
2265 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2272 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec)
2275 int spread_decision;
2278 VARDECL(celt_sig, freq);
2279 VARDECL(celt_norm, X);
2280 VARDECL(celt_ener, bandE);
2281 VARDECL(int, fine_quant);
2282 VARDECL(int, pulses);
2284 VARDECL(int, offsets);
2285 VARDECL(int, fine_priority);
2286 VARDECL(int, tf_res);
2287 VARDECL(unsigned char, collapse_masks);
2288 celt_sig *out_mem[2];
2289 celt_sig *decode_mem[2];
2290 celt_sig *overlap_mem[2];
2291 celt_sig *out_syn[2];
2293 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2298 const int CC = st->channels;
2303 int postfilter_pitch;
2304 opus_val16 postfilter_gain;
2307 opus_int32 total_bits;
2311 int postfilter_tapset;
2312 int anti_collapse_rsv;
2313 int anti_collapse_on=0;
2315 int C = st->stream_channels;
2318 frame_size *= st->downsample;
2321 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2322 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2323 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2325 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2326 oldBandE = lpc+CC*LPC_ORDER;
2327 oldLogE = oldBandE + 2*st->mode->nbEBands;
2328 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2329 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2332 if (st->signalling && data!=NULL)
2335 /* Convert "standard mode" to Opus header */
2336 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2338 data0 = fromOpus(data0);
2340 return OPUS_INVALID_PACKET;
2342 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2343 LM = (data0>>3)&0x3;
2344 C = 1 + ((data0>>2)&0x1);
2347 if (LM>st->mode->maxLM)
2348 return OPUS_INVALID_PACKET;
2349 if (frame_size < st->mode->shortMdctSize<<LM)
2350 return OPUS_BUFFER_TOO_SMALL;
2352 frame_size = st->mode->shortMdctSize<<LM;
2357 for (LM=0;LM<=st->mode->maxLM;LM++)
2358 if (st->mode->shortMdctSize<<LM==frame_size)
2360 if (LM>st->mode->maxLM)
2361 return OPUS_BAD_ARG;
2365 if (len<0 || len>1275 || pcm==NULL)
2366 return OPUS_BAD_ARG;
2368 N = M*st->mode->shortMdctSize;
2371 if (effEnd > st->mode->effEBands)
2372 effEnd = st->mode->effEBands;
2374 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2375 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2376 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2378 for (i=0;i<M*st->mode->eBands[st->start];i++)
2382 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2386 if (data == NULL || len<=1)
2388 celt_decode_lost(st, pcm, N, LM);
2390 return frame_size/st->downsample;
2395 ec_dec_init(&_dec,(unsigned char*)data,len);
2401 for (i=0;i<st->mode->nbEBands;i++)
2402 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2406 tell = ec_tell(dec);
2408 if (tell >= total_bits)
2411 silence = ec_dec_bit_logp(dec, 15);
2416 /* Pretend we've read all the remaining bits */
2418 dec->nbits_total+=tell-ec_tell(dec);
2421 postfilter_gain = 0;
2422 postfilter_pitch = 0;
2423 postfilter_tapset = 0;
2424 if (st->start==0 && tell+16 <= total_bits)
2426 if(ec_dec_bit_logp(dec, 1))
2429 octave = ec_dec_uint(dec, 6);
2430 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2431 qg = ec_dec_bits(dec, 3);
2432 if (ec_tell(dec)+2<=total_bits)
2433 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2434 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2436 tell = ec_tell(dec);
2439 if (LM > 0 && tell+3 <= total_bits)
2441 isTransient = ec_dec_bit_logp(dec, 3);
2442 tell = ec_tell(dec);
2452 /* Decode the global flags (first symbols in the stream) */
2453 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2454 /* Get band energies */
2455 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2456 intra_ener, dec, C, LM);
2458 ALLOC(tf_res, st->mode->nbEBands, int);
2459 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2461 tell = ec_tell(dec);
2462 spread_decision = SPREAD_NORMAL;
2463 if (tell+4 <= total_bits)
2464 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2466 ALLOC(pulses, st->mode->nbEBands, int);
2467 ALLOC(cap, st->mode->nbEBands, int);
2468 ALLOC(offsets, st->mode->nbEBands, int);
2469 ALLOC(fine_priority, st->mode->nbEBands, int);
2471 init_caps(st->mode,cap,LM,C);
2474 total_bits<<=BITRES;
2475 tell = ec_tell_frac(dec);
2476 for (i=st->start;i<st->end;i++)
2479 int dynalloc_loop_logp;
2481 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2482 /* quanta is 6 bits, but no more than 1 bit/sample
2483 and no less than 1/8 bit/sample */
2484 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2485 dynalloc_loop_logp = dynalloc_logp;
2487 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2490 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2491 tell = ec_tell_frac(dec);
2495 total_bits -= quanta;
2496 dynalloc_loop_logp = 1;
2499 /* Making dynalloc more likely */
2501 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2504 ALLOC(fine_quant, st->mode->nbEBands, int);
2505 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2506 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2508 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2509 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2510 bits -= anti_collapse_rsv;
2511 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2512 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2513 fine_quant, fine_priority, C, LM, dec, 0, 0);
2515 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2517 /* Decode fixed codebook */
2518 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2519 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2520 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2521 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2523 if (anti_collapse_rsv > 0)
2525 anti_collapse_on = ec_dec_bits(dec, 1);
2528 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2529 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2531 if (anti_collapse_on)
2532 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2533 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2535 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2539 for (i=0;i<C*st->mode->nbEBands;i++)
2542 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2546 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2548 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2550 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2553 for (i=0;i<M*st->mode->eBands[st->start];i++)
2557 int bound = M*st->mode->eBands[effEnd];
2558 if (st->downsample!=1)
2559 bound = IMIN(bound, N/st->downsample);
2560 for (i=bound;i<N;i++)
2564 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2566 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2571 freq[N+i] = freq[i];
2576 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2579 /* Compute inverse MDCTs */
2580 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2583 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2584 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2585 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2586 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2587 st->mode->window, st->overlap);
2589 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2590 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2591 st->mode->window, st->mode->overlap);
2594 st->postfilter_period_old = st->postfilter_period;
2595 st->postfilter_gain_old = st->postfilter_gain;
2596 st->postfilter_tapset_old = st->postfilter_tapset;
2597 st->postfilter_period = postfilter_pitch;
2598 st->postfilter_gain = postfilter_gain;
2599 st->postfilter_tapset = postfilter_tapset;
2602 st->postfilter_period_old = st->postfilter_period;
2603 st->postfilter_gain_old = st->postfilter_gain;
2604 st->postfilter_tapset_old = st->postfilter_tapset;
2608 for (i=0;i<st->mode->nbEBands;i++)
2609 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2612 /* In case start or end were to change */
2615 for (i=0;i<2*st->mode->nbEBands;i++)
2616 oldLogE2[i] = oldLogE[i];
2617 for (i=0;i<2*st->mode->nbEBands;i++)
2618 oldLogE[i] = oldBandE[i];
2619 for (i=0;i<2*st->mode->nbEBands;i++)
2620 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2622 for (i=0;i<2*st->mode->nbEBands;i++)
2623 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2627 for (i=0;i<st->start;i++)
2629 oldBandE[c*st->mode->nbEBands+i]=0;
2630 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2632 for (i=st->end;i<st->mode->nbEBands;i++)
2634 oldBandE[c*st->mode->nbEBands+i]=0;
2635 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2640 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2643 if (ec_tell(dec) > 8*len)
2644 return OPUS_INTERNAL_ERROR;
2645 if(ec_get_error(dec))
2647 return frame_size/st->downsample;
2654 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2656 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2659 #ifndef DISABLE_FLOAT_API
2660 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2663 VARDECL(opus_int16, out);
2667 return OPUS_BAD_ARG;
2672 ALLOC(out, C*N, opus_int16);
2673 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2675 for (j=0;j<C*ret;j++)
2676 pcm[j]=out[j]*(1.f/32768.f);
2681 #endif /* DISABLE_FLOAT_API */
2685 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2687 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2690 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2693 VARDECL(celt_sig, out);
2697 return OPUS_BAD_ARG;
2701 ALLOC(out, C*N, celt_sig);
2703 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2706 for (j=0;j<C*ret;j++)
2707 pcm[j] = FLOAT2INT16 (out[j]);
2714 #endif /* CUSTOM_MODES */
2716 int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...)
2720 va_start(ap, request);
2723 case CELT_SET_START_BAND_REQUEST:
2725 opus_int32 value = va_arg(ap, opus_int32);
2726 if (value<0 || value>=st->mode->nbEBands)
2731 case CELT_SET_END_BAND_REQUEST:
2733 opus_int32 value = va_arg(ap, opus_int32);
2734 if (value<1 || value>st->mode->nbEBands)
2739 case CELT_SET_CHANNELS_REQUEST:
2741 opus_int32 value = va_arg(ap, opus_int32);
2742 if (value<1 || value>2)
2744 st->stream_channels = value;
2747 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
2749 opus_int32 *value = va_arg(ap, opus_int32*);
2756 case OPUS_GET_LOOKAHEAD_REQUEST:
2758 opus_int32 *value = va_arg(ap, opus_int32*);
2761 *value = st->overlap/st->downsample;
2764 case OPUS_RESET_STATE:
2767 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
2768 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
2769 oldBandE = lpc+st->channels*LPC_ORDER;
2770 oldLogE = oldBandE + 2*st->mode->nbEBands;
2771 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2772 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
2773 opus_custom_decoder_get_size(st->mode, st->channels)-
2774 ((char*)&st->DECODER_RESET_START - (char*)st));
2775 for (i=0;i<2*st->mode->nbEBands;i++)
2776 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2779 case OPUS_GET_PITCH_REQUEST:
2781 opus_int32 *value = va_arg(ap, opus_int32*);
2784 *value = st->postfilter_period;
2788 case CELT_GET_MODE_REQUEST:
2790 const CELTMode ** value = va_arg(ap, const CELTMode**);
2796 case CELT_SET_SIGNALLING_REQUEST:
2798 opus_int32 value = va_arg(ap, opus_int32);
2799 st->signalling = value;
2802 case OPUS_GET_FINAL_RANGE_REQUEST:
2804 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2818 return OPUS_BAD_ARG;
2821 return OPUS_UNIMPLEMENTED;
2826 const char *opus_strerror(int error)
2828 static const char *error_strings[8] = {
2834 "request not implemented",
2836 "memory allocation failed"
2838 if (error > 0 || error < -7)
2839 return "unknown error";
2841 return error_strings[-error];
2844 const char *opus_get_version_string(void)
2846 return "libopus " OPUS_VERSION