44 #if FF_API_OLD_AVOPTIONS
57 if (!last &&
class &&
class->option &&
class->option[0].name)
59 if (last && last[1].
name)
87 (o->
max * den < num * intnum || o->
min * den > num * intnum)) {
93 double d = num*intnum/den;
94 if (d < -1.5 || d > 0xFFFFFFFF+0.5 || (
llrint(d*256) & 255)) {
96 "Value %f for parameter '%s' is not a valid set of 32bit integer flags\n",
97 num*intnum/den, o->
name);
123 if (c >=
'0' && c <=
'9')
return c -
'0';
124 if (c >=
'a' && c <=
'f')
return c -
'a' + 10;
125 if (c >=
'A' && c <=
'F')
return c -
'A' + 10;
131 int *lendst = (
int *)(dst + 1);
138 if (!val || !(len = strlen(val)))
151 if (a < 0 || b < 0) {
155 *ptr++ = (a << 4) | b;
167 return *dst ? 0 :
AVERROR(ENOMEM);
170 #define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
171 opt->type == AV_OPT_TYPE_CONST || \
172 opt->type == AV_OPT_TYPE_FLAGS || \
173 opt->type == AV_OPT_TYPE_INT) ? \
174 opt->default_val.i64 : opt->default_val.dbl)
182 if (sscanf(val,
"%d%*1[:/]%d%c", &num, &den, &c) == 2) {
183 if ((ret =
write_number(obj, o, dst, 1, den, num)) >= 0)
196 if (*val ==
'+' || *val ==
'-')
198 for (; i <
sizeof(
buf) - 1 && val[i] && val[i] !=
'+' && val[i] !=
'-'; i++)
207 double const_values[64];
208 const char * const_names[64];
221 const_names [ci ] = o_named->
name;
226 const_names [ci ] =
"default";
228 const_names [ci ] =
"max";
229 const_values[ci++] = o->
max;
230 const_names [ci ] =
"min";
231 const_values[ci++] = o->
min;
232 const_names [ci ] =
"none";
233 const_values[ci++] = 0;
234 const_names [ci ] =
"all";
235 const_values[ci++] = ~0;
236 const_names [ci] =
NULL;
237 const_values[ci] = 0;
249 if (cmd ==
'+') d = intnum | (int64_t)d;
250 else if (cmd ==
'-') d = intnum &~(int64_t)d;
267 if (!val || !strcmp(val,
"none")) {
274 av_log(obj,
AV_LOG_ERROR,
"Unable to parse option value \"%s\" as image size\n", val);
287 av_log(obj,
AV_LOG_ERROR,
"Unable to parse option value \"%s\" as video rate\n", val);
307 int fmt_nb,
int ((*get_fmt)(
const char *)),
const char *desc)
311 if (!val || !strcmp(val,
"none")) {
317 fmt = strtol(val, &tail, 0);
318 if (*tail || (
unsigned)fmt >= fmt_nb) {
320 "Unable to parse option value \"%s\" as %s\n", val, desc);
330 if(min == 0 && max == 0) {
335 if (fmt < min || fmt > max) {
337 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
338 fmt, o->
name, desc, min, max);
358 #if FF_API_OLD_AVOPTIONS
371 void *dst, *target_obj;
373 if (!o || !target_obj)
411 if (!val || !strcmp(val,
"none")) {
414 #if FF_API_GET_CHANNEL_LAYOUT_COMPAT
420 av_log(obj,
AV_LOG_ERROR,
"Unable to parse option value \"%s\" as channel layout\n", val);
423 *(int64_t *)dst = cl;
433 #define OPT_EVAL_NUMBER(name, opttype, vartype)\
434 int av_opt_eval_ ## name(void *obj, const AVOption *o, const char *val, vartype *name ## _out)\
436 if (!o || o->type != opttype || o->flags & AV_OPT_FLAG_READONLY)\
437 return AVERROR(EINVAL);\
438 return set_string_number(obj, obj, o, val, name ## _out);\
448 static
int set_number(
void *obj, const
char *
name,
double num,
int den, int64_t intnum,
451 void *dst, *target_obj;
454 if (!o || !target_obj)
464 #if FF_API_OLD_AVOPTIONS
492 return set_number(obj, name, 1, 1, val, search_flags);
497 return set_number(obj, name, val, 1, 1, search_flags);
513 if (!o || !target_obj)
524 lendst = (
int *)(dst + 1);
530 memcpy(ptr, val, len);
540 if (!o || !target_obj)
544 "The value set by option '%s' is not an image size.\n", o->
name);
549 "Invalid negative size value %dx%d for size '%s'\n", w, h, o->
name);
553 *(
int *)(((
uint8_t *)target_obj+
sizeof(int)) + o->
offset) = h;
562 if (!o || !target_obj)
566 "The value set by option '%s' is not a video rate.\n", o->
name);
569 if (val.
num <= 0 || val.
den <= 0)
579 search_flags, &target_obj);
582 if (!o || !target_obj)
584 if (o->
type != type) {
586 "The value set by option '%s' is not a %s format", name, desc);
593 if (fmt < min || fmt > max) {
595 "Value %d for parameter '%s' out of %s format range [%d - %d]\n",
596 fmt, name, desc, min, max);
618 if (!o || !target_obj)
622 "The value set by option '%s' is not a channel layout.\n", o->
name);
629 #if FF_API_OLD_AVOPTIONS
647 if (o_out) *o_out= o;
660 if (len >= (buf_len + 1)/2)
return NULL;
662 for (i = 0; i <
len; i++)
snprintf(buf + i*2, 3,
"%02X", bin[i]);
664 default:
return NULL;
676 if (!o || !target_obj)
690 void *dst, *target_obj;
716 return *out_val ? 0 :
AVERROR(ENOMEM);
719 if ((uint64_t)len*2 + 1 > INT_MAX)
728 for (i = 0; i <
len; i++)
729 snprintf(*out_val + i*2, 3,
"%02X", bin[i]);
732 ret =
snprintf(buf,
sizeof(buf),
"%dx%d", ((
int *)dst)[0], ((
int *)dst)[1]);
741 i64 = *(int64_t *)dst;
742 ret =
snprintf(buf,
sizeof(buf),
"%"PRIi64
":%02d:%02d.%06d",
743 i64 / 3600000000, (
int)((i64 / 60000000) % 60),
744 (
int)((i64 / 1000000) % 60), (
int)(i64 % 1000000));
747 ret =
snprintf(buf,
sizeof(buf),
"0x%02x%02x%02x%02x",
752 i64 = *(int64_t *)dst;
753 ret =
snprintf(buf,
sizeof(buf),
"0x%"PRIx64, i64);
759 if (ret >=
sizeof(buf))
762 return *out_val ? 0 :
AVERROR(ENOMEM);
768 void *dst, *target_obj;
770 if (!o || !target_obj)
775 if (o_out) *o_out= o;
784 #if FF_API_OLD_AVOPTIONS
791 if (
get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
793 return num*intnum/den;
802 if (
get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
804 if (num == 1.0 && (
int)intnum == intnum)
807 return av_d2q(num*intnum/den, 1<<24);
816 if (
get_number(obj, name, o_out, &num, &den, &intnum, 0) < 0)
818 return num*intnum/den;
828 if ((ret =
get_number(obj, name,
NULL, &num, &den, &intnum, search_flags)) < 0)
830 *out_val = num*intnum/den;
840 if ((ret =
get_number(obj, name,
NULL, &num, &den, &intnum, search_flags)) < 0)
842 *out_val = num*intnum/den;
852 if ((ret =
get_number(obj, name,
NULL, &num, &den, &intnum, search_flags)) < 0)
855 if (num == 1.0 && (
int)intnum == intnum)
858 *out_val =
av_d2q(num*intnum/den, 1<<24);
864 void *dst, *target_obj;
866 if (!o || !target_obj)
870 "The value for option '%s' is not an image size.\n", name);
875 if (w_out) *w_out = *(
int *)dst;
876 if (h_out) *h_out = *((
int *)dst+1);
886 if ((ret =
get_number(obj, name,
NULL, &num, &den, &intnum, search_flags)) < 0)
889 if (num == 1.0 && (
int)intnum == intnum)
892 *out_val =
av_d2q(num*intnum/den, 1<<24);
896 static int get_format(
void *obj,
const char *
name,
int search_flags,
int *out_fmt,
899 void *dst, *target_obj;
901 if (!o || !target_obj)
903 if (o->
type != type) {
905 "The value for option '%s' is not a %s format.\n", desc, name);
910 *out_fmt = *(
int *)dst;
926 void *dst, *target_obj;
928 if (!o || !target_obj)
932 "The value for option '%s' is not a channel layout.\n", name);
937 *cl = *(int64_t *)dst;
947 if (!o || !target_obj)
974 av_log(av_log_obj, level,
"INT_MAX");
975 }
else if (d == INT_MIN) {
976 av_log(av_log_obj, level,
"INT_MIN");
977 }
else if (d == UINT32_MAX) {
978 av_log(av_log_obj, level,
"UINT32_MAX");
979 }
else if (d == (
double)INT64_MAX) {
980 av_log(av_log_obj, level,
"I64_MAX");
981 }
else if (d == INT64_MIN) {
982 av_log(av_log_obj, level,
"I64_MIN");
983 }
else if (d == FLT_MAX) {
984 av_log(av_log_obj, level,
"FLT_MAX");
985 }
else if (d == FLT_MIN) {
986 av_log(av_log_obj, level,
"FLT_MIN");
987 }
else if (d == -FLT_MAX) {
988 av_log(av_log_obj, level,
"-FLT_MAX");
989 }
else if (d == -FLT_MIN) {
990 av_log(av_log_obj, level,
"-FLT_MIN");
991 }
else if (d == DBL_MAX) {
992 av_log(av_log_obj, level,
"DBL_MAX");
993 }
else if (d == DBL_MIN) {
994 av_log(av_log_obj, level,
"DBL_MIN");
995 }
else if (d == -DBL_MAX) {
996 av_log(av_log_obj, level,
"-DBL_MAX");
997 }
else if (d == -DBL_MIN) {
998 av_log(av_log_obj, level,
"-DBL_MIN");
1000 av_log(av_log_obj, level,
"%g", d);
1004 static void opt_list(
void *obj,
void *av_log_obj,
const char *unit,
1005 int req_flags,
int rej_flags)
1012 if (!(opt->
flags & req_flags) || (opt->
flags & rej_flags))
1032 switch (opt->
type) {
1096 switch (opt->
type) {
1122 switch (opt->
type) {
1160 opt_list(obj, av_log_obj, opt->
unit, req_flags, rej_flags);
1165 int av_opt_show2(
void *obj,
void *av_log_obj,
int req_flags,
int rej_flags)
1179 #if FF_API_OLD_AVOPTIONS
1189 #if FF_API_OLD_AVOPTIONS
1197 switch (opt->
type) {
1269 const char *key_val_sep,
const char *pairs_sep)
1278 if (*key && strspn(*buf, key_val_sep)) {
1286 av_log(ctx,
AV_LOG_ERROR,
"Missing key or no key/value separator found after key '%s'\n", key);
1303 const char *key_val_sep,
const char *pairs_sep)
1322 #define WHITESPACES " \n\t"
1326 return (
unsigned)((c | 32) -
'a') < 26 ||
1327 (unsigned)(c -
'0') < 10 ||
1328 c ==
'-' || c ==
'_' || c ==
'/' || c ==
'.';
1339 static int get_key(
const char **ropts,
const char *delim,
char **rkey)
1341 const char *opts = *ropts;
1342 const char *key_start, *key_end;
1349 if (!*opts || !strchr(delim, *opts))
1352 if (!(*rkey =
av_malloc(key_end - key_start + 1)))
1354 memcpy(*rkey, key_start, key_end - key_start);
1355 (*rkey)[key_end - key_start] = 0;
1361 const char *key_val_sep,
const char *pairs_sep,
1363 char **rkey,
char **rval)
1367 const char *opts = *ropts;
1369 if ((ret =
get_key(&opts, key_val_sep, &key)) < 0 &&
1383 const char *
const *shorthand,
1384 const char *key_val_sep,
const char *pairs_sep)
1387 const char *dummy_shorthand =
NULL;
1394 shorthand = &dummy_shorthand;
1399 &parsed_key, &
value);
1415 key = *(shorthand++);
1484 int opt_flags,
int search_flags)
1490 int opt_flags,
int search_flags,
void **target_obj)
1512 if (o =
av_opt_find2(child, name, unit, opt_flags, search_flags, target_obj))
1518 if (!strcmp(o->
name, name) && (o->
flags & opt_flags) == opt_flags &&
1598 if (*field_dst8 != *field_src8)
1601 if (*field_src8 && !*field_dst8)
1604 int len = *(
int*)(field_src8 + 1);
1605 if (*field_dst8 != *field_src8)
1607 *field_dst8 =
av_memdup(*field_src8, len);
1608 if (len && !*field_dst8) {
1612 *(
int*)(field_dst8 + 1) =
len;
1618 if (*sdict != *ddict)
1637 if (c->
version > (52 << 16 | 11 << 8))
1643 ret =
callback(ranges_arg, obj, key, flags);
1647 (*ranges_arg)->nb_components =
ret;
1662 if (!ranges || !range || !range_array || !field) {
1667 ranges->
range = range_array;
1668 ranges->
range[0] = range;
1675 switch (field->
type) {
1713 *ranges_arg = ranges;
1769 str = *(
char **)dst;
1791 int opt_size = *(
int *)((
void **)dst + 1);
1792 void *opt_ptr = *(
void **)dst;
1801 ret = !memcmp(opt_ptr, tmp.data, tmp.size);
1807 return !!(*(
void **)dst);
1813 return (w == *(
int *)dst) && (h == *((
int *)dst+1));
1827 return !memcmp(color, dst,
sizeof(color));
1849 const char key_val_sep,
const char pairs_sep)
1855 const char special_chars[] = {pairs_sep, key_val_sep,
'\0'};
1857 if (pairs_sep ==
'\0' || key_val_sep ==
'\0' || pairs_sep == key_val_sep ||
1858 pairs_sep ==
'\\' || key_val_sep ==
'\\') {
1863 if (!obj || !buffer)
1874 else if (((o->
flags & opt_flags) != opt_flags))
1897 typedef struct TestContext
1911 int64_t channel_layout;
1924 #define OFFSET(x) offsetof(TestContext, x)
1926 #define TEST_FLAG_COOL 01
1927 #define TEST_FLAG_LAME 02
1928 #define TEST_FLAG_MU 04
1930 static const AVOption test_options[]= {
1937 {
"cool",
"set cool flag", 0,
AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_COOL}, INT_MIN, INT_MAX, 1,
"flags" },
1938 {
"lame",
"set lame flag", 0,
AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_LAME}, INT_MIN, INT_MAX, 1,
"flags" },
1939 {
"mu",
"set mu flag", 0,
AV_OPT_TYPE_CONST, {.i64 = TEST_FLAG_MU}, INT_MIN, INT_MAX, 1,
"flags" },
1956 static const char *test_get_name(
void *ctx)
1961 static const AVClass test_class = {
1969 vfprintf(stdout, fmt, vl);
1979 printf(
"Testing default values\n");
1981 TestContext test_ctx = { 0 };
1982 test_ctx.class = &test_class;
1985 printf(
"num=%d\n", test_ctx.num);
1986 printf(
"toggle=%d\n", test_ctx.toggle);
1987 printf(
"string=%s\n", test_ctx.string);
1988 printf(
"escape=%s\n", test_ctx.escape);
1989 printf(
"flags=%d\n", test_ctx.flags);
1990 printf(
"rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den);
1991 printf(
"video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den);
1992 printf(
"width=%d height=%d\n", test_ctx.w, test_ctx.h);
1995 printf(
"duration=%"PRId64
"\n", test_ctx.duration);
1996 printf(
"color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]);
1997 printf(
"channel_layout=%"PRId64
"=%"PRId64
"\n", test_ctx.channel_layout, (int64_t)
AV_CH_LAYOUT_HEXAGONAL);
1998 if (test_ctx.binary)
1999 printf(
"binary=%x %x %x %x\n", ((
uint8_t*)test_ctx.binary)[0], ((
uint8_t*)test_ctx.binary)[1], ((
uint8_t*)test_ctx.binary)[2], ((
uint8_t*)test_ctx.binary)[3]);
2000 printf(
"binary_size=%d\n", test_ctx.binary_size);
2001 printf(
"num64=%"PRId64
"\n", test_ctx.num64);
2002 printf(
"flt=%.6f\n", test_ctx.flt);
2003 printf(
"dbl=%.6f\n", test_ctx.dbl);
2010 printf(
"\nTesting av_opt_is_set_to_default()\n");
2013 TestContext test_ctx = { 0 };
2015 test_ctx.class = &test_class;
2021 printf(
"name:%10s default:%d error:%s\n", o->
name, !!ret, ret < 0 ?
av_err2str(ret) :
"");
2026 printf(
"name:%10s default:%d error:%s\n", o->
name, !!ret, ret < 0 ?
av_err2str(ret) :
"");
2031 printf(
"\nTest av_opt_serialize()\n");
2033 TestContext test_ctx = { 0 };
2035 test_ctx.class = &test_class;
2041 printf(
"%s\n", buf);
2043 memset(&test_ctx, 0,
sizeof(test_ctx));
2044 test_ctx.class = &test_class;
2048 printf(
"%s\n", buf);
2055 printf(
"\nTesting av_set_options_string()\n");
2057 TestContext test_ctx = { 0 };
2058 static const char *
const options[] = {
2074 "flags=+mu-lame : num=42: toggle=0",
2075 "num=42 : string=blahblah",
2076 "rational=0 : rational=1/2 : rational=1/-1",
2089 "video_rate=30000/1001",
2090 "video_rate=30/1.001",
2094 "duration=1\\:23\\:45.67",
2098 "cl=stereo+downmix",
2120 test_ctx.class = &test_class;
2126 int silence_log = !strcmp(options[i],
"rational=-1/0");
2131 printf(
"Error '%s'\n", options[i]);
2133 printf(
"OK '%s'\n", options[i]);
2139 printf(
"\nTesting av_opt_set_from_string()\n");
2141 TestContext test_ctx = { 0 };
2142 static const char *
const options[] = {
2150 " 5 : hello : size = pal ",
2151 "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42"
2153 static const char *
const shorthand[] = {
"num",
"string",
NULL };
2155 test_ctx.class = &test_class;
2163 printf(
"Error '%s'\n", options[i]);
2165 printf(
"OK '%s'\n", options[i]);