sqcp plays with ffqclp in ffmpeg
[mplayer/glamo.git] / libaf / af_format.c
blobdf4dac284117c0cf0aeeb5dec25a2407528e5cad
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>
28 #include <math.h>
30 #include "config.h"
31 #include "af.h"
32 #include "mpbswap.h"
33 #include "libvo/fastmemcpy.h"
35 /* Functions used by play to convert the input audio to the correct
36 format */
38 /* The below includes retrieves functions for converting to and from
39 ulaw and alaw */
40 #include "af_format_ulaw.h"
41 #include "af_format_alaw.h"
43 // Switch endianness
44 static void endian(void* in, void* out, int len, int bps);
45 // From signed to unsigned and the other way
46 static void si2us(void* data, int len, int bps);
47 // Change the number of bits per sample
48 static void change_bps(void* in, void* out, int len, int inbps, int outbps);
49 // From float to int signed
50 static void float2int(float* in, void* out, int len, int bps);
51 // From signed int to float
52 static void int2float(void* in, float* out, int len, int bps);
54 static af_data_t* play(struct af_instance_s* af, af_data_t* data);
55 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data);
56 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data);
57 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data);
59 // Helper functions to check sanity for input arguments
61 // Sanity check for bytes per sample
62 static int check_bps(int bps)
64 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){
65 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample"
66 " must be 1, 2, 3 or 4. Current value is %i \n",bps);
67 return AF_ERROR;
69 return AF_OK;
72 // Check for unsupported formats
73 static int check_format(int format)
75 char buf[256];
76 switch(format & AF_FORMAT_SPECIAL_MASK){
77 case(AF_FORMAT_IMA_ADPCM):
78 case(AF_FORMAT_MPEG2):
79 case(AF_FORMAT_AC3):
80 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n",
81 af_fmt2str(format,buf,256));
82 return AF_ERROR;
84 return AF_OK;
87 // Initialization and runtime control
88 static int control(struct af_instance_s* af, int cmd, void* arg)
90 switch(cmd){
91 case AF_CONTROL_REINIT:{
92 char buf1[256];
93 char buf2[256];
94 af_data_t *data = arg;
96 // Make sure this filter isn't redundant
97 if(af->data->format == data->format &&
98 af->data->bps == data->bps)
99 return AF_DETACH;
101 // Check for errors in configuration
102 if((AF_OK != check_bps(data->bps)) ||
103 (AF_OK != check_format(data->format)) ||
104 (AF_OK != check_bps(af->data->bps)) ||
105 (AF_OK != check_format(af->data->format)))
106 return AF_ERROR;
108 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %s to %s\n",
109 af_fmt2str(data->format,buf1,256),
110 af_fmt2str(af->data->format,buf2,256));
112 af->data->rate = data->rate;
113 af->data->nch = data->nch;
114 af->mul = (double)af->data->bps / data->bps;
116 af->play = play; // set default
118 // look whether only endianness differences are there
119 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
120 (data->format & ~AF_FORMAT_END_MASK))
122 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianness conversion only\n");
123 af->play = play_swapendian;
125 if ((data->format == AF_FORMAT_FLOAT_NE) &&
126 (af->data->format == AF_FORMAT_S16_NE))
128 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
129 af_fmt2str(data->format,buf1,256),
130 af_fmt2str(af->data->format,buf2,256));
131 af->play = play_float_s16;
133 if ((data->format == AF_FORMAT_S16_NE) &&
134 (af->data->format == AF_FORMAT_FLOAT_NE))
136 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n",
137 af_fmt2str(data->format,buf1,256),
138 af_fmt2str(af->data->format,buf2,256));
139 af->play = play_s16_float;
141 return AF_OK;
143 case AF_CONTROL_COMMAND_LINE:{
144 int format = af_str2fmt_short(arg);
145 if (format == -1) {
146 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg);
147 return AF_ERROR;
149 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
150 return AF_ERROR;
151 return AF_OK;
153 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
154 // Check for errors in configuration
155 if(AF_OK != check_format(*(int*)arg))
156 return AF_ERROR;
158 af->data->format = *(int*)arg;
159 af->data->bps = af_fmt2bits(af->data->format)/8;
161 return AF_OK;
164 return AF_UNKNOWN;
167 // Deallocate memory
168 static void uninit(struct af_instance_s* af)
170 if (af->data)
171 free(af->data->audio);
172 free(af->data);
173 af->setup = 0;
176 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
178 af_data_t* l = af->data; // Local data
179 af_data_t* c = data; // Current working data
180 int len = c->len/c->bps; // Length in samples of current audio block
182 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
183 return NULL;
185 endian(c->audio,l->audio,len,c->bps);
187 c->audio = l->audio;
188 c->format = l->format;
190 return c;
193 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
195 af_data_t* l = af->data; // Local data
196 af_data_t* c = data; // Current working data
197 int len = c->len/4; // Length in samples of current audio block
199 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
200 return NULL;
202 float2int(c->audio, l->audio, len, 2);
204 c->audio = l->audio;
205 c->len = len*2;
206 c->bps = 2;
207 c->format = l->format;
209 return c;
212 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
214 af_data_t* l = af->data; // Local data
215 af_data_t* c = data; // Current working data
216 int len = c->len/2; // Length in samples of current audio block
218 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
219 return NULL;
221 int2float(c->audio, l->audio, len, 2);
223 c->audio = l->audio;
224 c->len = len*4;
225 c->bps = 4;
226 c->format = l->format;
228 return c;
231 // Filter data through filter
232 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
234 af_data_t* l = af->data; // Local data
235 af_data_t* c = data; // Current working data
236 int len = c->len/c->bps; // Length in samples of current audio block
238 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
239 return NULL;
241 // Change to cpu native endian format
242 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
243 endian(c->audio,c->audio,len,c->bps);
245 // Conversion table
246 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
247 from_ulaw(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_ulaw(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_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
253 from_alaw(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_alaw(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_POINT_MASK) == AF_FORMAT_F) {
259 switch(l->format&AF_FORMAT_SPECIAL_MASK){
260 case(AF_FORMAT_MU_LAW):
261 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
262 break;
263 case(AF_FORMAT_A_LAW):
264 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
265 break;
266 default:
267 float2int(c->audio, l->audio, len, l->bps);
268 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
269 si2us(l->audio,len,l->bps);
270 break;
272 } else {
273 // Input must be int
275 // Change signed/unsigned
276 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
277 si2us(c->audio,len,c->bps);
279 // Convert to special formats
280 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
281 case(AF_FORMAT_MU_LAW):
282 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
283 break;
284 case(AF_FORMAT_A_LAW):
285 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
286 break;
287 case(AF_FORMAT_F):
288 int2float(c->audio, l->audio, len, c->bps);
289 break;
290 default:
291 // Change the number of bits
292 if(c->bps != l->bps)
293 change_bps(c->audio,l->audio,len,c->bps,l->bps);
294 else
295 fast_memcpy(l->audio,c->audio,len*c->bps);
296 break;
300 // Switch from cpu native endian to the correct endianness
301 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
302 endian(l->audio,l->audio,len,l->bps);
304 // Set output data
305 c->audio = l->audio;
306 c->len = len*l->bps;
307 c->bps = l->bps;
308 c->format = l->format;
309 return c;
312 // Allocate memory and set function pointers
313 static int af_open(af_instance_t* af){
314 af->control=control;
315 af->uninit=uninit;
316 af->play=play;
317 af->mul=1;
318 af->data=calloc(1,sizeof(af_data_t));
319 if(af->data == NULL)
320 return AF_ERROR;
321 return AF_OK;
324 // Description of this filter
325 af_info_t af_info_format = {
326 "Sample format conversion",
327 "format",
328 "Anders",
330 AF_FLAGS_REENTRANT,
331 af_open
334 static inline uint32_t load24bit(void* data, int pos) {
335 #ifdef WORDS_BIGENDIAN
336 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
337 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
338 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
339 #else
340 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
341 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
342 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
343 #endif
346 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
347 #ifdef WORDS_BIGENDIAN
348 ((uint8_t*)data)[3*pos]=expanded_value>>24;
349 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
350 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
351 #else
352 ((uint8_t*)data)[3*pos]=expanded_value>>8;
353 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
354 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
355 #endif
358 // Function implementations used by play
359 static void endian(void* in, void* out, int len, int bps)
361 register int i;
362 switch(bps){
363 case(2):{
364 for(i=0;i<len;i++){
365 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
367 break;
369 case(3):{
370 register uint8_t s;
371 for(i=0;i<len;i++){
372 s=((uint8_t*)in)[3*i];
373 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
374 if (in != out)
375 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
376 ((uint8_t*)out)[3*i+2]=s;
378 break;
380 case(4):{
381 for(i=0;i<len;i++){
382 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
384 break;
389 static void si2us(void* data, int len, int bps)
391 register long i = -(len * bps);
392 register uint8_t *p = &((uint8_t *)data)[len * bps];
393 #if AF_FORMAT_NE == AF_FORMAT_LE
394 p += bps - 1;
395 #endif
396 if (len <= 0) return;
397 do {
398 p[i] ^= 0x80;
399 } while (i += bps);
402 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
404 register int i;
405 switch(inbps){
406 case(1):
407 switch(outbps){
408 case(2):
409 for(i=0;i<len;i++)
410 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
411 break;
412 case(3):
413 for(i=0;i<len;i++)
414 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
415 break;
416 case(4):
417 for(i=0;i<len;i++)
418 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
419 break;
421 break;
422 case(2):
423 switch(outbps){
424 case(1):
425 for(i=0;i<len;i++)
426 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
427 break;
428 case(3):
429 for(i=0;i<len;i++)
430 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
431 break;
432 case(4):
433 for(i=0;i<len;i++)
434 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
435 break;
437 break;
438 case(3):
439 switch(outbps){
440 case(1):
441 for(i=0;i<len;i++)
442 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
443 break;
444 case(2):
445 for(i=0;i<len;i++)
446 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
447 break;
448 case(4):
449 for(i=0;i<len;i++)
450 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
451 break;
453 break;
454 case(4):
455 switch(outbps){
456 case(1):
457 for(i=0;i<len;i++)
458 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
459 break;
460 case(2):
461 for(i=0;i<len;i++)
462 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
463 break;
464 case(3):
465 for(i=0;i<len;i++)
466 store24bit(out, i, ((uint32_t*)in)[i]);
467 break;
469 break;
473 static void float2int(float* in, void* out, int len, int bps)
475 register int i;
476 switch(bps){
477 case(1):
478 for(i=0;i<len;i++)
479 ((int8_t*)out)[i] = lrintf(127.0 * in[i]);
480 break;
481 case(2):
482 for(i=0;i<len;i++)
483 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]);
484 break;
485 case(3):
486 for(i=0;i<len;i++)
487 store24bit(out, i, lrintf(2147483647.0 * in[i]));
488 break;
489 case(4):
490 for(i=0;i<len;i++)
491 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]);
492 break;
496 static void int2float(void* in, float* out, int len, int bps)
498 register int i;
499 switch(bps){
500 case(1):
501 for(i=0;i<len;i++)
502 out[i]=(1.0/128.0)*((int8_t*)in)[i];
503 break;
504 case(2):
505 for(i=0;i<len;i++)
506 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
507 break;
508 case(3):
509 for(i=0;i<len;i++)
510 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
511 break;
512 case(4):
513 for(i=0;i<len;i++)
514 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
515 break;