comparison qcelpdec.c @ 8191:cc1e8c59f1e8 libavcodec

More OKed parts of the QCELP decoder patch by Kenan Gillet, kenan.gillet gmail com
author vitor
date Fri, 21 Nov 2008 18:39:16 +0000
parents e2c068cb210a
children d6b40db1a747
comparison
equal deleted inserted replaced
8190:e596efd84d27 8191:cc1e8c59f1e8
64 64
65 for (i = 0; i < 10; i++) 65 for (i = 0; i < 10; i++)
66 q->prev_lspf[i] = (i + 1) / 11.; 66 q->prev_lspf[i] = (i + 1) / 11.;
67 67
68 return 0; 68 return 0;
69 }
70
71 /**
72 * Decodes the 10 quantized LSP frequencies from the LSPV/LSP
73 * transmission codes of any bitrate and checks for badly received packets.
74 *
75 * @param q the context
76 * @param lspf line spectral pair frequencies
77 *
78 * @return 0 on success, -1 if the packet is badly received
79 *
80 * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3
81 */
82 static int decode_lspf(QCELPContext *q,
83 float *lspf) {
84 int i;
85 float tmp_lspf;
86
87 if (q->bitrate == RATE_OCTAVE ||
88 q->bitrate == I_F_Q) {
89 float smooth;
90 const float *predictors = (q->prev_bitrate != RATE_OCTAVE &&
91 q->prev_bitrate != I_F_Q ? q->prev_lspf
92 : q->predictor_lspf);
93
94 if (q->bitrate == RATE_OCTAVE) {
95 q->octave_count++;
96
97 for (i = 0; i < 10; i++) {
98 q->predictor_lspf[i] =
99 lspf[i] = (q->lspv[i] ? QCELP_LSP_SPREAD_FACTOR
100 : -QCELP_LSP_SPREAD_FACTOR)
101 + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR
102 + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11);
103 }
104 smooth = (q->octave_count < 10 ? .875 : 0.1);
105 } else {
106 float erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
107
108 assert(q->bitrate == I_F_Q);
109
110 if (q->erasure_count > 1)
111 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7);
112
113 for (i = 0; i < 10; i++) {
114 q->predictor_lspf[i] =
115 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11
116 + erasure_coeff * predictors[i];
117 }
118 smooth = 0.125;
119 }
120
121 // Check the stability of the LSP frequencies.
122 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
123 for (i = 1; i < 10; i++)
124 lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR));
125
126 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR));
127 for (i = 9; i > 0; i--)
128 lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR));
129
130 // Low-pass filter the LSP frequencies.
131 weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0 - smooth, 10);
132 } else {
133 q->octave_count = 0;
134
135 tmp_lspf = 0.;
136 for (i = 0; i < 5 ; i++) {
137 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][0] * 0.0001;
138 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][1] * 0.0001;
139 }
140
141 // Check for badly received packets.
142 if (q->bitrate == RATE_QUARTER) {
143 if (lspf[9] <= .70 || lspf[9] >= .97)
144 return -1;
145 for (i = 3; i < 10; i++)
146 if (fabs(lspf[i] - lspf[i-2]) < .08)
147 return -1;
148 } else {
149 if (lspf[9] <= .66 || lspf[9] >= .985)
150 return -1;
151 for (i = 4; i < 10; i++)
152 if (fabs(lspf[i] - lspf[i-4]) < .0931)
153 return -1;
154 }
155 }
156 return 0;
157 }
158
159 /**
160 * If the received packet is Rate 1/4 a further sanity check is made of the codebook gain.
161 *
162 * @param cbgain the unpacked cbgain array
163 * @return -1 if the sanity check fails, 0 otherwise
164 *
165 * TIA/EIA/IS-733 2.4.8.7.3
166 */
167 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain) {
168 int i, prev_diff=0;
169
170 for (i = 1; i < 5; i++) {
171 int diff = cbgain[i] - cbgain[i-1];
172 if (FFABS(diff) > 10)
173 return -1;
174 else if (FFABS(diff - prev_diff) > 12)
175 return -1;
176 prev_diff = diff;
177 }
178 return 0;
69 } 179 }
70 180
71 /** 181 /**
72 * Computes the scaled codebook vector Cdn From INDEX and GAIN 182 * Computes the scaled codebook vector Cdn From INDEX and GAIN
73 * for all rates. 183 * for all rates.
94 float *cdn_vector) { 204 float *cdn_vector) {
95 int i, j, k; 205 int i, j, k;
96 uint16_t cbseed, cindex; 206 uint16_t cbseed, cindex;
97 float *rnd, tmp_gain, fir_filter_value; 207 float *rnd, tmp_gain, fir_filter_value;
98 208
99 switch (q->framerate) { 209 switch (q->bitrate) {
100 case RATE_FULL: 210 case RATE_FULL:
101 for (i = 0; i < 16; i++) { 211 for (i = 0; i < 16; i++) {
102 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO; 212 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
103 cindex = -q->cindex[i]; 213 cindex = -q->cindex[i];
104 for (j = 0; j < 10; j++) 214 for (j = 0; j < 10; j++)
246 return memory + 143; 356 return memory + 143;
247 } 357 }
248 358
249 /** 359 /**
250 * Interpolates LSP frequencies and computes LPC coefficients 360 * Interpolates LSP frequencies and computes LPC coefficients
251 * for a given framerate & pitch subframe. 361 * for a given bitrate & pitch subframe.
252 * 362 *
253 * TIA/EIA/IS-733 2.4.3.3.4 363 * TIA/EIA/IS-733 2.4.3.3.4
254 * 364 *
255 * @param q the context 365 * @param q the context
256 * @param curr_lspf LSP frequencies vector of the current frame 366 * @param curr_lspf LSP frequencies vector of the current frame
261 const int subframe_num) 371 const int subframe_num)
262 { 372 {
263 float interpolated_lspf[10]; 373 float interpolated_lspf[10];
264 float weight; 374 float weight;
265 375
266 if(q->framerate >= RATE_QUARTER) 376 if(q->bitrate >= RATE_QUARTER)
267 weight = 0.25 * (subframe_num + 1); 377 weight = 0.25 * (subframe_num + 1);
268 else if(q->framerate == RATE_OCTAVE && !subframe_num) 378 else if(q->bitrate == RATE_OCTAVE && !subframe_num)
269 weight = 0.625; 379 weight = 0.625;
270 else 380 else
271 weight = 1.0; 381 weight = 1.0;
272 382
273 if(weight != 1.0) 383 if(weight != 1.0)
274 { 384 {
275 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, 385 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
276 weight, 1.0 - weight, 10); 386 weight, 1.0 - weight, 10);
277 qcelp_lspf2lpc(interpolated_lspf, lpc); 387 qcelp_lspf2lpc(interpolated_lspf, lpc);
278 }else if(q->framerate >= RATE_QUARTER || (q->framerate == I_F_Q && !subframe_num)) 388 }else if(q->bitrate >= RATE_QUARTER || (q->bitrate == I_F_Q && !subframe_num))
279 qcelp_lspf2lpc(curr_lspf, lpc); 389 qcelp_lspf2lpc(curr_lspf, lpc);
280 } 390 }
281 391
282 static int buf_size2framerate(const int buf_size) 392 static int buf_size2bitrate(const int buf_size)
283 { 393 {
284 switch(buf_size) 394 switch(buf_size)
285 { 395 {
286 case 35: 396 case 35:
287 return RATE_FULL; 397 return RATE_FULL;