2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
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.
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
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: output.c,v 1.47 2009/01/26 23:51:15 menno Exp $
39 #define FLOAT_SCALE (1.0f/(1<<15))
41 #define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42 #define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
45 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
46 uint8_t down_matrix, uint8_t *internal_channel)
49 return input[internal_channel[channel]][sample];
53 return DM_MUL * (input[internal_channel[1]][sample] +
54 input[internal_channel[0]][sample] * RSQRT2 +
55 input[internal_channel[3]][sample] * RSQRT2);
57 return DM_MUL * (input[internal_channel[2]][sample] +
58 input[internal_channel[0]][sample] * RSQRT2 +
59 input[internal_channel[4]][sample] * RSQRT2);
64 #define CLIP(sample, max, min) \
76 #define CLIP(sample, max, min) \
87 #define CONV(a,b) ((a<<1)|(b&0x1))
89 static void to_PCM_16bit(NeAACDecStruct *hDecoder, real_t **input,
90 uint8_t channels, uint16_t frame_len,
91 int16_t **sample_buffer)
96 switch (CONV(channels,hDecoder->downMatrix))
100 for(i = 0; i < frame_len; i++)
102 real_t inp = input[hDecoder->internal_channel[0]][i];
104 CLIP(inp, 32767.0f, -32768.0f);
106 (*sample_buffer)[i] = (int16_t)lrintf(inp);
110 if (hDecoder->upMatrix)
112 ch = hDecoder->internal_channel[0];
113 for(i = 0; i < frame_len; i++)
115 real_t inp0 = input[ch][i];
117 CLIP(inp0, 32767.0f, -32768.0f);
119 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
123 ch = hDecoder->internal_channel[0];
124 ch1 = hDecoder->internal_channel[1];
125 for(i = 0; i < frame_len; i++)
127 real_t inp0 = input[ch ][i];
128 real_t inp1 = input[ch1][i];
130 CLIP(inp0, 32767.0f, -32768.0f);
131 CLIP(inp1, 32767.0f, -32768.0f);
133 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
139 for (ch = 0; ch < channels; ch++)
141 for(i = 0; i < frame_len; i++)
143 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
145 CLIP(inp, 32767.0f, -32768.0f);
147 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
154 static void to_PCM_24bit(NeAACDecStruct *hDecoder, real_t **input,
155 uint8_t channels, uint16_t frame_len,
156 int32_t **sample_buffer)
161 switch (CONV(channels,hDecoder->downMatrix))
165 for(i = 0; i < frame_len; i++)
167 real_t inp = input[hDecoder->internal_channel[0]][i];
170 CLIP(inp, 8388607.0f, -8388608.0f);
172 (*sample_buffer)[i] = (int32_t)lrintf(inp);
176 if (hDecoder->upMatrix)
178 ch = hDecoder->internal_channel[0];
179 for(i = 0; i < frame_len; i++)
181 real_t inp0 = input[ch][i];
184 CLIP(inp0, 8388607.0f, -8388608.0f);
186 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
190 ch = hDecoder->internal_channel[0];
191 ch1 = hDecoder->internal_channel[1];
192 for(i = 0; i < frame_len; i++)
194 real_t inp0 = input[ch ][i];
195 real_t inp1 = input[ch1][i];
199 CLIP(inp0, 8388607.0f, -8388608.0f);
200 CLIP(inp1, 8388607.0f, -8388608.0f);
202 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
208 for (ch = 0; ch < channels; ch++)
210 for(i = 0; i < frame_len; i++)
212 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
215 CLIP(inp, 8388607.0f, -8388608.0f);
217 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
224 static void to_PCM_32bit(NeAACDecStruct *hDecoder, real_t **input,
225 uint8_t channels, uint16_t frame_len,
226 int32_t **sample_buffer)
231 switch (CONV(channels,hDecoder->downMatrix))
235 for(i = 0; i < frame_len; i++)
237 real_t inp = input[hDecoder->internal_channel[0]][i];
240 CLIP(inp, 2147483647.0f, -2147483648.0f);
242 (*sample_buffer)[i] = (int32_t)lrintf(inp);
246 if (hDecoder->upMatrix)
248 ch = hDecoder->internal_channel[0];
249 for(i = 0; i < frame_len; i++)
251 real_t inp0 = input[ch][i];
254 CLIP(inp0, 2147483647.0f, -2147483648.0f);
256 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
260 ch = hDecoder->internal_channel[0];
261 ch1 = hDecoder->internal_channel[1];
262 for(i = 0; i < frame_len; i++)
264 real_t inp0 = input[ch ][i];
265 real_t inp1 = input[ch1][i];
269 CLIP(inp0, 2147483647.0f, -2147483648.0f);
270 CLIP(inp1, 2147483647.0f, -2147483648.0f);
272 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
278 for (ch = 0; ch < channels; ch++)
280 for(i = 0; i < frame_len; i++)
282 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
285 CLIP(inp, 2147483647.0f, -2147483648.0f);
287 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
294 static void to_PCM_float(NeAACDecStruct *hDecoder, real_t **input,
295 uint8_t channels, uint16_t frame_len,
296 float32_t **sample_buffer)
301 switch (CONV(channels,hDecoder->downMatrix))
305 for(i = 0; i < frame_len; i++)
307 real_t inp = input[hDecoder->internal_channel[0]][i];
308 (*sample_buffer)[i] = inp*FLOAT_SCALE;
312 if (hDecoder->upMatrix)
314 ch = hDecoder->internal_channel[0];
315 for(i = 0; i < frame_len; i++)
317 real_t inp0 = input[ch][i];
318 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319 (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
322 ch = hDecoder->internal_channel[0];
323 ch1 = hDecoder->internal_channel[1];
324 for(i = 0; i < frame_len; i++)
326 real_t inp0 = input[ch ][i];
327 real_t inp1 = input[ch1][i];
328 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329 (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
334 for (ch = 0; ch < channels; ch++)
336 for(i = 0; i < frame_len; i++)
338 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
346 static void to_PCM_double(NeAACDecStruct *hDecoder, real_t **input,
347 uint8_t channels, uint16_t frame_len,
348 double **sample_buffer)
353 switch (CONV(channels,hDecoder->downMatrix))
357 for(i = 0; i < frame_len; i++)
359 real_t inp = input[hDecoder->internal_channel[0]][i];
360 (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
364 if (hDecoder->upMatrix)
366 ch = hDecoder->internal_channel[0];
367 for(i = 0; i < frame_len; i++)
369 real_t inp0 = input[ch][i];
370 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371 (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
374 ch = hDecoder->internal_channel[0];
375 ch1 = hDecoder->internal_channel[1];
376 for(i = 0; i < frame_len; i++)
378 real_t inp0 = input[ch ][i];
379 real_t inp1 = input[ch1][i];
380 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381 (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
386 for (ch = 0; ch < channels; ch++)
388 for(i = 0; i < frame_len; i++)
390 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
398 void *output_to_PCM(NeAACDecStruct *hDecoder,
399 real_t **input, void *sample_buffer, uint8_t channels,
400 uint16_t frame_len, uint8_t format)
402 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
403 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
404 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405 double *double_sample_buffer = (double*)sample_buffer;
408 int64_t count = faad_get_ts();
411 /* Copy output to a standard PCM buffer */
415 to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
418 to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
421 to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
424 to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
426 case FAAD_FMT_DOUBLE:
427 to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
432 count = faad_get_ts() - count;
433 hDecoder->output_cycles += count;
436 return sample_buffer;
441 #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
442 #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
444 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
445 uint8_t down_matrix, uint8_t up_matrix,
446 uint8_t *internal_channel)
449 return input[internal_channel[0]][sample];
452 return input[internal_channel[channel]][sample];
456 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
457 real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
458 real_t cum = input[internal_channel[1]][sample] + C + L_S;
459 return MUL_F(cum, DM_MUL);
461 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
462 real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
463 real_t cum = input[internal_channel[2]][sample] + C + R_S;
464 return MUL_F(cum, DM_MUL);
468 void* output_to_PCM(NeAACDecStruct *hDecoder,
469 real_t **input, void *sample_buffer, uint8_t channels,
470 uint16_t frame_len, uint8_t format)
474 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
475 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477 /* Copy output to a standard PCM buffer */
478 for (ch = 0; ch < channels; ch++)
483 for(i = 0; i < frame_len; i++)
485 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
486 hDecoder->internal_channel);
489 tmp += (1 << (REAL_BITS-1));
490 if (tmp >= REAL_CONST(32767))
492 tmp = REAL_CONST(32767);
495 tmp += -(1 << (REAL_BITS-1));
496 if (tmp <= REAL_CONST(-32768))
498 tmp = REAL_CONST(-32768);
502 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
506 for(i = 0; i < frame_len; i++)
508 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
509 hDecoder->internal_channel);
512 tmp += (1 << (REAL_BITS-9));
513 tmp >>= (REAL_BITS-8);
519 tmp += -(1 << (REAL_BITS-9));
520 tmp >>= (REAL_BITS-8);
526 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
530 for(i = 0; i < frame_len; i++)
532 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
533 hDecoder->internal_channel);
536 tmp += (1 << (16-REAL_BITS-1));
537 tmp <<= (16-REAL_BITS);
539 tmp += -(1 << (16-REAL_BITS-1));
540 tmp <<= (16-REAL_BITS);
542 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
546 for(i = 0; i < frame_len; i++)
548 real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
549 hDecoder->internal_channel);
550 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
556 return sample_buffer;