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 #include "ixheaacd_type_def.h"
21 #include "ixheaacd_constants.h"
22 #include "ixheaacd_basic_ops32.h"
23 #include "ixheaacd_basic_ops40.h"
24 #include "ixheaacd_basic_ops.h"
25 #include "ixheaacd_bitbuffer.h"
26 #include "ixheaacd_basic_op.h"
27 #include "ixheaacd_mps_aac_struct.h"
28 #include "ixheaacd_mps_res_rom.h"
29 #include "ixheaacd_mps_res_block.h"
30 #include "ixheaacd_mps_res_huffman.h"
31
ixheaacd_res_extract_symbol(WORD32 value,WORD32 l_shift,WORD32 r_shift,WORD32 * pow_table_q17)32 static PLATFORM_INLINE WORD32 ixheaacd_res_extract_symbol(WORD32 value, WORD32 l_shift,
33 WORD32 r_shift, WORD32 *pow_table_q17) {
34 WORD32 out;
35 out = (WORD16)((value << l_shift) >> r_shift);
36
37 if (out < 0) {
38 out = -out;
39 out = pow_table_q17[out];
40 out = -out;
41 } else
42 out = pow_table_q17[out];
43
44 return out;
45 }
46
ixheaacd_res_extract_signed_symbol(WORD32 value,WORD32 l_shift,WORD32 r_shift,WORD32 * pow_table_q17,WORD32 * temp_word,WORD32 * pr_bit_pos)47 static PLATFORM_INLINE WORD32 ixheaacd_res_extract_signed_symbol(WORD32 value, WORD32 l_shift,
48 WORD32 r_shift,
49 WORD32 *pow_table_q17,
50 WORD32 *temp_word,
51 WORD32 *pr_bit_pos) {
52 WORD32 out;
53 out = ixheaacd_extu(value, l_shift, r_shift);
54 if (out) {
55 WORD32 bit_pos = *pr_bit_pos;
56 out = pow_table_q17[out];
57 if (*temp_word & 0x80000000) {
58 out = -out;
59 }
60 *temp_word = *temp_word << 1;
61 bit_pos++;
62 *pr_bit_pos = bit_pos;
63 }
64 return out;
65 }
66
ixheaacd_res_inverse_quant_lb(WORD32 * x_invquant,WORD t_bands,WORD32 * pow_table_q17,WORD8 * pulse_data)67 VOID ixheaacd_res_inverse_quant_lb(WORD32 *x_invquant, WORD t_bands, WORD32 *pow_table_q17,
68 WORD8 *pulse_data) {
69 WORD32 j;
70 WORD32 temp;
71 WORD32 q_abs;
72
73 for (j = t_bands - 1; j >= 0; j--) {
74 q_abs = *pulse_data++;
75 temp = (pow_table_q17[q_abs]);
76 *x_invquant++ = -temp;
77 }
78 }
79
ixheaacd_res_c_block_decode_huff_word1(ia_bit_buf_struct * it_bit_buf,WORD32 * qp,WORD16 * offsets,WORD no_bands,WORD group_no,const UWORD16 * h_ori,WORD32 * pow_table_q17,WORD32 maximum_bins_short)80 static PLATFORM_INLINE WORD ixheaacd_res_c_block_decode_huff_word1(
81 ia_bit_buf_struct *it_bit_buf, WORD32 *qp, WORD16 *offsets, WORD no_bands, WORD group_no,
82 const UWORD16 *h_ori, WORD32 *pow_table_q17, WORD32 maximum_bins_short) {
83 WORD32 sp1, sp2;
84 WORD32 flush_cw;
85 WORD32 i, value, norm_val, off;
86 WORD idx, grp_idx;
87 WORD32 out1, out2;
88 WORD32 err_code = 0;
89 WORD len_idx = 0;
90 UWORD8 *ptr_read_next = it_bit_buf->ptr_read_next;
91 WORD32 bit_pos = it_bit_buf->bit_pos;
92 WORD32 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
93 ptr_read_next += 4;
94
95 do {
96 len_idx = offsets[1] - offsets[0];
97 grp_idx = group_no;
98 do {
99 qp = qp + offsets[0];
100 idx = len_idx;
101 do {
102 {
103 UWORD16 first_offset;
104 WORD16 sign_ret_val;
105 UWORD32 read_word1;
106 UWORD16 *h;
107
108 read_word1 = read_word << bit_pos;
109
110 h = (UWORD16 *)h_ori;
111 h += (read_word1) >> (27);
112 sign_ret_val = *h;
113
114 first_offset = 5;
115 while (sign_ret_val > 0) {
116 bit_pos += first_offset;
117 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
118 it_bit_buf->ptr_bit_buf_end);
119 read_word1 = (read_word1) << (first_offset);
120 first_offset = (sign_ret_val >> 11);
121 h += sign_ret_val & (0x07FF);
122 h += (read_word1) >> (32 - first_offset);
123 sign_ret_val = *h;
124 }
125 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
126 value = sign_ret_val & (0x07FF);
127 }
128 out1 = (value & 0x3E0) >> 5;
129 out2 = value & 0x1F;
130
131 flush_cw = read_word << bit_pos;
132
133 sp1 = out1;
134 sp2 = out2;
135
136 if (out1) {
137 if (flush_cw & 0x80000000) {
138 out1 = -out1;
139 }
140 bit_pos++;
141 flush_cw = (WORD32)flush_cw << 1;
142 }
143
144 if (out2) {
145 bit_pos++;
146 if (flush_cw & 0x80000000) {
147 out2 = -out2;
148 }
149 }
150
151 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
152 it_bit_buf->ptr_bit_buf_end);
153 if (sp1 == 16) {
154 i = 4;
155 value = ixheaacd_extu(read_word, bit_pos, 23);
156 value = value | 0xfffffe00;
157 norm_val = ixheaacd_norm32(value);
158
159 i += (norm_val - 22);
160 bit_pos += (norm_val - 21);
161
162 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
163 it_bit_buf->ptr_bit_buf_end);
164
165 off = ixheaacd_extu(read_word, bit_pos, 32 - i);
166
167 bit_pos += i;
168
169 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
170 it_bit_buf->ptr_bit_buf_end);
171
172 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
173 it_bit_buf->ptr_bit_buf_end);
174
175 i = off + ((WORD32)1 << i);
176
177 if (i <= IQ_TABLE_SIZE_HALF)
178 i = pow_table_q17[i];
179 else {
180 err_code |= ixheaacd_res_inv_quant(&i, pow_table_q17);
181 }
182
183 if (out1 < 0) {
184 out1 = -i;
185 } else {
186 out1 = i;
187 }
188 *qp++ = out1;
189 } else {
190 if (out1 <= 0) {
191 out1 = -out1;
192 out1 = pow_table_q17[out1];
193 *qp++ = -out1;
194 } else {
195 out1 = pow_table_q17[out1];
196 *qp++ = out1;
197 }
198 }
199 if (sp2 == 16) {
200 i = 4;
201 value = ixheaacd_extu(read_word, bit_pos, 23);
202 value = value | 0xfffffe00;
203 norm_val = ixheaacd_norm32(value);
204
205 i += (norm_val - 22);
206
207 bit_pos += (norm_val - 21);
208
209 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
210 it_bit_buf->ptr_bit_buf_end);
211
212 off = ixheaacd_extu(read_word, bit_pos, 32 - i);
213
214 bit_pos += i;
215
216 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
217 it_bit_buf->ptr_bit_buf_end);
218
219 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
220 it_bit_buf->ptr_bit_buf_end);
221
222 i = off + ((WORD32)1 << i);
223
224 if (i <= IQ_TABLE_SIZE_HALF)
225 i = pow_table_q17[i];
226 else {
227 err_code |= ixheaacd_res_inv_quant(&i, pow_table_q17);
228 }
229
230 if (out2 < 0) {
231 out2 = -i;
232 } else {
233 out2 = i;
234 }
235 *qp++ = out2;
236 } else {
237 if (out2 <= 0) {
238 out2 = -out2;
239 out2 = pow_table_q17[out2];
240 *qp++ = -out2;
241 } else {
242 out2 = pow_table_q17[out2];
243 *qp++ = out2;
244 }
245 }
246
247 idx -= 2;
248 } while (idx != 0);
249
250 qp += (maximum_bins_short - offsets[1]);
251 grp_idx--;
252 } while (grp_idx != 0);
253
254 offsets++;
255 qp -= (maximum_bins_short * group_no);
256 no_bands--;
257 } while (no_bands >= 0);
258
259 it_bit_buf->bit_pos = bit_pos;
260 it_bit_buf->ptr_read_next = ptr_read_next - 4;
261
262 return err_code;
263 }
264
ixheaacd_res_c_block_decode_huff_word1_lb(ia_bit_buf_struct * it_bif_buf,WORD32 len,const UWORD16 * h_ori,WORD32 * x_invquant,WORD32 * pow_table_q17,WORD8 * p_pul_arr)265 static PLATFORM_INLINE WORD ixheaacd_res_c_block_decode_huff_word1_lb(
266 ia_bit_buf_struct *it_bif_buf, WORD32 len, const UWORD16 *h_ori, WORD32 *x_invquant,
267 WORD32 *pow_table_q17, WORD8 *p_pul_arr) {
268 WORD32 sp1, sp2;
269 WORD32 flush_cw;
270 WORD32 i, value, norm_val, off;
271 WORD idx;
272 WORD32 out1, out2;
273 WORD32 err_code = 0;
274 UWORD8 *ptr_read_next = it_bif_buf->ptr_read_next;
275 WORD32 bit_pos = it_bif_buf->bit_pos;
276 WORD32 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
277 ptr_read_next += 4;
278
279 for (idx = len; idx != 0; idx -= 2) {
280 {
281 UWORD16 first_offset;
282 WORD16 sign_ret_val;
283 UWORD32 read_word1;
284 UWORD16 *h;
285
286 read_word1 = read_word << bit_pos;
287
288 h = (UWORD16 *)h_ori;
289 h += (read_word1) >> (27);
290 sign_ret_val = *h;
291
292 first_offset = 5;
293 while (sign_ret_val > 0) {
294 bit_pos += first_offset;
295 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
296 it_bif_buf->ptr_bit_buf_end);
297 read_word1 = (read_word1) << (first_offset);
298
299 first_offset = (sign_ret_val >> 11);
300 h += sign_ret_val & (0x07FF);
301
302 h += (read_word1) >> (32 - first_offset);
303 sign_ret_val = *h;
304 }
305 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
306 value = sign_ret_val & (0x07FF);
307 }
308
309 flush_cw = read_word << bit_pos;
310
311 out1 = (value & 0x3E0) >> 5;
312 out2 = value & 0x1F;
313
314 sp1 = out1;
315
316 if (out1) {
317 if (flush_cw & 0x80000000) {
318 out1 = -out1;
319 }
320
321 bit_pos++;
322 flush_cw = (WORD32)flush_cw << 1;
323 }
324
325 sp2 = out2;
326 if (out2) {
327 bit_pos++;
328 if (flush_cw & 0x80000000) {
329 out2 = -out2;
330 }
331 }
332
333 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
334 it_bif_buf->ptr_bit_buf_end);
335
336 if (sp1 == 16) {
337 i = 4;
338 value = ixheaacd_extu(read_word, bit_pos, 23);
339 value = value | 0xfffffe00;
340 norm_val = ixheaacd_norm32(value);
341 i += (norm_val - 22);
342 bit_pos += (norm_val - 21);
343
344 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
345 it_bif_buf->ptr_bit_buf_end);
346
347 off = ixheaacd_extu(read_word, bit_pos, 32 - i);
348
349 bit_pos += i;
350
351 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
352 it_bif_buf->ptr_bit_buf_end);
353 value = *p_pul_arr++;
354 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
355 it_bif_buf->ptr_bit_buf_end);
356 i = off + ((WORD32)1 << i);
357 i = add_d(i, value);
358
359 if (i <= IQ_TABLE_SIZE_HALF)
360 i = pow_table_q17[i];
361 else {
362 err_code |= ixheaacd_res_inv_quant(&i, pow_table_q17);
363 }
364 if (out1 < 0) {
365 i = -i;
366 }
367 *x_invquant++ = i;
368 } else {
369 WORD8 temp = *p_pul_arr++;
370 if (out1 <= 0) {
371 out1 = sub_d(temp, out1);
372 out1 = pow_table_q17[out1];
373 *x_invquant++ = -out1;
374 } else {
375 out1 = add_d(out1, temp);
376 out1 = pow_table_q17[out1];
377 *x_invquant++ = out1;
378 }
379 }
380
381 if (sp2 == 16) {
382 i = 4;
383 value = ixheaacd_extu(read_word, bit_pos, 23);
384 value = value | 0xfffffe00;
385 norm_val = ixheaacd_norm32(value);
386
387 i += (norm_val - 22);
388
389 bit_pos += (norm_val - 21);
390
391 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
392 it_bif_buf->ptr_bit_buf_end);
393
394 off = ixheaacd_extu(read_word, bit_pos, 32 - i);
395
396 bit_pos += i;
397
398 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
399 it_bif_buf->ptr_bit_buf_end);
400 value = *p_pul_arr++;
401 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
402 it_bif_buf->ptr_bit_buf_end);
403
404 i = off + ((WORD32)1 << i);
405 i = add_d(i, value);
406 if (i <= IQ_TABLE_SIZE_HALF)
407 i = pow_table_q17[i];
408 else {
409 err_code |= ixheaacd_res_inv_quant(&i, pow_table_q17);
410 }
411
412 if (out2 < 0) {
413 i = -i;
414 }
415 *x_invquant++ = i;
416 } else {
417 WORD8 temp = *p_pul_arr++;
418 if (out2 <= 0) {
419 out2 = sub_d(temp, out2);
420 out2 = pow_table_q17[out2];
421 *x_invquant++ = -out2;
422 } else {
423 out2 = add_d(out2, temp);
424 out2 = pow_table_q17[out2];
425 *x_invquant++ = out2;
426 }
427 }
428 }
429
430 it_bif_buf->ptr_read_next = ptr_read_next - 4;
431 it_bif_buf->bit_pos = bit_pos;
432
433 return err_code;
434 }
435
ixheaacd_res_c_block_decode_huff_word2_4(ia_bit_buf_struct * it_bit_buf,WORD32 * qp,WORD16 * offsets,WORD no_bands,WORD group_no,const UWORD16 * h_ori,WORD32 * pow_table_q17,WORD32 sign,WORD32 maximum_bins_short)436 static PLATFORM_INLINE WORD ixheaacd_res_c_block_decode_huff_word2_4(
437 ia_bit_buf_struct *it_bit_buf, WORD32 *qp, WORD16 *offsets, WORD no_bands, WORD group_no,
438 const UWORD16 *h_ori, WORD32 *pow_table_q17, WORD32 sign, WORD32 maximum_bins_short) {
439 WORD32 value;
440 WORD idx, grp_idx;
441 WORD idx_len;
442 WORD32 *qp_org;
443
444 UWORD8 *ptr_read_next = it_bit_buf->ptr_read_next;
445 WORD32 bit_pos = it_bit_buf->bit_pos;
446 WORD32 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
447 ptr_read_next += 4;
448 qp_org = qp;
449 do {
450 idx_len = offsets[1] - offsets[0];
451 grp_idx = group_no;
452
453 do {
454 qp = qp + offsets[0];
455 idx = idx_len;
456 do {
457 UWORD16 first_offset;
458 WORD16 sign_ret_val;
459 UWORD32 read_word1;
460 UWORD16 *h;
461
462 read_word1 = read_word << bit_pos;
463
464 h = (UWORD16 *)h_ori;
465 h += (read_word1) >> (27);
466 sign_ret_val = *h;
467
468 first_offset = 5;
469 while (sign_ret_val > 0) {
470 bit_pos += first_offset;
471 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
472 it_bit_buf->ptr_bit_buf_end);
473 read_word1 = (read_word1) << (first_offset);
474
475 first_offset = (sign_ret_val >> 11);
476 h += sign_ret_val & (0x07FF);
477
478 h += (read_word1) >> (32 - first_offset);
479 sign_ret_val = *h;
480 }
481 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
482 value = sign_ret_val & (0x07FF);
483
484 if (sign) {
485 WORD32 temp_word;
486 temp_word = read_word << bit_pos;
487
488 *qp++ = ixheaacd_res_extract_signed_symbol(value, 24, 30, pow_table_q17, &temp_word,
489 &bit_pos);
490 *qp++ = ixheaacd_res_extract_signed_symbol(value, 26, 30, pow_table_q17, &temp_word,
491 &bit_pos);
492 *qp++ = ixheaacd_res_extract_signed_symbol(value, 28, 30, pow_table_q17, &temp_word,
493 &bit_pos);
494 *qp++ = ixheaacd_res_extract_signed_symbol(value, 30, 30, pow_table_q17, &temp_word,
495 &bit_pos);
496 } else {
497 *qp++ = ixheaacd_res_extract_symbol(value, 24, 30, pow_table_q17);
498 *qp++ = ixheaacd_res_extract_symbol(value, 26, 30, pow_table_q17);
499 *qp++ = ixheaacd_res_extract_symbol(value, 28, 30, pow_table_q17);
500 *qp++ = ixheaacd_res_extract_symbol(value, 30, 30, pow_table_q17);
501 }
502
503 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
504 it_bit_buf->ptr_bit_buf_end);
505 idx -= 4;
506 } while (idx != 0);
507
508 qp += (maximum_bins_short - offsets[1]);
509 grp_idx--;
510 } while (grp_idx != 0);
511 offsets++;
512 qp = qp_org;
513 no_bands--;
514 } while (no_bands >= 0);
515
516 it_bit_buf->ptr_read_next = ptr_read_next - 4;
517 it_bit_buf->bit_pos = bit_pos;
518
519 return 0;
520 }
521
ixheaacd_res_c_block_decode_huff_word2_4_lb(ia_bit_buf_struct * it_bit_buf,WORD32 len,const UWORD16 * h_ori,WORD32 * x_invquant,WORD32 * pow_table_q17,WORD8 * p_pul_arr,WORD32 sign)522 static PLATFORM_INLINE WORD ixheaacd_res_c_block_decode_huff_word2_4_lb(
523 ia_bit_buf_struct *it_bit_buf, WORD32 len, const UWORD16 *h_ori, WORD32 *x_invquant,
524 WORD32 *pow_table_q17, WORD8 *p_pul_arr, WORD32 sign) {
525 WORD32 value;
526 WORD idx;
527
528 UWORD8 *ptr_read_next = it_bit_buf->ptr_read_next;
529 WORD32 bit_pos = it_bit_buf->bit_pos;
530 WORD32 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
531 ptr_read_next += 4;
532
533 for (idx = len; idx != 0; idx -= 4) {
534 WORD32 res;
535 WORD32 ampres, ampres1;
536 WORD32 ampres2, ampres3;
537 UWORD16 first_offset;
538 WORD16 sign_ret_val;
539 UWORD32 read_word1;
540 UWORD16 *h;
541
542 read_word1 = read_word << bit_pos;
543
544 h = (UWORD16 *)h_ori;
545 h += (read_word1) >> (27);
546 sign_ret_val = *h;
547
548 first_offset = 5;
549 while (sign_ret_val > 0) {
550 bit_pos += first_offset;
551 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
552 it_bit_buf->ptr_bit_buf_end);
553 read_word1 = (read_word1) << (first_offset);
554
555 first_offset = (sign_ret_val >> 11);
556 h += sign_ret_val & (0x07FF);
557
558 h += (read_word1) >> (32 - first_offset);
559 sign_ret_val = *h;
560 }
561 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
562
563 value = sign_ret_val & (0x07FF);
564
565 if (sign) {
566 WORD32 out0, out1, out2, out3;
567 WORD32 ampout0, ampout1, ampout2, ampout3;
568 WORD32 temp_word;
569 temp_word = read_word << bit_pos;
570
571 out0 = (ixheaacd_extu(value, 24, 30));
572 ampout0 = add_d(out0, *p_pul_arr++);
573 ampout0 = pow_table_q17[ampout0];
574
575 if (out0) {
576 if (temp_word & 0x80000000) {
577 ampout0 = -ampout0;
578 }
579 temp_word = temp_word << 1;
580 bit_pos++;
581 } else {
582 ampout0 = -ampout0;
583 }
584
585 out1 = (ixheaacd_extu(value, 26, 30));
586 ampout1 = add_d(out1, *p_pul_arr++);
587 ampout1 = pow_table_q17[ampout1];
588 if (out1) {
589 if (temp_word & 0x80000000) {
590 ampout1 = -(ampout1);
591 }
592 temp_word = temp_word << 1;
593 bit_pos++;
594 } else {
595 ampout1 = -ampout1;
596 }
597 out2 = (ixheaacd_extu(value, 28, 30));
598 ampout2 = add_d(out2, *p_pul_arr++);
599 ampout2 = pow_table_q17[ampout2];
600 if (out2) {
601 if (temp_word & 0x80000000) {
602 ampout2 = -(ampout2);
603 }
604 temp_word = temp_word << 1;
605 bit_pos++;
606 } else {
607 ampout2 = -ampout2;
608 }
609
610 *x_invquant++ = ampout0;
611 *x_invquant++ = ampout1;
612 *x_invquant++ = ampout2;
613
614 out3 = (ixheaacd_extu(value, 30, 30));
615 ampout3 = add_d(out3, *p_pul_arr++);
616 ampout3 = pow_table_q17[ampout3];
617 if (out3) {
618 if (temp_word & 0x80000000) {
619 ampout3 = -(ampout3);
620 }
621 temp_word = temp_word << 1;
622 bit_pos++;
623 } else {
624 ampout3 = -ampout3;
625 }
626
627 *x_invquant++ = ampout3;
628 } else {
629 ampres = *p_pul_arr++;
630 res = (ixheaacd_res_exts(value, 24, 30));
631 if (res > 0) {
632 ampres = add_d(res, ampres);
633 ampres = pow_table_q17[ampres];
634 } else {
635 ampres = sub_d(ampres, res);
636 ampres = pow_table_q17[ampres];
637 ampres = -ampres;
638 }
639 res = (ixheaacd_res_exts(value, 26, 30));
640 ampres1 = *p_pul_arr++;
641 if (res > 0) {
642 ampres1 = add_d(res, ampres1);
643 ampres1 = pow_table_q17[ampres1];
644 } else {
645 ampres1 = sub_d(ampres1, res);
646 ampres1 = pow_table_q17[ampres1];
647 ampres1 = -ampres1;
648 }
649 res = (ixheaacd_res_exts(value, 28, 30));
650 ampres2 = *p_pul_arr++;
651 if (res > 0) {
652 ampres2 = add_d(res, ampres2);
653 ampres2 = pow_table_q17[ampres2];
654 } else {
655 ampres2 = sub_d(ampres2, res);
656 ampres2 = pow_table_q17[ampres2];
657 ampres2 = -ampres2;
658 }
659 res = (ixheaacd_res_exts(value, 30, 30));
660 ampres3 = *p_pul_arr++;
661 if (res > 0) {
662 ampres3 = add_d(res, ampres3);
663 ampres3 = pow_table_q17[ampres3];
664 } else {
665 ampres3 = sub_d(ampres3, res);
666 ampres3 = pow_table_q17[ampres3];
667 ampres3 = -ampres3;
668 }
669 *x_invquant++ = ampres;
670 *x_invquant++ = ampres1;
671 *x_invquant++ = ampres2;
672 *x_invquant++ = ampres3;
673 }
674 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
675 it_bit_buf->ptr_bit_buf_end);
676 }
677
678 it_bit_buf->ptr_read_next = ptr_read_next - 4;
679 it_bit_buf->bit_pos = bit_pos;
680
681 return 0;
682 }
683
ixheaacd_res_c_block_decode_huff_word2_2(ia_bit_buf_struct * it_bif_buf,WORD32 * qp,WORD16 * offsets,WORD no_bands,WORD group_no,const UWORD16 * h_ori,WORD32 * pow_table_q17,WORD32 sign,WORD32 maximum_bins_short)684 static PLATFORM_INLINE WORD ixheaacd_res_c_block_decode_huff_word2_2(
685 ia_bit_buf_struct *it_bif_buf, WORD32 *qp, WORD16 *offsets, WORD no_bands, WORD group_no,
686 const UWORD16 *h_ori, WORD32 *pow_table_q17, WORD32 sign, WORD32 maximum_bins_short)
687
688 {
689 WORD32 value;
690 WORD idx, grp_idx;
691 WORD len_idx;
692
693 WORD32 *qp_org = qp;
694
695 UWORD8 *ptr_read_next = it_bif_buf->ptr_read_next;
696 WORD32 bit_pos = it_bif_buf->bit_pos;
697 WORD32 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
698 ptr_read_next += 4;
699
700 do {
701 len_idx = offsets[1] - offsets[0];
702 grp_idx = group_no;
703 do {
704 qp += offsets[0];
705 idx = len_idx;
706 do {
707 UWORD16 first_offset;
708 WORD16 sign_ret_val;
709 UWORD32 read_word1;
710 UWORD16 *h;
711
712 read_word1 = read_word << bit_pos;
713
714 h = (UWORD16 *)h_ori;
715 h += (read_word1) >> (27);
716 sign_ret_val = *h;
717
718 first_offset = 5;
719 while (sign_ret_val > 0) {
720 bit_pos += first_offset;
721 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
722 it_bif_buf->ptr_bit_buf_end);
723 read_word1 = (read_word1) << (first_offset);
724
725 first_offset = (sign_ret_val >> 11);
726 h += sign_ret_val & (0x07FF);
727
728 h += (read_word1) >> (32 - first_offset);
729 sign_ret_val = *h;
730 }
731 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
732 value = sign_ret_val & (0x07FF);
733
734 if (sign) {
735 WORD32 temp_word;
736 temp_word = read_word << bit_pos;
737
738 *qp++ = ixheaacd_res_extract_signed_symbol(value, 24, 28, pow_table_q17, &temp_word,
739 &bit_pos);
740 *qp++ = ixheaacd_res_extract_signed_symbol(value, 28, 28, pow_table_q17, &temp_word,
741 &bit_pos);
742 } else {
743 *qp++ = ixheaacd_res_extract_symbol(value, 24, 28, pow_table_q17);
744 *qp++ = ixheaacd_res_extract_symbol(value, 28, 28, pow_table_q17);
745 }
746
747 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
748 it_bif_buf->ptr_bit_buf_end);
749 idx -= 2;
750 } while (idx != 0);
751
752 qp += (maximum_bins_short - offsets[1]);
753 grp_idx--;
754 } while (grp_idx != 0);
755
756 offsets++;
757 qp = qp_org;
758 no_bands--;
759 } while (no_bands >= 0);
760
761 it_bif_buf->ptr_read_next = ptr_read_next - 4;
762 it_bif_buf->bit_pos = bit_pos;
763
764 return 0;
765 }
766
ixheaacd_res_c_block_decode_huff_word2_2_lb(ia_bit_buf_struct * it_bit_buf,WORD32 len,const UWORD16 * h_ori,WORD32 * x_invquant,WORD32 * pow_table_q17,WORD8 * p_pul_arr,WORD32 sign)767 static PLATFORM_INLINE WORD ixheaacd_res_c_block_decode_huff_word2_2_lb(
768 ia_bit_buf_struct *it_bit_buf, WORD32 len, const UWORD16 *h_ori, WORD32 *x_invquant,
769 WORD32 *pow_table_q17, WORD8 *p_pul_arr, WORD32 sign) {
770 WORD32 value, res, ampres;
771 WORD idx;
772
773 UWORD8 *ptr_read_next = it_bit_buf->ptr_read_next;
774 WORD32 bit_pos = it_bit_buf->bit_pos;
775 WORD32 read_word = ixheaacd_res_aac_showbits_32(ptr_read_next);
776 ptr_read_next += 4;
777
778 for (idx = len; idx != 0; idx -= 2) {
779 {
780 UWORD16 first_offset;
781 WORD16 sign_ret_val;
782 UWORD32 read_word1;
783 UWORD16 *h;
784
785 read_word1 = read_word << bit_pos;
786
787 h = (UWORD16 *)h_ori;
788 h += (read_word1) >> (27);
789 sign_ret_val = *h;
790
791 first_offset = 5;
792 while (sign_ret_val > 0) {
793 bit_pos += first_offset;
794 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
795 it_bit_buf->ptr_bit_buf_end);
796 read_word1 = (read_word1) << (first_offset);
797
798 first_offset = (sign_ret_val >> 11);
799 h += sign_ret_val & (0x07FF);
800
801 h += (read_word1) >> (32 - first_offset);
802 sign_ret_val = *h;
803 }
804 bit_pos += ((sign_ret_val & 0x7fff) >> 11);
805
806 value = sign_ret_val & (0x07FF);
807 }
808
809 if (sign) {
810 WORD32 out0, out1, temp_word;
811 WORD32 ampout0, ampout1;
812
813 ampout0 = *p_pul_arr++;
814 ampout1 = *p_pul_arr++;
815
816 out0 = value & 0xf0;
817
818 ampout0 = add_d(ampout0, (UWORD32)out0 >> 4);
819 ampout0 = pow_table_q17[ampout0];
820
821 out1 = value & 0xf;
822 ampout1 = add_d(out1, ampout1);
823 ampout1 = pow_table_q17[ampout1];
824
825 temp_word = read_word << bit_pos;
826 if (out0) {
827 if (temp_word & 0x80000000) {
828 ampout0 = -(ampout0);
829 }
830 bit_pos++;
831 temp_word = temp_word << 1;
832 } else {
833 ampout0 = -(ampout0);
834 }
835 if (out1) {
836 if (temp_word & 0x80000000) {
837 ampout1 = -(ampout1);
838 }
839 bit_pos++;
840 } else {
841 ampout1 = -(ampout1);
842 }
843 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
844 it_bit_buf->ptr_bit_buf_end);
845 *x_invquant++ = ampout0;
846 *x_invquant++ = ampout1;
847 } else {
848 res = ((value << 24) >> 28);
849 ampres = *p_pul_arr++;
850 if (res > 0) {
851 ampres = add_d(res, ampres);
852 *x_invquant++ = pow_table_q17[ampres];
853 } else {
854 ampres = sub_d(ampres, res);
855 ampres = pow_table_q17[ampres];
856 *x_invquant++ = -ampres;
857 }
858
859 res = ((value << 28) >> 28);
860 value = *p_pul_arr++;
861 if (res > 0) {
862 ampres = add_d(res, value);
863 *x_invquant++ = pow_table_q17[ampres];
864 } else {
865 ampres = sub_d(value, res);
866 ampres = pow_table_q17[ampres];
867 *x_invquant++ = -ampres;
868 }
869 }
870 ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
871 it_bit_buf->ptr_bit_buf_end);
872 }
873 it_bit_buf->ptr_read_next = ptr_read_next - 4;
874 it_bit_buf->bit_pos = bit_pos;
875
876 return 0;
877 }
878
ixheaacd_res_c_block_decode_huff_word_all(ia_bit_buf_struct * it_bit_buf,WORD32 code_no,WORD32 * quantized_coef,WORD16 * band_offsets,WORD start,WORD band,WORD group_no,ia_mps_dec_residual_aac_tables_struct * aac_tables_ptr,WORD32 maximum_bins_short)879 WORD ixheaacd_res_c_block_decode_huff_word_all(
880 ia_bit_buf_struct *it_bit_buf, WORD32 code_no, WORD32 *quantized_coef, WORD16 *band_offsets,
881 WORD start, WORD band, WORD group_no, ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr,
882 WORD32 maximum_bins_short) {
883 WORD ret_val = 0;
884 WORD start_bit_pos = it_bit_buf->bit_pos;
885 UWORD8 *start_read_pos = it_bit_buf->ptr_read_next;
886 const UWORD16 *h_ori = (UWORD16 *)(aac_tables_ptr->code_book[code_no]);
887 WORD32 *pow_table = (WORD32 *)aac_tables_ptr->res_block_tables_ptr->pow_table_q17;
888 WORD32 no_bands = band - start - 1;
889 WORD16 *p_band_off = band_offsets + start;
890
891 if (code_no == 11) {
892 const UWORD16 *h_ori = aac_tables_ptr->res_huffmann_tables_ptr->huffman_codebook_11;
893 ret_val =
894 ixheaacd_res_c_block_decode_huff_word1(it_bit_buf, quantized_coef, p_band_off, no_bands,
895 group_no, h_ori, pow_table, maximum_bins_short);
896 } else if (code_no <= 4) {
897 WORD32 sign = 0;
898
899 if (code_no > 2) sign = 1;
900 ret_val = ixheaacd_res_c_block_decode_huff_word2_4(it_bit_buf, quantized_coef, p_band_off,
901 no_bands, group_no, h_ori, pow_table, sign,
902 maximum_bins_short);
903 }
904
905 else if (code_no <= 10) {
906 WORD32 sign = 0;
907
908 if (code_no > 6) sign = 1;
909 ret_val = ixheaacd_res_c_block_decode_huff_word2_2(it_bit_buf, quantized_coef, p_band_off,
910 no_bands, group_no, h_ori, pow_table, sign,
911 maximum_bins_short);
912 }
913 {
914 WORD bits_cons;
915 bits_cons = (WORD)(((it_bit_buf->ptr_read_next - start_read_pos) << 3) +
916 (it_bit_buf->bit_pos - start_bit_pos));
917 it_bit_buf->cnt_bits -= bits_cons;
918 }
919 return ret_val;
920 }
921
ixheaacd_res_c_block_decode_huff_word_all_lb(ia_bit_buf_struct * it_bit_buf,WORD32 code_no,WORD32 len,ia_mps_dec_residual_aac_tables_struct * aac_tables_ptr,WORD32 * x_invquant,WORD8 * p_pul_arr)922 WORD ixheaacd_res_c_block_decode_huff_word_all_lb(
923 ia_bit_buf_struct *it_bit_buf, WORD32 code_no, WORD32 len,
924 ia_mps_dec_residual_aac_tables_struct *aac_tables_ptr, WORD32 *x_invquant, WORD8 *p_pul_arr) {
925 WORD ret_val = 0;
926 WORD start_bit_pos = it_bit_buf->bit_pos;
927 WORD32 *pow_table = (WORD32 *)aac_tables_ptr->res_block_tables_ptr->pow_table_q17;
928 UWORD8 *start_read_pos = it_bit_buf->ptr_read_next;
929
930 const UWORD16 *h_ori = (UWORD16 *)(aac_tables_ptr->code_book[code_no]);
931
932 if (code_no == 11) {
933 const UWORD16 *h_ori = aac_tables_ptr->res_huffmann_tables_ptr->huffman_codebook_11;
934 ret_val = ixheaacd_res_c_block_decode_huff_word1_lb(it_bit_buf, len, h_ori, x_invquant,
935 pow_table, p_pul_arr);
936 } else if (code_no <= 4) {
937 WORD32 sign = 0;
938 if (code_no > 2) sign = 1;
939 ret_val = ixheaacd_res_c_block_decode_huff_word2_4_lb(it_bit_buf, len, h_ori, x_invquant,
940 pow_table, p_pul_arr, sign);
941 } else if (code_no <= 10) {
942 WORD32 sign = 0;
943 if (code_no > 6) sign = 1;
944 ret_val = ixheaacd_res_c_block_decode_huff_word2_2_lb(it_bit_buf, len, h_ori, x_invquant,
945 pow_table, p_pul_arr, sign);
946 }
947
948 {
949 WORD bits_cons;
950 if (it_bit_buf->bit_pos <= 7) {
951 bits_cons = (WORD)(((it_bit_buf->ptr_read_next - start_read_pos) << 3) +
952 (it_bit_buf->bit_pos - start_bit_pos));
953 it_bit_buf->cnt_bits -= bits_cons;
954 } else {
955 it_bit_buf->ptr_read_next += (it_bit_buf->bit_pos) >> 3;
956 it_bit_buf->bit_pos = it_bit_buf->bit_pos & 0x7;
957
958 bits_cons = (WORD)(((it_bit_buf->ptr_read_next - start_read_pos) << 3) +
959 ((it_bit_buf->bit_pos - start_bit_pos)));
960 it_bit_buf->cnt_bits -= bits_cons;
961 }
962 }
963 return ret_val;
964 }
965
ixheaacd_res_apply_one_scf(WORD32 scale_factor,WORD32 * x_invquant,WORD32 end,WORD32 * scale_table_ptr)966 static VOID ixheaacd_res_apply_one_scf(WORD32 scale_factor, WORD32 *x_invquant, WORD32 end,
967 WORD32 *scale_table_ptr) {
968 WORD32 j;
969
970 WORD32 temp_1;
971 WORD32 q_factor;
972 WORD32 buffer1;
973 WORD16 scale_short;
974
975 if (scale_factor < 24) {
976 for (j = end; j > 0; j--) {
977 *x_invquant++ = 0;
978 }
979 } else {
980 WORD32 shift;
981 q_factor = 37 - (scale_factor >> 2);
982
983 scale_short = scale_table_ptr[(scale_factor & 0x0003)];
984
985 shift = q_factor;
986
987 if (shift > 0) {
988 if (scale_short == (WORD16)0x8000) {
989 for (j = end; j > 0; j--) {
990 temp_1 = *x_invquant;
991
992 buffer1 = ixheaacd_mult32x16in32_shl_sat(temp_1, scale_short);
993 buffer1 = ixheaacd_shr32(buffer1, shift);
994 *x_invquant++ = buffer1;
995 }
996 } else {
997 for (j = end; j > 0; j--) {
998 temp_1 = *x_invquant;
999
1000 buffer1 = ixheaacd_mult32x16in32_shl(temp_1, scale_short);
1001
1002 buffer1 = ixheaacd_shr32(buffer1, shift);
1003 *x_invquant++ = buffer1;
1004 }
1005 }
1006 } else {
1007 shift = -shift;
1008 if (shift > 0) {
1009 if (scale_short == (WORD16)0x8000) {
1010 for (j = end; j > 0; j--) {
1011 temp_1 = *x_invquant;
1012 temp_1 = ixheaacd_shl32(temp_1, shift - 1);
1013
1014 buffer1 = ixheaacd_mult32x16in32_shl_sat(temp_1, scale_short);
1015
1016 buffer1 = ixheaacd_shl32(buffer1, 1);
1017 *x_invquant++ = buffer1;
1018 }
1019 } else {
1020 for (j = end; j > 0; j--) {
1021 temp_1 = *x_invquant;
1022 temp_1 = ixheaacd_shl32(temp_1, shift - 1);
1023
1024 buffer1 = ixheaacd_mult32x16in32_shl(temp_1, scale_short);
1025
1026 buffer1 = ixheaacd_shl32(buffer1, 1);
1027 *x_invquant++ = buffer1;
1028 }
1029 }
1030 } else {
1031 if (scale_short == (WORD16)0x8000) {
1032 for (j = end; j > 0; j--) {
1033 temp_1 = *x_invquant;
1034
1035 buffer1 = ixheaacd_mult32x16in32_shl_sat(temp_1, scale_short);
1036
1037 *x_invquant++ = buffer1;
1038 }
1039 } else {
1040 for (j = end; j > 0; j--) {
1041 temp_1 = *x_invquant;
1042
1043 buffer1 = ixheaacd_mult32x16in32_shl(temp_1, scale_short);
1044
1045 *x_invquant++ = buffer1;
1046 }
1047 }
1048 }
1049 }
1050 }
1051 }
1052
ixheaacd_res_apply_scfs(WORD32 * x_invquant,WORD16 * sc_factor,WORD t_bands,WORD8 * offset,WORD32 * scale_table_ptr)1053 VOID ixheaacd_res_apply_scfs(WORD32 *x_invquant, WORD16 *sc_factor, WORD t_bands, WORD8 *offset,
1054 WORD32 *scale_table_ptr) {
1055 WORD32 i;
1056 WORD16 scale_factor;
1057
1058 for (i = t_bands - 1; i >= 0; i--) {
1059 scale_factor = *sc_factor++;
1060 ixheaacd_res_apply_one_scf(scale_factor, x_invquant, *offset, scale_table_ptr);
1061 x_invquant += *offset;
1062 offset++;
1063 }
1064 }
1065