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