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