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