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