2 * This audio filter changes the format of a data block. Valid
3 * formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE
4 * AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE.
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 // Integer to float conversion through lrintf()
32 long int lrintf(float);
34 #define lrintf(x) ((int)(x))
39 #include "libvo/fastmemcpy.h"
41 /* Functions used by play to convert the input audio to the correct
44 /* The below includes retrieves functions for converting to and from
46 #include "af_format_ulaw.h"
47 #include "af_format_alaw.h"
50 static void endian(void* in
, void* out
, int len
, int bps
);
51 // From signed to unsigned and the other way
52 static void si2us(void* data
, int len
, int bps
);
53 // Change the number of bits per sample
54 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
);
55 // From float to int signed
56 static void float2int(float* in
, void* out
, int len
, int bps
);
57 // From signed int to float
58 static void int2float(void* in
, float* out
, int len
, int bps
);
60 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
);
61 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
);
62 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
);
63 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
);
65 // Helper functions to check sanity for input arguments
67 // Sanity check for bytes per sample
68 static int check_bps(int bps
)
70 if(bps
!= 4 && bps
!= 3 && bps
!= 2 && bps
!= 1){
71 af_msg(AF_MSG_ERROR
,"[format] The number of bytes per sample"
72 " must be 1, 2, 3 or 4. Current value is %i \n",bps
);
78 // Check for unsupported formats
79 static int check_format(int format
)
82 switch(format
& AF_FORMAT_SPECIAL_MASK
){
83 case(AF_FORMAT_IMA_ADPCM
):
84 case(AF_FORMAT_MPEG2
):
86 af_msg(AF_MSG_ERROR
,"[format] Sample format %s not yet supported \n",
87 af_fmt2str(format
,buf
,256));
93 // Initialization and runtime control
94 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
97 case AF_CONTROL_REINIT
:{
100 af_data_t
*data
= arg
;
102 // Make sure this filter isn't redundant
103 if(af
->data
->format
== data
->format
&&
104 af
->data
->bps
== data
->bps
)
107 // Check for errors in configuration
108 if((AF_OK
!= check_bps(data
->bps
)) ||
109 (AF_OK
!= check_format(data
->format
)) ||
110 (AF_OK
!= check_bps(af
->data
->bps
)) ||
111 (AF_OK
!= check_format(af
->data
->format
)))
114 af_msg(AF_MSG_VERBOSE
,"[format] Changing sample format from %s to %s\n",
115 af_fmt2str(data
->format
,buf1
,256),
116 af_fmt2str(af
->data
->format
,buf2
,256));
118 af
->data
->rate
= data
->rate
;
119 af
->data
->nch
= data
->nch
;
120 af
->mul
= (double)af
->data
->bps
/ data
->bps
;
122 af
->play
= play
; // set default
124 // look whether only endianness differences are there
125 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
126 (data
->format
& ~AF_FORMAT_END_MASK
))
128 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated endianness conversion only\n");
129 af
->play
= play_swapendian
;
131 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
132 (af
->data
->format
== AF_FORMAT_S16_NE
))
134 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
135 af_fmt2str(data
->format
,buf1
,256),
136 af_fmt2str(af
->data
->format
,buf2
,256));
137 af
->play
= play_float_s16
;
139 if ((data
->format
== AF_FORMAT_S16_NE
) &&
140 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
142 af_msg(AF_MSG_VERBOSE
,"[format] Accelerated %s to %s conversion\n",
143 af_fmt2str(data
->format
,buf1
,256),
144 af_fmt2str(af
->data
->format
,buf2
,256));
145 af
->play
= play_s16_float
;
149 case AF_CONTROL_COMMAND_LINE
:{
150 int format
= af_str2fmt_short(arg
);
152 af_msg(AF_MSG_ERROR
, "[format] %s is not a valid format\n", (char *)arg
);
155 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
159 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
160 // Check for errors in configuration
161 if(AF_OK
!= check_format(*(int*)arg
))
164 af
->data
->format
= *(int*)arg
;
165 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
174 static void uninit(struct af_instance_s
* af
)
177 free(af
->data
->audio
);
182 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
184 af_data_t
* l
= af
->data
; // Local data
185 af_data_t
* c
= data
; // Current working data
186 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
188 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
191 endian(c
->audio
,l
->audio
,len
,c
->bps
);
194 c
->format
= l
->format
;
199 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
201 af_data_t
* l
= af
->data
; // Local data
202 af_data_t
* c
= data
; // Current working data
203 int len
= c
->len
/4; // Length in samples of current audio block
205 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
208 float2int(c
->audio
, l
->audio
, len
, 2);
213 c
->format
= l
->format
;
218 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
220 af_data_t
* l
= af
->data
; // Local data
221 af_data_t
* c
= data
; // Current working data
222 int len
= c
->len
/2; // Length in samples of current audio block
224 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
227 int2float(c
->audio
, l
->audio
, len
, 2);
232 c
->format
= l
->format
;
237 // Filter data through filter
238 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
240 af_data_t
* l
= af
->data
; // Local data
241 af_data_t
* c
= data
; // Current working data
242 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
244 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
247 // Change to cpu native endian format
248 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
249 endian(c
->audio
,c
->audio
,len
,c
->bps
);
252 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
253 from_ulaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
254 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
255 to_ulaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
256 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
257 si2us(l
->audio
,len
,l
->bps
);
258 } else if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
259 from_alaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
260 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
261 to_alaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
262 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
263 si2us(l
->audio
,len
,l
->bps
);
264 } else if((c
->format
& AF_FORMAT_POINT_MASK
) == AF_FORMAT_F
) {
265 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
266 case(AF_FORMAT_MU_LAW
):
267 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
269 case(AF_FORMAT_A_LAW
):
270 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
273 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
274 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
275 si2us(l
->audio
,len
,l
->bps
);
281 // Change signed/unsigned
282 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
283 si2us(c
->audio
,len
,c
->bps
);
285 // Convert to special formats
286 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
287 case(AF_FORMAT_MU_LAW
):
288 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
290 case(AF_FORMAT_A_LAW
):
291 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
294 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
297 // Change the number of bits
299 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
301 fast_memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
306 // Switch from cpu native endian to the correct endianness
307 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
308 endian(l
->audio
,l
->audio
,len
,l
->bps
);
314 c
->format
= l
->format
;
318 // Allocate memory and set function pointers
319 static int af_open(af_instance_t
* af
){
324 af
->data
=calloc(1,sizeof(af_data_t
));
330 // Description of this filter
331 af_info_t af_info_format
= {
332 "Sample format conversion",
340 static inline uint32_t load24bit(void* data
, int pos
) {
342 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
343 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
344 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
346 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
347 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
348 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
352 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
354 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
355 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
356 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
358 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
359 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
360 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
364 // Function implementations used by play
365 static void endian(void* in
, void* out
, int len
, int bps
)
371 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
378 s
=((uint8_t*)in
)[3*i
];
379 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
381 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
382 ((uint8_t*)out
)[3*i
+2]=s
;
388 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
395 static void si2us(void* data
, int len
, int bps
)
397 register long i
= -(len
* bps
);
398 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
399 #if AF_FORMAT_NE == AF_FORMAT_LE
402 if (len
<= 0) return;
408 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
416 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
420 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
424 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
432 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
436 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
440 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
448 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
452 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
456 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
464 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
468 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
472 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
479 static void float2int(float* in
, void* out
, int len
, int bps
)
485 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
489 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
493 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
497 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
502 static void int2float(void* in
, float* out
, int len
, int bps
)
508 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
512 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
516 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
520 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];