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"
38 //FIXME order them and do a bin search
39 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
41 AVClass *c= *(AVClass**)v; //FIXME silly way of storing AVClass
42 const AVOption *o= c->option;
44 for (; o && o->name; o++) {
45 if (!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags)
52 #if FF_API_OLD_AVOPTIONS
53 const AVOption *av_next_option(void *obj, const AVOption *last)
55 return av_opt_next(obj, last);
59 const AVOption *av_opt_next(void *obj, const AVOption *last)
61 AVClass *class = *(AVClass**)obj;
62 if (!last && class->option[0].name) return class->option;
63 if (last && last[1].name) return ++last;
67 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
70 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
71 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
72 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
73 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
74 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
75 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
76 *den = ((AVRational*)dst)->den;
79 return AVERROR(EINVAL);
82 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
84 if (o->max*den < num*intnum || o->min*den > num*intnum) {
85 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n",
86 num*intnum/den, o->name);
87 return AVERROR(ERANGE);
91 case AV_OPT_TYPE_FLAGS:
92 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
93 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
94 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
95 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
96 case AV_OPT_TYPE_RATIONAL:
97 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
98 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
101 return AVERROR(EINVAL);
106 static const double const_values[] = {
113 static const char * const const_names[] = {
120 static int hexchar2int(char c) {
121 if (c >= '0' && c <= '9') return c - '0';
122 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
123 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
127 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
129 int *lendst = (int *)(dst + 1);
131 int len = strlen(val);
137 return AVERROR(EINVAL);
140 ptr = bin = av_malloc(len);
142 int a = hexchar2int(*val++);
143 int b = hexchar2int(*val++);
144 if (a < 0 || b < 0) {
146 return AVERROR(EINVAL);
148 *ptr++ = (a << 4) | b;
156 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
159 *dst = av_strdup(val);
163 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
164 opt->type == AV_OPT_TYPE_CONST) ? \
165 opt->default_val.i64 : opt->default_val.dbl)
167 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
169 int ret = 0, notfirst = 0;
177 if (*val == '+' || *val == '-')
180 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
185 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
186 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
187 d = DEFAULT_NUMVAL(o_named);
188 else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
189 else if (!strcmp(buf, "max" )) d = o->max;
190 else if (!strcmp(buf, "min" )) d = o->min;
191 else if (!strcmp(buf, "none" )) d = 0;
192 else if (!strcmp(buf, "all" )) d = ~0;
194 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
196 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
201 if (o->type == AV_OPT_TYPE_FLAGS) {
202 read_number(o, dst, NULL, NULL, &intnum);
203 if (cmd == '+') d = intnum | (int64_t)d;
204 else if (cmd == '-') d = intnum &~(int64_t)d;
206 read_number(o, dst, &num, &den, &intnum);
207 if (cmd == '+') d = notfirst*num*intnum/den + d;
208 else if (cmd == '-') d = notfirst*num*intnum/den - d;
211 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
222 #if FF_API_OLD_AVOPTIONS
223 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
225 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
228 return av_opt_set(obj, name, val, 0);
232 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
234 void *dst, *target_obj;
235 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
236 if (!o || !target_obj)
237 return AVERROR_OPTION_NOT_FOUND;
239 return AVERROR(EINVAL);
241 dst = ((uint8_t*)target_obj) + o->offset;
243 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
244 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
245 case AV_OPT_TYPE_FLAGS:
246 case AV_OPT_TYPE_INT:
247 case AV_OPT_TYPE_INT64:
248 case AV_OPT_TYPE_FLOAT:
249 case AV_OPT_TYPE_DOUBLE:
250 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
253 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
254 return AVERROR(EINVAL);
257 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
258 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
260 if (!o || o->type != opttype)\
261 return AVERROR(EINVAL);\
262 return set_string_number(obj, o, val, name ## _out);\
265 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
266 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
267 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
268 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
269 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
270 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
272 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
275 void *dst, *target_obj;
276 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 dst = ((uint8_t*)target_obj) + o->offset;
282 return write_number(obj, o, dst, num, den, intnum);
285 #if FF_API_OLD_AVOPTIONS
286 const AVOption *av_set_double(void *obj, const char *name, double n)
288 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
289 if (set_number(obj, name, n, 1, 1, 0) < 0)
294 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
296 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
297 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
302 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
304 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
305 if (set_number(obj, name, 1, 1, n, 0) < 0)
311 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
313 return set_number(obj, name, 1, 1, val, search_flags);
316 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
318 return set_number(obj, name, val, 1, 1, search_flags);
321 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
323 return set_number(obj, name, val.num, val.den, 1, search_flags);
326 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
329 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
334 if (!o || !target_obj)
335 return AVERROR_OPTION_NOT_FOUND;
337 if (o->type != AV_OPT_TYPE_BINARY)
338 return AVERROR(EINVAL);
340 ptr = av_malloc(len);
342 return AVERROR(ENOMEM);
344 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
345 lendst = (int *)(dst + 1);
350 memcpy(ptr, val, len);
355 #if FF_API_OLD_AVOPTIONS
358 * @param buf a buffer which is used for returning non string values as strings, can be NULL
359 * @param buf_len allocated length in bytes of buf
361 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
363 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
369 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
372 dst= ((uint8_t*)obj) + o->offset;
373 if (o_out) *o_out= o;
376 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
377 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
378 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
379 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
380 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
381 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
382 case AV_OPT_TYPE_STRING: return *(void**)dst;
383 case AV_OPT_TYPE_BINARY:
384 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
385 if (len >= (buf_len + 1)/2) return NULL;
386 bin = *(uint8_t**)dst;
387 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
389 default: return NULL;
395 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
397 void *dst, *target_obj;
398 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
399 uint8_t *bin, buf[128];
402 if (!o || !target_obj)
403 return AVERROR_OPTION_NOT_FOUND;
405 dst = (uint8_t*)target_obj + o->offset;
409 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
410 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
411 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
412 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
413 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
414 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
415 case AV_OPT_TYPE_STRING:
417 *out_val = av_strdup(*(uint8_t**)dst);
419 *out_val = av_strdup("");
421 case AV_OPT_TYPE_BINARY:
422 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
423 if ((uint64_t)len*2 + 1 > INT_MAX)
424 return AVERROR(EINVAL);
425 if (!(*out_val = av_malloc(len*2 + 1)))
426 return AVERROR(ENOMEM);
427 bin = *(uint8_t**)dst;
428 for (i = 0; i < len; i++)
429 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
432 return AVERROR(EINVAL);
435 if (ret >= sizeof(buf))
436 return AVERROR(EINVAL);
437 *out_val = av_strdup(buf);
441 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
444 void *dst, *target_obj;
445 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
446 if (!o || !target_obj)
449 dst = ((uint8_t*)target_obj) + o->offset;
451 if (o_out) *o_out= o;
453 return read_number(o, dst, num, den, intnum);
460 #if FF_API_OLD_AVOPTIONS
461 double av_get_double(void *obj, const char *name, const AVOption **o_out)
467 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
469 return num*intnum/den;
472 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
478 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
479 return (AVRational){0, 0};
480 if (num == 1.0 && (int)intnum == intnum)
481 return (AVRational){intnum, den};
483 return av_d2q(num*intnum/den, 1<<24);
486 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
492 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
494 return num*intnum/den;
498 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
504 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
506 *out_val = num*intnum/den;
510 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
516 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
518 *out_val = num*intnum/den;
522 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
528 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
531 if (num == 1.0 && (int)intnum == intnum)
532 *out_val = (AVRational){intnum, den};
534 *out_val = av_d2q(num*intnum/den, 1<<24);
538 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
540 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
541 const AVOption *flag = av_opt_find(obj, flag_name,
542 field ? field->unit : NULL, 0, 0);
545 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
546 av_opt_get_int(obj, field_name, 0, &res) < 0)
548 return res & flag->default_val.i64;
551 static void opt_list(void *obj, void *av_log_obj, const char *unit,
552 int req_flags, int rej_flags)
554 const AVOption *opt=NULL;
556 while ((opt = av_opt_next(obj, opt))) {
557 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
560 /* Don't print CONST's on level one.
561 * Don't print anything but CONST's on level two.
562 * Only print items from the requested unit.
564 if (!unit && opt->type==AV_OPT_TYPE_CONST)
566 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
568 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
570 else if (unit && opt->type == AV_OPT_TYPE_CONST)
571 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
573 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
576 case AV_OPT_TYPE_FLAGS:
577 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
579 case AV_OPT_TYPE_INT:
580 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
582 case AV_OPT_TYPE_INT64:
583 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
585 case AV_OPT_TYPE_DOUBLE:
586 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
588 case AV_OPT_TYPE_FLOAT:
589 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
591 case AV_OPT_TYPE_STRING:
592 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
594 case AV_OPT_TYPE_RATIONAL:
595 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
597 case AV_OPT_TYPE_BINARY:
598 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
600 case AV_OPT_TYPE_CONST:
602 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
605 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
606 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
607 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
608 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
609 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
612 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
613 av_log(av_log_obj, AV_LOG_INFO, "\n");
614 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
615 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
620 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
625 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
627 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
632 void av_opt_set_defaults(void *s)
634 #if FF_API_OLD_AVOPTIONS
635 av_opt_set_defaults2(s, 0, 0);
638 void av_opt_set_defaults2(void *s, int mask, int flags)
641 const AVOption *opt = NULL;
642 while ((opt = av_opt_next(s, opt)) != NULL) {
643 #if FF_API_OLD_AVOPTIONS
644 if ((opt->flags & mask) != flags)
648 case AV_OPT_TYPE_CONST:
649 /* Nothing to be done here */
651 case AV_OPT_TYPE_FLAGS:
652 case AV_OPT_TYPE_INT: {
654 val = opt->default_val.dbl;
655 av_opt_set_int(s, opt->name, val, 0);
658 case AV_OPT_TYPE_INT64:
659 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
661 case AV_OPT_TYPE_DOUBLE:
662 case AV_OPT_TYPE_FLOAT: {
664 val = opt->default_val.dbl;
665 av_opt_set_double(s, opt->name, val, 0);
668 case AV_OPT_TYPE_RATIONAL: {
670 val = av_d2q(opt->default_val.dbl, INT_MAX);
671 av_opt_set_q(s, opt->name, val, 0);
674 case AV_OPT_TYPE_STRING:
675 av_opt_set(s, opt->name, opt->default_val.str, 0);
677 case AV_OPT_TYPE_BINARY:
678 /* Cannot set default for binary */
681 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
687 * Store the value in the field in ctx that is named like key.
688 * ctx must be an AVClass context, storing is done using AVOptions.
690 * @param buf the string to parse, buf will be updated to point at the
691 * separator just after the parsed key/value pair
692 * @param key_val_sep a 0-terminated list of characters used to
693 * separate key from value
694 * @param pairs_sep a 0-terminated list of characters used to separate
695 * two pairs from each other
696 * @return 0 if the key/value pair has been successfully parsed and
697 * set, or a negative value corresponding to an AVERROR code in case
699 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
700 * the error code issued by av_opt_set() if the key/value pair
703 static int parse_key_value_pair(void *ctx, const char **buf,
704 const char *key_val_sep, const char *pairs_sep)
706 char *key = av_get_token(buf, key_val_sep);
710 if (*key && strspn(*buf, key_val_sep)) {
712 val = av_get_token(buf, pairs_sep);
714 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
716 return AVERROR(EINVAL);
719 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
721 ret = av_opt_set(ctx, key, val, 0);
722 if (ret == AVERROR_OPTION_NOT_FOUND)
723 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
730 int av_set_options_string(void *ctx, const char *opts,
731 const char *key_val_sep, const char *pairs_sep)
739 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
750 void av_opt_free(void *obj)
752 const AVOption *o = NULL;
753 while ((o = av_opt_next(obj, o)))
754 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
755 av_freep((uint8_t *)obj + o->offset);
758 int av_opt_set_dict(void *obj, AVDictionary **options)
760 AVDictionaryEntry *t = NULL;
761 AVDictionary *tmp = NULL;
764 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
765 ret = av_opt_set(obj, t->key, t->value, 0);
766 if (ret == AVERROR_OPTION_NOT_FOUND)
767 av_dict_set(&tmp, t->key, t->value, 0);
769 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
774 av_dict_free(options);
779 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
780 int opt_flags, int search_flags)
782 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
785 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
786 int opt_flags, int search_flags, void **target_obj)
788 const AVClass *c = *(AVClass**)obj;
789 const AVOption *o = NULL;
791 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
792 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
793 const AVClass *child = NULL;
794 while (child = av_opt_child_class_next(c, child))
795 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
799 while (child = av_opt_child_next(obj, child))
800 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
805 while (o = av_opt_next(obj, o)) {
806 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
807 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
808 (unit && o->unit && !strcmp(o->unit, unit)))) {
810 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
821 void *av_opt_child_next(void *obj, void *prev)
823 const AVClass *c = *(AVClass**)obj;
825 return c->child_next(obj, prev);
829 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
831 if (parent->child_class_next)
832 return parent->child_class_next(prev);
840 typedef struct TestContext
842 const AVClass *class;
850 #define OFFSET(x) offsetof(TestContext, x)
852 #define TEST_FLAG_COOL 01
853 #define TEST_FLAG_LAME 02
854 #define TEST_FLAG_MU 04
856 static const AVOption test_options[]= {
857 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
858 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
859 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
860 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
861 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
862 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
863 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
864 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
868 static const char *test_get_name(void *ctx)
873 static const AVClass test_class = {
883 printf("\nTesting av_set_options_string()\n");
885 TestContext test_ctx;
886 const char *options[] = {
902 "flags=+mu-lame : num=42: toggle=0",
903 "num=42 : string=blahblah",
904 "rational=0 : rational=1/2 : rational=1/-1",
908 test_ctx.class = &test_class;
909 av_opt_set_defaults(&test_ctx);
910 test_ctx.string = av_strdup("default");
912 av_log_set_level(AV_LOG_DEBUG);
914 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
915 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
916 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
917 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);