Coverage Report

Created: 2026-08-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libFDK/src/nlc_dec.cpp
Line
Count
Source
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
84.6k
#define min(a, b) (((a) < (b)) ? (a) : (b))
110
#endif
111
112
10.4k
ERROR_t sym_restoreIPD(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
113
10.4k
  int sum_val = data[0] + data[1];
114
10.4k
  int diff_val = data[0] - data[1];
115
116
10.4k
  if (sum_val > lav) {
117
3.73k
    data[0] = -sum_val + (2 * lav + 1);
118
3.73k
    data[1] = -diff_val;
119
6.70k
  } else {
120
6.70k
    data[0] = sum_val;
121
6.70k
    data[1] = diff_val;
122
6.70k
  }
123
124
10.4k
  if (data[0] - data[1] != 0) {
125
4.96k
    ULONG sym_bit;
126
4.96k
    sym_bit = FDKreadBits(strm, 1);
127
4.96k
    if (sym_bit) {
128
1.63k
      int tmp;
129
1.63k
      tmp = data[0];
130
1.63k
      data[0] = data[1];
131
1.63k
      data[1] = tmp;
132
1.63k
    }
133
4.96k
  }
134
135
10.4k
  return HUFFDEC_OK;
136
10.4k
}
137
138
22.4k
static int ilog2(unsigned int i) {
139
22.4k
  int l = 0;
140
141
22.4k
  if (i) i--;
142
98.7k
  while (i > 0) {
143
76.2k
    i >>= 1;
144
76.2k
    l++;
145
76.2k
  }
146
147
22.4k
  return l;
148
22.4k
}
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
20.9k
                          int num_levels) {
153
20.9k
  int i = 0, j = 0, idx = 0;
154
20.9k
  int max_grp_len = 0, next_val = 0;
155
20.9k
  ULONG tmp;
156
157
20.9k
  int pcm_chunk_size[7] = {0};
158
159
20.9k
  switch (num_levels) {
160
306
    case 3:
161
306
      max_grp_len = 5;
162
306
      break;
163
44
    case 7:
164
44
      max_grp_len = 6;
165
44
      break;
166
40
    case 11:
167
40
      max_grp_len = 2;
168
40
      break;
169
0
    case 13:
170
0
      max_grp_len = 4;
171
0
      break;
172
1
    case 19:
173
1
      max_grp_len = 4;
174
1
      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
7.08k
    case 4:
182
10.6k
    case 8:
183
18.9k
    case 15:
184
20.1k
    case 16:
185
20.1k
    case 26:
186
20.5k
    case 31:
187
20.5k
      max_grp_len = 1;
188
20.5k
      break;
189
0
    default:
190
0
      return HUFFDEC_NOTOK;
191
20.9k
  }
192
193
20.9k
  tmp = 1;
194
43.4k
  for (i = 1; i <= max_grp_len; i++) {
195
22.4k
    tmp *= num_levels;
196
22.4k
    pcm_chunk_size[i] = ilog2(tmp);
197
22.4k
  }
198
199
105k
  for (i = 0; i < num_val; i += max_grp_len) {
200
84.6k
    int grp_len, grp_val, data;
201
84.6k
    grp_len = min(max_grp_len, num_val - i);
202
84.6k
    data = FDKreadBits(strm, pcm_chunk_size[grp_len]);
203
204
84.6k
    grp_val = data;
205
206
170k
    for (j = 0; j < grp_len; j++) {
207
85.4k
      idx = i + (grp_len - j - 1);
208
85.4k
      next_val = grp_val % num_levels;
209
210
85.4k
      if (out_data_2 == NULL) {
211
37.3k
        out_data_1[idx] = next_val - offset;
212
48.0k
      } else if (out_data_1 == NULL) {
213
0
        out_data_2[idx] = next_val - offset;
214
48.0k
      } else {
215
48.0k
        if (idx % 2) {
216
24.0k
          out_data_2[idx / 2] = next_val - offset;
217
24.0k
        } else {
218
24.0k
          out_data_1[idx / 2] = next_val - offset;
219
24.0k
        }
220
48.0k
      }
221
222
85.4k
      grp_val = (grp_val - next_val) / num_levels;
223
85.4k
    }
224
84.6k
  }
225
226
20.9k
  return HUFFDEC_OK;
227
20.9k
}
228
229
static ERROR_t huff_read(HANDLE_FDK_BITSTREAM strm,
230
                         const SHORT (*nodeTab)[MAX_ENTRIES][2],
231
169k
                         int* out_data) {
232
169k
  int node = 0;
233
169k
  int len = 0;
234
235
414k
  do {
236
414k
    ULONG next_bit;
237
414k
    next_bit = FDKreadBits(strm, 1);
238
414k
    len++;
239
414k
    node = (*nodeTab)[node][next_bit];
240
414k
  } while (node > 0);
241
242
169k
  *out_data = node;
243
244
169k
  return HUFFDEC_OK;
245
169k
}
246
247
static ERROR_t huff_read_2D(HANDLE_FDK_BITSTREAM strm,
248
                            const SHORT (*nodeTab)[MAX_ENTRIES][2],
249
73.1k
                            SCHAR out_data[2], int* escape) {
250
73.1k
  ERROR_t err = HUFFDEC_OK;
251
252
73.1k
  int huff_2D_8bit = 0;
253
73.1k
  int node = 0;
254
255
73.1k
  if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
256
0
    goto bail;
257
0
  }
