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)
233 if (o->type == AV_OPT_TYPE_RATIONAL || o->type == AV_OPT_TYPE_VIDEO_RATE) {
236 if (sscanf(val, "%d%*1[:/]%d%c", &num, &den, &c) == 2) {
237 if ((ret = write_number(obj, o, dst, 1, den, num)) >= 0)
250 if (o->type == AV_OPT_TYPE_FLAGS) {
251 if (*val == '+' || *val == '-')
253 for (; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
261 double const_values[64];
262 const char * const_names[64];
263 int search_flags = (o->flags & AV_OPT_FLAG_CHILD_CONSTS) ? AV_OPT_SEARCH_CHILDREN : 0;
264 const AVOption *o_named = av_opt_find(target_obj, i ? buf : val, o->unit, 0, search_flags);
265 if (o_named && o_named->type == AV_OPT_TYPE_CONST)
266 d = DEFAULT_NUMVAL(o_named);
269 for (o_named = NULL; o_named = av_opt_next(target_obj, o_named); ) {
270 if (o_named->type == AV_OPT_TYPE_CONST &&
272 !strcmp(o_named->unit, o->unit)) {
273 if (ci + 6 >= FF_ARRAY_ELEMS(const_values)) {
274 av_log(obj, AV_LOG_ERROR, "const_values array too small for %s\n", o->unit);
275 return AVERROR_PATCHWELCOME;
277 const_names [ci ] = o_named->name;
278 const_values[ci++] = DEFAULT_NUMVAL(o_named);
282 const_names [ci ] = "default";
283 const_values[ci++] = DEFAULT_NUMVAL(o);
284 const_names [ci ] = "max";
285 const_values[ci++] = o->max;
286 const_names [ci ] = "min";
287 const_values[ci++] = o->min;
288 const_names [ci ] = "none";
289 const_values[ci++] = 0;
290 const_names [ci ] = "all";
291 const_values[ci++] = ~0;
292 const_names [ci] = NULL;
293 const_values[ci] = 0;
295 res = av_expr_parse_and_eval(&d, i ? buf : val, const_names,
296 const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
298 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
303 if (o->type == AV_OPT_TYPE_FLAGS) {
304 read_number(o, dst, NULL, NULL, &intnum);
306 d = intnum | (int64_t)d;
308 d = intnum &~(int64_t)d;
311 if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
319 static int set_string_image_size(void *obj, const AVOption *o, const char *val, int *dst)
323 if (!val || !strcmp(val, "none")) {
328 ret = av_parse_video_size(dst, dst + 1, val);
330 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
334 static int set_string_video_rate(void *obj, const AVOption *o, const char *val, AVRational *dst)
336 int ret = av_parse_video_rate(dst, val);
338 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
342 static int set_string_color(void *obj, const AVOption *o, const char *val, uint8_t *dst)
349 ret = av_parse_color(dst, val, -1, obj);
351 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as color\n", val);
357 static const char *get_bool_name(int val)
361 return val ? "true" : "false";
364 static int set_string_bool(void *obj, const AVOption *o, const char *val, int *dst)
371 if (!strcmp(val, "auto")) {
373 } else if (av_match_name(val, "true,y,yes,enable,enabled,on")) {
375 } else if (av_match_name(val, "false,n,no,disable,disabled,off")) {
379 n = strtol(val, &end, 10);
380 if (val + strlen(val) != end)
384 if (n < o->min || n > o->max)
391 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as boolean\n", val);
392 return AVERROR(EINVAL);
395 static int set_string_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst,
396 int fmt_nb, int ((*get_fmt)(const char *)), const char *desc)
400 if (!val || !strcmp(val, "none")) {
406 fmt = strtol(val, &tail, 0);
407 if (*tail || (unsigned)fmt >= fmt_nb) {
408 av_log(obj, AV_LOG_ERROR,
409 "Unable to parse option value \"%s\" as %s\n", val, desc);
410 return AVERROR(EINVAL);
415 min = FFMAX(o->min, -1);
416 max = FFMIN(o->max, fmt_nb-1);
418 // hack for compatibility with old ffmpeg
419 if(min == 0 && max == 0) {
424 if (fmt < min || fmt > max) {
425 av_log(obj, AV_LOG_ERROR,
426 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
427 fmt, o->name, desc, min, max);
428 return AVERROR(ERANGE);
435 static int set_string_pixel_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
437 return set_string_fmt(obj, o, val, dst,
438 AV_PIX_FMT_NB, av_get_pix_fmt, "pixel format");
441 static int set_string_sample_fmt(void *obj, const AVOption *o, const char *val, uint8_t *dst)
443 return set_string_fmt(obj, o, val, dst,
444 AV_SAMPLE_FMT_NB, av_get_sample_fmt, "sample format");
447 static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_t **dst)
449 AVDictionary *options = NULL;
452 int ret = av_dict_parse_string(&options, val, "=", ":", 0);
454 av_dict_free(&options);
459 av_dict_free((AVDictionary **)dst);
460 *dst = (uint8_t *)options;
465 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
468 void *dst, *target_obj;
469 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
470 if (!o || !target_obj)
471 return AVERROR_OPTION_NOT_FOUND;
472 if (!val && (o->type != AV_OPT_TYPE_STRING &&
473 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
474 o->type != AV_OPT_TYPE_IMAGE_SIZE &&
475 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
476 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
477 return AVERROR(EINVAL);
479 if (o->flags & AV_OPT_FLAG_READONLY)
480 return AVERROR(EINVAL);
482 if (o->flags & AV_OPT_FLAG_DEPRECATED)
483 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
485 dst = ((uint8_t *)target_obj) + o->offset;
487 case AV_OPT_TYPE_BOOL:
488 return set_string_bool(obj, o, val, dst);
489 case AV_OPT_TYPE_STRING:
490 return set_string(obj, o, val, dst);
491 case AV_OPT_TYPE_BINARY:
492 return set_string_binary(obj, o, val, dst);
493 case AV_OPT_TYPE_FLAGS:
494 case AV_OPT_TYPE_INT:
495 case AV_OPT_TYPE_INT64:
496 case AV_OPT_TYPE_UINT64:
497 case AV_OPT_TYPE_FLOAT:
498 case AV_OPT_TYPE_DOUBLE:
499 case AV_OPT_TYPE_RATIONAL:
500 return set_string_number(obj, target_obj, o, val, dst);
501 case AV_OPT_TYPE_IMAGE_SIZE:
502 return set_string_image_size(obj, o, val, dst);
503 case AV_OPT_TYPE_VIDEO_RATE: {
505 ret = set_string_video_rate(obj, o, val, &tmp);
508 return write_number(obj, o, dst, 1, tmp.den, tmp.num);
510 case AV_OPT_TYPE_PIXEL_FMT:
511 return set_string_pixel_fmt(obj, o, val, dst);
512 case AV_OPT_TYPE_SAMPLE_FMT:
513 return set_string_sample_fmt(obj, o, val, dst);
514 case AV_OPT_TYPE_DURATION:
518 if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
519 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
523 if (usecs < o->min || usecs > o->max) {
524 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
525 usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
526 return AVERROR(ERANGE);
528 *(int64_t *)dst = usecs;
531 case AV_OPT_TYPE_COLOR:
532 return set_string_color(obj, o, val, dst);
533 case AV_OPT_TYPE_CHANNEL_LAYOUT:
534 if (!val || !strcmp(val, "none")) {
537 int64_t cl = av_get_channel_layout(val);
539 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
540 ret = AVERROR(EINVAL);
542 *(int64_t *)dst = cl;
546 case AV_OPT_TYPE_DICT:
547 return set_string_dict(obj, o, val, dst);
550 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
551 return AVERROR(EINVAL);
554 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
555 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
556 const char *val, vartype *name ## _out) \
558 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
559 return AVERROR(EINVAL); \
560 return set_string_number(obj, obj, o, val, name ## _out); \
563 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
564 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
565 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
566 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
567 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
568 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
570 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
573 void *dst, *target_obj;
574 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
576 if (!o || !target_obj)
577 return AVERROR_OPTION_NOT_FOUND;
579 if (o->flags & AV_OPT_FLAG_READONLY)
580 return AVERROR(EINVAL);
582 dst = ((uint8_t *)target_obj) + o->offset;
583 return write_number(obj, o, dst, num, den, intnum);
586 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
588 return set_number(obj, name, 1, 1, val, search_flags);
591 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
593 return set_number(obj, name, val, 1, 1, search_flags);
596 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
598 return set_number(obj, name, val.num, val.den, 1, search_flags);
601 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
604 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
609 if (!o || !target_obj)
610 return AVERROR_OPTION_NOT_FOUND;
612 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
613 return AVERROR(EINVAL);
615 ptr = len ? av_malloc(len) : NULL;
617 return AVERROR(ENOMEM);
619 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
620 lendst = (int *)(dst + 1);
626 memcpy(ptr, val, len);
631 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
634 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
636 if (!o || !target_obj)
637 return AVERROR_OPTION_NOT_FOUND;
638 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
639 av_log(obj, AV_LOG_ERROR,
640 "The value set by option '%s' is not an image size.\n", o->name);
641 return AVERROR(EINVAL);
644 av_log(obj, AV_LOG_ERROR,
645 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
646 return AVERROR(EINVAL);
648 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
649 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
653 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
656 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
658 if (!o || !target_obj)
659 return AVERROR_OPTION_NOT_FOUND;
660 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
661 av_log(obj, AV_LOG_ERROR,
662 "The value set by option '%s' is not a video rate.\n", o->name);
663 return AVERROR(EINVAL);
665 if (val.num <= 0 || val.den <= 0)
666 return AVERROR(EINVAL);
667 return set_number(obj, name, val.num, val.den, 1, search_flags);
670 static int set_format(void *obj, const char *name, int fmt, int search_flags,
671 enum AVOptionType type, const char *desc, int nb_fmts)
674 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
675 search_flags, &target_obj);
678 if (!o || !target_obj)
679 return AVERROR_OPTION_NOT_FOUND;
680 if (o->type != type) {
681 av_log(obj, AV_LOG_ERROR,
682 "The value set by option '%s' is not a %s format", name, desc);
683 return AVERROR(EINVAL);
686 min = FFMAX(o->min, -1);
687 max = FFMIN(o->max, nb_fmts-1);
689 if (fmt < min || fmt > max) {
690 av_log(obj, AV_LOG_ERROR,
691 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
692 fmt, name, desc, min, max);
693 return AVERROR(ERANGE);
695 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
699 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
701 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
704 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
706 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
709 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
712 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
714 if (!o || !target_obj)
715 return AVERROR_OPTION_NOT_FOUND;
716 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
717 av_log(obj, AV_LOG_ERROR,
718 "The value set by option '%s' is not a channel layout.\n", o->name);
719 return AVERROR(EINVAL);
721 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
725 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
730 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
732 if (!o || !target_obj)
733 return AVERROR_OPTION_NOT_FOUND;
734 if (o->flags & AV_OPT_FLAG_READONLY)
735 return AVERROR(EINVAL);
737 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
739 av_dict_copy(dst, val, 0);
744 static void format_duration(char *buf, size_t size, int64_t d)
748 av_assert0(size >= 25);
749 if (d < 0 && d != INT64_MIN) {
755 snprintf(buf, size, "INT64_MAX");
756 else if (d == INT64_MIN)
757 snprintf(buf, size, "INT64_MIN");
758 else if (d > (int64_t)3600*1000000)
759 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
760 (int)((d / 60000000) % 60),
761 (int)((d / 1000000) % 60),
763 else if (d > 60*1000000)
764 snprintf(buf, size, "%d:%02d.%06d",
766 (int)((d / 1000000) % 60),
769 snprintf(buf, size, "%d.%06d",
772 e = buf + strlen(buf);
773 while (e > buf && e[-1] == '0')
775 if (e > buf && e[-1] == '.')
779 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
781 void *dst, *target_obj;
782 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
783 uint8_t *bin, buf[128];
787 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
788 return AVERROR_OPTION_NOT_FOUND;
790 if (o->flags & AV_OPT_FLAG_DEPRECATED)
791 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
793 dst = (uint8_t *)target_obj + o->offset;
797 case AV_OPT_TYPE_BOOL:
798 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
800 case AV_OPT_TYPE_FLAGS:
801 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
803 case AV_OPT_TYPE_INT:
804 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
806 case AV_OPT_TYPE_INT64:
807 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
809 case AV_OPT_TYPE_UINT64:
810 ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst);
812 case AV_OPT_TYPE_FLOAT:
813 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
815 case AV_OPT_TYPE_DOUBLE:
816 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
818 case AV_OPT_TYPE_VIDEO_RATE:
819 case AV_OPT_TYPE_RATIONAL:
820 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
822 case AV_OPT_TYPE_CONST:
823 ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
825 case AV_OPT_TYPE_STRING:
826 if (*(uint8_t **)dst) {
827 *out_val = av_strdup(*(uint8_t **)dst);
828 } else if (search_flags & AV_OPT_ALLOW_NULL) {
832 *out_val = av_strdup("");
834 return *out_val ? 0 : AVERROR(ENOMEM);
835 case AV_OPT_TYPE_BINARY:
836 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
840 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
841 if ((uint64_t)len * 2 + 1 > INT_MAX)
842 return AVERROR(EINVAL);
843 if (!(*out_val = av_malloc(len * 2 + 1)))
844 return AVERROR(ENOMEM);
849 bin = *(uint8_t **)dst;
850 for (i = 0; i < len; i++)
851 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
853 case AV_OPT_TYPE_IMAGE_SIZE:
854 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
856 case AV_OPT_TYPE_PIXEL_FMT:
857 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
859 case AV_OPT_TYPE_SAMPLE_FMT:
860 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
862 case AV_OPT_TYPE_DURATION:
863 i64 = *(int64_t *)dst;
864 format_duration(buf, sizeof(buf), i64);
865 ret = strlen(buf); // no overflow possible, checked by an assert
867 case AV_OPT_TYPE_COLOR:
868 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
869 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
870 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
872 case AV_OPT_TYPE_CHANNEL_LAYOUT:
873 i64 = *(int64_t *)dst;
874 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
876 case AV_OPT_TYPE_DICT:
877 if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
881 return av_dict_get_string(*(AVDictionary **)dst, (char **)out_val, '=', ':');
883 return AVERROR(EINVAL);
886 if (ret >= sizeof(buf))
887 return AVERROR(EINVAL);
888 *out_val = av_strdup(buf);
889 return *out_val ? 0 : AVERROR(ENOMEM);
892 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
895 void *dst, *target_obj;
896 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
897 if (!o || !target_obj)
900 dst = ((uint8_t *)target_obj) + o->offset;
902 if (o_out) *o_out= o;
904 return read_number(o, dst, num, den, intnum);
912 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
918 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
920 *out_val = num * intnum / den;
924 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
930 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
932 *out_val = num * intnum / den;
936 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
942 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
945 if (num == 1.0 && (int)intnum == intnum)
946 *out_val = (AVRational){intnum, den};
948 *out_val = av_d2q(num*intnum/den, 1<<24);
952 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
954 void *dst, *target_obj;
955 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
956 if (!o || !target_obj)
957 return AVERROR_OPTION_NOT_FOUND;
958 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
959 av_log(obj, AV_LOG_ERROR,
960 "The value for option '%s' is not an image size.\n", name);
961 return AVERROR(EINVAL);
964 dst = ((uint8_t*)target_obj) + o->offset;
965 if (w_out) *w_out = *(int *)dst;
966 if (h_out) *h_out = *((int *)dst+1);
970 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
976 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
979 if (num == 1.0 && (int)intnum == intnum)
980 *out_val = (AVRational) { intnum, den };
982 *out_val = av_d2q(num * intnum / den, 1 << 24);
986 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
987 enum AVOptionType type, const char *desc)
989 void *dst, *target_obj;
990 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
991 if (!o || !target_obj)
992 return AVERROR_OPTION_NOT_FOUND;
993 if (o->type != type) {
994 av_log(obj, AV_LOG_ERROR,
995 "The value for option '%s' is not a %s format.\n", desc, name);
996 return AVERROR(EINVAL);
999 dst = ((uint8_t*)target_obj) + o->offset;
1000 *out_fmt = *(int *)dst;
1004 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
1006 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
1009 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
1011 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
1014 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
1016 void *dst, *target_obj;
1017 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1018 if (!o || !target_obj)
1019 return AVERROR_OPTION_NOT_FOUND;
1020 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
1021 av_log(obj, AV_LOG_ERROR,
1022 "The value for option '%s' is not a channel layout.\n", name);
1023 return AVERROR(EINVAL);
1026 dst = ((uint8_t*)target_obj) + o->offset;
1027 *cl = *(int64_t *)dst;
1031 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1035 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1037 if (!o || !target_obj)
1038 return AVERROR_OPTION_NOT_FOUND;
1039 if (o->type != AV_OPT_TYPE_DICT)
1040 return AVERROR(EINVAL);
1042 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1043 av_dict_copy(out_val, src, 0);
1048 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1050 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1051 const AVOption *flag = av_opt_find(obj, flag_name,
1052 field ? field->unit : NULL, 0, 0);
1055 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1056 av_opt_get_int(obj, field_name, 0, &res) < 0)
1058 return res & flag->default_val.i64;
1061 static void log_int_value(void *av_log_obj, int level, int64_t i)
1064 av_log(av_log_obj, level, "INT_MAX");
1065 } else if (i == INT_MIN) {
1066 av_log(av_log_obj, level, "INT_MIN");
1067 } else if (i == UINT32_MAX) {
1068 av_log(av_log_obj, level, "UINT32_MAX");
1069 } else if (i == INT64_MAX) {
1070 av_log(av_log_obj, level, "I64_MAX");
1071 } else if (i == INT64_MIN) {
1072 av_log(av_log_obj, level, "I64_MIN");
1074 av_log(av_log_obj, level, "%"PRId64, i);
1078 static void log_value(void *av_log_obj, int level, double d)
1081 av_log(av_log_obj, level, "INT_MAX");
1082 } else if (d == INT_MIN) {
1083 av_log(av_log_obj, level, "INT_MIN");
1084 } else if (d == UINT32_MAX) {
1085 av_log(av_log_obj, level, "UINT32_MAX");
1086 } else if (d == (double)INT64_MAX) {
1087 av_log(av_log_obj, level, "I64_MAX");
1088 } else if (d == INT64_MIN) {
1089 av_log(av_log_obj, level, "I64_MIN");
1090 } else if (d == FLT_MAX) {
1091 av_log(av_log_obj, level, "FLT_MAX");
1092 } else if (d == FLT_MIN) {
1093 av_log(av_log_obj, level, "FLT_MIN");
1094 } else if (d == -FLT_MAX) {
1095 av_log(av_log_obj, level, "-FLT_MAX");
1096 } else if (d == -FLT_MIN) {
1097 av_log(av_log_obj, level, "-FLT_MIN");
1098 } else if (d == DBL_MAX) {
1099 av_log(av_log_obj, level, "DBL_MAX");
1100 } else if (d == DBL_MIN) {
1101 av_log(av_log_obj, level, "DBL_MIN");
1102 } else if (d == -DBL_MAX) {
1103 av_log(av_log_obj, level, "-DBL_MAX");
1104 } else if (d == -DBL_MIN) {
1105 av_log(av_log_obj, level, "-DBL_MIN");
1107 av_log(av_log_obj, level, "%g", d);
1111 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1113 const AVOption *opt = NULL;
1117 while ((opt = av_opt_next(obj, opt)))
1118 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1119 opt->default_val.i64 == value)
1124 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1126 const AVOption *opt = NULL;
1132 while ((opt = av_opt_next(obj, opt))) {
1133 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1134 opt->default_val.i64 & value) {
1136 av_strlcatf(flags, sizeof(flags), "+");
1137 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1141 return av_strdup(flags);
1145 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1146 int req_flags, int rej_flags, enum AVOptionType parent_type)
1148 const AVOption *opt = NULL;
1152 while ((opt = av_opt_next(obj, opt))) {
1153 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1156 /* Don't print CONST's on level one.
1157 * Don't print anything but CONST's on level two.
1158 * Only print items from the requested unit.
1160 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1162 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1164 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1166 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1167 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1169 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1170 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1173 switch (opt->type) {
1174 case AV_OPT_TYPE_FLAGS:
1175 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1177 case AV_OPT_TYPE_INT:
1178 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1180 case AV_OPT_TYPE_INT64:
1181 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1183 case AV_OPT_TYPE_UINT64:
1184 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
1186 case AV_OPT_TYPE_DOUBLE:
1187 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1189 case AV_OPT_TYPE_FLOAT:
1190 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1192 case AV_OPT_TYPE_STRING:
1193 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1195 case AV_OPT_TYPE_RATIONAL:
1196 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1198 case AV_OPT_TYPE_BINARY:
1199 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1201 case AV_OPT_TYPE_DICT:
1202 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<dictionary>");
1204 case AV_OPT_TYPE_IMAGE_SIZE:
1205 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1207 case AV_OPT_TYPE_VIDEO_RATE:
1208 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1210 case AV_OPT_TYPE_PIXEL_FMT:
1211 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1213 case AV_OPT_TYPE_SAMPLE_FMT:
1214 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1216 case AV_OPT_TYPE_DURATION:
1217 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1219 case AV_OPT_TYPE_COLOR:
1220 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1222 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1223 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1225 case AV_OPT_TYPE_BOOL:
1226 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1228 case AV_OPT_TYPE_CONST:
1229 if (parent_type == AV_OPT_TYPE_INT)
1230 av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
1232 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1235 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1238 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1239 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1240 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1241 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1242 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1243 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1244 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1245 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1246 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.');
1247 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_RUNTIME_PARAM) ? 'T' : '.');
1248 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DEPRECATED) ? 'P' : '.');
1251 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1253 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1254 switch (opt->type) {
1255 case AV_OPT_TYPE_INT:
1256 case AV_OPT_TYPE_INT64:
1257 case AV_OPT_TYPE_UINT64:
1258 case AV_OPT_TYPE_DOUBLE:
1259 case AV_OPT_TYPE_FLOAT:
1260 case AV_OPT_TYPE_RATIONAL:
1261 for (i = 0; i < r->nb_ranges; i++) {
1262 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1263 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1264 av_log(av_log_obj, AV_LOG_INFO, " to ");
1265 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1266 av_log(av_log_obj, AV_LOG_INFO, ")");
1270 av_opt_freep_ranges(&r);
1273 if (opt->type != AV_OPT_TYPE_CONST &&
1274 opt->type != AV_OPT_TYPE_BINARY &&
1275 !((opt->type == AV_OPT_TYPE_COLOR ||
1276 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1277 opt->type == AV_OPT_TYPE_STRING ||
1278 opt->type == AV_OPT_TYPE_DICT ||
1279 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1280 !opt->default_val.str)) {
1281 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1282 switch (opt->type) {
1283 case AV_OPT_TYPE_BOOL:
1284 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1286 case AV_OPT_TYPE_FLAGS: {
1287 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1289 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1290 av_freep(&def_flags);
1292 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1296 case AV_OPT_TYPE_DURATION: {
1298 format_duration(buf, sizeof(buf), opt->default_val.i64);
1299 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1302 case AV_OPT_TYPE_INT:
1303 case AV_OPT_TYPE_UINT64:
1304 case AV_OPT_TYPE_INT64: {
1305 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1307 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1309 log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1312 case AV_OPT_TYPE_DOUBLE:
1313 case AV_OPT_TYPE_FLOAT:
1314 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1316 case AV_OPT_TYPE_RATIONAL: {
1317 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1318 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1320 case AV_OPT_TYPE_PIXEL_FMT:
1321 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1323 case AV_OPT_TYPE_SAMPLE_FMT:
1324 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1326 case AV_OPT_TYPE_COLOR:
1327 case AV_OPT_TYPE_IMAGE_SIZE:
1328 case AV_OPT_TYPE_STRING:
1329 case AV_OPT_TYPE_DICT:
1330 case AV_OPT_TYPE_VIDEO_RATE:
1331 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1333 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1334 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1337 av_log(av_log_obj, AV_LOG_INFO, ")");
1340 av_log(av_log_obj, AV_LOG_INFO, "\n");
1341 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1342 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
1346 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1351 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1353 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
1358 void av_opt_set_defaults(void *s)
1360 av_opt_set_defaults2(s, 0, 0);
1363 void av_opt_set_defaults2(void *s, int mask, int flags)
1365 const AVOption *opt = NULL;
1366 while ((opt = av_opt_next(s, opt))) {
1367 void *dst = ((uint8_t*)s) + opt->offset;
1369 if ((opt->flags & mask) != flags)
1372 if (opt->flags & AV_OPT_FLAG_READONLY)
1375 switch (opt->type) {
1376 case AV_OPT_TYPE_CONST:
1377 /* Nothing to be done here */
1379 case AV_OPT_TYPE_BOOL:
1380 case AV_OPT_TYPE_FLAGS:
1381 case AV_OPT_TYPE_INT:
1382 case AV_OPT_TYPE_INT64:
1383 case AV_OPT_TYPE_UINT64:
1384 case AV_OPT_TYPE_DURATION:
1385 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1386 case AV_OPT_TYPE_PIXEL_FMT:
1387 case AV_OPT_TYPE_SAMPLE_FMT:
1388 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1390 case AV_OPT_TYPE_DOUBLE:
1391 case AV_OPT_TYPE_FLOAT: {
1393 val = opt->default_val.dbl;
1394 write_number(s, opt, dst, val, 1, 1);
1397 case AV_OPT_TYPE_RATIONAL: {
1399 val = av_d2q(opt->default_val.dbl, INT_MAX);
1400 write_number(s, opt, dst, 1, val.den, val.num);
1403 case AV_OPT_TYPE_COLOR:
1404 set_string_color(s, opt, opt->default_val.str, dst);
1406 case AV_OPT_TYPE_STRING:
1407 set_string(s, opt, opt->default_val.str, dst);
1409 case AV_OPT_TYPE_IMAGE_SIZE:
1410 set_string_image_size(s, opt, opt->default_val.str, dst);
1412 case AV_OPT_TYPE_VIDEO_RATE:
1413 set_string_video_rate(s, opt, opt->default_val.str, dst);
1415 case AV_OPT_TYPE_BINARY:
1416 set_string_binary(s, opt, opt->default_val.str, dst);
1418 case AV_OPT_TYPE_DICT:
1419 set_string_dict(s, opt, opt->default_val.str, dst);
1422 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1423 opt->type, opt->name);
1429 * Store the value in the field in ctx that is named like key.
1430 * ctx must be an AVClass context, storing is done using AVOptions.
1432 * @param buf the string to parse, buf will be updated to point at the
1433 * separator just after the parsed key/value pair
1434 * @param key_val_sep a 0-terminated list of characters used to
1435 * separate key from value
1436 * @param pairs_sep a 0-terminated list of characters used to separate
1437 * two pairs from each other
1438 * @return 0 if the key/value pair has been successfully parsed and
1439 * set, or a negative value corresponding to an AVERROR code in case
1441 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1442 * the error code issued by av_opt_set() if the key/value pair
1445 static int parse_key_value_pair(void *ctx, const char **buf,
1446 const char *key_val_sep, const char *pairs_sep)
1448 char *key = av_get_token(buf, key_val_sep);
1453 return AVERROR(ENOMEM);
1455 if (*key && strspn(*buf, key_val_sep)) {
1457 val = av_get_token(buf, pairs_sep);
1460 return AVERROR(ENOMEM);
1463 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1465 return AVERROR(EINVAL);
1468 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1470 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1471 if (ret == AVERROR_OPTION_NOT_FOUND)
1472 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1479 int av_set_options_string(void *ctx, const char *opts,
1480 const char *key_val_sep, const char *pairs_sep)
1488 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1499 #define WHITESPACES " \n\t\r"
1501 static int is_key_char(char c)
1503 return (unsigned)((c | 32) - 'a') < 26 ||
1504 (unsigned)(c - '0') < 10 ||
1505 c == '-' || c == '_' || c == '/' || c == '.';
1509 * Read a key from a string.
1511 * The key consists of is_key_char characters and must be terminated by a
1512 * character from the delim string; spaces are ignored.
1514 * @return 0 for success (even with ellipsis), <0 for failure
1516 static int get_key(const char **ropts, const char *delim, char **rkey)
1518 const char *opts = *ropts;
1519 const char *key_start, *key_end;
1521 key_start = opts += strspn(opts, WHITESPACES);
1522 while (is_key_char(*opts))
1525 opts += strspn(opts, WHITESPACES);
1526 if (!*opts || !strchr(delim, *opts))
1527 return AVERROR(EINVAL);
1529 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1530 return AVERROR(ENOMEM);
1531 memcpy(*rkey, key_start, key_end - key_start);
1532 (*rkey)[key_end - key_start] = 0;
1537 int av_opt_get_key_value(const char **ropts,
1538 const char *key_val_sep, const char *pairs_sep,
1540 char **rkey, char **rval)
1543 char *key = NULL, *val;
1544 const char *opts = *ropts;
1546 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1547 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1548 return AVERROR(EINVAL);
1549 if (!(val = av_get_token(&opts, pairs_sep))) {
1551 return AVERROR(ENOMEM);
1559 int av_opt_set_from_string(void *ctx, const char *opts,
1560 const char *const *shorthand,
1561 const char *key_val_sep, const char *pairs_sep)
1564 const char *dummy_shorthand = NULL;
1565 char *av_uninit(parsed_key), *av_uninit(value);
1571 shorthand = &dummy_shorthand;
1574 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1575 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1576 &parsed_key, &value);
1578 if (ret == AVERROR(EINVAL))
1579 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1581 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1589 while (*shorthand) /* discard all remaining shorthand */
1592 key = *(shorthand++);
1595 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1596 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1597 if (ret == AVERROR_OPTION_NOT_FOUND)
1598 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1600 av_free(parsed_key);
1605 av_free(parsed_key);
1611 void av_opt_free(void *obj)
1613 const AVOption *o = NULL;
1614 while ((o = av_opt_next(obj, o))) {
1616 case AV_OPT_TYPE_STRING:
1617 case AV_OPT_TYPE_BINARY:
1618 av_freep((uint8_t *)obj + o->offset);
1621 case AV_OPT_TYPE_DICT:
1622 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1631 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1633 AVDictionaryEntry *t = NULL;
1634 AVDictionary *tmp = NULL;
1640 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1641 ret = av_opt_set(obj, t->key, t->value, search_flags);
1642 if (ret == AVERROR_OPTION_NOT_FOUND)
1643 ret = av_dict_set(&tmp, t->key, t->value, 0);
1645 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1651 av_dict_free(options);
1656 int av_opt_set_dict(void *obj, AVDictionary **options)
1658 return av_opt_set_dict2(obj, options, 0);
1661 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1662 int opt_flags, int search_flags)
1664 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1667 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1668 int opt_flags, int search_flags, void **target_obj)
1671 const AVOption *o = NULL;
1681 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1682 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1684 const AVClass *child;
1685 while (child = av_opt_child_class_iterate(c, &iter))
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 #if FF_API_CHILD_CLASS_NEXT
1721 FF_DISABLE_DEPRECATION_WARNINGS
1722 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1724 if (parent->child_class_next)
1725 return parent->child_class_next(prev);
1728 FF_ENABLE_DEPRECATION_WARNINGS
1731 const AVClass *av_opt_child_class_iterate(const AVClass *parent, void **iter)
1733 if (parent->child_class_iterate)
1734 return parent->child_class_iterate(iter);
1735 #if FF_API_CHILD_CLASS_NEXT
1736 FF_DISABLE_DEPRECATION_WARNINGS
1737 if (parent->child_class_next) {
1738 *iter = parent->child_class_next(*iter);
1741 FF_ENABLE_DEPRECATION_WARNINGS
1746 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1748 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1751 return (uint8_t*)obj + opt->offset;
1754 static int opt_size(enum AVOptionType type)
1757 case AV_OPT_TYPE_BOOL:
1758 case AV_OPT_TYPE_INT:
1759 case AV_OPT_TYPE_FLAGS:
1761 case AV_OPT_TYPE_DURATION:
1762 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1763 case AV_OPT_TYPE_INT64:
1764 case AV_OPT_TYPE_UINT64:
1765 return sizeof(int64_t);
1766 case AV_OPT_TYPE_DOUBLE:
1767 return sizeof(double);
1768 case AV_OPT_TYPE_FLOAT:
1769 return sizeof(float);
1770 case AV_OPT_TYPE_STRING:
1771 return sizeof(uint8_t*);
1772 case AV_OPT_TYPE_VIDEO_RATE:
1773 case AV_OPT_TYPE_RATIONAL:
1774 return sizeof(AVRational);
1775 case AV_OPT_TYPE_BINARY:
1776 return sizeof(uint8_t*) + sizeof(int);
1777 case AV_OPT_TYPE_IMAGE_SIZE:
1778 return sizeof(int[2]);
1779 case AV_OPT_TYPE_PIXEL_FMT:
1780 return sizeof(enum AVPixelFormat);
1781 case AV_OPT_TYPE_SAMPLE_FMT:
1782 return sizeof(enum AVSampleFormat);
1783 case AV_OPT_TYPE_COLOR:
1786 return AVERROR(EINVAL);
1789 int av_opt_copy(void *dst, const void *src)
1791 const AVOption *o = NULL;
1796 return AVERROR(EINVAL);
1798 c = *(AVClass **)src;
1799 if (!c || c != *(AVClass **)dst)
1800 return AVERROR(EINVAL);
1802 while ((o = av_opt_next(src, o))) {
1803 void *field_dst = (uint8_t *)dst + o->offset;
1804 void *field_src = (uint8_t *)src + o->offset;
1805 uint8_t **field_dst8 = (uint8_t **)field_dst;
1806 uint8_t **field_src8 = (uint8_t **)field_src;
1808 if (o->type == AV_OPT_TYPE_STRING) {
1809 if (*field_dst8 != *field_src8)
1810 av_freep(field_dst8);
1811 *field_dst8 = av_strdup(*field_src8);
1812 if (*field_src8 && !*field_dst8)
1813 ret = AVERROR(ENOMEM);
1814 } else if (o->type == AV_OPT_TYPE_BINARY) {
1815 int len = *(int *)(field_src8 + 1);
1816 if (*field_dst8 != *field_src8)
1817 av_freep(field_dst8);
1818 *field_dst8 = av_memdup(*field_src8, len);
1819 if (len && !*field_dst8) {
1820 ret = AVERROR(ENOMEM);
1823 *(int *)(field_dst8 + 1) = len;
1824 } else if (o->type == AV_OPT_TYPE_CONST) {
1826 } else if (o->type == AV_OPT_TYPE_DICT) {
1827 AVDictionary **sdict = (AVDictionary **) field_src;
1828 AVDictionary **ddict = (AVDictionary **) field_dst;
1829 if (*sdict != *ddict)
1830 av_dict_free(ddict);
1832 av_dict_copy(ddict, *sdict, 0);
1833 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1834 ret = AVERROR(ENOMEM);
1836 int size = opt_size(o->type);
1840 memcpy(field_dst, field_src, size);
1846 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1849 const AVClass *c = *(AVClass**)obj;
1850 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1852 if (c->version > (52 << 16 | 11 << 8))
1853 callback = c->query_ranges;
1856 callback = av_opt_query_ranges_default;
1858 ret = callback(ranges_arg, obj, key, flags);
1860 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1862 (*ranges_arg)->nb_components = ret;
1867 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1869 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1870 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1871 AVOptionRange *range = av_mallocz(sizeof(*range));
1872 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1877 if (!ranges || !range || !range_array || !field) {
1878 ret = AVERROR(ENOMEM);
1882 ranges->range = range_array;
1883 ranges->range[0] = range;
1884 ranges->nb_ranges = 1;
1885 ranges->nb_components = 1;
1886 range->is_range = 1;
1887 range->value_min = field->min;
1888 range->value_max = field->max;
1890 switch (field->type) {
1891 case AV_OPT_TYPE_BOOL:
1892 case AV_OPT_TYPE_INT:
1893 case AV_OPT_TYPE_INT64:
1894 case AV_OPT_TYPE_UINT64:
1895 case AV_OPT_TYPE_PIXEL_FMT:
1896 case AV_OPT_TYPE_SAMPLE_FMT:
1897 case AV_OPT_TYPE_FLOAT:
1898 case AV_OPT_TYPE_DOUBLE:
1899 case AV_OPT_TYPE_DURATION:
1900 case AV_OPT_TYPE_COLOR:
1901 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1903 case AV_OPT_TYPE_STRING:
1904 range->component_min = 0;
1905 range->component_max = 0x10FFFF; // max unicode value
1906 range->value_min = -1;
1907 range->value_max = INT_MAX;
1909 case AV_OPT_TYPE_RATIONAL:
1910 range->component_min = INT_MIN;
1911 range->component_max = INT_MAX;
1913 case AV_OPT_TYPE_IMAGE_SIZE:
1914 range->component_min = 0;
1915 range->component_max = INT_MAX/128/8;
1916 range->value_min = 0;
1917 range->value_max = INT_MAX/8;
1919 case AV_OPT_TYPE_VIDEO_RATE:
1920 range->component_min = 1;
1921 range->component_max = INT_MAX;
1922 range->value_min = 1;
1923 range->value_max = INT_MAX;
1926 ret = AVERROR(ENOSYS);
1930 *ranges_arg = ranges;
1935 av_free(range_array);
1939 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1942 AVOptionRanges *ranges = *rangesp;
1947 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1948 AVOptionRange *range = ranges->range[i];
1950 av_freep(&range->str);
1951 av_freep(&ranges->range[i]);
1954 av_freep(&ranges->range);
1958 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1969 return AVERROR(EINVAL);
1971 dst = ((uint8_t*)obj) + o->offset;
1974 case AV_OPT_TYPE_CONST:
1976 case AV_OPT_TYPE_BOOL:
1977 case AV_OPT_TYPE_FLAGS:
1978 case AV_OPT_TYPE_PIXEL_FMT:
1979 case AV_OPT_TYPE_SAMPLE_FMT:
1980 case AV_OPT_TYPE_INT:
1981 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1982 case AV_OPT_TYPE_DURATION:
1983 case AV_OPT_TYPE_INT64:
1984 case AV_OPT_TYPE_UINT64:
1985 read_number(o, dst, NULL, NULL, &i64);
1986 return o->default_val.i64 == i64;
1987 case AV_OPT_TYPE_STRING:
1988 str = *(char **)dst;
1989 if (str == o->default_val.str) //2 NULLs
1991 if (!str || !o->default_val.str) //1 NULL
1993 return !strcmp(str, o->default_val.str);
1994 case AV_OPT_TYPE_DOUBLE:
1995 read_number(o, dst, &d, NULL, NULL);
1996 return o->default_val.dbl == d;
1997 case AV_OPT_TYPE_FLOAT:
1998 read_number(o, dst, &d, NULL, NULL);
1999 f = o->default_val.dbl;
2002 case AV_OPT_TYPE_RATIONAL:
2003 q = av_d2q(o->default_val.dbl, INT_MAX);
2004 return !av_cmp_q(*(AVRational*)dst, q);
2005 case AV_OPT_TYPE_BINARY: {
2010 int opt_size = *(int *)((void **)dst + 1);
2011 void *opt_ptr = *(void **)dst;
2012 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
2014 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
2016 if (opt_size != strlen(o->default_val.str) / 2)
2018 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
2020 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
2024 case AV_OPT_TYPE_DICT: {
2025 AVDictionary *dict1 = NULL;
2026 AVDictionary *dict2 = *(AVDictionary **)dst;
2027 AVDictionaryEntry *en1 = NULL;
2028 AVDictionaryEntry *en2 = NULL;
2029 ret = av_dict_parse_string(&dict1, o->default_val.str, "=", ":", 0);
2031 av_dict_free(&dict1);
2035 en1 = av_dict_get(dict1, "", en1, AV_DICT_IGNORE_SUFFIX);
2036 en2 = av_dict_get(dict2, "", en2, AV_DICT_IGNORE_SUFFIX);
2037 } while (en1 && en2 && !strcmp(en1->key, en2->key) && !strcmp(en1->value, en2->value));
2038 av_dict_free(&dict1);
2039 return (!en1 && !en2);
2041 case AV_OPT_TYPE_IMAGE_SIZE:
2042 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
2044 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
2046 return (w == *(int *)dst) && (h == *((int *)dst+1));
2047 case AV_OPT_TYPE_VIDEO_RATE:
2048 q = (AVRational){0, 0};
2049 if (o->default_val.str) {
2050 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
2053 return !av_cmp_q(*(AVRational*)dst, q);
2054 case AV_OPT_TYPE_COLOR: {
2055 uint8_t color[4] = {0, 0, 0, 0};
2056 if (o->default_val.str) {
2057 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
2060 return !memcmp(color, dst, sizeof(color));
2063 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
2066 return AVERROR_PATCHWELCOME;
2069 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
2074 return AVERROR(EINVAL);
2075 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
2077 return AVERROR_OPTION_NOT_FOUND;
2078 return av_opt_is_set_to_default(target, o);
2081 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
2082 const char key_val_sep, const char pairs_sep)
2084 const AVOption *o = NULL;
2088 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
2090 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
2091 pairs_sep == '\\' || key_val_sep == '\\') {
2092 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
2093 return AVERROR(EINVAL);
2096 if (!obj || !buffer)
2097 return AVERROR(EINVAL);
2100 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
2102 while (o = av_opt_next(obj, o)) {
2103 if (o->type == AV_OPT_TYPE_CONST)
2105 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2107 else if (((o->flags & opt_flags) != opt_flags))
2109 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
2111 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2112 av_bprint_finalize(&bprint, NULL);
2117 av_bprint_append_data(&bprint, &pairs_sep, 1);
2118 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2119 av_bprint_append_data(&bprint, &key_val_sep, 1);
2120 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2124 ret = av_bprint_finalize(&bprint, buffer);