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;
195 celt_sig syn_mem[2][2*MAX_PERIOD];
198 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
199 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
200 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
201 /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
204 int celt_encoder_get_size(int channels)
206 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
207 return opus_custom_encoder_get_size(mode, channels);
210 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
212 int size = sizeof(struct CELTEncoder)
213 + (2*channels*mode->overlap-1)*sizeof(celt_sig)
214 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
215 + 3*channels*mode->nbEBands*sizeof(opus_val16);
220 CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
223 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
224 /* init will handle the NULL case */
225 ret = opus_custom_encoder_init(st, mode, channels);
228 opus_custom_encoder_destroy(st);
235 #endif /* CUSTOM_MODES */
237 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
240 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
243 st->upsample = resampling_factor(sampling_rate);
247 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
249 if (channels < 0 || channels > 2)
252 if (st==NULL || mode==NULL)
253 return OPUS_ALLOC_FAIL;
255 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
258 st->overlap = mode->overlap;
259 st->stream_channels = st->channels = channels;
263 st->end = st->mode->effEBands;
266 st->constrained_vbr = 1;
269 st->bitrate = OPUS_BITRATE_MAX;
275 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
281 void opus_custom_encoder_destroy(CELTEncoder *st)
285 #endif /* CUSTOM_MODES */
287 static inline opus_val16 SIG2WORD16(celt_sig x)
290 x = PSHR32(x, SIG_SHIFT);
291 x = MAX32(x, -32768);
295 return (opus_val16)x;
299 static int transient_analysis(const opus_val32 * restrict in, int len, int C,
300 int overlap, opus_val16 *tf_estimate, int *tf_chan, AnalysisInfo *analysis)
303 VARDECL(opus_val16, tmp);
304 opus_val32 mem0,mem1;
305 int is_transient = 0;
310 VARDECL(opus_val16, bins);
311 opus_val16 T1, T2, T3, T4, T5;
314 int fmetric=0, bmetric=0;
315 int count1, count2, count3, count4, count5;;
318 ALLOC(tmp, len, opus_val16);
322 ALLOC(bins, N, opus_val16);
330 tmp[i] = SHR32(in[i+c*len],SIG_SHIFT);
332 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
339 mem0 = mem1 + y - SHL32(x,1);
340 mem1 = x - SHR32(y,1);
342 mem0 = mem1 + y - 2*x;
345 tmp[i] = EXTRACT16(SHR(y,2));
347 /* First few samples are bad because we don't propagate the memory */
355 opus_val16 max_abs=0;
356 for (j=0;j<2*block;j++)
357 max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
359 maxbin = MAX16(maxbin, bins[i]);
362 T1 = QCONST16(.09f, 15);
363 T2 = QCONST16(.12f, 15);
364 T3 = QCONST16(.18f, 15);
365 T4 = QCONST16(.28f, 15);
366 T5 = QCONST16(.4f, 15);
369 count1=count2=count3=count4=count5=0;
372 follower = MAX16(bins[i], MULT16_16_Q15(QCONST16(0.97f, 15), follower));
373 if (bins[i] < MULT16_16_Q15(T1, follower))
375 if (bins[i] < MULT16_16_Q15(T2, follower))
377 if (bins[i] < MULT16_16_Q15(T3, follower))
379 if (bins[i] < MULT16_16_Q15(T4, follower))
381 if (bins[i] < MULT16_16_Q15(T5, follower))
384 fmetric = (5*count1 + 4*count2 + 3*count3 + 2*count4 + count5)/2;
386 count1=count2=count3=count4=count5=0;
389 follower = MAX16(bins[i], MULT16_16_Q15(QCONST16(0.97f, 15), follower));
390 if (bins[i] < MULT16_16_Q15(T1, follower))
392 if (bins[i] < MULT16_16_Q15(T2, follower))
394 if (bins[i] < MULT16_16_Q15(T3, follower))
396 if (bins[i] < MULT16_16_Q15(T4, follower))
398 if (bins[i] < MULT16_16_Q15(T5, follower))
401 bmetric = 5*count1 + 4*count2 + 3*count3 + 2*count4 + count5;
402 metric = fmetric+bmetric;
405 if (metric>20+50*MAX16(analysis->tonality, analysis->noisiness))
414 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
415 *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)));
419 is_transient = rand()&0x1;
421 /*printf("%d %f %f %f %f\n", is_transient, *tf_estimate, tf_max, analysis->tonality, analysis->noisiness);*/
425 /** Apply window and compute the MDCT for all sub-frames and
426 all channels in a frame */
427 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int C, int LM)
429 if (C==1 && !shortBlocks)
431 const int overlap = OVERLAP(mode);
432 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
434 const int overlap = OVERLAP(mode);
435 int N = mode->shortMdctSize<<LM;
440 N = mode->shortMdctSize;
446 /* Interleaving the sub-frames while doing the MDCTs */
447 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);
453 /** Compute the IMDCT and apply window for all sub-frames and
454 all channels in a frame */
455 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
456 celt_sig * restrict out_mem[],
457 celt_sig * restrict overlap_mem[], int C, int LM)
460 const int N = mode->shortMdctSize<<LM;
461 const int overlap = OVERLAP(mode);
462 VARDECL(opus_val32, x);
465 ALLOC(x, N+overlap, opus_val32);
474 N2 = mode->shortMdctSize;
477 /* Prevents problems from the imdct doing the overlap-add */
478 OPUS_CLEAR(x, overlap);
482 /* IMDCT on the interleaved the sub-frames */
483 clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
486 for (j=0;j<overlap;j++)
487 out_mem[c][j] = x[j] + overlap_mem[c][j];
489 out_mem[c][j] = x[j];
490 for (j=0;j<overlap;j++)
491 overlap_mem[c][j] = x[N+j];
496 static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
502 celt_sig * restrict x;
503 opus_val16 * restrict y;
509 celt_sig tmp = *x + m;
510 m = MULT16_32_Q15(coef[0], tmp)
511 - MULT16_32_Q15(coef[1], *x);
512 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
514 /* Technically the store could be moved outside of the if because
515 the stores we don't want will just be overwritten */
517 *y = SCALEOUT(SIG2WORD16(tmp));
518 if (++count==downsample)
528 static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
529 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
530 const opus_val16 *window, int overlap)
533 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
534 opus_val16 g00, g01, g02, g10, g11, g12;
535 static const opus_val16 gains[3][3] = {
536 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
537 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
538 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
539 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
540 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
541 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
542 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
543 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
544 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
545 for (i=0;i<overlap;i++)
548 f = MULT16_16_Q15(window[i],window[i]);
550 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
551 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
552 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
553 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
554 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
555 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
556 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
557 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
558 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
559 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
562 for (i=overlap;i<N;i++)
564 + MULT16_32_Q15(g10,x[i-T1])
565 + MULT16_32_Q15(g11,x[i-T1-1])
566 + MULT16_32_Q15(g11,x[i-T1+1])
567 + MULT16_32_Q15(g12,x[i-T1-2])
568 + MULT16_32_Q15(g12,x[i-T1+2]);
571 static const signed char tf_select_table[4][8] = {
572 {0, -1, 0, -1, 0,-1, 0,-1},
573 {0, -1, 0, -2, 1, 0, 1,-1},
574 {0, -2, 0, -3, 2, 0, 1,-1},
575 {0, -2, 0, -3, 3, 0, 1,-1},
578 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
584 L1 += EXTEND32(ABS16(tmp[i]));
585 /* When in doubt, prefer goo freq resolution */
586 L1 = MAC16_32_Q15(L1, LM*bias, L1);
591 static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
592 int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
593 int *tf_sum, opus_val16 tf_estimate, int tf_chan)
596 VARDECL(int, metric);
601 VARDECL(celt_norm, tmp);
607 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(1.5f,14)-tf_estimate));
608 /*printf("%f ", bias);*/
610 if (nbCompressedBytes<15*C)
614 tf_res[i] = isTransient;
617 if (nbCompressedBytes<40)
619 else if (nbCompressedBytes<60)
621 else if (nbCompressedBytes<100)
626 ALLOC(metric, len, int);
627 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
628 ALLOC(path0, len, int);
629 ALLOC(path1, len, int);
635 opus_val32 L1, best_L1;
637 N = (m->eBands[i+1]-m->eBands[i])<<LM;
639 tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)];
640 /* Just add the right channel if we're in stereo */
643 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
644 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
646 /*printf ("%f ", L1);*/
657 haar1(tmp, N>>(LM-k), 1<<(LM-k));
659 haar1(tmp, N>>k, 1<<k);
661 L1 = l1_metric(tmp, N, B, bias);
669 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
671 metric[i] = best_level;
673 metric[i] = -best_level;
674 /*printf("%d ", metric[i]);*/
675 *tf_sum += (isTransient ? LM : 0) - metric[i];
678 /* NOTE: Future optimized implementations could detect extreme transients and set
679 tf_select = 1 but so far we have not found a reliable way of making this useful */
683 cost1 = isTransient ? 0 : lambda;
684 /* Viterbi forward pass */
691 from1 = cost1 + lambda;
701 from0 = cost0 + lambda;
711 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
712 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
714 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
715 /* Viterbi backward pass to check the decisions */
716 for (i=len-2;i>=0;i--)
718 if (tf_res[i+1] == 1)
719 tf_res[i] = path1[i+1];
721 tf_res[i] = path0[i+1];
723 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
726 tf_select = rand()&0x1;
727 tf_res[0] = rand()&0x1;
729 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
734 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
742 budget = enc->storage*8;
744 logp = isTransient ? 2 : 4;
745 /* Reserve space to code the tf_select decision. */
746 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
747 budget -= tf_select_rsv;
748 curr = tf_changed = 0;
749 for (i=start;i<end;i++)
751 if (tell+logp<=budget)
753 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
760 logp = isTransient ? 4 : 5;
762 /* Only code tf_select if it would actually make a difference. */
764 tf_select_table[LM][4*isTransient+0+tf_changed]!=
765 tf_select_table[LM][4*isTransient+2+tf_changed])
766 ec_enc_bit_logp(enc, tf_select, 1);
769 for (i=start;i<end;i++)
770 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
771 /*for(i=0;i<end;i++)printf("%d ", isTransient ? LM-tf_res[i] : -tf_res[i]);printf("\n");*/
774 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
776 int i, curr, tf_select;
783 budget = dec->storage*8;
785 logp = isTransient ? 2 : 4;
786 tf_select_rsv = LM>0 && tell+logp+1<=budget;
787 budget -= tf_select_rsv;
788 tf_changed = curr = 0;
789 for (i=start;i<end;i++)
791 if (tell+logp<=budget)
793 curr ^= ec_dec_bit_logp(dec, logp);
798 logp = isTransient ? 4 : 5;
802 tf_select_table[LM][4*isTransient+0+tf_changed] !=
803 tf_select_table[LM][4*isTransient+2+tf_changed])
805 tf_select = ec_dec_bit_logp(dec, 1);
807 for (i=start;i<end;i++)
809 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
813 static void init_caps(const CELTMode *m,int *cap,int LM,int C)
816 for (i=0;i<m->nbEBands;i++)
819 N=(m->eBands[i+1]-m->eBands[i])<<LM;
820 cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
824 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
825 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
826 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
833 opus_val16 trim = QCONST16(5.f, 8);
834 opus_val16 logXC, logXC2;
837 opus_val16 sum = 0; /* Q10 */
838 opus_val16 minXC; /* Q10 */
839 /* Compute inter-channel correlation for low frequencies */
843 opus_val32 partial = 0;
844 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
845 partial = MAC16_16(partial, X[j], X[N0+j]);
846 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
848 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
850 for (i=8;i<intensity;i++)
853 opus_val32 partial = 0;
854 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
855 partial = MAC16_16(partial, X[j], X[N0+j]);
856 minXC = MIN16(minXC, EXTRACT16(SHR32(partial, 18)));
858 /*printf ("%f\n", sum);*/
859 if (sum > QCONST16(.995f,10))
861 else if (sum > QCONST16(.92f,10))
863 else if (sum > QCONST16(.85f,10))
865 else if (sum > QCONST16(.8f,10))
867 /* mid-side savings estimations based on the LF average*/
868 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
869 /* mid-side savings estimations based on min correlation */
870 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
872 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
873 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
874 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
877 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
878 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
881 /* Estimate spectral tilt */
883 for (i=0;i<end-1;i++)
885 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
889 /*printf("%f\n", diff);*/
890 if (diff > QCONST16(2.f, DB_SHIFT))
892 if (diff > QCONST16(8.f, DB_SHIFT))
894 if (diff < -QCONST16(4.f, DB_SHIFT))
896 if (diff < -QCONST16(10.f, DB_SHIFT))
898 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
899 trim -= 2*SHR16(tf_estimate-QCONST16(1.f,14), 14-8);
903 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), 2*(analysis->tonality_slope+.05)));
908 trim_index = PSHR32(trim, 8);
910 trim_index = floor(.5+trim);
916 /*printf("%d\n", trim_index);*/
918 trim_index = rand()%11;
923 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
928 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
930 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
934 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
936 opus_val32 L, R, M, S;
937 /* We cast to 32-bit first because of the -32768 case */
939 R = EXTEND32(X[N0+j]);
942 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
943 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
946 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
948 /* We don't need thetas for lower bands with LM<=1 */
951 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
952 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
955 int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
960 VARDECL(celt_sig, in);
961 VARDECL(celt_sig, freq);
962 VARDECL(celt_norm, X);
963 VARDECL(celt_ener, bandE);
964 VARDECL(opus_val16, bandLogE);
965 VARDECL(opus_val16, bandLogE2);
966 VARDECL(int, fine_quant);
967 VARDECL(opus_val16, error);
968 VARDECL(int, pulses);
970 VARDECL(int, offsets);
971 VARDECL(int, fine_priority);
972 VARDECL(int, tf_res);
973 VARDECL(unsigned char, collapse_masks);
974 celt_sig *prefilter_mem;
975 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
978 const int CC = st->channels;
979 const int C = st->stream_channels;
982 int nbFilledBytes, nbAvailableBytes;
987 int pitch_index=COMBFILTER_MINPERIOD;
988 opus_val16 gain1 = 0;
992 opus_val16 pf_threshold;
995 opus_int32 total_bits;
996 opus_int32 total_boost;
999 int prefilter_tapset=0;
1001 int anti_collapse_rsv;
1002 int anti_collapse_on=0;
1005 opus_val16 tf_estimate;
1007 opus_int32 tot_boost=0;
1008 opus_val16 sample_max;
1009 opus_val16 maxDepth;
1012 tf_estimate = QCONST16(1.0f,14);
1013 if (nbCompressedBytes<2 || pcm==NULL)
1014 return OPUS_BAD_ARG;
1016 frame_size *= st->upsample;
1017 for (LM=0;LM<=st->mode->maxLM;LM++)
1018 if (st->mode->shortMdctSize<<LM==frame_size)
1020 if (LM>st->mode->maxLM)
1021 return OPUS_BAD_ARG;
1023 N = M*st->mode->shortMdctSize;
1025 prefilter_mem = st->in_mem+CC*(st->overlap);
1026 oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
1027 oldLogE = oldBandE + CC*st->mode->nbEBands;
1028 oldLogE2 = oldLogE + CC*st->mode->nbEBands;
1036 nbFilledBytes=(tell+4)>>3;
1040 if (st->signalling && enc==NULL)
1042 int tmp = (st->mode->effEBands-st->end)>>1;
1043 st->end = IMAX(1, st->mode->effEBands-tmp);
1044 compressed[0] = tmp<<5;
1045 compressed[0] |= LM<<3;
1046 compressed[0] |= (C==2)<<2;
1047 /* Convert "standard mode" to Opus header */
1048 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
1050 int c0 = toOpus(compressed[0]);
1052 return OPUS_BAD_ARG;
1056 nbCompressedBytes--;
1059 celt_assert(st->signalling==0);
1062 /* Can't produce more than 1275 output bytes */
1063 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1064 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1066 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1068 opus_int32 den=st->mode->Fs>>BITRES;
1069 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1072 vbr_rate -= 8<<BITRES;
1074 effectiveBytes = vbr_rate>>(3+BITRES);
1078 tmp = st->bitrate*frame_size;
1081 if (st->bitrate!=OPUS_BITRATE_MAX)
1082 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1083 (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
1084 effectiveBytes = nbCompressedBytes;
1089 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1095 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1096 target rate and buffering.
1097 We must do this up front so that bust-prevention logic triggers
1098 correctly if we don't have enough bits. */
1099 if (st->constrained_vbr)
1101 opus_int32 vbr_bound;
1102 opus_int32 max_allowed;
1103 /* We could use any multiple of vbr_rate as bound (depending on the
1105 This is clamped to ensure we use at least two bytes if the encoder
1106 was entirely empty, but to allow 0 in hybrid mode. */
1107 vbr_bound = vbr_rate;
1108 max_allowed = IMIN(IMAX(tell==1?2:0,
1109 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1111 if(max_allowed < nbAvailableBytes)
1113 nbCompressedBytes = nbFilledBytes+max_allowed;
1114 nbAvailableBytes = max_allowed;
1115 ec_enc_shrink(enc, nbCompressedBytes);
1119 total_bits = nbCompressedBytes*8;
1122 if (effEnd > st->mode->effEBands)
1123 effEnd = st->mode->effEBands;
1125 ALLOC(in, CC*(N+st->overlap), celt_sig);
1127 sample_max=MAX16(st->overlap_max, celt_maxabs16(pcm, C*(N-st->mode->overlap)));
1128 st->overlap_max=celt_maxabs16(pcm+C*(N-st->mode->overlap), C*st->mode->overlap);
1129 sample_max=MAX16(sample_max, st->overlap_max);
1130 /* Find pitch period and gain */
1132 VARDECL(celt_sig, _pre);
1135 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1138 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1143 const opus_val16 * restrict pcmp = pcm+c;
1144 celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
1155 x = MAX32(-65536.f, MIN32(65536.f,x));
1157 if (++count==st->upsample)
1164 /* Apply pre-emphasis */
1165 tmp = MULT16_16(st->mode->preemph[2], x);
1166 *inp = tmp + st->preemph_memE[c];
1167 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
1168 - MULT16_32_Q15(st->mode->preemph[0], tmp);
1171 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1172 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1176 silence = (sample_max==0);
1178 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1181 if ((rand()&0x3F)==0)
1185 ec_enc_bit_logp(enc, silence, 15);
1190 /*In VBR mode there is no need to send more than the minimum. */
1193 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1194 total_bits=nbCompressedBytes*8;
1196 ec_enc_shrink(enc, nbCompressedBytes);
1198 /* Pretend we've filled all the remaining bits with zeros
1199 (that's what the initialiser did anyway) */
1200 tell = nbCompressedBytes*8;
1201 enc->nbits_total+=tell-ec_tell(enc);
1203 if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
1205 VARDECL(opus_val16, pitch_buf);
1206 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1208 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1209 /* Don't search for the fir last 1.5 octave of the range because
1210 there's too many false-positives due to short-term correlation */
1211 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1212 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index);
1213 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1215 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1216 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1217 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1218 pitch_index = COMBFILTER_MAXPERIOD-2;
1219 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1220 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && st->analysis.tonality > .3
1221 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
1223 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1224 if (st->loss_rate>2)
1225 gain1 = HALF32(gain1);
1226 if (st->loss_rate>4)
1227 gain1 = HALF32(gain1);
1228 if (st->loss_rate>8)
1230 prefilter_tapset = st->tapset_decision;
1235 /* Gain threshold for enabling the prefilter/postfilter */
1236 pf_threshold = QCONST16(.2f,15);
1238 /* Adjusting the threshold based on rate and continuity */
1239 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1240 pf_threshold += QCONST16(.2f,15);
1241 if (nbAvailableBytes<25)
1242 pf_threshold += QCONST16(.1f,15);
1243 if (nbAvailableBytes<35)
1244 pf_threshold += QCONST16(.1f,15);
1245 if (st->prefilter_gain > QCONST16(.4f,15))
1246 pf_threshold -= QCONST16(.1f,15);
1247 if (st->prefilter_gain > QCONST16(.55f,15))
1248 pf_threshold -= QCONST16(.1f,15);
1250 /* Hard threshold at 0.2 */
1251 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1252 if (gain1<pf_threshold)
1254 if(st->start==0 && tell+16<=total_bits)
1255 ec_enc_bit_logp(enc, 0, 1);
1259 /*This block is not gated by a total bits check only because
1260 of the nbAvailableBytes check above.*/
1264 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1265 gain1=st->prefilter_gain;
1268 qg = ((gain1+1536)>>10)/3-1;
1270 qg = (int)floor(.5f+gain1*32/3)-1;
1272 qg = IMAX(0, IMIN(7, qg));
1273 ec_enc_bit_logp(enc, 1, 1);
1275 octave = EC_ILOG(pitch_index)-5;
1276 ec_enc_uint(enc, octave, 6);
1277 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1279 ec_enc_bits(enc, qg, 3);
1280 if (ec_tell(enc)+2<=total_bits)
1281 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1283 prefilter_tapset = 0;
1284 gain1 = QCONST16(0.09375f,15)*(qg+1);
1287 /*printf("%d %f\n", pitch_index, gain1);*/
1290 int offset = st->mode->shortMdctSize-st->mode->overlap;
1291 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1292 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1294 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1295 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1296 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1298 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1299 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1300 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1301 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1303 if (N>COMBFILTER_MAXPERIOD)
1305 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1307 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1308 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1317 if (LM>0 && ec_tell(enc)+3<=total_bits)
1319 if (st->complexity > 1)
1321 isTransient = transient_analysis(in, N+st->overlap, CC,
1322 st->overlap, &tf_estimate, &tf_chan, &st->analysis);
1326 ec_enc_bit_logp(enc, isTransient, 3);
1329 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1330 ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
1331 ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
1333 compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
1338 freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
1341 if (st->upsample != 1)
1345 int bound = N/st->upsample;
1346 for (i=0;i<bound;i++)
1347 freq[c*N+i] *= st->upsample;
1352 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1354 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1355 /*for (i=0;i<21;i++)
1356 printf("%f ", bandLogE[i]);
1359 ALLOC(bandLogE2, C*st->mode->nbEBands, opus_val16);
1362 VARDECL(celt_sig, freq2);
1363 VARDECL(opus_val32, bandE2);
1364 ALLOC(freq2, CC*N, celt_sig);
1365 compute_mdcts(st->mode, 0, in, freq2, CC, LM);
1369 freq2[i] = ADD32(HALF32(freq2[i]), HALF32(freq2[N+i]));
1371 if (st->upsample != 1)
1375 int bound = N/st->upsample;
1376 for (i=0;i<bound;i++)
1377 freq2[c*N+i] *= st->upsample;
1382 ALLOC(bandE2, C*st->mode->nbEBands, opus_val32);
1383 compute_band_energies(st->mode, freq2, bandE2, effEnd, C, M);
1384 amp2Log2(st->mode, effEnd, st->end, bandE2, bandLogE2, C);
1385 for (i=0;i<C*st->mode->nbEBands;i++)
1386 bandLogE2[i] += HALF16(SHL(LM, DB_SHIFT));
1388 for (i=0;i<C*st->mode->nbEBands;i++)
1389 bandLogE2[i] = bandLogE[i];
1392 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1394 /* Band normalisation */
1395 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1397 ALLOC(tf_res, st->mode->nbEBands, int);
1398 tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum, tf_estimate, tf_chan);
1399 for (i=effEnd;i<st->end;i++)
1400 tf_res[i] = tf_res[effEnd-1];
1402 ALLOC(error, C*st->mode->nbEBands, opus_val16);
1403 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1404 oldBandE, total_bits, error, enc,
1405 C, LM, nbAvailableBytes, st->force_intra,
1406 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1408 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1410 if (ec_tell(enc)+4<=total_bits)
1412 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1414 if (st->complexity == 0)
1415 st->spread_decision = SPREAD_NONE;
1417 if (st->analysis.valid)
1419 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1420 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1421 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1422 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1423 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1424 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1426 st->spread_decision = spreading_decision(st->mode, X,
1427 &st->tonal_average, st->spread_decision, &st->hf_average,
1428 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1430 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1431 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1433 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1436 ALLOC(cap, st->mode->nbEBands, int);
1437 ALLOC(offsets, st->mode->nbEBands, int);
1439 init_caps(st->mode,cap,LM,C);
1440 for (i=0;i<st->mode->nbEBands;i++)
1442 /* Dynamic allocation code */
1443 maxDepth=-QCONST16(32.f, DB_SHIFT);
1444 /* Make sure that dynamic allocation can't make us bust the budget */
1445 if (effectiveBytes > 50 && LM>=1)
1448 VARDECL(opus_val16, follower);
1449 ALLOC(follower, C*st->mode->nbEBands, opus_val16);
1452 follower[c*st->mode->nbEBands] = bandLogE2[c*st->mode->nbEBands];
1453 for (i=1;i<st->end;i++)
1455 /* The last band to be at least 3 dB higher than the previous one
1456 is the last we'll consider. Otherwise, we run into problems on
1457 bandlimited signals. */
1458 if (bandLogE2[c*st->mode->nbEBands+i] > bandLogE2[c*st->mode->nbEBands+i-1]+QCONST16(.5f,DB_SHIFT))
1460 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]);
1462 for (i=last-1;i>=0;i--)
1463 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]));
1464 for (i=0;i<st->end;i++)
1466 opus_val16 noise_floor;
1467 /* Noise floor must take into account eMeans, the depth, the width of the bands
1468 and the preemphasis filter (approx. square of bark band ID) */
1469 noise_floor = MULT16_16(QCONST16(0.0625f, DB_SHIFT),st->mode->logN[i])
1470 +QCONST16(.5f,DB_SHIFT)+SHL16(9-st->lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6)
1471 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5));
1472 follower[c*st->mode->nbEBands+i] = MAX16(follower[c*st->mode->nbEBands+i], noise_floor);
1473 maxDepth = MAX16(maxDepth, bandLogE[i]-noise_floor);
1478 for (i=st->start;i<st->end;i++)
1480 /* Consider 24 dB "cross-talk" */
1481 follower[st->mode->nbEBands+i] = MAX16(follower[st->mode->nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT));
1482 follower[ i] = MAX16(follower[ i], follower[st->mode->nbEBands+i]-QCONST16(4.f,DB_SHIFT));
1483 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[st->mode->nbEBands+i]-follower[st->mode->nbEBands+i]));
1486 for (i=st->start;i<st->end;i++)
1488 follower[i] = MAX16(0, bandLogE[i]-follower[i]);
1491 for (i=st->start;i<st->end;i++)
1499 follower[i] = HALF16(follower[i]);
1500 follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT));
1501 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1504 boost = SHR32(EXTEND32(follower[i]),DB_SHIFT);
1505 tot_boost += boost*width<<BITRES;
1506 } else if (width > 48) {
1507 boost = SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
1508 tot_boost += (boost*width<<BITRES)/8;
1510 boost = SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
1511 tot_boost += boost*6<<BITRES;
1517 if (0 && st->analysis.valid)
1519 if (st->analysis.boost_amount[0]>.2)
1520 offsets[st->analysis.boost_band[0]]+=2;
1521 if (st->analysis.boost_amount[0]>.4)
1522 offsets[st->analysis.boost_band[0]]+=2;
1523 if (st->analysis.boost_amount[1]>.2)
1524 offsets[st->analysis.boost_band[1]]+=2;
1525 if (st->analysis.boost_amount[1]>.4)
1526 offsets[st->analysis.boost_band[1]]+=2;
1530 total_bits<<=BITRES;
1532 tell = ec_tell_frac(enc);
1533 for (i=st->start;i<st->end;i++)
1536 int dynalloc_loop_logp;
1539 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1540 /* quanta is 6 bits, but no more than 1 bit/sample
1541 and no less than 1/8 bit/sample */
1542 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1543 dynalloc_loop_logp = dynalloc_logp;
1545 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1546 && boost < cap[i]; j++)
1549 flag = j<offsets[i];
1550 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1551 tell = ec_tell_frac(enc);
1555 total_boost += quanta;
1556 dynalloc_loop_logp = 1;
1558 /* Making dynalloc more likely */
1560 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1568 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1570 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1572 /* Account for coarse energy */
1573 effectiveRate = (8*effectiveBytes - 80)>>LM;
1575 /* effectiveRate in kb/s */
1576 effectiveRate = 2*effectiveRate/5;
1577 if (effectiveRate<35)
1579 else if (effectiveRate<50)
1581 else if (effectiveRate<68)
1583 else if (effectiveRate<84)
1585 else if (effectiveRate<102)
1587 else if (effectiveRate<130)
1591 intensity = IMIN(st->end,IMAX(st->start, intensity));
1595 if (tell+(6<<BITRES) <= total_bits - total_boost)
1597 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1598 st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, intensity);
1599 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1600 tell = ec_tell_frac(enc);
1603 /* Variable bitrate */
1608 /* The target rate in 8th bits per frame */
1609 opus_int32 target, new_target;
1610 opus_int32 min_allowed;
1613 int lm_diff = st->mode->maxLM - LM;
1614 coded_bands = st->lastCodedBands ? st->lastCodedBands : st->mode->nbEBands;
1615 coded_bins = st->mode->eBands[coded_bands]<<LM;
1617 coded_bins += st->mode->eBands[IMIN(intensity, coded_bands)]<<LM;
1619 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1620 The CELT allocator will just not be able to use more than that anyway. */
1621 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1622 target = vbr_rate - ((40*C+20)<<BITRES);
1623 if (st->constrained_vbr)
1624 target += (st->vbr_offset>>lm_diff);
1626 /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1628 if (st->analysis.valid && st->analysis.activity<.4)
1629 target -= (coded_bins<<BITRES)*1*(.4-st->analysis.activity);
1633 int coded_stereo_bands;
1634 int coded_stereo_dof;
1635 coded_stereo_bands = IMIN(intensity, coded_bands);
1636 coded_stereo_dof = (st->mode->eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1637 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1638 target -= MIN32(target/3, SHR16(MULT16_16(st->stereo_saving,(coded_stereo_dof<<BITRES)),8));
1639 target += MULT16_16_Q15(QCONST16(0.035,15),coded_stereo_dof<<BITRES);
1641 /* Compensates for the average tonality boost */
1642 target -= MULT16_16_Q15(QCONST16(0.13f,15),coded_bins<<BITRES);
1643 /* Limits starving of other bands when using dynalloc */
1644 target += tot_boost;
1645 /* Compensates for the average transient boost */
1646 target = MULT16_32_Q15(QCONST16(0.96f,15),target);
1648 new_target = SHL32(MULT16_32_Q15(tf_estimate, target),1);
1651 if (st->analysis.valid) {
1654 tonal = MAX16(0,st->analysis.tonality-.2);
1655 tonal_target = new_target + (coded_bins<<BITRES)*2.0f*tonal;
1657 tonal_target += (coded_bins<<BITRES)*.8;
1658 /*printf("%f %f ", st->analysis.tonality, tonal);*/
1659 new_target = IMAX(tonal_target,new_target);
1664 opus_int32 floor_depth;
1666 bins = st->mode->eBands[st->mode->nbEBands-2]<<LM;
1667 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1668 floor_depth = SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT);
1669 floor_depth = IMAX(floor_depth, new_target>>2);
1670 new_target = IMIN(new_target, floor_depth);
1671 /*printf("%f %d\n", maxDepth, floor_depth);*/
1673 /* The current offset is removed from the target and the space used
1675 target=new_target+tell;
1676 /* In VBR mode the frame size must not be reduced so much that it would
1677 result in the encoder running out of bits.
1678 The margin of 2 bytes ensures that none of the bust-prevention logic
1679 in the decoder will have triggered so far. */
1680 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1682 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1683 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1684 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1686 /* By how much did we "miss" the target on that frame */
1687 delta = target - vbr_rate;
1689 target=nbAvailableBytes<<(BITRES+3);
1691 /*If the frame is silent we don't adjust our drift, otherwise
1692 the encoder will shoot to very high rates after hitting a
1693 span of silence, but we do allow the bitres to refill.
1694 This means that we'll undershoot our target in CVBR/VBR modes
1695 on files with lots of silence. */
1698 nbAvailableBytes = 2;
1699 target = 2*8<<BITRES;
1703 if (st->vbr_count < 970)
1706 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1708 alpha = QCONST16(.001f,15);
1709 /* How many bits have we used in excess of what we're allowed */
1710 if (st->constrained_vbr)
1711 st->vbr_reservoir += target - vbr_rate;
1712 /*printf ("%d\n", st->vbr_reservoir);*/
1714 /* Compute the offset we need to apply in order to reach the target */
1715 if (st->constrained_vbr)
1717 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1718 st->vbr_offset = -st->vbr_drift;
1720 /*printf ("%d\n", st->vbr_drift);*/
1722 if (st->constrained_vbr && st->vbr_reservoir < 0)
1724 /* We're under the min value -- increase rate */
1725 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1726 /* Unless we're just coding silence */
1727 nbAvailableBytes += silence?0:adjust;
1728 st->vbr_reservoir = 0;
1729 /*printf ("+%d\n", adjust);*/
1731 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1732 /*printf("%d\n", nbCompressedBytes*50*8);*/
1733 /* This moves the raw bits to take into account the new compressed size */
1734 ec_enc_shrink(enc, nbCompressedBytes);
1737 /* Bit allocation */
1738 ALLOC(fine_quant, st->mode->nbEBands, int);
1739 ALLOC(pulses, st->mode->nbEBands, int);
1740 ALLOC(fine_priority, st->mode->nbEBands, int);
1742 /* bits = packet size - where we are - safety*/
1743 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1744 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1745 bits -= anti_collapse_rsv;
1746 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
1747 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
1748 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
1749 st->lastCodedBands = codedBands;
1751 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1753 #ifdef MEASURE_NORM_MSE
1758 X0[i+c*N] = X[i+c*N];
1760 for (i=0;i<C*st->mode->nbEBands;i++)
1761 bandE0[i] = bandE[i];
1764 /* Residual quantisation */
1765 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
1766 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1767 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
1768 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1770 if (anti_collapse_rsv > 0)
1772 anti_collapse_on = st->consec_transient<2;
1774 anti_collapse_on = rand()&0x1;
1776 ec_enc_bits(enc, anti_collapse_on, 1);
1778 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1782 for (i=0;i<C*st->mode->nbEBands;i++)
1783 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1787 /* Re-synthesis of the coded audio if required */
1789 celt_sig *out_mem[2];
1790 celt_sig *overlap_mem[2];
1792 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1795 for (i=0;i<C*st->mode->nbEBands;i++)
1799 #ifdef MEASURE_NORM_MSE
1800 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1802 if (anti_collapse_on)
1804 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1805 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1809 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1811 OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1813 OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1816 for (i=0;i<M*st->mode->eBands[st->start];i++)
1820 for (i=M*st->mode->eBands[st->end];i<N;i++)
1827 freq[N+i] = freq[i];
1830 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1832 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1834 overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
1836 overlap_mem[1] = overlap_mem[0] + st->overlap;
1838 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
1841 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1842 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1843 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
1844 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1845 st->mode->window, st->overlap);
1847 comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
1848 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1849 st->mode->window, st->mode->overlap);
1852 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
1853 st->prefilter_period_old = st->prefilter_period;
1854 st->prefilter_gain_old = st->prefilter_gain;
1855 st->prefilter_tapset_old = st->prefilter_tapset;
1859 st->prefilter_period = pitch_index;
1860 st->prefilter_gain = gain1;
1861 st->prefilter_tapset = prefilter_tapset;
1865 st->prefilter_period_old = st->prefilter_period;
1866 st->prefilter_gain_old = st->prefilter_gain;
1867 st->prefilter_tapset_old = st->prefilter_tapset;
1872 for (i=0;i<st->mode->nbEBands;i++)
1873 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
1878 for (i=0;i<CC*st->mode->nbEBands;i++)
1879 oldLogE2[i] = oldLogE[i];
1880 for (i=0;i<CC*st->mode->nbEBands;i++)
1881 oldLogE[i] = oldBandE[i];
1883 for (i=0;i<CC*st->mode->nbEBands;i++)
1884 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1886 /* In case start or end were to change */
1889 for (i=0;i<st->start;i++)
1891 oldBandE[c*st->mode->nbEBands+i]=0;
1892 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1894 for (i=st->end;i<st->mode->nbEBands;i++)
1896 oldBandE[c*st->mode->nbEBands+i]=0;
1897 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1902 st->consec_transient++;
1904 st->consec_transient=0;
1907 /* If there's any room left (can only happen for very high rates),
1908 it's already filled with zeros */
1913 nbCompressedBytes++;
1917 if (ec_get_error(enc))
1918 return OPUS_INTERNAL_ERROR;
1920 return nbCompressedBytes;
1927 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1929 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1932 #ifndef DISABLE_FLOAT_API
1933 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1936 VARDECL(opus_int16, in);
1940 return OPUS_BAD_ARG;
1944 ALLOC(in, C*N, opus_int16);
1947 in[j] = FLOAT2INT16(pcm[j]);
1949 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1952 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1957 #endif /* DISABLE_FLOAT_API */
1960 int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1963 VARDECL(celt_sig, in);
1967 return OPUS_BAD_ARG;
1971 ALLOC(in, C*N, celt_sig);
1972 for (j=0;j<C*N;j++) {
1973 in[j] = SCALEOUT(pcm[j]);
1976 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1979 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1985 int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1987 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1992 #endif /* CUSTOM_MODES */
1994 int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1998 va_start(ap, request);
2001 case OPUS_SET_COMPLEXITY_REQUEST:
2003 int value = va_arg(ap, opus_int32);
2004 if (value<0 || value>10)
2006 st->complexity = value;
2009 case CELT_SET_START_BAND_REQUEST:
2011 opus_int32 value = va_arg(ap, opus_int32);
2012 if (value<0 || value>=st->mode->nbEBands)
2017 case CELT_SET_END_BAND_REQUEST:
2019 opus_int32 value = va_arg(ap, opus_int32);
2020 if (value<1 || value>st->mode->nbEBands)
2025 case CELT_SET_PREDICTION_REQUEST:
2027 int value = va_arg(ap, opus_int32);
2028 if (value<0 || value>2)
2030 st->disable_pf = value<=1;
2031 st->force_intra = value==0;
2034 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2036 int value = va_arg(ap, opus_int32);
2037 if (value<0 || value>100)
2039 st->loss_rate = value;
2042 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2044 opus_int32 value = va_arg(ap, opus_int32);
2045 st->constrained_vbr = value;
2048 case OPUS_SET_VBR_REQUEST:
2050 opus_int32 value = va_arg(ap, opus_int32);
2054 case OPUS_SET_BITRATE_REQUEST:
2056 opus_int32 value = va_arg(ap, opus_int32);
2057 if (value<=500 && value!=OPUS_BITRATE_MAX)
2059 value = IMIN(value, 260000*st->channels);
2060 st->bitrate = value;
2063 case CELT_SET_CHANNELS_REQUEST:
2065 opus_int32 value = va_arg(ap, opus_int32);
2066 if (value<1 || value>2)
2068 st->stream_channels = value;
2071 case OPUS_SET_LSB_DEPTH_REQUEST:
2073 opus_int32 value = va_arg(ap, opus_int32);
2074 if (value<8 || value>24)
2076 st->lsb_depth=value;
2079 case OPUS_GET_LSB_DEPTH_REQUEST:
2081 opus_int32 *value = va_arg(ap, opus_int32*);
2082 *value=st->lsb_depth;
2085 case OPUS_RESET_STATE:
2088 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
2089 oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD));
2090 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2091 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2092 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2093 opus_custom_encoder_get_size(st->mode, st->channels)-
2094 ((char*)&st->ENCODER_RESET_START - (char*)st));
2095 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2096 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2098 st->delayedIntra = 1;
2099 st->spread_decision = SPREAD_NORMAL;
2100 st->tonal_average = 256;
2102 st->tapset_decision = 0;
2106 case CELT_SET_INPUT_CLIPPING_REQUEST:
2108 opus_int32 value = va_arg(ap, opus_int32);
2113 case CELT_SET_SIGNALLING_REQUEST:
2115 opus_int32 value = va_arg(ap, opus_int32);
2116 st->signalling = value;
2119 case CELT_SET_ANALYSIS_REQUEST:
2121 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2123 OPUS_COPY(&st->analysis, info, 1);
2126 case CELT_GET_MODE_REQUEST:
2128 const CELTMode ** value = va_arg(ap, const CELTMode**);
2134 case OPUS_GET_FINAL_RANGE_REQUEST:
2136 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2149 return OPUS_BAD_ARG;
2152 return OPUS_UNIMPLEMENTED;
2155 /**********************************************************************/
2159 /**********************************************************************/
2160 #define DECODE_BUFFER_SIZE 2048
2163 @brief Decoder state
2165 struct OpusCustomDecoder {
2166 const OpusCustomMode *mode;
2169 int stream_channels;
2175 /* Everything beyond this point gets cleared on a reset */
2176 #define DECODER_RESET_START rng
2180 int last_pitch_index;
2182 int postfilter_period;
2183 int postfilter_period_old;
2184 opus_val16 postfilter_gain;
2185 opus_val16 postfilter_gain_old;
2186 int postfilter_tapset;
2187 int postfilter_tapset_old;
2189 celt_sig preemph_memD[2];
2191 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
2192 /* opus_val16 lpc[], Size = channels*LPC_ORDER */
2193 /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
2194 /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
2195 /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
2196 /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
2199 int celt_decoder_get_size(int channels)
2201 const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
2202 return opus_custom_decoder_get_size(mode, channels);
2205 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
2207 int size = sizeof(struct CELTDecoder)
2208 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
2209 + channels*LPC_ORDER*sizeof(opus_val16)
2210 + 4*2*mode->nbEBands*sizeof(opus_val16);
2215 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
2218 CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
2219 ret = opus_custom_decoder_init(st, mode, channels);
2222 opus_custom_decoder_destroy(st);
2229 #endif /* CUSTOM_MODES */
2231 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
2234 ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
2237 st->downsample = resampling_factor(sampling_rate);
2238 if (st->downsample==0)
2239 return OPUS_BAD_ARG;
2244 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
2246 if (channels < 0 || channels > 2)
2247 return OPUS_BAD_ARG;
2250 return OPUS_ALLOC_FAIL;
2252 OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
2255 st->overlap = mode->overlap;
2256 st->stream_channels = st->channels = channels;
2260 st->end = st->mode->effEBands;
2265 opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
2271 void opus_custom_decoder_destroy(CELTDecoder *st)
2275 #endif /* CUSTOM_MODES */
2277 static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)
2281 int overlap = st->mode->overlap;
2282 opus_val16 fade = Q15ONE;
2284 const int C = st->channels;
2286 celt_sig *out_mem[2];
2287 celt_sig *decode_mem[2];
2288 celt_sig *overlap_mem[2];
2290 opus_val32 *out_syn[2];
2291 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2295 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2296 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2297 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2299 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2300 oldBandE = lpc+C*LPC_ORDER;
2301 oldLogE = oldBandE + 2*st->mode->nbEBands;
2302 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2303 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2305 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2307 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2309 len = N+st->mode->overlap;
2311 if (st->loss_count >= 5 || st->start!=0)
2313 /* Noise-based PLC/CNG */
2314 VARDECL(celt_sig, freq);
2315 VARDECL(celt_norm, X);
2316 VARDECL(celt_ener, bandE);
2321 if (effEnd > st->mode->effEBands)
2322 effEnd = st->mode->effEBands;
2324 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2325 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2326 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2328 if (st->loss_count >= 5)
2329 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
2332 opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
2335 for (i=st->start;i<st->end;i++)
2336 oldBandE[c*st->mode->nbEBands+i] -= decay;
2338 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2343 for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
2345 for (i=st->start;i<st->mode->effEBands;i++)
2350 boffs = N*c+(st->mode->eBands[i]<<LM);
2351 blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2352 for (j=0;j<blen;j++)
2354 seed = celt_lcg_rand(seed);
2355 X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
2357 renormalise_vector(X+boffs, blen, Q15ONE);
2359 for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
2364 denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
2367 for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
2371 int bound = st->mode->eBands[effEnd]<<LM;
2372 if (st->downsample!=1)
2373 bound = IMIN(bound, N/st->downsample);
2374 for (i=bound;i<N;i++)
2377 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
2379 /* Pitch-based PLC */
2380 if (st->loss_count == 0)
2382 opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
2383 /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
2384 search by using only part of the decode buffer */
2386 pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
2387 /* Max pitch is 100 samples (480 Hz) */
2388 pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
2389 poffset-100, &pitch_index);
2390 pitch_index = poffset-pitch_index;
2391 st->last_pitch_index = pitch_index;
2393 pitch_index = st->last_pitch_index;
2394 fade = QCONST16(.8f,15);
2398 VARDECL(opus_val32, e);
2399 opus_val16 exc[MAX_PERIOD];
2400 opus_val32 ac[LPC_ORDER+1];
2401 opus_val16 decay = 1;
2403 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};
2405 ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
2407 offset = MAX_PERIOD-pitch_index;
2408 for (i=0;i<MAX_PERIOD;i++)
2409 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
2411 if (st->loss_count == 0)
2413 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
2414 LPC_ORDER, MAX_PERIOD);
2416 /* Noise floor -40 dB */
2418 ac[0] += SHR32(ac[0],13);
2423 for (i=1;i<=LPC_ORDER;i++)
2425 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
2427 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
2429 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
2433 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
2435 for (i=0;i<LPC_ORDER;i++)
2436 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2437 celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
2438 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
2439 /* Check if the waveform is decaying (and if so how fast) */
2441 opus_val32 E1=1, E2=1;
2443 if (pitch_index <= MAX_PERIOD/2)
2444 period = pitch_index;
2446 period = MAX_PERIOD/2;
2447 for (i=0;i<period;i++)
2449 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
2450 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
2454 decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
2457 /* Copy excitation, taking decay into account */
2458 for (i=0;i<len+st->mode->overlap;i++)
2461 if (offset+i >= MAX_PERIOD)
2463 offset -= pitch_index;
2464 decay = MULT16_16_Q15(decay, decay);
2466 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
2467 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
2468 S1 += SHR32(MULT16_16(tmp,tmp),8);
2470 for (i=0;i<LPC_ORDER;i++)
2471 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
2472 for (i=0;i<len+st->mode->overlap;i++)
2473 e[i] = MULT16_32_Q15(fade, e[i]);
2474 celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
2478 for (i=0;i<len+overlap;i++)
2480 opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
2481 S2 += SHR32(MULT16_16(tmp,tmp),8);
2483 /* This checks for an "explosion" in the synthesis */
2485 if (!(S1 > SHR32(S2,2)))
2487 /* Float test is written this way to catch NaNs at the same time */
2488 if (!(S1 > 0.2f*S2))
2491 for (i=0;i<len+overlap;i++)
2495 opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
2496 for (i=0;i<len+overlap;i++)
2497 e[i] = MULT16_32_Q15(ratio, e[i]);
2501 /* Apply post-filter to the MDCT overlap of the previous frame */
2502 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2503 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2506 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2507 out_mem[c][i] = out_mem[c][N+i];
2509 /* Apply TDAC to the concealed audio so that it blends with the
2510 previous and next frames */
2511 for (i=0;i<overlap/2;i++)
2514 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2515 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2516 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2517 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2520 out_mem[c][MAX_PERIOD-N+i] = e[i];
2522 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2523 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2524 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2526 for (i=0;i<overlap;i++)
2527 out_mem[c][MAX_PERIOD+i] = e[i];
2531 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2538 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec)
2541 int spread_decision;
2544 VARDECL(celt_sig, freq);
2545 VARDECL(celt_norm, X);
2546 VARDECL(celt_ener, bandE);
2547 VARDECL(int, fine_quant);
2548 VARDECL(int, pulses);
2550 VARDECL(int, offsets);
2551 VARDECL(int, fine_priority);
2552 VARDECL(int, tf_res);
2553 VARDECL(unsigned char, collapse_masks);
2554 celt_sig *out_mem[2];
2555 celt_sig *decode_mem[2];
2556 celt_sig *overlap_mem[2];
2557 celt_sig *out_syn[2];
2559 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2564 const int CC = st->channels;
2569 int postfilter_pitch;
2570 opus_val16 postfilter_gain;
2573 opus_int32 total_bits;
2577 int postfilter_tapset;
2578 int anti_collapse_rsv;
2579 int anti_collapse_on=0;
2581 int C = st->stream_channels;
2584 frame_size *= st->downsample;
2587 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2588 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2589 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2591 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
2592 oldBandE = lpc+CC*LPC_ORDER;
2593 oldLogE = oldBandE + 2*st->mode->nbEBands;
2594 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
2595 backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
2598 if (st->signalling && data!=NULL)
2601 /* Convert "standard mode" to Opus header */
2602 if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
2604 data0 = fromOpus(data0);
2606 return OPUS_INVALID_PACKET;
2608 st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
2609 LM = (data0>>3)&0x3;
2610 C = 1 + ((data0>>2)&0x1);
2613 if (LM>st->mode->maxLM)
2614 return OPUS_INVALID_PACKET;
2615 if (frame_size < st->mode->shortMdctSize<<LM)
2616 return OPUS_BUFFER_TOO_SMALL;
2618 frame_size = st->mode->shortMdctSize<<LM;
2623 for (LM=0;LM<=st->mode->maxLM;LM++)
2624 if (st->mode->shortMdctSize<<LM==frame_size)
2626 if (LM>st->mode->maxLM)
2627 return OPUS_BAD_ARG;
2631 if (len<0 || len>1275 || pcm==NULL)
2632 return OPUS_BAD_ARG;
2634 N = M*st->mode->shortMdctSize;
2637 if (effEnd > st->mode->effEBands)
2638 effEnd = st->mode->effEBands;
2640 ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
2641 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2642 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2644 for (i=0;i<M*st->mode->eBands[st->start];i++)
2648 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2652 if (data == NULL || len<=1)
2654 celt_decode_lost(st, pcm, N, LM);
2656 return frame_size/st->downsample;
2661 ec_dec_init(&_dec,(unsigned char*)data,len);
2667 for (i=0;i<st->mode->nbEBands;i++)
2668 oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
2672 tell = ec_tell(dec);
2674 if (tell >= total_bits)
2677 silence = ec_dec_bit_logp(dec, 15);
2682 /* Pretend we've read all the remaining bits */
2684 dec->nbits_total+=tell-ec_tell(dec);
2687 postfilter_gain = 0;
2688 postfilter_pitch = 0;
2689 postfilter_tapset = 0;
2690 if (st->start==0 && tell+16 <= total_bits)
2692 if(ec_dec_bit_logp(dec, 1))
2695 octave = ec_dec_uint(dec, 6);
2696 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2697 qg = ec_dec_bits(dec, 3);
2698 if (ec_tell(dec)+2<=total_bits)
2699 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2700 postfilter_gain = QCONST16(.09375f,15)*(qg+1);
2702 tell = ec_tell(dec);
2705 if (LM > 0 && tell+3 <= total_bits)
2707 isTransient = ec_dec_bit_logp(dec, 3);
2708 tell = ec_tell(dec);
2718 /* Decode the global flags (first symbols in the stream) */
2719 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2720 /* Get band energies */
2721 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2722 intra_ener, dec, C, LM);
2724 ALLOC(tf_res, st->mode->nbEBands, int);
2725 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2727 tell = ec_tell(dec);
2728 spread_decision = SPREAD_NORMAL;
2729 if (tell+4 <= total_bits)
2730 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2732 ALLOC(pulses, st->mode->nbEBands, int);
2733 ALLOC(cap, st->mode->nbEBands, int);
2734 ALLOC(offsets, st->mode->nbEBands, int);
2735 ALLOC(fine_priority, st->mode->nbEBands, int);
2737 init_caps(st->mode,cap,LM,C);
2740 total_bits<<=BITRES;
2741 tell = ec_tell_frac(dec);
2742 for (i=st->start;i<st->end;i++)
2745 int dynalloc_loop_logp;
2747 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2748 /* quanta is 6 bits, but no more than 1 bit/sample
2749 and no less than 1/8 bit/sample */
2750 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2751 dynalloc_loop_logp = dynalloc_logp;
2753 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
2756 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2757 tell = ec_tell_frac(dec);
2761 total_bits -= quanta;
2762 dynalloc_loop_logp = 1;
2765 /* Making dynalloc more likely */
2767 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2770 ALLOC(fine_quant, st->mode->nbEBands, int);
2771 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2772 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2774 bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
2775 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2776 bits -= anti_collapse_rsv;
2777 codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
2778 alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
2779 fine_quant, fine_priority, C, LM, dec, 0, 0);
2781 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2783 /* Decode fixed codebook */
2784 ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
2785 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2786 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
2787 len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
2789 if (anti_collapse_rsv > 0)
2791 anti_collapse_on = ec_dec_bits(dec, 1);
2794 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2795 fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
2797 if (anti_collapse_on)
2798 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2799 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2801 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2805 for (i=0;i<C*st->mode->nbEBands;i++)
2808 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2812 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2814 OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2816 OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2819 for (i=0;i<M*st->mode->eBands[st->start];i++)
2823 int bound = M*st->mode->eBands[effEnd];
2824 if (st->downsample!=1)
2825 bound = IMIN(bound, N/st->downsample);
2826 for (i=bound;i<N;i++)
2830 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2832 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2837 freq[N+i] = freq[i];
2842 freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
2845 /* Compute inverse MDCTs */
2846 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
2849 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2850 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2851 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
2852 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2853 st->mode->window, st->overlap);
2855 comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
2856 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2857 st->mode->window, st->mode->overlap);
2860 st->postfilter_period_old = st->postfilter_period;
2861 st->postfilter_gain_old = st->postfilter_gain;
2862 st->postfilter_tapset_old = st->postfilter_tapset;
2863 st->postfilter_period = postfilter_pitch;
2864 st->postfilter_gain = postfilter_gain;
2865 st->postfilter_tapset = postfilter_tapset;
2868 st->postfilter_period_old = st->postfilter_period;
2869 st->postfilter_gain_old = st->postfilter_gain;
2870 st->postfilter_tapset_old = st->postfilter_tapset;
2874 for (i=0;i<st->mode->nbEBands;i++)
2875 oldBandE[st->mode->nbEBands+i]=oldBandE[i];
2878 /* In case start or end were to change */
2881 for (i=0;i<2*st->mode->nbEBands;i++)
2882 oldLogE2[i] = oldLogE[i];
2883 for (i=0;i<2*st->mode->nbEBands;i++)
2884 oldLogE[i] = oldBandE[i];
2885 for (i=0;i<2*st->mode->nbEBands;i++)
2886 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2888 for (i=0;i<2*st->mode->nbEBands;i++)
2889 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2893 for (i=0;i<st->start;i++)
2895 oldBandE[c*st->mode->nbEBands+i]=0;
2896 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2898 for (i=st->end;i<st->mode->nbEBands;i++)
2900 oldBandE[c*st->mode->nbEBands+i]=0;
2901 oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2906 deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
2909 if (ec_tell(dec) > 8*len)
2910 return OPUS_INTERNAL_ERROR;
2911 if(ec_get_error(dec))
2913 return frame_size/st->downsample;
2920 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2922 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2925 #ifndef DISABLE_FLOAT_API
2926 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2929 VARDECL(opus_int16, out);
2933 return OPUS_BAD_ARG;
2938 ALLOC(out, C*N, opus_int16);
2939 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2941 for (j=0;j<C*ret;j++)
2942 pcm[j]=out[j]*(1.f/32768.f);
2947 #endif /* DISABLE_FLOAT_API */
2951 int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2953 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2956 int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
2959 VARDECL(celt_sig, out);
2963 return OPUS_BAD_ARG;
2967 ALLOC(out, C*N, celt_sig);
2969 ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
2972 for (j=0;j<C*ret;j++)
2973 pcm[j] = FLOAT2INT16 (out[j]);
2980 #endif /* CUSTOM_MODES */
2982 int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...)
2986 va_start(ap, request);
2989 case CELT_SET_START_BAND_REQUEST:
2991 opus_int32 value = va_arg(ap, opus_int32);
2992 if (value<0 || value>=st->mode->nbEBands)
2997 case CELT_SET_END_BAND_REQUEST:
2999 opus_int32 value = va_arg(ap, opus_int32);
3000 if (value<1 || value>st->mode->nbEBands)
3005 case CELT_SET_CHANNELS_REQUEST:
3007 opus_int32 value = va_arg(ap, opus_int32);
3008 if (value<1 || value>2)
3010 st->stream_channels = value;
3013 case CELT_GET_AND_CLEAR_ERROR_REQUEST:
3015 opus_int32 *value = va_arg(ap, opus_int32*);
3022 case OPUS_GET_LOOKAHEAD_REQUEST:
3024 opus_int32 *value = va_arg(ap, opus_int32*);
3027 *value = st->overlap/st->downsample;
3030 case OPUS_RESET_STATE:
3033 opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
3034 lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
3035 oldBandE = lpc+st->channels*LPC_ORDER;
3036 oldLogE = oldBandE + 2*st->mode->nbEBands;
3037 oldLogE2 = oldLogE + 2*st->mode->nbEBands;
3038 OPUS_CLEAR((char*)&st->DECODER_RESET_START,
3039 opus_custom_decoder_get_size(st->mode, st->channels)-
3040 ((char*)&st->DECODER_RESET_START - (char*)st));
3041 for (i=0;i<2*st->mode->nbEBands;i++)
3042 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
3045 case OPUS_GET_PITCH_REQUEST:
3047 opus_int32 *value = va_arg(ap, opus_int32*);
3050 *value = st->postfilter_period;
3054 case CELT_GET_MODE_REQUEST:
3056 const CELTMode ** value = va_arg(ap, const CELTMode**);
3062 case CELT_SET_SIGNALLING_REQUEST:
3064 opus_int32 value = va_arg(ap, opus_int32);
3065 st->signalling = value;
3068 case OPUS_GET_FINAL_RANGE_REQUEST:
3070 opus_uint32 * value = va_arg(ap, opus_uint32 *);
3084 return OPUS_BAD_ARG;
3087 return OPUS_UNIMPLEMENTED;
3092 const char *opus_strerror(int error)
3094 static const char *error_strings[8] = {
3100 "request not implemented",
3102 "memory allocation failed"
3104 if (error > 0 || error < -7)
3105 return "unknown error";
3107 return error_strings[-error];
3110 const char *opus_get_version_string(void)
3112 return "libopus " OPUS_VERSION