258
73.1k
  *escape = (node == 0);
259
260
73.1k
  if (*escape) {
261
605
    out_data[0] = 0;
262
605
    out_data[1] = 1;
263
72.5k
  } else {
264
72.5k
    huff_2D_8bit = -(node + 1);
265
72.5k
    out_data[0] = huff_2D_8bit >> 4;
266
72.5k
    out_data[1] = huff_2D_8bit & 0xf;
267
72.5k
  }
268
269
73.1k
bail:
270
73.1k
  return err;
271
73.1k
}
272
273
40.1k
static ERROR_t sym_restore(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
274
40.1k
  ULONG sym_bit = 0;
275
276
40.1k
  int sum_val = data[0] + data[1];
277
40.1k
  int diff_val = data[0] - data[1];
278
279
40.1k
  if (sum_val > lav) {
280
14.6k
    data[0] = -sum_val + (2 * lav + 1);
281
14.6k
    data[1] = -diff_val;
282
25.4k
  } else {
283
25.4k
    data[0] = sum_val;
284
25.4k
    data[1] = diff_val;
285
25.4k
  }
286
287
40.1k
  if (data[0] + data[1] != 0) {
288
21.6k
    sym_bit = FDKreadBits(strm, 1);
289
21.6k
    if (sym_bit) {
290
7.10k
      data[0] = -data[0];
291
7.10k
      data[1] = -data[1];
292
7.10k
    }
293
21.6k
  }
294
295
40.1k
  if (data[0] - data[1] != 0) {
296
21.1k
    sym_bit = FDKreadBits(strm, 1);
297
21.1k
    if (sym_bit) {
298
7.96k
      int tmp;
299
7.96k
      tmp = data[0];
300
7.96k
      data[0] = data[1];
301
7.96k
      data[1] = tmp;
302
7.96k
    }
303
21.1k
  }
304
305
40.1k
  return HUFFDEC_OK;
306
40.1k
}
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
16.0k
{
313
16.0k
  ERROR_t err = HUFFDEC_OK;
314
16.0k
  int i = 0, node = 0, offset = 0;
315
16.0k
  int od = 0, od_sign = 0;
316
16.0k
  ULONG data = 0;
317
16.0k
  int bitsAvail = 0;
318
319
16.0k
  const SHORT(*partTab)[MAX_ENTRIES][2] = NULL;
320
16.0k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
321
322
16.0k
  switch (data_type) {
323
6.27k
    case t_CLD:
324
6.27k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
325
6.27k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h1D[dim1]->nodeTab[0][0];
326
6.27k
      break;
327
6.55k
    case t_ICC:
328
6.55k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
329
6.55k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h1D[dim1]->nodeTab[0][0];
330
6.55k
      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
3.18k
    case t_IPD:
336
3.18k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
337
3.18k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h1D[dim1].nodeTab[0][0];
338
3.18k
      break;
339
0
    default:
340
0
      FDK_ASSERT(0);
341
0
      err = HUFFDEC_NOTOK;
342
0
      goto bail;
343
16.0k
  }
344
345
16.0k
  if (p0_flag) {
346
5.44k
    if ((err = huff_read(strm, partTab, &node)) != HUFFDEC_OK) {
347
0
      goto bail;
348
0
    }
349
350
5.44k
    out_data[0] = -(node + 1);
351
5.44k
    offset = 1;
352
5.44k
  }
353
354
73.1k
  for (i = offset; i < num_val; i++) {
355
58.3k
    bitsAvail = FDKgetValidBits(strm);
356
58.3k
    if (bitsAvail < 1) {
357
1.17k
      err = HUFFDEC_NOTOK;
358
1.17k
      goto bail;
359
1.17k
    }
360
361
57.2k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
362
0
      goto bail;
363
0
    }
364
57.2k
    od = -(node + 1);
365
366
57.2k
    if (data_type != t_IPD) {
367
43.4k
      if (od != 0) {
368
10.4k
        bitsAvail = FDKgetValidBits(strm);
369
10.4k
        if (bitsAvail < 1) {
370
33
          err = HUFFDEC_NOTOK;
371
33
          goto bail;
372
33
        }
373
374
10.4k
        data = FDKreadBits(strm, 1);
375
10.4k
        od_sign = data;
376
377
10.4k
        if (od_sign) od = -od;
378
10.4k
      }
379
43.4k
    }
380
381
57.1k
    out_data[i] = od;
382
57.1k
  }
