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