diff --git a/libcelt/bands.c b/libcelt/bands.c index 91d3ec72..6e3e8f98 100644 --- a/libcelt/bands.c +++ b/libcelt/bands.c @@ -300,13 +300,16 @@ static void stereo_merge(celt_norm *X, celt_norm *Y, celt_word16 mid, int N) } /* Decide whether we should spread the pulses in the current frame */ -int spreading_decision(const CELTMode *m, celt_norm *X, int *average, int last_decision, int end, int _C, int M) +int spreading_decision(const CELTMode *m, celt_norm *X, int *average, + int last_decision, int *hf_average, int *tapset_decision, int update_hf, + int end, int _C, int M) { int i, c, N0; int sum = 0, nbBands=0; const int C = CHANNELS(_C); const celt_int16 * restrict eBands = m->eBands; int decision; + int hf_sum=0; N0 = M*m->shortMdctSize; @@ -335,11 +338,33 @@ int spreading_decision(const CELTMode *m, celt_norm *X, int *average, int last_d tcount[2]++; } + /* Only include four last bands (8 kHz and up) */ + if (i>m->nbEBands-4) + hf_sum += 32*(tcount[1]+tcount[0])/N; tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N); sum += tmp*256; nbBands++; } } while (++cnbEBands+end); + *hf_average = (*hf_average+hf_sum)>>1; + hf_sum = *hf_average; + if (*tapset_decision==2) + hf_sum += 4; + else if (*tapset_decision==0) + hf_sum -= 4; + if (hf_sum > 22) + *tapset_decision=2; + else if (hf_sum > 18) + *tapset_decision=1; + else + *tapset_decision=0; + } + /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/ sum /= nbBands; /* Recursive averaging */ sum = (sum+*average)>>1; diff --git a/libcelt/bands.h b/libcelt/bands.h index f861dcba..aa3138f7 100644 --- a/libcelt/bands.h +++ b/libcelt/bands.h @@ -69,7 +69,9 @@ void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig #define SPREAD_NORMAL (2) #define SPREAD_AGGRESSIVE (3) -int spreading_decision(const CELTMode *m, celt_norm *X, int *average, int last_decision, int end, int _C, int M); +int spreading_decision(const CELTMode *m, celt_norm *X, int *average, + int last_decision, int *hf_average, int *tapset_decision, int update_hf, + int end, int _C, int M); #ifdef MEASURE_NORM_MSE void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C); diff --git a/libcelt/celt.c b/libcelt/celt.c index 84e2d95f..bcec7d75 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -83,6 +83,8 @@ struct CELTEncoder { int delayedIntra; int tonal_average; int lastCodedBands; + int hf_average; + int tapset_decision; int prefilter_period; celt_word16 prefilter_gain; @@ -160,6 +162,8 @@ CELTEncoder *celt_encoder_init(CELTEncoder *st, const CELTMode *mode, int channe st->delayedIntra = 1; st->tonal_average = 256; st->spread_decision = SPREAD_NORMAL; + st->hf_average = 0; + st->tapset_decision = 0; st->complexity = 5; if (error) @@ -809,7 +813,8 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i celt_int32 total_bits; celt_int32 total_boost; celt_int32 tell; - int prefilter_tapset; + int prefilter_tapset=0; + int pf_on; SAVE_STACK; if (nbCompressedBytes<0 || pcm==NULL) @@ -927,7 +932,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i if (pitch_index > COMBFILTER_MAXPERIOD-2) pitch_index = COMBFILTER_MAXPERIOD-2; gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); - prefilter_tapset = 0; + prefilter_tapset = st->tapset_decision; } else { gain1 = 0; } @@ -954,6 +959,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i if(tell+15<=total_bits) ec_enc_bit_logp(enc, 0, 1); gain1 = 0; + pf_on = 0; } else { int qg; int octave; @@ -979,11 +985,13 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i ec_enc_bits(enc, prefilter_tapset!=0, 1); if (prefilter_tapset!=0) ec_enc_bits(enc, prefilter_tapset>1, 1); + pf_on = 1; } /*printf("%d %f\n", pitch_index, gain1);*/ #else /* ENABLE_POSTFILTER */ - if(tell+15<=total_bits) + if(tell+17<=total_bits) ec_enc_bit_logp(enc, 0, 1); + pf_on = 0; #endif /* ENABLE_POSTFILTER */ c=0; do { @@ -1068,7 +1076,8 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i st->spread_decision = SPREAD_NONE; } else { st->spread_decision = spreading_decision(st->mode, X, - &st->tonal_average, st->spread_decision, effEnd, C, M); + &st->tonal_average, st->spread_decision, &st->hf_average, + &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); } ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); } @@ -1934,7 +1943,7 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da postfilter_gain = 0; postfilter_pitch = 0; postfilter_tapset = 0; - if (tell+15 <= total_bits) + if (tell+17 <= total_bits) { if(ec_dec_bit_logp(dec, 1)) {