10l: comparison of char* ptrs with string literals
[mplayer.git] / libaf / af_format.c
blob7c660d3fe37806b15f199198f545aa90e6ae7ea7
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 // Integer to float conversion through lrintf()
17 #ifdef HAVE_LRINTF
18 #include <math.h>
19 long int lrintf(float);
20 #else
21 #define lrintf(x) ((int)(x))
22 #endif
24 #include "af.h"
25 #include "libavutil/common.h"
26 #include "mpbswap.h"
27 #include "libvo/fastmemcpy.h"
29 /* Functions used by play to convert the input audio to the correct
30 format */
32 /* The below includes retrives functions for converting to and from
33 ulaw and alaw */
34 #include "af_format_ulaw.c"
35 #include "af_format_alaw.c"
37 // Switch endianess
38 static void endian(void* in, void* out, int len, int bps);
39 // From singed to unsigned and the other way
40 static void si2us(void* data, int len, int bps);
41 // Change the number of bits per sample
42 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
43 // From float to int signed
44 static void float2int(float* in, void* out, int len, int bps);
45 // From signed int to float
46 static void int2float(void* in, float* out, int len, int bps);
48 static af_data_t* play(struct af_instance_s* af, af_data_t* data);
49 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data);
50 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data);
51 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data);
53 // Helper functions to check sanity for input arguments
55 // Sanity check for bytes per sample
56 static int check_bps(int bps)
58 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){
59 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample"
60 " must be 1, 2, 3 or 4. Current value is %i \n",bps);
61 return AF_ERROR;
63 return AF_OK;
66 // Check for unsupported formats
67 static int check_format(int format)
69 char buf[256];
70 switch(format & AF_FORMAT_SPECIAL_MASK){
71 case(AF_FORMAT_IMA_ADPCM):
72 case(AF_FORMAT_MPEG2):
73 case(AF_FORMAT_AC3):
74 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n",
75 af_fmt2str(format,buf,256));
76 return AF_ERROR;
78 return AF_OK;
81 // Initialization and runtime control
82 static int control(struct af_instance_s* af, int cmd, void* arg)
84 switch(cmd){
85 case AF_CONTROL_REINIT:{
86 char buf1[256];
87 char buf2[256];
88 af_data_t *data = arg;
90 // Make sure this filter isn't redundant
91 if(af->data->format == data->format &&
92 af->data->bps == data->bps)
93 return AF_DETACH;
95 // Check for errors in configuraton
96 if((AF_OK != check_bps(data->bps)) ||
97 (AF_OK != check_format(data->format)) ||
98 (AF_OK != check_bps(af->data->bps)) ||
99 (AF_OK != check_format(af->data->format)))
100 return AF_ERROR;
102 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %s to %s\n",
103 af_fmt2str(data->format,buf1,256),
104 af_fmt2str(af->data->format,buf2,256));
106 af->data->rate = data->rate;
107 af->data->nch = data->nch;
108 af->mul.n = af->data->bps;
109 af->mul.d = data->bps;
110 af_frac_cancel(&af->mul);
112 af->play = play; // set default
114 // look whether only endianess differences are there
115 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
116 (data->format & ~AF_FORMAT_END_MASK))
118 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianess conversion only\n");
119 af->play = play_swapendian;
121 if ((data->format == AF_FORMAT_FLOAT_NE) &&
122 (af->data->format == AF_FORMAT_S16_NE))
124 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
125 af_fmt2str(data->format,buf1,256),
126 af_fmt2str(af->data->format,buf2,256));
127 af->play = play_float_s16;
129 if ((data->format == AF_FORMAT_S16_NE) &&
130 (af->data->format == AF_FORMAT_FLOAT_NE))
132 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
133 af_fmt2str(data->format,buf1,256),
134 af_fmt2str(af->data->format,buf2,256));
135 af->play = play_s16_float;
137 return AF_OK;
139 case AF_CONTROL_COMMAND_LINE:{
140 int format = af_str2fmt_short(arg);
141 if (format == -1) {
142 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg);
143 return AF_ERROR;
145 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
146 return AF_ERROR;
147 return AF_OK;
149 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
150 // Check for errors in configuraton
151 if(AF_OK != check_format(*(int*)arg))
152 return AF_ERROR;
154 af->data->format = *(int*)arg;
155 af->data->bps = af_fmt2bits(af->data->format)/8;
157 return AF_OK;
160 return AF_UNKNOWN;
163 // Deallocate memory
164 static void uninit(struct af_instance_s* af)
166 if (af->data)
167 free(af->data->audio);
168 free(af->data);
169 af->setup = 0;
172 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
174 af_data_t* l = af->data; // Local data
175 af_data_t* c = data; // Current working data
176 int len = c->len/c->bps; // Lenght in samples of current audio block
178 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
179 return NULL;
181 endian(c->audio,l->audio,len,c->bps);
183 c->audio = l->audio;
184 c->format = l->format;
186 return c;
189 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
191 af_data_t* l = af->data; // Local data
192 af_data_t* c = data; // Current working data
193 int len = c->len/4; // Lenght in samples of current audio block
195 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
196 return NULL;
198 float2int(c->audio, l->audio, len, 2);
200 c->audio = l->audio;
201 c->len = len*2;
202 c->bps = 2;
203 c->format = l->format;
205 return c;
208 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
210 af_data_t* l = af->data; // Local data
211 af_data_t* c = data; // Current working data
212 int len = c->len/2; // Lenght in samples of current audio block
214 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
215 return NULL;
217 int2float(c->audio, l->audio, len, 2);
219 c->audio = l->audio;
220 c->len = len*4;
221 c->bps = 4;
222 c->format = l->format;
224 return c;
227 // Filter data through filter
228 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
230 af_data_t* l = af->data; // Local data
231 af_data_t* c = data; // Current working data
232 int len = c->len/c->bps; // Lenght in samples of current audio block
234 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
235 return NULL;
237 // Change to cpu native endian format
238 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
239 endian(c->audio,c->audio,len,c->bps);
241 // Conversion table
242 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
243 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
244 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
245 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
246 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
247 si2us(l->audio,len,l->bps);
248 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
249 from_alaw(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_alaw(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_POINT_MASK) == AF_FORMAT_F) {
255 switch(l->format&AF_FORMAT_SPECIAL_MASK){
256 case(AF_FORMAT_MU_LAW):
257 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
258 break;
259 case(AF_FORMAT_A_LAW):
260 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
261 break;
262 default:
263 float2int(c->audio, l->audio, len, l->bps);
264 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
265 si2us(l->audio,len,l->bps);
266 break;
268 } else {
269 // Input must be int
271 // Change signed/unsigned
272 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
273 si2us(c->audio,len,c->bps);
275 // Convert to special formats
276 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
277 case(AF_FORMAT_MU_LAW):
278 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
279 break;
280 case(AF_FORMAT_A_LAW):
281 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
282 break;
283 case(AF_FORMAT_F):
284 int2float(c->audio, l->audio, len, c->bps);
285 break;
286 default:
287 // Change the number of bits
288 if(c->bps != l->bps)
289 change_bps(c->audio,l->audio,len,c->bps,l->bps);
290 else
291 memcpy(l->audio,c->audio,len*c->bps);
292 break;
296 // Switch from cpu native endian to the correct endianess
297 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
298 endian(l->audio,l->audio,len,l->bps);
300 // Set output data
301 c->audio = l->audio;
302 c->len = len*l->bps;
303 c->bps = l->bps;
304 c->format = l->format;
305 return c;
308 // Allocate memory and set function pointers
309 static int open(af_instance_t* af){
310 af->control=control;
311 af->uninit=uninit;
312 af->play=play;
313 af->mul.n=1;
314 af->mul.d=1;
315 af->data=calloc(1,sizeof(af_data_t));
316 if(af->data == NULL)
317 return AF_ERROR;
318 return AF_OK;
321 // Description of this filter
322 af_info_t af_info_format = {
323 "Sample format conversion",
324 "format",
325 "Anders",
327 AF_FLAGS_REENTRANT,
328 open
331 static inline uint32_t load24bit(void* data, int pos) {
332 #if WORDS_BIGENDIAN
333 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
334 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
335 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
336 #else
337 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
338 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
339 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
340 #endif
343 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
344 #if WORDS_BIGENDIAN
345 ((uint8_t*)data)[3*pos]=expanded_value>>24;
346 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
347 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
348 #else
349 ((uint8_t*)data)[3*pos]=expanded_value>>8;
350 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
351 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
352 #endif
355 // Function implementations used by play
356 static void endian(void* in, void* out, int len, int bps)
358 register int i;
359 switch(bps){
360 case(2):{
361 for(i=0;i<len;i++){
362 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
364 break;
366 case(3):{
367 register uint8_t s;
368 for(i=0;i<len;i++){
369 s=((uint8_t*)in)[3*i];
370 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
371 if (in != out)
372 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
373 ((uint8_t*)out)[3*i+2]=s;
375 break;
377 case(4):{
378 for(i=0;i<len;i++){
379 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
381 break;
386 static void si2us(void* data, int len, int bps)
388 register long i = -(len * bps);
389 register uint8_t *p = &((uint8_t *)data)[len * bps];
390 #if AF_FORMAT_NE == AF_FORMAT_LE
391 p += bps - 1;
392 #endif
393 if (len <= 0) return;
394 do {
395 p[i] ^= 0x80;
396 } while (i += bps);
399 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
401 register int i;
402 switch(inbps){
403 case(1):
404 switch(outbps){
405 case(2):
406 for(i=0;i<len;i++)
407 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
408 break;
409 case(3):
410 for(i=0;i<len;i++)
411 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
412 break;
413 case(4):
414 for(i=0;i<len;i++)
415 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
416 break;
418 break;
419 case(2):
420 switch(outbps){
421 case(1):
422 for(i=0;i<len;i++)
423 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
424 break;
425 case(3):
426 for(i=0;i<len;i++)
427 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
428 break;
429 case(4):
430 for(i=0;i<len;i++)
431 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
432 break;
434 break;
435 case(3):
436 switch(outbps){
437 case(1):
438 for(i=0;i<len;i++)
439 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
440 break;
441 case(2):
442 for(i=0;i<len;i++)
443 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
444 break;
445 case(4):
446 for(i=0;i<len;i++)
447 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
448 break;
450 break;
451 case(4):
452 switch(outbps){
453 case(1):
454 for(i=0;i<len;i++)
455 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
456 break;
457 case(2):
458 for(i=0;i<len;i++)
459 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
460 break;
461 case(3):
462 for(i=0;i<len;i++)
463 store24bit(out, i, ((uint32_t*)in)[i]);
464 break;
466 break;
470 static void float2int(float* in, void* out, int len, int bps)
472 register int i;
473 switch(bps){
474 case(1):
475 for(i=0;i<len;i++)
476 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
477 break;
478 case(2):
479 for(i=0;i<len;i++)
480 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
481 break;
482 case(3):
483 for(i=0;i<len;i++)
484 store24bit(out, i, lrintf(2147483647.0 * in[i]));
485 break;
486 case(4):
487 for(i=0;i<len;i++)
488 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
489 break;
493 static void int2float(void* in, float* out, int len, int bps)
495 register int i;
496 switch(bps){
497 case(1):
498 for(i=0;i<len;i++)
499 out[i]=(1.0/128.0)*((int8_t*)in)[i];
500 break;
501 case(2):
502 for(i=0;i<len;i++)
503 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
504 break;
505 case(3):
506 for(i=0;i<len;i++)
507 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
508 break;
509 case(4):
510 for(i=0;i<len;i++)
511 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
512 break;