Coverage Report

Created: 2026-07-10 07:11

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
110k
#define min(a, b) (((a) < (b)) ? (a) : (b))
110
#endif
111
112
13.3k
ERROR_t sym_restoreIPD(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
113
13.3k
  int sum_val = data[0] + data[1];
114
13.3k
  int diff_val = data[0] - data[1];
115
116
13.3k
  if (sum_val > lav) {
117
5.64k
    data[0] = -sum_val + (2 * lav + 1);
118
5.64k
    data[1] = -diff_val;
119
7.66k
  } else {
120
7.66k
    data[0] = sum_val;
121
7.66k
    data[1] = diff_val;
122
7.66k
  }
123
124
13.3k
  if (data[0] - data[1] != 0) {
125
7.61k
    ULONG sym_bit;
126
7.61k
    sym_bit = FDKreadBits(strm, 1);
127
7.61k
    if (sym_bit) {
128
2.28k
      int tmp;
129
2.28k
      tmp = data[0];
130
2.28k
      data[0] = data[1];
131
2.28k
      data[1] = tmp;
132
2.28k
    }
133
7.61k
  }
134
135
13.3k
  return HUFFDEC_OK;
136
13.3k
}
137
138
31.6k
static int ilog2(unsigned int i) {
139
31.6k
  int l = 0;
140
141
31.6k
  if (i) i--;
142
138k
  while (i > 0) {
143
106k
    i >>= 1;
144
106k
    l++;
145
106k
  }
146
147
31.6k
  return l;
148
31.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
29.8k
                          int num_levels) {
153
29.8k
  int i = 0, j = 0, idx = 0;
154
29.8k
  int max_grp_len = 0, next_val = 0;
155
29.8k
  ULONG tmp;
156
157
29.8k
  int pcm_chunk_size[7] = {0};
158
159
29.8k
  switch (num_levels) {
160
355
    case 3:
161
355
      max_grp_len = 5;
162
355
      break;
163
72
    case 7:
164
72
      max_grp_len = 6;
165
72
      break;
166
68
    case 11:
167
68
      max_grp_len = 2;
168
68
      break;
169
0
    case 13:
170
0
      max_grp_len = 4;
171
0
      break;
172
3
    case 19:
173
3
      max_grp_len = 4;
174
3
      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
10.6k
    case 4:
182
15.6k
    case 8:
183
26.1k
    case 15:
184
28.1k
    case 16:
185
28.1k
    case 26:
186
29.3k
    case 31:
187
29.3k
      max_grp_len = 1;
188
29.3k
      break;
189
0
    default:
190
0
      return HUFFDEC_NOTOK;
191
29.8k
  }
192
193
29.8k
  tmp = 1;
194
61.4k
  for (i = 1; i <= max_grp_len; i++) {
195
31.6k
    tmp *= num_levels;
196
31.6k
    pcm_chunk_size[i] = ilog2(tmp);
197
31.6k
  }
198
199
140k
  for (i = 0; i < num_val; i += max_grp_len) {
200
110k
    int grp_len, grp_val, data;
201
110k
    grp_len = min(max_grp_len, num_val - i);
202
110k
    data = FDKreadBits(strm, pcm_chunk_size[grp_len]);
203
204
110k
    grp_val = data;
205
206
222k
    for (j = 0; j < grp_len; j++) {
207
111k
      idx = i + (grp_len - j - 1);
208
111k
      next_val = grp_val % num_levels;
209
210
111k
      if (out_data_2 == NULL) {
211
40.5k
        out_data_1[idx] = next_val - offset;
212
70.9k
      } else if (out_data_1 == NULL) {
213
0
        out_data_2[idx] = next_val - offset;
214
70.9k
      } else {
215
70.9k
        if (idx % 2) {
216
35.4k
          out_data_2[idx / 2] = next_val - offset;
217
35.4k
        } else {
218
35.4k
          out_data_1[idx / 2] = next_val - offset;
219
35.4k
        }
220
70.9k
      }
221
222
111k
      grp_val = (grp_val - next_val) / num_levels;
223
111k
    }
224
110k
  }
225
226
29.8k
  return HUFFDEC_OK;
227
29.8k
}
228
229
static ERROR_t huff_read(HANDLE_FDK_BITSTREAM strm,
230
                         const SHORT (*nodeTab)[MAX_ENTRIES][2],
231
279k
                         int* out_data) {
232
279k
  int node = 0;
233
279k
  int len = 0;
234
235
717k
  do {
236
717k
    ULONG next_bit;
237
717k
    next_bit = FDKreadBits(strm, 1);
238
717k
    len++;
239
717k
    node = (*nodeTab)[node][next_bit];
240
717k
  } while (node > 0);
241
242
279k
  *out_data = node;
243
244
279k
  return HUFFDEC_OK;
245
279k
}
246
247
static ERROR_t huff_read_2D(HANDLE_FDK_BITSTREAM strm,
248
                            const SHORT (*nodeTab)[MAX_ENTRIES][2],
249
114k
                            SCHAR out_data[2], int* escape) {
250
114k
  ERROR_t err = HUFFDEC_OK;
251
252
114k
  int huff_2D_8bit = 0;
253
114k
  int node = 0;
254
255
114k
  if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
256
0
    goto bail;
257
0
  }
