2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003 M. Bakker, Ahead Software 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 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
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
)
47 return input
[internal_channel
[channel
]][sample
];
51 return DM_MUL
* (input
[internal_channel
[1]][sample
] +
52 input
[internal_channel
[0]][sample
] * RSQRT2
+
53 input
[internal_channel
[3]][sample
] * RSQRT2
);
55 return DM_MUL
* (input
[internal_channel
[2]][sample
] +
56 input
[internal_channel
[0]][sample
] * RSQRT2
+
57 input
[internal_channel
[4]][sample
] * RSQRT2
);
62 #define CLIP(sample, max, min) \
74 #define CLIP(sample, max, min) \
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
)
94 switch (CONV(channels
,hDecoder
->downMatrix
))
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
);
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
);
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
);
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
);
152 static void to_PCM_24bit(NeAACDecHandle hDecoder
, real_t
**input
,
153 uint8_t channels
, uint16_t frame_len
,
154 int32_t **sample_buffer
)
159 switch (CONV(channels
,hDecoder
->downMatrix
))
163 for(i
= 0; i
< frame_len
; i
++)
165 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
168 CLIP(inp
, 8388607.0f
, -8388608.0f
);
170 (*sample_buffer
)[i
] = (int32_t)lrintf(inp
);
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
];
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
);
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
];
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
);
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
);
213 CLIP(inp
, 8388607.0f
, -8388608.0f
);
215 (*sample_buffer
)[(i
*channels
)+ch
] = (int32_t)lrintf(inp
);
222 static void to_PCM_32bit(NeAACDecHandle hDecoder
, real_t
**input
,
223 uint8_t channels
, uint16_t frame_len
,
224 int32_t **sample_buffer
)
229 switch (CONV(channels
,hDecoder
->downMatrix
))
233 for(i
= 0; i
< frame_len
; i
++)
235 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
238 CLIP(inp
, 2147483647.0f
, -2147483648.0f
);
240 (*sample_buffer
)[i
] = (int32_t)lrintf(inp
);
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
];
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
);
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
];
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
);
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
);
283 CLIP(inp
, 2147483647.0f
, -2147483648.0f
);
285 (*sample_buffer
)[(i
*channels
)+ch
] = (int32_t)lrintf(inp
);
292 static void to_PCM_float(NeAACDecHandle hDecoder
, real_t
**input
,
293 uint8_t channels
, uint16_t frame_len
,
294 float32_t
**sample_buffer
)
299 switch (CONV(channels
,hDecoder
->downMatrix
))
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
;
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
;
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
;
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
;
344 static void to_PCM_double(NeAACDecHandle hDecoder
, real_t
**input
,
345 uint8_t channels
, uint16_t frame_len
,
346 double **sample_buffer
)
351 switch (CONV(channels
,hDecoder
->downMatrix
))
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
;
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
;
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
;
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
;
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
;
406 int64_t count
= faad_get_ts();
409 /* Copy output to a standard PCM buffer */
413 to_PCM_16bit(hDecoder
, input
, channels
, frame_len
, &short_sample_buffer
);
416 to_PCM_24bit(hDecoder
, input
, channels
, frame_len
, &int_sample_buffer
);
419 to_PCM_32bit(hDecoder
, input
, channels
, frame_len
, &int_sample_buffer
);
422 to_PCM_float(hDecoder
, input
, channels
, frame_len
, &float_sample_buffer
);
424 case FAAD_FMT_DOUBLE
:
425 to_PCM_double(hDecoder
, input
, channels
, frame_len
, &double_sample_buffer
);
430 count
= faad_get_ts() - count
;
431 hDecoder
->output_cycles
+= count
;
434 return sample_buffer
;
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
)
447 return input
[internal_channel
[0]][sample
];
450 return input
[internal_channel
[channel
]][sample
];
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
);
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
)
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
++)
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
);
487 tmp
+= (1 << (REAL_BITS
-1));
488 if (tmp
>= REAL_CONST(32767))
490 tmp
= REAL_CONST(32767);
493 tmp
+= -(1 << (REAL_BITS
-1));
494 if (tmp
<= REAL_CONST(-32768))
496 tmp
= REAL_CONST(-32768);
500 short_sample_buffer
[(i
*channels
)+ch
] = (int16_t)tmp
;
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
);
510 tmp
+= (1 << (REAL_BITS
-9));
511 tmp
>>= (REAL_BITS
-8);
517 tmp
+= -(1 << (REAL_BITS
-9));
518 tmp
>>= (REAL_BITS
-8);
524 int_sample_buffer
[(i
*channels
)+ch
] = (int32_t)tmp
;
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
);
534 tmp
+= (1 << (16-REAL_BITS
-1));
535 tmp
<<= (16-REAL_BITS
);
537 tmp
+= -(1 << (16-REAL_BITS
-1));
538 tmp
<<= (16-REAL_BITS
);
540 int_sample_buffer
[(i
*channels
)+ch
] = (int32_t)tmp
;
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
;
554 return sample_buffer
;