Libav
|
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 "get_bits.h" 00035 00036 #include "qcelpdata.h" 00037 00038 #include "celp_math.h" 00039 #include "celp_filters.h" 00040 #include "acelp_filters.h" 00041 #include "acelp_vectors.h" 00042 #include "lsp.h" 00043 00044 #undef NDEBUG 00045 #include <assert.h> 00046 00047 typedef enum 00048 { 00049 I_F_Q = -1, 00050 SILENCE, 00051 RATE_OCTAVE, 00052 RATE_QUARTER, 00053 RATE_HALF, 00054 RATE_FULL 00055 } qcelp_packet_rate; 00056 00057 typedef struct 00058 { 00059 GetBitContext gb; 00060 qcelp_packet_rate bitrate; 00061 QCELPFrame frame; 00063 uint8_t erasure_count; 00064 uint8_t octave_count; 00065 float prev_lspf[10]; 00066 float predictor_lspf[10]; 00067 float pitch_synthesis_filter_mem[303]; 00068 float pitch_pre_filter_mem[303]; 00069 float rnd_fir_filter_mem[180]; 00070 float formant_mem[170]; 00071 float last_codebook_gain; 00072 int prev_g1[2]; 00073 int prev_bitrate; 00074 float pitch_gain[4]; 00075 uint8_t pitch_lag[4]; 00076 uint16_t first16bits; 00077 uint8_t warned_buf_mismatch_bitrate; 00078 00079 /* postfilter */ 00080 float postfilter_synth_mem[10]; 00081 float postfilter_agc_mem; 00082 float postfilter_tilt_mem; 00083 } QCELPContext; 00084 00090 static av_cold int qcelp_decode_init(AVCodecContext *avctx) 00091 { 00092 QCELPContext *q = avctx->priv_data; 00093 int i; 00094 00095 avctx->sample_fmt = SAMPLE_FMT_FLT; 00096 00097 for(i=0; i<10; i++) 00098 q->prev_lspf[i] = (i+1)/11.; 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 { 00122 predictors = (q->prev_bitrate != RATE_OCTAVE && 00123 q->prev_bitrate != I_F_Q ? 00124 q->prev_lspf : q->predictor_lspf); 00125 00126 if(q->bitrate == RATE_OCTAVE) 00127 { 00128 q->octave_count++; 00129 00130 for(i=0; i<10; i++) 00131 { 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 { 00141 erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR; 00142 00143 assert(q->bitrate == I_F_Q); 00144 00145 if(q->erasure_count > 1) 00146 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7); 00147 00148 for(i=0; i<10; i++) 00149 { 00150 q->predictor_lspf[i] = 00151 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11 00152 + erasure_coeff * predictors[i]; 00153 } 00154 smooth = 0.125; 00155 } 00156 00157 // Check the stability of the LSP frequencies. 00158 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR); 00159 for(i=1; i<10; i++) 00160 lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR)); 00161 00162 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR)); 00163 for(i=9; i>0; i--) 00164 lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR)); 00165 00166 // Low-pass filter the LSP frequencies. 00167 ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10); 00168 }else 00169 { 00170 q->octave_count = 0; 00171 00172 tmp_lspf = 0.; 00173 for(i=0; i<5 ; i++) 00174 { 00175 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001; 00176 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001; 00177 } 00178 00179 // Check for badly received packets. 00180 if(q->bitrate == RATE_QUARTER) 00181 { 00182 if(lspf[9] <= .70 || lspf[9] >= .97) 00183 return -1; 00184 for(i=3; i<10; i++) 00185 if(fabs(lspf[i] - lspf[i-2]) < .08) 00186 return -1; 00187 }else 00188 { 00189 if(lspf[9] <= .66 || lspf[9] >= .985) 00190 return -1; 00191 for(i=4; i<10; i++) 00192 if (fabs(lspf[i] - lspf[i-4]) < .0931) 00193 return -1; 00194 } 00195 } 00196 return 0; 00197 } 00198 00207 static void decode_gain_and_index(QCELPContext *q, 00208 float *gain) { 00209 int i, subframes_count, g1[16]; 00210 float slope; 00211 00212 if(q->bitrate >= RATE_QUARTER) 00213 { 00214 switch(q->bitrate) 00215 { 00216 case RATE_FULL: subframes_count = 16; break; 00217 case RATE_HALF: subframes_count = 4; break; 00218 default: subframes_count = 5; 00219 } 00220 for(i=0; i<subframes_count; i++) 00221 { 00222 g1[i] = 4 * q->frame.cbgain[i]; 00223 if(q->bitrate == RATE_FULL && !((i+1) & 3)) 00224 { 00225 g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32); 00226 } 00227 00228 gain[i] = qcelp_g12ga[g1[i]]; 00229 00230 if(q->frame.cbsign[i]) 00231 { 00232 gain[i] = -gain[i]; 00233 q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127; 00234 } 00235 } 00236 00237 q->prev_g1[0] = g1[i-2]; 00238 q->prev_g1[1] = g1[i-1]; 00239 q->last_codebook_gain = qcelp_g12ga[g1[i-1]]; 00240 00241 if(q->bitrate == RATE_QUARTER) 00242 { 00243 // Provide smoothing of the unvoiced excitation energy. 00244 gain[7] = gain[4]; 00245 gain[6] = 0.4*gain[3] + 0.6*gain[4]; 00246 gain[5] = gain[3]; 00247 gain[4] = 0.8*gain[2] + 0.2*gain[3]; 00248 gain[3] = 0.2*gain[1] + 0.8*gain[2]; 00249 gain[2] = gain[1]; 00250 gain[1] = 0.6*gain[0] + 0.4*gain[1]; 00251 } 00252 }else if (q->bitrate != SILENCE) 00253 { 00254 if(q->bitrate == RATE_OCTAVE) 00255 { 00256 g1[0] = 2 * q->frame.cbgain[0] 00257 + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54); 00258 subframes_count = 8; 00259 }else 00260 { 00261 assert(q->bitrate == I_F_Q); 00262 00263 g1[0] = q->prev_g1[1]; 00264 switch(q->erasure_count) 00265 { 00266 case 1 : break; 00267 case 2 : g1[0] -= 1; break; 00268 case 3 : g1[0] -= 2; break; 00269 default: g1[0] -= 6; 00270 } 00271 if(g1[0] < 0) 00272 g1[0] = 0; 00273 subframes_count = 4; 00274 } 00275 // This interpolation is done to produce smoother background noise. 00276 slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count; 00277 for(i=1; i<=subframes_count; i++) 00278 gain[i-1] = q->last_codebook_gain + slope * i; 00279 00280 q->last_codebook_gain = gain[i-2]; 00281 q->prev_g1[0] = q->prev_g1[1]; 00282 q->prev_g1[1] = g1[0]; 00283 } 00284 } 00285 00295 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain) 00296 { 00297 int i, diff, prev_diff=0; 00298 00299 for(i=1; i<5; i++) 00300 { 00301 diff = cbgain[i] - cbgain[i-1]; 00302 if(FFABS(diff) > 10) 00303 return -1; 00304 else if(FFABS(diff - prev_diff) > 12) 00305 return -1; 00306 prev_diff = diff; 00307 } 00308 return 0; 00309 } 00310 00332 static void compute_svector(QCELPContext *q, const float *gain, 00333 float *cdn_vector) 00334 { 00335 int i, j, k; 00336 uint16_t cbseed, cindex; 00337 float *rnd, tmp_gain, fir_filter_value; 00338 00339 switch(q->bitrate) 00340 { 00341 case RATE_FULL: 00342 for(i=0; i<16; i++) 00343 { 00344 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; 00345 cindex = -q->frame.cindex[i]; 00346 for(j=0; j<10; j++) 00347 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127]; 00348 } 00349 break; 00350 case RATE_HALF: 00351 for(i=0; i<4; i++) 00352 { 00353 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO; 00354 cindex = -q->frame.cindex[i]; 00355 for (j = 0; j < 40; j++) 00356 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127]; 00357 } 00358 break; 00359 case RATE_QUARTER: 00360 cbseed = (0x0003 & q->frame.lspv[4])<<14 | 00361 (0x003F & q->frame.lspv[3])<< 8 | 00362 (0x0060 & q->frame.lspv[2])<< 1 | 00363 (0x0007 & q->frame.lspv[1])<< 3 | 00364 (0x0038 & q->frame.lspv[0])>> 3 ; 00365 rnd = q->rnd_fir_filter_mem + 20; 00366 for(i=0; i<8; i++) 00367 { 00368 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); 00369 for(k=0; k<20; k++) 00370 { 00371 cbseed = 521 * cbseed + 259; 00372 *rnd = (int16_t)cbseed; 00373 00374 // FIR filter 00375 fir_filter_value = 0.0; 00376 for(j=0; j<10; j++) 00377 fir_filter_value += qcelp_rnd_fir_coefs[j ] 00378 * (rnd[-j ] + rnd[-20+j]); 00379 00380 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10]; 00381 *cdn_vector++ = tmp_gain * fir_filter_value; 00382 rnd++; 00383 } 00384 } 00385 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float)); 00386 break; 00387 case RATE_OCTAVE: 00388 cbseed = q->first16bits; 00389 for(i=0; i<8; i++) 00390 { 00391 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); 00392 for(j=0; j<20; j++) 00393 { 00394 cbseed = 521 * cbseed + 259; 00395 *cdn_vector++ = tmp_gain * (int16_t)cbseed; 00396 } 00397 } 00398 break; 00399 case I_F_Q: 00400 cbseed = -44; // random codebook index 00401 for(i=0; i<4; i++) 00402 { 00403 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; 00404 for(j=0; j<40; j++) 00405 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127]; 00406 } 00407 break; 00408 case SILENCE: 00409 memset(cdn_vector, 0, 160 * sizeof(float)); 00410 break; 00411 } 00412 } 00413 00423 static void apply_gain_ctrl(float *v_out, const float *v_ref, 00424 const float *v_in) 00425 { 00426 int i; 00427 00428 for (i = 0; i < 160; i += 40) 00429 ff_scale_vector_to_given_sum_of_squares(v_out + i, v_in + i, 00430 ff_dot_productf(v_ref + i, 00431 v_ref + i, 40), 00432 40); 00433 } 00434 00452 static const float *do_pitchfilter(float memory[303], const float v_in[160], 00453 const float gain[4], const uint8_t *lag, 00454 const uint8_t pfrac[4]) 00455 { 00456 int i, j; 00457 float *v_lag, *v_out; 00458 const float *v_len; 00459 00460 v_out = memory + 143; // Output vector starts at memory[143]. 00461 00462 for(i=0; i<4; i++) 00463 { 00464 if(gain[i]) 00465 { 00466 v_lag = memory + 143 + 40 * i - lag[i]; 00467 for(v_len=v_in+40; v_in<v_len; v_in++) 00468 { 00469 if(pfrac[i]) // If it is a fractional lag... 00470 { 00471 for(j=0, *v_out=0.; j<4; j++) 00472 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]); 00473 }else 00474 *v_out = *v_lag; 00475 00476 *v_out = *v_in + gain[i] * *v_out; 00477 00478 v_lag++; 00479 v_out++; 00480 } 00481 }else 00482 { 00483 memcpy(v_out, v_in, 40 * sizeof(float)); 00484 v_in += 40; 00485 v_out += 40; 00486 } 00487 } 00488 00489 memmove(memory, memory + 160, 143 * sizeof(float)); 00490 return memory + 143; 00491 } 00492 00500 static void apply_pitch_filters(QCELPContext *q, float *cdn_vector) 00501 { 00502 int i; 00503 const float *v_synthesis_filtered, *v_pre_filtered; 00504 00505 if(q->bitrate >= RATE_HALF || 00506 q->bitrate == SILENCE || 00507 (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) 00508 { 00509 00510 if(q->bitrate >= RATE_HALF) 00511 { 00512 00513 // Compute gain & lag for the whole frame. 00514 for(i=0; i<4; i++) 00515 { 00516 q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0; 00517 00518 q->pitch_lag[i] = q->frame.plag[i] + 16; 00519 } 00520 }else 00521 { 00522 float max_pitch_gain; 00523 00524 if (q->bitrate == I_F_Q) 00525 { 00526 if (q->erasure_count < 3) 00527 max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1); 00528 else 00529 max_pitch_gain = 0.0; 00530 }else 00531 { 00532 assert(q->bitrate == SILENCE); 00533 max_pitch_gain = 1.0; 00534 } 00535 for(i=0; i<4; i++) 00536 q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain); 00537 00538 memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac)); 00539 } 00540 00541 // pitch synthesis filter 00542 v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem, 00543 cdn_vector, q->pitch_gain, 00544 q->pitch_lag, q->frame.pfrac); 00545 00546 // pitch prefilter update 00547 for(i=0; i<4; i++) 00548 q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0); 00549 00550 v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem, 00551 v_synthesis_filtered, 00552 q->pitch_gain, q->pitch_lag, 00553 q->frame.pfrac); 00554 00555 apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered); 00556 }else 00557 { 00558 memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 00559 143 * sizeof(float)); 00560 memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float)); 00561 memset(q->pitch_gain, 0, sizeof(q->pitch_gain)); 00562 memset(q->pitch_lag, 0, sizeof(q->pitch_lag)); 00563 } 00564 } 00565 00578 static void lspf2lpc(const float *lspf, float *lpc) 00579 { 00580 double lsp[10]; 00581 double bandwidth_expansion_coeff = QCELP_BANDWIDTH_EXPANSION_COEFF; 00582 int i; 00583 00584 for (i=0; i<10; i++) 00585 lsp[i] = cos(M_PI * lspf[i]); 00586 00587 ff_acelp_lspd2lpc(lsp, lpc, 5); 00588 00589 for (i=0; i<10; i++) 00590 { 00591 lpc[i] *= bandwidth_expansion_coeff; 00592 bandwidth_expansion_coeff *= QCELP_BANDWIDTH_EXPANSION_COEFF; 00593 } 00594 } 00595 00607 static void interpolate_lpc(QCELPContext *q, const float *curr_lspf, 00608 float *lpc, const int subframe_num) 00609 { 00610 float interpolated_lspf[10]; 00611 float weight; 00612 00613 if(q->bitrate >= RATE_QUARTER) 00614 weight = 0.25 * (subframe_num + 1); 00615 else if(q->bitrate == RATE_OCTAVE && !subframe_num) 00616 weight = 0.625; 00617 else 00618 weight = 1.0; 00619 00620 if(weight != 1.0) 00621 { 00622 ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, 00623 weight, 1.0 - weight, 10); 00624 lspf2lpc(interpolated_lspf, lpc); 00625 }else if(q->bitrate >= RATE_QUARTER || 00626 (q->bitrate == I_F_Q && !subframe_num)) 00627 lspf2lpc(curr_lspf, lpc); 00628 else if(q->bitrate == SILENCE && !subframe_num) 00629 lspf2lpc(q->prev_lspf, lpc); 00630 } 00631 00632 static qcelp_packet_rate buf_size2bitrate(const int buf_size) 00633 { 00634 switch(buf_size) 00635 { 00636 case 35: return RATE_FULL; 00637 case 17: return RATE_HALF; 00638 case 8: return RATE_QUARTER; 00639 case 4: return RATE_OCTAVE; 00640 case 1: return SILENCE; 00641 } 00642 00643 return I_F_Q; 00644 } 00645 00658 static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx, const int buf_size, 00659 const uint8_t **buf) 00660 { 00661 qcelp_packet_rate bitrate; 00662 00663 if((bitrate = buf_size2bitrate(buf_size)) >= 0) 00664 { 00665 if(bitrate > **buf) 00666 { 00667 QCELPContext *q = avctx->priv_data; 00668 if (!q->warned_buf_mismatch_bitrate) 00669 { 00670 av_log(avctx, AV_LOG_WARNING, 00671 "Claimed bitrate and buffer size mismatch.\n"); 00672 q->warned_buf_mismatch_bitrate = 1; 00673 } 00674 bitrate = **buf; 00675 }else if(bitrate < **buf) 00676 { 00677 av_log(avctx, AV_LOG_ERROR, 00678 "Buffer is too small for the claimed bitrate.\n"); 00679 return I_F_Q; 00680 } 00681 (*buf)++; 00682 }else if((bitrate = buf_size2bitrate(buf_size + 1)) >= 0) 00683 { 00684 av_log(avctx, AV_LOG_WARNING, 00685 "Bitrate byte is missing, guessing the bitrate from packet size.\n"); 00686 }else 00687 return I_F_Q; 00688 00689 if(bitrate == SILENCE) 00690 { 00691 //FIXME: Remove experimental warning when tested with samples. 00692 av_log_ask_for_sample(avctx, "'Blank frame handling is experimental."); 00693 } 00694 return bitrate; 00695 } 00696 00697 static void warn_insufficient_frame_quality(AVCodecContext *avctx, 00698 const char *message) 00699 { 00700 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, 00701 message); 00702 } 00703 00704 static void postfilter(QCELPContext *q, float *samples, float *lpc) 00705 { 00706 static const float pow_0_775[10] = { 00707 0.775000, 0.600625, 0.465484, 0.360750, 0.279582, 00708 0.216676, 0.167924, 0.130141, 0.100859, 0.078166 00709 }, pow_0_625[10] = { 00710 0.625000, 0.390625, 0.244141, 0.152588, 0.095367, 00711 0.059605, 0.037253, 0.023283, 0.014552, 0.009095 00712 }; 00713 float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160]; 00714 int n; 00715 00716 for (n = 0; n < 10; n++) { 00717 lpc_s[n] = lpc[n] * pow_0_625[n]; 00718 lpc_p[n] = lpc[n] * pow_0_775[n]; 00719 } 00720 00721 ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s, 00722 q->formant_mem + 10, 160, 10); 00723 memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10); 00724 ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10); 00725 memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10); 00726 00727 ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160); 00728 00729 ff_adaptive_gain_control(samples, pole_out + 10, 00730 ff_dot_productf(q->formant_mem + 10, q->formant_mem + 10, 160), 00731 160, 0.9375, &q->postfilter_agc_mem); 00732 } 00733 00734 static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size, 00735 AVPacket *avpkt) 00736 { 00737 const uint8_t *buf = avpkt->data; 00738 int buf_size = avpkt->size; 00739 QCELPContext *q = avctx->priv_data; 00740 float *outbuffer = data; 00741 int i; 00742 float quantized_lspf[10], lpc[10]; 00743 float gain[16]; 00744 float *formant_mem; 00745 00746 if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) 00747 { 00748 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined."); 00749 goto erasure; 00750 } 00751 00752 if(q->bitrate == RATE_OCTAVE && 00753 (q->first16bits = AV_RB16(buf)) == 0xFFFF) 00754 { 00755 warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on."); 00756 goto erasure; 00757 } 00758 00759 if(q->bitrate > SILENCE) 00760 { 00761 const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate]; 00762 const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate] 00763 + qcelp_unpacking_bitmaps_lengths[q->bitrate]; 00764 uint8_t *unpacked_data = (uint8_t *)&q->frame; 00765 00766 init_get_bits(&q->gb, buf, 8*buf_size); 00767 00768 memset(&q->frame, 0, sizeof(QCELPFrame)); 00769 00770 for(; bitmaps < bitmaps_end; bitmaps++) 00771 unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos; 00772 00773 // Check for erasures/blanks on rates 1, 1/4 and 1/8. 00774 if(q->frame.reserved) 00775 { 00776 warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area."); 00777 goto erasure; 00778 } 00779 if(q->bitrate == RATE_QUARTER && 00780 codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) 00781 { 00782 warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed."); 00783 goto erasure; 00784 } 00785 00786 if(q->bitrate >= RATE_HALF) 00787 { 00788 for(i=0; i<4; i++) 00789 { 00790 if(q->frame.pfrac[i] && q->frame.plag[i] >= 124) 00791 { 00792 warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter."); 00793 goto erasure; 00794 } 00795 } 00796 } 00797 } 00798 00799 decode_gain_and_index(q, gain); 00800 compute_svector(q, gain, outbuffer); 00801 00802 if(decode_lspf(q, quantized_lspf) < 0) 00803 { 00804 warn_insufficient_frame_quality(avctx, "Badly received packets in frame."); 00805 goto erasure; 00806 } 00807 00808 00809 apply_pitch_filters(q, outbuffer); 00810 00811 if(q->bitrate == I_F_Q) 00812 { 00813 erasure: 00814 q->bitrate = I_F_Q; 00815 q->erasure_count++; 00816 decode_gain_and_index(q, gain); 00817 compute_svector(q, gain, outbuffer); 00818 decode_lspf(q, quantized_lspf); 00819 apply_pitch_filters(q, outbuffer); 00820 }else 00821 q->erasure_count = 0; 00822 00823 formant_mem = q->formant_mem + 10; 00824 for(i=0; i<4; i++) 00825 { 00826 interpolate_lpc(q, quantized_lspf, lpc, i); 00827 ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 00828 10); 00829 formant_mem += 40; 00830 } 00831 00832 // postfilter, as per TIA/EIA/IS-733 2.4.8.6 00833 postfilter(q, outbuffer, lpc); 00834 00835 memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float)); 00836 00837 memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf)); 00838 q->prev_bitrate = q->bitrate; 00839 00840 *data_size = 160 * sizeof(*outbuffer); 00841 00842 return *data_size; 00843 } 00844 00845 AVCodec qcelp_decoder = 00846 { 00847 .name = "qcelp", 00848 .type = AVMEDIA_TYPE_AUDIO, 00849 .id = CODEC_ID_QCELP, 00850 .init = qcelp_decode_init, 00851 .decode = qcelp_decode_frame, 00852 .priv_data_size = sizeof(QCELPContext), 00853 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"), 00854 };