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;
180 AnalysisInfo analysis;
182 opus_val32 preemph_memE[2];
183 opus_val32 preemph_memD[2];
185 /* VBR-related parameters */
186 opus_int32 vbr_reservoir;
187 opus_int32 vbr_drift;
188 opus_int32 vbr_offset;
189 opus_int32 vbr_count;
192 celt_sig syn_mem[2][2*MAX_PERIOD];
195 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
196 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
197 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
198 /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
201 int celt_encoder_get_size(int channels)
203 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
204 return opus_custom_encoder_get_size(mode, channels);
207 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
209 int size = sizeof(struct CELTEncoder)
210 + (2*channels*mode->overlap-1)*sizeof(celt_sig)
211 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
212 + 3*channels*mode->nbEBands*sizeof(opus_val16);
217 CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
220 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
221 /* init will handle the NULL case */
222 ret = opus_custom_encoder_init(st, mode, channels);
225 opus_custom_encoder_destroy(st);
232 #endif /* CUSTOM_MODES */
234 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
237 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
240 st->upsample = resampling_factor(sampling_rate);
244 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
246 if (channels < 0 || channels > 2)
249 if (st==NULL || mode==NULL)
250 return OPUS_ALLOC_FAIL;
252 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
255 st->overlap = mode->overlap;
256 st->stream_channels = st->channels = channels;
260 st->end = st->mode->effEBands;
263 st->constrained_vbr = 1;
266 st->bitrate = OPUS_BITRATE_MAX;
271 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
277 void opus_custom_encoder_destroy(CELTEncoder *st)
281 #endif /* CUSTOM_MODES */
283 static inline opus_val16 SIG2WORD16(celt_sig x)
286 x = PSHR32(x, SIG_SHIFT);
287 x = MAX32(x, -32768);
291 return (opus_val16)x;
295 static int transient_analysis(const opus_val32 * restrict in, int len, int C,
296 int overlap, opus_val16 *tf_estimate)
299 VARDECL(opus_val16, tmp);
300 opus_val32 mem0=0,mem1=0;
301 int is_transient = 0;
305 opus_val32 L1, L2, tf_tmp;
306 VARDECL(opus_val16, bins);
308 ALLOC(tmp, len, opus_val16);
312 ALLOC(bins, N, opus_val16);
316 tmp[i] = SHR32(in[i],SIG_SHIFT);
319 tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
322 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
329 mem0 = mem1 + y - SHL32(x,1);
330 mem1 = x - SHR32(y,1);
332 mem0 = mem1 + y - 2*x;
335 tmp[i] = EXTRACT16(SHR32(y,2));
337 /* First few samples are bad because we don't propagate the memory */
345 opus_val16 max_abs=0;
346 for (j=0;j<block;j++)
347 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
349 maxbin = MAX16(maxbin, bins[i]);
357 opus_val16 t1, t2, t3;
360 tmp_bin = bins[i]+MULT16_16_Q15(QCONST16(.05f,15),maxbin);
361 L1 += EXTEND32(tmp_bin);
362 L2 += SHR32(MULT16_16(tmp_bin, tmp_bin), 4);
363 t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
364 t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
365 t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
389 tf_tmp = SHL32(DIV32( SHL32(EXTEND32(celt_sqrt(SHR16(L2,4) * N)), 14), ADD32(EPSILON, L1)), 4);
390 *tf_estimate = MAX16(QCONST16(1.f, 14), EXTRACT16(MIN16(QCONST32(1.99, 14), tf_tmp)));
393 is_transient = rand()&0x1;
398 /** Apply window and compute the MDCT for all sub-frames and
399 all channels in a frame */
400 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int C, int LM)
402 if (C==1 && !shortBlocks)
404 const int overlap = OVERLAP(mode);
405 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
407 const int overlap = OVERLAP(mode);
408 int N = mode->shortMdctSize<<LM;
413 N = mode->shortMdctSize;
419 /* Interleaving the sub-frames while doing the MDCTs */
420 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);
426 /** Compute the IMDCT and apply window for all sub-frames and
427 all channels in a frame */
428 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
429 celt_sig * restrict out_mem[],
430 celt_sig * restrict overlap_mem[], int C, int LM)
433 const int N = mode->shortMdctSize<<LM;
434 const int overlap = OVERLAP(mode);
435 VARDECL(opus_val32, x);
438 ALLOC(x, N+overlap, opus_val32);
447 N2 = mode->shortMdctSize;
450 /* Prevents problems from the imdct doing the overlap-add */
451 OPUS_CLEAR(x, overlap);
455 /* IMDCT on the interleaved the sub-frames */
456 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
459 for (j=0;j<overlap;j++)
460 out_mem[c][j] = x[j] + overlap_mem[c][j];
462 out_mem[c][j] = x[j];
463 for (j=0;j<overlap;j++)
464 overlap_mem[c][j] = x[N+j];
469 static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
475 celt_sig * restrict x;
476 opus_val16 * restrict y;
482 celt_sig tmp = *x + m;
483 m = MULT16_32_Q15(coef[0], tmp)
484 - MULT16_32_Q15(coef[1], *x);
485 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
487 /* Technically the store could be moved outside of the if because
488 the stores we don't want will just be overwritten */
490 *y = SCALEOUT(SIG2WORD16(tmp));
491 if (++count==downsample)
501 static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
502 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
503 const opus_val16 *window, int overlap)
506 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
507 opus_val16 g00, g01, g02, g10, g11, g12;
508 static const opus_val16 gains[3][3] = {
509 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
510 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
511 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
512 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
513 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
514 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
515 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
516 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
517 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
518 for (i=0;i<overlap;i++)
521 f = MULT16_16_Q15(window[i],window[i]);
523 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
524 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
525 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
526 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
527 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
528 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
529 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
530 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
531 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
532 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
535 for (i=overlap;i<N;i++)
537 + MULT16_32_Q15(g10,x[i-T1])
538 + MULT16_32_Q15(g11,x[i-T1-1])
539 + MULT16_32_Q15(g11,x[i-T1+1])
540 + MULT16_32_Q15(g12,x[i-T1-2])
541 + MULT16_32_Q15(g12,x[i-T1+2]);
544 static const signed char tf_select_table[4][8] = {
545 {0, -1, 0, -1, 0,-1, 0,-1},
546 {0, -1, 0, -2, 1, 0, 1,-1},
547 {0, -2, 0, -3, 2, 0, 1,-1},
548 {0, -2, 0, -3, 3, 0, 1,-1},
551 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM)
558 L1 += EXTEND32(ABS16(tmp[i]));
559 /* When in doubt, prefer goo freq resolution */
560 bias = QCONST16(.015f,15)*LM;
561 L1 = MAC16_32_Q15(L1, bias, L1);
566 static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
567 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
571 VARDECL(int, metric);
576 VARDECL(celt_norm, tmp);
581 if (nbCompressedBytes<15*C)
585 tf_res[i] = isTransient;
588 if (nbCompressedBytes<40)
590 else if (nbCompressedBytes<60)
592 else if (nbCompressedBytes<100)
597 ALLOC(metric, len, int);
598 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
599 ALLOC(path0, len, int);
600 ALLOC(path1, len, int);
606 opus_val32 L1, best_L1;
608 N = (m->eBands[i+1]-m->eBands[i])<<LM;
610 tmp[j] = X[j+(m->eBands[i]<<LM)];
611 /* Just add the right channel if we're in stereo */
614 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));
615 L1 = l1_metric(tmp, N, isTransient ? LM : 0);
617 /*printf ("%f ", L1);*/
628 haar1(tmp, N>>(LM-k), 1<<(LM-k));
630 haar1(tmp, N>>k, 1<<k);
632 L1 = l1_metric(tmp, N, B);
640 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
642 metric[i] = best_level;
644 metric[i] = -best_level;
645 *tf_sum += metric[i];
648 /* NOTE: Future optimized implementations could detect extreme transients and set
649 tf_select = 1 but so far we have not found a reliable way of making this useful */
653 cost1 = isTransient ? 0 : lambda;
654 /* Viterbi forward pass */
661 from1 = cost1 + lambda;
671 from0 = cost0 + lambda;
681 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
682 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
684 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
685 /* Viterbi backward pass to check the decisions */
686 for (i=len-2;i>=0;i--)
688 if (tf_res[i+1] == 1)
689 tf_res[i] = path1[i+1];
691 tf_res[i] = path0[i+1];
695 tf_select = rand()&0x1;
696 tf_res[0] = rand()&0x1;
698 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
703 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
711 budget = enc->storage*8;
713 logp = isTransient ? 2 : 4;
714 /* Reserve space to code the tf_select decision. */
715 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
716 budget -= tf_select_rsv;
717 curr = tf_changed = 0;
718 for (i=start;i<end;i++)
720 if (tell+logp<=budget)
722 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
729 logp = isTransient ? 4 : 5;
731 /* Only code tf_select if it would actually make a difference. */
733 tf_select_table[LM][4*isTransient+0+tf_changed]!=
734 tf_select_table[LM][4*isTransient+2+tf_changed])
735 ec_enc_bit_logp(enc, tf_select, 1);
738 for (i=start;i<end;i++)
739 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
740 /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
743 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
745 int i, curr, tf_select;
752 budget = dec->storage*8;
754 logp = isTransient ? 2 : 4;
755 tf_select_rsv = LM>0 && tell+logp+1<=budget;
756 budget -= tf_select_rsv;
757 tf_changed = curr = 0;
758 for (i=start;i<end;i++)
760 if (tell+logp<=budget)
762 curr ^= ec_dec_bit_logp(dec, logp);
767 logp = isTransient ? 4 : 5;
771 tf_select_table[LM][4*isTransient+0+tf_changed] !=
772 tf_select_table[LM][4*isTransient+2+tf_changed])
774 tf_select = ec_dec_bit_logp(dec, 1);
776 for (i=start;i<end;i++)
778 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
782 static void init_caps(const CELTMode *m,int *cap,int LM,int C)
785 for (i=0;i<m->nbEBands;i++)
788 N=(m->eBands[i+1]-m->eBands[i])<<LM;
789 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
793 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
794 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
795 AnalysisInfo *analysis, opus_val16 *stereo_saving)
801 opus_val16 trim = QCONST16(5.f, 8);
804 opus_val16 sum = 0; /* Q10 */
805 /* Compute inter-channel correlation for low frequencies */
809 opus_val32 partial = 0;
810 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
811 partial = MAC16_16(partial, X[j], X[N0+j]);
812 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
814 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
815 /*printf ("%f\n", sum);*/
816 if (sum > QCONST16(.995f,10))
818 else if (sum > QCONST16(.92f,10))
820 else if (sum > QCONST16(.85f,10))
822 else if (sum > QCONST16(.8f,10))
825 trim += MAX16(-QCONST16(4.f, 8), .75f*log2(1.001-sum*sum));
826 *stereo_saving = -.25*log2(1.01-sum*sum);
827 /*printf("%f\n", *stereo_saving);*/
833 /* Estimate spectral tilt */
835 for (i=0;i<end-1;i++)
837 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
840 /* We divide by two here to avoid making the tilt larger for stereo as a
841 result of a bug in the loop above */
843 /*printf("%f\n", diff);*/
844 if (diff > QCONST16(2.f, DB_SHIFT))
846 if (diff > QCONST16(8.f, DB_SHIFT))
848 if (diff < -QCONST16(4.f, DB_SHIFT))
850 if (diff < -QCONST16(10.f, DB_SHIFT))
852 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), (diff+QCONST16(1.f, DB_SHIFT))/16 ));
856 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), 2*(analysis->tonality_slope+.05)));
857 /*if (analysis->tonality_slope > .15)
859 if (analysis->tonality_slope > .3)
861 if (analysis->tonality_slope < -.15)
863 if (analysis->tonality_slope < -.3)
867 /*printf("%d %f\n", trim_index, trim);*/
868 /*trim_index = floor(.5+trim);*/
873 /*printf("%d\n", trim_index);*/
875 trim_index = rand()%11;
880 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
885 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
887 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
891 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
893 opus_val32 L, R, M, S;
894 /* We cast to 32-bit first because of the -32768 case */
896 R = EXTEND32(X[N0+j]);
899 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
900 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
903 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
905 /* We don't need thetas for lower bands with LM<=1 */
908 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
909 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
912 int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
917 VARDECL(celt_sig, in);
918 VARDECL(celt_sig, freq);
919 VARDECL(celt_norm, X);
920 VARDECL(celt_ener, bandE);
921 VARDECL(opus_val16, bandLogE);
922 VARDECL(int, fine_quant);
923 VARDECL(opus_val16, error);
924 VARDECL(int, pulses);
926 VARDECL(int, offsets);
927 VARDECL(int, fine_priority);
928 VARDECL(int, tf_res);
929 VARDECL(unsigned char, collapse_masks);
930 celt_sig *prefilter_mem;
931 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
934 const int CC = st->channels;
935 const int C = st->stream_channels;
938 int nbFilledBytes, nbAvailableBytes;
943 int pitch_index=COMBFILTER_MINPERIOD;
944 opus_val16 gain1 = 0;
948 opus_val16 pf_threshold;
951 opus_int32 total_bits;
952 opus_int32 total_boost;
955 int prefilter_tapset=0;
957 int anti_collapse_rsv;
958 int anti_collapse_on=0;
960 opus_val16 tf_estimate=0;
961 opus_val16 stereo_saving = 0;
964 if (nbCompressedBytes<2 || pcm==NULL)
967 frame_size *= st->upsample;
968 for (LM=0;LM<=st->mode->maxLM;LM++)
969 if (st->mode->shortMdctSize<<LM==frame_size)
971 if (LM>st->mode->maxLM)
974 N = M*st->mode->shortMdctSize;
976 prefilter_mem = st->in_mem+CC*(st->overlap);
977 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
978 oldLogE = oldBandE + CC*st->mode->nbEBands;
979 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
987 nbFilledBytes=(tell+4)>>3;
991 if (st->signalling && enc==NULL)
993 int tmp = (st->mode->effEBands-st->end)>>1;
994 st->end = IMAX(1, st->mode->effEBands-tmp);
995 compressed[0] = tmp<<5;
996 compressed[0] |= LM<<3;
997 compressed[0] |= (C==2)<<2;
998 /* Convert "standard mode" to Opus header */
999 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
1001 int c0 = toOpus(compressed[0]);
1003 return OPUS_BAD_ARG;
1007 nbCompressedBytes--;
1010 celt_assert(st->signalling==0);
1013 /* Can't produce more than 1275 output bytes */
1014 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1015 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1017 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1019 opus_int32 den=st->mode->Fs>>BITRES;
1020 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1023 vbr_rate -= 8<<BITRES;
1025 effectiveBytes = vbr_rate>>(3+BITRES);
1029 tmp = st->bitrate*frame_size;
1032 if (st->bitrate!=OPUS_BITRATE_MAX)
1033 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1034 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1035 effectiveBytes = nbCompressedBytes;
1040 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1046 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1047 target rate and buffering.
1048 We must do this up front so that bust-prevention logic triggers
1049 correctly if we don't have enough bits. */
1050 if (st->constrained_vbr)
1052 opus_int32 vbr_bound;
1053 opus_int32 max_allowed;
1054 /* We could use any multiple of vbr_rate as bound (depending on the
1056 This is clamped to ensure we use at least two bytes if the encoder
1057 was entirely empty, but to allow 0 in hybrid mode. */
1058 vbr_bound = vbr_rate;
1059 max_allowed = IMIN(IMAX(tell==1?2:0,
1060 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1062 if(max_allowed < nbAvailableBytes)
1064 nbCompressedBytes = nbFilledBytes+max_allowed;
1065 nbAvailableBytes = max_allowed;
1066 ec_enc_shrink(enc, nbCompressedBytes);
1070 total_bits = nbCompressedBytes*8;
1073 if (effEnd > st->mode->effEBands)
1074 effEnd = st->mode->effEBands;
1076 ALLOC(in, CC*(N+st->overlap), celt_sig);
1078 /* Find pitch period and gain */
1080 VARDECL(celt_sig, _pre);
1083 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1086 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1091 const opus_val16 * restrict pcmp = pcm+c;
1092 celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
1103 x = MAX32(-65536.f, MIN32(65536.f,x));
1105 if (++count==st->upsample)
1112 /* Apply pre-emphasis */
1113 tmp = MULT16_16(st->mode->preemph[2], x);
1114 *inp = tmp + st->preemph_memE[c];
1115 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1116 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1117 silence = silence && *inp == 0;
1120 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1121 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1125 if ((rand()&0x3F)==0)
1129 ec_enc_bit_logp(enc, silence, 15);
1134 /*In VBR mode there is no need to send more than the minimum. */
1137 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1138 total_bits=nbCompressedBytes*8;
1140 ec_enc_shrink(enc, nbCompressedBytes);
1142 /* Pretend we've filled all the remaining bits with zeros
1143 (that's what the initialiser did anyway) */
1144 tell = nbCompressedBytes*8;
1145 enc->nbits_total+=tell-ec_tell(enc);
1147 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1149 VARDECL(opus_val16, pitch_buf);
1150 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1152 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1153 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1154 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1155 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1157 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1158 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1159 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1160 pitch_index = COMBFILTER_MAXPERIOD-2;
1161 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1162 if (st->loss_rate>2)
1163 gain1 = HALF32(gain1);
1164 if (st->loss_rate>4)
1165 gain1 = HALF32(gain1);
1166 if (st->loss_rate>8)
1168 prefilter_tapset = st->tapset_decision;
1173 /* Gain threshold for enabling the prefilter/postfilter */
1174 pf_threshold = QCONST16(.2f,15);
1176 /* Adjusting the threshold based on rate and continuity */
1177 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1178 pf_threshold += QCONST16(.2f,15);
1179 if (nbAvailableBytes<25)
1180 pf_threshold += QCONST16(.1f,15);
1181 if (nbAvailableBytes<35)
1182 pf_threshold += QCONST16(.1f,15);
1183 if (st->prefilter_gain > QCONST16(.4f,15))
1184 pf_threshold -= QCONST16(.1f,15);
1185 if (st->prefilter_gain > QCONST16(.55f,15))
1186 pf_threshold -= QCONST16(.1f,15);
1188 /* Hard threshold at 0.2 */
1189 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1190 if (gain1<pf_threshold)
1192 if(st->start==0 && tell+16<=total_bits)
1193 ec_enc_bit_logp(enc, 0, 1);
1197 /*This block is not gated by a total bits check only because
1198 of the nbAvailableBytes check above.*/
1202 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1203 gain1=st->prefilter_gain;
1206 qg = ((gain1+1536)>>10)/3-1;
1208 qg = (int)floor(.5f+gain1*32/3)-1;
1210 qg = IMAX(0, IMIN(7, qg));
1211 ec_enc_bit_logp(enc, 1, 1);
1213 octave = EC_ILOG(pitch_index)-5;
1214 ec_enc_uint(enc, octave, 6);
1215 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1217 ec_enc_bits(enc, qg, 3);
1218 if (ec_tell(enc)+2<=total_bits)
1219 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1221 prefilter_tapset = 0;
1222 gain1 = QCONST16(0.09375f,15)*(qg+1);
1225 /*printf("%d %f\n", pitch_index, gain1);*/
1228 int offset = st->mode->shortMdctSize-st->mode->overlap;
1229 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1230 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1232 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1233 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1234 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1236 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1237 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1238 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1239 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1241 if (N>COMBFILTER_MAXPERIOD)
1243 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1245 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1246 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1255 if (LM>0 && ec_tell(enc)+3<=total_bits)
1257 if (st->complexity > 1)
1259 isTransient = transient_analysis(in, N+st->overlap, CC,
1260 st->overlap, &tf_estimate);
1264 ec_enc_bit_logp(enc, isTransient, 3);
1267 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1268 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1269 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1271 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1276 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1278 if (st->upsample != 1)
1282 int bound = N/st->upsample;
1283 for (i=0;i<bound;i++)
1284 freq[c*N+i] *= st->upsample;
1289 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1291 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1293 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1295 /* Band normalisation */
1296 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1298 ALLOC(tf_res, st->mode->nbEBands, int);
1299 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1300 for (i=effEnd;i<st->end;i++)
1301 tf_res[i] = tf_res[effEnd-1];
1303 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1304 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1305 oldBandE, total_bits, error, enc,
1306 C, LM, nbAvailableBytes, st->force_intra,
1307 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1309 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1311 if (ec_tell(enc)+4<=total_bits)
1313 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1315 if (st->complexity == 0)
1316 st->spread_decision = SPREAD_NONE;
1318 if (st->analysis.valid)
1320 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1321 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1322 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1323 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1324 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1325 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1327 st->spread_decision = spreading_decision(st->mode, X,
1328 &st->tonal_average, st->spread_decision, &st->hf_average,
1329 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1331 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1332 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1334 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1337 ALLOC(cap, st->mode->nbEBands, int);
1338 ALLOC(offsets, st->mode->nbEBands, int);
1340 init_caps(st->mode,cap,LM,C);
1341 for (i=0;i<st->mode->nbEBands;i++)
1343 /* Dynamic allocation code */
1344 /* Make sure that dynamic allocation can't make us bust the budget */
1345 if (effectiveBytes > 50 && LM>=1)
1356 for (i=st->start+1;i<st->end-1;i++)
1359 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1361 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1362 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1368 offsets[i] += 1+(rand()&0x3);
1371 if (d2 > SHL16(t1,DB_SHIFT))
1373 if (d2 > SHL16(t2,DB_SHIFT))
1379 if (0 && st->analysis.valid)
1381 if (st->analysis.boost_amount[0]>.2)
1382 offsets[st->analysis.boost_band[0]]+=2;
1383 if (st->analysis.boost_amount[0]>.4)
1384 offsets[st->analysis.boost_band[0]]+=2;
1385 if (st->analysis.boost_amount[1]>.2)
1386 offsets[st->analysis.boost_band[1]]+=2;
1387 if (st->analysis.boost_amount[1]>.4)
1388 offsets[st->analysis.boost_band[1]]+=2;
1392 total_bits<<=BITRES;
1394 tell = ec_tell_frac(enc);
1395 for (i=st->start;i<st->end;i++)
1398 int dynalloc_loop_logp;
1401 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1402 /* quanta is 6 bits, but no more than 1 bit/sample
1403 and no less than 1/8 bit/sample */
1404 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1405 dynalloc_loop_logp = dynalloc_logp;
1407 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1408 && boost < cap[i]; j++)
1411 flag = j<offsets[i];
1412 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1413 tell = ec_tell_frac(enc);
1417 total_boost += quanta;
1418 dynalloc_loop_logp = 1;
1420 /* Making dynalloc more likely */
1422 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1426 if (tell+(6<<BITRES) <= total_bits - total_boost)
1428 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1429 st->end, LM, C, N, &st->analysis, &stereo_saving);
1430 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1431 tell = ec_tell_frac(enc);
1438 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1440 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1442 /* Account for coarse energy */
1443 effectiveRate = (8*effectiveBytes - 80)>>LM;
1445 /* effectiveRate in kb/s */
1446 effectiveRate = 2*effectiveRate/5;
1447 if (effectiveRate<35)
1449 else if (effectiveRate<50)
1451 else if (effectiveRate<68)
1453 else if (effectiveRate<84)
1455 else if (effectiveRate<102)
1457 else if (effectiveRate<130)
1461 intensity = IMIN(st->end,IMAX(st->start, intensity));
1464 /* Variable bitrate */
1469 /* The target rate in 8th bits per frame */
1470 opus_int32 target, new_target;
1471 opus_int32 min_allowed;
1474 int lm_diff = st->mode->maxLM - LM;
1475 coded_bands = st->lastCodedBands ? st->lastCodedBands : st->mode->nbEBands;
1476 coded_bins = st->mode->eBands[coded_bands]<<LM;
1478 coded_bins += st->mode->eBands[IMIN(intensity, coded_bands)]<<LM;
1480 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1481 The CELT allocator will just not be able to use more than that anyway. */
1482 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1483 target = vbr_rate - ((40*C+20)<<BITRES);
1484 if (st->constrained_vbr)
1485 target += (st->vbr_offset>>lm_diff);
1487 /*printf("%f %f %f\n", st->analysis.activity, st->analysis.tonality, tf_estimate);*/
1489 if (st->analysis.valid && st->analysis.activity<.4)
1490 target -= (coded_bins<<BITRES)*1*(.4-st->analysis.activity);
1492 target -= MIN32(target/3, stereo_saving*(st->mode->eBands[intensity]<<LM<<BITRES));
1496 new_target = SHL32(MULT16_32_Q15(target, SUB16(tf_estimate, QCONST16(0.05, 14))),1);
1498 new_target = target*(tf_estimate-.05);
1502 if (st->analysis.valid) {
1505 tonal = st->analysis.tonality*st->analysis.tonality;
1507 tonal_target = target + (coded_bins<<BITRES)*1.5f*tonal;
1508 /*printf("%f %d\n", tonal, tonal_target);*/
1509 new_target = IMAX(tonal_target,new_target);
1513 /* The current offset is removed from the target and the space used
1515 target=new_target+tell;
1516 /* In VBR mode the frame size must not be reduced so much that it would
1517 result in the encoder running out of bits.
1518 The margin of 2 bytes ensures that none of the bust-prevention logic
1519 in the decoder will have triggered so far. */
1520 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1522 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1523 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1524 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1526 /* By how much did we "miss" the target on that frame */
1527 delta = target - vbr_rate;
1529 target=nbAvailableBytes<<(BITRES+3);
1531 /*If the frame is silent we don't adjust our drift, otherwise
1532 the encoder will shoot to very high rates after hitting a
1533 span of silence, but we do allow the bitres to refill.
1534 This means that we'll undershoot our target in CVBR/VBR modes
1535 on files with lots of silence. */
1538 nbAvailableBytes = 2;
1539 target = 2*8<<BITRES;
1543 if (st->vbr_count < 970)
1546 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1548 alpha = QCONST16(.001f,15);
1549 /* How many bits have we used in excess of what we're allowed */
1550 if (st->constrained_vbr)
1551 st->vbr_reservoir += target - vbr_rate;
1552 /*printf ("%d\n", st->vbr_reservoir);*/
1554 /* Compute the offset we need to apply in order to reach the target */
1555 if (st->constrained_vbr)
1557 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1558 st->vbr_offset = -st->vbr_drift;
1560 /*printf ("%d\n", st->vbr_drift);*/
1562 if (st->constrained_vbr && st->vbr_reservoir < 0)
1564 /* We're under the min value -- increase rate */
1565 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1566 /* Unless we're just coding silence */
1567 nbAvailableBytes += silence?0:adjust;
1568 st->vbr_reservoir = 0;
1569 /*printf ("+%d\n", adjust);*/
1571 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1572 /*printf("%d\n", nbCompressedBytes*50*8);*/
1573 /* This moves the raw bits to take into account the new compressed size */
1574 ec_enc_shrink(enc, nbCompressedBytes);
1577 /* Bit allocation */
1578 ALLOC(fine_quant, st->mode->nbEBands, int);
1579 ALLOC(pulses, st->mode->nbEBands, int);
1580 ALLOC(fine_priority, st->mode->nbEBands, int);
1582 /* bits = packet size - where we are - safety*/
1583 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1584 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1585 bits -= anti_collapse_rsv;
1586 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1587 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1588 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1589 st->lastCodedBands = codedBands;
1591 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1593 #ifdef MEASURE_NORM_MSE
1598 X0[i+c*N] = X[i+c*N];
1600 for (i=0;i<C*st->mode->nbEBands;i++)
1601 bandE0[i] = bandE[i];
1604 /* Residual quantisation */
1605 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1606 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1607 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1608 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1610 if (anti_collapse_rsv > 0)
1612 anti_collapse_on = st->consec_transient<2;
1614 anti_collapse_on = rand()&0x1;
1616 ec_enc_bits(enc, anti_collapse_on, 1);
1618 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1622 for (i=0;i<C*st->mode->nbEBands;i++)
1623 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1627 /* Re-synthesis of the coded audio if required */
1629 celt_sig *out_mem[2];
1630 celt_sig *overlap_mem[2];
1632 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1635 for (i=0;i<C*st->mode->nbEBands;i++)
1639 #ifdef MEASURE_NORM_MSE
1640 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1642 if (anti_collapse_on)
1644 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1645 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1649 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1651 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1653 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1656 for (i=0;i<M*st->mode->eBands[st->start];i++)
1660 for (i=M*st->mode->eBands[st->end];i<N;i++)
1667 freq[N+i] = freq[i];
1670 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1672 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1674 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1676 overlap_mem[1] = overlap_mem[0] + st->overlap;
1678 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1681 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1682 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1683 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1684 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1685 st->mode->window, st->overlap);
1687 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1688 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1689 st->mode->window, st->mode->overlap);
1692 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1693 st->prefilter_period_old = st->prefilter_period;
1694 st->prefilter_gain_old = st->prefilter_gain;
1695 st->prefilter_tapset_old = st->prefilter_tapset;
1699 st->prefilter_period = pitch_index;
1700 st->prefilter_gain = gain1;
1701 st->prefilter_tapset = prefilter_tapset;
1705 st->prefilter_period_old = st->prefilter_period;
1706 st->prefilter_gain_old = st->prefilter_gain;
1707 st->prefilter_tapset_old = st->prefilter_tapset;
1712 for (i=0;i<st->mode->nbEBands;i++)
1713 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1718 for (i=0;i<CC*st->mode->nbEBands;i++)
1719 oldLogE2[i] = oldLogE[i];
1720 for (i=0;i<CC*st->mode->nbEBands;i++)
1721 oldLogE[i] = oldBandE[i];
1723 for (i=0;i<CC*st->mode->nbEBands;i++)
1724 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1726 /* In case start or end were to change */
1729 for (i=0;i<st->start;i++)
1731 oldBandE[c*st->mode->nbEBands+i]=0;
1732 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1734 for (i=st->end;i<st->mode->nbEBands;i++)
1736 oldBandE[c*st->mode->nbEBands+i]=0;
1737 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1742 st->consec_transient++;
1744 st->consec_transient=0;
1747 /* If there's any room left (can only happen for very high rates),
1748 it's already filled with zeros */
1753 nbCompressedBytes++;
1757 if (ec_get_error(enc))
1758 return OPUS_INTERNAL_ERROR;
1760 return nbCompressedBytes;
1767 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1769 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1772 #ifndef DISABLE_FLOAT_API
1773 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1776 VARDECL(opus_int16, in);
1780 return OPUS_BAD_ARG;
1784 ALLOC(in, C*N, opus_int16);
1787 in[j] = FLOAT2INT16(pcm[j]);
1789 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1792 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1797 #endif /* DISABLE_FLOAT_API */
1800 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1803 VARDECL(celt_sig, in);
1807 return OPUS_BAD_ARG;
1811 ALLOC(in, C*N, celt_sig);
1812 for (j=0;j<C*N;j++) {
1813 in[j] = SCALEOUT(pcm[j]);
1816 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1819 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1825 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1827 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1832 #endif /* CUSTOM_MODES */
1834 int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1838 va_start(ap, request);
1841 case OPUS_SET_COMPLEXITY_REQUEST:
1843 int value = va_arg(ap, opus_int32);
1844 if (value<0 || value>10)
1846 st->complexity = value;
1849 case CELT_SET_START_BAND_REQUEST:
1851 opus_int32 value = va_arg(ap, opus_int32);
1852 if (value<0 || value>=st->mode->nbEBands)
1857 case CELT_SET_END_BAND_REQUEST:
1859 opus_int32 value = va_arg(ap, opus_int32);
1860 if (value<1 || value>st->mode->nbEBands)
1865 case CELT_SET_PREDICTION_REQUEST:
1867 int value = va_arg(ap, opus_int32);
1868 if (value<0 || value>2)
1870 st->disable_pf = value<=1;
1871 st->force_intra = value==0;
1874 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1876 int value = va_arg(ap, opus_int32);
1877 if (value<0 || value>100)
1879 st->loss_rate = value;
1882 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1884 opus_int32 value = va_arg(ap, opus_int32);
1885 st->constrained_vbr = value;
1888 case OPUS_SET_VBR_REQUEST:
1890 opus_int32 value = va_arg(ap, opus_int32);
1894 case OPUS_SET_BITRATE_REQUEST:
1896 opus_int32 value = va_arg(ap, opus_int32);
1897 if (value<=500 && value!=OPUS_BITRATE_MAX)
1899 value = IMIN(value, 260000*st->channels);
1900 st->bitrate = value;
1903 case CELT_SET_CHANNELS_REQUEST:
1905 opus_int32 value = va_arg(ap, opus_int32);
1906 if (value<1 || value>2)
1908 st->stream_channels = value;
1911 case OPUS_RESET_STATE:
1914 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1915 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
1916 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
1917 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
1918 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
1919 opus_custom_encoder_get_size(st->mode, st->channels)-
1920 ((char*)&st->ENCODER_RESET_START - (char*)st));
1921 for (i=0;i<st->channels*st->mode->nbEBands;i++)
1922 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1924 st->delayedIntra = 1;
1925 st->spread_decision = SPREAD_NORMAL;
1926 st->tonal_average = 256;
1928 st->tapset_decision = 0;
1932 case CELT_SET_INPUT_CLIPPING_REQUEST:
1934 opus_int32 value = va_arg(ap, opus_int32);
1939 case CELT_SET_SIGNALLING_REQUEST:
1941 opus_int32 value = va_arg(ap, opus_int32);
1942 st->signalling = value;
1945 case CELT_SET_ANALYSIS_REQUEST:
1947 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
1949 OPUS_COPY(&st->analysis, info, 1);
1952 case CELT_GET_MODE_REQUEST:
1954 const CELTMode ** value = va_arg(ap, const CELTMode**);
1960 case OPUS_GET_FINAL_RANGE_REQUEST:
1962 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1975 return OPUS_BAD_ARG;
1978 return OPUS_UNIMPLEMENTED;
1981 /**********************************************************************/
1985 /**********************************************************************/
1986 #define DECODE_BUFFER_SIZE 2048
1989 @brief Decoder state
1991 struct OpusCustomDecoder {
1992 const OpusCustomMode *mode;
1995 int stream_channels;
2001 /* Everything beyond this point gets cleared on a reset */
2002 #define DECODER_RESET_START rng
2006 int last_pitch_index;
2008 int postfilter_period;
2009 int postfilter_period_old;
2010 opus_val16 postfilter_gain;
2011 opus_val16 postfilter_gain_old;
2012 int postfilter_tapset;
2013 int postfilter_tapset_old;
2015 celt_sig preemph_memD[2];
2017 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
2018 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
2019 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
2020 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
2021 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
2022 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
2025 int celt_decoder_get_size(int channels)
2027 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
2028 return opus_custom_decoder_get_size(mode, channels);
2031 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
2033 int size = sizeof(struct CELTDecoder)
2034 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
2035 + channels*LPC_ORDER*sizeof(opus_val16)
2036 + 4*2*mode->nbEBands*sizeof(opus_val16);
2041 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
2044 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
2045 ret = opus_custom_decoder_init(st, mode, channels);
2048 opus_custom_decoder_destroy(st);
2055 #endif /* CUSTOM_MODES */
2057 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
2060 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
2063 st->downsample = resampling_factor(sampling_rate);
2064 if (st->downsample==0)
2065 return OPUS_BAD_ARG;
2070 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
2072 if (channels < 0 || channels > 2)
2073 return OPUS_BAD_ARG;
2076 return OPUS_ALLOC_FAIL;
2078 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2081 st->overlap = mode->overlap;
2082 st->stream_channels = st->channels = channels;
2086 st->end = st->mode->effEBands;
2091 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2097 void opus_custom_decoder_destroy(CELTDecoder *st)
2101 #endif /* CUSTOM_MODES */
2103 static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)
2107 int overlap = st->mode->overlap;
2108 opus_val16 fade = Q15ONE;
2110 const int C = st->channels;
2112 celt_sig *out_mem[2];
2113 celt_sig *decode_mem[2];
2114 celt_sig *overlap_mem[2];
2116 opus_val32 *out_syn[2];
2117 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2121 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2122 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2123 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2125 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2126 oldBandE = lpc+C*LPC_ORDER;
2127 oldLogE = oldBandE + 2*st->mode->nbEBands;
2128 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2129 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2131 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2133 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2135 len = N+st->mode->overlap;
2137 if (st->loss_count >= 5 || st->start!=0)
2139 /* Noise-based PLC/CNG */
2140 VARDECL(celt_sig, freq);
2141 VARDECL(celt_norm, X);
2142 VARDECL(celt_ener, bandE);
2147 if (effEnd > st->mode->effEBands)
2148 effEnd = st->mode->effEBands;
2150 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2151 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2152 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2154 if (st->loss_count >= 5)
2155 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2158 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2161 for (i=st->start;i<st->end;i++)
2162 oldBandE[c*st->mode->nbEBands+i] -= decay;
2164 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2169 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2171 for (i=st->start;i<st->mode->effEBands;i++)
2176 boffs = N*c+(st->mode->eBands[i]<<LM);
2177 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2178 for (j=0;j<blen;j++)
2180 seed = celt_lcg_rand(seed);
2181 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2183 renormalise_vector(X+boffs, blen, Q15ONE);
2185 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2190 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2193 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2197 int bound = st->mode->eBands[effEnd]<<LM;
2198 if (st->downsample!=1)
2199 bound = IMIN(bound, N/st->downsample);
2200 for (i=bound;i<N;i++)
2203 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2205 /* Pitch-based PLC */
2206 if (st->loss_count == 0)
2208 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2209 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2210 search by using only part of the decode buffer */
2212 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2213 /* Max pitch is 100 samples (480 Hz) */
2214 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2215 poffset-100, &pitch_index);
2216 pitch_index = poffset-pitch_index;
2217 st->last_pitch_index = pitch_index;
2219 pitch_index = st->last_pitch_index;
2220 fade = QCONST16(.8f,15);
2224 VARDECL(opus_val32, e);
2225 opus_val16 exc[MAX_PERIOD];
2226 opus_val32 ac[LPC_ORDER+1];
2227 opus_val16 decay = 1;
2229 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};
2231 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2233 offset = MAX_PERIOD-pitch_index;
2234 for (i=0;i<MAX_PERIOD;i++)
2235 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2237 if (st->loss_count == 0)
2239 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2240 LPC_ORDER, MAX_PERIOD);
2242 /* Noise floor -40 dB */
2244 ac[0] += SHR32(ac[0],13);
2249 for (i=1;i<=LPC_ORDER;i++)
2251 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2253 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2255 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2259 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2261 for (i=0;i<LPC_ORDER;i++)
2262 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2263 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2264 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2265 /* Check if the waveform is decaying (and if so how fast) */
2267 opus_val32 E1=1, E2=1;
2269 if (pitch_index <= MAX_PERIOD/2)
2270 period = pitch_index;
2272 period = MAX_PERIOD/2;
2273 for (i=0;i<period;i++)
2275 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2276 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2280 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2283 /* Copy excitation, taking decay into account */
2284 for (i=0;i<len+st->mode->overlap;i++)
2287 if (offset+i >= MAX_PERIOD)
2289 offset -= pitch_index;
2290 decay = MULT16_16_Q15(decay, decay);
2292 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2293 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2294 S1 += SHR32(MULT16_16(tmp,tmp),8);
2296 for (i=0;i<LPC_ORDER;i++)
2297 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2298 for (i=0;i<len+st->mode->overlap;i++)
2299 e[i] = MULT16_32_Q15(fade, e[i]);
2300 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2304 for (i=0;i<len+overlap;i++)
2306 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2307 S2 += SHR32(MULT16_16(tmp,tmp),8);
2309 /* This checks for an "explosion" in the synthesis */
2311 if (!(S1 > SHR32(S2,2)))
2313 /* Float test is written this way to catch NaNs at the same time */
2314 if (!(S1 > 0.2f*S2))
2317 for (i=0;i<len+overlap;i++)
2321 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2322 for (i=0;i<len+overlap;i++)
2323 e[i] = MULT16_32_Q15(ratio, e[i]);
2327 /* Apply post-filter to the MDCT overlap of the previous frame */
2328 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2329 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2332 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2333 out_mem[c][i] = out_mem[c][N+i];
2335 /* Apply TDAC to the concealed audio so that it blends with the
2336 previous and next frames */
2337 for (i=0;i<overlap/2;i++)
2340 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2341 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2342 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2343 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2346 out_mem[c][MAX_PERIOD-N+i] = e[i];
2348 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2349 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2350 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2352 for (i=0;i<overlap;i++)
2353 out_mem[c][MAX_PERIOD+i] = e[i];
2357 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2364 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec)
2367 int spread_decision;
2370 VARDECL(celt_sig, freq);
2371 VARDECL(celt_norm, X);
2372 VARDECL(celt_ener, bandE);
2373 VARDECL(int, fine_quant);
2374 VARDECL(int, pulses);
2376 VARDECL(int, offsets);
2377 VARDECL(int, fine_priority);
2378 VARDECL(int, tf_res);
2379 VARDECL(unsigned char, collapse_masks);
2380 celt_sig *out_mem[2];
2381 celt_sig *decode_mem[2];
2382 celt_sig *overlap_mem[2];
2383 celt_sig *out_syn[2];
2385 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2390 const int CC = st->channels;
2395 int postfilter_pitch;
2396 opus_val16 postfilter_gain;
2399 opus_int32 total_bits;
2403 int postfilter_tapset;
2404 int anti_collapse_rsv;
2405 int anti_collapse_on=0;
2407 int C = st->stream_channels;
2410 frame_size *= st->downsample;
2413 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2414 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2415 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2417 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2418 oldBandE = lpc+CC*LPC_ORDER;
2419 oldLogE = oldBandE + 2*st->mode->nbEBands;
2420 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2421 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2424 if (st->signalling && data!=NULL)
2427 /* Convert "standard mode" to Opus header */
2428 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2430 data0 = fromOpus(data0);
2432 return OPUS_INVALID_PACKET;
2434 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2435 LM = (data0>>3)&0x3;
2436 C = 1 + ((data0>>2)&0x1);
2439 if (LM>st->mode->maxLM)
2440 return OPUS_INVALID_PACKET;
2441 if (frame_size < st->mode->shortMdctSize<<LM)
2442 return OPUS_BUFFER_TOO_SMALL;
2444 frame_size = st->mode->shortMdctSize<<LM;
2449 for (LM=0;LM<=st->mode->maxLM;LM++)
2450 if (st->mode->shortMdctSize<<LM==frame_size)
2452 if (LM>st->mode->maxLM)
2453 return OPUS_BAD_ARG;
2457 if (len<0 || len>1275 || pcm==NULL)
2458 return OPUS_BAD_ARG;
2460 N = M*st->mode->shortMdctSize;
2463 if (effEnd > st->mode->effEBands)
2464 effEnd = st->mode->effEBands;
2466 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2467 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2468 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2470 for (i=0;i<M*st->mode->eBands[st->start];i++)
2474 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2478 if (data == NULL || len<=1)
2480 celt_decode_lost(st, pcm, N, LM);
2482 return frame_size/st->downsample;
2487 ec_dec_init(&_dec,(unsigned char*)data,len);
2493 for (i=0;i<st->mode->nbEBands;i++)
2494 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2498 tell = ec_tell(dec);
2500 if (tell >= total_bits)
2503 silence = ec_dec_bit_logp(dec, 15);
2508 /* Pretend we've read all the remaining bits */
2510 dec->nbits_total+=tell-ec_tell(dec);
2513 postfilter_gain = 0;
2514 postfilter_pitch = 0;
2515 postfilter_tapset = 0;
2516 if (st->start==0 && tell+16 <= total_bits)
2518 if(ec_dec_bit_logp(dec, 1))
2521 octave = ec_dec_uint(dec, 6);
2522 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2523 qg = ec_dec_bits(dec, 3);
2524 if (ec_tell(dec)+2<=total_bits)
2525 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2526 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2528 tell = ec_tell(dec);
2531 if (LM > 0 && tell+3 <= total_bits)
2533 isTransient = ec_dec_bit_logp(dec, 3);
2534 tell = ec_tell(dec);
2544 /* Decode the global flags (first symbols in the stream) */
2545 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2546 /* Get band energies */
2547 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2548 intra_ener, dec, C, LM);
2550 ALLOC(tf_res, st->mode->nbEBands, int);
2551 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2553 tell = ec_tell(dec);
2554 spread_decision = SPREAD_NORMAL;
2555 if (tell+4 <= total_bits)
2556 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2558 ALLOC(pulses, st->mode->nbEBands, int);
2559 ALLOC(cap, st->mode->nbEBands, int);
2560 ALLOC(offsets, st->mode->nbEBands, int);
2561 ALLOC(fine_priority, st->mode->nbEBands, int);
2563 init_caps(st->mode,cap,LM,C);
2566 total_bits<<=BITRES;
2567 tell = ec_tell_frac(dec);
2568 for (i=st->start;i<st->end;i++)
2571 int dynalloc_loop_logp;
2573 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2574 /* quanta is 6 bits, but no more than 1 bit/sample
2575 and no less than 1/8 bit/sample */
2576 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2577 dynalloc_loop_logp = dynalloc_logp;
2579 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2582 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2583 tell = ec_tell_frac(dec);
2587 total_bits -= quanta;
2588 dynalloc_loop_logp = 1;
2591 /* Making dynalloc more likely */
2593 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2596 ALLOC(fine_quant, st->mode->nbEBands, int);
2597 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2598 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2600 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2601 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2602 bits -= anti_collapse_rsv;
2603 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2604 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2605 fine_quant, fine_priority, C, LM, dec, 0, 0);
2607 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2609 /* Decode fixed codebook */
2610 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2611 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2612 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2613 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2615 if (anti_collapse_rsv > 0)
2617 anti_collapse_on = ec_dec_bits(dec, 1);
2620 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2621 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2623 if (anti_collapse_on)
2624 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2625 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2627 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2631 for (i=0;i<C*st->mode->nbEBands;i++)
2634 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2638 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2640 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2642 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2645 for (i=0;i<M*st->mode->eBands[st->start];i++)
2649 int bound = M*st->mode->eBands[effEnd];
2650 if (st->downsample!=1)
2651 bound = IMIN(bound, N/st->downsample);
2652 for (i=bound;i<N;i++)
2656 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2658 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2663 freq[N+i] = freq[i];
2668 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2671 /* Compute inverse MDCTs */
2672 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2675 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2676 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2677 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2678 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2679 st->mode->window, st->overlap);
2681 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2682 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2683 st->mode->window, st->mode->overlap);
2686 st->postfilter_period_old = st->postfilter_period;
2687 st->postfilter_gain_old = st->postfilter_gain;
2688 st->postfilter_tapset_old = st->postfilter_tapset;
2689 st->postfilter_period = postfilter_pitch;
2690 st->postfilter_gain = postfilter_gain;
2691 st->postfilter_tapset = postfilter_tapset;
2694 st->postfilter_period_old = st->postfilter_period;
2695 st->postfilter_gain_old = st->postfilter_gain;
2696 st->postfilter_tapset_old = st->postfilter_tapset;
2700 for (i=0;i<st->mode->nbEBands;i++)
2701 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2704 /* In case start or end were to change */
2707 for (i=0;i<2*st->mode->nbEBands;i++)
2708 oldLogE2[i] = oldLogE[i];
2709 for (i=0;i<2*st->mode->nbEBands;i++)
2710 oldLogE[i] = oldBandE[i];
2711 for (i=0;i<2*st->mode->nbEBands;i++)
2712 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2714 for (i=0;i<2*st->mode->nbEBands;i++)
2715 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2719 for (i=0;i<st->start;i++)
2721 oldBandE[c*st->mode->nbEBands+i]=0;
2722 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2724 for (i=st->end;i<st->mode->nbEBands;i++)
2726 oldBandE[c*st->mode->nbEBands+i]=0;
2727 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2732 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2735 if (ec_tell(dec) > 8*len)
2736 return OPUS_INTERNAL_ERROR;
2737 if(ec_get_error(dec))
2739 return frame_size/st->downsample;
2746 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2748 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2751 #ifndef DISABLE_FLOAT_API
2752 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2755 VARDECL(opus_int16, out);
2759 return OPUS_BAD_ARG;
2764 ALLOC(out, C*N, opus_int16);
2765 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2767 for (j=0;j<C*ret;j++)
2768 pcm[j]=out[j]*(1.f/32768.f);
2773 #endif /* DISABLE_FLOAT_API */
2777 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2779 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2782 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2785 VARDECL(celt_sig, out);
2789 return OPUS_BAD_ARG;
2793 ALLOC(out, C*N, celt_sig);
2795 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2798 for (j=0;j<C*ret;j++)
2799 pcm[j] = FLOAT2INT16 (out[j]);
2806 #endif /* CUSTOM_MODES */
2808 int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...)
2812 va_start(ap, request);
2815 case CELT_SET_START_BAND_REQUEST:
2817 opus_int32 value = va_arg(ap, opus_int32);
2818 if (value<0 || value>=st->mode->nbEBands)
2823 case CELT_SET_END_BAND_REQUEST:
2825 opus_int32 value = va_arg(ap, opus_int32);
2826 if (value<1 || value>st->mode->nbEBands)
2831 case CELT_SET_CHANNELS_REQUEST:
2833 opus_int32 value = va_arg(ap, opus_int32);
2834 if (value<1 || value>2)
2836 st->stream_channels = value;
2839 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
2841 opus_int32 *value = va_arg(ap, opus_int32*);
2848 case OPUS_GET_LOOKAHEAD_REQUEST:
2850 opus_int32 *value = va_arg(ap, opus_int32*);
2853 *value = st->overlap/st->downsample;
2856 case OPUS_RESET_STATE:
2859 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
2860 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
2861 oldBandE = lpc+st->channels*LPC_ORDER;
2862 oldLogE = oldBandE + 2*st->mode->nbEBands;
2863 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2864 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
2865 opus_custom_decoder_get_size(st->mode, st->channels)-
2866 ((char*)&st->DECODER_RESET_START - (char*)st));
2867 for (i=0;i<2*st->mode->nbEBands;i++)
2868 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2871 case OPUS_GET_PITCH_REQUEST:
2873 opus_int32 *value = va_arg(ap, opus_int32*);
2876 *value = st->postfilter_period;
2880 case CELT_GET_MODE_REQUEST:
2882 const CELTMode ** value = va_arg(ap, const CELTMode**);
2888 case CELT_SET_SIGNALLING_REQUEST:
2890 opus_int32 value = va_arg(ap, opus_int32);
2891 st->signalling = value;
2894 case OPUS_GET_FINAL_RANGE_REQUEST:
2896 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2910 return OPUS_BAD_ARG;
2913 return OPUS_UNIMPLEMENTED;
2918 const char *opus_strerror(int error)
2920 static const char *error_strings[8] = {
2926 "request not implemented",
2928 "memory allocation failed"
2930 if (error > 0 || error < -7)
2931 return "unknown error";
2933 return error_strings[-error];
2936 const char *opus_get_version_string(void)
2938 return "libopus " OPUS_VERSION