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 3337452 : 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 3337452 : int16_t *startLineFilter = &pTnsConfig->iFilterBorders[1];
86 :
87 3337452 : nSampleRate = inner_frame_tbl[bwidth] * FRAMES_PER_SEC;
88 :
89 : /* Sanity checks */
90 3337452 : assert( ( nSampleRate > 0 ) && ( frameLength > 0 ) && ( pTnsConfig != NULL ) );
91 3337452 : 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 3337452 : pTnsConfig->maxOrder = TNS_MAX_FILTER_ORDER;
99 3337452 : if ( total_brate <= ACELP_32k )
100 : {
101 557004 : pTnsConfig->nMaxFilters = sizeof( tnsParametersIGF32kHz_LowBR ) / sizeof( tnsParametersIGF32kHz_LowBR[0] );
102 :
103 557004 : pTnsConfig->pTnsParameters = tnsParametersIGF32kHz_LowBR;
104 : }
105 : else
106 : {
107 2780448 : if ( nSampleRate > 32000 && nSampleRate == 100 * frameLength )
108 : {
109 733054 : pTnsConfig->nMaxFilters = sizeof( tnsParameters48kHz_grouped ) / sizeof( tnsParameters48kHz_grouped[0] );
110 733054 : pTnsConfig->pTnsParameters = tnsParameters48kHz_grouped;
111 : }
112 2047394 : else if ( nSampleRate > 16000 )
113 : {
114 1926236 : if ( ( element_mode > IVAS_SCE ) && ( total_brate >= ( MCT_flag ? IVAS_32k : IVAS_48k ) ) )
115 : {
116 1677332 : pTnsConfig->nMaxFilters = sizeof( tnsParameters32kHz_Stereo ) / sizeof( tnsParameters32kHz_Stereo[0] );
117 1677332 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
118 : {
119 141224 : pTnsConfig->pTnsParameters = tnsParameters32kHz_grouped;
120 : }
121 : else
122 : {
123 1536108 : pTnsConfig->pTnsParameters = tnsParameters32kHz_Stereo;
124 : }
125 : }
126 : else
127 : {
128 248904 : pTnsConfig->nMaxFilters = sizeof( tnsParameters32kHz ) / sizeof( tnsParameters32kHz[0] );
129 248904 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
130 : {
131 12152 : pTnsConfig->pTnsParameters = tnsParameters32kHz_grouped;
132 : }
133 : else
134 : {
135 236752 : pTnsConfig->pTnsParameters = tnsParameters32kHz;
136 : }
137 : }
138 : }
139 : else
140 : {
141 121158 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
142 : {
143 40386 : pTnsConfig->nMaxFilters = sizeof( tnsParameters16kHz_grouped ) / sizeof( tnsParameters16kHz_grouped[0] );
144 40386 : pTnsConfig->pTnsParameters = tnsParameters16kHz_grouped;
145 : }
146 : else
147 : {
148 80772 : pTnsConfig->nMaxFilters = sizeof( tnsParameters16kHz ) / sizeof( tnsParameters16kHz[0] );
149 80772 : pTnsConfig->pTnsParameters = tnsParameters16kHz;
150 : }
151 : }
152 : }
153 :
154 3337452 : 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 9374580 : for ( iFilter = 0; iFilter < pTnsConfig->nMaxFilters; iFilter++ )
158 : {
159 6037128 : assert( pTnsConfig->pTnsParameters[iFilter].startLineFrequency < 0.5f * nSampleRate );
160 6037128 : startLineFilter[iFilter] = (int16_t) ( ( frameLength * 2 * pTnsConfig->pTnsParameters[iFilter].startLineFrequency ) / nSampleRate );
161 : }
162 3337452 : if ( igfStopFreq > 0 )
163 : {
164 2729031 : pTnsConfig->iFilterBorders[0] = (int16_t) ( ( frameLength * 2 * igfStopFreq ) / nSampleRate );
165 : }
166 : else
167 : {
168 608421 : pTnsConfig->iFilterBorders[0] = frameLength;
169 : }
170 :
171 3337452 : pTnsConfig->allowTnsOnWhite = 0;
172 :
173 3337452 : return /*TNS_NO_ERROR*/;
174 : }
175 :
176 :
177 : /*-------------------------------------------------------------------*
178 : * ApplyTnsFilter()
179 : *
180 : *-------------------------------------------------------------------*/
181 :
182 3911782 : 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 3911782 : filter = fIsAnalysis ? FIRLattice : IIRLattice;
195 3911782 : set_f( state, 0, TNS_MAX_FILTER_ORDER );
196 3911782 : pBorders = pTnsConfig->iFilterBorders;
197 :
198 11656333 : for ( iFilter = pTnsConfig->nMaxFilters - 1; iFilter >= 0; iFilter-- )
199 : {
200 : float parCoeff[TNS_MAX_FILTER_ORDER];
201 7744551 : const STnsFilter *pFilter = &pTnsData->filter[iFilter];
202 :
203 7744551 : set_f( parCoeff, 0, TNS_MAX_FILTER_ORDER );
204 7744551 : stopLine = pBorders[iFilter];
205 7744551 : startLine = pBorders[iFilter + 1];
206 :
207 7744551 : Index2Parcor( pFilter->coefIndex, parCoeff, pFilter->order );
208 :
209 7744551 : TnsFilter( &spectrum[startLine], stopLine - startLine, parCoeff, pFilter->order, filter, state, &spectrum[startLine] );
210 : }
211 :
212 3911782 : 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 15846449 : static int16_t GetBitsFromTable( int16_t value, const Coding codes[], const int16_t nSize )
226 : {
227 : (void) nSize;
228 15846449 : assert( ( value >= 0 ) && ( value < nSize ) && ( nSize >= 0 ) && ( nSize <= 256 ) );
229 15846449 : 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 7707592 : static int16_t EncodeUsingTable( int16_t value, const Coding codes[], const int16_t nSize )
236 : {
237 : (void) nSize;
238 7707592 : assert( ( value >= 0 ) && ( value < nSize ) && ( nSize >= 0 ) && ( nSize <= 256 ) );
239 7707592 : return codes[value].code;
240 : }
241 :
242 : /** Decode a value from a bitstream using a Huffman table. */
243 22391072 : static int16_t DecodeUsingTable( Decoder_State *st, int16_t *pValue, const Coding codes[], const int16_t nSize )
244 : {
245 22391072 : uint16_t code = 0;
246 22391072 : uint8_t nBits = 0;
247 22391072 : int16_t valueIndex = nSize;
248 :
249 22391072 : assert( ( nSize >= 0 ) && ( nSize <= 256 ) );
250 : /* Variable initialization. All are required! */
251 81908129 : while ( valueIndex == nSize )
252 : {
253 59517057 : code = ( code << 1 ) + get_next_indice( st, 1 );
254 59517057 : ++nBits;
255 59517057 : if ( nBits > nSize || nBits > 16 )
256 : {
257 0 : st->BER_detect = 1;
258 0 : *pValue = 0;
259 0 : return -1;
260 : }
261 765807766 : for ( valueIndex = 0; valueIndex < nSize; valueIndex++ )
262 : {
263 728681781 : if ( codes[valueIndex].nBits == nBits )
264 : {
265 67843614 : if ( codes[valueIndex].code == code )
266 : {
267 22391072 : break;
268 : }
269 : }
270 : }
271 : /* Loop condition */
272 : }
273 :
274 22391072 : if ( valueIndex < nSize )
275 : {
276 22391072 : *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 22391072 : return nBits;
286 : }
287 :
288 : /* TNS filter coefficients */
289 :
290 6851146 : void const *GetTnsFilterCoeff( void const *p, const int16_t index, int16_t *pValue )
291 : {
292 6851146 : *pValue = ( (int16_t const *) p )[index] + INDEX_SHIFT;
293 6851146 : return NULL;
294 : }
295 :
296 19898530 : void *SetTnsFilterCoeff( void *p, const int16_t index, const int16_t value )
297 : {
298 19898530 : ( (int16_t *) p )[index] = value - INDEX_SHIFT;
299 19898530 : return NULL;
300 : }
301 :
302 9793351 : int16_t GetSWBTCX20TnsFilterCoeffBits( const int16_t value, const int16_t index )
303 : {
304 9793351 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
305 9793351 : return GetBitsFromTable( value, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
306 : }
307 :
308 4759496 : int16_t EncodeSWBTCX20TnsFilterCoeff( const int16_t value, const int16_t index )
309 : {
310 4759496 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
311 4759496 : return EncodeUsingTable( value, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
312 : }
313 :
314 13831496 : int16_t DecodeSWBTCX20TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
315 : {
316 13831496 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
317 13831496 : return DecodeUsingTable( st, pValue, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
318 : }
319 :
320 3316980 : int16_t GetSWBTCX10TnsFilterCoeffBits( const int16_t value, const int16_t index )
321 : {
322 3316980 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
323 3316980 : return GetBitsFromTable( value, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
324 : }
325 :
326 1617789 : int16_t EncodeSWBTCX10TnsFilterCoeff( const int16_t value, const int16_t index )
327 : {
328 1617789 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
329 1617789 : return EncodeUsingTable( value, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
330 : }
331 :
332 4703463 : int16_t DecodeSWBTCX10TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
333 : {
334 4703463 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
335 4703463 : return DecodeUsingTable( st, pValue, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
336 : }
337 :
338 230337 : int16_t GetWBTCX20TnsFilterCoeffBits( const int16_t value, const int16_t index )
339 : {
340 230337 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
341 230337 : return GetBitsFromTable( value, codesTnsCoeffWBTCX20[index], nTnsCoeffCodes );
342 : }
343 :
344 112237 : int16_t EncodeWBTCX20TnsFilterCoeff( const int16_t value, const int16_t index )
345 : {
346 112237 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
347 112237 : return EncodeUsingTable( value, codesTnsCoeffWBTCX20[index], nTnsCoeffCodes );
348 : }
349 :
350 315890 : int16_t DecodeWBTCX20TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
351 : {
352 315890 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
353 315890 : return DecodeUsingTable( st, pValue, codesTnsCoeffWBTCX20[index], nTnsCoeffCodes );
354 : }
355 :
356 :
357 : /* TNS filter order */
358 :
359 1287711 : void const *GetTnsFilterOrder( void const *p, const int16_t index, int16_t *pValue )
360 : {
361 1287711 : *pValue = ( (STnsFilter const *) p )[index].order;
362 1287711 : return ( (STnsFilter const *) p )[index].coefIndex;
363 : }
364 :
365 3741028 : void *SetTnsFilterOrder( void *p, const int16_t index, const int16_t value )
366 : {
367 3741028 : ( (STnsFilter *) p )[index].order = value;
368 3741028 : return ( (STnsFilter *) p )[index].coefIndex;
369 : }
370 :
371 1724810 : int16_t GetTnsFilterOrderBitsSWBTCX20( const int16_t value, const int16_t index )
372 : {
373 : (void) index;
374 1724810 : return GetBitsFromTable( value - 1, codesTnsOrderTCX20, nTnsOrderCodes );
375 : }
376 :
377 837958 : int16_t EncodeTnsFilterOrderSWBTCX20( const int16_t value, const int16_t index )
378 : {
379 : (void) index;
380 837958 : return EncodeUsingTable( value - 1, codesTnsOrderTCX20, nTnsOrderCodes );
381 : }
382 :
383 2435084 : int16_t DecodeTnsFilterOrderSWBTCX20( Decoder_State *st, const int16_t index, int16_t *pValue )
384 : {
385 : (void) index;
386 2435084 : return DecodeUsingTable( st, pValue, codesTnsOrderTCX20, nTnsOrderCodes );
387 : }
388 :
389 742816 : int16_t GetTnsFilterOrderBitsSWBTCX10( const int16_t value, const int16_t index )
390 : {
391 : (void) index;
392 742816 : return GetBitsFromTable( value - 1, codesTnsOrderTCX10, nTnsOrderCodes );
393 : }
394 :
395 361575 : int16_t EncodeTnsFilterOrderSWBTCX10( const int16_t value, const int16_t index )
396 : {
397 : (void) index;
398 361575 : return EncodeUsingTable( value - 1, codesTnsOrderTCX10, nTnsOrderCodes );
399 : }
400 :
401 1052987 : int16_t DecodeTnsFilterOrderSWBTCX10( Decoder_State *st, const int16_t index, int16_t *pValue )
402 : {
403 : (void) index;
404 1052987 : return DecodeUsingTable( st, pValue, codesTnsOrderTCX10, nTnsOrderCodes );
405 : }
406 :
407 38155 : int16_t GetTnsFilterOrderBits( const int16_t value, const int16_t index )
408 : {
409 : (void) index;
410 38155 : return GetBitsFromTable( value - 1, codesTnsOrder, nTnsOrderCodes );
411 : }
412 :
413 18537 : int16_t EncodeTnsFilterOrder( const int16_t value, const int16_t index )
414 : {
415 : (void) index;
416 18537 : return EncodeUsingTable( value - 1, codesTnsOrder, nTnsOrderCodes );
417 : }
418 :
419 52152 : int16_t DecodeTnsFilterOrder( Decoder_State *st, const int16_t index, int16_t *pValue )
420 : {
421 : (void) index;
422 52152 : return DecodeUsingTable( st, pValue, codesTnsOrder, nTnsOrderCodes );
423 : }
424 :
425 : /* Number of TNS filters */
426 :
427 964141 : void const *GetNumOfTnsFilters( void const *p, const int16_t index, int16_t *pValue )
428 : {
429 964141 : *pValue = (int16_t) abs( ( (STnsData const *) p )[index].nFilters );
430 964141 : return ( (STnsData const *) p )[index].filter;
431 : }
432 :
433 2798113 : void *SetNumOfTnsFilters( void *p, const int16_t index, const int16_t value )
434 : {
435 2798113 : ( (STnsData *) p )[index].nFilters = (int16_t) abs( value );
436 2798113 : return ( (STnsData *) p )[index].filter;
437 : }
438 :
439 : /* TNS enabled/disabled flag */
440 :
441 16688633 : void const *GetTnsEnabled( void const *p, const int16_t index, int16_t *pValue )
442 : {
443 16688633 : *pValue = ( (STnsData const *) p )[index].nFilters != 0 ? 1 : 0;
444 16688633 : return NULL;
445 : }
446 :
447 25628707 : void *SetTnsEnabled( void *p, const int16_t index, const int16_t value )
448 : {
449 : (void) p, (void) index, (void) value;
450 25628707 : return NULL;
451 : }
452 :
453 : /* TNS on whitened spectra flag */
454 :
455 886267 : void const *GetTnsOnWhite( void const *p, const int16_t index, int16_t *pValue )
456 : {
457 886267 : *pValue = ( (STnsData const *) p )[index].tnsOnWhitenedSpectra > 0 ? 1 : 0;
458 886267 : return NULL;
459 : }
460 :
461 2571174 : void *SetTnsOnWhite( void *p, const int16_t index, const int16_t value )
462 : {
463 2571174 : ( (STnsData *) p )[index].tnsOnWhitenedSpectra = value;
464 2571174 : return NULL;
465 : }
466 :
467 3150491 : void const *GetTnsEnabledSingleFilter( void const *p, const int16_t index, int16_t *pValue )
468 : {
469 3150491 : *pValue = ( (STnsData const *) p )[index].nFilters != 0 ? 1 : 0;
470 3150491 : return ( (STnsData const *) p )[index].filter;
471 : }
472 :
473 5285751 : void *SetTnsEnabledSingleFilter( void *p, const int16_t index, const int16_t value )
474 : {
475 5285751 : ( (STnsData *) p )[index].nFilters = value;
476 5285751 : return ( (STnsData *) p )[index].filter;
477 : }
478 :
479 :
480 : /*-------------------------------------------------------------------*
481 : * ResetTnsData()
482 : *
483 : *-------------------------------------------------------------------*/
484 :
485 50753582 : void ResetTnsData(
486 : STnsData *pTnsData )
487 : {
488 : uint16_t iFilter;
489 :
490 50753582 : pTnsData->nFilters = 0;
491 50753582 : pTnsData->tnsOnWhitenedSpectra = 0;
492 :
493 152260746 : for ( iFilter = 0; iFilter < sizeof( pTnsData->filter ) / sizeof( pTnsData->filter[0] ); iFilter++ )
494 : {
495 101507164 : STnsFilter *const pTnsFilter = &pTnsData->filter[iFilter];
496 101507164 : pTnsFilter->spectrumLength = 0;
497 101507164 : pTnsFilter->predictionGain = 1.0f;
498 101507164 : pTnsFilter->avgSqrCoef = 0;
499 101507164 : pTnsFilter->filterType = TNS_FILTER_OFF;
500 101507164 : ClearTnsFilterCoefficients( pTnsFilter );
501 : }
502 :
503 50753582 : return;
504 : }
505 :
506 :
507 : /*-------------------------------------------------------------------*
508 : * ClearTnsFilterCoefficients()
509 : *
510 : *-------------------------------------------------------------------*/
511 :
512 153814300 : void ClearTnsFilterCoefficients(
513 : STnsFilter *pTnsFilter )
514 : {
515 153814300 : pTnsFilter->order = 0;
516 153814300 : set_s( pTnsFilter->coefIndex, 0, TNS_MAX_FILTER_ORDER );
517 :
518 153814300 : 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 7744551 : static void Index2Parcor(
528 : const int16_t index[],
529 : float parCoeff[],
530 : const int16_t order )
531 : {
532 7744551 : const float *values = tnsCoeff4;
533 : int16_t i;
534 :
535 35100382 : for ( i = 0; i < order; i++ )
536 : {
537 27355831 : parCoeff[i] = values[index[i] + INDEX_SHIFT];
538 : }
539 :
540 7744551 : return;
541 : }
542 :
543 : /* Linear prediction analysis filter. */
544 548749499 : 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 548749499 : tmpSave = x;
554 2975947424 : for ( i = 0; i < order - 1; i++ )
555 : {
556 2427197925 : const float tmp = parCoeff[i] * x + state[i];
557 : /* Variable initialization */
558 2427197925 : x += parCoeff[i] * state[i];
559 2427197925 : state[i] = tmpSave;
560 2427197925 : tmpSave = tmp;
561 : }
562 :
563 : /* last stage: only need half operations */
564 548749499 : x += parCoeff[order - 1] * state[order - 1];
565 548749499 : state[order - 1] = tmpSave;
566 :
567 548749499 : return x;
568 : }
569 :
570 : /* Linear prediction synthesis filter. */
571 1647163650 : 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 1647163650 : x -= parCoeff[order - 1] * state[order - 1];
581 8902323791 : for ( i = order - 2; i >= 0; i-- )
582 : {
583 7255160141 : x -= parCoeff[i] * state[i];
584 7255160141 : state[i + 1] = parCoeff[i] * x + state[i];
585 : }
586 :
587 1647163650 : state[0] = x;
588 :
589 1647163650 : 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 7744551 : 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 7744551 : assert( ( order >= 0 ) && ( order <= TNS_MAX_FILTER_ORDER ) );
614 7744551 : assert( ( numOfLines > 0 ) || ( ( numOfLines == 0 ) && ( order == 0 ) ) );
615 :
616 7744551 : if ( order == 0 )
617 : {
618 2599906 : if ( ( spectrum != output ) && ( numOfLines > 0 ) )
619 : {
620 0 : mvr2r( spectrum, output, numOfLines );
621 : }
622 : }
623 : else
624 : {
625 2201057794 : for ( j = 0; j < numOfLines; j++ )
626 : {
627 2195913149 : output[j] = filter( order, parCoeff, state, spectrum[j] );
628 : }
629 : }
630 :
631 7744551 : return;
632 : }
633 :
634 :
635 20356155 : 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 20356155 : assert( ( order >= 0 ) && ( order <= ITF_MAX_FILTER_ORDER ) );
646 20356155 : assert( ( numOfLines > 0 ) || ( ( numOfLines == 0 ) && ( order == 0 ) ) );
647 :
648 20356155 : if ( order == 0 )
649 : {
650 17544906 : if ( ( spectrum != output ) && ( numOfLines > 0 ) )
651 : {
652 0 : mvr2r( spectrum, output, numOfLines );
653 : }
654 : }
655 : else
656 : {
657 2811249 : p = buf + ITF_MAX_FILTER_ORDER;
658 :
659 2811249 : set_f( buf, 0, ITF_MAX_FILTER_ORDER );
660 2811249 : mvr2r( spectrum, p, numOfLines );
661 :
662 962935695 : for ( j = 0; j < numOfLines; j++, p++ )
663 : {
664 960124446 : output[j] = p[0];
665 7680995568 : for ( i = 1; i < order; i++ )
666 : {
667 6720871122 : output[j] += A[i] * p[-i];
668 : }
669 : }
670 : }
671 :
672 20356155 : 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 4141464 : 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 4141464 : float *const pWorkBuffer = &workBuffer[order]; /* temp pointer */
703 :
704 37273176 : for ( i = 0; i < order; i++ )
705 : {
706 33131712 : workBuffer[i] = input[i];
707 33131712 : pWorkBuffer[i] = input[i + 1];
708 : }
709 :
710 37273176 : for ( i = 0; i < order; i++ )
711 : {
712 33131712 : if ( workBuffer[0] < 1.0f / 65536.0f )
713 : {
714 0 : tmp = 0;
715 : }
716 : else
717 : {
718 33131712 : tmp = -pWorkBuffer[i] / workBuffer[0];
719 : }
720 :
721 : /* compensate for calculation inaccuracies limit reflection coefs to ]-1,1[ */
722 33131712 : tmp = min( 0.999f, max( -0.999f, tmp ) );
723 :
724 33131712 : parCoeff[i] = tmp;
725 182224416 : for ( j = i; j < order; j++ )
726 : {
727 149092704 : tmp2 = pWorkBuffer[j] + tmp * workBuffer[j - i];
728 149092704 : workBuffer[j - i] += tmp * pWorkBuffer[j];
729 149092704 : pWorkBuffer[j] = tmp2;
730 : }
731 : }
732 :
733 : /* Convert ParCor / reflection coefficients to LPC */
734 4141464 : a[0] = 1.0f;
735 4141464 : a[1] = parCoeff[0];
736 :
737 33131712 : for ( i = 1; i < order; i++ )
738 : {
739 78687816 : for ( j = 0; j < i / 2; j++ )
740 : {
741 49697568 : tmp = a[j + 1];
742 49697568 : a[j + 1] += parCoeff[i] * a[i - 1 - j + 1];
743 49697568 : a[i - 1 - j + 1] += parCoeff[i] * tmp;
744 : }
745 28990248 : if ( i & 1 )
746 : {
747 16565856 : a[j + 1] += parCoeff[i] * a[j + 1];
748 : }
749 :
750 28990248 : a[i + 1] = parCoeff[i];
751 : }
752 :
753 4141464 : return ( ( input[0] + 1e-30f ) / ( workBuffer[0] + 1e-30f ) );
754 : }
755 :
756 :
757 : /*-------------------------------------------------------------------*
758 : * ITF_Apply()
759 : *
760 : *-------------------------------------------------------------------*/
761 :
762 20356155 : 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 20356155 : ITF_TnsFilter( &spectrum[startLine], stopLine - startLine, A, order, &spectrum[startLine] );
770 :
771 20356155 : return;
772 : }
773 :
774 :
775 : /*-------------------------------------------------------------------*
776 : * ITF_Detect()
777 : *
778 : *
779 : *-------------------------------------------------------------------*/
780 :
781 28252139 : 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 28252139 : nSubdivisions = MAX_SUBDIVISIONS;
799 28252139 : set_f( norms, 0.f, MAX_SUBDIVISIONS );
800 28252139 : set_f( rxx, 0.f, ITF_MAX_FILTER_ORDER + 1 ); /* This initialization is required */
801 :
802 28252139 : if ( maxOrder <= 0 )
803 : {
804 0 : return 0;
805 : }
806 :
807 : /* Calculate norms for each spectrum part */
808 113008556 : for ( iSubdivisions = 0; iSubdivisions < nSubdivisions; iSubdivisions++ )
809 : {
810 84756417 : iStartLine = startLine + ( stopLine - startLine ) * iSubdivisions / nSubdivisions;
811 84756417 : iEndLine = startLine + ( stopLine - startLine ) * ( iSubdivisions + 1 ) / nSubdivisions;
812 :
813 : /* Variable initialization */
814 84756417 : 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 28252139 : spectrumLength = stopLine - startLine;
819 :
820 : /* Variable initialization */
821 46357908 : for ( iSubdivisions = 0; ( iSubdivisions < nSubdivisions ) && ( norms[iSubdivisions] > HLM_MIN_NRG ); iSubdivisions++ )
822 : {
823 18105769 : fac = 1.0f / norms[iSubdivisions];
824 18105769 : iStartLine = startLine + spectrumLength * iSubdivisions / nSubdivisions;
825 18105769 : iEndLine = startLine + spectrumLength * ( iSubdivisions + 1 ) / nSubdivisions;
826 18105769 : pWindow = tnsAcfWindow;
827 :
828 : /* For additional loop condition */
829 : /* Variable initialization */
830 162951921 : for ( lag = 1; lag <= maxOrder; lag++ )
831 : {
832 144846152 : rxx[lag] += fac * ( *pWindow ) * dotp( pSpectrum + iStartLine - IGF_START_MN, pSpectrum + iStartLine - IGF_START_MN + lag, iEndLine - iStartLine - lag );
833 144846152 : pWindow++;
834 : }
835 : }
836 :
837 28252139 : *predictionGain = 0;
838 28252139 : if ( iSubdivisions == nSubdivisions ) /* meaning there is no subdivision with low energy */
839 : {
840 4141464 : rxx[0] = (float) nSubdivisions;
841 :
842 : /* Limit the maximum order to spectrum length/4 */
843 4141464 : *predictionGain = ITF_AutoToLPcoef( rxx, A, min( maxOrder, spectrumLength / 4 ) );
844 :
845 4141464 : *curr_order = maxOrder;
846 : }
847 :
848 28252139 : return 1;
849 : }
|