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