• 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 <math.h>
21 #include <string.h>
22 #include "ixheaacd_type_def.h"
23 #include "ixheaacd_constants.h"
24 #include "ixheaacd_basic_ops32.h"
25 #include "ixheaacd_basic_ops40.h"
26 #include "ixheaacd_bitbuffer.h"
27 #include "ixheaacd_defines.h"
28 #include "ixheaacd_sbr_const.h"
29 #include "ixheaacd_memory_standards.h"
30 #include "ixheaacd_sbrdecsettings.h"
31 #include "ixheaacd_env_extr_part.h"
32 #include "ixheaacd_aac_rom.h"
33 #include "ixheaacd_common_rom.h"
34 #include "ixheaacd_sbr_rom.h"
35 #include "ixheaacd_pulsedata.h"
36 #include "ixheaacd_pns.h"
37 #include "ixheaacd_sbr_common.h"
38 #include "ixheaacd_drc_data_struct.h"
39 #include "ixheaacd_cnst.h"
40 #include "ixheaacd_ec_defines.h"
41 #include "ixheaacd_ec_struct_def.h"
42 #include "ixheaacd_error_codes.h"
43 #include "ixheaacd_channelinfo.h"
44 #include "ixheaacd_sbrdecoder.h"
45 #include "ixheaacd_audioobjtypes.h"
46 #include "ixheaacd_latmdemux.h"
47 #include "ixheaacd_aacdec.h"
48 #include "ixheaacd_hybrid.h"
49 #include "ixheaacd_sbr_scale.h"
50 #include "ixheaacd_ps_dec.h"
51 #include "ixheaacd_lpp_tran.h"
52 #include "ixheaacd_env_extr.h"
53 #include "ixheaacd_qmf_dec.h"
54 #include "ixheaacd_env_calc.h"
55 #include "ixheaacd_pvc_dec.h"
56 #include "ixheaacd_sbr_dec.h"
57 #include "ixheaacd_mps_polyphase.h"
58 #include "ixheaacd_config.h"
59 #include "ixheaacd_mps_macro_def.h"
60 #include "ixheaacd_mps_struct_def.h"
61 #include "ixheaacd_mps_res_rom.h"
62 #include "ixheaacd_mps_aac_struct.h"
63 #include "ixheaacd_mps_dec.h"
64 #include "ixheaacd_mps_interface.h"
65 #include "ixheaacd_struct_def.h"
66 #include "ixheaacd_mps_process.h"
67 #include "ixheaacd_mps_bitdec.h"
68 #include "ixheaacd_mps_smoothing.h"
69 #include "ixheaacd_mps_tp_process.h"
70 #include "ixheaacd_mps_reshape_bb_env.h"
71 #include "ixheaacd_mps_blind.h"
72 #include "ixheaacd_mps_m1m2.h"
73 #include "ixheaacd_mps_basic_op.h"
74 #include "ixheaacd_mps_decor.h"
75 #include "ixheaacd_mps_hybfilter.h"
76 #include "ixheaacd_mps_nlc_dec.h"
77 #include "ixheaacd_mps_huff_tab.h"
78 
79 extern const ia_huff_pt0_nodes_struct ixheaacd_huff_part0_nodes;
80 extern const ia_huff_ipd_nodes_struct ixheaacd_huff_ipd_nodes;
81 extern const ia_huff_lav_nodes_struct ixheaacd_huff_lav_idx_nodes;
82 extern const ia_huff_pt0_nodes_struct ixheaacd_huff_pilot_nodes;
83 extern const ia_huff_cld_nodes_struct ixheaacd_huff_cld_nodes;
84 extern const ia_huff_icc_nodes_struct ixheaacd_huff_icc_nodes;
85 extern const ia_huff_cpc_nodes_struct ixheaacd_huff_cpc_nodes;
86 extern const ia_huff_res_nodes_struct ixheaacd_huff_reshape_nodes;
87 
ixheaacd_mps_create(ia_mps_dec_state_struct * self,WORD32 bs_frame_len,WORD32 residual_coding,ia_usac_dec_mps_config_struct * mps212_config)88 WORD32 ixheaacd_mps_create(ia_mps_dec_state_struct* self, WORD32 bs_frame_len,
89                            WORD32 residual_coding,
90                            ia_usac_dec_mps_config_struct* mps212_config) {
91   WORD32 num_ch;
92   WORD32 err_code = 0;
93 
94   ia_mps_bs_frame bs_frame;
95 
96   self->num_parameter_sets = 1;
97   self->qmf_band_count = 64;
98 
99   self->res_ch_count = 0;
100 
101   if (mps212_config) {
102     self->config = mps212_config;
103     self->frame_length = bs_frame_len;
104     self->in_ch_count = 1;
105     self->out_ch_count = 2;
106     self->residual_coding = residual_coding;
107     if (self->residual_coding) {
108       self->bs_residual_present = 1;
109       self->bs_residual_bands = mps212_config->bs_residual_bands;
110       if (self->config->bs_phase_coding) {
111         self->config->bs_phase_coding = 2;
112       }
113     }
114   }
115 
116   err_code = ixheaacd_mps_header_decode(self);
117 
118   if (err_code != IA_NO_ERROR) {
119     self->mps_init_done = 0;
120     return err_code;
121   }
122 
123   if ((self->residual_coding) && (self->res_bands > 0)) self->res_ch_count++;
124 
125   ixheaacd_mps_env_init(self);
126 
127   self->resolution = self->qmf_band_count;
128 
129   for (num_ch = 0; num_ch < self->out_ch_count; num_ch++) {
130     ixheaacd_mps_synt_init(self->qmf_filt_state[num_ch]);
131   }
132 
133   ixheaacd_mps_qmf_hybrid_analysis_init(&self->hyb_filt_state[0]);
134 
135   if ((self->residual_coding) && (self->res_bands > 0))
136     ixheaacd_mps_qmf_hybrid_analysis_init(&self->hyb_filt_state[1]);
137 
138   err_code = ixheaacd_mps_decor_init(&(self->mps_decor), self->hyb_band_count_max,
139                                      self->config->bs_decorr_config,
140                                      self->object_type);
141 
142   if (err_code != IA_NO_ERROR) {
143     self->mps_init_done = 0;
144     return err_code;
145   }
146 
147   ixheaacd_mps_init_pre_and_post_matrix(self);
148 
149   self->parse_nxt_frame = 1;
150 
151   bs_frame = self->bs_frame;
152   memset(bs_frame.cld_idx_pre, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
153   memset(bs_frame.icc_idx_pre, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
154   memset(bs_frame.cmp_cld_idx_prev, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
155   memset(bs_frame.cmp_icc_idx_prev, 0, MAX_PARAMETER_BANDS * sizeof(WORD32));
156 
157   self->subband_var.init_flag = 0;
158   self->subband_var.update_old_ener = 0;
159   self->subband_var.nrg_dir = 0;
160   memset(self->subband_var.nrg_diff, 0, 2 * sizeof(FLOAT32));
161 
162   memset(self->opd_smooth.smooth_l_phase, 0,
163          MAX_PARAMETER_BANDS * sizeof(WORD32));
164   memset(self->opd_smooth.smooth_r_phase, 0,
165          MAX_PARAMETER_BANDS * sizeof(WORD32));
166   self->mps_init_done = 1;
167 
168   return 0;
169 }
170 
171 static const FLOAT32 ixheaacd_tsd_mul_re[] = {
172     1.0f,  0.707106781186548f,  0.0f, -0.707106781186548f,
173     -1.0f, -0.707106781186548f, 0.0f, 0.707106781186548f};
174 
175 static const FLOAT32 ixheaacd_tsd_mul_im[] = {
176     0.0f, 0.707106781186548f,  1.0f,  0.707106781186548f,
177     0.0f, -0.707106781186548f, -1.0f, -0.707106781186548f};
178 
ixheaacd_mps_qmf_hyb_analysis(ia_mps_dec_state_struct * self)179 VOID ixheaacd_mps_qmf_hyb_analysis(ia_mps_dec_state_struct* self) {
180   if (self->object_type == AOT_ER_AAC_ELD ||
181       self->object_type == AOT_ER_AAC_LD) {
182     WORD32 k, n;
183 
184     for (n = 0; n < self->time_slots; n++) {
185       for (k = 0; k < self->qmf_band_count; k++) {
186         self->hyb_in[0][k][n].re = self->qmf_in[0][n][k].re;
187         self->hyb_in[0][k][n].im = self->qmf_in[0][n][k].im;
188       }
189     }
190   } else {
191       ixheaacd_mps_qmf_hybrid_analysis(&self->hyb_filt_state[0], self->qmf_in[0],
192                                    self->qmf_band_count, self->time_slots,
193                                    self->hyb_in[0]);
194   }
195 
196   if ((self->residual_coding) && (self->res_bands > 0)) {
197     ixheaacd_mps_qmf_hybrid_analysis(&self->hyb_filt_state[self->in_ch_count],
198                                      self->qmf_in[1], self->band_count[1],
199                                      self->time_slots, self->hyb_res);
200   }
201 }
202 
ixheaacd_mps_qmf_hyb_synthesis(ia_mps_dec_state_struct * self)203 VOID ixheaacd_mps_qmf_hyb_synthesis(ia_mps_dec_state_struct* self) {
204   WORD32 ch;
205 
206   if (self->object_type == AOT_ER_AAC_ELD ||
207       self->object_type == AOT_ER_AAC_LD) {
208     WORD32 k, n;
209     for (ch = 0; ch < self->out_ch_count; ch++) {
210       for (n = 0; n < self->time_slots; n++) {
211         for (k = 0; k < self->qmf_band_count; k++) {
212           self->qmf_out_dir[ch][n][k].re = self->hyb_dir_out[ch][n][k].re;
213           self->qmf_out_dir[ch][n][k].im = self->hyb_dir_out[ch][n][k].im;
214         }
215       }
216     }
217   } else {
218       for (ch = 0; ch < self->out_ch_count; ch++) {
219           ixheaacd_mps_qmf_hybrid_synthesis(self->hyb_dir_out[ch],
220                                       self->qmf_band_count, self->time_slots,
221                                       self->qmf_out_dir[ch]);
222       }
223   }
224 }
225 
ixheaacd_mps_decor(ia_mps_dec_state_struct * self)226 VOID ixheaacd_mps_decor(ia_mps_dec_state_struct* self) {
227   WORD32 k, sb_sample, idx;
228 
229   ia_cmplx_flt_struct(*scratch)[MAX_HYBRID_BANDS_MPS];
230 
231   ia_cmplx_flt_struct coeff;
232   WORD32 band_start = 7;
233 
234   scratch = self->scratch;
235 
236   for (k = self->dir_sig_count; k < self->dir_sig_count + self->decor_sig_count;
237        k++) {
238     if (self->bs_tsd_enable) {
239       for (sb_sample = 0; sb_sample < self->time_slots; sb_sample++) {
240         if (self->bs_tsd_sep_data[sb_sample]) {
241           for (idx = band_start; idx < self->mps_decor.num_bins; idx++) {
242             scratch[sb_sample][idx].re = self->v[k][sb_sample][idx].re;
243             scratch[sb_sample][idx].im = self->v[k][sb_sample][idx].im;
244             self->v[k][sb_sample][idx].re = 0.0f;
245             self->v[k][sb_sample][idx].im = 0.0f;
246           }
247         }
248       }
249     }
250 
251     ixheaacd_mps_decor_apply(&self->mps_decor, self->v[k], self->w_diff[k],
252                              self->time_slots, NO_RES_BANDS,
253                              self->ldmps_config.ldmps_present_flag);
254 
255     if (self->bs_tsd_enable) {
256       for (sb_sample = 0; sb_sample < self->time_slots; sb_sample++) {
257         if (self->bs_tsd_sep_data[sb_sample]) {
258           coeff.re = ixheaacd_tsd_mul_re[self->bs_tsd_tr_phase_data[sb_sample]];
259           coeff.im = ixheaacd_tsd_mul_im[self->bs_tsd_tr_phase_data[sb_sample]];
260 
261           for (idx = band_start; idx < self->mps_decor.num_bins; idx++) {
262             self->w_diff[k][sb_sample][idx].re +=
263                 coeff.re * scratch[sb_sample][idx].re -
264                 coeff.im * scratch[sb_sample][idx].im;
265             self->w_diff[k][sb_sample][idx].im +=
266                 coeff.im * scratch[sb_sample][idx].re +
267                 coeff.re * scratch[sb_sample][idx].im;
268           }
269         }
270       }
271     }
272   }
273 }
274 
ixheaacd_mps_mix_res_decor(ia_mps_dec_state_struct * self)275 VOID ixheaacd_mps_mix_res_decor(ia_mps_dec_state_struct* self) {
276   WORD32 ts, qs, row, indx;
277 
278   for (ts = 0; ts < self->time_slots; ts++) {
279     for (qs = 0; qs < self->hyb_band_count_max; qs++) {
280       indx = self->hyb_band_to_processing_band_table[qs];
281 
282       for (row = 0; row < self->dir_sig_count; row++) {
283         self->w_dir[row][ts][qs].re = self->v[row][ts][qs].re;
284         self->w_dir[row][ts][qs].im = self->v[row][ts][qs].im;
285       }
286 
287       for (row = self->dir_sig_count;
288            row < (self->dir_sig_count + self->decor_sig_count); row++) {
289         if (indx < self->res_bands) {
290           self->w_dir[row][ts][qs].re = self->hyb_res[qs][ts].re;
291           self->w_dir[row][ts][qs].im = self->hyb_res[qs][ts].im;
292         } else {
293           self->w_dir[row][ts][qs].re = 0.0f;
294           self->w_dir[row][ts][qs].im = 0.0f;
295         }
296       }
297 
298       for (row = 0; row < self->dir_sig_count; row++) {
299         self->w_diff[row][ts][qs].re = 0.0f;
300         self->w_diff[row][ts][qs].im = 0.0f;
301       }
302 
303       for (row = self->dir_sig_count;
304            row < (self->dir_sig_count + self->decor_sig_count); row++) {
305         if (indx < self->res_bands) {
306           self->w_diff[row][ts][qs].re = 0.0f;
307           self->w_diff[row][ts][qs].im = 0.0f;
308         }
309       }
310     }
311   }
312 }
313 
ixheaacd_mps_mix_res_decor_residual_band(ia_mps_dec_state_struct * self)314 VOID ixheaacd_mps_mix_res_decor_residual_band(ia_mps_dec_state_struct* self) {
315   WORD32 ts, qs, indx;
316   for (qs = 0; qs < self->hyb_band_count_max; qs++) {
317     indx = self->hyb_band_to_processing_band_table[qs];
318     if (indx >= self->res_bands) {
319       if (qs < self->hyb_band_count[1]) {
320         for (ts = 0; ts < self->time_slots; ts++) {
321           self->w_dir[1][ts][qs].re = 0.0f;
322           self->w_dir[1][ts][qs].im = 0.0f;
323         }
324       }
325     } else {
326       for (ts = 0; ts < self->time_slots; ts++) {
327         self->w_diff[1][ts][qs].re = 0.0f;
328         self->w_diff[1][ts][qs].im = 0.0f;
329       }
330     }
331   }
332 }
333 
ixheaacd_mps_create_w(ia_mps_dec_state_struct * self)334 VOID ixheaacd_mps_create_w(ia_mps_dec_state_struct* self) {
335   ixheaacd_mps_decor(self);
336   ixheaacd_mps_mix_res_decor(self);
337 }
338 
ixheaacd_mps_qmf_hyb_analysis_no_pre_mix(ia_mps_dec_state_struct * self)339 VOID ixheaacd_mps_qmf_hyb_analysis_no_pre_mix(ia_mps_dec_state_struct* self) {
340   ixheaacd_mps_qmf_hybrid_analysis_no_pre_mix(
341       &self->hyb_filt_state[0], self->qmf_in[0], self->band_count[0],
342       self->time_slots, self->w_dir[0]);
343 
344   if (self->res_bands) {
345     ixheaacd_mps_qmf_hybrid_analysis_no_pre_mix(
346         &self->hyb_filt_state[1], self->qmf_in[1], self->band_count[1],
347         self->time_slots, self->w_dir[1]);
348 
349     if (self->res_bands != 28) {
350       ixheaacd_mps_decor_apply(&self->mps_decor, self->w_dir[0],
351                                self->w_diff[1], self->time_slots,
352                                self->res_bands,
353                                self->ldmps_config.ldmps_present_flag);
354 
355       ixheaacd_mps_mix_res_decor_residual_band(self);
356     }
357   } else {
358     ixheaacd_mps_decor_apply(&self->mps_decor, self->w_dir[0], self->w_diff[1],
359                              self->time_slots, NO_RES_BANDS,
360                              self->ldmps_config.ldmps_present_flag);
361   }
362 }
363 
ixheaacd_mps_apply(ia_mps_dec_state_struct * self,FLOAT32 ** input_buffer[4],FLOAT32 (* output_buffer)[4096])364 WORD32 ixheaacd_mps_apply(ia_mps_dec_state_struct* self,
365                           FLOAT32** input_buffer[4],
366                           FLOAT32 (*output_buffer)[4096]) {
367   WORD32 ch, ts, qs;
368   WORD32 time_slots = self->time_slots;
369   WORD32 in_ch_count = self->in_ch_count + self->res_ch_count;
370   WORD32 err = 0;
371   self->hyb_band_count[0] = self->band_count[0] - QMF_BANDS_TO_HYBRID + 10;
372   self->hyb_band_count[1] = self->band_count[1] - QMF_BANDS_TO_HYBRID + 10;
373   self->hyb_band_count_max =
374       max(self->hyb_band_count[0], self->hyb_band_count[1]);
375   self->mps_decor.decor_nrg_smooth.num_bins = self->hyb_band_count_max;
376   self->mps_decor.num_bins = self->hyb_band_count_max;
377   self->output_buffer = output_buffer;
378 
379   err = ixheaacd_mps_frame_decode(self);
380 
381   if (err != IA_NO_ERROR) return err;
382 
383   ixheaacd_pre_and_mix_matrix_calculation(self);
384 
385   ixheaacd_mps_pre_matrix_mix_matrix_smoothing(self);
386 
387   for (ch = 0; ch < in_ch_count; ch++) {
388     for (ts = 0; ts < time_slots; ts++) {
389       for (qs = 0; qs < self->band_count[ch]; qs++) {
390         self->qmf_in[ch][qs][ts].re =
391             self->input_gain * input_buffer[2 * ch][ts][qs];
392         self->qmf_in[ch][qs][ts].im =
393             self->input_gain * input_buffer[2 * ch + 1][ts][qs];
394       }
395     }
396   }
397 
398   if (!(self->pre_mix_req | self->bs_tsd_enable)) {
399     ixheaacd_mps_qmf_hyb_analysis_no_pre_mix(self);
400   } else {
401     ixheaacd_mps_qmf_hyb_analysis(self);
402 
403     ixheaacd_mps_apply_pre_matrix(self);
404 
405     ixheaacd_mps_create_w(self);
406   }
407 
408   if ((!(self->res_bands | self->pre_mix_req)) &&
409       (self->config->bs_phase_coding == 0)) {
410     ixheaacd_mps_apply_mix_matrix_type1(self);
411 
412   } else if (self->pre_mix_req) {
413     ixheaacd_mps_apply_mix_matrix_type2(self);
414 
415   } else {
416     ixheaacd_mps_apply_mix_matrix_type3(self);
417   }
418 
419   if (self->config->bs_temp_shape_config == 2) {
420     ixheaacd_mps_time_env_shaping(self);
421   }
422 
423   err = ixheaacd_mps_temp_process(self);
424   if (err) return err;
425 
426   self->parse_nxt_frame = 1;
427   self->pre_mix_req = 0;
428   return 0;
429 }
430 
ixheaacd_mps_pcm_decode(ia_bit_buf_struct * it_bit_buff,WORD32 * out_data_1,WORD32 * out_data_2,WORD32 ixheaacd_drc_offset,WORD32 num_val,WORD32 num_levels)431 static VOID ixheaacd_mps_pcm_decode(ia_bit_buf_struct *it_bit_buff,
432                                     WORD32* out_data_1, WORD32* out_data_2,
433                                     WORD32 ixheaacd_drc_offset, WORD32 num_val,
434                                     WORD32 num_levels) {
435   WORD32 i = 0, j = 0, idx = 0;
436   WORD32 max_grp_len = 0, grp_len = 0, next_val = 0, grp_val = 0;
437   UWORD32 data = 0;
438 
439   FLOAT32 ld_nlev = 0.f;
440 
441   WORD32 pcm_chunk_size[7] = {0};
442 
443   switch (num_levels) {
444     case 3:
445       max_grp_len = 5;
446       break;
447     case 7:
448       max_grp_len = 6;
449       break;
450     case 11:
451       max_grp_len = 2;
452       break;
453     case 13:
454       max_grp_len = 4;
455       break;
456     case 19:
457       max_grp_len = 4;
458       break;
459     case 25:
460       max_grp_len = 3;
461       break;
462     case 51:
463       max_grp_len = 4;
464       break;
465     case 4:
466     case 8:
467     case 15:
468     case 16:
469     case 26:
470     case 31:
471       max_grp_len = 1;
472       break;
473     default:
474       return;
475   }
476 
477   ld_nlev = (FLOAT32)(log((FLOAT32)num_levels) / log(2.f));
478 
479   for (i = 1; i <= max_grp_len; i++) {
480     pcm_chunk_size[i] = (WORD32)ceil((FLOAT32)(i)*ld_nlev);
481   }
482 
483   for (i = 0; i < num_val; i += max_grp_len) {
484     grp_len = min(max_grp_len, num_val - i);
485     data = ixheaacd_read_bits_buf(it_bit_buff, pcm_chunk_size[grp_len]);
486 
487     grp_val = data;
488 
489     for (j = 0; j < grp_len; j++) {
490       idx = i + (grp_len - j - 1);
491       next_val = grp_val % num_levels;
492 
493       if (out_data_2 == NULL) {
494         out_data_1[idx] = next_val - ixheaacd_drc_offset;
495       } else if (out_data_1 == NULL) {
496         out_data_2[idx] = next_val - ixheaacd_drc_offset;
497       } else {
498         if (idx % 2) {
499           out_data_2[idx / 2] = next_val - ixheaacd_drc_offset;
500         } else {
501           out_data_1[idx / 2] = next_val - ixheaacd_drc_offset;
502         }
503       }
504 
505       grp_val = (grp_val - next_val) / num_levels;
506     }
507   }
508 
509   return;
510 }
511 
ixheaacd_mps_huff_read(ia_bit_buf_struct * it_bit_buff,const WORD32 (* node_tab)[][2],WORD32 * out_data)512 static VOID ixheaacd_mps_huff_read(ia_bit_buf_struct *it_bit_buff,
513                                    const WORD32 (*node_tab)[][2],
514                                    WORD32* out_data) {
515   WORD32 node = 0;
516   UWORD32 next_bit = 0;
517 
518   do {
519     next_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
520     node = (*node_tab)[node][next_bit];
521   } while (node > 0);
522 
523   *out_data = node;
524 
525   return;
526 }
527 
ixheaacd_mps_huff_read_2d(ia_bit_buf_struct * it_bit_buff,const WORD32 (* node_tab)[][2],WORD32 out_data[2],WORD32 * escape)528 static VOID ixheaacd_mps_huff_read_2d(ia_bit_buf_struct *it_bit_buff,
529                                       const WORD32 (*node_tab)[][2],
530                                       WORD32 out_data[2], WORD32* escape)
531 
532 {
533   WORD32 huff_2d_8bit = 0;
534   WORD32 node = 0;
535 
536   ixheaacd_mps_huff_read(it_bit_buff, node_tab, &node);
537   *escape = (node == 0);
538 
539   if (*escape) {
540     out_data[0] = 0;
541     out_data[1] = 1;
542   } else {
543     huff_2d_8bit = -(node + 1);
544     out_data[0] = huff_2d_8bit >> 4;
545     out_data[1] = huff_2d_8bit & 0xf;
546   }
547 
548   return;
549 }
550 
ixheaacd_mps_sym_restore(ia_bit_buf_struct * it_bit_buff,WORD32 lav,WORD32 data[2])551 static VOID ixheaacd_mps_sym_restore(ia_bit_buf_struct *it_bit_buff,
552                                      WORD32 lav, WORD32 data[2]) {
553   WORD32 tmp = 0;
554   UWORD32 sym_bit = 0;
555 
556   WORD32 sum_val = data[0] + data[1];
557   WORD32 diff_val = data[0] - data[1];
558 
559   if (sum_val > lav) {
560     data[0] = -sum_val + (2 * lav + 1);
561     data[1] = -diff_val;
562   } else {
563     data[0] = sum_val;
564     data[1] = diff_val;
565   }
566 
567   if (data[0] + data[1] != 0) {
568     sym_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
569     if (sym_bit) {
570       data[0] = -data[0];
571       data[1] = -data[1];
572     }
573   }
574 
575   if (data[0] - data[1] != 0) {
576     sym_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
577     if (sym_bit) {
578       tmp = data[0];
579       data[0] = data[1];
580       data[1] = tmp;
581     }
582   }
583 
584   return;
585 }
586 
ixheaacd_mps_sym_restoreipd(ia_bit_buf_struct * it_bit_buff,WORD32 lav,WORD32 data[2])587 static VOID ixheaacd_mps_sym_restoreipd(ia_bit_buf_struct *it_bit_buff,
588                                         WORD32 lav, WORD32 data[2]) {
589   WORD32 tmp = 0;
590   UWORD32 sym_bit = 0;
591 
592   WORD32 sum_val = data[0] + data[1];
593   WORD32 diff_val = data[0] - data[1];
594 
595   if (sum_val > lav) {
596     data[0] = -sum_val + (2 * lav + 1);
597     data[1] = -diff_val;
598   } else {
599     data[0] = sum_val;
600     data[1] = diff_val;
601   }
602 
603   if (data[0] - data[1] != 0) {
604     sym_bit = ixheaacd_read_bits_buf(it_bit_buff, 1);
605     if (sym_bit) {
606       tmp = data[0];
607       data[0] = data[1];
608       data[1] = tmp;
609     }
610   }
611 
612   return;
613 }
614 
ixheaacd_mps_huff_dec_pilot(ia_bit_buf_struct * it_bit_buff,const WORD32 (* node_tab)[][2],WORD32 * pilot_data)615 static VOID ixheaacd_mps_huff_dec_pilot(ia_bit_buf_struct *it_bit_buff,
616                                         const WORD32 (*node_tab)[][2],
617                                         WORD32* pilot_data) {
618   WORD32 node = 0;
619 
620   ixheaacd_mps_huff_read(it_bit_buff, node_tab, &node);
621   *pilot_data = -(node + 1);
622 
623   return;
624 }
625 
ixheaacd_mps_huff_dec_cld_1d(ia_bit_buf_struct * it_bit_buff,const ia_huff_cld_node_1d_struct * huff_nodes,WORD32 * out_data,WORD32 num_val,WORD32 p0_flag)626 static VOID ixheaacd_mps_huff_dec_cld_1d(
627     ia_bit_buf_struct *it_bit_buff,
628     const ia_huff_cld_node_1d_struct* huff_nodes, WORD32* out_data,
629     WORD32 num_val, WORD32 p0_flag) {
630   WORD32 i = 0, node = 0, ixheaacd_drc_offset = 0;
631   WORD32 od = 0, od_sign = 0;
632   UWORD32 data = 0;
633 
634   if (p0_flag) {
635     ixheaacd_mps_huff_read(it_bit_buff,
636                            (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.cld,
637                            &node);
638     out_data[0] = -(node + 1);
639     ixheaacd_drc_offset = 1;
640   }
641 
642   for (i = ixheaacd_drc_offset; i < num_val; i++) {
643     ixheaacd_mps_huff_read(it_bit_buff,
644                            (ia_huff_node_struct)&huff_nodes->node_tab, &node);
645     od = -(node + 1);
646 
647     if (od != 0) {
648       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
649       od_sign = data;
650 
651       if (od_sign) od = -od;
652     }
653 
654     out_data[i] = od;
655   }
656 
657   return;
658 }
659 
ixheaacd_mps_huff_dec_ipd_1d(ia_bit_buf_struct * it_bit_buff,const ia_huff_ipd_node_1d_struct * huff_nodes,WORD32 * out_data,WORD32 num_val,WORD32 p0_flag)660 static VOID ixheaacd_mps_huff_dec_ipd_1d(
661     ia_bit_buf_struct *it_bit_buff,
662     const ia_huff_ipd_node_1d_struct* huff_nodes, WORD32* out_data,
663     WORD32 num_val, WORD32 p0_flag) {
664   WORD32 i = 0, node = 0, ixheaacd_drc_offset = 0;
665   WORD32 od = 0;
666 
667   if (p0_flag) {
668     ixheaacd_mps_huff_read(
669         it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_ipd_nodes.hp0.node_tab,
670         &node);
671     out_data[0] = -(node + 1);
672     ixheaacd_drc_offset = 1;
673   }
674 
675   for (i = ixheaacd_drc_offset; i < num_val; i++) {
676     ixheaacd_mps_huff_read(it_bit_buff,
677                            (ia_huff_node_struct)&huff_nodes->node_tab, &node);
678     od = -(node + 1);
679     out_data[i] = od;
680   }
681 
682   return;
683 }
684 
ixheaacd_mps_huff_dec_icc_1d(ia_bit_buf_struct * it_bit_buff,const ia_huff_icc_node_1d_struct * huff_nodes,WORD32 * out_data,WORD32 num_val,WORD32 p0_flag)685 static VOID ixheaacd_mps_huff_dec_icc_1d(
686     ia_bit_buf_struct *it_bit_buff,
687     const ia_huff_icc_node_1d_struct* huff_nodes, WORD32* out_data,
688     WORD32 num_val, WORD32 p0_flag) {
689   WORD32 i = 0, node = 0, ixheaacd_drc_offset = 0;
690   WORD32 od = 0, od_sign = 0;
691   UWORD32 data = 0;
692 
693   if (p0_flag) {
694     ixheaacd_mps_huff_read(it_bit_buff,
695                            (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.icc,
696                            &node);
697     out_data[0] = -(node + 1);
698     ixheaacd_drc_offset = 1;
699   }
700 
701   for (i = ixheaacd_drc_offset; i < num_val; i++) {
702     ixheaacd_mps_huff_read(it_bit_buff,
703                            (ia_huff_node_struct)&huff_nodes->node_tab, &node);
704     od = -(node + 1);
705 
706     if (od != 0) {
707       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
708       od_sign = data;
709 
710       if (od_sign) od = -od;
711     }
712 
713     out_data[i] = od;
714   }
715 
716   return;
717 }
718 
ia_mps_dec_huff_dec_cpc_1d(const ia_huff_cpc_node_1d_struct * huff_nodes,WORD32 * out_data,WORD32 num_val,WORD32 p0_flag,ia_bit_buf_struct * h_bit_buf)719 static VOID ia_mps_dec_huff_dec_cpc_1d(
720     const ia_huff_cpc_node_1d_struct *huff_nodes, WORD32 *out_data,
721     WORD32 num_val, WORD32 p0_flag, ia_bit_buf_struct *h_bit_buf) {
722   WORD32 i = 0, node = 0, offset = 0;
723   WORD32 od = 0, od_sign = 0;
724   WORD32 data = 0;
725 
726   if (p0_flag) {
727     ixheaacd_mps_huff_read(
728         h_bit_buf, (ia_huff_node_struct) & (ixheaacd_huff_part0_nodes.cpc),
729         &node);
730     out_data[0] = -(node + 1);
731     offset = 1;
732   }
733 
734   for (i = offset; i < num_val; i++) {
735     ixheaacd_mps_huff_read(h_bit_buf,
736                            (ia_huff_node_struct)&huff_nodes->node_tab, &node);
737     od = -(node + 1);
738 
739     if (od != 0) {
740       data = ixheaacd_read_bits_buf(h_bit_buf, 1);
741       od_sign = data;
742 
743       if (od_sign) od = -od;
744     }
745 
746     out_data[i] = od;
747   }
748 }
749 
ixheaacd_mps_huff_dec_cld_2d(ia_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])750 static VOID ixheaacd_mps_huff_dec_cld_2d(
751     ia_bit_buf_struct *it_bit_buff,
752     const ia_huff_cld_node_2d_struct* huff_nodes, WORD32 out_data[][2],
753     WORD32 num_val, WORD32 ch_fac, WORD32* p0_data[2]) {
754   WORD32 i = 0, lav = 0, escape = 0, esc_contrl = 0;
755   WORD32 node = 0;
756   UWORD32 data = 0;
757 
758   WORD32 esc_data[MAXBANDS][2] = {{0}};
759   WORD32 esc_idx[MAXBANDS] = {0};
760 
761   ixheaacd_mps_huff_read(
762       it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_lav_idx_nodes.node_tab,
763       &node);
764   data = -(node + 1);
765 
766   lav = 2 * data + 3;
767 
768   if (p0_data[0] != NULL) {
769     ixheaacd_mps_huff_read(it_bit_buff,
770                            (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.cld,
771                            &node);
772     *p0_data[0] = -(node + 1);
773   }
774   if (p0_data[1] != NULL) {
775     ixheaacd_mps_huff_read(it_bit_buff,
776                            (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.cld,
777                            &node);
778     *p0_data[1] = -(node + 1);
779   }
780 
781   for (i = 0; i < num_val; i += ch_fac) {
782     switch (lav) {
783       case 3:
784         ixheaacd_mps_huff_read_2d(it_bit_buff,
785                                   (ia_huff_node_struct)&huff_nodes->lav3,
786                                   out_data[i], &escape);
787         break;
788       case 5:
789         ixheaacd_mps_huff_read_2d(it_bit_buff,
790                                   (ia_huff_node_struct)&huff_nodes->lav5,
791                                   out_data[i], &escape);
792         break;
793       case 7:
794         ixheaacd_mps_huff_read_2d(it_bit_buff,
795                                   (ia_huff_node_struct)&huff_nodes->lav7,
796                                   out_data[i], &escape);
797         break;
798       case 9:
799         ixheaacd_mps_huff_read_2d(it_bit_buff,
800                                   (ia_huff_node_struct)&huff_nodes->lav9,
801                                   out_data[i], &escape);
802         break;
803       default:
804         break;
805     }
806 
807     if (escape) {
808       esc_idx[esc_contrl++] = i;
809     } else {
810       ixheaacd_mps_sym_restore(it_bit_buff, lav, out_data[i]);
811     }
812   }
813 
814   if (esc_contrl > 0) {
815     ixheaacd_mps_pcm_decode(it_bit_buff, esc_data[0], esc_data[1], 0,
816                             2 * esc_contrl, (2 * lav + 1));
817 
818     for (i = 0; i < esc_contrl; i++) {
819       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
820       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
821     }
822   }
823 
824   return;
825 }
826 
ixheaacd_mps_huff_dec_icc_2d(ia_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])827 static VOID ixheaacd_mps_huff_dec_icc_2d(
828     ia_bit_buf_struct *it_bit_buff,
829     const ia_huff_icc_node_2d_struct* huff_nodes, WORD32 out_data[][2],
830     WORD32 num_val, WORD32 ch_fac, WORD32* p0_data[2]) {
831   WORD32 i = 0, lav = 0, escape = 0, esc_contrl = 0;
832   WORD32 node = 0;
833   UWORD32 data = 0;
834 
835   WORD32 esc_data[2][MAXBANDS] = {{0}};
836   WORD32 esc_idx[MAXBANDS] = {0};
837 
838   ixheaacd_mps_huff_read(
839       it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_lav_idx_nodes.node_tab,
840       &node);
841   data = -(node + 1);
842 
843   lav = 2 * data + 1;
844 
845   if (p0_data[0] != NULL) {
846     ixheaacd_mps_huff_read(it_bit_buff,
847                            (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.icc,
848                            &node);
849     *p0_data[0] = -(node + 1);
850   }
851   if (p0_data[1] != NULL) {
852     ixheaacd_mps_huff_read(it_bit_buff,
853                            (ia_huff_node_struct)&ixheaacd_huff_part0_nodes.icc,
854                            &node);
855     *p0_data[1] = -(node + 1);
856   }
857 
858   for (i = 0; i < num_val; i += ch_fac) {
859     switch (lav) {
860       case 1:
861         ixheaacd_mps_huff_read_2d(it_bit_buff,
862                                   (ia_huff_node_struct)&huff_nodes->lav1,
863                                   out_data[i], &escape);
864         break;
865       case 3:
866         ixheaacd_mps_huff_read_2d(it_bit_buff,
867                                   (ia_huff_node_struct)&huff_nodes->lav3,
868                                   out_data[i], &escape);
869         break;
870       case 5:
871         ixheaacd_mps_huff_read_2d(it_bit_buff,
872                                   (ia_huff_node_struct)&huff_nodes->lav5,
873                                   out_data[i], &escape);
874         break;
875       case 7:
876         ixheaacd_mps_huff_read_2d(it_bit_buff,
877                                   (ia_huff_node_struct)&huff_nodes->lav7,
878                                   out_data[i], &escape);
879         break;
880     }
881 
882     if (escape) {
883       esc_idx[esc_contrl++] = i;
884     } else {
885       ixheaacd_mps_sym_restore(it_bit_buff, lav, out_data[i]);
886     }
887   }
888 
889   if (esc_contrl > 0) {
890     ixheaacd_mps_pcm_decode(it_bit_buff, esc_data[0], esc_data[1], 0,
891                             2 * esc_contrl, (2 * lav + 1));
892 
893     for (i = 0; i < esc_contrl; i++) {
894       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
895       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
896     }
897   }
898 
899   return;
900 }
901 
ixheaacd_mps_huff_dec_ipd_2d(ia_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])902 static VOID ixheaacd_mps_huff_dec_ipd_2d(
903     ia_bit_buf_struct *it_bit_buff,
904     const ia_huff_ipd_node_2d_struct* huff_nodes, WORD32 out_data[][2],
905     WORD32 num_val, WORD32 ch_fac, WORD32* p0_data[2]) {
906   WORD32 i = 0, lav = 0, escape = 0, esc_contrl = 0;
907   WORD32 node = 0;
908   UWORD32 data = 0;
909 
910   WORD32 esc_data[2][MAXBANDS] = {{0}};
911   WORD32 esc_idx[MAXBANDS] = {0};
912 
913   ixheaacd_mps_huff_read(
914       it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_lav_idx_nodes.node_tab,
915       &node);
916 
917   data = -(node + 1);
918   if (data == 0)
919     data = 3;
920   else
921     data--;
922 
923   lav = 2 * data + 1;
924 
925   if (p0_data[0] != NULL) {
926     ixheaacd_mps_huff_read(
927         it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_ipd_nodes.hp0.node_tab,
928         &node);
929     *p0_data[0] = -(node + 1);
930   }
931   if (p0_data[1] != NULL) {
932     ixheaacd_mps_huff_read(
933         it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_ipd_nodes.hp0.node_tab,
934         &node);
935     *p0_data[1] = -(node + 1);
936   }
937 
938   for (i = 0; i < num_val; i += ch_fac) {
939     switch (lav) {
940       case 1:
941         ixheaacd_mps_huff_read_2d(it_bit_buff,
942                                   (ia_huff_node_struct)&huff_nodes->lav1,
943                                   out_data[i], &escape);
944         break;
945       case 3:
946         ixheaacd_mps_huff_read_2d(it_bit_buff,
947                                   (ia_huff_node_struct)&huff_nodes->lav3,
948                                   out_data[i], &escape);
949         break;
950       case 5:
951         ixheaacd_mps_huff_read_2d(it_bit_buff,
952                                   (ia_huff_node_struct)&huff_nodes->lav5,
953                                   out_data[i], &escape);
954         break;
955       case 7:
956         ixheaacd_mps_huff_read_2d(it_bit_buff,
957                                   (ia_huff_node_struct)&huff_nodes->lav7,
958                                   out_data[i], &escape);
959         break;
960     }
961 
962     if (escape) {
963       esc_idx[esc_contrl++] = i;
964     } else {
965       ixheaacd_mps_sym_restoreipd(it_bit_buff, lav, out_data[i]);
966     }
967   }
968 
969   if (esc_contrl > 0) {
970     ixheaacd_mps_pcm_decode(it_bit_buff, esc_data[0], esc_data[1], 0,
971                             2 * esc_contrl, (2 * lav + 1));
972 
973     for (i = 0; i < esc_contrl; i++) {
974       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
975       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
976     }
977   }
978 
979   return;
980 }
981 
ia_mps_dec_huff_dec_cpc_2d(const ia_mps_dec_huff_cpc_nod_2d * huff_nodes,WORD32 out_data[][2],WORD32 num_val,WORD32 stride,WORD32 * p0_data[2],ia_bit_buf_struct * h_bit_buf)982 static VOID ia_mps_dec_huff_dec_cpc_2d(
983     const ia_mps_dec_huff_cpc_nod_2d *huff_nodes, WORD32 out_data[][2],
984     WORD32 num_val, WORD32 stride, WORD32 *p0_data[2],
985     ia_bit_buf_struct *h_bit_buf) {
986   WORD32 i = 0, lav = 0, escape = 0, esc_cntr = 0;
987   WORD32 node = 0;
988   WORD32 data = 0;
989 
990   WORD32 esc_data[2][MAXBANDS] = {{0}};
991   WORD32 esc_idx[MAXBANDS] = {0};
992 
993   ixheaacd_mps_huff_read(
994       h_bit_buf, (ia_huff_node_struct) & (ixheaacd_huff_lav_idx_nodes.node_tab),
995       &node);
996   data = -(node + 1);
997 
998   lav = 3 * data + 3;
999 
1000   if (p0_data[0] != NULL) {
1001     ixheaacd_mps_huff_read(
1002         h_bit_buf, (ia_huff_node_struct) & (ixheaacd_huff_part0_nodes.cpc),
1003         &node);
1004     *p0_data[0] = -(node + 1);
1005   }
1006   if (p0_data[1] != NULL) {
1007     ixheaacd_mps_huff_read(
1008         h_bit_buf, (ia_huff_node_struct) & (ixheaacd_huff_part0_nodes.cpc),
1009         &node);
1010     *p0_data[1] = -(node + 1);
1011   }
1012 
1013   for (i = 0; i < num_val; i += stride) {
1014     switch (lav) {
1015       case LAV_3:
1016         ixheaacd_mps_huff_read_2d(h_bit_buf,
1017                                   (ia_huff_node_struct)&huff_nodes->lav3,
1018                                   out_data[i], &escape);
1019         break;
1020       case LAV_6:
1021         ixheaacd_mps_huff_read_2d(h_bit_buf,
1022                                   (ia_huff_node_struct)&huff_nodes->lav6,
1023                                   out_data[i], &escape);
1024         break;
1025       case LAV_9:
1026         ixheaacd_mps_huff_read_2d(h_bit_buf,
1027                                   (ia_huff_node_struct)&huff_nodes->lav9,
1028                                   out_data[i], &escape);
1029         break;
1030       case LAV_12:
1031         ixheaacd_mps_huff_read_2d(h_bit_buf,
1032                                   (ia_huff_node_struct)&huff_nodes->lav12,
1033                                   out_data[i], &escape);
1034         break;
1035       default:
1036         break;
1037     }
1038 
1039     if (escape) {
1040       esc_idx[esc_cntr++] = i;
1041     } else {
1042       ixheaacd_mps_sym_restore(h_bit_buf, lav, out_data[i]);
1043     }
1044   }
1045 
1046   if (esc_cntr > 0) {
1047     ixheaacd_mps_pcm_decode(h_bit_buf, esc_data[0], esc_data[1], 0,
1048                             (esc_cntr << 1), ((lav << 1) + 1));
1049 
1050     for (i = 0; i < esc_cntr; i++) {
1051       out_data[esc_idx[i]][0] = esc_data[0][i] - lav;
1052       out_data[esc_idx[i]][1] = esc_data[1][i] - lav;
1053     }
1054   }
1055   return;
1056 }
1057 
ixheaacd_huff_decode(ia_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,WORD32 ld_mps_flag)1058 static VOID ixheaacd_huff_decode(ia_bit_buf_struct *it_bit_buff, WORD32 *out_data_1,
1059                                  WORD32 *out_data_2, WORD32 data_type, WORD32 diff_type_1,
1060                                  WORD32 diff_type_2, WORD32 pilot_coding_flag, WORD32 *pilot_data,
1061                                  WORD32 num_val, WORD32 *cdg_scheme, WORD32 ld_mps_flag) {
1062   WORD32 diff_type;
1063 
1064   WORD32 i = 0;
1065   UWORD32 data = 0;
1066 
1067   WORD32 pair_vec[MAXBANDS][2];
1068 
1069   WORD32* p0_data_1[2] = {NULL, NULL};
1070   WORD32* p0_data_2[2] = {NULL, NULL};
1071 
1072   WORD32 p0_flag[2];
1073 
1074   WORD32 num_val_1_int = num_val;
1075   WORD32 num_val_2_int = num_val;
1076 
1077   WORD32* out_data_1_int = out_data_1;
1078   WORD32* out_data_2_int = out_data_2;
1079 
1080   WORD32 df_rest_flag_1 = 0;
1081   WORD32 df_rest_flag_2 = 0;
1082 
1083   WORD32 huff_yy_1;
1084   WORD32 huff_yy_2;
1085   WORD32 huff_yy;
1086   if (pilot_coding_flag) {
1087     switch (data_type) {
1088       case CLD:
1089         if (out_data_1 != NULL) {
1090           ixheaacd_mps_huff_dec_pilot(
1091               it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_pilot_nodes.cld,
1092               pilot_data);
1093         }
1094         break;
1095 
1096       case ICC:
1097         if (out_data_1 != NULL) {
1098           ixheaacd_mps_huff_dec_pilot(
1099               it_bit_buff, (ia_huff_node_struct)&ixheaacd_huff_pilot_nodes.icc,
1100               pilot_data);
1101         }
1102         break;
1103 
1104       case CPC:
1105         if (out_data_1 != NULL) {
1106           ixheaacd_mps_huff_dec_pilot(
1107               it_bit_buff,
1108               (ia_huff_node_struct) & (ixheaacd_huff_pilot_nodes.cpc),
1109               pilot_data);
1110         }
1111         break;
1112 
1113       default:
1114         break;
1115     }
1116   }
1117 
1118   data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1119   *cdg_scheme = data << PAIR_SHIFT;
1120 
1121   if (*cdg_scheme >> PAIR_SHIFT == HUFF_2D) {
1122     if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ld_mps_flag != 1)) {
1123       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1124       *cdg_scheme |= data;
1125     } else {
1126       *cdg_scheme |= FREQ_PAIR;
1127     }
1128   }
1129 
1130   if (pilot_coding_flag) {
1131     huff_yy_1 = PCM_PLT;
1132     huff_yy_2 = PCM_PLT;
1133   } else {
1134     huff_yy_1 = diff_type_1;
1135     huff_yy_2 = diff_type_2;
1136   }
1137 
1138   switch (*cdg_scheme >> PAIR_SHIFT) {
1139     case HUFF_1D:
1140 
1141       p0_flag[0] = (diff_type_1 == DIFF_FREQ) && !pilot_coding_flag;
1142       p0_flag[1] = (diff_type_2 == DIFF_FREQ) && !pilot_coding_flag;
1143 
1144       switch (data_type) {
1145         case CLD:
1146           if (out_data_1 != NULL) {
1147             ixheaacd_mps_huff_dec_cld_1d(
1148                 it_bit_buff, &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_1],
1149                 out_data_1, num_val_1_int, p0_flag[0]);
1150           }
1151           if (out_data_2 != NULL) {
1152             ixheaacd_mps_huff_dec_cld_1d(
1153                 it_bit_buff, &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_2],
1154                 out_data_2, num_val_2_int, p0_flag[1]);
1155           }
1156 
1157           break;
1158 
1159         case ICC:
1160           if (out_data_1 != NULL) {
1161             ixheaacd_mps_huff_dec_icc_1d(
1162                 it_bit_buff, &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_1],
1163                 out_data_1, num_val_1_int, p0_flag[0]);
1164           }
1165           if (out_data_2 != NULL) {
1166             ixheaacd_mps_huff_dec_icc_1d(
1167                 it_bit_buff, &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_2],
1168                 out_data_2, num_val_2_int, p0_flag[1]);
1169           }
1170 
1171           break;
1172 
1173         case IPD:
1174           if (out_data_1 != NULL) {
1175             ixheaacd_mps_huff_dec_ipd_1d(
1176                 it_bit_buff, &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_1],
1177                 out_data_1, num_val_1_int, p0_flag[0]);
1178           }
1179           if (out_data_2 != NULL) {
1180             ixheaacd_mps_huff_dec_ipd_1d(
1181                 it_bit_buff, &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_2],
1182                 out_data_2, num_val_2_int, p0_flag[1]);
1183           }
1184 
1185           break;
1186         case CPC:
1187           if (out_data_1 != NULL) {
1188             ia_mps_dec_huff_dec_cpc_1d(
1189                 &(ixheaacd_huff_cpc_nodes.h_1_dim[huff_yy_1]), out_data_1,
1190                 num_val_1_int, p0_flag[0], it_bit_buff);
1191           }
1192           if (out_data_2 != NULL) {
1193             ia_mps_dec_huff_dec_cpc_1d(
1194                 &(ixheaacd_huff_cpc_nodes.h_1_dim[huff_yy_2]), out_data_2,
1195                 num_val_2_int, p0_flag[1], it_bit_buff);
1196           }
1197 
1198           break;
1199         default:
1200           break;
1201       }
1202 
1203       break;
1204 
1205     case HUFF_2D:
1206 
1207       switch (*cdg_scheme & PAIR_MASK) {
1208         case FREQ_PAIR:
1209 
1210           if (out_data_1 != NULL) {
1211             if (!pilot_coding_flag && diff_type_1 == DIFF_FREQ) {
1212               p0_data_1[0] = &out_data_1[0];
1213               p0_data_1[1] = NULL;
1214 
1215               num_val_1_int -= 1;
1216               out_data_1_int += 1;
1217             }
1218             df_rest_flag_1 = num_val_1_int % 2;
1219             if (df_rest_flag_1) num_val_1_int -= 1;
1220           }
1221           if (out_data_2 != NULL) {
1222             if (!pilot_coding_flag && diff_type_2 == DIFF_FREQ) {
1223               p0_data_2[0] = NULL;
1224               p0_data_2[1] = &out_data_2[0];
1225 
1226               num_val_2_int -= 1;
1227               out_data_2_int += 1;
1228             }
1229             df_rest_flag_2 = num_val_2_int % 2;
1230             if (df_rest_flag_2) num_val_2_int -= 1;
1231           }
1232 
1233           switch (data_type) {
1234             case CLD:
1235 
1236               if (out_data_1 != NULL) {
1237                 ixheaacd_mps_huff_dec_cld_2d(
1238                     it_bit_buff,
1239                     &ixheaacd_huff_cld_nodes.h_2_dim[huff_yy_1][FREQ_PAIR],
1240                     pair_vec, num_val_1_int, 2, p0_data_1);
1241                 if (df_rest_flag_1) {
1242                   ixheaacd_mps_huff_dec_cld_1d(
1243                       it_bit_buff, &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_1],
1244                       out_data_1_int + num_val_1_int, 1, 0);
1245                 }
1246               }
1247               if (out_data_2 != NULL) {
1248                 ixheaacd_mps_huff_dec_cld_2d(
1249                     it_bit_buff,
1250                     &ixheaacd_huff_cld_nodes.h_2_dim[huff_yy_2][FREQ_PAIR],
1251                     pair_vec + 1, num_val_2_int, 2, p0_data_2);
1252                 if (df_rest_flag_2) {
1253                   ixheaacd_mps_huff_dec_cld_1d(
1254                       it_bit_buff, &ixheaacd_huff_cld_nodes.h_1_dim[huff_yy_2],
1255                       out_data_2_int + num_val_2_int, 1, 0);
1256                 }
1257               }
1258               break;
1259 
1260             case ICC:
1261               if (out_data_1 != NULL) {
1262                 ixheaacd_mps_huff_dec_icc_2d(
1263                     it_bit_buff,
1264                     &ixheaacd_huff_icc_nodes.h_2_dim[huff_yy_1][FREQ_PAIR],
1265                     pair_vec, num_val_1_int, 2, p0_data_1);
1266                 if (df_rest_flag_1) {
1267                   ixheaacd_mps_huff_dec_icc_1d(
1268                       it_bit_buff, &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_1],
1269                       out_data_1_int + num_val_1_int, 1, 0);
1270                 }
1271               }
1272               if (out_data_2 != NULL) {
1273                 ixheaacd_mps_huff_dec_icc_2d(
1274                     it_bit_buff,
1275                     &ixheaacd_huff_icc_nodes.h_2_dim[huff_yy_2][FREQ_PAIR],
1276                     pair_vec + 1, num_val_2_int, 2, p0_data_2);
1277                 if (df_rest_flag_2) {
1278                   ixheaacd_mps_huff_dec_icc_1d(
1279                       it_bit_buff, &ixheaacd_huff_icc_nodes.h_1_dim[huff_yy_2],
1280                       out_data_2_int + num_val_2_int, 1, 0);
1281                 }
1282               }
1283               break;
1284 
1285             case IPD:
1286               if (out_data_1 != NULL) {
1287                 ixheaacd_mps_huff_dec_ipd_2d(
1288                     it_bit_buff,
1289                     &ixheaacd_huff_ipd_nodes.h_2_dim[huff_yy_1][FREQ_PAIR],
1290                     pair_vec, num_val_1_int, 2, p0_data_1);
1291                 if (df_rest_flag_1) {
1292                   ixheaacd_mps_huff_dec_ipd_1d(
1293                       it_bit_buff, &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_1],
1294                       out_data_1_int + num_val_1_int, 1, 0);
1295                 }
1296               }
1297               if (out_data_2 != NULL) {
1298                 ixheaacd_mps_huff_dec_ipd_2d(
1299                     it_bit_buff,
1300                     &ixheaacd_huff_ipd_nodes.h_2_dim[huff_yy_2][FREQ_PAIR],
1301                     pair_vec + 1, num_val_2_int, 2, p0_data_2);
1302                 if (df_rest_flag_2) {
1303                   ixheaacd_mps_huff_dec_ipd_1d(
1304                       it_bit_buff, &ixheaacd_huff_ipd_nodes.h_1_dim[huff_yy_2],
1305                       out_data_2_int + num_val_2_int, 1, 0);
1306                 }
1307               }
1308               break;
1309             case CPC:
1310               if (out_data_1 != NULL) {
1311                 ia_mps_dec_huff_dec_cpc_2d(
1312                     &(ixheaacd_huff_cpc_nodes.h_2_dim[huff_yy_1][FREQ_PAIR]), pair_vec,
1313                     num_val_1_int, 2, p0_data_1, it_bit_buff);
1314                 if (df_rest_flag_1) {
1315                   ia_mps_dec_huff_dec_cpc_1d(&(ixheaacd_huff_cpc_nodes.h_1_dim[huff_yy_1]),
1316                       out_data_1_int + num_val_1_int, 1, 0, it_bit_buff);
1317                 }
1318               }
1319               if (out_data_2 != NULL) {
1320                 ia_mps_dec_huff_dec_cpc_2d(
1321                     &(ixheaacd_huff_cpc_nodes.h_2_dim[huff_yy_2][FREQ_PAIR]), pair_vec + 1,
1322                     num_val_2_int, 2, p0_data_2, it_bit_buff);
1323 
1324                 if (df_rest_flag_2) {
1325                   ia_mps_dec_huff_dec_cpc_1d(
1326                       &(ixheaacd_huff_cpc_nodes.h_1_dim[huff_yy_2]),
1327                       out_data_2_int + num_val_2_int, 1, 0, it_bit_buff);
1328                 }
1329               }
1330               break;
1331             default:
1332               break;
1333           }
1334 
1335           if (out_data_1 != NULL) {
1336             for (i = 0; i < num_val_1_int - 1; i += 2) {
1337               out_data_1_int[i] = pair_vec[i][0];
1338               out_data_1_int[i + 1] = pair_vec[i][1];
1339             }
1340           }
1341           if (out_data_2 != NULL) {
1342             for (i = 0; i < num_val_2_int - 1; i += 2) {
1343               out_data_2_int[i] = pair_vec[i + 1][0];
1344               out_data_2_int[i + 1] = pair_vec[i + 1][1];
1345             }
1346           }
1347 
1348           break;
1349 
1350         case TIME_PAIR:
1351 
1352           if (!pilot_coding_flag &&
1353               ((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) {
1354             p0_data_1[0] = &out_data_1[0];
1355             p0_data_1[1] = &out_data_2[0];
1356 
1357             out_data_1_int += 1;
1358             out_data_2_int += 1;
1359 
1360             num_val_1_int -= 1;
1361           }
1362 
1363           if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) {
1364             diff_type = DIFF_TIME;
1365           } else {
1366             diff_type = DIFF_FREQ;
1367           }
1368           if (pilot_coding_flag) {
1369             huff_yy = PCM_PLT;
1370           } else {
1371             huff_yy = diff_type;
1372           }
1373 
1374           switch (data_type) {
1375             case CLD:
1376               ixheaacd_mps_huff_dec_cld_2d(
1377                   it_bit_buff,
1378                   &ixheaacd_huff_cld_nodes.h_2_dim[huff_yy][TIME_PAIR],
1379                   pair_vec, num_val_1_int, 1, p0_data_1);
1380               break;
1381 
1382             case ICC:
1383               ixheaacd_mps_huff_dec_icc_2d(
1384                   it_bit_buff,
1385                   &ixheaacd_huff_icc_nodes.h_2_dim[huff_yy][TIME_PAIR],
1386                   pair_vec, num_val_1_int, 1, p0_data_1);
1387               break;
1388 
1389             case IPD:
1390               ixheaacd_mps_huff_dec_ipd_2d(
1391                   it_bit_buff,
1392                   &ixheaacd_huff_ipd_nodes.h_2_dim[huff_yy][TIME_PAIR],
1393                   pair_vec, num_val_1_int, 1, p0_data_1);
1394               break;
1395             case CPC:
1396               ia_mps_dec_huff_dec_cpc_2d(&(ixheaacd_huff_cpc_nodes.h_2_dim[huff_yy][TIME_PAIR]),
1397                   pair_vec, num_val_1_int, 1, p0_data_1, it_bit_buff);
1398               break;
1399             default:
1400               break;
1401           }
1402 
1403           for (i = 0; i < num_val_1_int; i++) {
1404             out_data_1_int[i] = pair_vec[i][0];
1405             out_data_2_int[i] = pair_vec[i][1];
1406           }
1407 
1408           break;
1409 
1410         default:
1411           break;
1412       }
1413 
1414       break;
1415 
1416     default:
1417       break;
1418   }
1419 
1420   return;
1421 }
1422 
ixheaacd_diff_freq_decode(WORD32 * diff_data,WORD32 * out_data,WORD32 num_val)1423 static VOID ixheaacd_diff_freq_decode(WORD32* diff_data, WORD32* out_data,
1424                                       WORD32 num_val) {
1425   WORD32 i = 0;
1426 
1427   out_data[0] = diff_data[0];
1428 
1429   for (i = 1; i < num_val; i++) {
1430     out_data[i] = out_data[i - 1] + diff_data[i];
1431   }
1432 }
1433 
ixheaacd_mps_diff_time_dec_bwd(WORD32 * prev_data,WORD32 * diff_data,WORD32 * out_data,WORD32 mixed_diff_type,WORD32 num_val)1434 static VOID ixheaacd_mps_diff_time_dec_bwd(WORD32* prev_data, WORD32* diff_data,
1435                                            WORD32* out_data,
1436                                            WORD32 mixed_diff_type,
1437                                            WORD32 num_val) {
1438   WORD32 i = 0;
1439 
1440   if (mixed_diff_type) {
1441     out_data[0] = diff_data[0];
1442     for (i = 1; i < num_val; i++) {
1443       out_data[i] = prev_data[i] + diff_data[i];
1444     }
1445   } else {
1446     for (i = 0; i < num_val; i++) {
1447       out_data[i] = prev_data[i] + diff_data[i];
1448     }
1449   }
1450 }
1451 
ixheaacd_mps_diff_time_dec_fwd(WORD32 * prev_data,WORD32 * diff_data,WORD32 * out_data,WORD32 mixed_diff_type,WORD32 num_val)1452 static VOID ixheaacd_mps_diff_time_dec_fwd(WORD32* prev_data, WORD32* diff_data,
1453                                            WORD32* out_data,
1454                                            WORD32 mixed_diff_type,
1455                                            WORD32 num_val) {
1456   WORD32 i = 0;
1457 
1458   if (mixed_diff_type) {
1459     out_data[0] = diff_data[0];
1460     for (i = 1; i < num_val; i++) {
1461       out_data[i] = prev_data[i] - diff_data[i];
1462     }
1463   } else {
1464     for (i = 0; i < num_val; i++) {
1465       out_data[i] = prev_data[i] - diff_data[i];
1466     }
1467   }
1468 }
1469 
ixheaacd_attach_lsb(ia_bit_buf_struct * it_bit_buff,WORD32 * in_data_msb,WORD32 ixheaacd_drc_offset,WORD32 num_lsb,WORD32 num_val,WORD32 * out_data)1470 static VOID ixheaacd_attach_lsb(ia_bit_buf_struct *it_bit_buff,
1471                                 WORD32* in_data_msb, WORD32 ixheaacd_drc_offset,
1472                                 WORD32 num_lsb, WORD32 num_val,
1473                                 WORD32* out_data) {
1474   WORD32 i = 0, lsb = 0, msb = 0;
1475   UWORD32 data = 0;
1476 
1477   for (i = 0; i < num_val; i++) {
1478     msb = in_data_msb[i];
1479 
1480     if (num_lsb > 0) {
1481       data = ixheaacd_read_bits_buf(it_bit_buff, num_lsb);
1482       lsb = data;
1483 
1484       out_data[i] = ((msb << num_lsb) | lsb) - ixheaacd_drc_offset;
1485     } else
1486       out_data[i] = msb - ixheaacd_drc_offset;
1487   }
1488 
1489   return;
1490 }
1491 
ixheaacd_mps_ecdatapairdec(ia_bit_buf_struct * it_bit_buff,WORD32 outdata[][MAXBANDS],WORD32 history[MAXBANDS],WORD32 data_type,WORD32 set_idx,WORD32 start_band,WORD32 data_bands,WORD32 pair_flag,WORD32 coarse_flag,WORD32 diff_time_back_flag,WORD32 ld_mps_flag,WORD32 heaac_mps_present,WORD32 ec_flag)1492 WORD32 ixheaacd_mps_ecdatapairdec(ia_bit_buf_struct *it_bit_buff, WORD32 outdata[][MAXBANDS],
1493                                   WORD32 history[MAXBANDS], WORD32 data_type, WORD32 set_idx,
1494                                   WORD32 start_band, WORD32 data_bands, WORD32 pair_flag,
1495                                   WORD32 coarse_flag, WORD32 diff_time_back_flag,
1496                                   WORD32 ld_mps_flag, WORD32 heaac_mps_present, WORD32 ec_flag) {
1497   WORD32 attach_lsb_flag = 0;
1498   WORD32 pcm_coding_flag = 0;
1499   WORD32 pilot_coding_flag = 0;
1500   WORD32 pilot_data[2] = {0, 0};
1501   WORD32 mixed_time_pair = 0, pcm_val = 0;
1502   WORD32 quant_levels = 0, quant_offset = 0;
1503   UWORD32 data = 0;
1504   WORD32 band_start = 0;
1505 
1506   WORD32 data_pair[2][MAXBANDS] = {{0}};
1507   WORD32 data_diff[2][MAXBANDS] = {{0}};
1508 
1509   WORD32 msb_state[MAXBANDS] = {0};
1510 
1511   WORD32* data_array[2] = {NULL, NULL};
1512 
1513   WORD32 diff_type[2] = {DIFF_FREQ, DIFF_FREQ};
1514   WORD32 cdg_scheme = HUFF_1D;
1515   WORD32 direction = BACKWARDS;
1516 
1517   if (heaac_mps_present == 1) {
1518     band_start = start_band;
1519   }
1520 
1521   switch (data_type) {
1522     case CLD:
1523       if (coarse_flag) {
1524         attach_lsb_flag = 0;
1525         quant_levels = 15;
1526         quant_offset = 7;
1527       } else {
1528         attach_lsb_flag = 0;
1529         quant_levels = 31;
1530         quant_offset = 15;
1531       }
1532 
1533       break;
1534 
1535     case ICC:
1536       if (coarse_flag) {
1537         attach_lsb_flag = 0;
1538         quant_levels = 4;
1539         quant_offset = 0;
1540       } else {
1541         attach_lsb_flag = 0;
1542         quant_levels = 8;
1543         quant_offset = 0;
1544       }
1545 
1546       break;
1547 
1548     case IPD:
1549       if (coarse_flag) {
1550         attach_lsb_flag = 0;
1551         quant_levels = 8;
1552         quant_offset = 0;
1553       } else {
1554         attach_lsb_flag = 1;
1555         quant_levels = 16;
1556         quant_offset = 0;
1557       }
1558       break;
1559 
1560     case CPC:
1561       if (coarse_flag) {
1562         attach_lsb_flag = 0;
1563         quant_levels = 26;
1564         quant_offset = 10;
1565       } else {
1566         attach_lsb_flag = 1;
1567         quant_levels = 51;
1568         quant_offset = 20;
1569       }
1570       break;
1571 
1572     default:
1573       break;
1574   }
1575 
1576   data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1577   pcm_coding_flag = data;
1578 
1579   pilot_coding_flag = 0;
1580 
1581   if (heaac_mps_present == 1) {
1582     if (pcm_coding_flag && data_bands > 4) {
1583       data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1584       pilot_coding_flag = data;
1585     }
1586   }
1587 
1588   if (pcm_coding_flag && !pilot_coding_flag) {
1589     if (pair_flag) {
1590       data_array[0] = data_pair[0];
1591       data_array[1] = data_pair[1];
1592       pcm_val = 2 * data_bands;
1593     } else {
1594       data_array[0] = data_pair[0];
1595       data_array[1] = NULL;
1596       pcm_val = data_bands;
1597     }
1598 
1599     ixheaacd_mps_pcm_decode(it_bit_buff, data_array[0], data_array[1],
1600                             quant_offset, pcm_val, quant_levels);
1601 
1602   } else {
1603     if (pair_flag) {
1604       data_array[0] = data_diff[0];
1605       data_array[1] = data_diff[1];
1606     } else {
1607       data_array[0] = data_diff[0];
1608       data_array[1] = NULL;
1609     }
1610 
1611     diff_type[0] = DIFF_FREQ;
1612     diff_type[1] = DIFF_FREQ;
1613 
1614     direction = BACKWARDS;
1615 
1616     if (!pilot_coding_flag) {
1617       if (pair_flag || diff_time_back_flag) {
1618         data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1619         diff_type[0] = data;
1620       }
1621 
1622       if (pair_flag && ((diff_type[0] == DIFF_FREQ) || diff_time_back_flag)) {
1623         data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1624         diff_type[1] = data;
1625       }
1626     }
1627 
1628     if (data_bands <= 0) {
1629       if (!ec_flag)
1630         return -1;
1631       else
1632         longjmp(*(it_bit_buff->xaac_jmp_buf), IA_FATAL_ERROR);
1633     }
1634 
1635     ixheaacd_huff_decode(it_bit_buff, data_array[0], data_array[1], data_type, diff_type[0],
1636                          diff_type[1], pilot_coding_flag, pilot_data, data_bands, &cdg_scheme,
1637                          ld_mps_flag);
1638 
1639     if (pilot_coding_flag && heaac_mps_present == 1) {
1640       WORD32 i;
1641       for (i = 0; i < data_bands; i++) {
1642         data_pair[0][i] = data_diff[0][i] + pilot_data[0];
1643       }
1644 
1645       if (pair_flag) {
1646         for (i = 0; i < data_bands; i++) {
1647           data_pair[1][i] = data_diff[1][i] + pilot_data[0];
1648         }
1649       }
1650     } else {
1651       if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) {
1652         if (pair_flag) {
1653           if ((diff_type[0] == DIFF_TIME) && !diff_time_back_flag) {
1654             direction = FORWARDS;
1655           } else if (diff_type[1] == DIFF_TIME) {
1656             direction = BACKWARDS;
1657           } else {
1658             data = ixheaacd_read_bits_buf(it_bit_buff, 1);
1659             direction = data;
1660           }
1661         } else {
1662           direction = BACKWARDS;
1663         }
1664       }
1665 
1666       mixed_time_pair = (diff_type[0] != diff_type[1]) &&
1667                         ((cdg_scheme & PAIR_MASK) == TIME_PAIR);
1668 
1669       if (direction == BACKWARDS) {
1670         if (diff_type[0] == DIFF_FREQ) {
1671           ixheaacd_diff_freq_decode(data_diff[0], data_pair[0], data_bands);
1672         } else {
1673           WORD32 i;
1674           for (i = 0; i < data_bands; i++) {
1675             msb_state[i] = history[i + band_start] + quant_offset;
1676             if (attach_lsb_flag) {
1677               msb_state[i] >>= 1;
1678             }
1679           }
1680           ixheaacd_mps_diff_time_dec_bwd(msb_state, data_diff[0], data_pair[0],
1681                                          mixed_time_pair, data_bands);
1682         }
1683         if (diff_type[1] == DIFF_FREQ) {
1684           ixheaacd_diff_freq_decode(data_diff[1], data_pair[1], data_bands);
1685         } else {
1686           ixheaacd_mps_diff_time_dec_bwd(data_pair[0], data_diff[1], data_pair[1],
1687                                          mixed_time_pair, data_bands);
1688         }
1689       } else {
1690         ixheaacd_diff_freq_decode(data_diff[1], data_pair[1], data_bands);
1691 
1692         if (diff_type[0] == DIFF_FREQ) {
1693           ixheaacd_diff_freq_decode(data_diff[0], data_pair[0], data_bands);
1694         } else {
1695           ixheaacd_mps_diff_time_dec_fwd(data_pair[1], data_diff[0], data_pair[0],
1696                                          mixed_time_pair, data_bands);
1697         }
1698       }
1699     }
1700     ixheaacd_attach_lsb(it_bit_buff, data_pair[0], quant_offset,
1701                         attach_lsb_flag ? 1 : 0, data_bands, data_pair[0]);
1702     if (pair_flag) {
1703       ixheaacd_attach_lsb(it_bit_buff, data_pair[1], quant_offset,
1704                           attach_lsb_flag ? 1 : 0, data_bands, data_pair[1]);
1705     }
1706   }
1707 
1708   memcpy(outdata[set_idx] + band_start, data_pair[0],
1709          sizeof(WORD32) * data_bands);
1710   if (pair_flag) {
1711     memcpy(outdata[set_idx + 1] + band_start, data_pair[1],
1712            sizeof(WORD32) * data_bands);
1713   }
1714 
1715   return IA_NO_ERROR;
1716 }
1717 
ixheaacd_mps_huff_decode(ia_bit_buf_struct * it_bit_buff,WORD32 * out_data,WORD32 num_val)1718 VOID ixheaacd_mps_huff_decode(ia_bit_buf_struct *it_bit_buff,
1719                               WORD32* out_data, WORD32 num_val) {
1720   WORD32 val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0;
1721   WORD32 rl_data[2] = {0};
1722 
1723   while (val_rcvd < num_val) {
1724     ixheaacd_mps_huff_read_2d(it_bit_buff,
1725                               (ia_huff_node_struct)&ixheaacd_huff_reshape_nodes,
1726                               rl_data, &dummy);
1727     val = rl_data[0];
1728     len = rl_data[1] + 1;
1729     for (i = val_rcvd; i < val_rcvd + len; i++) {
1730       out_data[i] = val;
1731     }
1732     val_rcvd += len;
1733   }
1734 
1735   return;
1736 }
1737 
ixheaacd_update_out_buffer(ia_heaac_mps_state_struct * pstr_mps_state,WORD16 * out_buf)1738 VOID ixheaacd_update_out_buffer(ia_heaac_mps_state_struct *pstr_mps_state,
1739                                 WORD16 *out_buf) {
1740   WORD32 ch, sam;
1741   WORD32 num_output_channels_at = pstr_mps_state->num_output_channels_at;
1742   WORD32 frame_length = pstr_mps_state->frame_length;
1743 
1744   WORD32 *p_time_out = pstr_mps_state->array_struct->time_out;
1745 
1746   for (ch = 0; ch < num_output_channels_at; ch++) {
1747     WORD32 *time_out = p_time_out;
1748     for (sam = 0; sam < frame_length; sam++) {
1749       out_buf[sam * num_output_channels_at + ch] = (*time_out++) >> 16;
1750     }
1751     p_time_out += QBXTS;
1752   }
1753 
1754   return;
1755 }
1756 
ixheaacd_update_time_out_buffer(ia_heaac_mps_state_struct * pstr_mps_state)1757 VOID ixheaacd_update_time_out_buffer(ia_heaac_mps_state_struct *pstr_mps_state) {
1758   WORD32 n;
1759   WORD32 up_mix_type = pstr_mps_state->up_mix_type;
1760   WORD32 tree_config = pstr_mps_state->tree_config;
1761   WORD32 lfe_gain = pstr_mps_state->lfe_gain;
1762   WORD32 surround_gain = pstr_mps_state->surround_gain;
1763 
1764   if (!pstr_mps_state->bs_config.arbitrary_tree && up_mix_type != 2 && up_mix_type != 3) {
1765     WORD32 frame_length = pstr_mps_state->frame_length;
1766 
1767     WORD32 *time_out_3 = pstr_mps_state->array_struct->time_out + QBXTSX3;
1768     WORD32 *time_out_4 = time_out_3 + QBXTS;
1769     WORD32 *time_out_5 = time_out_4 + QBXTS;
1770     WORD32 *time_out_6 = time_out_5 + QBXTS;
1771     WORD32 *time_out_7 = time_out_6 + QBXTS;
1772 
1773     for (n = 0; n < frame_length; n++) {
1774       *time_out_3 = ixheaacd_mps_mult32_shr_15(*time_out_3, lfe_gain);
1775       time_out_3++;
1776       *time_out_4 = ixheaacd_mps_mult32_shr_15(*time_out_4, surround_gain);
1777       time_out_4++;
1778       *time_out_5 = ixheaacd_mps_mult32_shr_15(*time_out_5, surround_gain);
1779       time_out_5++;
1780     }
1781 
1782     if (tree_config == 4 || tree_config == 6) {
1783       for (n = 0; n < frame_length; n++) {
1784         *time_out_6 = ixheaacd_mps_mult32_shr_15(*time_out_6, surround_gain);
1785         time_out_6++;
1786         *time_out_7 = ixheaacd_mps_mult32_shr_15(*time_out_7, surround_gain);
1787         time_out_7++;
1788       }
1789     }
1790   }
1791 }
1792 
ixheaacd_apply_frame(ia_heaac_mps_state_struct * pstr_mps_state,WORD32 in_time_slots,WORD32 * m_qmf_real,WORD32 * m_qmf_imag,WORD16 * out_buf)1793 static IA_ERRORCODE ixheaacd_apply_frame(ia_heaac_mps_state_struct *pstr_mps_state,
1794                                          WORD32 in_time_slots,
1795                                          WORD32 *m_qmf_real, WORD32 *m_qmf_imag,
1796                                          WORD16 *out_buf) {
1797   IA_ERRORCODE err_code = IA_NO_ERROR;
1798   WORD32 ch, ts, qs;
1799   WORD32 *pbuf_real, *pbuf_imag, *pbuf_re, *pbuf_im;
1800   WORD32 *buf_real, *buf_imag;
1801   WORD32 *qmf_input_delay_real_2 =
1802       pstr_mps_state->mps_persistent_mem.qmf_input_delay_real;
1803   WORD32 *qmf_input_delay_imag_2 =
1804       pstr_mps_state->mps_persistent_mem.qmf_input_delay_imag;
1805   WORD32 num_input_channels = pstr_mps_state->num_input_channels;
1806   WORD32 qmf_bands = pstr_mps_state->qmf_bands;
1807   WORD32 cur_time_slot = pstr_mps_state->cur_time_slot;
1808   WORD32 time_slots = pstr_mps_state->time_slots;
1809   WORD32 qmf_input_delay_index = pstr_mps_state->qmf_input_delay_index;
1810   WORD32 up_mix_type = pstr_mps_state->up_mix_type;
1811   WORD32 residual_coding = pstr_mps_state->residual_coding;
1812   WORD32 arbitrary_downmix = pstr_mps_state->arbitrary_downmix;
1813 
1814   WORD32 *qmf_input_delay_real_1, *qmf_input_delay_imag_1;
1815   WORD32 *qmf_input_delay_real, *qmf_input_delay_imag;
1816 
1817   WORD32 *p_qmf_real = m_qmf_real;
1818   WORD32 *p_qmf_imag = m_qmf_imag;
1819   WORD32 *p_qmf_re, *p_qmf_im, *qmf_real, *qmf_imag;
1820 
1821   if (cur_time_slot + in_time_slots > time_slots) {
1822     if (pstr_mps_state->ec_flag)
1823       cur_time_slot = time_slots - in_time_slots;
1824     else
1825       return IA_FATAL_ERROR;
1826   }
1827   if (time_slots % HOP_SLOTS != 0) {
1828     if (pstr_mps_state->ec_flag)
1829       time_slots = time_slots - (time_slots % HOP_SLOTS);
1830     else
1831       return IA_XHEAAC_MPS_DEC_EXE_FATAL_INVALID_TIMESLOTS;
1832   }
1833 
1834   pbuf_real = pstr_mps_state->array_struct->buf_real;
1835   pbuf_imag = pstr_mps_state->array_struct->buf_imag;
1836 
1837   if (up_mix_type == 1) {
1838     for (ch = 0; ch < num_input_channels; ch++) {
1839       pbuf_re = pbuf_real;
1840       pbuf_im = pbuf_imag;
1841 
1842       p_qmf_re = p_qmf_real;
1843       p_qmf_im = p_qmf_imag;
1844 
1845       for (ts = 0; ts < in_time_slots; ts++) {
1846         buf_real = pbuf_re + (cur_time_slot + ts);
1847         buf_imag = pbuf_im + (cur_time_slot + ts);
1848 
1849         qmf_real = p_qmf_re;
1850         qmf_imag = p_qmf_im;
1851 
1852         for (qs = 0; qs < qmf_bands; qs++) {
1853           *buf_real++ = *qmf_real++;
1854           *buf_imag++ = *qmf_imag++;
1855         }
1856         pbuf_re += MAX_HYBRID_BANDS;
1857         pbuf_im += MAX_HYBRID_BANDS;
1858 
1859         p_qmf_re += MAX_NUM_QMF_BANDS;
1860         p_qmf_im += MAX_NUM_QMF_BANDS;
1861       }
1862       pbuf_real += TSXHB;
1863       pbuf_imag += TSXHB;
1864 
1865       p_qmf_real += QBXTS;
1866       p_qmf_imag += QBXTS;
1867     }
1868   } else {
1869     for (ch = 0; ch < num_input_channels; ch++) {
1870       WORD32 offset = ch * PCXQB;
1871       qmf_input_delay_index = pstr_mps_state->qmf_input_delay_index;
1872       qmf_input_delay_real_1 = qmf_input_delay_real_2 + offset;
1873       qmf_input_delay_imag_1 = qmf_input_delay_imag_2 + offset;
1874 
1875       pbuf_re = pbuf_real + cur_time_slot * MAX_HYBRID_BANDS;
1876       pbuf_im = pbuf_imag + cur_time_slot * MAX_HYBRID_BANDS;
1877 
1878       p_qmf_re = p_qmf_real;
1879       p_qmf_im = p_qmf_imag;
1880 
1881       for (ts = 0; ts < in_time_slots; ts++) {
1882         WORD32 off_set = qmf_input_delay_index * MAX_NUM_QMF_BANDS;
1883         qmf_input_delay_real = qmf_input_delay_real_1 + off_set;
1884         qmf_input_delay_imag = qmf_input_delay_imag_1 + off_set;
1885 
1886         buf_real = pbuf_re;
1887         buf_imag = pbuf_im;
1888 
1889         qmf_real = p_qmf_re;
1890         qmf_imag = p_qmf_im;
1891 
1892         for (qs = 0; qs < qmf_bands; qs++) {
1893           {
1894             *buf_real++ = *qmf_input_delay_real;
1895             *buf_imag++ = *qmf_input_delay_imag;
1896 
1897             *qmf_input_delay_real++ = *qmf_real++;
1898             *qmf_input_delay_imag++ = *qmf_imag++;
1899           }
1900         }
1901 
1902         qmf_input_delay_index++;
1903 
1904         if (qmf_input_delay_index == PC_FILTERDELAY) {
1905           qmf_input_delay_index = 0;
1906         }
1907         pbuf_re += MAX_HYBRID_BANDS;
1908         pbuf_im += MAX_HYBRID_BANDS;
1909 
1910         p_qmf_re += MAX_NUM_QMF_BANDS;
1911         p_qmf_im += MAX_NUM_QMF_BANDS;
1912       }
1913       pbuf_real += TSXHB;
1914       pbuf_imag += TSXHB;
1915 
1916       p_qmf_real += QBXTS;
1917       p_qmf_imag += QBXTS;
1918     }
1919     pstr_mps_state->qmf_input_delay_index = qmf_input_delay_index;
1920   }
1921 
1922   pstr_mps_state->cur_time_slot += in_time_slots;
1923   cur_time_slot = pstr_mps_state->cur_time_slot;
1924 
1925   if (pstr_mps_state->cur_time_slot < time_slots) {
1926     if (pstr_mps_state->ec_flag) {
1927       pstr_mps_state->cur_time_slot = time_slots;
1928     } else
1929       return IA_FATAL_ERROR;
1930   }
1931 
1932   pstr_mps_state->cur_time_slot = 0;
1933 
1934   err_code = ixheaacd_decode_frame(pstr_mps_state);
1935   if (err_code != IA_NO_ERROR) return err_code;
1936 
1937   ixheaacd_mdct_2_qmf(pstr_mps_state);
1938 
1939   ixheaacd_hybrid_qmf_analysis(pstr_mps_state);
1940 
1941   if (residual_coding || (arbitrary_downmix == 2)) {
1942     ixheaacd_update_buffers(pstr_mps_state);
1943   }
1944 
1945   if (up_mix_type == 1) {
1946     ixheaacd_apply_blind(pstr_mps_state);
1947   }
1948 
1949   ixheaacd_calc_m1m2(pstr_mps_state);
1950 
1951   ixheaacd_smooth_m1m2(pstr_mps_state);
1952 
1953   ixheaacd_mps_apply_m1(pstr_mps_state);
1954 
1955   ixheaacd_buffer_m1(pstr_mps_state);
1956 
1957   if (up_mix_type != 2) {
1958     if (pstr_mps_state->temp_shape_config == 2) {
1959       ixheaacd_pre_reshape_bb_env(pstr_mps_state);
1960     }
1961   }
1962 
1963   ixheaacd_create_w(pstr_mps_state);
1964 
1965   ixheaacd_apply_m2(pstr_mps_state);
1966 
1967   ixheaacd_buffer_m2(pstr_mps_state);
1968 
1969   if (up_mix_type != 2) {
1970     if (pstr_mps_state->temp_shape_config == 2) {
1971       ixheaacd_reshape_bb_env(pstr_mps_state);
1972     }
1973   }
1974 
1975   ixheaacd_tp_process(pstr_mps_state);
1976 
1977   ixheaacd_update_time_out_buffer(pstr_mps_state);
1978 
1979   ixheaacd_update_out_buffer(pstr_mps_state, out_buf);
1980 
1981   pstr_mps_state->parse_next_bitstream_frame = 1;
1982 
1983   return IA_NO_ERROR;
1984 }
1985 
ixheaacd_heaac_mps_apply(ia_exhaacplus_dec_api_struct * self,WORD16 * output_buf,UWORD8 * mps_buffer,WORD32 mps_bytes)1986 IA_ERRORCODE ixheaacd_heaac_mps_apply(ia_exhaacplus_dec_api_struct *self,
1987                                       WORD16 *output_buf, UWORD8 *mps_buffer,
1988                                       WORD32 mps_bytes) {
1989   ia_heaac_mps_state_struct *pstr_mps_state =
1990       &self->p_state_aac->heaac_mps_handle;
1991   IA_ERRORCODE error_code = IA_NO_ERROR;
1992   WORD32 n_channels, n_time_slots, qmf_bands, channel;
1993   ia_heaac_mps_state_struct *curr_state = pstr_mps_state;
1994 
1995   WORD32 *p_qmf_real = pstr_mps_state->array_struct->m_qmf_real;
1996   WORD32 *p_qmf_imag = pstr_mps_state->array_struct->m_qmf_imag;
1997   WORD32 buffer_size = mps_bytes;
1998 
1999   if (self->p_state_aac->heaac_mps_handle.is_first == 1) {
2000     self->p_state_aac->heaac_mps_handle.is_first = 1;
2001     if (pstr_mps_state->bytes_remaining != 0) {
2002       buffer_size = mps_bytes + pstr_mps_state->bytes_remaining;
2003       for (WORD32 ii = 0; ii < mps_bytes; ii++) {
2004         pstr_mps_state->temp_buf[ii + pstr_mps_state->bytes_remaining] =
2005             mps_buffer[ii];
2006       }
2007 
2008       pstr_mps_state->ptr_mps_bit_buff = ixheaacd_create_bit_buf(
2009           &pstr_mps_state->mps_bit_buf, (UWORD8 *)pstr_mps_state->temp_buf,
2010           buffer_size);
2011       pstr_mps_state->ptr_mps_bit_buff->xaac_jmp_buf =
2012           &self->p_state_aac->xaac_jmp_buf;
2013 
2014       pstr_mps_state->ptr_mps_bit_buff->cnt_bits += (8 * buffer_size);
2015     } else {
2016       memcpy(pstr_mps_state->temp_buf, mps_buffer, mps_bytes);
2017       buffer_size = mps_bytes;
2018       pstr_mps_state->ptr_mps_bit_buff = ixheaacd_create_bit_buf(
2019           &pstr_mps_state->mps_bit_buf, (UWORD8 *)mps_buffer, buffer_size);
2020       pstr_mps_state->ptr_mps_bit_buff->cnt_bits += (8 * buffer_size);
2021 
2022       pstr_mps_state->ptr_mps_bit_buff->xaac_jmp_buf =
2023           &self->p_state_aac->xaac_jmp_buf;
2024     }
2025   }
2026 
2027   if (curr_state->num_input_channels > 2 && pstr_mps_state->mps_with_sbr == 1) {
2028     if (pstr_mps_state->ec_flag) {
2029       curr_state->num_input_channels = 2;
2030       pstr_mps_state->frame_ok = 0;
2031     } else {
2032       return IA_FATAL_ERROR;
2033     }
2034   }
2035 
2036   n_channels = curr_state->num_input_channels;
2037   n_time_slots = curr_state->time_slots;
2038   qmf_bands = curr_state->qmf_bands;
2039 
2040   if (pstr_mps_state->mps_decode == 1) {
2041     if (pstr_mps_state->mps_with_sbr) {
2042       for (channel = 0; channel < n_channels; channel++) {
2043         WORD32 kk = 0;
2044 
2045         for (WORD32 ii = 0; ii < n_time_slots; ii++) {
2046           FLOAT32 *qmf_re = self->p_state_aac->str_sbr_dec_info[0]
2047                               ->pstr_sbr_channel[channel]
2048                               ->str_sbr_dec.pp_qmf_buf_real[ii];
2049           FLOAT32 *qmf_im = self->p_state_aac->str_sbr_dec_info[0]
2050                               ->pstr_sbr_channel[channel]
2051                               ->str_sbr_dec.pp_qmf_buf_imag[ii];
2052 
2053           for (WORD32 jj = 0; jj < qmf_bands; jj++) {
2054             p_qmf_real[kk] = ixheaacd_mps_mult32_shr_15(
2055                 curr_state->clip_protect_gain, (WORD32)(qmf_re[jj] * 1024));
2056             p_qmf_imag[kk++] = ixheaacd_mps_mult32_shr_15(
2057                 curr_state->clip_protect_gain, (WORD32)(qmf_im[jj] * 1024));
2058           }
2059         }
2060         p_qmf_real += QBXTS;
2061         p_qmf_imag += QBXTS;
2062       }
2063     } else {
2064       for (channel = 0; channel < n_channels; channel++) {
2065         ixheaacd_calc_ana_filt_bank(pstr_mps_state, output_buf, p_qmf_real,
2066                                     p_qmf_imag, channel);
2067 
2068         p_qmf_real += QBXTS;
2069         p_qmf_imag += QBXTS;
2070       }
2071     }
2072     if (!pstr_mps_state->ec_flag && pstr_mps_state->frame_ok) {
2073       error_code = ixheaacd_parse_frame(pstr_mps_state);
2074       if (error_code != IA_NO_ERROR) return error_code;
2075     }
2076 
2077     if (!pstr_mps_state->first_frame || !pstr_mps_state->ec_flag) {
2078       error_code = ixheaacd_apply_frame(pstr_mps_state, n_time_slots,
2079                                         pstr_mps_state->array_struct->m_qmf_real,
2080                                         pstr_mps_state->array_struct->m_qmf_imag, output_buf);
2081       if (error_code != IA_NO_ERROR) return error_code;
2082     }
2083     if (error_code == 0 && pstr_mps_state->ec_flag && pstr_mps_state->frame_ok) {
2084       error_code = ixheaacd_parse_frame(pstr_mps_state);
2085       if (error_code != IA_NO_ERROR) {
2086         pstr_mps_state->frame_ok = 0;
2087       }
2088     }
2089 
2090     pstr_mps_state->i_bytes_consumed_mps =
2091         (WORD32)(pstr_mps_state->ptr_mps_bit_buff->ptr_read_next -
2092                  pstr_mps_state->ptr_mps_bit_buff->ptr_bit_buf_base);
2093 
2094     pstr_mps_state->bytes_remaining =
2095         buffer_size - pstr_mps_state->i_bytes_consumed_mps;
2096 
2097     if (pstr_mps_state->bytes_remaining != 0) {
2098       for (WORD32 ii = 0; ii < pstr_mps_state->bytes_remaining; ii++) {
2099         pstr_mps_state->temp_buf[ii] =
2100             pstr_mps_state->temp_buf[ii + pstr_mps_state->i_bytes_consumed_mps];
2101       }
2102     }
2103   }
2104   self->p_state_aac->heaac_mps_handle.is_first = 1;
2105   self->p_state_aac->heaac_mps_handle.first_frame = 0;
2106 
2107   return error_code;
2108 }
2109