another round of '-' escapes
[mplayer/greg.git] / libaf / af_format.c
blob58f70732f3fa9c4f86a5bf34d0bb2b54a9925610
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 = (double)af->data->bps / data->bps;
109 af->play = play; // set default
111 // look whether only endianness differences are there
112 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
113 (data->format & ~AF_FORMAT_END_MASK))
115 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianness conversion only\n");
116 af->play = play_swapendian;
118 if ((data->format == AF_FORMAT_FLOAT_NE) &&
119 (af->data->format == AF_FORMAT_S16_NE))
121 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
122 af_fmt2str(data->format,buf1,256),
123 af_fmt2str(af->data->format,buf2,256));
124 af->play = play_float_s16;
126 if ((data->format == AF_FORMAT_S16_NE) &&
127 (af->data->format == AF_FORMAT_FLOAT_NE))
129 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
130 af_fmt2str(data->format,buf1,256),
131 af_fmt2str(af->data->format,buf2,256));
132 af->play = play_s16_float;
134 return AF_OK;
136 case AF_CONTROL_COMMAND_LINE:{
137 int format = af_str2fmt_short(arg);
138 if (format == -1) {
139 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg);
140 return AF_ERROR;
142 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
143 return AF_ERROR;
144 return AF_OK;
146 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
147 // Check for errors in configuration
148 if(AF_OK != check_format(*(int*)arg))
149 return AF_ERROR;
151 af->data->format = *(int*)arg;
152 af->data->bps = af_fmt2bits(af->data->format)/8;
154 return AF_OK;
157 return AF_UNKNOWN;
160 // Deallocate memory
161 static void uninit(struct af_instance_s* af)
163 if (af->data)
164 free(af->data->audio);
165 free(af->data);
166 af->setup = 0;
169 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
171 af_data_t* l = af->data; // Local data
172 af_data_t* c = data; // Current working data
173 int len = c->len/c->bps; // Length in samples of current audio block
175 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
176 return NULL;
178 endian(c->audio,l->audio,len,c->bps);
180 c->audio = l->audio;
181 c->format = l->format;
183 return c;
186 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
188 af_data_t* l = af->data; // Local data
189 af_data_t* c = data; // Current working data
190 int len = c->len/4; // Length in samples of current audio block
192 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
193 return NULL;
195 float2int(c->audio, l->audio, len, 2);
197 c->audio = l->audio;
198 c->len = len*2;
199 c->bps = 2;
200 c->format = l->format;
202 return c;
205 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
207 af_data_t* l = af->data; // Local data
208 af_data_t* c = data; // Current working data
209 int len = c->len/2; // Length in samples of current audio block
211 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
212 return NULL;
214 int2float(c->audio, l->audio, len, 2);
216 c->audio = l->audio;
217 c->len = len*4;
218 c->bps = 4;
219 c->format = l->format;
221 return c;
224 // Filter data through filter
225 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
227 af_data_t* l = af->data; // Local data
228 af_data_t* c = data; // Current working data
229 int len = c->len/c->bps; // Length in samples of current audio block
231 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
232 return NULL;
234 // Change to cpu native endian format
235 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
236 endian(c->audio,c->audio,len,c->bps);
238 // Conversion table
239 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
240 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
241 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
242 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
243 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
244 si2us(l->audio,len,l->bps);
245 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
246 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
247 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
248 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
249 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
250 si2us(l->audio,len,l->bps);
251 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) {
252 switch(l->format&AF_FORMAT_SPECIAL_MASK){
253 case(AF_FORMAT_MU_LAW):
254 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
255 break;
256 case(AF_FORMAT_A_LAW):
257 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
258 break;
259 default:
260 float2int(c->audio, l->audio, len, l->bps);
261 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
262 si2us(l->audio,len,l->bps);
263 break;
265 } else {
266 // Input must be int
268 // Change signed/unsigned
269 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
270 si2us(c->audio,len,c->bps);
272 // Convert to special formats
273 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
274 case(AF_FORMAT_MU_LAW):
275 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
276 break;
277 case(AF_FORMAT_A_LAW):
278 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
279 break;
280 case(AF_FORMAT_F):
281 int2float(c->audio, l->audio, len, c->bps);
282 break;
283 default:
284 // Change the number of bits
285 if(c->bps != l->bps)
286 change_bps(c->audio,l->audio,len,c->bps,l->bps);
287 else
288 fast_memcpy(l->audio,c->audio,len*c->bps);
289 break;
293 // Switch from cpu native endian to the correct endianness
294 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
295 endian(l->audio,l->audio,len,l->bps);
297 // Set output data
298 c->audio = l->audio;
299 c->len = len*l->bps;
300 c->bps = l->bps;
301 c->format = l->format;
302 return c;
305 // Allocate memory and set function pointers
306 static int af_open(af_instance_t* af){
307 af->control=control;
308 af->uninit=uninit;
309 af->play=play;
310 af->mul=1;
311 af->data=calloc(1,sizeof(af_data_t));
312 if(af->data == NULL)
313 return AF_ERROR;
314 return AF_OK;
317 // Description of this filter
318 af_info_t af_info_format = {
319 "Sample format conversion",
320 "format",
321 "Anders",
323 AF_FLAGS_REENTRANT,
324 af_open
327 static inline uint32_t load24bit(void* data, int pos) {
328 #if WORDS_BIGENDIAN
329 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
330 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
331 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
332 #else
333 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
334 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
335 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
336 #endif
339 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
340 #if WORDS_BIGENDIAN
341 ((uint8_t*)data)[3*pos]=expanded_value>>24;
342 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
343 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
344 #else
345 ((uint8_t*)data)[3*pos]=expanded_value>>8;
346 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
347 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
348 #endif
351 // Function implementations used by play
352 static void endian(void* in, void* out, int len, int bps)
354 register int i;
355 switch(bps){
356 case(2):{
357 for(i=0;i<len;i++){
358 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
360 break;
362 case(3):{
363 register uint8_t s;
364 for(i=0;i<len;i++){
365 s=((uint8_t*)in)[3*i];
366 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
367 if (in != out)
368 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
369 ((uint8_t*)out)[3*i+2]=s;
371 break;
373 case(4):{
374 for(i=0;i<len;i++){
375 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
377 break;
382 static void si2us(void* data, int len, int bps)
384 register long i = -(len * bps);
385 register uint8_t *p = &((uint8_t *)data)[len * bps];
386 #if AF_FORMAT_NE == AF_FORMAT_LE
387 p += bps - 1;
388 #endif
389 if (len <= 0) return;
390 do {
391 p[i] ^= 0x80;
392 } while (i += bps);
395 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
397 register int i;
398 switch(inbps){
399 case(1):
400 switch(outbps){
401 case(2):
402 for(i=0;i<len;i++)
403 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
404 break;
405 case(3):
406 for(i=0;i<len;i++)
407 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
408 break;
409 case(4):
410 for(i=0;i<len;i++)
411 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
412 break;
414 break;
415 case(2):
416 switch(outbps){
417 case(1):
418 for(i=0;i<len;i++)
419 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
420 break;
421 case(3):
422 for(i=0;i<len;i++)
423 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
424 break;
425 case(4):
426 for(i=0;i<len;i++)
427 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
428 break;
430 break;
431 case(3):
432 switch(outbps){
433 case(1):
434 for(i=0;i<len;i++)
435 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
436 break;
437 case(2):
438 for(i=0;i<len;i++)
439 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
440 break;
441 case(4):
442 for(i=0;i<len;i++)
443 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
444 break;
446 break;
447 case(4):
448 switch(outbps){
449 case(1):
450 for(i=0;i<len;i++)
451 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
452 break;
453 case(2):
454 for(i=0;i<len;i++)
455 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
456 break;
457 case(3):
458 for(i=0;i<len;i++)
459 store24bit(out, i, ((uint32_t*)in)[i]);
460 break;
462 break;
466 static void float2int(float* in, void* out, int len, int bps)
468 register int i;
469 switch(bps){
470 case(1):
471 for(i=0;i<len;i++)
472 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
473 break;
474 case(2):
475 for(i=0;i<len;i++)
476 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
477 break;
478 case(3):
479 for(i=0;i<len;i++)
480 store24bit(out, i, lrintf(2147483647.0 * in[i]));
481 break;
482 case(4):
483 for(i=0;i<len;i++)
484 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
485 break;
489 static void int2float(void* in, float* out, int len, int bps)
491 register int i;
492 switch(bps){
493 case(1):
494 for(i=0;i<len;i++)
495 out[i]=(1.0/128.0)*((int8_t*)in)[i];
496 break;
497 case(2):
498 for(i=0;i<len;i++)
499 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
500 break;
501 case(3):
502 for(i=0;i<len;i++)
503 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
504 break;
505 case(4):
506 for(i=0;i<len;i++)
507 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
508 break;