383
384
16.0k
bail:
385
16.0k
  return err;
386
16.0k
}
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
18.1k
                           SCHAR* p0_data[2]) {
392
18.1k
  ERROR_t err = HUFFDEC_OK;
393
18.1k
  int i = 0, lav = 0, escape = 0, escCntr = 0;
394
18.1k
  int node = 0;
395
18.1k
  unsigned long data = 0;
396
397
18.1k
  SCHAR esc_data[2][28] = {{0}};
398
18.1k
  int escIdx[28] = {0};
399
18.1k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
400
401
  /* LAV */
402
18.1k
  if ((err =
403
18.1k
           huff_read(strm, (HANDLE_HUFF_NODE)&FDK_huffLavIdxNodes.nodeTab[0][0],
404
18.1k
                     &node)) != HUFFDEC_OK) {
405
0
    goto bail;
406
0
  }
407
18.1k
  data = -(node + 1);
408
409
18.1k
  switch (data_type) {
410
9.49k
    case t_CLD:
411
9.49k
      lav = 2 * data + 3; /* 3, 5, 7, 9 */
412
9.49k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
413
9.49k
      break;
414
5.22k
    case t_ICC:
415
5.22k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
416
5.22k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
417
5.22k
      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
3.43k
    case t_IPD:
423
3.43k
      if (data == 0)
424
1.15k
        data = 3;
425
2.27k
      else
426
2.27k
        data--;
427
3.43k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
428
3.43k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
429
3.43k
      break;
430
0
    default:
431
0
      FDK_ASSERT(0);
432
0
      err = HUFFDEC_NOTOK;
433
0
      goto bail;
434
18.1k
  }
435
436
  /* Partition 0 */
437
18.1k
  if (p0_data[0] != NULL) {
438
9.82k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
439
0
      goto bail;
440
0
    }
441
9.82k
    *p0_data[0] = -(node + 1);
442
9.82k
  }
443
18.1k
  if (p0_data[1] != NULL) {
444
6.05k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
445
0
      goto bail;
446
0
    }
447
6.05k
    *p0_data[1] = -(node + 1);
448
6.05k
  }
449
450
18.1k
  switch (data_type) {
451
9.49k
    case t_CLD:
452
9.49k
      switch (lav) {
453
2.72k
        case 3:
454
2.72k
          nodeTab =
455
2.72k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav3[0][0];
456
2.72k
          break;
457
563
        case 5:
458
563
          nodeTab =
459
563
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav5[0][0];
460
563
          break;
461
2.71k
        case 7:
462
2.71k
          nodeTab =
463
2.71k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav7[0][0];
464
2.71k
          break;
465
3.49k
        case 9:
466
3.49k
          nodeTab =
467
3.49k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav9[0][0];
468
3.49k
          break;
469
9.49k
      }
470
9.49k
      break;
471
9.49k
    case t_ICC:
472
5.22k
      switch (lav) {
473
2.31k
        case 1:
474
2.31k
          nodeTab =
475
2.31k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav1[0][0];
476
2.31k
          break;
477
1.06k
        case 3:
478
1.06k
          nodeTab =
479
1.06k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav3[0][0];
480
1.06k
          break;
481
534
        case 5:
482
534
          nodeTab =
483
534
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav5[0][0];
484
534
          break;
485
1.31k
        case 7:
486
1.31k
          nodeTab =
487
1.31k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav7[0][0];
488
1.31k
          break;
489
5.22k
      }
490
5.22k
      break;
491
5.22k
    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
3.43k
    case t_IPD:
509
3.43k
      switch (lav) {
510
1.87k
        case 1:
511
1.87k
          nodeTab =
512
1.87k
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav1[0][0];
513
1.87k
          break;
514
109
        case 3:
515
109
          nodeTab =
516
109
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav3[0][0];
517
109
          break;
518
296
        case 5:
519
296
          nodeTab =
520
296
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav5[0][0];
521
296
          break;
522
1.15k
        case 7:
523
1.15k
          nodeTab =
524
1.15k
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav7[0][0];
525
1.15k
          break;
526
3.43k
      }
527
3.43k
      break;
528
3.43k
    default:
529
0
      break;
530
18.1k
  }
