summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libfaad/output.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/output.c')
-rw-r--r--lib/rbcodec/codecs/libfaad/output.c557
1 files changed, 557 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/output.c b/lib/rbcodec/codecs/libfaad/output.c
new file mode 100644
index 0000000000..6594582bbd
--- /dev/null
+++ b/lib/rbcodec/codecs/libfaad/output.c
@@ -0,0 +1,557 @@
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
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)
41
42
43static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
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) \
63if (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) \
75if (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
87static void to_PCM_16bit(NeAACDecHandle hDecoder, real_t **input,
88 uint8_t channels, uint16_t frame_len,
89 int16_t **sample_buffer)
90{
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 if (hDecoder->upMatrix)
109 {
110 ch = hDecoder->internal_channel[0];
111 for(i = 0; i < frame_len; i++)
112 {
113 real_t inp0 = input[ch][i];
114
115 CLIP(inp0, 32767.0f, -32768.0f);
116
117 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
118 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
119 }
120 } else {
121 ch = hDecoder->internal_channel[0];
122 ch1 = hDecoder->internal_channel[1];
123 for(i = 0; i < frame_len; i++)
124 {
125 real_t inp0 = input[ch ][i];
126 real_t inp1 = input[ch1][i];
127
128 CLIP(inp0, 32767.0f, -32768.0f);
129 CLIP(inp1, 32767.0f, -32768.0f);
130
131 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
132 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
133 }
134 }
135 break;
136 default:
137 for (ch = 0; ch < channels; ch++)
138 {
139 for(i = 0; i < frame_len; i++)
140 {
141 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
142
143 CLIP(inp, 32767.0f, -32768.0f);
144
145 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
146 }
147 }
148 break;
149 }
150}
151
152static void to_PCM_24bit(NeAACDecHandle hDecoder, real_t **input,
153 uint8_t channels, uint16_t frame_len,
154 int32_t **sample_buffer)
155{
156 uint8_t ch, ch1;
157 uint16_t i;
158
159 switch (CONV(channels,hDecoder->downMatrix))
160 {
161 case CONV(1,0):
162 case CONV(1,1):
163 for(i = 0; i < frame_len; i++)
164 {
165 real_t inp = input[hDecoder->internal_channel[0]][i];
166
167 inp *= 256.0f;
168 CLIP(inp, 8388607.0f, -8388608.0f);
169
170 (*sample_buffer)[i] = (int32_t)lrintf(inp);
171 }
172 break;
173 case CONV(2,0):
174 if (hDecoder->upMatrix)
175 {
176 ch = hDecoder->internal_channel[0];
177 for(i = 0; i < frame_len; i++)
178 {
179 real_t inp0 = input[ch][i];
180
181 inp0 *= 256.0f;
182 CLIP(inp0, 8388607.0f, -8388608.0f);
183
184 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
185 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
186 }
187 } else {
188 ch = hDecoder->internal_channel[0];
189 ch1 = hDecoder->internal_channel[1];
190 for(i = 0; i < frame_len; i++)
191 {
192 real_t inp0 = input[ch ][i];
193 real_t inp1 = input[ch1][i];
194
195 inp0 *= 256.0f;
196 inp1 *= 256.0f;
197 CLIP(inp0, 8388607.0f, -8388608.0f);
198 CLIP(inp1, 8388607.0f, -8388608.0f);
199
200 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
201 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
202 }
203 }
204 break;
205 default:
206 for (ch = 0; ch < channels; ch++)
207 {
208 for(i = 0; i < frame_len; i++)
209 {
210 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
211
212 inp *= 256.0f;
213 CLIP(inp, 8388607.0f, -8388608.0f);
214
215 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
216 }
217 }
218 break;
219 }
220}
221
222static void to_PCM_32bit(NeAACDecHandle hDecoder, real_t **input,
223 uint8_t channels, uint16_t frame_len,
224 int32_t **sample_buffer)
225{
226 uint8_t ch, ch1;
227 uint16_t i;
228
229 switch (CONV(channels,hDecoder->downMatrix))
230 {
231 case CONV(1,0):
232 case CONV(1,1):
233 for(i = 0; i < frame_len; i++)
234 {
235 real_t inp = input[hDecoder->internal_channel[0]][i];
236
237 inp *= 65536.0f;
238 CLIP(inp, 2147483647.0f, -2147483648.0f);
239
240 (*sample_buffer)[i] = (int32_t)lrintf(inp);
241 }
242 break;
243 case CONV(2,0):
244 if (hDecoder->upMatrix)
245 {
246 ch = hDecoder->internal_channel[0];
247 for(i = 0; i < frame_len; i++)
248 {
249 real_t inp0 = input[ch][i];
250
251 inp0 *= 65536.0f;
252 CLIP(inp0, 2147483647.0f, -2147483648.0f);
253
254 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
255 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
256 }
257 } else {
258 ch = hDecoder->internal_channel[0];
259 ch1 = hDecoder->internal_channel[1];
260 for(i = 0; i < frame_len; i++)
261 {
262 real_t inp0 = input[ch ][i];
263 real_t inp1 = input[ch1][i];
264
265 inp0 *= 65536.0f;
266 inp1 *= 65536.0f;
267 CLIP(inp0, 2147483647.0f, -2147483648.0f);
268 CLIP(inp1, 2147483647.0f, -2147483648.0f);
269
270 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
271 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
272 }
273 }
274 break;
275 default:
276 for (ch = 0; ch < channels; ch++)
277 {
278 for(i = 0; i < frame_len; i++)
279 {
280 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
281
282 inp *= 65536.0f;
283 CLIP(inp, 2147483647.0f, -2147483648.0f);
284
285 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
286 }
287 }
288 break;
289 }
290}
291
292static void to_PCM_float(NeAACDecHandle hDecoder, real_t **input,
293 uint8_t channels, uint16_t frame_len,
294 float32_t **sample_buffer)
295{
296 uint8_t ch, ch1;
297 uint16_t i;
298
299 switch (CONV(channels,hDecoder->downMatrix))
300 {
301 case CONV(1,0):
302 case CONV(1,1):
303 for(i = 0; i < frame_len; i++)
304 {
305 real_t inp = input[hDecoder->internal_channel[0]][i];
306 (*sample_buffer)[i] = inp*FLOAT_SCALE;
307 }
308 break;
309 case CONV(2,0):
310 if (hDecoder->upMatrix)
311 {
312 ch = hDecoder->internal_channel[0];
313 for(i = 0; i < frame_len; i++)
314 {
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;
318 }
319 } else {
320 ch = hDecoder->internal_channel[0];
321 ch1 = hDecoder->internal_channel[1];
322 for(i = 0; i < frame_len; i++)
323 {
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;
328 }
329 }
330 break;
331 default:
332 for (ch = 0; ch < channels; ch++)
333 {
334 for(i = 0; i < frame_len; i++)
335 {
336 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
337 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
338 }
339 }
340 break;
341 }
342}
343
344static void to_PCM_double(NeAACDecHandle hDecoder, real_t **input,
345 uint8_t channels, uint16_t frame_len,
346 double **sample_buffer)
347{
348 uint8_t ch, ch1;
349 uint16_t i;
350
351 switch (CONV(channels,hDecoder->downMatrix))
352 {
353 case CONV(1,0):
354 case CONV(1,1):
355 for(i = 0; i < frame_len; i++)
356 {
357 real_t inp = input[hDecoder->internal_channel[0]][i];
358 (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
359 }
360 break;
361 case CONV(2,0):
362 if (hDecoder->upMatrix)
363 {
364 ch = hDecoder->internal_channel[0];
365 for(i = 0; i < frame_len; i++)
366 {
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;
370 }
371 } else {
372 ch = hDecoder->internal_channel[0];
373 ch1 = hDecoder->internal_channel[1];
374 for(i = 0; i < frame_len; i++)
375 {
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;
380 }
381 }
382 break;
383 default:
384 for (ch = 0; ch < channels; ch++)
385 {
386 for(i = 0; i < frame_len; i++)
387 {
388 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
389 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
390 }
391 }
392 break;
393 }
394}
395
396void *output_to_PCM(NeAACDecHandle hDecoder,
397 real_t **input, void *sample_buffer, uint8_t channels,
398 uint16_t frame_len, uint8_t format)
399{
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;
404
405#ifdef PROFILE
406 int64_t count = faad_get_ts();
407#endif
408
409 /* Copy output to a standard PCM buffer */
410 switch (format)
411 {
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;
427 }
428
429#ifdef PROFILE
430 count = faad_get_ts() - count;
431 hDecoder->output_cycles += count;
432#endif
433
434 return sample_buffer;
435}
436
437#else
438
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)
441
442static 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)
445{
446 if (up_matrix == 1)
447 return input[internal_channel[0]][sample];
448
449 if (!down_matrix)
450 return input[internal_channel[channel]][sample];
451
452 if (channel == 0)
453 {
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);
463 }
464}
465
466void* output_to_PCM(NeAACDecHandle hDecoder,
467 real_t **input, void *sample_buffer, uint8_t channels,
468 uint16_t frame_len, uint8_t format)
469{
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;
474
475 /* Copy output to a standard PCM buffer */
476 for (ch = 0; ch < channels; ch++)
477 {
478 switch (format)
479 {
480 case FAAD_FMT_16BIT:
481 for(i = 0; i < frame_len; i++)
482 {
483 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
484 hDecoder->internal_channel);
485 if (tmp >= 0)
486 {
487 tmp += (1 << (REAL_BITS-1));
488 if (tmp >= REAL_CONST(32767))
489 {
490 tmp = REAL_CONST(32767);
491 }
492 } else {
493 tmp += -(1 << (REAL_BITS-1));
494 if (tmp <= REAL_CONST(-32768))
495 {
496 tmp = REAL_CONST(-32768);
497 }
498 }
499 tmp >>= REAL_BITS;
500 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
501 }
502 break;
503 case FAAD_FMT_24BIT:
504 for(i = 0; i < frame_len; i++)
505 {
506 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
507 hDecoder->internal_channel);
508 if (tmp >= 0)
509 {
510 tmp += (1 << (REAL_BITS-9));
511 tmp >>= (REAL_BITS-8);
512 if (tmp >= 8388607)
513 {
514 tmp = 8388607;
515 }
516 } else {
517 tmp += -(1 << (REAL_BITS-9));
518 tmp >>= (REAL_BITS-8);
519 if (tmp <= -8388608)
520 {
521 tmp = -8388608;
522 }
523 }
524 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
525 }
526 break;
527 case FAAD_FMT_32BIT:
528 for(i = 0; i < frame_len; i++)
529 {
530 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
531 hDecoder->internal_channel);
532 if (tmp >= 0)
533 {
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);
539 }
540 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
541 }
542 break;
543 case FAAD_FMT_FIXED:
544 for(i = 0; i < frame_len; i++)
545 {
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;
549 }
550 break;
551 }
552 }
553
554 return sample_buffer;
555}
556
557#endif