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))
24 #include "libavutil/common.h"
26 #include "libvo/fastmemcpy.h"
28 /* Functions used by play to convert the input audio to the correct
31 /* The below includes retrieves 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 signed 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 configuration
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
= (double)af
->data
->bps
/ data
->bps
;
109 af
->play
= play
; // set default
111 // look whether only endianness differences are there
112 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
113 (data
->format
& ~AF_FORMAT_END_MASK
))
115 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated endianness conversion only\n");
116 af
->play
= play_swapendian
;
118 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
119 (af
->data
->format
== AF_FORMAT_S16_NE
))
121 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
122 af_fmt2str(data
->format
,buf1
,256),
123 af_fmt2str(af
->data
->format
,buf2
,256));
124 af
->play
= play_float_s16
;
126 if ((data
->format
== AF_FORMAT_S16_NE
) &&
127 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
129 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
130 af_fmt2str(data
->format
,buf1
,256),
131 af_fmt2str(af
->data
->format
,buf2
,256));
132 af
->play
= play_s16_float
;
136 case AF_CONTROL_COMMAND_LINE
:{
137 int format
= af_str2fmt_short(arg
);
139 af_msg(AF_MSG_ERROR
, "[format] %s is not a valid format\n", (char *)arg
);
142 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
146 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
147 // Check for errors in configuration
148 if(AF_OK
!= check_format(*(int*)arg
))
151 af
->data
->format
= *(int*)arg
;
152 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
161 static void uninit(struct af_instance_s
* af
)
164 free(af
->data
->audio
);
169 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
171 af_data_t
* l
= af
->data
; // Local data
172 af_data_t
* c
= data
; // Current working data
173 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
175 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
178 endian(c
->audio
,l
->audio
,len
,c
->bps
);
181 c
->format
= l
->format
;
186 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
188 af_data_t
* l
= af
->data
; // Local data
189 af_data_t
* c
= data
; // Current working data
190 int len
= c
->len
/4; // Length in samples of current audio block
192 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
195 float2int(c
->audio
, l
->audio
, len
, 2);
200 c
->format
= l
->format
;
205 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
207 af_data_t
* l
= af
->data
; // Local data
208 af_data_t
* c
= data
; // Current working data
209 int len
= c
->len
/2; // Length in samples of current audio block
211 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
214 int2float(c
->audio
, l
->audio
, len
, 2);
219 c
->format
= l
->format
;
224 // Filter data through filter
225 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
227 af_data_t
* l
= af
->data
; // Local data
228 af_data_t
* c
= data
; // Current working data
229 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
231 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
234 // Change to cpu native endian format
235 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
236 endian(c
->audio
,c
->audio
,len
,c
->bps
);
239 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
240 from_ulaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
241 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
242 to_ulaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
243 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
244 si2us(l
->audio
,len
,l
->bps
);
245 } else if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
246 from_alaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
247 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
248 to_alaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
249 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
250 si2us(l
->audio
,len
,l
->bps
);
251 } else if((c
->format
& AF_FORMAT_POINT_MASK
) == AF_FORMAT_F
) {
252 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
253 case(AF_FORMAT_MU_LAW
):
254 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
256 case(AF_FORMAT_A_LAW
):
257 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
260 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
261 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
262 si2us(l
->audio
,len
,l
->bps
);
268 // Change signed/unsigned
269 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
270 si2us(c
->audio
,len
,c
->bps
);
272 // Convert to special formats
273 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
274 case(AF_FORMAT_MU_LAW
):
275 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
277 case(AF_FORMAT_A_LAW
):
278 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
281 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
284 // Change the number of bits
286 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
288 fast_memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
293 // Switch from cpu native endian to the correct endianness
294 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
295 endian(l
->audio
,l
->audio
,len
,l
->bps
);
301 c
->format
= l
->format
;
305 // Allocate memory and set function pointers
306 static int af_open(af_instance_t
* af
){
311 af
->data
=calloc(1,sizeof(af_data_t
));
317 // Description of this filter
318 af_info_t af_info_format
= {
319 "Sample format conversion",
327 static inline uint32_t load24bit(void* data
, int pos
) {
329 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
330 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
331 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
333 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
334 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
335 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
339 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
341 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
342 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
343 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
345 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
346 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
347 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
351 // Function implementations used by play
352 static void endian(void* in
, void* out
, int len
, int bps
)
358 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
365 s
=((uint8_t*)in
)[3*i
];
366 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
368 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
369 ((uint8_t*)out
)[3*i
+2]=s
;
375 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
382 static void si2us(void* data
, int len
, int bps
)
384 register long i
= -(len
* bps
);
385 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
386 #if AF_FORMAT_NE == AF_FORMAT_LE
389 if (len
<= 0) return;
395 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
403 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
407 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
411 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
419 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
423 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
427 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
435 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
439 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
443 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
451 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
455 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
459 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
466 static void float2int(float* in
, void* out
, int len
, int bps
)
472 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
476 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
480 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
484 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
489 static void int2float(void* in
, float* out
, int len
, int bps
)
495 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
499 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
503 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
507 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];