From patchwork Fri Jun 11 20:30:54 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: 28216 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5e:c91a:0:0:0:0:0 with SMTP id z26csp766160iol; Fri, 11 Jun 2021 13:38:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBv5C0ybCjE4cDvve8ou+FfD4M3ZOS+JDtrVs7wKpuuaLMU5aQ2V9ux6RJ9anza1w30n7x X-Received: by 2002:a17:907:270c:: with SMTP id w12mr5178733ejk.175.1623443899598; Fri, 11 Jun 2021 13:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623443899; cv=none; d=google.com; s=arc-20160816; b=IJ0zy9zEiWIAe8gjLjyE2ymZwOA4VdP1KGbKem/iUL7oy3BzYKvNtOIRIuUkmThpjJ rHfinCyZ2X6Sm0GYjHNjDbm8c/3FLXti3ZOaDRvMJOxETbvQBBLvoKjXzvSQgzSCco3+ z9DqA2uy7zlqtFzVf+wtbQmuQR6cyZfxkptUm3kr/66AF4MZcBsBuU27UxMUbPb8FTGE Rcnzl6ZzjUqrCXIvjmO5YClFYN/261j4O23lHVCEFQfPEvdCPWvvJL9RwYKDLGx5vTYz N7RaNlRd3kHW3coES2AU+E+rBaGGFdqJ73IF49bpefzYxHR7QUOv9hUPIs7sSdpAg9Wz E7nQ== 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=FDHDZDVIc2luBdDk/C2twnOrBee02Unz5yx3+16zgmY=; b=CJFbdYhYNZLhwuXF9cahW6Lzzdm0C2+ePeaeqwYvhkw6B8bcvF0rFWzNYKj5mOiXhl PFZiEcaqqhxdRXL56Nue/WeLWKnhwBHitFGzDFqwqnoYBsQy8UKz+6/JsX7wcCskqh8i 78lJhI5TYmW2Sb5kbykcZCnjysxbon8UivYuNImoGDxgEq+SgvtAbUBy2aOoQzseSxbf gTfjM8hVH7pXGRTGst4dncUhlqem8hKcZTSzYxC27Qi8UOU7G4iXG4u8cy5PEqvhuV1y vEIjUr7a/95Ag6HdMoeDAO0xMz3BN5Y24iLbz+W13e74Mm4PeybuNnyBRuBPM1QBowlx TGWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Kek6aoBq; 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 y1si5570052edu.152.2021.06.11.13.38.18; Fri, 11 Jun 2021 13:38:19 -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=Kek6aoBq; 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 DC477689965; Fri, 11 Jun 2021 23:38:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E865A688388 for ; Fri, 11 Jun 2021 23:38:08 +0300 (EEST) Received: by mail-lj1-f182.google.com with SMTP id x14so11321042ljp.7 for ; Fri, 11 Jun 2021 13:38:08 -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=FOWRE2gVlL9IfMVOz3GKh1a3HX5SUGmdMciAmvht/Fk=; b=Kek6aoBqbNTxlLxC3AD2xndSnlOt4dqWrby/d/g8dMLfTohonwA51z2UvPFGZaHg8O Vnmov10dTHN7WMvMxxReg5QZWsM+6fnzkYj1gpwP3s18VjqHLlnLW4waPc5I4+n0ksZK mMT5ZwAMYJsAW1u2kIXW6GLS3ftzEI8EHMxTz99DTpRvKpJZB+keUjSWfSXh4kky8D+w MMcRidgZy1oOZ4UY5v5+QLdgOdtiVaaPsX4ni3CejjTvBHy/2nkBC8vMAQRE+M10oW5+ nXh0xRwlc5811AoIQ1F6HjIkq+cADw2Jp7pSnkBVIkheQlQrQ9O/+fdJzPuq5PsdMHaD l/yQ== 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=FOWRE2gVlL9IfMVOz3GKh1a3HX5SUGmdMciAmvht/Fk=; b=kJtd9FjwdISu/kp+Z3NazhdVnqUEm3bLBF6hn9/RRViSmHgdwqQwTe1Z3x350j8+Zr BnNCAItYesYkHmE8kwNPcYCUwKaa/CKiZFz8oO8wlA9fRCTQYolKFkoIroN32rdhTfeF qBSzlshURdZ2LzSoFsgLNq2HWGPCar+/qrXGl7wWcrikdcZEvPhkYVFsSuSZopbmFMKn H31rK0p4wT1M7LhBmlpf4LSLQms1j6BO5kUX93MHWyWcQ+hfJklN5vJJDuanersU1bCk fUFkmebvqKdNeVLRsdw4FS6Vm2CMns2qnm7iRvK8QttwigkpoThclWK8LhXKwRisnyRJ Jp6A== X-Gm-Message-State: AOAM531lEtx7qyEpfDIa7aP7sD4UgHDSpZY0+tRxbP+6rQFVUGG7pVvn cGBXp4Wej9r+VfGrEuXjqJRGRhKBu9H12g== X-Received: by 2002:a2e:a612:: with SMTP id v18mr4455862ljp.358.1623443887993; Fri, 11 Jun 2021 13:38:07 -0700 (PDT) Received: from localhost.localdomain ([196.196.203.214]) by smtp.gmail.com with ESMTPSA id b18sm686787lfb.277.2021.06.11.13.38.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Jun 2021 13:38:07 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Fri, 11 Jun 2021 22:30:54 +0200 Message-Id: <20210611203104.1692-24-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210611203104.1692-1-dcnieho@gmail.com> References: <20210611203104.1692-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 23/33] avutil/opt: add av_opt_print_num 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: Rw4caCysh7to 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 | 81 +++++++++++++++++++++++++++++++++++++-------- libavutil/opt.h | 17 +++++++++- libavutil/version.h | 2 +- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/libavutil/opt.c b/libavutil/opt.c index edb2c2ca41..108e9baf3b 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, AVOption *o, 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]; 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; @@ -820,7 +810,8 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) 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); + if (o) + ret = snprintf(buf, sizeof(buf), "%f", o->default_val.dbl); break; case AV_OPT_TYPE_STRING: if (*(uint8_t **)dst) { @@ -889,6 +880,70 @@ 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); + + 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; + + return print_option(dst, o->type, search_flags, o, out_val); +} + +int av_opt_print_num(enum AVOptionType type, double val, 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, NULL, 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, NULL, out_val); + } + case AV_OPT_TYPE_UINT64: + { + uint64_t temp = llrint(val); + return print_option(&temp, type, 0, NULL, out_val); + } + case AV_OPT_TYPE_FLOAT: + { + float temp = (float)val; + return print_option(&temp, type, 0, NULL, out_val); + } + case AV_OPT_TYPE_DOUBLE: + return print_option(&val, type, 0, NULL, 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 3c64e8d3a5..f9da141588 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_print_num() may be of use. * } * av_opt_freep_ranges(&ranges); * @endcode @@ -760,6 +760,21 @@ 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] type Type of the option. + * @param[in] val An option value that can be represented as a double + (e.g. min or max fields of AVOption, or min_value and + max_value of AVOptionRange) + * @param[out] out_val Value of the option will be written as a string 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_print_num(enum AVOptionType type, double val, uint8_t **out_val); + /** * @} */ diff --git a/libavutil/version.h b/libavutil/version.h index e11eaa20d0..34b83112de 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 0 +#define LIBAVUTIL_VERSION_MINOR 1 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \