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 : #include "cnst.h"
41 : #include "rom_com.h"
42 : #include "prot.h"
43 : #include "stat_com.h"
44 : #include "wmc_auto.h"
45 :
46 :
47 : /*----------------------------------------------------------------------------
48 : * Local constants
49 : *---------------------------------------------------------------------------*/
50 :
51 : #define HLM_MIN_NRG PCM16_TO_FLT_FAC
52 : #define MAX_SUBDIVISIONS 3
53 :
54 :
55 : /*----------------------------------------------------------------------------
56 : * Local prototypes
57 : *---------------------------------------------------------------------------*/
58 :
59 :
60 : static void Index2Parcor( const int16_t index[], float parCoeff[], const int16_t order );
61 :
62 : static float FIRLattice( const int16_t order, const float *parCoeff, float *state, float x );
63 :
64 : static float IIRLattice( const int16_t order, const float *parCoeff, float *state, float x );
65 :
66 : static void TnsFilter( const float spectrum[], const int16_t numOfLines, const float parCoeff[], const int16_t order, TLinearPredictionFilter filter, float *state, float output[] );
67 :
68 :
69 : /*-------------------------------------------------------------------*
70 : * InitTnsConfiguration()
71 : *
72 : *-------------------------------------------------------------------*/
73 :
74 246564 : void InitTnsConfiguration(
75 : const int16_t bwidth,
76 : const int16_t frameLength,
77 : STnsConfig *pTnsConfig,
78 : const int16_t igfStopFreq,
79 : const int32_t total_brate,
80 : const int16_t element_mode,
81 : const int16_t MCT_flag )
82 : {
83 : int32_t nSampleRate;
84 : int16_t iFilter;
85 246564 : int16_t *startLineFilter = &pTnsConfig->iFilterBorders[1];
86 :
87 246564 : nSampleRate = inner_frame_tbl[bwidth] * FRAMES_PER_SEC;
88 :
89 : /* Sanity checks */
90 246564 : assert( ( nSampleRate > 0 ) && ( frameLength > 0 ) && ( pTnsConfig != NULL ) );
91 246564 : if ( ( nSampleRate <= 0 ) || ( frameLength <= 0 ) || ( pTnsConfig == NULL ) )
92 : {
93 0 : return /*TNS_FATAL_ERROR*/;
94 : }
95 :
96 : /* WMOPS: All initializations are either for safety or static (tables) and thus not to be counted */
97 : /* Initialize TNS filter flag and maximum order */
98 246564 : pTnsConfig->maxOrder = TNS_MAX_FILTER_ORDER;
99 246564 : if ( total_brate <= ACELP_32k )
100 : {
101 41280 : pTnsConfig->nMaxFilters = sizeof( tnsParametersIGF32kHz_LowBR ) / sizeof( tnsParametersIGF32kHz_LowBR[0] );
102 :
103 41280 : pTnsConfig->pTnsParameters = tnsParametersIGF32kHz_LowBR;
104 : }
105 : else
106 : {
107 205284 : if ( nSampleRate > 32000 && nSampleRate == 100 * frameLength )
108 : {
109 49260 : pTnsConfig->nMaxFilters = sizeof( tnsParameters48kHz_grouped ) / sizeof( tnsParameters48kHz_grouped[0] );
110 49260 : pTnsConfig->pTnsParameters = tnsParameters48kHz_grouped;
111 : }
112 156024 : else if ( nSampleRate > 16000 )
113 : {
114 144270 : if ( ( element_mode > IVAS_SCE ) && ( total_brate >= ( MCT_flag ? IVAS_32k : IVAS_48k ) ) )
115 : {
116 122862 : pTnsConfig->nMaxFilters = sizeof( tnsParameters32kHz_Stereo ) / sizeof( tnsParameters32kHz_Stereo[0] );
117 122862 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
118 : {
119 14158 : pTnsConfig->pTnsParameters = tnsParameters32kHz_grouped;
120 : }
121 : else
122 : {
123 108704 : pTnsConfig->pTnsParameters = tnsParameters32kHz_Stereo;
124 : }
125 : }
126 : else
127 : {
128 21408 : pTnsConfig->nMaxFilters = sizeof( tnsParameters32kHz ) / sizeof( tnsParameters32kHz[0] );
129 21408 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
130 : {
131 1092 : pTnsConfig->pTnsParameters = tnsParameters32kHz_grouped;
132 : }
133 : else
134 : {
135 20316 : pTnsConfig->pTnsParameters = tnsParameters32kHz;
136 : }
137 : }
138 : }
139 : else
140 : {
141 11754 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
142 : {
143 3918 : pTnsConfig->nMaxFilters = sizeof( tnsParameters16kHz_grouped ) / sizeof( tnsParameters16kHz_grouped[0] );
144 3918 : pTnsConfig->pTnsParameters = tnsParameters16kHz_grouped;
145 : }
146 : else
147 : {
148 7836 : pTnsConfig->nMaxFilters = sizeof( tnsParameters16kHz ) / sizeof( tnsParameters16kHz[0] );
149 7836 : pTnsConfig->pTnsParameters = tnsParameters16kHz;
150 : }
151 : }
152 : }
153 :
154 246564 : assert( pTnsConfig->nMaxFilters <= TNS_MAX_NUM_OF_FILTERS );
155 :
156 : /* Set starting MDCT line for each filter based on the starting frequencies from the TNS table */
157 690576 : for ( iFilter = 0; iFilter < pTnsConfig->nMaxFilters; iFilter++ )
158 : {
159 444012 : assert( pTnsConfig->pTnsParameters[iFilter].startLineFrequency < 0.5f * nSampleRate );
160 444012 : startLineFilter[iFilter] = (int16_t) ( ( frameLength * 2 * pTnsConfig->pTnsParameters[iFilter].startLineFrequency ) / nSampleRate );
161 : }
162 246564 : if ( igfStopFreq > 0 )
163 : {
164 189147 : pTnsConfig->iFilterBorders[0] = (int16_t) ( ( frameLength * 2 * igfStopFreq ) / nSampleRate );
165 : }
166 : else
167 : {
168 57417 : pTnsConfig->iFilterBorders[0] = frameLength;
169 : }
170 :
171 246564 : pTnsConfig->allowTnsOnWhite = 0;
172 :
173 246564 : return /*TNS_NO_ERROR*/;
174 : }
175 :
176 :
177 : /*-------------------------------------------------------------------*
178 : * ApplyTnsFilter()
179 : *
180 : *-------------------------------------------------------------------*/
181 :
182 376521 : void ApplyTnsFilter(
183 : STnsConfig const *pTnsConfig,
184 : STnsData const *pTnsData,
185 : float spectrum[],
186 : const int16_t fIsAnalysis )
187 : {
188 : TLinearPredictionFilter filter;
189 : float state[TNS_MAX_FILTER_ORDER];
190 : int16_t iFilter;
191 : int16_t stopLine, startLine;
192 : const int16_t *pBorders;
193 :
194 376521 : filter = fIsAnalysis ? FIRLattice : IIRLattice;
195 376521 : set_f( state, 0, TNS_MAX_FILTER_ORDER );
196 376521 : pBorders = pTnsConfig->iFilterBorders;
197 :
198 1123702 : for ( iFilter = pTnsConfig->nMaxFilters - 1; iFilter >= 0; iFilter-- )
199 : {
200 : float parCoeff[TNS_MAX_FILTER_ORDER];
201 747181 : const STnsFilter *pFilter = &pTnsData->filter[iFilter];
202 :
203 747181 : set_f( parCoeff, 0, TNS_MAX_FILTER_ORDER );
204 747181 : stopLine = pBorders[iFilter];
205 747181 : startLine = pBorders[iFilter + 1];
206 :
207 747181 : Index2Parcor( pFilter->coefIndex, parCoeff, pFilter->order );
208 :
209 747181 : TnsFilter( &spectrum[startLine], stopLine - startLine, parCoeff, pFilter->order, filter, state, &spectrum[startLine] );
210 : }
211 :
212 376521 : return /*( pTnsData->nFilters < 0 ) ? TNS_FATAL_ERROR : TNS_NO_ERROR*/;
213 : }
214 :
215 :
216 : /*********************************************************************************************/
217 : /* Definitions of functions used in the mapping between TNS parameters and a bitstream. */
218 : /*********************************************************************************************/
219 :
220 : /* Helper functions for hufmann table coding */
221 :
222 : /** Get number of bits from a Huffman table.
223 : * The table must be sorted by values.
224 : */
225 1497444 : static int16_t GetBitsFromTable( int16_t value, const Coding codes[], const int16_t nSize )
226 : {
227 : (void) nSize;
228 1497444 : assert( ( value >= 0 ) && ( value < nSize ) && ( nSize >= 0 ) && ( nSize <= 256 ) );
229 1497444 : return codes[value].nBits;
230 : }
231 :
232 : /** Get the code for a value from a Huffman table.
233 : * The table must be sorted by values.
234 : */
235 739793 : static int16_t EncodeUsingTable( int16_t value, const Coding codes[], const int16_t nSize )
236 : {
237 : (void) nSize;
238 739793 : assert( ( value >= 0 ) && ( value < nSize ) && ( nSize >= 0 ) && ( nSize <= 256 ) );
239 739793 : return codes[value].code;
240 : }
241 :
242 : /** Decode a value from a bitstream using a Huffman table. */
243 2191224 : static int16_t DecodeUsingTable( Decoder_State *st, int16_t *pValue, const Coding codes[], const int16_t nSize )
244 : {
245 2191224 : uint16_t code = 0;
246 2191224 : uint8_t nBits = 0;
247 2191224 : int16_t valueIndex = nSize;
248 :
249 2191224 : assert( ( nSize >= 0 ) && ( nSize <= 256 ) );
250 : /* Variable initialization. All are required! */
251 7888680 : while ( valueIndex == nSize )
252 : {
253 5697456 : code = ( code << 1 ) + get_next_indice( st, 1 );
254 5697456 : ++nBits;
255 5697456 : if ( nBits > nSize || nBits > 16 )
256 : {
257 0 : st->BER_detect = 1;
258 0 : *pValue = 0;
259 0 : return -1;
260 : }
261 73270974 : for ( valueIndex = 0; valueIndex < nSize; valueIndex++ )
262 : {
263 69764742 : if ( codes[valueIndex].nBits == nBits )
264 : {
265 6481014 : if ( codes[valueIndex].code == code )
266 : {
267 2191224 : break;
268 : }
269 : }
270 : }
271 : /* Loop condition */
272 : }
273 :
274 2191224 : if ( valueIndex < nSize )
275 : {
276 2191224 : *pValue = codes[valueIndex].value;
277 : }
278 : else
279 : {
280 0 : st->BER_detect = 1;
281 0 : *pValue = 0;
282 0 : return -1;
283 : }
284 :
285 2191224 : return nBits;
286 : }
287 :
288 : /* TNS filter coefficients */
289 :
290 640880 : void const *GetTnsFilterCoeff( void const *p, const int16_t index, int16_t *pValue )
291 : {
292 640880 : *pValue = ( (int16_t const *) p )[index] + INDEX_SHIFT;
293 640880 : return NULL;
294 : }
295 :
296 1898424 : void *SetTnsFilterCoeff( void *p, const int16_t index, const int16_t value )
297 : {
298 1898424 : ( (int16_t *) p )[index] = value - INDEX_SHIFT;
299 1898424 : return NULL;
300 : }
301 :
302 966897 : int16_t GetSWBTCX20TnsFilterCoeffBits( const int16_t value, const int16_t index )
303 : {
304 966897 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
305 966897 : return GetBitsFromTable( value, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
306 : }
307 :
308 477746 : int16_t EncodeSWBTCX20TnsFilterCoeff( const int16_t value, const int16_t index )
309 : {
310 477746 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
311 477746 : return EncodeUsingTable( value, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
312 : }
313 :
314 1417110 : int16_t DecodeSWBTCX20TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
315 : {
316 1417110 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
317 1417110 : return DecodeUsingTable( st, pValue, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
318 : }
319 :
320 283019 : int16_t GetSWBTCX10TnsFilterCoeffBits( const int16_t value, const int16_t index )
321 : {
322 283019 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
323 283019 : return GetBitsFromTable( value, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
324 : }
325 :
326 139921 : int16_t EncodeSWBTCX10TnsFilterCoeff( const int16_t value, const int16_t index )
327 : {
328 139921 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
329 139921 : return EncodeUsingTable( value, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
330 : }
331 :
332 413121 : int16_t DecodeSWBTCX10TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
333 : {
334 413121 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
335 413121 : return DecodeUsingTable( st, pValue, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
336 : }
337 :
338 16959 : int16_t GetWBTCX20TnsFilterCoeffBits( const int16_t value, const int16_t index )
339 : {
340 16959 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
341 16959 : return GetBitsFromTable( value, codesTnsCoeffWBTCX20[index], nTnsCoeffCodes );
342 : }
343 :
344 8328 : int16_t EncodeWBTCX20TnsFilterCoeff( const int16_t value, const int16_t index )
345 : {
346 8328 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
347 8328 : return EncodeUsingTable( value, codesTnsCoeffWBTCX20[index], nTnsCoeffCodes );
348 : }
349 :
350 24162 : int16_t DecodeWBTCX20TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
351 : {
352 24162 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
353 24162 : return DecodeUsingTable( st, pValue, codesTnsCoeffWBTCX20[index], nTnsCoeffCodes );
354 : }
355 :
356 :
357 : /* TNS filter order */
358 :
359 116771 : void const *GetTnsFilterOrder( void const *p, const int16_t index, int16_t *pValue )
360 : {
361 116771 : *pValue = ( (STnsFilter const *) p )[index].order;
362 116771 : return ( (STnsFilter const *) p )[index].coefIndex;
363 : }
364 :
365 345576 : void *SetTnsFilterOrder( void *p, const int16_t index, const int16_t value )
366 : {
367 345576 : ( (STnsFilter *) p )[index].order = value;
368 345576 : return ( (STnsFilter *) p )[index].coefIndex;
369 : }
370 :
371 168121 : int16_t GetTnsFilterOrderBitsSWBTCX20( const int16_t value, const int16_t index )
372 : {
373 : (void) index;
374 168121 : return GetBitsFromTable( value - 1, codesTnsOrderTCX20, nTnsOrderCodes );
375 : }
376 :
377 83002 : int16_t EncodeTnsFilterOrderSWBTCX20( const int16_t value, const int16_t index )
378 : {
379 : (void) index;
380 83002 : return EncodeUsingTable( value - 1, codesTnsOrderTCX20, nTnsOrderCodes );
381 : }
382 :
383 246069 : int16_t DecodeTnsFilterOrderSWBTCX20( Decoder_State *st, const int16_t index, int16_t *pValue )
384 : {
385 : (void) index;
386 246069 : return DecodeUsingTable( st, pValue, codesTnsOrderTCX20, nTnsOrderCodes );
387 : }
388 :
389 59665 : int16_t GetTnsFilterOrderBitsSWBTCX10( const int16_t value, const int16_t index )
390 : {
391 : (void) index;
392 59665 : return GetBitsFromTable( value - 1, codesTnsOrderTCX10, nTnsOrderCodes );
393 : }
394 :
395 29436 : int16_t EncodeTnsFilterOrderSWBTCX10( const int16_t value, const int16_t index )
396 : {
397 : (void) index;
398 29436 : return EncodeUsingTable( value - 1, codesTnsOrderTCX10, nTnsOrderCodes );
399 : }
400 :
401 86823 : int16_t DecodeTnsFilterOrderSWBTCX10( Decoder_State *st, const int16_t index, int16_t *pValue )
402 : {
403 : (void) index;
404 86823 : return DecodeUsingTable( st, pValue, codesTnsOrderTCX10, nTnsOrderCodes );
405 : }
406 :
407 2783 : int16_t GetTnsFilterOrderBits( const int16_t value, const int16_t index )
408 : {
409 : (void) index;
410 2783 : return GetBitsFromTable( value - 1, codesTnsOrder, nTnsOrderCodes );
411 : }
412 :
413 1360 : int16_t EncodeTnsFilterOrder( const int16_t value, const int16_t index )
414 : {
415 : (void) index;
416 1360 : return EncodeUsingTable( value - 1, codesTnsOrder, nTnsOrderCodes );
417 : }
418 :
419 3939 : int16_t DecodeTnsFilterOrder( Decoder_State *st, const int16_t index, int16_t *pValue )
420 : {
421 : (void) index;
422 3939 : return DecodeUsingTable( st, pValue, codesTnsOrder, nTnsOrderCodes );
423 : }
424 :
425 : /* Number of TNS filters */
426 :
427 89350 : void const *GetNumOfTnsFilters( void const *p, const int16_t index, int16_t *pValue )
428 : {
429 89350 : *pValue = (int16_t) abs( ( (STnsData const *) p )[index].nFilters );
430 89350 : return ( (STnsData const *) p )[index].filter;
431 : }
432 :
433 264828 : void *SetNumOfTnsFilters( void *p, const int16_t index, const int16_t value )
434 : {
435 264828 : ( (STnsData *) p )[index].nFilters = (int16_t) abs( value );
436 264828 : return ( (STnsData *) p )[index].filter;
437 : }
438 :
439 : /* TNS enabled/disabled flag */
440 :
441 1293768 : void const *GetTnsEnabled( void const *p, const int16_t index, int16_t *pValue )
442 : {
443 1293768 : *pValue = ( (STnsData const *) p )[index].nFilters != 0 ? 1 : 0;
444 1293768 : return NULL;
445 : }
446 :
447 2101545 : void *SetTnsEnabled( void *p, const int16_t index, const int16_t value )
448 : {
449 : (void) p, (void) index, (void) value;
450 2101545 : return NULL;
451 : }
452 :
453 : /* TNS on whitened spectra flag */
454 :
455 72031 : void const *GetTnsOnWhite( void const *p, const int16_t index, int16_t *pValue )
456 : {
457 72031 : *pValue = ( (STnsData const *) p )[index].tnsOnWhitenedSpectra > 0 ? 1 : 0;
458 72031 : return NULL;
459 : }
460 :
461 213732 : void *SetTnsOnWhite( void *p, const int16_t index, const int16_t value )
462 : {
463 213732 : ( (STnsData *) p )[index].tnsOnWhitenedSpectra = value;
464 213732 : return NULL;
465 : }
466 :
467 167049 : void const *GetTnsEnabledSingleFilter( void const *p, const int16_t index, int16_t *pValue )
468 : {
469 167049 : *pValue = ( (STnsData const *) p )[index].nFilters != 0 ? 1 : 0;
470 167049 : return ( (STnsData const *) p )[index].filter;
471 : }
472 :
473 305625 : void *SetTnsEnabledSingleFilter( void *p, const int16_t index, const int16_t value )
474 : {
475 305625 : ( (STnsData *) p )[index].nFilters = value;
476 305625 : return ( (STnsData *) p )[index].filter;
477 : }
478 :
479 :
480 : /*-------------------------------------------------------------------*
481 : * ResetTnsData()
482 : *
483 : *-------------------------------------------------------------------*/
484 :
485 3867987 : void ResetTnsData(
486 : STnsData *pTnsData )
487 : {
488 : uint16_t iFilter;
489 :
490 3867987 : pTnsData->nFilters = 0;
491 3867987 : pTnsData->tnsOnWhitenedSpectra = 0;
492 :
493 11603961 : for ( iFilter = 0; iFilter < sizeof( pTnsData->filter ) / sizeof( pTnsData->filter[0] ); iFilter++ )
494 : {
495 7735974 : STnsFilter *const pTnsFilter = &pTnsData->filter[iFilter];
496 7735974 : pTnsFilter->spectrumLength = 0;
497 7735974 : pTnsFilter->predictionGain = 1.0f;
498 7735974 : pTnsFilter->avgSqrCoef = 0;
499 7735974 : pTnsFilter->filterType = TNS_FILTER_OFF;
500 7735974 : ClearTnsFilterCoefficients( pTnsFilter );
501 : }
502 :
503 3867987 : return;
504 : }
505 :
506 :
507 : /*-------------------------------------------------------------------*
508 : * ClearTnsFilterCoefficients()
509 : *
510 : *-------------------------------------------------------------------*/
511 :
512 11609940 : void ClearTnsFilterCoefficients(
513 : STnsFilter *pTnsFilter )
514 : {
515 11609940 : pTnsFilter->order = 0;
516 11609940 : set_s( pTnsFilter->coefIndex, 0, TNS_MAX_FILTER_ORDER );
517 :
518 11609940 : return;
519 : }
520 :
521 : /** Inverse quantization for reflection coefficients.
522 : *
523 : * @param index input quantized values.
524 : * @param parCoeff output reflection coefficients.
525 : * @param order number of coefficients/values.
526 : */
527 747181 : static void Index2Parcor(
528 : const int16_t index[],
529 : float parCoeff[],
530 : const int16_t order )
531 : {
532 747181 : const float *values = tnsCoeff4;
533 : int16_t i;
534 :
535 3415669 : for ( i = 0; i < order; i++ )
536 : {
537 2668488 : parCoeff[i] = values[index[i] + INDEX_SHIFT];
538 : }
539 :
540 747181 : return;
541 : }
542 :
543 : /* Linear prediction analysis filter. */
544 51707379 : static float FIRLattice(
545 : const int16_t order,
546 : const float *parCoeff,
547 : float *state,
548 : float x )
549 : {
550 : int16_t i;
551 : float tmpSave;
552 :
553 51707379 : tmpSave = x;
554 287183186 : for ( i = 0; i < order - 1; i++ )
555 : {
556 235475807 : const float tmp = parCoeff[i] * x + state[i];
557 : /* Variable initialization */
558 235475807 : x += parCoeff[i] * state[i];
559 235475807 : state[i] = tmpSave;
560 235475807 : tmpSave = tmp;
561 : }
562 :
563 : /* last stage: only need half operations */
564 51707379 : x += parCoeff[order - 1] * state[order - 1];
565 51707379 : state[order - 1] = tmpSave;
566 :
567 51707379 : return x;
568 : }
569 :
570 : /* Linear prediction synthesis filter. */
571 164094193 : static float IIRLattice(
572 : const int16_t order,
573 : const float *parCoeff,
574 : float *state,
575 : float x )
576 : {
577 : int16_t i;
578 :
579 : /* first stage: no need to calculate state[order-1] */
580 164094193 : x -= parCoeff[order - 1] * state[order - 1];
581 906628217 : for ( i = order - 2; i >= 0; i-- )
582 : {
583 742534024 : x -= parCoeff[i] * state[i];
584 742534024 : state[i + 1] = parCoeff[i] * x + state[i];
585 : }
586 :
587 164094193 : state[0] = x;
588 :
589 164094193 : return x;
590 : }
591 :
592 : /** TNS analysis/synthesis filter.
593 : * @param spectrum input spectrum values.
594 : * @param numOfLines number of lines in the spectrum.
595 : * @param parCoeff filter (PARCOR) coefficients.
596 : * @param order filter order.
597 : * @param filter function that implements filtering.
598 : By this function it is defined whether analysis or synthesis is performed.
599 : * @param output filtered output spectrum values.
600 : Inplace operation is supported, so it can be equal to spectrum.
601 : */
602 747181 : static void TnsFilter(
603 : const float spectrum[],
604 : const int16_t numOfLines,
605 : const float parCoeff[],
606 : const int16_t order,
607 : TLinearPredictionFilter filter,
608 : float *state,
609 : float output[] )
610 : {
611 : int16_t j;
612 :
613 747181 : assert( ( order >= 0 ) && ( order <= TNS_MAX_FILTER_ORDER ) );
614 747181 : assert( ( numOfLines > 0 ) || ( ( numOfLines == 0 ) && ( order == 0 ) ) );
615 :
616 747181 : if ( order == 0 )
617 : {
618 260540 : if ( ( spectrum != output ) && ( numOfLines > 0 ) )
619 : {
620 0 : mvr2r( spectrum, output, numOfLines );
621 : }
622 : }
623 : else
624 : {
625 216288213 : for ( j = 0; j < numOfLines; j++ )
626 : {
627 215801572 : output[j] = filter( order, parCoeff, state, spectrum[j] );
628 : }
629 : }
630 :
631 747181 : return;
632 : }
633 :
634 :
635 1573581 : static void ITF_TnsFilter(
636 : const float spectrum[],
637 : const int16_t numOfLines,
638 : const float A[],
639 : const int16_t order,
640 : float output[] )
641 : {
642 : int16_t i, j;
643 : float *p, buf[ITF_MAX_FILTER_ORDER + N_MAX];
644 :
645 1573581 : assert( ( order >= 0 ) && ( order <= ITF_MAX_FILTER_ORDER ) );
646 1573581 : assert( ( numOfLines > 0 ) || ( ( numOfLines == 0 ) && ( order == 0 ) ) );
647 :
648 1573581 : if ( order == 0 )
649 : {
650 1347063 : if ( ( spectrum != output ) && ( numOfLines > 0 ) )
651 : {
652 0 : mvr2r( spectrum, output, numOfLines );
653 : }
654 : }
655 : else
656 : {
657 226518 : p = buf + ITF_MAX_FILTER_ORDER;
658 :
659 226518 : set_f( buf, 0, ITF_MAX_FILTER_ORDER );
660 226518 : mvr2r( spectrum, p, numOfLines );
661 :
662 74304798 : for ( j = 0; j < numOfLines; j++, p++ )
663 : {
664 74078280 : output[j] = p[0];
665 592626240 : for ( i = 1; i < order; i++ )
666 : {
667 518547960 : output[j] += A[i] * p[-i];
668 : }
669 : }
670 : }
671 :
672 1573581 : return;
673 : }
674 :
675 :
676 : /*********************************************************************************************/
677 : /* Definitions of functions used in the mapping between TNS parameters and a bitstream. */
678 : /*********************************************************************************************/
679 :
680 : /* Helper functions for hufmann table coding */
681 :
682 : /********************************/
683 : /* Private functions */
684 : /********************************/
685 :
686 : /** Autocorrelation to parcor coefficients.
687 : * Conversion of autocorrelation to parcor/reflection coefficients.
688 : * @param input Autocorrelation function/coefficients.
689 : * @param parCoeff output filter (PARCOR) coefficients.
690 : * @param order filter order.
691 : * @return prediction gain.
692 : */
693 330364 : static float ITF_AutoToLPcoef(
694 : const float input[],
695 : float a[],
696 : const int16_t order )
697 : {
698 : int16_t i, j;
699 : float tmp, tmp2;
700 : float workBuffer[2 * ITF_MAX_FILTER_ORDER];
701 : float parCoeff[ITF_MAX_FILTER_ORDER];
702 330364 : float *const pWorkBuffer = &workBuffer[order]; /* temp pointer */
703 :
704 2973276 : for ( i = 0; i < order; i++ )
705 : {
706 2642912 : workBuffer[i] = input[i];
707 2642912 : pWorkBuffer[i] = input[i + 1];
708 : }
709 :
710 2973276 : for ( i = 0; i < order; i++ )
711 : {
712 2642912 : if ( workBuffer[0] < 1.0f / 65536.0f )
713 : {
714 0 : tmp = 0;
715 : }
716 : else
717 : {
718 2642912 : tmp = -pWorkBuffer[i] / workBuffer[0];
719 : }
720 :
721 : /* compensate for calculation inaccuracies limit reflection coefs to ]-1,1[ */
722 2642912 : tmp = min( 0.999f, max( -0.999f, tmp ) );
723 :
724 2642912 : parCoeff[i] = tmp;
725 14536016 : for ( j = i; j < order; j++ )
726 : {
727 11893104 : tmp2 = pWorkBuffer[j] + tmp * workBuffer[j - i];
728 11893104 : workBuffer[j - i] += tmp * pWorkBuffer[j];
729 11893104 : pWorkBuffer[j] = tmp2;
730 : }
731 : }
732 :
733 : /* Convert ParCor / reflection coefficients to LPC */
734 330364 : a[0] = 1.0f;
735 330364 : a[1] = parCoeff[0];
736 :
737 2642912 : for ( i = 1; i < order; i++ )
738 : {
739 6276916 : for ( j = 0; j < i / 2; j++ )
740 : {
741 3964368 : tmp = a[j + 1];
742 3964368 : a[j + 1] += parCoeff[i] * a[i - 1 - j + 1];
743 3964368 : a[i - 1 - j + 1] += parCoeff[i] * tmp;
744 : }
745 2312548 : if ( i & 1 )
746 : {
747 1321456 : a[j + 1] += parCoeff[i] * a[j + 1];
748 : }
749 :
750 2312548 : a[i + 1] = parCoeff[i];
751 : }
752 :
753 330364 : return ( ( input[0] + 1e-30f ) / ( workBuffer[0] + 1e-30f ) );
754 : }
755 :
756 :
757 : /*-------------------------------------------------------------------*
758 : * ITF_Apply()
759 : *
760 : *-------------------------------------------------------------------*/
761 :
762 1573581 : void ITF_Apply(
763 : float spectrum[],
764 : const int16_t startLine,
765 : const int16_t stopLine,
766 : const float *A,
767 : const int16_t order )
768 : {
769 1573581 : ITF_TnsFilter( &spectrum[startLine], stopLine - startLine, A, order, &spectrum[startLine] );
770 :
771 1573581 : return;
772 : }
773 :
774 :
775 : /*-------------------------------------------------------------------*
776 : * ITF_Detect()
777 : *
778 : *
779 : *-------------------------------------------------------------------*/
780 :
781 2188516 : int16_t ITF_Detect(
782 : const float pSpectrum[],
783 : const int16_t startLine,
784 : const int16_t stopLine,
785 : const int16_t maxOrder,
786 : float *A,
787 : float *predictionGain,
788 : int16_t *curr_order )
789 : {
790 : float norms[MAX_SUBDIVISIONS];
791 : int16_t nSubdivisions;
792 : int16_t iStartLine, iEndLine, spectrumLength;
793 : float rxx[ITF_MAX_FILTER_ORDER + 1];
794 : float fac;
795 : const float *pWindow;
796 : int16_t iSubdivisions, lag;
797 :
798 2188516 : nSubdivisions = MAX_SUBDIVISIONS;
799 2188516 : set_f( norms, 0.f, MAX_SUBDIVISIONS );
800 2188516 : set_f( rxx, 0.f, ITF_MAX_FILTER_ORDER + 1 ); /* This initialization is required */
801 :
802 2188516 : if ( maxOrder <= 0 )
803 : {
804 0 : return 0;
805 : }
806 :
807 : /* Calculate norms for each spectrum part */
808 8754064 : for ( iSubdivisions = 0; iSubdivisions < nSubdivisions; iSubdivisions++ )
809 : {
810 6565548 : iStartLine = startLine + ( stopLine - startLine ) * iSubdivisions / nSubdivisions;
811 6565548 : iEndLine = startLine + ( stopLine - startLine ) * ( iSubdivisions + 1 ) / nSubdivisions;
812 :
813 : /* Variable initialization */
814 6565548 : norms[iSubdivisions] = sum2_f( pSpectrum + iStartLine - IGF_START_MN, iEndLine - iStartLine );
815 : }
816 :
817 : /* Calculate normalized autocorrelation for spectrum subdivision and get TNS filter parameters based on it */
818 2188516 : spectrumLength = stopLine - startLine;
819 :
820 : /* Variable initialization */
821 3510437 : for ( iSubdivisions = 0; ( iSubdivisions < nSubdivisions ) && ( norms[iSubdivisions] > HLM_MIN_NRG ); iSubdivisions++ )
822 : {
823 1321921 : fac = 1.0f / norms[iSubdivisions];
824 1321921 : iStartLine = startLine + spectrumLength * iSubdivisions / nSubdivisions;
825 1321921 : iEndLine = startLine + spectrumLength * ( iSubdivisions + 1 ) / nSubdivisions;
826 1321921 : pWindow = tnsAcfWindow;
827 :
828 : /* For additional loop condition */
829 : /* Variable initialization */
830 11897289 : for ( lag = 1; lag <= maxOrder; lag++ )
831 : {
832 10575368 : rxx[lag] += fac * ( *pWindow ) * dotp( pSpectrum + iStartLine - IGF_START_MN, pSpectrum + iStartLine - IGF_START_MN + lag, iEndLine - iStartLine - lag );
833 10575368 : pWindow++;
834 : }
835 : }
836 :
837 2188516 : *predictionGain = 0;
838 2188516 : if ( iSubdivisions == nSubdivisions ) /* meaning there is no subdivision with low energy */
839 : {
840 330364 : rxx[0] = (float) nSubdivisions;
841 :
842 : /* Limit the maximum order to spectrum length/4 */
843 330364 : *predictionGain = ITF_AutoToLPcoef( rxx, A, min( maxOrder, spectrumLength / 4 ) );
844 :
845 330364 : *curr_order = maxOrder;
846 : }
847 :
848 2188516 : return 1;
849 : }
|