From patchwork Fri May 3 11:47:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 10928367 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ED50914B6 for ; Fri, 3 May 2019 11:47:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB568284A5 for ; Fri, 3 May 2019 11:47:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF034284DC; Fri, 3 May 2019 11:47:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 62934284A5 for ; Fri, 3 May 2019 11:47:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fQI7xYFX/IxrRBOSalkpTI1qQBZImTQxppJQoZVNgNA=; b=Ii9HTWWH8eqHw8 Ehm5l07EWyAOIlxGQwprOBKPKmZ0ovyLIw4HvjqRMAZoFmILYHvz2Odxw1nSupASrMEChPehi+onV beJnelykMblFenjX2phfiudGe68GCwaFDvw64vujwAZWtvWE9kgmZ96RCjxD0L3VGE3Yhcr7ewNld JtH9KQEtgBeNeLxzgEHHGkBmbuiaKfosTN+7Ma2oV1xzuLHjv/ANu9HSZRHxO3m8bnWXzlHVcJ/+R YE5DX5bwJy33Hpzfvdme82fah2ToQMWe76CVg3ehGqz81B9EWl9FsfWj6SBCH3tBX0ebbW3Ic9SGB 7h700EwwwzoZjcpxEwKA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hMWfA-0002sj-BS; Fri, 03 May 2019 11:47:36 +0000 Received: from bhuna.collabora.co.uk ([46.235.227.227]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hMWf1-0002i2-AD for linux-rockchip@lists.infradead.org; Fri, 03 May 2019 11:47:30 +0000 Received: from localhost.localdomain (unknown [IPv6:2a01:e0a:2c:6930:5cf4:84a1:2763:fe0d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: bbrezillon) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 802D628361A; Fri, 3 May 2019 12:47:24 +0100 (BST) From: Boris Brezillon To: Mauro Carvalho Chehab , Hans Verkuil , Laurent Pinchart , Sakari Ailus , linux-media@vger.kernel.org Subject: [PATCH v5 03/15] media: v4l2-common: Support custom imagesize/bytesperline in fill_pixfmt() Date: Fri, 3 May 2019 13:47:07 +0200 Message-Id: <20190503114719.28784-4-boris.brezillon@collabora.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190503114719.28784-1-boris.brezillon@collabora.com> References: <20190503114719.28784-1-boris.brezillon@collabora.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190503_044727_618650_F817E494 X-CRM114-Status: GOOD ( 14.19 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tomasz Figa , Heiko Stuebner , Jonas Karlman , Nicolas Dufresne , Paul Kocialkowski , linux-rockchip@lists.infradead.org, Boris Brezillon , kernel@collabora.com, Ezequiel Garcia Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Users can define custom sizeimage and bytesperline as long as they're big enough to store the amount of pixels required for a specific width/height under a specific format. Avoid overriding those fields in this case. Signed-off-by: Boris Brezillon --- Hello Hans, The sizeimage/bytesperline check on !MPLANE formats is still not 100% sure, as custom bytesperline might induce bigger sizeimage than what we calculate. I tried implementing something smarter taking the per-component plane bpp + hdiv param as we discussed the other day but decided to step back after realizing the per-component plane macro block might also differ at least in theory (not sure that's true in practice) and that has an impact on bytesperline too. Let me know how you want to handle that case. Regards, Boris Changes in v5: * New patch --- drivers/media/v4l2-core/v4l2-common.c | 54 +++++++++++++++++++-------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c index 3c6f5c115fc5..37bfc984a8b5 100644 --- a/drivers/media/v4l2-core/v4l2-common.c +++ b/drivers/media/v4l2-core/v4l2-common.c @@ -563,9 +563,10 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, pixfmt->num_planes = info->mem_planes; if (info->mem_planes == 1) { + u32 bytesperline, sizeimage = 0; + plane = &pixfmt->plane_fmt[0]; - plane->bytesperline = ALIGN(width, v4l2_format_block_width(info, 0)) * info->bpp[0]; - plane->sizeimage = 0; + bytesperline = ALIGN(width, v4l2_format_block_width(info, 0)) * info->bpp[0]; for (i = 0; i < info->comp_planes; i++) { unsigned int hdiv = (i == 0) ? 1 : info->hdiv; @@ -576,10 +577,17 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, aligned_width = ALIGN(width, v4l2_format_block_width(info, i)); aligned_height = ALIGN(height, v4l2_format_block_height(info, i)); - plane->sizeimage += info->bpp[i] * - DIV_ROUND_UP(aligned_width, hdiv) * - DIV_ROUND_UP(aligned_height, vdiv); + sizeimage += info->bpp[i] * + DIV_ROUND_UP(aligned_width, hdiv) * + DIV_ROUND_UP(aligned_height, vdiv); } + + /* + * The user might have specified custom sizeimage/bytesperline, + * only override them if they're not big enough. + */ + plane->sizeimage = max(sizeimage, plane->sizeimage); + plane->bytesperline = max(bytesperline, plane->bytesperline); } else { for (i = 0; i < info->comp_planes; i++) { unsigned int hdiv = (i == 0) ? 1 : info->hdiv; @@ -591,10 +599,20 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, aligned_height = ALIGN(height, v4l2_format_block_height(info, i)); plane = &pixfmt->plane_fmt[i]; - plane->bytesperline = - info->bpp[i] * DIV_ROUND_UP(aligned_width, hdiv); - plane->sizeimage = - plane->bytesperline * DIV_ROUND_UP(aligned_height, vdiv); + + /* + * The user might have specified custom + * sizeimage/bytesperline, only override them if + * they're not big enough. + */ + plane->bytesperline = max_t(u32, + info->bpp[i] * + DIV_ROUND_UP(aligned_width, hdiv), + plane->bytesperline); + plane->sizeimage = max_t(u32, + plane->bytesperline * + DIV_ROUND_UP(aligned_height, vdiv), + plane->sizeimage); } } return 0; @@ -605,6 +623,7 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat, u32 width, u32 height) { const struct v4l2_format_info *info; + u32 bytesperline, sizeimage = 0; int i; info = v4l2_format_info(pixelformat); @@ -618,8 +637,7 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat, pixfmt->width = width; pixfmt->height = height; pixfmt->pixelformat = pixelformat; - pixfmt->bytesperline = ALIGN(width, v4l2_format_block_width(info, 0)) * info->bpp[0]; - pixfmt->sizeimage = 0; + bytesperline = ALIGN(width, v4l2_format_block_width(info, 0)) * info->bpp[0]; for (i = 0; i < info->comp_planes; i++) { unsigned int hdiv = (i == 0) ? 1 : info->hdiv; @@ -629,11 +647,17 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat, aligned_width = ALIGN(width, v4l2_format_block_width(info, i)); aligned_height = ALIGN(height, v4l2_format_block_height(info, i)); - - pixfmt->sizeimage += info->bpp[i] * - DIV_ROUND_UP(aligned_width, hdiv) * - DIV_ROUND_UP(aligned_height, vdiv); + sizeimage += info->bpp[i] * DIV_ROUND_UP(aligned_width, hdiv) * + DIV_ROUND_UP(aligned_height, vdiv); } + + /* + * The user might have specified its own sizeimage/bytesperline values, + * only override them if they're not big enough. + */ + pixfmt->sizeimage = max(sizeimage, pixfmt->sizeimage); + pixfmt->bytesperline = max(bytesperline, pixfmt->bytesperline); + return 0; } EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt);