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>
31 #include "channel_layout.h"
36 #include "parseutils.h"
38 #include "mathematics.h"
40 #include "samplefmt.h"
45 const AVOption *av_opt_next(const void *obj, const AVOption *last)
50 class = *(const AVClass**)obj;
51 if (!last && class && class->option && class->option[0].name)
53 if (last && last[1].name)
58 static int read_number(const AVOption *o, const void *dst, double *num, int *den, int64_t *intnum)
61 case AV_OPT_TYPE_FLAGS:
62 *intnum = *(unsigned int*)dst;
64 case AV_OPT_TYPE_PIXEL_FMT:
65 *intnum = *(enum AVPixelFormat *)dst;
67 case AV_OPT_TYPE_SAMPLE_FMT:
68 *intnum = *(enum AVSampleFormat *)dst;
70 case AV_OPT_TYPE_BOOL:
72 *intnum = *(int *)dst;
74 case AV_OPT_TYPE_CHANNEL_LAYOUT:
75 case AV_OPT_TYPE_DURATION:
76 case AV_OPT_TYPE_INT64:
77 case AV_OPT_TYPE_UINT64:
78 *intnum = *(int64_t *)dst;
80 case AV_OPT_TYPE_FLOAT:
83 case AV_OPT_TYPE_DOUBLE:
84 *num = *(double *)dst;
86 case AV_OPT_TYPE_RATIONAL:
87 *intnum = ((AVRational *)dst)->num;
88 *den = ((AVRational *)dst)->den;
90 case AV_OPT_TYPE_CONST:
91 *num = o->default_val.dbl;
94 return AVERROR(EINVAL);
97 static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
99 if (o->type != AV_OPT_TYPE_FLAGS &&
100 (!den || o->max * den < num * intnum || o->min * den > num * intnum)) {
101 num = den ? num * intnum / den : (num && intnum ? INFINITY : NAN);
102 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
103 num, o->name, o->min, o->max);
104 return AVERROR(ERANGE);
106 if (o->type == AV_OPT_TYPE_FLAGS) {
107 double d = num*intnum/den;
108 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (llrint(d*256) & 255)) {
109 av_log(obj, AV_LOG_ERROR,
110 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
111 num*intnum/den, o->name);
112 return AVERROR(ERANGE);
117 case AV_OPT_TYPE_PIXEL_FMT:
118 *(enum AVPixelFormat *)dst = llrint(num / den) * intnum;
120 case AV_OPT_TYPE_SAMPLE_FMT:
121 *(enum AVSampleFormat *)dst = llrint(num / den) * intnum;
123 case AV_OPT_TYPE_BOOL:
124 case AV_OPT_TYPE_FLAGS:
125 case AV_OPT_TYPE_INT:
126 *(int *)dst = llrint(num / den) * intnum;
128 case AV_OPT_TYPE_DURATION:
129 case AV_OPT_TYPE_CHANNEL_LAYOUT:
130 case AV_OPT_TYPE_INT64:{
131 double d = num / den;
132 if (intnum == 1 && d == (double)INT64_MAX) {
133 *(int64_t *)dst = INT64_MAX;
135 *(int64_t *)dst = llrint(d) * intnum;
137 case AV_OPT_TYPE_UINT64:{
138 double d = num / den;
139 // We must special case uint64_t here as llrint() does not support values
140 // outside the int64_t range and there is no portable function which does
141 // "INT64_MAX + 1ULL" is used as it is representable exactly as IEEE double
142 // while INT64_MAX is not
143 if (intnum == 1 && d == (double)UINT64_MAX) {
144 *(uint64_t *)dst = UINT64_MAX;
145 } else if (d > INT64_MAX + 1ULL) {
146 *(uint64_t *)dst = (llrint(d - (INT64_MAX + 1ULL)) + (INT64_MAX + 1ULL))*intnum;
148 *(uint64_t *)dst = llrint(d) * intnum;
151 case AV_OPT_TYPE_FLOAT:
152 *(float *)dst = num * intnum / den;
154 case AV_OPT_TYPE_DOUBLE:
155 *(double *)dst = num * intnum / den;
157 case AV_OPT_TYPE_RATIONAL:
158 case AV_OPT_TYPE_VIDEO_RATE:
159 if ((int) num == num)
160 *(AVRational *)dst = (AVRational) { num *intnum, den };
162 *(AVRational *)dst = av_d2q(num * intnum / den, 1 << 24);
165 return AVERROR(EINVAL);
170 static int hexchar2int(char c) {
171 if (c >= '0' && c <= '9')
173 if (c >= 'a' && c <= 'f')
175 if (c >= 'A' && c <= 'F')
180 static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
182 int *lendst = (int *)(dst + 1);
189 if (!val || !(len = strlen(val)))
193 return AVERROR(EINVAL);
196 ptr = bin = av_malloc(len);
198 return AVERROR(ENOMEM);
200 int a = hexchar2int(*val++);
201 int b = hexchar2int(*val++);
202 if (a < 0 || b < 0) {
204 return AVERROR(EINVAL);
206 *ptr++ = (a << 4) | b;
214 static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
217 *dst = av_strdup(val);
218 return *dst ? 0 : AVERROR(ENOMEM);
221 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
222 opt->type == AV_OPT_TYPE_UINT64 || \
223 opt->type == AV_OPT_TYPE_CONST || \
224 opt->type == AV_OPT_TYPE_FLAGS || \
225 opt->type == AV_OPT_TYPE_INT) \
226 ? opt->default_val.i64 \
227 : opt->default_val.dbl)
229 static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
235 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
236 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
248 if (o->type == AV_OPT_TYPE_FLAGS) {
249 if (*val == '+' || *val == '-')
251 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
257 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, 0);
260 double const_values[64];
261 const char * const_names[64];
262 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
263 d = DEFAULT_NUMVAL(o_named);
266 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
267 if (o_named->type == AV_OPT_TYPE_CONST &&
269 !strcmp(o_named->unit, o->unit)) {
270 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
271 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
272 return AVERROR_PATCHWELCOME;
274 const_names [ci ] = o_named->name;
275 const_values[ci++] = DEFAULT_NUMVAL(o_named);
279 const_names [ci ] = "default";
280 const_values[ci++] = DEFAULT_NUMVAL(o);
281 const_names [ci ] = "max";
282 const_values[ci++] = o->max;
283 const_names [ci ] = "min";
284 const_values[ci++] = o->min;
285 const_names [ci ] = "none";
286 const_values[ci++] = 0;
287 const_names [ci ] = "all";
288 const_values[ci++] = ~0;
289 const_names [ci] = NULL;
290 const_values[ci] = 0;
292 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
293 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
295 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
300 if (o->type == AV_OPT_TYPE_FLAGS) {
301 read_number(o, dst, NULL, NULL, &intnum);
303 d = intnum | (int64_t)d;
305 d = intnum &~(int64_t)d;
308 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
316 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
320 if (!val || !strcmp(val, "none")) {
325 ret = av_parse_video_size(dst, dst + 1, val);
327 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
331 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
333 int ret = av_parse_video_rate(dst, val);
335 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
339 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
346 ret = av_parse_color(dst, val, -1, obj);
348 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
354 static const char *get_bool_name(int val)
358 return val ? "true" : "false";
361 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
368 if (!strcmp(val, "auto")) {
370 } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
372 } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
376 n = strtol(val, &end, 10);
377 if (val + strlen(val) != end)
381 if (n < o->min || n > o->max)
388 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
389 return AVERROR(EINVAL);
392 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
393 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
397 if (!val || !strcmp(val, "none")) {
403 fmt = strtol(val, &tail, 0);
404 if (*tail || (unsigned)fmt >= fmt_nb) {
405 av_log(obj, AV_LOG_ERROR,
406 "Unable to parse option value \"%s\" as %s\n", val, desc);
407 return AVERROR(EINVAL);
412 min = FFMAX(o->min, -1);
413 max = FFMIN(o->max, fmt_nb-1);
415 // hack for compatibility with old ffmpeg
416 if(min == 0 && max == 0) {
421 if (fmt < min || fmt > max) {
422 av_log(obj, AV_LOG_ERROR,
423 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
424 fmt, o->name, desc, min, max);
425 return AVERROR(ERANGE);
432 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
434 return set_string_fmt(obj, o, val, dst,
435 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
438 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
440 return set_string_fmt(obj, o, val, dst,
441 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
444 static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
446 AVDictionary *options = NULL;
449 int ret = av_dict_parse_string(&options, val, "=", ":", 0);
451 av_dict_free(&options);
456 av_dict_free((AVDictionary **)dst);
457 *dst = (uint8_t *)options;
462 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
465 void *dst, *target_obj;
466 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
467 if (!o || !target_obj)
468 return AVERROR_OPTION_NOT_FOUND;
469 if (!val && (o->type != AV_OPT_TYPE_STRING &&
470 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
471 o->type != AV_OPT_TYPE_IMAGE_SIZE &&
472 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
473 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
474 return AVERROR(EINVAL);
476 if (o->flags & AV_OPT_FLAG_READONLY)
477 return AVERROR(EINVAL);
479 if (o->flags & AV_OPT_FLAG_DEPRECATED)
480 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
482 dst = ((uint8_t *)target_obj) + o->offset;
484 case AV_OPT_TYPE_BOOL:
485 return set_string_bool(obj, o, val, dst);
486 case AV_OPT_TYPE_STRING:
487 return set_string(obj, o, val, dst);
488 case AV_OPT_TYPE_BINARY:
489 return set_string_binary(obj, o, val, dst);
490 case AV_OPT_TYPE_FLAGS:
491 case AV_OPT_TYPE_INT:
492 case AV_OPT_TYPE_INT64:
493 case AV_OPT_TYPE_UINT64:
494 case AV_OPT_TYPE_FLOAT:
495 case AV_OPT_TYPE_DOUBLE:
496 case AV_OPT_TYPE_RATIONAL:
497 return set_string_number(obj, target_obj, o, val, dst);
498 case AV_OPT_TYPE_IMAGE_SIZE:
499 return set_string_image_size(obj, o, val, dst);
500 case AV_OPT_TYPE_VIDEO_RATE: {
502 ret = set_string_video_rate(obj, o, val, &tmp);
505 return write_number(obj, o, dst, 1, tmp.den, tmp.num);
507 case AV_OPT_TYPE_PIXEL_FMT:
508 return set_string_pixel_fmt(obj, o, val, dst);
509 case AV_OPT_TYPE_SAMPLE_FMT:
510 return set_string_sample_fmt(obj, o, val, dst);
511 case AV_OPT_TYPE_DURATION:
515 if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
516 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
520 if (usecs < o->min || usecs > o->max) {
521 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
522 usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
523 return AVERROR(ERANGE);
525 *(int64_t *)dst = usecs;
528 case AV_OPT_TYPE_COLOR:
529 return set_string_color(obj, o, val, dst);
530 case AV_OPT_TYPE_CHANNEL_LAYOUT:
531 if (!val || !strcmp(val, "none")) {
534 int64_t cl = av_get_channel_layout(val);
536 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
537 ret = AVERROR(EINVAL);
539 *(int64_t *)dst = cl;
543 case AV_OPT_TYPE_DICT:
544 return set_string_dict(obj, o, val, dst);
547 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
548 return AVERROR(EINVAL);
551 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
552 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
553 const char *val, vartype *name ## _out) \
555 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
556 return AVERROR(EINVAL); \
557 return set_string_number(obj, obj, o, val, name ## _out); \
560 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
561 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
562 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
563 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
564 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
565 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
567 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
570 void *dst, *target_obj;
571 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
573 if (!o || !target_obj)
574 return AVERROR_OPTION_NOT_FOUND;
576 if (o->flags & AV_OPT_FLAG_READONLY)
577 return AVERROR(EINVAL);
579 dst = ((uint8_t *)target_obj) + o->offset;
580 return write_number(obj, o, dst, num, den, intnum);
583 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
585 return set_number(obj, name, 1, 1, val, search_flags);
588 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
590 return set_number(obj, name, val, 1, 1, search_flags);
593 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
595 return set_number(obj, name, val.num, val.den, 1, search_flags);
598 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
601 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
606 if (!o || !target_obj)
607 return AVERROR_OPTION_NOT_FOUND;
609 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
610 return AVERROR(EINVAL);
612 ptr = len ? av_malloc(len) : NULL;
614 return AVERROR(ENOMEM);
616 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
617 lendst = (int *)(dst + 1);
623 memcpy(ptr, val, len);
628 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
631 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
633 if (!o || !target_obj)
634 return AVERROR_OPTION_NOT_FOUND;
635 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
636 av_log(obj, AV_LOG_ERROR,
637 "The value set by option '%s' is not an image size.\n", o->name);
638 return AVERROR(EINVAL);
641 av_log(obj, AV_LOG_ERROR,
642 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
643 return AVERROR(EINVAL);
645 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
646 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
650 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
653 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
655 if (!o || !target_obj)
656 return AVERROR_OPTION_NOT_FOUND;
657 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
658 av_log(obj, AV_LOG_ERROR,
659 "The value set by option '%s' is not a video rate.\n", o->name);
660 return AVERROR(EINVAL);
662 if (val.num <= 0 || val.den <= 0)
663 return AVERROR(EINVAL);
664 return set_number(obj, name, val.num, val.den, 1, search_flags);
667 static int set_format(void *obj, const char *name, int fmt, int search_flags,
668 enum AVOptionType type, const char *desc, int nb_fmts)
671 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
672 search_flags, &target_obj);
675 if (!o || !target_obj)
676 return AVERROR_OPTION_NOT_FOUND;
677 if (o->type != type) {
678 av_log(obj, AV_LOG_ERROR,
679 "The value set by option '%s' is not a %s format", name, desc);
680 return AVERROR(EINVAL);
683 min = FFMAX(o->min, -1);
684 max = FFMIN(o->max, nb_fmts-1);
686 if (fmt < min || fmt > max) {
687 av_log(obj, AV_LOG_ERROR,
688 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
689 fmt, name, desc, min, max);
690 return AVERROR(ERANGE);
692 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
696 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
698 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
701 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
703 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
706 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
709 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
711 if (!o || !target_obj)
712 return AVERROR_OPTION_NOT_FOUND;
713 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
714 av_log(obj, AV_LOG_ERROR,
715 "The value set by option '%s' is not a channel layout.\n", o->name);
716 return AVERROR(EINVAL);
718 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
722 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
727 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
729 if (!o || !target_obj)
730 return AVERROR_OPTION_NOT_FOUND;
731 if (o->flags & AV_OPT_FLAG_READONLY)
732 return AVERROR(EINVAL);
734 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
736 av_dict_copy(dst, val, 0);
741 static void format_duration(char *buf, size_t size, int64_t d)
745 av_assert0(size >= 25);
746 if (d < 0 && d != INT64_MIN) {
752 snprintf(buf, size, "INT64_MAX");
753 else if (d == INT64_MIN)
754 snprintf(buf, size, "INT64_MIN");
755 else if (d > (int64_t)3600*1000000)
756 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
757 (int)((d / 60000000) % 60),
758 (int)((d / 1000000) % 60),
760 else if (d > 60*1000000)
761 snprintf(buf, size, "%d:%02d.%06d",
763 (int)((d / 1000000) % 60),
766 snprintf(buf, size, "%d.%06d",
769 e = buf + strlen(buf);
770 while (e > buf && e[-1] == '0')
772 if (e > buf && e[-1] == '.')
776 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
778 void *dst, *target_obj;
779 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
780 uint8_t *bin, buf[128];
784 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
785 return AVERROR_OPTION_NOT_FOUND;
787 if (o->flags & AV_OPT_FLAG_DEPRECATED)
788 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
790 dst = (uint8_t *)target_obj + o->offset;
794 case AV_OPT_TYPE_BOOL:
795 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
797 case AV_OPT_TYPE_FLAGS:
798 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
800 case AV_OPT_TYPE_INT:
801 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
803 case AV_OPT_TYPE_INT64:
804 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
806 case AV_OPT_TYPE_UINT64:
807 ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst);
809 case AV_OPT_TYPE_FLOAT:
810 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
812 case AV_OPT_TYPE_DOUBLE:
813 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
815 case AV_OPT_TYPE_VIDEO_RATE:
816 case AV_OPT_TYPE_RATIONAL:
817 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
819 case AV_OPT_TYPE_CONST:
820 ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
822 case AV_OPT_TYPE_STRING:
823 if (*(uint8_t **)dst) {
824 *out_val = av_strdup(*(uint8_t **)dst);
825 } else if (search_flags & AV_OPT_ALLOW_NULL) {
829 *out_val = av_strdup("");
831 return *out_val ? 0 : AVERROR(ENOMEM);
832 case AV_OPT_TYPE_BINARY:
833 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
837 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
838 if ((uint64_t)len * 2 + 1 > INT_MAX)
839 return AVERROR(EINVAL);
840 if (!(*out_val = av_malloc(len * 2 + 1)))
841 return AVERROR(ENOMEM);
846 bin = *(uint8_t **)dst;
847 for (i = 0; i < len; i++)
848 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
850 case AV_OPT_TYPE_IMAGE_SIZE:
851 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
853 case AV_OPT_TYPE_PIXEL_FMT:
854 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
856 case AV_OPT_TYPE_SAMPLE_FMT:
857 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
859 case AV_OPT_TYPE_DURATION:
860 i64 = *(int64_t *)dst;
861 format_duration(buf, sizeof(buf), i64);
862 ret = strlen(buf); // no overflow possible, checked by an assert
864 case AV_OPT_TYPE_COLOR:
865 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
866 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
867 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
869 case AV_OPT_TYPE_CHANNEL_LAYOUT:
870 i64 = *(int64_t *)dst;
871 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
873 case AV_OPT_TYPE_DICT:
874 if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
878 return av_dict_get_string(*(AVDictionary **)dst, (char **)out_val, '=', ':');
880 return AVERROR(EINVAL);
883 if (ret >= sizeof(buf))
884 return AVERROR(EINVAL);
885 *out_val = av_strdup(buf);
886 return *out_val ? 0 : AVERROR(ENOMEM);
889 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
892 void *dst, *target_obj;
893 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
894 if (!o || !target_obj)
897 dst = ((uint8_t *)target_obj) + o->offset;
899 if (o_out) *o_out= o;
901 return read_number(o, dst, num, den, intnum);
909 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
915 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
917 *out_val = num * intnum / den;
921 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
927 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
929 *out_val = num * intnum / den;
933 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
939 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
942 if (num == 1.0 && (int)intnum == intnum)
943 *out_val = (AVRational){intnum, den};
945 *out_val = av_d2q(num*intnum/den, 1<<24);
949 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
951 void *dst, *target_obj;
952 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
953 if (!o || !target_obj)
954 return AVERROR_OPTION_NOT_FOUND;
955 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
956 av_log(obj, AV_LOG_ERROR,
957 "The value for option '%s' is not an image size.\n", name);
958 return AVERROR(EINVAL);
961 dst = ((uint8_t*)target_obj) + o->offset;
962 if (w_out) *w_out = *(int *)dst;
963 if (h_out) *h_out = *((int *)dst+1);
967 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
973 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
976 if (num == 1.0 && (int)intnum == intnum)
977 *out_val = (AVRational) { intnum, den };
979 *out_val = av_d2q(num * intnum / den, 1 << 24);
983 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
984 enum AVOptionType type, const char *desc)
986 void *dst, *target_obj;
987 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
988 if (!o || !target_obj)
989 return AVERROR_OPTION_NOT_FOUND;
990 if (o->type != type) {
991 av_log(obj, AV_LOG_ERROR,
992 "The value for option '%s' is not a %s format.\n", desc, name);
993 return AVERROR(EINVAL);
996 dst = ((uint8_t*)target_obj) + o->offset;
997 *out_fmt = *(int *)dst;
1001 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
1003 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
1006 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
1008 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
1011 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
1013 void *dst, *target_obj;
1014 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1015 if (!o || !target_obj)
1016 return AVERROR_OPTION_NOT_FOUND;
1017 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
1018 av_log(obj, AV_LOG_ERROR,
1019 "The value for option '%s' is not a channel layout.\n", name);
1020 return AVERROR(EINVAL);
1023 dst = ((uint8_t*)target_obj) + o->offset;
1024 *cl = *(int64_t *)dst;
1028 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1032 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1034 if (!o || !target_obj)
1035 return AVERROR_OPTION_NOT_FOUND;
1036 if (o->type != AV_OPT_TYPE_DICT)
1037 return AVERROR(EINVAL);
1039 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1040 av_dict_copy(out_val, src, 0);
1045 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1047 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1048 const AVOption *flag = av_opt_find(obj, flag_name,
1049 field ? field->unit : NULL, 0, 0);
1052 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1053 av_opt_get_int(obj, field_name, 0, &res) < 0)
1055 return res & flag->default_val.i64;
1058 static void log_int_value(void *av_log_obj, int level, int64_t i)
1061 av_log(av_log_obj, level, "INT_MAX");
1062 } else if (i == INT_MIN) {
1063 av_log(av_log_obj, level, "INT_MIN");
1064 } else if (i == UINT32_MAX) {
1065 av_log(av_log_obj, level, "UINT32_MAX");
1066 } else if (i == INT64_MAX) {
1067 av_log(av_log_obj, level, "I64_MAX");
1068 } else if (i == INT64_MIN) {
1069 av_log(av_log_obj, level, "I64_MIN");
1071 av_log(av_log_obj, level, "%"PRId64, i);
1075 static void log_value(void *av_log_obj, int level, double d)
1078 av_log(av_log_obj, level, "INT_MAX");
1079 } else if (d == INT_MIN) {
1080 av_log(av_log_obj, level, "INT_MIN");
1081 } else if (d == UINT32_MAX) {
1082 av_log(av_log_obj, level, "UINT32_MAX");
1083 } else if (d == (double)INT64_MAX) {
1084 av_log(av_log_obj, level, "I64_MAX");
1085 } else if (d == INT64_MIN) {
1086 av_log(av_log_obj, level, "I64_MIN");
1087 } else if (d == FLT_MAX) {
1088 av_log(av_log_obj, level, "FLT_MAX");
1089 } else if (d == FLT_MIN) {
1090 av_log(av_log_obj, level, "FLT_MIN");
1091 } else if (d == -FLT_MAX) {
1092 av_log(av_log_obj, level, "-FLT_MAX");
1093 } else if (d == -FLT_MIN) {
1094 av_log(av_log_obj, level, "-FLT_MIN");
1095 } else if (d == DBL_MAX) {
1096 av_log(av_log_obj, level, "DBL_MAX");
1097 } else if (d == DBL_MIN) {
1098 av_log(av_log_obj, level, "DBL_MIN");
1099 } else if (d == -DBL_MAX) {
1100 av_log(av_log_obj, level, "-DBL_MAX");
1101 } else if (d == -DBL_MIN) {
1102 av_log(av_log_obj, level, "-DBL_MIN");
1104 av_log(av_log_obj, level, "%g", d);
1108 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1110 const AVOption *opt = NULL;
1114 while ((opt = av_opt_next(obj, opt)))
1115 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1116 opt->default_val.i64 == value)
1121 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1123 const AVOption *opt = NULL;
1129 while ((opt = av_opt_next(obj, opt))) {
1130 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1131 opt->default_val.i64 & value) {
1133 av_strlcatf(flags, sizeof(flags), "+");
1134 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1138 return av_strdup(flags);
1142 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1143 int req_flags, int rej_flags, enum AVOptionType parent_type)
1145 const AVOption *opt = NULL;
1149 while ((opt = av_opt_next(obj, opt))) {
1150 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1153 /* Don't print CONST's on level one.
1154 * Don't print anything but CONST's on level two.
1155 * Only print items from the requested unit.
1157 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1159 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1161 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1163 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1164 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1166 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1167 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1170 switch (opt->type) {
1171 case AV_OPT_TYPE_FLAGS:
1172 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1174 case AV_OPT_TYPE_INT:
1175 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1177 case AV_OPT_TYPE_INT64:
1178 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1180 case AV_OPT_TYPE_UINT64:
1181 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
1183 case AV_OPT_TYPE_DOUBLE:
1184 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1186 case AV_OPT_TYPE_FLOAT:
1187 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1189 case AV_OPT_TYPE_STRING:
1190 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1192 case AV_OPT_TYPE_RATIONAL:
1193 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1195 case AV_OPT_TYPE_BINARY:
1196 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1198 case AV_OPT_TYPE_DICT:
1199 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<dictionary>");
1201 case AV_OPT_TYPE_IMAGE_SIZE:
1202 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1204 case AV_OPT_TYPE_VIDEO_RATE:
1205 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1207 case AV_OPT_TYPE_PIXEL_FMT:
1208 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1210 case AV_OPT_TYPE_SAMPLE_FMT:
1211 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1213 case AV_OPT_TYPE_DURATION:
1214 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1216 case AV_OPT_TYPE_COLOR:
1217 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1219 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1220 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1222 case AV_OPT_TYPE_BOOL:
1223 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1225 case AV_OPT_TYPE_CONST:
1226 if (parent_type == AV_OPT_TYPE_INT)
1227 av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
1229 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1232 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1235 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1236 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1237 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1238 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1239 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1240 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1241 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1242 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1243 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.');
1244 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_RUNTIME_PARAM) ? 'T' : '.');
1247 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1249 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1250 switch (opt->type) {
1251 case AV_OPT_TYPE_INT:
1252 case AV_OPT_TYPE_INT64:
1253 case AV_OPT_TYPE_UINT64:
1254 case AV_OPT_TYPE_DOUBLE:
1255 case AV_OPT_TYPE_FLOAT:
1256 case AV_OPT_TYPE_RATIONAL:
1257 for (i = 0; i < r->nb_ranges; i++) {
1258 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1259 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1260 av_log(av_log_obj, AV_LOG_INFO, " to ");
1261 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1262 av_log(av_log_obj, AV_LOG_INFO, ")");
1266 av_opt_freep_ranges(&r);
1269 if (opt->type != AV_OPT_TYPE_CONST &&
1270 opt->type != AV_OPT_TYPE_BINARY &&
1271 !((opt->type == AV_OPT_TYPE_COLOR ||
1272 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1273 opt->type == AV_OPT_TYPE_STRING ||
1274 opt->type == AV_OPT_TYPE_DICT ||
1275 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1276 !opt->default_val.str)) {
1277 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1278 switch (opt->type) {
1279 case AV_OPT_TYPE_BOOL:
1280 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1282 case AV_OPT_TYPE_FLAGS: {
1283 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1285 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1286 av_freep(&def_flags);
1288 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1292 case AV_OPT_TYPE_DURATION: {
1294 format_duration(buf, sizeof(buf), opt->default_val.i64);
1295 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1298 case AV_OPT_TYPE_INT:
1299 case AV_OPT_TYPE_UINT64:
1300 case AV_OPT_TYPE_INT64: {
1301 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1303 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1305 log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1308 case AV_OPT_TYPE_DOUBLE:
1309 case AV_OPT_TYPE_FLOAT:
1310 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1312 case AV_OPT_TYPE_RATIONAL: {
1313 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1314 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1316 case AV_OPT_TYPE_PIXEL_FMT:
1317 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1319 case AV_OPT_TYPE_SAMPLE_FMT:
1320 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1322 case AV_OPT_TYPE_COLOR:
1323 case AV_OPT_TYPE_IMAGE_SIZE:
1324 case AV_OPT_TYPE_STRING:
1325 case AV_OPT_TYPE_DICT:
1326 case AV_OPT_TYPE_VIDEO_RATE:
1327 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1329 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1330 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1333 av_log(av_log_obj, AV_LOG_INFO, ")");
1336 av_log(av_log_obj, AV_LOG_INFO, "\n");
1337 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1338 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
1342 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1347 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1349 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
1354 void av_opt_set_defaults(void *s)
1356 av_opt_set_defaults2(s, 0, 0);
1359 void av_opt_set_defaults2(void *s, int mask, int flags)
1361 const AVOption *opt = NULL;
1362 while ((opt = av_opt_next(s, opt))) {
1363 void *dst = ((uint8_t*)s) + opt->offset;
1365 if ((opt->flags & mask) != flags)
1368 if (opt->flags & AV_OPT_FLAG_READONLY)
1371 switch (opt->type) {
1372 case AV_OPT_TYPE_CONST:
1373 /* Nothing to be done here */
1375 case AV_OPT_TYPE_BOOL:
1376 case AV_OPT_TYPE_FLAGS:
1377 case AV_OPT_TYPE_INT:
1378 case AV_OPT_TYPE_INT64:
1379 case AV_OPT_TYPE_UINT64:
1380 case AV_OPT_TYPE_DURATION:
1381 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1382 case AV_OPT_TYPE_PIXEL_FMT:
1383 case AV_OPT_TYPE_SAMPLE_FMT:
1384 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1386 case AV_OPT_TYPE_DOUBLE:
1387 case AV_OPT_TYPE_FLOAT: {
1389 val = opt->default_val.dbl;
1390 write_number(s, opt, dst, val, 1, 1);
1393 case AV_OPT_TYPE_RATIONAL: {
1395 val = av_d2q(opt->default_val.dbl, INT_MAX);
1396 write_number(s, opt, dst, 1, val.den, val.num);
1399 case AV_OPT_TYPE_COLOR:
1400 set_string_color(s, opt, opt->default_val.str, dst);
1402 case AV_OPT_TYPE_STRING:
1403 set_string(s, opt, opt->default_val.str, dst);
1405 case AV_OPT_TYPE_IMAGE_SIZE:
1406 set_string_image_size(s, opt, opt->default_val.str, dst);
1408 case AV_OPT_TYPE_VIDEO_RATE:
1409 set_string_video_rate(s, opt, opt->default_val.str, dst);
1411 case AV_OPT_TYPE_BINARY:
1412 set_string_binary(s, opt, opt->default_val.str, dst);
1414 case AV_OPT_TYPE_DICT:
1415 set_string_dict(s, opt, opt->default_val.str, dst);
1418 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1419 opt->type, opt->name);
1425 * Store the value in the field in ctx that is named like key.
1426 * ctx must be an AVClass context, storing is done using AVOptions.
1428 * @param buf the string to parse, buf will be updated to point at the
1429 * separator just after the parsed key/value pair
1430 * @param key_val_sep a 0-terminated list of characters used to
1431 * separate key from value
1432 * @param pairs_sep a 0-terminated list of characters used to separate
1433 * two pairs from each other
1434 * @return 0 if the key/value pair has been successfully parsed and
1435 * set, or a negative value corresponding to an AVERROR code in case
1437 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1438 * the error code issued by av_opt_set() if the key/value pair
1441 static int parse_key_value_pair(void *ctx, const char **buf,
1442 const char *key_val_sep, const char *pairs_sep)
1444 char *key = av_get_token(buf, key_val_sep);
1449 return AVERROR(ENOMEM);
1451 if (*key && strspn(*buf, key_val_sep)) {
1453 val = av_get_token(buf, pairs_sep);
1456 return AVERROR(ENOMEM);
1459 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1461 return AVERROR(EINVAL);
1464 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1466 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1467 if (ret == AVERROR_OPTION_NOT_FOUND)
1468 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1475 int av_set_options_string(void *ctx, const char *opts,
1476 const char *key_val_sep, const char *pairs_sep)
1484 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1495 #define WHITESPACES " \n\t\r"
1497 static int is_key_char(char c)
1499 return (unsigned)((c | 32) - 'a') < 26 ||
1500 (unsigned)(c - '0') < 10 ||
1501 c == '-' || c == '_' || c == '/' || c == '.';
1505 * Read a key from a string.
1507 * The key consists of is_key_char characters and must be terminated by a
1508 * character from the delim string; spaces are ignored.
1510 * @return 0 for success (even with ellipsis), <0 for failure
1512 static int get_key(const char **ropts, const char *delim, char **rkey)
1514 const char *opts = *ropts;
1515 const char *key_start, *key_end;
1517 key_start = opts += strspn(opts, WHITESPACES);
1518 while (is_key_char(*opts))
1521 opts += strspn(opts, WHITESPACES);
1522 if (!*opts || !strchr(delim, *opts))
1523 return AVERROR(EINVAL);
1525 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1526 return AVERROR(ENOMEM);
1527 memcpy(*rkey, key_start, key_end - key_start);
1528 (*rkey)[key_end - key_start] = 0;
1533 int av_opt_get_key_value(const char **ropts,
1534 const char *key_val_sep, const char *pairs_sep,
1536 char **rkey, char **rval)
1539 char *key = NULL, *val;
1540 const char *opts = *ropts;
1542 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1543 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1544 return AVERROR(EINVAL);
1545 if (!(val = av_get_token(&opts, pairs_sep))) {
1547 return AVERROR(ENOMEM);
1555 int av_opt_set_from_string(void *ctx, const char *opts,
1556 const char *const *shorthand,
1557 const char *key_val_sep, const char *pairs_sep)
1560 const char *dummy_shorthand = NULL;
1561 char *av_uninit(parsed_key), *av_uninit(value);
1567 shorthand = &dummy_shorthand;
1570 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1571 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1572 &parsed_key, &value);
1574 if (ret == AVERROR(EINVAL))
1575 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1577 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1585 while (*shorthand) /* discard all remaining shorthand */
1588 key = *(shorthand++);
1591 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1592 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1593 if (ret == AVERROR_OPTION_NOT_FOUND)
1594 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1596 av_free(parsed_key);
1601 av_free(parsed_key);
1607 void av_opt_free(void *obj)
1609 const AVOption *o = NULL;
1610 while ((o = av_opt_next(obj, o))) {
1612 case AV_OPT_TYPE_STRING:
1613 case AV_OPT_TYPE_BINARY:
1614 av_freep((uint8_t *)obj + o->offset);
1617 case AV_OPT_TYPE_DICT:
1618 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1627 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1629 AVDictionaryEntry *t = NULL;
1630 AVDictionary *tmp = NULL;
1636 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1637 ret = av_opt_set(obj, t->key, t->value, search_flags);
1638 if (ret == AVERROR_OPTION_NOT_FOUND)
1639 ret = av_dict_set(&tmp, t->key, t->value, 0);
1641 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1647 av_dict_free(options);
1652 int av_opt_set_dict(void *obj, AVDictionary **options)
1654 return av_opt_set_dict2(obj, options, 0);
1657 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1658 int opt_flags, int search_flags)
1660 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1663 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1664 int opt_flags, int search_flags, void **target_obj)
1667 const AVOption *o = NULL;
1677 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1678 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1679 const AVClass *child = NULL;
1680 while (child = av_opt_child_class_next(c, child))
1681 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1685 while (child = av_opt_child_next(obj, child))
1686 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1691 while (o = av_opt_next(obj, o)) {
1692 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1693 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1694 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1696 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1707 void *av_opt_child_next(void *obj, void *prev)
1709 const AVClass *c = *(AVClass **)obj;
1711 return c->child_next(obj, prev);
1715 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1717 if (parent->child_class_next)
1718 return parent->child_class_next(prev);
1722 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1724 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1727 return (uint8_t*)obj + opt->offset;
1730 static int opt_size(enum AVOptionType type)
1733 case AV_OPT_TYPE_BOOL:
1734 case AV_OPT_TYPE_INT:
1735 case AV_OPT_TYPE_FLAGS:
1737 case AV_OPT_TYPE_DURATION:
1738 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1739 case AV_OPT_TYPE_INT64:
1740 case AV_OPT_TYPE_UINT64:
1741 return sizeof(int64_t);
1742 case AV_OPT_TYPE_DOUBLE:
1743 return sizeof(double);
1744 case AV_OPT_TYPE_FLOAT:
1745 return sizeof(float);
1746 case AV_OPT_TYPE_STRING:
1747 return sizeof(uint8_t*);
1748 case AV_OPT_TYPE_VIDEO_RATE:
1749 case AV_OPT_TYPE_RATIONAL:
1750 return sizeof(AVRational);
1751 case AV_OPT_TYPE_BINARY:
1752 return sizeof(uint8_t*) + sizeof(int);
1753 case AV_OPT_TYPE_IMAGE_SIZE:
1754 return sizeof(int[2]);
1755 case AV_OPT_TYPE_PIXEL_FMT:
1756 return sizeof(enum AVPixelFormat);
1757 case AV_OPT_TYPE_SAMPLE_FMT:
1758 return sizeof(enum AVSampleFormat);
1759 case AV_OPT_TYPE_COLOR:
1762 return AVERROR(EINVAL);
1765 int av_opt_copy(void *dst, const void *src)
1767 const AVOption *o = NULL;
1772 return AVERROR(EINVAL);
1774 c = *(AVClass **)src;
1775 if (!c || c != *(AVClass **)dst)
1776 return AVERROR(EINVAL);
1778 while ((o = av_opt_next(src, o))) {
1779 void *field_dst = (uint8_t *)dst + o->offset;
1780 void *field_src = (uint8_t *)src + o->offset;
1781 uint8_t **field_dst8 = (uint8_t **)field_dst;
1782 uint8_t **field_src8 = (uint8_t **)field_src;
1784 if (o->type == AV_OPT_TYPE_STRING) {
1785 if (*field_dst8 != *field_src8)
1786 av_freep(field_dst8);
1787 *field_dst8 = av_strdup(*field_src8);
1788 if (*field_src8 && !*field_dst8)
1789 ret = AVERROR(ENOMEM);
1790 } else if (o->type == AV_OPT_TYPE_BINARY) {
1791 int len = *(int *)(field_src8 + 1);
1792 if (*field_dst8 != *field_src8)
1793 av_freep(field_dst8);
1794 *field_dst8 = av_memdup(*field_src8, len);
1795 if (len && !*field_dst8) {
1796 ret = AVERROR(ENOMEM);
1799 *(int *)(field_dst8 + 1) = len;
1800 } else if (o->type == AV_OPT_TYPE_CONST) {
1802 } else if (o->type == AV_OPT_TYPE_DICT) {
1803 AVDictionary **sdict = (AVDictionary **) field_src;
1804 AVDictionary **ddict = (AVDictionary **) field_dst;
1805 if (*sdict != *ddict)
1806 av_dict_free(ddict);
1808 av_dict_copy(ddict, *sdict, 0);
1809 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1810 ret = AVERROR(ENOMEM);
1812 int size = opt_size(o->type);
1816 memcpy(field_dst, field_src, size);
1822 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1825 const AVClass *c = *(AVClass**)obj;
1826 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1828 if (c->version > (52 << 16 | 11 << 8))
1829 callback = c->query_ranges;
1832 callback = av_opt_query_ranges_default;
1834 ret = callback(ranges_arg, obj, key, flags);
1836 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1838 (*ranges_arg)->nb_components = ret;
1843 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1845 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1846 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1847 AVOptionRange *range = av_mallocz(sizeof(*range));
1848 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1853 if (!ranges || !range || !range_array || !field) {
1854 ret = AVERROR(ENOMEM);
1858 ranges->range = range_array;
1859 ranges->range[0] = range;
1860 ranges->nb_ranges = 1;
1861 ranges->nb_components = 1;
1862 range->is_range = 1;
1863 range->value_min = field->min;
1864 range->value_max = field->max;
1866 switch (field->type) {
1867 case AV_OPT_TYPE_BOOL:
1868 case AV_OPT_TYPE_INT:
1869 case AV_OPT_TYPE_INT64:
1870 case AV_OPT_TYPE_UINT64:
1871 case AV_OPT_TYPE_PIXEL_FMT:
1872 case AV_OPT_TYPE_SAMPLE_FMT:
1873 case AV_OPT_TYPE_FLOAT:
1874 case AV_OPT_TYPE_DOUBLE:
1875 case AV_OPT_TYPE_DURATION:
1876 case AV_OPT_TYPE_COLOR:
1877 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1879 case AV_OPT_TYPE_STRING:
1880 range->component_min = 0;
1881 range->component_max = 0x10FFFF; // max unicode value
1882 range->value_min = -1;
1883 range->value_max = INT_MAX;
1885 case AV_OPT_TYPE_RATIONAL:
1886 range->component_min = INT_MIN;
1887 range->component_max = INT_MAX;
1889 case AV_OPT_TYPE_IMAGE_SIZE:
1890 range->component_min = 0;
1891 range->component_max = INT_MAX/128/8;
1892 range->value_min = 0;
1893 range->value_max = INT_MAX/8;
1895 case AV_OPT_TYPE_VIDEO_RATE:
1896 range->component_min = 1;
1897 range->component_max = INT_MAX;
1898 range->value_min = 1;
1899 range->value_max = INT_MAX;
1902 ret = AVERROR(ENOSYS);
1906 *ranges_arg = ranges;
1911 av_free(range_array);
1915 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1918 AVOptionRanges *ranges = *rangesp;
1923 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1924 AVOptionRange *range = ranges->range[i];
1926 av_freep(&range->str);
1927 av_freep(&ranges->range[i]);
1930 av_freep(&ranges->range);
1934 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1945 return AVERROR(EINVAL);
1947 dst = ((uint8_t*)obj) + o->offset;
1950 case AV_OPT_TYPE_CONST:
1952 case AV_OPT_TYPE_BOOL:
1953 case AV_OPT_TYPE_FLAGS:
1954 case AV_OPT_TYPE_PIXEL_FMT:
1955 case AV_OPT_TYPE_SAMPLE_FMT:
1956 case AV_OPT_TYPE_INT:
1957 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1958 case AV_OPT_TYPE_DURATION:
1959 case AV_OPT_TYPE_INT64:
1960 case AV_OPT_TYPE_UINT64:
1961 read_number(o, dst, NULL, NULL, &i64);
1962 return o->default_val.i64 == i64;
1963 case AV_OPT_TYPE_STRING:
1964 str = *(char **)dst;
1965 if (str == o->default_val.str) //2 NULLs
1967 if (!str || !o->default_val.str) //1 NULL
1969 return !strcmp(str, o->default_val.str);
1970 case AV_OPT_TYPE_DOUBLE:
1971 read_number(o, dst, &d, NULL, NULL);
1972 return o->default_val.dbl == d;
1973 case AV_OPT_TYPE_FLOAT:
1974 read_number(o, dst, &d, NULL, NULL);
1975 f = o->default_val.dbl;
1978 case AV_OPT_TYPE_RATIONAL:
1979 q = av_d2q(o->default_val.dbl, INT_MAX);
1980 return !av_cmp_q(*(AVRational*)dst, q);
1981 case AV_OPT_TYPE_BINARY: {
1986 int opt_size = *(int *)((void **)dst + 1);
1987 void *opt_ptr = *(void **)dst;
1988 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1990 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1992 if (opt_size != strlen(o->default_val.str) / 2)
1994 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1996 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
2000 case AV_OPT_TYPE_DICT: {
2001 AVDictionary *dict1 = NULL;
2002 AVDictionary *dict2 = *(AVDictionary **)dst;
2003 AVDictionaryEntry *en1 = NULL;
2004 AVDictionaryEntry *en2 = NULL;
2005 ret = av_dict_parse_string(&dict1, o->default_val.str, "=", ":", 0);
2007 av_dict_free(&dict1);
2011 en1 = av_dict_get(dict1, "", en1, AV_DICT_IGNORE_SUFFIX);
2012 en2 = av_dict_get(dict2, "", en2, AV_DICT_IGNORE_SUFFIX);
2013 } while (en1 && en2 && !strcmp(en1->key, en2->key) && !strcmp(en1->value, en2->value));
2014 av_dict_free(&dict1);
2015 return (!en1 && !en2);
2017 case AV_OPT_TYPE_IMAGE_SIZE:
2018 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
2020 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
2022 return (w == *(int *)dst) && (h == *((int *)dst+1));
2023 case AV_OPT_TYPE_VIDEO_RATE:
2024 q = (AVRational){0, 0};
2025 if (o->default_val.str) {
2026 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
2029 return !av_cmp_q(*(AVRational*)dst, q);
2030 case AV_OPT_TYPE_COLOR: {
2031 uint8_t color[4] = {0, 0, 0, 0};
2032 if (o->default_val.str) {
2033 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
2036 return !memcmp(color, dst, sizeof(color));
2039 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
2042 return AVERROR_PATCHWELCOME;
2045 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
2050 return AVERROR(EINVAL);
2051 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
2053 return AVERROR_OPTION_NOT_FOUND;
2054 return av_opt_is_set_to_default(target, o);
2057 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
2058 const char key_val_sep, const char pairs_sep)
2060 const AVOption *o = NULL;
2064 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
2066 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
2067 pairs_sep == '\\' || key_val_sep == '\\') {
2068 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
2069 return AVERROR(EINVAL);
2072 if (!obj || !buffer)
2073 return AVERROR(EINVAL);
2076 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
2078 while (o = av_opt_next(obj, o)) {
2079 if (o->type == AV_OPT_TYPE_CONST)
2081 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2083 else if (((o->flags & opt_flags) != opt_flags))
2085 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
2087 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2088 av_bprint_finalize(&bprint, NULL);
2093 av_bprint_append_data(&bprint, &pairs_sep, 1);
2094 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2095 av_bprint_append_data(&bprint, &key_val_sep, 1);
2096 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2100 av_bprint_finalize(&bprint, buffer);