Mercurial > libavcodec.hg
annotate opt.c @ 2874:b6def74f5811 libavcodec
flags and named constants with type checking of course for AVOption
spliting AVOption specific stuff out of avcodec.h into opt.h
author | michael |
---|---|
date | Sun, 11 Sep 2005 14:22:42 +0000 |
parents | 55809f38eb63 |
children | 1021498a5159 |
rev | line source |
---|---|
2862 | 1 /* |
2 * AVOptions | |
3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at> | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 * | |
19 */ | |
20 | |
21 /** | |
22 * @file opt.c | |
23 * AVOptions | |
24 * @author Michael Niedermayer <michaelni@gmx.at> | |
25 */ | |
26 | |
27 #include "avcodec.h" | |
28 | |
29 static double av_parse_num(const char *name, char **tail){ | |
30 double d; | |
31 d= strtod(name, tail); | |
32 if(*tail>name && (**tail=='/' || **tail==':')) | |
33 d/=strtod((*tail)+1, tail); | |
34 return d; | |
35 } | |
36 | |
37 //FIXME order them and do a bin search | |
38 static AVOption *find_opt(void *v, const char *name){ | |
39 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass | |
40 AVOption *o= c->option; | |
41 | |
42 for(;o && o->name; o++){ | |
43 if(!strcmp(o->name, name)) | |
44 return o; | |
45 } | |
46 return NULL; | |
47 } | |
48 | |
2865
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
49 AVOption *av_next_option(void *obj, AVOption *last){ |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
50 if(last && last[1].name) return ++last; |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
51 else if(last) return NULL; |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
52 else return (*(AVClass**)obj)->option; |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
53 } |
3b999ce45b37
AVOption enumeration support and some flags to classify AVOptions
michael
parents:
2862
diff
changeset
|
54 |
2873 | 55 static AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum){ |
2862 | 56 AVOption *o= find_opt(obj, name); |
57 void *dst; | |
58 if(!o || o->offset<=0) | |
2873 | 59 return NULL; |
2862 | 60 |
61 if(o->max*den < num*intnum || o->min*den > num*intnum) | |
2873 | 62 return NULL; |
2862 | 63 |
64 dst= ((uint8_t*)obj) + o->offset; | |
65 | |
66 switch(o->type){ | |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
67 case FF_OPT_TYPE_FLAGS: |
2873 | 68 case FF_OPT_TYPE_INT: *(int *)dst= lrintf(num/den)*intnum; break; |
69 case FF_OPT_TYPE_INT64: *(int64_t *)dst= lrintf(num/den)*intnum; break; | |
70 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; | |
71 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; | |
2862 | 72 case FF_OPT_TYPE_RATIONAL: |
2873 | 73 if((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den}; |
74 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24); | |
2862 | 75 default: |
2873 | 76 return NULL; |
2862 | 77 } |
2873 | 78 return o; |
2862 | 79 } |
80 | |
81 //FIXME use eval.c maybe? | |
2873 | 82 AVOption *av_set_string(void *obj, const char *name, const char *val){ |
2862 | 83 AVOption *o= find_opt(obj, name); |
84 if(!o || !val || o->offset<=0) | |
2873 | 85 return NULL; |
2862 | 86 if(o->type != FF_OPT_TYPE_STRING){ |
87 for(;;){ | |
88 int i; | |
89 char buf[256], *tail; | |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
90 int cmd=0; |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
91 double d; |
2862 | 92 |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
93 if(*val == '+' || *val == '-') |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
94 cmd= *(val++); |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
95 |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
96 for(i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++) |
2862 | 97 buf[i]= val[i]; |
98 buf[i]=0; | |
99 val+= i; | |
100 | |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
101 d= av_parse_num(buf, &tail); |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
102 if(tail <= buf){ |
2862 | 103 AVOption *o_named= find_opt(obj, buf); |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
104 if(o_named && o_named->type == FF_OPT_TYPE_CONST && !strcmp(o_named->unit, o->unit)) |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
105 d= o_named->default_val; |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
106 else if(!strcmp(buf, "default")) d= o->default_val; |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
107 else if(!strcmp(buf, "max" )) d= o->max; |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
108 else if(!strcmp(buf, "min" )) d= o->min; |
2873 | 109 else return NULL; |
2862 | 110 } |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
111 if(o->type == FF_OPT_TYPE_FLAGS){ |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
112 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d; |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
113 else if(cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d; |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
114 }else if(cmd=='-') |
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
115 d= -d; |
2862 | 116 |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
117 av_set_number(obj, name, d, 1, 1); |
2862 | 118 if(!*val) |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
119 return o; |
2862 | 120 } |
2873 | 121 return NULL; |
2862 | 122 } |
123 | |
124 memcpy(((uint8_t*)obj) + o->offset, val, sizeof(val)); | |
2873 | 125 return o; |
2862 | 126 } |
127 | |
2873 | 128 AVOption *av_set_double(void *obj, const char *name, double n){ |
2862 | 129 return av_set_number(obj, name, n, 1, 1); |
130 } | |
131 | |
2873 | 132 AVOption *av_set_q(void *obj, const char *name, AVRational n){ |
2862 | 133 return av_set_number(obj, name, n.num, n.den, 1); |
134 } | |
135 | |
2873 | 136 AVOption *av_set_int(void *obj, const char *name, int64_t n){ |
2862 | 137 return av_set_number(obj, name, 1, 1, n); |
138 } | |
139 | |
2873 | 140 /** |
141 * | |
142 * @param buf a buffer which is used for returning non string values as strings, can be NULL | |
143 * @param buf_len allocated length in bytes of buf | |
144 */ | |
145 const char *av_get_string(void *obj, const char *name, AVOption **o_out, char *buf, int buf_len){ | |
2862 | 146 AVOption *o= find_opt(obj, name); |
2873 | 147 void *dst; |
2862 | 148 if(!o || o->offset<=0) |
149 return NULL; | |
2873 | 150 if(o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len)) |
2862 | 151 return NULL; |
152 | |
2873 | 153 dst= ((uint8_t*)obj) + o->offset; |
154 if(o_out) *o_out= o; | |
155 | |
156 if(o->type == FF_OPT_TYPE_STRING) | |
157 return dst; | |
158 | |
159 switch(o->type){ | |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
160 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break; |
2873 | 161 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break; |
162 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%Ld", *(int64_t*)dst);break; | |
163 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break; | |
164 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break; | |
165 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break; | |
166 default: return NULL; | |
167 } | |
168 return buf; | |
2862 | 169 } |
170 | |
2873 | 171 static int av_get_number(void *obj, const char *name, AVOption **o_out, double *num, int *den, int64_t *intnum){ |
2862 | 172 AVOption *o= find_opt(obj, name); |
173 void *dst; | |
174 if(!o || o->offset<=0) | |
2873 | 175 goto error; |
2862 | 176 |
177 dst= ((uint8_t*)obj) + o->offset; | |
178 | |
2873 | 179 if(o_out) *o_out= o; |
180 | |
2862 | 181 switch(o->type){ |
2874
b6def74f5811
flags and named constants with type checking of course for AVOption
michael
parents:
2873
diff
changeset
|
182 case FF_OPT_TYPE_FLAGS: |
2873 | 183 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0; |
184 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0; | |
185 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0; | |
186 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0; | |
187 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num; | |
188 *den = ((AVRational*)dst)->den; | |
189 return 0; | |
2862 | 190 } |
2873 | 191 error: |
192 *den=*intnum=0; | |
193 return -1; | |
2862 | 194 } |
2873 | 195 |
196 double av_get_double(void *obj, const char *name, AVOption **o_out){ | |
197 int64_t intnum=1; | |
198 double num=1; | |
199 int den=1; | |
200 | |
201 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
202 return num*intnum/den; | |
203 } | |
204 | |
205 AVRational av_get_q(void *obj, const char *name, AVOption **o_out){ | |
206 int64_t intnum=1; | |
207 double num=1; | |
208 int den=1; | |
209 | |
210 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
211 if(num == 1.0 && (int)intnum == intnum) | |
212 return (AVRational){intnum, den}; | |
213 else | |
214 return av_d2q(num*intnum/den, 1<<24); | |
215 } | |
216 | |
217 int64_t av_get_int(void *obj, const char *name, AVOption **o_out){ | |
218 int64_t intnum=1; | |
219 double num=1; | |
220 int den=1; | |
221 | |
222 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
223 return num*intnum/den; | |
224 } | |
225 | |
226 int av_opt_show(void *obj, FILE *f){ | |
227 AVOption *opt=NULL; | |
228 | |
229 if(!obj) | |
230 return -1; | |
231 #undef fprintf | |
232 fprintf(f, "%s AVOptions:\n", (*(AVClass**)obj)->class_name); | |
233 | |
234 while((opt= av_next_option(obj, opt))){ | |
235 if(!(opt->flags & (AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM))) | |
236 continue; | |
237 | |
238 fprintf(f, "-%-17s ", opt->name); | |
239 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.'); | |
240 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.'); | |
241 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.'); | |
242 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.'); | |
243 fprintf(f, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.'); | |
244 | |
245 fprintf(f, " %s\n", opt->help); | |
246 } | |
247 return 0; | |
248 } |