Mercurial > mplayer.hg
annotate libaf/af_format.c @ 14748:85dc314b3d80
move the format related stuff to format.c
author | alex |
---|---|
date | Mon, 21 Feb 2005 16:41:15 +0000 |
parents | 51a7560a92b7 |
children | 94456deb0624 |
rev | line source |
---|---|
7568 | 1 /* This audio output filter changes the format of a data block. Valid |
2 formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE | |
3 AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <unistd.h> | |
10 #include <inttypes.h> | |
11 #include <limits.h> | |
12 | |
13 #include "af.h" | |
12486 | 14 #include "../bswap.h" |
14272 | 15 #include "../libvo/fastmemcpy.h" |
7568 | 16 |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
17 // Integer to float conversion through lrintf() |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
18 #ifdef HAVE_LRINTF |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
19 #define __USE_ISOC99 1 |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
20 #include <math.h> |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
21 #else |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
22 #define lrintf(x) ((int)(x)) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
23 #endif |
8167 | 24 |
25 /* Functions used by play to convert the input audio to the correct | |
26 format */ | |
7568 | 27 |
8167 | 28 /* The below includes retrives functions for converting to and from |
29 ulaw and alaw */ | |
30 #include "af_format_ulaw.c" | |
31 #include "af_format_alaw.c" | |
7568 | 32 |
8167 | 33 // Switch endianess |
34 static void endian(void* in, void* out, int len, int bps); | |
35 // From singed to unsigned | |
36 static void si2us(void* in, void* out, int len, int bps); | |
37 // From unsinged to signed | |
38 static void us2si(void* in, void* out, int len, int bps); | |
39 // Change the number of bits per sample | |
40 static void change_bps(void* in, void* out, int len, int inbps, int outbps); | |
41 // From float to int signed | |
42 static void float2int(void* in, void* out, int len, int bps); | |
43 // From signed int to float | |
44 static void int2float(void* in, void* out, int len, int bps); | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
45 |
14272 | 46 static af_data_t* play(struct af_instance_s* af, af_data_t* data); |
47 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data); | |
48 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data); | |
49 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data); | |
50 | |
8607 | 51 // Helper functions to check sanity for input arguments |
52 | |
53 // 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
|
54 static int check_bps(int bps) |
8607 | 55 { |
12478 | 56 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ |
8607 | 57 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample" |
12478 | 58 " must be 1, 2, 3 or 4. Current value is %i \n",bps); |
8607 | 59 return AF_ERROR; |
60 } | |
61 return AF_OK; | |
62 } | |
63 | |
64 // Check for unsupported formats | |
13989
dcb6b4a33aaa
declare check_format and check_bps static, they are used nowhere else.
reimar
parents:
12486
diff
changeset
|
65 static int check_format(int format) |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
66 { |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
67 char buf[256]; |
8607 | 68 switch(format & AF_FORMAT_SPECIAL_MASK){ |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
69 case(AF_FORMAT_IMA_ADPCM): |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
70 case(AF_FORMAT_MPEG2): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
71 case(AF_FORMAT_AC3): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
72 af_msg(AF_MSG_ERROR,"[format] Sample format %s not yet supported \n", |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
73 af_fmt2str(format,buf,256)); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
74 return AF_ERROR; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
75 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
76 return AF_OK; |
8607 | 77 } |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
78 |
7568 | 79 // Initialization and runtime control |
80 static int control(struct af_instance_s* af, int cmd, void* arg) | |
81 { | |
82 switch(cmd){ | |
8167 | 83 case AF_CONTROL_REINIT:{ |
84 char buf1[256]; | |
85 char buf2[256]; | |
14272 | 86 af_data_t *data = arg; |
87 | |
7568 | 88 // Make sure this filter isn't redundant |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
89 if(af->data->format == data->format && |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
90 af->data->bps == data->bps) |
7568 | 91 return AF_DETACH; |
8607 | 92 |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
93 // Check for errors in configuraton |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
94 if((AF_OK != check_bps(data->bps)) || |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
95 (AF_OK != check_format(data->format)) || |
8607 | 96 (AF_OK != check_bps(af->data->bps)) || |
97 (AF_OK != check_format(af->data->format))) | |
8167 | 98 return AF_ERROR; |
99 | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
100 af_msg(AF_MSG_VERBOSE,"[format] Changing sample format from %s to %s\n", |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
101 af_fmt2str(data->format,buf1,256), |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
102 af_fmt2str(af->data->format,buf2,256)); |
7568 | 103 |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
104 af->data->rate = data->rate; |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
105 af->data->nch = data->nch; |
7568 | 106 af->mul.n = af->data->bps; |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
107 af->mul.d = data->bps; |
14433
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14399
diff
changeset
|
108 af_frac_cancel(&af->mul); |
14272 | 109 |
110 af->play = play; // set default | |
111 | |
112 // look whether only endianess differences are there | |
113 if ((af->data->format & ~AF_FORMAT_END_MASK) == | |
114 (data->format & ~AF_FORMAT_END_MASK)) | |
115 { | |
116 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianess conversion only\n"); | |
117 af->play = play_swapendian; | |
118 } | |
119 if ((data->format == AF_FORMAT_FLOAT_NE) && | |
120 (af->data->format == AF_FORMAT_S16_NE)) | |
121 { | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
122 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n", |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
123 af_fmt2str(data->format,buf1,256), |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
124 af_fmt2str(af->data->format,buf2,256)); |
14272 | 125 af->play = play_float_s16; |
126 } | |
127 if ((data->format == AF_FORMAT_S16_NE) && | |
128 (af->data->format == AF_FORMAT_FLOAT_NE)) | |
129 { | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
130 af_msg(AF_MSG_VERBOSE,"[format] Accelerated %s to %s conversion\n", |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
131 af_fmt2str(data->format,buf1,256), |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
132 af_fmt2str(af->data->format,buf2,256)); |
14272 | 133 af->play = play_s16_float; |
134 } | |
7568 | 135 return AF_OK; |
8167 | 136 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
137 case AF_CONTROL_COMMAND_LINE:{ |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
138 int format = af_str2fmt_short(arg); |
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
139 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) |
8607 | 140 return AF_ERROR; |
141 return AF_OK; | |
8167 | 142 } |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
143 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ |
8607 | 144 // Check for errors in configuraton |
145 if(AF_OK != check_format(*(int*)arg)) | |
146 return AF_ERROR; | |
147 | |
148 af->data->format = *(int*)arg; | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
149 af->data->bps = af_fmt2bits(af->data->format)/8; |
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
150 |
7568 | 151 return AF_OK; |
152 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
153 } |
7568 | 154 return AF_UNKNOWN; |
155 } | |
156 | |
157 // Deallocate memory | |
158 static void uninit(struct af_instance_s* af) | |
159 { | |
160 if(af->data) | |
161 free(af->data); | |
12386 | 162 af->setup = 0; |
7568 | 163 } |
164 | |
14272 | 165 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data) |
166 { | |
167 af_data_t* l = af->data; // Local data | |
168 af_data_t* c = data; // Current working data | |
169 int len = c->len/c->bps; // Lenght in samples of current audio block | |
170 | |
171 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
172 return NULL; | |
173 | |
174 endian(c->audio,l->audio,len,c->bps); | |
175 | |
176 c->audio = l->audio; | |
177 c->format = l->format; | |
178 | |
179 return c; | |
180 } | |
181 | |
182 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data) | |
183 { | |
184 af_data_t* l = af->data; // Local data | |
185 af_data_t* c = data; // Current working data | |
186 int len = c->len/4; // Lenght in samples of current audio block | |
187 | |
188 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
189 return NULL; | |
190 | |
191 float2int(c->audio, l->audio, len, 2); | |
192 | |
193 c->audio = l->audio; | |
194 c->len = len*2; | |
195 c->bps = 2; | |
196 c->format = l->format; | |
197 | |
198 return c; | |
199 } | |
200 | |
201 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data) | |
202 { | |
203 af_data_t* l = af->data; // Local data | |
204 af_data_t* c = data; // Current working data | |
205 int len = c->len/2; // Lenght in samples of current audio block | |
206 | |
207 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
208 return NULL; | |
209 | |
210 int2float(c->audio, l->audio, len, 2); | |
211 | |
212 c->audio = l->audio; | |
213 c->len = len*4; | |
214 c->bps = 4; | |
215 c->format = l->format; | |
216 | |
217 return c; | |
218 } | |
219 | |
7568 | 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 |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
235 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) { |
8167 | 236 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
237 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
238 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
239 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
240 si2us(l->audio,l->audio,len,l->bps); | |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
241 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) { |
8167 | 242 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
243 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
244 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
245 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
246 si2us(l->audio,l->audio,len,l->bps); | |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
247 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { |
8167 | 248 switch(l->format&AF_FORMAT_SPECIAL_MASK){ |
249 case(AF_FORMAT_MU_LAW): | |
250 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
251 break; | |
252 case(AF_FORMAT_A_LAW): | |
253 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
7568 | 254 break; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
255 default: |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
256 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
|
257 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
|
258 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
|
259 break; |
8167 | 260 } |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
261 } else { |
8167 | 262 // Input must be int |
263 | |
264 // Change signed/unsigned | |
265 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ | |
266 if((c->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
267 us2si(c->audio,c->audio,len,c->bps); | |
268 else | |
269 si2us(c->audio,c->audio,len,c->bps); | |
270 } | |
271 // Convert to special formats | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
272 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){ |
8167 | 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); | |
278 break; | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
279 case(AF_FORMAT_F): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
280 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
|
281 break; |
8167 | 282 default: |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
283 // Change the number of bits |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
284 if(c->bps != l->bps) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
285 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
|
286 else |
8185 | 287 memcpy(l->audio,c->audio,len*c->bps); |
7568 | 288 break; |
289 } | |
290 } | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
291 |
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
292 // Switch from cpu native endian to the correct endianess |
8167 | 293 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) |
294 endian(l->audio,l->audio,len,l->bps); | |
7568 | 295 |
296 // Set output data | |
297 c->audio = l->audio; | |
8167 | 298 c->len = len*l->bps; |
7568 | 299 c->bps = l->bps; |
300 c->format = l->format; | |
301 return c; | |
302 } | |
303 | |
304 // Allocate memory and set function pointers | |
305 static int open(af_instance_t* af){ | |
306 af->control=control; | |
307 af->uninit=uninit; | |
308 af->play=play; | |
309 af->mul.n=1; | |
310 af->mul.d=1; | |
311 af->data=calloc(1,sizeof(af_data_t)); | |
312 if(af->data == NULL) | |
313 return AF_ERROR; | |
314 return AF_OK; | |
315 } | |
316 | |
317 // Description of this filter | |
318 af_info_t af_info_format = { | |
319 "Sample format conversion", | |
320 "format", | |
321 "Anders", | |
322 "", | |
7615 | 323 AF_FLAGS_REENTRANT, |
7568 | 324 open |
325 }; | |
8167 | 326 |
12478 | 327 static inline uint32_t load24bit(void* data, int pos) { |
328 #if WORDS_BIGENDIAN | |
329 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) | | |
330 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
331 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8); | |
332 #else | |
333 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) | | |
334 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
335 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24); | |
336 #endif | |
337 } | |
338 | |
339 static inline void store24bit(void* data, int pos, uint32_t expanded_value) { | |
340 #if WORDS_BIGENDIAN | |
341 ((uint8_t*)data)[3*pos]=expanded_value>>24; | |
342 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
343 ((uint8_t*)data)[3*pos+2]=expanded_value>>8; | |
344 #else | |
345 ((uint8_t*)data)[3*pos]=expanded_value>>8; | |
346 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
347 ((uint8_t*)data)[3*pos+2]=expanded_value>>24; | |
348 #endif | |
349 } | |
350 | |
8167 | 351 // Function implementations used by play |
352 static void endian(void* in, void* out, int len, int bps) | |
353 { | |
354 register int i; | |
355 switch(bps){ | |
356 case(2):{ | |
357 for(i=0;i<len;i++){ | |
12486 | 358 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]); |
8167 | 359 } |
360 break; | |
361 } | |
12478 | 362 case(3):{ |
363 register uint8_t s; | |
364 for(i=0;i<len;i++){ | |
365 s=((uint8_t*)in)[3*i]; | |
366 ((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
|
367 if (in != out) |
dbbc25ea6403
fix endian conversion for (curently unused) case where in buffer != out buffer
reimar
parents:
12478
diff
changeset
|
368 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1]; |
12478 | 369 ((uint8_t*)out)[3*i+2]=s; |
370 } | |
371 break; | |
372 } | |
8167 | 373 case(4):{ |
374 for(i=0;i<len;i++){ | |
12486 | 375 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]); |
8167 | 376 } |
377 break; | |
378 } | |
379 } | |
380 } | |
381 | |
382 static void si2us(void* in, void* out, int len, int bps) | |
383 { | |
384 register int i; | |
385 switch(bps){ | |
386 case(1): | |
387 for(i=0;i<len;i++) | |
388 ((uint8_t*)out)[i]=(uint8_t)(SCHAR_MAX+((int)((int8_t*)in)[i])); | |
389 break; | |
390 case(2): | |
391 for(i=0;i<len;i++) | |
392 ((uint16_t*)out)[i]=(uint16_t)(SHRT_MAX+((int)((int16_t*)in)[i])); | |
393 break; | |
12478 | 394 case(3): |
395 for(i=0;i<len;i++) | |
396 store24bit(out, i, (uint32_t)(INT_MAX+(int32_t)load24bit(in, i))); | |
397 break; | |
8167 | 398 case(4): |
399 for(i=0;i<len;i++) | |
400 ((uint32_t*)out)[i]=(uint32_t)(INT_MAX+((int32_t*)in)[i]); | |
401 break; | |
402 } | |
403 } | |
404 | |
405 static void us2si(void* in, void* out, int len, int bps) | |
406 { | |
407 register int i; | |
408 switch(bps){ | |
409 case(1): | |
410 for(i=0;i<len;i++) | |
411 ((int8_t*)out)[i]=(int8_t)(SCHAR_MIN+((int)((uint8_t*)in)[i])); | |
412 break; | |
413 case(2): | |
414 for(i=0;i<len;i++) | |
415 ((int16_t*)out)[i]=(int16_t)(SHRT_MIN+((int)((uint16_t*)in)[i])); | |
416 break; | |
12478 | 417 case(3): |
418 for(i=0;i<len;i++) | |
419 store24bit(out, i, (int32_t)(INT_MIN+(uint32_t)load24bit(in, i))); | |
420 break; | |
8167 | 421 case(4): |
422 for(i=0;i<len;i++) | |
423 ((int32_t*)out)[i]=(int32_t)(INT_MIN+((uint32_t*)in)[i]); | |
424 break; | |
425 } | |
426 } | |
427 | |
428 static void change_bps(void* in, void* out, int len, int inbps, int outbps) | |
429 { | |
430 register int i; | |
431 switch(inbps){ | |
432 case(1): | |
433 switch(outbps){ | |
434 case(2): | |
435 for(i=0;i<len;i++) | |
436 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8; | |
437 break; | |
12478 | 438 case(3): |
439 for(i=0;i<len;i++) | |
440 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24); | |
441 break; | |
8167 | 442 case(4): |
443 for(i=0;i<len;i++) | |
444 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24; | |
445 break; | |
446 } | |
447 break; | |
448 case(2): | |
449 switch(outbps){ | |
450 case(1): | |
451 for(i=0;i<len;i++) | |
452 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8); | |
453 break; | |
12478 | 454 case(3): |
455 for(i=0;i<len;i++) | |
456 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16); | |
457 break; | |
8167 | 458 case(4): |
459 for(i=0;i<len;i++) | |
460 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16; | |
461 break; | |
462 } | |
463 break; | |
12478 | 464 case(3): |
465 switch(outbps){ | |
466 case(1): | |
467 for(i=0;i<len;i++) | |
468 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24); | |
469 break; | |
470 case(2): | |
471 for(i=0;i<len;i++) | |
472 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16); | |
473 break; | |
474 case(4): | |
475 for(i=0;i<len;i++) | |
476 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i); | |
477 break; | |
478 } | |
479 break; | |
8167 | 480 case(4): |
481 switch(outbps){ | |
482 case(1): | |
483 for(i=0;i<len;i++) | |
484 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24); | |
485 break; | |
486 case(2): | |
487 for(i=0;i<len;i++) | |
488 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16); | |
489 break; | |
12478 | 490 case(3): |
491 for(i=0;i<len;i++) | |
492 store24bit(out, i, ((uint32_t*)in)[i]); | |
493 break; | |
8167 | 494 } |
495 break; | |
496 } | |
497 } | |
498 | |
499 static void float2int(void* in, void* out, int len, int bps) | |
500 { | |
501 register int i; | |
502 switch(bps){ | |
503 case(1): | |
504 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
505 ((int8_t*)out)[i]=(int8_t)lrintf(SCHAR_MAX*((float*)in)[i]); |
8167 | 506 break; |
507 case(2): | |
508 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
509 ((int16_t*)out)[i]=(int16_t)lrintf(SHRT_MAX*((float*)in)[i]); |
8167 | 510 break; |
12478 | 511 case(3): |
512 for(i=0;i<len;i++) | |
513 store24bit(out, i, (int32_t)lrintf(INT_MAX*((float*)in)[i])); | |
514 break; | |
8167 | 515 case(4): |
516 for(i=0;i<len;i++) | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
517 ((int32_t*)out)[i]=(int32_t)lrintf(INT_MAX*((float*)in)[i]); |
8167 | 518 break; |
519 } | |
520 } | |
521 | |
522 static void int2float(void* in, void* out, int len, int bps) | |
523 { | |
524 register int i; | |
525 switch(bps){ | |
526 case(1): | |
527 for(i=0;i<len;i++) | |
528 ((float*)out)[i]=(1.0/SCHAR_MAX)*((float)((int8_t*)in)[i]); | |
529 break; | |
530 case(2): | |
531 for(i=0;i<len;i++) | |
532 ((float*)out)[i]=(1.0/SHRT_MAX)*((float)((int16_t*)in)[i]); | |
533 break; | |
12478 | 534 case(3): |
535 for(i=0;i<len;i++) | |
536 ((float*)out)[i]=(1.0/INT_MAX)*((float)((int32_t)load24bit(in, i))); | |
537 break; | |
8167 | 538 case(4): |
539 for(i=0;i<len;i++) | |
540 ((float*)out)[i]=(1.0/INT_MAX)*((float)((int32_t*)in)[i]); | |
541 break; | |
542 } | |
543 } |