1 /******************************************************************************
2 * *
3 * Copyright (C) 2018 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 #include <string.h>
21 #include "ixheaacd_sbr_common.h"
22 #include "ixheaacd_type_def.h"
23
24 #include "ixheaacd_constants.h"
25 #include "ixheaacd_basic_ops32.h"
26 #include "ixheaacd_basic_ops16.h"
27 #include "ixheaacd_basic_ops40.h"
28 #include "ixheaacd_basic_ops.h"
29
30 #include "ixheaacd_basic_op.h"
31 #include "ixheaacd_intrinsics.h"
32 #include "ixheaacd_bitbuffer.h"
33 #include "ixheaacd_sbrdecsettings.h"
34 #include "ixheaacd_sbr_scale.h"
35 #include "ixheaacd_lpp_tran.h"
36 #include "ixheaacd_env_extr_part.h"
37 #include "ixheaacd_sbr_rom.h"
38
39 #include "ixheaacd_hybrid.h"
40 #include "ixheaacd_ps_dec.h"
41
42 #include "ixheaacd_env_extr.h"
43 #include "ixheaacd_ps_bitdec.h"
44
45 const WORD16 ixheaacd_num_bands[3] = {10, 20, 34};
46
ixheaacd_clamp(WORD32 i,WORD16 min,WORD16 max)47 static WORD32 ixheaacd_clamp(WORD32 i, WORD16 min, WORD16 max) {
48 WORD32 result = i;
49
50 if (i < min) {
51 result = min;
52 } else {
53 if (i > max) {
54 result = max;
55 }
56 }
57
58 return result;
59 }
60
ixheaacd_divideby2(WORD op)61 WORD16 ixheaacd_divideby2(WORD op) {
62 FLAG sign = (op < 0);
63
64 if (sign) {
65 op = -(op);
66 }
67
68 op = (op >> 1);
69
70 if (sign) {
71 op = -op;
72 }
73
74 return (WORD16)op;
75 }
76
ixheaacd_divideby3(WORD op)77 WORD16 ixheaacd_divideby3(WORD op) {
78 WORD16 temp, ret;
79 FLAG sign = (op < 0);
80
81 if (sign) {
82 op = -(op);
83 }
84
85 temp = (WORD16)(op << 2);
86
87 temp = ixheaacd_mult16_shl(temp, 0x2aab);
88
89 ret = (temp >> 2);
90
91 if (sign) {
92 ret = -(ret);
93 }
94
95 return (WORD16)ret;
96 }
97
ixheaacd_decode_ps_data(ia_ps_dec_struct * ptr_ps_dec,WORD32 frame_size)98 VOID ixheaacd_decode_ps_data(ia_ps_dec_struct *ptr_ps_dec, WORD32 frame_size) {
99 WORD e, i, temp;
100 WORD16 iid_mode = (WORD16)((ptr_ps_dec->iid_mode) ? 1 : 2);
101 WORD16 icc_mode = (WORD16)((ptr_ps_dec->icc_mode) ? 1 : 2);
102 WORD16 num_iid_levels =
103 (WORD16)(ptr_ps_dec->iid_quant ? NUM_IID_LEVELS_FINE : NUM_IID_LEVELS);
104 WORD32 max_num_columns;
105
106 if (frame_size == 960)
107 max_num_columns = MAX_NUM_COLUMNS_960;
108 else
109 max_num_columns = MAX_NUM_COLUMNS;
110
111 if (!ptr_ps_dec->ps_data_present) {
112 ptr_ps_dec->num_env = 0;
113 }
114
115 for (e = 0; e < ptr_ps_dec->num_env; e++) {
116 WORD16 *p_iid_par_prev;
117 WORD16 *p_icc_par_prev;
118
119 if (e == 0) {
120 p_iid_par_prev = ptr_ps_dec->iid_par_prev;
121 p_icc_par_prev = ptr_ps_dec->icc_par_prev;
122 } else {
123 p_iid_par_prev = ptr_ps_dec->iid_par_table[e - 1];
124 p_icc_par_prev = ptr_ps_dec->icc_par_table[e - 1];
125 }
126
127 if (ptr_ps_dec->enable_iid) {
128 if (ptr_ps_dec->iid_dt[e]) {
129 for (i = 0; i < ixheaacd_num_bands[ptr_ps_dec->iid_mode]; i++) {
130 temp =
131 ixheaacd_add16(*p_iid_par_prev, ptr_ps_dec->iid_par_table[e][i]);
132 ptr_ps_dec->iid_par_table[e][i] = ixheaacd_clamp(
133 temp, ixheaacd_negate16(num_iid_levels), num_iid_levels);
134 p_iid_par_prev += iid_mode;
135 }
136 } else {
137 ptr_ps_dec->iid_par_table[e][0] =
138 ixheaacd_clamp(ptr_ps_dec->iid_par_table[e][0],
139 ixheaacd_negate16(num_iid_levels), num_iid_levels);
140 for (i = 1; i < ixheaacd_num_bands[ptr_ps_dec->iid_mode]; i++) {
141 temp = ixheaacd_add16(ptr_ps_dec->iid_par_table[e][i - 1],
142 ptr_ps_dec->iid_par_table[e][i]);
143 ptr_ps_dec->iid_par_table[e][i] = ixheaacd_clamp(
144 temp, ixheaacd_negate16(num_iid_levels), num_iid_levels);
145 }
146 }
147 } else {
148 memset(ptr_ps_dec->iid_par_table[e], 0,
149 sizeof(WORD16) * ixheaacd_num_bands[ptr_ps_dec->iid_mode]);
150 }
151
152 if (iid_mode == 2) {
153 for (i = (ixheaacd_num_bands[ptr_ps_dec->iid_mode] * iid_mode - 1);
154 i != 0; i--) {
155 ptr_ps_dec->iid_par_table[e][i] =
156 ptr_ps_dec->iid_par_table[e][ixheaacd_shr32(i, 1)];
157 }
158 }
159
160 if (ptr_ps_dec->enable_icc) {
161 if (ptr_ps_dec->icc_dt[e]) {
162 for (i = 0; i < ixheaacd_num_bands[ptr_ps_dec->icc_mode]; i++) {
163 temp =
164 ixheaacd_add16(*p_icc_par_prev, ptr_ps_dec->icc_par_table[e][i]);
165 ptr_ps_dec->icc_par_table[e][i] =
166 ixheaacd_clamp(temp, 0, (WORD16)(NUM_ICC_LEVELS - 1));
167 p_icc_par_prev += icc_mode;
168 }
169 } else {
170 ptr_ps_dec->icc_par_table[e][0] = ixheaacd_clamp(
171 ptr_ps_dec->icc_par_table[e][0], 0, (WORD16)(NUM_ICC_LEVELS - 1));
172 for (i = 1; i < ixheaacd_num_bands[ptr_ps_dec->icc_mode]; i++) {
173 temp = ixheaacd_add16(ptr_ps_dec->icc_par_table[e][i - 1],
174 ptr_ps_dec->icc_par_table[e][i]);
175 ptr_ps_dec->icc_par_table[e][i] =
176 ixheaacd_clamp(temp, 0, (WORD16)(NUM_ICC_LEVELS - 1));
177 }
178 }
179 } else {
180 memset(ptr_ps_dec->icc_par_table[e], 0,
181 sizeof(WORD16) * ixheaacd_num_bands[ptr_ps_dec->icc_mode]);
182 }
183
184 if (icc_mode == 2) {
185 for (i = (ixheaacd_num_bands[ptr_ps_dec->icc_mode] * icc_mode - 1);
186 i != 0; i--) {
187 ptr_ps_dec->icc_par_table[e][i] =
188 ptr_ps_dec->icc_par_table[e][ixheaacd_shr32(i, 1)];
189 }
190 }
191 }
192
193 if (ptr_ps_dec->num_env == 0) {
194 ptr_ps_dec->num_env = 1;
195
196 if (ptr_ps_dec->enable_iid) {
197 memcpy(ptr_ps_dec->iid_par_table[0], ptr_ps_dec->iid_par_prev,
198 sizeof(WORD16) * NUM_BANDS_FINE);
199 } else {
200 memset(ptr_ps_dec->iid_par_table[0], 0, sizeof(WORD16) * NUM_BANDS_FINE);
201 }
202
203 if (ptr_ps_dec->enable_icc) {
204 memcpy(ptr_ps_dec->icc_par_table[0], ptr_ps_dec->icc_par_prev,
205 sizeof(WORD16) * NUM_BANDS_FINE);
206 } else {
207 memset(ptr_ps_dec->icc_par_table[0], 0, sizeof(WORD16) * NUM_BANDS_FINE);
208 }
209 }
210
211 memcpy(ptr_ps_dec->iid_par_prev,
212 ptr_ps_dec->iid_par_table[ptr_ps_dec->num_env - 1],
213 sizeof(WORD16) * NUM_BANDS_FINE);
214
215 memcpy(ptr_ps_dec->icc_par_prev,
216 ptr_ps_dec->icc_par_table[ptr_ps_dec->num_env - 1],
217 sizeof(WORD16) * NUM_BANDS_FINE);
218
219 ptr_ps_dec->ps_data_present = 0;
220
221 if (ptr_ps_dec->frame_class == 0) {
222 WORD env_count;
223 WORD16 shift = 0;
224
225 switch (ptr_ps_dec->num_env) {
226 case 1:
227 shift = 0;
228 break;
229 case 2:
230 shift = 1;
231 break;
232 case 4:
233 shift = 2;
234 break;
235 }
236 ptr_ps_dec->border_position[0] = 0;
237 env_count = 0;
238
239 for (e = 1; e < ptr_ps_dec->num_env; e++) {
240 env_count = add_d(env_count, max_num_columns);
241 ptr_ps_dec->border_position[e] = (WORD16)(env_count >> shift);
242 }
243 ptr_ps_dec->border_position[ptr_ps_dec->num_env] = max_num_columns;
244 } else {
245 ptr_ps_dec->border_position[0] = 0;
246
247 if (ptr_ps_dec->border_position[ptr_ps_dec->num_env] < max_num_columns) {
248 ptr_ps_dec->num_env++;
249 ptr_ps_dec->border_position[ptr_ps_dec->num_env] = max_num_columns;
250
251 memcpy(ptr_ps_dec->iid_par_table[ptr_ps_dec->num_env - 1],
252 ptr_ps_dec->iid_par_table[ptr_ps_dec->num_env - 2],
253 sizeof(WORD16) * NUM_BANDS_FINE);
254
255 memcpy(ptr_ps_dec->icc_par_table[ptr_ps_dec->num_env - 1],
256 ptr_ps_dec->icc_par_table[ptr_ps_dec->num_env - 2],
257 sizeof(WORD16) * NUM_BANDS_FINE);
258 }
259
260 for (e = 1; e < ptr_ps_dec->num_env; e++) {
261 WORD threshold;
262 threshold = sub_d(max_num_columns, sub_d(ptr_ps_dec->num_env, e));
263
264 if (ptr_ps_dec->border_position[e] > threshold) {
265 ptr_ps_dec->border_position[e] = threshold;
266 } else {
267 threshold = add_d(ptr_ps_dec->border_position[e - 1], 1);
268
269 if (ptr_ps_dec->border_position[e] < threshold) {
270 ptr_ps_dec->border_position[e] = threshold;
271 }
272 }
273 }
274 }
275
276 for (e = 0; e < ptr_ps_dec->num_env; e++) {
277 if (ptr_ps_dec->iid_mode == 2)
278 ixheaacd_map_34_params_to_20(ptr_ps_dec->iid_par_table[e]);
279
280 if (ptr_ps_dec->icc_mode == 2)
281 ixheaacd_map_34_params_to_20(ptr_ps_dec->icc_par_table[e]);
282 }
283 }