[{"id":5102,"web_url":"https://patchwork.libcamera.org/comment/5102/","msgid":"<20200606224250.GK7339@pendragon.ideasonboard.com>","date":"2020-06-06T22:42:50","subject":"Re: [libcamera-devel] [PATCH 2/2] qcam: dng_writer: Add support for\n\tIPU3 Bayer formats","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Sat, Jun 06, 2020 at 05:50:39PM +0200, Niklas Söderlund wrote:\n> Add support for the Bayer formats produced on the IPU3. The format uses\n> a memory layout that is hard to repack and keep the 10-bit sample size,\n> therefore scale the samples to 16-bit when creating the scanlines.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/qcam/dng_writer.cpp | 77 +++++++++++++++++++++++++++++++++++++++++\n>  1 file changed, 77 insertions(+)\n> \n> diff --git a/src/qcam/dng_writer.cpp b/src/qcam/dng_writer.cpp\n> index 9435eeec5438f158..8c128041c37f26fe 100644\n> --- a/src/qcam/dng_writer.cpp\n> +++ b/src/qcam/dng_writer.cpp\n> @@ -83,6 +83,55 @@ void thumbScanlineSBGGRxxP(const FormatInfo &info, void *output,\n>  \t}\n>  }\n>  \n> +void packScanlineIPU3(void *output, const void *input, unsigned int width)\n> +{\n> +\tconst uint8_t *in = static_cast<const uint8_t *>(input);\n> +\tuint16_t *out = static_cast<uint16_t *>(output);\n> +\n> +\t/*\n> +\t * Upsacle the 10-bit format to 16-bit as it's none trivial to pack it\n\ns/Upsacle/Upscale/\ns/none/not/\n\n> +\t * it as 10-bits without gaps.\n\ns/it //\ns/10-bits/10-bit/\n\n> +\t *\n> +\t * \\todo Improve packig to keep the 10-bit sample size.\n\ns/packig/packing/\n\n> +\t */\n\nAs much as I'd like to improve that, I agree it's a nasty one. The only\noption I can think of is a two-pass operation, unpack + pack.\n\n> +\tfor (unsigned int x = 0; x < width; x += 25) {\n\nWhat happens if the width isn't a multiple of 25 pixels ? Will you read\nbeyond the end of the input buffer and/or write beyond the end of the\noutput buffer ?\n\n> +\t\tfor (unsigned int i = 0; i < 6; i++) {\n> +\t\t\t*out++ = ((in[1] & 0x02) << 8 | in[0]) << 6;\n\ns/0x02/0x03/ ?\n\n> +\t\t\t*out++ = ((in[2] & 0x0f) << 6 | in[1] >> 2) << 6;\n> +\t\t\t*out++ = ((in[3] & 0x3f) << 4 | in[2] >> 4) << 6;\n> +\t\t\t*out++ = ((in[4] & 0xff) << 2 | in[3] >> 6) << 6;\n\nI would have written this\n\n\t\t\t*out++ = (in[1] & 0x03) << 14 | (in[0] & 0xff) << 6;\n\t\t\t*out++ = (in[2] & 0x0f) << 12 | (in[1] & 0xfc) << 4;\n\t\t\t*out++ = (in[3] & 0x3f) << 10 | (in[2] & 0xf0) << 2;\n\t\t\t*out++ = (in[4] & 0xff) <<  8 | (in[3] & 0xc0) << 0;\n\nUp to you.\n\n> +\t\t\tin += 5;\n> +\t\t}\n> +\n> +\t\t*out++ = ((in[1] & 0x02) << 8 | in[0]) << 6;\n\ns/0x02/0x03/ here too. Same in thumbScanlineIPU3().\n\n> +\t\tin += 2;\n> +\t}\n> +}\n> +\n> +void thumbScanlineIPU3(const FormatInfo &info, void *output,\n> +\t\t       const void *input, unsigned int width,\n> +\t\t       unsigned int stride)\n> +{\n> +\tconst uint8_t *in = static_cast<const uint8_t *>(input);\n> +\tuint8_t *out = static_cast<uint8_t *>(output);\n> +\n> +\t/* Number of bytes corresponding to 25 pixels. */\n> +\tunsigned int skip = 32;\n> +\n> +\tfor (unsigned int x = 0; x < width; x++) {\n> +\t\tuint8_t val1 = ((in[1] & 0x02) << 8 | in[0]) << 6;\n> +\t\tuint8_t val2 = ((in[2] & 0x0f) << 6 | in[1] >> 2) << 6;\n> +\t\tuint8_t val3 = ((in[stride + 1] & 0x02) << 8 | in[stride + 0]) << 6;\n> +\t\tuint8_t val4 = ((in[stride + 2] & 0x0f) << 6 | in[stride + 1] >> 2) << 6;\n\nHave you checked the contents of the thumbnail ? val[1-4] are all\nuint8_t and you store 16-bit values in them.\n\n> +\t\tuint8_t value = (val1 + val2 + val3 + val4) >> 2;\n> +\n> +\t\t*out++ = value;\n> +\t\t*out++ = value;\n> +\t\t*out++ = value;\n> +\t\tin += skip;\n> +\t}\n> +}\n> +\n>  static const std::map<PixelFormat, FormatInfo> formatInfo = {\n>  \t{ PixelFormat(DRM_FORMAT_SBGGR10, MIPI_FORMAT_MOD_CSI2_PACKED), {\n>  \t\t.bitsPerSample = 10,\n> @@ -140,6 +189,34 @@ static const std::map<PixelFormat, FormatInfo> formatInfo = {\n>  \t\t.thumbDownscaleFactor = 16,\n>  \t\t.thumbScanline = thumbScanlineSBGGRxxP,\n>  \t} },\n> +\t{ PixelFormat(DRM_FORMAT_SBGGR10, IPU3_FORMAT_MOD_PACKED), {\n> +\t\t.bitsPerSample = 16,\n> +\t\t.pattern = { CFAPatternBlue, CFAPatternGreen, CFAPatternGreen, CFAPatternRed },\n> +\t\t.packScanline = packScanlineIPU3,\n> +\t\t.thumbDownscaleFactor = 25,\n\nNot a very nice downscaling factor :-( I think it would be possible for\nthumbScanlineIPU3() to handle a downscaling of 16, and you've nearly\nnerd-sniped me into doing so :-) Would it be difficult to implement if\nwe calculated, for each output pixel, the byte and bit offset of val1\nand val2 ? It could be a simple arithmetical calculation.\n\n> +\t\t.thumbScanline = thumbScanlineIPU3,\n> +\t} },\n> +\t{ PixelFormat(DRM_FORMAT_SGBRG10, IPU3_FORMAT_MOD_PACKED), {\n> +\t\t.bitsPerSample = 16,\n> +\t\t.pattern = { CFAPatternGreen, CFAPatternBlue, CFAPatternRed, CFAPatternGreen },\n> +\t\t.packScanline = packScanlineIPU3,\n> +\t\t.thumbDownscaleFactor = 25,\n> +\t\t.thumbScanline = thumbScanlineIPU3,\n> +\t} },\n> +\t{ PixelFormat(DRM_FORMAT_SGRBG10, IPU3_FORMAT_MOD_PACKED), {\n> +\t\t.bitsPerSample = 16,\n> +\t\t.pattern = { CFAPatternGreen, CFAPatternRed, CFAPatternBlue, CFAPatternGreen },\n> +\t\t.packScanline = packScanlineIPU3,\n> +\t\t.thumbDownscaleFactor = 25,\n> +\t\t.thumbScanline = thumbScanlineIPU3,\n> +\t} },\n> +\t{ PixelFormat(DRM_FORMAT_SRGGB10, IPU3_FORMAT_MOD_PACKED), {\n> +\t\t.bitsPerSample = 16,\n> +\t\t.pattern = { CFAPatternRed, CFAPatternGreen, CFAPatternGreen, CFAPatternBlue },\n> +\t\t.packScanline = packScanlineIPU3,\n> +\t\t.thumbDownscaleFactor = 25,\n> +\t\t.thumbScanline = thumbScanlineIPU3,\n> +\t} },\n>  };\n>  \n>  int DNGWriter::write(const char *filename, const Camera *camera,","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3753E603CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  7 Jun 2020 00:43:09 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A408830D;\n\tSun,  7 Jun 2020 00:43:08 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ITcynUmd\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591483388;\n\tbh=MVZuwjoXRVyN/gQAARE/LP4oC7Nr4NQbLv2BxKMdi/M=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ITcynUmd9s7eWnpm0+m0jwSx6Yv0HuYS46mm7a7V1JW/YxNauvOHozbfQ4jNUSDFt\n\tq9iKwFojsozzkZmh9Xh77a53l+Vr1RNsCQrowDc0wqYEy7QhRt8sWramPuuB+4at9y\n\tPkl8EcKpnKNuzzNg0iVXjotqQNUUVmS4/98xvFeA=","Date":"Sun, 7 Jun 2020 01:42:50 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200606224250.GK7339@pendragon.ideasonboard.com>","References":"<20200606155039.1874519-1-niklas.soderlund@ragnatech.se>\n\t<20200606155039.1874519-3-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200606155039.1874519-3-niklas.soderlund@ragnatech.se>","Subject":"Re: [libcamera-devel] [PATCH 2/2] qcam: dng_writer: Add support for\n\tIPU3 Bayer formats","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Sat, 06 Jun 2020 22:43:09 -0000"}}]