Mercurial > libavcodec.hg
annotate qcelpdec.c @ 8227:596677256482 libavcodec
Implement the fields rc_max_available_vbv_use and
rc_min_vbv_overflow_use in AVCodecContext, and use their values in the
ratecontrol code rather than hardcoded ones.
See the thread: "[RFC] ratecontrol buffer size magic".
Patch by Baptiste Coudurier.
author | stefano |
---|---|
date | Sat, 29 Nov 2008 14:08:48 +0000 |
parents | c04182909bd8 |
children | 72949bacc1b9 |
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 */ | |
8192 | 59 static av_cold int qcelp_decode_init(AVCodecContext *avctx) |
60 { | |
8145 | 61 QCELPContext *q = avctx->priv_data; |
62 int i; | |
63 | |
64 avctx->sample_fmt = SAMPLE_FMT_FLT; | |
65 | |
66 for (i = 0; i < 10; i++) | |
67 q->prev_lspf[i] = (i + 1) / 11.; | |
68 | |
69 return 0; | |
70 } | |
71 | |
72 /** | |
8191 | 73 * Decodes the 10 quantized LSP frequencies from the LSPV/LSP |
74 * transmission codes of any bitrate and checks for badly received packets. | |
75 * | |
76 * @param q the context | |
77 * @param lspf line spectral pair frequencies | |
78 * | |
79 * @return 0 on success, -1 if the packet is badly received | |
80 * | |
81 * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3 | |
82 */ | |
8192 | 83 static int decode_lspf(QCELPContext *q, float *lspf) |
84 { | |
8191 | 85 int i; |
86 float tmp_lspf; | |
87 | |
8192 | 88 if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) |
89 { | |
8191 | 90 float smooth; |
91 const float *predictors = (q->prev_bitrate != RATE_OCTAVE && | |
92 q->prev_bitrate != I_F_Q ? q->prev_lspf | |
93 : q->predictor_lspf); | |
94 | |
8192 | 95 if(q->bitrate == RATE_OCTAVE) |
96 { | |
8191 | 97 q->octave_count++; |
98 | |
8192 | 99 for(i=0; i<10; i++) |
100 { | |
8191 | 101 q->predictor_lspf[i] = |
102 lspf[i] = (q->lspv[i] ? QCELP_LSP_SPREAD_FACTOR | |
103 : -QCELP_LSP_SPREAD_FACTOR) | |
104 + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR | |
105 + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11); | |
106 } | |
107 smooth = (q->octave_count < 10 ? .875 : 0.1); | |
8192 | 108 }else |
109 { | |
8191 | 110 float erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR; |
111 | |
112 assert(q->bitrate == I_F_Q); | |
113 | |
8192 | 114 if(q->erasure_count > 1) |
8191 | 115 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7); |
116 | |
8192 | 117 for(i=0; i<10; i++) |
118 { | |
8191 | 119 q->predictor_lspf[i] = |
120 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11 | |
121 + erasure_coeff * predictors[i]; | |
122 } | |
123 smooth = 0.125; | |
124 } | |
125 | |
126 // Check the stability of the LSP frequencies. | |
127 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR); | |
8192 | 128 for(i=1; i<10; i++) |
8191 | 129 lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR)); |
130 | |
131 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR)); | |
8192 | 132 for(i=9; i>0; i--) |
8191 | 133 lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR)); |
134 | |
135 // Low-pass filter the LSP frequencies. | |
8192 | 136 weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10); |
137 }else | |
138 { | |
8191 | 139 q->octave_count = 0; |
140 | |
141 tmp_lspf = 0.; | |
8192 | 142 for(i=0; i<5 ; i++) |
143 { | |
8191 | 144 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][0] * 0.0001; |
145 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][1] * 0.0001; | |
146 } | |
147 | |
148 // Check for badly received packets. | |
8192 | 149 if(q->bitrate == RATE_QUARTER) |
150 { | |
151 if(lspf[9] <= .70 || lspf[9] >= .97) | |
8191 | 152 return -1; |
8192 | 153 for(i=3; i<10; i++) |
154 if(fabs(lspf[i] - lspf[i-2]) < .08) | |
8191 | 155 return -1; |
8192 | 156 }else |
157 { | |
158 if(lspf[9] <= .66 || lspf[9] >= .985) | |
8191 | 159 return -1; |
8192 | 160 for(i=4; i<10; i++) |
8191 | 161 if (fabs(lspf[i] - lspf[i-4]) < .0931) |
162 return -1; | |
163 } | |
164 } | |
165 return 0; | |
166 } | |
167 | |
168 /** | |
8192 | 169 * If the received packet is Rate 1/4 a further sanity check is made of the |
170 * codebook gain. | |
8191 | 171 * |
172 * @param cbgain the unpacked cbgain array | |
173 * @return -1 if the sanity check fails, 0 otherwise | |
174 * | |
175 * TIA/EIA/IS-733 2.4.8.7.3 | |
176 */ | |
8192 | 177 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain) |
178 { | |
8193 | 179 int i, prev_diff=0; |
8191 | 180 |
8192 | 181 for(i=1; i<5; i++) |
182 { | |
183 int diff = cbgain[i] - cbgain[i-1]; | |
184 if(FFABS(diff) > 10) | |
185 return -1; | |
186 else if(FFABS(diff - prev_diff) > 12) | |
187 return -1; | |
188 prev_diff = diff; | |
8193 | 189 } |
190 return 0; | |
8191 | 191 } |
192 | |
193 /** | |
8145 | 194 * Computes the scaled codebook vector Cdn From INDEX and GAIN |
195 * for all rates. | |
196 * | |
197 * The specification lacks some information here. | |
198 * | |
199 * TIA/EIA/IS-733 has an omission on the codebook index determination | |
200 * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says | |
201 * you have to subtract the decoded index parameter from the given scaled | |
202 * codebook vector index 'n' to get the desired circular codebook index, but | |
203 * it does not mention that you have to clamp 'n' to [0-9] in order to get | |
204 * RI-compliant results. | |
205 * | |
206 * The reason for this mistake seems to be the fact they forgot to mention you | |
207 * have to do these calculations per codebook subframe and adjust given | |
208 * equation values accordingly. | |
209 * | |
210 * @param q the context | |
211 * @param gain array holding the 4 pitch subframe gain values | |
212 * @param cdn_vector array for the generated scaled codebook vector | |
213 */ | |
8192 | 214 static void compute_svector(const QCELPContext *q, const float *gain, |
215 float *cdn_vector) | |
216 { | |
8145 | 217 int i, j, k; |
218 uint16_t cbseed, cindex; | |
219 float *rnd, tmp_gain, fir_filter_value; | |
220 | |
8192 | 221 switch(q->bitrate) |
222 { | |
223 case RATE_FULL: | |
224 for(i=0; i<16; i++) | |
225 { | |
226 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; | |
227 cindex = -q->cindex[i]; | |
228 for(j=0; j<10; j++) | |
229 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127]; | |
230 } | |
8145 | 231 break; |
8192 | 232 case RATE_HALF: |
233 for(i=0; i<4; i++) | |
234 { | |
235 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO; | |
236 cindex = -q->cindex[i]; | |
237 for (j = 0; j < 40; j++) | |
8145 | 238 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127]; |
8192 | 239 } |
8145 | 240 break; |
8192 | 241 case RATE_QUARTER: |
242 cbseed = (0x0003 & q->lspv[4])<<14 | | |
243 (0x003F & q->lspv[3])<< 8 | | |
244 (0x0060 & q->lspv[2])<< 1 | | |
245 (0x0007 & q->lspv[1])<< 3 | | |
246 (0x0038 & q->lspv[0])>> 3 ; | |
247 rnd = q->rnd_fir_filter_mem + 20; | |
248 for(i=0; i<8; i++) | |
249 { | |
250 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); | |
251 for(k=0; k<20; k++) | |
252 { | |
253 cbseed = 521 * cbseed + 259; | |
254 *rnd = (int16_t)cbseed; | |
8145 | 255 |
8192 | 256 // FIR filter |
257 fir_filter_value = 0.0; | |
258 for(j=0; j<10; j++) | |
259 fir_filter_value += qcelp_rnd_fir_coefs[j ] | |
260 * (rnd[-j ] + rnd[-20+j]); | |
8145 | 261 |
8192 | 262 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10]; |
263 *cdn_vector++ = tmp_gain * fir_filter_value; | |
264 rnd++; | |
265 } | |
8145 | 266 } |
8192 | 267 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float)); |
8145 | 268 break; |
8192 | 269 case RATE_OCTAVE: |
270 cbseed = q->first16bits; | |
271 for(i=0; i<8; i++) | |
272 { | |
273 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0); | |
274 for(j=0; j<20; j++) | |
275 { | |
276 cbseed = 521 * cbseed + 259; | |
277 *cdn_vector++ = tmp_gain * (int16_t)cbseed; | |
278 } | |
8145 | 279 } |
280 break; | |
8192 | 281 case I_F_Q: |
282 cbseed = -44; // random codebook index | |
283 for(i=0; i<4; i++) | |
284 { | |
285 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; | |
286 for(j=0; j<40; j++) | |
287 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127]; | |
288 } | |
8145 | 289 break; |
290 } | |
291 } | |
292 | |
293 /** | |
294 * Apply generic gain control. | |
295 * | |
296 * @param v_out output vector | |
297 * @param v_in gain-controlled vector | |
298 * @param v_ref vector to control gain of | |
299 * | |
300 * FIXME: If v_ref is a zero vector, it energy is zero | |
301 * and the behavior of the gain control is | |
302 * undefined in the specs. | |
303 * | |
304 * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6 | |
305 */ | |
8192 | 306 static void apply_gain_ctrl(float *v_out, const float *v_ref, |
307 const float *v_in) | |
308 { | |
8145 | 309 int i, j, len; |
310 float scalefactor; | |
311 | |
8192 | 312 for(i=0, j=0; i<4; i++) |
313 { | |
8145 | 314 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40); |
8192 | 315 if(scalefactor) |
316 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40) | |
317 / scalefactor); | |
8145 | 318 else |
319 av_log_missing_feature(NULL, "Zero energy for gain control", 1); | |
8192 | 320 for(len=j+40; j<len; j++) |
8145 | 321 v_out[j] = scalefactor * v_in[j]; |
322 } | |
323 } | |
324 | |
325 /** | |
8096 | 326 * Apply filter in pitch-subframe steps. |
327 * | |
328 * @param memory buffer for the previous state of the filter | |
329 * - must be able to contain 303 elements | |
330 * - the 143 first elements are from the previous state | |
331 * - the next 160 are for output | |
332 * @param v_in input filter vector | |
333 * @param gain per-subframe gain array, each element is between 0.0 and 2.0 | |
334 * @param lag per-subframe lag array, each element is | |
335 * - between 16 and 143 if its corresponding pfrac is 0, | |
336 * - between 16 and 139 otherwise | |
8192 | 337 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 |
338 * otherwise | |
8096 | 339 * |
340 * @return filter output vector | |
341 */ | |
8150 | 342 static const float *do_pitchfilter(float memory[303], const float v_in[160], |
343 const float gain[4], const uint8_t *lag, | |
344 const uint8_t pfrac[4]) | |
345 { | |
8096 | 346 int i, j; |
347 float *v_lag, *v_out; | |
348 const float *v_len; | |
349 | |
350 v_out = memory + 143; // Output vector starts at memory[143]. | |
351 | |
8150 | 352 for(i=0; i<4; i++) |
353 { | |
354 if(gain[i]) | |
355 { | |
8096 | 356 v_lag = memory + 143 + 40 * i - lag[i]; |
8150 | 357 for(v_len=v_in+40; v_in<v_len; v_in++) |
358 { | |
359 if(pfrac[i]) // If it is a fractional lag... | |
360 { | |
361 for(j=0, *v_out=0.; j<4; j++) | |
8096 | 362 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]); |
8150 | 363 }else |
8096 | 364 *v_out = *v_lag; |
365 | |
366 *v_out = *v_in + gain[i] * *v_out; | |
367 | |
368 v_lag++; | |
369 v_out++; | |
370 } | |
8150 | 371 }else |
372 { | |
8096 | 373 memcpy(v_out, v_in, 40 * sizeof(float)); |
374 v_in += 40; | |
375 v_out += 40; | |
376 } | |
8150 | 377 } |
8096 | 378 |
379 memmove(memory, memory + 160, 143 * sizeof(float)); | |
380 return memory + 143; | |
381 } | |
382 | |
8127 | 383 /** |
384 * Interpolates LSP frequencies and computes LPC coefficients | |
8191 | 385 * for a given bitrate & pitch subframe. |
8127 | 386 * |
387 * TIA/EIA/IS-733 2.4.3.3.4 | |
388 * | |
389 * @param q the context | |
390 * @param curr_lspf LSP frequencies vector of the current frame | |
391 * @param lpc float vector for the resulting LPC | |
392 * @param subframe_num frame number in decoded stream | |
393 */ | |
8150 | 394 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc, |
395 const int subframe_num) | |
396 { | |
8127 | 397 float interpolated_lspf[10]; |
398 float weight; | |
399 | |
8191 | 400 if(q->bitrate >= RATE_QUARTER) |
8127 | 401 weight = 0.25 * (subframe_num + 1); |
8191 | 402 else if(q->bitrate == RATE_OCTAVE && !subframe_num) |
8127 | 403 weight = 0.625; |
8150 | 404 else |
8127 | 405 weight = 1.0; |
406 | |
8150 | 407 if(weight != 1.0) |
408 { | |
409 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, | |
410 weight, 1.0 - weight, 10); | |
8145 | 411 qcelp_lspf2lpc(interpolated_lspf, lpc); |
8191 | 412 }else if(q->bitrate >= RATE_QUARTER || (q->bitrate == I_F_Q && !subframe_num)) |
8145 | 413 qcelp_lspf2lpc(curr_lspf, lpc); |
8127 | 414 } |
415 | |
8191 | 416 static int buf_size2bitrate(const int buf_size) |
8150 | 417 { |
418 switch(buf_size) | |
419 { | |
420 case 35: | |
421 return RATE_FULL; | |
422 case 17: | |
423 return RATE_HALF; | |
424 case 8: | |
425 return RATE_QUARTER; | |
426 case 4: | |
427 return RATE_OCTAVE; | |
428 case 1: | |
429 return SILENCE; | |
8123 | 430 } |
8150 | 431 |
8123 | 432 return -1; |
433 } | |
434 | |
8096 | 435 static void warn_insufficient_frame_quality(AVCodecContext *avctx, |
8150 | 436 const char *message) |
437 { | |
438 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, | |
439 message); | |
8096 | 440 } |
8127 | 441 |
442 AVCodec qcelp_decoder = | |
443 { | |
444 .name = "qcelp", | |
445 .type = CODEC_TYPE_AUDIO, | |
446 .id = CODEC_ID_QCELP, | |
447 .init = qcelp_decode_init, | |
448 .decode = qcelp_decode_frame, | |
449 .priv_data_size = sizeof(QCELPContext), | |
450 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"), | |
451 }; |