blob: cc0980069af331ddbc9e5f6e6e9dd45355ccefeb
1 | /* |
2 | ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding |
3 | ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com |
4 | ** |
5 | ** This program is free software; you can redistribute it and/or modify |
6 | ** it under the terms of the GNU General Public License as published by |
7 | ** the Free Software Foundation; either version 2 of the License, or |
8 | ** (at your option) any later version. |
9 | ** |
10 | ** This program is distributed in the hope that it will be useful, |
11 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | ** GNU General Public License for more details. |
14 | ** |
15 | ** You should have received a copy of the GNU General Public License |
16 | ** along with this program; if not, write to the Free Software |
17 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | ** |
19 | ** Any non-GPL usage of this software or parts of this software is strictly |
20 | ** forbidden. |
21 | ** |
22 | ** The "appropriate copyright message" mentioned in section 2c of the GPLv2 |
23 | ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com" |
24 | ** |
25 | ** Commercial non-GPL licensing of this software is possible. |
26 | ** For more info contact Nero AG through Mpeg4AAClicense@nero.com. |
27 | ** |
28 | ** $Id: specrec.c,v 1.62 2009/01/26 23:51:15 menno Exp $ |
29 | **/ |
30 | |
31 | /* |
32 | Spectral reconstruction: |
33 | - grouping/sectioning |
34 | - inverse quantization |
35 | - applying scalefactors |
36 | */ |
37 | #include <stdlib.h> |
38 | #include "common.h" |
39 | #include "structs.h" |
40 | |
41 | #include <string.h> |
42 | #include "specrec.h" |
43 | #include "filtbank.h" |
44 | #include "syntax.h" |
45 | #include "iq_table.h" |
46 | #include "ms.h" |
47 | #include "is.h" |
48 | #include "pns.h" |
49 | #include "tns.h" |
50 | #include "drc.h" |
51 | #include "lt_predict.h" |
52 | #include "ic_predict.h" |
53 | #ifdef SSR_DEC |
54 | #include "ssr.h" |
55 | #include "ssr_fb.h" |
56 | #endif |
57 | |
58 | |
59 | /* static function declarations */ |
60 | static uint8_t quant_to_spec(NeAACDecStruct *hDecoder, |
61 | ic_stream *ics, int16_t *quant_data, |
62 | real_t *spec_data, uint16_t frame_len); |
63 | |
64 | |
65 | #ifdef LD_DEC |
66 | ALIGN static const uint8_t num_swb_512_window[] = { |
67 | 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0 |
68 | }; |
69 | ALIGN static const uint8_t num_swb_480_window[] = { |
70 | 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0 |
71 | }; |
72 | #endif |
73 | |
74 | ALIGN static const uint8_t num_swb_960_window[] = { |
75 | 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40 |
76 | }; |
77 | |
78 | ALIGN static const uint8_t num_swb_1024_window[] = { |
79 | 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40 |
80 | }; |
81 | |
82 | ALIGN static const uint8_t num_swb_128_window[] = { |
83 | 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15 |
84 | }; |
85 | |
86 | ALIGN static const uint16_t swb_offset_1024_96[] = { |
87 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, |
88 | 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, |
89 | 276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024 |
90 | }; |
91 | |
92 | ALIGN static const uint16_t swb_offset_128_96[] = { |
93 | 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 |
94 | }; |
95 | |
96 | ALIGN static const uint16_t swb_offset_1024_64[] = { |
97 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, |
98 | 64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268, |
99 | 304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824, |
100 | 864, 904, 944, 984, 1024 |
101 | }; |
102 | |
103 | ALIGN static const uint16_t swb_offset_128_64[] = { |
104 | 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128 |
105 | }; |
106 | |
107 | ALIGN static const uint16_t swb_offset_1024_48[] = { |
108 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, |
109 | 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292, |
110 | 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736, |
111 | 768, 800, 832, 864, 896, 928, 1024 |
112 | }; |
113 | |
114 | #ifdef LD_DEC |
115 | ALIGN static const uint16_t swb_offset_512_48[] = { |
116 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84, |
117 | 92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396, |
118 | 428, 460, 512 |
119 | }; |
120 | |
121 | ALIGN static const uint16_t swb_offset_480_48[] = { |
122 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 , 80 , 88, |
123 | 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400, |
124 | 432, 480 |
125 | }; |
126 | #endif |
127 | |
128 | ALIGN static const uint16_t swb_offset_128_48[] = { |
129 | 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128 |
130 | }; |
131 | |
132 | ALIGN static const uint16_t swb_offset_1024_32[] = { |
133 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72, |
134 | 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292, |
135 | 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736, |
136 | 768, 800, 832, 864, 896, 928, 960, 992, 1024 |
137 | }; |
138 | |
139 | #ifdef LD_DEC |
140 | ALIGN static const uint16_t swb_offset_512_32[] = { |
141 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, |
142 | 88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352, |
143 | 384, 416, 448, 480, 512 |
144 | }; |
145 | |
146 | ALIGN static const uint16_t swb_offset_480_32[] = { |
147 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, |
148 | 88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352, |
149 | 384, 416, 448, 480 |
150 | }; |
151 | #endif |
152 | |
153 | ALIGN static const uint16_t swb_offset_1024_24[] = { |
154 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, |
155 | 76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220, |
156 | 240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704, |
157 | 768, 832, 896, 960, 1024 |
158 | }; |
159 | |
160 | #ifdef LD_DEC |
161 | ALIGN static const uint16_t swb_offset_512_24[] = { |
162 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, |
163 | 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, |
164 | 448, 480, 512 |
165 | }; |
166 | |
167 | ALIGN static const uint16_t swb_offset_480_24[] = { |
168 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120, |
169 | 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480 |
170 | }; |
171 | #endif |
172 | |
173 | ALIGN static const uint16_t swb_offset_128_24[] = { |
174 | 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128 |
175 | }; |
176 | |
177 | ALIGN static const uint16_t swb_offset_1024_16[] = { |
178 | 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124, |
179 | 136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344, |
180 | 368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024 |
181 | }; |
182 | |
183 | ALIGN static const uint16_t swb_offset_128_16[] = { |
184 | 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128 |
185 | }; |
186 | |
187 | ALIGN static const uint16_t swb_offset_1024_8[] = { |
188 | 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172, |
189 | 188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448, |
190 | 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024 |
191 | }; |
192 | |
193 | ALIGN static const uint16_t swb_offset_128_8[] = { |
194 | 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128 |
195 | }; |
196 | |
197 | ALIGN static const uint16_t *swb_offset_1024_window[] = { |
198 | swb_offset_1024_96, /* 96000 */ |
199 | swb_offset_1024_96, /* 88200 */ |
200 | swb_offset_1024_64, /* 64000 */ |
201 | swb_offset_1024_48, /* 48000 */ |
202 | swb_offset_1024_48, /* 44100 */ |
203 | swb_offset_1024_32, /* 32000 */ |
204 | swb_offset_1024_24, /* 24000 */ |
205 | swb_offset_1024_24, /* 22050 */ |
206 | swb_offset_1024_16, /* 16000 */ |
207 | swb_offset_1024_16, /* 12000 */ |
208 | swb_offset_1024_16, /* 11025 */ |
209 | swb_offset_1024_8 /* 8000 */ |
210 | }; |
211 | |
212 | #ifdef LD_DEC |
213 | ALIGN static const uint16_t *swb_offset_512_window[] = { |
214 | 0, /* 96000 */ |
215 | 0, /* 88200 */ |
216 | 0, /* 64000 */ |
217 | swb_offset_512_48, /* 48000 */ |
218 | swb_offset_512_48, /* 44100 */ |
219 | swb_offset_512_32, /* 32000 */ |
220 | swb_offset_512_24, /* 24000 */ |
221 | swb_offset_512_24, /* 22050 */ |
222 | 0, /* 16000 */ |
223 | 0, /* 12000 */ |
224 | 0, /* 11025 */ |
225 | 0 /* 8000 */ |
226 | }; |
227 | |
228 | ALIGN static const uint16_t *swb_offset_480_window[] = { |
229 | 0, /* 96000 */ |
230 | 0, /* 88200 */ |
231 | 0, /* 64000 */ |
232 | swb_offset_480_48, /* 48000 */ |
233 | swb_offset_480_48, /* 44100 */ |
234 | swb_offset_480_32, /* 32000 */ |
235 | swb_offset_480_24, /* 24000 */ |
236 | swb_offset_480_24, /* 22050 */ |
237 | 0, /* 16000 */ |
238 | 0, /* 12000 */ |
239 | 0, /* 11025 */ |
240 | 0 /* 8000 */ |
241 | }; |
242 | #endif |
243 | |
244 | ALIGN static const uint16_t *swb_offset_128_window[] = { |
245 | swb_offset_128_96, /* 96000 */ |
246 | swb_offset_128_96, /* 88200 */ |
247 | swb_offset_128_64, /* 64000 */ |
248 | swb_offset_128_48, /* 48000 */ |
249 | swb_offset_128_48, /* 44100 */ |
250 | swb_offset_128_48, /* 32000 */ |
251 | swb_offset_128_24, /* 24000 */ |
252 | swb_offset_128_24, /* 22050 */ |
253 | swb_offset_128_16, /* 16000 */ |
254 | swb_offset_128_16, /* 12000 */ |
255 | swb_offset_128_16, /* 11025 */ |
256 | swb_offset_128_8 /* 8000 */ |
257 | }; |
258 | |
259 | #define bit_set(A, B) ((A) & (1<<(B))) |
260 | |
261 | /* 4.5.2.3.4 */ |
262 | /* |
263 | - determine the number of windows in a window_sequence named num_windows |
264 | - determine the number of window_groups named num_window_groups |
265 | - determine the number of windows in each group named window_group_length[g] |
266 | - determine the total number of scalefactor window bands named num_swb for |
267 | the actual window type |
268 | - determine swb_offset[swb], the offset of the first coefficient in |
269 | scalefactor window band named swb of the window actually used |
270 | - determine sect_sfb_offset[g][section],the offset of the first coefficient |
271 | in section named section. This offset depends on window_sequence and |
272 | scale_factor_grouping and is needed to decode the spectral_data(). |
273 | */ |
274 | uint8_t window_grouping_info(NeAACDecStruct *hDecoder, ic_stream *ics) |
275 | { |
276 | uint8_t i, g; |
277 | |
278 | uint8_t sf_index = hDecoder->sf_index; |
279 | |
280 | switch (ics->window_sequence) { |
281 | case ONLY_LONG_SEQUENCE: |
282 | case LONG_START_SEQUENCE: |
283 | case LONG_STOP_SEQUENCE: |
284 | ics->num_windows = 1; |
285 | ics->num_window_groups = 1; |
286 | ics->window_group_length[ics->num_window_groups - 1] = 1; |
287 | #ifdef LD_DEC |
288 | if (hDecoder->object_type == LD) { |
289 | if (hDecoder->frameLength == 512) { |
290 | ics->num_swb = num_swb_512_window[sf_index]; |
291 | } else { /* if (hDecoder->frameLength == 480) */ |
292 | ics->num_swb = num_swb_480_window[sf_index]; |
293 | } |
294 | } else { |
295 | #endif |
296 | if (hDecoder->frameLength == 1024) { |
297 | ics->num_swb = num_swb_1024_window[sf_index]; |
298 | } else { /* if (hDecoder->frameLength == 960) */ |
299 | ics->num_swb = num_swb_960_window[sf_index]; |
300 | } |
301 | #ifdef LD_DEC |
302 | } |
303 | #endif |
304 | |
305 | if (ics->max_sfb > ics->num_swb) { |
306 | return 32; |
307 | } |
308 | |
309 | /* preparation of sect_sfb_offset for long blocks */ |
310 | /* also copy the last value! */ |
311 | #ifdef LD_DEC |
312 | if (hDecoder->object_type == LD) { |
313 | if (hDecoder->frameLength == 512) { |
314 | for (i = 0; i < ics->num_swb; i++) { |
315 | ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i]; |
316 | ics->swb_offset[i] = swb_offset_512_window[sf_index][i]; |
317 | } |
318 | } else { /* if (hDecoder->frameLength == 480) */ |
319 | for (i = 0; i < ics->num_swb; i++) { |
320 | ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i]; |
321 | ics->swb_offset[i] = swb_offset_480_window[sf_index][i]; |
322 | } |
323 | } |
324 | ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength; |
325 | ics->swb_offset[ics->num_swb] = hDecoder->frameLength; |
326 | ics->swb_offset_max = hDecoder->frameLength; |
327 | } else { |
328 | #endif |
329 | for (i = 0; i < ics->num_swb; i++) { |
330 | ics->sect_sfb_offset[0][i] = swb_offset_1024_window[sf_index][i]; |
331 | ics->swb_offset[i] = swb_offset_1024_window[sf_index][i]; |
332 | } |
333 | ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength; |
334 | ics->swb_offset[ics->num_swb] = hDecoder->frameLength; |
335 | ics->swb_offset_max = hDecoder->frameLength; |
336 | #ifdef LD_DEC |
337 | } |
338 | #endif |
339 | return 0; |
340 | case EIGHT_SHORT_SEQUENCE: |
341 | ics->num_windows = 8; |
342 | ics->num_window_groups = 1; |
343 | ics->window_group_length[ics->num_window_groups - 1] = 1; |
344 | ics->num_swb = num_swb_128_window[sf_index]; |
345 | |
346 | if (ics->max_sfb > ics->num_swb) { |
347 | return 32; |
348 | } |
349 | |
350 | for (i = 0; i < ics->num_swb; i++) { |
351 | ics->swb_offset[i] = swb_offset_128_window[sf_index][i]; |
352 | } |
353 | ics->swb_offset[ics->num_swb] = hDecoder->frameLength / 8; |
354 | ics->swb_offset_max = hDecoder->frameLength / 8; |
355 | |
356 | for (i = 0; i < ics->num_windows - 1; i++) { |
357 | if (bit_set(ics->scale_factor_grouping, 6 - i) == 0) { |
358 | ics->num_window_groups += 1; |
359 | ics->window_group_length[ics->num_window_groups - 1] = 1; |
360 | } else { |
361 | ics->window_group_length[ics->num_window_groups - 1] += 1; |
362 | } |
363 | } |
364 | |
365 | /* preparation of sect_sfb_offset for short blocks */ |
366 | for (g = 0; g < ics->num_window_groups; g++) { |
367 | uint16_t width; |
368 | uint8_t sect_sfb = 0; |
369 | uint16_t offset = 0; |
370 | |
371 | for (i = 0; i < ics->num_swb; i++) { |
372 | if (i + 1 == ics->num_swb) { |
373 | width = (hDecoder->frameLength / 8) - swb_offset_128_window[sf_index][i]; |
374 | } else { |
375 | width = swb_offset_128_window[sf_index][i + 1] - |
376 | swb_offset_128_window[sf_index][i]; |
377 | } |
378 | width *= ics->window_group_length[g]; |
379 | ics->sect_sfb_offset[g][sect_sfb++] = offset; |
380 | offset += width; |
381 | } |
382 | ics->sect_sfb_offset[g][sect_sfb] = offset; |
383 | } |
384 | return 0; |
385 | default: |
386 | return 32; |
387 | } |
388 | } |
389 | |
390 | /* iquant() * |
391 | /* output = sign(input)*abs(input)^(4/3) */ |
392 | /**/ |
393 | static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error) |
394 | { |
395 | #ifdef FIXED_POINT |
396 | /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */ |
397 | /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not |
398 | * defined a 1026 value table and interpolation will be used |
399 | */ |
400 | #ifndef BIG_IQ_TABLE |
401 | static const real_t errcorr[] = { |
402 | REAL_CONST(0), REAL_CONST(1.0 / 8.0), REAL_CONST(2.0 / 8.0), REAL_CONST(3.0 / 8.0), |
403 | REAL_CONST(4.0 / 8.0), REAL_CONST(5.0 / 8.0), REAL_CONST(6.0 / 8.0), REAL_CONST(7.0 / 8.0), |
404 | REAL_CONST(0) |
405 | }; |
406 | real_t x1, x2; |
407 | #endif |
408 | int16_t sgn = 1; |
409 | |
410 | if (q < 0) { |
411 | q = -q; |
412 | sgn = -1; |
413 | } |
414 | |
415 | if (q < IQ_TABLE_SIZE) { |
416 | //#define IQUANT_PRINT |
417 | #ifdef IQUANT_PRINT |
418 | //printf("0x%.8X\n", sgn * tab[q]); |
419 | printf("%d\n", sgn * tab[q]); |
420 | #endif |
421 | return sgn * tab[q]; |
422 | } |
423 | |
424 | #ifndef BIG_IQ_TABLE |
425 | if (q >= 8192) { |
426 | *error = 17; |
427 | return 0; |
428 | } |
429 | |
430 | /* linear interpolation */ |
431 | x1 = tab[q >> 3]; |
432 | x2 = tab[(q >> 3) + 1]; |
433 | return sgn * 16 * (MUL_R(errcorr[q & 7], (x2 - x1)) + x1); |
434 | #else |
435 | *error = 17; |
436 | return 0; |
437 | #endif |
438 | |
439 | #else |
440 | if (q < 0) { |
441 | /* tab contains a value for all possible q [0,8192] */ |
442 | if (-q < IQ_TABLE_SIZE) { |
443 | return -tab[-q]; |
444 | } |
445 | |
446 | *error = 17; |
447 | return 0; |
448 | } else { |
449 | /* tab contains a value for all possible q [0,8192] */ |
450 | if (q < IQ_TABLE_SIZE) { |
451 | return tab[q]; |
452 | } |
453 | |
454 | *error = 17; |
455 | return 0; |
456 | } |
457 | #endif |
458 | } |
459 | |
460 | #ifndef FIXED_POINT |
461 | ALIGN static const real_t pow2sf_tab[] = { |
462 | 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007, |
463 | 2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007, |
464 | 1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006, |
465 | 1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005, |
466 | 0.0001220703125, 0.000244140625, 0.00048828125, |
467 | 0.0009765625, 0.001953125, 0.00390625, |
468 | 0.0078125, 0.015625, 0.03125, |
469 | 0.0625, 0.125, 0.25, |
470 | 0.5, 1.0, 2.0, |
471 | 4.0, 8.0, 16.0, 32.0, |
472 | 64.0, 128.0, 256.0, |
473 | 512.0, 1024.0, 2048.0, |
474 | 4096.0, 8192.0, 16384.0, |
475 | 32768.0, 65536.0, 131072.0, |
476 | 262144.0, 524288.0, 1048576.0, |
477 | 2097152.0, 4194304.0, 8388608.0, |
478 | 16777216.0, 33554432.0, 67108864.0, |
479 | 134217728.0, 268435456.0, 536870912.0, |
480 | 1073741824.0, 2147483648.0, 4294967296.0, |
481 | 8589934592.0, 17179869184.0, 34359738368.0, |
482 | 68719476736.0, 137438953472.0, 274877906944.0 |
483 | }; |
484 | #endif |
485 | |
486 | /* quant_to_spec: perform dequantisation and scaling |
487 | * and in case of short block it also does the deinterleaving |
488 | */ |
489 | /* |
490 | For ONLY_LONG_SEQUENCE windows (num_window_groups = 1, |
491 | window_group_length[0] = 1) the spectral data is in ascending spectral |
492 | order. |
493 | For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the |
494 | grouping in the following manner: |
495 | - Groups are ordered sequentially |
496 | - Within a group, a scalefactor band consists of the spectral data of all |
497 | grouped SHORT_WINDOWs for the associated scalefactor window band. To |
498 | clarify via example, the length of a group is in the range of one to eight |
499 | SHORT_WINDOWs. |
500 | - If there are eight groups each with length one (num_window_groups = 8, |
501 | window_group_length[0..7] = 1), the result is a sequence of eight spectra, |
502 | each in ascending spectral order. |
503 | - If there is only one group with length eight (num_window_groups = 1, |
504 | window_group_length[0] = 8), the result is that spectral data of all eight |
505 | SHORT_WINDOWs is interleaved by scalefactor window bands. |
506 | - Within a scalefactor window band, the coefficients are in ascending |
507 | spectral order. |
508 | */ |
509 | static uint8_t quant_to_spec(NeAACDecStruct *hDecoder, |
510 | ic_stream *ics, int16_t *quant_data, |
511 | real_t *spec_data, uint16_t frame_len) |
512 | { |
513 | ALIGN static const real_t pow2_table[] = { |
514 | COEF_CONST(1.0), |
515 | COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */ |
516 | COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */ |
517 | COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */ |
518 | }; |
519 | const real_t *tab = iq_table; |
520 | |
521 | uint8_t g, sfb, win; |
522 | uint16_t width, bin, k, gindex, wa, wb; |
523 | uint8_t error = 0; /* Init error flag */ |
524 | #ifndef FIXED_POINT |
525 | real_t scf; |
526 | #endif |
527 | |
528 | k = 0; |
529 | gindex = 0; |
530 | |
531 | for (g = 0; g < ics->num_window_groups; g++) { |
532 | uint16_t j = 0; |
533 | uint16_t gincrease = 0; |
534 | uint16_t win_inc = ics->swb_offset[ics->num_swb]; |
535 | |
536 | for (sfb = 0; sfb < ics->num_swb; sfb++) { |
537 | int32_t exp, frac; |
538 | |
539 | width = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb]; |
540 | |
541 | /* this could be scalefactor for IS or PNS, those can be negative or bigger then 255 */ |
542 | /* just ignore them */ |
543 | if (ics->scale_factors[g][sfb] < 0 || ics->scale_factors[g][sfb] > 255) { |
544 | exp = 0; |
545 | frac = 0; |
546 | } else { |
547 | /* ics->scale_factors[g][sfb] must be between 0 and 255 */ |
548 | exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2; |
549 | /* frac must always be > 0 */ |
550 | frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3; |
551 | } |
552 | |
553 | #ifdef FIXED_POINT |
554 | exp -= 25; |
555 | /* IMDCT pre-scaling */ |
556 | if (hDecoder->object_type == LD) { |
557 | exp -= 6 /*9*/; |
558 | } else { |
559 | if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) { |
560 | exp -= 4 /*7*/; |
561 | } else { |
562 | exp -= 7 /*10*/; |
563 | } |
564 | } |
565 | #endif |
566 | |
567 | wa = gindex + j; |
568 | |
569 | #ifndef FIXED_POINT |
570 | scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac]; |
571 | #endif |
572 | |
573 | for (win = 0; win < ics->window_group_length[g]; win++) { |
574 | for (bin = 0; bin < width; bin += 4) { |
575 | #ifndef FIXED_POINT |
576 | wb = wa + bin; |
577 | |
578 | spec_data[wb + 0] = iquant(quant_data[k + 0], tab, &error) * scf; |
579 | spec_data[wb + 1] = iquant(quant_data[k + 1], tab, &error) * scf; |
580 | spec_data[wb + 2] = iquant(quant_data[k + 2], tab, &error) * scf; |
581 | spec_data[wb + 3] = iquant(quant_data[k + 3], tab, &error) * scf; |
582 | |
583 | #else |
584 | real_t iq0 = iquant(quant_data[k + 0], tab, &error); |
585 | real_t iq1 = iquant(quant_data[k + 1], tab, &error); |
586 | real_t iq2 = iquant(quant_data[k + 2], tab, &error); |
587 | real_t iq3 = iquant(quant_data[k + 3], tab, &error); |
588 | |
589 | wb = wa + bin; |
590 | |
591 | if (exp < 0) { |
592 | spec_data[wb + 0] = iq0 >>= -exp; |
593 | spec_data[wb + 1] = iq1 >>= -exp; |
594 | spec_data[wb + 2] = iq2 >>= -exp; |
595 | spec_data[wb + 3] = iq3 >>= -exp; |
596 | } else { |
597 | spec_data[wb + 0] = iq0 <<= exp; |
598 | spec_data[wb + 1] = iq1 <<= exp; |
599 | spec_data[wb + 2] = iq2 <<= exp; |
600 | spec_data[wb + 3] = iq3 <<= exp; |
601 | } |
602 | if (frac != 0) { |
603 | spec_data[wb + 0] = MUL_C(spec_data[wb + 0], pow2_table[frac]); |
604 | spec_data[wb + 1] = MUL_C(spec_data[wb + 1], pow2_table[frac]); |
605 | spec_data[wb + 2] = MUL_C(spec_data[wb + 2], pow2_table[frac]); |
606 | spec_data[wb + 3] = MUL_C(spec_data[wb + 3], pow2_table[frac]); |
607 | } |
608 | |
609 | //#define SCFS_PRINT |
610 | #ifdef SCFS_PRINT |
611 | printf("%d\n", spec_data[gindex + (win * win_inc) + j + bin + 0]); |
612 | printf("%d\n", spec_data[gindex + (win * win_inc) + j + bin + 1]); |
613 | printf("%d\n", spec_data[gindex + (win * win_inc) + j + bin + 2]); |
614 | printf("%d\n", spec_data[gindex + (win * win_inc) + j + bin + 3]); |
615 | //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]); |
616 | //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]); |
617 | //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]); |
618 | //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]); |
619 | #endif |
620 | #endif |
621 | |
622 | gincrease += 4; |
623 | k += 4; |
624 | } |
625 | wa += win_inc; |
626 | } |
627 | j += width; |
628 | } |
629 | gindex += gincrease; |
630 | } |
631 | |
632 | return error; |
633 | } |
634 | |
635 | static uint8_t allocate_single_channel(NeAACDecStruct *hDecoder, uint8_t channel, |
636 | uint8_t output_channels) |
637 | { |
638 | int mul = 1; |
639 | |
640 | #ifdef MAIN_DEC |
641 | /* MAIN object type prediction */ |
642 | if (hDecoder->object_type == MAIN) { |
643 | /* allocate the state only when needed */ |
644 | if (hDecoder->pred_stat[channel] != NULL) { |
645 | faad_free(hDecoder->pred_stat[channel]); |
646 | hDecoder->pred_stat[channel] = NULL; |
647 | } |
648 | |
649 | hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state)); |
650 | reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength); |
651 | } |
652 | #endif |
653 | |
654 | #ifdef LTP_DEC |
655 | if (is_ltp_ot(hDecoder->object_type)) { |
656 | /* allocate the state only when needed */ |
657 | if (hDecoder->lt_pred_stat[channel] != NULL) { |
658 | faad_free(hDecoder->lt_pred_stat[channel]); |
659 | hDecoder->lt_pred_stat[channel] = NULL; |
660 | } |
661 | |
662 | hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength * 4 * sizeof(int16_t)); |
663 | memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength * 4 * sizeof(int16_t)); |
664 | } |
665 | #endif |
666 | |
667 | if (hDecoder->time_out[channel] != NULL) { |
668 | faad_free(hDecoder->time_out[channel]); |
669 | hDecoder->time_out[channel] = NULL; |
670 | } |
671 | |
672 | { |
673 | mul = 1; |
674 | #ifdef SBR_DEC |
675 | hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0; |
676 | if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) { |
677 | /* SBR requires 2 times as much output data */ |
678 | mul = 2; |
679 | hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1; |
680 | } |
681 | #endif |
682 | hDecoder->time_out[channel] = (real_t*)faad_malloc(mul * hDecoder->frameLength * sizeof(real_t)); |
683 | memset(hDecoder->time_out[channel], 0, mul * hDecoder->frameLength * sizeof(real_t)); |
684 | } |
685 | |
686 | #if (defined(PS_DEC) || defined(DRM_PS)) |
687 | if (output_channels == 2) { |
688 | if (hDecoder->time_out[channel + 1] != NULL) { |
689 | faad_free(hDecoder->time_out[channel + 1]); |
690 | hDecoder->time_out[channel + 1] = NULL; |
691 | } |
692 | |
693 | hDecoder->time_out[channel + 1] = (real_t*)faad_malloc(mul * hDecoder->frameLength * sizeof(real_t)); |
694 | memset(hDecoder->time_out[channel + 1], 0, mul * hDecoder->frameLength * sizeof(real_t)); |
695 | } |
696 | #endif |
697 | |
698 | if (hDecoder->fb_intermed[channel] != NULL) { |
699 | faad_free(hDecoder->fb_intermed[channel]); |
700 | hDecoder->fb_intermed[channel] = NULL; |
701 | } |
702 | |
703 | hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength * sizeof(real_t)); |
704 | memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength * sizeof(real_t)); |
705 | |
706 | #ifdef SSR_DEC |
707 | if (hDecoder->object_type == SSR) { |
708 | if (hDecoder->ssr_overlap[channel] == NULL) { |
709 | hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2 * hDecoder->frameLength * sizeof(real_t)); |
710 | memset(hDecoder->ssr_overlap[channel], 0, 2 * hDecoder->frameLength * sizeof(real_t)); |
711 | } |
712 | if (hDecoder->prev_fmd[channel] == NULL) { |
713 | uint16_t k; |
714 | hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2 * hDecoder->frameLength * sizeof(real_t)); |
715 | for (k = 0; k < 2 * hDecoder->frameLength; k++) { |
716 | hDecoder->prev_fmd[channel][k] = REAL_CONST(-1); |
717 | } |
718 | } |
719 | } |
720 | #endif |
721 | |
722 | return 0; |
723 | } |
724 | |
725 | static uint8_t allocate_channel_pair(NeAACDecStruct *hDecoder, |
726 | uint8_t channel, uint8_t paired_channel) |
727 | { |
728 | int mul = 1; |
729 | |
730 | #ifdef MAIN_DEC |
731 | /* MAIN object type prediction */ |
732 | if (hDecoder->object_type == MAIN) { |
733 | /* allocate the state only when needed */ |
734 | if (hDecoder->pred_stat[channel] == NULL) { |
735 | hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state)); |
736 | reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength); |
737 | } |
738 | if (hDecoder->pred_stat[paired_channel] == NULL) { |
739 | hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state)); |
740 | reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength); |
741 | } |
742 | } |
743 | #endif |
744 | |
745 | #ifdef LTP_DEC |
746 | if (is_ltp_ot(hDecoder->object_type)) { |
747 | /* allocate the state only when needed */ |
748 | if (hDecoder->lt_pred_stat[channel] == NULL) { |
749 | hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength * 4 * sizeof(int16_t)); |
750 | memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength * 4 * sizeof(int16_t)); |
751 | } |
752 | if (hDecoder->lt_pred_stat[paired_channel] == NULL) { |
753 | hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength * 4 * sizeof(int16_t)); |
754 | memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength * 4 * sizeof(int16_t)); |
755 | } |
756 | } |
757 | #endif |
758 | |
759 | if (hDecoder->time_out[channel] == NULL) { |
760 | mul = 1; |
761 | #ifdef SBR_DEC |
762 | hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0; |
763 | if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) { |
764 | /* SBR requires 2 times as much output data */ |
765 | mul = 2; |
766 | hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1; |
767 | } |
768 | #endif |
769 | hDecoder->time_out[channel] = (real_t*)faad_malloc(mul * hDecoder->frameLength * sizeof(real_t)); |
770 | memset(hDecoder->time_out[channel], 0, mul * hDecoder->frameLength * sizeof(real_t)); |
771 | } |
772 | if (hDecoder->time_out[paired_channel] == NULL) { |
773 | hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul * hDecoder->frameLength * sizeof(real_t)); |
774 | memset(hDecoder->time_out[paired_channel], 0, mul * hDecoder->frameLength * sizeof(real_t)); |
775 | } |
776 | |
777 | if (hDecoder->fb_intermed[channel] == NULL) { |
778 | hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength * sizeof(real_t)); |
779 | memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength * sizeof(real_t)); |
780 | } |
781 | if (hDecoder->fb_intermed[paired_channel] == NULL) { |
782 | hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength * sizeof(real_t)); |
783 | memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength * sizeof(real_t)); |
784 | } |
785 | |
786 | #ifdef SSR_DEC |
787 | if (hDecoder->object_type == SSR) { |
788 | if (hDecoder->ssr_overlap[cpe->channel] == NULL) { |
789 | hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2 * hDecoder->frameLength * sizeof(real_t)); |
790 | memset(hDecoder->ssr_overlap[cpe->channel], 0, 2 * hDecoder->frameLength * sizeof(real_t)); |
791 | } |
792 | if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL) { |
793 | hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2 * hDecoder->frameLength * sizeof(real_t)); |
794 | memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2 * hDecoder->frameLength * sizeof(real_t)); |
795 | } |
796 | if (hDecoder->prev_fmd[cpe->channel] == NULL) { |
797 | uint16_t k; |
798 | hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2 * hDecoder->frameLength * sizeof(real_t)); |
799 | for (k = 0; k < 2 * hDecoder->frameLength; k++) { |
800 | hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1); |
801 | } |
802 | } |
803 | if (hDecoder->prev_fmd[cpe->paired_channel] == NULL) { |
804 | uint16_t k; |
805 | hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2 * hDecoder->frameLength * sizeof(real_t)); |
806 | for (k = 0; k < 2 * hDecoder->frameLength; k++) { |
807 | hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1); |
808 | } |
809 | } |
810 | } |
811 | #endif |
812 | |
813 | return 0; |
814 | } |
815 | |
816 | uint8_t reconstruct_single_channel(NeAACDecStruct *hDecoder, ic_stream *ics, |
817 | element *sce, int16_t *spec_data) |
818 | { |
819 | uint8_t retval; |
820 | int output_channels; |
821 | ALIGN real_t spec_coef[1024]; |
822 | |
823 | #ifdef PROFILE |
824 | int64_t count = faad_get_ts(); |
825 | #endif |
826 | |
827 | |
828 | /* always allocate 2 channels, PS can always "suddenly" turn up */ |
829 | #if ( (defined(DRM) && defined(DRM_PS)) ) |
830 | output_channels = 2; |
831 | #elif defined(PS_DEC) |
832 | if (hDecoder->ps_used[hDecoder->fr_ch_ele]) { |
833 | output_channels = 2; |
834 | } else { |
835 | output_channels = 1; |
836 | } |
837 | #else |
838 | output_channels = 1; |
839 | #endif |
840 | |
841 | if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0) { |
842 | /* element_output_channels not set yet */ |
843 | hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels; |
844 | } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) { |
845 | /* element inconsistency */ |
846 | |
847 | /* this only happens if PS is actually found but not in the first frame |
848 | * this means that there is only 1 bitstream element! |
849 | */ |
850 | |
851 | /* reset the allocation */ |
852 | hDecoder->element_alloced[hDecoder->fr_ch_ele] = 0; |
853 | |
854 | hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels; |
855 | |
856 | //return 21; |
857 | } |
858 | |
859 | if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0) { |
860 | retval = allocate_single_channel(hDecoder, sce->channel, output_channels); |
861 | if (retval > 0) { |
862 | return retval; |
863 | } |
864 | |
865 | hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1; |
866 | } |
867 | |
868 | |
869 | /* dequantisation and scaling */ |
870 | retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength); |
871 | if (retval > 0) { |
872 | return retval; |
873 | } |
874 | |
875 | #ifdef PROFILE |
876 | count = faad_get_ts() - count; |
877 | hDecoder->requant_cycles += count; |
878 | #endif |
879 | |
880 | |
881 | /* pns decoding */ |
882 | pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type, |
883 | &(hDecoder->__r1), &(hDecoder->__r2)); |
884 | |
885 | #ifdef MAIN_DEC |
886 | /* MAIN object type prediction */ |
887 | if (hDecoder->object_type == MAIN) { |
888 | if (!hDecoder->pred_stat[sce->channel]) { |
889 | return 33; |
890 | } |
891 | |
892 | /* intra channel prediction */ |
893 | ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength, |
894 | hDecoder->sf_index); |
895 | |
896 | /* In addition, for scalefactor bands coded by perceptual |
897 | noise substitution the predictors belonging to the |
898 | corresponding spectral coefficients are reset. |
899 | */ |
900 | pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]); |
901 | } |
902 | #endif |
903 | |
904 | #ifdef LTP_DEC |
905 | if (is_ltp_ot(hDecoder->object_type)) { |
906 | #ifdef LD_DEC |
907 | if (hDecoder->object_type == LD) { |
908 | if (ics->ltp.data_present) { |
909 | if (ics->ltp.lag_update) { |
910 | hDecoder->ltp_lag[sce->channel] = ics->ltp.lag; |
911 | } |
912 | } |
913 | ics->ltp.lag = hDecoder->ltp_lag[sce->channel]; |
914 | } |
915 | #endif |
916 | |
917 | /* long term prediction */ |
918 | lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb, |
919 | ics->window_shape, hDecoder->window_shape_prev[sce->channel], |
920 | hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength); |
921 | } |
922 | #endif |
923 | |
924 | /* tns decoding */ |
925 | tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type, |
926 | spec_coef, hDecoder->frameLength); |
927 | |
928 | /* drc decoding */ |
929 | if (hDecoder->drc->present) { |
930 | if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present) { |
931 | drc_decode(hDecoder->drc, spec_coef); |
932 | } |
933 | } |
934 | |
935 | /* filter bank */ |
936 | #ifdef SSR_DEC |
937 | if (hDecoder->object_type != SSR) { |
938 | #endif |
939 | ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape, |
940 | hDecoder->window_shape_prev[sce->channel], spec_coef, |
941 | hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel], |
942 | hDecoder->object_type, hDecoder->frameLength); |
943 | #ifdef SSR_DEC |
944 | } else { |
945 | ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape, |
946 | hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel], |
947 | hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel], |
948 | hDecoder->frameLength); |
949 | } |
950 | #endif |
951 | |
952 | /* save window shape for next frame */ |
953 | hDecoder->window_shape_prev[sce->channel] = ics->window_shape; |
954 | |
955 | #ifdef LTP_DEC |
956 | if (is_ltp_ot(hDecoder->object_type)) { |
957 | lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel], |
958 | hDecoder->fb_intermed[sce->channel], hDecoder->frameLength, hDecoder->object_type); |
959 | } |
960 | #endif |
961 | |
962 | #ifdef SBR_DEC |
963 | if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
964 | && hDecoder->sbr_alloced[hDecoder->fr_ch_ele]) { |
965 | int ele = hDecoder->fr_ch_ele; |
966 | int ch = sce->channel; |
967 | |
968 | /* following case can happen when forceUpSampling == 1 */ |
969 | if (hDecoder->sbr[ele] == NULL) { |
970 | hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, |
971 | hDecoder->element_id[ele], 2 * get_sample_rate(hDecoder->sf_index), |
972 | hDecoder->downSampledSBR |
973 | #ifdef DRM |
974 | , 0 |
975 | #endif |
976 | ); |
977 | } |
978 | |
979 | if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE) { |
980 | hDecoder->sbr[ele]->maxAACLine = 8 * min(sce->ics1.swb_offset[max(sce->ics1.max_sfb - 1, 0)], sce->ics1.swb_offset_max); |
981 | } else { |
982 | hDecoder->sbr[ele]->maxAACLine = min(sce->ics1.swb_offset[max(sce->ics1.max_sfb - 1, 0)], sce->ics1.swb_offset_max); |
983 | } |
984 | |
985 | /* check if any of the PS tools is used */ |
986 | #if (defined(PS_DEC) || defined(DRM_PS)) |
987 | if (hDecoder->ps_used[ele] == 0) { |
988 | #endif |
989 | retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch], |
990 | hDecoder->postSeekResetFlag, hDecoder->downSampledSBR); |
991 | #if (defined(PS_DEC) || defined(DRM_PS)) |
992 | } else { |
993 | retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch], |
994 | hDecoder->time_out[ch + 1], hDecoder->postSeekResetFlag, |
995 | hDecoder->downSampledSBR); |
996 | } |
997 | #endif |
998 | if (retval > 0) { |
999 | return retval; |
1000 | } |
1001 | } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
1002 | && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele]) { |
1003 | return 23; |
1004 | } |
1005 | #endif |
1006 | |
1007 | /* copy L to R when no PS is used */ |
1008 | #if (defined(PS_DEC) || defined(DRM_PS)) |
1009 | if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) && |
1010 | (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)) { |
1011 | int ele = hDecoder->fr_ch_ele; |
1012 | int ch = sce->channel; |
1013 | int frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1; |
1014 | frame_size *= hDecoder->frameLength * sizeof(real_t); |
1015 | |
1016 | memcpy(hDecoder->time_out[ch + 1], hDecoder->time_out[ch], frame_size); |
1017 | } |
1018 | #endif |
1019 | |
1020 | return 0; |
1021 | } |
1022 | |
1023 | uint8_t reconstruct_channel_pair(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2, |
1024 | element *cpe, int16_t *spec_data1, int16_t *spec_data2) |
1025 | { |
1026 | uint8_t retval; |
1027 | ALIGN real_t spec_coef1[1024]; |
1028 | ALIGN real_t spec_coef2[1024]; |
1029 | |
1030 | #ifdef PROFILE |
1031 | int64_t count = faad_get_ts(); |
1032 | #endif |
1033 | if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0) { |
1034 | retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel); |
1035 | if (retval > 0) { |
1036 | return retval; |
1037 | } |
1038 | |
1039 | hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1; |
1040 | } else { |
1041 | // 1ch --> 2ch aac stream, channel configuration mapping changed ,need reset decoder .mostly happened when channel num changed |
1042 | if (hDecoder->fb_intermed[(uint8_t)cpe->paired_channel] == NULL) { |
1043 | audio_codec_print("channel configure changed,need re-alloc the buffer\n"); |
1044 | return 34; |
1045 | } |
1046 | } |
1047 | |
1048 | /* dequantisation and scaling */ |
1049 | retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength); |
1050 | if (retval > 0) { |
1051 | return retval; |
1052 | } |
1053 | retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength); |
1054 | if (retval > 0) { |
1055 | return retval; |
1056 | } |
1057 | |
1058 | #ifdef PROFILE |
1059 | count = faad_get_ts() - count; |
1060 | hDecoder->requant_cycles += count; |
1061 | #endif |
1062 | |
1063 | |
1064 | /* pns decoding */ |
1065 | if (ics1->ms_mask_present) { |
1066 | pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type, |
1067 | &(hDecoder->__r1), &(hDecoder->__r2)); |
1068 | } else { |
1069 | pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type, |
1070 | &(hDecoder->__r1), &(hDecoder->__r2)); |
1071 | pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type, |
1072 | &(hDecoder->__r1), &(hDecoder->__r2)); |
1073 | } |
1074 | |
1075 | /* mid/side decoding */ |
1076 | ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength); |
1077 | |
1078 | #if 0 |
1079 | { |
1080 | int i; |
1081 | for (i = 0; i < 1024; i++) { |
1082 | //printf("%d\n", spec_coef1[i]); |
1083 | printf("0x%.8X\n", spec_coef1[i]); |
1084 | } |
1085 | for (i = 0; i < 1024; i++) { |
1086 | //printf("%d\n", spec_coef2[i]); |
1087 | printf("0x%.8X\n", spec_coef2[i]); |
1088 | } |
1089 | } |
1090 | #endif |
1091 | |
1092 | /* intensity stereo decoding */ |
1093 | is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength); |
1094 | |
1095 | #if 0 |
1096 | { |
1097 | int i; |
1098 | for (i = 0; i < 1024; i++) { |
1099 | printf("%d\n", spec_coef1[i]); |
1100 | //printf("0x%.8X\n", spec_coef1[i]); |
1101 | } |
1102 | for (i = 0; i < 1024; i++) { |
1103 | printf("%d\n", spec_coef2[i]); |
1104 | //printf("0x%.8X\n", spec_coef2[i]); |
1105 | } |
1106 | } |
1107 | #endif |
1108 | |
1109 | #ifdef MAIN_DEC |
1110 | /* MAIN object type prediction */ |
1111 | if (hDecoder->object_type == MAIN) { |
1112 | /* intra channel prediction */ |
1113 | ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength, |
1114 | hDecoder->sf_index); |
1115 | ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength, |
1116 | hDecoder->sf_index); |
1117 | |
1118 | /* In addition, for scalefactor bands coded by perceptual |
1119 | noise substitution the predictors belonging to the |
1120 | corresponding spectral coefficients are reset. |
1121 | */ |
1122 | pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]); |
1123 | pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]); |
1124 | } |
1125 | #endif |
1126 | |
1127 | #ifdef LTP_DEC |
1128 | if (is_ltp_ot(hDecoder->object_type)) { |
1129 | ltp_info *ltp1 = &(ics1->ltp); |
1130 | ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp); |
1131 | #ifdef LD_DEC |
1132 | if (hDecoder->object_type == LD) { |
1133 | if (ltp1->data_present) { |
1134 | if (ltp1->lag_update) { |
1135 | hDecoder->ltp_lag[cpe->channel] = ltp1->lag; |
1136 | } |
1137 | } |
1138 | ltp1->lag = hDecoder->ltp_lag[cpe->channel]; |
1139 | if (ltp2->data_present) { |
1140 | if (ltp2->lag_update) { |
1141 | hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag; |
1142 | } |
1143 | } |
1144 | ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel]; |
1145 | } |
1146 | #endif |
1147 | |
1148 | /* long term prediction */ |
1149 | lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb, |
1150 | ics1->window_shape, hDecoder->window_shape_prev[cpe->channel], |
1151 | hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength); |
1152 | lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb, |
1153 | ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel], |
1154 | hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength); |
1155 | } |
1156 | #endif |
1157 | |
1158 | /* tns decoding */ |
1159 | tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type, |
1160 | spec_coef1, hDecoder->frameLength); |
1161 | tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type, |
1162 | spec_coef2, hDecoder->frameLength); |
1163 | |
1164 | /* drc decoding */ |
1165 | if (hDecoder->drc->present) { |
1166 | if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present) { |
1167 | drc_decode(hDecoder->drc, spec_coef1); |
1168 | } |
1169 | if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present) { |
1170 | drc_decode(hDecoder->drc, spec_coef2); |
1171 | } |
1172 | } |
1173 | |
1174 | /* filter bank */ |
1175 | #ifdef SSR_DEC |
1176 | if (hDecoder->object_type != SSR) { |
1177 | #endif |
1178 | ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape, |
1179 | hDecoder->window_shape_prev[cpe->channel], spec_coef1, |
1180 | hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel], |
1181 | hDecoder->object_type, hDecoder->frameLength); |
1182 | ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape, |
1183 | hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, |
1184 | hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel], |
1185 | hDecoder->object_type, hDecoder->frameLength); |
1186 | #ifdef SSR_DEC |
1187 | } else { |
1188 | ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape, |
1189 | hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel], |
1190 | hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel], |
1191 | hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength); |
1192 | ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape, |
1193 | hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel], |
1194 | hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel], |
1195 | hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength); |
1196 | } |
1197 | #endif |
1198 | |
1199 | /* save window shape for next frame */ |
1200 | hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape; |
1201 | hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape; |
1202 | |
1203 | #ifdef LTP_DEC |
1204 | if (is_ltp_ot(hDecoder->object_type)) { |
1205 | lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel], |
1206 | hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type); |
1207 | lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel], |
1208 | hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type); |
1209 | } |
1210 | #endif |
1211 | |
1212 | #ifdef SBR_DEC |
1213 | if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
1214 | && hDecoder->sbr_alloced[hDecoder->fr_ch_ele]) { |
1215 | int ele = hDecoder->fr_ch_ele; |
1216 | int ch0 = cpe->channel; |
1217 | int ch1 = cpe->paired_channel; |
1218 | |
1219 | /* following case can happen when forceUpSampling == 1 */ |
1220 | if (hDecoder->sbr[ele] == NULL) { |
1221 | hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, |
1222 | hDecoder->element_id[ele], 2 * get_sample_rate(hDecoder->sf_index), |
1223 | hDecoder->downSampledSBR |
1224 | #ifdef DRM |
1225 | , 0 |
1226 | #endif |
1227 | ); |
1228 | } |
1229 | |
1230 | if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE) { |
1231 | hDecoder->sbr[ele]->maxAACLine = 8 * min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb - 1, 0)], cpe->ics1.swb_offset_max); |
1232 | } else { |
1233 | hDecoder->sbr[ele]->maxAACLine = min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb - 1, 0)], cpe->ics1.swb_offset_max); |
1234 | } |
1235 | |
1236 | retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele], |
1237 | hDecoder->time_out[ch0], hDecoder->time_out[ch1], |
1238 | hDecoder->postSeekResetFlag, hDecoder->downSampledSBR); |
1239 | if (retval > 0) { |
1240 | return retval; |
1241 | } |
1242 | } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
1243 | && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele]) { |
1244 | return 23; |
1245 | } |
1246 | #endif |
1247 | |
1248 | return 0; |
1249 | } |
1250 |