Statistics
| Branch: | Revision:

ffmpeg / libavcodec / sipr.c @ 1c3c129b

History | View | Annotate | Download (18.9 KB)

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 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
#include <stdint.h>
26

    
27
#include "avcodec.h"
28
#define ALT_BITSTREAM_READER_LE
29
#include "get_bits.h"
30
#include "dsputil.h"
31

    
32
#include "lsp.h"
33
#include "celp_math.h"
34
#include "acelp_vectors.h"
35
#include "acelp_pitch_delay.h"
36
#include "acelp_filters.h"
37
#include "celp_filters.h"
38

    
39
#define LSFQ_DIFF_MIN        (0.0125 * M_PI)
40

    
41
#define LP_FILTER_ORDER      10
42

    
43
/** Number of past samples needed for excitation interpolation */
44
#define L_INTERPOL           (LP_FILTER_ORDER + 1)
45

    
46
/**  Subframe size for all modes except 16k */
47
#define SUBFR_SIZE           48
48

    
49
#define MAX_SUBFRAME_COUNT   5
50

    
51
#include "siprdata.h"
52

    
53
typedef enum {
54
    MODE_16k,
55
    MODE_8k5,
56
    MODE_6k5,
57
    MODE_5k0,
58
    MODE_COUNT
59
} SiprMode;
60

    
61
typedef struct {
62
    const char *mode_name;
63
    uint16_t bits_per_frame;
64
    uint8_t subframe_count;
65
    uint8_t frames_per_packet;
66
    float pitch_sharp_factor;
67

    
68
    /* bitstream parameters */
69
    uint8_t number_of_fc_indexes;
70

    
71
    /** size in bits of the i-th stage vector of quantizer */
72
    uint8_t vq_indexes_bits[5];
73

    
74
    /** size in bits of the adaptive-codebook index for every subframe */
75
    uint8_t pitch_delay_bits[5];
76

    
77
    uint8_t gp_index_bits;
78
    uint8_t fc_index_bits[10]; ///< size in bits of the fixed codebook indexes
79
    uint8_t gc_index_bits;     ///< size in bits of the gain  codebook indexes
80
} SiprModeParam;
81

    
82
static const SiprModeParam modes[MODE_COUNT] = {
83
    [MODE_8k5] = {
84
        .mode_name          = "8k5",
85
        .bits_per_frame     = 152,
86
        .subframe_count     = 3,
87
        .frames_per_packet  = 1,
88
        .pitch_sharp_factor = 0.8,
89

    
90
        .number_of_fc_indexes = 3,
91
        .vq_indexes_bits      = {6, 7, 7, 7, 5},
92
        .pitch_delay_bits     = {8, 5, 5},
93
        .gp_index_bits        = 0,
94
        .fc_index_bits        = {9, 9, 9},
95
        .gc_index_bits        = 7
96
    },
97

    
98
    [MODE_6k5] = {
99
        .mode_name          = "6k5",
100
        .bits_per_frame     = 232,
101
        .subframe_count     = 3,
102
        .frames_per_packet  = 2,
103
        .pitch_sharp_factor = 0.8,
104

    
105
        .number_of_fc_indexes = 3,
106
        .vq_indexes_bits      = {6, 7, 7, 7, 5},
107
        .pitch_delay_bits     = {8, 5, 5},
108
        .gp_index_bits        = 0,
109
        .fc_index_bits        = {5, 5, 5},
110
        .gc_index_bits        = 7
111
    },
112

    
113
    [MODE_5k0] = {
114
        .mode_name          = "5k0",
115
        .bits_per_frame     = 296,
116
        .subframe_count     = 5,
117
        .frames_per_packet  = 2,
118
        .pitch_sharp_factor = 0.85,
119

    
120
        .number_of_fc_indexes = 1,
121
        .vq_indexes_bits      = {6, 7, 7, 7, 5},
122
        .pitch_delay_bits     = {8, 5, 8, 5, 5},
123
        .gp_index_bits        = 0,
124
        .fc_index_bits        = {10},
125
        .gc_index_bits        = 7
126
    }
127
};
128

    
129
typedef struct {
130
    AVCodecContext *avctx;
131
    DSPContext dsp;
132

    
133
    SiprMode mode;
134

    
135
    float past_pitch_gain;
136
    float lsf_history[LP_FILTER_ORDER];
137

    
138
    float excitation[L_INTERPOL + PITCH_DELAY_MAX + 5*SUBFR_SIZE];
139

    
140
    DECLARE_ALIGNED_16(float, synth_buf[LP_FILTER_ORDER + 5*SUBFR_SIZE + 6]);
141

    
142
    float lsp_history[LP_FILTER_ORDER];
143
    float gain_mem;
144
    float energy_history[4];
145
    float highpass_filt_mem[2];
146
    float postfilter_mem[PITCH_DELAY_MAX + LP_FILTER_ORDER];
147

    
148
    /* 5k0 */
149
    float tilt_mem;
150
    float postfilter_agc;
151
    float postfilter_mem5k0[PITCH_DELAY_MAX + LP_FILTER_ORDER];
152
    float postfilter_syn5k0[LP_FILTER_ORDER + SUBFR_SIZE*5];
153
} SiprContext;
154

    
155
typedef struct {
156
    int vq_indexes[5];
157
    int pitch_delay[5];        ///< pitch delay
158
    int gp_index[5];           ///< adaptive-codebook gain indexes
159
    int16_t fc_indexes[5][10]; ///< fixed-codebook indexes
160
    int gc_index[5];           ///< fixed-codebook gain indexes
161
} SiprParameters;
162

    
163

    
164
static void dequant(float *out, const int *idx, const float *cbs[])
165
{
166
    int i;
167
    int stride  = 2;
168
    int num_vec = 5;
169

    
170
    for (i = 0; i < num_vec; i++)
171
        memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float));
