📄 melp_sub.c
字号:
}
}
/* Don't use invalid code (only top band voiced) */
if (index == INVALID_BPVC){
bpvc[num_bands - 1] = 0;
index = 0;
}
} else {
/* Unvoiced: force all bands unvoiced */
uv_flag = TRUE;
index = 0;
v_zap(bpvc, num_bands);
}
*bpvc_index = index;
return(uv_flag);
}
void q_bpvc_dec(Shortword bpvc[], Shortword bpvc_index, BOOLEAN uv_flag,
Shortword num_bands)
{
register Shortword i;
if (uv_flag){ /* Unvoiced: set all bpvc to 0 */
bpvc_index = 0;
bpvc[0] = 0;
} else /* Voiced: set bpvc[0] to 1.0 */
bpvc[0] = ONE_Q14;
if (bpvc_index == INVALID_BPVC){
/* Invalid code received: set higher band voicing to zero */
bpvc_index = 0;
}
/* Decode remaining bands */
for (i = (Shortword) (num_bands - 1); i > 0; i--){
if (bpvc_index & 1)
bpvc[i] = ONE_Q14;
else
bpvc[i] = 0;
bpvc_index = shr(bpvc_index, 1);
}
}
/* Name: q_gain.c, q_gain_dec.c */
/* Description: Quantize/decode two gain terms using quasi-differential */
/* coding */
/* Inputs: */
/* gain[2],gain_index[2] */
/* gn_qlo, gn_qup, gn_qlev for second gain term */
/* Outputs: */
/* gain[2],gain_index[2] */
/* Returns: void */
/* */
/* Copyright (c) 1995,1997 by Texas Instruments, Inc. All rights reserved. */
/* */
/* Q values: */
/* *gain - Q8, gn_qlo - Q8, gn_qup - Q8, gn_qlev - Q10 */
void q_gain(Shortword *gain, Shortword *gain_index, Shortword gn_qlo,
Shortword gn_qup, Shortword gn_qlev, Shortword gn_qlev_q,
Shortword double_flag, Shortword scale)
{
static Shortword prev_gain = 0;
Shortword temp, temp2;
/* Quantize second gain term with uniform quantizer */
quant_u(&gain[1], &gain_index[1], gn_qlo, gn_qup, gn_qlev, gn_qlev_q,
double_flag, scale);
/* Check for intermediate gain interpolation */
if (gain[0] < gn_qlo){
gain[0] = gn_qlo;
}
if (gain[0] > gn_qup){
gain[0] = gn_qup;
}
/* if (fabs(gain[1] - prev_gain) < GAIN_INT_DB &&
fabs(gain[0] - 0.5*(gain[1]+prev_gain)) < 3.0) */
temp = add(shr(gain[1], 1), shr(prev_gain, 1));
if (abs_s(sub(gain[1], prev_gain)) < GAIN_INT_DB_Q8 &&
abs_s(sub(gain[0], temp)) < THREE_Q8){
/* interpolate and set special code */
/* gain[0] = 0.5*(gain[1] + prev_gain); */
gain[0] = temp;
gain_index[0] = 0;
} else {
/* Code intermediate gain with 7-levels */
if (prev_gain < gain[1]){
temp = prev_gain;
temp2 = gain[1];
} else {
temp = gain[1];
temp2 = prev_gain;
}
temp = sub(temp, SIX_Q8);
temp2 = add(temp2, SIX_Q8);
if (temp < gn_qlo){
temp = gn_qlo;
}
if (temp2 > gn_qup){
temp2 = gn_qup;
}
quant_u(&gain[0], &gain_index[0], temp, temp2, 6, SIX_Q12, 0, 3);
/* Skip all-zero code */
gain_index[0] = add(gain_index[0], 1);
}
/* Update previous gain for next time */
prev_gain = gain[1];
}
void q_gain_dec(Shortword *gain, Shortword *gain_index, Shortword gn_qlo,
Shortword gn_qup, Shortword gn_qlev_q, Shortword scale)
{
static Shortword prev_gain = 0;
static BOOLEAN prev_gain_err = FALSE;
Shortword temp, temp2;
/* Decode second gain term */
quant_u_dec(gain_index[1], &gain[1], gn_qlo, gn_qup, gn_qlev_q, scale);
if (gain_index[0] == 0){
/* interpolation bit code for intermediate gain */
temp = sub(gain[1], prev_gain);
temp = abs_s(temp);
if (temp > GAIN_INT_DB_Q8){
/* Invalid received data (bit error) */
if (!prev_gain_err){
/* First time: don't allow gain excursion */
gain[1] = prev_gain;
}
prev_gain_err = TRUE;
} else
prev_gain_err = FALSE;
/* Use interpolated gain value */
/* gain[0] = 0.5*(gain[1]+prev_gain); */
gain[0] = add(shr(gain[1], 1), shr(prev_gain, 1));
} else {
/* Decode 7-bit quantizer for first gain term */
prev_gain_err = FALSE;
if (prev_gain < gain[1]){
temp = prev_gain;
temp2 = gain[1];
} else {
temp = gain[1];
temp2 = prev_gain;
}
temp = sub(temp, SIX_Q8);
temp2 = add(temp2, SIX_Q8);
if (temp < gn_qlo)
temp = gn_qlo;
if (temp2 > gn_qup)
temp2 = gn_qup;
/* Previously we subtract 1 from gain_index[0] in this function. */
/* Now to incorporate the transcoder, we want to avoid changing */
/* gain_index[0] in this function. */
quant_u_dec(sub(gain_index[0], 1), &gain[0], temp, temp2, SIX_Q12, 3);
}
/* Update previous gain for next time */
prev_gain = gain[1];
}
/* Name: scale_adj.c */
/* Description: Adjust scaling of output speech signal. */
/* Inputs: */
/* speech - speech signal */
/* gain - desired RMS gain (log gain) */
/* length - number of samples in signal */
/* scale_over - number of points to interpolate scale factor */
/* Warning: scale_over is assumed to be less than length. */
/* Outputs: */
/* speech - scaled speech signal */
/* Returns: void */
/* */
/* Copyright (c) 1995,1997 by Texas Instruments, Inc. All rights reserved. */
/* */
/* Q values: */
/* *speech - Q0 */
/* gain - Q12 */
void scale_adj(Shortword *speech, Shortword gain, Shortword length,
Shortword scale_over, Shortword inv_scale_over)
{
static Shortword prev_scale; /* Previous scale factor, Q13 */
register Shortword i;
Shortword scale, shift, log_magsq, log_length;
Shortword temp;
Shortword *tempbuf;
Longword L_magsq, L_length, interp1, interp2;
Longword L_temp;
/* Calculate desired scaling factor to match gain level */
/* scale = gain / (sqrt(v_magsq(&speech[0],length) / length) + .01); */
/* find normalization factor for buffer */
tempbuf = v_get(length);
shift = 4;
v_equ_shr(tempbuf, speech, shift, length);
L_magsq = L_v_magsq(tempbuf, length, 0, 1);
if (L_magsq){
/* normalize with 1 bit of headroom */
temp = norm_l(L_magsq);
temp = sub(temp, 1);
shift = sub(shift, shr(temp, 1));
} else
shift = 0;
v_equ_shr(tempbuf, speech, shift, length);
/* exp = log(2^shift) */
shift = shl(shift, 1); /* total compensation = shift*2 */
temp = shl(ONE_Q8, shift);
shift = log10_fxp(temp, 8); /* shift in Q12 */
L_magsq = L_v_magsq(tempbuf, length, 0, 0);
L_magsq = L_add(L_magsq, 1); /* ensure L_magsq is not zero */
log_magsq = L_log10_fxp(L_magsq, 0); /* log_magsq in Q11 */
/* L_magsq = log_magsq in Q12 */
L_magsq = L_deposit_l(log_magsq);
L_magsq = L_shl(L_magsq, 1);
/* compensate for normalization */
/* L_magsq = log magnitude/2 in Q13 */
L_temp = L_deposit_l(shift);
L_magsq = L_add(L_magsq, L_temp);
temp = shl(length, 7);
log_length = log10_fxp(temp, 7); /* log_length in Q12 */
/* L_length = log_length/2 in Q13 */
L_length = L_deposit_l(log_length);
L_temp = L_deposit_l(gain);
L_temp = L_shl(L_temp, 1); /* L_temp in Q13 */
L_temp = L_sub(L_temp, L_magsq);
L_temp = L_add(L_temp, L_length);
L_temp = L_shr(L_temp, 1); /* Q13 -> Q12 */
temp = extract_l(L_temp); /* temp in Q12 */
scale = pow10_fxp(temp, 13);
/* interpolate scale factors for first scale_over points */
for (i = 1; i < scale_over; i++){
/* speech[i-1] *= ((scale*i + prev_scale*(scale_over - i))
* (1.0/scale_over)); */
temp = sub(scale_over, i);
temp = shl(temp, 11);
L_temp = L_mult(temp, inv_scale_over); /* L_temp in Q30 */
L_temp = L_shl(L_temp, 1); /* L_temp in Q31 */
temp = extract_h(L_temp); /* temp in Q15 */
interp1 = L_mult(prev_scale, temp); /* interp1 in Q29 */
temp = shl(i, 11);
L_temp = L_mult(temp, inv_scale_over); /* L_temp in Q30 */
L_temp = L_shl(L_temp, 1); /* L_temp in Q31 */
temp = extract_h(L_temp); /* temp in Q15 */
interp2 = L_mult(scale, temp); /* interp2 in Q29 */
L_temp = L_add(interp1, interp2);
interp1 = extract_h(L_temp); /* interp1 in Q13 */
L_temp = L_mult(speech[i - 1], (Shortword) interp1);
L_temp = L_shl(L_temp, 2);
speech[i-1] = extract_h(L_temp);
}
/* Scale rest of signal */
v_scale_shl(&speech[scale_over - 1], scale, (Shortword) (length - scale_over + 1), 2);
/* Update previous scale factor for next call */
prev_scale = scale;
v_free(tempbuf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -