Coverage Report

Created: 2026-09-01 07:17

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
62.1k
#define min(a, b) (((a) < (b)) ? (a) : (b))
110
#endif
111
112
7.35k
ERROR_t sym_restoreIPD(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
113
7.35k
  int sum_val = data[0] + data[1];
114
7.35k
  int diff_val = data[0] - data[1];
115
116
7.35k
  if (sum_val > lav) {
117
2.51k
    data[0] = -sum_val + (2 * lav + 1);
118
2.51k
    data[1] = -diff_val;
119
4.83k
  } else {
120
4.83k
    data[0] = sum_val;
121
4.83k
    data[1] = diff_val;
122
4.83k
  }
123
124
7.35k
  if (data[0] - data[1] != 0) {
125
3.34k
    ULONG sym_bit;
126
3.34k
    sym_bit = FDKreadBits(strm, 1);
127
3.34k
    if (sym_bit) {
128
1.08k
      int tmp;
129
1.08k
      tmp = data[0];
130
1.08k
      data[0] = data[1];
131
1.08k
      data[1] = tmp;
132
1.08k
    }
133
3.34k
  }
134
135
7.35k
  return HUFFDEC_OK;
136
7.35k
}
137
138
15.7k
static int ilog2(unsigned int i) {
139
15.7k
  int l = 0;
140
141
15.7k
  if (i) i--;
142
69.6k
  while (i > 0) {
143
53.8k
    i >>= 1;
144
53.8k
    l++;
145
53.8k
  }
146
147
15.7k
  return l;
148
15.7k
}
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
14.6k
                          int num_levels) {
153
14.6k
  int i = 0, j = 0, idx = 0;
154
14.6k
  int max_grp_len = 0, next_val = 0;
155
14.6k
  ULONG tmp;
156
157
14.6k
  int pcm_chunk_size[7] = {0};
158
159
14.6k
  switch (num_levels) {
160
221
    case 3:
161
221
      max_grp_len = 5;
162
221
      break;
163
33
    case 7:
164
33
      max_grp_len = 6;
165
33
      break;
166
24
    case 11:
167
24
      max_grp_len = 2;
168
24
      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
4.80k
    case 4:
182
7.34k
    case 8:
183
13.3k
    case 15:
184
14.0k
    case 16:
185
14.0k
    case 26:
186
14.3k
    case 31:
187
14.3k
      max_grp_len = 1;
188
14.3k
      break;
189
0
    default:
190
0
      return HUFFDEC_NOTOK;
191
14.6k
  }
192
193
14.6k
  tmp = 1;
194
30.4k
  for (i = 1; i <= max_grp_len; i++) {
195
15.7k
    tmp *= num_levels;
196
15.7k
    pcm_chunk_size[i] = ilog2(tmp);
197
15.7k
  }
198
199
76.8k
  for (i = 0; i < num_val; i += max_grp_len) {
200
62.1k
    int grp_len, grp_val, data;
201
62.1k
    grp_len = min(max_grp_len, num_val - i);
202
62.1k
    data = FDKreadBits(strm, pcm_chunk_size[grp_len]);
203
204
62.1k
    grp_val = data;
205
206
124k
    for (j = 0; j < grp_len; j++) {
207
62.6k
      idx = i + (grp_len - j - 1);
208
62.6k
      next_val = grp_val % num_levels;
209
210
62.6k
      if (out_data_2 == NULL) {
211
30.4k
        out_data_1[idx] = next_val - offset;
212
32.2k
      } else if (out_data_1 == NULL) {
213
0
        out_data_2[idx] = next_val - offset;
214
32.2k
      } else {
215
32.2k
        if (idx % 2) {
216
16.1k
          out_data_2[idx / 2] = next_val - offset;
217
16.1k
        } else {
218
16.1k
          out_data_1[idx / 2] = next_val - offset;
219
16.1k
        }
220
32.2k
      }
221
222
62.6k
      grp_val = (grp_val - next_val) / num_levels;
223
62.6k
    }
224
62.1k
  }
225
226
14.6k
  return HUFFDEC_OK;
227
14.6k
}
228
229
static ERROR_t huff_read(HANDLE_FDK_BITSTREAM strm,
230
                         const SHORT (*nodeTab)[MAX_ENTRIES][2],
231
127k
                         int* out_data) {
232
127k
  int node = 0;
233
127k
  int len = 0;
234
235
319k
  do {
236
319k
    ULONG next_bit;
237
319k
    next_bit = FDKreadBits(strm, 1);
238
319k
    len++;
239
319k
    node = (*nodeTab)[node][next_bit];
240
319k
  } while (node > 0);
241
242
127k
  *out_data = node;
243
244
127k
  return HUFFDEC_OK;
245
127k
}
246
247
static ERROR_t huff_read_2D(HANDLE_FDK_BITSTREAM strm,
248
                            const SHORT (*nodeTab)[MAX_ENTRIES][2],
249
56.5k
                            SCHAR out_data[2], int* escape) {
250
56.5k
  ERROR_t err = HUFFDEC_OK;
251
252
56.5k
  int huff_2D_8bit = 0;
253
56.5k
  int node = 0;
254
255
56.5k
  if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
256
0
    goto bail;
257
0
  }
258
56.5k
  *escape = (node == 0);
259
260
56.5k
  if (*escape) {
261
417
    out_data[0] = 0;
262
417
    out_data[1] = 1;
263
56.1k
  } else {
264
56.1k
    huff_2D_8bit = -(node + 1);
265
56.1k
    out_data[0] = huff_2D_8bit >> 4;
266
56.1k
    out_data[1] = huff_2D_8bit & 0xf;
267
56.1k
  }
268
269
56.5k
bail:
270
56.5k
  return err;
271
56.5k
}
272
273
28.7k
static ERROR_t sym_restore(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
274
28.7k
  ULONG sym_bit = 0;
275
276
28.7k
  int sum_val = data[0] + data[1];
277
28.7k
  int diff_val = data[0] - data[1];
278
279
28.7k
  if (sum_val > lav) {
280
11.0k
    data[0] = -sum_val + (2 * lav + 1);
281
11.0k
    data[1] = -diff_val;
282
17.7k
  } else {
283
17.7k
    data[0] = sum_val;
284
17.7k
    data[1] = diff_val;
285
17.7k
  }
286
287
28.7k
  if (data[0] + data[1] != 0) {
288
15.9k
    sym_bit = FDKreadBits(strm, 1);
289
15.9k
    if (sym_bit) {
290
5.53k
      data[0] = -data[0];
291
5.53k
      data[1] = -data[1];
292
5.53k
    }
293
15.9k
  }
294
295
28.7k
  if (data[0] - data[1] != 0) {
296
15.7k
    sym_bit = FDKreadBits(strm, 1);
297
15.7k
    if (sym_bit) {
298
6.08k
      int tmp;
299
6.08k
      tmp = data[0];
300
6.08k
      data[0] = data[1];
301
6.08k
      data[1] = tmp;
302
6.08k
    }
303
15.7k
  }
304
305
28.7k
  return HUFFDEC_OK;
306
28.7k
}
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
11.9k
{
313
11.9k
  ERROR_t err = HUFFDEC_OK;
314
11.9k
  int i = 0, node = 0, offset = 0;
315
11.9k
  int od = 0, od_sign = 0;
316
11.9k
  ULONG data = 0;
317
11.9k
  int bitsAvail = 0;
318
319
11.9k
  const SHORT(*partTab)[MAX_ENTRIES][2] = NULL;
320
11.9k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
321
322
11.9k
  switch (data_type) {
323
4.54k
    case t_CLD:
324
4.54k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
325
4.54k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h1D[dim1]->nodeTab[0][0];
326
4.54k
      break;
327
5.34k
    case t_ICC:
328
5.34k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
329
5.34k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h1D[dim1]->nodeTab[0][0];
330
5.34k
      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
2.08k
    case t_IPD:
336
2.08k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
337
2.08k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h1D[dim1].nodeTab[0][0];
338
2.08k
      break;
339
0
    default:
340
0
      FDK_ASSERT(0);
341
0
      err = HUFFDEC_NOTOK;
342
0
      goto bail;
343
11.9k
  }
344
345
11.9k
  if (p0_flag) {
346
3.67k
    if ((err = huff_read(strm, partTab, &node)) != HUFFDEC_OK) {
347
0
      goto bail;
348
0
    }
349
350
3.67k
    out_data[0] = -(node + 1);
351
3.67k
    offset = 1;
352
3.67k
  }
353
354
53.2k
  for (i = offset; i < num_val; i++) {
355
42.0k
    bitsAvail = FDKgetValidBits(strm);
356
42.0k
    if (bitsAvail < 1) {
357
738
      err = HUFFDEC_NOTOK;
358
738
      goto bail;
359
738
    }
360
361
41.2k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
362
0
      goto bail;
363
0
    }
364
41.2k
    od = -(node + 1);
365
366
41.2k
    if (data_type != t_IPD) {
367
32.8k
      if (od != 0) {
368
9.67k
        bitsAvail = FDKgetValidBits(strm);
369
9.67k
        if (bitsAvail < 1) {
370
27
          err = HUFFDEC_NOTOK;
371
27
          goto bail;
372
27
        }
373
374
9.65k
        data = FDKreadBits(strm, 1);
375
9.65k
        od_sign = data;
376
377
9.65k
        if (od_sign) od = -od;
378
9.65k
      }
379
32.8k
    }
380
381
41.2k
    out_data[i] = od;
382
41.2k
  }