172

    
173
}
174

    
175
static void lsf_decode_fp(float *lsfnew, float *lsf_history,
176
                          const SiprParameters *parm)
177
{
178
    int i;
179
    float lsf_tmp[LP_FILTER_ORDER];
180

    
181
    dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks);
182

    
183
    for (i = 0; i < LP_FILTER_ORDER; i++)
184
        lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i];
185

    
186
    ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1);
187

    
188
    /* Note that a minimum distance is not enforced between the last value and
189
       the previous one, contrary to what is done in ff_acelp_reorder_lsf() */
190
    ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1);
191
    lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI);
192

    
193
    memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history));
194

    
195
    for (i = 0; i < LP_FILTER_ORDER - 1; i++)
196
        lsfnew[i] = cos(lsfnew[i]);
197
    lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI;
198
}
199

    
200
/** Apply pitch lag to the fixed vector (AMR section 6.1.2). */
201
static void pitch_sharpening(int pitch_lag_int, float beta,
202
                             float *fixed_vector)
203
{
204
    int i;
205

    
206
    for (i = pitch_lag_int; i < SUBFR_SIZE; i++)
207
        fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int];
208
}
209

    
210
/**
211
 * Extracts decoding parameters from the input bitstream.
212
 * @param parms          parameters structure
213
 * @param pgb            pointer to initialized GetBitContext structure
214
 */
215
static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
216
                              const SiprModeParam *p)
217
{
218
    int i, j;
219

    
220
    for (i = 0; i < 5; i++)
221
        parms->vq_indexes[i]        = get_bits(pgb, p->vq_indexes_bits[i]);
222

    
223
    for (i = 0; i < p->subframe_count; i++) {
224
        parms->pitch_delay[i]       = get_bits(pgb, p->pitch_delay_bits[i]);
225
        parms->gp_index[i]          = get_bits(pgb, p->gp_index_bits);
226

    
227
        for (j = 0; j < p->number_of_fc_indexes; j++)
228
            parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]);
229

    
230
        parms->gc_index[i]          = get_bits(pgb, p->gc_index_bits);
231
    }
232
}
233

    
234
static void lsp2lpc_sipr(const double *lsp, float *Az)
235
{
236
    int lp_half_order = LP_FILTER_ORDER >> 1;
237
    double buf[(LP_FILTER_ORDER >> 1) + 1];
238
    double pa[(LP_FILTER_ORDER >> 1) + 1];
239
    double *qa = buf + 1;
240
    int i,j;
241

    
242
    qa[-1] = 0.0;
243

    
244
    ff_lsp2polyf(lsp    , pa, lp_half_order    );
245
    ff_lsp2polyf(lsp + 1, qa, lp_half_order - 1);
246

    
247
    for (i = 1, j = LP_FILTER_ORDER - 1; i < lp_half_order; i++, j--) {
248
        double paf =  pa[i]            * (1 + lsp[LP_FILTER_ORDER - 1]);
249
        double qaf = (qa[i] - qa[i-2]) * (1 - lsp[LP_FILTER_ORDER - 1]);
250
        Az[i-1]  = (paf + qaf) * 0.5;
251
        Az[j-1]  = (paf - qaf) * 0.5;
252
    }
253

    
254
    Az[lp_half_order - 1] = (1.0 + lsp[LP_FILTER_ORDER - 1]) *
255
        pa[lp_half_order] * 0.5;
256

    
257
    Az[LP_FILTER_ORDER - 1] = lsp[LP_FILTER_ORDER - 1];
258
}
259

    
260
static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az,
261
                           int num_subfr)
262
{
263
    double lsfint[LP_FILTER_ORDER];
264
    int i,j;
265
    float t, t0 = 1.0 / num_subfr;
266

    
267
    t = t0 * 0.5;
268
    for (i = 0; i < num_subfr; i++) {
269
        for (j = 0; j < LP_FILTER_ORDER; j++)
270
            lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
271

    
272
        lsp2lpc_sipr(lsfint, Az);
273
        Az += LP_FILTER_ORDER;
274
        t += t0;
275
    }
276
}
277

    
278
/**
279
 * Evaluates the adaptative impulse response.
280
 */
281
static void eval_ir(const float *Az, int pitch_lag, float *freq,
282
                    float pitch_sharp_factor)
283
{
284
    float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1];
285
    int i;
286

    
287
    tmp1[0] = 1.;
288
    for (i = 0; i < LP_FILTER_ORDER; i++) {
289
        tmp1[i+1] = Az[i] * ff_pow_0_55[i];
290
        tmp2[i  ] = Az[i] * ff_pow_0_7 [i];
291
    }
292
    memset(tmp1 + 11, 0, 37 * sizeof(float));
293

    
294
    ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE,
295
                                 LP_FILTER_ORDER);
296

    
297
    pitch_sharpening(pitch_lag, pitch_sharp_factor, freq);
298
}
299

    
300
/**
301
 * Evaluates the convolution of a vector with a sparse vector.
302
 */
303
static void convolute_with_sparse(float *out, const AMRFixed *pulses,
304
                                  const float *shape, int length)
305
{
306
    int i, j;
307

    
308
    memset(out, 0, length*sizeof(float));
309
    for (i = 0; i < pulses->n; i++)
310
        for (j = pulses->x[i]; j < length; j++)
311
            out[j] += pulses->y[i] * shape[j - pulses->x[i]];
312
}
313

    
314
/**
315
 * Apply postfilter, very similar to AMR one.
316
 */
317
static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples)
318
{
319
    float buf[SUBFR_SIZE + LP_FILTER_ORDER];
320
    float *pole_out = buf + LP_FILTER_ORDER;
321
    float lpc_n[LP_FILTER_ORDER];
322
    float lpc_d[LP_FILTER_ORDER];
323
    int i;
324

    
325
    for (i = 0; i < LP_FILTER_ORDER; i++) {
326
        lpc_d[i] = lpc[i] * ff_pow_0_75[i];
327
        lpc_n[i] = lpc[i] *    pow_0_5 [i];
328
    };
329

    
330
    memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem,
331
           LP_FILTER_ORDER*sizeof(float));
332

    
333
    ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE,
334
                                 LP_FILTER_ORDER);
335

    
336
    memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
337
           LP_FILTER_ORDER*sizeof(float));
338

    
339
    ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE);
340

    
341
    memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0,
342
           LP_FILTER_ORDER*sizeof(*pole_out));
343

    
344
    memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
345
           LP_FILTER_ORDER*sizeof(*pole_out));
346

    
347
    ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE,
348
                                      LP_FILTER_ORDER);
349

    
350
}
351

    
352
static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses,
353
                                SiprMode mode, int low_gain)
