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!
18 #include "libvo/fastmemcpy.h"
20 // Integer to float conversion through lrintf()
23 long int lrintf(float);
25 #define lrintf(x) ((int)(x))
28 /* Functions used by play to convert the input audio to the correct
31 /* The below includes retrives functions for converting to and from
33 #include "af_format_ulaw.c"
34 #include "af_format_alaw.c"
37 static void endian(void* in
, void* out
, int len
, int bps
);
38 // From singed to unsigned and the other way
39 static void si2us(void* data
, int len
, int bps
);
40 // Change the number of bits per sample
41 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
);
42 // From float to int signed
43 static void float2int(float* in
, void* out
, int len
, int bps
);
44 // From signed int to float
45 static void int2float(void* in
, float* out
, int len
, int bps
);
47 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
);
48 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
);
49 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
);
50 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
);
52 // Helper functions to check sanity for input arguments
54 // Sanity check for bytes per sample
55 static int check_bps(int bps
)
57 if(bps
!= 4 && bps
!= 3 && bps
!= 2 && bps
!= 1){
58 af_msg(AF_MSG_ERROR
,"[format] The number of bytes per sample"
59 " must be 1, 2, 3 or 4. Current value is %i \n",bps
);
65 // Check for unsupported formats
66 static int check_format(int format
)
69 switch(format
& AF_FORMAT_SPECIAL_MASK
){
70 case(AF_FORMAT_IMA_ADPCM
):
71 case(AF_FORMAT_MPEG2
):
73 af_msg(AF_MSG_ERROR
,"[format] Sample format %s not yet supported \n",
74 af_fmt2str(format
,buf
,256));
80 // Initialization and runtime control
81 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
84 case AF_CONTROL_REINIT
:{
87 af_data_t
*data
= arg
;
89 // Make sure this filter isn't redundant
90 if(af
->data
->format
== data
->format
&&
91 af
->data
->bps
== data
->bps
)
94 // Check for errors in configuraton
95 if((AF_OK
!= check_bps(data
->bps
)) ||
96 (AF_OK
!= check_format(data
->format
)) ||
97 (AF_OK
!= check_bps(af
->data
->bps
)) ||
98 (AF_OK
!= check_format(af
->data
->format
)))
101 af_msg(AF_MSG_VERBOSE
,"[format] Changing sample format from %s to %s\n",
102 af_fmt2str(data
->format
,buf1
,256),
103 af_fmt2str(af
->data
->format
,buf2
,256));
105 af
->data
->rate
= data
->rate
;
106 af
->data
->nch
= data
->nch
;
107 af
->mul
.n
= af
->data
->bps
;
108 af
->mul
.d
= data
->bps
;
109 af_frac_cancel(&af
->mul
);
111 af
->play
= play
; // set default
113 // look whether only endianess differences are there
114 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
115 (data
->format
& ~AF_FORMAT_END_MASK
))
117 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated endianess conversion only\n");
118 af
->play
= play_swapendian
;
120 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
121 (af
->data
->format
== AF_FORMAT_S16_NE
))
123 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
124 af_fmt2str(data
->format
,buf1
,256),
125 af_fmt2str(af
->data
->format
,buf2
,256));
126 af
->play
= play_float_s16
;
128 if ((data
->format
== AF_FORMAT_S16_NE
) &&
129 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
131 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
132 af_fmt2str(data
->format
,buf1
,256),
133 af_fmt2str(af
->data
->format
,buf2
,256));
134 af
->play
= play_s16_float
;
138 case AF_CONTROL_COMMAND_LINE
:{
139 int format
= af_str2fmt_short(arg
);
141 af_msg(AF_MSG_ERROR
, "[format] %s is not a valid format\n", (char *)arg
);
144 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
148 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
149 // Check for errors in configuraton
150 if(AF_OK
!= check_format(*(int*)arg
))
153 af
->data
->format
= *(int*)arg
;
154 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
163 static void uninit(struct af_instance_s
* af
)
170 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
172 af_data_t
* l
= af
->data
; // Local data
173 af_data_t
* c
= data
; // Current working data
174 int len
= c
->len
/c
->bps
; // Lenght in samples of current audio block
176 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
179 endian(c
->audio
,l
->audio
,len
,c
->bps
);
182 c
->format
= l
->format
;
187 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
189 af_data_t
* l
= af
->data
; // Local data
190 af_data_t
* c
= data
; // Current working data
191 int len
= c
->len
/4; // Lenght in samples of current audio block
193 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
196 float2int(c
->audio
, l
->audio
, len
, 2);
201 c
->format
= l
->format
;
206 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
208 af_data_t
* l
= af
->data
; // Local data
209 af_data_t
* c
= data
; // Current working data
210 int len
= c
->len
/2; // Lenght in samples of current audio block
212 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
215 int2float(c
->audio
, l
->audio
, len
, 2);
220 c
->format
= l
->format
;
225 // Filter data through filter
226 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
228 af_data_t
* l
= af
->data
; // Local data
229 af_data_t
* c
= data
; // Current working data
230 int len
= c
->len
/c
->bps
; // Lenght in samples of current audio block
232 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
235 // Change to cpu native endian format
236 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
237 endian(c
->audio
,c
->audio
,len
,c
->bps
);
240 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
241 from_ulaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
242 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
243 to_ulaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
244 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
245 si2us(l
->audio
,len
,l
->bps
);
246 } else if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
247 from_alaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
248 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
249 to_alaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
250 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
251 si2us(l
->audio
,len
,l
->bps
);
252 } else if((c
->format
& AF_FORMAT_POINT_MASK
) == AF_FORMAT_F
) {
253 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
254 case(AF_FORMAT_MU_LAW
):
255 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
257 case(AF_FORMAT_A_LAW
):
258 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
261 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
262 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
263 si2us(l
->audio
,len
,l
->bps
);
269 // Change signed/unsigned
270 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
271 si2us(c
->audio
,len
,c
->bps
);
273 // Convert to special formats
274 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
275 case(AF_FORMAT_MU_LAW
):
276 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
278 case(AF_FORMAT_A_LAW
):
279 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
282 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
285 // Change the number of bits
287 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
289 memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
294 // Switch from cpu native endian to the correct endianess
295 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
296 endian(l
->audio
,l
->audio
,len
,l
->bps
);
302 c
->format
= l
->format
;
306 // Allocate memory and set function pointers
307 static int open(af_instance_t
* af
){
313 af
->data
=calloc(1,sizeof(af_data_t
));
319 // Description of this filter
320 af_info_t af_info_format
= {
321 "Sample format conversion",
329 static inline uint32_t load24bit(void* data
, int pos
) {
331 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
332 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
333 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
335 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
336 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
337 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
341 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
343 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
344 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
345 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
347 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
348 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
349 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
353 // Function implementations used by play
354 static void endian(void* in
, void* out
, int len
, int bps
)
360 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
367 s
=((uint8_t*)in
)[3*i
];
368 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
370 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
371 ((uint8_t*)out
)[3*i
+2]=s
;
377 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
384 static void si2us(void* data
, int len
, int bps
)
386 register long i
= -(len
* bps
);
387 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
388 #if AF_FORMAT_NE == AF_FORMAT_LE
391 if (len
<= 0) return;
397 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
405 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
409 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
413 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
421 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
425 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
429 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
437 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
441 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
445 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
453 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
457 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
461 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
468 static void float2int(float* in
, void* out
, int len
, int bps
)
474 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
478 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
482 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
486 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
491 static void int2float(void* in
, float* out
, int len
, int bps
)
497 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
501 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
505 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
509 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];