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