Mercurial > mplayer.hg
annotate libaf/af_format.c @ 8223:a88e53ef3029
exporting fs_seg
author | alex |
---|---|
date | Sun, 17 Nov 2002 17:41:19 +0000 |
parents | aae2d081864a |
children | d6f40a06867b |
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" | |
14 | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
15 // Integer to float conversion through lrintf() |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
16 #ifdef HAVE_LRINTF |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
17 #define __USE_ISOC99 1 |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
18 #include <math.h> |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
19 #else |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
20 #define lrintf(x) ((int)(x)) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
21 #endif |
8167 | 22 |
23 /* Functions used by play to convert the input audio to the correct | |
24 format */ | |
7568 | 25 |
8167 | 26 /* The below includes retrives functions for converting to and from |
27 ulaw and alaw */ | |
28 #include "af_format_ulaw.c" | |
29 #include "af_format_alaw.c" | |
7568 | 30 |
8167 | 31 // Switch endianess |
32 static void endian(void* in, void* out, int len, int bps); | |
33 // From singed to unsigned | |
34 static void si2us(void* in, void* out, int len, int bps); | |
35 // From unsinged to signed | |
36 static void us2si(void* in, void* out, int len, int bps); | |
37 // Change the number of bits per sample | |
38 static void change_bps(void* in, void* out, int len, int inbps, int outbps); | |
39 // From float to int signed | |
40 static void float2int(void* in, void* out, int len, int bps); | |
41 // From signed int to float | |
42 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
|
43 |
8167 | 44 // Convert from string to format |
45 static int str2fmt(char* str) | |
7568 | 46 { |
8167 | 47 int format=0; |
48 // Scan for endianess | |
49 if(strstr(str,"be") || strstr(str,"BE")) | |
50 format |= AF_FORMAT_BE; | |
51 else if(strstr(str,"le") || strstr(str,"LE")) | |
52 format |= AF_FORMAT_LE; | |
53 else | |
54 format |= AF_FORMAT_NE; | |
55 | |
56 // Scan for special formats | |
57 if(strstr(str,"mulaw") || strstr(str,"MULAW")){ | |
58 format |= AF_FORMAT_MU_LAW; return format; | |
59 } | |
60 if(strstr(str,"alaw") || strstr(str,"ALAW")){ | |
61 format |= AF_FORMAT_A_LAW; return format; | |
62 } | |
63 if(strstr(str,"ac3") || strstr(str,"AC3")){ | |
64 format |= AF_FORMAT_AC3; return format; | |
65 } | |
66 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){ | |
67 format |= AF_FORMAT_MPEG2; return format; | |
68 } | |
69 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){ | |
70 format |= AF_FORMAT_IMA_ADPCM; return format; | |
71 } | |
72 | |
73 // Scan for int/float | |
74 if(strstr(str,"float") || strstr(str,"FLOAT")){ | |
75 format |= AF_FORMAT_F; return format; | |
7568 | 76 } |
8167 | 77 else |
78 format |= AF_FORMAT_I; | |
7568 | 79 |
8167 | 80 // Scan for signed/unsigned |
81 if(strstr(str,"unsigned") || strstr(str,"UNSIGNED")) | |
82 format |= AF_FORMAT_US; | |
83 else | |
84 format |= AF_FORMAT_SI; | |
85 | |
86 return format; | |
87 } | |
88 | |
89 /* Convert format to str input str is a buffer for the | |
90 converted string, size is the size of the buffer */ | |
91 static char* fmt2str(int format, char* str, size_t size) | |
92 { | |
93 int i=0; | |
94 // Print endinaness | |
95 if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK)) | |
96 i+=snprintf(str,size,"little endian "); | |
97 else | |
98 i+=snprintf(str,size,"big endian "); | |
99 | |
100 if(format & AF_FORMAT_SPECIAL_MASK){ | |
101 switch(format & AF_FORMAT_SPECIAL_MASK){ | |
102 case(AF_FORMAT_MU_LAW): | |
103 i+=snprintf(&str[i],size-i,"mu law "); break; | |
104 case(AF_FORMAT_A_LAW): | |
105 i+=snprintf(&str[i],size-i,"A law "); break; | |
106 case(AF_FORMAT_MPEG2): | |
107 i+=snprintf(&str[i],size-i,"MPEG 2 "); break; | |
108 case(AF_FORMAT_AC3): | |
109 i+=snprintf(&str[i],size-i,"AC3 "); break; | |
110 } | |
111 } | |
112 else{ | |
113 // Point | |
114 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK)) | |
115 i+=snprintf(&str[i],size,"float "); | |
116 else{ | |
117 // Sign | |
118 if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK)) | |
119 i+=snprintf(&str[i],size-i,"unsigned "); | |
120 else | |
121 i+=snprintf(&str[i],size-i,"signed "); | |
122 | |
123 i+=snprintf(&str[i],size,"int "); | |
124 } | |
125 } | |
126 return str; | |
7568 | 127 } |
128 | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
129 // Helper function to check sanity for input arguments |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
130 int check_sanity(af_data_t* data) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
131 { |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
132 char buf[256]; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
133 // Sanity check for bytes per sample |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
134 if(data->bps != 4 && data->bps != 2 && data->bps != 1){ |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
135 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample must be 1, 2 or 4. Current value is %i \n",data->bps); |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
136 return AF_ERROR; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
137 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
138 |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
139 // Check for unsupported formats |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
140 switch(data->format & AF_FORMAT_SPECIAL_MASK){ |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
141 case(AF_FORMAT_MPEG2): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
142 case(AF_FORMAT_AC3): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
143 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n", |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
144 fmt2str(data->format,buf,255)); |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
145 return AF_ERROR; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
146 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
147 return AF_OK; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
148 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
149 |
7568 | 150 // Initialization and runtime control |
151 static int control(struct af_instance_s* af, int cmd, void* arg) | |
152 { | |
153 switch(cmd){ | |
8167 | 154 case AF_CONTROL_REINIT:{ |
155 char buf1[256]; | |
156 char buf2[256]; | |
7568 | 157 // Make sure this filter isn't redundant |
8167 | 158 if(af->data->format == ((af_data_t*)arg)->format && |
159 af->data->bps == ((af_data_t*)arg)->bps) | |
7568 | 160 return AF_DETACH; |
8167 | 161 |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
162 // Check for errors in configuraton |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
163 if(AF_OK != check_sanity((af_data_t*)arg) || |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
164 AF_OK != check_sanity(af->data)) |
8167 | 165 return AF_ERROR; |
166 | |
167 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
|
168 ((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
|
169 af->data->bps*8,fmt2str(af->data->format,buf2,255)); |
7568 | 170 |
171 af->data->rate = ((af_data_t*)arg)->rate; | |
172 af->data->nch = ((af_data_t*)arg)->nch; | |
173 af->mul.n = af->data->bps; | |
174 af->mul.d = ((af_data_t*)arg)->bps; | |
175 return AF_OK; | |
8167 | 176 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
177 case AF_CONTROL_COMMAND_LINE:{ |
8167 | 178 af_data_t d={NULL,0,0,0,0,2}; |
179 char str[256]; | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
180 str[0] = '\0'; |
8167 | 181 sscanf((char*)arg,"%i:%s",&(d.bps),str); |
182 // Convert string to format | |
183 d.format = str2fmt(str); | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
184 |
8167 | 185 // Automatic correction of errors |
186 switch(d.format & AF_FORMAT_SPECIAL_MASK){ | |
187 case(AF_FORMAT_A_LAW): | |
188 case(AF_FORMAT_MU_LAW): | |
189 d.bps=1; break; | |
190 case(AF_FORMAT_AC3): | |
191 d.bps=4; break; // I think | |
192 } | |
193 if(AF_FORMAT_F == (d.format & AF_FORMAT_POINT_MASK)) | |
194 d.bps=4; | |
195 | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
196 return af->control(af,AF_CONTROL_FORMAT,&d); |
8167 | 197 } |
7568 | 198 case AF_CONTROL_FORMAT: |
199 // Reinit must be called after this function has been called | |
200 | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
201 // Check for errors in configuraton |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
202 if(AF_OK != check_sanity((af_data_t*)arg)) |
7568 | 203 return AF_ERROR; |
8167 | 204 |
205 af->data->format = ((af_data_t*)arg)->format; | |
7568 | 206 af->data->bps=((af_data_t*)arg)->bps; |
207 return AF_OK; | |
208 } | |
209 return AF_UNKNOWN; | |
210 } | |
211 | |
212 // Deallocate memory | |
213 static void uninit(struct af_instance_s* af) | |
214 { | |
215 if(af->data) | |
216 free(af->data); | |
217 (int)af->setup = 0; | |
218 } | |
219 | |
220 // Filter data through filter | |
221 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
222 { | |
8167 | 223 af_data_t* l = af->data; // Local data |
224 af_data_t* c = data; // Current working data | |
225 int len = c->len/c->bps; // Lenght in samples of current audio block | |
7568 | 226 |
227 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
228 return NULL; | |
229 | |
8167 | 230 // Change to cpu native endian format |
231 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) | |
232 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
|
233 |
8167 | 234 // Conversion table |
235 switch(c->format & ~AF_FORMAT_END_MASK){ | |
236 case(AF_FORMAT_MU_LAW): | |
237 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); | |
238 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
239 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
240 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
241 si2us(l->audio,l->audio,len,l->bps); | |
242 break; | |
243 case(AF_FORMAT_A_LAW): | |
244 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); | |
245 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
246 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
247 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
248 si2us(l->audio,l->audio,len,l->bps); | |
249 break; | |
250 case(AF_FORMAT_F): | |
251 switch(l->format&AF_FORMAT_SPECIAL_MASK){ | |
252 case(AF_FORMAT_MU_LAW): | |
253 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
254 break; | |
255 case(AF_FORMAT_A_LAW): | |
256 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
7568 | 257 break; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
258 default: |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 break; |
8167 | 263 } |
264 break; | |
265 default: | |
266 // Input must be int | |
267 | |
268 // Change signed/unsigned | |
269 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ | |
270 if((c->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
271 us2si(c->audio,c->audio,len,c->bps); | |
272 else | |
273 si2us(c->audio,c->audio,len,c->bps); | |
274 } | |
275 // Convert to special formats | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
276 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){ |
8167 | 277 case(AF_FORMAT_MU_LAW): |
278 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
279 break; | |
280 case(AF_FORMAT_A_LAW): | |
281 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
282 break; | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
283 case(AF_FORMAT_F): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
284 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
|
285 break; |
8167 | 286 default: |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
287 // Change the number of bits |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
288 if(c->bps != l->bps) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
289 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
|
290 else |
8185 | 291 memcpy(l->audio,c->audio,len*c->bps); |
7568 | 292 break; |
293 } | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
294 break; |
7568 | 295 } |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
296 |
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
297 // Switch from cpu native endian to the correct endianess |
8167 | 298 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) |
299 endian(l->audio,l->audio,len,l->bps); | |
7568 | 300 |
301 // Set output data | |
302 c->audio = l->audio; | |
8167 | 303 c->len = len*l->bps; |
7568 | 304 c->bps = l->bps; |
305 c->format = l->format; | |
306 return c; | |
307 } | |
308 | |
309 // Allocate memory and set function pointers | |
310 static int open(af_instance_t* af){ | |
311 af->control=control; | |
312 af->uninit=uninit; | |
313 af->play=play; | |
314 af->mul.n=1; | |
315 af->mul.d=1; | |
316 af->data=calloc(1,sizeof(af_data_t)); | |
317 if(af->data == NULL) | |
318 return AF_ERROR; | |
319 return AF_OK; | |
320 } | |
321 | |
322 // Description of this filter | |
323 af_info_t af_info_format = { | |
324 "Sample format conversion", | |
325 "format", | |
326 "Anders", | |
327 "", | |
7615 | 328 AF_FLAGS_REENTRANT, |
7568 | 329 open |
330 }; | |
8167 | 331 |
332 // Function implementations used by play | |
333 static void endian(void* in, void* out, int len, int bps) | |
334 { | |
335 register int i; | |
336 switch(bps){ | |
337 case(2):{ | |
338 register uint16_t s; | |
339 for(i=0;i<len;i++){ | |
340 s=((uint16_t*)in)[i]; | |
341 ((uint16_t*)out)[i]=(uint16_t)(((s&0x00FF)<<8) | (s&0xFF00)>>8); | |
342 } | |
343 break; | |
344 } | |
345 case(4):{ | |
346 register uint32_t s; | |
347 for(i=0;i<len;i++){ | |
348 s=((uint32_t*)in)[i]; | |
349 ((uint32_t*)out)[i]=(uint32_t)(((s&0x000000FF)<<24) | | |
350 ((s&0x0000FF00)<<8) | | |
351 ((s&0x00FF0000)>>8) | | |
352 ((s&0xFF000000)>>24)); | |
353 } | |
354 break; | |
355 } | |
356 } | |
357 } | |
358 | |
359 static void si2us(void* in, void* out, int len, int bps) | |
360 { | |
361 register int i; | |
362 switch(bps){ | |
363 case(1): | |
364 for(i=0;i<len;i++) | |
365 ((uint8_t*)out)[i]=(uint8_t)(SCHAR_MAX+((int)((int8_t*)in)[i])); | |
366 break; | |
367 case(2): | |
368 for(i=0;i<len;i++) | |
369 ((uint16_t*)out)[i]=(uint16_t)(SHRT_MAX+((int)((int16_t*)in)[i])); | |
370 break; | |
371 case(4): | |
372 for(i=0;i<len;i++) | |
373 ((uint32_t*)out)[i]=(uint32_t)(INT_MAX+((int32_t*)in)[i]); | |
374 break; | |
375 } | |
376 } | |
377 | |
378 static void us2si(void* in, void* out, int len, int bps) | |
379 { | |
380 register int i; | |
381 switch(bps){ | |
382 case(1): | |
383 for(i=0;i<len;i++) | |
384 ((int8_t*)out)[i]=(int8_t)(SCHAR_MIN+((int)((uint8_t*)in)[i])); | |
385 break; | |
386 case(2): | |
387 for(i=0;i<len;i++) | |
388 ((int16_t*)out)[i]=(int16_t)(SHRT_MIN+((int)((uint16_t*)in)[i])); | |
389 break; | |
390 case(4): | |
391 for(i=0;i<len;i++) | |
392 ((int32_t*)out)[i]=(int32_t)(INT_MIN+((uint32_t*)in)[i]); | |
393 break; | |
394 } | |
395 } | |
396 | |
397 static void change_bps(void* in, void* out, int len, int inbps, int outbps) | |
398 { | |
399 register int i; | |
400 switch(inbps){ | |
401 case(1): | |
402 switch(outbps){ | |
403 case(2): | |
404 for(i=0;i<len;i++) | |
405 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8; | |
406 break; | |
407 case(4): | |
408 for(i=0;i<len;i++) | |
409 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24; | |
410 break; | |
411 } | |
412 break; | |
413 case(2): | |
414 switch(outbps){ | |
415 case(1): | |
416 for(i=0;i<len;i++) | |
417 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8); | |
418 break; | |
419 case(4): | |
420 for(i=0;i<len;i++) | |
421 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16; | |
422 break; | |
423 } | |
424 break; | |
425 case(4): | |
426 switch(outbps){ | |
427 case(1): | |
428 for(i=0;i<len;i++) | |
429 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24); | |
430 break; | |
431 case(2): | |
432 for(i=0;i<len;i++) | |
433 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16); | |
434 break; | |
435 } | |
436 break; | |
437 } | |
438 } | |
439 | |
440 static void float2int(void* in, void* out, int len, int bps) | |
441 { | |
442 register int i; | |
443 switch(bps){ | |
444 case(1): | |
445 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
446 ((int8_t*)out)[i]=(int8_t)lrintf(SCHAR_MAX*((float*)in)[i]); |
8167 | 447 break; |
448 case(2): | |
449 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
450 ((int16_t*)out)[i]=(int16_t)lrintf(SHRT_MAX*((float*)in)[i]); |
8167 | 451 break; |
452 case(4): | |
453 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
454 ((int32_t*)out)[i]=(int32_t)lrintf(INT_MAX*((float*)in)[i]); |
8167 | 455 break; |
456 } | |
457 } | |
458 | |
459 static void int2float(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 ((float*)out)[i]=(1.0/SCHAR_MAX)*((float)((int8_t*)in)[i]); | |
466 break; | |
467 case(2): | |
468 for(i=0;i<len;i++) | |
469 ((float*)out)[i]=(1.0/SHRT_MAX)*((float)((int16_t*)in)[i]); | |
470 break; | |
471 case(4): | |
472 for(i=0;i<len;i++) | |
473 ((float*)out)[i]=(1.0/INT_MAX)*((float)((int32_t*)in)[i]); | |
474 break; | |
475 } | |
476 } |