531
532
69.3k
  for (i = 0; i < num_val; i += stride) {
533
51.1k
    if ((err = huff_read_2D(strm, nodeTab, out_data[i], &escape)) !=
534
51.1k
        HUFFDEC_OK) {
535
0
      goto bail;
536
0
    }
537
538
51.1k
    if (escape) {
539
605
      escIdx[escCntr++] = i;
540
50.5k
    } else {
541
50.5k
      if (data_type == t_IPD) {
542
10.4k
        if ((err = sym_restoreIPD(strm, lav, out_data[i])) != HUFFDEC_OK) {
543
0
          goto bail;
544
0
        }
545
40.1k
      } else {
546
40.1k
        if ((err = sym_restore(strm, lav, out_data[i])) != HUFFDEC_OK) {
547
0
          goto bail;
548
0
        }
549
40.1k
      }
550
50.5k
    }
551
51.1k
  } /* i */
552
553
18.1k
  if (escCntr > 0) {
554
391
    if ((err = pcm_decode(strm, esc_data[0], esc_data[1], 0, 2 * escCntr,
555
391
                          (2 * lav + 1))) != HUFFDEC_OK) {
556
0
      goto bail;
557
0
    }
558
559
996
    for (i = 0; i < escCntr; i++) {
560
605
      out_data[escIdx[i]][0] = esc_data[0][i] - lav;
561
605
      out_data[escIdx[i]][1] = esc_data[1][i] - lav;
562
605
    }
563
391
  }
564
18.1k
bail:
565
18.1k
  return err;
566
18.1k
}
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
21.2k
                           int num_val, PAIRING* pairing_scheme, int ldMode) {
572
21.2k
  ERROR_t err = HUFFDEC_OK;
573
21.2k
  CODING_SCHEME coding_scheme = HUFF_1D;
574
21.2k
  DIFF_TYPE diff_type;
575
576
21.2k
  int i = 0;
577
578
21.2k
  SCHAR pair_vec[28][2];
579
580
21.2k
  SCHAR* p0_data_1[2] = {NULL, NULL};
581
21.2k
  SCHAR* p0_data_2[2] = {NULL, NULL};
582
583
21.2k
  int p0_flag[2];
584
585
21.2k
  int num_val_1_int = num_val;
586
21.2k
  int num_val_2_int = num_val;
587
588
21.2k
  SCHAR* out_data_1_int = out_data_1;
589
21.2k
  SCHAR* out_data_2_int = out_data_2;
590
591
21.2k
  int df_rest_flag_1 = 0;
592
21.2k
  int df_rest_flag_2 = 0;
593
594
21.2k
  int hufYY1;
595
21.2k
  int hufYY2;
596
21.2k
  int hufYY;
597
598
  /* Coding scheme */
599
21.2k
  coding_scheme = (CODING_SCHEME)FDKreadBits(strm, 1);
600
601
21.2k
  if (coding_scheme == HUFF_2D) {
602
13.9k
    if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ldMode == 0)) {
603
6.44k
      *pairing_scheme = (PAIRING)FDKreadBits(strm, 1);
604
7.45k
    } else {
605
7.45k
      *pairing_scheme = FREQ_PAIR;
606
7.45k
    }
607
13.9k
  }
608
609
21.2k
  {
610
21.2k
    hufYY1 = diff_type_1;
611
21.2k
    hufYY2 = diff_type_2;
612
21.2k
  }
