Comment out the correct #endif directive.
[mplayer/greg.git] / libaf / format.c
blob675c593224da9708c4620cba0969de405b594edc
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 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <limits.h>
17 #include "af.h"
18 #include "help_mp.h"
20 // Convert from string to format
21 int af_str2fmt(const char* str)
23 int format=0;
24 // Scan for endianness
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;
32 // Scan for special formats
33 if(strstr(str,"mulaw") || strstr(str,"MULAW")){
34 format |= AF_FORMAT_MU_LAW; return format;
36 if(strstr(str,"alaw") || strstr(str,"ALAW")){
37 format |= AF_FORMAT_A_LAW; return format;
39 if(strstr(str,"ac3") || strstr(str,"AC3")){
40 format |= AF_FORMAT_AC3; return format;
42 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){
43 format |= AF_FORMAT_MPEG2; return format;
45 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){
46 format |= AF_FORMAT_IMA_ADPCM; return format;
49 // Scan for int/float
50 if(strstr(str,"float") || strstr(str,"FLOAT")){
51 format |= AF_FORMAT_F; return format;
53 else
54 format |= AF_FORMAT_I;
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;
62 return format;
65 int af_fmt2bits(int format)
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)
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;
78 #endif
79 return -1;
82 int af_bits2fmt(int bits)
84 return (bits/8 - 1) << 3;
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)
91 int i=0;
93 if (size < 1)
94 return NULL;
95 size--; // reserve one for terminating 0
97 // Endianness
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 ");
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 i+=snprintf(&str[i],size-i,MSGTR_AF_FORMAT_UnknownFormat);
119 else{
120 // Bits
121 i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format));
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 ");
133 i+=snprintf(&str[i],size-i,"int ");
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;
143 static struct {
144 const char *name;
145 const int format;
146 } af_fmtstr_table[] = {
147 { "mulaw", AF_FORMAT_MU_LAW },
148 { "alaw", AF_FORMAT_A_LAW },
149 { "mpeg2", AF_FORMAT_MPEG2 },
150 { "ac3", AF_FORMAT_AC3 },
151 { "imaadpcm", AF_FORMAT_IMA_ADPCM },
153 { "u8", AF_FORMAT_U8 },
154 { "s8", AF_FORMAT_S8 },
155 { "u16le", AF_FORMAT_U16_LE },
156 { "u16be", AF_FORMAT_U16_BE },
157 { "u16ne", AF_FORMAT_U16_NE },
158 { "s16le", AF_FORMAT_S16_LE },
159 { "s16be", AF_FORMAT_S16_BE },
160 { "s16ne", AF_FORMAT_S16_NE },
161 { "u24le", AF_FORMAT_U24_LE },
162 { "u24be", AF_FORMAT_U24_BE },
163 { "u24ne", AF_FORMAT_U24_NE },
164 { "s24le", AF_FORMAT_S24_LE },
165 { "s24be", AF_FORMAT_S24_BE },
166 { "s24ne", AF_FORMAT_S24_NE },
167 { "u32le", AF_FORMAT_U32_LE },
168 { "u32be", AF_FORMAT_U32_BE },
169 { "u32ne", AF_FORMAT_U32_NE },
170 { "s32le", AF_FORMAT_S32_LE },
171 { "s32be", AF_FORMAT_S32_BE },
172 { "s32ne", AF_FORMAT_S32_NE },
173 { "floatle", AF_FORMAT_FLOAT_LE },
174 { "floatbe", AF_FORMAT_FLOAT_BE },
175 { "floatne", AF_FORMAT_FLOAT_NE },
177 { NULL, 0 }
180 const char *af_fmt2str_short(int format)
182 int i;
184 for (i = 0; af_fmtstr_table[i].name; i++)
185 if (af_fmtstr_table[i].format == format)
186 return af_fmtstr_table[i].name;
188 return "??";
191 int af_str2fmt_short(const char* str)
193 int i;
195 for (i = 0; af_fmtstr_table[i].name; i++)
196 if (!strcasecmp(str, af_fmtstr_table[i].name))
197 return af_fmtstr_table[i].format;
199 return -1;