2 * SIPR / ACELP.NET decoder
4 * Copyright (c) 2008 Vladimir Voroshilov
5 * Copyright (c) 2009 Vitor Sessak
7 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/mathematics.h"
29 #define ALT_BITSTREAM_READER_LE
34 #include "celp_math.h"
35 #include "acelp_vectors.h"
36 #include "acelp_pitch_delay.h"
37 #include "acelp_filters.h"
38 #include "celp_filters.h"
40 #define LSFQ_DIFF_MIN (0.0125 * M_PI)
42 #define LP_FILTER_ORDER 10
44 /** Number of past samples needed for excitation interpolation */
45 #define L_INTERPOL (LP_FILTER_ORDER + 1)
47 /** Subframe size for all modes except 16k */
50 #define MAX_SUBFRAME_COUNT 5
63 const char *mode_name;
64 uint16_t bits_per_frame;
65 uint8_t subframe_count;
66 uint8_t frames_per_packet;
67 float pitch_sharp_factor;
69 /* bitstream parameters */
70 uint8_t number_of_fc_indexes;
72 /** size in bits of the i-th stage vector of quantizer */
73 uint8_t vq_indexes_bits[5];
75 /** size in bits of the adaptive-codebook index for every subframe */
76 uint8_t pitch_delay_bits[5];
78 uint8_t gp_index_bits;
79 uint8_t fc_index_bits[10]; ///< size in bits of the fixed codebook indexes
80 uint8_t gc_index_bits; ///< size in bits of the gain codebook indexes
83 static const SiprModeParam modes[MODE_COUNT] = {
86 .bits_per_frame = 152,
88 .frames_per_packet = 1,
89 .pitch_sharp_factor = 0.8,
91 .number_of_fc_indexes = 3,
92 .vq_indexes_bits = {6, 7, 7, 7, 5},
93 .pitch_delay_bits = {8, 5, 5},
95 .fc_index_bits = {9, 9, 9},
101 .bits_per_frame = 232,
103 .frames_per_packet = 2,
104 .pitch_sharp_factor = 0.8,
106 .number_of_fc_indexes = 3,
107 .vq_indexes_bits = {6, 7, 7, 7, 5},
108 .pitch_delay_bits = {8, 5, 5},
110 .fc_index_bits = {5, 5, 5},
116 .bits_per_frame = 296,
118 .frames_per_packet = 2,
119 .pitch_sharp_factor = 0.85,
121 .number_of_fc_indexes = 1,
122 .vq_indexes_bits = {6, 7, 7, 7, 5},
123 .pitch_delay_bits = {8, 5, 8, 5, 5},
125 .fc_index_bits = {10},
131 AVCodecContext *avctx;
136 float past_pitch_gain;
137 float lsf_history[LP_FILTER_ORDER];
139 float excitation[L_INTERPOL + PITCH_DELAY_MAX + 5*SUBFR_SIZE];
141 DECLARE_ALIGNED_16(float, synth_buf[LP_FILTER_ORDER + 5*SUBFR_SIZE + 6]);
143 float lsp_history[LP_FILTER_ORDER];
145 float energy_history[4];
146 float highpass_filt_mem[2];
147 float postfilter_mem[PITCH_DELAY_MAX + LP_FILTER_ORDER];
151 float postfilter_agc;
152 float postfilter_mem5k0[PITCH_DELAY_MAX + LP_FILTER_ORDER];
153 float postfilter_syn5k0[LP_FILTER_ORDER + SUBFR_SIZE*5];
158 int pitch_delay[5]; ///< pitch delay
159 int gp_index[5]; ///< adaptive-codebook gain indexes
160 int16_t fc_indexes[5][10]; ///< fixed-codebook indexes
161 int gc_index[5]; ///< fixed-codebook gain indexes
165 static void dequant(float *out, const int *idx, const float *cbs[])
171 for (i = 0; i < num_vec; i++)
172 memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float));
176 static void lsf_decode_fp(float *lsfnew, float *lsf_history,
177 const SiprParameters *parm)
180 float lsf_tmp[LP_FILTER_ORDER];
182 dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks);
184 for (i = 0; i < LP_FILTER_ORDER; i++)
185 lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i];
187 ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1);
189 /* Note that a minimum distance is not enforced between the last value and
190 the previous one, contrary to what is done in ff_acelp_reorder_lsf() */
191 ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1);
192 lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI);
194 memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history));
196 for (i = 0; i < LP_FILTER_ORDER - 1; i++)
197 lsfnew[i] = cos(lsfnew[i]);
198 lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI;
201 /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */
202 static void pitch_sharpening(int pitch_lag_int, float beta,
207 for (i = pitch_lag_int; i < SUBFR_SIZE; i++)
208 fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int];
212 * Extracts decoding parameters from the input bitstream.
213 * @param parms parameters structure
214 * @param pgb pointer to initialized GetBitContext structure
216 static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
217 const SiprModeParam *p)
221 for (i = 0; i < 5; i++)
222 parms->vq_indexes[i] = get_bits(pgb, p->vq_indexes_bits[i]);
224 for (i = 0; i < p->subframe_count; i++) {
225 parms->pitch_delay[i] = get_bits(pgb, p->pitch_delay_bits[i]);
226 parms->gp_index[i] = get_bits(pgb, p->gp_index_bits);
228 for (j = 0; j < p->number_of_fc_indexes; j++)
229 parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]);
231 parms->gc_index[i] = get_bits(pgb, p->gc_index_bits);
235 static void lsp2lpc_sipr(const double *lsp, float *Az)
237 int lp_half_order = LP_FILTER_ORDER >> 1;
238 double buf[(LP_FILTER_ORDER >> 1) + 1];
239 double pa[(LP_FILTER_ORDER >> 1) + 1];
240 double *qa = buf + 1;
245 ff_lsp2polyf(lsp , pa, lp_half_order );
246 ff_lsp2polyf(lsp + 1, qa, lp_half_order - 1);
248 for (i = 1, j = LP_FILTER_ORDER - 1; i < lp_half_order; i++, j--) {
249 double paf = pa[i] * (1 + lsp[LP_FILTER_ORDER - 1]);
250 double qaf = (qa[i] - qa[i-2]) * (1 - lsp[LP_FILTER_ORDER - 1]);
251 Az[i-1] = (paf + qaf) * 0.5;
252 Az[j-1] = (paf - qaf) * 0.5;
255 Az[lp_half_order - 1] = (1.0 + lsp[LP_FILTER_ORDER - 1]) *
256 pa[lp_half_order] * 0.5;
258 Az[LP_FILTER_ORDER - 1] = lsp[LP_FILTER_ORDER - 1];
261 static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az,
264 double lsfint[LP_FILTER_ORDER];
266 float t, t0 = 1.0 / num_subfr;
269 for (i = 0; i < num_subfr; i++) {
270 for (j = 0; j < LP_FILTER_ORDER; j++)
271 lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
273 lsp2lpc_sipr(lsfint, Az);
274 Az += LP_FILTER_ORDER;
280 * Evaluates the adaptative impulse response.
282 static void eval_ir(const float *Az, int pitch_lag, float *freq,
283 float pitch_sharp_factor)
285 float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1];
289 for (i = 0; i < LP_FILTER_ORDER; i++) {
290 tmp1[i+1] = Az[i] * ff_pow_0_55[i];
291 tmp2[i ] = Az[i] * ff_pow_0_7 [i];
293 memset(tmp1 + 11, 0, 37 * sizeof(float));
295 ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE,
298 pitch_sharpening(pitch_lag, pitch_sharp_factor, freq);
302 * Evaluates the convolution of a vector with a sparse vector.
304 static void convolute_with_sparse(float *out, const AMRFixed *pulses,
305 const float *shape, int length)
309 memset(out, 0, length*sizeof(float));
310 for (i = 0; i < pulses->n; i++)
311 for (j = pulses->x[i]; j < length; j++)
312 out[j] += pulses->y[i] * shape[j - pulses->x[i]];
316 * Apply postfilter, very similar to AMR one.
318 static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples)
320 float buf[SUBFR_SIZE + LP_FILTER_ORDER];
321 float *pole_out = buf + LP_FILTER_ORDER;
322 float lpc_n[LP_FILTER_ORDER];
323 float lpc_d[LP_FILTER_ORDER];
326 for (i = 0; i < LP_FILTER_ORDER; i++) {
327 lpc_d[i] = lpc[i] * ff_pow_0_75[i];
328 lpc_n[i] = lpc[i] * pow_0_5 [i];
331 memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem,
332 LP_FILTER_ORDER*sizeof(float));
334 ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE,
337 memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
338 LP_FILTER_ORDER*sizeof(float));
340 ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE);
342 memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0,
343 LP_FILTER_ORDER*sizeof(*pole_out));
345 memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
346 LP_FILTER_ORDER*sizeof(*pole_out));
348 ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE,
353 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses,
354 SiprMode mode, int low_gain)
360 for (i = 0; i < 3; i++) {
361 fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i;
362 fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1;
367 for (i = 0; i < 3; i++) {
368 fixed_sparse->x[2*i ] = 3 * ((pulses[i] >> 4) & 0xf) + i;
369 fixed_sparse->x[2*i + 1] = 3 * ( pulses[i] & 0xf) + i;
371 fixed_sparse->y[2*i ] = (pulses[i] & 0x100) ? -1.0: 1.0;
373 fixed_sparse->y[2*i + 1] =
374 (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ?
375 -fixed_sparse->y[2*i ] : fixed_sparse->y[2*i];
383 int offset = (pulses[0] & 0x200) ? 2 : 0;
386 for (i = 0; i < 3; i++) {
387 int index = (val & 0x7) * 6 + 4 - i*2;
389 fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1;
390 fixed_sparse->x[i] = index;
396 int pulse_subset = (pulses[0] >> 8) & 1;
398 fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset;
399 fixed_sparse->x[1] = ( pulses[0] & 15) * 3 + pulse_subset + 1;
401 fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1;
402 fixed_sparse->y[1] = -fixed_sparse->y[0];
409 static void decode_frame(SiprContext *ctx, SiprParameters *params,
413 int subframe_count = modes[ctx->mode].subframe_count;
414 int frame_size = subframe_count * SUBFR_SIZE;
415 float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT];
417 float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER];
418 float lsf_new[LP_FILTER_ORDER];
419 float *impulse_response = ir_buf + LP_FILTER_ORDER;
420 float *synth = ctx->synth_buf + 16; // 16 instead of LP_FILTER_ORDER for
425 memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
426 lsf_decode_fp(lsf_new, ctx->lsf_history, params);
428 sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count);
430 memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
432 excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
434 for (i = 0; i < subframe_count; i++) {
435 float *pAz = Az + i*LP_FILTER_ORDER;
436 float fixed_vector[SUBFR_SIZE];
438 float pitch_gain, gain_code, avg_energy;
440 ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i,
441 ctx->mode == MODE_5k0, 6);
443 if (i == 0 || (i == 2 && ctx->mode == MODE_5k0))
446 ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0),
448 2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER,
451 decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode,
452 ctx->past_pitch_gain < 0.8);
454 eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor);
456 convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
460 (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/
463 ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0];
465 gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1],
466 avg_energy, ctx->energy_history,
467 34 - 15.0/(0.05*M_LN10/M_LN2),
470 ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
471 pitch_gain, gain_code, SUBFR_SIZE);
473 pitch_gain *= 0.5 * pitch_gain;
474 pitch_gain = FFMIN(pitch_gain, 0.4);
476 ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain;
477 ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain);
478 gain_code *= ctx->gain_mem;
480 for (j = 0; j < SUBFR_SIZE; j++)
481 fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
483 if (ctx->mode == MODE_5k0) {
484 postfilter_5k0(ctx, pAz, fixed_vector);
486 ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
487 pAz, excitation, SUBFR_SIZE,
491 ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector,
492 SUBFR_SIZE, LP_FILTER_ORDER);
494 excitation += SUBFR_SIZE;
497 memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
498 LP_FILTER_ORDER * sizeof(float));
500 if (ctx->mode == MODE_5k0) {
501 for (i = 0; i < subframe_count; i++) {
502 float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
503 ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
505 ff_adaptative_gain_control(&synth[i * SUBFR_SIZE], energy,
506 SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
509 memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size,
510 LP_FILTER_ORDER*sizeof(float));
512 memcpy(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL,
513 (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float));
515 ff_acelp_apply_order_2_transfer_function(synth,
516 (const float[2]) {-1.99997 , 1.000000000},
517 (const float[2]) {-1.93307352, 0.935891986},
519 ctx->highpass_filt_mem,
522 ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size);
526 static av_cold int sipr_decoder_init(AVCodecContext * avctx)
528 SiprContext *ctx = avctx->priv_data;
531 if (avctx->bit_rate > 12200) ctx->mode = MODE_16k;
532 else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5;
533 else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5;
534 else ctx->mode = MODE_5k0;
536 av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name);
538 for (i = 0; i < LP_FILTER_ORDER; i++)
539 ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1));
541 for (i = 0; i < 4; i++)
542 ctx->energy_history[i] = -14;
544 avctx->sample_fmt = SAMPLE_FMT_FLT;
546 if (ctx->mode == MODE_16k) {
547 av_log(avctx, AV_LOG_ERROR, "decoding 16kbps SIPR files is not "
552 dsputil_init(&ctx->dsp, avctx);
557 static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
558 int *data_size, AVPacket *avpkt)
560 SiprContext *ctx = avctx->priv_data;
561 const uint8_t *buf=avpkt->data;
563 const SiprModeParam *mode_par = &modes[ctx->mode];
569 if (avpkt->size < (mode_par->bits_per_frame >> 3)) {
570 av_log(avctx, AV_LOG_ERROR,
571 "Error processing packet: packet size (%d) too small\n",
577 if (*data_size < SUBFR_SIZE * mode_par->subframe_count * sizeof(float)) {
578 av_log(avctx, AV_LOG_ERROR,
579 "Error processing packet: output buffer (%d) too small\n",
586 init_get_bits(&gb, buf, mode_par->bits_per_frame);
588 for (i = 0; i < mode_par->frames_per_packet; i++) {
589 decode_parameters(&parm, &gb, mode_par);
590 decode_frame(ctx, &parm, data);
592 data += SUBFR_SIZE * mode_par->subframe_count;
595 *data_size = mode_par->frames_per_packet * SUBFR_SIZE *
596 mode_par->subframe_count * sizeof(float);
598 return mode_par->bits_per_frame >> 3;
601 AVCodec sipr_decoder = {
610 .long_name = NULL_IF_CONFIG_SMALL("RealAudio SIPR / ACELP.NET"),