613
614
21.2k
  switch (coding_scheme) {
615
7.39k
    case HUFF_1D:
616
7.39k
      p0_flag[0] = (diff_type_1 == DIFF_FREQ);
617
7.39k
      p0_flag[1] = (diff_type_2 == DIFF_FREQ);
618
7.39k
      if (out_data_1 != NULL) {
619
7.39k
        if ((err = huff_dec_1D(strm, data_type, hufYY1, out_data_1,
620
7.39k
                               num_val_1_int, p0_flag[0])) != HUFFDEC_OK) {
621
553
          goto bail;
622
553
        }
623
7.39k
      }
624
6.84k
      if (out_data_2 != NULL) {
625
1.66k
        if ((err = huff_dec_1D(strm, data_type, hufYY2, out_data_2,
626
1.66k
                               num_val_2_int, p0_flag[1])) != HUFFDEC_OK) {
627
8
          goto bail;
628
8
        }
629
1.66k
      }
630
631
6.83k
      break; /* HUFF_1D */
632
633
13.9k
    case HUFF_2D:
634
635
13.9k
      switch (*pairing_scheme) {
636
9.28k
        case FREQ_PAIR:
637
638
9.28k
          if (out_data_1 != NULL) {
639
9.28k
            if (diff_type_1 == DIFF_FREQ) {
640
5.58k
              p0_data_1[0] = &out_data_1[0];
641
5.58k
              p0_data_1[1] = NULL;
642
643
5.58k
              num_val_1_int -= 1;
644
5.58k
              out_data_1_int += 1;
645
5.58k
            }
646
9.28k
            df_rest_flag_1 = num_val_1_int % 2;
647
9.28k
            if (df_rest_flag_1) num_val_1_int -= 1;
648
9.28k
            if (num_val_1_int < 0) {
649
3
              err = HUFFDEC_NOTOK;
650
3
              goto bail;
651
3
            }
652
9.28k
          }
653
9.28k
          if (out_data_2 != NULL) {
654
4.30k
            if (diff_type_2 == DIFF_FREQ) {
655
1.83k
              p0_data_2[0] = NULL;
656
1.83k
              p0_data_2[1] = &out_data_2[0];
657
658
1.83k
              num_val_2_int -= 1;
659
1.83k
              out_data_2_int += 1;
660
1.83k
            }
661
4.30k
            df_rest_flag_2 = num_val_2_int % 2;
662
4.30k
            if (df_rest_flag_2) num_val_2_int -= 1;
663
4.30k
            if (num_val_2_int < 0) {
664
1
              err = HUFFDEC_NOTOK;
665
1
              goto bail;
666
1
            }
667
4.30k
          }
668
669
9.28k
          if (out_data_1 != NULL) {
670
9.28k
            if ((err = huff_dec_2D(strm, data_type, hufYY1, FREQ_PAIR, pair_vec,
671
9.28k
                                   num_val_1_int, 2, p0_data_1)) !=
672
9.28k
                HUFFDEC_OK) {
673
0
              goto bail;
674
0
            }
675
9.28k
            if (df_rest_flag_1) {
676
4.41k
              if ((err = huff_dec_1D(strm, data_type, hufYY1,
677
4.41k
                                     out_data_1_int + num_val_1_int, 1, 0)) !=
678
4.41k
                  HUFFDEC_OK) {
679
458
                goto bail;
680
458
              }
681
4.41k
            }
682
9.28k
          }
683
8.82k
          if (out_data_2 != NULL) {
684
4.25k
            if ((err = huff_dec_2D(strm, data_type, hufYY2, FREQ_PAIR,
685
4.25k
                                   pair_vec + 1, num_val_2_int, 2,
686
4.25k
                                   p0_data_2)) != HUFFDEC_OK) {
687
0
              goto bail;
688
0
            }
689
4.25k
            if (df_rest_flag_2) {
690
2.52k
              if ((err = huff_dec_1D(strm, data_type, hufYY2,
691
2.52k
                                     out_data_2_int + num_val_2_int, 1, 0)) !=
692
2.52k
                  HUFFDEC_OK) {
693
190
                goto bail;
694
190
              }
695
2.52k
            }
696
4.25k
          }
697
698
8.63k
          if (out_data_1 != NULL) {
699
34.2k
            for (i = 0; i < num_val_1_int - 1; i += 2) {
700
25.6k
              out_data_1_int[i] = pair_vec[i][0];
701
25.6k
              out_data_1_int[i + 1] = pair_vec[i][1];
702
25.6k
            }
703
8.63k
          }
704
8.63k
          if (out_data_2 != NULL) {
705
18.8k
            for (i = 0; i < num_val_2_int - 1; i += 2) {
706
14.7k
              out_data_2_int[i] = pair_vec[i + 1][0];
707
14.7k
              out_data_2_int[i + 1] = pair_vec[i + 1][1];
708
14.7k
            }
709
4.06k
          }
710
8.63k
          break; /* FREQ_PAIR */
711
712
4.61k
        case TIME_PAIR:
713
4.61k
          if (((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
714
4.23k
            p0_data_1[0] = &out_data_1[0];
715
4.23k
            p0_data_1[1] = &out_data_2[0];
716
717
4.23k
            out_data_1_int += 1;
718
4.23k
            out_data_2_int += 1;
719
720
4.23k
            num_val_1_int -= 1;
721
4.23k
          }
722
723
4.61k
          if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
724
3.45k
            diff_type = DIFF_TIME;
725
3.45k
          } else {
726
1.16k
            diff_type = DIFF_FREQ;
727
1.16k
          }
728
4.61k
          { hufYY = diff_type; }
729
730
4.61k
          if ((err = huff_dec_2D(strm, data_type, hufYY, TIME_PAIR, pair_vec,
731
4.61k
                                 num_val_1_int, 1, p0_data_1)) != HUFFDEC_OK) {
732
0
            goto bail;
733
0
          }
734
735
13.5k
          for (i = 0; i < num_val_1_int; i++) {
736
8.88k
            out_data_1_int[i] = pair_vec[i][0];
737
8.88k
            out_data_2_int[i] = pair_vec[i][1];
738
8.88k
          }
739
740
4.61k
          break; /* TIME_PAIR */
741
742
0
        default:
743
0
          break;
744
13.9k
      }
745
746
13.2k
      break; /* HUFF_2D */
747
748
13.2k
    default:
749
0
      break;
750
21.2k
  }
751
21.2k
bail:
752
21.2k
  return err;
753
21.2k
}
754
755
static void diff_freq_decode(const SCHAR* const diff_data,
756
27.2k
                             SCHAR* const out_data, const int num_val) {
757
27.2k
  int i = 0;
758
27.2k
  out_data[0] = diff_data[0];
759
760
162k
  for (i = 1; i < num_val; i++) {
761
134k
    out_data[i] = out_data[i - 1] + diff_data[i];
762
134k
  }
763
27.2k
}
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
9.78k
                                       const int num_val) {
770
9.78k
  int i = 0; /* default start value*/
771
772
9.78k
  if (mixed_diff_type) {
773
1.52k
    out_data[0] = diff_data[0];
774
1.52k
    i = 1; /* new start value */
775
1.52k
  }
776
79.9k
  for (; i < num_val; i++) {
777
70.1k
    out_data[i] = prev_data[i] + diff_data[i];
778
70.1k
  }
779
9.78k
}
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
3.13k
                                      const int num_val) {
786
3.13k
  int i = 0; /* default start value*/
787
788
3.13k
  if (mixed_diff_type) {
789
1.55k
    out_data[0] = diff_data[0];
790
1.55k
    i = 1; /* new start value */
791
1.55k
  }
792
10.9k
  for (; i < num_val; i++) {
793
7.85k
    out_data[i] = prev_data[i] - diff_data[i];
794
7.85k
  }
795
3.13k
}
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
30.4k
                          SCHAR* out_data) {
800
30.4k
  int i = 0, lsb = 0;
801
30.4k
  ULONG data = 0;
802
803
206k
  for (i = 0; i < num_val; i++) {
804
175k
    int msb;
805
175k
    msb = in_data_msb[i];
806
807
175k
    if (num_lsb > 0) {
808
29.0k
      data = FDKreadBits(strm, num_lsb);
809
29.0k
      lsb = data;
810
811
29.0k
      out_data[i] = ((msb << num_lsb) | lsb) - offset;
812
29.0k
    } else
813
146k
      out_data[i] = msb - offset;
814
175k
  }
815
816
30.4k
  return HUFFDEC_OK; /* dummy */
817
30.4k
}
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
41.8k
{
826
41.8k
  ERROR_t err = HUFFDEC_OK;
827
828
  // int allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
829
41.8k
  int attachLsb_flag = 0;
830
41.8k
  int pcmCoding_flag = 0;
831
832
41.8k
  int mixed_time_pair = 0, numValPcm = 0;
833
41.8k
  int quant_levels = 0, quant_offset = 0;
834
41.8k
  ULONG data = 0;
835
836
41.8k
  SCHAR aaDataPair[2][28] = {{0}};
837
41.8k
  SCHAR aaDataDiff[2][28] = {{0}};
838
839
41.8k
  SCHAR aHistoryMsb[28] = {0};
840
841
41.8k
  SCHAR* pDataVec[2] = {NULL, NULL};
842
843
41.8k
  DIFF_TYPE diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
844
41.8k
  PAIRING pairing = FREQ_PAIR;
845
41.8k
  DIRECTION direction = BACKWARDS;
846
847
41.8k
  switch (data_type) {
848
18.7k
    case t_CLD:
849
18.7k
      if (coarse_flag) {
850
12.3k
        attachLsb_flag = 0;
851
12.3k
        quant_levels = 15;
852
12.3k
        quant_offset = 7;
853
12.3k
      } else {
854
6.39k
        attachLsb_flag = 0;
855
6.39k
        quant_levels = 31;
856
6.39k
        quant_offset = 15;
857
6.39k
      }
858
859
18.7k
      break;
860
861
16.9k
    case t_ICC:
862
16.9k
      if (coarse_flag) {
863
9.46k
        attachLsb_flag = 0;
864
9.46k
        quant_levels = 4;
865
9.46k
        quant_offset = 0;
866
9.46k
      } else {
867
7.49k
        attachLsb_flag = 0;
868
7.49k
        quant_levels = 8;
869
7.49k
        quant_offset = 0;
870
7.49k
      }
871
872
16.9k
      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
6.13k
    case t_IPD:
899
6.13k
      if (!coarse_flag) {
900
4.04k
        attachLsb_flag = 1;
901
4.04k
        quant_levels = 16;
902
4.04k
        quant_offset = 0;
903
4.04k
      } else {
904
2.09k
        attachLsb_flag = 0;
905
2.09k
        quant_levels = 8;
906
2.09k
        quant_offset = 0;
907
2.09k
      }
908
6.13k
      break;
909
910
0
    default:
911
0
      return HUFFDEC_NOTOK;
912
41.8k
  }
913
914
41.8k
  data = FDKreadBits(strm, 1);
915
41.8k
  pcmCoding_flag = data;
916
917
41.8k
  if (pcmCoding_flag) {
918
20.5k
    if (pair_flag) {
919
14.0k
      pDataVec[0] = aaDataPair[0];
920
14.0k
      pDataVec[1] = aaDataPair[1];
921
14.0k
      numValPcm = 2 * dataBands;
922
14.0k
    } else {
923
6.50k
      pDataVec[0] = aaDataPair[0];
924
6.50k
      pDataVec[1] = NULL;
925
6.50k
      numValPcm = dataBands;
926
6.50k
    }
927
928
20.5k
    err = pcm_decode(strm, pDataVec[0], pDataVec[1], quant_offset, numValPcm,
929
20.5k
                     quant_levels);
930
20.5k
    if (err != HUFFDEC_OK) return HUFFDEC_NOTOK;
931
932
21.2k
  } else { /* Differential/Huffman/LSB Coding */
933
934
21.2k
    if (pair_flag) {
935
10.7k
      pDataVec[0] = aaDataDiff[0];
936
10.7k
      pDataVec[1] = aaDataDiff[1];
937
10.7k
    } else {
938
10.5k
      pDataVec[0] = aaDataDiff[0];
939
10.5k
      pDataVec[1] = NULL;
940
10.5k
    }
941
942
21.2k
    diff_type[0] = DIFF_FREQ;
943
21.2k
    diff_type[1] = DIFF_FREQ;
944
945
21.2k
    direction = BACKWARDS;
946
21.2k
    {
947
21.2k
      if (pair_flag || allowDiffTimeBack_flag) {
948
18.4k
        data = FDKreadBits(strm, 1);
949
18.4k
        diff_type[0] = (DIFF_TYPE)data;
950
18.4k
      }
951
952
21.2k
      if (pair_flag &&
953
10.7k
          ((diff_type[0] == DIFF_FREQ) || allowDiffTimeBack_flag)) {
954
8.93k
        data = FDKreadBits(strm, 1);
955
8.93k
        diff_type[1] = (DIFF_TYPE)data;
956
8.93k
      }
957
21.2k
    }
958
    /* Huffman decoding */
959
21.2k
    err = huff_decode(strm, pDataVec[0], pDataVec[1], data_type, diff_type[0],
960
21.2k
                      diff_type[1], dataBands, &pairing,
961
21.2k
                      (DECODER == SAOC_DECODER));
962
21.2k
    if (err != HUFFDEC_OK) {
963
1.21k
      return HUFFDEC_NOTOK;
964
1.21k
    }
965
966
20.0k
    {
967
      /* Differential decoding */
968
20.0k
      if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
969
11.8k
        if (DECODER == SAOC_DECODER) {
970
2.38k
          direction = BACKWARDS;
971
9.43k
        } else {
972
9.43k
          if (pair_flag) {
973
5.67k
            if ((diff_type[0] == DIFF_TIME) && !allowDiffTimeBack_flag) {
974
1.83k
              direction = FORWARDS;
975
3.83k
            } else if (diff_type[1] == DIFF_TIME) {
976
2.36k
              direction = BACKWARDS;
977
2.36k
            } else {
978
1.47k
              data = FDKreadBits(strm, 1);
979
1.47k
              direction = (DIRECTION)data;
980
1.47k
            }
981
5.67k
          } else {
982
3.76k
            direction = BACKWARDS;
983
3.76k
          }
984
9.43k
        }
985
11.8k
      }
986
987
20.0k
      mixed_time_pair =
988
20.0k
          (diff_type[0] != diff_type[1]) && (pairing == TIME_PAIR);
989
990
20.0k
      if (direction == BACKWARDS) {
991
16.9k
        if (diff_type[0] == DIFF_FREQ) {
992
11.4k
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
993
11.4k
        } else {
994
5.54k
          int i;
995
51.9k
          for (i = 0; i < dataBands; i++) {
996
46.4k
            aHistoryMsb[i] = aHistory[i + startBand] + quant_offset;
997
46.4k
            if (attachLsb_flag) {
998
8.88k
              aHistoryMsb[i] >>= 1;
999
8.88k
            }
1000
46.4k
          }
1001
5.54k
          diff_time_decode_backwards(aHistoryMsb, aaDataDiff[0], aaDataPair[0],
1002
5.54k
                                     mixed_time_pair, dataBands);
1003
5.54k
        }
1004
16.9k
        if (diff_type[1] == DIFF_FREQ) {
1005
12.7k
          diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1006
12.7k
        } else {
1007
4.24k
          diff_time_decode_backwards(aaDataPair[0], aaDataDiff[1],
1008
4.24k
                                     aaDataPair[1], mixed_time_pair, dataBands);
1009
4.24k
        }
1010
16.9k
      } else {
1011
        /* diff_type[1] MUST BE DIFF_FREQ */
1012
3.13k
        diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1013
1014
3.13k
        if (diff_type[0] == DIFF_FREQ) {
1015
0
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
1016
3.13k
        } else {
1017
3.13k
          diff_time_decode_forwards(aaDataPair[1], aaDataDiff[0], aaDataPair[0],
1018
3.13k
                                    mixed_time_pair, dataBands);
1019
3.13k
        }
1020
3.13k
      }
1021
20.0k
    }
1022
1023
    /* LSB decoding */
1024
20.0k
    err = attach_lsb(strm, aaDataPair[0], quant_offset, attachLsb_flag ? 1 : 0,
1025
20.0k
                     dataBands, aaDataPair[0]);
1026
20.0k
    if (err != HUFFDEC_OK) goto bail;
1027
1028
20.0k
    if (pair_flag) {
1029
10.3k
      err = attach_lsb(strm, aaDataPair[1], quant_offset,
1030
10.3k
                       attachLsb_flag ? 1 : 0, dataBands, aaDataPair[1]);
1031
10.3k
      if (err != HUFFDEC_OK) goto bail;
1032
10.3k
    }
1033
20.0k
  } /* End: Differential/Huffman/LSB Coding */
