diff --git a/libcelt/celt.c b/libcelt/celt.c index 382aa90ba9b21b5de9c5ec612db2e8126f47d492..34d4f3886ae85b80ba0241e2248b4fb29bec20a4 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -372,6 +372,7 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si #endif int i, c, N, N4; int has_pitch; + int id; int pitch_index; int bits; int has_fold=1; @@ -554,27 +555,24 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si /* Check if we can safely use the pitch (i.e. effective gain isn't too high) */ curr_power = bandE[0]+bandE[1]+bandE[2]; + has_pitch = 0; if (st->pitch_enabled && !shortBlocks && (MULT16_32_Q15(QCONST16(.1f, 15),curr_power) + QCONST32(10.f,ENER_SHIFT) < pitch_power)) { - int id; - /* Pitch prediction */ compute_pitch_gain(st->mode, X, P, gains); - id = quant_pitch(gains, st->mode->nbPBands, &enc); - if (id != -1) + id = quant_pitch(gains, st->mode->nbPBands); + if (id > -1) has_pitch = 1; - else - has_pitch = 0; + } + + if (has_pitch) + { + unquant_pitch(id, gains, st->mode->nbPBands); ec_enc_bits(&enc, has_pitch, 1); /* Pitch flag */ - if (has_pitch) - { - ec_enc_bits(&enc, has_fold, 1); /* Folding flag */ - ec_enc_bits(&enc, id, 7); - ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4)); - } else if (st->mode->nbShortMdcts > 1) { - ec_enc_bits(&enc, 0, 1); /* Transient off */ - has_fold = 1; - } + ec_enc_bits(&enc, has_fold, 1); /* Folding flag */ + ec_enc_bits(&enc, id, 7); + ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4)); + pitch_quant_bands(st->mode, P, gains); } else { if (!shortBlocks) { @@ -624,8 +622,6 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si quant_fine_energy(st->mode, bandE, st->oldBandE, error, fine_quant, &enc); - pitch_quant_bands(st->mode, P, gains); - /* Residual quantisation */ quant_bands(st->mode, X, P, NULL, bandE, stereo_mode, pulses, shortBlocks, has_fold, nbCompressedBytes*8, &enc); @@ -972,16 +968,16 @@ int celt_decode_float(CELTDecoder * restrict st, unsigned char *data, int len, c transient_time = -1; transient_shift = 0; } - /* Get the pitch gains */ - /* Get the pitch index */ if (has_pitch) { - has_pitch = unquant_pitch(gains, st->mode->nbPBands, &dec); + int id; + /* Get the pitch gains and index */ + id = ec_dec_bits(&dec, 7); + unquant_pitch(id, gains, st->mode->nbPBands); pitch_index = ec_dec_uint(&dec, MAX_PERIOD-(2*N-2*N4)); st->last_pitch_index = pitch_index; } else { - /* FIXME: We could be more intelligent here and just not compute the MDCT */ pitch_index = 0; for (i=0;i<st->mode->nbPBands;i++) gains[i] = 0; @@ -1016,14 +1012,13 @@ int celt_decode_float(CELTDecoder * restrict st, unsigned char *data, int len, c ALLOC(bandEp, st->mode->nbEBands*C, celt_ener_t); compute_band_energies(st->mode, freq, bandEp); normalise_bands(st->mode, freq, P, bandEp); + /* Apply pitch gains */ + pitch_quant_bands(st->mode, P, gains); } else { for (i=0;i<C*N;i++) P[i] = 0; } - /* Apply pitch gains */ - pitch_quant_bands(st->mode, P, gains); - /* Decode fixed codebook and merge with pitch */ unquant_bands(st->mode, X, P, bandE, stereo_mode, pulses, shortBlocks, has_fold, len*8, &dec); diff --git a/libcelt/quant_pitch.c b/libcelt/quant_pitch.c index 52e7901019e5feb10c1d079d63af1e539d7195b2..82c7d3bba067eb2074fd3c5d3b37c711011e0c32 100644 --- a/libcelt/quant_pitch.c +++ b/libcelt/quant_pitch.c @@ -84,15 +84,7 @@ int vq_index(const celt_pgain_t *in, const celt_uint16_t *codebook, int len, int return best_index; } -/** Returns the pitch gain vector corresponding to a certain id */ -static void id2gains(int id, celt_pgain_t *gains, int len) -{ - int i; - for (i=0;i<len;i++) - gains[i] = celt_sqrt(Q1515ONE-MULT16_16(Q15ONE-PGAIN(pgain_table,id*len+i),Q15ONE-PGAIN(pgain_table,id*len+i))); -} - -int quant_pitch(celt_pgain_t *gains, int len, ec_enc *enc) +int quant_pitch(celt_pgain_t *gains, int len) { int i, id; celt_word32_t gain_sum = 0; @@ -107,21 +99,19 @@ int quant_pitch(celt_pgain_t *gains, int len, ec_enc *enc) if (gain_sum > QCONST32(.3f,15)) { id = vq_index(gains, pgain_table, len, 128); + /* FIXME: Remove when we're not waisting a transmitted index on 0 gains */ + if (id==0) + id = -1; } else { - id = 0; + id = -1; } - /*for (i=0;i<len;i++) printf ("%f ", pgain_table[id*len+i]);printf ("\n");*/ - id2gains(id, gains, len); - if (id != 0) - return id; - else - return -1; + return id; } -int unquant_pitch(celt_pgain_t *gains, int len, ec_dec *dec) +/** Returns the pitch gain vector corresponding to a certain id */ +void unquant_pitch(int id, celt_pgain_t *gains, int len) { - int id; - id = ec_dec_bits(dec, 7); - id2gains(id, gains, len); - return id!=0; + int i; + for (i=0;i<len;i++) + gains[i] = celt_sqrt(Q1515ONE-MULT16_16(Q15ONE-PGAIN(pgain_table,id*len+i),Q15ONE-PGAIN(pgain_table,id*len+i))); } diff --git a/libcelt/quant_pitch.h b/libcelt/quant_pitch.h index 6ee4ecfd34a0f903bb7d06a464cd1d1b3567f669..194f769cbc90de438f7efabf30bac92ed55a1da2 100644 --- a/libcelt/quant_pitch.h +++ b/libcelt/quant_pitch.h @@ -36,10 +36,9 @@ #include "entenc.h" #include "entdec.h" -/** If this returns 0, the gain is zero (don't encode the pitch index) */ -int quant_pitch(celt_pgain_t *gains, int len, ec_enc *enc); +/** If this returns -1, the gain is zero (don't encode the pitch index) */ +int quant_pitch(celt_pgain_t *gains, int len); -/** If this returns 0, the gain is zero (don't decode the pitch index) */ -int unquant_pitch(celt_pgain_t *gains, int len, ec_dec *dec); +void unquant_pitch(int id, celt_pgain_t *gains, int len); #endif /* QUANT_PITCH_H */