Get rid of code I neither know nor use anymore.
[mplayer/glamo.git] / libaf / af_format.c
blobe5b7cc900e74bdcf595b246150250c270f392e6c
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 <unistd.h>
13 #include <inttypes.h>
14 #include <limits.h>
16 #include "af.h"
17 #include "bswap.h"
18 #include "libvo/fastmemcpy.h"
20 // Integer to float conversion through lrintf()
21 #ifdef HAVE_LRINTF
22 #include <math.h>
23 long int lrintf(float);
24 #else
25 #define lrintf(x) ((int)(x))
26 #endif
28 /* Functions used by play to convert the input audio to the correct
29 format */
31 /* The below includes retrives functions for converting to and from
32 ulaw and alaw */
33 #include "af_format_ulaw.c"
34 #include "af_format_alaw.c"
36 // Switch endianess
37 static void endian(void* in, void* out, int len, int bps);
38 // From singed to unsigned and the other way
39 static void si2us(void* data, int len, int bps);
40 // Change the number of bits per sample
41 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
42 // From float to int signed
43 static void float2int(float* in, void* out, int len, int bps);
44 // From signed int to float
45 static void int2float(void* in, float* out, int len, int bps);
47 static af_data_t* play(struct af_instance_s* af, af_data_t* data);
48 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data);
49 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data);
50 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data);
52 // Helper functions to check sanity for input arguments
54 // Sanity check for bytes per sample
55 static int check_bps(int bps)
57 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){
58 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample"
59 " must be 1, 2, 3 or 4. Current value is %i \n",bps);
60 return AF_ERROR;
62 return AF_OK;
65 // Check for unsupported formats
66 static int check_format(int format)
68 char buf[256];
69 switch(format & AF_FORMAT_SPECIAL_MASK){
70 case(AF_FORMAT_IMA_ADPCM):
71 case(AF_FORMAT_MPEG2):
72 case(AF_FORMAT_AC3):
73 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n",
74 af_fmt2str(format,buf,256));
75 return AF_ERROR;
77 return AF_OK;
80 // Initialization and runtime control
81 static int control(struct af_instance_s* af, int cmd, void* arg)
83 switch(cmd){
84 case AF_CONTROL_REINIT:{
85 char buf1[256];
86 char buf2[256];
87 af_data_t *data = arg;
89 // Make sure this filter isn't redundant
90 if(af->data->format == data->format &&
91 af->data->bps == data->bps)
92 return AF_DETACH;
94 // Check for errors in configuraton
95 if((AF_OK != check_bps(data->bps)) ||
96 (AF_OK != check_format(data->format)) ||
97 (AF_OK != check_bps(af->data->bps)) ||
98 (AF_OK != check_format(af->data->format)))
99 return AF_ERROR;
101 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %s to %s\n",
102 af_fmt2str(data->format,buf1,256),
103 af_fmt2str(af->data->format,buf2,256));
105 af->data->rate = data->rate;
106 af->data->nch = data->nch;
107 af->mul.n = af->data->bps;
108 af->mul.d = data->bps;
109 af_frac_cancel(&af->mul);
111 af->play = play; // set default
113 // look whether only endianess differences are there
114 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
115 (data->format & ~AF_FORMAT_END_MASK))
117 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianess conversion only\n");
118 af->play = play_swapendian;
120 if ((data->format == AF_FORMAT_FLOAT_NE) &&
121 (af->data->format == AF_FORMAT_S16_NE))
123 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
124 af_fmt2str(data->format,buf1,256),
125 af_fmt2str(af->data->format,buf2,256));
126 af->play = play_float_s16;
128 if ((data->format == AF_FORMAT_S16_NE) &&
129 (af->data->format == AF_FORMAT_FLOAT_NE))
131 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
132 af_fmt2str(data->format,buf1,256),
133 af_fmt2str(af->data->format,buf2,256));
134 af->play = play_s16_float;
136 return AF_OK;
138 case AF_CONTROL_COMMAND_LINE:{
139 int format = af_str2fmt_short(arg);
140 if (format == -1) {
141 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg);
142 return AF_ERROR;
144 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
145 return AF_ERROR;
146 return AF_OK;
148 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
149 // Check for errors in configuraton
150 if(AF_OK != check_format(*(int*)arg))
151 return AF_ERROR;
153 af->data->format = *(int*)arg;
154 af->data->bps = af_fmt2bits(af->data->format)/8;
156 return AF_OK;
159 return AF_UNKNOWN;
162 // Deallocate memory
163 static void uninit(struct af_instance_s* af)
165 if(af->data)
166 free(af->data);
167 af->setup = 0;
170 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
172 af_data_t* l = af->data; // Local data
173 af_data_t* c = data; // Current working data
174 int len = c->len/c->bps; // Lenght in samples of current audio block
176 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
177 return NULL;
179 endian(c->audio,l->audio,len,c->bps);
181 c->audio = l->audio;
182 c->format = l->format;
184 return c;
187 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
189 af_data_t* l = af->data; // Local data
190 af_data_t* c = data; // Current working data
191 int len = c->len/4; // Lenght in samples of current audio block
193 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
194 return NULL;
196 float2int(c->audio, l->audio, len, 2);
198 c->audio = l->audio;
199 c->len = len*2;
200 c->bps = 2;
201 c->format = l->format;
203 return c;
206 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
208 af_data_t* l = af->data; // Local data
209 af_data_t* c = data; // Current working data
210 int len = c->len/2; // Lenght in samples of current audio block
212 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
213 return NULL;
215 int2float(c->audio, l->audio, len, 2);
217 c->audio = l->audio;
218 c->len = len*4;
219 c->bps = 4;
220 c->format = l->format;
222 return c;
225 // Filter data through filter
226 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
228 af_data_t* l = af->data; // Local data
229 af_data_t* c = data; // Current working data
230 int len = c->len/c->bps; // Lenght in samples of current audio block
232 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
233 return NULL;
235 // Change to cpu native endian format
236 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
237 endian(c->audio,c->audio,len,c->bps);
239 // Conversion table
240 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
241 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
242 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
243 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
244 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
245 si2us(l->audio,len,l->bps);
246 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
247 from_alaw(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_alaw(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_POINT_MASK) == AF_FORMAT_F) {
253 switch(l->format&AF_FORMAT_SPECIAL_MASK){
254 case(AF_FORMAT_MU_LAW):
255 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
256 break;
257 case(AF_FORMAT_A_LAW):
258 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
259 break;
260 default:
261 float2int(c->audio, l->audio, len, l->bps);
262 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
263 si2us(l->audio,len,l->bps);
264 break;
266 } else {
267 // Input must be int
269 // Change signed/unsigned
270 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
271 si2us(c->audio,len,c->bps);
273 // Convert to special formats
274 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
275 case(AF_FORMAT_MU_LAW):
276 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
277 break;
278 case(AF_FORMAT_A_LAW):
279 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
280 break;
281 case(AF_FORMAT_F):
282 int2float(c->audio, l->audio, len, c->bps);
283 break;
284 default:
285 // Change the number of bits
286 if(c->bps != l->bps)
287 change_bps(c->audio,l->audio,len,c->bps,l->bps);
288 else
289 memcpy(l->audio,c->audio,len*c->bps);
290 break;
294 // Switch from cpu native endian to the correct endianess
295 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
296 endian(l->audio,l->audio,len,l->bps);
298 // Set output data
299 c->audio = l->audio;
300 c->len = len*l->bps;
301 c->bps = l->bps;
302 c->format = l->format;
303 return c;
306 // Allocate memory and set function pointers
307 static int open(af_instance_t* af){
308 af->control=control;
309 af->uninit=uninit;
310 af->play=play;
311 af->mul.n=1;
312 af->mul.d=1;
313 af->data=calloc(1,sizeof(af_data_t));
314 if(af->data == NULL)
315 return AF_ERROR;
316 return AF_OK;
319 // Description of this filter
320 af_info_t af_info_format = {
321 "Sample format conversion",
322 "format",
323 "Anders",
325 AF_FLAGS_REENTRANT,
326 open
329 static inline uint32_t load24bit(void* data, int pos) {
330 #if WORDS_BIGENDIAN
331 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
332 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
333 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
334 #else
335 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
336 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
337 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
338 #endif
341 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
342 #if WORDS_BIGENDIAN
343 ((uint8_t*)data)[3*pos]=expanded_value>>24;
344 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
345 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
346 #else
347 ((uint8_t*)data)[3*pos]=expanded_value>>8;
348 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
349 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
350 #endif
353 // Function implementations used by play
354 static void endian(void* in, void* out, int len, int bps)
356 register int i;
357 switch(bps){
358 case(2):{
359 for(i=0;i<len;i++){
360 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
362 break;
364 case(3):{
365 register uint8_t s;
366 for(i=0;i<len;i++){
367 s=((uint8_t*)in)[3*i];
368 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
369 if (in != out)
370 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
371 ((uint8_t*)out)[3*i+2]=s;
373 break;
375 case(4):{
376 for(i=0;i<len;i++){
377 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
379 break;
384 static void si2us(void* data, int len, int bps)
386 register long i = -(len * bps);
387 register uint8_t *p = &((uint8_t *)data)[len * bps];
388 #if AF_FORMAT_NE == AF_FORMAT_LE
389 p += bps - 1;
390 #endif
391 if (len <= 0) return;
392 do {
393 p[i] ^= 0x80;
394 } while (i += bps);
397 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
399 register int i;
400 switch(inbps){
401 case(1):
402 switch(outbps){
403 case(2):
404 for(i=0;i<len;i++)
405 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
406 break;
407 case(3):
408 for(i=0;i<len;i++)
409 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
410 break;
411 case(4):
412 for(i=0;i<len;i++)
413 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
414 break;
416 break;
417 case(2):
418 switch(outbps){
419 case(1):
420 for(i=0;i<len;i++)
421 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
422 break;
423 case(3):
424 for(i=0;i<len;i++)
425 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
426 break;
427 case(4):
428 for(i=0;i<len;i++)
429 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
430 break;
432 break;
433 case(3):
434 switch(outbps){
435 case(1):
436 for(i=0;i<len;i++)
437 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
438 break;
439 case(2):
440 for(i=0;i<len;i++)
441 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
442 break;
443 case(4):
444 for(i=0;i<len;i++)
445 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
446 break;
448 break;
449 case(4):
450 switch(outbps){
451 case(1):
452 for(i=0;i<len;i++)
453 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
454 break;
455 case(2):
456 for(i=0;i<len;i++)
457 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
458 break;
459 case(3):
460 for(i=0;i<len;i++)
461 store24bit(out, i, ((uint32_t*)in)[i]);
462 break;
464 break;
468 static void float2int(float* in, void* out, int len, int bps)
470 register int i;
471 switch(bps){
472 case(1):
473 for(i=0;i<len;i++)
474 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
475 break;
476 case(2):
477 for(i=0;i<len;i++)
478 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
479 break;
480 case(3):
481 for(i=0;i<len;i++)
482 store24bit(out, i, lrintf(2147483647.0 * in[i]));
483 break;
484 case(4):
485 for(i=0;i<len;i++)
486 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
487 break;
491 static void int2float(void* in, float* out, int len, int bps)
493 register int i;
494 switch(bps){
495 case(1):
496 for(i=0;i<len;i++)
497 out[i]=(1.0/128.0)*((int8_t*)in)[i];
498 break;
499 case(2):
500 for(i=0;i<len;i++)
501 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
502 break;
503 case(3):
504 for(i=0;i<len;i++)
505 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
506 break;
507 case(4):
508 for(i=0;i<len;i++)
509 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
510 break;