383
384
11.9k
bail:
385
11.9k
  return err;
386
11.9k
}
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
13.5k
                           SCHAR* p0_data[2]) {
392
13.5k
  ERROR_t err = HUFFDEC_OK;
393
13.5k
  int i = 0, lav = 0, escape = 0, escCntr = 0;
394
13.5k
  int node = 0;
395
13.5k
  unsigned long data = 0;
396
397
13.5k
  SCHAR esc_data[2][28] = {{0}};
398
13.5k
  int escIdx[28] = {0};
399
13.5k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
400
401
  /* LAV */
402
13.5k
  if ((err =
403
13.5k
           huff_read(strm, (HANDLE_HUFF_NODE)&FDK_huffLavIdxNodes.nodeTab[0][0],
404
13.5k
                     &node)) != HUFFDEC_OK) {
405
0
    goto bail;
406
0
  }
407
13.5k
  data = -(node + 1);
408
409
13.5k
  switch (data_type) {
410
7.04k
    case t_CLD:
411
7.04k
      lav = 2 * data + 3; /* 3, 5, 7, 9 */
412
7.04k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
413
7.04k
      break;
414
4.21k
    case t_ICC:
415
4.21k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
416
4.21k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
417
4.21k
      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
2.30k
    case t_IPD:
423
2.30k
      if (data == 0)
424
758
        data = 3;
425
1.54k
      else
426
1.54k
        data--;
427
2.30k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
428
2.30k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
429
2.30k
      break;
430
0
    default:
431
0
      FDK_ASSERT(0);
432
0
      err = HUFFDEC_NOTOK;
433
0
      goto bail;
434
13.5k
  }
435
436
  /* Partition 0 */
437
13.5k
  if (p0_data[0] != NULL) {
438
7.60k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
439
0
      goto bail;
440
0
    }
441
7.60k
    *p0_data[0] = -(node + 1);
442
7.60k
  }
443
13.5k
  if (p0_data[1] != NULL) {
444
4.90k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
445
0
      goto bail;
446
0
    }
447
4.90k
    *p0_data[1] = -(node + 1);
448
4.90k
  }
449
450
13.5k
  switch (data_type) {
451
7.04k
    case t_CLD:
452
7.04k
      switch (lav) {
453
2.10k
        case 3:
454
2.10k
          nodeTab =
455
2.10k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav3[0][0];
456
2.10k
          break;
457
362
        case 5:
458
362
          nodeTab =
459
362
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav5[0][0];
460
362
          break;
461
2.39k
        case 7:
462
2.39k
          nodeTab =
463
2.39k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav7[0][0];
464
2.39k
          break;
465
2.17k
        case 9:
466
2.17k
          nodeTab =
467
2.17k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav9[0][0];
468
2.17k
          break;
469
7.04k
      }
470
7.04k
      break;
471
7.04k
    case t_ICC:
472
4.21k
      switch (lav) {
473
1.79k
        case 1:
474
1.79k
          nodeTab =
475
1.79k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav1[0][0];
476
1.79k
          break;
477
837
        case 3:
478
837
          nodeTab =
479
837
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav3[0][0];
480
837
          break;
481
250
        case 5:
482
250
          nodeTab =
483
250
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav5[0][0];
484
250
          break;
485
1.32k
        case 7:
486
1.32k
          nodeTab =
487
1.32k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav7[0][0];
488
1.32k
          break;
489
4.21k
      }
490
4.21k
      break;
491
4.21k
    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
2.30k
    case t_IPD:
509
2.30k
      switch (lav) {
510
1.26k
        case 1:
511
1.26k
          nodeTab =
512
1.26k
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav1[0][0];
513
1.26k
          break;
514
69
        case 3:
515
69
          nodeTab =
516
69
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav3[0][0];
517
69
          break;
518
215
        case 5:
519
215
          nodeTab =
520
215
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav5[0][0];
521
215
          break;
522
758
        case 7:
523
758
          nodeTab =
524
758
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav7[0][0];
525
758
          break;
526
2.30k
      }
527
2.30k
      break;
528
2.30k
    default:
529
0
      break;
530
13.5k
  }
531
532
50.0k
  for (i = 0; i < num_val; i += stride) {
533
36.5k
    if ((err = huff_read_2D(strm, nodeTab, out_data[i], &escape)) !=
534
36.5k
        HUFFDEC_OK) {
535
0
      goto bail;
536
0
    }
537
538
36.5k
    if (escape) {
539
417
      escIdx[escCntr++] = i;
540
36.1k
    } else {
541
36.1k
      if (data_type == t_IPD) {
542
7.35k
        if ((err = sym_restoreIPD(strm, lav, out_data[i])) != HUFFDEC_OK) {
543
0
          goto bail;
544
0
        }
545
28.7k
      } else {
546
28.7k
        if ((err = sym_restore(strm, lav, out_data[i])) != HUFFDEC_OK) {
547
0
          goto bail;
548
0
        }
549
28.7k
      }
550
36.1k
    }
551
36.5k
  } /* i */
552
553
13.5k
  if (escCntr > 0) {
554
279
    if ((err = pcm_decode(strm, esc_data[0], esc_data[1], 0, 2 * escCntr,
555
279
                          (2 * lav + 1))) != HUFFDEC_OK) {
556
0
      goto bail;
557
0
    }
558
559
696
    for (i = 0; i < escCntr; i++) {
560
417
      out_data[escIdx[i]][0] = esc_data[0][i] - lav;
561
417
      out_data[escIdx[i]][1] = esc_data[1][i] - lav;
562
417
    }
563
279
  }
564
13.5k
bail:
565
13.5k
  return err;
566
13.5k
}
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
16.0k
                           int num_val, PAIRING* pairing_scheme, int ldMode) {
572
16.0k
  ERROR_t err = HUFFDEC_OK;
573
16.0k
  CODING_SCHEME coding_scheme = HUFF_1D;
574
16.0k
  DIFF_TYPE diff_type;
575
576
16.0k
  int i = 0;
577
578
16.0k
  SCHAR pair_vec[28][2];
579
580
16.0k
  SCHAR* p0_data_1[2] = {NULL, NULL};
581
16.0k
  SCHAR* p0_data_2[2] = {NULL, NULL};
582
583
16.0k
  int p0_flag[2];
584
585
16.0k
  int num_val_1_int = num_val;
586
16.0k
  int num_val_2_int = num_val;
587
588
16.0k
  SCHAR* out_data_1_int = out_data_1;
589
16.0k
  SCHAR* out_data_2_int = out_data_2;
590
591
16.0k
  int df_rest_flag_1 = 0;
592
16.0k
  int df_rest_flag_2 = 0;
593
594
16.0k
  int hufYY1;
595
16.0k
  int hufYY2;
596
16.0k
  int hufYY;
597
598
  /* Coding scheme */
599
16.0k
  coding_scheme = (CODING_SCHEME)FDKreadBits(strm, 1);
600
601
16.0k
  if (coding_scheme == HUFF_2D) {
602
10.5k
    if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ldMode == 0)) {
603
5.14k
      *pairing_scheme = (PAIRING)FDKreadBits(strm, 1);
604
5.38k
    } else {
605
5.38k
      *pairing_scheme = FREQ_PAIR;
606
5.38k
    }
607
10.5k
  }
608
609
16.0k
  {
610
16.0k
    hufYY1 = diff_type_1;
611
16.0k
    hufYY2 = diff_type_2;
612
16.0k
  }
