xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_sbr_env_est_init.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 <limits.h>
23 
24 #include "ixheaac_type_def.h"
25 #include "ixheaac_constants.h"
26 #include "ixheaac_error_standards.h"
27 #include "ixheaace_error_codes.h"
28 #include "ixheaace_aac_constants.h"
29 #include "ixheaac_basic_ops32.h"
30 #include "ixheaac_basic_ops16.h"
31 #include "ixheaac_basic_ops40.h"
32 #include "ixheaac_basic_ops.h"
33 #include "ixheaace_sbr_header.h"
34 #include "ixheaace_sbr_def.h"
35 #include "ixheaace_resampler.h"
36 #include "ixheaace_sbr_rom.h"
37 #include "ixheaace_common_rom.h"
38 #include "ixheaace_sbr_hbe.h"
39 #include "ixheaace_sbr_qmf_enc.h"
40 #include "ixheaace_sbr_tran_det.h"
41 #include "ixheaace_sbr_frame_info_gen.h"
42 #include "ixheaace_sbr_env_est.h"
43 #include "ixheaace_sbr_code_envelope.h"
44 #include "ixheaace_sbr_main.h"
45 #include "ixheaace_sbr_missing_harmonics_det.h"
46 #include "ixheaace_sbr_inv_filtering_estimation.h"
47 #include "ixheaace_sbr_noise_floor_est.h"
48 
49 #include "ixheaace_common_rom.h"
50 #include "ixheaace_sbr_ton_corr.h"
51 #include "iusace_esbr_pvc.h"
52 #include "iusace_esbr_inter_tes.h"
53 #include "ixheaace_sbr.h"
54 #include "ixheaace_bitbuffer.h"
55 #include "ixheaace_sbr_cmondata.h"
56 #include "ixheaace_sbr_write_bitstream.h"
57 #include "ixheaace_sbr_hybrid.h"
58 #include "ixheaace_sbr_ps_enc.h"
59 
60 IA_ERRORCODE
ixheaace_create_extract_sbr_envelope(WORD32 ch,ixheaace_pstr_sbr_extract_envelope pstr_sbr_ext_env,WORD32 start_index,WORD32 * ptr_common_buffer2,FLOAT32 * ptr_sbr_env_r_buf,FLOAT32 * ptr_sbr_env_i_buf,WORD32 is_ld_sbr,WORD32 frame_flag_480,ixheaace_sbr_codec_type sbr_codec)61 ixheaace_create_extract_sbr_envelope(WORD32 ch,
62                                      ixheaace_pstr_sbr_extract_envelope pstr_sbr_ext_env,
63                                      WORD32 start_index, WORD32 *ptr_common_buffer2,
64                                      FLOAT32 *ptr_sbr_env_r_buf, FLOAT32 *ptr_sbr_env_i_buf,
65                                      WORD32 is_ld_sbr, WORD32 frame_flag_480,
66                                      ixheaace_sbr_codec_type sbr_codec) {
67   WORD32 i;
68   WORD32 y_buffer_length, r_buffer_length;
69   WORD32 offset = 0;
70   WORD32 y_buffer_write_offset = 32;
71   WORD32 no_cols = 32;
72   WORD32 time_slots = 16;
73   WORD32 sbr_ratio_idx = pstr_sbr_ext_env->sbr_ratio_idx;
74   WORD32 qmf_time_slots = IXHEAACE_QMF_TIME_SLOTS;
75   FLOAT32 *ptr_buffer = NULL;
76   FLOAT32 *ptr_i_buffer = NULL;
77   memset(pstr_sbr_ext_env, 0, sizeof(ixheaace_str_sbr_extr_env));
78 
79   pstr_sbr_ext_env->pre_transient_info[0] = 0;
80   pstr_sbr_ext_env->pre_transient_info[1] = 0;
81 
82   if (sbr_codec == ELD_SBR) {
83     if (frame_flag_480) {
84       no_cols = 30;
85       time_slots = 15;
86     }
87 
88     y_buffer_write_offset = time_slots / 2;
89     pstr_sbr_ext_env->y_buffer_write_offset = y_buffer_write_offset;
90     pstr_sbr_ext_env->no_cols = no_cols;
91     pstr_sbr_ext_env->no_rows = 64;
92     pstr_sbr_ext_env->start_index = start_index;
93     pstr_sbr_ext_env->time_slots = time_slots;
94     pstr_sbr_ext_env->time_step = 1;
95 
96     y_buffer_length = y_buffer_write_offset + time_slots;
97     r_buffer_length = time_slots;
98   } else {
99     if ((sbr_codec == USAC_SBR) && (USAC_SBR_RATIO_INDEX_4_1 == sbr_ratio_idx)) {
100       qmf_time_slots = QMF_TIME_SLOTS_USAC_4_1;
101       y_buffer_write_offset = QMF_TIME_SLOTS_USAC_4_1;
102     }
103     if (is_ld_sbr && frame_flag_480) {
104       y_buffer_write_offset = 30;
105       no_cols = 30;
106       time_slots = 15;
107     }
108     pstr_sbr_ext_env->y_buffer_write_offset = y_buffer_write_offset;
109 
110     y_buffer_length = pstr_sbr_ext_env->y_buffer_write_offset + y_buffer_write_offset;
111 
112     r_buffer_length = y_buffer_write_offset;
113 
114     pstr_sbr_ext_env->pre_transient_info[0] = 0;
115     pstr_sbr_ext_env->pre_transient_info[1] = 0;
116 
117     pstr_sbr_ext_env->no_cols = no_cols;
118     pstr_sbr_ext_env->no_rows = 64;
119     pstr_sbr_ext_env->start_index = start_index;
120 
121     pstr_sbr_ext_env->time_slots = time_slots;
122     pstr_sbr_ext_env->time_step = no_cols / time_slots;
123 
124     if ((r_buffer_length != qmf_time_slots) || (y_buffer_length != 2 * qmf_time_slots)) {
125       return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_BUFFER_LENGTH;
126     }
127 
128     y_buffer_length /= 2;
129 
130     if ((sbr_codec == USAC_SBR) && (USAC_SBR_RATIO_INDEX_4_1 == sbr_ratio_idx)) {
131       pstr_sbr_ext_env->y_buffer_write_offset /= 4;
132     } else {
133       pstr_sbr_ext_env->y_buffer_write_offset /= 2;
134     }
135   }
136 
137   pstr_sbr_ext_env->buffer_flag = 0;
138 
139   ptr_buffer =
140       (FLOAT32 *)&ptr_common_buffer2[ch * y_buffer_length * 64 + offset * IXHEAACE_QMF_CHANNELS];
141 
142   i = 0;
143   while (i < pstr_sbr_ext_env->y_buffer_write_offset) {
144     pstr_sbr_ext_env->ptr_y_buffer[i] = ptr_buffer + i * IXHEAACE_QMF_CHANNELS;
145     memset(pstr_sbr_ext_env->ptr_y_buffer[i], 0,
146            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_y_buffer[0]));
147     i++;
148   }
149 
150   ptr_buffer =
151       (FLOAT32 *)&ptr_common_buffer2[ch * y_buffer_length * 64 - offset * IXHEAACE_QMF_CHANNELS];
152   for (i = pstr_sbr_ext_env->y_buffer_write_offset; i < y_buffer_length; i++) {
153     pstr_sbr_ext_env->ptr_y_buffer[i] = ptr_buffer + i * IXHEAACE_QMF_CHANNELS;
154     memset(pstr_sbr_ext_env->ptr_y_buffer[i], 0,
155            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_y_buffer[0]));
156   }
157 
158   ptr_buffer = &ptr_sbr_env_r_buf[ch * qmf_time_slots * IXHEAACE_QMF_CHANNELS];
159   ptr_i_buffer = &ptr_sbr_env_i_buf[ch * qmf_time_slots * IXHEAACE_QMF_CHANNELS];
160   for (i = 0; i < r_buffer_length; i++) {
161     pstr_sbr_ext_env->ptr_r_buffer[i] = ptr_buffer + i * IXHEAACE_QMF_CHANNELS;
162     memset(pstr_sbr_ext_env->ptr_r_buffer[i], 0,
163            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_r_buffer[0]));
164     pstr_sbr_ext_env->ptr_i_buffer[i] = ptr_i_buffer + i * IXHEAACE_QMF_CHANNELS;
165     memset(pstr_sbr_ext_env->ptr_i_buffer[i], 0,
166            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_i_buffer[0]));
167   }
168 
169   memset(pstr_sbr_ext_env->envelope_compensation, 0,
170          sizeof(pstr_sbr_ext_env->envelope_compensation[0]) * MAXIMUM_FREQ_COEFFS);
171 
172   return IA_NO_ERROR;
173 }
174