258
114k
  *escape = (node == 0);
259
260
114k
  if (*escape) {
261
618
    out_data[0] = 0;
262
618
    out_data[1] = 1;
263
113k
  } else {
264
113k
    huff_2D_8bit = -(node + 1);
265
113k
    out_data[0] = huff_2D_8bit >> 4;
266
113k
    out_data[1] = huff_2D_8bit & 0xf;
267
113k
  }
268
269
114k
bail:
270
114k
  return err;
271
114k
}
272
273
51.2k
static ERROR_t sym_restore(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) {
274
51.2k
  ULONG sym_bit = 0;
275
276
51.2k
  int sum_val = data[0] + data[1];
277
51.2k
  int diff_val = data[0] - data[1];
278
279
51.2k
  if (sum_val > lav) {
280
20.0k
    data[0] = -sum_val + (2 * lav + 1);
281
20.0k
    data[1] = -diff_val;
282
31.1k
  } else {
283
31.1k
    data[0] = sum_val;
284
31.1k
    data[1] = diff_val;
285
31.1k
  }
286
287
51.2k
  if (data[0] + data[1] != 0) {
288
28.9k
    sym_bit = FDKreadBits(strm, 1);
289
28.9k
    if (sym_bit) {
290
10.9k
      data[0] = -data[0];
291
10.9k
      data[1] = -data[1];
292
10.9k
    }
293
28.9k
  }
294
295
51.2k
  if (data[0] - data[1] != 0) {
296
28.8k
    sym_bit = FDKreadBits(strm, 1);
297
28.8k
    if (sym_bit) {
298
12.6k
      int tmp;
299
12.6k
      tmp = data[0];
300
12.6k
      data[0] = data[1];
301
12.6k
      data[1] = tmp;
302
12.6k
    }
303
28.8k
  }
304
305
51.2k
  return HUFFDEC_OK;
306
51.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
27.4k
{
313
27.4k
  ERROR_t err = HUFFDEC_OK;
314
27.4k
  int i = 0, node = 0, offset = 0;
315
27.4k
  int od = 0, od_sign = 0;
316
27.4k
  ULONG data = 0;
317
27.4k
  int bitsAvail = 0;
318
319
27.4k
  const SHORT(*partTab)[MAX_ENTRIES][2] = NULL;
320
27.4k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
321
322
27.4k
  switch (data_type) {
323
8.75k
    case t_CLD:
324
8.75k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
325
8.75k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h1D[dim1]->nodeTab[0][0];
326
8.75k
      break;
327
12.9k
    case t_ICC:
328
12.9k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
329
12.9k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h1D[dim1]->nodeTab[0][0];
330
12.9k
      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
5.74k
    case t_IPD:
336
5.74k
      partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
337
5.74k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h1D[dim1].nodeTab[0][0];
338
5.74k
      break;
339
0
    default:
340
0
      FDK_ASSERT(0);
341
0
      err = HUFFDEC_NOTOK;
342
0
      goto bail;
343
27.4k
  }
344
345
27.4k
  if (p0_flag) {
346
8.60k
    if ((err = huff_read(strm, partTab, &node)) != HUFFDEC_OK) {
347
0
      goto bail;
348
0
    }
349
350
8.60k
    out_data[0] = -(node + 1);
351
8.60k
    offset = 1;
352
8.60k
  }
353
354
124k
  for (i = offset; i < num_val; i++) {
355
98.1k
    bitsAvail = FDKgetValidBits(strm);
356
98.1k
    if (bitsAvail < 1) {
357
1.25k
      err = HUFFDEC_NOTOK;
358
1.25k
      goto bail;
359
1.25k
    }
360
361
96.8k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
362
0
      goto bail;
363
0
    }
364
96.8k
    od = -(node + 1);
365
366
96.8k
    if (data_type != t_IPD) {
367
74.7k
      if (od != 0) {
368
24.2k
        bitsAvail = FDKgetValidBits(strm);
369
24.2k
        if (bitsAvail < 1) {
370
51
          err = HUFFDEC_NOTOK;
371
51
          goto bail;
372
51
        }
373
374
24.2k
        data = FDKreadBits(strm, 1);
375
24.2k
        od_sign = data;
376
377
24.2k
        if (od_sign) od = -od;
378
24.2k
      }
379
74.7k
    }
380
381
96.7k
    out_data[i] = od;
382
96.7k
  }
383
384
27.4k
bail:
385
27.4k
  return err;
386
27.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.5k
                           SCHAR* p0_data[2]) {
392
29.5k
  ERROR_t err = HUFFDEC_OK;
393
29.5k
  int i = 0, lav = 0, escape = 0, escCntr = 0;
394
29.5k
  int node = 0;
395
29.5k
  unsigned long data = 0;
396
397
29.5k
  SCHAR esc_data[2][28] = {{0}};
398
29.5k
  int escIdx[28] = {0};
399
29.5k
  const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL;
400
401
  /* LAV */
402
29.5k
  if ((err =
403
29.5k
           huff_read(strm, (HANDLE_HUFF_NODE)&FDK_huffLavIdxNodes.nodeTab[0][0],
404
29.5k
                     &node)) != HUFFDEC_OK) {
405
0
    goto bail;
406
0
  }
407
29.5k
  data = -(node + 1);
408
409
29.5k
  switch (data_type) {
410
14.7k
    case t_CLD:
411
14.7k
      lav = 2 * data + 3; /* 3, 5, 7, 9 */
412
14.7k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0];
413
14.7k
      break;
414
8.78k
    case t_ICC:
415
8.78k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
416
8.78k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0];
417
8.78k
      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.99k
    case t_IPD:
423
5.99k
      if (data == 0)
424
2.07k
        data = 3;
425
3.91k
      else
426
3.91k
        data--;
427
5.99k
      lav = 2 * data + 1; /* 1, 3, 5, 7 */
428
5.99k
      nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0];
429
5.99k
      break;
430
0
    default:
431
0
      FDK_ASSERT(0);
432
0
      err = HUFFDEC_NOTOK;
433
0
      goto bail;
434
29.5k
  }
435
436
  /* Partition 0 */
437
29.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
29.5k
  if (p0_data[1] != NULL) {
444
12.4k
    if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) {
445
0
      goto bail;
446
0
    }
447
12.4k
    *p0_data[1] = -(node + 1);
448
12.4k
  }
449
450
29.5k
  switch (data_type) {
451
14.7k
    case t_CLD:
452
14.7k
      switch (lav) {
453
3.37k
        case 3:
454
3.37k
          nodeTab =
455
3.37k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav3[0][0];
456
3.37k
          break;
457
909
        case 5:
458
909
          nodeTab =
459
909
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav5[0][0];
460
909
          break;
461
5.16k
        case 7:
462
5.16k
          nodeTab =
463
5.16k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav7[0][0];
464
5.16k
          break;
465
5.32k
        case 9:
466
5.32k
          nodeTab =
467
5.32k
              (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav9[0][0];
468
5.32k
          break;
469
14.7k
      }
470
14.7k
      break;
471
14.7k
    case t_ICC:
472
8.78k
      switch (lav) {
473
3.30k
        case 1:
474
3.30k
          nodeTab =
475
3.30k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav1[0][0];
476
3.30k
          break;
477
1.75k
        case 3:
478
1.75k
          nodeTab =
479
1.75k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav3[0][0];
480
1.75k
          break;
481
607
        case 5:
482
607
          nodeTab =
483
607
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav5[0][0];
484
607
          break;
485
3.12k
        case 7:
486
3.12k
          nodeTab =
487
3.12k
              (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav7[0][0];
488
3.12k
          break;
489
8.78k
      }
490
8.78k
      break;
491
8.78k
    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.99k
    case t_IPD:
509
5.99k
      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
175
        case 3:
515
175
          nodeTab =
516
175
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav3[0][0];
517
175
          break;
518
426
        case 5:
519
426
          nodeTab =
520
426
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav5[0][0];
521
426
          break;
522
2.07k
        case 7:
523
2.07k
          nodeTab =
524
2.07k
              (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav7[0][0];
525
2.07k
          break;
526
5.99k
      }
527
5.99k
      break;
528
5.99k
    default:
529
0
      break;
530
29.5k
  }
531
532
94.7k
  for (i = 0; i < num_val; i += stride) {
533
65.1k
    if ((err = huff_read_2D(strm, nodeTab, out_data[i], &escape)) !=
534
65.1k
        HUFFDEC_OK) {
535
0
      goto bail;
536
0
    }
537
538
65.1k
    if (escape) {
539
618
      escIdx[escCntr++] = i;
540
64.5k
    } else {
541
64.5k
      if (data_type == t_IPD) {
542
13.3k
        if ((err = sym_restoreIPD(strm, lav, out_data[i])) != HUFFDEC_OK) {
543
0
          goto bail;
544
0
        }
545
51.2k
      } else {
546
51.2k
        if ((err = sym_restore(strm, lav, out_data[i])) != HUFFDEC_OK) {
547
0
          goto bail;
548
0
        }
549
51.2k
      }
550
64.5k
    }
551
65.1k
  } /* i */
552
553
29.5k
  if (escCntr > 0) {
554
498
    if ((err = pcm_decode(strm, esc_data[0], esc_data[1], 0, 2 * escCntr,
555
498
                          (2 * lav + 1))) != HUFFDEC_OK) {
556
0
      goto bail;
557
0
    }
558
559
1.11k
    for (i = 0; i < escCntr; i++) {
560
618
      out_data[escIdx[i]][0] = esc_data[0][i] - lav;
561
618
      out_data[escIdx[i]][1] = esc_data[1][i] - lav;
562
618
    }
563
498
  }
564
29.5k
bail:
565
29.5k
  return err;
566
29.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
36.5k
                           int num_val, PAIRING* pairing_scheme, int ldMode) {
572
36.5k
  ERROR_t err = HUFFDEC_OK;
573
36.5k
  CODING_SCHEME coding_scheme = HUFF_1D;
574
36.5k
  DIFF_TYPE diff_type;
575
576
36.5k
  int i = 0;
577
578
36.5k
  SCHAR pair_vec[28][2];
579
580
36.5k
  SCHAR* p0_data_1[2] = {NULL, NULL};
581
36.5k
  SCHAR* p0_data_2[2] = {NULL, NULL};
582
583
36.5k
  int p0_flag[2];
584
585
36.5k
  int num_val_1_int = num_val;
586
36.5k
  int num_val_2_int = num_val;
587
588
36.5k
  SCHAR* out_data_1_int = out_data_1;
589
36.5k
  SCHAR* out_data_2_int = out_data_2;
590
591
36.5k
  int df_rest_flag_1 = 0;
592
36.5k
  int df_rest_flag_2 = 0;
593
594
36.5k
  int hufYY1;
595
36.5k
  int hufYY2;
596
36.5k
  int hufYY;
597
598
  /* Coding scheme */
599
36.5k
  coding_scheme = (CODING_SCHEME)FDKreadBits(strm, 1);
600
601
36.5k
  if (coding_scheme == HUFF_2D) {
602
22.9k
    if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ldMode == 0)) {
603
13.0k
      *pairing_scheme = (PAIRING)FDKreadBits(strm, 1);
604
13.0k
    } else {
605
9.82k
      *pairing_scheme = FREQ_PAIR;
606
9.82k
    }
607
22.9k
  }
