/src/libxaac/encoder/ixheaace_enc_init.c
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * * |
3 | | * Copyright (C) 2023 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 | | |
21 | | #include <string.h> |
22 | | #include "ixheaac_type_def.h" |
23 | | #include "ixheaac_constants.h" |
24 | | #include "ixheaace_aac_constants.h" |
25 | | #include "impd_drc_common_enc.h" |
26 | | #include "impd_drc_uni_drc.h" |
27 | | #include "impd_drc_tables.h" |
28 | | #include "impd_drc_api.h" |
29 | | #include "ixheaace_api.h" |
30 | | #include "ixheaac_error_standards.h" |
31 | | #include "ixheaace_error_codes.h" |
32 | | #include "ixheaace_psy_const.h" |
33 | | #include "ixheaace_tns.h" |
34 | | #include "ixheaace_tns_params.h" |
35 | | #include "ixheaace_rom.h" |
36 | | #include "ixheaace_common_rom.h" |
37 | | #include "ixheaace_bitbuffer.h" |
38 | | #include "ixheaac_basic_ops32.h" |
39 | | #include "ixheaac_basic_ops40.h" |
40 | | #include "ixheaac_basic_ops.h" |
41 | | #include "ixheaace_adjust_threshold_data.h" |
42 | | |
43 | | #include "ixheaace_dynamic_bits.h" |
44 | | #include "ixheaace_qc_data.h" |
45 | | #include "ixheaace_channel_map.h" |
46 | | #include "ixheaace_block_switch.h" |
47 | | #include "ixheaace_psy_data.h" |
48 | | #include "ixheaace_interface.h" |
49 | | #include "ixheaace_write_bitstream.h" |
50 | | #include "ixheaace_psy_configuration.h" |
51 | | #include "ixheaace_psy_mod.h" |
52 | | #include "ixheaace_stereo_preproc.h" |
53 | | #include "ixheaace_enc_main.h" |
54 | | #include "ixheaace_qc_util.h" |
55 | | #include "ixheaace_config_params.h" |
56 | | #include "ixheaace_common_utils.h" |
57 | | #define ALIGNMENT_DEFINE __attribute__((aligned(8))) |
58 | | |
59 | | static WORD32 ixheaace_calculate_bandwidth(const WORD32 sample_rate, |
60 | | const WORD32 channel_bit_rate, const WORD32 num_ch, |
61 | 1.10k | WORD32 aot) { |
62 | 1.10k | WORD32 bandwidth = -1; |
63 | 1.10k | const ixheaace_bandwidth_table *pstr_bandwidth_table = NULL; |
64 | 1.10k | WORD32 bandwidth_table_size = 0; |
65 | 1.10k | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
66 | 700 | pstr_bandwidth_table = bandwidth_table_lc; |
67 | 700 | bandwidth_table_size = sizeof(bandwidth_table_lc) / sizeof(ixheaace_bandwidth_table); |
68 | 700 | } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
69 | 407 | switch (sample_rate) { |
70 | 21 | case 48000: |
71 | 66 | case 64000: |
72 | 101 | case 88200: |
73 | 123 | case 96000: |
74 | 123 | pstr_bandwidth_table = bandwidth_table_ld_48000; |
75 | 123 | bandwidth_table_size = |
76 | 123 | sizeof(bandwidth_table_ld_48000) / sizeof(ixheaace_bandwidth_table); |
77 | 123 | break; |
78 | 18 | case 44100: |
79 | 18 | pstr_bandwidth_table = bandwidth_table_ld_44100; |
80 | 18 | bandwidth_table_size = |
81 | 18 | sizeof(bandwidth_table_ld_44100) / sizeof(ixheaace_bandwidth_table); |
82 | 18 | break; |
83 | 22 | case 32000: |
84 | 22 | pstr_bandwidth_table = bandwidth_table_ld_32000; |
85 | 22 | bandwidth_table_size = |
86 | 22 | sizeof(bandwidth_table_ld_32000) / sizeof(ixheaace_bandwidth_table); |
87 | 22 | break; |
88 | 101 | case 24000: |
89 | 101 | pstr_bandwidth_table = bandwidth_table_ld_24000; |
90 | 101 | bandwidth_table_size = |
91 | 101 | sizeof(bandwidth_table_ld_24000) / sizeof(ixheaace_bandwidth_table); |
92 | 101 | break; |
93 | 47 | case 8000: |
94 | 74 | case 11025: |
95 | 87 | case 12000: |
96 | 128 | case 16000: |
97 | 143 | case 22050: |
98 | 143 | pstr_bandwidth_table = bandwidth_table_ld_22050; |
99 | 143 | bandwidth_table_size = |
100 | 143 | sizeof(bandwidth_table_ld_22050) / sizeof(ixheaace_bandwidth_table); |
101 | 143 | break; |
102 | 407 | } |
103 | 407 | } |
104 | 2.63k | for (WORD32 i = 0; i < bandwidth_table_size - 1; i++) { |
105 | 2.63k | if (channel_bit_rate >= 96000) { |
106 | 158 | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
107 | 80 | bandwidth = 20000; |
108 | 80 | } else { |
109 | 78 | if (num_ch == 1) { |
110 | 41 | bandwidth = 19000; |
111 | 41 | } else { |
112 | 37 | bandwidth = 22000; |
113 | 37 | } |
114 | 78 | } |
115 | 158 | break; |
116 | 2.48k | } else if (channel_bit_rate >= pstr_bandwidth_table[i].channel_bit_rate && |
117 | 2.48k | channel_bit_rate < pstr_bandwidth_table[i + 1].channel_bit_rate) { |
118 | 949 | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
119 | 620 | bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i].bandwidth_mono |
120 | 620 | : pstr_bandwidth_table[i].bandwidth_stereo; |
121 | 620 | bandwidth = bandwidth - (pstr_bandwidth_table[i].channel_bit_rate / 32); |
122 | 620 | break; |
123 | 620 | } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
124 | 329 | WORD32 start_bandwidth, end_bandwidth, start_bitrate, end_bitrate; |
125 | 329 | FLOAT32 bandwidth_fac; |
126 | 329 | start_bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i].bandwidth_mono |
127 | 329 | : pstr_bandwidth_table[i].bandwidth_stereo; |
128 | 329 | start_bandwidth = start_bandwidth - (pstr_bandwidth_table[i].channel_bit_rate / 32); |
129 | 329 | end_bandwidth = (num_ch == 1) ? pstr_bandwidth_table[i + 1].bandwidth_mono |
130 | 329 | : pstr_bandwidth_table[i + 1].bandwidth_stereo; |
131 | 329 | end_bandwidth = end_bandwidth - (pstr_bandwidth_table[i + 1].channel_bit_rate / 32); |
132 | 329 | start_bitrate = pstr_bandwidth_table[i].channel_bit_rate; |
133 | 329 | end_bitrate = pstr_bandwidth_table[i + 1].channel_bit_rate; |
134 | 329 | bandwidth_fac = |
135 | 329 | (FLOAT32)((channel_bit_rate - start_bitrate) / (end_bitrate - start_bitrate)); |
136 | 329 | bandwidth = (WORD32)(bandwidth_fac * (end_bandwidth - start_bandwidth) + start_bandwidth); |
137 | 329 | break; |
138 | 329 | } |
139 | 949 | } |
140 | 2.63k | } |
141 | 1.10k | return bandwidth; |
142 | 1.10k | } |
143 | | |
144 | | static VOID ixheaace_determine_bandwidth(const WORD32 proposed_bandwidth, const WORD32 bitrate, |
145 | | const WORD32 sample_rate, const WORD32 channels, |
146 | 2.93k | WORD32 *const bandwidth, WORD32 aot) { |
147 | 2.93k | WORD32 channel_bit_rate = bitrate / channels; |
148 | 2.93k | if (proposed_bandwidth == 0) { |
149 | 1.10k | *bandwidth = ixheaace_calculate_bandwidth(sample_rate, channel_bit_rate, channels, aot); |
150 | 1.82k | } else { |
151 | 1.82k | *bandwidth = MIN(proposed_bandwidth, MIN(20000, sample_rate >> 1)); |
152 | 1.82k | } |
153 | 2.93k | *bandwidth = MIN(*bandwidth, sample_rate / 2); |
154 | 2.93k | } |
155 | | |
156 | 10.0k | WORD32 ia_enhaacplus_enc_aac_enc_pers_size(WORD32 num_aac_chan, WORD32 aot) { |
157 | 10.0k | WORD32 num_bytes; |
158 | 10.0k | num_bytes = IXHEAAC_GET_SIZE_ALIGNED(sizeof(iexheaac_encoder_str), BYTE_ALIGN_8); |
159 | 10.0k | num_bytes += (num_aac_chan * |
160 | 10.0k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_out_channel), BYTE_ALIGN_8)); |
161 | 10.0k | num_bytes += (num_aac_chan * |
162 | 10.0k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_data), BYTE_ALIGN_8)); |
163 | 10.0k | num_bytes += (num_aac_chan * |
164 | 10.0k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_temporal_noise_shaping_data), BYTE_ALIGN_8)); |
165 | 10.0k | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
166 | 6.35k | num_bytes += (num_aac_chan * |
167 | 6.35k | IXHEAAC_GET_SIZE_ALIGNED(BLK_SWITCH_OFFSET_LC_128 * sizeof(FLOAT32), BYTE_ALIGN_8)); |
168 | 6.35k | } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
169 | 3.66k | num_bytes += (num_aac_chan * |
170 | 3.66k | IXHEAAC_GET_SIZE_ALIGNED(BLK_SWITCH_OFFSET_LD * sizeof(FLOAT32), BYTE_ALIGN_8)); |
171 | 3.66k | } |
172 | | |
173 | 10.0k | num_bytes += (num_aac_chan * |
174 | 10.0k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_qc_out_channel), BYTE_ALIGN_8)); |
175 | 10.0k | return num_bytes; |
176 | 10.0k | } |
177 | | |
178 | 330k | WORD32 ia_enhaacplus_enc_aac_enc_scr_size(VOID) { |
179 | 330k | return IXHEAAC_GET_SIZE_ALIGNED(sizeof(iaace_scratch), BYTE_ALIGN_8); |
180 | 330k | } |
181 | | |
182 | | VOID ia_enhaacplus_enc_set_shared_bufs(iaace_scratch *scr, WORD32 **shared_buf1, |
183 | | WORD32 **shared_buf2, WORD32 **shared_buf3, |
184 | 4.96k | WORD8 **shared_buf5) { |
185 | 4.96k | iaace_scratch *pstr_aac_enc_scratch = scr; |
186 | | /* Fill addresses of shared buffers */ |
187 | 4.96k | pstr_aac_enc_scratch->shared_buffer1 = *shared_buf1; |
188 | 4.96k | pstr_aac_enc_scratch->shared_buffer_2 = *shared_buf2; |
189 | 4.96k | pstr_aac_enc_scratch->shared_buffer3 = *shared_buf3; |
190 | 4.96k | pstr_aac_enc_scratch->shared_buffer5 = (WORD8 *)*shared_buf5; |
191 | 4.96k | } |
192 | | |
193 | 54.9k | VOID ia_enhaacplus_enc_aac_init_default_config(iaace_config *config, WORD32 aot) { |
194 | 54.9k | memset(config, 0, sizeof(iaace_config)); |
195 | | |
196 | | /* default configurations */ |
197 | 54.9k | config->bit_rate = AAC_BITRATE_DEFAULT_VALUE; |
198 | 54.9k | config->band_width = 0; |
199 | 54.9k | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
200 | 12.4k | config->inv_quant = 0; |
201 | 42.4k | } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
202 | 9.44k | config->inv_quant = 2; |
203 | 9.44k | } |
204 | 54.9k | config->bitreservoir_size = BITRESERVOIR_SIZE_CONFIG_PARAM_DEFAULT_VALUE; |
205 | 54.9k | config->use_tns = 0; |
206 | 54.9k | config->flag_framelength_small = |
207 | 54.9k | USE_FRAMELENGTH_SMALL_PARAM_DEFAULT_VALUE; // assume framelength large |
208 | 54.9k | } |
209 | | |
210 | | static VOID ia_enhaacplus_enc_aac_set_scratch_ptr(iexheaac_encoder_str *pstr_exheaac_encoder, |
211 | 4.96k | iaace_scratch *pstr_scr) { |
212 | 4.96k | pstr_exheaac_encoder->pstr_aac_scratch = pstr_scr; |
213 | 4.96k | } |
214 | | |
215 | 6.87k | VOID ia_enhaacplus_enc_init_aac_tabs(ixheaace_aac_tables *pstr_aac_tabs) { |
216 | 6.87k | pstr_aac_tabs->pstr_mdct_tab = (ixheaace_mdct_tables *)&ixheaace_enc_mdct_tab; |
217 | 6.87k | pstr_aac_tabs->pstr_huff_tab = (ixheaace_huffman_tables *)&ixheaace_enc_huff_tab; |
218 | 6.87k | pstr_aac_tabs->pstr_psycho_tab = (ixheaace_psycho_tables *)&ixheaace_enc_psycho_tab; |
219 | 6.87k | pstr_aac_tabs->pstr_quant_tab = (ixheaace_quant_tables *)&ixheaace_enc_quant_tab; |
220 | 6.87k | pstr_aac_tabs->pstr_tns_tab = |
221 | 6.87k | (ixheaace_temporal_noise_shaping_tables *)&ixheaace_enhaacplus_enc_tns_tab; |
222 | 6.87k | } |
223 | | |
224 | 4.96k | static VOID ia_enhaacplus_enc_aac_set_persist_buf(WORD8 *ptr_base, WORD32 num_chan, WORD32 aot) { |
225 | 4.96k | iexheaac_encoder_str *pstr_exheaac_encoder; |
226 | 4.96k | WORD8 *ptr_curr_mem = ptr_base + |
227 | 4.96k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(iexheaac_encoder_str), BYTE_ALIGN_8); |
228 | 4.96k | WORD32 i; |
229 | | |
230 | 4.96k | pstr_exheaac_encoder = (iexheaac_encoder_str *)ptr_base; |
231 | | |
232 | 12.2k | for (i = 0; i < num_chan; i++) { |
233 | 7.25k | pstr_exheaac_encoder->psy_out.psy_out_ch[i] = (ixheaace_psy_out_channel *)(ptr_curr_mem); |
234 | 7.25k | ptr_curr_mem = ptr_curr_mem + |
235 | 7.25k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_out_channel), BYTE_ALIGN_8); |
236 | 7.25k | } |
237 | | |
238 | 12.2k | for (i = 0; i < num_chan; i++) { |
239 | 7.25k | pstr_exheaac_encoder->psy_kernel.psy_data[i] = (ixheaace_psy_data *)(ptr_curr_mem); |
240 | 7.25k | ptr_curr_mem = ptr_curr_mem + |
241 | 7.25k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_psy_data), BYTE_ALIGN_8); |
242 | 7.25k | } |
243 | | |
244 | 12.2k | for (i = 0; i < num_chan; i++) { |
245 | 7.25k | pstr_exheaac_encoder->psy_kernel.temporal_noise_shaping_data[i] = |
246 | 7.25k | (ixheaace_temporal_noise_shaping_data *)(ptr_curr_mem); |
247 | 7.25k | ptr_curr_mem = ptr_curr_mem + |
248 | 7.25k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_temporal_noise_shaping_data), BYTE_ALIGN_8); |
249 | 7.25k | } |
250 | | |
251 | 12.2k | for (i = 0; i < num_chan; i++) { |
252 | 7.25k | switch (aot) { |
253 | 1.64k | case AOT_AAC_LC: |
254 | 4.35k | case AOT_SBR: |
255 | 4.60k | case AOT_PS: |
256 | 4.60k | pstr_exheaac_encoder->psy_kernel.psy_data[i]->ptr_mdct_delay_buf = |
257 | 4.60k | (FLOAT32 *)(ptr_curr_mem); |
258 | 4.60k | ptr_curr_mem = ptr_curr_mem + |
259 | 4.60k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * BLK_SWITCH_OFFSET_LC_128, BYTE_ALIGN_8); |
260 | 4.60k | break; |
261 | | |
262 | 762 | case AOT_AAC_LD: |
263 | 2.64k | case AOT_AAC_ELD: |
264 | 2.64k | pstr_exheaac_encoder->psy_kernel.psy_data[i]->ptr_mdct_delay_buf = |
265 | 2.64k | (FLOAT32 *)(ptr_curr_mem); |
266 | 2.64k | ptr_curr_mem = ptr_curr_mem + |
267 | 2.64k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(FLOAT32) * BLK_SWITCH_OFFSET_LD, BYTE_ALIGN_8); |
268 | 2.64k | break; |
269 | 7.25k | } |
270 | 7.25k | } |
271 | | |
272 | 12.2k | for (i = 0; i < num_chan; i++) { |
273 | 7.25k | pstr_exheaac_encoder->qc_out.qc_channel[i] = (ixheaace_qc_out_channel *)(ptr_curr_mem); |
274 | 7.25k | ptr_curr_mem = ptr_curr_mem + |
275 | 7.25k | IXHEAAC_GET_SIZE_ALIGNED(sizeof(ixheaace_qc_out_channel), BYTE_ALIGN_8); |
276 | 7.25k | } |
277 | 4.96k | } |
278 | | |
279 | | IA_ERRORCODE ia_enhaacplus_enc_aac_enc_open(iexheaac_encoder_str **ppstr_exheaac_encoder, |
280 | | const iaace_config config, |
281 | | iaace_scratch *pstr_aac_scratch, |
282 | | ixheaace_aac_tables *pstr_aac_tabs, WORD32 ele_type, |
283 | 4.96k | WORD32 element_instance_tag, WORD32 aot) { |
284 | 4.96k | IA_ERRORCODE error = IA_NO_ERROR; |
285 | 4.96k | WORD32 profile = 1; |
286 | 4.96k | ixheaace_element_info *pstr_element_info = NULL; |
287 | 4.96k | iexheaac_encoder_str *pstr_exheaac_encoder; |
288 | 4.96k | WORD32 frame_len_long = FRAME_LEN_1024; |
289 | 4.96k | switch (aot) { |
290 | 1.09k | case AOT_AAC_LC: |
291 | 2.90k | case AOT_SBR: |
292 | 3.15k | case AOT_PS: |
293 | 3.15k | if (config.flag_framelength_small) { |
294 | 1.08k | frame_len_long = FRAME_LEN_960; |
295 | 2.07k | } else { |
296 | 2.07k | frame_len_long = FRAME_LEN_1024; |
297 | 2.07k | } |
298 | 3.15k | break; |
299 | | |
300 | 526 | case AOT_AAC_LD: |
301 | 1.81k | case AOT_AAC_ELD: |
302 | 1.81k | if (config.flag_framelength_small) { |
303 | 833 | frame_len_long = FRAME_LEN_480; |
304 | 977 | } else { |
305 | 977 | frame_len_long = FRAME_LEN_512; |
306 | 977 | } |
307 | 1.81k | break; |
308 | 4.96k | } |
309 | | |
310 | 4.96k | if ((config.num_in_channels < 1) || (config.num_out_channels > IXHEAACE_MAX_CH_IN_BS_ELE) || |
311 | 4.96k | (config.num_out_channels < 1) || (config.num_in_channels < config.num_out_channels)) { |
312 | 0 | return IA_EXHEAACE_INIT_FATAL_INVALID_NUM_CHANNELS_IN_ELE; |
313 | 0 | } |
314 | 4.96k | if ((config.bit_rate != 0) && ((config.bit_rate / config.num_out_channels < 8000) || |
315 | 4.96k | (config.bit_rate / config.num_out_channels > 576000))) { |
316 | 0 | error = IA_EXHEAACE_INIT_FATAL_BITRATE_NOT_SUPPORTED; |
317 | 0 | } |
318 | 4.96k | if (error != IA_NO_ERROR) { |
319 | 0 | return error; |
320 | 0 | } |
321 | | |
322 | 4.96k | pstr_exheaac_encoder = *ppstr_exheaac_encoder; |
323 | | |
324 | 4.96k | memset(pstr_exheaac_encoder, 0, sizeof(iexheaac_encoder_str)); |
325 | | |
326 | 4.96k | ia_enhaacplus_enc_aac_set_scratch_ptr(pstr_exheaac_encoder, pstr_aac_scratch); |
327 | | |
328 | 4.96k | ia_enhaacplus_enc_aac_set_persist_buf((WORD8 *)pstr_exheaac_encoder, config.num_out_channels, |
329 | 4.96k | aot); |
330 | | |
331 | | /* check sample rate */ |
332 | | |
333 | 4.96k | switch (config.core_sample_rate) { |
334 | 182 | case 8000: |
335 | 289 | case 11025: |
336 | 473 | case 12000: |
337 | 1.77k | case 16000: |
338 | 2.83k | case 22050: |
339 | 4.10k | case 24000: |
340 | 4.20k | case 32000: |
341 | 4.28k | case 44100: |
342 | 4.42k | case 48000: |
343 | 4.52k | case 64000: |
344 | 4.83k | case 88200: |
345 | 4.96k | case 96000: |
346 | 4.96k | break; |
347 | | |
348 | 0 | default: |
349 | 0 | return IA_EXHEAACE_INIT_FATAL_INVALID_CORE_SAMPLE_RATE; |
350 | 0 | break; |
351 | 4.96k | } |
352 | | |
353 | 4.96k | pstr_exheaac_encoder->config = config; |
354 | | |
355 | 4.96k | error = ia_enhaacplus_enc_init_element_info(config.num_out_channels, |
356 | 4.96k | &pstr_exheaac_encoder->element_info, ele_type, |
357 | 4.96k | element_instance_tag); |
358 | 4.96k | if (error != IA_NO_ERROR) { |
359 | 0 | return error; |
360 | 0 | } |
361 | | |
362 | 4.96k | pstr_element_info = &pstr_exheaac_encoder->element_info; |
363 | | |
364 | | /* allocate the Psy aud Psy Out structure */ |
365 | | |
366 | 4.96k | error = (ia_enhaacplus_enc_psy_new( |
367 | 4.96k | &pstr_exheaac_encoder->psy_kernel, pstr_element_info->n_channels_in_el, |
368 | 4.96k | pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer_2, frame_len_long)); |
369 | | |
370 | 4.96k | if (error != IA_NO_ERROR) { |
371 | 0 | return error; |
372 | 0 | } |
373 | | |
374 | 4.96k | WORD32 tns_mask = config.use_tns; |
375 | 4.96k | if (config.full_bandwidth) { |
376 | 2.03k | pstr_exheaac_encoder->config.band_width = config.core_sample_rate >> 2; |
377 | 2.93k | } else { |
378 | 2.93k | ixheaace_determine_bandwidth(pstr_exheaac_encoder->config.band_width, config.bit_rate, |
379 | 2.93k | config.core_sample_rate, pstr_element_info->n_channels_in_el, |
380 | 2.93k | &pstr_exheaac_encoder->config.band_width, aot); |
381 | 2.93k | } |
382 | 4.96k | pstr_exheaac_encoder->bandwidth_90_dB = (WORD32)pstr_exheaac_encoder->config.band_width; |
383 | 4.96k | if (ele_type == ID_LFE) { |
384 | 438 | tns_mask = 0; |
385 | 438 | } |
386 | | |
387 | 4.96k | error = ia_enhaacplus_enc_psy_main_init( |
388 | 4.96k | &pstr_exheaac_encoder->psy_kernel, config.core_sample_rate, config.bit_rate, |
389 | 4.96k | pstr_element_info->n_channels_in_el, tns_mask, pstr_exheaac_encoder->bandwidth_90_dB, aot, |
390 | 4.96k | pstr_aac_tabs, frame_len_long); |
391 | 4.96k | if (error != IA_NO_ERROR) { |
392 | 0 | return error; |
393 | 0 | } |
394 | | |
395 | | /* allocate the Q&C Out structure */ |
396 | 4.96k | error = ia_enhaacplus_enc_qc_out_new( |
397 | 4.96k | &pstr_exheaac_encoder->qc_out, pstr_element_info->n_channels_in_el, |
398 | 4.96k | pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer1, |
399 | 4.96k | pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer3, frame_len_long); |
400 | | |
401 | 4.96k | if (error != IA_NO_ERROR) { |
402 | 0 | return error; |
403 | 0 | } |
404 | | |
405 | | /* allocate the Q&C kernel */ |
406 | 4.96k | error = ia_enhaacplus_enc_qc_new(&pstr_exheaac_encoder->qc_kernel, |
407 | 4.96k | pstr_exheaac_encoder->pstr_aac_scratch->shared_buffer_2, |
408 | 4.96k | frame_len_long); |
409 | 4.96k | if (error != IA_NO_ERROR) { |
410 | 0 | return error; |
411 | 0 | } |
412 | | |
413 | 4.96k | ixheaace_qc_init qc_init; |
414 | | |
415 | 4.96k | qc_init.pstr_element_info = &pstr_exheaac_encoder->element_info; |
416 | | |
417 | 4.96k | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
418 | 3.15k | if (config.flag_framelength_small) { |
419 | 1.08k | qc_init.max_bits = MAXIMUM_CHANNEL_BITS_960 * pstr_element_info->n_channels_in_el; |
420 | 2.07k | } else { |
421 | 2.07k | qc_init.max_bits = MAXIMUM_CHANNEL_BITS_1024 * pstr_element_info->n_channels_in_el; |
422 | 2.07k | } |
423 | | |
424 | 3.15k | qc_init.bit_res = qc_init.max_bits; |
425 | 3.15k | } |
426 | | |
427 | 4.96k | qc_init.average_bits = (config.bit_rate * frame_len_long) / config.core_sample_rate; |
428 | | |
429 | 4.96k | if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
430 | 1.81k | if (pstr_exheaac_encoder->config.bitreservoir_size != -1) { |
431 | 1.66k | qc_init.max_bits = (pstr_exheaac_encoder->config.bitreservoir_size * 8) * |
432 | 1.66k | pstr_element_info->n_channels_in_el; |
433 | 1.66k | if (qc_init.max_bits > qc_init.average_bits) { |
434 | 1.18k | qc_init.bit_res = (pstr_exheaac_encoder->config.bitreservoir_size * 8) * |
435 | 1.18k | pstr_element_info->n_channels_in_el; |
436 | 1.18k | } else { |
437 | 481 | qc_init.max_bits = qc_init.average_bits; |
438 | 481 | qc_init.bit_res = 0; |
439 | 481 | } |
440 | 1.66k | } else { |
441 | 143 | qc_init.max_bits = qc_init.average_bits; |
442 | 143 | qc_init.bit_res = 0; |
443 | 143 | } |
444 | 1.81k | } |
445 | | |
446 | 4.96k | qc_init.padding.padding_rest = config.core_sample_rate; |
447 | | |
448 | 4.96k | qc_init.mean_pe = ((FLOAT32)10 * frame_len_long * pstr_exheaac_encoder->bandwidth_90_dB * 2) / |
449 | 4.96k | config.core_sample_rate; |
450 | | |
451 | 4.96k | switch (aot) { |
452 | 1.09k | case AOT_AAC_LC: |
453 | 2.90k | case AOT_SBR: |
454 | 3.15k | case AOT_PS: |
455 | 3.15k | if (config.flag_framelength_small) { |
456 | 1.08k | qc_init.max_bit_fac = |
457 | 1.08k | (float)(MAXIMUM_CHANNEL_BITS_960 * pstr_element_info->n_channels_in_el) / |
458 | 1.08k | (float)(qc_init.average_bits ? qc_init.average_bits : 1); |
459 | 2.07k | } else { |
460 | 2.07k | qc_init.max_bit_fac = |
461 | 2.07k | (float)(MAXIMUM_CHANNEL_BITS_1024 * pstr_element_info->n_channels_in_el) / |
462 | 2.07k | (float)(qc_init.average_bits ? qc_init.average_bits : 1); |
463 | 2.07k | } |
464 | 3.15k | break; |
465 | | |
466 | 526 | case AOT_AAC_LD: |
467 | 1.81k | case AOT_AAC_ELD: |
468 | 1.81k | if (config.flag_framelength_small) { |
469 | 833 | qc_init.max_bit_fac = (FLOAT32)((MAXIMUM_CHANNEL_BITS_480)*pstr_element_info |
470 | 833 | ->n_channels_in_el); // no anc data in aacld |
471 | 977 | } else { |
472 | 977 | qc_init.max_bit_fac = (FLOAT32)((MAXIMUM_CHANNEL_BITS_512)*pstr_element_info |
473 | 977 | ->n_channels_in_el); // no anc data in aacld |
474 | 977 | } |
475 | 1.81k | qc_init.max_bit_fac = |
476 | 1.81k | qc_init.max_bit_fac / (qc_init.average_bits ? qc_init.average_bits : 1); |
477 | 1.81k | break; |
478 | 4.96k | } |
479 | | |
480 | 4.96k | qc_init.bitrate = config.bit_rate; |
481 | 4.96k | qc_init.inv_quant = config.inv_quant; |
482 | | |
483 | 4.96k | error = ia_enhaacplus_enc_qc_init(&pstr_exheaac_encoder->qc_kernel, aot, &qc_init, |
484 | 4.96k | config.flag_framelength_small); |
485 | 4.96k | if (error != IA_NO_ERROR) { |
486 | 0 | return error; |
487 | 0 | } |
488 | | |
489 | | /* init bitstream encoder */ |
490 | 4.96k | pstr_exheaac_encoder->bse_init.num_channels = pstr_element_info->n_channels_in_el; |
491 | 4.96k | pstr_exheaac_encoder->bse_init.bitrate = config.bit_rate; |
492 | 4.96k | pstr_exheaac_encoder->bse_init.sample_rate = config.core_sample_rate; |
493 | 4.96k | pstr_exheaac_encoder->bse_init.profile = profile; |
494 | | |
495 | 4.96k | if (config.num_in_channels > config.num_out_channels) { |
496 | 816 | pstr_exheaac_encoder->downmix = 1; |
497 | 816 | pstr_exheaac_encoder->downmix_fac = config.num_in_channels / config.num_out_channels; |
498 | 816 | } |
499 | | |
500 | 4.96k | if (pstr_element_info->el_type == ID_CPE && |
501 | 2.29k | (config.core_sample_rate <= 24000 && |
502 | 1.85k | (config.bit_rate / pstr_element_info->n_channels_in_el * 2) < 60000)) { |
503 | 1.28k | FLOAT32 scf_used_ratio = (FLOAT32)pstr_exheaac_encoder->psy_kernel.psy_conf_long.sfb_active / |
504 | 1.28k | pstr_exheaac_encoder->psy_kernel.psy_conf_long.sfb_cnt; |
505 | | |
506 | 1.28k | error = iaace_init_stereo_pre_processing(&(pstr_exheaac_encoder->str_stereo_pre_pro), |
507 | 1.28k | pstr_element_info->n_channels_in_el, config.bit_rate, |
508 | 1.28k | config.core_sample_rate, scf_used_ratio); |
509 | 1.28k | } |
510 | | |
511 | 4.96k | if (error != IA_NO_ERROR) { |
512 | 0 | return error; |
513 | 0 | } |
514 | | |
515 | 4.96k | *ppstr_exheaac_encoder = pstr_exheaac_encoder; |
516 | | |
517 | 4.96k | return IA_NO_ERROR; |
518 | 4.96k | } |