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