{"id":12285,"url":"https://patchwork.libcamera.org/api/1.1/patches/12285/?format=json","web_url":"https://patchwork.libcamera.org/patch/12285/","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":"<20210514064303.798045-2-paul.elder@ideasonboard.com>","date":"2021-05-14T06:43:01","name":"[libcamera-devel,v5,1/3] android: camera_metadata: Auto-resize CameraMetadata","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"37f788324c458383b175db475b557f4bc256f4a1","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12285/mbox/","series":[{"id":2028,"url":"https://patchwork.libcamera.org/api/1.1/series/2028/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2028","date":"2021-05-14T06:43:00","name":"Auto-resize CameraMetadata","version":5,"mbox":"https://patchwork.libcamera.org/series/2028/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12285/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12285/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 16656C31F6\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 14 May 2021 06:43:26 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CACA668920;\n\tFri, 14 May 2021 08:43:25 +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 0BFD5602B4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 14 May 2021 08:43:25 +0200 (CEST)","from pyrite.rasen.tech (unknown\n\t[IPv6:2400:4051:61:600:2c71:1b79:d06d:5032])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 389EC9F0;\n\tFri, 14 May 2021 08:43:22 +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=\"mcHbrs9F\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1620974604;\n\tbh=KgXsb3qm8FN88kwW+gkYf6fPufr8KbU0EszNGCecN2E=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=mcHbrs9FotIIfh4m3xamhDxi5mbBuoAZA64FQ2rKwm6uzSmXuOuKBdiFfn0Z50WG6\n\tTQ55XtVxLvD7tJdCYZI8eoSm9XtBpAFXEqAnEzrftj95RHbNpl+U1PN0drRs9u4bCq\n\tLwvTfRq4SWnz16qc53R1WYOF4bVqYwa/I3D+c6BY=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 14 May 2021 15:43:01 +0900","Message-Id":"<20210514064303.798045-2-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20210514064303.798045-1-paul.elder@ideasonboard.com>","References":"<20210514064303.798045-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v5 1/3] android: camera_metadata:\n\tAuto-resize CameraMetadata","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=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Previously we had to manually declare the size of CameraMetadata on\nallocation, and its count could not be changed after construction.\nChange CameraMetadata's behavior so that the user can simply add or\nupdate entries, and the CameraMetadata will auto-resize (double the\nsize) as necessary. Also remove everything involved with calculating\nthe initial size for any CameraMetadata instances.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n---\nChanges in v5:\n- add overload for C arrays\n\nChanges in v4:\n- upgrade the second overload to allow STL containers instead of just\n  vector\n  - the main use case I'm imagining is vector and array. C arrays won't\n    work because they don't have .data() and .size() (the latter is the\n    more important one)\n  - libcamera::Span doesn't work because the template engine can't match\n    std::vector/std::array to libcamera::Span\n    - ‘std::vector<int>’ is not derived from ‘libcamera::Span<const T>’\n- return bool from addEntry and updateEntry\n\nChanges in v3:\n- dependency on \"FULL hardware level fixes\" removed\n- split addEntry and updateEntry\n  - i think the lookup on every (merged) addEntry isn't worth it, since\n    the user probably knows if they need to add or update\n- add auto-resize (and corresponding check) to updateEntry as well\n- add the templates and overloads to updateEntry as well\n- increase the default size for the static metadata to be above what we\n  had before\n- valid_ is now used to indicate a failed addEntry/updateEntry in\n  addition to metadata_ == nullptr. this way the user doesn't have to\n  check the return value of every single addEntry/updateEntry call, and\n  can just check valid_ at the end\n- print resize when it happens\n- updateEntry resizes if the new data size is greater than the old data\n\nQuestion: I think on failure of addEntry/updateEntry the metadata could\nstill be \"valid\", it's just that the content isn't what the user\nexpects. Should we keep valid_ as I've repurposed it here in v3, or\nshould we add another flag?\n\nChanges in v2:\n- add overloading of addEntry\n- better logging with the separation of core code vs template code\n- addEntry failure makes the metadata invalid\n\nThe main feature of v2 is the overloading of addEntry. I have three\noverloads and the main one:\n- tag, single piece of data (reference)\n- tag, C array (v5)\n- tag, STL container (v3: s/span/vector/) (v4: s/vector/STL container/)\n- tag, data pointer, count\n- tag, data pointer, count, size of one instance of the data\n\nThe last one is the main one, and is not templated. The first two are\nnice and convenient, because now we can do things like:\n\nuint32_t orientation = 0;\nresultMetadata->addEntry(ANDROID_JPEG_ORIENTATION, orientation);\n\nand\n\nstd::vector<uint32_t> availableStates = { SOME_STATE_1, SOME_STATE_2 };\nmetadata->addEntry(ANDROID_AVAILABLE_STATES, availableStates);\n\nThe third is for when android metadata is copied directly from another\npiece of android metadata. All the old calls are backward-compatible\nusing this overload.\n---\n src/android/camera_device.cpp   | 47 +-----------------\n src/android/camera_device.h     |  1 -\n src/android/camera_metadata.cpp | 84 +++++++++++++++++++++++++++++----\n src/android/camera_metadata.h   | 52 +++++++++++++++++++-\n 4 files changed, 127 insertions(+), 57 deletions(-)","diff":"diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex 7d4d0feb..74f6915c 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -773,43 +773,6 @@ void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks)\n \tcallbacks_ = callbacks;\n }\n \n-std::tuple<uint32_t, uint32_t> CameraDevice::calculateStaticMetadataSize()\n-{\n-\t/*\n-\t * \\todo Keep this in sync with the actual number of entries.\n-\t * Currently: 54 entries, 874 bytes of static metadata\n-\t */\n-\tuint32_t numEntries = 54;\n-\tuint32_t byteSize = 874;\n-\n-\t/*\n-\t * Calculate space occupation in bytes for dynamically built metadata\n-\t * entries.\n-\t *\n-\t * Each stream configuration entry requires 48 bytes:\n-\t * 4 32bits integers for ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS\n-\t * 4 64bits integers for ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS\n-\t */\n-\tbyteSize += streamConfigurations_.size() * 48;\n-\n-\t/*\n-\t * 2 32bits integers for each HAL_PIXEL_FORMAT_BLOB for thumbnail sizes\n-\t * 2 32bits integers for the (0, 0) thumbnail size\n-\t *\n-\t * This is a worst case estimates as different configurations with the\n-\t * same aspect ratio will generate the same size.\n-\t */\n-\tfor (const auto &entry : streamConfigurations_) {\n-\t\tif (entry.androidFormat != HAL_PIXEL_FORMAT_BLOB)\n-\t\t\tcontinue;\n-\n-\t\tbyteSize += 8;\n-\t}\n-\tbyteSize += 8;\n-\n-\treturn std::make_tuple(numEntries, byteSize);\n-}\n-\n /*\n  * Return static information for the camera.\n  */\n@@ -818,15 +781,7 @@ const camera_metadata_t *CameraDevice::getStaticMetadata()\n \tif (staticMetadata_)\n \t\treturn staticMetadata_->get();\n \n-\t/*\n-\t * The here reported metadata are enough to implement a basic capture\n-\t * example application, but a real camera implementation will require\n-\t * more.\n-\t */\n-\tuint32_t numEntries;\n-\tuint32_t byteSize;\n-\tstd::tie(numEntries, byteSize) = calculateStaticMetadataSize();\n-\tstaticMetadata_ = std::make_unique<CameraMetadata>(numEntries, byteSize);\n+\tstaticMetadata_ = std::make_unique<CameraMetadata>(64, 1024);\n \tif (!staticMetadata_->isValid()) {\n \t\tLOG(HAL, Error) << \"Failed to allocate static metadata\";\n \t\tstaticMetadata_.reset();\ndiff --git a/src/android/camera_device.h b/src/android/camera_device.h\nindex 23457e47..ae162a45 100644\n--- a/src/android/camera_device.h\n+++ b/src/android/camera_device.h\n@@ -98,7 +98,6 @@ private:\n \tstd::vector<libcamera::Size>\n \tgetRawResolutions(const libcamera::PixelFormat &pixelFormat);\n \n-\tstd::tuple<uint32_t, uint32_t> calculateStaticMetadataSize();\n \tlibcamera::FrameBuffer *createFrameBuffer(const buffer_handle_t camera3buffer);\n \tvoid notifyShutter(uint32_t frameNumber, uint64_t timestamp);\n \tvoid notifyError(uint32_t frameNumber, camera3_stream_t *stream);\ndiff --git a/src/android/camera_metadata.cpp b/src/android/camera_metadata.cpp\nindex 6f1bcdbe..bf8d2781 100644\n--- a/src/android/camera_metadata.cpp\n+++ b/src/android/camera_metadata.cpp\n@@ -63,11 +63,64 @@ bool CameraMetadata::getEntry(uint32_t tag, camera_metadata_ro_entry_t *entry) c\n \treturn true;\n }\n \n-bool CameraMetadata::addEntry(uint32_t tag, const void *data, size_t count)\n+/*\n+ * \\brief Resize the metadata container, if necessary\n+ * \\param[in] count Number of entries to add to the container\n+ * \\param[in] size Total size of entries to add, in bytes\n+ * \\return True if resize was successful or unnecessary, false otherwise\n+ */\n+bool CameraMetadata::resize(size_t count, size_t size)\n+{\n+\tif (!valid_)\n+\t\treturn false;\n+\n+\tif (!count && !size)\n+\t\treturn true;\n+\n+\tsize_t currentEntryCount = get_camera_metadata_entry_count(metadata_);\n+\tsize_t currentEntryCapacity = get_camera_metadata_entry_capacity(metadata_);\n+\tsize_t newEntryCapacity = currentEntryCapacity < currentEntryCount + count ?\n+\t\t\t\t  currentEntryCapacity * 2 : currentEntryCapacity;\n+\n+\tsize_t currentDataCount = get_camera_metadata_data_count(metadata_);\n+\tsize_t currentDataCapacity = get_camera_metadata_data_capacity(metadata_);\n+\tsize_t newDataCapacity = currentDataCapacity < currentDataCount + size ?\n+\t\t\t\t currentDataCapacity * 2 : currentDataCapacity;\n+\n+\tif (newEntryCapacity > currentEntryCapacity ||\n+\t    newDataCapacity > currentDataCapacity) {\n+\t\tcamera_metadata_t *oldMetadata = metadata_;\n+\t\tmetadata_ = allocate_camera_metadata(newEntryCapacity, newDataCapacity);\n+\t\tif (!metadata_) {\n+\t\t\tmetadata_ = oldMetadata;\n+\t\t\treturn false;\n+\t\t}\n+\n+\t\tLOG(CameraMetadata, Info)\n+\t\t\t<< \"Resized: old entry capacity \" << currentEntryCapacity\n+\t\t\t<< \", old data capacity \" << currentDataCapacity\n+\t\t\t<< \", new entry capacity \" << newEntryCapacity\n+\t\t\t<< \", new data capacity \" << newDataCapacity;\n+\n+\t\tappend_camera_metadata(metadata_, oldMetadata);\n+\t\tfree_camera_metadata(oldMetadata);\n+\t}\n+\n+\treturn true;\n+}\n+\n+bool CameraMetadata::addEntry(uint32_t tag, const void *data, size_t count,\n+\t\t\t      size_t sizeofT)\n {\n \tif (!valid_)\n \t\treturn false;\n \n+\tif (!resize(1, count * sizeofT)) {\n+\t\tLOG(CameraMetadata, Error) << \"Failed to resize\";\n+\t\tvalid_ = false;\n+\t\treturn false;\n+\t}\n+\n \tif (!add_camera_metadata_entry(metadata_, tag, data, count))\n \t\treturn true;\n \n@@ -99,16 +152,31 @@ bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t count)\n \t\treturn false;\n \t}\n \n-\tret = update_camera_metadata_entry(metadata_, entry.index, data,\n-\t\t\t\t\t   count, nullptr);\n-\tif (ret) {\n-\t\tconst char *name = get_camera_metadata_tag_name(tag);\n-\t\tLOG(CameraMetadata, Error)\n-\t\t\t<< \"Failed to update tag \" << (name ? name : \"<unknown>\");\n+\tsize_t oldSize =\n+\t\tcalculate_camera_metadata_entry_data_size(entry.type,\n+\t\t\t\t\t\t\t  entry.count);\n+\tsize_t newSize =\n+\t\tcalculate_camera_metadata_entry_data_size(entry.type,\n+\t\t\t\t\t\t\t  count);\n+\tsize_t sizeIncrement = newSize - oldSize > 0 ? newSize - oldSize : 0;\n+\tif (!resize(0, sizeIncrement)) {\n+\t\tLOG(CameraMetadata, Error) << \"Failed to resize\";\n+\t\tvalid_ = false;\n \t\treturn false;\n \t}\n \n-\treturn true;\n+\tret = update_camera_metadata_entry(metadata_, entry.index, data,\n+\t\t\t\t\t   count, nullptr);\n+\tif (!ret)\n+\t\treturn true;\n+\n+\tconst char *name = get_camera_metadata_tag_name(tag);\n+\tLOG(CameraMetadata, Error)\n+\t\t<< \"Failed to update tag \" << (name ? name : \"<unknown>\");\n+\n+\tvalid_ = false;\n+\n+\treturn false;\n }\n \n camera_metadata_t *CameraMetadata::get()\ndiff --git a/src/android/camera_metadata.h b/src/android/camera_metadata.h\nindex d653e2f0..07afd4b2 100644\n--- a/src/android/camera_metadata.h\n+++ b/src/android/camera_metadata.h\n@@ -8,6 +8,7 @@\n #define __ANDROID_CAMERA_METADATA_H__\n \n #include <stdint.h>\n+#include <vector>\n \n #include <system/camera_metadata.h>\n \n@@ -23,9 +24,56 @@ public:\n \tCameraMetadata &operator=(const CameraMetadata &other);\n \n \tbool isValid() const { return valid_; }\n+\tbool resize(size_t count, size_t size);\n \tbool getEntry(uint32_t tag, camera_metadata_ro_entry_t *entry) const;\n-\tbool addEntry(uint32_t tag, const void *data, size_t data_count);\n-\tbool updateEntry(uint32_t tag, const void *data, size_t data_count);\n+\n+\ttemplate<typename T,\n+\t\t std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>\n+\tbool addEntry(uint32_t tag, const T &data)\n+\t{\n+\t\treturn addEntry(tag, &data, 1, sizeof(T));\n+\t}\n+\n+\ttemplate<typename T, size_t size>\n+\tbool addEntry(uint32_t tag, const T (&data)[size])\n+\t{\n+\t\treturn addEntry(tag, data, size, sizeof(T));\n+\t}\n+\n+\ttemplate<typename S,\n+\t\t typename T = typename S::value_type>\n+\tbool addEntry(uint32_t tag, S &data)\n+\t{\n+\t\treturn addEntry(tag, data.data(), data.size(), sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tbool addEntry(uint32_t tag, const T *data, size_t count)\n+\t{\n+\t\treturn addEntry(tag, data, count, sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tbool updateEntry(uint32_t tag, const T &data)\n+\t{\n+\t\treturn updateEntry(tag, &data, 1);\n+\t}\n+\n+\ttemplate<typename T, size_t size>\n+\tbool updateEntry(uint32_t tag, const T (&data)[size])\n+\t{\n+\t\treturn updateEntry(tag, data, size, sizeof(T));\n+\t}\n+\n+\ttemplate<typename S,\n+\t\t typename T = typename S::value_type>\n+\tbool updateEntry(uint32_t tag, S &data)\n+\t{\n+\t\treturn updateEntry(tag, data.data(), data.size());\n+\t}\n+\n+\tbool addEntry(uint32_t tag, const void *data, size_t count, size_t sizeofT);\n+\tbool updateEntry(uint32_t tag, const void *data, size_t count);\n \n \tcamera_metadata_t *get();\n \tconst camera_metadata_t *get() const;\n","prefixes":["libcamera-devel","v5","1/3"]}