xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_radix2_fft.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 <math.h>
22 #include "ixheaac_type_def.h"
23 #include "ixheaac_constants.h"
24 #include "ixheaace_aac_constants.h"
25 #include "ixheaace_psy_const.h"
26 #include "ixheaace_tns.h"
27 #include "ixheaace_tns_params.h"
28 #include "ixheaace_rom.h"
29 #include "ixheaace_common_rom.h"
30 #include "ixheaace_bitbuffer.h"
31 
ia_enhaacplus_enc_ps_fft(complex * out,LOOPINDEX N,WORD32 nob,ixheaace_common_tables * comm_tab_ptr)32 static VOID ia_enhaacplus_enc_ps_fft(complex *out, LOOPINDEX N, WORD32 nob,
33                                      ixheaace_common_tables *comm_tab_ptr) {
34   LOOPINDEX block_per_stage, stage_num, inner;
35 
36   WORD32 index_1, index_2, tab_modifier;
37   WORD32 len, increment, i;
38 
39   FLOAT32 cos_val;
40   FLOAT32 sin_val;
41 
42   WORD16 index1;
43   FLOAT32 re_temp, re_temp2;
44   FLOAT32 im_temp, im_temp2;
45   FLOAT32 *out1_w32, *out2_w32;
46 
47   len = N >> 1;
48   index_1 = 0;
49 
50   out1_w32 = (FLOAT32 *)&out[index_1];
51   out2_w32 = (FLOAT32 *)&out[index_1 + 1];
52 
53   for (block_per_stage = len - 1; block_per_stage >= 0; block_per_stage--) {
54     re_temp = out1_w32[0];
55     im_temp = out1_w32[1];
56 
57     re_temp2 = out2_w32[0];
58     im_temp2 = out2_w32[1];
59 
60     out1_w32[0] = re_temp + re_temp2;
61     out1_w32[1] = im_temp + im_temp2;
62 
63     out2_w32[0] = re_temp - re_temp2;
64     out2_w32[1] = im_temp - im_temp2;
65 
66     out1_w32 += 4;
67     out2_w32 += 4;
68   }
69 
70   if (nob == 3) {
71     i = 2; /* used for dist calculation*/
72     increment = 1;
73     tab_modifier = 2;
74     for (stage_num = 1; stage_num >= 0; stage_num--) {
75       len = len >> 1;
76       index_1 = 0;
77       increment += 1;
78 
79       tab_modifier--;
80 
81       for (block_per_stage = len - 1; block_per_stage >= 0; block_per_stage--) {
82         index_2 = index_1 + i;
83 
84         out1_w32 = (FLOAT32 *)&out[index_1];
85         out2_w32 = (FLOAT32 *)&out[index_2];
86 
87         re_temp = out1_w32[0];
88         im_temp = out1_w32[1];
89 
90         re_temp2 = out2_w32[0];
91         im_temp2 = out2_w32[1];
92 
93         out1_w32[0] = re_temp + re_temp2;
94         out1_w32[1] = im_temp + im_temp2;
95 
96         out2_w32[0] = re_temp - re_temp2;
97         out2_w32[1] = im_temp - im_temp2;
98 
99         index1 = (WORD16)tab_modifier;
100 
101         out1_w32 += 2;
102         out2_w32 += 2;
103 
104         for (inner = 0; inner < (2 * i - 2); inner += 2) {
105           cos_val = comm_tab_ptr->cos_arr[index1];
106           sin_val = comm_tab_ptr->sin_arr[index1];
107 
108           index1++;
109 
110           re_temp = (out2_w32[inner] * cos_val) + (out2_w32[inner + 1] * sin_val);
111           im_temp = (out2_w32[inner + 1] * cos_val) - (out2_w32[inner] * sin_val);
112 
113           re_temp2 = out1_w32[inner];
114           im_temp2 = out1_w32[inner + 1];
115 
116           out1_w32[inner] = re_temp2 + re_temp;
117           out1_w32[inner + 1] = im_temp2 + im_temp;
118 
119           out2_w32[inner] = re_temp2 - re_temp;
120           out2_w32[inner + 1] = im_temp2 - im_temp;
121         }
122 
123         index_1 += (WORD32)pow(2, increment);
124       }
125       i <<= 1;
126     }
127   } else {
128     out1_w32 = (FLOAT32 *)&out[0];
129     out2_w32 = (FLOAT32 *)&out[2];
130 
131     re_temp = out1_w32[0];
132     im_temp = out1_w32[1];
133 
134     re_temp2 = out2_w32[0];
135     im_temp2 = out2_w32[1];
136 
137     out1_w32[0] = re_temp + re_temp2;
138     out1_w32[1] = im_temp + im_temp2;
139 
140     out2_w32[0] = re_temp - re_temp2;
141     out2_w32[1] = im_temp - im_temp2;
142 
143     out1_w32 += 2;
144     out2_w32 += 2;
145 
146     sin_val = comm_tab_ptr->sin_arr[1];
147 
148     re_temp = out2_w32[1] * sin_val;
149     im_temp = out2_w32[0] * sin_val;
150 
151     re_temp2 = out1_w32[0];
152     im_temp2 = out1_w32[1];
153 
154     out1_w32[0] = re_temp2 + re_temp;
155     out1_w32[1] = im_temp2 - im_temp;
156 
157     out2_w32[0] = re_temp2 - re_temp;
158     out2_w32[1] = im_temp2 + im_temp;
159   }
160 }
161 
ia_enhaacplus_enc_fft(complex * out,WORD32 N,ixheaace_common_tables * pstr_common_tab)162 VOID ia_enhaacplus_enc_fft(complex *out, WORD32 N, ixheaace_common_tables *pstr_common_tab) {
163   WORD32 nob;
164   WORD32 len;
165 
166   /* time domain samples obtained with bit reversal*/
167   len = N;
168 
169   if (len == 8) {
170     nob = 3;
171   } else {
172     nob = 2;
173   }
174   ia_enhaacplus_enc_ps_fft(out, N, nob, pstr_common_tab);
175 }
176