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