613
614
16.0k
  switch (coding_scheme) {
615
5.52k
    case HUFF_1D:
616
5.52k
      p0_flag[0] = (diff_type_1 == DIFF_FREQ);
617
5.52k
      p0_flag[1] = (diff_type_2 == DIFF_FREQ);
618
5.52k
      if (out_data_1 != NULL) {
619
5.52k
        if ((err = huff_dec_1D(strm, data_type, hufYY1, out_data_1,
620
5.52k
                               num_val_1_int, p0_flag[0])) != HUFFDEC_OK) {
621
403
          goto bail;
622
403
        }
623
5.52k
      }
624
5.12k
      if (out_data_2 != NULL) {
625
1.16k
        if ((err = huff_dec_1D(strm, data_type, hufYY2, out_data_2,
626
1.16k
                               num_val_2_int, p0_flag[1])) != HUFFDEC_OK) {
627
4
          goto bail;
628
4
        }
629
1.16k
      }
630
631
5.11k
      break; /* HUFF_1D */
632
633
10.5k
    case HUFF_2D:
634
635
10.5k
      switch (*pairing_scheme) {
636
6.82k
        case FREQ_PAIR:
637
638
6.82k
          if (out_data_1 != NULL) {
639
6.82k
            if (diff_type_1 == DIFF_FREQ) {
640
4.22k
              p0_data_1[0] = &out_data_1[0];
641
4.22k
              p0_data_1[1] = NULL;
642
643
4.22k
              num_val_1_int -= 1;
644
4.22k
              out_data_1_int += 1;
645
4.22k
            }
646
6.82k
            df_rest_flag_1 = num_val_1_int % 2;
647
6.82k
            if (df_rest_flag_1) num_val_1_int -= 1;
648
6.82k
            if (num_val_1_int < 0) {
649
0
              err = HUFFDEC_NOTOK;
650
0
              goto bail;
651
0
            }
652
6.82k
          }
653
6.82k
          if (out_data_2 != NULL) {
654
3.06k
            if (diff_type_2 == DIFF_FREQ) {
655
1.53k
              p0_data_2[0] = NULL;
656
1.53k
              p0_data_2[1] = &out_data_2[0];
657
658
1.53k
              num_val_2_int -= 1;
659
1.53k
              out_data_2_int += 1;
660
1.53k
            }
661
3.06k
            df_rest_flag_2 = num_val_2_int % 2;
662
3.06k
            if (df_rest_flag_2) num_val_2_int -= 1;
663
3.06k
            if (num_val_2_int < 0) {
664
1
              err = HUFFDEC_NOTOK;
665
1
              goto bail;
666
1
            }
667
3.06k
          }
668
669
6.82k
          if (out_data_1 != NULL) {
670
6.82k
            if ((err = huff_dec_2D(strm, data_type, hufYY1, FREQ_PAIR, pair_vec,
671
6.82k
                                   num_val_1_int, 2, p0_data_1)) !=
672
6.82k
                HUFFDEC_OK) {
673
0
              goto bail;
674
0
            }
675
6.82k
            if (df_rest_flag_1) {
676
3.39k
              if ((err = huff_dec_1D(strm, data_type, hufYY1,
677
3.39k
                                     out_data_1_int + num_val_1_int, 1, 0)) !=
678
3.39k
                  HUFFDEC_OK) {
679
259
                goto bail;
680
259
              }
681
3.39k
            }
682
6.82k
          }
683
6.56k
          if (out_data_2 != NULL) {
684
3.02k
            if ((err = huff_dec_2D(strm, data_type, hufYY2, FREQ_PAIR,
685
3.02k
                                   pair_vec + 1, num_val_2_int, 2,
686
3.02k
                                   p0_data_2)) != HUFFDEC_OK) {
687
0
              goto bail;
688
0
            }
689
3.02k
            if (df_rest_flag_2) {
690
1.88k
              if ((err = huff_dec_1D(strm, data_type, hufYY2,
691
1.88k
                                     out_data_2_int + num_val_2_int, 1, 0)) !=
692
1.88k
                  HUFFDEC_OK) {
693
99
                goto bail;
694
99
              }
695
1.88k
            }
696
3.02k
          }
697
698
6.46k
          if (out_data_1 != NULL) {
699
25.0k
            for (i = 0; i < num_val_1_int - 1; i += 2) {
700
18.6k
              out_data_1_int[i] = pair_vec[i][0];
701
18.6k
              out_data_1_int[i + 1] = pair_vec[i][1];
702
18.6k
            }
703
6.46k
          }
704
6.46k
          if (out_data_2 != NULL) {
705
12.8k
            for (i = 0; i < num_val_2_int - 1; i += 2) {
706
9.96k
              out_data_2_int[i] = pair_vec[i + 1][0];
707
9.96k
              out_data_2_int[i + 1] = pair_vec[i + 1][1];
708
9.96k
            }
709
2.92k
          }
710
6.46k
          break; /* FREQ_PAIR */
711
712
3.70k
        case TIME_PAIR:
713
3.70k
          if (((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
714
3.38k
            p0_data_1[0] = &out_data_1[0];
715
3.38k
            p0_data_1[1] = &out_data_2[0];
716
717
3.38k
            out_data_1_int += 1;
718
3.38k
            out_data_2_int += 1;
719
720
3.38k
            num_val_1_int -= 1;
721
3.38k
          }
722
723
3.70k
          if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
724
2.97k
            diff_type = DIFF_TIME;
725
2.97k
          } else {
726
733
            diff_type = DIFF_FREQ;
727
733
          }
728
3.70k
          { hufYY = diff_type; }
729
730
3.70k
          if ((err = huff_dec_2D(strm, data_type, hufYY, TIME_PAIR, pair_vec,
731
3.70k
                                 num_val_1_int, 1, p0_data_1)) != HUFFDEC_OK) {
732
0
            goto bail;
733
0
          }
734
735
10.6k
          for (i = 0; i < num_val_1_int; i++) {
736
6.92k
            out_data_1_int[i] = pair_vec[i][0];
737
6.92k
            out_data_2_int[i] = pair_vec[i][1];
738
6.92k
          }
739
740
3.70k
          break; /* TIME_PAIR */
741
742
0
        default:
743
0
          break;
744
10.5k
      }
745
746
10.1k
      break; /* HUFF_2D */
747
748
10.1k
    default:
749
0
      break;
750
16.0k
  }
751
16.0k
bail:
752
16.0k
  return err;
753
16.0k
}
754
755
static void diff_freq_decode(const SCHAR* const diff_data,
756
20.5k
                             SCHAR* const out_data, const int num_val) {
757
20.5k
  int i = 0;
758
20.5k
  out_data[0] = diff_data[0];
759
760
118k
  for (i = 1; i < num_val; i++) {
761
97.8k
    out_data[i] = out_data[i - 1] + diff_data[i];
762
97.8k
  }
763
20.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
7.90k
                                       const int num_val) {
770
7.90k
  int i = 0; /* default start value*/
771
772
7.90k
  if (mixed_diff_type) {
773
1.63k
    out_data[0] = diff_data[0];
774
1.63k
    i = 1; /* new start value */
775
1.63k
  }
776
60.1k
  for (; i < num_val; i++) {
777
52.2k
    out_data[i] = prev_data[i] + diff_data[i];
778
52.2k
  }
779
7.90k
}
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
2.12k
                                      const int num_val) {
786
2.12k
  int i = 0; /* default start value*/
787
788
2.12k
  if (mixed_diff_type) {
789
1.02k
    out_data[0] = diff_data[0];
790
1.02k
    i = 1; /* new start value */
791
1.02k
  }
792
7.43k
  for (; i < num_val; i++) {
793
5.30k
    out_data[i] = prev_data[i] - diff_data[i];
794
5.30k
  }
795
2.12k
}
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
23.0k
                          SCHAR* out_data) {
800
23.0k
  int i = 0, lsb = 0;
801
23.0k
  ULONG data = 0;
802
803
150k
  for (i = 0; i < num_val; i++) {
804
127k
    int msb;
805
127k
    msb = in_data_msb[i];
806
807
127k
    if (num_lsb > 0) {
808
18.9k
      data = FDKreadBits(strm, num_lsb);
809
18.9k
      lsb = data;
810
811
18.9k
      out_data[i] = ((msb << num_lsb) | lsb) - offset;
812
18.9k
    } else
813
108k
      out_data[i] = msb - offset;
814
127k
  }
815
816
23.0k
  return HUFFDEC_OK; /* dummy */
817
23.0k
}
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
30.4k
{
826
30.4k
  ERROR_t err = HUFFDEC_OK;
827
828
  // int allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
829
30.4k
  int attachLsb_flag = 0;
830
30.4k
  int pcmCoding_flag = 0;
831
832
30.4k
  int mixed_time_pair = 0, numValPcm = 0;
833
30.4k
  int quant_levels = 0, quant_offset = 0;
834
30.4k
  ULONG data = 0;
835
836
30.4k
  SCHAR aaDataPair[2][28] = {{0}};
837
30.4k
  SCHAR aaDataDiff[2][28] = {{0}};
838
839
30.4k
  SCHAR aHistoryMsb[28] = {0};
840
841
30.4k
  SCHAR* pDataVec[2] = {NULL, NULL};
842
843
30.4k
  DIFF_TYPE diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
844
30.4k
  PAIRING pairing = FREQ_PAIR;
845
30.4k
  DIRECTION direction = BACKWARDS;
846
847
30.4k
  switch (data_type) {
848
13.8k
    case t_CLD:
849
13.8k
      if (coarse_flag) {
850
9.14k
        attachLsb_flag = 0;
851
9.14k
        quant_levels = 15;
852
9.14k
        quant_offset = 7;
853
9.14k
      } else {
854
4.65k
        attachLsb_flag = 0;
855
4.65k
        quant_levels = 31;
856
4.65k
        quant_offset = 15;
857
4.65k
      }
858
859
13.8k
      break;
860
861
12.6k
    case t_ICC:
862
12.6k
      if (coarse_flag) {
863
6.77k
        attachLsb_flag = 0;
864
6.77k
        quant_levels = 4;
865
6.77k
        quant_offset = 0;
866
6.77k
      } else {
867
5.87k
        attachLsb_flag = 0;
868
5.87k
        quant_levels = 8;
869
5.87k
        quant_offset = 0;
870
5.87k
      }
871
872
12.6k
      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
4.00k
    case t_IPD:
899
4.00k
      if (!coarse_flag) {
900
2.63k
        attachLsb_flag = 1;
901
2.63k
        quant_levels = 16;
902
2.63k
        quant_offset = 0;
903
2.63k
      } else {
904
1.36k
        attachLsb_flag = 0;
905
1.36k
        quant_levels = 8;
906
1.36k
        quant_offset = 0;
907
1.36k
      }
908
4.00k
      break;
909
910
0
    default:
911
0
      return HUFFDEC_NOTOK;
912
30.4k
  }
913
914
30.4k
  data = FDKreadBits(strm, 1);
915
30.4k
  pcmCoding_flag = data;
916
917
30.4k
  if (pcmCoding_flag) {
918
14.3k
    if (pair_flag) {
919
9.37k
      pDataVec[0] = aaDataPair[0];
920
9.37k
      pDataVec[1] = aaDataPair[1];
921
9.37k
      numValPcm = 2 * dataBands;
922
9.37k
    } else {
923
5.02k
      pDataVec[0] = aaDataPair[0];
924
5.02k
      pDataVec[1] = NULL;
925
5.02k
      numValPcm = dataBands;
926
5.02k
    }
927
928
14.3k
    err = pcm_decode(strm, pDataVec[0], pDataVec[1], quant_offset, numValPcm,
929
14.3k
                     quant_levels);
930
14.3k
    if (err != HUFFDEC_OK) return HUFFDEC_NOTOK;
931
932
16.0k
  } else { /* Differential/Huffman/LSB Coding */
933
934
16.0k
    if (pair_flag) {
935
7.99k
      pDataVec[0] = aaDataDiff[0];
936
7.99k
      pDataVec[1] = aaDataDiff[1];
937
8.06k
    } else {
938
8.06k
      pDataVec[0] = aaDataDiff[0];
939
8.06k
      pDataVec[1] = NULL;
940
8.06k
    }
941
942
16.0k
    diff_type[0] = DIFF_FREQ;
943
16.0k
    diff_type[1] = DIFF_FREQ;
944
945
16.0k
    direction = BACKWARDS;
946
16.0k
    {
947
16.0k
      if (pair_flag || allowDiffTimeBack_flag) {
948
13.9k
        data = FDKreadBits(strm, 1);
949
13.9k
        diff_type[0] = (DIFF_TYPE)data;
950
13.9k
      }
951
952
16.0k
      if (pair_flag &&
953
7.99k
          ((diff_type[0] == DIFF_FREQ) || allowDiffTimeBack_flag)) {
954
6.76k
        data = FDKreadBits(strm, 1);
955
6.76k
        diff_type[1] = (DIFF_TYPE)data;
956
6.76k
      }
957
16.0k
    }
958
    /* Huffman decoding */
959
16.0k
    err = huff_decode(strm, pDataVec[0], pDataVec[1], data_type, diff_type[0],
960
16.0k
                      diff_type[1], dataBands, &pairing,
961
16.0k
                      (DECODER == SAOC_DECODER));
962
16.0k
    if (err != HUFFDEC_OK) {
963
766
      return HUFFDEC_NOTOK;
964
766
    }
965
966
15.2k
    {
967
      /* Differential decoding */
968
15.2k
      if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
969
9.30k
        if (DECODER == SAOC_DECODER) {
970
1.62k
          direction = BACKWARDS;
971
7.68k
        } else {
972
7.68k
          if (pair_flag) {
973
4.46k
            if ((diff_type[0] == DIFF_TIME) && !allowDiffTimeBack_flag) {
974
1.22k
              direction = FORWARDS;
975
3.24k
            } else if (diff_type[1] == DIFF_TIME) {
976
2.18k
              direction = BACKWARDS;
977
2.18k
            } else {
978
1.05k
              data = FDKreadBits(strm, 1);
979
1.05k
              direction = (DIRECTION)data;
980
1.05k
            }
981
4.46k
          } else {
982
3.21k
            direction = BACKWARDS;
983
3.21k
          }
984
7.68k
        }
985
9.30k
      }
986
987
15.2k
      mixed_time_pair =
988
15.2k
          (diff_type[0] != diff_type[1]) && (pairing == TIME_PAIR);
989
990
15.2k
      if (direction == BACKWARDS) {
991
13.1k
        if (diff_type[0] == DIFF_FREQ) {
992
8.68k
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
993
8.68k
        } else {
994
4.47k
          int i;
995
40.5k
          for (i = 0; i < dataBands; i++) {
996
36.0k
            aHistoryMsb[i] = aHistory[i + startBand] + quant_offset;
997
36.0k
            if (attachLsb_flag) {
998
5.78k
              aHistoryMsb[i] >>= 1;
999
5.78k
            }
1000
36.0k
          }
1001
4.47k
          diff_time_decode_backwards(aHistoryMsb, aaDataDiff[0], aaDataPair[0],
1002
4.47k
                                     mixed_time_pair, dataBands);
1003
4.47k
        }
1004
13.1k
        if (diff_type[1] == DIFF_FREQ) {
1005
9.74k
          diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1006
9.74k
        } else {
1007
3.42k
          diff_time_decode_backwards(aaDataPair[0], aaDataDiff[1],
1008
3.42k
                                     aaDataPair[1], mixed_time_pair, dataBands);
1009
3.42k
        }
1010
13.1k
      } else {
1011
        /* diff_type[1] MUST BE DIFF_FREQ */
1012
2.12k
        diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1013
1014
2.12k
        if (diff_type[0] == DIFF_FREQ) {
1015
0
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
1016
2.12k
        } else {
1017
2.12k
          diff_time_decode_forwards(aaDataPair[1], aaDataDiff[0], aaDataPair[0],
1018
2.12k
                                    mixed_time_pair, dataBands);
1019
2.12k
        }
1020
2.12k
      }
1021
15.2k
    }
1022
1023
    /* LSB decoding */
1024
15.2k
    err = attach_lsb(strm, aaDataPair[0], quant_offset, attachLsb_flag ? 1 : 0,
1025
15.2k
                     dataBands, aaDataPair[0]);
1026
15.2k
    if (err != HUFFDEC_OK) goto bail;
1027
1028
15.2k
    if (pair_flag) {
1029
7.79k
      err = attach_lsb(strm, aaDataPair[1], quant_offset,
1030
7.79k
                       attachLsb_flag ? 1 : 0, dataBands, aaDataPair[1]);
1031
7.79k
      if (err != HUFFDEC_OK) goto bail;
1032
7.79k
    }
1033
15.2k
  } /* End: Differential/Huffman/LSB Coding */
