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 <stdio.h>
21 #include <string.h>
22 #include <math.h>
23 
24 #include <ixheaacd_type_def.h>
25 #include <ixheaacd_type_def.h>
26 #include "ixheaacd_memory_standards.h"
27 #include "ixheaacd_sbrdecsettings.h"
28 #include "ixheaacd_env_extr_part.h"
29 #include "ixheaacd_defines.h"
30 #include <ixheaacd_aac_rom.h>
31 #include "ixheaacd_common_rom.h"
32 #include <ixheaacd_sbr_rom.h>
33 #include "ixheaacd_bitbuffer.h"
34 #include "ixheaacd_pulsedata.h"
35 #include "ixheaacd_pns.h"
36 
37 #include "ixheaacd_sbr_common.h"
38 #include "ixheaacd_drc_data_struct.h"
39 #include "ixheaacd_drc_dec.h"
40 
41 #include "ixheaacd_lt_predict.h"
42 #include "ixheaacd_channelinfo.h"
43 #include "ixheaacd_channel.h"
44 #include "ixheaacd_sbrdecoder.h"
45 #include "ixheaacd_audioobjtypes.h"
46 #include "ixheaacd_latmdemux.h"
47 #include "ixheaacd_aacdec.h"
48 #include "ixheaacd_sbr_common.h"
49 
50 #include "ixheaacd_mps_polyphase.h"
51 #include "ixheaacd_config.h"
52 #include "ixheaacd_mps_dec.h"
53 #include "ixheaacd_mps_interface.h"
54 #include "ixheaacd_struct_def.h"
55 #include "ixheaacd_config.h"
56 #include "ixheaacd_bitbuffer.h"
57 #include "ixheaacd_interface.h"
58 
59 #include "ixheaacd_tns_usac.h"
60 #include "ixheaacd_cnst.h"
61 #include "ixheaacd_acelp_info.h"
62 #include "ixheaacd_sbrdecsettings.h"
63 #include "ixheaacd_info.h"
64 #include "ixheaacd_struct.h"
65 
66 #include "ixheaacd_sbrdecoder.h"
67 #include "ixheaacd_mps_polyphase.h"
68 #include "ixheaacd_sbr_const.h"
69 #include "ixheaacd_main.h"
70 #include "ixheaacd_arith_dec.h"
71 #include "ixheaacd_bit_extract.h"
72 #include "ixheaacd_create.h"
73 
74 #include "ixheaacd_func_def.h"
75 #include "ixheaacd_mps_interface.h"
76 #include "ixheaacd_mps_polyphase.h"
77 
78 #include "ixheaacd_defines.h"
79 
80 #include "ixheaacd_sbr_scale.h"
81 #include "ixheaacd_lpp_tran.h"
82 #include "ixheaacd_hybrid.h"
83 #include "ixheaacd_ps_dec.h"
84 #include "ixheaacd_env_extr.h"
85 #include "ixheaacd_env_calc.h"
86 #include "ixheaacd_qmf_dec.h"
87 
88 #include "ixheaacd_pvc_dec.h"
89 #include "ixheaacd_sbr_dec.h"
90 
91 #include "ixheaacd_error_codes.h"
92 
93 #define MAXNRSBRELEMENTS 6
94 
95 VOID ixheaacd_allocate_sbr_scr(ia_sbr_scr_struct *sbr_scratch_struct,
96                                VOID *base_scratch_ptr, VOID *output_ptr,
97                                WORD total_elements, WORD ch_fac,
98                                WORD32 object_type);
99 
100 WORD16 ixheaacd_applysbr(
101     ia_handle_sbr_dec_inst_struct self,
102     ia_aac_dec_sbr_bitstream_struct *p_sbr_bit_stream, WORD16 *core_sample_buf,
103     WORD16 *codec_num_channels, FLAG frame_status, FLAG down_samp_flag,
104     FLAG down_mix_flag, ia_sbr_scr_struct *sbr_scratch_struct, WORD32 ps_enable,
105     WORD32 ch_fac, WORD32 slot_element, ia_bit_buf_struct *it_bit_buff,
106     ia_drc_dec_struct *pstr_drc_dec, WORD eld_sbr_flag, WORD32 object_type);
107 
ixheaacd_esbr_process(ia_usac_data_struct * usac_data,ia_bit_buf_struct * it_bit_buff,WORD32 stereo_config_idx,WORD16 num_channels,WORD32 audio_object_type)108 WORD16 ixheaacd_esbr_process(ia_usac_data_struct *usac_data,
109                              ia_bit_buf_struct *it_bit_buff,
110                              WORD32 stereo_config_idx, WORD16 num_channels,
111                              WORD32 audio_object_type) {
112   WORD16 err_code = 0;
113   ia_aac_dec_sbr_bitstream_struct *esbr_bit_str = &usac_data->esbr_bit_str[0];
114   ia_handle_sbr_dec_inst_struct self = usac_data->pstr_esbr_dec;
115 
116   ia_sbr_scr_struct sbr_scratch_struct;
117   ixheaacd_allocate_sbr_scr(&sbr_scratch_struct,
118                             usac_data->sbr_scratch_mem_base, NULL, 2, 1,
119                             audio_object_type);
120 
121   self->usac_independency_flag = usac_data->usac_independency_flg;
122 
123   self->time_sample_buf[0] = usac_data->time_sample_vector[0];
124   self->time_sample_buf[1] = usac_data->time_sample_vector[1];
125   self->stereo_config_idx = stereo_config_idx;
126 
127   self->sbr_mode = usac_data->sbr_mode;
128   self->aot_usac_flag = usac_data->usac_flag;
129 
130   err_code = ixheaacd_applysbr(self, esbr_bit_str, NULL, &num_channels, 1, 0, 0,
131                                &sbr_scratch_struct, 0, 1, 0, it_bit_buff, NULL,
132                                0, audio_object_type);
133 
134   usac_data->sbr_mode = self->sbr_mode;
135 
136   return err_code;
137 }
138 
ixheaacd_sbr_ext_data_read(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_sbr_bitstream_struct * esbr_bit_str)139 static VOID ixheaacd_sbr_ext_data_read(
140     ia_bit_buf_struct *it_bit_buff,
141     ia_aac_dec_sbr_bitstream_struct *esbr_bit_str) {
142   WORD32 count = 0;
143   WORD32 read_bits = 0;
144   WORD32 unaligned_bits = 0;
145   WORD32 cnt_bits_in;
146 
147   cnt_bits_in = it_bit_buff->cnt_bits;
148   count = (it_bit_buff->cnt_bits) >> 3;
149   if (count > 0) {
150     if ((esbr_bit_str->no_elements < MAXNRSBRELEMENTS)) {
151       esbr_bit_str->str_sbr_ele[esbr_bit_str->no_elements].extension_type =
152           EXT_SBR_DATA;
153       esbr_bit_str->str_sbr_ele[esbr_bit_str->no_elements].size_payload = count;
154 
155       read_bits = count << 3;
156 
157       unaligned_bits = (cnt_bits_in - read_bits);
158       if (unaligned_bits > 0 && unaligned_bits < 8) {
159         count++;
160         esbr_bit_str->str_sbr_ele[esbr_bit_str->no_elements].size_payload =
161             count;
162       }
163       esbr_bit_str->no_elements += 1;
164     }
165   }
166 }
167 
ixheaacd_read_ext_element(UWORD32 usac_ext_element_default_length,UWORD32 usac_ext_element_payload_frag,ia_bit_buf_struct * it_bit_buff,ia_usac_decoder_config_struct * pstr_usac_dec_config,WORD32 elem_idx)168 static WORD32 ixheaacd_read_ext_element(
169     UWORD32 usac_ext_element_default_length,
170     UWORD32 usac_ext_element_payload_frag, ia_bit_buf_struct *it_bit_buff,
171     ia_usac_decoder_config_struct *pstr_usac_dec_config, WORD32 elem_idx) {
172   UWORD32 usac_ext_element_present;
173   UWORD32 usac_ext_element_use_dft_length;
174   UWORD32 pay_load_length, tmp;
175   UWORD32 i;
176   usac_ext_element_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
177 
178   if (usac_ext_element_present) {
179     usac_ext_element_use_dft_length = ixheaacd_read_bits_buf(it_bit_buff, 1);
180 
181     if (usac_ext_element_use_dft_length) {
182       pay_load_length = usac_ext_element_default_length;
183     } else {
184       pay_load_length = ixheaacd_read_bits_buf(it_bit_buff, 8);
185 
186       if (pay_load_length == 255) {
187         WORD32 val_add = 0;
188         val_add = ixheaacd_read_bits_buf(it_bit_buff, 16);
189         pay_load_length = (UWORD32)((WORD32)pay_load_length + val_add - 2);
190       }
191     }
192     if ((it_bit_buff->cnt_bits >> 3) < (WORD32)pay_load_length)
193       return IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES;
194     if (pay_load_length > 0) {
195       if (usac_ext_element_payload_frag)
196         tmp = ixheaacd_read_bits_buf(it_bit_buff, 2);
197 
198       if (pstr_usac_dec_config->usac_ext_ele_payload_present[elem_idx]) {
199         for (i = 0; i < pay_load_length; i++) {
200           pstr_usac_dec_config->usac_ext_gain_payload_buf
201               [i + pstr_usac_dec_config->usac_ext_gain_payload_len] =
202               ixheaacd_read_bits_buf(it_bit_buff, 8);
203         }
204         pstr_usac_dec_config->usac_ext_gain_payload_len += pay_load_length;
205       } else {
206         it_bit_buff->ptr_read_next =
207             it_bit_buff->ptr_read_next + pay_load_length;
208         it_bit_buff->cnt_bits =
209             it_bit_buff->cnt_bits - (WORD32)(pay_load_length * 8);
210       }
211     }
212   }
213   return 0;
214 }
215 
ixheaacd_sbr_ele_type_set(ia_aac_dec_sbr_bitstream_struct * esbr_bit_str0,ia_aac_dec_sbr_bitstream_struct * esbr_bit_str1,WORD32 ele_id,WORD32 st_config_idx)216 static VOID ixheaacd_sbr_ele_type_set(
217     ia_aac_dec_sbr_bitstream_struct *esbr_bit_str0,
218     ia_aac_dec_sbr_bitstream_struct *esbr_bit_str1, WORD32 ele_id,
219     WORD32 st_config_idx) {
220   if (ele_id == ID_USAC_SCE) {
221     esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
222         SBR_ID_SCE;
223     esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
224         SBR_ID_SCE;
225   }
226   if (ele_id == ID_USAC_CPE) {
227     if ((st_config_idx == 0) || (st_config_idx == 3)) {
228       esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
229           SBR_ID_CPE;
230       esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
231           SBR_ID_CPE;
232     } else {
233       esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
234           SBR_ID_SCE;
235       esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
236           SBR_ID_SCE;
237     }
238   }
239   if (ele_id == ID_USAC_LFE) {
240     esbr_bit_str0->str_sbr_ele[esbr_bit_str0->no_elements].sbr_ele_id =
241         SBR_ID_SCE;
242     esbr_bit_str1->str_sbr_ele[esbr_bit_str1->no_elements].sbr_ele_id =
243         SBR_ID_SCE;
244 
245     esbr_bit_str0->no_elements++;
246     esbr_bit_str0->str_sbr_ele[0].size_payload = 0;
247   }
248 }
249 
ixheaacd_ms_processing(ia_usac_data_struct * pstr_usac_data)250 VOID ixheaacd_ms_processing(ia_usac_data_struct *pstr_usac_data) {
251   WORD32 i;
252 
253   FLOAT32 tmp, tmp1;
254   FLOAT32 ms_factor = (FLOAT32)0.7071067812;
255   for (i = 0; i < pstr_usac_data->ccfl; i++) {
256     tmp = (FLOAT32)((pstr_usac_data->time_sample_vector[0][i] +
257                      pstr_usac_data->time_sample_vector[1][i]) *
258                     ms_factor);
259     tmp1 = (FLOAT32)((pstr_usac_data->time_sample_vector[0][i] -
260                       pstr_usac_data->time_sample_vector[1][i]) *
261                      ms_factor);
262     pstr_usac_data->time_sample_vector[1][i] = tmp1;
263     pstr_usac_data->time_sample_vector[0][i] = tmp;
264   }
265 }
266 
ixheaacd_usac_process(ia_dec_data_struct * pstr_dec_data,WORD32 * num_out_channels,VOID * codec_handle)267 WORD32 ixheaacd_usac_process(ia_dec_data_struct *pstr_dec_data,
268                              WORD32 *num_out_channels, VOID *codec_handle) {
269   WORD32 ele_id = 0;
270   WORD32 err_code = 0;
271 
272   ia_aac_dec_state_struct *p_state_aac_dec =
273       (ia_aac_dec_state_struct *)codec_handle;
274 
275   ia_usac_data_struct *pstr_usac_data = &(pstr_dec_data->str_usac_data);
276   ia_bit_buf_struct *it_bit_buff = &pstr_dec_data->dec_bit_buf;
277 
278   ia_frame_data_struct *fd = &(pstr_dec_data->str_frame_data);
279 
280   ia_usac_config_struct *pstr_usac_config =
281       &(fd->str_audio_specific_config.str_usac_config);
282   ia_usac_decoder_config_struct *pstr_usac_dec_config =
283       &(fd->str_audio_specific_config.str_usac_config.str_usac_dec_config);
284 
285   WORD32 err = 0;
286   WORD16 nr_core_coder_channels = 0;
287   WORD32 ch_offset = 0;
288 
289   WORD32 elem_idx = 0;
290   WORD32 num_elements = pstr_usac_dec_config->num_elements;
291 
292   pstr_usac_dec_config->usac_ext_gain_payload_len = 0;
293   pstr_usac_data->usac_independency_flg =
294       ixheaacd_read_bits_buf(it_bit_buff, 1);
295 
296   for (elem_idx = 0; elem_idx < num_elements; elem_idx++) {
297     WORD32 stereo_config_index =
298         pstr_usac_config->str_usac_dec_config.str_usac_element_config[elem_idx]
299             .stereo_config_index;
300 
301     pstr_usac_data->esbr_bit_str[0].no_elements = 0;
302     pstr_usac_data->esbr_bit_str[1].no_elements = 0;
303 
304     switch (ele_id = pstr_usac_dec_config->usac_element_type[elem_idx]) {
305       case ID_USAC_SCE:
306         nr_core_coder_channels = 1;
307         goto core_data_extracting;
308 
309       case ID_USAC_CPE:
310         nr_core_coder_channels = (stereo_config_index == 1) ? 1 : 2;
311         if ((stereo_config_index > 1) &&
312             (p_state_aac_dec->num_of_output_ch < 2))
313           return -1;
314         goto core_data_extracting;
315       case ID_USAC_LFE:
316         nr_core_coder_channels = 1;
317 
318       core_data_extracting:
319         if (ch_offset >= MAX_NUM_CHANNELS) return -1;
320         err = ixheaacd_core_coder_data(ele_id, pstr_usac_data, elem_idx,
321                                        &ch_offset, it_bit_buff,
322                                        nr_core_coder_channels);
323         if (err != 0) return -1;
324 
325         ixheaacd_sbr_ele_type_set(&pstr_usac_data->esbr_bit_str[0],
326                                   &pstr_usac_data->esbr_bit_str[1], ele_id,
327                                   stereo_config_index);
328 
329         if (pstr_usac_data->mps_pseudo_lr[elem_idx])
330           ixheaacd_ms_processing(pstr_usac_data);
331 
332         if (ele_id != ID_USAC_LFE) {
333           if (pstr_usac_data->sbr_ratio_idx > 0)
334             ixheaacd_sbr_ext_data_read(it_bit_buff,
335                                        &pstr_usac_data->esbr_bit_str[0]);
336         }
337 
338         if ((pstr_usac_data->sbr_ratio_idx > 0) &&
339             (pstr_usac_data->esbr_bit_str[0].no_elements != 0)) {
340           err_code = ixheaacd_esbr_process(
341               pstr_usac_data, it_bit_buff, stereo_config_index,
342               nr_core_coder_channels,
343               pstr_dec_data->str_usac_data.audio_object_type);
344           if (err_code < 0) return err_code;
345         }
346 
347         if (stereo_config_index > 0) {
348           FLOAT32 **ptr_inp[2 * 2];
349           WORD32 ch;
350 
351           *num_out_channels = p_state_aac_dec->mps_dec_handle.out_ch_count;
352 
353           ixheaacd_mps_frame_parsing(&p_state_aac_dec->mps_dec_handle,
354                                      pstr_usac_data->usac_independency_flg,
355                                      it_bit_buff);
356 
357           for (ch = 0; ch < nr_core_coder_channels; ch++) {
358             ptr_inp[2 * ch] =
359                 pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[ch]
360                     ->str_sbr_dec.pp_qmf_buf_real;
361             ptr_inp[2 * ch + 1] =
362                 pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[ch]
363                     ->str_sbr_dec.pp_qmf_buf_imag;
364             p_state_aac_dec->mps_dec_handle.p_sbr_dec[ch] =
365                 (VOID *)(&pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[ch]
366                               ->str_sbr_dec);
367             p_state_aac_dec->mps_dec_handle.p_sbr_header[ch] =
368                 (VOID *)(pstr_usac_data->pstr_esbr_dec->pstr_sbr_header[ch]);
369             p_state_aac_dec->mps_dec_handle.p_sbr_frame[ch] =
370                 (VOID *)(pstr_usac_data->pstr_esbr_dec->frame_buffer[ch]);
371           }
372           if (nr_core_coder_channels == 1) {
373             p_state_aac_dec->mps_dec_handle.p_sbr_dec[1] =
374                 (VOID *)(&pstr_usac_data->pstr_esbr_dec->pstr_sbr_channel[1]
375                               ->str_sbr_dec);
376             p_state_aac_dec->mps_dec_handle.p_sbr_header[1] =
377                 (VOID *)(pstr_usac_data->pstr_esbr_dec->pstr_sbr_header[1]);
378             p_state_aac_dec->mps_dec_handle.p_sbr_frame[1] =
379                 (VOID *)(pstr_usac_data->pstr_esbr_dec->frame_buffer[1]);
380           }
381 
382           err = ixheaacd_mps_apply(&p_state_aac_dec->mps_dec_handle, ptr_inp,
383                                    pstr_usac_data->time_sample_vector);
384           if (err) return err;
385         }
386 
387         ch_offset += nr_core_coder_channels;
388         break;
389 
390       case ID_USAC_EXT: {
391         ia_usac_dec_element_config_struct *pusac_element_config =
392             &pstr_usac_dec_config->str_usac_element_config[elem_idx];
393         err = ixheaacd_read_ext_element(
394             pusac_element_config->usac_ext_eleme_def_len,
395             pusac_element_config->usac_ext_elem_pld_frag, it_bit_buff,
396             pstr_usac_dec_config, elem_idx);
397         if (err != 0) return err;
398 
399         break;
400       }
401 
402       default:
403 
404         return -1;
405 
406         break;
407     }
408   }
409 
410   return 0;
411 }
412