Coverage Report

Created: 2026-08-13 06:43

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