synced with r26729
[mplayer/greg.git] / libaf / af_format.c
blobbc6830ab583b7c898976525f860362d99a2ab161
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.
4 */
6 // Must be defined before any libc headers are included!
7 #define _ISOC9X_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #include <limits.h>
15 // Integer to float conversion through lrintf()
16 #ifdef HAVE_LRINTF
17 #include <math.h>
18 long int lrintf(float);
19 #else
20 #define lrintf(x) ((int)(x))
21 #endif
23 #include "af.h"
24 #include "mpbswap.h"
25 #include "libvo/fastmemcpy.h"
27 /* Functions used by play to convert the input audio to the correct
28 format */
30 /* The below includes retrieves functions for converting to and from
31 ulaw and alaw */
32 #include "af_format_ulaw.c"
33 #include "af_format_alaw.c"
35 // Switch endianness
36 static void endian(void* in, void* out, int len, int bps);
37 // From signed to unsigned and the other way
38 static void si2us(void* data, int len, int bps);
39 // Change the number of bits per sample
40 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
41 // From float to int signed
42 static void float2int(float* in, void* out, int len, int bps);
43 // From signed int to float
44 static void int2float(void* in, float* out, int len, int bps);
46 static af_data_t* play(struct af_instance_s* af, af_data_t* data);
47 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data);
48 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data);
49 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data);
51 // Helper functions to check sanity for input arguments
53 // Sanity check for bytes per sample
54 static int check_bps(int bps)
56 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){
57 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample"
58 " must be 1, 2, 3 or 4. Current value is %i \n",bps);
59 return AF_ERROR;
61 return AF_OK;
64 // Check for unsupported formats
65 static int check_format(int format)
67 char buf[256];
68 switch(format & AF_FORMAT_SPECIAL_MASK){
69 case(AF_FORMAT_IMA_ADPCM):
70 case(AF_FORMAT_MPEG2):
71 case(AF_FORMAT_AC3):
72 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n",
73 af_fmt2str(format,buf,256));
74 return AF_ERROR;
76 return AF_OK;
79 // Initialization and runtime control
80 static int control(struct af_instance_s* af, int cmd, void* arg)
82 switch(cmd){
83 case AF_CONTROL_REINIT:{
84 char buf1[256];
85 char buf2[256];
86 af_data_t *data = arg;
88 // Make sure this filter isn't redundant
89 if(af->data->format == data->format &&
90 af->data->bps == data->bps)
91 return AF_DETACH;
93 // Check for errors in configuration
94 if((AF_OK != check_bps(data->bps)) ||
95 (AF_OK != check_format(data->format)) ||
96 (AF_OK != check_bps(af->data->bps)) ||
97 (AF_OK != check_format(af->data->format)))
98 return AF_ERROR;
100 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %s to %s\n",
101 af_fmt2str(data->format,buf1,256),
102 af_fmt2str(af->data->format,buf2,256));
104 af->data->rate = data->rate;
105 af->data->nch = data->nch;
106 af->mul = (double)af->data->bps / data->bps;
108 af->play = play; // set default
110 // look whether only endianness differences are there
111 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
112 (data->format & ~AF_FORMAT_END_MASK))
114 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianness conversion only\n");
115 af->play = play_swapendian;
117 if ((data->format == AF_FORMAT_FLOAT_NE) &&
118 (af->data->format == AF_FORMAT_S16_NE))
120 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
121 af_fmt2str(data->format,buf1,256),
122 af_fmt2str(af->data->format,buf2,256));
123 af->play = play_float_s16;
125 if ((data->format == AF_FORMAT_S16_NE) &&
126 (af->data->format == AF_FORMAT_FLOAT_NE))
128 af_msg(AF_MSG_VERBOSE,"[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_s16_float;
133 return AF_OK;
135 case AF_CONTROL_COMMAND_LINE:{
136 int format = af_str2fmt_short(arg);
137 if (format == -1) {
138 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg);
139 return AF_ERROR;
141 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
142 return AF_ERROR;
143 return AF_OK;
145 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
146 // Check for errors in configuration
147 if(AF_OK != check_format(*(int*)arg))
148 return AF_ERROR;
150 af->data->format = *(int*)arg;
151 af->data->bps = af_fmt2bits(af->data->format)/8;
153 return AF_OK;
156 return AF_UNKNOWN;
159 // Deallocate memory
160 static void uninit(struct af_instance_s* af)
162 if (af->data)
163 free(af->data->audio);
164 free(af->data);
165 af->setup = 0;
168 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
170 af_data_t* l = af->data; // Local data
171 af_data_t* c = data; // Current working data
172 int len = c->len/c->bps; // Length in samples of current audio block
174 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
175 return NULL;
177 endian(c->audio,l->audio,len,c->bps);
179 c->audio = l->audio;
180 c->format = l->format;
182 return c;
185 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
187 af_data_t* l = af->data; // Local data
188 af_data_t* c = data; // Current working data
189 int len = c->len/4; // Length in samples of current audio block
191 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
192 return NULL;
194 float2int(c->audio, l->audio, len, 2);
196 c->audio = l->audio;
197 c->len = len*2;
198 c->bps = 2;
199 c->format = l->format;
201 return c;
204 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
206 af_data_t* l = af->data; // Local data
207 af_data_t* c = data; // Current working data
208 int len = c->len/2; // Length in samples of current audio block
210 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
211 return NULL;
213 int2float(c->audio, l->audio, len, 2);
215 c->audio = l->audio;
216 c->len = len*4;
217 c->bps = 4;
218 c->format = l->format;
220 return c;
223 // Filter data through filter
224 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
226 af_data_t* l = af->data; // Local data
227 af_data_t* c = data; // Current working data
228 int len = c->len/c->bps; // Length in samples of current audio block
230 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
231 return NULL;
233 // Change to cpu native endian format
234 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
235 endian(c->audio,c->audio,len,c->bps);
237 // Conversion table
238 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
239 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
240 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
241 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
242 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
243 si2us(l->audio,len,l->bps);
244 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
245 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
246 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
247 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
248 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
249 si2us(l->audio,len,l->bps);
250 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) {
251 switch(l->format&AF_FORMAT_SPECIAL_MASK){
252 case(AF_FORMAT_MU_LAW):
253 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
254 break;
255 case(AF_FORMAT_A_LAW):
256 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
257 break;
258 default:
259 float2int(c->audio, l->audio, len, l->bps);
260 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
261 si2us(l->audio,len,l->bps);
262 break;
264 } else {
265 // Input must be int
267 // Change signed/unsigned
268 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
269 si2us(c->audio,len,c->bps);
271 // Convert to special formats
272 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
273 case(AF_FORMAT_MU_LAW):
274 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
275 break;
276 case(AF_FORMAT_A_LAW):
277 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
278 break;
279 case(AF_FORMAT_F):
280 int2float(c->audio, l->audio, len, c->bps);
281 break;
282 default:
283 // Change the number of bits
284 if(c->bps != l->bps)
285 change_bps(c->audio,l->audio,len,c->bps,l->bps);
286 else
287 fast_memcpy(l->audio,c->audio,len*c->bps);
288 break;
292 // Switch from cpu native endian to the correct endianness
293 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
294 endian(l->audio,l->audio,len,l->bps);
296 // Set output data
297 c->audio = l->audio;
298 c->len = len*l->bps;
299 c->bps = l->bps;
300 c->format = l->format;
301 return c;
304 // Allocate memory and set function pointers
305 static int af_open(af_instance_t* af){
306 af->control=control;
307 af->uninit=uninit;
308 af->play=play;
309 af->mul=1;
310 af->data=calloc(1,sizeof(af_data_t));
311 if(af->data == NULL)
312 return AF_ERROR;
313 return AF_OK;
316 // Description of this filter
317 af_info_t af_info_format = {
318 "Sample format conversion",
319 "format",
320 "Anders",
322 AF_FLAGS_REENTRANT,
323 af_open
326 static inline uint32_t load24bit(void* data, int pos) {
327 #if WORDS_BIGENDIAN
328 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
329 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
330 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
331 #else
332 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
333 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
334 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
335 #endif
338 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
339 #if WORDS_BIGENDIAN
340 ((uint8_t*)data)[3*pos]=expanded_value>>24;
341 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
342 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
343 #else
344 ((uint8_t*)data)[3*pos]=expanded_value>>8;
345 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
346 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
347 #endif
350 // Function implementations used by play
351 static void endian(void* in, void* out, int len, int bps)
353 register int i;
354 switch(bps){
355 case(2):{
356 for(i=0;i<len;i++){
357 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
359 break;
361 case(3):{
362 register uint8_t s;
363 for(i=0;i<len;i++){
364 s=((uint8_t*)in)[3*i];
365 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
366 if (in != out)
367 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
368 ((uint8_t*)out)[3*i+2]=s;
370 break;
372 case(4):{
373 for(i=0;i<len;i++){
374 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
376 break;
381 static void si2us(void* data, int len, int bps)
383 register long i = -(len * bps);
384 register uint8_t *p = &((uint8_t *)data)[len * bps];
385 #if AF_FORMAT_NE == AF_FORMAT_LE
386 p += bps - 1;
387 #endif
388 if (len <= 0) return;
389 do {
390 p[i] ^= 0x80;
391 } while (i += bps);
394 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
396 register int i;
397 switch(inbps){
398 case(1):
399 switch(outbps){
400 case(2):
401 for(i=0;i<len;i++)
402 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
403 break;
404 case(3):
405 for(i=0;i<len;i++)
406 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
407 break;
408 case(4):
409 for(i=0;i<len;i++)
410 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
411 break;
413 break;
414 case(2):
415 switch(outbps){
416 case(1):
417 for(i=0;i<len;i++)
418 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
419 break;
420 case(3):
421 for(i=0;i<len;i++)
422 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
423 break;
424 case(4):
425 for(i=0;i<len;i++)
426 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
427 break;
429 break;
430 case(3):
431 switch(outbps){
432 case(1):
433 for(i=0;i<len;i++)
434 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
435 break;
436 case(2):
437 for(i=0;i<len;i++)
438 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
439 break;
440 case(4):
441 for(i=0;i<len;i++)
442 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
443 break;
445 break;
446 case(4):
447 switch(outbps){
448 case(1):
449 for(i=0;i<len;i++)
450 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
451 break;
452 case(2):
453 for(i=0;i<len;i++)
454 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
455 break;
456 case(3):
457 for(i=0;i<len;i++)
458 store24bit(out, i, ((uint32_t*)in)[i]);
459 break;
461 break;
465 static void float2int(float* in, void* out, int len, int bps)
467 register int i;
468 switch(bps){
469 case(1):
470 for(i=0;i<len;i++)
471 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
472 break;
473 case(2):
474 for(i=0;i<len;i++)
475 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
476 break;
477 case(3):
478 for(i=0;i<len;i++)
479 store24bit(out, i, lrintf(2147483647.0 * in[i]));
480 break;
481 case(4):
482 for(i=0;i<len;i++)
483 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
484 break;
488 static void int2float(void* in, float* out, int len, int bps)
490 register int i;
491 switch(bps){
492 case(1):
493 for(i=0;i<len;i++)
494 out[i]=(1.0/128.0)*((int8_t*)in)[i];
495 break;
496 case(2):
497 for(i=0;i<len;i++)
498 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
499 break;
500 case(3):
501 for(i=0;i<len;i++)
502 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
503 break;
504 case(4):
505 for(i=0;i<len;i++)
506 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
507 break;