• 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 <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_bitbuffer.h"
28 #include "ixheaacd_defines.h"
29 #include "ixheaacd_aac_rom.h"
30 #include "ixheaacd_pulsedata.h"
31 
32 #include "ixheaacd_pns.h"
33 #include "ixheaacd_drc_data_struct.h"
34 
35 #include "ixheaacd_lt_predict.h"
36 
37 #include "ixheaacd_cnst.h"
38 #include "ixheaacd_ec_defines.h"
39 #include "ixheaacd_ec_struct_def.h"
40 #include "ixheaacd_channelinfo.h"
41 #include "ixheaacd_drc_dec.h"
42 #include "ixheaacd_sbrdecoder.h"
43 
44 #include "ixheaacd_audioobjtypes.h"
45 #include "ixheaacd_sbrdecsettings.h"
46 #include "ixheaacd_memory_standards.h"
47 #include "ixheaacd_error_codes.h"
48 
49 #include "ixheaacd_defines.h"
50 
51 #include "ixheaacd_sbr_scale.h"
52 #include "ixheaacd_lpp_tran.h"
53 #include "ixheaacd_env_extr_part.h"
54 #include "ixheaacd_sbr_rom.h"
55 
56 #include "ixheaacd_hybrid.h"
57 #include "ixheaacd_ps_dec.h"
58 #include "ixheaacd_ps_bitdec.h"
59 
60 #include "ixheaacd_pulsedata.h"
61 
62 #include "ixheaacd_pns.h"
63 
64 #include "ixheaacd_channelinfo.h"
65 
66 #include "ixheaacd_env_extr.h"
67 #include "ixheaacd_common_rom.h"
68 #include "ixheaacd_block.h"
69 #include "ixheaacd_channel.h"
70 #include "ixheaacd_audioobjtypes.h"
71 #include "ixheaacd_latmdemux.h"
72 #include "ixheaacd_aacdec.h"
73 #include "ixheaacd_hybrid.h"
74 #include "ixheaacd_ps_dec.h"
75 
76 #include "ixheaacd_mps_polyphase.h"
77 #include "ixheaacd_config.h"
78 #include "ixheaacd_qmf_dec.h"
79 #include "ixheaacd_mps_macro_def.h"
80 #include "ixheaacd_mps_struct_def.h"
81 #include "ixheaacd_mps_res_rom.h"
82 #include "ixheaacd_mps_aac_struct.h"
83 #include "ixheaacd_mps_dec.h"
84 #include "ixheaacd_struct_def.h"
85 #include "ixheaacd_headerdecode.h"
86 
87 #include "ixheaacd_multichannel.h"
88 
89 #include "ixheaacd_basic_op.h"
90 #include "ixheaacd_intrinsics.h"
91 
92 static PLATFORM_INLINE UWORD32
ixheaacd_aac_showbits_7(ia_bit_buf_struct * it_bit_buff)93 ixheaacd_aac_showbits_7(ia_bit_buf_struct *it_bit_buff) {
94   UWORD8 *v = it_bit_buff->ptr_read_next;
95   UWORD32 b = 0;
96   UWORD32 x;
97   b = ((WORD32)v[0] << 8);
98   if (it_bit_buff->bit_pos < 6) {
99     b |= (WORD32)(v[1]);
100   }
101   x = (UWORD32)b << (15 + 8 - it_bit_buff->bit_pos);
102   x = (UWORD32)x >> (25);
103 
104   return x;
105 }
106 
ixheaacd_get_channel_mask(ia_exhaacplus_dec_api_struct * p_obj_enhaacplus_dec)107 WORD ixheaacd_get_channel_mask(
108     ia_exhaacplus_dec_api_struct *p_obj_enhaacplus_dec) {
109   WORD ixheaacd_drc_offset = 0, channel_mask = 0;
110   WORD flag1 = 0, flag2 = 0;
111   WORD ch_idx;
112   WORD *ptr_slot_element = p_obj_enhaacplus_dec->aac_config.slot_element;
113   WORD *ptr_element_type = p_obj_enhaacplus_dec->aac_config.element_type;
114 
115   memset(ptr_slot_element, 0, sizeof(WORD) * MAX_BS_ELEMENT);
116 
117   for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
118     if (ptr_element_type[ch_idx] == 1) {
119       channel_mask += 0x3;
120       ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
121       ixheaacd_drc_offset += 2;
122       flag1 = ch_idx + 1;
123       break;
124     }
125   }
126 
127   for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
128     if (ptr_element_type[ch_idx] == 0) {
129       channel_mask += 0x4;
130       ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
131       ixheaacd_drc_offset += 1;
132       flag2 = ch_idx + 1;
133       break;
134     }
135   }
136   for (ch_idx = 0; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
137     if (ptr_element_type[ch_idx] == 3) {
138       channel_mask += 0x8;
139       ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
140       ixheaacd_drc_offset += 1;
141       break;
142     }
143   }
144   for (ch_idx = flag1; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
145     if (ptr_element_type[ch_idx] == 1) {
146       channel_mask += 0x30;
147       ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
148       ixheaacd_drc_offset += 2;
149       flag1 = ch_idx + 1;
150       break;
151     }
152   }
153   for (ch_idx = flag2; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
154     if (ptr_element_type[ch_idx] == 0) {
155       channel_mask += 0x100;
156       ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
157       ixheaacd_drc_offset += 1;
158       break;
159     }
160   }
161   for (ch_idx = flag1; ch_idx < MAX_BS_ELEMENT; ch_idx++) {
162     if (ptr_element_type[ch_idx] == 1) {
163       {
164         channel_mask += (0x40 + 0x80);
165         ptr_slot_element[ch_idx] = ixheaacd_drc_offset;
166         ixheaacd_drc_offset += 2;
167         break;
168       }
169     }
170   }
171 
172   return channel_mask;
173 }
174 
ixheaacd_read_data_stream_element(ia_bit_buf_struct * it_bit_buff,WORD32 * byte_align_bits,ia_drc_dec_struct * drc_handle)175 VOID ixheaacd_read_data_stream_element(ia_bit_buf_struct *it_bit_buff,
176                                        WORD32 *byte_align_bits,
177                                        ia_drc_dec_struct *drc_handle) {
178   ia_bit_buf_struct temp_bs = {0};
179   WORD32 count = ixheaacd_read_bits_buf(it_bit_buff, 13);
180   WORD32 cnt = (count & 0xff);
181   WORD32 start_pos = 0;
182 
183   if (cnt == 255) {
184     cnt += ixheaacd_read_bits_buf(it_bit_buff, 8);
185   }
186 
187   if ((count & 0x0100) >> 8) {
188     ixheaacd_byte_align(it_bit_buff, byte_align_bits);
189   }
190 
191   {
192     memcpy(&temp_bs, it_bit_buff, sizeof(ia_bit_buf_struct));
193     start_pos = temp_bs.cnt_bits;
194 
195     if (ixheaacd_read_bits_buf(&temp_bs, 8) == DVB_ANC_DATA_SYNC_BYTE) {
196       int dmx_level_present, compression_present;
197       int coarse_gain_present, fine_grain_present;
198 
199       ixheaacd_read_bits_buf(&temp_bs, 8);
200 
201       ixheaacd_read_bits_buf(&temp_bs, 3);
202       dmx_level_present = ixheaacd_read_bits_buf(&temp_bs, 1);
203       ixheaacd_read_bits_buf(&temp_bs, 1);
204       compression_present = ixheaacd_read_bits_buf(&temp_bs, 1);
205       coarse_gain_present = ixheaacd_read_bits_buf(&temp_bs, 1);
206       fine_grain_present = ixheaacd_read_bits_buf(&temp_bs, 1);
207 
208       if (dmx_level_present) ixheaacd_read_bits_buf(&temp_bs, 8);
209 
210       if (compression_present) ixheaacd_read_bits_buf(&temp_bs, 16);
211 
212       if (coarse_gain_present) ixheaacd_read_bits_buf(&temp_bs, 16);
213 
214       if (fine_grain_present) ixheaacd_read_bits_buf(&temp_bs, 16);
215 
216       if (!drc_handle->dvb_anc_data_present && temp_bs.cnt_bits >= 0) {
217         drc_handle->dvb_anc_data_pos = start_pos;
218         drc_handle->dvb_anc_data_present = 1;
219       }
220     }
221   }
222 
223   if (it_bit_buff->cnt_bits < (cnt << 3)) {
224     longjmp(*(it_bit_buff->xaac_jmp_buf),
225             IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
226   }
227   it_bit_buff->ptr_read_next += cnt;
228   it_bit_buff->cnt_bits -= ((cnt) << 3);
229 }
230 
ixheaacd_read_fill_element(ia_bit_buf_struct * it_bit_buff,ia_drc_dec_struct * drc_dummy,ia_drc_dec_struct * ptr_drc_dec,UWORD8 * mps_buffer,WORD32 * mps_header,WORD32 * mps_bytes)231 VOID ixheaacd_read_fill_element(
232     ia_bit_buf_struct *it_bit_buff, ia_drc_dec_struct *drc_dummy,
233     ia_drc_dec_struct *ptr_drc_dec, UWORD8 *mps_buffer, WORD32 *mps_header,
234     WORD32 *mps_bytes) {
235   WORD32 count;
236   count = ixheaacd_read_bits_buf(it_bit_buff, 4);
237 
238   if ((count - 15) == 0) {
239     count = ixheaacd_read_bits_buf(it_bit_buff, 8);
240     count = (count + 14);
241   }
242 
243   if (count > 0) {
244     WORD32 extension_type;
245 
246     extension_type = ixheaacd_read_bits_buf(it_bit_buff, 4);
247 
248     if (extension_type == EXT_DYNAMIC_RANGE) {
249       ptr_drc_dec->drc_element_found = 1;
250       count -=
251           ixheaacd_dec_drc_read_element(ptr_drc_dec, drc_dummy, it_bit_buff);
252     }
253     if (EXT_SAC_DATA == extension_type) {
254       WORD32 anc_type, i;
255       anc_type = ixheaacd_read_bits_buf(it_bit_buff, 2);
256       *mps_header = anc_type;
257 
258       ixheaacd_read_bits_buf(it_bit_buff, 1);
259 
260       ixheaacd_read_bits_buf(it_bit_buff, 1);
261 
262       for (i = 0; i < count - 1; i++) {
263         mps_buffer[i] = ixheaacd_read_bits_buf(it_bit_buff, 8);
264       }
265 
266       *mps_bytes = count - 1;
267     }
268     else {
269       ixheaacd_read_bits_buf(it_bit_buff, 4);
270 
271       if (it_bit_buff->cnt_bits < ((count - 1) << 3)) {
272         longjmp(*(it_bit_buff->xaac_jmp_buf),
273                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
274       }
275       it_bit_buff->ptr_read_next += count - 1;
276       it_bit_buff->cnt_bits -= ((count - 1) << 3);
277     }
278   }
279 }
280 
ixheaacd_get_element_index_tag(ia_exhaacplus_dec_api_struct * p_obj_enhaacplus_dec,WORD ch_idx1,WORD * ch_idx,WORD * channel,WORD * ele_idx_order,WORD total_elements,WORD8 * element_used,WORD total_channels,ia_drc_dec_struct * pstr_drc_dec,ia_drc_dec_struct * drc_dummy,UWORD8 * mps_buffer,WORD32 * mps_header,WORD32 * mps_bytes)281 WORD32 ixheaacd_get_element_index_tag(
282     ia_exhaacplus_dec_api_struct *p_obj_enhaacplus_dec, WORD ch_idx1,
283     WORD *ch_idx, WORD *channel, WORD *ele_idx_order, WORD total_elements,
284     WORD8 *element_used, WORD total_channels, ia_drc_dec_struct *pstr_drc_dec,
285     ia_drc_dec_struct *drc_dummy
286     ,
287     UWORD8 *mps_buffer, WORD32 *mps_header, WORD32 *mps_bytes
288 ) {
289   WORD element_tag, j;
290   ia_aac_dec_state_struct *p_state_enhaacplus_dec =
291       p_obj_enhaacplus_dec->p_state_aac;
292 
293   ia_bit_buf_struct *it_bit_buff = p_state_enhaacplus_dec->ptr_bit_stream;
294   WORD element_idx;
295   WORD element_type;
296 
297   ia_aac_decoder_struct *aac_dec_handle =
298       p_state_enhaacplus_dec->pstr_aac_dec_info[ch_idx1];
299 
300   *ch_idx = ch_idx1;
301 
302   if (p_state_enhaacplus_dec->bs_format != LOAS_BSFORMAT) {
303     if (ch_idx1 == 0) {
304       ixheaacd_byte_align(it_bit_buff, &aac_dec_handle->byte_align_bits);
305     }
306   }
307   {
308     if (ch_idx1 == 0) {
309       aac_dec_handle->byte_align_bits = it_bit_buff->cnt_bits;
310     }
311   }
312 
313   if (it_bit_buff->cnt_bits < 3) {
314     it_bit_buff->cnt_bits = -1;
315     return (WORD16)(
316         (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
317   }
318 
319   element_tag = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 7);
320   ixheaacd_read_bidirection(it_bit_buff, -7);
321 
322   element_idx = (element_tag & 0xF);
323   element_type = (element_tag >> 4) & 0x7;
324 
325   p_obj_enhaacplus_dec->aac_config.str_prog_config.alignment_bits =
326       it_bit_buff->bit_pos;
327 
328   while (element_type == 4 || element_type == 5 || element_type == 6) {
329     WORD type = (WORD)ixheaacd_read_bits_buf(it_bit_buff, 3);
330 
331     if (it_bit_buff->cnt_bits < 3) {
332       it_bit_buff->cnt_bits = -1;
333       return (WORD16)(
334           (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
335     }
336 
337     if (type == 4) {
338       ixheaacd_read_data_stream_element(
339           it_bit_buff, &aac_dec_handle->byte_align_bits, pstr_drc_dec);
340     }
341     if (type == 5) {
342       WORD32 error_code = 0;
343       error_code = ixheaacd_decode_pce(
344           it_bit_buff, &p_obj_enhaacplus_dec->aac_config.ui_pce_found_in_hdr,
345           &p_obj_enhaacplus_dec->aac_config.str_prog_config);
346       if (error_code != 0) {
347         if (error_code < 0) return error_code;
348         return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
349       }
350     }
351     if (type == 6) {
352       ixheaacd_read_fill_element(it_bit_buff, drc_dummy, pstr_drc_dec,
353                                  mps_buffer, mps_header, mps_bytes);
354     }
355 
356     if (it_bit_buff->cnt_bits < 7) {
357       it_bit_buff->cnt_bits = -1;
358       return (WORD16)(
359           (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
360     }
361 
362     element_tag = (WORD)ixheaacd_aac_showbits_7(it_bit_buff);
363     element_idx = (element_tag & 0xF);
364     element_type = (element_tag >> 4) & 0x7;
365   }
366 
367   if (total_elements == 2 && total_channels == 2 &&
368       p_state_enhaacplus_dec->p_config->ui_pce_found_in_hdr == 2 &&
369       ch_idx1 == 0) {
370     ixheaacd_fill_prog_config_slots(p_state_enhaacplus_dec);
371   }
372 
373   *channel = 1;
374   if (element_type == 1) {
375     *channel = 2;
376   }
377 
378   for (j = 0; j < total_elements; j++) {
379     if (p_obj_enhaacplus_dec->aac_config.element_type[j] == element_type &&
380         (element_idx == ele_idx_order[j]) && (element_used[j] == 0)) {
381       *ch_idx = j;
382       element_used[j] = 1;
383       break;
384     }
385   }
386 
387   if (j == total_elements) {
388     if (it_bit_buff->cnt_bits < 0) {
389       return (WORD16)(
390           (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
391     }
392 
393     ixheaacd_read_bidirection(
394         it_bit_buff, (WORD16)(it_bit_buff->cnt_bits - it_bit_buff->size));
395     return IA_XHEAAC_DEC_EXE_NONFATAL_ELE_INSTANCE_TAG_NOT_FOUND;
396   } else
397     return 0;
398 }
399