00001 /* 00002 * QCELP decoder 00003 * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00030 #include <stddef.h> 00031 00032 #include "avcodec.h" 00033 #include "internal.h" 00034 #include "bitstream.h" 00035 00036 #include "qcelpdata.h" 00037 00038 #include "celp_math.h" 00039 #include "celp_filters.h" 00040 00041 #undef NDEBUG 00042 #include <assert.h> 00043 00044 typedef enum 00045 { 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 { 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 } QCELPContext; 00076 00082 void ff_qcelp_lspf2lpc(const float *lspf, float *lpc); 00083 00084 static void weighted_vector_sumf(float *out, const float *in_a, 00085 const float *in_b, float weight_coeff_a, 00086 float weight_coeff_b, int length) 00087 { 00088 int i; 00089 00090 for(i=0; i<length; i++) 00091 out[i] = weight_coeff_a * in_a[i] 00092 + weight_coeff_b * in_b[i]; 00093 } 00094 00100 static av_cold int qcelp_decode_init(AVCodecContext *avctx) 00101 { 00102 QCELPContext *q = avctx->priv_data; 00103 int i; 00104 00105 avctx->sample_fmt = SAMPLE_FMT_FLT; 00106 00107 for(i=0; i<10; i++) 00108 q->prev_lspf[i] = (i+1)/11.; 00109 00110 return 0; 00111 } 00112 00124 static int decode_lspf(QCELPContext *q, float *lspf) 00125 { 00126 int i; 00127 float tmp_lspf, smooth, erasure_coeff; 00128 const float *predictors; 00129 00130 if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) 00131 { 00132 predictors = (q->prev_bitrate != RATE_OCTAVE && 00133 q->prev_bitrate != I_F_Q ? 00134 q->prev_lspf : q->predictor_lspf); 00135 00136 if(q->bitrate == RATE_OCTAVE) 00137 { 00138 q->octave_count++; 00139 00140 for(i=0; i<10; i++) 00141 { 00142 q->predictor_lspf[i] = 00143 lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR 00144 : -QCELP_LSP_SPREAD_FACTOR) 00145 + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR 00146 + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11); 00147 } 00148 smooth = (q->octave_count < 10 ? .875 : 0.1); 00149 }else 00150 { 00151 erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR; 00152 00153 assert(q->bitrate == I_F_Q); 00154 00155 if(q->erasure_count > 1) 00156 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7); 00157 00158 for(i=0; i<10; i++) 00159 { 00160 q->predictor_lspf[i] = 00161 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11 00162 + erasure_coeff * predictors[i]; 00163 } 00164 smooth = 0.125; 00165 } 00166 00167 // Check the stability of the LSP frequencies. 00168 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR); 00169 for(i=1; i<10; i++) 00170 lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR)); 00171 00172 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR)); 00173 for(i=9; i>0; i--) 00174 lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR)); 00175 00176 // Low-pass filter the LSP frequencies. 00177 weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10); 00178 }else 00179 { 00180 q->octave_count = 0; 00181 00182 tmp_lspf = 0.; 00183 for(i=0; i<5 ; i++) 00184 { 00185 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001; 00186 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001; 00187 } 00188 00189 // Check for badly received packets. 00190 if(q->bitrate == RATE_QUARTER) 00191 { 00192 if(lspf[9] <= .70 || lspf[9] >= .97) 00193 return -1; 00194 for(i=3; i<10; i++) 00195 if(fabs(lspf[i] - lspf[i-2]) < .08) 00196 return -1; 00197 }else 00198 { 00199 if(lspf[9] <= .66 || lspf[9] >= .985) 00200 return -1; 00201 for(i=4; i<10; i++) 00202 if (fabs(lspf[i] - lspf[i-4]) < .0931) 00203 return -1; 00204 } 00205 } 00206 return 0; 00207 } 00208 00217 static void decode_gain_and_index(QCELPContext *q, 00218 float *gain) { 00219 int i, subframes_count, g1[16]; 00220 float slope; 00221 00222 if(q->bitrate >= RATE_QUARTER) 00223 { 00224 switch(q->bitrate) 00225 { 00226 case RATE_FULL: subframes_count = 16; break; 00227 case RATE_HALF: subframes_count = 4; break; 00228 default: subframes_count = 5; 00229 } 00230 for(i=0; i<subframes_count; i++) 00231 { 00232 g1[i] = 4 * q->frame.cbgain[i]; 00233 if(q->bitrate == RATE_FULL && !((i+1) & 3)) 00234 { 00235 g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32); 00236 } 00237 00238 gain[i] = qcelp_g12ga[g1[i]]; 00239 00240 if(q->frame.cbsign[i]) 00241 { 00242 gain[i] = -gain[i]; 00243 q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127; 00244 } 00245 } 00246 00247 q->prev_g1[0] = g1[i-2]; 00248 q->prev_g1[1] = g1[i-1]; 00249 q->last_codebook_gain = qcelp_g12ga[g1[i-1]]; 00250 00251 if(q->bitrate == RATE_QUARTER) 00252 { 00253 // Provide smoothing of the unvoiced excitation energy. 00254 gain[7] = gain[4]; 00255 gain[6] = 0.4*gain[3] + 0.6*gain[4]; 00256 gain[5] = gain[3]; 00257 gain[4] = 0.8*gain[2] + 0.2*gain[3]; 00258 gain[3] = 0.2*gain[1] + 0.8*gain[2]; 00259 gain[2] = gain[1]; 00260 gain[1] = 0.6*gain[0] + 0.4*gain[1]; 00261 } 00262 }else 00263 { 00264 if(q->bitrate == RATE_OCTAVE) 00265 { 00266 g1[0] = 2 * q->frame.cbgain[0] 00267 + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54); 00268 subframes_count = 8; 00269 }else 00270 { 00271 assert(q->bitrate == I_F_Q); 00272 00273 g1[0] = q->prev_g1[1]; 00274 switch(q->erasure_count) 00275 { 00276 case 1 : break; 00277 case 2 : g1[0] -= 1; break; 00278 case 3 : g1[0] -= 2; break; 00279 default: g1[0] -= 6; 00280 } 00281 if(g1[0] < 0) 00282 g1[0] = 0; 00283 subframes_count = 4; 00284 } 00285 // This interpolation is done to produce smoother background noise. 00286 slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count; 00287 for(i=1; i<=subframes_count; i++) 00288 gain[i-1] = q->last_codebook_gain + slope * i; 00289 00290 q->last_codebook_gain = gain[i-2]; 00291 q->prev_g1[0] = q->prev_g1[1]; 00292 q->prev_g1[1] = g1[0]; 00293 } 00294 } 00295 00305 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain) 00306 { 00307 int i, diff, prev_diff=0; 00308 00309 for(i=1; i<5; i++) 00310 { 00311 diff = cbgain[i] - cbgain[i-1]; 00312 if(FFABS(diff) > 10) 00313 return -1; 00314 else if(FFABS(diff - prev_diff) > 12) 00315 return -1; 00316 prev_diff = diff; 00317 } 00318 return 0; 00319 } 00320 00342 static void compute_svector(QCELPContext *q, const float *gain, 00343 float *cdn_vector) 00344 { 00345 int i, j, k; 00346 uint16_t cbseed, cindex; 00347 float *rnd, tmp_gain, fir_filter_value; 00348 00349 switch(q->bitrate) 00350 { 00351 case RATE_FULL: 00352 for(i=0; i<16; i++) 00353 { 00354 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; 00355 cindex = -q->frame.cindex[i]; 00356 for(j=0; j<10; j++) 00357 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127]; 00358 } 00359 break; 00360 case RATE_HALF: 00361 for(i=0; i<4; i++) 00362 { 00363 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO; 00364 cindex = -q->frame.cindex[i]; 00365 for (j = 0; j < 40; j++) 00366 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127]; 00367 } 00368 break; 00369 case RATE_QUARTER: 00370 cbseed = (0x0003 & q->frame.lspv[4])<<14 | 00371 (0x003F & q->frame.lspv[3])<< 8 | 00372 (0x0060 & q->frame.lspv[2])<< 1 | 00373 (0x0007 & q->frame.lspv[1])<< 3 | 00374 (0x0038 & q->frame.lspv[0])>> 3 ; 00375 rnd = q->rnd_fir_filter_mem + 20; 00376 for(i=0; i<8; i++) 00377 { 00378 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); 00379 for(k=0; k<20; k++) 00380 { 00381 cbseed = 521 * cbseed + 259; 00382 *rnd = (int16_t)cbseed; 00383 00384 // FIR filter 00385 fir_filter_value = 0.0; 00386 for(j=0; j<10; j++) 00387 fir_filter_value += qcelp_rnd_fir_coefs[j ] 00388 * (rnd[-j ] + rnd[-20+j]); 00389 00390 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10]; 00391 *cdn_vector++ = tmp_gain * fir_filter_value; 00392 rnd++; 00393 } 00394 } 00395 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float)); 00396 break; 00397 case RATE_OCTAVE: 00398 cbseed = q->first16bits; 00399 for(i=0; i<8; i++) 00400 { 00401 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); 00402 for(j=0; j<20; j++) 00403 { 00404 cbseed = 521 * cbseed + 259; 00405 *cdn_vector++ = tmp_gain * (int16_t)cbseed; 00406 } 00407 } 00408 break; 00409 case I_F_Q: 00410 cbseed = -44; // random codebook index 00411 for(i=0; i<4; i++) 00412 { 00413 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; 00414 for(j=0; j<40; j++) 00415 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127]; 00416 } 00417 break; 00418 case SILENCE: 00419 memset(cdn_vector, 0, 160 * sizeof(float)); 00420 break; 00421 } 00422 } 00423 00437 static void apply_gain_ctrl(float *v_out, const float *v_ref, 00438 const float *v_in) 00439 { 00440 int i, j, len; 00441 float scalefactor; 00442 00443 for(i=0, j=0; i<4; i++) 00444 { 00445 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40); 00446 if(scalefactor) 00447 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40) 00448 / scalefactor); 00449 else 00450 ff_log_missing_feature(NULL, "Zero energy for gain control", 1); 00451 for(len=j+40; j<len; j++) 00452 v_out[j] = scalefactor * v_in[j]; 00453 } 00454 } 00455 00473 static const float *do_pitchfilter(float memory[303], const float v_in[160], 00474 const float gain[4], const uint8_t *lag, 00475 const uint8_t pfrac[4]) 00476 { 00477 int i, j; 00478 float *v_lag, *v_out; 00479 const float *v_len; 00480 00481 v_out = memory + 143; // Output vector starts at memory[143]. 00482 00483 for(i=0; i<4; i++) 00484 { 00485 if(gain[i]) 00486 { 00487 v_lag = memory + 143 + 40 * i - lag[i]; 00488 for(v_len=v_in+40; v_in<v_len; v_in++) 00489 { 00490 if(pfrac[i]) // If it is a fractional lag... 00491 { 00492 for(j=0, *v_out=0.; j<4; j++) 00493 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]); 00494 }else 00495 *v_out = *v_lag; 00496 00497 *v_out = *v_in + gain[i] * *v_out; 00498 00499 v_lag++; 00500 v_out++; 00501 } 00502 }else 00503 { 00504 memcpy(v_out, v_in, 40 * sizeof(float)); 00505 v_in += 40; 00506 v_out += 40; 00507 } 00508 } 00509 00510 memmove(memory, memory + 160, 143 * sizeof(float)); 00511 return memory + 143; 00512 } 00513 00521 static void apply_pitch_filters(QCELPContext *q, float *cdn_vector) 00522 { 00523 int i; 00524 const float *v_synthesis_filtered, *v_pre_filtered; 00525 00526 if(q->bitrate >= RATE_HALF || 00527 q->bitrate == SILENCE || 00528 (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) 00529 { 00530 00531 if(q->bitrate >= RATE_HALF) 00532 { 00533 00534 // Compute gain & lag for the whole frame. 00535 for(i=0; i<4; i++) 00536 { 00537 q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0; 00538 00539 q->pitch_lag[i] = q->frame.plag[i] + 16; 00540 } 00541 }else 00542 { 00543 float max_pitch_gain; 00544 00545 if (q->bitrate == I_F_Q) 00546 { 00547 if (q->erasure_count < 3) 00548 max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1); 00549 else 00550 max_pitch_gain = 0.0; 00551 }else 00552 { 00553 assert(q->bitrate == SILENCE); 00554 max_pitch_gain = 1.0; 00555 } 00556 for(i=0; i<4; i++) 00557 q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain); 00558 00559 memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac)); 00560 } 00561 00562 // pitch synthesis filter 00563 v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem, 00564 cdn_vector, q->pitch_gain, 00565 q->pitch_lag, q->frame.pfrac); 00566 00567 // pitch prefilter update 00568 for(i=0; i<4; i++) 00569 q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0); 00570 00571 v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem, 00572 v_synthesis_filtered, 00573 q->pitch_gain, q->pitch_lag, 00574 q->frame.pfrac); 00575 00576 apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered); 00577 }else 00578 { 00579 memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 00580 143 * sizeof(float)); 00581 memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float)); 00582 memset(q->pitch_gain, 0, sizeof(q->pitch_gain)); 00583 memset(q->pitch_lag, 0, sizeof(q->pitch_lag)); 00584 } 00585 } 00586 00598 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc, 00599 const int subframe_num) 00600 { 00601 float interpolated_lspf[10]; 00602 float weight; 00603 00604 if(q->bitrate >= RATE_QUARTER) 00605 weight = 0.25 * (subframe_num + 1); 00606 else if(q->bitrate == RATE_OCTAVE && !subframe_num) 00607 weight = 0.625; 00608 else 00609 weight = 1.0; 00610 00611 if(weight != 1.0) 00612 { 00613 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, 00614 weight, 1.0 - weight, 10); 00615 ff_qcelp_lspf2lpc(interpolated_lspf, lpc); 00616 }else if(q->bitrate >= RATE_QUARTER || 00617 (q->bitrate == I_F_Q && !subframe_num)) 00618 ff_qcelp_lspf2lpc(curr_lspf, lpc); 00619 else if(q->bitrate == SILENCE && !subframe_num) 00620 ff_qcelp_lspf2lpc(q->prev_lspf, lpc); 00621 } 00622 00623 static qcelp_packet_rate buf_size2bitrate(const int buf_size) 00624 { 00625 switch(buf_size) 00626 { 00627 case 35: return RATE_FULL; 00628 case 17: return RATE_HALF; 00629 case 8: return RATE_QUARTER; 00630 case 4: return RATE_OCTAVE; 00631 case 1: return SILENCE; 00632 } 00633 00634 return I_F_Q; 00635 } 00636 00649 static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx, const int buf_size, 00650 const uint8_t **buf) 00651 { 00652 qcelp_packet_rate bitrate; 00653 00654 if((bitrate = buf_size2bitrate(buf_size)) >= 0) 00655 { 00656 if(bitrate > **buf) 00657 { 00658 QCELPContext *q = avctx->priv_data; 00659 if (!q->warned_buf_mismatch_bitrate) 00660 { 00661 av_log(avctx, AV_LOG_WARNING, 00662 "Claimed bitrate and buffer size mismatch.\n"); 00663 q->warned_buf_mismatch_bitrate = 1; 00664 } 00665 bitrate = **buf; 00666 }else if(bitrate < **buf) 00667 { 00668 av_log(avctx, AV_LOG_ERROR, 00669 "Buffer is too small for the claimed bitrate.\n"); 00670 return I_F_Q; 00671 } 00672 (*buf)++; 00673 }else if((bitrate = buf_size2bitrate(buf_size + 1)) >= 0) 00674 { 00675 av_log(avctx, AV_LOG_WARNING, 00676 "Bitrate byte is missing, guessing the bitrate from packet size.\n"); 00677 }else 00678 return I_F_Q; 00679 00680 if(bitrate == SILENCE) 00681 { 00682 //FIXME: Remove experimental warning when tested with samples. 00683 ff_log_ask_for_sample(avctx, "'Blank frame handling is experimental."); 00684 } 00685 return bitrate; 00686 } 00687 00688 static void warn_insufficient_frame_quality(AVCodecContext *avctx, 00689 const char *message) 00690 { 00691 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, 00692 message); 00693 } 00694 00695 static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size, 00696 const uint8_t *buf, int buf_size) 00697 { 00698 QCELPContext *q = avctx->priv_data; 00699 float *outbuffer = data; 00700 int i; 00701 float quantized_lspf[10], lpc[10]; 00702 float gain[16]; 00703 float *formant_mem; 00704 00705 if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) 00706 { 00707 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined."); 00708 goto erasure; 00709 } 00710 00711 if(q->bitrate == RATE_OCTAVE && 00712 (q->first16bits = AV_RB16(buf)) == 0xFFFF) 00713 { 00714 warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on."); 00715 goto erasure; 00716 } 00717 00718 if(q->bitrate > SILENCE) 00719 { 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 // Check for erasures/blanks on rates 1, 1/4 and 1/8. 00733 if(q->frame.reserved) 00734 { 00735 warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area."); 00736 goto erasure; 00737 } 00738 if(q->bitrate == RATE_QUARTER && 00739 codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) 00740 { 00741 warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed."); 00742 goto erasure; 00743 } 00744 00745 if(q->bitrate >= RATE_HALF) 00746 { 00747 for(i=0; i<4; i++) 00748 { 00749 if(q->frame.pfrac[i] && q->frame.plag[i] >= 124) 00750 { 00751 warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter."); 00752 goto erasure; 00753 } 00754 } 00755 } 00756 } 00757 00758 decode_gain_and_index(q, gain); 00759 compute_svector(q, gain, outbuffer); 00760 00761 if(decode_lspf(q, quantized_lspf) < 0) 00762 { 00763 warn_insufficient_frame_quality(avctx, "Badly received packets in frame."); 00764 goto erasure; 00765 } 00766 00767 00768 apply_pitch_filters(q, outbuffer); 00769 00770 if(q->bitrate == I_F_Q) 00771 { 00772 erasure: 00773 q->bitrate = I_F_Q; 00774 q->erasure_count++; 00775 decode_gain_and_index(q, gain); 00776 compute_svector(q, gain, outbuffer); 00777 decode_lspf(q, quantized_lspf); 00778 apply_pitch_filters(q, outbuffer); 00779 }else 00780 q->erasure_count = 0; 00781 00782 formant_mem = q->formant_mem + 10; 00783 for(i=0; i<4; i++) 00784 { 00785 interpolate_lpc(q, quantized_lspf, lpc, i); 00786 ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 00787 10); 00788 formant_mem += 40; 00789 } 00790 memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float)); 00791 00792 // FIXME: postfilter and final gain control should be here. 00793 // TIA/EIA/IS-733 2.4.8.6 00794 00795 formant_mem = q->formant_mem + 10; 00796 for(i=0; i<160; i++) 00797 *outbuffer++ = av_clipf(*formant_mem++, QCELP_CLIP_LOWER_BOUND, 00798 QCELP_CLIP_UPPER_BOUND); 00799 00800 memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf)); 00801 q->prev_bitrate = q->bitrate; 00802 00803 *data_size = 160 * sizeof(*outbuffer); 00804 00805 return buf_size; 00806 } 00807 00808 AVCodec qcelp_decoder = 00809 { 00810 .name = "qcelp", 00811 .type = CODEC_TYPE_AUDIO, 00812 .id = CODEC_ID_QCELP, 00813 .init = qcelp_decode_init, 00814 .decode = qcelp_decode_frame, 00815 .priv_data_size = sizeof(QCELPContext), 00816 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"), 00817 };