comparison libfaad2/specrec.c @ 12527:4a370c80fe5c

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