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 ** Initially modified for use with MPlayer by Rich Felker on 2005/03/29
24 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
36 #define FLOAT_SCALE (1.0f/(1<<15))
38 #define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
39 #define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
42 static INLINE real_t
get_sample(real_t
**input
, uint8_t channel
, uint16_t sample
,
43 uint8_t down_matrix
, uint8_t *internal_channel
)
46 return input
[internal_channel
[channel
]][sample
];
50 return DM_MUL
* (input
[internal_channel
[1]][sample
] +
51 input
[internal_channel
[0]][sample
] * RSQRT2
+
52 input
[internal_channel
[3]][sample
] * RSQRT2
);
54 return DM_MUL
* (input
[internal_channel
[2]][sample
] +
55 input
[internal_channel
[0]][sample
] * RSQRT2
+
56 input
[internal_channel
[4]][sample
] * RSQRT2
);
61 #define CLIP(sample, max, min) \
73 #define CLIP(sample, max, min) \
84 #define CONV(a,b) ((a<<1)|(b&0x1))
86 static void to_PCM_16bit(NeAACDecHandle hDecoder
, real_t
**input
,
87 uint8_t channels
, uint16_t frame_len
,
88 int16_t **sample_buffer
)
93 switch (CONV(channels
,hDecoder
->downMatrix
))
97 for(i
= 0; i
< frame_len
; i
++)
99 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
101 CLIP(inp
, 32767.0f
, -32768.0f
);
103 (*sample_buffer
)[i
] = (int16_t)lrintf(inp
);
107 if (hDecoder
->upMatrix
)
109 ch
= hDecoder
->internal_channel
[0];
110 for(i
= 0; i
< frame_len
; i
++)
112 real_t inp0
= input
[ch
][i
];
114 CLIP(inp0
, 32767.0f
, -32768.0f
);
116 (*sample_buffer
)[(i
*2)+0] = (int16_t)lrintf(inp0
);
117 (*sample_buffer
)[(i
*2)+1] = (int16_t)lrintf(inp0
);
120 ch
= hDecoder
->internal_channel
[0];
121 ch1
= hDecoder
->internal_channel
[1];
122 for(i
= 0; i
< frame_len
; i
++)
124 real_t inp0
= input
[ch
][i
];
125 real_t inp1
= input
[ch1
][i
];
127 CLIP(inp0
, 32767.0f
, -32768.0f
);
128 CLIP(inp1
, 32767.0f
, -32768.0f
);
130 (*sample_buffer
)[(i
*2)+0] = (int16_t)lrintf(inp0
);
131 (*sample_buffer
)[(i
*2)+1] = (int16_t)lrintf(inp1
);
136 for (ch
= 0; ch
< channels
; ch
++)
138 for(i
= 0; i
< frame_len
; i
++)
140 real_t inp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->internal_channel
);
142 CLIP(inp
, 32767.0f
, -32768.0f
);
144 (*sample_buffer
)[(i
*channels
)+ch
] = (int16_t)lrintf(inp
);
151 static void to_PCM_24bit(NeAACDecHandle hDecoder
, real_t
**input
,
152 uint8_t channels
, uint16_t frame_len
,
153 int32_t **sample_buffer
)
158 switch (CONV(channels
,hDecoder
->downMatrix
))
162 for(i
= 0; i
< frame_len
; i
++)
164 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
167 CLIP(inp
, 8388607.0f
, -8388608.0f
);
169 (*sample_buffer
)[i
] = (int32_t)lrintf(inp
);
173 if (hDecoder
->upMatrix
)
175 ch
= hDecoder
->internal_channel
[0];
176 for(i
= 0; i
< frame_len
; i
++)
178 real_t inp0
= input
[ch
][i
];
181 CLIP(inp0
, 8388607.0f
, -8388608.0f
);
183 (*sample_buffer
)[(i
*2)+0] = (int32_t)lrintf(inp0
);
184 (*sample_buffer
)[(i
*2)+1] = (int32_t)lrintf(inp0
);
187 ch
= hDecoder
->internal_channel
[0];
188 ch1
= hDecoder
->internal_channel
[1];
189 for(i
= 0; i
< frame_len
; i
++)
191 real_t inp0
= input
[ch
][i
];
192 real_t inp1
= input
[ch1
][i
];
196 CLIP(inp0
, 8388607.0f
, -8388608.0f
);
197 CLIP(inp1
, 8388607.0f
, -8388608.0f
);
199 (*sample_buffer
)[(i
*2)+0] = (int32_t)lrintf(inp0
);
200 (*sample_buffer
)[(i
*2)+1] = (int32_t)lrintf(inp1
);
205 for (ch
= 0; ch
< channels
; ch
++)
207 for(i
= 0; i
< frame_len
; i
++)
209 real_t inp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->internal_channel
);
212 CLIP(inp
, 8388607.0f
, -8388608.0f
);
214 (*sample_buffer
)[(i
*channels
)+ch
] = (int32_t)lrintf(inp
);
221 static void to_PCM_32bit(NeAACDecHandle hDecoder
, real_t
**input
,
222 uint8_t channels
, uint16_t frame_len
,
223 int32_t **sample_buffer
)
228 switch (CONV(channels
,hDecoder
->downMatrix
))
232 for(i
= 0; i
< frame_len
; i
++)
234 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
237 CLIP(inp
, 2147483647.0f
, -2147483648.0f
);
239 (*sample_buffer
)[i
] = (int32_t)lrintf(inp
);
243 if (hDecoder
->upMatrix
)
245 ch
= hDecoder
->internal_channel
[0];
246 for(i
= 0; i
< frame_len
; i
++)
248 real_t inp0
= input
[ch
][i
];
251 CLIP(inp0
, 2147483647.0f
, -2147483648.0f
);
253 (*sample_buffer
)[(i
*2)+0] = (int32_t)lrintf(inp0
);
254 (*sample_buffer
)[(i
*2)+1] = (int32_t)lrintf(inp0
);
257 ch
= hDecoder
->internal_channel
[0];
258 ch1
= hDecoder
->internal_channel
[1];
259 for(i
= 0; i
< frame_len
; i
++)
261 real_t inp0
= input
[ch
][i
];
262 real_t inp1
= input
[ch1
][i
];
266 CLIP(inp0
, 2147483647.0f
, -2147483648.0f
);
267 CLIP(inp1
, 2147483647.0f
, -2147483648.0f
);
269 (*sample_buffer
)[(i
*2)+0] = (int32_t)lrintf(inp0
);
270 (*sample_buffer
)[(i
*2)+1] = (int32_t)lrintf(inp1
);
275 for (ch
= 0; ch
< channels
; ch
++)
277 for(i
= 0; i
< frame_len
; i
++)
279 real_t inp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->internal_channel
);
282 CLIP(inp
, 2147483647.0f
, -2147483648.0f
);
284 (*sample_buffer
)[(i
*channels
)+ch
] = (int32_t)lrintf(inp
);
291 static void to_PCM_float(NeAACDecHandle hDecoder
, real_t
**input
,
292 uint8_t channels
, uint16_t frame_len
,
293 float32_t
**sample_buffer
)
298 switch (CONV(channels
,hDecoder
->downMatrix
))
302 for(i
= 0; i
< frame_len
; i
++)
304 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
305 (*sample_buffer
)[i
] = inp
*FLOAT_SCALE
;
309 if (hDecoder
->upMatrix
)
311 ch
= hDecoder
->internal_channel
[0];
312 for(i
= 0; i
< frame_len
; i
++)
314 real_t inp0
= input
[ch
][i
];
315 (*sample_buffer
)[(i
*2)+0] = inp0
*FLOAT_SCALE
;
316 (*sample_buffer
)[(i
*2)+1] = inp0
*FLOAT_SCALE
;
319 ch
= hDecoder
->internal_channel
[0];
320 ch1
= hDecoder
->internal_channel
[1];
321 for(i
= 0; i
< frame_len
; i
++)
323 real_t inp0
= input
[ch
][i
];
324 real_t inp1
= input
[ch1
][i
];
325 (*sample_buffer
)[(i
*2)+0] = inp0
*FLOAT_SCALE
;
326 (*sample_buffer
)[(i
*2)+1] = inp1
*FLOAT_SCALE
;
331 for (ch
= 0; ch
< channels
; ch
++)
333 for(i
= 0; i
< frame_len
; i
++)
335 real_t inp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->internal_channel
);
336 (*sample_buffer
)[(i
*channels
)+ch
] = inp
*FLOAT_SCALE
;
343 static void to_PCM_double(NeAACDecHandle hDecoder
, real_t
**input
,
344 uint8_t channels
, uint16_t frame_len
,
345 double **sample_buffer
)
350 switch (CONV(channels
,hDecoder
->downMatrix
))
354 for(i
= 0; i
< frame_len
; i
++)
356 real_t inp
= input
[hDecoder
->internal_channel
[0]][i
];
357 (*sample_buffer
)[i
] = (double)inp
*FLOAT_SCALE
;
361 if (hDecoder
->upMatrix
)
363 ch
= hDecoder
->internal_channel
[0];
364 for(i
= 0; i
< frame_len
; i
++)
366 real_t inp0
= input
[ch
][i
];
367 (*sample_buffer
)[(i
*2)+0] = (double)inp0
*FLOAT_SCALE
;
368 (*sample_buffer
)[(i
*2)+1] = (double)inp0
*FLOAT_SCALE
;
371 ch
= hDecoder
->internal_channel
[0];
372 ch1
= hDecoder
->internal_channel
[1];
373 for(i
= 0; i
< frame_len
; i
++)
375 real_t inp0
= input
[ch
][i
];
376 real_t inp1
= input
[ch1
][i
];
377 (*sample_buffer
)[(i
*2)+0] = (double)inp0
*FLOAT_SCALE
;
378 (*sample_buffer
)[(i
*2)+1] = (double)inp1
*FLOAT_SCALE
;
383 for (ch
= 0; ch
< channels
; ch
++)
385 for(i
= 0; i
< frame_len
; i
++)
387 real_t inp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->internal_channel
);
388 (*sample_buffer
)[(i
*channels
)+ch
] = (double)inp
*FLOAT_SCALE
;
395 void *output_to_PCM(NeAACDecHandle hDecoder
,
396 real_t
**input
, void *sample_buffer
, uint8_t channels
,
397 uint16_t frame_len
, uint8_t format
)
399 int16_t *short_sample_buffer
= (int16_t*)sample_buffer
;
400 int32_t *int_sample_buffer
= (int32_t*)sample_buffer
;
401 float32_t
*float_sample_buffer
= (float32_t
*)sample_buffer
;
402 double *double_sample_buffer
= (double*)sample_buffer
;
405 int64_t count
= faad_get_ts();
408 /* Copy output to a standard PCM buffer */
412 to_PCM_16bit(hDecoder
, input
, channels
, frame_len
, &short_sample_buffer
);
415 to_PCM_24bit(hDecoder
, input
, channels
, frame_len
, &int_sample_buffer
);
418 to_PCM_32bit(hDecoder
, input
, channels
, frame_len
, &int_sample_buffer
);
421 to_PCM_float(hDecoder
, input
, channels
, frame_len
, &float_sample_buffer
);
423 case FAAD_FMT_DOUBLE
:
424 to_PCM_double(hDecoder
, input
, channels
, frame_len
, &double_sample_buffer
);
429 count
= faad_get_ts() - count
;
430 hDecoder
->output_cycles
+= count
;
433 return sample_buffer
;
438 #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
439 #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
441 static INLINE real_t
get_sample(real_t
**input
, uint8_t channel
, uint16_t sample
,
442 uint8_t down_matrix
, uint8_t up_matrix
,
443 uint8_t *internal_channel
)
446 return input
[internal_channel
[0]][sample
];
449 return input
[internal_channel
[channel
]][sample
];
453 real_t C
= MUL_F(input
[internal_channel
[0]][sample
], RSQRT2
);
454 real_t L_S
= MUL_F(input
[internal_channel
[3]][sample
], RSQRT2
);
455 real_t cum
= input
[internal_channel
[1]][sample
] + C
+ L_S
;
456 return MUL_F(cum
, DM_MUL
);
458 real_t C
= MUL_F(input
[internal_channel
[0]][sample
], RSQRT2
);
459 real_t R_S
= MUL_F(input
[internal_channel
[4]][sample
], RSQRT2
);
460 real_t cum
= input
[internal_channel
[2]][sample
] + C
+ R_S
;
461 return MUL_F(cum
, DM_MUL
);
465 void* output_to_PCM_sux(NeAACDecHandle hDecoder
,
466 real_t
**input
, void *sample_buffer
, uint8_t channels
,
467 uint16_t frame_len
, uint8_t format
)
471 int16_t *short_sample_buffer
= (int16_t*)sample_buffer
;
472 int32_t *int_sample_buffer
= (int32_t*)sample_buffer
;
474 /* Copy output to a standard PCM buffer */
475 for (ch
= 0; ch
< channels
; ch
++)
480 for(i
= 0; i
< frame_len
; i
++)
482 int32_t tmp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->upMatrix
,
483 hDecoder
->internal_channel
);
486 tmp
+= (1 << (REAL_BITS
-1));
487 if (tmp
>= REAL_CONST(32767))
489 tmp
= REAL_CONST(32767);
492 tmp
+= -(1 << (REAL_BITS
-1));
493 if (tmp
<= REAL_CONST(-32768))
495 tmp
= REAL_CONST(-32768);
499 short_sample_buffer
[(i
*channels
)+ch
] = (int16_t)tmp
;
503 for(i
= 0; i
< frame_len
; i
++)
505 int32_t tmp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->upMatrix
,
506 hDecoder
->internal_channel
);
509 tmp
+= (1 << (REAL_BITS
-9));
510 tmp
>>= (REAL_BITS
-8);
516 tmp
+= -(1 << (REAL_BITS
-9));
517 tmp
>>= (REAL_BITS
-8);
523 int_sample_buffer
[(i
*channels
)+ch
] = (int32_t)tmp
;
527 for(i
= 0; i
< frame_len
; i
++)
529 int32_t tmp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->upMatrix
,
530 hDecoder
->internal_channel
);
533 tmp
+= (1 << (16-REAL_BITS
-1));
534 tmp
<<= (16-REAL_BITS
);
536 tmp
+= -(1 << (16-REAL_BITS
-1));
537 tmp
<<= (16-REAL_BITS
);
539 int_sample_buffer
[(i
*channels
)+ch
] = (int32_t)tmp
;
543 for(i
= 0; i
< frame_len
; i
++)
545 real_t tmp
= get_sample(input
, ch
, i
, hDecoder
->downMatrix
, hDecoder
->upMatrix
,
546 hDecoder
->internal_channel
);
547 int_sample_buffer
[(i
*channels
)+ch
] = (int32_t)tmp
;
553 return sample_buffer
;
556 void* output_to_PCM(NeAACDecHandle hDecoder
,
557 real_t
**input
, void *sample_buffer
, uint8_t channels
,
558 uint16_t frame_len
, uint8_t format
)
562 int16_t *short_sample_buffer
= (int16_t*)sample_buffer
;
563 real_t
*ch0
= input
[hDecoder
->internal_channel
[0]];
564 real_t
*ch1
= input
[hDecoder
->internal_channel
[1]];
565 real_t
*ch2
= input
[hDecoder
->internal_channel
[2]];
566 real_t
*ch3
= input
[hDecoder
->internal_channel
[3]];
567 real_t
*ch4
= input
[hDecoder
->internal_channel
[4]];
569 if (format
!= FAAD_FMT_16BIT
)
570 return output_to_PCM_sux(hDecoder
, input
, sample_buffer
, channels
, frame_len
, format
);
572 if (hDecoder
->downMatrix
) {
573 for(i
= 0; i
< frame_len
; i
++)
576 tmp
= (ch1
[i
] + ((ch0
[i
]+ch3
[i
])>>1) + ((ch0
[i
]+ch3
[i
])>>2) + (1<<(REAL_BITS
))) >> (REAL_BITS
+1);
577 if ((tmp
+0x8000) & ~0xffff) tmp
= ~(tmp
>>31)-0x8000;
578 short_sample_buffer
[0] = tmp
;
579 tmp
= (ch2
[i
] + ((ch0
[i
]+ch4
[i
])>>1) + ((ch0
[i
]+ch4
[i
])>>2) + (1<<(REAL_BITS
))) >> (REAL_BITS
+1);
580 if ((tmp
+0x8000) & ~0xffff) tmp
= ~(tmp
>>31)-0x8000;
581 short_sample_buffer
[1] = tmp
;
582 short_sample_buffer
+= channels
;
584 return sample_buffer
;
587 /* Copy output to a standard PCM buffer */
588 for(i
= 0; i
< frame_len
; i
++)
590 for (ch
= 0; ch
< channels
; ch
++)
592 int32_t tmp
= input
[ch
][i
];
593 tmp
+= (1 << (REAL_BITS
-1));
595 if ((tmp
+0x8000) & ~0xffff) tmp
= ~(tmp
>>31)-0x8000;
596 *(short_sample_buffer
++) = tmp
;
600 return sample_buffer
;