Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *                                                                            *
      3  * Copyright (C) 2018 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 #include <stdlib.h>
     21 #include <stdio.h>
     22 
     23 #include <ixheaacd_type_def.h>
     24 
     25 #include "ixheaacd_bitbuffer.h"
     26 
     27 #include "ixheaacd_defines.h"
     28 #include "ixheaacd_memory_standards.h"
     29 #include "ixheaacd_sbrdecsettings.h"
     30 #include "ixheaacd_env_extr_part.h"
     31 #include <ixheaacd_aac_rom.h>
     32 #include "ixheaacd_common_rom.h"
     33 #include <ixheaacd_sbr_rom.h>
     34 #include "ixheaacd_pulsedata.h"
     35 #include "ixheaacd_pns.h"
     36 
     37 #include "ixheaacd_sbr_common.h"
     38 #include "ixheaacd_drc_data_struct.h"
     39 #include "ixheaacd_drc_dec.h"
     40 
     41 #include "ixheaacd_lt_predict.h"
     42 #include "ixheaacd_channelinfo.h"
     43 #include "ixheaacd_channel.h"
     44 #include "ixheaacd_channelinfo.h"
     45 #include "ixheaacd_sbrdecoder.h"
     46 #include "ixheaacd_audioobjtypes.h"
     47 #include "ixheaacd_latmdemux.h"
     48 #include "ixheaacd_aacdec.h"
     49 #include "ixheaacd_sbr_common.h"
     50 
     51 #include "ixheaacd_mps_polyphase.h"
     52 #include "ixheaacd_config.h"
     53 #include "ixheaacd_mps_dec.h"
     54 #include "ixheaacd_mps_interface.h"
     55 #include "ixheaacd_struct_def.h"
     56 
     57 #include "ixheaacd_config.h"
     58 #include "ixheaacd_mps_interface.h"
     59 
     60 #include "ixheaacd_mps_polyphase.h"
     61 
     62 #include "ixheaacd_mps_dec.h"
     63 #include "ixheaacd_mps_process.h"
     64 #include "ixheaacd_mps_decor.h"
     65 #include "ixheaacd_mps_hybfilter.h"
     66 #include "ixheaacd_mps_nlc_dec.h"
     67 #include "ixheaacd_mps_huff_tab.h"
     68 
     69 #include "math.h"
     70 
     71 #include <assert.h>
     72 #include <string.h>
     73 
     74 extern ia_huff_pt0_nodes_struct ixheaacd_huff_part0_nodes;
     75 extern ia_huff_ipd_nodes_struct ixheaacd_huff_ipd_nodes;
     76 extern ia_huff_lav_nodes_struct ixheaacd_huff_lav_idx_nodes;
     77 extern ia_huff_pt0_nodes_struct ixheaacd_huff_pilot_nodes;
     78 extern ia_huff_cld_nodes_struct ixheaacd_huff_cld_nodes;
     79 extern ia_huff_icc_nodes_struct ixheaacd_huff_icc_nodes;
     80 extern ia_huff_res_nodes_struct ixheaacd_huff_reshape_nodes;
     81 
     82 VOID ixheaacd_mps_create(ia_mps_dec_state_struct* self, WORD32 bs_frame_len,
     83                          WORD32 residual_coding,
     84                          ia_usac_dec_mps_config_struct* mps212_config) {
     85   WORD32 num_ch;
     86   WORD32 err_code = 0;
     87 
     88   ia_mps_bs_frame bs_frame;
     89 
     90   self->num_parameter_sets = 1;
     91   self->qmf_band_count = 64;
     92 
     93   self->res_ch_count = 0;
     94 
     95   if (mps212_config) {
     96     self->config = mps212_config;
     97     self->frame_length = bs_frame_len;
     98     self->in_ch_count = 1;
     99     self->out_ch_count = 2;
    100     self->residual_coding = residual_coding;
    101     if (self->residual_coding) {
    102       self->bs_residual_present = 1;
    103       self->bs_residual_bands = mps212_config->bs_residual_bands;
    104       if (self->config->bs_phase_coding) {
    105         self->config->bs_phase_coding = 2;
    106       }
    107     }
    108   }
    109 
    110   err_code = ixheaacd_mps_header_decode(self);
    111 
    112   if ((self->residual_coding) && (self->res_bands > 0)) self->res_ch_count++;
    113 
    114   ixheaacd_mps_env_init(self);
    115 
    116   ixheaacd_mps_synt_create(&self->poly_phase_filt_kernel, self->qmf_band_count);
    117 
    118   for (num_ch = 0; num_ch < self->out_ch_count; num_ch++) {
    119     ixheaacd_mps_synt_init(&self->qmf_filt_state[num_ch]);
    120   }
    121 
    122   ixheaacd_mps_qmf_hybrid_analysis_init(&self->hyb_filt_state[0]);
    123 
    124   if ((self->residual_coding) && (self->res_bands > 0))
    125     ixheaacd_mps_qmf_hybrid_analysis_init(&self->hyb_filt_state[1]);
    126 
    127   ixheaacd_mps_decor_init(&(self->mps_decor), self->hyb_band_count,
    128                           self->config->bs_decorr_config);
    129 
    130   ixheaacd_mps_init_pre_and_post_matrix(self);
    131 
    132   self->parse_nxt_frame = 1;
    133 
    134   bs_frame = self->bs_frame;
    135   memset(bs_frame.cld_idx_pre, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
    136   memset(bs_frame.icc_idx_pre, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
    137   memset(bs_frame.cmp_cld_idx_prev, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
    138   memset(bs_frame.cmp_icc_idx_prev, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
    139 
    140   self->subband_var.init_flag = 0;
    141   self->subband_var.update_old_ener = 0;
    142   self->subband_var.nrg_dir = 0;
    143   memset(self->subband_var.nrg_diff, 0, 2 * sizeof(FLOAT32));
    144 
    145   memset(self->opd_smooth.smooth_l_phase, 0,
    146          MAX_PARAMETER_BANDS * sizeof(WORD32));
    147   memset(self->opd_smooth.smooth_r_phase, 0,
    148          MAX_PARAMETER_BANDS * sizeof(WORD32));
    149 
    150   return;
    151 }
    152 
    153 static FLOAT32 ixheaacd_tsd_mul_re[] = {
    154     1.0f,  0.707106781186548f,  0.0f, -0.707106781186548f,
    155     -1.0f, -0.707106781186548f, 0.0f, 0.707106781186548f};
    156 
    157 static FLOAT32 ixheaacd_tsd_mul_im[] = {
    158     0.0f, 0.707106781186548f,  1.0f,  0.707106781186548f,
    159     0.0f, -0.707106781186548f, -1.0f, -0.707106781186548f};
    160 
    161 VOID ixheaacd_mps_qmf_hyb_analysis(ia_mps_dec_state_struct* self) {
    162   ixheaacd_mps_qmf_hybrid_analysis(&self->hyb_filt_state[0], self->qmf_in[0],
    163                                    self->qmf_band_count, self->time_slots,
    164                                    self->hyb_in[0]);
    165 
    166   if ((self->residual_coding) && (self->res_bands > 0)) {
    167     ixheaacd_mps_qmf_hybrid_analysis(&self->hyb_filt_state[self->in_ch_count],
    168                                      self->qmf_in[1], self->qmf_band_count,
    169                                      self->time_slots, self->hyb_res);
    170   }
    171 }
    172 
    173 VOID ixheaacd_mps_qmf_hyb_synthesis(ia_mps_dec_state_struct* self) {
    174   WORD32 ch;
    175 
    176   for (ch = 0; ch < self->out_ch_count; ch++) {
    177     ixheaacd_mps_qmf_hybrid_synthesis(self->hyb_dir_out[ch],
    178                                       self->qmf_band_count, self->time_slots,
    179                                       self->qmf_out_dir[ch]);
    180   }
    181 }
    182 
    183 VOID ixheaacd_mps_decor(ia_mps_dec_state_struct* self) {
    184   WORD32 k, sb_sample, idx;
    185 
    186   ia_cmplx_flt_struct(*scratch)[MAX_HYBRID_BANDS_MPS];
    187 
    188   ia_cmplx_flt_struct coeff;
    189   WORD32 band_start = 7;
    190 
    191   scratch = self->scratch;
    192 
    193   for (k = self->dir_sig_count; k < self->dir_sig_count + self->decor_sig_count;
    194        k++) {
    195     if (self->bs_tsd_enable) {
    196       for (sb_sample = 0; sb_sample < self->time_slots; sb_sample++) {
    197         if (self->bs_tsd_sep_data[sb_sample]) {
    198           for (idx = band_start; idx < self->mps_decor.num_bins; idx++) {
    199             scratch[sb_sample][idx].re = self->v[k][sb_sample][idx].re;
    200             scratch[sb_sample][idx].im = self->v[k][sb_sample][idx].im;
    201             self->v[k][sb_sample][idx].re = 0.0f;
    202             self->v[k][sb_sample][idx].im = 0.0f;
    203           }
    204         }
    205       }
    206     }
    207 
    208     ixheaacd_mps_decor_apply(&self->mps_decor, self->v[k], self->w_diff[k],
    209                              self->time_slots);
    210 
    211     if (self->bs_tsd_enable) {
    212       for (sb_sample = 0; sb_sample < self->time_slots; sb_sample++) {
    213         if (self->bs_tsd_sep_data[sb_sample]) {
    214           coeff.re = ixheaacd_tsd_mul_re[self->bs_tsd_tr_phase_data[sb_sample]];
    215           coeff.im = ixheaacd_tsd_mul_im[self->bs_tsd_tr_phase_data[sb_sample]];
    216 
    217           for (idx = band_start; idx < self->mps_decor.num_bins; idx++) {
    218             self->w_diff[k][sb_sample][idx].re +=
    219                 coeff.re * scratch[sb_sample][idx].re -
    220                 coeff.im * scratch[sb_sample][idx].im;
    221             self->w_diff[k][sb_sample][idx].im +=
    222                 coeff.im * scratch[sb_sample][idx].re +
    223                 coeff.re * scratch[sb_sample][idx].im;
    224           }
    225         }
    226       }
    227     }
    228   }
    229 }
    230 
    231 VOID ixheaacd_mps_mix_res_decor(ia_mps_dec_state_struct* self) {
    232   WORD32 ts, qs, row, indx;
    233 
    234   for (ts = 0; ts < self->time_slots; ts++) {
    235     for (qs = 0; qs < self->hyb_band_count; qs++) {
    236       indx = self->hyb_band_to_processing_band_table[qs];
    237 
    238       for (row = 0; row < self->dir_sig_count; row++) {
    239         self->w_dir[row][ts][qs].re = self->v[row][ts][qs].re;
    240         self->w_dir[row][ts][qs].im = self->v[row][ts][qs].im;
    241       }
    242 
    243       for (row = self->dir_sig_count;
    244            row < (self->dir_sig_count + self->decor_sig_count); row++) {
    245         if (indx < self->res_bands) {
    246           self->w_dir[row][ts][qs].re = self->hyb_res[ts][qs].re;
    247           self->w_dir[row][ts][qs].im = self->hyb_res[ts][qs].im;
    248         } else {
    249           self->w_dir[row][ts][qs].re = 0.0f;
    250           self->w_dir[row][ts][qs].im = 0.0f;
    251         }
    252       }
    253 
    254       for (row = 0; row < self->dir_sig_count; row++) {
    255         self->w_diff[row][ts][qs].re = 0.0f;
    256         self->w_diff[row][ts][qs].im = 0.0f;
    257       }
    258 
    259       for (row = self->dir_sig_count;
    260            row < (self->dir_sig_count + self->decor_sig_count); row++) {
    261         if (indx < self->res_bands) {
    262           self->w_diff[row][ts][qs].re = 0.0f;
    263           self->w_diff[row][ts][qs].im = 0.0f;
    264         }
    265       }
    266     }
    267   }
    268 }
    269 
    270 VOID ixheaacd_mps_create_w(ia_mps_dec_state_struct* self) {
    271   ixheaacd_mps_decor(self);
    272   ixheaacd_mps_mix_res_decor(self);
    273 }
    274 WORD32 ixheaacd_mps_apply(ia_mps_dec_state_struct* self,
    275                           FLOAT32** input_buffer[4],
    276                           FLOAT32 (*output_buffer)[4096]) {
    277   WORD32 ch, ts, qs;
    278   WORD32 time_slots = self->time_slots;
    279   WORD32 in_ch_count = self->in_ch_count + self->res_ch_count;
    280   WORD32 err = 0;
    281 
    282   self->output_buffer = output_buffer;
    283 
    284   assert(self->present_time_slot + time_slots <= self->time_slots);
    285 
    286   for (ts = 0; ts < time_slots; ts++) {
    287     for (ch = 0; ch < in_ch_count; ch++) {
    288       for (qs = 0; qs < self->qmf_band_count; qs++) {
    289         self->qmf_in[ch][self->present_time_slot + ts][qs].re =
    290             self->input_gain * input_buffer[2 * ch][ts][qs];
    291         self->qmf_in[ch][self->present_time_slot + ts][qs].im =
    292             self->input_gain * input_buffer[2 * ch + 1][ts][qs];
    293       }
    294     }
    295   }
    296 
    297   self->present_time_slot += time_slots;
    298 
    299   if (self->present_time_slot < self->time_slots) return 0;
    300 
    301   self->present_time_slot = 0;
    302 
    303   ixheaacd_mps_frame_decode(self);
    304 
    305   ixheaacd_mps_qmf_hyb_analysis(self);
    306 
    307   ixheaacd_pre_and_mix_matrix_calculation(self);
    308 
    309   ixheaacd_mps_pre_matrix_mix_matrix_smoothing(self);
    310 
    311   ixheaacd_mps_apply_pre_matrix(self);
    312 
    313   ixheaacd_mps_create_w(self);
    314 
    315   ixheaacd_mps_apply_mix_matrix(self);
    316 
    317   if (self->config->bs_temp_shape_config == 2) {
    318     ixheaacd_mps_time_env_shaping(self);
    319   }
    320 
    321   err = ixheaacd_mps_temp_process(self);
    322   if (err) return err;
    323 
    324   self->parse_nxt_frame = 1;
    325   return 0;
    326 }
    327 
    328 #define min(a, b) (((a) < (b)) ? (a) : (b))
    329 
    330 static WORD32 ixheaacd_mps_pcm_decode(ia_handle_bit_buf_struct it_bit_buff,
    331                                       WORD32* out_data_1, WORD32* out_data_2,
    332                                       WORD32 ixheaacd_drc_offset,
    333                                       WORD32 num_val, WORD32 num_levels) {
    334   WORD32 i = 0, j = 0, idx = 0;
    335   WORD32 max_grp_len = 0, grp_len = 0, next_val = 0, grp_val = 0;
    336   UWORD32 data = 0;
    337 
    338   FLOAT32 ld_nlev = 0.f;
    339 
    340   WORD32 pcm_chunk_size[7] = {0};
    341 
    342   switch (num_levels) {
    343     case 3:
    344       max_grp_len = 5;
    345       break;
    346     case 7:
    347       max_grp_len = 6;
    348       break;
    349     case 11:
    350       max_grp_len = 2;
    351       break;
    352     case 13:
    353       max_grp_len = 4;
    354       break;
    355     case 19:
    356       max_grp_len = 4;
    357       break;
    358     case 25:
    359       max_grp_len = 3;
    360       break;
    361     case 51:
    362       max_grp_len = 4;
    363       break;
    364     case 4:
    365     case 8:
    366     case 15:
    367     case 16:
    368     case 26:
    369     case 31:
    370       max_grp_len = 1;
    371       break;
    372     default:
    373       assert(0);
    374   }
    375 
    376   ld_nlev = (FLOAT32)(log((FLOAT32)num_levels) / log(2.f));
    377 
    378   for (i = 1; i <= max_grp_len; i++) {
    379     pcm_chunk_size[i] = (WORD32)ceil((FLOAT32)(i)*ld_nlev);
    380   }
    381 
    382   for (i = 0; i < num_val; i += max_grp_len) {
    383     grp_len = min(max_grp_len, num_val - i);
    384     data = ixheaacd_read_bits_buf(it_bit_buff, pcm_chunk_size[grp_len]);
    385 
    386     grp_val = data;
    387 
    388     for (j = 0; j < grp_len; j++) {
    389       idx = i + (grp_len - j - 1);
    390       next_val = grp_val % num_levels;
    391 
    392       if (out_data_2 == NULL) {
    393         out_data_1[idx] = next_val - ixheaacd_drc_offset;
    394       } else if (out_data_1 == NULL) {
    395         out_data_2[idx] = next_val - ixheaacd_drc_offset;
    396       } else {
    397         if (idx % 2) {
    398           out_data_2[idx / 2] = next_val - ixheaacd_drc_offset;
    399         } else {
    400           out_data_1[idx / 2] = next_val - ixheaacd_drc_offset;
    401         }
    402       }
    403 
    404       grp_val = (grp_val - next_val) / num_levels;
    405     }
    406   }
    407 
    408   return 1;
    409 }
    410 
    411 static WORD32 ixheaacd_mps_huff_read(ia_handle_bit_buf_struct it_bit_buff,
    412                                      const WORD32 (*node_tab)[][2],
    413                                      WORD32* out_data) {
    414   WORD32 node = 0;
    415   UWORD32 next_bit = 0;
    416 
    417   do {
    418     next_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    419     node = (*node_tab)[node][next_bit];
    420   } while (node > 0);
    421 
    422   *out_data = node;
    423 
    424   return 1;
    425 }
    426 
    427 static WORD32 ixheaacd_mps_huff_read_2d(ia_handle_bit_buf_struct it_bit_buff,
    428                                         const WORD32 (*node_tab)[][2],
    429                                         WORD32 out_data[2], WORD32* escape)
    430 
    431 {
    432   WORD32 huff_2d_8bit = 0;
    433   WORD32 node = 0;
    434 
    435   if (!ixheaacd_mps_huff_read(it_bit_buff, node_tab, &node)) return 0;
    436   *escape = (node == 0);
    437 
    438   if (*escape) {
    439     out_data[0] = 0;
    440     out_data[1] = 1;
    441   } else {
    442     huff_2d_8bit = -(node + 1);
    443     out_data[0] = huff_2d_8bit >> 4;
    444     out_data[1] = huff_2d_8bit & 0xf;
    445   }
    446 
    447   return 1;
    448 }
    449 
    450 static WORD32 ixheaacd_mps_sym_restore(ia_handle_bit_buf_struct it_bit_buff,
    451                                        WORD32 lav, WORD32 data[2]) {
    452   WORD32 tmp = 0;
    453   UWORD32 sym_bit = 0;
    454 
    455   WORD32 sum_val = data[0] + data[1];
    456   WORD32 diff_val = data[0] - data[1];
    457 
    458   if (sum_val > lav) {
    459     data[0] = -sum_val + (2 * lav + 1);
    460     data[1] = -diff_val;
    461   } else {
    462     data[0] = sum_val;
    463     data[1] = diff_val;
    464   }
    465 
    466   if (data[0] + data[1] != 0) {
    467     sym_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    468     if (sym_bit) {
    469       data[0] = -data[0];
    470       data[1] = -data[1];
    471     }
    472   }
    473 
    474   if (data[0] - data[1] != 0) {
    475     sym_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    476     if (sym_bit) {
    477       tmp = data[0];
    478       data[0] = data[1];
    479       data[1] = tmp;
    480     }
    481   }
    482 
    483   return 1;
    484 }
    485 
    486 static WORD32 ixheaacd_mps_sym_restoreipd(ia_handle_bit_buf_struct it_bit_buff,
    487                                           WORD32 lav, WORD32 data[2]) {
    488   WORD32 tmp = 0;
    489   UWORD32 sym_bit = 0;
    490 
    491   WORD32 sum_val = data[0] + data[1];
    492   WORD32 diff_val = data[0] - data[1];
    493 
    494   if (sum_val > lav) {
    495     data[0] = -sum_val + (2 * lav + 1);
    496     data[1] = -diff_val;
    497   } else {
    498     data[0] = sum_val;
    499     data[1] = diff_val;
    500   }
    501 
    502   if (data[0] - data[1] != 0) {
    503     sym_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
    504     if (sym_bit) {
    505       tmp = data[0];
    506       data[0] = data[1];
    507       data[1] = tmp;
    508     }
    509   }
    510 
    511   return 1;
    512 }
    513 
    514 static WORD32 ixheaacd_mps_huff_dec_pilot(ia_handle_bit_buf_struct it_bit_buff,
    515                                           const WORD32 (*node_tab)[][2],
    516                                           WORD32* pilot_data) {
    517   WORD32 node = 0;
    518 
    519   if (!ixheaacd_mps_huff_read(it_bit_buff, node_tab, &node)) return 0;
    520   *pilot_data = -(node + 1);
    521 
    522   return 1;
    523 }
    524 
    525 static WORD32 ixheaacd_mps_huff_dec_cld_1d(
    526     ia_handle_bit_buf_struct it_bit_buff,
    527     const ia_huff_cld_node_1d_struct* huff_nodes, WORD32* out_data,
    528     WORD32 num_val, WORD32 p0_flag) {
    529   WORD32 i = 0, node = 0, ixheaacd_drc_offset = 0;
    530   WORD32 od = 0, od_sign = 0;
    531   UWORD32 data = 0;
    532 
    533   if (p0_flag) {
    534     if (!ixheaacd_mps_huff_read(
    535             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.cld,
    536             &node))
    537       return 0;
    538     out_data[0] = -(node + 1);
    539     ixheaacd_drc_offset = 1;
    540   }
    541 
    542   for (i = ixheaacd_drc_offset; i < num_val; i++) {
    543     if (!ixheaacd_mps_huff_read(
    544             it_bit_buff, (ia_huff_node_struct)&huff_nodes->node_tab, &node))
    545       return 0;
    546     od = -(node + 1);
    547 
    548     if (od != 0) {
    549       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
    550       od_sign = data;
    551 
    552       if (od_sign) od = -od;
    553     }
    554 
    555     out_data[i] = od;
    556   }
    557 
    558   return 1;
    559 }
    560 
    561 static WORD32 ixheaacd_mps_huff_dec_ipd_1d(
    562     ia_handle_bit_buf_struct it_bit_buff,
    563     const ia_huff_ipd_node_1d_struct* huff_nodes, WORD32* out_data,
    564     WORD32 num_val, WORD32 p0_flag) {
    565   WORD32 i = 0, node = 0, ixheaacd_drc_offset = 0;
    566   WORD32 od = 0;
    567 
    568   if (p0_flag) {
    569     if (!ixheaacd_mps_huff_read(
    570             it_bit_buff,
    571             (ia_huff_node_struct)&ixheaacd_huff_ipd_nodes.hp0.node_tab, &node))
    572       return 0;
    573     out_data[0] = -(node + 1);
    574     ixheaacd_drc_offset = 1;
    575   }
    576 
    577   for (i = ixheaacd_drc_offset; i < num_val; i++) {
    578     if (!ixheaacd_mps_huff_read(
    579             it_bit_buff, (ia_huff_node_struct)&huff_nodes->node_tab, &node))
    580       return 0;
    581     od = -(node + 1);
    582     out_data[i] = od;
    583   }
    584 
    585   return 1;
    586 }
    587 
    588 static WORD32 ixheaacd_mps_huff_dec_icc_1d(
    589     ia_handle_bit_buf_struct it_bit_buff,
    590     const ia_huff_icc_node_1d_struct* huff_nodes, WORD32* out_data,
    591     WORD32 num_val, WORD32 p0_flag) {
    592   WORD32 i = 0, node = 0, ixheaacd_drc_offset = 0;
    593   WORD32 od = 0, od_sign = 0;
    594   UWORD32 data = 0;
    595 
    596   if (p0_flag) {
    597     if (!ixheaacd_mps_huff_read(
    598             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.icc,
    599             &node))
    600       return 0;
    601     out_data[0] = -(node + 1);
    602     ixheaacd_drc_offset = 1;
    603   }
    604 
    605   for (i = ixheaacd_drc_offset; i < num_val; i++) {
    606     if (!ixheaacd_mps_huff_read(
    607             it_bit_buff, (ia_huff_node_struct)&huff_nodes->node_tab, &node))
    608       return 0;
    609     od = -(node + 1);
    610 
    611     if (od != 0) {
    612       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
    613       od_sign = data;
    614 
    615       if (od_sign) od = -od;
    616     }
    617 
    618     out_data[i] = od;
    619   }
    620 
    621   return 1;
    622 }
    623 
    624 static WORD32 ixheaacd_mps_huff_dec_cld_2d(
    625     ia_handle_bit_buf_struct it_bit_buff,
    626     const ia_huff_cld_node_2d_struct* huff_nodes, WORD32 out_data[][2],
    627     WORD32 num_val, WORD32 ch_fac, WORD32* p0_data[2]) {
    628   WORD32 i = 0, lav = 0, escape = 0, esc_contrl = 0;
    629   WORD32 node = 0;
    630   UWORD32 data = 0;
    631 
    632   WORD32 esc_data[MAXBANDS][2] = {{0}};
    633   WORD32 esc_idx[MAXBANDS] = {0};
    634 
    635   if (!ixheaacd_mps_huff_read(
    636           it_bit_buff,
    637           (ia_huff_node_struct)&ixheaacd_huff_lav_idx_nodes.node_tab, &node))
    638     return 0;
    639   data = -(node + 1);
    640 
    641   lav = 2 * data + 3;
    642 
    643   if (p0_data[0] != NULL) {
    644     if (!ixheaacd_mps_huff_read(
    645             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.cld,
    646             &node))
    647       return 0;
    648     *p0_data[0] = -(node + 1);
    649   }
    650   if (p0_data[1] != NULL) {
    651     if (!ixheaacd_mps_huff_read(
    652             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.cld,
    653             &node))
    654       return 0;
    655     *p0_data[1] = -(node + 1);
    656   }
    657 
    658   for (i = 0; i < num_val; i += ch_fac) {
    659     switch (lav) {
    660       case 3:
    661         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    662                                        (ia_huff_node_struct)&huff_nodes->lav3,
    663                                        out_data[i], &escape))
    664           return 0;
    665         break;
    666       case 5:
    667         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    668                                        (ia_huff_node_struct)&huff_nodes->lav5,
    669                                        out_data[i], &escape))
    670           return 0;
    671         break;
    672       case 7:
    673         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    674                                        (ia_huff_node_struct)&huff_nodes->lav7,
    675                                        out_data[i], &escape))
    676           return 0;
    677         break;
    678       case 9:
    679         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    680                                        (ia_huff_node_struct)&huff_nodes->lav9,
    681                                        out_data[i], &escape))
    682           return 0;
    683         break;
    684       default:
    685         break;
    686     }
    687 
    688     if (escape) {
    689       esc_idx[esc_contrl++] = i;
    690     } else {
    691       if (!ixheaacd_mps_sym_restore(it_bit_buff, lav, out_data[i])) return 0;
    692     }
    693   }
    694 
    695   if (esc_contrl > 0) {
    696     if (!ixheaacd_mps_pcm_decode(it_bit_buff, esc_data[0], esc_data[1], 0,
    697                                  2 * esc_contrl, (2 * lav + 1)))
    698       return 0;
    699 
    700     for (i = 0; i < esc_contrl; i++) {
    701       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
    702       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
    703     }
    704   }
    705 
    706   return 1;
    707 }
    708 
    709 static WORD32 ixheaacd_mps_huff_dec_icc_2d(
    710     ia_handle_bit_buf_struct it_bit_buff,
    711     const ia_huff_icc_node_2d_struct* huff_nodes, WORD32 out_data[][2],
    712     WORD32 num_val, WORD32 ch_fac, WORD32* p0_data[2]) {
    713   WORD32 i = 0, lav = 0, escape = 0, esc_contrl = 0;
    714   WORD32 node = 0;
    715   UWORD32 data = 0;
    716 
    717   WORD32 esc_data[2][MAXBANDS] = {{0}};
    718   WORD32 esc_idx[MAXBANDS] = {0};
    719 
    720   if (!ixheaacd_mps_huff_read(
    721           it_bit_buff,
    722           (ia_huff_node_struct)&ixheaacd_huff_lav_idx_nodes.node_tab, &node))
    723     return 0;
    724   data = -(node + 1);
    725 
    726   lav = 2 * data + 1;
    727 
    728   if (p0_data[0] != NULL) {
    729     if (!ixheaacd_mps_huff_read(
    730             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.icc,
    731             &node))
    732       return 0;
    733     *p0_data[0] = -(node + 1);
    734   }
    735   if (p0_data[1] != NULL) {
    736     if (!ixheaacd_mps_huff_read(
    737             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.icc,
    738             &node))
    739       return 0;
    740     *p0_data[1] = -(node + 1);
    741   }
    742 
    743   for (i = 0; i < num_val; i += ch_fac) {
    744     switch (lav) {
    745       case 1:
    746         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    747                                        (ia_huff_node_struct)&huff_nodes->lav1,
    748                                        out_data[i], &escape))
    749           return 0;
    750         break;
    751       case 3:
    752         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    753                                        (ia_huff_node_struct)&huff_nodes->lav3,
    754                                        out_data[i], &escape))
    755           return 0;
    756         break;
    757       case 5:
    758         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    759                                        (ia_huff_node_struct)&huff_nodes->lav5,
    760                                        out_data[i], &escape))
    761           return 0;
    762         break;
    763       case 7:
    764         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    765                                        (ia_huff_node_struct)&huff_nodes->lav7,
    766                                        out_data[i], &escape))
    767           return 0;
    768         break;
    769     }
    770 
    771     if (escape) {
    772       esc_idx[esc_contrl++] = i;
    773     } else {
    774       if (!ixheaacd_mps_sym_restore(it_bit_buff, lav, out_data[i])) return 0;
    775     }
    776   }
    777 
    778   if (esc_contrl > 0) {
    779     if (!ixheaacd_mps_pcm_decode(it_bit_buff, esc_data[0], esc_data[1], 0,
    780                                  2 * esc_contrl, (2 * lav + 1)))
    781       return 0;
    782 
    783     for (i = 0; i < esc_contrl; i++) {
    784       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
    785       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
    786     }
    787   }
    788 
    789   return 1;
    790 }
    791 
    792 static WORD32 ixheaacd_mps_huff_dec_ipd_2d(
    793     ia_handle_bit_buf_struct it_bit_buff,
    794     const ia_huff_ipd_node_2d_struct* huff_nodes, WORD32 out_data[][2],
    795     WORD32 num_val, WORD32 ch_fac, WORD32* p0_data[2]) {
    796   WORD32 i = 0, lav = 0, escape = 0, esc_contrl = 0;
    797   WORD32 node = 0;
    798   UWORD32 data = 0;
    799 
    800   WORD32 esc_data[2][MAXBANDS] = {{0}};
    801   WORD32 esc_idx[MAXBANDS] = {0};
    802 
    803   if (!ixheaacd_mps_huff_read(
    804           it_bit_buff,
    805           (ia_huff_node_struct)&ixheaacd_huff_lav_idx_nodes.node_tab, &node))
    806     return 0;
    807 
    808   data = -(node + 1);
    809   if (data == 0)
    810     data = 3;
    811   else
    812     data--;
    813 
    814   lav = 2 * data + 1;
    815 
    816   if (p0_data[0] != NULL) {
    817     if (!ixheaacd_mps_huff_read(
    818             it_bit_buff,
    819             (ia_huff_node_struct)&ixheaacd_huff_ipd_nodes.hp0.node_tab, &node))
    820       return 0;
    821     *p0_data[0] = -(node + 1);
    822   }
    823   if (p0_data[1] != NULL) {
    824     if (!ixheaacd_mps_huff_read(
    825             it_bit_buff,
    826             (ia_huff_node_struct)&ixheaacd_huff_ipd_nodes.hp0.node_tab, &node))
    827       return 0;
    828     *p0_data[1] = -(node + 1);
    829   }
    830 
    831   for (i = 0; i < num_val; i += ch_fac) {
    832     switch (lav) {
    833       case 1:
    834         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    835                                        (ia_huff_node_struct)&huff_nodes->lav1,
    836                                        out_data[i], &escape))
    837           return 0;
    838         break;
    839       case 3:
    840         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    841                                        (ia_huff_node_struct)&huff_nodes->lav3,
    842                                        out_data[i], &escape))
    843           return 0;
    844         break;
    845       case 5:
    846         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    847                                        (ia_huff_node_struct)&huff_nodes->lav5,
    848                                        out_data[i], &escape))
    849           return 0;
    850         break;
    851       case 7:
    852         if (!ixheaacd_mps_huff_read_2d(it_bit_buff,
    853                                        (ia_huff_node_struct)&huff_nodes->lav7,
    854                                        out_data[i], &escape))
    855           return 0;
    856         break;
    857     }
    858 
    859     if (escape) {
    860       esc_idx[esc_contrl++] = i;
    861     } else {
    862       if (!ixheaacd_mps_sym_restoreipd(it_bit_buff, lav, out_data[i])) return 0;
    863     }
    864   }
    865 
    866   if (esc_contrl > 0) {
    867     if (!ixheaacd_mps_pcm_decode(it_bit_buff, esc_data[0], esc_data[1], 0,
    868                                  2 * esc_contrl, (2 * lav + 1)))
    869       return 0;
    870 
    871     for (i = 0; i < esc_contrl; i++) {
    872       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
    873       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
    874     }
    875   }
    876 
    877   return 1;
    878 }
    879 
    880 static WORD32 ixheaacd_huff_decode(ia_handle_bit_buf_struct it_bit_buff,
    881                                    WORD32* out_data_1, WORD32* out_data_2,
    882                                    WORD32 data_type, WORD32 diff_type_1,
    883                                    WORD32 diff_type_2, WORD32 pilot_coding_flag,
    884                                    WORD32* pilot_data, WORD32 num_val,
    885                                    WORD32* cdg_scheme) {
    886   WORD32 diff_type;
    887 
    888   WORD32 i = 0;
    889   UWORD32 data = 0;
    890 
    891   WORD32 pair_vec[MAXBANDS][2];
    892 
    893   WORD32* p0_data_1[2] = {NULL, NULL};
    894   WORD32* p0_data_2[2] = {NULL, NULL};
    895 
    896   WORD32 p0_flag[2];
    897 
    898   WORD32 num_val_1_int = num_val;
    899   WORD32 num_val_2_int = num_val;
    900 
    901   WORD32* out_data_1_int = out_data_1;
    902   WORD32* out_data_2_int = out_data_2;
    903 
    904   WORD32 df_rest_flag_1 = 0;
    905   WORD32 df_rest_flag_2 = 0;
    906 
    907   WORD32 huff_yy_1;
    908   WORD32 huff_yy_2;
    909   WORD32 huff_yy;
    910 
    911   if (pilot_coding_flag) {
    912     switch (data_type) {
    913       case CLD:
    914         if (out_data_1 != NULL) {
    915           if (!ixheaacd_mps_huff_dec_pilot(
    916                   it_bit_buff,
    917                   (ia_huff_node_struct)&ixheaacd_huff_pilot_nodes.cld,
    918                   pilot_data))
    919             return 0;
    920         }
    921         break;
    922 
    923       case ICC:
    924         if (out_data_1 != NULL) {
    925           if (!ixheaacd_mps_huff_dec_pilot(
    926                   it_bit_buff,
    927                   (ia_huff_node_struct)&ixheaacd_huff_pilot_nodes.icc,
    928                   pilot_data))
    929             return 0;
    930         }
    931         break;
    932 
    933       default:
    934         if (out_data_1 != NULL) {
    935           return 0;
    936         }
    937         break;
    938     }
    939   }
    940 
    941   data = ixheaacd_read_bits_buf(it_bit_buff, 1);
    942   *cdg_scheme = data << PAIR_SHIFT;
    943 
    944   if (*cdg_scheme >> PAIR_SHIFT == HUFF_2D) {
    945     if ((out_data_1 != NULL) && (out_data_2 != NULL)) {
    946       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
    947       *cdg_scheme |= data;
    948     } else {
    949       *cdg_scheme |= FREQ_PAIR;
    950     }
    951   }
    952 
    953   if (pilot_coding_flag) {
    954     huff_yy_1 = PCM_PLT;
    955     huff_yy_2 = PCM_PLT;
    956   } else {
    957     huff_yy_1 = diff_type_1;
    958     huff_yy_2 = diff_type_2;
    959   }
    960 
    961   switch (*cdg_scheme >> PAIR_SHIFT) {
    962     case HUFF_1D:
    963 
    964       p0_flag[0] = (diff_type_1 == DIFF_FREQ) && !pilot_coding_flag;
    965       p0_flag[1] = (diff_type_2 == DIFF_FREQ) && !pilot_coding_flag;
    966 
    967       switch (data_type) {
    968         case CLD:
    969           if (out_data_1 != NULL) {
    970             if (!ixheaacd_mps_huff_dec_cld_1d(
    971                     it_bit_buff, &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_1],
    972                     out_data_1, num_val_1_int, p0_flag[0]))
    973               return 0;
    974           }
    975           if (out_data_2 != NULL) {
    976             if (!ixheaacd_mps_huff_dec_cld_1d(
    977                     it_bit_buff, &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_2],
    978                     out_data_2, num_val_2_int, p0_flag[1]))
    979               return 0;
    980           }
    981 
    982           break;
    983 
    984         case ICC:
    985           if (out_data_1 != NULL) {
    986             if (!ixheaacd_mps_huff_dec_icc_1d(
    987                     it_bit_buff, &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_1],
    988                     out_data_1, num_val_1_int, p0_flag[0]))
    989               return 0;
    990           }
    991           if (out_data_2 != NULL) {
    992             if (!ixheaacd_mps_huff_dec_icc_1d(
    993                     it_bit_buff, &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_2],
    994                     out_data_2, num_val_2_int, p0_flag[1]))
    995               return 0;
    996           }
    997 
    998           break;
    999 
   1000         case IPD:
   1001           if (out_data_1 != NULL) {
   1002             if (!ixheaacd_mps_huff_dec_ipd_1d(
   1003                     it_bit_buff, &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_1],
   1004                     out_data_1, num_val_1_int, p0_flag[0]))
   1005               return 0;
   1006           }
   1007           if (out_data_2 != NULL) {
   1008             if (!ixheaacd_mps_huff_dec_ipd_1d(
   1009                     it_bit_buff, &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_2],
   1010                     out_data_2, num_val_2_int, p0_flag[1]))
   1011               return 0;
   1012           }
   1013 
   1014           break;
   1015 
   1016         default:
   1017           break;
   1018       }
   1019 
   1020       break;
   1021 
   1022     case HUFF_2D:
   1023 
   1024       switch (*cdg_scheme & PAIR_MASK) {
   1025         case FREQ_PAIR:
   1026 
   1027           if (out_data_1 != NULL) {
   1028             if (!pilot_coding_flag && diff_type_1 == DIFF_FREQ) {
   1029               p0_data_1[0] = &out_data_1[0];
   1030               p0_data_1[1] = NULL;
   1031 
   1032               num_val_1_int -= 1;
   1033               out_data_1_int += 1;
   1034             }
   1035             df_rest_flag_1 = num_val_1_int % 2;
   1036             if (df_rest_flag_1) num_val_1_int -= 1;
   1037           }
   1038           if (out_data_2 != NULL) {
   1039             if (!pilot_coding_flag && diff_type_2 == DIFF_FREQ) {
   1040               p0_data_2[0] = NULL;
   1041               p0_data_2[1] = &out_data_2[0];
   1042 
   1043               num_val_2_int -= 1;
   1044               out_data_2_int += 1;
   1045             }
   1046             df_rest_flag_2 = num_val_2_int % 2;
   1047             if (df_rest_flag_2) num_val_2_int -= 1;
   1048           }
   1049 
   1050           switch (data_type) {
   1051             case CLD:
   1052 
   1053               if (out_data_1 != NULL) {
   1054                 if (!ixheaacd_mps_huff_dec_cld_2d(
   1055                         it_bit_buff,
   1056                         &ixheaacd_huff_cld_nodes.h_2_dim[huff_yy_1][FREQ_PAIR],
   1057                         pair_vec, num_val_1_int, 2, p0_data_1))
   1058                   return 0;
   1059                 if (df_rest_flag_1) {
   1060                   if (!ixheaacd_mps_huff_dec_cld_1d(
   1061                           it_bit_buff,
   1062                           &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_1],
   1063                           out_data_1_int + num_val_1_int, 1, 0))
   1064                     return 0;
   1065                 }
   1066               }
   1067               if (out_data_2 != NULL) {
   1068                 if (!ixheaacd_mps_huff_dec_cld_2d(
   1069                         it_bit_buff,
   1070                         &ixheaacd_huff_cld_nodes.h_2_dim[huff_yy_2][FREQ_PAIR],
   1071                         pair_vec + 1, num_val_2_int, 2, p0_data_2))
   1072                   return 0;
   1073                 if (df_rest_flag_2) {
   1074                   if (!ixheaacd_mps_huff_dec_cld_1d(
   1075                           it_bit_buff,
   1076                           &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_2],
   1077                           out_data_2_int + num_val_2_int, 1, 0))
   1078                     return 0;
   1079                 }
   1080               }
   1081               break;
   1082 
   1083             case ICC:
   1084               if (out_data_1 != NULL) {
   1085                 if (!ixheaacd_mps_huff_dec_icc_2d(
   1086                         it_bit_buff,
   1087                         &ixheaacd_huff_icc_nodes.h_2_dim[huff_yy_1][FREQ_PAIR],
   1088                         pair_vec, num_val_1_int, 2, p0_data_1))
   1089                   return 0;
   1090                 if (df_rest_flag_1) {
   1091                   if (!ixheaacd_mps_huff_dec_icc_1d(
   1092                           it_bit_buff,
   1093                           &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_1],
   1094                           out_data_1_int + num_val_1_int, 1, 0))
   1095                     return 0;
   1096                 }
   1097               }
   1098               if (out_data_2 != NULL) {
   1099                 if (!ixheaacd_mps_huff_dec_icc_2d(
   1100                         it_bit_buff,
   1101                         &ixheaacd_huff_icc_nodes.h_2_dim[huff_yy_2][FREQ_PAIR],
   1102                         pair_vec + 1, num_val_2_int, 2, p0_data_2))
   1103                   return 0;
   1104                 if (df_rest_flag_2) {
   1105                   if (!ixheaacd_mps_huff_dec_icc_1d(
   1106                           it_bit_buff,
   1107                           &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_2],
   1108                           out_data_2_int + num_val_2_int, 1, 0))
   1109                     return 0;
   1110                 }
   1111               }
   1112               break;
   1113 
   1114             case IPD:
   1115               if (out_data_1 != NULL) {
   1116                 if (!ixheaacd_mps_huff_dec_ipd_2d(
   1117                         it_bit_buff,
   1118                         &ixheaacd_huff_ipd_nodes.h_2_dim[huff_yy_1][FREQ_PAIR],
   1119                         pair_vec, num_val_1_int, 2, p0_data_1))
   1120                   return 0;
   1121                 if (df_rest_flag_1) {
   1122                   if (!ixheaacd_mps_huff_dec_ipd_1d(
   1123                           it_bit_buff,
   1124                           &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_1],
   1125                           out_data_1_int + num_val_1_int, 1, 0))
   1126                     return 0;
   1127                 }
   1128               }
   1129               if (out_data_2 != NULL) {
   1130                 if (!ixheaacd_mps_huff_dec_ipd_2d(
   1131                         it_bit_buff,
   1132                         &ixheaacd_huff_ipd_nodes.h_2_dim[huff_yy_2][FREQ_PAIR],
   1133                         pair_vec + 1, num_val_2_int, 2, p0_data_2))
   1134                   return 0;
   1135                 if (df_rest_flag_2) {
   1136                   if (!ixheaacd_mps_huff_dec_ipd_1d(
   1137                           it_bit_buff,
   1138                           &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_2],
   1139                           out_data_2_int + num_val_2_int, 1, 0))
   1140                     return 0;
   1141                 }
   1142               }
   1143               break;
   1144 
   1145             default:
   1146               break;
   1147           }
   1148 
   1149           if (out_data_1 != NULL) {
   1150             for (i = 0; i < num_val_1_int - 1; i += 2) {
   1151               out_data_1_int[i] = pair_vec[i][0];
   1152               out_data_1_int[i + 1] = pair_vec[i][1];
   1153             }
   1154           }
   1155           if (out_data_2 != NULL) {
   1156             for (i = 0; i < num_val_2_int - 1; i += 2) {
   1157               out_data_2_int[i] = pair_vec[i + 1][0];
   1158               out_data_2_int[i + 1] = pair_vec[i + 1][1];
   1159             }
   1160           }
   1161 
   1162           break;
   1163 
   1164         case TIME_PAIR:
   1165 
   1166           if (!pilot_coding_flag &&
   1167               ((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
   1168             p0_data_1[0] = &out_data_1[0];
   1169             p0_data_1[1] = &out_data_2[0];
   1170 
   1171             out_data_1_int += 1;
   1172             out_data_2_int += 1;
   1173 
   1174             num_val_1_int -= 1;
   1175           }
   1176 
   1177           if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
   1178             diff_type = DIFF_TIME;
   1179           } else {
   1180             diff_type = DIFF_FREQ;
   1181           }
   1182           if (pilot_coding_flag) {
   1183             huff_yy = PCM_PLT;
   1184           } else {
   1185             huff_yy = diff_type;
   1186           }
   1187 
   1188           switch (data_type) {
   1189             case CLD:
   1190               if (!ixheaacd_mps_huff_dec_cld_2d(
   1191                       it_bit_buff,
   1192                       &ixheaacd_huff_cld_nodes.h_2_dim[huff_yy][TIME_PAIR],
   1193                       pair_vec, num_val_1_int, 1, p0_data_1))
   1194                 return 0;
   1195               break;
   1196 
   1197             case ICC:
   1198               if (!ixheaacd_mps_huff_dec_icc_2d(
   1199                       it_bit_buff,
   1200                       &ixheaacd_huff_icc_nodes.h_2_dim[huff_yy][TIME_PAIR],
   1201                       pair_vec, num_val_1_int, 1, p0_data_1))
   1202                 return 0;
   1203               break;
   1204 
   1205             case IPD:
   1206               if (!ixheaacd_mps_huff_dec_ipd_2d(
   1207                       it_bit_buff,
   1208                       &ixheaacd_huff_ipd_nodes.h_2_dim[huff_yy][TIME_PAIR],
   1209                       pair_vec, num_val_1_int, 1, p0_data_1))
   1210                 return 0;
   1211               break;
   1212 
   1213             default:
   1214               break;
   1215           }
   1216 
   1217           for (i = 0; i < num_val_1_int; i++) {
   1218             out_data_1_int[i] = pair_vec[i][0];
   1219             out_data_2_int[i] = pair_vec[i][1];
   1220           }
   1221 
   1222           break;
   1223 
   1224         default:
   1225           break;
   1226       }
   1227 
   1228       break;
   1229 
   1230     default:
   1231       break;
   1232   }
   1233 
   1234   return 1;
   1235 }
   1236 
   1237 static VOID ixheaacd_diff_freq_decode(WORD32* diff_data, WORD32* out_data,
   1238                                       WORD32 num_val) {
   1239   WORD32 i = 0;
   1240 
   1241   out_data[0] = diff_data[0];
   1242 
   1243   for (i = 1; i < num_val; i++) {
   1244     out_data[i] = out_data[i - 1] + diff_data[i];
   1245   }
   1246 }
   1247 
   1248 static VOID ixheaacd_mps_diff_time_dec_bwd(WORD32* prev_data, WORD32* diff_data,
   1249                                            WORD32* out_data,
   1250                                            WORD32 mixed_diff_type,
   1251                                            WORD32 num_val) {
   1252   WORD32 i = 0;
   1253 
   1254   if (mixed_diff_type) {
   1255     out_data[0] = diff_data[0];
   1256     for (i = 1; i < num_val; i++) {
   1257       out_data[i] = prev_data[i] + diff_data[i];
   1258     }
   1259   } else {
   1260     for (i = 0; i < num_val; i++) {
   1261       out_data[i] = prev_data[i] + diff_data[i];
   1262     }
   1263   }
   1264 }
   1265 
   1266 static VOID ixheaacd_mps_diff_time_dec_fwd(WORD32* prev_data, WORD32* diff_data,
   1267                                            WORD32* out_data,
   1268                                            WORD32 mixed_diff_type,
   1269                                            WORD32 num_val) {
   1270   WORD32 i = 0;
   1271 
   1272   if (mixed_diff_type) {
   1273     out_data[0] = diff_data[0];
   1274     for (i = 1; i < num_val; i++) {
   1275       out_data[i] = prev_data[i] - diff_data[i];
   1276     }
   1277   } else {
   1278     for (i = 0; i < num_val; i++) {
   1279       out_data[i] = prev_data[i] - diff_data[i];
   1280     }
   1281   }
   1282 }
   1283 
   1284 static WORD32 ixheaacd_attach_lsb(ia_handle_bit_buf_struct it_bit_buff,
   1285                                   WORD32* in_data_msb,
   1286                                   WORD32 ixheaacd_drc_offset, WORD32 num_lsb,
   1287                                   WORD32 num_val, WORD32* out_data) {
   1288   WORD32 i = 0, lsb = 0, msb = 0;
   1289   UWORD32 data = 0;
   1290 
   1291   for (i = 0; i < num_val; i++) {
   1292     msb = in_data_msb[i];
   1293 
   1294     if (num_lsb > 0) {
   1295       data = ixheaacd_read_bits_buf(it_bit_buff, num_lsb);
   1296       lsb = data;
   1297 
   1298       out_data[i] = ((msb << num_lsb) | lsb) - ixheaacd_drc_offset;
   1299     } else
   1300       out_data[i] = msb - ixheaacd_drc_offset;
   1301   }
   1302 
   1303   return 0;
   1304 }
   1305 
   1306 WORD32 ixheaacd_mps_ecdatapairdec(ia_handle_bit_buf_struct it_bit_buff,
   1307                                   WORD32 outdata[][MAXBANDS],
   1308                                   WORD32 history[MAXBANDS], WORD32 data_type,
   1309                                   WORD32 set_idx, WORD32 data_bands,
   1310                                   WORD32 pair_flag, WORD32 coarse_flag,
   1311                                   WORD32 independency_flag)
   1312 
   1313 {
   1314   WORD32 diff_time_back_flag = !independency_flag || (set_idx > 0);
   1315   WORD32 attach_lsb_flag = 0;
   1316   WORD32 pcm_coding_flag = 0;
   1317   WORD32 pilot_coding_flag = 0;
   1318   WORD32 pilot_data[2] = {0, 0};
   1319   WORD32 mixed_time_pair = 0, pcm_val = 0;
   1320   WORD32 quant_levels = 0, quant_offset = 0;
   1321   UWORD32 data = 0;
   1322   WORD32 band_start = 0;
   1323 
   1324   WORD32 data_pair[2][MAXBANDS] = {{0}};
   1325   WORD32 data_diff[2][MAXBANDS] = {{0}};
   1326 
   1327   WORD32 msb_state[MAXBANDS] = {0};
   1328 
   1329   WORD32* data_array[2] = {NULL, NULL};
   1330 
   1331   WORD32 diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
   1332   WORD32 cdg_scheme = HUFF_1D;
   1333   WORD32 direction = BACKWARDS;
   1334 
   1335   switch (data_type) {
   1336     case CLD:
   1337       if (coarse_flag) {
   1338         attach_lsb_flag = 0;
   1339         quant_levels = 15;
   1340         quant_offset = 7;
   1341       } else {
   1342         attach_lsb_flag = 0;
   1343         quant_levels = 31;
   1344         quant_offset = 15;
   1345       }
   1346 
   1347       break;
   1348 
   1349     case ICC:
   1350       if (coarse_flag) {
   1351         attach_lsb_flag = 0;
   1352         quant_levels = 4;
   1353         quant_offset = 0;
   1354       } else {
   1355         attach_lsb_flag = 0;
   1356         quant_levels = 8;
   1357         quant_offset = 0;
   1358       }
   1359 
   1360       break;
   1361 
   1362     case IPD:
   1363       if (coarse_flag) {
   1364         attach_lsb_flag = 0;
   1365         quant_levels = 8;
   1366         quant_offset = 0;
   1367       } else {
   1368         attach_lsb_flag = 1;
   1369         quant_levels = 16;
   1370         quant_offset = 0;
   1371       }
   1372       break;
   1373 
   1374     default:
   1375       fprintf(stderr, "Unknown type of data!\n");
   1376       return 0;
   1377   }
   1378 
   1379   data = ixheaacd_read_bits_buf(it_bit_buff, 1);
   1380   pcm_coding_flag = data;
   1381 
   1382   pilot_coding_flag = 0;
   1383 
   1384   if (pcm_coding_flag && !pilot_coding_flag) {
   1385     if (pair_flag) {
   1386       data_array[0] = data_pair[0];
   1387       data_array[1] = data_pair[1];
   1388       pcm_val = 2 * data_bands;
   1389     } else {
   1390       data_array[0] = data_pair[0];
   1391       data_array[1] = NULL;
   1392       pcm_val = data_bands;
   1393     }
   1394 
   1395     if (!ixheaacd_mps_pcm_decode(it_bit_buff, data_array[0], data_array[1],
   1396                                  quant_offset, pcm_val, quant_levels))
   1397       return 0;
   1398 
   1399   } else {
   1400     if (pair_flag) {
   1401       data_array[0] = data_diff[0];
   1402       data_array[1] = data_diff[1];
   1403     } else {
   1404       data_array[0] = data_diff[0];
   1405       data_array[1] = NULL;
   1406     }
   1407 
   1408     diff_type[0] = DIFF_FREQ;
   1409     diff_type[1] = DIFF_FREQ;
   1410 
   1411     direction = BACKWARDS;
   1412 
   1413     if (!pilot_coding_flag) {
   1414       if (pair_flag || diff_time_back_flag) {
   1415         data = ixheaacd_read_bits_buf(it_bit_buff, 1);
   1416         diff_type[0] = data;
   1417       }
   1418 
   1419       if (pair_flag && ((diff_type[0] == DIFF_FREQ) || diff_time_back_flag)) {
   1420         data = ixheaacd_read_bits_buf(it_bit_buff, 1);
   1421         diff_type[1] = data;
   1422       }
   1423     }
   1424 
   1425     if (!ixheaacd_huff_decode(it_bit_buff, data_array[0], data_array[1],
   1426                               data_type, diff_type[0], diff_type[1],
   1427                               pilot_coding_flag, pilot_data, data_bands,
   1428                               &cdg_scheme)) {
   1429       return 0;
   1430     }
   1431 
   1432     if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
   1433       if (pair_flag) {
   1434         if ((diff_type[0] == DIFF_TIME) && !diff_time_back_flag) {
   1435           direction = FORWARDS;
   1436         } else if (diff_type[1] == DIFF_TIME) {
   1437           direction = BACKWARDS;
   1438         } else {
   1439           data = ixheaacd_read_bits_buf(it_bit_buff, 1);
   1440           direction = data;
   1441         }
   1442       } else {
   1443         direction = BACKWARDS;
   1444       }
   1445     }
   1446 
   1447     mixed_time_pair = (diff_type[0] != diff_type[1]) &&
   1448                       ((cdg_scheme & PAIR_MASK) == TIME_PAIR);
   1449 
   1450     if (direction == BACKWARDS) {
   1451       if (diff_type[0] == DIFF_FREQ) {
   1452         ixheaacd_diff_freq_decode(data_diff[0], data_pair[0], data_bands);
   1453       } else {
   1454         WORD32 i;
   1455         for (i = 0; i < data_bands; i++) {
   1456           msb_state[i] = history[i + band_start] + quant_offset;
   1457           if (attach_lsb_flag) {
   1458             msb_state[i] >>= 1;
   1459           }
   1460         }
   1461         ixheaacd_mps_diff_time_dec_bwd(msb_state, data_diff[0], data_pair[0],
   1462                                        mixed_time_pair, data_bands);
   1463       }
   1464       if (diff_type[1] == DIFF_FREQ) {
   1465         ixheaacd_diff_freq_decode(data_diff[1], data_pair[1], data_bands);
   1466       } else {
   1467         ixheaacd_mps_diff_time_dec_bwd(data_pair[0], data_diff[1], data_pair[1],
   1468                                        mixed_time_pair, data_bands);
   1469       }
   1470     } else {
   1471       ixheaacd_diff_freq_decode(data_diff[1], data_pair[1], data_bands);
   1472 
   1473       if (diff_type[0] == DIFF_FREQ) {
   1474         ixheaacd_diff_freq_decode(data_diff[0], data_pair[0], data_bands);
   1475       } else {
   1476         ixheaacd_mps_diff_time_dec_fwd(data_pair[1], data_diff[0], data_pair[0],
   1477                                        mixed_time_pair, data_bands);
   1478       }
   1479     }
   1480 
   1481     ixheaacd_attach_lsb(it_bit_buff, data_pair[0], quant_offset,
   1482                         attach_lsb_flag ? 1 : 0, data_bands, data_pair[0]);
   1483     if (pair_flag) {
   1484       ixheaacd_attach_lsb(it_bit_buff, data_pair[1], quant_offset,
   1485                           attach_lsb_flag ? 1 : 0, data_bands, data_pair[1]);
   1486     }
   1487   }
   1488 
   1489   memcpy(outdata[set_idx] + band_start, data_pair[0],
   1490          sizeof(WORD32) * data_bands);
   1491   if (pair_flag) {
   1492     memcpy(outdata[set_idx + 1] + band_start, data_pair[1],
   1493            sizeof(WORD32) * data_bands);
   1494   }
   1495 
   1496   return 1;
   1497 }
   1498 
   1499 WORD32 ixheaacd_mps_huff_decode(ia_handle_bit_buf_struct it_bit_buff,
   1500                                 WORD32* out_data, WORD32 num_val) {
   1501   WORD32 val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
   1502   WORD32 rl_data[2] = {0};
   1503 
   1504   while (val_rcvd < num_val) {
   1505     if (!ixheaacd_mps_huff_read_2d(
   1506             it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_reshape_nodes,
   1507             rl_data, &dummy))
   1508       return 0;
   1509     val = rl_data[0];
   1510     len = rl_data[1] + 1;
   1511     for (i = val_rcvd; i < val_rcvd + len; i++) {
   1512       out_data[i] = val;
   1513     }
   1514     val_rcvd += len;
   1515   }
   1516 
   1517   return 1;
   1518 }
   1519