Statistics
| Branch: | Revision:

ffmpeg / libavcodec / sipr.c @ 7bd3096f

History | View | Annotate | Download (18.8 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
#include "siprdata.h"
50

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

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

    
66
    /* bitstream parameters */
67
    uint8_t number_of_fc_indexes;
68

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

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

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

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

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

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

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

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

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

    
127
typedef struct {
128
    AVCodecContext *avctx;
129
    DSPContext dsp;
130

    
131
    SiprModeParam m;
132
    SiprMode mode;
133

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

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

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

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

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

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

    
162

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

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

    
172
}
173

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

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

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

    
185
    ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1);
186

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

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

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

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

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

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

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

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

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

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

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

    
241
    qa[-1] = 0.0;
242

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
349
}
350

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

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

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

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

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

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

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

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

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

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

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

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

    
425
    sipr_decode_lp(lsf_new, ctx->lsp_history, Az, ctx->m.subframe_count);
426

    
427
    memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
428

    
429
    excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
430

    
431
    for (i = 0; i < ctx->m.subframe_count; i++) {
432
        float *pAz = Az + i*LP_FILTER_ORDER;
433
        float fixed_vector[SUBFR_SIZE];
434
        int T0,T0_frac;
435
        float pitch_gain, gain_code, avg_energy;
436

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

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

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

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

    
451
        eval_ir(pAz, T0, impulse_response, ctx->m.pitch_sharp_factor);
452

    
453
        convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
454
                              SUBFR_SIZE);
455

    
456
        avg_energy =
457
            (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/
458
                SUBFR_SIZE;
459

    
460
        ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0];
461

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

    
467
        ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
468
                                pitch_gain, gain_code, SUBFR_SIZE);
469

    
470
        pitch_gain *= 0.5 * pitch_gain;
471
        pitch_gain = FFMIN(pitch_gain, 0.4);
472

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

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

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

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

    
488
        ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector,
489
                                     SUBFR_SIZE, LP_FILTER_ORDER);
490

    
491
        excitation += SUBFR_SIZE;
492
    }
493

    
494
    memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
495
           LP_FILTER_ORDER * sizeof(float));
496

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

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

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

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

    
521
}
522

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

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

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

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

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

    
542
    avctx->sample_fmt = SAMPLE_FMT_FLT;
543

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

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

    
552
    return 0;
553
}
554

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

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

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

    
579
        *data_size = 0;
580
        return -1;
581
    }
582

    
583
    init_get_bits(&gb, buf, ctx->m.bits_per_frame);
584

    
585
    for (i = 0; i < ctx->m.frames_per_packet; i++) {
586
        decode_parameters(&parm, &gb, &ctx->m);
587
        decode_frame(ctx, &parm, data);
588

    
589
        data += SUBFR_SIZE * ctx->m.subframe_count;
590
    }
591

    
592
    *data_size = ctx->m.frames_per_packet * SUBFR_SIZE *
593
        ctx->m.subframe_count * sizeof(float);
594

    
595
    return ctx->m.bits_per_frame >> 3;
596
};
597

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