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