summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/output.c (plain)
blob: dc7c37d3552bbe711543cefd2aa5f66ed9b9c089
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2005 M. Bakker, Nero 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** 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"
24**
25** Commercial non-GPL licensing of this software is possible.
26** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27**
28** $Id: output.c,v 1.47 2009/01/26 23:51:15 menno Exp $
29**/
30
31#include "common.h"
32#include "structs.h"
33
34#include "output.h"
35
36#ifndef FIXED_POINT
37
38
39#define FLOAT_SCALE (1.0f/(1<<15))
40
41#define DM_MUL 1//REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42#define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
43
44/*
45
46There are two types of down-mix to stereo:
471. Lt/Rt downmix:
48Lt = L + (0.707*C) - (0.707*Ls) - (0.707*Rs)
49Rt = R + (0.707*C) + (0.707*Ls) + (0.707*Rs)
502. Lo/Ro downmix:
51Lo = L + (0.707*C) + (0.707*Ls)
52Ro = R + (0.707*C) + (0.707*Rs)
53
54*/
55static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
56 uint8_t down_matrix, uint8_t *internal_channel)
57{
58 if (!down_matrix) {
59 return input[internal_channel[channel]][sample];
60 }
61
62 // for multi-channel( > 2ch ) downmix to 2ch case
63#if 0 //LoRo downmix mode
64 if (channel == 0) {
65 return DM_MUL * (input[internal_channel[1]][sample] +
66 input[internal_channel[0]][sample] * RSQRT2 +
67 input[internal_channel[3]][sample] * RSQRT2);
68 } else {
69 return DM_MUL * (input[internal_channel[2]][sample] +
70 input[internal_channel[0]][sample] * RSQRT2 +
71 input[internal_channel[4]][sample] * RSQRT2);
72 }
73#else //LtRt downmix mode
74 if (channel == 0) {
75 return DM_MUL * (input[internal_channel[1]][sample] +
76 input[internal_channel[0]][sample] * RSQRT2 -
77 input[internal_channel[3]][sample] * RSQRT2 - input[internal_channel[4]][sample] * RSQRT2);
78 } else {
79 return DM_MUL * (input[internal_channel[2]][sample] +
80 input[internal_channel[0]][sample] * RSQRT2 +
81 input[internal_channel[3]][sample] * RSQRT2 +
82 input[internal_channel[4]][sample] * RSQRT2);
83 }
84#endif
85}
86
87#ifndef HAS_LRINTF
88#define CLIP(sample, max, min) \
89if (sample >= 0.0f) \
90{ \
91 sample += 0.5f; \
92 if (sample >= max) \
93 sample = max; \
94} else { \
95 sample += -0.5f; \
96 if (sample <= min) \
97 sample = min; \
98}
99#else
100#define CLIP(sample, max, min) \
101if (sample >= 0.0f) \
102{ \
103 if (sample >= max) \
104 sample = max; \
105} else { \
106 if (sample <= min) \
107 sample = min; \
108}
109#endif
110
111#define CONV(a,b) ((a<<1)|(b&0x1))
112
113static void to_PCM_16bit(NeAACDecStruct *hDecoder, real_t **input,
114 uint8_t channels, uint16_t frame_len,
115 int16_t **sample_buffer)
116{
117 uint8_t ch, ch1;
118 uint16_t i;
119 uint16_t k;
120
121 switch (CONV(channels, hDecoder->downMatrix)) {
122 case CONV(1, 0):
123 case CONV(1, 1):
124 for (i = 0; i < frame_len; i++) {
125 real_t inp = input[hDecoder->internal_channel[0]][i];
126
127 CLIP(inp, 32767.0f, -32768.0f);
128
129 (*sample_buffer)[i] = (int16_t)lrintf(inp);
130 }
131 break;
132 case CONV(2, 0):
133 if (hDecoder->upMatrix) {
134 ch = hDecoder->internal_channel[0];
135 for (i = 0; i < frame_len; i++) {
136 real_t inp0 = input[ch][i];
137
138 CLIP(inp0, 32767.0f, -32768.0f);
139
140 (*sample_buffer)[(i * 2) + 0] = (int16_t)lrintf(inp0);
141 (*sample_buffer)[(i * 2) + 1] = (int16_t)lrintf(inp0);
142 }
143 } else {
144 ch = hDecoder->internal_channel[0];
145 ch1 = hDecoder->internal_channel[1];
146 for (i = 0; i < frame_len; i++) {
147 real_t inp0 = input[ch ][i];
148 real_t inp1 = input[ch1][i];
149
150 CLIP(inp0, 32767.0f, -32768.0f);
151 CLIP(inp1, 32767.0f, -32768.0f);
152
153 (*sample_buffer)[(i * 2) + 0] = (int16_t)lrintf(inp0);
154 (*sample_buffer)[(i * 2) + 1] = (int16_t)lrintf(inp1);
155 }
156 }
157 break;
158 default:
159#if 0
160 for (ch = 0; ch < channels; ch++) {
161 for (i = 0; i < frame_len; i++) {
162 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
163
164 CLIP(inp, 32767.0f, -32768.0f);
165
166 (*sample_buffer)[(i * channels) + ch] = (int16_t)lrintf(inp);
167 }
168 }
169#endif
170 if (channels == 6) {
171 for (ch = 0; ch < channels; ch += 6) {
172 for (i = 0; i < frame_len; i++) {
173 real_t inp[6];
174 real_t Lt, Rt;
175 for (k = 0; k < 6; k++) {
176 inp[k] = get_sample(input, ch + k, i, hDecoder->downMatrix, hDecoder->internal_channel);
177
178 CLIP(inp[k], 32767.0f, -32768.0f);
179 }
180
181 Lt = 1.0 * inp[1] + 0.707 * inp[0] - 0.707 * inp[3] - 0.707 * inp[4];
182 Rt = 1.0 * inp[2] + 0.707 * inp[0] + 0.707 * inp[3] - 0.707 * inp[4];
183
184 (*sample_buffer)[(i * channels) + ch] = (int16_t)lrintf(Lt);
185 (*sample_buffer)[(i * channels) + ch + 1] = (int16_t)lrintf(Rt);
186 }
187 }
188 } else if (4 == channels) {
189 for (ch = 0; ch < channels; ch++) {
190 if (ch % 2 != 0) {
191 continue;
192 }
193 for (i = 0; i < frame_len; i++) {
194 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
195
196 CLIP(inp, 32767.0f, -32768.0f);
197 if (ch > 0) {
198 ch = 1;
199 }
200
201 (*sample_buffer)[(i * channels) + ch] = (int16_t)lrintf(inp);
202 }
203 }
204 } else {
205 for (ch = 0; ch < channels; ch++) {
206 for (i = 0; i < frame_len; i++) {
207 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
208
209 CLIP(inp, 32767.0f, -32768.0f);
210
211 (*sample_buffer)[(i * channels) + ch] = (int16_t)lrintf(inp);
212 }
213 }
214 }
215 break;
216 }
217}
218
219static void to_PCM_24bit(NeAACDecStruct *hDecoder, real_t **input,
220 uint8_t channels, uint16_t frame_len,
221 int32_t **sample_buffer)
222{
223 uint8_t ch, ch1;
224 uint16_t i;
225
226 switch (CONV(channels, hDecoder->downMatrix)) {
227 case CONV(1, 0):
228 case CONV(1, 1):
229 for (i = 0; i < frame_len; i++) {
230 real_t inp = input[hDecoder->internal_channel[0]][i];
231
232 inp *= 256.0f;
233 CLIP(inp, 8388607.0f, -8388608.0f);
234
235 (*sample_buffer)[i] = (int32_t)lrintf(inp);
236 }
237 break;
238 case CONV(2, 0):
239 if (hDecoder->upMatrix) {
240 ch = hDecoder->internal_channel[0];
241 for (i = 0; i < frame_len; i++) {
242 real_t inp0 = input[ch][i];
243
244 inp0 *= 256.0f;
245 CLIP(inp0, 8388607.0f, -8388608.0f);
246
247 (*sample_buffer)[(i * 2) + 0] = (int32_t)lrintf(inp0);
248 (*sample_buffer)[(i * 2) + 1] = (int32_t)lrintf(inp0);
249 }
250 } else {
251 ch = hDecoder->internal_channel[0];
252 ch1 = hDecoder->internal_channel[1];
253 for (i = 0; i < frame_len; i++) {
254 real_t inp0 = input[ch ][i];
255 real_t inp1 = input[ch1][i];
256
257 inp0 *= 256.0f;
258 inp1 *= 256.0f;
259 CLIP(inp0, 8388607.0f, -8388608.0f);
260 CLIP(inp1, 8388607.0f, -8388608.0f);
261
262 (*sample_buffer)[(i * 2) + 0] = (int32_t)lrintf(inp0);
263 (*sample_buffer)[(i * 2) + 1] = (int32_t)lrintf(inp1);
264 }
265 }
266 break;
267 default:
268 for (ch = 0; ch < channels; ch++) {
269 for (i = 0; i < frame_len; i++) {
270 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
271
272 inp *= 256.0f;
273 CLIP(inp, 8388607.0f, -8388608.0f);
274
275 (*sample_buffer)[(i * channels) + ch] = (int32_t)lrintf(inp);
276 }
277 }
278 break;
279 }
280}
281
282static void to_PCM_32bit(NeAACDecStruct *hDecoder, real_t **input,
283 uint8_t channels, uint16_t frame_len,
284 int32_t **sample_buffer)
285{
286 uint8_t ch, ch1;
287 uint16_t i;
288
289 switch (CONV(channels, hDecoder->downMatrix)) {
290 case CONV(1, 0):
291 case CONV(1, 1):
292 for (i = 0; i < frame_len; i++) {
293 real_t inp = input[hDecoder->internal_channel[0]][i];
294
295 inp *= 65536.0f;
296 CLIP(inp, 2147483647.0f, -2147483648.0f);
297
298 (*sample_buffer)[i] = (int32_t)lrintf(inp);
299 }
300 break;
301 case CONV(2, 0):
302 if (hDecoder->upMatrix) {
303 ch = hDecoder->internal_channel[0];
304 for (i = 0; i < frame_len; i++) {
305 real_t inp0 = input[ch][i];
306
307 inp0 *= 65536.0f;
308 CLIP(inp0, 2147483647.0f, -2147483648.0f);
309
310 (*sample_buffer)[(i * 2) + 0] = (int32_t)lrintf(inp0);
311 (*sample_buffer)[(i * 2) + 1] = (int32_t)lrintf(inp0);
312 }
313 } else {
314 ch = hDecoder->internal_channel[0];
315 ch1 = hDecoder->internal_channel[1];
316 for (i = 0; i < frame_len; i++) {
317 real_t inp0 = input[ch ][i];
318 real_t inp1 = input[ch1][i];
319
320 inp0 *= 65536.0f;
321 inp1 *= 65536.0f;
322 CLIP(inp0, 2147483647.0f, -2147483648.0f);
323 CLIP(inp1, 2147483647.0f, -2147483648.0f);
324
325 (*sample_buffer)[(i * 2) + 0] = (int32_t)lrintf(inp0);
326 (*sample_buffer)[(i * 2) + 1] = (int32_t)lrintf(inp1);
327 }
328 }
329 break;
330 default:
331 for (ch = 0; ch < channels; ch++) {
332 for (i = 0; i < frame_len; i++) {
333 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
334
335 inp *= 65536.0f;
336 CLIP(inp, 2147483647.0f, -2147483648.0f);
337
338 (*sample_buffer)[(i * channels) + ch] = (int32_t)lrintf(inp);
339 }
340 }
341 break;
342 }
343}
344
345static void to_PCM_float(NeAACDecStruct *hDecoder, real_t **input,
346 uint8_t channels, uint16_t frame_len,
347 float32_t **sample_buffer)
348{
349 uint8_t ch, ch1;
350 uint16_t i;
351
352 switch (CONV(channels, hDecoder->downMatrix)) {
353 case CONV(1, 0):
354 case CONV(1, 1):
355 for (i = 0; i < frame_len; i++) {
356 real_t inp = input[hDecoder->internal_channel[0]][i];
357 (*sample_buffer)[i] = inp * FLOAT_SCALE;
358 }
359 break;
360 case CONV(2, 0):
361 if (hDecoder->upMatrix) {
362 ch = hDecoder->internal_channel[0];
363 for (i = 0; i < frame_len; i++) {
364 real_t inp0 = input[ch][i];
365 (*sample_buffer)[(i * 2) + 0] = inp0 * FLOAT_SCALE;
366 (*sample_buffer)[(i * 2) + 1] = inp0 * FLOAT_SCALE;
367 }
368 } else {
369 ch = hDecoder->internal_channel[0];
370 ch1 = hDecoder->internal_channel[1];
371 for (i = 0; i < frame_len; i++) {
372 real_t inp0 = input[ch ][i];
373 real_t inp1 = input[ch1][i];
374 (*sample_buffer)[(i * 2) + 0] = inp0 * FLOAT_SCALE;
375 (*sample_buffer)[(i * 2) + 1] = inp1 * FLOAT_SCALE;
376 }
377 }
378 break;
379 default:
380 for (ch = 0; ch < channels; ch++) {
381 for (i = 0; i < frame_len; i++) {
382 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
383 (*sample_buffer)[(i * channels) + ch] = inp * FLOAT_SCALE;
384 }
385 }
386 break;
387 }
388}
389
390static void to_PCM_double(NeAACDecStruct *hDecoder, real_t **input,
391 uint8_t channels, uint16_t frame_len,
392 double **sample_buffer)
393{
394 uint8_t ch, ch1;
395 uint16_t i;
396
397 switch (CONV(channels, hDecoder->downMatrix)) {
398 case CONV(1, 0):
399 case CONV(1, 1):
400 for (i = 0; i < frame_len; i++) {
401 real_t inp = input[hDecoder->internal_channel[0]][i];
402 (*sample_buffer)[i] = (double)inp * FLOAT_SCALE;
403 }
404 break;
405 case CONV(2, 0):
406 if (hDecoder->upMatrix) {
407 ch = hDecoder->internal_channel[0];
408 for (i = 0; i < frame_len; i++) {
409 real_t inp0 = input[ch][i];
410 (*sample_buffer)[(i * 2) + 0] = (double)inp0 * FLOAT_SCALE;
411 (*sample_buffer)[(i * 2) + 1] = (double)inp0 * FLOAT_SCALE;
412 }
413 } else {
414 ch = hDecoder->internal_channel[0];
415 ch1 = hDecoder->internal_channel[1];
416 for (i = 0; i < frame_len; i++) {
417 real_t inp0 = input[ch ][i];
418 real_t inp1 = input[ch1][i];
419 (*sample_buffer)[(i * 2) + 0] = (double)inp0 * FLOAT_SCALE;
420 (*sample_buffer)[(i * 2) + 1] = (double)inp1 * FLOAT_SCALE;
421 }
422 }
423 break;
424 default:
425 for (ch = 0; ch < channels; ch++) {
426 for (i = 0; i < frame_len; i++) {
427 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
428 (*sample_buffer)[(i * channels) + ch] = (double)inp * FLOAT_SCALE;
429 }
430 }
431 break;
432 }
433}
434
435void *output_to_PCM(NeAACDecStruct *hDecoder,
436 real_t **input, void *sample_buffer, uint8_t channels,
437 uint16_t frame_len, uint8_t format)
438{
439 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
440 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
441 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
442 double *double_sample_buffer = (double*)sample_buffer;
443
444#ifdef PROFILE
445 int64_t count = faad_get_ts();
446#endif
447
448 /* Copy output to a standard PCM buffer */
449 switch (format) {
450 case FAAD_FMT_16BIT:
451 to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
452 break;
453 case FAAD_FMT_24BIT:
454 to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
455 break;
456 case FAAD_FMT_32BIT:
457 to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
458 break;
459 case FAAD_FMT_FLOAT:
460 to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
461 break;
462 case FAAD_FMT_DOUBLE:
463 to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
464 break;
465 }
466
467#ifdef PROFILE
468 count = faad_get_ts() - count;
469 hDecoder->output_cycles += count;
470#endif
471
472 return sample_buffer;
473}
474
475#else
476
477#define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
478#define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
479
480static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
481 uint8_t down_matrix, uint8_t up_matrix,
482 uint8_t *internal_channel)
483{
484 if (up_matrix == 1) {
485 return input[internal_channel[0]][sample];
486 }
487
488 if (!down_matrix) {
489 return input[internal_channel[channel]][sample];
490 }
491
492 if (channel == 0) {
493 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
494 real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
495 real_t cum = input[internal_channel[1]][sample] + C + L_S;
496 return MUL_F(cum, DM_MUL);
497 } else {
498 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2);
499 real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
500 real_t cum = input[internal_channel[2]][sample] + C + R_S;
501 return MUL_F(cum, DM_MUL);
502 }
503}
504
505void* output_to_PCM(NeAACDecStruct *hDecoder,
506 real_t **input, void *sample_buffer, uint8_t channels,
507 uint16_t frame_len, uint8_t format)
508{
509 uint8_t ch;
510 uint16_t i;
511 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
512 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
513
514 /* Copy output to a standard PCM buffer */
515 for (ch = 0; ch < channels; ch++) {
516 switch (format) {
517 case FAAD_FMT_16BIT:
518 for (i = 0; i < frame_len; i++) {
519 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
520 hDecoder->internal_channel);
521 if (tmp >= 0) {
522 tmp += (1 << (REAL_BITS - 1));
523 if (tmp >= REAL_CONST(32767)) {
524 tmp = REAL_CONST(32767);
525 }
526 } else {
527 tmp += -(1 << (REAL_BITS - 1));
528 if (tmp <= REAL_CONST(-32768)) {
529 tmp = REAL_CONST(-32768);
530 }
531 }
532 tmp >>= REAL_BITS;
533 short_sample_buffer[(i * channels) + ch] = (int16_t)tmp;
534 }
535 break;
536 case FAAD_FMT_24BIT:
537 for (i = 0; i < frame_len; i++) {
538 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
539 hDecoder->internal_channel);
540 if (tmp >= 0) {
541 tmp += (1 << (REAL_BITS - 9));
542 tmp >>= (REAL_BITS - 8);
543 if (tmp >= 8388607) {
544 tmp = 8388607;
545 }
546 } else {
547 tmp += -(1 << (REAL_BITS - 9));
548 tmp >>= (REAL_BITS - 8);
549 if (tmp <= -8388608) {
550 tmp = -8388608;
551 }
552 }
553 int_sample_buffer[(i * channels) + ch] = (int32_t)tmp;
554 }
555 break;
556 case FAAD_FMT_32BIT:
557 for (i = 0; i < frame_len; i++) {
558 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559 hDecoder->internal_channel);
560 if (tmp >= 0) {
561 tmp += (1 << (16 - REAL_BITS - 1));
562 tmp <<= (16 - REAL_BITS);
563 } else {
564 tmp += -(1 << (16 - REAL_BITS - 1));
565 tmp <<= (16 - REAL_BITS);
566 }
567 int_sample_buffer[(i * channels) + ch] = (int32_t)tmp;
568 }
569 break;
570 case FAAD_FMT_FIXED:
571 for (i = 0; i < frame_len; i++) {
572 real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
573 hDecoder->internal_channel);
574 int_sample_buffer[(i * channels) + ch] = (int32_t)tmp;
575 }
576 break;
577 }
578 }
579
580 return sample_buffer;
581}
582
583#endif
584