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 *out_data, float* synth, float* iir_mem,
00098 float* filt_mem[2], 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, 30,
00118 LP_FILTER_ORDER_16k);
00119
00120 memcpy(out_data + 30 - LP_FILTER_ORDER_16k,
00121 synth + 30 - LP_FILTER_ORDER_16k,
00122 LP_FILTER_ORDER_16k * sizeof(*synth));
00123
00124 ff_celp_lp_synthesis_filterf(out_data + 30, filt_mem[0],
00125 synth + 30, 2 * L_SUBFR_16k - 30,
00126 LP_FILTER_ORDER_16k);
00127
00128
00129 memcpy(mem_preemph, out_data + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
00130 LP_FILTER_ORDER_16k * sizeof(*synth));
00131
00132 FFSWAP(float *, filt_mem[0], filt_mem[1]);
00133 for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
00134 out_data[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
00135 }
00136
00140 static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
00141 const double *lsp_2nd, const double *lsp_prev)
00142 {
00143 double lsp_1st[LP_FILTER_ORDER_16k];
00144 int i;
00145
00146
00147 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
00148 lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
00149
00150 ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
00151
00152
00153 ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
00154 }
00155
00159 static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
00160 float mr_energy, const float *quant_energy,
00161 const float *ma_prediction_coeff,
00162 int subframe_size, int ma_pred_order)
00163 {
00164 mr_energy +=
00165 ff_dot_productf(quant_energy, ma_prediction_coeff, ma_pred_order);
00166
00167 mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
00168 sqrt((0.01 + ff_dot_productf(fc_v, fc_v, subframe_size)));
00169 return mr_energy;
00170 }
00171
00172 #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
00173
00174 void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params,
00175 float *out_data)
00176 {
00177 int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k;
00178 float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
00179 float lsf_new[LP_FILTER_ORDER_16k];
00180 double lsp_new[LP_FILTER_ORDER_16k];
00181 float Az[2][LP_FILTER_ORDER_16k];
00182 float fixed_vector[L_SUBFR_16k];
00183 float pitch_fac, gain_code;
00184
00185 int i;
00186 int pitch_delay_3x;
00187
00188 float *excitation = ctx->excitation + 292;
00189
00190 lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
00191 params->ma_pred_switch);
00192
00193 ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k);
00194
00195 lsf2lsp(lsf_new, lsp_new);
00196
00197 acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
00198
00199 memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
00200
00201 memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
00202 LP_FILTER_ORDER_16k * sizeof(*synth));
00203
00204 for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
00205 int i_subfr = i * L_SUBFR_16k;
00206 AMRFixed f;
00207 float gain_corr_factor;
00208 int pitch_delay_int;
00209 int pitch_delay_frac;
00210
00211 if (!i) {
00212 pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
00213 } else
00214 pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
00215 PITCH_MIN, PITCH_MAX,
00216 ctx->pitch_lag_prev);
00217
00218 pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
00219 f.pitch_fac = FFMIN(pitch_fac, 1.0);
00220 f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
00221 ctx->pitch_lag_prev = f.pitch_lag;
00222
00223 pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
00224 pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
00225
00226 ff_acelp_interpolatef(&excitation[i_subfr],
00227 &excitation[i_subfr] - pitch_delay_int + 1,
00228 sinc_win, 3, pitch_delay_frac + 1,
00229 LP_FILTER_ORDER, L_SUBFR_16k);
00230
00231
00232 memset(fixed_vector, 0, sizeof(fixed_vector));
00233
00234 ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
00235 ff_fc_4pulses_8bits_tracks_13, 5, 4);
00236
00237 ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
00238
00239 gain_corr_factor = gain_cb_16k[params->gc_index[i]];
00240 gain_code = gain_corr_factor *
00241 acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
00242 19.0 - 15.0/(0.05*M_LN10/M_LN2),
00243 pred_16k, ctx->energy_history,
00244 L_SUBFR_16k, 2);
00245
00246 ctx->energy_history[1] = ctx->energy_history[0];
00247 ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
00248
00249 ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
00250 fixed_vector, pitch_fac,
00251 gain_code, L_SUBFR_16k);
00252
00253 ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
00254 &excitation[i_subfr], L_SUBFR_16k,
00255 LP_FILTER_ORDER_16k);
00256
00257 }
00258 memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
00259 LP_FILTER_ORDER_16k * sizeof(*synth));
00260
00261 memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
00262 (L_INTERPOL+PITCH_MAX) * sizeof(float));
00263
00264 postfilter(out_data, synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
00265
00266 memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
00267 }
00268
00269 void ff_sipr_init_16k(SiprContext *ctx)
00270 {
00271 int i;
00272
00273 for (i = 0; i < LP_FILTER_ORDER_16k; i++)
00274 ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
00275
00276 ctx->filt_mem[0] = ctx->filt_buf[0];
00277 ctx->filt_mem[1] = ctx->filt_buf[1];
00278
00279 ctx->pitch_lag_prev = 180;
00280 }