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;
183 opus_val32 preemph_memE[2];
184 opus_val32 preemph_memD[2];
186 /* VBR-related parameters */
187 opus_int32 vbr_reservoir;
188 opus_int32 vbr_drift;
189 opus_int32 vbr_offset;
190 opus_int32 vbr_count;
193 celt_sig syn_mem[2][2*MAX_PERIOD];
196 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
197 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
198 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
199 /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
202 int celt_encoder_get_size(int channels)
204 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
205 return opus_custom_encoder_get_size(mode, channels);
208 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
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);
218 CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
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);
226 opus_custom_encoder_destroy(st);
233 #endif /* CUSTOM_MODES */
235 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
238 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
241 st->upsample = resampling_factor(sampling_rate);
245 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
247 if (channels < 0 || channels > 2)
250 if (st==NULL || mode==NULL)
251 return OPUS_ALLOC_FAIL;
253 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
256 st->overlap = mode->overlap;
257 st->stream_channels = st->channels = channels;
261 st->end = st->mode->effEBands;
264 st->constrained_vbr = 1;
267 st->bitrate = OPUS_BITRATE_MAX;
272 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
278 void opus_custom_encoder_destroy(CELTEncoder *st)
282 #endif /* CUSTOM_MODES */
284 static inline opus_val16 SIG2WORD16(celt_sig x)
287 x = PSHR32(x, SIG_SHIFT);
288 x = MAX32(x, -32768);
292 return (opus_val16)x;
296 static int transient_analysis(const opus_val32 * restrict in, int len, int C,
297 int overlap, opus_val16 *tf_estimate)
300 VARDECL(opus_val16, tmp);
301 opus_val32 mem0=0,mem1=0;
302 int is_transient = 0;
306 opus_val32 L1, L2, tf_tmp;
307 VARDECL(opus_val16, bins);
309 ALLOC(tmp, len, opus_val16);
313 ALLOC(bins, N, opus_val16);
317 tmp[i] = SHR32(in[i],SIG_SHIFT);
320 tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
323 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
330 mem0 = mem1 + y - SHL32(x,1);
331 mem1 = x - SHR32(y,1);
333 mem0 = mem1 + y - 2*x;
336 tmp[i] = EXTRACT16(SHR32(y,2));
338 /* First few samples are bad because we don't propagate the memory */
346 opus_val16 max_abs=0;
347 for (j=0;j<block;j++)
348 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
350 maxbin = MAX16(maxbin, bins[i]);
358 opus_val16 t1, t2, t3;
361 tmp_bin = bins[i]+MULT16_16_Q15(QCONST16(.05f,15),maxbin);
362 L1 += EXTEND32(tmp_bin);
363 L2 += SHR32(MULT16_16(tmp_bin, tmp_bin), 4);
364 t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
365 t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
366 t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
390 tf_tmp = SHL32(DIV32( SHL32(EXTEND32(celt_sqrt(SHR16(L2,4) * N)), 14), ADD32(EPSILON, L1)), 4);
391 *tf_estimate = MAX16(QCONST16(1.f, 14), EXTRACT16(MIN16(QCONST32(1.99, 14), tf_tmp)));
394 is_transient = rand()&0x1;
399 /** Apply window and compute the MDCT for all sub-frames and
400 all channels in a frame */
401 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int C, int LM)
403 if (C==1 && !shortBlocks)
405 const int overlap = OVERLAP(mode);
406 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
408 const int overlap = OVERLAP(mode);
409 int N = mode->shortMdctSize<<LM;
414 N = mode->shortMdctSize;
420 /* Interleaving the sub-frames while doing the MDCTs */
421 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);
427 /** Compute the IMDCT and apply window for all sub-frames and
428 all channels in a frame */
429 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
430 celt_sig * restrict out_mem[],
431 celt_sig * restrict overlap_mem[], int C, int LM)
434 const int N = mode->shortMdctSize<<LM;
435 const int overlap = OVERLAP(mode);
436 VARDECL(opus_val32, x);
439 ALLOC(x, N+overlap, opus_val32);
448 N2 = mode->shortMdctSize;
451 /* Prevents problems from the imdct doing the overlap-add */
452 OPUS_CLEAR(x, overlap);
456 /* IMDCT on the interleaved the sub-frames */
457 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
460 for (j=0;j<overlap;j++)
461 out_mem[c][j] = x[j] + overlap_mem[c][j];
463 out_mem[c][j] = x[j];
464 for (j=0;j<overlap;j++)
465 overlap_mem[c][j] = x[N+j];
470 static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
476 celt_sig * restrict x;
477 opus_val16 * restrict y;
483 celt_sig tmp = *x + m;
484 m = MULT16_32_Q15(coef[0], tmp)
485 - MULT16_32_Q15(coef[1], *x);
486 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
488 /* Technically the store could be moved outside of the if because
489 the stores we don't want will just be overwritten */
491 *y = SCALEOUT(SIG2WORD16(tmp));
492 if (++count==downsample)
502 static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
503 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
504 const opus_val16 *window, int overlap)
507 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
508 opus_val16 g00, g01, g02, g10, g11, g12;
509 static const opus_val16 gains[3][3] = {
510 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
511 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
512 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
513 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
514 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
515 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
516 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
517 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
518 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
519 for (i=0;i<overlap;i++)
522 f = MULT16_16_Q15(window[i],window[i]);
524 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
525 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
526 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
527 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
528 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
529 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
530 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
531 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
532 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
533 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
536 for (i=overlap;i<N;i++)
538 + MULT16_32_Q15(g10,x[i-T1])
539 + MULT16_32_Q15(g11,x[i-T1-1])
540 + MULT16_32_Q15(g11,x[i-T1+1])
541 + MULT16_32_Q15(g12,x[i-T1-2])
542 + MULT16_32_Q15(g12,x[i-T1+2]);
545 static const signed char tf_select_table[4][8] = {
546 {0, -1, 0, -1, 0,-1, 0,-1},
547 {0, -1, 0, -2, 1, 0, 1,-1},
548 {0, -2, 0, -3, 2, 0, 1,-1},
549 {0, -2, 0, -3, 3, 0, 1,-1},
552 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM)
559 L1 += EXTEND32(ABS16(tmp[i]));
560 /* When in doubt, prefer goo freq resolution */
561 bias = QCONST16(.015f,15)*LM;
562 L1 = MAC16_32_Q15(L1, bias, L1);
567 static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
568 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
572 VARDECL(int, metric);
577 VARDECL(celt_norm, tmp);
582 if (nbCompressedBytes<15*C)
586 tf_res[i] = isTransient;
589 if (nbCompressedBytes<40)
591 else if (nbCompressedBytes<60)
593 else if (nbCompressedBytes<100)
598 ALLOC(metric, len, int);
599 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
600 ALLOC(path0, len, int);
601 ALLOC(path1, len, int);
607 opus_val32 L1, best_L1;
609 N = (m->eBands[i+1]-m->eBands[i])<<LM;
611 tmp[j] = X[j+(m->eBands[i]<<LM)];
612 /* Just add the right channel if we're in stereo */
615 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));
616 L1 = l1_metric(tmp, N, isTransient ? LM : 0);
618 /*printf ("%f ", L1);*/
629 haar1(tmp, N>>(LM-k), 1<<(LM-k));
631 haar1(tmp, N>>k, 1<<k);
633 L1 = l1_metric(tmp, N, B);
641 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
643 metric[i] = best_level;
645 metric[i] = -best_level;
646 *tf_sum += metric[i];
649 /* NOTE: Future optimized implementations could detect extreme transients and set
650 tf_select = 1 but so far we have not found a reliable way of making this useful */
654 cost1 = isTransient ? 0 : lambda;
655 /* Viterbi forward pass */
662 from1 = cost1 + lambda;
672 from0 = cost0 + lambda;
682 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
683 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
685 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
686 /* Viterbi backward pass to check the decisions */
687 for (i=len-2;i>=0;i--)
689 if (tf_res[i+1] == 1)
690 tf_res[i] = path1[i+1];
692 tf_res[i] = path0[i+1];
696 tf_select = rand()&0x1;
697 tf_res[0] = rand()&0x1;
699 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
704 extern int boost_band[2];
705 extern float boost_amount[2];
707 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
715 budget = enc->storage*8;
717 logp = isTransient ? 2 : 4;
718 /* Reserve space to code the tf_select decision. */
719 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
720 budget -= tf_select_rsv;
721 curr = tf_changed = 0;
722 for (i=start;i<end;i++)
724 if (tell+logp<=budget)
726 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
733 logp = isTransient ? 4 : 5;
735 /* Only code tf_select if it would actually make a difference. */
737 tf_select_table[LM][4*isTransient+0+tf_changed]!=
738 tf_select_table[LM][4*isTransient+2+tf_changed])
739 ec_enc_bit_logp(enc, tf_select, 1);
742 for (i=start;i<end;i++)
743 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
744 /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
747 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
749 int i, curr, tf_select;
756 budget = dec->storage*8;
758 logp = isTransient ? 2 : 4;
759 tf_select_rsv = LM>0 && tell+logp+1<=budget;
760 budget -= tf_select_rsv;
761 tf_changed = curr = 0;
762 for (i=start;i<end;i++)
764 if (tell+logp<=budget)
766 curr ^= ec_dec_bit_logp(dec, logp);
771 logp = isTransient ? 4 : 5;
775 tf_select_table[LM][4*isTransient+0+tf_changed] !=
776 tf_select_table[LM][4*isTransient+2+tf_changed])
778 tf_select = ec_dec_bit_logp(dec, 1);
780 for (i=start;i<end;i++)
782 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
786 static void init_caps(const CELTMode *m,int *cap,int LM,int C)
789 for (i=0;i<m->nbEBands;i++)
792 N=(m->eBands[i+1]-m->eBands[i])<<LM;
793 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
797 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
798 const opus_val16 *bandLogE, int end, int LM, int C, int N0, float tonality_slope)
806 opus_val16 sum = 0; /* Q10 */
807 /* Compute inter-channel correlation for low frequencies */
811 opus_val32 partial = 0;
812 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
813 partial = MAC16_16(partial, X[j], X[N0+j]);
814 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
816 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
817 /*printf ("%f\n", sum);*/
818 if (sum > QCONST16(.995f,10))
820 else if (sum > QCONST16(.92f,10))
822 else if (sum > QCONST16(.85f,10))
824 else if (sum > QCONST16(.8f,10))
828 /* Estimate spectral tilt */
830 for (i=0;i<end-1;i++)
832 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
835 /* We divide by two here to avoid making the tilt larger for stereo as a
836 result of a bug in the loop above */
838 /*printf("%f\n", diff);*/
840 if (diff > QCONST16(2.f, DB_SHIFT))
842 if (diff > QCONST16(8.f, DB_SHIFT))
844 if (diff < -QCONST16(4.f, DB_SHIFT))
846 if (diff < -QCONST16(10.f, DB_SHIFT))
850 if (tonality_slope > .15)
852 if (tonality_slope > .3)
854 if (tonality_slope < -.15)
856 if (tonality_slope < -.3)
859 //printf("%f\n", tonality_slope);
864 //printf("%f %d\n", tonality_slope, trim_index);
866 trim_index = rand()%11;
871 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
876 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
878 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
882 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
884 opus_val32 L, R, M, S;
885 /* We cast to 32-bit first because of the -32768 case */
887 R = EXTEND32(X[N0+j]);
890 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
891 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
894 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
896 /* We don't need thetas for lower bands with LM<=1 */
899 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
900 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
903 int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
908 VARDECL(celt_sig, in);
909 VARDECL(celt_sig, freq);
910 VARDECL(celt_norm, X);
911 VARDECL(celt_ener, bandE);
912 VARDECL(opus_val16, bandLogE);
913 VARDECL(int, fine_quant);
914 VARDECL(opus_val16, error);
915 VARDECL(int, pulses);
917 VARDECL(int, offsets);
918 VARDECL(int, fine_priority);
919 VARDECL(int, tf_res);
920 VARDECL(unsigned char, collapse_masks);
921 celt_sig *prefilter_mem;
922 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
925 const int CC = st->channels;
926 const int C = st->stream_channels;
929 int nbFilledBytes, nbAvailableBytes;
934 int pitch_index=COMBFILTER_MINPERIOD;
935 opus_val16 gain1 = 0;
939 opus_val16 pf_threshold;
942 opus_int32 total_bits;
943 opus_int32 total_boost;
946 int prefilter_tapset=0;
948 int anti_collapse_rsv;
949 int anti_collapse_on=0;
951 opus_val16 tf_estimate;
954 if (nbCompressedBytes<2 || pcm==NULL)
957 frame_size *= st->upsample;
958 for (LM=0;LM<=st->mode->maxLM;LM++)
959 if (st->mode->shortMdctSize<<LM==frame_size)
961 if (LM>st->mode->maxLM)
964 N = M*st->mode->shortMdctSize;
966 prefilter_mem = st->in_mem+CC*(st->overlap);
967 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
968 oldLogE = oldBandE + CC*st->mode->nbEBands;
969 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
977 nbFilledBytes=(tell+4)>>3;
981 if (st->signalling && enc==NULL)
983 int tmp = (st->mode->effEBands-st->end)>>1;
984 st->end = IMAX(1, st->mode->effEBands-tmp);
985 compressed[0] = tmp<<5;
986 compressed[0] |= LM<<3;
987 compressed[0] |= (C==2)<<2;
988 /* Convert "standard mode" to Opus header */
989 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
991 int c0 = toOpus(compressed[0]);
1000 celt_assert(st->signalling==0);
1003 /* Can't produce more than 1275 output bytes */
1004 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1005 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1007 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1009 opus_int32 den=st->mode->Fs>>BITRES;
1010 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1013 vbr_rate -= 8<<BITRES;
1015 effectiveBytes = vbr_rate>>(3+BITRES);
1019 tmp = st->bitrate*frame_size;
1022 if (st->bitrate!=OPUS_BITRATE_MAX)
1023 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1024 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1025 effectiveBytes = nbCompressedBytes;
1030 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1036 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1037 target rate and buffering.
1038 We must do this up front so that bust-prevention logic triggers
1039 correctly if we don't have enough bits. */
1040 if (st->constrained_vbr)
1042 opus_int32 vbr_bound;
1043 opus_int32 max_allowed;
1044 /* We could use any multiple of vbr_rate as bound (depending on the
1046 This is clamped to ensure we use at least two bytes if the encoder
1047 was entirely empty, but to allow 0 in hybrid mode. */
1048 vbr_bound = vbr_rate;
1049 max_allowed = IMIN(IMAX(tell==1?2:0,
1050 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1052 if(max_allowed < nbAvailableBytes)
1054 nbCompressedBytes = nbFilledBytes+max_allowed;
1055 nbAvailableBytes = max_allowed;
1056 ec_enc_shrink(enc, nbCompressedBytes);
1060 total_bits = nbCompressedBytes*8;
1063 if (effEnd > st->mode->effEBands)
1064 effEnd = st->mode->effEBands;
1066 ALLOC(in, CC*(N+st->overlap), celt_sig);
1068 /* Find pitch period and gain */
1070 VARDECL(celt_sig, _pre);
1073 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1076 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1081 const opus_val16 * restrict pcmp = pcm+c;
1082 celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
1093 x = MAX32(-65536.f, MIN32(65536.f,x));
1095 if (++count==st->upsample)
1102 /* Apply pre-emphasis */
1103 tmp = MULT16_16(st->mode->preemph[2], x);
1104 *inp = tmp + st->preemph_memE[c];
1105 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1106 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1107 silence = silence && *inp == 0;
1110 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1111 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1115 if ((rand()&0x3F)==0)
1119 ec_enc_bit_logp(enc, silence, 15);
1124 /*In VBR mode there is no need to send more than the minimum. */
1127 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1128 total_bits=nbCompressedBytes*8;
1130 ec_enc_shrink(enc, nbCompressedBytes);
1132 /* Pretend we've filled all the remaining bits with zeros
1133 (that's what the initialiser did anyway) */
1134 tell = nbCompressedBytes*8;
1135 enc->nbits_total+=tell-ec_tell(enc);
1137 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1139 VARDECL(opus_val16, pitch_buf);
1140 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1142 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1143 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1144 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1145 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1147 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1148 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1149 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1150 pitch_index = COMBFILTER_MAXPERIOD-2;
1151 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1152 if (st->loss_rate>2)
1153 gain1 = HALF32(gain1);
1154 if (st->loss_rate>4)
1155 gain1 = HALF32(gain1);
1156 if (st->loss_rate>8)
1158 prefilter_tapset = st->tapset_decision;
1163 /* Gain threshold for enabling the prefilter/postfilter */
1164 pf_threshold = QCONST16(.2f,15);
1166 /* Adjusting the threshold based on rate and continuity */
1167 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1168 pf_threshold += QCONST16(.2f,15);
1169 if (nbAvailableBytes<25)
1170 pf_threshold += QCONST16(.1f,15);
1171 if (nbAvailableBytes<35)
1172 pf_threshold += QCONST16(.1f,15);
1173 if (st->prefilter_gain > QCONST16(.4f,15))
1174 pf_threshold -= QCONST16(.1f,15);
1175 if (st->prefilter_gain > QCONST16(.55f,15))
1176 pf_threshold -= QCONST16(.1f,15);
1178 /* Hard threshold at 0.2 */
1179 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1180 if (gain1<pf_threshold)
1182 if(st->start==0 && tell+16<=total_bits)
1183 ec_enc_bit_logp(enc, 0, 1);
1187 /*This block is not gated by a total bits check only because
1188 of the nbAvailableBytes check above.*/
1192 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1193 gain1=st->prefilter_gain;
1196 qg = ((gain1+1536)>>10)/3-1;
1198 qg = (int)floor(.5f+gain1*32/3)-1;
1200 qg = IMAX(0, IMIN(7, qg));
1201 ec_enc_bit_logp(enc, 1, 1);
1203 octave = EC_ILOG(pitch_index)-5;
1204 ec_enc_uint(enc, octave, 6);
1205 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1207 ec_enc_bits(enc, qg, 3);
1208 if (ec_tell(enc)+2<=total_bits)
1209 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1211 prefilter_tapset = 0;
1212 gain1 = QCONST16(0.09375f,15)*(qg+1);
1215 /*printf("%d %f\n", pitch_index, gain1);*/
1218 int offset = st->mode->shortMdctSize-st->mode->overlap;
1219 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1220 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1222 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1223 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1224 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1226 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1227 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1228 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1229 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1231 if (N>COMBFILTER_MAXPERIOD)
1233 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1235 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1236 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1245 if (LM>0 && ec_tell(enc)+3<=total_bits)
1247 if (st->complexity > 1)
1249 isTransient = transient_analysis(in, N+st->overlap, CC,
1250 st->overlap, &tf_estimate);
1254 ec_enc_bit_logp(enc, isTransient, 3);
1257 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1258 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1259 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1261 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1266 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1268 if (st->upsample != 1)
1272 int bound = N/st->upsample;
1273 for (i=0;i<bound;i++)
1274 freq[c*N+i] *= st->upsample;
1279 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1281 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1283 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1285 /* Band normalisation */
1286 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1288 ALLOC(tf_res, st->mode->nbEBands, int);
1289 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1290 for (i=effEnd;i<st->end;i++)
1291 tf_res[i] = tf_res[effEnd-1];
1293 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1294 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1295 oldBandE, total_bits, error, enc,
1296 C, LM, nbAvailableBytes, st->force_intra,
1297 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1299 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1301 st->spread_decision = SPREAD_NORMAL;
1302 if (ec_tell(enc)+4<=total_bits)
1304 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1306 if (st->complexity == 0)
1307 st->spread_decision = SPREAD_NONE;
1309 st->spread_decision = spreading_decision(st->mode, X,
1310 &st->tonal_average, st->spread_decision, &st->hf_average,
1311 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1312 /*if (st->frame_tonality > .7*32768)
1313 st->spread_decision = SPREAD_NONE;
1314 else if (st->frame_tonality > .3*32768)
1315 st->spread_decision = SPREAD_LIGHT;
1316 else if (st->frame_tonality > .1*32768)
1317 st->spread_decision = SPREAD_NORMAL;
1319 st->spread_decision = SPREAD_AGGRESSIVE;*/
1321 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1324 ALLOC(cap, st->mode->nbEBands, int);
1325 ALLOC(offsets, st->mode->nbEBands, int);
1327 init_caps(st->mode,cap,LM,C);
1328 for (i=0;i<st->mode->nbEBands;i++)
1330 /* Dynamic allocation code */
1331 /* Make sure that dynamic allocation can't make us bust the budget */
1332 if (effectiveBytes > 50 && LM>=1)
1343 for (i=st->start+1;i<st->end-1;i++)
1346 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1348 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1349 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1355 offsets[i] += 1+(rand()&0x3);
1358 if (d2 > SHL16(t1,DB_SHIFT))
1360 if (d2 > SHL16(t2,DB_SHIFT))
1367 if (boost_amount[0]>.2)
1368 offsets[boost_band[0]]+=2;
1369 if (boost_amount[0]>.4)
1370 offsets[boost_band[0]]+=2;
1371 if (boost_amount[1]>.2)
1372 offsets[boost_band[1]]+=2;
1373 if (boost_amount[1]>.4)
1374 offsets[boost_band[1]]+=2;
1375 //printf("%f %f\n", boost_amount[0], boost_amount[1]);
1378 total_bits<<=BITRES;
1380 tell = ec_tell_frac(enc);
1381 for (i=st->start;i<st->end;i++)
1384 int dynalloc_loop_logp;
1387 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1388 /* quanta is 6 bits, but no more than 1 bit/sample
1389 and no less than 1/8 bit/sample */
1390 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1391 dynalloc_loop_logp = dynalloc_logp;
1393 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1394 && boost < cap[i]; j++)
1397 flag = j<offsets[i];
1398 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1399 tell = ec_tell_frac(enc);
1403 total_boost += quanta;
1404 dynalloc_loop_logp = 1;
1406 /* Making dynalloc more likely */
1408 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1412 if (tell+(6<<BITRES) <= total_bits - total_boost)
1414 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1415 st->end, LM, C, N, st->tonality_slope/16384.);
1416 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1417 tell = ec_tell_frac(enc);
1424 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1426 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1428 /* Account for coarse energy */
1429 effectiveRate = (8*effectiveBytes - 80)>>LM;
1431 /* effectiveRate in kb/s */
1432 effectiveRate = 2*effectiveRate/5;
1433 if (effectiveRate<35)
1435 else if (effectiveRate<50)
1437 else if (effectiveRate<68)
1439 else if (effectiveRate<84)
1441 else if (effectiveRate<102)
1443 else if (effectiveRate<130)
1447 intensity = IMIN(st->end,IMAX(st->start, intensity));
1450 /* Variable bitrate */
1455 /* The target rate in 8th bits per frame */
1456 opus_int32 target, new_target;
1457 opus_int32 min_allowed;
1458 int lm_diff = st->mode->maxLM - LM;
1460 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1461 The CELT allocator will just not be able to use more than that anyway. */
1462 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1463 target = vbr_rate - ((40*C+20)<<BITRES);
1464 if (st->constrained_vbr)
1465 target += (st->vbr_offset>>lm_diff);
1468 new_target = SHL32(MULT16_32_Q15(target, SUB16(tf_estimate, QCONST16(0.05, 14))),1);
1470 new_target = target*(tf_estimate-.05);
1477 tonal = st->frame_tonality/32768.;
1479 coded_bands = st->lastCodedBands ? st->lastCodedBands : st->mode->nbEBands;
1480 //coded_bands = IMIN(coded_bands, st->mode->nbEBands-1);
1481 coded_bins = st->mode->eBands[coded_bands]<<LM;
1483 coded_bins += st->mode->eBands[IMIN(intensity, coded_bands)]<<LM;
1484 tonal_target = target + (coded_bins<<BITRES)*1.55*tonal;
1485 new_target = IMAX(tonal_target,new_target);
1488 /* The current offset is removed from the target and the space used
1490 target=new_target+tell;
1491 //printf("%d\n", target);
1492 /* In VBR mode the frame size must not be reduced so much that it would
1493 result in the encoder running out of bits.
1494 The margin of 2 bytes ensures that none of the bust-prevention logic
1495 in the decoder will have triggered so far. */
1496 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1498 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1499 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1500 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1502 /* By how much did we "miss" the target on that frame */
1503 delta = target - vbr_rate;
1505 target=nbAvailableBytes<<(BITRES+3);
1507 /*If the frame is silent we don't adjust our drift, otherwise
1508 the encoder will shoot to very high rates after hitting a
1509 span of silence, but we do allow the bitres to refill.
1510 This means that we'll undershoot our target in CVBR/VBR modes
1511 on files with lots of silence. */
1514 nbAvailableBytes = 2;
1515 target = 2*8<<BITRES;
1519 if (st->vbr_count < 970)
1522 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1524 alpha = QCONST16(.001f,15);
1525 /* How many bits have we used in excess of what we're allowed */
1526 if (st->constrained_vbr)
1527 st->vbr_reservoir += target - vbr_rate;
1528 /*printf ("%d\n", st->vbr_reservoir);*/
1530 /* Compute the offset we need to apply in order to reach the target */
1531 if (st->constrained_vbr)
1533 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1534 st->vbr_offset = -st->vbr_drift;
1536 /*printf ("%d\n", st->vbr_drift);*/
1538 if (st->constrained_vbr && st->vbr_reservoir < 0)
1540 /* We're under the min value -- increase rate */
1541 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1542 /* Unless we're just coding silence */
1543 nbAvailableBytes += silence?0:adjust;
1544 st->vbr_reservoir = 0;
1545 /*printf ("+%d\n", adjust);*/
1547 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1548 /* This moves the raw bits to take into account the new compressed size */
1549 ec_enc_shrink(enc, nbCompressedBytes);
1552 /* Bit allocation */
1553 ALLOC(fine_quant, st->mode->nbEBands, int);
1554 ALLOC(pulses, st->mode->nbEBands, int);
1555 ALLOC(fine_priority, st->mode->nbEBands, int);
1557 /* bits = packet size - where we are - safety*/
1558 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1559 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1560 bits -= anti_collapse_rsv;
1561 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1562 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1563 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1564 st->lastCodedBands = codedBands;
1566 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1568 #ifdef MEASURE_NORM_MSE
1573 X0[i+c*N] = X[i+c*N];
1575 for (i=0;i<C*st->mode->nbEBands;i++)
1576 bandE0[i] = bandE[i];
1579 /* Residual quantisation */
1580 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1581 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1582 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1583 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1585 if (anti_collapse_rsv > 0)
1587 anti_collapse_on = st->consec_transient<2;
1589 anti_collapse_on = rand()&0x1;
1591 ec_enc_bits(enc, anti_collapse_on, 1);
1593 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1597 for (i=0;i<C*st->mode->nbEBands;i++)
1598 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1602 /* Re-synthesis of the coded audio if required */
1604 celt_sig *out_mem[2];
1605 celt_sig *overlap_mem[2];
1607 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1610 for (i=0;i<C*st->mode->nbEBands;i++)
1614 #ifdef MEASURE_NORM_MSE
1615 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1617 if (anti_collapse_on)
1619 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1620 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1624 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1626 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1628 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1631 for (i=0;i<M*st->mode->eBands[st->start];i++)
1635 for (i=M*st->mode->eBands[st->end];i<N;i++)
1642 freq[N+i] = freq[i];
1645 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1647 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1649 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1651 overlap_mem[1] = overlap_mem[0] + st->overlap;
1653 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1656 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1657 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1658 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1659 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1660 st->mode->window, st->overlap);
1662 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1663 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1664 st->mode->window, st->mode->overlap);
1667 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1668 st->prefilter_period_old = st->prefilter_period;
1669 st->prefilter_gain_old = st->prefilter_gain;
1670 st->prefilter_tapset_old = st->prefilter_tapset;
1674 st->prefilter_period = pitch_index;
1675 st->prefilter_gain = gain1;
1676 st->prefilter_tapset = prefilter_tapset;
1680 st->prefilter_period_old = st->prefilter_period;
1681 st->prefilter_gain_old = st->prefilter_gain;
1682 st->prefilter_tapset_old = st->prefilter_tapset;
1687 for (i=0;i<st->mode->nbEBands;i++)
1688 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1693 for (i=0;i<CC*st->mode->nbEBands;i++)
1694 oldLogE2[i] = oldLogE[i];
1695 for (i=0;i<CC*st->mode->nbEBands;i++)
1696 oldLogE[i] = oldBandE[i];
1698 for (i=0;i<CC*st->mode->nbEBands;i++)
1699 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1701 /* In case start or end were to change */
1704 for (i=0;i<st->start;i++)
1706 oldBandE[c*st->mode->nbEBands+i]=0;
1707 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1709 for (i=st->end;i<st->mode->nbEBands;i++)
1711 oldBandE[c*st->mode->nbEBands+i]=0;
1712 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1717 st->consec_transient++;
1719 st->consec_transient=0;
1722 /* If there's any room left (can only happen for very high rates),
1723 it's already filled with zeros */
1728 nbCompressedBytes++;
1732 if (ec_get_error(enc))
1733 return OPUS_INTERNAL_ERROR;
1735 return nbCompressedBytes;
1742 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1744 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1747 #ifndef DISABLE_FLOAT_API
1748 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1751 VARDECL(opus_int16, in);
1755 return OPUS_BAD_ARG;
1759 ALLOC(in, C*N, opus_int16);
1762 in[j] = FLOAT2INT16(pcm[j]);
1764 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1767 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1772 #endif /* DISABLE_FLOAT_API */
1775 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1778 VARDECL(celt_sig, in);
1782 return OPUS_BAD_ARG;
1786 ALLOC(in, C*N, celt_sig);
1787 for (j=0;j<C*N;j++) {
1788 in[j] = SCALEOUT(pcm[j]);
1791 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1794 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1800 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1802 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1807 #endif /* CUSTOM_MODES */
1809 int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1813 va_start(ap, request);
1816 case OPUS_SET_COMPLEXITY_REQUEST:
1818 int value = va_arg(ap, opus_int32);
1819 if (value<0 || value>10)
1821 st->complexity = value;
1824 case CELT_SET_START_BAND_REQUEST:
1826 opus_int32 value = va_arg(ap, opus_int32);
1827 if (value<0 || value>=st->mode->nbEBands)
1832 case CELT_SET_END_BAND_REQUEST:
1834 opus_int32 value = va_arg(ap, opus_int32);
1835 if (value<1 || value>st->mode->nbEBands)
1840 case CELT_SET_PREDICTION_REQUEST:
1842 int value = va_arg(ap, opus_int32);
1843 if (value<0 || value>2)
1845 st->disable_pf = value<=1;
1846 st->force_intra = value==0;
1849 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1851 int value = va_arg(ap, opus_int32);
1852 if (value<0 || value>100)
1854 st->loss_rate = value;
1857 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1859 opus_int32 value = va_arg(ap, opus_int32);
1860 st->constrained_vbr = value;
1863 case OPUS_SET_VBR_REQUEST:
1865 opus_int32 value = va_arg(ap, opus_int32);
1869 case OPUS_SET_BITRATE_REQUEST:
1871 opus_int32 value = va_arg(ap, opus_int32);
1872 if (value<=500 && value!=OPUS_BITRATE_MAX)
1874 value = IMIN(value, 260000*st->channels);
1875 st->bitrate = value;
1878 case CELT_SET_CHANNELS_REQUEST:
1880 opus_int32 value = va_arg(ap, opus_int32);
1881 if (value<1 || value>2)
1883 st->stream_channels = value;
1886 case OPUS_RESET_STATE:
1889 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1890 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
1891 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
1892 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
1893 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
1894 opus_custom_encoder_get_size(st->mode, st->channels)-
1895 ((char*)&st->ENCODER_RESET_START - (char*)st));
1896 for (i=0;i<st->channels*st->mode->nbEBands;i++)
1897 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1899 st->delayedIntra = 1;
1900 st->spread_decision = SPREAD_NORMAL;
1901 st->tonal_average = 256;
1903 st->tapset_decision = 0;
1907 case CELT_SET_INPUT_CLIPPING_REQUEST:
1909 opus_int32 value = va_arg(ap, opus_int32);
1914 case CELT_SET_SIGNALLING_REQUEST:
1916 opus_int32 value = va_arg(ap, opus_int32);
1917 st->signalling = value;
1920 case CELT_SET_TONALITY_REQUEST:
1922 opus_int32 value = va_arg(ap, opus_int32);
1923 st->frame_tonality = value;
1926 case CELT_SET_TONALITY_SLOPE_REQUEST:
1928 opus_int32 value = va_arg(ap, opus_int32);
1929 st->tonality_slope = value;
1932 case CELT_GET_MODE_REQUEST:
1934 const CELTMode ** value = va_arg(ap, const CELTMode**);
1940 case OPUS_GET_FINAL_RANGE_REQUEST:
1942 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1955 return OPUS_BAD_ARG;
1958 return OPUS_UNIMPLEMENTED;
1961 /**********************************************************************/
1965 /**********************************************************************/
1966 #define DECODE_BUFFER_SIZE 2048
1969 @brief Decoder state
1971 struct OpusCustomDecoder {
1972 const OpusCustomMode *mode;
1975 int stream_channels;
1981 /* Everything beyond this point gets cleared on a reset */
1982 #define DECODER_RESET_START rng
1986 int last_pitch_index;
1988 int postfilter_period;
1989 int postfilter_period_old;
1990 opus_val16 postfilter_gain;
1991 opus_val16 postfilter_gain_old;
1992 int postfilter_tapset;
1993 int postfilter_tapset_old;
1995 celt_sig preemph_memD[2];
1997 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
1998 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
1999 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
2000 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
2001 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
2002 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
2005 int celt_decoder_get_size(int channels)
2007 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
2008 return opus_custom_decoder_get_size(mode, channels);
2011 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
2013 int size = sizeof(struct CELTDecoder)
2014 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
2015 + channels*LPC_ORDER*sizeof(opus_val16)
2016 + 4*2*mode->nbEBands*sizeof(opus_val16);
2021 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
2024 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
2025 ret = opus_custom_decoder_init(st, mode, channels);
2028 opus_custom_decoder_destroy(st);
2035 #endif /* CUSTOM_MODES */
2037 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
2040 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
2043 st->downsample = resampling_factor(sampling_rate);
2044 if (st->downsample==0)
2045 return OPUS_BAD_ARG;
2050 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
2052 if (channels < 0 || channels > 2)
2053 return OPUS_BAD_ARG;
2056 return OPUS_ALLOC_FAIL;
2058 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2061 st->overlap = mode->overlap;
2062 st->stream_channels = st->channels = channels;
2066 st->end = st->mode->effEBands;
2071 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2077 void opus_custom_decoder_destroy(CELTDecoder *st)
2081 #endif /* CUSTOM_MODES */
2083 static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)
2087 int overlap = st->mode->overlap;
2088 opus_val16 fade = Q15ONE;
2090 const int C = st->channels;
2092 celt_sig *out_mem[2];
2093 celt_sig *decode_mem[2];
2094 celt_sig *overlap_mem[2];
2096 opus_val32 *out_syn[2];
2097 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2101 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2102 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2103 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2105 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2106 oldBandE = lpc+C*LPC_ORDER;
2107 oldLogE = oldBandE + 2*st->mode->nbEBands;
2108 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2109 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2111 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2113 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2115 len = N+st->mode->overlap;
2117 if (st->loss_count >= 5 || st->start!=0)
2119 /* Noise-based PLC/CNG */
2120 VARDECL(celt_sig, freq);
2121 VARDECL(celt_norm, X);
2122 VARDECL(celt_ener, bandE);
2127 if (effEnd > st->mode->effEBands)
2128 effEnd = st->mode->effEBands;
2130 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2131 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2132 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2134 if (st->loss_count >= 5)
2135 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2138 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2141 for (i=st->start;i<st->end;i++)
2142 oldBandE[c*st->mode->nbEBands+i] -= decay;
2144 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2149 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2151 for (i=st->start;i<st->mode->effEBands;i++)
2156 boffs = N*c+(st->mode->eBands[i]<<LM);
2157 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2158 for (j=0;j<blen;j++)
2160 seed = celt_lcg_rand(seed);
2161 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2163 renormalise_vector(X+boffs, blen, Q15ONE);
2165 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2170 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2173 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2177 int bound = st->mode->eBands[effEnd]<<LM;
2178 if (st->downsample!=1)
2179 bound = IMIN(bound, N/st->downsample);
2180 for (i=bound;i<N;i++)
2183 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2185 /* Pitch-based PLC */
2186 if (st->loss_count == 0)
2188 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2189 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2190 search by using only part of the decode buffer */
2192 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2193 /* Max pitch is 100 samples (480 Hz) */
2194 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2195 poffset-100, &pitch_index);
2196 pitch_index = poffset-pitch_index;
2197 st->last_pitch_index = pitch_index;
2199 pitch_index = st->last_pitch_index;
2200 fade = QCONST16(.8f,15);
2204 VARDECL(opus_val32, e);
2205 opus_val16 exc[MAX_PERIOD];
2206 opus_val32 ac[LPC_ORDER+1];
2207 opus_val16 decay = 1;
2209 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};
2211 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2213 offset = MAX_PERIOD-pitch_index;
2214 for (i=0;i<MAX_PERIOD;i++)
2215 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2217 if (st->loss_count == 0)
2219 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2220 LPC_ORDER, MAX_PERIOD);
2222 /* Noise floor -40 dB */
2224 ac[0] += SHR32(ac[0],13);
2229 for (i=1;i<=LPC_ORDER;i++)
2231 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2233 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2235 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2239 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2241 for (i=0;i<LPC_ORDER;i++)
2242 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2243 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2244 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2245 /* Check if the waveform is decaying (and if so how fast) */
2247 opus_val32 E1=1, E2=1;
2249 if (pitch_index <= MAX_PERIOD/2)
2250 period = pitch_index;
2252 period = MAX_PERIOD/2;
2253 for (i=0;i<period;i++)
2255 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2256 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2260 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2263 /* Copy excitation, taking decay into account */
2264 for (i=0;i<len+st->mode->overlap;i++)
2267 if (offset+i >= MAX_PERIOD)
2269 offset -= pitch_index;
2270 decay = MULT16_16_Q15(decay, decay);
2272 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2273 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2274 S1 += SHR32(MULT16_16(tmp,tmp),8);
2276 for (i=0;i<LPC_ORDER;i++)
2277 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2278 for (i=0;i<len+st->mode->overlap;i++)
2279 e[i] = MULT16_32_Q15(fade, e[i]);
2280 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2284 for (i=0;i<len+overlap;i++)
2286 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2287 S2 += SHR32(MULT16_16(tmp,tmp),8);
2289 /* This checks for an "explosion" in the synthesis */
2291 if (!(S1 > SHR32(S2,2)))
2293 /* Float test is written this way to catch NaNs at the same time */
2294 if (!(S1 > 0.2f*S2))
2297 for (i=0;i<len+overlap;i++)
2301 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2302 for (i=0;i<len+overlap;i++)
2303 e[i] = MULT16_32_Q15(ratio, e[i]);
2307 /* Apply post-filter to the MDCT overlap of the previous frame */
2308 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2309 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2312 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2313 out_mem[c][i] = out_mem[c][N+i];
2315 /* Apply TDAC to the concealed audio so that it blends with the
2316 previous and next frames */
2317 for (i=0;i<overlap/2;i++)
2320 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2321 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2322 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2323 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2326 out_mem[c][MAX_PERIOD-N+i] = e[i];
2328 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2329 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2330 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2332 for (i=0;i<overlap;i++)
2333 out_mem[c][MAX_PERIOD+i] = e[i];
2337 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2344 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec)
2347 int spread_decision;
2350 VARDECL(celt_sig, freq);
2351 VARDECL(celt_norm, X);
2352 VARDECL(celt_ener, bandE);
2353 VARDECL(int, fine_quant);
2354 VARDECL(int, pulses);
2356 VARDECL(int, offsets);
2357 VARDECL(int, fine_priority);
2358 VARDECL(int, tf_res);
2359 VARDECL(unsigned char, collapse_masks);
2360 celt_sig *out_mem[2];
2361 celt_sig *decode_mem[2];
2362 celt_sig *overlap_mem[2];
2363 celt_sig *out_syn[2];
2365 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2370 const int CC = st->channels;
2375 int postfilter_pitch;
2376 opus_val16 postfilter_gain;
2379 opus_int32 total_bits;
2383 int postfilter_tapset;
2384 int anti_collapse_rsv;
2385 int anti_collapse_on=0;
2387 int C = st->stream_channels;
2390 frame_size *= st->downsample;
2393 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2394 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2395 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2397 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2398 oldBandE = lpc+CC*LPC_ORDER;
2399 oldLogE = oldBandE + 2*st->mode->nbEBands;
2400 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2401 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2404 if (st->signalling && data!=NULL)
2407 /* Convert "standard mode" to Opus header */
2408 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2410 data0 = fromOpus(data0);
2412 return OPUS_INVALID_PACKET;
2414 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2415 LM = (data0>>3)&0x3;
2416 C = 1 + ((data0>>2)&0x1);
2419 if (LM>st->mode->maxLM)
2420 return OPUS_INVALID_PACKET;
2421 if (frame_size < st->mode->shortMdctSize<<LM)
2422 return OPUS_BUFFER_TOO_SMALL;
2424 frame_size = st->mode->shortMdctSize<<LM;
2429 for (LM=0;LM<=st->mode->maxLM;LM++)
2430 if (st->mode->shortMdctSize<<LM==frame_size)
2432 if (LM>st->mode->maxLM)
2433 return OPUS_BAD_ARG;
2437 if (len<0 || len>1275 || pcm==NULL)
2438 return OPUS_BAD_ARG;
2440 N = M*st->mode->shortMdctSize;
2443 if (effEnd > st->mode->effEBands)
2444 effEnd = st->mode->effEBands;
2446 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2447 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2448 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2450 for (i=0;i<M*st->mode->eBands[st->start];i++)
2454 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2458 if (data == NULL || len<=1)
2460 celt_decode_lost(st, pcm, N, LM);
2462 return frame_size/st->downsample;
2467 ec_dec_init(&_dec,(unsigned char*)data,len);
2473 for (i=0;i<st->mode->nbEBands;i++)
2474 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2478 tell = ec_tell(dec);
2480 if (tell >= total_bits)
2483 silence = ec_dec_bit_logp(dec, 15);
2488 /* Pretend we've read all the remaining bits */
2490 dec->nbits_total+=tell-ec_tell(dec);
2493 postfilter_gain = 0;
2494 postfilter_pitch = 0;
2495 postfilter_tapset = 0;
2496 if (st->start==0 && tell+16 <= total_bits)
2498 if(ec_dec_bit_logp(dec, 1))
2501 octave = ec_dec_uint(dec, 6);
2502 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2503 qg = ec_dec_bits(dec, 3);
2504 if (ec_tell(dec)+2<=total_bits)
2505 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2506 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2508 tell = ec_tell(dec);
2511 if (LM > 0 && tell+3 <= total_bits)
2513 isTransient = ec_dec_bit_logp(dec, 3);
2514 tell = ec_tell(dec);
2524 /* Decode the global flags (first symbols in the stream) */
2525 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2526 /* Get band energies */
2527 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2528 intra_ener, dec, C, LM);
2530 ALLOC(tf_res, st->mode->nbEBands, int);
2531 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2533 tell = ec_tell(dec);
2534 spread_decision = SPREAD_NORMAL;
2535 if (tell+4 <= total_bits)
2536 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2538 ALLOC(pulses, st->mode->nbEBands, int);
2539 ALLOC(cap, st->mode->nbEBands, int);
2540 ALLOC(offsets, st->mode->nbEBands, int);
2541 ALLOC(fine_priority, st->mode->nbEBands, int);
2543 init_caps(st->mode,cap,LM,C);
2546 total_bits<<=BITRES;
2547 tell = ec_tell_frac(dec);
2548 for (i=st->start;i<st->end;i++)
2551 int dynalloc_loop_logp;
2553 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2554 /* quanta is 6 bits, but no more than 1 bit/sample
2555 and no less than 1/8 bit/sample */
2556 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2557 dynalloc_loop_logp = dynalloc_logp;
2559 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2562 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2563 tell = ec_tell_frac(dec);
2567 total_bits -= quanta;
2568 dynalloc_loop_logp = 1;
2571 /* Making dynalloc more likely */
2573 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2576 ALLOC(fine_quant, st->mode->nbEBands, int);
2577 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2578 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2580 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2581 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2582 bits -= anti_collapse_rsv;
2583 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2584 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2585 fine_quant, fine_priority, C, LM, dec, 0, 0);
2587 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2589 /* Decode fixed codebook */
2590 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2591 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2592 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2593 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2595 if (anti_collapse_rsv > 0)
2597 anti_collapse_on = ec_dec_bits(dec, 1);
2600 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2601 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2603 if (anti_collapse_on)
2604 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2605 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2607 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2611 for (i=0;i<C*st->mode->nbEBands;i++)
2614 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2618 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2620 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2622 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2625 for (i=0;i<M*st->mode->eBands[st->start];i++)
2629 int bound = M*st->mode->eBands[effEnd];
2630 if (st->downsample!=1)
2631 bound = IMIN(bound, N/st->downsample);
2632 for (i=bound;i<N;i++)
2636 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2638 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2643 freq[N+i] = freq[i];
2648 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2651 /* Compute inverse MDCTs */
2652 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2655 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2656 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2657 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2658 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2659 st->mode->window, st->overlap);
2661 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2662 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2663 st->mode->window, st->mode->overlap);
2666 st->postfilter_period_old = st->postfilter_period;
2667 st->postfilter_gain_old = st->postfilter_gain;
2668 st->postfilter_tapset_old = st->postfilter_tapset;
2669 st->postfilter_period = postfilter_pitch;
2670 st->postfilter_gain = postfilter_gain;
2671 st->postfilter_tapset = postfilter_tapset;
2674 st->postfilter_period_old = st->postfilter_period;
2675 st->postfilter_gain_old = st->postfilter_gain;
2676 st->postfilter_tapset_old = st->postfilter_tapset;
2680 for (i=0;i<st->mode->nbEBands;i++)
2681 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2684 /* In case start or end were to change */
2687 for (i=0;i<2*st->mode->nbEBands;i++)
2688 oldLogE2[i] = oldLogE[i];
2689 for (i=0;i<2*st->mode->nbEBands;i++)
2690 oldLogE[i] = oldBandE[i];
2691 for (i=0;i<2*st->mode->nbEBands;i++)
2692 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2694 for (i=0;i<2*st->mode->nbEBands;i++)
2695 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2699 for (i=0;i<st->start;i++)
2701 oldBandE[c*st->mode->nbEBands+i]=0;
2702 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2704 for (i=st->end;i<st->mode->nbEBands;i++)
2706 oldBandE[c*st->mode->nbEBands+i]=0;
2707 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2712 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2715 if (ec_tell(dec) > 8*len)
2716 return OPUS_INTERNAL_ERROR;
2717 if(ec_get_error(dec))
2719 return frame_size/st->downsample;
2726 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2728 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2731 #ifndef DISABLE_FLOAT_API
2732 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2735 VARDECL(opus_int16, out);
2739 return OPUS_BAD_ARG;
2744 ALLOC(out, C*N, opus_int16);
2745 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2747 for (j=0;j<C*ret;j++)
2748 pcm[j]=out[j]*(1.f/32768.f);
2753 #endif /* DISABLE_FLOAT_API */
2757 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2759 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2762 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2765 VARDECL(celt_sig, out);
2769 return OPUS_BAD_ARG;
2773 ALLOC(out, C*N, celt_sig);
2775 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2778 for (j=0;j<C*ret;j++)
2779 pcm[j] = FLOAT2INT16 (out[j]);
2786 #endif /* CUSTOM_MODES */
2788 int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...)
2792 va_start(ap, request);
2795 case CELT_SET_START_BAND_REQUEST:
2797 opus_int32 value = va_arg(ap, opus_int32);
2798 if (value<0 || value>=st->mode->nbEBands)
2803 case CELT_SET_END_BAND_REQUEST:
2805 opus_int32 value = va_arg(ap, opus_int32);
2806 if (value<1 || value>st->mode->nbEBands)
2811 case CELT_SET_CHANNELS_REQUEST:
2813 opus_int32 value = va_arg(ap, opus_int32);
2814 if (value<1 || value>2)
2816 st->stream_channels = value;
2819 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
2821 opus_int32 *value = va_arg(ap, opus_int32*);
2828 case OPUS_GET_LOOKAHEAD_REQUEST:
2830 opus_int32 *value = va_arg(ap, opus_int32*);
2833 *value = st->overlap/st->downsample;
2836 case OPUS_RESET_STATE:
2839 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
2840 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
2841 oldBandE = lpc+st->channels*LPC_ORDER;
2842 oldLogE = oldBandE + 2*st->mode->nbEBands;
2843 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2844 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
2845 opus_custom_decoder_get_size(st->mode, st->channels)-
2846 ((char*)&st->DECODER_RESET_START - (char*)st));
2847 for (i=0;i<2*st->mode->nbEBands;i++)
2848 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2851 case OPUS_GET_PITCH_REQUEST:
2853 opus_int32 *value = va_arg(ap, opus_int32*);
2856 *value = st->postfilter_period;
2860 case CELT_GET_MODE_REQUEST:
2862 const CELTMode ** value = va_arg(ap, const CELTMode**);
2868 case CELT_SET_SIGNALLING_REQUEST:
2870 opus_int32 value = va_arg(ap, opus_int32);
2871 st->signalling = value;
2874 case OPUS_GET_FINAL_RANGE_REQUEST:
2876 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2890 return OPUS_BAD_ARG;
2893 return OPUS_UNIMPLEMENTED;
2898 const char *opus_strerror(int error)
2900 static const char *error_strings[8] = {
2906 "request not implemented",
2908 "memory allocation failed"
2910 if (error > 0 || error < -7)
2911 return "unknown error";
2913 return error_strings[-error];
2916 const char *opus_get_version_string(void)
2918 return "libopus " OPUS_VERSION