1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 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 celt_word16 transientWindow[16] = {
58 279, 1106, 2454, 4276, 6510, 9081, 11900, 14872,
59 17896, 20868, 23687, 26258, 28492, 30314, 31662, 32489};
61 static const float transientWindow[16] = {
62 0.0085135f, 0.0337639f, 0.0748914f, 0.1304955f,
63 0.1986827f, 0.2771308f, 0.3631685f, 0.4538658f,
64 0.5461342f, 0.6368315f, 0.7228692f, 0.8013173f,
65 0.8695045f, 0.9251086f, 0.9662361f, 0.9914865f};
68 #define ENCODERVALID 0x4c434554
69 #define ENCODERPARTIAL 0x5445434c
70 #define ENCODERFREED 0x4c004500
77 const CELTMode *mode; /**< Mode used by the encoder */
83 celt_word16 tonal_average;
85 celt_word16 gain_prod;
86 celt_word32 frame_max;
89 /* VBR-related parameters */
90 celt_int32 vbr_reservoir;
92 celt_int32 vbr_offset;
95 celt_int32 vbr_rate_norm; /* Target number of 16th bits per frame */
96 celt_word32 preemph_memE[2];
97 celt_word32 preemph_memD[2];
100 celt_sig *overlap_mem[2];
102 celt_word16 *oldBandE;
105 static int check_encoder(const CELTEncoder *st)
109 celt_warning("NULL passed as an encoder structure");
110 return CELT_INVALID_STATE;
112 if (st->marker == ENCODERVALID)
114 if (st->marker == ENCODERFREED)
115 celt_warning("Referencing an encoder that has already been freed");
117 celt_warning("This is not a valid CELT encoder structure");
118 return CELT_INVALID_STATE;
121 CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error)
126 if (check_mode(mode) != CELT_OK)
129 *error = CELT_INVALID_MODE;
133 if (channels < 0 || channels > 2)
135 celt_warning("Only mono and stereo supported");
137 *error = CELT_BAD_ARG;
142 st = celt_alloc(sizeof(CELTEncoder));
147 *error = CELT_ALLOC_FAIL;
150 st->marker = ENCODERPARTIAL;
152 st->overlap = mode->overlap;
153 st->channels = channels;
156 st->end = st->mode->effEBands;
158 st->vbr_rate_norm = 0;
160 st->delayedIntra = 1;
161 st->tonal_average = QCONST16(1.f,8);
162 st->fold_decision = 1;
164 st->in_mem = celt_alloc(st->overlap*C*sizeof(celt_sig));
165 st->overlap_mem[0] = celt_alloc(st->overlap*C*sizeof(celt_sig));
167 st->overlap_mem[1] = st->overlap_mem[0] + st->overlap;
169 st->oldBandE = (celt_word16*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16));
171 if ((st->in_mem!=NULL) && (st->overlap_mem[0]!=NULL) && (st->oldBandE!=NULL))
175 st->marker = ENCODERVALID;
178 /* If the setup fails for some reason deallocate it. */
179 celt_encoder_destroy(st);
181 *error = CELT_ALLOC_FAIL;
185 void celt_encoder_destroy(CELTEncoder *st)
189 celt_warning("NULL passed to celt_encoder_destroy");
193 if (st->marker == ENCODERFREED)
195 celt_warning("Freeing an encoder which has already been freed");
199 if (st->marker != ENCODERVALID && st->marker != ENCODERPARTIAL)
201 celt_warning("This is not a valid CELT encoder structure");
204 /*Check_mode is non-fatal here because we can still free
205 the encoder memory even if the mode is bad, although calling
206 the free functions in this order is a violation of the API.*/
207 check_mode(st->mode);
209 celt_free(st->in_mem);
210 celt_free(st->overlap_mem[0]);
211 celt_free(st->oldBandE);
213 st->marker = ENCODERFREED;
218 static inline celt_int16 FLOAT2INT16(float x)
220 x = x*CELT_SIG_SCALE;
221 x = MAX32(x, -32768);
223 return (celt_int16)float2int(x);
226 static inline celt_word16 SIG2WORD16(celt_sig x)
229 x = PSHR32(x, SIG_SHIFT);
230 x = MAX32(x, -32768);
234 return (celt_word16)x;
238 static int transient_analysis(const celt_word32 * restrict in, int len, int C,
239 int *transient_time, int *transient_shift,
240 celt_word32 *frame_max, int overlap)
244 celt_word32 threshold;
245 VARDECL(celt_word32, begin);
247 ALLOC(begin, len+1, celt_word32);
252 begin[i+1] = MAX32(begin[i], ABS32(in[i]));
255 begin[i+1] = MAX32(begin[i], MAX32(ABS32(in[C*i]),
260 threshold = MULT16_32_Q15(QCONST16(.4f,15),begin[len]);
261 /* If the following condition isn't met, there's just no way
262 we'll have a transient*/
263 if (*frame_max < threshold)
265 /* It's likely we have a transient, now find it */
266 for (i=8;i<len-8;i++)
268 if (begin[i+1] < threshold)
277 ratio = DIV32(begin[len],1+MAX32(*frame_max, begin[n-16]));
281 *transient_shift = 3;
283 *transient_shift = 0;
286 *frame_max = begin[len-overlap];
292 /** Apply window and compute the MDCT for all sub-frames and
293 all channels in a frame */
294 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int _C, int LM)
296 const int C = CHANNELS(_C);
297 if (C==1 && !shortBlocks)
299 const int overlap = OVERLAP(mode);
300 clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM);
302 const int overlap = OVERLAP(mode);
303 int N = mode->shortMdctSize<<LM;
306 VARDECL(celt_word32, x);
307 VARDECL(celt_word32, tmp);
311 /*lookup = &mode->mdct[0];*/
312 N = mode->shortMdctSize;
315 ALLOC(x, N+overlap, celt_word32);
316 ALLOC(tmp, N, celt_word32);
322 for (j=0;j<N+overlap;j++)
323 x[j] = in[C*(b*N+j)+c];
324 clt_mdct_forward(&mode->mdct, x, tmp, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM);
325 /* Interleaving the sub-frames */
327 out[(j*B+b)+c*N*B] = tmp[j];
334 /** Compute the IMDCT and apply window for all sub-frames and
335 all channels in a frame */
336 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
337 int transient_time, int transient_shift, celt_sig * restrict out_mem[],
338 celt_sig * restrict overlap_mem[], int _C, int LM)
341 const int C = CHANNELS(_C);
342 const int N = mode->shortMdctSize<<LM;
343 const int overlap = OVERLAP(mode);
348 if (0 && transient_shift==0 && C==1 && !shortBlocks) {
349 clt_mdct_backward(&mode->mdct, X, out_mem[0]+(MAX_PERIOD-N-N4), mode->window, overlap, mode->maxLM-LM);
351 VARDECL(celt_word32, x);
352 VARDECL(celt_word32, tmp);
359 ALLOC(x, 2*N, celt_word32);
360 ALLOC(tmp, N, celt_word32);
364 /*lookup = &mode->mdct[0];*/
365 N2 = mode->shortMdctSize;
369 /* Prevents problems from the imdct doing the overlap-add */
370 CELT_MEMSET(x+N4, 0, N2);
374 /* De-interleaving the sub-frames */
376 tmp[j] = X[(j*B+b)+c*N2*B];
377 clt_mdct_backward(&mode->mdct, tmp, x+n4offset+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM);
380 if (transient_shift > 0)
384 x[N4+transient_time+j-16] = MULT16_32_Q15(SHR16(Q15_ONE-transientWindow[j],transient_shift)+transientWindow[j], SHL32(x[N4+transient_time+j-16],transient_shift));
385 for (j=transient_time;j<N+overlap;j++)
386 x[N4+j] = SHL32(x[N4+j], transient_shift);
389 x[N4+transient_time+j-16] *= 1+transientWindow[j]*((1<<transient_shift)-1);
390 for (j=transient_time;j<N+overlap;j++)
391 x[N4+j] *= 1<<transient_shift;
394 /* The first and last part would need to be set to zero
395 if we actually wanted to use them. */
396 for (j=0;j<overlap;j++)
397 out_mem[c][MAX_PERIOD-N+j] = x[j+N4] + overlap_mem[c][j];
399 out_mem[c][MAX_PERIOD-N+j] = x[j+N4];
400 for (j=0;j<overlap;j++)
401 overlap_mem[c][j] = x[N+j+N4];
407 static void deemphasis(celt_sig *in[], celt_word16 *pcm, int N, int _C, const celt_word16 *coef, celt_sig *mem)
409 const int C = CHANNELS(_C);
414 celt_sig * restrict x;
415 celt_word16 * restrict y;
417 x = &in[c][MAX_PERIOD-N];
421 celt_sig tmp = *x + m;
422 m = MULT16_32_Q15(coef[0], tmp)
423 - MULT16_32_Q15(coef[1], *x);
424 tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
425 *y = SCALEOUT(SIG2WORD16(tmp));
433 static void mdct_shape(const CELTMode *mode, celt_norm *X, int start,
435 int mdct_weight_shift, int end_band, int _C, int renorm, int M)
438 const int C = CHANNELS(_C);
440 for (m=start;m<end;m++)
441 for (i=m+c*N;i<(c+1)*N;i+=M)
443 X[i] = SHR16(X[i], mdct_weight_shift);
445 X[i] = (1.f/(1<<mdct_weight_shift))*X[i];
448 renormalise_bands(mode, X, end_band, C, M);
451 static signed char tf_select_table[4][8] = {
452 {0, -1, 0, -1, 0,-1, 0,-1},
453 {0, -1, 0, -2, 1, 0, 1 -1},
454 {0, -2, 0, -3, 2, 0, 1 -1},
455 {0, -2, 0, -3, 2, 0, 1 -1},
458 static int tf_analysis(celt_word16 *bandLogE, celt_word16 *oldBandE, int len, int C, int isTransient, int *tf_res, int nbCompressedBytes)
461 celt_word16 threshold;
462 VARDECL(celt_word16, metric);
463 celt_word32 average=0;
472 /* FIXME: Should check number of bytes *left* */
473 if (nbCompressedBytes<15*C)
479 if (nbCompressedBytes<40)
480 lambda = QCONST16(5.f, DB_SHIFT);
481 else if (nbCompressedBytes<60)
482 lambda = QCONST16(2.f, DB_SHIFT);
483 else if (nbCompressedBytes<100)
484 lambda = QCONST16(1.f, DB_SHIFT);
486 lambda = QCONST16(.5f, DB_SHIFT);
488 ALLOC(metric, len, celt_word16);
489 ALLOC(path0, len, int);
490 ALLOC(path1, len, int);
493 metric[i] = SUB16(bandLogE[i], oldBandE[i]);
494 average += metric[i];
501 metric[i] = HALF32(metric[i]) + HALF32(SUB16(bandLogE[i+len], oldBandE[i+len]));
502 average += metric[i];
505 average = DIV32(average, len);
507 printf ("%f\n", average);*/
510 threshold = QCONST16(1.f,DB_SHIFT);
511 tf_select = average > QCONST16(3.f,DB_SHIFT);
513 threshold = QCONST16(.5f,DB_SHIFT);
514 tf_select = average > QCONST16(1.f,DB_SHIFT);
518 /* Viterbi forward pass */
521 celt_word32 curr0, curr1;
522 celt_word32 from0, from1;
525 from1 = cost1 + lambda;
535 from0 = cost0 + lambda;
545 cost0 = curr0 + (metric[i]-threshold);
548 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
549 /* Viterbi backward pass to check the decisions */
550 for (i=len-2;i>=0;i--)
552 if (tf_res[i+1] == 1)
553 tf_res[i] = path1[i+1];
555 tf_res[i] = path0[i+1];
561 static void tf_encode(int start, int end, int isTransient, int *tf_res, int nbCompressedBytes, int LM, int tf_select, ec_enc *enc)
564 if (8*nbCompressedBytes - ec_enc_tell(enc, 0) < 100)
566 for (i=start;i<end;i++)
567 tf_res[i] = isTransient;
569 ec_enc_bit_prob(enc, tf_res[start], isTransient ? 16384 : 4096);
570 curr = tf_res[start];
571 for (i=start+1;i<end;i++)
573 ec_enc_bit_prob(enc, tf_res[i] ^ curr, isTransient ? 4096 : 2048);
577 ec_enc_bits(enc, tf_select, 1);
578 for (i=start;i<end;i++)
579 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
582 static void tf_decode(int start, int end, int C, int isTransient, int *tf_res, int nbCompressedBytes, int LM, ec_dec *dec)
584 int i, curr, tf_select;
585 if (8*nbCompressedBytes - ec_dec_tell(dec, 0) < 100)
587 for (i=start;i<end;i++)
588 tf_res[i] = isTransient;
590 tf_res[start] = ec_dec_bit_prob(dec, isTransient ? 16384 : 4096);
591 curr = tf_res[start];
592 for (i=start+1;i<end;i++)
594 tf_res[i] = ec_dec_bit_prob(dec, isTransient ? 4096 : 2048) ^ curr;
598 tf_select = ec_dec_bits(dec, 1);
599 for (i=start;i<end;i++)
600 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
604 int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
607 int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, celt_sig * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
615 VARDECL(celt_sig, in);
616 VARDECL(celt_sig, freq);
617 VARDECL(celt_norm, X);
618 VARDECL(celt_ener, bandE);
619 VARDECL(celt_word16, bandLogE);
620 VARDECL(int, fine_quant);
621 VARDECL(celt_word16, error);
622 VARDECL(int, pulses);
623 VARDECL(int, offsets);
624 VARDECL(int, fine_priority);
625 VARDECL(int, tf_res);
628 int transient_time, transient_time_quant;
631 const int C = CHANNELS(st->channels);
632 int mdct_weight_shift = 0;
633 int mdct_weight_pos=0;
636 int nbFilledBytes, nbAvailableBytes;
640 if (check_encoder(st) != CELT_OK)
641 return CELT_INVALID_STATE;
643 if (check_mode(st->mode) != CELT_OK)
644 return CELT_INVALID_MODE;
646 if (nbCompressedBytes<0 || pcm==NULL)
650 if (st->mode->shortMdctSize<<LM==frame_size)
652 if (LM>=MAX_CONFIG_SIZES)
658 ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes);
659 ec_enc_init(&_enc,&buf);
663 nbFilledBytes=(ec_enc_tell(enc, 0)+4)>>3;
665 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
668 if (effEnd > st->mode->effEBands)
669 effEnd = st->mode->effEBands;
671 N = M*st->mode->shortMdctSize;
672 N4 = (N-st->overlap)>>1;
673 ALLOC(in, 2*C*N-2*C*N4, celt_sig);
675 CELT_COPY(in, st->in_mem, C*st->overlap);
678 const celt_word16 * restrict pcmp = pcm+c;
679 celt_sig * restrict inp = in+C*st->overlap+c;
682 /* Apply pre-emphasis */
683 celt_sig tmp = MULT16_16(st->mode->preemph[2], SCALEIN(*pcmp));
684 *inp = tmp + st->preemph_memE[c];
685 st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
686 - MULT16_32_Q15(st->mode->preemph[0], tmp);
691 CELT_COPY(st->in_mem, in+C*(2*N-2*N4-st->overlap), C*st->overlap);
693 /* Transient handling */
695 transient_time_quant = -1;
699 resynth = optional_resynthesis!=NULL;
701 if (M > 1 && transient_analysis(in, N+st->overlap, C, &transient_time, &transient_shift, &st->frame_max, st->overlap))
706 /* Apply the inverse shaping window */
709 transient_time_quant = transient_time*(celt_int32)8000/st->mode->Fs;
710 transient_time = transient_time_quant*(celt_int32)st->mode->Fs/8000;
714 in[C*(transient_time+i-16)+c] = MULT16_32_Q15(EXTRACT16(SHR32(celt_rcp(Q15ONE+MULT16_16(transientWindow[i],((1<<transient_shift)-1))),1)), in[C*(transient_time+i-16)+c]);
716 for (i=transient_time;i<N+st->overlap;i++)
717 in[C*i+c] = SHR32(in[C*i+c], transient_shift);
721 in[C*(transient_time+i-16)+c] /= 1+transientWindow[i]*((1<<transient_shift)-1);
722 gain_1 = 1.f/(1<<transient_shift);
724 for (i=transient_time;i<N+st->overlap;i++)
737 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
738 ALLOC(bandE,st->mode->nbEBands*C, celt_ener);
739 ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16);
741 compute_mdcts(st->mode, shortBlocks, in, freq, C, LM);
743 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
745 compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
747 amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
749 /* Band normalisation */
750 normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
752 NN = M*st->mode->eBands[effEnd];
753 if (shortBlocks && !transient_shift)
755 celt_word32 sum[8]={1,1,1,1,1,1,1,1};
762 for (i=m+c*N;i<c*N+NN;i+=M)
770 if (SHR32(sum[m+1],3) > sum[m])
774 } else if (SHR32(sum[m+1],1) > sum[m] && mdct_weight_shift < 2)
783 if (sum[m+1] > 8*sum[m])
787 } else if (sum[m+1] > 2*sum[m] && mdct_weight_shift < 2)
795 if (mdct_weight_shift)
796 mdct_shape(st->mode, X, mdct_weight_pos+1, M, N, mdct_weight_shift, effEnd, C, 0, M);
799 ALLOC(tf_res, st->mode->nbEBands, int);
800 /* Needs to be before coarse energy quantization because otherwise the energy gets modified */
801 tf_select = tf_analysis(bandLogE, st->oldBandE, effEnd, C, isTransient, tf_res, nbAvailableBytes);
802 for (i=effEnd;i<st->end;i++)
803 tf_res[i] = tf_res[effEnd-1];
805 ALLOC(error, C*st->mode->nbEBands, celt_word16);
806 quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
807 st->oldBandE, nbCompressedBytes*8, st->mode->prob,
808 error, enc, C, LM, nbAvailableBytes, st->force_intra, &st->delayedIntra);
810 ec_enc_bit_prob(enc, shortBlocks!=0, 8192);
816 int max_time = (N+st->mode->overlap)*(celt_int32)8000/st->mode->Fs;
817 ec_enc_uint(enc, transient_shift, 4);
818 ec_enc_uint(enc, transient_time_quant, max_time);
820 ec_enc_uint(enc, mdct_weight_shift, 4);
821 if (mdct_weight_shift && M!=2)
822 ec_enc_uint(enc, mdct_weight_pos, M-1);
826 tf_encode(st->start, st->end, isTransient, tf_res, nbAvailableBytes, LM, tf_select, enc);
828 if (!shortBlocks && !folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, effEnd, C, M))
830 ec_enc_bit_prob(enc, has_fold>>1, 8192);
831 ec_enc_bit_prob(enc, has_fold&1, (has_fold>>1) ? 32768 : 49152);
833 /* Variable bitrate */
834 if (st->vbr_rate_norm>0)
838 /* The target rate in 16th bits per frame */
841 celt_int32 vbr_bound, max_allowed;
843 vbr_rate = M*st->vbr_rate_norm;
845 /* Computes the max bit-rate allowed in VBR more to avoid busting the budget */
846 vbr_bound = vbr_rate;
847 max_allowed = (vbr_rate + vbr_bound - st->vbr_reservoir)>>(BITRES+3);
850 if (max_allowed < nbAvailableBytes)
851 nbAvailableBytes = max_allowed;
854 /* Shortblocks get a large boost in bitrate, but since they
855 are uncommon long blocks are not greatly effected */
859 target-=(target+14)/28;
861 /* The average energy is removed from the target and the actual
863 target=target+st->vbr_offset-588+ec_enc_tell(enc, BITRES);
865 /* In VBR mode the frame size must not be reduced so much that it would result in the coarse energy busting its budget */
866 target=IMIN(nbAvailableBytes,target);
867 /* Make the adaptation coef (alpha) higher at the beginning */
868 if (st->vbr_count < 990)
871 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+10),16));
872 /*printf ("%d %d\n", st->vbr_count+10, alpha);*/
874 alpha = QCONST16(.001f,15);
876 /* By how much did we "miss" the target on that frame */
877 delta = (8<<BITRES)*(celt_int32)target - vbr_rate;
878 /* How many bits have we used in excess of what we're allowed */
879 st->vbr_reservoir += delta;
880 /*printf ("%d\n", st->vbr_reservoir);*/
882 /* Compute the offset we need to apply in order to reach the target */
883 st->vbr_drift += MULT16_32_Q15(alpha,delta-st->vbr_offset-st->vbr_drift);
884 st->vbr_offset = -st->vbr_drift;
885 /*printf ("%d\n", st->vbr_drift);*/
887 /* We could use any multiple of vbr_rate as bound (depending on the delay) */
888 if (st->vbr_reservoir < 0)
890 /* We're under the min value -- increase rate */
891 int adjust = 1-(st->vbr_reservoir-1)/(8<<BITRES);
892 st->vbr_reservoir += adjust*(8<<BITRES);
894 /*printf ("+%d\n", adjust);*/
896 if (target < nbAvailableBytes)
897 nbAvailableBytes = target;
898 nbCompressedBytes = nbAvailableBytes + nbFilledBytes;
900 /* This moves the raw bits to take into account the new compressed size */
901 ec_byte_shrink(&buf, nbCompressedBytes);
905 ALLOC(fine_quant, st->mode->nbEBands, int);
906 ALLOC(pulses, st->mode->nbEBands, int);
907 ALLOC(offsets, st->mode->nbEBands, int);
908 ALLOC(fine_priority, st->mode->nbEBands, int);
910 for (i=0;i<st->mode->nbEBands;i++)
912 bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1;
913 compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, M);
915 quant_fine_energy(st->mode, st->start, st->end, bandE, st->oldBandE, error, fine_quant, enc, C);
917 #ifdef MEASURE_NORM_MSE
922 X0[i+c*N] = X[i+c*N];
923 for (i=0;i<C*st->mode->nbEBands;i++)
924 bandE0[i] = bandE[i];
927 /* Residual quantisation */
928 quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, bandE, pulses, shortBlocks, has_fold, tf_res, resynth, nbCompressedBytes*8, enc, LM);
930 quant_energy_finalise(st->mode, st->start, st->end, bandE, st->oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_enc_tell(enc, 0), enc, C);
932 /* Re-synthesis of the coded audio if required */
935 VARDECL(celt_sig, _out_mem);
936 celt_sig *out_mem[2];
938 log2Amp(st->mode, st->start, st->end, bandE, st->oldBandE, C);
940 #ifdef MEASURE_NORM_MSE
941 measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
944 if (mdct_weight_shift)
946 mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, mdct_weight_shift, effEnd, C, 1, M);
950 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
953 for (i=0;i<M*st->mode->eBands[st->start];i++)
956 for (i=M*st->mode->eBands[st->end];i<N;i++)
959 ALLOC(_out_mem, C*N, celt_sig);
960 out_mem[0] = _out_mem;
962 out_mem[1] = _out_mem+N;
964 compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time,
965 transient_shift, out_mem, st->overlap_mem, C, LM);
967 /* De-emphasis and put everything back at the right place
968 in the synthesis history */
969 if (optional_resynthesis != NULL) {
970 deemphasis(out_mem, optional_resynthesis, N, C, st->mode->preemph, st->preemph_memD);
975 /* If there's any room left (can only happen for very high rates),
976 fill it with zeros */
977 while (ec_enc_tell(enc,0) + 8 <= nbCompressedBytes*8)
978 ec_enc_bits(enc, 0, 8);
982 if (ec_enc_get_error(enc))
983 return CELT_CORRUPTED_DATA;
985 return nbCompressedBytes;
989 #ifndef DISABLE_FLOAT_API
990 int celt_encode_with_ec_float(CELTEncoder * restrict st, const float * pcm, float * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
992 int j, ret, C, N, LM, M;
993 VARDECL(celt_int16, in);
996 if (check_encoder(st) != CELT_OK)
997 return CELT_INVALID_STATE;
999 if (check_mode(st->mode) != CELT_OK)
1000 return CELT_INVALID_MODE;
1003 return CELT_BAD_ARG;
1005 for (LM=0;LM<4;LM++)
1006 if (st->mode->shortMdctSize<<LM==frame_size)
1008 if (LM>=MAX_CONFIG_SIZES)
1009 return CELT_BAD_ARG;
1012 C = CHANNELS(st->channels);
1013 N = M*st->mode->shortMdctSize;
1014 ALLOC(in, C*N, celt_int16);
1017 in[j] = FLOAT2INT16(pcm[j]);
1019 if (optional_resynthesis != NULL) {
1020 ret=celt_encode_with_ec(st,in,in,frame_size,compressed,nbCompressedBytes, enc);
1022 optional_resynthesis[j]=in[j]*(1.f/32768.f);
1024 ret=celt_encode_with_ec(st,in,NULL,frame_size,compressed,nbCompressedBytes, enc);
1030 #endif /*DISABLE_FLOAT_API*/
1032 int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1034 int j, ret, C, N, LM, M;
1035 VARDECL(celt_sig, in);
1038 if (check_encoder(st) != CELT_OK)
1039 return CELT_INVALID_STATE;
1041 if (check_mode(st->mode) != CELT_OK)
1042 return CELT_INVALID_MODE;
1045 return CELT_BAD_ARG;
1047 for (LM=0;LM<4;LM++)
1048 if (st->mode->shortMdctSize<<LM==frame_size)
1050 if (LM>=MAX_CONFIG_SIZES)
1051 return CELT_BAD_ARG;
1054 C=CHANNELS(st->channels);
1055 N=M*st->mode->shortMdctSize;
1056 ALLOC(in, C*N, celt_sig);
1057 for (j=0;j<C*N;j++) {
1058 in[j] = SCALEOUT(pcm[j]);
1061 if (optional_resynthesis != NULL) {
1062 ret = celt_encode_with_ec_float(st,in,in,frame_size,compressed,nbCompressedBytes, enc);
1064 optional_resynthesis[j] = FLOAT2INT16(in[j]);
1066 ret = celt_encode_with_ec_float(st,in,NULL,frame_size,compressed,nbCompressedBytes, enc);
1073 int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1075 return celt_encode_with_ec(st, pcm, NULL, frame_size, compressed, nbCompressedBytes, NULL);
1078 #ifndef DISABLE_FLOAT_API
1079 int celt_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1081 return celt_encode_with_ec_float(st, pcm, NULL, frame_size, compressed, nbCompressedBytes, NULL);
1083 #endif /* DISABLE_FLOAT_API */
1085 int celt_encode_resynthesis(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1087 return celt_encode_with_ec(st, pcm, optional_resynthesis, frame_size, compressed, nbCompressedBytes, NULL);
1090 #ifndef DISABLE_FLOAT_API
1091 int celt_encode_resynthesis_float(CELTEncoder * restrict st, const float * pcm, float * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1093 return celt_encode_with_ec_float(st, pcm, optional_resynthesis, frame_size, compressed, nbCompressedBytes, NULL);
1095 #endif /* DISABLE_FLOAT_API */
1098 int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...)
1102 if (check_encoder(st) != CELT_OK)
1103 return CELT_INVALID_STATE;
1105 va_start(ap, request);
1106 if ((request!=CELT_GET_MODE_REQUEST) && (check_mode(st->mode) != CELT_OK))
1110 case CELT_GET_MODE_REQUEST:
1112 const CELTMode ** value = va_arg(ap, const CELTMode**);
1118 case CELT_SET_COMPLEXITY_REQUEST:
1120 int value = va_arg(ap, celt_int32);
1121 if (value<0 || value>10)
1125 case CELT_SET_START_BAND_REQUEST:
1127 celt_int32 value = va_arg(ap, celt_int32);
1128 if (value<0 || value>=st->mode->nbEBands)
1133 case CELT_SET_END_BAND_REQUEST:
1135 celt_int32 value = va_arg(ap, celt_int32);
1136 if (value<0 || value>=st->mode->nbEBands)
1141 case CELT_SET_PREDICTION_REQUEST:
1143 int value = va_arg(ap, celt_int32);
1144 if (value<0 || value>2)
1148 st->force_intra = 1;
1149 } else if (value==1) {
1150 st->force_intra = 0;
1152 st->force_intra = 0;
1156 case CELT_SET_VBR_RATE_REQUEST:
1158 celt_int32 value = va_arg(ap, celt_int32);
1160 int N = st->mode->shortMdctSize;
1165 frame_rate = ((st->mode->Fs<<3)+(N>>1))/N;
1166 st->vbr_rate_norm = ((value<<(BITRES+3))+(frame_rate>>1))/frame_rate;
1169 case CELT_RESET_STATE:
1171 const CELTMode *mode = st->mode;
1172 int C = st->channels;
1174 CELT_MEMSET(st->in_mem, 0, st->overlap*C);
1175 CELT_MEMSET(st->overlap_mem[0], 0, st->overlap*C);
1177 CELT_MEMSET(st->oldBandE, 0, C*mode->nbEBands);
1179 CELT_MEMSET(st->preemph_memE, 0, C);
1180 CELT_MEMSET(st->preemph_memD, 0, C);
1181 st->delayedIntra = 1;
1183 st->fold_decision = 1;
1184 st->tonal_average = QCONST16(1.f,8);
1186 st->vbr_reservoir = 0;
1200 return CELT_INVALID_MODE;
1203 return CELT_BAD_ARG;
1206 return CELT_UNIMPLEMENTED;
1209 /**********************************************************************/
1213 /**********************************************************************/
1214 #define DECODE_BUFFER_SIZE 2048
1216 #define DECODERVALID 0x4c434454
1217 #define DECODERPARTIAL 0x5444434c
1218 #define DECODERFREED 0x4c004400
1221 @brief Decoder state
1223 struct CELTDecoder {
1225 const CELTMode *mode;
1231 celt_sig preemph_memD[2];
1233 celt_sig *out_mem[2];
1234 celt_sig *decode_mem[2];
1235 celt_sig *overlap_mem[2];
1237 celt_word16 *oldBandE;
1241 int last_pitch_index;
1245 int check_decoder(const CELTDecoder *st)
1249 celt_warning("NULL passed a decoder structure");
1250 return CELT_INVALID_STATE;
1252 if (st->marker == DECODERVALID)
1254 if (st->marker == DECODERFREED)
1255 celt_warning("Referencing a decoder that has already been freed");
1257 celt_warning("This is not a valid CELT decoder structure");
1258 return CELT_INVALID_STATE;
1261 CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error)
1266 if (check_mode(mode) != CELT_OK)
1269 *error = CELT_INVALID_MODE;
1273 if (channels < 0 || channels > 2)
1275 celt_warning("Only mono and stereo supported");
1277 *error = CELT_BAD_ARG;
1281 C = CHANNELS(channels);
1282 st = celt_alloc(sizeof(CELTDecoder));
1287 *error = CELT_ALLOC_FAIL;
1291 st->marker = DECODERPARTIAL;
1293 st->overlap = mode->overlap;
1294 st->channels = channels;
1297 st->end = st->mode->effEBands;
1299 st->decode_mem[0] = (celt_sig*)celt_alloc((DECODE_BUFFER_SIZE+st->overlap)*C*sizeof(celt_sig));
1301 st->decode_mem[1] = st->decode_mem[0] + (DECODE_BUFFER_SIZE+st->overlap);
1304 st->out_mem[c] = st->decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
1305 st->overlap_mem[c] = st->decode_mem[c]+DECODE_BUFFER_SIZE;
1307 st->oldBandE = (celt_word16*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16));
1309 st->lpc = (celt_word16*)celt_alloc(C*LPC_ORDER*sizeof(celt_word16));
1313 if ((st->decode_mem!=NULL) && (st->out_mem[0]!=NULL) && (st->oldBandE!=NULL) &&
1318 st->marker = DECODERVALID;
1321 /* If the setup fails for some reason deallocate it. */
1322 celt_decoder_destroy(st);
1324 *error = CELT_ALLOC_FAIL;
1328 void celt_decoder_destroy(CELTDecoder *st)
1332 celt_warning("NULL passed to celt_decoder_destroy");
1336 if (st->marker == DECODERFREED)
1338 celt_warning("Freeing a decoder which has already been freed");
1342 if (st->marker != DECODERVALID && st->marker != DECODERPARTIAL)
1344 celt_warning("This is not a valid CELT decoder structure");
1348 /*Check_mode is non-fatal here because we can still free
1349 the encoder memory even if the mode is bad, although calling
1350 the free functions in this order is a violation of the API.*/
1351 check_mode(st->mode);
1353 celt_free(st->decode_mem[0]);
1354 celt_free(st->oldBandE);
1357 st->marker = DECODERFREED;
1362 static void celt_decode_lost(CELTDecoder * restrict st, celt_word16 * restrict pcm, int N, int LM)
1366 int overlap = st->mode->overlap;
1367 celt_word16 fade = Q15ONE;
1369 const int C = CHANNELS(st->channels);
1373 len = N+st->mode->overlap;
1375 if (st->loss_count == 0)
1377 celt_word16 pitch_buf[MAX_PERIOD>>1];
1379 celt_word32 mem0[2]={0,0};
1380 celt_word16 mem1[2]={0,0};
1382 /* FIXME: This is a kludge */
1383 if (len2>MAX_PERIOD>>1)
1384 len2 = MAX_PERIOD>>1;
1385 pitch_downsample(st->out_mem, pitch_buf, MAX_PERIOD, MAX_PERIOD,
1387 pitch_search(st->mode, pitch_buf+((MAX_PERIOD-len2)>>1), pitch_buf, len2,
1388 MAX_PERIOD-len2-100, &pitch_index, &tmp, 1<<LM);
1389 pitch_index = MAX_PERIOD-len2-pitch_index;
1390 st->last_pitch_index = pitch_index;
1392 pitch_index = st->last_pitch_index;
1393 if (st->loss_count < 5)
1394 fade = QCONST16(.8f,15);
1401 /* FIXME: This is more memory than necessary */
1402 celt_word32 e[2*MAX_PERIOD];
1403 celt_word16 exc[2*MAX_PERIOD];
1404 celt_word32 ac[LPC_ORDER+1];
1405 celt_word16 decay = 1;
1407 celt_word16 mem[LPC_ORDER]={0};
1409 offset = MAX_PERIOD-pitch_index;
1410 for (i=0;i<MAX_PERIOD;i++)
1411 exc[i] = ROUND16(st->out_mem[c][i], SIG_SHIFT);
1413 if (st->loss_count == 0)
1415 _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
1416 LPC_ORDER, MAX_PERIOD);
1418 /* Noise floor -40 dB */
1420 ac[0] += SHR32(ac[0],13);
1425 for (i=1;i<=LPC_ORDER;i++)
1427 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
1429 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
1431 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
1435 _celt_lpc(st->lpc+c*LPC_ORDER, ac, LPC_ORDER);
1437 fir(exc, st->lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
1438 /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
1439 /* Check if the waveform is decaying (and if so how fast) */
1441 celt_word32 E1=1, E2=1;
1443 if (pitch_index <= MAX_PERIOD/2)
1444 period = pitch_index;
1446 period = MAX_PERIOD/2;
1447 for (i=0;i<period;i++)
1449 E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
1450 E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
1454 decay = celt_sqrt(frac_div32(SHR(E1,1),E2));
1457 /* Copy excitation, taking decay into account */
1458 for (i=0;i<len+st->mode->overlap;i++)
1460 if (offset+i >= MAX_PERIOD)
1462 offset -= pitch_index;
1463 decay = MULT16_16_Q15(decay, decay);
1465 e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
1466 S1 += SHR32(MULT16_16(st->out_mem[c][offset+i],st->out_mem[c][offset+i]),8);
1469 iir(e, st->lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
1473 for (i=0;i<len+overlap;i++)
1474 S2 += SHR32(MULT16_16(e[i],e[i]),8);
1475 /* This checks for an "explosion" in the synthesis */
1477 if (!(S1 > SHR32(S2,2)))
1479 /* Float test is written this way to catch NaNs at the same time */
1480 if (!(S1 > 0.2f*S2))
1483 for (i=0;i<len+overlap;i++)
1487 celt_word16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
1488 for (i=0;i<len+overlap;i++)
1489 e[i] = MULT16_16_Q15(ratio, e[i]);
1493 for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
1494 st->out_mem[c][i] = st->out_mem[c][N+i];
1496 /* Apply TDAC to the concealed audio so that it blends with the
1497 previous and next frames */
1498 for (i=0;i<overlap/2;i++)
1500 celt_word32 tmp1, tmp2;
1501 tmp1 = MULT16_32_Q15(st->mode->window[i ], e[i ]) -
1502 MULT16_32_Q15(st->mode->window[overlap-i-1], e[overlap-i-1]);
1503 tmp2 = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
1504 MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
1505 tmp1 = MULT16_32_Q15(fade, tmp1);
1506 tmp2 = MULT16_32_Q15(fade, tmp2);
1507 st->out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp2);
1508 st->out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp2);
1509 st->out_mem[c][MAX_PERIOD-N+i] += MULT16_32_Q15(st->mode->window[i], tmp1);
1510 st->out_mem[c][MAX_PERIOD-N+overlap-i-1] -= MULT16_32_Q15(st->mode->window[overlap-i-1], tmp1);
1512 for (i=0;i<N-overlap;i++)
1513 st->out_mem[c][MAX_PERIOD-N+overlap+i] = MULT16_32_Q15(fade, e[overlap+i]);
1516 deemphasis(st->out_mem, pcm, N, C, st->mode->preemph, st->preemph_memD);
1524 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size, ec_dec *dec)
1527 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)
1535 VARDECL(celt_sig, freq);
1536 VARDECL(celt_norm, X);
1537 VARDECL(celt_ener, bandE);
1538 VARDECL(int, fine_quant);
1539 VARDECL(int, pulses);
1540 VARDECL(int, offsets);
1541 VARDECL(int, fine_priority);
1542 VARDECL(int, tf_res);
1548 int transient_shift;
1549 int mdct_weight_shift=0;
1550 const int C = CHANNELS(st->channels);
1551 int mdct_weight_pos=0;
1553 int nbFilledBytes, nbAvailableBytes;
1557 if (check_decoder(st) != CELT_OK)
1558 return CELT_INVALID_STATE;
1560 if (check_mode(st->mode) != CELT_OK)
1561 return CELT_INVALID_MODE;
1564 return CELT_BAD_ARG;
1566 for (LM=0;LM<4;LM++)
1567 if (st->mode->shortMdctSize<<LM==frame_size)
1569 if (LM>=MAX_CONFIG_SIZES)
1570 return CELT_BAD_ARG;
1573 N = M*st->mode->shortMdctSize;
1574 N4 = (N-st->overlap)>>1;
1577 if (effEnd > st->mode->effEBands)
1578 effEnd = st->mode->effEBands;
1580 ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
1581 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1582 ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
1584 for (i=0;i<M*st->mode->eBands[st->start];i++)
1587 for (i=M*st->mode->eBands[effEnd];i<N;i++)
1592 celt_decode_lost(st, pcm, N, LM);
1598 return CELT_BAD_ARG;
1603 ec_byte_readinit(&buf,(unsigned char*)data,len);
1604 ec_dec_init(&_dec,&buf);
1608 nbFilledBytes = (ec_dec_tell(dec, 0)+4)>>3;
1610 nbAvailableBytes = len-nbFilledBytes;
1612 /* Decode the global flags (first symbols in the stream) */
1613 intra_ener = ec_dec_bit_prob(dec, 8192);
1614 /* Get band energies */
1615 unquant_coarse_energy(st->mode, st->start, st->end, bandE, st->oldBandE, intra_ener, st->mode->prob, dec, C, LM);
1617 isTransient = ec_dec_bit_prob(dec, 8192);
1626 transient_shift = ec_dec_uint(dec, 4);
1627 if (transient_shift == 3)
1629 int transient_time_quant;
1630 int max_time = (N+st->mode->overlap)*(celt_int32)8000/st->mode->Fs;
1631 transient_time_quant = ec_dec_uint(dec, max_time);
1632 transient_time = transient_time_quant*(celt_int32)st->mode->Fs/8000;
1634 mdct_weight_shift = transient_shift;
1635 if (mdct_weight_shift && M>2)
1636 mdct_weight_pos = ec_dec_uint(dec, M-1);
1637 transient_shift = 0;
1641 transient_time = -1;
1642 transient_shift = 0;
1645 ALLOC(tf_res, st->mode->nbEBands, int);
1646 tf_decode(st->start, st->end, C, isTransient, tf_res, nbAvailableBytes, LM, dec);
1648 has_fold = ec_dec_bit_prob(dec, 8192)<<1;
1649 has_fold |= ec_dec_bit_prob(dec, (has_fold>>1) ? 32768 : 49152);
1651 ALLOC(pulses, st->mode->nbEBands, int);
1652 ALLOC(offsets, st->mode->nbEBands, int);
1653 ALLOC(fine_priority, st->mode->nbEBands, int);
1655 for (i=0;i<st->mode->nbEBands;i++)
1658 bits = len*8 - ec_dec_tell(dec, 0) - 1;
1659 ALLOC(fine_quant, st->mode->nbEBands, int);
1660 compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, M);
1661 /*bits = ec_dec_tell(dec, 0);
1662 compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(dec, 0)-bits))/C);*/
1664 unquant_fine_energy(st->mode, st->start, st->end, bandE, st->oldBandE, fine_quant, dec, C);
1666 /* Decode fixed codebook */
1667 quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, NULL, pulses, shortBlocks, has_fold, tf_res, 1, len*8, dec, LM);
1669 unquant_energy_finalise(st->mode, st->start, st->end, bandE, st->oldBandE, fine_quant, fine_priority, len*8-ec_dec_tell(dec, 0), dec, C);
1671 log2Amp(st->mode, st->start, st->end, bandE, st->oldBandE, C);
1673 if (mdct_weight_shift)
1675 mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, mdct_weight_shift, effEnd, C, 1, M);
1679 denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
1681 CELT_MOVE(st->decode_mem[0], st->decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
1683 CELT_MOVE(st->decode_mem[1], st->decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
1686 for (i=0;i<M*st->mode->eBands[st->start];i++)
1689 for (i=M*st->mode->eBands[effEnd];i<N;i++)
1692 /* Compute inverse MDCTs */
1693 compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time,
1694 transient_shift, st->out_mem, st->overlap_mem, C, LM);
1696 deemphasis(st->out_mem, pcm, N, C, st->mode->preemph, st->preemph_memD);
1699 if (ec_dec_get_error(dec))
1700 return CELT_CORRUPTED_DATA;
1706 #ifndef DISABLE_FLOAT_API
1707 int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size, ec_dec *dec)
1709 int j, ret, C, N, LM, M;
1710 VARDECL(celt_int16, out);
1713 if (check_decoder(st) != CELT_OK)
1714 return CELT_INVALID_STATE;
1716 if (check_mode(st->mode) != CELT_OK)
1717 return CELT_INVALID_MODE;
1720 return CELT_BAD_ARG;
1722 for (LM=0;LM<4;LM++)
1723 if (st->mode->shortMdctSize<<LM==frame_size)
1725 if (LM>=MAX_CONFIG_SIZES)
1726 return CELT_BAD_ARG;
1729 C = CHANNELS(st->channels);
1730 N = M*st->mode->shortMdctSize;
1732 ALLOC(out, C*N, celt_int16);
1733 ret=celt_decode_with_ec(st, data, len, out, frame_size, dec);
1736 pcm[j]=out[j]*(1.f/32768.f);
1741 #endif /*DISABLE_FLOAT_API*/
1743 int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size, ec_dec *dec)
1745 int j, ret, C, N, LM, M;
1746 VARDECL(celt_sig, out);
1749 if (check_decoder(st) != CELT_OK)
1750 return CELT_INVALID_STATE;
1752 if (check_mode(st->mode) != CELT_OK)
1753 return CELT_INVALID_MODE;
1756 return CELT_BAD_ARG;
1758 for (LM=0;LM<4;LM++)
1759 if (st->mode->shortMdctSize<<LM==frame_size)
1761 if (LM>=MAX_CONFIG_SIZES)
1762 return CELT_BAD_ARG;
1765 C = CHANNELS(st->channels);
1766 N = M*st->mode->shortMdctSize;
1767 ALLOC(out, C*N, celt_sig);
1769 ret=celt_decode_with_ec_float(st, data, len, out, frame_size, dec);
1773 pcm[j] = FLOAT2INT16 (out[j]);
1780 int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size)
1782 return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
1785 #ifndef DISABLE_FLOAT_API
1786 int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
1788 return celt_decode_with_ec_float(st, data, len, pcm, frame_size, NULL);
1790 #endif /* DISABLE_FLOAT_API */
1792 int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...)
1796 if (check_decoder(st) != CELT_OK)
1797 return CELT_INVALID_STATE;
1799 va_start(ap, request);
1800 if ((request!=CELT_GET_MODE_REQUEST) && (check_mode(st->mode) != CELT_OK))
1804 case CELT_GET_MODE_REQUEST:
1806 const CELTMode ** value = va_arg(ap, const CELTMode**);
1812 case CELT_SET_START_BAND_REQUEST:
1814 celt_int32 value = va_arg(ap, celt_int32);
1815 if (value<0 || value>=st->mode->nbEBands)
1820 case CELT_SET_END_BAND_REQUEST:
1822 celt_int32 value = va_arg(ap, celt_int32);
1823 if (value<0 || value>=st->mode->nbEBands)
1828 case CELT_RESET_STATE:
1830 const CELTMode *mode = st->mode;
1831 int C = st->channels;
1833 CELT_MEMSET(st->decode_mem[0], 0, (DECODE_BUFFER_SIZE+st->overlap)*C);
1834 CELT_MEMSET(st->oldBandE, 0, C*mode->nbEBands);
1836 CELT_MEMSET(st->preemph_memD, 0, C);
1840 CELT_MEMSET(st->lpc, 0, C*LPC_ORDER);
1850 return CELT_INVALID_MODE;
1853 return CELT_BAD_ARG;
1856 return CELT_UNIMPLEMENTED;
1859 const char *celt_strerror(int error)
1861 static const char *error_strings[8] = {
1867 "request not implemented",
1869 "memory allocation failed"
1871 if (error > 0 || error < -7)
1872 return "unknown error";
1874 return error_strings[-error];