]> git.sesse.net Git - ffmpeg/blob - libavcodec/sipr.c
vorbisdec: Rename silly "class_" variable to plain "class".
[ffmpeg] / libavcodec / sipr.c
1 /*
2  * SIPR / ACELP.NET decoder
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 #include <stdint.h>
26
27 #include "libavutil/mathematics.h"
28 #include "avcodec.h"
29 #define ALT_BITSTREAM_READER_LE
30 #include "get_bits.h"
31 #include "dsputil.h"
32
33 #include "lsp.h"
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"
39
40 #define MAX_SUBFRAME_COUNT   5
41
42 #include "sipr.h"
43 #include "siprdata.h"
44
45 typedef struct {
46     const char *mode_name;
47     uint16_t bits_per_frame;
48     uint8_t subframe_count;
49     uint8_t frames_per_packet;
50     float pitch_sharp_factor;
51
52     /* bitstream parameters */
53     uint8_t number_of_fc_indexes;
54     uint8_t ma_predictor_bits;  ///< size in bits of the switched MA predictor
55
56     /** size in bits of the i-th stage vector of quantizer */
57     uint8_t vq_indexes_bits[5];
58
59     /** size in bits of the adaptive-codebook index for every subframe */
60     uint8_t pitch_delay_bits[5];
61
62     uint8_t gp_index_bits;
63     uint8_t fc_index_bits[10]; ///< size in bits of the fixed codebook indexes
64     uint8_t gc_index_bits;     ///< size in bits of the gain  codebook indexes
65 } SiprModeParam;
66
67 static const SiprModeParam modes[MODE_COUNT] = {
68     [MODE_16k] = {
69         .mode_name          = "16k",
70         .bits_per_frame     = 160,
71         .subframe_count     = SUBFRAME_COUNT_16k,
72         .frames_per_packet  = 1,
73         .pitch_sharp_factor = 0.00,
74
75         .number_of_fc_indexes = 10,
76         .ma_predictor_bits    = 1,
77         .vq_indexes_bits      = {7, 8, 7, 7, 7},
78         .pitch_delay_bits     = {9, 6},
79         .gp_index_bits        = 4,
80         .fc_index_bits        = {4, 5, 4, 5, 4, 5, 4, 5, 4, 5},
81         .gc_index_bits        = 5
82     },
83
84     [MODE_8k5] = {
85         .mode_name          = "8k5",
86         .bits_per_frame     = 152,
87         .subframe_count     = 3,
88         .frames_per_packet  = 1,
89         .pitch_sharp_factor = 0.8,
90
91         .number_of_fc_indexes = 3,
92         .ma_predictor_bits    = 0,
93         .vq_indexes_bits      = {6, 7, 7, 7, 5},
94         .pitch_delay_bits     = {8, 5, 5},
95         .gp_index_bits        = 0,
96         .fc_index_bits        = {9, 9, 9},
97         .gc_index_bits        = 7
98     },
99
100     [MODE_6k5] = {
101         .mode_name          = "6k5",
102         .bits_per_frame     = 232,
103         .subframe_count     = 3,
104         .frames_per_packet  = 2,
105         .pitch_sharp_factor = 0.8,
106
107         .number_of_fc_indexes = 3,
108         .ma_predictor_bits    = 0,
109         .vq_indexes_bits      = {6, 7, 7, 7, 5},
110         .pitch_delay_bits     = {8, 5, 5},
111         .gp_index_bits        = 0,
112         .fc_index_bits        = {5, 5, 5},
113         .gc_index_bits        = 7
114     },
115
116     [MODE_5k0] = {
117         .mode_name          = "5k0",
118         .bits_per_frame     = 296,
119         .subframe_count     = 5,
120         .frames_per_packet  = 2,
121         .pitch_sharp_factor = 0.85,
122
123         .number_of_fc_indexes = 1,
124         .ma_predictor_bits    = 0,
125         .vq_indexes_bits      = {6, 7, 7, 7, 5},
126         .pitch_delay_bits     = {8, 5, 8, 5, 5},
127         .gp_index_bits        = 0,
128         .fc_index_bits        = {10},
129         .gc_index_bits        = 7
130     }
131 };
132
133 const float ff_pow_0_5[] = {
134     1.0/(1 <<  1), 1.0/(1 <<  2), 1.0/(1 <<  3), 1.0/(1 <<  4),
135     1.0/(1 <<  5), 1.0/(1 <<  6), 1.0/(1 <<  7), 1.0/(1 <<  8),
136     1.0/(1 <<  9), 1.0/(1 << 10), 1.0/(1 << 11), 1.0/(1 << 12),
137     1.0/(1 << 13), 1.0/(1 << 14), 1.0/(1 << 15), 1.0/(1 << 16)
138 };
139
140 static void dequant(float *out, const int *idx, const float *cbs[])
141 {
142     int i;
143     int stride  = 2;
144     int num_vec = 5;
145
146     for (i = 0; i < num_vec; i++)
147         memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float));
148
149 }
150
151 static void lsf_decode_fp(float *lsfnew, float *lsf_history,
152                           const SiprParameters *parm)
153 {
154     int i;
155     float lsf_tmp[LP_FILTER_ORDER];
156
157     dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks);
158
159     for (i = 0; i < LP_FILTER_ORDER; i++)
160         lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i];
161
162     ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1);
163
164     /* Note that a minimum distance is not enforced between the last value and
165        the previous one, contrary to what is done in ff_acelp_reorder_lsf() */
166     ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1);
167     lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI);
168
169     memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history));
170
171     for (i = 0; i < LP_FILTER_ORDER - 1; i++)
172         lsfnew[i] = cos(lsfnew[i]);
173     lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI;
174 }
175
176 /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */
177 static void pitch_sharpening(int pitch_lag_int, float beta,
178                              float *fixed_vector)
179 {
180     int i;
181
182     for (i = pitch_lag_int; i < SUBFR_SIZE; i++)
183         fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int];
184 }
185
186 /**
187  * Extract decoding parameters from the input bitstream.
188  * @param parms          parameters structure
189  * @param pgb            pointer to initialized GetBitContext structure
190  */
191 static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
192                               const SiprModeParam *p)
193 {
194     int i, j;
195
196     parms->ma_pred_switch           = get_bits(pgb, p->ma_predictor_bits);
197
198     for (i = 0; i < 5; i++)
199         parms->vq_indexes[i]        = get_bits(pgb, p->vq_indexes_bits[i]);
200
201     for (i = 0; i < p->subframe_count; i++) {
202         parms->pitch_delay[i]       = get_bits(pgb, p->pitch_delay_bits[i]);
203         parms->gp_index[i]          = get_bits(pgb, p->gp_index_bits);
204
205         for (j = 0; j < p->number_of_fc_indexes; j++)
206             parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]);
207
208         parms->gc_index[i]          = get_bits(pgb, p->gc_index_bits);
209     }
210 }
211
212 static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az,
213                            int num_subfr)
214 {
215     double lsfint[LP_FILTER_ORDER];
216     int i,j;
217     float t, t0 = 1.0 / num_subfr;
218
219     t = t0 * 0.5;
220     for (i = 0; i < num_subfr; i++) {
221         for (j = 0; j < LP_FILTER_ORDER; j++)
222             lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
223
224         ff_amrwb_lsp2lpc(lsfint, Az, LP_FILTER_ORDER);
225         Az += LP_FILTER_ORDER;
226         t += t0;
227     }
228 }
229
230 /**
231  * Evaluate the adaptive impulse response.
232  */
233 static void eval_ir(const float *Az, int pitch_lag, float *freq,
234                     float pitch_sharp_factor)
235 {
236     float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1];
237     int i;
238
239     tmp1[0] = 1.;
240     for (i = 0; i < LP_FILTER_ORDER; i++) {
241         tmp1[i+1] = Az[i] * ff_pow_0_55[i];
242         tmp2[i  ] = Az[i] * ff_pow_0_7 [i];
243     }
244     memset(tmp1 + 11, 0, 37 * sizeof(float));
245
246     ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE,
247                                  LP_FILTER_ORDER);
248
249     pitch_sharpening(pitch_lag, pitch_sharp_factor, freq);
250 }
251
252 /**
253  * Evaluate the convolution of a vector with a sparse vector.
254  */
255 static void convolute_with_sparse(float *out, const AMRFixed *pulses,
256                                   const float *shape, int length)
257 {
258     int i, j;
259
260     memset(out, 0, length*sizeof(float));
261     for (i = 0; i < pulses->n; i++)
262         for (j = pulses->x[i]; j < length; j++)
263             out[j] += pulses->y[i] * shape[j - pulses->x[i]];
264 }
265
266 /**
267  * Apply postfilter, very similar to AMR one.
268  */
269 static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples)
270 {
271     float buf[SUBFR_SIZE + LP_FILTER_ORDER];
272     float *pole_out = buf + LP_FILTER_ORDER;
273     float lpc_n[LP_FILTER_ORDER];
274     float lpc_d[LP_FILTER_ORDER];
275     int i;
276
277     for (i = 0; i < LP_FILTER_ORDER; i++) {
278         lpc_d[i] = lpc[i] * ff_pow_0_75[i];
279         lpc_n[i] = lpc[i] * ff_pow_0_5 [i];
280     };
281
282     memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem,
283            LP_FILTER_ORDER*sizeof(float));
284
285     ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE,
286                                  LP_FILTER_ORDER);
287
288     memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
289            LP_FILTER_ORDER*sizeof(float));
290
291     ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE);
292
293     memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0,
294            LP_FILTER_ORDER*sizeof(*pole_out));
295
296     memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
297            LP_FILTER_ORDER*sizeof(*pole_out));
298
299     ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE,
300                                       LP_FILTER_ORDER);
301
302 }
303
304 static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses,
305                                 SiprMode mode, int low_gain)
306 {
307     int i;
308
309     switch (mode) {
310     case MODE_6k5:
311         for (i = 0; i < 3; i++) {
312             fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i;
313             fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1;
314         }
315         fixed_sparse->n = 3;
316         break;
317     case MODE_8k5:
318         for (i = 0; i < 3; i++) {
319             fixed_sparse->x[2*i    ] = 3 * ((pulses[i] >> 4) & 0xf) + i;
320             fixed_sparse->x[2*i + 1] = 3 * ( pulses[i]       & 0xf) + i;
321
322             fixed_sparse->y[2*i    ] = (pulses[i] & 0x100) ? -1.0: 1.0;
323
324             fixed_sparse->y[2*i + 1] =
325                 (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ?
326                 -fixed_sparse->y[2*i    ] : fixed_sparse->y[2*i];
327         }
328
329         fixed_sparse->n = 6;
330         break;
331     case MODE_5k0:
332     default:
333         if (low_gain) {
334             int offset = (pulses[0] & 0x200) ? 2 : 0;
335             int val = pulses[0];
336
337             for (i = 0; i < 3; i++) {
338                 int index = (val & 0x7) * 6 + 4 - i*2;
339
340                 fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1;
341                 fixed_sparse->x[i] = index;
342
343                 val >>= 3;
344             }
345             fixed_sparse->n = 3;
346         } else {
347             int pulse_subset = (pulses[0] >> 8) & 1;
348
349             fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset;
350             fixed_sparse->x[1] = ( pulses[0]       & 15) * 3 + pulse_subset + 1;
351
352             fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1;
353             fixed_sparse->y[1] = -fixed_sparse->y[0];
354             fixed_sparse->n = 2;
355         }
356         break;
357     }
358 }
359
360 static void decode_frame(SiprContext *ctx, SiprParameters *params,
361                          float *out_data)
362 {
363     int i, j;
364     int subframe_count = modes[ctx->mode].subframe_count;
365     int frame_size = subframe_count * SUBFR_SIZE;
366     float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT];
367     float *excitation;
368     float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER];
369     float lsf_new[LP_FILTER_ORDER];
370     float *impulse_response = ir_buf + LP_FILTER_ORDER;
371     float *synth = ctx->synth_buf + 16; // 16 instead of LP_FILTER_ORDER for
372                                         // memory alignment
373     int t0_first = 0;
374     AMRFixed fixed_cb;
375
376     memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
377     lsf_decode_fp(lsf_new, ctx->lsf_history, params);
378
379     sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count);
380
381     memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
382
383     excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
384
385     for (i = 0; i < subframe_count; i++) {
386         float *pAz = Az + i*LP_FILTER_ORDER;
387         float fixed_vector[SUBFR_SIZE];
388         int T0,T0_frac;
389         float pitch_gain, gain_code, avg_energy;
390
391         ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i,
392                             ctx->mode == MODE_5k0, 6);
393
394         if (i == 0 || (i == 2 && ctx->mode == MODE_5k0))
395             t0_first = T0;
396
397         ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0),
398                               ff_b60_sinc, 6,
399                               2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER,
400                               SUBFR_SIZE);
401
402         decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode,
403                             ctx->past_pitch_gain < 0.8);
404
405         eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor);
406
407         convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
408                               SUBFR_SIZE);
409
410         avg_energy =
411             (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/
412                 SUBFR_SIZE;
413
414         ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0];
415
416         gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1],
417                                           avg_energy, ctx->energy_history,
418                                           34 - 15.0/(0.05*M_LN10/M_LN2),
419                                           pred);
420
421         ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
422                                 pitch_gain, gain_code, SUBFR_SIZE);
423
424         pitch_gain *= 0.5 * pitch_gain;
425         pitch_gain = FFMIN(pitch_gain, 0.4);
426
427         ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain;
428         ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain);
429         gain_code *= ctx->gain_mem;
430
431         for (j = 0; j < SUBFR_SIZE; j++)
432             fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
433
434         if (ctx->mode == MODE_5k0) {
435             postfilter_5k0(ctx, pAz, fixed_vector);
436
437             ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
438                                          pAz, excitation, SUBFR_SIZE,
439                                          LP_FILTER_ORDER);
440         }
441
442         ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector,
443                                      SUBFR_SIZE, LP_FILTER_ORDER);
444
445         excitation += SUBFR_SIZE;
446     }
447
448     memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
449            LP_FILTER_ORDER * sizeof(float));
450
451     if (ctx->mode == MODE_5k0) {
452         for (i = 0; i < subframe_count; i++) {
453             float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
454                                            ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
455                                            SUBFR_SIZE);
456             ff_adaptive_gain_control(&synth[i * SUBFR_SIZE],
457                                      &synth[i * SUBFR_SIZE], energy,
458                                      SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
459         }
460
461         memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size,
462                LP_FILTER_ORDER*sizeof(float));
463     }
464     memcpy(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL,
465            (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float));
466
467     ff_acelp_apply_order_2_transfer_function(out_data, synth,
468                                              (const float[2]) {-1.99997   , 1.000000000},
469                                              (const float[2]) {-1.93307352, 0.935891986},
470                                              0.939805806,
471                                              ctx->highpass_filt_mem,
472                                              frame_size);
473 }
474
475 static av_cold int sipr_decoder_init(AVCodecContext * avctx)
476 {
477     SiprContext *ctx = avctx->priv_data;
478     int i;
479
480     if      (avctx->bit_rate > 12200) ctx->mode = MODE_16k;
481     else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5;
482     else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5;
483     else                              ctx->mode = MODE_5k0;
484
485     av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name);
486
487     if (ctx->mode == MODE_16k)
488         ff_sipr_init_16k(ctx);
489
490     for (i = 0; i < LP_FILTER_ORDER; i++)
491         ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1));
492
493     for (i = 0; i < 4; i++)
494         ctx->energy_history[i] = -14;
495
496     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
497
498     dsputil_init(&ctx->dsp, avctx);
499
500     return 0;
501 }
502
503 static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
504                              int *data_size, AVPacket *avpkt)
505 {
506     SiprContext *ctx = avctx->priv_data;
507     const uint8_t *buf=avpkt->data;
508     SiprParameters parm;
509     const SiprModeParam *mode_par = &modes[ctx->mode];
510     GetBitContext gb;
511     float *data = datap;
512     int subframe_size = ctx->mode == MODE_16k ? L_SUBFR_16k : SUBFR_SIZE;
513     int i;
514
515     ctx->avctx = avctx;
516     if (avpkt->size < (mode_par->bits_per_frame >> 3)) {
517         av_log(avctx, AV_LOG_ERROR,
518                "Error processing packet: packet size (%d) too small\n",
519                avpkt->size);
520
521         *data_size = 0;
522         return -1;
523     }
524     if (*data_size < subframe_size * mode_par->subframe_count * sizeof(float)) {
525         av_log(avctx, AV_LOG_ERROR,
526                "Error processing packet: output buffer (%d) too small\n",
527                *data_size);
528
529         *data_size = 0;
530         return -1;
531     }
532
533     init_get_bits(&gb, buf, mode_par->bits_per_frame);
534
535     for (i = 0; i < mode_par->frames_per_packet; i++) {
536         decode_parameters(&parm, &gb, mode_par);
537
538         if (ctx->mode == MODE_16k)
539             ff_sipr_decode_frame_16k(ctx, &parm, data);
540         else
541             decode_frame(ctx, &parm, data);
542
543         data += subframe_size * mode_par->subframe_count;
544     }
545
546     *data_size = mode_par->frames_per_packet * subframe_size *
547         mode_par->subframe_count * sizeof(float);
548
549     return mode_par->bits_per_frame >> 3;
550 }
551
552 AVCodec ff_sipr_decoder = {
553     "sipr",
554     AVMEDIA_TYPE_AUDIO,
555     CODEC_ID_SIPR,
556     sizeof(SiprContext),
557     sipr_decoder_init,
558     NULL,
559     NULL,
560     sipr_decode_frame,
561     .long_name = NULL_IF_CONFIG_SMALL("RealAudio SIPR / ACELP.NET"),
562 };