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>
34 #include "mathematics.h"
37 const AVOption
*av_opt_next(const void *obj
, const AVOption
*last
)
39 AVClass
*class = *(AVClass
**)obj
;
40 if (!last
&& class->option
&& class->option
[0].name
)
42 if (last
&& last
[1].name
)
47 static int read_number(const AVOption
*o
, const void *dst
, double *num
, int *den
, int64_t *intnum
)
50 case AV_OPT_TYPE_FLAGS
:
51 *intnum
= *(unsigned int *)dst
;
54 *intnum
= *(int *)dst
;
56 case AV_OPT_TYPE_INT64
:
57 *intnum
= *(int64_t *)dst
;
59 case AV_OPT_TYPE_FLOAT
:
62 case AV_OPT_TYPE_DOUBLE
:
63 *num
= *(double *)dst
;
65 case AV_OPT_TYPE_RATIONAL
:
66 *intnum
= ((AVRational
*)dst
)->num
;
67 *den
= ((AVRational
*)dst
)->den
;
70 return AVERROR(EINVAL
);
73 static int write_number(void *obj
, const AVOption
*o
, void *dst
, double num
, int den
, int64_t intnum
)
75 if (o
->type
!= AV_OPT_TYPE_FLAGS
&&
76 (o
->max
* den
< num
* intnum
|| o
->min
* den
> num
* intnum
)) {
77 av_log(obj
, AV_LOG_ERROR
, "Value %f for parameter '%s' out of range\n",
78 num
* intnum
/ den
, o
->name
);
79 return AVERROR(ERANGE
);
83 case AV_OPT_TYPE_FLAGS
:
85 *(int *)dst
= llrint(num
/ den
) * intnum
;
87 case AV_OPT_TYPE_INT64
:
88 *(int64_t *)dst
= llrint(num
/ den
) * intnum
;
90 case AV_OPT_TYPE_FLOAT
:
91 *(float *)dst
= num
* intnum
/ den
;
93 case AV_OPT_TYPE_DOUBLE
:
94 *(double *)dst
= num
* intnum
/ den
;
96 case AV_OPT_TYPE_RATIONAL
:
98 *(AVRational
*)dst
= (AVRational
) { num
*intnum
, den
};
100 *(AVRational
*)dst
= av_d2q(num
* intnum
/ den
, 1 << 24);
103 return AVERROR(EINVAL
);
108 static const double const_values
[] = {
115 static const char *const const_names
[] = {
122 static int hexchar2int(char c
)
124 if (c
>= '0' && c
<= '9')
126 if (c
>= 'a' && c
<= 'f')
128 if (c
>= 'A' && c
<= 'F')
133 static int set_string_binary(void *obj
, const AVOption
*o
, const char *val
, uint8_t **dst
)
135 int *lendst
= (int *)(dst
+ 1);
137 int len
= strlen(val
);
143 return AVERROR(EINVAL
);
146 ptr
= bin
= av_malloc(len
);
148 return AVERROR(ENOMEM
);
150 int a
= hexchar2int(*val
++);
151 int b
= hexchar2int(*val
++);
152 if (a
< 0 || b
< 0) {
154 return AVERROR(EINVAL
);
156 *ptr
++ = (a
<< 4) | b
;
164 static int set_string(void *obj
, const AVOption
*o
, const char *val
, uint8_t **dst
)
167 *dst
= av_strdup(val
);
168 return *dst
? 0 : AVERROR(ENOMEM
);
171 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
172 opt->type == AV_OPT_TYPE_CONST || \
173 opt->type == AV_OPT_TYPE_FLAGS || \
174 opt->type == AV_OPT_TYPE_INT) \
175 ? opt->default_val.i64 \
176 : opt->default_val.dbl)
178 static int set_string_number(void *obj
, void *target_obj
, const AVOption
*o
, const char *val
, void *dst
)
180 int ret
= 0, notfirst
= 0;
189 if (*val
== '+' || *val
== '-') {
190 if (o
->type
== AV_OPT_TYPE_FLAGS
)
196 for (; i
< sizeof(buf
) - 1 && val
[i
] && val
[i
] != '+' && val
[i
] != '-'; i
++)
201 const AVOption
*o_named
= av_opt_find(target_obj
, buf
, o
->unit
, 0, 0);
202 if (o_named
&& o_named
->type
== AV_OPT_TYPE_CONST
)
203 d
= DEFAULT_NUMVAL(o_named
);
204 else if (!strcmp(buf
, "default"))
205 d
= DEFAULT_NUMVAL(o
);
206 else if (!strcmp(buf
, "max"))
208 else if (!strcmp(buf
, "min"))
210 else if (!strcmp(buf
, "none"))
212 else if (!strcmp(buf
, "all"))
215 int res
= av_expr_parse_and_eval(&d
, buf
, const_names
, const_values
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, obj
);
217 av_log(obj
, AV_LOG_ERROR
, "Unable to parse option value \"%s\"\n", val
);
222 if (o
->type
== AV_OPT_TYPE_FLAGS
) {
223 read_number(o
, dst
, NULL
, NULL
, &intnum
);
225 d
= intnum
| (int64_t)d
;
227 d
= intnum
& ~(int64_t)d
;
229 read_number(o
, dst
, &num
, &den
, &intnum
);
231 d
= notfirst
* num
* intnum
/ den
+ d
;
233 d
= notfirst
* num
* intnum
/ den
- d
;
236 if ((ret
= write_number(obj
, o
, dst
, d
, 1, 1)) < 0)
245 int av_opt_set(void *obj
, const char *name
, const char *val
, int search_flags
)
247 void *dst
, *target_obj
;
248 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
249 if (!o
|| !target_obj
)
250 return AVERROR_OPTION_NOT_FOUND
;
251 if (!val
|| o
->flags
& AV_OPT_FLAG_READONLY
)
252 return AVERROR(EINVAL
);
254 dst
= ((uint8_t *)target_obj
) + o
->offset
;
256 case AV_OPT_TYPE_STRING
:
257 return set_string(obj
, o
, val
, dst
);
258 case AV_OPT_TYPE_BINARY
:
259 return set_string_binary(obj
, o
, val
, dst
);
260 case AV_OPT_TYPE_FLAGS
:
261 case AV_OPT_TYPE_INT
:
262 case AV_OPT_TYPE_INT64
:
263 case AV_OPT_TYPE_FLOAT
:
264 case AV_OPT_TYPE_DOUBLE
:
265 case AV_OPT_TYPE_RATIONAL
:
266 return set_string_number(obj
, target_obj
, o
, val
, dst
);
269 av_log(obj
, AV_LOG_ERROR
, "Invalid option type.\n");
270 return AVERROR(EINVAL
);
273 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
274 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
275 const char *val, vartype *name ## _out) \
277 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
278 return AVERROR(EINVAL); \
279 return set_string_number(obj, obj, o, val, name ## _out); \
282 OPT_EVAL_NUMBER(flags
, AV_OPT_TYPE_FLAGS
, int)
283 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT
, int)
284 OPT_EVAL_NUMBER(int64
, AV_OPT_TYPE_INT64
, int64_t)
285 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT
, float)
286 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE
, double)
287 OPT_EVAL_NUMBER(q
, AV_OPT_TYPE_RATIONAL
, AVRational
)
289 static int set_number(void *obj
, const char *name
, double num
, int den
, int64_t intnum
,
292 void *dst
, *target_obj
;
293 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
295 if (!o
|| !target_obj
)
296 return AVERROR_OPTION_NOT_FOUND
;
298 if (o
->flags
& AV_OPT_FLAG_READONLY
)
299 return AVERROR(EINVAL
);
301 dst
= ((uint8_t *)target_obj
) + o
->offset
;
302 return write_number(obj
, o
, dst
, num
, den
, intnum
);
305 int av_opt_set_int(void *obj
, const char *name
, int64_t val
, int search_flags
)
307 return set_number(obj
, name
, 1, 1, val
, search_flags
);
310 int av_opt_set_double(void *obj
, const char *name
, double val
, int search_flags
)
312 return set_number(obj
, name
, val
, 1, 1, search_flags
);
315 int av_opt_set_q(void *obj
, const char *name
, AVRational val
, int search_flags
)
317 return set_number(obj
, name
, val
.num
, val
.den
, 1, search_flags
);
320 int av_opt_set_bin(void *obj
, const char *name
, const uint8_t *val
, int len
, int search_flags
)
323 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
328 if (!o
|| !target_obj
)
329 return AVERROR_OPTION_NOT_FOUND
;
331 if (o
->type
!= AV_OPT_TYPE_BINARY
|| o
->flags
& AV_OPT_FLAG_READONLY
)
332 return AVERROR(EINVAL
);
334 ptr
= av_malloc(len
);
336 return AVERROR(ENOMEM
);
338 dst
= (uint8_t **)(((uint8_t *)target_obj
) + o
->offset
);
339 lendst
= (int *)(dst
+ 1);
344 memcpy(ptr
, val
, len
);
349 int av_opt_set_dict_val(void *obj
, const char *name
, const AVDictionary
*val
,
354 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
356 if (!o
|| !target_obj
)
357 return AVERROR_OPTION_NOT_FOUND
;
358 if (o
->flags
& AV_OPT_FLAG_READONLY
)
359 return AVERROR(EINVAL
);
361 dst
= (AVDictionary
**)(((uint8_t *)target_obj
) + o
->offset
);
363 av_dict_copy(dst
, val
, 0);
368 int av_opt_get(void *obj
, const char *name
, int search_flags
, uint8_t **out_val
)
370 void *dst
, *target_obj
;
371 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
372 uint8_t *bin
, buf
[128];
375 if (!o
|| !target_obj
)
376 return AVERROR_OPTION_NOT_FOUND
;
378 dst
= (uint8_t *)target_obj
+ o
->offset
;
382 case AV_OPT_TYPE_FLAGS
:
383 ret
= snprintf(buf
, sizeof(buf
), "0x%08X", *(int *)dst
);
385 case AV_OPT_TYPE_INT
:
386 ret
= snprintf(buf
, sizeof(buf
), "%d", *(int *)dst
);
388 case AV_OPT_TYPE_INT64
:
389 ret
= snprintf(buf
, sizeof(buf
), "%" PRId64
, *(int64_t *)dst
);
391 case AV_OPT_TYPE_FLOAT
:
392 ret
= snprintf(buf
, sizeof(buf
), "%f", *(float *)dst
);
394 case AV_OPT_TYPE_DOUBLE
:
395 ret
= snprintf(buf
, sizeof(buf
), "%f", *(double *)dst
);
397 case AV_OPT_TYPE_RATIONAL
:
398 ret
= snprintf(buf
, sizeof(buf
), "%d/%d", ((AVRational
*)dst
)->num
,
399 ((AVRational
*)dst
)->den
);
401 case AV_OPT_TYPE_STRING
:
402 if (*(uint8_t **)dst
)
403 *out_val
= av_strdup(*(uint8_t **)dst
);
405 *out_val
= av_strdup("");
406 return *out_val
? 0 : AVERROR(ENOMEM
);
407 case AV_OPT_TYPE_BINARY
:
408 len
= *(int *)(((uint8_t *)dst
) + sizeof(uint8_t *));
409 if ((uint64_t)len
* 2 + 1 > INT_MAX
)
410 return AVERROR(EINVAL
);
411 if (!(*out_val
= av_malloc(len
* 2 + 1)))
412 return AVERROR(ENOMEM
);
413 bin
= *(uint8_t **)dst
;
414 for (i
= 0; i
< len
; i
++)
415 snprintf(*out_val
+ i
* 2, 3, "%02X", bin
[i
]);
418 return AVERROR(EINVAL
);
421 if (ret
>= sizeof(buf
))
422 return AVERROR(EINVAL
);
423 *out_val
= av_strdup(buf
);
424 return *out_val
? 0 : AVERROR(ENOMEM
);
427 static int get_number(void *obj
, const char *name
, double *num
, int *den
, int64_t *intnum
,
430 void *dst
, *target_obj
;
431 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
432 if (!o
|| !target_obj
)
435 dst
= ((uint8_t *)target_obj
) + o
->offset
;
437 return read_number(o
, dst
, num
, den
, intnum
);
445 int av_opt_get_int(void *obj
, const char *name
, int search_flags
, int64_t *out_val
)
451 if ((ret
= get_number(obj
, name
, &num
, &den
, &intnum
, search_flags
)) < 0)
453 *out_val
= num
* intnum
/ den
;
457 int av_opt_get_double(void *obj
, const char *name
, int search_flags
, double *out_val
)
463 if ((ret
= get_number(obj
, name
, &num
, &den
, &intnum
, search_flags
)) < 0)
465 *out_val
= num
* intnum
/ den
;
469 int av_opt_get_q(void *obj
, const char *name
, int search_flags
, AVRational
*out_val
)
475 if ((ret
= get_number(obj
, name
, &num
, &den
, &intnum
, search_flags
)) < 0)
478 if (num
== 1.0 && (int)intnum
== intnum
)
479 *out_val
= (AVRational
) { intnum
, den
};
481 *out_val
= av_d2q(num
* intnum
/ den
, 1 << 24);
485 int av_opt_get_dict_val(void *obj
, const char *name
, int search_flags
, AVDictionary
**out_val
)
489 const AVOption
*o
= av_opt_find2(obj
, name
, NULL
, 0, search_flags
, &target_obj
);
491 if (!o
|| !target_obj
)
492 return AVERROR_OPTION_NOT_FOUND
;
493 if (o
->type
!= AV_OPT_TYPE_DICT
)
494 return AVERROR(EINVAL
);
496 src
= *(AVDictionary
**)(((uint8_t *)target_obj
) + o
->offset
);
497 av_dict_copy(out_val
, src
, 0);
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
& flag
->default_val
.i64
;
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' : '.');
574 av_log(av_log_obj
, AV_LOG_INFO
, "%c", (opt
->flags
& AV_OPT_FLAG_EXPORT
) ? 'X' : '.');
575 av_log(av_log_obj
, AV_LOG_INFO
, "%c", (opt
->flags
& AV_OPT_FLAG_READONLY
) ? 'R' : '.');
578 av_log(av_log_obj
, AV_LOG_INFO
, " %s", opt
->help
);
579 av_log(av_log_obj
, AV_LOG_INFO
, "\n");
580 if (opt
->unit
&& opt
->type
!= AV_OPT_TYPE_CONST
)
581 opt_list(obj
, av_log_obj
, opt
->unit
, req_flags
, rej_flags
);
585 int av_opt_show2(void *obj
, void *av_log_obj
, int req_flags
, int rej_flags
)
590 av_log(av_log_obj
, AV_LOG_INFO
, "%s AVOptions:\n", (*(AVClass
**)obj
)->class_name
);
592 opt_list(obj
, av_log_obj
, NULL
, req_flags
, rej_flags
);
597 void av_opt_set_defaults(void *s
)
599 const AVOption
*opt
= NULL
;
600 while ((opt
= av_opt_next(s
, opt
))) {
601 if (opt
->flags
& AV_OPT_FLAG_READONLY
)
605 case AV_OPT_TYPE_CONST
:
606 /* Nothing to be done here */
608 case AV_OPT_TYPE_FLAGS
:
609 case AV_OPT_TYPE_INT
:
610 case AV_OPT_TYPE_INT64
:
611 av_opt_set_int(s
, opt
->name
, opt
->default_val
.i64
, 0);
613 case AV_OPT_TYPE_DOUBLE
:
614 case AV_OPT_TYPE_FLOAT
:
617 val
= opt
->default_val
.dbl
;
618 av_opt_set_double(s
, opt
->name
, val
, 0);
621 case AV_OPT_TYPE_RATIONAL
:
624 val
= av_d2q(opt
->default_val
.dbl
, INT_MAX
);
625 av_opt_set_q(s
, opt
->name
, val
, 0);
628 case AV_OPT_TYPE_STRING
:
629 av_opt_set(s
, opt
->name
, opt
->default_val
.str
, 0);
631 case AV_OPT_TYPE_BINARY
:
632 case AV_OPT_TYPE_DICT
:
633 /* Cannot set defaults for these types */
636 av_log(s
, AV_LOG_DEBUG
, "AVOption type %d of option %s not implemented yet\n",
637 opt
->type
, opt
->name
);
643 * Store the value in the field in ctx that is named like key.
644 * ctx must be an AVClass context, storing is done using AVOptions.
646 * @param buf the string to parse, buf will be updated to point at the
647 * separator just after the parsed key/value pair
648 * @param key_val_sep a 0-terminated list of characters used to
649 * separate key from value
650 * @param pairs_sep a 0-terminated list of characters used to separate
651 * two pairs from each other
652 * @return 0 if the key/value pair has been successfully parsed and
653 * set, or a negative value corresponding to an AVERROR code in case
655 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
656 * the error code issued by av_opt_set() if the key/value pair
659 static int parse_key_value_pair(void *ctx
, const char **buf
,
660 const char *key_val_sep
, const char *pairs_sep
)
662 char *key
= av_get_token(buf
, key_val_sep
);
667 return AVERROR(ENOMEM
);
669 if (*key
&& strspn(*buf
, key_val_sep
)) {
671 val
= av_get_token(buf
, pairs_sep
);
674 return AVERROR(ENOMEM
);
677 av_log(ctx
, AV_LOG_ERROR
, "Missing key or no key/value separator found after key '%s'\n", key
);
679 return AVERROR(EINVAL
);
682 av_log(ctx
, AV_LOG_DEBUG
, "Setting value '%s' for key '%s'\n", val
, key
);
684 ret
= av_opt_set(ctx
, key
, val
, AV_OPT_SEARCH_CHILDREN
);
685 if (ret
== AVERROR_OPTION_NOT_FOUND
)
686 av_log(ctx
, AV_LOG_ERROR
, "Key '%s' not found.\n", key
);
693 int av_set_options_string(void *ctx
, const char *opts
,
694 const char *key_val_sep
, const char *pairs_sep
)
702 if ((ret
= parse_key_value_pair(ctx
, &opts
, key_val_sep
, pairs_sep
)) < 0)
713 void av_opt_free(void *obj
)
715 const AVOption
*o
= NULL
;
716 while ((o
= av_opt_next(obj
, o
))) {
718 case AV_OPT_TYPE_STRING
:
719 case AV_OPT_TYPE_BINARY
:
720 av_freep((uint8_t *)obj
+ o
->offset
);
723 case AV_OPT_TYPE_DICT
:
724 av_dict_free((AVDictionary
**)(((uint8_t *)obj
) + o
->offset
));
733 int av_opt_set_dict(void *obj
, AVDictionary
**options
)
735 AVDictionaryEntry
*t
= NULL
;
736 AVDictionary
*tmp
= NULL
;
739 while ((t
= av_dict_get(*options
, "", t
, AV_DICT_IGNORE_SUFFIX
))) {
740 ret
= av_opt_set(obj
, t
->key
, t
->value
, 0);
741 if (ret
== AVERROR_OPTION_NOT_FOUND
)
742 av_dict_set(&tmp
, t
->key
, t
->value
, 0);
744 av_log(obj
, AV_LOG_ERROR
, "Error setting option %s to value %s.\n", t
->key
, t
->value
);
749 av_dict_free(options
);
754 const AVOption
*av_opt_find(void *obj
, const char *name
, const char *unit
,
755 int opt_flags
, int search_flags
)
757 return av_opt_find2(obj
, name
, unit
, opt_flags
, search_flags
, NULL
);
760 const AVOption
*av_opt_find2(void *obj
, const char *name
, const char *unit
,
761 int opt_flags
, int search_flags
, void **target_obj
)
763 const AVClass
*c
= *(AVClass
**)obj
;
764 const AVOption
*o
= NULL
;
769 if (search_flags
& AV_OPT_SEARCH_CHILDREN
) {
770 if (search_flags
& AV_OPT_SEARCH_FAKE_OBJ
) {
771 const AVClass
*child
= NULL
;
772 while (child
= av_opt_child_class_next(c
, child
))
773 if (o
= av_opt_find2(&child
, name
, unit
, opt_flags
, search_flags
, NULL
))
777 while (child
= av_opt_child_next(obj
, child
))
778 if (o
= av_opt_find2(child
, name
, unit
, opt_flags
, search_flags
, target_obj
))
783 while (o
= av_opt_next(obj
, o
)) {
784 if (!strcmp(o
->name
, name
) && (o
->flags
& opt_flags
) == opt_flags
&&
785 ((!unit
&& o
->type
!= AV_OPT_TYPE_CONST
) ||
786 (unit
&& o
->unit
&& !strcmp(o
->unit
, unit
)))) {
788 if (!(search_flags
& AV_OPT_SEARCH_FAKE_OBJ
))
799 void *av_opt_child_next(void *obj
, void *prev
)
801 const AVClass
*c
= *(AVClass
**)obj
;
803 return c
->child_next(obj
, prev
);
807 const AVClass
*av_opt_child_class_next(const AVClass
*parent
, const AVClass
*prev
)
809 if (parent
->child_class_next
)
810 return parent
->child_class_next(prev
);
814 static int opt_size(enum AVOptionType type
)
817 case AV_OPT_TYPE_INT
:
818 case AV_OPT_TYPE_FLAGS
:
820 case AV_OPT_TYPE_INT64
:
821 return sizeof(int64_t);
822 case AV_OPT_TYPE_DOUBLE
:
823 return sizeof(double);
824 case AV_OPT_TYPE_FLOAT
:
825 return sizeof(float);
826 case AV_OPT_TYPE_STRING
:
827 return sizeof(uint8_t *);
828 case AV_OPT_TYPE_RATIONAL
:
829 return sizeof(AVRational
);
830 case AV_OPT_TYPE_BINARY
:
831 return sizeof(uint8_t *) + sizeof(int);
833 return AVERROR(EINVAL
);
836 int av_opt_copy(void *dst
, const void *src
)
838 const AVOption
*o
= NULL
;
843 return AVERROR(EINVAL
);
845 c
= *(AVClass
**)src
;
846 if (!c
|| c
!= *(AVClass
**)dst
)
847 return AVERROR(EINVAL
);
849 while ((o
= av_opt_next(src
, o
))) {
850 void *field_dst
= (uint8_t *)dst
+ o
->offset
;
851 void *field_src
= (uint8_t *)src
+ o
->offset
;
852 uint8_t **field_dst8
= (uint8_t **)field_dst
;
853 uint8_t **field_src8
= (uint8_t **)field_src
;
855 if (o
->type
== AV_OPT_TYPE_STRING
) {
856 set_string(dst
, o
, *field_src8
, field_dst8
);
857 if (*field_src8
&& !*field_dst8
)
858 ret
= AVERROR(ENOMEM
);
859 } else if (o
->type
== AV_OPT_TYPE_BINARY
) {
860 int len
= *(int *)(field_src8
+ 1);
861 if (*field_dst8
!= *field_src8
)
862 av_freep(field_dst8
);
864 *field_dst8
= av_malloc(len
);
866 ret
= AVERROR(ENOMEM
);
869 memcpy(*field_dst8
, *field_src8
, len
);
873 *(int *)(field_dst8
+ 1) = len
;
874 } else if (o
->type
== AV_OPT_TYPE_CONST
) {
877 int size
= opt_size(o
->type
);
881 memcpy(field_dst
, field_src
, size
);