3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Michael Niedermayer <michaelni@gmx.at>
36 //FIXME order them and do a bin search
37 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
39 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
40 const AVOption *o= c->option;
42 for (; o && o->name; o++) {
43 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
50 const AVOption *av_next_option(void *obj, const AVOption *last)
52 if (last && last[1].name) return ++last;
53 else if (last) return NULL;
54 else return (*(AVClass**)obj)->option;
57 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
59 if (o->max*den < num*intnum || o->min*den > num*intnum) {
60 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, o->name);
61 return AVERROR(ERANGE);
65 case FF_OPT_TYPE_FLAGS:
66 case FF_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
67 case FF_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
68 case FF_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
69 case FF_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
70 case FF_OPT_TYPE_RATIONAL:
71 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
72 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
75 return AVERROR(EINVAL);
80 static const double const_values[] = {
87 static const char * const const_names[] = {
94 static int hexchar2int(char c) {
95 if (c >= '0' && c <= '9') return c - '0';
96 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
97 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
101 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
103 int *lendst = (int *)(dst + 1);
105 int len = strlen(val);
111 return AVERROR(EINVAL);
114 ptr = bin = av_malloc(len);
116 int a = hexchar2int(*val++);
117 int b = hexchar2int(*val++);
118 if (a < 0 || b < 0) {
120 return AVERROR(EINVAL);
122 *ptr++ = (a << 4) | b;
130 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
133 *dst = av_strdup(val);
137 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
139 int ret = 0, notfirst = 0;
146 if (*val == '+' || *val == '-')
149 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
154 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
155 if (o_named && o_named->type == FF_OPT_TYPE_CONST)
156 d = o_named->default_val.dbl;
157 else if (!strcmp(buf, "default")) d = o->default_val.dbl;
158 else if (!strcmp(buf, "max" )) d = o->max;
159 else if (!strcmp(buf, "min" )) d = o->min;
160 else if (!strcmp(buf, "none" )) d = 0;
161 else if (!strcmp(buf, "all" )) d = ~0;
163 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
165 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
170 if (o->type == FF_OPT_TYPE_FLAGS) {
171 if (cmd == '+') d = av_get_int(obj, o->name, NULL) | (int64_t)d;
172 else if (cmd == '-') d = av_get_int(obj, o->name, NULL) &~(int64_t)d;
174 if (cmd == '+') d = notfirst*av_get_double(obj, o->name, NULL) + d;
175 else if (cmd == '-') d = notfirst*av_get_double(obj, o->name, NULL) - d;
178 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
189 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
191 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
196 return AVERROR_OPTION_NOT_FOUND;
198 return AVERROR(EINVAL);
200 dst = ((uint8_t*)obj) + o->offset;
202 case FF_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
203 case FF_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
204 case FF_OPT_TYPE_FLAGS:
205 case FF_OPT_TYPE_INT:
206 case FF_OPT_TYPE_INT64:
207 case FF_OPT_TYPE_FLOAT:
208 case FF_OPT_TYPE_DOUBLE:
209 case FF_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
212 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
213 return AVERROR(EINVAL);
216 static const AVOption *set_number(void *obj, const char *name, double num, int den, int64_t intnum)
218 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
224 dst = ((uint8_t*)obj) + o->offset;
225 if (write_number(obj, o, dst, num, den, intnum) < 0)
231 const AVOption *av_set_double(void *obj, const char *name, double n)
233 return set_number(obj, name, n, 1, 1);
236 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
238 return set_number(obj, name, n.num, n.den, 1);
241 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
243 return set_number(obj, name, 1, 1, n);
248 * @param buf a buffer which is used for returning non string values as strings, can be NULL
249 * @param buf_len allocated length in bytes of buf
251 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
253 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
259 if (o->type != FF_OPT_TYPE_STRING && (!buf || !buf_len))
262 dst= ((uint8_t*)obj) + o->offset;
263 if (o_out) *o_out= o;
266 case FF_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
267 case FF_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
268 case FF_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
269 case FF_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
270 case FF_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
271 case FF_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
272 case FF_OPT_TYPE_STRING: return *(void**)dst;
273 case FF_OPT_TYPE_BINARY:
274 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
275 if (len >= (buf_len + 1)/2) return NULL;
276 bin = *(uint8_t**)dst;
277 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
279 default: return NULL;
284 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum)
286 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
291 dst= ((uint8_t*)obj) + o->offset;
293 if (o_out) *o_out= o;
296 case FF_OPT_TYPE_FLAGS: *intnum= *(unsigned int*)dst;return 0;
297 case FF_OPT_TYPE_INT: *intnum= *(int *)dst;return 0;
298 case FF_OPT_TYPE_INT64: *intnum= *(int64_t*)dst;return 0;
299 case FF_OPT_TYPE_FLOAT: *num= *(float *)dst;return 0;
300 case FF_OPT_TYPE_DOUBLE: *num= *(double *)dst;return 0;
301 case FF_OPT_TYPE_RATIONAL: *intnum= ((AVRational*)dst)->num;
302 *den = ((AVRational*)dst)->den;
310 double av_get_double(void *obj, const char *name, const AVOption **o_out)
316 if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
318 return num*intnum/den;
321 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
327 if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
328 return (AVRational){0, 0};
329 if (num == 1.0 && (int)intnum == intnum)
330 return (AVRational){intnum, den};
332 return av_d2q(num*intnum/den, 1<<24);
335 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
341 if (get_number(obj, name, o_out, &num, &den, &intnum) < 0)
343 return num*intnum/den;
346 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
348 const AVOption *field = av_find_opt(obj, field_name, NULL, 0, 0);
349 const AVOption *flag = av_find_opt(obj, flag_name, NULL, 0, 0);
351 if (!field || !flag || flag->type != FF_OPT_TYPE_CONST)
353 return av_get_int(obj, field_name, NULL) & (int) flag->default_val.dbl;
356 static void opt_list(void *obj, void *av_log_obj, const char *unit,
357 int req_flags, int rej_flags)
359 const AVOption *opt=NULL;
361 while ((opt= av_next_option(obj, opt))) {
362 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
365 /* Don't print CONST's on level one.
366 * Don't print anything but CONST's on level two.
367 * Only print items from the requested unit.
369 if (!unit && opt->type==FF_OPT_TYPE_CONST)
371 else if (unit && opt->type!=FF_OPT_TYPE_CONST)
373 else if (unit && opt->type==FF_OPT_TYPE_CONST && strcmp(unit, opt->unit))
375 else if (unit && opt->type == FF_OPT_TYPE_CONST)
376 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
378 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
381 case FF_OPT_TYPE_FLAGS:
382 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
384 case FF_OPT_TYPE_INT:
385 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
387 case FF_OPT_TYPE_INT64:
388 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
390 case FF_OPT_TYPE_DOUBLE:
391 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
393 case FF_OPT_TYPE_FLOAT:
394 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
396 case FF_OPT_TYPE_STRING:
397 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
399 case FF_OPT_TYPE_RATIONAL:
400 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
402 case FF_OPT_TYPE_BINARY:
403 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
405 case FF_OPT_TYPE_CONST:
407 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
410 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
411 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
412 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
413 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
414 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
417 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
418 av_log(av_log_obj, AV_LOG_INFO, "\n");
419 if (opt->unit && opt->type != FF_OPT_TYPE_CONST) {
420 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
425 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
430 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
432 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
437 void av_opt_set_defaults(void *s)
439 #if FF_API_OLD_AVOPTIONS
440 av_opt_set_defaults2(s, 0, 0);
443 void av_opt_set_defaults2(void *s, int mask, int flags)
446 const AVOption *opt = NULL;
447 while ((opt = av_next_option(s, opt)) != NULL) {
448 #if FF_API_OLD_AVOPTIONS
449 if ((opt->flags & mask) != flags)
453 case FF_OPT_TYPE_CONST:
454 /* Nothing to be done here */
456 case FF_OPT_TYPE_FLAGS:
457 case FF_OPT_TYPE_INT: {
459 val = opt->default_val.dbl;
460 av_set_int(s, opt->name, val);
463 case FF_OPT_TYPE_INT64:
464 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
465 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
466 av_set_int(s, opt->name, opt->default_val.dbl);
468 case FF_OPT_TYPE_DOUBLE:
469 case FF_OPT_TYPE_FLOAT: {
471 val = opt->default_val.dbl;
472 av_set_double(s, opt->name, val);
475 case FF_OPT_TYPE_RATIONAL: {
477 val = av_d2q(opt->default_val.dbl, INT_MAX);
478 av_set_q(s, opt->name, val);
481 case FF_OPT_TYPE_STRING:
482 av_set_string3(s, opt->name, opt->default_val.str, 1, NULL);
484 case FF_OPT_TYPE_BINARY:
485 /* Cannot set default for binary */
488 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
494 * Store the value in the field in ctx that is named like key.
495 * ctx must be an AVClass context, storing is done using AVOptions.
497 * @param buf the string to parse, buf will be updated to point at the
498 * separator just after the parsed key/value pair
499 * @param key_val_sep a 0-terminated list of characters used to
500 * separate key from value
501 * @param pairs_sep a 0-terminated list of characters used to separate
502 * two pairs from each other
503 * @return 0 if the key/value pair has been successfully parsed and
504 * set, or a negative value corresponding to an AVERROR code in case
506 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
507 * the error code issued by av_set_string3() if the key/value pair
510 static int parse_key_value_pair(void *ctx, const char **buf,
511 const char *key_val_sep, const char *pairs_sep)
513 char *key = av_get_token(buf, key_val_sep);
517 if (*key && strspn(*buf, key_val_sep)) {
519 val = av_get_token(buf, pairs_sep);
521 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
523 return AVERROR(EINVAL);
526 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
528 ret = av_set_string3(ctx, key, val, 1, NULL);
529 if (ret == AVERROR_OPTION_NOT_FOUND)
530 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
537 int av_set_options_string(void *ctx, const char *opts,
538 const char *key_val_sep, const char *pairs_sep)
543 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
554 void av_opt_free(void *obj)
556 const AVOption *o = NULL;
557 while ((o = av_next_option(obj, o)))
558 if (o->type == FF_OPT_TYPE_STRING || o->type == FF_OPT_TYPE_BINARY)
559 av_freep((uint8_t *)obj + o->offset);
562 int av_opt_set_dict(void *obj, AVDictionary **options)
564 AVDictionaryEntry *t = NULL;
565 AVDictionary *tmp = NULL;
568 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
569 ret = av_set_string3(obj, t->key, t->value, 1, NULL);
570 if (ret == AVERROR_OPTION_NOT_FOUND)
571 av_dict_set(&tmp, t->key, t->value, 0);
573 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
578 av_dict_free(options);
583 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
584 int opt_flags, int search_flags)
586 AVClass *c = *(AVClass**)obj;
587 const AVOption *o = NULL;
589 if (c->opt_find && search_flags & AV_OPT_SEARCH_CHILDREN &&
590 (o = c->opt_find(obj, name, unit, opt_flags, search_flags)))
593 while (o = av_next_option(obj, o)) {
594 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
595 ((!unit && o->type != FF_OPT_TYPE_CONST) ||
596 (unit && o->unit && !strcmp(o->unit, unit))))
606 typedef struct TestContext
608 const AVClass *class;
616 #define OFFSET(x) offsetof(TestContext, x)
618 #define TEST_FLAG_COOL 01
619 #define TEST_FLAG_LAME 02
620 #define TEST_FLAG_MU 04
622 static const AVOption test_options[]= {
623 {"num", "set num", OFFSET(num), FF_OPT_TYPE_INT, {0}, 0, 100 },
624 {"toggle", "set toggle", OFFSET(toggle), FF_OPT_TYPE_INT, {0}, 0, 1 },
625 {"rational", "set rational", OFFSET(rational), FF_OPT_TYPE_RATIONAL, {0}, 0, 10 },
626 {"string", "set string", OFFSET(string), FF_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
627 {"flags", "set flags", OFFSET(flags), FF_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
628 {"cool", "set cool flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
629 {"lame", "set lame flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
630 {"mu", "set mu flag ", 0, FF_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
634 static const char *test_get_name(void *ctx)
639 static const AVClass test_class = {
649 printf("\nTesting av_set_options_string()\n");
651 TestContext test_ctx;
652 const char *options[] = {
668 "flags=+mu-lame : num=42: toggle=0",
669 "num=42 : string=blahblah",
670 "rational=0 : rational=1/2 : rational=1/-1",
674 test_ctx.class = &test_class;
675 av_opt_set_defaults(&test_ctx);
676 test_ctx.string = av_strdup("default");
678 av_log_set_level(AV_LOG_DEBUG);
680 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
681 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
682 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
683 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);