Coverage Report

Created: 2025-09-05 06:55

/src/aac/libFDK/src/nlc_dec.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/******************* Library for basic calculation routines ********************
96
97
   Author(s):   Omer Osman
98
99
   Description: SAC/SAOC Dec Noiseless Coding
100
101
*******************************************************************************/
102
103
#include "nlc_dec.h"
104
#include "FDK_tools_rom.h"
105
106
/* MAX_PARAMETER_BANDS defines array length in huffdec */
107
108
#ifndef min
109
87.2k
#define min(a, b) (((a) < (b)) ? (a) : (b))
110
#endif
111
112
15.3k
ERROR_t sym_restoreIPD(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
113
15.3k
  int sum_val = data[0] + data[1];
114
15.3k
  int diff_val = data[0] - data[1];
115
116
15.3k
  if (sum_val > lav) {
117
6.63k
    data[0] = -sum_val + (2 * lav + 1);
118
6.63k
    data[1] = -diff_val;
119
8.74k
  } else {
120
8.74k
    data[0] = sum_val;
121
8.74k
    data[1] = diff_val;
122
8.74k
  }
123
124
15.3k
  if (data[0] - data[1] != 0) {
125
8.98k
    ULONG sym_bit;
126
8.98k
    sym_bit = FDKreadBits(strm, 1);
127
8.98k
    if (sym_bit) {
128
2.55k
      int tmp;
129
2.55k
      tmp = data[0];
130
2.55k
      data[0] = data[1];
131
2.55k
      data[1] = tmp;
132
2.55k
    }
133
8.98k
  }
134
135
15.3k
  return HUFFDEC_OK;
136
15.3k
}
137
138
23.5k
static int ilog2(unsigned int i) {
139
23.5k
  int l = 0;
140
141
23.5k
  if (i) i--;
142
101k
  while (i > 0) {
143
77.4k
    i >>= 1;
144
77.4k
    l++;
145
77.4k
  }
146
147
23.5k
  return l;
148
23.5k
}
149
150
static ERROR_t pcm_decode(HANDLE_FDK_BITSTREAM strm, SCHAR* out_data_1,
151
                          SCHAR* out_data_2, int offset, int num_val,
152
22.3k
                          int num_levels) {
153
22.3k
  int i = 0, j = 0, idx = 0;
154
22.3k
  int max_grp_len = 0, next_val = 0;
155
22.3k
  ULONG tmp;
156
157
22.3k
  int pcm_chunk_size[7] = {0};
158
159
22.3k
  switch (num_levels) {
160
239
    case 3:
161
239
      max_grp_len = 5;
162
239
      break;
163
41
    case 7:
164
41
      max_grp_len = 6;
165
41
      break;
166
35
    case 11:
167
35
      max_grp_len = 2;
168
35
      break;
169
0
    case 13:
170
0
      max_grp_len = 4;
171
0
      break;
172
0
    case 19:
173
0
      max_grp_len = 4;
174
0
      break;
175
0
    case 25:
176
0
      max_grp_len = 3;
177
0
      break;
178
0
    case 51:
179
0
      max_grp_len = 4;
180
0
      break;
181
8.52k
    case 4:
182
12.4k
    case 8:
183
18.5k
    case 15:
184
21.0k
    case 16:
185
21.0k
    case 26:
186
22.0k
    case 31:
187
22.0k
      max_grp_len = 1;
188
22.0k
      break;
189
0
    default:
190
0
      return HUFFDEC_NOTOK;
191
22.3k
  }
192
193
22.3k
  tmp = 1;
194
45.9k
  for (i = 1; i <= max_grp_len; i++) {
195
23.5k
    tmp *= num_levels;
196
23.5k
    pcm_chunk_size[i] = ilog2(tmp);
197
23.5k
  }
198
199
109k
  for (i = 0; i < num_val; i += max_grp_len) {
200
87.2k
    int grp_len, grp_val, data;
201
87.2k
    grp_len = min(max_grp_len, num_val - i);
202
87.2k
    data = FDKreadBits(strm, pcm_chunk_size[grp_len]);
203
204
87.2k
    grp_val = data;
205
206
175k
    for (j = 0; j < grp_len; j++) {
207
87.8k
      idx = i + (grp_len - j - 1);
208
87.8k
      next_val = grp_val % num_levels;
209
210
87.8k
      if (out_data_2 == NULL) {
211
29.7k
        out_data_1[idx] = next_val - offset;
212
58.0k
      } else if (out_data_1 == NULL) {
213
0
        out_data_2[idx] = next_val - offset;
214
58.0k
      } else {
215
58.0k
        if (idx % 2) {
216
29.0k
          out_data_2[idx / 2] = next_val - offset;
217
29.0k
        } else {
218
29.0k
          out_data_1[idx / 2] = next_val - offset;
219
29.0k
        }
220
58.0k
      }
221
222
87.8k
      grp_val = (grp_val - next_val) / num_levels;
223
87.8k
    }
224
87.2k
  }
225
226
22.3k
  return HUFFDEC_OK;
227
22.3k
}
228
229
static ERROR_t huff_read(HANDLE_FDK_BITSTREAM strm,
230
                         const SHORT (*nodeTab)[MAX_ENTRIES][2],
231
288k
                         int* out_data) {
232
288k
  int node = 0;
233
288k
  int len = 0;
234
235
740k
  do {
236
740k
    ULONG next_bit;
237
740k
    next_bit = FDKreadBits(strm, 1);
238
740k
    len++;
239
740k
    node = (*nodeTab)[node][next_bit];
240
740k
  } while (node > 0);
241
242
288k
  *out_data = node;
243
244
288k
  return HUFFDEC_OK;
245
288k
}
246
247
static ERROR_t huff_read_2D(HANDLE_FDK_BITSTREAM strm,
248
                            const SHORT (*nodeTab)[MAX_ENTRIES][2],
249
114k
                            SCHAR out_data[2], int* escape) {
250
114k
  ERROR_t err = HUFFDEC_OK;
251
252
114k
  int huff_2D_8bit = 0;
253
114k
  int node = 0;
254
255
114k
  if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
256
0
    goto bail;
257
0
  }
