Mercurial > libavcodec.hg
annotate qcelpdec.c @ 8151:e2c068cb210a libavcodec
Credit Kenan Gillet for his contributions towards merging the SoC QCELP decoder.
| author | reynaldo |
|---|---|
| date | Sun, 16 Nov 2008 01:00:25 +0000 |
| parents | 4da8fc62ae00 |
| children | cc1e8c59f1e8 |
| 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 | |
| 8150 | 43 static void weighted_vector_sumf(float *out, const float *in_a, |
| 44 const float *in_b, float weight_coeff_a, | |
| 45 float weight_coeff_b, int length) | |
| 46 { | |
| 47 int i; | |
| 8123 | 48 |
| 8150 | 49 for(i=0; i<length; i++) |
| 8123 | 50 out[i] = weight_coeff_a * in_a[i] |
| 51 + weight_coeff_b * in_b[i]; | |
| 52 } | |
| 53 | |
| 8096 | 54 /** |
| 8145 | 55 * Initialize the speech codec according to the specification. |
| 56 * | |
| 57 * TIA/EIA/IS-733 2.4.9 | |
| 58 */ | |
| 59 static av_cold int qcelp_decode_init(AVCodecContext *avctx) { | |
| 60 QCELPContext *q = avctx->priv_data; | |
| 61 int i; | |
| 62 | |
| 63 avctx->sample_fmt = SAMPLE_FMT_FLT; | |
| 64 | |
| 65 for (i = 0; i < 10; i++) | |
| 66 q->prev_lspf[i] = (i + 1) / 11.; | |
| 67 | |
| 68 return 0; | |
| 69 } | |
| 70 | |
| 71 /** | |
| 72 * Computes the scaled codebook vector Cdn From INDEX and GAIN | |
| 73 * for all rates. | |
| 74 * | |
| 75 * The specification lacks some information here. | |
| 76 * | |
| 77 * TIA/EIA/IS-733 has an omission on the codebook index determination | |
| 78 * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says | |
| 79 * you have to subtract the decoded index parameter from the given scaled | |
| 80 * codebook vector index 'n' to get the desired circular codebook index, but | |
| 81 * it does not mention that you have to clamp 'n' to [0-9] in order to get | |
| 82 * RI-compliant results. | |
| 83 * | |
| 84 * The reason for this mistake seems to be the fact they forgot to mention you | |
| 85 * have to do these calculations per codebook subframe and adjust given | |
| 86 * equation values accordingly. | |
| 87 * | |
| 88 * @param q the context | |
| 89 * @param gain array holding the 4 pitch subframe gain values | |
| 90 * @param cdn_vector array for the generated scaled codebook vector | |
| 91 */ | |
| 92 static void compute_svector(const QCELPContext *q, | |
| 93 const float *gain, | |
| 94 float *cdn_vector) { | |
| 95 int i, j, k; | |
| 96 uint16_t cbseed, cindex; | |
| 97 float *rnd, tmp_gain, fir_filter_value; | |
| 98 | |
| 99 switch (q->framerate) { | |
| 100 case RATE_FULL: | |
| 101 for (i = 0; i < 16; i++) { | |
| 102 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; | |
| 103 cindex = -q->cindex[i]; | |
| 104 for (j = 0; j < 10; j++) | |
| 105 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127]; | |
| 106 } | |
| 107 break; | |
| 108 case RATE_HALF: | |
| 109 for (i = 0; i < 4; i++) { | |
| 110 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO; | |
| 111 cindex = -q->cindex[i]; | |
| 112 for (j = 0; j < 40; j++) | |
| 113 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127]; | |
| 114 } | |
| 115 break; | |
| 116 case RATE_QUARTER: | |
| 117 cbseed = (0x0003 & q->lspv[4])<<14 | | |
| 118 (0x003F & q->lspv[3])<< 8 | | |
| 119 (0x0060 & q->lspv[2])<< 1 | | |
| 120 (0x0007 & q->lspv[1])<< 3 | | |
| 121 (0x0038 & q->lspv[0])>> 3 ; | |
| 122 rnd = q->rnd_fir_filter_mem + 20; | |
| 123 for (i = 0; i < 8; i++) { | |
| 124 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); | |
| 125 for (k = 0; k < 20; k++) { | |
| 126 cbseed = 521 * cbseed + 259; | |
| 127 *rnd = (int16_t)cbseed; | |
| 128 | |
| 129 // FIR filter | |
| 130 fir_filter_value = 0.0; | |
| 131 for (j = 0; j < 10; j++) | |
| 132 fir_filter_value += qcelp_rnd_fir_coefs[j ] * (rnd[-j ] + rnd[-20+j]); | |
| 133 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10]; | |
| 134 | |
| 135 *cdn_vector++ = tmp_gain * fir_filter_value; | |
| 136 rnd++; | |
| 137 } | |
| 138 } | |
| 139 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float)); | |
| 140 break; | |
| 141 case RATE_OCTAVE: | |
| 142 cbseed = q->first16bits; | |
| 143 for (i = 0; i < 8; i++) { | |
| 144 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); | |
| 145 for (j = 0; j < 20; j++) { | |
| 146 cbseed = 521 * cbseed + 259; | |
| 147 *cdn_vector++ = tmp_gain * (int16_t)cbseed; | |
| 148 } | |
| 149 } | |
| 150 break; | |
| 151 case I_F_Q: | |
| 152 cbseed = -44; // random codebook index | |
| 153 for (i = 0; i < 4; i++) { | |
| 154 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; | |
| 155 for (j = 0; j < 40; j++) | |
| 156 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127]; | |
| 157 } | |
| 158 break; | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 /** | |
| 163 * Apply generic gain control. | |
| 164 * | |
| 165 * @param v_out output vector | |
| 166 * @param v_in gain-controlled vector | |
| 167 * @param v_ref vector to control gain of | |
| 168 * | |
| 169 * FIXME: If v_ref is a zero vector, it energy is zero | |
| 170 * and the behavior of the gain control is | |
| 171 * undefined in the specs. | |
| 172 * | |
| 173 * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6 | |
| 174 */ | |
| 175 static void apply_gain_ctrl(float *v_out, | |
| 176 const float *v_ref, | |
| 177 const float *v_in) { | |
| 178 int i, j, len; | |
| 179 float scalefactor; | |
| 180 | |
| 181 for (i = 0, j = 0; i < 4; i++) { | |
| 182 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40); | |
| 183 if (scalefactor) | |
| 184 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40) / scalefactor); | |
| 185 else | |
| 186 av_log_missing_feature(NULL, "Zero energy for gain control", 1); | |
| 187 for (len = j + 40; j < len; j++) | |
| 188 v_out[j] = scalefactor * v_in[j]; | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 /** | |
| 8096 | 193 * Apply filter in pitch-subframe steps. |
| 194 * | |
| 195 * @param memory buffer for the previous state of the filter | |
| 196 * - must be able to contain 303 elements | |
| 197 * - the 143 first elements are from the previous state | |
| 198 * - the next 160 are for output | |
| 199 * @param v_in input filter vector | |
| 200 * @param gain per-subframe gain array, each element is between 0.0 and 2.0 | |
| 201 * @param lag per-subframe lag array, each element is | |
| 202 * - between 16 and 143 if its corresponding pfrac is 0, | |
| 203 * - between 16 and 139 otherwise | |
| 204 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 otherwise | |
| 205 * | |
| 206 * @return filter output vector | |
| 207 */ | |
| 8150 | 208 static const float *do_pitchfilter(float memory[303], const float v_in[160], |
| 209 const float gain[4], const uint8_t *lag, | |
| 210 const uint8_t pfrac[4]) | |
| 211 { | |
| 8096 | 212 int i, j; |
| 213 float *v_lag, *v_out; | |
| 214 const float *v_len; | |
| 215 | |
| 216 v_out = memory + 143; // Output vector starts at memory[143]. | |
| 217 | |
| 8150 | 218 for(i=0; i<4; i++) |
| 219 { | |
| 220 if(gain[i]) | |
| 221 { | |
| 8096 | 222 v_lag = memory + 143 + 40 * i - lag[i]; |
| 8150 | 223 for(v_len=v_in+40; v_in<v_len; v_in++) |
| 224 { | |
| 225 if(pfrac[i]) // If it is a fractional lag... | |
| 226 { | |
| 227 for(j=0, *v_out=0.; j<4; j++) | |
| 8096 | 228 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]); |
| 8150 | 229 }else |
| 8096 | 230 *v_out = *v_lag; |
| 231 | |
| 232 *v_out = *v_in + gain[i] * *v_out; | |
| 233 | |
| 234 v_lag++; | |
| 235 v_out++; | |
| 236 } | |
| 8150 | 237 }else |
| 238 { | |
| 8096 | 239 memcpy(v_out, v_in, 40 * sizeof(float)); |
| 240 v_in += 40; | |
| 241 v_out += 40; | |
| 242 } | |
| 8150 | 243 } |
| 8096 | 244 |
| 245 memmove(memory, memory + 160, 143 * sizeof(float)); | |
| 246 return memory + 143; | |
| 247 } | |
| 248 | |
| 8127 | 249 /** |
| 250 * Interpolates LSP frequencies and computes LPC coefficients | |
| 251 * for a given framerate & pitch subframe. | |
| 252 * | |
| 253 * TIA/EIA/IS-733 2.4.3.3.4 | |
| 254 * | |
| 255 * @param q the context | |
| 256 * @param curr_lspf LSP frequencies vector of the current frame | |
| 257 * @param lpc float vector for the resulting LPC | |
| 258 * @param subframe_num frame number in decoded stream | |
| 259 */ | |
| 8150 | 260 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc, |
| 261 const int subframe_num) | |
| 262 { | |
| 8127 | 263 float interpolated_lspf[10]; |
| 264 float weight; | |
| 265 | |
| 8150 | 266 if(q->framerate >= RATE_QUARTER) |
| 8127 | 267 weight = 0.25 * (subframe_num + 1); |
| 8150 | 268 else if(q->framerate == RATE_OCTAVE && !subframe_num) |
| 8127 | 269 weight = 0.625; |
| 8150 | 270 else |
| 8127 | 271 weight = 1.0; |
| 272 | |
| 8150 | 273 if(weight != 1.0) |
| 274 { | |
| 275 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, | |
| 276 weight, 1.0 - weight, 10); | |
| 8145 | 277 qcelp_lspf2lpc(interpolated_lspf, lpc); |
| 8150 | 278 }else if(q->framerate >= RATE_QUARTER || (q->framerate == I_F_Q && !subframe_num)) |
| 8145 | 279 qcelp_lspf2lpc(curr_lspf, lpc); |
| 8127 | 280 } |
| 281 | |
| 8150 | 282 static int buf_size2framerate(const int buf_size) |
| 283 { | |
| 284 switch(buf_size) | |
| 285 { | |
| 286 case 35: | |
| 287 return RATE_FULL; | |
| 288 case 17: | |
| 289 return RATE_HALF; | |
| 290 case 8: | |
| 291 return RATE_QUARTER; | |
| 292 case 4: | |
| 293 return RATE_OCTAVE; | |
| 294 case 1: | |
| 295 return SILENCE; | |
| 8123 | 296 } |
| 8150 | 297 |
| 8123 | 298 return -1; |
| 299 } | |
| 300 | |
| 8096 | 301 static void warn_insufficient_frame_quality(AVCodecContext *avctx, |
| 8150 | 302 const char *message) |
| 303 { | |
| 304 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, | |
| 305 message); | |
| 8096 | 306 } |
| 8127 | 307 |
| 308 AVCodec qcelp_decoder = | |
| 309 { | |
| 310 .name = "qcelp", | |
| 311 .type = CODEC_TYPE_AUDIO, | |
| 312 .id = CODEC_ID_QCELP, | |
| 313 .init = qcelp_decode_init, | |
| 314 .decode = qcelp_decode_frame, | |
| 315 .priv_data_size = sizeof(QCELPContext), | |
| 316 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"), | |
| 317 }; |
