vobsub: Only try to merge packets if we have an earlier one stored
[mplayer/glamo.git] / libfaad2 / output.c
bloba8c41b894119418ccad2554aef3d2b517a68ea8b
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.
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
20 ** forbidden.
22 ** Initially modified for use with MPlayer by Rich Felker on 2005/03/29
23 ** $Id$
24 ** detailed changelog at http://svn.mplayerhq.hu/mplayer/trunk/
25 **/
27 #include "common.h"
28 #include "structs.h"
30 #include "output.h"
31 #include "decoder.h"
33 #ifndef FIXED_POINT
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)
45 if (!down_matrix)
46 return input[internal_channel[channel]][sample];
48 if (channel == 0)
50 return DM_MUL * (input[internal_channel[1]][sample] +
51 input[internal_channel[0]][sample] * RSQRT2 +
52 input[internal_channel[3]][sample] * RSQRT2);
53 } else {
54 return DM_MUL * (input[internal_channel[2]][sample] +
55 input[internal_channel[0]][sample] * RSQRT2 +
56 input[internal_channel[4]][sample] * RSQRT2);
60 #ifndef HAS_LRINTF
61 #define CLIP(sample, max, min) \
62 if (sample >= 0.0f) \
63 { \
64 sample += 0.5f; \
65 if (sample >= max) \
66 sample = max; \
67 } else { \
68 sample += -0.5f; \
69 if (sample <= min) \
70 sample = min; \
72 #else
73 #define CLIP(sample, max, min) \
74 if (sample >= 0.0f) \
75 { \
76 if (sample >= max) \
77 sample = max; \
78 } else { \
79 if (sample <= min) \
80 sample = min; \
82 #endif
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)
90 uint8_t ch, ch1;
91 uint16_t i;
93 switch (CONV(channels,hDecoder->downMatrix))
95 case CONV(1,0):
96 case CONV(1,1):
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);
105 break;
106 case CONV(2,0):
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);
119 } else {
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);
134 break;
135 default:
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);
147 break;
151 static void to_PCM_24bit(NeAACDecHandle hDecoder, real_t **input,
152 uint8_t channels, uint16_t frame_len,
153 int32_t **sample_buffer)
155 uint8_t ch, ch1;
156 uint16_t i;
158 switch (CONV(channels,hDecoder->downMatrix))
160 case CONV(1,0):
161 case CONV(1,1):
162 for(i = 0; i < frame_len; i++)
164 real_t inp = input[hDecoder->internal_channel[0]][i];
166 inp *= 256.0f;
167 CLIP(inp, 8388607.0f, -8388608.0f);
169 (*sample_buffer)[i] = (int32_t)lrintf(inp);
171 break;
172 case CONV(2,0):
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];
180 inp0 *= 256.0f;
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);
186 } else {
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];
194 inp0 *= 256.0f;
195 inp1 *= 256.0f;
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);
203 break;
204 default:
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);
211 inp *= 256.0f;
212 CLIP(inp, 8388607.0f, -8388608.0f);
214 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
217 break;
221 static void to_PCM_32bit(NeAACDecHandle hDecoder, real_t **input,
222 uint8_t channels, uint16_t frame_len,
223 int32_t **sample_buffer)
225 uint8_t ch, ch1;
226 uint16_t i;
228 switch (CONV(channels,hDecoder->downMatrix))
230 case CONV(1,0):
231 case CONV(1,1):
232 for(i = 0; i < frame_len; i++)
234 real_t inp = input[hDecoder->internal_channel[0]][i];
236 inp *= 65536.0f;
237 CLIP(inp, 2147483647.0f, -2147483648.0f);
239 (*sample_buffer)[i] = (int32_t)lrintf(inp);
241 break;
242 case CONV(2,0):
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];
250 inp0 *= 65536.0f;
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);
256 } else {
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];
264 inp0 *= 65536.0f;
265 inp1 *= 65536.0f;
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);
273 break;
274 default:
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);
281 inp *= 65536.0f;
282 CLIP(inp, 2147483647.0f, -2147483648.0f);
284 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
287 break;
291 static void to_PCM_float(NeAACDecHandle hDecoder, real_t **input,
292 uint8_t channels, uint16_t frame_len,
293 float32_t **sample_buffer)
295 uint8_t ch, ch1;
296 uint16_t i;
298 switch (CONV(channels,hDecoder->downMatrix))
300 case CONV(1,0):
301 case CONV(1,1):
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;
307 break;
308 case CONV(2,0):
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;
318 } else {
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;
329 break;
330 default:
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;
339 break;
343 static void to_PCM_double(NeAACDecHandle hDecoder, real_t **input,
344 uint8_t channels, uint16_t frame_len,
345 double **sample_buffer)
347 uint8_t ch, ch1;
348 uint16_t i;
350 switch (CONV(channels,hDecoder->downMatrix))
352 case CONV(1,0):
353 case CONV(1,1):
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;
359 break;
360 case CONV(2,0):
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;
370 } else {
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;
381 break;
382 default:
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;
391 break;
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;
404 #ifdef PROFILE
405 int64_t count = faad_get_ts();
406 #endif
408 /* Copy output to a standard PCM buffer */
409 switch (format)
411 case FAAD_FMT_16BIT:
412 to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
413 break;
414 case FAAD_FMT_24BIT:
415 to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
416 break;
417 case FAAD_FMT_32BIT:
418 to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419 break;
420 case FAAD_FMT_FLOAT:
421 to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
422 break;
423 case FAAD_FMT_DOUBLE:
424 to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
425 break;
428 #ifdef PROFILE
429 count = faad_get_ts() - count;
430 hDecoder->output_cycles += count;
431 #endif
433 return sample_buffer;
436 #else
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)
445 if (up_matrix == 1)
446 return input[internal_channel[0]][sample];
448 if (!down_matrix)
449 return input[internal_channel[channel]][sample];
451 if (channel == 0)
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);
457 } else {
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)
469 uint8_t ch;
470 uint16_t i;
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++)
477 switch (format)
479 case FAAD_FMT_16BIT:
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);
484 if (tmp >= 0)
486 tmp += (1 << (REAL_BITS-1));
487 if (tmp >= REAL_CONST(32767))
489 tmp = REAL_CONST(32767);
491 } else {
492 tmp += -(1 << (REAL_BITS-1));
493 if (tmp <= REAL_CONST(-32768))
495 tmp = REAL_CONST(-32768);
498 tmp >>= REAL_BITS;
499 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
501 break;
502 case FAAD_FMT_24BIT:
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);
507 if (tmp >= 0)
509 tmp += (1 << (REAL_BITS-9));
510 tmp >>= (REAL_BITS-8);
511 if (tmp >= 8388607)
513 tmp = 8388607;
515 } else {
516 tmp += -(1 << (REAL_BITS-9));
517 tmp >>= (REAL_BITS-8);
518 if (tmp <= -8388608)
520 tmp = -8388608;
523 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
525 break;
526 case FAAD_FMT_32BIT:
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);
531 if (tmp >= 0)
533 tmp += (1 << (16-REAL_BITS-1));
534 tmp <<= (16-REAL_BITS);
535 } else {
536 tmp += -(1 << (16-REAL_BITS-1));
537 tmp <<= (16-REAL_BITS);
539 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
541 break;
542 case FAAD_FMT_FIXED:
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;
549 break;
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)
560 int ch;
561 int i;
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++)
575 int32_t tmp;
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[hDecoder->internal_channel[ch]][i];
593 tmp += (1 << (REAL_BITS-1));
594 tmp >>= REAL_BITS;
595 if ((tmp+0x8000) & ~0xffff) tmp = ~(tmp>>31)-0x8000;
596 *(short_sample_buffer++) = tmp;
600 return sample_buffer;
603 #endif