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