1034
1035
  /* Copy data to output arrays */
1036
40.6k
  FDKmemcpy(aaOutData1 + startBand, aaDataPair[0], sizeof(SCHAR) * dataBands);
1037
40.6k
  if (pair_flag) {
1038
24.4k
    FDKmemcpy(aaOutData2 + startBand, aaDataPair[1], sizeof(SCHAR) * dataBands);
1039
24.4k
  }
1040
1041
40.6k
bail:
1042
40.6k
  return err;
1043
40.6k
}
1044
1045
ERROR_t huff_dec_reshape(HANDLE_FDK_BITSTREAM strm, int* out_data,
1046
1.48k
                         int num_val) {
1047
1.48k
  ERROR_t err = HUFFDEC_OK;
1048
1.48k
  int val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
1049
1.48k
  SCHAR rl_data[2] = {0};
1050
1051
23.2k
  while (val_rcvd < num_val) {
1052
21.9k
    err = huff_read_2D(strm,
1053
21.9k
                       (HANDLE_HUFF_NODE)&FDK_huffReshapeNodes.nodeTab[0][0],
1054
21.9k
                       rl_data, &dummy);
1055
21.9k
    if (err != HUFFDEC_OK) goto bail;
1056
21.9k
    val = rl_data[0];
1057
21.9k
    len = rl_data[1] + 1;
1058
21.9k
    if (val_rcvd + len > num_val) {
1059
170
      err = HUFFDEC_NOTOK;
1060
170
      goto bail;
1061
170
    }
1062
64.9k
    for (i = val_rcvd; i < val_rcvd + len; i++) {
1063
43.1k
      out_data[i] = val;
1064
43.1k
    }
1065
21.7k
    val_rcvd += len;
1066
21.7k
  }
1067
1.48k
bail:
1068
1.48k
  return err;
1069
1.48k
}