10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
|
25 ** $Id$
|
|
26 **/
|
|
27
|
|
28 #include "common.h"
|
|
29 #include "structs.h"
|
|
30
|
|
31 #include "output.h"
|
|
32 #include "decoder.h"
|
|
33
|
|
34 #ifndef FIXED_POINT
|
|
35
|
|
36
|
|
37 #define FLOAT_SCALE (1.0f/(1<<15))
|
|
38
|
12527
|
39 #define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
|
|
40 #define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
|
10725
|
41
|
|
42
|
|
43 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
|
12527
|
44 uint8_t down_matrix, uint8_t *internal_channel)
|
|
45 {
|
|
46 if (!down_matrix)
|
|
47 return input[internal_channel[channel]][sample];
|
|
48
|
|
49 if (channel == 0)
|
|
50 {
|
|
51 return DM_MUL * (input[internal_channel[1]][sample] +
|
|
52 input[internal_channel[0]][sample] * RSQRT2 +
|
|
53 input[internal_channel[3]][sample] * RSQRT2);
|
|
54 } else {
|
|
55 return DM_MUL * (input[internal_channel[2]][sample] +
|
|
56 input[internal_channel[0]][sample] * RSQRT2 +
|
|
57 input[internal_channel[4]][sample] * RSQRT2);
|
|
58 }
|
|
59 }
|
|
60
|
|
61 #ifndef HAS_LRINTF
|
|
62 #define CLIP(sample, max, min) \
|
|
63 if (sample >= 0.0f) \
|
|
64 { \
|
|
65 sample += 0.5f; \
|
|
66 if (sample >= max) \
|
|
67 sample = max; \
|
|
68 } else { \
|
|
69 sample += -0.5f; \
|
|
70 if (sample <= min) \
|
|
71 sample = min; \
|
|
72 }
|
|
73 #else
|
|
74 #define CLIP(sample, max, min) \
|
|
75 if (sample >= 0.0f) \
|
|
76 { \
|
|
77 if (sample >= max) \
|
|
78 sample = max; \
|
|
79 } else { \
|
|
80 if (sample <= min) \
|
|
81 sample = min; \
|
|
82 }
|
|
83 #endif
|
|
84
|
|
85 #define CONV(a,b) ((a<<1)|(b&0x1))
|
|
86
|
|
87 static void to_PCM_16bit(faacDecHandle hDecoder, real_t **input,
|
|
88 uint8_t channels, uint16_t frame_len,
|
|
89 int16_t **sample_buffer)
|
10725
|
90 {
|
12527
|
91 uint8_t ch, ch1;
|
|
92 uint16_t i;
|
|
93
|
|
94 switch (CONV(channels,hDecoder->downMatrix))
|
|
95 {
|
|
96 case CONV(1,0):
|
|
97 case CONV(1,1):
|
|
98 for(i = 0; i < frame_len; i++)
|
|
99 {
|
|
100 real_t inp = input[hDecoder->internal_channel[0]][i];
|
|
101
|
|
102 CLIP(inp, 32767.0f, -32768.0f);
|
|
103
|
|
104 (*sample_buffer)[i] = (int16_t)lrintf(inp);
|
|
105 }
|
|
106 break;
|
|
107 case CONV(2,0):
|
|
108 ch = hDecoder->internal_channel[0];
|
|
109 ch1 = hDecoder->internal_channel[1];
|
|
110 for(i = 0; i < frame_len; i++)
|
|
111 {
|
|
112 real_t inp0 = input[ch ][i];
|
|
113 real_t inp1 = input[ch1][i];
|
|
114
|
|
115 CLIP(inp0, 32767.0f, -32768.0f);
|
|
116 CLIP(inp1, 32767.0f, -32768.0f);
|
|
117
|
|
118 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
|
|
119 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
|
|
120 }
|
|
121 break;
|
|
122 default:
|
|
123 for (ch = 0; ch < channels; ch++)
|
|
124 {
|
|
125 for(i = 0; i < frame_len; i++)
|
|
126 {
|
|
127 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
128
|
|
129 CLIP(inp, 32767.0f, -32768.0f);
|
|
130
|
|
131 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
|
|
132 }
|
|
133 }
|
|
134 break;
|
|
135 }
|
|
136 }
|
|
137
|
|
138 static void to_PCM_24bit(faacDecHandle hDecoder, real_t **input,
|
|
139 uint8_t channels, uint16_t frame_len,
|
|
140 int32_t **sample_buffer)
|
|
141 {
|
|
142 uint8_t ch, ch1;
|
|
143 uint16_t i;
|
|
144
|
|
145 switch (CONV(channels,hDecoder->downMatrix))
|
10725
|
146 {
|
12527
|
147 case CONV(1,0):
|
|
148 case CONV(1,1):
|
|
149 for(i = 0; i < frame_len; i++)
|
|
150 {
|
|
151 real_t inp = input[hDecoder->internal_channel[0]][i];
|
|
152
|
|
153 inp *= 256.0f;
|
|
154 CLIP(inp, 8388607.0f, -8388608.0f);
|
|
155
|
|
156 (*sample_buffer)[i] = (int32_t)lrintf(inp);
|
|
157 }
|
|
158 break;
|
|
159 case CONV(2,0):
|
|
160 ch = hDecoder->internal_channel[0];
|
|
161 ch1 = hDecoder->internal_channel[1];
|
|
162 for(i = 0; i < frame_len; i++)
|
|
163 {
|
|
164 real_t inp0 = input[ch ][i];
|
|
165 real_t inp1 = input[ch1][i];
|
|
166
|
|
167 inp0 *= 256.0f;
|
|
168 inp1 *= 256.0f;
|
|
169 CLIP(inp0, 8388607.0f, -8388608.0f);
|
|
170 CLIP(inp1, 8388607.0f, -8388608.0f);
|
|
171
|
|
172 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
|
|
173 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
|
|
174 }
|
|
175 break;
|
|
176 default:
|
|
177 for (ch = 0; ch < channels; ch++)
|
|
178 {
|
|
179 for(i = 0; i < frame_len; i++)
|
|
180 {
|
|
181 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
182
|
|
183 inp *= 256.0f;
|
|
184 CLIP(inp, 8388607.0f, -8388608.0f);
|
|
185
|
|
186 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
|
|
187 }
|
|
188 }
|
|
189 break;
|
|
190 }
|
|
191 }
|
|
192
|
|
193 static void to_PCM_32bit(faacDecHandle hDecoder, real_t **input,
|
|
194 uint8_t channels, uint16_t frame_len,
|
|
195 int32_t **sample_buffer)
|
|
196 {
|
|
197 uint8_t ch, ch1;
|
|
198 uint16_t i;
|
|
199
|
|
200 switch (CONV(channels,hDecoder->downMatrix))
|
|
201 {
|
|
202 case CONV(1,0):
|
|
203 case CONV(1,1):
|
|
204 for(i = 0; i < frame_len; i++)
|
|
205 {
|
|
206 real_t inp = input[hDecoder->internal_channel[0]][i];
|
|
207
|
|
208 inp *= 65536.0f;
|
|
209 CLIP(inp, 2147483647.0f, -2147483648.0f);
|
|
210
|
|
211 (*sample_buffer)[i] = (int32_t)lrintf(inp);
|
|
212 }
|
|
213 break;
|
|
214 case CONV(2,0):
|
|
215 ch = hDecoder->internal_channel[0];
|
|
216 ch1 = hDecoder->internal_channel[1];
|
|
217 for(i = 0; i < frame_len; i++)
|
10725
|
218 {
|
12527
|
219 real_t inp0 = input[ch ][i];
|
|
220 real_t inp1 = input[ch1][i];
|
|
221
|
|
222 inp0 *= 65536.0f;
|
|
223 inp1 *= 65536.0f;
|
|
224 CLIP(inp0, 2147483647.0f, -2147483648.0f);
|
|
225 CLIP(inp1, 2147483647.0f, -2147483648.0f);
|
|
226
|
|
227 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
|
|
228 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
|
|
229 }
|
|
230 break;
|
|
231 default:
|
|
232 for (ch = 0; ch < channels; ch++)
|
|
233 {
|
|
234 for(i = 0; i < frame_len; i++)
|
|
235 {
|
|
236 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
237
|
|
238 inp *= 65536.0f;
|
|
239 CLIP(inp, 2147483647.0f, -2147483648.0f);
|
|
240
|
|
241 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
|
|
242 }
|
|
243 }
|
|
244 break;
|
|
245 }
|
|
246 }
|
|
247
|
|
248 static void to_PCM_float(faacDecHandle hDecoder, real_t **input,
|
|
249 uint8_t channels, uint16_t frame_len,
|
|
250 float32_t **sample_buffer)
|
|
251 {
|
|
252 uint8_t ch, ch1;
|
|
253 uint16_t i;
|
|
254
|
|
255 switch (CONV(channels,hDecoder->downMatrix))
|
|
256 {
|
|
257 case CONV(1,0):
|
|
258 case CONV(1,1):
|
|
259 for(i = 0; i < frame_len; i++)
|
|
260 {
|
|
261 real_t inp = input[hDecoder->internal_channel[0]][i];
|
|
262 (*sample_buffer)[i] = inp*FLOAT_SCALE;
|
|
263 }
|
|
264 break;
|
|
265 case CONV(2,0):
|
|
266 ch = hDecoder->internal_channel[0];
|
|
267 ch1 = hDecoder->internal_channel[1];
|
|
268 for(i = 0; i < frame_len; i++)
|
|
269 {
|
|
270 real_t inp0 = input[ch ][i];
|
|
271 real_t inp1 = input[ch1][i];
|
|
272 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
|
|
273 (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
|
|
274 }
|
|
275 break;
|
|
276 default:
|
|
277 for (ch = 0; ch < channels; ch++)
|
|
278 {
|
|
279 for(i = 0; i < frame_len; i++)
|
|
280 {
|
|
281 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
282 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
|
|
283 }
|
|
284 }
|
|
285 break;
|
|
286 }
|
|
287 }
|
|
288
|
|
289 static void to_PCM_double(faacDecHandle hDecoder, real_t **input,
|
|
290 uint8_t channels, uint16_t frame_len,
|
|
291 double **sample_buffer)
|
|
292 {
|
|
293 uint8_t ch, ch1;
|
|
294 uint16_t i;
|
|
295
|
|
296 switch (CONV(channels,hDecoder->downMatrix))
|
|
297 {
|
|
298 case CONV(1,0):
|
|
299 case CONV(1,1):
|
|
300 for(i = 0; i < frame_len; i++)
|
|
301 {
|
|
302 real_t inp = input[hDecoder->internal_channel[0]][i];
|
|
303 (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
|
10725
|
304 }
|
12527
|
305 break;
|
|
306 case CONV(2,0):
|
|
307 ch = hDecoder->internal_channel[0];
|
|
308 ch1 = hDecoder->internal_channel[1];
|
|
309 for(i = 0; i < frame_len; i++)
|
|
310 {
|
|
311 real_t inp0 = input[ch ][i];
|
|
312 real_t inp1 = input[ch1][i];
|
|
313 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
|
|
314 (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
|
|
315 }
|
|
316 break;
|
|
317 default:
|
|
318 for (ch = 0; ch < channels; ch++)
|
|
319 {
|
|
320 for(i = 0; i < frame_len; i++)
|
|
321 {
|
|
322 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
323 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
|
|
324 }
|
|
325 }
|
|
326 break;
|
|
327 }
|
|
328 }
|
|
329
|
|
330 void *output_to_PCM(faacDecHandle hDecoder,
|
|
331 real_t **input, void *sample_buffer, uint8_t channels,
|
|
332 uint16_t frame_len, uint8_t format)
|
|
333 {
|
|
334 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
|
|
335 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
|
|
336 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
|
|
337 double *double_sample_buffer = (double*)sample_buffer;
|
|
338
|
|
339 #ifdef PROFILE
|
|
340 int64_t count = faad_get_ts();
|
|
341 #endif
|
|
342
|
|
343 /* Copy output to a standard PCM buffer */
|
|
344 switch (format)
|
|
345 {
|
|
346 case FAAD_FMT_16BIT:
|
|
347 to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
|
|
348 break;
|
|
349 case FAAD_FMT_24BIT:
|
|
350 to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
|
|
351 break;
|
|
352 case FAAD_FMT_32BIT:
|
|
353 to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
|
|
354 break;
|
|
355 case FAAD_FMT_FLOAT:
|
|
356 to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
|
|
357 break;
|
|
358 case FAAD_FMT_DOUBLE:
|
|
359 to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
|
|
360 break;
|
|
361 }
|
|
362
|
|
363 #ifdef PROFILE
|
|
364 count = faad_get_ts() - count;
|
|
365 hDecoder->output_cycles += count;
|
|
366 #endif
|
|
367
|
|
368 return sample_buffer;
|
|
369 }
|
|
370
|
|
371 #else
|
|
372
|
|
373 #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
|
|
374 #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
|
|
375
|
|
376 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
|
|
377 uint8_t down_matrix, uint8_t *internal_channel)
|
|
378 {
|
|
379 if (!down_matrix)
|
|
380 return input[internal_channel[channel]][sample];
|
|
381
|
|
382 if (channel == 0)
|
|
383 {
|
|
384 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
|
|
385 real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
|
|
386 real_t cum = input[internal_channel[1]][sample] + C + L_S;
|
|
387 return MUL_F(cum, DM_MUL);
|
10725
|
388 } else {
|
12527
|
389 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
|
|
390 real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
|
|
391 real_t cum = input[internal_channel[2]][sample] + C + R_S;
|
|
392 return MUL_F(cum, DM_MUL);
|
10725
|
393 }
|
|
394 }
|
|
395
|
|
396 void* output_to_PCM(faacDecHandle hDecoder,
|
|
397 real_t **input, void *sample_buffer, uint8_t channels,
|
|
398 uint16_t frame_len, uint8_t format)
|
|
399 {
|
|
400 uint8_t ch;
|
12527
|
401 uint16_t i;
|
|
402 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
|
|
403 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
|
10725
|
404
|
|
405 /* Copy output to a standard PCM buffer */
|
|
406 for (ch = 0; ch < channels; ch++)
|
|
407 {
|
|
408 switch (format)
|
|
409 {
|
|
410 case FAAD_FMT_16BIT:
|
|
411 for(i = 0; i < frame_len; i++)
|
|
412 {
|
12527
|
413 //int32_t tmp = input[ch][i];
|
|
414 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
415 if (tmp >= 0)
|
|
416 {
|
|
417 tmp += (1 << (REAL_BITS-1));
|
|
418 if (tmp >= REAL_CONST(32767))
|
|
419 {
|
|
420 tmp = REAL_CONST(32767);
|
|
421 }
|
|
422 } else {
|
|
423 tmp += -(1 << (REAL_BITS-1));
|
|
424 if (tmp <= REAL_CONST(-32768))
|
|
425 {
|
|
426 tmp = REAL_CONST(-32768);
|
|
427 }
|
|
428 }
|
|
429 tmp >>= REAL_BITS;
|
|
430 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
|
10725
|
431 }
|
|
432 break;
|
|
433 case FAAD_FMT_24BIT:
|
|
434 for(i = 0; i < frame_len; i++)
|
|
435 {
|
12527
|
436 //int32_t tmp = input[ch][i];
|
|
437 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
438 if (tmp >= 0)
|
|
439 {
|
|
440 tmp += (1 << (REAL_BITS-9));
|
|
441 tmp >>= (REAL_BITS-8);
|
|
442 if (tmp >= 8388607)
|
|
443 {
|
|
444 tmp = 8388607;
|
|
445 }
|
|
446 } else {
|
|
447 tmp += -(1 << (REAL_BITS-9));
|
|
448 tmp >>= (REAL_BITS-8);
|
|
449 if (tmp <= -8388608)
|
|
450 {
|
|
451 tmp = -8388608;
|
|
452 }
|
|
453 }
|
|
454 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
|
10725
|
455 }
|
|
456 break;
|
|
457 case FAAD_FMT_32BIT:
|
|
458 for(i = 0; i < frame_len; i++)
|
|
459 {
|
12527
|
460 //int32_t tmp = input[ch][i];
|
|
461 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
462 if (tmp >= 0)
|
|
463 {
|
|
464 tmp += (1 << (16-REAL_BITS-1));
|
|
465 tmp <<= (16-REAL_BITS);
|
|
466 } else {
|
|
467 tmp += -(1 << (16-REAL_BITS-1));
|
|
468 tmp <<= (16-REAL_BITS);
|
|
469 }
|
|
470 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
|
10725
|
471 }
|
|
472 break;
|
|
473 }
|
|
474 }
|
|
475
|
|
476 return sample_buffer;
|
|
477 }
|
|
478
|
|
479 #endif
|