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 #if FF_API_OLD_AVOPTIONS
51 const AVOption *av_next_option(void *obj, const AVOption *last)
53 return av_opt_next(obj, last);
57 const AVOption *av_opt_next(void *obj, const AVOption *last)
59 if (last && last[1].name) return ++last;
60 else if (last) return NULL;
61 else return (*(AVClass**)obj)->option;
64 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
67 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
68 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
69 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
70 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
71 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
72 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
73 *den = ((AVRational*)dst)->den;
76 return AVERROR(EINVAL);
79 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
81 if (o->max*den < num*intnum || o->min*den > num*intnum) {
82 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, o->name);
83 return AVERROR(ERANGE);
87 case AV_OPT_TYPE_FLAGS:
88 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
89 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
90 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
91 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
92 case AV_OPT_TYPE_RATIONAL:
93 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
94 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
97 return AVERROR(EINVAL);
102 static const double const_values[] = {
109 static const char * const const_names[] = {
116 static int hexchar2int(char c) {
117 if (c >= '0' && c <= '9') return c - '0';
118 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
119 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
123 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
125 int *lendst = (int *)(dst + 1);
127 int len = strlen(val);
133 return AVERROR(EINVAL);
136 ptr = bin = av_malloc(len);
138 int a = hexchar2int(*val++);
139 int b = hexchar2int(*val++);
140 if (a < 0 || b < 0) {
142 return AVERROR(EINVAL);
144 *ptr++ = (a << 4) | b;
152 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
155 *dst = av_strdup(val);
159 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
161 int ret = 0, notfirst = 0;
169 if (*val == '+' || *val == '-')
172 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
177 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
178 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
179 d = o_named->default_val.dbl;
180 else if (!strcmp(buf, "default")) d = o->default_val.dbl;
181 else if (!strcmp(buf, "max" )) d = o->max;
182 else if (!strcmp(buf, "min" )) d = o->min;
183 else if (!strcmp(buf, "none" )) d = 0;
184 else if (!strcmp(buf, "all" )) d = ~0;
186 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
188 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
193 if (o->type == AV_OPT_TYPE_FLAGS) {
194 read_number(o, dst, NULL, NULL, &intnum);
195 if (cmd == '+') d = intnum | (int64_t)d;
196 else if (cmd == '-') d = intnum &~(int64_t)d;
198 read_number(o, dst, &num, &den, &intnum);
199 if (cmd == '+') d = notfirst*num*intnum/den + d;
200 else if (cmd == '-') d = notfirst*num*intnum/den - d;
203 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
214 #if FF_API_OLD_AVOPTIONS
215 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
217 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
220 return av_opt_set(obj, name, val, 0);
224 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
226 void *dst, *target_obj;
227 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
228 if (!o || !target_obj)
229 return AVERROR_OPTION_NOT_FOUND;
231 return AVERROR(EINVAL);
233 dst = ((uint8_t*)target_obj) + o->offset;
235 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
236 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
237 case AV_OPT_TYPE_FLAGS:
238 case AV_OPT_TYPE_INT:
239 case AV_OPT_TYPE_INT64:
240 case AV_OPT_TYPE_FLOAT:
241 case AV_OPT_TYPE_DOUBLE:
242 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
245 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
246 return AVERROR(EINVAL);
249 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
250 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
252 if (!o || o->type != opttype)\
253 return AVERROR(EINVAL);\
254 return set_string_number(obj, o, val, name ## _out);\
257 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
258 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
259 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
260 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
261 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
262 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
264 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
267 void *dst, *target_obj;
268 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
270 if (!o || !target_obj)
271 return AVERROR_OPTION_NOT_FOUND;
273 dst = ((uint8_t*)target_obj) + o->offset;
274 return write_number(obj, o, dst, num, den, intnum);
277 #if FF_API_OLD_AVOPTIONS
278 const AVOption *av_set_double(void *obj, const char *name, double n)
280 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
281 if (set_number(obj, name, n, 1, 1, 0) < 0)
286 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
288 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
289 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
294 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
296 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
297 if (set_number(obj, name, 1, 1, n, 0) < 0)
303 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
305 return set_number(obj, name, 1, 1, val, search_flags);
308 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
310 return set_number(obj, name, val, 1, 1, search_flags);
313 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
315 return set_number(obj, name, val.num, val.den, 1, search_flags);
318 #if FF_API_OLD_AVOPTIONS
321 * @param buf a buffer which is used for returning non string values as strings, can be NULL
322 * @param buf_len allocated length in bytes of buf
324 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
326 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
332 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
335 dst= ((uint8_t*)obj) + o->offset;
336 if (o_out) *o_out= o;
339 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
340 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
341 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
342 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
343 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
344 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
345 case AV_OPT_TYPE_STRING: return *(void**)dst;
346 case AV_OPT_TYPE_BINARY:
347 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
348 if (len >= (buf_len + 1)/2) return NULL;
349 bin = *(uint8_t**)dst;
350 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
352 default: return NULL;
358 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
360 void *dst, *target_obj;
361 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
362 uint8_t *bin, buf[128];
365 if (!o || !target_obj)
366 return AVERROR_OPTION_NOT_FOUND;
368 dst = (uint8_t*)target_obj + o->offset;
372 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
373 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
374 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
375 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
376 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
377 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
378 case AV_OPT_TYPE_STRING:
380 *out_val = av_strdup(*(uint8_t**)dst);
382 *out_val = av_strdup("");
384 case AV_OPT_TYPE_BINARY:
385 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
386 if ((uint64_t)len*2 + 1 > INT_MAX)
387 return AVERROR(EINVAL);
388 if (!(*out_val = av_malloc(len*2 + 1)))
389 return AVERROR(ENOMEM);
390 bin = *(uint8_t**)dst;
391 for (i = 0; i < len; i++)
392 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
395 return AVERROR(EINVAL);
398 if (ret >= sizeof(buf))
399 return AVERROR(EINVAL);
400 *out_val = av_strdup(buf);
404 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
407 void *dst, *target_obj;
408 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
409 if (!o || !target_obj)
412 dst = ((uint8_t*)target_obj) + o->offset;
414 if (o_out) *o_out= o;
416 return read_number(o, dst, num, den, intnum);
423 #if FF_API_OLD_AVOPTIONS
424 double av_get_double(void *obj, const char *name, const AVOption **o_out)
430 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
432 return num*intnum/den;
435 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
441 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
442 return (AVRational){0, 0};
443 if (num == 1.0 && (int)intnum == intnum)
444 return (AVRational){intnum, den};
446 return av_d2q(num*intnum/den, 1<<24);
449 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
455 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
457 return num*intnum/den;
461 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
467 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
469 *out_val = num*intnum/den;
473 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
479 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
481 *out_val = num*intnum/den;
485 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
491 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
494 if (num == 1.0 && (int)intnum == intnum)
495 *out_val = (AVRational){intnum, den};
497 *out_val = av_d2q(num*intnum/den, 1<<24);
501 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
503 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
504 const AVOption *flag = av_opt_find(obj, flag_name, NULL, 0, 0);
507 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
508 av_opt_get_int(obj, field_name, 0, &res) < 0)
510 return res & (int) flag->default_val.dbl;
513 static void opt_list(void *obj, void *av_log_obj, const char *unit,
514 int req_flags, int rej_flags)
516 const AVOption *opt=NULL;
518 while ((opt = av_opt_next(obj, opt))) {
519 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
522 /* Don't print CONST's on level one.
523 * Don't print anything but CONST's on level two.
524 * Only print items from the requested unit.
526 if (!unit && opt->type==AV_OPT_TYPE_CONST)
528 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
530 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
532 else if (unit && opt->type == AV_OPT_TYPE_CONST)
533 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
535 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
538 case AV_OPT_TYPE_FLAGS:
539 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
541 case AV_OPT_TYPE_INT:
542 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
544 case AV_OPT_TYPE_INT64:
545 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
547 case AV_OPT_TYPE_DOUBLE:
548 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
550 case AV_OPT_TYPE_FLOAT:
551 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
553 case AV_OPT_TYPE_STRING:
554 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
556 case AV_OPT_TYPE_RATIONAL:
557 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
559 case AV_OPT_TYPE_BINARY:
560 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
562 case AV_OPT_TYPE_CONST:
564 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
567 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
568 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
569 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
570 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
571 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
574 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
575 av_log(av_log_obj, AV_LOG_INFO, "\n");
576 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
577 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
582 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
587 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
589 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
594 void av_opt_set_defaults(void *s)
596 #if FF_API_OLD_AVOPTIONS
597 av_opt_set_defaults2(s, 0, 0);
600 void av_opt_set_defaults2(void *s, int mask, int flags)
603 const AVOption *opt = NULL;
604 while ((opt = av_opt_next(s, opt)) != NULL) {
605 #if FF_API_OLD_AVOPTIONS
606 if ((opt->flags & mask) != flags)
610 case AV_OPT_TYPE_CONST:
611 /* Nothing to be done here */
613 case AV_OPT_TYPE_FLAGS:
614 case AV_OPT_TYPE_INT: {
616 val = opt->default_val.dbl;
617 av_opt_set_int(s, opt->name, val, 0);
620 case AV_OPT_TYPE_INT64:
621 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
622 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
623 av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
625 case AV_OPT_TYPE_DOUBLE:
626 case AV_OPT_TYPE_FLOAT: {
628 val = opt->default_val.dbl;
629 av_opt_set_double(s, opt->name, val, 0);
632 case AV_OPT_TYPE_RATIONAL: {
634 val = av_d2q(opt->default_val.dbl, INT_MAX);
635 av_opt_set_q(s, opt->name, val, 0);
638 case AV_OPT_TYPE_STRING:
639 av_opt_set(s, opt->name, opt->default_val.str, 0);
641 case AV_OPT_TYPE_BINARY:
642 /* Cannot set default for binary */
645 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
651 * Store the value in the field in ctx that is named like key.
652 * ctx must be an AVClass context, storing is done using AVOptions.
654 * @param buf the string to parse, buf will be updated to point at the
655 * separator just after the parsed key/value pair
656 * @param key_val_sep a 0-terminated list of characters used to
657 * separate key from value
658 * @param pairs_sep a 0-terminated list of characters used to separate
659 * two pairs from each other
660 * @return 0 if the key/value pair has been successfully parsed and
661 * set, or a negative value corresponding to an AVERROR code in case
663 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
664 * the error code issued by av_opt_set() if the key/value pair
667 static int parse_key_value_pair(void *ctx, const char **buf,
668 const char *key_val_sep, const char *pairs_sep)
670 char *key = av_get_token(buf, key_val_sep);
674 if (*key && strspn(*buf, key_val_sep)) {
676 val = av_get_token(buf, pairs_sep);
678 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
680 return AVERROR(EINVAL);
683 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
685 ret = av_opt_set(ctx, key, val, 0);
686 if (ret == AVERROR_OPTION_NOT_FOUND)
687 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
694 int av_set_options_string(void *ctx, const char *opts,
695 const char *key_val_sep, const char *pairs_sep)
700 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
711 void av_opt_free(void *obj)
713 const AVOption *o = NULL;
714 while ((o = av_opt_next(obj, o)))
715 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
716 av_freep((uint8_t *)obj + o->offset);
719 int av_opt_set_dict(void *obj, AVDictionary **options)
721 AVDictionaryEntry *t = NULL;
722 AVDictionary *tmp = NULL;
725 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
726 ret = av_opt_set(obj, t->key, t->value, 0);
727 if (ret == AVERROR_OPTION_NOT_FOUND)
728 av_dict_set(&tmp, t->key, t->value, 0);
730 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
735 av_dict_free(options);
740 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
741 int opt_flags, int search_flags)
743 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
746 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
747 int opt_flags, int search_flags, void **target_obj)
749 const AVClass *c = *(AVClass**)obj;
750 const AVOption *o = NULL;
752 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
753 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
754 const AVClass *child = NULL;
755 while (child = av_opt_child_class_next(c, child))
756 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
760 while (child = av_opt_child_next(obj, child))
761 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
766 while (o = av_opt_next(obj, o)) {
767 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
768 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
769 (unit && o->unit && !strcmp(o->unit, unit)))) {
771 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
782 void *av_opt_child_next(void *obj, void *prev)
784 const AVClass *c = *(AVClass**)obj;
786 return c->child_next(obj, prev);
790 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
792 if (parent->child_class_next)
793 return parent->child_class_next(prev);
801 typedef struct TestContext
803 const AVClass *class;
811 #define OFFSET(x) offsetof(TestContext, x)
813 #define TEST_FLAG_COOL 01
814 #define TEST_FLAG_LAME 02
815 #define TEST_FLAG_MU 04
817 static const AVOption test_options[]= {
818 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
819 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
820 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
821 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
822 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
823 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
824 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
825 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
829 static const char *test_get_name(void *ctx)
834 static const AVClass test_class = {
844 printf("\nTesting av_set_options_string()\n");
846 TestContext test_ctx;
847 const char *options[] = {
863 "flags=+mu-lame : num=42: toggle=0",
864 "num=42 : string=blahblah",
865 "rational=0 : rational=1/2 : rational=1/-1",
869 test_ctx.class = &test_class;
870 av_opt_set_defaults(&test_ctx);
871 test_ctx.string = av_strdup("default");
873 av_log_set_level(AV_LOG_DEBUG);
875 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
876 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
877 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
878 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);