synced with r24788
[mplayer/glamo.git] / libaf / af_format.c
blobc042c6acce96877d0c3a81f1aef531a524ff2718
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 "libavutil/common.h"
25 #include "mpbswap.h"
26 #include "libvo/fastmemcpy.h"
28 /* Functions used by play to convert the input audio to the correct
29 format */
31 /* The below includes retrieves functions for converting to and from
32 ulaw and alaw */
33 #include "af_format_ulaw.c"
34 #include "af_format_alaw.c"
36 // Switch endianness
37 static void endian(void* in, void* out, int len, int bps);
38 // From signed 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 configuration
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 endianness 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 endianness 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 configuration
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->audio);
167 free(af->data);
168 af->setup = 0;
171 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
173 af_data_t* l = af->data; // Local data
174 af_data_t* c = data; // Current working data
175 int len = c->len/c->bps; // Length in samples of current audio block
177 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
178 return NULL;
180 endian(c->audio,l->audio,len,c->bps);
182 c->audio = l->audio;
183 c->format = l->format;
185 return c;
188 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
190 af_data_t* l = af->data; // Local data
191 af_data_t* c = data; // Current working data
192 int len = c->len/4; // Length in samples of current audio block
194 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
195 return NULL;
197 float2int(c->audio, l->audio, len, 2);
199 c->audio = l->audio;
200 c->len = len*2;
201 c->bps = 2;
202 c->format = l->format;
204 return c;
207 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
209 af_data_t* l = af->data; // Local data
210 af_data_t* c = data; // Current working data
211 int len = c->len/2; // Length in samples of current audio block
213 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
214 return NULL;
216 int2float(c->audio, l->audio, len, 2);
218 c->audio = l->audio;
219 c->len = len*4;
220 c->bps = 4;
221 c->format = l->format;
223 return c;
226 // Filter data through filter
227 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
229 af_data_t* l = af->data; // Local data
230 af_data_t* c = data; // Current working data
231 int len = c->len/c->bps; // Length in samples of current audio block
233 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
234 return NULL;
236 // Change to cpu native endian format
237 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
238 endian(c->audio,c->audio,len,c->bps);
240 // Conversion table
241 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
242 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
243 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
244 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
245 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
246 si2us(l->audio,len,l->bps);
247 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
248 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
249 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
250 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
251 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
252 si2us(l->audio,len,l->bps);
253 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) {
254 switch(l->format&AF_FORMAT_SPECIAL_MASK){
255 case(AF_FORMAT_MU_LAW):
256 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
257 break;
258 case(AF_FORMAT_A_LAW):
259 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
260 break;
261 default:
262 float2int(c->audio, l->audio, len, l->bps);
263 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
264 si2us(l->audio,len,l->bps);
265 break;
267 } else {
268 // Input must be int
270 // Change signed/unsigned
271 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
272 si2us(c->audio,len,c->bps);
274 // Convert to special formats
275 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
276 case(AF_FORMAT_MU_LAW):
277 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
278 break;
279 case(AF_FORMAT_A_LAW):
280 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
281 break;
282 case(AF_FORMAT_F):
283 int2float(c->audio, l->audio, len, c->bps);
284 break;
285 default:
286 // Change the number of bits
287 if(c->bps != l->bps)
288 change_bps(c->audio,l->audio,len,c->bps,l->bps);
289 else
290 fast_memcpy(l->audio,c->audio,len*c->bps);
291 break;
295 // Switch from cpu native endian to the correct endianness
296 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
297 endian(l->audio,l->audio,len,l->bps);
299 // Set output data
300 c->audio = l->audio;
301 c->len = len*l->bps;
302 c->bps = l->bps;
303 c->format = l->format;
304 return c;
307 // Allocate memory and set function pointers
308 static int af_open(af_instance_t* af){
309 af->control=control;
310 af->uninit=uninit;
311 af->play=play;
312 af->mul.n=1;
313 af->mul.d=1;
314 af->data=calloc(1,sizeof(af_data_t));
315 if(af->data == NULL)
316 return AF_ERROR;
317 return AF_OK;
320 // Description of this filter
321 af_info_t af_info_format = {
322 "Sample format conversion",
323 "format",
324 "Anders",
326 AF_FLAGS_REENTRANT,
327 af_open
330 static inline uint32_t load24bit(void* data, int pos) {
331 #if WORDS_BIGENDIAN
332 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
333 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
334 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
335 #else
336 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
337 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
338 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
339 #endif
342 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
343 #if WORDS_BIGENDIAN
344 ((uint8_t*)data)[3*pos]=expanded_value>>24;
345 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
346 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
347 #else
348 ((uint8_t*)data)[3*pos]=expanded_value>>8;
349 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
350 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
351 #endif
354 // Function implementations used by play
355 static void endian(void* in, void* out, int len, int bps)
357 register int i;
358 switch(bps){
359 case(2):{
360 for(i=0;i<len;i++){
361 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
363 break;
365 case(3):{
366 register uint8_t s;
367 for(i=0;i<len;i++){
368 s=((uint8_t*)in)[3*i];
369 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
370 if (in != out)
371 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
372 ((uint8_t*)out)[3*i+2]=s;
374 break;
376 case(4):{
377 for(i=0;i<len;i++){
378 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
380 break;
385 static void si2us(void* data, int len, int bps)
387 register long i = -(len * bps);
388 register uint8_t *p = &((uint8_t *)data)[len * bps];
389 #if AF_FORMAT_NE == AF_FORMAT_LE
390 p += bps - 1;
391 #endif
392 if (len <= 0) return;
393 do {
394 p[i] ^= 0x80;
395 } while (i += bps);
398 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
400 register int i;
401 switch(inbps){
402 case(1):
403 switch(outbps){
404 case(2):
405 for(i=0;i<len;i++)
406 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
407 break;
408 case(3):
409 for(i=0;i<len;i++)
410 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
411 break;
412 case(4):
413 for(i=0;i<len;i++)
414 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
415 break;
417 break;
418 case(2):
419 switch(outbps){
420 case(1):
421 for(i=0;i<len;i++)
422 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
423 break;
424 case(3):
425 for(i=0;i<len;i++)
426 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
427 break;
428 case(4):
429 for(i=0;i<len;i++)
430 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
431 break;
433 break;
434 case(3):
435 switch(outbps){
436 case(1):
437 for(i=0;i<len;i++)
438 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
439 break;
440 case(2):
441 for(i=0;i<len;i++)
442 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
443 break;
444 case(4):
445 for(i=0;i<len;i++)
446 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
447 break;
449 break;
450 case(4):
451 switch(outbps){
452 case(1):
453 for(i=0;i<len;i++)
454 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
455 break;
456 case(2):
457 for(i=0;i<len;i++)
458 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
459 break;
460 case(3):
461 for(i=0;i<len;i++)
462 store24bit(out, i, ((uint32_t*)in)[i]);
463 break;
465 break;
469 static void float2int(float* in, void* out, int len, int bps)
471 register int i;
472 switch(bps){
473 case(1):
474 for(i=0;i<len;i++)
475 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
476 break;
477 case(2):
478 for(i=0;i<len;i++)
479 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
480 break;
481 case(3):
482 for(i=0;i<len;i++)
483 store24bit(out, i, lrintf(2147483647.0 * in[i]));
484 break;
485 case(4):
486 for(i=0;i<len;i++)
487 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
488 break;
492 static void int2float(void* in, float* out, int len, int bps)
494 register int i;
495 switch(bps){
496 case(1):
497 for(i=0;i<len;i++)
498 out[i]=(1.0/128.0)*((int8_t*)in)[i];
499 break;
500 case(2):
501 for(i=0;i<len;i++)
502 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
503 break;
504 case(3):
505 for(i=0;i<len;i++)
506 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
507 break;
508 case(4):
509 for(i=0;i<len;i++)
510 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
511 break;