608
609
36.5k
  {
610
36.5k
    hufYY1 = diff_type_1;
611
36.5k
    hufYY2 = diff_type_2;
612
36.5k
  }
613
614
36.5k
  switch (coding_scheme) {
615
13.6k
    case HUFF_1D:
616
13.6k
      p0_flag[0] = (diff_type_1 == DIFF_FREQ);
617
13.6k
      p0_flag[1] = (diff_type_2 == DIFF_FREQ);
618
13.6k
      if (out_data_1 != NULL) {
619
13.6k
        if ((err = huff_dec_1D(strm, data_type, hufYY1, out_data_1,
620
13.6k
                               num_val_1_int, p0_flag[0])) != HUFFDEC_OK) {
621
746
          goto bail;
622
746
        }
623
13.6k
      }
624
12.8k
      if (out_data_2 != NULL) {
625
3.47k
        if ((err = huff_dec_1D(strm, data_type, hufYY2, out_data_2,
626
3.47k
                               num_val_2_int, p0_flag[1])) != HUFFDEC_OK) {
627
13
          goto bail;
628
13
        }
629
3.47k
      }
630
631
12.8k
      break; /* HUFF_1D */
632
633
22.9k
    case HUFF_2D:
634
635
22.9k
      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
8.59k
              p0_data_1[0] = &out_data_1[0];
641
8.59k
              p0_data_1[1] = NULL;
642
643
8.59k
              num_val_1_int -= 1;
644
8.59k
              out_data_1_int += 1;
645
8.59k
            }
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
5
              err = HUFFDEC_NOTOK;
