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