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, AnalysisInfo *analysis)
802 opus_val16 sum = 0; /* Q10 */
803 /* Compute inter-channel correlation for low frequencies */
807 opus_val32 partial = 0;
808 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
809 partial = MAC16_16(partial, X[j], X[N0+j]);
810 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
812 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
813 /*printf ("%f\n", sum);*/
814 if (sum > QCONST16(.995f,10))
816 else if (sum > QCONST16(.92f,10))
818 else if (sum > QCONST16(.85f,10))
820 else if (sum > QCONST16(.8f,10))
824 /* Estimate spectral tilt */
826 for (i=0;i<end-1;i++)
828 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
831 /* We divide by two here to avoid making the tilt larger for stereo as a
832 result of a bug in the loop above */
834 /*printf("%f\n", diff);*/
835 if (diff > QCONST16(2.f, DB_SHIFT))
837 if (diff > QCONST16(8.f, DB_SHIFT))
839 if (diff < -QCONST16(4.f, DB_SHIFT))
841 if (diff < -QCONST16(10.f, DB_SHIFT))
844 if (0 && analysis->valid)
846 if (analysis->tonality_slope > .15)
848 if (analysis->tonality_slope > .3)
850 if (analysis->tonality_slope < -.15)
852 if (analysis->tonality_slope < -.3)
861 trim_index = rand()%11;
866 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
871 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
873 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
877 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
879 opus_val32 L, R, M, S;
880 /* We cast to 32-bit first because of the -32768 case */
882 R = EXTEND32(X[N0+j]);
885 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
886 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
889 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
891 /* We don't need thetas for lower bands with LM<=1 */
894 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
895 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
898 int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
903 VARDECL(celt_sig, in);
904 VARDECL(celt_sig, freq);
905 VARDECL(celt_norm, X);
906 VARDECL(celt_ener, bandE);
907 VARDECL(opus_val16, bandLogE);
908 VARDECL(int, fine_quant);
909 VARDECL(opus_val16, error);
910 VARDECL(int, pulses);
912 VARDECL(int, offsets);
913 VARDECL(int, fine_priority);
914 VARDECL(int, tf_res);
915 VARDECL(unsigned char, collapse_masks);
916 celt_sig *prefilter_mem;
917 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
920 const int CC = st->channels;
921 const int C = st->stream_channels;
924 int nbFilledBytes, nbAvailableBytes;
929 int pitch_index=COMBFILTER_MINPERIOD;
930 opus_val16 gain1 = 0;
934 opus_val16 pf_threshold;
937 opus_int32 total_bits;
938 opus_int32 total_boost;
941 int prefilter_tapset=0;
943 int anti_collapse_rsv;
944 int anti_collapse_on=0;
946 opus_val16 tf_estimate=0;
949 if (nbCompressedBytes<2 || pcm==NULL)
952 frame_size *= st->upsample;
953 for (LM=0;LM<=st->mode->maxLM;LM++)
954 if (st->mode->shortMdctSize<<LM==frame_size)
956 if (LM>st->mode->maxLM)
959 N = M*st->mode->shortMdctSize;
961 prefilter_mem = st->in_mem+CC*(st->overlap);
962 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
963 oldLogE = oldBandE + CC*st->mode->nbEBands;
964 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
972 nbFilledBytes=(tell+4)>>3;
976 if (st->signalling && enc==NULL)
978 int tmp = (st->mode->effEBands-st->end)>>1;
979 st->end = IMAX(1, st->mode->effEBands-tmp);
980 compressed[0] = tmp<<5;
981 compressed[0] |= LM<<3;
982 compressed[0] |= (C==2)<<2;
983 /* Convert "standard mode" to Opus header */
984 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
986 int c0 = toOpus(compressed[0]);
995 celt_assert(st->signalling==0);
998 /* Can't produce more than 1275 output bytes */
999 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1000 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1002 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1004 opus_int32 den=st->mode->Fs>>BITRES;
1005 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1008 vbr_rate -= 8<<BITRES;
1010 effectiveBytes = vbr_rate>>(3+BITRES);
1014 tmp = st->bitrate*frame_size;
1017 if (st->bitrate!=OPUS_BITRATE_MAX)
1018 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1019 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1020 effectiveBytes = nbCompressedBytes;
1025 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1031 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1032 target rate and buffering.
1033 We must do this up front so that bust-prevention logic triggers
1034 correctly if we don't have enough bits. */
1035 if (st->constrained_vbr)
1037 opus_int32 vbr_bound;
1038 opus_int32 max_allowed;
1039 /* We could use any multiple of vbr_rate as bound (depending on the
1041 This is clamped to ensure we use at least two bytes if the encoder
1042 was entirely empty, but to allow 0 in hybrid mode. */
1043 vbr_bound = vbr_rate;
1044 max_allowed = IMIN(IMAX(tell==1?2:0,
1045 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1047 if(max_allowed < nbAvailableBytes)
1049 nbCompressedBytes = nbFilledBytes+max_allowed;
1050 nbAvailableBytes = max_allowed;
1051 ec_enc_shrink(enc, nbCompressedBytes);
1055 total_bits = nbCompressedBytes*8;
1058 if (effEnd > st->mode->effEBands)
1059 effEnd = st->mode->effEBands;
1061 ALLOC(in, CC*(N+st->overlap), celt_sig);
1063 /* Find pitch period and gain */
1065 VARDECL(celt_sig, _pre);
1068 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1071 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1076 const opus_val16 * restrict pcmp = pcm+c;
1077 celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
1088 x = MAX32(-65536.f, MIN32(65536.f,x));
1090 if (++count==st->upsample)
1097 /* Apply pre-emphasis */
1098 tmp = MULT16_16(st->mode->preemph[2], x);
1099 *inp = tmp + st->preemph_memE[c];
1100 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1101 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1102 silence = silence && *inp == 0;
1105 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1106 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1110 if ((rand()&0x3F)==0)
1114 ec_enc_bit_logp(enc, silence, 15);
1119 /*In VBR mode there is no need to send more than the minimum. */
1122 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1123 total_bits=nbCompressedBytes*8;
1125 ec_enc_shrink(enc, nbCompressedBytes);
1127 /* Pretend we've filled all the remaining bits with zeros
1128 (that's what the initialiser did anyway) */
1129 tell = nbCompressedBytes*8;
1130 enc->nbits_total+=tell-ec_tell(enc);
1132 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1134 VARDECL(opus_val16, pitch_buf);
1135 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1137 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1138 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1139 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1140 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1142 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1143 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1144 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1145 pitch_index = COMBFILTER_MAXPERIOD-2;
1146 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1147 if (st->loss_rate>2)
1148 gain1 = HALF32(gain1);
1149 if (st->loss_rate>4)
1150 gain1 = HALF32(gain1);
1151 if (st->loss_rate>8)
1153 prefilter_tapset = st->tapset_decision;
1158 /* Gain threshold for enabling the prefilter/postfilter */
1159 pf_threshold = QCONST16(.2f,15);
1161 /* Adjusting the threshold based on rate and continuity */
1162 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1163 pf_threshold += QCONST16(.2f,15);
1164 if (nbAvailableBytes<25)
1165 pf_threshold += QCONST16(.1f,15);
1166 if (nbAvailableBytes<35)
1167 pf_threshold += QCONST16(.1f,15);
1168 if (st->prefilter_gain > QCONST16(.4f,15))
1169 pf_threshold -= QCONST16(.1f,15);
1170 if (st->prefilter_gain > QCONST16(.55f,15))
1171 pf_threshold -= QCONST16(.1f,15);
1173 /* Hard threshold at 0.2 */
1174 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1175 if (gain1<pf_threshold)
1177 if(st->start==0 && tell+16<=total_bits)
1178 ec_enc_bit_logp(enc, 0, 1);
1182 /*This block is not gated by a total bits check only because
1183 of the nbAvailableBytes check above.*/
1187 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1188 gain1=st->prefilter_gain;
1191 qg = ((gain1+1536)>>10)/3-1;
1193 qg = (int)floor(.5f+gain1*32/3)-1;
1195 qg = IMAX(0, IMIN(7, qg));
1196 ec_enc_bit_logp(enc, 1, 1);
1198 octave = EC_ILOG(pitch_index)-5;
1199 ec_enc_uint(enc, octave, 6);
1200 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1202 ec_enc_bits(enc, qg, 3);
1203 if (ec_tell(enc)+2<=total_bits)
1204 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1206 prefilter_tapset = 0;
1207 gain1 = QCONST16(0.09375f,15)*(qg+1);
1210 /*printf("%d %f\n", pitch_index, gain1);*/
1213 int offset = st->mode->shortMdctSize-st->mode->overlap;
1214 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1215 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1217 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1218 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1219 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1221 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1222 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1223 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1224 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1226 if (N>COMBFILTER_MAXPERIOD)
1228 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1230 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1231 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1240 if (LM>0 && ec_tell(enc)+3<=total_bits)
1242 if (st->complexity > 1)
1244 isTransient = transient_analysis(in, N+st->overlap, CC,
1245 st->overlap, &tf_estimate);
1249 ec_enc_bit_logp(enc, isTransient, 3);
1252 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1253 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1254 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1256 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1261 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1263 if (st->upsample != 1)
1267 int bound = N/st->upsample;
1268 for (i=0;i<bound;i++)
1269 freq[c*N+i] *= st->upsample;
1274 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1276 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1278 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1280 /* Band normalisation */
1281 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1283 ALLOC(tf_res, st->mode->nbEBands, int);
1284 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1285 for (i=effEnd;i<st->end;i++)
1286 tf_res[i] = tf_res[effEnd-1];
1288 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1289 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1290 oldBandE, total_bits, error, enc,
1291 C, LM, nbAvailableBytes, st->force_intra,
1292 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1294 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1296 st->spread_decision = SPREAD_NORMAL;
1297 if (ec_tell(enc)+4<=total_bits)
1299 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1301 if (st->complexity == 0)
1302 st->spread_decision = SPREAD_NONE;
1304 st->spread_decision = spreading_decision(st->mode, X,
1305 &st->tonal_average, st->spread_decision, &st->hf_average,
1306 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1307 /*printf("%f %d\n", st->analysis.tonality_slope, st->tapset_decision);*/
1308 /*if (st->frame_tonality > .7*32768)
1309 st->spread_decision = SPREAD_NONE;
1310 else if (st->frame_tonality > .3*32768)
1311 st->spread_decision = SPREAD_LIGHT;
1312 else if (st->frame_tonality > .1*32768)
1313 st->spread_decision = SPREAD_NORMAL;
1315 st->spread_decision = SPREAD_AGGRESSIVE;*/
1317 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1320 ALLOC(cap, st->mode->nbEBands, int);
1321 ALLOC(offsets, st->mode->nbEBands, int);
1323 init_caps(st->mode,cap,LM,C);
1324 for (i=0;i<st->mode->nbEBands;i++)
1326 /* Dynamic allocation code */
1327 /* Make sure that dynamic allocation can't make us bust the budget */
1328 if (effectiveBytes > 50 && LM>=1)
1339 for (i=st->start+1;i<st->end-1;i++)
1342 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1344 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1345 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1351 offsets[i] += 1+(rand()&0x3);
1354 if (d2 > SHL16(t1,DB_SHIFT))
1356 if (d2 > SHL16(t2,DB_SHIFT))
1362 if (0 && st->analysis.valid)
1364 if (st->analysis.boost_amount[0]>.2)
1365 offsets[st->analysis.boost_band[0]]+=2;
1366 if (st->analysis.boost_amount[0]>.4)
1367 offsets[st->analysis.boost_band[0]]+=2;
1368 if (st->analysis.boost_amount[1]>.2)
1369 offsets[st->analysis.boost_band[1]]+=2;
1370 if (st->analysis.boost_amount[1]>.4)
1371 offsets[st->analysis.boost_band[1]]+=2;
1375 total_bits<<=BITRES;
1377 tell = ec_tell_frac(enc);
1378 for (i=st->start;i<st->end;i++)
1381 int dynalloc_loop_logp;
1384 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1385 /* quanta is 6 bits, but no more than 1 bit/sample
1386 and no less than 1/8 bit/sample */
1387 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1388 dynalloc_loop_logp = dynalloc_logp;
1390 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1391 && boost < cap[i]; j++)
1394 flag = j<offsets[i];
1395 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1396 tell = ec_tell_frac(enc);
1400 total_boost += quanta;
1401 dynalloc_loop_logp = 1;
1403 /* Making dynalloc more likely */
1405 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1409 if (tell+(6<<BITRES) <= total_bits - total_boost)
1411 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1412 st->end, LM, C, N, &st->analysis);
1413 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1414 tell = ec_tell_frac(enc);
1421 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1423 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1425 /* Account for coarse energy */
1426 effectiveRate = (8*effectiveBytes - 80)>>LM;
1428 /* effectiveRate in kb/s */
1429 effectiveRate = 2*effectiveRate/5;
1430 if (effectiveRate<35)
1432 else if (effectiveRate<50)
1434 else if (effectiveRate<68)
1436 else if (effectiveRate<84)
1438 else if (effectiveRate<102)
1440 else if (effectiveRate<130)
1444 intensity = IMIN(st->end,IMAX(st->start, intensity));
1447 /* Variable bitrate */
1452 /* The target rate in 8th bits per frame */
1453 opus_int32 target, new_target;
1454 opus_int32 min_allowed;
1457 int lm_diff = st->mode->maxLM - LM;
1458 coded_bands = st->lastCodedBands ? st->lastCodedBands : st->mode->nbEBands;
1459 coded_bins = st->mode->eBands[coded_bands]<<LM;
1461 coded_bins += st->mode->eBands[IMIN(intensity, coded_bands)]<<LM;
1463 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1464 The CELT allocator will just not be able to use more than that anyway. */
1465 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1466 target = vbr_rate - ((40*C+20)<<BITRES);
1467 if (st->constrained_vbr)
1468 target += (st->vbr_offset>>lm_diff);
1471 if (st->analysis.valid && st->analysis.activity<.4)
1472 target -= (coded_bins<<BITRES)*2*(.4-st->analysis.activity);
1476 new_target = SHL32(MULT16_32_Q15(target, SUB16(tf_estimate, QCONST16(0.05, 14))),1);
1478 new_target = target*(tf_estimate-.05);
1482 if (st->analysis.valid) {
1485 tonal = st->analysis.tonality;
1487 tonal_target = target + (coded_bins<<BITRES)*1.55*tonal;
1488 new_target = IMAX(tonal_target,new_target);
1492 /* The current offset is removed from the target and the space used
1494 target=new_target+tell;
1495 /* In VBR mode the frame size must not be reduced so much that it would
1496 result in the encoder running out of bits.
1497 The margin of 2 bytes ensures that none of the bust-prevention logic
1498 in the decoder will have triggered so far. */
1499 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1501 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1502 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1503 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1505 /* By how much did we "miss" the target on that frame */
1506 delta = target - vbr_rate;
1508 target=nbAvailableBytes<<(BITRES+3);
1510 /*If the frame is silent we don't adjust our drift, otherwise
1511 the encoder will shoot to very high rates after hitting a
1512 span of silence, but we do allow the bitres to refill.
1513 This means that we'll undershoot our target in CVBR/VBR modes
1514 on files with lots of silence. */
1517 nbAvailableBytes = 2;
1518 target = 2*8<<BITRES;
1522 if (st->vbr_count < 970)
1525 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1527 alpha = QCONST16(.001f,15);
1528 /* How many bits have we used in excess of what we're allowed */
1529 if (st->constrained_vbr)
1530 st->vbr_reservoir += target - vbr_rate;
1531 /*printf ("%d\n", st->vbr_reservoir);*/
1533 /* Compute the offset we need to apply in order to reach the target */
1534 if (st->constrained_vbr)
1536 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1537 st->vbr_offset = -st->vbr_drift;
1539 /*printf ("%d\n", st->vbr_drift);*/
1541 if (st->constrained_vbr && st->vbr_reservoir < 0)
1543 /* We're under the min value -- increase rate */
1544 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1545 /* Unless we're just coding silence */
1546 nbAvailableBytes += silence?0:adjust;
1547 st->vbr_reservoir = 0;
1548 /*printf ("+%d\n", adjust);*/
1550 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1551 /* This moves the raw bits to take into account the new compressed size */
1552 ec_enc_shrink(enc, nbCompressedBytes);
1555 /* Bit allocation */
1556 ALLOC(fine_quant, st->mode->nbEBands, int);
1557 ALLOC(pulses, st->mode->nbEBands, int);
1558 ALLOC(fine_priority, st->mode->nbEBands, int);
1560 /* bits = packet size - where we are - safety*/
1561 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1562 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1563 bits -= anti_collapse_rsv;
1564 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1565 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1566 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1567 st->lastCodedBands = codedBands;
1569 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1571 #ifdef MEASURE_NORM_MSE
1576 X0[i+c*N] = X[i+c*N];
1578 for (i=0;i<C*st->mode->nbEBands;i++)
1579 bandE0[i] = bandE[i];
1582 /* Residual quantisation */
1583 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1584 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1585 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1586 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1588 if (anti_collapse_rsv > 0)
1590 anti_collapse_on = st->consec_transient<2;
1592 anti_collapse_on = rand()&0x1;
1594 ec_enc_bits(enc, anti_collapse_on, 1);
1596 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1600 for (i=0;i<C*st->mode->nbEBands;i++)
1601 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1605 /* Re-synthesis of the coded audio if required */
1607 celt_sig *out_mem[2];
1608 celt_sig *overlap_mem[2];
1610 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1613 for (i=0;i<C*st->mode->nbEBands;i++)
1617 #ifdef MEASURE_NORM_MSE
1618 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1620 if (anti_collapse_on)
1622 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1623 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1627 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1629 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1631 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1634 for (i=0;i<M*st->mode->eBands[st->start];i++)
1638 for (i=M*st->mode->eBands[st->end];i<N;i++)
1645 freq[N+i] = freq[i];
1648 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1650 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1652 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1654 overlap_mem[1] = overlap_mem[0] + st->overlap;
1656 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1659 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1660 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1661 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1662 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1663 st->mode->window, st->overlap);
1665 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1666 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1667 st->mode->window, st->mode->overlap);
1670 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1671 st->prefilter_period_old = st->prefilter_period;
1672 st->prefilter_gain_old = st->prefilter_gain;
1673 st->prefilter_tapset_old = st->prefilter_tapset;
1677 st->prefilter_period = pitch_index;
1678 st->prefilter_gain = gain1;
1679 st->prefilter_tapset = prefilter_tapset;
1683 st->prefilter_period_old = st->prefilter_period;
1684 st->prefilter_gain_old = st->prefilter_gain;
1685 st->prefilter_tapset_old = st->prefilter_tapset;
1690 for (i=0;i<st->mode->nbEBands;i++)
1691 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1696 for (i=0;i<CC*st->mode->nbEBands;i++)
1697 oldLogE2[i] = oldLogE[i];
1698 for (i=0;i<CC*st->mode->nbEBands;i++)
1699 oldLogE[i] = oldBandE[i];
1701 for (i=0;i<CC*st->mode->nbEBands;i++)
1702 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1704 /* In case start or end were to change */
1707 for (i=0;i<st->start;i++)
1709 oldBandE[c*st->mode->nbEBands+i]=0;
1710 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1712 for (i=st->end;i<st->mode->nbEBands;i++)
1714 oldBandE[c*st->mode->nbEBands+i]=0;
1715 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1720 st->consec_transient++;
1722 st->consec_transient=0;
1725 /* If there's any room left (can only happen for very high rates),
1726 it's already filled with zeros */
1731 nbCompressedBytes++;
1735 if (ec_get_error(enc))
1736 return OPUS_INTERNAL_ERROR;
1738 return nbCompressedBytes;
1745 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1747 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1750 #ifndef DISABLE_FLOAT_API
1751 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1754 VARDECL(opus_int16, in);
1758 return OPUS_BAD_ARG;
1762 ALLOC(in, C*N, opus_int16);
1765 in[j] = FLOAT2INT16(pcm[j]);
1767 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1770 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1775 #endif /* DISABLE_FLOAT_API */
1778 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1781 VARDECL(celt_sig, in);
1785 return OPUS_BAD_ARG;
1789 ALLOC(in, C*N, celt_sig);
1790 for (j=0;j<C*N;j++) {
1791 in[j] = SCALEOUT(pcm[j]);
1794 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1797 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1803 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1805 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1810 #endif /* CUSTOM_MODES */
1812 int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1816 va_start(ap, request);
1819 case OPUS_SET_COMPLEXITY_REQUEST:
1821 int value = va_arg(ap, opus_int32);
1822 if (value<0 || value>10)
1824 st->complexity = value;
1827 case CELT_SET_START_BAND_REQUEST:
1829 opus_int32 value = va_arg(ap, opus_int32);
1830 if (value<0 || value>=st->mode->nbEBands)
1835 case CELT_SET_END_BAND_REQUEST:
1837 opus_int32 value = va_arg(ap, opus_int32);
1838 if (value<1 || value>st->mode->nbEBands)
1843 case CELT_SET_PREDICTION_REQUEST:
1845 int value = va_arg(ap, opus_int32);
1846 if (value<0 || value>2)
1848 st->disable_pf = value<=1;
1849 st->force_intra = value==0;
1852 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1854 int value = va_arg(ap, opus_int32);
1855 if (value<0 || value>100)
1857 st->loss_rate = value;
1860 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
1862 opus_int32 value = va_arg(ap, opus_int32);
1863 st->constrained_vbr = value;
1866 case OPUS_SET_VBR_REQUEST:
1868 opus_int32 value = va_arg(ap, opus_int32);
1872 case OPUS_SET_BITRATE_REQUEST:
1874 opus_int32 value = va_arg(ap, opus_int32);
1875 if (value<=500 && value!=OPUS_BITRATE_MAX)
1877 value = IMIN(value, 260000*st->channels);
1878 st->bitrate = value;
1881 case CELT_SET_CHANNELS_REQUEST:
1883 opus_int32 value = va_arg(ap, opus_int32);
1884 if (value<1 || value>2)
1886 st->stream_channels = value;
1889 case OPUS_RESET_STATE:
1892 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1893 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
1894 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
1895 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
1896 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
1897 opus_custom_encoder_get_size(st->mode, st->channels)-
1898 ((char*)&st->ENCODER_RESET_START - (char*)st));
1899 for (i=0;i<st->channels*st->mode->nbEBands;i++)
1900 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
1902 st->delayedIntra = 1;
1903 st->spread_decision = SPREAD_NORMAL;
1904 st->tonal_average = 256;
1906 st->tapset_decision = 0;
1910 case CELT_SET_INPUT_CLIPPING_REQUEST:
1912 opus_int32 value = va_arg(ap, opus_int32);
1917 case CELT_SET_SIGNALLING_REQUEST:
1919 opus_int32 value = va_arg(ap, opus_int32);
1920 st->signalling = value;
1923 case CELT_SET_ANALYSIS_REQUEST:
1925 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
1927 OPUS_COPY(&st->analysis, info, 1);
1930 case CELT_GET_MODE_REQUEST:
1932 const CELTMode ** value = va_arg(ap, const CELTMode**);
1938 case OPUS_GET_FINAL_RANGE_REQUEST:
1940 opus_uint32 * value = va_arg(ap, opus_uint32 *);
1953 return OPUS_BAD_ARG;
1956 return OPUS_UNIMPLEMENTED;
1959 /**********************************************************************/
1963 /**********************************************************************/
1964 #define DECODE_BUFFER_SIZE 2048
1967 @brief Decoder state
1969 struct OpusCustomDecoder {
1970 const OpusCustomMode *mode;
1973 int stream_channels;
1979 /* Everything beyond this point gets cleared on a reset */
1980 #define DECODER_RESET_START rng
1984 int last_pitch_index;
1986 int postfilter_period;
1987 int postfilter_period_old;
1988 opus_val16 postfilter_gain;
1989 opus_val16 postfilter_gain_old;
1990 int postfilter_tapset;
1991 int postfilter_tapset_old;
1993 celt_sig preemph_memD[2];
1995 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
1996 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
1997 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
1998 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
1999 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
2000 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
2003 int celt_decoder_get_size(int channels)
2005 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
2006 return opus_custom_decoder_get_size(mode, channels);
2009 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
2011 int size = sizeof(struct CELTDecoder)
2012 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
2013 + channels*LPC_ORDER*sizeof(opus_val16)
2014 + 4*2*mode->nbEBands*sizeof(opus_val16);
2019 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
2022 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
2023 ret = opus_custom_decoder_init(st, mode, channels);
2026 opus_custom_decoder_destroy(st);
2033 #endif /* CUSTOM_MODES */
2035 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
2038 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
2041 st->downsample = resampling_factor(sampling_rate);
2042 if (st->downsample==0)
2043 return OPUS_BAD_ARG;
2048 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
2050 if (channels < 0 || channels > 2)
2051 return OPUS_BAD_ARG;
2054 return OPUS_ALLOC_FAIL;
2056 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2059 st->overlap = mode->overlap;
2060 st->stream_channels = st->channels = channels;
2064 st->end = st->mode->effEBands;
2069 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2075 void opus_custom_decoder_destroy(CELTDecoder *st)
2079 #endif /* CUSTOM_MODES */
2081 static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)
2085 int overlap = st->mode->overlap;
2086 opus_val16 fade = Q15ONE;
2088 const int C = st->channels;
2090 celt_sig *out_mem[2];
2091 celt_sig *decode_mem[2];
2092 celt_sig *overlap_mem[2];
2094 opus_val32 *out_syn[2];
2095 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2099 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2100 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2101 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2103 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2104 oldBandE = lpc+C*LPC_ORDER;
2105 oldLogE = oldBandE + 2*st->mode->nbEBands;
2106 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2107 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2109 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2111 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2113 len = N+st->mode->overlap;
2115 if (st->loss_count >= 5 || st->start!=0)
2117 /* Noise-based PLC/CNG */
2118 VARDECL(celt_sig, freq);
2119 VARDECL(celt_norm, X);
2120 VARDECL(celt_ener, bandE);
2125 if (effEnd > st->mode->effEBands)
2126 effEnd = st->mode->effEBands;
2128 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2129 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2130 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2132 if (st->loss_count >= 5)
2133 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2136 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2139 for (i=st->start;i<st->end;i++)
2140 oldBandE[c*st->mode->nbEBands+i] -= decay;
2142 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2147 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2149 for (i=st->start;i<st->mode->effEBands;i++)
2154 boffs = N*c+(st->mode->eBands[i]<<LM);
2155 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2156 for (j=0;j<blen;j++)
2158 seed = celt_lcg_rand(seed);
2159 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2161 renormalise_vector(X+boffs, blen, Q15ONE);
2163 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2168 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2171 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2175 int bound = st->mode->eBands[effEnd]<<LM;
2176 if (st->downsample!=1)
2177 bound = IMIN(bound, N/st->downsample);
2178 for (i=bound;i<N;i++)
2181 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2183 /* Pitch-based PLC */
2184 if (st->loss_count == 0)
2186 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2187 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2188 search by using only part of the decode buffer */
2190 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2191 /* Max pitch is 100 samples (480 Hz) */
2192 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2193 poffset-100, &pitch_index);
2194 pitch_index = poffset-pitch_index;
2195 st->last_pitch_index = pitch_index;
2197 pitch_index = st->last_pitch_index;
2198 fade = QCONST16(.8f,15);
2202 VARDECL(opus_val32, e);
2203 opus_val16 exc[MAX_PERIOD];
2204 opus_val32 ac[LPC_ORDER+1];
2205 opus_val16 decay = 1;
2207 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};
2209 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2211 offset = MAX_PERIOD-pitch_index;
2212 for (i=0;i<MAX_PERIOD;i++)
2213 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2215 if (st->loss_count == 0)
2217 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2218 LPC_ORDER, MAX_PERIOD);
2220 /* Noise floor -40 dB */
2222 ac[0] += SHR32(ac[0],13);
2227 for (i=1;i<=LPC_ORDER;i++)
2229 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2231 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2233 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2237 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2239 for (i=0;i<LPC_ORDER;i++)
2240 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2241 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2242 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2243 /* Check if the waveform is decaying (and if so how fast) */
2245 opus_val32 E1=1, E2=1;
2247 if (pitch_index <= MAX_PERIOD/2)
2248 period = pitch_index;
2250 period = MAX_PERIOD/2;
2251 for (i=0;i<period;i++)
2253 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2254 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2258 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2261 /* Copy excitation, taking decay into account */
2262 for (i=0;i<len+st->mode->overlap;i++)
2265 if (offset+i >= MAX_PERIOD)
2267 offset -= pitch_index;
2268 decay = MULT16_16_Q15(decay, decay);
2270 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2271 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2272 S1 += SHR32(MULT16_16(tmp,tmp),8);
2274 for (i=0;i<LPC_ORDER;i++)
2275 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2276 for (i=0;i<len+st->mode->overlap;i++)
2277 e[i] = MULT16_32_Q15(fade, e[i]);
2278 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2282 for (i=0;i<len+overlap;i++)
2284 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2285 S2 += SHR32(MULT16_16(tmp,tmp),8);
2287 /* This checks for an "explosion" in the synthesis */
2289 if (!(S1 > SHR32(S2,2)))
2291 /* Float test is written this way to catch NaNs at the same time */
2292 if (!(S1 > 0.2f*S2))
2295 for (i=0;i<len+overlap;i++)
2299 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2300 for (i=0;i<len+overlap;i++)
2301 e[i] = MULT16_32_Q15(ratio, e[i]);
2305 /* Apply post-filter to the MDCT overlap of the previous frame */
2306 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2307 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2310 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2311 out_mem[c][i] = out_mem[c][N+i];
2313 /* Apply TDAC to the concealed audio so that it blends with the
2314 previous and next frames */
2315 for (i=0;i<overlap/2;i++)
2318 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2319 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2320 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2321 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2324 out_mem[c][MAX_PERIOD-N+i] = e[i];
2326 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2327 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2328 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2330 for (i=0;i<overlap;i++)
2331 out_mem[c][MAX_PERIOD+i] = e[i];
2335 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2342 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec)
2345 int spread_decision;
2348 VARDECL(celt_sig, freq);
2349 VARDECL(celt_norm, X);
2350 VARDECL(celt_ener, bandE);
2351 VARDECL(int, fine_quant);
2352 VARDECL(int, pulses);
2354 VARDECL(int, offsets);
2355 VARDECL(int, fine_priority);
2356 VARDECL(int, tf_res);
2357 VARDECL(unsigned char, collapse_masks);
2358 celt_sig *out_mem[2];
2359 celt_sig *decode_mem[2];
2360 celt_sig *overlap_mem[2];
2361 celt_sig *out_syn[2];
2363 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2368 const int CC = st->channels;
2373 int postfilter_pitch;
2374 opus_val16 postfilter_gain;
2377 opus_int32 total_bits;
2381 int postfilter_tapset;
2382 int anti_collapse_rsv;
2383 int anti_collapse_on=0;
2385 int C = st->stream_channels;
2388 frame_size *= st->downsample;
2391 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2392 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2393 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2395 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2396 oldBandE = lpc+CC*LPC_ORDER;
2397 oldLogE = oldBandE + 2*st->mode->nbEBands;
2398 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2399 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2402 if (st->signalling && data!=NULL)
2405 /* Convert "standard mode" to Opus header */
2406 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2408 data0 = fromOpus(data0);
2410 return OPUS_INVALID_PACKET;
2412 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2413 LM = (data0>>3)&0x3;
2414 C = 1 + ((data0>>2)&0x1);
2417 if (LM>st->mode->maxLM)
2418 return OPUS_INVALID_PACKET;
2419 if (frame_size < st->mode->shortMdctSize<<LM)
2420 return OPUS_BUFFER_TOO_SMALL;
2422 frame_size = st->mode->shortMdctSize<<LM;
2427 for (LM=0;LM<=st->mode->maxLM;LM++)
2428 if (st->mode->shortMdctSize<<LM==frame_size)
2430 if (LM>st->mode->maxLM)
2431 return OPUS_BAD_ARG;
2435 if (len<0 || len>1275 || pcm==NULL)
2436 return OPUS_BAD_ARG;
2438 N = M*st->mode->shortMdctSize;
2441 if (effEnd > st->mode->effEBands)
2442 effEnd = st->mode->effEBands;
2444 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2445 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2446 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2448 for (i=0;i<M*st->mode->eBands[st->start];i++)
2452 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2456 if (data == NULL || len<=1)
2458 celt_decode_lost(st, pcm, N, LM);
2460 return frame_size/st->downsample;
2465 ec_dec_init(&_dec,(unsigned char*)data,len);
2471 for (i=0;i<st->mode->nbEBands;i++)
2472 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2476 tell = ec_tell(dec);
2478 if (tell >= total_bits)
2481 silence = ec_dec_bit_logp(dec, 15);
2486 /* Pretend we've read all the remaining bits */
2488 dec->nbits_total+=tell-ec_tell(dec);
2491 postfilter_gain = 0;
2492 postfilter_pitch = 0;
2493 postfilter_tapset = 0;
2494 if (st->start==0 && tell+16 <= total_bits)
2496 if(ec_dec_bit_logp(dec, 1))
2499 octave = ec_dec_uint(dec, 6);
2500 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2501 qg = ec_dec_bits(dec, 3);
2502 if (ec_tell(dec)+2<=total_bits)
2503 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2504 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2506 tell = ec_tell(dec);
2509 if (LM > 0 && tell+3 <= total_bits)
2511 isTransient = ec_dec_bit_logp(dec, 3);
2512 tell = ec_tell(dec);
2522 /* Decode the global flags (first symbols in the stream) */
2523 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2524 /* Get band energies */
2525 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2526 intra_ener, dec, C, LM);
2528 ALLOC(tf_res, st->mode->nbEBands, int);
2529 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2531 tell = ec_tell(dec);
2532 spread_decision = SPREAD_NORMAL;
2533 if (tell+4 <= total_bits)
2534 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2536 ALLOC(pulses, st->mode->nbEBands, int);
2537 ALLOC(cap, st->mode->nbEBands, int);
2538 ALLOC(offsets, st->mode->nbEBands, int);
2539 ALLOC(fine_priority, st->mode->nbEBands, int);
2541 init_caps(st->mode,cap,LM,C);
2544 total_bits<<=BITRES;
2545 tell = ec_tell_frac(dec);
2546 for (i=st->start;i<st->end;i++)
2549 int dynalloc_loop_logp;
2551 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2552 /* quanta is 6 bits, but no more than 1 bit/sample
2553 and no less than 1/8 bit/sample */
2554 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2555 dynalloc_loop_logp = dynalloc_logp;
2557 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2560 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2561 tell = ec_tell_frac(dec);
2565 total_bits -= quanta;
2566 dynalloc_loop_logp = 1;
2569 /* Making dynalloc more likely */
2571 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2574 ALLOC(fine_quant, st->mode->nbEBands, int);
2575 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2576 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2578 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2579 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2580 bits -= anti_collapse_rsv;
2581 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2582 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2583 fine_quant, fine_priority, C, LM, dec, 0, 0);
2585 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2587 /* Decode fixed codebook */
2588 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2589 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2590 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2591 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2593 if (anti_collapse_rsv > 0)
2595 anti_collapse_on = ec_dec_bits(dec, 1);
2598 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2599 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2601 if (anti_collapse_on)
2602 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2603 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2605 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2609 for (i=0;i<C*st->mode->nbEBands;i++)
2612 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2616 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2618 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2620 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2623 for (i=0;i<M*st->mode->eBands[st->start];i++)
2627 int bound = M*st->mode->eBands[effEnd];
2628 if (st->downsample!=1)
2629 bound = IMIN(bound, N/st->downsample);
2630 for (i=bound;i<N;i++)
2634 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2636 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2641 freq[N+i] = freq[i];
2646 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2649 /* Compute inverse MDCTs */
2650 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2653 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2654 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2655 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2656 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2657 st->mode->window, st->overlap);
2659 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2660 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2661 st->mode->window, st->mode->overlap);
2664 st->postfilter_period_old = st->postfilter_period;
2665 st->postfilter_gain_old = st->postfilter_gain;
2666 st->postfilter_tapset_old = st->postfilter_tapset;
2667 st->postfilter_period = postfilter_pitch;
2668 st->postfilter_gain = postfilter_gain;
2669 st->postfilter_tapset = postfilter_tapset;
2672 st->postfilter_period_old = st->postfilter_period;
2673 st->postfilter_gain_old = st->postfilter_gain;
2674 st->postfilter_tapset_old = st->postfilter_tapset;
2678 for (i=0;i<st->mode->nbEBands;i++)
2679 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2682 /* In case start or end were to change */
2685 for (i=0;i<2*st->mode->nbEBands;i++)
2686 oldLogE2[i] = oldLogE[i];
2687 for (i=0;i<2*st->mode->nbEBands;i++)
2688 oldLogE[i] = oldBandE[i];
2689 for (i=0;i<2*st->mode->nbEBands;i++)
2690 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2692 for (i=0;i<2*st->mode->nbEBands;i++)
2693 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2697 for (i=0;i<st->start;i++)
2699 oldBandE[c*st->mode->nbEBands+i]=0;
2700 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2702 for (i=st->end;i<st->mode->nbEBands;i++)
2704 oldBandE[c*st->mode->nbEBands+i]=0;
2705 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2710 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2713 if (ec_tell(dec) > 8*len)
2714 return OPUS_INTERNAL_ERROR;
2715 if(ec_get_error(dec))
2717 return frame_size/st->downsample;
2724 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2726 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2729 #ifndef DISABLE_FLOAT_API
2730 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2733 VARDECL(opus_int16, out);
2737 return OPUS_BAD_ARG;
2742 ALLOC(out, C*N, opus_int16);
2743 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2745 for (j=0;j<C*ret;j++)
2746 pcm[j]=out[j]*(1.f/32768.f);
2751 #endif /* DISABLE_FLOAT_API */
2755 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2757 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2760 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2763 VARDECL(celt_sig, out);
2767 return OPUS_BAD_ARG;
2771 ALLOC(out, C*N, celt_sig);
2773 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2776 for (j=0;j<C*ret;j++)
2777 pcm[j] = FLOAT2INT16 (out[j]);
2784 #endif /* CUSTOM_MODES */
2786 int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...)
2790 va_start(ap, request);
2793 case CELT_SET_START_BAND_REQUEST:
2795 opus_int32 value = va_arg(ap, opus_int32);
2796 if (value<0 || value>=st->mode->nbEBands)
2801 case CELT_SET_END_BAND_REQUEST:
2803 opus_int32 value = va_arg(ap, opus_int32);
2804 if (value<1 || value>st->mode->nbEBands)
2809 case CELT_SET_CHANNELS_REQUEST:
2811 opus_int32 value = va_arg(ap, opus_int32);
2812 if (value<1 || value>2)
2814 st->stream_channels = value;
2817 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
2819 opus_int32 *value = va_arg(ap, opus_int32*);
2826 case OPUS_GET_LOOKAHEAD_REQUEST:
2828 opus_int32 *value = va_arg(ap, opus_int32*);
2831 *value = st->overlap/st->downsample;
2834 case OPUS_RESET_STATE:
2837 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
2838 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
2839 oldBandE = lpc+st->channels*LPC_ORDER;
2840 oldLogE = oldBandE + 2*st->mode->nbEBands;
2841 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2842 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
2843 opus_custom_decoder_get_size(st->mode, st->channels)-
2844 ((char*)&st->DECODER_RESET_START - (char*)st));
2845 for (i=0;i<2*st->mode->nbEBands;i++)
2846 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2849 case OPUS_GET_PITCH_REQUEST:
2851 opus_int32 *value = va_arg(ap, opus_int32*);
2854 *value = st->postfilter_period;
2858 case CELT_GET_MODE_REQUEST:
2860 const CELTMode ** value = va_arg(ap, const CELTMode**);
2866 case CELT_SET_SIGNALLING_REQUEST:
2868 opus_int32 value = va_arg(ap, opus_int32);
2869 st->signalling = value;
2872 case OPUS_GET_FINAL_RANGE_REQUEST:
2874 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2888 return OPUS_BAD_ARG;
2891 return OPUS_UNIMPLEMENTED;
2896 const char *opus_strerror(int error)
2898 static const char *error_strings[8] = {
2904 "request not implemented",
2906 "memory allocation failed"
2908 if (error > 0 || error < -7)
2909 return "unknown error";
2911 return error_strings[-error];
2914 const char *opus_get_version_string(void)
2916 return "libopus " OPUS_VERSION