00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <math.h>
00025
00026 #include "sipr.h"
00027 #include "libavutil/mathematics.h"
00028 #include "lsp.h"
00029 #include "celp_math.h"
00030 #include "acelp_vectors.h"
00031 #include "acelp_pitch_delay.h"
00032 #include "acelp_filters.h"
00033 #include "celp_filters.h"
00034
00035 #include "sipr16kdata.h"
00036
00043 static void lsf2lsp(const float *lsf, double *lsp)
00044 {
00045 int i;
00046
00047 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
00048 lsp[i] = cosf(lsf[i]);
00049 }
00050
00051 static void dequant(float *out, const int *idx, const float *cbs[])
00052 {
00053 int i;
00054
00055 for (i = 0; i < 4; i++)
00056 memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
00057
00058 memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
00059 }
00060
00061 static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
00062 const int* parm, int ma_pred)
00063 {
00064 int i;
00065 float isp_q[LP_FILTER_ORDER_16k];
00066
00067 dequant(isp_q, parm, lsf_codebooks_16k);
00068
00069 for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
00070 isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
00071 + qu[ma_pred] * lsf_history[i]
00072 + mean_lsf_16k[i];
00073 }
00074
00075 memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
00076 }
00077
00078 static int dec_delay3_1st(int index)
00079 {
00080 if (index < 390) {
00081 return index + 88;
00082 } else
00083 return 3 * index - 690;
00084 }
00085
00086 static int dec_delay3_2nd(int index, int pit_min, int pit_max,
00087 int pitch_lag_prev)
00088 {
00089 if (index < 62) {
00090 int pitch_delay_min = av_clip(pitch_lag_prev - 10,
00091 pit_min, pit_max - 19);
00092 return 3 * pitch_delay_min + index - 2;
00093 } else
00094 return 3 * pitch_lag_prev;
00095 }
00096
00097 static void postfilter(float* synth, float* iir_mem, float* filt_mem[2],
00098 float* mem_preemph)
00099 {
00100 float buf[30 + LP_FILTER_ORDER_16k];
00101 float *tmpbuf = buf + LP_FILTER_ORDER_16k;
00102 float s;
00103 int i;
00104
00105 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
00106 filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
00107
00108 memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
00109 LP_FILTER_ORDER_16k*sizeof(*buf));
00110
00111 ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30,
00112 LP_FILTER_ORDER_16k);
00113
00114 memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph,
00115 LP_FILTER_ORDER_16k * sizeof(*synth));
00116
00117 ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 2*L_SUBFR_16k,
00118 LP_FILTER_ORDER_16k);
00119
00120 memcpy(mem_preemph, synth + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
00121 LP_FILTER_ORDER_16k * sizeof(*synth));
00122
00123 FFSWAP(float *, filt_mem[0], filt_mem[1]);
00124 for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
00125 synth[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
00126 }
00127
00131 static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
00132 const double *lsp_2nd, const double *lsp_prev)
00133 {
00134 double lsp_1st[LP_FILTER_ORDER_16k];
00135 int i;
00136
00137
00138 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
00139 lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
00140
00141 ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
00142
00143
00144 ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
00145 }
00146
00150 static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
00151 float mr_energy, const float *quant_energy,
00152 const float *ma_prediction_coeff,
00153 int subframe_size, int ma_pred_order)
00154 {
00155 mr_energy +=
00156 ff_dot_productf(quant_energy, ma_prediction_coeff, ma_pred_order);
00157
00158 mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
00159 sqrt((0.01 + ff_dot_productf(fc_v, fc_v, subframe_size)));
00160 return mr_energy;
00161 }
00162
00163 #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
00164
00165 void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params,
00166 float *out_data)
00167 {
00168 int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k;
00169 float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
00170 float lsf_new[LP_FILTER_ORDER_16k];
00171 double lsp_new[LP_FILTER_ORDER_16k];
00172 float Az[2][LP_FILTER_ORDER_16k];
00173 float fixed_vector[L_SUBFR_16k];
00174 float pitch_fac, gain_code;
00175
00176 int i;
00177 int pitch_delay_3x;
00178
00179 float *excitation = ctx->excitation + 292;
00180
00181 lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
00182 params->ma_pred_switch);
00183
00184 ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k);
00185
00186 lsf2lsp(lsf_new, lsp_new);
00187
00188 acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
00189
00190 memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
00191
00192 memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
00193 LP_FILTER_ORDER_16k * sizeof(*synth));
00194
00195 for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
00196 int i_subfr = i * L_SUBFR_16k;
00197 AMRFixed f;
00198 float gain_corr_factor;
00199 int pitch_delay_int;
00200 int pitch_delay_frac;
00201
00202 if (!i) {
00203 pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
00204 } else
00205 pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
00206 PITCH_MIN, PITCH_MAX,
00207 ctx->pitch_lag_prev);
00208
00209 pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
00210 f.pitch_fac = FFMIN(pitch_fac, 1.0);
00211 f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
00212 ctx->pitch_lag_prev = f.pitch_lag;
00213
00214 pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
00215 pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
00216
00217 ff_acelp_interpolatef(&excitation[i_subfr],
00218 &excitation[i_subfr] - pitch_delay_int + 1,
00219 sinc_win, 3, pitch_delay_frac + 1,
00220 LP_FILTER_ORDER, L_SUBFR_16k);
00221
00222
00223 memset(fixed_vector, 0, sizeof(fixed_vector));
00224
00225 ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
00226 ff_fc_4pulses_8bits_tracks_13, 5, 4);
00227
00228 ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
00229
00230 gain_corr_factor = gain_cb_16k[params->gc_index[i]];
00231 gain_code = gain_corr_factor *
00232 acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
00233 19.0 - 15.0/(0.05*M_LN10/M_LN2),
00234 pred_16k, ctx->energy_history,
00235 L_SUBFR_16k, 2);
00236
00237 ctx->energy_history[1] = ctx->energy_history[0];
00238 ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
00239
00240 ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
00241 fixed_vector, pitch_fac,
00242 gain_code, L_SUBFR_16k);
00243
00244 ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
00245 &excitation[i_subfr], L_SUBFR_16k,
00246 LP_FILTER_ORDER_16k);
00247
00248 }
00249 memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
00250 LP_FILTER_ORDER_16k * sizeof(*synth));
00251
00252 memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
00253 (L_INTERPOL+PITCH_MAX) * sizeof(float));
00254
00255 postfilter(synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
00256
00257 memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
00258
00259 ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size);
00260
00261 }
00262
00263 void ff_sipr_init_16k(SiprContext *ctx)
00264 {
00265 int i;
00266
00267 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
00268 ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
00269
00270 ctx->filt_mem[0] = ctx->filt_buf[0];
00271 ctx->filt_mem[1] = ctx->filt_buf[1];
00272
00273 ctx->pitch_lag_prev = 180;
00274 }