From b8ba70c99be6e597bbfee3e18b949193f52ef075 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin <jean-marc.valin@usherbrooke.ca> Date: Sun, 18 Apr 2010 22:10:24 -0400 Subject: [PATCH] Disabling resynthesis when not needed (need to remove folding for this to work) --- libcelt/bands.c | 64 +++++++++++++++++++++++++++---------------------- libcelt/bands.h | 4 ++-- libcelt/celt.c | 11 +++++---- libcelt/vq.c | 11 +++++---- libcelt/vq.h | 2 +- 5 files changed, 53 insertions(+), 39 deletions(-) diff --git a/libcelt/bands.c b/libcelt/bands.c index 25ce79bf0..105fbada5 100644 --- a/libcelt/bands.c +++ b/libcelt/bands.c @@ -448,7 +448,7 @@ int folding_decision(const CELTMode *m, celt_norm *X, celt_word16 *average, int } /* Quantisation of the residual */ -void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int total_bits, int encode, void *enc_dec) +void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int resynth, int total_bits, int encode, void *enc_dec) { int i, j, remaining_bits, balance; const celt_int16 * restrict eBands = m->eBands; @@ -467,7 +467,6 @@ void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const cel int tell; int N; int q; - celt_word16 n; const celt_int16 * const *BPbits; int curr_balance, curr_bits; @@ -498,27 +497,32 @@ void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const cel } balance += pulses[i] + tell; - n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22)); if (q > 0) { int spread = fold ? B : 0; if (encode) - alg_quant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, enc_dec); + alg_quant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, resynth, enc_dec); else alg_unquant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, enc_dec); } else { - intra_fold(m, start, eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B); + if (resynth) + intra_fold(m, start, eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B); + } + if (resynth) + { + celt_word16 n; + n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22)); + for (j=eBands[i];j<eBands[i+1];j++) + norm[j] = MULT16_16_Q15(n,X[j]); } - for (j=eBands[i];j<eBands[i+1];j++) - norm[j] = MULT16_16_Q15(n,X[j]); } RESTORE_STACK; } #ifndef DISABLE_STEREO -void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_enc *enc) +void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int resynth, int total_bits, ec_enc *enc) { int i, j, remaining_bits, balance; const celt_int16 * restrict eBands = m->eBands; @@ -537,7 +541,6 @@ void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ { int tell; int q1, q2; - celt_word16 n; const celt_int16 * const *BPbits; int b, qb; int N; @@ -607,7 +610,6 @@ void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ iside = bitexact_cos(16384-itheta); delta = (N-1)*(log2_frac(iside,BITRES+2)-log2_frac(imid,BITRES+2))>>2; } - n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22)); #if 0 if (N==2) { @@ -710,13 +712,14 @@ void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ if (q1 > 0) { int spread = fold ? B : 0; - alg_quant(X, N, q1, spread, enc); + alg_quant(X, N, q1, spread, resynth, enc); } else { - intra_fold(m, start, eBands[i+1]-eBands[i], norm, X, eBands[i], B); + if (resynth) + intra_fold(m, start, eBands[i+1]-eBands[i], norm, X, eBands[i], B); } if (q2 > 0) { int spread = fold ? B : 0; - alg_quant(Y, N, q2, spread, enc); + alg_quant(Y, N, q2, spread, resynth, enc); } else for (j=0;j<N;j++) Y[j] = 0; @@ -724,24 +727,29 @@ void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ balance += pulses[i] + tell; + if (resynth) + { + celt_word16 n; #ifdef FIXED_POINT - mid = imid; - side = iside; + mid = imid; + side = iside; #else - mid = (1.f/32768)*imid; - side = (1.f/32768)*iside; + mid = (1.f/32768)*imid; + side = (1.f/32768)*iside; #endif - for (j=0;j<N;j++) - norm[eBands[i]+j] = MULT16_16_Q15(n,X[j]); - - for (j=0;j<N;j++) - X[j] = MULT16_16_Q15(X[j], mid); - for (j=0;j<N;j++) - Y[j] = MULT16_16_Q15(Y[j], side); - - stereo_band_mix(m, X, Y, bandE, 0, i, -1); - renormalise_vector(X, Q15ONE, N, 1); - renormalise_vector(Y, Q15ONE, N, 1); + n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22)); + for (j=0;j<N;j++) + norm[eBands[i]+j] = MULT16_16_Q15(n,X[j]); + + for (j=0;j<N;j++) + X[j] = MULT16_16_Q15(X[j], mid); + for (j=0;j<N;j++) + Y[j] = MULT16_16_Q15(Y[j], side); + + stereo_band_mix(m, X, Y, bandE, 0, i, -1); + renormalise_vector(X, Q15ONE, N, 1); + renormalise_vector(Y, Q15ONE, N, 1); + } } RESTORE_STACK; } diff --git a/libcelt/bands.h b/libcelt/bands.h index db629273a..82229d974 100644 --- a/libcelt/bands.h +++ b/libcelt/bands.h @@ -85,9 +85,9 @@ int folding_decision(const CELTMode *m, celt_norm *X, celt_word16 *average, int * @param total_bits Total number of bits that can be used for the frame (including the ones already spent) * @param enc Entropy encoder */ -void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int total_bits, int encode, void *enc_dec); +void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int resynth, int total_bits, int encode, void *enc_dec); -void quant_bands_stereo(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int total_bits, ec_enc *enc); +void quant_bands_stereo(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int resynth, int total_bits, ec_enc *enc); /** Decoding of the residual spectrum * @param m Mode data diff --git a/libcelt/celt.c b/libcelt/celt.c index 24ebcdda3..386f1060e 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -561,6 +561,7 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig * pcm, celt_sig int shortBlocks=0; int transient_time; int transient_shift; + int resynth; const int C = CHANNELS(st->channels); int mdct_weight_shift = 0; int mdct_weight_pos=0; @@ -610,6 +611,8 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig * pcm, celt_sig transient_shift = 0; shortBlocks = 0; + resynth = st->pitch_available>0 || optional_synthesis!=NULL; + if (st->mode->nbShortMdcts > 1 && transient_analysis(in, N+st->overlap, C, &transient_time, &transient_shift)) { #ifndef FIXED_POINT @@ -854,16 +857,16 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig * pcm, celt_sig /* Residual quantisation */ if (C==1) - quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, nbCompressedBytes*8, 1, &enc); + quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, resynth, nbCompressedBytes*8, 1, &enc); #ifndef DISABLE_STEREO else - quant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, nbCompressedBytes*8, &enc); + quant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, resynth, nbCompressedBytes*8, &enc); #endif quant_energy_finalise(st->mode, start, bandE, st->oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_enc_tell(&enc, 0), &enc, C); /* Re-synthesis of the coded audio if required */ - if (st->pitch_available>0 || optional_synthesis!=NULL) + if (resynth) { if (st->pitch_available>0 && st->pitch_available<MAX_PERIOD) st->pitch_available+=st->frame_size; @@ -1527,7 +1530,7 @@ int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int /* Decode fixed codebook and merge with pitch */ if (C==1) - quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, len*8, 0, &dec); + quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, 1, len*8, 0, &dec); #ifndef DISABLE_STEREO else unquant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, len*8, &dec); diff --git a/libcelt/vq.c b/libcelt/vq.c index baa652c46..725bafb27 100644 --- a/libcelt/vq.c +++ b/libcelt/vq.c @@ -257,7 +257,7 @@ static void normalise_residual(int * restrict iy, celt_norm * restrict X, int N, while (++i < N); } -void alg_quant(celt_norm *X, int N, int K, int spread, ec_enc *enc) +void alg_quant(celt_norm *X, int N, int K, int spread, int resynth, ec_enc *enc) { VARDECL(celt_norm, y); VARDECL(int, iy); @@ -414,9 +414,12 @@ void alg_quant(celt_norm *X, int N, int K, int spread, ec_enc *enc) /* Recompute the gain in one pass to reduce the encoder-decoder mismatch due to the recursive computation used in quantisation. */ - normalise_residual(iy, X, N, K, EXTRACT16(SHR32(yy,2*yshift))); - if (spread) - exp_rotation(X, N, -1, spread, K); + if (resynth) + { + normalise_residual(iy, X, N, K, EXTRACT16(SHR32(yy,2*yshift))); + if (spread) + exp_rotation(X, N, -1, spread, K); + } RESTORE_STACK; } diff --git a/libcelt/vq.h b/libcelt/vq.h index b2a3f5cd7..60f827ac6 100644 --- a/libcelt/vq.h +++ b/libcelt/vq.h @@ -51,7 +51,7 @@ * @param p Pitch vector (it is assumed that p+x is a unit vector) * @param enc Entropy encoder state */ -void alg_quant(celt_norm *X, int N, int K, int spread, ec_enc *enc); +void alg_quant(celt_norm *X, int N, int K, int spread, int resynth, ec_enc *enc); /** Algebraic pulse decoder * @param x Decoded normalised spectrum (returned) -- GitLab