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