00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include <stddef.h>
00031
00032 #include "avcodec.h"
00033 #include "internal.h"
00034 #include "get_bits.h"
00035 #include "dsputil.h"
00036 #include "qcelpdata.h"
00037 #include "celp_filters.h"
00038 #include "acelp_filters.h"
00039 #include "acelp_vectors.h"
00040 #include "lsp.h"
00041
00042 #undef NDEBUG
00043 #include <assert.h>
00044
00045 typedef enum {
00046 I_F_Q = -1,
00047 SILENCE,
00048 RATE_OCTAVE,
00049 RATE_QUARTER,
00050 RATE_HALF,
00051 RATE_FULL
00052 } qcelp_packet_rate;
00053
00054 typedef struct {
00055 AVFrame avframe;
00056 GetBitContext gb;
00057 qcelp_packet_rate bitrate;
00058 QCELPFrame frame;
00060 uint8_t erasure_count;
00061 uint8_t octave_count;
00062 float prev_lspf[10];
00063 float predictor_lspf[10];
00064 float pitch_synthesis_filter_mem[303];
00065 float pitch_pre_filter_mem[303];
00066 float rnd_fir_filter_mem[180];
00067 float formant_mem[170];
00068 float last_codebook_gain;
00069 int prev_g1[2];
00070 int prev_bitrate;
00071 float pitch_gain[4];
00072 uint8_t pitch_lag[4];
00073 uint16_t first16bits;
00074 uint8_t warned_buf_mismatch_bitrate;
00075
00076
00077 float postfilter_synth_mem[10];
00078 float postfilter_agc_mem;
00079 float postfilter_tilt_mem;
00080 } QCELPContext;
00081
00087 static av_cold int qcelp_decode_init(AVCodecContext *avctx)
00088 {
00089 QCELPContext *q = avctx->priv_data;
00090 int i;
00091
00092 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00093
00094 for (i = 0; i < 10; i++)
00095 q->prev_lspf[i] = (i + 1) / 11.;
00096
00097 avcodec_get_frame_defaults(&q->avframe);
00098 avctx->coded_frame = &q->avframe;
00099
00100 return 0;
00101 }
00102
00114 static int decode_lspf(QCELPContext *q, float *lspf)
00115 {
00116 int i;
00117 float tmp_lspf, smooth, erasure_coeff;
00118 const float *predictors;
00119
00120 if (q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) {
00121 predictors = q->prev_bitrate != RATE_OCTAVE &&
00122 q->prev_bitrate != I_F_Q ? q->prev_lspf
00123 : q->predictor_lspf;
00124
00125 if (q->bitrate == RATE_OCTAVE) {
00126 q->octave_count++;
00127
00128 for (i = 0; i < 10; i++) {
00129 q->predictor_lspf[i] =
00130 lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR
00131 : -QCELP_LSP_SPREAD_FACTOR) +
00132 predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR +
00133 (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR) / 11);
00134 }
00135 smooth = q->octave_count < 10 ? .875 : 0.1;
00136 } else {
00137 erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
00138
00139 assert(q->bitrate == I_F_Q);
00140
00141 if (q->erasure_count > 1)
00142 erasure_coeff *= q->erasure_count < 4 ? 0.9 : 0.7;
00143
00144 for (i = 0; i < 10; i++) {
00145 q->predictor_lspf[i] =
00146 lspf[i] = (i + 1) * (1 - erasure_coeff) / 11 +
00147 erasure_coeff * predictors[i];
00148 }
00149 smooth = 0.125;
00150 }
00151
00152
00153 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
00154 for (i = 1; i < 10; i++)
00155 lspf[i] = FFMAX(lspf[i], lspf[i - 1] + QCELP_LSP_SPREAD_FACTOR);
00156
00157 lspf[9] = FFMIN(lspf[9], 1.0 - QCELP_LSP_SPREAD_FACTOR);
00158 for (i = 9; i > 0; i--)
00159 lspf[i - 1] = FFMIN(lspf[i - 1], lspf[i] - QCELP_LSP_SPREAD_FACTOR);
00160
00161
00162 ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0 - smooth, 10);
00163 } else {
00164 q->octave_count = 0;
00165
00166 tmp_lspf = 0.;
00167 for (i = 0; i < 5; i++) {
00168 lspf[2 * i + 0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
00169 lspf[2 * i + 1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
00170 }
00171
00172
00173 if (q->bitrate == RATE_QUARTER) {
00174 if (lspf[9] <= .70 || lspf[9] >= .97)
00175 return -1;
00176 for (i = 3; i < 10; i++)
00177 if (fabs(lspf[i] - lspf[i - 2]) < .08)
00178 return -1;
00179 } else {
00180 if (lspf[9] <= .66 || lspf[9] >= .985)
00181 return -1;
00182 for (i = 4; i < 10; i++)
00183 if (fabs(lspf[i] - lspf[i - 4]) < .0931)
00184 return -1;
00185 }
00186 }
00187 return 0;
00188 }
00189
00198 static void decode_gain_and_index(QCELPContext *q, float *gain)
00199 {
00200 int i, subframes_count, g1[16];
00201 float slope;
00202
00203 if (q->bitrate >= RATE_QUARTER) {
00204 switch (q->bitrate) {
00205 case RATE_FULL: subframes_count = 16; break;
00206 case RATE_HALF: subframes_count = 4; break;
00207 default: subframes_count = 5;
00208 }
00209 for (i = 0; i < subframes_count; i++) {
00210 g1[i] = 4 * q->frame.cbgain[i];
00211 if (q->bitrate == RATE_FULL && !((i + 1) & 3)) {
00212 g1[i] += av_clip((g1[i - 1] + g1[i - 2] + g1[i - 3]) / 3 - 6, 0, 32);
00213 }
00214
00215 gain[i] = qcelp_g12ga[g1[i]];
00216
00217 if (q->frame.cbsign[i]) {
00218 gain[i] = -gain[i];
00219 q->frame.cindex[i] = (q->frame.cindex[i] - 89) & 127;
00220 }
00221 }
00222
00223 q->prev_g1[0] = g1[i - 2];
00224 q->prev_g1[1] = g1[i - 1];
00225 q->last_codebook_gain = qcelp_g12ga[g1[i - 1]];
00226
00227 if (q->bitrate == RATE_QUARTER) {
00228
00229 gain[7] = gain[4];
00230 gain[6] = 0.4 * gain[3] + 0.6 * gain[4];
00231 gain[5] = gain[3];
00232 gain[4] = 0.8 * gain[2] + 0.2 * gain[3];
00233 gain[3] = 0.2 * gain[1] + 0.8 * gain[2];
00234 gain[2] = gain[1];
00235 gain[1] = 0.6 * gain[0] + 0.4 * gain[1];
00236 }
00237 } else if (q->bitrate != SILENCE) {
00238 if (q->bitrate == RATE_OCTAVE) {
00239 g1[0] = 2 * q->frame.cbgain[0] +
00240 av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
00241 subframes_count = 8;
00242 } else {
00243 assert(q->bitrate == I_F_Q);
00244
00245 g1[0] = q->prev_g1[1];
00246 switch (q->erasure_count) {
00247 case 1 : break;
00248 case 2 : g1[0] -= 1; break;
00249 case 3 : g1[0] -= 2; break;
00250 default: g1[0] -= 6;
00251 }
00252 if (g1[0] < 0)
00253 g1[0] = 0;
00254 subframes_count = 4;
00255 }
00256
00257 slope = 0.5 * (qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
00258 for (i = 1; i <= subframes_count; i++)
00259 gain[i - 1] = q->last_codebook_gain + slope * i;
00260
00261 q->last_codebook_gain = gain[i - 2];
00262 q->prev_g1[0] = q->prev_g1[1];
00263 q->prev_g1[1] = g1[0];
00264 }
00265 }
00266
00276 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
00277 {
00278 int i, diff, prev_diff = 0;
00279
00280 for (i = 1; i < 5; i++) {
00281 diff = cbgain[i] - cbgain[i-1];
00282 if (FFABS(diff) > 10)
00283 return -1;
00284 else if (FFABS(diff - prev_diff) > 12)
00285 return -1;
00286 prev_diff = diff;
00287 }
00288 return 0;
00289 }
00290
00312 static void compute_svector(QCELPContext *q, const float *gain,
00313 float *cdn_vector)
00314 {
00315 int i, j, k;
00316 uint16_t cbseed, cindex;
00317 float *rnd, tmp_gain, fir_filter_value;
00318
00319 switch (q->bitrate) {
00320 case RATE_FULL:
00321 for (i = 0; i < 16; i++) {
00322 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
00323 cindex = -q->frame.cindex[i];
00324 for (j = 0; j < 10; j++)
00325 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
00326 }
00327 break;
00328 case RATE_HALF:
00329 for (i = 0; i < 4; i++) {
00330 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
00331 cindex = -q->frame.cindex[i];
00332 for (j = 0; j < 40; j++)
00333 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
00334 }
00335 break;
00336 case RATE_QUARTER:
00337 cbseed = (0x0003 & q->frame.lspv[4]) << 14 |
00338 (0x003F & q->frame.lspv[3]) << 8 |
00339 (0x0060 & q->frame.lspv[2]) << 1 |
00340 (0x0007 & q->frame.lspv[1]) << 3 |
00341 (0x0038 & q->frame.lspv[0]) >> 3;
00342 rnd = q->rnd_fir_filter_mem + 20;
00343 for (i = 0; i < 8; i++) {
00344 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
00345 for (k = 0; k < 20; k++) {
00346 cbseed = 521 * cbseed + 259;
00347 *rnd = (int16_t) cbseed;
00348
00349
00350 fir_filter_value = 0.0;
00351 for (j = 0; j < 10; j++)
00352 fir_filter_value += qcelp_rnd_fir_coefs[j] *
00353 (rnd[-j] + rnd[-20+j]);
00354
00355 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
00356 *cdn_vector++ = tmp_gain * fir_filter_value;
00357 rnd++;
00358 }
00359 }
00360 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160,
00361 20 * sizeof(float));
00362 break;
00363 case RATE_OCTAVE:
00364 cbseed = q->first16bits;
00365 for (i = 0; i < 8; i++) {
00366 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
00367 for (j = 0; j < 20; j++) {
00368 cbseed = 521 * cbseed + 259;
00369 *cdn_vector++ = tmp_gain * (int16_t) cbseed;
00370 }
00371 }
00372 break;
00373 case I_F_Q:
00374 cbseed = -44;
00375 for (i = 0; i < 4; i++) {
00376 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
00377 for (j = 0; j < 40; j++)
00378 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
00379 }
00380 break;
00381 case SILENCE:
00382 memset(cdn_vector, 0, 160 * sizeof(float));
00383 break;
00384 }
00385 }
00386
00396 static void apply_gain_ctrl(float *v_out, const float *v_ref, const float *v_in)
00397 {
00398 int i;
00399
00400 for (i = 0; i < 160; i += 40)
00401 ff_scale_vector_to_given_sum_of_squares(v_out + i, v_in + i,
00402 ff_scalarproduct_float_c(v_ref + i,
00403 v_ref + i,
00404 40),
00405 40);
00406 }
00407
00425 static const float *do_pitchfilter(float memory[303], const float v_in[160],
00426 const float gain[4], const uint8_t *lag,
00427 const uint8_t pfrac[4])
00428 {
00429 int i, j;
00430 float *v_lag, *v_out;
00431 const float *v_len;
00432
00433 v_out = memory + 143;
00434
00435 for (i = 0; i < 4; i++) {
00436 if (gain[i]) {
00437 v_lag = memory + 143 + 40 * i - lag[i];
00438 for (v_len = v_in + 40; v_in < v_len; v_in++) {
00439 if (pfrac[i]) {
00440 for (j = 0, *v_out = 0.; j < 4; j++)
00441 *v_out += qcelp_hammsinc_table[j] * (v_lag[j - 4] + v_lag[3 - j]);
00442 } else
00443 *v_out = *v_lag;
00444
00445 *v_out = *v_in + gain[i] * *v_out;
00446
00447 v_lag++;
00448 v_out++;
00449 }
00450 } else {
00451 memcpy(v_out, v_in, 40 * sizeof(float));
00452 v_in += 40;
00453 v_out += 40;
00454 }
00455 }
00456
00457 memmove(memory, memory + 160, 143 * sizeof(float));
00458 return memory + 143;
00459 }
00460
00468 static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
00469 {
00470 int i;
00471 const float *v_synthesis_filtered, *v_pre_filtered;
00472
00473 if (q->bitrate >= RATE_HALF || q->bitrate == SILENCE ||
00474 (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) {
00475
00476 if (q->bitrate >= RATE_HALF) {
00477
00478 for (i = 0; i < 4; i++) {
00479 q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
00480
00481 q->pitch_lag[i] = q->frame.plag[i] + 16;
00482 }
00483 } else {
00484 float max_pitch_gain;
00485
00486 if (q->bitrate == I_F_Q) {
00487 if (q->erasure_count < 3)
00488 max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
00489 else
00490 max_pitch_gain = 0.0;
00491 } else {
00492 assert(q->bitrate == SILENCE);
00493 max_pitch_gain = 1.0;
00494 }
00495 for (i = 0; i < 4; i++)
00496 q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
00497
00498 memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
00499 }
00500
00501
00502 v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem,
00503 cdn_vector, q->pitch_gain,
00504 q->pitch_lag, q->frame.pfrac);
00505
00506
00507 for (i = 0; i < 4; i++)
00508 q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
00509
00510 v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem,
00511 v_synthesis_filtered,
00512 q->pitch_gain, q->pitch_lag,
00513 q->frame.pfrac);
00514
00515 apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
00516 } else {
00517 memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 143 * sizeof(float));
00518 memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
00519 memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
00520 memset(q->pitch_lag, 0, sizeof(q->pitch_lag));
00521 }
00522 }
00523
00536 static void lspf2lpc(const float *lspf, float *lpc)
00537 {
00538 double lsp[10];
00539 double bandwidth_expansion_coeff = QCELP_BANDWIDTH_EXPANSION_COEFF;
00540 int i;
00541
00542 for (i = 0; i < 10; i++)
00543 lsp[i] = cos(M_PI * lspf[i]);
00544
00545 ff_acelp_lspd2lpc(lsp, lpc, 5);
00546
00547 for (i = 0; i < 10; i++) {
00548 lpc[i] *= bandwidth_expansion_coeff;
00549 bandwidth_expansion_coeff *= QCELP_BANDWIDTH_EXPANSION_COEFF;
00550 }
00551 }
00552
00564 static void interpolate_lpc(QCELPContext *q, const float *curr_lspf,
00565 float *lpc, const int subframe_num)
00566 {
00567 float interpolated_lspf[10];
00568 float weight;
00569
00570 if (q->bitrate >= RATE_QUARTER)
00571 weight = 0.25 * (subframe_num + 1);
00572 else if (q->bitrate == RATE_OCTAVE && !subframe_num)
00573 weight = 0.625;
00574 else
00575 weight = 1.0;
00576
00577 if (weight != 1.0) {
00578 ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
00579 weight, 1.0 - weight, 10);
00580 lspf2lpc(interpolated_lspf, lpc);
00581 } else if (q->bitrate >= RATE_QUARTER ||
00582 (q->bitrate == I_F_Q && !subframe_num))
00583 lspf2lpc(curr_lspf, lpc);
00584 else if (q->bitrate == SILENCE && !subframe_num)
00585 lspf2lpc(q->prev_lspf, lpc);
00586 }
00587
00588 static qcelp_packet_rate buf_size2bitrate(const int buf_size)
00589 {
00590 switch (buf_size) {
00591 case 35: return RATE_FULL;
00592 case 17: return RATE_HALF;
00593 case 8: return RATE_QUARTER;
00594 case 4: return RATE_OCTAVE;
00595 case 1: return SILENCE;
00596 }
00597
00598 return I_F_Q;
00599 }
00600
00613 static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx,
00614 const int buf_size,
00615 const uint8_t **buf)
00616 {
00617 qcelp_packet_rate bitrate;
00618
00619 if ((bitrate = buf_size2bitrate(buf_size)) >= 0) {
00620 if (bitrate > **buf) {
00621 QCELPContext *q = avctx->priv_data;
00622 if (!q->warned_buf_mismatch_bitrate) {
00623 av_log(avctx, AV_LOG_WARNING,
00624 "Claimed bitrate and buffer size mismatch.\n");
00625 q->warned_buf_mismatch_bitrate = 1;
00626 }
00627 bitrate = **buf;
00628 } else if (bitrate < **buf) {
00629 av_log(avctx, AV_LOG_ERROR,
00630 "Buffer is too small for the claimed bitrate.\n");
00631 return I_F_Q;
00632 }
00633 (*buf)++;
00634 } else if ((bitrate = buf_size2bitrate(buf_size + 1)) >= 0) {
00635 av_log(avctx, AV_LOG_WARNING,
00636 "Bitrate byte is missing, guessing the bitrate from packet size.\n");
00637 } else
00638 return I_F_Q;
00639
00640 if (bitrate == SILENCE) {
00641
00642 av_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
00643 }
00644 return bitrate;
00645 }
00646
00647 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
00648 const char *message)
00649 {
00650 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n",
00651 avctx->frame_number, message);
00652 }
00653
00654 static void postfilter(QCELPContext *q, float *samples, float *lpc)
00655 {
00656 static const float pow_0_775[10] = {
00657 0.775000, 0.600625, 0.465484, 0.360750, 0.279582,
00658 0.216676, 0.167924, 0.130141, 0.100859, 0.078166
00659 }, pow_0_625[10] = {
00660 0.625000, 0.390625, 0.244141, 0.152588, 0.095367,
00661 0.059605, 0.037253, 0.023283, 0.014552, 0.009095
00662 };
00663 float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160];
00664 int n;
00665
00666 for (n = 0; n < 10; n++) {
00667 lpc_s[n] = lpc[n] * pow_0_625[n];
00668 lpc_p[n] = lpc[n] * pow_0_775[n];
00669 }
00670
00671 ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
00672 q->formant_mem + 10, 160, 10);
00673 memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
00674 ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
00675 memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
00676
00677 ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
00678
00679 ff_adaptive_gain_control(samples, pole_out + 10,
00680 ff_scalarproduct_float_c(q->formant_mem + 10,
00681 q->formant_mem + 10, 160),
00682 160, 0.9375, &q->postfilter_agc_mem);
00683 }
00684
00685 static int qcelp_decode_frame(AVCodecContext *avctx, void *data,
00686 int *got_frame_ptr, AVPacket *avpkt)
00687 {
00688 const uint8_t *buf = avpkt->data;
00689 int buf_size = avpkt->size;
00690 QCELPContext *q = avctx->priv_data;
00691 float *outbuffer;
00692 int i, ret;
00693 float quantized_lspf[10], lpc[10];
00694 float gain[16];
00695 float *formant_mem;
00696
00697
00698 q->avframe.nb_samples = 160;
00699 if ((ret = avctx->get_buffer(avctx, &q->avframe)) < 0) {
00700 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00701 return ret;
00702 }
00703 outbuffer = (float *)q->avframe.data[0];
00704
00705 if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) {
00706 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
00707 goto erasure;
00708 }
00709
00710 if (q->bitrate == RATE_OCTAVE &&
00711 (q->first16bits = AV_RB16(buf)) == 0xFFFF) {
00712 warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
00713 goto erasure;
00714 }
00715
00716 if (q->bitrate > SILENCE) {
00717 const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
00718 const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate] +
00719 qcelp_unpacking_bitmaps_lengths[q->bitrate];
00720 uint8_t *unpacked_data = (uint8_t *)&q->frame;
00721
00722 init_get_bits(&q->gb, buf, 8 * buf_size);
00723
00724 memset(&q->frame, 0, sizeof(QCELPFrame));
00725
00726 for (; bitmaps < bitmaps_end; bitmaps++)
00727 unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
00728
00729
00730 if (q->frame.reserved) {
00731 warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
00732 goto erasure;
00733 }
00734 if (q->bitrate == RATE_QUARTER &&
00735 codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) {
00736 warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
00737 goto erasure;
00738 }
00739
00740 if (q->bitrate >= RATE_HALF) {
00741 for (i = 0; i < 4; i++) {
00742 if (q->frame.pfrac[i] && q->frame.plag[i] >= 124) {
00743 warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
00744 goto erasure;
00745 }
00746 }
00747 }
00748 }
00749
00750 decode_gain_and_index(q, gain);
00751 compute_svector(q, gain, outbuffer);
00752
00753 if (decode_lspf(q, quantized_lspf) < 0) {
00754 warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
00755 goto erasure;
00756 }
00757
00758 apply_pitch_filters(q, outbuffer);
00759
00760 if (q->bitrate == I_F_Q) {
00761 erasure:
00762 q->bitrate = I_F_Q;
00763 q->erasure_count++;
00764 decode_gain_and_index(q, gain);
00765 compute_svector(q, gain, outbuffer);
00766 decode_lspf(q, quantized_lspf);
00767 apply_pitch_filters(q, outbuffer);
00768 } else
00769 q->erasure_count = 0;
00770
00771 formant_mem = q->formant_mem + 10;
00772 for (i = 0; i < 4; i++) {
00773 interpolate_lpc(q, quantized_lspf, lpc, i);
00774 ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 10);
00775 formant_mem += 40;
00776 }
00777
00778
00779 postfilter(q, outbuffer, lpc);
00780
00781 memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
00782
00783 memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
00784 q->prev_bitrate = q->bitrate;
00785
00786 *got_frame_ptr = 1;
00787 *(AVFrame *)data = q->avframe;
00788
00789 return buf_size;
00790 }
00791
00792 AVCodec ff_qcelp_decoder = {
00793 .name = "qcelp",
00794 .type = AVMEDIA_TYPE_AUDIO,
00795 .id = AV_CODEC_ID_QCELP,
00796 .init = qcelp_decode_init,
00797 .decode = qcelp_decode_frame,
00798 .capabilities = CODEC_CAP_DR1,
00799 .priv_data_size = sizeof(QCELPContext),
00800 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
00801 };