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