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 9252592 : 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 9252592 : if ( resetMemory )
68 : {
69 449746962 : for ( i = 0; i < lowpassLine; i++ )
70 : {
71 449259800 : noiseFlags[i] = 0;
72 : }
73 : }
74 9252592 : for ( i = lowpassLine; i < L_frame; i++ )
75 : {
76 0 : noiseFlags[i] = 1;
77 : }
78 :
79 9252592 : if ( powerSpec && startLine + 6 < L_frame )
80 : {
81 8750332 : lastTone = 0;
82 : /* noise-measure flags for spectrum filling and quantization (0: tonal, 1: noise-like) */
83 8750332 : i = startLine - 1;
84 8750332 : s = powerSpec[i - 7] + powerSpec[i - 6] + powerSpec[i - 5] +
85 8750332 : powerSpec[i - 4] + powerSpec[i - 3] + powerSpec[i - 2] +
86 8750332 : powerSpec[i - 1] + powerSpec[i] + powerSpec[i + 1] +
87 8750332 : powerSpec[i + 2] + powerSpec[i + 3] + powerSpec[i + 4] +
88 8750332 : powerSpec[i + 5] + powerSpec[i + 6] + powerSpec[i + 7];
89 :
90 5147472617 : for ( i++; i < lowpassLine - 7; i++ )
91 : {
92 5138722285 : c = powerSpec[i - 1] + powerSpec[i] + powerSpec[i + 1];
93 5138722285 : s += powerSpec[i + 7] - powerSpec[i - 8];
94 5138722285 : if ( s >= ( 1.75f - 0.5f * noiseFlags[i] ) * c )
95 : {
96 5099872618 : noiseFlags[i] = 1;
97 : }
98 : else
99 : {
100 38849667 : noiseFlags[i] = 0;
101 38849667 : lastTone = i;
102 : }
103 : }
104 : /* lower L_frame*startRatio lines are tonal (0), upper 7 lines are processed separately */
105 61252324 : for ( ; i < lowpassLine - 1; i++ )
106 : {
107 52501992 : c = powerSpec[i - 1] + powerSpec[i] + powerSpec[i + 1];
108 : /* running sum can't be updated any more, just use the latest one */
109 52501992 : if ( s >= ( 1.75f - 0.5f * noiseFlags[i] ) * c )
110 : {
111 52052231 : noiseFlags[i] = 1;
112 : }
113 : else
114 : {
115 449761 : noiseFlags[i] = 0;
116 : /* lastTone = i; */
117 : }
118 : }
119 8750332 : noiseFlags[i] = 1; /* uppermost line is defined as noise-like (1) */
120 :
121 8750332 : if ( lastTone > 0 ) /* spread uppermost tonal line one line upward */
122 : {
123 1884503 : noiseFlags[lastTone + 1] = 0;
124 : }
125 : }
126 :
127 9252592 : return;
128 : }
129 :
130 :
131 : /*---------------------------------------------------------------------*
132 : * detectLowpassFac()
133 : *
134 : *
135 : *---------------------------------------------------------------------*/
136 :
137 1274615 : 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 1274615 : threshold = 0.1f * 2 * NORM_MDCT_FACTOR;
148 1274615 : if ( rectWin )
149 : {
150 : /* compensate for bad side-lobe attenuation with asymmetric windows */
151 60524 : threshold *= 2.f;
152 : }
153 :
154 43250613 : for ( i = lowpassLine - 1; i >= lowpassLine / 2; i-- )
155 : {
156 43030327 : if ( powerSpec[i] > threshold )
157 : {
158 1054329 : break;
159 : }
160 : }
161 :
162 1274615 : *pLpFac = ( 0.3f * ( *pLpFac ) ) + ( 0.7f * ( (float) ( i + 1 ) / (float) L_frame ) );
163 :
164 1274615 : 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 2676327 : 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 2676327 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
185 : int16_t i, iStart, iEnd, lowpassLine;
186 :
187 2676327 : lowpassLine = L_frameTCX;
188 2676327 : iStart = 0;
189 2676327 : iEnd = L_frameTCX;
190 :
191 2676327 : TCX_MDST( signal, powerSpec, left_overlap, L_frameTCX - ( left_overlap + right_overlap ) / 2, right_overlap, st->element_mode );
192 :
193 2676327 : if ( st->narrowBand )
194 : {
195 1221 : attenuateNbSpectrum( L_frameTCX, powerSpec );
196 : }
197 :
198 : /* power spectrum: MDCT^2 + MDST^2 */
199 2341913967 : for ( i = iStart; i < iEnd; i++ )
200 : {
201 2339237640 : powerSpec[i] *= powerSpec[i];
202 2339237640 : powerSpec[i] += mdctSpectrum[i] * mdctSpectrum[i];
203 : }
204 :
205 2676327 : 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 2676327 : if ( st->total_brate <= ACELP_24k40 )
208 : {
209 1274615 : lowpassLine = (int16_t) ( 2.0f * st->hTcxCfg->bandwidth * L_frame );
210 1274615 : detectLowpassFac( powerSpec, L_frame, ( st->last_core == ACELP_CORE ), &hTcxEnc->measuredBwRatio, lowpassLine );
211 : }
212 : else
213 : {
214 1401712 : hTcxEnc->measuredBwRatio = 1.0f;
215 : }
216 :
217 2676327 : return;
218 : }
219 :
220 :
221 : /*---------------------------------------------------------------------*
222 : * mdct_preShaping()
223 : *
224 : *
225 : *---------------------------------------------------------------------*/
226 :
227 6434045 : 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 6434045 : j = 0; /* not counted, is included in ptr init */
237 : /* FDNS_NPTS = 64 !!! */
238 6434045 : k = lg / FDNS_NPTS;
239 6434045 : m = lg % FDNS_NPTS;
240 6434045 : if ( m )
241 : {
242 291686 : if ( m <= ( FDNS_NPTS / 2 ) )
243 : {
244 291686 : n = FDNS_NPTS / m;
245 291686 : k1 = k;
246 291686 : 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 18959590 : for ( i = 0; i < lg; )
255 : {
256 18667904 : if ( j % n )
257 : {
258 13934768 : k = k1;
259 : }
260 : else
261 : {
262 4733136 : k = k2;
263 : }
264 18667904 : g = 1.f / gains[j++];
265 :
266 : /* Limit number of loops, if end is reached */
267 18667904 : k = min( k, lg - i );
268 136061904 : for ( l = 0; l < k; l++ )
269 : {
270 117394000 : x[i++] *= g;
271 : }
272 : }
273 : }
274 : else
275 : {
276 399253335 : for ( i = 0; i < lg; )
277 : {
278 393110976 : g = 1.f / gains[j++];
279 2441368000 : for ( l = 0; l < k; l++ )
280 : {
281 2048257024 : x[i++] *= g;
282 : }
283 : }
284 : }
285 :
286 6434045 : return;
287 : }
288 :
289 :
290 : /*---------------------------------------------------------------------*
291 : * AdaptLowFreqEmph()
292 : *
293 : *
294 : *---------------------------------------------------------------------*/
295 :
296 9827575 : 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 9827575 : if ( !tcx_lpc_shaped_ari )
307 : {
308 : /* 1. find first magnitude maximum in lower quarter of spectrum */
309 9563634 : invGain *= 2.0f;
310 9563634 : i_max = -1;
311 386689130 : for ( i = 0; i < lg / 4; i++ )
312 : {
313 385121126 : if ( ( ( xq[i] <= -2 ) || ( xq[i] >= 2 ) ) &&
314 8927768 : ( ( invGain * x[i] <= -3.625f ) || ( invGain * x[i] >= 3.625f ) ) )
315 : {
316 7995630 : xq[i] += ( xq[i] < 0 ) ? -2 : 2;
317 7995630 : i_max = i;
318 7995630 : break;
319 : }
320 : }
321 : /* 2. compress value range of all xq up to i_max: add two steps */
322 51345050 : for ( i = 0; i < i_max; i++ )
323 : {
324 41781416 : if ( x[i] < 0.0f )
325 : {
326 20872192 : xq[i] = (int16_t) ( invGain * x[i] - 0.375f );
327 : }
328 : else
329 : {
330 20909224 : 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 9563634 : i_max_old = i_max;
335 9563634 : if ( i_max_old > -1 )
336 : {
337 7995630 : invGain *= 2.0f;
338 7995630 : i_max = -1; /* reset first maximum, update inverse gain */
339 33630208 : for ( i = 0; i < lg / 4; i++ )
340 : {
341 33630208 : if ( ( ( xq[i] <= -2 ) || ( xq[i] >= 2 ) ) &&
342 8647980 : ( ( invGain * x[i] <= -3.625f ) || ( invGain * x[i] >= 3.625f ) ) )
343 : {
344 7995630 : xq[i] += ( xq[i] < 0 ) ? -2 : 2;
345 7995630 : i_max = i;
346 7995630 : break;
347 : }
348 : }
349 : }
350 : /* 4. re-compress and quantize all xq up to half-height i_max+1 */
351 35198212 : for ( i = 0; i < i_max; i++ )
352 : {
353 25634578 : if ( x[i] < 0.0f )
354 : {
355 12824732 : xq[i] = (int16_t) ( invGain * x[i] - 0.375f );
356 : }
357 : else
358 : {
359 12809846 : 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 9563634 : if ( i_max_old > -1 )
364 : {
365 7995630 : invGain *= 0.5f; /* reset inverse gain */
366 7995630 : if ( i_max < i_max_old )
367 : {
368 3528703 : i_max = i_max_old;
369 : }
370 : }
371 9563634 : i = i_max + 1;
372 :
373 9563634 : if ( x[i] < 0.0f )
374 : {
375 4548975 : xq[i] = ( invGain * x[i] <= -3.625f ) ? xq[i] - 2 : (int16_t) ( invGain * x[i] - 0.375f );
376 : }
377 : else
378 : {
379 5014659 : xq[i] = ( invGain * x[i] >= 3.625f ) ? xq[i] + 2 : (int16_t) ( invGain * x[i] + 0.375f );
380 : }
381 9563634 : i++;
382 9563634 : if ( x[i] < 0.0f )
383 : {
384 4577789 : xq[i] = ( invGain * x[i] <= -3.625f ) ? xq[i] - 2 : (int16_t) ( invGain * x[i] - 0.375f );
385 : }
386 : else
387 : {
388 4985845 : xq[i] = ( invGain * x[i] >= 3.625f ) ? xq[i] + 2 : (int16_t) ( invGain * x[i] + 0.375f );
389 : }
390 : }
391 : else
392 : {
393 263941 : PsychAdaptLowFreqEmph( x, lpcGains );
394 : }
395 :
396 9827575 : return;
397 : }
398 :
399 :
400 : /*---------------------------------------------------------------------*
401 : * PsychAdaptLowFreqEmph()
402 : *
403 : *
404 : *---------------------------------------------------------------------*/
405 :
406 311941 : void PsychAdaptLowFreqEmph(
407 : float x[],
408 : const float lpcGains[] )
409 : {
410 : int16_t i;
411 : float max_val, fac, tmp;
412 311941 : max_val = tmp = lpcGains[0];
413 :
414 : /* find minimum (tmp) and maximum (max) of LPC gains in low frequencies */
415 2807469 : for ( i = 1; i < 9; i++ )
416 : {
417 2495528 : if ( tmp > lpcGains[i] )
418 : {
419 1575355 : tmp = lpcGains[i];
420 : }
421 2495528 : if ( max_val < lpcGains[i] )
422 : {
423 692451 : max_val = lpcGains[i];
424 : }
425 : }
426 311941 : tmp *= 32.0f;
427 311941 : if ( ( max_val < tmp ) && ( max_val > FLT_MIN ) )
428 : {
429 310793 : 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 10256169 : for ( i = 31; i >= 0; i-- )
433 : {
434 9945376 : x[i] *= fac;
435 9945376 : fac *= tmp;
436 : }
437 : }
438 :
439 311941 : return;
440 : }
441 :
442 : /*---------------------------------------------------------------------*
443 : * SQ_gain()
444 : *
445 : *
446 : *---------------------------------------------------------------------*/
447 :
448 : /*! r: SQ gain */
449 2493947 : 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 505753667 : for ( i = 0; i < lg; i += 4 )
461 : {
462 503259720 : 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 503259720 : en[i >> 2] = (float) log10( ener );
464 : }
465 :
466 : /* SQ scale: 4 bits / 6 dB per quadruple */
467 2493947 : target = 0.15f * (float) ( nbitsSQ - ( lg >> 4 ) );
468 2493947 : fac = 12.8f;
469 2493947 : offset = fac;
470 :
471 : /* find offset (0 to 128 dB with step of 0.125dB) */
472 27433417 : for ( iter = 0; iter < 10; iter++ )
473 : {
474 24939470 : fac *= 0.5f;
475 24939470 : offset -= fac;
476 24939470 : ener = 0.0f;
477 :
478 4129459452 : for ( i = 0; i < lg / 4; i++ )
479 : {
480 4111937187 : tmp = en[i] - offset;
481 :
482 : /* avoid SV with 1 bin of amp < 0.5f */
483 4111937187 : if ( tmp > 0.3f )
484 : {
485 1057557096 : ener += tmp;
486 :
487 : /* if ener is above target -> break and increase offset */
488 1057557096 : if ( ener > target )
489 : {
490 7417205 : offset += fac;
491 7417205 : break;
492 : }
493 : }
494 : }
495 : }
496 :
497 : /* return gain */
498 2493947 : 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 10224721 : 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 10224721 : float minGainInv = 0.5f * (float) log10( (float) lg / (float) NORM_MDCT_FACTOR ); /* lowest possible quantized gain */
519 :
520 10224721 : 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 2007692921 : for ( i = 0; i < lg; i += 4 )
523 : {
524 1997468200 : 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 1997468200 : en[i >> 2] = (float) log10( ener ) + tmp;
526 : }
527 :
528 : /* SQ scale: 4 bits / 6 dB per quadruple */
529 10224721 : target = 0.15f * (float) ( nbitsSQ - ( lg >> 4 ) );
530 10224721 : fac = 18.4f;
531 10224721 : max_iter = 8; /* for 0.36dB steps */
532 10224721 : offset = fac - fac / (float) ( 2 << max_iter );
533 :
534 : /* find offset, resolution similar to SQ gain quantizer resolution (92dB range, 0.719db resolution) */
535 92022489 : for ( iter = 0; iter < max_iter; iter++ )
536 : {
537 81797768 : fac *= 0.5f;
538 81797768 : offset -= fac;
539 81797768 : ener = 0.0f;
540 :
541 12029252637 : for ( i = 0; i < lg / 4; i++ )
542 : {
543 11979994785 : tmp = en[i] - offset;
544 :
545 : /* avoid SV with 1 bin of amp < 0.5f */
546 11979994785 : if ( tmp > 0.3f )
547 : {
548 4163547249 : ener += tmp;
549 :
550 : /* if SNR is above target -> break and increase offset */
551 4163547249 : if ( ener > target )
552 : {
553 32539916 : offset += fac;
554 32539916 : break;
555 : }
556 : }
557 : }
558 : }
559 :
560 : /* return gain */
561 10224721 : return (float) pow( 10.0f, 0.5f * offset - minGainInv );
562 : }
563 :
564 : /*---------------------------------------------------------------------*
565 : * tcx_scalar_quantization()
566 : *
567 : *
568 : *---------------------------------------------------------------------*/
569 :
570 65149435 : 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 65149435 : gainInv = 1.0f / gain;
584 65149435 : rounding = offset;
585 65149435 : rounding2 = -offset;
586 :
587 65149435 : if ( memQuantZeros == NULL )
588 : {
589 9529928 : i = L_frame - 1;
590 : }
591 : else
592 : {
593 16477661703 : for ( i = L_frame - 1; ( memQuantZeros[i] ) && ( (float) fabs( x[i] ) * gainInv < 1.0f ); i-- )
594 : {
595 16422042196 : xq[i] = 0;
596 : }
597 : }
598 :
599 32604155079 : for ( ; i >= 0; i-- )
600 : {
601 32539005644 : if ( x[i] > 0.f )
602 : {
603 14925018770 : 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 17613986874 : xq[i] = ( (int16_t) max( -32767, ( rounding2 + x[i] * gainInv ) ) );
609 : }
610 : }
611 :
612 65149435 : if ( !tcxonly )
613 : {
614 9563634 : AdaptLowFreqEmph( x, xq, gainInv, 0, NULL, L_frame );
615 : }
616 :
617 65149435 : return;
618 : }
619 :
620 :
621 : /*---------------------------------------------------------------------*
622 : * tcx_scalar_quantization_rateloop()
623 : *
624 : *
625 : *---------------------------------------------------------------------*/
626 :
627 12718668 : 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 12718668 : float ub = 0.f, lb = 0.f;
649 : float shift;
650 : int16_t iter;
651 : float sqGain;
652 : float w_lb, w_ub;
653 12718668 : 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 12718668 : const float kMargin = 0.96f;
658 : int16_t lastnz;
659 : int16_t saturated;
660 : float minSqGain;
661 :
662 : /* Init */
663 12718668 : saturated = 0;
664 12718668 : minSqGain = (float) sqrt( (float) NORM_MDCT_FACTOR / (float) L_frame );
665 :
666 12718668 : sqGain = *gain;
667 12718668 : stopFlag = *stop;
668 12718668 : ubfound = 0;
669 12718668 : lbfound = 0;
670 12718668 : shift = 0.25f;
671 12718668 : w_lb = 0.0f;
672 12718668 : w_ub = 0.0f;
673 12718668 : lastnz = *lastnz_out;
674 :
675 12718668 : old_stopFlag = stopFlag;
676 12718668 : old_nEncoded = *nEncoded;
677 12718668 : old_sqBits = sqBits_in_noStop;
678 :
679 12718668 : sqBits = sqBits_in;
680 :
681 12718668 : mod_adjust0 = max( 1.0f, 2.3f - 0.0025f * target );
682 12718668 : mod_adjust1 = 1.0f / mod_adjust0;
683 :
684 12718668 : inv_target = 1.0f / (float) target;
685 :
686 : /* Loop */
687 42693508 : for ( iter = 0; iter < iter_max; iter++ )
688 : {
689 30425230 : if ( tcxRateLoopOpt >= 2 )
690 : {
691 : /* Ajust sqGain */
692 24049474 : if ( stopFlag )
693 : {
694 10825628 : lbfound = 1;
695 10825628 : lb = sqGain;
696 10825628 : w_lb = (float) ( stopFlag - target + kDampen );
697 10825628 : saturated = 0;
698 :
699 10825628 : if ( ubfound )
700 : {
701 4611814 : sqGain = ( lb * w_ub + ub * w_lb ) / ( w_ub + w_lb );
702 : }
703 : else
704 : {
705 6213814 : sqGain *= ( 1.0f + ( (float) ( stopFlag / kMargin ) * inv_target - 1.0f ) * mod_adjust0 );
706 : }
707 : }
708 13223846 : else if ( saturated == 0 )
709 : {
710 12773456 : ubfound = 1;
711 12773456 : ub = sqGain;
712 12773456 : w_ub = (float) ( target - sqBits + kDampen );
713 12773456 : if ( lbfound )
714 : {
715 5771768 : sqGain = ( lb * w_ub + ub * w_lb ) / ( w_ub + w_lb );
716 : }
717 : else
718 : {
719 7001688 : sqGain *= ( 1.0f - ( 1.0f - (float) ( sqBits * kMargin ) * inv_target ) * mod_adjust1 );
720 : }
721 :
722 12773456 : if ( tcxRateLoopOpt == 3 && sqGain < minSqGain )
723 : {
724 464656 : sqGain = minSqGain;
725 464656 : saturated = 1;
726 : }
727 : }
728 : else
729 : {
730 450390 : break; /* we cannot go any lower anyway*/
731 : }
732 : }
733 : else /* tcxRateLoopOpt != 2 */
734 : {
735 : /* Ajust sqGain */
736 6375756 : if ( stopFlag )
737 : {
738 2367629 : lbfound = 1;
739 2367629 : lb = sqGain;
740 2367629 : if ( ubfound )
741 : {
742 1691480 : sqGain = (float) sqrt( lb * ub );
743 : }
744 : else
745 : {
746 676149 : sqGain = sqGain * (float) pow( 10.0f, shift / 10.0f );
747 676149 : shift *= 2.0f;
748 : }
749 : }
750 : else
751 : {
752 4008127 : ubfound = 1;
753 4008127 : ub = sqGain;
754 4008127 : if ( lbfound )
755 : {
756 1295733 : sqGain = (float) sqrt( lb * ub );
757 : }
758 : else
759 : {
760 2712394 : sqGain = sqGain * (float) pow( 10.0f, -shift / 10.0f );
761 2712394 : shift *= 2.0f;
762 : }
763 : }
764 : }
765 :
766 : /* Quantize spectrum */
767 29974840 : tcx_scalar_quantization( x, xq, L_frame, sqGain, offset, memQuantZeros, tcxonly );
768 :
769 : /* Estimate bitrate */
770 29974840 : if ( tcxRateLoopOpt >= 1 )
771 : {
772 29974840 : stopFlag = 0;
773 : }
774 : else
775 : {
776 0 : stopFlag = 1;
777 : }
778 :
779 29974840 : if ( element_mode > EVS_MONO )
780 : {
781 29862584 : sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( xq, L_frame, &lastnz, nEncoded, target, &stopFlag, 0, hm_cfg );
782 : }
783 : else
784 : {
785 112256 : sqBits = ACcontextMapping_encode2_estimate_no_mem_s17_LC( xq, L_frame, &lastnz, nEncoded, target, &stopFlag, hm_cfg );
786 : }
787 :
788 29974840 : if ( tcxRateLoopOpt >= 1 )
789 : {
790 29974840 : if ( ( *nEncoded >= old_nEncoded && ( stopFlag >= old_stopFlag ) ) || ( *nEncoded > old_nEncoded && ( stopFlag == 0 && old_stopFlag > 0 ) ) || ( stopFlag == 0 && old_stopFlag == 0 ) )
791 : {
792 16971407 : *gain = sqGain;
793 16971407 : old_nEncoded = *nEncoded;
794 16971407 : old_stopFlag = stopFlag;
795 16971407 : old_sqBits = sqBits;
796 16971407 : *lastnz_out = lastnz;
797 : }
798 : }
799 : } /* for ( iter=0; iter<iter_max; iter++ ) */
800 :
801 12718668 : if ( tcxRateLoopOpt >= 1 )
802 : {
803 : /* Quantize spectrum */
804 12718668 : tcx_scalar_quantization( x, xq, L_frame, *gain, offset, memQuantZeros, tcxonly );
805 :
806 : /* Output */
807 12718668 : *nEncoded = old_nEncoded;
808 12718668 : sqBits = old_sqBits;
809 12718668 : *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 12718668 : return sqBits;
820 : }
821 :
822 : /*---------------------------------------------------------------------*
823 : * tcx_QuantizeGain()
824 : *
825 : *
826 : *---------------------------------------------------------------------*/
827 :
828 12982609 : 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 12982609 : ener = (float) sqrt( (float) n / (float) NORM_MDCT_FACTOR );
837 :
838 12982609 : gain = *pGain * ener;
839 :
840 12982609 : assert( gain > 0 );
841 :
842 : /* quantize gain with step of 0.714 dB */
843 12982609 : quantizedGain = (int16_t) floor( 0.5f + 28.0f * (float) log10( gain ) );
844 :
845 12982609 : if ( quantizedGain < 0 )
846 : {
847 244782 : quantizedGain = 0;
848 : }
849 12982609 : if ( quantizedGain > 127 )
850 : {
851 124 : quantizedGain = 127;
852 : }
853 :
854 12982609 : *pQuantizedGain = quantizedGain;
855 12982609 : *pGain = (float) pow( 10.0f, ( (float) quantizedGain ) / 28.0f ) / ener;
856 :
857 12982609 : return;
858 : }
859 :
860 : /*---------------------------------------------------------------------*
861 : * tcx_noise_factor()
862 : *
863 : *
864 : *---------------------------------------------------------------------*/
865 :
866 12329295 : 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 12329295 : 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 12329295 : sqErrorNrg = 0.0f;
886 12329295 : n = 0.0f;
887 : /* max() */
888 12329295 : tilt_factor = 1.0f / (float) pow( max( 0.375f, tiltCompFactor ), 1.0f / (float) L_frame ); /* 1/(a^b) = a^-b */
889 12329295 : inv_gain2 = 1.0f / ( (float) ( nTransWidth * nTransWidth ) * gain_tcx );
890 :
891 : /* find last nonzero line below iFirstLine, use it as start offset */
892 12329295 : i = iFirstLine;
893 :
894 12329295 : if ( element_mode == IVAS_CPE_MDCT ) /* ... but only in mono or parametric stereo since it may cause binaural unmasking in discrete stereo */
895 : {
896 9786604 : segmentOffset = i;
897 : }
898 : else
899 : {
900 16295295 : for ( ; i > ( iFirstLine >> 1 ); i-- )
901 : {
902 16003454 : if ( sqQ[i] != 0.0f )
903 : {
904 2250850 : break;
905 : }
906 : }
907 2542691 : inv_gain2 *= (float) pow( tilt_factor, (float) i );
908 2542691 : segmentOffset = ++i;
909 : }
910 :
911 12329295 : if ( nTransWidth <= 3 )
912 : {
913 461922 : att = tmp = FLT_MIN;
914 46159289 : for ( k = i & 0xFFFE; k < lowpassLine; k++ )
915 : {
916 45697367 : att += x_orig[k] * x_orig[k]; /* even-index bins, left sub-win */
917 45697367 : k++;
918 45697367 : tmp += x_orig[k] * x_orig[k]; /* odd-index bins, right sub-win */
919 : }
920 461922 : att = (float) sqrt( ( min( att, tmp ) * 2.0f ) / ( att + tmp ) );
921 : }
922 : else
923 : {
924 11867373 : att = 1.0f;
925 : }
926 12329295 : win = 0;
927 4623234977 : for ( ; i < lowpassLine; i++ )
928 : {
929 4610905682 : inv_gain2 *= tilt_factor;
930 4610905682 : if ( sqQ[i] != 0 ) /* current line is not zero, so reset pointers */
931 : {
932 1065451920 : if ( win > 0 ) /* add segment sum to sum of segment magnitudes */
933 : {
934 476878352 : k = i - segmentOffset;
935 476878352 : if ( nTransWidth <= 3 )
936 : {
937 10713145 : n += ( k > 2 * nTransWidth - 4 ) ? (float) ( k - nTransWidth + 1 )
938 10713145 : : (float) ( k * k ) * 0.28125f / nTransWidth; /* table lookup instead of */
939 : }
940 : else
941 : {
942 466165207 : n += ( k > 12 ) ? (float) k - 7.0f : (float) ( k * k ) * 0.03515625f;
943 : }
944 1317648601 : for ( k = segmentOffset; k < i - win; k++ )
945 : {
946 840770249 : sqErrorNrg += sqQ[k] * (float) nTransWidth;
947 840770249 : sqQ[k] = 0;
948 : }
949 1826524018 : for ( ; win > 0; win-- )
950 : {
951 1349645666 : sqErrorNrg += sqQ[k] * (float) win;
952 1349645666 : sqQ[k++] = 0;
953 : }
954 : }
955 1065451920 : segmentOffset = i + 1; /* new segment might start at next line */
956 : }
957 : else /* current line is zero, so update pointers & segment sum */
958 : {
959 3545453762 : if ( win < nTransWidth )
960 : {
961 1419782830 : win++;
962 : }
963 : /* update segment sum: magnitudes scaled by smoothing function */
964 3545453762 : sqQ[i] = (float) fabs( x_orig[i] ) * (float) win * inv_gain2;
965 : }
966 : }
967 12329295 : if ( win > 0 ) /* add last segment sum to sum of segment magnitudes */
968 : {
969 11372671 : k = i - segmentOffset;
970 11372671 : if ( nTransWidth <= 3 )
971 : {
972 421157 : n += ( k > 2 * nTransWidth - 4 ) ? (float) ( k - nTransWidth + 1 )
973 421157 : : (float) ( k * k ) * 0.28125f / nTransWidth; /* table lookup instead of */
974 : }
975 : else
976 : {
977 10951514 : n += ( k > 12 ) ? (float) k - 7.0f : (float) ( k * k ) * 0.03515625f;
978 : }
979 1296273354 : for ( k = segmentOffset; k < i - win; k++ )
980 : {
981 1284900683 : sqErrorNrg += sqQ[k] * (float) nTransWidth;
982 1284900683 : sqQ[k] = 0;
983 : }
984 81509835 : for ( ; win > 0; win-- )
985 : {
986 70137164 : sqErrorNrg += sqQ[k] * (float) win;
987 70137164 : sqQ[k++] = 0;
988 : }
989 : }
990 :
991 : /* noise level factor: average of segment magnitudes of noise bins */
992 12329295 : if ( n > 0.0f )
993 : {
994 12329295 : *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 12329295 : *quantized_fac_ns = (int16_t) ( 0.5f + *fac_ns * 1.34375f * ( 1 << NBITS_NOISE_FILL_LEVEL ) );
1003 12329295 : if ( *quantized_fac_ns > ( 1 << NBITS_NOISE_FILL_LEVEL ) - 1 )
1004 : {
1005 683913 : *quantized_fac_ns = ( 1 << NBITS_NOISE_FILL_LEVEL ) - 1;
1006 : }
1007 12329295 : *fac_ns = (float) ( *quantized_fac_ns ) * 0.75f / ( 1 << NBITS_NOISE_FILL_LEVEL );
1008 :
1009 12329295 : return;
1010 : }
1011 :
1012 :
1013 : /*---------------------------------------------------------------------*
1014 : * tcx_encoder_memory_update()
1015 : *
1016 : *
1017 : *---------------------------------------------------------------------*/
1018 :
1019 2744597 : 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 2744597 : LPD_state *LPDmem = st->hLPDmem;
1031 :
1032 2744597 : L_frame_glob = st->L_frame;
1033 :
1034 : /* Output synth */
1035 2744597 : mvr2r( xn_buf, st->synth, L_frame_glob );
1036 :
1037 : /* Update synth */
1038 2744597 : synth = buf + 1 + M;
1039 2744597 : mvr2r( LPDmem->syn, buf, 1 + M );
1040 :
1041 2744597 : mvr2r( xn_buf, synth, L_frame_glob );
1042 2744597 : mvr2r( synth + L_frame_glob - M - 1, LPDmem->syn, 1 + M );
1043 :
1044 2744597 : if ( !st->tcxonly )
1045 : {
1046 : /* Update weighted synthesis */
1047 1857880 : residu( Ai + ( st->nb_subfr - 1 ) * ( M + 1 ), M, synth + L_frame_glob - 1, &tmp, 1 );
1048 1857880 : LPDmem->mem_w0 = st->wspeech_enc[L_frame_glob - 1] - tmp;
1049 : }
1050 :
1051 : /* Emphasis of synth -> synth_pe */
1052 2744597 : tmp = synth[-M - 1];
1053 2744597 : preemph( synth - M, st->hTcxCfg->preemph_fac, M + L_frame_glob, &tmp );
1054 2744597 : mvr2r( synth + L_frame_glob - M, LPDmem->mem_syn, M );
1055 2744597 : mvr2r( synth + L_frame_glob - M, LPDmem->mem_syn2, M );
1056 2744597 : mvr2r( synth + L_frame_glob - L_SYN_MEM, LPDmem->mem_syn_r, L_SYN_MEM );
1057 :
1058 2744597 : if ( !st->tcxonly || ( L_frame_glob == L_FRAME16k ) )
1059 : {
1060 : /* Update excitation */
1061 1883622 : if ( L_frame_glob < L_EXC_MEM )
1062 : {
1063 647774 : mvr2r( LPDmem->old_exc + ( L_frame_glob ), LPDmem->old_exc, L_EXC_MEM - L_frame_glob );
1064 647774 : residu( A, M, synth, LPDmem->old_exc + L_EXC_MEM - ( L_frame_glob ), L_frame_glob );
1065 : }
1066 : else
1067 : {
1068 1235848 : residu( A, M, synth + (L_frame_glob) -L_EXC_MEM, LPDmem->old_exc, L_EXC_MEM );
1069 : }
1070 : }
1071 :
1072 2744597 : 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 263941 : 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 263941 : target_bits = min( target_bits, NPRM_RESQ );
1102 :
1103 :
1104 : /* Requantize the spectrum line-by-line */
1105 263941 : fac_m = deadzone * 0.5f;
1106 263941 : fac_p = 0.5f - fac_m;
1107 263941 : num_zeros = 0;
1108 :
1109 263941 : 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 26637160 : for ( i = 0; i < L_frame; ++i )
1171 : {
1172 26595204 : if ( bits >= target_bits )
1173 : {
1174 : /* no bits left */
1175 221985 : break;
1176 : }
1177 26373219 : if ( x_Q[i] != 0 )
1178 : {
1179 272796 : float sign = ( 1 - 2 * signs[i] ) * x_fac[i];
1180 :
1181 272796 : x_Q_m = x_Q[i] - sign * fac_m;
1182 272796 : x_Q_p = x_Q[i] + sign * fac_p;
1183 272796 : if ( fabs( x_orig[i] - gain * x_Q_m ) < fabs( x_orig[i] - gain * x_Q_p ) ) /* Decrease magnitude */
1184 : {
1185 171949 : x_Q[i] = x_Q_m;
1186 171949 : prm[bits++] = 0;
1187 : }
1188 : else /* Increase magnitude */
1189 : {
1190 100847 : x_Q[i] = x_Q_p;
1191 100847 : prm[bits++] = 1;
1192 : }
1193 : }
1194 : else
1195 : {
1196 26100423 : zeros[num_zeros++] = i;
1197 : }
1198 : }
1199 :
1200 : /* Requantize zeroed-lines of the spectrum */
1201 263941 : fac_p = ( 1.0f - deadzone ) * 0.33f;
1202 263941 : --target_bits; /* reserve 1 bit for the check below */
1203 1964903 : for ( j = 0; j < num_zeros; ++j )
1204 : {
1205 1750862 : if ( bits >= target_bits )
1206 : {
1207 : /* 1 or 0 bits left */
1208 49900 : break;
1209 : }
1210 :
1211 1700962 : i = zeros[j];
1212 :
1213 1700962 : thres = fac_p * x_fac[i];
1214 1700962 : if ( fabs( x_orig[i] ) > thres * gain )
1215 : {
1216 366029 : prm[bits++] = 1;
1217 366029 : prm[bits++] = 1 - signs[i];
1218 366029 : x_Q[i] = ( 2 - 4 * signs[i] ) * thres;
1219 : }
1220 : else
1221 : {
1222 1334933 : prm[bits++] = 0;
1223 : }
1224 : }
1225 :
1226 263941 : 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 5698816 : 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 5698816 : gain_reQ = *gain_tcx;
1250 22795264 : for ( bits = 0; bits < TCX_RES_Q_BITS_GAIN; bits++ )
1251 : {
1252 17096448 : if ( sqGain < gain_reQ )
1253 : {
1254 8668591 : prm[bits] = 0;
1255 8668591 : gain_reQ *= gain_corr_inv_fac[bits];
1256 : }
1257 : else
1258 : {
1259 8427857 : prm[bits] = 1;
1260 8427857 : gain_reQ *= gain_corr_fac[bits];
1261 : }
1262 17096448 : if ( bits < sqTargetBits )
1263 : {
1264 14119436 : *gain_tcx = gain_reQ;
1265 : }
1266 : }
1267 :
1268 5698816 : return ( bits );
1269 : }
1270 :
1271 : /*---------------------------------------------------------------------*
1272 : * refine_0()
1273 : *
1274 : *
1275 : *---------------------------------------------------------------------*/
1276 :
1277 29220567 : 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 29220567 : thres = ( 1.0f - sq_round ) * 0.33f * lf_deemph_factor;
1290 29220567 : if ( x_orig > thres * sqGain )
1291 : {
1292 : /* was (b > thres) */
1293 2664176 : prm[( *bits )++] = 1;
1294 2664176 : prm[( *bits )++] = 1;
1295 2664176 : *x_Q = 2.f * thres;
1296 : }
1297 26556391 : else if ( x_orig < -thres * sqGain )
1298 : {
1299 : /* was (b < -thres) */
1300 2672374 : prm[( *bits )++] = 1;
1301 2672374 : prm[( *bits )++] = 0;
1302 2672374 : *x_Q = -2.f * thres;
1303 : }
1304 : else
1305 : {
1306 23884017 : prm[( *bits )++] = 0;
1307 : }
1308 :
1309 29220567 : return;
1310 : }
1311 :
1312 : /*---------------------------------------------------------------------*
1313 : * tcx_res_Q_spec()
1314 : *
1315 : *
1316 : *---------------------------------------------------------------------*/
1317 :
1318 5698816 : 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 5698816 : sqTargetBits = min( sqTargetBits, NPRM_RESQ );
1334 :
1335 : /* Requantize the spectrum line-by-line */
1336 5698816 : fac_p = 0.5f - sq_round * 0.5f;
1337 5698816 : fac_m = sq_round * 0.5f;
1338 5698816 : if ( !lf_deemph_factors )
1339 : {
1340 423526144 : for ( i = 0; i < L_frame; i++ )
1341 : {
1342 423129000 : if ( bits >= sqTargetBits - kMaxEstimatorUndershoot )
1343 : {
1344 66483019 : fac_m = fac_p = 0;
1345 66483019 : if ( bits >= min( NPRM_RESQ, sqTargetBits + kMaxEstimatorOvershoot ) )
1346 : {
1347 3707733 : break;
1348 : }
1349 : }
1350 :
1351 419421267 : if ( x_Q[i] != 0.0f )
1352 : {
1353 51029387 : if ( x_orig[i] < (sqGain) *x_Q[i] )
1354 : {
1355 25510012 : prm[bits++] = 0;
1356 25510012 : x_Q[i] -= ( x_Q[i] > 0 ) ? fac_m : fac_p;
1357 : }
1358 : else
1359 : {
1360 25519375 : prm[bits++] = 1;
1361 25519375 : x_Q[i] += ( x_Q[i] > 0 ) ? fac_p : fac_m;
1362 : }
1363 : }
1364 : }
1365 4104877 : sqTargetBits -= 2; /* Quantize zeroed lines of the spectrum */
1366 21358519 : for ( i = 0; ( i < L_frame ) && ( bits < sqTargetBits ); i++ )
1367 : {
1368 : /* bits < sqTargetBits */
1369 17253642 : if ( x_Q[i] == 0.0f )
1370 : {
1371 12926758 : 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 334197595 : for ( i = bits; i < NPRM_RESQ; i++ )
1376 : {
1377 330092718 : prm[i] = 0;
1378 : }
1379 4104877 : return bits;
1380 : }
1381 271425843 : for ( i = 0; i < L_frame; i++ )
1382 : {
1383 271173833 : if ( bits >= sqTargetBits - kMaxEstimatorUndershoot )
1384 : {
1385 33023040 : fac_m = fac_p = 0;
1386 33023040 : if ( bits >= min( NPRM_RESQ, sqTargetBits + kMaxEstimatorOvershoot ) )
1387 : {
1388 1341929 : break;
1389 : }
1390 : }
1391 :
1392 269831904 : if ( x_Q[i] != 0 && lf_deemph_factors[i] > 0.5f )
1393 : {
1394 14910558 : if ( x_orig[i] < (sqGain) *x_Q[i] )
1395 : {
1396 7457516 : prm[bits++] = 0;
1397 7457516 : x_Q[i] -= ( x_Q[i] > 0 ) ? fac_m * lf_deemph_factors[i] : fac_p * lf_deemph_factors[i];
1398 : }
1399 : else
1400 : {
1401 7453042 : prm[bits++] = 1;
1402 7453042 : 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 19343265 : for ( i = 0; ( i < L_frame ) && ( bits < ( sqTargetBits - 2 ) ); i++ )
1409 : {
1410 : /* For (bits >= (sqTargetBits-2)) */
1411 17749326 : if ( x_Q[i] == 0 && lf_deemph_factors[i] > 0.5f )
1412 : {
1413 16293809 : 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 123789311 : for ( i = bits; i < NPRM_RESQ; i++ )
1419 : {
1420 122195372 : prm[i] = 0;
1421 : }
1422 :
1423 1593939 : return bits;
1424 : }
1425 :
1426 :
1427 : /*---------------------------------------------------------------------*
1428 : * ProcessIGF()
1429 : *
1430 : *
1431 : *---------------------------------------------------------------------*/
1432 :
1433 6905107 : 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 6905107 : IGF_ENC_INSTANCE_HANDLE hIGFEnc = st->hIGFEnc;
1449 6905107 : BSTR_ENC_HANDLE hBstr = st->hBstr;
1450 :
1451 6905107 : isIndepFlag = 1;
1452 6905107 : if ( st->last_core == ACELP_CORE && isTCX20 )
1453 : {
1454 94793 : igfGridIdx = IGF_GRID_LB_TRAN;
1455 : }
1456 6810314 : else if ( isTCX20 )
1457 : {
1458 6555938 : igfGridIdx = IGF_GRID_LB_NORM;
1459 : }
1460 : else
1461 : {
1462 : /* It is short block */
1463 254376 : igfGridIdx = IGF_GRID_LB_SHORT;
1464 254376 : if ( frameno == 1 )
1465 : {
1466 128600 : isIndepFlag = 0;
1467 : }
1468 : }
1469 :
1470 6905107 : IGFSaveSpectrumForITF( hIGFEnc, igfGridIdx, pITFMDCTSpectrum );
1471 :
1472 6905107 : IGFEncApplyMono( st, igfGridIdx, pMDCTSpectrum, pPowerSpectrum, isTCX20, st->hTcxEnc->fUseTns[frameno], sp_aud_decision0, vad_hover_flag );
1473 :
1474 6905107 : curr_order = 0;
1475 6905107 : predictionGain = 0;
1476 :
1477 6905107 : ITF_Detect( hIGFEnc->spec_be_igf, hIGFEnc->infoStartLine, hIGFEnc->infoStopLine, 8 /*maxOrder*/, A, &predictionGain, &curr_order );
1478 :
1479 6905107 : hIGFEnc->flatteningTrigger = hIGFEnc->tns_predictionGain < 1.15 && predictionGain < 1.15;
1480 :
1481 6905107 : hIGFEnc->infoTotalBitsPerFrameWritten = 0;
1482 :
1483 6905107 : if ( isTCX20 )
1484 : {
1485 6650731 : IGFEncWriteBitstream( hIGFEnc, NULL, &hIGFEnc->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1486 : }
1487 : else
1488 : {
1489 254376 : pBsStart = hBstr->nb_ind_tot;
1490 :
1491 254376 : IGFEncWriteBitstream( hIGFEnc, hBstr, &hIGFEnc->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1492 :
1493 254376 : bsBits = hBstr->nb_ind_tot - pBsStart;
1494 254376 : IGFEncConcatenateBitstream( hIGFEnc, bsBits, hBstr );
1495 : }
1496 :
1497 6905107 : return;
1498 : }
1499 :
1500 :
1501 : /*---------------------------------------------------------------------*
1502 : * ProcessStereoIGF()
1503 : *
1504 : *
1505 : *---------------------------------------------------------------------*/
1506 :
1507 909267 : 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 909267 : hIGFEnc[0] = sts[0]->hIGFEnc;
1528 909267 : hIGFEnc[1] = sts[1]->hIGFEnc;
1529 :
1530 909267 : isIndepFlag = 1;
1531 :
1532 909267 : if ( sts[0]->last_core == ACELP_CORE && sts[0]->core == TCX_20_CORE )
1533 : {
1534 0 : igfGridIdx = IGF_GRID_LB_TRAN;
1535 : }
1536 909267 : else if ( sts[0]->core == TCX_20_CORE )
1537 : {
1538 868501 : igfGridIdx = IGF_GRID_LB_NORM;
1539 : }
1540 : else
1541 : {
1542 : /* It is short block */
1543 40766 : igfGridIdx = IGF_GRID_LB_SHORT;
1544 40766 : if ( frameno == 1 )
1545 : {
1546 19677 : isIndepFlag = 0;
1547 : }
1548 : }
1549 :
1550 909267 : IGFSaveSpectrumForITF( hIGFEnc[0], igfGridIdx, pITFMDCTSpectrum[0][frameno] );
1551 :
1552 909267 : IGFSaveSpectrumForITF( hIGFEnc[1], igfGridIdx, pITFMDCTSpectrum[1][frameno] );
1553 :
1554 909267 : IGFEncApplyStereo( hStereoMdct, ms_mask, hIGFEnc, igfGridIdx, sts, pPowerSpectrum, pPowerSpectrumMsInv, inv_spectrum, frameno, sp_aud_decision0, element_brate, mct_on );
1555 :
1556 2727801 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1557 : {
1558 1818534 : curr_order = 0;
1559 :
1560 1818534 : ITF_Detect( hIGFEnc[ch]->spec_be_igf, hIGFEnc[ch]->infoStartLine, hIGFEnc[ch]->infoStopLine, 8 /*maxOrder*/, A, &predictionGain, &curr_order );
1561 :
1562 1818534 : hIGFEnc[ch]->flatteningTrigger = hIGFEnc[ch]->tns_predictionGain < 1.15 && predictionGain < 1.15;
1563 :
1564 1818534 : hIGFEnc[ch]->infoTotalBitsPerFrameWritten = 0;
1565 :
1566 1818534 : if ( sts[ch]->core == TCX_20_CORE )
1567 : {
1568 1737002 : IGFEncWriteBitstream( hIGFEnc[ch], NULL, &hIGFEnc[ch]->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1569 : }
1570 : else
1571 : {
1572 81532 : hBstr = sts[ch]->hBstr;
1573 81532 : pBsStart = hBstr->nb_ind_tot;
1574 81532 : if ( ch > 0 )
1575 : {
1576 40766 : hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
1577 : }
1578 81532 : IGFEncWriteBitstream( hIGFEnc[ch], hBstr, &hIGFEnc[ch]->infoTotalBitsPerFrameWritten, igfGridIdx, isIndepFlag );
1579 :
1580 81532 : bsBits = hBstr->nb_ind_tot - pBsStart;
1581 81532 : IGFEncConcatenateBitstream( hIGFEnc[ch], bsBits, hBstr );
1582 : }
1583 : }
1584 :
1585 909267 : return;
1586 : }
1587 :
1588 : /*---------------------------------------------------------------------*
1589 : * attenuateNbSpectrum()
1590 : *
1591 : *
1592 : *---------------------------------------------------------------------*/
1593 :
1594 2442 : void attenuateNbSpectrum(
1595 : const int16_t L_frame,
1596 : float *spectrum )
1597 : {
1598 : int16_t i, length;
1599 : float att;
1600 :
1601 2442 : length = L_frame / 20;
1602 2442 : att = ( length == 8 ) ? 0.6f : 0.66f;
1603 :
1604 57438 : for ( i = 0; i < length; i++ )
1605 : {
1606 54996 : spectrum[L_frame - length + i] *= att;
1607 54996 : att *= att;
1608 : }
1609 :
1610 2442 : return;
1611 : }
|