From patchwork Wed Dec 6 08:22:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 44954 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:9153:b0:181:818d:5e7f with SMTP id x19csp106481pzc; Wed, 6 Dec 2023 00:23:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnxLAH3efbgz5LmGI4o8Aur+ZWbrUVBIBJ6qOnYWeP2Z/WIDsJPzRz8Xw0bjg738vKcvBl X-Received: by 2002:a17:906:c7cf:b0:a19:6a79:2d3f with SMTP id dc15-20020a170906c7cf00b00a196a792d3fmr715736ejb.1.1701851005208; Wed, 06 Dec 2023 00:23:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701851005; cv=none; d=google.com; s=arc-20160816; b=s3GaMuetPuPSKWhOw0jdM6Awk214ENjY0M484NanWxb1p2G967xbl9GuFolVFtTug/ Jm7ku4l++4jUqkuqwyv6IyQbO/7aKW9QhgIFMOudiFgSQOh9g0meHvPwB8zfMRSH4Npq VKbdM6XcLuPkGoT7F5JFgg8xwGg13JKhSwlhg1s08pXNS6hiR2QdDSomWIeHhhG6EQKt kgPhyrvPMcYj8uP0D6+Vi2c50sRVNm6JYYLnLCYN68DGbc2aXwuHm58puvrn527p4ukO 0lOGEhKX5t1GTRBqYKdGTGz6/Z5H1dv2m/T9bHuV0KZoLR/1V0UJAhHZybyKCfoJHnm2 5dlQ== 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:delivered-to; bh=WhxYM5ckJ/lGtZl4PCFK3uD+jUpwd5bOkenWTPwQkz0=; fh=s+G8Ss4JbeMFkWPSp2LmIH/LXFyBlALRiww1j3FPjHY=; b=LYN6w/zgkbjfUQIdHFTtbr3+xRLP9ApBwmHoeMi9iyULYWjs4y93AimXxolLlOm/U3 lLSrBkDUAPm5yfj0HOEOrQ+8GND5adREwg+l5uFlZ3xaKoJSi5cNi/P2Gv5rXrFctHCp Imxbz8RqeBM4GWoRAZ80zWwEHle1PsXXO0TZElIxNjz0vJzz1dhkRoxoTBNoYCMTUN2S yYBRYRK7qSaxaKaethMTr9+/RJOUYBe1oTRjLABEdl9ALbqWHqahpIGh544LeY3eZM1g b1tlM8DtVA4ydo/H2lnIH5O7JR/PH0DNWZEyOwIQOldmOCjlQCw5w2XV/moO6feTLlz/ NMRQ== ARC-Authentication-Results: i=1; mx.google.com; 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 f23-20020a170906561700b00a0f78bc6480si6459910ejq.934.2023.12.06.00.23.24; Wed, 06 Dec 2023 00:23:25 -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; 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 E05E768CF5D; Wed, 6 Dec 2023 10:22:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3F53C68CEDF for ; Wed, 6 Dec 2023 10:22:33 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 279F5E9A1E; Wed, 6 Dec 2023 09:22:33 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MDP33us6S7uU; Wed, 6 Dec 2023 09:22:31 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 0BE6EE9A25; Wed, 6 Dec 2023 09:22:30 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Dec 2023 09:22:18 +0100 Message-Id: <20231206082220.5532-5-cus@passwd.hu> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231206082220.5532-1-cus@passwd.hu> References: <20231206082220.5532-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 5/7] avutil/imgutils: factorize a fill color function 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: Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 5tQQQ49gBIC9 In preparation for making it public. Signed-off-by: Marton Balint --- libavutil/imgutils.c | 103 +++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c index 67119b0870..278e30ee0f 100644 --- a/libavutil/imgutils.c +++ b/libavutil/imgutils.c @@ -579,30 +579,24 @@ static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear, // if it's a subsampled packed format). #define MAX_BLOCK_SIZE 32 -int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4], - enum AVPixelFormat pix_fmt, enum AVColorRange range, +static int image_fill_color(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4], + enum AVPixelFormat pix_fmt, const uint32_t color[4], int width, int height) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); int nb_planes = av_pix_fmt_count_planes(pix_fmt); - // A pixel or a group of pixels on each plane, with a value that represents black. + // A pixel or a group of pixels on each plane, with a value that represents the color. // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases. uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0 int clear_block_size[4] = {0}; ptrdiff_t plane_line_bytes[4] = {0}; - int rgb, xyz, pal, limited, alpha, bitstream, fltp; + int bitstream; int plane, c; if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) return AVERROR(EINVAL); - rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB); - xyz = !!(desc->flags & AV_PIX_FMT_FLAG_XYZ); - pal = !!(desc->flags & AV_PIX_FMT_FLAG_PAL); - limited = !rgb && !xyz && !pal && range != AVCOL_RANGE_JPEG; - alpha = !pal && !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA); bitstream = !!(desc->flags & AV_PIX_FMT_FLAG_BITSTREAM); - fltp = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT); for (c = 0; c < desc->nb_components; c++) { const AVComponentDescriptor comp = desc->comp[c]; @@ -623,7 +617,6 @@ int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesiz uint8_t *c_data[4]; const int c_linesize[4] = {0}; uint32_t src_array[MAX_BLOCK_SIZE]; - uint32_t src = 0; int x; if (comp.depth > 32) @@ -631,35 +624,8 @@ int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesiz if (w < 1) return AVERROR(EINVAL); - if (pix_fmt == AV_PIX_FMT_MONOWHITE) { - src = 1; - } else if (c + 1 == desc->nb_components && alpha) { - // (Assume even limited YUV uses full range alpha.) - if (fltp) { - if (comp.depth != 16 && comp.depth != 32) - return AVERROR(EINVAL); - src = (comp.depth == 16 ? 0x3C00 : 0x3F800000); // 1.0 - } else { - src = (comp.depth == 32 ? 0 : (1 << comp.depth)) - 1; - } - } else if (c == 0 && limited && comp.depth > 1) { - if (comp.depth < 8 || (fltp && comp.depth != 16 && comp.depth != 32)) - return AVERROR(EINVAL); - if (fltp) - src = (comp.depth == 16 ? 0x3000 : 0x3D800000); // 0.0625 - else - src = 16 << (comp.depth - 8); - } else if ((c == 1 || c == 2) && !rgb && !xyz) { - if (comp.depth < 8 || fltp && comp.depth != 16 && comp.depth != 32) - return AVERROR(EINVAL); - if (fltp) - src = (comp.depth == 16 ? 0x3800 : 0x3F000000); // 0.5 - else - src = 128 << (comp.depth - 8); - } - for (x = 0; x < w; x++) - src_array[x] = src; + src_array[x] = color[c]; for (x = 0; x < 4; x++) c_data[x] = &clear_block[x][0]; @@ -690,3 +656,62 @@ int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesiz return 0; } + +int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4], + enum AVPixelFormat pix_fmt, enum AVColorRange range, + int width, int height) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); + int nb_planes = av_pix_fmt_count_planes(pix_fmt); + int rgb, xyz, pal, limited, alpha, fltp; + uint32_t colors[4] = {0}; + + if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) + return AVERROR(EINVAL); + + rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB); + xyz = !!(desc->flags & AV_PIX_FMT_FLAG_XYZ); + pal = !!(desc->flags & AV_PIX_FMT_FLAG_PAL); + limited = !rgb && !xyz && !pal && range != AVCOL_RANGE_JPEG; + alpha = !pal && !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA); + fltp = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT); + + for (int c = 0; c < desc->nb_components; c++) { + const AVComponentDescriptor comp = desc->comp[c]; + uint32_t color = 0; + + if (comp.depth > 32) + return AVERROR(EINVAL); + + if (pix_fmt == AV_PIX_FMT_MONOWHITE) { + color = 1; + } else if (c + 1 == desc->nb_components && alpha) { + // (Assume even limited YUV uses full range alpha.) + if (fltp) { + if (comp.depth != 16 && comp.depth != 32) + return AVERROR(EINVAL); + color = (comp.depth == 16 ? 0x3C00 : 0x3F800000); // 1.0 + } else { + color = (comp.depth == 32 ? 0 : (1 << comp.depth)) - 1; + } + } else if (c == 0 && limited && comp.depth > 1) { + if (comp.depth < 8 || (fltp && comp.depth != 16 && comp.depth != 32)) + return AVERROR(EINVAL); + if (fltp) + color = (comp.depth == 16 ? 0x3000 : 0x3D800000); // 0.0625 + else + color = 16 << (comp.depth - 8); + } else if ((c == 1 || c == 2) && !rgb && !xyz) { + if (comp.depth < 8 || fltp && comp.depth != 16 && comp.depth != 32) + return AVERROR(EINVAL); + if (fltp) + color = (comp.depth == 16 ? 0x3800 : 0x3F000000); // 0.5 + else + color = 128 << (comp.depth - 8); + } + + colors[c] = color; + } + + return image_fill_color(dst_data, dst_linesize, pix_fmt, colors, width, height); +}