comparison libaf/af_format.c @ 14791:df515839c8a9

100l for me, lrintf is better. now fixed so it should be prototyped, and should work even if there is no prototype
author rfelker
date Thu, 24 Feb 2005 16:48:18 +0000
parents 94456deb0624
children b00b16a1ef05
comparison
equal deleted inserted replaced
14790:fa645382285f 14791:df515839c8a9
1 /* This audio output filter changes the format of a data block. Valid 1 /* This audio output filter changes the format of a data block. Valid
2 formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE 2 formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE
3 AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE. 3 AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE.
4 */ 4 */
5
6 // Must be defined before any libc headers are included!
7 #define _ISOC9X_SOURCE
5 8
6 #include <stdio.h> 9 #include <stdio.h>
7 #include <stdlib.h> 10 #include <stdlib.h>
8 #include <string.h> 11 #include <string.h>
9 #include <unistd.h> 12 #include <unistd.h>
11 #include <limits.h> 14 #include <limits.h>
12 15
13 #include "af.h" 16 #include "af.h"
14 #include "../bswap.h" 17 #include "../bswap.h"
15 #include "../libvo/fastmemcpy.h" 18 #include "../libvo/fastmemcpy.h"
19
20 // Integer to float conversion through lrintf()
21 #ifdef HAVE_LRINTF
22 #include <math.h>
23 long int lrintf(float);
24 #else
25 #define lrintf(x) ((int)(x))
26 #endif
16 27
17 /* Functions used by play to convert the input audio to the correct 28 /* Functions used by play to convert the input audio to the correct
18 format */ 29 format */
19 30
20 /* The below includes retrives functions for converting to and from 31 /* The below includes retrives functions for converting to and from
29 // From unsinged to signed 40 // From unsinged to signed
30 static void us2si(void* in, void* out, int len, int bps); 41 static void us2si(void* in, void* out, int len, int bps);
31 // Change the number of bits per sample 42 // Change the number of bits per sample
32 static void change_bps(void* in, void* out, int len, int inbps, int outbps); 43 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
33 // From float to int signed 44 // From float to int signed
34 static void float2int(void* in, void* out, int len, int bps); 45 static void float2int(float* in, void* out, int len, int bps);
35 // From signed int to float 46 // From signed int to float
36 static void int2float(void* in, void* out, int len, int bps); 47 static void int2float(void* in, float* out, int len, int bps);
37 48
38 static af_data_t* play(struct af_instance_s* af, af_data_t* data); 49 static af_data_t* play(struct af_instance_s* af, af_data_t* data);
39 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data); 50 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data);
40 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data); 51 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data);
41 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data); 52 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data);
486 } 497 }
487 break; 498 break;
488 } 499 }
489 } 500 }
490 501
491 static void float2int(void* in, void* out, int len, int bps) 502 static void float2int(float* in, void* out, int len, int bps)
492 { 503 {
493 register int i; 504 register int i;
494 switch(bps){ 505 switch(bps){
495 case(1): 506 case(1):
496 for(i=0;i<len;i++) 507 for(i=0;i<len;i++)
497 ((int8_t*)out)[i] = (int)(127.0 * (1.0+((float*)in)[i])) - 127; 508 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
498 break; 509 break;
499 case(2): 510 case(2):
500 for(i=0;i<len;i++) 511 for(i=0;i<len;i++)
501 ((int16_t*)out)[i] = (int)(32767.0 * (1.0+((float*)in)[i])) - 32767; 512 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
502 break; 513 break;
503 case(3): 514 case(3):
504 for(i=0;i<len;i++) 515 for(i=0;i<len;i++)
505 store24bit(out, i, (int)(2147483647.0 * (1.0+((float*)in)[i])) - 2147483647); 516 store24bit(out, i, lrintf(2147483647.0 * in[i]));
506 break; 517 break;
507 case(4): 518 case(4):
508 for(i=0;i<len;i++) 519 for(i=0;i<len;i++)
509 ((int32_t*)out)[i] = (int)(2147483647.0 * (1.0+((float*)in)[i])) - 2147483647; 520 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
510 break; 521 break;
511 } 522 }
512 } 523 }
513 524
514 static void int2float(void* in, void* out, int len, int bps) 525 static void int2float(void* in, float* out, int len, int bps)
515 { 526 {
516 register int i; 527 register int i;
517 switch(bps){ 528 switch(bps){
518 case(1): 529 case(1):
519 for(i=0;i<len;i++) 530 for(i=0;i<len;i++)
520 ((float*)out)[i]=(1.0/128.0)*((float)((int8_t*)in)[i]); 531 out[i]=(1.0/128.0)*((int8_t*)in)[i];
521 break; 532 break;
522 case(2): 533 case(2):
523 for(i=0;i<len;i++) 534 for(i=0;i<len;i++)
524 ((float*)out)[i]=(1.0/32768.0)*((float)((int16_t*)in)[i]); 535 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
525 break; 536 break;
526 case(3): 537 case(3):
527 for(i=0;i<len;i++) 538 for(i=0;i<len;i++)
528 ((float*)out)[i]=(1.0/2147483648.0)*((float)((int32_t)load24bit(in, i))); 539 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
529 break; 540 break;
530 case(4): 541 case(4):
531 for(i=0;i<len;i++) 542 for(i=0;i<len;i++)
532 ((float*)out)[i]=(1.0/2147483648.0)*((float)((int32_t*)in)[i]); 543 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
533 break; 544 break;
534 } 545 }
535 } 546 }