FFmpeg
sipr16k.c
Go to the documentation of this file.
1 /*
2  * SIPR decoder for the 16k mode
3  *
4  * Copyright (c) 2008 Vladimir Voroshilov
5  * Copyright (c) 2009 Vitor Sessak
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <math.h>
25 
26 #include "sipr.h"
27 #include "libavutil/attributes.h"
28 #include "libavutil/common.h"
29 #include "libavutil/float_dsp.h"
30 #include "libavutil/mathematics.h"
31 #include "lsp.h"
32 #include "acelp_vectors.h"
33 #include "acelp_pitch_delay.h"
34 #include "acelp_filters.h"
35 #include "celp_filters.h"
36 
37 #include "sipr16kdata.h"
38 
39 /**
40  * Convert an lsf vector into an lsp vector.
41  *
42  * @param lsf input lsf vector
43  * @param lsp output lsp vector
44  */
45 static void lsf2lsp(const float *lsf, double *lsp)
46 {
47  int i;
48 
49  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
50  lsp[i] = cosf(lsf[i]);
51 }
52 
53 static void dequant(float *out, const int *idx, const float * const cbs[])
54 {
55  int i;
56 
57  for (i = 0; i < 4; i++)
58  memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
59 
60  memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
61 }
62 
63 static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
64  const int* parm, int ma_pred)
65 {
66  int i;
67  float isp_q[LP_FILTER_ORDER_16k];
68 
69  dequant(isp_q, parm, lsf_codebooks_16k);
70 
71  for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
72  isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
73  + qu[ma_pred] * lsf_history[i]
74  + mean_lsf_16k[i];
75  }
76 
77  memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
78 }
79 
80 static int dec_delay3_1st(int index)
81 {
82  if (index < 390) {
83  return index + 88;
84  } else
85  return 3 * index - 690;
86 }
87 
88 static int dec_delay3_2nd(int index, int pit_min, int pit_max,
89  int pitch_lag_prev)
90 {
91  if (index < 62) {
92  int pitch_delay_min = av_clip(pitch_lag_prev - 10,
93  pit_min, pit_max - 19);
94  return 3 * pitch_delay_min + index - 2;
95  } else
96  return 3 * pitch_lag_prev;
97 }
98 
99 static void postfilter(float *out_data, float* synth, float* iir_mem,
100  float* filt_mem[2], float* mem_preemph)
101 {
102  float buf[30 + LP_FILTER_ORDER_16k];
103  float *tmpbuf = buf + LP_FILTER_ORDER_16k;
104  float s;
105  int i;
106 
107  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
108  filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
109 
110  memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
111  LP_FILTER_ORDER_16k*sizeof(*buf));
112 
115 
117  LP_FILTER_ORDER_16k * sizeof(*synth));
118 
121 
122  memcpy(out_data + 30 - LP_FILTER_ORDER_16k,
123  synth + 30 - LP_FILTER_ORDER_16k,
124  LP_FILTER_ORDER_16k * sizeof(*synth));
125 
126  ff_celp_lp_synthesis_filterf(out_data + 30, filt_mem[0],
127  synth + 30, 2 * L_SUBFR_16k - 30,
129 
130 
131  memcpy(mem_preemph, out_data + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
132  LP_FILTER_ORDER_16k * sizeof(*synth));
133 
134  FFSWAP(float *, filt_mem[0], filt_mem[1]);
135  for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
136  out_data[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
137 }
138 
139 /**
140  * Floating point version of ff_acelp_lp_decode().
141  */
142 static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
143  const double *lsp_2nd, const double *lsp_prev)
144 {
145  double lsp_1st[LP_FILTER_ORDER_16k];
146  int i;
147 
148  /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */
149  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
150  lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
151 
152  ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
153 
154  /* LSP values for second subframe (3.2.5 of G.729) */
155  ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
156 }
157 
158 /**
159  * Floating point version of ff_acelp_decode_gain_code().
160  */
161 static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
162  float mr_energy, const float *quant_energy,
163  const float *ma_prediction_coeff,
164  int subframe_size, int ma_pred_order)
165 {
166  mr_energy += avpriv_scalarproduct_float_c(quant_energy, ma_prediction_coeff,
167  ma_pred_order);
168 
169  mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
170  sqrt((0.01 + avpriv_scalarproduct_float_c(fc_v, fc_v, subframe_size)));
171  return mr_energy;
172 }
173 
174 #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
175 
177  float *out_data)
178 {
180  float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
181  float lsf_new[LP_FILTER_ORDER_16k];
182  double lsp_new[LP_FILTER_ORDER_16k];
183  float Az[2][LP_FILTER_ORDER_16k];
184  float fixed_vector[L_SUBFR_16k];
185  float pitch_fac, gain_code;
186 
187  int i;
188  int pitch_delay_3x;
189 
190  float *excitation = ctx->excitation + 292;
191 
192  lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
193  params->ma_pred_switch);
194 
196 
197  lsf2lsp(lsf_new, lsp_new);
198 
199  acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
200 
201  memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
202 
203  memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
204  LP_FILTER_ORDER_16k * sizeof(*synth));
205 
206  for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
207  int i_subfr = i * L_SUBFR_16k;
208  AMRFixed f;
209  float gain_corr_factor;
210  int pitch_delay_int;
211  int pitch_delay_frac;
212 
213  if (!i) {
214  pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
215  } else
216  pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
218  ctx->pitch_lag_prev);
219 
220  pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
221  f.pitch_fac = FFMIN(pitch_fac, 1.0);
222  f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
223  ctx->pitch_lag_prev = f.pitch_lag;
224 
225  pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
226  pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
227 
229  &excitation[i_subfr] - pitch_delay_int + 1,
230  sinc_win, 3, pitch_delay_frac + 1,
232 
233 
234  memset(fixed_vector, 0, sizeof(fixed_vector));
235 
238 
239  ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
240 
241  gain_corr_factor = gain_cb_16k[params->gc_index[i]];
242  gain_code = gain_corr_factor *
243  acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
244  19.0 - 15.0/(0.05*M_LN10/M_LN2),
245  pred_16k, ctx->energy_history,
246  L_SUBFR_16k, 2);
247 
248  ctx->energy_history[1] = ctx->energy_history[0];
249  ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
250 
251  ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
252  fixed_vector, pitch_fac,
253  gain_code, L_SUBFR_16k);
254 
255  ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
256  &excitation[i_subfr], L_SUBFR_16k,
258 
259  }
260  memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
261  LP_FILTER_ORDER_16k * sizeof(*synth));
262 
263  memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
264  (L_INTERPOL+PITCH_MAX) * sizeof(float));
265 
266  postfilter(out_data, synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
267 
268  memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
269 }
270 
272 {
273  int i;
274 
275  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
276  ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
277 
278  ctx->filt_mem[0] = ctx->filt_buf[0];
279  ctx->filt_mem[1] = ctx->filt_buf[1];
280 
281  ctx->pitch_lag_prev = 180;
282 }
av_clip
#define av_clip
Definition: common.h:100
acelp_vectors.h
out
FILE * out
Definition: movenc.c:55
ff_sipr_decode_frame_16k
void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params, float *out_data)
Definition: sipr16k.c:176
mathematics.h
LP_FILTER_ORDER
#define LP_FILTER_ORDER
linear predictive coding filter order
Definition: amrnbdata.h:53
ff_celp_lp_synthesis_filterf
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
Definition: celp_filters.c:85
gain_cb_16k
static const float gain_cb_16k[32]
Definition: sipr16kdata.h:30
SiprContext::lsf_history
float lsf_history[LP_FILTER_ORDER_16k]
Definition: sipr.h:68
LSFQ_DIFF_MIN
#define LSFQ_DIFF_MIN
minimum LSF distance (3.2.4) 0.0391 in Q13
Definition: g729dec.c:59
M_LN2
#define M_LN2
Definition: mathematics.h:43
dec_delay3_2nd
static int dec_delay3_2nd(int index, int pit_min, int pit_max, int pitch_lag_prev)
Definition: sipr16k.c:88
PITCH_MIN
#define PITCH_MIN
Definition: g723_1.h:43
LP_FILTER_ORDER_16k
#define LP_FILTER_ORDER_16k
Definition: sipr.h:30
cosf
#define cosf(x)
Definition: libm.h:78
log10f
#define log10f(x)
Definition: libm.h:414
SiprParameters
Definition: sipr.h:55
SiprParameters::gc_index
int gc_index[5]
fixed-codebook gain indexes
Definition: sipr.h:61
postfilter
static void postfilter(float *out_data, float *synth, float *iir_mem, float *filt_mem[2], float *mem_preemph)
Definition: sipr16k.c:99
av_cold
#define av_cold
Definition: attributes.h:90
dequant
static void dequant(float *out, const int *idx, const float *const cbs[])
Definition: sipr16k.c:53
s
#define s(width, name)
Definition: cbs_vp9.c:198
frame_size
int frame_size
Definition: mxfenc.c:2424
AMRFixed
Sparse representation for the algebraic codebook (fixed) vector.
Definition: acelp_vectors.h:53
ctx
AVFormatContext * ctx
Definition: movenc.c:49
pred_16k
static const float pred_16k[2]
Definition: sipr16kdata.h:27
acelp_lp_decodef
static void acelp_lp_decodef(float *lp_1st, float *lp_2nd, const double *lsp_2nd, const double *lsp_prev)
Floating point version of ff_acelp_lp_decode().
Definition: sipr16k.c:142
SiprContext::synth
float synth[LP_FILTER_ORDER_16k]
Definition: sipr.h:92
SiprParameters::ma_pred_switch
int ma_pred_switch
switched moving average predictor
Definition: sipr.h:56
L_INTERPOL
#define L_INTERPOL
Number of past samples needed for excitation interpolation.
Definition: sipr.h:40
DIVIDE_BY_3
#define DIVIDE_BY_3(x)
Definition: sipr16k.c:174
dec_delay3_1st
static int dec_delay3_1st(int index)
Definition: sipr16k.c:80
ff_decode_10_pulses_35bits
void ff_decode_10_pulses_35bits(const int16_t *fixed_index, AMRFixed *fixed_sparse, const uint8_t *gray_decode, int half_pulse_count, int bits)
Decode the algebraic codebook index to pulse positions and signs and construct the algebraic codebook...
Definition: acelp_vectors.c:141
ff_sipr_init_16k
av_cold void ff_sipr_init_16k(SiprContext *ctx)
Definition: sipr16k.c:271
SiprParameters::fc_indexes
int16_t fc_indexes[5][10]
fixed-codebook indexes
Definition: sipr.h:60
SiprContext::mem_preemph
float mem_preemph[LP_FILTER_ORDER_16k]
Definition: sipr.h:91
ff_fc_4pulses_8bits_tracks_13
const uint8_t ff_fc_4pulses_8bits_tracks_13[16]
Definition: acelp_vectors.c:63
SiprParameters::pitch_delay
int pitch_delay[5]
pitch delay
Definition: sipr.h:58
celp_filters.h
exp
int8_t exp
Definition: eval.c:73
index
int index
Definition: gxfenc.c:90
float_dsp.h
ma_prediction_coeff
static const uint16_t ma_prediction_coeff[4]
MA prediction coefficients (3.9.1 of G.729, near Equation 69)
Definition: g729data.h:343
f
f
Definition: af_crystalizer.c:122
mean_lsf_16k
static const float mean_lsf_16k[16]
Definition: sipr16kdata.h:47
SiprContext::pitch_lag_prev
int pitch_lag_prev
Definition: sipr.h:87
lsf_decode_fp_16k
static void lsf_decode_fp_16k(float *lsf_history, float *isp_new, const int *parm, int ma_pred)
Definition: sipr16k.c:63
SiprContext::excitation
float excitation[L_INTERPOL+PITCH_MAX+2 *L_SUBFR_16k]
Definition: sipr.h:70
PITCH_MAX
#define PITCH_MAX
Definition: g723_1.h:44
attributes.h
ff_pow_0_5
const float ff_pow_0_5[]
Definition: sipr.c:136
acelp_decode_gain_codef
static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v, float mr_energy, const float *quant_energy, const float *ma_prediction_coeff, int subframe_size, int ma_pred_order)
Floating point version of ff_acelp_decode_gain_code().
Definition: sipr16k.c:161
M_PI
#define M_PI
Definition: mathematics.h:67
sipr.h
SiprParameters::vq_indexes
int vq_indexes[5]
Definition: sipr.h:57
SiprContext
Definition: sipr.h:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
iir_mem
static void iir_mem(const float *x, const float *den, float *y, int N, int ord, float *mem)
Definition: speexdec.c:587
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
acelp_filters.h
ff_weighted_vector_sumf
void ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b, float weight_coeff_a, float weight_coeff_b, int length)
float implementation of weighted sum of two vectors.
Definition: acelp_vectors.c:182
SiprContext::filt_mem
float * filt_mem[2]
Definition: sipr.h:90
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
lsp.h
lsf_codebooks_16k
static const float *const lsf_codebooks_16k[]
Definition: sipr16kdata.h:528
sinc_win
static const float sinc_win[40]
Hamming windowed sinc function, like in AMR.
Definition: sipr16kdata.h:57
L_SUBFR_16k
#define L_SUBFR_16k
Definition: sipr.h:31
ff_set_fixed_vector
void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size)
Add fixed vector to an array from a sparse representation.
Definition: acelp_vectors.c:224
SUBFRAME_COUNT_16k
#define SUBFRAME_COUNT_16k
Definition: sipr.h:45
qu
static const float qu[2]
Definition: sipr16kdata.h:28
avpriv_scalarproduct_float_c
float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
Return the scalar product of two vectors of floats.
Definition: float_dsp.c:124
M_LN10
#define M_LN10
Definition: mathematics.h:49
lsf2lsp
static void lsf2lsp(const float *lsf, double *lsp)
Convert an lsf vector into an lsp vector.
Definition: sipr16k.c:45
SiprParameters::gp_index
int gp_index[5]
adaptive-codebook gain indexes
Definition: sipr.h:59
sipr16kdata.h
ff_acelp_lspd2lpc
void ff_acelp_lspd2lpc(const double *lsp, float *lpc, int lp_half_order)
Reconstruct LPC coefficients from the line spectral pair frequencies.
Definition: lsp.c:220
ff_acelp_interpolatef
void ff_acelp_interpolatef(float *out, const float *in, const float *filter_coeffs, int precision, int frac_pos, int filter_length, int length)
Floating point version of ff_acelp_interpolate()
Definition: acelp_filters.c:80
gain_pitch_cb_16k
static const float gain_pitch_cb_16k[16]
Definition: sipr16kdata.h:41
ff_set_min_dist_lsf
void ff_set_min_dist_lsf(float *lsf, double min_spacing, int size)
Adjust the quantized LSFs so they are increasing and not too close.
Definition: lsp.c:55
acelp_pitch_delay.h