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