From patchwork Mon Jun 7 23:04:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Diederick C. Niehorster" X-Patchwork-Id: 28134 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp3886727iof; Mon, 7 Jun 2021 16:06:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1twi5KGEyNWosulwaKPfhSkCfvrWLIU7cjxpjzxJSSr1QFFIuzoRDsvRySvyUJEKdtqv9 X-Received: by 2002:a17:906:d92:: with SMTP id m18mr18277502eji.27.1623107169316; Mon, 07 Jun 2021 16:06:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623107169; cv=none; d=google.com; s=arc-20160816; b=ZbJ1Pi8J5C5NYP9v3wYgE5TsLybQJblaykc1gRS9MAxQLNQLWTkvak5HxCU8WxGMgp KswCnfaBMzmGhCjx1pGWCZgR+beW1a+rqUkbmoVEw8VvwEvY6kvx+OFHlcuzpXo1RpP9 WVJngp4KkqQRLsa6TXefVYzsc7g1h5bg9AEgs6ujJdonq5LSbgD/Wnvttc9NnStIjUQQ Q/m1eikUCTx2FepKdRX9cUa/3BSkcdbSGWLifIGslvc1xnLwthO3zDhhVTHilT1zQi/R 0W1nyl6QEjs4a0jXS0jThy1SCiDIbTrXK+gdvIt+MIZjqx/tPxk9wjRsefSweizjxRdF koTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=ogfrJfKIvcyDIYa2Yy98fo95/RBkYPC4XwiE4cBrpDU=; b=oitGEcOCVk3NUtAeyKER+Y7bAMNJTXm0r/ex0Hmy3jjmHUSW3N5Twzw9Y437KiQh9q DDPZe4c3tAhEucVzu30VUiMVGiVT3k61ZmGufQb+Ffba42nOSk/Gn+0pcoLYSGhr3JJq 8gQzPsPufvGKEUDq3Qu5BcksEbQ/ZKvq/S2MzOlnTSTIjK4YsvDckJ0hpQ5+t3UmslRC +wlzS9s/F9rNAwOHnzWjGhQfHD/eq3wjFPWf3uo51MMR+npHL/5+mZYeZK+/VZGltcmG 6uYI9j74Cyw6RjBu/rEaMixUdDKikKTzWV9OZCL0k8vcw/RAGRa2SQCiqZxez8pPdlKz FLiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=afZnLDC2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j12si13239360ejy.26.2021.06.07.16.06.09; Mon, 07 Jun 2021 16:06:09 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=afZnLDC2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EEE966809C9; Tue, 8 Jun 2021 02:04:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 324B76808F5 for ; Tue, 8 Jun 2021 02:04:51 +0300 (EEST) Received: by mail-lf1-f42.google.com with SMTP id v8so28962034lft.8 for ; Mon, 07 Jun 2021 16:04:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wV/vu1hwYesFxXQ71GNOAneoY6bxdimNCPxHKRhh4Kc=; b=afZnLDC2FFlS0tisa/W4vxJwZiFfS5sKrSpJCEUNlhCi2YGPn5tNurpFOzNi3/Yt7Y VvNyYKhV83g/xkOlaBYQ1gmvnC/wYADS8h3JJ8GpvEdYKcC0OAuPep0dxJxSuzyWFfe1 6JoiCMFIAZGcbdeOdDAy04BNxwxjAmmenI/XHZolcdK79eUhoq/VsGtpAlmDYrJpsQOE OYyQseVdSssgM2h7D/5gd4DgtfhVE5NMnTqUW0u7fY64a8XrNyjY7RmPeuupal2KXjr8 Md5RKrasSqfVCZHQHCdgK8Hz/xp476WWMrIhhIHNE7CG6HpPDeCFQPV4pFq+RsJv0ULJ 6rOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wV/vu1hwYesFxXQ71GNOAneoY6bxdimNCPxHKRhh4Kc=; b=X1ZJPmJa+0LpGlw/4sO+Odk3HnPqjA9//+PP5b5N2bRlVRoAjCGGvdCFv0eC+QwPC9 QzZguvQdWSdOpfS3cpnwLCSdiIJECh0Z3QzGD7ebTh7o1u8DJl5Dg7UUbHSOIQFGl1vD WeDLN978/Q+BlOxnZ8AxzWEpejlA7KT832dcrjB3qKsZynIpJ/Bz8YMCkQ3MwKRb7nbD gSHRx86QE1EzeYoGGX8dCRqm0NBT3Ju7qrSNma+WEHr3WfY+rmyDDVAsSkAJAJ4z2COE 0GltNZ/vCiwLgQvELgZ5W4dNM10J0sAre/za5SFwMyiq7lscL0EeegUff+BLh1tJougO EXhA== X-Gm-Message-State: AOAM532iwgHkNJLr8qMlHIqBdfNFK9yJeC9cB3eGazUUs7ouIbUkxglN kguO1nYtvP4b6ytLGNaOF6oHIpJD5cXUJA== X-Received: by 2002:a05:6512:38ce:: with SMTP id p14mr13706214lft.197.1623107090206; Mon, 07 Jun 2021 16:04:50 -0700 (PDT) Received: from localhost.localdomain (84-217-56-54.customers.ownit.se. [84.217.56.54]) by smtp.gmail.com with ESMTPSA id v9sm1999563ljv.131.2021.06.07.16.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 16:04:49 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Jun 2021 01:04:04 +0200 Message-Id: <20210607230414.612-26-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210607230414.612-1-dcnieho@gmail.com> References: <20210607230414.612-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 25/35] avutil/opt: add av_opt_to_string X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Diederick Niehorster Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: W2r6xFmtbiJp This function allows formatting an option value stored in a double (such as the min and max fields of an AVOption, or min_value and max_value of an AVOptionRange) properly, e.g. 1 for a AV_OPT_TYPE_PIXEL_FMT -> yuyv422. Useful when printing more info about an option than just its value. Usage will be shown in upcoming device_get_capabilities example. av_opt_get (body changed) still passes FATE. Signed-off-by: Diederick Niehorster --- libavutil/opt.c | 93 +++++++++++++++++++++++++++++++++++++-------- libavutil/opt.h | 12 +++++- libavutil/version.h | 2 +- 3 files changed, 89 insertions(+), 18 deletions(-) diff --git a/libavutil/opt.c b/libavutil/opt.c index ab127b30fa..3e385852eb 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -776,24 +776,14 @@ static void format_duration(char *buf, size_t size, int64_t d) *(--e) = 0; } -int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) +static int print_option(void* dst, enum AVOptionType type, int search_flags, uint8_t** out_val) { - void *dst, *target_obj; - const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); - uint8_t *bin, buf[128]; + uint8_t* bin, buf[128]; int len, i, ret; int64_t i64; - if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST)) - return AVERROR_OPTION_NOT_FOUND; - - if (o->flags & AV_OPT_FLAG_DEPRECATED) - av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help); - - dst = (uint8_t *)target_obj + o->offset; - buf[0] = 0; - switch (o->type) { + switch (type) { case AV_OPT_TYPE_BOOL: ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(get_bool_name(*(int *)dst), "invalid")); break; @@ -819,9 +809,6 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) case AV_OPT_TYPE_RATIONAL: ret = snprintf(buf, sizeof(buf), "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den); break; - case AV_OPT_TYPE_CONST: - ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl); - break; case AV_OPT_TYPE_STRING: if (*(uint8_t **)dst) { *out_val = av_strdup(*(uint8_t **)dst); @@ -889,6 +876,80 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) return *out_val ? 0 : AVERROR(ENOMEM); } +int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) +{ + void *dst, *target_obj; + const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); + uint8_t buf[128]; + int ret; + + if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST)) + return AVERROR_OPTION_NOT_FOUND; + + if (o->flags & AV_OPT_FLAG_DEPRECATED) + av_log(obj, AV_LOG_WARNING, "The \"%s\" option is deprecated: %s\n", name, o->help); + + dst = (uint8_t *)target_obj + o->offset; + + + if (o->type != AV_OPT_TYPE_CONST) + return print_option(dst, o->type, search_flags, out_val); + + // special case for a const + ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl); + if (ret >= sizeof(buf)) + return AVERROR(EINVAL); + *out_val = av_strdup(buf); + return *out_val ? 0 : AVERROR(ENOMEM); +} +int av_opt_to_string(double val, enum AVOptionType type, uint8_t** out_val) +{ + *out_val = NULL; + + switch (type) { + case AV_OPT_TYPE_FLAGS: + case AV_OPT_TYPE_BOOL: + case AV_OPT_TYPE_INT: + case AV_OPT_TYPE_PIXEL_FMT: + case AV_OPT_TYPE_SAMPLE_FMT: + { + int temp = lrint(val); + return print_option(&temp, type, 0, out_val); + } + case AV_OPT_TYPE_INT64: + case AV_OPT_TYPE_DURATION: + case AV_OPT_TYPE_CHANNEL_LAYOUT: + { + int64_t temp = llrint(val); + return print_option(&temp, type, 0, out_val); + } + case AV_OPT_TYPE_UINT64: + { + uint64_t temp = llrint(val); + return print_option(&temp, type, 0, out_val); + } + case AV_OPT_TYPE_FLOAT: + { + float temp = (float)val; + return print_option(&temp, type, 0, out_val); + } + case AV_OPT_TYPE_DOUBLE: + return print_option(&val, type, 0, out_val); + + default: + // AV_OPT_TYPE_DICT, + // AV_OPT_TYPE_COLOR, + // AV_OPT_TYPE_BINARY, + // AV_OPT_TYPE_STRING, + // AV_OPT_TYPE_RATIONAL, + // AV_OPT_TYPE_IMAGE_SIZE, + // AV_OPT_TYPE_VIDEO_RATE, + // AV_OPT_TYPE_CONST + // cannot be stored in a single double, and are thus not a valid input + return AVERROR(EINVAL); + } +} + static int get_number(void *obj, const char *name, const AVOption **o_out, double *num, int *den, int64_t *intnum, int search_flags) { diff --git a/libavutil/opt.h b/libavutil/opt.h index ac0b4567a6..bd386c411b 100644 --- a/libavutil/opt.h +++ b/libavutil/opt.h @@ -361,7 +361,7 @@ typedef struct AVOptionRanges { * for (range_index = 0; range_index < ranges->nb_ranges; range_index++) { * for (component_index = 0; component_index < ranges->nb_components; component_index++) * range[component_index] = ranges->range[ranges->nb_ranges * component_index + range_index]; - * //do something with range here. + * //do something with range here. For printing the value, av_opt_to_string() may be of use. * } * av_opt_freep_ranges(&ranges); * @endcode @@ -760,6 +760,16 @@ int av_opt_get_channel_layout(void *obj, const char *name, int search_flags, int * be freed with av_dict_free() by the caller */ int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val); +/** + * Format option value and output to string. + * @param[in] val an option value that can be represented as a double. + * @param[in] type of the option. + * @param[out] out_val value of the option will be written here + * @return >=0 on success, a negative error code otherwise + * + * @note the returned string will be av_malloc()ed and must be av_free()ed by the caller + */ +int av_opt_to_string(double val, enum AVOptionType type, uint8_t **out_val); /** * @} */ diff --git a/libavutil/version.h b/libavutil/version.h index 34b83112de..77ca4becd6 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 1 +#define LIBAVUTIL_VERSION_MINOR 2 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \