comparison Plugins/Input/aac/libfaad2/output.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children 0a2ad94e8607
comparison
equal deleted inserted replaced
60:1771f253e1b2 61:fa848bd484d8
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: output.c,v 1.29 2003/11/12 20:47:58 menno Exp $
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_t)1.0/((real_t)1.0+(real_t)sqrt(2.0)))
40
41
42 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
43 uint8_t downMatrix, uint8_t *internal_channel)
44 {
45 if (!downMatrix)
46 return input[internal_channel[channel]][sample];
47
48 if (channel == 0)
49 {
50 return DM_MUL * (input[internal_channel[1]][sample] +
51 input[internal_channel[0]][sample]/(real_t)sqrt(2.) +
52 input[internal_channel[3]][sample]/(real_t)sqrt(2.));
53 } else {
54 return DM_MUL * (input[internal_channel[2]][sample] +
55 input[internal_channel[0]][sample]/(real_t)sqrt(2.) +
56 input[internal_channel[4]][sample]/(real_t)sqrt(2.));
57 }
58 }
59
60 void* output_to_PCM(faacDecHandle hDecoder,
61 real_t **input, void *sample_buffer, uint8_t channels,
62 uint16_t frame_len, uint8_t format)
63 {
64 uint8_t ch;
65 uint16_t i, j = 0;
66 uint8_t internal_channel;
67
68 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
69 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
70 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
71 double *double_sample_buffer = (double*)sample_buffer;
72
73 /* Copy output to a standard PCM buffer */
74 for (ch = 0; ch < channels; ch++)
75 {
76 internal_channel = hDecoder->internal_channel[ch];
77
78 switch (format)
79 {
80 case FAAD_FMT_16BIT:
81 for(i = 0; i < frame_len; i++)
82 {
83 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
84 if (inp >= 0.0f)
85 {
86 #ifndef HAS_LRINTF
87 inp += 0.5f;
88 #endif
89 if (inp >= 32768.0f)
90 {
91 inp = 32767.0f;
92 }
93 } else {
94 #ifndef HAS_LRINTF
95 inp += -0.5f;
96 #endif
97 if (inp <= -32769.0f)
98 {
99 inp = -32768.0f;
100 }
101 }
102 short_sample_buffer[(i*channels)+ch] = (int16_t)lrintf(inp);
103 }
104 break;
105 case FAAD_FMT_24BIT:
106 for(i = 0; i < frame_len; i++)
107 {
108 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
109 inp *= 256.0f;
110 if (inp >= 0.0f)
111 {
112 #ifndef HAS_LRINTF
113 inp += 0.5f;
114 #endif
115 if (inp >= 8388608.0f)
116 {
117 inp = 8388607.0f;
118 }
119 } else {
120 #ifndef HAS_LRINTF
121 inp += -0.5f;
122 #endif
123 if (inp <= -8388609.0f)
124 {
125 inp = -8388608.0f;
126 }
127 }
128 int_sample_buffer[(i*channels)+ch] = lrintf(inp);
129 }
130 break;
131 case FAAD_FMT_32BIT:
132 for(i = 0; i < frame_len; i++)
133 {
134 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
135 inp *= 65536.0f;
136 if (inp >= 0.0f)
137 {
138 #ifndef HAS_LRINTF
139 inp += 0.5f;
140 #endif
141 if (inp >= 2147483648.0f)
142 {
143 inp = 2147483647.0f;
144 }
145 } else {
146 #ifndef HAS_LRINTF
147 inp += -0.5f;
148 #endif
149 if (inp <= -2147483649.0f)
150 {
151 inp = -2147483648.0f;
152 }
153 }
154 int_sample_buffer[(i*channels)+ch] = lrintf(inp);
155 }
156 break;
157 case FAAD_FMT_FLOAT:
158 for(i = 0; i < frame_len; i++)
159 {
160 //real_t inp = input[internal_channel][i];
161 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
162 float_sample_buffer[(i*channels)+ch] = inp*FLOAT_SCALE;
163 }
164 break;
165 case FAAD_FMT_DOUBLE:
166 for(i = 0; i < frame_len; i++)
167 {
168 //real_t inp = input[internal_channel][i];
169 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
170 double_sample_buffer[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
171 }
172 break;
173 }
174 }
175
176 return sample_buffer;
177 }
178
179 #else
180
181 void* output_to_PCM(faacDecHandle hDecoder,
182 real_t **input, void *sample_buffer, uint8_t channels,
183 uint16_t frame_len, uint8_t format)
184 {
185 uint8_t ch;
186 uint16_t i;
187 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
188 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
189
190 /* Copy output to a standard PCM buffer */
191 for (ch = 0; ch < channels; ch++)
192 {
193 switch (format)
194 {
195 case FAAD_FMT_16BIT:
196 for(i = 0; i < frame_len; i++)
197 {
198 int32_t tmp = input[ch][i];
199 if (tmp >= 0)
200 {
201 tmp += (1 << (REAL_BITS-1));
202 if (tmp >= REAL_CONST(32768))
203 {
204 tmp = REAL_CONST(32767);
205 }
206 } else {
207 tmp += -(1 << (REAL_BITS-1));
208 if (tmp <= REAL_CONST(-32769))
209 {
210 tmp = REAL_CONST(-32768);
211 }
212 }
213 tmp >>= REAL_BITS;
214 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
215 }
216 break;
217 case FAAD_FMT_24BIT:
218 for(i = 0; i < frame_len; i++)
219 {
220 int32_t tmp = input[ch][i];
221 if (tmp >= 0)
222 {
223 tmp += (1 << (REAL_BITS-9));
224 tmp >>= (REAL_BITS-8);
225 if (tmp >= 8388608)
226 {
227 tmp = 8388607;
228 }
229 } else {
230 tmp += -(1 << (REAL_BITS-9));
231 tmp >>= (REAL_BITS-8);
232 if (tmp <= -8388609)
233 {
234 tmp = -8388608;
235 }
236 }
237 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
238 }
239 break;
240 case FAAD_FMT_32BIT:
241 for(i = 0; i < frame_len; i++)
242 {
243 int32_t tmp = input[ch][i];
244 if (tmp >= 0)
245 {
246 tmp += (1 << (16-REAL_BITS-1));
247 tmp <<= (16-REAL_BITS);
248 } else {
249 tmp += -(1 << (16-REAL_BITS-1));
250 tmp <<= (16-REAL_BITS);
251 }
252 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
253 }
254 break;
255 }
256 }
257
258 return sample_buffer;
259 }
260
261 #endif