650
5
              goto bail;
651
5
            }
652
13.5k
          }
653
13.5k
          if (out_data_2 != NULL) {
654
6.69k
            if (diff_type_2 == DIFF_FREQ) {
655
3.85k
              p0_data_2[0] = NULL;
656
3.85k
              p0_data_2[1] = &out_data_2[0];
657
658
3.85k
              num_val_2_int -= 1;
659
3.85k
              out_data_2_int += 1;
660
3.85k
            }
661
6.69k
            df_rest_flag_2 = num_val_2_int % 2;
662
6.69k
            if (df_rest_flag_2) num_val_2_int -= 1;
663
6.69k
            if (num_val_2_int < 0) {
664
1
              err = HUFFDEC_NOTOK;
665
1
              goto bail;
666
1
            }
667
6.69k
          }
668
669
13.5k
          if (out_data_1 != NULL) {
670
13.5k
            if ((err = huff_dec_2D(strm, data_type, hufYY1, FREQ_PAIR, pair_vec,
671
13.5k
                                   num_val_1_int, 2, p0_data_1)) !=
672
13.5k
                HUFFDEC_OK) {
673
0
              goto bail;
674
0
            }
675
13.5k
            if (df_rest_flag_1) {
676
6.43k
              if ((err = huff_dec_1D(strm, data_type, hufYY1,
677
6.43k
                                     out_data_1_int + num_val_1_int, 1, 0)) !=
678
6.43k
                  HUFFDEC_OK) {
679
266
                goto bail;
680
266
              }
681
6.43k
            }
682
13.5k
          }
683
13.2k
          if (out_data_2 != NULL) {
684
6.64k
            if ((err = huff_dec_2D(strm, data_type, hufYY2, FREQ_PAIR,
685
6.64k
                                   pair_vec + 1, num_val_2_int, 2,
686
6.64k
                                   p0_data_2)) != HUFFDEC_OK) {
687
0
              goto bail;
688
0
            }
689
6.64k
            if (df_rest_flag_2) {
690
3.86k
              if ((err = huff_dec_1D(strm, data_type, hufYY2,
691
3.86k
                                     out_data_2_int + num_val_2_int, 1, 0)) !=
692
3.86k
                  HUFFDEC_OK) {
693
280
                goto bail;
694
280
              }
695
3.86k
            }
696
6.64k
          }
697
698
12.9k
          if (out_data_1 != NULL) {
699
45.7k
            for (i = 0; i < num_val_1_int - 1; i += 2) {
700
32.7k
              out_data_1_int[i] = pair_vec[i][0];
701
32.7k
              out_data_1_int[i + 1] = pair_vec[i][1];
702
32.7k
            }
703
12.9k
          }
704
12.9k
          if (out_data_2 != NULL) {
705
24.5k
            for (i = 0; i < num_val_2_int - 1; i += 2) {
706
18.2k
              out_data_2_int[i] = pair_vec[i + 1][0];
707
18.2k
              out_data_2_int[i + 1] = pair_vec[i + 1][1];
708
18.2k
            }
709
6.36k
          }
710
12.9k
          break; /* FREQ_PAIR */
711
712
9.36k
        case TIME_PAIR:
