Mercurial > mplayer.hg
annotate libaf/af_format.c @ 22374:592e6aa9585b
clarify root vs admin
author | michael |
---|---|
date | Thu, 01 Mar 2007 12:43:14 +0000 |
parents | ecf562795caf |
children | fd6f824ef894 |
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 | |
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
|
16 // 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
|
17 #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
|
18 #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
|
19 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
|
20 #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
|
21 #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
|
22 #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
|
23 |
21353
a965ca17debc
reordering of #include to avoid clash with math.h and quicktime/*.h, patch by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
16982
diff
changeset
|
24 #include "af.h" |
21372 | 25 #include "libavutil/common.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21372
diff
changeset
|
26 #include "mpbswap.h" |
21353
a965ca17debc
reordering of #include to avoid clash with math.h and quicktime/*.h, patch by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
16982
diff
changeset
|
27 #include "libvo/fastmemcpy.h" |
a965ca17debc
reordering of #include to avoid clash with math.h and quicktime/*.h, patch by Crhis Roccati<roccati@pobox.com>
nplourde
parents:
16982
diff
changeset
|
28 |
8167 | 29 /* Functions used by play to convert the input audio to the correct |
30 format */ | |
7568 | 31 |
8167 | 32 /* The below includes retrives functions for converting to and from |
33 ulaw and alaw */ | |
34 #include "af_format_ulaw.c" | |
35 #include "af_format_alaw.c" | |
7568 | 36 |
8167 | 37 // Switch endianess |
38 static void endian(void* in, void* out, int len, int bps); | |
16664 | 39 // From singed to unsigned and the other way |
40 static void si2us(void* data, int len, int bps); | |
8167 | 41 // Change the number of bits per sample |
42 static void change_bps(void* in, void* out, int len, int inbps, int outbps); | |
43 // 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
|
44 static void float2int(float* in, void* out, int len, int bps); |
8167 | 45 // 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
|
46 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
|
47 |
14272 | 48 static af_data_t* play(struct af_instance_s* af, af_data_t* data); |
49 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data); | |
50 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data); | |
51 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data); | |
52 | |
8607 | 53 // Helper functions to check sanity for input arguments |
54 | |
55 // 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
|
56 static int check_bps(int bps) |
8607 | 57 { |
12478 | 58 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ |
8607 | 59 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample" |
12478 | 60 " must be 1, 2, 3 or 4. Current value is %i \n",bps); |
8607 | 61 return AF_ERROR; |
62 } | |
63 return AF_OK; | |
64 } | |
65 | |
66 // Check for unsupported formats | |
13989
dcb6b4a33aaa
declare check_format and check_bps static, they are used nowhere else.
reimar
parents:
12486
diff
changeset
|
67 static int check_format(int format) |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
68 { |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
69 char buf[256]; |
8607 | 70 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
|
71 case(AF_FORMAT_IMA_ADPCM): |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
72 case(AF_FORMAT_MPEG2): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
73 case(AF_FORMAT_AC3): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
74 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
|
75 af_fmt2str(format,buf,256)); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
76 return AF_ERROR; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
77 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
78 return AF_OK; |
8607 | 79 } |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
80 |
7568 | 81 // Initialization and runtime control |
82 static int control(struct af_instance_s* af, int cmd, void* arg) | |
83 { | |
84 switch(cmd){ | |
8167 | 85 case AF_CONTROL_REINIT:{ |
86 char buf1[256]; | |
87 char buf2[256]; | |
14272 | 88 af_data_t *data = arg; |
89 | |
7568 | 90 // Make sure this filter isn't redundant |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
91 if(af->data->format == data->format && |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
92 af->data->bps == data->bps) |
7568 | 93 return AF_DETACH; |
8607 | 94 |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
95 // Check for errors in configuraton |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
96 if((AF_OK != check_bps(data->bps)) || |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
97 (AF_OK != check_format(data->format)) || |
8607 | 98 (AF_OK != check_bps(af->data->bps)) || |
99 (AF_OK != check_format(af->data->format))) | |
8167 | 100 return AF_ERROR; |
101 | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
102 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
|
103 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
|
104 af_fmt2str(af->data->format,buf2,256)); |
7568 | 105 |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
106 af->data->rate = data->rate; |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
107 af->data->nch = data->nch; |
7568 | 108 af->mul.n = af->data->bps; |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
109 af->mul.d = data->bps; |
14433
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14399
diff
changeset
|
110 af_frac_cancel(&af->mul); |
14272 | 111 |
112 af->play = play; // set default | |
113 | |
114 // look whether only endianess differences are there | |
115 if ((af->data->format & ~AF_FORMAT_END_MASK) == | |
116 (data->format & ~AF_FORMAT_END_MASK)) | |
117 { | |
118 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianess conversion only\n"); | |
119 af->play = play_swapendian; | |
120 } | |
121 if ((data->format == AF_FORMAT_FLOAT_NE) && | |
122 (af->data->format == AF_FORMAT_S16_NE)) | |
123 { | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
124 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
|
125 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
|
126 af_fmt2str(af->data->format,buf2,256)); |
14272 | 127 af->play = play_float_s16; |
128 } | |
129 if ((data->format == AF_FORMAT_S16_NE) && | |
130 (af->data->format == AF_FORMAT_FLOAT_NE)) | |
131 { | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
132 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
|
133 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
|
134 af_fmt2str(af->data->format,buf2,256)); |
14272 | 135 af->play = play_s16_float; |
136 } | |
7568 | 137 return AF_OK; |
8167 | 138 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
139 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
|
140 int format = af_str2fmt_short(arg); |
15311 | 141 if (format == -1) { |
142 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg); | |
143 return AF_ERROR; | |
144 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
145 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) |
8607 | 146 return AF_ERROR; |
147 return AF_OK; | |
8167 | 148 } |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
149 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ |
8607 | 150 // Check for errors in configuraton |
151 if(AF_OK != check_format(*(int*)arg)) | |
152 return AF_ERROR; | |
153 | |
154 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
|
155 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
|
156 |
7568 | 157 return AF_OK; |
158 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
159 } |
7568 | 160 return AF_UNKNOWN; |
161 } | |
162 | |
163 // Deallocate memory | |
164 static void uninit(struct af_instance_s* af) | |
165 { | |
22179 | 166 if (af->data) |
167 free(af->data->audio); | |
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) | |
16664 | 247 si2us(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) | |
16664 | 253 si2us(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) |
16664 | 265 si2us(l->audio,len,l->bps); |
8180
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)){ | |
16664 | 273 si2us(c->audio,len,c->bps); |
8167 | 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 } | |
294 } | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
295 |
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
296 // Switch from cpu native endian to the correct endianess |
8167 | 297 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) |
298 endian(l->audio,l->audio,len,l->bps); | |
7568 | 299 |
300 // Set output data | |
301 c->audio = l->audio; | |
8167 | 302 c->len = len*l->bps; |
7568 | 303 c->bps = l->bps; |
304 c->format = l->format; | |
305 return c; | |
306 } | |
307 | |
308 // Allocate memory and set function pointers | |
309 static int open(af_instance_t* af){ | |
310 af->control=control; | |
311 af->uninit=uninit; | |
312 af->play=play; | |
313 af->mul.n=1; | |
314 af->mul.d=1; | |
315 af->data=calloc(1,sizeof(af_data_t)); | |
316 if(af->data == NULL) | |
317 return AF_ERROR; | |
318 return AF_OK; | |
319 } | |
320 | |
321 // Description of this filter | |
322 af_info_t af_info_format = { | |
323 "Sample format conversion", | |
324 "format", | |
325 "Anders", | |
326 "", | |
7615 | 327 AF_FLAGS_REENTRANT, |
7568 | 328 open |
329 }; | |
8167 | 330 |
12478 | 331 static inline uint32_t load24bit(void* data, int pos) { |
332 #if WORDS_BIGENDIAN | |
333 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) | | |
334 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
335 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8); | |
336 #else | |
337 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) | | |
338 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
339 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24); | |
340 #endif | |
341 } | |
342 | |
343 static inline void store24bit(void* data, int pos, uint32_t expanded_value) { | |
344 #if WORDS_BIGENDIAN | |
345 ((uint8_t*)data)[3*pos]=expanded_value>>24; | |
346 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
347 ((uint8_t*)data)[3*pos+2]=expanded_value>>8; | |
348 #else | |
349 ((uint8_t*)data)[3*pos]=expanded_value>>8; | |
350 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
351 ((uint8_t*)data)[3*pos+2]=expanded_value>>24; | |
352 #endif | |
353 } | |
354 | |
8167 | 355 // Function implementations used by play |
356 static void endian(void* in, void* out, int len, int bps) | |
357 { | |
358 register int i; | |
359 switch(bps){ | |
360 case(2):{ | |
361 for(i=0;i<len;i++){ | |
12486 | 362 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]); |
8167 | 363 } |
364 break; | |
365 } | |
12478 | 366 case(3):{ |
367 register uint8_t s; | |
368 for(i=0;i<len;i++){ | |
369 s=((uint8_t*)in)[3*i]; | |
370 ((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
|
371 if (in != out) |
dbbc25ea6403
fix endian conversion for (curently unused) case where in buffer != out buffer
reimar
parents:
12478
diff
changeset
|
372 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1]; |
12478 | 373 ((uint8_t*)out)[3*i+2]=s; |
374 } | |
375 break; | |
376 } | |
8167 | 377 case(4):{ |
378 for(i=0;i<len;i++){ | |
12486 | 379 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]); |
8167 | 380 } |
381 break; | |
382 } | |
383 } | |
384 } | |
385 | |
16664 | 386 static void si2us(void* data, int len, int bps) |
8167 | 387 { |
16664 | 388 register long i = -(len * bps); |
389 register uint8_t *p = &((uint8_t *)data)[len * bps]; | |
390 #if AF_FORMAT_NE == AF_FORMAT_LE | |
391 p += bps - 1; | |
392 #endif | |
393 if (len <= 0) return; | |
394 do { | |
395 p[i] ^= 0x80; | |
396 } while (i += bps); | |
8167 | 397 } |
398 | |
399 static void change_bps(void* in, void* out, int len, int inbps, int outbps) | |
400 { | |
401 register int i; | |
402 switch(inbps){ | |
403 case(1): | |
404 switch(outbps){ | |
405 case(2): | |
406 for(i=0;i<len;i++) | |
407 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8; | |
408 break; | |
12478 | 409 case(3): |
410 for(i=0;i<len;i++) | |
411 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24); | |
412 break; | |
8167 | 413 case(4): |
414 for(i=0;i<len;i++) | |
415 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24; | |
416 break; | |
417 } | |
418 break; | |
419 case(2): | |
420 switch(outbps){ | |
421 case(1): | |
422 for(i=0;i<len;i++) | |
423 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8); | |
424 break; | |
12478 | 425 case(3): |
426 for(i=0;i<len;i++) | |
427 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16); | |
428 break; | |
8167 | 429 case(4): |
430 for(i=0;i<len;i++) | |
431 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16; | |
432 break; | |
433 } | |
434 break; | |
12478 | 435 case(3): |
436 switch(outbps){ | |
437 case(1): | |
438 for(i=0;i<len;i++) | |
439 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24); | |
440 break; | |
441 case(2): | |
442 for(i=0;i<len;i++) | |
443 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16); | |
444 break; | |
445 case(4): | |
446 for(i=0;i<len;i++) | |
447 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i); | |
448 break; | |
449 } | |
450 break; | |
8167 | 451 case(4): |
452 switch(outbps){ | |
453 case(1): | |
454 for(i=0;i<len;i++) | |
455 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24); | |
456 break; | |
457 case(2): | |
458 for(i=0;i<len;i++) | |
459 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16); | |
460 break; | |
12478 | 461 case(3): |
462 for(i=0;i<len;i++) | |
463 store24bit(out, i, ((uint32_t*)in)[i]); | |
464 break; | |
8167 | 465 } |
466 break; | |
467 } | |
468 } | |
469 | |
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
|
470 static void float2int(float* in, void* out, int len, int bps) |
8167 | 471 { |
472 register int i; | |
473 switch(bps){ | |
474 case(1): | |
475 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
|
476 ((int8_t*)out)[i] = lrintf(127.0 * in[i]); |
8167 | 477 break; |
478 case(2): | |
479 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
|
480 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]); |
8167 | 481 break; |
12478 | 482 case(3): |
483 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
|
484 store24bit(out, i, lrintf(2147483647.0 * in[i])); |
12478 | 485 break; |
8167 | 486 case(4): |
487 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
|
488 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]); |
8167 | 489 break; |
490 } | |
491 } | |
492 | |
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
|
493 static void int2float(void* in, float* out, int len, int bps) |
8167 | 494 { |
495 register int i; | |
496 switch(bps){ | |
497 case(1): | |
498 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
|
499 out[i]=(1.0/128.0)*((int8_t*)in)[i]; |
8167 | 500 break; |
501 case(2): | |
502 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
|
503 out[i]=(1.0/32768.0)*((int16_t*)in)[i]; |
8167 | 504 break; |
12478 | 505 case(3): |
506 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
|
507 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i)); |
12478 | 508 break; |
8167 | 509 case(4): |
510 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
|
511 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i]; |
8167 | 512 break; |
513 } | |
514 } |