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 3893988 : static TableNode *CreateTableList(
129 : const int32_t iReadLength )
130 : {
131 : int32_t n;
132 : int32_t iMaxTables;
133 : TableNode *ptable_top;
134 :
135 3893988 : iMaxTables = 1 << iReadLength;
136 3893988 : ptable_top = (TableNode *) malloc( sizeof( TableNode ) );
137 3893988 : ptable_top->ppoNextTable = (TableNode **) malloc( iMaxTables * sizeof( TableNode * ) );
138 3893988 : ptable_top->piCodeIndex = (int32_t *) malloc( iMaxTables * sizeof( int32_t ) );
139 3893988 : ptable_top->piDifference = (int32_t *) malloc( iMaxTables * sizeof( int32_t ) );
140 3893988 : ptable_top->piLength = (int32_t *) malloc( iMaxTables * sizeof( int32_t ) );
141 66197796 : for ( n = 0; n < iMaxTables; n++ )
142 : {
143 62303808 : ptable_top->ppoNextTable[n] = NULL;
144 62303808 : ptable_top->piCodeIndex[n] = 0xffff;
145 62303808 : ptable_top->piDifference[n] = 0;
146 62303808 : ptable_top->piLength[n] = 0;
147 : }
148 :
149 3893988 : return ptable_top;
150 : }
151 :
152 :
153 133238 : static void DeleteTableList(
154 : TableList *ptable_list,
155 : int32_t iTables )
156 : {
157 :
158 : TableNode *node;
159 133238 : node = ptable_list->poOrderedTop;
160 :
161 4027226 : while ( iTables )
162 : {
163 3893988 : TableNode *node1 = node;
164 3893988 : node = node1->poOrderedNext;
165 3893988 : if ( node1->piCodeIndex != NULL )
166 : {
167 3893988 : free( node1->piCodeIndex );
168 : }
169 3893988 : if ( node1->piLength != NULL )
170 : {
171 3893988 : free( node1->piLength );
172 : }
173 3893988 : if ( node1->piDifference != NULL )
174 : {
175 3893988 : free( node1->piDifference );
176 : }
177 3893988 : if ( node1->ppoNextTable != NULL )
178 : {
179 3893988 : free( node1->ppoNextTable );
180 : }
181 3893988 : if ( node1 != NULL )
182 : {
183 3893988 : free( node1 );
184 : }
185 3893988 : iTables--;
186 : }
187 :
188 133238 : if ( ptable_list != NULL )
189 : {
190 133238 : free( ptable_list );
191 : }
192 :
193 133238 : return;
194 : }
195 :
196 :
197 69391210 : 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 69391210 : if ( poParent->ppoNextTable[iIndex] == NULL )
207 : {
208 3760750 : poNextNode = CreateTableList( iReadLength );
209 3760750 : poParent->ppoNextTable[iIndex] = poNextNode;
210 3760750 : poParent->piDifference[iIndex] = *iTablesCreated; /* this is a link to the next table rather than the difference */
211 3760750 : table_list->poOrderedBottom->poOrderedNext = poNextNode;
212 3760750 : table_list->poOrderedBottom = poNextNode;
213 :
214 3760750 : ( *iTablesCreated )++;
215 : }
216 : else
217 : {
218 65630460 : poNextNode = poParent->ppoNextTable[iIndex];
219 : }
220 :
221 69391210 : return poNextNode;
222 : }
223 :
224 :
225 133238 : 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 133238 : iMaxTables = 1 << iReadLength;
237 133238 : psLCLDDecoder->c_apauiHuffDecTable_RAM[n] = malloc( iTablesCreated * iMaxTables * sizeof( uint32_t ) );
238 :
239 133238 : poNode = ptable_list->poOrderedTop;
240 4027226 : for ( j = 0; j < iTablesCreated; j++ )
241 : {
242 : int32_t k;
243 3893988 : if ( poNode != NULL )
244 : {
245 66197796 : for ( k = 0; k < iMaxTables; k++ )
246 : {
247 : uint32_t uiCode;
248 62303808 : uiCode = poNode->piDifference[k];
249 62303808 : uiCode <<= 16;
250 62303808 : uiCode |= poNode->piCodeIndex[k];
251 62303808 : psLCLDDecoder->c_apauiHuffDecTable_RAM[n][j][k] = uiCode;
252 : }
253 : }
254 3893988 : poNode = poNode->poOrderedNext;
255 : }
256 :
257 133238 : return;
258 : }
259 :
260 :
261 21085988 : 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 21085988 : poNode = ptable_list->poOrderedTop;
278 21085988 : iMask = ( 1 << iReadLength ) - 1;
279 21085988 : iCurrentLength = iLength;
280 90477198 : while ( iCurrentLength > iReadLength )
281 : {
282 69391210 : iDifference = iCurrentLength - iReadLength;
283 69391210 : iIndex = iCode >> iDifference;
284 69391210 : iIndex &= iMask;
285 69391210 : poNode = GetNextTable( iIndex, ptable_list, poNode, iReadLength, iTables );
286 69391210 : iCurrentLength -= iReadLength;
287 : }
288 :
289 21085988 : iMask = ( 1 << iCurrentLength ) - 1;
290 21085988 : iDifference = iReadLength - iCurrentLength;
291 21085988 : iCodeLow = ( iCode & iMask ) << iDifference;
292 21085988 : iMask = ( 1 << iDifference ) - 1;
293 21085988 : iCodeHigh = iCodeLow | iMask;
294 79629046 : for ( iIndex = iCodeLow; iIndex <= iCodeHigh; iIndex++ )
295 : {
296 58543058 : poNode->piCodeIndex[iIndex] = iCodeIndex;
297 58543058 : poNode->piDifference[iIndex] = iDifference;
298 58543058 : poNode->piLength[iIndex] = iLength;
299 : }
300 :
301 21085988 : return;
302 : }
303 :
304 :
305 133238 : 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 133238 : ptable_list = (TableList *) malloc( sizeof( TableList ) );
318 :
319 133238 : ppsort_enc_table = (uint32_t **) malloc( iSize * sizeof( int32_t * ) );
320 :
321 21219226 : for ( n = 0; n < iSize; n++ )
322 : {
323 21085988 : ppsort_enc_table[n] = (uint32_t *) malloc( 3 * sizeof( int32_t ) );
324 21085988 : ppsort_enc_table[n][0] = (uint32_t) ppuiEncTable[n][0];
325 21085988 : ppsort_enc_table[n][1] = (uint32_t) ppuiEncTable[n][1];
326 21085988 : ppsort_enc_table[n][2] = (uint32_t) n;
327 : }
328 :
329 21219226 : for ( n = 0; n < iSize; n++ )
330 : {
331 : uint32_t iMin;
332 : int32_t iMinIndex;
333 : int32_t k;
334 :
335 21085988 : iMin = ppsort_enc_table[n][0];
336 21085988 : iMinIndex = n;
337 4223739156 : for ( k = n; k < iSize; k++ )
338 : {
339 4202653168 : if ( ppsort_enc_table[k][0] < iMin )
340 : {
341 10121790 : iMin = ppsort_enc_table[k][0];
342 10121790 : iMinIndex = k;
343 : }
344 : }
345 :
346 21085988 : if ( iMinIndex != n )
347 : {
348 : uint32_t uiLength;
349 : uint32_t uiCode;
350 : uint32_t uiCodeIndex;
351 :
352 5381096 : uiLength = ppsort_enc_table[n][0];
353 5381096 : uiCode = ppsort_enc_table[n][1];
354 5381096 : uiCodeIndex = ppsort_enc_table[n][2];
355 :
356 5381096 : ppsort_enc_table[n][0] = ppsort_enc_table[iMinIndex][0];
357 5381096 : ppsort_enc_table[n][1] = ppsort_enc_table[iMinIndex][1];
358 5381096 : ppsort_enc_table[n][2] = ppsort_enc_table[iMinIndex][2];
359 :
360 5381096 : ppsort_enc_table[iMinIndex][0] = uiLength;
361 5381096 : ppsort_enc_table[iMinIndex][1] = uiCode;
362 5381096 : ppsort_enc_table[iMinIndex][2] = uiCodeIndex;
363 : }
364 : }
365 133238 : ptable_list->poOrderedTop = CreateTableList( iReadLength );
366 133238 : ptable_list->poOrderedBottom = ptable_list->poOrderedTop;
367 21219226 : for ( n = 0; n < iSize; n++ )
368 : {
369 : int32_t iLength;
370 : int32_t iCode;
371 : int32_t iCodeIndex;
372 :
373 21085988 : iLength = ppsort_enc_table[n][0];
374 21085988 : iCode = ppsort_enc_table[n][1];
375 21085988 : iCodeIndex = ppsort_enc_table[n][2];
376 21085988 : AddcodeTableList( ptable_list, iLength, iCode, iCodeIndex, iReadLength, iTables );
377 : }
378 :
379 133238 : CompleteTables( psLCLDDecoder, num, ptable_list, iReadLength, *iTables );
380 133238 : DeleteTableList( ptable_list, *iTables );
381 21219226 : for ( n = 0; n < iSize; n++ )
382 : {
383 21085988 : free( ppsort_enc_table[n] );
384 : }
385 :
386 133238 : free( ppsort_enc_table );
387 :
388 133238 : return;
389 : }
390 :
391 :
392 : /*------------------------------------------------------------------------------------------*
393 : * Function CreateLCLDDecoder()
394 : *
395 : *
396 : *------------------------------------------------------------------------------------------*/
397 :
398 2149 : 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 2149 : LCLDDecoder *psLCLDDecoder = NULL;
409 :
410 2149 : assert( iSampleRate == 48000 );
411 2149 : assert( iNumBlocks == 16 || iNumBlocks == 8 || iNumBlocks == 4 );
412 2149 : 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 2149 : psLCLDDecoder->iSampleRate = iSampleRate;
417 2149 : psLCLDDecoder->iChannels = iChannels;
418 2149 : psLCLDDecoder->iAllocOffset = 0;
419 2149 : psLCLDDecoder->iRealOnlyOut = iRealOnlyOut;
420 2149 : if ( iRealOnlyOut == 1 )
421 : {
422 0 : psLCLDDecoder->iNumBlocks = iNumBlocks / 2;
423 : }
424 : else
425 : {
426 2149 : psLCLDDecoder->iNumBlocks = iNumBlocks;
427 : }
428 2149 : psLCLDDecoder->iNumBands = 0; /* read from bitstream*/
429 2149 : psLCLDDecoder->piBandwidths = c_aiBandwidths48;
430 :
431 2149 : psLCLDDecoder->iMSMode = 0;
432 2149 : 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 51576 : for ( n = 0; n < MAX_BANDS; n++ )
437 : {
438 49427 : psLCLDDecoder->piLRPhaseDiffs[n] = 0;
439 49427 : psLCLDDecoder->piMSPredCoefs[n] = 0;
440 : }
441 :
442 2149 : psLCLDDecoder->iCommonGrouping = 1; /* Common grouping always on only impacts stereo */
443 2149 : 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 2149 : 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 2149 : 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 2149 : 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 2149 : 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 2149 : 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 2149 : 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 2149 : 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 2149 : 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 2149 : 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 6447 : for ( n = 0; n < iChannels; n++ )
486 : {
487 : int16_t k;
488 4298 : 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 4298 : 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 4298 : 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 4298 : 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 4298 : 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 4298 : 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 4298 : 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 4298 : 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 4298 : 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 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
527 : {
528 68768 : 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 68768 : 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 68768 : 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 68768 : 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 68768 : 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 68768 : 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 68768 : 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 68768 : 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 2149 : read_length = READ_LENGTH;
565 139685 : for ( n = 0; n < ALLOC_TABLE_SIZE * 2; n++ )
566 : {
567 137536 : psLCLDDecoder->num_decode_table[n] = 1;
568 137536 : if ( c_apauiHuffEncTabels[n] != NULL )
569 : {
570 :
571 133238 : CreateDecodeTable( psLCLDDecoder, n, c_apauiHuffEncTabels[n], num_row_aauiLCLDHuff[n], read_length, &psLCLDDecoder->num_decode_table[n] );
572 : }
573 : else
574 : {
575 4298 : psLCLDDecoder->c_apauiHuffDecTable_RAM[n] = NULL;
576 : }
577 : }
578 :
579 2149 : if ( ( error = CreatePredictionDecoder( &psLCLDDecoder->psPredictionDecoder, iChannels, psLCLDDecoder->iNumBlocks ) ) != IVAS_ERR_OK )
580 : {
581 0 : return error;
582 : }
583 2149 : *psLCLDDecoder_out = psLCLDDecoder;
584 :
585 2149 : return IVAS_ERR_OK;
586 : }
587 :
588 :
589 : /*------------------------------------------------------------------------------------------*
590 : * Function CreateLCLDDecoder()
591 : *
592 : *
593 : *------------------------------------------------------------------------------------------*/
594 :
595 2149 : void DeleteLCLDDecoder(
596 : LCLDDecoder *psLCLDDecoder )
597 : {
598 : int32_t k, n;
599 :
600 2149 : if ( psLCLDDecoder != NULL )
601 : {
602 2149 : if ( psLCLDDecoder->piMSFlags != NULL )
603 : {
604 2149 : free( psLCLDDecoder->piMSFlags );
605 : }
606 :
607 2149 : if ( psLCLDDecoder->piNumGroups != NULL )
608 : {
609 2149 : free( psLCLDDecoder->piNumGroups );
610 : }
611 :
612 2149 : if ( psLCLDDecoder->ppiGroupLengths != NULL )
613 : {
614 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
615 : {
616 4298 : free( psLCLDDecoder->ppiGroupLengths[n] );
617 : }
618 2149 : free( psLCLDDecoder->ppiGroupLengths );
619 : }
620 :
621 2149 : if ( psLCLDDecoder->pppiRMSEnvelope != NULL )
622 : {
623 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
624 : {
625 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
626 : {
627 68768 : free( psLCLDDecoder->pppiRMSEnvelope[n][k] );
628 : }
629 4298 : free( psLCLDDecoder->pppiRMSEnvelope[n] );
630 : }
631 2149 : free( psLCLDDecoder->pppiRMSEnvelope );
632 : }
633 :
634 2149 : if ( psLCLDDecoder->pppiSMR != NULL )
635 : {
636 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
637 : {
638 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
639 : {
640 68768 : free( psLCLDDecoder->pppiSMR[n][k] );
641 : }
642 4298 : free( psLCLDDecoder->pppiSMR[n] );
643 : }
644 2149 : free( psLCLDDecoder->pppiSMR );
645 : }
646 :
647 2149 : if ( psLCLDDecoder->pppiExcitation != NULL )
648 : {
649 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
650 : {
651 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
652 : {
653 68768 : free( psLCLDDecoder->pppiExcitation[n][k] );
654 : }
655 4298 : free( psLCLDDecoder->pppiExcitation[n] );
656 : }
657 2149 : free( psLCLDDecoder->pppiExcitation );
658 : }
659 :
660 :
661 2149 : if ( psLCLDDecoder->pppiAlloc != NULL )
662 : {
663 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
664 : {
665 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
666 : {
667 68768 : free( psLCLDDecoder->pppiAlloc[n][k] );
668 : }
669 4298 : free( psLCLDDecoder->pppiAlloc[n] );
670 : }
671 2149 : free( psLCLDDecoder->pppiAlloc );
672 : }
673 :
674 2149 : if ( psLCLDDecoder->pppiLCLDSignReal != NULL )
675 : {
676 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
677 : {
678 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
679 : {
680 68768 : free( psLCLDDecoder->pppiLCLDSignReal[n][k] );
681 : }
682 4298 : free( psLCLDDecoder->pppiLCLDSignReal[n] );
683 : }
684 2149 : free( psLCLDDecoder->pppiLCLDSignReal );
685 : }
686 :
687 2149 : if ( psLCLDDecoder->pppiLCLDSignImag != NULL )
688 : {
689 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
690 : {
691 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
692 : {
693 68768 : free( psLCLDDecoder->pppiLCLDSignImag[n][k] );
694 : }
695 4298 : free( psLCLDDecoder->pppiLCLDSignImag[n] );
696 : }
697 2149 : free( psLCLDDecoder->pppiLCLDSignImag );
698 : }
699 :
700 2149 : if ( psLCLDDecoder->pppiQLCLDReal != NULL )
701 : {
702 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
703 : {
704 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
705 : {
706 68768 : free( psLCLDDecoder->pppiQLCLDReal[n][k] );
707 : }
708 4298 : free( psLCLDDecoder->pppiQLCLDReal[n] );
709 : }
710 2149 : free( psLCLDDecoder->pppiQLCLDReal );
711 : }
712 :
713 2149 : if ( psLCLDDecoder->pppiQLCLDImag != NULL )
714 : {
715 6447 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
716 : {
717 73066 : for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
718 : {
719 68768 : free( psLCLDDecoder->pppiQLCLDImag[n][k] );
720 : }
721 4298 : free( psLCLDDecoder->pppiQLCLDImag[n] );
722 : }
723 2149 : free( psLCLDDecoder->pppiQLCLDImag );
724 : }
725 :
726 139685 : for ( n = 0; n < ALLOC_TABLE_SIZE * 2; n++ )
727 : {
728 137536 : if ( psLCLDDecoder->num_decode_table[n] > 1 )
729 : {
730 :
731 133238 : if ( psLCLDDecoder->c_apauiHuffDecTable_RAM[n] != NULL )
732 : {
733 133238 : free( psLCLDDecoder->c_apauiHuffDecTable_RAM[n] );
734 : }
735 : }
736 : }
737 :
738 2149 : if ( psLCLDDecoder->psPredictionDecoder != NULL )
739 : {
740 2149 : DeletePredictionDecoder( psLCLDDecoder->psPredictionDecoder );
741 2149 : psLCLDDecoder->psPredictionDecoder = NULL;
742 : }
743 :
744 :
745 2149 : free( psLCLDDecoder );
746 : }
747 :
748 2149 : 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 463 : void SetDecodingUnresolved(
790 : LCLDDecoder *psLCLDDecoder )
791 : {
792 : int32_t n, ch;
793 463 : PredictionDecoder *psPredictionDecoder = psLCLDDecoder->psPredictionDecoder;
794 :
795 1389 : for ( ch = 0; ch < psPredictionDecoder->iChannels; ch++ )
796 : {
797 1852 : for ( n = 0; n < psPredictionDecoder->iNumSubSets; n++ )
798 : {
799 926 : psPredictionDecoder->ppiDecodingUnresolved[ch][n] = 1;
800 926 : psPredictionDecoder->ppiDecodingFailed[ch][n] = 1;
801 926 : psPredictionDecoder->ppiDecodingFailedPrev[ch][n] = 1;
802 : }
803 : }
804 :
805 463 : return;
806 : }
807 :
808 :
809 : /*------------------------------------------------------------------------------------------*
810 : * function AnyDecodingFailedPrev()
811 : *
812 : *
813 : *------------------------------------------------------------------------------------------*/
814 :
815 335441 : int16_t AnyDecodingFailedPrev(
816 : LCLDDecoder *psLCLDDecoder )
817 : {
818 : int32_t n, ch;
819 335441 : PredictionDecoder *psPredictionDecoder = psLCLDDecoder->psPredictionDecoder;
820 :
821 1005779 : for ( ch = 0; ch < psPredictionDecoder->iChannels; ch++ )
822 : {
823 1431040 : for ( n = 0; n < psPredictionDecoder->iNumSubSets; n++ )
824 : {
825 760702 : if ( psPredictionDecoder->ppiDecodingFailedPrev[ch][n] == 1 )
826 : {
827 272 : return 1;
828 : }
829 : }
830 : }
831 :
832 335169 : return 0;
833 : }
834 :
835 : /*------------------------------------------------------------------------------------------*
836 : * function AnyDecodingFailed()
837 : *
838 : *
839 : *------------------------------------------------------------------------------------------*/
840 :
841 335441 : int16_t AnyDecodingFailed(
842 : LCLDDecoder *psLCLDDecoder )
843 : {
844 : int32_t n, ch;
845 335441 : PredictionDecoder *psPredictionDecoder = psLCLDDecoder->psPredictionDecoder;
846 :
847 1006323 : for ( ch = 0; ch < psPredictionDecoder->iChannels; ch++ )
848 : {
849 1431856 : for ( n = 0; n < psPredictionDecoder->iNumSubSets; n++ )
850 : {
851 760974 : if ( psPredictionDecoder->ppiDecodingFailed[ch][n] == 1 )
852 : {
853 0 : return 1;
854 : }
855 : }
856 : }
857 :
858 335441 : return 0;
859 : }
860 :
861 :
862 : /*------------------------------------------------------------------------------------------*
863 : * function GetDecodingFailedStatus()
864 : *
865 : *
866 : *------------------------------------------------------------------------------------------*/
867 :
868 735 : int32_t **GetDecodingFailedStatus(
869 : LCLDDecoder *psLCLDDecoder )
870 : {
871 735 : return psLCLDDecoder->psPredictionDecoder->ppiDecodingFailed;
872 : }
873 :
874 :
875 : /*------------------------------------------------------------------------------------------*
876 : * function GetNumSubSets()
877 : *
878 : *
879 : *------------------------------------------------------------------------------------------*/
880 :
881 2149 : int16_t GetNumSubSets(
882 : LCLDDecoder *psLCLDDecoder )
883 : {
884 2149 : return (int16_t) psLCLDDecoder->psPredictionDecoder->iNumSubSets;
885 : }
886 :
887 :
888 : /*------------------------------------------------------------------------------------------*
889 : * function GetDecodingFailedPrevStatus()
890 : *
891 : *
892 : *------------------------------------------------------------------------------------------*/
893 :
894 272 : int32_t **GetDecodingFailedPrevStatus(
895 : LCLDDecoder *psLCLDDecoder )
896 : {
897 272 : 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 335441 : 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 335441 : ReadHeaderInformation( &psLCLDDecoder->iNumBands, pBits );
950 :
951 335441 : if ( psLCLDDecoder->iChannels == 2 )
952 : {
953 335441 : ReadMSInformation( psLCLDDecoder->iNumBands, &psLCLDDecoder->iMSMode, psLCLDDecoder->piMSFlags, psLCLDDecoder->piLRPhaseDiffs, psLCLDDecoder->piMSPredCoefs, pBits );
954 : }
955 :
956 335441 : ReadPredictors( psLCLDDecoder->psPredictionDecoder, pBits );
957 335441 : UpdateDecodingUnresolved( psLCLDDecoder->psPredictionDecoder );
958 335441 : UpdateDecodingFailedStatus( psLCLDDecoder->psPredictionDecoder );
959 :
960 335441 : ReadGroupInformation( psLCLDDecoder->iChannels, psLCLDDecoder->iNumBlocks, &psLCLDDecoder->iCommonGrouping, psLCLDDecoder->piNumGroups, psLCLDDecoder->ppiGroupLengths, pBits );
961 :
962 335441 : ReadRMSEnvelope( psLCLDDecoder->iChannels, (const int32_t *) psLCLDDecoder->piNumGroups, psLCLDDecoder->iNumBands, psLCLDDecoder->pppiRMSEnvelope, pBits );
963 :
964 335441 : ReadAllocInformation( &psLCLDDecoder->iAllocOffset, pBits );
965 :
966 335441 : if ( psLCLDDecoder->iChannels == 2 && psLCLDDecoder->iCommonGrouping == 1 )
967 : { /* MS Mode? */
968 977760 : for ( k = 0; k < psLCLDDecoder->piNumGroups[0]; k++ )
969 : {
970 642319 : PerceptualModelStereo( psLCLDDecoder->iNumBands, psLCLDDecoder->piMSFlags,
971 642319 : psLCLDDecoder->pppiRMSEnvelope[0][k],
972 642319 : psLCLDDecoder->pppiRMSEnvelope[1][k],
973 642319 : psLCLDDecoder->pppiExcitation[0][k],
974 642319 : psLCLDDecoder->pppiExcitation[1][k],
975 642319 : psLCLDDecoder->pppiSMR[0][k],
976 642319 : 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 335441 : ComputeAllocation( psLCLDDecoder->iChannels, (const int32_t *) psLCLDDecoder->piNumGroups, psLCLDDecoder->iNumBands, psLCLDDecoder->pppiSMR, psLCLDDecoder->iAllocOffset, psLCLDDecoder->pppiAlloc );
991 :
992 335441 : ReadLCLDData(
993 335441 : psLCLDDecoder->piNumGroups,
994 : psLCLDDecoder->ppiGroupLengths,
995 : psLCLDDecoder->iNumBands,
996 : psLCLDDecoder->iChannels,
997 335441 : psLCLDDecoder->psPredictionDecoder->ppiDecodingUnresolved,
998 335441 : psLCLDDecoder->psPredictionDecoder->ppiPredBandEnable,
999 335441 : psLCLDDecoder->psPredictionDecoder->iNumSubSets,
1000 335441 : psLCLDDecoder->psPredictionDecoder->iSubSetId,
1001 : psLCLDDecoder->pppiAlloc,
1002 : psLCLDDecoder->pppiLCLDSignReal,
1003 : psLCLDDecoder->pppiLCLDSignImag,
1004 : psLCLDDecoder->pppiQLCLDReal,
1005 : psLCLDDecoder->pppiQLCLDImag,
1006 335441 : psLCLDDecoder->psPredictionDecoder->ppiDecodingFailed,
1007 : pBits,
1008 335441 : psLCLDDecoder->c_apauiHuffDecTable_RAM );
1009 :
1010 1006323 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
1011 : {
1012 670882 : InvQuantizeSpectrum( psLCLDDecoder->piNumGroups[n],
1013 670882 : (const int32_t *) psLCLDDecoder->ppiGroupLengths[n],
1014 : psLCLDDecoder->iNumBands, psLCLDDecoder->piBandwidths,
1015 670882 : psLCLDDecoder->pppiAlloc[n],
1016 670882 : psLCLDDecoder->pppiQLCLDReal[n],
1017 670882 : psLCLDDecoder->pppiQLCLDImag[n],
1018 670882 : pppfLCLDReal[n], pppfLCLDImag[n] );
1019 :
1020 670882 : ReplaceSign( psLCLDDecoder->iNumBlocks, psLCLDDecoder->iNumBands,
1021 670882 : psLCLDDecoder->pppiLCLDSignReal[n],
1022 670882 : psLCLDDecoder->pppiLCLDSignImag[n],
1023 670882 : 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 335441 : ApplyInversePredictors( psLCLDDecoder->psPredictionDecoder, pppfLCLDReal, pppfLCLDImag );
1041 :
1042 1006323 : for ( n = 0; n < psLCLDDecoder->iChannels; n++ )
1043 : {
1044 670882 : ApplyRMSEnvelope( psLCLDDecoder->iNumBands, psLCLDDecoder->piBandwidths,
1045 670882 : psLCLDDecoder->piNumGroups[n],
1046 670882 : (const int32_t *) psLCLDDecoder->ppiGroupLengths[n],
1047 670882 : psLCLDDecoder->pppiRMSEnvelope[n],
1048 670882 : pppfLCLDReal[n], pppfLCLDImag[n] );
1049 : }
1050 :
1051 335441 : if ( psLCLDDecoder->iChannels == 2 && psLCLDDecoder->iMSMode > 0 )
1052 : {
1053 334466 : InvMSCoding( psLCLDDecoder->iNumBlocks, psLCLDDecoder->iNumBands,
1054 : psLCLDDecoder->piBandwidths, psLCLDDecoder->iMSMode,
1055 334466 : (const int32_t *) psLCLDDecoder->piMSFlags,
1056 334466 : (const int32_t *) psLCLDDecoder->piLRPhaseDiffs,
1057 334466 : (const int32_t *) psLCLDDecoder->piMSPredCoefs,
1058 : pppfLCLDReal, pppfLCLDImag );
1059 : }
1060 :
1061 335441 : if ( psLCLDDecoder->iRealOnlyOut == 1 )
1062 : {
1063 0 : UnpackReal( psLCLDDecoder->iChannels,
1064 0 : psLCLDDecoder->iNumBlocks * 2,
1065 : pppfLCLDReal,
1066 : pppfLCLDImag );
1067 : }
1068 :
1069 335441 : return AnyDecodingUnresolved( psLCLDDecoder->psPredictionDecoder );
1070 : }
1071 :
1072 :
1073 : /*------------------------------------------------------------------------------------------*
1074 : * Local functions
1075 : *
1076 : *
1077 : *------------------------------------------------------------------------------------------*/
1078 :
1079 670882 : 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 670882 : iBlockOffset = 0;
1092 1955520 : for ( n = 0; n < iNumGroups; n++ )
1093 : {
1094 11568302 : for ( k = 0; k < piGroupLengths[n]; k++ )
1095 : {
1096 10283664 : iFBOffset = 0;
1097 236524272 : for ( b = 0; b < iNumBands; b++ )
1098 : {
1099 : int32_t m;
1100 : int32_t iRMSEnv;
1101 : float fGain;
1102 :
1103 226240608 : iRMSEnv = ppiRMSEnvelope[n][b];
1104 226240608 : fGain =
1105 226240608 : c_afRMSEnvReconstructTable[ENV_RECONSTRUCT_TABLE_CENTER + iRMSEnv];
1106 740423808 : for ( m = 0; m < piBandwidths[b]; m++ )
1107 : {
1108 514183200 : ppfReal[iBlockOffset][iFBOffset] *= fGain;
1109 514183200 : ppfImag[iBlockOffset][iFBOffset] *= fGain;
1110 514183200 : iFBOffset++;
1111 : }
1112 : }
1113 10283664 : iBlockOffset++;
1114 : }
1115 : }
1116 :
1117 670882 : return;
1118 : }
1119 :
1120 :
1121 670882 : 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 10954546 : for ( n = 0; n < iNumBlocks; n++ )
1134 : {
1135 10283664 : idx = 0;
1136 236524272 : for ( b = 0; b < iNumLCLDBands; b++ )
1137 : {
1138 740423808 : for ( m = 0; m < piBandwidths[b]; m++ )
1139 : {
1140 514183200 : if ( ppiSignReal[n][idx] == 1 )
1141 : {
1142 153110869 : ppfReal[n][idx] = -ppfReal[n][idx];
1143 : }
1144 :
1145 514183200 : if ( ppiSignImag[n][idx] == 1 )
1146 : {
1147 152962158 : ppfImag[n][idx] = -ppfImag[n][idx];
1148 : }
1149 514183200 : idx++;
1150 : }
1151 : }
1152 : }
1153 :
1154 670882 : return;
1155 : }
1156 :
1157 :
1158 670882 : 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 670882 : iBlockOffest = 0;
1173 1955520 : for ( n = 0; n < iNumGroups; n++ )
1174 : {
1175 11568302 : for ( k = 0; k < piGroupLengths[n]; k++ )
1176 : {
1177 10283664 : iFBOffset = 0;
1178 236524272 : for ( b = 0; b < iNumBands; b++ )
1179 : {
1180 : int32_t m;
1181 : int32_t iAlloc;
1182 : float fInvSCFGain;
1183 :
1184 226240608 : iAlloc = ppiAlloc[n][b];
1185 226240608 : fInvSCFGain = c_afInvScaleFactor[iAlloc];
1186 :
1187 226240608 : if ( iAlloc > 0 )
1188 : {
1189 675759498 : for ( m = 0; m < piBandwidths[b]; m++ )
1190 : {
1191 : int32_t iQuantValue;
1192 :
1193 462944040 : iQuantValue = ppiQReal[iBlockOffest][iFBOffset];
1194 462944040 : ppfReal[iBlockOffest][iFBOffset] = (float) iQuantValue * fInvSCFGain;
1195 :
1196 462944040 : iQuantValue = ppiQImag[iBlockOffest][iFBOffset];
1197 462944040 : ppfImag[iBlockOffest][iFBOffset] = (float) iQuantValue * fInvSCFGain;
1198 :
1199 462944040 : iFBOffset++;
1200 : }
1201 : }
1202 : else
1203 : {
1204 13425150 : iFBOffset += piBandwidths[b];
1205 : }
1206 : }
1207 :
1208 10283664 : iBlockOffest++;
1209 : }
1210 : }
1211 :
1212 670882 : return;
1213 : }
1214 :
1215 :
1216 334466 : 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 334466 : if ( iMSMode > 0 )
1228 : {
1229 : int32_t b;
1230 : int32_t iFBOffset;
1231 334466 : int32_t bMSPred = 0;
1232 :
1233 334466 : iFBOffset = 0;
1234 7692718 : for ( b = 0; b < iNumBands; b++ )
1235 : {
1236 7358252 : if ( piMSFlags[b] == 1 )
1237 : {
1238 : int32_t n;
1239 : int32_t phaseIdx;
1240 : float fPred;
1241 :
1242 5042364 : phaseIdx = piLRPhaseDiffs[bMSPred] - PHASE_MIN_VAL;
1243 5042364 : fPred = dequantPred( piMSPredCoefs[bMSPred] );
1244 16042667 : for ( n = 0; n < piBandwidths[b]; n++ )
1245 : {
1246 : int32_t k;
1247 179958279 : for ( k = 0; k < iNumBlocks; k++ )
1248 : {
1249 : float fLeftReal;
1250 : float fLeftImag;
1251 : float fRightReal;
1252 : float fRightImag;
1253 :
1254 168957976 : if ( iMSMode == 3 )
1255 : {
1256 110890424 : pppfReal[1][k][iFBOffset] += fPred * pppfReal[0][k][iFBOffset];
1257 110890424 : pppfImag[1][k][iFBOffset] += fPred * pppfImag[0][k][iFBOffset];
1258 : }
1259 :
1260 168957976 : fLeftReal = ( pppfReal[0][k][iFBOffset] + pppfReal[1][k][iFBOffset] );
1261 168957976 : fLeftImag = ( pppfImag[0][k][iFBOffset] + pppfImag[1][k][iFBOffset] );
1262 168957976 : fRightReal = ( pppfReal[0][k][iFBOffset] - pppfReal[1][k][iFBOffset] );
1263 168957976 : fRightImag = ( pppfImag[0][k][iFBOffset] - pppfImag[1][k][iFBOffset] );
1264 :
1265 168957976 : if ( iMSMode == 3 )
1266 : {
1267 110890424 : cplxmult_lcld( &fRightReal, &fRightImag, c_afRotRealImag[phaseIdx][0], -c_afRotRealImag[phaseIdx][1] );
1268 : }
1269 :
1270 168957976 : pppfReal[0][k][iFBOffset] = fLeftReal;
1271 168957976 : pppfReal[1][k][iFBOffset] = fRightReal;
1272 168957976 : pppfImag[0][k][iFBOffset] = fLeftImag;
1273 168957976 : pppfImag[1][k][iFBOffset] = fRightImag;
1274 : }
1275 11000303 : iFBOffset++;
1276 : }
1277 5042364 : bMSPred++;
1278 : }
1279 : else
1280 : {
1281 2315888 : iFBOffset += piBandwidths[b];
1282 : }
1283 : }
1284 : }
1285 :
1286 334466 : return;
1287 : }
1288 :
1289 :
1290 335441 : static int32_t ReadHeaderInformation(
1291 : int32_t *piNumBands,
1292 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1293 : {
1294 : int32_t iBitsRead;
1295 :
1296 335441 : iBitsRead = 0;
1297 335441 : *piNumBands = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 5 );
1298 335441 : iBitsRead += 5;
1299 :
1300 335441 : return iBitsRead;
1301 : }
1302 :
1303 :
1304 335441 : 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 335441 : iBitsRead = 0;
1315 335441 : *piMSMode = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 2 );
1316 335441 : iBitsRead += 2;
1317 :
1318 335441 : if ( *piMSMode == 0 )
1319 : {
1320 : int32_t n;
1321 22425 : for ( n = 0; n < iNumBands; n++ )
1322 : {
1323 21450 : piMSFlags[n] = 0;
1324 : }
1325 : }
1326 334466 : else if ( *piMSMode == 1 )
1327 : {
1328 : int32_t n;
1329 149799 : for ( n = 0; n < iNumBands; n++ )
1330 : {
1331 143286 : piMSFlags[n] = 1;
1332 : }
1333 : }
1334 327953 : else if ( *piMSMode == 2 )
1335 : {
1336 : int32_t n;
1337 2762599 : for ( n = 0; n < iNumBands; n++ )
1338 : {
1339 2642486 : piMSFlags[n] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1340 2642486 : iBitsRead += 1;
1341 : }
1342 : }
1343 207840 : else if ( *piMSMode == 3 )
1344 : {
1345 : int32_t n;
1346 : int32_t iMSPredAll;
1347 207840 : int32_t iNumMSPredBands = 0;
1348 : int32_t anyNonZero;
1349 207840 : iMSPredAll = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1350 207840 : iBitsRead += 1;
1351 207840 : if ( iMSPredAll )
1352 : {
1353 24720 : iNumMSPredBands = iNumBands;
1354 568560 : for ( n = 0; n < iNumBands; n++ )
1355 : {
1356 543840 : piMSFlags[n] = 1;
1357 : }
1358 : }
1359 : else
1360 : {
1361 4211760 : for ( n = 0; n < iNumBands; n++ )
1362 : {
1363 4028640 : piMSFlags[n] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1364 4028640 : iBitsRead += 1;
1365 4028640 : if ( piMSFlags[n] )
1366 : {
1367 2743209 : iNumMSPredBands++;
1368 : }
1369 : }
1370 : }
1371 :
1372 207840 : anyNonZero = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1373 :
1374 207840 : if ( anyNonZero )
1375 : {
1376 158278 : piLRPhaseDiffs[0] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, PHASE_BAND0_BITS );
1377 158278 : piLRPhaseDiffs[0] += PHASE_MIN_VAL;
1378 158278 : iBitsRead += PHASE_BAND0_BITS;
1379 2437148 : for ( n = 1; n < iNumMSPredBands; n++ )
1380 : {
1381 : int32_t tabIdx;
1382 2278870 : iBitsRead += ReadHuff( c_aaiRMSEnvHuffDec, &tabIdx, pBits );
1383 2278870 : piLRPhaseDiffs[n] = tabIdx + ENV_DELTA_MIN;
1384 : }
1385 158278 : DecodePhase( piLRPhaseDiffs, iNumMSPredBands, PHASE_DIFF_DIM );
1386 : }
1387 : else
1388 : {
1389 899463 : for ( n = 0; n < iNumMSPredBands; n++ )
1390 : {
1391 849901 : piLRPhaseDiffs[n] = 0;
1392 : }
1393 : }
1394 :
1395 207840 : anyNonZero = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1396 :
1397 207840 : if ( anyNonZero )
1398 : {
1399 199853 : piMSPredCoefs[0] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, PRED_BAND0_BITS );
1400 199853 : piMSPredCoefs[0] += PRED_MIN_VAL;
1401 199853 : iBitsRead += PRED_BAND0_BITS;
1402 3231535 : for ( n = 1; n < iNumMSPredBands; n++ )
1403 : {
1404 : int32_t tabIdx;
1405 3031682 : iBitsRead += ReadHuff( c_aaiRMSEnvHuffDec, &tabIdx, pBits );
1406 3031682 : piMSPredCoefs[n] = tabIdx + ENV_DELTA_MIN;
1407 : }
1408 199853 : DecodePredCoef( piMSPredCoefs, iNumMSPredBands );
1409 : }
1410 : else
1411 : {
1412 63501 : for ( n = 0; n < iNumMSPredBands; n++ )
1413 : {
1414 55514 : 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 335441 : return iBitsRead;
1432 : }
1433 :
1434 :
1435 335441 : 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 335441 : iBitsRead = 0;
1446 335441 : if ( iChannels == 2 )
1447 : {
1448 335441 : *piCommonGrouping = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1449 335441 : iBitsRead += 1;
1450 :
1451 335441 : if ( *piCommonGrouping == 1 )
1452 : {
1453 335441 : piNumGroups[0] = 0;
1454 335441 : ppiGroupLengths[0][piNumGroups[0]] = 1;
1455 5141832 : for ( k = 0; k < ( iNumBlocks - 1 ); k++ )
1456 : {
1457 : int32_t iGroupStart;
1458 :
1459 4806391 : iGroupStart = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1460 4806391 : iBitsRead += 1;
1461 :
1462 4806391 : if ( iGroupStart == 1 )
1463 : {
1464 306878 : piNumGroups[0]++;
1465 306878 : ppiGroupLengths[0][piNumGroups[0]] = 1;
1466 : }
1467 : else
1468 : {
1469 4499513 : ppiGroupLengths[0][piNumGroups[0]]++;
1470 : }
1471 : }
1472 335441 : piNumGroups[0]++;
1473 :
1474 335441 : piNumGroups[1] = piNumGroups[0];
1475 977760 : for ( k = 0; k < piNumGroups[1]; k++ )
1476 : {
1477 642319 : 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 335441 : return iBitsRead;
1535 : }
1536 :
1537 :
1538 481150232 : static int32_t BSForceBack(
1539 : ISAR_SPLIT_REND_BITS_HANDLE pBits,
1540 : int32_t iValue,
1541 : int32_t iBitCount )
1542 : {
1543 481150232 : pBits->bits_read -= iBitCount;
1544 :
1545 481150232 : return ( iValue >> iBitCount );
1546 : }
1547 :
1548 :
1549 628717624 : 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 628717624 : iVal = 0;
1560 628717624 : iIndex = 0;
1561 628717624 : iSymbol = 0xFFFF;
1562 628717624 : iBitsRead = 0;
1563 1517639341 : while ( iSymbol == 0xFFFF )
1564 : {
1565 888921717 : iIndex = ISAR_SPLIT_REND_BITStream_read_int32( pBits, HUFF_READ_SIZE );
1566 888921717 : iBitsRead += HUFF_READ_SIZE;
1567 :
1568 888921717 : iIndex = pauiHuffDecTable[iVal][iIndex];
1569 888921717 : iSymbol = ( iIndex & 0xFFFF );
1570 :
1571 888921717 : iVal = ( iIndex >> 16 );
1572 : }
1573 :
1574 628717624 : if ( iVal )
1575 : {
1576 481150232 : BSForceBack( pBits, iIndex, iVal );
1577 481150232 : iBitsRead -= iVal;
1578 : }
1579 :
1580 628717624 : *piSymbol = iSymbol;
1581 :
1582 628717624 : return iBitsRead;
1583 : }
1584 :
1585 :
1586 335441 : 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 335441 : iBitsRead = 0;
1597 1006323 : for ( n = 0; n < iChannels; n++ )
1598 : {
1599 1955520 : for ( k = 0; k < piNumGroups[n]; k++ )
1600 : {
1601 1284638 : iLastRMSVal = ISAR_SPLIT_REND_BITStream_read_int32( pBits, ENV0_BITS );
1602 1284638 : iBitsRead += ENV0_BITS;
1603 :
1604 1284638 : iLastRMSVal += ENV_MIN;
1605 1284638 : pppiRMSEnvelope[n][k][0] = iLastRMSVal;
1606 28262036 : for ( b = 1; b < iNumBands; b++ )
1607 : {
1608 : int32_t iDelta;
1609 :
1610 26977398 : iBitsRead += ReadHuff( c_aaiRMSEnvHuffDec, &iDelta, pBits );
1611 :
1612 26977398 : iDelta += ENV_DELTA_MIN;
1613 26977398 : iLastRMSVal += iDelta;
1614 26977398 : pppiRMSEnvelope[n][k][b] = iLastRMSVal;
1615 : }
1616 : }
1617 : }
1618 :
1619 335441 : return iBitsRead;
1620 : }
1621 :
1622 :
1623 335441 : static int32_t ReadAllocInformation(
1624 : int32_t *piAllocOffset,
1625 : ISAR_SPLIT_REND_BITS_HANDLE pBits )
1626 : {
1627 : int32_t iBitsRead;
1628 :
1629 335441 : iBitsRead = 0;
1630 335441 : *piAllocOffset = ISAR_SPLIT_REND_BITStream_read_int32( pBits, ALLOC_OFFSET_BITS );
1631 335441 : *piAllocOffset += MIN_ALLOC_OFFSET;
1632 335441 : iBitsRead += ALLOC_OFFSET_BITS;
1633 :
1634 335441 : return iBitsRead;
1635 : }
1636 :
1637 :
1638 335441 : 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 335441 : int32_t iDecodingStopped = 0;
1658 335441 : int32_t iNumLcldBands = c_aiNumLcldBandsPerBand[iNumBands - 1];
1659 : int32_t s;
1660 335441 : int32_t iSet = iSubSetId;
1661 :
1662 335441 : iBitsRead = 0;
1663 715928 : for ( s = 0; s < iNumSubSets; s++, iSet-- )
1664 : {
1665 : int32_t ch;
1666 :
1667 380487 : if ( iSet < 0 )
1668 : {
1669 14077 : iSet = iNumSubSets - 1;
1670 : }
1671 :
1672 1141461 : for ( ch = 0; ch < iNumChannels; ch++ )
1673 : {
1674 : int32_t n;
1675 : int32_t iBlockOffest;
1676 :
1677 760974 : 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 760974 : ppiDecodingFailed[ch][iSet] = 0; /* mark as correctly decoded */
1685 : }
1686 760974 : iBlockOffest = 0;
1687 2147576 : for ( n = 0; n < piNumGroups[ch]; n++ )
1688 : {
1689 : int32_t k;
1690 12120714 : for ( k = 0; k < ppiGroupLengths[ch][n]; k++ )
1691 : {
1692 : int32_t iFBOffset;
1693 :
1694 524917312 : 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 514183200 : b = c_aiBandIdPerLcldBand[iFBOffset];
1702 :
1703 514183200 : iAlloc = pppiAlloc[ch][n][b];
1704 :
1705 514183200 : iHuffDim = c_aiHuffmanDim[iAlloc];
1706 514183200 : iHuffMod = c_aiHuffmanMod[iAlloc];
1707 :
1708 514183200 : 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 514183200 : else if ( iAlloc > 0 )
1716 : {
1717 462944040 : const uint32_t( *pauiHuffmanTable )[HUFF_DEC_TABLE_SIZE] = NULL;
1718 462944040 : const uint32_t( *pauiHuffmanTableDPCM )[HUFF_DEC_TABLE_SIZE] = NULL;
1719 462944040 : int32_t iQuantValue1 = 0;
1720 462944040 : int32_t iQuantValue2 = 0;
1721 462944040 : pauiHuffmanTable = (const uint32_t( * )[HUFF_DEC_TABLE_SIZE]) c_apauiHuffDecTables[iAlloc];
1722 462944040 : 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 462944040 : if ( ppiPredEnable[ch][iFBOffset] == 1 )
1727 : #endif
1728 : {
1729 30396296 : if ( iHuffDim == 2 )
1730 : {
1731 : int32_t iSymbol;
1732 10128318 : iBitsRead += ReadHuff( pauiHuffmanTableDPCM, &iSymbol, pBits );
1733 10128318 : iQuantValue1 = iSymbol / iHuffMod;
1734 10128318 : iQuantValue2 = iSymbol % iHuffMod;
1735 : }
1736 : else
1737 : {
1738 20267978 : iBitsRead += ReadHuff( pauiHuffmanTableDPCM, &iQuantValue1, pBits );
1739 20267978 : iBitsRead += ReadHuff( pauiHuffmanTableDPCM, &iQuantValue2, pBits );
1740 : }
1741 : }
1742 : else
1743 : {
1744 432547744 : if ( iHuffDim == 2 )
1745 : {
1746 : int32_t iSymbol;
1747 :
1748 319330088 : iBitsRead += ReadHuff( pauiHuffmanTable, &iSymbol, pBits );
1749 319330088 : iQuantValue1 = iSymbol / iHuffMod;
1750 319330088 : iQuantValue2 = iSymbol % iHuffMod;
1751 : }
1752 : else
1753 : {
1754 113217656 : iBitsRead += ReadHuff( pauiHuffmanTable, &iQuantValue1, pBits );
1755 113217656 : iBitsRead += ReadHuff( pauiHuffmanTable, &iQuantValue2, pBits );
1756 : }
1757 : }
1758 :
1759 462944040 : pppiQReal[ch][iBlockOffest][iFBOffset] = iQuantValue1;
1760 462944040 : pppiQImag[ch][iBlockOffest][iFBOffset] = iQuantValue2;
1761 :
1762 462944040 : if ( iQuantValue1 > 0 )
1763 : {
1764 306508092 : pppiSignReal[ch][iBlockOffest][iFBOffset] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1765 306508092 : iBitsRead += 1;
1766 : }
1767 : else
1768 : {
1769 156435948 : pppiSignReal[ch][iBlockOffest][iFBOffset] = 0;
1770 : }
1771 462944040 : if ( iQuantValue2 > 0 )
1772 : {
1773 305890082 : pppiSignImag[ch][iBlockOffest][iFBOffset] = ISAR_SPLIT_REND_BITStream_read_int32( pBits, 1 );
1774 305890082 : iBitsRead += 1;
1775 : }
1776 : else
1777 : {
1778 157053958 : pppiSignImag[ch][iBlockOffest][iFBOffset] = 0;
1779 : }
1780 : }
1781 : else
1782 : {
1783 51239160 : pppiSignReal[ch][iBlockOffest][iFBOffset] = 0;
1784 51239160 : pppiSignImag[ch][iBlockOffest][iFBOffset] = 0;
1785 : }
1786 : }
1787 10734112 : iBlockOffest++;
1788 : }
1789 : }
1790 : }
1791 : }
1792 :
1793 335441 : return iBitsRead;
1794 : }
1795 :
1796 :
1797 335441 : 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 1006323 : for ( n = 0; n < iChannels; n++ )
1808 : {
1809 1955520 : for ( k = 0; k < piNumGroups[n]; k++ )
1810 : {
1811 29546674 : for ( b = 0; b < iNumBands; b++ )
1812 : {
1813 28262036 : iAlloc = ( ( pppiSMR[n][k][b] + iAllocOffset * ALLOC_OFFSET_SCALE ) >> 5 );
1814 28262036 : iAlloc = ( iAlloc > MIN_ALLOC ) ? iAlloc : MIN_ALLOC;
1815 28262036 : iAlloc = ( iAlloc < MAX_ALLOC ) ? iAlloc : MAX_ALLOC;
1816 28262036 : pppiAlloc[n][k][b] = iAlloc;
1817 : }
1818 : }
1819 : }
1820 :
1821 335441 : return;
1822 : }
|