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