From abd512f7f645502afb54401473eb334a2955b80c Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Wed, 26 Jun 2024 13:37:24 -0400 Subject: [PATCH] Defining MAXG/MING/GCONST for log gain --- celt/arch.h | 3 + celt/bands.c | 8 +- celt/celt_decoder.c | 37 ++++---- celt/celt_encoder.c | 156 ++++++++++++++++----------------- celt/fixed_debug.h | 2 + celt/fixed_generic.h | 6 ++ celt/quant_bands.c | 34 +++---- src/opus_encoder.c | 12 +-- src/opus_multistream_encoder.c | 30 +++---- 9 files changed, 150 insertions(+), 138 deletions(-) diff --git a/celt/arch.h b/celt/arch.h index 2bd87edc..0b08733b 100644 --- a/celt/arch.h +++ b/celt/arch.h @@ -105,6 +105,8 @@ void celt_fatal(const char *str, const char *file, int line) #define IMAX(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum int value. */ #define UADD32(a,b) ((a)+(b)) #define USUB32(a,b) ((a)-(b)) +#define MAXG(a,b) MAX16(a, b) +#define MING(a,b) MIN16(a, b) /* Throughout the code, we use the following scaling for signals: FLOAT: used for float API, normalized to +/-1. @@ -270,6 +272,7 @@ static OPUS_INLINE int celt_isnan(float x) #define QCONST16(x,bits) (x) #define QCONST32(x,bits) (x) +#define GCONST(x) (x) #define NEG16(x) (-(x)) #define NEG32(x) (-(x)) diff --git a/celt/bands.c b/celt/bands.c index c334abbc..625b64c6 100644 --- a/celt/bands.c +++ b/celt/bands.c @@ -325,14 +325,14 @@ void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_mas prev2 = prev2logE[c*m->nbEBands+i]; if (!encode && C==1) { - prev1 = MAX16(prev1,prev1logE[m->nbEBands+i]); - prev2 = MAX16(prev2,prev2logE[m->nbEBands+i]); + prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]); + prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]); } - Ediff = EXTEND32(logE[c*m->nbEBands+i])-EXTEND32(MIN16(prev1,prev2)); + Ediff = EXTEND32(logE[c*m->nbEBands+i])-EXTEND32(MING(prev1,prev2)); Ediff = MAX32(0, Ediff); #ifdef FIXED_POINT - if (Ediff < 16384) + if (Ediff < GCONST(16.f)) { opus_val32 r32 = SHR32(celt_exp2(-EXTRACT16(Ediff)),1); r = 2*MIN16(16383,r32); diff --git a/celt/celt_decoder.c b/celt/celt_decoder.c index 26b3bac9..546e5db4 100644 --- a/celt/celt_decoder.c +++ b/celt/celt_decoder.c @@ -659,11 +659,11 @@ static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, int N, int LM } /* Energy decay */ - decay = loss_duration==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT); + decay = loss_duration==0 ? GCONST(1.5f) : GCONST(.5f); c=0; do { for (i=start;irng; for (c=0;closs_duration>>LM); - if (LM==0) safety = QCONST16(1.5f,DB_SHIFT); - else if (LM==1) safety = QCONST16(.5f,DB_SHIFT); + if (LM==0) safety = GCONST(1.5f); + else if (LM==1) safety = GCONST(.5f); for (i=start;iprefilter_and_fold) { prefilter_and_fold(st, N); @@ -1325,26 +1325,26 @@ int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char OPUS_COPY(oldLogE, oldBandE, 2*nbEBands); } else { for (i=0;i<2*nbEBands;i++) - oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); + oldLogE[i] = MING(oldLogE[i], oldBandE[i]); } /* In normal circumstances, we only allow the noise floor to increase by up to 2.4 dB/second, but when we're in DTX we give the weight of all missing packets to the update packet. */ - max_background_increase = IMIN(160, st->loss_duration+M)*QCONST16(0.001f,DB_SHIFT); + max_background_increase = IMIN(160, st->loss_duration+M)*GCONST(0.001f); for (i=0;i<2*nbEBands;i++) - backgroundLogE[i] = MIN16(backgroundLogE[i] + max_background_increase, oldBandE[i]); + backgroundLogE[i] = MING(backgroundLogE[i] + max_background_increase, oldBandE[i]); /* In case start or end were to change */ c=0; do { for (i=0;irng = dec->rng; @@ -1537,16 +1537,17 @@ int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...) case OPUS_RESET_STATE: { int i; - celt_glog *lpc, *oldBandE, *oldLogE, *oldLogE2; - lpc = (celt_glog*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels); - oldBandE = lpc+st->channels*CELT_LPC_ORDER; + opus_val16 *lpc; + celt_glog *oldBandE, *oldLogE, *oldLogE2; + lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels); + oldBandE = (celt_glog*)(lpc+st->channels*CELT_LPC_ORDER); oldLogE = oldBandE + 2*st->mode->nbEBands; oldLogE2 = oldLogE + 2*st->mode->nbEBands; OPUS_CLEAR((char*)&st->DECODER_RESET_START, opus_custom_decoder_get_size(st->mode, st->channels)- ((char*)&st->DECODER_RESET_START - (char*)st)); for (i=0;i<2*st->mode->nbEBands;i++) - oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); + oldLogE[i]=oldLogE2[i]=-GCONST(28.f); st->skip_plc = 1; } break; diff --git a/celt/celt_encoder.c b/celt/celt_encoder.c index 880bdd39..03416024 100644 --- a/celt/celt_encoder.c +++ b/celt/celt_encoder.c @@ -441,28 +441,28 @@ static int patch_transient_decision(celt_glog *newE, celt_glog *oldE, int nbEBan { spread_old[start] = oldE[start]; for (i=start+1;i=start;i--) - spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT)); + spread_old[i] = MAXG(spread_old[i], spread_old[i+1]-GCONST(1.0f)); /* Compute mean increase */ c=0; do { for (i=IMAX(2,start);i QCONST16(1.f, DB_SHIFT); + return mean_diff > GCONST(1.f); } /** Apply window and compute the MDCT for all sub-frames and @@ -852,8 +852,8 @@ static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC))); #ifdef FIXED_POINT /* Compensate for Q20 vs Q14 input and convert output to Q8 */ - logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); - logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); + logXC = PSHR32(logXC-QCONST16(6.f, 10),10-8); + logXC2 = PSHR32(logXC2-QCONST16(6.f, 10),10-8); #endif trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC)); @@ -869,7 +869,7 @@ static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, } while (++c t1) { if (t1 < t3) - return MIN16(t2, t3); + return MING(t2, t3); else - return MIN16(t4, t1); + return MING(t4, t1); } else { if (t2 < t3) - return MIN16(t1, t3); + return MING(t1, t3); else - return MIN16(t2, t4); + return MING(t2, t4); } } @@ -1005,19 +1005,19 @@ static celt_glog dynalloc_analysis(const celt_glog *bandLogE, const celt_glog *b ALLOC(bandLogE3, nbEBands, celt_glog); OPUS_CLEAR(offsets, nbEBands); /* Dynamic allocation code */ - maxDepth=-QCONST16(31.9f, DB_SHIFT); + maxDepth=-GCONST(31.9f); for (i=0;i=0;i--) - mask[i] = MAX16(mask[i], mask[i+1] - QCONST16(3.f, DB_SHIFT)); + mask[i] = MAXG(mask[i], mask[i+1] - GCONST(3.f)); for (i=0;i bandLogE3[i-1]+QCONST16(.5f,DB_SHIFT)) + if (bandLogE3[i] > bandLogE3[i-1]+GCONST(.5f)) last=i; - f[i] = MIN16(f[i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE3[i]); + f[i] = MING(f[i-1]+GCONST(1.5f), bandLogE3[i]); } for (i=last-1;i>=0;i--) - f[i] = MIN16(f[i], MIN16(f[i+1]+QCONST16(2.f,DB_SHIFT), bandLogE3[i])); + f[i] = MING(f[i], MING(f[i+1]+GCONST(2.f), bandLogE3[i])); /* Combine with a median filter to avoid dynalloc triggering unnecessarily. The "offset" value controls how conservative we are -- a higher offset reduces the impact of the median filter and makes dynalloc use more bits. */ - offset = QCONST16(1.f, DB_SHIFT); + offset = GCONST(1.f); for (i=2;i=12) - follower[i] = HALF16(follower[i]); + follower[i] = HALF32(follower[i]); } /* Compensate for Opus' under-allocation on tones. */ if (toneishness > QCONST32(.98f, 29)) { @@ -1150,10 +1150,10 @@ static celt_glog dynalloc_analysis(const celt_glog *bandLogE, const celt_glog *b int freq_bin = (int)floor(.5 + tone_freq*120/M_PI); #endif for (i=start;i= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += QCONST16(2., DB_SHIFT); - if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += QCONST16(1., DB_SHIFT); - if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += QCONST16(1., DB_SHIFT); - if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += QCONST16(.5, DB_SHIFT); + if (freq_bin >= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += GCONST(2.f); + if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += GCONST(1.f); + if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += GCONST(1.f); + if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += GCONST(.5f); } } #ifdef DISABLE_FLOAT_API @@ -1162,7 +1162,7 @@ static celt_glog dynalloc_analysis(const celt_glog *bandLogE, const celt_glog *b if (analysis->valid) { for (i=start;ileak_boost[i]; + follower[i] = follower[i] + GCONST(1.f/64.f)*analysis->leak_boost[i]; } #endif for (i=start;ienergy_mask[nbEBands*c+i], - QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); + mask = MAXG(MING(st->energy_mask[nbEBands*c+i], + GCONST(.25f)), -GCONST(2.0f)); if (mask > 0) - mask = HALF16(mask); + mask = HALF32(mask); mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]); count += eBands[i+1]-eBands[i]; diff += MULT16_16(mask, 1+2*i-mask_end); @@ -1970,11 +1970,11 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in } celt_assert(count>0); mask_avg = DIV32_16(mask_avg,count); - mask_avg += QCONST16(.2f, DB_SHIFT); + mask_avg += GCONST(.2f); diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end); /* Again, being conservative */ diff = HALF32(diff); - diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_SHIFT)); + diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f)); /* Find the band that's in the middle of the coded spectrum */ for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++); count_dynalloc=0; @@ -1984,14 +1984,14 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in celt_glog unmask; lin = mask_avg + diff*(i-midband); if (C==2) - unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]); + unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i]); else unmask = st->energy_mask[i]; - unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT)); + unmask = MING(unmask, GCONST(.0f)); unmask -= lin; - if (unmask > QCONST16(.25f, DB_SHIFT)) + if (unmask > GCONST(.25f)) { - surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT); + surround_dynalloc[i] = unmask - GCONST(.25f); count_dynalloc++; } } @@ -1999,7 +1999,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in { /* If we need dynalloc in many bands, it's probably because our initial masking rate was too low. */ - mask_avg += QCONST16(.25f, DB_SHIFT); + mask_avg += GCONST(.25f); if (mask_avg>0) { /* Something went really wrong in the original calculations, @@ -2009,10 +2009,10 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in OPUS_CLEAR(surround_dynalloc, mask_end); } else { for(i=0;ilfe) { - celt_glog follow=-QCONST16(10.0f,DB_SHIFT); + celt_glog follow=-GCONST(10.0f); opus_val32 frame_avg=0; - celt_glog offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0; + celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT)):0; for(i=start;ispec_avg); - temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHIFT), temporal_vbr)); + temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr)); st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr); } /*for (i=0;i<21;i++) @@ -2060,7 +2060,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in for (c=0;cmode, st->channels)- ((char*)&st->ENCODER_RESET_START - (char*)st)); for (i=0;ichannels*st->mode->nbEBands;i++) - oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); + oldLogE[i]=oldLogE2[i]=-GCONST(28.f); st->vbr_offset = 0; st->delayedIntra = 1; st->spread_decision = SPREAD_NORMAL; diff --git a/celt/fixed_debug.h b/celt/fixed_debug.h index ef2e5d02..919b8db2 100644 --- a/celt/fixed_debug.h +++ b/celt/fixed_debug.h @@ -51,6 +51,8 @@ extern opus_int64 celt_mips; #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits)))) #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits)))) +#define GCONST2(x,bits) ((celt_glog)(.5+(x)*(((celt_glog)1)<<(bits)))) +#define GCONST(x) GCONST2((x),DB_SHIFT) #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768) #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL) diff --git a/celt/fixed_generic.h b/celt/fixed_generic.h index 8f29d46b..deaf6367 100644 --- a/celt/fixed_generic.h +++ b/celt/fixed_generic.h @@ -77,6 +77,12 @@ /** Compile-time conversion of float constant to 32-bit value */ #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits)))) +/** Compile-time conversion of float constant to log gain value */ +#define GCONST2(x,bits) ((celt_glog)(.5+(x)*(((celt_glog)1)<<(bits)))) + +/** Compile-time conversion of float constant to DB_SHFIT log gain value */ +#define GCONST(x) GCONST2((x),DB_SHIFT) + /** Negate a 16-bit value */ #define NEG16(x) (-(x)) /** Negate a 32-bit value */ diff --git a/celt/quant_bands.c b/celt/quant_bands.c index c7a64ebd..f9a28c18 100644 --- a/celt/quant_bands.c +++ b/celt/quant_bands.c @@ -146,7 +146,7 @@ static opus_val32 loss_distortion(const celt_glog *eBands, celt_glog *oldEBands, c=0; do { for (i=start;inbEBands]; - oldE = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]); + oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]); #ifdef FIXED_POINT f = SHL32(EXTEND32(x),7) - PSHR32(MULT16_16(coef,oldE), 8) - prev[c]; /* Rounding to nearest integer here is really important! */ qi = (f+QCONST32(.5f,DB_SHIFT+7))>>(DB_SHIFT+7); - decay_bound = EXTRACT16(MAX32(-QCONST16(28.f,DB_SHIFT), + decay_bound = EXTRACT16(MAX32(-GCONST(28.f), SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay))); #else f = x-coef*oldE-prev[c]; /* Rounding to nearest integer here is really important! */ qi = (int)floor(.5f+f); - decay_bound = MAX16(-QCONST16(28.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]) - max_decay; + decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay; #endif /* Prevent the energy from going down too quickly (e.g. for bands that have just one bin) */ if (qi < 0 && x < decay_bound) { - qi += (int)SHR16(SUB16(decay_bound,x), DB_SHIFT); + qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT); if (qi > 0) qi = 0; } @@ -243,7 +243,7 @@ static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, } else qi = -1; - error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT); + error[i+c*m->nbEBands] = PSHR32(f,7) - SHL32(qi,DB_SHIFT); badness += abs(qi0-qi); q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); @@ -282,7 +282,7 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, if (tell+3 > budget) two_pass = intra = 0; - max_decay = QCONST16(16.f,DB_SHIFT); + max_decay = GCONST(16.f); if (end-start>10) { #ifdef FIXED_POINT @@ -292,7 +292,7 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, #endif } if (lfe) - max_decay = QCONST16(3.f,DB_SHIFT); + max_decay = GCONST(3.f); enc_start_state = *enc; ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog); @@ -374,7 +374,7 @@ void quant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEBan celt_glog offset; #ifdef FIXED_POINT /* Has to be without rounding */ - q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]); + q2 = (error[i+c*m->nbEBands]+GCONST(.5f))>>(DB_SHIFT-fine_quant[i]); #else q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac); #endif @@ -384,7 +384,7 @@ void quant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEBan q2 = 0; ec_enc_bits(enc, q2, fine_quant[i]); #ifdef FIXED_POINT - offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT)); + offset = SUB32(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+GCONST(.5f),fine_quant[i]),GCONST(.5f)); #else offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f; #endif @@ -413,7 +413,7 @@ void quant_energy_finalise(const CELTMode *m, int start, int end, celt_glog *old q2 = error[i+c*m->nbEBands]<0 ? 0 : 1; ec_enc_bits(enc, q2, 1); #ifdef FIXED_POINT - offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1); + offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1); #else offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); #endif @@ -479,7 +479,7 @@ void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_glog *old qi = -1; q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); - oldEBands[i+c*m->nbEBands] = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]); + oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]); tmp = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]),8) + prev[c] + SHL32(q,7); #ifdef FIXED_POINT tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp); @@ -504,7 +504,7 @@ void unquant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEB celt_glog offset; q2 = ec_dec_bits(dec, fine_quant[i]); #ifdef FIXED_POINT - offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT)); + offset = SUB32(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+GCONST(.5f),fine_quant[i]),GCONST(.5f)); #else offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f; #endif @@ -530,7 +530,7 @@ void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_glog *o celt_glog offset; q2 = ec_dec_bits(dec, 1); #ifdef FIXED_POINT - offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1); + offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1); #else offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); #endif @@ -551,13 +551,13 @@ void amp2Log2(const CELTMode *m, int effEnd, int end, { bandLogE[i+c*m->nbEBands] = celt_log2(bandE[i+c*m->nbEBands]) - - SHL16((celt_glog)eMeans[i],DB_SHIFT-4); + - SHL32((celt_glog)eMeans[i],DB_SHIFT-4); #ifdef FIXED_POINT /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */ - bandLogE[i+c*m->nbEBands] += QCONST16(2.f, DB_SHIFT); + bandLogE[i+c*m->nbEBands] += GCONST(2.f); #endif } for (i=effEnd;inbEBands+i] = -QCONST16(14.f,DB_SHIFT); + bandLogE[c*m->nbEBands+i] = -GCONST(14.f); } while (++c < C); } diff --git a/src/opus_encoder.c b/src/opus_encoder.c index e724f610..b4260e53 100644 --- a/src/opus_encoder.c +++ b/src/opus_encoder.c @@ -1952,7 +1952,7 @@ static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, if (st->energy_masking && st->use_vbr && !st->lfe) { opus_val32 mask_sum=0; - opus_val16 masking_depth; + celt_glog masking_depth; opus_int32 rate_offset; int c; int end = 17; @@ -1970,17 +1970,17 @@ static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, { for(i=0;ienergy_masking[21*c+i], - QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); + celt_glog mask; + mask = MAXG(MING(st->energy_masking[21*c+i], + GCONST(.5f)), -GCONST(2.0f)); if (mask > 0) - mask = HALF16(mask); + mask = HALF32(mask); mask_sum += mask; } } /* Conservative rate reduction, we cut the masking in half */ masking_depth = mask_sum / end*st->channels; - masking_depth += QCONST16(.2f, DB_SHIFT); + masking_depth += GCONST(.2f); rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT); rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); /* Split the rate change between the SILK and CELT part for hybrid. */ diff --git a/src/opus_multistream_encoder.c b/src/opus_multistream_encoder.c index af628d0d..c7a5a2a8 100644 --- a/src/opus_multistream_encoder.c +++ b/src/opus_multistream_encoder.c @@ -184,15 +184,15 @@ static void channel_pos(int channels, int pos[8]) #if 1 /* Computes a rough approximation of log2(2^a + 2^b) */ -static opus_val16 logSum(opus_val16 a, opus_val16 b) +static opus_val16 logSum(celt_glog a, celt_glog b) { - opus_val16 max; - opus_val32 diff; - opus_val16 frac; + celt_glog max; + celt_glog diff; + celt_glog frac; static const opus_val16 diff_table[17] = { - QCONST16(0.5000000f, DB_SHIFT), QCONST16(0.2924813f, DB_SHIFT), QCONST16(0.1609640f, DB_SHIFT), QCONST16(0.0849625f, DB_SHIFT), - QCONST16(0.0437314f, DB_SHIFT), QCONST16(0.0221971f, DB_SHIFT), QCONST16(0.0111839f, DB_SHIFT), QCONST16(0.0056136f, DB_SHIFT), - QCONST16(0.0028123f, DB_SHIFT) + GCONST(0.5000000f), GCONST(0.2924813f), GCONST(0.1609640f), GCONST(0.0849625f), + GCONST(0.0437314f), GCONST(0.0221971f), GCONST(0.0111839f), GCONST(0.0056136f), + GCONST(0.0028123f) }; int low; if (a>b) @@ -203,16 +203,16 @@ static opus_val16 logSum(opus_val16 a, opus_val16 b) max = b; diff = SUB32(EXTEND32(b),EXTEND32(a)); } - if (!(diff < QCONST16(8.f, DB_SHIFT))) /* inverted to catch NaNs */ + if (!(diff < GCONST(8.f))) /* inverted to catch NaNs */ return max; #ifdef FIXED_POINT low = SHR32(diff, DB_SHIFT-1); - frac = SHL16(diff - SHL16(low, DB_SHIFT-1), 16-DB_SHIFT); + frac = SHL32(diff - SHL32(low, DB_SHIFT-1), 16-DB_SHIFT); #else low = (int)floor(2*diff); frac = 2*diff - low; #endif - return max + diff_table[low] + MULT16_16_Q15(frac, SUB16(diff_table[low+1], diff_table[low])); + return max + diff_table[low] + MULT16_16_Q15(frac, SUB32(diff_table[low+1], diff_table[low])); } #else opus_val16 logSum(opus_val16 a, opus_val16 b) @@ -257,7 +257,7 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, celt_glog *ba for (c=0;c<3;c++) for (i=0;i<21;i++) - maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT); + maskLogE[c][i] = -GCONST(28.f); for (c=0;c=0;i--) - bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16(2.f, DB_SHIFT)); + bandLogE[21*c+i] = MAXG(bandLogE[21*c+i], bandLogE[21*c+i+1]-GCONST(2.f)); if (pos[c]==1) { for (i=0;i<21;i++) @@ -318,8 +318,8 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, celt_glog *ba { for (i=0;i<21;i++) { - maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); - maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); + maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-GCONST(.5f)); + maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]-GCONST(.5f)); } } #if 0