/src/libxaac/encoder/ixheaace_enc_main.c
Line | Count | Source (jump to first uncovered line) |
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 <stdlib.h> |
23 | | #include "ixheaac_type_def.h" |
24 | | #include "ixheaac_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 "ixheaace_aac_constants.h" |
31 | | #include "ixheaac_basic_ops32.h" |
32 | | #include "ixheaac_basic_ops16.h" |
33 | | #include "ixheaac_basic_ops40.h" |
34 | | #include "ixheaac_basic_ops.h" |
35 | | |
36 | | #include "ixheaac_error_standards.h" |
37 | | #include "ixheaace_psy_const.h" |
38 | | #include "ixheaace_tns.h" |
39 | | #include "ixheaace_tns_params.h" |
40 | | #include "ixheaace_rom.h" |
41 | | #include "ixheaace_common_rom.h" |
42 | | |
43 | | #include "ixheaace_adjust_threshold_data.h" |
44 | | #include "ixheaace_bitbuffer.h" |
45 | | #include "ixheaace_dynamic_bits.h" |
46 | | #include "ixheaace_qc_data.h" |
47 | | #include "ixheaace_channel_map.h" |
48 | | #include "ixheaace_block_switch.h" |
49 | | #include "ixheaace_psy_data.h" |
50 | | #include "ixheaace_interface.h" |
51 | | #include "ixheaace_write_bitstream.h" |
52 | | #include "ixheaace_psy_configuration.h" |
53 | | #include "ixheaace_psy_mod.h" |
54 | | #include "ixheaace_stereo_preproc.h" |
55 | | #include "ixheaace_enc_main.h" |
56 | | #include "ixheaace_qc_util.h" |
57 | | #include "ixheaace_error_codes.h" |
58 | | #include "ixheaace_common_utils.h" |
59 | | |
60 | | #define ALIGNMENT_DEFINE __attribute__((aligned(8))) |
61 | | |
62 | | IA_ERRORCODE ia_enhaacplus_enc_aac_core_encode( |
63 | | iexheaac_encoder_str **pstr_aac_enc, FLOAT32 *ptr_time_signal, UWORD32 time_sn_stride, |
64 | | const UWORD8 *ptr_anc_bytes, UWORD8 *num_anc_bytes, UWORD8 *ptr_out_bytes, |
65 | | WORD32 *num_out_bytes, ixheaace_aac_tables *pstr_aac_tabs, VOID *ptr_bit_stream_handle, |
66 | | VOID *ptr_bit_stream, FLAG flag_last_element, WORD32 *write_program_config_element, |
67 | | WORD32 i_num_coup_channels, WORD32 i_channels_mask, WORD32 ele_idx, WORD32 *total_fill_bits, |
68 | | WORD32 total_channels, WORD32 aot, WORD32 adts_flag, WORD32 num_bs_elements, |
69 | 362k | WORD32 *is_quant_spec_zero, WORD32 *is_gain_limited) { |
70 | 362k | IA_ERRORCODE err_code = IA_NO_ERROR; |
71 | 362k | iexheaac_encoder_str *pstr_aac_encoder = pstr_aac_enc[ele_idx]; |
72 | 362k | ixheaace_element_info *pstr_element_info = &pstr_aac_encoder->element_info; |
73 | 362k | WORD32 glob_used_bits; |
74 | 362k | WORD32 anc_data_bytes, anc_data_bytes_left; |
75 | 362k | WORD32 stat_bits_flag = 0; |
76 | 362k | WORD32 frame_len_long = FRAME_LEN_1024; |
77 | 362k | WORD32 ch; |
78 | | |
79 | 362k | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
80 | 212k | frame_len_long = |
81 | 212k | (pstr_aac_encoder->config.flag_framelength_small == 1) ? FRAME_LEN_960 : FRAME_LEN_1024; |
82 | 212k | } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
83 | 150k | frame_len_long = |
84 | 150k | (pstr_aac_encoder->config.flag_framelength_small == 1) ? FRAME_LEN_480 : FRAME_LEN_512; |
85 | 150k | } |
86 | | |
87 | 362k | if (ele_idx == 0) { |
88 | 181k | if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) { |
89 | 97.0k | ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer( |
90 | 97.0k | ptr_bit_stream, (UWORD8 *)ptr_out_bytes, |
91 | 97.0k | (((pstr_aac_encoder->config.flag_framelength_small == 1) ? MAXIMUM_CHANNEL_BITS_960 |
92 | 97.0k | : MAXIMUM_CHANNEL_BITS_1024) / |
93 | 97.0k | 8) * |
94 | 97.0k | total_channels); |
95 | 97.0k | } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) { |
96 | 84.8k | if (pstr_aac_encoder->config.bitreservoir_size != -1) { |
97 | 82.8k | WORD32 avg_byte_perframe = (pstr_aac_encoder->config.bit_rate * frame_len_long / |
98 | 82.8k | (pstr_aac_encoder->config.core_sample_rate * 8)); |
99 | | |
100 | 82.8k | if ((pstr_aac_encoder->config.bitreservoir_size * total_channels) > avg_byte_perframe) { |
101 | 69.0k | ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer( |
102 | 69.0k | ptr_bit_stream, (UWORD8 *)ptr_out_bytes, |
103 | 69.0k | pstr_aac_encoder->config.bitreservoir_size * total_channels); |
104 | 69.0k | } else { |
105 | 13.7k | ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer( |
106 | 13.7k | ptr_bit_stream, (UWORD8 *)ptr_out_bytes, |
107 | 13.7k | (pstr_aac_encoder->config.bit_rate * frame_len_long / |
108 | 13.7k | (pstr_aac_encoder->config.core_sample_rate * 8))); |
109 | 13.7k | } |
110 | 82.8k | } else { |
111 | 1.94k | ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer( |
112 | 1.94k | ptr_bit_stream, (UWORD8 *)ptr_out_bytes, |
113 | 1.94k | (pstr_aac_encoder->config.bit_rate * frame_len_long / |
114 | 1.94k | (pstr_aac_encoder->config.core_sample_rate * 8))); |
115 | 1.94k | } |
116 | 84.8k | } |
117 | 181k | } else { |
118 | 180k | ptr_bit_stream_handle = ptr_bit_stream; |
119 | 180k | } |
120 | | |
121 | 362k | if (adts_flag) { |
122 | 110k | stat_bits_flag = 0; /* fix for low bit-rate */ |
123 | 110k | if ((pstr_aac_encoder->config.core_sample_rate * 3) <= (pstr_aac_encoder->config.bit_rate)) { |
124 | 35.3k | stat_bits_flag = 7; /* fix for low bit-rate */ |
125 | 35.3k | } |
126 | 110k | } |
127 | | |
128 | 362k | anc_data_bytes = anc_data_bytes_left = *num_anc_bytes; |
129 | | |
130 | 362k | if (pstr_element_info->el_type == ID_CPE) { |
131 | 166k | if (!pstr_aac_encoder->config.num_stereo_preprocessing) { |
132 | 166k | iaace_apply_stereo_preproc(&pstr_aac_encoder->str_stereo_pre_pro, time_sn_stride, |
133 | 166k | pstr_element_info, ptr_time_signal, frame_len_long); |
134 | 166k | } |
135 | 166k | } |
136 | | |
137 | 362k | err_code = ia_enhaacplus_enc_psy_main( |
138 | 362k | time_sn_stride, pstr_element_info, ptr_time_signal, aot, |
139 | 362k | pstr_aac_encoder->psy_kernel.psy_data, |
140 | 362k | pstr_aac_encoder->psy_kernel.temporal_noise_shaping_data, |
141 | 362k | &pstr_aac_encoder->psy_kernel.psy_conf_long, &pstr_aac_encoder->psy_kernel.psy_conf_short, |
142 | 362k | pstr_aac_encoder->psy_out.psy_out_ch, |
143 | 362k | &pstr_aac_encoder->psy_out.psy_out_element, |
144 | 362k | pstr_aac_encoder->psy_kernel.p_scratch_tns_float, |
145 | 362k | (FLOAT32 *)pstr_aac_encoder->pstr_aac_scratch->shared_buffer1, |
146 | 362k | pstr_aac_encoder->pstr_aac_scratch->shared_buffer5, pstr_aac_tabs, frame_len_long); |
147 | | |
148 | 362k | if (err_code != IA_NO_ERROR) { |
149 | 0 | return err_code; |
150 | 0 | } |
151 | | |
152 | 362k | ia_enhaacplus_enc_adjust_bitrate( |
153 | 362k | &pstr_aac_encoder->qc_kernel, pstr_aac_encoder->config.bit_rate, |
154 | 362k | pstr_aac_encoder->config.core_sample_rate, flag_last_element, frame_len_long); |
155 | | |
156 | 890k | for (ch = 0; ch < pstr_element_info->n_channels_in_el; ch++) { |
157 | 528k | pstr_aac_encoder->psy_out.psy_out_ch[ch]->ms_digest = |
158 | 528k | pstr_aac_encoder->psy_out.psy_out_element.tools_info.ms_digest; |
159 | | |
160 | 528k | memcpy( |
161 | 528k | &pstr_aac_encoder->psy_out.psy_out_ch[ch]->ms_used[0], |
162 | 528k | &pstr_aac_encoder->psy_out.psy_out_element.tools_info.ms_mask[0], |
163 | 528k | MAXIMUM_GROUPED_SCALE_FACTOR_BAND * |
164 | 528k | sizeof(pstr_aac_encoder->psy_out.psy_out_ch[ch]->ms_used[0])); |
165 | 528k | } |
166 | | |
167 | 362k | err_code = ia_enhaacplus_enc_qc_main( |
168 | 362k | &pstr_aac_encoder->qc_kernel, pstr_element_info->n_channels_in_el, |
169 | 362k | &pstr_aac_encoder->qc_kernel.element_bits, |
170 | 362k | pstr_aac_encoder->psy_out.psy_out_ch, |
171 | 362k | &pstr_aac_encoder->psy_out.psy_out_element, |
172 | 362k | pstr_aac_encoder->qc_out.qc_channel, |
173 | 362k | &pstr_aac_encoder->qc_out.qc_element, MIN(anc_data_bytes_left, anc_data_bytes), |
174 | 362k | pstr_aac_tabs, adts_flag, aot, stat_bits_flag, flag_last_element, frame_len_long, |
175 | 362k | pstr_aac_encoder->pstr_aac_scratch->shared_buffer5, is_quant_spec_zero, |
176 | 362k | is_gain_limited); |
177 | | |
178 | 362k | if (err_code != IA_NO_ERROR) { |
179 | 285 | return err_code; |
180 | 285 | } |
181 | | |
182 | 362k | if (pstr_element_info->el_type == ID_CPE) { |
183 | 166k | if (!pstr_aac_encoder->config.num_stereo_preprocessing) { |
184 | 166k | iaace_update_stereo_pre_process( |
185 | 166k | pstr_aac_encoder->psy_out.psy_out_ch, |
186 | 166k | &pstr_aac_encoder->qc_out.qc_element, &pstr_aac_encoder->str_stereo_pre_pro, |
187 | 166k | pstr_aac_encoder->psy_out.psy_out_element.weight_ms_lr_pe_ratio); |
188 | 166k | } |
189 | 166k | } |
190 | | |
191 | | /* Update bit reservoir levels */ |
192 | 362k | ia_enhaacplus_enc_update_bit_reservoir(&pstr_aac_encoder->qc_kernel, &pstr_aac_encoder->qc_out); |
193 | | |
194 | 362k | err_code = ia_enhaacplus_enc_finalize_bit_consumption( |
195 | 362k | &pstr_aac_encoder->qc_kernel, &pstr_aac_encoder->qc_out, flag_last_element, |
196 | 362k | ((ixheaace_bit_buf_handle)ptr_bit_stream_handle)->cnt_bits, total_fill_bits, pstr_aac_enc, |
197 | 362k | num_bs_elements, aot); |
198 | | |
199 | 362k | if (err_code != IA_NO_ERROR) { |
200 | 0 | return err_code; |
201 | 0 | } |
202 | | |
203 | 362k | err_code = ia_enhaacplus_enc_write_bitstream( |
204 | 362k | ptr_bit_stream_handle, *pstr_element_info, &pstr_aac_encoder->qc_out, |
205 | 362k | &pstr_aac_encoder->psy_out, &glob_used_bits, ptr_anc_bytes, pstr_aac_tabs, |
206 | 362k | flag_last_element, write_program_config_element, i_num_coup_channels, i_channels_mask, |
207 | 362k | pstr_aac_encoder->config.core_sample_rate, ele_idx, aot, total_fill_bits); |
208 | 362k | if (err_code != IA_NO_ERROR) { |
209 | 222 | return err_code; |
210 | 222 | } |
211 | | /* write out the bitstream */ |
212 | 361k | *num_out_bytes = ia_enhaacplus_enc_get_bits_available(ptr_bit_stream_handle) / 8; |
213 | | |
214 | | /* Validate that this frame is not too large */ |
215 | 361k | if (pstr_aac_encoder->config.bitreservoir_size != -1) { |
216 | 356k | WORD32 avg_bytes_perframe = (pstr_aac_encoder->config.bit_rate * frame_len_long / |
217 | 356k | (pstr_aac_encoder->config.core_sample_rate * 8)); |
218 | | |
219 | 356k | if (pstr_aac_encoder->config.bitreservoir_size * total_channels > avg_bytes_perframe) { |
220 | 334k | if (*num_out_bytes > (pstr_aac_encoder->config.bitreservoir_size * total_channels)) { |
221 | 627 | err_code = IA_EXHEAACE_EXE_FATAL_INVALID_OUT_BYTES; |
222 | 627 | } |
223 | 334k | } else { |
224 | 21.5k | if (*num_out_bytes > avg_bytes_perframe) { |
225 | 1.04k | err_code = IA_EXHEAACE_EXE_FATAL_INVALID_OUT_BYTES; |
226 | 1.04k | } |
227 | 21.5k | } |
228 | 356k | } else { |
229 | 5.58k | WORD32 avg_bytes_perframe = (pstr_aac_encoder->config.bit_rate * frame_len_long / |
230 | 5.58k | (pstr_aac_encoder->config.core_sample_rate * 8)); |
231 | 5.58k | if (*num_out_bytes > avg_bytes_perframe) { |
232 | 1.09k | err_code = IA_EXHEAACE_EXE_FATAL_INVALID_OUT_BYTES; |
233 | 1.09k | } |
234 | 5.58k | } |
235 | | |
236 | 361k | return err_code; |
237 | 362k | } |