Coverage Report

Created: 2025-07-12 07:06

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