~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/qcelpdec.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#undef NDEBUG
45
45
#include <assert.h>
46
46
 
47
 
typedef enum
48
 
{
49
 
    I_F_Q = -1,    /*!< insufficient frame quality */
 
47
typedef enum {
 
48
    I_F_Q = -1,    /**< insufficient frame quality */
50
49
    SILENCE,
51
50
    RATE_OCTAVE,
52
51
    RATE_QUARTER,
54
53
    RATE_FULL
55
54
} qcelp_packet_rate;
56
55
 
57
 
typedef struct
58
 
{
 
56
typedef struct {
 
57
    AVFrame           avframe;
59
58
    GetBitContext     gb;
60
59
    qcelp_packet_rate bitrate;
61
 
    QCELPFrame        frame;    /*!< unpacked data frame */
 
60
    QCELPFrame        frame;    /**< unpacked data frame */
62
61
 
63
62
    uint8_t  erasure_count;
64
 
    uint8_t  octave_count;      /*!< count the consecutive RATE_OCTAVE frames */
 
63
    uint8_t  octave_count;      /**< count the consecutive RATE_OCTAVE frames */
65
64
    float    prev_lspf[10];
66
 
    float    predictor_lspf[10];/*!< LSP predictor for RATE_OCTAVE and I_F_Q */
 
65
    float    predictor_lspf[10];/**< LSP predictor for RATE_OCTAVE and I_F_Q */
67
66
    float    pitch_synthesis_filter_mem[303];
68
67
    float    pitch_pre_filter_mem[303];
69
68
    float    rnd_fir_filter_mem[180];
94
93
 
95
94
    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
96
95
 
97
 
    for(i=0; i<10; i++)
98
 
        q->prev_lspf[i] = (i+1)/11.;
 
96
    for (i = 0; i < 10; i++)
 
97
        q->prev_lspf[i] = (i + 1) / 11.;
 
98
 
 
99
    avcodec_get_frame_defaults(&q->avframe);
 
100
    avctx->coded_frame = &q->avframe;
99
101
 
100
102
    return 0;
101
103
}
117
119
    float tmp_lspf, smooth, erasure_coeff;
118
120
    const float *predictors;
119
121
 
120
 
    if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q)
121
 
    {
122
 
        predictors = (q->prev_bitrate != RATE_OCTAVE &&
123
 
                       q->prev_bitrate != I_F_Q ?
124
 
                       q->prev_lspf : q->predictor_lspf);
 
122
    if (q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) {
 
123
        predictors = q->prev_bitrate != RATE_OCTAVE &&
 
124
                     q->prev_bitrate != I_F_Q ? q->prev_lspf
 
125
                                              : q->predictor_lspf;
125
126
 
126
 
        if(q->bitrate == RATE_OCTAVE)
127
 
        {
 
127
        if (q->bitrate == RATE_OCTAVE) {
128
128
            q->octave_count++;
129
129
 
130
 
            for(i=0; i<10; i++)
131
 
            {
 
130
            for (i = 0; i < 10; i++) {
132
131
                q->predictor_lspf[i] =
133
132
                             lspf[i] = (q->frame.lspv[i] ?  QCELP_LSP_SPREAD_FACTOR
134
 
                                                         : -QCELP_LSP_SPREAD_FACTOR)
135
 
                                     + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR
136
 
                                     + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11);
 
133
                                                         : -QCELP_LSP_SPREAD_FACTOR) +
 
134
                                        predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR   +
 
135
                                        (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR) / 11);
137
136
            }
138
 
            smooth = (q->octave_count < 10 ? .875 : 0.1);
139
 
        }else
140
 
        {
 
137
            smooth = q->octave_count < 10 ? .875 : 0.1;
 
138
        } else {
141
139
            erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
142
140
 
143
141
            assert(q->bitrate == I_F_Q);
144
142
 
145
 
            if(q->erasure_count > 1)
146
 
                erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7);
 
143
            if (q->erasure_count > 1)
 
144
                erasure_coeff *= q->erasure_count < 4 ? 0.9 : 0.7;
147
145
 
148
 
            for(i=0; i<10; i++)
149
 
            {
 
146
            for (i = 0; i < 10; i++) {
150
147
                q->predictor_lspf[i] =
151
 
                             lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11
152
 
                                     + erasure_coeff * predictors[i];
 
148
                             lspf[i] = (i + 1) * (1 - erasure_coeff) / 11 +
 
149
                                       erasure_coeff * predictors[i];
153
150
            }
154
151
            smooth = 0.125;
155
152
        }
156
153
 
157
154
        // Check the stability of the LSP frequencies.
158
155
        lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
159
 
        for(i=1; i<10; i++)
160
 
            lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR));
 
