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