1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
2 (C) 2008 Gregory Maxwell */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "os_support.h"
43 #include "kiss_fftr.h"
47 #include "quant_bands.h"
50 #include "stack_alloc.h"
52 #include "float_cast.h"
55 static const celt_word16_t preemph = QCONST16(0.8f,15);
58 static const celt_word16_t transientWindow[16] = {
59 279, 1106, 2454, 4276, 6510, 9081, 11900, 14872,
60 17896, 20868, 23687, 26258, 28492, 30314, 31662, 32489};
62 static const float transientWindow[16] = {
63 0.0085135, 0.0337639, 0.0748914, 0.1304955, 0.1986827, 0.2771308, 0.3631685, 0.4538658,
64 0.5461342, 0.6368315, 0.7228692, 0.8013173, 0.8695045, 0.9251086, 0.9662361, 0.9914865};
72 const CELTMode *mode; /**< Mode used by the encoder */
81 celt_word16_t * restrict preemph_memE; /* Input is 16-bit, so why bother with 32 */
82 celt_sig_t * restrict preemph_memD;
87 celt_word16_t *oldBandE;
89 celt_word16_t *psy_mem;
94 CELTEncoder *celt_encoder_create(const CELTMode *mode)
99 if (check_mode(mode) != CELT_OK)
103 C = mode->nbChannels;
104 st = celt_alloc(sizeof(CELTEncoder));
109 st->overlap = mode->overlap;
111 st->pitch_enabled = 1;
112 st->pitch_available = 1;
113 st->delayedIntra = 1;
115 st->in_mem = celt_alloc(st->overlap*C*sizeof(celt_sig_t));
116 st->out_mem = celt_alloc((MAX_PERIOD+st->overlap)*C*sizeof(celt_sig_t));
118 st->oldBandE = (celt_word16_t*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16_t));
120 st->preemph_memE = (celt_word16_t*)celt_alloc(C*sizeof(celt_word16_t));
121 st->preemph_memD = (celt_sig_t*)celt_alloc(C*sizeof(celt_sig_t));
124 st->psy_mem = celt_alloc(MAX_PERIOD*sizeof(celt_word16_t));
125 psydecay_init(&st->psy, MAX_PERIOD/2, st->mode->Fs);
131 void celt_encoder_destroy(CELTEncoder *st)
135 celt_warning("NULL passed to celt_encoder_destroy");
138 if (check_mode(st->mode) != CELT_OK)
141 celt_free(st->in_mem);
142 celt_free(st->out_mem);
144 celt_free(st->oldBandE);
146 celt_free(st->preemph_memE);
147 celt_free(st->preemph_memD);
150 celt_free (st->psy_mem);
151 psydecay_clear(&st->psy);
157 static inline celt_int16_t FLOAT2INT16(float x)
160 x = MAX32(x, -32768);
162 return (celt_int16_t)float2int(x);
165 static inline celt_word16_t SIG2WORD16(celt_sig_t x)
168 x = PSHR32(x, SIG_SHIFT);
169 x = MAX32(x, -32768);
173 return (celt_word16_t)x;
177 static int transient_analysis(celt_word32_t *in, int len, int C, int *transient_time, int *transient_shift)
181 /* FIXME: Remove the floats here */
182 VARDECL(celt_word32_t, begin);
184 ALLOC(begin, len, celt_word32_t);
186 begin[i] = ABS32(SHR32(in[C*i],SIG_SHIFT));
190 begin[i] = MAX32(begin[i], ABS32(SHR32(in[C*i+c],SIG_SHIFT)));
193 begin[i] = MAX32(begin[i-1],begin[i]);
195 for (i=8;i<len-8;i++)
197 if (begin[i] < MULT16_32_Q15(QCONST16(.2f,15),begin[len-1]))
205 ratio = DIV32(begin[len-1],1+begin[n-16]);
207 /*printf ("%d %f\n", n, ratio*ratio);*/
214 *transient_shift = 0;
215 else if (ratio < 256)
216 *transient_shift = 1;
217 else if (ratio < 4096)
218 *transient_shift = 2;
220 *transient_shift = 3;
227 /** Apply window and compute the MDCT for all sub-frames and all channels in a frame */
228 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig_t * restrict in, celt_sig_t * restrict out)
230 const int C = CHANNELS(mode);
231 if (C==1 && !shortBlocks)
233 const mdct_lookup *lookup = MDCT(mode);
234 const int overlap = OVERLAP(mode);
235 mdct_forward(lookup, in, out, mode->window, overlap);
236 } else if (!shortBlocks) {
237 const mdct_lookup *lookup = MDCT(mode);
238 const int overlap = OVERLAP(mode);
239 const int N = FRAMESIZE(mode);
241 VARDECL(celt_word32_t, x);
242 VARDECL(celt_word32_t, tmp);
244 ALLOC(x, N+overlap, celt_word32_t);
245 ALLOC(tmp, N, celt_word32_t);
249 for (j=0;j<N+overlap;j++)
251 mdct_forward(lookup, x, tmp, mode->window, overlap);
252 /* Interleaving the sub-frames */
258 const mdct_lookup *lookup = &mode->shortMdct;
259 const int overlap = mode->overlap;
260 const int N = mode->shortMdctSize;
262 VARDECL(celt_word32_t, x);
263 VARDECL(celt_word32_t, tmp);
265 ALLOC(x, N+overlap, celt_word32_t);
266 ALLOC(tmp, N, celt_word32_t);
269 int B = mode->nbShortMdcts;
273 for (j=0;j<N+overlap;j++)
274 x[j] = in[C*(b*N+j)+c];
275 mdct_forward(lookup, x, tmp, mode->window, overlap);
276 /* Interleaving the sub-frames */
278 out[C*(j*B+b)+c] = tmp[j];
285 /** Compute the IMDCT and apply window for all sub-frames and all channels in a frame */
286 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig_t *X, int transient_time, int transient_shift, celt_sig_t * restrict out_mem)
289 const int C = CHANNELS(mode);
290 const int N = FRAMESIZE(mode);
291 const int overlap = OVERLAP(mode);
296 if (transient_shift==0 && C==1 && !shortBlocks) {
297 const mdct_lookup *lookup = MDCT(mode);
298 mdct_backward(lookup, X, out_mem+C*(MAX_PERIOD-N-N4), mode->window, overlap);
299 } else if (!shortBlocks) {
300 const mdct_lookup *lookup = MDCT(mode);
301 VARDECL(celt_word32_t, x);
302 VARDECL(celt_word32_t, tmp);
304 ALLOC(x, 2*N, celt_word32_t);
305 ALLOC(tmp, N, celt_word32_t);
306 /* De-interleaving the sub-frames */
309 /* Prevents problems from the imdct doing the overlap-add */
310 CELT_MEMSET(x+N4, 0, N);
311 mdct_backward(lookup, tmp, x, mode->window, overlap);
312 celt_assert(transient_shift == 0);
313 /* The first and last part would need to be set to zero if we actually
314 wanted to use them. */
315 for (j=0;j<overlap;j++)
316 out_mem[C*(MAX_PERIOD-N)+C*j+c] += x[j+N4];
317 for (j=0;j<overlap;j++)
318 out_mem[C*(MAX_PERIOD)+C*(overlap-j-1)+c] = x[2*N-j-N4-1];
320 out_mem[C*(MAX_PERIOD-N)+C*(j+overlap)+c] = x[j+N4+overlap];
324 const int N2 = mode->shortMdctSize;
325 const int B = mode->nbShortMdcts;
326 const mdct_lookup *lookup = &mode->shortMdct;
327 VARDECL(celt_word32_t, x);
328 VARDECL(celt_word32_t, tmp);
330 ALLOC(x, 2*N, celt_word32_t);
331 ALLOC(tmp, N, celt_word32_t);
332 /* Prevents problems from the imdct doing the overlap-add */
333 CELT_MEMSET(x+N4, 0, N2);
336 /* De-interleaving the sub-frames */
338 tmp[j] = X[C*(j*B+b)+c];
339 mdct_backward(lookup, tmp, x+N4+N2*b, mode->window, overlap);
341 if (transient_shift > 0)
345 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));
346 for (j=transient_time;j<N+overlap;j++)
347 x[N4+j] = SHL32(x[N4+j], transient_shift);
350 x[N4+transient_time+j-16] *= 1+transientWindow[j]*((1<<transient_shift)-1);
351 for (j=transient_time;j<N+overlap;j++)
352 x[N4+j] *= 1<<transient_shift;
355 /* The first and last part would need to be set to zero if we actually
356 wanted to use them. */
357 for (j=0;j<overlap;j++)
358 out_mem[C*(MAX_PERIOD-N)+C*j+c] += x[j+N4];
359 for (j=0;j<overlap;j++)
360 out_mem[C*(MAX_PERIOD)+C*(overlap-j-1)+c] = x[2*N-j-N4-1];
362 out_mem[C*(MAX_PERIOD-N)+C*(j+overlap)+c] = x[j+N4+overlap];
369 #define FLAG_INTRA 1U<<16
370 #define FLAG_PITCH 1U<<15
371 #define FLAG_SHORT 1U<<14
372 #define FLAG_FOLD 1U<<13
373 #define FLAG_MASK (FLAG_INTRA|FLAG_PITCH|FLAG_SHORT|FLAG_FOLD)
375 celt_int32_t flaglist[8] = {
376 0 /*00 */ | FLAG_FOLD,
377 1 /*01 */ | FLAG_PITCH|FLAG_FOLD,
378 8 /*1000*/ | FLAG_NONE,
379 9 /*1001*/ | FLAG_SHORT|FLAG_FOLD,
380 10 /*1010*/ | FLAG_PITCH,
381 11 /*1011*/ | FLAG_INTRA,
382 6 /*110 */ | FLAG_INTRA|FLAG_FOLD,
383 7 /*111 */ | FLAG_INTRA|FLAG_SHORT|FLAG_FOLD
386 void encode_flags(ec_enc *enc, int intra_ener, int has_pitch, int shortBlocks, int has_fold)
391 flags |= intra_ener ? FLAG_INTRA : 0;
392 flags |= has_pitch ? FLAG_PITCH : 0;
393 flags |= shortBlocks ? FLAG_SHORT : 0;
394 flags |= has_fold ? FLAG_FOLD : 0;
396 if (flags == (flaglist[i]&FLAG_MASK))
399 flag_bits = flaglist[i]&0xf;
400 /*printf ("enc %d: %d %d %d %d\n", flag_bits, intra_ener, has_pitch, shortBlocks, has_fold);*/
402 ec_enc_bits(enc, flag_bits, 2);
404 ec_enc_bits(enc, flag_bits, 4);
406 ec_enc_bits(enc, flag_bits, 3);
409 void decode_flags(ec_dec *dec, int *intra_ener, int *has_pitch, int *shortBlocks, int *has_fold)
413 flag_bits = ec_dec_bits(dec, 2);
414 /*printf ("(%d) ", flag_bits);*/
416 flag_bits = (flag_bits<<2) | ec_dec_bits(dec, 2);
417 else if (flag_bits==3)
418 flag_bits = (flag_bits<<1) | ec_dec_bits(dec, 1);
420 if (flag_bits == (flaglist[i]&0xf))
423 *intra_ener = (flaglist[i]&FLAG_INTRA) != 0;
424 *has_pitch = (flaglist[i]&FLAG_PITCH) != 0;
425 *shortBlocks = (flaglist[i]&FLAG_SHORT) != 0;
426 *has_fold = (flaglist[i]&FLAG_FOLD ) != 0;
427 /*printf ("dec %d: %d %d %d %d\n", flag_bits, *intra_ener, *has_pitch, *shortBlocks, *has_fold);*/
431 int celt_encode(CELTEncoder * restrict st, const celt_int16_t * pcm, celt_int16_t * optional_synthesis, unsigned char *compressed, int nbCompressedBytes)
434 int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_sig_t * optional_synthesis, unsigned char *compressed, int nbCompressedBytes)
444 VARDECL(celt_sig_t, in);
445 VARDECL(celt_sig_t, freq);
446 VARDECL(celt_norm_t, X);
447 VARDECL(celt_norm_t, P);
448 VARDECL(celt_ener_t, bandE);
449 VARDECL(celt_pgain_t, gains);
450 VARDECL(int, stereo_mode);
451 VARDECL(int, fine_quant);
452 VARDECL(celt_word16_t, error);
453 VARDECL(int, pulses);
454 VARDECL(int, offsets);
456 VARDECL(celt_word32_t, mask);
457 VARDECL(celt_word32_t, tonality);
458 VARDECL(celt_word32_t, bandM);
459 VARDECL(celt_ener_t, bandN);
465 const int C = CHANNELS(st->mode);
468 if (check_mode(st->mode) != CELT_OK)
469 return CELT_INVALID_MODE;
471 if (nbCompressedBytes<0)
474 /* The memset is important for now in case the encoder doesn't fill up all the bytes */
475 CELT_MEMSET(compressed, 0, nbCompressedBytes);
476 ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes);
477 ec_enc_init(&enc,&buf);
480 N4 = (N-st->overlap)>>1;
481 ALLOC(in, 2*C*N-2*C*N4, celt_sig_t);
483 CELT_COPY(in, st->in_mem, C*st->overlap);
486 const celt_word16_t * restrict pcmp = pcm+c;
487 celt_sig_t * restrict inp = in+C*st->overlap+c;
490 /* Apply pre-emphasis */
491 celt_sig_t tmp = SCALEIN(SHL32(EXTEND32(*pcmp), SIG_SHIFT));
492 *inp = SUB32(tmp, SHR32(MULT16_16(preemph,st->preemph_memE[c]),3));
493 st->preemph_memE[c] = SCALEIN(*pcmp);
498 CELT_COPY(st->in_mem, in+C*(2*N-2*N4-st->overlap), C*st->overlap);
500 /* Transient handling */
501 if (st->mode->nbShortMdcts > 1)
503 if (transient_analysis(in, N+st->overlap, C, &transient_time, &transient_shift))
508 /* Apply the inverse shaping window */
514 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]);
516 for (i=transient_time;i<N+st->overlap;i++)
517 in[C*i+c] = SHR32(in[C*i+c], transient_shift);
521 in[C*(transient_time+i-16)+c] /= 1+transientWindow[i]*((1<<transient_shift)-1);
522 gain_1 = 1./(1<<transient_shift);
524 for (i=transient_time;i<N+st->overlap;i++)
540 ALLOC(freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */
541 ALLOC(bandE,st->mode->nbEBands*C, celt_ener_t);
543 compute_mdcts(st->mode, shortBlocks, in, freq);
544 compute_band_energies(st->mode, freq, bandE);
546 intra_ener = st->delayedIntra;
547 if (intra_decision(bandE, st->oldBandE, st->mode->nbEBands) || shortBlocks)
548 st->delayedIntra = 1;
550 st->delayedIntra = 0;
551 /* Pitch analysis: we do it early to save on the peak stack space */
552 /* Don't use pitch if there isn't enough data available yet, or if we're using shortBlocks */
553 has_pitch = st->pitch_enabled && (st->pitch_available >= MAX_PERIOD) && (!shortBlocks) && !intra_ener;
555 ALLOC(tonality, MAX_PERIOD/4, celt_word16_t);
557 VARDECL(celt_word16_t, X);
558 ALLOC(X, MAX_PERIOD/2, celt_word16_t);
559 find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, in, st->out_mem, st->mode->window, X, 2*N-2*N4, MAX_PERIOD-(2*N-2*N4), &pitch_index);
560 compute_tonality(st->mode, X, st->psy_mem, MAX_PERIOD, tonality, MAX_PERIOD/4);
565 find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, in, st->out_mem, st->mode->window, NULL, 2*N-2*N4, MAX_PERIOD-(2*N-2*N4), &pitch_index);
570 ALLOC(mask, N, celt_sig_t);
571 compute_mdct_masking(&st->psy, freq, tonality, st->psy_mem, mask, C*N);
572 /*for (i=0;i<256;i++)
573 printf ("%f %f %f ", freq[i], tonality[i], mask[i]);
577 /* Deferred allocation after find_spectral_pitch() to reduce the peak memory usage */
578 ALLOC(X, C*N, celt_norm_t); /**< Interleaved normalised MDCTs */
579 ALLOC(P, C*N, celt_norm_t); /**< Interleaved normalised pitch MDCTs*/
580 ALLOC(gains,st->mode->nbPBands, celt_pgain_t);
583 /* Band normalisation */
584 normalise_bands(st->mode, freq, X, bandE);
587 ALLOC(bandN,C*st->mode->nbEBands, celt_ener_t);
588 ALLOC(bandM,st->mode->nbEBands, celt_ener_t);
589 compute_noise_energies(st->mode, freq, tonality, bandN);
591 /*for (i=0;i<st->mode->nbEBands;i++)
592 printf ("%f ", (.1+bandN[i])/(.1+bandE[i]));
595 for (i=st->mode->nbPBands;i<st->mode->nbEBands;i++)
596 if (bandN[i] < .4*bandE[i])
598 /*printf ("%d\n", has_fold);*/
604 mask[i] = sqrt(mask[i]);
605 compute_band_energies(st->mode, mask, bandM);
606 /*for (i=0;i<st->mode->nbEBands;i++)
607 printf ("%f %f ", bandE[i], bandM[i]);
611 /* Compute MDCTs of the pitch part */
614 celt_word32_t curr_power, pitch_power=0;
615 /* Normalise the pitch vector as well (discard the energies) */
616 VARDECL(celt_ener_t, bandEp);
618 compute_mdcts(st->mode, 0, st->out_mem+pitch_index*C, freq);
619 ALLOC(bandEp, st->mode->nbEBands*st->mode->nbChannels, celt_ener_t);
620 compute_band_energies(st->mode, freq, bandEp);
621 normalise_bands(st->mode, freq, P, bandEp);
622 pitch_power = bandEp[0]+bandEp[1]+bandEp[2];
623 /* Check if we can safely use the pitch (i.e. effective gain isn't too high) */
624 curr_power = bandE[0]+bandE[1]+bandE[2];
625 if ((MULT16_32_Q15(QCONST16(.1f, 15),curr_power) + QCONST32(10.f,ENER_SHIFT) < pitch_power))
627 /* Pitch prediction */
628 has_pitch = compute_pitch_gain(st->mode, X, P, gains);
634 encode_flags(&enc, intra_ener, has_pitch, shortBlocks, has_fold);
637 ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4));
639 for (i=0;i<st->mode->nbPBands;i++)
646 ec_enc_bits(&enc, transient_shift, 2);
648 ec_enc_uint(&enc, transient_time, N+st->overlap);
652 static int fine_quant[30];
653 static int pulses[30];
657 for (i=0;i<st->mode->nbEBands;i++)
658 scanf("%d ", &fine_quant[i]);
659 for (i=0;i<st->mode->nbEBands;i++)
660 scanf("%d ", &pulses[i]);
664 ALLOC(fine_quant, st->mode->nbEBands, int);
665 ALLOC(pulses, st->mode->nbEBands, int);
669 ALLOC(error, C*st->mode->nbEBands, celt_word16_t);
670 quant_coarse_energy(st->mode, bandE, st->oldBandE, nbCompressedBytes*8/3, intra_ener, st->mode->prob, error, &enc);
672 ALLOC(offsets, st->mode->nbEBands, int);
673 ALLOC(stereo_mode, st->mode->nbEBands, int);
674 stereo_decision(st->mode, X, stereo_mode, st->mode->nbEBands);
676 for (i=0;i<st->mode->nbEBands;i++)
678 bits = nbCompressedBytes*8 - ec_enc_tell(&enc, 0) - 1;
680 bits -= st->mode->nbPBands;
682 compute_allocation(st->mode, offsets, stereo_mode, bits, pulses, fine_quant);
685 quant_fine_energy(st->mode, bandE, st->oldBandE, error, fine_quant, &enc);
687 /* Residual quantisation */
689 quant_bands(st->mode, X, P, NULL, has_pitch, gains, bandE, pulses, shortBlocks, has_fold, nbCompressedBytes*8, &enc);
691 quant_bands_stereo(st->mode, X, P, NULL, has_pitch, gains, bandE, pulses, shortBlocks, has_fold, nbCompressedBytes*8, &enc);
693 /* Re-synthesis of the coded audio if required */
694 if (st->pitch_available>0 || optional_synthesis!=NULL)
696 if (st->pitch_available>0 && st->pitch_available<MAX_PERIOD)
697 st->pitch_available+=st->frame_size;
700 denormalise_bands(st->mode, X, freq, bandE);
703 CELT_MOVE(st->out_mem, st->out_mem+C*N, C*(MAX_PERIOD+st->overlap-N));
705 compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, transient_shift, st->out_mem);
706 /* De-emphasis and put everything back at the right place in the synthesis history */
707 if (optional_synthesis != NULL) {
713 celt_sig_t tmp = MAC16_32_Q15(st->out_mem[C*(MAX_PERIOD-N)+C*j+c],
714 preemph,st->preemph_memD[c]);
715 st->preemph_memD[c] = tmp;
716 optional_synthesis[C*j+c] = SCALEOUT(SIG2WORD16(tmp));
722 /*fprintf (stderr, "remaining bits after encode = %d\n", nbCompressedBytes*8-ec_enc_tell(&enc, 0));*/
723 /*if (ec_enc_tell(&enc, 0) < nbCompressedBytes*8 - 7)
724 celt_warning_int ("many unused bits: ", nbCompressedBytes*8-ec_enc_tell(&enc, 0));*/
726 /* Finishing the stream with a 0101... pattern so that the decoder can check is everything's right */
729 while (ec_enc_tell(&enc, 0) < nbCompressedBytes*8)
731 ec_enc_uint(&enc, val, 2);
737 /*unsigned char *data;*/
738 int nbBytes = ec_byte_bytes(&buf);
739 if (nbBytes > nbCompressedBytes)
741 celt_warning_int ("got too many bytes:", nbBytes);
743 return CELT_INTERNAL_ERROR;
748 return nbCompressedBytes;
752 #ifndef DISABLE_FLOAT_API
753 int celt_encode_float(CELTEncoder * restrict st, const float * pcm, float * optional_synthesis, unsigned char *compressed, int nbCompressedBytes)
756 const int C = CHANNELS(st->mode);
757 const int N = st->block_size;
758 VARDECL(celt_int16_t, in);
760 ALLOC(in, C*N, celt_int16_t);
763 in[j] = FLOAT2INT16(pcm[j]);
765 if (optional_synthesis != NULL) {
766 ret=celt_encode(st,in,in,compressed,nbCompressedBytes);
768 optional_synthesis[j]=in[j]*(1/32768.);
770 ret=celt_encode(st,in,NULL,compressed,nbCompressedBytes);
776 #endif /*DISABLE_FLOAT_API*/
778 int celt_encode(CELTEncoder * restrict st, const celt_int16_t * pcm, celt_int16_t * optional_synthesis, unsigned char *compressed, int nbCompressedBytes)
781 VARDECL(celt_sig_t, in);
782 const int C = CHANNELS(st->mode);
783 const int N = st->block_size;
785 ALLOC(in, C*N, celt_sig_t);
786 for (j=0;j<C*N;j++) {
787 in[j] = SCALEOUT(pcm[j]);
790 if (optional_synthesis != NULL) {
791 ret = celt_encode_float(st,in,in,compressed,nbCompressedBytes);
793 optional_synthesis[j] = FLOAT2INT16(in[j]);
795 ret = celt_encode_float(st,in,NULL,compressed,nbCompressedBytes);
802 int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...)
805 va_start(ap, request);
808 case CELT_SET_COMPLEXITY_REQUEST:
810 int value = va_arg(ap, int);
811 if (value<0 || value>10)
814 st->pitch_enabled = 0;
815 st->pitch_available = 0;
817 st->pitch_enabled = 1;
818 if (st->pitch_available<1)
819 st->pitch_available = 1;
823 case CELT_SET_LTP_REQUEST:
825 int value = va_arg(ap, int);
826 if (value<0 || value>1 || (value==1 && st->pitch_available==0))
829 st->pitch_enabled = 0;
831 st->pitch_enabled = 1;
844 return CELT_UNIMPLEMENTED;
847 /****************************************************************************/
851 /****************************************************************************/
853 #define DECODE_BUFFER_SIZE 2048
855 #define DECODE_BUFFER_SIZE MAX_PERIOD
862 const CELTMode *mode;
870 celt_sig_t * restrict preemph_memD;
873 celt_sig_t *decode_mem;
875 celt_word16_t *oldBandE;
877 int last_pitch_index;
880 CELTDecoder *celt_decoder_create(const CELTMode *mode)
885 if (check_mode(mode) != CELT_OK)
890 st = celt_alloc(sizeof(CELTDecoder));
895 st->overlap = mode->overlap;
897 st->decode_mem = celt_alloc((DECODE_BUFFER_SIZE+st->overlap)*C*sizeof(celt_sig_t));
898 st->out_mem = st->decode_mem+DECODE_BUFFER_SIZE-MAX_PERIOD;
900 st->oldBandE = (celt_word16_t*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16_t));
902 st->preemph_memD = (celt_sig_t*)celt_alloc(C*sizeof(celt_sig_t));
904 st->last_pitch_index = 0;
908 void celt_decoder_destroy(CELTDecoder *st)
912 celt_warning("NULL passed to celt_encoder_destroy");
915 if (check_mode(st->mode) != CELT_OK)
919 celt_free(st->decode_mem);
921 celt_free(st->oldBandE);
923 celt_free(st->preemph_memD);
928 /** Handles lost packets by just copying past data with the same offset as the last
933 static void celt_decode_lost(CELTDecoder * restrict st, celt_word16_t * restrict pcm)
938 VARDECL(celt_sig_t, freq);
939 const int C = CHANNELS(st->mode);
943 ALLOC(freq,C*N, celt_sig_t); /**< Interleaved signal MDCTs */
945 len = N+st->mode->overlap;
947 pitch_index = st->last_pitch_index;
949 /* Use the pitch MDCT as the "guessed" signal */
950 compute_mdcts(st->mode, st->mode->window, st->out_mem+pitch_index*C, freq);
953 find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, st->out_mem+MAX_PERIOD-len, st->out_mem, st->mode->window, NULL, len, MAX_PERIOD-len-100, &pitch_index);
954 pitch_index = MAX_PERIOD-len-pitch_index;
955 offset = MAX_PERIOD-pitch_index;
956 while (offset+len >= MAX_PERIOD)
957 offset -= pitch_index;
958 compute_mdcts(st->mode, 0, st->out_mem+offset*C, freq);
960 freq[i] = ADD32(EPSILON, MULT16_32_Q15(QCONST16(.9f,15),freq[i]));
965 CELT_MOVE(st->out_mem, st->out_mem+C*N, C*(MAX_PERIOD+st->mode->overlap-N));
966 /* Compute inverse MDCTs */
967 compute_inv_mdcts(st->mode, 0, freq, -1, 0, st->out_mem);
974 celt_sig_t tmp = MAC16_32_Q15(st->out_mem[C*(MAX_PERIOD-N)+C*j+c],
975 preemph,st->preemph_memD[c]);
976 st->preemph_memD[c] = tmp;
977 pcm[C*j+c] = SCALEOUT(SIG2WORD16(tmp));
985 int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16_t * restrict pcm)
988 int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, celt_sig_t * restrict pcm)
992 int has_pitch, has_fold;
997 VARDECL(celt_sig_t, freq);
998 VARDECL(celt_norm_t, X);
999 VARDECL(celt_norm_t, P);
1000 VARDECL(celt_ener_t, bandE);
1001 VARDECL(celt_pgain_t, gains);
1002 VARDECL(int, stereo_mode);
1003 VARDECL(int, fine_quant);
1004 VARDECL(int, pulses);
1005 VARDECL(int, offsets);
1010 int transient_shift;
1011 const int C = CHANNELS(st->mode);
1014 if (check_mode(st->mode) != CELT_OK)
1015 return CELT_INVALID_MODE;
1018 N4 = (N-st->overlap)>>1;
1020 ALLOC(freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */
1021 ALLOC(X, C*N, celt_norm_t); /**< Interleaved normalised MDCTs */
1022 ALLOC(P, C*N, celt_norm_t); /**< Interleaved normalised pitch MDCTs*/
1023 ALLOC(bandE, st->mode->nbEBands*C, celt_ener_t);
1024 ALLOC(gains, st->mode->nbPBands, celt_pgain_t);
1026 if (check_mode(st->mode) != CELT_OK)
1029 return CELT_INVALID_MODE;
1033 celt_decode_lost(st, pcm);
1039 return CELT_BAD_ARG;
1042 ec_byte_readinit(&buf,(unsigned char*)data,len);
1043 ec_dec_init(&dec,&buf);
1045 decode_flags(&dec, &intra_ener, &has_pitch, &shortBlocks, &has_fold);
1048 transient_shift = ec_dec_bits(&dec, 2);
1049 if (transient_shift)
1050 transient_time = ec_dec_uint(&dec, N+st->mode->overlap);
1054 transient_time = -1;
1055 transient_shift = 0;
1060 pitch_index = ec_dec_uint(&dec, MAX_PERIOD-(2*N-2*N4));
1061 st->last_pitch_index = pitch_index;
1064 for (i=0;i<st->mode->nbPBands;i++)
1068 ALLOC(fine_quant, st->mode->nbEBands, int);
1069 /* Get band energies */
1070 unquant_coarse_energy(st->mode, bandE, st->oldBandE, len*8/3, intra_ener, st->mode->prob, &dec);
1072 ALLOC(pulses, st->mode->nbEBands, int);
1073 ALLOC(offsets, st->mode->nbEBands, int);
1074 ALLOC(stereo_mode, st->mode->nbEBands, int);
1075 stereo_decision(st->mode, X, stereo_mode, st->mode->nbEBands);
1077 for (i=0;i<st->mode->nbEBands;i++)
1080 bits = len*8 - ec_dec_tell(&dec, 0) - 1;
1082 bits -= st->mode->nbPBands;
1083 compute_allocation(st->mode, offsets, stereo_mode, bits, pulses, fine_quant);
1084 /*bits = ec_dec_tell(&dec, 0);
1085 compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(&dec, 0)-bits))/C);*/
1087 unquant_fine_energy(st->mode, bandE, st->oldBandE, fine_quant, &dec);
1092 VARDECL(celt_ener_t, bandEp);
1095 compute_mdcts(st->mode, 0, st->out_mem+pitch_index*C, freq);
1096 ALLOC(bandEp, st->mode->nbEBands*C, celt_ener_t);
1097 compute_band_energies(st->mode, freq, bandEp);
1098 normalise_bands(st->mode, freq, P, bandEp);
1099 /* Apply pitch gains */
1105 /* Decode fixed codebook and merge with pitch */
1107 unquant_bands(st->mode, X, P, has_pitch, gains, bandE, pulses, shortBlocks, has_fold, len*8, &dec);
1109 unquant_bands_stereo(st->mode, X, P, has_pitch, gains, bandE, pulses, shortBlocks, has_fold, len*8, &dec);
1112 denormalise_bands(st->mode, X, freq, bandE);
1115 CELT_MOVE(st->decode_mem, st->decode_mem+C*N, C*(DECODE_BUFFER_SIZE+st->overlap-N));
1116 /* Compute inverse MDCTs */
1117 compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, transient_shift, st->out_mem);
1124 celt_sig_t tmp = MAC16_32_Q15(st->out_mem[C*(MAX_PERIOD-N)+C*j+c],
1125 preemph,st->preemph_memD[c]);
1126 st->preemph_memD[c] = tmp;
1127 pcm[C*j+c] = SCALEOUT(SIG2WORD16(tmp));
1132 unsigned int val = 0;
1133 while (ec_dec_tell(&dec, 0) < len*8)
1135 if (ec_dec_uint(&dec, 2) != val)
1137 celt_warning("decode error");
1139 return CELT_CORRUPTED_DATA;
1151 #ifndef DISABLE_FLOAT_API
1152 int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm)
1155 const int C = CHANNELS(st->mode);
1156 const int N = st->block_size;
1157 VARDECL(celt_int16_t, out);
1159 ALLOC(out, C*N, celt_int16_t);
1161 ret=celt_decode(st, data, len, out);
1164 pcm[j]=out[j]*(1/32768.);
1168 #endif /*DISABLE_FLOAT_API*/
1170 int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16_t * restrict pcm)
1173 VARDECL(celt_sig_t, out);
1174 const int C = CHANNELS(st->mode);
1175 const int N = st->block_size;
1177 ALLOC(out, C*N, celt_sig_t);
1179 ret=celt_decode_float(st, data, len, out);
1182 pcm[j] = FLOAT2INT16 (out[j]);