00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/common.h"
00024 #include "libavutil/libm.h"
00025 #include "libavutil/mathematics.h"
00026 #include "avcodec.h"
00027 #include "dsputil.h"
00028 #include "acelp_pitch_delay.h"
00029 #include "celp_math.h"
00030
00031 int ff_acelp_decode_8bit_to_1st_delay3(int ac_index)
00032 {
00033 ac_index += 58;
00034 if(ac_index > 254)
00035 ac_index = 3 * ac_index - 510;
00036 return ac_index;
00037 }
00038
00039 int ff_acelp_decode_4bit_to_2nd_delay3(
00040 int ac_index,
00041 int pitch_delay_min)
00042 {
00043 if(ac_index < 4)
00044 return 3 * (ac_index + pitch_delay_min);
00045 else if(ac_index < 12)
00046 return 3 * pitch_delay_min + ac_index + 6;
00047 else
00048 return 3 * (ac_index + pitch_delay_min) - 18;
00049 }
00050
00051 int ff_acelp_decode_5_6_bit_to_2nd_delay3(
00052 int ac_index,
00053 int pitch_delay_min)
00054 {
00055 return 3 * pitch_delay_min + ac_index - 2;
00056 }
00057
00058 int ff_acelp_decode_9bit_to_1st_delay6(int ac_index)
00059 {
00060 if(ac_index < 463)
00061 return ac_index + 105;
00062 else
00063 return 6 * (ac_index - 368);
00064 }
00065 int ff_acelp_decode_6bit_to_2nd_delay6(
00066 int ac_index,
00067 int pitch_delay_min)
00068 {
00069 return 6 * pitch_delay_min + ac_index - 3;
00070 }
00071
00072 void ff_acelp_update_past_gain(
00073 int16_t* quant_energy,
00074 int gain_corr_factor,
00075 int log2_ma_pred_order,
00076 int erasure)
00077 {
00078 int i;
00079 int avg_gain=quant_energy[(1 << log2_ma_pred_order) - 1];
00080
00081 for(i=(1 << log2_ma_pred_order) - 1; i>0; i--)
00082 {
00083 avg_gain += quant_energy[i-1];
00084 quant_energy[i] = quant_energy[i-1];
00085 }
00086
00087 if(erasure)
00088 quant_energy[0] = FFMAX(avg_gain >> log2_ma_pred_order, -10240) - 4096;
00089 else
00090 quant_energy[0] = (6165 * ((ff_log2(gain_corr_factor) >> 2) - (13 << 13))) >> 13;
00091 }
00092
00093 int16_t ff_acelp_decode_gain_code(
00094 DSPContext *dsp,
00095 int gain_corr_factor,
00096 const int16_t* fc_v,
00097 int mr_energy,
00098 const int16_t* quant_energy,
00099 const int16_t* ma_prediction_coeff,
00100 int subframe_size,
00101 int ma_pred_order)
00102 {
00103 int i;
00104
00105 mr_energy <<= 10;
00106
00107 for(i=0; i<ma_pred_order; i++)
00108 mr_energy += quant_energy[i] * ma_prediction_coeff[i];
00109
00110 #ifdef G729_BITEXACT
00111 mr_energy += (((-6165LL * ff_log2(dsp->scalarproduct_int16(fc_v, fc_v, subframe_size, 0))) >> 3) & ~0x3ff);
00112
00113 mr_energy = (5439 * (mr_energy >> 15)) >> 8;
00114
00115 return bidir_sal(
00116 ((ff_exp2(mr_energy & 0x7fff) + 16) >> 5) * (gain_corr_factor >> 1),
00117 (mr_energy >> 15) - 25
00118 );
00119 #else
00120 mr_energy = gain_corr_factor * exp(M_LN10 / (20 << 23) * mr_energy) /
00121 sqrt(dsp->scalarproduct_int16(fc_v, fc_v, subframe_size));
00122 return mr_energy >> 12;
00123 #endif
00124 }
00125
00126 float ff_amr_set_fixed_gain(float fixed_gain_factor, float fixed_mean_energy,
00127 float *prediction_error, float energy_mean,
00128 const float *pred_table)
00129 {
00130
00131
00132
00133 float val = fixed_gain_factor *
00134 exp2f(M_LOG2_10 * 0.05 *
00135 (ff_scalarproduct_float_c(pred_table, prediction_error, 4) +
00136 energy_mean)) /
00137 sqrtf(fixed_mean_energy);
00138
00139
00140 memmove(&prediction_error[0], &prediction_error[1],
00141 3 * sizeof(prediction_error[0]));
00142 prediction_error[3] = 20.0 * log10f(fixed_gain_factor);
00143
00144 return val;
00145 }
00146
00147 void ff_decode_pitch_lag(int *lag_int, int *lag_frac, int pitch_index,
00148 const int prev_lag_int, const int subframe,
00149 int third_as_first, int resolution)
00150 {
00151
00152 if (subframe == 0 || (subframe == 2 && third_as_first)) {
00153
00154 if (pitch_index < 197)
00155 pitch_index += 59;
00156 else
00157 pitch_index = 3 * pitch_index - 335;
00158
00159 } else {
00160 if (resolution == 4) {
00161 int search_range_min = av_clip(prev_lag_int - 5, PITCH_DELAY_MIN,
00162 PITCH_DELAY_MAX - 9);
00163
00164
00165 if (pitch_index < 4) {
00166
00167 pitch_index = 3 * (pitch_index + search_range_min) + 1;
00168 } else if (pitch_index < 12) {
00169
00170 pitch_index += 3 * search_range_min + 7;
00171 } else {
00172
00173 pitch_index = 3 * (pitch_index + search_range_min - 6) + 1;
00174 }
00175 } else {
00176
00177 pitch_index--;
00178
00179 if (resolution == 5) {
00180 pitch_index += 3 * av_clip(prev_lag_int - 10, PITCH_DELAY_MIN,
00181 PITCH_DELAY_MAX - 19);
00182 } else
00183 pitch_index += 3 * av_clip(prev_lag_int - 5, PITCH_DELAY_MIN,
00184 PITCH_DELAY_MAX - 9);
00185 }
00186 }
00187 *lag_int = pitch_index * 10923 >> 15;
00188 *lag_frac = pitch_index - 3 * *lag_int - 1;
00189 }