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 "ixheaacd_sbr_common.h"
21 #include "ixheaacd_type_def.h"
22 
23 #include "ixheaacd_constants.h"
24 #include "ixheaacd_basic_ops32.h"
25 #include "ixheaacd_basic_ops16.h"
26 #include "ixheaacd_basic_ops40.h"
27 #include "ixheaacd_basic_ops.h"
28 #include "ixheaacd_common_rom.h"
29 #include "ixheaacd_basic_funcs.h"
30 #include "ixheaacd_defines.h"
31 #include "ixheaacd_aac_rom.h"
32 #include "ixheaacd_bitbuffer.h"
33 #include "ixheaacd_intrinsics.h"
34 #include "ixheaacd_pulsedata.h"
35 
36 #include "ixheaacd_pns.h"
37 #include "ixheaacd_drc_data_struct.h"
38 
39 #include "ixheaacd_lt_predict.h"
40 
41 #include "ixheaacd_channelinfo.h"
42 #include "ixheaacd_drc_dec.h"
43 
44 #include "ixheaacd_block.h"
45 #include "ixheaacd_channel.h"
46 
47 #include "ixheaacd_basic_op.h"
48 
49 #include "ixheaacd_tns.h"
50 #include "ixheaacd_sbrdecoder.h"
51 #include "ixheaacd_error_codes.h"
52 
53 #include "ixheaacd_audioobjtypes.h"
54 #include "ixheaacd_latmdemux.h"
55 
56 #include "ixheaacd_aacdec.h"
57 
ixheaacd_shr32_drc(WORD32 a,WORD32 b)58 static PLATFORM_INLINE WORD32 ixheaacd_shr32_drc(WORD32 a, WORD32 b) {
59   WORD32 out_val;
60 
61   b = ((UWORD32)(b << 24) >> 24);
62   if (b == 0) {
63     out_val = a;
64   } else if (b >= 31) {
65     if (a < 0)
66       out_val = -1;
67     else
68       out_val = 0;
69   } else {
70     a += (1 << (b - 1));
71     out_val = (WORD32)a >> b;
72   }
73 
74   return out_val;
75 }
76 
ixheaacd_mult32x16in32_drc(WORD32 a,WORD16 b)77 static PLATFORM_INLINE WORD32 ixheaacd_mult32x16in32_drc(WORD32 a, WORD16 b) {
78   WORD32 result;
79   WORD64 temp_result;
80 
81   temp_result = (WORD64)a * (WORD64)b;
82 
83   if (temp_result < (WORD64)MIN_32)
84     result = MIN_32;
85 
86   else if (temp_result > (WORD64)MAX_32)
87     result = MAX_32;
88 
89   else
90     result = (WORD32)(temp_result);
91 
92   return (result);
93 }
ixheaacd_mac32x16in32_drc(WORD32 a,WORD32 b,WORD16 c)94 static PLATFORM_INLINE WORD32 ixheaacd_mac32x16in32_drc(WORD32 a, WORD32 b,
95                                                         WORD16 c) {
96   WORD32 acc;
97 
98   acc = ixheaacd_mult32x16in32_drc(b, c);
99 
100   acc = ixheaacd_add32_sat(a, acc);
101 
102   return acc;
103 }
104 
105 WORD32 ixheaacd_cnt_leading_ones(WORD32 a);
106 
ixheaacd_huff_sfb_table(WORD32 it_bit_buff,WORD16 * huff_index,WORD32 * len,const UWORD16 * code_book_tbl,const UWORD32 * idx_table)107 VOID ixheaacd_huff_sfb_table(WORD32 it_bit_buff, WORD16 *huff_index,
108                              WORD32 *len, const UWORD16 *code_book_tbl,
109                              const UWORD32 *idx_table) {
110   UWORD32 temp = 0;
111   UWORD32 temp1 = 0;
112   WORD32 found = 0;
113   UWORD32 mask = 0x80000000;
114 
115   WORD32 leading_ones;
116   WORD32 max_len;
117   WORD32 ixheaacd_drc_offset = 0;
118   WORD32 length;
119   UWORD32 code_word;
120   WORD32 len_end;
121 
122   max_len = code_book_tbl[0];
123   mask = mask - (1 << (31 - max_len));
124   mask = mask << 1;
125 
126   temp = (UWORD32)((it_bit_buff & mask));
127 
128   len_end = code_book_tbl[0];
129   leading_ones = ixheaacd_cnt_leading_ones(temp);
130   do {
131     ixheaacd_drc_offset = (idx_table[leading_ones] >> 20) & 0x1ff;
132     length = code_book_tbl[ixheaacd_drc_offset + 1] & 0x1f;
133     code_word = idx_table[leading_ones] & 0xfffff;
134     temp1 = temp >> (32 - length);
135     if (temp1 <= code_word) {
136       ixheaacd_drc_offset = ixheaacd_drc_offset - (code_word - temp1);
137       found = 1;
138     } else {
139       len_end = len_end + ((idx_table[leading_ones] >> 29) & 0x7);
140       leading_ones = len_end;
141     }
142   } while (!found);
143   *huff_index = code_book_tbl[ixheaacd_drc_offset + 1] >> 5;
144   *len = length;
145 }
146 
ixheaacd_inverse_quantize(WORD32 * x_invquant,WORD no_band,WORD32 * ixheaacd_pow_table_Q13,WORD8 * scratch_in)147 VOID ixheaacd_inverse_quantize(WORD32 *x_invquant, WORD no_band,
148                                WORD32 *ixheaacd_pow_table_Q13,
149                                WORD8 *scratch_in) {
150   WORD32 j;
151   WORD32 temp;
152   WORD32 q_abs;
153 
154   for (j = no_band - 1; j >= 0; j--) {
155     q_abs = *scratch_in++;
156     temp = (ixheaacd_pow_table_Q13[q_abs]);
157     *x_invquant++ = -temp;
158   }
159 }
160 
ixheaacd_huffman_dec_word1(ia_bit_buf_struct * it_bit_buff,WORD32 * spec_coef,WORD16 * offsets,WORD no_bands,WORD group_len,const UWORD16 * code_book_tbl,WORD32 * ixheaacd_pow_table_Q13,const UWORD32 * idx_table)161 static PLATFORM_INLINE WORD ixheaacd_huffman_dec_word1(
162     ia_bit_buf_struct *it_bit_buff, WORD32 *spec_coef, WORD16 *offsets,
163     WORD no_bands, WORD group_len, const UWORD16 *code_book_tbl,
164     WORD32 *ixheaacd_pow_table_Q13, const UWORD32 *idx_table) {
165   WORD32 sp1, sp2;
166   WORD32 flush_cw;
167   WORD32 i, value, norm_val, off;
168   WORD idx, grp_idx;
169   WORD32 out1, out2;
170   WORD32 err_code = 0;
171   WORD len_idx = 0;
172   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
173   WORD32 bit_pos = it_bit_buff->bit_pos;
174   WORD16 index;
175   WORD32 length;
176   WORD32 read_word;
177   WORD32 increment;
178 
179   read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
180                                        &increment);
181   ptr_read_next += increment;
182 
183   do {
184     len_idx = offsets[1] - offsets[0];
185     grp_idx = group_len;
186 
187     do {
188       spec_coef = spec_coef + offsets[0];
189       idx = len_idx;
190       do {
191         {
192           UWORD32 read_word1;
193 
194           read_word1 = read_word << bit_pos;
195           ixheaacd_huff_sfb_table(read_word1, &index, &length, code_book_tbl,
196                                   idx_table);
197           bit_pos += length;
198           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
199                                       it_bit_buff->ptr_bit_buf_end);
200         }
201 
202         out1 = index / 17;
203         out2 = index - out1 * 17;
204         flush_cw = read_word << bit_pos;
205 
206         sp1 = out1;
207         sp2 = out2;
208 
209         if (out1) {
210           if (flush_cw & 0x80000000) {
211             out1 = -out1;
212           }
213           bit_pos++;
214           flush_cw = (WORD32)flush_cw << 1;
215         }
216 
217         if (out2) {
218           bit_pos++;
219           if (flush_cw & 0x80000000) {
220             out2 = -out2;
221           }
222         }
223         ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
224                                     it_bit_buff->ptr_bit_buf_end);
225 
226         if (sp1 == 16) {
227           i = 4;
228           value = ixheaacd_extu(read_word, bit_pos, 23);
229           value = value | 0xfffffe00;
230           norm_val = ixheaacd_norm32(value);
231 
232           i += (norm_val - 22);
233           bit_pos += (norm_val - 21);
234           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
235                                       it_bit_buff->ptr_bit_buf_end);
236 
237           off = ixheaacd_extu(read_word, bit_pos, 32 - i);
238 
239           bit_pos += i;
240 
241           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
242                                       it_bit_buff->ptr_bit_buf_end);
243           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
244                                       it_bit_buff->ptr_bit_buf_end);
245 
246           i = off + ((WORD32)1 << i);
247 
248           if (i <= IQ_TABLE_SIZE_HALF)
249             i = ixheaacd_pow_table_Q13[i];
250           else {
251             err_code |= ixheaacd_inv_quant(&i, ixheaacd_pow_table_Q13);
252           }
253 
254           if (out1 < 0) {
255             out1 = -i;
256           } else {
257             out1 = i;
258           }
259           *spec_coef++ = out1;
260         } else {
261           if (out1 <= 0) {
262             out1 = -out1;
263             out1 = ixheaacd_pow_table_Q13[out1];
264             *spec_coef++ = -out1;
265           } else {
266             out1 = ixheaacd_pow_table_Q13[out1];
267             *spec_coef++ = out1;
268           }
269         }
270 
271         if (sp2 == 16) {
272           i = 4;
273           value = ixheaacd_extu(read_word, bit_pos, 23);
274           value = value | 0xfffffe00;
275           norm_val = ixheaacd_norm32(value);
276 
277           i += (norm_val - 22);
278 
279           bit_pos += (norm_val - 21);
280           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
281                                       it_bit_buff->ptr_bit_buf_end);
282 
283           off = ixheaacd_extu(read_word, bit_pos, 32 - i);
284 
285           bit_pos += i;
286 
287           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
288                                       it_bit_buff->ptr_bit_buf_end);
289           ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
290                                       it_bit_buff->ptr_bit_buf_end);
291 
292           i = off + ((WORD32)1 << i);
293 
294           if (i <= IQ_TABLE_SIZE_HALF)
295             i = ixheaacd_pow_table_Q13[i];
296           else {
297             err_code |= ixheaacd_inv_quant(&i, ixheaacd_pow_table_Q13);
298           }
299 
300           if (out2 < 0) {
301             out2 = -i;
302           } else {
303             out2 = i;
304           }
305           *spec_coef++ = out2;
306         } else {
307           if (out2 <= 0) {
308             out2 = -out2;
309             out2 = ixheaacd_pow_table_Q13[out2];
310             *spec_coef++ = -out2;
311           } else {
312             out2 = ixheaacd_pow_table_Q13[out2];
313             *spec_coef++ = out2;
314           }
315         }
316 
317         idx -= 2;
318       } while (idx != 0);
319 
320       spec_coef += (MAX_BINS_SHORT - offsets[1]);
321       grp_idx--;
322     } while (grp_idx != 0);
323 
324     offsets++;
325     spec_coef -= (MAX_BINS_SHORT * group_len);
326     no_bands--;
327   } while (no_bands >= 0);
328 
329   ptr_read_next = ptr_read_next - increment;
330   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
331                                it_bit_buff->ptr_bit_buf_end);
332 
333   it_bit_buff->bit_pos = bit_pos;
334   it_bit_buff->ptr_read_next = ptr_read_next;
335 
336   return err_code;
337 }
338 
ixheaacd_huffman_dec_word2_11(ia_bit_buf_struct * it_bit_buff,WORD32 width,const UWORD16 * code_book_tbl,WORD32 * x_invquant,WORD32 * ixheaacd_pow_table_Q13,WORD8 * ptr_scratch,const UWORD32 * idx_table)339 static PLATFORM_INLINE WORD ixheaacd_huffman_dec_word2_11(
340     ia_bit_buf_struct *it_bit_buff, WORD32 width, const UWORD16 *code_book_tbl,
341     WORD32 *x_invquant, WORD32 *ixheaacd_pow_table_Q13, WORD8 *ptr_scratch,
342     const UWORD32 *idx_table) {
343   WORD32 sp1, sp2;
344   WORD32 flush_cw;
345   WORD32 i, value, norm_val, off;
346   WORD idx;
347   WORD32 out1, out2;
348   WORD32 err_code = 0;
349   WORD16 index;
350   WORD32 length;
351   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
352   WORD32 bit_pos = it_bit_buff->bit_pos;
353   WORD32 read_word;
354   WORD32 increment;
355 
356   read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
357                                        &increment);
358   ptr_read_next += increment;
359 
360   for (idx = width; idx != 0; idx -= 2) {
361     {
362       UWORD32 read_word1;
363 
364       read_word1 = read_word << bit_pos;
365       ixheaacd_huff_sfb_table(read_word1, &index, &length, code_book_tbl,
366                               idx_table);
367       bit_pos += length;
368       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
369                                   it_bit_buff->ptr_bit_buf_end);
370     }
371 
372     flush_cw = read_word << bit_pos;
373     out1 = index / 17;
374     out2 = index - out1 * 17;
375     sp1 = out1;
376 
377     if (out1) {
378       if (flush_cw & 0x80000000) {
379         out1 = -out1;
380       }
381 
382       bit_pos++;
383       flush_cw = (WORD32)flush_cw << 1;
384     }
385 
386     sp2 = out2;
387     if (out2) {
388       bit_pos++;
389       if (flush_cw & 0x80000000) {
390         out2 = -out2;
391       }
392     }
393 
394     ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
395                                 it_bit_buff->ptr_bit_buf_end);
396 
397     if (sp1 == 16) {
398       i = 4;
399       value = ixheaacd_extu(read_word, bit_pos, 23);
400       value = value | 0xfffffe00;
401       norm_val = ixheaacd_norm32(value);
402       i += (norm_val - 22);
403       bit_pos += (norm_val - 21);
404 
405       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
406                                   it_bit_buff->ptr_bit_buf_end);
407 
408       off = ixheaacd_extu(read_word, bit_pos, 32 - i);
409 
410       bit_pos += i;
411       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
412                                   it_bit_buff->ptr_bit_buf_end);
413 
414       value = *ptr_scratch++;
415 
416       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
417                                   it_bit_buff->ptr_bit_buf_end);
418       i = off + ((WORD32)1 << i);
419       i += value;
420 
421       if (i <= IQ_TABLE_SIZE_HALF)
422         i = ixheaacd_pow_table_Q13[i];
423       else {
424         err_code |= ixheaacd_inv_quant(&i, ixheaacd_pow_table_Q13);
425       }
426       if (out1 < 0) {
427         i = -i;
428       }
429       *x_invquant++ = i;
430     } else {
431       WORD8 temp = *ptr_scratch++;
432       if (out1 <= 0) {
433         out1 = temp - out1;
434         out1 = ixheaacd_pow_table_Q13[out1];
435         *x_invquant++ = -out1;
436       } else {
437         out1 += temp;
438         out1 = ixheaacd_pow_table_Q13[out1];
439         *x_invquant++ = out1;
440       }
441     }
442 
443     if (sp2 == 16) {
444       i = 4;
445       value = ixheaacd_extu(read_word, bit_pos, 23);
446       value = value | 0xfffffe00;
447       norm_val = ixheaacd_norm32(value);
448 
449       i += (norm_val - 22);
450 
451       bit_pos += (norm_val - 21);
452       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
453                                   it_bit_buff->ptr_bit_buf_end);
454 
455       off = ixheaacd_extu(read_word, bit_pos, 32 - i);
456 
457       bit_pos += i;
458       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
459                                   it_bit_buff->ptr_bit_buf_end);
460       value = *ptr_scratch++;
461       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
462                                   it_bit_buff->ptr_bit_buf_end);
463 
464       i = off + ((WORD32)1 << i);
465       i += value;
466       if (i <= IQ_TABLE_SIZE_HALF)
467         i = ixheaacd_pow_table_Q13[i];
468       else {
469         err_code |= ixheaacd_inv_quant(&i, ixheaacd_pow_table_Q13);
470       }
471 
472       if (out2 < 0) {
473         i = -i;
474       }
475       *x_invquant++ = i;
476 
477     } else {
478       WORD8 temp = *ptr_scratch++;
479       if (out2 <= 0) {
480         out2 = temp - out2;
481         out2 = ixheaacd_pow_table_Q13[out2];
482         *x_invquant++ = -out2;
483       } else {
484         out2 += temp;
485         out2 = ixheaacd_pow_table_Q13[out2];
486         *x_invquant++ = out2;
487       }
488     }
489   }
490   ptr_read_next = ptr_read_next - increment;
491   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
492                                it_bit_buff->ptr_bit_buf_end);
493 
494   it_bit_buff->ptr_read_next = ptr_read_next;
495   it_bit_buff->bit_pos = bit_pos;
496 
497   return err_code;
498 }
499 
ixheaacd_huffman_dec_quad(ia_bit_buf_struct * it_bit_buff,WORD32 * spec_coef,WORD16 * offsets,WORD no_bands,WORD group_len,const UWORD16 * code_book_tbl,WORD32 * ixheaacd_pow_table_Q13,WORD32 tbl_sign,const UWORD32 * idx_table)500 static PLATFORM_INLINE WORD ixheaacd_huffman_dec_quad(
501     ia_bit_buf_struct *it_bit_buff, WORD32 *spec_coef, WORD16 *offsets,
502     WORD no_bands, WORD group_len, const UWORD16 *code_book_tbl,
503     WORD32 *ixheaacd_pow_table_Q13, WORD32 tbl_sign, const UWORD32 *idx_table) {
504   WORD idx, grp_idx;
505   WORD idx_len;
506   WORD32 *spec_orig;
507   WORD16 index, length;
508   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
509   WORD32 bit_pos = it_bit_buff->bit_pos;
510   WORD32 read_word;
511   WORD32 increment;
512 
513   read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
514                                        &increment);
515   ptr_read_next += increment;
516   spec_orig = spec_coef;
517   do {
518     idx_len = offsets[1] - offsets[0];
519     grp_idx = group_len;
520 
521     do {
522       spec_coef = spec_coef + offsets[0];
523       idx = idx_len;
524       do {
525         UWORD32 read_word1;
526 
527         read_word1 = read_word << bit_pos;
528         ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
529                                 idx_table);
530         bit_pos += length;
531         ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
532                                     it_bit_buff->ptr_bit_buf_end);
533         if (tbl_sign) {
534           WORD32 temp_word;
535           WORD32 w, x, y, z;
536           temp_word = read_word << bit_pos;
537           w = index / 27;
538           index = index - w * 27;
539           x = index / 9;
540           index = index - x * 9;
541           y = index / 3;
542           z = index - y * 3;
543           if (w) {
544             w = ixheaacd_pow_table_Q13[w];
545             if (temp_word & 0x80000000) w = -w;
546             temp_word <<= 1;
547             bit_pos++;
548           }
549           *spec_coef++ = w;
550 
551           if (x) {
552             x = ixheaacd_pow_table_Q13[x];
553             if (temp_word & 0x80000000) x = -x;
554             temp_word <<= 1;
555             bit_pos++;
556           }
557           *spec_coef++ = x;
558           if (y) {
559             y = ixheaacd_pow_table_Q13[y];
560             if (temp_word & 0x80000000) y = -y;
561             temp_word <<= 1;
562             bit_pos++;
563           }
564           *spec_coef++ = y;
565           if (z) {
566             z = ixheaacd_pow_table_Q13[z];
567             if (temp_word & 0x80000000) z = -z;
568             temp_word <<= 1;
569             bit_pos++;
570           }
571           *spec_coef++ = z;
572 
573         }
574 
575         else {
576           WORD32 w, x, y, z;
577 
578           w = index / 27 - 1;
579           index = index - (w + 1) * 27;
580           x = index / 9 - 1;
581           index = index - (x + 1) * 9;
582           y = index / 3 - 1;
583           z = index - ((y + 1) * 3) - 1;
584           if (w < 0) {
585             w = -w;
586             w = ixheaacd_pow_table_Q13[w];
587             w = -w;
588           } else
589             w = ixheaacd_pow_table_Q13[w];
590 
591           *spec_coef++ = w;
592 
593           if (x < 0) {
594             x = -x;
595             x = ixheaacd_pow_table_Q13[x];
596             x = -x;
597           } else
598             x = ixheaacd_pow_table_Q13[x];
599 
600           *spec_coef++ = x;
601 
602           if (y < 0) {
603             y = -y;
604             y = ixheaacd_pow_table_Q13[y];
605             y = -y;
606           } else
607             y = ixheaacd_pow_table_Q13[y];
608 
609           *spec_coef++ = y;
610 
611           if (z < 0) {
612             z = -z;
613             z = ixheaacd_pow_table_Q13[z];
614             z = -z;
615           } else
616             z = ixheaacd_pow_table_Q13[z];
617 
618           *spec_coef++ = z;
619         }
620 
621         ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
622                                     it_bit_buff->ptr_bit_buf_end);
623         idx -= 4;
624       } while (idx != 0);
625 
626       spec_coef += (MAX_BINS_SHORT - offsets[1]);
627       grp_idx--;
628     } while (grp_idx != 0);
629     offsets++;
630     spec_coef = spec_orig;
631     no_bands--;
632   } while (no_bands >= 0);
633 
634   ptr_read_next = ptr_read_next - increment;
635   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
636                                it_bit_buff->ptr_bit_buf_end);
637   it_bit_buff->ptr_read_next = ptr_read_next;
638   it_bit_buff->bit_pos = bit_pos;
639 
640   return 0;
641 }
642 
ixheaacd_huffman_dec_word2_quad(ia_bit_buf_struct * it_bit_buff,WORD32 width,const UWORD16 * code_book_tbl,WORD32 * x_invquant,WORD32 * ixheaacd_pow_table_Q13,WORD8 * ptr_scratch,WORD32 tbl_sign,const UWORD32 * idx_table)643 static PLATFORM_INLINE WORD ixheaacd_huffman_dec_word2_quad(
644     ia_bit_buf_struct *it_bit_buff, WORD32 width, const UWORD16 *code_book_tbl,
645     WORD32 *x_invquant, WORD32 *ixheaacd_pow_table_Q13, WORD8 *ptr_scratch,
646     WORD32 tbl_sign, const UWORD32 *idx_table) {
647   WORD idx;
648   WORD16 index, length;
649   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
650   WORD32 bit_pos = it_bit_buff->bit_pos;
651   WORD32 read_word;
652   WORD32 increment;
653 
654   read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
655                                        &increment);
656   ptr_read_next += increment;
657 
658   for (idx = width; idx != 0; idx -= 4) {
659     WORD32 ampres, ampres1;
660     WORD32 ampres2, ampres3;
661     UWORD32 read_word1;
662 
663     read_word1 = read_word << bit_pos;
664     ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
665                             idx_table);
666     bit_pos += length;
667     ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
668                                 it_bit_buff->ptr_bit_buf_end);
669     if (tbl_sign) {
670       WORD32 w, x, y, z;
671       WORD32 ampout0, ampout1, ampout2, ampout3;
672       WORD32 temp_word;
673       temp_word = read_word << bit_pos;
674 
675       w = index / 27;
676       index = index - w * 27;
677       x = index / 9;
678       index = index - x * 9;
679       y = index / 3;
680       z = index - y * 3;
681 
682       ampout0 = w + *ptr_scratch++;
683       ampout0 = ixheaacd_pow_table_Q13[ampout0];
684 
685       if (w) {
686         if (temp_word & 0x80000000) {
687           ampout0 = -ampout0;
688         }
689         temp_word = temp_word << 1;
690         bit_pos++;
691       } else {
692         ampout0 = -ampout0;
693       }
694 
695       ampout1 = x + *ptr_scratch++;
696       ampout1 = ixheaacd_pow_table_Q13[ampout1];
697 
698       if (x) {
699         if (temp_word & 0x80000000) {
700           ampout1 = -ampout1;
701         }
702         temp_word = temp_word << 1;
703         bit_pos++;
704       } else {
705         ampout1 = -ampout1;
706       }
707 
708       ampout2 = y + *ptr_scratch++;
709       ampout2 = ixheaacd_pow_table_Q13[ampout2];
710 
711       if (y) {
712         if (temp_word & 0x80000000) {
713           ampout2 = -ampout2;
714         }
715         temp_word = temp_word << 1;
716         bit_pos++;
717       } else {
718         ampout2 = -ampout2;
719       }
720 
721       ampout3 = z + *ptr_scratch++;
722       ampout3 = ixheaacd_pow_table_Q13[ampout3];
723 
724       if (z) {
725         if (temp_word & 0x80000000) {
726           ampout3 = -ampout3;
727         }
728         temp_word = temp_word << 1;
729         bit_pos++;
730       } else {
731         ampout3 = -ampout3;
732       }
733       *x_invquant++ = ampout0;
734       *x_invquant++ = ampout1;
735       *x_invquant++ = ampout2;
736       *x_invquant++ = ampout3;
737     } else {
738       WORD32 w, x, y, z;
739       ampres = *ptr_scratch++;
740       ampres1 = *ptr_scratch++;
741       ampres2 = *ptr_scratch++;
742       ampres3 = *ptr_scratch++;
743 
744       w = index / 27 - 1;
745       index = index - (w + 1) * 27;
746       x = index / 9 - 1;
747       index = index - (x + 1) * 9;
748       y = index / 3 - 1;
749       z = index - ((y + 1) * 3) - 1;
750       if (w <= 0) {
751         ampres = ampres - w;
752         ampres = ixheaacd_pow_table_Q13[ampres];
753         ampres = -ampres;
754       } else {
755         ampres += w;
756         ampres = ixheaacd_pow_table_Q13[ampres];
757       }
758 
759       if (x <= 0) {
760         ampres1 = ampres1 - x;
761         ampres1 = ixheaacd_pow_table_Q13[ampres1];
762         ampres1 = -ampres1;
763       } else {
764         ampres1 += x;
765         ampres1 = ixheaacd_pow_table_Q13[ampres1];
766       }
767 
768       if (y <= 0) {
769         ampres2 = ampres2 - y;
770         ampres2 = ixheaacd_pow_table_Q13[ampres2];
771         ampres2 = -ampres2;
772       } else {
773         ampres2 += y;
774         ampres2 = ixheaacd_pow_table_Q13[ampres2];
775       }
776 
777       if (z <= 0) {
778         ampres3 = ampres3 - z;
779         ampres3 = ixheaacd_pow_table_Q13[ampres3];
780         ampres3 = -ampres3;
781       } else {
782         ampres3 += z;
783         ampres3 = ixheaacd_pow_table_Q13[ampres3];
784       }
785 
786       *x_invquant++ = ampres;
787       *x_invquant++ = ampres1;
788       *x_invquant++ = ampres2;
789       *x_invquant++ = ampres3;
790     }
791 
792     ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
793                                 it_bit_buff->ptr_bit_buf_end);
794   }
795 
796   ptr_read_next = ptr_read_next - increment;
797   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
798                                it_bit_buff->ptr_bit_buf_end);
799   it_bit_buff->ptr_read_next = ptr_read_next;
800   it_bit_buff->bit_pos = bit_pos;
801 
802   return 0;
803 }
804 
ixheaacd_huffman_dec_pair(ia_bit_buf_struct * it_bit_buff,WORD32 * spec_coef,WORD16 * offsets,WORD no_bands,WORD group_len,const UWORD16 * code_book_tbl,WORD32 * ixheaacd_pow_table_Q13,WORD32 tbl_sign,const UWORD32 * idx_table,WORD32 huff_mode)805 static PLATFORM_INLINE WORD ixheaacd_huffman_dec_pair(
806     ia_bit_buf_struct *it_bit_buff, WORD32 *spec_coef, WORD16 *offsets,
807     WORD no_bands, WORD group_len, const UWORD16 *code_book_tbl,
808     WORD32 *ixheaacd_pow_table_Q13, WORD32 tbl_sign, const UWORD32 *idx_table,
809     WORD32 huff_mode)
810 
811 {
812   WORD idx, grp_idx;
813   WORD len_idx;
814   WORD16 index, length;
815   WORD32 y, z;
816   WORD32 *spec_orig = spec_coef;
817 
818   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
819   WORD32 bit_pos = it_bit_buff->bit_pos;
820   WORD32 read_word;
821   WORD32 increment;
822 
823   read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
824                                        &increment);
825   ptr_read_next += increment;
826 
827   do {
828     len_idx = offsets[1] - offsets[0];
829     grp_idx = group_len;
830     do {
831       spec_coef += offsets[0];
832       idx = len_idx;
833       do {
834         UWORD32 read_word1;
835         read_word1 = read_word << bit_pos;
836         ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
837                                 idx_table);
838         bit_pos += length;
839         ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
840                                     it_bit_buff->ptr_bit_buf_end);
841         if (tbl_sign) {
842           WORD32 temp_word;
843           temp_word = read_word << bit_pos;
844           y = index / huff_mode;
845           z = index - huff_mode * y;
846           if (y) {
847             y = ixheaacd_pow_table_Q13[y];
848             if (temp_word & 0x80000000) y = -y;
849 
850             temp_word = temp_word << 1;
851             bit_pos++;
852           }
853           *spec_coef++ = y;
854 
855           if (z) {
856             z = ixheaacd_pow_table_Q13[z];
857             if (temp_word & 0x80000000) {
858               z = -z;
859             }
860             temp_word <<= 1;
861             bit_pos++;
862           }
863           *spec_coef++ = z;
864         } else {
865           y = (index / huff_mode) - 4;
866           z = index - ((y + 4) * huff_mode) - 4;
867           if (y < 0) {
868             y = -y;
869             y = ixheaacd_pow_table_Q13[y];
870             y = -y;
871           } else
872             y = ixheaacd_pow_table_Q13[y];
873 
874           if (z < 0) {
875             z = -z;
876             z = ixheaacd_pow_table_Q13[z];
877             z = -z;
878           } else
879             z = ixheaacd_pow_table_Q13[z];
880 
881           *spec_coef++ = y;
882           *spec_coef++ = z;
883         }
884         ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
885                                     it_bit_buff->ptr_bit_buf_end);
886         idx -= 2;
887       } while (idx != 0);
888 
889       spec_coef += (MAX_BINS_SHORT - offsets[1]);
890       grp_idx--;
891     } while (grp_idx != 0);
892 
893     offsets++;
894     spec_coef = spec_orig;
895     no_bands--;
896   } while (no_bands >= 0);
897 
898   ptr_read_next = ptr_read_next - increment;
899   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
900                                it_bit_buff->ptr_bit_buf_end);
901   it_bit_buff->ptr_read_next = ptr_read_next;
902   it_bit_buff->bit_pos = bit_pos;
903 
904   return 0;
905 }
906 
ixheaacd_huffman_dec_word2_pair(ia_bit_buf_struct * it_bit_buff,WORD32 width,const UWORD16 * code_book_tbl,WORD32 * x_invquant,WORD32 * ixheaacd_pow_table_Q13,WORD8 * ptr_scratch,WORD32 tbl_sign,const UWORD32 * idx_table,WORD32 huff_mode)907 static PLATFORM_INLINE WORD ixheaacd_huffman_dec_word2_pair(
908     ia_bit_buf_struct *it_bit_buff, WORD32 width, const UWORD16 *code_book_tbl,
909     WORD32 *x_invquant, WORD32 *ixheaacd_pow_table_Q13, WORD8 *ptr_scratch,
910     WORD32 tbl_sign, const UWORD32 *idx_table, WORD32 huff_mode)
911 
912 {
913   WORD32 ampres;
914   WORD idx;
915   WORD16 index, length;
916   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
917   WORD32 bit_pos = it_bit_buff->bit_pos;
918   WORD32 read_word;
919   WORD32 increment;
920 
921   read_word = ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->cnt_bits,
922                                        &increment);
923   ptr_read_next += increment;
924 
925   for (idx = width; idx != 0; idx -= 2) {
926     {
927       UWORD32 read_word1;
928       read_word1 = read_word << bit_pos;
929       ixheaacd_huffman_decode(read_word1, &index, &length, code_book_tbl,
930                               idx_table);
931       bit_pos += length;
932       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
933                                   it_bit_buff->ptr_bit_buf_end);
934     }
935 
936     if (tbl_sign) {
937       WORD32 out0, out1, temp_word;
938       WORD32 ampout0, ampout1;
939 
940       ampout0 = *ptr_scratch++;
941       ampout1 = *ptr_scratch++;
942       out0 = index / huff_mode;
943       out1 = index - huff_mode * out0;
944       ampout0 += out0;
945       ampout0 = ixheaacd_pow_table_Q13[ampout0];
946 
947       ampout1 += out1;
948       ampout1 = ixheaacd_pow_table_Q13[ampout1];
949       temp_word = read_word << bit_pos;
950       if (out0) {
951         if (temp_word & 0x80000000) {
952           ampout0 = -(ampout0);
953         }
954 
955         bit_pos++;
956         temp_word = temp_word << 1;
957       } else {
958         ampout0 = -(ampout0);
959       }
960 
961       if (out1) {
962         if (temp_word & 0x80000000) {
963           ampout1 = -(ampout1);
964         }
965         bit_pos++;
966       } else {
967         ampout1 = -(ampout1);
968       }
969 
970       ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
971                                   it_bit_buff->ptr_bit_buf_end);
972       *x_invquant++ = ampout0;
973       *x_invquant++ = ampout1;
974     } else {
975       WORD32 y, z;
976       y = (index / huff_mode) - 4;
977       z = index - ((y + 4) * huff_mode) - 4;
978 
979       ampres = *ptr_scratch++;
980       if (y <= 0) {
981         ampres = ampres - y;
982         ampres = ixheaacd_pow_table_Q13[ampres];
983         *x_invquant++ = -ampres;
984       } else {
985         ampres += y;
986         *x_invquant++ = ixheaacd_pow_table_Q13[ampres];
987       }
988       ampres = *ptr_scratch++;
989       if (z <= 0) {
990         ampres = ampres - z;
991         ampres = ixheaacd_pow_table_Q13[ampres];
992         *x_invquant++ = -ampres;
993       } else {
994         ampres += z;
995         *x_invquant++ = ixheaacd_pow_table_Q13[ampres];
996       }
997     }
998     ixheaacd_aac_read_byte_corr(&ptr_read_next, &bit_pos, &read_word,
999                                 it_bit_buff->ptr_bit_buf_end);
1000   }
1001 
1002   ptr_read_next = ptr_read_next - increment;
1003   ixheaacd_aac_read_byte_corr1(&ptr_read_next, &bit_pos, &read_word,
1004                                it_bit_buff->ptr_bit_buf_end);
1005   it_bit_buff->ptr_read_next = ptr_read_next;
1006   it_bit_buff->bit_pos = bit_pos;
1007 
1008   return 0;
1009 }
1010 
ixheaacd_decode_huffman(ia_bit_buf_struct * it_bit_buff,WORD32 cb_no,WORD32 * spec_coef,WORD16 * sfb_offset,WORD start,WORD sfb,WORD group_len,ia_aac_dec_tables_struct * ptr_aac_tables)1011 WORD ixheaacd_decode_huffman(ia_bit_buf_struct *it_bit_buff, WORD32 cb_no,
1012                              WORD32 *spec_coef, WORD16 *sfb_offset, WORD start,
1013                              WORD sfb, WORD group_len,
1014                              ia_aac_dec_tables_struct *ptr_aac_tables) {
1015   WORD ret_val = 0;
1016   WORD start_bit_pos = it_bit_buff->bit_pos;
1017   UWORD8 *start_read_pos = it_bit_buff->ptr_read_next;
1018   const UWORD16 *cb_table = (UWORD16 *)(ptr_aac_tables->code_book[cb_no]);
1019   WORD32 huff_mode;
1020   const UWORD32 *idx_table = (UWORD32 *)(ptr_aac_tables->index_table[cb_no]);
1021   WORD32 *pow_table =
1022       (WORD32 *)ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13;
1023   WORD32 no_bands = sfb - start - 1;
1024   WORD16 *band_offset = sfb_offset + start;
1025 
1026   if (cb_no == 11) {
1027     const UWORD32 *idx_table =
1028         ptr_aac_tables->pstr_huffmann_tables->idx_table_hf11;
1029     const UWORD16 *cb_table =
1030         ptr_aac_tables->pstr_huffmann_tables->input_table_cb11;
1031 
1032     ret_val = ixheaacd_huffman_dec_word1(it_bit_buff, spec_coef, band_offset,
1033                                          no_bands, group_len, cb_table,
1034                                          pow_table, idx_table);
1035 
1036   } else if (cb_no <= 4) {
1037     WORD32 tbl_sign = 0;
1038 
1039     if (cb_no > 2) {
1040       tbl_sign = 1;
1041     }
1042     ret_val = ixheaacd_huffman_dec_quad(it_bit_buff, spec_coef, band_offset,
1043                                         no_bands, group_len, cb_table,
1044                                         pow_table, tbl_sign, idx_table);
1045   }
1046 
1047   else if (cb_no <= 10) {
1048     WORD32 tbl_sign = 0;
1049     huff_mode = 9;
1050     if (cb_no > 6) {
1051       if (cb_no > 8)
1052         huff_mode = 13;
1053       else
1054         huff_mode = 8;
1055       tbl_sign = 1;
1056     }
1057     ret_val = ixheaacd_huffman_dec_pair(
1058         it_bit_buff, spec_coef, band_offset, no_bands, group_len, cb_table,
1059         pow_table, tbl_sign, idx_table, huff_mode);
1060   }
1061 
1062   {
1063     WORD bits_cons;
1064     bits_cons = ((it_bit_buff->ptr_read_next - start_read_pos) << 3) +
1065                 (it_bit_buff->bit_pos - start_bit_pos);
1066     it_bit_buff->cnt_bits -= bits_cons;
1067   }
1068   return ret_val;
1069 }
1070 
ixheaacd_huffman_dec_word2(ia_bit_buf_struct * it_bit_buff,WORD32 cb_no,WORD32 width,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 * x_invquant,WORD8 * scratch_ptr)1071 WORD ixheaacd_huffman_dec_word2(ia_bit_buf_struct *it_bit_buff, WORD32 cb_no,
1072                                 WORD32 width,
1073                                 ia_aac_dec_tables_struct *ptr_aac_tables,
1074                                 WORD32 *x_invquant, WORD8 *scratch_ptr) {
1075   WORD ret_val = 0;
1076   WORD32 huff_mode;
1077   WORD start_bit_pos = it_bit_buff->bit_pos;
1078   WORD32 cnt_bits = it_bit_buff->cnt_bits;
1079   WORD32 *pow_table =
1080       (WORD32 *)ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13;
1081   UWORD8 *start_read_pos = it_bit_buff->ptr_read_next;
1082 
1083   const UWORD16 *cb_table = (UWORD16 *)(ptr_aac_tables->code_book[cb_no]);
1084   const UWORD32 *idx_table = (UWORD32 *)(ptr_aac_tables->index_table[cb_no]);
1085 
1086   if (cb_no == 11) {
1087     const UWORD16 *cb_table =
1088         ptr_aac_tables->pstr_huffmann_tables->input_table_cb11;
1089 
1090     ret_val = ixheaacd_huffman_dec_word2_11(
1091         it_bit_buff, width, cb_table, x_invquant, pow_table, scratch_ptr,
1092         ptr_aac_tables->pstr_huffmann_tables->idx_table_hf11);
1093   } else if (cb_no <= 4) {
1094     WORD32 tbl_sign = 0;
1095     if (cb_no > 2) tbl_sign = 1;
1096     ret_val = ixheaacd_huffman_dec_word2_quad(it_bit_buff, width, cb_table,
1097                                               x_invquant, pow_table,
1098                                               scratch_ptr, tbl_sign, idx_table);
1099   } else if (cb_no <= 10) {
1100     WORD32 tbl_sign = 0;
1101     huff_mode = 9;
1102     if (cb_no > 6) {
1103       if (cb_no > 8) {
1104         huff_mode = 13;
1105       } else {
1106         huff_mode = 8;
1107       }
1108 
1109       tbl_sign = 1;
1110     }
1111     ret_val = ixheaacd_huffman_dec_word2_pair(
1112         it_bit_buff, width, cb_table, x_invquant, pow_table, scratch_ptr,
1113         tbl_sign, idx_table, huff_mode);
1114   }
1115 
1116   {
1117     WORD bits_cons;
1118     if (it_bit_buff->bit_pos <= 7) {
1119       bits_cons = ((it_bit_buff->ptr_read_next - start_read_pos) << 3) +
1120                   (it_bit_buff->bit_pos - start_bit_pos);
1121       it_bit_buff->cnt_bits = cnt_bits - bits_cons;
1122     } else {
1123       it_bit_buff->ptr_read_next += (it_bit_buff->bit_pos) >> 3;
1124       it_bit_buff->bit_pos = it_bit_buff->bit_pos & 0x7;
1125 
1126       bits_cons = ((it_bit_buff->ptr_read_next - start_read_pos) << 3) +
1127                   ((it_bit_buff->bit_pos - start_bit_pos));
1128       it_bit_buff->cnt_bits = cnt_bits - bits_cons;
1129     }
1130   }
1131   return ret_val;
1132 }
1133 
ixheaacd_lap1_512_480(WORD32 * coef,WORD32 * prev,WORD16 * out,const WORD16 * window,WORD16 q_shift,WORD16 size,WORD16 stride)1134 void ixheaacd_lap1_512_480(WORD32 *coef, WORD32 *prev, WORD16 *out,
1135                            const WORD16 *window, WORD16 q_shift, WORD16 size,
1136                            WORD16 stride) {
1137   WORD32 accu;
1138   WORD32 i;
1139   WORD16 rounding_fac = -0x2000;
1140 
1141   WORD32 *window_i = (WORD32 *)window;
1142 
1143   WORD16 *ptr_out1, *ptr_out2;
1144 
1145   WORD32 *pwin1, *pwin2;
1146   WORD32 *pCoef = &coef[size * 2 - 1 - 0];
1147 
1148   pwin1 = &window_i[size - 1 - 0];
1149   pwin2 = &window_i[size + 0];
1150 
1151   ptr_out1 = &out[stride * (size - 1 - 0)];
1152   ptr_out2 = &out[stride * (size + 0)];
1153 
1154   for (i = 0; i < size; i++) {
1155     WORD32 win1, win2, coeff;
1156     WORD32 prev_data = *prev++;
1157 
1158     win1 = *pwin1--;
1159     coeff = *pCoef--;
1160     win2 = *pwin2++;
1161 
1162     accu = ixheaacd_sub32_sat(
1163         ixheaacd_shl32_dir_sat_limit(ixheaacd_mult32_shl(coeff, win1), q_shift),
1164         ixheaacd_mac32x16in32_shl(rounding_fac, win2, (WORD16)(prev_data)));
1165 
1166     accu = ixheaacd_add32_sat(accu, accu);
1167     accu = ixheaacd_add32_sat(accu, accu);
1168 
1169     *ptr_out1 = ixheaacd_shr32(accu, 16);
1170     ptr_out1 -= stride;
1171 
1172     accu = ixheaacd_sub32_sat(
1173         ixheaacd_shl32_dir_sat_limit(
1174             ixheaacd_mult32_shl(ixheaacd_negate32(coeff), win2), q_shift),
1175         ixheaacd_mac32x16in32_shl(rounding_fac, win1, (WORD16)(prev_data)));
1176 
1177     accu = ixheaacd_add32_sat(accu, accu);
1178     accu = ixheaacd_add32_sat(accu, accu);
1179 
1180     *ptr_out2 = ixheaacd_shr32(accu, 16);
1181     ptr_out2 += stride;
1182   }
1183 }
1184 
ixheaacd_over_lap_add1_dec(WORD32 * coef,WORD32 * prev,WORD16 * out,const WORD16 * window,WORD16 q_shift,WORD16 size,WORD16 ch_fac)1185 VOID ixheaacd_over_lap_add1_dec(WORD32 *coef, WORD32 *prev, WORD16 *out,
1186                                 const WORD16 *window, WORD16 q_shift,
1187                                 WORD16 size, WORD16 ch_fac) {
1188   WORD32 accu;
1189   WORD32 i;
1190   WORD16 rounding_fac = -0x2000;
1191 
1192   for (i = 0; i < size; i++) {
1193     WORD16 window1, window2;
1194 
1195     window1 = window[2 * size - 2 * i - 1];
1196     window2 = window[2 * size - 2 * i - 2];
1197     accu = ixheaacd_sub32_sat(
1198         ixheaacd_shl32_dir_sat_limit(
1199             ixheaacd_mult32x16in32(coef[size * 2 - 1 - i], window2), q_shift),
1200         ixheaacd_mac32x16in32_drc(rounding_fac, prev[i], window1));
1201     out[ch_fac * (size - i - 1)] =
1202         ixheaacd_shr32(ixheaacd_shl32_dir_sat_limit(accu, 2), 16);
1203     accu = ixheaacd_sub32_sat(
1204         ixheaacd_shl32_dir_sat_limit(
1205             ixheaacd_mult32x16in32(ixheaacd_negate32(coef[size * 2 - 1 - i]),
1206                                    window1),
1207             q_shift),
1208         ixheaacd_mac32x16in32_drc(rounding_fac, prev[i], window2));
1209     out[ch_fac * (size + i)] =
1210         ixheaacd_shr32(ixheaacd_shl32_dir_sat_limit(accu, 2), 16);
1211   }
1212 }
1213 
ixheaacd_over_lap_add2_dec(WORD32 * coef,WORD32 * prev,WORD32 * out,const WORD16 * window,WORD16 q_shift,WORD16 size,WORD16 ch_fac)1214 VOID ixheaacd_over_lap_add2_dec(WORD32 *coef, WORD32 *prev, WORD32 *out,
1215                                 const WORD16 *window, WORD16 q_shift,
1216                                 WORD16 size, WORD16 ch_fac) {
1217   WORD32 accu;
1218   WORD32 i;
1219 
1220   for (i = 0; i < size; i++) {
1221     accu = ixheaacd_sub32_sat(
1222         ixheaacd_mult32x16in32(coef[size + i], window[2 * i]),
1223         ixheaacd_mult32x16in32(prev[size - 1 - i], window[2 * i + 1]));
1224     out[ch_fac * i] = ixheaacd_shr32_drc(accu, 16 - (q_shift + 1));
1225   }
1226 
1227   for (i = 0; i < size; i++) {
1228     accu = ixheaacd_sub32_sat(
1229         ixheaacd_mult32x16in32(ixheaacd_negate32_sat(coef[size * 2 - 1 - i]),
1230                                window[2 * size - 2 * i - 1]),
1231         ixheaacd_mult32x16in32(prev[i], window[2 * size - 2 * i - 2]));
1232     out[ch_fac * (i + size)] = ixheaacd_shr32_drc(accu, 16 - (q_shift + 1));
1233   }
1234 }
1235 
ixheaacd_process_single_scf(WORD32 scale_factor,WORD32 * x_invquant,WORD32 width,WORD32 * ptr_scale_table,WORD32 total_channels,WORD32 object_type,WORD32 aac_sf_data_resil_flag)1236 VOID ixheaacd_process_single_scf(WORD32 scale_factor, WORD32 *x_invquant,
1237                                  WORD32 width, WORD32 *ptr_scale_table,
1238                                  WORD32 total_channels, WORD32 object_type,
1239                                  WORD32 aac_sf_data_resil_flag) {
1240   WORD32 j;
1241 
1242   WORD32 temp1;
1243   WORD32 q_factor;
1244   WORD32 buffer1;
1245   WORD16 scale_short;
1246 
1247   object_type = 0;
1248   aac_sf_data_resil_flag = 0;
1249 
1250   if (scale_factor < 24) {
1251     for (j = width; j > 0; j--) {
1252       *x_invquant++ = 0;
1253     }
1254   } else {
1255     WORD32 shift;
1256 
1257     if (total_channels > 2)
1258       q_factor = 34 - (scale_factor >> 2);
1259     else
1260       q_factor = 37 - (scale_factor >> 2);
1261 
1262     scale_short = ptr_scale_table[(scale_factor & 0x0003)];
1263     shift = q_factor;
1264     if (shift > 0) {
1265       if (scale_short == (WORD16)0x8000) {
1266         for (j = width; j > 0; j--) {
1267           temp1 = *x_invquant;
1268           buffer1 = ixheaacd_mult32x16in32_shl_sat(temp1, scale_short);
1269           buffer1 = ixheaacd_shr32(buffer1, shift);
1270           *x_invquant++ = buffer1;
1271         }
1272       } else {
1273         for (j = width; j > 0; j--) {
1274           temp1 = *x_invquant;
1275           buffer1 = ixheaacd_mult32x16in32_shl(temp1, scale_short);
1276           buffer1 = ixheaacd_shr32(buffer1, shift);
1277           *x_invquant++ = buffer1;
1278         }
1279       }
1280     } else {
1281       shift = -shift;
1282       if (shift > 0) {
1283         if (scale_short == (WORD16)0x8000) {
1284           for (j = width; j > 0; j--) {
1285             temp1 = *x_invquant;
1286             temp1 = ixheaacd_shl32(temp1, shift - 1);
1287             buffer1 = ixheaacd_mult32x16in32_shl_sat(temp1, scale_short);
1288             buffer1 = ixheaacd_shl32(buffer1, 1);
1289             *x_invquant++ = buffer1;
1290           }
1291         } else {
1292           for (j = width; j > 0; j--) {
1293             temp1 = *x_invquant;
1294             temp1 = ixheaacd_shl32(temp1, shift - 1);
1295             buffer1 = ixheaacd_mult32x16in32_shl(temp1, scale_short);
1296             buffer1 = ixheaacd_shl32(buffer1, 1);
1297             *x_invquant++ = buffer1;
1298           }
1299         }
1300 
1301       } else {
1302         if (scale_short == (WORD16)0x8000) {
1303           for (j = width; j > 0; j--) {
1304             temp1 = *x_invquant;
1305             buffer1 = ixheaacd_mult32x16in32_shl_sat(temp1, scale_short);
1306             *x_invquant++ = buffer1;
1307           }
1308         } else {
1309           for (j = width; j > 0; j--) {
1310             temp1 = *x_invquant;
1311             buffer1 = ixheaacd_mult32x16in32_shl(temp1, scale_short);
1312             *x_invquant++ = buffer1;
1313           }
1314         }
1315       }
1316     }
1317   }
1318 }
1319 
ixheaacd_scale_factor_process_dec(WORD32 * x_invquant,WORD16 * scale_fact,WORD no_band,WORD8 * width,WORD32 * ptr_scale_table,WORD32 total_channels,WORD32 object_type,WORD32 aac_sf_data_resil_flag)1320 VOID ixheaacd_scale_factor_process_dec(WORD32 *x_invquant, WORD16 *scale_fact,
1321                                        WORD no_band, WORD8 *width,
1322                                        WORD32 *ptr_scale_table,
1323                                        WORD32 total_channels,
1324                                        WORD32 object_type,
1325                                        WORD32 aac_sf_data_resil_flag) {
1326   WORD32 i;
1327   WORD16 scale_factor;
1328 
1329   for (i = no_band - 1; i >= 0; i--) {
1330     scale_factor = *scale_fact++;
1331     ixheaacd_process_single_scf(scale_factor, x_invquant, *width,
1332                                 ptr_scale_table, total_channels, object_type,
1333                                 aac_sf_data_resil_flag);
1334 
1335     x_invquant += *width;
1336     width++;
1337   }
1338 }
1339 
ixheaacd_right_shift_block(WORD32 * p_spectrum,WORD32 length,WORD32 shift_val)1340 void ixheaacd_right_shift_block(WORD32 *p_spectrum, WORD32 length,
1341                                 WORD32 shift_val) {
1342   WORD32 i;
1343   WORD32 temp1, temp2;
1344   WORD32 *temp_ptr = &p_spectrum[0];
1345   length = length >> 2;
1346 
1347   for (i = length - 1; i >= 0; i--) {
1348     temp1 = *temp_ptr;
1349     temp2 = *(temp_ptr + 1);
1350     *temp_ptr++ = temp1 >> shift_val;
1351     temp1 = *(temp_ptr + 1);
1352     *temp_ptr++ = temp2 >> shift_val;
1353     temp2 = *(temp_ptr + 1);
1354     *temp_ptr++ = temp1 >> shift_val;
1355     *temp_ptr++ = temp2 >> shift_val;
1356   }
1357 }
1358