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 <assert.h>
22 #include <stdio.h>
23 #include "ixheaacd_type_def.h"
24 #include "ixheaacd_bitbuffer.h"
25 #include "ixheaacd_config.h"
26 #include "ixheaacd_mps_polyphase.h"
27 #include "ixheaacd_mps_dec.h"
28 #include "ixheaacd_mps_interface.h"
29 #include "ixheaacd_mps_nlc_dec.h"
30 #include "ixheaacd_mps_hybfilter.h"
31 #include "ixheaacd_error_standards.h"
32 
33 #define min(a, b) ((a) < (b) ? (a) : (b))
34 
35 #define max(a, b) ((a) > (b) ? (a) : (b))
36 
37 static const int ixheaacd_freq_res_table[] = {0, 28, 20, 14, 10, 7, 5, 4};
38 
39 static const int
40     ixheaacd_hybrid_band_71_to_processing_band_4_map[MAX_HYBRID_BANDS_MPS] = {
41         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
42         2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
43         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
44 
45 static const int
46     ixheaacd_hybrid_band_71_to_processing_band_5_map[MAX_HYBRID_BANDS_MPS] = {
47         0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
48         3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
49         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
50 
51 static const int
52     ixheaacd_hybrid_band_71_to_processing_band_7_map[MAX_HYBRID_BANDS_MPS] = {
53         0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
54         5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
55         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
56 
57 static const int
58     ixheaacd_hybrid_band_71_to_processing_band_10_map[MAX_HYBRID_BANDS_MPS] = {
59         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8,
60         8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
61         9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
62 
63 static const int
64     ixheaacd_hybrid_band_71_to_processing_band_14_map[MAX_HYBRID_BANDS_MPS] = {
65         0,  0,  0,  0,  1,  1,  2,  3,  4,  4,  5,  6,  6,  7,  7,  8,  8,  8,
66         9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
67         12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
68         13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
69 
70 const int
71     ixheaacd_hybrid_band_71_to_processing_band_20_map[MAX_HYBRID_BANDS_MPS] = {
72         1,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 14,
73         15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18,
74         18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
75         19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19};
76 
77 const int
78     ixheaacd_hybrid_band_71_to_processing_band_28_map[MAX_HYBRID_BANDS_MPS] = {
79         1,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
80         16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23,
81         23, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26,
82         26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27};
83 
84 static const float ixheaacd_mps_clip_gain_table[] = {
85     1.000000f, 1.189207f, 1.414213f, 1.681792f,
86     2.000000f, 2.378414f, 2.828427f, 4.000000f};
87 
88 static const int ixheaacd_mps_stride_table[] = {1, 2, 5, 28};
89 
90 static const float ixheaacd_cld_de_quant_table[] = {
91     -150.0, -45.0, -40.0, -35.0, -30.0, -25.0, -22.0, -19.0,
92     -16.0,  -13.0, -10.0, -8.0,  -6.0,  -4.0,  -2.0,  0.0,
93     2.0,    4.0,   6.0,   8.0,   10.0,  13.0,  16.0,  19.0,
94     22.0,   25.0,  30.0,  35.0,  40.0,  45.0,  150.0};
95 
96 static const float ixheaacd_icc_de_quant_table[] = {
97     1.0000f, 0.9370f, 0.84118f, 0.60092f, 0.36764f, 0.0f, -0.5890f, -0.9900f};
98 
99 const float ixheaacd_ipd_de_quant_table[] = {
100     0.f,          0.392699082f, 0.785398163f, 1.178097245f,
101     1.570796327f, 1.963495408f, 2.35619449f,  2.748893572f,
102     3.141592654f, 3.534291735f, 3.926990817f, 4.319689899f,
103     4.71238898f,  5.105088062f, 5.497787144f, 5.890486225f};
104 const int ixheaacd_ipd_de_quant_table_q28[] = {
105     0,          105414360,  210828720,  316243072, 421657440,  527071776,
106     632486144,  737900480,  843314880,  948729216, 1054143552, 1159557888,
107     1264972288, 1370386688, 1475800960, 1581215360};
108 static const int ixheaacd_smoothing_time_table[] = {64, 128, 256, 512};
109 
110 static const int ixheaacd_inverse_smoothing_time_table_q30[] = {
111     16777216, 8388608, 4194304, 2097152};
112 
bound_check(WORD32 var,WORD32 lower_bound,WORD32 upper_bound)113 static WORD32 bound_check(WORD32 var, WORD32 lower_bound, WORD32 upper_bound) {
114   var = min(var, upper_bound);
115   var = max(var, lower_bound);
116   return var;
117 }
118 
ixheaacd_longmult1(unsigned short a[],unsigned short b,unsigned short d[],int len)119 static VOID ixheaacd_longmult1(unsigned short a[], unsigned short b,
120                                unsigned short d[], int len) {
121   int k;
122   UWORD32 tmp;
123   UWORD32 b0 = (UWORD32)b;
124 
125   tmp = ((UWORD32)a[0]) * b0;
126   d[0] = (unsigned short)tmp;
127 
128   for (k = 1; k < len; k++) {
129     tmp = (tmp >> 16) + ((UWORD32)a[k]) * b0;
130     d[k] = (unsigned short)tmp;
131   }
132 }
133 
ixheaacd_longdiv(unsigned short b[],unsigned short a,unsigned short d[],unsigned short * pr,int len)134 static VOID ixheaacd_longdiv(unsigned short b[], unsigned short a,
135                              unsigned short d[], unsigned short *pr, int len) {
136   UWORD32 r;
137   UWORD32 tmp;
138   UWORD32 temp;
139   int k;
140 
141   assert(a != 0);
142 
143   r = 0;
144 
145   for (k = len - 1; k >= 0; k--) {
146     tmp = ((UWORD32)b[k]) + (r << 16);
147 
148     if (tmp) {
149       d[k] = (unsigned short)(tmp / a);
150       temp = d[k] * a;
151       r = tmp - temp;
152     } else {
153       d[k] = 0;
154     }
155   }
156   *pr = (unsigned short)r;
157 }
158 
ixheaacd_longsub(unsigned short a[],unsigned short b[],int lena,int lenb)159 static VOID ixheaacd_longsub(unsigned short a[], unsigned short b[], int lena,
160                              int lenb) {
161   int h;
162   WORD32 carry = 0;
163 
164   assert(lena >= lenb);
165   for (h = 0; h < lenb; h++) {
166     carry = carry + (WORD32)(a[h] - b[h]);
167     a[h] = (unsigned short)carry;
168     carry = carry >> 16;
169   }
170 
171   for (; h < lena; h++) {
172     carry = ((UWORD32)a[h]) + carry;
173     a[h] = (unsigned short)carry;
174     carry = carry >> 16;
175   }
176 
177   assert(carry == 0);
178   return;
179 }
180 
ixheaacd_longcompare(unsigned short a[],unsigned short b[],int len)181 static int ixheaacd_longcompare(unsigned short a[], unsigned short b[],
182                                 int len) {
183   int i;
184 
185   for (i = len - 1; i > 0; i--) {
186     if (a[i] != b[i]) break;
187   }
188   return (a[i] >= b[i]) ? 1 : 0;
189 }
190 
ixheaacd_mps_coarse2fine(int * data,WORD32 data_type,int band_start,int ixheaacd_num_bands)191 static VOID ixheaacd_mps_coarse2fine(int *data, WORD32 data_type,
192                                      int band_start, int ixheaacd_num_bands) {
193   int i;
194 
195   for (i = band_start; i < band_start + ixheaacd_num_bands; i++) {
196     data[i] <<= 1;
197   }
198 
199   if (data_type == CLD) {
200     for (i = band_start; i < band_start + ixheaacd_num_bands; i++) {
201       if (data[i] == -14)
202         data[i] = -15;
203       else if (data[i] == 14)
204         data[i] = 15;
205     }
206   }
207 }
208 
ixheaacd_mps_fine2coarse(int * data,int ixheaacd_num_bands)209 static VOID ixheaacd_mps_fine2coarse(int *data, int ixheaacd_num_bands) {
210   int i;
211 
212   for (i = 0; i < ixheaacd_num_bands; i++) {
213     data[i] /= 2;
214   }
215 }
216 
ixheaacd_mps_getstridemap(int freq_res_stride,int band_start,int band_stop,int * strides)217 static int ixheaacd_mps_getstridemap(int freq_res_stride, int band_start,
218                                      int band_stop, int *strides) {
219   int i, pb, ch_fac, data_bands, start_offset;
220 
221   ch_fac = ixheaacd_mps_stride_table[freq_res_stride];
222   data_bands = (band_stop - band_start - 1) / ch_fac + 1;
223 
224   strides[0] = band_start;
225   for (pb = 1; pb <= data_bands; pb++) {
226     strides[pb] = strides[pb - 1] + ch_fac;
227   }
228   start_offset = 0;
229   while (strides[data_bands] > band_stop) {
230     if (start_offset < data_bands)
231       start_offset++;
232     else
233       start_offset = 1;
234 
235     for (i = start_offset; i <= data_bands; i++) {
236       strides[i]--;
237     }
238   }
239 
240   return data_bands;
241 }
242 
ixheaacd_mps_ecdata_decoding(ia_mps_dec_state_struct * self,ia_handle_bit_buf_struct bitstream,int data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],int datatype)243 static IA_ERRORCODE ixheaacd_mps_ecdata_decoding(
244     ia_mps_dec_state_struct *self, ia_handle_bit_buf_struct bitstream,
245     int data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS], int datatype) {
246   int i, j, pb, set_index, bs_data_pair, data_bands, old_quant_coarse_xxx;
247   int strides[MAX_PARAMETER_BANDS + 1] = {0};
248   int band_stop = 0;
249 
250   int *lastdata = NULL;
251   ia_mps_data_struct *frame_xxx_data = NULL;
252   int default_val = 0;
253   IA_ERRORCODE err = IA_NO_ERROR;
254 
255   ia_mps_bs_frame *frame = &(self->bs_frame);
256 
257   if (datatype == 0) {
258     frame_xxx_data = &frame->cld_data;
259     lastdata = frame->cmp_cld_idx_prev;
260     band_stop = self->bs_param_bands;
261   } else if (datatype == 1) {
262     frame_xxx_data = &frame->icc_data;
263     lastdata = frame->cmp_icc_idx_prev;
264     band_stop = self->bs_param_bands;
265   } else if (datatype == 2) {
266     frame_xxx_data = &frame->ipd_data;
267     lastdata = frame->ipd_idx_data_prev;
268     band_stop = self->num_bands_ipd;
269   } else {
270     frame_xxx_data = &frame->cld_data;
271     lastdata = frame->cmp_cld_idx_prev;
272     band_stop = self->bs_param_bands;
273   }
274 
275   for (i = 0; i < self->num_parameter_sets; i++) {
276     frame_xxx_data->bs_xxx_data_mode[i] = ixheaacd_read_bits_buf(bitstream, 2);
277   }
278 
279   set_index = 0;
280   bs_data_pair = 0;
281   old_quant_coarse_xxx = frame_xxx_data->bs_quant_coarse_xxx_prev;
282 
283   for (i = 0; i < self->num_parameter_sets; i++) {
284     if (frame_xxx_data->bs_xxx_data_mode[i] == 0) {
285       for (pb = 0; pb < band_stop; pb++) {
286         lastdata[pb] = default_val;
287       }
288 
289       old_quant_coarse_xxx = 0;
290     }
291 
292     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
293       if (bs_data_pair) {
294         bs_data_pair = 0;
295       } else {
296         bs_data_pair = ixheaacd_read_bits_buf(bitstream, 1);
297         frame_xxx_data->bs_quant_coarse_xxx[set_index] =
298             ixheaacd_read_bits_buf(bitstream, 1);
299         frame_xxx_data->bs_freq_res_stride_xxx[set_index] =
300             ixheaacd_read_bits_buf(bitstream, 2);
301 
302         if (frame_xxx_data->bs_quant_coarse_xxx[set_index] !=
303             old_quant_coarse_xxx) {
304           if (old_quant_coarse_xxx) {
305             ixheaacd_mps_coarse2fine(lastdata, datatype, 0, band_stop - 0);
306           } else {
307             ixheaacd_mps_fine2coarse(lastdata, band_stop);
308           }
309         }
310 
311         data_bands = ixheaacd_mps_getstridemap(
312             frame_xxx_data->bs_freq_res_stride_xxx[set_index], 0, band_stop,
313             strides);
314 
315         for (pb = 0; pb < data_bands; pb++) {
316           lastdata[pb] = lastdata[strides[pb]];
317         }
318 
319         err = ixheaacd_mps_ecdatapairdec(
320             bitstream, data, lastdata, datatype, set_index, data_bands,
321             bs_data_pair, frame_xxx_data->bs_quant_coarse_xxx[set_index],
322             frame->independency_flag && (i == 0));
323         if (err) return err;
324 
325         for (pb = 0; pb < data_bands; pb++) {
326           for (j = strides[pb]; j < strides[pb + 1]; j++) {
327             if (datatype == IPD) {
328               if (frame_xxx_data->bs_quant_coarse_xxx[set_index]) {
329                 lastdata[j] = data[set_index + bs_data_pair][pb] & 7;
330               } else {
331                 lastdata[j] = data[set_index + bs_data_pair][pb] & 15;
332               }
333             } else {
334               lastdata[j] = data[set_index + bs_data_pair][pb];
335             }
336           }
337         }
338 
339         old_quant_coarse_xxx = frame_xxx_data->bs_quant_coarse_xxx[set_index];
340 
341         if (bs_data_pair) {
342           frame_xxx_data->bs_quant_coarse_xxx[set_index + 1] =
343               frame_xxx_data->bs_quant_coarse_xxx[set_index];
344           frame_xxx_data->bs_freq_res_stride_xxx[set_index + 1] =
345               frame_xxx_data->bs_freq_res_stride_xxx[set_index];
346         }
347         set_index += bs_data_pair + 1;
348       }
349     }
350   }
351   return err;
352 }
353 
ixheaacd_mps_frame_parsing(ia_mps_dec_state_struct * self,int usac_independency_flag,ia_handle_bit_buf_struct bitstream)354 IA_ERRORCODE ixheaacd_mps_frame_parsing(ia_mps_dec_state_struct *self,
355                                         int usac_independency_flag,
356                                         ia_handle_bit_buf_struct bitstream) {
357   int i, bs_frame_type, data_bands, bs_temp_shape_enable, num_of_temp_shape_ch;
358   int ps, pg, ts, pb;
359   int env_shape_data[MAX_TIME_SLOTS];
360 
361   int bits_param_slot = 0;
362 
363   ia_mps_bs_frame *frame = &(self->bs_frame);
364   IA_ERRORCODE err = IA_NO_ERROR;
365 
366   if (self->parse_nxt_frame == 0) return IA_NO_ERROR;
367 
368   self->num_parameter_sets_prev = self->num_parameter_sets;
369 
370   if (self->bs_high_rate_mode) {
371     bs_frame_type = ixheaacd_read_bits_buf(bitstream, 1);
372     self->num_parameter_sets = ixheaacd_read_bits_buf(bitstream, 3) + 1;
373   } else {
374     bs_frame_type = 0;
375     self->num_parameter_sets = 1;
376   }
377 
378   if (self->time_slots == 32)
379     bits_param_slot = 5;
380   else if (self->time_slots == 64)
381     bits_param_slot = 6;
382 
383   if (bs_frame_type) {
384     for (i = 0; i < self->num_parameter_sets; i++) {
385       self->param_slots[i] = ixheaacd_read_bits_buf(bitstream, bits_param_slot);
386     }
387   } else {
388     self->param_slots[0] = self->time_slots - 1;
389   }
390 
391   if (!usac_independency_flag) {
392     frame->independency_flag = ixheaacd_read_bits_buf(bitstream, 1);
393   } else {
394     frame->independency_flag = 1;
395   }
396 
397   err = ixheaacd_mps_ecdata_decoding(self, bitstream, frame->cmp_cld_idx, CLD);
398   if (err) return err;
399 
400   err = ixheaacd_mps_ecdata_decoding(self, bitstream, frame->cmp_icc_idx, ICC);
401   if (err) return err;
402 
403   if (self->config->bs_phase_coding) {
404     self->bs_phase_mode = ixheaacd_read_bits_buf(bitstream, 1);
405 
406     if (!self->bs_phase_mode) {
407       for (pb = 0; pb < self->num_bands_ipd; pb++) {
408         frame->ipd_idx_data_prev[pb] = 0;
409         for (i = 0; i < self->num_parameter_sets; i++) {
410           frame->ipd_idx_data[i][pb] = 0;
411           self->bs_frame.ipd_idx[i][pb] = 0;
412         }
413         self->bs_frame.ipd_idx_prev[pb] = 0;
414       }
415       self->opd_smoothing_mode = 0;
416     } else {
417       self->opd_smoothing_mode = ixheaacd_read_bits_buf(bitstream, 1);
418       err = ixheaacd_mps_ecdata_decoding(self, bitstream, frame->ipd_idx_data,
419                                          IPD);
420       if (err) return err;
421     }
422   }
423 
424   else {
425     self->bs_phase_mode = 0;
426     for (pb = 0; pb < self->num_bands_ipd; pb++) {
427       frame->ipd_idx_data_prev[pb] = 0;
428       for (i = 0; i < self->num_parameter_sets; i++) {
429         frame->ipd_idx_data[i][pb] = 0;
430         self->bs_frame.ipd_idx[i][pb] = 0;
431       }
432       self->bs_frame.ipd_idx_prev[pb] = 0;
433     }
434     self->opd_smoothing_mode = 0;
435   }
436 
437   if (self->bs_high_rate_mode) {
438     for (ps = 0; ps < self->num_parameter_sets; ps++) {
439       frame->bs_smooth_mode[ps] = ixheaacd_read_bits_buf(bitstream, 2);
440       if (frame->bs_smooth_mode[ps] >= 2) {
441         frame->bs_smooth_time[ps] = ixheaacd_read_bits_buf(bitstream, 2);
442       }
443       if (frame->bs_smooth_mode[ps] == 3) {
444         frame->bs_freq_res_stride_smg[ps] =
445             ixheaacd_read_bits_buf(bitstream, 2);
446         data_bands =
447             (self->bs_param_bands - 1) /
448                 ixheaacd_mps_stride_table[frame->bs_freq_res_stride_smg[ps]] +
449             1;
450         for (pg = 0; pg < data_bands; pg++) {
451           frame->bs_smg_data[ps][pg] = ixheaacd_read_bits_buf(bitstream, 1);
452         }
453       }
454     }
455   } else {
456     for (ps = 0; ps < self->num_parameter_sets; ps++) {
457       frame->bs_smooth_mode[ps] = 0;
458     }
459   }
460 
461   for (i = 0; i < 2; i++) {
462     self->temp_shape_enable_ch_stp[i] = 0;
463     self->temp_shape_enable_ch_ges[i] = 0;
464   }
465 
466   self->bs_tsd_enable = 0;
467   if (self->config->bs_temp_shape_config == 3) {
468     self->bs_tsd_enable = ixheaacd_read_bits_buf(bitstream, 1);
469   } else if (self->config->bs_temp_shape_config != 0) {
470     bs_temp_shape_enable = ixheaacd_read_bits_buf(bitstream, 1);
471     if (bs_temp_shape_enable) {
472       num_of_temp_shape_ch = 2;
473       switch (self->config->bs_temp_shape_config) {
474         case 1:
475           for (i = 0; i < num_of_temp_shape_ch; i++) {
476             self->temp_shape_enable_ch_stp[i] =
477                 ixheaacd_read_bits_buf(bitstream, 1);
478           }
479           break;
480         case 2:
481           for (i = 0; i < num_of_temp_shape_ch; i++) {
482             self->temp_shape_enable_ch_ges[i] =
483                 ixheaacd_read_bits_buf(bitstream, 1);
484           }
485           for (i = 0; i < num_of_temp_shape_ch; i++) {
486             if (self->temp_shape_enable_ch_ges[i]) {
487               ixheaacd_mps_huff_decode(bitstream, env_shape_data,
488                                        self->time_slots);
489               for (ts = 0; ts < self->time_slots; ts++) {
490                 self->env_shape_data[i][ts] = (float)pow(
491                     2, (float)env_shape_data[ts] /
492                                (self->config->bs_env_quant_mode + 2) -
493                            1);
494               }
495             }
496           }
497           break;
498         default:
499           assert(0);
500       }
501     }
502   }
503 
504   if (self->bs_tsd_enable) {
505     unsigned short s[4];
506     UWORD64 s_64;
507     unsigned short c[5];
508     UWORD64 c_64;
509     unsigned short b;
510     unsigned short r[1];
511     static const unsigned short table_64[] = {
512         6,  11, 16, 20, 23, 27, 30, 33, 35, 38, 40, 42, 44, 46, 48, 49,
513         51, 52, 53, 55, 56, 57, 58, 58, 59, 60, 60, 60, 61, 61, 61, 61};
514     static const unsigned short table_32[] = {5,  9,  13, 16, 18, 20, 22, 24,
515                                               25, 26, 27, 28, 29, 29, 30, 30};
516     unsigned const short *tab = NULL;
517     int k;
518     unsigned short h;
519     WORD32 nbits_tr_slots = 0;
520 
521     if (self->time_slots == 32) {
522       nbits_tr_slots = 4;
523       tab = table_32;
524     } else if (self->time_slots == 64) {
525       nbits_tr_slots = 5;
526       tab = table_64;
527     }
528 
529     self->tsd_num_tr_slots = ixheaacd_read_bits_buf(bitstream, nbits_tr_slots);
530     self->tsd_num_tr_slots++;
531     self->tsd_codeword_len = tab[self->tsd_num_tr_slots - 1];
532 
533     if (self->tsd_codeword_len > 48) {
534       s[3] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len - 48);
535       s_64 = s[3];
536       s[2] = ixheaacd_read_bits_buf(bitstream, 16);
537       s_64 = (s_64 << 16) | s[2];
538       s[1] = ixheaacd_read_bits_buf(bitstream, 16);
539       s_64 = (s_64 << 16) | s[1];
540       s[0] = ixheaacd_read_bits_buf(bitstream, 16);
541       s_64 = (s_64 << 16) | s[0];
542     } else if (self->tsd_codeword_len > 32) {
543       s[3] = 0;
544       s_64 = s[3];
545       s[2] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len - 32);
546       s_64 = (s_64 << 16) | s[2];
547       s[1] = ixheaacd_read_bits_buf(bitstream, 16);
548       s_64 = (s_64 << 16) | s[1];
549       s[0] = ixheaacd_read_bits_buf(bitstream, 16);
550       s_64 = (s_64 << 16) | s[0];
551     } else if (self->tsd_codeword_len > 16) {
552       s[3] = 0;
553       s_64 = s[3];
554       s[2] = 0;
555       s_64 = (s_64 << 16) | s[2];
556       s[1] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len - 16);
557       s_64 = (s_64 << 16) | s[1];
558       s[0] = ixheaacd_read_bits_buf(bitstream, 16);
559       s_64 = (s_64 << 16) | s[0];
560     } else {
561       s[3] = 0;
562       s_64 = s[3];
563       s[2] = 0;
564       s_64 = (s_64 << 16) | s[2];
565       s[1] = 0;
566       s_64 = (s_64 << 16) | s[1];
567       s[0] = ixheaacd_read_bits_buf(bitstream, self->tsd_codeword_len);
568       s_64 = (s_64 << 16) | s[0];
569     }
570 
571     {
572       int p = self->tsd_num_tr_slots;
573 
574       for (i = 0; i < self->time_slots; i++) self->bs_tsd_sep_data[i] = 0;
575 
576       for (k = self->time_slots - 1; k >= 0; k--) {
577         if (p > k) {
578           for (; k >= 0; k--) self->bs_tsd_sep_data[k] = 1;
579           break;
580         }
581 
582         c[0] = k - p + 1;
583         c_64 = c[0];
584         for (i = 1; i < 5; i++) c[i] = 0;
585 
586         for (h = 2; h <= p; h++) {
587           b = k - p + h;
588           c_64 = c_64 * (b / h);
589           ixheaacd_longmult1(c, b, c, 5);
590           b = h;
591           ixheaacd_longdiv(c, b, c, r, 5);
592         }
593 
594         if (ixheaacd_longcompare(s, c, 4)) {
595           ixheaacd_longsub(s, c, 4, 4);
596           self->bs_tsd_sep_data[k] = 1;
597           p--;
598           if (p == 0) break;
599         }
600       }
601     }
602 
603     for (i = 0; i < self->time_slots; i++) {
604       if (self->bs_tsd_sep_data[i])
605         self->bs_tsd_tr_phase_data[i] = ixheaacd_read_bits_buf(bitstream, 3);
606     }
607   }
608 
609   self->parse_nxt_frame = 0;
610   return err;
611 }
612 
ixheaacd_mps_createmapping(int map[MAX_PARAMETER_BANDS+1],int band_start,int band_stop,int ch_fac)613 static VOID ixheaacd_mps_createmapping(int map[MAX_PARAMETER_BANDS + 1],
614                                        int band_start, int band_stop,
615                                        int ch_fac) {
616   int input_bands, out_bands, bands_achived, bands_diff, incr, k, i;
617   int vdk[MAX_PARAMETER_BANDS + 1];
618   input_bands = band_stop - band_start;
619   out_bands = (input_bands - 1) / ch_fac + 1;
620   if (out_bands < 1) {
621     out_bands = 1;
622   }
623 
624   bands_achived = out_bands * ch_fac;
625   bands_diff = input_bands - bands_achived;
626   for (i = 0; i < out_bands; i++) {
627     vdk[i] = ch_fac;
628   }
629 
630   if (bands_diff > 0) {
631     incr = -1;
632     k = out_bands - 1;
633   } else {
634     incr = 1;
635     k = 0;
636   }
637 
638   while (bands_diff != 0) {
639     vdk[k] = vdk[k] - incr;
640     k = k + incr;
641     bands_diff = bands_diff + incr;
642     if (k >= out_bands) {
643       if (bands_diff > 0) {
644         k = out_bands - 1;
645       } else if (bands_diff < 0) {
646         k = 0;
647       }
648     }
649   }
650   map[0] = band_start;
651   for (i = 0; i < out_bands; i++) {
652     map[i + 1] = map[i] + vdk[i];
653   }
654 }
655 
ixheaacd_mps_mapfrequency(int * in,int * out,int * map,int data_bands)656 static VOID ixheaacd_mps_mapfrequency(int *in, int *out, int *map,
657                                       int data_bands) {
658   int i, j, band_start, band_stop, value;
659   int start_band_0 = map[0];
660 
661   for (i = 0; i < data_bands; i++) {
662     value = in[i + start_band_0];
663 
664     band_start = map[i];
665     band_stop = map[i + 1];
666     for (j = band_start; j < band_stop; j++) {
667       out[j] = value;
668     }
669   }
670 }
671 
ixheaacd_mps_de_quantize(int value,int param_type)672 static float ixheaacd_mps_de_quantize(int value, int param_type) {
673   switch (param_type) {
674     case CLD:
675       return ixheaacd_cld_de_quant_table[value + 15];
676 
677     case ICC:
678       return ixheaacd_icc_de_quant_table[value];
679 
680     case IPD:
681       return ixheaacd_ipd_de_quant_table[(value & 15)];
682 
683     default:
684       assert(0);
685       return 0.0;
686   }
687 }
688 
ixheaacd_mps_mapindexdata(ia_mps_dec_state_struct * self,ia_mps_data_struct * frame_xxx_data,float out_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],int out_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],int cmp_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],int idx_prev[MAX_PARAMETER_BANDS],int param_type)689 static WORD32 ixheaacd_mps_mapindexdata(
690     ia_mps_dec_state_struct *self, ia_mps_data_struct *frame_xxx_data,
691     float out_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
692     int out_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
693     int cmp_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
694     int idx_prev[MAX_PARAMETER_BANDS], int param_type) {
695   int interpolate_local[MAX_PARAMETER_SETS_MPS] = {0};
696   int map[MAX_PARAMETER_BANDS + 1];
697 
698   int set_index, i, band, parm_slot;
699   int data_bands, ch_fac;
700   int ps;
701 
702   int i1, i2, x1, xi, x2;
703   int band_start = 0;
704   int ext_frame_flag = self->ext_frame_flag;
705   int *param_slots = self->param_slots;
706   int num_parameter_sets = self->num_parameter_sets;
707   int band_stop = self->bs_param_bands;
708   int default_val = 0;
709 
710   set_index = 0;
711 
712   for (i = 0; i < num_parameter_sets; i++) {
713     if (frame_xxx_data->bs_xxx_data_mode[i] == 0) {
714       frame_xxx_data->quant_coarse_xxx_flag[i] = 0;
715       for (band = band_start; band < band_stop; band++) {
716         out_idx_data[i][band] = default_val;
717       }
718       for (band = band_start; band < band_stop; band++) {
719         idx_prev[band] = out_idx_data[i][band];
720       }
721 
722       frame_xxx_data->bs_quant_coarse_xxx_prev = 0;
723     }
724 
725     if (frame_xxx_data->bs_xxx_data_mode[i] == 1) {
726       for (band = band_start; band < band_stop; band++) {
727         out_idx_data[i][band] = idx_prev[band];
728       }
729       frame_xxx_data->quant_coarse_xxx_flag[i] =
730           frame_xxx_data->bs_quant_coarse_xxx_prev;
731     }
732 
733     if (frame_xxx_data->bs_xxx_data_mode[i] == 2) {
734       for (band = band_start; band < band_stop; band++) {
735         out_idx_data[i][band] = idx_prev[band];
736       }
737       frame_xxx_data->quant_coarse_xxx_flag[i] =
738           frame_xxx_data->bs_quant_coarse_xxx_prev;
739       interpolate_local[i] = 1;
740     } else {
741       interpolate_local[i] = 0;
742     }
743 
744     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
745       parm_slot = i;
746       ch_fac =
747           ixheaacd_mps_stride_table[frame_xxx_data
748                                         ->bs_freq_res_stride_xxx[set_index]];
749       data_bands = (band_stop - band_start - 1) / ch_fac + 1;
750       ixheaacd_mps_createmapping(map, band_start, band_stop, ch_fac);
751       ixheaacd_mps_mapfrequency(&cmp_idx_data[set_index][0],
752                                 &out_idx_data[parm_slot][0], map, data_bands);
753 
754       for (band = band_start; band < band_stop; band++) {
755         idx_prev[band] = out_idx_data[parm_slot][band];
756       }
757 
758       frame_xxx_data->bs_quant_coarse_xxx_prev =
759           frame_xxx_data->bs_quant_coarse_xxx[set_index];
760       frame_xxx_data->quant_coarse_xxx_flag[i] =
761           frame_xxx_data->bs_quant_coarse_xxx[set_index];
762 
763       set_index++;
764     }
765   }
766 
767   for (i = 0; i < num_parameter_sets; i++) {
768     if (frame_xxx_data->quant_coarse_xxx_flag[i] == 1) {
769       ixheaacd_mps_coarse2fine(out_idx_data[i], param_type, band_start,
770                                band_stop - band_start);
771       frame_xxx_data->quant_coarse_xxx_flag[i] = 0;
772     }
773   }
774 
775   i1 = -1;
776   x1 = 0;
777   i2 = 0;
778   for (i = 0; i < num_parameter_sets; i++) {
779     if (interpolate_local[i] != 1) {
780       i1 = i;
781     }
782     i2 = i;
783     while (interpolate_local[i2] == 1) {
784       i2++;
785     }
786     if (i1 == -1) {
787       x1 = 0;
788       i1 = 0;
789     } else {
790       x1 = param_slots[i1];
791     }
792     xi = param_slots[i];
793     x2 = param_slots[i2];
794 
795     if (interpolate_local[i] == 1) {
796       if (i2 < num_parameter_sets) {
797         return -1;
798       }
799       for (band = band_start; band < band_stop; band++) {
800         int yi, y1, y2;
801         yi = 0;
802         y1 = out_idx_data[i1][band];
803         y2 = out_idx_data[i2][band];
804         if (param_type == IPD) {
805           if (y2 - y1 > 8) y1 += 16;
806           if (y1 - y2 > 8) y2 += 16;
807 
808           if (x2 != x1) {
809             yi = (y1 + (xi - x1) * (y2 - y1) / (x2 - x1)) % 16;
810           }
811         } else {
812           if (x2 != x1) {
813             yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
814           }
815         }
816         out_idx_data[i][band] = yi;
817       }
818     }
819   }
820 
821   for (ps = 0; ps < num_parameter_sets; ps++) {
822     for (band = band_start; band < band_stop; band++) {
823       if (param_type == CLD) {
824         out_idx_data[ps][band] = bound_check(out_idx_data[ps][band], -15, 15);
825       } else if (param_type == ICC)  // param_type is ICC
826       {
827         out_idx_data[ps][band] = bound_check(out_idx_data[ps][band], 0, 7);
828       }
829       out_data[ps][band] =
830           ixheaacd_mps_de_quantize(out_idx_data[ps][band], param_type);
831     }
832   }
833 
834   if (ext_frame_flag) {
835     for (band = band_start; band < band_stop; band++) {
836       out_data[num_parameter_sets][band] =
837           out_data[num_parameter_sets - 1][band];
838       out_idx_data[num_parameter_sets][band] =
839           out_idx_data[num_parameter_sets - 1][band];
840     }
841   }
842 
843   return 0;
844 }
845 
ixheaacd_mps_dec_and_mapframeott(ia_mps_dec_state_struct * self)846 static WORD32 ixheaacd_mps_dec_and_mapframeott(ia_mps_dec_state_struct *self) {
847   ia_mps_bs_frame *cur_bit_stream_ptr = &(self->bs_frame);
848   WORD32 err_code = 0;
849 
850   err_code = ixheaacd_mps_mapindexdata(
851       self, &cur_bit_stream_ptr->cld_data, self->cld_data,
852       cur_bit_stream_ptr->cld_idx, cur_bit_stream_ptr->cmp_cld_idx,
853       cur_bit_stream_ptr->cld_idx_pre, CLD);
854   if (err_code != 0) return err_code;
855 
856   err_code = ixheaacd_mps_mapindexdata(
857       self, &cur_bit_stream_ptr->icc_data, self->icc_data,
858       cur_bit_stream_ptr->icc_idx, cur_bit_stream_ptr->cmp_icc_idx,
859       cur_bit_stream_ptr->icc_idx_pre, ICC);
860   if (err_code != 0) return err_code;
861   if ((self->config->bs_phase_coding)) {
862     err_code = ixheaacd_mps_mapindexdata(
863         self, &cur_bit_stream_ptr->ipd_data, self->ipd_data,
864         cur_bit_stream_ptr->ipd_idx, cur_bit_stream_ptr->ipd_idx_data,
865         cur_bit_stream_ptr->ipd_idx_prev, IPD);
866 
867     if (err_code != 0) return err_code;
868   }
869 
870   return 0;
871 }
872 
ixheaacd_mps_dec_and_mapframesmg(ia_mps_dec_state_struct * self)873 static VOID ixheaacd_mps_dec_and_mapframesmg(ia_mps_dec_state_struct *self) {
874   int ps, pb, pg, ch_fac, data_bands, param_band_start, param_band_stop,
875       group_to_band[MAX_PARAMETER_BANDS + 1];
876   ia_mps_bs_frame *frame = &(self->bs_frame);
877   for (ps = 0; ps < self->num_parameter_sets; ps++) {
878     switch (frame->bs_smooth_mode[ps]) {
879       case 0:
880         self->smoothing_time[ps] = 256;
881         self->inv_smoothing_time[ps] = 4194304;
882 
883         for (pb = 0; pb < self->bs_param_bands; pb++) {
884           self->smoothing_data[ps][pb] = 0;
885         }
886         break;
887 
888       case 1:
889         if (ps > 0) {
890           self->smoothing_time[ps] = self->smoothing_time[ps - 1];
891           self->inv_smoothing_time[ps] = self->inv_smoothing_time[ps - 1];
892         } else {
893           self->smoothing_time[ps] = self->smoothing_filt_state.prev_smg_time;
894           self->inv_smoothing_time[ps] =
895               self->smoothing_filt_state.inv_prev_smg_time;
896         }
897 
898         for (pb = 0; pb < self->bs_param_bands; pb++) {
899           if (ps > 0)
900             self->smoothing_data[ps][pb] = self->smoothing_data[ps - 1][pb];
901           else
902             self->smoothing_data[ps][pb] =
903                 self->smoothing_filt_state.prev_smg_data[pb];
904         }
905         break;
906 
907       case 2:
908         self->smoothing_time[ps] =
909             ixheaacd_smoothing_time_table[frame->bs_smooth_time[ps]];
910         self->inv_smoothing_time[ps] =
911             ixheaacd_inverse_smoothing_time_table_q30[frame
912                                                           ->bs_smooth_time[ps]];
913         for (pb = 0; pb < self->bs_param_bands; pb++) {
914           self->smoothing_data[ps][pb] = 1;
915         }
916         break;
917 
918       case 3:
919         self->smoothing_time[ps] =
920             ixheaacd_smoothing_time_table[frame->bs_smooth_time[ps]];
921         self->inv_smoothing_time[ps] =
922             ixheaacd_inverse_smoothing_time_table_q30[frame
923                                                           ->bs_smooth_time[ps]];
924 
925         ch_fac = ixheaacd_mps_stride_table[frame->bs_freq_res_stride_smg[ps]];
926         data_bands = (self->bs_param_bands - 1) / ch_fac + 1;
927         ixheaacd_mps_createmapping(group_to_band, 0, self->bs_param_bands,
928                                    ch_fac);
929         for (pg = 0; pg < data_bands; pg++) {
930           param_band_start = group_to_band[pg];
931           param_band_stop = group_to_band[pg + 1];
932           for (pb = param_band_start; pb < param_band_stop; pb++) {
933             self->smoothing_data[ps][pb] = frame->bs_smg_data[ps][pg];
934           }
935         }
936         break;
937     }
938   }
939 
940   self->smoothing_filt_state.prev_smg_time =
941       self->smoothing_time[self->num_parameter_sets - 1];
942   self->smoothing_filt_state.inv_prev_smg_time =
943       self->inv_smoothing_time[self->num_parameter_sets - 1];
944   for (pb = 0; pb < self->bs_param_bands; pb++) {
945     self->smoothing_filt_state.prev_smg_data[pb] =
946         self->smoothing_data[self->num_parameter_sets - 1][pb];
947   }
948 
949   if (self->ext_frame_flag) {
950     self->smoothing_time[self->num_parameter_sets] =
951         self->smoothing_time[self->num_parameter_sets - 1];
952     self->inv_smoothing_time[self->num_parameter_sets] =
953         self->inv_smoothing_time[self->num_parameter_sets - 1];
954     for (pb = 0; pb < self->bs_param_bands; pb++) {
955       self->smoothing_data[self->num_parameter_sets][pb] =
956           self->smoothing_data[self->num_parameter_sets - 1][pb];
957     }
958   }
959 }
960 
ixheaacd_mps_frame_decode(ia_mps_dec_state_struct * self)961 WORD32 ixheaacd_mps_frame_decode(ia_mps_dec_state_struct *self) {
962   int i;
963   WORD32 err_code = 0;
964   if (self->parse_nxt_frame == 1) return 0;
965 
966   self->ext_frame_flag = 0;
967   if (self->param_slots[self->num_parameter_sets - 1] != self->time_slots - 1) {
968     self->ext_frame_flag = 1;
969   }
970 
971   err_code = ixheaacd_mps_dec_and_mapframeott(self);
972 
973   if (err_code != 0) return err_code;
974 
975   ixheaacd_mps_dec_and_mapframesmg(self);
976 
977   if (self->ext_frame_flag) {
978     self->num_parameter_sets++;
979     self->param_slots[self->num_parameter_sets - 1] = self->time_slots - 1;
980   }
981   self->param_slot_diff[0] = self->param_slots[0] + 1;
982   self->inv_param_slot_diff[0] = (float)1 / self->param_slot_diff[0];
983   self->inv_param_slot_diff_Q30[0] =
984       (int)floor(self->inv_param_slot_diff[0] * 1073741824 + 0.5);
985   for (i = 1; i < self->num_parameter_sets; i++) {
986     self->param_slot_diff[i] = self->param_slots[i] - self->param_slots[i - 1];
987     self->inv_param_slot_diff[i] = (float)1 / self->param_slot_diff[i];
988     self->inv_param_slot_diff_Q30[i] =
989         (int)floor(self->inv_param_slot_diff[i] * 1073741824 + 0.5);
990   }
991 
992   return 0;
993 }
994 
ixheaacd_mps_header_decode(ia_mps_dec_state_struct * self)995 WORD32 ixheaacd_mps_header_decode(ia_mps_dec_state_struct *self) {
996   self->time_slots = self->frame_length;
997   self->frame_len = self->time_slots * self->qmf_band_count;
998   self->bs_param_bands = ixheaacd_freq_res_table[self->config->bs_freq_res];
999 
1000   self->hyb_band_count = self->qmf_band_count - QMF_BANDS_TO_HYBRID + 10;
1001 
1002   switch (self->bs_param_bands) {
1003     case 4:
1004 
1005       self->hyb_band_to_processing_band_table =
1006           ixheaacd_hybrid_band_71_to_processing_band_4_map;
1007       break;
1008     case 5:
1009 
1010       self->hyb_band_to_processing_band_table =
1011           ixheaacd_hybrid_band_71_to_processing_band_5_map;
1012       break;
1013     case 7:
1014 
1015       self->hyb_band_to_processing_band_table =
1016           ixheaacd_hybrid_band_71_to_processing_band_7_map;
1017       break;
1018     case 10:
1019 
1020       self->hyb_band_to_processing_band_table =
1021           ixheaacd_hybrid_band_71_to_processing_band_10_map;
1022       break;
1023     case 14:
1024 
1025       self->hyb_band_to_processing_band_table =
1026           ixheaacd_hybrid_band_71_to_processing_band_14_map;
1027       break;
1028     case 20:
1029 
1030       self->hyb_band_to_processing_band_table =
1031           ixheaacd_hybrid_band_71_to_processing_band_20_map;
1032       break;
1033     case 28:
1034 
1035       self->hyb_band_to_processing_band_table =
1036           ixheaacd_hybrid_band_71_to_processing_band_28_map;
1037       break;
1038     default:
1039       self->hyb_band_to_processing_band_table = NULL;
1040       return -1;
1041       break;
1042   };
1043 
1044   self->in_ch_count = 1;
1045   self->out_ch_count = 2;
1046 
1047   self->input_gain =
1048       ixheaacd_mps_clip_gain_table[self->config->bs_fixed_gain_dmx];
1049 
1050   if (self->config->bs_ott_bands_phase_present) {
1051     self->num_bands_ipd = self->config->bs_ott_bands_phase;
1052   } else {
1053     switch (self->bs_param_bands) {
1054       case 4:
1055       case 5:
1056         self->num_bands_ipd = 2;
1057         break;
1058       case 7:
1059         self->num_bands_ipd = 3;
1060         break;
1061       case 10:
1062         self->num_bands_ipd = 5;
1063         break;
1064       case 14:
1065         self->num_bands_ipd = 7;
1066         break;
1067       case 20:
1068       case 28:
1069         self->num_bands_ipd = 10;
1070         break;
1071       default:
1072         assert(0);
1073         break;
1074     }
1075   }
1076 
1077   if (self->residual_coding) {
1078     self->num_bands_ipd = max(self->bs_residual_bands, self->num_bands_ipd);
1079     self->max_res_bands = 0;
1080     if (self->bs_residual_present) {
1081       self->res_bands = self->bs_residual_bands;
1082       if (self->res_bands > self->max_res_bands) {
1083         self->max_res_bands = self->res_bands;
1084       }
1085     } else {
1086       self->res_bands = 0;
1087     }
1088   }
1089 
1090   if (self->num_bands_ipd > MAX_PARAMETER_BANDS) return -1;
1091 
1092   self->dir_sig_count = 1;
1093   self->decor_sig_count = 1;
1094 
1095   self->bs_high_rate_mode = self->config->bs_high_rate_mode;
1096 
1097   return 0;
1098 }
1099