258
114k
  *escape = (node == 0);
259
260
114k
  if (*escape) {
261
441
    out_data[0] = 0;
262
441
    out_data[1] = 1;
263
113k
  } else {
264
113k
    huff_2D_8bit = -(node + 1);
265
113k
    out_data[0] = huff_2D_8bit >> 4;
266
113k
    out_data[1] = huff_2D_8bit & 0xf;
267
113k
  }
268
269
114k
bail:
270
114k
  return err;
271
114k
}
272
273
49.8k
static ERROR_t sym_restore(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
274
49.8k
  ULONG sym_bit = 0;
275
276
49.8k
  int sum_val = data[0] + data[1];
277
49.8k
  int diff_val = data[0] - data[1];
278
279
49.8k
  if (sum_val > lav) {
280
21.7k
    data[0] = -sum_val + (2 * lav + 1);
281
21.7k
    data[1] = -diff_val;
282
28.1k
  } else {
283
28.1k
    data[0] = sum_val;
284
28.1k
    data[1] = diff_val;
285
28.1k
  }
286
287
49.8k
  if (data[0] + data[1] != 0) {
288
29.7k
    sym_bit = FDKreadBits(strm, 1);
289
29.7k
    if (sym_bit) {
290
10.3k
      data[0] = -data[0];
291
10.3k
      data[1] = -data[1];
292
10.3k
    }
293
29.7k
  }
294
295
49.8k
  if (data[0] - data[1] != 0) {
296
30.2k
    sym_bit = FDKreadBits(strm, 1);
297
30.2k
    if (sym_bit) {
298
12.3k
      int tmp;
299
12.3k
      tmp = data[0];
300
12.3k
      data[0] = data[1];
301
12.3k
      data[1] = tmp;
302
12.3k
    }
303
30.2k
  }
304
305
49.8k
  return HUFFDEC_OK;
306
49.8k
}
307
308
static ERROR_t huff_dec_1D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type,
309
                           const INT dim1, SCHAR* out_data, const INT num_val,
310
                           const INT p0_flag)
311
312
28.2k
{
313
28.2k
  ERROR_t err = HUFFDEC_OK;
314
28.2k
  int i = 0, node = 0, offset = 0;
315
28.2k
  int od = 0, od_sign = 0;
316
28.2k
  ULONG data = 0;
317
28.2k
  int bitsAvail = 0;
318
319
28.2k
  const SHORT(*partTab)[MAX_ENTRIES][2] = NULL;
320
28.2k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
321
322
28.2k
  switch (data_type) {
323
8.01k
    case t_CLD:
324
8.01k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
325
8.01k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h1D[dim1]->nodeTab[0][0];
326
8.01k
      break;
327
13.5k
    case t_ICC:
328
13.5k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
329
13.5k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h1D[dim1]->nodeTab[0][0];
330
13.5k
      break;
331
0
    case t_OLD:
332
0
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.old[0][0];
333
0
      nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h1D[dim1]->nodeTab[0][0];
334
0
      break;
335
6.70k
    case t_IPD:
336
6.70k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
337
6.70k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h1D[dim1].nodeTab[0][0];
338
6.70k
      break;
339
0
    default:
340
0
      FDK_ASSERT(0);
341
0
      err = HUFFDEC_NOTOK;
342
0
      goto bail;
343
28.2k
  }
344
345
28.2k
  if (p0_flag) {
346
9.66k
    if ((err = huff_read(strm, partTab, &node)) != HUFFDEC_OK) {
347
0
      goto bail;
348
0
    }
349
350
9.66k
    out_data[0] = -(node + 1);
351
9.66k
    offset = 1;
352
9.66k
  }
353
354
128k
  for (i = offset; i < num_val; i++) {
355
102k
    bitsAvail = FDKgetValidBits(strm);
356
102k
    if (bitsAvail < 1) {
357
1.45k
      err = HUFFDEC_NOTOK;
358
1.45k
      goto bail;
359
1.45k
    }
360
361
100k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
362
0
      goto bail;
363
0
    }
364
100k
    od = -(node + 1);
365
366
100k
    if (data_type != t_IPD) {
367
73.4k
      if (od != 0) {
368
21.8k
        bitsAvail = FDKgetValidBits(strm);
369
21.8k
        if (bitsAvail < 1) {
370
59
          err = HUFFDEC_NOTOK;
371
59
          goto bail;
372
59
        }
373
374
21.8k
        data = FDKreadBits(strm, 1);
375
21.8k
        od_sign = data;
376
377
21.8k
        if (od_sign) od = -od;
378
21.8k
      }
379
73.4k
    }
380
381
100k
    out_data[i] = od;
382
100k
  }
