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 : #include <stdint.h>
34 : #include "options.h"
35 : #include "isar_lcld_prot.h"
36 : #include "isar_rom_lcld_tables.h"
37 : #include "prot.h"
38 : #include <assert.h>
39 : #include "isar_prot.h"
40 : #include "wmc_auto.h"
41 :
42 :
43 : /*------------------------------------------------------------------------------------------*
44 : * Local constants
45 : *------------------------------------------------------------------------------------------*/
46 :
47 : #define HUFF_READ_SIZE ( 4 )
48 :
49 : /*------------------------------------------------------------------------------------------*
50 : * Local structures
51 : *------------------------------------------------------------------------------------------*/
52 :
53 : typedef struct TableNode
54 : {
55 : struct TableNode **ppoNextTable;
56 : struct TableNode *poOrderedNext;
57 :
58 : int32_t *piCodeIndex;
59 : int32_t *piDifference;
60 : int32_t *piLength;
61 : } TableNode;
62 :
63 : typedef struct TableList
64 : {
65 : TableNode *poOrderedTop;
66 : TableNode *poOrderedBottom;
67 :
68 : } TableList;
69 :
70 : struct LCLD_DECODER
71 : {
72 : int32_t iSampleRate;
73 : int32_t iChannels;
74 : int32_t iNumBlocks;
75 :
76 : int32_t iNumBands;
77 : const int32_t *piBandwidths;
78 :
79 : int32_t iMSMode;
80 : int32_t *piMSFlags;
81 : TableList *ptable_list;
82 : uint32_t ( *c_apauiHuffDecTable_RAM[2 * ALLOC_TABLE_SIZE] )[HUFF_DEC_TABLE_SIZE];
83 : uint32_t num_decode_table[2 * ALLOC_TABLE_SIZE];
84 : int32_t piMSPredCoefs[MAX_BANDS];
85 : int32_t piLRPhaseDiffs[MAX_BANDS];
86 : int32_t iCommonGrouping;
87 : int32_t *piNumGroups;
88 : int32_t **ppiGroupLengths;
89 :
90 : int32_t ***pppiRMSEnvelope;
91 : int32_t ***pppiSMR;
92 : int32_t ***pppiExcitation;
93 : int32_t ***pppiAlloc;
94 :
95 : int32_t iAllocOffset;
96 : int32_t iRealOnlyOut;
97 :
98 : int32_t ***pppiLCLDSignReal;
99 : int32_t ***pppiLCLDSignImag;
100 : int32_t ***pppiQLCLDReal;
101 : int32_t ***pppiQLCLDImag;
102 :
103 : PredictionDecoder *psPredictionDecoder;
104 :
105 : NoiseGen *psNoiseGen;
106 : };
107 :
108 :
109 : /*------------------------------------------------------------------------------------------*
110 : * Local functions declarations
111 : *------------------------------------------------------------------------------------------*/
112 :
113 : static void CreateDecodeTable( LCLDDecoder *psLCLDDecoder, const int32_t num, const uint16_t ( *ppuiEncTable )[2], const int32_t iSize, const int32_t iReadLength, uint32_t *iTables );
114 :
115 : static TableNode *CreateTableList( const int32_t iReadLength );
116 :
117 : static void DeleteTableList( TableList *ptable_list, int32_t iTables );
118 :
119 : static TableNode *GetNextTable( const int32_t iIndex, TableList *table_list, TableNode *poParent, const int32_t iReadLength, uint32_t *iTablesCreated );
120 :
121 : static void AddcodeTableList( TableList *ptable_list, const int32_t iLength, const int32_t iCode, const int32_t iCodeIndex, const int32_t iReadLength, uint32_t *iTables );
122 :
123 : static void CompleteTables( LCLDDecoder *psLCLDDecoder, const int32_t n, TableList *ptable_list, const int32_t iReadLength, const int32_t iTablesCreated );
124 :
125 :
126 : /*------------------------------------------------------------------------------------------*
127 : * Local functions
128 : *------------------------------------------------------------------------------------------*/
129 :
130 1203168 : static TableNode *CreateTableList(
131 : const int32_t iReadLength )
132 : {
133 : int32_t n;
134 : int32_t iMaxTables;
135 : TableNode *ptable_top;
136 :
137 1203168 : iMaxTables = 1 << iReadLength;
138 1203168 : ptable_top = (TableNode *) malloc( sizeof( TableNode ) );
139 1203168 : ptable_top->ppoNextTable = (TableNode **) malloc( iMaxTables * sizeof( TableNode * ) );
140 1203168 : ptable_top->piCodeIndex = (int32_t *) malloc( iMaxTables * sizeof( int32_t ) );
141 1203168 : ptable_top->piDifference = (int32_t *) malloc( iMaxTables * sizeof( int32_t ) );
142 1203168 : ptable_top->piLength = (int32_t *) malloc( iMaxTables * sizeof( int32_t ) );
143 20453856 : for ( n = 0; n < iMaxTables; n++ )
144 : {
145 19250688 : ptable_top->ppoNextTable[n] = NULL;
146 19250688 : ptable_top->piCodeIndex[n] = 0xffff;
147 19250688 : ptable_top->piDifference[n] = 0;
148 19250688 : ptable_top->piLength[n] = 0;
149 : }
150 :
151 1203168 : return ptable_top;
152 : }
153 :
154 :
155 41168 : static void DeleteTableList(
156 : TableList *ptable_list,
157 : int32_t iTables )
158 : {
159 :
160 : TableNode *node;
161 41168 : node = ptable_list->poOrderedTop;
162 :
163 1244336 : while ( iTables )
164 : {
165 1203168 : TableNode *node1 = node;
166 1203168 : node = node1->poOrderedNext;
167 1203168 : if ( node1->piCodeIndex != NULL )
168 : {
169 1203168 : free( node1->piCodeIndex );
170 : }
171 1203168 : if ( node1->piLength != NULL )
172 : {
173 1203168 : free( node1->piLength );
174 : }
175 1203168 : if ( node1->piDifference != NULL )
176 : {
177 1203168 : free( node1->piDifference );
178 : }
179 1203168 : if ( node1->ppoNextTable != NULL )
180 : {
181 1203168 : free( node1->ppoNextTable );
182 : }
183 1203168 : if ( node1 != NULL )
184 : {
185 1203168 : free( node1 );
186 : }
187 1203168 : iTables--;
188 : }
189 :
190 41168 : if ( ptable_list != NULL )
191 : {
192 41168 : free( ptable_list );
193 : }
194 :
195 41168 : return;
196 : }
197 :
198 :
199 21440560 : static TableNode *GetNextTable(
200 : const int32_t iIndex,
201 : TableList *table_list,
202 : TableNode *poParent,
203 : const int32_t iReadLength,
204 : uint32_t *iTablesCreated )
205 : {
206 : TableNode *poNextNode;
207 :
208 21440560 : if ( poParent->ppoNextTable[iIndex] == NULL )
209 : {
210 1162000 : poNextNode = CreateTableList( iReadLength );
211 1162000 : poParent->ppoNextTable[iIndex] = poNextNode;
212 1162000 : poParent->piDifference[iIndex] = *iTablesCreated; /* this is a link to the next table rather than the difference */
213 1162000 : table_list->poOrderedBottom->poOrderedNext = poNextNode;
214 1162000 : table_list->poOrderedBottom = poNextNode;
215 :
216 1162000 : ( *iTablesCreated )++;
217 : }
218 : else
219 : {
220 20278560 : poNextNode = poParent->ppoNextTable[iIndex];
221 : }
222 :
223 21440560 : return poNextNode;
224 : }
225 :
226 :
227 41168 : static void CompleteTables(
228 : LCLDDecoder *psLCLDDecoder,
229 : const int32_t n,
230 : TableList *ptable_list,
231 : const int32_t iReadLength,
232 : const int32_t iTablesCreated )
233 : {
234 : int32_t iMaxTables;
235 : int32_t j;
236 : TableNode *poNode;
237 :
238 41168 : iMaxTables = 1 << iReadLength;
239 41168 : psLCLDDecoder->c_apauiHuffDecTable_RAM[n] = malloc( iTablesCreated * iMaxTables * sizeof( uint32_t ) );
240 :
241 41168 : poNode = ptable_list->poOrderedTop;
242 1244336 : for ( j = 0; j < iTablesCreated; j++ )
243 : {
244 : int32_t k;
245 1203168 : if ( poNode != NULL )
246 : {
247 20453856 : for ( k = 0; k < iMaxTables; k++ )
248 : {
249 : uint32_t uiCode;
250 19250688 : uiCode = poNode->piDifference[k];
251 19250688 : uiCode <<= 16;
252 19250688 : uiCode |= poNode->piCodeIndex[k];
253 19250688 : psLCLDDecoder->c_apauiHuffDecTable_RAM[n][j][k] = uiCode;
254 : }
255 : }
256 1203168 : poNode = poNode->poOrderedNext;
257 : }
258 :
259 41168 : return;
260 : }
261 :
262 :
263 6515168 : static void AddcodeTableList(
264 : TableList *ptable_list,
265 : const int32_t iLength,
266 : const int32_t iCode,
267 : const int32_t iCodeIndex,
268 : const int32_t iReadLength,
269 : uint32_t *iTables )
270 : {
271 : int32_t iDifference;
272 : int32_t iMask;
273 : int32_t iCurrentLength;
274 : int32_t iIndex;
275 : int32_t iCodeLow;
276 : int32_t iCodeHigh;
277 : TableNode *poNode;
278 :
279 6515168 : poNode = ptable_list->poOrderedTop;
280 6515168 : iMask = ( 1 << iReadLength ) - 1;
281 6515168 : iCurrentLength = iLength;
282 27955728 : while ( iCurrentLength > iReadLength )
283 : {
284 21440560 : iDifference = iCurrentLength - iReadLength;
285 21440560 : iIndex = iCode >> iDifference;
286 21440560 : iIndex &= iMask;
287 21440560 : poNode = GetNextTable( iIndex, ptable_list, poNode, iReadLength, iTables );
288 21440560 : iCurrentLength -= iReadLength;
289 : }
290 :
291 6515168 : iMask = ( 1 << iCurrentLength ) - 1;
292 6515168 : iDifference = iReadLength - iCurrentLength;
293 6515168 : iCodeLow = ( iCode & iMask ) << iDifference;
294 6515168 : iMask = ( 1 << iDifference ) - 1;
295 6515168 : iCodeHigh = iCodeLow | iMask;
296 24603856 : for ( iIndex = iCodeLow; iIndex <= iCodeHigh; iIndex++ )
297 : {
298 18088688 : poNode->piCodeIndex[iIndex] = iCodeIndex;
299 18088688 : poNode->piDifference[iIndex] = iDifference;
300 18088688 : poNode->piLength[iIndex] = iLength;
301 : }
302 :
303 6515168 : return;
304 : }
305 :
306 :
307 41168 : static void CreateDecodeTable(
308 : LCLDDecoder *psLCLDDecoder,
309 : const int32_t num,
310 : const uint16_t ( *ppuiEncTable )[2],
311 : const int32_t iSize,
312 : const int32_t iReadLength,
313 : uint32_t *iTables )
314 : {
315 : int32_t n;
316 : uint32_t **ppsort_enc_table;
317 : TableList *ptable_list;
318 :
319 41168 : ptable_list = (TableList *) malloc( sizeof( TableList ) );
320 :
321 41168 : ppsort_enc_table = (uint32_t **) malloc( iSize * sizeof( int32_t * ) );
322 :
323 6556336 : for ( n = 0; n < iSize; n++ )
324 : {
325 6515168 : ppsort_enc_table[n] = (uint32_t *) malloc( 3 * sizeof( int32_t ) );
326 6515168 : ppsort_enc_table[n][0] = (uint32_t) ppuiEncTable[n][0];
327 6515168 : ppsort_enc_table[n][1] = (uint32_t) ppuiEncTable[n][1];
328 6515168 : ppsort_enc_table[n][2] = (uint32_t) n;
329 : }
330 :
331 6556336 : for ( n = 0; n < iSize; n++ )
332 : {
333 : uint32_t iMin;
334 : int32_t iMinIndex;
335 : int32_t k;
336 :
337 6515168 : iMin = ppsort_enc_table[n][0];
338 6515168 : iMinIndex = n;
339 1305054816 : for ( k = n; k < iSize; k++ )
340 : {
341 1298539648 : if ( ppsort_enc_table[k][0] < iMin )
342 : {
343 3127440 : iMin = ppsort_enc_table[k][0];
344 3127440 : iMinIndex = k;
345 : }
346 : }
347 :
348 6515168 : if ( iMinIndex != n )
349 : {
350 : uint32_t uiLength;
351 : uint32_t uiCode;
352 : uint32_t uiCodeIndex;
353 :
354 1662656 : uiLength = ppsort_enc_table[n][0];
355 1662656 : uiCode = ppsort_enc_table[n][1];
356 1662656 : uiCodeIndex = ppsort_enc_table[n][2];
357 :
358 1662656 : ppsort_enc_table[n][0] = ppsort_enc_table[iMinIndex][0];
359 1662656 : ppsort_enc_table[n][1] = ppsort_enc_table[iMinIndex][1];
360 1662656 : ppsort_enc_table[n][2] = ppsort_enc_table[iMinIndex][2];
361 :
362 1662656 : ppsort_enc_table[iMinIndex][0] = uiLength;
363 1662656 : ppsort_enc_table[iMinIndex][1] = uiCode;
364 1662656 : ppsort_enc_table[iMinIndex][2] = uiCodeIndex;
365 : }
366 : }
367 41168 : ptable_list->poOrderedTop = CreateTableList( iReadLength );
368 41168 : ptable_list->poOrderedBottom = ptable_list->poOrderedTop;
369 6556336 : for ( n = 0; n < iSize; n++ )
370 : {
371 : int32_t iLength;
372 : int32_t iCode;
373 : int32_t iCodeIndex;
374 :
375 6515168 : iLength = ppsort_enc_table[n][0];
376 6515168 : iCode = ppsort_enc_table[n][1];
377 6515168 : iCodeIndex = ppsort_enc_table[n][2];
378 6515168 : AddcodeTableList( ptable_list, iLength, iCode, iCodeIndex, iReadLength, iTables );
379 : }
380 :
381 41168 : CompleteTables( psLCLDDecoder, num, ptable_list, iReadLength, *iTables );
382 41168 : DeleteTableList( ptable_list, *iTables );
383 6556336 : for ( n = 0; n < iSize; n++ )
384 : {
385 6515168 : free( ppsort_enc_table[n] );
386 : }
387 :
388 41168 : free( ppsort_enc_table );
389 :
390 41168 : return;
391 : }
392 :
393 :
394 : /*------------------------------------------------------------------------------------------*
395 : * Function CreateLCLDDecoder()
396 : *
397 : *
398 : *------------------------------------------------------------------------------------------*/
399 :
400 664 : ivas_error CreateLCLDDecoder(
401 : LCLDDecoder **psLCLDDecoder_out,
402 : const int32_t iSampleRate,
403 : const int32_t iChannels,
404 : const int32_t iNumBlocks,
405 : const int32_t iRealOnlyOut )
406 : {
407 : int32_t n;
408 : int32_t read_length;
409 : ivas_error error;
410 664 : LCLDDecoder *psLCLDDecoder = NULL;
411 :
412 664 : assert( iSampleRate == 48000 );
413 664 : assert( iNumBlocks == 16 || iNumBlocks == 8 || iNumBlocks == 4 );
414 664 : if ( ( psLCLDDecoder = (LCLDDecoder *) malloc( sizeof( LCLDDecoder ) ) ) == NULL )
415 : {
416 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
417 : }
418 664 : psLCLDDecoder->iSampleRate = iSampleRate;
419 664 : psLCLDDecoder->iChannels = iChannels;
420 664 : psLCLDDecoder->iAllocOffset = 0;
421 664 : psLCLDDecoder->iRealOnlyOut = iRealOnlyOut;
422 664 : if ( iRealOnlyOut == 1 )
423 : {
424 0 : psLCLDDecoder->iNumBlocks = iNumBlocks / 2;
425 : }
426 : else
427 : {
428 664 : psLCLDDecoder->iNumBlocks = iNumBlocks;
429 : }
430 664 : psLCLDDecoder->iNumBands = 0; /* read from bitstream*/
431 664 : psLCLDDecoder->piBandwidths = c_aiBandwidths48;
432 :
433 664 : psLCLDDecoder->iMSMode = 0;
434 664 : if ( ( psLCLDDecoder->piMSFlags = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
435 : {
436 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
437 : }
438 15936 : for ( n = 0; n < MAX_BANDS; n++ )
439 : {
440 15272 : psLCLDDecoder->piLRPhaseDiffs[n] = 0;
441 15272 : psLCLDDecoder->piMSPredCoefs[n] = 0;
442 : }
443 :
444 664 : psLCLDDecoder->iCommonGrouping = 1; /* Common grouping always on only impacts stereo */
445 664 : if ( ( psLCLDDecoder->piNumGroups = (int32_t *) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ) ) ) == NULL )
446 : {
447 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
448 : }
449 664 : if ( ( psLCLDDecoder->ppiGroupLengths = (int32_t **) malloc( psLCLDDecoder->iChannels * sizeof( int32_t * ) ) ) == NULL )
450 : {
451 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
452 : }
453 664 : if ( ( psLCLDDecoder->pppiRMSEnvelope = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
454 : {
455 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
456 : }
457 664 : if ( ( psLCLDDecoder->pppiSMR = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
458 : {
459 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
460 : }
461 664 : if ( ( psLCLDDecoder->pppiExcitation = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
462 : {
463 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
464 : }
465 664 : if ( ( psLCLDDecoder->pppiAlloc = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
466 : {
467 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
468 : }
469 :
470 664 : if ( ( psLCLDDecoder->pppiLCLDSignReal = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
471 : {
472 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
473 : }
474 664 : if ( ( psLCLDDecoder->pppiLCLDSignImag = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
475 : {
476 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
477 : }
478 664 : if ( ( psLCLDDecoder->pppiQLCLDReal = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
479 : {
480 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
481 : }
482 664 : if ( ( psLCLDDecoder->pppiQLCLDImag = (int32_t ***) malloc( psLCLDDecoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
483 : {
484 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
485 : }
486 :
487 1992 : for ( n = 0; n < iChannels; n++ )
488 : {
489 : int16_t k;
490 1328 : if ( ( psLCLDDecoder->ppiGroupLengths[n] = (int32_t *) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t ) ) ) == NULL )
491 : {
492 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
493 : }
494 1328 : if ( ( psLCLDDecoder->pppiRMSEnvelope[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
495 : {
496 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
497 : }
498 1328 : if ( ( psLCLDDecoder->pppiSMR[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
499 : {
500 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
501 : }
502 1328 : if ( ( psLCLDDecoder->pppiExcitation[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
503 : {
504 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
505 : }
506 1328 : if ( ( psLCLDDecoder->pppiAlloc[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
507 : {
508 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
509 : }
510 :
511 1328 : if ( ( psLCLDDecoder->pppiLCLDSignReal[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
512 : {
513 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
514 : }
515 1328 : if ( ( psLCLDDecoder->pppiLCLDSignImag[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
516 : {
517 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
518 : }
519 1328 : if ( ( psLCLDDecoder->pppiQLCLDReal[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
520 : {
521 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
522 : }
523 1328 : if ( ( psLCLDDecoder->pppiQLCLDImag[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
524 : {
525 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
526 : }
527 :
528 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
529 : {
530 21248 : if ( ( psLCLDDecoder->pppiRMSEnvelope[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
531 : {
532 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
533 : }
534 21248 : if ( ( psLCLDDecoder->pppiSMR[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
535 : {
536 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
537 : }
538 21248 : if ( ( psLCLDDecoder->pppiExcitation[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
539 : {
540 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
541 : }
542 21248 : if ( ( psLCLDDecoder->pppiAlloc[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
543 : {
544 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
545 : }
546 :
547 21248 : if ( ( psLCLDDecoder->pppiLCLDSignReal[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
548 : {
549 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
550 : }
551 21248 : if ( ( psLCLDDecoder->pppiLCLDSignImag[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
552 : {
553 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
554 : }
555 21248 : if ( ( psLCLDDecoder->pppiQLCLDReal[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
556 : {
557 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
558 : }
559 21248 : if ( ( psLCLDDecoder->pppiQLCLDImag[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
560 : {
561 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
562 : }
563 : }
564 : }
565 :
566 664 : read_length = READ_LENGTH;
567 43160 : for ( n = 0; n < ALLOC_TABLE_SIZE * 2; n++ )
568 : {
569 42496 : psLCLDDecoder->num_decode_table[n] = 1;
570 42496 : if ( c_apauiHuffEncTabels[n] != NULL )
571 : {
572 :
573 41168 : CreateDecodeTable( psLCLDDecoder, n, c_apauiHuffEncTabels[n], num_row_aauiLCLDHuff[n], read_length, &psLCLDDecoder->num_decode_table[n] );
574 : }
575 : else
576 : {
577 1328 : psLCLDDecoder->c_apauiHuffDecTable_RAM[n] = NULL;
578 : }
579 : }
580 :
581 664 : if ( ( error = CreatePredictionDecoder( &psLCLDDecoder->psPredictionDecoder, iChannels, psLCLDDecoder->iNumBlocks ) ) != IVAS_ERR_OK )
582 : {
583 0 : return error;
584 : }
585 664 : psLCLDDecoder->psNoiseGen = NULL; /* CreateNoiseGen(); No noise fill for now*/
586 664 : *psLCLDDecoder_out = psLCLDDecoder;
587 :
588 664 : return IVAS_ERR_OK;
589 : }
590 :
591 :
592 : /*------------------------------------------------------------------------------------------*
593 : * Function CreateLCLDDecoder()
594 : *
595 : *
596 : *------------------------------------------------------------------------------------------*/
597 :
598 664 : void DeleteLCLDDecoder(
599 : LCLDDecoder *psLCLDDecoder )
600 : {
601 : int32_t k, n;
602 :
603 664 : if ( psLCLDDecoder != NULL )
604 : {
605 664 : if ( psLCLDDecoder->piMSFlags != NULL )
606 : {
607 664 : free( psLCLDDecoder->piMSFlags );
608 : }
609 :
610 664 : if ( psLCLDDecoder->piNumGroups != NULL )
611 : {
612 664 : free( psLCLDDecoder->piNumGroups );
613 : }
614 :
615 664 : if ( psLCLDDecoder->ppiGroupLengths != NULL )
616 : {
617 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
618 : {
619 1328 : free( psLCLDDecoder->ppiGroupLengths[n] );
620 : }
621 664 : free( psLCLDDecoder->ppiGroupLengths );
622 : }
623 :
624 664 : if ( psLCLDDecoder->pppiRMSEnvelope != NULL )
625 : {
626 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
627 : {
628 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
629 : {
630 21248 : free( psLCLDDecoder->pppiRMSEnvelope[n][k] );
631 : }
632 1328 : free( psLCLDDecoder->pppiRMSEnvelope[n] );
633 : }
634 664 : free( psLCLDDecoder->pppiRMSEnvelope );
635 : }
636 :
637 664 : if ( psLCLDDecoder->pppiSMR != NULL )
638 : {
639 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
640 : {
641 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
642 : {
643 21248 : free( psLCLDDecoder->pppiSMR[n][k] );
644 : }
645 1328 : free( psLCLDDecoder->pppiSMR[n] );
646 : }
647 664 : free( psLCLDDecoder->pppiSMR );
648 : }
649 :
650 664 : if ( psLCLDDecoder->pppiExcitation != NULL )
651 : {
652 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
653 : {
654 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
655 : {
656 21248 : free( psLCLDDecoder->pppiExcitation[n][k] );
657 : }
658 1328 : free( psLCLDDecoder->pppiExcitation[n] );
659 : }
660 664 : free( psLCLDDecoder->pppiExcitation );
661 : }
662 :
663 :
664 664 : if ( psLCLDDecoder->pppiAlloc != NULL )
665 : {
666 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
667 : {
668 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
669 : {
670 21248 : free( psLCLDDecoder->pppiAlloc[n][k] );
671 : }
672 1328 : free( psLCLDDecoder->pppiAlloc[n] );
673 : }
674 664 : free( psLCLDDecoder->pppiAlloc );
675 : }
676 :
677 664 : if ( psLCLDDecoder->pppiLCLDSignReal != NULL )
678 : {
679 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
680 : {
681 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
682 : {
683 21248 : free( psLCLDDecoder->pppiLCLDSignReal[n][k] );
684 : }
685 1328 : free( psLCLDDecoder->pppiLCLDSignReal[n] );
686 : }
687 664 : free( psLCLDDecoder->pppiLCLDSignReal );
688 : }
689 :
690 664 : if ( psLCLDDecoder->pppiLCLDSignImag != NULL )
691 : {
692 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
693 : {
694 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
695 : {
696 21248 : free( psLCLDDecoder->pppiLCLDSignImag[n][k] );
697 : }
698 1328 : free( psLCLDDecoder->pppiLCLDSignImag[n] );
699 : }
700 664 : free( psLCLDDecoder->pppiLCLDSignImag );
701 : }
702 :
703 664 : if ( psLCLDDecoder->pppiQLCLDReal != NULL )
704 : {
705 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
706 : {
707 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
708 : {
709 21248 : free( psLCLDDecoder->pppiQLCLDReal[n][k] );
710 : }
711 1328 : free( psLCLDDecoder->pppiQLCLDReal[n] );
712 : }
713 664 : free( psLCLDDecoder->pppiQLCLDReal );
714 : }
715 :
716 664 : if ( psLCLDDecoder->pppiQLCLDImag != NULL )
717 : {
718 1992 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
719 : {
720 22576 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
721 : {
722 21248 : free( psLCLDDecoder->pppiQLCLDImag[n][k] );
723 : }
724 1328 : free( psLCLDDecoder->pppiQLCLDImag[n] );
725 : }
726 664 : free( psLCLDDecoder->pppiQLCLDImag );
727 : }
728 :
729 43160 : for ( n = 0; n < ALLOC_TABLE_SIZE * 2; n++ )
730 : {
731 42496 : if ( psLCLDDecoder->num_decode_table[n] > 1 )
732 : {
733 :
734 41168 : if ( psLCLDDecoder->c_apauiHuffDecTable_RAM[n] != NULL )
735 : {
736 41168 : free( psLCLDDecoder->c_apauiHuffDecTable_RAM[n] );
737 : }
738 : }
739 : }
740 :
741 664 : if ( psLCLDDecoder->psPredictionDecoder != NULL )
742 : {
743 664 : DeletePredictionDecoder( psLCLDDecoder->psPredictionDecoder );
744 664 : psLCLDDecoder->psPredictionDecoder = NULL;
745 : }
746 :
747 664 : if ( psLCLDDecoder->psNoiseGen != NULL )
748 : {
749 0 : DeleteNoiseGen( psLCLDDecoder->psNoiseGen );
750 : }
751 :
752 664 : free( psLCLDDecoder );
753 : }
754 :
755 664 : return;
756 : }
757 :
758 :
759 : /*------------------------------------------------------------------------------------------*
760 : * Local function declarations
761 : *
762 : *
763 : *------------------------------------------------------------------------------------------*/
764 :
765 : static void ApplyRMSEnvelope( const int32_t iNumBands, const int32_t *piBandwidths, const int32_t iNumGroups, const int32_t *piGroupLengths, int32_t **ppiRMSEnvelope, float **ppfReal, float **ppfImag );
766 :
767 : static void ReplaceSign( const int32_t iNumBlocks, const int32_t iNumLCLDBands, int32_t **ppiSignReal, int32_t **ppiSignImag, float **ppfReal, float **ppfImag, const int32_t *piBandwidths );
768 :
769 : static void InvQuantizeSpectrum( const int32_t iNumGroups, const int32_t *piGroupLengths, const int32_t iNumBands, const int32_t *piBandwidths, int32_t **ppiAlloc, int32_t **ppiQReal, int32_t **ppiQImag, float **ppfReal, float **ppfImag, NoiseGen *psNoiseGen );
770 :
771 : static void InvMSCoding( const int32_t iNumBlocks, const int32_t iNumBands, const int32_t *piBandwidths, const int32_t iMSMode, const int32_t *piMSFlags, const int32_t *piLRPhaseDiffs, const int32_t *piMSPredCoefs, float ***pppfReal, float ***pppfImag );
772 :
773 : static int32_t ReadHeaderInformation( int32_t *piNumBands, ISAR_SPLIT_REND_BITS_HANDLE pBits );
774 :
775 : static int32_t ReadMSInformation( const int32_t iNumBands, int32_t *piMSMode, int32_t *piMSFlags, int32_t *piLRPhaseDiffs, int32_t *piMSPredCoefs, ISAR_SPLIT_REND_BITS_HANDLE pBits );
776 :
777 : static int32_t ReadGroupInformation( const int32_t iChannels, const int32_t iNumBlocks, int32_t *piCommonGrouping, int32_t *piNumGroups, int32_t **ppiGroupLengths, ISAR_SPLIT_REND_BITS_HANDLE pBits );
778 :
779 : static int32_t ReadHuff( const uint32_t ( *pauiHuffDecTable )[HUFF_DEC_TABLE_SIZE], int32_t *piSymbol, ISAR_SPLIT_REND_BITS_HANDLE pBits );
780 :
781 : static int32_t ReadRMSEnvelope( const int32_t iChannels, const int32_t *piNumGroups, const int32_t iNumBands, int32_t ***pppiRMSEnvelope, ISAR_SPLIT_REND_BITS_HANDLE pBits );
782 :
783 : static int32_t ReadAllocInformation( int32_t *piAllocOffset, ISAR_SPLIT_REND_BITS_HANDLE pBits );
784 :
785 : static int32_t ReadLCLDData( const int32_t *piNumGroups, int32_t **ppiGroupLengths, const int32_t iNumBands, const int32_t iNumChannels, int32_t **ppiDecodingUnresolved, int32_t **ppiPredEnable, const int32_t iNumSubSets, const int32_t iSubSetId, int32_t ***pppiAlloc, int32_t ***pppiSignReal, int32_t ***pppiSignImag, int32_t ***pppiQReal, int32_t ***pppiQImag, int32_t **ppiDecodingFailed, ISAR_SPLIT_REND_BITS_HANDLE pBits, uint32_t ( *c_apauiHuffDecTables[2 * ALLOC_TABLE_SIZE] )[HUFF_DEC_TABLE_SIZE] );
786 :
787 : static void ComputeAllocation( const int32_t iChannels, const int32_t *piNumGroups, const int32_t iNumBands, int32_t ***pppiSMR, const int32_t iAllocOffset, int32_t ***pppiAlloc );
788 :
789 :
790 : /*------------------------------------------------------------------------------------------*
791 : * function LSetDecodingUnresolved()
792 : *
793 : *
794 : *------------------------------------------------------------------------------------------*/
795 :
796 0 : void SetDecodingUnresolved(
797 : LCLDDecoder *psLCLDDecoder )
798 : {
799 : int32_t n, ch;
800 0 : PredictionDecoder *psPredictionDecoder = psLCLDDecoder->psPredictionDecoder;
801 :
802 0 : for ( ch = 0; ch < psPredictionDecoder->iChannels; ch++ )
803 : {
804 0 : for ( n = 0; n < psPredictionDecoder->iNumSubSets; n++ )
805 : {
806 0 : psPredictionDecoder->ppiDecodingUnresolved[ch][n] = 1;
807 0 : psPredictionDecoder->ppiDecodingFailed[ch][n] = 1;
808 0 : psPredictionDecoder->ppiDecodingFailedPrev[ch][n] = 1;
809 : }
810 : }
811 :
812 0 : return;
813 : }
814 :
815 :
816 : /*------------------------------------------------------------------------------------------*
817 : * function AnyDecodingFailedPrev()
818 : *
819 : *
820 : *------------------------------------------------------------------------------------------*/
821 :
822 133227 : int16_t AnyDecodingFailedPrev(
823 : LCLDDecoder *psLCLDDecoder )
824 : {
825 : int32_t n, ch;
826 133227 : PredictionDecoder *psPredictionDecoder = psLCLDDecoder->psPredictionDecoder;
827 :
828 399681 : for ( ch = 0; ch < psPredictionDecoder->iChannels; ch++ )
829 : {
830 565106 : for ( n = 0; n < psPredictionDecoder->iNumSubSets; n++ )
831 : {
832 298652 : if ( psPredictionDecoder->ppiDecodingFailedPrev[ch][n] == 1 )
833 : {
834 0 : return 1;
835 : }
836 : }
837 : }
838 :
839 133227 : return 0;
840 : }
841 :
842 : /*------------------------------------------------------------------------------------------*
843 : * function AnyDecodingFailed()
844 : *
845 : *
846 : *------------------------------------------------------------------------------------------*/
847 :
848 133227 : int16_t AnyDecodingFailed(
849 : LCLDDecoder *psLCLDDecoder )
850 : {
851 : int32_t n, ch;
852 133227 : PredictionDecoder *psPredictionDecoder = psLCLDDecoder->psPredictionDecoder;
853 :
854 399681 : for ( ch = 0; ch < psPredictionDecoder->iChannels; ch++ )
855 : {
856 565106 : for ( n = 0; n < psPredictionDecoder->iNumSubSets; n++ )
857 : {
858 298652 : if ( psPredictionDecoder->ppiDecodingFailed[ch][n] == 1 )
859 : {
860 0 : return 1;
861 : }
862 : }
863 : }
864 :
865 133227 : return 0;
866 : }
867 :
868 :
869 : /*------------------------------------------------------------------------------------------*
870 : * function GetDecodingFailedStatus()
871 : *
872 : *
873 : *------------------------------------------------------------------------------------------*/
874 :
875 0 : int32_t **GetDecodingFailedStatus(
876 : LCLDDecoder *psLCLDDecoder )
877 : {
878 0 : return psLCLDDecoder->psPredictionDecoder->ppiDecodingFailed;
879 : }
880 :
881 :
882 : /*------------------------------------------------------------------------------------------*
883 : * function GetNumSubSets()
884 : *
885 : *
886 : *------------------------------------------------------------------------------------------*/
887 :
888 664 : int16_t GetNumSubSets(
889 : LCLDDecoder *psLCLDDecoder )
890 : {
891 664 : return (int16_t) psLCLDDecoder->psPredictionDecoder->iNumSubSets;
892 : }
893 :
894 :
895 : /*------------------------------------------------------------------------------------------*
896 : * function GetDecodingFailedPrevStatus()
897 : *
898 : *
899 : *------------------------------------------------------------------------------------------*/
900 :
901 0 : int32_t **GetDecodingFailedPrevStatus(
902 : LCLDDecoder *psLCLDDecoder )
903 : {
904 0 : return psLCLDDecoder->psPredictionDecoder->ppiDecodingFailedPrev;
905 : }
906 :
907 :
908 : /*------------------------------------------------------------------------------------------*
909 : * function UnpackReal()
910 : *
911 : *
912 : *------------------------------------------------------------------------------------------*/
913 :
914 0 : static void UnpackReal(
915 : const int32_t iChannels,
916 : const int32_t iNumBlocks,
917 : float ***pppfReal,
918 : float ***pppfImag )
919 : {
920 : int32_t ch, b, n;
921 :
922 0 : for ( ch = 0; ch < iChannels; ch++ )
923 : {
924 0 : for ( b = 0; b < LCLD_BANDS; b++ )
925 : {
926 0 : int32_t iRealBlock = iNumBlocks - 1;
927 0 : for ( n = iNumBlocks / 2 - 1; n >= 0; n-- )
928 : {
929 0 : pppfReal[ch][iRealBlock][b] = pppfImag[ch][n][b] * 2.0f;
930 0 : pppfReal[ch][iRealBlock - 1][b] = pppfReal[ch][n][b] * 2.0f;
931 0 : pppfImag[ch][iRealBlock][b] = 0.0f;
932 0 : pppfImag[ch][iRealBlock - 1][b] = 0.0f;
933 0 : iRealBlock -= 2;
934 : }
935 : }
936 : }
937 :
938 0 : return;
939 : }
940 :
941 :
942 : /*------------------------------------------------------------------------------------------*
943 : * Function DecodeLCLDFrame()
944 : *
945 : *
946 : *------------------------------------------------------------------------------------------*/
947 :
948 133227 : int32_t DecodeLCLDFrame(
949 : LCLDDecoder *psLCLDDecoder,
950 : ISAR_SPLIT_REND_BITS_HANDLE pBits, /* i/o: ISAR bits handle */
951 : float ***pppfLCLDReal,
952 : float ***pppfLCLDImag )
953 : {
954 : int32_t k, n;
955 :
956 133227 : ReadHeaderInformation( &psLCLDDecoder->iNumBands, pBits );
957 :
958 133227 : if ( psLCLDDecoder->iChannels == 2 )
959 : {
960 133227 : ReadMSInformation( psLCLDDecoder->iNumBands, &psLCLDDecoder->iMSMode, psLCLDDecoder->piMSFlags, psLCLDDecoder->piLRPhaseDiffs, psLCLDDecoder->piMSPredCoefs, pBits );
961 : }
962 :
963 133227 : ReadPredictors( psLCLDDecoder->psPredictionDecoder, pBits );
964 133227 : UpdateDecodingUnresolved( psLCLDDecoder->psPredictionDecoder );
965 133227 : UpdateDecodingFailedStatus( psLCLDDecoder->psPredictionDecoder );
966 :
967 133227 : ReadGroupInformation( psLCLDDecoder->iChannels, psLCLDDecoder->iNumBlocks, &psLCLDDecoder->iCommonGrouping, psLCLDDecoder->piNumGroups, psLCLDDecoder->ppiGroupLengths, pBits );
968 :
969 133227 : ReadRMSEnvelope( psLCLDDecoder->iChannels, (const int32_t *) psLCLDDecoder->piNumGroups, psLCLDDecoder->iNumBands, psLCLDDecoder->pppiRMSEnvelope, pBits );
970 :
971 133227 : ReadAllocInformation( &psLCLDDecoder->iAllocOffset, pBits );
972 :
973 133227 : if ( psLCLDDecoder->iChannels == 2 && psLCLDDecoder->iCommonGrouping == 1 )
974 : { /* MS Mode? */
975 331078 : for ( k = 0; k < psLCLDDecoder->piNumGroups[0]; k++ )
976 : {
977 197851 : PerceptualModelStereo( psLCLDDecoder->iNumBands, psLCLDDecoder->piMSFlags,
978 197851 : psLCLDDecoder->pppiRMSEnvelope[0][k],
979 197851 : psLCLDDecoder->pppiRMSEnvelope[1][k],
980 197851 : psLCLDDecoder->pppiExcitation[0][k],
981 197851 : psLCLDDecoder->pppiExcitation[1][k],
982 197851 : psLCLDDecoder->pppiSMR[0][k],
983 197851 : psLCLDDecoder->pppiSMR[1][k] );
984 : }
985 : }
986 : else
987 : {
988 0 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
989 : { /* This will be updated to support multiple sample rates*/
990 0 : for ( k = 0; k < psLCLDDecoder->piNumGroups[n]; k++ )
991 : {
992 0 : PerceptualModel( psLCLDDecoder->iNumBands, psLCLDDecoder->pppiRMSEnvelope[n][k], psLCLDDecoder->pppiExcitation[n][k], psLCLDDecoder->pppiSMR[n][k] );
993 : }
994 : }
995 : }
996 :
997 133227 : ComputeAllocation( psLCLDDecoder->iChannels, (const int32_t *) psLCLDDecoder->piNumGroups, psLCLDDecoder->iNumBands, psLCLDDecoder->pppiSMR, psLCLDDecoder->iAllocOffset, psLCLDDecoder->pppiAlloc );
998 :
999 133227 : ReadLCLDData(
1000 133227 : psLCLDDecoder->piNumGroups,
1001 : psLCLDDecoder->ppiGroupLengths,
1002 : psLCLDDecoder->iNumBands,
1003 : psLCLDDecoder->iChannels,
1004 133227 : psLCLDDecoder->psPredictionDecoder->ppiDecodingUnresolved,
1005 133227 : psLCLDDecoder->psPredictionDecoder->ppiPredBandEnable,
1006 133227 : psLCLDDecoder->psPredictionDecoder->iNumSubSets,
1007 133227 : psLCLDDecoder->psPredictionDecoder->iSubSetId,
1008 : psLCLDDecoder->pppiAlloc,
1009 : psLCLDDecoder->pppiLCLDSignReal,
1010 : psLCLDDecoder->pppiLCLDSignImag,
1011 : psLCLDDecoder->pppiQLCLDReal,
1012 : psLCLDDecoder->pppiQLCLDImag,
1013 133227 : psLCLDDecoder->psPredictionDecoder->ppiDecodingFailed,
1014 : pBits,
1015 133227 : psLCLDDecoder->c_apauiHuffDecTable_RAM );
1016 :
1017 399681 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
1018 : {
1019 266454 : InvQuantizeSpectrum( psLCLDDecoder->piNumGroups[n],
1020 266454 : (const int32_t *) psLCLDDecoder->ppiGroupLengths[n],
1021 : psLCLDDecoder->iNumBands, psLCLDDecoder->piBandwidths,
1022 266454 : psLCLDDecoder->pppiAlloc[n],
1023 266454 : psLCLDDecoder->pppiQLCLDReal[n],
1024 266454 : psLCLDDecoder->pppiQLCLDImag[n],
1025 266454 : pppfLCLDReal[n], pppfLCLDImag[n],
1026 : psLCLDDecoder->psNoiseGen );
1027 :
1028 266454 : ReplaceSign( psLCLDDecoder->iNumBlocks, psLCLDDecoder->iNumBands,
1029 266454 : psLCLDDecoder->pppiLCLDSignReal[n],
1030 266454 : psLCLDDecoder->pppiLCLDSignImag[n],
1031 266454 : pppfLCLDReal[n], pppfLCLDImag[n], psLCLDDecoder->piBandwidths );
1032 : }
1033 : #ifdef DEBUG_WRITE_PREDICTORS
1034 : {
1035 : static FILE *fid;
1036 : if ( !fid )
1037 : fid = fopen( "pred_dec.txt", "wt" );
1038 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
1039 : {
1040 : int16_t b;
1041 : for ( b = 0; b < 60; b++ )
1042 : fprintf( fid, "%.5f ", (float) psLCLDDecoder->psPredictionDecoder->ppiPredBandEnable[n][b] * psLCLDDecoder->psPredictionDecoder->ppfA1Imag[n][b] );
1043 : }
1044 : fprintf( fid, "%d %d\n", psLCLDDecoder->psPredictionDecoder->iSubSetId, psLCLDDecoder->psPredictionDecoder->piPredChanEnable[n] );
1045 : }
1046 : #endif
1047 :
1048 133227 : ApplyInversePredictors( psLCLDDecoder->psPredictionDecoder, pppfLCLDReal, pppfLCLDImag );
1049 :
1050 399681 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
1051 : {
1052 266454 : ApplyRMSEnvelope( psLCLDDecoder->iNumBands, psLCLDDecoder->piBandwidths,
1053 266454 : psLCLDDecoder->piNumGroups[n],
1054 266454 : (const int32_t *) psLCLDDecoder->ppiGroupLengths[n],
1055 266454 : psLCLDDecoder->pppiRMSEnvelope[n],
1056 266454 : pppfLCLDReal[n], pppfLCLDImag[n] );
1057 : }
1058 :
1059 133227 : if ( psLCLDDecoder->iChannels == 2 && psLCLDDecoder->iMSMode > 0 )
1060 : {
1061 133216 : InvMSCoding( psLCLDDecoder->iNumBlocks, psLCLDDecoder->iNumBands,
1062 : psLCLDDecoder->piBandwidths, psLCLDDecoder->iMSMode,
1063 133216 : (const int32_t *) psLCLDDecoder->piMSFlags,
1064 133216 : (const int32_t *) psLCLDDecoder->piLRPhaseDiffs,
1065 133216 : (const int32_t *) psLCLDDecoder->piMSPredCoefs,
1066 : pppfLCLDReal, pppfLCLDImag );
1067 : }
1068 :
1069 133227 : if ( psLCLDDecoder->iRealOnlyOut == 1 )
1070 : {
1071 0 : UnpackReal( psLCLDDecoder->iChannels,
1072 0 : psLCLDDecoder->iNumBlocks * 2,
1073 : pppfLCLDReal,
1074 : pppfLCLDImag );
1075 : }
1076 :
1077 133227 : return AnyDecodingUnresolved( psLCLDDecoder->psPredictionDecoder );
1078 : }
1079 :
1080 :
1081 : /*------------------------------------------------------------------------------------------*
1082 : * Local functions
1083 : *
1084 : *
1085 : *------------------------------------------------------------------------------------------*/
1086 :
1087 266454 : static void ApplyRMSEnvelope(
1088 : const int32_t iNumBands,
1089 : const int32_t *piBandwidths,
1090 : const int32_t iNumGroups,
1091 : const int32_t *piGroupLengths,
1092 : int32_t **ppiRMSEnvelope,
1093 : float **ppfReal,
1094 : float **ppfImag )
1095 : {
1096 : int32_t b, k, n;
1097 : int32_t iBlockOffset, iFBOffset;
1098 :
1099 266454 : iBlockOffset = 0;
1100 662156 : for ( n = 0; n < iNumGroups; n++ )
1101 : {
1102 4497982 : for ( k = 0; k < piGroupLengths[n]; k++ )
1103 : {
1104 4102280 : iFBOffset = 0;
1105 94352440 : for ( b = 0; b < iNumBands; b++ )
1106 : {
1107 : int32_t m;
1108 : int32_t iRMSEnv;
1109 : float fGain;
1110 :
1111 90250160 : iRMSEnv = ppiRMSEnvelope[n][b];
1112 90250160 : fGain =
1113 90250160 : c_afRMSEnvReconstructTable[ENV_RECONSTRUCT_TABLE_CENTER + iRMSEnv];
1114 295364160 : for ( m = 0; m < piBandwidths[b]; m++ )
1115 : {
1116 205114000 : ppfReal[iBlockOffset][iFBOffset] *= fGain;
1117 205114000 : ppfImag[iBlockOffset][iFBOffset] *= fGain;
1118 205114000 : iFBOffset++;
1119 : }
1120 : }
1121 4102280 : iBlockOffset++;
1122 : }
1123 : }
1124 :
1125 266454 : return;
1126 : }
1127 :
1128 :
1129 266454 : static void ReplaceSign(
1130 : const int32_t iNumBlocks,
1131 : const int32_t iNumLCLDBands,
1132 : int32_t **ppiSignReal,
1133 : int32_t **ppiSignImag,
1134 : float **ppfReal,
1135 : float **ppfImag,
1136 : const int32_t *piBandwidths )
1137 : {
1138 : int32_t b, n;
1139 : int32_t m, idx;
1140 :
1141 4368734 : for ( n = 0; n < iNumBlocks; n++ )
1142 : {
1143 4102280 : idx = 0;
1144 94352440 : for ( b = 0; b < iNumLCLDBands; b++ )
1145 : {
1146 295364160 : for ( m = 0; m < piBandwidths[b]; m++ )
1147 : {
1148 205114000 : if ( ppiSignReal[n][idx] == 1 )
1149 : {
1150 63830537 : ppfReal[n][idx] = -ppfReal[n][idx];
1151 : }
1152 :
1153 205114000 : if ( ppiSignImag[n][idx] == 1 )
1154 : {
1155 63741967 : ppfImag[n][idx] = -ppfImag[n][idx];
1156 : }
1157 205114000 : idx++;
1158 : }
1159 : }
1160 : }
1161 :
1162 266454 : return;
1163 : }
1164 :
1165 :
1166 266454 : static void InvQuantizeSpectrum(
1167 : const int32_t iNumGroups,
1168 : const int32_t *piGroupLengths,
1169 : const int32_t iNumBands,
1170 : const int32_t *piBandwidths,
1171 : int32_t **ppiAlloc,
1172 : int32_t **ppiQReal,
1173 : int32_t **ppiQImag,
1174 : float **ppfReal,
1175 : float **ppfImag,
1176 : NoiseGen *psNoiseGen /* Pass in NULL to switch off noise gen */
1177 : )
1178 : {
1179 : int32_t b, k, n;
1180 : int32_t iBlockOffest, iFBOffset;
1181 :
1182 266454 : iBlockOffest = 0;
1183 662156 : for ( n = 0; n < iNumGroups; n++ )
1184 : {
1185 4497982 : for ( k = 0; k < piGroupLengths[n]; k++ )
1186 : {
1187 4102280 : iFBOffset = 0;
1188 94352440 : for ( b = 0; b < iNumBands; b++ )
1189 : {
1190 : int32_t m;
1191 : int32_t iAlloc;
1192 : float fInvSCFGain;
1193 :
1194 90250160 : iAlloc = ppiAlloc[n][b];
1195 90250160 : fInvSCFGain = c_afInvScaleFactor[iAlloc];
1196 :
1197 90250160 : if ( iAlloc > 0 )
1198 : {
1199 274312530 : for ( m = 0; m < piBandwidths[b]; m++ )
1200 : {
1201 : int32_t iQuantValue;
1202 :
1203 188115418 : iQuantValue = ppiQReal[iBlockOffest][iFBOffset];
1204 188115418 : ppfReal[iBlockOffest][iFBOffset] = (float) iQuantValue * fInvSCFGain;
1205 :
1206 188115418 : iQuantValue = ppiQImag[iBlockOffest][iFBOffset];
1207 188115418 : ppfImag[iBlockOffest][iFBOffset] = (float) iQuantValue * fInvSCFGain;
1208 :
1209 188115418 : iFBOffset++;
1210 : }
1211 : }
1212 4053048 : else if ( psNoiseGen != NULL )
1213 : {
1214 0 : for ( m = 0; m < piBandwidths[b]; m++ )
1215 : {
1216 0 : ppfReal[iBlockOffest][iFBOffset] = 0.7f * GetNoise( psNoiseGen );
1217 0 : ppfImag[iBlockOffest][iFBOffset] = 0.7f * GetNoise( psNoiseGen );
1218 :
1219 0 : iFBOffset++;
1220 : }
1221 : }
1222 : else
1223 : {
1224 4053048 : iFBOffset += piBandwidths[b];
1225 : }
1226 : }
1227 :
1228 4102280 : iBlockOffest++;
1229 : }
1230 : }
1231 :
1232 266454 : return;
1233 : }
1234 :
1235 :
1236 133216 : static void InvMSCoding(
1237 : const int32_t iNumBlocks,
1238 : const int32_t iNumBands,
1239 : const int32_t *piBandwidths,
1240 : const int32_t iMSMode,
1241 : const int32_t *piMSFlags,
1242 : const int32_t *piLRPhaseDiffs,
1243 : const int32_t *piMSPredCoefs,
1244 : float ***pppfReal,
1245 : float ***pppfImag )
1246 : {
1247 133216 : if ( iMSMode > 0 )
1248 : {
1249 : int32_t b;
1250 : int32_t iFBOffset;
1251 133216 : int32_t bMSPred = 0;
1252 :
1253 133216 : iFBOffset = 0;
1254 3063968 : for ( b = 0; b < iNumBands; b++ )
1255 : {
1256 2930752 : if ( piMSFlags[b] == 1 )
1257 : {
1258 : int32_t n;
1259 : int32_t phaseIdx;
1260 : float fPred;
1261 :
1262 1968751 : phaseIdx = piLRPhaseDiffs[bMSPred] - PHASE_MIN_VAL;
1263 1968751 : fPred = dequantPred( piMSPredCoefs[bMSPred] );
1264 6205817 : for ( n = 0; n < piBandwidths[b]; n++ )
1265 : {
1266 : int32_t k;
1267 69536790 : for ( k = 0; k < iNumBlocks; k++ )
1268 : {
1269 : float fLeftReal;
1270 : float fLeftImag;
1271 : float fRightReal;
1272 : float fRightImag;
1273 :
1274 65299724 : if ( iMSMode == 3 )
1275 : {
1276 40977852 : pppfReal[1][k][iFBOffset] += fPred * pppfReal[0][k][iFBOffset];
1277 40977852 : pppfImag[1][k][iFBOffset] += fPred * pppfImag[0][k][iFBOffset];
1278 : }
1279 :
1280 65299724 : fLeftReal = ( pppfReal[0][k][iFBOffset] + pppfReal[1][k][iFBOffset] );
1281 65299724 : fLeftImag = ( pppfImag[0][k][iFBOffset] + pppfImag[1][k][iFBOffset] );
1282 65299724 : fRightReal = ( pppfReal[0][k][iFBOffset] - pppfReal[1][k][iFBOffset] );
1283 65299724 : fRightImag = ( pppfImag[0][k][iFBOffset] - pppfImag[1][k][iFBOffset] );
1284 :
1285 65299724 : if ( iMSMode == 3 )
1286 : {
1287 40977852 : cplxmult_lcld( &fRightReal, &fRightImag, c_afRotRealImag[phaseIdx][0], -c_afRotRealImag[phaseIdx][1] );
1288 : }
1289 :
1290 65299724 : pppfReal[0][k][iFBOffset] = fLeftReal;
1291 65299724 : pppfReal[1][k][iFBOffset] = fRightReal;
1292 65299724 : pppfImag[0][k][iFBOffset] = fLeftImag;
1293 65299724 : pppfImag[1][k][iFBOffset] = fRightImag;
1294 : }
1295 4237066 : iFBOffset++;
1296 : }
1297 1968751 : bMSPred++;
1298 : }
1299 : else
1300 : {
1301 962001 : iFBOffset += piBandwidths[b];
1302 : }
1303 : }
1304 : }
1305 :
1306 133216 : return;
1307 : }
1308 :
1309 :
1310 133227 : static int32_t ReadHeaderInformation(
1311 : int32_t *piNumBands,
1312 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1313 : {
1314 : int32_t iBitsRead;
1315 :
1316 133227 : iBitsRead = 0;
1317 133227 : *piNumBands = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 5 );
1318 133227 : iBitsRead += 5;
1319 :
1320 133227 : return iBitsRead;
1321 : }
1322 :
1323 :
1324 133227 : static int32_t ReadMSInformation(
1325 : const int32_t iNumBands,
1326 : int32_t *piMSMode,
1327 : int32_t *piMSFlags,
1328 : int32_t *piLRPhaseDiffs,
1329 : int32_t *piMSPredCoefs,
1330 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1331 : {
1332 : int32_t iBitsRead;
1333 :
1334 133227 : iBitsRead = 0;
1335 133227 : *piMSMode = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 2 );
1336 133227 : iBitsRead += 2;
1337 :
1338 133227 : if ( *piMSMode == 0 )
1339 : {
1340 : int32_t n;
1341 253 : for ( n = 0; n < iNumBands; n++ )
1342 : {
1343 242 : piMSFlags[n] = 0;
1344 : }
1345 : }
1346 133216 : else if ( *piMSMode == 1 )
1347 : {
1348 : int32_t n;
1349 37950 : for ( n = 0; n < iNumBands; n++ )
1350 : {
1351 36300 : piMSFlags[n] = 1;
1352 : }
1353 : }
1354 131566 : else if ( *piMSMode == 2 )
1355 : {
1356 : int32_t n;
1357 1174863 : for ( n = 0; n < iNumBands; n++ )
1358 : {
1359 1123782 : piMSFlags[n] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1360 1123782 : iBitsRead += 1;
1361 : }
1362 : }
1363 80485 : else if ( *piMSMode == 3 )
1364 : {
1365 : int32_t n;
1366 : int32_t iMSPredAll;
1367 80485 : int32_t iNumMSPredBands = 0;
1368 : int32_t anyNonZero;
1369 80485 : iMSPredAll = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1370 80485 : iBitsRead += 1;
1371 80485 : if ( iMSPredAll )
1372 : {
1373 8956 : iNumMSPredBands = iNumBands;
1374 205988 : for ( n = 0; n < iNumBands; n++ )
1375 : {
1376 197032 : piMSFlags[n] = 1;
1377 : }
1378 : }
1379 : else
1380 : {
1381 1645167 : for ( n = 0; n < iNumBands; n++ )
1382 : {
1383 1573638 : piMSFlags[n] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1384 1573638 : iBitsRead += 1;
1385 1573638 : if ( piMSFlags[n] )
1386 : {
1387 1040197 : iNumMSPredBands++;
1388 : }
1389 : }
1390 : }
1391 :
1392 80485 : anyNonZero = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1393 :
1394 80485 : if ( anyNonZero )
1395 : {
1396 62285 : piLRPhaseDiffs[0] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, PHASE_BAND0_BITS );
1397 62285 : piLRPhaseDiffs[0] += PHASE_MIN_VAL;
1398 62285 : iBitsRead += PHASE_BAND0_BITS;
1399 925771 : for ( n = 1; n < iNumMSPredBands; n++ )
1400 : {
1401 : int32_t tabIdx;
1402 863486 : iBitsRead += ReadHuff( c_aaiRMSEnvHuffDec, &tabIdx, pBits );
1403 863486 : piLRPhaseDiffs[n] = tabIdx + ENV_DELTA_MIN;
1404 : }
1405 62285 : DecodePhase( piLRPhaseDiffs, iNumMSPredBands, PHASE_DIFF_DIM );
1406 : }
1407 : else
1408 : {
1409 329658 : for ( n = 0; n < iNumMSPredBands; n++ )
1410 : {
1411 311458 : piLRPhaseDiffs[n] = 0;
1412 : }
1413 : }
1414 :
1415 80485 : anyNonZero = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1416 :
1417 80485 : if ( anyNonZero )
1418 : {
1419 76953 : piMSPredCoefs[0] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, PRED_BAND0_BITS );
1420 76953 : piMSPredCoefs[0] += PRED_MIN_VAL;
1421 76953 : iBitsRead += PRED_BAND0_BITS;
1422 1215045 : for ( n = 1; n < iNumMSPredBands; n++ )
1423 : {
1424 : int32_t tabIdx;
1425 1138092 : iBitsRead += ReadHuff( c_aaiRMSEnvHuffDec, &tabIdx, pBits );
1426 1138092 : piMSPredCoefs[n] = tabIdx + ENV_DELTA_MIN;
1427 : }
1428 76953 : DecodePredCoef( piMSPredCoefs, iNumMSPredBands );
1429 : }
1430 : else
1431 : {
1432 25716 : for ( n = 0; n < iNumMSPredBands; n++ )
1433 : {
1434 22184 : piMSPredCoefs[n] = 0;
1435 : }
1436 : }
1437 : #ifdef DEBUG_WRITE_MS_PRED
1438 : {
1439 : static FILE *fid;
1440 : if ( !fid )
1441 : fid = fopen( "ms_pred_dec.txt", "wt" );
1442 : writeMSPred( piLRPhaseDiffs, piMSPredCoefs, *piMSMode, iNumMSPredBands, iNumBands, fid, piMSFlags );
1443 : }
1444 : #endif
1445 : }
1446 : else
1447 : {
1448 0 : printf( "ERROR UNSUPPORTED MS MODE\n" );
1449 : }
1450 :
1451 133227 : return iBitsRead;
1452 : }
1453 :
1454 :
1455 133227 : static int32_t ReadGroupInformation(
1456 : const int32_t iChannels,
1457 : const int32_t iNumBlocks,
1458 : int32_t *piCommonGrouping,
1459 : int32_t *piNumGroups,
1460 : int32_t **ppiGroupLengths,
1461 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1462 : {
1463 : int32_t c, k, iBitsRead;
1464 :
1465 133227 : iBitsRead = 0;
1466 133227 : if ( iChannels == 2 )
1467 : {
1468 133227 : *piCommonGrouping = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1469 133227 : iBitsRead += 1;
1470 :
1471 133227 : if ( *piCommonGrouping == 1 )
1472 : {
1473 133227 : piNumGroups[0] = 0;
1474 133227 : ppiGroupLengths[0][piNumGroups[0]] = 1;
1475 2051140 : for ( k = 0; k < ( iNumBlocks - 1 ); k++ )
1476 : {
1477 : int32_t iGroupStart;
1478 :
1479 1917913 : iGroupStart = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1480 1917913 : iBitsRead += 1;
1481 :
1482 1917913 : if ( iGroupStart == 1 )
1483 : {
1484 64624 : piNumGroups[0]++;
1485 64624 : ppiGroupLengths[0][piNumGroups[0]] = 1;
1486 : }
1487 : else
1488 : {
1489 1853289 : ppiGroupLengths[0][piNumGroups[0]]++;
1490 : }
1491 : }
1492 133227 : piNumGroups[0]++;
1493 :
1494 133227 : piNumGroups[1] = piNumGroups[0];
1495 331078 : for ( k = 0; k < piNumGroups[1]; k++ )
1496 : {
1497 197851 : ppiGroupLengths[1][k] = ppiGroupLengths[0][k];
1498 : }
1499 : }
1500 : else
1501 : {
1502 0 : for ( c = 0; c < iChannels; c++ )
1503 : {
1504 0 : piNumGroups[c] = 0;
1505 0 : ppiGroupLengths[c][piNumGroups[c]] = 1;
1506 0 : for ( k = 0; k < ( iNumBlocks - 1 ); k++ )
1507 : {
1508 : int32_t iGroupStart;
1509 :
1510 0 : iGroupStart = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1511 0 : iBitsRead += 1;
1512 :
1513 0 : if ( iGroupStart == 1 )
1514 : {
1515 0 : piNumGroups[c]++;
1516 0 : ppiGroupLengths[c][piNumGroups[c]] = 1;
1517 : }
1518 : else
1519 : {
1520 0 : ppiGroupLengths[c][piNumGroups[c]]++;
1521 : }
1522 : }
1523 0 : piNumGroups[c]++;
1524 : }
1525 : }
1526 : }
1527 : else
1528 : {
1529 0 : for ( c = 0; c < iChannels; c++ )
1530 : {
1531 0 : piNumGroups[c] = 0;
1532 0 : ppiGroupLengths[c][piNumGroups[c]] = 1;
1533 0 : for ( k = 0; k < ( iNumBlocks - 1 ); k++ )
1534 : {
1535 : int32_t iGroupStart;
1536 :
1537 0 : iGroupStart = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1538 0 : iBitsRead += 1;
1539 :
1540 0 : if ( iGroupStart == 1 )
1541 : {
1542 0 : piNumGroups[c]++;
1543 0 : ppiGroupLengths[c][piNumGroups[c]] = 1;
1544 : }
1545 : else
1546 : {
1547 0 : ppiGroupLengths[c][piNumGroups[c]]++;
1548 : }
1549 : }
1550 0 : piNumGroups[c]++;
1551 : }
1552 : }
1553 :
1554 133227 : return iBitsRead;
1555 : }
1556 :
1557 :
1558 191644125 : static int32_t BSForceBack(
1559 : ISAR_SPLIT_REND_BITS_HANDLE pBits,
1560 : int32_t iValue,
1561 : int32_t iBitCount )
1562 : {
1563 191644125 : pBits->bits_read -= iBitCount;
1564 :
1565 191644125 : return ( iValue >> iBitCount );
1566 : }
1567 :
1568 :
1569 251110128 : static int32_t ReadHuff(
1570 : const uint32_t ( *pauiHuffDecTable )[HUFF_DEC_TABLE_SIZE],
1571 : int32_t *piSymbol,
1572 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1573 : {
1574 : int32_t iBitsRead;
1575 : int32_t iSymbol;
1576 : int32_t iIndex;
1577 : int32_t iVal;
1578 :
1579 251110128 : iVal = 0;
1580 251110128 : iIndex = 0;
1581 251110128 : iSymbol = 0xFFFF;
1582 251110128 : iBitsRead = 0;
1583 605684273 : while ( iSymbol == 0xFFFF )
1584 : {
1585 354574145 : iIndex = ISAR_SPLIT_REND_BITStream_read_int32( pBits, HUFF_READ_SIZE );
1586 354574145 : iBitsRead += HUFF_READ_SIZE;
1587 :
1588 354574145 : iIndex = pauiHuffDecTable[iVal][iIndex];
1589 354574145 : iSymbol = ( iIndex & 0xFFFF );
1590 :
1591 354574145 : iVal = ( iIndex >> 16 );
1592 : }
1593 :
1594 251110128 : if ( iVal )
1595 : {
1596 191644125 : BSForceBack( pBits, iIndex, iVal );
1597 191644125 : iBitsRead -= iVal;
1598 : }
1599 :
1600 251110128 : *piSymbol = iSymbol;
1601 :
1602 251110128 : return iBitsRead;
1603 : }
1604 :
1605 :
1606 133227 : static int32_t ReadRMSEnvelope(
1607 : const int32_t iChannels,
1608 : const int32_t *piNumGroups,
1609 : const int32_t iNumBands,
1610 : int32_t ***pppiRMSEnvelope,
1611 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1612 : {
1613 : int32_t b, k, n;
1614 : int32_t iBitsRead, iLastRMSVal;
1615 :
1616 133227 : iBitsRead = 0;
1617 399681 : for ( n = 0; n < iChannels; n++ )
1618 : {
1619 662156 : for ( k = 0; k < piNumGroups[n]; k++ )
1620 : {
1621 395702 : iLastRMSVal = ISAR_SPLIT_REND_BITStream_read_int32( pBits, ENV0_BITS );
1622 395702 : iBitsRead += ENV0_BITS;
1623 :
1624 395702 : iLastRMSVal += ENV_MIN;
1625 395702 : pppiRMSEnvelope[n][k][0] = iLastRMSVal;
1626 8705444 : for ( b = 1; b < iNumBands; b++ )
1627 : {
1628 : int32_t iDelta;
1629 :
1630 8309742 : iBitsRead += ReadHuff( c_aaiRMSEnvHuffDec, &iDelta, pBits );
1631 :
1632 8309742 : iDelta += ENV_DELTA_MIN;
1633 8309742 : iLastRMSVal += iDelta;
1634 8309742 : pppiRMSEnvelope[n][k][b] = iLastRMSVal;
1635 : }
1636 : }
1637 : }
1638 :
1639 133227 : return iBitsRead;
1640 : }
1641 :
1642 :
1643 133227 : static int32_t ReadAllocInformation(
1644 : int32_t *piAllocOffset,
1645 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1646 : {
1647 : int32_t iBitsRead;
1648 :
1649 133227 : iBitsRead = 0;
1650 133227 : *piAllocOffset = ISAR_SPLIT_REND_BITStream_read_int32( pBits, ALLOC_OFFSET_BITS );
1651 133227 : *piAllocOffset += MIN_ALLOC_OFFSET;
1652 133227 : iBitsRead += ALLOC_OFFSET_BITS;
1653 :
1654 133227 : return iBitsRead;
1655 : }
1656 :
1657 :
1658 133227 : static int32_t ReadLCLDData(
1659 : const int32_t *piNumGroups,
1660 : int32_t **ppiGroupLengths,
1661 : const int32_t iNumBands,
1662 : const int32_t iNumChannels,
1663 : int32_t **ppiDecodingUnresolved,
1664 : int32_t **ppiPredEnable,
1665 : const int32_t iNumSubSets,
1666 : const int32_t iSubSetId,
1667 : int32_t ***pppiAlloc,
1668 : int32_t ***pppiSignReal,
1669 : int32_t ***pppiSignImag,
1670 : int32_t ***pppiQReal,
1671 : int32_t ***pppiQImag,
1672 : int32_t **ppiDecodingFailed,
1673 : ISAR_SPLIT_REND_BITS_HANDLE pBits,
1674 : uint32_t ( *c_apauiHuffDecTables[2 * ALLOC_TABLE_SIZE] )[HUFF_DEC_TABLE_SIZE] )
1675 : {
1676 : int32_t iBitsRead;
1677 133227 : int32_t iDecodingStopped = 0;
1678 133227 : int32_t iNumLcldBands = c_aiNumLcldBandsPerBand[iNumBands - 1];
1679 : int32_t s;
1680 133227 : int32_t iSet = iSubSetId;
1681 :
1682 133227 : iBitsRead = 0;
1683 282553 : for ( s = 0; s < iNumSubSets; s++, iSet-- )
1684 : {
1685 : int32_t ch;
1686 :
1687 149326 : if ( iSet < 0 )
1688 : {
1689 5031 : iSet = iNumSubSets - 1;
1690 : }
1691 :
1692 447978 : for ( ch = 0; ch < iNumChannels; ch++ )
1693 : {
1694 : int32_t n;
1695 : int32_t iBlockOffest;
1696 :
1697 298652 : if ( ppiDecodingUnresolved[ch][iSet] == 1 )
1698 : {
1699 0 : iDecodingStopped = 1;
1700 0 : ppiDecodingFailed[ch][iSet] = 1; /* mark as not decoded (is also initialized like that when a frame is lost */
1701 : }
1702 : else
1703 : {
1704 298652 : ppiDecodingFailed[ch][iSet] = 0; /* mark as correctly decoded */
1705 : }
1706 298652 : iBlockOffest = 0;
1707 726980 : for ( n = 0; n < piNumGroups[ch]; n++ )
1708 : {
1709 : int32_t k;
1710 4691592 : for ( k = 0; k < ppiGroupLengths[ch][n]; k++ )
1711 : {
1712 : int32_t iFBOffset;
1713 :
1714 209377264 : for ( iFBOffset = iSet; iFBOffset < iNumLcldBands; iFBOffset += iNumSubSets )
1715 : {
1716 : int32_t b;
1717 : int32_t iAlloc;
1718 : int32_t iHuffDim;
1719 : int32_t iHuffMod;
1720 :
1721 205114000 : b = c_aiBandIdPerLcldBand[iFBOffset];
1722 :
1723 205114000 : iAlloc = pppiAlloc[ch][n][b];
1724 :
1725 205114000 : iHuffDim = c_aiHuffmanDim[iAlloc];
1726 205114000 : iHuffMod = c_aiHuffmanMod[iAlloc];
1727 :
1728 205114000 : if ( iDecodingStopped == 1 )
1729 : {
1730 0 : pppiQReal[ch][iBlockOffest][iFBOffset] = 0;
1731 0 : pppiQImag[ch][iBlockOffest][iFBOffset] = 0;
1732 0 : pppiSignReal[ch][iBlockOffest][iFBOffset] = 0;
1733 0 : pppiSignImag[ch][iBlockOffest][iFBOffset] = 0;
1734 : }
1735 205114000 : else if ( iAlloc > 0 )
1736 : {
1737 188115418 : const uint32_t( *pauiHuffmanTable )[HUFF_DEC_TABLE_SIZE] = NULL;
1738 188115418 : const uint32_t( *pauiHuffmanTableDPCM )[HUFF_DEC_TABLE_SIZE] = NULL;
1739 188115418 : int32_t iQuantValue1 = 0;
1740 188115418 : int32_t iQuantValue2 = 0;
1741 188115418 : pauiHuffmanTable = (const uint32_t( * )[HUFF_DEC_TABLE_SIZE]) c_apauiHuffDecTables[iAlloc];
1742 188115418 : pauiHuffmanTableDPCM = (const uint32_t( * )[HUFF_DEC_TABLE_SIZE]) c_apauiHuffDecTables[ALLOC_TABLE_SIZE + iAlloc];
1743 : #ifdef LCLD_HANDLE_PRED_START_SAMPLE
1744 : if ( ppiPredEnable[ch][iFBOffset] == 1 && ( iBlockOffest > 0 || iSet != iSubSetId ) )
1745 : #else
1746 188115418 : if ( ppiPredEnable[ch][iFBOffset] == 1 )
1747 : #endif
1748 : {
1749 11016700 : if ( iHuffDim == 2 )
1750 : {
1751 : int32_t iSymbol;
1752 3826848 : iBitsRead += ReadHuff( pauiHuffmanTableDPCM, &iSymbol, pBits );
1753 3826848 : iQuantValue1 = iSymbol / iHuffMod;
1754 3826848 : iQuantValue2 = iSymbol % iHuffMod;
1755 : }
1756 : else
1757 : {
1758 7189852 : iBitsRead += ReadHuff( pauiHuffmanTableDPCM, &iQuantValue1, pBits );
1759 7189852 : iBitsRead += ReadHuff( pauiHuffmanTableDPCM, &iQuantValue2, pBits );
1760 : }
1761 : }
1762 : else
1763 : {
1764 177098718 : if ( iHuffDim == 2 )
1765 : {
1766 : int32_t iSymbol;
1767 :
1768 131605180 : iBitsRead += ReadHuff( pauiHuffmanTable, &iSymbol, pBits );
1769 131605180 : iQuantValue1 = iSymbol / iHuffMod;
1770 131605180 : iQuantValue2 = iSymbol % iHuffMod;
1771 : }
1772 : else
1773 : {
1774 45493538 : iBitsRead += ReadHuff( pauiHuffmanTable, &iQuantValue1, pBits );
1775 45493538 : iBitsRead += ReadHuff( pauiHuffmanTable, &iQuantValue2, pBits );
1776 : }
1777 : }
1778 :
1779 188115418 : pppiQReal[ch][iBlockOffest][iFBOffset] = iQuantValue1;
1780 188115418 : pppiQImag[ch][iBlockOffest][iFBOffset] = iQuantValue2;
1781 :
1782 188115418 : if ( iQuantValue1 > 0 )
1783 : {
1784 127673299 : pppiSignReal[ch][iBlockOffest][iFBOffset] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1785 127673299 : iBitsRead += 1;
1786 : }
1787 : else
1788 : {
1789 60442119 : pppiSignReal[ch][iBlockOffest][iFBOffset] = 0;
1790 : }
1791 188115418 : if ( iQuantValue2 > 0 )
1792 : {
1793 127474297 : pppiSignImag[ch][iBlockOffest][iFBOffset] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1794 127474297 : iBitsRead += 1;
1795 : }
1796 : else
1797 : {
1798 60641121 : pppiSignImag[ch][iBlockOffest][iFBOffset] = 0;
1799 : }
1800 : }
1801 : else
1802 : {
1803 16998582 : pppiSignReal[ch][iBlockOffest][iFBOffset] = 0;
1804 16998582 : pppiSignImag[ch][iBlockOffest][iFBOffset] = 0;
1805 : }
1806 : }
1807 4263264 : iBlockOffest++;
1808 : }
1809 : }
1810 : }
1811 : }
1812 :
1813 133227 : return iBitsRead;
1814 : }
1815 :
1816 :
1817 133227 : static void ComputeAllocation(
1818 : const int32_t iChannels,
1819 : const int32_t *piNumGroups,
1820 : const int32_t iNumBands,
1821 : int32_t ***pppiSMR,
1822 : const int32_t iAllocOffset,
1823 : int32_t ***pppiAlloc )
1824 : {
1825 : int32_t b, k, n, iAlloc;
1826 :
1827 399681 : for ( n = 0; n < iChannels; n++ )
1828 : {
1829 662156 : for ( k = 0; k < piNumGroups[n]; k++ )
1830 : {
1831 9101146 : for ( b = 0; b < iNumBands; b++ )
1832 : {
1833 8705444 : iAlloc = ( ( pppiSMR[n][k][b] + iAllocOffset * ALLOC_OFFSET_SCALE ) >> 5 );
1834 8705444 : iAlloc = ( iAlloc > MIN_ALLOC ) ? iAlloc : MIN_ALLOC;
1835 8705444 : iAlloc = ( iAlloc < MAX_ALLOC ) ? iAlloc : MAX_ALLOC;
1836 8705444 : pppiAlloc[n][k][b] = iAlloc;
1837 : }
1838 : }
1839 : }
1840 :
1841 133227 : return;
1842 : }
|