{"id":12253,"url":"https://patchwork.libcamera.org/api/1.1/patches/12253/?format=json","web_url":"https://patchwork.libcamera.org/patch/12253/","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":"<20210512102541.722956-2-paul.elder@ideasonboard.com>","date":"2021-05-12T10:25:39","name":"[libcamera-devel,v3,1/3] android: camera_metadata: Auto-resize CameraMetadata","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"a1b9ac4ef5fac9064b738d235cefa508951e2949","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/12253/mbox/","series":[{"id":2020,"url":"https://patchwork.libcamera.org/api/1.1/series/2020/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2020","date":"2021-05-12T10:25:38","name":"[libcamera-devel,v3,1/3] android: camera_metadata: Auto-resize CameraMetadata","version":3,"mbox":"https://patchwork.libcamera.org/series/2020/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12253/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12253/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 05E64C31E7\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 12 May 2021 10:25:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5451468920;\n\tWed, 12 May 2021 12:25:53 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 61E8668915\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 12 May 2021 12:25:52 +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 E846E436;\n\tWed, 12 May 2021 12:25:50 +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=\"JN0ivD+V\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1620815152;\n\tbh=aOi5tb6nCyVcRMrCbHYJwRM0G1MGstvxAYyIG/JtdfQ=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=JN0ivD+V4TzuB4dwboXzK+PuSdpuY54iG3YK2r8K8ly23tIFw2XJAT+cYCyL4lZUP\n\te4ShTOB+VqxA8mb4juOKkayg+D8O8ObpuwZfsCGUpFf064bY0nSREmLl6v1J2R0w1T\n\tdft5gbN0Gj7wvFzalxP6R7HnJxbwDsQ43Iie6S08=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed, 12 May 2021 19:25:39 +0900","Message-Id":"<20210512102541.722956-2-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20210512102541.722956-1-paul.elder@ideasonboard.com>","References":"<20210512102541.722956-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v3 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=\"us-ascii\"","Content-Transfer-Encoding":"7bit","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>\n\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, vector (v3: s/span/vector/)\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. This is very common, and wasn't very nice\nhaving to wrap everything in a Span. All the old calls are\nbackward-compatible using 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   | 44 ++++++++++++++++-\n 4 files changed, 117 insertions(+), 59 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..6394fd74 100644\n--- a/src/android/camera_metadata.cpp\n+++ b/src/android/camera_metadata.cpp\n@@ -63,14 +63,67 @@ 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 (!add_camera_metadata_entry(metadata_, tag, data, count))\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+void 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;\n+\n+\tif (!resize(1, count * sizeofT)) {\n+\t\tLOG(CameraMetadata, Error) << \"Failed to resize\";\n+\t\tvalid_ = false;\n+\t\treturn;\n+\t}\n+\n+\tif (!add_camera_metadata_entry(metadata_, tag, data, count))\n+\t\treturn;\n+\n \tconst char *name = get_camera_metadata_tag_name(tag);\n \tif (name)\n \t\tLOG(CameraMetadata, Error)\n@@ -80,14 +133,13 @@ bool CameraMetadata::addEntry(uint32_t tag, const void *data, size_t count)\n \t\t\t<< \"Failed to add unknown tag \" << tag;\n \n \tvalid_ = false;\n-\n-\treturn false;\n }\n \n-bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t count)\n+void CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t count,\n+\t\t\t\t size_t sizeofT)\n {\n \tif (!valid_)\n-\t\treturn false;\n+\t\treturn;\n \n \tcamera_metadata_entry_t entry;\n \tint ret = find_camera_metadata_entry(metadata_, tag, &entry);\n@@ -96,7 +148,20 @@ bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t count)\n \t\tLOG(CameraMetadata, Error)\n \t\t\t<< \"Failed to update tag \"\n \t\t\t<< (name ? name : \"<unknown>\") << \": not present\";\n-\t\treturn false;\n+\t\treturn;\n+\t}\n+\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 * sizeofT);\n+\tsize_t sizeIncrement = newSize - oldSize > 0 ? newSize - oldSize : 0;\n+\tif (!resize(0, count * sizeIncrement)) {\n+\t\tLOG(CameraMetadata, Error) << \"Failed to resize\";\n+\t\tvalid_ = false;\n+\t\treturn;\n \t}\n \n \tret = update_camera_metadata_entry(metadata_, entry.index, data,\n@@ -105,10 +170,9 @@ bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t count)\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-\t\treturn false;\n-\t}\n \n-\treturn true;\n+\t\tvalid_ = false;\n+\t}\n }\n \n camera_metadata_t *CameraMetadata::get()\ndiff --git a/src/android/camera_metadata.h b/src/android/camera_metadata.h\nindex d653e2f0..aa875fe4 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,48 @@ 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+\tvoid addEntry(uint32_t tag, const T &data)\n+\t{\n+\t\taddEntry(tag, &data, 1, sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid addEntry(uint32_t tag, std::vector<T> &data)\n+\t{\n+\t\taddEntry(tag, data.data(), data.size(), sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid addEntry(uint32_t tag, const T *data, size_t count)\n+\t{\n+\t\taddEntry(tag, data, count, sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid updateEntry(uint32_t tag, const T &data)\n+\t{\n+\t\tupdateEntry(tag, &data, 1, sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid updateEntry(uint32_t tag, std::vector<T> &data)\n+\t{\n+\t\tupdateEntry(tag, data.data(), data.size(), sizeof(T));\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid updateEntry(uint32_t tag, const T *data, size_t count)\n+\t{\n+\t\tupdateEntry(tag, data, count, sizeof(T));\n+\t}\n+\n+\tvoid addEntry(uint32_t tag, const void *data, size_t count, size_t sizeofT);\n+\tvoid updateEntry(uint32_t tag, const void *data, size_t count, size_t sizeofT);\n \n \tcamera_metadata_t *get();\n \tconst camera_metadata_t *get() const;\n","prefixes":["libcamera-devel","v3","1/3"]}