383
384
28.2k
bail:
385
28.2k
  return err;
386
28.2k
}
387
388
static ERROR_t huff_dec_2D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type,
389
                           const INT dim1, const INT dim2, SCHAR out_data[][2],
390
                           const INT num_val, const INT stride,
391
31.2k
                           SCHAR* p0_data[2]) {
392
31.2k
  ERROR_t err = HUFFDEC_OK;
393
31.2k
  int i = 0, lav = 0, escape = 0, escCntr = 0;
394
31.2k
  int node = 0;
395
31.2k
  unsigned long data = 0;
396
397
31.2k
  SCHAR esc_data[2][28] = {{0}};
398
31.2k
  int escIdx[28] = {0};
399
31.2k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
400
401
  /* LAV */
402
31.2k
  if ((err =
403
31.2k
           huff_read(strm, (HANDLE_HUFF_NODE)&FDK_huffLavIdxNodes.nodeTab[0][0],
404
31.2k
                     &node)) != HUFFDEC_OK) {
405
0
    goto bail;
406
0
  }
407
31.2k
  data = -(node + 1);
408
409
31.2k
  switch (data_type) {
410
15.6k
    case t_CLD:
411
15.6k
      lav = 2 * data + 3; /* 3, 5, 7, 9 */
412
15.6k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
413
15.6k
      break;
414
8.40k
    case t_ICC:
415
8.40k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
416
8.40k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
417
8.40k
      break;
418
0
    case t_OLD:
419
0
      lav = 3 * data + 3;
420
0
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.old[0][0];
421
0
      break;
422
7.17k
    case t_IPD:
423
7.17k
      if (data == 0)
424
2.73k
        data = 3;
425
4.43k
      else
426
4.43k
        data--;
427
7.17k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
428
7.17k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
429
7.17k
      break;
430
0
    default:
431
0
      FDK_ASSERT(0);
432
0
      err = HUFFDEC_NOTOK;
433
0
      goto bail;
434
31.2k
  }
435
436
  /* Partition 0 */
437
31.2k
  if (p0_data[0] != NULL) {
438
19.3k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
439
0
      goto bail;
440
0
    }
441
19.3k
    *p0_data[0] = -(node + 1);
442
19.3k
  }
443
31.2k
  if (p0_data[1] != NULL) {
444
13.4k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
445
0
      goto bail;
446
0
    }
447
13.4k
    *p0_data[1] = -(node + 1);
448
13.4k
  }
