cosmetics: av_sub
[mplayer/greg.git] / libaf / format.c
blob56766d2a2e47d6cf63ec836cedda37b9a25bd0b5
1 /*
2 * Copyright (C) 2005 Alex Beregszaszi
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <limits.h>
27 #include "af.h"
29 // Convert from string to format
30 int af_str2fmt(const char* str)
32 int format=0;
33 // Scan for endianness
34 if(strstr(str,"be") || strstr(str,"BE"))
35 format |= AF_FORMAT_BE;
36 else if(strstr(str,"le") || strstr(str,"LE"))
37 format |= AF_FORMAT_LE;
38 else
39 format |= AF_FORMAT_NE;
41 // Scan for special formats
42 if(strstr(str,"mulaw") || strstr(str,"MULAW")){
43 format |= AF_FORMAT_MU_LAW; return format;
45 if(strstr(str,"alaw") || strstr(str,"ALAW")){
46 format |= AF_FORMAT_A_LAW; return format;
48 if(strstr(str,"ac3") || strstr(str,"AC3")){
49 format |= AF_FORMAT_AC3 | AF_FORMAT_16BIT; return format;
51 if(strstr(str,"mpeg2") || strstr(str,"MPEG2")){
52 format |= AF_FORMAT_MPEG2; return format;
54 if(strstr(str,"imaadpcm") || strstr(str,"IMAADPCM")){
55 format |= AF_FORMAT_IMA_ADPCM; return format;
58 // Scan for int/float
59 if(strstr(str,"float") || strstr(str,"FLOAT")){
60 format |= AF_FORMAT_F; return format;
62 else
63 format |= AF_FORMAT_I;
65 // Scan for signed/unsigned
66 if(strstr(str,"unsigned") || strstr(str,"UNSIGNED"))
67 format |= AF_FORMAT_US;
68 else
69 format |= AF_FORMAT_SI;
71 return format;
74 int af_fmt2bits(int format)
76 if (AF_FORMAT_IS_AC3(format)) return 16;
77 return (format & AF_FORMAT_BITS_MASK)+8;
78 // return (((format & AF_FORMAT_BITS_MASK)>>3)+1) * 8;
79 #if 0
80 switch(format & AF_FORMAT_BITS_MASK)
82 case AF_FORMAT_8BIT: return 8;
83 case AF_FORMAT_16BIT: return 16;
84 case AF_FORMAT_24BIT: return 24;
85 case AF_FORMAT_32BIT: return 32;
86 case AF_FORMAT_48BIT: return 48;
88 #endif
89 return -1;
92 int af_bits2fmt(int bits)
94 return (bits/8 - 1) << 3;
97 /* Convert format to str input str is a buffer for the
98 converted string, size is the size of the buffer */
99 char* af_fmt2str(int format, char* str, int size)
101 int i=0;
103 if (size < 1)
104 return NULL;
105 size--; // reserve one for terminating 0
107 // Endianness
108 if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK))
109 i+=snprintf(str,size-i,"little-endian ");
110 else
111 i+=snprintf(str,size-i,"big-endian ");
113 if(format & AF_FORMAT_SPECIAL_MASK){
114 switch(format & AF_FORMAT_SPECIAL_MASK){
115 case(AF_FORMAT_MU_LAW):
116 i+=snprintf(&str[i],size-i,"mu-law "); break;
117 case(AF_FORMAT_A_LAW):
118 i+=snprintf(&str[i],size-i,"A-law "); break;
119 case(AF_FORMAT_MPEG2):
120 i+=snprintf(&str[i],size-i,"MPEG-2 "); break;
121 case(AF_FORMAT_AC3):
122 i+=snprintf(&str[i],size-i,"AC3 "); break;
123 case(AF_FORMAT_IMA_ADPCM):
124 i+=snprintf(&str[i],size-i,"IMA-ADPCM "); break;
125 default:
126 i+=snprintf(&str[i],size-i,mp_gtext("unknown format "));
129 else{
130 // Bits
131 i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format));
133 // Point
134 if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK))
135 i+=snprintf(&str[i],size-i,"float ");
136 else{
137 // Sign
138 if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK))
139 i+=snprintf(&str[i],size-i,"unsigned ");
140 else
141 i+=snprintf(&str[i],size-i,"signed ");
143 i+=snprintf(&str[i],size-i,"int ");
146 // remove trailing space
147 if (i > 0 && str[i - 1] == ' ')
148 i--;
149 str[i] = 0; // make sure it is 0 terminated.
150 return str;
153 static struct {
154 const char *name;
155 const int format;
156 } af_fmtstr_table[] = {
157 { "mulaw", AF_FORMAT_MU_LAW },
158 { "alaw", AF_FORMAT_A_LAW },
159 { "mpeg2", AF_FORMAT_MPEG2 },
160 { "ac3le", AF_FORMAT_AC3_LE },
161 { "ac3be", AF_FORMAT_AC3_BE },
162 { "ac3ne", AF_FORMAT_AC3_NE },
163 { "imaadpcm", AF_FORMAT_IMA_ADPCM },
165 { "u8", AF_FORMAT_U8 },
166 { "s8", AF_FORMAT_S8 },
167 { "u16le", AF_FORMAT_U16_LE },
168 { "u16be", AF_FORMAT_U16_BE },
169 { "u16ne", AF_FORMAT_U16_NE },
170 { "s16le", AF_FORMAT_S16_LE },
171 { "s16be", AF_FORMAT_S16_BE },
172 { "s16ne", AF_FORMAT_S16_NE },
173 { "u24le", AF_FORMAT_U24_LE },
174 { "u24be", AF_FORMAT_U24_BE },
175 { "u24ne", AF_FORMAT_U24_NE },
176 { "s24le", AF_FORMAT_S24_LE },
177 { "s24be", AF_FORMAT_S24_BE },
178 { "s24ne", AF_FORMAT_S24_NE },
179 { "u32le", AF_FORMAT_U32_LE },
180 { "u32be", AF_FORMAT_U32_BE },
181 { "u32ne", AF_FORMAT_U32_NE },
182 { "s32le", AF_FORMAT_S32_LE },
183 { "s32be", AF_FORMAT_S32_BE },
184 { "s32ne", AF_FORMAT_S32_NE },
185 { "floatle", AF_FORMAT_FLOAT_LE },
186 { "floatbe", AF_FORMAT_FLOAT_BE },
187 { "floatne", AF_FORMAT_FLOAT_NE },
189 { NULL, 0 }
192 const char *af_fmt2str_short(int format)
194 int i;
196 for (i = 0; af_fmtstr_table[i].name; i++)
197 if (af_fmtstr_table[i].format == format)
198 return af_fmtstr_table[i].name;
200 return "??";
203 int af_str2fmt_short(const char* str)
205 int i;
207 for (i = 0; af_fmtstr_table[i].name; i++)
208 if (!strcasecmp(str, af_fmtstr_table[i].name))
209 return af_fmtstr_table[i].format;
211 return -1;