From nobody Fri Oct 24 20:29:26 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8019CC25B0E for ; Mon, 15 Aug 2022 20:59:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244013AbiHOU7g (ORCPT ); Mon, 15 Aug 2022 16:59:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244334AbiHOU6g (ORCPT ); Mon, 15 Aug 2022 16:58:36 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9D08C12D5; Mon, 15 Aug 2022 12:12:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 00A33B810C6; Mon, 15 Aug 2022 19:12:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62830C433D7; Mon, 15 Aug 2022 19:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590751; bh=TBR7tEtI0kFfWKhyk7Qo3n1m3u21I+jSMxM4M25irVY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hGBdjHhtFFksk4y4+i2hCfMWCgjBBBnnqPknx1Ll4a3OQ1FH4TjPGOVBOilr0trs9 1VNpMVywMU34Y1e6YP+nnHzJBTUShWTbpWU8lheBkfjiYA1XX4kB0Ee+K1W9QXelK4 co+O9MKexCoJsQHOlm5GxAtQB702iiwet+SHqSfA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ming Qian , Hans Verkuil , Mauro Carvalho Chehab , Sasha Levin Subject: [PATCH 5.18 0358/1095] media: imx-jpeg: Align upwards buffer size Date: Mon, 15 Aug 2022 19:55:57 +0200 Message-Id: <20220815180444.553100126@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Ming Qian [ Upstream commit 9e7aa76cdb02923ee23a0ddd48f38bdc3512f92b ] The hardware can support any image size WxH, with arbitrary W (image width) and H (image height) dimensions. Align upwards buffer size for both encoder and decoder. and leave the picture resolution unchanged. For decoder, the risk of memory out of bounds can be avoided. For both encoder and decoder, the driver will lift the limitation of resolution alignment. For example, the decoder can support jpeg whose resolution is 227x149 the encoder can support nv12 1080P, won't change it to 1920x1072. Fixes: 2db16c6ed72ce ("media: imx-jpeg: Add V4L2 driver for i.MX8 JPEG Enco= der/Decoder") Signed-off-by: Ming Qian Signed-off-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- .../media/platform/nxp/imx-jpeg/mxc-jpeg.c | 88 ++++++++----------- 1 file changed, 37 insertions(+), 51 deletions(-) diff --git a/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c b/drivers/media= /platform/nxp/imx-jpeg/mxc-jpeg.c index eea03556bec7..04617bf40c51 100644 --- a/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c +++ b/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c @@ -894,8 +894,8 @@ static void mxc_jpeg_config_enc_desc(struct vb2_buffer = *out_buf, jpeg->slot_data[slot].cfg_stream_size =3D mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr, q_data->fmt->fourcc, - q_data->w_adjusted, - q_data->h_adjusted); + q_data->w, + q_data->h); =20 /* chain the config descriptor with the encoding descriptor */ cfg_desc->next_descpt_ptr =3D desc_handle | MXC_NXT_DESCPT_EN; @@ -977,7 +977,7 @@ static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx = *ctx, &q_data_cap->h_adjusted, q_data_cap->h_adjusted, /* adjust up */ MXC_JPEG_MAX_HEIGHT, - q_data_cap->fmt->v_align, + 0, 0); =20 /* setup bytesperline/sizeimage for capture queue */ @@ -1161,16 +1161,28 @@ static int mxc_jpeg_queue_setup(struct vb2_queue *q, { struct mxc_jpeg_ctx *ctx =3D vb2_get_drv_priv(q); struct mxc_jpeg_q_data *q_data =3D NULL; + struct mxc_jpeg_q_data tmp_q; int i; =20 q_data =3D mxc_jpeg_get_q_data(ctx, q->type); if (!q_data) return -EINVAL; =20 + tmp_q.fmt =3D q_data->fmt; + tmp_q.w =3D q_data->w_adjusted; + tmp_q.h =3D q_data->h_adjusted; + for (i =3D 0; i < MXC_JPEG_MAX_PLANES; i++) { + tmp_q.bytesperline[i] =3D q_data->bytesperline[i]; + tmp_q.sizeimage[i] =3D q_data->sizeimage[i]; + } + mxc_jpeg_sizeimage(&tmp_q); + for (i =3D 0; i < MXC_JPEG_MAX_PLANES; i++) + tmp_q.sizeimage[i] =3D max(tmp_q.sizeimage[i], q_data->sizeimage[i]); + /* Handle CREATE_BUFS situation - *nplanes !=3D 0 */ if (*nplanes) { for (i =3D 0; i < *nplanes; i++) { - if (sizes[i] < q_data->sizeimage[i]) + if (sizes[i] < tmp_q.sizeimage[i]) return -EINVAL; } return 0; @@ -1179,7 +1191,7 @@ static int mxc_jpeg_queue_setup(struct vb2_queue *q, /* Handle REQBUFS situation */ *nplanes =3D q_data->fmt->colplanes; for (i =3D 0; i < *nplanes; i++) - sizes[i] =3D q_data->sizeimage[i]; + sizes[i] =3D tmp_q.sizeimage[i]; =20 return 0; } @@ -1379,11 +1391,6 @@ static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, = struct vb2_buffer *vb) } q_data_out->w =3D header.frame.width; q_data_out->h =3D header.frame.height; - if (header.frame.width % 8 !=3D 0 || header.frame.height % 8 !=3D 0) { - dev_err(dev, "JPEG width or height not multiple of 8: %dx%d\n", - header.frame.width, header.frame.height); - return -EINVAL; - } if (header.frame.width > MXC_JPEG_MAX_WIDTH || header.frame.height > MXC_JPEG_MAX_HEIGHT) { dev_err(dev, "JPEG width or height should be <=3D 8192: %dx%d\n", @@ -1695,22 +1702,17 @@ static int mxc_jpeg_try_fmt(struct v4l2_format *f, = const struct mxc_jpeg_fmt *fm pix_mp->num_planes =3D fmt->colplanes; pix_mp->pixelformat =3D fmt->fourcc; =20 - /* - * use MXC_JPEG_H_ALIGN instead of fmt->v_align, for vertical - * alignment, to loosen up the alignment to multiple of 8, - * otherwise NV12-1080p fails as 1080 is not a multiple of 16 - */ + pix_mp->width =3D w; + pix_mp->height =3D h; v4l_bound_align_image(&w, - MXC_JPEG_MIN_WIDTH, - w, /* adjust downwards*/ + w, /* adjust upwards*/ + MXC_JPEG_MAX_WIDTH, fmt->h_align, &h, - MXC_JPEG_MIN_HEIGHT, - h, /* adjust downwards*/ - MXC_JPEG_H_ALIGN, + h, /* adjust upwards*/ + MXC_JPEG_MAX_HEIGHT, + 0, 0); - pix_mp->width =3D w; /* negotiate the width */ - pix_mp->height =3D h; /* negotiate the height */ =20 /* get user input into the tmp_q */ tmp_q.w =3D w; @@ -1836,35 +1838,19 @@ static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx, =20 q_data->w_adjusted =3D q_data->w; q_data->h_adjusted =3D q_data->h; - if (jpeg->mode =3D=3D MXC_JPEG_DECODE) { - /* - * align up the resolution for CAST IP, - * but leave the buffer resolution unchanged - */ - v4l_bound_align_image(&q_data->w_adjusted, - q_data->w_adjusted, /* adjust upwards */ - MXC_JPEG_MAX_WIDTH, - q_data->fmt->h_align, - &q_data->h_adjusted, - q_data->h_adjusted, /* adjust upwards */ - MXC_JPEG_MAX_HEIGHT, - q_data->fmt->v_align, - 0); - } else { - /* - * align down the resolution for CAST IP, - * but leave the buffer resolution unchanged - */ - v4l_bound_align_image(&q_data->w_adjusted, - MXC_JPEG_MIN_WIDTH, - q_data->w_adjusted, /* adjust downwards*/ - q_data->fmt->h_align, - &q_data->h_adjusted, - MXC_JPEG_MIN_HEIGHT, - q_data->h_adjusted, /* adjust downwards*/ - q_data->fmt->v_align, - 0); - } + /* + * align up the resolution for CAST IP, + * but leave the buffer resolution unchanged + */ + v4l_bound_align_image(&q_data->w_adjusted, + q_data->w_adjusted, /* adjust upwards */ + MXC_JPEG_MAX_WIDTH, + q_data->fmt->h_align, + &q_data->h_adjusted, + q_data->h_adjusted, /* adjust upwards */ + MXC_JPEG_MAX_HEIGHT, + q_data->fmt->v_align, + 0); =20 for (i =3D 0; i < pix_mp->num_planes; i++) { q_data->bytesperline[i] =3D pix_mp->plane_fmt[i].bytesperline; --=20 2.35.1