713
9.36k
          if (((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
714
8.57k
            p0_data_1[0] = &out_data_1[0];
715
8.57k
            p0_data_1[1] = &out_data_2[0];
716
717
8.57k
            out_data_1_int += 1;
718
8.57k
            out_data_2_int += 1;
719
720
8.57k
            num_val_1_int -= 1;
721
8.57k
          }
722
723
9.36k
          if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
724
7.39k
            diff_type = DIFF_TIME;
725
7.39k
          } else {
726
1.96k
            diff_type = DIFF_FREQ;
727
1.96k
          }
728
9.36k
          { hufYY = diff_type; }
729
730
9.36k
          if ((err = huff_dec_2D(strm, data_type, hufYY, TIME_PAIR, pair_vec,
731
9.36k
                                 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.3k
            out_data_1_int[i] = pair_vec[i][0];
737
12.3k
            out_data_2_int[i] = pair_vec[i][1];
738
12.3k
          }
739
740
9.36k
          break; /* TIME_PAIR */
741
742
0
        default:
743
0
          break;
744
22.9k
      }
745
746
22.3k
      break; /* HUFF_2D */
747
748
22.3k
    default:
749
0
      break;
750
36.5k
  }
751
36.5k
bail:
752
36.5k
  return err;
753
36.5k
}
754
755
static void diff_freq_decode(const SCHAR* const diff_data,
756
46.6k
                             SCHAR* const out_data, const int num_val) {
757
46.6k
  int i = 0;
758
46.6k
  out_data[0] = diff_data[0];
759
760
242k
  for (i = 1; i < num_val; i++) {
761
196k
    out_data[i] = out_data[i - 1] + diff_data[i];
762
196k
  }
763
46.6k
}
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
17.4k
                                       const int num_val) {
770
17.4k
  int i = 0; /* default start value*/
771
772
17.4k
  if (mixed_diff_type) {
773
3.79k
    out_data[0] = diff_data[0];
774
3.79k
    i = 1; /* new start value */
775
3.79k
  }
776
122k
  for (; i < num_val; i++) {
777
104k
    out_data[i] = prev_data[i] + diff_data[i];
778
104k
  }
779
17.4k
}
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
6.38k
                                      const int num_val) {
786
6.38k
  int i = 0; /* default start value*/
787
788
6.38k
  if (mixed_diff_type) {
789
2.81k
    out_data[0] = diff_data[0];
790
2.81k
    i = 1; /* new start value */
791
2.81k
  }
792
20.6k
  for (; i < num_val; i++) {
793
14.2k
    out_data[i] = prev_data[i] - diff_data[i];
794
14.2k
  }
795
6.38k
}
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
54.4k
                          SCHAR* out_data) {
800
54.4k
  int i = 0, lsb = 0;
801
54.4k
  ULONG data = 0;
802
803
314k
  for (i = 0; i < num_val; i++) {
804
259k
    int msb;
805
259k
    msb = in_data_msb[i];
806
807
259k
    if (num_lsb > 0) {
808
40.8k
      data = FDKreadBits(strm, num_lsb);
809
40.8k
      lsb = data;
810
811
40.8k
      out_data[i] = ((msb << num_lsb) | lsb) - offset;
812
40.8k
    } else
813
218k
      out_data[i] = msb - offset;
814
259k
  }
815
816
54.4k
  return HUFFDEC_OK; /* dummy */
817
54.4k
}
818
819
ERROR_t EcDataPairDec(DECODER_TYPE DECODER, HANDLE_FDK_BITSTREAM strm,
820
                      SCHAR* aaOutData1, SCHAR* aaOutData2, SCHAR* aHistory,
821
                      DATA_TYPE data_type, int startBand, int dataBands,
822
                      int pair_flag, int coarse_flag,
823
                      int allowDiffTimeBack_flag)
