Mercurial > mplayer.hg
annotate libaf/af_format.c @ 14322:e0f63bf677c0
synced to 1.74 (typo fix)
author | gabrov |
---|---|
date | Mon, 03 Jan 2005 10:19:48 +0000 |
parents | 5a2473f37499 |
children | 8380694ba14f |
rev | line source |
---|---|
7568 | 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 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <unistd.h> | |
10 #include <inttypes.h> | |
11 #include <limits.h> | |
12 | |
13 #include "af.h" | |
12486 | 14 #include "../bswap.h" |
14272 | 15 #include "../libvo/fastmemcpy.h" |
7568 | 16 |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
17 // Integer to float conversion through lrintf() |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
18 #ifdef HAVE_LRINTF |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
19 #define __USE_ISOC99 1 |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
20 #include <math.h> |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
21 #else |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
22 #define lrintf(x) ((int)(x)) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
23 #endif |
8167 | 24 |
25 /* Functions used by play to convert the input audio to the correct | |
26 format */ | |
7568 | 27 |
8167 | 28 /* The below includes retrives functions for converting to and from |
29 ulaw and alaw */ | |
30 #include "af_format_ulaw.c" | |
31 #include "af_format_alaw.c" | |
7568 | 32 |
8167 | 33 // Switch endianess |
34 static void endian(void* in, void* out, int len, int bps); | |
35 // From singed to unsigned | |
36 static void si2us(void* in, void* out, int len, int bps); | |
37 // From unsinged to signed | |
38 static void us2si(void* in, void* out, int len, int bps); | |
39 // Change the number of bits per sample | |
40 static void change_bps(void* in, void* out, int len, int inbps, int outbps); | |
41 // From float to int signed | |
42 static void float2int(void* in, void* out, int len, int bps); | |
43 // From signed int to float | |
44 static void int2float(void* in, void* out, int len, int bps); | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
45 |
14272 | 46 static af_data_t* play(struct af_instance_s* af, af_data_t* data); |
47 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data); | |
48 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data); | |
49 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data); | |
50 | |
8167 | 51 // Convert from string to format |
14245 | 52 int af_str2fmt(char* str) |
7568 | 53 { |
8167 | 54 int format=0; |
55 // Scan for endianess | |
56 if(strstr(str,"be") || strstr(str,"BE")) | |
57 format |= AF_FORMAT_BE; | |
58 else if(strstr(str,"le") || strstr(str,"LE")) | |
59 format |= AF_FORMAT_LE; | |
60 else | |
61 format |= AF_FORMAT_NE; | |
62 | |
63 // Scan for special formats | |
64 if(strstr(str,"mulaw") || strstr(str,"MULAW")){ | |
65 format |= AF_FORMAT_MU_LAW; return format; | |
66 } | |
67 if(strstr(str,"alaw") || strstr(str,"ALAW")){ | |
68 format |= AF_FORMAT_A_LAW; return format; | |
69 } | |
70 if(strstr(str,"ac3") || strstr(str,"AC3")){ | |
71 format |= AF_FORMAT_AC3; return format; | |
72 } | |
73 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){ | |
74 format |= AF_FORMAT_MPEG2; return format; | |
75 } | |
76 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){ | |
77 format |= AF_FORMAT_IMA_ADPCM; return format; | |
78 } | |
79 | |
80 // Scan for int/float | |
81 if(strstr(str,"float") || strstr(str,"FLOAT")){ | |
82 format |= AF_FORMAT_F; return format; | |
7568 | 83 } |
8167 | 84 else |
85 format |= AF_FORMAT_I; | |
7568 | 86 |
8167 | 87 // Scan for signed/unsigned |
88 if(strstr(str,"unsigned") || strstr(str,"UNSIGNED")) | |
89 format |= AF_FORMAT_US; | |
90 else | |
91 format |= AF_FORMAT_SI; | |
92 | |
93 return format; | |
94 } | |
95 | |
14245 | 96 inline int af_fmt2bits(int format) |
97 { | |
98 return (format & AF_FORMAT_BITS_MASK)+8; | |
99 // return (((format & AF_FORMAT_BITS_MASK)>>3)+1) * 8; | |
100 #if 0 | |
101 switch(format & AF_FORMAT_BITS_MASK) | |
102 { | |
103 case AF_FORMAT_8BIT: return 8; | |
104 case AF_FORMAT_16BIT: return 16; | |
105 case AF_FORMAT_24BIT: return 24; | |
106 case AF_FORMAT_32BIT: return 32; | |
107 case AF_FORMAT_48BIT: return 48; | |
108 } | |
109 #endif | |
110 return -1; | |
111 } | |
112 | |
8167 | 113 /* Convert format to str input str is a buffer for the |
114 converted string, size is the size of the buffer */ | |
14245 | 115 char* af_fmt2str(int format, char* str, int size) |
8167 | 116 { |
117 int i=0; | |
14245 | 118 |
14256
27002dcf0a70
ensure af_fmt2str always return a 0 terminated string
reimar
parents:
14245
diff
changeset
|
119 if (size < 1) |
27002dcf0a70
ensure af_fmt2str always return a 0 terminated string
reimar
parents:
14245
diff
changeset
|
120 return NULL; |
27002dcf0a70
ensure af_fmt2str always return a 0 terminated string
reimar
parents:
14245
diff
changeset
|
121 size--; // reserve one for terminating 0 |
27002dcf0a70
ensure af_fmt2str always return a 0 terminated string
reimar
parents:
14245
diff
changeset
|
122 |
14245 | 123 // Endianess |
8167 | 124 if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK)) |
14272 | 125 i+=snprintf(str,size-i,"little-endian "); |
8167 | 126 else |
14272 | 127 i+=snprintf(str,size-i,"big-endian "); |
8167 | 128 |
129 if(format & AF_FORMAT_SPECIAL_MASK){ | |
130 switch(format & AF_FORMAT_SPECIAL_MASK){ | |
131 case(AF_FORMAT_MU_LAW): | |
14272 | 132 i+=snprintf(&str[i],size-i,"mu-law "); break; |
8167 | 133 case(AF_FORMAT_A_LAW): |
14272 | 134 i+=snprintf(&str[i],size-i,"A-law "); break; |
8167 | 135 case(AF_FORMAT_MPEG2): |
14272 | 136 i+=snprintf(&str[i],size-i,"MPEG-2 "); break; |
8167 | 137 case(AF_FORMAT_AC3): |
138 i+=snprintf(&str[i],size-i,"AC3 "); break; | |
14263 | 139 case(AF_FORMAT_IMA_ADPCM): |
14272 | 140 i+=snprintf(&str[i],size-i,"IMA-ADPCM "); break; |
14245 | 141 default: |
142 printf("Unknown special\n"); | |
8167 | 143 } |
144 } | |
145 else{ | |
14245 | 146 // Bits |
147 i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format)); | |
148 | |
8167 | 149 // Point |
150 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK)) | |
14245 | 151 i+=snprintf(&str[i],size-i,"float "); |
8167 | 152 else{ |
153 // Sign | |
154 if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK)) | |
155 i+=snprintf(&str[i],size-i,"unsigned "); | |
156 else | |
157 i+=snprintf(&str[i],size-i,"signed "); | |
158 | |
14245 | 159 i+=snprintf(&str[i],size-i,"int "); |
8167 | 160 } |
161 } | |
14256
27002dcf0a70
ensure af_fmt2str always return a 0 terminated string
reimar
parents:
14245
diff
changeset
|
162 str[i] = 0; // make sure it is 0 terminated. |
8167 | 163 return str; |
7568 | 164 } |
165 | |
14263 | 166 char *af_fmt2str_short(int format) |
167 { | |
168 switch(format) | |
169 { | |
170 // special | |
171 case AF_FORMAT_MU_LAW: return "mulaw"; | |
172 case AF_FORMAT_A_LAW: return "alaw"; | |
173 case AF_FORMAT_MPEG2: return "mpeg2"; | |
174 case AF_FORMAT_AC3: return "ac3"; | |
175 case AF_FORMAT_IMA_ADPCM: return "imaadpcm"; | |
176 // ordinary | |
177 case AF_FORMAT_U8: return "u8"; | |
178 case AF_FORMAT_S8: return "s8"; | |
179 case AF_FORMAT_U16_LE: return "u16le"; | |
180 case AF_FORMAT_U16_BE: return "u16be"; | |
181 case AF_FORMAT_S16_LE: return "s16le"; | |
182 case AF_FORMAT_S16_BE: return "s16be"; | |
183 case AF_FORMAT_U24_LE: return "u24le"; | |
184 case AF_FORMAT_U24_BE: return "u24be"; | |
185 case AF_FORMAT_S24_LE: return "s24le"; | |
186 case AF_FORMAT_S24_BE: return "s24be"; | |
187 case AF_FORMAT_U32_LE: return "u32le"; | |
188 case AF_FORMAT_U32_BE: return "u32be"; | |
189 case AF_FORMAT_S32_LE: return "s32le"; | |
190 case AF_FORMAT_S32_BE: return "s32be"; | |
191 case AF_FORMAT_FLOAT_LE: return "floatle"; | |
192 case AF_FORMAT_FLOAT_BE: return "floatbe"; | |
193 } | |
194 return "??"; | |
195 } | |
196 | |
8607 | 197 // Helper functions to check sanity for input arguments |
198 | |
199 // Sanity check for bytes per sample | |
13989
dcb6b4a33aaa
declare check_format and check_bps static, they are used nowhere else.
reimar
parents:
12486
diff
changeset
|
200 static int check_bps(int bps) |
8607 | 201 { |
12478 | 202 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ |
8607 | 203 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample" |
12478 | 204 " must be 1, 2, 3 or 4. Current value is %i \n",bps); |
8607 | 205 return AF_ERROR; |
206 } | |
207 return AF_OK; | |
208 } | |
209 | |
210 // Check for unsupported formats | |
13989
dcb6b4a33aaa
declare check_format and check_bps static, they are used nowhere else.
reimar
parents:
12486
diff
changeset
|
211 static int check_format(int format) |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
212 { |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
213 char buf[256]; |
8607 | 214 switch(format & AF_FORMAT_SPECIAL_MASK){ |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
215 case(AF_FORMAT_MPEG2): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
216 case(AF_FORMAT_AC3): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
217 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n", |
14245 | 218 af_fmt2str(format,buf,255)); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
219 return AF_ERROR; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
220 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
221 return AF_OK; |
8607 | 222 } |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
223 |
7568 | 224 // Initialization and runtime control |
225 static int control(struct af_instance_s* af, int cmd, void* arg) | |
226 { | |
227 switch(cmd){ | |
8167 | 228 case AF_CONTROL_REINIT:{ |
229 char buf1[256]; | |
230 char buf2[256]; | |
14272 | 231 af_data_t *data = arg; |
232 | |
7568 | 233 // Make sure this filter isn't redundant |
8167 | 234 if(af->data->format == ((af_data_t*)arg)->format && |
235 af->data->bps == ((af_data_t*)arg)->bps) | |
7568 | 236 return AF_DETACH; |
8607 | 237 |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
238 // Check for errors in configuraton |
8607 | 239 if((AF_OK != check_bps(((af_data_t*)arg)->bps)) || |
240 (AF_OK != check_format(((af_data_t*)arg)->format)) || | |
241 (AF_OK != check_bps(af->data->bps)) || | |
242 (AF_OK != check_format(af->data->format))) | |
8167 | 243 return AF_ERROR; |
244 | |
14245 | 245 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %sto %s \n", |
246 af_fmt2str(((af_data_t*)arg)->format,buf1,255), | |
247 af_fmt2str(af->data->format,buf2,255)); | |
7568 | 248 |
249 af->data->rate = ((af_data_t*)arg)->rate; | |
250 af->data->nch = ((af_data_t*)arg)->nch; | |
251 af->mul.n = af->data->bps; | |
252 af->mul.d = ((af_data_t*)arg)->bps; | |
14272 | 253 |
254 af->play = play; // set default | |
255 | |
256 // look whether only endianess differences are there | |
257 if ((af->data->format & ~AF_FORMAT_END_MASK) == | |
258 (data->format & ~AF_FORMAT_END_MASK)) | |
259 { | |
260 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianess conversion only\n"); | |
261 af->play = play_swapendian; | |
262 } | |
263 if ((data->format == AF_FORMAT_FLOAT_NE) && | |
264 (af->data->format == AF_FORMAT_S16_NE)) | |
265 { | |
266 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %sto %sconversion\n", | |
267 af_fmt2str(((af_data_t*)arg)->format,buf1,255), | |
268 af_fmt2str(af->data->format,buf2,255)); | |
269 af->play = play_float_s16; | |
270 } | |
271 if ((data->format == AF_FORMAT_S16_NE) && | |
272 (af->data->format == AF_FORMAT_FLOAT_NE)) | |
273 { | |
274 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %sto %sconversion\n", | |
275 af_fmt2str(((af_data_t*)arg)->format,buf1,255), | |
276 af_fmt2str(af->data->format,buf2,255)); | |
277 af->play = play_s16_float; | |
278 } | |
7568 | 279 return AF_OK; |
8167 | 280 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
281 case AF_CONTROL_COMMAND_LINE:{ |
8607 | 282 int bps = 2; |
283 int format = AF_FORMAT_NE; | |
8167 | 284 char str[256]; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
285 str[0] = '\0'; |
8607 | 286 sscanf((char*)arg,"%i:%s",&bps,str); |
8167 | 287 // Convert string to format |
14245 | 288 format = af_str2fmt(str); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
289 |
8167 | 290 // Automatic correction of errors |
8607 | 291 switch(format & AF_FORMAT_SPECIAL_MASK){ |
8167 | 292 case(AF_FORMAT_A_LAW): |
293 case(AF_FORMAT_MU_LAW): | |
8607 | 294 bps=1; break; |
8167 | 295 case(AF_FORMAT_AC3): |
8607 | 296 bps=4; break; // I think |
8167 | 297 } |
8607 | 298 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK)) |
299 bps=4; | |
14272 | 300 |
301 // set appropriate AF_FORMAT_BITS | |
302 format |= (bps-1)<<3; // hack | |
8607 | 303 |
304 if((AF_OK != af->control(af,AF_CONTROL_FORMAT_BPS | AF_CONTROL_SET,&bps)) || | |
305 (AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))) | |
306 return AF_ERROR; | |
307 return AF_OK; | |
8167 | 308 } |
8607 | 309 case AF_CONTROL_FORMAT_BPS | AF_CONTROL_SET: |
7568 | 310 // Reinit must be called after this function has been called |
311 | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
312 // Check for errors in configuraton |
8607 | 313 if(AF_OK != check_bps(*(int*)arg)) |
7568 | 314 return AF_ERROR; |
8167 | 315 |
8607 | 316 af->data->bps = *(int*)arg; |
317 return AF_OK; | |
318 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET: | |
319 // Reinit must be called after this function has been called | |
320 | |
321 // Check for errors in configuraton | |
322 if(AF_OK != check_format(*(int*)arg)) | |
323 return AF_ERROR; | |
324 | |
325 af->data->format = *(int*)arg; | |
7568 | 326 return AF_OK; |
327 } | |
328 return AF_UNKNOWN; | |
329 } | |
330 | |
331 // Deallocate memory | |
332 static void uninit(struct af_instance_s* af) | |
333 { | |
334 if(af->data) | |
335 free(af->data); | |
12386 | 336 af->setup = 0; |
7568 | 337 } |
338 | |
14272 | 339 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data) |
340 { | |
341 af_data_t* l = af->data; // Local data | |
342 af_data_t* c = data; // Current working data | |
343 int len = c->len/c->bps; // Lenght in samples of current audio block | |
344 | |
345 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
346 return NULL; | |
347 | |
348 endian(c->audio,l->audio,len,c->bps); | |
349 | |
350 c->audio = l->audio; | |
351 c->format = l->format; | |
352 | |
353 return c; | |
354 } | |
355 | |
356 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data) | |
357 { | |
358 af_data_t* l = af->data; // Local data | |
359 af_data_t* c = data; // Current working data | |
360 int len = c->len/4; // Lenght in samples of current audio block | |
361 | |
362 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
363 return NULL; | |
364 | |
365 float2int(c->audio, l->audio, len, 2); | |
366 | |
367 c->audio = l->audio; | |
368 c->len = len*2; | |
369 c->bps = 2; | |
370 c->format = l->format; | |
371 | |
372 return c; | |
373 } | |
374 | |
375 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data) | |
376 { | |
377 af_data_t* l = af->data; // Local data | |
378 af_data_t* c = data; // Current working data | |
379 int len = c->len/2; // Lenght in samples of current audio block | |
380 | |
381 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
382 return NULL; | |
383 | |
384 int2float(c->audio, l->audio, len, 2); | |
385 | |
386 c->audio = l->audio; | |
387 c->len = len*4; | |
388 c->bps = 4; | |
389 c->format = l->format; | |
390 | |
391 return c; | |
392 } | |
393 | |
7568 | 394 // Filter data through filter |
395 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
396 { | |
8167 | 397 af_data_t* l = af->data; // Local data |
398 af_data_t* c = data; // Current working data | |
399 int len = c->len/c->bps; // Lenght in samples of current audio block | |
7568 | 400 |
401 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
402 return NULL; | |
403 | |
8167 | 404 // Change to cpu native endian format |
405 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) | |
406 endian(c->audio,c->audio,len,c->bps); | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
407 |
8167 | 408 // Conversion table |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
409 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) { |
8167 | 410 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
411 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
412 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
413 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
414 si2us(l->audio,l->audio,len,l->bps); | |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
415 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) { |
8167 | 416 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
417 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
418 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
419 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
420 si2us(l->audio,l->audio,len,l->bps); | |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
421 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { |
8167 | 422 switch(l->format&AF_FORMAT_SPECIAL_MASK){ |
423 case(AF_FORMAT_MU_LAW): | |
424 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
425 break; | |
426 case(AF_FORMAT_A_LAW): | |
427 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
7568 | 428 break; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
429 default: |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
430 float2int(c->audio, l->audio, len, l->bps); |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
431 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
432 si2us(l->audio,l->audio,len,l->bps); |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
433 break; |
8167 | 434 } |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
435 } else { |
8167 | 436 // Input must be int |
437 | |
438 // Change signed/unsigned | |
439 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ | |
440 if((c->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
441 us2si(c->audio,c->audio,len,c->bps); | |
442 else | |
443 si2us(c->audio,c->audio,len,c->bps); | |
444 } | |
445 // Convert to special formats | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
446 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){ |
8167 | 447 case(AF_FORMAT_MU_LAW): |
448 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
449 break; | |
450 case(AF_FORMAT_A_LAW): | |
451 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
452 break; | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
453 case(AF_FORMAT_F): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
454 int2float(c->audio, l->audio, len, c->bps); |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
455 break; |
8167 | 456 default: |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
457 // Change the number of bits |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
458 if(c->bps != l->bps) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
459 change_bps(c->audio,l->audio,len,c->bps,l->bps); |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
460 else |
8185 | 461 memcpy(l->audio,c->audio,len*c->bps); |
7568 | 462 break; |
463 } | |
464 } | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
465 |
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
466 // Switch from cpu native endian to the correct endianess |
8167 | 467 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) |
468 endian(l->audio,l->audio,len,l->bps); | |
7568 | 469 |
470 // Set output data | |
471 c->audio = l->audio; | |
8167 | 472 c->len = len*l->bps; |
7568 | 473 c->bps = l->bps; |
474 c->format = l->format; | |
475 return c; | |
476 } | |
477 | |
478 // Allocate memory and set function pointers | |
479 static int open(af_instance_t* af){ | |
480 af->control=control; | |
481 af->uninit=uninit; | |
482 af->play=play; | |
483 af->mul.n=1; | |
484 af->mul.d=1; | |
485 af->data=calloc(1,sizeof(af_data_t)); | |
486 if(af->data == NULL) | |
487 return AF_ERROR; | |
488 return AF_OK; | |
489 } | |
490 | |
491 // Description of this filter | |
492 af_info_t af_info_format = { | |
493 "Sample format conversion", | |
494 "format", | |
495 "Anders", | |
496 "", | |
7615 | 497 AF_FLAGS_REENTRANT, |
7568 | 498 open |
499 }; | |
8167 | 500 |
12478 | 501 static inline uint32_t load24bit(void* data, int pos) { |
502 #if WORDS_BIGENDIAN | |
503 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) | | |
504 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
505 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8); | |
506 #else | |
507 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) | | |
508 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
509 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24); | |
510 #endif | |
511 } | |
512 | |
513 static inline void store24bit(void* data, int pos, uint32_t expanded_value) { | |
514 #if WORDS_BIGENDIAN | |
515 ((uint8_t*)data)[3*pos]=expanded_value>>24; | |
516 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
517 ((uint8_t*)data)[3*pos+2]=expanded_value>>8; | |
518 #else | |
519 ((uint8_t*)data)[3*pos]=expanded_value>>8; | |
520 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
521 ((uint8_t*)data)[3*pos+2]=expanded_value>>24; | |
522 #endif | |
523 } | |
524 | |
8167 | 525 // Function implementations used by play |
526 static void endian(void* in, void* out, int len, int bps) | |
527 { | |
528 register int i; | |
529 switch(bps){ | |
530 case(2):{ | |
531 for(i=0;i<len;i++){ | |
12486 | 532 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]); |
8167 | 533 } |
534 break; | |
535 } | |
12478 | 536 case(3):{ |
537 register uint8_t s; | |
538 for(i=0;i<len;i++){ | |
539 s=((uint8_t*)in)[3*i]; | |
540 ((uint8_t*)out)[3*i]=((uint8_t*)in)[3*i+2]; | |
12481
dbbc25ea6403
fix endian conversion for (curently unused) case where in buffer != out buffer
reimar
parents:
12478
diff
changeset
|
541 if (in != out) |
dbbc25ea6403
fix endian conversion for (curently unused) case where in buffer != out buffer
reimar
parents:
12478
diff
changeset
|
542 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1]; |
12478 | 543 ((uint8_t*)out)[3*i+2]=s; |
544 } | |
545 break; | |
546 } | |
8167 | 547 case(4):{ |
548 for(i=0;i<len;i++){ | |
12486 | 549 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]); |
8167 | 550 } |
551 break; | |
552 } | |
553 } | |
554 } | |
555 | |
556 static void si2us(void* in, void* out, int len, int bps) | |
557 { | |
558 register int i; | |
559 switch(bps){ | |
560 case(1): | |
561 for(i=0;i<len;i++) | |
562 ((uint8_t*)out)[i]=(uint8_t)(SCHAR_MAX+((int)((int8_t*)in)[i])); | |
563 break; | |
564 case(2): | |
565 for(i=0;i<len;i++) | |
566 ((uint16_t*)out)[i]=(uint16_t)(SHRT_MAX+((int)((int16_t*)in)[i])); | |
567 break; | |
12478 | 568 case(3): |
569 for(i=0;i<len;i++) | |
570 store24bit(out, i, (uint32_t)(INT_MAX+(int32_t)load24bit(in, i))); | |
571 break; | |
8167 | 572 case(4): |
573 for(i=0;i<len;i++) | |
574 ((uint32_t*)out)[i]=(uint32_t)(INT_MAX+((int32_t*)in)[i]); | |
575 break; | |
576 } | |
577 } | |
578 | |
579 static void us2si(void* in, void* out, int len, int bps) | |
580 { | |
581 register int i; | |
582 switch(bps){ | |
583 case(1): | |
584 for(i=0;i<len;i++) | |
585 ((int8_t*)out)[i]=(int8_t)(SCHAR_MIN+((int)((uint8_t*)in)[i])); | |
586 break; | |
587 case(2): | |
588 for(i=0;i<len;i++) | |
589 ((int16_t*)out)[i]=(int16_t)(SHRT_MIN+((int)((uint16_t*)in)[i])); | |
590 break; | |
12478 | 591 case(3): |
592 for(i=0;i<len;i++) | |
593 store24bit(out, i, (int32_t)(INT_MIN+(uint32_t)load24bit(in, i))); | |
594 break; | |
8167 | 595 case(4): |
596 for(i=0;i<len;i++) | |
597 ((int32_t*)out)[i]=(int32_t)(INT_MIN+((uint32_t*)in)[i]); | |
598 break; | |
599 } | |
600 } | |
601 | |
602 static void change_bps(void* in, void* out, int len, int inbps, int outbps) | |
603 { | |
604 register int i; | |
605 switch(inbps){ | |
606 case(1): | |
607 switch(outbps){ | |
608 case(2): | |
609 for(i=0;i<len;i++) | |
610 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8; | |
611 break; | |
12478 | 612 case(3): |
613 for(i=0;i<len;i++) | |
614 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24); | |
615 break; | |
8167 | 616 case(4): |
617 for(i=0;i<len;i++) | |
618 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24; | |
619 break; | |
620 } | |
621 break; | |
622 case(2): | |
623 switch(outbps){ | |
624 case(1): | |
625 for(i=0;i<len;i++) | |
626 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8); | |
627 break; | |
12478 | 628 case(3): |
629 for(i=0;i<len;i++) | |
630 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16); | |
631 break; | |
8167 | 632 case(4): |
633 for(i=0;i<len;i++) | |
634 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16; | |
635 break; | |
636 } | |
637 break; | |
12478 | 638 case(3): |
639 switch(outbps){ | |
640 case(1): | |
641 for(i=0;i<len;i++) | |
642 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24); | |
643 break; | |
644 case(2): | |
645 for(i=0;i<len;i++) | |
646 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16); | |
647 break; | |
648 case(4): | |
649 for(i=0;i<len;i++) | |
650 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i); | |
651 break; | |
652 } | |
653 break; | |
8167 | 654 case(4): |
655 switch(outbps){ | |
656 case(1): | |
657 for(i=0;i<len;i++) | |
658 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24); | |
659 break; | |
660 case(2): | |
661 for(i=0;i<len;i++) | |
662 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16); | |
663 break; | |
12478 | 664 case(3): |
665 for(i=0;i<len;i++) | |
666 store24bit(out, i, ((uint32_t*)in)[i]); | |
667 break; | |
8167 | 668 } |
669 break; | |
670 } | |
671 } | |
672 | |
673 static void float2int(void* in, void* out, int len, int bps) | |
674 { | |
675 register int i; | |
676 switch(bps){ | |
677 case(1): | |
678 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
679 ((int8_t*)out)[i]=(int8_t)lrintf(SCHAR_MAX*((float*)in)[i]); |
8167 | 680 break; |
681 case(2): | |
682 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
683 ((int16_t*)out)[i]=(int16_t)lrintf(SHRT_MAX*((float*)in)[i]); |
8167 | 684 break; |
12478 | 685 case(3): |
686 for(i=0;i<len;i++) | |
687 store24bit(out, i, (int32_t)lrintf(INT_MAX*((float*)in)[i])); | |
688 break; | |
8167 | 689 case(4): |
690 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
691 ((int32_t*)out)[i]=(int32_t)lrintf(INT_MAX*((float*)in)[i]); |
8167 | 692 break; |
693 } | |
694 } | |
695 | |
696 static void int2float(void* in, void* out, int len, int bps) | |
697 { | |
698 register int i; | |
699 switch(bps){ | |
700 case(1): | |
701 for(i=0;i<len;i++) | |
702 ((float*)out)[i]=(1.0/SCHAR_MAX)*((float)((int8_t*)in)[i]); | |
703 break; | |
704 case(2): | |
705 for(i=0;i<len;i++) | |
706 ((float*)out)[i]=(1.0/SHRT_MAX)*((float)((int16_t*)in)[i]); | |
707 break; | |
12478 | 708 case(3): |
709 for(i=0;i<len;i++) | |
710 ((float*)out)[i]=(1.0/INT_MAX)*((float)((int32_t)load24bit(in, i))); | |
711 break; | |
8167 | 712 case(4): |
713 for(i=0;i<len;i++) | |
714 ((float*)out)[i]=(1.0/INT_MAX)*((float)((int32_t*)in)[i]); | |
715 break; | |
716 } | |
717 } |