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_basic_ops.h"
28 #include "ixheaacd_bitbuffer.h"
29 
30 #include "ixheaacd_basic_op.h"
31 #include "ixheaacd_intrinsics.h"
32 
33 #include "ixheaacd_defines.h"
34 
35 #include "ixheaacd_aac_rom.h"
36 
37 #include "ixheaacd_definitions.h"
38 
39 #include "ixheaacd_error_codes.h"
40 
41 #include "ixheaacd_pulsedata.h"
42 
43 #include "ixheaacd_pns.h"
44 #include "ixheaacd_drc_data_struct.h"
45 
46 #include "ixheaacd_lt_predict.h"
47 
48 #include "ixheaacd_channelinfo.h"
49 #include "ixheaacd_drc_dec.h"
50 #include "ixheaacd_sbrdecoder.h"
51 #include "ixheaacd_block.h"
52 #include "ixheaacd_channel.h"
53 
54 #include "ixheaacd_sbr_payload.h"
55 #include "ixheaacd_common_rom.h"
56 #include "ixheaacd_sbrdecsettings.h"
57 #include "ixheaacd_sbr_scale.h"
58 #include "ixheaacd_env_extr_part.h"
59 #include "ixheaacd_sbr_rom.h"
60 #include "ixheaacd_stereo.h"
61 #include "ixheaacd_lpp_tran.h"
62 #include "ixheaacd_hybrid.h"
63 #include "ixheaacd_ps_dec.h"
64 
65 #include "ixheaacd_env_extr.h"
66 #include "ixheaacd_adts.h"
67 #include "ixheaacd_audioobjtypes.h"
68 #include "ixheaacd_memory_standards.h"
69 
70 #include "ixheaacd_latmdemux.h"
71 
72 #include "ixheaacd_aacdec.h"
73 #include "ixheaacd_config.h"
74 #include "ixheaacd_mps_polyphase.h"
75 #include "ixheaacd_mps_dec.h"
76 #include "ixheaacd_struct_def.h"
77 
78 #include "ixheaacd_multichannel.h"
79 #include "ixheaacd_headerdecode.h"
80 #include "ixheaacd_error_standards.h"
81 
ixheaacd_latm_au_chunk_length_info(struct ia_bit_buf_struct * it_bit_buff)82 WORD32 ixheaacd_latm_au_chunk_length_info(
83     struct ia_bit_buf_struct *it_bit_buff) {
84   UWORD8 reading_done;
85   WORD32 len = 0;
86 
87   do {
88     UWORD32 tmp = ixheaacd_read_bits_buf(it_bit_buff, 8);
89     reading_done = (tmp < 255);
90 
91     len += tmp;
92 
93   } while (reading_done == 0);
94 
95   len <<= 3;
96 
97   return len;
98 }
99 
ixheaacd_latm_payload_length_info(struct ia_bit_buf_struct * it_bit_buff,ixheaacd_latm_struct * latm_element)100 WORD32 ixheaacd_latm_payload_length_info(struct ia_bit_buf_struct *it_bit_buff,
101                                          ixheaacd_latm_struct *latm_element) {
102   WORD32 error_code = AAC_DEC_OK;
103   UWORD32 prog, lay;
104 
105   if (latm_element->all_streams_same_time_framing == 1) {
106     for (prog = 0; prog < latm_element->num_program; prog++) {
107       for (lay = 0; lay < latm_element->num_layer; lay++) {
108         ixheaacd_latm_layer_info *layer_info =
109             &latm_element->layer_info[prog][lay];
110 
111         switch (layer_info->frame_len_type) {
112           case 0:
113             layer_info->frame_len_bits =
114                 ixheaacd_latm_au_chunk_length_info(it_bit_buff);
115             if (layer_info->frame_len_bits % 8 != 0) {
116               error_code = IA_ENHAACPLUS_DEC_EXE_FATAL_INVALID_LOAS_HEADER;
117               return error_code;
118             }
119 
120             latm_element->frame_length = layer_info->frame_len_bits >> 3;
121             latm_element->frame_length +=
122                 (it_bit_buff->size - it_bit_buff->cnt_bits) >> 3;
123             break;
124 
125           default:
126             error_code = IA_ENHAACPLUS_DEC_EXE_FATAL_INVALID_LOAS_HEADER;
127             return error_code;
128         }
129       }
130     }
131   } else {
132     error_code = IA_ENHAACPLUS_DEC_EXE_FATAL_INVALID_LOAS_HEADER;
133     return error_code;
134   }
135 
136   return (error_code);
137 }
138 
ixheaacd_latm_get_value(ia_bit_buf_struct * it_bit_buff)139 static UWORD32 ixheaacd_latm_get_value(ia_bit_buf_struct *it_bit_buff) {
140   UWORD32 bytes_read;
141 
142   bytes_read = ixheaacd_read_bits_buf(it_bit_buff, 2) + 1;
143 
144   if (bytes_read <= 3)
145     return ixheaacd_read_bits_buf(it_bit_buff, 8 * bytes_read);
146   else
147     return (ixheaacd_read_bits_buf(it_bit_buff, 24) << 8) +
148            ixheaacd_read_bits_buf(it_bit_buff, 8);
149 }
150 
ixheaacd_latm_stream_mux_config(struct ia_bit_buf_struct * it_bit_buff,ixheaacd_latm_struct * latm_element,ia_aac_dec_state_struct * aac_state_struct,ia_sampling_rate_info_struct * sample_rate_info)151 IA_ERRORCODE ixheaacd_latm_stream_mux_config(
152     struct ia_bit_buf_struct *it_bit_buff, ixheaacd_latm_struct *latm_element,
153     ia_aac_dec_state_struct *aac_state_struct,
154     ia_sampling_rate_info_struct *sample_rate_info) {
155   UWORD32 prog;
156   UWORD32 lay;
157   WORD32 bytes_consumed;
158   WORD32 audio_mux_version_a;
159   UWORD32 tara_buf_fullness;
160   IA_ERRORCODE error_code = AAC_DEC_OK;
161   ixheaacd_latm_layer_info *layer_info = 0;
162 
163   latm_element->audio_mux_version = ixheaacd_read_bits_buf(it_bit_buff, 1);
164 
165   if (latm_element->audio_mux_version == 1)
166     audio_mux_version_a = ixheaacd_read_bits_buf(it_bit_buff, 1);
167   else
168     audio_mux_version_a = 0;
169 
170   if (audio_mux_version_a == 0) {
171     if (latm_element->audio_mux_version == 1) {
172       tara_buf_fullness = ixheaacd_latm_get_value(it_bit_buff);
173     }
174     latm_element->all_streams_same_time_framing =
175         ixheaacd_read_bits_buf(it_bit_buff, 1);
176 
177     latm_element->num_sub_frames = ixheaacd_read_bits_buf(it_bit_buff, 6) + 1;
178 
179     if (latm_element->num_sub_frames != 1)
180       return IA_ENHAACPLUS_DEC_EXE_FATAL_INVALID_LOAS_HEADER;
181 
182     latm_element->num_program = ixheaacd_read_bits_buf(it_bit_buff, 4) + 1;
183 
184     if (latm_element->num_program > LATM_MAX_PROG) return IA_FATAL_ERROR;
185 
186     for (prog = 0; prog < latm_element->num_program; prog++) {
187       latm_element->num_layer = ixheaacd_read_bits_buf(it_bit_buff, 3) + 1;
188 
189       for (lay = 0; lay < latm_element->num_layer; lay++) {
190         layer_info = &latm_element->layer_info[prog][lay];
191         layer_info->frame_len_bits = 0;
192 
193         if ((prog == 0) && (lay == 0)) {
194           WORD32 asc_len, pos;
195 
196           latm_element->use_same_config = 0;
197 
198           asc_len = (latm_element->audio_mux_version == 1)
199                         ? ixheaacd_latm_get_value(it_bit_buff)
200                         : 0;
201           pos = it_bit_buff->size - it_bit_buff->cnt_bits;
202 
203           if (asc_len > it_bit_buff->size - 106 || asc_len > 2592 ||
204               asc_len < 0) {
205             return IA_ENHAACPLUS_DEC_INIT_FATAL_DEC_INIT_FAIL;
206           }
207 
208           if ((error_code = ixheaacd_ga_hdr_dec(
209                    aac_state_struct, it_bit_buff->cnt_bits, &bytes_consumed,
210                    sample_rate_info, it_bit_buff)))
211             return (error_code);
212 
213           if (asc_len) {
214             asc_len -= (it_bit_buff->size - it_bit_buff->cnt_bits) - pos;
215             ixheaacd_read_bidirection(it_bit_buff, asc_len);
216           }
217 
218           layer_info->asc.aot = aac_state_struct->audio_object_type;
219           layer_info->asc.channel_config = aac_state_struct->ch_config;
220           layer_info->asc.samples_per_frame = aac_state_struct->frame_len_flag;
221           layer_info->asc.sampling_freq = aac_state_struct->sampling_rate;
222           layer_info->asc.samples_per_frame = aac_state_struct->frame_length;
223         } else {
224           latm_element->use_same_config =
225               ixheaacd_read_bits_buf(it_bit_buff, 1);
226 
227           if (latm_element->use_same_config && (lay > 0)) {
228             layer_info->asc = latm_element->layer_info[prog][lay - 1].asc;
229           } else {
230             if ((error_code = ixheaacd_ga_hdr_dec(
231                      aac_state_struct, it_bit_buff->cnt_bits, &bytes_consumed,
232                      sample_rate_info, it_bit_buff)))
233               return (error_code);
234           }
235         }
236 
237         layer_info->frame_len_type = ixheaacd_read_bits_buf(it_bit_buff, 3);
238 
239         switch (layer_info->frame_len_type) {
240           case 0:
241             layer_info->buffer_fullness =
242                 ixheaacd_read_bits_buf(it_bit_buff, 8);
243 
244             if (!latm_element->all_streams_same_time_framing) {
245               if ((lay > 0) && layer_info->asc.aot == AOT_AAC_SCAL) {
246               }
247             }
248             break;
249 
250           default:
251             return IA_ENHAACPLUS_DEC_EXE_FATAL_INVALID_LOAS_HEADER;
252         }
253       }
254     }
255 
256     latm_element->other_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
257 
258     if (latm_element->other_data_present) {
259       if (latm_element->audio_mux_version == 1) {
260         latm_element->other_data_length = ixheaacd_latm_get_value(it_bit_buff);
261       } else {
262         UWORD32 other_data_len;
263         latm_element->other_data_length = 0;
264         do {
265           other_data_len = ixheaacd_read_bits_buf(it_bit_buff, 1);
266           latm_element->other_data_length <<= 8;
267           latm_element->other_data_length +=
268               ixheaacd_read_bits_buf(it_bit_buff, 8);
269           if (latm_element->other_data_length > (UWORD32)it_bit_buff->cnt_bits)
270             return IA_FATAL_ERROR;
271         } while (other_data_len);
272       }
273     }
274 
275     latm_element->crc_check_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
276 
277     if (latm_element->crc_check_present) {
278       latm_element->crc_check_sum = ixheaacd_read_bits_buf(it_bit_buff, 8);
279     }
280   } else {
281     error_code = IA_ENHAACPLUS_DEC_EXE_FATAL_INVALID_LOAS_HEADER;
282   }
283   return (error_code);
284 }
285 
ixheaacd_latm_audio_mux_element(struct ia_bit_buf_struct * it_bit_buff,ixheaacd_latm_struct * latm_element,ia_aac_dec_state_struct * aac_state_struct,ia_sampling_rate_info_struct * sample_rate_info)286 IA_ERRORCODE ixheaacd_latm_audio_mux_element(
287     struct ia_bit_buf_struct *it_bit_buff, ixheaacd_latm_struct *latm_element,
288     ia_aac_dec_state_struct *aac_state_struct,
289     ia_sampling_rate_info_struct *sample_rate_info) {
290   UWORD32 i;
291   IA_ERRORCODE error_code = AAC_DEC_OK;
292 
293   ixheaacd_read_bits_buf(it_bit_buff, 13);
294 
295   latm_element->use_same_stream_mux = ixheaacd_read_bits_buf(it_bit_buff, 1);
296 
297   if (!latm_element->use_same_stream_mux) {
298     if ((error_code = ixheaacd_latm_stream_mux_config(
299              it_bit_buff, latm_element, aac_state_struct, sample_rate_info))) {
300       return (error_code);
301     }
302   }
303 
304   for (i = 0; i < latm_element->num_sub_frames; i++) {
305     if ((error_code =
306              ixheaacd_latm_payload_length_info(it_bit_buff, latm_element))) {
307       if (error_code != 0) return (error_code);
308     }
309   }
310 
311   return (error_code);
312 }
313