Mercurial > libavcodec.hg
annotate qcelpdec.c @ 8233:ababfa151ced libavcodec
enable RV40 decoder
| author | kostya |
|---|---|
| date | Mon, 01 Dec 2008 06:40:36 +0000 |
| parents | 72949bacc1b9 |
| children | 93ee77578391 |
| rev | line source |
|---|---|
| 8096 | 1 /* |
| 2 * QCELP decoder | |
| 3 * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet | |
| 4 * | |
| 5 * This file is part of FFmpeg. | |
| 6 * | |
| 7 * FFmpeg is free software; you can redistribute it and/or | |
| 8 * modify it under the terms of the GNU Lesser General Public | |
| 9 * License as published by the Free Software Foundation; either | |
| 10 * version 2.1 of the License, or (at your option) any later version. | |
| 11 * | |
| 12 * FFmpeg is distributed in the hope that it will be useful, | |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
| 18 * License along with FFmpeg; if not, write to the Free Software | |
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 */ | |
| 8150 | 21 |
| 8096 | 22 /** |
| 23 * @file qcelpdec.c | |
| 24 * QCELP decoder | |
| 25 * @author Reynaldo H. Verdejo Pinochet | |
|
8151
e2c068cb210a
Credit Kenan Gillet for his contributions towards merging the SoC QCELP decoder.
reynaldo
parents:
8150
diff
changeset
|
26 * @remark FFmpeg merging spearheaded by Kenan Gillet |
| 8096 | 27 */ |
| 28 | |
| 29 #include <stddef.h> | |
| 30 | |
| 31 #include "avcodec.h" | |
| 32 #include "bitstream.h" | |
| 33 | |
| 34 #include "qcelp.h" | |
| 35 #include "qcelpdata.h" | |
| 36 | |
| 37 #include "celp_math.h" | |
| 38 #include "celp_filters.h" | |
| 39 | |
| 40 #undef NDEBUG | |
| 41 #include <assert.h> | |
| 42 | |
| 8230 | 43 typedef struct { |
| 44 GetBitContext gb; | |
| 45 qcelp_packet_rate bitrate; | |
| 46 QCELPFrame frame; /*!< unpacked data frame */ | |
| 47 uint8_t erasure_count; | |
| 48 uint8_t octave_count; /*!< count the consecutive RATE_OCTAVE frames */ | |
| 49 float prev_lspf[10]; | |
| 50 float predictor_lspf[10]; /*!< LSP predictor, | |
| 51 only use for RATE_OCTAVE and I_F_Q */ | |
| 52 float formant_mem[170]; | |
| 53 float last_codebook_gain; | |
| 54 int prev_g1[2]; | |
| 55 int prev_bitrate; | |
| 56 float prev_pitch_gain[4]; | |
| 57 uint8_t prev_pitch_lag[4]; | |
| 58 uint16_t first16bits; | |
| 59 } QCELPContext; | |
| 60 | |
| 8150 | 61 static void weighted_vector_sumf(float *out, const float *in_a, |
| 62 const float *in_b, float weight_coeff_a, | |
| 63 float weight_coeff_b, int length) | |
| 64 { | |
| 65 int i; | |
| 8123 | 66 |
| 8150 | 67 for(i=0; i<length; i++) |
| 8123 | 68 out[i] = weight_coeff_a * in_a[i] |
| 69 + weight_coeff_b * in_b[i]; | |
| 70 } | |
| 71 | |
| 8096 | 72 /** |
| 8145 | 73 * Initialize the speech codec according to the specification. |
| 74 * | |
| 75 * TIA/EIA/IS-733 2.4.9 | |
| 76 */ | |
| 8192 | 77 static av_cold int qcelp_decode_init(AVCodecContext *avctx) |
| 78 { | |
| 8145 | 79 QCELPContext *q = avctx->priv_data; |
| 80 int i; | |
| 81 | |
| 82 avctx->sample_fmt = SAMPLE_FMT_FLT; | |
| 83 | |
| 84 for (i = 0; i < 10; i++) | |
| 85 q->prev_lspf[i] = (i + 1) / 11.; | |
| 86 | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 /** | |
| 8191 | 91 * Decodes the 10 quantized LSP frequencies from the LSPV/LSP |
| 92 * transmission codes of any bitrate and checks for badly received packets. | |
| 93 * | |
| 94 * @param q the context | |
| 95 * @param lspf line spectral pair frequencies | |
| 96 * | |
| 97 * @return 0 on success, -1 if the packet is badly received | |
| 98 * | |
| 99 * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3 | |
| 100 */ | |
| 8192 | 101 static int decode_lspf(QCELPContext *q, float *lspf) |
| 102 { | |
| 8191 | 103 int i; |
| 104 float tmp_lspf; | |
| 105 | |
| 8192 | 106 if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) |
| 107 { | |
| 8191 | 108 float smooth; |
| 109 const float *predictors = (q->prev_bitrate != RATE_OCTAVE && | |
| 110 q->prev_bitrate != I_F_Q ? q->prev_lspf | |
| 111 : q->predictor_lspf); | |
| 112 | |
| 8192 | 113 if(q->bitrate == RATE_OCTAVE) |
| 114 { | |
| 8191 | 115 q->octave_count++; |
| 116 | |
| 8192 | 117 for(i=0; i<10; i++) |
| 118 { | |
| 8191 | 119 q->predictor_lspf[i] = |
| 8230 | 120 lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR |
| 121 : -QCELP_LSP_SPREAD_FACTOR) | |
| 8191 | 122 + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR |
| 123 + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11); | |
| 124 } | |
| 125 smooth = (q->octave_count < 10 ? .875 : 0.1); | |
| 8192 | 126 }else |
| 127 { | |
| 8191 | 128 float erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR; |
| 129 | |
| 130 assert(q->bitrate == I_F_Q); | |
| 131 | |
| 8192 | 132 if(q->erasure_count > 1) |
| 8191 | 133 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7); |
| 134 | |
| 8192 | 135 for(i=0; i<10; i++) |
| 136 { | |
| 8191 | 137 q->predictor_lspf[i] = |
| 138 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11 | |
| 139 + erasure_coeff * predictors[i]; | |
| 140 } | |
| 141 smooth = 0.125; | |
| 142 } | |
| 143 | |
| 144 // Check the stability of the LSP frequencies. | |
| 145 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR); | |
| 8192 | 146 for(i=1; i<10; i++) |
| 8191 | 147 lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR)); |
| 148 | |
| 149 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR)); | |
| 8192 | 150 for(i=9; i>0; i--) |
| 8191 | 151 lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR)); |
| 152 | |
| 153 // Low-pass filter the LSP frequencies. | |
| 8192 | 154 weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10); |
| 155 }else | |
| 156 { | |
| 8191 | 157 q->octave_count = 0; |
| 158 | |
| 159 tmp_lspf = 0.; | |
| 8192 | 160 for(i=0; i<5 ; i++) |
| 161 { | |
| 8230 | 162 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001; |
| 163 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001; | |
| 8191 | 164 } |
| 165 | |
| 166 // Check for badly received packets. | |
| 8192 | 167 if(q->bitrate == RATE_QUARTER) |
| 168 { | |
| 169 if(lspf[9] <= .70 || lspf[9] >= .97) | |
| 8191 | 170 return -1; |
| 8192 | 171 for(i=3; i<10; i++) |
| 172 if(fabs(lspf[i] - lspf[i-2]) < .08) | |
| 8191 | 173 return -1; |
| 8192 | 174 }else |
| 175 { | |
| 176 if(lspf[9] <= .66 || lspf[9] >= .985) | |
| 8191 | 177 return -1; |
| 8192 | 178 for(i=4; i<10; i++) |
| 8191 | 179 if (fabs(lspf[i] - lspf[i-4]) < .0931) |
| 180 return -1; | |
| 181 } | |
| 182 } | |
| 183 return 0; | |
| 184 } | |
| 185 | |
| 186 /** | |
| 8230 | 187 * Converts codebook transmission codes to GAIN and INDEX. |
| 188 * | |
| 189 * @param q the context | |
| 190 * @param gain array holding the decoded gain | |
| 191 * | |
| 192 * TIA/EIA/IS-733 2.4.6.2 | |
| 193 */ | |
| 194 static void decode_gain_and_index(QCELPContext *q, | |
| 195 float *gain) { | |
| 196 int i, subframes_count, g1[16]; | |
| 197 float slope; | |
| 198 | |
| 199 if (q->bitrate >= RATE_QUARTER) { | |
| 200 switch (q->bitrate) { | |
| 201 case RATE_FULL: subframes_count = 16; break; | |
| 202 case RATE_HALF: subframes_count = 4; break; | |
| 203 default: subframes_count = 5; | |
| 204 } | |
| 205 for (i = 0; i < subframes_count; i++) { | |
| 206 g1[i] = 4 * q->frame.cbgain[i]; | |
| 207 if (q->bitrate == RATE_FULL && !((i+1) & 3)) { | |
| 208 g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32); | |
| 209 } | |
| 210 | |
| 211 gain[i] = qcelp_g12ga[g1[i]]; | |
| 212 | |
| 213 if (q->frame.cbsign[i]) { | |
| 214 gain[i] = -gain[i]; | |
| 215 q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127; | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 q->prev_g1[0] = g1[i-2]; | |
| 220 q->prev_g1[1] = g1[i-1]; | |
| 221 q->last_codebook_gain = qcelp_g12ga[g1[i-1]]; | |
| 222 | |
| 223 if (q->bitrate == RATE_QUARTER) { | |
| 224 // Provide smoothing of the unvoiced excitation energy. | |
| 225 gain[7] = gain[4]; | |
| 226 gain[6] = 0.4*gain[3] + 0.6*gain[4]; | |
| 227 gain[5] = gain[3]; | |
| 228 gain[4] = 0.8*gain[2] + 0.2*gain[3]; | |
| 229 gain[3] = 0.2*gain[1] + 0.8*gain[2]; | |
| 230 gain[2] = gain[1]; | |
| 231 gain[1] = 0.6*gain[0] + 0.4*gain[1]; | |
| 232 } | |
| 233 } else { | |
| 234 if (q->bitrate == RATE_OCTAVE) { | |
| 235 g1[0] = 2 * q->frame.cbgain[0] | |
| 236 + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54); | |
| 237 subframes_count = 8; | |
| 238 } else { | |
| 239 assert(q->bitrate == I_F_Q); | |
| 240 | |
| 241 g1[0] = q->prev_g1[1]; | |
| 242 switch (q->erasure_count) { | |
| 243 case 1 : break; | |
| 244 case 2 : g1[0] -= 1; break; | |
| 245 case 3 : g1[0] -= 2; break; | |
| 246 default: g1[0] -= 6; | |
| 247 } | |
| 248 if (g1[0] < 0) | |
| 249 g1[0] = 0; | |
| 250 subframes_count = 4; | |
| 251 } | |
| 252 // This interpolation is done to produce smoother background noise. | |
| 253 slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count; | |
| 254 for (i = 1; i <= subframes_count; i++) | |
| 255 gain[i-1] = q->last_codebook_gain + slope * i; | |
| 256 q->last_codebook_gain = gain[i-2]; | |
| 257 | |
| 258 q->prev_g1[0] = q->prev_g1[1]; | |
| 259 q->prev_g1[1] = g1[0]; | |
| 260 } | |
| 261 } | |
| 262 | |
| 263 /** | |
| 8192 | 264 * If the received packet is Rate 1/4 a further sanity check is made of the |
| 265 * codebook gain. | |
| 8191 | 266 * |
| 267 * @param cbgain the unpacked cbgain array | |
| 268 * @return -1 if the sanity check fails, 0 otherwise | |
| 269 * | |
| 270 * TIA/EIA/IS-733 2.4.8.7.3 | |
| 271 */ | |
| 8192 | 272 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain) |
| 273 { | |
| 8193 | 274 int i, prev_diff=0; |
| 8191 | 275 |
| 8192 | 276 for(i=1; i<5; i++) |
| 277 { | |
| 278 int diff = cbgain[i] - cbgain[i-1]; | |
| 279 if(FFABS(diff) > 10) | |
| 280 return -1; | |
| 281 else if(FFABS(diff - prev_diff) > 12) | |
| 282 return -1; | |
| 283 prev_diff = diff; | |
| 8193 | 284 } |
| 285 return 0; | |
| 8191 | 286 } |
| 287 | |
| 288 /** | |
| 8145 | 289 * Computes the scaled codebook vector Cdn From INDEX and GAIN |
| 290 * for all rates. | |
| 291 * | |
| 292 * The specification lacks some information here. | |
| 293 * | |
| 294 * TIA/EIA/IS-733 has an omission on the codebook index determination | |
| 295 * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says | |
| 296 * you have to subtract the decoded index parameter from the given scaled | |
| 297 * codebook vector index 'n' to get the desired circular codebook index, but | |
| 298 * it does not mention that you have to clamp 'n' to [0-9] in order to get | |
| 299 * RI-compliant results. | |
| 300 * | |
| 301 * The reason for this mistake seems to be the fact they forgot to mention you | |
| 302 * have to do these calculations per codebook subframe and adjust given | |
| 303 * equation values accordingly. | |
| 304 * | |
| 305 * @param q the context | |
| 306 * @param gain array holding the 4 pitch subframe gain values | |
| 307 * @param cdn_vector array for the generated scaled codebook vector | |
| 308 */ | |
| 8192 | 309 static void compute_svector(const QCELPContext *q, const float *gain, |
| 310 float *cdn_vector) | |
| 311 { | |
| 8145 | 312 int i, j, k; |
| 313 uint16_t cbseed, cindex; | |
| 314 float *rnd, tmp_gain, fir_filter_value; | |
| 315 | |
| 8192 | 316 switch(q->bitrate) |
| 317 { | |
| 318 case RATE_FULL: | |
| 319 for(i=0; i<16; i++) | |
| 320 { | |
| 321 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; | |
| 8230 | 322 cindex = -q->frame.cindex[i]; |
| 8192 | 323 for(j=0; j<10; j++) |
| 324 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127]; | |
| 325 } | |
| 8145 | 326 break; |
| 8192 | 327 case RATE_HALF: |
| 328 for(i=0; i<4; i++) | |
| 329 { | |
| 330 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO; | |
| 8230 | 331 cindex = -q->frame.cindex[i]; |
| 8192 | 332 for (j = 0; j < 40; j++) |
| 8145 | 333 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127]; |
| 8192 | 334 } |
| 8145 | 335 break; |
| 8192 | 336 case RATE_QUARTER: |
| 8230 | 337 cbseed = (0x0003 & q->frame.lspv[4])<<14 | |
| 338 (0x003F & q->frame.lspv[3])<< 8 | | |
| 339 (0x0060 & q->frame.lspv[2])<< 1 | | |
| 340 (0x0007 & q->frame.lspv[1])<< 3 | | |
| 341 (0x0038 & q->frame.lspv[0])>> 3 ; | |
| 8192 | 342 rnd = q->rnd_fir_filter_mem + 20; |
| 343 for(i=0; i<8; i++) | |
| 344 { | |
| 345 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); | |
| 346 for(k=0; k<20; k++) | |
| 347 { | |
| 348 cbseed = 521 * cbseed + 259; | |
| 349 *rnd = (int16_t)cbseed; | |
| 8145 | 350 |
| 8192 | 351 // FIR filter |
| 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]); | |
| 8145 | 356 |
| 8192 | 357 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10]; |
| 358 *cdn_vector++ = tmp_gain * fir_filter_value; | |
| 359 rnd++; | |
| 360 } | |
| 8145 | 361 } |
| 8192 | 362 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float)); |
| 8145 | 363 break; |
| 8192 | 364 case RATE_OCTAVE: |
| 365 cbseed = q->first16bits; | |
| 366 for(i=0; i<8; i++) | |
| 367 { | |
| 368 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); | |
| 369 for(j=0; j<20; j++) | |
| 370 { | |
| 371 cbseed = 521 * cbseed + 259; | |
| 372 *cdn_vector++ = tmp_gain * (int16_t)cbseed; | |
| 373 } | |
| 8145 | 374 } |
| 375 break; | |
| 8192 | 376 case I_F_Q: |
| 377 cbseed = -44; // random codebook index | |
| 378 for(i=0; i<4; i++) | |
| 379 { | |
| 380 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; | |
| 381 for(j=0; j<40; j++) | |
| 382 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127]; | |
| 383 } | |
| 8145 | 384 break; |
| 385 } | |
| 386 } | |
| 387 | |
| 388 /** | |
| 389 * Apply generic gain control. | |
| 390 * | |
| 391 * @param v_out output vector | |
| 392 * @param v_in gain-controlled vector | |
| 393 * @param v_ref vector to control gain of | |
| 394 * | |
| 395 * FIXME: If v_ref is a zero vector, it energy is zero | |
| 396 * and the behavior of the gain control is | |
| 397 * undefined in the specs. | |
| 398 * | |
| 399 * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6 | |
| 400 */ | |
| 8192 | 401 static void apply_gain_ctrl(float *v_out, const float *v_ref, |
| 402 const float *v_in) | |
| 403 { | |
| 8145 | 404 int i, j, len; |
| 405 float scalefactor; | |
| 406 | |
| 8192 | 407 for(i=0, j=0; i<4; i++) |
| 408 { | |
| 8145 | 409 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40); |
| 8192 | 410 if(scalefactor) |
| 411 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40) | |
| 412 / scalefactor); | |
| 8145 | 413 else |
| 414 av_log_missing_feature(NULL, "Zero energy for gain control", 1); | |
| 8192 | 415 for(len=j+40; j<len; j++) |
| 8145 | 416 v_out[j] = scalefactor * v_in[j]; |
| 417 } | |
| 418 } | |
| 419 | |
| 420 /** | |
| 8096 | 421 * Apply filter in pitch-subframe steps. |
| 422 * | |
| 423 * @param memory buffer for the previous state of the filter | |
| 424 * - must be able to contain 303 elements | |
| 425 * - the 143 first elements are from the previous state | |
| 426 * - the next 160 are for output | |
| 427 * @param v_in input filter vector | |
| 428 * @param gain per-subframe gain array, each element is between 0.0 and 2.0 | |
| 429 * @param lag per-subframe lag array, each element is | |
| 430 * - between 16 and 143 if its corresponding pfrac is 0, | |
| 431 * - between 16 and 139 otherwise | |
| 8192 | 432 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 |
| 433 * otherwise | |
| 8096 | 434 * |
| 435 * @return filter output vector | |
| 436 */ | |
| 8150 | 437 static const float *do_pitchfilter(float memory[303], const float v_in[160], |
| 438 const float gain[4], const uint8_t *lag, | |
| 439 const uint8_t pfrac[4]) | |
| 440 { | |
| 8096 | 441 int i, j; |
| 442 float *v_lag, *v_out; | |
| 443 const float *v_len; | |
| 444 | |
| 445 v_out = memory + 143; // Output vector starts at memory[143]. | |
| 446 | |
| 8150 | 447 for(i=0; i<4; i++) |
| 448 { | |
| 449 if(gain[i]) | |
| 450 { | |
| 8096 | 451 v_lag = memory + 143 + 40 * i - lag[i]; |
| 8150 | 452 for(v_len=v_in+40; v_in<v_len; v_in++) |
| 453 { | |
| 454 if(pfrac[i]) // If it is a fractional lag... | |
| 455 { | |
| 456 for(j=0, *v_out=0.; j<4; j++) | |
| 8096 | 457 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]); |
| 8150 | 458 }else |
| 8096 | 459 *v_out = *v_lag; |
| 460 | |
| 461 *v_out = *v_in + gain[i] * *v_out; | |
| 462 | |
| 463 v_lag++; | |
| 464 v_out++; | |
| 465 } | |
| 8150 | 466 }else |
| 467 { | |
| 8096 | 468 memcpy(v_out, v_in, 40 * sizeof(float)); |
| 469 v_in += 40; | |
| 470 v_out += 40; | |
| 471 } | |
| 8150 | 472 } |
| 8096 | 473 |
| 474 memmove(memory, memory + 160, 143 * sizeof(float)); | |
| 475 return memory + 143; | |
| 476 } | |
| 477 | |
| 8127 | 478 /** |
| 479 * Interpolates LSP frequencies and computes LPC coefficients | |
| 8191 | 480 * for a given bitrate & pitch subframe. |
| 8127 | 481 * |
| 482 * TIA/EIA/IS-733 2.4.3.3.4 | |
| 483 * | |
| 484 * @param q the context | |
| 485 * @param curr_lspf LSP frequencies vector of the current frame | |
| 486 * @param lpc float vector for the resulting LPC | |
| 487 * @param subframe_num frame number in decoded stream | |
| 488 */ | |
| 8150 | 489 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc, |
| 490 const int subframe_num) | |
| 491 { | |
| 8127 | 492 float interpolated_lspf[10]; |
| 493 float weight; | |
| 494 | |
| 8191 | 495 if(q->bitrate >= RATE_QUARTER) |
| 8127 | 496 weight = 0.25 * (subframe_num + 1); |
| 8191 | 497 else if(q->bitrate == RATE_OCTAVE && !subframe_num) |
| 8127 | 498 weight = 0.625; |
| 8150 | 499 else |
| 8127 | 500 weight = 1.0; |
| 501 | |
| 8150 | 502 if(weight != 1.0) |
| 503 { | |
| 504 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, | |
| 505 weight, 1.0 - weight, 10); | |
| 8145 | 506 qcelp_lspf2lpc(interpolated_lspf, lpc); |
| 8191 | 507 }else if(q->bitrate >= RATE_QUARTER || (q->bitrate == I_F_Q && !subframe_num)) |
| 8145 | 508 qcelp_lspf2lpc(curr_lspf, lpc); |
| 8127 | 509 } |
| 510 | |
| 8191 | 511 static int buf_size2bitrate(const int buf_size) |
| 8150 | 512 { |
| 513 switch(buf_size) | |
| 514 { | |
| 515 case 35: | |
| 516 return RATE_FULL; | |
| 517 case 17: | |
| 518 return RATE_HALF; | |
| 519 case 8: | |
| 520 return RATE_QUARTER; | |
| 521 case 4: | |
| 522 return RATE_OCTAVE; | |
| 523 case 1: | |
| 524 return SILENCE; | |
| 8123 | 525 } |
| 8150 | 526 |
| 8123 | 527 return -1; |
| 528 } | |
| 529 | |
| 8096 | 530 static void warn_insufficient_frame_quality(AVCodecContext *avctx, |
| 8150 | 531 const char *message) |
| 532 { | |
| 533 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, | |
| 534 message); | |
| 8096 | 535 } |
| 8127 | 536 |
| 8230 | 537 static int qcelp_decode_frame(AVCodecContext *avctx, |
| 538 void *data, | |
| 539 int *data_size, | |
| 540 uint8_t *buf, | |
| 541 const int buf_size) { | |
| 542 QCELPContext *q = avctx->priv_data; | |
| 543 float *outbuffer = data; | |
| 544 int i; | |
| 545 float quantized_lspf[10], lpc[10]; | |
| 546 float gain[16]; | |
| 547 float *formant_mem; | |
| 548 | |
| 549 if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) { | |
| 550 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined."); | |
| 551 goto erasure; | |
| 552 } | |
| 553 | |
| 554 if (q->bitrate == RATE_OCTAVE && | |
| 555 (q->first16bits = AV_RB16(buf)) == 0xFFFF) { | |
| 556 warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on."); | |
| 557 goto erasure; | |
| 558 } | |
| 559 | |
| 560 if (q->bitrate > SILENCE) { | |
| 561 const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate]; | |
| 562 const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate] | |
| 563 + qcelp_unpacking_bitmaps_lengths[q->bitrate]; | |
| 564 uint8_t *unpacked_data = (uint8_t *)&q->frame; | |
| 565 | |
| 566 init_get_bits(&q->gb, buf, 8*buf_size); | |
| 567 | |
| 568 memset(&q->frame, 0, sizeof(QCELPFrame)); | |
| 569 | |
| 570 for (; bitmaps < bitmaps_end; bitmaps++) | |
| 571 unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos; | |
| 572 | |
| 573 // Check for erasures/blanks on rates 1, 1/4 and 1/8. | |
| 574 if (q->frame.reserved) { | |
| 575 warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area."); | |
| 576 goto erasure; | |
| 577 } | |
| 578 if (q->bitrate == RATE_QUARTER && codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) { | |
| 579 warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed."); | |
| 580 goto erasure; | |
| 581 } | |
| 582 | |
| 583 if (q->bitrate >= RATE_HALF) { | |
| 584 for (i = 0; i < 4; i++) { | |
| 585 if (q->frame.pfrac[i] && q->frame.plag[i] >= 124) { | |
| 586 warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter."); | |
| 587 goto erasure; | |
| 588 } | |
| 589 } | |
| 590 } | |
| 591 } | |
| 592 | |
| 593 decode_gain_and_index(q, gain); | |
| 594 compute_svector(q, gain, outbuffer); | |
| 595 | |
| 596 if (decode_lspf(q, quantized_lspf) < 0) { | |
| 597 warn_insufficient_frame_quality(avctx, "Badly received packets in frame."); | |
| 598 goto erasure; | |
| 599 } | |
| 600 | |
| 601 | |
| 602 apply_pitch_filters(q, outbuffer); | |
| 603 | |
| 604 if (q->bitrate == I_F_Q) { | |
| 605 erasure: | |
| 606 q->bitrate = I_F_Q; | |
| 607 q->erasure_count++; | |
| 608 decode_gain_and_index(q, gain); | |
| 609 compute_svector(q, gain, outbuffer); | |
| 610 decode_lspf(q, quantized_lspf); | |
| 611 apply_pitch_filters(q, outbuffer); | |
| 612 } else | |
| 613 q->erasure_count = 0; | |
| 614 | |
| 615 formant_mem = q->formant_mem + 10; | |
| 616 for (i = 0; i < 4; i++) { | |
| 617 interpolate_lpc(q, quantized_lspf, lpc, i); | |
| 618 ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 10); | |
| 619 formant_mem += 40; | |
| 620 } | |
| 621 memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float)); | |
| 622 | |
| 623 // FIXME: postfilter and final gain control should be here. | |
| 624 // TIA/EIA/IS-733 2.4.8.6 | |
| 625 | |
| 626 formant_mem = q->formant_mem + 10; | |
| 627 for (i = 0; i < 160; i++) | |
| 628 *outbuffer++ = av_clipf(*formant_mem++, QCELP_CLIP_LOWER_BOUND, QCELP_CLIP_UPPER_BOUND); | |
| 629 | |
| 630 memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf)); | |
| 631 q->prev_bitrate = q->bitrate; | |
| 632 | |
| 633 *data_size = 160 * sizeof(*outbuffer); | |
| 634 | |
| 635 return *data_size; | |
| 636 } | |
| 637 | |
| 8127 | 638 AVCodec qcelp_decoder = |
| 639 { | |
| 640 .name = "qcelp", | |
| 641 .type = CODEC_TYPE_AUDIO, | |
| 642 .id = CODEC_ID_QCELP, | |
| 643 .init = qcelp_decode_init, | |
| 644 .decode = qcelp_decode_frame, | |
| 645 .priv_data_size = sizeof(QCELPContext), | |
| 646 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"), | |
| 647 }; |