156
        for (i = 1; i < 10; i++)
 
157
            lspf[i] = FFMAX(lspf[i], lspf[i - 1] + QCELP_LSP_SPREAD_FACTOR);
161
158
 
162
 
        lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR));
163
 
        for(i=9; i>0; i--)
164
 
            lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR));
 
159
        lspf[9] = FFMIN(lspf[9], 1.0 - QCELP_LSP_SPREAD_FACTOR);
 
160
        for (i = 9; i > 0; i--)
 
161
            lspf[i - 1] = FFMIN(lspf[i - 1], lspf[i] - QCELP_LSP_SPREAD_FACTOR);
165
162
 
166
163
        // Low-pass filter the LSP frequencies.
167
 
        ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10);
168
 
    }else
169
 
    {
 
164
        ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0 - smooth, 10);
 
165
    } else {
170
166
        q->octave_count = 0;
171
167
 
172
168
        tmp_lspf = 0.;
173
 
        for(i=0; i<5 ; i++)
174
 
        {
175
 
            lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
176
 
            lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
 
169
        for (i = 0; i < 5; i++) {
 
170
            lspf[2 * i + 0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
 
171
            lspf[2 * i + 1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
177
172
        }
178
173
 
179
174
        // Check for badly received packets.
180
 
        if(q->bitrate == RATE_QUARTER)
181
 
        {
182
 
            if(lspf[9] <= .70 || lspf[9] >=  .97)
 
175
        if (q->bitrate == RATE_QUARTER) {
 
176
            if (lspf[9] <= .70 || lspf[9] >= .97)
183
177
                return -1;
184
 
            for(i=3; i<10; i++)
185
 
                if(fabs(lspf[i] - lspf[i-2]) < .08)
 
178
            for (i = 3; i < 10; i++)
 
179
                if (fabs(lspf[i] - lspf[i - 2]) < .08)
186
180
                    return -1;
187
 
        }else
188
 
        {
189
 
            if(lspf[9] <= .66 || lspf[9] >= .985)
 
181
        } else {
 
182
            if (lspf[9] <= .66 || lspf[9] >= .985)
190
183
                return -1;
191
 
            for(i=4; i<10; i++)
192
 
                if (fabs(lspf[i] - lspf[i-4]) < .0931)
 
184
            for (i = 4; i < 10; i++)
 
185
                if (fabs(lspf[i] - lspf[i - 4]) < .0931)
193
186
                    return -1;
194
187
        }
195
188
    }
204
197
 *
205
198
 * TIA/EIA/IS-733 2.4.6.2
206
199
 */
207
 
static void decode_gain_and_index(QCELPContext  *q,
208
 
                                  float *gain) {
209
 
    int   i, subframes_count, g1[16];
 
200
static void decode_gain_and_index(QCELPContext *q, float *gain)
 
201
{
 
202
    int i, subframes_count, g1[16];
210
203
    float slope;
211
204
 
212
 
    if(q->bitrate >= RATE_QUARTER)
213
 
    {
214
 
        switch(q->bitrate)
215
 
        {
216
 
            case RATE_FULL: subframes_count = 16; break;
217
 
            case RATE_HALF: subframes_count = 4;  break;
218
 
            default:        subframes_count = 5;
 
205
    if (q->bitrate >= RATE_QUARTER) {
 
206
        switch (q->bitrate) {
 
207
        case RATE_FULL: subframes_count = 16; break;
 
208
        case RATE_HALF: subframes_count =  4; break;
 
209
        default:        subframes_count =  5;
219
210
        }
220
 
        for(i=0; i<subframes_count; i++)
221
 
        {
 
211
        for (i = 0; i < subframes_count; i++) {
222
212
            g1[i] = 4 * q->frame.cbgain[i];
223
 
            if(q->bitrate == RATE_FULL && !((i+1) & 3))
224
 
            {
225
 
                g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32);
 
213
            if (q->bitrate == RATE_FULL && !((i + 1) & 3)) {
 
214
                g1[i] += av_clip((g1[i - 1] + g1[i - 2] + g1[i - 3]) / 3 - 6, 0, 32);
226
215
            }
227
216
 
228
217
            gain[i] = qcelp_g12ga[g1[i]];
229
218
 
230
 
            if(q->frame.cbsign[i])
231
 
            {
 
219
            if (q->frame.cbsign[i]) {
232
220
                gain[i] = -gain[i];
233
 
                q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127;
 
221
                q->frame.cindex[i] = (q->frame.cindex[i] - 89) & 127;
234
222
            }
235
223
        }
236
224
 
237
 
        q->prev_g1[0] = g1[i-2];
238
 
        q->prev_g1[1] = g1[i-1];
239
 
        q->last_codebook_gain = qcelp_g12ga[g1[i-1]];
 
225
        q->prev_g1[0]         = g1[i - 2];
 
226
        q->prev_g1[1]         = g1[i - 1];
 
227
        q->last_codebook_gain = qcelp_g12ga[g1[i - 1]];
240
228
 
241
 
        if(q->bitrate == RATE_QUARTER)
242
 
        {
 
229
        if (q->bitrate == RATE_QUARTER) {
243
230
            // Provide smoothing of the unvoiced excitation energy.
244
 
            gain[7] =     gain[4];
245
 
            gain[6] = 0.4*gain[3] + 0.6*gain[4];
246
 
            gain[5] =     gain[3];
247
 
            gain[4] = 0.8*gain[2] + 0.2*gain[3];
248
 
            gain[3] = 0.2*gain[1] + 0.8*gain[2];
249
 
            gain[2] =     gain[1];
250
 
            gain[1] = 0.6*gain[0] + 0.4*gain[1];
 
231
            gain[7] =       gain[4];
 
232
            gain[6] = 0.4 * gain[3] + 0.6 * gain[4];
 
233
            gain[5] =       gain[3];
 
234
            gain[4] = 0.8 * gain[2] + 0.2 * gain[3];
 
235
            gain[3] = 0.2 * gain[1] + 0.8 * gain[2];
 
236
            gain[2] =       gain[1];
 
237
            gain[1] = 0.6 * gain[0] + 0.4 * gain[1];
251
238
        }
252
 
    }else if (q->bitrate != SILENCE)
253
 
    {
254
 
        if(q->bitrate == RATE_OCTAVE)
255
 
        {
256
 
            g1[0] = 2 * q->frame.cbgain[0]
257
 
                  + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
 
239
    } else if (q->bitrate != SILENCE) {
 
240
        if (q->bitrate == RATE_OCTAVE) {
 
241
            g1[0] = 2 * q->frame.cbgain[0] +
 
242
                    av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
258
243
            subframes_count = 8;
259
 
        }else
260
 
        {
 
244
        } else {
261
245
            assert(q->bitrate == I_F_Q);
262
246
 
263
247
            g1[0] = q->prev_g1[1];
264
 
            switch(q->erasure_count)
265
 
            {
266
 
                case 1 : break;
267
 
                case 2 : g1[0] -= 1; break;
268
 
                case 3 : g1[0] -= 2; break;
269
 
                default: g1[0] -= 6;
 
248
            switch (q->erasure_count) {
 
249
            case 1 : break;
 
250
            case 2 : g1[0] -= 1; break;
 
251
            case 3 : g1[0] -= 2; break;
 
252
            default: g1[0] -= 6;
270
253
            }
271
 
            if(g1[0] < 0)
 
254
            if (g1[0] < 0)
272
255
                g1[0] = 0;
273
256
            subframes_count = 4;
274
257
        }
275
258
        // This interpolation is done to produce smoother background noise.
276
 
        slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
277
 
        for(i=1; i<=subframes_count; i++)
278
 
            gain[i-1] = q->last_codebook_gain + slope * i;
 
259
        slope = 0.5 * (qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
 
260
        for (i = 1; i <= subframes_count; i++)
 
261
                gain[i - 1] = q->last_codebook_gain + slope * i;
279
262
 
280
 
        q->last_codebook_gain = gain[i-2];
281
 
        q->prev_g1[0] = q->prev_g1[1];
282
 
        q->prev_g1[1] = g1[0];
 
263
        q->last_codebook_gain = gain[i - 2];
 
264
        q->prev_g1[0]         = q->prev_g1[1];
 
265
        q->prev_g1[1]         = g1[0];
283
266
    }
284
267
}
285
268
 
294
277
 */
295
278
static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
296
279
{
297
 
    int i, diff, prev_diff=0;
 
280
    int i, diff, prev_diff = 0;
298
281
 
299
 
    for(i=1; i<5; i++)
300
 
    {
 
282
    for (i = 1; i < 5; i++) {
301
283
        diff = cbgain[i] - cbgain[i-1];
302
 
        if(FFABS(diff) > 10)
 
284
        if (FFABS(diff) > 10)
303
285
            return -1;
304
 
        else if(FFABS(diff - prev_diff) > 12)
 
286
        else if (FFABS(diff - prev_diff) > 12)
305
287
            return -1;
306
288
        prev_diff = diff;
307
289
    }
332
314
static void compute_svector(QCELPContext *q, const float *gain,
333
315
                            float *cdn_vector)
334
316
{
335
 
    int      i, j, k;
 
317
    int i, j, k;
336
318
    uint16_t cbseed, cindex;
337
 
    float    *rnd, tmp_gain, fir_filter_value;
 
319
    float *rnd, tmp_gain, fir_filter_value;
338
320
 
339
 
    switch(q->bitrate)
340
 
    {
341
 
        case RATE_FULL:
342
 
            for(i=0; i<16; i++)
343
 
            {
344
 
                tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
345
 
                cindex = -q->frame.cindex[i];
346
 
                for(j=0; j<10; j++)
347
 
                    *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
348
 
            }
 
321
    switch (q->bitrate) {
 
322
    case RATE_FULL:
 
323
        for (i = 0; i < 16; i++) {
 
324
            tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
 
325
            cindex   = -q->frame.cindex[i];
 
326
            for (j = 0; j < 10; j++)
 
327
                *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
 
328
        }
349
329
        break;
350
 
        case RATE_HALF:
351
 
            for(i=0; i<4; i++)
352
 
            {
353
 
                tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
354
 
                cindex = -q->frame.cindex[i];
355
 
                for (j = 0; j < 40; j++)
 
330
    case RATE_HALF:
 
331
        for (i = 0; i < 4; i++) {
 
332
            tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
 
333
            cindex   = -q->frame.cindex[i];
 
334
            for (j = 0; j < 40; j++)
356
335
                *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
357
 
            }
 
336
        }
358
337
        break;
359
 
        case RATE_QUARTER:
360
 
            cbseed = (0x0003 & q->frame.lspv[4])<<14 |
361
 
                     (0x003F & q->frame.lspv[3])<< 8 |
362
 
                     (0x0060 & q->frame.lspv[2])<< 1 |
363
 
                     (0x0007 & q->frame.lspv[1])<< 3 |
364
 
                     (0x0038 & q->frame.lspv[0])>> 3 ;
365
 
            rnd = q->rnd_fir_filter_mem + 20;
366
 
            for(i=0; i<8; i++)
367
 
            {
368
 
                tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
369
 
                for(k=0; k<20; k++)
370
 
                {
371
 
                    cbseed = 521 * cbseed + 259;
372
 
                    *rnd = (int16_t)cbseed;
 
338
    case RATE_QUARTER:
 
339
        cbseed = (0x0003 & q->frame.lspv[4]) << 14 |
 
340
                 (0x003F & q->frame.lspv[3]) <<  8 |
 
341
                 (0x0060 & q->frame.lspv[2]) <<  1 |
 
342
                 (0x0007 & q->frame.lspv[1]) <<  3 |
 
343
                 (0x0038 & q->frame.lspv[0]) >>  3;
 
344
        rnd    = q->rnd_fir_filter_mem + 20;
 
345
        for (i = 0; i < 8; i++) {
 
346
            tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
 
347
            for (k = 0; k < 20; k++) {
 
348
                cbseed = 521 * cbseed + 259;
 
349
                *rnd   = (int16_t) cbseed;
373
350
 
374
351
                    // FIR filter
375
 
                    fir_filter_value = 0.0;
376
 
                    for(j=0; j<10; j++)
377
 
                        fir_filter_value += qcelp_rnd_fir_coefs[j ]
378
 
                                          * (rnd[-j ] + rnd[-20+j]);
 
352
                fir_filter_value = 0.0;
 
353
                for (j = 0; j < 10; j++)
 
354
                    fir_filter_value += qcelp_rnd_fir_coefs[j] *
 
355
                                        (rnd[-j] + rnd[-20+j]);
379
356
 
380
 
                    fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
381
 
                    *cdn_vector++ = tmp_gain * fir_filter_value;
382
 
                    rnd++;
383
 
                }
384
 
            }
385
 
            memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float));
386
 
        break;
387
 
        case RATE_OCTAVE:
388
 
            cbseed = q->first16bits;
389
 
            for(i=0; i<8; i++)
390
 
            {
391
 
                tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
392
 
                for(j=0; j<20; j++)
393
 
                {
394
 
                    cbseed = 521 * cbseed + 259;
395
 
                    *cdn_vector++ = tmp_gain * (int16_t)cbseed;
396
 
                }
397
 
            }
398
 
        break;
399
 
        case I_F_Q:
400
 
            cbseed = -44; // random codebook index
401
 
            for(i=0; i<4; i++)
402
 
            {
403
 
                tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
404
 
                for(j=0; j<40; j++)
405
 
                    *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
406
 
            }
407
 
        break;
408
 
        case SILENCE:
409
 
            memset(cdn_vector, 0, 160 * sizeof(float));
 
357
                fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
 
358
                *cdn_vector++     = tmp_gain * fir_filter_value;
 
359
                rnd++;
 
360
            }
 
361
        }
 
362
        memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160,
 
363
               20 * sizeof(float));
 
364
        break;
 
365
    case RATE_OCTAVE:
 
366
        cbseed = q->first16bits;
 
367
        for (i = 0; i < 8; i++) {
 
368
            tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
 
369
            for (j = 0; j < 20; j++) {
 
370
                cbseed        = 521 * cbseed + 259;
 
371
                *cdn_vector++ = tmp_gain * (int16_t) cbseed;
 
372
            }
 
373
        }
 
374
        break;
 
375
    case I_F_Q:
 
376
        cbseed = -44; // random codebook index
 
377
        for (i = 0; i < 4; i++) {
 
378
            tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
 
379
            for (j = 0; j < 40; j++)
 
380
                *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
 
381
        }
 
382
        break;
 
383
    case SILENCE:
 
384
        memset(cdn_vector, 0, 160 * sizeof(float));
410
385
        break;
411
386
    }
412
387
}
420
395
 *
421
396
 * TIA/EIA/IS-733 2.4.8.3, 2.4.8.6
422
397
 */
423
 
static void apply_gain_ctrl(float *v_out, const float *v_ref,
424
 
                            const float *v_in)
 
398
static void apply_gain_ctrl(float *v_out, const float *v_ref, const float *v_in)
425
399
{
426
400
    int i;
427
401
 
453
427
                                   const float gain[4], const uint8_t *lag,
454
428
                                   const uint8_t pfrac[4])
455
429
{
456
 
    int         i, j;
457
 
    float       *v_lag, *v_out;
 
430
    int i, j;
 
431
    float *v_lag, *v_out;
458
432
    const float *v_len;
459
433
 
460
434
    v_out = memory + 143; // Output vector starts at memory[143].
461
435
 
462
 
    for(i=0; i<4; i++)
463
 
    {
464
 
        if(gain[i])
465
 
        {
 
436
    for (i = 0; i < 4; i++) {
 
437
        if (gain[i]) {
466
438
            v_lag = memory + 143 + 40 * i - lag[i];
467
 
            for(v_len=v_in+40; v_in<v_len; v_in++)
468
 
            {
469
 
                if(pfrac[i]) // If it is a fractional lag...
470
 
                {
471
 
                    for(j=0, *v_out=0.; j<4; j++)
472
 
                        *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
473
 
                }else
 
439
            for (v_len = v_in + 40; v_in < v_len; v_in++) {
 
440
                if (pfrac[i]) { // If it is a fractional lag...
 
441
                    for (j = 0, *v_out = 0.; j < 4; j++)
 
442
                        *v_out += qcelp_hammsinc_table[j] * (v_lag[j - 4] + v_lag[3 - j]);
 
443
                } else
474
444
                    *v_out = *v_lag;
475
445
 
476
446
                *v_out = *v_in + gain[i] * *v_out;
478
448
                v_lag++;
479
449
                v_out++;
480
450
            }
481
 
        }else
482
 
        {
 
451
        } else {
483
452
            memcpy(v_out, v_in, 40 * sizeof(float));
484
453
            v_in  += 40;
485
454
            v_out += 40;
499
468
 */
500
469
static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
501
470
{
502
 
    int         i;
 
471
    int i;
503
472
    const float *v_synthesis_filtered, *v_pre_filtered;
504
473
 
505
 
    if(q->bitrate >= RATE_HALF ||
506
 
       q->bitrate == SILENCE ||
507
 
       (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF)))
508
 
    {
509
 
 
510
 
        if(q->bitrate >= RATE_HALF)
511
 
        {
512
 
 
 
474
    if (q->bitrate >= RATE_HALF || q->bitrate == SILENCE ||
 
475
        (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) {
 
476
 
 
477
        if (q->bitrate >= RATE_HALF) {
513
478
            // Compute gain & lag for the whole frame.
514
 
            for(i=0; i<4; i++)
515
 
            {
 
479
            for (i = 0; i < 4; i++) {
516
480
                q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
517
481
 
518
482
                q->pitch_lag[i] = q->frame.plag[i] + 16;
519
483
            }
520
 
        }else
521
 
        {
 
484
        } else {
522
485
            float max_pitch_gain;
523
486
 
524
 
            if (q->bitrate == I_F_Q)
525
 
            {
 
487
            if (q->bitrate == I_F_Q) {
526
488
                  if (q->erasure_count < 3)
527
489
                      max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
528
490
                  else
529
491
                      max_pitch_gain = 0.0;
530
 
            }else
531
 
            {
 
492
            } else {
532
493
                assert(q->bitrate == SILENCE);
533
494
                max_pitch_gain = 1.0;
534
495
            }
535
 
            for(i=0; i<4; i++)
 
496
            for (i = 0; i < 4; i++)
536
497
                q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
537
498
 
538
499
            memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
544
505
                                              q->pitch_lag, q->frame.pfrac);
545
506
 
546
507
        // pitch prefilter update
547
 
        for(i=0; i<4; i++)
 
508
        for (i = 0; i < 4; i++)
548
509
            q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
549
510
 
550
 
        v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem,
551
 
                                        v_synthesis_filtered,
552
 
                                        q->pitch_gain, q->pitch_lag,
553
 
                                        q->frame.pfrac);
 
511
        v_pre_filtered       = do_pitchfilter(q->pitch_pre_filter_mem,
 
512
                                              v_synthesis_filtered,
 
513
                                              q->pitch_gain, q->pitch_lag,
 
514
                                              q->frame.pfrac);
554
515
 
555
516
        apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
556
 
    }else
557
 
    {
558
 
        memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17,
559
 
               143 * sizeof(float));
 
517
    } else {
 
518
        memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 143 * sizeof(float));
560
519
        memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
561
520
        memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
562
521
        memset(q->pitch_lag,  0, sizeof(q->pitch_lag));
579
538
{
580
539
    double lsp[10];
581
540
    double bandwidth_expansion_coeff = QCELP_BANDWIDTH_EXPANSION_COEFF;
582
 
    int   i;
 
541
    int i;
583
542
 
584
 
    for (i=0; i<10; i++)
 
543
    for (i = 0; i < 10; i++)
585
544
        lsp[i] = cos(M_PI * lspf[i]);
586
545
 
587
546
    ff_acelp_lspd2lpc(lsp, lpc, 5);
588
547
 
589
 
    for (i=0; i<10; i++)
590
 
    {
591
 
        lpc[i] *= bandwidth_expansion_coeff;
 
548
    for (i = 0; i < 10; i++) {
 
549
        lpc[i]                    *= bandwidth_expansion_coeff;
592
550
        bandwidth_expansion_coeff *= QCELP_BANDWIDTH_EXPANSION_COEFF;
593
551
    }
594
552
}
610
568
    float interpolated_lspf[10];
611
569
    float weight;
612
570
 
613
 
    if(q->bitrate >= RATE_QUARTER)
 
571
    if (q->bitrate >= RATE_QUARTER)
614
572
        weight = 0.25 * (subframe_num + 1);
615
 
    else if(q->bitrate == RATE_OCTAVE && !subframe_num)
 
573
    else if (q->bitrate == RATE_OCTAVE && !subframe_num)
616
574
        weight = 0.625;
617
575
    else
618
576
        weight = 1.0;
619
577
 
620
 
    if(weight != 1.0)
621
 
    {
 
578
    if (weight != 1.0) {
622
579
        ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
623
580
                                weight, 1.0 - weight, 10);
624
581
        lspf2lpc(interpolated_lspf, lpc);
625
 
    }else if(q->bitrate >= RATE_QUARTER ||
626
 
             (q->bitrate == I_F_Q && !subframe_num))
 
582
    } else if (q->bitrate >= RATE_QUARTER ||
 
583
               (q->bitrate == I_F_Q && !subframe_num))
627
584
        lspf2lpc(curr_lspf, lpc);
628
 
    else if(q->bitrate == SILENCE && !subframe_num)
 
585
    else if (q->bitrate == SILENCE && !subframe_num)
629
586
        lspf2lpc(q->prev_lspf, lpc);
630
587
}
631
588
 
632
589
static qcelp_packet_rate buf_size2bitrate(const int buf_size)
633
590
{
634
 
    switch(buf_size)
635
 
    {
636
 
        case 35: return RATE_FULL;
637
 
        case 17: return RATE_HALF;
638
 
        case  8: return RATE_QUARTER;
639
 
        case  4: return RATE_OCTAVE;
640
 
        case  1: return SILENCE;
 
591
    switch (buf_size) {
 
592
    case 35: return RATE_FULL;
 
593
    case 17: return RATE_HALF;
 
594
    case  8: return RATE_QUARTER;
 
595
    case  4: return RATE_OCTAVE;
 
596
    case  1: return SILENCE;
641
597
    }
642
598
 
643
599
    return I_F_Q;
655
611
 *
656
612
 * TIA/EIA/IS-733 2.4.8.7.1
657
613
 */
658
 
static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx, const int buf_size,
659
 
                             const uint8_t **buf)
 
614
static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx,
 
615
                                           const int buf_size,
 
616
                                           const uint8_t **buf)
660
617
{
661
618
    qcelp_packet_rate bitrate;
662
619
 
663
 
    if((bitrate = buf_size2bitrate(buf_size)) >= 0)
664
 
    {
665
 
        if(bitrate > **buf)
666
 
        {
 
620
    if ((bitrate = buf_size2bitrate(buf_size)) >= 0) {
 
621
        if (bitrate > **buf) {
667
622
            QCELPContext *q = avctx->priv_data;
668
 
            if (!q->warned_buf_mismatch_bitrate)
669
 
            {
 
623
            if (!q->warned_buf_mismatch_bitrate) {
670
624
            av_log(avctx, AV_LOG_WARNING,
671
625
                   "Claimed bitrate and buffer size mismatch.\n");
672
626
                q->warned_buf_mismatch_bitrate = 1;
673
627
            }
674
628
            bitrate = **buf;
675
 
        }else if(bitrate < **buf)
676
 
        {
 
629
        } else if (bitrate < **buf) {
677
630
            av_log(avctx, AV_LOG_ERROR,
678
631
                   "Buffer is too small for the claimed bitrate.\n");
679
632
            return I_F_Q;
680
633
        }
681
634
        (*buf)++;
682
 
    }else if((bitrate = buf_size2bitrate(buf_size + 1)) >= 0)
683
 
    {
 
635
    } else if ((bitrate = buf_size2bitrate(buf_size + 1)) >= 0) {
684
636
        av_log(avctx, AV_LOG_WARNING,
685
637
               "Bitrate byte is missing, guessing the bitrate from packet size.\n");
686
 
    }else
 
638
    } else
687
639
        return I_F_Q;
688
640
 
689
 
    if(bitrate == SILENCE)
690
 
    {
 
641
    if (bitrate == SILENCE) {
691
642
        //FIXME: Remove experimental warning when tested with samples.
692
643
        av_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
693
644
    }
697
648
static void warn_insufficient_frame_quality(AVCodecContext *avctx,
698
649
                                            const char *message)
699
650
{
700
 
    av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
701
 
           message);
 
651
    av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n",
 
652
           avctx->frame_number, message);
702
653
}
703
654
 
704
655
static void postfilter(QCELPContext *q, float *samples, float *lpc)
720
671
 
721
672
    ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
722
673
                                      q->formant_mem + 10, 160, 10);
723
 
    memcpy(pole_out, q->postfilter_synth_mem,       sizeof(float) * 10);
 
674
    memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
724
675
    ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
725
676
    memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
726
677
 
727
678
    ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
728
679
 
729
680
    ff_adaptive_gain_control(samples, pole_out + 10,
730
 
        ff_dot_productf(q->formant_mem + 10, q->formant_mem + 10, 160),
731
 
        160, 0.9375, &q->postfilter_agc_mem);
 
681
                             ff_dot_productf(q->formant_mem + 10,
 
682
                                             q->formant_mem + 10, 160),
 
683
                             160, 0.9375, &q->postfilter_agc_mem);
732
684
}
733
685
 
734
 
static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
735
 
                              AVPacket *avpkt)
 
686
static int qcelp_decode_frame(AVCodecContext *avctx, void *data,
 
687
                              int *got_frame_ptr, AVPacket *avpkt)
736
688
{
737
689
    const uint8_t *buf = avpkt->data;
738
 
    int buf_size = avpkt->size;
739
 
    QCELPContext *q = avctx->priv_data;
740
 
    float *outbuffer = data;
741
 
    int   i;
 
690
    int buf_size       = avpkt->size;
 
691
    QCELPContext *q    = avctx->priv_data;
 
692
    float *outbuffer;
 
693
    int   i, ret;
742
694
    float quantized_lspf[10], lpc[10];
743
695
    float gain[16];
744
696
    float *formant_mem;
745
697
 
746
 
    if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q)
747
 
    {
 
698
    /* get output buffer */
 
699
    q->avframe.nb_samples = 160;
 
700
    if ((ret = avctx->get_buffer(avctx, &q->avframe)) < 0) {
 
701
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
702
        return ret;
 
703
    }
 
704
    outbuffer = (float *)q->avframe.data[0];
 
705
 
 
706
    if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) {
748
707
        warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
749
708
        goto erasure;
750
709
    }
751
710
 
752
 
    if(q->bitrate == RATE_OCTAVE &&
753
 
       (q->first16bits = AV_RB16(buf)) == 0xFFFF)
754
 
    {
 
711
    if (q->bitrate == RATE_OCTAVE &&
 
712
        (q->first16bits = AV_RB16(buf)) == 0xFFFF) {
755
713
        warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
756
714
        goto erasure;
757
715
    }
758
716
 
759
 
    if(q->bitrate > SILENCE)
760
 
    {
 
717
    if (q->bitrate > SILENCE) {
761
718
        const QCELPBitmap *bitmaps     = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
762
 
        const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate]
763
 
                                       + qcelp_unpacking_bitmaps_lengths[q->bitrate];
764
 
        uint8_t           *unpacked_data = (uint8_t *)&q->frame;
 
719
        const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate] +
 
720
                                         qcelp_unpacking_bitmaps_lengths[q->bitrate];
 
721
        uint8_t *unpacked_data         = (uint8_t *)&q->frame;
765
722
 
766
 
        init_get_bits(&q->gb, buf, 8*buf_size);
 
723
        init_get_bits(&q->gb, buf, 8 * buf_size);
767
724
 
768
725
        memset(&q->frame, 0, sizeof(QCELPFrame));
769
726
 
770
 
        for(; bitmaps < bitmaps_end; bitmaps++)
 
727
        for (; bitmaps < bitmaps_end; bitmaps++)
771
728
            unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
772
729
 
773
730
        // Check for erasures/blanks on rates 1, 1/4 and 1/8.
774
 
        if(q->frame.reserved)
775
 
        {
 
731
        if (q->frame.reserved) {
776
732
            warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
777
733
            goto erasure;
778
734
        }
779
 
        if(q->bitrate == RATE_QUARTER &&
780
 
           codebook_sanity_check_for_rate_quarter(q->frame.cbgain))
781
 
        {
 
735
        if (q->bitrate == RATE_QUARTER &&
 
736
            codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) {
782
737
            warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
783
738
            goto erasure;
784
739
        }
785
740
 
786
 
        if(q->bitrate >= RATE_HALF)
787
 
        {
788
 
            for(i=0; i<4; i++)
789
 
            {
790
 
                if(q->frame.pfrac[i] && q->frame.plag[i] >= 124)
791
 
                {
 
741
        if (q->bitrate >= RATE_HALF) {
 
742
            for (i = 0; i < 4; i++) {
 
743
                if (q->frame.pfrac[i] && q->frame.plag[i] >= 124) {
792
744
                    warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
793
745
                    goto erasure;
794
746
                }
799
751
    decode_gain_and_index(q, gain);
800
752
    compute_svector(q, gain, outbuffer);
801
753
 
802
 
    if(decode_lspf(q, quantized_lspf) < 0)
803
 
    {
 
754
    if (decode_lspf(q, quantized_lspf) < 0) {
804
755
        warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
805
756
        goto erasure;
806
757
    }
807
758
 
808
 
 
809
759
    apply_pitch_filters(q, outbuffer);
810
760
 
811
 
    if(q->bitrate == I_F_Q)
812
 
    {
 
761
    if (q->bitrate == I_F_Q) {
813
762
erasure:
814
763
        q->bitrate = I_F_Q;
815
764
        q->erasure_count++;
817
766
        compute_svector(q, gain, outbuffer);
818
767
        decode_lspf(q, quantized_lspf);
819
768
        apply_pitch_filters(q, outbuffer);
820
 
    }else
 
769
    } else
821
770
        q->erasure_count = 0;
822
771
 
823
772
    formant_mem = q->formant_mem + 10;
824
 
    for(i=0; i<4; i++)
825
 
    {
 
773
    for (i = 0; i < 4; i++) {
826
774
        interpolate_lpc(q, quantized_lspf, lpc, i);
827
 
        ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40,
828
 
                                     10);
 
775
        ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 10);
829
776
        formant_mem += 40;
830
777
    }
831
778
 
835
782
    memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
836
783
 
837
784
    memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
838
 
    q->prev_bitrate = q->bitrate;
839
 
 
840
 
    *data_size = 160 * sizeof(*outbuffer);
841
 
 
842
 
    return *data_size;
 
785
    q->prev_bitrate  = q->bitrate;
 
786
 
 
787
    *got_frame_ptr   = 1;
 
788
    *(AVFrame *)data = q->avframe;
 
789
 
 
790
    return buf_size;
843
791
}
844
792
 
845
 
AVCodec ff_qcelp_decoder =
846
 
{
847
 
    .name   = "qcelp",
848
 
    .type   = AVMEDIA_TYPE_AUDIO,
849
 
    .id     = CODEC_ID_QCELP,
850
 
    .init   = qcelp_decode_init,
851
 
    .decode = qcelp_decode_frame,
 
793
AVCodec ff_qcelp_decoder = {
 
794
    .name           = "qcelp",
 
795
    .type           = AVMEDIA_TYPE_AUDIO,
 
796
    .id             = CODEC_ID_QCELP,
 
797
    .init           = qcelp_decode_init,
 
798
    .decode         = qcelp_decode_frame,
 
799
    .capabilities   = CODEC_CAP_DR1,
852
800
    .priv_data_size = sizeof(QCELPContext),
853
 
    .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
 
801
    .long_name      = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
854
802
};