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>
35 #include "mathematics.h"
37 const AVOption *av_opt_next(void *obj, const AVOption *last)
39 AVClass *class = *(AVClass**)obj;
40 if (!last && class->option[0].name) return class->option;
41 if (last && last[1].name) return ++last;
45 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
48 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
49 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
50 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
51 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
52 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
53 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
54 *den = ((AVRational*)dst)->den;
57 return AVERROR(EINVAL);
60 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
62 if (o->max*den < num*intnum || o->min*den > num*intnum) {
63 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n",
64 num*intnum/den, o->name);
65 return AVERROR(ERANGE);
69 case AV_OPT_TYPE_FLAGS:
70 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
71 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
72 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
73 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
74 case AV_OPT_TYPE_RATIONAL:
75 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
76 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
79 return AVERROR(EINVAL);
84 static const double const_values[] = {
91 static const char * const const_names[] = {
98 static int hexchar2int(char c) {
99 if (c >= '0' && c <= '9') return c - '0';
100 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
101 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
105 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
107 int *lendst = (int *)(dst + 1);
109 int len = strlen(val);
115 return AVERROR(EINVAL);
118 ptr = bin = av_malloc(len);
120 int a = hexchar2int(*val++);
121 int b = hexchar2int(*val++);
122 if (a < 0 || b < 0) {
124 return AVERROR(EINVAL);
126 *ptr++ = (a << 4) | b;
134 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
137 *dst = av_strdup(val);
141 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
142 opt->type == AV_OPT_TYPE_CONST || \
143 opt->type == AV_OPT_TYPE_FLAGS || \
144 opt->type == AV_OPT_TYPE_INT) ? \
145 opt->default_val.i64 : opt->default_val.dbl)
147 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
149 int ret = 0, notfirst = 0;
157 if (*val == '+' || *val == '-')
160 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
165 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
166 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
167 d = DEFAULT_NUMVAL(o_named);
168 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
169 else if (!strcmp(buf, "max" )) d = o->max;
170 else if (!strcmp(buf, "min" )) d = o->min;
171 else if (!strcmp(buf, "none" )) d = 0;
172 else if (!strcmp(buf, "all" )) d = ~0;
174 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
176 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
181 if (o->type == AV_OPT_TYPE_FLAGS) {
182 read_number(o, dst, NULL, NULL, &intnum);
183 if (cmd == '+') d = intnum | (int64_t)d;
184 else if (cmd == '-') d = intnum &~(int64_t)d;
186 read_number(o, dst, &num, &den, &intnum);
187 if (cmd == '+') d = notfirst*num*intnum/den + d;
188 else if (cmd == '-') d = notfirst*num*intnum/den - d;
191 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
202 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
204 void *dst, *target_obj;
205 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
206 if (!o || !target_obj)
207 return AVERROR_OPTION_NOT_FOUND;
209 return AVERROR(EINVAL);
211 dst = ((uint8_t*)target_obj) + o->offset;
213 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
214 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
215 case AV_OPT_TYPE_FLAGS:
216 case AV_OPT_TYPE_INT:
217 case AV_OPT_TYPE_INT64:
218 case AV_OPT_TYPE_FLOAT:
219 case AV_OPT_TYPE_DOUBLE:
220 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
223 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
224 return AVERROR(EINVAL);
227 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
228 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
230 if (!o || o->type != opttype)\
231 return AVERROR(EINVAL);\
232 return set_string_number(obj, o, val, name ## _out);\
235 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
236 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
237 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
238 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
239 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
240 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
242 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
245 void *dst, *target_obj;
246 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
248 if (!o || !target_obj)
249 return AVERROR_OPTION_NOT_FOUND;
251 dst = ((uint8_t*)target_obj) + o->offset;
252 return write_number(obj, o, dst, num, den, intnum);
255 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
257 return set_number(obj, name, 1, 1, val, search_flags);
260 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
262 return set_number(obj, name, val, 1, 1, search_flags);
265 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
267 return set_number(obj, name, val.num, val.den, 1, search_flags);
270 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
273 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
278 if (!o || !target_obj)
279 return AVERROR_OPTION_NOT_FOUND;
281 if (o->type != AV_OPT_TYPE_BINARY)
282 return AVERROR(EINVAL);
284 ptr = av_malloc(len);
286 return AVERROR(ENOMEM);
288 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
289 lendst = (int *)(dst + 1);
294 memcpy(ptr, val, len);
299 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
301 void *dst, *target_obj;
302 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
303 uint8_t *bin, buf[128];
306 if (!o || !target_obj)
307 return AVERROR_OPTION_NOT_FOUND;
309 dst = (uint8_t*)target_obj + o->offset;
313 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
314 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
315 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
316 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
317 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
318 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
319 case AV_OPT_TYPE_STRING:
321 *out_val = av_strdup(*(uint8_t**)dst);
323 *out_val = av_strdup("");
325 case AV_OPT_TYPE_BINARY:
326 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
327 if ((uint64_t)len*2 + 1 > INT_MAX)
328 return AVERROR(EINVAL);
329 if (!(*out_val = av_malloc(len*2 + 1)))
330 return AVERROR(ENOMEM);
331 bin = *(uint8_t**)dst;
332 for (i = 0; i < len; i++)
333 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
336 return AVERROR(EINVAL);
339 if (ret >= sizeof(buf))
340 return AVERROR(EINVAL);
341 *out_val = av_strdup(buf);
345 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
348 void *dst, *target_obj;
349 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
350 if (!o || !target_obj)
353 dst = ((uint8_t*)target_obj) + o->offset;
355 if (o_out) *o_out= o;
357 return read_number(o, dst, num, den, intnum);
364 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
370 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
372 *out_val = num*intnum/den;
376 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
382 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
384 *out_val = num*intnum/den;
388 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
394 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
397 if (num == 1.0 && (int)intnum == intnum)
398 *out_val = (AVRational){intnum, den};
400 *out_val = av_d2q(num*intnum/den, 1<<24);
404 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
406 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
407 const AVOption *flag = av_opt_find(obj, flag_name,
408 field ? field->unit : NULL, 0, 0);
411 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
412 av_opt_get_int(obj, field_name, 0, &res) < 0)
414 return res & flag->default_val.i64;
417 static void opt_list(void *obj, void *av_log_obj, const char *unit,
418 int req_flags, int rej_flags)
420 const AVOption *opt=NULL;
422 while ((opt = av_opt_next(obj, opt))) {
423 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
426 /* Don't print CONST's on level one.
427 * Don't print anything but CONST's on level two.
428 * Only print items from the requested unit.
430 if (!unit && opt->type==AV_OPT_TYPE_CONST)
432 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
434 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
436 else if (unit && opt->type == AV_OPT_TYPE_CONST)
437 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
439 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
442 case AV_OPT_TYPE_FLAGS:
443 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
445 case AV_OPT_TYPE_INT:
446 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
448 case AV_OPT_TYPE_INT64:
449 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
451 case AV_OPT_TYPE_DOUBLE:
452 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
454 case AV_OPT_TYPE_FLOAT:
455 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
457 case AV_OPT_TYPE_STRING:
458 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
460 case AV_OPT_TYPE_RATIONAL:
461 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
463 case AV_OPT_TYPE_BINARY:
464 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
466 case AV_OPT_TYPE_CONST:
468 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
471 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
472 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
473 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
474 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
475 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
478 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
479 av_log(av_log_obj, AV_LOG_INFO, "\n");
480 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
481 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
486 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
491 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
493 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
498 void av_opt_set_defaults(void *s)
500 const AVOption *opt = NULL;
501 while ((opt = av_opt_next(s, opt)) != NULL) {
503 case AV_OPT_TYPE_CONST:
504 /* Nothing to be done here */
506 case AV_OPT_TYPE_FLAGS:
507 case AV_OPT_TYPE_INT:
508 case AV_OPT_TYPE_INT64:
509 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
511 case AV_OPT_TYPE_DOUBLE:
512 case AV_OPT_TYPE_FLOAT: {
514 val = opt->default_val.dbl;
515 av_opt_set_double(s, opt->name, val, 0);
518 case AV_OPT_TYPE_RATIONAL: {
520 val = av_d2q(opt->default_val.dbl, INT_MAX);
521 av_opt_set_q(s, opt->name, val, 0);
524 case AV_OPT_TYPE_STRING:
525 av_opt_set(s, opt->name, opt->default_val.str, 0);
527 case AV_OPT_TYPE_BINARY:
528 /* Cannot set default for binary */
531 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
537 * Store the value in the field in ctx that is named like key.
538 * ctx must be an AVClass context, storing is done using AVOptions.
540 * @param buf the string to parse, buf will be updated to point at the
541 * separator just after the parsed key/value pair
542 * @param key_val_sep a 0-terminated list of characters used to
543 * separate key from value
544 * @param pairs_sep a 0-terminated list of characters used to separate
545 * two pairs from each other
546 * @return 0 if the key/value pair has been successfully parsed and
547 * set, or a negative value corresponding to an AVERROR code in case
549 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
550 * the error code issued by av_opt_set() if the key/value pair
553 static int parse_key_value_pair(void *ctx, const char **buf,
554 const char *key_val_sep, const char *pairs_sep)
556 char *key = av_get_token(buf, key_val_sep);
560 if (*key && strspn(*buf, key_val_sep)) {
562 val = av_get_token(buf, pairs_sep);
564 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
566 return AVERROR(EINVAL);
569 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
571 ret = av_opt_set(ctx, key, val, 0);
572 if (ret == AVERROR_OPTION_NOT_FOUND)
573 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
580 int av_set_options_string(void *ctx, const char *opts,
581 const char *key_val_sep, const char *pairs_sep)
589 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
600 void av_opt_free(void *obj)
602 const AVOption *o = NULL;
603 while ((o = av_opt_next(obj, o)))
604 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
605 av_freep((uint8_t *)obj + o->offset);
608 int av_opt_set_dict(void *obj, AVDictionary **options)
610 AVDictionaryEntry *t = NULL;
611 AVDictionary *tmp = NULL;
614 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
615 ret = av_opt_set(obj, t->key, t->value, 0);
616 if (ret == AVERROR_OPTION_NOT_FOUND)
617 av_dict_set(&tmp, t->key, t->value, 0);
619 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
624 av_dict_free(options);
629 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
630 int opt_flags, int search_flags)
632 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
635 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
636 int opt_flags, int search_flags, void **target_obj)
638 const AVClass *c = *(AVClass**)obj;
639 const AVOption *o = NULL;
641 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
642 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
643 const AVClass *child = NULL;
644 while (child = av_opt_child_class_next(c, child))
645 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
649 while (child = av_opt_child_next(obj, child))
650 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
655 while (o = av_opt_next(obj, o)) {
656 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
657 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
658 (unit && o->unit && !strcmp(o->unit, unit)))) {
660 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
671 void *av_opt_child_next(void *obj, void *prev)
673 const AVClass *c = *(AVClass**)obj;
675 return c->child_next(obj, prev);
679 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
681 if (parent->child_class_next)
682 return parent->child_class_next(prev);
690 typedef struct TestContext
692 const AVClass *class;
700 #define OFFSET(x) offsetof(TestContext, x)
702 #define TEST_FLAG_COOL 01
703 #define TEST_FLAG_LAME 02
704 #define TEST_FLAG_MU 04
706 static const AVOption test_options[]= {
707 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 100 },
708 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
709 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10 },
710 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
711 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, 0, "flags" },
712 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
713 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
714 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
718 static const char *test_get_name(void *ctx)
723 static const AVClass test_class = {
733 printf("\nTesting av_set_options_string()\n");
735 TestContext test_ctx;
736 const char *options[] = {
752 "flags=+mu-lame : num=42: toggle=0",
753 "num=42 : string=blahblah",
754 "rational=0 : rational=1/2 : rational=1/-1",
758 test_ctx.class = &test_class;
759 av_opt_set_defaults(&test_ctx);
760 test_ctx.string = av_strdup("default");
762 av_log_set_level(AV_LOG_DEBUG);
764 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
765 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
766 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
767 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);