16115
|
1 /*=============================================================================
|
|
2 //
|
|
3 // This software has been released under the terms of the GNU General Public
|
|
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
|
|
5 //
|
|
6 // Copyright 2005 Alex Beregszaszi
|
|
7 //
|
|
8 //=============================================================================
|
|
9 */
|
|
10
|
14748
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <string.h>
|
|
14 #include <unistd.h>
|
|
15 #include <inttypes.h>
|
|
16 #include <limits.h>
|
|
17
|
|
18 #include "af.h"
|
|
19
|
|
20 // Convert from string to format
|
|
21 int af_str2fmt(char* str)
|
|
22 {
|
|
23 int format=0;
|
|
24 // Scan for endianess
|
|
25 if(strstr(str,"be") || strstr(str,"BE"))
|
|
26 format |= AF_FORMAT_BE;
|
|
27 else if(strstr(str,"le") || strstr(str,"LE"))
|
|
28 format |= AF_FORMAT_LE;
|
|
29 else
|
|
30 format |= AF_FORMAT_NE;
|
|
31
|
|
32 // Scan for special formats
|
|
33 if(strstr(str,"mulaw") || strstr(str,"MULAW")){
|
|
34 format |= AF_FORMAT_MU_LAW; return format;
|
|
35 }
|
|
36 if(strstr(str,"alaw") || strstr(str,"ALAW")){
|
|
37 format |= AF_FORMAT_A_LAW; return format;
|
|
38 }
|
|
39 if(strstr(str,"ac3") || strstr(str,"AC3")){
|
|
40 format |= AF_FORMAT_AC3; return format;
|
|
41 }
|
|
42 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){
|
|
43 format |= AF_FORMAT_MPEG2; return format;
|
|
44 }
|
|
45 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){
|
|
46 format |= AF_FORMAT_IMA_ADPCM; return format;
|
|
47 }
|
|
48
|
|
49 // Scan for int/float
|
|
50 if(strstr(str,"float") || strstr(str,"FLOAT")){
|
|
51 format |= AF_FORMAT_F; return format;
|
|
52 }
|
|
53 else
|
|
54 format |= AF_FORMAT_I;
|
|
55
|
|
56 // Scan for signed/unsigned
|
|
57 if(strstr(str,"unsigned") || strstr(str,"UNSIGNED"))
|
|
58 format |= AF_FORMAT_US;
|
|
59 else
|
|
60 format |= AF_FORMAT_SI;
|
|
61
|
|
62 return format;
|
|
63 }
|
|
64
|
|
65 inline int af_fmt2bits(int format)
|
|
66 {
|
|
67 return (format & AF_FORMAT_BITS_MASK)+8;
|
|
68 // return (((format & AF_FORMAT_BITS_MASK)>>3)+1) * 8;
|
|
69 #if 0
|
|
70 switch(format & AF_FORMAT_BITS_MASK)
|
|
71 {
|
|
72 case AF_FORMAT_8BIT: return 8;
|
|
73 case AF_FORMAT_16BIT: return 16;
|
|
74 case AF_FORMAT_24BIT: return 24;
|
|
75 case AF_FORMAT_32BIT: return 32;
|
|
76 case AF_FORMAT_48BIT: return 48;
|
|
77 }
|
|
78 #endif
|
|
79 return -1;
|
|
80 }
|
|
81
|
|
82 inline int af_bits2fmt(int bits)
|
|
83 {
|
|
84 return (bits/8 - 1) << 3;
|
|
85 }
|
|
86
|
|
87 /* Convert format to str input str is a buffer for the
|
|
88 converted string, size is the size of the buffer */
|
|
89 char* af_fmt2str(int format, char* str, int size)
|
|
90 {
|
|
91 int i=0;
|
|
92
|
|
93 if (size < 1)
|
|
94 return NULL;
|
|
95 size--; // reserve one for terminating 0
|
|
96
|
|
97 // Endianess
|
|
98 if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK))
|
|
99 i+=snprintf(str,size-i,"little-endian ");
|
|
100 else
|
|
101 i+=snprintf(str,size-i,"big-endian ");
|
|
102
|
|
103 if(format & AF_FORMAT_SPECIAL_MASK){
|
|
104 switch(format & AF_FORMAT_SPECIAL_MASK){
|
|
105 case(AF_FORMAT_MU_LAW):
|
|
106 i+=snprintf(&str[i],size-i,"mu-law "); break;
|
|
107 case(AF_FORMAT_A_LAW):
|
|
108 i+=snprintf(&str[i],size-i,"A-law "); break;
|
|
109 case(AF_FORMAT_MPEG2):
|
|
110 i+=snprintf(&str[i],size-i,"MPEG-2 "); break;
|
|
111 case(AF_FORMAT_AC3):
|
|
112 i+=snprintf(&str[i],size-i,"AC3 "); break;
|
|
113 case(AF_FORMAT_IMA_ADPCM):
|
|
114 i+=snprintf(&str[i],size-i,"IMA-ADPCM "); break;
|
|
115 default:
|
|
116 printf("Unknown special\n");
|
|
117 }
|
|
118 }
|
|
119 else{
|
|
120 // Bits
|
|
121 i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format));
|
|
122
|
|
123 // Point
|
|
124 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK))
|
|
125 i+=snprintf(&str[i],size-i,"float ");
|
|
126 else{
|
|
127 // Sign
|
|
128 if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK))
|
|
129 i+=snprintf(&str[i],size-i,"unsigned ");
|
|
130 else
|
|
131 i+=snprintf(&str[i],size-i,"signed ");
|
|
132
|
|
133 i+=snprintf(&str[i],size-i,"int ");
|
|
134 }
|
|
135 }
|
|
136 // remove trailing space
|
|
137 if (i > 0 && str[i - 1] == ' ')
|
|
138 i--;
|
|
139 str[i] = 0; // make sure it is 0 terminated.
|
|
140 return str;
|
|
141 }
|
|
142
|
|
143 char *af_fmt2str_short(int format)
|
|
144 {
|
|
145 switch(format)
|
|
146 {
|
|
147 // special
|
|
148 case AF_FORMAT_MU_LAW: return "mulaw";
|
|
149 case AF_FORMAT_A_LAW: return "alaw";
|
|
150 case AF_FORMAT_MPEG2: return "mpeg2";
|
|
151 case AF_FORMAT_AC3: return "ac3";
|
|
152 case AF_FORMAT_IMA_ADPCM: return "imaadpcm";
|
|
153 // ordinary
|
|
154 case AF_FORMAT_U8: return "u8";
|
|
155 case AF_FORMAT_S8: return "s8";
|
|
156 case AF_FORMAT_U16_LE: return "u16le";
|
|
157 case AF_FORMAT_U16_BE: return "u16be";
|
|
158 case AF_FORMAT_S16_LE: return "s16le";
|
|
159 case AF_FORMAT_S16_BE: return "s16be";
|
|
160 case AF_FORMAT_U24_LE: return "u24le";
|
|
161 case AF_FORMAT_U24_BE: return "u24be";
|
|
162 case AF_FORMAT_S24_LE: return "s24le";
|
|
163 case AF_FORMAT_S24_BE: return "s24be";
|
|
164 case AF_FORMAT_U32_LE: return "u32le";
|
|
165 case AF_FORMAT_U32_BE: return "u32be";
|
|
166 case AF_FORMAT_S32_LE: return "s32le";
|
|
167 case AF_FORMAT_S32_BE: return "s32be";
|
|
168 case AF_FORMAT_FLOAT_LE: return "floatle";
|
|
169 case AF_FORMAT_FLOAT_BE: return "floatbe";
|
|
170 }
|
|
171 return "??";
|
|
172 }
|
|
173
|
|
174 int af_str2fmt_short(char* str)
|
|
175 {
|
|
176 int i;
|
|
177 static struct {
|
|
178 const char *name;
|
|
179 const int format;
|
|
180 } table[] = {
|
|
181 { "mulaw", AF_FORMAT_MU_LAW },
|
|
182 { "alaw", AF_FORMAT_A_LAW },
|
|
183 { "mpeg2", AF_FORMAT_MPEG2 },
|
|
184 { "ac3", AF_FORMAT_AC3 },
|
|
185 { "imaadpcm", AF_FORMAT_IMA_ADPCM },
|
|
186
|
|
187 { "u8", AF_FORMAT_U8 },
|
|
188 { "s8", AF_FORMAT_S8 },
|
|
189 { "u16le", AF_FORMAT_U16_LE },
|
|
190 { "u16be", AF_FORMAT_U16_BE },
|
|
191 { "u16ne", AF_FORMAT_U16_NE },
|
|
192 { "s16le", AF_FORMAT_S16_LE },
|
|
193 { "s16be", AF_FORMAT_S16_BE },
|
|
194 { "s16ne", AF_FORMAT_S16_NE },
|
|
195 { "u24le", AF_FORMAT_U24_LE },
|
|
196 { "u24be", AF_FORMAT_U24_BE },
|
|
197 { "u24ne", AF_FORMAT_U24_NE },
|
|
198 { "s24le", AF_FORMAT_S24_LE },
|
|
199 { "s24be", AF_FORMAT_S24_BE },
|
|
200 { "s24ne", AF_FORMAT_S24_NE },
|
|
201 { "u32le", AF_FORMAT_U32_LE },
|
|
202 { "u32be", AF_FORMAT_U32_BE },
|
|
203 { "u32ne", AF_FORMAT_U32_NE },
|
|
204 { "s32le", AF_FORMAT_S32_LE },
|
|
205 { "s32be", AF_FORMAT_S32_BE },
|
|
206 { "s32ne", AF_FORMAT_S32_NE },
|
|
207 { "floatle", AF_FORMAT_FLOAT_LE },
|
|
208 { "floatbe", AF_FORMAT_FLOAT_BE },
|
|
209 { "floatne", AF_FORMAT_FLOAT_NE },
|
|
210
|
|
211 { NULL, 0 }
|
|
212 };
|
|
213
|
|
214 for (i = 0; table[i].name; i++)
|
|
215 if (!strcasecmp(str, table[i].name))
|
|
216 return table[i].format;
|
|
217
|
|
218 return -1;
|
|
219 }
|