449
450
31.2k
  switch (data_type) {
451
15.6k
    case t_CLD:
452
15.6k
      switch (lav) {
453
3.92k
        case 3:
454
3.92k
          nodeTab =
455
3.92k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav3[0][0];
456
3.92k
          break;
457
697
        case 5:
458
697
          nodeTab =
459
697
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav5[0][0];
460
697
          break;
461
4.33k
        case 7:
462
4.33k
          nodeTab =
463
4.33k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav7[0][0];
464
4.33k
          break;
465
6.68k
        case 9:
466
6.68k
          nodeTab =
467
6.68k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav9[0][0];
468
6.68k
          break;
469
15.6k
      }
470
15.6k
      break;
471
15.6k
    case t_ICC:
472
8.40k
      switch (lav) {
473
3.15k
        case 1:
474
3.15k
          nodeTab =
475
3.15k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav1[0][0];
476
3.15k
          break;
477
1.40k
        case 3:
478
1.40k
          nodeTab =
479
1.40k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav3[0][0];
480
1.40k
          break;
481
686
        case 5:
482
686
          nodeTab =
483
686
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav5[0][0];
484
686
          break;
485
3.16k
        case 7:
486
3.16k
          nodeTab =
487
3.16k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav7[0][0];
488
3.16k
          break;
489
8.40k
      }
490
8.40k
      break;
491
8.40k
    case t_OLD:
492
0
      switch (lav) {
493
0
        case 3:
494
0
          nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav3[0][0];
495
0
          break;
496
0
        case 6:
497
0
          nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav6[0][0];
498
0
          break;
499
0
        case 9:
500
0
          nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav9[0][0];
501
0
          break;
502
0
        case 12:
503
0
          nodeTab =
504
0
              (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav12[0][0];
505
0
          break;
506
0
      }
507
0
      break;
508
7.17k
    case t_IPD:
509
7.17k
      switch (lav) {
510
3.87k
        case 1:
511
3.87k
          nodeTab =
512
3.87k
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav1[0][0];
513
3.87k
          break;
514
196
        case 3:
515
196
          nodeTab =
516
196
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav3[0][0];
517
196
          break;
518
371
        case 5:
519
371
          nodeTab =
520
371
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav5[0][0];
521
371
          break;
522
2.73k
        case 7:
523
2.73k
          nodeTab =
524
2.73k
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav7[0][0];
525
2.73k
          break;
526
7.17k
      }
527
7.17k
      break;
528
7.17k
    default:
529
0
      break;
530
31.2k
  }
531
532
96.8k
  for (i = 0; i < num_val; i += stride) {
533
65.6k
    if ((err = huff_read_2D(strm, nodeTab, out_data[i], &escape)) !=
534
65.6k
        HUFFDEC_OK) {
535
0
      goto bail;
536
0
    }
537
538
65.6k
    if (escape) {
539
441
      escIdx[escCntr++] = i;
540
65.2k
    } else {
541
65.2k
      if (data_type == t_IPD) {
542
15.3k
        if ((err = sym_restoreIPD(strm, lav, out_data[i])) != HUFFDEC_OK) {
543
0
          goto bail;
544
0
        }
545
49.8k
      } else {
546
49.8k
        if ((err = sym_restore(strm, lav, out_data[i])) != HUFFDEC_OK) {
547
0
          goto bail;
548
0
        }
549
49.8k
      }
550
65.2k
    }
551
65.6k
  } /* i */
552
553
31.2k
  if (escCntr > 0) {
554
315
    if ((err = pcm_decode(strm, esc_data[0], esc_data[1], 0, 2 * escCntr,
555
315
                          (2 * lav + 1))) != HUFFDEC_OK) {
556
0
      goto bail;
557
0
    }
558
559
756
    for (i = 0; i < escCntr; i++) {
560
441
      out_data[escIdx[i]][0] = esc_data[0][i] - lav;
561
441
      out_data[escIdx[i]][1] = esc_data[1][i] - lav;
562
441
    }
563
315
  }
564
31.2k
bail:
565
31.2k
  return err;
566
31.2k
}
567
568
static ERROR_t huff_decode(HANDLE_FDK_BITSTREAM strm, SCHAR* out_data_1,
569
                           SCHAR* out_data_2, DATA_TYPE data_type,
570
                           DIFF_TYPE diff_type_1, DIFF_TYPE diff_type_2,
571
38.9k
                           int num_val, PAIRING* pairing_scheme, int ldMode) {
572
38.9k
  ERROR_t err = HUFFDEC_OK;
573
38.9k
  CODING_SCHEME coding_scheme = HUFF_1D;
574
38.9k
  DIFF_TYPE diff_type;
575
576
38.9k
  int i = 0;
577
578
38.9k
  SCHAR pair_vec[28][2];
579
580
38.9k
  SCHAR* p0_data_1[2] = {NULL, NULL};
581
38.9k
  SCHAR* p0_data_2[2] = {NULL, NULL};
582
583
38.9k
  int p0_flag[2];
584
585
38.9k
  int num_val_1_int = num_val;
586
38.9k
  int num_val_2_int = num_val;
587
588
38.9k
  SCHAR* out_data_1_int = out_data_1;
589
38.9k
  SCHAR* out_data_2_int = out_data_2;
590
591
38.9k
  int df_rest_flag_1 = 0;
592
38.9k
  int df_rest_flag_2 = 0;
593
594
38.9k
  int hufYY1;
595
38.9k
  int hufYY2;
596
38.9k
  int hufYY;
597
598
  /* Coding scheme */
599
38.9k
  coding_scheme = (CODING_SCHEME)FDKreadBits(strm, 1);
600
601
38.9k
  if (coding_scheme == HUFF_2D) {
602
24.4k
    if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ldMode == 0)) {
603
14.1k
      *pairing_scheme = (PAIRING)FDKreadBits(strm, 1);
604
14.1k
    } else {
605
10.2k
      *pairing_scheme = FREQ_PAIR;
606
10.2k
    }
607
24.4k
  }
608
609
38.9k
  {
610
38.9k
    hufYY1 = diff_type_1;
611
38.9k
    hufYY2 = diff_type_2;
612
38.9k
  }
613
614
38.9k
  switch (coding_scheme) {
615
14.5k
    case HUFF_1D:
616
14.5k
      p0_flag[0] = (diff_type_1 == DIFF_FREQ);
617
14.5k
      p0_flag[1] = (diff_type_2 == DIFF_FREQ);
618
14.5k
      if (out_data_1 != NULL) {
619
14.5k
        if ((err = huff_dec_1D(strm, data_type, hufYY1, out_data_1,
620
14.5k
                               num_val_1_int, p0_flag[0])) != HUFFDEC_OK) {
621
811
          goto bail;
622
811
        }
623
14.5k
      }
624
13.6k
      if (out_data_2 != NULL) {
625
3.93k
        if ((err = huff_dec_1D(strm, data_type, hufYY2, out_data_2,
626
3.93k
                               num_val_2_int, p0_flag[1])) != HUFFDEC_OK) {
627
27
          goto bail;
628
27
        }
629
3.93k
      }
630
631
13.6k
      break; /* HUFF_1D */
632
633
24.4k
    case HUFF_2D:
634
635
24.4k
      switch (*pairing_scheme) {
636
13.8k
        case FREQ_PAIR:
637
638
13.8k
          if (out_data_1 != NULL) {
639
13.8k
            if (diff_type_1 == DIFF_FREQ) {
640
9.57k
              p0_data_1[0] = &out_data_1[0];
641
9.57k
              p0_data_1[1] = NULL;
642
643
9.57k
              num_val_1_int -= 1;
644
9.57k
              out_data_1_int += 1;
645
9.57k
            }
646
13.8k
            df_rest_flag_1 = num_val_1_int % 2;
647
13.8k
            if (df_rest_flag_1) num_val_1_int -= 1;
648
13.8k
            if (num_val_1_int < 0) {
649
6
              err = HUFFDEC_NOTOK;
650
6
              goto bail;
651
6
            }
652
13.8k
          }
653
13.8k
          if (out_data_2 != NULL) {
654
6.86k
            if (diff_type_2 == DIFF_FREQ) {
655
3.76k
              p0_data_2[0] = NULL;
656
3.76k
              p0_data_2[1] = &out_data_2[0];
657
658
3.76k
              num_val_2_int -= 1;
659
3.76k
              out_data_2_int += 1;
660
3.76k
            }
661
6.86k
            df_rest_flag_2 = num_val_2_int % 2;
662
6.86k
            if (df_rest_flag_2) num_val_2_int -= 1;
663
6.86k
            if (num_val_2_int < 0) {
664
3
              err = HUFFDEC_NOTOK;
665
3
              goto bail;
666
3
            }
667
6.86k
          }
668
669
13.8k
          if (out_data_1 != NULL) {
670
13.8k
            if ((err = huff_dec_2D(strm, data_type, hufYY1, FREQ_PAIR, pair_vec,
671
13.8k
                                   num_val_1_int, 2, p0_data_1)) !=
672
13.8k
                HUFFDEC_OK) {
673
0
              goto bail;
674
0
            }
675
13.8k
            if (df_rest_flag_1) {
676
5.61k
              if ((err = huff_dec_1D(strm, data_type, hufYY1,
677
5.61k
                                     out_data_1_int + num_val_1_int, 1, 0)) !=
678
5.61k
                  HUFFDEC_OK) {
679
286
                goto bail;
680
286
              }
681
5.61k
            }
682
13.8k
          }
683
13.5k
          if (out_data_2 != NULL) {
684
6.79k
            if ((err = huff_dec_2D(strm, data_type, hufYY2, FREQ_PAIR,
685
6.79k
                                   pair_vec + 1, num_val_2_int, 2,
686
6.79k
                                   p0_data_2)) != HUFFDEC_OK) {
687
0
              goto bail;
688
0
            }
689
6.79k
            if (df_rest_flag_2) {
690
4.22k
              if ((err = huff_dec_1D(strm, data_type, hufYY2,
691
4.22k
                                     out_data_2_int + num_val_2_int, 1, 0)) !=
692
4.22k
                  HUFFDEC_OK) {
693
394
                goto bail;
694
394
              }
695
4.22k
            }
696
6.79k
          }
697
698
13.2k
          if (out_data_1 != NULL) {
699
44.8k
            for (i = 0; i < num_val_1_int - 1; i += 2) {
700
31.6k
              out_data_1_int[i] = pair_vec[i][0];
701
31.6k
              out_data_1_int[i + 1] = pair_vec[i][1];
702
31.6k
            }
703
13.2k
          }
704
13.2k
          if (out_data_2 != NULL) {
705
23.2k
            for (i = 0; i < num_val_2_int - 1; i += 2) {
706
16.8k
              out_data_2_int[i] = pair_vec[i + 1][0];
707
16.8k
              out_data_2_int[i + 1] = pair_vec[i + 1][1];
708
16.8k
            }
709
6.39k
          }
710
13.2k
          break; /* FREQ_PAIR */
711
712
10.5k
        case TIME_PAIR:
713
10.5k
          if (((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
714
9.73k
            p0_data_1[0] = &out_data_1[0];
715
9.73k
            p0_data_1[1] = &out_data_2[0];
716
717
9.73k
            out_data_1_int += 1;
718
9.73k
            out_data_2_int += 1;
719
720
9.73k
            num_val_1_int -= 1;
721
9.73k
          }
722
723
10.5k
          if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
724
8.05k
            diff_type = DIFF_TIME;
725
8.05k
          } else {
726
2.48k
            diff_type = DIFF_FREQ;
727
2.48k
          }
728
10.5k
          { hufYY = diff_type; }
729
730
10.5k
          if ((err = huff_dec_2D(strm, data_type, hufYY, TIME_PAIR, pair_vec,
731
10.5k
                                 num_val_1_int, 1, p0_data_1)) != HUFFDEC_OK) {
732
0
            goto bail;
733
0
          }
734
735
26.3k
          for (i = 0; i < num_val_1_int; i++) {
736
15.7k
            out_data_1_int[i] = pair_vec[i][0];
737
15.7k
            out_data_2_int[i] = pair_vec[i][1];
738
15.7k
          }
739
740
10.5k
          break; /* TIME_PAIR */
741
742
0
        default:
743
0
          break;
744
24.4k
      }
745
746
23.7k
      break; /* HUFF_2D */
747
748
23.7k
    default:
749
0
      break;
750
38.9k
  }
751
38.9k
bail:
752
38.9k
  return err;
753
38.9k
}
754
755
static void diff_freq_decode(const SCHAR* const diff_data,
756
50.5k
                             SCHAR* const out_data, const int num_val) {
757
50.5k
  int i = 0;
758
50.5k
  out_data[0] = diff_data[0];
759
760
248k
  for (i = 1; i < num_val; i++) {
761
198k
    out_data[i] = out_data[i - 1] + diff_data[i];
762
198k
  }
763
50.5k
}
764
765
static void diff_time_decode_backwards(const SCHAR* const prev_data,
766
                                       const SCHAR* const diff_data,
767
                                       SCHAR* const out_data,
768
                                       const int mixed_diff_type,
769
16.1k
                                       const int num_val) {
770
16.1k
  int i = 0; /* default start value*/
771
772
16.1k
  if (mixed_diff_type) {
773
3.14k
    out_data[0] = diff_data[0];
774
3.14k
    i = 1; /* new start value */
775
3.14k
  }
776
119k
  for (; i < num_val; i++) {
777
103k
    out_data[i] = prev_data[i] + diff_data[i];
778
103k
  }
779
16.1k
}
780
781
static void diff_time_decode_forwards(const SCHAR* const prev_data,
782
                                      const SCHAR* const diff_data,
783
                                      SCHAR* const out_data,
784
                                      const int mixed_diff_type,
785
8.15k
                                      const int num_val) {
786
8.15k
  int i = 0; /* default start value*/
787
788
8.15k
  if (mixed_diff_type) {
789
4.10k
    out_data[0] = diff_data[0];
790
4.10k
    i = 1; /* new start value */
791
4.10k
  }
792
28.3k
  for (; i < num_val; i++) {
793
20.1k
    out_data[i] = prev_data[i] - diff_data[i];
794
20.1k
  }
795
8.15k
}
796
797
static ERROR_t attach_lsb(HANDLE_FDK_BITSTREAM strm, SCHAR* in_data_msb,
798
                          int offset, int num_lsb, int num_val,
799
58.2k
                          SCHAR* out_data) {
800
58.2k
  int i = 0, lsb = 0;
801
58.2k
  ULONG data = 0;
802
803
327k
  for (i = 0; i < num_val; i++) {
804
269k
    int msb;
805
269k
    msb = in_data_msb[i];
806
807
269k
    if (num_lsb > 0) {
808
49.4k
      data = FDKreadBits(strm, num_lsb);
809
49.4k
      lsb = data;
810
811
49.4k
      out_data[i] = ((msb << num_lsb) | lsb) - offset;
812
49.4k
    } else
813
219k
      out_data[i] = msb - offset;
814
269k
  }
815
816
58.2k
  return HUFFDEC_OK; /* dummy */
817
58.2k
}
818
819
ERROR_t EcDataPairDec(DECODER_TYPE DECODER, HANDLE_FDK_BITSTREAM strm,
820
                      SCHAR* aaOutData1, SCHAR* aaOutData2, SCHAR* aHistory,
821
                      DATA_TYPE data_type, int startBand, int dataBands,
822
                      int pair_flag, int coarse_flag,
823
                      int allowDiffTimeBack_flag)
