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