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 AVClass *class = *(AVClass**)obj;
60 if (!last && class->option[0].name) return class->option;
61 if (last && last[1].name) return ++last;
65 static int read_number(const AVOption *o, void *dst, double *num, int *den, int64_t *intnum)
68 case AV_OPT_TYPE_FLAGS: *intnum = *(unsigned int*)dst;return 0;
69 case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0;
70 case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0;
71 case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0;
72 case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0;
73 case AV_OPT_TYPE_RATIONAL: *intnum = ((AVRational*)dst)->num;
74 *den = ((AVRational*)dst)->den;
77 return AVERROR(EINVAL);
80 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
82 if (o->max*den < num*intnum || o->min*den > num*intnum) {
83 av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, o->name);
84 return AVERROR(ERANGE);
88 case AV_OPT_TYPE_FLAGS:
89 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
90 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
91 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
92 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
93 case AV_OPT_TYPE_RATIONAL:
94 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
95 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
98 return AVERROR(EINVAL);
103 static const double const_values[] = {
110 static const char * const const_names[] = {
117 static int hexchar2int(char c) {
118 if (c >= '0' && c <= '9') return c - '0';
119 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
120 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
124 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
126 int *lendst = (int *)(dst + 1);
128 int len = strlen(val);
134 return AVERROR(EINVAL);
137 ptr = bin = av_malloc(len);
139 int a = hexchar2int(*val++);
140 int b = hexchar2int(*val++);
141 if (a < 0 || b < 0) {
143 return AVERROR(EINVAL);
145 *ptr++ = (a << 4) | b;
153 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
156 *dst = av_strdup(val);
160 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
162 int ret = 0, notfirst = 0;
170 if (*val == '+' || *val == '-')
173 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
178 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
179 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
180 d = o_named->default_val.dbl;
181 else if (!strcmp(buf, "default")) d = o->default_val.dbl;
182 else if (!strcmp(buf, "max" )) d = o->max;
183 else if (!strcmp(buf, "min" )) d = o->min;
184 else if (!strcmp(buf, "none" )) d = 0;
185 else if (!strcmp(buf, "all" )) d = ~0;
187 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
189 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
194 if (o->type == AV_OPT_TYPE_FLAGS) {
195 read_number(o, dst, NULL, NULL, &intnum);
196 if (cmd == '+') d = intnum | (int64_t)d;
197 else if (cmd == '-') d = intnum &~(int64_t)d;
199 read_number(o, dst, &num, &den, &intnum);
200 if (cmd == '+') d = notfirst*num*intnum/den + d;
201 else if (cmd == '-') d = notfirst*num*intnum/den - d;
204 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
215 #if FF_API_OLD_AVOPTIONS
216 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
218 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
221 return av_opt_set(obj, name, val, 0);
225 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
227 void *dst, *target_obj;
228 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
229 if (!o || !target_obj)
230 return AVERROR_OPTION_NOT_FOUND;
232 return AVERROR(EINVAL);
234 dst = ((uint8_t*)target_obj) + o->offset;
236 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
237 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
238 case AV_OPT_TYPE_FLAGS:
239 case AV_OPT_TYPE_INT:
240 case AV_OPT_TYPE_INT64:
241 case AV_OPT_TYPE_FLOAT:
242 case AV_OPT_TYPE_DOUBLE:
243 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
246 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
247 return AVERROR(EINVAL);
250 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
251 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
253 if (!o || o->type != opttype)\
254 return AVERROR(EINVAL);\
255 return set_string_number(obj, o, val, name ## _out);\
258 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
259 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
260 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
261 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
262 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
263 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
265 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
268 void *dst, *target_obj;
269 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
271 if (!o || !target_obj)
272 return AVERROR_OPTION_NOT_FOUND;
274 dst = ((uint8_t*)target_obj) + o->offset;
275 return write_number(obj, o, dst, num, den, intnum);
278 #if FF_API_OLD_AVOPTIONS
279 const AVOption *av_set_double(void *obj, const char *name, double n)
281 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
282 if (set_number(obj, name, n, 1, 1, 0) < 0)
287 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
289 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
290 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
295 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
297 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
298 if (set_number(obj, name, 1, 1, n, 0) < 0)
304 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
306 return set_number(obj, name, 1, 1, val, search_flags);
309 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
311 return set_number(obj, name, val, 1, 1, search_flags);
314 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
316 return set_number(obj, name, val.num, val.den, 1, search_flags);
319 #if FF_API_OLD_AVOPTIONS
322 * @param buf a buffer which is used for returning non string values as strings, can be NULL
323 * @param buf_len allocated length in bytes of buf
325 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
327 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
333 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
336 dst= ((uint8_t*)obj) + o->offset;
337 if (o_out) *o_out= o;
340 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
341 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
342 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
343 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
344 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
345 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
346 case AV_OPT_TYPE_STRING: return *(void**)dst;
347 case AV_OPT_TYPE_BINARY:
348 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
349 if (len >= (buf_len + 1)/2) return NULL;
350 bin = *(uint8_t**)dst;
351 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
353 default: return NULL;
359 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
361 void *dst, *target_obj;
362 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
363 uint8_t *bin, buf[128];
366 if (!o || !target_obj)
367 return AVERROR_OPTION_NOT_FOUND;
369 dst = (uint8_t*)target_obj + o->offset;
373 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
374 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
375 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
376 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
377 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
378 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
379 case AV_OPT_TYPE_STRING:
381 *out_val = av_strdup(*(uint8_t**)dst);
383 *out_val = av_strdup("");
385 case AV_OPT_TYPE_BINARY:
386 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
387 if ((uint64_t)len*2 + 1 > INT_MAX)
388 return AVERROR(EINVAL);
389 if (!(*out_val = av_malloc(len*2 + 1)))
390 return AVERROR(ENOMEM);
391 bin = *(uint8_t**)dst;
392 for (i = 0; i < len; i++)
393 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
396 return AVERROR(EINVAL);
399 if (ret >= sizeof(buf))
400 return AVERROR(EINVAL);
401 *out_val = av_strdup(buf);
405 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
408 void *dst, *target_obj;
409 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
410 if (!o || !target_obj)
413 dst = ((uint8_t*)target_obj) + o->offset;
415 if (o_out) *o_out= o;
417 return read_number(o, dst, num, den, intnum);
424 #if FF_API_OLD_AVOPTIONS
425 double av_get_double(void *obj, const char *name, const AVOption **o_out)
431 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
433 return num*intnum/den;
436 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
442 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
443 return (AVRational){0, 0};
444 if (num == 1.0 && (int)intnum == intnum)
445 return (AVRational){intnum, den};
447 return av_d2q(num*intnum/den, 1<<24);
450 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
456 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
458 return num*intnum/den;
462 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
468 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
470 *out_val = num*intnum/den;
474 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
480 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
482 *out_val = num*intnum/den;
486 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
492 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
495 if (num == 1.0 && (int)intnum == intnum)
496 *out_val = (AVRational){intnum, den};
498 *out_val = av_d2q(num*intnum/den, 1<<24);
502 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
504 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
505 const AVOption *flag = av_opt_find(obj, flag_name,
506 field ? field->unit : NULL, 0, 0);
509 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
510 av_opt_get_int(obj, field_name, 0, &res) < 0)
512 return res & (int) flag->default_val.dbl;
515 static void opt_list(void *obj, void *av_log_obj, const char *unit,
516 int req_flags, int rej_flags)
518 const AVOption *opt=NULL;
520 while ((opt = av_opt_next(obj, opt))) {
521 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
524 /* Don't print CONST's on level one.
525 * Don't print anything but CONST's on level two.
526 * Only print items from the requested unit.
528 if (!unit && opt->type==AV_OPT_TYPE_CONST)
530 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
532 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
534 else if (unit && opt->type == AV_OPT_TYPE_CONST)
535 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
537 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
540 case AV_OPT_TYPE_FLAGS:
541 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
543 case AV_OPT_TYPE_INT:
544 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
546 case AV_OPT_TYPE_INT64:
547 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
549 case AV_OPT_TYPE_DOUBLE:
550 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
552 case AV_OPT_TYPE_FLOAT:
553 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
555 case AV_OPT_TYPE_STRING:
556 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
558 case AV_OPT_TYPE_RATIONAL:
559 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
561 case AV_OPT_TYPE_BINARY:
562 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
564 case AV_OPT_TYPE_CONST:
566 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
569 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
570 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
571 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
572 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
573 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
576 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
577 av_log(av_log_obj, AV_LOG_INFO, "\n");
578 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
579 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
584 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
589 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
591 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
596 void av_opt_set_defaults(void *s)
598 #if FF_API_OLD_AVOPTIONS
599 av_opt_set_defaults2(s, 0, 0);
602 void av_opt_set_defaults2(void *s, int mask, int flags)
605 const AVOption *opt = NULL;
606 while ((opt = av_opt_next(s, opt)) != NULL) {
607 #if FF_API_OLD_AVOPTIONS
608 if ((opt->flags & mask) != flags)
612 case AV_OPT_TYPE_CONST:
613 /* Nothing to be done here */
615 case AV_OPT_TYPE_FLAGS:
616 case AV_OPT_TYPE_INT: {
618 val = opt->default_val.dbl;
619 av_opt_set_int(s, opt->name, val, 0);
622 case AV_OPT_TYPE_INT64:
623 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
624 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
625 av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
627 case AV_OPT_TYPE_DOUBLE:
628 case AV_OPT_TYPE_FLOAT: {
630 val = opt->default_val.dbl;
631 av_opt_set_double(s, opt->name, val, 0);
634 case AV_OPT_TYPE_RATIONAL: {
636 val = av_d2q(opt->default_val.dbl, INT_MAX);
637 av_opt_set_q(s, opt->name, val, 0);
640 case AV_OPT_TYPE_STRING:
641 av_opt_set(s, opt->name, opt->default_val.str, 0);
643 case AV_OPT_TYPE_BINARY:
644 /* Cannot set default for binary */
647 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
653 * Store the value in the field in ctx that is named like key.
654 * ctx must be an AVClass context, storing is done using AVOptions.
656 * @param buf the string to parse, buf will be updated to point at the
657 * separator just after the parsed key/value pair
658 * @param key_val_sep a 0-terminated list of characters used to
659 * separate key from value
660 * @param pairs_sep a 0-terminated list of characters used to separate
661 * two pairs from each other
662 * @return 0 if the key/value pair has been successfully parsed and
663 * set, or a negative value corresponding to an AVERROR code in case
665 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
666 * the error code issued by av_opt_set() if the key/value pair
669 static int parse_key_value_pair(void *ctx, const char **buf,
670 const char *key_val_sep, const char *pairs_sep)
672 char *key = av_get_token(buf, key_val_sep);
676 if (*key && strspn(*buf, key_val_sep)) {
678 val = av_get_token(buf, pairs_sep);
680 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
682 return AVERROR(EINVAL);
685 av_log(ctx, AV_LOG_DEBUG, "Setting value '%s' for key '%s'\n", val, key);
687 ret = av_opt_set(ctx, key, val, 0);
688 if (ret == AVERROR_OPTION_NOT_FOUND)
689 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
696 int av_set_options_string(void *ctx, const char *opts,
697 const char *key_val_sep, const char *pairs_sep)
705 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
716 void av_opt_free(void *obj)
718 const AVOption *o = NULL;
719 while ((o = av_opt_next(obj, o)))
720 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
721 av_freep((uint8_t *)obj + o->offset);
724 int av_opt_set_dict(void *obj, AVDictionary **options)
726 AVDictionaryEntry *t = NULL;
727 AVDictionary *tmp = NULL;
730 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
731 ret = av_opt_set(obj, t->key, t->value, 0);
732 if (ret == AVERROR_OPTION_NOT_FOUND)
733 av_dict_set(&tmp, t->key, t->value, 0);
735 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
740 av_dict_free(options);
745 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
746 int opt_flags, int search_flags)
748 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
751 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
752 int opt_flags, int search_flags, void **target_obj)
754 const AVClass *c = *(AVClass**)obj;
755 const AVOption *o = NULL;
757 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
758 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
759 const AVClass *child = NULL;
760 while (child = av_opt_child_class_next(c, child))
761 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
765 while (child = av_opt_child_next(obj, child))
766 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
771 while (o = av_opt_next(obj, o)) {
772 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
773 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
774 (unit && o->unit && !strcmp(o->unit, unit)))) {
776 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
787 void *av_opt_child_next(void *obj, void *prev)
789 const AVClass *c = *(AVClass**)obj;
791 return c->child_next(obj, prev);
795 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
797 if (parent->child_class_next)
798 return parent->child_class_next(prev);
806 typedef struct TestContext
808 const AVClass *class;
816 #define OFFSET(x) offsetof(TestContext, x)
818 #define TEST_FLAG_COOL 01
819 #define TEST_FLAG_LAME 02
820 #define TEST_FLAG_MU 04
822 static const AVOption test_options[]= {
823 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
824 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
825 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
826 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
827 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
828 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
829 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
830 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
834 static const char *test_get_name(void *ctx)
839 static const AVClass test_class = {
849 printf("\nTesting av_set_options_string()\n");
851 TestContext test_ctx;
852 const char *options[] = {
868 "flags=+mu-lame : num=42: toggle=0",
869 "num=42 : string=blahblah",
870 "rational=0 : rational=1/2 : rational=1/-1",
874 test_ctx.class = &test_class;
875 av_opt_set_defaults(&test_ctx);
876 test_ctx.string = av_strdup("default");
878 av_log_set_level(AV_LOG_DEBUG);
880 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
881 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
882 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
883 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);