audio: add af_lavrresample, remove old resampling filters
[mplayer2.git] / libaf / af_format.c
blob729b964726c3647c886638b24a46bb678df286aa
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 mp_msg(MSGT_AFILTER, MSGL_ERR, "[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 mp_msg(MSGT_AFILTER, MSGL_ERR, "[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 // Allow trivial AC3-endianness conversion
102 if (!AF_FORMAT_IS_AC3(af->data->format) || !AF_FORMAT_IS_AC3(data->format))
103 // Check for errors in configuration
104 if((AF_OK != check_bps(data->bps)) ||
105 (AF_OK != check_format(data->format)) ||
106 (AF_OK != check_bps(af->data->bps)) ||
107 (AF_OK != check_format(af->data->format)))
108 return AF_ERROR;
110 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Changing sample format from %s to %s\n",
111 af_fmt2str(data->format,buf1,256),
112 af_fmt2str(af->data->format,buf2,256));
114 af->data->rate = data->rate;
115 af->data->nch = data->nch;
116 af->mul = (double)af->data->bps / data->bps;
118 af->play = play; // set default
120 // look whether only endianness differences are there
121 if ((af->data->format & ~AF_FORMAT_END_MASK) ==
122 (data->format & ~AF_FORMAT_END_MASK))
124 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated endianness conversion only\n");
125 af->play = play_swapendian;
127 if ((data->format == AF_FORMAT_FLOAT_NE) &&
128 (af->data->format == AF_FORMAT_S16_NE))
130 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n",
131 af_fmt2str(data->format,buf1,256),
132 af_fmt2str(af->data->format,buf2,256));
133 af->play = play_float_s16;
135 if ((data->format == AF_FORMAT_S16_NE) &&
136 (af->data->format == AF_FORMAT_FLOAT_NE))
138 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n",
139 af_fmt2str(data->format,buf1,256),
140 af_fmt2str(af->data->format,buf2,256));
141 af->play = play_s16_float;
143 return AF_OK;
145 case AF_CONTROL_COMMAND_LINE:{
146 int format = af_str2fmt_short(arg);
147 if (format == -1) {
148 mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] %s is not a valid format\n", (char *)arg);
149 return AF_ERROR;
151 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))
152 return AF_ERROR;
153 return AF_OK;
155 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{
156 // Check for errors in configuration
157 if(!AF_FORMAT_IS_AC3(*(int*)arg) && AF_OK != check_format(*(int*)arg))
158 return AF_ERROR;
160 af->data->format = *(int*)arg;
161 af->data->bps = af_fmt2bits(af->data->format)/8;
163 return AF_OK;
166 return AF_UNKNOWN;
169 // Deallocate memory
170 static void uninit(struct af_instance_s* af)
172 if (af->data)
173 free(af->data->audio);
174 free(af->data);
175 af->setup = 0;
178 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data)
180 af_data_t* l = af->data; // Local data
181 af_data_t* c = data; // Current working data
182 int len = c->len/c->bps; // Length in samples of current audio block
184 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
185 return NULL;
187 endian(c->audio,l->audio,len,c->bps);
189 c->audio = l->audio;
190 c->format = l->format;
192 return c;
195 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data)
197 af_data_t* l = af->data; // Local data
198 af_data_t* c = data; // Current working data
199 int len = c->len/4; // Length in samples of current audio block
201 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
202 return NULL;
204 float2int(c->audio, l->audio, len, 2);
206 c->audio = l->audio;
207 c->len = len*2;
208 c->bps = 2;
209 c->format = l->format;
211 return c;
214 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data)
216 af_data_t* l = af->data; // Local data
217 af_data_t* c = data; // Current working data
218 int len = c->len/2; // Length in samples of current audio block
220 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
221 return NULL;
223 int2float(c->audio, l->audio, len, 2);
225 c->audio = l->audio;
226 c->len = len*4;
227 c->bps = 4;
228 c->format = l->format;
230 return c;
233 // Filter data through filter
234 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
236 af_data_t* l = af->data; // Local data
237 af_data_t* c = data; // Current working data
238 int len = c->len/c->bps; // Length in samples of current audio block
240 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
241 return NULL;
243 // Change to cpu native endian format
244 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
245 endian(c->audio,c->audio,len,c->bps);
247 // Conversion table
248 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) {
249 from_ulaw(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_ulaw(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_SPECIAL_MASK) == AF_FORMAT_A_LAW) {
255 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK);
256 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK))
257 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI);
258 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
259 si2us(l->audio,len,l->bps);
260 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) {
261 switch(l->format&AF_FORMAT_SPECIAL_MASK){
262 case(AF_FORMAT_MU_LAW):
263 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
264 break;
265 case(AF_FORMAT_A_LAW):
266 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
267 break;
268 default:
269 float2int(c->audio, l->audio, len, l->bps);
270 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
271 si2us(l->audio,len,l->bps);
272 break;
274 } else {
275 // Input must be int
277 // Change signed/unsigned
278 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){
279 si2us(c->audio,len,c->bps);
281 // Convert to special formats
282 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){
283 case(AF_FORMAT_MU_LAW):
284 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
285 break;
286 case(AF_FORMAT_A_LAW):
287 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK);
288 break;
289 case(AF_FORMAT_F):
290 int2float(c->audio, l->audio, len, c->bps);
291 break;
292 default:
293 // Change the number of bits
294 if(c->bps != l->bps)
295 change_bps(c->audio,l->audio,len,c->bps,l->bps);
296 else
297 fast_memcpy(l->audio,c->audio,len*c->bps);
298 break;
302 // Switch from cpu native endian to the correct endianness
303 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE)
304 endian(l->audio,l->audio,len,l->bps);
306 // Set output data
307 c->audio = l->audio;
308 c->len = len*l->bps;
309 c->bps = l->bps;
310 c->format = l->format;
311 return c;
314 // Allocate memory and set function pointers
315 static int af_open(af_instance_t* af){
316 af->control=control;
317 af->uninit=uninit;
318 af->play=play;
319 af->mul=1;
320 af->data=calloc(1,sizeof(af_data_t));
321 if(af->data == NULL)
322 return AF_ERROR;
323 return AF_OK;
326 // Description of this filter
327 af_info_t af_info_format = {
328 "Sample format conversion",
329 "format",
330 "Anders",
332 AF_FLAGS_REENTRANT,
333 af_open
336 static inline uint32_t load24bit(void* data, int pos) {
337 #if HAVE_BIGENDIAN
338 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) |
339 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
340 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8);
341 #else
342 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) |
343 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) |
344 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24);
345 #endif
348 static inline void store24bit(void* data, int pos, uint32_t expanded_value) {
349 #if HAVE_BIGENDIAN
350 ((uint8_t*)data)[3*pos]=expanded_value>>24;
351 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
352 ((uint8_t*)data)[3*pos+2]=expanded_value>>8;
353 #else
354 ((uint8_t*)data)[3*pos]=expanded_value>>8;
355 ((uint8_t*)data)[3*pos+1]=expanded_value>>16;
356 ((uint8_t*)data)[3*pos+2]=expanded_value>>24;
357 #endif
360 // Function implementations used by play
361 static void endian(void* in, void* out, int len, int bps)
363 register int i;
364 switch(bps){
365 case(2):{
366 for(i=0;i<len;i++){
367 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]);
369 break;
371 case(3):{
372 register uint8_t s;
373 for(i=0;i<len;i++){
374 s=((uint8_t*)in)[3*i];
375 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2];
376 if (in != out)
377 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1];
378 ((uint8_t*)out)[3*i+2]=s;
380 break;
382 case(4):{
383 for(i=0;i<len;i++){
384 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]);
386 break;
391 static void si2us(void* data, int len, int bps)
393 register long i = -(len * bps);
394 register uint8_t *p = &((uint8_t *)data)[len * bps];
395 #if AF_FORMAT_NE == AF_FORMAT_LE
396 p += bps - 1;
397 #endif
398 if (len <= 0) return;
399 do {
400 p[i] ^= 0x80;
401 } while (i += bps);
404 static void change_bps(void* in, void* out, int len, int inbps, int outbps)
406 register int i;
407 switch(inbps){
408 case(1):
409 switch(outbps){
410 case(2):
411 for(i=0;i<len;i++)
412 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8;
413 break;
414 case(3):
415 for(i=0;i<len;i++)
416 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24);
417 break;
418 case(4):
419 for(i=0;i<len;i++)
420 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24;
421 break;
423 break;
424 case(2):
425 switch(outbps){
426 case(1):
427 for(i=0;i<len;i++)
428 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8);
429 break;
430 case(3):
431 for(i=0;i<len;i++)
432 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16);
433 break;
434 case(4):
435 for(i=0;i<len;i++)
436 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16;
437 break;
439 break;
440 case(3):
441 switch(outbps){
442 case(1):
443 for(i=0;i<len;i++)
444 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24);
445 break;
446 case(2):
447 for(i=0;i<len;i++)
448 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16);
449 break;
450 case(4):
451 for(i=0;i<len;i++)
452 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i);
453 break;
455 break;
456 case(4):
457 switch(outbps){
458 case(1):
459 for(i=0;i<len;i++)
460 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24);
461 break;
462 case(2):
463 for(i=0;i<len;i++)
464 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16);
465 break;
466 case(3):
467 for(i=0;i<len;i++)
468 store24bit(out, i, ((uint32_t*)in)[i]);
469 break;
471 break;
475 static void float2int(float* in, void* out, int len, int bps)
477 register int i;
478 switch(bps){
479 case(1):
480 for(i=0;i<len;i++)
481 ((int8_t*)out)[i] = lrintf(127.0 * clamp(in[i], -1.0f, +1.0f));
482 break;
483 case(2):
484 for(i=0;i<len;i++)
485 ((int16_t*)out)[i] = lrintf(32767.0 * clamp(in[i], -1.0f, +1.0f));
486 break;
487 case(3):
488 for(i=0;i<len;i++)
489 store24bit(out, i, lrintf(2147483647.0 * clamp(in[i], -1.0f, +1.0f)));
490 break;
491 case(4):
492 for(i=0;i<len;i++)
493 ((int32_t*)out)[i] = lrintf(2147483647.0 * clamp(in[i], -1.0f, +1.0f));
494 break;
498 static void int2float(void* in, float* out, int len, int bps)
500 register int i;
501 switch(bps){
502 case(1):
503 for(i=0;i<len;i++)
504 out[i]=(1.0/128.0)*((int8_t*)in)[i];
505 break;
506 case(2):
507 for(i=0;i<len;i++)
508 out[i]=(1.0/32768.0)*((int16_t*)in)[i];
509 break;
510 case(3):
511 for(i=0;i<len;i++)
512 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i));
513 break;
514 case(4):
515 for(i=0;i<len;i++)
516 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i];
517 break;