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 */
161 /* Everything beyond this point gets cleared on a reset */
162 #define ENCODER_RESET_START rng
166 opus_val32 delayedIntra;
172 int prefilter_period;
173 opus_val16 prefilter_gain;
174 int prefilter_tapset;
176 int prefilter_period_old;
177 opus_val16 prefilter_gain_old;
178 int prefilter_tapset_old;
180 int consec_transient;
181 AnalysisInfo analysis;
183 opus_val32 preemph_memE[2];
184 opus_val32 preemph_memD[2];
186 /* VBR-related parameters */
187 opus_int32 vbr_reservoir;
188 opus_int32 vbr_drift;
189 opus_int32 vbr_offset;
190 opus_int32 vbr_count;
191 opus_val16 overlap_max;
192 opus_val16 stereo_saving;
196 celt_sig syn_mem[2][2*MAX_PERIOD];
199 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
200 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */
201 /* opus_val16 oldBandE[], Size = channels*mode->nbEBands */
202 /* opus_val16 oldLogE[], Size = channels*mode->nbEBands */
203 /* opus_val16 oldLogE2[], Size = channels*mode->nbEBands */
205 /* opus_val16 overlap_mem[], Size = channels*overlap */
209 int celt_encoder_get_size(int channels)
211 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
212 return opus_custom_encoder_get_size(mode, channels);
215 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
217 int size = sizeof(struct CELTEncoder)
218 + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
219 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
220 + 3*channels*mode->nbEBands*sizeof(opus_val16); /* opus_val16 oldBandE[channels*mode->nbEBands]; */
221 /* opus_val16 oldLogE[channels*mode->nbEBands]; */
222 /* opus_val16 oldLogE2[channels*mode->nbEBands]; */
224 size += channels*mode->overlap*sizeof(celt_sig); /* celt_sig overlap_mem[channels*mode->nbEBands]; */
230 CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
233 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
234 /* init will handle the NULL case */
235 ret = opus_custom_encoder_init(st, mode, channels);
238 opus_custom_encoder_destroy(st);
245 #endif /* CUSTOM_MODES */
247 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
250 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
253 st->upsample = resampling_factor(sampling_rate);
257 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
259 if (channels < 0 || channels > 2)
262 if (st==NULL || mode==NULL)
263 return OPUS_ALLOC_FAIL;
265 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
268 st->overlap = mode->overlap;
269 st->stream_channels = st->channels = channels;
273 st->end = st->mode->effEBands;
276 st->constrained_vbr = 1;
279 st->bitrate = OPUS_BITRATE_MAX;
285 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
291 void opus_custom_encoder_destroy(CELTEncoder *st)
295 #endif /* CUSTOM_MODES */
297 static inline opus_val16 SIG2WORD16(celt_sig x)
300 x = PSHR32(x, SIG_SHIFT);
301 x = MAX32(x, -32768);
305 return (opus_val16)x;
309 static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
310 int overlap, opus_val16 *tf_estimate, int *tf_chan, AnalysisInfo *analysis)
313 VARDECL(opus_val16, tmp);
314 opus_val32 mem0,mem1;
315 int is_transient = 0;
320 VARDECL(opus_val16, bins);
321 opus_val16 T1, T2, T3, T4, T5;
324 int fmetric=0, bmetric=0;
325 int count1, count2, count3, count4, count5;
328 ALLOC(tmp, len, opus_val16);
332 ALLOC(bins, N, opus_val16);
340 tmp[i] = SHR32(in[i+c*len],SIG_SHIFT);
342 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
349 mem0 = mem1 + y - SHL32(x,1);
350 mem1 = x - SHR32(y,1);
352 mem0 = mem1 + y - 2*x;
355 tmp[i] = EXTRACT16(SHR(y,2));
357 /* First few samples are bad because we don't propagate the memory */
365 opus_val16 max_abs=0;
366 for (j=0;j<2*block;j++)
367 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
369 maxbin = MAX16(maxbin, bins[i]);
372 T1 = QCONST16(.09f, 15);
373 T2 = QCONST16(.12f, 15);
374 T3 = QCONST16(.18f, 15);
375 T4 = QCONST16(.28f, 15);
376 T5 = QCONST16(.4f, 15);
379 count1=count2=count3=count4=count5=0;
382 follower = MAX16(bins[i], MULT16_16_Q15(QCONST16(0.97f, 15), follower));
383 if (bins[i] < MULT16_16_Q15(T1, follower))
385 if (bins[i] < MULT16_16_Q15(T2, follower))
387 if (bins[i] < MULT16_16_Q15(T3, follower))
389 if (bins[i] < MULT16_16_Q15(T4, follower))
391 if (bins[i] < MULT16_16_Q15(T5, follower))
394 fmetric = (5*count1 + 4*count2 + 3*count3 + 2*count4 + count5)/2;
396 count1=count2=count3=count4=count5=0;
399 follower = MAX16(bins[i], MULT16_16_Q15(QCONST16(0.97f, 15), follower));
400 if (bins[i] < MULT16_16_Q15(T1, follower))
402 if (bins[i] < MULT16_16_Q15(T2, follower))
404 if (bins[i] < MULT16_16_Q15(T3, follower))
406 if (bins[i] < MULT16_16_Q15(T4, follower))
408 if (bins[i] < MULT16_16_Q15(T5, follower))
411 bmetric = 5*count1 + 4*count2 + 3*count3 + 2*count4 + count5;
412 metric = fmetric+bmetric;
415 if (metric>20+50*MAX16(analysis->tonality, analysis->noisiness))
424 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
425 *tf_estimate = QCONST16(1.f, 14) + celt_sqrt(MAX16(0, SHL32(MULT16_16(QCONST16(0.0069,14),IMIN(163,tf_max)),14)-QCONST32(0.139,28)));
429 is_transient = rand()&0x1;
431 /*printf("%d %f %f %f %f\n", is_transient, *tf_estimate, tf_max, analysis->tonality, analysis->noisiness);*/
435 /** Apply window and compute the MDCT for all sub-frames and
436 all channels in a frame */
437 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in, celt_sig * OPUS_RESTRICT out, int C, int LM)
439 if (C==1 && !shortBlocks)
441 const int overlap = OVERLAP(mode);
442 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
444 const int overlap = OVERLAP(mode);
445 int N = mode->shortMdctSize<<LM;
450 N = mode->shortMdctSize;
456 /* Interleaving the sub-frames while doing the MDCTs */
457 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);
463 /** Compute the IMDCT and apply window for all sub-frames and
464 all channels in a frame */
465 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
466 celt_sig * OPUS_RESTRICT out_mem[],
467 celt_sig * OPUS_RESTRICT overlap_mem[], int C, int LM)
470 const int N = mode->shortMdctSize<<LM;
471 const int overlap = OVERLAP(mode);
472 VARDECL(opus_val32, x);
475 ALLOC(x, N+overlap, opus_val32);
484 N2 = mode->shortMdctSize;
487 /* Prevents problems from the imdct doing the overlap-add */
488 OPUS_CLEAR(x, overlap);
492 /* IMDCT on the interleaved the sub-frames */
493 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
496 for (j=0;j<overlap;j++)
497 out_mem[c][j] = x[j] + overlap_mem[c][j];
499 out_mem[c][j] = x[j];
500 for (j=0;j<overlap;j++)
501 overlap_mem[c][j] = x[N+j];
506 static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
512 celt_sig * OPUS_RESTRICT x;
513 opus_val16 * OPUS_RESTRICT y;
519 celt_sig tmp = *x + m;
520 m = MULT16_32_Q15(coef[0], tmp)
521 - MULT16_32_Q15(coef[1], *x);
522 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
524 /* Technically the store could be moved outside of the if because
525 the stores we don't want will just be overwritten */
527 *y = SCALEOUT(SIG2WORD16(tmp));
528 if (++count==downsample)
538 static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
539 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
540 const opus_val16 *window, int overlap)
543 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
544 opus_val16 g00, g01, g02, g10, g11, g12;
545 static const opus_val16 gains[3][3] = {
546 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
547 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
548 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
549 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
550 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
551 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
552 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
553 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
554 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
555 for (i=0;i<overlap;i++)
558 f = MULT16_16_Q15(window[i],window[i]);
560 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
561 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
562 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
563 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
564 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
565 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
566 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
567 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
568 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
569 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
572 for (i=overlap;i<N;i++)
574 + MULT16_32_Q15(g10,x[i-T1])
575 + MULT16_32_Q15(g11,x[i-T1-1])
576 + MULT16_32_Q15(g11,x[i-T1+1])
577 + MULT16_32_Q15(g12,x[i-T1-2])
578 + MULT16_32_Q15(g12,x[i-T1+2]);
581 static const signed char tf_select_table[4][8] = {
582 {0, -1, 0, -1, 0,-1, 0,-1},
583 {0, -1, 0, -2, 1, 0, 1,-1},
584 {0, -2, 0, -3, 2, 0, 1,-1},
585 {0, -2, 0, -3, 3, 0, 1,-1},
588 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
594 L1 += EXTEND32(ABS16(tmp[i]));
595 /* When in doubt, prefer good freq resolution */
596 L1 = MAC16_32_Q15(L1, LM*bias, L1);
601 static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
602 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
603 int *tf_sum, opus_val16 tf_estimate, int tf_chan)
606 VARDECL(int, metric);
611 VARDECL(celt_norm, tmp);
612 VARDECL(celt_norm, tmp_1);
620 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(1.5f,14)-tf_estimate));
621 /*printf("%f ", bias);*/
623 if (nbCompressedBytes<15*C)
627 tf_res[i] = isTransient;
630 if (nbCompressedBytes<40)
632 else if (nbCompressedBytes<60)
634 else if (nbCompressedBytes<100)
639 ALLOC(metric, len, int);
640 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
641 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
642 ALLOC(path0, len, int);
643 ALLOC(path1, len, int);
650 opus_val32 L1, best_L1;
652 N = (m->eBands[i+1]-m->eBands[i])<<LM;
653 /* band is too narrow to be split down to LM=-1 */
654 narrow = (m->eBands[i+1]-m->eBands[i])==1;
656 tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)];
657 /* Just add the right channel if we're in stereo */
660 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
661 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
663 /* Check the -1 case for transients */
664 if (isTransient && !narrow)
668 haar1(tmp_1, N>>LM, 1<<LM);
669 L1 = l1_metric(tmp_1, N, LM+1, bias);
676 /*printf ("%f ", L1);*/
677 for (k=0;k<LM+!(isTransient||narrow);k++)
686 haar1(tmp, N>>k, 1<<k);
688 L1 = l1_metric(tmp, N, B, bias);
696 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
697 /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
699 metric[i] = 2*best_level;
701 metric[i] = -2*best_level;
702 *tf_sum += (isTransient ? LM : 0) - metric[i]/2;
703 /* For bands that can't be split to -1, set the metric to the half-way point to avoid
704 biasing the decision */
705 if (narrow && (metric[i]==0 || metric[i]==-2*LM))
707 /*printf("%d ", metric[i]);*/
710 /* Search for the optimal tf resolution, including tf_select */
712 for (sel=0;sel<2;sel++)
715 cost1 = isTransient ? 0 : lambda;
719 curr0 = IMIN(cost0, cost1 + lambda);
720 curr1 = IMIN(cost0 + lambda, cost1);
721 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
722 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
724 cost0 = IMIN(cost0, cost1);
727 /* For now, we're conservative and only allow tf_select=1 for transients.
728 * If tests confirm it's useful for non-transients, we could allow it. */
729 if (selcost[1]<selcost[0] && isTransient)
732 cost1 = isTransient ? 0 : lambda;
733 /* Viterbi forward pass */
740 from1 = cost1 + lambda;
750 from0 = cost0 + lambda;
760 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
761 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
763 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
764 /* Viterbi backward pass to check the decisions */
765 for (i=len-2;i>=0;i--)
767 if (tf_res[i+1] == 1)
768 tf_res[i] = path1[i+1];
770 tf_res[i] = path0[i+1];
772 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
775 tf_select = rand()&0x1;
776 tf_res[0] = rand()&0x1;
778 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
783 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
791 budget = enc->storage*8;
793 logp = isTransient ? 2 : 4;
794 /* Reserve space to code the tf_select decision. */
795 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
796 budget -= tf_select_rsv;
797 curr = tf_changed = 0;
798 for (i=start;i<end;i++)
800 if (tell+logp<=budget)
802 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
809 logp = isTransient ? 4 : 5;
811 /* Only code tf_select if it would actually make a difference. */
813 tf_select_table[LM][4*isTransient+0+tf_changed]!=
814 tf_select_table[LM][4*isTransient+2+tf_changed])
815 ec_enc_bit_logp(enc, tf_select, 1);
818 for (i=start;i<end;i++)
819 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
820 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
823 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
825 int i, curr, tf_select;
832 budget = dec->storage*8;
834 logp = isTransient ? 2 : 4;
835 tf_select_rsv = LM>0 && tell+logp+1<=budget;
836 budget -= tf_select_rsv;
837 tf_changed = curr = 0;
838 for (i=start;i<end;i++)
840 if (tell+logp<=budget)
842 curr ^= ec_dec_bit_logp(dec, logp);
847 logp = isTransient ? 4 : 5;
851 tf_select_table[LM][4*isTransient+0+tf_changed] !=
852 tf_select_table[LM][4*isTransient+2+tf_changed])
854 tf_select = ec_dec_bit_logp(dec, 1);
856 for (i=start;i<end;i++)
858 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
862 static void init_caps(const CELTMode *m,int *cap,int LM,int C)
865 for (i=0;i<m->nbEBands;i++)
868 N=(m->eBands[i+1]-m->eBands[i])<<LM;
869 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
873 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
874 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
875 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
882 opus_val16 trim = QCONST16(5.f, 8);
883 opus_val16 logXC, logXC2;
886 opus_val16 sum = 0; /* Q10 */
887 opus_val16 minXC; /* Q10 */
888 /* Compute inter-channel correlation for low frequencies */
892 opus_val32 partial = 0;
893 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
894 partial = MAC16_16(partial, X[j], X[N0+j]);
895 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
897 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
898 sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
900 for (i=8;i<intensity;i++)
903 opus_val32 partial = 0;
904 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
905 partial = MAC16_16(partial, X[j], X[N0+j]);
906 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
908 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
909 /*printf ("%f\n", sum);*/
910 if (sum > QCONST16(.995f,10))
912 else if (sum > QCONST16(.92f,10))
914 else if (sum > QCONST16(.85f,10))
916 else if (sum > QCONST16(.8f,10))
918 /* mid-side savings estimations based on the LF average*/
919 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
920 /* mid-side savings estimations based on min correlation */
921 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
923 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
924 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
925 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
928 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
929 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
932 /* Estimate spectral tilt */
934 for (i=0;i<end-1;i++)
936 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
940 /*printf("%f\n", diff);*/
941 if (diff > QCONST16(2.f, DB_SHIFT))
943 if (diff > QCONST16(8.f, DB_SHIFT))
945 if (diff < -QCONST16(4.f, DB_SHIFT))
947 if (diff < -QCONST16(10.f, DB_SHIFT))
949 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
950 trim -= 2*SHR16(tf_estimate-QCONST16(1.f,14), 14-8);
954 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), 2*(analysis->tonality_slope+.05)));
959 trim_index = PSHR32(trim, 8);
961 trim_index = floor(.5+trim);
967 /*printf("%d\n", trim_index);*/
969 trim_index = rand()%11;
974 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
979 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
981 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
985 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
987 opus_val32 L, R, M, S;
988 /* We cast to 32-bit first because of the -32768 case */
990 R = EXTEND32(X[N0+j]);
993 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
994 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
997 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
999 /* We don't need thetas for lower bands with LM<=1 */
1002 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
1003 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
1006 int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1011 VARDECL(celt_sig, in);
1012 VARDECL(celt_sig, freq);
1013 VARDECL(celt_norm, X);
1014 VARDECL(celt_ener, bandE);
1015 VARDECL(opus_val16, bandLogE);
1016 VARDECL(opus_val16, bandLogE2);
1017 VARDECL(int, fine_quant);
1018 VARDECL(opus_val16, error);
1019 VARDECL(int, pulses);
1021 VARDECL(int, offsets);
1022 VARDECL(int, fine_priority);
1023 VARDECL(int, tf_res);
1024 VARDECL(unsigned char, collapse_masks);
1025 celt_sig *prefilter_mem;
1026 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1029 const int CC = st->channels;
1030 const int C = st->stream_channels;
1033 int nbFilledBytes, nbAvailableBytes;
1038 int pitch_index=COMBFILTER_MINPERIOD;
1039 opus_val16 gain1 = 0;
1042 opus_val16 pf_threshold;
1044 opus_int32 vbr_rate;
1045 opus_int32 total_bits;
1046 opus_int32 total_boost;
1049 int prefilter_tapset=0;
1051 int anti_collapse_rsv;
1052 int anti_collapse_on=0;
1055 opus_val16 tf_estimate;
1057 opus_int32 tot_boost=0;
1058 opus_val16 sample_max;
1059 opus_val16 maxDepth;
1062 tf_estimate = QCONST16(1.0f,14);
1063 if (nbCompressedBytes<2 || pcm==NULL)
1064 return OPUS_BAD_ARG;
1066 frame_size *= st->upsample;
1067 for (LM=0;LM<=st->mode->maxLM;LM++)
1068 if (st->mode->shortMdctSize<<LM==frame_size)
1070 if (LM>st->mode->maxLM)
1071 return OPUS_BAD_ARG;
1073 N = M*st->mode->shortMdctSize;
1075 prefilter_mem = st->in_mem+CC*(st->overlap);
1076 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD));
1077 oldLogE = oldBandE + CC*st->mode->nbEBands;
1078 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
1086 nbFilledBytes=(tell+4)>>3;
1090 if (st->signalling && enc==NULL)
1092 int tmp = (st->mode->effEBands-st->end)>>1;
1093 st->end = IMAX(1, st->mode->effEBands-tmp);
1094 compressed[0] = tmp<<5;
1095 compressed[0] |= LM<<3;
1096 compressed[0] |= (C==2)<<2;
1097 /* Convert "standard mode" to Opus header */
1098 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
1100 int c0 = toOpus(compressed[0]);
1102 return OPUS_BAD_ARG;
1106 nbCompressedBytes--;
1109 celt_assert(st->signalling==0);
1112 /* Can't produce more than 1275 output bytes */
1113 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1114 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1116 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1118 opus_int32 den=st->mode->Fs>>BITRES;
1119 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1122 vbr_rate -= 8<<BITRES;
1124 effectiveBytes = vbr_rate>>(3+BITRES);
1128 tmp = st->bitrate*frame_size;
1131 if (st->bitrate!=OPUS_BITRATE_MAX)
1132 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1133 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1134 effectiveBytes = nbCompressedBytes;
1139 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1145 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1146 target rate and buffering.
1147 We must do this up front so that bust-prevention logic triggers
1148 correctly if we don't have enough bits. */
1149 if (st->constrained_vbr)
1151 opus_int32 vbr_bound;
1152 opus_int32 max_allowed;
1153 /* We could use any multiple of vbr_rate as bound (depending on the
1155 This is clamped to ensure we use at least two bytes if the encoder
1156 was entirely empty, but to allow 0 in hybrid mode. */
1157 vbr_bound = vbr_rate;
1158 max_allowed = IMIN(IMAX(tell==1?2:0,
1159 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1161 if(max_allowed < nbAvailableBytes)
1163 nbCompressedBytes = nbFilledBytes+max_allowed;
1164 nbAvailableBytes = max_allowed;
1165 ec_enc_shrink(enc, nbCompressedBytes);
1169 total_bits = nbCompressedBytes*8;
1172 if (effEnd > st->mode->effEBands)
1173 effEnd = st->mode->effEBands;
1175 ALLOC(in, CC*(N+st->overlap), celt_sig);
1177 sample_max=MAX16(st->overlap_max, celt_maxabs16(pcm, C*(N-st->mode->overlap)));
1178 st->overlap_max=celt_maxabs16(pcm+C*(N-st->mode->overlap), C*st->mode->overlap);
1179 sample_max=MAX16(sample_max, st->overlap_max);
1180 /* Find pitch period and gain */
1182 VARDECL(celt_sig, _pre);
1185 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1188 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1193 const opus_val16 * OPUS_RESTRICT pcmp = pcm+c;
1194 celt_sig * OPUS_RESTRICT inp = in+c*(N+st->overlap)+st->overlap;
1205 x = MAX32(-65536.f, MIN32(65536.f,x));
1207 if (++count==st->upsample)
1214 /* Apply pre-emphasis */
1215 tmp = MULT16_16(st->mode->preemph[2], x);
1216 *inp = tmp + st->preemph_memE[c];
1217 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1218 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1221 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1222 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1226 silence = (sample_max==0);
1228 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1231 if ((rand()&0x3F)==0)
1235 ec_enc_bit_logp(enc, silence, 15);
1240 /*In VBR mode there is no need to send more than the minimum. */
1243 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1244 total_bits=nbCompressedBytes*8;
1246 ec_enc_shrink(enc, nbCompressedBytes);
1248 /* Pretend we've filled all the remaining bits with zeros
1249 (that's what the initialiser did anyway) */
1250 tell = nbCompressedBytes*8;
1251 enc->nbits_total+=tell-ec_tell(enc);
1253 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1255 VARDECL(opus_val16, pitch_buf);
1256 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1258 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1259 /* Don't search for the fir last 1.5 octave of the range because
1260 there's too many false-positives due to short-term correlation */
1261 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1262 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index);
1263 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1265 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1266 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1267 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1268 pitch_index = COMBFILTER_MAXPERIOD-2;
1269 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1270 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && st->analysis.tonality > .3
1271 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
1273 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1274 if (st->loss_rate>2)
1275 gain1 = HALF32(gain1);
1276 if (st->loss_rate>4)
1277 gain1 = HALF32(gain1);
1278 if (st->loss_rate>8)
1280 prefilter_tapset = st->tapset_decision;
1285 /* Gain threshold for enabling the prefilter/postfilter */
1286 pf_threshold = QCONST16(.2f,15);
1288 /* Adjusting the threshold based on rate and continuity */
1289 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1290 pf_threshold += QCONST16(.2f,15);
1291 if (nbAvailableBytes<25)
1292 pf_threshold += QCONST16(.1f,15);
1293 if (nbAvailableBytes<35)
1294 pf_threshold += QCONST16(.1f,15);
1295 if (st->prefilter_gain > QCONST16(.4f,15))
1296 pf_threshold -= QCONST16(.1f,15);
1297 if (st->prefilter_gain > QCONST16(.55f,15))
1298 pf_threshold -= QCONST16(.1f,15);
1300 /* Hard threshold at 0.2 */
1301 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1302 if (gain1<pf_threshold)
1304 if(st->start==0 && tell+16<=total_bits)
1305 ec_enc_bit_logp(enc, 0, 1);
1309 /*This block is not gated by a total bits check only because
1310 of the nbAvailableBytes check above.*/
1314 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1315 gain1=st->prefilter_gain;
1318 qg = ((gain1+1536)>>10)/3-1;
1320 qg = (int)floor(.5f+gain1*32/3)-1;
1322 qg = IMAX(0, IMIN(7, qg));
1323 ec_enc_bit_logp(enc, 1, 1);
1325 octave = EC_ILOG(pitch_index)-5;
1326 ec_enc_uint(enc, octave, 6);
1327 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1329 ec_enc_bits(enc, qg, 3);
1330 if (ec_tell(enc)+2<=total_bits)
1331 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1333 prefilter_tapset = 0;
1334 gain1 = QCONST16(0.09375f,15)*(qg+1);
1337 /*printf("%d %f\n", pitch_index, gain1);*/
1340 int offset = st->mode->shortMdctSize-st->mode->overlap;
1341 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1342 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1344 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1345 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1346 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1348 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1349 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1350 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1351 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1353 if (N>COMBFILTER_MAXPERIOD)
1355 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1357 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1358 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1367 if (LM>0 && ec_tell(enc)+3<=total_bits)
1369 if (st->complexity > 1)
1371 isTransient = transient_analysis(in, N+st->overlap, CC,
1372 st->overlap, &tf_estimate, &tf_chan, &st->analysis);
1376 ec_enc_bit_logp(enc, isTransient, 3);
1379 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1380 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1381 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1383 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1388 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1391 if (st->upsample != 1)
1395 int bound = N/st->upsample;
1396 for (i=0;i<bound;i++)
1397 freq[c*N+i] *= st->upsample;
1402 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1404 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1405 /*for (i=0;i<21;i++)
1406 printf("%f ", bandLogE[i]);
1409 ALLOC(bandLogE2, C*st->mode->nbEBands, opus_val16);
1410 if (shortBlocks && st->complexity>=8)
1412 VARDECL(celt_sig, freq2);
1413 VARDECL(opus_val32, bandE2);
1414 ALLOC(freq2, CC*N, celt_sig);
1415 compute_mdcts(st->mode, 0, in, freq2, CC, LM);
1419 freq2[i] = ADD32(HALF32(freq2[i]), HALF32(freq2[N+i]));
1421 if (st->upsample != 1)
1425 int bound = N/st->upsample;
1426 for (i=0;i<bound;i++)
1427 freq2[c*N+i] *= st->upsample;
1432 ALLOC(bandE2, C*st->mode->nbEBands, opus_val32);
1433 compute_band_energies(st->mode, freq2, bandE2, effEnd, C, M);
1434 amp2Log2(st->mode, effEnd, st->end, bandE2, bandLogE2, C);
1435 for (i=0;i<C*st->mode->nbEBands;i++)
1436 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1438 for (i=0;i<C*st->mode->nbEBands;i++)
1439 bandLogE2[i] = bandLogE[i];
1442 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1444 /* Band normalisation */
1445 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1447 ALLOC(tf_res, st->mode->nbEBands, int);
1448 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum, tf_estimate, tf_chan);
1449 for (i=effEnd;i<st->end;i++)
1450 tf_res[i] = tf_res[effEnd-1];
1452 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1453 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1454 oldBandE, total_bits, error, enc,
1455 C, LM, nbAvailableBytes, st->force_intra,
1456 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1458 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1460 if (ec_tell(enc)+4<=total_bits)
1462 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1464 if (st->complexity == 0)
1465 st->spread_decision = SPREAD_NONE;
1467 if (st->analysis.valid)
1469 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1470 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1471 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1472 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1473 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1474 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1476 st->spread_decision = spreading_decision(st->mode, X,
1477 &st->tonal_average, st->spread_decision, &st->hf_average,
1478 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1480 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1481 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1483 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1486 ALLOC(cap, st->mode->nbEBands, int);
1487 ALLOC(offsets, st->mode->nbEBands, int);
1489 init_caps(st->mode,cap,LM,C);
1490 for (i=0;i<st->mode->nbEBands;i++)
1492 /* Dynamic allocation code */
1493 maxDepth=-QCONST16(32.f, DB_SHIFT);
1494 /* Make sure that dynamic allocation can't make us bust the budget */
1495 if (effectiveBytes > 50 && LM>=1)
1498 VARDECL(opus_val16, follower);
1499 ALLOC(follower, C*st->mode->nbEBands, opus_val16);
1502 follower[c*st->mode->nbEBands] = bandLogE2[c*st->mode->nbEBands];
1503 for (i=1;i<st->end;i++)
1505 /* The last band to be at least 3 dB higher than the previous one
1506 is the last we'll consider. Otherwise, we run into problems on
1507 bandlimited signals. */
1508 if (bandLogE2[c*st->mode->nbEBands+i] > bandLogE2[c*st->mode->nbEBands+i-1]+QCONST16(.5f,DB_SHIFT))
1510 follower[c*st->mode->nbEBands+i] = MIN16(follower[c*st->mode->nbEBands+i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*st->mode->nbEBands+i]);
1512 for (i=last-1;i>=0;i--)
1513 follower[c*st->mode->nbEBands+i] = MIN16(follower[c*st->mode->nbEBands+i], MIN16(follower[c*st->mode->nbEBands+i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*st->mode->nbEBands+i]));
1514 for (i=0;i<st->end;i++)
1516 opus_val16 noise_floor;
1517 /* Noise floor must take into account eMeans, the depth, the width of the bands
1518 and the preemphasis filter (approx. square of bark band ID) */
1519 noise_floor = MULT16_16(QCONST16(0.0625f, DB_SHIFT),st->mode->logN[i])
1520 +QCONST16(.5f,DB_SHIFT)+SHL16(9-st->lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6)
1521 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5));
1522 follower[c*st->mode->nbEBands+i] = MAX16(follower[c*st->mode->nbEBands+i], noise_floor);
1523 maxDepth = MAX16(maxDepth, bandLogE[c*st->mode->nbEBands+i]-noise_floor);
1528 for (i=st->start;i<st->end;i++)
1530 /* Consider 24 dB "cross-talk" */
1531 follower[st->mode->nbEBands+i] = MAX16(follower[st->mode->nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT));
1532 follower[ i] = MAX16(follower[ i], follower[st->mode->nbEBands+i]-QCONST16(4.f,DB_SHIFT));
1533 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[st->mode->nbEBands+i]-follower[st->mode->nbEBands+i]));
1536 for (i=st->start;i<st->end;i++)
1538 follower[i] = MAX16(0, bandLogE[i]-follower[i]);
1541 /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1542 if ((!st->vbr || st->constrained_vbr)&&!isTransient)
1544 for (i=st->start;i<st->end;i++)
1545 follower[i] = HALF16(follower[i]);
1547 for (i=st->start;i<st->end;i++)
1556 follower[i] = HALF16(follower[i]);
1557 follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT));
1559 /* FIXME: Adaptively reduce follower at low rate or for cbr/cvbr */
1560 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1563 boost = SHR32(EXTEND32(follower[i]),DB_SHIFT);
1564 boost_bits = boost*width<<BITRES;
1565 } else if (width > 48) {
1566 boost = SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
1567 boost_bits = (boost*width<<BITRES)/8;
1569 boost = SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
1570 boost_bits = boost*6<<BITRES;
1572 /* For CBR and non-transient CVBR frames, limit dynalloc to 1/4 of the bits */
1573 if ((!st->vbr || (st->constrained_vbr&&!isTransient))
1574 && (tot_boost+boost_bits)>>BITRES>>3 > effectiveBytes/4)
1580 tot_boost += boost_bits;
1585 total_bits<<=BITRES;
1587 tell = ec_tell_frac(enc);
1588 for (i=st->start;i<st->end;i++)
1591 int dynalloc_loop_logp;
1594 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1595 /* quanta is 6 bits, but no more than 1 bit/sample
1596 and no less than 1/8 bit/sample */
1597 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1598 dynalloc_loop_logp = dynalloc_logp;
1600 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1601 && boost < cap[i]; j++)
1604 flag = j<offsets[i];
1605 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1606 tell = ec_tell_frac(enc);
1610 total_boost += quanta;
1611 dynalloc_loop_logp = 1;
1613 /* Making dynalloc more likely */
1615 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1623 static const opus_val16 intensity_thresholds[21]=
1624 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
1625 { 16,21,23,25,27,29,31,33,35,38,42,46,50,54,58,63,68,75,84,102,130};
1626 static const opus_val16 intensity_histeresis[21]=
1627 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 8, 12};
1629 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1631 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1633 /* Account for coarse energy */
1634 effectiveRate = (8*effectiveBytes - 80)>>LM;
1636 /* effectiveRate in kb/s */
1637 effectiveRate = 2*effectiveRate/5;
1639 st->intensity = hysteresis_decision(effectiveRate, intensity_thresholds, intensity_histeresis, 21, st->intensity);
1640 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity));
1644 if (tell+(6<<BITRES) <= total_bits - total_boost)
1646 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1647 st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, st->intensity);
1648 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1649 tell = ec_tell_frac(enc);
1652 /* Variable bitrate */
1657 /* The target rate in 8th bits per frame */
1658 opus_int32 target, base_target;
1659 opus_int32 min_allowed;
1662 int lm_diff = st->mode->maxLM - LM;
1663 coded_bands = st->lastCodedBands ? st->lastCodedBands : st->mode->nbEBands;
1664 coded_bins = st->mode->eBands[coded_bands]<<LM;
1666 coded_bins += st->mode->eBands[IMIN(st->intensity, coded_bands)]<<LM;
1668 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1669 The CELT allocator will just not be able to use more than that anyway. */
1670 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1671 target = vbr_rate - ((40*C+20)<<BITRES);
1672 base_target = target;
1674 if (st->constrained_vbr)
1675 target += (st->vbr_offset>>lm_diff);
1677 /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1679 if (st->analysis.valid && st->analysis.activity<.4)
1680 target -= (coded_bins<<BITRES)*1*(.4-st->analysis.activity);
1682 /* Stereo savings */
1685 int coded_stereo_bands;
1686 int coded_stereo_dof;
1687 coded_stereo_bands = IMIN(st->intensity, coded_bands);
1688 coded_stereo_dof = (st->mode->eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1689 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1690 target -= MIN32(target/3, SHR16(MULT16_16(st->stereo_saving,(coded_stereo_dof<<BITRES)),8));
1691 target += MULT16_16_Q15(QCONST16(0.035,15),coded_stereo_dof<<BITRES);
1693 /* Limits starving of other bands when using dynalloc */
1694 target += tot_boost;
1695 /* Compensates for the average transient boost */
1696 target = MULT16_32_Q15(QCONST16(0.96f,15),target);
1697 /* Apply transient boost */
1698 target = SHL32(MULT16_32_Q15(tf_estimate, target),1);
1701 /* Apply tonality boost */
1702 if (st->analysis.valid) {
1706 /* Compensates for the average tonality boost */
1707 target -= MULT16_16_Q15(QCONST16(0.13f,15),coded_bins<<BITRES);
1709 tonal = MAX16(0,st->analysis.tonality-.2);
1710 tonal_target = target + (coded_bins<<BITRES)*2.0f*tonal;
1712 tonal_target += (coded_bins<<BITRES)*.8;
1713 /*printf("%f %f ", st->analysis.tonality, tonal);*/
1714 target = IMAX(tonal_target,target);
1719 opus_int32 floor_depth;
1721 bins = st->mode->eBands[st->mode->nbEBands-2]<<LM;
1722 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1723 floor_depth = SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT);
1724 floor_depth = IMAX(floor_depth, target>>2);
1725 target = IMIN(target, floor_depth);
1726 /*printf("%f %d\n", maxDepth, floor_depth);*/
1729 if (st->constrained_vbr || st->bitrate<64000)
1731 opus_val16 rate_factor;
1733 rate_factor = MAX16(0,(st->bitrate-32000));
1735 rate_factor = MAX16(0,(1.f/32768)*(st->bitrate-32000));
1737 if (st->constrained_vbr)
1738 rate_factor = MIN16(rate_factor, QCONST16(0.67f, 15));
1739 target = base_target + MULT16_32_Q15(rate_factor, target-base_target);
1742 /* Don't allow more than doubling the rate */
1743 target = IMIN(2*base_target, target);
1745 /* The current offset is removed from the target and the space used
1748 /* In VBR mode the frame size must not be reduced so much that it would
1749 result in the encoder running out of bits.
1750 The margin of 2 bytes ensures that none of the bust-prevention logic
1751 in the decoder will have triggered so far. */
1752 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1754 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1755 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1756 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1758 /* By how much did we "miss" the target on that frame */
1759 delta = target - vbr_rate;
1761 target=nbAvailableBytes<<(BITRES+3);
1763 /*If the frame is silent we don't adjust our drift, otherwise
1764 the encoder will shoot to very high rates after hitting a
1765 span of silence, but we do allow the bitres to refill.
1766 This means that we'll undershoot our target in CVBR/VBR modes
1767 on files with lots of silence. */
1770 nbAvailableBytes = 2;
1771 target = 2*8<<BITRES;
1775 if (st->vbr_count < 970)
1778 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1780 alpha = QCONST16(.001f,15);
1781 /* How many bits have we used in excess of what we're allowed */
1782 if (st->constrained_vbr)
1783 st->vbr_reservoir += target - vbr_rate;
1784 /*printf ("%d\n", st->vbr_reservoir);*/
1786 /* Compute the offset we need to apply in order to reach the target */
1787 if (st->constrained_vbr)
1789 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1790 st->vbr_offset = -st->vbr_drift;
1792 /*printf ("%d\n", st->vbr_drift);*/
1794 if (st->constrained_vbr && st->vbr_reservoir < 0)
1796 /* We're under the min value -- increase rate */
1797 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1798 /* Unless we're just coding silence */
1799 nbAvailableBytes += silence?0:adjust;
1800 st->vbr_reservoir = 0;
1801 /*printf ("+%d\n", adjust);*/
1803 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1804 /*printf("%d\n", nbCompressedBytes*50*8);*/
1805 /* This moves the raw bits to take into account the new compressed size */
1806 ec_enc_shrink(enc, nbCompressedBytes);
1809 /* Bit allocation */
1810 ALLOC(fine_quant, st->mode->nbEBands, int);
1811 ALLOC(pulses, st->mode->nbEBands, int);
1812 ALLOC(fine_priority, st->mode->nbEBands, int);
1814 /* bits = packet size - where we are - safety*/
1815 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1816 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1817 bits -= anti_collapse_rsv;
1818 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1819 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
1820 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1821 st->lastCodedBands = codedBands;
1823 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1825 #ifdef MEASURE_NORM_MSE
1830 X0[i+c*N] = X[i+c*N];
1832 for (i=0;i<C*st->mode->nbEBands;i++)
1833 bandE0[i] = bandE[i];
1836 /* Residual quantisation */
1837 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1838 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1839 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, st->intensity, tf_res,
1840 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1842 if (anti_collapse_rsv > 0)
1844 anti_collapse_on = st->consec_transient<2;
1846 anti_collapse_on = rand()&0x1;
1848 ec_enc_bits(enc, anti_collapse_on, 1);
1850 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1854 for (i=0;i<C*st->mode->nbEBands;i++)
1855 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1859 /* Re-synthesis of the coded audio if required */
1861 celt_sig *out_mem[2];
1862 celt_sig *overlap_mem[2];
1864 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1867 for (i=0;i<C*st->mode->nbEBands;i++)
1871 #ifdef MEASURE_NORM_MSE
1872 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1874 if (anti_collapse_on)
1876 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1877 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1881 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1883 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1885 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1888 for (i=0;i<M*st->mode->eBands[st->start];i++)
1892 for (i=M*st->mode->eBands[st->end];i<N;i++)
1899 freq[N+i] = freq[i];
1902 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1904 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1906 overlap_mem[0] = (celt_sig*)(oldLogE2 + CC*st->mode->nbEBands);
1908 overlap_mem[1] = overlap_mem[0] + st->overlap;
1910 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1913 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1914 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1915 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1916 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1917 st->mode->window, st->overlap);
1919 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1920 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1921 st->mode->window, st->mode->overlap);
1924 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1925 st->prefilter_period_old = st->prefilter_period;
1926 st->prefilter_gain_old = st->prefilter_gain;
1927 st->prefilter_tapset_old = st->prefilter_tapset;
1931 st->prefilter_period = pitch_index;
1932 st->prefilter_gain = gain1;
1933 st->prefilter_tapset = prefilter_tapset;
1937 st->prefilter_period_old = st->prefilter_period;
1938 st->prefilter_gain_old = st->prefilter_gain;
1939 st->prefilter_tapset_old = st->prefilter_tapset;
1944 for (i=0;i<st->mode->nbEBands;i++)
1945 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1950 for (i=0;i<CC*st->mode->nbEBands;i++)
1951 oldLogE2[i] = oldLogE[i];
1952 for (i=0;i<CC*st->mode->nbEBands;i++)
1953 oldLogE[i] = oldBandE[i];
1955 for (i=0;i<CC*st->mode->nbEBands;i++)
1956 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1958 /* In case start or end were to change */
1961 for (i=0;i<st->start;i++)
1963 oldBandE[c*st->mode->nbEBands+i]=0;
1964 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1966 for (i=st->end;i<st->mode->nbEBands;i++)
1968 oldBandE[c*st->mode->nbEBands+i]=0;
1969 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1974 st->consec_transient++;
1976 st->consec_transient=0;
1979 /* If there's any room left (can only happen for very high rates),
1980 it's already filled with zeros */
1985 nbCompressedBytes++;
1989 if (ec_get_error(enc))
1990 return OPUS_INTERNAL_ERROR;
1992 return nbCompressedBytes;
1999 int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2001 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2004 #ifndef DISABLE_FLOAT_API
2005 int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2008 VARDECL(opus_int16, in);
2012 return OPUS_BAD_ARG;
2016 ALLOC(in, C*N, opus_int16);
2019 in[j] = FLOAT2INT16(pcm[j]);
2021 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2024 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
2029 #endif /* DISABLE_FLOAT_API */
2032 int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2035 VARDECL(celt_sig, in);
2039 return OPUS_BAD_ARG;
2043 ALLOC(in, C*N, celt_sig);
2044 for (j=0;j<C*N;j++) {
2045 in[j] = SCALEOUT(pcm[j]);
2048 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2051 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
2057 int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2059 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2064 #endif /* CUSTOM_MODES */
2066 int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2070 va_start(ap, request);
2073 case OPUS_SET_COMPLEXITY_REQUEST:
2075 int value = va_arg(ap, opus_int32);
2076 if (value<0 || value>10)
2078 st->complexity = value;
2081 case CELT_SET_START_BAND_REQUEST:
2083 opus_int32 value = va_arg(ap, opus_int32);
2084 if (value<0 || value>=st->mode->nbEBands)
2089 case CELT_SET_END_BAND_REQUEST:
2091 opus_int32 value = va_arg(ap, opus_int32);
2092 if (value<1 || value>st->mode->nbEBands)
2097 case CELT_SET_PREDICTION_REQUEST:
2099 int value = va_arg(ap, opus_int32);
2100 if (value<0 || value>2)
2102 st->disable_pf = value<=1;
2103 st->force_intra = value==0;
2106 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2108 int value = va_arg(ap, opus_int32);
2109 if (value<0 || value>100)
2111 st->loss_rate = value;
2114 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2116 opus_int32 value = va_arg(ap, opus_int32);
2117 st->constrained_vbr = value;
2120 case OPUS_SET_VBR_REQUEST:
2122 opus_int32 value = va_arg(ap, opus_int32);
2126 case OPUS_SET_BITRATE_REQUEST:
2128 opus_int32 value = va_arg(ap, opus_int32);
2129 if (value<=500 && value!=OPUS_BITRATE_MAX)
2131 value = IMIN(value, 260000*st->channels);
2132 st->bitrate = value;
2135 case CELT_SET_CHANNELS_REQUEST:
2137 opus_int32 value = va_arg(ap, opus_int32);
2138 if (value<1 || value>2)
2140 st->stream_channels = value;
2143 case OPUS_SET_LSB_DEPTH_REQUEST:
2145 opus_int32 value = va_arg(ap, opus_int32);
2146 if (value<8 || value>24)
2148 st->lsb_depth=value;
2151 case OPUS_GET_LSB_DEPTH_REQUEST:
2153 opus_int32 *value = va_arg(ap, opus_int32*);
2154 *value=st->lsb_depth;
2157 case OPUS_RESET_STATE:
2160 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
2161 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTER_MAXPERIOD));
2162 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2163 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2164 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2165 opus_custom_encoder_get_size(st->mode, st->channels)-
2166 ((char*)&st->ENCODER_RESET_START - (char*)st));
2167 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2168 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2170 st->delayedIntra = 1;
2171 st->spread_decision = SPREAD_NORMAL;
2172 st->tonal_average = 256;
2174 st->tapset_decision = 0;
2178 case CELT_SET_INPUT_CLIPPING_REQUEST:
2180 opus_int32 value = va_arg(ap, opus_int32);
2185 case CELT_SET_SIGNALLING_REQUEST:
2187 opus_int32 value = va_arg(ap, opus_int32);
2188 st->signalling = value;
2191 case CELT_SET_ANALYSIS_REQUEST:
2193 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2195 OPUS_COPY(&st->analysis, info, 1);
2198 case CELT_GET_MODE_REQUEST:
2200 const CELTMode ** value = va_arg(ap, const CELTMode**);
2206 case OPUS_GET_FINAL_RANGE_REQUEST:
2208 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2221 return OPUS_BAD_ARG;
2224 return OPUS_UNIMPLEMENTED;
2227 /**********************************************************************/
2231 /**********************************************************************/
2232 #define DECODE_BUFFER_SIZE 2048
2235 @brief Decoder state
2237 struct OpusCustomDecoder {
2238 const OpusCustomMode *mode;
2241 int stream_channels;
2247 /* Everything beyond this point gets cleared on a reset */
2248 #define DECODER_RESET_START rng
2252 int last_pitch_index;
2254 int postfilter_period;
2255 int postfilter_period_old;
2256 opus_val16 postfilter_gain;
2257 opus_val16 postfilter_gain_old;
2258 int postfilter_tapset;
2259 int postfilter_tapset_old;
2261 celt_sig preemph_memD[2];
2263 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
2264 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
2265 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
2266 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
2267 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
2268 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
2271 int celt_decoder_get_size(int channels)
2273 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
2274 return opus_custom_decoder_get_size(mode, channels);
2277 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
2279 int size = sizeof(struct CELTDecoder)
2280 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
2281 + channels*LPC_ORDER*sizeof(opus_val16)
2282 + 4*2*mode->nbEBands*sizeof(opus_val16);
2287 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
2290 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
2291 ret = opus_custom_decoder_init(st, mode, channels);
2294 opus_custom_decoder_destroy(st);
2301 #endif /* CUSTOM_MODES */
2303 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
2306 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
2309 st->downsample = resampling_factor(sampling_rate);
2310 if (st->downsample==0)
2311 return OPUS_BAD_ARG;
2316 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
2318 if (channels < 0 || channels > 2)
2319 return OPUS_BAD_ARG;
2322 return OPUS_ALLOC_FAIL;
2324 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2327 st->overlap = mode->overlap;
2328 st->stream_channels = st->channels = channels;
2332 st->end = st->mode->effEBands;
2337 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2343 void opus_custom_decoder_destroy(CELTDecoder *st)
2347 #endif /* CUSTOM_MODES */
2349 static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM)
2353 int overlap = st->mode->overlap;
2354 opus_val16 fade = Q15ONE;
2356 const int C = st->channels;
2358 celt_sig *out_mem[2];
2359 celt_sig *decode_mem[2];
2360 celt_sig *overlap_mem[2];
2362 opus_val32 *out_syn[2];
2363 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2367 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2368 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2369 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2371 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2372 oldBandE = lpc+C*LPC_ORDER;
2373 oldLogE = oldBandE + 2*st->mode->nbEBands;
2374 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2375 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2377 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2379 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2381 len = N+st->mode->overlap;
2383 if (st->loss_count >= 5 || st->start!=0)
2385 /* Noise-based PLC/CNG */
2386 VARDECL(celt_sig, freq);
2387 VARDECL(celt_norm, X);
2388 VARDECL(celt_ener, bandE);
2393 if (effEnd > st->mode->effEBands)
2394 effEnd = st->mode->effEBands;
2396 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2397 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2398 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2400 if (st->loss_count >= 5)
2401 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2404 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2407 for (i=st->start;i<st->end;i++)
2408 oldBandE[c*st->mode->nbEBands+i] -= decay;
2410 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2415 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2417 for (i=st->start;i<st->mode->effEBands;i++)
2422 boffs = N*c+(st->mode->eBands[i]<<LM);
2423 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2424 for (j=0;j<blen;j++)
2426 seed = celt_lcg_rand(seed);
2427 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2429 renormalise_vector(X+boffs, blen, Q15ONE);
2431 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2436 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2439 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2443 int bound = st->mode->eBands[effEnd]<<LM;
2444 if (st->downsample!=1)
2445 bound = IMIN(bound, N/st->downsample);
2446 for (i=bound;i<N;i++)
2449 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2451 /* Pitch-based PLC */
2452 if (st->loss_count == 0)
2454 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2455 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2456 search by using only part of the decode buffer */
2458 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2459 /* Max pitch is 100 samples (480 Hz) */
2460 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2461 poffset-100, &pitch_index);
2462 pitch_index = poffset-pitch_index;
2463 st->last_pitch_index = pitch_index;
2465 pitch_index = st->last_pitch_index;
2466 fade = QCONST16(.8f,15);
2470 VARDECL(opus_val32, e);
2471 opus_val16 exc[MAX_PERIOD];
2472 opus_val32 ac[LPC_ORDER+1];
2473 opus_val16 decay = 1;
2475 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};
2477 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2479 offset = MAX_PERIOD-pitch_index;
2480 for (i=0;i<MAX_PERIOD;i++)
2481 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2483 if (st->loss_count == 0)
2485 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2486 LPC_ORDER, MAX_PERIOD);
2488 /* Noise floor -40 dB */
2490 ac[0] += SHR32(ac[0],13);
2495 for (i=1;i<=LPC_ORDER;i++)
2497 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2499 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2501 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2505 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2507 for (i=0;i<LPC_ORDER;i++)
2508 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2509 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2510 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2511 /* Check if the waveform is decaying (and if so how fast) */
2513 opus_val32 E1=1, E2=1;
2515 if (pitch_index <= MAX_PERIOD/2)
2516 period = pitch_index;
2518 period = MAX_PERIOD/2;
2519 for (i=0;i<period;i++)
2521 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2522 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2526 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2529 /* Copy excitation, taking decay into account */
2530 for (i=0;i<len+st->mode->overlap;i++)
2533 if (offset+i >= MAX_PERIOD)
2535 offset -= pitch_index;
2536 decay = MULT16_16_Q15(decay, decay);
2538 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2539 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2540 S1 += SHR32(MULT16_16(tmp,tmp),8);
2542 for (i=0;i<LPC_ORDER;i++)
2543 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2544 for (i=0;i<len+st->mode->overlap;i++)
2545 e[i] = MULT16_32_Q15(fade, e[i]);
2546 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2550 for (i=0;i<len+overlap;i++)
2552 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2553 S2 += SHR32(MULT16_16(tmp,tmp),8);
2555 /* This checks for an "explosion" in the synthesis */
2557 if (!(S1 > SHR32(S2,2)))
2559 /* Float test is written this way to catch NaNs at the same time */
2560 if (!(S1 > 0.2f*S2))
2563 for (i=0;i<len+overlap;i++)
2567 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2568 for (i=0;i<len+overlap;i++)
2569 e[i] = MULT16_32_Q15(ratio, e[i]);
2573 /* Apply post-filter to the MDCT overlap of the previous frame */
2574 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2575 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2578 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2579 out_mem[c][i] = out_mem[c][N+i];
2581 /* Apply TDAC to the concealed audio so that it blends with the
2582 previous and next frames */
2583 for (i=0;i<overlap/2;i++)
2586 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2587 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2588 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2589 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2592 out_mem[c][MAX_PERIOD-N+i] = e[i];
2594 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2595 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2596 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2598 for (i=0;i<overlap;i++)
2599 out_mem[c][MAX_PERIOD+i] = e[i];
2603 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2610 int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec)
2613 int spread_decision;
2616 VARDECL(celt_sig, freq);
2617 VARDECL(celt_norm, X);
2618 VARDECL(celt_ener, bandE);
2619 VARDECL(int, fine_quant);
2620 VARDECL(int, pulses);
2622 VARDECL(int, offsets);
2623 VARDECL(int, fine_priority);
2624 VARDECL(int, tf_res);
2625 VARDECL(unsigned char, collapse_masks);
2626 celt_sig *out_mem[2];
2627 celt_sig *decode_mem[2];
2628 celt_sig *overlap_mem[2];
2629 celt_sig *out_syn[2];
2631 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2636 const int CC = st->channels;
2641 int postfilter_pitch;
2642 opus_val16 postfilter_gain;
2645 opus_int32 total_bits;
2649 int postfilter_tapset;
2650 int anti_collapse_rsv;
2651 int anti_collapse_on=0;
2653 int C = st->stream_channels;
2656 frame_size *= st->downsample;
2659 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2660 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2661 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2663 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2664 oldBandE = lpc+CC*LPC_ORDER;
2665 oldLogE = oldBandE + 2*st->mode->nbEBands;
2666 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2667 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2670 if (st->signalling && data!=NULL)
2673 /* Convert "standard mode" to Opus header */
2674 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2676 data0 = fromOpus(data0);
2678 return OPUS_INVALID_PACKET;
2680 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2681 LM = (data0>>3)&0x3;
2682 C = 1 + ((data0>>2)&0x1);
2685 if (LM>st->mode->maxLM)
2686 return OPUS_INVALID_PACKET;
2687 if (frame_size < st->mode->shortMdctSize<<LM)
2688 return OPUS_BUFFER_TOO_SMALL;
2690 frame_size = st->mode->shortMdctSize<<LM;
2695 for (LM=0;LM<=st->mode->maxLM;LM++)
2696 if (st->mode->shortMdctSize<<LM==frame_size)
2698 if (LM>st->mode->maxLM)
2699 return OPUS_BAD_ARG;
2703 if (len<0 || len>1275 || pcm==NULL)
2704 return OPUS_BAD_ARG;
2706 N = M*st->mode->shortMdctSize;
2709 if (effEnd > st->mode->effEBands)
2710 effEnd = st->mode->effEBands;
2712 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2713 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2714 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2716 for (i=0;i<M*st->mode->eBands[st->start];i++)
2720 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2724 if (data == NULL || len<=1)
2726 celt_decode_lost(st, pcm, N, LM);
2728 return frame_size/st->downsample;
2733 ec_dec_init(&_dec,(unsigned char*)data,len);
2739 for (i=0;i<st->mode->nbEBands;i++)
2740 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2744 tell = ec_tell(dec);
2746 if (tell >= total_bits)
2749 silence = ec_dec_bit_logp(dec, 15);
2754 /* Pretend we've read all the remaining bits */
2756 dec->nbits_total+=tell-ec_tell(dec);
2759 postfilter_gain = 0;
2760 postfilter_pitch = 0;
2761 postfilter_tapset = 0;
2762 if (st->start==0 && tell+16 <= total_bits)
2764 if(ec_dec_bit_logp(dec, 1))
2767 octave = ec_dec_uint(dec, 6);
2768 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2769 qg = ec_dec_bits(dec, 3);
2770 if (ec_tell(dec)+2<=total_bits)
2771 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2772 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2774 tell = ec_tell(dec);
2777 if (LM > 0 && tell+3 <= total_bits)
2779 isTransient = ec_dec_bit_logp(dec, 3);
2780 tell = ec_tell(dec);
2790 /* Decode the global flags (first symbols in the stream) */
2791 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2792 /* Get band energies */
2793 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2794 intra_ener, dec, C, LM);
2796 ALLOC(tf_res, st->mode->nbEBands, int);
2797 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2799 tell = ec_tell(dec);
2800 spread_decision = SPREAD_NORMAL;
2801 if (tell+4 <= total_bits)
2802 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2804 ALLOC(pulses, st->mode->nbEBands, int);
2805 ALLOC(cap, st->mode->nbEBands, int);
2806 ALLOC(offsets, st->mode->nbEBands, int);
2807 ALLOC(fine_priority, st->mode->nbEBands, int);
2809 init_caps(st->mode,cap,LM,C);
2812 total_bits<<=BITRES;
2813 tell = ec_tell_frac(dec);
2814 for (i=st->start;i<st->end;i++)
2817 int dynalloc_loop_logp;
2819 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2820 /* quanta is 6 bits, but no more than 1 bit/sample
2821 and no less than 1/8 bit/sample */
2822 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2823 dynalloc_loop_logp = dynalloc_logp;
2825 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2828 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2829 tell = ec_tell_frac(dec);
2833 total_bits -= quanta;
2834 dynalloc_loop_logp = 1;
2837 /* Making dynalloc more likely */
2839 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2842 ALLOC(fine_quant, st->mode->nbEBands, int);
2843 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2844 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2846 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2847 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2848 bits -= anti_collapse_rsv;
2849 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2850 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2851 fine_quant, fine_priority, C, LM, dec, 0, 0);
2853 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2855 /* Decode fixed codebook */
2856 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2857 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2858 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2859 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2861 if (anti_collapse_rsv > 0)
2863 anti_collapse_on = ec_dec_bits(dec, 1);
2866 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2867 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2869 if (anti_collapse_on)
2870 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2871 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2873 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2877 for (i=0;i<C*st->mode->nbEBands;i++)
2880 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2884 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2886 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2888 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2891 for (i=0;i<M*st->mode->eBands[st->start];i++)
2895 int bound = M*st->mode->eBands[effEnd];
2896 if (st->downsample!=1)
2897 bound = IMIN(bound, N/st->downsample);
2898 for (i=bound;i<N;i++)
2902 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2904 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2909 freq[N+i] = freq[i];
2914 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2917 /* Compute inverse MDCTs */
2918 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2921 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2922 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2923 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2924 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2925 st->mode->window, st->overlap);
2927 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2928 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2929 st->mode->window, st->mode->overlap);
2932 st->postfilter_period_old = st->postfilter_period;
2933 st->postfilter_gain_old = st->postfilter_gain;
2934 st->postfilter_tapset_old = st->postfilter_tapset;
2935 st->postfilter_period = postfilter_pitch;
2936 st->postfilter_gain = postfilter_gain;
2937 st->postfilter_tapset = postfilter_tapset;
2940 st->postfilter_period_old = st->postfilter_period;
2941 st->postfilter_gain_old = st->postfilter_gain;
2942 st->postfilter_tapset_old = st->postfilter_tapset;
2946 for (i=0;i<st->mode->nbEBands;i++)
2947 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2950 /* In case start or end were to change */
2953 for (i=0;i<2*st->mode->nbEBands;i++)
2954 oldLogE2[i] = oldLogE[i];
2955 for (i=0;i<2*st->mode->nbEBands;i++)
2956 oldLogE[i] = oldBandE[i];
2957 for (i=0;i<2*st->mode->nbEBands;i++)
2958 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2960 for (i=0;i<2*st->mode->nbEBands;i++)
2961 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2965 for (i=0;i<st->start;i++)
2967 oldBandE[c*st->mode->nbEBands+i]=0;
2968 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2970 for (i=st->end;i<st->mode->nbEBands;i++)
2972 oldBandE[c*st->mode->nbEBands+i]=0;
2973 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2978 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2981 if (ec_tell(dec) > 8*len)
2982 return OPUS_INTERNAL_ERROR;
2983 if(ec_get_error(dec))
2985 return frame_size/st->downsample;
2992 int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
2994 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2997 #ifndef DISABLE_FLOAT_API
2998 int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
3001 VARDECL(opus_int16, out);
3005 return OPUS_BAD_ARG;
3010 ALLOC(out, C*N, opus_int16);
3011 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
3013 for (j=0;j<C*ret;j++)
3014 pcm[j]=out[j]*(1.f/32768.f);
3019 #endif /* DISABLE_FLOAT_API */
3023 int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
3025 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
3028 int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
3031 VARDECL(celt_sig, out);
3035 return OPUS_BAD_ARG;
3039 ALLOC(out, C*N, celt_sig);
3041 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
3044 for (j=0;j<C*ret;j++)
3045 pcm[j] = FLOAT2INT16 (out[j]);
3052 #endif /* CUSTOM_MODES */
3054 int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
3058 va_start(ap, request);
3061 case CELT_SET_START_BAND_REQUEST:
3063 opus_int32 value = va_arg(ap, opus_int32);
3064 if (value<0 || value>=st->mode->nbEBands)
3069 case CELT_SET_END_BAND_REQUEST:
3071 opus_int32 value = va_arg(ap, opus_int32);
3072 if (value<1 || value>st->mode->nbEBands)
3077 case CELT_SET_CHANNELS_REQUEST:
3079 opus_int32 value = va_arg(ap, opus_int32);
3080 if (value<1 || value>2)
3082 st->stream_channels = value;
3085 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
3087 opus_int32 *value = va_arg(ap, opus_int32*);
3094 case OPUS_GET_LOOKAHEAD_REQUEST:
3096 opus_int32 *value = va_arg(ap, opus_int32*);
3099 *value = st->overlap/st->downsample;
3102 case OPUS_RESET_STATE:
3105 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
3106 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
3107 oldBandE = lpc+st->channels*LPC_ORDER;
3108 oldLogE = oldBandE + 2*st->mode->nbEBands;
3109 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
3110 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
3111 opus_custom_decoder_get_size(st->mode, st->channels)-
3112 ((char*)&st->DECODER_RESET_START - (char*)st));
3113 for (i=0;i<2*st->mode->nbEBands;i++)
3114 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
3117 case OPUS_GET_PITCH_REQUEST:
3119 opus_int32 *value = va_arg(ap, opus_int32*);
3122 *value = st->postfilter_period;
3126 case CELT_GET_MODE_REQUEST:
3128 const CELTMode ** value = va_arg(ap, const CELTMode**);
3134 case CELT_SET_SIGNALLING_REQUEST:
3136 opus_int32 value = va_arg(ap, opus_int32);
3137 st->signalling = value;
3140 case OPUS_GET_FINAL_RANGE_REQUEST:
3142 opus_uint32 * value = va_arg(ap, opus_uint32 *);
3156 return OPUS_BAD_ARG;
3159 return OPUS_UNIMPLEMENTED;
3164 const char *opus_strerror(int error)
3166 static const char * const error_strings[8] = {
3172 "request not implemented",
3174 "memory allocation failed"
3176 if (error > 0 || error < -7)
3177 return "unknown error";
3179 return error_strings[-error];
3182 const char *opus_get_version_string(void)
3184 return "libopus " OPUS_VERSION