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!
16 // Integer to float conversion through lrintf()
19 long int lrintf(float);
21 #define lrintf(x) ((int)(x))
25 #include "libavutil/common.h"
27 #include "libvo/fastmemcpy.h"
29 /* Functions used by play to convert the input audio to the correct
32 /* The below includes retrives functions for converting to and from
34 #include "af_format_ulaw.c"
35 #include "af_format_alaw.c"
38 static void endian(void* in
, void* out
, int len
, int bps
);
39 // From singed to unsigned and the other way
40 static void si2us(void* data
, int len
, int bps
);
41 // Change the number of bits per sample
42 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
);
43 // From float to int signed
44 static void float2int(float* in
, void* out
, int len
, int bps
);
45 // From signed int to float
46 static void int2float(void* in
, float* out
, int len
, int bps
);
48 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
);
49 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
);
50 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
);
51 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
);
53 // Helper functions to check sanity for input arguments
55 // Sanity check for bytes per sample
56 static int check_bps(int bps
)
58 if(bps
!= 4 && bps
!= 3 && bps
!= 2 && bps
!= 1){
59 af_msg(AF_MSG_ERROR
,"[format] The number of bytes per sample"
60 " must be 1, 2, 3 or 4. Current value is %i \n",bps
);
66 // Check for unsupported formats
67 static int check_format(int format
)
70 switch(format
& AF_FORMAT_SPECIAL_MASK
){
71 case(AF_FORMAT_IMA_ADPCM
):
72 case(AF_FORMAT_MPEG2
):
74 af_msg(AF_MSG_ERROR
,"[format] Sample format %s not yet supported \n",
75 af_fmt2str(format
,buf
,256));
81 // Initialization and runtime control
82 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
85 case AF_CONTROL_REINIT
:{
88 af_data_t
*data
= arg
;
90 // Make sure this filter isn't redundant
91 if(af
->data
->format
== data
->format
&&
92 af
->data
->bps
== data
->bps
)
95 // Check for errors in configuraton
96 if((AF_OK
!= check_bps(data
->bps
)) ||
97 (AF_OK
!= check_format(data
->format
)) ||
98 (AF_OK
!= check_bps(af
->data
->bps
)) ||
99 (AF_OK
!= check_format(af
->data
->format
)))
102 af_msg(AF_MSG_VERBOSE
,"[format] Changing sample format from %s to %s\n",
103 af_fmt2str(data
->format
,buf1
,256),
104 af_fmt2str(af
->data
->format
,buf2
,256));
106 af
->data
->rate
= data
->rate
;
107 af
->data
->nch
= data
->nch
;
108 af
->mul
.n
= af
->data
->bps
;
109 af
->mul
.d
= data
->bps
;
110 af_frac_cancel(&af
->mul
);
112 af
->play
= play
; // set default
114 // look whether only endianess differences are there
115 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
116 (data
->format
& ~AF_FORMAT_END_MASK
))
118 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated endianess conversion only\n");
119 af
->play
= play_swapendian
;
121 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
122 (af
->data
->format
== AF_FORMAT_S16_NE
))
124 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
125 af_fmt2str(data
->format
,buf1
,256),
126 af_fmt2str(af
->data
->format
,buf2
,256));
127 af
->play
= play_float_s16
;
129 if ((data
->format
== AF_FORMAT_S16_NE
) &&
130 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
132 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
133 af_fmt2str(data
->format
,buf1
,256),
134 af_fmt2str(af
->data
->format
,buf2
,256));
135 af
->play
= play_s16_float
;
139 case AF_CONTROL_COMMAND_LINE
:{
140 int format
= af_str2fmt_short(arg
);
142 af_msg(AF_MSG_ERROR
, "[format] %s is not a valid format\n", (char *)arg
);
145 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
149 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
150 // Check for errors in configuraton
151 if(AF_OK
!= check_format(*(int*)arg
))
154 af
->data
->format
= *(int*)arg
;
155 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
164 static void uninit(struct af_instance_s
* af
)
167 free(af
->data
->audio
);
172 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
174 af_data_t
* l
= af
->data
; // Local data
175 af_data_t
* c
= data
; // Current working data
176 int len
= c
->len
/c
->bps
; // Lenght in samples of current audio block
178 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
181 endian(c
->audio
,l
->audio
,len
,c
->bps
);
184 c
->format
= l
->format
;
189 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
191 af_data_t
* l
= af
->data
; // Local data
192 af_data_t
* c
= data
; // Current working data
193 int len
= c
->len
/4; // Lenght in samples of current audio block
195 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
198 float2int(c
->audio
, l
->audio
, len
, 2);
203 c
->format
= l
->format
;
208 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
210 af_data_t
* l
= af
->data
; // Local data
211 af_data_t
* c
= data
; // Current working data
212 int len
= c
->len
/2; // Lenght in samples of current audio block
214 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
217 int2float(c
->audio
, l
->audio
, len
, 2);
222 c
->format
= l
->format
;
227 // Filter data through filter
228 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
230 af_data_t
* l
= af
->data
; // Local data
231 af_data_t
* c
= data
; // Current working data
232 int len
= c
->len
/c
->bps
; // Lenght in samples of current audio block
234 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
237 // Change to cpu native endian format
238 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
239 endian(c
->audio
,c
->audio
,len
,c
->bps
);
242 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
243 from_ulaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
244 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
245 to_ulaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
246 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
247 si2us(l
->audio
,len
,l
->bps
);
248 } else if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
249 from_alaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
250 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
251 to_alaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
252 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
253 si2us(l
->audio
,len
,l
->bps
);
254 } else if((c
->format
& AF_FORMAT_POINT_MASK
) == AF_FORMAT_F
) {
255 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
256 case(AF_FORMAT_MU_LAW
):
257 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
259 case(AF_FORMAT_A_LAW
):
260 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
263 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
264 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
265 si2us(l
->audio
,len
,l
->bps
);
271 // Change signed/unsigned
272 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
273 si2us(c
->audio
,len
,c
->bps
);
275 // Convert to special formats
276 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
277 case(AF_FORMAT_MU_LAW
):
278 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
280 case(AF_FORMAT_A_LAW
):
281 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
284 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
287 // Change the number of bits
289 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
291 memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
296 // Switch from cpu native endian to the correct endianess
297 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
298 endian(l
->audio
,l
->audio
,len
,l
->bps
);
304 c
->format
= l
->format
;
308 // Allocate memory and set function pointers
309 static int open(af_instance_t
* af
){
315 af
->data
=calloc(1,sizeof(af_data_t
));
321 // Description of this filter
322 af_info_t af_info_format
= {
323 "Sample format conversion",
331 static inline uint32_t load24bit(void* data
, int pos
) {
333 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
334 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
335 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
337 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
338 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
339 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
343 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
345 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
346 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
347 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
349 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
350 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
351 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
355 // Function implementations used by play
356 static void endian(void* in
, void* out
, int len
, int bps
)
362 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
369 s
=((uint8_t*)in
)[3*i
];
370 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
372 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
373 ((uint8_t*)out
)[3*i
+2]=s
;
379 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
386 static void si2us(void* data
, int len
, int bps
)
388 register long i
= -(len
* bps
);
389 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
390 #if AF_FORMAT_NE == AF_FORMAT_LE
393 if (len
<= 0) return;
399 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
407 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
411 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
415 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
423 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
427 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
431 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
439 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
443 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
447 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
455 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
459 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
463 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
470 static void float2int(float* in
, void* out
, int len
, int bps
)
476 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
480 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
484 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
488 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
493 static void int2float(void* in
, float* out
, int len
, int bps
)
499 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
503 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
507 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
511 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];