1034
1035
  /* Copy data to output arrays */
1036
29.6k
  FDKmemcpy(aaOutData1 + startBand, aaDataPair[0], sizeof(SCHAR) * dataBands);
1037
29.6k
  if (pair_flag) {
1038
17.1k
    FDKmemcpy(aaOutData2 + startBand, aaDataPair[1], sizeof(SCHAR) * dataBands);
1039
17.1k
  }
1040
1041
29.6k
bail:
1042
29.6k
  return err;
1043
29.6k
}
1044
1045
ERROR_t huff_dec_reshape(HANDLE_FDK_BITSTREAM strm, int* out_data,
1046
1.29k
                         int num_val) {
1047
1.29k
  ERROR_t err = HUFFDEC_OK;
1048
1.29k
  int val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
1049
1.29k
  SCHAR rl_data[2] = {0};
1050
1051
21.2k
  while (val_rcvd < num_val) {
1052
20.0k
    err = huff_read_2D(strm,
1053
20.0k
                       (HANDLE_HUFF_NODE)&FDK_huffReshapeNodes.nodeTab[0][0],
1054
20.0k
                       rl_data, &dummy);
1055
20.0k
    if (err != HUFFDEC_OK) goto bail;
1056
20.0k
    val = rl_data[0];
1057
20.0k
    len = rl_data[1] + 1;
1058
20.0k
    if (val_rcvd + len > num_val) {
1059
92
      err = HUFFDEC_NOTOK;
1060
92
      goto bail;
1061
92
    }
1062
58.9k
    for (i = val_rcvd; i < val_rcvd + len; i++) {
1063
39.0k
      out_data[i] = val;
1064
39.0k
    }
1065
19.9k
    val_rcvd += len;
1066
19.9k
  }
1067
1.29k
bail:
1068
1.29k
  return err;
1069
1.29k
}