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 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
452 void *dst, *target_obj;
453 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
454 if (!o || !target_obj)
455 return AVERROR_OPTION_NOT_FOUND;
456 if (!val && (o->type != AV_OPT_TYPE_STRING &&
457 o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
458 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
459 o->type != AV_OPT_TYPE_DURATION && o->type != AV_OPT_TYPE_COLOR &&
460 o->type != AV_OPT_TYPE_CHANNEL_LAYOUT && o->type != AV_OPT_TYPE_BOOL))
461 return AVERROR(EINVAL);
463 if (o->flags & AV_OPT_FLAG_READONLY)
464 return AVERROR(EINVAL);
466 if (o->flags & AV_OPT_FLAG_DEPRECATED)
467 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
469 dst = ((uint8_t *)target_obj) + o->offset;
471 case AV_OPT_TYPE_BOOL:
472 return set_string_bool(obj, o, val, dst);
473 case AV_OPT_TYPE_STRING:
474 return set_string(obj, o, val, dst);
475 case AV_OPT_TYPE_BINARY:
476 return set_string_binary(obj, o, val, dst);
477 case AV_OPT_TYPE_FLAGS:
478 case AV_OPT_TYPE_INT:
479 case AV_OPT_TYPE_INT64:
480 case AV_OPT_TYPE_UINT64:
481 case AV_OPT_TYPE_FLOAT:
482 case AV_OPT_TYPE_DOUBLE:
483 case AV_OPT_TYPE_RATIONAL:
484 return set_string_number(obj, target_obj, o, val, dst);
485 case AV_OPT_TYPE_IMAGE_SIZE:
486 return set_string_image_size(obj, o, val, dst);
487 case AV_OPT_TYPE_VIDEO_RATE: {
489 ret = set_string_video_rate(obj, o, val, &tmp);
492 return write_number(obj, o, dst, 1, tmp.den, tmp.num);
494 case AV_OPT_TYPE_PIXEL_FMT:
495 return set_string_pixel_fmt(obj, o, val, dst);
496 case AV_OPT_TYPE_SAMPLE_FMT:
497 return set_string_sample_fmt(obj, o, val, dst);
498 case AV_OPT_TYPE_DURATION:
502 if ((ret = av_parse_time(&usecs, val, 1)) < 0) {
503 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
507 if (usecs < o->min || usecs > o->max) {
508 av_log(obj, AV_LOG_ERROR, "Value %f for parameter '%s' out of range [%g - %g]\n",
509 usecs / 1000000.0, o->name, o->min / 1000000.0, o->max / 1000000.0);
510 return AVERROR(ERANGE);
512 *(int64_t *)dst = usecs;
515 case AV_OPT_TYPE_COLOR:
516 return set_string_color(obj, o, val, dst);
517 case AV_OPT_TYPE_CHANNEL_LAYOUT:
518 if (!val || !strcmp(val, "none")) {
521 int64_t cl = av_get_channel_layout(val);
523 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
524 ret = AVERROR(EINVAL);
526 *(int64_t *)dst = cl;
532 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
533 return AVERROR(EINVAL);
536 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
537 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
538 const char *val, vartype *name ## _out) \
540 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
541 return AVERROR(EINVAL); \
542 return set_string_number(obj, obj, o, val, name ## _out); \
545 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
546 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
547 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
548 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
549 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
550 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
552 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
555 void *dst, *target_obj;
556 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
558 if (!o || !target_obj)
559 return AVERROR_OPTION_NOT_FOUND;
561 if (o->flags & AV_OPT_FLAG_READONLY)
562 return AVERROR(EINVAL);
564 dst = ((uint8_t *)target_obj) + o->offset;
565 return write_number(obj, o, dst, num, den, intnum);
568 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
570 return set_number(obj, name, 1, 1, val, search_flags);
573 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
575 return set_number(obj, name, val, 1, 1, search_flags);
578 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
580 return set_number(obj, name, val.num, val.den, 1, search_flags);
583 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
586 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
591 if (!o || !target_obj)
592 return AVERROR_OPTION_NOT_FOUND;
594 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
595 return AVERROR(EINVAL);
597 ptr = len ? av_malloc(len) : NULL;
599 return AVERROR(ENOMEM);
601 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
602 lendst = (int *)(dst + 1);
608 memcpy(ptr, val, len);
613 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
616 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
618 if (!o || !target_obj)
619 return AVERROR_OPTION_NOT_FOUND;
620 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
621 av_log(obj, AV_LOG_ERROR,
622 "The value set by option '%s' is not an image size.\n", o->name);
623 return AVERROR(EINVAL);
626 av_log(obj, AV_LOG_ERROR,
627 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
628 return AVERROR(EINVAL);
630 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
631 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
635 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
638 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
640 if (!o || !target_obj)
641 return AVERROR_OPTION_NOT_FOUND;
642 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
643 av_log(obj, AV_LOG_ERROR,
644 "The value set by option '%s' is not a video rate.\n", o->name);
645 return AVERROR(EINVAL);
647 if (val.num <= 0 || val.den <= 0)
648 return AVERROR(EINVAL);
649 return set_number(obj, name, val.num, val.den, 1, search_flags);
652 static int set_format(void *obj, const char *name, int fmt, int search_flags,
653 enum AVOptionType type, const char *desc, int nb_fmts)
656 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
657 search_flags, &target_obj);
660 if (!o || !target_obj)
661 return AVERROR_OPTION_NOT_FOUND;
662 if (o->type != type) {
663 av_log(obj, AV_LOG_ERROR,
664 "The value set by option '%s' is not a %s format", name, desc);
665 return AVERROR(EINVAL);
668 min = FFMAX(o->min, -1);
669 max = FFMIN(o->max, nb_fmts-1);
671 if (fmt < min || fmt > max) {
672 av_log(obj, AV_LOG_ERROR,
673 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
674 fmt, name, desc, min, max);
675 return AVERROR(ERANGE);
677 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
681 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
683 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
686 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
688 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
691 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
694 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
696 if (!o || !target_obj)
697 return AVERROR_OPTION_NOT_FOUND;
698 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
699 av_log(obj, AV_LOG_ERROR,
700 "The value set by option '%s' is not a channel layout.\n", o->name);
701 return AVERROR(EINVAL);
703 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
707 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
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->flags & AV_OPT_FLAG_READONLY)
717 return AVERROR(EINVAL);
719 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
721 av_dict_copy(dst, val, 0);
726 static void format_duration(char *buf, size_t size, int64_t d)
730 av_assert0(size >= 25);
731 if (d < 0 && d != INT64_MIN) {
737 snprintf(buf, size, "INT64_MAX");
738 else if (d == INT64_MIN)
739 snprintf(buf, size, "INT64_MIN");
740 else if (d > (int64_t)3600*1000000)
741 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
742 (int)((d / 60000000) % 60),
743 (int)((d / 1000000) % 60),
745 else if (d > 60*1000000)
746 snprintf(buf, size, "%d:%02d.%06d",
748 (int)((d / 1000000) % 60),
751 snprintf(buf, size, "%d.%06d",
754 e = buf + strlen(buf);
755 while (e > buf && e[-1] == '0')
757 if (e > buf && e[-1] == '.')
761 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
763 void *dst, *target_obj;
764 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
765 uint8_t *bin, buf[128];
769 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
770 return AVERROR_OPTION_NOT_FOUND;
772 if (o->flags & AV_OPT_FLAG_DEPRECATED)
773 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
775 dst = (uint8_t *)target_obj + o->offset;
779 case AV_OPT_TYPE_BOOL:
780 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
782 case AV_OPT_TYPE_FLAGS:
783 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
785 case AV_OPT_TYPE_INT:
786 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
788 case AV_OPT_TYPE_INT64:
789 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
791 case AV_OPT_TYPE_UINT64:
792 ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst);
794 case AV_OPT_TYPE_FLOAT:
795 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
797 case AV_OPT_TYPE_DOUBLE:
798 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
800 case AV_OPT_TYPE_VIDEO_RATE:
801 case AV_OPT_TYPE_RATIONAL:
802 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
804 case AV_OPT_TYPE_CONST:
805 ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
807 case AV_OPT_TYPE_STRING:
808 if (*(uint8_t **)dst) {
809 *out_val = av_strdup(*(uint8_t **)dst);
810 } else if (search_flags & AV_OPT_ALLOW_NULL) {
814 *out_val = av_strdup("");
816 return *out_val ? 0 : AVERROR(ENOMEM);
817 case AV_OPT_TYPE_BINARY:
818 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
822 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
823 if ((uint64_t)len * 2 + 1 > INT_MAX)
824 return AVERROR(EINVAL);
825 if (!(*out_val = av_malloc(len * 2 + 1)))
826 return AVERROR(ENOMEM);
831 bin = *(uint8_t **)dst;
832 for (i = 0; i < len; i++)
833 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
835 case AV_OPT_TYPE_IMAGE_SIZE:
836 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
838 case AV_OPT_TYPE_PIXEL_FMT:
839 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
841 case AV_OPT_TYPE_SAMPLE_FMT:
842 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
844 case AV_OPT_TYPE_DURATION:
845 i64 = *(int64_t *)dst;
846 format_duration(buf, sizeof(buf), i64);
847 ret = strlen(buf); // no overflow possible, checked by an assert
849 case AV_OPT_TYPE_COLOR:
850 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
851 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
852 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
854 case AV_OPT_TYPE_CHANNEL_LAYOUT:
855 i64 = *(int64_t *)dst;
856 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
859 return AVERROR(EINVAL);
862 if (ret >= sizeof(buf))
863 return AVERROR(EINVAL);
864 *out_val = av_strdup(buf);
865 return *out_val ? 0 : AVERROR(ENOMEM);
868 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
871 void *dst, *target_obj;
872 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
873 if (!o || !target_obj)
876 dst = ((uint8_t *)target_obj) + o->offset;
878 if (o_out) *o_out= o;
880 return read_number(o, dst, num, den, intnum);
888 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
894 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
896 *out_val = num * intnum / den;
900 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
906 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
908 *out_val = num * intnum / den;
912 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
918 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
921 if (num == 1.0 && (int)intnum == intnum)
922 *out_val = (AVRational){intnum, den};
924 *out_val = av_d2q(num*intnum/den, 1<<24);
928 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
930 void *dst, *target_obj;
931 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
932 if (!o || !target_obj)
933 return AVERROR_OPTION_NOT_FOUND;
934 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
935 av_log(obj, AV_LOG_ERROR,
936 "The value for option '%s' is not an image size.\n", name);
937 return AVERROR(EINVAL);
940 dst = ((uint8_t*)target_obj) + o->offset;
941 if (w_out) *w_out = *(int *)dst;
942 if (h_out) *h_out = *((int *)dst+1);
946 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
952 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
955 if (num == 1.0 && (int)intnum == intnum)
956 *out_val = (AVRational) { intnum, den };
958 *out_val = av_d2q(num * intnum / den, 1 << 24);
962 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
963 enum AVOptionType type, const char *desc)
965 void *dst, *target_obj;
966 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
967 if (!o || !target_obj)
968 return AVERROR_OPTION_NOT_FOUND;
969 if (o->type != type) {
970 av_log(obj, AV_LOG_ERROR,
971 "The value for option '%s' is not a %s format.\n", desc, name);
972 return AVERROR(EINVAL);
975 dst = ((uint8_t*)target_obj) + o->offset;
976 *out_fmt = *(int *)dst;
980 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
982 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
985 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
987 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
990 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
992 void *dst, *target_obj;
993 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
994 if (!o || !target_obj)
995 return AVERROR_OPTION_NOT_FOUND;
996 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
997 av_log(obj, AV_LOG_ERROR,
998 "The value for option '%s' is not a channel layout.\n", name);
999 return AVERROR(EINVAL);
1002 dst = ((uint8_t*)target_obj) + o->offset;
1003 *cl = *(int64_t *)dst;
1007 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1011 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1013 if (!o || !target_obj)
1014 return AVERROR_OPTION_NOT_FOUND;
1015 if (o->type != AV_OPT_TYPE_DICT)
1016 return AVERROR(EINVAL);
1018 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1019 av_dict_copy(out_val, src, 0);
1024 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1026 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1027 const AVOption *flag = av_opt_find(obj, flag_name,
1028 field ? field->unit : NULL, 0, 0);
1031 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1032 av_opt_get_int(obj, field_name, 0, &res) < 0)
1034 return res & flag->default_val.i64;
1037 static void log_int_value(void *av_log_obj, int level, int64_t i)
1040 av_log(av_log_obj, level, "INT_MAX");
1041 } else if (i == INT_MIN) {
1042 av_log(av_log_obj, level, "INT_MIN");
1043 } else if (i == UINT32_MAX) {
1044 av_log(av_log_obj, level, "UINT32_MAX");
1045 } else if (i == INT64_MAX) {
1046 av_log(av_log_obj, level, "I64_MAX");
1047 } else if (i == INT64_MIN) {
1048 av_log(av_log_obj, level, "I64_MIN");
1050 av_log(av_log_obj, level, "%"PRId64, i);
1054 static void log_value(void *av_log_obj, int level, double d)
1057 av_log(av_log_obj, level, "INT_MAX");
1058 } else if (d == INT_MIN) {
1059 av_log(av_log_obj, level, "INT_MIN");
1060 } else if (d == UINT32_MAX) {
1061 av_log(av_log_obj, level, "UINT32_MAX");
1062 } else if (d == (double)INT64_MAX) {
1063 av_log(av_log_obj, level, "I64_MAX");
1064 } else if (d == INT64_MIN) {
1065 av_log(av_log_obj, level, "I64_MIN");
1066 } else if (d == FLT_MAX) {
1067 av_log(av_log_obj, level, "FLT_MAX");
1068 } else if (d == FLT_MIN) {
1069 av_log(av_log_obj, level, "FLT_MIN");
1070 } else if (d == -FLT_MAX) {
1071 av_log(av_log_obj, level, "-FLT_MAX");
1072 } else if (d == -FLT_MIN) {
1073 av_log(av_log_obj, level, "-FLT_MIN");
1074 } else if (d == DBL_MAX) {
1075 av_log(av_log_obj, level, "DBL_MAX");
1076 } else if (d == DBL_MIN) {
1077 av_log(av_log_obj, level, "DBL_MIN");
1078 } else if (d == -DBL_MAX) {
1079 av_log(av_log_obj, level, "-DBL_MAX");
1080 } else if (d == -DBL_MIN) {
1081 av_log(av_log_obj, level, "-DBL_MIN");
1083 av_log(av_log_obj, level, "%g", d);
1087 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1089 const AVOption *opt = NULL;
1093 while ((opt = av_opt_next(obj, opt)))
1094 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1095 opt->default_val.i64 == value)
1100 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1102 const AVOption *opt = NULL;
1108 while ((opt = av_opt_next(obj, opt))) {
1109 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1110 opt->default_val.i64 & value) {
1112 av_strlcatf(flags, sizeof(flags), "+");
1113 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1117 return av_strdup(flags);
1121 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1122 int req_flags, int rej_flags, enum AVOptionType parent_type)
1124 const AVOption *opt = NULL;
1128 while ((opt = av_opt_next(obj, opt))) {
1129 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1132 /* Don't print CONST's on level one.
1133 * Don't print anything but CONST's on level two.
1134 * Only print items from the requested unit.
1136 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1138 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1140 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1142 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1143 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1145 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1146 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1149 switch (opt->type) {
1150 case AV_OPT_TYPE_FLAGS:
1151 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1153 case AV_OPT_TYPE_INT:
1154 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1156 case AV_OPT_TYPE_INT64:
1157 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1159 case AV_OPT_TYPE_UINT64:
1160 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
1162 case AV_OPT_TYPE_DOUBLE:
1163 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1165 case AV_OPT_TYPE_FLOAT:
1166 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1168 case AV_OPT_TYPE_STRING:
1169 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1171 case AV_OPT_TYPE_RATIONAL:
1172 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1174 case AV_OPT_TYPE_BINARY:
1175 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1177 case AV_OPT_TYPE_IMAGE_SIZE:
1178 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1180 case AV_OPT_TYPE_VIDEO_RATE:
1181 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1183 case AV_OPT_TYPE_PIXEL_FMT:
1184 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1186 case AV_OPT_TYPE_SAMPLE_FMT:
1187 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1189 case AV_OPT_TYPE_DURATION:
1190 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1192 case AV_OPT_TYPE_COLOR:
1193 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1195 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1196 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1198 case AV_OPT_TYPE_BOOL:
1199 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1201 case AV_OPT_TYPE_CONST:
1202 if (parent_type == AV_OPT_TYPE_INT)
1203 av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", opt->default_val.i64);
1205 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1208 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1211 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1212 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1213 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1214 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1215 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1216 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1217 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1218 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1219 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.');
1222 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1224 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1225 switch (opt->type) {
1226 case AV_OPT_TYPE_INT:
1227 case AV_OPT_TYPE_INT64:
1228 case AV_OPT_TYPE_UINT64:
1229 case AV_OPT_TYPE_DOUBLE:
1230 case AV_OPT_TYPE_FLOAT:
1231 case AV_OPT_TYPE_RATIONAL:
1232 for (i = 0; i < r->nb_ranges; i++) {
1233 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1234 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1235 av_log(av_log_obj, AV_LOG_INFO, " to ");
1236 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1237 av_log(av_log_obj, AV_LOG_INFO, ")");
1241 av_opt_freep_ranges(&r);
1244 if (opt->type != AV_OPT_TYPE_CONST &&
1245 opt->type != AV_OPT_TYPE_BINARY &&
1246 !((opt->type == AV_OPT_TYPE_COLOR ||
1247 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1248 opt->type == AV_OPT_TYPE_STRING ||
1249 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1250 !opt->default_val.str)) {
1251 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1252 switch (opt->type) {
1253 case AV_OPT_TYPE_BOOL:
1254 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1256 case AV_OPT_TYPE_FLAGS: {
1257 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1259 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1260 av_freep(&def_flags);
1262 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1266 case AV_OPT_TYPE_DURATION: {
1268 format_duration(buf, sizeof(buf), opt->default_val.i64);
1269 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1272 case AV_OPT_TYPE_INT:
1273 case AV_OPT_TYPE_UINT64:
1274 case AV_OPT_TYPE_INT64: {
1275 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1277 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1279 log_int_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1282 case AV_OPT_TYPE_DOUBLE:
1283 case AV_OPT_TYPE_FLOAT:
1284 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1286 case AV_OPT_TYPE_RATIONAL: {
1287 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1288 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1290 case AV_OPT_TYPE_PIXEL_FMT:
1291 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1293 case AV_OPT_TYPE_SAMPLE_FMT:
1294 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1296 case AV_OPT_TYPE_COLOR:
1297 case AV_OPT_TYPE_IMAGE_SIZE:
1298 case AV_OPT_TYPE_STRING:
1299 case AV_OPT_TYPE_VIDEO_RATE:
1300 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1302 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1303 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1306 av_log(av_log_obj, AV_LOG_INFO, ")");
1309 av_log(av_log_obj, AV_LOG_INFO, "\n");
1310 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1311 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags, opt->type);
1315 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1320 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1322 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags, -1);
1327 void av_opt_set_defaults(void *s)
1329 av_opt_set_defaults2(s, 0, 0);
1332 void av_opt_set_defaults2(void *s, int mask, int flags)
1334 const AVOption *opt = NULL;
1335 while ((opt = av_opt_next(s, opt))) {
1336 void *dst = ((uint8_t*)s) + opt->offset;
1338 if ((opt->flags & mask) != flags)
1341 if (opt->flags & AV_OPT_FLAG_READONLY)
1344 switch (opt->type) {
1345 case AV_OPT_TYPE_CONST:
1346 /* Nothing to be done here */
1348 case AV_OPT_TYPE_BOOL:
1349 case AV_OPT_TYPE_FLAGS:
1350 case AV_OPT_TYPE_INT:
1351 case AV_OPT_TYPE_INT64:
1352 case AV_OPT_TYPE_UINT64:
1353 case AV_OPT_TYPE_DURATION:
1354 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1355 case AV_OPT_TYPE_PIXEL_FMT:
1356 case AV_OPT_TYPE_SAMPLE_FMT:
1357 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1359 case AV_OPT_TYPE_DOUBLE:
1360 case AV_OPT_TYPE_FLOAT: {
1362 val = opt->default_val.dbl;
1363 write_number(s, opt, dst, val, 1, 1);
1366 case AV_OPT_TYPE_RATIONAL: {
1368 val = av_d2q(opt->default_val.dbl, INT_MAX);
1369 write_number(s, opt, dst, 1, val.den, val.num);
1372 case AV_OPT_TYPE_COLOR:
1373 set_string_color(s, opt, opt->default_val.str, dst);
1375 case AV_OPT_TYPE_STRING:
1376 set_string(s, opt, opt->default_val.str, dst);
1378 case AV_OPT_TYPE_IMAGE_SIZE:
1379 set_string_image_size(s, opt, opt->default_val.str, dst);
1381 case AV_OPT_TYPE_VIDEO_RATE:
1382 set_string_video_rate(s, opt, opt->default_val.str, dst);
1384 case AV_OPT_TYPE_BINARY:
1385 set_string_binary(s, opt, opt->default_val.str, dst);
1387 case AV_OPT_TYPE_DICT:
1388 /* Cannot set defaults for these types */
1391 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1392 opt->type, opt->name);
1398 * Store the value in the field in ctx that is named like key.
1399 * ctx must be an AVClass context, storing is done using AVOptions.
1401 * @param buf the string to parse, buf will be updated to point at the
1402 * separator just after the parsed key/value pair
1403 * @param key_val_sep a 0-terminated list of characters used to
1404 * separate key from value
1405 * @param pairs_sep a 0-terminated list of characters used to separate
1406 * two pairs from each other
1407 * @return 0 if the key/value pair has been successfully parsed and
1408 * set, or a negative value corresponding to an AVERROR code in case
1410 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1411 * the error code issued by av_opt_set() if the key/value pair
1414 static int parse_key_value_pair(void *ctx, const char **buf,
1415 const char *key_val_sep, const char *pairs_sep)
1417 char *key = av_get_token(buf, key_val_sep);
1422 return AVERROR(ENOMEM);
1424 if (*key && strspn(*buf, key_val_sep)) {
1426 val = av_get_token(buf, pairs_sep);
1429 return AVERROR(ENOMEM);
1432 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1434 return AVERROR(EINVAL);
1437 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1439 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1440 if (ret == AVERROR_OPTION_NOT_FOUND)
1441 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1448 int av_set_options_string(void *ctx, const char *opts,
1449 const char *key_val_sep, const char *pairs_sep)
1457 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1468 #define WHITESPACES " \n\t\r"
1470 static int is_key_char(char c)
1472 return (unsigned)((c | 32) - 'a') < 26 ||
1473 (unsigned)(c - '0') < 10 ||
1474 c == '-' || c == '_' || c == '/' || c == '.';
1478 * Read a key from a string.
1480 * The key consists of is_key_char characters and must be terminated by a
1481 * character from the delim string; spaces are ignored.
1483 * @return 0 for success (even with ellipsis), <0 for failure
1485 static int get_key(const char **ropts, const char *delim, char **rkey)
1487 const char *opts = *ropts;
1488 const char *key_start, *key_end;
1490 key_start = opts += strspn(opts, WHITESPACES);
1491 while (is_key_char(*opts))
1494 opts += strspn(opts, WHITESPACES);
1495 if (!*opts || !strchr(delim, *opts))
1496 return AVERROR(EINVAL);
1498 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1499 return AVERROR(ENOMEM);
1500 memcpy(*rkey, key_start, key_end - key_start);
1501 (*rkey)[key_end - key_start] = 0;
1506 int av_opt_get_key_value(const char **ropts,
1507 const char *key_val_sep, const char *pairs_sep,
1509 char **rkey, char **rval)
1512 char *key = NULL, *val;
1513 const char *opts = *ropts;
1515 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1516 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1517 return AVERROR(EINVAL);
1518 if (!(val = av_get_token(&opts, pairs_sep))) {
1520 return AVERROR(ENOMEM);
1528 int av_opt_set_from_string(void *ctx, const char *opts,
1529 const char *const *shorthand,
1530 const char *key_val_sep, const char *pairs_sep)
1533 const char *dummy_shorthand = NULL;
1534 char *av_uninit(parsed_key), *av_uninit(value);
1540 shorthand = &dummy_shorthand;
1543 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1544 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1545 &parsed_key, &value);
1547 if (ret == AVERROR(EINVAL))
1548 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1550 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1558 while (*shorthand) /* discard all remaining shorthand */
1561 key = *(shorthand++);
1564 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1565 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1566 if (ret == AVERROR_OPTION_NOT_FOUND)
1567 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1569 av_free(parsed_key);
1574 av_free(parsed_key);
1580 void av_opt_free(void *obj)
1582 const AVOption *o = NULL;
1583 while ((o = av_opt_next(obj, o))) {
1585 case AV_OPT_TYPE_STRING:
1586 case AV_OPT_TYPE_BINARY:
1587 av_freep((uint8_t *)obj + o->offset);
1590 case AV_OPT_TYPE_DICT:
1591 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1600 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1602 AVDictionaryEntry *t = NULL;
1603 AVDictionary *tmp = NULL;
1609 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1610 ret = av_opt_set(obj, t->key, t->value, search_flags);
1611 if (ret == AVERROR_OPTION_NOT_FOUND)
1612 ret = av_dict_set(&tmp, t->key, t->value, 0);
1614 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1620 av_dict_free(options);
1625 int av_opt_set_dict(void *obj, AVDictionary **options)
1627 return av_opt_set_dict2(obj, options, 0);
1630 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1631 int opt_flags, int search_flags)
1633 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1636 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1637 int opt_flags, int search_flags, void **target_obj)
1640 const AVOption *o = NULL;
1650 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1651 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1652 const AVClass *child = NULL;
1653 while (child = av_opt_child_class_next(c, child))
1654 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1658 while (child = av_opt_child_next(obj, child))
1659 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1664 while (o = av_opt_next(obj, o)) {
1665 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1666 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1667 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1669 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1680 void *av_opt_child_next(void *obj, void *prev)
1682 const AVClass *c = *(AVClass **)obj;
1684 return c->child_next(obj, prev);
1688 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1690 if (parent->child_class_next)
1691 return parent->child_class_next(prev);
1695 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1697 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1700 return (uint8_t*)obj + opt->offset;
1703 static int opt_size(enum AVOptionType type)
1706 case AV_OPT_TYPE_BOOL:
1707 case AV_OPT_TYPE_INT:
1708 case AV_OPT_TYPE_FLAGS:
1710 case AV_OPT_TYPE_DURATION:
1711 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1712 case AV_OPT_TYPE_INT64:
1713 case AV_OPT_TYPE_UINT64:
1714 return sizeof(int64_t);
1715 case AV_OPT_TYPE_DOUBLE:
1716 return sizeof(double);
1717 case AV_OPT_TYPE_FLOAT:
1718 return sizeof(float);
1719 case AV_OPT_TYPE_STRING:
1720 return sizeof(uint8_t*);
1721 case AV_OPT_TYPE_VIDEO_RATE:
1722 case AV_OPT_TYPE_RATIONAL:
1723 return sizeof(AVRational);
1724 case AV_OPT_TYPE_BINARY:
1725 return sizeof(uint8_t*) + sizeof(int);
1726 case AV_OPT_TYPE_IMAGE_SIZE:
1727 return sizeof(int[2]);
1728 case AV_OPT_TYPE_PIXEL_FMT:
1729 return sizeof(enum AVPixelFormat);
1730 case AV_OPT_TYPE_SAMPLE_FMT:
1731 return sizeof(enum AVSampleFormat);
1732 case AV_OPT_TYPE_COLOR:
1735 return AVERROR(EINVAL);
1738 int av_opt_copy(void *dst, const void *src)
1740 const AVOption *o = NULL;
1745 return AVERROR(EINVAL);
1747 c = *(AVClass **)src;
1748 if (!c || c != *(AVClass **)dst)
1749 return AVERROR(EINVAL);
1751 while ((o = av_opt_next(src, o))) {
1752 void *field_dst = (uint8_t *)dst + o->offset;
1753 void *field_src = (uint8_t *)src + o->offset;
1754 uint8_t **field_dst8 = (uint8_t **)field_dst;
1755 uint8_t **field_src8 = (uint8_t **)field_src;
1757 if (o->type == AV_OPT_TYPE_STRING) {
1758 if (*field_dst8 != *field_src8)
1759 av_freep(field_dst8);
1760 *field_dst8 = av_strdup(*field_src8);
1761 if (*field_src8 && !*field_dst8)
1762 ret = AVERROR(ENOMEM);
1763 } else if (o->type == AV_OPT_TYPE_BINARY) {
1764 int len = *(int *)(field_src8 + 1);
1765 if (*field_dst8 != *field_src8)
1766 av_freep(field_dst8);
1767 *field_dst8 = av_memdup(*field_src8, len);
1768 if (len && !*field_dst8) {
1769 ret = AVERROR(ENOMEM);
1772 *(int *)(field_dst8 + 1) = len;
1773 } else if (o->type == AV_OPT_TYPE_CONST) {
1775 } else if (o->type == AV_OPT_TYPE_DICT) {
1776 AVDictionary **sdict = (AVDictionary **) field_src;
1777 AVDictionary **ddict = (AVDictionary **) field_dst;
1778 if (*sdict != *ddict)
1779 av_dict_free(ddict);
1781 av_dict_copy(ddict, *sdict, 0);
1782 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1783 ret = AVERROR(ENOMEM);
1785 int size = opt_size(o->type);
1789 memcpy(field_dst, field_src, size);
1795 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1798 const AVClass *c = *(AVClass**)obj;
1799 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1801 if (c->version > (52 << 16 | 11 << 8))
1802 callback = c->query_ranges;
1805 callback = av_opt_query_ranges_default;
1807 ret = callback(ranges_arg, obj, key, flags);
1809 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1811 (*ranges_arg)->nb_components = ret;
1816 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1818 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1819 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1820 AVOptionRange *range = av_mallocz(sizeof(*range));
1821 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1826 if (!ranges || !range || !range_array || !field) {
1827 ret = AVERROR(ENOMEM);
1831 ranges->range = range_array;
1832 ranges->range[0] = range;
1833 ranges->nb_ranges = 1;
1834 ranges->nb_components = 1;
1835 range->is_range = 1;
1836 range->value_min = field->min;
1837 range->value_max = field->max;
1839 switch (field->type) {
1840 case AV_OPT_TYPE_BOOL:
1841 case AV_OPT_TYPE_INT:
1842 case AV_OPT_TYPE_INT64:
1843 case AV_OPT_TYPE_UINT64:
1844 case AV_OPT_TYPE_PIXEL_FMT:
1845 case AV_OPT_TYPE_SAMPLE_FMT:
1846 case AV_OPT_TYPE_FLOAT:
1847 case AV_OPT_TYPE_DOUBLE:
1848 case AV_OPT_TYPE_DURATION:
1849 case AV_OPT_TYPE_COLOR:
1850 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1852 case AV_OPT_TYPE_STRING:
1853 range->component_min = 0;
1854 range->component_max = 0x10FFFF; // max unicode value
1855 range->value_min = -1;
1856 range->value_max = INT_MAX;
1858 case AV_OPT_TYPE_RATIONAL:
1859 range->component_min = INT_MIN;
1860 range->component_max = INT_MAX;
1862 case AV_OPT_TYPE_IMAGE_SIZE:
1863 range->component_min = 0;
1864 range->component_max = INT_MAX/128/8;
1865 range->value_min = 0;
1866 range->value_max = INT_MAX/8;
1868 case AV_OPT_TYPE_VIDEO_RATE:
1869 range->component_min = 1;
1870 range->component_max = INT_MAX;
1871 range->value_min = 1;
1872 range->value_max = INT_MAX;
1875 ret = AVERROR(ENOSYS);
1879 *ranges_arg = ranges;
1884 av_free(range_array);
1888 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1891 AVOptionRanges *ranges = *rangesp;
1896 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1897 AVOptionRange *range = ranges->range[i];
1899 av_freep(&range->str);
1900 av_freep(&ranges->range[i]);
1903 av_freep(&ranges->range);
1907 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1918 return AVERROR(EINVAL);
1920 dst = ((uint8_t*)obj) + o->offset;
1923 case AV_OPT_TYPE_CONST:
1925 case AV_OPT_TYPE_BOOL:
1926 case AV_OPT_TYPE_FLAGS:
1927 case AV_OPT_TYPE_PIXEL_FMT:
1928 case AV_OPT_TYPE_SAMPLE_FMT:
1929 case AV_OPT_TYPE_INT:
1930 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1931 case AV_OPT_TYPE_DURATION:
1932 case AV_OPT_TYPE_INT64:
1933 case AV_OPT_TYPE_UINT64:
1934 read_number(o, dst, NULL, NULL, &i64);
1935 return o->default_val.i64 == i64;
1936 case AV_OPT_TYPE_STRING:
1937 str = *(char **)dst;
1938 if (str == o->default_val.str) //2 NULLs
1940 if (!str || !o->default_val.str) //1 NULL
1942 return !strcmp(str, o->default_val.str);
1943 case AV_OPT_TYPE_DOUBLE:
1944 read_number(o, dst, &d, NULL, NULL);
1945 return o->default_val.dbl == d;
1946 case AV_OPT_TYPE_FLOAT:
1947 read_number(o, dst, &d, NULL, NULL);
1948 f = o->default_val.dbl;
1951 case AV_OPT_TYPE_RATIONAL:
1952 q = av_d2q(o->default_val.dbl, INT_MAX);
1953 return !av_cmp_q(*(AVRational*)dst, q);
1954 case AV_OPT_TYPE_BINARY: {
1959 int opt_size = *(int *)((void **)dst + 1);
1960 void *opt_ptr = *(void **)dst;
1961 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1963 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1965 if (opt_size != strlen(o->default_val.str) / 2)
1967 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1969 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1973 case AV_OPT_TYPE_DICT:
1974 /* Binary and dict have not default support yet. Any pointer is not default. */
1975 return !!(*(void **)dst);
1976 case AV_OPT_TYPE_IMAGE_SIZE:
1977 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1979 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1981 return (w == *(int *)dst) && (h == *((int *)dst+1));
1982 case AV_OPT_TYPE_VIDEO_RATE:
1983 q = (AVRational){0, 0};
1984 if (o->default_val.str) {
1985 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1988 return !av_cmp_q(*(AVRational*)dst, q);
1989 case AV_OPT_TYPE_COLOR: {
1990 uint8_t color[4] = {0, 0, 0, 0};
1991 if (o->default_val.str) {
1992 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1995 return !memcmp(color, dst, sizeof(color));
1998 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
2001 return AVERROR_PATCHWELCOME;
2004 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
2009 return AVERROR(EINVAL);
2010 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
2012 return AVERROR_OPTION_NOT_FOUND;
2013 return av_opt_is_set_to_default(target, o);
2016 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
2017 const char key_val_sep, const char pairs_sep)
2019 const AVOption *o = NULL;
2023 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
2025 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
2026 pairs_sep == '\\' || key_val_sep == '\\') {
2027 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
2028 return AVERROR(EINVAL);
2031 if (!obj || !buffer)
2032 return AVERROR(EINVAL);
2035 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
2037 while (o = av_opt_next(obj, o)) {
2038 if (o->type == AV_OPT_TYPE_CONST)
2040 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2042 else if (((o->flags & opt_flags) != opt_flags))
2044 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
2046 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2047 av_bprint_finalize(&bprint, NULL);
2052 av_bprint_append_data(&bprint, &pairs_sep, 1);
2053 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2054 av_bprint_append_data(&bprint, &key_val_sep, 1);
2055 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2059 av_bprint_finalize(&bprint, buffer);