Mercurial > mplayer.hg
annotate libaf/af_format.c @ 35608:ada9d862292e
sync with en/mplayer.1 rev. 35685
author | jrash |
---|---|
date | Fri, 21 Dec 2012 05:18:50 +0000 |
parents | 29f66b254dd7 |
children | d206960484fe |
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> | |
28337
34b7297904ac
Drop HAVE_LRINTF check, lrintf is used without checking in other places.
diego
parents:
28333
diff
changeset
|
28 #include <math.h> |
7568 | 29 |
28333
0ec2ac66f064
Fix build: Add required header and adjust preprocessor check.
diego
parents:
28238
diff
changeset
|
30 #include "config.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
|
31 #include "af.h" |
34174
a93891202051
Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents:
33736
diff
changeset
|
32 #include "mp_msg.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21372
diff
changeset
|
33 #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
|
34 #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
|
35 |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
36 #include "libavutil/avutil.h" |
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
37 |
8167 | 38 /* Functions used by play to convert the input audio to the correct |
39 format */ | |
7568 | 40 |
24561 | 41 /* The below includes retrieves functions for converting to and from |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
42 ulaw and alaw */ |
28238
1bf9023840f9
Rename libaf/af_format_alaw.c --> libaf/af_format_alaw.h and
diego
parents:
28236
diff
changeset
|
43 #include "af_format_ulaw.h" |
1bf9023840f9
Rename libaf/af_format_alaw.c --> libaf/af_format_alaw.h and
diego
parents:
28236
diff
changeset
|
44 #include "af_format_alaw.h" |
7568 | 45 |
24561 | 46 // Switch endianness |
8167 | 47 static void endian(void* in, void* out, int len, int bps); |
24561 | 48 // From signed to unsigned and the other way |
16664 | 49 static void si2us(void* data, int len, int bps); |
8167 | 50 // Change the number of bits per sample |
51 static void change_bps(void* in, void* out, int len, int inbps, int outbps); | |
52 // 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
|
53 static void float2int(float* in, void* out, int len, int bps); |
8167 | 54 // 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
|
55 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
|
56 |
14272 | 57 static af_data_t* play(struct af_instance_s* af, af_data_t* data); |
58 static af_data_t* play_swapendian(struct af_instance_s* af, af_data_t* data); | |
59 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data); | |
60 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data); | |
61 | |
8607 | 62 // Helper functions to check sanity for input arguments |
63 | |
64 // 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
|
65 static int check_bps(int bps) |
8607 | 66 { |
12478 | 67 if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
68 mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] The number of bytes per sample" |
12478 | 69 " must be 1, 2, 3 or 4. Current value is %i \n",bps); |
8607 | 70 return AF_ERROR; |
71 } | |
72 return AF_OK; | |
73 } | |
74 | |
75 // Check for unsupported formats | |
13989
dcb6b4a33aaa
declare check_format and check_bps static, they are used nowhere else.
reimar
parents:
12486
diff
changeset
|
76 static int check_format(int format) |
8180
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 char buf[256]; |
8607 | 79 switch(format & AF_FORMAT_SPECIAL_MASK){ |
35451
29f66b254dd7
af_format: Fix check_format, non-special formats are of course supported.
reimar
parents:
35444
diff
changeset
|
80 case 0: |
35440
c2b5fda66143
Replace outdated list of unsupported formats by list of supported formats.
reimar
parents:
34174
diff
changeset
|
81 case AF_FORMAT_MU_LAW: |
c2b5fda66143
Replace outdated list of unsupported formats by list of supported formats.
reimar
parents:
34174
diff
changeset
|
82 case AF_FORMAT_A_LAW: |
c2b5fda66143
Replace outdated list of unsupported formats by list of supported formats.
reimar
parents:
34174
diff
changeset
|
83 return AF_OK; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
84 } |
35440
c2b5fda66143
Replace outdated list of unsupported formats by list of supported formats.
reimar
parents:
34174
diff
changeset
|
85 mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] Sample format %s not yet supported \n", |
c2b5fda66143
Replace outdated list of unsupported formats by list of supported formats.
reimar
parents:
34174
diff
changeset
|
86 af_fmt2str(format,buf,256)); |
c2b5fda66143
Replace outdated list of unsupported formats by list of supported formats.
reimar
parents:
34174
diff
changeset
|
87 return AF_ERROR; |
8607 | 88 } |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
89 |
7568 | 90 // Initialization and runtime control |
91 static int control(struct af_instance_s* af, int cmd, void* arg) | |
92 { | |
93 switch(cmd){ | |
8167 | 94 case AF_CONTROL_REINIT:{ |
95 char buf1[256]; | |
96 char buf2[256]; | |
14272 | 97 af_data_t *data = arg; |
35444
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
98 int supported_ac3 = 0; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
99 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
100 // Make sure this filter isn't redundant |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
101 if(af->data->format == data->format && |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
102 af->data->bps == data->bps) |
7568 | 103 return AF_DETACH; |
8607 | 104 |
35444
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
105 // A bit complex because we can convert AC3 |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
106 // to generic iec61937 but not the other way |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
107 // round. |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
108 if (AF_FORMAT_IS_AC3(af->data->format)) |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
109 supported_ac3 = AF_FORMAT_IS_AC3(data->format); |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
110 else if (AF_FORMAT_IS_IEC61937(af->data->format)) |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
111 supported_ac3 = AF_FORMAT_IS_IEC61937(data->format); |
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
112 |
30241
02b9c1a452e1
Add support for distinguishing between little- and big-endian SPDIF AC3
reimar
parents:
29401
diff
changeset
|
113 // Allow trivial AC3-endianness conversion |
35444
4644c320daf4
af_format: support endianness conversion also for iec61937
reimar
parents:
35441
diff
changeset
|
114 if (!supported_ac3) |
24521 | 115 // Check for errors in configuration |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
116 if((AF_OK != check_bps(data->bps)) || |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
117 (AF_OK != check_format(data->format)) || |
8607 | 118 (AF_OK != check_bps(af->data->bps)) || |
119 (AF_OK != check_format(af->data->format))) | |
8167 | 120 return AF_ERROR; |
121 | |
35441
6dc94af7016d
Do not call af_fmt2str on the same data over and over.
reimar
parents:
35440
diff
changeset
|
122 af_fmt2str(data->format,buf1,256); |
6dc94af7016d
Do not call af_fmt2str on the same data over and over.
reimar
parents:
35440
diff
changeset
|
123 af_fmt2str(af->data->format,buf2,256); |
29049 | 124 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Changing sample format from %s to %s\n", |
35441
6dc94af7016d
Do not call af_fmt2str on the same data over and over.
reimar
parents:
35440
diff
changeset
|
125 buf1, buf2); |
7568 | 126 |
14717
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
127 af->data->rate = data->rate; |
51a7560a92b7
confusing mixture of typecasts and casted variable, removed typecasts
reimar
parents:
14433
diff
changeset
|
128 af->data->nch = data->nch; |
24888 | 129 af->mul = (double)af->data->bps / data->bps; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
130 |
14272 | 131 af->play = play; // set default |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
132 |
24561 | 133 // look whether only endianness differences are there |
14272 | 134 if ((af->data->format & ~AF_FORMAT_END_MASK) == |
135 (data->format & ~AF_FORMAT_END_MASK)) | |
136 { | |
29049 | 137 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated endianness conversion only\n"); |
14272 | 138 af->play = play_swapendian; |
139 } | |
140 if ((data->format == AF_FORMAT_FLOAT_NE) && | |
141 (af->data->format == AF_FORMAT_S16_NE)) | |
142 { | |
29049 | 143 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", |
35441
6dc94af7016d
Do not call af_fmt2str on the same data over and over.
reimar
parents:
35440
diff
changeset
|
144 buf1, buf2); |
14272 | 145 af->play = play_float_s16; |
146 } | |
147 if ((data->format == AF_FORMAT_S16_NE) && | |
148 (af->data->format == AF_FORMAT_FLOAT_NE)) | |
149 { | |
29049 | 150 mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", |
35441
6dc94af7016d
Do not call af_fmt2str on the same data over and over.
reimar
parents:
35440
diff
changeset
|
151 buf1, buf2); |
14272 | 152 af->play = play_s16_float; |
153 } | |
7568 | 154 return AF_OK; |
8167 | 155 } |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7719
diff
changeset
|
156 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
|
157 int format = af_str2fmt_short(arg); |
15311 | 158 if (format == -1) { |
29049 | 159 mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] %s is not a valid format\n", (char *)arg); |
15311 | 160 return AF_ERROR; |
161 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
162 if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) |
8607 | 163 return AF_ERROR; |
164 return AF_OK; | |
8167 | 165 } |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
166 case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ |
24561 | 167 // Check for errors in configuration |
30241
02b9c1a452e1
Add support for distinguishing between little- and big-endian SPDIF AC3
reimar
parents:
29401
diff
changeset
|
168 if(!AF_FORMAT_IS_AC3(*(int*)arg) && AF_OK != check_format(*(int*)arg)) |
8607 | 169 return AF_ERROR; |
170 | |
171 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
|
172 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
|
173 |
7568 | 174 return AF_OK; |
175 } | |
14335
8380694ba14f
af_bits2fmt and af_str2fmt_short, also removed the extra FORMAT_BPS control in format.c
alex
parents:
14272
diff
changeset
|
176 } |
7568 | 177 return AF_UNKNOWN; |
178 } | |
179 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
180 // Deallocate memory |
7568 | 181 static void uninit(struct af_instance_s* af) |
182 { | |
22179 | 183 if (af->data) |
184 free(af->data->audio); | |
185 free(af->data); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
186 af->setup = 0; |
7568 | 187 } |
188 | |
14272 | 189 static af_data_t* play_swapendian(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 | |
24561 | 193 int len = c->len/c->bps; // Length in samples of current audio block |
14272 | 194 |
195 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
196 return NULL; | |
197 | |
198 endian(c->audio,l->audio,len,c->bps); | |
199 | |
200 c->audio = l->audio; | |
201 c->format = l->format; | |
202 | |
203 return c; | |
204 } | |
205 | |
206 static af_data_t* play_float_s16(struct af_instance_s* af, af_data_t* data) | |
207 { | |
208 af_data_t* l = af->data; // Local data | |
209 af_data_t* c = data; // Current working data | |
24561 | 210 int len = c->len/4; // Length in samples of current audio block |
14272 | 211 |
212 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
213 return NULL; | |
214 | |
215 float2int(c->audio, l->audio, len, 2); | |
216 | |
217 c->audio = l->audio; | |
218 c->len = len*2; | |
219 c->bps = 2; | |
220 c->format = l->format; | |
221 | |
222 return c; | |
223 } | |
224 | |
225 static af_data_t* play_s16_float(struct af_instance_s* af, af_data_t* data) | |
226 { | |
227 af_data_t* l = af->data; // Local data | |
228 af_data_t* c = data; // Current working data | |
24561 | 229 int len = c->len/2; // Length in samples of current audio block |
14272 | 230 |
231 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
232 return NULL; | |
233 | |
234 int2float(c->audio, l->audio, len, 2); | |
235 | |
236 c->audio = l->audio; | |
237 c->len = len*4; | |
238 c->bps = 4; | |
239 c->format = l->format; | |
240 | |
241 return c; | |
242 } | |
243 | |
7568 | 244 // Filter data through filter |
245 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
246 { | |
8167 | 247 af_data_t* l = af->data; // Local data |
248 af_data_t* c = data; // Current working data | |
24561 | 249 int len = c->len/c->bps; // Length in samples of current audio block |
7568 | 250 |
251 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
252 return NULL; | |
253 | |
8167 | 254 // Change to cpu native endian format |
255 if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) | |
256 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
|
257 |
8167 | 258 // Conversion table |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
259 if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_MU_LAW) { |
8167 | 260 from_ulaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
261 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
262 to_ulaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
263 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
16664 | 264 si2us(l->audio,len,l->bps); |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
265 } else if((c->format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_A_LAW) { |
8167 | 266 from_alaw(c->audio, l->audio, len, l->bps, l->format&AF_FORMAT_POINT_MASK); |
267 if(AF_FORMAT_A_LAW == (l->format&AF_FORMAT_SPECIAL_MASK)) | |
268 to_alaw(l->audio, l->audio, len, 1, AF_FORMAT_SI); | |
269 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) | |
16664 | 270 si2us(l->audio,len,l->bps); |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
271 } else if((c->format & AF_FORMAT_POINT_MASK) == AF_FORMAT_F) { |
8167 | 272 switch(l->format&AF_FORMAT_SPECIAL_MASK){ |
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); | |
7568 | 278 break; |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
279 default: |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
280 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
|
281 if((l->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) |
16664 | 282 si2us(l->audio,len,l->bps); |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
283 break; |
8167 | 284 } |
14261
710b223604fa
100l use right mask type when checking for input format
rtognimp
parents:
14256
diff
changeset
|
285 } else { |
8167 | 286 // Input must be int |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
287 |
8167 | 288 // Change signed/unsigned |
289 if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
290 si2us(c->audio,len,c->bps); |
8167 | 291 } |
292 // Convert to special formats | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
293 switch(l->format&(AF_FORMAT_SPECIAL_MASK|AF_FORMAT_POINT_MASK)){ |
8167 | 294 case(AF_FORMAT_MU_LAW): |
295 to_ulaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
296 break; | |
297 case(AF_FORMAT_A_LAW): | |
298 to_alaw(c->audio, l->audio, len, c->bps, c->format&AF_FORMAT_POINT_MASK); | |
299 break; | |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
300 case(AF_FORMAT_F): |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
301 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
|
302 break; |
8167 | 303 default: |
8180
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
304 // Change the number of bits |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
305 if(c->bps != l->bps) |
4ba9aed295f2
Fixing segfault bug and addnig support for lrintf() in format conversion
anders
parents:
8167
diff
changeset
|
306 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
|
307 else |
23457
a124f3abc1ec
Replace implicit use of fast_memcpy via macro by explicit use to allow
reimar
parents:
22748
diff
changeset
|
308 fast_memcpy(l->audio,c->audio,len*c->bps); |
7568 | 309 break; |
310 } | |
311 } | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7711
diff
changeset
|
312 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
313 // Switch from cpu native endian to the correct endianness |
8167 | 314 if((l->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) |
315 endian(l->audio,l->audio,len,l->bps); | |
7568 | 316 |
317 // Set output data | |
318 c->audio = l->audio; | |
8167 | 319 c->len = len*l->bps; |
7568 | 320 c->bps = l->bps; |
321 c->format = l->format; | |
322 return c; | |
323 } | |
324 | |
325 // 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
|
326 static int af_open(af_instance_t* af){ |
7568 | 327 af->control=control; |
328 af->uninit=uninit; | |
329 af->play=play; | |
24888 | 330 af->mul=1; |
7568 | 331 af->data=calloc(1,sizeof(af_data_t)); |
332 if(af->data == NULL) | |
333 return AF_ERROR; | |
334 return AF_OK; | |
335 } | |
336 | |
337 // Description of this filter | |
338 af_info_t af_info_format = { | |
339 "Sample format conversion", | |
340 "format", | |
341 "Anders", | |
342 "", | |
7615 | 343 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
|
344 af_open |
7568 | 345 }; |
8167 | 346 |
12478 | 347 static inline uint32_t load24bit(void* data, int pos) { |
29401
f01023c524c3
Replace WORDS_BIGENDIAN by HAVE_BIGENDIAN in all internal code.
diego
parents:
29263
diff
changeset
|
348 #if HAVE_BIGENDIAN |
12478 | 349 return (((uint32_t)((uint8_t*)data)[3*pos])<<24) | |
350 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
351 (((uint32_t)((uint8_t*)data)[3*pos+2])<<8); | |
352 #else | |
353 return (((uint32_t)((uint8_t*)data)[3*pos])<<8) | | |
354 (((uint32_t)((uint8_t*)data)[3*pos+1])<<16) | | |
355 (((uint32_t)((uint8_t*)data)[3*pos+2])<<24); | |
356 #endif | |
357 } | |
358 | |
359 static inline void store24bit(void* data, int pos, uint32_t expanded_value) { | |
29401
f01023c524c3
Replace WORDS_BIGENDIAN by HAVE_BIGENDIAN in all internal code.
diego
parents:
29263
diff
changeset
|
360 #if HAVE_BIGENDIAN |
12478 | 361 ((uint8_t*)data)[3*pos]=expanded_value>>24; |
362 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
363 ((uint8_t*)data)[3*pos+2]=expanded_value>>8; | |
364 #else | |
365 ((uint8_t*)data)[3*pos]=expanded_value>>8; | |
366 ((uint8_t*)data)[3*pos+1]=expanded_value>>16; | |
367 ((uint8_t*)data)[3*pos+2]=expanded_value>>24; | |
368 #endif | |
369 } | |
370 | |
8167 | 371 // Function implementations used by play |
372 static void endian(void* in, void* out, int len, int bps) | |
373 { | |
374 register int i; | |
375 switch(bps){ | |
376 case(2):{ | |
377 for(i=0;i<len;i++){ | |
12486 | 378 ((uint16_t*)out)[i]=bswap_16(((uint16_t*)in)[i]); |
8167 | 379 } |
380 break; | |
381 } | |
12478 | 382 case(3):{ |
383 register uint8_t s; | |
384 for(i=0;i<len;i++){ | |
385 s=((uint8_t*)in)[3*i]; | |
386 ((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
|
387 if (in != out) |
dbbc25ea6403
fix endian conversion for (curently unused) case where in buffer != out buffer
reimar
parents:
12478
diff
changeset
|
388 ((uint8_t*)out)[3*i+1]=((uint8_t*)in)[3*i+1]; |
12478 | 389 ((uint8_t*)out)[3*i+2]=s; |
390 } | |
391 break; | |
392 } | |
8167 | 393 case(4):{ |
394 for(i=0;i<len;i++){ | |
12486 | 395 ((uint32_t*)out)[i]=bswap_32(((uint32_t*)in)[i]); |
8167 | 396 } |
397 break; | |
398 } | |
399 } | |
400 } | |
401 | |
16664 | 402 static void si2us(void* data, int len, int bps) |
8167 | 403 { |
16664 | 404 register long i = -(len * bps); |
405 register uint8_t *p = &((uint8_t *)data)[len * bps]; | |
406 #if AF_FORMAT_NE == AF_FORMAT_LE | |
407 p += bps - 1; | |
408 #endif | |
409 if (len <= 0) return; | |
410 do { | |
411 p[i] ^= 0x80; | |
412 } while (i += bps); | |
8167 | 413 } |
414 | |
415 static void change_bps(void* in, void* out, int len, int inbps, int outbps) | |
416 { | |
417 register int i; | |
418 switch(inbps){ | |
419 case(1): | |
420 switch(outbps){ | |
421 case(2): | |
422 for(i=0;i<len;i++) | |
423 ((uint16_t*)out)[i]=((uint16_t)((uint8_t*)in)[i])<<8; | |
424 break; | |
12478 | 425 case(3): |
426 for(i=0;i<len;i++) | |
427 store24bit(out, i, ((uint32_t)((uint8_t*)in)[i])<<24); | |
428 break; | |
8167 | 429 case(4): |
430 for(i=0;i<len;i++) | |
431 ((uint32_t*)out)[i]=((uint32_t)((uint8_t*)in)[i])<<24; | |
432 break; | |
433 } | |
434 break; | |
435 case(2): | |
436 switch(outbps){ | |
437 case(1): | |
438 for(i=0;i<len;i++) | |
439 ((uint8_t*)out)[i]=(uint8_t)((((uint16_t*)in)[i])>>8); | |
440 break; | |
12478 | 441 case(3): |
442 for(i=0;i<len;i++) | |
443 store24bit(out, i, ((uint32_t)((uint16_t*)in)[i])<<16); | |
444 break; | |
8167 | 445 case(4): |
446 for(i=0;i<len;i++) | |
447 ((uint32_t*)out)[i]=((uint32_t)((uint16_t*)in)[i])<<16; | |
448 break; | |
449 } | |
450 break; | |
12478 | 451 case(3): |
452 switch(outbps){ | |
453 case(1): | |
454 for(i=0;i<len;i++) | |
455 ((uint8_t*)out)[i]=(uint8_t)(load24bit(in, i)>>24); | |
456 break; | |
457 case(2): | |
458 for(i=0;i<len;i++) | |
459 ((uint16_t*)out)[i]=(uint16_t)(load24bit(in, i)>>16); | |
460 break; | |
461 case(4): | |
462 for(i=0;i<len;i++) | |
463 ((uint32_t*)out)[i]=(uint32_t)load24bit(in, i); | |
464 break; | |
465 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
466 break; |
8167 | 467 case(4): |
468 switch(outbps){ | |
469 case(1): | |
470 for(i=0;i<len;i++) | |
471 ((uint8_t*)out)[i]=(uint8_t)((((uint32_t*)in)[i])>>24); | |
472 break; | |
473 case(2): | |
474 for(i=0;i<len;i++) | |
475 ((uint16_t*)out)[i]=(uint16_t)((((uint32_t*)in)[i])>>16); | |
476 break; | |
12478 | 477 case(3): |
478 for(i=0;i<len;i++) | |
479 store24bit(out, i, ((uint32_t*)in)[i]); | |
480 break; | |
8167 | 481 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
482 break; |
8167 | 483 } |
484 } | |
485 | |
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
|
486 static void float2int(float* in, void* out, int len, int bps) |
8167 | 487 { |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
488 float f; |
8167 | 489 register int i; |
490 switch(bps){ | |
491 case(1): | |
492 for(i=0;i<len;i++) | |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
493 ((int8_t *)out)[i] = av_clip_int8(lrintf(128.0 * in[i])); |
8167 | 494 break; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
495 case(2): |
8167 | 496 for(i=0;i<len;i++) |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
497 ((int16_t*)out)[i] = av_clip_int16(lrintf(32768.0 * in[i])); |
8167 | 498 break; |
12478 | 499 case(3): |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
500 for(i=0;i<len;i++){ |
33736
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
501 f = in[i] * 8388608; |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
502 store24bit(out, i, av_clip(lrintf(f), -1*(1<<23), (1<<23)-1) << 8); |
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
503 } |
12478 | 504 break; |
8167 | 505 case(4): |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
506 for(i=0;i<len;i++){ |
33736
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
507 f = in[i]; |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
508 if (f <= -1.0) |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
509 ((int32_t*)out)[i] = INT_MIN; |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
510 else |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
511 if (f >= 1.0)//no need to use corrected constant, rounding won't cause overflow |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
512 ((int32_t*)out)[i] = INT_MAX; |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
513 else |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
514 ((int32_t*)out)[i] = lrintf(f*2147483648.0); |
a5a54c7a15ce
Fix the precision loss in float -> 32bit conversion case, introduced
iive
parents:
33723
diff
changeset
|
515 |
33723
6c8743e5fa30
Audio format conversion from float to int is not checked for overflow.
iive
parents:
30241
diff
changeset
|
516 } |
8167 | 517 break; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
518 } |
8167 | 519 } |
520 | |
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
|
521 static void int2float(void* in, float* out, int len, int bps) |
8167 | 522 { |
523 register int i; | |
524 switch(bps){ | |
525 case(1): | |
526 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
|
527 out[i]=(1.0/128.0)*((int8_t*)in)[i]; |
8167 | 528 break; |
529 case(2): | |
530 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
|
531 out[i]=(1.0/32768.0)*((int16_t*)in)[i]; |
8167 | 532 break; |
12478 | 533 case(3): |
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/2147483648.0)*((int32_t)load24bit(in, i)); |
12478 | 536 break; |
8167 | 537 case(4): |
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/2147483648.0)*((int32_t*)in)[i]; |
8167 | 540 break; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
541 } |
8167 | 542 } |