354
{
355
    int i;
356

    
357
    switch (mode) {
358
    case MODE_6k5:
359
        for (i = 0; i < 3; i++) {
360
            fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i;
361
            fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1;
362
        }
363
        fixed_sparse->n = 3;
364
        break;
365
    case MODE_8k5:
366
        for (i = 0; i < 3; i++) {
367
            fixed_sparse->x[2*i    ] = 3 * ((pulses[i] >> 4) & 0xf) + i;
368
            fixed_sparse->x[2*i + 1] = 3 * ( pulses[i]       & 0xf) + i;
369

    
370
            fixed_sparse->y[2*i    ] = (pulses[i] & 0x100) ? -1.0: 1.0;
371

    
372
            fixed_sparse->y[2*i + 1] =
373
                (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ?
374
                -fixed_sparse->y[2*i    ] : fixed_sparse->y[2*i];
375
        }
376

    
377
        fixed_sparse->n = 6;
378
        break;
379
    case MODE_5k0:
380
    default:
381
        if (low_gain) {
382
            int offset = (pulses[0] & 0x200) ? 2 : 0;
383
            int val = pulses[0];
384

    
385
            for (i = 0; i < 3; i++) {
386
                int index = (val & 0x7) * 6 + 4 - i*2;
387

    
388
                fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1;
389
                fixed_sparse->x[i] = index;
390

    
391
                val >>= 3;
392
            }
393
            fixed_sparse->n = 3;
394
        } else {
395
            int pulse_subset = (pulses[0] >> 8) & 1;
396

    
397
            fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset;
398
            fixed_sparse->x[1] = ( pulses[0]       & 15) * 3 + pulse_subset + 1;
399

    
400
            fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1;
401
            fixed_sparse->y[1] = -fixed_sparse->y[0];
402
            fixed_sparse->n = 2;
403
        }
404
        break;
405
    }
406
}
407

    
408
static void decode_frame(SiprContext *ctx, SiprParameters *params,
409
                         float *out_data)
410
{
411
    int i, j;
412
    int subframe_count = modes[ctx->mode].subframe_count;
413
    int frame_size = subframe_count * SUBFR_SIZE;
414
    float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT];
415
    float *excitation;
416
    float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER];
417
    float lsf_new[LP_FILTER_ORDER];
418
    float *impulse_response = ir_buf + LP_FILTER_ORDER;
419
    float *synth = ctx->synth_buf + 16; // 16 instead of LP_FILTER_ORDER for
420
                                        // memory alignment
421
    int t0_first = 0;
422
    AMRFixed fixed_cb;
423

    
424
    memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
425
    lsf_decode_fp(lsf_new, ctx->lsf_history, params);
426

    
427
    sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count);
428

    
429
    memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
430

    
431
    excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
432

    
433
    for (i = 0; i < subframe_count; i++) {
434
        float *pAz = Az + i*LP_FILTER_ORDER;
435
        float fixed_vector[SUBFR_SIZE];
436
        int T0,T0_frac;
437
        float pitch_gain, gain_code, avg_energy;
438

    
439
        ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i,
440
                            ctx->mode == MODE_5k0, 6);
441

    
442
        if (i == 0 || (i == 2 && ctx->mode == MODE_5k0))
443
            t0_first = T0;
444

    
445
        ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0),
446
                              ff_b60_sinc, 6,
447
                              2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER,
448
                              SUBFR_SIZE);
449

    
450
        decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode,
451
                            ctx->past_pitch_gain < 0.8);
452

    
453
        eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor);
454

    
455
        convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
456
                              SUBFR_SIZE);
457

    
458
        avg_energy =
459
            (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/
460
                SUBFR_SIZE;
461

    
462
        ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0];
463

    
464
        gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1],
465
                                          avg_energy, ctx->energy_history,
466
                                          34 - 15.0/(log2f(10.0) * 0.05),
467
                                          pred);
468

    
469
        ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
470
                                pitch_gain, gain_code, SUBFR_SIZE);
471

    
472
        pitch_gain *= 0.5 * pitch_gain;
473
        pitch_gain = FFMIN(pitch_gain, 0.4);
474

    
475
        ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain;
476
        ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain);
477
        gain_code *= ctx->gain_mem;
478

    
479
        for (j = 0; j < SUBFR_SIZE; j++)
480
            fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
481

    
482
        if (ctx->mode == MODE_5k0) {
483
            postfilter_5k0(ctx, pAz, fixed_vector);
484

    
485
            ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
486
                                         pAz, excitation, SUBFR_SIZE,
487
                                         LP_FILTER_ORDER);
