Mercurial > libavutil.hg
annotate opt.c @ 1026:580d47a2f015 libavutil
Simplify include, since opt.c is now in libavutil there is no need
to use the library path prefix for eval.h.
author | stefano |
---|---|
date | Mon, 27 Sep 2010 10:34:03 +0000 |
parents | 0ece1ad81f21 |
children | 5dbb12a37c3d |
rev | line source |
---|---|
1022 | 1 /* |
2 * AVOptions | |
3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at> | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 /** | |
23 * @file | |
24 * AVOptions | |
25 * @author Michael Niedermayer <michaelni@gmx.at> | |
26 */ | |
27 | |
28 #include "avutil.h" | |
29 #include "opt.h" | |
1026
580d47a2f015
Simplify include, since opt.c is now in libavutil there is no need
stefano
parents:
1025
diff
changeset
|
30 #include "eval.h" |
1022 | 31 |
32 //FIXME order them and do a bin search | |
1025 | 33 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags) |
34 { | |
1022 | 35 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass |
36 const AVOption *o= c->option; | |
37 | |
1025 | 38 for (; o && o->name; o++) { |
39 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags) | |
1022 | 40 return o; |
41 } | |
42 return NULL; | |
43 } | |
44 | |
1025 | 45 const AVOption *av_next_option(void *obj, const AVOption *last) |
46 { | |
47 if (last && last[1].name) return ++last; | |
48 else if (last) return NULL; | |
49 else return (*(AVClass**)obj)->option; | |
1022 | 50 } |
51 | |
1025 | 52 static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out) |
53 { | |
1022 | 54 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0); |
55 void *dst; | |
1025 | 56 if (o_out) |
1022 | 57 *o_out= o; |
1025 | 58 if (!o || o->offset<=0) |
1022 | 59 return AVERROR(ENOENT); |
60 | |
1025 | 61 if (o->max*den < num*intnum || o->min*den > num*intnum) { |
1022 | 62 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name); |
63 return AVERROR(ERANGE); | |
64 } | |
65 | |
66 dst= ((uint8_t*)obj) + o->offset; | |
67 | |
1025 | 68 switch (o->type) { |
1022 | 69 case FF_OPT_TYPE_FLAGS: |
70 case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break; | |
71 case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break; | |
72 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; | |
73 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; | |
74 case FF_OPT_TYPE_RATIONAL: | |
1025 | 75 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den}; |
76 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24); | |
1022 | 77 break; |
78 default: | |
79 return AVERROR(EINVAL); | |
80 } | |
81 return 0; | |
82 } | |
83 | |
1025 | 84 static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum) |
85 { | |
1022 | 86 const AVOption *o = NULL; |
87 if (av_set_number2(obj, name, num, den, intnum, &o) < 0) | |
88 return NULL; | |
89 else | |
90 return o; | |
91 } | |
92 | |
1025 | 93 static const double const_values[] = { |
1022 | 94 M_PI, |
95 M_E, | |
96 FF_QP2LAMBDA, | |
97 0 | |
98 }; | |
99 | |
1025 | 100 static const char * const const_names[] = { |
1022 | 101 "PI", |
102 "E", | |
103 "QP2LAMBDA", | |
104 0 | |
105 }; | |
106 | |
107 static int hexchar2int(char c) { | |
108 if (c >= '0' && c <= '9') return c - '0'; | |
109 if (c >= 'a' && c <= 'f') return c - 'a' + 10; | |
110 if (c >= 'A' && c <= 'F') return c - 'A' + 10; | |
111 return -1; | |
112 } | |
113 | |
1025 | 114 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out) |
115 { | |
1022 | 116 int ret; |
117 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0); | |
118 if (o_out) | |
119 *o_out = o; | |
1025 | 120 if (!o) |
1022 | 121 return AVERROR(ENOENT); |
1025 | 122 if (!val || o->offset<=0) |
1022 | 123 return AVERROR(EINVAL); |
124 | |
1025 | 125 if (o->type == FF_OPT_TYPE_BINARY) { |
1022 | 126 uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset); |
127 int *lendst = (int *)(dst + 1); | |
128 uint8_t *bin, *ptr; | |
129 int len = strlen(val); | |
130 av_freep(dst); | |
131 *lendst = 0; | |
132 if (len & 1) return AVERROR(EINVAL); | |
133 len /= 2; | |
134 ptr = bin = av_malloc(len); | |
135 while (*val) { | |
136 int a = hexchar2int(*val++); | |
137 int b = hexchar2int(*val++); | |
138 if (a < 0 || b < 0) { | |
139 av_free(bin); | |
140 return AVERROR(EINVAL); | |
141 } | |
142 *ptr++ = (a << 4) | b; | |
143 } | |
144 *dst = bin; | |
145 *lendst = len; | |
146 return 0; | |
147 } | |
1025 | 148 if (o->type != FF_OPT_TYPE_STRING) { |
1022 | 149 int notfirst=0; |
1025 | 150 for (;;) { |
1022 | 151 int i; |
152 char buf[256]; | |
153 int cmd=0; | |
154 double d; | |
155 | |
1025 | 156 if (*val == '+' || *val == '-') |
1022 | 157 cmd= *(val++); |
158 | |
1025 | 159 for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++) |
1022 | 160 buf[i]= val[i]; |
161 buf[i]=0; | |
162 | |
163 { | |
164 const AVOption *o_named= av_find_opt(obj, buf, o->unit, 0, 0); | |
1025 | 165 if (o_named && o_named->type == FF_OPT_TYPE_CONST) |
1022 | 166 d= o_named->default_val; |
1025 | 167 else if (!strcmp(buf, "default")) d= o->default_val; |
168 else if (!strcmp(buf, "max" )) d= o->max; | |
169 else if (!strcmp(buf, "min" )) d= o->min; | |
170 else if (!strcmp(buf, "none" )) d= 0; | |
171 else if (!strcmp(buf, "all" )) d= ~0; | |
1022 | 172 else { |
173 int res = av_parse_and_eval_expr(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj); | |
174 if (res < 0) { | |
175 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val); | |
176 return res; | |
177 } | |
178 } | |
179 } | |
1025 | 180 if (o->type == FF_OPT_TYPE_FLAGS) { |
181 if (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d; | |
182 else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d; | |
183 } else { | |
184 if (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d; | |
185 else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d; | |
1022 | 186 } |
187 | |
188 if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0) | |
189 return ret; | |
190 val+= i; | |
1025 | 191 if (!*val) |
1022 | 192 return 0; |
193 notfirst=1; | |
194 } | |
195 return AVERROR(EINVAL); | |
196 } | |
197 | |
1025 | 198 if (alloc) { |
1022 | 199 av_free(*(void**)(((uint8_t*)obj) + o->offset)); |
200 val= av_strdup(val); | |
201 } | |
202 | |
203 memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val)); | |
204 return 0; | |
205 } | |
206 | |
1025 | 207 const AVOption *av_set_double(void *obj, const char *name, double n) |
208 { | |
1022 | 209 return av_set_number(obj, name, n, 1, 1); |
210 } | |
211 | |
1025 | 212 const AVOption *av_set_q(void *obj, const char *name, AVRational n) |
213 { | |
1022 | 214 return av_set_number(obj, name, n.num, n.den, 1); |
215 } | |
216 | |
1025 | 217 const AVOption *av_set_int(void *obj, const char *name, int64_t n) |
218 { | |
1022 | 219 return av_set_number(obj, name, 1, 1, n); |
220 } | |
221 | |
222 /** | |
223 * | |
224 * @param buf a buffer which is used for returning non string values as strings, can be NULL | |
225 * @param buf_len allocated length in bytes of buf | |
226 */ | |
1025 | 227 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len) |
228 { | |
1022 | 229 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0); |
230 void *dst; | |
231 uint8_t *bin; | |
232 int len, i; | |
1025 | 233 if (!o || o->offset<=0) |
1022 | 234 return NULL; |
1025 | 235 if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len)) |
1022 | 236 return NULL; |
237 | |
238 dst= ((uint8_t*)obj) + o->offset; | |
1025 | 239 if (o_out) *o_out= o; |
1022 | 240 |
1025 | 241 switch (o->type) { |
1022 | 242 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break; |
243 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break; | |
244 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break; | |
245 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break; | |
246 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break; | |
247 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break; | |
248 case FF_OPT_TYPE_STRING: return *(void**)dst; | |
249 case FF_OPT_TYPE_BINARY: | |
250 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *)); | |
1025 | 251 if (len >= (buf_len + 1)/2) return NULL; |
1022 | 252 bin = *(uint8_t**)dst; |
1025 | 253 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]); |
1022 | 254 break; |
255 default: return NULL; | |
256 } | |
257 return buf; | |
258 } | |
259 | |
1025 | 260 static int av_get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum) |
261 { | |
1022 | 262 const AVOption *o= av_find_opt(obj, name, NULL, 0, 0); |
263 void *dst; | |
1025 | 264 if (!o || o->offset<=0) |
1022 | 265 goto error; |
266 | |
267 dst= ((uint8_t*)obj) + o->offset; | |
268 | |
1025 | 269 if (o_out) *o_out= o; |
1022 | 270 |
1025 | 271 switch (o->type) { |
1022 | 272 case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0; |
273 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0; | |
274 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0; | |
275 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0; | |
276 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0; | |
277 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num; | |
278 *den = ((AVRational*)dst)->den; | |
279 return 0; | |
280 } | |
281 error: | |
282 *den=*intnum=0; | |
283 return -1; | |
284 } | |
285 | |
1025 | 286 double av_get_double(void *obj, const char *name, const AVOption **o_out) |
287 { | |
1022 | 288 int64_t intnum=1; |
289 double num=1; | |
290 int den=1; | |
291 | |
292 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
293 return num*intnum/den; | |
294 } | |
295 | |
1025 | 296 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out) |
297 { | |
1022 | 298 int64_t intnum=1; |
299 double num=1; | |
300 int den=1; | |
301 | |
302 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
1025 | 303 if (num == 1.0 && (int)intnum == intnum) |
1022 | 304 return (AVRational){intnum, den}; |
305 else | |
306 return av_d2q(num*intnum/den, 1<<24); | |
307 } | |
308 | |
1025 | 309 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out) |
310 { | |
1022 | 311 int64_t intnum=1; |
312 double num=1; | |
313 int den=1; | |
314 | |
315 av_get_number(obj, name, o_out, &num, &den, &intnum); | |
316 return num*intnum/den; | |
317 } | |
318 | |
319 static void opt_list(void *obj, void *av_log_obj, const char *unit, | |
320 int req_flags, int rej_flags) | |
321 { | |
322 const AVOption *opt=NULL; | |
323 | |
1025 | 324 while ((opt= av_next_option(obj, opt))) { |
1022 | 325 if (!(opt->flags & req_flags) || (opt->flags & rej_flags)) |
326 continue; | |
327 | |
328 /* Don't print CONST's on level one. | |
329 * Don't print anything but CONST's on level two. | |
330 * Only print items from the requested unit. | |
331 */ | |
332 if (!unit && opt->type==FF_OPT_TYPE_CONST) | |
333 continue; | |
334 else if (unit && opt->type!=FF_OPT_TYPE_CONST) | |
335 continue; | |
336 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit)) | |
337 continue; | |
338 else if (unit && opt->type == FF_OPT_TYPE_CONST) | |
339 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name); | |
340 else | |
341 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name); | |
342 | |
1025 | 343 switch (opt->type) { |
1022 | 344 case FF_OPT_TYPE_FLAGS: |
1025 | 345 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>"); |
1022 | 346 break; |
347 case FF_OPT_TYPE_INT: | |
1025 | 348 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>"); |
1022 | 349 break; |
350 case FF_OPT_TYPE_INT64: | |
1025 | 351 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>"); |
1022 | 352 break; |
353 case FF_OPT_TYPE_DOUBLE: | |
1025 | 354 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>"); |
1022 | 355 break; |
356 case FF_OPT_TYPE_FLOAT: | |
1025 | 357 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>"); |
1022 | 358 break; |
359 case FF_OPT_TYPE_STRING: | |
1025 | 360 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>"); |
1022 | 361 break; |
362 case FF_OPT_TYPE_RATIONAL: | |
1025 | 363 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>"); |
1022 | 364 break; |
365 case FF_OPT_TYPE_BINARY: | |
1025 | 366 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>"); |
1022 | 367 break; |
368 case FF_OPT_TYPE_CONST: | |
369 default: | |
1025 | 370 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", ""); |
1022 | 371 break; |
372 } | |
373 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.'); | |
374 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.'); | |
375 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.'); | |
376 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.'); | |
377 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.'); | |
378 | |
1025 | 379 if (opt->help) |
1022 | 380 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help); |
381 av_log(av_log_obj, AV_LOG_INFO, "\n"); | |
382 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) { | |
383 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags); | |
384 } | |
385 } | |
386 } | |
387 | |
388 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags) | |
389 { | |
1025 | 390 if (!obj) |
1022 | 391 return -1; |
392 | |
393 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name); | |
394 | |
395 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags); | |
396 | |
397 return 0; | |
398 } | |
399 | |
400 /** Set the values of the AVCodecContext or AVFormatContext structure. | |
401 * They are set to the defaults specified in the according AVOption options | |
402 * array default_val field. | |
403 * | |
404 * @param s AVCodecContext or AVFormatContext for which the defaults will be set | |
405 */ | |
406 void av_opt_set_defaults2(void *s, int mask, int flags) | |
407 { | |
408 const AVOption *opt = NULL; | |
409 while ((opt = av_next_option(s, opt)) != NULL) { | |
1025 | 410 if ((opt->flags & mask) != flags) |
1022 | 411 continue; |
1025 | 412 switch (opt->type) { |
1022 | 413 case FF_OPT_TYPE_CONST: |
414 /* Nothing to be done here */ | |
415 break; | |
416 case FF_OPT_TYPE_FLAGS: | |
417 case FF_OPT_TYPE_INT: { | |
418 int val; | |
419 val = opt->default_val; | |
420 av_set_int(s, opt->name, val); | |
421 } | |
422 break; | |
423 case FF_OPT_TYPE_INT64: | |
1025 | 424 if ((double)(opt->default_val+0.6) == opt->default_val) |
1022 | 425 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name); |
426 av_set_int(s, opt->name, opt->default_val); | |
427 break; | |
428 case FF_OPT_TYPE_FLOAT: { | |
429 double val; | |
430 val = opt->default_val; | |
431 av_set_double(s, opt->name, val); | |
432 } | |
433 break; | |
434 case FF_OPT_TYPE_RATIONAL: { | |
435 AVRational val; | |
436 val = av_d2q(opt->default_val, INT_MAX); | |
437 av_set_q(s, opt->name, val); | |
438 } | |
439 break; | |
440 case FF_OPT_TYPE_STRING: | |
441 case FF_OPT_TYPE_BINARY: | |
442 /* Cannot set default for string as default_val is of type * double */ | |
443 break; | |
444 default: | |
445 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name); | |
446 } | |
447 } | |
448 } | |
449 | |
1025 | 450 void av_opt_set_defaults(void *s) |
451 { | |
1022 | 452 av_opt_set_defaults2(s, 0, 0); |
453 } | |
454 |