824
825
60.9k
{
826
60.9k
  ERROR_t err = HUFFDEC_OK;
827
828
  // int allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
829
60.9k
  int attachLsb_flag = 0;
830
60.9k
  int pcmCoding_flag = 0;
831
832
60.9k
  int mixed_time_pair = 0, numValPcm = 0;
833
60.9k
  int quant_levels = 0, quant_offset = 0;
834
60.9k
  ULONG data = 0;
835
836
60.9k
  SCHAR aaDataPair[2][28] = {{0}};
837
60.9k
  SCHAR aaDataDiff[2][28] = {{0}};
838
839
60.9k
  SCHAR aHistoryMsb[28] = {0};
840
841
60.9k
  SCHAR* pDataVec[2] = {NULL, NULL};
842
843
60.9k
  DIFF_TYPE diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
844
60.9k
  PAIRING pairing = FREQ_PAIR;
845
60.9k
  DIRECTION direction = BACKWARDS;
846
847
60.9k
  switch (data_type) {
848
23.5k
    case t_CLD:
849
23.5k
      if (coarse_flag) {
850
13.3k
        attachLsb_flag = 0;
851
13.3k
        quant_levels = 15;
852
13.3k
        quant_offset = 7;
853
13.3k
      } else {
854
10.1k
        attachLsb_flag = 0;
855
10.1k
        quant_levels = 31;
856
10.1k
        quant_offset = 15;
857
10.1k
      }
858
859
23.5k
      break;
860
861
25.0k
    case t_ICC:
862
25.0k
      if (coarse_flag) {
863
14.4k
        attachLsb_flag = 0;
864
14.4k
        quant_levels = 4;
865
14.4k
        quant_offset = 0;
866
14.4k
      } else {
867
10.5k
        attachLsb_flag = 0;
868
10.5k
        quant_levels = 8;
869
10.5k
        quant_offset = 0;
870
10.5k
      }
871
872
25.0k
      break;
873
874
0
    case t_OLD:
875
0
      if (coarse_flag) {
876
0
        attachLsb_flag = 0;
877
0
        quant_levels = 8;
878
0
        quant_offset = 0;
879
0
      } else {
880
0
        attachLsb_flag = 0;
881
0
        quant_levels = 16;
882
0
        quant_offset = 0;
883
0
      }
884
0
      break;
885
886
0
    case t_NRG:
887
0
      if (coarse_flag) {
888
0
        attachLsb_flag = 0;
889
0
        quant_levels = 32;
890
0
        quant_offset = 0;
891
0
      } else {
892
0
        attachLsb_flag = 0;
893
0
        quant_levels = 64;
894
0
        quant_offset = 0;
895
0
      }
896
0
      break;
897
898
12.4k
    case t_IPD:
899
12.4k
      if (!coarse_flag) {
900
8.30k
        attachLsb_flag = 1;
901
8.30k
        quant_levels = 16;
902
8.30k
        quant_offset = 0;
903
8.30k
      } else {
904
4.11k
        attachLsb_flag = 0;
905
4.11k
        quant_levels = 8;
906
4.11k
        quant_offset = 0;
907
4.11k
      }
908
12.4k
      break;
909
910
0
    default:
911
0
      return HUFFDEC_NOTOK;
912
60.9k
  }
913
914
60.9k
  data = FDKreadBits(strm, 1);
915
60.9k
  pcmCoding_flag = data;
916
917
60.9k
  if (pcmCoding_flag) {
918
22.0k
    if (pair_flag) {
919
15.7k
      pDataVec[0] = aaDataPair[0];
920
15.7k
      pDataVec[1] = aaDataPair[1];
921
15.7k
      numValPcm = 2 * dataBands;
922
15.7k
    } else {
923
6.28k
      pDataVec[0] = aaDataPair[0];
924
6.28k
      pDataVec[1] = NULL;
925
6.28k
      numValPcm = dataBands;
926
6.28k
    }
927
928
22.0k
    err = pcm_decode(strm, pDataVec[0], pDataVec[1], quant_offset, numValPcm,
929
22.0k
                     quant_levels);
930
22.0k
    if (err != HUFFDEC_OK) return HUFFDEC_NOTOK;
931
932
38.9k
  } else { /* Differential/Huffman/LSB Coding */
933
934
38.9k
    if (pair_flag) {
935
21.4k
      pDataVec[0] = aaDataDiff[0];
936
21.4k
      pDataVec[1] = aaDataDiff[1];
937
21.4k
    } else {
938
17.4k
      pDataVec[0] = aaDataDiff[0];
939
17.4k
      pDataVec[1] = NULL;
940
17.4k
    }
941
942
38.9k
    diff_type[0] = DIFF_FREQ;
943
38.9k
    diff_type[1] = DIFF_FREQ;
944
945
38.9k
    direction = BACKWARDS;
946
38.9k
    {
947
38.9k
      if (pair_flag || allowDiffTimeBack_flag) {
948
34.1k
        data = FDKreadBits(strm, 1);
949
34.1k
        diff_type[0] = (DIFF_TYPE)data;
950
34.1k
      }
951
952
38.9k
      if (pair_flag &&
953
38.9k
          ((diff_type[0] == DIFF_FREQ) || allowDiffTimeBack_flag)) {
954
16.6k
        data = FDKreadBits(strm, 1);
955
16.6k
        diff_type[1] = (DIFF_TYPE)data;
956
16.6k
      }
957
38.9k
    }
958
    /* Huffman decoding */
959
38.9k
    err = huff_decode(strm, pDataVec[0], pDataVec[1], data_type, diff_type[0],
960
38.9k
                      diff_type[1], dataBands, &pairing,
961
38.9k
                      (DECODER == SAOC_DECODER));
962
38.9k
    if (err != HUFFDEC_OK) {
963
1.52k
      return HUFFDEC_NOTOK;
964
1.52k
    }
965
966
37.4k
    {
967
      /* Differential decoding */
968
37.4k
      if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
969
22.6k
        if (DECODER == SAOC_DECODER) {
970
2.62k
          direction = BACKWARDS;
971
20.0k
        } else {
972
20.0k
          if (pair_flag) {
973
13.0k
            if ((diff_type[0] == DIFF_TIME) && !allowDiffTimeBack_flag) {
974
4.81k
              direction = FORWARDS;
975
8.27k
            } else if (diff_type[1] == DIFF_TIME) {
976
4.25k
              direction = BACKWARDS;
977
4.25k
            } else {
978
4.01k
              data = FDKreadBits(strm, 1);
979
4.01k
              direction = (DIRECTION)data;
980
4.01k
            }
981
13.0k
          } else {
982
6.97k
            direction = BACKWARDS;
983
6.97k
          }
984
20.0k
        }
985
22.6k
      }
986
987
37.4k
      mixed_time_pair =
988
37.4k
          (diff_type[0] != diff_type[1]) && (pairing == TIME_PAIR);
989
990
37.4k
      if (direction == BACKWARDS) {
991
29.2k
        if (diff_type[0] == DIFF_FREQ) {
992
19.9k
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
993
19.9k
        } else {
994
9.34k
          int i;
995
79.9k
          for (i = 0; i < dataBands; i++) {
996
70.5k
            aHistoryMsb[i] = aHistory[i + startBand] + quant_offset;
997
70.5k
            if (attachLsb_flag) {
998
21.2k
              aHistoryMsb[i] >>= 1;
999
21.2k
            }
1000
70.5k
          }
1001
9.34k
          diff_time_decode_backwards(aHistoryMsb, aaDataDiff[0], aaDataPair[0],
1002
9.34k
                                     mixed_time_pair, dataBands);
1003
9.34k
        }
1004
29.2k
        if (diff_type[1] == DIFF_FREQ) {
1005
22.4k
          diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1006
22.4k
        } else {
1007
6.78k
          diff_time_decode_backwards(aaDataPair[0], aaDataDiff[1],
1008
6.78k
                                     aaDataPair[1], mixed_time_pair, dataBands);
1009
6.78k
        }
1010
29.2k
      } else {
1011
        /* diff_type[1] MUST BE DIFF_FREQ */
1012
8.15k
        diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1013
1014
8.15k
        if (diff_type[0] == DIFF_FREQ) {
1015
0
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
1016
8.15k
        } else {
1017
8.15k
          diff_time_decode_forwards(aaDataPair[1], aaDataDiff[0], aaDataPair[0],
1018
8.15k
                                    mixed_time_pair, dataBands);
1019
8.15k
        }
1020
8.15k
      }
1021
37.4k
    }
1022
1023
    /* LSB decoding */
1024
37.4k
    err = attach_lsb(strm, aaDataPair[0], quant_offset, attachLsb_flag ? 1 : 0,
1025
37.4k
                     dataBands, aaDataPair[0]);
1026
37.4k
    if (err != HUFFDEC_OK) goto bail;
1027
1028
37.4k
    if (pair_flag) {
1029
20.8k
      err = attach_lsb(strm, aaDataPair[1], quant_offset,
1030
20.8k
                       attachLsb_flag ? 1 : 0, dataBands, aaDataPair[1]);
1031
20.8k
      if (err != HUFFDEC_OK) goto bail;
1032
20.8k
    }
1033
37.4k
  } /* End: Differential/Huffman/LSB Coding */
