From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 81C3F49BA9 for ; Mon, 4 Mar 2024 13:07:17 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 906F568D407; Mon, 4 Mar 2024 15:07:14 +0200 (EET) Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BA92668D35D for ; Mon, 4 Mar 2024 15:07:08 +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=ZMIVKbUk; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 1AF494D78 for ; Mon, 4 Mar 2024 14:07:07 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id zpECdzo0PblK for ; Mon, 4 Mar 2024 14:07:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1709557626; bh=pc/P0BtVmYjHuWV5PF95zU4LZ3s/q/N4h4TSHkymCFo=; h=From:To:Subject:Date:From; b=ZMIVKbUkWQTu0VRxfodeGoC0TdN3x4YnBGVBDYsyeCnE3lhXVs7SzjeDSwf6XaD8N OF4ODpLQ7ZPCq2kQ3hVjsyNADAO8X1NsiNSH0jCjacFVv8XVRhJEqFOkROVgfIEY4s 89yL7MMuLeAhsl1NbEmFczE8+rBZlAuGcyIb0q5kiNNap/Svr8//aIWUueWZU4gOL0 DPv0xRrWd/YO3liV1aXH4T8pBieTRqJZStn29a5ytFprhG7SDt4NHxgyz1VgS1Xk4y N6pxk0euxnN2KNGgHLd5NSIJ/WxhworZIiZTH2YZk/r5dU+NDIo4m+JCprTSR/Tyfx V7g3pZmt7LAZQ== 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 126984D3D for ; Mon, 4 Mar 2024 14:07:06 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id D7C7E3A0357 for ; Mon, 4 Mar 2024 14:06:59 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Mar 2024 14:06:16 +0100 Message-ID: <20240304130657.30631-1-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/29] 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 d68184c2cc..dac00478ee 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -844,13 +844,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; @@ -861,107 +971,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); } -- 2.43.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".