Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / libfaad / output.c
blob6594582bbdf20ce8242440bede1cb61348322ec3
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
28 #include "common.h"
29 #include "structs.h"
31 #include "output.h"
32 #include "decoder.h"
34 #ifndef FIXED_POINT
37 #define FLOAT_SCALE (1.0f/(1<<15))
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)
43 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
44 uint8_t down_matrix, uint8_t *internal_channel)
46 if (!down_matrix)
47 return input[internal_channel[channel]][sample];
49 if (channel == 0)
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);
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; \
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; \
83 #endif
85 #define CONV(a,b) ((a<<1)|(b&0x1))
87 static void to_PCM_16bit(NeAACDecHandle hDecoder, real_t **input,
88 uint8_t channels, uint16_t frame_len,
89 int16_t **sample_buffer)
91 uint8_t ch, ch1;
92 uint16_t i;
94 switch (CONV(channels,hDecoder->downMatrix))
96 case CONV(1,0):
97 case CONV(1,1):
98 for(i = 0; i < frame_len; i++)
100 real_t inp = input[hDecoder->internal_channel[0]][i];
102 CLIP(inp, 32767.0f, -32768.0f);
104 (*sample_buffer)[i] = (int16_t)lrintf(inp);
106 break;
107 case CONV(2,0):
108 if (hDecoder->upMatrix)
110 ch = hDecoder->internal_channel[0];
111 for(i = 0; i < frame_len; i++)
113 real_t inp0 = input[ch][i];
115 CLIP(inp0, 32767.0f, -32768.0f);
117 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
118 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
120 } else {
121 ch = hDecoder->internal_channel[0];
122 ch1 = hDecoder->internal_channel[1];
123 for(i = 0; i < frame_len; i++)
125 real_t inp0 = input[ch ][i];
126 real_t inp1 = input[ch1][i];
128 CLIP(inp0, 32767.0f, -32768.0f);
129 CLIP(inp1, 32767.0f, -32768.0f);
131 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
132 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135 break;
136 default:
137 for (ch = 0; ch < channels; ch++)
139 for(i = 0; i < frame_len; i++)
141 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
143 CLIP(inp, 32767.0f, -32768.0f);
145 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148 break;
152 static void to_PCM_24bit(NeAACDecHandle hDecoder, real_t **input,
153 uint8_t channels, uint16_t frame_len,
154 int32_t **sample_buffer)
156 uint8_t ch, ch1;
157 uint16_t i;
159 switch (CONV(channels,hDecoder->downMatrix))
161 case CONV(1,0):
162 case CONV(1,1):
163 for(i = 0; i < frame_len; i++)
165 real_t inp = input[hDecoder->internal_channel[0]][i];
167 inp *= 256.0f;
168 CLIP(inp, 8388607.0f, -8388608.0f);
170 (*sample_buffer)[i] = (int32_t)lrintf(inp);
172 break;
173 case CONV(2,0):
174 if (hDecoder->upMatrix)
176 ch = hDecoder->internal_channel[0];
177 for(i = 0; i < frame_len; i++)
179 real_t inp0 = input[ch][i];
181 inp0 *= 256.0f;
182 CLIP(inp0, 8388607.0f, -8388608.0f);
184 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
185 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
187 } else {
188 ch = hDecoder->internal_channel[0];
189 ch1 = hDecoder->internal_channel[1];
190 for(i = 0; i < frame_len; i++)
192 real_t inp0 = input[ch ][i];
193 real_t inp1 = input[ch1][i];
195 inp0 *= 256.0f;
196 inp1 *= 256.0f;
197 CLIP(inp0, 8388607.0f, -8388608.0f);
198 CLIP(inp1, 8388607.0f, -8388608.0f);
200 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
201 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204 break;
205 default:
206 for (ch = 0; ch < channels; ch++)
208 for(i = 0; i < frame_len; i++)
210 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
212 inp *= 256.0f;
213 CLIP(inp, 8388607.0f, -8388608.0f);
215 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218 break;
222 static void to_PCM_32bit(NeAACDecHandle hDecoder, real_t **input,
223 uint8_t channels, uint16_t frame_len,
224 int32_t **sample_buffer)
226 uint8_t ch, ch1;
227 uint16_t i;
229 switch (CONV(channels,hDecoder->downMatrix))
231 case CONV(1,0):
232 case CONV(1,1):
233 for(i = 0; i < frame_len; i++)
235 real_t inp = input[hDecoder->internal_channel[0]][i];
237 inp *= 65536.0f;
238 CLIP(inp, 2147483647.0f, -2147483648.0f);
240 (*sample_buffer)[i] = (int32_t)lrintf(inp);
242 break;
243 case CONV(2,0):
244 if (hDecoder->upMatrix)
246 ch = hDecoder->internal_channel[0];
247 for(i = 0; i < frame_len; i++)
249 real_t inp0 = input[ch][i];
251 inp0 *= 65536.0f;
252 CLIP(inp0, 2147483647.0f, -2147483648.0f);
254 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
255 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
257 } else {
258 ch = hDecoder->internal_channel[0];
259 ch1 = hDecoder->internal_channel[1];
260 for(i = 0; i < frame_len; i++)
262 real_t inp0 = input[ch ][i];
263 real_t inp1 = input[ch1][i];
265 inp0 *= 65536.0f;
266 inp1 *= 65536.0f;
267 CLIP(inp0, 2147483647.0f, -2147483648.0f);
268 CLIP(inp1, 2147483647.0f, -2147483648.0f);
270 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
271 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274 break;
275 default:
276 for (ch = 0; ch < channels; ch++)
278 for(i = 0; i < frame_len; i++)
280 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
282 inp *= 65536.0f;
283 CLIP(inp, 2147483647.0f, -2147483648.0f);
285 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288 break;
292 static void to_PCM_float(NeAACDecHandle hDecoder, real_t **input,
293 uint8_t channels, uint16_t frame_len,
294 float32_t **sample_buffer)
296 uint8_t ch, ch1;
297 uint16_t i;
299 switch (CONV(channels,hDecoder->downMatrix))
301 case CONV(1,0):
302 case CONV(1,1):
303 for(i = 0; i < frame_len; i++)
305 real_t inp = input[hDecoder->internal_channel[0]][i];
306 (*sample_buffer)[i] = inp*FLOAT_SCALE;
308 break;
309 case CONV(2,0):
310 if (hDecoder->upMatrix)
312 ch = hDecoder->internal_channel[0];
313 for(i = 0; i < frame_len; i++)
315 real_t inp0 = input[ch][i];
316 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
317 (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
319 } else {
320 ch = hDecoder->internal_channel[0];
321 ch1 = hDecoder->internal_channel[1];
322 for(i = 0; i < frame_len; i++)
324 real_t inp0 = input[ch ][i];
325 real_t inp1 = input[ch1][i];
326 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
327 (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330 break;
331 default:
332 for (ch = 0; ch < channels; ch++)
334 for(i = 0; i < frame_len; i++)
336 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
337 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340 break;
344 static void to_PCM_double(NeAACDecHandle hDecoder, real_t **input,
345 uint8_t channels, uint16_t frame_len,
346 double **sample_buffer)
348 uint8_t ch, ch1;
349 uint16_t i;
351 switch (CONV(channels,hDecoder->downMatrix))
353 case CONV(1,0):
354 case CONV(1,1):
355 for(i = 0; i < frame_len; i++)
357 real_t inp = input[hDecoder->internal_channel[0]][i];
358 (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
360 break;
361 case CONV(2,0):
362 if (hDecoder->upMatrix)
364 ch = hDecoder->internal_channel[0];
365 for(i = 0; i < frame_len; i++)
367 real_t inp0 = input[ch][i];
368 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
369 (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
371 } else {
372 ch = hDecoder->internal_channel[0];
373 ch1 = hDecoder->internal_channel[1];
374 for(i = 0; i < frame_len; i++)
376 real_t inp0 = input[ch ][i];
377 real_t inp1 = input[ch1][i];
378 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
379 (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382 break;
383 default:
384 for (ch = 0; ch < channels; ch++)
386 for(i = 0; i < frame_len; i++)
388 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
389 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392 break;
396 void *output_to_PCM(NeAACDecHandle hDecoder,
397 real_t **input, void *sample_buffer, uint8_t channels,
398 uint16_t frame_len, uint8_t format)
400 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
401 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
402 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
403 double *double_sample_buffer = (double*)sample_buffer;
405 #ifdef PROFILE
406 int64_t count = faad_get_ts();
407 #endif
409 /* Copy output to a standard PCM buffer */
410 switch (format)
412 case FAAD_FMT_16BIT:
413 to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
414 break;
415 case FAAD_FMT_24BIT:
416 to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
417 break;
418 case FAAD_FMT_32BIT:
419 to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
420 break;
421 case FAAD_FMT_FLOAT:
422 to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
423 break;
424 case FAAD_FMT_DOUBLE:
425 to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
426 break;
429 #ifdef PROFILE
430 count = faad_get_ts() - count;
431 hDecoder->output_cycles += count;
432 #endif
434 return sample_buffer;
437 #else
439 #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
440 #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
442 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
443 uint8_t down_matrix, uint8_t up_matrix,
444 uint8_t *internal_channel)
446 if (up_matrix == 1)
447 return input[internal_channel[0]][sample];
449 if (!down_matrix)
450 return input[internal_channel[channel]][sample];
452 if (channel == 0)
454 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
455 real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
456 real_t cum = input[internal_channel[1]][sample] + C + L_S;
457 return MUL_F(cum, DM_MUL);
458 } else {
459 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
460 real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
461 real_t cum = input[internal_channel[2]][sample] + C + R_S;
462 return MUL_F(cum, DM_MUL);
466 void* output_to_PCM(NeAACDecHandle hDecoder,
467 real_t **input, void *sample_buffer, uint8_t channels,
468 uint16_t frame_len, uint8_t format)
470 uint8_t ch;
471 uint16_t i;
472 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
473 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
475 /* Copy output to a standard PCM buffer */
476 for (ch = 0; ch < channels; ch++)
478 switch (format)
480 case FAAD_FMT_16BIT:
481 for(i = 0; i < frame_len; i++)
483 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
484 hDecoder->internal_channel);
485 if (tmp >= 0)
487 tmp += (1 << (REAL_BITS-1));
488 if (tmp >= REAL_CONST(32767))
490 tmp = REAL_CONST(32767);
492 } else {
493 tmp += -(1 << (REAL_BITS-1));
494 if (tmp <= REAL_CONST(-32768))
496 tmp = REAL_CONST(-32768);
499 tmp >>= REAL_BITS;
500 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
502 break;
503 case FAAD_FMT_24BIT:
504 for(i = 0; i < frame_len; i++)
506 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
507 hDecoder->internal_channel);
508 if (tmp >= 0)
510 tmp += (1 << (REAL_BITS-9));
511 tmp >>= (REAL_BITS-8);
512 if (tmp >= 8388607)
514 tmp = 8388607;
516 } else {
517 tmp += -(1 << (REAL_BITS-9));
518 tmp >>= (REAL_BITS-8);
519 if (tmp <= -8388608)
521 tmp = -8388608;
524 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
526 break;
527 case FAAD_FMT_32BIT:
528 for(i = 0; i < frame_len; i++)
530 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
531 hDecoder->internal_channel);
532 if (tmp >= 0)
534 tmp += (1 << (16-REAL_BITS-1));
535 tmp <<= (16-REAL_BITS);
536 } else {
537 tmp += -(1 << (16-REAL_BITS-1));
538 tmp <<= (16-REAL_BITS);
540 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
542 break;
543 case FAAD_FMT_FIXED:
544 for(i = 0; i < frame_len; i++)
546 real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
547 hDecoder->internal_channel);
548 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
550 break;
554 return sample_buffer;
557 #endif