1 /******************************************************************************
2 *
3 * Copyright (C) 2023 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 "ixheaacd_type_def.h"
21 #include "ixheaacd_constants.h"
22 #include "ixheaacd_cnst.h"
23 #include "ixheaacd_basic_ops32.h"
24 #include "ixheaacd_basic_ops16.h"
25 #include "ixheaacd_bitbuffer.h"
26 #include "ixheaacd_mps_aac_struct.h"
27 #include "ixheaacd_mps_res_rom.h"
28 #include "ixheaacd_mps_res.h"
29 #include "ixheaacd_mps_res_huffman.h"
30
31 #define LONG_BLOCK_SECT_LEN 5
32 #define SHORT_BLOCK_SECT_LEN 3
33
ixheaacd_c_block_read_section_data(ia_bit_buf_struct * it_bit_buf,ia_mps_dec_residual_channel_info_struct * p_aac_decoder_channel_info)34 WORD16 ixheaacd_c_block_read_section_data(
35 ia_bit_buf_struct *it_bit_buf,
36 ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info) {
37 WORD band;
38 WORD sect_cb;
39 WORD sect_len;
40 WORD sect_len_incr;
41 WORD sect_esc_val;
42 ia_mps_dec_residual_ics_info_struct *p_ics_info = &p_aac_decoder_channel_info->ics_info;
43 WORD sfb_transmitted = p_ics_info->max_sf_bands;
44 WORD win_group = p_ics_info->window_groups;
45
46 WORD8 *p_code_book = p_aac_decoder_channel_info->p_code_book;
47 WORD8 *p_code_book_temp = p_code_book;
48 WORD32 sect_bitlen = LONG_BLOCK_SECT_LEN;
49
50 if (p_aac_decoder_channel_info->ics_info.window_sequence == EIGHT_SHORT_SEQUENCE)
51 sect_bitlen = SHORT_BLOCK_SECT_LEN;
52
53 sect_esc_val = (1 << sect_bitlen) - 1;
54
55 do {
56 band = 0;
57
58 while (band < sfb_transmitted) {
59 WORD32 temp_word;
60 sect_len = 0;
61 temp_word = ixheaacd_read_bits_buf(it_bit_buf, 4 + sect_bitlen);
62 sect_cb = temp_word >> sect_bitlen;
63 sect_len_incr = temp_word & sect_esc_val;
64
65 while (sect_len_incr == sect_esc_val) {
66 sect_len = (sect_len + sect_esc_val);
67 sect_len_incr = ixheaacd_read_bits_buf(it_bit_buf, sect_bitlen);
68 }
69
70 sect_len = (sect_len + sect_len_incr);
71
72 band = (band + sect_len);
73 if (band > sfb_transmitted) {
74 return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_SFB_TRANSMITTED);
75 }
76
77 if (sect_cb == BOOKSCL) {
78 return (WORD16)((WORD32)AAC_DEC_INVALID_CODE_BOOK);
79 }
80
81 sect_len = sect_len - 1;
82 for (; sect_len >= 0; sect_len--) {
83 *p_code_book_temp++ = sect_cb;
84 }
85 }
86 p_code_book += MAX_SFB_SHORT;
87 p_code_book_temp = p_code_book;
88 win_group--;
89 } while (win_group != 0);
90 return AAC_DEC_OK;
91 }
92
ixheaacd_res_c_block_read_scf_data(ia_bit_buf_struct * it_bit_buf,ia_mps_dec_residual_channel_info_struct * p_aac_decoder_channel_info,WORD16 global_gain,ia_mps_dec_residual_aac_tables_struct * aac_tables_ptr)93 VOID ixheaacd_res_c_block_read_scf_data(
94 ia_bit_buf_struct *it_bit_buf,
95 ia_mps_dec_residual_channel_info_struct *p_aac_decoder_channel_info, WORD16 global_gain,
96 ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr) {
97 WORD band;
98 WORD16 position = 0;
99 WORD group;
100 WORD16 factor = global_gain;
101 WORD8 *p_code_book, *p_codebook_tmp;
102 WORD16 *p_scale_factor, *p_scale_factor_tmp;
103 WORD16 norm_value;
104 ia_mps_dec_residual_ics_info_struct *p_ics_info;
105 WORD window_groups, sfb_transmitted;
106 UWORD16 *h;
107
108 const UWORD16 *hscf = &aac_tables_ptr->res_huffmann_tables_ptr->huffman_code_book_scl[1];
109
110 WORD start_bit_pos = it_bit_buf->bit_pos;
111 UWORD8 *start_read_pos = it_bit_buf->ptr_read_next;
112 UWORD8 *ptr_read_next = it_bit_buf->ptr_read_next;
113 WORD32 bit_pos = 7 - it_bit_buf->bit_pos;
114
115 WORD32 read_word;
116 WORD32 diffbytes;
117
118 diffbytes = (WORD32)(it_bit_buf->ptr_bit_buf_end - ptr_read_next);
119 diffbytes++;
120 if (diffbytes >= 4) {
121 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
122 diffbytes = 4;
123 ptr_read_next = it_bit_buf->ptr_read_next + 4;
124 } else {
125 WORD32 ii;
126 read_word = 0;
127 for (ii = 0; ii < diffbytes; ii++) {
128 read_word = (read_word << 8) | (*ptr_read_next);
129 ptr_read_next++;
130 }
131 read_word <<= ((4 - diffbytes) << 3);
132 }
133 p_code_book = p_aac_decoder_channel_info->p_code_book;
134
135 p_ics_info = &p_aac_decoder_channel_info->ics_info;
136 sfb_transmitted = p_ics_info->max_sf_bands;
137
138 p_scale_factor = p_aac_decoder_channel_info->p_scale_factor;
139 window_groups = p_ics_info->window_groups;
140 band = sfb_transmitted - 1;
141
142 for (group = 0; group < window_groups; group++) {
143 p_codebook_tmp = &p_code_book[group * MAX_SFB_SHORT];
144 p_scale_factor_tmp = &p_scale_factor[group * MAX_SFB_SHORT];
145 for (band = sfb_transmitted - 1; band >= 0; band--) {
146 WORD32 cb_num = *p_codebook_tmp++;
147
148 if (cb_num == ZERO_HCB)
149 *p_scale_factor_tmp++ = 0;
150 else {
151 {
152 WORD32 flag = 1;
153 WORD pns_band;
154 ia_mps_dec_residual_pns_data_struct *p_pns_data = &p_aac_decoder_channel_info->pns_data;
155 if (cb_num == NOISE_HCB && (p_pns_data->pns_active != 1)) flag = 0;
156
157 if (flag) {
158 UWORD16 first_offset;
159 WORD16 sign_ret_val;
160 UWORD32 read_word1;
161
162 read_word1 = read_word << bit_pos;
163 h = (UWORD16 *)(hscf);
164 first_offset = 7;
165 h += (read_word1) >> (32 - first_offset);
166 sign_ret_val = *h;
167
168 while (sign_ret_val > 0) {
169 bit_pos += first_offset;
170
171 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
172 it_bit_buf->ptr_bit_buf_end);
173 read_word1 = (read_word1) << (first_offset);
174
175 first_offset = (sign_ret_val >> 11);
176 first_offset = (sign_ret_val >> 11);
177 h += sign_ret_val & (0x07FF);
178 h += (read_word1) >> (32 - first_offset);
179 sign_ret_val = *h;
180 }
181
182 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
183
184 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
185 it_bit_buf->ptr_bit_buf_end);
186 norm_value = (sign_ret_val & (0x07FF)) - 60;
187 } else {
188 WORD32 noise_start_value;
189 UWORD32 temp;
190 temp = (read_word << bit_pos);
191 temp = ((UWORD32)temp >> (32 - 9));
192 noise_start_value = temp;
193 bit_pos += 9;
194
195 ixheaacd_aac_read_2bytes(&ptr_read_next, &bit_pos, &read_word);
196
197 norm_value = noise_start_value - 256;
198 p_pns_data->pns_active = 1;
199
200 p_pns_data->current_energy = global_gain - NOISE_OFFSET;
201 }
202
203 if (cb_num > NOISE_HCB) {
204 position = position + norm_value;
205 *p_scale_factor_tmp++ = -position;
206 } else if (cb_num < NOISE_HCB) {
207 factor = factor + norm_value;
208 *p_scale_factor_tmp++ = factor;
209 } else {
210 p_pns_data->current_energy =
211 ixheaacd_add16_sat(p_pns_data->current_energy, norm_value);
212
213 pns_band = (group << 4) + sfb_transmitted - band - 1;
214 p_aac_decoder_channel_info->p_scale_factor[pns_band] = p_pns_data->current_energy;
215
216 p_pns_data->pns_used[pns_band] = 1;
217 p_scale_factor_tmp++;
218 }
219 }
220 }
221 }
222 }
223
224 it_bit_buf->ptr_read_next = ptr_read_next - diffbytes;
225
226 it_bit_buf->bit_pos = 7 - bit_pos;
227 {
228 WORD bits_cons;
229 bits_cons = (WORD)(((it_bit_buf->ptr_read_next - start_read_pos) << 3) +
230 (start_bit_pos - it_bit_buf->bit_pos));
231 it_bit_buf->cnt_bits -= bits_cons;
232 }
233 }
234