824
825
65.8k
{
826
65.8k
  ERROR_t err = HUFFDEC_OK;
827
828
  // int allowDiffTimeBack_flag = !independency_flag || (setIdx > 0);
829
65.8k
  int attachLsb_flag = 0;
830
65.8k
  int pcmCoding_flag = 0;
831
832
65.8k
  int mixed_time_pair = 0, numValPcm = 0;
833
65.8k
  int quant_levels = 0, quant_offset = 0;
834
65.8k
  ULONG data = 0;
835
836
65.8k
  SCHAR aaDataPair[2][28] = {{0}};
837
65.8k
  SCHAR aaDataDiff[2][28] = {{0}};
838
839
65.8k
  SCHAR aHistoryMsb[28] = {0};
840
841
65.8k
  SCHAR* pDataVec[2] = {NULL, NULL};
842
843
65.8k
  DIFF_TYPE diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
844
65.8k
  PAIRING pairing = FREQ_PAIR;
845
65.8k
  DIRECTION direction = BACKWARDS;
846
847
65.8k
  switch (data_type) {
848
28.1k
    case t_CLD:
849
28.1k
      if (coarse_flag) {
850
17.0k
        attachLsb_flag = 0;
851
17.0k
        quant_levels = 15;
852
17.0k
        quant_offset = 7;
853
17.0k
      } else {
854
11.0k
        attachLsb_flag = 0;
855
11.0k
        quant_levels = 31;
856
11.0k
        quant_offset = 15;
857
11.0k
      }
858
859
28.1k
      break;
860
861
27.4k
    case t_ICC:
862
27.4k
      if (coarse_flag) {
863
16.2k
        attachLsb_flag = 0;
864
16.2k
        quant_levels = 4;
865
16.2k
        quant_offset = 0;
866
16.2k
      } else {
867
11.1k
        attachLsb_flag = 0;
868
11.1k
        quant_levels = 8;
869
11.1k
        quant_offset = 0;
870
11.1k
      }
871
872
27.4k
      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.2k
    case t_IPD:
899
10.2k
      if (!coarse_flag) {
900
6.61k
        attachLsb_flag = 1;
901
6.61k
        quant_levels = 16;
902
6.61k
        quant_offset = 0;
903
6.61k
      } else {
904
3.68k
        attachLsb_flag = 0;
905
3.68k
        quant_levels = 8;
906
3.68k
        quant_offset = 0;
907
3.68k
      }
908
10.2k
      break;
909
910
0
    default:
911
0
      return HUFFDEC_NOTOK;
912
65.8k
  }
913
914
65.8k
  data = FDKreadBits(strm, 1);
915
65.8k
  pcmCoding_flag = data;
916
917
65.8k
  if (pcmCoding_flag) {
918
29.3k
    if (pair_flag) {
919
22.1k
      pDataVec[0] = aaDataPair[0];
920
22.1k
      pDataVec[1] = aaDataPair[1];
921
22.1k
      numValPcm = 2 * dataBands;
922
22.1k
    } else {
923
7.19k
      pDataVec[0] = aaDataPair[0];
924
7.19k
      pDataVec[1] = NULL;
925
7.19k
      numValPcm = dataBands;
926
7.19k
    }
927
928
29.3k
    err = pcm_decode(strm, pDataVec[0], pDataVec[1], quant_offset, numValPcm,
929
29.3k
                     quant_levels);
930
29.3k
    if (err != HUFFDEC_OK) return HUFFDEC_NOTOK;
931
932
36.5k
  } else { /* Differential/Huffman/LSB Coding */
933
934
36.5k
    if (pair_flag) {
935
19.7k
      pDataVec[0] = aaDataDiff[0];
936
19.7k
      pDataVec[1] = aaDataDiff[1];
937
19.7k
    } else {
938
16.7k
      pDataVec[0] = aaDataDiff[0];
939
16.7k
      pDataVec[1] = NULL;
940
16.7k
    }
941
942
36.5k
    diff_type[0] = DIFF_FREQ;
943
36.5k
    diff_type[1] = DIFF_FREQ;
944
945
36.5k
    direction = BACKWARDS;
946
36.5k
    {
947
36.5k
      if (pair_flag || allowDiffTimeBack_flag) {
948
32.3k
        data = FDKreadBits(strm, 1);
949
32.3k
        diff_type[0] = (DIFF_TYPE)data;
950
32.3k
      }
951
952
36.5k
      if (pair_flag &&
953
19.7k
          ((diff_type[0] == DIFF_FREQ) || allowDiffTimeBack_flag)) {
954
16.1k
        data = FDKreadBits(strm, 1);
955
16.1k
        diff_type[1] = (DIFF_TYPE)data;
956
16.1k
      }
957
36.5k
    }
958
    /* Huffman decoding */
959
36.5k
    err = huff_decode(strm, pDataVec[0], pDataVec[1], data_type, diff_type[0],
960
36.5k
                      diff_type[1], dataBands, &pairing,
961
36.5k
                      (DECODER == SAOC_DECODER));
962
36.5k
    if (err != HUFFDEC_OK) {
963
1.31k
      return HUFFDEC_NOTOK;
964
1.31k
    }
965
966
35.2k
    {
967
      /* Differential decoding */
968
35.2k
      if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
969
22.3k
        if (DECODER == SAOC_DECODER) {
970
2.83k
          direction = BACKWARDS;
971
19.4k
        } else {
972
19.4k
          if (pair_flag) {
973
11.9k
            if ((diff_type[0] == DIFF_TIME) && !allowDiffTimeBack_flag) {
974
3.66k
              direction = FORWARDS;
975
8.32k
            } else if (diff_type[1] == DIFF_TIME) {
976
5.26k
              direction = BACKWARDS;
977
5.26k
            } else {
978
3.06k
              data = FDKreadBits(strm, 1);
979
3.06k
              direction = (DIRECTION)data;
980
3.06k
            }
981
11.9k
          } else {
982
7.49k
            direction = BACKWARDS;
983
7.49k
          }
984
19.4k
        }
985
22.3k
      }
986
987
35.2k
      mixed_time_pair =
988
35.2k
          (diff_type[0] != diff_type[1]) && (pairing == TIME_PAIR);
989
990
35.2k
      if (direction == BACKWARDS) {
991
28.8k
        if (diff_type[0] == DIFF_FREQ) {
992
18.8k
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
993
18.8k
        } else {
994
10.0k
          int i;
995
87.8k
          for (i = 0; i < dataBands; i++) {
996
77.8k
            aHistoryMsb[i] = aHistory[i + startBand] + quant_offset;
997
77.8k
            if (attachLsb_flag) {
998
16.0k
              aHistoryMsb[i] >>= 1;
999
16.0k
            }
1000
77.8k
          }
1001
10.0k
          diff_time_decode_backwards(aHistoryMsb, aaDataDiff[0], aaDataPair[0],
1002
10.0k
                                     mixed_time_pair, dataBands);
1003
10.0k
        }
1004
28.8k
        if (diff_type[1] == DIFF_FREQ) {
1005
21.4k
          diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1006
21.4k
        } else {
1007
7.42k
          diff_time_decode_backwards(aaDataPair[0], aaDataDiff[1],
1008
7.42k
                                     aaDataPair[1], mixed_time_pair, dataBands);
1009
7.42k
        }
1010
28.8k
      } else {
1011
        /* diff_type[1] MUST BE DIFF_FREQ */
1012
6.38k
        diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands);
1013
1014
6.38k
        if (diff_type[0] == DIFF_FREQ) {
1015
0
          diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands);
1016
6.38k
        } else {
1017
6.38k
          diff_time_decode_forwards(aaDataPair[1], aaDataDiff[0], aaDataPair[0],
1018
6.38k
                                    mixed_time_pair, dataBands);
1019
6.38k
        }
1020
6.38k
      }
