Patch Detail
Show a patch.
GET /api/1.1/patches/9205/?format=api
{ "id": 9205, "url": "https://patchwork.libcamera.org/api/1.1/patches/9205/?format=api", "web_url": "https://patchwork.libcamera.org/patch/9205/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20200804214711.177645-13-kieran.bingham@ideasonboard.com>", "date": "2020-08-04T21:47:10", "name": "[libcamera-devel,v3,12/13] android: Introduce JPEG compression", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "ade4a86874c7c66058779459f321f2ac81fedd9b", "submitter": { "id": 4, "url": "https://patchwork.libcamera.org/api/1.1/people/4/?format=api", "name": "Kieran Bingham", "email": "kieran.bingham@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/9205/mbox/", "series": [ { "id": 1191, "url": "https://patchwork.libcamera.org/api/1.1/series/1191/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1191", "date": "2020-08-04T21:46:58", "name": "android: JPEG support", "version": 3, "mbox": "https://patchwork.libcamera.org/series/1191/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/9205/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/9205/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 5A581BD87A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 4 Aug 2020 21:47:26 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 27FB3605AD;\n\tTue, 4 Aug 2020 23:47:26 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4349860580\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 4 Aug 2020 23:47:21 +0200 (CEST)", "from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net\n\t[86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BBB12D6E;\n\tTue, 4 Aug 2020 23:47:20 +0200 (CEST)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"laRgQzFD\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1596577641;\n\tbh=Dj0d6ykbvzP8nbjRPjQMTKtOTv7C+W0PjYil6v4gahI=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=laRgQzFDOXItSjWutDosRuTMXA0jQTZSg1mM1bDPVaQfwu+Gra21TX30W+m6BnJeH\n\tgDDP4wXpbc4k+H20qaN3VaSoFzDdsQ/EwX+WV4PdziKx14cAmQz2tY82ejDkrGXbRr\n\tcKGm+lTvCxl5nYDy7MyuZQctw+ld8hRzbsXIlqRA=", "From": "Kieran Bingham <kieran.bingham@ideasonboard.com>", "To": "libcamera devel <libcamera-devel@lists.libcamera.org>", "Date": "Tue, 4 Aug 2020 22:47:10 +0100", "Message-Id": "<20200804214711.177645-13-kieran.bingham@ideasonboard.com>", "X-Mailer": "git-send-email 2.25.1", "In-Reply-To": "<20200804214711.177645-1-kieran.bingham@ideasonboard.com>", "References": "<20200804214711.177645-1-kieran.bingham@ideasonboard.com>", "MIME-Version": "1.0", "Subject": "[libcamera-devel] [PATCH v3 12/13] android: Introduce JPEG\n\tcompression", "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": "Provide a compressor interface and implement a JPEG compressor using\nlibjpeg.\n\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nv2:\n\n - Convert to use the libcamera format information rather than duplicating it ourselves.\n - Not easy to get the horizontal subsampling\n - not easy to determine if we have an nvSwap...\n\nv3:\n - Fix frame.error return value (negative inversion)\n - Fix comments\n - constify data table\n - set jpeg image lenght to zero after free\n - set override on class interface functions.\n - Remove ununsed fucntion prototype\n - Rename pixelFormatInfo to pixelFormatInfo_\n---\n src/android/jpeg/compressor.h | 28 ++++\n src/android/jpeg/compressor_jpeg.cpp | 217 +++++++++++++++++++++++++++\n src/android/jpeg/compressor_jpeg.h | 45 ++++++\n src/android/meson.build | 1 +\n src/libcamera/meson.build | 2 +\n 5 files changed, 293 insertions(+)\n create mode 100644 src/android/jpeg/compressor.h\n create mode 100644 src/android/jpeg/compressor_jpeg.cpp\n create mode 100644 src/android/jpeg/compressor_jpeg.h", "diff": "diff --git a/src/android/jpeg/compressor.h b/src/android/jpeg/compressor.h\nnew file mode 100644\nindex 000000000000..18d8f65eba02\n--- /dev/null\n+++ b/src/android/jpeg/compressor.h\n@@ -0,0 +1,28 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * compressor.h - Image compression interface\n+ */\n+#ifndef __ANDROID_JPEG_COMPRESSOR_H__\n+#define __ANDROID_JPEG_COMPRESSOR_H__\n+\n+#include <libcamera/buffer.h>\n+#include <libcamera/stream.h>\n+\n+struct CompressedImage {\n+\tunsigned char *data;\n+\tunsigned long length;\n+};\n+\n+class Compressor\n+{\n+public:\n+\tvirtual ~Compressor(){};\n+\n+\tvirtual int configure(const libcamera::StreamConfiguration &cfg) = 0;\n+\tvirtual int compress(const libcamera::FrameBuffer *source, CompressedImage *image) = 0;\n+\tvirtual void free(CompressedImage *image) = 0;\n+};\n+\n+#endif /* __ANDROID_JPEG_COMPRESSOR_H__ */\ndiff --git a/src/android/jpeg/compressor_jpeg.cpp b/src/android/jpeg/compressor_jpeg.cpp\nnew file mode 100644\nindex 000000000000..9921a294128c\n--- /dev/null\n+++ b/src/android/jpeg/compressor_jpeg.cpp\n@@ -0,0 +1,217 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * compressor_jpeg.cpp - JPEG compression using libjpeg native API\n+ */\n+\n+#include \"compressor_jpeg.h\"\n+\n+#include <fcntl.h>\n+#include <iomanip>\n+#include <iostream>\n+#include <sstream>\n+#include <string.h>\n+#include <sys/mman.h>\n+#include <unistd.h>\n+#include <vector>\n+\n+#include <libcamera/camera.h>\n+#include <libcamera/formats.h>\n+#include <libcamera/pixel_format.h>\n+\n+#include \"libcamera/internal/formats.h\"\n+#include \"libcamera/internal/log.h\"\n+\n+using namespace libcamera;\n+\n+LOG_DEFINE_CATEGORY(JPEG)\n+\n+namespace {\n+\n+struct JPEGPixelFormatInfo {\n+\tJ_COLOR_SPACE colorSpace;\n+\tconst PixelFormatInfo &pixelFormatInfo;\n+\tbool nvSwap;\n+};\n+\n+const std::map<PixelFormat, JPEGPixelFormatInfo> pixelInfo{\n+\t{ formats::R8, { JCS_GRAYSCALE, PixelFormatInfo::info(formats::R8), false } },\n+\n+\t{ formats::RGB888, { JCS_EXT_BGR, PixelFormatInfo::info(formats::RGB888), false } },\n+\t{ formats::BGR888, { JCS_EXT_RGB, PixelFormatInfo::info(formats::BGR888), false } },\n+\n+\t{ formats::NV12, { JCS_YCbCr, PixelFormatInfo::info(formats::NV12), false } },\n+\t{ formats::NV21, { JCS_YCbCr, PixelFormatInfo::info(formats::NV21), true } },\n+\t{ formats::NV16, { JCS_YCbCr, PixelFormatInfo::info(formats::NV16), false } },\n+\t{ formats::NV61, { JCS_YCbCr, PixelFormatInfo::info(formats::NV61), true } },\n+\t{ formats::NV24, { JCS_YCbCr, PixelFormatInfo::info(formats::NV24), false } },\n+\t{ formats::NV42, { JCS_YCbCr, PixelFormatInfo::info(formats::NV42), true } },\n+};\n+\n+const struct JPEGPixelFormatInfo &findPixelInfo(const PixelFormat &format)\n+{\n+\tstatic const struct JPEGPixelFormatInfo invalidPixelFormat {\n+\t\tJCS_UNKNOWN, PixelFormatInfo(), false\n+\t};\n+\n+\tconst auto iter = pixelInfo.find(format);\n+\tif (iter == pixelInfo.end()) {\n+\t\tLOG(JPEG, Error) << \"Unsupported pixel format for JPEG compressor: \"\n+\t\t\t\t << format.toString();\n+\t\treturn invalidPixelFormat;\n+\t}\n+\n+\treturn iter->second;\n+}\n+\n+} /* namespace */\n+\n+CompressorJPEG::CompressorJPEG()\n+\t: quality_(95)\n+{\n+\t/* \\todo: Expand error handling coverage with a custom handler. */\n+\tcompress_.err = jpeg_std_error(&jerr_);\n+\n+\tjpeg_create_compress(&compress_);\n+}\n+\n+CompressorJPEG::~CompressorJPEG()\n+{\n+\tjpeg_destroy_compress(&compress_);\n+}\n+\n+int CompressorJPEG::configure(const StreamConfiguration &cfg)\n+{\n+\tconst struct JPEGPixelFormatInfo info = findPixelInfo(cfg.pixelFormat);\n+\tif (info.colorSpace == JCS_UNKNOWN)\n+\t\treturn -ENOTSUP;\n+\n+\tcompress_.image_width = cfg.size.width;\n+\tcompress_.image_height = cfg.size.height;\n+\tcompress_.in_color_space = info.colorSpace;\n+\n+\tcompress_.input_components = info.colorSpace == JCS_GRAYSCALE ? 1 : 3;\n+\n+\tjpeg_set_defaults(&compress_);\n+\tjpeg_set_quality(&compress_, quality_, TRUE);\n+\n+\tpixelFormatInfo_ = &info.pixelFormatInfo;\n+\n+\tnv_ = pixelFormatInfo_->numPlanes() == 2;\n+\tnvSwap_ = info.nvSwap;\n+\n+\treturn 0;\n+}\n+\n+void CompressorJPEG::compressRGB(const libcamera::MappedBuffer *frame)\n+{\n+\tunsigned char *src = static_cast<unsigned char *>(frame->maps()[0].data());\n+\tunsigned int stride = pixelFormatInfo_->stride(compress_.image_width, 0);\n+\n+\tJSAMPROW row_pointer[1];\n+\n+\twhile (compress_.next_scanline < compress_.image_height) {\n+\t\trow_pointer[0] = &src[compress_.next_scanline * stride];\n+\t\tjpeg_write_scanlines(&compress_, row_pointer, 1);\n+\t}\n+}\n+\n+/*\n+ * Compress the incoming buffer from a supported NV format.\n+ * This naively unpacks the semi-planar NV12 to a YUV888 format for libjpeg.\n+ * Utilisation of the RAW api will be implemented on top as a performance\n+ * improvement.\n+ */\n+void CompressorJPEG::compressNV(const libcamera::MappedBuffer *frame)\n+{\n+\tstd::vector<uint8_t> tmprowbuf(compress_.image_width * 3);\n+\n+\t/*\n+\t * \\todo Use the raw api, and only unpack the cb/cr samples to new line\n+\t * buffers. If possible, see if we can set appropriate pixel strides\n+\t * too to save even that copy.\n+\t *\n+\t * Possible hints at:\n+\t * https://sourceforge.net/p/libjpeg/mailman/message/30815123/\n+\t */\n+\tunsigned int y_stride = pixelFormatInfo_->stride(compress_.image_width, 0);\n+\tunsigned int c_stride = pixelFormatInfo_->stride(compress_.image_width, 1);\n+\n+\tunsigned int horzSubSample = 2 * compress_.image_width / c_stride;\n+\tunsigned int vertSubSample = pixelFormatInfo_->planes[1].verticalSubSampling;\n+\n+\tunsigned int c_inc = horzSubSample == 1 ? 2 : 0;\n+\tunsigned int cb_pos = nvSwap_ ? 1 : 0;\n+\tunsigned int cr_pos = nvSwap_ ? 0 : 1;\n+\n+\tconst unsigned char *src = static_cast<unsigned char *>(frame->maps()[0].data());\n+\tconst unsigned char *src_c = src + y_stride * compress_.image_height;\n+\n+\tJSAMPROW row_pointer[1];\n+\trow_pointer[0] = &tmprowbuf[0];\n+\n+\tfor (unsigned int y = 0; y < compress_.image_height; y++) {\n+\t\tunsigned char *dst = &tmprowbuf[0];\n+\n+\t\tconst unsigned char *src_y = src + y * compress_.image_width;\n+\t\tconst unsigned char *src_cb = src_c + (y / vertSubSample) * c_stride + cb_pos;\n+\t\tconst unsigned char *src_cr = src_c + (y / vertSubSample) * c_stride + cr_pos;\n+\n+\t\tfor (unsigned int x = 0; x < compress_.image_width; x += 2) {\n+\t\t\tdst[0] = *src_y;\n+\t\t\tdst[1] = *src_cb;\n+\t\t\tdst[2] = *src_cr;\n+\t\t\tsrc_y++;\n+\t\t\tsrc_cb += c_inc;\n+\t\t\tsrc_cr += c_inc;\n+\t\t\tdst += 3;\n+\n+\t\t\tdst[0] = *src_y;\n+\t\t\tdst[1] = *src_cb;\n+\t\t\tdst[2] = *src_cr;\n+\t\t\tsrc_y++;\n+\t\t\tsrc_cb += 2;\n+\t\t\tsrc_cr += 2;\n+\t\t\tdst += 3;\n+\t\t}\n+\n+\t\tjpeg_write_scanlines(&compress_, row_pointer, 1);\n+\t}\n+}\n+\n+int CompressorJPEG::compress(const FrameBuffer *source, CompressedImage *jpeg)\n+{\n+\tMappedFrameBuffer frame(source, PROT_READ);\n+\tif (!frame.isValid()) {\n+\t\tLOG(JPEG, Error) << \"Failed to map FrameBuffer : \"\n+\t\t\t\t << strerror(frame.error());\n+\t\treturn frame.error();\n+\t}\n+\n+\tjpeg_mem_dest(&compress_, &jpeg->data, &jpeg->length);\n+\n+\tjpeg_start_compress(&compress_, TRUE);\n+\n+\tLOG(JPEG, Debug) << \"JPEG Compress Starting:\"\n+\t\t\t << \" Width: \" << compress_.image_width\n+\t\t\t << \" height: \" << compress_.image_height;\n+\n+\tif (nv_)\n+\t\tcompressNV(&frame);\n+\telse\n+\t\tcompressRGB(&frame);\n+\n+\tLOG(JPEG, Debug) << \"JPEG Compress Completed\";\n+\n+\tjpeg_finish_compress(&compress_);\n+\n+\treturn 0;\n+}\n+\n+void CompressorJPEG::free(CompressedImage *jpeg)\n+{\n+\t::free(jpeg->data);\n+\tjpeg->data = nullptr;\n+\tjpeg->length = 0;\n+}\ndiff --git a/src/android/jpeg/compressor_jpeg.h b/src/android/jpeg/compressor_jpeg.h\nnew file mode 100644\nindex 000000000000..2284e8a6bff1\n--- /dev/null\n+++ b/src/android/jpeg/compressor_jpeg.h\n@@ -0,0 +1,45 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * compressor_jpeg.h - JPEG compression using libjpeg\n+ */\n+#ifndef __ANDROID_JPEG_COMPRESSOR_JPEG_H__\n+#define __ANDROID_JPEG_COMPRESSOR_JPEG_H__\n+\n+#include \"compressor.h\"\n+\n+#include <libcamera/buffer.h>\n+#include <libcamera/stream.h>\n+\n+#include \"libcamera/internal/buffer.h\"\n+#include \"libcamera/internal/formats.h\"\n+\n+#include <jpeglib.h>\n+\n+class CompressorJPEG : public Compressor\n+{\n+public:\n+\tCompressorJPEG();\n+\t~CompressorJPEG();\n+\n+\tint configure(const libcamera::StreamConfiguration &cfg) override;\n+\tint compress(const libcamera::FrameBuffer *source, CompressedImage *jpeg) override;\n+\tvoid free(CompressedImage *jpeg) override;\n+\n+private:\n+\tvoid compressRGB(const libcamera::MappedBuffer *frame);\n+\tvoid compressNV(const libcamera::MappedBuffer *frame);\n+\n+\tstruct jpeg_compress_struct compress_;\n+\tstruct jpeg_error_mgr jerr_;\n+\n+\tunsigned int quality_;\n+\n+\tconst libcamera::PixelFormatInfo *pixelFormatInfo_;\n+\n+\tbool nv_;\n+\tbool nvSwap_;\n+};\n+\n+#endif /* __ANDROID_JPEG_COMPRESSOR_JPEG_H__ */\ndiff --git a/src/android/meson.build b/src/android/meson.build\nindex 822cad621f01..51dcd99ee62f 100644\n--- a/src/android/meson.build\n+++ b/src/android/meson.build\n@@ -6,6 +6,7 @@ android_hal_sources = files([\n 'camera_device.cpp',\n 'camera_metadata.cpp',\n 'camera_ops.cpp',\n+ 'jpeg/compressor_jpeg.cpp',\n ])\n \n android_camera_metadata_sources = files([\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 3aad4386ffc2..d78e2c1f6eb8 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -124,6 +124,8 @@ if get_option('android')\n libcamera_sources += android_hal_sources\n includes += android_includes\n libcamera_link_with += android_camera_metadata\n+\n+ libcamera_deps += dependency('libjpeg')\n endif\n \n # We add '/' to the build_rpath as a 'safe' path to act as a boolean flag.\n", "prefixes": [ "libcamera-devel", "v3", "12/13" ] }