1 /* (C) 2007 Jean-Marc Valin, CSIRO
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.
36 #include "os_support.h"
41 #include "kiss_fftr.h"
45 #include "quant_pitch.h"
46 #include "quant_bands.h"
50 #define MAX_PERIOD 1024
53 #define M_PI 3.14159263
60 const CELTMode *mode; /**< Mode used by the encoder */
74 mdct_lookup mdct_lookup;
80 celt_sig_t *mdct_overlap;
88 CELTEncoder *celt_encoder_create(const CELTMode *mode)
93 if (check_mode(mode) != CELT_OK)
97 B = mode->nbMdctBlocks;
99 st = celt_alloc(sizeof(CELTEncoder));
102 st->frame_size = B*N;
105 st->overlap = mode->overlap;
107 N4 = (N-st->overlap)/2;
108 ec_byte_writeinit(&st->buf);
109 ec_enc_init(&st->enc,&st->buf);
111 mdct_init(&st->mdct_lookup, 2*N);
112 st->fft = kiss_fftr_alloc(MAX_PERIOD*C, 0, 0);
113 psydecay_init(&st->psy, MAX_PERIOD*C/2, st->mode->Fs);
115 st->window = celt_alloc(2*N*sizeof(float));
116 st->in_mem = celt_alloc(N*C*sizeof(celt_sig_t));
117 st->mdct_overlap = celt_alloc(N*C*sizeof(celt_sig_t));
118 st->out_mem = celt_alloc(MAX_PERIOD*C*sizeof(celt_sig_t));
121 for (i=0;i<st->overlap;i++)
122 st->window[N4+i] = st->window[2*N-N4-i-1]
123 = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/st->overlap) * sin(.5*M_PI*(i+.5)/st->overlap));
125 st->window[N-N4+i] = 1;
126 st->oldBandE = celt_alloc(C*mode->nbEBands*sizeof(float));
129 st->preemph_memE = celt_alloc(C*sizeof(float));;
130 st->preemph_memD = celt_alloc(C*sizeof(float));;
135 void celt_encoder_destroy(CELTEncoder *st)
139 celt_warning("NULL passed to celt_encoder_destroy");
142 if (check_mode(st->mode) != CELT_OK)
145 ec_byte_writeclear(&st->buf);
147 mdct_clear(&st->mdct_lookup);
148 kiss_fft_free(st->fft);
149 psydecay_clear(&st->psy);
151 celt_free(st->window);
152 celt_free(st->in_mem);
153 celt_free(st->mdct_overlap);
154 celt_free(st->out_mem);
156 celt_free(st->oldBandE);
158 celt_free(st->preemph_memE);
159 celt_free(st->preemph_memD);
164 /** Apply window and compute the MDCT for all sub-frames and all channels in a frame */
165 static float compute_mdcts(mdct_lookup *mdct_lookup, float *window, celt_sig_t *in, celt_sig_t *out, int N, int B, int C)
169 VARDECL(celt_word32_t *x);
170 VARDECL(celt_word32_t *tmp);
171 ALLOC(x, 2*N, celt_word32_t);
172 ALLOC(tmp, N, celt_word32_t);
180 x[j] = window[j]*in[C*i*N+C*j+c];
181 E += SIG_SCALING_1*SIG_SCALING_1*x[j]*x[j];
183 mdct_forward(mdct_lookup, x, tmp);
184 /* Interleaving the sub-frames */
186 out[C*B*j+C*i+c] = tmp[j];
192 /** Compute the IMDCT and apply window for all sub-frames and all channels in a frame */
193 static void compute_inv_mdcts(mdct_lookup *mdct_lookup, float *window, celt_sig_t *X, celt_sig_t *out_mem, celt_sig_t *mdct_overlap, int N, int overlap, int B, int C)
196 VARDECL(celt_word32_t *x);
197 VARDECL(celt_word32_t *tmp);
198 ALLOC(x, 2*N, celt_word32_t);
199 ALLOC(tmp, N, celt_word32_t);
206 /* De-interleaving the sub-frames */
208 tmp[j] = X[C*B*j+C*i+c];
209 mdct_backward(mdct_lookup, tmp, x);
211 x[j] = window[j]*x[j];
212 for (j=0;j<overlap;j++)
213 out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] = 2*(x[N4+j]+mdct_overlap[C*j+c]);
215 out_mem[C*(MAX_PERIOD+(i-B)*N)+C*(j+overlap)+c] = 2*x[j+N4+overlap];
216 for (j=0;j<overlap;j++)
217 mdct_overlap[C*j+c] = x[N+N4+j];
222 int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, int nbCompressedBytes)
224 int i, c, N, B, C, N4;
227 float curr_power, pitch_power;
228 VARDECL(celt_sig_t *in);
229 VARDECL(celt_sig_t *freq);
230 VARDECL(celt_norm_t *X);
231 VARDECL(celt_norm_t *P);
232 VARDECL(float *mask);
233 VARDECL(celt_ener_t *bandE);
234 VARDECL(celt_pgain_t *gains);
236 if (check_mode(st->mode) != CELT_OK)
237 return CELT_INVALID_MODE;
241 C = st->mode->nbChannels;
242 ALLOC(in, (B+1)*C*N, celt_sig_t);
243 ALLOC(freq, B*C*N, celt_sig_t); /**< Interleaved signal MDCTs */
244 ALLOC(X, B*C*N, celt_norm_t); /**< Interleaved normalised MDCTs */
245 ALLOC(P, B*C*N, celt_norm_t); /**< Interleaved normalised pitch MDCTs*/
246 ALLOC(mask, B*C*N, float); /**< Masking curve */
247 ALLOC(bandE,st->mode->nbEBands*C, celt_ener_t);
248 ALLOC(gains,st->mode->nbPBands, celt_pgain_t);
250 N4 = (N-st->overlap)/2;
256 for (i=0;i<st->overlap;i++)
257 in[C*(i+N4)+c] = st->in_mem[C*i+c];
260 float tmp = SIG_SCALING*pcm[C*i+c];
261 in[C*(i+st->overlap+N4)+c] = tmp - st->preemph*st->preemph_memE[c];
262 st->preemph_memE[c] = tmp;
264 for (i=N*(B+1)-N4;i<N*(B+1);i++)
266 for (i=0;i<st->overlap;i++)
267 st->in_mem[C*i+c] = in[C*(N*(B+1)-N4-st->overlap+i)+c];
269 /*for (i=0;i<(B+1)*C*N;i++) printf ("%f(%d) ", in[i], i); printf ("\n");*/
271 curr_power = compute_mdcts(&st->mdct_lookup, st->window, in, freq, N, B, C);
273 #if 0 /* Mask disabled until it can be made to do something useful */
274 compute_mdct_masking(X, mask, B*C*N, st->Fs);
276 /* Invert and stretch the mask to length of X
277 For some reason, I get better results by using the sqrt instead,
278 although there's no valid reason to. Must investigate further */
279 for (i=0;i<B*C*N;i++)
280 mask[i] = 1/(.1+mask[i]);
282 for (i=0;i<B*C*N;i++)
290 in[C*i+c] *= st->window[i];
291 in[C*(B*N+i)+c] *= st->window[N+i];
294 find_spectral_pitch(st->fft, &st->psy, in, st->out_mem, MAX_PERIOD, (B+1)*N, C, &pitch_index);
296 /*printf ("%f %f\n", curr_power, pitch_power);*/
299 printf ("%f ", X[j]);
301 printf ("%f ", P[j]);
304 /* Band normalisation */
305 compute_band_energies(st->mode, freq, bandE);
306 normalise_bands(st->mode, freq, X, bandE);
307 /*for (i=0;i<st->mode->nbEBands;i++)printf("%f ", bandE[i]);printf("\n");*/
308 /*for (i=0;i<N*B*C;i++)printf("%f ", X[i]);printf("\n");*/
310 /* Compute MDCTs of the pitch part */
311 pitch_power = compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, freq, N, B, C);
314 quant_energy(st->mode, bandE, st->oldBandE, nbCompressedBytes*8/3, &st->enc);
318 stereo_mix(st->mode, X, bandE, 1);
321 /* Check if we can safely use the pitch (i.e. effective gain isn't too high) */
322 if (curr_power + 1e5f < 10.f*pitch_power)
324 /* Normalise the pitch vector as well (discard the energies) */
325 VARDECL(celt_ener_t *bandEp);
326 ALLOC(bandEp, st->mode->nbEBands*st->mode->nbChannels, celt_ener_t);
327 compute_band_energies(st->mode, freq, bandEp);
328 normalise_bands(st->mode, freq, P, bandEp);
331 stereo_mix(st->mode, P, bandE, 1);
332 /* Simulates intensity stereo */
333 /*for (i=30;i<N*B;i++)
334 X[i*C+1] = P[i*C+1] = 0;*/
336 /* Pitch prediction */
337 compute_pitch_gain(st->mode, X, P, gains, bandE);
338 has_pitch = quant_pitch(gains, st->mode->nbPBands, &st->enc);
340 ec_enc_uint(&st->enc, pitch_index, MAX_PERIOD-(B+1)*N);
342 /* No pitch, so we just pretend we found a gain of zero */
343 for (i=0;i<st->mode->nbPBands;i++)
345 ec_enc_uint(&st->enc, 0, 128);
346 for (i=0;i<B*C*N;i++)
351 pitch_quant_bands(st->mode, X, P, gains);
353 /*for (i=0;i<B*N;i++) printf("%f ",P[i]);printf("\n");*/
354 /* Compute residual that we're going to encode */
355 for (i=0;i<B*C*N;i++)
361 printf ("%f\n", sum);*/
362 /* Residual quantisation */
363 quant_bands(st->mode, X, P, mask, nbCompressedBytes*8, &st->enc);
367 stereo_mix(st->mode, X, bandE, -1);
368 renormalise_bands(st->mode, X);
371 denormalise_bands(st->mode, X, freq, bandE);
374 CELT_MOVE(st->out_mem, st->out_mem+C*B*N, C*(MAX_PERIOD-B*N));
376 compute_inv_mdcts(&st->mdct_lookup, st->window, freq, st->out_mem, st->mdct_overlap, N, st->overlap, B, C);
377 /* De-emphasis and put everything back at the right place in the synthesis history */
385 float tmp = st->out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] + st->preemph*st->preemph_memD[c];
386 st->preemph_memD[c] = tmp;
387 tmp *= SIG_SCALING_1;
388 if (tmp > 32767) tmp = 32767;
389 if (tmp < -32767) tmp = -32767;
390 pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
395 if (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8 - 7)
396 celt_warning_int ("many unused bits: ", nbCompressedBytes*8-ec_enc_tell(&st->enc, 0));
397 /*printf ("%d\n", ec_enc_tell(&st->enc, 0)-8*nbCompressedBytes);*/
398 /* Finishing the stream with a 0101... pattern so that the decoder can check is everything's right */
401 while (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8)
403 ec_enc_uint(&st->enc, val, 2);
407 ec_enc_done(&st->enc);
410 int nbBytes = ec_byte_bytes(&st->buf);
411 if (nbBytes > nbCompressedBytes)
413 celt_warning_int ("got too many bytes:", nbBytes);
414 return CELT_INTERNAL_ERROR;
416 /*printf ("%d\n", *nbBytes);*/
417 data = ec_byte_get_buffer(&st->buf);
418 for (i=0;i<nbBytes;i++)
419 compressed[i] = data[i];
420 for (;i<nbCompressedBytes;i++)
423 /* Reset the packing for the next encoding */
424 ec_byte_reset(&st->buf);
425 ec_enc_init(&st->enc,&st->buf);
427 return nbCompressedBytes;
431 /****************************************************************************/
435 /****************************************************************************/
442 const CELTMode *mode;
454 mdct_lookup mdct_lookup;
457 celt_sig_t *mdct_overlap;
462 int last_pitch_index;
465 CELTDecoder *celt_decoder_create(const CELTMode *mode)
470 if (check_mode(mode) != CELT_OK)
474 B = mode->nbMdctBlocks;
475 C = mode->nbChannels;
476 st = celt_alloc(sizeof(CELTDecoder));
479 st->frame_size = B*N;
482 st->overlap = mode->overlap;
484 N4 = (N-st->overlap)/2;
486 mdct_init(&st->mdct_lookup, 2*N);
488 st->window = celt_alloc(2*N*sizeof(float));
489 st->mdct_overlap = celt_alloc(N*C*sizeof(celt_sig_t));
490 st->out_mem = celt_alloc(MAX_PERIOD*C*sizeof(celt_sig_t));
494 for (i=0;i<st->overlap;i++)
495 st->window[N4+i] = st->window[2*N-N4-i-1]
496 = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/st->overlap) * sin(.5*M_PI*(i+.5)/st->overlap));
498 st->window[N-N4+i] = 1;
500 st->oldBandE = celt_alloc(C*mode->nbEBands*sizeof(float));
503 st->preemph_memD = celt_alloc(C*sizeof(float));;
505 st->last_pitch_index = 0;
509 void celt_decoder_destroy(CELTDecoder *st)
513 celt_warning("NULL passed to celt_encoder_destroy");
516 if (check_mode(st->mode) != CELT_OK)
519 mdct_clear(&st->mdct_lookup);
521 celt_free(st->window);
522 celt_free(st->mdct_overlap);
523 celt_free(st->out_mem);
525 celt_free(st->oldBandE);
527 celt_free(st->preemph_memD);
532 /** Handles lost packets by just copying past data with the same offset as the last
534 static void celt_decode_lost(CELTDecoder *st, short *pcm)
538 VARDECL(celt_sig_t *freq);
541 C = st->mode->nbChannels;
542 ALLOC(freq,C*B*N, celt_sig_t); /**< Interleaved signal MDCTs */
544 pitch_index = st->last_pitch_index;
546 /* Use the pitch MDCT as the "guessed" signal */
547 compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, freq, N, B, C);
549 CELT_MOVE(st->out_mem, st->out_mem+C*B*N, C*(MAX_PERIOD-B*N));
550 /* Compute inverse MDCTs */
551 compute_inv_mdcts(&st->mdct_lookup, st->window, freq, st->out_mem, st->mdct_overlap, N, st->overlap, B, C);
560 float tmp = st->out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] + st->preemph*st->preemph_memD[c];
561 st->preemph_memD[c] = tmp;
562 tmp *= SIG_SCALING_1;
563 if (tmp > 32767) tmp = 32767;
564 if (tmp < -32767) tmp = -32767;
565 pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
571 int celt_decode(CELTDecoder *st, unsigned char *data, int len, celt_int16_t *pcm)
578 VARDECL(celt_sig_t *freq);
579 VARDECL(celt_norm_t *X);
580 VARDECL(celt_norm_t *P);
581 VARDECL(celt_ener_t *bandE);
582 VARDECL(celt_pgain_t *gains);
584 if (check_mode(st->mode) != CELT_OK)
585 return CELT_INVALID_MODE;
589 C = st->mode->nbChannels;
591 ALLOC(freq, C*B*N, celt_sig_t); /**< Interleaved signal MDCTs */
592 ALLOC(X, C*B*N, celt_norm_t); /**< Interleaved normalised MDCTs */
593 ALLOC(P, C*B*N, celt_norm_t); /**< Interleaved normalised pitch MDCTs*/
594 ALLOC(bandE, st->mode->nbEBands*C, celt_ener_t);
595 ALLOC(gains, st->mode->nbPBands, celt_pgain_t);
597 if (check_mode(st->mode) != CELT_OK)
598 return CELT_INVALID_MODE;
601 celt_decode_lost(st, pcm);
605 ec_byte_readinit(&buf,data,len);
606 ec_dec_init(&dec,&buf);
608 /* Get band energies */
609 unquant_energy(st->mode, bandE, st->oldBandE, len*8/3, &dec);
611 /* Get the pitch gains */
612 has_pitch = unquant_pitch(gains, st->mode->nbPBands, &dec);
614 /* Get the pitch index */
617 pitch_index = ec_dec_uint(&dec, MAX_PERIOD-(B+1)*N);
618 st->last_pitch_index = pitch_index;
620 /* FIXME: We could be more intelligent here and just not compute the MDCT */
625 compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, freq, N, B, C);
628 VARDECL(celt_ener_t *bandEp);
629 ALLOC(bandEp, st->mode->nbEBands*C, celt_ener_t);
630 compute_band_energies(st->mode, freq, bandEp);
631 normalise_bands(st->mode, freq, P, bandEp);
635 stereo_mix(st->mode, P, bandE, 1);
637 /* Apply pitch gains */
638 pitch_quant_bands(st->mode, X, P, gains);
640 /* Decode fixed codebook and merge with pitch */
641 unquant_bands(st->mode, X, P, len*8, &dec);
645 stereo_mix(st->mode, X, bandE, -1);
646 renormalise_bands(st->mode, X);
649 denormalise_bands(st->mode, X, freq, bandE);
652 CELT_MOVE(st->out_mem, st->out_mem+C*B*N, C*(MAX_PERIOD-B*N));
653 /* Compute inverse MDCTs */
654 compute_inv_mdcts(&st->mdct_lookup, st->window, freq, st->out_mem, st->mdct_overlap, N, st->overlap, B, C);
663 float tmp = st->out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] + st->preemph*st->preemph_memD[c];
664 st->preemph_memD[c] = tmp;
665 tmp *= SIG_SCALING_1;
666 if (tmp > 32767) tmp = 32767;
667 if (tmp < -32767) tmp = -32767;
668 pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
675 while (ec_dec_tell(&dec, 0) < len*8)
677 if (ec_dec_uint(&dec, 2) != val)
679 celt_warning("decode error");
680 return CELT_CORRUPTED_DATA;