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
3 AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE.
6 // Must be defined before any libc headers are included!
15 // Integer to float conversion through lrintf()
18 long int lrintf(float);
20 #define lrintf(x) ((int)(x))
25 #include "libvo/fastmemcpy.h"
27 /* Functions used by play to convert the input audio to the correct
30 /* The below includes retrieves functions for converting to and from
32 #include "af_format_ulaw.c"
33 #include "af_format_alaw.c"
36 static void endian(void* in
, void* out
, int len
, int bps
);
37 // From signed to unsigned and the other way
38 static void si2us(void* data
, int len
, int bps
);
39 // Change the number of bits per sample
40 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
);
41 // From float to int signed
42 static void float2int(float* in
, void* out
, int len
, int bps
);
43 // From signed int to float
44 static void int2float(void* in
, float* out
, int len
, int bps
);
46 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
);
47 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
);
48 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
);
49 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
);
51 // Helper functions to check sanity for input arguments
53 // Sanity check for bytes per sample
54 static int check_bps(int bps
)
56 if(bps
!= 4 && bps
!= 3 && bps
!= 2 && bps
!= 1){
57 af_msg(AF_MSG_ERROR
,"[format] The number of bytes per sample"
58 " must be 1, 2, 3 or 4. Current value is %i \n",bps
);
64 // Check for unsupported formats
65 static int check_format(int format
)
68 switch(format
& AF_FORMAT_SPECIAL_MASK
){
69 case(AF_FORMAT_IMA_ADPCM
):
70 case(AF_FORMAT_MPEG2
):
72 af_msg(AF_MSG_ERROR
,"[format] Sample format %s not yet supported \n",
73 af_fmt2str(format
,buf
,256));
79 // Initialization and runtime control
80 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
83 case AF_CONTROL_REINIT
:{
86 af_data_t
*data
= arg
;
88 // Make sure this filter isn't redundant
89 if(af
->data
->format
== data
->format
&&
90 af
->data
->bps
== data
->bps
)
93 // Check for errors in configuration
94 if((AF_OK
!= check_bps(data
->bps
)) ||
95 (AF_OK
!= check_format(data
->format
)) ||
96 (AF_OK
!= check_bps(af
->data
->bps
)) ||
97 (AF_OK
!= check_format(af
->data
->format
)))
100 af_msg(AF_MSG_VERBOSE
,"[format] Changing sample format from %s to %s\n",
101 af_fmt2str(data
->format
,buf1
,256),
102 af_fmt2str(af
->data
->format
,buf2
,256));
104 af
->data
->rate
= data
->rate
;
105 af
->data
->nch
= data
->nch
;
106 af
->mul
= (double)af
->data
->bps
/ data
->bps
;
108 af
->play
= play
; // set default
110 // look whether only endianness differences are there
111 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
112 (data
->format
& ~AF_FORMAT_END_MASK
))
114 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated endianness conversion only\n");
115 af
->play
= play_swapendian
;
117 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
118 (af
->data
->format
== AF_FORMAT_S16_NE
))
120 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
121 af_fmt2str(data
->format
,buf1
,256),
122 af_fmt2str(af
->data
->format
,buf2
,256));
123 af
->play
= play_float_s16
;
125 if ((data
->format
== AF_FORMAT_S16_NE
) &&
126 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
128 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
129 af_fmt2str(data
->format
,buf1
,256),
130 af_fmt2str(af
->data
->format
,buf2
,256));
131 af
->play
= play_s16_float
;
135 case AF_CONTROL_COMMAND_LINE
:{
136 int format
= af_str2fmt_short(arg
);
138 af_msg(AF_MSG_ERROR
, "[format] %s is not a valid format\n", (char *)arg
);
141 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
145 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
146 // Check for errors in configuration
147 if(AF_OK
!= check_format(*(int*)arg
))
150 af
->data
->format
= *(int*)arg
;
151 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
160 static void uninit(struct af_instance_s
* af
)
163 free(af
->data
->audio
);
168 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
170 af_data_t
* l
= af
->data
; // Local data
171 af_data_t
* c
= data
; // Current working data
172 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
174 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
177 endian(c
->audio
,l
->audio
,len
,c
->bps
);
180 c
->format
= l
->format
;
185 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
187 af_data_t
* l
= af
->data
; // Local data
188 af_data_t
* c
= data
; // Current working data
189 int len
= c
->len
/4; // Length in samples of current audio block
191 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
194 float2int(c
->audio
, l
->audio
, len
, 2);
199 c
->format
= l
->format
;
204 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
206 af_data_t
* l
= af
->data
; // Local data
207 af_data_t
* c
= data
; // Current working data
208 int len
= c
->len
/2; // Length in samples of current audio block
210 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
213 int2float(c
->audio
, l
->audio
, len
, 2);
218 c
->format
= l
->format
;
223 // Filter data through filter
224 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
226 af_data_t
* l
= af
->data
; // Local data
227 af_data_t
* c
= data
; // Current working data
228 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
230 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
233 // Change to cpu native endian format
234 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
235 endian(c
->audio
,c
->audio
,len
,c
->bps
);
238 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
239 from_ulaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
240 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
241 to_ulaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
242 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
243 si2us(l
->audio
,len
,l
->bps
);
244 } else if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
245 from_alaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
246 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
247 to_alaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
248 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
249 si2us(l
->audio
,len
,l
->bps
);
250 } else if((c
->format
& AF_FORMAT_POINT_MASK
) == AF_FORMAT_F
) {
251 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
252 case(AF_FORMAT_MU_LAW
):
253 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
255 case(AF_FORMAT_A_LAW
):
256 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
259 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
260 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
261 si2us(l
->audio
,len
,l
->bps
);
267 // Change signed/unsigned
268 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
269 si2us(c
->audio
,len
,c
->bps
);
271 // Convert to special formats
272 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
273 case(AF_FORMAT_MU_LAW
):
274 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
276 case(AF_FORMAT_A_LAW
):
277 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
280 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
283 // Change the number of bits
285 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
287 fast_memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
292 // Switch from cpu native endian to the correct endianness
293 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
294 endian(l
->audio
,l
->audio
,len
,l
->bps
);
300 c
->format
= l
->format
;
304 // Allocate memory and set function pointers
305 static int af_open(af_instance_t
* af
){
310 af
->data
=calloc(1,sizeof(af_data_t
));
316 // Description of this filter
317 af_info_t af_info_format
= {
318 "Sample format conversion",
326 static inline uint32_t load24bit(void* data
, int pos
) {
328 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
329 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
330 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
332 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
333 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
334 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
338 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
340 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
341 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
342 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
344 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
345 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
346 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
350 // Function implementations used by play
351 static void endian(void* in
, void* out
, int len
, int bps
)
357 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
364 s
=((uint8_t*)in
)[3*i
];
365 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
367 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
368 ((uint8_t*)out
)[3*i
+2]=s
;
374 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
381 static void si2us(void* data
, int len
, int bps
)
383 register long i
= -(len
* bps
);
384 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
385 #if AF_FORMAT_NE == AF_FORMAT_LE
388 if (len
<= 0) return;
394 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
402 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
406 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
410 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
418 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
422 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
426 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
434 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
438 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
442 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
450 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
454 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
458 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
465 static void float2int(float* in
, void* out
, int len
, int bps
)
471 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
475 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
479 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
483 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
488 static void int2float(void* in
, float* out
, int len
, int bps
)
494 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
498 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
502 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
506 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];