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 246504 : 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 246504 : int16_t *startLineFilter = &pTnsConfig->iFilterBorders[1];
86 :
87 246504 : nSampleRate = inner_frame_tbl[bwidth] * FRAMES_PER_SEC;
88 :
89 : /* Sanity checks */
90 246504 : assert( ( nSampleRate > 0 ) && ( frameLength > 0 ) && ( pTnsConfig != NULL ) );
91 246504 : 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 246504 : pTnsConfig->maxOrder = TNS_MAX_FILTER_ORDER;
99 246504 : 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 205224 : if ( nSampleRate > 32000 && nSampleRate == 100 * frameLength )
108 : {
109 49240 : pTnsConfig->nMaxFilters = sizeof( tnsParameters48kHz_grouped ) / sizeof( tnsParameters48kHz_grouped[0] );
110 49240 : pTnsConfig->pTnsParameters = tnsParameters48kHz_grouped;
111 : }
112 155984 : else if ( nSampleRate > 16000 )
113 : {
114 144230 : if ( ( element_mode > IVAS_SCE ) && ( total_brate >= ( MCT_flag ? IVAS_32k : IVAS_48k ) ) )
115 : {
116 122822 : pTnsConfig->nMaxFilters = sizeof( tnsParameters32kHz_Stereo ) / sizeof( tnsParameters32kHz_Stereo[0] );
117 122822 : if ( nSampleRate == 100 * frameLength ) /* sub-frame length is <= 10 ms */
118 : {
119 14158 : pTnsConfig->pTnsParameters = tnsParameters32kHz_grouped;
120 : }
121 : else
122 : {
123 108664 : 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 246504 : 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 690396 : for ( iFilter = 0; iFilter < pTnsConfig->nMaxFilters; iFilter++ )
158 : {
159 443892 : assert( pTnsConfig->pTnsParameters[iFilter].startLineFrequency < 0.5f * nSampleRate );
160 443892 : startLineFilter[iFilter] = (int16_t) ( ( frameLength * 2 * pTnsConfig->pTnsParameters[iFilter].startLineFrequency ) / nSampleRate );
161 : }
162 246504 : if ( igfStopFreq > 0 )
163 : {
164 189087 : pTnsConfig->iFilterBorders[0] = (int16_t) ( ( frameLength * 2 * igfStopFreq ) / nSampleRate );
165 : }
166 : else
167 : {
168 57417 : pTnsConfig->iFilterBorders[0] = frameLength;
169 : }
170 :
171 246504 : pTnsConfig->allowTnsOnWhite = 0;
172 :
173 246504 : return /*TNS_NO_ERROR*/;
174 : }
175 :
176 :
177 : /*-------------------------------------------------------------------*
178 : * ApplyTnsFilter()
179 : *
180 : *-------------------------------------------------------------------*/
181 :
182 375229 : 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 375229 : filter = fIsAnalysis ? FIRLattice : IIRLattice;
195 375229 : set_f( state, 0, TNS_MAX_FILTER_ORDER );
196 375229 : pBorders = pTnsConfig->iFilterBorders;
197 :
198 1119826 : for ( iFilter = pTnsConfig->nMaxFilters - 1; iFilter >= 0; iFilter-- )
199 : {
200 : float parCoeff[TNS_MAX_FILTER_ORDER];
201 744597 : const STnsFilter *pFilter = &pTnsData->filter[iFilter];
202 :
203 744597 : set_f( parCoeff, 0, TNS_MAX_FILTER_ORDER );
204 744597 : stopLine = pBorders[iFilter];
205 744597 : startLine = pBorders[iFilter + 1];
206 :
207 744597 : Index2Parcor( pFilter->coefIndex, parCoeff, pFilter->order );
208 :
209 744597 : TnsFilter( &spectrum[startLine], stopLine - startLine, parCoeff, pFilter->order, filter, state, &spectrum[startLine] );
210 : }
211 :
212 375229 : 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 1492168 : static int16_t GetBitsFromTable( int16_t value, const Coding codes[], const int16_t nSize )
226 : {
227 : (void) nSize;
228 1492168 : assert( ( value >= 0 ) && ( value < nSize ) && ( nSize >= 0 ) && ( nSize <= 256 ) );
229 1492168 : 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 737155 : static int16_t EncodeUsingTable( int16_t value, const Coding codes[], const int16_t nSize )
236 : {
237 : (void) nSize;
238 737155 : assert( ( value >= 0 ) && ( value < nSize ) && ( nSize >= 0 ) && ( nSize <= 256 ) );
239 737155 : return codes[value].code;
240 : }
241 :
242 : /** Decode a value from a bitstream using a Huffman table. */
243 2183310 : static int16_t DecodeUsingTable( Decoder_State *st, int16_t *pValue, const Coding codes[], const int16_t nSize )
244 : {
245 2183310 : uint16_t code = 0;
246 2183310 : uint8_t nBits = 0;
247 2183310 : int16_t valueIndex = nSize;
248 :
249 2183310 : assert( ( nSize >= 0 ) && ( nSize <= 256 ) );
250 : /* Variable initialization. All are required! */
251 7859973 : while ( valueIndex == nSize )
252 : {
253 5676663 : code = ( code << 1 ) + get_next_indice( st, 1 );
254 5676663 : ++nBits;
255 5676663 : if ( nBits > nSize || nBits > 16 )
256 : {
257 0 : st->BER_detect = 1;
258 0 : *pValue = 0;
259 0 : return -1;
260 : }
261 73001127 : for ( valueIndex = 0; valueIndex < nSize; valueIndex++ )
262 : {
263 69507774 : if ( codes[valueIndex].nBits == nBits )
264 : {
265 6457476 : if ( codes[valueIndex].code == code )
266 : {
267 2183310 : break;
268 : }
269 : }
270 : }
271 : /* Loop condition */
272 : }
273 :
274 2183310 : if ( valueIndex < nSize )
275 : {
276 2183310 : *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 2183310 : return nBits;
286 : }
287 :
288 : /* TNS filter coefficients */
289 :
290 638626 : void const *GetTnsFilterCoeff( void const *p, const int16_t index, int16_t *pValue )
291 : {
292 638626 : *pValue = ( (int16_t const *) p )[index] + INDEX_SHIFT;
293 638626 : return NULL;
294 : }
295 :
296 1891662 : void *SetTnsFilterCoeff( void *p, const int16_t index, const int16_t value )
297 : {
298 1891662 : ( (int16_t *) p )[index] = value - INDEX_SHIFT;
299 1891662 : return NULL;
300 : }
301 :
302 963099 : int16_t GetSWBTCX20TnsFilterCoeffBits( const int16_t value, const int16_t index )
303 : {
304 963099 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
305 963099 : return GetBitsFromTable( value, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
306 : }
307 :
308 475847 : int16_t EncodeSWBTCX20TnsFilterCoeff( const int16_t value, const int16_t index )
309 : {
310 475847 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
311 475847 : return EncodeUsingTable( value, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
312 : }
313 :
314 1411413 : int16_t DecodeSWBTCX20TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
315 : {
316 1411413 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
317 1411413 : return DecodeUsingTable( st, pValue, codesTnsCoeffSWBTCX20[index], nTnsCoeffCodes );
318 : }
319 :
320 282309 : int16_t GetSWBTCX10TnsFilterCoeffBits( const int16_t value, const int16_t index )
321 : {
322 282309 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
323 282309 : return GetBitsFromTable( value, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
324 : }
325 :
326 139566 : int16_t EncodeSWBTCX10TnsFilterCoeff( const int16_t value, const int16_t index )
327 : {
328 139566 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
329 139566 : return EncodeUsingTable( value, codesTnsCoeffSWBTCX10[index], nTnsCoeffCodes );
330 : }
331 :
332 412056 : int16_t DecodeSWBTCX10TnsFilterCoeff( Decoder_State *st, const int16_t index, int16_t *pValue )
333 : {
334 412056 : assert( ( index >= 0 ) && ( index < nTnsCoeffTables ) );
335 412056 : 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 116387 : void const *GetTnsFilterOrder( void const *p, const int16_t index, int16_t *pValue )
360 : {
361 116387 : *pValue = ( (STnsFilter const *) p )[index].order;
362 116387 : return ( (STnsFilter const *) p )[index].coefIndex;
363 : }
364 :
365 344424 : void *SetTnsFilterOrder( void *p, const int16_t index, const int16_t value )
366 : {
367 344424 : ( (STnsFilter *) p )[index].order = value;
368 344424 : return ( (STnsFilter *) p )[index].coefIndex;
369 : }
370 :
371 167489 : int16_t GetTnsFilterOrderBitsSWBTCX20( const int16_t value, const int16_t index )
372 : {
373 : (void) index;
374 167489 : return GetBitsFromTable( value - 1, codesTnsOrderTCX20, nTnsOrderCodes );
375 : }
376 :
377 82686 : int16_t EncodeTnsFilterOrderSWBTCX20( const int16_t value, const int16_t index )
378 : {
379 : (void) index;
380 82686 : return EncodeUsingTable( value - 1, codesTnsOrderTCX20, nTnsOrderCodes );
381 : }
382 :
383 245121 : int16_t DecodeTnsFilterOrderSWBTCX20( Decoder_State *st, const int16_t index, int16_t *pValue )
384 : {
385 : (void) index;
386 245121 : return DecodeUsingTable( st, pValue, codesTnsOrderTCX20, nTnsOrderCodes );
387 : }
388 :
389 59529 : int16_t GetTnsFilterOrderBitsSWBTCX10( const int16_t value, const int16_t index )
390 : {
391 : (void) index;
392 59529 : return GetBitsFromTable( value - 1, codesTnsOrderTCX10, nTnsOrderCodes );
393 : }
394 :
395 29368 : int16_t EncodeTnsFilterOrderSWBTCX10( const int16_t value, const int16_t index )
396 : {
397 : (void) index;
398 29368 : return EncodeUsingTable( value - 1, codesTnsOrderTCX10, nTnsOrderCodes );
399 : }
400 :
401 86619 : int16_t DecodeTnsFilterOrderSWBTCX10( Decoder_State *st, const int16_t index, int16_t *pValue )
402 : {
403 : (void) index;
404 86619 : 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 89027 : void const *GetNumOfTnsFilters( void const *p, const int16_t index, int16_t *pValue )
428 : {
429 89027 : *pValue = (int16_t) abs( ( (STnsData const *) p )[index].nFilters );
430 89027 : return ( (STnsData const *) p )[index].filter;
431 : }
432 :
433 263859 : void *SetNumOfTnsFilters( void *p, const int16_t index, const int16_t value )
434 : {
435 263859 : ( (STnsData *) p )[index].nFilters = (int16_t) abs( value );
436 263859 : return ( (STnsData *) p )[index].filter;
437 : }
438 :
439 : /* TNS enabled/disabled flag */
440 :
441 1287771 : void const *GetTnsEnabled( void const *p, const int16_t index, int16_t *pValue )
442 : {
443 1287771 : *pValue = ( (STnsData const *) p )[index].nFilters != 0 ? 1 : 0;
444 1287771 : return NULL;
445 : }
446 :
447 2092443 : void *SetTnsEnabled( void *p, const int16_t index, const int16_t value )
448 : {
449 : (void) p, (void) index, (void) value;
450 2092443 : return NULL;
451 : }
452 :
453 : /* TNS on whitened spectra flag */
454 :
455 71708 : void const *GetTnsOnWhite( void const *p, const int16_t index, int16_t *pValue )
456 : {
457 71708 : *pValue = ( (STnsData const *) p )[index].tnsOnWhitenedSpectra > 0 ? 1 : 0;
458 71708 : return NULL;
459 : }
460 :
461 212763 : void *SetTnsOnWhite( void *p, const int16_t index, const int16_t value )
462 : {
463 212763 : ( (STnsData *) p )[index].tnsOnWhitenedSpectra = value;
464 212763 : 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 3852888 : void ResetTnsData(
486 : STnsData *pTnsData )
487 : {
488 : uint16_t iFilter;
489 :
490 3852888 : pTnsData->nFilters = 0;
491 3852888 : pTnsData->tnsOnWhitenedSpectra = 0;
492 :
493 11558664 : for ( iFilter = 0; iFilter < sizeof( pTnsData->filter ) / sizeof( pTnsData->filter[0] ); iFilter++ )
494 : {
495 7705776 : STnsFilter *const pTnsFilter = &pTnsData->filter[iFilter];
496 7705776 : pTnsFilter->spectrumLength = 0;
497 7705776 : pTnsFilter->predictionGain = 1.0f;
498 7705776 : pTnsFilter->avgSqrCoef = 0;
499 7705776 : pTnsFilter->filterType = TNS_FILTER_OFF;
500 7705776 : ClearTnsFilterCoefficients( pTnsFilter );
501 : }
502 :
503 3852888 : return;
504 : }
505 :
506 :
507 : /*-------------------------------------------------------------------*
508 : * ClearTnsFilterCoefficients()
509 : *
510 : *-------------------------------------------------------------------*/
511 :
512 11562184 : void ClearTnsFilterCoefficients(
513 : STnsFilter *pTnsFilter )
514 : {
515 11562184 : pTnsFilter->order = 0;
516 11562184 : set_s( pTnsFilter->coefIndex, 0, TNS_MAX_FILTER_ORDER );
517 :
518 11562184 : 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 744597 : static void Index2Parcor(
528 : const int16_t index[],
529 : float parCoeff[],
530 : const int16_t order )
531 : {
532 744597 : const float *values = tnsCoeff4;
533 : int16_t i;
534 :
535 3404069 : for ( i = 0; i < order; i++ )
536 : {
537 2659472 : parCoeff[i] = values[index[i] + INDEX_SHIFT];
538 : }
539 :
540 744597 : return;
541 : }
542 :
543 : /* Linear prediction analysis filter. */
544 51515571 : 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 51515571 : tmpSave = x;
554 286044830 : for ( i = 0; i < order - 1; i++ )
555 : {
556 234529259 : const float tmp = parCoeff[i] * x + state[i];
557 : /* Variable initialization */
558 234529259 : x += parCoeff[i] * state[i];
559 234529259 : state[i] = tmpSave;
560 234529259 : tmpSave = tmp;
561 : }
562 :
563 : /* last stage: only need half operations */
564 51515571 : x += parCoeff[order - 1] * state[order - 1];
565 51515571 : state[order - 1] = tmpSave;
566 :
567 51515571 : return x;
568 : }
569 :
570 : /* Linear prediction synthesis filter. */
571 163518769 : 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 163518769 : x -= parCoeff[order - 1] * state[order - 1];
581 903213149 : for ( i = order - 2; i >= 0; i-- )
582 : {
583 739694380 : x -= parCoeff[i] * state[i];
584 739694380 : state[i + 1] = parCoeff[i] * x + state[i];
585 : }
586 :
587 163518769 : state[0] = x;
588 :
589 163518769 : 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 744597 : 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 744597 : assert( ( order >= 0 ) && ( order <= TNS_MAX_FILTER_ORDER ) );
614 744597 : assert( ( numOfLines > 0 ) || ( ( numOfLines == 0 ) && ( order == 0 ) ) );
615 :
616 744597 : if ( order == 0 )
617 : {
618 259492 : if ( ( spectrum != output ) && ( numOfLines > 0 ) )
619 : {
620 0 : mvr2r( spectrum, output, numOfLines );
621 : }
622 : }
623 : else
624 : {
625 215519445 : for ( j = 0; j < numOfLines; j++ )
626 : {
627 215034340 : output[j] = filter( order, parCoeff, state, spectrum[j] );
628 : }
629 : }
630 :
631 744597 : return;
632 : }
633 :
634 :
635 1565487 : 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 1565487 : assert( ( order >= 0 ) && ( order <= ITF_MAX_FILTER_ORDER ) );
646 1565487 : assert( ( numOfLines > 0 ) || ( ( numOfLines == 0 ) && ( order == 0 ) ) );
647 :
648 1565487 : if ( order == 0 )
649 : {
650 1339998 : if ( ( spectrum != output ) && ( numOfLines > 0 ) )
651 : {
652 0 : mvr2r( spectrum, output, numOfLines );
653 : }
654 : }
655 : else
656 : {
657 225489 : p = buf + ITF_MAX_FILTER_ORDER;
658 :
659 225489 : set_f( buf, 0, ITF_MAX_FILTER_ORDER );
660 225489 : mvr2r( spectrum, p, numOfLines );
661 :
662 74009577 : for ( j = 0; j < numOfLines; j++, p++ )
663 : {
664 73784088 : output[j] = p[0];
665 590272704 : for ( i = 1; i < order; i++ )
666 : {
667 516488616 : output[j] += A[i] * p[-i];
668 : }
669 : }
670 : }
671 :
672 1565487 : 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 328962 : 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 328962 : float *const pWorkBuffer = &workBuffer[order]; /* temp pointer */
703 :
704 2960658 : for ( i = 0; i < order; i++ )
705 : {
706 2631696 : workBuffer[i] = input[i];
707 2631696 : pWorkBuffer[i] = input[i + 1];
708 : }
709 :
710 2960658 : for ( i = 0; i < order; i++ )
711 : {
712 2631696 : if ( workBuffer[0] < 1.0f / 65536.0f )
713 : {
714 0 : tmp = 0;
715 : }
716 : else
717 : {
718 2631696 : tmp = -pWorkBuffer[i] / workBuffer[0];
719 : }
720 :
721 : /* compensate for calculation inaccuracies limit reflection coefs to ]-1,1[ */
722 2631696 : tmp = min( 0.999f, max( -0.999f, tmp ) );
723 :
724 2631696 : parCoeff[i] = tmp;
725 14474328 : for ( j = i; j < order; j++ )
726 : {
727 11842632 : tmp2 = pWorkBuffer[j] + tmp * workBuffer[j - i];
728 11842632 : workBuffer[j - i] += tmp * pWorkBuffer[j];
729 11842632 : pWorkBuffer[j] = tmp2;
730 : }
731 : }
732 :
733 : /* Convert ParCor / reflection coefficients to LPC */
734 328962 : a[0] = 1.0f;
735 328962 : a[1] = parCoeff[0];
736 :
737 2631696 : for ( i = 1; i < order; i++ )
738 : {
739 6250278 : for ( j = 0; j < i / 2; j++ )
740 : {
741 3947544 : tmp = a[j + 1];
742 3947544 : a[j + 1] += parCoeff[i] * a[i - 1 - j + 1];
743 3947544 : a[i - 1 - j + 1] += parCoeff[i] * tmp;
744 : }
745 2302734 : if ( i & 1 )
746 : {
747 1315848 : a[j + 1] += parCoeff[i] * a[j + 1];
748 : }
749 :
750 2302734 : a[i + 1] = parCoeff[i];
751 : }
752 :
753 328962 : return ( ( input[0] + 1e-30f ) / ( workBuffer[0] + 1e-30f ) );
754 : }
755 :
756 :
757 : /*-------------------------------------------------------------------*
758 : * ITF_Apply()
759 : *
760 : *-------------------------------------------------------------------*/
761 :
762 1565487 : 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 1565487 : ITF_TnsFilter( &spectrum[startLine], stopLine - startLine, A, order, &spectrum[startLine] );
770 :
771 1565487 : return;
772 : }
773 :
774 :
775 : /*-------------------------------------------------------------------*
776 : * ITF_Detect()
777 : *
778 : *
779 : *-------------------------------------------------------------------*/
780 :
781 2177388 : 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 2177388 : nSubdivisions = MAX_SUBDIVISIONS;
799 2177388 : set_f( norms, 0.f, MAX_SUBDIVISIONS );
800 2177388 : set_f( rxx, 0.f, ITF_MAX_FILTER_ORDER + 1 ); /* This initialization is required */
801 :
802 2177388 : if ( maxOrder <= 0 )
803 : {
804 0 : return 0;
805 : }
806 :
807 : /* Calculate norms for each spectrum part */
808 8709552 : for ( iSubdivisions = 0; iSubdivisions < nSubdivisions; iSubdivisions++ )
809 : {
810 6532164 : iStartLine = startLine + ( stopLine - startLine ) * iSubdivisions / nSubdivisions;
811 6532164 : iEndLine = startLine + ( stopLine - startLine ) * ( iSubdivisions + 1 ) / nSubdivisions;
812 :
813 : /* Variable initialization */
814 6532164 : 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 2177388 : spectrumLength = stopLine - startLine;
819 :
820 : /* Variable initialization */
821 3494495 : for ( iSubdivisions = 0; ( iSubdivisions < nSubdivisions ) && ( norms[iSubdivisions] > HLM_MIN_NRG ); iSubdivisions++ )
822 : {
823 1317107 : fac = 1.0f / norms[iSubdivisions];
824 1317107 : iStartLine = startLine + spectrumLength * iSubdivisions / nSubdivisions;
825 1317107 : iEndLine = startLine + spectrumLength * ( iSubdivisions + 1 ) / nSubdivisions;
826 1317107 : pWindow = tnsAcfWindow;
827 :
828 : /* For additional loop condition */
829 : /* Variable initialization */
830 11853963 : for ( lag = 1; lag <= maxOrder; lag++ )
831 : {
832 10536856 : rxx[lag] += fac * ( *pWindow ) * dotp( pSpectrum + iStartLine - IGF_START_MN, pSpectrum + iStartLine - IGF_START_MN + lag, iEndLine - iStartLine - lag );
833 10536856 : pWindow++;
834 : }
835 : }
836 :
837 2177388 : *predictionGain = 0;
838 2177388 : if ( iSubdivisions == nSubdivisions ) /* meaning there is no subdivision with low energy */
839 : {
840 328962 : rxx[0] = (float) nSubdivisions;
841 :
842 : /* Limit the maximum order to spectrum length/4 */
843 328962 : *predictionGain = ITF_AutoToLPcoef( rxx, A, min( maxOrder, spectrumLength / 4 ) );
844 :
845 328962 : *curr_order = maxOrder;
846 : }
847 :
848 2177388 : return 1;
849 : }
|