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 - Neither the name of the Xiph.org Foundation nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "os_support.h"
48 #include "quant_bands.h"
50 #include "stack_alloc.h"
52 #include "float_cast.h"
57 static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
58 /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
59 static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
61 static const unsigned char tapset_icdf[3]={2,1,0};
63 #define COMBFILTER_MAXPERIOD 1024
64 #define COMBFILTER_MINPERIOD 15
66 static int resampling_factor(celt_int32 rate)
96 const CELTMode *mode; /**< Mode used by the encoder */
105 celt_int32 vbr_rate_norm; /* Target number of 8th bits per frame */
106 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
108 /* Everything beyond this point gets cleared on a reset */
109 #define ENCODER_RESET_START rng
119 int prefilter_period;
120 celt_word16 prefilter_gain;
121 int prefilter_tapset;
123 int prefilter_period_old;
124 celt_word16 prefilter_gain_old;
125 int prefilter_tapset_old;
127 int consec_transient;
129 /* VBR-related parameters */
130 celt_int32 vbr_reservoir;
131 celt_int32 vbr_drift;
132 celt_int32 vbr_offset;
133 celt_int32 vbr_count;
135 celt_word32 preemph_memE[2];
136 celt_word32 preemph_memD[2];
139 celt_sig syn_mem[2][2*MAX_PERIOD];
142 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
143 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
144 /* celt_sig overlap_mem[], Size = channels*mode->overlap */
145 /* celt_word16 oldEBands[], Size = 2*channels*mode->nbEBands */
148 int celt_encoder_get_size(const CELTMode *mode, int channels)
150 int size = sizeof(struct CELTEncoder)
151 + (2*channels*mode->overlap-1)*sizeof(celt_sig)
152 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
153 + 3*channels*mode->nbEBands*sizeof(celt_word16);
157 CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
159 CELTMode *mode = celt_mode_create(48000, 960, NULL);
160 CELTEncoder *st = celt_encoder_init(
161 (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)),
162 sampling_rate, channels, error);
166 CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error)
168 return celt_encoder_init_custom(
169 (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)),
170 mode, channels, error);
173 CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error)
175 celt_encoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels, error);
176 st->upsample = resampling_factor(sampling_rate);
180 *error = CELT_BAD_ARG;
186 CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error)
188 if (channels < 0 || channels > 2)
191 *error = CELT_BAD_ARG;
198 *error = CELT_ALLOC_FAIL;
202 CELT_MEMSET((char*)st, 0, celt_encoder_get_size(mode, channels));
205 st->overlap = mode->overlap;
206 st->channels = channels;
210 st->end = st->mode->effEBands;
211 st->constrained_vbr = 1;
213 st->vbr_rate_norm = 0;
216 st->delayedIntra = 1;
217 st->tonal_average = 256;
218 st->spread_decision = SPREAD_NORMAL;
220 st->tapset_decision = 0;
228 void celt_encoder_destroy(CELTEncoder *st)
233 static inline celt_int16 FLOAT2INT16(float x)
235 x = x*CELT_SIG_SCALE;
236 x = MAX32(x, -32768);
238 return (celt_int16)float2int(x);
241 static inline celt_word16 SIG2WORD16(celt_sig x)
244 x = PSHR32(x, SIG_SHIFT);
245 x = MAX32(x, -32768);
249 return (celt_word16)x;
253 static int transient_analysis(const celt_word32 * restrict in, int len, int C,
257 VARDECL(celt_word16, tmp);
258 celt_word32 mem0=0,mem1=0;
259 int is_transient = 0;
262 /* FIXME: Make that smaller */
263 celt_word16 bins[50];
265 ALLOC(tmp, len, celt_word16);
272 tmp[i] = SHR32(in[i],SIG_SHIFT);
275 tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
278 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
285 mem0 = mem1 + y - SHL32(x,1);
286 mem1 = x - SHR32(y,1);
288 mem0 = mem1 + y - 2*x;
291 tmp[i] = EXTRACT16(SHR(y,2));
293 /* First few samples are bad because we don't propagate the memory */
301 for (j=0;j<block;j++)
302 max_abs = MAX32(max_abs, tmp[i*block+j]);
309 celt_word16 t1, t2, t3;
311 t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
312 t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
313 t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
340 /** Apply window and compute the MDCT for all sub-frames and
341 all channels in a frame */
342 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int _C, int LM)
344 const int C = CHANNELS(_C);
345 if (C==1 && !shortBlocks)
347 const int overlap = OVERLAP(mode);
348 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM);
350 const int overlap = OVERLAP(mode);
351 int N = mode->shortMdctSize<<LM;
354 VARDECL(celt_word32, tmp);
358 /*lookup = &mode->mdct[0];*/
359 N = mode->shortMdctSize;
362 ALLOC(tmp, N, celt_word32);
367 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, tmp, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM);
368 /* Interleaving the sub-frames */
370 out[(j*B+b)+c*N*B] = tmp[j];
377 /** Compute the IMDCT and apply window for all sub-frames and
378 all channels in a frame */
379 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
380 celt_sig * restrict out_mem[],
381 celt_sig * restrict overlap_mem[], int _C, int LM)
384 const int C = CHANNELS(_C);
385 const int N = mode->shortMdctSize<<LM;
386 const int overlap = OVERLAP(mode);
389 VARDECL(celt_word32, x);
390 VARDECL(celt_word32, tmp);
396 ALLOC(x, N+overlap, celt_word32);
397 ALLOC(tmp, N, celt_word32);
401 N2 = mode->shortMdctSize;
404 /* Prevents problems from the imdct doing the overlap-add */
405 CELT_MEMSET(x, 0, overlap);
409 /* De-interleaving the sub-frames */
411 tmp[j] = X[(j*B+b)+c*N2*B];
412 clt_mdct_backward(&mode->mdct, tmp, x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM);
415 for (j=0;j<overlap;j++)
416 out_mem[c][j] = x[j] + overlap_mem[c][j];
418 out_mem[c][j] = x[j];
419 for (j=0;j<overlap;j++)
420 overlap_mem[c][j] = x[N+j];
425 static void deemphasis(celt_sig *in[], celt_word16 *pcm, int N, int _C, int downsample, const celt_word16 *coef, celt_sig *mem)
427 const int C = CHANNELS(_C);
432 celt_sig * restrict x;
433 celt_word16 * restrict y;
439 celt_sig tmp = *x + m;
440 m = MULT16_32_Q15(coef[0], tmp)
441 - MULT16_32_Q15(coef[1], *x);
442 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
444 /* Technically the store could be moved outside of the if because
445 the stores we don't want will just be overwritten */
446 if (++count==downsample)
448 *y = SCALEOUT(SIG2WORD16(tmp));
457 #ifdef ENABLE_POSTFILTER
458 static void comb_filter(celt_word32 *y, celt_word32 *x, int T0, int T1, int N,
459 celt_word16 g0, celt_word16 g1, int tapset0, int tapset1,
460 const celt_word16 *window, int overlap)
463 /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
464 celt_word16 g00, g01, g02, g10, g11, g12;
465 static const celt_word16 gains[3][3] = {
466 {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
467 {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
468 {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
469 g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
470 g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
471 g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
472 g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
473 g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
474 g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
475 for (i=0;i<overlap;i++)
478 f = MULT16_16_Q15(window[i],window[i]);
480 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
481 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
482 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
483 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
484 + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
485 + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
486 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
487 + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
488 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
489 + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
492 for (i=overlap;i<N;i++)
494 + MULT16_32_Q15(g10,x[i-T1])
495 + MULT16_32_Q15(g11,x[i-T1-1])
496 + MULT16_32_Q15(g11,x[i-T1+1])
497 + MULT16_32_Q15(g12,x[i-T1-2])
498 + MULT16_32_Q15(g12,x[i-T1+2]);
500 #endif /* ENABLE_POSTFILTER */
502 static const signed char tf_select_table[4][8] = {
503 {0, -1, 0, -1, 0,-1, 0,-1},
504 {0, -1, 0, -2, 1, 0, 1,-1},
505 {0, -2, 0, -3, 2, 0, 1,-1},
506 {0, -2, 0, -3, 3, 0, 1,-1},
509 static celt_word32 l1_metric(const celt_norm *tmp, int N, int LM, int width)
512 static const celt_word16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)};
516 for (i=0;i<1<<LM;i++)
519 for (j=0;j<N>>LM;j++)
520 L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]);
523 L1 = MULT16_32_Q15(sqrtM_1[LM], L1);
525 bias = QCONST16(.12f,15)*LM;
527 bias = QCONST16(.05f,15)*LM;
529 bias = QCONST16(.02f,15)*LM;
530 L1 = MAC16_32_Q15(L1, bias, L1);
534 static int tf_analysis(const CELTMode *m, celt_word16 *bandLogE, celt_word16 *oldBandE,
535 int len, int C, int isTransient, int *tf_res, int nbCompressedBytes, celt_norm *X,
536 int N0, int LM, int *tf_sum)
539 VARDECL(int, metric);
544 VARDECL(celt_norm, tmp);
549 if (nbCompressedBytes<15*C)
553 tf_res[i] = isTransient;
556 if (nbCompressedBytes<40)
558 else if (nbCompressedBytes<60)
560 else if (nbCompressedBytes<100)
565 ALLOC(metric, len, int);
566 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
567 ALLOC(path0, len, int);
568 ALLOC(path1, len, int);
574 celt_word32 L1, best_L1;
576 N = (m->eBands[i+1]-m->eBands[i])<<LM;
578 tmp[j] = X[j+(m->eBands[i]<<LM)];
579 /* Just add the right channel if we're in stereo */
582 tmp[j] = ADD16(tmp[j],X[N0+j+(m->eBands[i]<<LM)]);
583 L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM);
585 /*printf ("%f ", L1);*/
596 haar1(tmp, N>>(LM-k), 1<<(LM-k));
598 haar1(tmp, N>>k, 1<<k);
600 L1 = l1_metric(tmp, N, B, N>>LM);
608 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
610 metric[i] = best_level;
612 metric[i] = -best_level;
613 *tf_sum += metric[i];
616 /* FIXME: Figure out how to set this */
620 cost1 = isTransient ? 0 : lambda;
621 /* Viterbi forward pass */
628 from1 = cost1 + lambda;
638 from0 = cost0 + lambda;
648 cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
649 cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
651 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
652 /* Viterbi backward pass to check the decisions */
653 for (i=len-2;i>=0;i--)
655 if (tf_res[i+1] == 1)
656 tf_res[i] = path1[i+1];
658 tf_res[i] = path0[i+1];
664 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
672 budget = enc->buf->storage*8;
673 tell = ec_enc_tell(enc, 0);
674 logp = isTransient ? 2 : 4;
675 /* Reserve space to code the tf_select decision. */
676 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
677 budget -= tf_select_rsv;
678 curr = tf_changed = 0;
679 for (i=start;i<end;i++)
681 if (tell+logp<=budget)
683 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
684 tell = ec_enc_tell(enc, 0);
690 logp = isTransient ? 4 : 5;
692 /* Only code tf_select if it would actually make a difference. */
694 tf_select_table[LM][4*isTransient+0+tf_changed]!=
695 tf_select_table[LM][4*isTransient+2+tf_changed])
696 ec_enc_bit_logp(enc, tf_select, 1);
699 for (i=start;i<end;i++)
700 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
701 /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
704 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
706 int i, curr, tf_select;
713 budget = dec->buf->storage*8;
714 tell = ec_dec_tell(dec, 0);
715 logp = isTransient ? 2 : 4;
716 tf_select_rsv = LM>0 && tell+logp+1<=budget;
717 budget -= tf_select_rsv;
718 tf_changed = curr = 0;
719 for (i=start;i<end;i++)
721 if (tell+logp<=budget)
723 curr ^= ec_dec_bit_logp(dec, logp);
724 tell = ec_dec_tell(dec, 0);
728 logp = isTransient ? 4 : 5;
732 tf_select_table[LM][4*isTransient+0+tf_changed] !=
733 tf_select_table[LM][4*isTransient+2+tf_changed])
735 tf_select = ec_dec_bit_logp(dec, 1);
737 for (i=start;i<end;i++)
739 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
743 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
744 const celt_word16 *bandLogE, int nbEBands, int LM, int C, int N0)
752 celt_word16 sum = 0; /* Q10 */
753 /* Compute inter-channel correlation for low frequencies */
757 celt_word32 partial = 0;
758 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
759 partial = MAC16_16(partial, X[j], X[N0+j]);
760 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
762 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
763 /*printf ("%f\n", sum);*/
764 if (sum > QCONST16(.995f,10))
766 else if (sum > QCONST16(.92f,10))
768 else if (sum > QCONST16(.85f,10))
770 else if (sum > QCONST16(.8f,10))
774 /* Estimate spectral tilt */
776 for (i=0;i<nbEBands-1;i++)
778 diff += bandLogE[i+c*nbEBands]*(celt_int32)(2+2*i-nbEBands);
781 diff /= C*(nbEBands-1);
782 /*printf("%f\n", diff);*/
783 if (diff > QCONST16(2.f, DB_SHIFT))
785 if (diff > QCONST16(8.f, DB_SHIFT))
787 if (diff < -QCONST16(4.f, DB_SHIFT))
789 if (diff < -QCONST16(10.f, DB_SHIFT))
799 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
804 celt_word32 sumLR = EPSILON, sumMS = EPSILON;
806 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
810 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
812 celt_word16 L, R, M, S;
817 sumLR += EXTEND32(ABS16(L)) + EXTEND32(ABS16(R));
818 sumMS += EXTEND32(ABS16(M)) + EXTEND32(ABS16(S));
821 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
823 /* We don't need thetas for lower bands with LM<=1 */
826 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
827 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
831 int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
834 int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
841 VARDECL(celt_sig, in);
842 VARDECL(celt_sig, freq);
843 VARDECL(celt_norm, X);
844 VARDECL(celt_ener, bandE);
845 VARDECL(celt_word16, bandLogE);
846 VARDECL(int, fine_quant);
847 VARDECL(celt_word16, error);
848 VARDECL(int, pulses);
849 VARDECL(int, offsets);
850 VARDECL(int, fine_priority);
851 VARDECL(int, tf_res);
852 VARDECL(unsigned char, collapse_masks);
853 celt_sig *_overlap_mem;
854 celt_sig *prefilter_mem;
855 celt_word16 *oldBandE, *oldLogE, *oldLogE2;
859 const int C = CHANNELS(st->channels);
862 int nbFilledBytes, nbAvailableBytes;
867 int pitch_index=COMBFILTER_MINPERIOD;
868 celt_word16 gain1 = 0;
872 celt_word16 pf_threshold;
875 celt_int32 total_bits;
876 celt_int32 total_boost;
878 int prefilter_tapset=0;
880 int anti_collapse_rsv;
881 int anti_collapse_on=0;
885 if (nbCompressedBytes<2 || pcm==NULL)
888 frame_size *= st->upsample;
890 if (st->mode->shortMdctSize<<LM==frame_size)
892 if (LM>=MAX_CONFIG_SIZES)
896 prefilter_mem = st->in_mem+C*(st->overlap);
897 _overlap_mem = prefilter_mem+C*COMBFILTER_MAXPERIOD;
898 /*_overlap_mem = st->in_mem+C*(st->overlap);*/
899 oldBandE = (celt_word16*)(st->in_mem+C*(2*st->overlap+COMBFILTER_MAXPERIOD));
900 oldLogE = oldBandE + C*st->mode->nbEBands;
901 oldLogE2 = oldLogE + C*st->mode->nbEBands;
905 ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes);
906 ec_enc_init(&_enc,&buf);
911 tell=ec_enc_tell(enc, 0);
912 nbFilledBytes=(tell+4)>>3;
914 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
916 vbr_rate = st->vbr_rate_norm<<LM;
919 effectiveBytes = st->vbr_rate_norm>>BITRES<<LM>>3;
920 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
921 target rate and buffering.
922 We must do this up front so that bust-prevention logic triggers
923 correctly if we don't have enough bits. */
924 if (st->constrained_vbr)
926 celt_int32 vbr_bound;
927 celt_int32 max_allowed;
928 /* We could use any multiple of vbr_rate as bound (depending on the
930 This is clamped to ensure we use at least two bytes if the encoder
931 was entirely empty, but to allow 0 in hybrid mode. */
932 vbr_bound = vbr_rate;
933 max_allowed = IMIN(IMAX(tell==1?2:0,
934 vbr_rate+vbr_bound-st->vbr_reservoir>>(BITRES+3)),
936 if(max_allowed < nbAvailableBytes)
938 nbCompressedBytes = nbFilledBytes+max_allowed;
939 nbAvailableBytes = max_allowed;
940 ec_byte_shrink(&buf, nbCompressedBytes);
944 effectiveBytes = nbCompressedBytes;
945 total_bits = nbCompressedBytes*8;
948 if (effEnd > st->mode->effEBands)
949 effEnd = st->mode->effEBands;
951 N = M*st->mode->shortMdctSize;
952 ALLOC(in, C*(N+st->overlap), celt_sig);
954 /* Find pitch period and gain */
956 VARDECL(celt_sig, _pre);
960 ALLOC(_pre, C*(N+COMBFILTER_MAXPERIOD), celt_sig);
963 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
968 const celt_word16 * restrict pcmp = pcm+c;
969 celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
976 if (++count==st->upsample)
983 /* Apply pre-emphasis */
984 tmp = MULT16_16(st->mode->preemph[2], x);
985 *inp = tmp + st->preemph_memE[c];
986 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
987 - MULT16_32_Q15(st->mode->preemph[0], tmp);
988 silence = silence && *inp == 0;
991 CELT_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
992 CELT_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
996 ec_enc_bit_logp(enc, silence, 15);
1001 /*In VBR mode there is no need to send more than the minimum. */
1004 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1005 total_bits=nbCompressedBytes*8;
1007 ec_byte_shrink(&buf, nbCompressedBytes);
1009 /* Pretend we've filled all the remaining bits with zeros
1010 (that's what the initialiser did anyway) */
1011 tell = nbCompressedBytes*8;
1012 enc->nbits_total+=tell-ec_enc_tell(enc,0);
1014 #ifdef ENABLE_POSTFILTER
1015 if (nbAvailableBytes>12*C && st->start==0 && !silence)
1017 VARDECL(celt_word16, pitch_buf);
1018 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, celt_word16);
1020 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, C);
1021 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1022 COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
1023 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1025 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1026 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1027 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1028 pitch_index = COMBFILTER_MAXPERIOD-2;
1029 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1030 prefilter_tapset = st->tapset_decision;
1035 /* Gain threshold for enabling the prefilter/postfilter */
1036 pf_threshold = QCONST16(.2f,15);
1038 /* Adjusting the threshold based on rate and continuity */
1039 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1040 pf_threshold += QCONST16(.2f,15);
1041 if (nbAvailableBytes<25)
1042 pf_threshold += QCONST16(.1f,15);
1043 if (nbAvailableBytes<35)
1044 pf_threshold += QCONST16(.1f,15);
1045 if (st->prefilter_gain > QCONST16(.4f,15))
1046 pf_threshold -= QCONST16(.1f,15);
1047 if (st->prefilter_gain > QCONST16(.55f,15))
1048 pf_threshold -= QCONST16(.1f,15);
1050 /* Hard threshold at 0.2 */
1051 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1052 if (gain1<pf_threshold)
1054 if(st->start==0 && tell+17<=total_bits)
1055 ec_enc_bit_logp(enc, 0, 1);
1062 if (gain1 > QCONST16(.6f,15))
1063 gain1 = QCONST16(.6f,15);
1064 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1065 gain1=st->prefilter_gain;
1068 qg = ((gain1+2048)>>12)-2;
1070 qg = floor(.5+gain1*8)-2;
1072 ec_enc_bit_logp(enc, 1, 1);
1074 octave = EC_ILOG(pitch_index)-5;
1075 ec_enc_uint(enc, octave, 6);
1076 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1078 ec_enc_bits(enc, qg, 2);
1079 gain1 = QCONST16(.125f,15)*(qg+2);
1080 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1083 /*printf("%d %f\n", pitch_index, gain1);*/
1084 #else /* ENABLE_POSTFILTER */
1085 if(st->start==0 && tell+17<=total_bits)
1086 ec_enc_bit_logp(enc, 0, 1);
1088 #endif /* ENABLE_POSTFILTER */
1091 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1092 CELT_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1093 #ifdef ENABLE_POSTFILTER
1094 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1095 st->prefilter_period, pitch_index, N, -st->prefilter_gain, -gain1,
1096 st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
1097 #endif /* ENABLE_POSTFILTER */
1098 CELT_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1100 #ifdef ENABLE_POSTFILTER
1101 if (N>COMBFILTER_MAXPERIOD)
1103 CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1105 CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1106 CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1108 #endif /* ENABLE_POSTFILTER */
1122 if (LM>0 && ec_enc_tell(enc, 0)+3<=total_bits)
1124 if (st->complexity > 1)
1126 isTransient = transient_analysis(in, N+st->overlap, C,
1131 ec_enc_bit_logp(enc, isTransient, 3);
1134 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
1135 ALLOC(bandE,st->mode->nbEBands*C, celt_ener);
1136 ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16);
1138 compute_mdcts(st->mode, shortBlocks, in, freq, C, LM);
1140 if (st->upsample != 1)
1144 int bound = N/st->upsample;
1145 for (i=0;i<bound;i++)
1146 freq[c*N+i] *= st->upsample;
1151 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1153 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
1155 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
1157 /* Band normalisation */
1158 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
1160 ALLOC(tf_res, st->mode->nbEBands, int);
1161 /* Needs to be before coarse energy quantization because otherwise the energy gets modified */
1162 tf_select = tf_analysis(st->mode, bandLogE, oldBandE, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
1163 for (i=effEnd;i<st->end;i++)
1164 tf_res[i] = tf_res[effEnd-1];
1166 ALLOC(error, C*st->mode->nbEBands, celt_word16);
1167 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
1168 oldBandE, total_bits, error, enc,
1169 C, LM, nbAvailableBytes, st->force_intra,
1170 &st->delayedIntra, st->complexity >= 4);
1172 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1174 st->spread_decision = SPREAD_NORMAL;
1175 if (ec_enc_tell(enc, 0)+4<=total_bits)
1177 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
1179 if (st->complexity == 0)
1180 st->spread_decision = SPREAD_NONE;
1182 st->spread_decision = spreading_decision(st->mode, X,
1183 &st->tonal_average, st->spread_decision, &st->hf_average,
1184 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1186 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1189 ALLOC(offsets, st->mode->nbEBands, int);
1191 for (i=0;i<st->mode->nbEBands;i++)
1193 /* Dynamic allocation code */
1194 /* Make sure that dynamic allocation can't make us bust the budget */
1195 if (effectiveBytes > 50 && LM>=1)
1206 for (i=1;i<st->mode->nbEBands-1;i++)
1209 d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
1211 d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
1212 bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
1213 if (d2 > SHL16(t1,DB_SHIFT))
1215 if (d2 > SHL16(t2,DB_SHIFT))
1220 total_bits<<=BITRES;
1222 tell = ec_enc_tell(enc, BITRES);
1223 for (i=st->start;i<st->end;i++)
1226 int dynalloc_loop_logp;
1229 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
1230 /* quanta is 6 bits, but no more than 1 bit/sample
1231 and no less than 1/8 bit/sample */
1232 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1233 dynalloc_loop_logp = dynalloc_logp;
1235 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1236 && boost < (64<<LM)*(C<<BITRES); j++)
1239 flag = j<offsets[i];
1240 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1241 tell = ec_enc_tell(enc, BITRES);
1245 total_boost += quanta;
1246 dynalloc_loop_logp = 1;
1248 /* Making dynalloc more likely */
1250 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1254 if (tell+(6<<BITRES) <= total_bits - total_boost)
1256 alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
1257 st->mode->nbEBands, LM, C, N);
1258 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1259 tell = ec_enc_tell(enc, BITRES);
1262 /* Variable bitrate */
1267 /* The target rate in 8th bits per frame */
1269 celt_int32 min_allowed;
1271 target = vbr_rate + st->vbr_offset - ((40*C+20)<<BITRES);
1273 /* Shortblocks get a large boost in bitrate, but since they
1274 are uncommon long blocks are not greatly affected */
1275 if (shortBlocks || tf_sum < -2*(st->end-st->start))
1276 target = 7*target/4;
1277 else if (tf_sum < -(st->end-st->start))
1278 target = 3*target/2;
1280 target-=(target+14)/28;
1282 /* The current offset is removed from the target and the space used
1286 /* In VBR mode the frame size must not be reduced so much that it would
1287 result in the encoder running out of bits.
1288 The margin of 2 bytes ensures that none of the bust-prevention logic
1289 in the decoder will have triggered so far. */
1290 min_allowed = (tell+total_boost+(1<<BITRES+3)-1>>(BITRES+3)) + 2 - nbFilledBytes;
1292 nbAvailableBytes = target+(1<<(BITRES+2))>>(BITRES+3);
1293 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1294 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1298 nbAvailableBytes = 2;
1299 target = 2*8<<BITRES;
1302 /* By how much did we "miss" the target on that frame */
1303 delta = target - vbr_rate;
1305 target=nbAvailableBytes<<(BITRES+3);
1307 if (st->vbr_count < 970)
1310 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1312 alpha = QCONST16(.001f,15);
1313 /* How many bits have we used in excess of what we're allowed */
1314 if (st->constrained_vbr)
1315 st->vbr_reservoir += target - vbr_rate;
1316 /*printf ("%d\n", st->vbr_reservoir);*/
1318 /* Compute the offset we need to apply in order to reach the target */
1319 st->vbr_drift += (celt_int32)MULT16_32_Q15(alpha,delta-st->vbr_offset-st->vbr_drift);
1320 st->vbr_offset = -st->vbr_drift;
1321 /*printf ("%d\n", st->vbr_drift);*/
1323 if (st->constrained_vbr && st->vbr_reservoir < 0)
1325 /* We're under the min value -- increase rate */
1326 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1327 /* Unless we're just coding silence */
1328 nbAvailableBytes += silence?0:adjust;
1329 st->vbr_reservoir = 0;
1330 /*printf ("+%d\n", adjust);*/
1332 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1333 /* This moves the raw bits to take into account the new compressed size */
1334 ec_byte_shrink(&buf, nbCompressedBytes);
1340 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1342 dual_stereo = stereo_analysis(st->mode, X, LM, N);
1344 /* Account for coarse energy */
1345 effectiveRate = (8*effectiveBytes - 80)>>LM;
1347 /* effectiveRate in kb/s */
1348 effectiveRate = 2*effectiveRate/5;
1349 if (effectiveRate<35)
1351 else if (effectiveRate<50)
1353 else if (effectiveRate<68)
1355 else if (effectiveRate<84)
1357 else if (effectiveRate<102)
1359 else if (effectiveRate<130)
1363 intensity = IMIN(st->end,IMAX(st->start, intensity));
1366 /* Bit allocation */
1367 ALLOC(fine_quant, st->mode->nbEBands, int);
1368 ALLOC(pulses, st->mode->nbEBands, int);
1369 ALLOC(fine_priority, st->mode->nbEBands, int);
1371 /* bits = packet size - where we are - safety*/
1372 bits = (nbCompressedBytes*8<<BITRES) - ec_enc_tell(enc, BITRES) - 1;
1373 anti_collapse_rsv = isTransient&&LM>=2&&bits>=(LM+2<<BITRES) ? (1<<BITRES) : 0;
1374 bits -= anti_collapse_rsv;
1375 codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
1376 alloc_trim, &intensity, &dual_stereo, bits, pulses, fine_quant,
1377 fine_priority, C, LM, enc, 1, st->lastCodedBands);
1378 st->lastCodedBands = codedBands;
1380 quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1382 #ifdef MEASURE_NORM_MSE
1387 X0[i+c*N] = X[i+c*N];
1389 for (i=0;i<C*st->mode->nbEBands;i++)
1390 bandE0[i] = bandE[i];
1393 /* Residual quantisation */
1394 ALLOC(collapse_masks, st->mode->nbEBands, unsigned char);
1395 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1396 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res, resynth,
1397 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, enc, LM, codedBands, &st->rng);
1399 if (anti_collapse_rsv > 0)
1401 anti_collapse_on = st->consec_transient<2;
1402 ec_enc_bits(enc, anti_collapse_on, 1);
1404 quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_enc_tell(enc, 0), enc, C);
1408 for (i=0;i<C*st->mode->nbEBands;i++)
1409 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1413 /* Re-synthesis of the coded audio if required */
1416 celt_sig *out_mem[2];
1417 celt_sig *overlap_mem[2];
1419 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
1422 for (i=0;i<C*st->mode->nbEBands;i++)
1426 #ifdef MEASURE_NORM_MSE
1427 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
1429 if (anti_collapse_on)
1431 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
1432 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1436 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1438 CELT_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
1440 CELT_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
1443 for (i=0;i<M*st->mode->eBands[st->start];i++)
1447 for (i=M*st->mode->eBands[st->end];i<N;i++)
1451 out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
1453 out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
1456 overlap_mem[c] = _overlap_mem + c*st->overlap;
1459 compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, C, LM);
1461 #ifdef ENABLE_POSTFILTER
1463 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1464 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1467 comb_filter(out_mem[c], out_mem[c], st->prefilter_period, st->prefilter_period, st->overlap,
1468 st->prefilter_gain, st->prefilter_gain, st->prefilter_tapset, st->prefilter_tapset,
1470 comb_filter(out_mem[c]+st->overlap, out_mem[c]+st->overlap, st->prefilter_period, pitch_index, N-st->overlap,
1471 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1472 st->mode->window, st->mode->overlap);
1474 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, N,
1475 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1476 st->mode->window, st->mode->overlap);
1479 #endif /* ENABLE_POSTFILTER */
1481 deemphasis(out_mem, (celt_word16*)pcm, N, C, st->upsample, st->mode->preemph, st->preemph_memD);
1482 st->prefilter_period_old = st->prefilter_period;
1483 st->prefilter_gain_old = st->prefilter_gain;
1484 st->prefilter_tapset_old = st->prefilter_tapset;
1488 st->prefilter_period = pitch_index;
1489 st->prefilter_gain = gain1;
1490 st->prefilter_tapset = prefilter_tapset;
1492 /* In case start or end were to change */
1495 for (i=0;i<st->start;i++)
1496 oldBandE[c*st->mode->nbEBands+i]=0;
1497 for (i=st->end;i<st->mode->nbEBands;i++)
1498 oldBandE[c*st->mode->nbEBands+i]=0;
1502 for (i=0;i<C*st->mode->nbEBands;i++)
1503 oldLogE2[i] = oldLogE[i];
1504 for (i=0;i<C*st->mode->nbEBands;i++)
1505 oldLogE[i] = oldBandE[i];
1507 for (i=0;i<C*st->mode->nbEBands;i++)
1508 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1511 st->consec_transient++;
1513 st->consec_transient=0;
1516 /* If there's any room left (can only happen for very high rates),
1517 it's already filled with zeros */
1521 if (ec_enc_get_error(enc))
1522 return CELT_CORRUPTED_DATA;
1524 return nbCompressedBytes;
1528 #ifndef DISABLE_FLOAT_API
1529 int celt_encode_with_ec_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1531 int j, ret, C, N, LM, M;
1532 VARDECL(celt_int16, in);
1536 return CELT_BAD_ARG;
1538 for (LM=0;LM<4;LM++)
1539 if (st->mode->shortMdctSize<<LM==frame_size)
1541 if (LM>=MAX_CONFIG_SIZES)
1542 return CELT_BAD_ARG;
1545 C = CHANNELS(st->channels);
1546 N = M*st->mode->shortMdctSize;
1547 ALLOC(in, C*N, celt_int16);
1550 in[j] = FLOAT2INT16(pcm[j]);
1552 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, enc);
1555 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1561 #endif /*DISABLE_FLOAT_API*/
1563 int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1565 int j, ret, C, N, LM, M;
1566 VARDECL(celt_sig, in);
1570 return CELT_BAD_ARG;
1572 for (LM=0;LM<4;LM++)
1573 if (st->mode->shortMdctSize<<LM==frame_size)
1575 if (LM>=MAX_CONFIG_SIZES)
1576 return CELT_BAD_ARG;
1579 C=CHANNELS(st->channels);
1580 N=M*st->mode->shortMdctSize;
1581 ALLOC(in, C*N, celt_sig);
1582 for (j=0;j<C*N;j++) {
1583 in[j] = SCALEOUT(pcm[j]);
1586 ret = celt_encode_with_ec_float(st,in,frame_size,compressed,nbCompressedBytes, enc);
1589 ((celt_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1596 int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1598 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1601 #ifndef DISABLE_FLOAT_API
1602 int celt_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1604 return celt_encode_with_ec_float(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1606 #endif /* DISABLE_FLOAT_API */
1608 int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1612 va_start(ap, request);
1615 case CELT_GET_MODE_REQUEST:
1617 const CELTMode ** value = va_arg(ap, const CELTMode**);
1623 case CELT_SET_COMPLEXITY_REQUEST:
1625 int value = va_arg(ap, celt_int32);
1626 if (value<0 || value>10)
1628 st->complexity = value;
1631 case CELT_SET_START_BAND_REQUEST:
1633 celt_int32 value = va_arg(ap, celt_int32);
1634 if (value<0 || value>=st->mode->nbEBands)
1639 case CELT_SET_END_BAND_REQUEST:
1641 celt_int32 value = va_arg(ap, celt_int32);
1642 if (value<1 || value>st->mode->nbEBands)
1647 case CELT_SET_PREDICTION_REQUEST:
1649 int value = va_arg(ap, celt_int32);
1650 if (value<0 || value>2)
1654 st->force_intra = 1;
1655 } else if (value==1) {
1656 st->force_intra = 0;
1658 st->force_intra = 0;
1662 case CELT_SET_VBR_CONSTRAINT_REQUEST:
1664 celt_int32 value = va_arg(ap, celt_int32);
1665 st->constrained_vbr = value;
1668 case CELT_SET_VBR_RATE_REQUEST:
1670 celt_int32 value = va_arg(ap, celt_int32);
1672 int N = st->mode->shortMdctSize;
1677 frame_rate = ((st->mode->Fs<<3)+(N>>1))/N;
1678 st->vbr_rate_norm = value>0?IMAX(1,((value<<(BITRES+3))+(frame_rate>>1))/frame_rate):0;
1681 case CELT_RESET_STATE:
1683 CELT_MEMSET((char*)&st->ENCODER_RESET_START, 0,
1684 celt_encoder_get_size(st->mode, st->channels)-
1685 ((char*)&st->ENCODER_RESET_START - (char*)st));
1687 st->delayedIntra = 1;
1688 st->spread_decision = SPREAD_NORMAL;
1689 st->tonal_average = QCONST16(1.f,8);
1699 return CELT_BAD_ARG;
1702 return CELT_UNIMPLEMENTED;
1705 /**********************************************************************/
1709 /**********************************************************************/
1710 #define DECODE_BUFFER_SIZE 2048
1713 @brief Decoder state
1715 struct CELTDecoder {
1716 const CELTMode *mode;
1723 /* Everything beyond this point gets cleared on a reset */
1724 #define DECODER_RESET_START rng
1727 int last_pitch_index;
1729 int postfilter_period;
1730 int postfilter_period_old;
1731 celt_word16 postfilter_gain;
1732 celt_word16 postfilter_gain_old;
1733 int postfilter_tapset;
1734 int postfilter_tapset_old;
1736 celt_sig preemph_memD[2];
1738 celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
1739 /* celt_word16 lpc[], Size = channels*LPC_ORDER */
1740 /* celt_word16 oldEBands[], Size = channels*mode->nbEBands */
1741 /* celt_word16 oldLogE[], Size = channels*mode->nbEBands */
1742 /* celt_word16 oldLogE2[], Size = channels*mode->nbEBands */
1743 /* celt_word16 backgroundLogE[], Size = channels*mode->nbEBands */
1746 int celt_decoder_get_size(const CELTMode *mode, int channels)
1748 int size = sizeof(struct CELTDecoder)
1749 + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
1750 + channels*LPC_ORDER*sizeof(celt_word16)
1751 + 4*channels*mode->nbEBands*sizeof(celt_word16);
1755 CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
1757 const CELTMode *mode = celt_mode_create(48000, 960, NULL);
1758 return celt_decoder_init(
1759 (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)),
1760 sampling_rate, channels, error);
1763 CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error)
1765 return celt_decoder_init_custom(
1766 (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)),
1767 mode, channels, error);
1770 CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error)
1772 celt_decoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels, error);
1773 st->downsample = resampling_factor(sampling_rate);
1774 if (st->downsample==0)
1777 *error = CELT_BAD_ARG;
1783 CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error)
1785 if (channels < 0 || channels > 2)
1788 *error = CELT_BAD_ARG;
1795 *error = CELT_ALLOC_FAIL;
1799 CELT_MEMSET((char*)st, 0, celt_decoder_get_size(mode, channels));
1802 st->overlap = mode->overlap;
1803 st->channels = channels;
1807 st->end = st->mode->effEBands;
1816 void celt_decoder_destroy(CELTDecoder *st)
1821 static void celt_decode_lost(CELTDecoder * restrict st, celt_word16 * restrict pcm, int N, int LM)
1825 int overlap = st->mode->overlap;
1826 celt_word16 fade = Q15ONE;
1828 const int C = CHANNELS(st->channels);
1830 celt_sig *out_mem[2];
1831 celt_sig *decode_mem[2];
1832 celt_sig *overlap_mem[2];
1834 celt_word32 *out_syn[2];
1835 celt_word16 *oldBandE, *oldLogE2, *backgroundLogE;
1839 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
1840 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
1841 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
1843 lpc = (celt_word16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
1844 oldBandE = lpc+C*LPC_ORDER;
1845 oldLogE2 = oldBandE + C*st->mode->nbEBands;
1846 backgroundLogE = oldLogE2 + C*st->mode->nbEBands;
1848 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
1850 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
1852 len = N+st->mode->overlap;
1854 if (st->loss_count >= 5)
1856 VARDECL(celt_sig, freq);
1857 VARDECL(celt_norm, X);
1858 VARDECL(celt_ener, bandE);
1861 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
1862 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1863 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
1865 log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
1870 seed = lcg_rand(seed);
1871 X[i] = (celt_int32)(seed)>>20;
1875 for (i=0;i<st->mode->nbEBands;i++)
1876 renormalise_vector(X+N*c+(st->mode->eBands[i]<<LM), (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM, Q15ONE);
1878 denormalise_bands(st->mode, X, freq, bandE, st->mode->nbEBands, C, 1<<LM);
1880 compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
1881 } else if (st->loss_count == 0)
1883 celt_word16 pitch_buf[MAX_PERIOD>>1];
1885 /* FIXME: This is a kludge */
1886 if (len2>MAX_PERIOD>>1)
1887 len2 = MAX_PERIOD>>1;
1888 pitch_downsample(out_mem, pitch_buf, MAX_PERIOD, C);
1889 pitch_search(pitch_buf+((MAX_PERIOD-len2)>>1), pitch_buf, len2,
1890 MAX_PERIOD-len2-100, &pitch_index);
1891 pitch_index = MAX_PERIOD-len2-pitch_index;
1892 st->last_pitch_index = pitch_index;
1894 pitch_index = st->last_pitch_index;
1895 fade = QCONST16(.8f,15);
1899 /* FIXME: This is more memory than necessary */
1900 celt_word32 e[2*MAX_PERIOD];
1901 celt_word16 exc[2*MAX_PERIOD];
1902 celt_word32 ac[LPC_ORDER+1];
1903 celt_word16 decay = 1;
1905 celt_word16 mem[LPC_ORDER]={0};
1907 offset = MAX_PERIOD-pitch_index;
1908 for (i=0;i<MAX_PERIOD;i++)
1909 exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
1911 if (st->loss_count == 0)
1913 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
1914 LPC_ORDER, MAX_PERIOD);
1916 /* Noise floor -40 dB */
1918 ac[0] += SHR32(ac[0],13);
1923 for (i=1;i<=LPC_ORDER;i++)
1925 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
1927 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
1929 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
1933 _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
1935 for (i=0;i<LPC_ORDER;i++)
1936 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
1937 fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
1938 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
1939 /* Check if the waveform is decaying (and if so how fast) */
1941 celt_word32 E1=1, E2=1;
1943 if (pitch_index <= MAX_PERIOD/2)
1944 period = pitch_index;
1946 period = MAX_PERIOD/2;
1947 for (i=0;i<period;i++)
1949 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
1950 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
1954 decay = celt_sqrt(frac_div32(SHR(E1,1),E2));
1957 /* Copy excitation, taking decay into account */
1958 for (i=0;i<len+st->mode->overlap;i++)
1961 if (offset+i >= MAX_PERIOD)
1963 offset -= pitch_index;
1964 decay = MULT16_16_Q15(decay, decay);
1966 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
1967 tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
1968 S1 += SHR32(MULT16_16(tmp,tmp),8);
1970 for (i=0;i<LPC_ORDER;i++)
1971 mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
1972 for (i=0;i<len+st->mode->overlap;i++)
1973 e[i] = MULT16_32_Q15(fade, e[i]);
1974 iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
1978 for (i=0;i<len+overlap;i++)
1980 celt_word16 tmp = ROUND16(e[i],SIG_SHIFT);
1981 S2 += SHR32(MULT16_16(tmp,tmp),8);
1983 /* This checks for an "explosion" in the synthesis */
1985 if (!(S1 > SHR32(S2,2)))
1987 /* Float test is written this way to catch NaNs at the same time */
1988 if (!(S1 > 0.2f*S2))
1991 for (i=0;i<len+overlap;i++)
1995 celt_word16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
1996 for (i=0;i<len+overlap;i++)
1997 e[i] = MULT16_32_Q15(ratio, e[i]);
2001 #ifdef ENABLE_POSTFILTER
2002 /* Apply post-filter to the MDCT overlap of the previous frame */
2003 comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2004 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2006 #endif /* ENABLE_POSTFILTER */
2008 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
2009 out_mem[c][i] = out_mem[c][N+i];
2011 /* Apply TDAC to the concealed audio so that it blends with the
2012 previous and next frames */
2013 for (i=0;i<overlap/2;i++)
2016 tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
2017 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
2018 out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
2019 out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
2022 out_mem[c][MAX_PERIOD-N+i] = e[i];
2024 #ifdef ENABLE_POSTFILTER
2025 /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
2026 comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
2027 -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2029 #endif /* ENABLE_POSTFILTER */
2030 for (i=0;i<overlap;i++)
2031 out_mem[c][MAX_PERIOD+i] = e[i];
2034 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2042 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size, ec_dec *dec)
2045 int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *data, int len, celt_sig * restrict pcm, int frame_size, ec_dec *dec)
2049 int spread_decision;
2053 VARDECL(celt_sig, freq);
2054 VARDECL(celt_norm, X);
2055 VARDECL(celt_ener, bandE);
2056 VARDECL(int, fine_quant);
2057 VARDECL(int, pulses);
2058 VARDECL(int, offsets);
2059 VARDECL(int, fine_priority);
2060 VARDECL(int, tf_res);
2061 VARDECL(unsigned char, collapse_masks);
2062 celt_sig *out_mem[2];
2063 celt_sig *decode_mem[2];
2064 celt_sig *overlap_mem[2];
2065 celt_sig *out_syn[2];
2067 celt_word16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
2072 const int C = CHANNELS(st->channels);
2077 int postfilter_pitch;
2078 celt_word16 postfilter_gain;
2081 celt_int32 total_bits;
2084 int postfilter_tapset;
2085 int anti_collapse_rsv;
2086 int anti_collapse_on=0;
2092 return CELT_BAD_ARG;
2094 frame_size *= st->downsample;
2095 for (LM=0;LM<4;LM++)
2096 if (st->mode->shortMdctSize<<LM==frame_size)
2098 if (LM>=MAX_CONFIG_SIZES)
2099 return CELT_BAD_ARG;
2103 decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
2104 out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
2105 overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
2107 lpc = (celt_word16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
2108 oldBandE = lpc+C*LPC_ORDER;
2109 oldLogE = oldBandE + C*st->mode->nbEBands;
2110 oldLogE2 = oldLogE + C*st->mode->nbEBands;
2111 backgroundLogE = oldLogE2 + C*st->mode->nbEBands;
2113 N = M*st->mode->shortMdctSize;
2116 if (effEnd > st->mode->effEBands)
2117 effEnd = st->mode->effEBands;
2119 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
2120 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2121 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
2123 for (i=0;i<M*st->mode->eBands[st->start];i++)
2127 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2131 if (data == NULL || len<=1)
2133 celt_decode_lost(st, pcm, N, LM);
2139 return CELT_BAD_ARG;
2144 ec_byte_readinit(&buf,(unsigned char*)data,len);
2145 ec_dec_init(&_dec,&buf);
2150 tell = ec_dec_tell(dec, 0);
2153 silence = ec_dec_bit_logp(dec, 15);
2158 /* Pretend we've read all the remaining bits */
2160 dec->nbits_total+=tell-ec_dec_tell(dec,0);
2163 postfilter_gain = 0;
2164 postfilter_pitch = 0;
2165 postfilter_tapset = 0;
2166 if (st->start==0 && tell+17 <= total_bits)
2168 if(ec_dec_bit_logp(dec, 1))
2170 #ifdef ENABLE_POSTFILTER
2172 octave = ec_dec_uint(dec, 6);
2173 postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
2174 qg = ec_dec_bits(dec, 2);
2175 postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
2176 postfilter_gain = QCONST16(.125f,15)*(qg+2);
2177 #else /* ENABLE_POSTFILTER */
2179 return CELT_CORRUPTED_DATA;
2180 #endif /* ENABLE_POSTFILTER */
2182 tell = ec_dec_tell(dec, 0);
2185 if (LM > 0 && tell+3 <= total_bits)
2187 isTransient = ec_dec_bit_logp(dec, 3);
2188 tell = ec_dec_tell(dec, 0);
2198 /* Decode the global flags (first symbols in the stream) */
2199 intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
2200 /* Get band energies */
2201 unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
2202 intra_ener, dec, C, LM);
2204 ALLOC(tf_res, st->mode->nbEBands, int);
2205 tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
2207 tell = ec_dec_tell(dec, 0);
2208 spread_decision = SPREAD_NORMAL;
2209 if (tell+4 <= total_bits)
2210 spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
2212 ALLOC(pulses, st->mode->nbEBands, int);
2213 ALLOC(offsets, st->mode->nbEBands, int);
2214 ALLOC(fine_priority, st->mode->nbEBands, int);
2217 total_bits<<=BITRES;
2218 tell = ec_dec_tell(dec, BITRES);
2219 for (i=st->start;i<st->end;i++)
2222 int dynalloc_loop_logp;
2224 width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
2225 /* quanta is 6 bits, but no more than 1 bit/sample
2226 and no less than 1/8 bit/sample */
2227 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2228 dynalloc_loop_logp = dynalloc_logp;
2230 while (tell+(dynalloc_loop_logp<<BITRES) < total_bits &&
2231 boost < (64<<LM)*(C<<BITRES))
2234 flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
2235 tell = ec_dec_tell(dec, BITRES);
2239 total_bits -= quanta;
2240 dynalloc_loop_logp = 1;
2243 /* Making dynalloc more likely */
2245 dynalloc_logp = IMAX(2, dynalloc_logp-1);
2248 ALLOC(fine_quant, st->mode->nbEBands, int);
2249 alloc_trim = tell+(6<<BITRES) <= total_bits ?
2250 ec_dec_icdf(dec, trim_icdf, 7) : 5;
2252 bits = (len*8<<BITRES) - ec_dec_tell(dec, BITRES) - 1;
2253 anti_collapse_rsv = isTransient&&LM>=2&&bits>=(LM+2<<BITRES) ? (1<<BITRES) : 0;
2254 bits -= anti_collapse_rsv;
2255 codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
2256 alloc_trim, &intensity, &dual_stereo, bits, pulses, fine_quant,
2257 fine_priority, C, LM, dec, 0, 0);
2259 unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
2261 /* Decode fixed codebook */
2262 ALLOC(collapse_masks, st->mode->nbEBands, unsigned char);
2263 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
2264 NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, 1,
2265 len*(8<<BITRES)-anti_collapse_rsv, dec, LM, codedBands, &st->rng);
2267 if (anti_collapse_rsv > 0)
2269 anti_collapse_on = ec_dec_bits(dec, 1);
2272 unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
2273 fine_quant, fine_priority, len*8-ec_dec_tell(dec, 0), dec, C);
2275 if (anti_collapse_on)
2276 anti_collapse(st->mode, X, collapse_masks, LM, C, N,
2277 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
2279 log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
2283 for (i=0;i<C*st->mode->nbEBands;i++)
2286 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2290 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
2292 CELT_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
2294 CELT_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
2297 for (i=0;i<M*st->mode->eBands[st->start];i++)
2301 int bound = M*st->mode->eBands[effEnd];
2302 if (st->downsample!=1)
2303 bound = IMIN(bound, N/st->downsample);
2304 for (i=M*st->mode->eBands[effEnd];i<N;i++)
2308 out_syn[0] = out_mem[0]+MAX_PERIOD-N;
2310 out_syn[1] = out_mem[1]+MAX_PERIOD-N;
2312 /* Compute inverse MDCTs */
2313 compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, C, LM);
2315 #ifdef ENABLE_POSTFILTER
2317 st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
2318 st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
2321 comb_filter(out_syn[c], out_syn[c], st->postfilter_period, st->postfilter_period, st->overlap,
2322 st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
2324 comb_filter(out_syn[c]+st->overlap, out_syn[c]+st->overlap, st->postfilter_period, postfilter_pitch, N-st->overlap,
2325 st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
2326 st->mode->window, st->mode->overlap);
2328 comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, N-st->overlap,
2329 st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
2330 st->mode->window, st->mode->overlap);
2333 st->postfilter_period_old = st->postfilter_period;
2334 st->postfilter_gain_old = st->postfilter_gain;
2335 st->postfilter_tapset_old = st->postfilter_tapset;
2336 st->postfilter_period = postfilter_pitch;
2337 st->postfilter_gain = postfilter_gain;
2338 st->postfilter_tapset = postfilter_tapset;
2339 #endif /* ENABLE_POSTFILTER */
2341 /* In case start or end were to change */
2344 for (i=0;i<st->start;i++)
2345 oldBandE[c*st->mode->nbEBands+i]=0;
2346 for (i=st->end;i<st->mode->nbEBands;i++)
2347 oldBandE[c*st->mode->nbEBands+i]=0;
2351 for (i=0;i<C*st->mode->nbEBands;i++)
2352 oldLogE2[i] = oldLogE[i];
2353 for (i=0;i<C*st->mode->nbEBands;i++)
2354 oldLogE[i] = oldBandE[i];
2355 for (i=0;i<C*st->mode->nbEBands;i++)
2356 backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
2358 for (i=0;i<C*st->mode->nbEBands;i++)
2359 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2363 deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
2366 if (ec_dec_tell(dec,0) > 8*len || ec_dec_get_error(dec))
2367 return CELT_CORRUPTED_DATA;
2373 #ifndef DISABLE_FLOAT_API
2374 int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size, ec_dec *dec)
2376 int j, ret, C, N, LM, M;
2377 VARDECL(celt_int16, out);
2381 return CELT_BAD_ARG;
2383 for (LM=0;LM<4;LM++)
2384 if (st->mode->shortMdctSize<<LM==frame_size)
2386 if (LM>=MAX_CONFIG_SIZES)
2387 return CELT_BAD_ARG;
2390 C = CHANNELS(st->channels);
2391 N = M*st->mode->shortMdctSize;
2393 ALLOC(out, C*N, celt_int16);
2394 ret=celt_decode_with_ec(st, data, len, out, frame_size, dec);
2397 pcm[j]=out[j]*(1.f/32768.f);
2402 #endif /*DISABLE_FLOAT_API*/
2404 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size, ec_dec *dec)
2406 int j, ret, C, N, LM, M;
2407 VARDECL(celt_sig, out);
2411 return CELT_BAD_ARG;
2413 for (LM=0;LM<4;LM++)
2414 if (st->mode->shortMdctSize<<LM==frame_size)
2416 if (LM>=MAX_CONFIG_SIZES)
2417 return CELT_BAD_ARG;
2420 C = CHANNELS(st->channels);
2421 N = M*st->mode->shortMdctSize;
2422 ALLOC(out, C*N, celt_sig);
2424 ret=celt_decode_with_ec_float(st, data, len, out, frame_size, dec);
2428 pcm[j] = FLOAT2INT16 (out[j]);
2435 int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size)
2437 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
2440 #ifndef DISABLE_FLOAT_API
2441 int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
2443 return celt_decode_with_ec_float(st, data, len, pcm, frame_size, NULL);
2445 #endif /* DISABLE_FLOAT_API */
2447 int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...)
2451 va_start(ap, request);
2454 case CELT_GET_MODE_REQUEST:
2456 const CELTMode ** value = va_arg(ap, const CELTMode**);
2462 case CELT_SET_START_BAND_REQUEST:
2464 celt_int32 value = va_arg(ap, celt_int32);
2465 if (value<0 || value>=st->mode->nbEBands)
2470 case CELT_SET_END_BAND_REQUEST:
2472 celt_int32 value = va_arg(ap, celt_int32);
2473 if (value<0 || value>=st->mode->nbEBands)
2478 case CELT_RESET_STATE:
2480 CELT_MEMSET((char*)&st->DECODER_RESET_START, 0,
2481 celt_decoder_get_size(st->mode, st->channels)-
2482 ((char*)&st->DECODER_RESET_START - (char*)st));
2492 return CELT_BAD_ARG;
2495 return CELT_UNIMPLEMENTED;
2498 const char *celt_strerror(int error)
2500 static const char *error_strings[8] = {
2506 "request not implemented",
2508 "memory allocation failed"
2510 if (error > 0 || error < -7)
2511 return "unknown error";
2513 return error_strings[-error];