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 132941 : 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 132941 : if ( L_frame == L_FRAME )
80 : {
81 60780 : weight = 0.25f;
82 : }
83 : else /* L_frame == L_FRAME16k */
84 : {
85 72161 : weight = 0.2f;
86 : }
87 :
88 132941 : mean_ener_code = 0.0f;
89 736866 : for ( i_subfr = 0; i_subfr < L_frame; i_subfr += L_subfr )
90 : {
91 : /* calculate the energy of residual signal */
92 603925 : ener = sum2_f( &res[i_subfr], L_subfr ) + 0.01f;
93 603925 : ener = 10 * (float) log10( ener / ( (float) L_subfr ) );
94 603925 : if ( ( ener < 0 ) && !( no_ltp ) )
95 : {
96 12818 : ener = 0;
97 : }
98 :
99 : /* update the average energy of residual signal */
100 603925 : mean_ener_code += weight * ener;
101 : }
102 :
103 132941 : if ( !no_ltp )
104 : {
105 : /*----------------------------------------------------------*
106 : * subtract an estimate of adaptive codebook contribution
107 : *----------------------------------------------------------*/
108 :
109 130903 : mean_ener_code -= 10.0f * ( 0.5f * voicing[0] + 0.5f * voicing[1] );
110 :
111 : /*----------------------------------------------------------*
112 : * quantize the average predicted innovation energy
113 : *----------------------------------------------------------*/
114 130903 : switch ( nb_bits )
115 : {
116 112929 : case 5:
117 : {
118 112929 : qua_table = Es_pred_qua_5b;
119 112929 : break;
120 : }
121 17370 : case 4:
122 : {
123 17370 : qua_table = Es_pred_qua_4b;
124 17370 : break;
125 : }
126 604 : case 3:
127 : {
128 604 : qua_table = Es_pred_qua_3b;
129 604 : break;
130 : }
131 0 : default:
132 : {
133 0 : qua_table = Es_pred_qua_5b;
134 0 : break;
135 : }
136 : }
137 : }
138 : else
139 : {
140 2038 : qua_table = Es_pred_qua_4b_no_ltp;
141 : }
142 :
143 : /* select codebook, size and number of bits */
144 132941 : size = 1 << nb_bits;
145 :
146 : /* find the nearest neighbour (codevector) */
147 132941 : tmp = 1e30f;
148 132941 : *Es_pred_indice = 0;
149 4062029 : for ( i = 0; i < size; i++ )
150 : {
151 3929088 : dist = (float) fabs( mean_ener_code - qua_table[i] );
152 3929088 : if ( dist < tmp )
153 : {
154 2086158 : tmp = dist;
155 2086158 : *Es_pred = qua_table[i];
156 2086158 : *Es_pred_indice = i;
157 : }
158 : }
159 :
160 132941 : 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 0 : 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 0 : E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
201 0 : g_corr[2] += 0.01F;
202 0 : g_corr[3] -= 0.02F;
203 0 : g_corr[4] += 0.02F;
204 0 : *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 0 : if ( core_brate < ACELP_12k65 )
212 : {
213 0 : t_qua_gain = t_qua_gain6b;
214 0 : nBits = 6;
215 0 : min_ind = 0;
216 0 : size = RANGE;
217 0 : if ( clip_gain == 1 )
218 : {
219 0 : size -= 16; /* limit pitch gain to 1.0 */
220 : }
221 : }
222 : else
223 : {
224 0 : t_qua_gain = t_qua_gain7b;
225 0 : nBits = 7;
226 0 : p = t_qua_gain7b + RANGE; /* pt at 1/4th of table */
227 0 : j = NB_QUA_GAIN7B - RANGE;
228 0 : if ( clip_gain == 1 )
229 : {
230 0 : j -= 27; /* limit pitch gain to 1.0 */
231 : }
232 :
233 0 : min_ind = 0;
234 0 : g_pitch = *gain_pit;
235 :
236 :
237 0 : for ( i = 0; i < j; i++, p += 2 )
238 : {
239 0 : if ( g_pitch > *p )
240 : {
241 0 : min_ind++;
242 : }
243 : }
244 0 : size = RANGE;
245 : }
246 :
247 : /*-----------------------------------------------------------------*
248 : * predicted code gain
249 : *-----------------------------------------------------------------*/
250 :
251 : /* start with predicting code energy in dB */
252 0 : gcode0 = MEAN_ENER;
253 0 : for ( i = 0; i < GAIN_PRED_ORDER; i++ )
254 : {
255 0 : gcode0 += pred_gain[i] * past_qua_en[i];
256 : }
257 0 : gcode0 += (float) ( 20.0 * log10( *gain_inov ) );
258 :
259 : /* convert from energy in dB to gain */
260 0 : gcode0 = (float) pow( 10.0, gcode0 / 20.0 );
261 :
262 : /*-----------------------------------------------------------------*
263 : * search the codebook
264 : *-----------------------------------------------------------------*/
265 :
266 0 : dist_min = 3.402823466e+38F;
267 0 : p = t_qua_gain + min_ind * 2;
268 :
269 0 : index = 0;
270 0 : for ( i = 0; i < size; i++ )
271 : {
272 0 : g_pitch = *p++; /* pitch gain */
273 0 : g_code = gcode0 * *p++; /* code gain */
274 0 : 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 0 : if ( dist < dist_min )
276 : {
277 0 : dist_min = dist;
278 0 : index = i;
279 : }
280 : }
281 0 : index = index + min_ind;
282 0 : *gain_pit = t_qua_gain[index * 2];
283 0 : qua_en = t_qua_gain[index * 2 + 1];
284 0 : *gain_code = qua_en * gcode0;
285 :
286 : /*-----------------------------------------------------------------*
287 : * update table of past quantized energies
288 : *-----------------------------------------------------------------*/
289 :
290 0 : for ( i = GAIN_PRED_ORDER - 1; i > 0; i-- )
291 : {
292 0 : past_qua_en[i] = past_qua_en[i - 1];
293 : }
294 0 : past_qua_en[0] = (float) ( 20.0 * log10( qua_en ) );
295 :
296 0 : push_indice( hBstr, IND_GAIN, index, nBits );
297 :
298 0 : *norm_gain_code = *gain_code / *gain_inov;
299 :
300 0 : 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 525932 : 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 525932 : E_corr_xy2( xn, y1, y2, g_corr, L_SUBFR );
345 525932 : g_corr[2] += 0.01F;
346 525932 : g_corr[3] -= 0.02F;
347 525932 : g_corr[4] += 0.02F;
348 :
349 : /*-----------------------------------------------------------------*
350 : * calculate the unscaled innovation energy
351 : * calculate the predicted gain code
352 : *-----------------------------------------------------------------*/
353 :
354 525932 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
355 525932 : *gain_inov = 1.0f / (float) sqrt( Ecode );
356 525932 : Ei = 10 * (float) log10( Ecode );
357 525932 : 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 525932 : nBits = gains_mode[i_subfr / L_SUBFR];
365 :
366 525932 : if ( ( tc_subfr == 3 * L_SUBFR && i_subfr == 3 * L_SUBFR && L_frame == L_FRAME ) ||
367 1816 : ( 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 3637 : tmp1 = ( g_corr[0] * g_corr[2] ) - ( 0.25f * g_corr[4] * g_corr[4] );
371 3637 : tmp2 = -0.5f * g_corr[1] / tmp1;
372 3637 : tmp1 = -0.5f * g_corr[3] / tmp1;
373 :
374 3637 : *gain_pit = ( g_corr[2] * tmp2 ) - ( 0.5f * g_corr[4] * tmp1 );
375 3637 : *gain_code = ( g_corr[0] * tmp1 ) - ( 0.5f * g_corr[4] * tmp2 );
376 :
377 3637 : *gain_pit = max( G_PITCH_MIN_TC192, min( *gain_pit, G_PITCH_MAX_TC192 ) );
378 :
379 : /* set number of bits for two SQs */
380 3637 : nBits2 = ( nBits + 1 ) >> 1;
381 3637 : nBits = nBits >> 1;
382 :
383 : /* gain_pit Q */
384 3637 : tmp1 = ( G_PITCH_MAX_TC192 - G_PITCH_MIN_TC192 ) / ( ( 1 << nBits ) - 1 ); /* set quantization step */
385 3637 : index = usquant( *gain_pit, gain_pit, G_PITCH_MIN_TC192, tmp1, ( 1 << nBits ) );
386 3637 : push_indice( hBstr, IND_GAIN_PIT, index, nBits );
387 :
388 : /* gain_code Q */
389 3637 : *gain_code /= gcode0;
390 3637 : index = gain_quant( gain_code, G_CODE_MIN_TC192, G_CODE_MAX_TC192, nBits2 );
391 3637 : push_indice( hBstr, IND_GAIN_CODE, index, nBits2 );
392 3637 : *gain_code *= gcode0;
393 : }
394 : else
395 : {
396 522295 : size = 1 << nBits;
397 :
398 522295 : switch ( nBits )
399 : {
400 742 : case 7:
401 : {
402 742 : qua_table = gain_qua_mless_7b;
403 742 : if ( clip_gain == 1 )
404 : {
405 0 : size -= 30;
406 : }
407 742 : break;
408 : }
409 521013 : case 6:
410 : {
411 521013 : qua_table = gain_qua_mless_6b;
412 :
413 521013 : if ( element_mode > EVS_MONO )
414 : {
415 518146 : qua_table = gain_qua_mless_6b_stereo;
416 : }
417 :
418 521013 : if ( clip_gain == 1 )
419 : {
420 6218 : size -= 14;
421 : }
422 521013 : break;
423 : }
424 540 : case 5:
425 : {
426 540 : qua_table = gain_qua_mless_5b;
427 540 : if ( clip_gain == 1 )
428 : {
429 0 : size -= 6;
430 : }
431 540 : 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 522295 : if ( clip_gain == 2 && nBits == 6 )
447 : {
448 5315 : size -= 36;
449 5315 : nBits--;
450 : }
451 :
452 : /*-----------------------------------------------------------------*
453 : * search for the best quantizer
454 : *-----------------------------------------------------------------*/
455 :
456 522295 : p = qua_table;
457 522295 : dist_min = 3.402823466e+38F;
458 522295 : index = 0;
459 33700991 : for ( i = 0; i < size; i++ )
460 : {
461 33178696 : g_pitch = *p++; /* pitch gain */
462 33178696 : g_code = gcode0 * *p++; /* code gain */
463 33178696 : 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 33178696 : if ( dist < dist_min )
465 : {
466 6618507 : dist_min = dist;
467 6618507 : index = i;
468 : }
469 : }
470 522295 : *gain_pit = qua_table[index * 2];
471 522295 : *gain_code = qua_table[index * 2 + 1] * gcode0;
472 :
473 522295 : push_indice( hBstr, IND_GAIN, index, nBits );
474 : }
475 :
476 525932 : *norm_gain_code = *gain_code / *gain_inov;
477 :
478 525932 : 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 46555 : 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 46555 : g_corr[1] *= -0.5;
519 46555 : g_corr[2] = dotp( y2, y2, L_SUBFR ) + 0.01f;
520 46555 : g_corr[3] = dotp( xn, y2, L_SUBFR ) - 0.02f;
521 46555 : 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 46555 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
530 46555 : *gain_inov = 1.0f / (float) sqrt( Ecode );
531 46555 : Ei = 10 * (float) log10( Ecode );
532 46555 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
533 :
534 46555 : tmp1 = ( g_corr[0] * g_corr[2] ) - ( g_corr[4] * g_corr[4] );
535 46555 : tmp2 = g_corr[1] / tmp1;
536 46555 : tmp1 = g_corr[3] / tmp1;
537 :
538 46555 : *gain_pit = ( g_corr[2] * tmp2 ) - ( g_corr[4] * tmp1 );
539 46555 : *gain_code = ( g_corr[0] * tmp1 ) - ( g_corr[4] * tmp2 );
540 :
541 46555 : *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 46555 : if ( clip_gain == 1 && *gain_pit > 0.95f )
548 : {
549 19 : *gain_pit = 0.95f;
550 : }
551 46536 : else if ( clip_gain == 2 && *gain_pit > 0.65f )
552 : {
553 1046 : *gain_pit = 0.65f;
554 : }
555 :
556 : /*-----------------------------------------------------------------*
557 : * search for the best quantized values
558 : *-----------------------------------------------------------------*/
559 :
560 46555 : nBits_pitch = gains_mode[i_subfr / L_SUBFR];
561 46555 : nBits_code = ( nBits_pitch + 1 ) >> 1;
562 46555 : nBits_pitch = nBits_pitch >> 1;
563 :
564 46555 : tmp16 = div_s( 1, ( ( 1 << ( nBits_pitch ) ) - 1 ) ); /* Q15*/
565 46555 : tmp1 = (float) mult_r( (int16_t) ( G_PITCH_MAX * 8192.0f + 0.5f ), tmp16 ) / 8192.0f;
566 46555 : index = usquant( *gain_pit, gain_pit, G_PITCH_MIN, tmp1, ( 1 << nBits_pitch ) );
567 46555 : push_indice( hBstr, IND_GAIN_PIT, index, nBits_pitch );
568 :
569 46555 : g_code = *gain_code / gcode0;
570 46555 : index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits_code );
571 46555 : *gain_code = g_code * gcode0;
572 46555 : push_indice( hBstr, IND_GAIN_CODE, index, nBits_code );
573 :
574 46555 : *norm_gain_code = *gain_code / *gain_inov;
575 :
576 46555 : 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 0 : 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 0 : enr = (float) ( 20.0 * log10( *gain + 0.001f ) ); /* codebook gain in dB */
597 :
598 : /*-----------------------------------------------------------------*
599 : * quantize linearly the log E
600 : *-----------------------------------------------------------------*/
601 :
602 0 : stepSize = ( topBound - lowBound ) / ( (float) ( 1 << bits ) );
603 0 : index = (int16_t) ( ( ( enr - lowBound ) / stepSize ) + 0.5f );
604 0 : if ( index >= ( 1 << bits ) )
605 : {
606 0 : index = ( 1 << bits ) - 1;
607 : }
608 :
609 0 : if ( index < 0 )
610 : {
611 0 : index = 0;
612 : }
613 :
614 0 : enr = (float) index * stepSize + lowBound; /* quantized codebook gain in dB */
615 0 : *gain = (float) pow( 10.0f, enr / 20.0f ); /* quantized codebook gain */
616 :
617 0 : 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 20291 : 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 20291 : nBits = gains_mode[i_subfr / L_SUBFR];
652 :
653 : /*----------------------------------------------------------------*
654 : * find the code pitch (for current subframe)
655 : *----------------------------------------------------------------*/
656 :
657 20291 : *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 20291 : *gain_pit = 0;
665 :
666 20291 : Ecode = ( dotp( code, code, L_SUBFR ) + 0.01f ) / L_SUBFR;
667 20291 : *gain_inov = 1.0f / (float) sqrt( Ecode );
668 20291 : Ei = 10 * (float) log10( Ecode );
669 20291 : gcode0 = (float) pow( 10, 0.05 * ( Es_pred - Ei ) );
670 :
671 20291 : if ( nBits > 3 )
672 : {
673 1430 : g_code = *gain_code / gcode0;
674 1430 : index = gain_quant( &g_code, G_CODE_MIN, G_CODE_MAX, nBits );
675 1430 : *gain_code = g_code * gcode0;
676 1430 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
677 : }
678 : else
679 : {
680 18861 : index = N_GAIN_CODE_TC - 1;
681 64007 : for ( i = 0; i < N_GAIN_CODE_TC - 1; i++ )
682 : {
683 63121 : if ( *gain_code < ( ( tbl_gain_code_tc[i] + ( tbl_gain_code_tc[i + 1] - tbl_gain_code_tc[i] ) / 2 ) * gcode0 ) )
684 : {
685 17975 : index = i;
686 17975 : break;
687 : }
688 : }
689 :
690 : /*----------------------------------------------------------------*
691 : * 3-bit -> 2-bit encoding
692 : *----------------------------------------------------------------*/
693 :
694 18861 : 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 18861 : *gain_code = tbl_gain_code_tc[index] * gcode0;
703 18861 : push_indice( hBstr, IND_GAIN_CODE, index, nBits );
704 : }
705 : }
706 :
707 20291 : *norm_gain_code = *gain_code / *gain_inov;
708 :
709 20291 : 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 554566 : 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 554566 : g_corr[2] = dotp( y2, y2, L_subfr );
729 554566 : g_corr[3] = -2.0f * dotp( xn, y2, L_subfr );
730 554566 : g_corr[4] = 2.0f * dotp( y1, y2, L_subfr );
731 :
732 554566 : 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 17466 : 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 17466 : int16_t index = 0, i, size, nBits, n_pred, ctype;
769 : float dist, dist_min, g_pitch, g_code, gcode0, aux[10], Ecode;
770 17466 : int16_t rf_flag = 0;
771 17466 : 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 17466 : E_corr_xy2( xn, y1, y2, g_corr, L_subfr );
780 17466 : g_corr[2] += 0.01F;
781 17466 : g_corr[3] -= 0.02F;
782 17466 : g_corr[4] += 0.02F;
783 :
784 17466 : Ecode = ( dotp( code, code, L_subfr ) + 0.01f ) / L_subfr;
785 17466 : *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 17466 : nBits = gains_mode[i_subfr / L_subfr];
793 17466 : size = 1 << nBits;
794 :
795 : /*-----------------------------------------------------------------*
796 : * calculate prediction of gcode
797 : * search for the best codeword
798 : *-----------------------------------------------------------------*/
799 :
800 17466 : ctype = 2 * ( coder_type - 1 );
801 17466 : if ( i_subfr == 0 )
802 : {
803 5388 : b = b_1sfr;
804 5388 : n_pred = 2;
805 :
806 5388 : switch ( nBits )
807 : {
808 2151 : case 8:
809 : {
810 2151 : cdbk = gp_gamma_1sfr_8b;
811 2151 : if ( clip_gain == 1 )
812 : {
813 0 : size -= 60;
814 : }
815 2151 : break;
816 : }
817 436 : case 7:
818 : {
819 436 : cdbk = gp_gamma_1sfr_7b;
820 436 : if ( clip_gain == 1 )
821 : {
822 0 : size -= 27;
823 : }
824 436 : break;
825 : }
826 2801 : case 6:
827 : {
828 2801 : cdbk = gp_gamma_1sfr_6b;
829 2801 : if ( clip_gain == 1 )
830 : {
831 0 : size -= 10;
832 : }
833 2801 : break;
834 : }
835 : }
836 :
837 : /* calculate predicted gain */
838 5388 : aux[0] = 1.0f;
839 5388 : aux[1] = ctype;
840 5388 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) - 0.5f * (float) log10( Ecode ) );
841 :
842 : /* searching of codebook */
843 5388 : p = cdbk;
844 5388 : dist_min = 3.402823466e+38F;
845 5388 : index = 0;
846 791116 : for ( i = 0; i < size; i++ )
847 : {
848 785728 : g_pitch = *p++;
849 785728 : g_code = gcode0 * *p++;
850 785728 : 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 785728 : if ( dist < dist_min )
853 : {
854 73236 : dist_min = dist;
855 73236 : index = i;
856 : }
857 : }
858 :
859 5388 : *gain_pit = cdbk[index * 2];
860 5388 : *gain_code = cdbk[index * 2 + 1] * gcode0;
861 :
862 5388 : gains_mem[0] = *gain_code;
863 5388 : gains_mem[3] = *gain_pit;
864 : }
865 12078 : else if ( i_subfr == L_SUBFR || ( L_subfr == 2 * L_SUBFR ) )
866 : {
867 5388 : b = b_2sfr;
868 5388 : n_pred = 4;
869 :
870 5388 : switch ( nBits )
871 : {
872 2149 : case 7:
873 : {
874 2149 : cdbk = gp_gamma_2sfr_7b;
875 2149 : if ( clip_gain == 1 )
876 : {
877 0 : size -= 30;
878 : }
879 2149 : break;
880 : }
881 3239 : case 6:
882 : {
883 3239 : cdbk = gp_gamma_2sfr_6b;
884 3239 : if ( clip_gain == 1 )
885 : {
886 0 : size -= 12;
887 : }
888 3239 : break;
889 : }
890 : }
891 :
892 : /* calculate predicted gain */
893 5388 : aux[0] = 1.0f;
894 5388 : aux[1] = ctype;
895 5388 : aux[2] = (float) log10( gains_mem[0] );
896 5388 : aux[3] = gains_mem[3];
897 5388 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
898 :
899 : /* searching of codebook */
900 5388 : p = cdbk;
901 5388 : dist_min = 3.402823466e+38F;
902 5388 : index = 0;
903 487756 : for ( i = 0; i < size; i++ )
904 : {
905 482368 : g_pitch = *p++;
906 482368 : g_code = gcode0 * *p++;
907 482368 : 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 482368 : if ( dist < dist_min )
910 : {
911 86059 : dist_min = dist;
912 86059 : index = i;
913 : }
914 : }
915 5388 : *gain_pit = cdbk[index * 2];
916 5388 : *gain_code = cdbk[index * 2 + 1] * gcode0;
917 :
918 5388 : gains_mem[1] = *gain_code;
919 5388 : gains_mem[4] = *gain_pit;
920 : }
921 6690 : else if ( i_subfr == 2 * L_SUBFR )
922 : {
923 3345 : if ( rf_flag == 1 )
924 : {
925 0 : gains_mem[1] = gains_mem[0];
926 0 : gains_mem[4] = gains_mem[3];
927 : }
928 :
929 3345 : b = b_3sfr;
930 3345 : n_pred = 6;
931 :
932 3345 : switch ( nBits )
933 : {
934 2 : case 7:
935 : {
936 2 : cdbk = gp_gamma_3sfr_7b;
937 2 : if ( clip_gain == 1 )
938 : {
939 0 : size -= 28;
940 : }
941 2 : break;
942 : }
943 3343 : case 6:
944 : {
945 3343 : cdbk = gp_gamma_3sfr_6b;
946 3343 : if ( clip_gain == 1 )
947 : {
948 0 : size -= 11;
949 : }
950 3343 : break;
951 : }
952 : }
953 :
954 : /* calculate predicted gain */
955 3345 : aux[0] = 1.0f;
956 3345 : aux[1] = ctype;
957 3345 : aux[2] = (float) log10( gains_mem[0] );
958 3345 : aux[3] = (float) log10( gains_mem[1] );
959 3345 : aux[4] = gains_mem[3];
960 3345 : aux[5] = gains_mem[4];
961 3345 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
962 :
963 : /* searching of codebook */
964 3345 : p = cdbk;
965 3345 : dist_min = 3.402823466e+38F;
966 3345 : index = 0;
967 217553 : for ( i = 0; i < size; i++ )
968 : {
969 214208 : g_pitch = *p++;
970 214208 : g_code = gcode0 * *p++;
971 214208 : 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 214208 : if ( dist < dist_min )
974 : {
975 49640 : dist_min = dist;
976 49640 : index = i;
977 : }
978 : }
979 3345 : *gain_pit = cdbk[index * 2];
980 3345 : *gain_code = cdbk[index * 2 + 1] * gcode0;
981 :
982 3345 : gains_mem[2] = *gain_code;
983 3345 : gains_mem[5] = *gain_pit;
984 : }
985 3345 : else if ( i_subfr == 3 * L_SUBFR )
986 : {
987 3345 : b = b_4sfr;
988 3345 : n_pred = 8;
989 :
990 3345 : switch ( nBits )
991 : {
992 0 : case 7:
993 : {
994 0 : cdbk = gp_gamma_4sfr_7b;
995 0 : if ( clip_gain == 1 )
996 : {
997 0 : size -= 25;
998 : }
999 0 : break;
1000 : }
1001 3345 : case 6:
1002 : {
1003 3345 : cdbk = gp_gamma_4sfr_6b;
1004 3345 : if ( clip_gain == 1 )
1005 : {
1006 0 : size -= 11;
1007 : }
1008 3345 : break;
1009 : }
1010 : }
1011 :
1012 : /* calculate predicted gain */
1013 3345 : aux[0] = 1.0f;
1014 3345 : aux[1] = ctype;
1015 3345 : aux[2] = (float) log10( gains_mem[0] );
1016 3345 : aux[3] = (float) log10( gains_mem[1] );
1017 3345 : aux[4] = (float) log10( gains_mem[2] );
1018 3345 : aux[5] = gains_mem[3];
1019 3345 : aux[6] = gains_mem[4];
1020 3345 : aux[7] = gains_mem[5];
1021 3345 : gcode0 = (float) pow( 10, dotp( b, aux, n_pred ) );
1022 :
1023 : /* searching of codebook */
1024 3345 : p = cdbk;
1025 3345 : dist_min = 3.402823466e+38F;
1026 3345 : index = 0;
1027 217425 : for ( i = 0; i < size; i++ )
1028 : {
1029 214080 : g_pitch = *p++;
1030 214080 : g_code = gcode0 * *p++;
1031 214080 : 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 214080 : if ( dist < dist_min )
1034 : {
1035 54262 : dist_min = dist;
1036 54262 : index = i;
1037 : }
1038 : }
1039 3345 : *gain_pit = cdbk[index * 2];
1040 3345 : *gain_code = cdbk[index * 2 + 1] * gcode0;
1041 : }
1042 :
1043 17466 : *norm_gain_code = *gain_code / *gain_inov;
1044 :
1045 17466 : push_indice( hBstr, IND_GAIN, index, nBits );
1046 :
1047 17466 : return;
1048 : }
|