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)
335 ret = AVERROR(EINVAL);
337 ret = av_parse_video_rate(dst, val);
340 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
344 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
351 ret = av_parse_color(dst, val, -1, obj);
353 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
359 static const char *get_bool_name(int val)
363 return val ? "true" : "false";
366 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
373 if (!strcmp(val, "auto")) {
375 } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
377 } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
381 n = strtol(val, &end, 10);
382 if (val + strlen(val) != end)
386 if (n < o->min || n > o->max)
393 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
394 return AVERROR(EINVAL);
397 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
398 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
402 if (!val || !strcmp(val, "none")) {
408 fmt = strtol(val, &tail, 0);
409 if (*tail || (unsigned)fmt >= fmt_nb) {
410 av_log(obj, AV_LOG_ERROR,
411 "Unable to parse option value \"%s\" as %s\n", val, desc);
412 return AVERROR(EINVAL);
417 min = FFMAX(o->min, -1);
418 max = FFMIN(o->max, fmt_nb-1);
420 // hack for compatibility with old ffmpeg
421 if(min == 0 && max == 0) {
426 if (fmt < min || fmt > max) {
427 av_log(obj, AV_LOG_ERROR,
428 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
429 fmt, o->name, desc, min, max);
430 return AVERROR(ERANGE);
437 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
439 return set_string_fmt(obj, o, val, dst,
440 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
443 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
445 return set_string_fmt(obj, o, val, dst,
446 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
449 static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
451 AVDictionary *options = NULL;
454 int ret = av_dict_parse_string(&options, val, "=", ":", 0);
456 av_dict_free(&options);
461 av_dict_free((AVDictionary **)dst);
462 *dst = (uint8_t *)options;
467 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
470 void *dst, *target_obj;
471 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
472 if (!o || !target_obj)
473 return AVERROR_OPTION_NOT_FOUND;
474 if (!val && (o->type != AV_OPT_TYPE_STRING &&
475 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
476 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
477 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
478 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
479 return AVERROR(EINVAL);
481 if (o->flags & AV_OPT_FLAG_READONLY)
482 return AVERROR(EINVAL);
484 if (o->flags & AV_OPT_FLAG_DEPRECATED)
485 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
487 dst = ((uint8_t *)target_obj) + o->offset;
489 case AV_OPT_TYPE_BOOL:
490 return set_string_bool(obj, o, val, dst);
491 case AV_OPT_TYPE_STRING:
492 return set_string(obj, o, val, dst);
493 case AV_OPT_TYPE_BINARY:
494 return set_string_binary(obj, o, val, dst);
495 case AV_OPT_TYPE_FLAGS:
496 case AV_OPT_TYPE_INT:
497 case AV_OPT_TYPE_INT64:
498 case AV_OPT_TYPE_UINT64:
499 case AV_OPT_TYPE_FLOAT:
500 case AV_OPT_TYPE_DOUBLE:
501 case AV_OPT_TYPE_RATIONAL:
502 return set_string_number(obj, target_obj, o, val, dst);
503 case AV_OPT_TYPE_IMAGE_SIZE:
504 return set_string_image_size(obj, o, val, dst);
505 case AV_OPT_TYPE_VIDEO_RATE: {
507 ret = set_string_video_rate(obj, o, val, &tmp);
510 return write_number(obj, o, dst, 1, tmp.den, tmp.num);
512 case AV_OPT_TYPE_PIXEL_FMT:
513 return set_string_pixel_fmt(obj, o, val, dst);
514 case AV_OPT_TYPE_SAMPLE_FMT:
515 return set_string_sample_fmt(obj, o, val, dst);
516 case AV_OPT_TYPE_DURATION:
520 if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
521 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
525 if (usecs < o->min || usecs > o->max) {
526 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
527 usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
528 return AVERROR(ERANGE);
530 *(int64_t *)dst = usecs;
533 case AV_OPT_TYPE_COLOR:
534 return set_string_color(obj, o, val, dst);
535 case AV_OPT_TYPE_CHANNEL_LAYOUT:
536 if (!val || !strcmp(val, "none")) {
539 int64_t cl = av_get_channel_layout(val);
541 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
542 ret = AVERROR(EINVAL);
544 *(int64_t *)dst = cl;
548 case AV_OPT_TYPE_DICT:
549 return set_string_dict(obj, o, val, dst);
552 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
553 return AVERROR(EINVAL);
556 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
557 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
558 const char *val, vartype *name ## _out) \
560 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
561 return AVERROR(EINVAL); \
562 return set_string_number(obj, obj, o, val, name ## _out); \
565 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
566 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
567 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
568 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
569 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
570 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
572 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
575 void *dst, *target_obj;
576 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
578 if (!o || !target_obj)
579 return AVERROR_OPTION_NOT_FOUND;
581 if (o->flags & AV_OPT_FLAG_READONLY)
582 return AVERROR(EINVAL);
584 dst = ((uint8_t *)target_obj) + o->offset;
585 return write_number(obj, o, dst, num, den, intnum);
588 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
590 return set_number(obj, name, 1, 1, val, search_flags);
593 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
595 return set_number(obj, name, val, 1, 1, search_flags);
598 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
600 return set_number(obj, name, val.num, val.den, 1, search_flags);
603 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
606 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
611 if (!o || !target_obj)
612 return AVERROR_OPTION_NOT_FOUND;
614 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
615 return AVERROR(EINVAL);
617 ptr = len ? av_malloc(len) : NULL;
619 return AVERROR(ENOMEM);
621 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
622 lendst = (int *)(dst + 1);
628 memcpy(ptr, val, len);
633 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
636 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
638 if (!o || !target_obj)
639 return AVERROR_OPTION_NOT_FOUND;
640 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
641 av_log(obj, AV_LOG_ERROR,
642 "The value set by option '%s' is not an image size.\n", o->name);
643 return AVERROR(EINVAL);
646 av_log(obj, AV_LOG_ERROR,
647 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
648 return AVERROR(EINVAL);
650 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
651 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
655 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
658 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
660 if (!o || !target_obj)
661 return AVERROR_OPTION_NOT_FOUND;
662 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
663 av_log(obj, AV_LOG_ERROR,
664 "The value set by option '%s' is not a video rate.\n", o->name);
665 return AVERROR(EINVAL);
667 if (val.num <= 0 || val.den <= 0)
668 return AVERROR(EINVAL);
669 return set_number(obj, name, val.num, val.den, 1, search_flags);
672 static int set_format(void *obj, const char *name, int fmt, int search_flags,
673 enum AVOptionType type, const char *desc, int nb_fmts)
676 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
677 search_flags, &target_obj);
680 if (!o || !target_obj)
681 return AVERROR_OPTION_NOT_FOUND;
682 if (o->type != type) {
683 av_log(obj, AV_LOG_ERROR,
684 "The value set by option '%s' is not a %s format", name, desc);
685 return AVERROR(EINVAL);
688 min = FFMAX(o->min, -1);
689 max = FFMIN(o->max, nb_fmts-1);
691 if (fmt < min || fmt > max) {
692 av_log(obj, AV_LOG_ERROR,
693 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
694 fmt, name, desc, min, max);
695 return AVERROR(ERANGE);
697 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
701 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
703 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
706 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
708 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
711 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
714 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
716 if (!o || !target_obj)
717 return AVERROR_OPTION_NOT_FOUND;
718 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
719 av_log(obj, AV_LOG_ERROR,
720 "The value set by option '%s' is not a channel layout.\n", o->name);
721 return AVERROR(EINVAL);
723 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
727 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
732 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
734 if (!o || !target_obj)
735 return AVERROR_OPTION_NOT_FOUND;
736 if (o->flags & AV_OPT_FLAG_READONLY)
737 return AVERROR(EINVAL);
739 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
741 av_dict_copy(dst, val, 0);
746 static void format_duration(char *buf, size_t size, int64_t d)
750 av_assert0(size >= 25);
751 if (d < 0 && d != INT64_MIN) {
757 snprintf(buf, size, "INT64_MAX");
758 else if (d == INT64_MIN)
759 snprintf(buf, size, "INT64_MIN");
760 else if (d > (int64_t)3600*1000000)
761 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
762 (int)((d / 60000000) % 60),
763 (int)((d / 1000000) % 60),
765 else if (d > 60*1000000)
766 snprintf(buf, size, "%d:%02d.%06d",
768 (int)((d / 1000000) % 60),
771 snprintf(buf, size, "%d.%06d",
774 e = buf + strlen(buf);
775 while (e > buf && e[-1] == '0')
777 if (e > buf && e[-1] == '.')
781 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
783 void *dst, *target_obj;
784 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
785 uint8_t *bin, buf[128];
789 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
790 return AVERROR_OPTION_NOT_FOUND;
792 if (o->flags & AV_OPT_FLAG_DEPRECATED)
793 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
795 dst = (uint8_t *)target_obj + o->offset;
799 case AV_OPT_TYPE_BOOL:
800 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
802 case AV_OPT_TYPE_FLAGS:
803 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
805 case AV_OPT_TYPE_INT:
806 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
808 case AV_OPT_TYPE_INT64:
809 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
811 case AV_OPT_TYPE_UINT64:
812 ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst);
814 case AV_OPT_TYPE_FLOAT:
815 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
817 case AV_OPT_TYPE_DOUBLE:
818 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
820 case AV_OPT_TYPE_VIDEO_RATE:
821 case AV_OPT_TYPE_RATIONAL:
822 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
824 case AV_OPT_TYPE_CONST:
825 ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
827 case AV_OPT_TYPE_STRING:
828 if (*(uint8_t **)dst) {
829 *out_val = av_strdup(*(uint8_t **)dst);
830 } else if (search_flags & AV_OPT_ALLOW_NULL) {
834 *out_val = av_strdup("");
836 return *out_val ? 0 : AVERROR(ENOMEM);
837 case AV_OPT_TYPE_BINARY:
838 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
842 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
843 if ((uint64_t)len * 2 + 1 > INT_MAX)
844 return AVERROR(EINVAL);
845 if (!(*out_val = av_malloc(len * 2 + 1)))
846 return AVERROR(ENOMEM);
851 bin = *(uint8_t **)dst;
852 for (i = 0; i < len; i++)
853 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
855 case AV_OPT_TYPE_IMAGE_SIZE:
856 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
858 case AV_OPT_TYPE_PIXEL_FMT:
859 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
861 case AV_OPT_TYPE_SAMPLE_FMT:
862 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
864 case AV_OPT_TYPE_DURATION:
865 i64 = *(int64_t *)dst;
866 format_duration(buf, sizeof(buf), i64);
867 ret = strlen(buf); // no overflow possible, checked by an assert
869 case AV_OPT_TYPE_COLOR:
870 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
871 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
872 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
874 case AV_OPT_TYPE_CHANNEL_LAYOUT:
875 i64 = *(int64_t *)dst;
876 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
878 case AV_OPT_TYPE_DICT:
879 if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
883 return av_dict_get_string(*(AVDictionary **)dst, (char **)out_val, '=', ':');
885 return AVERROR(EINVAL);
888 if (ret >= sizeof(buf))
889 return AVERROR(EINVAL);
890 *out_val = av_strdup(buf);
891 return *out_val ? 0 : AVERROR(ENOMEM);
894 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
897 void *dst, *target_obj;
898 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
899 if (!o || !target_obj)
902 dst = ((uint8_t *)target_obj) + o->offset;
904 if (o_out) *o_out= o;
906 return read_number(o, dst, num, den, intnum);
914 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
920 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
922 *out_val = num * intnum / den;
926 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
932 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
934 *out_val = num * intnum / den;
938 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
944 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
947 if (num == 1.0 && (int)intnum == intnum)
948 *out_val = (AVRational){intnum, den};
950 *out_val = av_d2q(num*intnum/den, 1<<24);
954 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
956 void *dst, *target_obj;
957 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
958 if (!o || !target_obj)
959 return AVERROR_OPTION_NOT_FOUND;
960 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
961 av_log(obj, AV_LOG_ERROR,
962 "The value for option '%s' is not an image size.\n", name);
963 return AVERROR(EINVAL);
966 dst = ((uint8_t*)target_obj) + o->offset;
967 if (w_out) *w_out = *(int *)dst;
968 if (h_out) *h_out = *((int *)dst+1);
972 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
978 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
981 if (num == 1.0 && (int)intnum == intnum)
982 *out_val = (AVRational) { intnum, den };
984 *out_val = av_d2q(num * intnum / den, 1 << 24);
988 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
989 enum AVOptionType type, const char *desc)
991 void *dst, *target_obj;
992 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
993 if (!o || !target_obj)
994 return AVERROR_OPTION_NOT_FOUND;
995 if (o->type != type) {
996 av_log(obj, AV_LOG_ERROR,
997 "The value for option '%s' is not a %s format.\n", desc, name);
998 return AVERROR(EINVAL);
1001 dst = ((uint8_t*)target_obj) + o->offset;
1002 *out_fmt = *(int *)dst;
1006 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
1008 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
1011 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
1013 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
1016 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
1018 void *dst, *target_obj;
1019 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1020 if (!o || !target_obj)
1021 return AVERROR_OPTION_NOT_FOUND;
1022 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
1023 av_log(obj, AV_LOG_ERROR,
1024 "The value for option '%s' is not a channel layout.\n", name);
1025 return AVERROR(EINVAL);
1028 dst = ((uint8_t*)target_obj) + o->offset;
1029 *cl = *(int64_t *)dst;
1033 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1037 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1039 if (!o || !target_obj)
1040 return AVERROR_OPTION_NOT_FOUND;
1041 if (o->type != AV_OPT_TYPE_DICT)
1042 return AVERROR(EINVAL);
1044 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1045 av_dict_copy(out_val, src, 0);
1050 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1052 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1053 const AVOption *flag = av_opt_find(obj, flag_name,
1054 field ? field->unit : NULL, 0, 0);
1057 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1058 av_opt_get_int(obj, field_name, 0, &res) < 0)
1060 return res & flag->default_val.i64;
1063 static void log_int_value(void *av_log_obj, int level, int64_t i)
1066 av_log(av_log_obj, level, "INT_MAX");
1067 } else if (i == INT_MIN) {
1068 av_log(av_log_obj, level, "INT_MIN");
1069 } else if (i == UINT32_MAX) {
1070 av_log(av_log_obj, level, "UINT32_MAX");
1071 } else if (i == INT64_MAX) {
1072 av_log(av_log_obj, level, "I64_MAX");
1073 } else if (i == INT64_MIN) {
1074 av_log(av_log_obj, level, "I64_MIN");
1076 av_log(av_log_obj, level, "%"PRId64, i);
1080 static void log_value(void *av_log_obj, int level, double d)
1083 av_log(av_log_obj, level, "INT_MAX");
1084 } else if (d == INT_MIN) {
1085 av_log(av_log_obj, level, "INT_MIN");
1086 } else if (d == UINT32_MAX) {
1087 av_log(av_log_obj, level, "UINT32_MAX");
1088 } else if (d == (double)INT64_MAX) {
1089 av_log(av_log_obj, level, "I64_MAX");
1090 } else if (d == INT64_MIN) {
1091 av_log(av_log_obj, level, "I64_MIN");
1092 } else if (d == FLT_MAX) {
1093 av_log(av_log_obj, level, "FLT_MAX");
1094 } else if (d == FLT_MIN) {
1095 av_log(av_log_obj, level, "FLT_MIN");
1096 } else if (d == -FLT_MAX) {
1097 av_log(av_log_obj, level, "-FLT_MAX");
1098 } else if (d == -FLT_MIN) {
1099 av_log(av_log_obj, level, "-FLT_MIN");
1100 } else if (d == DBL_MAX) {
1101 av_log(av_log_obj, level, "DBL_MAX");
1102 } else if (d == DBL_MIN) {
1103 av_log(av_log_obj, level, "DBL_MIN");
1104 } else if (d == -DBL_MAX) {
1105 av_log(av_log_obj, level, "-DBL_MAX");
1106 } else if (d == -DBL_MIN) {
1107 av_log(av_log_obj, level, "-DBL_MIN");
1109 av_log(av_log_obj, level, "%g", d);
1113 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1115 const AVOption *opt = NULL;
1119 while ((opt = av_opt_next(obj, opt)))
1120 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1121 opt->default_val.i64 == value)
1126 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1128 const AVOption *opt = NULL;
1134 while ((opt = av_opt_next(obj, opt))) {
1135 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1136 opt->default_val.i64 & value) {
1138 av_strlcatf(flags, sizeof(flags), "+");
1139 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1143 return av_strdup(flags);
1147 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1148 int req_flags, int rej_flags, enum AVOptionType parent_type)
1150 const AVOption *opt = NULL;
1154 while ((opt = av_opt_next(obj, opt))) {
1155 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1158 /* Don't print CONST's on level one.
1159 * Don't print anything but CONST's on level two.
1160 * Only print items from the requested unit.
1162 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1164 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1166 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1168 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1169 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1171 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1172 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1175 switch (opt->type) {
1176 case AV_OPT_TYPE_FLAGS:
1177 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1179 case AV_OPT_TYPE_INT:
1180 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1182 case AV_OPT_TYPE_INT64:
1183 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1185 case AV_OPT_TYPE_UINT64:
1186 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
1188 case AV_OPT_TYPE_DOUBLE:
1189 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1191 case AV_OPT_TYPE_FLOAT:
1192 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1194 case AV_OPT_TYPE_STRING:
1195 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1197 case AV_OPT_TYPE_RATIONAL:
1198 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1200 case AV_OPT_TYPE_BINARY:
1201 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1203 case AV_OPT_TYPE_DICT:
1204 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<dictionary>");
1206 case AV_OPT_TYPE_IMAGE_SIZE:
1207 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1209 case AV_OPT_TYPE_VIDEO_RATE:
1210 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1212 case AV_OPT_TYPE_PIXEL_FMT:
1213 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1215 case AV_OPT_TYPE_SAMPLE_FMT:
1216 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1218 case AV_OPT_TYPE_DURATION:
1219 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1221 case AV_OPT_TYPE_COLOR:
1222 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1224 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1225 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1227 case AV_OPT_TYPE_BOOL:
1228 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1230 case AV_OPT_TYPE_CONST:
1231 if (parent_type == AV_OPT_TYPE_INT)
1232 av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
1234 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1237 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1240 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1241 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1242 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1243 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1244 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1245 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1246 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1247 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1248 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.');
1249 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_RUNTIME_PARAM) ? 'T' : '.');
1252 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1254 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1255 switch (opt->type) {
1256 case AV_OPT_TYPE_INT:
1257 case AV_OPT_TYPE_INT64:
1258 case AV_OPT_TYPE_UINT64:
1259 case AV_OPT_TYPE_DOUBLE:
1260 case AV_OPT_TYPE_FLOAT:
1261 case AV_OPT_TYPE_RATIONAL:
1262 for (i = 0; i < r->nb_ranges; i++) {
1263 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1264 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1265 av_log(av_log_obj, AV_LOG_INFO, " to ");
1266 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1267 av_log(av_log_obj, AV_LOG_INFO, ")");
1271 av_opt_freep_ranges(&r);
1274 if (opt->type != AV_OPT_TYPE_CONST &&
1275 opt->type != AV_OPT_TYPE_BINARY &&
1276 !((opt->type == AV_OPT_TYPE_COLOR ||
1277 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1278 opt->type == AV_OPT_TYPE_STRING ||
1279 opt->type == AV_OPT_TYPE_DICT ||
1280 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1281 !opt->default_val.str)) {
1282 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1283 switch (opt->type) {
1284 case AV_OPT_TYPE_BOOL:
1285 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1287 case AV_OPT_TYPE_FLAGS: {
1288 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1290 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1291 av_freep(&def_flags);
1293 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1297 case AV_OPT_TYPE_DURATION: {
1299 format_duration(buf, sizeof(buf), opt->default_val.i64);
1300 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1303 case AV_OPT_TYPE_INT:
1304 case AV_OPT_TYPE_UINT64:
1305 case AV_OPT_TYPE_INT64: {
1306 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1308 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1310 log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1313 case AV_OPT_TYPE_DOUBLE:
1314 case AV_OPT_TYPE_FLOAT:
1315 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1317 case AV_OPT_TYPE_RATIONAL: {
1318 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1319 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1321 case AV_OPT_TYPE_PIXEL_FMT:
1322 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1324 case AV_OPT_TYPE_SAMPLE_FMT:
1325 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1327 case AV_OPT_TYPE_COLOR:
1328 case AV_OPT_TYPE_IMAGE_SIZE:
1329 case AV_OPT_TYPE_STRING:
1330 case AV_OPT_TYPE_DICT:
1331 case AV_OPT_TYPE_VIDEO_RATE:
1332 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1334 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1335 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1338 av_log(av_log_obj, AV_LOG_INFO, ")");
1341 av_log(av_log_obj, AV_LOG_INFO, "\n");
1342 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1343 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
1347 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1352 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1354 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
1359 void av_opt_set_defaults(void *s)
1361 av_opt_set_defaults2(s, 0, 0);
1364 void av_opt_set_defaults2(void *s, int mask, int flags)
1366 const AVOption *opt = NULL;
1367 while ((opt = av_opt_next(s, opt))) {
1368 void *dst = ((uint8_t*)s) + opt->offset;
1370 if ((opt->flags & mask) != flags)
1373 if (opt->flags & AV_OPT_FLAG_READONLY)
1376 switch (opt->type) {
1377 case AV_OPT_TYPE_CONST:
1378 /* Nothing to be done here */
1380 case AV_OPT_TYPE_BOOL:
1381 case AV_OPT_TYPE_FLAGS:
1382 case AV_OPT_TYPE_INT:
1383 case AV_OPT_TYPE_INT64:
1384 case AV_OPT_TYPE_UINT64:
1385 case AV_OPT_TYPE_DURATION:
1386 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1387 case AV_OPT_TYPE_PIXEL_FMT:
1388 case AV_OPT_TYPE_SAMPLE_FMT:
1389 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1391 case AV_OPT_TYPE_DOUBLE:
1392 case AV_OPT_TYPE_FLOAT: {
1394 val = opt->default_val.dbl;
1395 write_number(s, opt, dst, val, 1, 1);
1398 case AV_OPT_TYPE_RATIONAL: {
1400 val = av_d2q(opt->default_val.dbl, INT_MAX);
1401 write_number(s, opt, dst, 1, val.den, val.num);
1404 case AV_OPT_TYPE_COLOR:
1405 set_string_color(s, opt, opt->default_val.str, dst);
1407 case AV_OPT_TYPE_STRING:
1408 set_string(s, opt, opt->default_val.str, dst);
1410 case AV_OPT_TYPE_IMAGE_SIZE:
1411 set_string_image_size(s, opt, opt->default_val.str, dst);
1413 case AV_OPT_TYPE_VIDEO_RATE:
1414 set_string_video_rate(s, opt, opt->default_val.str, dst);
1416 case AV_OPT_TYPE_BINARY:
1417 set_string_binary(s, opt, opt->default_val.str, dst);
1419 case AV_OPT_TYPE_DICT:
1420 set_string_dict(s, opt, opt->default_val.str, dst);
1423 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1424 opt->type, opt->name);
1430 * Store the value in the field in ctx that is named like key.
1431 * ctx must be an AVClass context, storing is done using AVOptions.
1433 * @param buf the string to parse, buf will be updated to point at the
1434 * separator just after the parsed key/value pair
1435 * @param key_val_sep a 0-terminated list of characters used to
1436 * separate key from value
1437 * @param pairs_sep a 0-terminated list of characters used to separate
1438 * two pairs from each other
1439 * @return 0 if the key/value pair has been successfully parsed and
1440 * set, or a negative value corresponding to an AVERROR code in case
1442 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1443 * the error code issued by av_opt_set() if the key/value pair
1446 static int parse_key_value_pair(void *ctx, const char **buf,
1447 const char *key_val_sep, const char *pairs_sep)
1449 char *key = av_get_token(buf, key_val_sep);
1454 return AVERROR(ENOMEM);
1456 if (*key && strspn(*buf, key_val_sep)) {
1458 val = av_get_token(buf, pairs_sep);
1461 return AVERROR(ENOMEM);
1464 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1466 return AVERROR(EINVAL);
1469 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1471 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1472 if (ret == AVERROR_OPTION_NOT_FOUND)
1473 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1480 int av_set_options_string(void *ctx, const char *opts,
1481 const char *key_val_sep, const char *pairs_sep)
1489 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1500 #define WHITESPACES " \n\t\r"
1502 static int is_key_char(char c)
1504 return (unsigned)((c | 32) - 'a') < 26 ||
1505 (unsigned)(c - '0') < 10 ||
1506 c == '-' || c == '_' || c == '/' || c == '.';
1510 * Read a key from a string.
1512 * The key consists of is_key_char characters and must be terminated by a
1513 * character from the delim string; spaces are ignored.
1515 * @return 0 for success (even with ellipsis), <0 for failure
1517 static int get_key(const char **ropts, const char *delim, char **rkey)
1519 const char *opts = *ropts;
1520 const char *key_start, *key_end;
1522 key_start = opts += strspn(opts, WHITESPACES);
1523 while (is_key_char(*opts))
1526 opts += strspn(opts, WHITESPACES);
1527 if (!*opts || !strchr(delim, *opts))
1528 return AVERROR(EINVAL);
1530 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1531 return AVERROR(ENOMEM);
1532 memcpy(*rkey, key_start, key_end - key_start);
1533 (*rkey)[key_end - key_start] = 0;
1538 int av_opt_get_key_value(const char **ropts,
1539 const char *key_val_sep, const char *pairs_sep,
1541 char **rkey, char **rval)
1544 char *key = NULL, *val;
1545 const char *opts = *ropts;
1547 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1548 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1549 return AVERROR(EINVAL);
1550 if (!(val = av_get_token(&opts, pairs_sep))) {
1552 return AVERROR(ENOMEM);
1560 int av_opt_set_from_string(void *ctx, const char *opts,
1561 const char *const *shorthand,
1562 const char *key_val_sep, const char *pairs_sep)
1565 const char *dummy_shorthand = NULL;
1566 char *av_uninit(parsed_key), *av_uninit(value);
1572 shorthand = &dummy_shorthand;
1575 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1576 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1577 &parsed_key, &value);
1579 if (ret == AVERROR(EINVAL))
1580 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1582 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1590 while (*shorthand) /* discard all remaining shorthand */
1593 key = *(shorthand++);
1596 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1597 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1598 if (ret == AVERROR_OPTION_NOT_FOUND)
1599 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1601 av_free(parsed_key);
1606 av_free(parsed_key);
1612 void av_opt_free(void *obj)
1614 const AVOption *o = NULL;
1615 while ((o = av_opt_next(obj, o))) {
1617 case AV_OPT_TYPE_STRING:
1618 case AV_OPT_TYPE_BINARY:
1619 av_freep((uint8_t *)obj + o->offset);
1622 case AV_OPT_TYPE_DICT:
1623 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1632 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1634 AVDictionaryEntry *t = NULL;
1635 AVDictionary *tmp = NULL;
1641 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1642 ret = av_opt_set(obj, t->key, t->value, search_flags);
1643 if (ret == AVERROR_OPTION_NOT_FOUND)
1644 ret = av_dict_set(&tmp, t->key, t->value, 0);
1646 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1652 av_dict_free(options);
1657 int av_opt_set_dict(void *obj, AVDictionary **options)
1659 return av_opt_set_dict2(obj, options, 0);
1662 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1663 int opt_flags, int search_flags)
1665 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1668 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1669 int opt_flags, int search_flags, void **target_obj)
1672 const AVOption *o = NULL;
1682 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1683 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1684 const AVClass *child = NULL;
1685 while (child = av_opt_child_class_next(c, child))
1686 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1690 while (child = av_opt_child_next(obj, child))
1691 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1696 while (o = av_opt_next(obj, o)) {
1697 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1698 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1699 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1701 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1712 void *av_opt_child_next(void *obj, void *prev)
1714 const AVClass *c = *(AVClass **)obj;
1716 return c->child_next(obj, prev);
1720 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1722 if (parent->child_class_next)
1723 return parent->child_class_next(prev);
1727 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1729 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1732 return (uint8_t*)obj + opt->offset;
1735 static int opt_size(enum AVOptionType type)
1738 case AV_OPT_TYPE_BOOL:
1739 case AV_OPT_TYPE_INT:
1740 case AV_OPT_TYPE_FLAGS:
1742 case AV_OPT_TYPE_DURATION:
1743 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1744 case AV_OPT_TYPE_INT64:
1745 case AV_OPT_TYPE_UINT64:
1746 return sizeof(int64_t);
1747 case AV_OPT_TYPE_DOUBLE:
1748 return sizeof(double);
1749 case AV_OPT_TYPE_FLOAT:
1750 return sizeof(float);
1751 case AV_OPT_TYPE_STRING:
1752 return sizeof(uint8_t*);
1753 case AV_OPT_TYPE_VIDEO_RATE:
1754 case AV_OPT_TYPE_RATIONAL:
1755 return sizeof(AVRational);
1756 case AV_OPT_TYPE_BINARY:
1757 return sizeof(uint8_t*) + sizeof(int);
1758 case AV_OPT_TYPE_IMAGE_SIZE:
1759 return sizeof(int[2]);
1760 case AV_OPT_TYPE_PIXEL_FMT:
1761 return sizeof(enum AVPixelFormat);
1762 case AV_OPT_TYPE_SAMPLE_FMT:
1763 return sizeof(enum AVSampleFormat);
1764 case AV_OPT_TYPE_COLOR:
1767 return AVERROR(EINVAL);
1770 int av_opt_copy(void *dst, const void *src)
1772 const AVOption *o = NULL;
1777 return AVERROR(EINVAL);
1779 c = *(AVClass **)src;
1780 if (!c || c != *(AVClass **)dst)
1781 return AVERROR(EINVAL);
1783 while ((o = av_opt_next(src, o))) {
1784 void *field_dst = (uint8_t *)dst + o->offset;
1785 void *field_src = (uint8_t *)src + o->offset;
1786 uint8_t **field_dst8 = (uint8_t **)field_dst;
1787 uint8_t **field_src8 = (uint8_t **)field_src;
1789 if (o->type == AV_OPT_TYPE_STRING) {
1790 if (*field_dst8 != *field_src8)
1791 av_freep(field_dst8);
1792 *field_dst8 = av_strdup(*field_src8);
1793 if (*field_src8 && !*field_dst8)
1794 ret = AVERROR(ENOMEM);
1795 } else if (o->type == AV_OPT_TYPE_BINARY) {
1796 int len = *(int *)(field_src8 + 1);
1797 if (*field_dst8 != *field_src8)
1798 av_freep(field_dst8);
1799 *field_dst8 = av_memdup(*field_src8, len);
1800 if (len && !*field_dst8) {
1801 ret = AVERROR(ENOMEM);
1804 *(int *)(field_dst8 + 1) = len;
1805 } else if (o->type == AV_OPT_TYPE_CONST) {
1807 } else if (o->type == AV_OPT_TYPE_DICT) {
1808 AVDictionary **sdict = (AVDictionary **) field_src;
1809 AVDictionary **ddict = (AVDictionary **) field_dst;
1810 if (*sdict != *ddict)
1811 av_dict_free(ddict);
1813 av_dict_copy(ddict, *sdict, 0);
1814 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1815 ret = AVERROR(ENOMEM);
1817 int size = opt_size(o->type);
1821 memcpy(field_dst, field_src, size);
1827 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1830 const AVClass *c = *(AVClass**)obj;
1831 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1833 if (c->version > (52 << 16 | 11 << 8))
1834 callback = c->query_ranges;
1837 callback = av_opt_query_ranges_default;
1839 ret = callback(ranges_arg, obj, key, flags);
1841 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1843 (*ranges_arg)->nb_components = ret;
1848 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1850 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1851 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1852 AVOptionRange *range = av_mallocz(sizeof(*range));
1853 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1858 if (!ranges || !range || !range_array || !field) {
1859 ret = AVERROR(ENOMEM);
1863 ranges->range = range_array;
1864 ranges->range[0] = range;
1865 ranges->nb_ranges = 1;
1866 ranges->nb_components = 1;
1867 range->is_range = 1;
1868 range->value_min = field->min;
1869 range->value_max = field->max;
1871 switch (field->type) {
1872 case AV_OPT_TYPE_BOOL:
1873 case AV_OPT_TYPE_INT:
1874 case AV_OPT_TYPE_INT64:
1875 case AV_OPT_TYPE_UINT64:
1876 case AV_OPT_TYPE_PIXEL_FMT:
1877 case AV_OPT_TYPE_SAMPLE_FMT:
1878 case AV_OPT_TYPE_FLOAT:
1879 case AV_OPT_TYPE_DOUBLE:
1880 case AV_OPT_TYPE_DURATION:
1881 case AV_OPT_TYPE_COLOR:
1882 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1884 case AV_OPT_TYPE_STRING:
1885 range->component_min = 0;
1886 range->component_max = 0x10FFFF; // max unicode value
1887 range->value_min = -1;
1888 range->value_max = INT_MAX;
1890 case AV_OPT_TYPE_RATIONAL:
1891 range->component_min = INT_MIN;
1892 range->component_max = INT_MAX;
1894 case AV_OPT_TYPE_IMAGE_SIZE:
1895 range->component_min = 0;
1896 range->component_max = INT_MAX/128/8;
1897 range->value_min = 0;
1898 range->value_max = INT_MAX/8;
1900 case AV_OPT_TYPE_VIDEO_RATE:
1901 range->component_min = 1;
1902 range->component_max = INT_MAX;
1903 range->value_min = 1;
1904 range->value_max = INT_MAX;
1907 ret = AVERROR(ENOSYS);
1911 *ranges_arg = ranges;
1916 av_free(range_array);
1920 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1923 AVOptionRanges *ranges = *rangesp;
1928 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1929 AVOptionRange *range = ranges->range[i];
1931 av_freep(&range->str);
1932 av_freep(&ranges->range[i]);
1935 av_freep(&ranges->range);
1939 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1950 return AVERROR(EINVAL);
1952 dst = ((uint8_t*)obj) + o->offset;
1955 case AV_OPT_TYPE_CONST:
1957 case AV_OPT_TYPE_BOOL:
1958 case AV_OPT_TYPE_FLAGS:
1959 case AV_OPT_TYPE_PIXEL_FMT:
1960 case AV_OPT_TYPE_SAMPLE_FMT:
1961 case AV_OPT_TYPE_INT:
1962 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1963 case AV_OPT_TYPE_DURATION:
1964 case AV_OPT_TYPE_INT64:
1965 case AV_OPT_TYPE_UINT64:
1966 read_number(o, dst, NULL, NULL, &i64);
1967 return o->default_val.i64 == i64;
1968 case AV_OPT_TYPE_STRING:
1969 str = *(char **)dst;
1970 if (str == o->default_val.str) //2 NULLs
1972 if (!str || !o->default_val.str) //1 NULL
1974 return !strcmp(str, o->default_val.str);
1975 case AV_OPT_TYPE_DOUBLE:
1976 read_number(o, dst, &d, NULL, NULL);
1977 return o->default_val.dbl == d;
1978 case AV_OPT_TYPE_FLOAT:
1979 read_number(o, dst, &d, NULL, NULL);
1980 f = o->default_val.dbl;
1983 case AV_OPT_TYPE_RATIONAL:
1984 q = av_d2q(o->default_val.dbl, INT_MAX);
1985 return !av_cmp_q(*(AVRational*)dst, q);
1986 case AV_OPT_TYPE_BINARY: {
1991 int opt_size = *(int *)((void **)dst + 1);
1992 void *opt_ptr = *(void **)dst;
1993 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1995 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1997 if (opt_size != strlen(o->default_val.str) / 2)
1999 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
2001 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
2005 case AV_OPT_TYPE_DICT: {
2006 AVDictionary *dict1 = NULL;
2007 AVDictionary *dict2 = *(AVDictionary **)dst;
2008 AVDictionaryEntry *en1 = NULL;
2009 AVDictionaryEntry *en2 = NULL;
2010 ret = av_dict_parse_string(&dict1, o->default_val.str, "=", ":", 0);
2012 av_dict_free(&dict1);
2016 en1 = av_dict_get(dict1, "", en1, AV_DICT_IGNORE_SUFFIX);
2017 en2 = av_dict_get(dict2, "", en2, AV_DICT_IGNORE_SUFFIX);
2018 } while (en1 && en2 && !strcmp(en1->key, en2->key) && !strcmp(en1->value, en2->value));
2019 av_dict_free(&dict1);
2020 return (!en1 && !en2);
2022 case AV_OPT_TYPE_IMAGE_SIZE:
2023 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
2025 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
2027 return (w == *(int *)dst) && (h == *((int *)dst+1));
2028 case AV_OPT_TYPE_VIDEO_RATE:
2029 q = (AVRational){0, 0};
2030 if (o->default_val.str) {
2031 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
2034 return !av_cmp_q(*(AVRational*)dst, q);
2035 case AV_OPT_TYPE_COLOR: {
2036 uint8_t color[4] = {0, 0, 0, 0};
2037 if (o->default_val.str) {
2038 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
2041 return !memcmp(color, dst, sizeof(color));
2044 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
2047 return AVERROR_PATCHWELCOME;
2050 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
2055 return AVERROR(EINVAL);
2056 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
2058 return AVERROR_OPTION_NOT_FOUND;
2059 return av_opt_is_set_to_default(target, o);
2062 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
2063 const char key_val_sep, const char pairs_sep)
2065 const AVOption *o = NULL;
2069 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
2071 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
2072 pairs_sep == '\\' || key_val_sep == '\\') {
2073 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
2074 return AVERROR(EINVAL);
2077 if (!obj || !buffer)
2078 return AVERROR(EINVAL);
2081 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
2083 while (o = av_opt_next(obj, o)) {
2084 if (o->type == AV_OPT_TYPE_CONST)
2086 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2088 else if (((o->flags & opt_flags) != opt_flags))
2090 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
2092 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2093 av_bprint_finalize(&bprint, NULL);
2098 av_bprint_append_data(&bprint, &pairs_sep, 1);
2099 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2100 av_bprint_append_data(&bprint, &key_val_sep, 1);
2101 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2105 av_bprint_finalize(&bprint, buffer);