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 // Check for errors in configuration
102 if((AF_OK
!= check_bps(data
->bps
)) ||
103 (AF_OK
!= check_format(data
->format
)) ||
104 (AF_OK
!= check_bps(af
->data
->bps
)) ||
105 (AF_OK
!= check_format(af
->data
->format
)))
108 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Changing sample format from %s to %s\n",
109 af_fmt2str(data
->format
,buf1
,256),
110 af_fmt2str(af
->data
->format
,buf2
,256));
112 af
->data
->rate
= data
->rate
;
113 af
->data
->nch
= data
->nch
;
114 af
->mul
= (double)af
->data
->bps
/ data
->bps
;
116 af
->play
= play
; // set default
118 // look whether only endianness differences are there
119 if ((af
->data
->format
& ~AF_FORMAT_END_MASK
) ==
120 (data
->format
& ~AF_FORMAT_END_MASK
))
122 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Accelerated endianness conversion only\n");
123 af
->play
= play_swapendian
;
125 if ((data
->format
== AF_FORMAT_FLOAT_NE
) &&
126 (af
->data
->format
== AF_FORMAT_S16_NE
))
128 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Accelerated %s to %s conversion\n",
129 af_fmt2str(data
->format
,buf1
,256),
130 af_fmt2str(af
->data
->format
,buf2
,256));
131 af
->play
= play_float_s16
;
133 if ((data
->format
== AF_FORMAT_S16_NE
) &&
134 (af
->data
->format
== AF_FORMAT_FLOAT_NE
))
136 mp_msg(MSGT_AFILTER
, MSGL_V
, "[format] Accelerated %s to %s conversion\n",
137 af_fmt2str(data
->format
,buf1
,256),
138 af_fmt2str(af
->data
->format
,buf2
,256));
139 af
->play
= play_s16_float
;
143 case AF_CONTROL_COMMAND_LINE
:{
144 int format
= af_str2fmt_short(arg
);
146 mp_msg(MSGT_AFILTER
, MSGL_ERR
, "[format] %s is not a valid format\n", (char *)arg
);
149 if(AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
,&format
))
153 case AF_CONTROL_FORMAT_FMT
| AF_CONTROL_SET
:{
154 // Check for errors in configuration
155 if(AF_OK
!= check_format(*(int*)arg
))
158 af
->data
->format
= *(int*)arg
;
159 af
->data
->bps
= af_fmt2bits(af
->data
->format
)/8;
168 static void uninit(struct af_instance_s
* af
)
171 free(af
->data
->audio
);
176 static af_data_t
* play_swapendian(struct af_instance_s
* af
, af_data_t
* data
)
178 af_data_t
* l
= af
->data
; // Local data
179 af_data_t
* c
= data
; // Current working data
180 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
182 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
185 endian(c
->audio
,l
->audio
,len
,c
->bps
);
188 c
->format
= l
->format
;
193 static af_data_t
* play_float_s16(struct af_instance_s
* af
, af_data_t
* data
)
195 af_data_t
* l
= af
->data
; // Local data
196 af_data_t
* c
= data
; // Current working data
197 int len
= c
->len
/4; // Length in samples of current audio block
199 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
202 float2int(c
->audio
, l
->audio
, len
, 2);
207 c
->format
= l
->format
;
212 static af_data_t
* play_s16_float(struct af_instance_s
* af
, af_data_t
* data
)
214 af_data_t
* l
= af
->data
; // Local data
215 af_data_t
* c
= data
; // Current working data
216 int len
= c
->len
/2; // Length in samples of current audio block
218 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
221 int2float(c
->audio
, l
->audio
, len
, 2);
226 c
->format
= l
->format
;
231 // Filter data through filter
232 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
234 af_data_t
* l
= af
->data
; // Local data
235 af_data_t
* c
= data
; // Current working data
236 int len
= c
->len
/c
->bps
; // Length in samples of current audio block
238 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
241 // Change to cpu native endian format
242 if((c
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
243 endian(c
->audio
,c
->audio
,len
,c
->bps
);
246 if((c
->format
& AF_FORMAT_SPECIAL_MASK
) == AF_FORMAT_MU_LAW
) {
247 from_ulaw(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_ulaw(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_SPECIAL_MASK
) == AF_FORMAT_A_LAW
) {
253 from_alaw(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_alaw(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_POINT_MASK
) == AF_FORMAT_F
) {
259 switch(l
->format
&AF_FORMAT_SPECIAL_MASK
){
260 case(AF_FORMAT_MU_LAW
):
261 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
263 case(AF_FORMAT_A_LAW
):
264 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
267 float2int(c
->audio
, l
->audio
, len
, l
->bps
);
268 if((l
->format
&AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
269 si2us(l
->audio
,len
,l
->bps
);
275 // Change signed/unsigned
276 if((c
->format
&AF_FORMAT_SIGN_MASK
) != (l
->format
&AF_FORMAT_SIGN_MASK
)){
277 si2us(c
->audio
,len
,c
->bps
);
279 // Convert to special formats
280 switch(l
->format
&(AF_FORMAT_SPECIAL_MASK
|AF_FORMAT_POINT_MASK
)){
281 case(AF_FORMAT_MU_LAW
):
282 to_ulaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
284 case(AF_FORMAT_A_LAW
):
285 to_alaw(c
->audio
, l
->audio
, len
, c
->bps
, c
->format
&AF_FORMAT_POINT_MASK
);
288 int2float(c
->audio
, l
->audio
, len
, c
->bps
);
291 // Change the number of bits
293 change_bps(c
->audio
,l
->audio
,len
,c
->bps
,l
->bps
);
295 fast_memcpy(l
->audio
,c
->audio
,len
*c
->bps
);
300 // Switch from cpu native endian to the correct endianness
301 if((l
->format
&AF_FORMAT_END_MASK
)!=AF_FORMAT_NE
)
302 endian(l
->audio
,l
->audio
,len
,l
->bps
);
308 c
->format
= l
->format
;
312 // Allocate memory and set function pointers
313 static int af_open(af_instance_t
* af
){
318 af
->data
=calloc(1,sizeof(af_data_t
));
324 // Description of this filter
325 af_info_t af_info_format
= {
326 "Sample format conversion",
334 static inline uint32_t load24bit(void* data
, int pos
) {
336 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<24) |
337 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
338 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<8);
340 return (((uint32_t)((uint8_t*)data
)[3*pos
])<<8) |
341 (((uint32_t)((uint8_t*)data
)[3*pos
+1])<<16) |
342 (((uint32_t)((uint8_t*)data
)[3*pos
+2])<<24);
346 static inline void store24bit(void* data
, int pos
, uint32_t expanded_value
) {
348 ((uint8_t*)data
)[3*pos
]=expanded_value
>>24;
349 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
350 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>8;
352 ((uint8_t*)data
)[3*pos
]=expanded_value
>>8;
353 ((uint8_t*)data
)[3*pos
+1]=expanded_value
>>16;
354 ((uint8_t*)data
)[3*pos
+2]=expanded_value
>>24;
358 // Function implementations used by play
359 static void endian(void* in
, void* out
, int len
, int bps
)
365 ((uint16_t*)out
)[i
]=bswap_16(((uint16_t*)in
)[i
]);
372 s
=((uint8_t*)in
)[3*i
];
373 ((uint8_t*)out
)[3*i
]=((uint8_t*)in
)[3*i
+2];
375 ((uint8_t*)out
)[3*i
+1]=((uint8_t*)in
)[3*i
+1];
376 ((uint8_t*)out
)[3*i
+2]=s
;
382 ((uint32_t*)out
)[i
]=bswap_32(((uint32_t*)in
)[i
]);
389 static void si2us(void* data
, int len
, int bps
)
391 register long i
= -(len
* bps
);
392 register uint8_t *p
= &((uint8_t *)data
)[len
* bps
];
393 #if AF_FORMAT_NE == AF_FORMAT_LE
396 if (len
<= 0) return;
402 static void change_bps(void* in
, void* out
, int len
, int inbps
, int outbps
)
410 ((uint16_t*)out
)[i
]=((uint16_t)((uint8_t*)in
)[i
])<<8;
414 store24bit(out
, i
, ((uint32_t)((uint8_t*)in
)[i
])<<24);
418 ((uint32_t*)out
)[i
]=((uint32_t)((uint8_t*)in
)[i
])<<24;
426 ((uint8_t*)out
)[i
]=(uint8_t)((((uint16_t*)in
)[i
])>>8);
430 store24bit(out
, i
, ((uint32_t)((uint16_t*)in
)[i
])<<16);
434 ((uint32_t*)out
)[i
]=((uint32_t)((uint16_t*)in
)[i
])<<16;
442 ((uint8_t*)out
)[i
]=(uint8_t)(load24bit(in
, i
)>>24);
446 ((uint16_t*)out
)[i
]=(uint16_t)(load24bit(in
, i
)>>16);
450 ((uint32_t*)out
)[i
]=(uint32_t)load24bit(in
, i
);
458 ((uint8_t*)out
)[i
]=(uint8_t)((((uint32_t*)in
)[i
])>>24);
462 ((uint16_t*)out
)[i
]=(uint16_t)((((uint32_t*)in
)[i
])>>16);
466 store24bit(out
, i
, ((uint32_t*)in
)[i
]);
473 static void float2int(float* in
, void* out
, int len
, int bps
)
479 ((int8_t*)out
)[i
] = lrintf(127.0 * in
[i
]);
483 ((int16_t*)out
)[i
] = lrintf(32767.0 * in
[i
]);
487 store24bit(out
, i
, lrintf(2147483647.0 * in
[i
]));
491 ((int32_t*)out
)[i
] = lrintf(2147483647.0 * in
[i
]);
496 static void int2float(void* in
, float* out
, int len
, int bps
)
502 out
[i
]=(1.0/128.0)*((int8_t*)in
)[i
];
506 out
[i
]=(1.0/32768.0)*((int16_t*)in
)[i
];
510 out
[i
]=(1.0/2147483648.0)*((int32_t)load24bit(in
, i
));
514 out
[i
]=(1.0/2147483648.0)*((int32_t*)in
)[i
];