{"id":9683,"url":"https://patchwork.libcamera.org/api/1.1/patches/9683/?format=json","web_url":"https://patchwork.libcamera.org/patch/9683/","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":"<20200922085444.15764-2-email@uajain.com>","date":"2020-09-22T08:54:44","name":"[libcamera-devel,1/1] android: jpeg: Add a basic NV12 image scaler","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"67deaac3c5293ff87579d6725611b08148f2bcd9","submitter":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/people/1/?format=json","name":"Umang Jain","email":"email@uajain.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9683/mbox/","series":[{"id":1305,"url":"https://patchwork.libcamera.org/api/1.1/series/1305/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1305","date":"2020-09-22T08:54:44","name":"[libcamera-devel,1/1] android: jpeg: Add a basic NV12 image scaler","version":1,"mbox":"https://patchwork.libcamera.org/series/1305/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9683/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9683/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 76F54BF01C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 22 Sep 2020 08:55:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 446E762FD6;\n\tTue, 22 Sep 2020 10:55:02 +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 E79C362FAD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 22 Sep 2020 10:55:00 +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=\"ZdS2OrCn\"; 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=1600764900; bh=dWtGyVfNBJkKaH4yOiZN6xeDSymWIdXR0fnJLPttPps=;\n\th=From:To:Cc:Subject:In-Reply-To:References;\n\tb=ZdS2OrCnnkrVpYtMp+vh9/pKv95Lk6K/ogcdfcOilqmvlaFPz22CMwu4bmcfKBLmA\n\tUQLLHQcb1H+CDeuL3oPEAVqWVkmTJe5RezumBiqUiIXi6VjM5PAVOJQg+Ku4AWTSue\n\t7C8FzhUdlptO+XHvzteWKqQe3SnBNPQcYBO5IvkAPxKnYUFbDIY8OJrDqyX0U82LCP\n\tkeT3rBLiYLpm4Fs21krpunJBzLntJCeg7jdzuNg8D81MkFxveFDV0XClNsPBQL0L/D\n\t2RGF8+Dpe9KYFy2F2HJB9sCGs6UlyDcwC/XFiST3noEzj5e45tsCea8wC3cg+yrlpR\n\tEr4wf3OU79z6A==","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 22 Sep 2020 14:24:44 +0530","Message-Id":"<20200922085444.15764-2-email@uajain.com>","In-Reply-To":"<20200922085444.15764-1-email@uajain.com>","References":"<20200922085444.15764-1-email@uajain.com>","Mime-Version":"1.0","Subject":"[libcamera-devel] [PATCH 1/1] android: jpeg: Add a basic NV12 image\n\tscaler","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 scaler for NV12 frames being captured. The primary\nuse of this scaler will be to generate a thumbnail image to be embedded\nas a part of EXIF metadata of the frame.\n\nSigned-off-by: Umang Jain <email@uajain.com>\n---\n src/android/jpeg/encoder_libjpeg.cpp |  10 ++\n src/android/jpeg/scaler.cpp          | 137 +++++++++++++++++++++++++++\n src/android/jpeg/scaler.h            |  32 +++++++\n src/android/meson.build              |   1 +\n 4 files changed, 180 insertions(+)\n create mode 100644 src/android/jpeg/scaler.cpp\n create mode 100644 src/android/jpeg/scaler.h","diff":"diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp\nindex 510613c..9ecf9b1 100644\n--- a/src/android/jpeg/encoder_libjpeg.cpp\n+++ b/src/android/jpeg/encoder_libjpeg.cpp\n@@ -6,6 +6,7 @@\n  */\n \n #include \"encoder_libjpeg.h\"\n+#include \"scaler.h\"\n \n #include <fcntl.h>\n #include <iomanip>\n@@ -214,6 +215,15 @@ int EncoderLibJpeg::encode(const FrameBuffer *source,\n \tLOG(JPEG, Debug) << \"JPEG Encode Starting:\" << compress_.image_width\n \t\t\t << \"x\" << compress_.image_height;\n \n+\tScaler scaler;\n+\tlibcamera::Span<uint8_t> thumbnail;\n+\tscaler.configure(Size (compress_.image_width, compress_.image_height),\n+\t\t\t /* \\todo: Check for exact thumbnail size required by EXIF spec. */\n+\t\t         Size (compress_.image_width / 3, compress_.image_height / 3),\n+\t\t\t pixelFormatInfo_);\n+\tscaler.scaleBuffer(source, thumbnail);\n+\t/* \\todo: Write thumbnail as part of exifData. */\n+\n \tif (nv_)\n \t\tcompressNV(&frame);\n \telse\ndiff --git a/src/android/jpeg/scaler.cpp b/src/android/jpeg/scaler.cpp\nnew file mode 100644\nindex 0000000..ff36ece\n--- /dev/null\n+++ b/src/android/jpeg/scaler.cpp\n@@ -0,0 +1,137 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * scaler.cpp - Basic image scaler from NV12 to RGB24 format\n+ */\n+\n+#include \"scaler.h\"\n+\n+#include <math.h>\n+#include <string.h>\n+\n+#include \"libcamera/internal/log.h\"\n+#include \"libcamera/internal/file.h\"\n+\n+#define RGBSHIFT                8\n+#ifndef MAX\n+#define MAX(a,b)                ((a)>(b)?(a):(b))\n+#endif\n+#ifndef MIN\n+#define MIN(a,b)                ((a)<(b)?(a):(b))\n+#endif\n+#ifndef CLAMP\n+#define CLAMP(a,low,high)       MAX((low),MIN((high),(a)))\n+#endif\n+#ifndef CLIP\n+#define CLIP(x)                 CLAMP(x,0,255)\n+#endif\n+\n+using namespace libcamera;\n+\n+LOG_DEFINE_CATEGORY(SCALER)\n+\n+Scaler::Scaler()\n+\t: sourceSize_(0, 0), targetSize_(0,0)\n+{\n+}\n+\n+Scaler::~Scaler()\n+{\n+}\n+\n+void Scaler::configure(Size sourceSize, Size targetSize, const PixelFormatInfo *pixelFormatInfo)\n+{\n+\tsourceSize_ = sourceSize;\n+\ttargetSize_ = targetSize;\n+\tpixelFormatInfo_ = pixelFormatInfo;\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+/* Handpicked from src/qcam/format_converter.cpp */\n+static void yuv_to_rgb(int y, int u, int v, int *r, int *g, int *b)\n+{\n+\tint c = y - 16;\n+\tint d = u - 128;\n+\tint e = v - 128;\n+\t*r = CLIP(( 298 * c           + 409 * e + 128) >> RGBSHIFT);\n+\t*g = CLIP(( 298 * c - 100 * d - 208 * e + 128) >> RGBSHIFT);\n+\t*b = CLIP(( 298 * c + 516 * d           + 128) >> RGBSHIFT);\n+}\n+\n+int Scaler::scaleBuffer(const FrameBuffer *source, Span<uint8_t> &dest)\n+{\n+\tMappedFrameBuffer frame(source, PROT_READ);\n+\tif (!frame.isValid()) {\n+\t\tLOG(SCALER, Error) << \"Failed to map FrameBuffer : \"\n+\t\t\t\t   << strerror(frame.error());\n+\t\treturn frame.error();\n+\t}\n+\n+\tif (strcmp(pixelFormatInfo_->name, \"NV12\") != 0) {\n+\t\tLOG (SCALER, Info) << \"Source Buffer not in NV12 format, returning...\";\n+\t\treturn -1;\n+\t}\n+\n+\t/* Image scaling block implementing nearest-neighbour algorithm. */\n+\t{\n+\t\tunsigned int cb_pos = 0;\n+\t\tunsigned int cr_pos = 1;\n+\t\tunsigned char *src = static_cast<unsigned char *>(frame.maps()[0].data());\n+\t\tunsigned char *src_c = src + sourceSize_.height * sourceSize_.width;\n+\t\tunsigned int stride = sourceSize_.width;\n+\t\tunsigned char *src_y, *src_cb, *src_cr;\n+\n+\t\tunsigned int bpp = 3;\n+\t\tsize_t dstSize = targetSize_.height * targetSize_.width * bpp;\n+\t\tunsigned char *dst = static_cast<unsigned char *>(malloc(dstSize));\n+\t\tunsigned char *destination = dst;\n+\t\tint r, g, b;\n+\n+\t\tfor (unsigned int y = 0; y < targetSize_.height; y++) {\n+\t\t\tint32_t sourceY = lround(sourceSize_.height * y / targetSize_.height);\n+\n+\t\t\tfor (unsigned int x = 0; x < targetSize_.width; x++) {\n+\t\t\t\tint32_t sourceX = lround(sourceSize_.width * x / targetSize_.width);\n+\n+\t\t\t\tsrc_y = src + stride * sourceY + sourceX;\n+\t\t\t\tsrc_cb = src_c + (sourceY / 2) * stride + (sourceX / 2) * 2 + cb_pos;\n+\t\t\t\tsrc_cr = src_c + (sourceY / 2) * stride + (sourceX / 2) * 2 + cr_pos;\n+\n+\t\t\t\tyuv_to_rgb(*src_y, *src_cb, *src_cr, &r, &g, &b);\n+\n+\t\t\t\tdestination[x * bpp + 0] = r;\n+\t\t\t\tdestination[x * bpp + 1] = g;\n+\t\t\t\tdestination[x * bpp + 2] = b;\n+\t\t\t}\n+\n+\t\t\tdestination = destination + bpp * targetSize_.width;\n+\t\t}\n+\n+\t\t/* Helper code: Write the output pixels to a file so we can inspect */\n+\t\tFile file(\"/tmp/\" + datetime() + \".raw\");\n+\t\tint32_t ret = file.open(File::WriteOnly);\n+\t\tret = file.write({ dst, dstSize });\n+\t\tLOG(SCALER, Info) << \"Wrote \" << ret << \" bytes: \" << targetSize_.width << \"x\" << targetSize_.height;\n+\n+\t\t/* Write scaled pixels to dest */\n+\t\tdest = { dst, dstSize };\n+\t}\n+\n+\treturn 0;\n+}\ndiff --git a/src/android/jpeg/scaler.h b/src/android/jpeg/scaler.h\nnew file mode 100644\nindex 0000000..c68a279\n--- /dev/null\n+++ b/src/android/jpeg/scaler.h\n@@ -0,0 +1,32 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * Basic image scaler from NV12 to RGB24 format\n+ */\n+#ifndef __ANDROID_JPEG_SCALER_H__\n+#define __ANDROID_JPEG_SCALER_H__\n+\n+#include <libcamera/geometry.h>\n+\n+#include \"libcamera/internal/buffer.h\"\n+#include \"libcamera/internal/formats.h\"\n+\n+class Scaler\n+{\n+public:\n+\tScaler();\n+\t~Scaler();\n+\n+\tvoid configure(libcamera::Size sourceSize, libcamera::Size targetSize,\n+\t\t       const libcamera::PixelFormatInfo *pixelFormatInfo);\n+\tint scaleBuffer(const libcamera::FrameBuffer *source, libcamera::Span<uint8_t> &dest);\n+\n+private:\n+\n+\tlibcamera::Size sourceSize_;\n+\tlibcamera::Size targetSize_;\n+\tconst libcamera::PixelFormatInfo *pixelFormatInfo_;\n+};\n+\n+#endif /* __ANDROID_JPEG_SCALER_H__ */\ndiff --git a/src/android/meson.build b/src/android/meson.build\nindex 0293c20..aefb0da 100644\n--- a/src/android/meson.build\n+++ b/src/android/meson.build\n@@ -22,6 +22,7 @@ android_hal_sources = files([\n     'camera_ops.cpp',\n     'jpeg/encoder_libjpeg.cpp',\n     'jpeg/exif.cpp',\n+    'jpeg/scaler.cpp',\n ])\n \n android_camera_metadata_sources = files([\n","prefixes":["libcamera-devel","1/1"]}