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