1034
1035
  /* Copy data to output arrays */
1036
59.4k
  FDKmemcpy(aaOutData1 + startBand, aaDataPair[0], sizeof(SCHAR) * dataBands);
1037
59.4k
  if (pair_flag) {
1038
36.6k
    FDKmemcpy(aaOutData2 + startBand, aaDataPair[1], sizeof(SCHAR) * dataBands);
1039
36.6k
  }
1040
1041
59.4k
bail:
1042
59.4k
  return err;
1043
59.4k
}
1044
1045
ERROR_t huff_dec_reshape(HANDLE_FDK_BITSTREAM strm, int* out_data,
1046
3.02k
                         int num_val) {
1047
3.02k
  ERROR_t err = HUFFDEC_OK;
1048
3.02k
  int val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
1049
3.02k
  SCHAR rl_data[2] = {0};
1050
1051
51.5k
  while (val_rcvd < num_val) {
1052
48.6k
    err = huff_read_2D(strm,
1053
48.6k
                       (HANDLE_HUFF_NODE)&FDK_huffReshapeNodes.nodeTab[0][0],
1054
48.6k
                       rl_data, &dummy);
1055
48.6k
    if (err != HUFFDEC_OK) goto bail;
1056
48.6k
    val = rl_data[0];
1057
48.6k
    len = rl_data[1] + 1;
1058
48.6k
    if (val_rcvd + len > num_val) {
1059
97
      err = HUFFDEC_NOTOK;
1060
97
      goto bail;
1061
97
    }
1062
142k
    for (i = val_rcvd; i < val_rcvd + len; i++) {
1063
94.1k
      out_data[i] = val;
1064
94.1k
    }
1065
48.5k
    val_rcvd += len;
1066
48.5k
  }
1067
3.02k
bail:
1068
3.02k
  return err;
1069
3.02k
}