Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local constants
50 : *-------------------------------------------------------------------*/
51 :
52 : #define RANGE 64
53 :
54 : /*---------------------------------------------------------------------*
55 : * Es_pred_enc()
56 : *
57 : * Calculation and quantization of average predicted innovation energy to be
58 : *---------------------------------------------------------------------*/
59 :
60 1425286 : void Es_pred_enc(
61 : float *Es_pred, /* o : predicited scaled innovation energy */
62 : int16_t *Es_pred_indice, /* o : indice corresponding to above parameter */
63 : const int16_t L_frame, /* i : length of the frame */
64 : const int16_t L_subfr, /* i : length of the subframe */
65 : const float *res, /* i : residual signal */
66 : const float *voicing, /* i : normalized correlation in three 1/2frames */
67 : const int16_t nb_bits, /* i : allocated number of bits */
68 : const int16_t no_ltp /* i : no_ltp flag */
69 : )
70 : {
71 : int16_t i, i_subfr, size;
72 : float tmp, dist, mean_ener_code, ener;
73 : float weight;
74 : const float *qua_table;
75 :
76 : /*----------------------------------------------------------*
77 : * calculate the average residual signal energy
78 : *----------------------------------------------------------*/
79 1425286 : if ( L_frame == L_FRAME )
80 : {
81 645971 : weight = 0.25f;
82 : }
83 : else /* L_frame == L_FRAME16k */
84 : {
85 779315 : weight = 0.2f;
86 : }
87 :
88 1425286 : mean_ener_code = 0.0f;
89 7905745 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_subfr )
90 : {
91 : /* calculate the energy of residual signal */
92 6480459 : ener = sum2_f( &res[i_subfr], L_subfr ) + 0.01f;
93 6480459 : ener = 10 * (float) log10( ener / ( (float) L_subfr ) );
94 6480459 : if ( ( ener < 0 ) && !( no_ltp ) )
95 : {
96 238791 : ener = 0;
97 : }
98 :
99 : /* update the average energy of residual signal */
100 6480459 : mean_ener_code += weight * ener;
101 : }
102 :
103 1425286 : if ( !no_ltp )
104 : {
105 : /*----------------------------------------------------------*
106 : * subtract an estimate of adaptive codebook contribution
107 : *----------------------------------------------------------*/
108 :
109 1405455 : mean_ener_code -= 10.0f * ( 0.5f * voicing[0] + 0.5f * voicing[1] );
110 :
111 : /*----------------------------------------------------------*
112 : * quantize the average predicted innovation energy
113 : *----------------------------------------------------------*/
114 1405455 : switch ( nb_bits )
115 : {
116 1191445 : case 5:
117 : {
118 1191445 : qua_table = Es_pred_qua_5b;
119 1191445 : break;
120 : }
121 196396 : case 4:
122 : {
123 196396 : qua_table = Es_pred_qua_4b;
124 196396 : break;
125 : }
126 17614 : case 3:
127 : {
128 17614 : qua_table = Es_pred_qua_3b;
129 17614 : break;
130 : }
131 0 : default:
132 : {
133 0 : qua_table = Es_pred_qua_5b;
134 0 : break;
135 : }
136 : }
137 : }
138 : else
139 : {
140 19831 : qua_table = Es_pred_qua_4b_no_ltp;
141 : }
142 :
143 : /* select codebook, size and number of bits */
144 1425286 : size = 1 << nb_bits;
145 :
146 : /* find the nearest neighbour (codevector) */
147 1425286 : tmp = 1e30f;
148 1425286 : *Es_pred_indice = 0;
149 43152070 : for ( i = 0; i < size; i++ )
150 : {
151 41726784 : dist = (float) fabs( mean_ener_code - qua_table[i] );
152 41726784 : if ( dist < tmp )
153 : {
154 20771351 : tmp = dist;
155 20771351 : *Es_pred = qua_table[i];
156 20771351 : *Es_pred_indice = i;
157 : }
158 : }
159 :
160 1425286 : return;
161 : }
162 :
163 :
164 : /*-------------------------------------------------------------------*
165 : * gain_enc_amr_wb()
166 : *
167 : * Quantization of pitch and codebook gains (used also in AMR-WB IO mode)
168 : * MA prediction is performed on the innovation energy (in dB with mean removed).
169 : * An initial predicted gain, gcode0, is first determined and the correction
170 : * factor alpha = g_code / gcode0 is quantized.
171 : * The pitch gain and the correction factor are vector quantized and the
172 : * mean-squared weighted error criterion is used in the quantizer search.
173 : *-------------------------------------------------------------------*/
174 :
175 17608 : void gain_enc_amr_wb(
176 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
177 : const float *xn, /* i : target vector */
178 : const float *y1, /* i : zero-memory filtered adaptive excitation */
179 : const float *y2, /* i : zero-memory filtered algebraic codebook excitation */
180 : const float *code, /* i : algebraic excitation */
181 : const int32_t core_brate, /* i : core bitrate */
182 : float *gain_pit, /* i/o: pitch gain / Quantized pitch gain */
183 : float *gain_code, /* o : quantized codebook gain */
184 : float *gain_inov, /* o : gain of the innovation (used for normalization) */
185 : float *norm_gain_code, /* o : norm. gain of the codebook excitation */
186 : float *g_corr, /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
187 : const int16_t clip_gain, /* i : gain pitch clipping flag (1 = clipping) */
188 : float *past_qua_en /* i/o: gain quantization memory (4 words) */
189 : )
190 : {
191 : int16_t index, i, j, min_ind, size, nBits;
192 : float dist, dist_min, g_pitch, g_code, qua_en, gcode0;
193 : const float *p, *t_qua_gain;
194 :
195 : /*-----------------------------------------------------------------*
196 : * gain computation correlations
197 : * find raw innovation energy
198 : *-----------------------------------------------------------------*/
199 :
200 17608 : E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
201 17608 : g_corr[2] += 0.01F;
202 17608 : g_corr[3] -= 0.02F;
203 17608 : g_corr[4] += 0.02F;
204 17608 : *gain_inov = 1.0f / (float) sqrt( ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR );
205 :
206 : /*-----------------------------------------------------------------*
207 : * find the initial quantization pitch index
208 : * set gains search range
209 : *-----------------------------------------------------------------*/
210 :
211 17608 : if ( core_brate < ACELP_12k65 )
212 : {
213 2560 : t_qua_gain = t_qua_gain6b;
214 2560 : nBits = 6;
215 2560 : min_ind = 0;
216 2560 : size = RANGE;
217 2560 : if ( clip_gain == 1 )
218 : {
219 0 : size -= 16; /* limit pitch gain to 1.0 */
220 : }
221 : }
222 : else
223 : {
224 15048 : t_qua_gain = t_qua_gain7b;
225 15048 : nBits = 7;
226 15048 : p = t_qua_gain7b + RANGE; /* pt at 1/4th of table */
227 15048 : j = NB_QUA_GAIN7B - RANGE;
228 15048 : if ( clip_gain == 1 )
229 : {
230 493 : j -= 27; /* limit pitch gain to 1.0 */
231 : }
232 :
233 15048 : min_ind = 0;
234 15048 : g_pitch = *gain_pit;
235 :
236 :
237 964809 : for ( i = 0; i < j; i++, p += 2 )
238 : {
239 949761 : if ( g_pitch > *p )
240 : {
241 469006 : min_ind++;
242 : }
243 : }
244 15048 : size = RANGE;
245 : }
246 :
247 : /*-----------------------------------------------------------------*
248 : * predicted code gain
249 : *-----------------------------------------------------------------*/
250 :
251 : /* start with predicting code energy in dB */
252 17608 : gcode0 = MEAN_ENER;
253 88040 : for ( i = 0; i < GAIN_PRED_ORDER; i++ )
254 : {
255 70432 : gcode0 += pred_gain[i] * past_qua_en[i];
256 : }
257 17608 : gcode0 += (float) ( 20.0 * log10( *gain_inov ) );
258 :
259 : /* convert from energy in dB to gain */
260 17608 : gcode0 = (float) pow( 10.0, gcode0 / 20.0 );
261 :
262 : /*-----------------------------------------------------------------*
263 : * search the codebook
264 : *-----------------------------------------------------------------*/
265 :
266 17608 : dist_min = 3.402823466e+38F;
267 17608 : p = t_qua_gain + min_ind * 2;
268 :
269 17608 : index = 0;
270 1144520 : for ( i = 0; i < size; i++ )
271 : {
272 1126912 : g_pitch = *p++; /* pitch gain */
273 1126912 : g_code = gcode0 * *p++; /* code gain */
274 1126912 : dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
275 1126912 : if ( dist < dist_min )
276 : {
277 135334 : dist_min = dist;
278 135334 : index = i;
279 : }
280 : }
281 17608 : index = index + min_ind;
282 17608 : *gain_pit = t_qua_gain[index * 2];
283 17608 : qua_en = t_qua_gain[index * 2 + 1];
284 17608 : *gain_code = qua_en * gcode0;
285 :
286 : /*-----------------------------------------------------------------*
287 : * update table of past quantized energies
288 : *-----------------------------------------------------------------*/
289 :
290 70432 : for ( i = GAIN_PRED_ORDER - 1; i > 0; i-- )
291 : {
292 52824 : past_qua_en[i] = past_qua_en[i - 1];
293 : }
294 17608 : past_qua_en[0] = (float) ( 20.0 * log10( qua_en ) );
295 :
296 17608 : push_indice( hBstr, IND_GAIN, index, nBits );
297 :
298 17608 : *norm_gain_code = *gain_code / *gain_inov;
299 :
300 17608 : return;
301 : }
302 :
303 : /*---------------------------------------------------------------------*
304 : * gain_enc_mless()
305 : *
306 : * Quantization of pitch and codebook gains without prediction (memory-less)
307 : * - an initial predicted gain, gcode0, is first determined based on
308 : * the predicted average innovation energy
309 : * - a correction factor gamma = g_code / gcode0 is then vector quantized along with gain_pit
310 : * - the mean-squared weighted error criterion is used for codebook search
311 : *---------------------------------------------------------------------*/
312 :
313 5541367 : void gain_enc_mless(
314 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
315 : const int16_t gains_mode[], /* i : gain bits */
316 : const int16_t element_mode, /* i : element mode */
317 : const int16_t L_frame, /* i : length of the frame */
318 : const int16_t i_subfr, /* i : subframe index */
319 : const int16_t tc_subfr, /* i : TC subframe index */
320 : const float *xn, /* i : target vector */
321 : const float *y1, /* i : zero-memory filtered adaptive excitation */
322 : const float *y2, /* i : zero-memory filtered algebraic codebook excitation */
323 : const float *code, /* i : algebraic excitation */
324 : const float Es_pred, /* i : predicted scaled innovation energy */
325 : float *gain_pit, /* o : quantized pitch gain */
326 : float *gain_code, /* o : quantized codebook gain */
327 : float *gain_inov, /* o : gain of the innovation (used for normalization) */
328 : float *norm_gain_code, /* o : norm. gain of the codebook excitation */
329 : float *g_corr, /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
330 : const int16_t clip_gain /* i : gain pitch clipping flag (1 = clipping) */
331 : )
332 : {
333 : int16_t index, i, size, nBits;
334 : float dist, dist_min, g_pitch, g_code, gcode0, Ei, Ecode;
335 : int16_t nBits2;
336 : float tmp1, tmp2;
337 : const float *p, *qua_table;
338 :
339 : /*-----------------------------------------------------------------*
340 : * calculate the rest of the correlation coefficients
341 : * c2 = <y2,y2>, c3 = -2<xn,y2>, c4 = 2<y1,y2>
342 : *-----------------------------------------------------------------*/
343 :
344 5541367 : E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
345 5541367 : g_corr[2] += 0.01F;
346 5541367 : g_corr[3] -= 0.02F;
347 5541367 : g_corr[4] += 0.02F;
348 :
349 : /*-----------------------------------------------------------------*
350 : * calculate the unscaled innovation energy
351 : * calculate the predicted gain code
352 : *-----------------------------------------------------------------*/
353 :
354 5541367 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
355 5541367 : *gain_inov = 1.0f / (float) sqrt( Ecode );
356 5541367 : Ei = 10 * (float) log10( Ecode );
357 5541367 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
358 :
359 : /*-----------------------------------------------------------------*
360 : * select the codebook, size and number of bits
361 : * set the gains searching range
362 : *-----------------------------------------------------------------*/
363 :
364 5541367 : nBits = gains_mode[i_subfr / L_SUBFR];
365 :
366 5541367 : if ( ( tc_subfr == 3 * L_SUBFR && i_subfr == 3 * L_SUBFR && L_frame == L_FRAME ) ||
367 21176 : ( tc_subfr == 4 * L_SUBFR && i_subfr == 4 * L_SUBFR && L_frame == L_FRAME16k ) )
368 : {
369 : /* in case of attack at the end of the frame, use scalar gain quantizers */
370 44942 : tmp1 = ( g_corr[0] * g_corr[2] ) - ( 0.25f * g_corr[4] * g_corr[4] );
371 44942 : tmp2 = -0.5f * g_corr[1] / tmp1;
372 44942 : tmp1 = -0.5f * g_corr[3] / tmp1;
373 :
374 44942 : *gain_pit = ( g_corr[2] * tmp2 ) - ( 0.5f * g_corr[4] * tmp1 );
375 44942 : *gain_code = ( g_corr[0] * tmp1 ) - ( 0.5f * g_corr[4] * tmp2 );
376 :
377 44942 : *gain_pit = max( G_PITCH_MIN_TC192, min( *gain_pit, G_PITCH_MAX_TC192 ) );
378 :
379 : /* set number of bits for two SQs */
380 44942 : nBits2 = ( nBits + 1 ) >> 1;
381 44942 : nBits = nBits >> 1;
382 :
383 : /* gain_pit Q */
384 44942 : tmp1 = ( G_PITCH_MAX_TC192 - G_PITCH_MIN_TC192 ) / ( ( 1 << nBits ) - 1 ); /* set quantization step */
385 44942 : index = usquant( *gain_pit, gain_pit, G_PITCH_MIN_TC192, tmp1, ( 1 << nBits ) );
386 44942 : push_indice( hBstr, IND_GAIN_PIT, index, nBits );
387 :
388 : /* gain_code Q */
389 44942 : *gain_code /= gcode0;
390 44942 : index = gain_quant( gain_code, G_CODE_MIN_TC192, G_CODE_MAX_TC192, nBits2 );
391 44942 : push_indice( hBstr, IND_GAIN_CODE, index, nBits2 );
392 44942 : *gain_code *= gcode0;
393 : }
394 : else
395 : {
396 5496425 : size = 1 << nBits;
397 :
398 5496425 : switch ( nBits )
399 : {
400 4022 : case 7:
401 : {
402 4022 : qua_table = gain_qua_mless_7b;
403 4022 : if ( clip_gain == 1 )
404 : {
405 0 : size -= 30;
406 : }
407 4022 : break;
408 : }
409 5487824 : case 6:
410 : {
411 5487824 : qua_table = gain_qua_mless_6b;
412 :
413 5487824 : if ( element_mode > EVS_MONO )
414 : {
415 5408840 : qua_table = gain_qua_mless_6b_stereo;
416 : }
417 :
418 5487824 : if ( clip_gain == 1 )
419 : {
420 151428 : size -= 14;
421 : }
422 5487824 : break;
423 : }
424 4579 : case 5:
425 : {
426 4579 : qua_table = gain_qua_mless_5b;
427 4579 : if ( clip_gain == 1 )
428 : {
429 1 : size -= 6;
430 : }
431 4579 : break;
432 : }
433 0 : default:
434 : {
435 0 : qua_table = gain_qua_mless_6b;
436 0 : size = 64;
437 0 : if ( clip_gain == 1 )
438 : {
439 0 : size -= 14;
440 : }
441 0 : break;
442 : }
443 : }
444 :
445 : /* in case of AVQ inactive, limit the gain_pit to 0.65 */
446 5496425 : if ( clip_gain == 2 && nBits == 6 )
447 : {
448 55460 : size -= 36;
449 55460 : nBits--;
450 : }
451 :
452 : /*-----------------------------------------------------------------*
453 : * search for the best quantizer
454 : *-----------------------------------------------------------------*/
455 :
456 5496425 : p = qua_table;
457 5496425 : dist_min = 3.402823466e+38F;
458 5496425 : index = 0;
459 353261947 : for ( i = 0; i < size; i++ )
460 : {
461 347765522 : g_pitch = *p++; /* pitch gain */
462 347765522 : g_code = gcode0 * *p++; /* code gain */
463 347765522 : dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
464 347765522 : if ( dist < dist_min )
465 : {
466 72633294 : dist_min = dist;
467 72633294 : index = i;
468 : }
469 : }
470 5496425 : *gain_pit = qua_table[index * 2];
471 5496425 : *gain_code = qua_table[index * 2 + 1] * gcode0;
472 :
473 5496425 : push_indice( hBstr, IND_GAIN, index, nBits );
474 : }
475 :
476 5541367 : *norm_gain_code = *gain_code / *gain_inov;
477 :
478 5541367 : return;
479 : }
480 :
481 :
482 : /*---------------------------------------------------------------------*
483 : * gain_enc_SQ()
484 : *
485 : * Scalar Quantization of pitch and codebook gains without prediction
486 : * - an initial predicted gain, gcode0, is first determined based on
487 : * the predicted scaled innovation energy
488 : * - a correction factor gamma = g_code / gcode0 is then vector quantized
489 : * along with gain_pit
490 : * - the mean-squared weighted error criterion is used for codebook search
491 : *---------------------------------------------------------------------*/
492 :
493 519622 : void gain_enc_SQ(
494 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
495 : const int16_t gains_mode[], /* i : gain bits */
496 : const int16_t i_subfr, /* i : subframe index */
497 : const float *xn, /* i : target vector */
498 : const float *yy1, /* i : zero-memory filtered adaptive excitation */
499 : const float *y2, /* i : zero-memory filtered algebraic codebook excitation */
500 : const float *code, /* i : algebraic excitation */
501 : const float Es_pred, /* i : predicted scaled innovation energy */
502 : float *gain_pit, /* o : quantized pitch gain */
503 : float *gain_code, /* o : quantized codebook gain */
504 : float *gain_inov, /* o : gain of the innovation (used for normalization) */
505 : float *norm_gain_code, /* o : norm. gain of the codebook excitation */
506 : float *g_corr, /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
507 : const int16_t clip_gain /* i : gain pitch clipping flag (1 = clipping) */
508 : )
509 : {
510 : int16_t index, nBits_pitch, nBits_code;
511 : float g_code, gcode0, Ei, Ecode, tmp1, tmp2;
512 : int16_t tmp16;
513 : /*-----------------------------------------------------------------*
514 : * calculate the rest of the correlation coefficients
515 : * c2 = <y2,y2>, c3 = -2<xn,y2>, c4 = 2<y1,y2>
516 : *-----------------------------------------------------------------*/
517 :
518 519622 : g_corr[1] *= -0.5;
519 519622 : g_corr[2] = dotp( y2, y2, L_SUBFR ) + 0.01f;
520 519622 : g_corr[3] = dotp( xn, y2, L_SUBFR ) - 0.02f;
521 519622 : g_corr[4] = dotp( yy1, y2, L_SUBFR ) + 0.02f;
522 :
523 : /*-----------------------------------------------------------------*
524 : * calculate the unscaled innovation energy
525 : * calculate the predicted gain code
526 : * calculate optimal gains
527 : *-----------------------------------------------------------------*/
528 :
529 519622 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
530 519622 : *gain_inov = 1.0f / (float) sqrt( Ecode );
531 519622 : Ei = 10 * (float) log10( Ecode );
532 519622 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
533 :
534 519622 : tmp1 = ( g_corr[0] * g_corr[2] ) - ( g_corr[4] * g_corr[4] );
535 519622 : tmp2 = g_corr[1] / tmp1;
536 519622 : tmp1 = g_corr[3] / tmp1;
537 :
538 519622 : *gain_pit = ( g_corr[2] * tmp2 ) - ( g_corr[4] * tmp1 );
539 519622 : *gain_code = ( g_corr[0] * tmp1 ) - ( g_corr[4] * tmp2 );
540 :
541 519622 : *gain_pit = max( G_PITCH_MIN, min( *gain_pit, G_PITCH_MAX ) );
542 :
543 : /*-----------------------------------------------------------------*
544 : * limit the pitch gain searching range (if indicated by clip_gain)
545 : *-----------------------------------------------------------------*/
546 :
547 519622 : if ( clip_gain == 1 && *gain_pit > 0.95f )
548 : {
549 9020 : *gain_pit = 0.95f;
550 : }
551 510602 : else if ( clip_gain == 2 && *gain_pit > 0.65f )
552 : {
553 5199 : *gain_pit = 0.65f;
554 : }
555 :
556 : /*-----------------------------------------------------------------*
557 : * search for the best quantized values
558 : *-----------------------------------------------------------------*/
559 :
560 519622 : nBits_pitch = gains_mode[i_subfr / L_SUBFR];
561 519622 : nBits_code = ( nBits_pitch + 1 ) >> 1;
562 519622 : nBits_pitch = nBits_pitch >> 1;
563 :
564 519622 : tmp16 = div_s( 1, ( ( 1 << ( nBits_pitch ) ) - 1 ) ); /* Q15*/
565 519622 : tmp1 = (float) mult_r( (int16_t) ( G_PITCH_MAX * 8192.0f + 0.5f ), tmp16 ) / 8192.0f;
566 519622 : index = usquant( *gain_pit, gain_pit, G_PITCH_MIN, tmp1, ( 1 << nBits_pitch ) );
567 519622 : push_indice( hBstr, IND_GAIN_PIT, index, nBits_pitch );
568 :
569 519622 : g_code = *gain_code / gcode0;
570 519622 : index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits_code );
571 519622 : *gain_code = g_code * gcode0;
572 519622 : push_indice( hBstr, IND_GAIN_CODE, index, nBits_code );
573 :
574 519622 : *norm_gain_code = *gain_code / *gain_inov;
575 :
576 519622 : return;
577 : }
578 :
579 : /*-------------------------------------------------------------------*
580 : * gain_enc_gaus()
581 : *
582 : * Quantization of gain for Gaussian codebook
583 : *-------------------------------------------------------------------*/
584 :
585 : /*! r: Return index of quantization */
586 880 : int16_t gain_enc_gaus(
587 : float *gain, /* i/o: Code gain to quantize */
588 : const int16_t bits, /* i : number of bits to quantize */
589 : const float lowBound, /* i : lower bound of quantizer (dB) */
590 : const float topBound /* i : upper bound of quantizer (dB) */
591 : )
592 : {
593 : int16_t index;
594 : float enr, stepSize;
595 :
596 880 : enr = (float) ( 20.0 * log10( *gain + 0.001f ) ); /* codebook gain in dB */
597 :
598 : /*-----------------------------------------------------------------*
599 : * quantize linearly the log E
600 : *-----------------------------------------------------------------*/
601 :
602 880 : stepSize = ( topBound - lowBound ) / ( (float) ( 1 << bits ) );
603 880 : index = (int16_t) ( ( ( enr - lowBound ) / stepSize ) + 0.5f );
604 880 : if ( index >= ( 1 << bits ) )
605 : {
606 0 : index = ( 1 << bits ) - 1;
607 : }
608 :
609 880 : if ( index < 0 )
610 : {
611 2 : index = 0;
612 : }
613 :
614 880 : enr = (float) index * stepSize + lowBound; /* quantized codebook gain in dB */
615 880 : *gain = (float) pow( 10.0f, enr / 20.0f ); /* quantized codebook gain */
616 :
617 880 : return index;
618 : }
619 :
620 : /*-----------------------------------------------------------------*
621 : * gain_enc_tc()
622 : *
623 : * Search and quantization of gain_code for subframes (in the
624 : * beginning of frame) without pulses in TC - 3b coding.
625 : * In this case:
626 : * - gain_pit = 0
627 : * - gain_code - scalar quantization (no prediciton history used)
628 : *-----------------------------------------------------------------*/
629 :
630 258113 : void gain_enc_tc(
631 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
632 : const int16_t gains_mode[], /* i : gain bits */
633 : const int16_t i_subfr, /* i : subframe index */
634 : const float xn[], /* i : target vector */
635 : const float y2[], /* i : zero-memory filtered algebraic codebook excitation */
636 : const float code[], /* i : algebraic excitation */
637 : const float Es_pred, /* i : predicted scaled innovation energy */
638 : float *gain_pit, /* o : Pitch gain / Quantized pitch gain */
639 : float *gain_code, /* o : quantized codebook gain */
640 : float *gain_inov, /* o : innovation gain */
641 : float *norm_gain_code /* o : norm. gain of the codebook excitation */
642 : )
643 : {
644 : int16_t i, index, nBits;
645 : float Ei, g_code, gcode0, Ecode;
646 :
647 : /*----------------------------------------------------------------*
648 : * get number of bits for gain quantization
649 : *----------------------------------------------------------------*/
650 :
651 258113 : nBits = gains_mode[i_subfr / L_SUBFR];
652 :
653 : /*----------------------------------------------------------------*
654 : * find the code pitch (for current subframe)
655 : *----------------------------------------------------------------*/
656 :
657 258113 : *gain_code = dotp( xn, y2, L_SUBFR ) / ( dotp( y2, y2, L_SUBFR ) + 0.01f );
658 :
659 : /*----------------------------------------------------------------*
660 : * calculate the predicted gain code
661 : * decode codebook gain
662 : *----------------------------------------------------------------*/
663 :
664 258113 : *gain_pit = 0;
665 :
666 258113 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
667 258113 : *gain_inov = 1.0f / (float) sqrt( Ecode );
668 258113 : Ei = 10 * (float) log10( Ecode );
669 258113 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
670 :
671 258113 : if ( nBits > 3 )
672 : {
673 27338 : g_code = *gain_code / gcode0;
674 27338 : index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits );
675 27338 : *gain_code = g_code * gcode0;
676 27338 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
677 : }
678 : else
679 : {
680 230775 : index = N_GAIN_CODE_TC - 1;
681 787574 : for ( i = 0; i < N_GAIN_CODE_TC - 1; i++ )
682 : {
683 771513 : if ( *gain_code < ( ( tbl_gain_code_tc[i] + ( tbl_gain_code_tc[i + 1] - tbl_gain_code_tc[i] ) / 2 ) * gcode0 ) )
684 : {
685 214714 : index = i;
686 214714 : break;
687 : }
688 : }
689 :
690 : /*----------------------------------------------------------------*
691 : * 3-bit -> 2-bit encoding
692 : *----------------------------------------------------------------*/
693 :
694 230775 : if ( nBits == 2 )
695 : {
696 0 : index /= 2;
697 0 : *gain_code = tbl_gain_code_tc[index * 2] * gcode0;
698 0 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
699 : }
700 : else
701 : {
702 230775 : *gain_code = tbl_gain_code_tc[index] * gcode0;
703 230775 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
704 : }
705 : }
706 :
707 258113 : *norm_gain_code = *gain_code / *gain_inov;
708 :
709 258113 : return;
710 : }
711 :
712 : /*---------------------------------------------------------------------*
713 : * E_corr_xy2()
714 : *
715 : * Find the correlations between the target xn[], the filtered adaptive
716 : * codebook exc. y1[], and the filtered fixed codebook innovation y2[].
717 : * ( <y2,y2>, -2<xn,y2> and 2<y1,y2> ) (stored in g_corr[2..4])
718 : *---------------------------------------------------------------------*/
719 :
720 5846031 : void E_corr_xy2(
721 : const float xn[], /* i : target vector */
722 : const float y1[], /* i : filtered excitation components 1 */
723 : const float y2[], /* i : filtered excitation components 2 */
724 : float g_corr[], /* o : correlations between x, y1, y2, y3, y4 */
725 : const int16_t L_subfr /* i : subframe size */
726 : )
727 : {
728 5846031 : g_corr[2] = dotp( y2, y2, L_subfr );
729 5846031 : g_corr[3] = -2.0f * dotp( xn, y2, L_subfr );
730 5846031 : g_corr[4] = 2.0f * dotp( y1, y2, L_subfr );
731 :
732 5846031 : return;
733 : }
734 :
735 :
736 : /*---------------------------------------------------------------------*
737 : * gain_enc_lbr()
738 : *
739 : * Quantization of pitch and codebook gains without prediction (memory-less)
740 : * in ACELP at 7.2 and 8.0 kbps
741 : * - the gain codebooks and gain estimation constants are different in each subframe
742 : * - the estimated gain, gcode0, is first determined based on
743 : * classification and/or previous quantized gains (from previous subframes in the current frame)
744 : * - a correction factor gamma = g_code / gcode0 is then vector quantized
745 : * along with gain_pit
746 : * - the mean-squared error criterion is used for codebook search
747 : *---------------------------------------------------------------------*/
748 :
749 126480 : void gain_enc_lbr(
750 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
751 : const int16_t gains_mode[], /* i : gain bits */
752 : const int16_t coder_type, /* i : coding type */
753 : const int16_t i_subfr, /* i : subframe index */
754 : const float *xn, /* i : target vector */
755 : const float *y1, /* i : zero-memory filtered adaptive excitation */
756 : const float *y2, /* i : zero-memory filtered algebraic codebook excitation */
757 : const float *code, /* i : algebraic excitation */
758 : float *gain_pit, /* o : quantized pitch gain */
759 : float *gain_code, /* o : quantized codebook gain */
760 : float *gain_inov, /* o : gain of the innovation (used for normalization) */
761 : float *norm_gain_code, /* o : norm. gain of the codebook excitation */
762 : float *g_corr, /* i/o: correlations <y1,y1>, -2<xn,y1>,<y2,y2>, -2<xn,y2> and 2<y1,y2> */
763 : float gains_mem[], /* i/o: pitch gain and code gain from previous subframes */
764 : const int16_t clip_gain, /* i : gain pitch clipping flag (1 = clipping) */
765 : const int16_t L_subfr /* i : subframe length */
766 : )
767 : {
768 126480 : int16_t index = 0, i, size, nBits, n_pred, ctype;
769 : float dist, dist_min, g_pitch, g_code, gcode0, aux[10], Ecode;
770 126480 : int16_t rf_flag = 0;
771 126480 : const float *p, *b, *cdbk = 0;
772 :
773 : /*-----------------------------------------------------------------*
774 : * calculate the rest of the correlation coefficients
775 : * c2 = <y2,y2>, c3 = -2<xn,y2>, c4 = 2<y1,y2>, c5* = <xn,xn>
776 : * c5* - not necessary to calculate
777 : *-----------------------------------------------------------------*/
778 :
779 126480 : E_corr_xy2( xn, y1, y2, g_corr, L_subfr );
780 126480 : g_corr[2] += 0.01F;
781 126480 : g_corr[3] -= 0.02F;
782 126480 : g_corr[4] += 0.02F;
783 :
784 126480 : Ecode = ( dotp( code, code, L_subfr ) + 0.01f ) / L_subfr;
785 126480 : *gain_inov = 1.0f / (float) sqrt( Ecode );
786 :
787 : /*-----------------------------------------------------------------*
788 : * select the codebook, size and number of bits
789 : * set the gains searching range
790 : *-----------------------------------------------------------------*/
791 :
792 126480 : nBits = gains_mode[i_subfr / L_subfr];
793 126480 : size = 1 << nBits;
794 :
795 : /*-----------------------------------------------------------------*
796 : * calculate prediction of gcode
797 : * search for the best codeword
798 : *-----------------------------------------------------------------*/
799 :
800 126480 : ctype = 2 * ( coder_type - 1 );
801 126480 : if ( i_subfr == 0 )
802 : {
803 42352 : b = b_1sfr;
804 42352 : n_pred = 2;
805 :
806 42352 : switch ( nBits )
807 : {
808 12028 : case 8:
809 : {
810 12028 : cdbk = gp_gamma_1sfr_8b;
811 12028 : if ( clip_gain == 1 )
812 : {
813 135 : size -= 60;
814 : }
815 12028 : break;
816 : }
817 3658 : case 7:
818 : {
819 3658 : cdbk = gp_gamma_1sfr_7b;
820 3658 : if ( clip_gain == 1 )
821 : {
822 101 : size -= 27;
823 : }
824 3658 : break;
825 : }
826 26666 : case 6:
827 : {
828 26666 : cdbk = gp_gamma_1sfr_6b;
829 26666 : if ( clip_gain == 1 )
830 : {
831 96 : size -= 10;
832 : }
833 26666 : break;
834 : }
835 : }
836 :
837 : /* calculate predicted gain */
838 42352 : aux[0] = 1.0f;
839 42352 : aux[1] = ctype;
840 42352 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) - 0.5f * (float) log10( Ecode ) );
841 :
842 : /* searching of codebook */
843 42352 : p = cdbk;
844 42352 : dist_min = 3.402823466e+38F;
845 42352 : index = 0;
846 5284581 : for ( i = 0; i < size; i++ )
847 : {
848 5242229 : g_pitch = *p++;
849 5242229 : g_code = gcode0 * *p++;
850 5242229 : dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
851 :
852 5242229 : if ( dist < dist_min )
853 : {
854 482906 : dist_min = dist;
855 482906 : index = i;
856 : }
857 : }
858 :
859 42352 : *gain_pit = cdbk[index * 2];
860 42352 : *gain_code = cdbk[index * 2 + 1] * gcode0;
861 :
862 42352 : gains_mem[0] = *gain_code;
863 42352 : gains_mem[3] = *gain_pit;
864 : }
865 84128 : else if ( i_subfr == L_SUBFR || ( L_subfr == 2 * L_SUBFR ) )
866 : {
867 42352 : b = b_2sfr;
868 42352 : n_pred = 4;
869 :
870 42352 : switch ( nBits )
871 : {
872 12014 : case 7:
873 : {
874 12014 : cdbk = gp_gamma_2sfr_7b;
875 12014 : if ( clip_gain == 1 )
876 : {
877 136 : size -= 30;
878 : }
879 12014 : break;
880 : }
881 30338 : case 6:
882 : {
883 30338 : cdbk = gp_gamma_2sfr_6b;
884 30338 : if ( clip_gain == 1 )
885 : {
886 194 : size -= 12;
887 : }
888 30338 : break;
889 : }
890 : }
891 :
892 : /* calculate predicted gain */
893 42352 : aux[0] = 1.0f;
894 42352 : aux[1] = ctype;
895 42352 : aux[2] = (float) log10( gains_mem[0] );
896 42352 : aux[3] = gains_mem[3];
897 42352 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
898 :
899 : /* searching of codebook */
900 42352 : p = cdbk;
901 42352 : dist_min = 3.402823466e+38F;
902 42352 : index = 0;
903 3515368 : for ( i = 0; i < size; i++ )
904 : {
905 3473016 : g_pitch = *p++;
906 3473016 : g_code = gcode0 * *p++;
907 3473016 : dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
908 :
909 3473016 : if ( dist < dist_min )
910 : {
911 571897 : dist_min = dist;
912 571897 : index = i;
913 : }
914 : }
915 42352 : *gain_pit = cdbk[index * 2];
916 42352 : *gain_code = cdbk[index * 2 + 1] * gcode0;
917 :
918 42352 : gains_mem[1] = *gain_code;
919 42352 : gains_mem[4] = *gain_pit;
920 : }
921 41776 : else if ( i_subfr == 2 * L_SUBFR )
922 : {
923 20888 : if ( rf_flag == 1 )
924 : {
925 0 : gains_mem[1] = gains_mem[0];
926 0 : gains_mem[4] = gains_mem[3];
927 : }
928 :
929 20888 : b = b_3sfr;
930 20888 : n_pred = 6;
931 :
932 20888 : switch ( nBits )
933 : {
934 45 : case 7:
935 : {
936 45 : cdbk = gp_gamma_3sfr_7b;
937 45 : if ( clip_gain == 1 )
938 : {
939 0 : size -= 28;
940 : }
941 45 : break;
942 : }
943 20843 : case 6:
944 : {
945 20843 : cdbk = gp_gamma_3sfr_6b;
946 20843 : if ( clip_gain == 1 )
947 : {
948 333 : size -= 11;
949 : }
950 20843 : break;
951 : }
952 : }
953 :
954 : /* calculate predicted gain */
955 20888 : aux[0] = 1.0f;
956 20888 : aux[1] = ctype;
957 20888 : aux[2] = (float) log10( gains_mem[0] );
958 20888 : aux[3] = (float) log10( gains_mem[1] );
959 20888 : aux[4] = gains_mem[3];
960 20888 : aux[5] = gains_mem[4];
961 20888 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
962 :
963 : /* searching of codebook */
964 20888 : p = cdbk;
965 20888 : dist_min = 3.402823466e+38F;
966 20888 : index = 0;
967 1356937 : for ( i = 0; i < size; i++ )
968 : {
969 1336049 : g_pitch = *p++;
970 1336049 : g_code = gcode0 * *p++;
971 1336049 : dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
972 :
973 1336049 : if ( dist < dist_min )
974 : {
975 308714 : dist_min = dist;
976 308714 : index = i;
977 : }
978 : }
979 20888 : *gain_pit = cdbk[index * 2];
980 20888 : *gain_code = cdbk[index * 2 + 1] * gcode0;
981 :
982 20888 : gains_mem[2] = *gain_code;
983 20888 : gains_mem[5] = *gain_pit;
984 : }
985 20888 : else if ( i_subfr == 3 * L_SUBFR )
986 : {
987 20888 : b = b_4sfr;
988 20888 : n_pred = 8;
989 :
990 20888 : switch ( nBits )
991 : {
992 7 : case 7:
993 : {
994 7 : cdbk = gp_gamma_4sfr_7b;
995 7 : if ( clip_gain == 1 )
996 : {
997 0 : size -= 25;
998 : }
999 7 : break;
1000 : }
1001 20881 : case 6:
1002 : {
1003 20881 : cdbk = gp_gamma_4sfr_6b;
1004 20881 : if ( clip_gain == 1 )
1005 : {
1006 350 : size -= 11;
1007 : }
1008 20881 : break;
1009 : }
1010 : }
1011 :
1012 : /* calculate predicted gain */
1013 20888 : aux[0] = 1.0f;
1014 20888 : aux[1] = ctype;
1015 20888 : aux[2] = (float) log10( gains_mem[0] );
1016 20888 : aux[3] = (float) log10( gains_mem[1] );
1017 20888 : aux[4] = (float) log10( gains_mem[2] );
1018 20888 : aux[5] = gains_mem[3];
1019 20888 : aux[6] = gains_mem[4];
1020 20888 : aux[7] = gains_mem[5];
1021 20888 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
1022 :
1023 : /* searching of codebook */
1024 20888 : p = cdbk;
1025 20888 : dist_min = 3.402823466e+38F;
1026 20888 : index = 0;
1027 1354318 : for ( i = 0; i < size; i++ )
1028 : {
1029 1333430 : g_pitch = *p++;
1030 1333430 : g_code = gcode0 * *p++;
1031 1333430 : dist = g_pitch * g_pitch * g_corr[0] + g_pitch * g_corr[1] + g_code * g_code * g_corr[2] + g_code * g_corr[3] + g_pitch * g_code * g_corr[4];
1032 :
1033 1333430 : if ( dist < dist_min )
1034 : {
1035 330368 : dist_min = dist;
1036 330368 : index = i;
1037 : }
1038 : }
1039 20888 : *gain_pit = cdbk[index * 2];
1040 20888 : *gain_code = cdbk[index * 2 + 1] * gcode0;
1041 : }
1042 :
1043 126480 : *norm_gain_code = *gain_code / *gain_inov;
1044 :
1045 126480 : push_indice( hBstr, IND_GAIN, index, nBits );
1046 :
1047 126480 : return;
1048 : }
|