488
        }
489

    
490
        ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector,
491
                                     SUBFR_SIZE, LP_FILTER_ORDER);
492

    
493
        excitation += SUBFR_SIZE;
494
    }
495

    
496
    memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
497
           LP_FILTER_ORDER * sizeof(float));
498

    
499
    if (ctx->mode == MODE_5k0) {
500
        for (i = 0; i < subframe_count; i++) {
501
            float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
502
                                           ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
503
                                           SUBFR_SIZE);
504
            ff_adaptative_gain_control(&synth[i * SUBFR_SIZE], energy,
505
                                       SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
506
        }
507

    
508
        memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size,
509
               LP_FILTER_ORDER*sizeof(float));
510
    }
511
    memcpy(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL,
512
           (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float));
513

    
514
    ff_acelp_apply_order_2_transfer_function(synth,
515
                                             (const float[2]) {-1.99997   , 1.000000000},
516
                                             (const float[2]) {-1.93307352, 0.935891986},
517
                                             0.939805806,
518
                                             ctx->highpass_filt_mem,
519
                                             frame_size);
520

    
521
    ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size);
522

    
523
}
524

    
525
static av_cold int sipr_decoder_init(AVCodecContext * avctx)
526
{
527
    SiprContext *ctx = avctx->priv_data;
528
    int i;
529

    
530
    if      (avctx->bit_rate > 12200) ctx->mode = MODE_16k;
531
    else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5;
532
    else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5;
533
    else                              ctx->mode = MODE_5k0;
534

    
535
    av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name);
536

    
537
    for (i = 0; i < LP_FILTER_ORDER; i++)
538
        ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1));
539

    
540
    for (i = 0; i < 4; i++)
541
        ctx->energy_history[i] = -14;
542

    
543
    avctx->sample_fmt = SAMPLE_FMT_FLT;
544

    
545
    if (ctx->mode == MODE_16k) {
546
        av_log(avctx, AV_LOG_ERROR, "decoding 16kbps SIPR files is not "
547
                                    "supported yet.\n");
548
        return -1;
549
    }
550

    
551
    dsputil_init(&ctx->dsp, avctx);
552

    
553
    return 0;
554
}
555

    
556
static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
557
                             int *data_size, AVPacket *avpkt)
558
{
559
    SiprContext *ctx = avctx->priv_data;
560
    const uint8_t *buf=avpkt->data;
561
    SiprParameters parm;
562
    const SiprModeParam *mode_par = &modes[ctx->mode];
563
    GetBitContext gb;
564
    float *data = datap;
565
    int i;
566

    
567
    ctx->avctx = avctx;
568
    if (avpkt->size < (mode_par->bits_per_frame >> 3)) {
569
        av_log(avctx, AV_LOG_ERROR,
570
               "Error processing packet: packet size (%d) too small\n",
571
               avpkt->size);
572

    
573
        *data_size = 0;
574
        return -1;
575
    }
576
    if (*data_size < SUBFR_SIZE * mode_par->subframe_count * sizeof(float)) {
577
        av_log(avctx, AV_LOG_ERROR,
578
               "Error processing packet: output buffer (%d) too small\n",
579
               *data_size);
580

    
581
        *data_size = 0;
582
        return -1;
583
    }
584

    
585
    init_get_bits(&gb, buf, mode_par->bits_per_frame);
586

    
587
    for (i = 0; i < mode_par->frames_per_packet; i++) {
588
        decode_parameters(&parm, &gb, mode_par);
589
        decode_frame(ctx, &parm, data);
590

    
591
        data += SUBFR_SIZE * mode_par->subframe_count;
592
    }
593

    
594
    *data_size = mode_par->frames_per_packet * SUBFR_SIZE *
595
        mode_par->subframe_count * sizeof(float);
596

    
597
    return mode_par->bits_per_frame >> 3;
598
};
599

    
600
AVCodec sipr_decoder = {
601
    "sipr",
602
    CODEC_TYPE_AUDIO,
603
    CODEC_ID_SIPR,
604
    sizeof(SiprContext),
605
    sipr_decoder_init,
606
    NULL,
607
    NULL,
608
    sipr_decode_frame,
609
    .long_name = NULL_IF_CONFIG_SMALL("RealAudio SIPR / ACELP.NET"),
610
};