From 0d09645cfe8876e16d70f5b87bb07c120e291717 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 28 Sep 2024 15:05:12 +0200 Subject: [PATCH] lavu/opt: combine option type size+name into a single descriptor Makes it easier to handle options generically and will be useful in future commits. --- libavutil/opt.c | 84 +++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/libavutil/opt.c b/libavutil/opt.c index 93f2bb1320..48acc11fb5 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -58,26 +58,29 @@ const AVOption *av_opt_next(const void *obj, const AVOption *last) return NULL; } -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), - [AV_OPT_TYPE_STRING] = sizeof(char *), - [AV_OPT_TYPE_RATIONAL] = sizeof(AVRational), - [AV_OPT_TYPE_BINARY] = sizeof(uint8_t *), - [AV_OPT_TYPE_DICT] = sizeof(AVDictionary *), - [AV_OPT_TYPE_IMAGE_SIZE] = sizeof(int[2]), - [AV_OPT_TYPE_VIDEO_RATE] = sizeof(AVRational), - [AV_OPT_TYPE_PIXEL_FMT] = sizeof(int), - [AV_OPT_TYPE_SAMPLE_FMT] = sizeof(int), - [AV_OPT_TYPE_DURATION] = sizeof(int64_t), - [AV_OPT_TYPE_COLOR] = sizeof(uint8_t[4]), - [AV_OPT_TYPE_CHLAYOUT] = sizeof(AVChannelLayout), - [AV_OPT_TYPE_BOOL] = sizeof(int), +static const struct { + size_t size; + const char *name; +} opt_type_desc[] = { + [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), "" }, + [AV_OPT_TYPE_STRING] = { sizeof(char *), "" }, + [AV_OPT_TYPE_RATIONAL] = { sizeof(AVRational), "" }, + [AV_OPT_TYPE_BINARY] = { sizeof(uint8_t *), "" }, + [AV_OPT_TYPE_DICT] = { sizeof(AVDictionary *), "" }, + [AV_OPT_TYPE_IMAGE_SIZE] = { sizeof(int[2]), "" }, + [AV_OPT_TYPE_VIDEO_RATE] = { sizeof(AVRational), "" }, + [AV_OPT_TYPE_PIXEL_FMT] = { sizeof(int), "" }, + [AV_OPT_TYPE_SAMPLE_FMT] = { sizeof(int), "" }, + [AV_OPT_TYPE_DURATION] = { sizeof(int64_t), "" }, + [AV_OPT_TYPE_COLOR] = { sizeof(uint8_t[4]), "" }, + [AV_OPT_TYPE_CHLAYOUT] = { sizeof(AVChannelLayout),"" }, + [AV_OPT_TYPE_BOOL] = { sizeof(int), "" }, }; // option is plain old data @@ -114,7 +117,7 @@ static uint8_t opt_array_sep(const AVOption *o) static void *opt_array_pelem(const AVOption *o, void *array, unsigned idx) { av_assert1(o->type & AV_OPT_TYPE_FLAG_ARRAY); - return (uint8_t *)array + idx * opt_elem_size[TYPE_BASE(o->type)]; + return (uint8_t *)array + idx * opt_type_desc[TYPE_BASE(o->type)].size; } static unsigned *opt_array_pcount(const void *parray) @@ -670,7 +673,7 @@ static int opt_set_array(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst) { const AVOptionArrayDef *arr = o->default_val.arr; - const size_t elem_size = opt_elem_size[TYPE_BASE(o->type)]; + const size_t elem_size = opt_type_desc[TYPE_BASE(o->type)].size; const uint8_t sep = opt_array_sep(o); uint8_t *str = NULL; @@ -1441,36 +1444,15 @@ static char *get_opt_flags_string(void *obj, const char *unit, int64_t value) static void log_type(void *av_log_obj, const AVOption *o, enum AVOptionType parent_type) { - const char *desc[] = { - [AV_OPT_TYPE_FLAGS] = "", - [AV_OPT_TYPE_INT] = "", - [AV_OPT_TYPE_INT64] = "", - [AV_OPT_TYPE_UINT] = "", - [AV_OPT_TYPE_UINT64] = "", - [AV_OPT_TYPE_DOUBLE] = "", - [AV_OPT_TYPE_FLOAT] = "", - [AV_OPT_TYPE_STRING] = "", - [AV_OPT_TYPE_RATIONAL] = "", - [AV_OPT_TYPE_BINARY] = "", - [AV_OPT_TYPE_DICT] = "", - [AV_OPT_TYPE_IMAGE_SIZE] = "", - [AV_OPT_TYPE_VIDEO_RATE] = "", - [AV_OPT_TYPE_PIXEL_FMT] = "", - [AV_OPT_TYPE_SAMPLE_FMT] = "", - [AV_OPT_TYPE_DURATION] = "", - [AV_OPT_TYPE_COLOR] = "", - [AV_OPT_TYPE_CHLAYOUT] = "", - [AV_OPT_TYPE_BOOL] = "", - }; const enum AVOptionType type = TYPE_BASE(o->type); if (o->type == AV_OPT_TYPE_CONST && TYPE_BASE(parent_type) == AV_OPT_TYPE_INT) av_log(av_log_obj, AV_LOG_INFO, "%-12"PRId64" ", o->default_val.i64); - else if (type < FF_ARRAY_ELEMS(desc) && desc[type]) { + else if (type < FF_ARRAY_ELEMS(opt_type_desc) && opt_type_desc[type].name) { if (o->type & AV_OPT_TYPE_FLAG_ARRAY) - av_log(av_log_obj, AV_LOG_INFO, "[%-10s]", desc[type]); + av_log(av_log_obj, AV_LOG_INFO, "[%-10s]", opt_type_desc[type].name); else - av_log(av_log_obj, AV_LOG_INFO, "%-12s ", desc[type]); + av_log(av_log_obj, AV_LOG_INFO, "%-12s ", opt_type_desc[type].name); } else av_log(av_log_obj, AV_LOG_INFO, "%-12s ", ""); @@ -2068,7 +2050,7 @@ static int opt_copy_elem(void *logctx, enum AVOptionType type, if (dst != src) return av_channel_layout_copy(dst, src); } else if (opt_is_pod(type)) { - size_t size = opt_elem_size[type]; + size_t size = opt_type_desc[type].size; memcpy(dst, src, size); } else { av_log(logctx, AV_LOG_ERROR, "Unhandled option type: %d\n", type); @@ -2092,7 +2074,7 @@ static int opt_copy_array(void *logctx, const AVOption *o, opt_free_array(o, pdst, opt_array_pcount(pdst)); - dst = av_calloc(nb_elems, opt_elem_size[TYPE_BASE(o->type)]); + dst = av_calloc(nb_elems, opt_type_desc[TYPE_BASE(o->type)].size); if (!dst) return AVERROR(ENOMEM); @@ -2160,7 +2142,7 @@ int av_opt_get_array(void *obj, const char *name, int search_flags, unsigned int start_elem, unsigned int nb_elems, enum AVOptionType out_type, void *out_val) { - const size_t elem_size_out = opt_elem_size[TYPE_BASE(out_type)]; + const size_t elem_size_out = opt_type_desc[TYPE_BASE(out_type)].size; const AVOption *o; void *target_obj; @@ -2248,7 +2230,7 @@ int av_opt_set_array(void *obj, const char *name, int search_flags, unsigned int start_elem, unsigned int nb_elems, enum AVOptionType val_type, const void *val) { - const size_t elem_size_val = opt_elem_size[TYPE_BASE(val_type)]; + const size_t elem_size_val = opt_type_desc[TYPE_BASE(val_type)].size; const AVOption *o; const AVOptionArrayDef *arr; @@ -2271,7 +2253,7 @@ int av_opt_set_array(void *obj, const char *name, int search_flags, arr = o->default_val.arr; parray = (uint8_t *)target_obj + o->offset; array_size = opt_array_pcount(parray); - elem_size = opt_elem_size[TYPE_BASE(o->type)]; + elem_size = opt_type_desc[TYPE_BASE(o->type)].size; if (start_elem > *array_size) return AVERROR(EINVAL);