small warning fix:
[mplayer/glamo.git] / libaf / format.c
blob5669ac81f4a25da6318bc670fb166038b479c061
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <inttypes.h>
6 #include <limits.h>
8 #include "af.h"
10 // Convert from string to format
11 int af_str2fmt(char* str)
13 int format=0;
14 // Scan for endianess
15 if(strstr(str,"be") || strstr(str,"BE"))
16 format |= AF_FORMAT_BE;
17 else if(strstr(str,"le") || strstr(str,"LE"))
18 format |= AF_FORMAT_LE;
19 else
20 format |= AF_FORMAT_NE;
22 // Scan for special formats
23 if(strstr(str,"mulaw") || strstr(str,"MULAW")){
24 format |= AF_FORMAT_MU_LAW; return format;
26 if(strstr(str,"alaw") || strstr(str,"ALAW")){
27 format |= AF_FORMAT_A_LAW; return format;
29 if(strstr(str,"ac3") || strstr(str,"AC3")){
30 format |= AF_FORMAT_AC3; return format;
32 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){
33 format |= AF_FORMAT_MPEG2; return format;
35 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){
36 format |= AF_FORMAT_IMA_ADPCM; return format;
39 // Scan for int/float
40 if(strstr(str,"float") || strstr(str,"FLOAT")){
41 format |= AF_FORMAT_F; return format;
43 else
44 format |= AF_FORMAT_I;
46 // Scan for signed/unsigned
47 if(strstr(str,"unsigned") || strstr(str,"UNSIGNED"))
48 format |= AF_FORMAT_US;
49 else
50 format |= AF_FORMAT_SI;
52 return format;
55 inline int af_fmt2bits(int format)
57 return (format & AF_FORMAT_BITS_MASK)+8;
58 // return (((format & AF_FORMAT_BITS_MASK)>>3)+1) * 8;
59 #if 0
60 switch(format & AF_FORMAT_BITS_MASK)
62 case AF_FORMAT_8BIT: return 8;
63 case AF_FORMAT_16BIT: return 16;
64 case AF_FORMAT_24BIT: return 24;
65 case AF_FORMAT_32BIT: return 32;
66 case AF_FORMAT_48BIT: return 48;
68 #endif
69 return -1;
72 inline int af_bits2fmt(int bits)
74 return (bits/8 - 1) << 3;
77 /* Convert format to str input str is a buffer for the
78 converted string, size is the size of the buffer */
79 char* af_fmt2str(int format, char* str, int size)
81 int i=0;
83 if (size < 1)
84 return NULL;
85 size--; // reserve one for terminating 0
87 // Endianess
88 if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK))
89 i+=snprintf(str,size-i,"little-endian ");
90 else
91 i+=snprintf(str,size-i,"big-endian ");
93 if(format & AF_FORMAT_SPECIAL_MASK){
94 switch(format & AF_FORMAT_SPECIAL_MASK){
95 case(AF_FORMAT_MU_LAW):
96 i+=snprintf(&str[i],size-i,"mu-law "); break;
97 case(AF_FORMAT_A_LAW):
98 i+=snprintf(&str[i],size-i,"A-law "); break;
99 case(AF_FORMAT_MPEG2):
100 i+=snprintf(&str[i],size-i,"MPEG-2 "); break;
101 case(AF_FORMAT_AC3):
102 i+=snprintf(&str[i],size-i,"AC3 "); break;
103 case(AF_FORMAT_IMA_ADPCM):
104 i+=snprintf(&str[i],size-i,"IMA-ADPCM "); break;
105 default:
106 printf("Unknown special\n");
109 else{
110 // Bits
111 i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format));
113 // Point
114 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK))
115 i+=snprintf(&str[i],size-i,"float ");
116 else{
117 // Sign
118 if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK))
119 i+=snprintf(&str[i],size-i,"unsigned ");
120 else
121 i+=snprintf(&str[i],size-i,"signed ");
123 i+=snprintf(&str[i],size-i,"int ");
126 // remove trailing space
127 if (i > 0 && str[i - 1] == ' ')
128 i--;
129 str[i] = 0; // make sure it is 0 terminated.
130 return str;
133 char *af_fmt2str_short(int format)
135 switch(format)
137 // special
138 case AF_FORMAT_MU_LAW: return "mulaw";
139 case AF_FORMAT_A_LAW: return "alaw";
140 case AF_FORMAT_MPEG2: return "mpeg2";
141 case AF_FORMAT_AC3: return "ac3";
142 case AF_FORMAT_IMA_ADPCM: return "imaadpcm";
143 // ordinary
144 case AF_FORMAT_U8: return "u8";
145 case AF_FORMAT_S8: return "s8";
146 case AF_FORMAT_U16_LE: return "u16le";
147 case AF_FORMAT_U16_BE: return "u16be";
148 case AF_FORMAT_S16_LE: return "s16le";
149 case AF_FORMAT_S16_BE: return "s16be";
150 case AF_FORMAT_U24_LE: return "u24le";
151 case AF_FORMAT_U24_BE: return "u24be";
152 case AF_FORMAT_S24_LE: return "s24le";
153 case AF_FORMAT_S24_BE: return "s24be";
154 case AF_FORMAT_U32_LE: return "u32le";
155 case AF_FORMAT_U32_BE: return "u32be";
156 case AF_FORMAT_S32_LE: return "s32le";
157 case AF_FORMAT_S32_BE: return "s32be";
158 case AF_FORMAT_FLOAT_LE: return "floatle";
159 case AF_FORMAT_FLOAT_BE: return "floatbe";
161 return "??";
164 int af_str2fmt_short(char* str)
166 int i;
167 static struct {
168 const char *name;
169 const int format;
170 } table[] = {
171 { "mulaw", AF_FORMAT_MU_LAW },
172 { "alaw", AF_FORMAT_A_LAW },
173 { "mpeg2", AF_FORMAT_MPEG2 },
174 { "ac3", AF_FORMAT_AC3 },
175 { "imaadpcm", AF_FORMAT_IMA_ADPCM },
177 { "u8", AF_FORMAT_U8 },
178 { "s8", AF_FORMAT_S8 },
179 { "u16le", AF_FORMAT_U16_LE },
180 { "u16be", AF_FORMAT_U16_BE },
181 { "u16ne", AF_FORMAT_U16_NE },
182 { "s16le", AF_FORMAT_S16_LE },
183 { "s16be", AF_FORMAT_S16_BE },
184 { "s16ne", AF_FORMAT_S16_NE },
185 { "u24le", AF_FORMAT_U24_LE },
186 { "u24be", AF_FORMAT_U24_BE },
187 { "u24ne", AF_FORMAT_U24_NE },
188 { "s24le", AF_FORMAT_S24_LE },
189 { "s24be", AF_FORMAT_S24_BE },
190 { "s24ne", AF_FORMAT_S24_NE },
191 { "u32le", AF_FORMAT_U32_LE },
192 { "u32be", AF_FORMAT_U32_BE },
193 { "u32ne", AF_FORMAT_U32_NE },
194 { "s32le", AF_FORMAT_S32_LE },
195 { "s32be", AF_FORMAT_S32_BE },
196 { "s32ne", AF_FORMAT_S32_NE },
197 { "floatle", AF_FORMAT_FLOAT_LE },
198 { "floatbe", AF_FORMAT_FLOAT_BE },
199 { "floatne", AF_FORMAT_FLOAT_NE },
201 { NULL, 0 }
204 for (i = 0; table[i].name; i++)
205 if (!strcasecmp(str, table[i].name))
206 return table[i].format;
208 return -1;