3 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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>
34 #include "parseutils.h"
37 //FIXME order them and do a bin search
38 const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mask, int flags)
40 const AVOption *o = NULL;
42 while ((o = av_next_option(v, 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;
76 case AV_OPT_TYPE_CONST: *num = o->default_val.dbl; return 0;
78 return AVERROR(EINVAL);
81 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
83 if (o->max*den < num*intnum || o->min*den > num*intnum) {
84 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range\n",
85 num*intnum/den, o->name);
86 return AVERROR(ERANGE);
90 case AV_OPT_TYPE_FLAGS:
91 case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break;
92 case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break;
93 case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break;
94 case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break;
95 case AV_OPT_TYPE_RATIONAL:
96 if ((int)num == num) *(AVRational*)dst= (AVRational){num*intnum, den};
97 else *(AVRational*)dst= av_d2q(num*intnum/den, 1<<24);
100 return AVERROR(EINVAL);
105 static const double const_values[] = {
112 static const char * const const_names[] = {
119 static int hexchar2int(char c) {
120 if (c >= '0' && c <= '9') return c - '0';
121 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
122 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
126 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
128 int *lendst = (int *)(dst + 1);
130 int len = strlen(val);
136 return AVERROR(EINVAL);
139 ptr = bin = av_malloc(len);
141 int a = hexchar2int(*val++);
142 int b = hexchar2int(*val++);
143 if (a < 0 || b < 0) {
145 return AVERROR(EINVAL);
147 *ptr++ = (a << 4) | b;
155 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
158 *dst = av_strdup(val);
162 static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
164 int ret = 0, notfirst = 0;
172 if (*val == '+' || *val == '-')
175 for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
180 const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
181 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
182 d = o_named->default_val.dbl;
183 else if (!strcmp(buf, "default")) d = o->default_val.dbl;
184 else if (!strcmp(buf, "max" )) d = o->max;
185 else if (!strcmp(buf, "min" )) d = o->min;
186 else if (!strcmp(buf, "none" )) d = 0;
187 else if (!strcmp(buf, "all" )) d = ~0;
189 int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
191 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
196 if (o->type == AV_OPT_TYPE_FLAGS) {
197 read_number(o, dst, NULL, NULL, &intnum);
198 if (cmd == '+') d = intnum | (int64_t)d;
199 else if (cmd == '-') d = intnum &~(int64_t)d;
201 read_number(o, dst, &num, &den, &intnum);
202 if (cmd == '+') d = notfirst*num*intnum/den + d;
203 else if (cmd == '-') d = notfirst*num*intnum/den - d;
206 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
217 #if FF_API_OLD_AVOPTIONS
218 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
220 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
223 return av_opt_set(obj, name, val, 0);
227 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
230 void *dst, *target_obj;
231 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
232 if (!o || !target_obj)
233 return AVERROR_OPTION_NOT_FOUND;
234 if (!val && o->type != AV_OPT_TYPE_STRING)
235 return AVERROR(EINVAL);
237 dst = ((uint8_t*)target_obj) + o->offset;
239 case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst);
240 case AV_OPT_TYPE_BINARY: return set_string_binary(obj, o, val, dst);
241 case AV_OPT_TYPE_FLAGS:
242 case AV_OPT_TYPE_INT:
243 case AV_OPT_TYPE_INT64:
244 case AV_OPT_TYPE_FLOAT:
245 case AV_OPT_TYPE_DOUBLE:
246 case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
247 case AV_OPT_TYPE_IMAGE_SIZE:
248 ret = av_parse_video_size(dst, ((int *)dst) + 1, val);
250 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
254 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
255 return AVERROR(EINVAL);
258 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
259 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
261 if (!o || o->type != opttype)\
262 return AVERROR(EINVAL);\
263 return set_string_number(obj, o, val, name ## _out);\
266 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
267 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
268 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
269 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
270 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
271 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
273 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
276 void *dst, *target_obj;
277 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
279 if (!o || !target_obj)
280 return AVERROR_OPTION_NOT_FOUND;
282 dst = ((uint8_t*)target_obj) + o->offset;
283 return write_number(obj, o, dst, num, den, intnum);
286 #if FF_API_OLD_AVOPTIONS
287 const AVOption *av_set_double(void *obj, const char *name, double n)
289 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
290 if (set_number(obj, name, n, 1, 1, 0) < 0)
295 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
297 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
298 if (set_number(obj, name, n.num, n.den, 1, 0) < 0)
303 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
305 const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
306 if (set_number(obj, name, 1, 1, n, 0) < 0)
312 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
314 return set_number(obj, name, 1, 1, val, search_flags);
317 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
319 return set_number(obj, name, val, 1, 1, search_flags);
322 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
324 return set_number(obj, name, val.num, val.den, 1, search_flags);
327 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
330 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
335 if (!o || !target_obj)
336 return AVERROR_OPTION_NOT_FOUND;
338 if (o->type != AV_OPT_TYPE_BINARY)
339 return AVERROR(EINVAL);
341 ptr = av_malloc(len);
343 return AVERROR(ENOMEM);
345 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
346 lendst = (int *)(dst + 1);
351 memcpy(ptr, val, len);
356 #if FF_API_OLD_AVOPTIONS
359 * @param buf a buffer which is used for returning non string values as strings, can be NULL
360 * @param buf_len allocated length in bytes of buf
362 const char *av_get_string(void *obj, const char *name, const AVOption **o_out, char *buf, int buf_len)
364 const AVOption *o = av_opt_find(obj, name, NULL, 0, AV_OPT_SEARCH_CHILDREN);
370 if (o->type != AV_OPT_TYPE_STRING && (!buf || !buf_len))
373 dst= ((uint8_t*)obj) + o->offset;
374 if (o_out) *o_out= o;
377 case AV_OPT_TYPE_FLAGS: snprintf(buf, buf_len, "0x%08X",*(int *)dst);break;
378 case AV_OPT_TYPE_INT: snprintf(buf, buf_len, "%d" , *(int *)dst);break;
379 case AV_OPT_TYPE_INT64: snprintf(buf, buf_len, "%"PRId64, *(int64_t*)dst);break;
380 case AV_OPT_TYPE_FLOAT: snprintf(buf, buf_len, "%f" , *(float *)dst);break;
381 case AV_OPT_TYPE_DOUBLE: snprintf(buf, buf_len, "%f" , *(double *)dst);break;
382 case AV_OPT_TYPE_RATIONAL: snprintf(buf, buf_len, "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
383 case AV_OPT_TYPE_CONST: snprintf(buf, buf_len, "%f" , o->default_val.dbl);break;
384 case AV_OPT_TYPE_STRING: return *(void**)dst;
385 case AV_OPT_TYPE_BINARY:
386 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
387 if (len >= (buf_len + 1)/2) return NULL;
388 bin = *(uint8_t**)dst;
389 for (i = 0; i < len; i++) snprintf(buf + i*2, 3, "%02X", bin[i]);
391 default: return NULL;
397 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
399 void *dst, *target_obj;
400 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
401 uint8_t *bin, buf[128];
404 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
405 return AVERROR_OPTION_NOT_FOUND;
407 dst = (uint8_t*)target_obj + o->offset;
411 case AV_OPT_TYPE_FLAGS: ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);break;
412 case AV_OPT_TYPE_INT: ret = snprintf(buf, sizeof(buf), "%d" , *(int *)dst);break;
413 case AV_OPT_TYPE_INT64: ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
414 case AV_OPT_TYPE_FLOAT: ret = snprintf(buf, sizeof(buf), "%f" , *(float *)dst);break;
415 case AV_OPT_TYPE_DOUBLE: ret = snprintf(buf, sizeof(buf), "%f" , *(double *)dst);break;
416 case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
417 case AV_OPT_TYPE_CONST: ret = snprintf(buf, sizeof(buf), "%f" , o->default_val.dbl);break;
418 case AV_OPT_TYPE_STRING:
420 *out_val = av_strdup(*(uint8_t**)dst);
422 *out_val = av_strdup("");
424 case AV_OPT_TYPE_BINARY:
425 len = *(int*)(((uint8_t *)dst) + sizeof(uint8_t *));
426 if ((uint64_t)len*2 + 1 > INT_MAX)
427 return AVERROR(EINVAL);
428 if (!(*out_val = av_malloc(len*2 + 1)))
429 return AVERROR(ENOMEM);
430 bin = *(uint8_t**)dst;
431 for (i = 0; i < len; i++)
432 snprintf(*out_val + i*2, 3, "%02X", bin[i]);
434 case AV_OPT_TYPE_IMAGE_SIZE:
435 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
438 return AVERROR(EINVAL);
441 if (ret >= sizeof(buf))
442 return AVERROR(EINVAL);
443 *out_val = av_strdup(buf);
447 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
450 void *dst, *target_obj;
451 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
452 if (!o || !target_obj)
455 dst = ((uint8_t*)target_obj) + o->offset;
457 if (o_out) *o_out= o;
459 return read_number(o, dst, num, den, intnum);
466 #if FF_API_OLD_AVOPTIONS
467 double av_get_double(void *obj, const char *name, const AVOption **o_out)
473 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
475 return num*intnum/den;
478 AVRational av_get_q(void *obj, const char *name, const AVOption **o_out)
484 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
485 return (AVRational){0, 0};
486 if (num == 1.0 && (int)intnum == intnum)
487 return (AVRational){intnum, den};
489 return av_d2q(num*intnum/den, 1<<24);
492 int64_t av_get_int(void *obj, const char *name, const AVOption **o_out)
498 if (get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
500 return num*intnum/den;
504 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
510 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
512 *out_val = num*intnum/den;
516 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
522 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
524 *out_val = num*intnum/den;
528 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
534 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
537 if (num == 1.0 && (int)intnum == intnum)
538 *out_val = (AVRational){intnum, den};
540 *out_val = av_d2q(num*intnum/den, 1<<24);
544 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
546 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
547 const AVOption *flag = av_opt_find(obj, flag_name,
548 field ? field->unit : NULL, 0, 0);
551 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
552 av_opt_get_int(obj, field_name, 0, &res) < 0)
554 return res & (int) flag->default_val.dbl;
557 static void opt_list(void *obj, void *av_log_obj, const char *unit,
558 int req_flags, int rej_flags)
560 const AVOption *opt=NULL;
562 while ((opt = av_opt_next(obj, opt))) {
563 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
566 /* Don't print CONST's on level one.
567 * Don't print anything but CONST's on level two.
568 * Only print items from the requested unit.
570 if (!unit && opt->type==AV_OPT_TYPE_CONST)
572 else if (unit && opt->type!=AV_OPT_TYPE_CONST)
574 else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
576 else if (unit && opt->type == AV_OPT_TYPE_CONST)
577 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
579 av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
582 case AV_OPT_TYPE_FLAGS:
583 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<flags>");
585 case AV_OPT_TYPE_INT:
586 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int>");
588 case AV_OPT_TYPE_INT64:
589 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<int64>");
591 case AV_OPT_TYPE_DOUBLE:
592 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<double>");
594 case AV_OPT_TYPE_FLOAT:
595 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<float>");
597 case AV_OPT_TYPE_STRING:
598 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<string>");
600 case AV_OPT_TYPE_RATIONAL:
601 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<rational>");
603 case AV_OPT_TYPE_BINARY:
604 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<binary>");
606 case AV_OPT_TYPE_IMAGE_SIZE:
607 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "<image_size>");
609 case AV_OPT_TYPE_CONST:
611 av_log(av_log_obj, AV_LOG_INFO, "%-7s ", "");
614 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
615 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
616 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
617 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
618 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
621 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
622 av_log(av_log_obj, AV_LOG_INFO, "\n");
623 if (opt->unit && opt->type != AV_OPT_TYPE_CONST) {
624 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
629 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
634 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass**)obj)->class_name);
636 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
641 void av_opt_set_defaults(void *s)
643 #if FF_API_OLD_AVOPTIONS
644 av_opt_set_defaults2(s, 0, 0);
647 void av_opt_set_defaults2(void *s, int mask, int flags)
650 const AVOption *opt = NULL;
651 while ((opt = av_opt_next(s, opt)) != NULL) {
652 #if FF_API_OLD_AVOPTIONS
653 if ((opt->flags & mask) != flags)
657 case AV_OPT_TYPE_CONST:
658 /* Nothing to be done here */
660 case AV_OPT_TYPE_FLAGS:
661 case AV_OPT_TYPE_INT: {
663 val = opt->default_val.dbl;
664 av_opt_set_int(s, opt->name, val, 0);
667 case AV_OPT_TYPE_INT64:
668 if ((double)(opt->default_val.dbl+0.6) == opt->default_val.dbl)
669 av_log(s, AV_LOG_DEBUG, "loss of precision in default of %s\n", opt->name);
670 av_opt_set_int(s, opt->name, opt->default_val.dbl, 0);
672 case AV_OPT_TYPE_DOUBLE:
673 case AV_OPT_TYPE_FLOAT: {
675 val = opt->default_val.dbl;
676 av_opt_set_double(s, opt->name, val, 0);
679 case AV_OPT_TYPE_RATIONAL: {
681 val = av_d2q(opt->default_val.dbl, INT_MAX);
682 av_opt_set_q(s, opt->name, val, 0);
685 case AV_OPT_TYPE_STRING:
686 case AV_OPT_TYPE_IMAGE_SIZE:
687 av_opt_set(s, opt->name, opt->default_val.str, 0);
689 case AV_OPT_TYPE_BINARY:
690 /* Cannot set default for binary */
693 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n", opt->type, opt->name);
699 * Store the value in the field in ctx that is named like key.
700 * ctx must be an AVClass context, storing is done using AVOptions.
702 * @param buf the string to parse, buf will be updated to point at the
703 * separator just after the parsed key/value pair
704 * @param key_val_sep a 0-terminated list of characters used to
705 * separate key from value
706 * @param pairs_sep a 0-terminated list of characters used to separate
707 * two pairs from each other
708 * @return 0 if the key/value pair has been successfully parsed and
709 * set, or a negative value corresponding to an AVERROR code in case
711 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
712 * the error code issued by av_opt_set() if the key/value pair
715 static int parse_key_value_pair(void *ctx, const char **buf,
716 const char *key_val_sep, const char *pairs_sep)
718 char *key = av_get_token(buf, key_val_sep);
722 if (*key && strspn(*buf, key_val_sep)) {
724 val = av_get_token(buf, pairs_sep);
726 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
728 return AVERROR(EINVAL);
731 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
733 ret = av_opt_set(ctx, key, val, 0);
734 if (ret == AVERROR_OPTION_NOT_FOUND)
735 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
742 int av_set_options_string(void *ctx, const char *opts,
743 const char *key_val_sep, const char *pairs_sep)
751 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
762 void av_opt_free(void *obj)
764 const AVOption *o = NULL;
765 while ((o = av_opt_next(obj, o)))
766 if (o->type == AV_OPT_TYPE_STRING || o->type == AV_OPT_TYPE_BINARY)
767 av_freep((uint8_t *)obj + o->offset);
770 int av_opt_set_dict(void *obj, AVDictionary **options)
772 AVDictionaryEntry *t = NULL;
773 AVDictionary *tmp = NULL;
776 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
777 ret = av_opt_set(obj, t->key, t->value, 0);
778 if (ret == AVERROR_OPTION_NOT_FOUND)
779 av_dict_set(&tmp, t->key, t->value, 0);
781 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
786 av_dict_free(options);
791 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
792 int opt_flags, int search_flags)
794 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
797 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
798 int opt_flags, int search_flags, void **target_obj)
801 const AVOption *o = NULL;
808 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
809 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
810 const AVClass *child = NULL;
811 while (child = av_opt_child_class_next(c, child))
812 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
816 while (child = av_opt_child_next(obj, child))
817 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
822 while (o = av_opt_next(obj, o)) {
823 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
824 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
825 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
827 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
838 void *av_opt_child_next(void *obj, void *prev)
840 const AVClass *c = *(AVClass**)obj;
842 return c->child_next(obj, prev);
846 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
848 if (parent->child_class_next)
849 return parent->child_class_next(prev);
853 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
855 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
858 return (uint8_t*)obj + opt->offset;
865 typedef struct TestContext
867 const AVClass *class;
875 #define OFFSET(x) offsetof(TestContext, x)
877 #define TEST_FLAG_COOL 01
878 #define TEST_FLAG_LAME 02
879 #define TEST_FLAG_MU 04
881 static const AVOption test_options[]= {
882 {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, {0}, 0, 100 },
883 {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, {0}, 0, 1 },
884 {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, {0}, 0, 10 },
885 {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, {0}, CHAR_MIN, CHAR_MAX },
886 {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {0}, 0, INT_MAX, 0, "flags" },
887 {"cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_COOL}, INT_MIN, INT_MAX, 0, "flags" },
888 {"lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_LAME}, INT_MIN, INT_MAX, 0, "flags" },
889 {"mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, {TEST_FLAG_MU}, INT_MIN, INT_MAX, 0, "flags" },
893 static const char *test_get_name(void *ctx)
898 static const AVClass test_class = {
908 printf("\nTesting av_set_options_string()\n");
910 TestContext test_ctx;
911 const char *options[] = {
927 "flags=+mu-lame : num=42: toggle=0",
928 "num=42 : string=blahblah",
929 "rational=0 : rational=1/2 : rational=1/-1",
933 test_ctx.class = &test_class;
934 av_opt_set_defaults(&test_ctx);
935 test_ctx.string = av_strdup("default");
937 av_log_set_level(AV_LOG_DEBUG);
939 for (i=0; i < FF_ARRAY_ELEMS(options); i++) {
940 av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]);
941 if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0)
942 av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]);