{"id":10024,"url":"https://patchwork.libcamera.org/api/1.1/patches/10024/?format=json","web_url":"https://patchwork.libcamera.org/patch/10024/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20201008141038.83425-4-email@uajain.com>","date":"2020-10-08T14:10:38","name":"[libcamera-devel,RFC,3/3] android: jpeg: Add a basic NV12 image thumbnailer","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ecb5620ecfcdf07ac07c963adc4c1221cf3f3de3","submitter":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/people/1/?format=json","name":"Umang Jain","email":"email@uajain.com"},"delegate":{"id":12,"url":"https://patchwork.libcamera.org/api/1.1/users/12/?format=json","username":"uajain","first_name":"Umang","last_name":"Jain","email":"umang.jain@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/10024/mbox/","series":[{"id":1367,"url":"https://patchwork.libcamera.org/api/1.1/series/1367/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1367","date":"2020-10-08T14:10:35","name":"Introduce PostProcessor Interface for CameraStream","version":1,"mbox":"https://patchwork.libcamera.org/series/1367/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10024/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10024/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 21510BEEE0\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  8 Oct 2020 14:11:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E30B6605BD;\n\tThu,  8 Oct 2020 16:11:05 +0200 (CEST)","from mail.uajain.com (static.126.159.217.95.clients.your-server.de\n\t[95.217.159.126])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 48710605C6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  8 Oct 2020 16:11:05 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=uajain.com header.i=@uajain.com\n\theader.b=\"QDloVlxu\"; dkim-atps=neutral","From":"Umang Jain <email@uajain.com>","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=uajain.com; s=mail;\n\tt=1602166264; bh=+pNVRCEjylQXJmnshAK8AfP4E9dazoNgS0yyDsIketU=;\n\th=From:To:Cc:Subject:In-Reply-To:References;\n\tb=QDloVlxuCY2EdyFg6KlCG1l3TYaEleDMyUVz+E0RDFQ5mOuK+78yrgbmx/aH48ZNj\n\tfLvfnmN3tf1spA6/zSlQ1VDaTMDjMRLmPpzky14YR7dUaT68VDLZTm/KTW3FsaBE6E\n\tqaUUx5di9qSFoR4S7MkB53LKb1U3ebximFiQRHphySk5t2efFSDMkU7tPLtMZJG2Gg\n\t2BJHeNgA8n2SqGmKheBQ+b3t8e3O7jlStAIe5ycXknwsyt7KtV8w+U+XEkq9D7AvnD\n\t2HCqIRLDL8SEDltmJhISL4nMxOmGDL/b3ir8onCSbDPjfuPB7pTzMx43OqgDkyqUyd\n\tEmM6qhMCsVbsw==","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  8 Oct 2020 19:40:38 +0530","Message-Id":"<20201008141038.83425-4-email@uajain.com>","In-Reply-To":"<20201008141038.83425-1-email@uajain.com>","References":"<20201008141038.83425-1-email@uajain.com>","Mime-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH 3/3] android: jpeg: Add a basic NV12\n\timage thumbnailer","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a basic image thumbnailer for NV12 frames being captured.\nIt shall generate a thumbnail image to be embedded as a part of\nEXIF metadata of the frame. The output of the thumbnail will still\nbe NV12.\n\nSigned-off-by: Umang Jain <email@uajain.com>\n---\n src/android/jpeg/post_processor_jpeg.cpp |  11 ++\n src/android/jpeg/thumbnailer.cpp         | 134 +++++++++++++++++++++++\n src/android/jpeg/thumbnailer.h           |  34 ++++++\n src/android/meson.build                  |   1 +\n 4 files changed, 180 insertions(+)\n create mode 100644 src/android/jpeg/thumbnailer.cpp\n create mode 100644 src/android/jpeg/thumbnailer.h","diff":"diff --git a/src/android/jpeg/post_processor_jpeg.cpp b/src/android/jpeg/post_processor_jpeg.cpp\nindex eeb4e95..9076d04 100644\n--- a/src/android/jpeg/post_processor_jpeg.cpp\n+++ b/src/android/jpeg/post_processor_jpeg.cpp\n@@ -8,6 +8,7 @@\n #include \"post_processor_jpeg.h\"\n \n #include \"exif.h\"\n+#include \"thumbnailer.h\"\n \n #include \"../camera_device.h\"\n \n@@ -286,6 +287,16 @@ int PostProcessorJpeg::encode(const FrameBuffer *source,\n \tLOG(JPEG, Debug) << \"JPEG Encode Starting:\" << compress_.image_width\n \t\t\t << \"x\" << compress_.image_height;\n \n+\tThumbnailer th;\n+\tlibcamera::Span<uint8_t> thumbnail;\n+\tth.configure(Size (compress_.image_width, compress_.image_height),\n+\t\t     pixelFormatInfo_->format);\n+\tth.scaleBuffer(source, thumbnail);\n+\t/*\n+\t * \\todo: Compress the thumbnail again encode() and set it in the\n+\t * respective EXIF field.\n+\t */\n+\n \tif (nv_)\n \t\tcompressNV(&frame);\n \telse\ndiff --git a/src/android/jpeg/thumbnailer.cpp b/src/android/jpeg/thumbnailer.cpp\nnew file mode 100644\nindex 0000000..d01b4af\n--- /dev/null\n+++ b/src/android/jpeg/thumbnailer.cpp\n@@ -0,0 +1,134 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * thumbnailer.cpp - Basic image thumbnailer from NV12\n+ */\n+\n+#include \"thumbnailer.h\"\n+\n+#include <libcamera/formats.h>\n+\n+#include \"libcamera/internal/file.h\"\n+#include \"libcamera/internal/log.h\"\n+\n+using namespace libcamera;\n+\n+LOG_DEFINE_CATEGORY(Thumbnailer)\n+\n+Thumbnailer::Thumbnailer()\n+\t: validConfiguration_(false)\n+{\n+}\n+\n+void Thumbnailer::configure(const Size &sourceSize, PixelFormat pixelFormat)\n+{\n+\tsourceSize_ = sourceSize;\n+\tpixelFormat_ = pixelFormat;\n+\n+\tif (pixelFormat_ != formats::NV12) {\n+\t\tLOG (Thumbnailer, Error) << \"Failed to configure: Pixel Format \"\n+\t\t\t\t    << pixelFormat_.toString() << \" unsupported.\";\n+\t\treturn;\n+\t}\n+\n+\tvalidConfiguration_ = true;\n+}\n+\n+static std::string datetime()\n+{\n+\ttime_t rawtime;\n+\tstruct tm *timeinfo;\n+\tchar buffer[80];\n+\tstatic unsigned int milliseconds = 0;\n+\n+\ttime(&rawtime);\n+\ttimeinfo = localtime(&rawtime);\n+\n+\tstrftime(buffer, 80, \"%d-%m-%Y.%H-%M-%S.\", timeinfo);\n+\n+\t/* milliseconds is just a fast hack to ensure unique filenames */\n+\treturn std::string(buffer) + std::to_string(milliseconds++);\n+}\n+\n+/*\n+ * The Exif specification recommends the width of the thumbnail to be a\n+ * mutiple of 16 (section 4.8.1). Hence, compute the corresponding height\n+ * keeping the aspect ratio same as of the source.\n+ */\n+Size Thumbnailer::computeThumbnailSize()\n+{\n+\tunsigned int targetHeight;\n+\tunsigned int targetWidth = 160;\n+\n+\ttargetHeight = targetWidth * sourceSize_.height / sourceSize_.width;\n+\n+\tif (targetHeight & 1)\n+\t\ttargetHeight++;\n+\n+\treturn Size(targetWidth, targetHeight);\n+}\n+\n+int Thumbnailer::scaleBuffer(const FrameBuffer *source, Span<uint8_t> &dest)\n+{\n+\tMappedFrameBuffer frame(source, PROT_READ);\n+\tif (!frame.isValid()) {\n+\t\tLOG(Thumbnailer, Error) << \"Failed to map FrameBuffer : \"\n+\t\t\t\t        << strerror(frame.error());\n+\t\treturn frame.error();\n+\t}\n+\n+\tif (!validConfiguration_) {\n+\t\tLOG(Thumbnailer, Error) << \"config is unconfigured or invalid.\";\n+\t\treturn -1;\n+\t}\n+\n+\ttargetSize_ = computeThumbnailSize();\n+\n+\tconst unsigned int sw = sourceSize_.width;\n+\tconst unsigned int sh = sourceSize_.height;\n+\tconst unsigned int tw = targetSize_.width;\n+\tconst unsigned int th = targetSize_.height;\n+\n+\t/* Image scaling block implementing nearest-neighbour algorithm. */\n+\tunsigned char *src = static_cast<unsigned char *>(frame.maps()[0].data());\n+\tunsigned char *src_c = src + sh * sw;\n+\tunsigned int cb_pos = 0;\n+\tunsigned int cr_pos = 1;\n+\tunsigned char *src_cb, *src_cr;\n+\n+\tsize_t dstSize = (th * tw) + ((th/2) * tw);\n+\tunsigned char *destination = static_cast<unsigned char *>(malloc(dstSize));\n+\tunsigned char *dst = destination;\n+\tunsigned char *dst_c = destination + th * tw;\n+\n+\tfor (unsigned int y = 0; y < th; y+=2) {\n+\t\tunsigned int sourceY = (sh*y + th/2) / th;\n+\n+\t\tsrc_cb = src_c + (sourceY/2) * sw + cb_pos;\n+\t\tsrc_cr = src_c + (sourceY/2) * sw + cr_pos;\n+\n+\t\tfor (unsigned int x = 0; x < tw; x+=2) {\n+\t\t\tunsigned int sourceX = (sw*x + tw/2) / tw;\n+\n+\t\t\tdst[y     * tw + x]     = src[sw * sourceY     + sourceX];\n+\t\t\tdst[(y+1) * tw + x]     = src[sw * (sourceY+1) + sourceX];\n+\t\t\tdst[y     * tw + (x+1)] = src[sw * sourceY     + (sourceX+1)];\n+\t\t\tdst[(y+1) * tw + (x+1)] = src[sw * (sourceY+1) + (sourceX+1)];\n+\n+\t\t\tdst_c[(y/2) * tw + x + cb_pos] = src_cb[(sourceX/2) * 2];\n+\t\t\tdst_c[(y/2) * tw + x + cr_pos] = src_cr[(sourceX/2) * 2];\n+\t\t}\n+\t}\n+\n+\t/* Helper code: Write the output pixels to a file so we can inspect */\n+\tFile file(\"/tmp/\" + datetime() + \".raw\");\n+\tint32_t ret = file.open(File::WriteOnly);\n+\tret = file.write({ destination, dstSize });\n+\tLOG(Thumbnailer, Info) << \"Wrote \" << ret << \" bytes: \" << targetSize_.width << \"x\" << targetSize_.height;\n+\n+\t/* Write scaled pixels to dest */\n+\tdest = { destination, dstSize };\n+\n+\treturn 0;\n+}\ndiff --git a/src/android/jpeg/thumbnailer.h b/src/android/jpeg/thumbnailer.h\nnew file mode 100644\nindex 0000000..af3a194\n--- /dev/null\n+++ b/src/android/jpeg/thumbnailer.h\n@@ -0,0 +1,34 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * thumbnailer.h - Basic image thumbnailer from NV12\n+ */\n+#ifndef __ANDROID_JPEG_THUMBNAILER_H__\n+#define __ANDROID_JPEG_THUMBNAILER_H__\n+\n+#include <libcamera/geometry.h>\n+\n+#include \"libcamera/internal/buffer.h\"\n+#include \"libcamera/internal/formats.h\"\n+\n+class Thumbnailer\n+{\n+public:\n+\tThumbnailer();\n+\n+\tvoid configure(const libcamera::Size &sourceSize,\n+\t\t       libcamera::PixelFormat pixelFormat);\n+\tint scaleBuffer(const libcamera::FrameBuffer *source, libcamera::Span<uint8_t> &dest);\n+\n+private:\n+\tlibcamera::Size computeThumbnailSize();\n+\n+\tlibcamera::PixelFormat pixelFormat_;\n+\tlibcamera::Size sourceSize_;\n+\tlibcamera::Size targetSize_;\n+\n+\tbool validConfiguration_;\n+};\n+\n+#endif /* __ANDROID_JPEG_THUMBNAILER_H__ */\ndiff --git a/src/android/meson.build b/src/android/meson.build\nindex 02b3b47..854005e 100644\n--- a/src/android/meson.build\n+++ b/src/android/meson.build\n@@ -23,6 +23,7 @@ android_hal_sources = files([\n     'camera_stream.cpp',\n     'jpeg/exif.cpp',\n     'jpeg/post_processor_jpeg.cpp',\n+    'jpeg/thumbnailer.cpp',\n ])\n \n android_camera_metadata_sources = files([\n","prefixes":["libcamera-devel","RFC","3/3"]}