xref: /aosp_15_r20/external/libxaac/encoder/iusace_windowing.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 "ixheaac_error_standards.h"
23 #include "ixheaac_type_def.h"
24 #include "iusace_cnst.h"
25 #include "iusace_block_switch_const.h"
26 #include "iusace_rom.h"
27 #include "ixheaace_error_codes.h"
28 
iusace_calc_window(FLOAT64 ** win,WORD32 win_sz,WORD32 win_sel)29 IA_ERRORCODE iusace_calc_window(FLOAT64 **win, WORD32 win_sz, WORD32 win_sel) {
30   switch (win_sel) {
31     case WIN_SEL_0:
32       switch (win_sz) {
33         case WIN_LEN_96:
34           *win = (FLOAT64 *)iexheaac_sine_win_96;
35           break;
36         case WIN_LEN_192:
37           *win = (FLOAT64 *)iexheaac_sine_win_192;
38           break;
39         case WIN_LEN_128:
40           *win = (FLOAT64 *)iusace_sine_win_128;
41           break;
42         case WIN_LEN_256:
43           *win = (FLOAT64 *)iusace_sine_win_256;
44           break;
45         case WIN_LEN_768:
46           *win = (FLOAT64 *)iexheaac_sine_win_768;
47           break;
48         case WIN_LEN_1024:
49           *win = (FLOAT64 *)iusace_sine_win_1024;
50           break;
51         default:
52           return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_WINDOW_LENGTH;
53           break;
54       }
55       break;
56     case WIN_SEL_1:
57       switch (win_sz) {
58         case WIN_LEN_96:
59           *win = (FLOAT64 *)iexheaac_kbd_win_96;
60           break;
61         case WIN_LEN_128:
62           *win = (FLOAT64 *)iusace_sine_win_128;
63           break;
64         case WIN_LEN_192:
65           *win = (FLOAT64 *)iexheaac_kbd_win_192;
66           break;
67         case WIN_LEN_256:
68           *win = (FLOAT64 *)iusace_kbd_win256;
69           break;
70         case WIN_LEN_768:
71           *win = (FLOAT64 *)iexheaac_kbd_win_768;
72           break;
73         case WIN_LEN_1024:
74           *win = (FLOAT64 *)iusace_kbd_win1024;
75           break;
76         default:
77           return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_WINDOW_LENGTH;
78           break;
79       }
80       break;
81 
82     default:
83       return IA_EXHEAACE_EXE_FATAL_USAC_INVALID_WINDOW_SHAPE;
84       break;
85   }
86   return IA_NO_ERROR;
87 }
88 
iusace_windowing_long(FLOAT64 * ptr_overlap,FLOAT64 * ptr_win_long,FLOAT64 * ptr_win_buf,FLOAT64 * ptr_in_data,WORD32 n_long)89 VOID iusace_windowing_long(FLOAT64 *ptr_overlap, FLOAT64 *ptr_win_long, FLOAT64 *ptr_win_buf,
90                            FLOAT64 *ptr_in_data, WORD32 n_long) {
91   WORD32 i;
92   FLOAT64 *ptr_win = ptr_win_long + n_long - 1;
93   WORD32 data_size = (OVERLAP_WIN_SIZE_576 * n_long) / LEN_SUPERFRAME;
94 
95   for (i = 0; i < n_long; i++) {
96     ptr_win_buf[i] = ptr_overlap[i] * ptr_win_long[i];
97   }
98 
99   memcpy(ptr_overlap, ptr_overlap + n_long, data_size * sizeof(ptr_overlap[0]));
100   memcpy(ptr_overlap + data_size, ptr_in_data, n_long * sizeof(ptr_overlap[0]));
101 
102   for (i = 0; i < n_long; i++) {
103     ptr_win_buf[i + n_long] = ptr_overlap[i] * (*ptr_win--);
104   }
105 
106   return;
107 }
108 
iusace_windowing_long_start(FLOAT64 * ptr_overlap,FLOAT64 * ptr_win_long,FLOAT64 * ptr_win_buf,FLOAT64 * ptr_in_data,WORD32 n_long,WORD32 nflat_ls,FLOAT64 * ptr_win_med,WORD32 win_sz)109 VOID iusace_windowing_long_start(FLOAT64 *ptr_overlap, FLOAT64 *ptr_win_long,
110                                  FLOAT64 *ptr_win_buf, FLOAT64 *ptr_in_data, WORD32 n_long,
111                                  WORD32 nflat_ls, FLOAT64 *ptr_win_med, WORD32 win_sz) {
112   WORD32 i;
113   FLOAT64 *ptr_win = ptr_win_buf + 2 * n_long - 1;
114   WORD32 data_size = (OVERLAP_WIN_SIZE_576 * n_long) / LEN_SUPERFRAME;
115 
116   for (i = 0; i < n_long; i++) {
117     ptr_win_buf[i] = ptr_overlap[i] * ptr_win_long[i];
118   }
119 
120   memcpy(ptr_overlap, ptr_overlap + n_long, data_size * sizeof(ptr_overlap[0]));
121   memcpy(ptr_overlap + data_size, ptr_in_data, n_long * sizeof(ptr_overlap[0]));
122   memcpy(ptr_win_buf + n_long, ptr_overlap, nflat_ls * sizeof(ptr_win_buf[0]));
123 
124   ptr_win_med = ptr_win_med + win_sz - 1;
125   win_sz = n_long - 2 * nflat_ls;
126 
127   for (i = 0; i < win_sz; i++) {
128     ptr_win_buf[i + n_long + nflat_ls] = ptr_overlap[i + nflat_ls] * (*ptr_win_med--);
129   }
130 
131   for (i = 0; i < nflat_ls; i++) {
132     *ptr_win-- = 0;
133   }
134 
135   return;
136 }
137 
iusace_windowing_long_stop(FLOAT64 * ptr_overlap,FLOAT64 * ptr_win_long,FLOAT64 * ptr_win_buf,FLOAT64 * ptr_in_data,WORD32 n_long,WORD32 nflat_ls,FLOAT64 * ptr_win_med,WORD32 win_sz)138 VOID iusace_windowing_long_stop(FLOAT64 *ptr_overlap, FLOAT64 *ptr_win_long, FLOAT64 *ptr_win_buf,
139                                 FLOAT64 *ptr_in_data, WORD32 n_long, WORD32 nflat_ls,
140                                 FLOAT64 *ptr_win_med, WORD32 win_sz) {
141   WORD32 i;
142   FLOAT64 *ptr_win = ptr_win_long + n_long - 1;
143   WORD32 data_size = (OVERLAP_WIN_SIZE_576 * n_long) / LEN_SUPERFRAME;
144 
145   memset(ptr_win_buf, 0, nflat_ls * sizeof(FLOAT64));
146   for (i = 0; i < win_sz; i++) {
147     ptr_win_buf[i + nflat_ls] = ptr_overlap[i + nflat_ls] * ptr_win_med[i];
148   }
149 
150   memcpy(ptr_win_buf + nflat_ls + win_sz, ptr_overlap + nflat_ls + win_sz,
151          nflat_ls * sizeof(ptr_win_buf[0]));
152   memcpy(ptr_overlap, ptr_overlap + n_long, data_size * sizeof(ptr_overlap[0]));
153   memcpy(ptr_overlap + data_size, ptr_in_data, n_long * sizeof(ptr_overlap[0]));
154 
155   for (i = 0; i < n_long; i++) {
156     ptr_win_buf[i + n_long] = ptr_overlap[i] * (*ptr_win--);
157   }
158   return;
159 }
160 
iusace_windowing_stop_start(FLOAT64 * ptr_overlap,FLOAT64 * ptr_win_buf,FLOAT64 * ptr_win_med,WORD32 win_sz,WORD32 n_long)161 VOID iusace_windowing_stop_start(FLOAT64 *ptr_overlap, FLOAT64 *ptr_win_buf, FLOAT64 *ptr_win_med,
162                                  WORD32 win_sz, WORD32 n_long) {
163   WORD32 i;
164   FLOAT64 *win_gen;
165   WORD32 wsize = (n_long - win_sz) >> 1;
166   win_gen = ptr_win_med;
167 
168   for (i = 0; i < win_sz; i++) {
169     ptr_win_buf[wsize + i] = ptr_overlap[wsize + i] * (*win_gen++);
170   }
171   memcpy(ptr_win_buf + wsize, ptr_overlap + wsize, wsize * sizeof(FLOAT64));
172   memcpy(ptr_win_buf + n_long, ptr_overlap + n_long, wsize * sizeof(FLOAT64));
173 
174   win_gen = ptr_win_med + win_sz - 1;
175   win_sz = n_long - 2 * wsize;
176 
177   for (i = 0; i < win_sz; i++) {
178     ptr_win_buf[n_long + wsize + i] = ptr_overlap[n_long + wsize + i] * (*win_gen--);
179   }
180   return;
181 }
182