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.
33 #include "libvo/fastmemcpy.h"
35 /* Functions used by play to convert the input audio to the correct
38 /* The below includes retrieves functions for converting to and from
40 #include "af_format_ulaw.h"
41 #include "af_format_alaw.h"
44 static void endian(void* in
, void* out
, int len
, int bps
);
45 // From signed to unsigned and the other way
46 static void si2us(void* data
, int len
, int bps
);
47 // Change the number of bits per sample
48 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
);
49 // From float to int signed
50 static void float2int(float* in
, void* out
, int len
, int bps
);
51 // From signed int to float
52 static void int2float(void* in
, float* out
, int len
, int bps
);
54 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
);
55 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
);
56 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
);
57 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
);
59 // Helper functions to check sanity for input arguments
61 // Sanity check for bytes per sample
62 static int check_bps(int bps
)
64 if(bps
!= 4 && bps
!= 3 && bps
!= 2 && bps
!= 1){
65 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "[format] The number of bytes per sample"
66 " must be 1, 2, 3 or 4. Current value is %i \n",bps
);
72 // Check for unsupported formats
73 static int check_format(int format
)
76 switch(format
& AF_FORMAT_SPECIAL_MASK
){
77 case(AF_FORMAT_IMA_ADPCM
):
78 case(AF_FORMAT_MPEG2
):
80 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "[format] Sample format %s not yet supported \n",
81 af_fmt2str(format
,buf
,256));
87 // Initialization and runtime control
88 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
91 case AF_CONTROL_REINIT
:{
94 af_data_t
*data
= arg
;
96 // Make sure this filter isn't redundant
97 if(af
->data
->format
== data
->format
&&
98 af
->data
->bps
== data
->bps
)
101 // Allow trivial AC3-endianness conversion
102 if (!AF_FORMAT_IS_AC3(af
->data
->format
) || !AF_FORMAT_IS_AC3(data
->format
))
103 // Check for errors in configuration
104 if((AF_OK
!= check_bps(data
->bps
)) ||
105 (AF_OK
!= check_format(data
->format
)) ||
106 (AF_OK
!= check_bps(af
->data
->bps
)) ||
107 (AF_OK
!= check_format(af
->data
->format
)))
110 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Changing sample format from %s to %s\n",
111 af_fmt2str(data
->format
,buf1
,256),
112 af_fmt2str(af
->data
->format
,buf2
,256));
114 af
->data
->rate
= data
->rate
;
115 af
->data
->nch
= data
->nch
;
116 af
->mul
= (double)af
->data
->bps
/ data
->bps
;
118 af
->play
= play
; // set default
120 // look whether only endianness differences are there
121 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
122 (data
->format
& ~AF_FORMAT_END_MASK
))
124 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Accelerated endianness conversion only\n");
125 af
->play
= play_swapendian
;
127 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
128 (af
->data
->format
== AF_FORMAT_S16_NE
))
130 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Accelerated %s to %s conversion\n",
131 af_fmt2str(data
->format
,buf1
,256),
132 af_fmt2str(af
->data
->format
,buf2
,256));
133 af
->play
= play_float_s16
;
135 if ((data
->format
== AF_FORMAT_S16_NE
) &&
136 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
138 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Accelerated %s to %s conversion\n",
139 af_fmt2str(data
->format
,buf1
,256),
140 af_fmt2str(af
->data
->format
,buf2
,256));
141 af
->play
= play_s16_float
;
145 case AF_CONTROL_COMMAND_LINE
:{
146 int format
= af_str2fmt_short(arg
);
148 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "[format] %s is not a valid format\n", (char *)arg
);
151 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
155 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
156 // Check for errors in configuration
157 if(!AF_FORMAT_IS_AC3(*(int*)arg
) && AF_OK
!= check_format(*(int*)arg
))
160 af
->data
->format
= *(int*)arg
;
161 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
170 static void uninit(struct af_instance_s
* af
)
173 free(af
->data
->audio
);
178 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
180 af_data_t
* l
= af
->data
; // Local data
181 af_data_t
* c
= data
; // Current working data
182 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
184 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
187 endian(c
->audio
,l
->audio
,len
,c
->bps
);
190 c
->format
= l
->format
;
195 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
197 af_data_t
* l
= af
->data
; // Local data
198 af_data_t
* c
= data
; // Current working data
199 int len
= c
->len
/4; // Length in samples of current audio block
201 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
204 float2int(c
->audio
, l
->audio
, len
, 2);
209 c
->format
= l
->format
;
214 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
216 af_data_t
* l
= af
->data
; // Local data
217 af_data_t
* c
= data
; // Current working data
218 int len
= c
->len
/2; // Length in samples of current audio block
220 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
223 int2float(c
->audio
, l
->audio
, len
, 2);
228 c
->format
= l
->format
;
233 // Filter data through filter
234 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
236 af_data_t
* l
= af
->data
; // Local data
237 af_data_t
* c
= data
; // Current working data
238 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
240 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
243 // Change to cpu native endian format
244 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
245 endian(c
->audio
,c
->audio
,len
,c
->bps
);
248 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
249 from_ulaw(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_ulaw(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_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
255 from_alaw(c
->audio
, l
->audio
, len
, l
->bps
, l
->format
&AF_FORMAT_POINT_MASK
);
256 if(AF_FORMAT_A_LAW
== (l
->format
&AF_FORMAT_SPECIAL_MASK
))
257 to_alaw(l
->audio
, l
->audio
, len
, 1, AF_FORMAT_SI
);
258 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
259 si2us(l
->audio
,len
,l
->bps
);
260 } else if((c
->format
& AF_FORMAT_POINT_MASK
) == AF_FORMAT_F
) {
261 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
262 case(AF_FORMAT_MU_LAW
):
263 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
265 case(AF_FORMAT_A_LAW
):
266 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
269 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
270 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
271 si2us(l
->audio
,len
,l
->bps
);
277 // Change signed/unsigned
278 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
279 si2us(c
->audio
,len
,c
->bps
);
281 // Convert to special formats
282 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
283 case(AF_FORMAT_MU_LAW
):
284 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
286 case(AF_FORMAT_A_LAW
):
287 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
290 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
293 // Change the number of bits
295 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
297 fast_memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
302 // Switch from cpu native endian to the correct endianness
303 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
304 endian(l
->audio
,l
->audio
,len
,l
->bps
);
310 c
->format
= l
->format
;
314 // Allocate memory and set function pointers
315 static int af_open(af_instance_t
* af
){
320 af
->data
=calloc(1,sizeof(af_data_t
));
326 // Description of this filter
327 af_info_t af_info_format
= {
328 "Sample format conversion",
336 static inline uint32_t load24bit(void* data
, int pos
) {
338 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
339 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
340 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
342 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
343 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
344 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
348 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
350 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
351 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
352 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
354 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
355 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
356 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
360 // Function implementations used by play
361 static void endian(void* in
, void* out
, int len
, int bps
)
367 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
374 s
=((uint8_t*)in
)[3*i
];
375 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
377 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
378 ((uint8_t*)out
)[3*i
+2]=s
;
384 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
391 static void si2us(void* data
, int len
, int bps
)
393 register long i
= -(len
* bps
);
394 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
395 #if AF_FORMAT_NE == AF_FORMAT_LE
398 if (len
<= 0) return;
404 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
412 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
416 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
420 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
428 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
432 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
436 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
444 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
448 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
452 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
460 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
464 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
468 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
475 static void float2int(float* in
, void* out
, int len
, int bps
)
481 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
485 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
489 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
493 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
498 static void int2float(void* in
, float* out
, int len
, int bps
)
504 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
508 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
512 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
516 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];