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 1269839 : 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 1269839 : if ( L_frame == L_FRAME )
80 : {
81 576312 : weight = 0.25f;
82 : }
83 : else /* L_frame == L_FRAME16k */
84 : {
85 693527 : weight = 0.2f;
86 : }
87 :
88 1269839 : mean_ener_code = 0.0f;
89 7042722 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_subfr )
90 : {
91 : /* calculate the energy of residual signal */
92 5772883 : ener = sum2_f( &res[i_subfr], L_subfr ) + 0.01f;
93 5772883 : ener = 10 * (float) log10( ener / ( (float) L_subfr ) );
94 5772883 : if ( ( ener < 0 ) && !( no_ltp ) )
95 : {
96 204881 : ener = 0;
97 : }
98 :
99 : /* update the average energy of residual signal */
100 5772883 : mean_ener_code += weight * ener;
101 : }
102 :
103 1269839 : if ( !no_ltp )
104 : {
105 : /*----------------------------------------------------------*
106 : * subtract an estimate of adaptive codebook contribution
107 : *----------------------------------------------------------*/
108 :
109 1251374 : mean_ener_code -= 10.0f * ( 0.5f * voicing[0] + 0.5f * voicing[1] );
110 :
111 : /*----------------------------------------------------------*
112 : * quantize the average predicted innovation energy
113 : *----------------------------------------------------------*/
114 1251374 : switch ( nb_bits )
115 : {
116 1057809 : case 5:
117 : {
118 1057809 : qua_table = Es_pred_qua_5b;
119 1057809 : break;
120 : }
121 177159 : case 4:
122 : {
123 177159 : qua_table = Es_pred_qua_4b;
124 177159 : break;
125 : }
126 16406 : case 3:
127 : {
128 16406 : qua_table = Es_pred_qua_3b;
129 16406 : break;
130 : }
131 0 : default:
132 : {
133 0 : qua_table = Es_pred_qua_5b;
134 0 : break;
135 : }
136 : }
137 : }
138 : else
139 : {
140 18465 : qua_table = Es_pred_qua_4b_no_ltp;
141 : }
142 :
143 : /* select codebook, size and number of bits */
144 1269839 : size = 1 << nb_bits;
145 :
146 : /* find the nearest neighbour (codevector) */
147 1269839 : tmp = 1e30f;
148 1269839 : *Es_pred_indice = 0;
149 38380959 : for ( i = 0; i < size; i++ )
150 : {
151 37111120 : dist = (float) fabs( mean_ener_code - qua_table[i] );
152 37111120 : if ( dist < tmp )
153 : {
154 18412172 : tmp = dist;
155 18412172 : *Es_pred = qua_table[i];
156 18412172 : *Es_pred_indice = i;
157 : }
158 : }
159 :
160 1269839 : 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 4964122 : 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 4964122 : E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
345 4964122 : g_corr[2] += 0.01F;
346 4964122 : g_corr[3] -= 0.02F;
347 4964122 : g_corr[4] += 0.02F;
348 :
349 : /*-----------------------------------------------------------------*
350 : * calculate the unscaled innovation energy
351 : * calculate the predicted gain code
352 : *-----------------------------------------------------------------*/
353 :
354 4964122 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
355 4964122 : *gain_inov = 1.0f / (float) sqrt( Ecode );
356 4964122 : Ei = 10 * (float) log10( Ecode );
357 4964122 : 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 4964122 : nBits = gains_mode[i_subfr / L_SUBFR];
365 :
366 4964122 : if ( ( tc_subfr == 3 * L_SUBFR && i_subfr == 3 * L_SUBFR && L_frame == L_FRAME ) ||
367 18465 : ( 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 40038 : tmp1 = ( g_corr[0] * g_corr[2] ) - ( 0.25f * g_corr[4] * g_corr[4] );
371 40038 : tmp2 = -0.5f * g_corr[1] / tmp1;
372 40038 : tmp1 = -0.5f * g_corr[3] / tmp1;
373 :
374 40038 : *gain_pit = ( g_corr[2] * tmp2 ) - ( 0.5f * g_corr[4] * tmp1 );
375 40038 : *gain_code = ( g_corr[0] * tmp1 ) - ( 0.5f * g_corr[4] * tmp2 );
376 :
377 40038 : *gain_pit = max( G_PITCH_MIN_TC192, min( *gain_pit, G_PITCH_MAX_TC192 ) );
378 :
379 : /* set number of bits for two SQs */
380 40038 : nBits2 = ( nBits + 1 ) >> 1;
381 40038 : nBits = nBits >> 1;
382 :
383 : /* gain_pit Q */
384 40038 : tmp1 = ( G_PITCH_MAX_TC192 - G_PITCH_MIN_TC192 ) / ( ( 1 << nBits ) - 1 ); /* set quantization step */
385 40038 : index = usquant( *gain_pit, gain_pit, G_PITCH_MIN_TC192, tmp1, ( 1 << nBits ) );
386 40038 : push_indice( hBstr, IND_GAIN_PIT, index, nBits );
387 :
388 : /* gain_code Q */
389 40038 : *gain_code /= gcode0;
390 40038 : index = gain_quant( gain_code, G_CODE_MIN_TC192, G_CODE_MAX_TC192, nBits2 );
391 40038 : push_indice( hBstr, IND_GAIN_CODE, index, nBits2 );
392 40038 : *gain_code *= gcode0;
393 : }
394 : else
395 : {
396 4924084 : size = 1 << nBits;
397 :
398 4924084 : switch ( nBits )
399 : {
400 3513 : case 7:
401 : {
402 3513 : qua_table = gain_qua_mless_7b;
403 3513 : if ( clip_gain == 1 )
404 : {
405 0 : size -= 30;
406 : }
407 3513 : break;
408 : }
409 4916287 : case 6:
410 : {
411 4916287 : qua_table = gain_qua_mless_6b;
412 :
413 4916287 : if ( element_mode > EVS_MONO )
414 : {
415 4842768 : qua_table = gain_qua_mless_6b_stereo;
416 : }
417 :
418 4916287 : if ( clip_gain == 1 )
419 : {
420 147432 : size -= 14;
421 : }
422 4916287 : break;
423 : }
424 4284 : case 5:
425 : {
426 4284 : qua_table = gain_qua_mless_5b;
427 4284 : if ( clip_gain == 1 )
428 : {
429 1 : size -= 6;
430 : }
431 4284 : 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 4924084 : if ( clip_gain == 2 && nBits == 6 )
447 : {
448 31305 : size -= 36;
449 31305 : nBits--;
450 : }
451 :
452 : /*-----------------------------------------------------------------*
453 : * search for the best quantizer
454 : *-----------------------------------------------------------------*/
455 :
456 4924084 : p = qua_table;
457 4924084 : dist_min = 3.402823466e+38F;
458 4924084 : index = 0;
459 316962170 : for ( i = 0; i < size; i++ )
460 : {
461 312038086 : g_pitch = *p++; /* pitch gain */
462 312038086 : g_code = gcode0 * *p++; /* code gain */
463 312038086 : 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 312038086 : if ( dist < dist_min )
465 : {
466 65662046 : dist_min = dist;
467 65662046 : index = i;
468 : }
469 : }
470 4924084 : *gain_pit = qua_table[index * 2];
471 4924084 : *gain_code = qua_table[index * 2 + 1] * gcode0;
472 :
473 4924084 : push_indice( hBstr, IND_GAIN, index, nBits );
474 : }
475 :
476 4964122 : *norm_gain_code = *gain_code / *gain_inov;
477 :
478 4964122 : 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 431227 : 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 431227 : g_corr[1] *= -0.5;
519 431227 : g_corr[2] = dotp( y2, y2, L_SUBFR ) + 0.01f;
520 431227 : g_corr[3] = dotp( xn, y2, L_SUBFR ) - 0.02f;
521 431227 : 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 431227 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
530 431227 : *gain_inov = 1.0f / (float) sqrt( Ecode );
531 431227 : Ei = 10 * (float) log10( Ecode );
532 431227 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
533 :
534 431227 : tmp1 = ( g_corr[0] * g_corr[2] ) - ( g_corr[4] * g_corr[4] );
535 431227 : tmp2 = g_corr[1] / tmp1;
536 431227 : tmp1 = g_corr[3] / tmp1;
537 :
538 431227 : *gain_pit = ( g_corr[2] * tmp2 ) - ( g_corr[4] * tmp1 );
539 431227 : *gain_code = ( g_corr[0] * tmp1 ) - ( g_corr[4] * tmp2 );
540 :
541 431227 : *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 431227 : if ( clip_gain == 1 && *gain_pit > 0.95f )
548 : {
549 9001 : *gain_pit = 0.95f;
550 : }
551 422226 : else if ( clip_gain == 2 && *gain_pit > 0.65f )
552 : {
553 3583 : *gain_pit = 0.65f;
554 : }
555 :
556 : /*-----------------------------------------------------------------*
557 : * search for the best quantized values
558 : *-----------------------------------------------------------------*/
559 :
560 431227 : nBits_pitch = gains_mode[i_subfr / L_SUBFR];
561 431227 : nBits_code = ( nBits_pitch + 1 ) >> 1;
562 431227 : nBits_pitch = nBits_pitch >> 1;
563 :
564 431227 : tmp16 = div_s( 1, ( ( 1 << ( nBits_pitch ) ) - 1 ) ); /* Q15*/
565 431227 : tmp1 = (float) mult_r( (int16_t) ( G_PITCH_MAX * 8192.0f + 0.5f ), tmp16 ) / 8192.0f;
566 431227 : index = usquant( *gain_pit, gain_pit, G_PITCH_MIN, tmp1, ( 1 << nBits_pitch ) );
567 431227 : push_indice( hBstr, IND_GAIN_PIT, index, nBits_pitch );
568 :
569 431227 : g_code = *gain_code / gcode0;
570 431227 : index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits_code );
571 431227 : *gain_code = g_code * gcode0;
572 431227 : push_indice( hBstr, IND_GAIN_CODE, index, nBits_code );
573 :
574 431227 : *norm_gain_code = *gain_code / *gain_inov;
575 :
576 431227 : 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 227646 : 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 227646 : nBits = gains_mode[i_subfr / L_SUBFR];
652 :
653 : /*----------------------------------------------------------------*
654 : * find the code pitch (for current subframe)
655 : *----------------------------------------------------------------*/
656 :
657 227646 : *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 227646 : *gain_pit = 0;
665 :
666 227646 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
667 227646 : *gain_inov = 1.0f / (float) sqrt( Ecode );
668 227646 : Ei = 10 * (float) log10( Ecode );
669 227646 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
670 :
671 227646 : if ( nBits > 3 )
672 : {
673 21078 : g_code = *gain_code / gcode0;
674 21078 : index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits );
675 21078 : *gain_code = g_code * gcode0;
676 21078 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
677 : }
678 : else
679 : {
680 206568 : index = N_GAIN_CODE_TC - 1;
681 716083 : for ( i = 0; i < N_GAIN_CODE_TC - 1; i++ )
682 : {
683 701454 : if ( *gain_code < ( ( tbl_gain_code_tc[i] + ( tbl_gain_code_tc[i + 1] - tbl_gain_code_tc[i] ) / 2 ) * gcode0 ) )
684 : {
685 191939 : index = i;
686 191939 : break;
687 : }
688 : }
689 :
690 : /*----------------------------------------------------------------*
691 : * 3-bit -> 2-bit encoding
692 : *----------------------------------------------------------------*/
693 :
694 206568 : 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 206568 : *gain_code = tbl_gain_code_tc[index] * gcode0;
703 206568 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
704 : }
705 : }
706 :
707 227646 : *norm_gain_code = *gain_code / *gain_inov;
708 :
709 227646 : 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 5240626 : 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 5240626 : g_corr[2] = dotp( y2, y2, L_subfr );
729 5240626 : g_corr[3] = -2.0f * dotp( xn, y2, L_subfr );
730 5240626 : g_corr[4] = 2.0f * dotp( y1, y2, L_subfr );
731 :
732 5240626 : 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 109820 : 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 109820 : int16_t index = 0, i, size, nBits, n_pred, ctype;
769 : float dist, dist_min, g_pitch, g_code, gcode0, aux[10], Ecode;
770 109820 : int16_t rf_flag = 0;
771 109820 : 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 109820 : E_corr_xy2( xn, y1, y2, g_corr, L_subfr );
780 109820 : g_corr[2] += 0.01F;
781 109820 : g_corr[3] -= 0.02F;
782 109820 : g_corr[4] += 0.02F;
783 :
784 109820 : Ecode = ( dotp( code, code, L_subfr ) + 0.01f ) / L_subfr;
785 109820 : *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 109820 : nBits = gains_mode[i_subfr / L_subfr];
793 109820 : size = 1 << nBits;
794 :
795 : /*-----------------------------------------------------------------*
796 : * calculate prediction of gcode
797 : * search for the best codeword
798 : *-----------------------------------------------------------------*/
799 :
800 109820 : ctype = 2 * ( coder_type - 1 );
801 109820 : if ( i_subfr == 0 )
802 : {
803 35530 : b = b_1sfr;
804 35530 : n_pred = 2;
805 :
806 35530 : switch ( nBits )
807 : {
808 11044 : case 8:
809 : {
810 11044 : cdbk = gp_gamma_1sfr_8b;
811 11044 : if ( clip_gain == 1 )
812 : {
813 135 : size -= 60;
814 : }
815 11044 : break;
816 : }
817 3443 : case 7:
818 : {
819 3443 : cdbk = gp_gamma_1sfr_7b;
820 3443 : if ( clip_gain == 1 )
821 : {
822 101 : size -= 27;
823 : }
824 3443 : break;
825 : }
826 21043 : case 6:
827 : {
828 21043 : cdbk = gp_gamma_1sfr_6b;
829 21043 : if ( clip_gain == 1 )
830 : {
831 96 : size -= 10;
832 : }
833 21043 : break;
834 : }
835 : }
836 :
837 : /* calculate predicted gain */
838 35530 : aux[0] = 1.0f;
839 35530 : aux[1] = ctype;
840 35530 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) - 0.5f * (float) log10( Ecode ) );
841 :
842 : /* searching of codebook */
843 35530 : p = cdbk;
844 35530 : dist_min = 3.402823466e+38F;
845 35530 : index = 0;
846 4638463 : for ( i = 0; i < size; i++ )
847 : {
848 4602933 : g_pitch = *p++;
849 4602933 : g_code = gcode0 * *p++;
850 4602933 : 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 4602933 : if ( dist < dist_min )
853 : {
854 419938 : dist_min = dist;
855 419938 : index = i;
856 : }
857 : }
858 :
859 35530 : *gain_pit = cdbk[index * 2];
860 35530 : *gain_code = cdbk[index * 2 + 1] * gcode0;
861 :
862 35530 : gains_mem[0] = *gain_code;
863 35530 : gains_mem[3] = *gain_pit;
864 : }
865 74290 : else if ( i_subfr == L_SUBFR || ( L_subfr == 2 * L_SUBFR ) )
866 : {
867 35530 : b = b_2sfr;
868 35530 : n_pred = 4;
869 :
870 35530 : switch ( nBits )
871 : {
872 11033 : case 7:
873 : {
874 11033 : cdbk = gp_gamma_2sfr_7b;
875 11033 : if ( clip_gain == 1 )
876 : {
877 136 : size -= 30;
878 : }
879 11033 : break;
880 : }
881 24497 : case 6:
882 : {
883 24497 : cdbk = gp_gamma_2sfr_6b;
884 24497 : if ( clip_gain == 1 )
885 : {
886 194 : size -= 12;
887 : }
888 24497 : break;
889 : }
890 : }
891 :
892 : /* calculate predicted gain */
893 35530 : aux[0] = 1.0f;
894 35530 : aux[1] = ctype;
895 35530 : aux[2] = (float) log10( gains_mem[0] );
896 35530 : aux[3] = gains_mem[3];
897 35530 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
898 :
899 : /* searching of codebook */
900 35530 : p = cdbk;
901 35530 : dist_min = 3.402823466e+38F;
902 35530 : index = 0;
903 3009154 : for ( i = 0; i < size; i++ )
904 : {
905 2973624 : g_pitch = *p++;
906 2973624 : g_code = gcode0 * *p++;
907 2973624 : 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 2973624 : if ( dist < dist_min )
910 : {
911 496216 : dist_min = dist;
912 496216 : index = i;
913 : }
914 : }
915 35530 : *gain_pit = cdbk[index * 2];
916 35530 : *gain_code = cdbk[index * 2 + 1] * gcode0;
917 :
918 35530 : gains_mem[1] = *gain_code;
919 35530 : gains_mem[4] = *gain_pit;
920 : }
921 38760 : else if ( i_subfr == 2 * L_SUBFR )
922 : {
923 19380 : if ( rf_flag == 1 )
924 : {
925 0 : gains_mem[1] = gains_mem[0];
926 0 : gains_mem[4] = gains_mem[3];
927 : }
928 :
929 19380 : b = b_3sfr;
930 19380 : n_pred = 6;
931 :
932 19380 : switch ( nBits )
933 : {
934 43 : case 7:
935 : {
936 43 : cdbk = gp_gamma_3sfr_7b;
937 43 : if ( clip_gain == 1 )
938 : {
939 0 : size -= 28;
940 : }
941 43 : break;
942 : }
943 19337 : case 6:
944 : {
945 19337 : cdbk = gp_gamma_3sfr_6b;
946 19337 : if ( clip_gain == 1 )
947 : {
948 333 : size -= 11;
949 : }
950 19337 : break;
951 : }
952 : }
953 :
954 : /* calculate predicted gain */
955 19380 : aux[0] = 1.0f;
956 19380 : aux[1] = ctype;
957 19380 : aux[2] = (float) log10( gains_mem[0] );
958 19380 : aux[3] = (float) log10( gains_mem[1] );
959 19380 : aux[4] = gains_mem[3];
960 19380 : aux[5] = gains_mem[4];
961 19380 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
962 :
963 : /* searching of codebook */
964 19380 : p = cdbk;
965 19380 : dist_min = 3.402823466e+38F;
966 19380 : index = 0;
967 1258789 : for ( i = 0; i < size; i++ )
968 : {
969 1239409 : g_pitch = *p++;
970 1239409 : g_code = gcode0 * *p++;
971 1239409 : 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 1239409 : if ( dist < dist_min )
974 : {
975 286903 : dist_min = dist;
976 286903 : index = i;
977 : }
978 : }
979 19380 : *gain_pit = cdbk[index * 2];
980 19380 : *gain_code = cdbk[index * 2 + 1] * gcode0;
981 :
982 19380 : gains_mem[2] = *gain_code;
983 19380 : gains_mem[5] = *gain_pit;
984 : }
985 19380 : else if ( i_subfr == 3 * L_SUBFR )
986 : {
987 19380 : b = b_4sfr;
988 19380 : n_pred = 8;
989 :
990 19380 : 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 19373 : case 6:
1002 : {
1003 19373 : cdbk = gp_gamma_4sfr_6b;
1004 19373 : if ( clip_gain == 1 )
1005 : {
1006 350 : size -= 11;
1007 : }
1008 19373 : break;
1009 : }
1010 : }
1011 :
1012 : /* calculate predicted gain */
1013 19380 : aux[0] = 1.0f;
1014 19380 : aux[1] = ctype;
1015 19380 : aux[2] = (float) log10( gains_mem[0] );
1016 19380 : aux[3] = (float) log10( gains_mem[1] );
1017 19380 : aux[4] = (float) log10( gains_mem[2] );
1018 19380 : aux[5] = gains_mem[3];
1019 19380 : aux[6] = gains_mem[4];
1020 19380 : aux[7] = gains_mem[5];
1021 19380 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
1022 :
1023 : /* searching of codebook */
1024 19380 : p = cdbk;
1025 19380 : dist_min = 3.402823466e+38F;
1026 19380 : index = 0;
1027 1256298 : for ( i = 0; i < size; i++ )
1028 : {
1029 1236918 : g_pitch = *p++;
1030 1236918 : g_code = gcode0 * *p++;
1031 1236918 : 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 1236918 : if ( dist < dist_min )
1034 : {
1035 306710 : dist_min = dist;
1036 306710 : index = i;
1037 : }
1038 : }
1039 19380 : *gain_pit = cdbk[index * 2];
1040 19380 : *gain_code = cdbk[index * 2 + 1] * gcode0;
1041 : }
1042 :
1043 109820 : *norm_gain_code = *gain_code / *gain_inov;
1044 :
1045 109820 : push_indice( hBstr, IND_GAIN, index, nBits );
1046 :
1047 109820 : return;
1048 : }
|