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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include <math.h>
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "cnst.h"
47 : #include "wmc_auto.h"
48 : #include "ivas_prot.h"
49 :
50 : /*---------------------------------------------------------------------*
51 : * ComputeSpectrumNoiseMeasure()
52 : *
53 : * compute noise-measure flags for spectrum filling and quantization (0: tonal, 1: noise-like)
54 : *---------------------------------------------------------------------*/
55 :
56 1597145 : void ComputeSpectrumNoiseMeasure(
57 : const float *powerSpec,
58 : const int16_t L_frame,
59 : const int16_t startLine,
60 : const int16_t resetMemory,
61 : int16_t *noiseFlags,
62 : const int16_t lowpassLine )
63 : {
64 : int16_t i, lastTone;
65 : float s, c;
66 :
67 1597145 : if ( resetMemory )
68 : {
69 80495429 : for ( i = 0; i < lowpassLine; i++ )
70 : {
71 80409960 : noiseFlags[i] = 0;
72 : }
73 : }
74 1597145 : for ( i = lowpassLine; i < L_frame; i++ )
75 : {
76 0 : noiseFlags[i] = 1;
77 : }
78 :
79 1597145 : if ( powerSpec && startLine + 6 < L_frame )
80 : {
81 1528535 : lastTone = 0;
82 : /* noise-measure flags for spectrum filling and quantization (0: tonal, 1: noise-like) */
83 1528535 : i = startLine - 1;
84 1528535 : s = powerSpec[i - 7] + powerSpec[i - 6] + powerSpec[i - 5] +
85 1528535 : powerSpec[i - 4] + powerSpec[i - 3] + powerSpec[i - 2] +
86 1528535 : powerSpec[i - 1] + powerSpec[i] + powerSpec[i + 1] +
87 1528535 : powerSpec[i + 2] + powerSpec[i + 3] + powerSpec[i + 4] +
88 1528535 : powerSpec[i + 5] + powerSpec[i + 6] + powerSpec[i + 7];
89 :
90 924872187 : for ( i++; i < lowpassLine - 7; i++ )
91 : {
92 923343652 : c = powerSpec[i - 1] + powerSpec[i] + powerSpec[i + 1];
93 923343652 : s += powerSpec[i + 7] - powerSpec[i - 8];
94 923343652 : if ( s >= ( 1.75f - 0.5f * noiseFlags[i] ) * c )
95 : {
96 917352657 : noiseFlags[i] = 1;
97 : }
98 : else
99 : {
100 5990995 : noiseFlags[i] = 0;
101 5990995 : lastTone = i;
102 : }
103 : }
104 : /* lower L_frame*startRatio lines are tonal (0), upper 7 lines are processed separately */
105 10699745 : for ( ; i < lowpassLine - 1; i++ )
106 : {
107 9171210 : c = powerSpec[i - 1] + powerSpec[i] + powerSpec[i + 1];
108 : /* running sum can't be updated any more, just use the latest one */
109 9171210 : if ( s >= ( 1.75f - 0.5f * noiseFlags[i] ) * c )
110 : {
111 9062676 : noiseFlags[i] = 1;
112 : }
113 : else
114 : {
115 108534 : noiseFlags[i] = 0;
116 : /* lastTone = i; */
117 : }
118 : }
119 1528535 : noiseFlags[i] = 1; /* uppermost line is defined as noise-like (1) */
120 :
121 1528535 : if ( lastTone > 0 ) /* spread uppermost tonal line one line upward */
122 : {
123 318289 : noiseFlags[lastTone + 1] = 0;
124 : }
125 : }
126 :
127 1597145 : return;
128 : }
129 :
130 :
131 : /*---------------------------------------------------------------------*
132 : * detectLowpassFac()
133 : *
134 : *
135 : *---------------------------------------------------------------------*/
136 :
137 161999 : static void detectLowpassFac(
138 : const float *powerSpec,
139 : const int16_t L_frame,
140 : const int16_t rectWin,
141 : float *pLpFac,
142 : const int16_t lowpassLine )
143 : {
144 : int16_t i;
145 : float threshold;
146 :
147 161999 : threshold = 0.1f * 2 * NORM_MDCT_FACTOR;
148 161999 : if ( rectWin )
149 : {
150 : /* compensate for bad side-lobe attenuation with asymmetric windows */
151 10533 : threshold *= 2.f;
152 : }
153 :
154 2367067 : for ( i = lowpassLine - 1; i >= lowpassLine / 2; i-- )
155 : {
156 2359355 : if ( powerSpec[i] > threshold )
157 : {
158 154287 : break;
159 : }
160 : }
161 :
162 161999 : *pLpFac = ( 0.3f * ( *pLpFac ) ) + ( 0.7f * ( (float) ( i + 1 ) / (float) L_frame ) );
163 :
164 161999 : return;
165 : }
166 :
167 : /*-----------------------------------------------------------*
168 : * Compute noise-measure flags for spectrum filling *
169 : * and quantization (0: tonal, 1: noise-like). *
170 : * Detect low pass if present. *
171 : *-----------------------------------------------------------*/
172 :
173 459272 : void AnalyzePowerSpectrum(
174 : Encoder_State *st, /* i/o: encoder states */
175 : const int16_t L_frame, /* i : frame length */
176 : const int16_t L_frameTCX, /* i : full band frame length */
177 : const int16_t left_overlap, /* i : left overlap length */
178 : const int16_t right_overlap, /* i : right overlap length */
179 : const float mdctSpectrum[], /* i : MDCT spectrum */
180 : const float signal[], /* i : windowed signal corresponding to mdctSpectrum */
181 : float powerSpec[] /* o : Power spectrum. Can point to signal */
182 : )
183 : {
184 459272 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
185 : int16_t i, iStart, iEnd, lowpassLine;
186 :
187 459272 : lowpassLine = L_frameTCX;
188 459272 : iStart = 0;
189 459272 : iEnd = L_frameTCX;
190 :
191 459272 : TCX_MDST( signal, powerSpec, left_overlap, L_frameTCX - ( left_overlap + right_overlap ) / 2, right_overlap, st->element_mode );
192 :
193 459272 : if ( st->narrowBand )
194 : {
195 57 : attenuateNbSpectrum( L_frameTCX, powerSpec );
196 : }
197 :
198 : /* power spectrum: MDCT^2 + MDST^2 */
199 410754992 : for ( i = iStart; i < iEnd; i++ )
200 : {
201 410295720 : powerSpec[i] *= powerSpec[i];
202 410295720 : powerSpec[i] += mdctSpectrum[i] * mdctSpectrum[i];
203 : }
204 :
205 459272 : ComputeSpectrumNoiseMeasure( powerSpec, L_frameTCX, hTcxEnc->nmStartLine * L_frame / st->L_frame, ( st->L_frame * st->last_sr_core != st->L_frame_past * st->sr_core ) || ( st->last_core != TCX_20_CORE ), hTcxEnc->memQuantZeros, lowpassLine );
206 :
207 459272 : if ( st->total_brate <= ACELP_24k40 )
208 : {
209 161999 : lowpassLine = (int16_t) ( 2.0f * st->hTcxCfg->bandwidth * L_frame );
210 161999 : detectLowpassFac( powerSpec, L_frame, ( st->last_core == ACELP_CORE ), &hTcxEnc->measuredBwRatio, lowpassLine );
211 : }
212 : else
213 : {
214 297273 : hTcxEnc->measuredBwRatio = 1.0f;
215 : }
216 :
217 459272 : return;
218 : }
219 :
220 :
221 : /*---------------------------------------------------------------------*
222 : * mdct_preShaping()
223 : *
224 : *
225 : *---------------------------------------------------------------------*/
226 :
227 1116463 : void mdct_preShaping(
228 : float x[],
229 : const int16_t lg,
230 : const float gains[] )
231 : {
232 : int16_t i, j, k, l;
233 : float g;
234 : int16_t m, n, k1, k2;
235 :
236 1116463 : j = 0; /* not counted, is included in ptr init */
237 : /* FDNS_NPTS = 64 !!! */
238 1116463 : k = lg / FDNS_NPTS;
239 1116463 : m = lg % FDNS_NPTS;
240 1116463 : if ( m )
241 : {
242 33311 : if ( m <= ( FDNS_NPTS / 2 ) )
243 : {
244 33311 : n = FDNS_NPTS / m;
245 33311 : k1 = k;
246 33311 : k2 = k + 1;
247 : }
248 : else
249 : {
250 0 : n = FDNS_NPTS / ( FDNS_NPTS - m );
251 0 : k1 = k + 1;
252 0 : k2 = k;
253 : }
254 2165215 : for ( i = 0; i < lg; )
255 : {
256 2131904 : if ( j % n )
257 : {
258 1574864 : k = k1;
259 : }
260 : else
261 : {
262 557040 : k = k2;
263 : }
264 2131904 : g = 1.f / gains[j++];
265 :
266 : /* Limit number of loops, if end is reached */
267 2131904 : k = min( k, lg - i );
268 15282224 : for ( l = 0; l < k; l++ )
269 : {
270 13150320 : x[i++] *= g;
271 : }
272 : }
273 : }
274 : else
275 : {
276 70404880 : for ( i = 0; i < lg; )
277 : {
278 69321728 : g = 1.f / gains[j++];
279 436616896 : for ( l = 0; l < k; l++ )
280 : {
281 367295168 : x[i++] *= g;
282 : }
283 : }
284 : }
285 :
286 1116463 : return;
287 : }
288 :
289 :
290 : /*---------------------------------------------------------------------*
291 : * AdaptLowFreqEmph()
292 : *
293 : *
294 : *---------------------------------------------------------------------*/
295 :
296 1419931 : void AdaptLowFreqEmph(
297 : float x[],
298 : int16_t xq[],
299 : float invGain,
300 : const int16_t tcx_lpc_shaped_ari,
301 : const float lpcGains[],
302 : const int16_t lg )
303 : {
304 : int16_t i, i_max, i_max_old;
305 :
306 1419931 : if ( !tcx_lpc_shaped_ari )
307 : {
308 : /* 1. find first magnitude maximum in lower quarter of spectrum */
309 1383336 : invGain *= 2.0f;
310 1383336 : i_max = -1;
311 21794112 : for ( i = 0; i < lg / 4; i++ )
312 : {
313 21717251 : if ( ( ( xq[i] <= -2 ) || ( xq[i] >= 2 ) ) &&
314 1429824 : ( ( invGain * x[i] <= -3.625f ) || ( invGain * x[i] >= 3.625f ) ) )
315 : {
316 1306475 : xq[i] += ( xq[i] < 0 ) ? -2 : 2;
317 1306475 : i_max = i;
318 1306475 : break;
319 : }
320 : }
321 : /* 2. compress value range of all xq up to i_max: add two steps */
322 5946352 : for ( i = 0; i < i_max; i++ )
323 : {
324 4563016 : if ( x[i] < 0.0f )
325 : {
326 2238694 : xq[i] = (int16_t) ( invGain * x[i] - 0.375f );
327 : }
328 : else
329 : {
330 2324322 : xq[i] = (int16_t) ( invGain * x[i] + 0.375f );
331 : }
332 : }
333 : /* 3. find first mag. maximum below i_max which is half as high */
334 1383336 : i_max_old = i_max;
335 1383336 : if ( i_max_old > -1 )
336 : {
337 1306475 : invGain *= 2.0f;
338 1306475 : i_max = -1; /* reset first maximum, update inverse gain */
339 3926485 : for ( i = 0; i < lg / 4; i++ )
340 : {
341 3926485 : if ( ( ( xq[i] <= -2 ) || ( xq[i] >= 2 ) ) &&
342 1400425 : ( ( invGain * x[i] <= -3.625f ) || ( invGain * x[i] >= 3.625f ) ) )
343 : {
344 1306475 : xq[i] += ( xq[i] < 0 ) ? -2 : 2;
345 1306475 : i_max = i;
346 1306475 : break;
347 : }
348 : }
349 : }
350 : /* 4. re-compress and quantize all xq up to half-height i_max+1 */
351 4003346 : for ( i = 0; i < i_max; i++ )
352 : {
353 2620010 : if ( x[i] < 0.0f )
354 : {
355 1288700 : xq[i] = (int16_t) ( invGain * x[i] - 0.375f );
356 : }
357 : else
358 : {
359 1331310 : xq[i] = (int16_t) ( invGain * x[i] + 0.375f );
360 : }
361 : }
362 : /* 5. always compress 2 lines; lines could be at index 0 and 1! */
363 1383336 : if ( i_max_old > -1 )
364 : {
365 1306475 : invGain *= 0.5f; /* reset inverse gain */
366 1306475 : if ( i_max < i_max_old )
367 : {
368 519486 : i_max = i_max_old;
369 : }
370 : }
371 1383336 : i = i_max + 1;
372 :
373 1383336 : if ( x[i] < 0.0f )
374 : {
375 682215 : xq[i] = ( invGain * x[i] <= -3.625f ) ? xq[i] - 2 : (int16_t) ( invGain * x[i] - 0.375f );
376 : }
377 : else
378 : {
379 701121 : xq[i] = ( invGain * x[i] >= 3.625f ) ? xq[i] + 2 : (int16_t) ( invGain * x[i] + 0.375f );
380 : }
381 1383336 : i++;
382 1383336 : if ( x[i] < 0.0f )
383 : {
384 691229 : xq[i] = ( invGain * x[i] <= -3.625f ) ? xq[i] - 2 : (int16_t) ( invGain * x[i] - 0.375f );
385 : }
386 : else
387 : {
388 692107 : xq[i] = ( invGain * x[i] >= 3.625f ) ? xq[i] + 2 : (int16_t) ( invGain * x[i] + 0.375f );
389 : }
390 : }
391 : else
392 : {
393 36595 : PsychAdaptLowFreqEmph( x, lpcGains );
394 : }
395 :
396 1419931 : return;
397 : }
398 :
399 :
400 : /*---------------------------------------------------------------------*
401 : * PsychAdaptLowFreqEmph()
402 : *
403 : *
404 : *---------------------------------------------------------------------*/
405 :
406 65447 : void PsychAdaptLowFreqEmph(
407 : float x[],
408 : const float lpcGains[] )
409 : {
410 : int16_t i;
411 : float max_val, fac, tmp;
412 65447 : max_val = tmp = lpcGains[0];
413 :
414 : /* find minimum (tmp) and maximum (max) of LPC gains in low frequencies */
415 589023 : for ( i = 1; i < 9; i++ )
416 : {
417 523576 : if ( tmp > lpcGains[i] )
418 : {
419 332086 : tmp = lpcGains[i];
420 : }
421 523576 : if ( max_val < lpcGains[i] )
422 : {
423 129258 : max_val = lpcGains[i];
424 : }
425 : }
426 65447 : tmp *= 32.0f;
427 65447 : if ( ( max_val < tmp ) && ( max_val > FLT_MIN ) )
428 : {
429 65447 : fac = tmp = (float) pow( tmp / max_val, 0.0078125f );
430 :
431 : /* gradual boosting of lowest 32 bins; DC is boosted by (tmp/max)^1/4 */
432 2159751 : for ( i = 31; i >= 0; i-- )
433 : {
434 2094304 : x[i] *= fac;
435 2094304 : fac *= tmp;
436 : }
437 : }
438 :
439 65447 : return;
440 : }
441 :
442 : /*---------------------------------------------------------------------*
443 : * SQ_gain()
444 : *
445 : *
446 : *---------------------------------------------------------------------*/
447 :
448 : /*! r: SQ gain */
449 441093 : float SQ_gain(
450 : const float x[], /* i : vector to quantize */
451 : const int16_t nbitsSQ, /* i : number of bits targeted */
452 : const int16_t lg /* i : vector size (2048 max) */
453 : )
454 : {
455 : int16_t i, iter;
456 : float ener, tmp, target, fac, offset;
457 : float en[N_MAX / 2];
458 :
459 : /* energy of quadruples with 9dB offset */
460 90404433 : for ( i = 0; i < lg; i += 4 )
461 : {
462 89963340 : ener = 0.01f + x[i] * x[i] + x[i + 1] * x[i + 1] + x[i + 2] * x[i + 2] + x[i + 3] * x[i + 3];
463 89963340 : en[i >> 2] = (float) log10( ener );
464 : }
465 :
466 : /* SQ scale: 4 bits / 6 dB per quadruple */
467 441093 : target = 0.15f * (float) ( nbitsSQ - ( lg >> 4 ) );
468 441093 : fac = 12.8f;
469 441093 : offset = fac;
470 :
471 : /* find offset (0 to 128 dB with step of 0.125dB) */
472 4852023 : for ( iter = 0; iter < 10; iter++ )
473 : {
474 4410930 : fac *= 0.5f;
475 4410930 : offset -= fac;
476 4410930 : ener = 0.0f;
477 :
478 700057842 : for ( i = 0; i < lg / 4; i++ )
479 : {
480 697273315 : tmp = en[i] - offset;
481 :
482 : /* avoid SV with 1 bin of amp < 0.5f */
483 697273315 : if ( tmp > 0.3f )
484 : {
485 272463135 : ener += tmp;
486 :
487 : /* if ener is above target -> break and increase offset */
488 272463135 : if ( ener > target )
489 : {
490 1626403 : offset += fac;
491 1626403 : break;
492 : }
493 : }
494 : }
495 : }
496 :
497 : /* return gain */
498 441093 : return (float) pow( 10.0f, 0.45f + 0.5f * offset );
499 : }
500 :
501 :
502 : /*---------------------------------------------------------------------*
503 : * SQ_gain_estimate()
504 : *
505 : *
506 : *---------------------------------------------------------------------*/
507 :
508 : /*! r: SQ gain */
509 1904033 : float SQ_gain_estimate(
510 : const float x[], /* i : vector to quantize */
511 : const int16_t nbitsSQ, /* i : number of bits targeted */
512 : const int16_t lg /* i : vector size (2048 max) */
513 : )
514 : {
515 : int16_t i, iter, max_iter;
516 : float ener, tmp, target, fac, offset;
517 : float en[N_MAX / 2];
518 1904033 : float minGainInv = 0.5f * (float) log10( (float) lg / (float) NORM_MDCT_FACTOR ); /* lowest possible quantized gain */
519 :
520 1904033 : tmp = minGainInv + 0.94f; /* lowest gain + expected value of the quantization noise energy per quadruple (log10(4/12)) */
521 : /* SNR of quadruples for unit step quantizer and lowest possible gain */
522 400184933 : for ( i = 0; i < lg; i += 4 )
523 : {
524 398280900 : ener = 0.01f + x[i] * x[i] + x[i + 1] * x[i + 1] + x[i + 2] * x[i + 2] + x[i + 3] * x[i + 3];
525 398280900 : en[i >> 2] = (float) log10( ener ) + tmp;
526 : }
527 :
528 : /* SQ scale: 4 bits / 6 dB per quadruple */
529 1904033 : target = 0.15f * (float) ( nbitsSQ - ( lg >> 4 ) );
530 1904033 : fac = 18.4f;
531 1904033 : max_iter = 8; /* for 0.36dB steps */
532 1904033 : offset = fac - fac / (float) ( 2 << max_iter );
533 :
534 : /* find offset, resolution similar to SQ gain quantizer resolution (92dB range, 0.719db resolution) */
535 17136297 : for ( iter = 0; iter < max_iter; iter++ )
536 : {
537 15232264 : fac *= 0.5f;
538 15232264 : offset -= fac;
539 15232264 : ener = 0.0f;
540 :
541 2370632703 : for ( i = 0; i < lg / 4; i++ )
542 : {
543 2361509635 : tmp = en[i] - offset;
544 :
545 : /* avoid SV with 1 bin of amp < 0.5f */
546 2361509635 : if ( tmp > 0.3f )
547 : {
548 808698532 : ener += tmp;
549 :
550 : /* if SNR is above target -> break and increase offset */
551 808698532 : if ( ener > target )
552 : {
553 6109196 : offset += fac;
554 6109196 : break;
555 : }
556 : }
557 : }
558 : }
559 :
560 : /* return gain */
561 1904033 : return (float) pow( 10.0f, 0.5f * offset - minGainInv );
562 : }
563 :
564 : /*---------------------------------------------------------------------*
565 : * tcx_scalar_quantization()
566 : *
567 : *
568 : *---------------------------------------------------------------------*/
569 :
570 11946378 : void tcx_scalar_quantization(
571 : float *x, /* i : input coefficients */
572 : int16_t *xq, /* o : quantized coefficients */
573 : const int16_t L_frame, /* i : frame length */
574 : const float gain, /* i : quantization gain */
575 : const float offset, /* i : rounding offset (deadzone) */
576 : int16_t *memQuantZeros, /* o : coefficients set to 0 */
577 : const int16_t tcxonly )
578 : {
579 : int16_t i;
580 : float gainInv, rounding, rounding2;
581 :
582 : /* Init scalar quantizer */
583 11946378 : gainInv = 1.0f / gain;
584 11946378 : rounding = offset;
585 11946378 : rounding2 = -offset;
586 :
587 11946378 : if ( memQuantZeros == NULL )
588 : {
589 1754440 : i = L_frame - 1;
590 : }
591 : else
592 : {
593 3036819505 : for ( i = L_frame - 1; ( memQuantZeros[i] ) && ( (float) fabs( x[i] ) * gainInv < 1.0f ); i-- )
594 : {
595 3026627567 : xq[i] = 0;
596 : }
597 : }
598 :
599 6464058043 : for ( ; i >= 0; i-- )
600 : {
601 6452111665 : if ( x[i] > 0.f )
602 : {
603 2980981825 : xq[i] = ( (int16_t) min( MAX16B, ( rounding + x[i] * gainInv ) ) );
604 : }
605 : else
606 : {
607 : /* limit to -32767 (although 16 bit would allow -32768) - avoids potential issues in arithmetic coding routines, where a sign flip is happening */
608 3471129840 : xq[i] = ( (int16_t) max( -32767, ( rounding2 + x[i] * gainInv ) ) );
609 : }
610 : }
611 :
612 11946378 : if ( !tcxonly )
613 : {
614 1383336 : AdaptLowFreqEmph( x, xq, gainInv, 0, NULL, L_frame );
615 : }
616 :
617 11946378 : return;
618 : }
619 :
620 :
621 : /*---------------------------------------------------------------------*
622 : * tcx_scalar_quantization_rateloop()
623 : *
624 : *
625 : *---------------------------------------------------------------------*/
626 :
627 2345126 : int16_t tcx_scalar_quantization_rateloop(
628 : float *x, /* i : input coefficients */
629 : int16_t *xq, /* o : quantized coefficients */
630 : const int16_t L_frame, /* i : frame length */
631 : float *gain, /* i/o: quantization gain */
632 : float offset, /* i : rounding offset (deadzone) */
633 : int16_t *memQuantZeros, /* o : coefficients set to 0 */
634 : int16_t *lastnz_out, /* i/o: last nonzero coeff index */
635 : const int16_t target, /* i : target number of bits */
636 : int16_t *nEncoded, /* o : number of encoded coeff */
637 : int16_t *stop, /* i/o: stop param */
638 : const int16_t sqBits_in_noStop, /* i : number of sqBits as determined in prev. quant. stage, w/o using stop mechanism (ie might exceed target bits) */
639 : const int16_t sqBits_in, /* i : number of sqBits as determined in prev. quant. stage, using stop mechanism (ie always <= target bits) */
640 : const int16_t tcxRateLoopOpt, /* i : turns on/off rateloop optimization */
641 : const int16_t tcxonly,
642 : CONTEXT_HM_CONFIG *hm_cfg,
643 : const int16_t iter_max,
644 : const int16_t element_mode )
645 : {
646 : int16_t sqBits, stopFlag;
647 : int16_t ubfound, lbfound;
648 2345126 : float ub = 0.f, lb = 0.f;
649 : float shift;
650 : int16_t iter;
651 : float sqGain;
652 : float w_lb, w_ub;
653 2345126 : const int16_t kDampen = 10;
654 : int16_t old_stopFlag, old_nEncoded, old_sqBits;
655 : float mod_adjust0, mod_adjust1;
656 : float inv_target;
657 2345126 : const float kMargin = 0.96f;
658 : int16_t lastnz;
659 : int16_t saturated;
660 : float minSqGain;
661 :
662 : /* Init */
663 2345126 : saturated = 0;
664 2345126 : minSqGain = (float) sqrt( (float) NORM_MDCT_FACTOR / (float) L_frame );
665 :
666 2345126 : sqGain = *gain;
667 2345126 : stopFlag = *stop;
668 2345126 : ubfound = 0;
669 2345126 : lbfound = 0;
670 2345126 : shift = 0.25f;
671 2345126 : w_lb = 0.0f;
672 2345126 : w_ub = 0.0f;
673 2345126 : lastnz = *lastnz_out;
674 :
675 2345126 : old_stopFlag = stopFlag;
676 2345126 : old_nEncoded = *nEncoded;
677 2345126 : old_sqBits = sqBits_in_noStop;
678 :
679 2345126 : sqBits = sqBits_in;
680 :
681 2345126 : mod_adjust0 = max( 1.0f, 2.3f - 0.0025f * target );
682 2345126 : mod_adjust1 = 1.0f / mod_adjust0;
683 :
684 2345126 : inv_target = 1.0f / (float) target;
685 :
686 : /* Loop */
687 7832966 : for ( iter = 0; iter < iter_max; iter++ )
688 : {
689 5572438 : if ( tcxRateLoopOpt >= 2 )
690 : {
691 : /* Ajust sqGain */
692 4650214 : if ( stopFlag )
693 : {
694 2161697 : lbfound = 1;
695 2161697 : lb = sqGain;
696 2161697 : w_lb = (float) ( stopFlag - target + kDampen );
697 2161697 : saturated = 0;
698 :
699 2161697 : if ( ubfound )
700 : {
701 912436 : sqGain = ( lb * w_ub + ub * w_lb ) / ( w_ub + w_lb );
702 : }
703 : else
704 : {
705 1249261 : sqGain *= ( 1.0f + ( (float) ( stopFlag / kMargin ) * inv_target - 1.0f ) * mod_adjust0 );
706 : }
707 : }
708 2488517 : else if ( saturated == 0 )
709 : {
710 2403919 : ubfound = 1;
711 2403919 : ub = sqGain;
712 2403919 : w_ub = (float) ( target - sqBits + kDampen );
713 2403919 : if ( lbfound )
714 : {
715 1116602 : sqGain = ( lb * w_ub + ub * w_lb ) / ( w_ub + w_lb );
716 : }
717 : else
718 : {
719 1287317 : sqGain *= ( 1.0f - ( 1.0f - (float) ( sqBits * kMargin ) * inv_target ) * mod_adjust1 );
720 : }
721 :
722 2403919 : if ( tcxRateLoopOpt == 3 && sqGain < minSqGain )
723 : {
724 86568 : sqGain = minSqGain;
725 86568 : saturated = 1;
726 : }
727 : }
728 : else
729 : {
730 84598 : break; /* we cannot go any lower anyway*/
731 : }
732 : }
733 : else /* tcxRateLoopOpt != 2 */
734 : {
735 : /* Ajust sqGain */
736 922224 : if ( stopFlag )
737 : {
738 384135 : lbfound = 1;
739 384135 : lb = sqGain;
740 384135 : if ( ubfound )
741 : {
742 278773 : sqGain = (float) sqrt( lb * ub );
743 : }
744 : else
745 : {
746 105362 : sqGain = sqGain * (float) pow( 10.0f, shift / 10.0f );
747 105362 : shift *= 2.0f;
748 : }
749 : }
750 : else
751 : {
752 538089 : ubfound = 1;
753 538089 : ub = sqGain;
754 538089 : if ( lbfound )
755 : {
756 211777 : sqGain = (float) sqrt( lb * ub );
757 : }
758 : else
759 : {
760 326312 : sqGain = sqGain * (float) pow( 10.0f, -shift / 10.0f );
761 326312 : shift *= 2.0f;
762 : }
763 : }
764 : }
765 :
766 : /* Quantize spectrum */
767 5487840 : tcx_scalar_quantization( x, xq, L_frame, sqGain, offset, memQuantZeros, tcxonly );
768 :
769 : /* Estimate bitrate */
770 5487840 : if ( tcxRateLoopOpt >= 1 )
771 : {
772 5487840 : stopFlag = 0;
773 : }
774 : else
775 : {
776 0 : stopFlag = 1;
777 : }
778 :
779 5487840 : if ( element_mode > EVS_MONO )
780 : {
781 5470960 : sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( xq, L_frame, &lastnz, nEncoded, target, &stopFlag, 0, hm_cfg );
782 : }
783 : else
784 : {
785 16880 : sqBits = ACcontextMapping_encode2_estimate_no_mem_s17_LC( xq, L_frame, &lastnz, nEncoded, target, &stopFlag, hm_cfg );
786 : }
787 :
788 5487840 : if ( tcxRateLoopOpt >= 1 )
789 : {
790 5487840 : if ( ( *nEncoded >= old_nEncoded && ( stopFlag >= old_stopFlag ) ) || ( *nEncoded > old_nEncoded && ( stopFlag == 0 && old_stopFlag > 0 ) ) || ( stopFlag == 0 && old_stopFlag == 0 ) )
791 : {
792 2996551 : *gain = sqGain;
793 2996551 : old_nEncoded = *nEncoded;
794 2996551 : old_stopFlag = stopFlag;
795 2996551 : old_sqBits = sqBits;
796 2996551 : *lastnz_out = lastnz;
797 : }
798 : }
799 : } /* for ( iter=0; iter<iter_max; iter++ ) */
800 :
801 2345126 : if ( tcxRateLoopOpt >= 1 )
802 : {
803 : /* Quantize spectrum */
804 2345126 : tcx_scalar_quantization( x, xq, L_frame, *gain, offset, memQuantZeros, tcxonly );
805 :
806 : /* Output */
807 2345126 : *nEncoded = old_nEncoded;
808 2345126 : sqBits = old_sqBits;
809 2345126 : *stop = old_stopFlag;
810 : }
811 : else
812 : {
813 : /* Output */
814 0 : *gain = sqGain;
815 0 : *stop = stopFlag;
816 0 : *lastnz_out = lastnz;
817 : }
818 :
819 2345126 : return sqBits;
820 : }
821 :
822 : /*---------------------------------------------------------------------*
823 : * tcx_QuantizeGain()
824 : *
825 : *
826 : *---------------------------------------------------------------------*/
827 :
828 2381721 : void tcx_QuantizeGain(
829 : const int16_t n,
830 : float *pGain,
831 : int16_t *pQuantizedGain )
832 : {
833 : float ener, gain;
834 : int16_t quantizedGain;
835 :
836 2381721 : ener = (float) sqrt( (float) n / (float) NORM_MDCT_FACTOR );
837 :
838 2381721 : gain = *pGain * ener;
839 :
840 2381721 : assert( gain > 0 );
841 :
842 : /* quantize gain with step of 0.714 dB */
843 2381721 : quantizedGain = (int16_t) floor( 0.5f + 28.0f * (float) log10( gain ) );
844 :
845 2381721 : if ( quantizedGain < 0 )
846 : {
847 9265 : quantizedGain = 0;
848 : }
849 2381721 : if ( quantizedGain > 127 )
850 : {
851 0 : quantizedGain = 127;
852 : }
853 :
854 2381721 : *pQuantizedGain = quantizedGain;
855 2381721 : *pGain = (float) pow( 10.0f, ( (float) quantizedGain ) / 28.0f ) / ener;
856 :
857 2381721 : return;
858 : }
859 :
860 : /*---------------------------------------------------------------------*
861 : * tcx_noise_factor()
862 : *
863 : *
864 : *---------------------------------------------------------------------*/
865 :
866 2241591 : void tcx_noise_factor(
867 : const float *x_orig, /* i : unquantized mdct coefficients */
868 : float *sqQ, /* i/o: quantized mdct coefficients */
869 : const int16_t iFirstLine, /* i : first coefficient to be considered */
870 : const int16_t lowpassLine, /* i : last nonzero coefficients after low-pass */
871 : const int16_t nTransWidth, /* i : minimum size of hole to be checked */
872 : const int16_t L_frame, /* i : frame length */
873 : const float gain_tcx, /* i : tcx gain */
874 : const float tiltCompFactor, /* i : LPC tilt compensation factor */
875 : float *fac_ns, /* o : noise factor */
876 : int16_t *quantized_fac_ns, /* o : quantized noise factor */
877 : const int16_t element_mode /* i : IVAS element mode */
878 : )
879 : {
880 : int16_t i, k, win, segmentOffset;
881 2241591 : float inv_gain2 = 0.f, sqErrorNrg, n, tilt_factor, tmp;
882 : float att; /* noise level attenuation factor for transient windows */
883 :
884 : /*Adjust noise filling level*/
885 2241591 : sqErrorNrg = 0.0f;
886 2241591 : n = 0.0f;
887 : /* max() */
888 2241591 : tilt_factor = 1.0f / (float) pow( max( 0.375f, tiltCompFactor ), 1.0f / (float) L_frame ); /* 1/(a^b) = a^-b */
889 2241591 : inv_gain2 = 1.0f / ( (float) ( nTransWidth * nTransWidth ) * gain_tcx );
890 :
891 : /* find last nonzero line below iFirstLine, use it as start offset */
892 2241591 : i = iFirstLine;
893 :
894 2241591 : if ( element_mode == IVAS_CPE_MDCT ) /* ... but only in mono or parametric stereo since it may cause binaural unmasking in discrete stereo */
895 : {
896 1814644 : segmentOffset = i;
897 : }
898 : else
899 : {
900 1862178 : for ( ; i > ( iFirstLine >> 1 ); i-- )
901 : {
902 1835274 : if ( sqQ[i] != 0.0f )
903 : {
904 400043 : break;
905 : }
906 : }
907 426947 : inv_gain2 *= (float) pow( tilt_factor, (float) i );
908 426947 : segmentOffset = ++i;
909 : }
910 :
911 2241591 : if ( nTransWidth <= 3 )
912 : {
913 114076 : att = tmp = FLT_MIN;
914 11954744 : for ( k = i & 0xFFFE; k < lowpassLine; k++ )
915 : {
916 11840668 : att += x_orig[k] * x_orig[k]; /* even-index bins, left sub-win */
917 11840668 : k++;
918 11840668 : tmp += x_orig[k] * x_orig[k]; /* odd-index bins, right sub-win */
919 : }
920 114076 : att = (float) sqrt( ( min( att, tmp ) * 2.0f ) / ( att + tmp ) );
921 : }
922 : else
923 : {
924 2127515 : att = 1.0f;
925 : }
926 2241591 : win = 0;
927 896921303 : for ( ; i < lowpassLine; i++ )
928 : {
929 894679712 : inv_gain2 *= tilt_factor;
930 894679712 : if ( sqQ[i] != 0 ) /* current line is not zero, so reset pointers */
931 : {
932 209566049 : if ( win > 0 ) /* add segment sum to sum of segment magnitudes */
933 : {
934 96108875 : k = i - segmentOffset;
935 96108875 : if ( nTransWidth <= 3 )
936 : {
937 2886786 : n += ( k > 2 * nTransWidth - 4 ) ? (float) ( k - nTransWidth + 1 )
938 2886786 : : (float) ( k * k ) * 0.28125f / nTransWidth; /* table lookup instead of */
939 : }
940 : else
941 : {
942 93222089 : n += ( k > 12 ) ? (float) k - 7.0f : (float) ( k * k ) * 0.03515625f;
943 : }
944 241722114 : for ( k = segmentOffset; k < i - win; k++ )
945 : {
946 145613239 : sqErrorNrg += sqQ[k] * (float) nTransWidth;
947 145613239 : sqQ[k] = 0;
948 : }
949 374781877 : for ( ; win > 0; win-- )
950 : {
951 278673002 : sqErrorNrg += sqQ[k] * (float) win;
952 278673002 : sqQ[k++] = 0;
953 : }
954 : }
955 209566049 : segmentOffset = i + 1; /* new segment might start at next line */
956 : }
957 : else /* current line is zero, so update pointers & segment sum */
958 : {
959 685113663 : if ( win < nTransWidth )
960 : {
961 292603800 : win++;
962 : }
963 : /* update segment sum: magnitudes scaled by smoothing function */
964 685113663 : sqQ[i] = (float) fabs( x_orig[i] ) * (float) win * inv_gain2;
965 : }
966 : }
967 2241591 : if ( win > 0 ) /* add last segment sum to sum of segment magnitudes */
968 : {
969 2055772 : k = i - segmentOffset;
970 2055772 : if ( nTransWidth <= 3 )
971 : {
972 103498 : n += ( k > 2 * nTransWidth - 4 ) ? (float) ( k - nTransWidth + 1 )
973 103498 : : (float) ( k * k ) * 0.28125f / nTransWidth; /* table lookup instead of */
974 : }
975 : else
976 : {
977 1952274 : n += ( k > 12 ) ? (float) k - 7.0f : (float) ( k * k ) * 0.03515625f;
978 : }
979 248952396 : for ( k = segmentOffset; k < i - win; k++ )
980 : {
981 246896624 : sqErrorNrg += sqQ[k] * (float) nTransWidth;
982 246896624 : sqQ[k] = 0;
983 : }
984 15986570 : for ( ; win > 0; win-- )
985 : {
986 13930798 : sqErrorNrg += sqQ[k] * (float) win;
987 13930798 : sqQ[k++] = 0;
988 : }
989 : }
990 :
991 : /* noise level factor: average of segment magnitudes of noise bins */
992 2241591 : if ( n > 0.0f )
993 : {
994 2241591 : *fac_ns = ( sqErrorNrg * att ) / n;
995 : }
996 : else
997 : {
998 0 : *fac_ns = 0.0f;
999 : }
1000 :
1001 : /* quantize, dequantize noise level factor (range 0.09375 - 0.65625) */
1002 2241591 : *quantized_fac_ns = (int16_t) ( 0.5f + *fac_ns * 1.34375f * ( 1 << NBITS_NOISE_FILL_LEVEL ) );
1003 2241591 : if ( *quantized_fac_ns > ( 1 << NBITS_NOISE_FILL_LEVEL ) - 1 )
1004 : {
1005 78707 : *quantized_fac_ns = ( 1 << NBITS_NOISE_FILL_LEVEL ) - 1;
1006 : }
1007 2241591 : *fac_ns = (float) ( *quantized_fac_ns ) * 0.75f / ( 1 << NBITS_NOISE_FILL_LEVEL );
1008 :
1009 2241591 : return;
1010 : }
1011 :
1012 :
1013 : /*---------------------------------------------------------------------*
1014 : * tcx_encoder_memory_update()
1015 : *
1016 : *
1017 : *---------------------------------------------------------------------*/
1018 :
1019 473989 : void tcx_encoder_memory_update(
1020 : Encoder_State *st, /* i/o: encoder memory state */
1021 : float *xn_buf, /* i/o: mdct output buffer/time domain weigthed synthesis */
1022 : const float *Ai, /* i : Unquantized (interpolated) LPC coefficients */
1023 : const float *A /* i : Quantized LPC coefficients */
1024 : )
1025 : {
1026 : int16_t L_frame_glob;
1027 : float tmp;
1028 : float buf[1 + M + L_FRAME_PLUS];
1029 : float *synth;
1030 473989 : LPD_state *LPDmem = st->hLPDmem;
1031 :
1032 473989 : L_frame_glob = st->L_frame;
1033 :
1034 : /* Output synth */
1035 473989 : mvr2r( xn_buf, st->synth, L_frame_glob );
1036 :
1037 : /* Update synth */
1038 473989 : synth = buf + 1 + M;
1039 473989 : mvr2r( LPDmem->syn, buf, 1 + M );
1040 :
1041 473989 : mvr2r( xn_buf, synth, L_frame_glob );
1042 473989 : mvr2r( synth + L_frame_glob - M - 1, LPDmem->syn, 1 + M );
1043 :
1044 473989 : if ( !st->tcxonly )
1045 : {
1046 : /* Update weighted synthesis */
1047 267151 : residu( Ai + ( st->nb_subfr - 1 ) * ( M + 1 ), M, synth + L_frame_glob - 1, &tmp, 1 );
1048 267151 : LPDmem->mem_w0 = st->wspeech_enc[L_frame_glob - 1] - tmp;
1049 : }
1050 :
1051 : /* Emphasis of synth -> synth_pe */
1052 473989 : tmp = synth[-M - 1];
1053 473989 : preemph( synth - M, st->hTcxCfg->preemph_fac, M + L_frame_glob, &tmp );
1054 473989 : mvr2r( synth + L_frame_glob - M, LPDmem->mem_syn, M );
1055 473989 : mvr2r( synth + L_frame_glob - M, LPDmem->mem_syn2, M );
1056 473989 : mvr2r( synth + L_frame_glob - L_SYN_MEM, LPDmem->mem_syn_r, L_SYN_MEM );
1057 :
1058 473989 : if ( !st->tcxonly || ( L_frame_glob == L_FRAME16k ) )
1059 : {
1060 : /* Update excitation */
1061 281861 : if ( L_frame_glob < L_EXC_MEM )
1062 : {
1063 96368 : mvr2r( LPDmem->old_exc + ( L_frame_glob ), LPDmem->old_exc, L_EXC_MEM - L_frame_glob );
1064 96368 : residu( A, M, synth, LPDmem->old_exc + L_EXC_MEM - ( L_frame_glob ), L_frame_glob );
1065 : }
1066 : else
1067 : {
1068 185493 : residu( A, M, synth + (L_frame_glob) -L_EXC_MEM, LPDmem->old_exc, L_EXC_MEM );
1069 : }
1070 : }
1071 :
1072 473989 : return;
1073 : }
1074 :
1075 :
1076 : /*---------------------------------------------------------------------*
1077 : * tcx_ari_res_Q_spec()
1078 : *
1079 : * Residual Quantization
1080 : *---------------------------------------------------------------------*/
1081 :
1082 : /*! r: number of bits used (including "bits") */
1083 36595 : int16_t tcx_ari_res_Q_spec(
1084 : const float x_orig[], /* i : original spectrum */
1085 : const int16_t signs[], /* i : signs (x_orig[.]<0) */
1086 : float x_Q[], /* i/o: quantized spectrum */
1087 : const int16_t L_frame, /* i : number of lines */
1088 : const float gain, /* i : TCX gain */
1089 : int16_t prm[], /* o : bitstream */
1090 : int16_t target_bits, /* i : number of bits available */
1091 : int16_t bits, /* i : number of bits used so far */
1092 : const float deadzone, /* i : quantizer deadzone */
1093 : const float x_fac[] /* i : spectrum post-quantization factors */
1094 : )
1095 : {
1096 : int16_t i, j, num_zeros;
1097 : int16_t zeros[L_FRAME_PLUS];
1098 : float fac_m, fac_p, thres, x_Q_m, x_Q_p;
1099 :
1100 : /* Limit the number of residual bits */
1101 36595 : target_bits = min( target_bits, NPRM_RESQ );
1102 :
1103 :
1104 : /* Requantize the spectrum line-by-line */
1105 36595 : fac_m = deadzone * 0.5f;
1106 36595 : fac_p = 0.5f - fac_m;
1107 36595 : num_zeros = 0;
1108 :
1109 36595 : if ( x_fac == NULL )
1110 : {
1111 0 : for ( i = 0; i < L_frame; ++i )
1112 : {
1113 0 : if ( bits >= target_bits )
1114 : {
1115 : /* no bits left */
1116 0 : break;
1117 : }
1118 0 : if ( x_Q[i] != 0 )
1119 : {
1120 0 : int16_t sign = ( 1 - 2 * signs[i] );
1121 :
1122 0 : x_Q_m = x_Q[i] - sign * fac_m;
1123 0 : x_Q_p = x_Q[i] + sign * fac_p;
1124 0 : if ( fabs( x_orig[i] - gain * x_Q_m ) < fabs( x_orig[i] - gain * x_Q_p ) ) /* Decrease magnitude */
1125 : {
1126 0 : x_Q[i] = x_Q_m;
1127 0 : prm[bits++] = 0;
1128 : }
1129 : else /* Increase magnitude */
1130 : {
1131 0 : x_Q[i] = x_Q_p;
1132 0 : prm[bits++] = 1;
1133 : }
1134 : }
1135 : else
1136 : {
1137 0 : zeros[num_zeros++] = i;
1138 : }
1139 : }
1140 :
1141 : /* Requantize zeroed-lines of the spectrum */
1142 0 : fac_p = ( 1.0f - deadzone ) * 0.33f;
1143 0 : --target_bits; /* reserve 1 bit for the check below */
1144 0 : for ( j = 0; j < num_zeros; ++j )
1145 : {
1146 0 : if ( bits >= target_bits )
1147 : {
1148 : /* 1 or 0 bits left */
1149 0 : break;
1150 : }
1151 :
1152 0 : i = zeros[j];
1153 :
1154 0 : thres = fac_p;
1155 0 : if ( fabs( x_orig[i] ) > thres * gain )
1156 : {
1157 0 : prm[bits++] = 1;
1158 0 : prm[bits++] = 1 - signs[i];
1159 0 : x_Q[i] = ( 2 - 4 * signs[i] ) * thres;
1160 : }
1161 : else
1162 : {
1163 0 : prm[bits++] = 0;
1164 : }
1165 : }
1166 :
1167 0 : return bits;
1168 : }
1169 :
1170 3139119 : for ( i = 0; i < L_frame; ++i )
1171 : {
1172 3134527 : if ( bits >= target_bits )
1173 : {
1174 : /* no bits left */
1175 32003 : break;
1176 : }
1177 3102524 : if ( x_Q[i] != 0 )
1178 : {
1179 39948 : float sign = ( 1 - 2 * signs[i] ) * x_fac[i];
1180 :
1181 39948 : x_Q_m = x_Q[i] - sign * fac_m;
1182 39948 : x_Q_p = x_Q[i] + sign * fac_p;
1183 39948 : if ( fabs( x_orig[i] - gain * x_Q_m ) < fabs( x_orig[i] - gain * x_Q_p ) ) /* Decrease magnitude */
1184 : {
1185 26114 : x_Q[i] = x_Q_m;
1186 26114 : prm[bits++] = 0;
1187 : }
1188 : else /* Increase magnitude */
1189 : {
1190 13834 : x_Q[i] = x_Q_p;
1191 13834 : prm[bits++] = 1;
1192 : }
1193 : }
1194 : else
1195 : {
1196 3062576 : zeros[num_zeros++] = i;
1197 : }
1198 : }
1199 :
1200 : /* Requantize zeroed-lines of the spectrum */
1201 36595 : fac_p = ( 1.0f - deadzone ) * 0.33f;
1202 36595 : --target_bits; /* reserve 1 bit for the check below */
1203 170141 : for ( j = 0; j < num_zeros; ++j )
1204 : {
1205 139454 : if ( bits >= target_bits )
1206 : {
1207 : /* 1 or 0 bits left */
1208 5908 : break;
1209 : }
1210 :
1211 133546 : i = zeros[j];
1212 :
1213 133546 : thres = fac_p * x_fac[i];
1214 133546 : if ( fabs( x_orig[i] ) > thres * gain )
1215 : {
1216 19804 : prm[bits++] = 1;
1217 19804 : prm[bits++] = 1 - signs[i];
1218 19804 : x_Q[i] = ( 2 - 4 * signs[i] ) * thres;
1219 : }
1220 : else
1221 : {
1222 113742 : prm[bits++] = 0;
1223 : }
1224 : }
1225 :
1226 36595 : return bits;
1227 : }
1228 :
1229 :
1230 : /*---------------------------------------------------------------------*
1231 : * tcx_res_Q_gain()
1232 : *
1233 : *
1234 : *---------------------------------------------------------------------*/
1235 :
1236 : #define kMaxEstimatorOvershoot 5
1237 : #define kMaxEstimatorUndershoot 0
1238 :
1239 864761 : int16_t tcx_res_Q_gain(
1240 : float sqGain,
1241 : float *gain_tcx,
1242 : int16_t *prm,
1243 : int16_t sqTargetBits )
1244 : {
1245 : int16_t bits;
1246 : float gain_reQ;
1247 :
1248 : /*Refine the gain quantization : Normal greedy gain coding */
1249 864761 : gain_reQ = *gain_tcx;
1250 3459044 : for ( bits = 0; bits < TCX_RES_Q_BITS_GAIN; bits++ )
1251 : {
1252 2594283 : if ( sqGain < gain_reQ )
1253 : {
1254 1331339 : prm[bits] = 0;
1255 1331339 : gain_reQ *= gain_corr_inv_fac[bits];
1256 : }
1257 : else
1258 : {
1259 1262944 : prm[bits] = 1;
1260 1262944 : gain_reQ *= gain_corr_fac[bits];
1261 : }
1262 2594283 : if ( bits < sqTargetBits )
1263 : {
1264 2066716 : *gain_tcx = gain_reQ;
1265 : }
1266 : }
1267 :
1268 864761 : return ( bits );
1269 : }
1270 :
1271 : /*---------------------------------------------------------------------*
1272 : * refine_0()
1273 : *
1274 : *
1275 : *---------------------------------------------------------------------*/
1276 :
1277 2892625 : static void refine_0(
1278 : const float x_orig,
1279 : float *x_Q,
1280 : const float sqGain,
1281 : int16_t *prm,
1282 : int16_t *bits,
1283 : const float sq_round,
1284 : const float lf_deemph_factor )
1285 : {
1286 : float /*b,*/ thres;
1287 : /* was */
1288 : /*b = x_orig/sqGain;*/
1289 2892625 : thres = ( 1.0f - sq_round ) * 0.33f * lf_deemph_factor;
1290 2892625 : if ( x_orig > thres * sqGain )
1291 : {
1292 : /* was (b > thres) */
1293 121593 : prm[( *bits )++] = 1;
1294 121593 : prm[( *bits )++] = 1;
1295 121593 : *x_Q = 2.f * thres;
1296 : }
1297 2771032 : else if ( x_orig < -thres * sqGain )
1298 : {
1299 : /* was (b < -thres) */
1300 130855 : prm[( *bits )++] = 1;
1301 130855 : prm[( *bits )++] = 0;
1302 130855 : *x_Q = -2.f * thres;
1303 : }
1304 : else
1305 : {
1306 2640177 : prm[( *bits )++] = 0;
1307 : }
1308 :
1309 2892625 : return;
1310 : }
1311 :
1312 : /*---------------------------------------------------------------------*
1313 : * tcx_res_Q_spec()
1314 : *
1315 : *
1316 : *---------------------------------------------------------------------*/
1317 :
1318 864761 : int16_t tcx_res_Q_spec(
1319 : const float *x_orig,
1320 : float *x_Q,
1321 : const int16_t L_frame,
1322 : const float sqGain,
1323 : int16_t *prm,
1324 : int16_t sqTargetBits,
1325 : int16_t bits,
1326 : const float sq_round,
1327 : const float lf_deemph_factors[] )
1328 : {
1329 : int16_t i;
1330 : float fac_m, fac_p;
1331 :
1332 : /* Limit the number of residual bits */
1333 864761 : sqTargetBits = min( sqTargetBits, NPRM_RESQ );
1334 :
1335 : /* Requantize the spectrum line-by-line */
1336 864761 : fac_p = 0.5f - sq_round * 0.5f;
1337 864761 : fac_m = sq_round * 0.5f;
1338 864761 : if ( !lf_deemph_factors )
1339 : {
1340 44554152 : for ( i = 0; i < L_frame; i++ )
1341 : {
1342 44515926 : if ( bits >= sqTargetBits - kMaxEstimatorUndershoot )
1343 : {
1344 11789901 : fac_m = fac_p = 0;
1345 11789901 : if ( bits >= min( NPRM_RESQ, sqTargetBits + kMaxEstimatorOvershoot ) )
1346 : {
1347 595979 : break;
1348 : }
1349 : }
1350 :
1351 43919947 : if ( x_Q[i] != 0.0f )
1352 : {
1353 6200975 : if ( x_orig[i] < (sqGain) *x_Q[i] )
1354 : {
1355 3097141 : prm[bits++] = 0;
1356 3097141 : x_Q[i] -= ( x_Q[i] > 0 ) ? fac_m : fac_p;
1357 : }
1358 : else
1359 : {
1360 3103834 : prm[bits++] = 1;
1361 3103834 : x_Q[i] += ( x_Q[i] > 0 ) ? fac_p : fac_m;
1362 : }
1363 : }
1364 : }
1365 634205 : sqTargetBits -= 2; /* Quantize zeroed lines of the spectrum */
1366 2621661 : for ( i = 0; ( i < L_frame ) && ( bits < sqTargetBits ); i++ )
1367 : {
1368 : /* bits < sqTargetBits */
1369 1987456 : if ( x_Q[i] == 0.0f )
1370 : {
1371 1892080 : refine_0( x_orig[i], &x_Q[i], sqGain, prm, &bits, sq_round, 1.0f ); /* inlined */
1372 : }
1373 : }
1374 : /* Make sure that all possible bits are initialized */
1375 53953709 : for ( i = bits; i < NPRM_RESQ; i++ )
1376 : {
1377 53319504 : prm[i] = 0;
1378 : }
1379 634205 : return bits;
1380 : }
1381 17640863 : for ( i = 0; i < L_frame; i++ )
1382 : {
1383 17624141 : if ( bits >= sqTargetBits - kMaxEstimatorUndershoot )
1384 : {
1385 3317380 : fac_m = fac_p = 0;
1386 3317380 : if ( bits >= min( NPRM_RESQ, sqTargetBits + kMaxEstimatorOvershoot ) )
1387 : {
1388 213834 : break;
1389 : }
1390 : }
1391 :
1392 17410307 : if ( x_Q[i] != 0 && lf_deemph_factors[i] > 0.5f )
1393 : {
1394 1699167 : if ( x_orig[i] < (sqGain) *x_Q[i] )
1395 : {
1396 848230 : prm[bits++] = 0;
1397 848230 : x_Q[i] -= ( x_Q[i] > 0 ) ? fac_m * lf_deemph_factors[i] : fac_p * lf_deemph_factors[i];
1398 : }
1399 : else
1400 : {
1401 850937 : prm[bits++] = 1;
1402 850937 : x_Q[i] += ( x_Q[i] > 0 ) ? fac_p * lf_deemph_factors[i] : fac_m * lf_deemph_factors[i];
1403 : }
1404 : }
1405 : }
1406 :
1407 : /*Quantize zeroed-line of the spectrum*/
1408 1345096 : for ( i = 0; ( i < L_frame ) && ( bits < ( sqTargetBits - 2 ) ); i++ )
1409 : {
1410 : /* For (bits >= (sqTargetBits-2)) */
1411 1114540 : if ( x_Q[i] == 0 && lf_deemph_factors[i] > 0.5f )
1412 : {
1413 1000545 : refine_0( x_orig[i], &x_Q[i], sqGain, prm, &bits, sq_round, lf_deemph_factors[i] );
1414 : }
1415 : }
1416 :
1417 : /*Be sure that every possible bits are initialized*/
1418 19747654 : for ( i = bits; i < NPRM_RESQ; i++ )
1419 : {
1420 19517098 : prm[i] = 0;
1421 : }
1422 :
1423 230556 : return bits;
1424 : }
1425 :
1426 :
1427 : /*---------------------------------------------------------------------*
1428 : * ProcessIGF()
1429 : *
1430 : *
1431 : *---------------------------------------------------------------------*/
1432 :
1433 1310443 : void ProcessIGF(
1434 : Encoder_State *st, /* i : Encoder state */
1435 : float *pMDCTSpectrum, /* i : MDCT spectrum */
1436 : const float *pITFMDCTSpectrum, /* i : MDCT spectrum fir ITF */
1437 : float *pPowerSpectrum, /* i : MDCT^2 + MDST^2 spectrum, or estimate */
1438 : const int16_t isTCX20, /* i : flag indicating if the input is TCX20 or TCX10/2xTCX5 */
1439 : const int16_t frameno, /* i : flag indicating index of current subframe */
1440 : const int16_t sp_aud_decision0, /* i : first stage switching decision */
1441 : const int16_t vad_hover_flag /* i : VAD hangover flag */
1442 : )
1443 : {
1444 : int16_t igfGridIdx, isIndepFlag, bsBits, pBsStart, curr_order;
1445 : float predictionGain;
1446 : float A[ITF_MAX_FILTER_ORDER + 1];
1447 :
1448 1310443 : IGF_ENC_INSTANCE_HANDLE hIGFEnc = st->hIGFEnc;
1449 1310443 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1450 :
1451 1310443 : isIndepFlag = 1;
1452 1310443 : if ( st->last_core == ACELP_CORE && isTCX20 )
1453 : {
1454 17676 : igfGridIdx = IGF_GRID_LB_TRAN;
1455 : }
1456 1292767 : else if ( isTCX20 )
1457 : {
1458 1212255 : igfGridIdx = IGF_GRID_LB_NORM;
1459 : }
1460 : else
1461 : {
1462 : /* It is short block */
1463 80512 : igfGridIdx = IGF_GRID_LB_SHORT;
1464 80512 : if ( frameno == 1 )
1465 : {
1466 40233 : isIndepFlag = 0;
1467 : }
1468 : }
1469 :
1470 1310443 : IGFSaveSpectrumForITF( hIGFEnc, igfGridIdx, pITFMDCTSpectrum );
1471 :
1472 1310443 : IGFEncApplyMono( st, igfGridIdx, pMDCTSpectrum, pPowerSpectrum, isTCX20, st->hTcxEnc->fUseTns[frameno], sp_aud_decision0, vad_hover_flag );
1473 :
1474 1310443 : curr_order = 0;
1475 1310443 : predictionGain = 0;
1476 :
1477 1310443 : ITF_Detect( hIGFEnc->spec_be_igf, hIGFEnc->infoStartLine, hIGFEnc->infoStopLine, 8 /*maxOrder*/, A, &predictionGain, &curr_order );
1478 :
1479 1310443 : hIGFEnc->flatteningTrigger = hIGFEnc->tns_predictionGain < 1.15 && predictionGain < 1.15;
1480 :
1481 1310443 : hIGFEnc->infoTotalBitsPerFrameWritten = 0;
1482 :
1483 1310443 : if ( isTCX20 )
1484 : {
1485 1229931 : IGFEncWriteBitstream( hIGFEnc, NULL, &hIGFEnc->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1486 : }
1487 : else
1488 : {
1489 80512 : pBsStart = hBstr->nb_ind_tot;
1490 :
1491 80512 : IGFEncWriteBitstream( hIGFEnc, hBstr, &hIGFEnc->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1492 :
1493 80512 : bsBits = hBstr->nb_ind_tot - pBsStart;
1494 80512 : IGFEncConcatenateBitstream( hIGFEnc, bsBits, hBstr );
1495 : }
1496 :
1497 1310443 : return;
1498 : }
1499 :
1500 :
1501 : /*---------------------------------------------------------------------*
1502 : * ProcessStereoIGF()
1503 : *
1504 : *
1505 : *---------------------------------------------------------------------*/
1506 :
1507 141415 : void ProcessStereoIGF(
1508 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct,
1509 : Encoder_State *sts[CPE_CHANNELS], /* i : Encoder state */
1510 : int16_t ms_mask[2][MAX_SFB], /* i : bandwise MS mask */
1511 : float *pITFMDCTSpectrum[CPE_CHANNELS][NB_DIV], /* i : MDCT spectrum fir ITF */
1512 : float *pPowerSpectrum[CPE_CHANNELS], /* i/o: MDCT^2 + MDST^2 spectrum, or estimate */
1513 : float *pPowerSpectrumMsInv[CPE_CHANNELS][NB_DIV], /* i : inverse power spectrum */
1514 : float *inv_spectrum[CPE_CHANNELS][NB_DIV], /* i : inverse spectrum */
1515 : const int16_t frameno, /* i : flag indicating index of current subfr. */
1516 : const int16_t sp_aud_decision0, /* i : sp_aud_decision0 */
1517 : const int32_t element_brate, /* i : element bitrate */
1518 : const int16_t mct_on /* i : flag mct block (1) or stereo (0) */
1519 : )
1520 : {
1521 : int16_t ch, igfGridIdx, isIndepFlag, bsBits, pBsStart, curr_order;
1522 : float predictionGain;
1523 : float A[ITF_MAX_FILTER_ORDER + 1];
1524 : IGF_ENC_INSTANCE_HANDLE hIGFEnc[CPE_CHANNELS];
1525 : BSTR_ENC_HANDLE hBstr;
1526 :
1527 141415 : hIGFEnc[0] = sts[0]->hIGFEnc;
1528 141415 : hIGFEnc[1] = sts[1]->hIGFEnc;
1529 :
1530 141415 : isIndepFlag = 1;
1531 :
1532 141415 : if ( sts[0]->last_core == ACELP_CORE && sts[0]->core == TCX_20_CORE )
1533 : {
1534 0 : igfGridIdx = IGF_GRID_LB_TRAN;
1535 : }
1536 141415 : else if ( sts[0]->core == TCX_20_CORE )
1537 : {
1538 135862 : igfGridIdx = IGF_GRID_LB_NORM;
1539 : }
1540 : else
1541 : {
1542 : /* It is short block */
1543 5553 : igfGridIdx = IGF_GRID_LB_SHORT;
1544 5553 : if ( frameno == 1 )
1545 : {
1546 2788 : isIndepFlag = 0;
1547 : }
1548 : }
1549 :
1550 141415 : IGFSaveSpectrumForITF( hIGFEnc[0], igfGridIdx, pITFMDCTSpectrum[0][frameno] );
1551 :
1552 141415 : IGFSaveSpectrumForITF( hIGFEnc[1], igfGridIdx, pITFMDCTSpectrum[1][frameno] );
1553 :
1554 141415 : IGFEncApplyStereo( hStereoMdct, ms_mask, hIGFEnc, igfGridIdx, sts, pPowerSpectrum, pPowerSpectrumMsInv, inv_spectrum, frameno, sp_aud_decision0, element_brate, mct_on );
1555 :
1556 424245 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1557 : {
1558 282830 : curr_order = 0;
1559 :
1560 282830 : ITF_Detect( hIGFEnc[ch]->spec_be_igf, hIGFEnc[ch]->infoStartLine, hIGFEnc[ch]->infoStopLine, 8 /*maxOrder*/, A, &predictionGain, &curr_order );
1561 :
1562 282830 : hIGFEnc[ch]->flatteningTrigger = hIGFEnc[ch]->tns_predictionGain < 1.15 && predictionGain < 1.15;
1563 :
1564 282830 : hIGFEnc[ch]->infoTotalBitsPerFrameWritten = 0;
1565 :
1566 282830 : if ( sts[ch]->core == TCX_20_CORE )
1567 : {
1568 271724 : IGFEncWriteBitstream( hIGFEnc[ch], NULL, &hIGFEnc[ch]->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1569 : }
1570 : else
1571 : {
1572 11106 : hBstr = sts[ch]->hBstr;
1573 11106 : pBsStart = hBstr->nb_ind_tot;
1574 11106 : if ( ch > 0 )
1575 : {
1576 5553 : hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
1577 : }
1578 11106 : IGFEncWriteBitstream( hIGFEnc[ch], hBstr, &hIGFEnc[ch]->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1579 :
1580 11106 : bsBits = hBstr->nb_ind_tot - pBsStart;
1581 11106 : IGFEncConcatenateBitstream( hIGFEnc[ch], bsBits, hBstr );
1582 : }
1583 : }
1584 :
1585 141415 : return;
1586 : }
1587 :
1588 : /*---------------------------------------------------------------------*
1589 : * attenuateNbSpectrum()
1590 : *
1591 : *
1592 : *---------------------------------------------------------------------*/
1593 :
1594 114 : void attenuateNbSpectrum(
1595 : const int16_t L_frame,
1596 : float *spectrum )
1597 : {
1598 : int16_t i, length;
1599 : float att;
1600 :
1601 114 : length = L_frame / 20;
1602 114 : att = ( length == 8 ) ? 0.6f : 0.66f;
1603 :
1604 1038 : for ( i = 0; i < length; i++ )
1605 : {
1606 924 : spectrum[L_frame - length + i] *= att;
1607 924 : att *= att;
1608 : }
1609 :
1610 114 : return;
1611 : }
|