• 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 
ixheaacd_longmult1(unsigned short a[],unsigned short b,unsigned short d[],int len)113 static VOID ixheaacd_longmult1(unsigned short a[], unsigned short b,
114                                unsigned short d[], int len) {
115   int k;
116   UWORD32 tmp;
117   UWORD32 b0 = (UWORD32)b;
118 
119   tmp = ((UWORD32)a[0]) * b0;
120   d[0] = (unsigned short)tmp;
121 
122   for (k = 1; k < len; k++) {
123     tmp = (tmp >> 16) + ((UWORD32)a[k]) * b0;
124     d[k] = (unsigned short)tmp;
125   }
126 }
127 
ixheaacd_longdiv(unsigned short b[],unsigned short a,unsigned short d[],unsigned short * pr,int len)128 static VOID ixheaacd_longdiv(unsigned short b[], unsigned short a,
129                              unsigned short d[], unsigned short *pr, int len) {
130   UWORD32 r;
131   UWORD32 tmp;
132   UWORD32 temp;
133   int k;
134 
135   assert(a != 0);
136 
137   r = 0;
138 
139   for (k = len - 1; k >= 0; k--) {
140     tmp = ((UWORD32)b[k]) + (r << 16);
141 
142     if (tmp) {
143       d[k] = (unsigned short)(tmp / a);
144       temp = d[k] * a;
145       r = tmp - temp;
146     } else {
147       d[k] = 0;
148     }
149   }
150   *pr = (unsigned short)r;
151 }
152 
ixheaacd_longsub(unsigned short a[],unsigned short b[],int lena,int lenb)153 static VOID ixheaacd_longsub(unsigned short a[], unsigned short b[], int lena,
154                              int lenb) {
155   int h;
156   WORD32 carry = 0;
157 
158   assert(lena >= lenb);
159   for (h = 0; h < lenb; h++) {
160     carry = carry + (WORD32)(a[h] - b[h]);
161     a[h] = (unsigned short)carry;
162     carry = carry >> 16;
163   }
164 
165   for (; h < lena; h++) {
166     carry = ((UWORD32)a[h]) + carry;
167     a[h] = (unsigned short)carry;
168     carry = carry >> 16;
169   }
170 
171   assert(carry == 0);
172   return;
173 }
174 
ixheaacd_longcompare(unsigned short a[],unsigned short b[],int len)175 static int ixheaacd_longcompare(unsigned short a[], unsigned short b[],
176                                 int len) {
177   int i;
178 
179   for (i = len - 1; i > 0; i--) {
180     if (a[i] != b[i]) break;
181   }
182   return (a[i] >= b[i]) ? 1 : 0;
183 }
184 
ixheaacd_mps_coarse2fine(int * data,WORD32 data_type,int band_start,int ixheaacd_num_bands)185 static VOID ixheaacd_mps_coarse2fine(int *data, WORD32 data_type,
186                                      int band_start, int ixheaacd_num_bands) {
187   int i;
188 
189   for (i = band_start; i < band_start + ixheaacd_num_bands; i++) {
190     data[i] <<= 1;
191   }
192 
193   if (data_type == CLD) {
194     for (i = band_start; i < band_start + ixheaacd_num_bands; i++) {
195       if (data[i] == -14)
196         data[i] = -15;
197       else if (data[i] == 14)
198         data[i] = 15;
199     }
200   }
201 }
202 
ixheaacd_mps_fine2coarse(int * data,int ixheaacd_num_bands)203 static VOID ixheaacd_mps_fine2coarse(int *data, int ixheaacd_num_bands) {
204   int i;
205 
206   for (i = 0; i < ixheaacd_num_bands; i++) {
207     data[i] /= 2;
208   }
209 }
210 
ixheaacd_mps_getstridemap(int freq_res_stride,int band_start,int band_stop,int * strides)211 static int ixheaacd_mps_getstridemap(int freq_res_stride, int band_start,
212                                      int band_stop, int *strides) {
213   int i, pb, ch_fac, data_bands, start_offset;
214 
215   ch_fac = ixheaacd_mps_stride_table[freq_res_stride];
216   data_bands = (band_stop - band_start - 1) / ch_fac + 1;
217 
218   strides[0] = band_start;
219   for (pb = 1; pb <= data_bands; pb++) {
220     strides[pb] = strides[pb - 1] + ch_fac;
221   }
222   start_offset = 0;
223   while (strides[data_bands] > band_stop) {
224     if (start_offset < data_bands)
225       start_offset++;
226     else
227       start_offset = 1;
228 
229     for (i = start_offset; i <= data_bands; i++) {
230       strides[i]--;
231     }
232   }
233 
234   return data_bands;
235 }
236 
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)237 static VOID ixheaacd_mps_ecdata_decoding(
238     ia_mps_dec_state_struct *self, ia_handle_bit_buf_struct bitstream,
239     int data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS], int datatype) {
240   int i, j, pb, data_set, set_index, bs_data_pair, data_bands,
241       old_quant_coarse_xxx;
242   int strides[MAX_PARAMETER_BANDS + 1] = {0};
243   int band_stop = 0;
244 
245   int *lastdata = NULL;
246   ia_mps_data_struct *frame_xxx_data = NULL;
247   int default_val = 0;
248 
249   ia_mps_bs_frame *frame = &(self->bs_frame);
250 
251   if (datatype == 0) {
252     frame_xxx_data = &frame->cld_data;
253     lastdata = frame->cmp_cld_idx_prev;
254     band_stop = self->bs_param_bands;
255   } else if (datatype == 1) {
256     frame_xxx_data = &frame->icc_data;
257     lastdata = frame->cmp_icc_idx_prev;
258     band_stop = self->bs_param_bands;
259   } else if (datatype == 2) {
260     frame_xxx_data = &frame->ipd_data;
261     lastdata = frame->ipd_idx_data_prev;
262     band_stop = self->num_bands_ipd;
263   } else {
264     frame_xxx_data = &frame->cld_data;
265     lastdata = frame->cmp_cld_idx_prev;
266     band_stop = self->bs_param_bands;
267   }
268 
269   data_set = 0;
270   for (i = 0; i < self->num_parameter_sets; i++) {
271     frame_xxx_data->bs_xxx_data_mode[i] = ixheaacd_read_bits_buf(bitstream, 2);
272 
273     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
274       data_set++;
275     }
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       assert((value % 16) < 16);
672       return ixheaacd_ipd_de_quant_table[(value & 15)];
673 
674     default:
675       assert(0);
676       return 0.0;
677   }
678 }
679 
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)680 static VOID ixheaacd_mps_mapindexdata(
681     ia_mps_dec_state_struct *self, ia_mps_data_struct *frame_xxx_data,
682     float out_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
683     int out_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
684     int cmp_idx_data[MAX_PARAMETER_SETS_MPS][MAX_PARAMETER_BANDS],
685     int idx_prev[MAX_PARAMETER_BANDS], int param_type) {
686   int interpolate_local[MAX_PARAMETER_SETS_MPS];
687   int map[MAX_PARAMETER_BANDS + 1];
688 
689   int set_index, i, band, parm_slot;
690   int data_bands, ch_fac;
691   int ps;
692 
693   int i1, i2, x1, xi, x2;
694   int band_start = 0;
695   int ext_frame_flag = self->ext_frame_flag;
696   int *param_slots = self->param_slots;
697   int num_parameter_sets = self->num_parameter_sets;
698   int band_stop = self->bs_param_bands;
699   int default_val = 0;
700 
701   set_index = 0;
702 
703   for (i = 0; i < num_parameter_sets; i++) {
704     if (frame_xxx_data->bs_xxx_data_mode[i] == 0) {
705       frame_xxx_data->quant_coarse_xxx_flag[i] = 0;
706       for (band = band_start; band < band_stop; band++) {
707         out_idx_data[i][band] = default_val;
708       }
709       for (band = band_start; band < band_stop; band++) {
710         idx_prev[band] = out_idx_data[i][band];
711       }
712 
713       frame_xxx_data->bs_quant_coarse_xxx_prev = 0;
714     }
715 
716     if (frame_xxx_data->bs_xxx_data_mode[i] == 1) {
717       for (band = band_start; band < band_stop; band++) {
718         out_idx_data[i][band] = idx_prev[band];
719       }
720       frame_xxx_data->quant_coarse_xxx_flag[i] =
721           frame_xxx_data->bs_quant_coarse_xxx_prev;
722     }
723 
724     if (frame_xxx_data->bs_xxx_data_mode[i] == 2) {
725       for (band = band_start; band < band_stop; band++) {
726         out_idx_data[i][band] = idx_prev[band];
727       }
728       frame_xxx_data->quant_coarse_xxx_flag[i] =
729           frame_xxx_data->bs_quant_coarse_xxx_prev;
730       interpolate_local[i] = 1;
731     } else {
732       interpolate_local[i] = 0;
733     }
734 
735     if (frame_xxx_data->bs_xxx_data_mode[i] == 3) {
736       parm_slot = i;
737       ch_fac =
738           ixheaacd_mps_stride_table[frame_xxx_data
739                                         ->bs_freq_res_stride_xxx[set_index]];
740       data_bands = (band_stop - band_start - 1) / ch_fac + 1;
741       ixheaacd_mps_createmapping(map, band_start, band_stop, ch_fac);
742       ixheaacd_mps_mapfrequency(&cmp_idx_data[set_index][0],
743                                 &out_idx_data[parm_slot][0], map, data_bands);
744 
745       for (band = band_start; band < band_stop; band++) {
746         idx_prev[band] = out_idx_data[parm_slot][band];
747       }
748 
749       frame_xxx_data->bs_quant_coarse_xxx_prev =
750           frame_xxx_data->bs_quant_coarse_xxx[set_index];
751       frame_xxx_data->quant_coarse_xxx_flag[i] =
752           frame_xxx_data->bs_quant_coarse_xxx[set_index];
753 
754       set_index++;
755     }
756   }
757 
758   for (i = 0; i < num_parameter_sets; i++) {
759     if (frame_xxx_data->quant_coarse_xxx_flag[i] == 1) {
760       ixheaacd_mps_coarse2fine(out_idx_data[i], param_type, band_start,
761                                band_stop - band_start);
762       frame_xxx_data->quant_coarse_xxx_flag[i] = 0;
763     }
764   }
765 
766   i1 = -1;
767   x1 = 0;
768   i2 = 0;
769   for (i = 0; i < num_parameter_sets; i++) {
770     if (interpolate_local[i] != 1) {
771       i1 = i;
772     }
773     i2 = i;
774     while (interpolate_local[i2] == 1) {
775       i2++;
776     }
777     if (i1 == -1) {
778       x1 = 0;
779       i1 = 0;
780     } else {
781       x1 = param_slots[i1];
782     }
783     xi = param_slots[i];
784     x2 = param_slots[i2];
785 
786     if (interpolate_local[i] == 1) {
787       assert(i2 < num_parameter_sets);
788       for (band = band_start; band < band_stop; band++) {
789         int yi, y1, y2;
790         y1 = out_idx_data[i1][band];
791         y2 = out_idx_data[i2][band];
792         if (param_type == IPD) {
793           if (y2 - y1 > 8) y1 += 16;
794           if (y1 - y2 > 8) y2 += 16;
795 
796           yi = (y1 + (xi - x1) * (y2 - y1) / (x2 - x1)) % 16;
797         } else {
798           yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
799         }
800         out_idx_data[i][band] = yi;
801       }
802     }
803   }
804 
805   for (ps = 0; ps < num_parameter_sets; ps++) {
806     for (band = band_start; band < band_stop; band++)
807       out_data[ps][band] =
808           ixheaacd_mps_de_quantize(out_idx_data[ps][band], param_type);
809   }
810 
811   if (ext_frame_flag) {
812     for (band = band_start; band < band_stop; band++) {
813       out_data[num_parameter_sets][band] =
814           out_data[num_parameter_sets - 1][band];
815       out_idx_data[num_parameter_sets][band] =
816           out_idx_data[num_parameter_sets - 1][band];
817     }
818   }
819 }
820 
ixheaacd_mps_dec_and_mapframeott(ia_mps_dec_state_struct * self)821 static VOID ixheaacd_mps_dec_and_mapframeott(ia_mps_dec_state_struct *self) {
822   ia_mps_bs_frame *cur_bit_stream_ptr = &(self->bs_frame);
823 
824   ixheaacd_mps_mapindexdata(self, &cur_bit_stream_ptr->cld_data, self->cld_data,
825                             cur_bit_stream_ptr->cld_idx,
826                             cur_bit_stream_ptr->cmp_cld_idx,
827                             cur_bit_stream_ptr->cld_idx_pre, CLD);
828 
829   ixheaacd_mps_mapindexdata(self, &cur_bit_stream_ptr->icc_data, self->icc_data,
830                             cur_bit_stream_ptr->icc_idx,
831                             cur_bit_stream_ptr->cmp_icc_idx,
832                             cur_bit_stream_ptr->icc_idx_pre, ICC);
833 
834   if ((self->config->bs_phase_coding))
835     ixheaacd_mps_mapindexdata(self, &cur_bit_stream_ptr->ipd_data,
836                               self->ipd_data, cur_bit_stream_ptr->ipd_idx,
837                               cur_bit_stream_ptr->ipd_idx_data,
838                               cur_bit_stream_ptr->ipd_idx_prev, IPD);
839 }
840 
ixheaacd_mps_dec_and_mapframesmg(ia_mps_dec_state_struct * self)841 static VOID ixheaacd_mps_dec_and_mapframesmg(ia_mps_dec_state_struct *self) {
842   int ps, pb, pg, ch_fac, data_bands, param_band_start, param_band_stop,
843       group_to_band[MAX_PARAMETER_BANDS + 1];
844   ia_mps_bs_frame *frame = &(self->bs_frame);
845   for (ps = 0; ps < self->num_parameter_sets; ps++) {
846     switch (frame->bs_smooth_mode[ps]) {
847       case 0:
848         self->smoothing_time[ps] = 256;
849         self->inv_smoothing_time[ps] = 4194304;
850 
851         for (pb = 0; pb < self->bs_param_bands; pb++) {
852           self->smoothing_data[ps][pb] = 0;
853         }
854         break;
855 
856       case 1:
857         if (ps > 0) {
858           self->smoothing_time[ps] = self->smoothing_time[ps - 1];
859           self->inv_smoothing_time[ps] = self->inv_smoothing_time[ps - 1];
860         } else {
861           self->smoothing_time[ps] = self->smoothing_filt_state.prev_smg_time;
862           self->inv_smoothing_time[ps] =
863               self->smoothing_filt_state.inv_prev_smg_time;
864         }
865 
866         for (pb = 0; pb < self->bs_param_bands; pb++) {
867           if (ps > 0)
868             self->smoothing_data[ps][pb] = self->smoothing_data[ps - 1][pb];
869           else
870             self->smoothing_data[ps][pb] =
871                 self->smoothing_filt_state.prev_smg_data[pb];
872         }
873         break;
874 
875       case 2:
876         self->smoothing_time[ps] =
877             ixheaacd_smoothing_time_table[frame->bs_smooth_time[ps]];
878         self->inv_smoothing_time[ps] =
879             ixheaacd_inverse_smoothing_time_table_q30[frame
880                                                           ->bs_smooth_time[ps]];
881         for (pb = 0; pb < self->bs_param_bands; pb++) {
882           self->smoothing_data[ps][pb] = 1;
883         }
884         break;
885 
886       case 3:
887         self->smoothing_time[ps] =
888             ixheaacd_smoothing_time_table[frame->bs_smooth_time[ps]];
889         self->inv_smoothing_time[ps] =
890             ixheaacd_inverse_smoothing_time_table_q30[frame
891                                                           ->bs_smooth_time[ps]];
892 
893         ch_fac = ixheaacd_mps_stride_table[frame->bs_freq_res_stride_smg[ps]];
894         data_bands = (self->bs_param_bands - 1) / ch_fac + 1;
895         ixheaacd_mps_createmapping(group_to_band, 0, self->bs_param_bands,
896                                    ch_fac);
897         for (pg = 0; pg < data_bands; pg++) {
898           param_band_start = group_to_band[pg];
899           param_band_stop = group_to_band[pg + 1];
900           for (pb = param_band_start; pb < param_band_stop; pb++) {
901             self->smoothing_data[ps][pb] = frame->bs_smg_data[ps][pg];
902           }
903         }
904         break;
905     }
906   }
907 
908   self->smoothing_filt_state.prev_smg_time =
909       self->smoothing_time[self->num_parameter_sets - 1];
910   self->smoothing_filt_state.inv_prev_smg_time =
911       self->inv_smoothing_time[self->num_parameter_sets - 1];
912   for (pb = 0; pb < self->bs_param_bands; pb++) {
913     self->smoothing_filt_state.prev_smg_data[pb] =
914         self->smoothing_data[self->num_parameter_sets - 1][pb];
915   }
916 
917   if (self->ext_frame_flag) {
918     self->smoothing_time[self->num_parameter_sets] =
919         self->smoothing_time[self->num_parameter_sets - 1];
920     self->inv_smoothing_time[self->num_parameter_sets] =
921         self->inv_smoothing_time[self->num_parameter_sets - 1];
922     for (pb = 0; pb < self->bs_param_bands; pb++) {
923       self->smoothing_data[self->num_parameter_sets][pb] =
924           self->smoothing_data[self->num_parameter_sets - 1][pb];
925     }
926   }
927 }
928 
ixheaacd_mps_frame_decode(ia_mps_dec_state_struct * self)929 VOID ixheaacd_mps_frame_decode(ia_mps_dec_state_struct *self) {
930   int i;
931   if (self->parse_nxt_frame == 1) return;
932 
933   self->ext_frame_flag = 0;
934   if (self->param_slots[self->num_parameter_sets - 1] != self->time_slots - 1) {
935     self->ext_frame_flag = 1;
936   }
937 
938   ixheaacd_mps_dec_and_mapframeott(self);
939 
940   ixheaacd_mps_dec_and_mapframesmg(self);
941 
942   if (self->ext_frame_flag) {
943     self->num_parameter_sets++;
944     self->param_slots[self->num_parameter_sets - 1] = self->time_slots - 1;
945   }
946   self->param_slot_diff[0] = self->param_slots[0] + 1;
947   self->inv_param_slot_diff[0] = (float)1 / self->param_slot_diff[0];
948   self->inv_param_slot_diff_Q30[0] =
949       (int)floor(self->inv_param_slot_diff[0] * 1073741824 + 0.5);
950   for (i = 1; i < self->num_parameter_sets; i++) {
951     self->param_slot_diff[i] = self->param_slots[i] - self->param_slots[i - 1];
952     self->inv_param_slot_diff[i] = (float)1 / self->param_slot_diff[i];
953     self->inv_param_slot_diff_Q30[i] =
954         (int)floor(self->inv_param_slot_diff[i] * 1073741824 + 0.5);
955   }
956 }
957 
ixheaacd_mps_header_decode(ia_mps_dec_state_struct * self)958 WORD32 ixheaacd_mps_header_decode(ia_mps_dec_state_struct *self) {
959   self->time_slots = self->frame_length + 1;
960   self->frame_len = self->time_slots * self->qmf_band_count;
961   self->bs_param_bands = ixheaacd_freq_res_table[self->config->bs_freq_res];
962 
963   self->hyb_band_count = self->qmf_band_count - QMF_BANDS_TO_HYBRID + 10;
964 
965   switch (self->bs_param_bands) {
966     case 4:
967 
968       self->hyb_band_to_processing_band_table =
969           ixheaacd_hybrid_band_71_to_processing_band_4_map;
970       break;
971     case 5:
972 
973       self->hyb_band_to_processing_band_table =
974           ixheaacd_hybrid_band_71_to_processing_band_5_map;
975       break;
976     case 7:
977 
978       self->hyb_band_to_processing_band_table =
979           ixheaacd_hybrid_band_71_to_processing_band_7_map;
980       break;
981     case 10:
982 
983       self->hyb_band_to_processing_band_table =
984           ixheaacd_hybrid_band_71_to_processing_band_10_map;
985       break;
986     case 14:
987 
988       self->hyb_band_to_processing_band_table =
989           ixheaacd_hybrid_band_71_to_processing_band_14_map;
990       break;
991     case 20:
992 
993       self->hyb_band_to_processing_band_table =
994           ixheaacd_hybrid_band_71_to_processing_band_20_map;
995       break;
996     case 28:
997 
998       self->hyb_band_to_processing_band_table =
999           ixheaacd_hybrid_band_71_to_processing_band_28_map;
1000       break;
1001     default:
1002       self->hyb_band_to_processing_band_table = NULL;
1003       return -1;
1004       break;
1005   };
1006 
1007   self->in_ch_count = 1;
1008   self->out_ch_count = 2;
1009 
1010   self->input_gain =
1011       ixheaacd_mps_clip_gain_table[self->config->bs_fixed_gain_dmx];
1012 
1013   if (self->config->bs_ott_bands_phase_present) {
1014     self->num_bands_ipd = self->config->bs_ott_bands_phase;
1015   } else {
1016     switch (self->bs_param_bands) {
1017       case 4:
1018       case 5:
1019         self->num_bands_ipd = 2;
1020         break;
1021       case 7:
1022         self->num_bands_ipd = 3;
1023         break;
1024       case 10:
1025         self->num_bands_ipd = 5;
1026         break;
1027       case 14:
1028         self->num_bands_ipd = 7;
1029         break;
1030       case 20:
1031       case 28:
1032         self->num_bands_ipd = 10;
1033         break;
1034       default:
1035         assert(0);
1036         break;
1037     }
1038   }
1039 
1040   if (self->residual_coding) {
1041     self->num_bands_ipd = max(self->bs_residual_bands, self->num_bands_ipd);
1042     self->max_res_bands = 0;
1043     if (self->bs_residual_present) {
1044       self->res_bands = self->bs_residual_bands;
1045       if (self->res_bands > self->max_res_bands) {
1046         self->max_res_bands = self->res_bands;
1047       }
1048     } else {
1049       self->res_bands = 0;
1050     }
1051   }
1052 
1053   self->dir_sig_count = 1;
1054   self->decor_sig_count = 1;
1055 
1056   self->bs_high_rate_mode = self->config->bs_high_rate_mode;
1057 
1058   return 0;
1059 }
1060