1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avutil/opt: add an unsigned option type

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2024-02-27 17:30:40 -03:00
parent 9902fc550a
commit d053290d8d
6 changed files with 63 additions and 4 deletions

View File

@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07
API changes, most recent first:
2024-05-04 - xxxxxxxxxx - lavu 59.17.100 - opt.h
Add AV_OPT_TYPE_UINT and av_opt_eval_uint().
2024-04-24 - 8616cfe0890 - lavu 59.16.100 - opt.h
Add AV_OPT_SERIALIZE_SEARCH_CHILDREN.

View File

@ -62,6 +62,7 @@ static const size_t opt_elem_size[] = {
[AV_OPT_TYPE_FLAGS] = sizeof(unsigned),
[AV_OPT_TYPE_INT] = sizeof(int),
[AV_OPT_TYPE_INT64] = sizeof(int64_t),
[AV_OPT_TYPE_UINT] = sizeof(unsigned),
[AV_OPT_TYPE_UINT64] = sizeof(uint64_t),
[AV_OPT_TYPE_DOUBLE] = sizeof(double),
[AV_OPT_TYPE_FLOAT] = sizeof(float),
@ -166,6 +167,9 @@ static int read_number(const AVOption *o, const void *dst, double *num, int *den
case AV_OPT_TYPE_INT:
*intnum = *(int *)dst;
return 0;
case AV_OPT_TYPE_UINT:
*intnum = *(unsigned int *)dst;
return 0;
case AV_OPT_TYPE_DURATION:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_UINT64:
@ -219,6 +223,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int
case AV_OPT_TYPE_BOOL:
case AV_OPT_TYPE_FLAGS:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT:
*(int *)dst = llrint(num / den) * intnum;
break;
case AV_OPT_TYPE_DURATION:
@ -319,6 +324,7 @@ static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **d
opt->type == AV_OPT_TYPE_UINT64 || \
opt->type == AV_OPT_TYPE_CONST || \
opt->type == AV_OPT_TYPE_FLAGS || \
opt->type == AV_OPT_TYPE_UINT || \
opt->type == AV_OPT_TYPE_INT) \
? opt->default_val.i64 \
: opt->default_val.dbl)
@ -605,6 +611,7 @@ static int opt_set_elem(void *obj, void *target_obj, const AVOption *o,
return set_string_binary(obj, o, val, dst);
case AV_OPT_TYPE_FLAGS:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_UINT64:
case AV_OPT_TYPE_FLOAT:
@ -767,6 +774,7 @@ int av_opt_eval_ ## name(void *obj, const AVOption *o, \
OPT_EVAL_NUMBER(flags, AV_OPT_TYPE_FLAGS, int)
OPT_EVAL_NUMBER(int, AV_OPT_TYPE_INT, int)
OPT_EVAL_NUMBER(uint, AV_OPT_TYPE_UINT, unsigned)
OPT_EVAL_NUMBER(int64, AV_OPT_TYPE_INT64, int64_t)
OPT_EVAL_NUMBER(float, AV_OPT_TYPE_FLOAT, float)
OPT_EVAL_NUMBER(double, AV_OPT_TYPE_DOUBLE, double)
@ -997,6 +1005,9 @@ static int opt_get_elem(const AVOption *o, uint8_t **pbuf, size_t buf_len,
case AV_OPT_TYPE_INT:
ret = snprintf(*pbuf, buf_len, "%d", *(int *)dst);
break;
case AV_OPT_TYPE_UINT:
ret = snprintf(*pbuf, buf_len, "%u", *(unsigned *)dst);
break;
case AV_OPT_TYPE_INT64:
ret = snprintf(*pbuf, buf_len, "%"PRId64, *(int64_t *)dst);
break;
@ -1444,6 +1455,7 @@ static void log_type(void *av_log_obj, const AVOption *o,
[AV_OPT_TYPE_FLAGS] = "<flags>",
[AV_OPT_TYPE_INT] = "<int>",
[AV_OPT_TYPE_INT64] = "<int64>",
[AV_OPT_TYPE_UINT] = "<unsigned>",
[AV_OPT_TYPE_UINT64] = "<uint64>",
[AV_OPT_TYPE_DOUBLE] = "<double>",
[AV_OPT_TYPE_FLOAT] = "<float>",
@ -1515,6 +1527,7 @@ static void log_default(void *obj, void *av_log_obj, const AVOption *opt)
av_log(av_log_obj, AV_LOG_INFO, "%s", buf);
break;
}
case AV_OPT_TYPE_UINT:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT64:
case AV_OPT_TYPE_INT64: {
@ -1600,6 +1613,7 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit,
if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 0) {
switch (opt->type) {
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_UINT64:
case AV_OPT_TYPE_DOUBLE:
@ -1676,6 +1690,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags)
case AV_OPT_TYPE_BOOL:
case AV_OPT_TYPE_FLAGS:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_UINT64:
case AV_OPT_TYPE_DURATION:
@ -2177,6 +2192,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch
switch (field->type) {
case AV_OPT_TYPE_BOOL:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_UINT64:
case AV_OPT_TYPE_PIXEL_FMT:
@ -2281,6 +2297,7 @@ int av_opt_is_set_to_default(void *obj, const AVOption *o)
case AV_OPT_TYPE_PIXEL_FMT:
case AV_OPT_TYPE_SAMPLE_FMT:
case AV_OPT_TYPE_INT:
case AV_OPT_TYPE_UINT:
case AV_OPT_TYPE_DURATION:
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_UINT64:

View File

@ -260,6 +260,7 @@ enum AVOptionType{
AV_OPT_TYPE_COLOR,
AV_OPT_TYPE_BOOL,
AV_OPT_TYPE_CHLAYOUT,
AV_OPT_TYPE_UINT,
/**
* May be combined with another regular option type to declare an array
@ -883,6 +884,7 @@ int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDiction
*/
int av_opt_eval_flags (void *obj, const AVOption *o, const char *val, int *flags_out);
int av_opt_eval_int (void *obj, const AVOption *o, const char *val, int *int_out);
int av_opt_eval_uint (void *obj, const AVOption *o, const char *val, unsigned *uint_out);
int av_opt_eval_int64 (void *obj, const AVOption *o, const char *val, int64_t *int64_out);
int av_opt_eval_float (void *obj, const AVOption *o, const char *val, float *float_out);
int av_opt_eval_double(void *obj, const AVOption *o, const char *val, double *double_out);

View File

@ -32,6 +32,7 @@ typedef struct TestContext {
const AVClass *class;
struct ChildContext *child;
int num;
int unum;
int toggle;
char *string;
int flags;
@ -87,6 +88,7 @@ static const AVOptionArrayDef array_dict = {
static const AVOption test_options[]= {
{"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 100, 1 },
{"unum", "set unum", OFFSET(unum), AV_OPT_TYPE_UINT, { .i64 = 1U << 31 }, 0, 1U << 31, 1 },
{"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 },
{"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 },
{"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 },
@ -186,6 +188,7 @@ int main(void)
av_opt_set_defaults(&test_ctx);
printf("num=%d\n", test_ctx.num);
printf("unum=%u\n", test_ctx.unum);
printf("toggle=%d\n", test_ctx.toggle);
printf("string=%s\n", test_ctx.string);
printf("escape=%s\n", test_ctx.escape);
@ -386,6 +389,12 @@ int main(void)
"num=-1",
"num=-2",
"num=101",
"unum=bogus",
"unum=44",
"unum=44.4",
"unum=-1",
"unum=2147483648",
"unum=2147483649",
"num64=bogus",
"num64=44",
"num64=44.4",

View File

@ -79,8 +79,8 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 59
#define LIBAVUTIL_VERSION_MINOR 16
#define LIBAVUTIL_VERSION_MICRO 101
#define LIBAVUTIL_VERSION_MINOR 17
#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \

View File

@ -1,5 +1,6 @@
Testing default values
num=0
unum=2147483648
toggle=1
string=default
escape=\=,
@ -25,6 +26,7 @@ array_dict[0]: k01 v,01
array_dict[1]: k10 v=1:0
TestContext AVOptions:
-num <int> E.......... set num (from -1 to 100) (default 0)
-unum <unsigned> E.......... set unum (from 0 to 2.14748e+09) (default 2147483648)
-toggle <int> E.......... set toggle (from 0 to 1) (default 1)
-rational <rational> E.......... set rational (from 0 to 10) (default 1/1)
-string <string> E.......... set string (default "default")
@ -57,6 +59,7 @@ TestContext AVOptions:
Testing av_opt_is_set_to_default()
name: num default:1 error:
name: unum default:0 error:
name: toggle default:0 error:
name: rational default:0 error:
name: string default:0 error:
@ -87,6 +90,7 @@ name: array_int default:0 error:
name: array_str default:0 error:
name:array_dict default:0 error:
name: num default:1 error:
name: unum default:1 error:
name: toggle default:1 error:
name: rational default:1 error:
name: string default:1 error:
@ -119,6 +123,7 @@ name:array_dict default:1 error:
Testing av_opt_get/av_opt_set()
name: num get: 0 set: OK get: 0 OK
name: unum get: 2147483648 set: OK get: 2147483648 OK
name: toggle get: 1 set: OK get: 1 OK
name: rational get: 1/1 set: OK get: 1/1 OK
name: string get: default set: OK get: default OK
@ -150,8 +155,9 @@ array_dict=NULL; nb_array_dict=0
av_opt_get("array_dict") -> NULL
Test av_opt_serialize()
num=0,toggle=1,rational=1/1,string=default,escape=\\\=\,,flags=0x00000001,size=200x300,pix_fmt=0bgr,sample_fmt=s16,video_rate=25/1,duration=0.001,color=0xffc0cbff,cl=hexagonal,bin=62696E00,bin1=,bin2=,num64=4294967296,flt=0.333333,dbl=0.333333,bool1=auto,bool2=true,bool3=false,dict1=,dict2=happy\=\\:-),array_int=,array_str=str0|str\\|1|str\\\\2,array_dict=k00\=v\\\\\\\\00:k01\=v\\\,01\,k10\=v\\\\\=1\\\\:0
num=0,unum=2147483648,toggle=1,rational=1/1,string=default,escape=\\\=\,,flags=0x00000001,size=200x300,pix_fmt=0bgr,sample_fmt=s16,video_rate=25/1,duration=0.001,color=0xffc0cbff,cl=hexagonal,bin=62696E00,bin1=,bin2=,num64=4294967296,flt=0.333333,dbl=0.333333,bool1=auto,bool2=true,bool3=false,dict1=,dict2=happy\=\\:-),array_int=,array_str=str0|str\\|1|str\\\\2,array_dict=k00\=v\\\\\\\\00:k01\=v\\\,01\,k10\=v\\\\\=1\\\\:0
Setting entry with key 'num' to value '0'
Setting entry with key 'unum' to value '2147483648'
Setting entry with key 'toggle' to value '1'
Setting entry with key 'rational' to value '1/1'
Setting entry with key 'string' to value 'default'
@ -178,7 +184,7 @@ Setting entry with key 'dict2' to value 'happy=\:-)'
Setting entry with key 'array_int' to value ''
Setting entry with key 'array_str' to value 'str0|str\|1|str\\2'
Setting entry with key 'array_dict' to value 'k00=v\\\\00:k01=v\,01,k10=v\\=1\\:0'
num=0,toggle=1,rational=1/1,string=default,escape=\\\=\,,flags=0x00000001,size=200x300,pix_fmt=0bgr,sample_fmt=s16,video_rate=25/1,duration=0.001,color=0xffc0cbff,cl=hexagonal,bin=62696E00,bin1=,bin2=,num64=4294967296,flt=0.333333,dbl=0.333333,bool1=auto,bool2=true,bool3=false,dict1=,dict2=happy\=\\:-),array_int=,array_str=str0|str\\|1|str\\\\2,array_dict=k00\=v\\\\\\\\00:k01\=v\\\,01\,k10\=v\\\\\=1\\\\:0
num=0,unum=2147483648,toggle=1,rational=1/1,string=default,escape=\\\=\,,flags=0x00000001,size=200x300,pix_fmt=0bgr,sample_fmt=s16,video_rate=25/1,duration=0.001,color=0xffc0cbff,cl=hexagonal,bin=62696E00,bin1=,bin2=,num64=4294967296,flt=0.333333,dbl=0.333333,bool1=auto,bool2=true,bool3=false,dict1=,dict2=happy\=\\:-),array_int=,array_str=str0|str\\|1|str\\\\2,array_dict=k00\=v\\\\\\\\00:k01\=v\\\,01\,k10\=v\\\\\=1\\\\:0
child_num=0,flt=0.333333,dbl=0.333333,array_int=
Testing av_set_options_string()
@ -359,6 +365,28 @@ Setting options string 'num=101'
Setting entry with key 'num' to value '101'
Value 101.000000 for parameter 'num' out of range [-1 - 100]
Error 'num=101'
Setting options string 'unum=bogus'
Setting entry with key 'unum' to value 'bogus'
Undefined constant or missing '(' in 'bogus'
Unable to parse option value "bogus"
Error 'unum=bogus'
Setting options string 'unum=44'
Setting entry with key 'unum' to value '44'
OK 'unum=44'
Setting options string 'unum=44.4'
Setting entry with key 'unum' to value '44.4'
OK 'unum=44.4'
Setting options string 'unum=-1'
Setting entry with key 'unum' to value '-1'
Value -1.000000 for parameter 'unum' out of range [0 - 2.14748e+09]
Error 'unum=-1'
Setting options string 'unum=2147483648'
Setting entry with key 'unum' to value '2147483648'
OK 'unum=2147483648'
Setting options string 'unum=2147483649'
Setting entry with key 'unum' to value '2147483649'
Value 2147483649.000000 for parameter 'unum' out of range [0 - 2.14748e+09]
Error 'unum=2147483649'
Setting options string 'num64=bogus'
Setting entry with key 'num64' to value 'bogus'
Undefined constant or missing '(' in 'bogus'