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
ia_enhaacplus_enc_aac_core_encode(iexheaac_encoder_str ** pstr_aac_enc,FLOAT32 * ptr_time_signal,UWORD32 time_sn_stride,const UWORD8 * ptr_anc_bytes,UWORD8 * num_anc_bytes,UWORD8 * ptr_out_bytes,WORD32 * num_out_bytes,ixheaace_aac_tables * pstr_aac_tabs,VOID * ptr_bit_stream_handle,VOID * ptr_bit_stream,FLAG flag_last_element,WORD32 * write_program_config_element,WORD32 i_num_coup_channels,WORD32 i_channels_mask,WORD32 ele_idx,WORD32 * total_fill_bits,WORD32 total_channels,WORD32 aot,WORD32 adts_flag,WORD32 num_bs_elements,WORD32 * is_quant_spec_zero,WORD32 * is_gain_limited)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 WORD32 *is_quant_spec_zero, WORD32 *is_gain_limited) {
70 IA_ERRORCODE err_code = IA_NO_ERROR;
71 iexheaac_encoder_str *pstr_aac_encoder = pstr_aac_enc[ele_idx];
72 ixheaace_element_info *pstr_element_info = &pstr_aac_encoder->element_info;
73 WORD32 glob_used_bits;
74 WORD32 anc_data_bytes, anc_data_bytes_left;
75 WORD32 stat_bits_flag = 0;
76 WORD32 frame_len_long = FRAME_LEN_1024;
77 WORD32 ch;
78
79 if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
80 frame_len_long =
81 (pstr_aac_encoder->config.flag_framelength_small == 1) ? FRAME_LEN_960 : FRAME_LEN_1024;
82 } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
83 frame_len_long =
84 (pstr_aac_encoder->config.flag_framelength_small == 1) ? FRAME_LEN_480 : FRAME_LEN_512;
85 }
86
87 if (ele_idx == 0) {
88 if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
89 ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer(
90 ptr_bit_stream, (UWORD8 *)ptr_out_bytes,
91 (((pstr_aac_encoder->config.flag_framelength_small == 1) ? MAXIMUM_CHANNEL_BITS_960
92 : MAXIMUM_CHANNEL_BITS_1024) /
93 8) *
94 total_channels);
95 } else if (aot == AOT_AAC_LD || aot == AOT_AAC_ELD) {
96 if (pstr_aac_encoder->config.bitreservoir_size != -1) {
97 WORD32 avg_byte_perframe = (pstr_aac_encoder->config.bit_rate * frame_len_long /
98 (pstr_aac_encoder->config.core_sample_rate * 8));
99
100 if ((pstr_aac_encoder->config.bitreservoir_size * total_channels) > avg_byte_perframe) {
101 ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer(
102 ptr_bit_stream, (UWORD8 *)ptr_out_bytes,
103 pstr_aac_encoder->config.bitreservoir_size * total_channels);
104 } else {
105 ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer(
106 ptr_bit_stream, (UWORD8 *)ptr_out_bytes,
107 (pstr_aac_encoder->config.bit_rate * frame_len_long /
108 (pstr_aac_encoder->config.core_sample_rate * 8)));
109 }
110 } else {
111 ptr_bit_stream_handle = ia_enhaacplus_enc_create_bitbuffer(
112 ptr_bit_stream, (UWORD8 *)ptr_out_bytes,
113 (pstr_aac_encoder->config.bit_rate * frame_len_long /
114 (pstr_aac_encoder->config.core_sample_rate * 8)));
115 }
116 }
117 } else {
118 ptr_bit_stream_handle = ptr_bit_stream;
119 }
120
121 if (adts_flag) {
122 stat_bits_flag = 0; /* fix for low bit-rate */
123 if ((pstr_aac_encoder->config.core_sample_rate * 3) <= (pstr_aac_encoder->config.bit_rate)) {
124 stat_bits_flag = 7; /* fix for low bit-rate */
125 }
126 }
127
128 anc_data_bytes = anc_data_bytes_left = *num_anc_bytes;
129
130 if (pstr_element_info->el_type == ID_CPE) {
131 if (!pstr_aac_encoder->config.num_stereo_preprocessing) {
132 iaace_apply_stereo_preproc(&pstr_aac_encoder->str_stereo_pre_pro, time_sn_stride,
133 pstr_element_info, ptr_time_signal, frame_len_long);
134 }
135 }
136
137 err_code = ia_enhaacplus_enc_psy_main(
138 time_sn_stride, pstr_element_info, ptr_time_signal, aot,
139 pstr_aac_encoder->psy_kernel.psy_data,
140 pstr_aac_encoder->psy_kernel.temporal_noise_shaping_data,
141 &pstr_aac_encoder->psy_kernel.psy_conf_long, &pstr_aac_encoder->psy_kernel.psy_conf_short,
142 pstr_aac_encoder->psy_out.psy_out_ch,
143 &pstr_aac_encoder->psy_out.psy_out_element,
144 pstr_aac_encoder->psy_kernel.p_scratch_tns_float,
145 (FLOAT32 *)pstr_aac_encoder->pstr_aac_scratch->shared_buffer1,
146 pstr_aac_encoder->pstr_aac_scratch->shared_buffer5, pstr_aac_tabs, frame_len_long);
147
148 if (err_code != IA_NO_ERROR) {
149 return err_code;
150 }
151
152 ia_enhaacplus_enc_adjust_bitrate(
153 &pstr_aac_encoder->qc_kernel, pstr_aac_encoder->config.bit_rate,
154 pstr_aac_encoder->config.core_sample_rate, flag_last_element, frame_len_long);
155
156 for (ch = 0; ch < pstr_element_info->n_channels_in_el; ch++) {
157 pstr_aac_encoder->psy_out.psy_out_ch[ch]->ms_digest =
158 pstr_aac_encoder->psy_out.psy_out_element.tools_info.ms_digest;
159
160 memcpy(
161 &pstr_aac_encoder->psy_out.psy_out_ch[ch]->ms_used[0],
162 &pstr_aac_encoder->psy_out.psy_out_element.tools_info.ms_mask[0],
163 MAXIMUM_GROUPED_SCALE_FACTOR_BAND *
164 sizeof(pstr_aac_encoder->psy_out.psy_out_ch[ch]->ms_used[0]));
165 }
166
167 err_code = ia_enhaacplus_enc_qc_main(
168 &pstr_aac_encoder->qc_kernel, pstr_element_info->n_channels_in_el,
169 &pstr_aac_encoder->qc_kernel.element_bits,
170 pstr_aac_encoder->psy_out.psy_out_ch,
171 &pstr_aac_encoder->psy_out.psy_out_element,
172 pstr_aac_encoder->qc_out.qc_channel,
173 &pstr_aac_encoder->qc_out.qc_element, MIN(anc_data_bytes_left, anc_data_bytes),
174 pstr_aac_tabs, adts_flag, aot, stat_bits_flag, flag_last_element, frame_len_long,
175 pstr_aac_encoder->pstr_aac_scratch->shared_buffer5, is_quant_spec_zero,
176 is_gain_limited);
177
178 if (err_code != IA_NO_ERROR) {
179 return err_code;
180 }
181
182 if (pstr_element_info->el_type == ID_CPE) {
183 if (!pstr_aac_encoder->config.num_stereo_preprocessing) {
184 iaace_update_stereo_pre_process(
185 pstr_aac_encoder->psy_out.psy_out_ch,
186 &pstr_aac_encoder->qc_out.qc_element, &pstr_aac_encoder->str_stereo_pre_pro,
187 pstr_aac_encoder->psy_out.psy_out_element.weight_ms_lr_pe_ratio);
188 }
189 }
190
191 /* Update bit reservoir levels */
192 ia_enhaacplus_enc_update_bit_reservoir(&pstr_aac_encoder->qc_kernel, &pstr_aac_encoder->qc_out);
193
194 err_code = ia_enhaacplus_enc_finalize_bit_consumption(
195 &pstr_aac_encoder->qc_kernel, &pstr_aac_encoder->qc_out, flag_last_element,
196 ((ixheaace_bit_buf_handle)ptr_bit_stream_handle)->cnt_bits, total_fill_bits, pstr_aac_enc,
197 num_bs_elements, aot);
198
199 if (err_code != IA_NO_ERROR) {
200 return err_code;
201 }
202
203 err_code = ia_enhaacplus_enc_write_bitstream(
204 ptr_bit_stream_handle, *pstr_element_info, &pstr_aac_encoder->qc_out,
205 &pstr_aac_encoder->psy_out, &glob_used_bits, ptr_anc_bytes, pstr_aac_tabs,
206 flag_last_element, write_program_config_element, i_num_coup_channels, i_channels_mask,
207 pstr_aac_encoder->config.core_sample_rate, ele_idx, aot, total_fill_bits);
208 if (err_code != IA_NO_ERROR) {
209 return err_code;
210 }
211 /* write out the bitstream */
212 *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 if (pstr_aac_encoder->config.bitreservoir_size != -1) {
216 WORD32 avg_bytes_perframe = (pstr_aac_encoder->config.bit_rate * frame_len_long /
217 (pstr_aac_encoder->config.core_sample_rate * 8));
218
219 if (pstr_aac_encoder->config.bitreservoir_size * total_channels > avg_bytes_perframe) {
220 if (*num_out_bytes > (pstr_aac_encoder->config.bitreservoir_size * total_channels)) {
221 err_code = IA_EXHEAACE_EXE_FATAL_INVALID_OUT_BYTES;
222 }
223 } else {
224 if (*num_out_bytes > avg_bytes_perframe) {
225 err_code = IA_EXHEAACE_EXE_FATAL_INVALID_OUT_BYTES;
226 }
227 }
228 } else {
229 WORD32 avg_bytes_perframe = (pstr_aac_encoder->config.bit_rate * frame_len_long /
230 (pstr_aac_encoder->config.core_sample_rate * 8));
231 if (*num_out_bytes > avg_bytes_perframe) {
232 err_code = IA_EXHEAACE_EXE_FATAL_INVALID_OUT_BYTES;
233 }
234 }
235
236 return err_code;
237 }
238