/src/libxaac/decoder/ixheaacd_latmdemux.c
Line | Count | Source (jump to first uncovered line) |
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 "ixheaac_type_def.h" |
23 | | #include "ixheaac_constants.h" |
24 | | #include "ixheaac_basic_ops32.h" |
25 | | #include "ixheaac_basic_ops16.h" |
26 | | #include "ixheaac_basic_ops40.h" |
27 | | #include "ixheaac_basic_ops.h" |
28 | | #include "ixheaacd_bitbuffer.h" |
29 | | |
30 | | #include "ixheaac_basic_op.h" |
31 | | #include "ixheaacd_intrinsics.h" |
32 | | |
33 | | #include "ixheaacd_defines.h" |
34 | | |
35 | | #include "ixheaacd_aac_rom.h" |
36 | | |
37 | | #include "ixheaacd_definitions.h" |
38 | | |
39 | | #include "ixheaacd_error_codes.h" |
40 | | |
41 | | #include "ixheaacd_pulsedata.h" |
42 | | |
43 | | #include "ixheaacd_pns.h" |
44 | | #include "ixheaacd_drc_data_struct.h" |
45 | | |
46 | | #include "ixheaacd_lt_predict.h" |
47 | | #include "ixheaacd_cnst.h" |
48 | | #include "ixheaacd_ec_defines.h" |
49 | | #include "ixheaacd_ec_struct_def.h" |
50 | | #include "ixheaacd_channelinfo.h" |
51 | | #include "ixheaacd_drc_dec.h" |
52 | | #include "ixheaacd_sbrdecoder.h" |
53 | | #include "ixheaacd_block.h" |
54 | | #include "ixheaacd_channel.h" |
55 | | |
56 | | #include "ixheaacd_sbr_payload.h" |
57 | | #include "ixheaacd_common_rom.h" |
58 | | #include "ixheaacd_sbrdecsettings.h" |
59 | | #include "ixheaacd_sbr_scale.h" |
60 | | #include "ixheaacd_env_extr_part.h" |
61 | | #include "ixheaacd_sbr_rom.h" |
62 | | #include "ixheaacd_stereo.h" |
63 | | #include "ixheaacd_lpp_tran.h" |
64 | | #include "ixheaacd_hybrid.h" |
65 | | #include "ixheaacd_ps_dec.h" |
66 | | |
67 | | #include "ixheaacd_env_extr.h" |
68 | | #include "ixheaacd_adts.h" |
69 | | #include "ixheaacd_audioobjtypes.h" |
70 | | #include "ixheaacd_memory_standards.h" |
71 | | |
72 | | #include "ixheaacd_latmdemux.h" |
73 | | |
74 | | #include "ixheaacd_aacdec.h" |
75 | | #include "ixheaacd_config.h" |
76 | | #include "ixheaacd_qmf_dec.h" |
77 | | #include "ixheaacd_mps_polyphase.h" |
78 | | #include "ixheaacd_mps_macro_def.h" |
79 | | #include "ixheaacd_mps_struct_def.h" |
80 | | #include "ixheaacd_mps_res_rom.h" |
81 | | #include "ixheaacd_mps_aac_struct.h" |
82 | | #include "ixheaacd_mps_dec.h" |
83 | | #include "ixheaacd_struct_def.h" |
84 | | |
85 | | #include "ixheaacd_multichannel.h" |
86 | | #include "ixheaacd_headerdecode.h" |
87 | | #include "ixheaac_error_standards.h" |
88 | | |
89 | | WORD32 ixheaacd_latm_au_chunk_length_info( |
90 | 226k | struct ia_bit_buf_struct *it_bit_buff) { |
91 | 226k | UWORD8 reading_done; |
92 | 226k | WORD32 len = 0; |
93 | | |
94 | 240k | do { |
95 | 240k | UWORD32 tmp = ixheaacd_read_bits_buf(it_bit_buff, 8); |
96 | 240k | reading_done = (tmp < 255); |
97 | | |
98 | 240k | len += tmp; |
99 | | |
100 | 240k | } while (reading_done == 0); |
101 | | |
102 | 226k | len <<= 3; |
103 | | |
104 | 226k | return len; |
105 | 226k | } |
106 | | |
107 | | WORD32 ixheaacd_latm_payload_length_info(struct ia_bit_buf_struct *it_bit_buff, |
108 | 224k | ixheaacd_latm_struct *latm_element) { |
109 | 224k | WORD32 error_code = AAC_DEC_OK; |
110 | 224k | UWORD32 prog, lay; |
111 | | |
112 | 224k | if (latm_element->all_streams_same_time_framing == 1) { |
113 | 449k | for (prog = 0; prog < latm_element->num_program; prog++) { |
114 | 451k | for (lay = 0; lay < latm_element->num_layer; lay++) { |
115 | 226k | ixheaacd_latm_layer_info *layer_info = |
116 | 226k | &latm_element->layer_info[prog][lay]; |
117 | | |
118 | 226k | switch (layer_info->frame_len_type) { |
119 | 226k | case 0: |
120 | 226k | layer_info->frame_len_bits = |
121 | 226k | ixheaacd_latm_au_chunk_length_info(it_bit_buff); |
122 | 226k | if (layer_info->frame_len_bits % 8 != 0) { |
123 | 0 | error_code = IA_XHEAAC_DEC_EXE_FATAL_INVALID_LOAS_HEADER; |
124 | 0 | return error_code; |
125 | 0 | } |
126 | | |
127 | 226k | latm_element->frame_length = layer_info->frame_len_bits >> 3; |
128 | 226k | latm_element->frame_length += |
129 | 226k | (it_bit_buff->size - it_bit_buff->cnt_bits) >> 3; |
130 | 226k | break; |
131 | | |
132 | 0 | default: |
133 | 0 | error_code = IA_XHEAAC_DEC_EXE_FATAL_INVALID_LOAS_HEADER; |
134 | 0 | return error_code; |
135 | 226k | } |
136 | 226k | } |
137 | 224k | } |
138 | 224k | } else { |
139 | 10 | error_code = IA_XHEAAC_DEC_EXE_FATAL_INVALID_LOAS_HEADER; |
140 | 10 | return error_code; |
141 | 10 | } |
142 | | |
143 | 224k | return (error_code); |
144 | 224k | } |
145 | | |
146 | 139 | static UWORD32 ixheaacd_latm_get_value(ia_bit_buf_struct *it_bit_buff) { |
147 | 139 | UWORD32 bytes_read; |
148 | | |
149 | 139 | bytes_read = ixheaacd_read_bits_buf(it_bit_buff, 2) + 1; |
150 | | |
151 | 139 | if (bytes_read <= 3) |
152 | 99 | return ixheaacd_read_bits_buf(it_bit_buff, 8 * bytes_read); |
153 | 40 | else |
154 | 40 | return ixheaac_add32_sat(ixheaac_shl32_sat(ixheaacd_read_bits_buf(it_bit_buff, 24), 8), |
155 | 40 | ixheaacd_read_bits_buf(it_bit_buff, 8)); |
156 | 139 | } |
157 | | |
158 | | IA_ERRORCODE ixheaacd_latm_stream_mux_config( |
159 | | struct ia_bit_buf_struct *it_bit_buff, ixheaacd_latm_struct *latm_element, |
160 | | ia_aac_dec_state_struct *aac_state_struct, |
161 | 170k | ia_sampling_rate_info_struct *sample_rate_info) { |
162 | 170k | UWORD32 prog; |
163 | 170k | UWORD32 lay; |
164 | 170k | WORD32 bytes_consumed; |
165 | 170k | WORD32 audio_mux_version_a; |
166 | 170k | IA_ERRORCODE error_code = AAC_DEC_OK; |
167 | 170k | ixheaacd_latm_layer_info *layer_info = 0; |
168 | | |
169 | 170k | latm_element->audio_mux_version = ixheaacd_read_bits_buf(it_bit_buff, 1); |
170 | | |
171 | 170k | if (latm_element->audio_mux_version == 1) |
172 | 97 | audio_mux_version_a = ixheaacd_read_bits_buf(it_bit_buff, 1); |
173 | 170k | else |
174 | 170k | audio_mux_version_a = 0; |
175 | | |
176 | 170k | if (audio_mux_version_a == 0) { |
177 | 170k | if (latm_element->audio_mux_version == 1) { |
178 | 69 | ixheaacd_latm_get_value(it_bit_buff);/*tara_buf_fullness*/ |
179 | 69 | } |
180 | 170k | latm_element->all_streams_same_time_framing = |
181 | 170k | ixheaacd_read_bits_buf(it_bit_buff, 1); |
182 | | |
183 | 170k | latm_element->num_sub_frames = ixheaacd_read_bits_buf(it_bit_buff, 6) + 1; |
184 | | |
185 | 170k | if (latm_element->num_sub_frames != 1) |
186 | 214 | return IA_XHEAAC_DEC_EXE_FATAL_INVALID_LOAS_HEADER; |
187 | | |
188 | 170k | latm_element->num_program = ixheaacd_read_bits_buf(it_bit_buff, 4) + 1; |
189 | | |
190 | 170k | if (latm_element->num_program > LATM_MAX_PROG) return IA_FATAL_ERROR; |
191 | | |
192 | 328k | for (prog = 0; prog < latm_element->num_program; prog++) { |
193 | 170k | latm_element->num_layer = ixheaacd_read_bits_buf(it_bit_buff, 3) + 1; |
194 | | |
195 | 330k | for (lay = 0; lay < latm_element->num_layer; lay++) { |
196 | 172k | layer_info = &latm_element->layer_info[prog][lay]; |
197 | 172k | layer_info->frame_len_bits = 0; |
198 | | |
199 | 172k | if ((prog == 0) && (lay == 0)) { |
200 | 170k | WORD32 asc_len, pos; |
201 | | |
202 | 170k | latm_element->use_same_config = 0; |
203 | | |
204 | 170k | asc_len = (latm_element->audio_mux_version == 1) |
205 | 170k | ? ixheaacd_latm_get_value(it_bit_buff) |
206 | 170k | : 0; |
207 | 170k | pos = it_bit_buff->size - it_bit_buff->cnt_bits; |
208 | | |
209 | 170k | if (asc_len > it_bit_buff->size - 106 || asc_len > 2592 || |
210 | 170k | asc_len < 0) { |
211 | 9 | return IA_XHEAAC_DEC_INIT_FATAL_DEC_INIT_FAIL; |
212 | 9 | } |
213 | | |
214 | 170k | if ((error_code = ixheaacd_ga_hdr_dec( |
215 | 170k | aac_state_struct, it_bit_buff->cnt_bits, &bytes_consumed, |
216 | 170k | sample_rate_info, it_bit_buff))) |
217 | 11.9k | return (error_code); |
218 | | |
219 | 158k | if (asc_len) { |
220 | 23 | asc_len -= (it_bit_buff->size - it_bit_buff->cnt_bits) - pos; |
221 | 23 | ixheaacd_read_bidirection(it_bit_buff, asc_len); |
222 | 23 | } |
223 | | |
224 | 158k | layer_info->asc.aot = aac_state_struct->audio_object_type; |
225 | 158k | layer_info->asc.channel_config = aac_state_struct->ch_config; |
226 | 158k | layer_info->asc.samples_per_frame = aac_state_struct->frame_len_flag; |
227 | 158k | layer_info->asc.sampling_freq = aac_state_struct->sampling_rate; |
228 | 158k | layer_info->asc.samples_per_frame = aac_state_struct->frame_length; |
229 | 158k | } else { |
230 | 2.18k | latm_element->use_same_config = |
231 | 2.18k | ixheaacd_read_bits_buf(it_bit_buff, 1); |
232 | | |
233 | 2.18k | if (latm_element->use_same_config && (lay > 0)) { |
234 | 1.95k | layer_info->asc = latm_element->layer_info[prog][lay - 1].asc; |
235 | 1.95k | } else { |
236 | 230 | if ((error_code = ixheaacd_ga_hdr_dec( |
237 | 230 | aac_state_struct, it_bit_buff->cnt_bits, &bytes_consumed, |
238 | 230 | sample_rate_info, it_bit_buff))) |
239 | 214 | return (error_code); |
240 | 230 | } |
241 | 2.18k | } |
242 | | |
243 | 160k | layer_info->frame_len_type = ixheaacd_read_bits_buf(it_bit_buff, 3); |
244 | | |
245 | 160k | switch (layer_info->frame_len_type) { |
246 | 160k | case 0: |
247 | 160k | layer_info->buffer_fullness = |
248 | 160k | ixheaacd_read_bits_buf(it_bit_buff, 8); |
249 | | |
250 | 160k | if (!latm_element->all_streams_same_time_framing) { |
251 | 203 | if ((lay > 0) && layer_info->asc.aot == AOT_AAC_SCAL) { |
252 | 0 | } |
253 | 203 | } |
254 | 160k | break; |
255 | | |
256 | 47 | case 1: |
257 | 47 | latm_element->frame_length = ixheaacd_read_bits_buf(it_bit_buff, 9); |
258 | 124 | default: |
259 | 124 | return IA_XHEAAC_DEC_EXE_FATAL_INVALID_LOAS_HEADER; |
260 | 160k | } |
261 | 160k | } |
262 | 170k | } |
263 | | |
264 | 157k | latm_element->other_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1); |
265 | | |
266 | 157k | if (latm_element->other_data_present) { |
267 | 4.58k | if (latm_element->audio_mux_version == 1) { |
268 | 21 | latm_element->other_data_length = ixheaacd_latm_get_value(it_bit_buff); |
269 | 4.56k | } else { |
270 | 4.56k | UWORD32 other_data_len; |
271 | 4.56k | latm_element->other_data_length = 0; |
272 | 4.74k | do { |
273 | 4.74k | other_data_len = ixheaacd_read_bits_buf(it_bit_buff, 1); |
274 | 4.74k | latm_element->other_data_length <<= 8; |
275 | 4.74k | latm_element->other_data_length += |
276 | 4.74k | ixheaacd_read_bits_buf(it_bit_buff, 8); |
277 | 4.74k | if (latm_element->other_data_length > (UWORD32)it_bit_buff->cnt_bits) |
278 | 34 | return IA_FATAL_ERROR; |
279 | 4.74k | } while (other_data_len); |
280 | 4.56k | } |
281 | 4.58k | } |
282 | | |
283 | 157k | latm_element->crc_check_present = ixheaacd_read_bits_buf(it_bit_buff, 1); |
284 | | |
285 | 157k | if (latm_element->crc_check_present) { |
286 | 54.0k | latm_element->crc_check_sum = ixheaacd_read_bits_buf(it_bit_buff, 8); |
287 | 54.0k | } |
288 | 157k | } else { |
289 | 28 | error_code = IA_XHEAAC_DEC_EXE_FATAL_INVALID_LOAS_HEADER; |
290 | 28 | } |
291 | 157k | return (error_code); |
292 | 170k | } |
293 | | |
294 | | IA_ERRORCODE ixheaacd_latm_audio_mux_element( |
295 | | struct ia_bit_buf_struct *it_bit_buff, ixheaacd_latm_struct *latm_element, |
296 | | ia_aac_dec_state_struct *aac_state_struct, |
297 | 237k | ia_sampling_rate_info_struct *sample_rate_info) { |
298 | 237k | UWORD32 i; |
299 | 237k | IA_ERRORCODE error_code = AAC_DEC_OK; |
300 | | |
301 | 237k | ixheaacd_read_bits_buf(it_bit_buff, 13); |
302 | | |
303 | 237k | latm_element->use_same_stream_mux = ixheaacd_read_bits_buf(it_bit_buff, 1); |
304 | | |
305 | 237k | if (!latm_element->use_same_stream_mux) { |
306 | 170k | if ((error_code = ixheaacd_latm_stream_mux_config( |
307 | 170k | it_bit_buff, latm_element, aac_state_struct, sample_rate_info))) { |
308 | 12.5k | return (error_code); |
309 | 12.5k | } |
310 | 170k | } |
311 | | |
312 | 449k | for (i = 0; i < latm_element->num_sub_frames; i++) { |
313 | 224k | if ((error_code = |
314 | 224k | ixheaacd_latm_payload_length_info(it_bit_buff, latm_element))) { |
315 | 10 | if (error_code != 0) return (error_code); |
316 | 10 | } |
317 | 224k | } |
318 | | |
319 | 224k | return (error_code); |
320 | 224k | } |