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