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