• 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 <string.h>
21 #include "ixheaacd_sbr_common.h"
22 #include "ixheaacd_type_def.h"
23 #include "ixheaacd_error_standards.h"
24 
25 #include "ixheaacd_constants.h"
26 #include "ixheaacd_basic_ops32.h"
27 #include "ixheaacd_basic_ops40.h"
28 #include "ixheaacd_basic_ops.h"
29 
30 #include "ixheaacd_bitbuffer.h"
31 #include "ixheaacd_defines.h"
32 #include "ixheaacd_aac_rom.h"
33 #include "ixheaacd_pulsedata.h"
34 
35 #include "ixheaacd_pns.h"
36 
37 #include "ixheaacd_lt_predict.h"
38 #include "ixheaacd_cnst.h"
39 #include "ixheaacd_ec_defines.h"
40 #include "ixheaacd_ec_struct_def.h"
41 #include "ixheaacd_channelinfo.h"
42 
43 #include "ixheaacd_drc_data_struct.h"
44 #include "ixheaacd_drc_dec.h"
45 
46 #include "ixheaacd_sbrdecoder.h"
47 
48 #include "ixheaacd_block.h"
49 #include "ixheaacd_channel.h"
50 #include "ixheaacd_common_rom.h"
51 
52 #include "ixheaacd_aacdec.h"
53 
54 #include "ixheaacd_sbrdecsettings.h"
55 #include "ixheaacd_sbr_scale.h"
56 #include "ixheaacd_env_extr_part.h"
57 #include "ixheaacd_sbr_rom.h"
58 #include "ixheaacd_audioobjtypes.h"
59 #include "ixheaacd_memory_standards.h"
60 #include "ixheaacd_latmdemux.h"
61 #include "ixheaacd_mps_polyphase.h"
62 #include "ixheaacd_config.h"
63 #include "ixheaacd_hybrid.h"
64 #include "ixheaacd_ps_dec.h"
65 #include "ixheaacd_qmf_dec.h"
66 #include "ixheaacd_mps_macro_def.h"
67 #include "ixheaacd_mps_struct_def.h"
68 #include "ixheaacd_mps_res_rom.h"
69 #include "ixheaacd_mps_aac_struct.h"
70 #include "ixheaacd_mps_dec.h"
71 #include "ixheaacd_struct_def.h"
72 
73 #include "ixheaacd_rvlc.h"
74 
75 const UWORD8 ixheaacd_min_huff_cb_pair_tbl[MAX_CB_PAIRS] = {
76     0,  1,  3,  5,  7,  9,  16, 17, 18, 19, 20, 21,
77     22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 11};
78 const UWORD8 ixheaacd_max_huff_cb_pair_table[MAX_CB_PAIRS] = {
79     0,  2,  4,  6,  8,  10, 16, 17, 18, 19, 20, 21,
80     22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 11};
81 const UWORD8 ixheaacd_max_huff_cw_len_table[MAX_CB] = {
82     0,  11, 9,  20, 16, 13, 11, 14, 12, 17, 14, 49, 0,  0,  0,  0,
83     14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
84 const UWORD8 ixheaacd_huff_cb_dim_table[MAX_CB] = {
85     2, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
86     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
87 const UWORD8 ixheaacd_huff_cb_dim_shift_table[MAX_CB] = {
88     1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
89     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
90 const UWORD8 ixheaacd_huff_cb_sign_table[MAX_CB] = {
91     0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
92     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
93 const UWORD8 ixheaacd_huff_cb_priority_table[MAX_CB] = {
94     0, 1, 1, 2, 2,  3,  3,  4,  4,  5,  5,  22, 0,  0,  0,  0,
95     6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
96 const UWORD16 ixheaacd_huff_reord_lav_table[MAX_CB] = {
97     0,    1,   1,   2,   2,   4,   4,   7,   7,    12,  12,
98     8191, 0,   0,   0,   0,   15,  31,  47,  63,   95,  127,
99     159,  191, 223, 255, 319, 383, 511, 767, 1023, 2047};
100 
ixheaacd_huff_code_reorder_tbl_init(ia_hcr_info_struct * ptr_hcr_info)101 VOID ixheaacd_huff_code_reorder_tbl_init(ia_hcr_info_struct *ptr_hcr_info) {
102   ptr_hcr_info->codebook_pairs.ptr_min_cb_pair_tbl =
103       ixheaacd_min_huff_cb_pair_tbl;
104   ptr_hcr_info->codebook_pairs.ptr_max_cb_pair_tbl =
105       ixheaacd_max_huff_cb_pair_table;
106   ptr_hcr_info->table_info.ptr_max_cw_len_tbl = ixheaacd_max_huff_cw_len_table;
107   ptr_hcr_info->table_info.ptr_cb_dimension_tbl = ixheaacd_huff_cb_dim_table;
108   ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl =
109       ixheaacd_huff_cb_dim_shift_table;
110   ptr_hcr_info->table_info.ptr_cb_sign_tbl = ixheaacd_huff_cb_sign_table;
111   ptr_hcr_info->table_info.ptr_cb_priority = ixheaacd_huff_cb_priority_table;
112   ptr_hcr_info->table_info.ptr_lav_tbl = ixheaacd_huff_reord_lav_table;
113 }
114 
ixheaacd_huff_mute_erroneous_lines(ia_hcr_info_struct * ptr_hcr_info)115 VOID ixheaacd_huff_mute_erroneous_lines(ia_hcr_info_struct *ptr_hcr_info) {
116   WORD32 c;
117   WORD32 *ptr_long = ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
118 
119   for (c = 0; c < 1024; c++) {
120     if (ptr_long[c] == (WORD32)8192) {
121       ptr_long[c] = 0;
122     }
123   }
124 }
125 
ixheaacd_err_detect_pcw_segment(WORD8 remaining_bits_in_segment,ia_hcr_info_struct * ptr_hcr_info,ia_pcw_type_struct kind,WORD32 * qsc_base_of_cw,UWORD8 dimension)126 static UWORD8 ixheaacd_err_detect_pcw_segment(WORD8 remaining_bits_in_segment,
127                                               ia_hcr_info_struct *ptr_hcr_info,
128                                               ia_pcw_type_struct kind,
129                                               WORD32 *qsc_base_of_cw,
130                                               UWORD8 dimension) {
131   WORD8 i;
132   if (remaining_bits_in_segment < 0) {
133     switch (kind) {
134       case PCW:
135         ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 31);
136         break;
137       case PCW_SIGN:
138         ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 30);
139         break;
140       case PCW_ESC_SIGN:
141         ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 29);
142         break;
143     }
144     for (i = dimension; i != 0; i--) {
145       *qsc_base_of_cw++ = (WORD32)8192;
146     }
147     return 1;
148   }
149   return 0;
150 }
151 
ixheaacd_nonpcw_sideinfo_init(ia_hcr_info_struct * ptr_hcr_info)152 static VOID ixheaacd_nonpcw_sideinfo_init(ia_hcr_info_struct *ptr_hcr_info) {
153   UWORD16 i, k;
154   UWORD8 cb_dim;
155   UWORD8 *ptr_cb = ptr_hcr_info->str_non_pcw_side_info.ptr_cb;
156   UWORD16 *res_ptr_idx = ptr_hcr_info->str_non_pcw_side_info.res_ptr_idx;
157   UWORD16 *ptr_num_ext_sorted_cw_in_sect =
158       ptr_hcr_info->sect_info.ptr_num_ext_sorted_cw_in_sect;
159   WORD32 num_ext_sorted_cw_in_sect_idx =
160       ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx;
161   UWORD8 *ptr_ext_sorted_cw = ptr_hcr_info->sect_info.ptr_ext_sorted_cw;
162   WORD32 ext_sorted_cw_idx = ptr_hcr_info->sect_info.ext_sorted_cw_idx;
163   UWORD16 *ptr_num_ext_sorted_sect_in_sets =
164       ptr_hcr_info->sect_info.ptr_num_ext_sorted_sect_in_sets;
165   WORD32 num_ext_sorted_sect_in_sets_idx =
166       ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx;
167   WORD32 quant_spec_coeff_idx = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
168   const UWORD8 *ptr_cb_dimension_tbl =
169       ptr_hcr_info->table_info.ptr_cb_dimension_tbl;
170   WORD32 loop_idx = 0;
171 
172   for (i = ptr_num_ext_sorted_sect_in_sets[num_ext_sorted_sect_in_sets_idx];
173        i != 0; i--) {
174     cb_dim = ptr_cb_dimension_tbl[ptr_ext_sorted_cw[ext_sorted_cw_idx]];
175 
176     for (k = ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
177          k != 0; k--) {
178       loop_idx++;
179       if (loop_idx > 256) {
180         return;
181       }
182       *ptr_cb++ = ptr_ext_sorted_cw[ext_sorted_cw_idx];
183       *res_ptr_idx++ = quant_spec_coeff_idx;
184       quant_spec_coeff_idx += cb_dim;
185       if (quant_spec_coeff_idx >= 1024) {
186         return;
187       }
188     }
189     num_ext_sorted_cw_in_sect_idx++;
190     ext_sorted_cw_idx++;
191     if (num_ext_sorted_cw_in_sect_idx >= (MAX_SFB_HCR + MAX_HCR_SETS) ||
192         ext_sorted_cw_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
193       return;
194     }
195   }
196   num_ext_sorted_sect_in_sets_idx++;
197   if (num_ext_sorted_cw_in_sect_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
198     return;
199   }
200 
201   ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx =
202       num_ext_sorted_cw_in_sect_idx;
203   ptr_hcr_info->sect_info.ext_sorted_cw_idx = ext_sorted_cw_idx;
204   ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx =
205       num_ext_sorted_sect_in_sets_idx;
206   ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx =
207       num_ext_sorted_cw_in_sect_idx;
208   ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = quant_spec_coeff_idx;
209 }
210 
ixheaacd_calc_num_ext_sorted_sect_sets(UWORD32 num_segment,UWORD16 * ptr_num_ext_sorted_cw_in_sect,WORD32 num_ext_sorted_cw_in_sect_idx,UWORD16 * ptr_num_ext_sorted_sect_in_sets,WORD32 num_ext_sorted_sect_in_sets_idx)211 static VOID ixheaacd_calc_num_ext_sorted_sect_sets(
212     UWORD32 num_segment, UWORD16 *ptr_num_ext_sorted_cw_in_sect,
213     WORD32 num_ext_sorted_cw_in_sect_idx,
214     UWORD16 *ptr_num_ext_sorted_sect_in_sets,
215     WORD32 num_ext_sorted_sect_in_sets_idx) {
216   UWORD16 counter = 0;
217   UWORD32 cw_sum = 0;
218   UWORD16 *ptr_num_ext_sort_cw_in_sect = ptr_num_ext_sorted_cw_in_sect;
219   UWORD16 *ptr_num_ext_sort_sect_in_sets = ptr_num_ext_sorted_sect_in_sets;
220 
221   while (ptr_num_ext_sort_cw_in_sect[num_ext_sorted_cw_in_sect_idx] != 0) {
222     cw_sum += ptr_num_ext_sort_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
223     num_ext_sorted_cw_in_sect_idx++;
224     if (num_ext_sorted_cw_in_sect_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
225       return;
226     }
227     if (cw_sum > num_segment) {
228       return;
229     }
230     counter++;
231     if (counter > 256) {
232       return;
233     }
234     if (cw_sum == num_segment) {
235       ptr_num_ext_sort_sect_in_sets[num_ext_sorted_sect_in_sets_idx] = counter;
236       num_ext_sorted_sect_in_sets_idx++;
237       if (num_ext_sorted_sect_in_sets_idx >= MAX_HCR_SETS) {
238         return;
239       }
240       counter = 0;
241       cw_sum = 0;
242     }
243   }
244   ptr_num_ext_sort_sect_in_sets[num_ext_sorted_sect_in_sets_idx] = counter;
245 }
246 
ixheaacd_validate_hcr_sideinfo(WORD8 cb,WORD32 num_line,UWORD32 * error_word)247 static VOID ixheaacd_validate_hcr_sideinfo(WORD8 cb, WORD32 num_line,
248                                            UWORD32 *error_word) {
249   if (cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == (ESC_HCB + 1)) {
250     *error_word |= (ERROR_POS << 4);
251   }
252   if (num_line < 0 || num_line > 1024) {
253     *error_word |= (ERROR_POS << 5);
254   }
255 }
256 
ixheaacd_validate_hcr_lengths(WORD8 longest_cw_len,WORD16 reordered_spec_data_len,UWORD32 * error_word)257 static VOID ixheaacd_validate_hcr_lengths(WORD8 longest_cw_len,
258                                           WORD16 reordered_spec_data_len,
259                                           UWORD32 *error_word) {
260   if (reordered_spec_data_len < longest_cw_len) {
261     *error_word |= (ERROR_POS << 8);
262   }
263 }
264 
ixheaacd_huff_code_reorder_init(ia_hcr_info_struct * ptr_hcr_info,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables,ia_bit_buf_struct * itt_bit_buff)265 UWORD32 ixheaacd_huff_code_reorder_init(
266     ia_hcr_info_struct *ptr_hcr_info,
267     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
268     ia_aac_dec_tables_struct *ptr_aac_tables, ia_bit_buf_struct *itt_bit_buff) {
269   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
270   WORD16 *ptr_num_sect_lines;
271   UWORD8 *ptr_cb;
272   WORD16 num_sect;
273   WORD8 cb;
274   WORD32 num_line;
275   WORD32 i;
276 
277   ptr_hcr_info->str_dec_io.reordered_spec_data_len =
278       ptr_aac_dec_channel_info->reorder_spect_data_len;
279   ptr_hcr_info->str_dec_io.longest_cw_len =
280       ptr_aac_dec_channel_info->longest_cw_len;
281   ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base =
282       ptr_aac_dec_channel_info->ptr_spec_coeff;
283   ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = 0;
284   ptr_hcr_info->str_dec_io.ptr_cb = ptr_aac_dec_channel_info->cb4_hcr_arr;
285   ptr_hcr_info->str_dec_io.ptr_num_line_in_sect =
286       ptr_aac_dec_channel_info->num_line_in_sec4_hcr_arr;
287   ptr_hcr_info->str_dec_io.num_sect = ptr_aac_dec_channel_info->number_sect;
288   ptr_hcr_info->str_dec_io.err_log = 0;
289   ptr_hcr_info->str_non_pcw_side_info.ptr_result_base =
290       ptr_aac_dec_channel_info->ptr_spec_coeff;
291 
292   ptr_hcr_info->str_dec_io.bit_str_idx =
293       itt_bit_buff->size - itt_bit_buff->cnt_bits;
294   itt_bit_buff->byte_ptr = (UWORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
295   itt_bit_buff->ptr_start = (UWORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
296 
297   if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
298       EIGHT_SHORT_SEQUENCE) {
299     WORD16 band;
300     WORD16 max_band;
301     WORD8 group;
302     WORD8 win_group_len;
303     WORD8 window;
304     WORD8 num_unit_in_band;
305     WORD8 cnt_unit_in_band;
306     WORD8 grp_win;
307     WORD8 cb_prev;
308 
309     WORD8 *ptr_code_book;
310     const WORD16 *band_offsets;
311     WORD16 num_groups;
312 
313     ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
314     ptr_num_sect_lines = ptr_hcr_info->str_dec_io.ptr_num_line_in_sect;
315     ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
316     band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(ptr_ics_info,
317                                                                 ptr_aac_tables);
318     num_groups = ptr_ics_info->num_window_groups;
319 
320     num_line = 0;
321     num_sect = 0;
322     cb = ptr_code_book[0];
323     cb_prev = ptr_code_book[0];
324 
325     *ptr_cb++ = cb_prev;
326 
327     max_band = ptr_ics_info->max_sfb;
328     for (band = 0; band < max_band; band++) {
329       num_unit_in_band = ((band_offsets[band + 1] - band_offsets[band]) >> 2);
330       for (cnt_unit_in_band = num_unit_in_band; cnt_unit_in_band != 0;
331            cnt_unit_in_band--) {
332         for (window = 0, group = 0; group < num_groups; group++) {
333           win_group_len = ptr_ics_info->window_group_length[group];
334           for (grp_win = win_group_len; grp_win != 0; grp_win--, window++) {
335             cb = ptr_code_book[group * 16 + band];
336             if (cb != cb_prev) {
337               ixheaacd_validate_hcr_sideinfo(cb, num_line,
338                                              &ptr_hcr_info->str_dec_io.err_log);
339               if (ptr_hcr_info->str_dec_io.err_log != 0) {
340                 return (ptr_hcr_info->str_dec_io.err_log);
341               }
342               *ptr_cb++ = cb;
343               *ptr_num_sect_lines++ = num_line;
344               num_sect++;
345 
346               cb_prev = cb;
347               num_line = LINES_PER_UNIT;
348             } else {
349               num_line += LINES_PER_UNIT;
350             }
351           }
352         }
353       }
354     }
355 
356     num_sect++;
357 
358     ixheaacd_validate_hcr_sideinfo(cb, num_line,
359                                    &ptr_hcr_info->str_dec_io.err_log);
360     if (num_sect <= 0 || num_sect > 1024 / 2) {
361       ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 7);
362     }
363     ixheaacd_validate_hcr_lengths(
364         ptr_hcr_info->str_dec_io.longest_cw_len,
365         ptr_hcr_info->str_dec_io.reordered_spec_data_len,
366         &ptr_hcr_info->str_dec_io.err_log);
367     if (ptr_hcr_info->str_dec_io.err_log != 0) {
368       return (ptr_hcr_info->str_dec_io.err_log);
369     }
370 
371     *ptr_cb = cb;
372     *ptr_num_sect_lines = num_line;
373     ptr_hcr_info->str_dec_io.num_sect = num_sect;
374 
375   } else {
376     ixheaacd_validate_hcr_lengths(
377         ptr_hcr_info->str_dec_io.longest_cw_len,
378         ptr_hcr_info->str_dec_io.reordered_spec_data_len,
379         &ptr_hcr_info->str_dec_io.err_log);
380     num_sect = ptr_hcr_info->str_dec_io.num_sect;
381     ptr_num_sect_lines = ptr_hcr_info->str_dec_io.ptr_num_line_in_sect;
382     ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
383     if (num_sect <= 0 || num_sect > 64) {
384       ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 6);
385       num_sect = 0;
386     }
387 
388     for (i = num_sect; i != 0; i--) {
389       cb = *ptr_cb++;
390 
391       if (cb < ZERO_HCB || cb >= MAX_CB_CHECK || cb == (ESC_HCB + 1)) {
392         ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 2);
393       }
394 
395       num_line = *ptr_num_sect_lines++;
396 
397       if ((num_line <= 0) || (num_line > 1024)) {
398         ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 3);
399       }
400     }
401     if (ptr_hcr_info->str_dec_io.err_log != 0) {
402       return (ptr_hcr_info->str_dec_io.err_log);
403     }
404   }
405 
406   ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
407   for (i = 0; i < num_sect; i++) {
408     if ((*ptr_cb == NOISE_HCB) || (*ptr_cb == INTENSITY_HCB2) ||
409         (*ptr_cb == INTENSITY_HCB)) {
410       *ptr_cb = 0;
411     }
412     ptr_cb++;
413   }
414 
415   return (ptr_hcr_info->str_dec_io.err_log);
416 }
417 
ixheaacd_huff_calc_num_cwd(ia_hcr_info_struct * ptr_hcr_info)418 static VOID ixheaacd_huff_calc_num_cwd(ia_hcr_info_struct *ptr_hcr_info) {
419   WORD32 sect_idx;
420   UWORD32 num_code_word;
421 
422   UWORD32 num_sect = ptr_hcr_info->str_dec_io.num_sect;
423   UWORD8 *ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
424   WORD16 *ptr_num_line_in_sect = ptr_hcr_info->str_dec_io.ptr_num_line_in_sect;
425   const UWORD8 *ptr_cb_dim_shift_tbl =
426       ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl;
427   UWORD16 *ptr_num_cw_in_sect = ptr_hcr_info->sect_info.ptr_num_cw_in_sect;
428 
429   num_code_word = 0;
430   for (sect_idx = num_sect; sect_idx != 0; sect_idx--) {
431     *ptr_num_cw_in_sect =
432         *ptr_num_line_in_sect++ >> ptr_cb_dim_shift_tbl[*ptr_cb];
433     if (*ptr_cb != 0) {
434       num_code_word += *ptr_num_cw_in_sect;
435     }
436     ptr_num_cw_in_sect++;
437     ptr_cb++;
438   }
439   ptr_hcr_info->sect_info.num_code_word = num_code_word;
440 }
441 
ixheaacd_huff_sort_sect_cb_cwd(ia_hcr_info_struct * ptr_hcr_info)442 static VOID ixheaacd_huff_sort_sect_cb_cwd(ia_hcr_info_struct *ptr_hcr_info) {
443   UWORD32 i, j, k;
444   UWORD8 temp;
445   UWORD32 counter;
446   UWORD32 start_offset;
447   UWORD32 num_zero_sect;
448   UWORD8 *ptr_dest;
449   UWORD32 num_sect_dec;
450 
451   UWORD32 num_sect = ptr_hcr_info->str_dec_io.num_sect;
452   UWORD8 *ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
453   UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
454   UWORD16 *ptr_num_cw_in_sect = ptr_hcr_info->sect_info.ptr_num_cw_in_sect;
455   UWORD16 *ptr_num_sorted_cw_in_sect =
456       ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
457   UWORD8 *ptr_cb_switch = ptr_hcr_info->sect_info.ptr_cb_switch;
458   UWORD16 *ptr_reorder_offset = ptr_hcr_info->sect_info.ptr_reorder_offset;
459   const UWORD8 *ptr_cb_priority = ptr_hcr_info->table_info.ptr_cb_priority;
460   const UWORD8 *ptr_min_cb_pair_tbl =
461       ptr_hcr_info->codebook_pairs.ptr_min_cb_pair_tbl;
462   const UWORD8 *ptr_max_cb_pair_tbl =
463       ptr_hcr_info->codebook_pairs.ptr_max_cb_pair_tbl;
464   const UWORD8 *ptr_cb_dim_shift_tbl =
465       ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl;
466 
467   UWORD32 search_start_idx = 0;
468 
469   ptr_dest = ptr_sorted_cb;
470   num_zero_sect = 0;
471   for (i = num_sect; i != 0; i--) {
472     if (ptr_cb_priority[*ptr_cb] == 0) {
473       num_zero_sect += 1;
474     }
475     *ptr_dest++ = ptr_cb_priority[*ptr_cb++];
476   }
477   ptr_hcr_info->sect_info.num_sorted_section = num_sect - num_zero_sect;
478   ptr_cb = ptr_hcr_info->str_dec_io.ptr_cb;
479 
480   num_sect_dec = num_sect - 1;
481   if (num_sect_dec > 0) {
482     counter = num_sect_dec;
483     for (j = num_sect_dec; j != 0; j--) {
484       for (i = 0; i < counter; i++) {
485         if (ptr_sorted_cb[i + 1] > ptr_sorted_cb[i]) {
486           temp = ptr_sorted_cb[i];
487           ptr_sorted_cb[i] = ptr_sorted_cb[i + 1];
488           ptr_sorted_cb[i + 1] = temp;
489         }
490       }
491       counter -= 1;
492     }
493   }
494 
495   for (i = num_sect; i != 0; i--) {
496     *ptr_cb_switch++ = 0;
497   }
498   ptr_cb_switch = ptr_hcr_info->sect_info.ptr_cb_switch;
499 
500   for (j = 0; j < num_sect; j++) {
501     for (i = search_start_idx; i < num_sect; i++) {
502       if (ptr_cb_switch[i] == 0 &&
503           (ptr_min_cb_pair_tbl[ptr_sorted_cb[j]] == ptr_cb[i] ||
504            ptr_max_cb_pair_tbl[ptr_sorted_cb[j]] == ptr_cb[i])) {
505         ptr_cb_switch[i] = 1;
506         ptr_sorted_cb[j] = ptr_cb[i];
507         ptr_num_sorted_cw_in_sect[j] = ptr_num_cw_in_sect[i];
508 
509         start_offset = 0;
510         for (k = 0; k < i; k++) {
511           start_offset += ptr_num_cw_in_sect[k]
512                           << ptr_cb_dim_shift_tbl[ptr_cb[k]];
513         }
514         ptr_reorder_offset[j] = start_offset;
515 
516         if (i == search_start_idx) {
517           UWORD32 k = i;
518           while (ptr_cb_switch[k++] == 1) search_start_idx++;
519         }
520         break;
521       }
522     }
523   }
524 }
525 
ixheaacd_huff_ext_sect_info(ia_hcr_info_struct * ptr_hcr_info)526 static VOID ixheaacd_huff_ext_sect_info(ia_hcr_info_struct *ptr_hcr_info) {
527   UWORD32 srt_sec_cnt = 0;
528   UWORD32 x_srt_sc_cnt = 0;
529   UWORD32 remain_num_cw_sort_sec;
530   UWORD32 in_segment_remain_num_cw;
531 
532   UWORD32 num_sorted_section = ptr_hcr_info->sect_info.num_sorted_section;
533   UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
534   UWORD16 *ptr_num_sorted_cw_in_sect =
535       ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
536   UWORD8 *ptr_extended_sorted_code_book =
537       ptr_hcr_info->sect_info.ptr_ext_sorted_cw;
538   UWORD16 *ptr_num_ext_sort_cw_sect =
539       ptr_hcr_info->sect_info.ptr_num_ext_sorted_cw_in_sect;
540   UWORD32 num_segment = ptr_hcr_info->str_segment_info.num_segment;
541   UWORD8 *ptr_ext_sorted_sect_max_cb_len =
542       ptr_hcr_info->sect_info.ptr_ext_sorted_sect_max_cb_len;
543   WORD8 longest_cw_len = ptr_hcr_info->str_dec_io.longest_cw_len;
544   const UWORD8 *ptr_max_cw_len_tbl =
545       ptr_hcr_info->table_info.ptr_max_cw_len_tbl;
546 
547   remain_num_cw_sort_sec = ptr_num_sorted_cw_in_sect[srt_sec_cnt];
548   in_segment_remain_num_cw = num_segment;
549 
550   while (srt_sec_cnt < num_sorted_section) {
551     if (in_segment_remain_num_cw < remain_num_cw_sort_sec) {
552       ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = in_segment_remain_num_cw;
553       ptr_extended_sorted_code_book[x_srt_sc_cnt] = ptr_sorted_cb[srt_sec_cnt];
554 
555       remain_num_cw_sort_sec -= in_segment_remain_num_cw;
556       in_segment_remain_num_cw = num_segment;
557     } else if (in_segment_remain_num_cw == remain_num_cw_sort_sec) {
558       ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = in_segment_remain_num_cw;
559       ptr_extended_sorted_code_book[x_srt_sc_cnt] = ptr_sorted_cb[srt_sec_cnt];
560 
561       srt_sec_cnt++;
562       remain_num_cw_sort_sec = ptr_num_sorted_cw_in_sect[srt_sec_cnt];
563       in_segment_remain_num_cw = num_segment;
564     } else {
565       ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = remain_num_cw_sort_sec;
566       ptr_extended_sorted_code_book[x_srt_sc_cnt] = ptr_sorted_cb[srt_sec_cnt];
567 
568       in_segment_remain_num_cw -= remain_num_cw_sort_sec;
569       srt_sec_cnt++;
570       remain_num_cw_sort_sec = ptr_num_sorted_cw_in_sect[srt_sec_cnt];
571     }
572     ptr_ext_sorted_sect_max_cb_len[x_srt_sc_cnt] =
573         min(ptr_max_cw_len_tbl[ptr_extended_sorted_code_book[x_srt_sc_cnt]],
574             longest_cw_len);
575 
576     x_srt_sc_cnt += 1;
577 
578     if (x_srt_sc_cnt >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
579       ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 28);
580       return;
581     }
582   }
583   ptr_num_ext_sort_cw_sect[x_srt_sc_cnt] = 0;
584 }
585 
ixheaacd_hcr_prepare_segmentation_grid(ia_hcr_info_struct * ptr_hcr_info)586 static UWORD32 ixheaacd_hcr_prepare_segmentation_grid(
587     ia_hcr_info_struct *ptr_hcr_info) {
588   UWORD16 i, j;
589   UWORD16 num_segment = 0;
590   UWORD16 segment_start = 0;
591   UWORD8 segment_width;
592   UWORD8 last_segment_width;
593   UWORD8 sorted_code_book;
594   UWORD8 end_flag = 0;
595   UWORD16 intermediate_result;
596 
597   WORD8 longest_cw_len = ptr_hcr_info->str_dec_io.longest_cw_len;
598   WORD16 reordered_spec_data_len =
599       ptr_hcr_info->str_dec_io.reordered_spec_data_len;
600   UWORD32 num_sorted_section = ptr_hcr_info->sect_info.num_sorted_section;
601   UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
602   UWORD16 *ptr_num_sorted_cw_in_sect =
603       ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
604   UWORD16 *arr_seg_start_l = ptr_hcr_info->str_segment_info.arr_seg_start_l;
605   UWORD16 *arr_seg_start_r = ptr_hcr_info->str_segment_info.arr_seg_start_r;
606   WORD8 *p_remaining_bits_in_seg =
607       ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
608   UWORD16 bit_str_idx = ptr_hcr_info->str_dec_io.bit_str_idx;
609   const UWORD8 *ptr_max_cw_len_tbl =
610       ptr_hcr_info->table_info.ptr_max_cw_len_tbl;
611 
612   for (i = num_sorted_section; i != 0; i--) {
613     sorted_code_book = *ptr_sorted_cb++;
614     segment_width = min(ptr_max_cw_len_tbl[sorted_code_book], longest_cw_len);
615 
616     for (j = *ptr_num_sorted_cw_in_sect; j != 0; j--) {
617       intermediate_result = bit_str_idx + segment_start;
618       if ((segment_start + segment_width) <= reordered_spec_data_len) {
619         *arr_seg_start_l++ = intermediate_result;
620         *arr_seg_start_r++ = intermediate_result + segment_width - 1;
621         *p_remaining_bits_in_seg++ = segment_width;
622         segment_start += segment_width;
623         num_segment += 1;
624       } else {
625         arr_seg_start_l--;
626         arr_seg_start_r--;
627         p_remaining_bits_in_seg--;
628         segment_start = *arr_seg_start_l - bit_str_idx;
629 
630         last_segment_width = reordered_spec_data_len - segment_start;
631         *p_remaining_bits_in_seg = last_segment_width;
632         *arr_seg_start_r = bit_str_idx + segment_start + last_segment_width - 1;
633         end_flag = 1;
634         break;
635       }
636     }
637     ptr_num_sorted_cw_in_sect++;
638     if (end_flag != 0) {
639       break;
640     }
641   }
642 
643   if (num_segment == 0) ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 9);
644 
645   ptr_hcr_info->str_segment_info.num_segment = num_segment;
646 
647   return (ptr_hcr_info->str_dec_io.err_log);
648 }
649 
ixheaacd_huff_dec_pair_hcr_pcw(ia_hcr_info_struct * ptr_hcr_info,ia_bit_buf_struct * it_bit_buff,WORD no_bands,const UWORD16 * code_book_tbl,WORD32 * read_word,WORD32 tbl_sign,const UWORD32 * idx_table,UWORD16 * arr_seg_start_l,WORD32 * read_bits,WORD32 huff_mode,WORD8 * p_remaining_bits_in_seg,WORD32 * ptr_num_decoded_bits)650 static PLATFORM_INLINE UWORD16 *ixheaacd_huff_dec_pair_hcr_pcw(
651     ia_hcr_info_struct *ptr_hcr_info, ia_bit_buf_struct *it_bit_buff,
652     WORD no_bands, const UWORD16 *code_book_tbl, WORD32 *read_word,
653     WORD32 tbl_sign, const UWORD32 *idx_table, UWORD16 *arr_seg_start_l,
654     WORD32 *read_bits, WORD32 huff_mode, WORD8 *p_remaining_bits_in_seg,
655     WORD32 *ptr_num_decoded_bits)
656 
657 {
658   WORD32 spec_index = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
659   WORD32 *spec_coef =
660       ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base + spec_index;
661   WORD16 index, length;
662   WORD32 y, z;
663   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
664   WORD32 *bit_pos = &it_bit_buff->bit_pos;
665 
666   do {
667     UWORD32 read_word1;
668 
669     WORD32 read_bit_offset =
670         *arr_seg_start_l - (it_bit_buff->size - *read_bits);
671 
672     if (read_bit_offset) {
673       *read_bits -= read_bit_offset;
674       *bit_pos += read_bit_offset;
675       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
676                                   it_bit_buff->ptr_bit_buf_end);
677     }
678 
679     read_word1 = *read_word << *bit_pos;
680     ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
681                             idx_table);
682     *bit_pos += length;
683     *ptr_num_decoded_bits += length;
684     *p_remaining_bits_in_seg -= length;
685     *arr_seg_start_l += length;
686     *read_bits -= length;
687     ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
688                                 it_bit_buff->ptr_bit_buf_end);
689     if (tbl_sign) {
690       WORD32 temp_word;
691       temp_word = *read_word << *bit_pos;
692       y = index / huff_mode;
693       z = index - huff_mode * y;
694 
695       if (y) {
696         if (temp_word & 0x80000000) y = -y;
697 
698         temp_word = temp_word << 1;
699         *bit_pos += 1;
700         *p_remaining_bits_in_seg -= 1;
701         *ptr_num_decoded_bits += 1;
702         *arr_seg_start_l += 1;
703         *read_bits -= 1;
704       }
705       *spec_coef++ = y;
706       spec_index++;
707 
708       if (z) {
709         if (temp_word & 0x80000000) {
710           z = -z;
711         }
712         temp_word <<= 1;
713         *bit_pos += 1;
714         *p_remaining_bits_in_seg -= 1;
715         *ptr_num_decoded_bits += 1;
716         *arr_seg_start_l += 1;
717         *read_bits -= 1;
718       }
719       *spec_coef++ = z;
720       spec_index++;
721     } else {
722       y = (index / huff_mode) - 4;
723       z = index - ((y + 4) * huff_mode) - 4;
724 
725       *spec_coef++ = y;
726       *spec_coef++ = z;
727       spec_index += 2;
728     }
729     ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
730                                 it_bit_buff->ptr_bit_buf_end);
731     no_bands--;
732     arr_seg_start_l++;
733     p_remaining_bits_in_seg++;
734   } while (no_bands != 0);
735 
736   it_bit_buff->ptr_read_next = ptr_read_next;
737   ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = spec_index;
738 
739   return arr_seg_start_l;
740 }
741 
ixheaacd_huff_dec_pair_hcr_non_pcw(ia_bit_buf_struct * itt_bit_buff,WORD32 * spec_coef,const UWORD16 * code_book_tbl,WORD32 tbl_sign,const UWORD32 * idx_table,WORD32 huff_mode)742 static PLATFORM_INLINE WORD16 ixheaacd_huff_dec_pair_hcr_non_pcw(
743     ia_bit_buf_struct *itt_bit_buff, WORD32 *spec_coef,
744     const UWORD16 *code_book_tbl, WORD32 tbl_sign, const UWORD32 *idx_table,
745     WORD32 huff_mode)
746 
747 {
748   WORD16 index, length;
749   WORD32 y, z;
750   WORD32 read_word1;
751   WORD32 read_word;
752 
753   read_word = ixheaacd_aac_showbits_32(itt_bit_buff->byte_ptr,
754                                        itt_bit_buff->bit_count, NULL);
755 
756   ixheaacd_huffman_decode(read_word, &index, &length, code_book_tbl, idx_table);
757   read_word1 = read_word << length;
758   if (tbl_sign) {
759     WORD32 temp_word;
760     temp_word = read_word1;
761     y = index / huff_mode;
762     z = index - huff_mode * y;
763 
764     if (y) {
765       if (temp_word & 0x80000000) y = -y;
766 
767       temp_word = temp_word << 1;
768       length++;
769     }
770     *spec_coef++ = y;
771 
772     if (z) {
773       if (temp_word & 0x80000000) {
774         z = -z;
775       }
776       temp_word <<= 1;
777       length++;
778     }
779     *spec_coef++ = z;
780   } else {
781     y = (index / huff_mode) - 4;
782     z = index - ((y + 4) * huff_mode) - 4;
783 
784     *spec_coef++ = y;
785     *spec_coef++ = z;
786   }
787 
788   return length;
789 }
790 
ixheaacd_huff_dec_quad_hcr_pcw(ia_hcr_info_struct * ptr_hcr_info,ia_bit_buf_struct * it_bit_buff,WORD no_bands,const UWORD16 * code_book_tbl,WORD32 tbl_sign,const UWORD32 * idx_table,WORD32 * read_word,WORD32 * read_bits,UWORD16 * arr_seg_start_l,WORD8 * p_remaining_bits_in_seg,WORD32 * ptr_num_decoded_bits)791 static PLATFORM_INLINE UWORD16 *ixheaacd_huff_dec_quad_hcr_pcw(
792     ia_hcr_info_struct *ptr_hcr_info, ia_bit_buf_struct *it_bit_buff,
793     WORD no_bands, const UWORD16 *code_book_tbl, WORD32 tbl_sign,
794     const UWORD32 *idx_table, WORD32 *read_word, WORD32 *read_bits,
795     UWORD16 *arr_seg_start_l, WORD8 *p_remaining_bits_in_seg,
796     WORD32 *ptr_num_decoded_bits) {
797   WORD16 index, length;
798 
799   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
800   WORD32 spec_index = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
801   WORD32 *spec_coef =
802       ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base + spec_index;
803   WORD32 *bit_pos = &it_bit_buff->bit_pos;
804 
805   do {
806     UWORD32 read_word1;
807 
808     WORD32 read_bit_offset =
809         *arr_seg_start_l - (it_bit_buff->size - *read_bits);
810 
811     if (read_bit_offset) {
812       *read_bits -= read_bit_offset;
813       *bit_pos += read_bit_offset;
814       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
815                                   it_bit_buff->ptr_bit_buf_end);
816     }
817 
818     read_word1 = *read_word << *bit_pos;
819     ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
820                             idx_table);
821     *bit_pos += length;
822     *p_remaining_bits_in_seg -= length;
823     *read_bits -= length;
824     *ptr_num_decoded_bits += length;
825     *arr_seg_start_l += length;
826     ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
827                                 it_bit_buff->ptr_bit_buf_end);
828     if (tbl_sign) {
829       WORD32 temp_word;
830       WORD32 w, x, y, z;
831       temp_word = *read_word << *bit_pos;
832       w = index / 27;
833       index = index - w * 27;
834       x = index / 9;
835       index = index - x * 9;
836       y = index / 3;
837       z = index - y * 3;
838       if (w) {
839         if (temp_word & 0x80000000) w = -w;
840         temp_word <<= 1;
841         *bit_pos += 1;
842         *p_remaining_bits_in_seg -= 1;
843         *read_bits -= 1;
844         *ptr_num_decoded_bits += 1;
845         *arr_seg_start_l += 1;
846       }
847       *spec_coef++ = w;
848       spec_index++;
849 
850       if (x) {
851         if (temp_word & 0x80000000) x = -x;
852         temp_word <<= 1;
853         *bit_pos += 1;
854         *p_remaining_bits_in_seg -= 1;
855         *read_bits -= 1;
856         *ptr_num_decoded_bits += 1;
857         *arr_seg_start_l += 1;
858       }
859       *spec_coef++ = x;
860       spec_index++;
861       if (y) {
862         if (temp_word & 0x80000000) y = -y;
863         temp_word <<= 1;
864         *bit_pos += 1;
865         *p_remaining_bits_in_seg -= 1;
866         *read_bits -= 1;
867         *ptr_num_decoded_bits += 1;
868         *arr_seg_start_l += 1;
869       }
870       *spec_coef++ = y;
871       spec_index++;
872       if (z) {
873         if (temp_word & 0x80000000) z = -z;
874         temp_word <<= 1;
875         *bit_pos += 1;
876         *p_remaining_bits_in_seg -= 1;
877         *read_bits -= 1;
878         *ptr_num_decoded_bits += 1;
879         *arr_seg_start_l += 1;
880       }
881       *spec_coef++ = z;
882       spec_index++;
883 
884     }
885 
886     else {
887       WORD32 w, x, y, z;
888 
889       w = index / 27 - 1;
890       index = index - (w + 1) * 27;
891       x = index / 9 - 1;
892       index = index - (x + 1) * 9;
893       y = index / 3 - 1;
894       z = index - ((y + 1) * 3) - 1;
895       *spec_coef++ = w;
896 
897       *spec_coef++ = x;
898 
899       *spec_coef++ = y;
900 
901       *spec_coef++ = z;
902       spec_index += 4;
903     }
904 
905     ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
906                                 it_bit_buff->ptr_bit_buf_end);
907     arr_seg_start_l++;
908     p_remaining_bits_in_seg++;
909     no_bands--;
910   } while (no_bands != 0);
911 
912   it_bit_buff->ptr_read_next = ptr_read_next;
913   ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = spec_index;
914 
915   return arr_seg_start_l;
916 }
917 
ixheaacd_huff_dec_word_hcr_pcw(ia_hcr_info_struct * ptr_hcr_info,ia_bit_buf_struct * it_bit_buff,WORD no_bands,const UWORD16 * code_book_tbl,WORD32 * read_word,const UWORD32 * idx_table,UWORD16 * arr_seg_start_l,WORD32 * read_bits,WORD8 * p_remaining_bits_in_seg,WORD32 * ptr_num_decoded_bits)918 static UWORD16 *ixheaacd_huff_dec_word_hcr_pcw(
919     ia_hcr_info_struct *ptr_hcr_info, ia_bit_buf_struct *it_bit_buff,
920     WORD no_bands, const UWORD16 *code_book_tbl, WORD32 *read_word,
921     const UWORD32 *idx_table, UWORD16 *arr_seg_start_l, WORD32 *read_bits,
922     WORD8 *p_remaining_bits_in_seg, WORD32 *ptr_num_decoded_bits) {
923   WORD32 sp1, sp2;
924   WORD32 flush_cw;
925   WORD32 i, value, norm_val, off;
926   WORD32 out1, out2;
927   WORD16 index;
928   WORD32 length;
929   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
930   WORD32 spec_index = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
931   WORD32 *spec_coef =
932       ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base + spec_index;
933   WORD32 *bit_pos = &it_bit_buff->bit_pos;
934 
935   do {
936     UWORD32 read_word1;
937 
938     WORD32 read_bit_offset =
939         *arr_seg_start_l - (it_bit_buff->size - *read_bits);
940 
941     if (read_bit_offset) {
942       *read_bits -= read_bit_offset;
943       *bit_pos += read_bit_offset;
944       ixheaacd_aac_read_byte_corr1(&ptr_read_next, bit_pos, read_word,
945                                    it_bit_buff->ptr_bit_buf_end);
946     }
947 
948     read_word1 = *read_word << *bit_pos;
949     ixheaacd_huff_sfb_table(read_word1, &index, &length, code_book_tbl,
950                             idx_table);
951     *bit_pos += length;
952     *read_bits -= length;
953     *arr_seg_start_l += length;
954     *p_remaining_bits_in_seg -= length;
955     *ptr_num_decoded_bits += length;
956     ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
957                                 it_bit_buff->ptr_bit_buf_end);
958 
959     out1 = index / 17;
960     out2 = index - out1 * 17;
961     flush_cw = *read_word << *bit_pos;
962 
963     sp1 = out1;
964     sp2 = out2;
965 
966     if (out1) {
967       if (flush_cw & 0x80000000) {
968         out1 = -out1;
969       }
970       *bit_pos += 1;
971       *read_bits -= 1;
972       *p_remaining_bits_in_seg -= 1;
973       *ptr_num_decoded_bits += 1;
974       *arr_seg_start_l += 1;
975       flush_cw = (WORD32)flush_cw << 1;
976     }
977 
978     if (out2) {
979       *bit_pos += 1;
980       *read_bits -= 1;
981       *p_remaining_bits_in_seg -= 1;
982       *ptr_num_decoded_bits += 1;
983       *arr_seg_start_l += 1;
984       if (flush_cw & 0x80000000) {
985         out2 = -out2;
986       }
987     }
988 
989     ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
990                                 it_bit_buff->ptr_bit_buf_end);
991 
992     if (sp1 == 16) {
993       i = 4;
994       value = ixheaacd_extu(*read_word, *bit_pos, 23);
995       value = value | 0xfffffe00;
996       norm_val = ixheaacd_norm32(value);
997 
998       i += (norm_val - 22);
999       *bit_pos += (norm_val - 21);
1000       *p_remaining_bits_in_seg -= (norm_val - 21);
1001       *ptr_num_decoded_bits += (norm_val - 21);
1002       *read_bits -= (norm_val - 21);
1003       *arr_seg_start_l += (norm_val - 21);
1004 
1005       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1006                                   it_bit_buff->ptr_bit_buf_end);
1007 
1008       off = ixheaacd_extu(*read_word, *bit_pos, 32 - i);
1009 
1010       *bit_pos += i;
1011       *p_remaining_bits_in_seg -= i;
1012       *ptr_num_decoded_bits += i;
1013       *read_bits -= i;
1014       *arr_seg_start_l += i;
1015 
1016       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1017                                   it_bit_buff->ptr_bit_buf_end);
1018       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1019                                   it_bit_buff->ptr_bit_buf_end);
1020 
1021       i = off + ((WORD32)1 << i);
1022 
1023       if (out1 < 0)
1024         *spec_coef++ = -i;
1025       else
1026         *spec_coef++ = i;
1027       spec_index++;
1028     } else {
1029       *spec_coef++ = out1;
1030       spec_index++;
1031     }
1032 
1033     if (sp2 == 16) {
1034       i = 4;
1035       value = ixheaacd_extu(*read_word, *bit_pos, 23);
1036       value = value | 0xfffffe00;
1037       norm_val = ixheaacd_norm32(value);
1038 
1039       i += (norm_val - 22);
1040 
1041       *bit_pos += (norm_val - 21);
1042       *read_bits -= (norm_val - 21);
1043       *p_remaining_bits_in_seg -= (norm_val - 21);
1044       *ptr_num_decoded_bits += (norm_val - 21);
1045       *arr_seg_start_l += (norm_val - 21);
1046       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1047                                   it_bit_buff->ptr_bit_buf_end);
1048 
1049       off = ixheaacd_extu(*read_word, *bit_pos, 32 - i);
1050 
1051       *bit_pos += i;
1052       *p_remaining_bits_in_seg -= i;
1053       *ptr_num_decoded_bits += i;
1054       *read_bits -= i;
1055       *arr_seg_start_l += i;
1056 
1057       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1058                                   it_bit_buff->ptr_bit_buf_end);
1059       ixheaacd_aac_read_byte_corr(&ptr_read_next, bit_pos, read_word,
1060                                   it_bit_buff->ptr_bit_buf_end);
1061 
1062       i = off + ((WORD32)1 << i);
1063 
1064       if (out2 < 0)
1065         *spec_coef++ = -i;
1066       else
1067         *spec_coef++ = i;
1068       spec_index++;
1069     } else {
1070       *spec_coef++ = out2;
1071       spec_index++;
1072     }
1073 
1074     arr_seg_start_l++;
1075     p_remaining_bits_in_seg++;
1076 
1077     no_bands--;
1078   } while (no_bands != 0);
1079 
1080   it_bit_buff->ptr_read_next = ptr_read_next;
1081   ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = spec_index;
1082 
1083   return arr_seg_start_l;
1084 }
1085 
ixheaacd_decode_pcw(ia_bit_buf_struct * itt_bit_buff,ia_hcr_info_struct * ptr_hcr_info,ia_aac_dec_tables_struct * ptr_aac_tables)1086 static VOID ixheaacd_decode_pcw(ia_bit_buf_struct *itt_bit_buff,
1087                                 ia_hcr_info_struct *ptr_hcr_info,
1088                                 ia_aac_dec_tables_struct *ptr_aac_tables) {
1089   UWORD16 ext_sort_sec;
1090   UWORD16 cur_ext_sort_cw_sec;
1091   UWORD8 codebook;
1092   UWORD8 dimension;
1093   WORD32 increment;
1094 
1095   WORD32 num_ext_sorted_cw_in_sect_idx =
1096       ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx;
1097   UWORD8 *ptr_ext_sorted_cw = ptr_hcr_info->sect_info.ptr_ext_sorted_cw;
1098   WORD32 ext_sorted_cw_idx = ptr_hcr_info->sect_info.ext_sorted_cw_idx;
1099   UWORD16 *ptr_num_ext_sorted_sect_in_sets =
1100       ptr_hcr_info->sect_info.ptr_num_ext_sorted_sect_in_sets;
1101   WORD32 num_ext_sorted_sect_in_sets_idx =
1102       ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx;
1103   WORD32 *ptr_quant_spec_coeff =
1104       ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
1105   UWORD16 *arr_seg_start_l = ptr_hcr_info->str_segment_info.arr_seg_start_l;
1106   WORD8 *p_remaining_bits_in_seg =
1107       ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
1108   UWORD8 *ptr_ext_sorted_sect_max_cb_len =
1109       ptr_hcr_info->sect_info.ptr_ext_sorted_sect_max_cb_len;
1110   WORD32 ext_sorted_sect_max_cb_len_idx =
1111       ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx;
1112   UWORD8 max_allowed_cw_len;
1113   WORD32 num_decoded_bits;
1114   const UWORD8 *ptr_cb_dimension_tbl =
1115       ptr_hcr_info->table_info.ptr_cb_dimension_tbl;
1116 
1117   WORD32 read_word = ixheaacd_aac_showbits_32(
1118       itt_bit_buff->ptr_read_next, itt_bit_buff->cnt_bits, &increment);
1119   WORD32 read_bits = itt_bit_buff->cnt_bits;
1120 
1121   itt_bit_buff->ptr_read_next += increment;
1122 
1123   for (ext_sort_sec =
1124            ptr_num_ext_sorted_sect_in_sets[num_ext_sorted_sect_in_sets_idx];
1125        ext_sort_sec != 0; ext_sort_sec--) {
1126     codebook = ptr_ext_sorted_cw[ext_sorted_cw_idx];
1127     if (codebook <= 0) return;
1128 
1129     ext_sorted_cw_idx++;
1130     if (ext_sorted_cw_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
1131       return;
1132     }
1133     dimension = ptr_cb_dimension_tbl[codebook];
1134     max_allowed_cw_len =
1135         ptr_ext_sorted_sect_max_cb_len[ext_sorted_sect_max_cb_len_idx];
1136     ext_sorted_sect_max_cb_len_idx++;
1137     if (ext_sorted_sect_max_cb_len_idx >= (MAX_SFB_HCR + MAX_HCR_SETS)) {
1138       return;
1139     }
1140 
1141     if (codebook <= 4) {
1142       WORD32 tbl_sign = 0;
1143       const UWORD16 *cb_table =
1144           (UWORD16 *)(ptr_aac_tables->code_book[codebook]);
1145       const UWORD32 *idx_table =
1146           (UWORD32 *)(ptr_aac_tables->index_table[codebook]);
1147 
1148       if (codebook > 2) {
1149         tbl_sign = 1;
1150       }
1151 
1152       {
1153         num_decoded_bits = 0;
1154         cur_ext_sort_cw_sec =
1155             ptr_hcr_info->sect_info
1156                 .ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
1157 
1158         arr_seg_start_l = ixheaacd_huff_dec_quad_hcr_pcw(
1159             ptr_hcr_info, itt_bit_buff, cur_ext_sort_cw_sec, cb_table, tbl_sign,
1160             idx_table, &read_word, &read_bits, arr_seg_start_l,
1161             p_remaining_bits_in_seg, &num_decoded_bits);
1162 
1163         p_remaining_bits_in_seg += cur_ext_sort_cw_sec;
1164 
1165         if (cur_ext_sort_cw_sec * max_allowed_cw_len < num_decoded_bits) {
1166           ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 19);
1167         }
1168 
1169         if (1 ==
1170             ixheaacd_err_detect_pcw_segment(
1171                 *p_remaining_bits_in_seg, ptr_hcr_info, PCW,
1172                 ptr_quant_spec_coeff +
1173                     ptr_hcr_info->str_dec_io.quant_spec_coeff_idx - dimension,
1174                 dimension)) {
1175           return;
1176         }
1177       }
1178     } else if (codebook < 11) {
1179       {
1180         WORD32 tbl_sign = 0;
1181         WORD32 huff_mode = 9;
1182         const UWORD16 *cb_table =
1183             (UWORD16 *)(ptr_aac_tables->code_book[codebook]);
1184         const UWORD32 *idx_table =
1185             (UWORD32 *)(ptr_aac_tables->index_table[codebook]);
1186         num_decoded_bits = 0;
1187 
1188         if (codebook > 6) {
1189           if (codebook > 8)
1190             huff_mode = 13;
1191           else
1192             huff_mode = 8;
1193           tbl_sign = 1;
1194         }
1195 
1196         cur_ext_sort_cw_sec =
1197             ptr_hcr_info->sect_info
1198                 .ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
1199 
1200         arr_seg_start_l = ixheaacd_huff_dec_pair_hcr_pcw(
1201             ptr_hcr_info, itt_bit_buff, cur_ext_sort_cw_sec, cb_table,
1202             &read_word, tbl_sign, idx_table, arr_seg_start_l, &read_bits,
1203             huff_mode, p_remaining_bits_in_seg, &num_decoded_bits);
1204 
1205         p_remaining_bits_in_seg += cur_ext_sort_cw_sec;
1206 
1207         if (cur_ext_sort_cw_sec * max_allowed_cw_len < num_decoded_bits) {
1208           ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 18);
1209         }
1210 
1211         if (1 ==
1212             ixheaacd_err_detect_pcw_segment(
1213                 *p_remaining_bits_in_seg, ptr_hcr_info, PCW_SIGN,
1214                 ptr_quant_spec_coeff +
1215                     ptr_hcr_info->str_dec_io.quant_spec_coeff_idx - dimension,
1216                 dimension)) {
1217           return;
1218         }
1219       }
1220     } else if ((codebook >= 11)) {
1221       const UWORD32 *idx_table =
1222           ptr_aac_tables->pstr_huffmann_tables->idx_table_hf11;
1223       const UWORD16 *cb_table =
1224           ptr_aac_tables->pstr_huffmann_tables->input_table_cb11;
1225       num_decoded_bits = 0;
1226 
1227       cur_ext_sort_cw_sec =
1228           ptr_hcr_info->sect_info
1229               .ptr_num_ext_sorted_cw_in_sect[num_ext_sorted_cw_in_sect_idx];
1230 
1231       arr_seg_start_l = ixheaacd_huff_dec_word_hcr_pcw(
1232           ptr_hcr_info, itt_bit_buff, cur_ext_sort_cw_sec, cb_table, &read_word,
1233           idx_table, arr_seg_start_l, &read_bits, p_remaining_bits_in_seg,
1234           &num_decoded_bits);
1235 
1236       p_remaining_bits_in_seg += cur_ext_sort_cw_sec;
1237 
1238       if (cur_ext_sort_cw_sec * max_allowed_cw_len < num_decoded_bits) {
1239         ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 17);
1240       }
1241 
1242       if (1 == ixheaacd_err_detect_pcw_segment(
1243                    *p_remaining_bits_in_seg, ptr_hcr_info, PCW_ESC_SIGN,
1244                    ptr_quant_spec_coeff +
1245                        ptr_hcr_info->str_dec_io.quant_spec_coeff_idx - 2,
1246                    2)) {
1247         return;
1248       }
1249     }
1250 
1251     num_ext_sorted_cw_in_sect_idx++;
1252     if (num_ext_sorted_cw_in_sect_idx >= MAX_SFB_HCR + MAX_HCR_SETS) {
1253       return;
1254     }
1255   }
1256 
1257   num_ext_sorted_sect_in_sets_idx++;
1258   if (num_ext_sorted_sect_in_sets_idx >= MAX_HCR_SETS) {
1259     return;
1260   }
1261 
1262   itt_bit_buff->cnt_bits = read_bits;
1263 
1264   ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx =
1265       num_ext_sorted_cw_in_sect_idx;
1266   ptr_hcr_info->sect_info.ext_sorted_cw_idx = ext_sorted_cw_idx;
1267   ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx =
1268       num_ext_sorted_sect_in_sets_idx;
1269   ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx =
1270       ext_sorted_sect_max_cb_len_idx;
1271 }
1272 
ixheaacd_init_segment_bit_field(WORD32 num_segment,WORD8 * p_remaining_bits_in_seg)1273 static UWORD32 ixheaacd_init_segment_bit_field(WORD32 num_segment,
1274                                                WORD8 *p_remaining_bits_in_seg) {
1275   WORD16 i;
1276   WORD16 num_valid_segment = 0;
1277 
1278   for (i = 0; i < num_segment; i++) {
1279     if (p_remaining_bits_in_seg[i] != 0) {
1280       num_valid_segment += 1;
1281     }
1282   }
1283 
1284   return num_valid_segment;
1285 }
1286 
ixheaacd_toggle_read_dir(UWORD8 read_direction)1287 UWORD8 ixheaacd_toggle_read_dir(UWORD8 read_direction) {
1288   if (read_direction == FROM_LEFT_TO_RIGHT) {
1289     return FROM_RIGHT_TO_LEFT;
1290   } else {
1291     return FROM_LEFT_TO_RIGHT;
1292   }
1293 }
1294 
ixheaacd_huff_dec_quad_hcr_non_pcw(ia_bit_buf_struct * itt_bit_buff,WORD32 * spec_coef,const UWORD16 * code_book_tbl,WORD32 tbl_sign,const UWORD32 * idx_table)1295 static PLATFORM_INLINE UWORD16 ixheaacd_huff_dec_quad_hcr_non_pcw(
1296     ia_bit_buf_struct *itt_bit_buff, WORD32 *spec_coef,
1297     const UWORD16 *code_book_tbl, WORD32 tbl_sign, const UWORD32 *idx_table) {
1298   WORD16 index, length;
1299   WORD16 cw_len;
1300   WORD32 read_word;
1301 
1302   read_word = ixheaacd_aac_showbits_32(itt_bit_buff->byte_ptr,
1303                                        itt_bit_buff->bit_count, NULL);
1304   ixheaacd_huffman_decode(read_word, &index, &length, code_book_tbl, idx_table);
1305   cw_len = length;
1306   if (tbl_sign) {
1307     WORD32 temp_word;
1308     WORD32 w, x, y, z;
1309     temp_word = read_word << length;
1310     w = index / 27;
1311     index = index - w * 27;
1312     x = index / 9;
1313     index = index - x * 9;
1314     y = index / 3;
1315     z = index - y * 3;
1316     if (w) {
1317       if (temp_word & 0x80000000) w = -w;
1318       temp_word <<= 1;
1319       cw_len++;
1320     }
1321     *spec_coef++ = w;
1322 
1323     if (x) {
1324       if (temp_word & 0x80000000) x = -x;
1325       temp_word <<= 1;
1326       cw_len++;
1327     }
1328     *spec_coef++ = x;
1329     if (y) {
1330       if (temp_word & 0x80000000) y = -y;
1331       temp_word <<= 1;
1332       cw_len++;
1333     }
1334     *spec_coef++ = y;
1335     if (z) {
1336       if (temp_word & 0x80000000) z = -z;
1337       temp_word <<= 1;
1338       cw_len++;
1339     }
1340     *spec_coef++ = z;
1341 
1342   }
1343 
1344   else {
1345     WORD32 w, x, y, z;
1346 
1347     w = index / 27 - 1;
1348     index = index - (w + 1) * 27;
1349     x = index / 9 - 1;
1350     index = index - (x + 1) * 9;
1351     y = index / 3 - 1;
1352     z = index - ((y + 1) * 3) - 1;
1353     *spec_coef++ = w;
1354     *spec_coef++ = x;
1355     *spec_coef++ = y;
1356     *spec_coef++ = z;
1357   }
1358 
1359   return cw_len;
1360 }
1361 
ixheaacd_huff_dec_word_hcr_non_pcw(ia_bit_buf_struct * itt_bit_buff,WORD32 * spec_coef,const UWORD16 * code_book_tbl,const UWORD32 * idx_table)1362 static PLATFORM_INLINE UWORD16 ixheaacd_huff_dec_word_hcr_non_pcw(
1363     ia_bit_buf_struct *itt_bit_buff, WORD32 *spec_coef,
1364     const UWORD16 *code_book_tbl, const UWORD32 *idx_table) {
1365   WORD32 sp1, sp2;
1366   WORD32 flush_cw;
1367   WORD32 i, value, norm_val, off;
1368   WORD32 out1, out2;
1369   UWORD16 cw_len;
1370 
1371   WORD16 index;
1372   WORD32 length;
1373 
1374   WORD32 read_word;
1375   WORD32 increment;
1376   UWORD8 *ptr_read_next;
1377 
1378   read_word = ixheaacd_aac_showbits_32(itt_bit_buff->byte_ptr,
1379                                        itt_bit_buff->bit_count, &increment);
1380 
1381   ptr_read_next = itt_bit_buff->byte_ptr;
1382   ptr_read_next += increment;
1383 
1384   ixheaacd_huff_sfb_table(read_word, &index, &length, code_book_tbl, idx_table);
1385   cw_len = length;
1386 
1387   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1388 
1389   out1 = index / 17;
1390   out2 = index - out1 * 17;
1391   flush_cw = read_word << length;
1392 
1393   sp1 = out1;
1394   sp2 = out2;
1395 
1396   if (out1) {
1397     if (flush_cw & 0x80000000) {
1398       out1 = -out1;
1399     }
1400     flush_cw = (WORD32)flush_cw << 1;
1401     length++;
1402     cw_len++;
1403   }
1404 
1405   if (out2) {
1406     if (flush_cw & 0x80000000) {
1407       out2 = -out2;
1408     }
1409     length++;
1410     cw_len++;
1411   }
1412 
1413   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1414 
1415   if (sp1 == 16) {
1416     i = 4;
1417     value = ixheaacd_extu(read_word, length, 23);
1418     value = value | 0xfffffe00;
1419     norm_val = ixheaacd_norm32(value);
1420 
1421     i += (norm_val - 22);
1422     length += (norm_val - 21);
1423     cw_len += (norm_val - 21);
1424 
1425     ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1426 
1427     off = ixheaacd_extu(read_word, length, 32 - i);
1428     length += i;
1429     cw_len += i;
1430 
1431     ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1432 
1433     i = off + ((WORD32)1 << i);
1434 
1435     if (out1 < 0)
1436       *spec_coef++ = -i;
1437     else
1438       *spec_coef++ = i;
1439   } else {
1440     *spec_coef++ = out1;
1441   }
1442 
1443   if (sp2 == 16) {
1444     i = 4;
1445     value = ixheaacd_extu(read_word, length, 23);
1446     value = value | 0xfffffe00;
1447     norm_val = ixheaacd_norm32(value);
1448 
1449     i += (norm_val - 22);
1450     length += (norm_val - 21);
1451     cw_len += (norm_val - 21);
1452 
1453     ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1454 
1455     off = ixheaacd_extu(read_word, length, 32 - i);
1456     length += i;
1457     cw_len += i;
1458 
1459     ixheaacd_aac_read_byte_corr1(&ptr_read_next, &length, &read_word, NULL);
1460     i = off + ((WORD32)1 << i);
1461 
1462     if (out2 < 0)
1463       *spec_coef++ = -i;
1464     else
1465       *spec_coef++ = i;
1466   } else {
1467     *spec_coef++ = out2;
1468   }
1469 
1470   return cw_len;
1471 }
1472 
ixheaacd_decode_hcr_non_pcw(ia_bit_buf_struct * itt_bit_buff,ia_hcr_info_struct * ptr_hcr_info,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 * cw_offset,WORD32 trial,WORD32 start)1473 static VOID ixheaacd_decode_hcr_non_pcw(
1474     ia_bit_buf_struct *itt_bit_buff, ia_hcr_info_struct *ptr_hcr_info,
1475     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 *cw_offset, WORD32 trial,
1476     WORD32 start) {
1477   WORD16 codeword_len = 0;
1478   WORD8 seg_bits_left;
1479   UWORD8 tot_bits_to_save, code_bits_to_save, extra_code_bits;
1480   WORD32 segment_offset = 0;
1481   WORD8 *p_remaining_bits_in_seg =
1482       ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
1483   WORD32 num_segment = ptr_hcr_info->str_segment_info.num_segment;
1484 
1485   for (segment_offset = start; segment_offset < trial;
1486        segment_offset++, *cw_offset += 1) {
1487     if (p_remaining_bits_in_seg[segment_offset] &&
1488         !ptr_hcr_info->str_segment_info.is_decoded[*cw_offset]) {
1489       {
1490         UWORD32 i_qsc;
1491         WORD8 current_seg_bits = p_remaining_bits_in_seg[segment_offset];
1492 
1493         itt_bit_buff->byte_ptr = itt_bit_buff->ptr_start;
1494         itt_bit_buff->valid_bits = 0;
1495         itt_bit_buff->byte = 0;
1496         itt_bit_buff->bit_count = 0;
1497         itt_bit_buff->write_bit_count = 0;
1498 
1499         if (ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset]) {
1500           extra_code_bits = max(
1501               ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset] - 32, 0);
1502           code_bits_to_save =
1503               min(ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset], 32);
1504 
1505           ixheaacd_write_bit(
1506               itt_bit_buff,
1507               ptr_hcr_info->str_segment_info.code_extra[*cw_offset],
1508               extra_code_bits);
1509           ixheaacd_write_bit(itt_bit_buff,
1510                              ptr_hcr_info->str_segment_info.code[*cw_offset],
1511                              code_bits_to_save);
1512         }
1513         {
1514           UWORD32 bit;
1515           WORD32 read_bit_offset;
1516 
1517           if (ptr_hcr_info->str_segment_info.read_direction ==
1518               FROM_LEFT_TO_RIGHT) {
1519             read_bit_offset =
1520                 ptr_hcr_info->str_segment_info.arr_seg_start_l[segment_offset] -
1521                 (itt_bit_buff->size - itt_bit_buff->cnt_bits);
1522             if (read_bit_offset) {
1523               itt_bit_buff->cnt_bits += -read_bit_offset;
1524             }
1525             itt_bit_buff->ptr_read_next =
1526                 itt_bit_buff->ptr_bit_buf_base +
1527                 ((itt_bit_buff->size - itt_bit_buff->cnt_bits) >> 3);
1528             itt_bit_buff->bit_pos =
1529                 ((itt_bit_buff->size - itt_bit_buff->cnt_bits) & 7);
1530 
1531             for (; p_remaining_bits_in_seg[segment_offset] > 0;
1532                  p_remaining_bits_in_seg[segment_offset] -= 1) {
1533               bit = ixheaacd_aac_read_bit_rev(itt_bit_buff);
1534               ptr_hcr_info->str_segment_info.arr_seg_start_l[segment_offset] +=
1535                   1;
1536 
1537               ixheaacd_write_bit(itt_bit_buff, bit, 1);
1538             }
1539 
1540           } else {
1541             read_bit_offset =
1542                 ptr_hcr_info->str_segment_info.arr_seg_start_r[segment_offset] -
1543                 (itt_bit_buff->size - itt_bit_buff->cnt_bits);
1544             if (read_bit_offset) {
1545               itt_bit_buff->cnt_bits += -read_bit_offset;
1546             }
1547             itt_bit_buff->ptr_read_next =
1548                 itt_bit_buff->ptr_bit_buf_base +
1549                 ((itt_bit_buff->size - itt_bit_buff->cnt_bits) >> 3);
1550             itt_bit_buff->bit_pos =
1551                 ((itt_bit_buff->size - itt_bit_buff->cnt_bits) & 7);
1552 
1553             for (; p_remaining_bits_in_seg[segment_offset] > 0;
1554                  p_remaining_bits_in_seg[segment_offset] -= 1) {
1555               bit = ixheaacd_aac_read_bit(itt_bit_buff);
1556               ptr_hcr_info->str_segment_info.arr_seg_start_r[segment_offset] -=
1557                   1;
1558               ixheaacd_write_bit(itt_bit_buff, bit, 1);
1559             }
1560           }
1561         }
1562 
1563         ixheaacd_write_bit(itt_bit_buff, 0, 32 - itt_bit_buff->bit_count % 32);
1564         itt_bit_buff->valid_bits = 8;
1565         itt_bit_buff->byte_ptr = itt_bit_buff->ptr_start;
1566         itt_bit_buff->byte = *itt_bit_buff->ptr_start;
1567 
1568         if (current_seg_bits) {
1569           i_qsc = ptr_hcr_info->str_non_pcw_side_info
1570                       .res_ptr_idx[*cw_offset % num_segment];
1571 
1572           if (ptr_hcr_info->str_non_pcw_side_info
1573                   .ptr_cb[*cw_offset % num_segment] <= 4) {
1574             WORD32 tbl_sign = 0;
1575             const UWORD16 *cb_table =
1576                 (UWORD16
1577                      *)(ptr_aac_tables
1578                             ->code_book[ptr_hcr_info->str_non_pcw_side_info
1579                                             .ptr_cb[*cw_offset % num_segment]]);
1580             const UWORD32 *idx_table =
1581                 (UWORD32 *)(ptr_aac_tables->index_table
1582                                 [ptr_hcr_info->str_non_pcw_side_info
1583                                      .ptr_cb[*cw_offset % num_segment]]);
1584 
1585             if (ptr_hcr_info->str_non_pcw_side_info
1586                     .ptr_cb[*cw_offset % num_segment] > 2) {
1587               tbl_sign = 1;
1588             }
1589 
1590             codeword_len = ixheaacd_huff_dec_quad_hcr_non_pcw(
1591                 itt_bit_buff,
1592                 &ptr_hcr_info->str_non_pcw_side_info.ptr_result_base[i_qsc],
1593                 cb_table, tbl_sign, idx_table);
1594 
1595             seg_bits_left =
1596                 current_seg_bits - codeword_len +
1597                 ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1598 
1599           }
1600 
1601           else if (ptr_hcr_info->str_non_pcw_side_info
1602                        .ptr_cb[*cw_offset % num_segment] < 11) {
1603             WORD32 tbl_sign = 0;
1604             WORD32 huff_mode = 9;
1605 
1606             const UWORD16 *cb_table =
1607                 (UWORD16
1608                      *)(ptr_aac_tables
1609                             ->code_book[ptr_hcr_info->str_non_pcw_side_info
1610                                             .ptr_cb[*cw_offset % num_segment]]);
1611             const UWORD32 *idx_table =
1612                 (UWORD32 *)(ptr_aac_tables->index_table
1613                                 [ptr_hcr_info->str_non_pcw_side_info
1614                                      .ptr_cb[*cw_offset % num_segment]]);
1615 
1616             if (ptr_hcr_info->str_non_pcw_side_info
1617                     .ptr_cb[*cw_offset % num_segment] > 6) {
1618               if (ptr_hcr_info->str_non_pcw_side_info
1619                       .ptr_cb[*cw_offset % num_segment] > 8)
1620                 huff_mode = 13;
1621               else
1622                 huff_mode = 8;
1623               tbl_sign = 1;
1624             }
1625             codeword_len = ixheaacd_huff_dec_pair_hcr_non_pcw(
1626                 itt_bit_buff,
1627                 &ptr_hcr_info->str_non_pcw_side_info.ptr_result_base[i_qsc],
1628                 cb_table, tbl_sign, idx_table, huff_mode);
1629 
1630             seg_bits_left =
1631                 current_seg_bits - codeword_len +
1632                 ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1633           }
1634           if (ptr_hcr_info->str_non_pcw_side_info
1635                   .ptr_cb[*cw_offset % num_segment] >= 11) {
1636             const UWORD32 *idx_table =
1637                 ptr_aac_tables->pstr_huffmann_tables->idx_table_hf11;
1638             const UWORD16 *cb_table =
1639                 ptr_aac_tables->pstr_huffmann_tables->input_table_cb11;
1640 
1641             codeword_len = ixheaacd_huff_dec_word_hcr_non_pcw(
1642                 itt_bit_buff,
1643                 &ptr_hcr_info->str_non_pcw_side_info.ptr_result_base[i_qsc],
1644                 cb_table, idx_table);
1645 
1646             seg_bits_left =
1647                 current_seg_bits - codeword_len +
1648                 ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1649           }
1650           if (seg_bits_left < 0) {
1651             tot_bits_to_save =
1652                 current_seg_bits +
1653                 ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset];
1654             extra_code_bits = max(tot_bits_to_save - 32, 0);
1655             code_bits_to_save = min(tot_bits_to_save, 32);
1656 
1657             ptr_hcr_info->str_segment_info.code_extra[*cw_offset] =
1658                 ixheaacd_read_bit(itt_bit_buff, extra_code_bits);
1659             ptr_hcr_info->str_segment_info.code[*cw_offset] =
1660                 ixheaacd_read_bit(itt_bit_buff, code_bits_to_save);
1661             ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset] =
1662                 tot_bits_to_save;
1663 
1664             p_remaining_bits_in_seg[segment_offset] = 0;
1665             if (p_remaining_bits_in_seg[segment_offset] < 0)
1666               p_remaining_bits_in_seg[segment_offset] = 0;
1667           } else {
1668             p_remaining_bits_in_seg[segment_offset] =
1669                 current_seg_bits -
1670                 (codeword_len -
1671                  ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset]);
1672             ptr_hcr_info->str_segment_info.p_num_bits[*cw_offset] = 0;
1673             ptr_hcr_info->str_segment_info.is_decoded[*cw_offset] = 1;
1674             if (p_remaining_bits_in_seg[segment_offset] < 0)
1675               p_remaining_bits_in_seg[segment_offset] = 0;
1676           }
1677 
1678           if (p_remaining_bits_in_seg[segment_offset] > 0) {
1679             if (ptr_hcr_info->str_segment_info.read_direction ==
1680                 FROM_LEFT_TO_RIGHT)
1681               ptr_hcr_info->str_segment_info.arr_seg_start_l[segment_offset] -=
1682                   (p_remaining_bits_in_seg[segment_offset]);
1683             else
1684               ptr_hcr_info->str_segment_info.arr_seg_start_r[segment_offset] +=
1685                   (p_remaining_bits_in_seg[segment_offset]);
1686           }
1687         }
1688       }
1689     }
1690   }
1691 }
1692 
ixheaacd_decode_non_pcw(ia_bit_buf_struct * itt_bit_buff,ia_hcr_info_struct * ptr_hcr_info,ia_aac_dec_tables_struct * ptr_aac_tables)1693 VOID ixheaacd_decode_non_pcw(ia_bit_buf_struct *itt_bit_buff,
1694                              ia_hcr_info_struct *ptr_hcr_info,
1695                              ia_aac_dec_tables_struct *ptr_aac_tables) {
1696   UWORD32 num_valid_segment;
1697   WORD32 cw_offset;
1698   WORD32 trial;
1699   WORD32 num_segment;
1700   WORD32 num_code_word;
1701   UWORD8 num_set;
1702   UWORD8 current_set;
1703   WORD32 code_word_set;
1704   WORD32 loop1, loop2;
1705 
1706   num_segment = ptr_hcr_info->str_segment_info.num_segment;
1707 
1708   num_valid_segment = ixheaacd_init_segment_bit_field(
1709       num_segment, ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg);
1710 
1711   if (num_valid_segment != 0) {
1712     num_code_word = ptr_hcr_info->sect_info.num_code_word;
1713     num_set = ((num_code_word - 1) / num_segment) + 1;
1714 
1715     ptr_hcr_info->str_segment_info.read_direction = FROM_RIGHT_TO_LEFT;
1716 
1717     for (current_set = 1; current_set < num_set; current_set++) {
1718       num_code_word -= num_segment;
1719       if (num_code_word < num_segment) {
1720         code_word_set = num_code_word;
1721       } else {
1722         code_word_set = num_segment;
1723       }
1724 
1725       ixheaacd_nonpcw_sideinfo_init(ptr_hcr_info);
1726 
1727       cw_offset = num_segment * current_set;
1728 
1729       ixheaacd_decode_hcr_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables,
1730                                   &cw_offset, code_word_set, 0);
1731 
1732       for (trial = 1; trial < num_segment; trial++) {
1733         cw_offset = num_segment * current_set;
1734 
1735         loop1 = min(num_segment, trial + code_word_set);
1736         loop2 = max(0, trial + code_word_set - num_segment);
1737 
1738         ixheaacd_decode_hcr_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables,
1739                                     &cw_offset, loop1, trial);
1740 
1741         ixheaacd_decode_hcr_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables,
1742                                     &cw_offset, loop2, 0);
1743       }
1744 
1745       ptr_hcr_info->str_segment_info.read_direction = ixheaacd_toggle_read_dir(
1746           ptr_hcr_info->str_segment_info.read_direction);
1747     }
1748   }
1749 }
1750 
ixheaacd_hcr_reorder_quantized_spec_coeff(ia_hcr_info_struct * ptr_hcr_info,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info)1751 static VOID ixheaacd_hcr_reorder_quantized_spec_coeff(
1752     ia_hcr_info_struct *ptr_hcr_info,
1753     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info) {
1754   WORD32 qsc;
1755   UWORD32 abs_qsc;
1756   UWORD32 i, j;
1757   UWORD16 num_spec_val_sect;
1758   WORD32 *ptr_teva;
1759   UWORD16 lav_err_cnt = 0;
1760 
1761   UWORD32 num_sect = ptr_hcr_info->str_dec_io.num_sect;
1762   WORD32 *ptr_quant_spec_coeff_base =
1763       ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
1764   WORD32 *ptr_quant_spec_coeff =
1765       ptr_hcr_info->str_dec_io.ptr_quant_spec_coeff_base;
1766   const UWORD8 *ptr_cb_dim_shift_tbl =
1767       ptr_hcr_info->table_info.ptr_cb_dim_shift_tbl;
1768   const UWORD16 *ptr_lav_tbl = ptr_hcr_info->table_info.ptr_lav_tbl;
1769   UWORD8 *ptr_sorted_cb = ptr_hcr_info->sect_info.ptr_sorted_cb;
1770   UWORD16 *ptr_num_sorted_cw_in_sect =
1771       ptr_hcr_info->sect_info.ptr_num_sorted_cw_in_sect;
1772   UWORD16 *ptr_reorder_offset = ptr_hcr_info->sect_info.ptr_reorder_offset;
1773   WORD32 *arr_temp_values = ptr_hcr_info->str_segment_info.arr_temp_values;
1774   WORD32 *ptr_bak = ptr_hcr_info->str_segment_info.arr_temp_values;
1775 
1776   for (i = num_sect; i != 0; i--) {
1777     num_spec_val_sect = *ptr_num_sorted_cw_in_sect++
1778                         << ptr_cb_dim_shift_tbl[*ptr_sorted_cb];
1779     ptr_teva = &arr_temp_values[*ptr_reorder_offset++];
1780     for (j = num_spec_val_sect; j != 0; j--) {
1781       qsc = *ptr_quant_spec_coeff++;
1782       abs_qsc = ixheaacd_abs32(qsc);
1783       if (abs_qsc <= ptr_lav_tbl[*ptr_sorted_cb]) {
1784         *ptr_teva++ = (WORD32)qsc;
1785       } else {
1786         if (abs_qsc == 8192) {
1787           *ptr_teva++ = (WORD32)qsc;
1788         } else {
1789           *ptr_teva++ = (WORD32)8192;
1790           lav_err_cnt += 1;
1791         }
1792       }
1793     }
1794     ptr_sorted_cb++;
1795   }
1796 
1797   if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
1798       EIGHT_SHORT_SEQUENCE) {
1799     WORD32 *ptr_out;
1800     WORD8 window;
1801 
1802     ptr_bak = ptr_hcr_info->str_segment_info.arr_temp_values;
1803     for (window = 0; window < 8; window++) {
1804       ptr_out = ptr_quant_spec_coeff_base +
1805                 (window * ptr_aac_dec_channel_info->granule_len);
1806       for (i = 0; i < (LINES_PER_UNIT_GROUP); i++) {
1807         ptr_teva = ptr_bak + (window << 2) + i * 32;
1808         for (j = (LINES_PER_UNIT); j != 0; j--) {
1809           *ptr_out++ = *ptr_teva++;
1810         }
1811       }
1812     }
1813   } else {
1814     ptr_quant_spec_coeff = ptr_quant_spec_coeff_base;
1815     for (i = 1024; i != 0; i--) {
1816       *ptr_quant_spec_coeff++ = *ptr_bak++;
1817     }
1818   }
1819 
1820   if (lav_err_cnt != 0) {
1821     ptr_hcr_info->str_dec_io.err_log |= (ERROR_POS << 1);
1822   }
1823 }
1824 
ixheaacd_err_detect_segmentation_final(ia_hcr_info_struct * ptr_hcr_info)1825 static VOID ixheaacd_err_detect_segmentation_final(
1826     ia_hcr_info_struct *ptr_hcr_info) {
1827   UWORD8 segmentation_err_flag = 0;
1828   UWORD16 i;
1829   WORD8 *p_remaining_bits_in_seg =
1830       ptr_hcr_info->str_segment_info.p_remaining_bits_in_seg;
1831   UWORD32 num_segment = ptr_hcr_info->str_segment_info.num_segment;
1832 
1833   for (i = num_segment; i != 0; i--) {
1834     if (*p_remaining_bits_in_seg++ != 0) {
1835       segmentation_err_flag = 1;
1836     }
1837   }
1838   if (segmentation_err_flag == 1) {
1839     ptr_hcr_info->str_dec_io.err_log |= ERROR_POS;
1840   }
1841 }
1842 
ixheaacd_hcr_decoder(ia_hcr_info_struct * ptr_hcr_info,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables,ia_bit_buf_struct * itt_bit_buff)1843 UWORD32 ixheaacd_hcr_decoder(
1844     ia_hcr_info_struct *ptr_hcr_info,
1845     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
1846     ia_aac_dec_tables_struct *ptr_aac_tables, ia_bit_buf_struct *itt_bit_buff) {
1847   WORD32 ptr_tmp1, ptr_tmp2, ptr_tmp3, ptr_tmp4;
1848   WORD32 ptr_tmp5;
1849 
1850   WORD32 bit_cnt_offset;
1851   UWORD32 save_bit_cnt = itt_bit_buff->cnt_bits;
1852 
1853   ixheaacd_huff_calc_num_cwd(ptr_hcr_info);
1854 
1855   ixheaacd_huff_sort_sect_cb_cwd(ptr_hcr_info);
1856 
1857   if (ixheaacd_hcr_prepare_segmentation_grid(ptr_hcr_info) != 0)
1858     return (ptr_hcr_info->str_dec_io.err_log);
1859 
1860   ixheaacd_huff_ext_sect_info(ptr_hcr_info);
1861 
1862   if ((ptr_hcr_info->str_dec_io.err_log & HCR_FATAL_PCW_ERROR_MASK) != 0) {
1863     return (ptr_hcr_info->str_dec_io.err_log);
1864   }
1865 
1866   ixheaacd_calc_num_ext_sorted_sect_sets(
1867       ptr_hcr_info->str_segment_info.num_segment,
1868       ptr_hcr_info->sect_info.ptr_num_ext_sorted_cw_in_sect,
1869       ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx,
1870       ptr_hcr_info->sect_info.ptr_num_ext_sorted_sect_in_sets,
1871       ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx);
1872 
1873   ptr_tmp1 = ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx;
1874   ptr_tmp2 = ptr_hcr_info->sect_info.ext_sorted_cw_idx;
1875   ptr_tmp3 = ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx;
1876   ptr_tmp4 = ptr_hcr_info->str_dec_io.quant_spec_coeff_idx;
1877   ptr_tmp5 = ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx;
1878 
1879   ixheaacd_decode_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables);
1880 
1881   if ((ptr_hcr_info->str_dec_io.err_log & HCR_FATAL_PCW_ERROR_MASK) == 0) {
1882     ixheaacd_decode_non_pcw(itt_bit_buff, ptr_hcr_info, ptr_aac_tables);
1883   }
1884 
1885   ixheaacd_err_detect_segmentation_final(ptr_hcr_info);
1886 
1887   ptr_hcr_info->sect_info.num_ext_sorted_cw_in_sect_idx = ptr_tmp1;
1888   ptr_hcr_info->sect_info.ext_sorted_cw_idx = ptr_tmp2;
1889   ptr_hcr_info->sect_info.num_ext_sorted_sect_in_sets_idx = ptr_tmp3;
1890   ptr_hcr_info->str_dec_io.quant_spec_coeff_idx = ptr_tmp4;
1891   ptr_hcr_info->sect_info.ext_sorted_sect_max_cb_len_idx = ptr_tmp5;
1892 
1893   ixheaacd_hcr_reorder_quantized_spec_coeff(ptr_hcr_info,
1894                                             ptr_aac_dec_channel_info);
1895 
1896   bit_cnt_offset = (WORD32)itt_bit_buff->cnt_bits - (WORD32)save_bit_cnt;
1897   if (bit_cnt_offset) {
1898     itt_bit_buff->cnt_bits += -bit_cnt_offset;
1899     itt_bit_buff->ptr_read_next =
1900         itt_bit_buff->ptr_bit_buf_base +
1901         ((itt_bit_buff->size - itt_bit_buff->cnt_bits) >> 3);
1902     itt_bit_buff->bit_pos = (itt_bit_buff->size - itt_bit_buff->cnt_bits) & 7;
1903   }
1904 
1905   return (ptr_hcr_info->str_dec_io.err_log);
1906 }
1907