Coverage Report

Created: 2025-07-18 06:04

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