Mercurial > mplayer.hg
annotate libfaad2/output.c @ 13439:b35ec818ebeb
small fixes
author | diego |
---|---|
date | Wed, 22 Sep 2004 17:43:39 +0000 |
parents | d81145997036 |
children | 6d50ef45a058 |
rev | line source |
---|---|
10725 | 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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 |
10725 | 26 ** $Id$ |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10725 | 28 **/ |
29 | |
30 #include "common.h" | |
31 #include "structs.h" | |
32 | |
33 #include "output.h" | |
34 #include "decoder.h" | |
35 | |
36 #ifndef FIXED_POINT | |
37 | |
38 | |
39 #define FLOAT_SCALE (1.0f/(1<<15)) | |
40 | |
12527 | 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) | |
10725 | 43 |
44 | |
45 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample, | |
12527 | 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(faacDecHandle hDecoder, real_t **input, | |
90 uint8_t channels, uint16_t frame_len, | |
91 int16_t **sample_buffer) | |
10725 | 92 { |
12527 | 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 ch = hDecoder->internal_channel[0]; | |
111 ch1 = hDecoder->internal_channel[1]; | |
112 for(i = 0; i < frame_len; i++) | |
113 { | |
114 real_t inp0 = input[ch ][i]; | |
115 real_t inp1 = input[ch1][i]; | |
116 | |
117 CLIP(inp0, 32767.0f, -32768.0f); | |
118 CLIP(inp1, 32767.0f, -32768.0f); | |
119 | |
120 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0); | |
121 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1); | |
122 } | |
123 break; | |
124 default: | |
125 for (ch = 0; ch < channels; ch++) | |
126 { | |
127 for(i = 0; i < frame_len; i++) | |
128 { | |
129 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
130 | |
131 CLIP(inp, 32767.0f, -32768.0f); | |
132 | |
133 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp); | |
134 } | |
135 } | |
136 break; | |
137 } | |
138 } | |
139 | |
140 static void to_PCM_24bit(faacDecHandle hDecoder, real_t **input, | |
141 uint8_t channels, uint16_t frame_len, | |
142 int32_t **sample_buffer) | |
143 { | |
144 uint8_t ch, ch1; | |
145 uint16_t i; | |
146 | |
147 switch (CONV(channels,hDecoder->downMatrix)) | |
10725 | 148 { |
12527 | 149 case CONV(1,0): |
150 case CONV(1,1): | |
151 for(i = 0; i < frame_len; i++) | |
152 { | |
153 real_t inp = input[hDecoder->internal_channel[0]][i]; | |
154 | |
155 inp *= 256.0f; | |
156 CLIP(inp, 8388607.0f, -8388608.0f); | |
157 | |
158 (*sample_buffer)[i] = (int32_t)lrintf(inp); | |
159 } | |
160 break; | |
161 case CONV(2,0): | |
162 ch = hDecoder->internal_channel[0]; | |
163 ch1 = hDecoder->internal_channel[1]; | |
164 for(i = 0; i < frame_len; i++) | |
165 { | |
166 real_t inp0 = input[ch ][i]; | |
167 real_t inp1 = input[ch1][i]; | |
168 | |
169 inp0 *= 256.0f; | |
170 inp1 *= 256.0f; | |
171 CLIP(inp0, 8388607.0f, -8388608.0f); | |
172 CLIP(inp1, 8388607.0f, -8388608.0f); | |
173 | |
174 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0); | |
175 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1); | |
176 } | |
177 break; | |
178 default: | |
179 for (ch = 0; ch < channels; ch++) | |
180 { | |
181 for(i = 0; i < frame_len; i++) | |
182 { | |
183 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
184 | |
185 inp *= 256.0f; | |
186 CLIP(inp, 8388607.0f, -8388608.0f); | |
187 | |
188 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp); | |
189 } | |
190 } | |
191 break; | |
192 } | |
193 } | |
194 | |
195 static void to_PCM_32bit(faacDecHandle hDecoder, real_t **input, | |
196 uint8_t channels, uint16_t frame_len, | |
197 int32_t **sample_buffer) | |
198 { | |
199 uint8_t ch, ch1; | |
200 uint16_t i; | |
201 | |
202 switch (CONV(channels,hDecoder->downMatrix)) | |
203 { | |
204 case CONV(1,0): | |
205 case CONV(1,1): | |
206 for(i = 0; i < frame_len; i++) | |
207 { | |
208 real_t inp = input[hDecoder->internal_channel[0]][i]; | |
209 | |
210 inp *= 65536.0f; | |
211 CLIP(inp, 2147483647.0f, -2147483648.0f); | |
212 | |
213 (*sample_buffer)[i] = (int32_t)lrintf(inp); | |
214 } | |
215 break; | |
216 case CONV(2,0): | |
217 ch = hDecoder->internal_channel[0]; | |
218 ch1 = hDecoder->internal_channel[1]; | |
219 for(i = 0; i < frame_len; i++) | |
10725 | 220 { |
12527 | 221 real_t inp0 = input[ch ][i]; |
222 real_t inp1 = input[ch1][i]; | |
223 | |
224 inp0 *= 65536.0f; | |
225 inp1 *= 65536.0f; | |
226 CLIP(inp0, 2147483647.0f, -2147483648.0f); | |
227 CLIP(inp1, 2147483647.0f, -2147483648.0f); | |
228 | |
229 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0); | |
230 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1); | |
231 } | |
232 break; | |
233 default: | |
234 for (ch = 0; ch < channels; ch++) | |
235 { | |
236 for(i = 0; i < frame_len; i++) | |
237 { | |
238 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
239 | |
240 inp *= 65536.0f; | |
241 CLIP(inp, 2147483647.0f, -2147483648.0f); | |
242 | |
243 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp); | |
244 } | |
245 } | |
246 break; | |
247 } | |
248 } | |
249 | |
250 static void to_PCM_float(faacDecHandle hDecoder, real_t **input, | |
251 uint8_t channels, uint16_t frame_len, | |
252 float32_t **sample_buffer) | |
253 { | |
254 uint8_t ch, ch1; | |
255 uint16_t i; | |
256 | |
257 switch (CONV(channels,hDecoder->downMatrix)) | |
258 { | |
259 case CONV(1,0): | |
260 case CONV(1,1): | |
261 for(i = 0; i < frame_len; i++) | |
262 { | |
263 real_t inp = input[hDecoder->internal_channel[0]][i]; | |
264 (*sample_buffer)[i] = inp*FLOAT_SCALE; | |
265 } | |
266 break; | |
267 case CONV(2,0): | |
268 ch = hDecoder->internal_channel[0]; | |
269 ch1 = hDecoder->internal_channel[1]; | |
270 for(i = 0; i < frame_len; i++) | |
271 { | |
272 real_t inp0 = input[ch ][i]; | |
273 real_t inp1 = input[ch1][i]; | |
274 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE; | |
275 (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE; | |
276 } | |
277 break; | |
278 default: | |
279 for (ch = 0; ch < channels; ch++) | |
280 { | |
281 for(i = 0; i < frame_len; i++) | |
282 { | |
283 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
284 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE; | |
285 } | |
286 } | |
287 break; | |
288 } | |
289 } | |
290 | |
291 static void to_PCM_double(faacDecHandle hDecoder, real_t **input, | |
292 uint8_t channels, uint16_t frame_len, | |
293 double **sample_buffer) | |
294 { | |
295 uint8_t ch, ch1; | |
296 uint16_t i; | |
297 | |
298 switch (CONV(channels,hDecoder->downMatrix)) | |
299 { | |
300 case CONV(1,0): | |
301 case CONV(1,1): | |
302 for(i = 0; i < frame_len; i++) | |
303 { | |
304 real_t inp = input[hDecoder->internal_channel[0]][i]; | |
305 (*sample_buffer)[i] = (double)inp*FLOAT_SCALE; | |
10725 | 306 } |
12527 | 307 break; |
308 case CONV(2,0): | |
309 ch = hDecoder->internal_channel[0]; | |
310 ch1 = hDecoder->internal_channel[1]; | |
311 for(i = 0; i < frame_len; i++) | |
312 { | |
313 real_t inp0 = input[ch ][i]; | |
314 real_t inp1 = input[ch1][i]; | |
315 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE; | |
316 (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE; | |
317 } | |
318 break; | |
319 default: | |
320 for (ch = 0; ch < channels; ch++) | |
321 { | |
322 for(i = 0; i < frame_len; i++) | |
323 { | |
324 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
325 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE; | |
326 } | |
327 } | |
328 break; | |
329 } | |
330 } | |
331 | |
332 void *output_to_PCM(faacDecHandle hDecoder, | |
333 real_t **input, void *sample_buffer, uint8_t channels, | |
334 uint16_t frame_len, uint8_t format) | |
335 { | |
336 int16_t *short_sample_buffer = (int16_t*)sample_buffer; | |
337 int32_t *int_sample_buffer = (int32_t*)sample_buffer; | |
338 float32_t *float_sample_buffer = (float32_t*)sample_buffer; | |
339 double *double_sample_buffer = (double*)sample_buffer; | |
340 | |
341 #ifdef PROFILE | |
342 int64_t count = faad_get_ts(); | |
343 #endif | |
344 | |
345 /* Copy output to a standard PCM buffer */ | |
346 switch (format) | |
347 { | |
348 case FAAD_FMT_16BIT: | |
349 to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer); | |
350 break; | |
351 case FAAD_FMT_24BIT: | |
352 to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer); | |
353 break; | |
354 case FAAD_FMT_32BIT: | |
355 to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer); | |
356 break; | |
357 case FAAD_FMT_FLOAT: | |
358 to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer); | |
359 break; | |
360 case FAAD_FMT_DOUBLE: | |
361 to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer); | |
362 break; | |
363 } | |
364 | |
365 #ifdef PROFILE | |
366 count = faad_get_ts() - count; | |
367 hDecoder->output_cycles += count; | |
368 #endif | |
369 | |
370 return sample_buffer; | |
371 } | |
372 | |
373 #else | |
374 | |
375 #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2)) | |
376 #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2) | |
377 | |
378 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample, | |
379 uint8_t down_matrix, uint8_t *internal_channel) | |
380 { | |
381 if (!down_matrix) | |
382 return input[internal_channel[channel]][sample]; | |
383 | |
384 if (channel == 0) | |
385 { | |
386 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2); | |
387 real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2); | |
388 real_t cum = input[internal_channel[1]][sample] + C + L_S; | |
389 return MUL_F(cum, DM_MUL); | |
10725 | 390 } else { |
12527 | 391 real_t C = MUL_F(input[internal_channel[0]][sample], RSQRT2); |
392 real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2); | |
393 real_t cum = input[internal_channel[2]][sample] + C + R_S; | |
394 return MUL_F(cum, DM_MUL); | |
10725 | 395 } |
396 } | |
397 | |
398 void* output_to_PCM(faacDecHandle hDecoder, | |
399 real_t **input, void *sample_buffer, uint8_t channels, | |
400 uint16_t frame_len, uint8_t format) | |
401 { | |
402 uint8_t ch; | |
12527 | 403 uint16_t i; |
404 int16_t *short_sample_buffer = (int16_t*)sample_buffer; | |
405 int32_t *int_sample_buffer = (int32_t*)sample_buffer; | |
10725 | 406 |
407 /* Copy output to a standard PCM buffer */ | |
408 for (ch = 0; ch < channels; ch++) | |
409 { | |
410 switch (format) | |
411 { | |
412 case FAAD_FMT_16BIT: | |
413 for(i = 0; i < frame_len; i++) | |
414 { | |
12527 | 415 //int32_t tmp = input[ch][i]; |
416 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
417 if (tmp >= 0) | |
418 { | |
419 tmp += (1 << (REAL_BITS-1)); | |
420 if (tmp >= REAL_CONST(32767)) | |
421 { | |
422 tmp = REAL_CONST(32767); | |
423 } | |
424 } else { | |
425 tmp += -(1 << (REAL_BITS-1)); | |
426 if (tmp <= REAL_CONST(-32768)) | |
427 { | |
428 tmp = REAL_CONST(-32768); | |
429 } | |
430 } | |
431 tmp >>= REAL_BITS; | |
432 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp; | |
10725 | 433 } |
434 break; | |
435 case FAAD_FMT_24BIT: | |
436 for(i = 0; i < frame_len; i++) | |
437 { | |
12527 | 438 //int32_t tmp = input[ch][i]; |
439 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
440 if (tmp >= 0) | |
441 { | |
442 tmp += (1 << (REAL_BITS-9)); | |
443 tmp >>= (REAL_BITS-8); | |
444 if (tmp >= 8388607) | |
445 { | |
446 tmp = 8388607; | |
447 } | |
448 } else { | |
449 tmp += -(1 << (REAL_BITS-9)); | |
450 tmp >>= (REAL_BITS-8); | |
451 if (tmp <= -8388608) | |
452 { | |
453 tmp = -8388608; | |
454 } | |
455 } | |
456 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp; | |
10725 | 457 } |
458 break; | |
459 case FAAD_FMT_32BIT: | |
460 for(i = 0; i < frame_len; i++) | |
461 { | |
12527 | 462 //int32_t tmp = input[ch][i]; |
463 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel); | |
464 if (tmp >= 0) | |
465 { | |
466 tmp += (1 << (16-REAL_BITS-1)); | |
467 tmp <<= (16-REAL_BITS); | |
468 } else { | |
469 tmp += -(1 << (16-REAL_BITS-1)); | |
470 tmp <<= (16-REAL_BITS); | |
471 } | |
472 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp; | |
10725 | 473 } |
474 break; | |
475 } | |
476 } | |
477 | |
478 return sample_buffer; | |
479 } | |
480 | |
481 #endif |