From patchwork Fri Feb 23 13:58:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 46469 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:26a3:b0:19e:cdac:8cce with SMTP id h35csp993137pze; Fri, 23 Feb 2024 06:33:25 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVk8I/vakSgEXJjV9YFgtIQVHfgdQQfWdvU4qWmpBLbjwCr1WJSkoBbmozLACqHUDv83Mf7guelvGMQkZymysxj2s5Q0GJr8vRebA== X-Google-Smtp-Source: AGHT+IGkltE5k0Xm+1bZwS4NzPjsM0Y8njdxVOq9Soag+dJcAk/GLaZRJSzGLUN7r+AoMN9XtFbY X-Received: by 2002:a05:6402:31fa:b0:565:78ff:a65b with SMTP id dy26-20020a05640231fa00b0056578ffa65bmr24115edb.24.1708698804720; Fri, 23 Feb 2024 06:33:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708698804; cv=none; d=google.com; s=arc-20160816; b=0YCaaQWWmsPTImjomZ9ATGy+4d/4uhTGztGlGn1HV58P0q9wPctO4mB3nE5z6FwmDb vh58f02OsdQcXwHYcE6Dr1JwiFdJ4taEIZRb3sH26bdeU2lTKZJqSTk9uMVTQNRK+p+f QfNJH0ISjr3qR3eIU40Tz/qM3ZbGN9wl+YLvsMV0R5O3h9D/fRAbnfPX94Ek6MjLmu43 jvdVyvmbO23v7pwH5GK9T+N2PzIOrHUE7SO9RZSaqsbNtKVD98d+Iif4w75FJwtpW9wo +A85iPF7VvdniXOJDTNyC4fYU+aL6WVr1mX2S3T5B8I7h18tl206HkqKXuQKIWwpzK0C XwoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding: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=EN5cPbcXxX+cKxAp1sZ02ek6mwBs5EW6+gxQGkLLgG8=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=i1SMSZyusA3gU48bfqEMvkG9uFCn78ng/XKew5MpVpGH+3qTWEmsbJHGOhZLgjHFF1 GBnCTAK0XHiVw1xaLfwFSBNY4R/QOhoJXkhoSLMfUluQpPhsIJrwmr7bBj0aWRh5DdX0 /Oz+ya5yTocz5Q7sE2JN1dBZSfS44KbJjkjHQD80PawwKY7zNiLRR7iHprqmkxl4MWpO FCaFWOtgYFdCokhKnpFM/AbUeTD6/HvVrthSWLLJQpkONYwofRKSd/yw39w8zq0eW4NU Dgp8dGeg/23IvaPvoyZktHBlw3En1jPIc7RLT6LuRYH1WXwFmFcx12uE1fiD/5+OyiBM iNZg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=NdIAbADM; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j22-20020a508a96000000b00564953c2af7si3995806edj.224.2024.02.23.06.33.24; Fri, 23 Feb 2024 06:33:24 -0800 (PST) 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=@khirnov.net header.s=mail header.b=NdIAbADM; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0683668C95B; Fri, 23 Feb 2024 16:31:50 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A498F68C462 for ; Fri, 23 Feb 2024 16:31:35 +0200 (EET) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=NdIAbADM; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 5EE334D26 for ; Fri, 23 Feb 2024 15:31:31 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id znK6Tb0f9YAS for ; Fri, 23 Feb 2024 15:31:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1708698687; bh=B3qKkjein579Kz0OT+Sh2MG3bwpgvqkPDygadJpC1R8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=NdIAbADMbRVsAe+AXzkhH3A5kLpD6QMzvxn41yu8PylpE1iHSnW1L0C5VuO4+keJp Bd5LnzVCPs2XtdF+WQINygXYDoIjKpeXGdk2gSsytleX4f2AmH593naQu+Vwff+9F4 0CxffHA+Luly3n4u9aTsDYkZVPtKXo4iK1/E7vrsvg2RKaeE/u3wLvPHQhduCf/4F+ YZa7Xu0sQjBjwPZwduVR3DyCYdMcCja91uLQPs1QSvtjKdhFp4mzZvbfwizAoJa5is 48mp/gDjofTG0/e1D01vYqqy+sMfqzNM/Oe9dxnOemKPvtVEMKi/QTakUYFtDZS7UJ MjSBiVCRFme/g== Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 895484D42 for ; Fri, 23 Feb 2024 15:31:27 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 6A52B3A1AF2 for ; Fri, 23 Feb 2024 15:31:22 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 23 Feb 2024 14:58:35 +0100 Message-ID: <20240223143115.16521-14-anton@khirnov.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240223143115.16521-1-anton@khirnov.net> References: <20240223143115.16521-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 13/38] lavu/opt: factor per-type dispatch out of av_opt_get() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KYNZX59u6uWR Will be useful in following commits. --- libavutil/opt.c | 220 ++++++++++++++++++++++++++---------------------- 1 file changed, 121 insertions(+), 99 deletions(-) diff --git a/libavutil/opt.c b/libavutil/opt.c index 8ee89237c5..0ca6d4d555 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -834,13 +834,123 @@ static void format_duration(char *buf, size_t size, int64_t d) *(--e) = 0; } +static int opt_get_elem(const AVOption *o, uint8_t **pbuf, size_t buf_len, + void *dst, int search_flags) +{ + int ret; + + switch (o->type) { + case AV_OPT_TYPE_BOOL: + ret = snprintf(*pbuf, buf_len, "%s", get_bool_name(*(int *)dst)); + break; + case AV_OPT_TYPE_FLAGS: + ret = snprintf(*pbuf, buf_len, "0x%08X", *(int *)dst); + break; + case AV_OPT_TYPE_INT: + ret = snprintf(*pbuf, buf_len, "%d", *(int *)dst); + break; + case AV_OPT_TYPE_INT64: + ret = snprintf(*pbuf, buf_len, "%"PRId64, *(int64_t *)dst); + break; + case AV_OPT_TYPE_UINT64: + ret = snprintf(*pbuf, buf_len, "%"PRIu64, *(uint64_t *)dst); + break; + case AV_OPT_TYPE_FLOAT: + ret = snprintf(*pbuf, buf_len, "%f", *(float *)dst); + break; + case AV_OPT_TYPE_DOUBLE: + ret = snprintf(*pbuf, buf_len, "%f", *(double *)dst); + break; + case AV_OPT_TYPE_VIDEO_RATE: + case AV_OPT_TYPE_RATIONAL: + ret = snprintf(*pbuf, buf_len, "%d/%d", ((AVRational *)dst)->num, ((AVRational *)dst)->den); + break; + case AV_OPT_TYPE_CONST: + ret = snprintf(*pbuf, buf_len, "%"PRId64, o->default_val.i64); + break; + case AV_OPT_TYPE_STRING: + if (*(uint8_t **)dst) { + *pbuf = av_strdup(*(uint8_t **)dst); + } else if (search_flags & AV_OPT_ALLOW_NULL) { + *pbuf = NULL; + return 0; + } else { + *pbuf = av_strdup(""); + } + return *pbuf ? 0 : AVERROR(ENOMEM); + case AV_OPT_TYPE_BINARY: { + const uint8_t *bin; + int len; + + if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) { + *pbuf = NULL; + return 0; + } + len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *)); + if ((uint64_t)len * 2 + 1 > INT_MAX) + return AVERROR(EINVAL); + if (!(*pbuf = av_malloc(len * 2 + 1))) + return AVERROR(ENOMEM); + if (!len) { + *pbuf[0] = '\0'; + return 0; + } + bin = *(uint8_t **)dst; + for (int i = 0; i < len; i++) + snprintf(*pbuf + i * 2, 3, "%02X", bin[i]); + return 0; + } + case AV_OPT_TYPE_IMAGE_SIZE: + ret = snprintf(*pbuf, buf_len, "%dx%d", ((int *)dst)[0], ((int *)dst)[1]); + break; + case AV_OPT_TYPE_PIXEL_FMT: + ret = snprintf(*pbuf, buf_len, "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none")); + break; + case AV_OPT_TYPE_SAMPLE_FMT: + ret = snprintf(*pbuf, buf_len, "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none")); + break; + case AV_OPT_TYPE_DURATION: { + int64_t i64 = *(int64_t *)dst; + format_duration(*pbuf, buf_len, i64); + ret = strlen(*pbuf); // no overflow possible, checked by an assert + break; + } + case AV_OPT_TYPE_COLOR: + ret = snprintf(*pbuf, buf_len, "0x%02x%02x%02x%02x", + (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1], + (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]); + break; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + case AV_OPT_TYPE_CHANNEL_LAYOUT: { + int64_t i64 = *(int64_t *)dst; + ret = snprintf(*pbuf, buf_len, "0x%"PRIx64, i64); + break; + } +FF_ENABLE_DEPRECATION_WARNINGS +#endif + case AV_OPT_TYPE_CHLAYOUT: + ret = av_channel_layout_describe(dst, *pbuf, buf_len); + break; + case AV_OPT_TYPE_DICT: + if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) { + *pbuf = NULL; + return 0; + } + return av_dict_get_string(*(AVDictionary **)dst, (char **)pbuf, '=', ':'); + default: + return AVERROR(EINVAL); + } + + return ret; +} + 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 *bin, buf[128]; - int len, i, ret; - int64_t i64; + uint8_t *out, buf[128]; + int ret; if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST)) return AVERROR_OPTION_NOT_FOUND; @@ -851,107 +961,19 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) dst = (uint8_t *)target_obj + o->offset; buf[0] = 0; - switch (o->type) { - case AV_OPT_TYPE_BOOL: - ret = snprintf(buf, sizeof(buf), "%s", get_bool_name(*(int *)dst)); - break; - case AV_OPT_TYPE_FLAGS: - ret = snprintf(buf, sizeof(buf), "0x%08X", *(int *)dst); - break; - case AV_OPT_TYPE_INT: - ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst); - break; - case AV_OPT_TYPE_INT64: - ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst); - break; - case AV_OPT_TYPE_UINT64: - ret = snprintf(buf, sizeof(buf), "%"PRIu64, *(uint64_t *)dst); - break; - case AV_OPT_TYPE_FLOAT: - ret = snprintf(buf, sizeof(buf), "%f", *(float *)dst); - break; - case AV_OPT_TYPE_DOUBLE: - ret = snprintf(buf, sizeof(buf), "%f", *(double *)dst); - break; - case AV_OPT_TYPE_VIDEO_RATE: - 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), "%"PRId64, o->default_val.i64); - break; - case AV_OPT_TYPE_STRING: - if (*(uint8_t **)dst) { - *out_val = av_strdup(*(uint8_t **)dst); - } else if (search_flags & AV_OPT_ALLOW_NULL) { - *out_val = NULL; - return 0; - } else { - *out_val = av_strdup(""); - } - return *out_val ? 0 : AVERROR(ENOMEM); - case AV_OPT_TYPE_BINARY: - if (!*(uint8_t **)dst && (search_flags & AV_OPT_ALLOW_NULL)) { - *out_val = NULL; - return 0; - } - len = *(int *)(((uint8_t *)dst) + sizeof(uint8_t *)); - if ((uint64_t)len * 2 + 1 > INT_MAX) - return AVERROR(EINVAL); - if (!(*out_val = av_malloc(len * 2 + 1))) - return AVERROR(ENOMEM); - if (!len) { - *out_val[0] = '\0'; - return 0; - } - bin = *(uint8_t **)dst; - for (i = 0; i < len; i++) - snprintf(*out_val + i * 2, 3, "%02X", bin[i]); - return 0; - case AV_OPT_TYPE_IMAGE_SIZE: - ret = snprintf(buf, sizeof(buf), "%dx%d", ((int *)dst)[0], ((int *)dst)[1]); - break; - case AV_OPT_TYPE_PIXEL_FMT: - ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_pix_fmt_name(*(enum AVPixelFormat *)dst), "none")); - break; - case AV_OPT_TYPE_SAMPLE_FMT: - ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none")); - break; - case AV_OPT_TYPE_DURATION: - i64 = *(int64_t *)dst; - format_duration(buf, sizeof(buf), i64); - ret = strlen(buf); // no overflow possible, checked by an assert - break; - case AV_OPT_TYPE_COLOR: - ret = snprintf(buf, sizeof(buf), "0x%02x%02x%02x%02x", - (int)((uint8_t *)dst)[0], (int)((uint8_t *)dst)[1], - (int)((uint8_t *)dst)[2], (int)((uint8_t *)dst)[3]); - break; -#if FF_API_OLD_CHANNEL_LAYOUT -FF_DISABLE_DEPRECATION_WARNINGS - case AV_OPT_TYPE_CHANNEL_LAYOUT: - i64 = *(int64_t *)dst; - ret = snprintf(buf, sizeof(buf), "0x%"PRIx64, i64); - break; -FF_ENABLE_DEPRECATION_WARNINGS -#endif - case AV_OPT_TYPE_CHLAYOUT: - ret = av_channel_layout_describe(dst, buf, sizeof(buf)); - break; - case AV_OPT_TYPE_DICT: - if (!*(AVDictionary **)dst && (search_flags & AV_OPT_ALLOW_NULL)) { - *out_val = NULL; - return 0; - } - return av_dict_get_string(*(AVDictionary **)dst, (char **)out_val, '=', ':'); - default: - return AVERROR(EINVAL); + out = buf; + ret = opt_get_elem(o, &out, sizeof(buf), dst, search_flags); + if (ret < 0) + return ret; + if (out != buf) { + *out_val = out; + return 0; } if (ret >= sizeof(buf)) return AVERROR(EINVAL); - *out_val = av_strdup(buf); + *out_val = av_strdup(out); return *out_val ? 0 : AVERROR(ENOMEM); }