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:
503 if ((ret = av_parse_time(dst, val, 1)) < 0)
504 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
508 case AV_OPT_TYPE_COLOR:
509 return set_string_color(obj, o, val, dst);
510 case AV_OPT_TYPE_CHANNEL_LAYOUT:
511 if (!val || !strcmp(val, "none")) {
514 int64_t cl = av_get_channel_layout(val);
516 av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as channel layout\n", val);
517 ret = AVERROR(EINVAL);
519 *(int64_t *)dst = cl;
525 av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
526 return AVERROR(EINVAL);
529 #define OPT_EVAL_NUMBER(name, opttype, vartype) \
530 int av_opt_eval_ ## name(void *obj, const AVOption *o, \
531 const char *val, vartype *name ## _out) \
533 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY) \
534 return AVERROR(EINVAL); \
535 return set_string_number(obj, obj, o, val, name ## _out); \
538 OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
539 OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
540 OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
541 OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
542 OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
543 OPT_EVAL_NUMBER(q, AV_OPT_TYPE_RATIONAL, AVRational)
545 static int set_number(void *obj, const char *name, double num, int den, int64_t intnum,
548 void *dst, *target_obj;
549 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
551 if (!o || !target_obj)
552 return AVERROR_OPTION_NOT_FOUND;
554 if (o->flags & AV_OPT_FLAG_READONLY)
555 return AVERROR(EINVAL);
557 dst = ((uint8_t *)target_obj) + o->offset;
558 return write_number(obj, o, dst, num, den, intnum);
561 int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
563 return set_number(obj, name, 1, 1, val, search_flags);
566 int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
568 return set_number(obj, name, val, 1, 1, search_flags);
571 int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
573 return set_number(obj, name, val.num, val.den, 1, search_flags);
576 int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
579 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
584 if (!o || !target_obj)
585 return AVERROR_OPTION_NOT_FOUND;
587 if (o->type != AV_OPT_TYPE_BINARY || o->flags & AV_OPT_FLAG_READONLY)
588 return AVERROR(EINVAL);
590 ptr = len ? av_malloc(len) : NULL;
592 return AVERROR(ENOMEM);
594 dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
595 lendst = (int *)(dst + 1);
601 memcpy(ptr, val, len);
606 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags)
609 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
611 if (!o || !target_obj)
612 return AVERROR_OPTION_NOT_FOUND;
613 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
614 av_log(obj, AV_LOG_ERROR,
615 "The value set by option '%s' is not an image size.\n", o->name);
616 return AVERROR(EINVAL);
619 av_log(obj, AV_LOG_ERROR,
620 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->name);
621 return AVERROR(EINVAL);
623 *(int *)(((uint8_t *)target_obj) + o->offset) = w;
624 *(int *)(((uint8_t *)target_obj+sizeof(int)) + o->offset) = h;
628 int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
631 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
633 if (!o || !target_obj)
634 return AVERROR_OPTION_NOT_FOUND;
635 if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
636 av_log(obj, AV_LOG_ERROR,
637 "The value set by option '%s' is not a video rate.\n", o->name);
638 return AVERROR(EINVAL);
640 if (val.num <= 0 || val.den <= 0)
641 return AVERROR(EINVAL);
642 return set_number(obj, name, val.num, val.den, 1, search_flags);
645 static int set_format(void *obj, const char *name, int fmt, int search_flags,
646 enum AVOptionType type, const char *desc, int nb_fmts)
649 const AVOption *o = av_opt_find2(obj, name, NULL, 0,
650 search_flags, &target_obj);
653 if (!o || !target_obj)
654 return AVERROR_OPTION_NOT_FOUND;
655 if (o->type != type) {
656 av_log(obj, AV_LOG_ERROR,
657 "The value set by option '%s' is not a %s format", name, desc);
658 return AVERROR(EINVAL);
661 min = FFMAX(o->min, -1);
662 max = FFMIN(o->max, nb_fmts-1);
664 if (fmt < min || fmt > max) {
665 av_log(obj, AV_LOG_ERROR,
666 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
667 fmt, name, desc, min, max);
668 return AVERROR(ERANGE);
670 *(int *)(((uint8_t *)target_obj) + o->offset) = fmt;
674 int av_opt_set_pixel_fmt(void *obj, const char *name, enum AVPixelFormat fmt, int search_flags)
676 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_PIXEL_FMT, "pixel", AV_PIX_FMT_NB);
679 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
681 return set_format(obj, name, fmt, search_flags, AV_OPT_TYPE_SAMPLE_FMT, "sample", AV_SAMPLE_FMT_NB);
684 int av_opt_set_channel_layout(void *obj, const char *name, int64_t cl, int search_flags)
687 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
689 if (!o || !target_obj)
690 return AVERROR_OPTION_NOT_FOUND;
691 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
692 av_log(obj, AV_LOG_ERROR,
693 "The value set by option '%s' is not a channel layout.\n", o->name);
694 return AVERROR(EINVAL);
696 *(int64_t *)(((uint8_t *)target_obj) + o->offset) = cl;
700 int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val,
705 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
707 if (!o || !target_obj)
708 return AVERROR_OPTION_NOT_FOUND;
709 if (o->flags & AV_OPT_FLAG_READONLY)
710 return AVERROR(EINVAL);
712 dst = (AVDictionary **)(((uint8_t *)target_obj) + o->offset);
714 av_dict_copy(dst, val, 0);
719 static void format_duration(char *buf, size_t size, int64_t d)
723 av_assert0(size >= 25);
724 if (d < 0 && d != INT64_MIN) {
730 snprintf(buf, size, "INT64_MAX");
731 else if (d == INT64_MIN)
732 snprintf(buf, size, "INT64_MIN");
733 else if (d > (int64_t)3600*1000000)
734 snprintf(buf, size, "%"PRId64":%02d:%02d.%06d", d / 3600000000,
735 (int)((d / 60000000) % 60),
736 (int)((d / 1000000) % 60),
738 else if (d > 60*1000000)
739 snprintf(buf, size, "%d:%02d.%06d",
741 (int)((d / 1000000) % 60),
744 snprintf(buf, size, "%d.%06d",
747 e = buf + strlen(buf);
748 while (e > buf && e[-1] == '0')
750 if (e > buf && e[-1] == '.')
754 int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
756 void *dst, *target_obj;
757 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
758 uint8_t *bin, buf[128];
762 if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
763 return AVERROR_OPTION_NOT_FOUND;
765 if (o->flags & AV_OPT_FLAG_DEPRECATED)
766 av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help);
768 dst = (uint8_t *)target_obj + o->offset;
772 case AV_OPT_TYPE_BOOL:
773 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid"));
775 case AV_OPT_TYPE_FLAGS:
776 ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst);
778 case AV_OPT_TYPE_INT:
779 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
781 case AV_OPT_TYPE_INT64:
782 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
784 case AV_OPT_TYPE_UINT64:
785 ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst);
787 case AV_OPT_TYPE_FLOAT:
788 ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst);
790 case AV_OPT_TYPE_DOUBLE:
791 ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst);
793 case AV_OPT_TYPE_VIDEO_RATE:
794 case AV_OPT_TYPE_RATIONAL:
795 ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den);
797 case AV_OPT_TYPE_CONST:
798 ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl);
800 case AV_OPT_TYPE_STRING:
801 if (*(uint8_t **)dst) {
802 *out_val = av_strdup(*(uint8_t **)dst);
803 } else if (search_flags & AV_OPT_ALLOW_NULL) {
807 *out_val = av_strdup("");
809 return *out_val ? 0 : AVERROR(ENOMEM);
810 case AV_OPT_TYPE_BINARY:
811 if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) {
815 len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *));
816 if ((uint64_t)len * 2 + 1 > INT_MAX)
817 return AVERROR(EINVAL);
818 if (!(*out_val = av_malloc(len * 2 + 1)))
819 return AVERROR(ENOMEM);
824 bin = *(uint8_t **)dst;
825 for (i = 0; i < len; i++)
826 snprintf(*out_val + i * 2, 3, "%02X", bin[i]);
828 case AV_OPT_TYPE_IMAGE_SIZE:
829 ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]);
831 case AV_OPT_TYPE_PIXEL_FMT:
832 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none"));
834 case AV_OPT_TYPE_SAMPLE_FMT:
835 ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
837 case AV_OPT_TYPE_DURATION:
838 i64 = *(int64_t *)dst;
839 format_duration(buf, sizeof(buf), i64);
840 ret = strlen(buf); // no overflow possible, checked by an assert
842 case AV_OPT_TYPE_COLOR:
843 ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x",
844 (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1],
845 (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]);
847 case AV_OPT_TYPE_CHANNEL_LAYOUT:
848 i64 = *(int64_t *)dst;
849 ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64);
852 return AVERROR(EINVAL);
855 if (ret >= sizeof(buf))
856 return AVERROR(EINVAL);
857 *out_val = av_strdup(buf);
858 return *out_val ? 0 : AVERROR(ENOMEM);
861 static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum,
864 void *dst, *target_obj;
865 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
866 if (!o || !target_obj)
869 dst = ((uint8_t *)target_obj) + o->offset;
871 if (o_out) *o_out= o;
873 return read_number(o, dst, num, den, intnum);
881 int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
887 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
889 *out_val = num * intnum / den;
893 int av_opt_get_double(void *obj, const char *name, int search_flags, double *out_val)
899 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
901 *out_val = num * intnum / den;
905 int av_opt_get_q(void *obj, const char *name, int search_flags, AVRational *out_val)
911 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
914 if (num == 1.0 && (int)intnum == intnum)
915 *out_val = (AVRational){intnum, den};
917 *out_val = av_d2q(num*intnum/den, 1<<24);
921 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out)
923 void *dst, *target_obj;
924 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
925 if (!o || !target_obj)
926 return AVERROR_OPTION_NOT_FOUND;
927 if (o->type != AV_OPT_TYPE_IMAGE_SIZE) {
928 av_log(obj, AV_LOG_ERROR,
929 "The value for option '%s' is not an image size.\n", name);
930 return AVERROR(EINVAL);
933 dst = ((uint8_t*)target_obj) + o->offset;
934 if (w_out) *w_out = *(int *)dst;
935 if (h_out) *h_out = *((int *)dst+1);
939 int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
945 if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
948 if (num == 1.0 && (int)intnum == intnum)
949 *out_val = (AVRational) { intnum, den };
951 *out_val = av_d2q(num * intnum / den, 1 << 24);
955 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
956 enum AVOptionType type, const char *desc)
958 void *dst, *target_obj;
959 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
960 if (!o || !target_obj)
961 return AVERROR_OPTION_NOT_FOUND;
962 if (o->type != type) {
963 av_log(obj, AV_LOG_ERROR,
964 "The value for option '%s' is not a %s format.\n", desc, name);
965 return AVERROR(EINVAL);
968 dst = ((uint8_t*)target_obj) + o->offset;
969 *out_fmt = *(int *)dst;
973 int av_opt_get_pixel_fmt(void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt)
975 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_PIXEL_FMT, "pixel");
978 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt)
980 return get_format(obj, name, search_flags, out_fmt, AV_OPT_TYPE_SAMPLE_FMT, "sample");
983 int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int64_t *cl)
985 void *dst, *target_obj;
986 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
987 if (!o || !target_obj)
988 return AVERROR_OPTION_NOT_FOUND;
989 if (o->type != AV_OPT_TYPE_CHANNEL_LAYOUT) {
990 av_log(obj, AV_LOG_ERROR,
991 "The value for option '%s' is not a channel layout.\n", name);
992 return AVERROR(EINVAL);
995 dst = ((uint8_t*)target_obj) + o->offset;
996 *cl = *(int64_t *)dst;
1000 int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
1004 const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
1006 if (!o || !target_obj)
1007 return AVERROR_OPTION_NOT_FOUND;
1008 if (o->type != AV_OPT_TYPE_DICT)
1009 return AVERROR(EINVAL);
1011 src = *(AVDictionary **)(((uint8_t *)target_obj) + o->offset);
1012 av_dict_copy(out_val, src, 0);
1017 int av_opt_flag_is_set(void *obj, const char *field_name, const char *flag_name)
1019 const AVOption *field = av_opt_find(obj, field_name, NULL, 0, 0);
1020 const AVOption *flag = av_opt_find(obj, flag_name,
1021 field ? field->unit : NULL, 0, 0);
1024 if (!field || !flag || flag->type != AV_OPT_TYPE_CONST ||
1025 av_opt_get_int(obj, field_name, 0, &res) < 0)
1027 return res & flag->default_val.i64;
1030 static void log_value(void *av_log_obj, int level, double d)
1033 av_log(av_log_obj, level, "INT_MAX");
1034 } else if (d == INT_MIN) {
1035 av_log(av_log_obj, level, "INT_MIN");
1036 } else if (d == UINT32_MAX) {
1037 av_log(av_log_obj, level, "UINT32_MAX");
1038 } else if (d == (double)INT64_MAX) {
1039 av_log(av_log_obj, level, "I64_MAX");
1040 } else if (d == INT64_MIN) {
1041 av_log(av_log_obj, level, "I64_MIN");
1042 } else if (d == FLT_MAX) {
1043 av_log(av_log_obj, level, "FLT_MAX");
1044 } else if (d == FLT_MIN) {
1045 av_log(av_log_obj, level, "FLT_MIN");
1046 } else if (d == -FLT_MAX) {
1047 av_log(av_log_obj, level, "-FLT_MAX");
1048 } else if (d == -FLT_MIN) {
1049 av_log(av_log_obj, level, "-FLT_MIN");
1050 } else if (d == DBL_MAX) {
1051 av_log(av_log_obj, level, "DBL_MAX");
1052 } else if (d == DBL_MIN) {
1053 av_log(av_log_obj, level, "DBL_MIN");
1054 } else if (d == -DBL_MAX) {
1055 av_log(av_log_obj, level, "-DBL_MAX");
1056 } else if (d == -DBL_MIN) {
1057 av_log(av_log_obj, level, "-DBL_MIN");
1059 av_log(av_log_obj, level, "%g", d);
1063 static const char *get_opt_const_name(void *obj, const char *unit, int64_t value)
1065 const AVOption *opt = NULL;
1069 while ((opt = av_opt_next(obj, opt)))
1070 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1071 opt->default_val.i64 == value)
1076 static char *get_opt_flags_string(void *obj, const char *unit, int64_t value)
1078 const AVOption *opt = NULL;
1084 while ((opt = av_opt_next(obj, opt))) {
1085 if (opt->type == AV_OPT_TYPE_CONST && !strcmp(opt->unit, unit) &&
1086 opt->default_val.i64 & value) {
1088 av_strlcatf(flags, sizeof(flags), "+");
1089 av_strlcatf(flags, sizeof(flags), "%s", opt->name);
1093 return av_strdup(flags);
1097 static void opt_list(void *obj, void *av_log_obj, const char *unit,
1098 int req_flags, int rej_flags)
1100 const AVOption *opt = NULL;
1104 while ((opt = av_opt_next(obj, opt))) {
1105 if (!(opt->flags & req_flags) || (opt->flags & rej_flags))
1108 /* Don't print CONST's on level one.
1109 * Don't print anything but CONST's on level two.
1110 * Only print items from the requested unit.
1112 if (!unit && opt->type == AV_OPT_TYPE_CONST)
1114 else if (unit && opt->type != AV_OPT_TYPE_CONST)
1116 else if (unit && opt->type == AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
1118 else if (unit && opt->type == AV_OPT_TYPE_CONST)
1119 av_log(av_log_obj, AV_LOG_INFO, " %-15s ", opt->name);
1121 av_log(av_log_obj, AV_LOG_INFO, " %s%-17s ",
1122 (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
1125 switch (opt->type) {
1126 case AV_OPT_TYPE_FLAGS:
1127 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<flags>");
1129 case AV_OPT_TYPE_INT:
1130 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int>");
1132 case AV_OPT_TYPE_INT64:
1133 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<int64>");
1135 case AV_OPT_TYPE_UINT64:
1136 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<uint64>");
1138 case AV_OPT_TYPE_DOUBLE:
1139 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<double>");
1141 case AV_OPT_TYPE_FLOAT:
1142 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<float>");
1144 case AV_OPT_TYPE_STRING:
1145 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<string>");
1147 case AV_OPT_TYPE_RATIONAL:
1148 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<rational>");
1150 case AV_OPT_TYPE_BINARY:
1151 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<binary>");
1153 case AV_OPT_TYPE_IMAGE_SIZE:
1154 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
1156 case AV_OPT_TYPE_VIDEO_RATE:
1157 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
1159 case AV_OPT_TYPE_PIXEL_FMT:
1160 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
1162 case AV_OPT_TYPE_SAMPLE_FMT:
1163 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
1165 case AV_OPT_TYPE_DURATION:
1166 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
1168 case AV_OPT_TYPE_COLOR:
1169 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<color>");
1171 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1172 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<channel_layout>");
1174 case AV_OPT_TYPE_BOOL:
1175 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<boolean>");
1177 case AV_OPT_TYPE_CONST:
1179 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
1182 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_ENCODING_PARAM) ? 'E' : '.');
1183 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_DECODING_PARAM) ? 'D' : '.');
1184 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_FILTERING_PARAM)? 'F' : '.');
1185 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_VIDEO_PARAM ) ? 'V' : '.');
1186 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_AUDIO_PARAM ) ? 'A' : '.');
1187 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) ? 'S' : '.');
1188 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_EXPORT) ? 'X' : '.');
1189 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_READONLY) ? 'R' : '.');
1190 av_log(av_log_obj, AV_LOG_INFO, "%c", (opt->flags & AV_OPT_FLAG_BSF_PARAM) ? 'B' : '.');
1193 av_log(av_log_obj, AV_LOG_INFO, " %s", opt->help);
1195 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
1196 switch (opt->type) {
1197 case AV_OPT_TYPE_INT:
1198 case AV_OPT_TYPE_INT64:
1199 case AV_OPT_TYPE_UINT64:
1200 case AV_OPT_TYPE_DOUBLE:
1201 case AV_OPT_TYPE_FLOAT:
1202 case AV_OPT_TYPE_RATIONAL:
1203 for (i = 0; i < r->nb_ranges; i++) {
1204 av_log(av_log_obj, AV_LOG_INFO, " (from ");
1205 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_min);
1206 av_log(av_log_obj, AV_LOG_INFO, " to ");
1207 log_value(av_log_obj, AV_LOG_INFO, r->range[i]->value_max);
1208 av_log(av_log_obj, AV_LOG_INFO, ")");
1212 av_opt_freep_ranges(&r);
1215 if (opt->type != AV_OPT_TYPE_CONST &&
1216 opt->type != AV_OPT_TYPE_BINARY &&
1217 !((opt->type == AV_OPT_TYPE_COLOR ||
1218 opt->type == AV_OPT_TYPE_IMAGE_SIZE ||
1219 opt->type == AV_OPT_TYPE_STRING ||
1220 opt->type == AV_OPT_TYPE_VIDEO_RATE) &&
1221 !opt->default_val.str)) {
1222 av_log(av_log_obj, AV_LOG_INFO, " (default ");
1223 switch (opt->type) {
1224 case AV_OPT_TYPE_BOOL:
1225 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(get_bool_name(opt->default_val.i64), "invalid"));
1227 case AV_OPT_TYPE_FLAGS: {
1228 char *def_flags = get_opt_flags_string(obj, opt->unit, opt->default_val.i64);
1230 av_log(av_log_obj, AV_LOG_INFO, "%s", def_flags);
1231 av_freep(&def_flags);
1233 av_log(av_log_obj, AV_LOG_INFO, "%"PRIX64, opt->default_val.i64);
1237 case AV_OPT_TYPE_DURATION: {
1239 format_duration(buf, sizeof(buf), opt->default_val.i64);
1240 av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
1243 case AV_OPT_TYPE_INT:
1244 case AV_OPT_TYPE_UINT64:
1245 case AV_OPT_TYPE_INT64: {
1246 const char *def_const = get_opt_const_name(obj, opt->unit, opt->default_val.i64);
1248 av_log(av_log_obj, AV_LOG_INFO, "%s", def_const);
1250 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.i64);
1253 case AV_OPT_TYPE_DOUBLE:
1254 case AV_OPT_TYPE_FLOAT:
1255 log_value(av_log_obj, AV_LOG_INFO, opt->default_val.dbl);
1257 case AV_OPT_TYPE_RATIONAL: {
1258 AVRational q = av_d2q(opt->default_val.dbl, INT_MAX);
1259 av_log(av_log_obj, AV_LOG_INFO, "%d/%d", q.num, q.den); }
1261 case AV_OPT_TYPE_PIXEL_FMT:
1262 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(opt->default_val.i64), "none"));
1264 case AV_OPT_TYPE_SAMPLE_FMT:
1265 av_log(av_log_obj, AV_LOG_INFO, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(opt->default_val.i64), "none"));
1267 case AV_OPT_TYPE_COLOR:
1268 case AV_OPT_TYPE_IMAGE_SIZE:
1269 case AV_OPT_TYPE_STRING:
1270 case AV_OPT_TYPE_VIDEO_RATE:
1271 av_log(av_log_obj, AV_LOG_INFO, "\"%s\"", opt->default_val.str);
1273 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1274 av_log(av_log_obj, AV_LOG_INFO, "0x%"PRIx64, opt->default_val.i64);
1277 av_log(av_log_obj, AV_LOG_INFO, ")");
1280 av_log(av_log_obj, AV_LOG_INFO, "\n");
1281 if (opt->unit && opt->type != AV_OPT_TYPE_CONST)
1282 opt_list(obj, av_log_obj, opt->unit, req_flags, rej_flags);
1286 int av_opt_show2(void *obj, void *av_log_obj, int req_flags, int rej_flags)
1291 av_log(av_log_obj, AV_LOG_INFO, "%s AVOptions:\n", (*(AVClass **)obj)->class_name);
1293 opt_list(obj, av_log_obj, NULL, req_flags, rej_flags);
1298 void av_opt_set_defaults(void *s)
1300 av_opt_set_defaults2(s, 0, 0);
1303 void av_opt_set_defaults2(void *s, int mask, int flags)
1305 const AVOption *opt = NULL;
1306 while ((opt = av_opt_next(s, opt))) {
1307 void *dst = ((uint8_t*)s) + opt->offset;
1309 if ((opt->flags & mask) != flags)
1312 if (opt->flags & AV_OPT_FLAG_READONLY)
1315 switch (opt->type) {
1316 case AV_OPT_TYPE_CONST:
1317 /* Nothing to be done here */
1319 case AV_OPT_TYPE_BOOL:
1320 case AV_OPT_TYPE_FLAGS:
1321 case AV_OPT_TYPE_INT:
1322 case AV_OPT_TYPE_INT64:
1323 case AV_OPT_TYPE_UINT64:
1324 case AV_OPT_TYPE_DURATION:
1325 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1326 case AV_OPT_TYPE_PIXEL_FMT:
1327 case AV_OPT_TYPE_SAMPLE_FMT:
1328 write_number(s, opt, dst, 1, 1, opt->default_val.i64);
1330 case AV_OPT_TYPE_DOUBLE:
1331 case AV_OPT_TYPE_FLOAT: {
1333 val = opt->default_val.dbl;
1334 write_number(s, opt, dst, val, 1, 1);
1337 case AV_OPT_TYPE_RATIONAL: {
1339 val = av_d2q(opt->default_val.dbl, INT_MAX);
1340 write_number(s, opt, dst, 1, val.den, val.num);
1343 case AV_OPT_TYPE_COLOR:
1344 set_string_color(s, opt, opt->default_val.str, dst);
1346 case AV_OPT_TYPE_STRING:
1347 set_string(s, opt, opt->default_val.str, dst);
1349 case AV_OPT_TYPE_IMAGE_SIZE:
1350 set_string_image_size(s, opt, opt->default_val.str, dst);
1352 case AV_OPT_TYPE_VIDEO_RATE:
1353 set_string_video_rate(s, opt, opt->default_val.str, dst);
1355 case AV_OPT_TYPE_BINARY:
1356 set_string_binary(s, opt, opt->default_val.str, dst);
1358 case AV_OPT_TYPE_DICT:
1359 /* Cannot set defaults for these types */
1362 av_log(s, AV_LOG_DEBUG, "AVOption type %d of option %s not implemented yet\n",
1363 opt->type, opt->name);
1369 * Store the value in the field in ctx that is named like key.
1370 * ctx must be an AVClass context, storing is done using AVOptions.
1372 * @param buf the string to parse, buf will be updated to point at the
1373 * separator just after the parsed key/value pair
1374 * @param key_val_sep a 0-terminated list of characters used to
1375 * separate key from value
1376 * @param pairs_sep a 0-terminated list of characters used to separate
1377 * two pairs from each other
1378 * @return 0 if the key/value pair has been successfully parsed and
1379 * set, or a negative value corresponding to an AVERROR code in case
1381 * AVERROR(EINVAL) if the key/value pair cannot be parsed,
1382 * the error code issued by av_opt_set() if the key/value pair
1385 static int parse_key_value_pair(void *ctx, const char **buf,
1386 const char *key_val_sep, const char *pairs_sep)
1388 char *key = av_get_token(buf, key_val_sep);
1393 return AVERROR(ENOMEM);
1395 if (*key && strspn(*buf, key_val_sep)) {
1397 val = av_get_token(buf, pairs_sep);
1400 return AVERROR(ENOMEM);
1403 av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
1405 return AVERROR(EINVAL);
1408 av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
1410 ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
1411 if (ret == AVERROR_OPTION_NOT_FOUND)
1412 av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
1419 int av_set_options_string(void *ctx, const char *opts,
1420 const char *key_val_sep, const char *pairs_sep)
1428 if ((ret = parse_key_value_pair(ctx, &opts, key_val_sep, pairs_sep)) < 0)
1439 #define WHITESPACES " \n\t\r"
1441 static int is_key_char(char c)
1443 return (unsigned)((c | 32) - 'a') < 26 ||
1444 (unsigned)(c - '0') < 10 ||
1445 c == '-' || c == '_' || c == '/' || c == '.';
1449 * Read a key from a string.
1451 * The key consists of is_key_char characters and must be terminated by a
1452 * character from the delim string; spaces are ignored.
1454 * @return 0 for success (even with ellipsis), <0 for failure
1456 static int get_key(const char **ropts, const char *delim, char **rkey)
1458 const char *opts = *ropts;
1459 const char *key_start, *key_end;
1461 key_start = opts += strspn(opts, WHITESPACES);
1462 while (is_key_char(*opts))
1465 opts += strspn(opts, WHITESPACES);
1466 if (!*opts || !strchr(delim, *opts))
1467 return AVERROR(EINVAL);
1469 if (!(*rkey = av_malloc(key_end - key_start + 1)))
1470 return AVERROR(ENOMEM);
1471 memcpy(*rkey, key_start, key_end - key_start);
1472 (*rkey)[key_end - key_start] = 0;
1477 int av_opt_get_key_value(const char **ropts,
1478 const char *key_val_sep, const char *pairs_sep,
1480 char **rkey, char **rval)
1483 char *key = NULL, *val;
1484 const char *opts = *ropts;
1486 if ((ret = get_key(&opts, key_val_sep, &key)) < 0 &&
1487 !(flags & AV_OPT_FLAG_IMPLICIT_KEY))
1488 return AVERROR(EINVAL);
1489 if (!(val = av_get_token(&opts, pairs_sep))) {
1491 return AVERROR(ENOMEM);
1499 int av_opt_set_from_string(void *ctx, const char *opts,
1500 const char *const *shorthand,
1501 const char *key_val_sep, const char *pairs_sep)
1504 const char *dummy_shorthand = NULL;
1505 char *av_uninit(parsed_key), *av_uninit(value);
1511 shorthand = &dummy_shorthand;
1514 ret = av_opt_get_key_value(&opts, key_val_sep, pairs_sep,
1515 *shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
1516 &parsed_key, &value);
1518 if (ret == AVERROR(EINVAL))
1519 av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", opts);
1521 av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", opts,
1529 while (*shorthand) /* discard all remaining shorthand */
1532 key = *(shorthand++);
1535 av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
1536 if ((ret = av_opt_set(ctx, key, value, 0)) < 0) {
1537 if (ret == AVERROR_OPTION_NOT_FOUND)
1538 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
1540 av_free(parsed_key);
1545 av_free(parsed_key);
1551 void av_opt_free(void *obj)
1553 const AVOption *o = NULL;
1554 while ((o = av_opt_next(obj, o))) {
1556 case AV_OPT_TYPE_STRING:
1557 case AV_OPT_TYPE_BINARY:
1558 av_freep((uint8_t *)obj + o->offset);
1561 case AV_OPT_TYPE_DICT:
1562 av_dict_free((AVDictionary **)(((uint8_t *)obj) + o->offset));
1571 int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
1573 AVDictionaryEntry *t = NULL;
1574 AVDictionary *tmp = NULL;
1580 while ((t = av_dict_get(*options, "", t, AV_DICT_IGNORE_SUFFIX))) {
1581 ret = av_opt_set(obj, t->key, t->value, search_flags);
1582 if (ret == AVERROR_OPTION_NOT_FOUND)
1583 ret = av_dict_set(&tmp, t->key, t->value, 0);
1585 av_log(obj, AV_LOG_ERROR, "Error setting option %s to value %s.\n", t->key, t->value);
1591 av_dict_free(options);
1596 int av_opt_set_dict(void *obj, AVDictionary **options)
1598 return av_opt_set_dict2(obj, options, 0);
1601 const AVOption *av_opt_find(void *obj, const char *name, const char *unit,
1602 int opt_flags, int search_flags)
1604 return av_opt_find2(obj, name, unit, opt_flags, search_flags, NULL);
1607 const AVOption *av_opt_find2(void *obj, const char *name, const char *unit,
1608 int opt_flags, int search_flags, void **target_obj)
1611 const AVOption *o = NULL;
1621 if (search_flags & AV_OPT_SEARCH_CHILDREN) {
1622 if (search_flags & AV_OPT_SEARCH_FAKE_OBJ) {
1623 const AVClass *child = NULL;
1624 while (child = av_opt_child_class_next(c, child))
1625 if (o = av_opt_find2(&child, name, unit, opt_flags, search_flags, NULL))
1629 while (child = av_opt_child_next(obj, child))
1630 if (o = av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1635 while (o = av_opt_next(obj, o)) {
1636 if (!strcmp(o->name, name) && (o->flags & opt_flags) == opt_flags &&
1637 ((!unit && o->type != AV_OPT_TYPE_CONST) ||
1638 (unit && o->type == AV_OPT_TYPE_CONST && o->unit && !strcmp(o->unit, unit)))) {
1640 if (!(search_flags & AV_OPT_SEARCH_FAKE_OBJ))
1651 void *av_opt_child_next(void *obj, void *prev)
1653 const AVClass *c = *(AVClass **)obj;
1655 return c->child_next(obj, prev);
1659 const AVClass *av_opt_child_class_next(const AVClass *parent, const AVClass *prev)
1661 if (parent->child_class_next)
1662 return parent->child_class_next(prev);
1666 void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
1668 const AVOption *opt= av_opt_find2(&class, name, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
1671 return (uint8_t*)obj + opt->offset;
1674 static int opt_size(enum AVOptionType type)
1677 case AV_OPT_TYPE_BOOL:
1678 case AV_OPT_TYPE_INT:
1679 case AV_OPT_TYPE_FLAGS:
1681 case AV_OPT_TYPE_DURATION:
1682 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1683 case AV_OPT_TYPE_INT64:
1684 case AV_OPT_TYPE_UINT64:
1685 return sizeof(int64_t);
1686 case AV_OPT_TYPE_DOUBLE:
1687 return sizeof(double);
1688 case AV_OPT_TYPE_FLOAT:
1689 return sizeof(float);
1690 case AV_OPT_TYPE_STRING:
1691 return sizeof(uint8_t*);
1692 case AV_OPT_TYPE_VIDEO_RATE:
1693 case AV_OPT_TYPE_RATIONAL:
1694 return sizeof(AVRational);
1695 case AV_OPT_TYPE_BINARY:
1696 return sizeof(uint8_t*) + sizeof(int);
1697 case AV_OPT_TYPE_IMAGE_SIZE:
1698 return sizeof(int[2]);
1699 case AV_OPT_TYPE_PIXEL_FMT:
1700 return sizeof(enum AVPixelFormat);
1701 case AV_OPT_TYPE_SAMPLE_FMT:
1702 return sizeof(enum AVSampleFormat);
1703 case AV_OPT_TYPE_COLOR:
1706 return AVERROR(EINVAL);
1709 int av_opt_copy(void *dst, const void *src)
1711 const AVOption *o = NULL;
1716 return AVERROR(EINVAL);
1718 c = *(AVClass **)src;
1719 if (!c || c != *(AVClass **)dst)
1720 return AVERROR(EINVAL);
1722 while ((o = av_opt_next(src, o))) {
1723 void *field_dst = (uint8_t *)dst + o->offset;
1724 void *field_src = (uint8_t *)src + o->offset;
1725 uint8_t **field_dst8 = (uint8_t **)field_dst;
1726 uint8_t **field_src8 = (uint8_t **)field_src;
1728 if (o->type == AV_OPT_TYPE_STRING) {
1729 if (*field_dst8 != *field_src8)
1730 av_freep(field_dst8);
1731 *field_dst8 = av_strdup(*field_src8);
1732 if (*field_src8 && !*field_dst8)
1733 ret = AVERROR(ENOMEM);
1734 } else if (o->type == AV_OPT_TYPE_BINARY) {
1735 int len = *(int *)(field_src8 + 1);
1736 if (*field_dst8 != *field_src8)
1737 av_freep(field_dst8);
1738 *field_dst8 = av_memdup(*field_src8, len);
1739 if (len && !*field_dst8) {
1740 ret = AVERROR(ENOMEM);
1743 *(int *)(field_dst8 + 1) = len;
1744 } else if (o->type == AV_OPT_TYPE_CONST) {
1746 } else if (o->type == AV_OPT_TYPE_DICT) {
1747 AVDictionary **sdict = (AVDictionary **) field_src;
1748 AVDictionary **ddict = (AVDictionary **) field_dst;
1749 if (*sdict != *ddict)
1750 av_dict_free(ddict);
1752 av_dict_copy(ddict, *sdict, 0);
1753 if (av_dict_count(*sdict) != av_dict_count(*ddict))
1754 ret = AVERROR(ENOMEM);
1756 int size = opt_size(o->type);
1760 memcpy(field_dst, field_src, size);
1766 int av_opt_query_ranges(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1769 const AVClass *c = *(AVClass**)obj;
1770 int (*callback)(AVOptionRanges **, void *obj, const char *key, int flags) = NULL;
1772 if (c->version > (52 << 16 | 11 << 8))
1773 callback = c->query_ranges;
1776 callback = av_opt_query_ranges_default;
1778 ret = callback(ranges_arg, obj, key, flags);
1780 if (!(flags & AV_OPT_MULTI_COMPONENT_RANGE))
1782 (*ranges_arg)->nb_components = ret;
1787 int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const char *key, int flags)
1789 AVOptionRanges *ranges = av_mallocz(sizeof(*ranges));
1790 AVOptionRange **range_array = av_mallocz(sizeof(void*));
1791 AVOptionRange *range = av_mallocz(sizeof(*range));
1792 const AVOption *field = av_opt_find(obj, key, NULL, 0, flags);
1797 if (!ranges || !range || !range_array || !field) {
1798 ret = AVERROR(ENOMEM);
1802 ranges->range = range_array;
1803 ranges->range[0] = range;
1804 ranges->nb_ranges = 1;
1805 ranges->nb_components = 1;
1806 range->is_range = 1;
1807 range->value_min = field->min;
1808 range->value_max = field->max;
1810 switch (field->type) {
1811 case AV_OPT_TYPE_BOOL:
1812 case AV_OPT_TYPE_INT:
1813 case AV_OPT_TYPE_INT64:
1814 case AV_OPT_TYPE_UINT64:
1815 case AV_OPT_TYPE_PIXEL_FMT:
1816 case AV_OPT_TYPE_SAMPLE_FMT:
1817 case AV_OPT_TYPE_FLOAT:
1818 case AV_OPT_TYPE_DOUBLE:
1819 case AV_OPT_TYPE_DURATION:
1820 case AV_OPT_TYPE_COLOR:
1821 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1823 case AV_OPT_TYPE_STRING:
1824 range->component_min = 0;
1825 range->component_max = 0x10FFFF; // max unicode value
1826 range->value_min = -1;
1827 range->value_max = INT_MAX;
1829 case AV_OPT_TYPE_RATIONAL:
1830 range->component_min = INT_MIN;
1831 range->component_max = INT_MAX;
1833 case AV_OPT_TYPE_IMAGE_SIZE:
1834 range->component_min = 0;
1835 range->component_max = INT_MAX/128/8;
1836 range->value_min = 0;
1837 range->value_max = INT_MAX/8;
1839 case AV_OPT_TYPE_VIDEO_RATE:
1840 range->component_min = 1;
1841 range->component_max = INT_MAX;
1842 range->value_min = 1;
1843 range->value_max = INT_MAX;
1846 ret = AVERROR(ENOSYS);
1850 *ranges_arg = ranges;
1855 av_free(range_array);
1859 void av_opt_freep_ranges(AVOptionRanges **rangesp)
1862 AVOptionRanges *ranges = *rangesp;
1867 for (i = 0; i < ranges->nb_ranges * ranges->nb_components; i++) {
1868 AVOptionRange *range = ranges->range[i];
1870 av_freep(&range->str);
1871 av_freep(&ranges->range[i]);
1874 av_freep(&ranges->range);
1878 int av_opt_is_set_to_default(void *obj, const AVOption *o)
1889 return AVERROR(EINVAL);
1891 dst = ((uint8_t*)obj) + o->offset;
1894 case AV_OPT_TYPE_CONST:
1896 case AV_OPT_TYPE_BOOL:
1897 case AV_OPT_TYPE_FLAGS:
1898 case AV_OPT_TYPE_PIXEL_FMT:
1899 case AV_OPT_TYPE_SAMPLE_FMT:
1900 case AV_OPT_TYPE_INT:
1901 case AV_OPT_TYPE_CHANNEL_LAYOUT:
1902 case AV_OPT_TYPE_DURATION:
1903 case AV_OPT_TYPE_INT64:
1904 case AV_OPT_TYPE_UINT64:
1905 read_number(o, dst, NULL, NULL, &i64);
1906 return o->default_val.i64 == i64;
1907 case AV_OPT_TYPE_STRING:
1908 str = *(char **)dst;
1909 if (str == o->default_val.str) //2 NULLs
1911 if (!str || !o->default_val.str) //1 NULL
1913 return !strcmp(str, o->default_val.str);
1914 case AV_OPT_TYPE_DOUBLE:
1915 read_number(o, dst, &d, NULL, NULL);
1916 return o->default_val.dbl == d;
1917 case AV_OPT_TYPE_FLOAT:
1918 read_number(o, dst, &d, NULL, NULL);
1919 f = o->default_val.dbl;
1922 case AV_OPT_TYPE_RATIONAL:
1923 q = av_d2q(o->default_val.dbl, INT_MAX);
1924 return !av_cmp_q(*(AVRational*)dst, q);
1925 case AV_OPT_TYPE_BINARY: {
1930 int opt_size = *(int *)((void **)dst + 1);
1931 void *opt_ptr = *(void **)dst;
1932 if (!opt_size && (!o->default_val.str || !strlen(o->default_val.str)))
1934 if (!opt_size || !o->default_val.str || !strlen(o->default_val.str ))
1936 if (opt_size != strlen(o->default_val.str) / 2)
1938 ret = set_string_binary(NULL, NULL, o->default_val.str, &tmp.data);
1940 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1944 case AV_OPT_TYPE_DICT:
1945 /* Binary and dict have not default support yet. Any pointer is not default. */
1946 return !!(*(void **)dst);
1947 case AV_OPT_TYPE_IMAGE_SIZE:
1948 if (!o->default_val.str || !strcmp(o->default_val.str, "none"))
1950 else if ((ret = av_parse_video_size(&w, &h, o->default_val.str)) < 0)
1952 return (w == *(int *)dst) && (h == *((int *)dst+1));
1953 case AV_OPT_TYPE_VIDEO_RATE:
1954 q = (AVRational){0, 0};
1955 if (o->default_val.str) {
1956 if ((ret = av_parse_video_rate(&q, o->default_val.str)) < 0)
1959 return !av_cmp_q(*(AVRational*)dst, q);
1960 case AV_OPT_TYPE_COLOR: {
1961 uint8_t color[4] = {0, 0, 0, 0};
1962 if (o->default_val.str) {
1963 if ((ret = av_parse_color(color, o->default_val.str, -1, NULL)) < 0)
1966 return !memcmp(color, dst, sizeof(color));
1969 av_log(obj, AV_LOG_WARNING, "Not supported option type: %d, option name: %s\n", o->type, o->name);
1972 return AVERROR_PATCHWELCOME;
1975 int av_opt_is_set_to_default_by_name(void *obj, const char *name, int search_flags)
1980 return AVERROR(EINVAL);
1981 o = av_opt_find2(obj, name, NULL, 0, search_flags, &target);
1983 return AVERROR_OPTION_NOT_FOUND;
1984 return av_opt_is_set_to_default(target, o);
1987 int av_opt_serialize(void *obj, int opt_flags, int flags, char **buffer,
1988 const char key_val_sep, const char pairs_sep)
1990 const AVOption *o = NULL;
1994 const char special_chars[] = {pairs_sep, key_val_sep, '\0'};
1996 if (pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
1997 pairs_sep == '\\' || key_val_sep == '\\') {
1998 av_log(obj, AV_LOG_ERROR, "Invalid separator(s) found.");
1999 return AVERROR(EINVAL);
2002 if (!obj || !buffer)
2003 return AVERROR(EINVAL);
2006 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
2008 while (o = av_opt_next(obj, o)) {
2009 if (o->type == AV_OPT_TYPE_CONST)
2011 if ((flags & AV_OPT_SERIALIZE_OPT_FLAGS_EXACT) && o->flags != opt_flags)
2013 else if (((o->flags & opt_flags) != opt_flags))
2015 if (flags & AV_OPT_SERIALIZE_SKIP_DEFAULTS && av_opt_is_set_to_default(obj, o) > 0)
2017 if ((ret = av_opt_get(obj, o->name, 0, &buf)) < 0) {
2018 av_bprint_finalize(&bprint, NULL);
2023 av_bprint_append_data(&bprint, &pairs_sep, 1);
2024 av_bprint_escape(&bprint, o->name, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2025 av_bprint_append_data(&bprint, &key_val_sep, 1);
2026 av_bprint_escape(&bprint, buf, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
2030 av_bprint_finalize(&bprint, buffer);