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 "ixheaac_type_def.h"
22 #include "ixheaace_error_codes.h"
23 #include "ixheaac_error_standards.h"
24 #include "ixheaace_mps_common_fix.h"
25 #include "ixheaace_mps_defines.h"
26 #include "ixheaace_mps_common_define.h"
27 #include "ixheaace_bitbuffer.h"
28
29 #include "ixheaace_psy_const.h"
30 #include "ixheaace_tns.h"
31 #include "ixheaace_tns_params.h"
32 #include "ixheaace_rom.h"
33 #include "ixheaace_common_rom.h"
34 #include "ixheaace_fft.h"
35
36 #include "ixheaace_mps_dct.h"
37 #include "ixheaace_mps_buf.h"
38 #include "ixheaace_mps_lib.h"
39 #include "ixheaace_mps_main_structure.h"
40
41 #include "ixheaace_mps_bitstream.h"
42 #include "ixheaace_mps_frame_windowing.h"
43 #include "ixheaace_mps_param_extract.h"
44 #include "ixheaace_mps_static_gain.h"
45 #include "ixheaace_mps_filter.h"
46 #include "ixheaace_mps_delay.h"
47 #include "ixheaace_mps_dmx_tdom_enh.h"
48 #include "ixheaace_mps_tools_rom.h"
49
ixheaace_mps_212_dct_iv(FLOAT32 * ptr_data,WORD32 length,WORD8 * ptr_scratch)50 IA_ERRORCODE ixheaace_mps_212_dct_iv(FLOAT32 *ptr_data, WORD32 length, WORD8 *ptr_scratch) {
51 WORD32 sin_step = 0;
52 WORD32 length_by_2 = length >> 1;
53 WORD32 step, idx;
54 FLOAT32 accu_1, accu_2, accu_3, accu_4;
55 FLOAT32 *ptr_data_0;
56 FLOAT32 *ptr_data_1;
57 const ixheaace_cmplx_str *ptr_cmplx_twiddle;
58 const ixheaace_cmplx_str *ptr_cmplx_sin_twiddle;
59 ixheaace_scratch_mem *pstr_scratch = (ixheaace_scratch_mem *)ptr_scratch;
60
61 if (length == 64) {
62 ptr_cmplx_twiddle = sine_window_64;
63 ptr_cmplx_sin_twiddle = sine_table_1024;
64 sin_step = 32;
65 } else {
66 ptr_cmplx_twiddle = sine_window_32;
67 ptr_cmplx_sin_twiddle = sine_table_1024;
68 sin_step = 64;
69 }
70
71 ptr_data_0 = &ptr_data[0];
72 ptr_data_1 = &ptr_data[length - 2];
73 for (idx = 0; idx < length_by_2 - 1; idx += 2, ptr_data_0 += 2, ptr_data_1 -= 2) {
74 accu_1 =
75 (ptr_data_1[1] * ptr_cmplx_twiddle[idx].re) - (ptr_data_0[0] * ptr_cmplx_twiddle[idx].im);
76 accu_2 =
77 (ptr_data_1[1] * ptr_cmplx_twiddle[idx].im) + (ptr_data_0[0] * ptr_cmplx_twiddle[idx].re);
78 accu_3 = (ptr_data_1[0] * ptr_cmplx_twiddle[idx + 1].re) -
79 (ptr_data_0[1] * ptr_cmplx_twiddle[idx + 1].im);
80 accu_4 = (ptr_data_1[0] * ptr_cmplx_twiddle[idx + 1].im) +
81 (ptr_data_0[1] * ptr_cmplx_twiddle[idx + 1].re);
82
83 ptr_data_0[0] = accu_2 / 4;
84 ptr_data_0[1] = accu_1 / 4;
85 ptr_data_1[0] = accu_4 / 4;
86 ptr_data_1[1] = -(accu_3 / 4);
87 }
88 ia_enhaacplus_enc_complex_fft(ptr_data, length_by_2, pstr_scratch);
89 if (length_by_2 == 32) {
90 for (idx = 0; idx < length; idx++) {
91 ptr_data[idx] = (ptr_data[idx] / (1 << (4)));
92 }
93 } else {
94 for (idx = 0; idx < length; idx++) {
95 ptr_data[idx] = (ptr_data[idx] / (1 << (3)));
96 }
97 }
98
99 ptr_data_0 = &ptr_data[0];
100 ptr_data_1 = &ptr_data[length - 2];
101 accu_1 = ptr_data_1[0];
102 accu_2 = ptr_data_1[1];
103 ptr_data_1[1] = -ptr_data_0[1];
104
105 for (step = sin_step, idx = 1; idx<(length_by_2 + 1)>> 1; idx++, step += sin_step) {
106 ixheaace_cmplx_str twd = ptr_cmplx_sin_twiddle[step];
107 accu_3 = (accu_1 * twd.re) - (accu_2 * twd.im);
108 accu_4 = (accu_1 * twd.im) + (accu_2 * twd.re);
109 ptr_data_0[1] = accu_3;
110 ptr_data_1[0] = accu_4;
111
112 ptr_data_0 += 2;
113 ptr_data_1 -= 2;
114
115 accu_3 = (ptr_data_0[1] * twd.re) - (ptr_data_0[0] * twd.im);
116 accu_4 = (ptr_data_0[1] * twd.im) + (ptr_data_0[0] * twd.re);
117 accu_1 = ptr_data_1[0];
118 accu_2 = ptr_data_1[1];
119
120 ptr_data_1[1] = -accu_3;
121 ptr_data_0[0] = accu_4;
122 }
123 accu_1 = (accu_1 * SQUARE_ROOT_TWO);
124 accu_2 = (accu_2 * SQUARE_ROOT_TWO);
125
126 ptr_data_1[0] = (accu_1 + accu_2) / 2;
127 ptr_data_0[1] = (accu_1 - accu_2) / 2;
128
129 return IA_NO_ERROR;
130 }
131
ixheaace_mps_212_dst_iv(FLOAT32 * ptr_data,WORD32 length,WORD8 * ptr_scratch)132 IA_ERRORCODE ixheaace_mps_212_dst_iv(FLOAT32 *ptr_data, WORD32 length, WORD8 *ptr_scratch) {
133 WORD32 sin_step = 0;
134 WORD32 step, idx;
135 WORD32 length_by_2 = length >> 1;
136 FLOAT32 accu_1, accu_2, accu_3, accu_4;
137 FLOAT32 *ptr_data_0;
138 FLOAT32 *ptr_data_1;
139 const ixheaace_cmplx_str *ptr_cmplx_twiddle;
140 const ixheaace_cmplx_str *ptr_cmplx_sin_twiddle;
141 ixheaace_scratch_mem *pstr_scratch = (ixheaace_scratch_mem *)ptr_scratch;
142 if (length == 64) {
143 ptr_cmplx_twiddle = sine_window_64;
144 ptr_cmplx_sin_twiddle = sine_table_1024;
145 sin_step = 32;
146 } else {
147 ptr_cmplx_twiddle = sine_window_32;
148 ptr_cmplx_sin_twiddle = sine_table_1024;
149 sin_step = 64;
150 }
151
152 ptr_data_0 = &ptr_data[0];
153 ptr_data_1 = &ptr_data[length - 2];
154 for (idx = 0; idx < length_by_2 - 1; idx += 2, ptr_data_0 += 2, ptr_data_1 -= 2) {
155 accu_1 = (ptr_data_1[1] * ptr_cmplx_twiddle[idx].re) -
156 ((-ptr_data_0[0]) * ptr_cmplx_twiddle[idx].im);
157 accu_2 = (ptr_data_1[1] * ptr_cmplx_twiddle[idx].im) +
158 ((-ptr_data_0[0]) * ptr_cmplx_twiddle[idx].re);
159 accu_3 = ((-ptr_data_1[0]) * ptr_cmplx_twiddle[idx + 1].re) -
160 (ptr_data_0[1] * ptr_cmplx_twiddle[idx + 1].im);
161 accu_4 = ((-ptr_data_1[0]) * ptr_cmplx_twiddle[idx + 1].im) +
162 (ptr_data_0[1] * ptr_cmplx_twiddle[idx + 1].re);
163 ptr_data_0[0] = accu_2 / 4;
164 ptr_data_0[1] = accu_1 / 4;
165 ptr_data_1[0] = accu_4 / 4;
166 ptr_data_1[1] = -(accu_3) / 4;
167 }
168 ia_enhaacplus_enc_complex_fft(ptr_data, length_by_2, pstr_scratch);
169 if (length_by_2 == 32) {
170 for (idx = 0; idx < length; idx++) {
171 ptr_data[idx] = (ptr_data[idx] / (1 << (4)));
172 }
173 } else {
174 for (idx = 0; idx < length; idx++) {
175 ptr_data[idx] = (ptr_data[idx] / (1 << (3)));
176 }
177 }
178 ptr_data_0 = &ptr_data[0];
179 ptr_data_1 = &ptr_data[length - 2];
180 accu_1 = ptr_data_1[0];
181 accu_2 = ptr_data_1[1];
182 ptr_data_1[1] = -ptr_data_0[0];
183 ptr_data_0[0] = ptr_data_0[1];
184
185 for (step = sin_step, idx = 1; idx<(length_by_2 + 1)>> 1; idx++, step += sin_step) {
186 ixheaace_cmplx_str twd = ptr_cmplx_sin_twiddle[step];
187
188 accu_3 = (accu_1 * twd.re) - (accu_2 * twd.im);
189 accu_4 = (accu_1 * twd.im) + (accu_2 * twd.re);
190 ptr_data_1[0] = -accu_3;
191 ptr_data_0[1] = -accu_4;
192
193 ptr_data_0 += 2;
194 ptr_data_1 -= 2;
195
196 accu_3 = (ptr_data_0[1] * twd.re) - (ptr_data_0[0] * twd.im);
197 accu_4 = (ptr_data_0[1] * twd.im) + (ptr_data_0[0] * twd.re);
198 accu_1 = ptr_data_1[0];
199 accu_2 = ptr_data_1[1];
200
201 ptr_data_0[0] = accu_3;
202 ptr_data_1[1] = -accu_4;
203 }
204
205 accu_1 = (accu_1 * SQUARE_ROOT_TWO);
206 accu_2 = (accu_2 * SQUARE_ROOT_TWO);
207 ptr_data_1[0] = (accu_1 + accu_2) / 2;
208 ptr_data_0[1] = (accu_1 - accu_2) / 2;
209
210 return IA_NO_ERROR;
211 }
212