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 <stdlib.h>
21 #include <string.h>
22 #include <ixheaacd_type_def.h>
23 #include <ixheaacd_type_def.h>
24 #include "ixheaacd_memory_standards.h"
25 #include "ixheaacd_sbrdecsettings.h"
26 #include "ixheaacd_env_extr_part.h"
27 #include "ixheaacd_defines.h"
28 #include <ixheaacd_aac_rom.h>
29 #include "ixheaacd_common_rom.h"
30 #include <ixheaacd_sbr_rom.h>
31 #include "ixheaacd_bitbuffer.h"
32 #include "ixheaacd_pulsedata.h"
33 #include "ixheaacd_pns.h"
34
35 #include "ixheaacd_lt_predict.h"
36
37 #include "ixheaacd_channelinfo.h"
38 #include "ixheaacd_sbr_common.h"
39 #include "ixheaacd_drc_data_struct.h"
40 #include "ixheaacd_drc_dec.h"
41 #include "ixheaacd_channel.h"
42 #include "ixheaacd_channelinfo.h"
43 #include "ixheaacd_sbrdecoder.h"
44 #include "ixheaacd_audioobjtypes.h"
45 #include "ixheaacd_latmdemux.h"
46 #include "ixheaacd_aacdec.h"
47 #include "ixheaacd_sbr_common.h"
48
49 #include "ixheaacd_mps_polyphase.h"
50 #include "ixheaacd_config.h"
51 #include "ixheaacd_mps_dec.h"
52
53 #include "ixheaacd_struct_def.h"
54
55 #include <ixheaacd_type_def.h>
56 #include "ixheaacd_bitbuffer.h"
57 #include "ixheaacd_interface.h"
58
59 #include "ixheaacd_tns_usac.h"
60 #include "ixheaacd_cnst.h"
61
62 #include "ixheaacd_acelp_info.h"
63
64 #include "ixheaacd_sbrdecsettings.h"
65 #include "ixheaacd_info.h"
66 #include "ixheaacd_sbrdecoder.h"
67 #include "ixheaacd_mps_polyphase.h"
68 #include "ixheaacd_sbr_const.h"
69 #include "ixheaacd_main.h"
70
71 #include "ixheaacd_arith_dec.h"
72
73 #include "ixheaacd_config.h"
74 #include "ixheaacd_struct.h"
75
76 #include "ixheaacd_create.h"
77
78 #include "ixheaacd_dec_main.h"
79
ixheaacd_samples_sat(WORD8 * outbuffer,WORD32 num_samples_out,WORD32 pcmsize,FLOAT32 (* out_samples)[4096],WORD32 * out_bytes,WORD32 num_channel_out)80 VOID ixheaacd_samples_sat(WORD8 *outbuffer, WORD32 num_samples_out,
81 WORD32 pcmsize, FLOAT32 (*out_samples)[4096],
82 WORD32 *out_bytes, WORD32 num_channel_out) {
83 WORD32 num;
84 WORD32 i;
85 WORD64 write_local;
86
87 WORD16 *out_buf = (WORD16 *)outbuffer;
88
89 num = num_channel_out * num_samples_out;
90
91 if (pcmsize == 16) {
92 for (i = 0; i < num; i++) {
93 write_local =
94 ((WORD64)(out_samples[i % num_channel_out][i / num_channel_out]));
95
96 if (write_local > 32767) {
97 write_local = 32767;
98 }
99 if (write_local < -32768) {
100 write_local = -32768;
101 }
102 out_buf[i] = (WORD16)write_local;
103 }
104
105 *out_bytes = num * sizeof(WORD16);
106 } else {
107 WORD8 *out_24bit = (WORD8 *)out_buf;
108 for (i = 0; i < num; i++) {
109 write_local = ((WORD64)(
110 out_samples[i % num_channel_out][i / num_channel_out] * 256));
111
112 if (write_local > 8388607) {
113 write_local = 8388607;
114 }
115 if (write_local < -8388608) {
116 write_local = -8388608;
117 }
118 *out_24bit++ = (WORD32)write_local & 0xff;
119 *out_24bit++ = ((WORD32)write_local >> 8) & 0xff;
120 *out_24bit++ = ((WORD32)write_local >> 16) & 0xff;
121 }
122
123 *out_bytes = num * 3 * sizeof(WORD8);
124 }
125 }
126
127 /* audio pre roll frame parsing*/
ixheaacd_audio_preroll_parsing(ia_dec_data_struct * pstr_dec_data,UWORD8 * conf_buf,WORD32 * preroll_units,WORD32 * preroll_frame_offset)128 static WORD32 ixheaacd_audio_preroll_parsing(ia_dec_data_struct *pstr_dec_data,
129 UWORD8 *conf_buf,
130 WORD32 *preroll_units,
131 WORD32 *preroll_frame_offset) {
132 ia_bit_buf_struct *temp_buff =
133 (ia_bit_buf_struct *)&(pstr_dec_data->dec_bit_buf);
134 WORD32 independency_flag = 0;
135 WORD32 ext_ele_present = 0;
136 WORD32 ext_ele_use_dflt_len = 0;
137 WORD32 ext_ele_payload_len = 0;
138
139 WORD32 apply_crossfade = 0;
140 WORD32 un_used_val = 0;
141 WORD32 num_pre_roll_frames = 0;
142
143 WORD32 frame_idx = 0;
144 WORD32 frame_len[18] = {
145 0}; // max of escapedValue(2, 4, 0) i.e. 2^2 -1 + 2^4 -1;
146 WORD32 temp = 0;
147
148 WORD32 config_len = 0;
149 WORD32 loop;
150
151 if (pstr_dec_data->str_frame_data.str_audio_specific_config.str_usac_config
152 .str_usac_dec_config.usac_element_type[0] == ID_USAC_EXT) {
153 temp = ixheaacd_show_bits_buf(temp_buff, 3);
154 independency_flag = (temp >> 2) & 0x1;
155 ext_ele_present = (temp >> 1) & 0x1;
156
157 if (ext_ele_present) {
158 ext_ele_use_dflt_len = temp & 0x1; // ixheaacd_read_bit(&temp_buff, 1);
159 if (ext_ele_use_dflt_len != 0) return 0;
160
161 un_used_val = ixheaacd_read_bits_buf(temp_buff, 3);
162
163 ext_ele_payload_len = ixheaacd_read_bits_buf(temp_buff, 8);
164
165 if (ext_ele_payload_len == 255) {
166 WORD32 val_add = 0;
167 val_add = ixheaacd_read_bits_buf(temp_buff, 16);
168 ext_ele_payload_len =
169 (UWORD32)((WORD32)ext_ele_payload_len + val_add - 2);
170 }
171
172 // escapedValue(4, 4, 8);
173 config_len = ixheaacd_read_bits_buf(temp_buff, 4);
174 if (config_len == 15) {
175 WORD32 val_add = 0;
176 val_add = ixheaacd_read_bits_buf(temp_buff, 4);
177 config_len += val_add;
178 if (val_add == 15) {
179 WORD32 val_add1 = 0;
180 val_add1 = ixheaacd_read_bits_buf(temp_buff, 8);
181 config_len += val_add1;
182 }
183 }
184
185 for (loop = 0; loop < config_len; loop++)
186 conf_buf[loop] = ixheaacd_read_bits_buf(temp_buff, 8);
187
188 apply_crossfade = ixheaacd_read_bits_buf(temp_buff, 1);
189 un_used_val = ixheaacd_read_bits_buf(temp_buff, 1); // reserverd
190
191 // escapedValue(2, 4, 0);
192 num_pre_roll_frames = ixheaacd_read_bits_buf(temp_buff, 2);
193 if (num_pre_roll_frames == 3) {
194 WORD32 val_add = 0;
195 val_add = ixheaacd_read_bits_buf(temp_buff, 4);
196 num_pre_roll_frames += val_add;
197 }
198
199 for (frame_idx = 0; frame_idx < num_pre_roll_frames; frame_idx++) {
200 WORD32 au_len = 0; // escapedValued(16,16,0)
201 au_len = ixheaacd_read_bits_buf(temp_buff, 16);
202 if (au_len == 65535) {
203 WORD32 val_add = ixheaacd_read_bits_buf(temp_buff, 16);
204 au_len += val_add;
205 }
206 preroll_frame_offset[frame_idx] = temp_buff->size - temp_buff->cnt_bits;
207 frame_len[frame_idx] =
208 (8 * au_len) + (temp_buff->size - temp_buff->cnt_bits);
209 temp_buff->ptr_read_next += au_len;
210 temp_buff->cnt_bits -= au_len * 8;
211 }
212 }
213 }
214 *preroll_units = num_pre_roll_frames;
215 return config_len;
216 }
217
ixheaacd_dec_main(VOID * temp_handle,WORD8 * inbuffer,WORD8 * outbuffer,WORD32 * out_bytes,WORD32 frames_done,WORD32 pcmsize,WORD32 * num_channel_out)218 WORD32 ixheaacd_dec_main(VOID *temp_handle, WORD8 *inbuffer, WORD8 *outbuffer,
219 WORD32 *out_bytes, WORD32 frames_done, WORD32 pcmsize,
220 WORD32 *num_channel_out) {
221 WORD32 err = 0;
222 ia_exhaacplus_dec_api_struct *handle =
223 (ia_exhaacplus_dec_api_struct *)temp_handle;
224 ia_aac_dec_state_struct *aac_dec_handle = handle->p_state_aac;
225
226 WORD32 tmp;
227 ia_audio_specific_config_struct *pstr_audio_specific_config =
228 (ia_audio_specific_config_struct *)
229 aac_dec_handle->ia_audio_specific_config;
230 WORD32 suitable_tracks = 1;
231 WORD32 num_samples_out;
232 ia_dec_data_struct *pstr_dec_data;
233 UWORD8 config[285]; // max of escapedValue(4, 4, 8) i.e. 2^4 -1 + 2^4 -1 +
234 // 2^8 -1;
235 WORD32 config_len;
236 WORD32 delay;
237 WORD preroll_frame_offset[4] = {0};
238 WORD preroll_units = -1;
239 WORD32 access_units = 0;
240
241 if (frames_done == 0) {
242 if ((pstr_audio_specific_config->channel_configuration > 2) ||
243 (pstr_audio_specific_config->channel_configuration == 0)) {
244 return -1;
245 }
246
247 pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
248
249 tmp = pstr_audio_specific_config->channel_configuration;
250
251 suitable_tracks =
252 ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
253
254 pstr_audio_specific_config->channel_configuration = tmp;
255
256 if (suitable_tracks <= 0) {
257 return -1;
258 }
259 }
260
261 {
262 pstr_dec_data = (ia_dec_data_struct *)aac_dec_handle->pstr_dec_data;
263
264 if (frames_done == 0) {
265 WORD32 delay;
266 delay = ixheaacd_decode_create(
267 handle, pstr_dec_data,
268 pstr_dec_data->str_frame_data.scal_out_select + 1);
269 pstr_dec_data->dec_bit_buf.max_size =
270 handle->p_mem_info_aac[IA_MEMTYPE_INPUT].ui_size;
271 if (delay == -1) return -1;
272 *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
273 return 0;
274 }
275
276 pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
277 pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
278 pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
279 (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
280 pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
281 pstr_dec_data->dec_bit_buf.bit_pos = 7;
282 pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
283
284 pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
285 if (pstr_dec_data->dec_bit_buf.size > pstr_dec_data->dec_bit_buf.max_size)
286 pstr_dec_data->dec_bit_buf.max_size = pstr_dec_data->dec_bit_buf.size;
287 /* audio pre roll frame parsing*/
288
289 do {
290 config_len = 0;
291 if (access_units == 0 &&
292 pstr_audio_specific_config->str_usac_config.str_usac_dec_config
293 .preroll_flag) {
294 config_len = ixheaacd_audio_preroll_parsing(pstr_dec_data, &config[0],
295 &preroll_units,
296 &preroll_frame_offset[0]);
297 }
298
299 if (config_len != 0) {
300 /* updating the config parameters*/
301 ia_bit_buf_struct *config_bit_buf =
302 (ia_bit_buf_struct *)malloc(sizeof(ia_bit_buf_struct));
303
304 config_bit_buf->ptr_bit_buf_base = config;
305 config_bit_buf->size = config_len << 3;
306 config_bit_buf->ptr_read_next = config_bit_buf->ptr_bit_buf_base;
307 config_bit_buf->ptr_bit_buf_end = (UWORD8 *)config + config_len;
308 config_bit_buf->bit_pos = 7;
309 config_bit_buf->cnt_bits = config_bit_buf->size;
310
311 suitable_tracks =
312 ixheaacd_frm_data_init(pstr_audio_specific_config, pstr_dec_data);
313
314 if (suitable_tracks <= 0) return -1;
315
316 /* call codec re-configure*/
317 err = ixheaacd_config(
318 config_bit_buf, &(pstr_dec_data->str_frame_data
319 .str_audio_specific_config.str_usac_config),
320 &(pstr_audio_specific_config
321 ->channel_configuration) /*&pstr_audio_specific_config->str_usac_config*/);
322 free(config_bit_buf);
323 if (err != 0) return -1;
324
325 delay = ixheaacd_decode_create(
326 handle, pstr_dec_data,
327 pstr_dec_data->str_frame_data.scal_out_select + 1);
328 if (delay == -1) return -1;
329 *num_channel_out = pstr_dec_data->str_frame_data.scal_out_num_channels;
330 }
331
332 pstr_dec_data->dec_bit_buf.ptr_bit_buf_base = (UWORD8 *)inbuffer;
333 pstr_dec_data->dec_bit_buf.size = aac_dec_handle->ui_in_bytes << 3;
334 pstr_dec_data->dec_bit_buf.ptr_bit_buf_end =
335 (UWORD8 *)inbuffer + aac_dec_handle->ui_in_bytes - 1;
336 pstr_dec_data->dec_bit_buf.ptr_read_next = (UWORD8 *)inbuffer;
337 pstr_dec_data->dec_bit_buf.bit_pos = 7;
338 pstr_dec_data->dec_bit_buf.cnt_bits = pstr_dec_data->dec_bit_buf.size;
339
340 pstr_dec_data->str_usac_data.usac_flag = aac_dec_handle->usac_flag;
341
342 if (preroll_frame_offset[access_units]) {
343 pstr_dec_data->dec_bit_buf.cnt_bits =
344 pstr_dec_data->dec_bit_buf.size -
345 preroll_frame_offset[access_units];
346 pstr_dec_data->dec_bit_buf.bit_pos =
347 7 - preroll_frame_offset[access_units] % 8;
348 pstr_dec_data->dec_bit_buf.ptr_read_next =
349 pstr_dec_data->dec_bit_buf.ptr_read_next +
350 (preroll_frame_offset[access_units] / 8);
351 }
352
353 // temp_read=ixheaacd_show_bits_buf(pstr_dec_data->dec_bit_buf,preroll_frame_offset[access_unit]);
354
355 err =
356 ixheaacd_usac_process(pstr_dec_data, num_channel_out, aac_dec_handle);
357
358 switch (pstr_dec_data->str_usac_data.sbr_ratio_idx) {
359 case 0:
360 handle->aac_config.ui_sbr_mode = 0;
361 break;
362 case 1:
363 handle->aac_config.ui_sbr_mode = 1;
364 break;
365 case 2:
366 handle->aac_config.ui_sbr_mode = 1;
367 break;
368 case 3:
369 handle->aac_config.ui_sbr_mode = 3;
370 break;
371
372 default:
373 handle->aac_config.ui_sbr_mode = 0;
374 }
375
376 if (err == -1) return err;
377
378 num_samples_out = pstr_dec_data->str_usac_data.output_samples;
379
380 ixheaacd_samples_sat(outbuffer, num_samples_out, pcmsize,
381 pstr_dec_data->str_usac_data.time_sample_vector,
382 out_bytes, *num_channel_out);
383 pstr_audio_specific_config->str_usac_config.str_usac_dec_config
384 .usac_ext_gain_payload_len =
385 pstr_dec_data->str_frame_data.str_audio_specific_config
386 .str_usac_config.str_usac_dec_config.usac_ext_gain_payload_len;
387 memcpy(pstr_audio_specific_config->str_usac_config.str_usac_dec_config
388 .usac_ext_gain_payload_buf,
389 pstr_dec_data->str_frame_data.str_audio_specific_config
390 .str_usac_config.str_usac_dec_config.usac_ext_gain_payload_buf,
391 pstr_dec_data->str_frame_data.str_audio_specific_config
392 .str_usac_config.str_usac_dec_config
393 .usac_ext_gain_payload_len *
394 sizeof(WORD8));
395
396 access_units++;
397 preroll_units--;
398 } while (preroll_units >= 0);
399 }
400
401 return err;
402 }
403