• 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 <math.h>
21 #include "ixheaacd_type_def.h"
22 #include "ixheaacd_constants.h"
23 #include "ixheaacd_bitbuffer.h"
24 #include "ixheaacd_common_rom.h"
25 #include "ixheaacd_sbrdecsettings.h"
26 #include "ixheaacd_sbr_scale.h"
27 #include "ixheaacd_env_extr_part.h"
28 #include "ixheaacd_sbr_rom.h"
29 #include "ixheaacd_hybrid.h"
30 #include "ixheaacd_ps_dec.h"
31 #include "ixheaacd_config.h"
32 #include "ixheaacd_qmf_dec.h"
33 #include "ixheaacd_audioobjtypes.h"
34 #include "ixheaacd_mps_polyphase.h"
35 #include "ixheaacd_mps_struct_def.h"
36 #include "ixheaacd_mps_res_rom.h"
37 #include "ixheaacd_mps_defines.h"
38 #include "ixheaacd_mps_aac_struct.h"
39 #include "ixheaacd_mps_dec.h"
40 #include "ixheaacd_mps_nlc_dec.h"
41 #include "ixheaacd_error_standards.h"
42 
43 static const WORD32 ixheaacd_freq_res_table[] = {0, 28, 20, 14, 10, 7, 5, 4};
44 
45 static const WORD32 ixheaacd_freq_res_table_ld[] = {0, 23, 15, 12, 9, 7, 5, 4};
46 
47 static const WORD32
48     ixheaacd_hybrid_band_71_to_processing_band_4_map[MAX_HYBRID_BANDS_MPS] = {
49         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50         2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
51         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
52 
53 static const WORD32
54     ixheaacd_hybrid_band_71_to_processing_band_5_map[MAX_HYBRID_BANDS_MPS] = {
55         0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
56         3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
57         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
58 
59 static const WORD32
60     ixheaacd_hybrid_band_71_to_processing_band_7_map[MAX_HYBRID_BANDS_MPS] = {
61         0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
62         5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
63         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
64 
65 static const WORD32
66     ixheaacd_hybrid_band_71_to_processing_band_10_map[MAX_HYBRID_BANDS_MPS] = {
67         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8,
68         8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
69         9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
70 
71 static const WORD32
72     ixheaacd_hybrid_band_71_to_processing_band_14_map[MAX_HYBRID_BANDS_MPS] = {
73         0,  0,  0,  0,  1,  1,  2,  3,  4,  4,  5,  6,  6,  7,  7,  8,  8,  8,
74         9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
75         12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
76         13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
77 
78 const WORD32
79     ixheaacd_hybrid_band_71_to_processing_band_20_map[MAX_HYBRID_BANDS_MPS] = {
80         1,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 14,
81         15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18,
82         18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
83         19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19};
84 
85 const WORD32
86     ixheaacd_hybrid_band_71_to_processing_band_28_map[MAX_HYBRID_BANDS_MPS] = {
87         1,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
88         16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23,
89         23, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26,
90         26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27};
91 
92 const WORD32
93     ixheaacd_hybrid_band_64_to_processing_band_4_map[MAX_HYBRID_BANDS_MPS] =
94         {0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
95          2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
96          3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
97          3, 3, 3, 3, 3};
98 
99 const WORD32
100     ixheaacd_hybrid_band_64_to_processing_band_5_map[MAX_HYBRID_BANDS_MPS] =
101         {0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
102          3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
103          4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
104          4, 4, 4, 4, 4};
105 
106 const WORD32
107     ixheaacd_hybrid_band_64_to_processing_band_7_map[MAX_HYBRID_BANDS_MPS] =
108         {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
109          5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
110          6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
111          6, 6, 6, 6, 6};
112 
113 const WORD32
114     ixheaacd_hybrid_band_64_to_processing_band_9_map[MAX_HYBRID_BANDS_MPS] =
115         {0, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
116          7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
117          8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
118          8, 8, 8, 8, 8};
119 
120 const WORD32
121     ixheaacd_hybrid_band_64_to_processing_band_12_map[MAX_HYBRID_BANDS_MPS] =
122         {0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  6,  7,  7,  7,  8,  8,
123          8,  8,  9,  9,  9,  9,  9,  10, 10, 10, 10, 10, 10, 10, 10, 10,
124          10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
125          11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
126          11, 11, 11, 11, 11, 11, 11};
127 
128 const WORD32
129     ixheaacd_hybrid_band_64_to_processing_band_15_map[MAX_HYBRID_BANDS_MPS] =
130         {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  9,  10, 10, 10, 11, 11,
131          11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13,
132          13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
133          14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
134          14, 14, 14, 14, 14, 14, 14};
135 
136 const WORD32
137     ixheaacd_hybrid_band_64_to_processing_band_23_map[MAX_HYBRID_BANDS_MPS] =
138         {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 12, 13, 13,
139          14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19,
140          19, 19, 19, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21,
141          22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
142          22, 22, 22, 22, 22, 22, 22};
143 
144 static const FLOAT32 ixheaacd_mps_clip_gain_table[] = {
145     1.000000f, 1.189207f, 1.414213f, 1.681792f,
146     2.000000f, 2.378414f, 2.828427f, 4.000000f};
147 
148 static const WORD32 ixheaacd_mps_stride_table[] = {1, 2, 5, 28};
149 
150 static const FLOAT32 ixheaacd_cld_de_quant_table[] = {
151     -150.0, -45.0, -40.0, -35.0, -30.0, -25.0, -22.0, -19.0,
152     -16.0,  -13.0, -10.0, -8.0,  -6.0,  -4.0,  -2.0,  0.0,
153     2.0,    4.0,   6.0,   8.0,   10.0,  13.0,  16.0,  19.0,
154     22.0,   25.0,  30.0,  35.0,  40.0,  45.0,  150.0};
155 
156 static const FLOAT32 ixheaacd_icc_de_quant_table[] = {
157     1.0000f, 0.9370f, 0.84118f, 0.60092f, 0.36764f, 0.0f, -0.5890f, -0.9900f};
158 
159 const FLOAT32 ixheaacd_ipd_de_quant_table[] = {
160     0.f,          0.392699082f, 0.785398163f, 1.178097245f,
161     1.570796327f, 1.963495408f, 2.35619449f,  2.748893572f,
162     3.141592654f, 3.534291735f, 3.926990817f, 4.319689899f,
163     4.71238898f,  5.105088062f, 5.497787144f, 5.890486225f};
164 const WORD32 ixheaacd_ipd_de_quant_table_q28[] = {
165     0,          105414360,  210828720,  316243072, 421657440,  527071776,
166     632486144,  737900480,  843314880,  948729216, 1054143552, 1159557888,
167     1264972288, 1370386688, 1475800960, 1581215360};
168 static const WORD32 ixheaacd_smoothing_time_table[] = {64, 128, 256, 512};
169 
170 static const FLOAT32 ixheaacd_inverse_smoothing_time_table[] = {
171     1.0f / 64.0f, 1.0f / 128.0f, 1.0f / 256.0f, 1.0f / 512.0f};
172 
bound_check(WORD32 var,WORD32 lower_bound,WORD32 upper_bound)173 static WORD32 bound_check(WORD32 var, WORD32 lower_bound, WORD32 upper_bound) {
174   var = min(var, upper_bound);
175   var = max(var, lower_bound);
176   return var;
177 }
178 
ixheaacd_longmult1(UWORD16 a[],UWORD16 b,UWORD16 d[],WORD32 len)179 static VOID ixheaacd_longmult1(UWORD16 a[], UWORD16 b,
180                                UWORD16 d[], WORD32 len) {
181   WORD32 k;
182   UWORD32 tmp;
183   UWORD32 b0 = (UWORD32)b;
184 
185   tmp = ((UWORD32)a[0]) * b0;
186   d[0] = (UWORD16)tmp;
187 
188   for (k = 1; k < len; k++) {
189     tmp = (tmp >> 16) + ((UWORD32)a[k]) * b0;
190     d[k] = (UWORD16)tmp;
191   }
192 }
193 
ixheaacd_longdiv(UWORD16 b[],UWORD16 a,UWORD16 d[],UWORD16 * pr,WORD32 len)194 static VOID ixheaacd_longdiv(UWORD16 b[], UWORD16 a,
195                              UWORD16 d[], UWORD16 *pr, WORD32 len) {
196   UWORD32 r;
197   UWORD32 tmp;
198   UWORD32 temp;
199   WORD32 k;
200 
201   if (a == 0)
202     return;
203 
204   r = 0;
205 
206   for (k = len - 1; k >= 0; k--) {
207     tmp = ((UWORD32)b[k]) + (r << 16);
208 
209     if (tmp) {
210       d[k] = (UWORD16)(tmp / a);
211       temp = d[k] * a;
212       r = tmp - temp;
213     } else {
214       d[k] = 0;
215     }
216   }
217   *pr = (UWORD16)r;
218 }
219 
ixheaacd_longsub(UWORD16 a[],UWORD16 b[],WORD32 lena,WORD32 lenb)220 static VOID ixheaacd_longsub(UWORD16 a[], UWORD16 b[], WORD32 lena,
221                              WORD32 lenb) {
222   WORD32 h;
223   WORD32 carry = 0;
224 
225   if (lenb > lena)
226     return;
227 
228   for (h = 0; h < lenb; h++) {
229     carry = carry + (WORD32)(a[h] - b[h]);
230     a[h] = (UWORD16)carry;
231     carry = carry >> 16;
232   }
233 
234   for (; h < lena; h++) {
235     carry = ((UWORD32)a[h]) + carry;
236     a[h] = (UWORD16)carry;
237     carry = carry >> 16;
238   }
239 
240   if (carry != 0)
241     return;
242   return;
243 }
244 
ixheaacd_longcompare(UWORD16 a[],UWORD16 b[],WORD32 len)245 static WORD32 ixheaacd_longcompare(UWORD16 a[], UWORD16 b[],
246                                 WORD32 len) {
247   WORD32 i;
248 
249   for (i = len - 1; i > 0; i--) {
250     if (a[i] != b[i]) break;
251   }
252   return (a[i] >= b[i]) ? 1 : 0;
253 }
254 
ixheaacd_mps_coarse2fine(WORD32 * data,WORD32 data_type,WORD32 band_start,WORD32 ixheaacd_num_bands)255 static VOID ixheaacd_mps_coarse2fine(WORD32 *data, WORD32 data_type,
256                                      WORD32 band_start, WORD32 ixheaacd_num_bands) {
257   WORD32 i;
258 
259   for (i = band_start; i < band_start + ixheaacd_num_bands; i++) {
260     data[i] <<= 1;
261   }
262 
263   if (data_type == CLD) {
264     for (i = band_start; i < band_start + ixheaacd_num_bands; i++) {
265       if (data[i] == -14)
266         data[i] = -15;
267       else if (data[i] == 14)
268         data[i] = 15;
269     }
270   }
271 }
272 
ixheaacd_mps_fine2coarse(WORD32 * data,WORD32 ixheaacd_num_bands)273 static VOID ixheaacd_mps_fine2coarse(WORD32 *data, WORD32 ixheaacd_num_bands) {
274   WORD32 i;
275 
276   for (i = 0; i < ixheaacd_num_bands; i++) {
277     data[i] /= 2;
278   }
279 }
280 
ixheaacd_mps_getstridemap(WORD32 freq_res_stride,WORD32 band_start,WORD32 band_stop,WORD32 * strides)281 static WORD32 ixheaacd_mps_getstridemap(WORD32 freq_res_stride, WORD32 band_start,
282                                      WORD32 band_stop, WORD32 *strides) {
283   WORD32 i, pb, ch_fac, data_bands, start_offset;
284 
285   ch_fac = ixheaacd_mps_stride_table[freq_res_stride];
286   data_bands = (band_stop - band_start - 1) / ch_fac + 1;
287 
288   strides[0] = band_start;
289   for (pb = 1; pb <= data_bands; pb++) {
290     strides[pb] = strides[pb - 1] + ch_fac;
291   }
292   start_offset = 0;
293   while (strides[data_bands] > band_stop) {
294     if (start_offset < data_bands)
295       start_offset++;
296     else
297       start_offset = 1;
298 
299     for (i = start_offset; i <= data_bands; i++) {
300       strides[i]--;
301     }
302   }
303 
304   return data_bands;
305 }
306 
ixheaacd_mps_ecdata_decoding(ia_mps_dec_state_struct * self,ia_bit_buf_struct * bitstream,WORD32 data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],WORD32 datatype)307 static IA_ERRORCODE ixheaacd_mps_ecdata_decoding(
308     ia_mps_dec_state_struct *self, ia_bit_buf_struct *bitstream,
309     WORD32 data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS], WORD32 datatype) {
310   WORD32 i, j, pb, set_index, bs_data_pair, data_bands, old_quant_coarse_xxx;
311   WORD32 strides[MAX_PARAMETER_BANDS + 1] = {0};
312   WORD32 band_stop = 0;
313 
314   WORD32 *lastdata = NULL;
315   ia_mps_data_struct *frame_xxx_data = NULL;
316   WORD32 default_val = 0;
317   IA_ERRORCODE err = IA_NO_ERROR;
318 
319   ia_mps_bs_frame *frame = &(self->bs_frame);
320 
321   if (datatype == 0) {
322     frame_xxx_data = &frame->cld_data;
323     lastdata = frame->cmp_cld_idx_prev;
324     band_stop = self->bs_param_bands;
325   } else if (datatype == 1) {
326     frame_xxx_data = &frame->icc_data;
327     lastdata = frame->cmp_icc_idx_prev;
328     band_stop = self->bs_param_bands;
329   } else if (datatype == 2) {
330     frame_xxx_data = &frame->ipd_data;
331     lastdata = frame->ipd_idx_data_prev;
332     band_stop = self->num_bands_ipd;
333   } else {
334     frame_xxx_data = &frame->cld_data;
335     lastdata = frame->cmp_cld_idx_prev;
336     band_stop = self->bs_param_bands;
337   }
338 
339   for (i = 0; i < self->num_parameter_sets; i++) {
340     frame_xxx_data->bs_xxx_data_mode[i] = ixheaacd_read_bits_buf(bitstream, 2);
341   }
342 
343   set_index = 0;
344   bs_data_pair = 0;
345   old_quant_coarse_xxx = frame_xxx_data->bs_quant_coarse_xxx_prev;
346 
347   for (i = 0; i < self->num_parameter_sets; i++) {
348     if (frame_xxx_data->bs_xxx_data_mode[i] == 0) {
349       for (pb = 0; pb < band_stop; pb++) {
350         lastdata[pb] = default_val;
351       }
352 
353       old_quant_coarse_xxx = 0;
354     }
355 
356     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
357       if (bs_data_pair) {
358         bs_data_pair = 0;
359       } else {
360         bs_data_pair = ixheaacd_read_bits_buf(bitstream, 1);
361         frame_xxx_data->bs_quant_coarse_xxx[set_index] =
362             ixheaacd_read_bits_buf(bitstream, 1);
363         frame_xxx_data->bs_freq_res_stride_xxx[set_index] =
364             ixheaacd_read_bits_buf(bitstream, 2);
365 
366         if (frame_xxx_data->bs_quant_coarse_xxx[set_index] !=
367             old_quant_coarse_xxx) {
368           if (old_quant_coarse_xxx) {
369             ixheaacd_mps_coarse2fine(lastdata, datatype, 0, band_stop - 0);
370           } else {
371             ixheaacd_mps_fine2coarse(lastdata, band_stop);
372           }
373         }
374 
375         data_bands = ixheaacd_mps_getstridemap(
376             frame_xxx_data->bs_freq_res_stride_xxx[set_index], 0, band_stop,
377             strides);
378 
379         for (pb = 0; pb < data_bands; pb++) {
380           lastdata[pb] = lastdata[strides[pb]];
381         }
382 
383         err = ixheaacd_mps_ecdatapairdec(
384             bitstream, data, lastdata, datatype, set_index, 0, data_bands, bs_data_pair,
385             frame_xxx_data->bs_quant_coarse_xxx[set_index],
386             !(frame->independency_flag && (i == 0)) || (set_index > 0), 0, 0, self->ec_flag);
387         if (err) return err;
388 
389         for (pb = 0; pb < data_bands; pb++) {
390           for (j = strides[pb]; j < strides[pb + 1]; j++) {
391             if (datatype == IPD) {
392               if (frame_xxx_data->bs_quant_coarse_xxx[set_index]) {
393                 lastdata[j] = data[set_index + bs_data_pair][pb] & 7;
394               } else {
395                 lastdata[j] = data[set_index + bs_data_pair][pb] & 15;
396               }
397             } else {
398               lastdata[j] = data[set_index + bs_data_pair][pb];
399             }
400           }
401         }
402 
403         old_quant_coarse_xxx = frame_xxx_data->bs_quant_coarse_xxx[set_index];
404 
405         if (bs_data_pair) {
406           frame_xxx_data->bs_quant_coarse_xxx[set_index + 1] =
407               frame_xxx_data->bs_quant_coarse_xxx[set_index];
408           frame_xxx_data->bs_freq_res_stride_xxx[set_index + 1] =
409               frame_xxx_data->bs_freq_res_stride_xxx[set_index];
410         }
411         set_index += bs_data_pair + 1;
412       }
413     }
414   }
415   return err;
416 }
417 
ixheaacd_mps_frame_parsing(ia_mps_dec_state_struct * self,WORD32 usac_independency_flag,ia_bit_buf_struct * bitstream)418 IA_ERRORCODE ixheaacd_mps_frame_parsing(ia_mps_dec_state_struct *self,
419                                         WORD32 usac_independency_flag,
420                                         ia_bit_buf_struct *bitstream) {
421   WORD32 i, bs_frame_type, data_bands, bs_temp_shape_enable, num_of_temp_shape_ch;
422   WORD32 ps, pg, ts, pb;
423   WORD32 env_shape_data[MAX_TIME_SLOTS];
424 
425   WORD32 bits_param_slot = 0;
426 
427   ia_mps_bs_frame *frame = &(self->bs_frame);
428   IA_ERRORCODE err = IA_NO_ERROR;
429 
430   if (self->parse_nxt_frame == 0) return IA_NO_ERROR;
431 
432   self->num_parameter_sets_prev = self->num_parameter_sets;
433 
434   if (self->bs_high_rate_mode) {
435     bs_frame_type = ixheaacd_read_bits_buf(bitstream, 1);
436     self->num_parameter_sets = ixheaacd_read_bits_buf(bitstream, 3) + 1;
437   } else {
438     bs_frame_type = 0;
439     self->num_parameter_sets = 1;
440   }
441 
442   if (self->time_slots == 32)
443     bits_param_slot = 5;
444   else if (self->time_slots == 64)
445     bits_param_slot = 6;
446 
447   if (bs_frame_type) {
448     for (i = 0; i < self->num_parameter_sets; i++) {
449       self->param_slots[i] = ixheaacd_read_bits_buf(bitstream, bits_param_slot);
450     }
451   } else {
452     self->param_slots[0] = self->time_slots - 1;
453   }
454 
455   if (!usac_independency_flag) {
456     frame->independency_flag = ixheaacd_read_bits_buf(bitstream, 1);
457   } else {
458     frame->independency_flag = 1;
459   }
460 
461   err = ixheaacd_mps_ecdata_decoding(self, bitstream, frame->cmp_cld_idx, CLD);
462   if (err) return err;
463 
464   err = ixheaacd_mps_ecdata_decoding(self, bitstream, frame->cmp_icc_idx, ICC);
465   if (err) return err;
466 
467   if (self->config->bs_phase_coding) {
468     self->bs_phase_mode = ixheaacd_read_bits_buf(bitstream, 1);
469 
470     if (!self->bs_phase_mode) {
471       for (pb = 0; pb < self->num_bands_ipd; pb++) {
472         frame->ipd_idx_data_prev[pb] = 0;
473         for (i = 0; i < self->num_parameter_sets; i++) {
474           frame->ipd_idx_data[i][pb] = 0;
475           self->bs_frame.ipd_idx[i][pb] = 0;
476         }
477         self->bs_frame.ipd_idx_prev[pb] = 0;
478       }
479       self->opd_smoothing_mode = 0;
480     } else {
481       self->opd_smoothing_mode = ixheaacd_read_bits_buf(bitstream, 1);
482       err = ixheaacd_mps_ecdata_decoding(self, bitstream, frame->ipd_idx_data,
483                                          IPD);
484       if (err) return err;
485     }
486   }
487 
488   else {
489     self->bs_phase_mode = 0;
490     for (pb = 0; pb < self->num_bands_ipd; pb++) {
491       frame->ipd_idx_data_prev[pb] = 0;
492       for (i = 0; i < self->num_parameter_sets; i++) {
493         frame->ipd_idx_data[i][pb] = 0;
494         self->bs_frame.ipd_idx[i][pb] = 0;
495       }
496       self->bs_frame.ipd_idx_prev[pb] = 0;
497     }
498     self->opd_smoothing_mode = 0;
499   }
500 
501   if (self->bs_high_rate_mode) {
502     for (ps = 0; ps < self->num_parameter_sets; ps++) {
503       frame->bs_smooth_mode[ps] = ixheaacd_read_bits_buf(bitstream, 2);
504       if (frame->bs_smooth_mode[ps] >= 2) {
505         frame->bs_smooth_time[ps] = ixheaacd_read_bits_buf(bitstream, 2);
506       }
507       if (frame->bs_smooth_mode[ps] == 3) {
508         frame->bs_freq_res_stride_smg[ps] =
509             ixheaacd_read_bits_buf(bitstream, 2);
510         data_bands =
511             (self->bs_param_bands - 1) /
512                 ixheaacd_mps_stride_table[frame->bs_freq_res_stride_smg[ps]] +
513             1;
514         for (pg = 0; pg < data_bands; pg++) {
515           frame->bs_smg_data[ps][pg] = ixheaacd_read_bits_buf(bitstream, 1);
516         }
517       }
518     }
519   } else {
520     for (ps = 0; ps < self->num_parameter_sets; ps++) {
521       frame->bs_smooth_mode[ps] = 0;
522     }
523   }
524 
525   for (i = 0; i < 2; i++) {
526     self->temp_shape_enable_ch_stp[i] = 0;
527     self->temp_shape_enable_ch_ges[i] = 0;
528   }
529 
530   self->bs_tsd_enable = 0;
531   if (self->config->bs_temp_shape_config == 3) {
532     self->bs_tsd_enable = ixheaacd_read_bits_buf(bitstream, 1);
533   } else if (self->config->bs_temp_shape_config != 0) {
534     bs_temp_shape_enable = ixheaacd_read_bits_buf(bitstream, 1);
535     if (bs_temp_shape_enable) {
536       num_of_temp_shape_ch = 2;
537       switch (self->config->bs_temp_shape_config) {
538         case 1:
539           for (i = 0; i < num_of_temp_shape_ch; i++) {
540             self->temp_shape_enable_ch_stp[i] =
541                 ixheaacd_read_bits_buf(bitstream, 1);
542           }
543           break;
544         case 2:
545           for (i = 0; i < num_of_temp_shape_ch; i++) {
546             self->temp_shape_enable_ch_ges[i] =
547                 ixheaacd_read_bits_buf(bitstream, 1);
548           }
549           for (i = 0; i < num_of_temp_shape_ch; i++) {
550             if (self->temp_shape_enable_ch_ges[i]) {
551               ixheaacd_mps_huff_decode(bitstream, env_shape_data,
552                                        self->time_slots);
553               for (ts = 0; ts < self->time_slots; ts++) {
554                 self->env_shape_data[i][ts] = (FLOAT32)pow(
555                     2, (FLOAT32)env_shape_data[ts] /
556                                (self->config->bs_env_quant_mode + 2) -
557                            1);
558               }
559             }
560           }
561           break;
562         default:
563           return -1;
564       }
565     }
566   }
567 
568   if (self->bs_tsd_enable) {
569     UWORD16 s[4];
570     UWORD64 s_64;
571     UWORD16 c[5];
572     UWORD64 c_64;
573     UWORD16 b;
574     UWORD16 r[1];
575     static const UWORD16 table_64[] = {
576         6,  11, 16, 20, 23, 27, 30, 33, 35, 38, 40, 42, 44, 46, 48, 49,
577         51, 52, 53, 55, 56, 57, 58, 58, 59, 60, 60, 60, 61, 61, 61, 61};
578     static const UWORD16 table_32[] = {5,  9,  13, 16, 18, 20, 22, 24,
579                                               25, 26, 27, 28, 29, 29, 30, 30};
580     unsigned const short *tab = NULL;
581     WORD32 k;
582     UWORD16 h;
583     WORD32 nbits_tr_slots = 0;
584 
585     if (self->time_slots == 32) {
586       nbits_tr_slots = 4;
587       tab = table_32;
588     } else if (self->time_slots == 64) {
589       nbits_tr_slots = 5;
590       tab = table_64;
591     }
592 
593     self->tsd_num_tr_slots = ixheaacd_read_bits_buf(bitstream, nbits_tr_slots);
594     self->tsd_num_tr_slots++;
595     self->tsd_codeword_len = tab[self->tsd_num_tr_slots - 1];
596 
597     if (self->tsd_codeword_len > 48) {
598       s[3] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len - 48);
599       s_64 = s[3];
600       s[2] = ixheaacd_read_bits_buf(bitstream, 16);
601       s_64 = (s_64 << 16) | s[2];
602       s[1] = ixheaacd_read_bits_buf(bitstream, 16);
603       s_64 = (s_64 << 16) | s[1];
604       s[0] = ixheaacd_read_bits_buf(bitstream, 16);
605       s_64 = (s_64 << 16) | s[0];
606     } else if (self->tsd_codeword_len > 32) {
607       s[3] = 0;
608       s_64 = s[3];
609       s[2] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len - 32);
610       s_64 = (s_64 << 16) | s[2];
611       s[1] = ixheaacd_read_bits_buf(bitstream, 16);
612       s_64 = (s_64 << 16) | s[1];
613       s[0] = ixheaacd_read_bits_buf(bitstream, 16);
614       s_64 = (s_64 << 16) | s[0];
615     } else if (self->tsd_codeword_len > 16) {
616       s[3] = 0;
617       s_64 = s[3];
618       s[2] = 0;
619       s_64 = (s_64 << 16) | s[2];
620       s[1] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len - 16);
621       s_64 = (s_64 << 16) | s[1];
622       s[0] = ixheaacd_read_bits_buf(bitstream, 16);
623       s_64 = (s_64 << 16) | s[0];
624     } else {
625       s[3] = 0;
626       s_64 = s[3];
627       s[2] = 0;
628       s_64 = (s_64 << 16) | s[2];
629       s[1] = 0;
630       s_64 = (s_64 << 16) | s[1];
631       s[0] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len);
632       s_64 = (s_64 << 16) | s[0];
633     }
634 
635     {
636       WORD32 p = self->tsd_num_tr_slots;
637 
638       for (i = 0; i < self->time_slots; i++) self->bs_tsd_sep_data[i] = 0;
639 
640       for (k = self->time_slots - 1; k >= 0; k--) {
641         if (p > k) {
642           for (; k >= 0; k--) self->bs_tsd_sep_data[k] = 1;
643           break;
644         }
645 
646         c[0] = k - p + 1;
647         c_64 = c[0];
648         for (i = 1; i < 5; i++) c[i] = 0;
649 
650         for (h = 2; h <= p; h++) {
651           b = k - p + h;
652           c_64 = c_64 * (b / h);
653           ixheaacd_longmult1(c, b, c, 5);
654           b = h;
655           ixheaacd_longdiv(c, b, c, r, 5);
656         }
657 
658         if (ixheaacd_longcompare(s, c, 4)) {
659           ixheaacd_longsub(s, c, 4, 4);
660           self->bs_tsd_sep_data[k] = 1;
661           p--;
662           if (p == 0) break;
663         }
664       }
665     }
666 
667     for (i = 0; i < self->time_slots; i++) {
668       if (self->bs_tsd_sep_data[i])
669         self->bs_tsd_tr_phase_data[i] = ixheaacd_read_bits_buf(bitstream, 3);
670     }
671   }
672 
673   self->parse_nxt_frame = 0;
674   return err;
675 }
676 
ixheaacd_ld_mps_ecdata_decoding(ia_mps_dec_state_struct * self,ia_bit_buf_struct * it_bit_buff,WORD32 data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],WORD32 datatype,WORD32 start_band)677 static VOID ixheaacd_ld_mps_ecdata_decoding(
678     ia_mps_dec_state_struct *self, ia_bit_buf_struct *it_bit_buff,
679     WORD32 data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS], WORD32 datatype,
680     WORD32 start_band) {
681   WORD32 i, j, pb, data_set, set_index, bs_data_pair, data_bands,
682       old_quant_coarse_xxx;
683   WORD32 strides[MAX_PARAMETER_BANDS + 1] = {0};
684   WORD32 band_stop = 0;
685 
686   WORD32 *lastdata = NULL;
687   ia_mps_data_struct *frame_xxx_data = NULL;
688   WORD32 default_val = 0;
689 
690   ia_mps_bs_frame *frame = &(self->bs_frame);
691 
692   if (datatype == CLD) {
693     frame_xxx_data = &frame->cld_data;
694     lastdata = frame->cmp_cld_idx_prev;
695     band_stop = self->bs_param_bands;
696   } else if (datatype == ICC) {
697     frame_xxx_data = &frame->icc_data;
698     lastdata = frame->cmp_icc_idx_prev;
699     band_stop = self->bs_param_bands;
700   } else if (datatype == IPD) {
701     frame_xxx_data = &frame->ipd_data;
702     lastdata = frame->ipd_idx_data_prev;
703     band_stop = self->num_bands_ipd;
704   } else {
705     frame_xxx_data = &frame->cld_data;
706     lastdata = frame->cmp_cld_idx_prev;
707     band_stop = self->bs_param_bands;
708   }
709   data_set = 0;
710   for (i = 0; i < self->num_parameter_sets; i++) {
711     frame_xxx_data->bs_xxx_data_mode[i] =
712         ixheaacd_read_bits_buf(it_bit_buff, 2);
713     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
714       data_set++;
715     }
716   }
717 
718   set_index = 0;
719   bs_data_pair = 0;
720   old_quant_coarse_xxx = frame_xxx_data->bs_quant_coarse_xxx_prev;
721 
722   for (i = 0; i < self->num_parameter_sets; i++) {
723     if (frame_xxx_data->bs_xxx_data_mode[i] == 0) {
724       for (pb = 0; pb < band_stop; pb++) {
725         lastdata[pb] = default_val;
726       }
727 
728       old_quant_coarse_xxx = 0;
729     }
730 
731     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
732       if (bs_data_pair) {
733         bs_data_pair = 0;
734       } else {
735         bs_data_pair = ixheaacd_read_bits_buf(it_bit_buff, 1);
736         frame_xxx_data->bs_quant_coarse_xxx[set_index] =
737             ixheaacd_read_bits_buf(it_bit_buff, 1);
738         frame_xxx_data->bs_freq_res_stride_xxx[set_index] =
739             ixheaacd_read_bits_buf(it_bit_buff, 2);
740 
741         if (frame_xxx_data->bs_quant_coarse_xxx[set_index] !=
742             old_quant_coarse_xxx) {
743           if (old_quant_coarse_xxx) {
744             ixheaacd_mps_coarse2fine(lastdata, datatype, 0, band_stop - 0);
745           } else {
746             ixheaacd_mps_fine2coarse(lastdata, band_stop);
747           }
748         }
749 
750         data_bands = ixheaacd_mps_getstridemap(
751             frame_xxx_data->bs_freq_res_stride_xxx[set_index], start_band,
752             band_stop, strides);
753         for (pb = 0; pb < data_bands; pb++) {
754           lastdata[pb] = lastdata[strides[pb]];
755         }
756 
757         ixheaacd_mps_ecdatapairdec(
758             it_bit_buff, data, lastdata, datatype, set_index, 0, data_bands, bs_data_pair,
759             frame_xxx_data->bs_quant_coarse_xxx[set_index],
760             !(frame->independency_flag && (i == 0)) || (set_index > 0), 1, 0, self->ec_flag);
761 
762         for (pb = 0; pb < data_bands; pb++) {
763           for (j = strides[pb]; j < strides[pb + 1]; j++) {
764             if (datatype == IPD) {
765               if (frame_xxx_data->bs_quant_coarse_xxx[set_index]) {
766                 lastdata[j] = data[set_index + bs_data_pair][pb] & 7;
767               } else {
768                 lastdata[j] = data[set_index + bs_data_pair][pb] & 15;
769               }
770             } else {
771               lastdata[j] = data[set_index + bs_data_pair][pb];
772             }
773           }
774         }
775 
776         old_quant_coarse_xxx = frame_xxx_data->bs_quant_coarse_xxx[set_index];
777 
778         if (bs_data_pair) {
779           frame_xxx_data->bs_quant_coarse_xxx[set_index + 1] =
780               frame_xxx_data->bs_quant_coarse_xxx[set_index];
781           frame_xxx_data->bs_freq_res_stride_xxx[set_index + 1] =
782               frame_xxx_data->bs_freq_res_stride_xxx[set_index];
783         }
784         set_index += bs_data_pair + 1;
785       }
786     }
787   }
788 }
789 
ixheaacd_ld_mps_frame_parsing(ia_mps_dec_state_struct * self,ia_bit_buf_struct * it_bit_buff)790 IA_ERRORCODE ixheaacd_ld_mps_frame_parsing(
791     ia_mps_dec_state_struct *self, ia_bit_buf_struct *it_bit_buff) {
792   WORD32 i, bs_frame_type, data_bands, bs_temp_shape_enable,
793       num_of_temp_shape_ch;
794   WORD32 ps, pg, ts, ic;
795   WORD32 env_shape_data[MAX_TIME_SLOTS];
796   WORD32 alignment;
797   WORD32 bits_param_slot = 0;
798 
799   ia_mps_bs_frame *frame = &(self->bs_frame);
800   alignment = it_bit_buff->cnt_bits;
801   if (self->parse_nxt_frame == 0) return IA_NO_ERROR;
802 
803   self->num_parameter_sets_prev = self->num_parameter_sets;
804 
805   bs_frame_type = ixheaacd_read_bits_buf(it_bit_buff, 1);
806   self->num_parameter_sets = ixheaacd_read_bits_buf(it_bit_buff, 1) + 1;
807 
808   if (self->time_slots == 32)
809     bits_param_slot = 5;
810   else if (self->time_slots == 64)
811     bits_param_slot = 6;
812   else if (self->time_slots == 8)
813     bits_param_slot = 3;
814   else if (self->time_slots == 16 || self->time_slots == 15)
815     bits_param_slot = 4;
816 
817   if (bs_frame_type) {
818     for (i = 0; i < self->num_parameter_sets; i++) {
819       self->param_slots[i] =
820           ixheaacd_read_bits_buf(it_bit_buff, bits_param_slot);
821     }
822   } else {
823     self->param_slots[0] = self->time_slots - 1;
824   }
825 
826   frame->independency_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
827 
828   for (i = 0; i < self->ldmps_config.num_ott_boxes; i++) {
829     ixheaacd_ld_mps_ecdata_decoding(self, it_bit_buff, frame->cmp_cld_idx, CLD,
830                                     0);
831   }
832 
833   if (self->ldmps_config.bs_one_icc) {
834     ixheaacd_ld_mps_ecdata_decoding(self, it_bit_buff, frame->cmp_icc_idx, ICC,
835                                     0);
836   } else {
837     for (i = 0; i < self->ldmps_config.num_ott_boxes; i++) {
838       if (!self->ldmps_config.ott_mode_lfe[i])
839         ixheaacd_ld_mps_ecdata_decoding(self, it_bit_buff, frame->cmp_icc_idx,
840                                         ICC, 0);
841     }
842   }
843 
844   for (ps = 0; ps < self->num_parameter_sets; ps++) {
845     frame->bs_smooth_mode[ps] = ixheaacd_read_bits_buf(it_bit_buff, 2);
846     if (frame->bs_smooth_mode[ps] >= 2) {
847       frame->bs_smooth_time[ps] = ixheaacd_read_bits_buf(it_bit_buff, 2);
848     }
849     if (frame->bs_smooth_mode[ps] == 3) {
850       frame->bs_freq_res_stride_smg[ps] =
851           ixheaacd_read_bits_buf(it_bit_buff, 2);
852       data_bands =
853           (ixheaacd_freq_res_table_ld[self->ldmps_config.bs_freq_res] - 1) /
854               ixheaacd_mps_stride_table[frame->bs_freq_res_stride_smg[ps]] +
855           1;
856       for (pg = 0; pg < data_bands; pg++) {
857         frame->bs_smg_data[ps][pg] = ixheaacd_read_bits_buf(it_bit_buff, 1);
858       }
859     }
860   }
861 
862   for (i = 0; i < 2; i++) {
863     self->temp_shape_enable_ch_stp[i] = 0;
864     self->temp_shape_enable_ch_ges[i] = 0;
865   }
866 
867   if (self->ldmps_config.bs_temp_shape_config != 0) {
868     bs_temp_shape_enable = ixheaacd_read_bits_buf(it_bit_buff, 1);
869     if (bs_temp_shape_enable) {
870       num_of_temp_shape_ch = 2;
871       switch (self->ldmps_config.bs_temp_shape_config) {
872         case 1:
873           for (i = 0; i < num_of_temp_shape_ch; i++) {
874             self->temp_shape_enable_ch_stp[i] =
875                 ixheaacd_read_bits_buf(it_bit_buff, 1);
876           }
877           break;
878         case 2:
879           for (i = 0; i < num_of_temp_shape_ch; i++) {
880             self->temp_shape_enable_ch_ges[i] =
881                 ixheaacd_read_bits_buf(it_bit_buff, 1);
882           }
883           for (i = 0; i < num_of_temp_shape_ch; i++) {
884             if (self->temp_shape_enable_ch_ges[i]) {
885               ixheaacd_mps_huff_decode(it_bit_buff, env_shape_data,
886                                        self->time_slots);
887               for (ts = 0; ts < self->time_slots; ts++) {
888                 self->env_shape_data[i][ts] = (FLOAT32)pow(
889                     2, (FLOAT32)env_shape_data[ts] /
890                                (self->ldmps_config.bs_env_quant_mode + 2) -
891                            1);
892               }
893             }
894           }
895           break;
896         default:
897           if (self->ec_flag == 0) {
898           return IA_FATAL_ERROR;
899       }
900           break;
901       }
902     }
903   }
904 
905   if (self->ldmps_config.bs_arbitrary_downmix != 0) {
906     for (ic = 0; ic < self->ldmps_config.num_input_channels; ic++) {
907       ixheaacd_ld_mps_ecdata_decoding(self, it_bit_buff, frame->cmp_cld_idx,
908                                       CLD, 0);
909     }
910   }
911 
912   ixheaacd_byte_align(it_bit_buff, &alignment);
913 
914   while (it_bit_buff->cnt_bits > 8) {
915     ixheaacd_read_bits_buf(it_bit_buff, 8);
916   }
917   ixheaacd_read_bits_buf(it_bit_buff, it_bit_buff->cnt_bits);
918   return IA_NO_ERROR;
919 }
920 
ixheaacd_mps_createmapping(WORD32 map[MAX_PARAMETER_BANDS+1],WORD32 band_start,WORD32 band_stop,WORD32 ch_fac)921 static VOID ixheaacd_mps_createmapping(WORD32 map[MAX_PARAMETER_BANDS + 1],
922                                        WORD32 band_start, WORD32 band_stop,
923                                        WORD32 ch_fac) {
924   WORD32 input_bands, out_bands, bands_achived, bands_diff, incr, k, i;
925   WORD32 vdk[MAX_PARAMETER_BANDS + 1];
926   input_bands = band_stop - band_start;
927   out_bands = (input_bands - 1) / ch_fac + 1;
928   if (out_bands < 1) {
929     out_bands = 1;
930   }
931 
932   bands_achived = out_bands * ch_fac;
933   bands_diff = input_bands - bands_achived;
934   for (i = 0; i < out_bands; i++) {
935     vdk[i] = ch_fac;
936   }
937 
938   if (bands_diff > 0) {
939     incr = -1;
940     k = out_bands - 1;
941   } else {
942     incr = 1;
943     k = 0;
944   }
945 
946   while (bands_diff != 0) {
947     vdk[k] = vdk[k] - incr;
948     k = k + incr;
949     bands_diff = bands_diff + incr;
950     if (k >= out_bands) {
951       if (bands_diff > 0) {
952         k = out_bands - 1;
953       } else if (bands_diff < 0) {
954         k = 0;
955       }
956     }
957   }
958   map[0] = band_start;
959   for (i = 0; i < out_bands; i++) {
960     map[i + 1] = map[i] + vdk[i];
961   }
962 }
963 
ixheaacd_mps_mapfrequency(WORD32 * in,WORD32 * out,WORD32 * map,WORD32 data_bands)964 static VOID ixheaacd_mps_mapfrequency(WORD32 *in, WORD32 *out, WORD32 *map,
965                                       WORD32 data_bands) {
966   WORD32 i, j, band_start, band_stop, value;
967   WORD32 start_band_0 = map[0];
968 
969   for (i = 0; i < data_bands; i++) {
970     value = in[i + start_band_0];
971 
972     band_start = map[i];
973     band_stop = map[i + 1];
974     for (j = band_start; j < band_stop; j++) {
975       out[j] = value;
976     }
977   }
978 }
979 
ixheaacd_mps_de_quantize(WORD32 value,WORD32 param_type)980 static FLOAT32 ixheaacd_mps_de_quantize(WORD32 value, WORD32 param_type) {
981   switch (param_type) {
982     case CLD:
983       return ixheaacd_cld_de_quant_table[(value + 15)];
984 
985     case ICC:
986       return ixheaacd_icc_de_quant_table[value];
987 
988     case IPD:
989       return ixheaacd_ipd_de_quant_table[(value & 15)];
990 
991     default:
992       return 0;
993       return 0;
994   }
995 }
996 
ixheaacd_mps_mapindexdata(ia_mps_dec_state_struct * self,ia_mps_data_struct * frame_xxx_data,FLOAT32 out_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],WORD32 out_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],WORD32 cmp_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],WORD32 idx_prev[MAX_PARAMETER_BANDS],WORD32 param_type)997 static IA_ERRORCODE ixheaacd_mps_mapindexdata(
998     ia_mps_dec_state_struct *self, ia_mps_data_struct *frame_xxx_data,
999     FLOAT32 out_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
1000     WORD32 out_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
1001     WORD32 cmp_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
1002     WORD32 idx_prev[MAX_PARAMETER_BANDS], WORD32 param_type) {
1003   WORD32 interpolate_local[MAX_PARAMETER_SETS_MPS] = {0};
1004   WORD32 map[MAX_PARAMETER_BANDS + 1];
1005 
1006   WORD32 set_index, i, band, parm_slot;
1007   WORD32 data_bands, ch_fac;
1008   WORD32 ps;
1009 
1010   WORD32 i1, i2, x1, xi, x2;
1011   WORD32 band_start = 0;
1012   WORD32 ext_frame_flag = self->ext_frame_flag;
1013   WORD32 *param_slots = self->param_slots;
1014   WORD32 num_parameter_sets = self->num_parameter_sets;
1015   WORD32 band_stop = self->bs_param_bands;
1016   WORD32 default_val = 0;
1017 
1018   set_index = 0;
1019 
1020   for (i = 0; i < num_parameter_sets; i++) {
1021     if (frame_xxx_data->bs_xxx_data_mode[i] == 0) {
1022       frame_xxx_data->quant_coarse_xxx_flag[i] = 0;
1023       for (band = band_start; band < band_stop; band++) {
1024         out_idx_data[i][band] = default_val;
1025       }
1026       for (band = band_start; band < band_stop; band++) {
1027         idx_prev[band] = out_idx_data[i][band];
1028       }
1029 
1030       frame_xxx_data->bs_quant_coarse_xxx_prev = 0;
1031     }
1032 
1033     if (frame_xxx_data->bs_xxx_data_mode[i] == 1) {
1034       for (band = band_start; band < band_stop; band++) {
1035         out_idx_data[i][band] = idx_prev[band];
1036       }
1037       frame_xxx_data->quant_coarse_xxx_flag[i] =
1038           frame_xxx_data->bs_quant_coarse_xxx_prev;
1039     }
1040 
1041     if (frame_xxx_data->bs_xxx_data_mode[i] == 2) {
1042       for (band = band_start; band < band_stop; band++) {
1043         out_idx_data[i][band] = idx_prev[band];
1044       }
1045       frame_xxx_data->quant_coarse_xxx_flag[i] =
1046           frame_xxx_data->bs_quant_coarse_xxx_prev;
1047       interpolate_local[i] = 1;
1048     } else {
1049       interpolate_local[i] = 0;
1050     }
1051 
1052     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
1053       parm_slot = i;
1054       ch_fac =
1055           ixheaacd_mps_stride_table[frame_xxx_data
1056                                         ->bs_freq_res_stride_xxx[set_index]];
1057       data_bands = (band_stop - band_start - 1) / ch_fac + 1;
1058       ixheaacd_mps_createmapping(map, band_start, band_stop, ch_fac);
1059       ixheaacd_mps_mapfrequency(&cmp_idx_data[set_index][0],
1060                                 &out_idx_data[parm_slot][0], map, data_bands);
1061 
1062       for (band = band_start; band < band_stop; band++) {
1063         idx_prev[band] = out_idx_data[parm_slot][band];
1064       }
1065 
1066       frame_xxx_data->bs_quant_coarse_xxx_prev =
1067           frame_xxx_data->bs_quant_coarse_xxx[set_index];
1068       frame_xxx_data->quant_coarse_xxx_flag[i] =
1069           frame_xxx_data->bs_quant_coarse_xxx[set_index];
1070 
1071       set_index++;
1072     }
1073   }
1074 
1075   for (i = 0; i < num_parameter_sets; i++) {
1076     if (frame_xxx_data->quant_coarse_xxx_flag[i] == 1) {
1077       ixheaacd_mps_coarse2fine(out_idx_data[i], param_type, band_start,
1078                                band_stop - band_start);
1079       frame_xxx_data->quant_coarse_xxx_flag[i] = 0;
1080     }
1081   }
1082 
1083   i1 = -1;
1084   x1 = 0;
1085   i2 = 0;
1086   for (i = 0; i < num_parameter_sets; i++) {
1087     if (interpolate_local[i] != 1) {
1088       i1 = i;
1089     }
1090     i2 = i;
1091     while (interpolate_local[i2] == 1) {
1092       i2++;
1093     }
1094     if (i1 == -1) {
1095       x1 = 0;
1096       i1 = 0;
1097     } else {
1098       x1 = param_slots[i1];
1099     }
1100     xi = param_slots[i];
1101     x2 = param_slots[i2];
1102 
1103     if (interpolate_local[i] == 1) {
1104       if (i2 < num_parameter_sets) {
1105         if (self->ec_flag == 0) {
1106           return IA_FATAL_ERROR;
1107         }
1108       }
1109 
1110       for (band = band_start; band < band_stop; band++) {
1111         WORD32 yi, y1, y2;
1112         yi = 0;
1113         y1 = out_idx_data[i1][band];
1114         y2 = out_idx_data[i2][band];
1115         if (param_type == IPD) {
1116           if (y2 - y1 > 8) y1 += 16;
1117           if (y1 - y2 > 8) y2 += 16;
1118 
1119           if (x2 != x1) yi = (y1 + (xi - x1) * (y2 - y1) / (x2 - x1)) % 16;
1120         } else {
1121           if (x2 != x1) {
1122             yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
1123           }
1124         }
1125         out_idx_data[i][band] = yi;
1126       }
1127     }
1128   }
1129 
1130   for (ps = 0; ps < num_parameter_sets; ps++) {
1131     for (band = band_start; band < band_stop; band++) {
1132       if (param_type == CLD) {
1133         out_idx_data[ps][band] = bound_check(out_idx_data[ps][band], -15, 15);
1134       } else if (param_type == ICC)
1135       {
1136         out_idx_data[ps][band] = bound_check(out_idx_data[ps][band], 0, 7);
1137       }
1138       out_data[ps][band] =
1139           ixheaacd_mps_de_quantize(out_idx_data[ps][band], param_type);
1140     }
1141   }
1142 
1143   if (ext_frame_flag) {
1144     for (band = band_start; band < band_stop; band++) {
1145       out_data[num_parameter_sets][band] =
1146           out_data[num_parameter_sets - 1][band];
1147       out_idx_data[num_parameter_sets][band] =
1148           out_idx_data[num_parameter_sets - 1][band];
1149     }
1150   }
1151   return IA_NO_ERROR;
1152 }
1153 
ixheaacd_mps_dec_and_mapframeott(ia_mps_dec_state_struct * self)1154 static IA_ERRORCODE ixheaacd_mps_dec_and_mapframeott(
1155     ia_mps_dec_state_struct *self) {
1156   ia_mps_bs_frame *cur_bit_stream_ptr = &(self->bs_frame);
1157   IA_ERRORCODE err_code = 0;
1158 
1159   err_code = ixheaacd_mps_mapindexdata(
1160       self, &cur_bit_stream_ptr->cld_data, self->cld_data,
1161       cur_bit_stream_ptr->cld_idx, cur_bit_stream_ptr->cmp_cld_idx,
1162       cur_bit_stream_ptr->cld_idx_pre, CLD);
1163   if (err_code != IA_NO_ERROR) return err_code;
1164   err_code = ixheaacd_mps_mapindexdata(
1165       self, &cur_bit_stream_ptr->icc_data, self->icc_data,
1166       cur_bit_stream_ptr->icc_idx, cur_bit_stream_ptr->cmp_icc_idx,
1167       cur_bit_stream_ptr->icc_idx_pre, ICC);
1168   if (err_code != IA_NO_ERROR) return err_code;
1169   if ((self->config->bs_phase_coding)) {
1170     err_code = ixheaacd_mps_mapindexdata(
1171         self, &cur_bit_stream_ptr->ipd_data, self->ipd_data,
1172         cur_bit_stream_ptr->ipd_idx, cur_bit_stream_ptr->ipd_idx_data,
1173         cur_bit_stream_ptr->ipd_idx_prev, IPD);
1174 
1175     if (err_code != IA_NO_ERROR) return err_code;
1176   }
1177   return IA_NO_ERROR;
1178 }
1179 
ixheaacd_mps_dec_and_mapframesmg(ia_mps_dec_state_struct * self)1180 static VOID ixheaacd_mps_dec_and_mapframesmg(ia_mps_dec_state_struct *self) {
1181   WORD32 ps, pb, pg, ch_fac, data_bands, param_band_start, param_band_stop,
1182       group_to_band[MAX_PARAMETER_BANDS + 1];
1183   ia_mps_bs_frame *frame = &(self->bs_frame);
1184   for (ps = 0; ps < self->num_parameter_sets; ps++) {
1185     switch (frame->bs_smooth_mode[ps]) {
1186       case 0:
1187         self->smoothing_time[ps] = 256;
1188         self->inv_smoothing_time[ps] = (FLOAT32)(1.0f / 256.0f);
1189 
1190         for (pb = 0; pb < self->bs_param_bands; pb++) {
1191           self->smoothing_data[ps][pb] = 0;
1192         }
1193         break;
1194 
1195       case 1:
1196         if (ps > 0) {
1197           self->smoothing_time[ps] = self->smoothing_time[ps - 1];
1198           self->inv_smoothing_time[ps] = self->inv_smoothing_time[ps - 1];
1199         } else {
1200           self->smoothing_time[ps] = self->smoothing_filt_state.prev_smg_time;
1201           self->inv_smoothing_time[ps] =
1202               self->smoothing_filt_state.inv_prev_smg_time;
1203         }
1204 
1205         for (pb = 0; pb < self->bs_param_bands; pb++) {
1206           if (ps > 0)
1207             self->smoothing_data[ps][pb] = self->smoothing_data[ps - 1][pb];
1208           else
1209             self->smoothing_data[ps][pb] =
1210                 self->smoothing_filt_state.prev_smg_data[pb];
1211         }
1212         break;
1213 
1214       case 2:
1215         self->smoothing_time[ps] =
1216             ixheaacd_smoothing_time_table[frame->bs_smooth_time[ps]];
1217         self->inv_smoothing_time[ps] =
1218             ixheaacd_inverse_smoothing_time_table[frame->bs_smooth_time[ps]];
1219         for (pb = 0; pb < self->bs_param_bands; pb++) {
1220           self->smoothing_data[ps][pb] = 1;
1221         }
1222         break;
1223 
1224       case 3:
1225         self->smoothing_time[ps] =
1226             ixheaacd_smoothing_time_table[frame->bs_smooth_time[ps]];
1227         self->inv_smoothing_time[ps] =
1228             ixheaacd_inverse_smoothing_time_table[frame->bs_smooth_time[ps]];
1229 
1230         ch_fac = ixheaacd_mps_stride_table[frame->bs_freq_res_stride_smg[ps]];
1231         data_bands = (self->bs_param_bands - 1) / ch_fac + 1;
1232         ixheaacd_mps_createmapping(group_to_band, 0, self->bs_param_bands,
1233                                    ch_fac);
1234         for (pg = 0; pg < data_bands; pg++) {
1235           param_band_start = group_to_band[pg];
1236           param_band_stop = group_to_band[pg + 1];
1237           for (pb = param_band_start; pb < param_band_stop; pb++) {
1238             self->smoothing_data[ps][pb] = frame->bs_smg_data[ps][pg];
1239           }
1240         }
1241         break;
1242     }
1243   }
1244 
1245   self->smoothing_filt_state.prev_smg_time =
1246       self->smoothing_time[self->num_parameter_sets - 1];
1247   self->smoothing_filt_state.inv_prev_smg_time =
1248       self->inv_smoothing_time[self->num_parameter_sets - 1];
1249   for (pb = 0; pb < self->bs_param_bands; pb++) {
1250     self->smoothing_filt_state.prev_smg_data[pb] =
1251         self->smoothing_data[self->num_parameter_sets - 1][pb];
1252   }
1253 
1254   if (self->ext_frame_flag) {
1255     self->smoothing_time[self->num_parameter_sets] =
1256         self->smoothing_time[self->num_parameter_sets - 1];
1257     self->inv_smoothing_time[self->num_parameter_sets] =
1258         self->inv_smoothing_time[self->num_parameter_sets - 1];
1259     for (pb = 0; pb < self->bs_param_bands; pb++) {
1260       self->smoothing_data[self->num_parameter_sets][pb] =
1261           self->smoothing_data[self->num_parameter_sets - 1][pb];
1262     }
1263   }
1264 }
1265 
ixheaacd_mps_frame_decode(ia_mps_dec_state_struct * self)1266 IA_ERRORCODE ixheaacd_mps_frame_decode(ia_mps_dec_state_struct *self) {
1267   WORD32 i;
1268   IA_ERRORCODE err_code = 0;
1269   if (self->ldmps_config.ldmps_present_flag != 1)
1270     if (self->parse_nxt_frame == 1) return err_code;
1271   self->ext_frame_flag = 0;
1272   if (self->param_slots[self->num_parameter_sets - 1] != self->time_slots - 1) {
1273     self->ext_frame_flag = 1;
1274   }
1275 
1276   err_code = ixheaacd_mps_dec_and_mapframeott(self);
1277   if (err_code != IA_NO_ERROR) return err_code;
1278   ixheaacd_mps_dec_and_mapframesmg(self);
1279 
1280   if (self->ext_frame_flag) {
1281     self->num_parameter_sets++;
1282     self->param_slots[self->num_parameter_sets - 1] = self->time_slots - 1;
1283   }
1284   self->param_slot_diff[0] = self->param_slots[0] + 1;
1285   if (MAX_TIME_SLOTS < (self->param_slot_diff[0])) {
1286     if (self->ec_flag == 0) {
1287       return -1;
1288     } else {
1289       self->param_slot_diff[0] = MAX_TIME_SLOTS;
1290     }
1291   }
1292   self->inv_param_slot_diff[0] = (FLOAT32)1 / self->param_slot_diff[0];
1293   self->inv_param_slot_diff_Q30[0] =
1294       (WORD32)floor(self->inv_param_slot_diff[0] * 1073741824 + 0.5);
1295   for (i = 1; i < self->num_parameter_sets; i++) {
1296     self->param_slot_diff[i] = self->param_slots[i] - self->param_slots[i - 1];
1297     if (MAX_TIME_SLOTS < (self->param_slot_diff[0] + self->param_slot_diff[i])) {
1298       if (self->ec_flag == 0) {
1299         return -1;
1300       } else {
1301         self->param_slot_diff[i] = 1;
1302         self->inv_param_slot_diff[i] = 1;
1303       }
1304     }
1305     self->inv_param_slot_diff[i] = (FLOAT32)1 / self->param_slot_diff[i];
1306     self->inv_param_slot_diff_Q30[i] =
1307         (WORD32)floor(self->inv_param_slot_diff[i] * 1073741824 + 0.5);
1308   }
1309   return IA_NO_ERROR;
1310 }
1311 
ixheaacd_mps_header_decode(ia_mps_dec_state_struct * self)1312 WORD32 ixheaacd_mps_header_decode(ia_mps_dec_state_struct *self) {
1313   WORD32 samp_freq;
1314   WORD32 sampling_rate_tbl[] = {96000, 88200, 64000, 48000, 44100, 32000,
1315                                 24000, 22050, 16000, 12000, 11025, 8000,
1316                                 7350,  0,     0,     0};
1317 
1318   if (self->ldmps_config.ldmps_present_flag == 1)
1319     self->time_slots = self->frame_length + 1;
1320   else
1321     self->time_slots = self->frame_length;
1322 
1323   self->bs_param_bands = ixheaacd_freq_res_table[self->config->bs_freq_res];
1324 
1325   if (self->ldmps_config.ldmps_present_flag == 1) {
1326     if (self->ldmps_config.bs_sampling_freq_index == 15) {
1327       samp_freq = self->ldmps_config.bs_fampling_frequency;
1328     } else {
1329       samp_freq = sampling_rate_tbl[self->ldmps_config.bs_sampling_freq_index];
1330     }
1331 
1332     if (samp_freq < 27713.0) {
1333       self->qmf_band_count = 32;
1334     } else if (samp_freq >= 55426.0) {
1335       self->qmf_band_count = 128;
1336     } else {
1337       self->qmf_band_count = 64;
1338     }
1339   }
1340 
1341   if (self->object_type == AOT_ER_AAC_ELD ||
1342       self->object_type == AOT_ER_AAC_LD) {
1343     self->bs_param_bands =
1344         ixheaacd_freq_res_table_ld[self->config->bs_freq_res];
1345     self->hyb_band_count_max = self->qmf_band_count;
1346   } else
1347     self->hyb_band_count_max = self->qmf_band_count - QMF_BANDS_TO_HYBRID + 10;
1348 
1349   if (self->object_type == AOT_ER_AAC_ELD ||
1350       self->object_type == AOT_ER_AAC_LD) {
1351     switch (self->bs_param_bands) {
1352       case 4:
1353 
1354         self->hyb_band_to_processing_band_table =
1355             ixheaacd_hybrid_band_64_to_processing_band_4_map;
1356         break;
1357       case 5:
1358 
1359         self->hyb_band_to_processing_band_table =
1360             ixheaacd_hybrid_band_64_to_processing_band_5_map;
1361         break;
1362       case 7:
1363 
1364         self->hyb_band_to_processing_band_table =
1365             ixheaacd_hybrid_band_64_to_processing_band_7_map;
1366         break;
1367       case 9:
1368 
1369         self->hyb_band_to_processing_band_table =
1370             ixheaacd_hybrid_band_64_to_processing_band_9_map;
1371         break;
1372       case 12:
1373 
1374         self->hyb_band_to_processing_band_table =
1375             ixheaacd_hybrid_band_64_to_processing_band_12_map;
1376         break;
1377       case 15:
1378 
1379         self->hyb_band_to_processing_band_table =
1380             ixheaacd_hybrid_band_64_to_processing_band_15_map;
1381         break;
1382       case 23:
1383 
1384         self->hyb_band_to_processing_band_table =
1385             ixheaacd_hybrid_band_64_to_processing_band_23_map;
1386         break;
1387       default:
1388         self->hyb_band_to_processing_band_table = NULL;
1389         return -1;
1390         break;
1391     }
1392   } else {
1393     switch (self->bs_param_bands) {
1394       case 4:
1395         self->hyb_band_to_processing_band_table =
1396             ixheaacd_hybrid_band_71_to_processing_band_4_map;
1397         break;
1398       case 5:
1399         self->hyb_band_to_processing_band_table =
1400             ixheaacd_hybrid_band_71_to_processing_band_5_map;
1401         break;
1402       case 7:
1403         self->hyb_band_to_processing_band_table =
1404             ixheaacd_hybrid_band_71_to_processing_band_7_map;
1405         break;
1406       case 10:
1407         self->hyb_band_to_processing_band_table =
1408             ixheaacd_hybrid_band_71_to_processing_band_10_map;
1409         break;
1410       case 14:
1411         self->hyb_band_to_processing_band_table =
1412             ixheaacd_hybrid_band_71_to_processing_band_14_map;
1413         break;
1414       case 20:
1415         self->hyb_band_to_processing_band_table =
1416             ixheaacd_hybrid_band_71_to_processing_band_20_map;
1417         break;
1418       case 28:
1419         self->hyb_band_to_processing_band_table =
1420             ixheaacd_hybrid_band_71_to_processing_band_28_map;
1421         break;
1422       default:
1423         self->hyb_band_to_processing_band_table = NULL;
1424         return -1;
1425         break;
1426     }
1427   }
1428   self->in_ch_count = 1;
1429   self->out_ch_count = 2;
1430 
1431   self->input_gain =
1432       ixheaacd_mps_clip_gain_table[self->config->bs_fixed_gain_dmx];
1433 
1434   if (self->config->bs_ott_bands_phase_present) {
1435     self->num_bands_ipd = self->config->bs_ott_bands_phase;
1436   } else {
1437     if (!(self->object_type == AOT_ER_AAC_ELD ||
1438           self->object_type == AOT_ER_AAC_LD)) {
1439       switch (self->bs_param_bands) {
1440         case 4:
1441         case 5:
1442           self->num_bands_ipd = 2;
1443           break;
1444         case 7:
1445           self->num_bands_ipd = 3;
1446           break;
1447         case 10:
1448           self->num_bands_ipd = 5;
1449           break;
1450         case 14:
1451           self->num_bands_ipd = 7;
1452           break;
1453         case 20:
1454         case 28:
1455           self->num_bands_ipd = 10;
1456           break;
1457         default:
1458           return -1;
1459           break;
1460       }
1461     }
1462   }
1463 
1464   if (self->residual_coding) {
1465     self->num_bands_ipd = max(self->bs_residual_bands, self->num_bands_ipd);
1466     self->max_res_bands = 0;
1467     if (self->bs_residual_present) {
1468       self->res_bands = self->bs_residual_bands;
1469       if (self->res_bands > self->max_res_bands) {
1470         self->max_res_bands = self->res_bands;
1471       }
1472     } else {
1473       self->res_bands = 0;
1474     }
1475   }
1476 
1477   if (self->num_bands_ipd > MAX_PARAMETER_BANDS) return -1;
1478 
1479   self->dir_sig_count = 1;
1480   self->decor_sig_count = 1;
1481 
1482   self->bs_high_rate_mode = self->config->bs_high_rate_mode;
1483   self->pre_mix_req = 1;
1484 
1485   return 0;
1486 }
1487