1021
35.2k
    }
1022
1023
    /* LSB decoding */
1024
35.2k
    err = attach_lsb(strm, aaDataPair[0], quant_offset, attachLsb_flag ? 1 : 0,
1025
35.2k
                     dataBands, aaDataPair[0]);
1026
35.2k
    if (err != HUFFDEC_OK) goto bail;
1027
1028
35.2k
    if (pair_flag) {
1029
19.1k
      err = attach_lsb(strm, aaDataPair[1], quant_offset,
1030
19.1k
                       attachLsb_flag ? 1 : 0, dataBands, aaDataPair[1]);
1031
19.1k
      if (err != HUFFDEC_OK) goto bail;
1032
19.1k
    }
1033
35.2k
  } /* End: Differential/Huffman/LSB Coding */
1034
1035
  /* Copy data to output arrays */
1036
64.5k
  FDKmemcpy(aaOutData1 + startBand, aaDataPair[0], sizeof(SCHAR) * dataBands);
1037
64.5k
  if (pair_flag) {
1038
41.3k
    FDKmemcpy(aaOutData2 + startBand, aaDataPair[1], sizeof(SCHAR) * dataBands);
1039
41.3k
  }
1040
1041
64.5k
bail:
1042
64.5k
  return err;
1043
64.5k
}
1044
1045
ERROR_t huff_dec_reshape(HANDLE_FDK_BITSTREAM strm, int* out_data,
1046
3.13k
                         int num_val) {
1047
3.13k
  ERROR_t err = HUFFDEC_OK;
1048
3.13k
  int val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
1049
3.13k
  SCHAR rl_data[2] = {0};
1050
1051
52.2k
  while (val_rcvd < num_val) {
1052
49.3k
    err = huff_read_2D(strm,
1053
49.3k
                       (HANDLE_HUFF_NODE)&FDK_huffReshapeNodes.nodeTab[0][0],
1054
49.3k
                       rl_data, &dummy);
1055
49.3k
    if (err != HUFFDEC_OK) goto bail;
1056
49.3k
    val = rl_data[0];
1057
49.3k
    len = rl_data[1] + 1;
1058
49.3k
    if (val_rcvd + len > num_val) {
1059
172
      err = HUFFDEC_NOTOK;
1060
172
      goto bail;
1061
172
    }
1062
143k
    for (i = val_rcvd; i < val_rcvd + len; i++) {
1063
94.8k
      out_data[i] = val;
1064
94.8k
    }
1065
49.1k
    val_rcvd += len;
1066
49.1k
  }
1067
3.13k
bail:
1068
3.13k
  return err;
1069
3.13k
}