Merge svn changes up to r28310
[mplayer.git] / libaf / af_format.c
blobfdac68147ed15bcd12337ea53c4633c4c9e8fbce
1 /*
2 * This audio filter changes the format of a data block. Valid
3 * formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE
4 * AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE.
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <limits.h>
29 // Integer to float conversion through lrintf()
30 #ifdef HAVE_LRINTF
31 #include <math.h>
32 long int lrintf(float);
33 #else
34 #define lrintf(x) ((int)(x))
35 #endif
37 #include "af.h"
38 #include "mpbswap.h"
39 #include "libvo/fastmemcpy.h"
41 /* Functions used by play to convert the input audio to the correct
42 format */
44 /* The below includes retrieves functions for converting to and from
45 ulaw and alaw */
46 #include "af_format_ulaw.h"
47 #include "af_format_alaw.h"
49 // Switch endianness
50 static void endian(void* in, void* out, int len, int bps);
51 // From signed to unsigned and the other way
52 static void si2us(void* data, int len, int bps);
53 // Change the number of bits per sample
54 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
55 // From float to int signed
56 static void float2int(float* in, void* out, int len, int bps);
57 // From signed int to float
58 static void int2float(void* in, float* out, int len, int bps);
60 static af_data_t* play(struct af_instance_s* af, af_data_t* data);
61 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data);
62 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data);
63 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data);
65 // Helper functions to check sanity for input arguments
67 // Sanity check for bytes per sample
68 static int check_bps(int bps)
70 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){
71 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample"
72 " must be 1, 2, 3 or 4. Current value is %i \n",bps);
73 return AF_ERROR;
75 return AF_OK;
78 // Check for unsupported formats
79 static int check_format(int format)
81 char buf[256];
82 switch(format & AF_FORMAT_SPECIAL_MASK){
83 case(AF_FORMAT_IMA_ADPCM):
84 case(AF_FORMAT_MPEG2):
85 case(AF_FORMAT_AC3):
86 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n",
87 af_fmt2str(format,buf,256));
88 return AF_ERROR;
90 return AF_OK;
93 // Initialization and runtime control
94 static int control(struct af_instance_s* af, int cmd, void* arg)
96 switch(cmd){
97 case AF_CONTROL_REINIT:{
98 char buf1[256];
99 char buf2[256];
100 af_data_t *data = arg;
102 // Make sure this filter isn't redundant
103 if(af->data->format == data->format &&
104 af->data->bps == data->bps)
105 return AF_DETACH;
107 // Check for errors in configuration
108 if((AF_OK != check_bps(data->bps)) ||
109 (AF_OK != check_format(data->format)) ||
110 (AF_OK != check_bps(af->data->bps)) ||
111 (AF_OK != check_format(af->data->format)))
112 return AF_ERROR;
114 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %s to %s\n",
115 af_fmt2str(data->format,buf1,256),
116 af_fmt2str(af->data->format,buf2,256));
118 af->data->rate = data->rate;
119 af->data->nch = data->nch;
120 af->mul = (double)af->data->bps / data->bps;
122 af->play = play; // set default
124 // look whether only endianness differences are there
125 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
126 (data->format & ~AF_FORMAT_END_MASK))
128 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianness conversion only\n");
129 af->play = play_swapendian;
131 if ((data->format == AF_FORMAT_FLOAT_NE) &&
132 (af->data->format == AF_FORMAT_S16_NE))
134 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
135 af_fmt2str(data->format,buf1,256),
136 af_fmt2str(af->data->format,buf2,256));
137 af->play = play_float_s16;
139 if ((data->format == AF_FORMAT_S16_NE) &&
140 (af->data->format == AF_FORMAT_FLOAT_NE))
142 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
143 af_fmt2str(data->format,buf1,256),
144 af_fmt2str(af->data->format,buf2,256));
145 af->play = play_s16_float;
147 return AF_OK;
149 case AF_CONTROL_COMMAND_LINE:{
150 int format = af_str2fmt_short(arg);
151 if (format == -1) {
152 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg);
153 return AF_ERROR;
155 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
156 return AF_ERROR;
157 return AF_OK;
159 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
160 // Check for errors in configuration
161 if(AF_OK != check_format(*(int*)arg))
162 return AF_ERROR;
164 af->data->format = *(int*)arg;
165 af->data->bps = af_fmt2bits(af->data->format)/8;
167 return AF_OK;
170 return AF_UNKNOWN;
173 // Deallocate memory
174 static void uninit(struct af_instance_s* af)
176 if (af->data)
177 free(af->data->audio);
178 free(af->data);
179 af->setup = 0;
182 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
184 af_data_t* l = af->data; // Local data
185 af_data_t* c = data; // Current working data
186 int len = c->len/c->bps; // Length in samples of current audio block
188 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
189 return NULL;
191 endian(c->audio,l->audio,len,c->bps);
193 c->audio = l->audio;
194 c->format = l->format;
196 return c;
199 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
201 af_data_t* l = af->data; // Local data
202 af_data_t* c = data; // Current working data
203 int len = c->len/4; // Length in samples of current audio block
205 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
206 return NULL;
208 float2int(c->audio, l->audio, len, 2);
210 c->audio = l->audio;
211 c->len = len*2;
212 c->bps = 2;
213 c->format = l->format;
215 return c;
218 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
220 af_data_t* l = af->data; // Local data
221 af_data_t* c = data; // Current working data
222 int len = c->len/2; // Length in samples of current audio block
224 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
225 return NULL;
227 int2float(c->audio, l->audio, len, 2);
229 c->audio = l->audio;
230 c->len = len*4;
231 c->bps = 4;
232 c->format = l->format;
234 return c;
237 // Filter data through filter
238 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
240 af_data_t* l = af->data; // Local data
241 af_data_t* c = data; // Current working data
242 int len = c->len/c->bps; // Length in samples of current audio block
244 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
245 return NULL;
247 // Change to cpu native endian format
248 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
249 endian(c->audio,c->audio,len,c->bps);
251 // Conversion table
252 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
253 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
254 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
255 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
256 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
257 si2us(l->audio,len,l->bps);
258 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
259 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
260 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
261 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
262 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
263 si2us(l->audio,len,l->bps);
264 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) {
265 switch(l->format&AF_FORMAT_SPECIAL_MASK){
266 case(AF_FORMAT_MU_LAW):
267 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
268 break;
269 case(AF_FORMAT_A_LAW):
270 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
271 break;
272 default:
273 float2int(c->audio, l->audio, len, l->bps);
274 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
275 si2us(l->audio,len,l->bps);
276 break;
278 } else {
279 // Input must be int
281 // Change signed/unsigned
282 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
283 si2us(c->audio,len,c->bps);
285 // Convert to special formats
286 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
287 case(AF_FORMAT_MU_LAW):
288 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
289 break;
290 case(AF_FORMAT_A_LAW):
291 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
292 break;
293 case(AF_FORMAT_F):
294 int2float(c->audio, l->audio, len, c->bps);
295 break;
296 default:
297 // Change the number of bits
298 if(c->bps != l->bps)
299 change_bps(c->audio,l->audio,len,c->bps,l->bps);
300 else
301 fast_memcpy(l->audio,c->audio,len*c->bps);
302 break;
306 // Switch from cpu native endian to the correct endianness
307 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
308 endian(l->audio,l->audio,len,l->bps);
310 // Set output data
311 c->audio = l->audio;
312 c->len = len*l->bps;
313 c->bps = l->bps;
314 c->format = l->format;
315 return c;
318 // Allocate memory and set function pointers
319 static int af_open(af_instance_t* af){
320 af->control=control;
321 af->uninit=uninit;
322 af->play=play;
323 af->mul=1;
324 af->data=calloc(1,sizeof(af_data_t));
325 if(af->data == NULL)
326 return AF_ERROR;
327 return AF_OK;
330 // Description of this filter
331 af_info_t af_info_format = {
332 "Sample format conversion",
333 "format",
334 "Anders",
336 AF_FLAGS_REENTRANT,
337 af_open
340 static inline uint32_t load24bit(void* data, int pos) {
341 #if WORDS_BIGENDIAN
342 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
343 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
344 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
345 #else
346 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
347 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
348 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
349 #endif
352 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
353 #if WORDS_BIGENDIAN
354 ((uint8_t*)data)[3*pos]=expanded_value>>24;
355 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
356 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
357 #else
358 ((uint8_t*)data)[3*pos]=expanded_value>>8;
359 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
360 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
361 #endif
364 // Function implementations used by play
365 static void endian(void* in, void* out, int len, int bps)
367 register int i;
368 switch(bps){
369 case(2):{
370 for(i=0;i<len;i++){
371 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
373 break;
375 case(3):{
376 register uint8_t s;
377 for(i=0;i<len;i++){
378 s=((uint8_t*)in)[3*i];
379 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
380 if (in != out)
381 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
382 ((uint8_t*)out)[3*i+2]=s;
384 break;
386 case(4):{
387 for(i=0;i<len;i++){
388 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
390 break;
395 static void si2us(void* data, int len, int bps)
397 register long i = -(len * bps);
398 register uint8_t *p = &((uint8_t *)data)[len * bps];
399 #if AF_FORMAT_NE == AF_FORMAT_LE
400 p += bps - 1;
401 #endif
402 if (len <= 0) return;
403 do {
404 p[i] ^= 0x80;
405 } while (i += bps);
408 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
410 register int i;
411 switch(inbps){
412 case(1):
413 switch(outbps){
414 case(2):
415 for(i=0;i<len;i++)
416 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
417 break;
418 case(3):
419 for(i=0;i<len;i++)
420 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
421 break;
422 case(4):
423 for(i=0;i<len;i++)
424 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
425 break;
427 break;
428 case(2):
429 switch(outbps){
430 case(1):
431 for(i=0;i<len;i++)
432 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
433 break;
434 case(3):
435 for(i=0;i<len;i++)
436 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
437 break;
438 case(4):
439 for(i=0;i<len;i++)
440 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
441 break;
443 break;
444 case(3):
445 switch(outbps){
446 case(1):
447 for(i=0;i<len;i++)
448 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
449 break;
450 case(2):
451 for(i=0;i<len;i++)
452 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
453 break;
454 case(4):
455 for(i=0;i<len;i++)
456 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
457 break;
459 break;
460 case(4):
461 switch(outbps){
462 case(1):
463 for(i=0;i<len;i++)
464 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
465 break;
466 case(2):
467 for(i=0;i<len;i++)
468 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
469 break;
470 case(3):
471 for(i=0;i<len;i++)
472 store24bit(out, i, ((uint32_t*)in)[i]);
473 break;
475 break;
479 static void float2int(float* in, void* out, int len, int bps)
481 register int i;
482 switch(bps){
483 case(1):
484 for(i=0;i<len;i++)
485 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
486 break;
487 case(2):
488 for(i=0;i<len;i++)
489 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
490 break;
491 case(3):
492 for(i=0;i<len;i++)
493 store24bit(out, i, lrintf(2147483647.0 * in[i]));
494 break;
495 case(4):
496 for(i=0;i<len;i++)
497 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
498 break;
502 static void int2float(void* in, float* out, int len, int bps)
504 register int i;
505 switch(bps){
506 case(1):
507 for(i=0;i<len;i++)
508 out[i]=(1.0/128.0)*((int8_t*)in)[i];
509 break;
510 case(2):
511 for(i=0;i<len;i++)
512 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
513 break;
514 case(3):
515 for(i=0;i<len;i++)
516 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
517 break;
518 case(4):
519 for(i=0;i<len;i++)
520 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
521 break;