Mercurial > mplayer.hg
annotate libaf/af_format.c @ 28263:89b5b5b03ff4
cosmetics: Move some checks to more logical places.
author | diego |
---|---|
date | Sun, 11 Jan 2009 12:58:06 +0000 |
parents | 1bf9023840f9 |
children | 0ec2ac66f064 |
rev | line source |
---|---|
28229
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
1 /* |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
2 * This audio filter changes the format of a data block. Valid |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
3 * formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
4 * AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
5 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
6 * This file is part of MPlayer. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
7 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
8 * MPlayer is free software; you can redistribute it and/or modify |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
11 * (at your option) any later version. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
12 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
13 * MPlayer is distributed in the hope that it will be useful, |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
16 * GNU General Public License for more details. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
17 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
18 * You should have received a copy of the GNU General Public License along |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
19 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
26345
diff
changeset
|
21 */ |
7568 | 22 |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 #include <inttypes.h> | |
27 #include <limits.h> | |
28 | |
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
|
29 // 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
|
30 #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
|
31 #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
|
32 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
|
33 #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
|
34 #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
|
35 #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
|
36 |
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
|
37 #include "af.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21372
diff
changeset
|
38 #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
|
39 #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
|
40 |
8167 | 41 /* Functions used by play to convert the input audio to the correct |
42 format */ | |
7568 | 43 |
24561 | 44 /* The below includes retrieves functions for converting to and from |
8167 | 45 ulaw and alaw */ |
28238
1bf9023840f9
Rename libaf/af_format_alaw.c --> libaf/af_format_alaw.h and
diego
parents:
28236
diff
changeset
|
46 #include "af_format_ulaw.h" |
1bf9023840f9
Rename libaf/af_format_alaw.c --> libaf/af_format_alaw.h and
diego
parents:
28236
diff
changeset
|
47 #include "af_format_alaw.h" |
7568 | 48 |
24561 | 49 // Switch endianness |
8167 | 50 static void endian(void* in, void* out, int len, int bps); |
24561 | 51 // From signed to unsigned and the other way |
16664 | 52 static void si2us(void* data, int len, int bps); |
8167 | 53 // Change the number of bits per sample |
54 static void change_bps(void* in, void* out, int len, int inbps, int outbps); | |
55 // 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
|
56 static void float2int(float* in, void* out, int len, int bps); |
8167 | 57 // 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
|
58 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
|
59 |
14272 | 60 static af_data_t* play(struct af_instance_s* af, af_data_t* data); |
61 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data); | |
62 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data); | |
63 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data); | |
64 | |
8607 | 65 // Helper functions to check sanity for input arguments |
66 | |
67 // 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
|
68 static int check_bps(int bps) |
8607 | 69 { |
12478 | 70 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ |
8607 | 71 af_msg(AF_MSG_ERROR,"[format] The number of bytes per sample" |
12478 | 72 " must be 1, 2, 3 or 4. Current value is %i \n",bps); |
8607 | 73 return AF_ERROR; |
74 } | |
75 return AF_OK; | |
76 } | |
77 | |
78 // Check for unsupported formats | |
13989
dcb6b4a33aaa
declare check_format and check_bps static, they are used nowhere else.
reimar
parents:
12486
diff
changeset
|
79 static int check_format(int format) |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
80 { |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
81 char buf[256]; |
8607 | 82 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
|
83 case(AF_FORMAT_IMA_ADPCM): |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
84 case(AF_FORMAT_MPEG2): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
85 case(AF_FORMAT_AC3): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
86 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
|
87 af_fmt2str(format,buf,256)); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
88 return AF_ERROR; |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
89 } |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
90 return AF_OK; |
8607 | 91 } |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
92 |
7568 | 93 // Initialization and runtime control |
94 static int control(struct af_instance_s* af, int cmd, void* arg) | |
95 { | |
96 switch(cmd){ | |
8167 | 97 case AF_CONTROL_REINIT:{ |
98 char buf1[256]; | |
99 char buf2[256]; | |
14272 | 100 af_data_t *data = arg; |
101 | |
7568 | 102 // Make sure this filter isn't redundant |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
103 if(af->data->format == data->format && |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
104 af->data->bps == data->bps) |
7568 | 105 return AF_DETACH; |
8607 | 106 |
24521 | 107 // Check for errors in configuration |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
108 if((AF_OK != check_bps(data->bps)) || |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
109 (AF_OK != check_format(data->format)) || |
8607 | 110 (AF_OK != check_bps(af->data->bps)) || |
111 (AF_OK != check_format(af->data->format))) | |
8167 | 112 return AF_ERROR; |
113 | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
114 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
|
115 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
|
116 af_fmt2str(af->data->format,buf2,256)); |
7568 | 117 |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
118 af->data->rate = data->rate; |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
119 af->data->nch = data->nch; |
24888 | 120 af->mul = (double)af->data->bps / data->bps; |
14272 | 121 |
122 af->play = play; // set default | |
123 | |
24561 | 124 // look whether only endianness differences are there |
14272 | 125 if ((af->data->format & ~AF_FORMAT_END_MASK) == |
126 (data->format & ~AF_FORMAT_END_MASK)) | |
127 { | |
24561 | 128 af_msg(AF_MSG_VERBOSE,"[format] Accelerated endianness conversion only\n"); |
14272 | 129 af->play = play_swapendian; |
130 } | |
131 if ((data->format == AF_FORMAT_FLOAT_NE) && | |
132 (af->data->format == AF_FORMAT_S16_NE)) | |
133 { | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
134 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
|
135 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
|
136 af_fmt2str(af->data->format,buf2,256)); |
14272 | 137 af->play = play_float_s16; |
138 } | |
139 if ((data->format == AF_FORMAT_S16_NE) && | |
140 (af->data->format == AF_FORMAT_FLOAT_NE)) | |
141 { | |
14399
1a882e2a419b
af_fmt2str fixes (remove trailing space, call with size of buffer, not size-1)
reimar
parents:
14335
diff
changeset
|
142 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
|
143 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
|
144 af_fmt2str(af->data->format,buf2,256)); |
14272 | 145 af->play = play_s16_float; |
146 } | |
7568 | 147 return AF_OK; |
8167 | 148 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
149 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
|
150 int format = af_str2fmt_short(arg); |
15311 | 151 if (format == -1) { |
152 af_msg(AF_MSG_ERROR, "[format] %s is not a valid format\n", (char *)arg); | |
153 return AF_ERROR; | |
154 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
155 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) |
8607 | 156 return AF_ERROR; |
157 return AF_OK; | |
8167 | 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 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ |
24561 | 160 // Check for errors in configuration |
8607 | 161 if(AF_OK != check_format(*(int*)arg)) |
162 return AF_ERROR; | |
163 | |
164 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
|
165 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
|
166 |
7568 | 167 return AF_OK; |
168 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
169 } |
7568 | 170 return AF_UNKNOWN; |
171 } | |
172 | |
173 // Deallocate memory | |
174 static void uninit(struct af_instance_s* af) | |
175 { | |
22179 | 176 if (af->data) |
177 free(af->data->audio); | |
178 free(af->data); | |
12386 | 179 af->setup = 0; |
7568 | 180 } |
181 | |
14272 | 182 static af_data_t* play_swapendian(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 | |
24561 | 186 int len = c->len/c->bps; // Length in samples of current audio block |
14272 | 187 |
188 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
189 return NULL; | |
190 | |
191 endian(c->audio,l->audio,len,c->bps); | |
192 | |
193 c->audio = l->audio; | |
194 c->format = l->format; | |
195 | |
196 return c; | |
197 } | |
198 | |
199 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data) | |
200 { | |
201 af_data_t* l = af->data; // Local data | |
202 af_data_t* c = data; // Current working data | |
24561 | 203 int len = c->len/4; // Length in samples of current audio block |
14272 | 204 |
205 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
206 return NULL; | |
207 | |
208 float2int(c->audio, l->audio, len, 2); | |
209 | |
210 c->audio = l->audio; | |
211 c->len = len*2; | |
212 c->bps = 2; | |
213 c->format = l->format; | |
214 | |
215 return c; | |
216 } | |
217 | |
218 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data) | |
219 { | |
220 af_data_t* l = af->data; // Local data | |
221 af_data_t* c = data; // Current working data | |
24561 | 222 int len = c->len/2; // Length in samples of current audio block |
14272 | 223 |
224 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
225 return NULL; | |
226 | |
227 int2float(c->audio, l->audio, len, 2); | |
228 | |
229 c->audio = l->audio; | |
230 c->len = len*4; | |
231 c->bps = 4; | |
232 c->format = l->format; | |
233 | |
234 return c; | |
235 } | |
236 | |
7568 | 237 // Filter data through filter |
238 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
239 { | |
8167 | 240 af_data_t* l = af->data; // Local data |
241 af_data_t* c = data; // Current working data | |
24561 | 242 int len = c->len/c->bps; // Length in samples of current audio block |
7568 | 243 |
244 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
245 return NULL; | |
246 | |
8167 | 247 // Change to cpu native endian format |
248 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) | |
249 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
|
250 |
8167 | 251 // Conversion table |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
252 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) { |
8167 | 253 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
254 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
255 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
256 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
16664 | 257 si2us(l->audio,len,l->bps); |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
258 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) { |
8167 | 259 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
260 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
261 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
262 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
16664 | 263 si2us(l->audio,len,l->bps); |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
264 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { |
8167 | 265 switch(l->format&AF_FORMAT_SPECIAL_MASK){ |
266 case(AF_FORMAT_MU_LAW): | |
267 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
268 break; | |
269 case(AF_FORMAT_A_LAW): | |
270 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
7568 | 271 break; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
272 default: |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
273 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
|
274 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) |
16664 | 275 si2us(l->audio,len,l->bps); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
276 break; |
8167 | 277 } |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
278 } else { |
8167 | 279 // Input must be int |
280 | |
281 // Change signed/unsigned | |
282 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ | |
16664 | 283 si2us(c->audio,len,c->bps); |
8167 | 284 } |
285 // Convert to special formats | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
286 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){ |
8167 | 287 case(AF_FORMAT_MU_LAW): |
288 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
289 break; | |
290 case(AF_FORMAT_A_LAW): | |
291 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
292 break; | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
293 case(AF_FORMAT_F): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
294 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
|
295 break; |
8167 | 296 default: |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
297 // Change the number of bits |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
298 if(c->bps != l->bps) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
299 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
|
300 else |
23457
a124f3abc1ec
Replace implicit use of fast_memcpy via macro by explicit use to allow
reimar
parents:
22748
diff
changeset
|
301 fast_memcpy(l->audio,c->audio,len*c->bps); |
7568 | 302 break; |
303 } | |
304 } | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
305 |
24561 | 306 // Switch from cpu native endian to the correct endianness |
8167 | 307 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) |
308 endian(l->audio,l->audio,len,l->bps); | |
7568 | 309 |
310 // Set output data | |
311 c->audio = l->audio; | |
8167 | 312 c->len = len*l->bps; |
7568 | 313 c->bps = l->bps; |
314 c->format = l->format; | |
315 return c; | |
316 } | |
317 | |
318 // Allocate memory and set function pointers | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22179
diff
changeset
|
319 static int af_open(af_instance_t* af){ |
7568 | 320 af->control=control; |
321 af->uninit=uninit; | |
322 af->play=play; | |
24888 | 323 af->mul=1; |
7568 | 324 af->data=calloc(1,sizeof(af_data_t)); |
325 if(af->data == NULL) | |
326 return AF_ERROR; | |
327 return AF_OK; | |
328 } | |
329 | |
330 // Description of this filter | |
331 af_info_t af_info_format = { | |
332 "Sample format conversion", | |
333 "format", | |
334 "Anders", | |
335 "", | |
7615 | 336 AF_FLAGS_REENTRANT, |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22179
diff
changeset
|
337 af_open |
7568 | 338 }; |
8167 | 339 |
12478 | 340 static inline uint32_t load24bit(void* data, int pos) { |
341 #if WORDS_BIGENDIAN | |
342 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) | | |
343 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
344 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8); | |
345 #else | |
346 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) | | |
347 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
348 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24); | |
349 #endif | |
350 } | |
351 | |
352 static inline void store24bit(void* data, int pos, uint32_t expanded_value) { | |
353 #if WORDS_BIGENDIAN | |
354 ((uint8_t*)data)[3*pos]=expanded_value>>24; | |
355 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
356 ((uint8_t*)data)[3*pos+2]=expanded_value>>8; | |
357 #else | |
358 ((uint8_t*)data)[3*pos]=expanded_value>>8; | |
359 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
360 ((uint8_t*)data)[3*pos+2]=expanded_value>>24; | |
361 #endif | |
362 } | |
363 | |
8167 | 364 // Function implementations used by play |
365 static void endian(void* in, void* out, int len, int bps) | |
366 { | |
367 register int i; | |
368 switch(bps){ | |
369 case(2):{ | |
370 for(i=0;i<len;i++){ | |
12486 | 371 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]); |
8167 | 372 } |
373 break; | |
374 } | |
12478 | 375 case(3):{ |
376 register uint8_t s; | |
377 for(i=0;i<len;i++){ | |
378 s=((uint8_t*)in)[3*i]; | |
379 ((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
|
380 if (in != out) |
dbbc25ea6403
fix endian conversion for (curently unused) case where in buffer != out buffer
reimar
parents:
12478
diff
changeset
|
381 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1]; |
12478 | 382 ((uint8_t*)out)[3*i+2]=s; |
383 } | |
384 break; | |
385 } | |
8167 | 386 case(4):{ |
387 for(i=0;i<len;i++){ | |
12486 | 388 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]); |
8167 | 389 } |
390 break; | |
391 } | |
392 } | |
393 } | |
394 | |
16664 | 395 static void si2us(void* data, int len, int bps) |
8167 | 396 { |
16664 | 397 register long i = -(len * bps); |
398 register uint8_t *p = &((uint8_t *)data)[len * bps]; | |
399 #if AF_FORMAT_NE == AF_FORMAT_LE | |
400 p += bps - 1; | |
401 #endif | |
402 if (len <= 0) return; | |
403 do { | |
404 p[i] ^= 0x80; | |
405 } while (i += bps); | |
8167 | 406 } |
407 | |
408 static void change_bps(void* in, void* out, int len, int inbps, int outbps) | |
409 { | |
410 register int i; | |
411 switch(inbps){ | |
412 case(1): | |
413 switch(outbps){ | |
414 case(2): | |
415 for(i=0;i<len;i++) | |
416 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8; | |
417 break; | |
12478 | 418 case(3): |
419 for(i=0;i<len;i++) | |
420 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24); | |
421 break; | |
8167 | 422 case(4): |
423 for(i=0;i<len;i++) | |
424 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24; | |
425 break; | |
426 } | |
427 break; | |
428 case(2): | |
429 switch(outbps){ | |
430 case(1): | |
431 for(i=0;i<len;i++) | |
432 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8); | |
433 break; | |
12478 | 434 case(3): |
435 for(i=0;i<len;i++) | |
436 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16); | |
437 break; | |
8167 | 438 case(4): |
439 for(i=0;i<len;i++) | |
440 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16; | |
441 break; | |
442 } | |
443 break; | |
12478 | 444 case(3): |
445 switch(outbps){ | |
446 case(1): | |
447 for(i=0;i<len;i++) | |
448 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24); | |
449 break; | |
450 case(2): | |
451 for(i=0;i<len;i++) | |
452 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16); | |
453 break; | |
454 case(4): | |
455 for(i=0;i<len;i++) | |
456 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i); | |
457 break; | |
458 } | |
459 break; | |
8167 | 460 case(4): |
461 switch(outbps){ | |
462 case(1): | |
463 for(i=0;i<len;i++) | |
464 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24); | |
465 break; | |
466 case(2): | |
467 for(i=0;i<len;i++) | |
468 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16); | |
469 break; | |
12478 | 470 case(3): |
471 for(i=0;i<len;i++) | |
472 store24bit(out, i, ((uint32_t*)in)[i]); | |
473 break; | |
8167 | 474 } |
475 break; | |
476 } | |
477 } | |
478 | |
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
|
479 static void float2int(float* in, void* out, int len, int bps) |
8167 | 480 { |
481 register int i; | |
482 switch(bps){ | |
483 case(1): | |
484 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
|
485 ((int8_t*)out)[i] = lrintf(127.0 * in[i]); |
8167 | 486 break; |
487 case(2): | |
488 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
|
489 ((int16_t*)out)[i] = lrintf(32767.0 * in[i]); |
8167 | 490 break; |
12478 | 491 case(3): |
492 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
|
493 store24bit(out, i, lrintf(2147483647.0 * in[i])); |
12478 | 494 break; |
8167 | 495 case(4): |
496 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
|
497 ((int32_t*)out)[i] = lrintf(2147483647.0 * in[i]); |
8167 | 498 break; |
499 } | |
500 } | |
501 | |
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
|
502 static void int2float(void* in, float* out, int len, int bps) |
8167 | 503 { |
504 register int i; | |
505 switch(bps){ | |
506 case(1): | |
507 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
|
508 out[i]=(1.0/128.0)*((int8_t*)in)[i]; |
8167 | 509 break; |
510 case(2): | |
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 out[i]=(1.0/32768.0)*((int16_t*)in)[i]; |
8167 | 513 break; |
12478 | 514 case(3): |
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 out[i]=(1.0/2147483648.0)*((int32_t)load24bit(in, i)); |
12478 | 517 break; |
8167 | 518 case(4): |
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 out[i]=(1.0/2147483648.0)*((int32_t*)in)[i]; |
8167 | 521 break; |
522 } | |
523 } |