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