Libav
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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 "celp_filters.h"
33 #include "acelp_vectors.h"
34 #include "acelp_pitch_delay.h"
35 #include "acelp_filters.h"
36 #include "celp_filters.h"
37 
38 #include "sipr16kdata.h"
39 
46 static void lsf2lsp(const float *lsf, double *lsp)
47 {
48  int i;
49 
50  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
51  lsp[i] = cosf(lsf[i]);
52 }
53 
54 static void dequant(float *out, const int *idx, const float *cbs[])
55 {
56  int i;
57 
58  for (i = 0; i < 4; i++)
59  memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
60 
61  memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
62 }
63 
64 static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
65  const int* parm, int ma_pred)
66 {
67  int i;
68  float isp_q[LP_FILTER_ORDER_16k];
69 
70  dequant(isp_q, parm, lsf_codebooks_16k);
71 
72  for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
73  isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
74  + qu[ma_pred] * lsf_history[i]
75  + mean_lsf_16k[i];
76  }
77 
78  memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
79 }
80 
81 static int dec_delay3_1st(int index)
82 {
83  if (index < 390) {
84  return index + 88;
85  } else
86  return 3 * index - 690;
87 }
88 
89 static int dec_delay3_2nd(int index, int pit_min, int pit_max,
90  int pitch_lag_prev)
91 {
92  if (index < 62) {
93  int pitch_delay_min = av_clip(pitch_lag_prev - 10,
94  pit_min, pit_max - 19);
95  return 3 * pitch_delay_min + index - 2;
96  } else
97  return 3 * pitch_lag_prev;
98 }
99 
100 static void postfilter(float *out_data, float* synth, float* iir_mem,
101  float* filt_mem[2], float* mem_preemph)
102 {
103  float buf[30 + LP_FILTER_ORDER_16k];
104  float *tmpbuf = buf + LP_FILTER_ORDER_16k;
105  float s;
106  int i;
107 
108  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
109  filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
110 
111  memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
112  LP_FILTER_ORDER_16k*sizeof(*buf));
113 
114  ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30,
115  LP_FILTER_ORDER_16k);
116 
117  memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph,
118  LP_FILTER_ORDER_16k * sizeof(*synth));
119 
120  ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 30,
121  LP_FILTER_ORDER_16k);
122 
123  memcpy(out_data + 30 - LP_FILTER_ORDER_16k,
124  synth + 30 - LP_FILTER_ORDER_16k,
125  LP_FILTER_ORDER_16k * sizeof(*synth));
126 
127  ff_celp_lp_synthesis_filterf(out_data + 30, filt_mem[0],
128  synth + 30, 2 * L_SUBFR_16k - 30,
129  LP_FILTER_ORDER_16k);
130 
131 
132  memcpy(mem_preemph, out_data + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
133  LP_FILTER_ORDER_16k * sizeof(*synth));
134 
135  FFSWAP(float *, filt_mem[0], filt_mem[1]);
136  for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
137  out_data[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
138 }
139 
143 static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
144  const double *lsp_2nd, const double *lsp_prev)
145 {
146  double lsp_1st[LP_FILTER_ORDER_16k];
147  int i;
148 
149  /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */
150  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
151  lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
152 
153  ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
154 
155  /* LSP values for second subframe (3.2.5 of G.729) */
156  ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
157 }
158 
162 static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
163  float mr_energy, const float *quant_energy,
164  const float *ma_prediction_coeff,
165  int subframe_size, int ma_pred_order)
166 {
167  mr_energy += avpriv_scalarproduct_float_c(quant_energy, ma_prediction_coeff,
168  ma_pred_order);
169 
170  mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
171  sqrt((0.01 + avpriv_scalarproduct_float_c(fc_v, fc_v, subframe_size)));
172  return mr_energy;
173 }
174 
175 #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
176 
178  float *out_data)
179 {
181  float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
182  float lsf_new[LP_FILTER_ORDER_16k];
183  double lsp_new[LP_FILTER_ORDER_16k];
184  float Az[2][LP_FILTER_ORDER_16k];
185  float fixed_vector[L_SUBFR_16k];
186  float pitch_fac, gain_code;
187 
188  int i;
189  int pitch_delay_3x;
190 
191  float *excitation = ctx->excitation + 292;
192 
193  lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
194  params->ma_pred_switch);
195 
197 
198  lsf2lsp(lsf_new, lsp_new);
199 
200  acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
201 
202  memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
203 
204  memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
205  LP_FILTER_ORDER_16k * sizeof(*synth));
206 
207  for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
208  int i_subfr = i * L_SUBFR_16k;
209  AMRFixed f;
210  float gain_corr_factor;
211  int pitch_delay_int;
212  int pitch_delay_frac;
213 
214  if (!i) {
215  pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
216  } else
217  pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
219  ctx->pitch_lag_prev);
220 
221  pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
222  f.pitch_fac = FFMIN(pitch_fac, 1.0);
223  f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
224  ctx->pitch_lag_prev = f.pitch_lag;
225 
226  pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
227  pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
228 
229  ff_acelp_interpolatef(&excitation[i_subfr],
230  &excitation[i_subfr] - pitch_delay_int + 1,
231  sinc_win, 3, pitch_delay_frac + 1,
232  LP_FILTER_ORDER, L_SUBFR_16k);
233 
234 
235  memset(fixed_vector, 0, sizeof(fixed_vector));
236 
237  ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
239 
240  ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
241 
242  gain_corr_factor = gain_cb_16k[params->gc_index[i]];
243  gain_code = gain_corr_factor *
244  acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
245  19.0 - 15.0/(0.05*M_LN10/M_LN2),
246  pred_16k, ctx->energy_history,
247  L_SUBFR_16k, 2);
248 
249  ctx->energy_history[1] = ctx->energy_history[0];
250  ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
251 
252  ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
253  fixed_vector, pitch_fac,
254  gain_code, L_SUBFR_16k);
255 
256  ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
257  &excitation[i_subfr], L_SUBFR_16k,
259 
260  }
261  memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
262  LP_FILTER_ORDER_16k * sizeof(*synth));
263 
264  memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
265  (L_INTERPOL+PITCH_MAX) * sizeof(float));
266 
267  postfilter(out_data, synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
268 
269  memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
270 }
271 
273 {
274  int i;
275 
276  for (i = 0; i < LP_FILTER_ORDER_16k; i++)
277  ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
278 
279  ctx->filt_mem[0] = ctx->filt_buf[0];
280  ctx->filt_mem[1] = ctx->filt_buf[1];
281 
282  ctx->pitch_lag_prev = 180;
283 }
void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params, float *out_data)
Definition: sipr16k.c:177
int gp_index[5]
adaptive-codebook gain indexes
Definition: sipr.h:60
int pitch_delay[5]
pitch delay
Definition: sipr.h:59
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:83
int vq_indexes[5]
Definition: sipr.h:58
int pitch_lag_prev
Definition: sipr.h:90
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...
static const float pred_16k[2]
Definition: sipr16kdata.h:27
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.
double lsp_history_16k[16]
Definition: sipr.h:96
float iir_mem[LP_FILTER_ORDER_16k+1]
Definition: sipr.h:91
#define PITCH_MAX
Definition: g723_1_data.h:40
#define SUBFRAME_COUNT_16k
Definition: sipr.h:46
void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size)
Add fixed vector to an array from a sparse representation.
float pitch_fac
Definition: acelp_vectors.h:35
static const float gain_pitch_cb_16k[16]
Definition: sipr16kdata.h:41
float synth_buf[LP_FILTER_ORDER+5 *SUBFR_SIZE+6]
Definition: sipr.h:75
Macro definitions for various function/variable attributes.
static const float mean_lsf_16k[16]
Definition: sipr16kdata.h:47
float mem_preemph[LP_FILTER_ORDER_16k]
Definition: sipr.h:94
#define LSFQ_DIFF_MIN
Definition: sipr.h:36
float * filt_mem[2]
Definition: sipr.h:93
float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
Return the scalar product of two vectors.
Definition: float_dsp.c:104
float lsf_history[LP_FILTER_ORDER_16k]
Definition: sipr.h:71
#define av_cold
Definition: attributes.h:66
Sparse representation for the algebraic codebook (fixed) vector.
Definition: acelp_vectors.h:29
static void postfilter(float *out_data, float *synth, float *iir_mem, float *filt_mem[2], float *mem_preemph)
Definition: sipr16k.c:100
static int dec_delay3_2nd(int index, int pit_min, int pit_max, int pitch_lag_prev)
Definition: sipr16k.c:89
#define DIVIDE_BY_3(x)
Definition: sipr16k.c:175
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define cosf(x)
Definition: libm.h:56
#define L_INTERPOL
Number of past samples needed for excitation interpolation.
Definition: sipr.h:41
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
av_cold void ff_sipr_init_16k(SiprContext *ctx)
Definition: sipr16k.c:272
#define LP_FILTER_ORDER
linear predictive coding filter order
Definition: amrnbdata.h:53
static const float qu[2]
Definition: sipr16kdata.h:28
static void lsf_decode_fp_16k(float *lsf_history, float *isp_new, const int *parm, int ma_pred)
Definition: sipr16k.c:64
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:162
#define FFMIN(a, b)
Definition: common.h:57
static int dec_delay3_1st(int index)
Definition: sipr16k.c:81
#define L_SUBFR_16k
Definition: sipr.h:32
#define PITCH_MIN
Definition: g723_1_data.h:39
static const float * lsf_codebooks_16k[]
Definition: sipr16kdata.h:528
int16_t fc_indexes[5][10]
fixed-codebook indexes
Definition: sipr.h:61
float synth[LP_FILTER_ORDER_16k]
Definition: sipr.h:95
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:143
float energy_history[4]
Definition: sipr.h:79
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:201
const uint8_t ff_fc_4pulses_8bits_tracks_13[16]
Track|Pulse| Positions 1 | 0 | 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75 2 | 1 | 1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76 3 | 2 | 2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77
Definition: acelp_vectors.c:53
int index
Definition: gxfenc.c:72
static void dequant(float *out, const int *idx, const float *cbs[])
Definition: sipr16k.c:54
static const float gain_cb_16k[32]
Definition: sipr16kdata.h:30
common internal and external API header
int gc_index[5]
fixed-codebook gain indexes
Definition: sipr.h:62
int pitch_lag
Definition: acelp_vectors.h:34
float excitation[L_INTERPOL+PITCH_MAX+2 *L_SUBFR_16k]
Definition: sipr.h:73
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:49
#define LP_FILTER_ORDER_16k
Definition: sipr.h:31
const float ff_pow_0_5[]
Definition: sipr.c:135
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:77
static void lsf2lsp(const float *lsf, double *lsp)
Convert an lsf vector into an lsp vector.
Definition: sipr16k.c:46
static const float sinc_win[40]
Hamming windowed sinc function, like in AMR.
Definition: sipr16kdata.h:57
int ma_pred_switch
switched moving average predictor
Definition: sipr.h:57
#define log10f(x)
Definition: libm.h:121
#define FFSWAP(type, a, b)
Definition: common.h:60
float filt_buf[2][LP_FILTER_ORDER_16k+1]
Definition: sipr.h:92