2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // #define LOG_NDEBUG 0
19 #define LOG_TAG "Camera2-Metadata"
20 #include <utils/Log.h>
21 #include <utils/Errors.h>
23 #include <binder/Parcel.h>
24 #include <camera/CameraMetadata.h>
25 #include <camera/VendorTagDescriptor.h>
29 #define ALIGN_TO(val, alignment) \
30 (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
32 typedef Parcel::WritableBlob WritableBlob;
33 typedef Parcel::ReadableBlob ReadableBlob;
35 CameraMetadata::CameraMetadata() :
36 mBuffer(NULL), mLocked(false) {
39 CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) :
42 mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity);
45 CameraMetadata::CameraMetadata(const CameraMetadata &other) :
47 mBuffer = clone_camera_metadata(other.mBuffer);
50 CameraMetadata::CameraMetadata(camera_metadata_t *buffer) :
51 mBuffer(NULL), mLocked(false) {
55 CameraMetadata &CameraMetadata::operator=(const CameraMetadata &other) {
56 return operator=(other.mBuffer);
59 CameraMetadata &CameraMetadata::operator=(const camera_metadata_t *buffer) {
61 ALOGE("%s: Assignment to a locked CameraMetadata!", __FUNCTION__);
65 if (CC_LIKELY(buffer != mBuffer)) {
66 camera_metadata_t *newBuffer = clone_camera_metadata(buffer);
73 CameraMetadata::~CameraMetadata() {
78 const camera_metadata_t* CameraMetadata::getAndLock() const {
83 status_t CameraMetadata::unlock(const camera_metadata_t *buffer) const {
85 ALOGE("%s: Can't unlock a non-locked CameraMetadata!", __FUNCTION__);
86 return INVALID_OPERATION;
88 if (buffer != mBuffer) {
89 ALOGE("%s: Can't unlock CameraMetadata with wrong pointer!",
97 camera_metadata_t* CameraMetadata::release() {
99 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
102 camera_metadata_t *released = mBuffer;
107 void CameraMetadata::clear() {
109 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
113 free_camera_metadata(mBuffer);
118 void CameraMetadata::acquire(camera_metadata_t *buffer) {
120 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
126 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/NULL) != OK,
127 "%s: Failed to validate metadata structure %p",
128 __FUNCTION__, buffer);
131 void CameraMetadata::acquire(CameraMetadata &other) {
133 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
136 acquire(other.release());
139 status_t CameraMetadata::append(const CameraMetadata &other) {
140 return append(other.mBuffer);
143 status_t CameraMetadata::append(const camera_metadata_t* other) {
145 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
146 return INVALID_OPERATION;
148 size_t extraEntries = get_camera_metadata_entry_count(other);
149 size_t extraData = get_camera_metadata_data_count(other);
150 resizeIfNeeded(extraEntries, extraData);
152 return append_camera_metadata(mBuffer, other);
155 size_t CameraMetadata::entryCount() const {
156 return (mBuffer == NULL) ? 0 :
157 get_camera_metadata_entry_count(mBuffer);
160 bool CameraMetadata::isEmpty() const {
161 return entryCount() == 0;
164 status_t CameraMetadata::sort() {
166 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
167 return INVALID_OPERATION;
169 return sort_camera_metadata(mBuffer);
172 status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) {
173 int tagType = get_local_camera_metadata_tag_type(tag, mBuffer);
174 if ( CC_UNLIKELY(tagType == -1)) {
175 ALOGE("Update metadata entry: Unknown tag %d", tag);
176 return INVALID_OPERATION;
178 if ( CC_UNLIKELY(tagType != expectedType) ) {
179 ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; "
180 "got type %s data instead ",
181 get_local_camera_metadata_tag_name(tag, mBuffer), tag,
182 camera_metadata_type_names[tagType],
183 camera_metadata_type_names[expectedType]);
184 return INVALID_OPERATION;
189 status_t CameraMetadata::update(uint32_t tag,
190 const int32_t *data, size_t data_count) {
193 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
194 return INVALID_OPERATION;
196 if ( (res = checkType(tag, TYPE_INT32)) != OK) {
199 return updateImpl(tag, (const void*)data, data_count);
202 status_t CameraMetadata::update(uint32_t tag,
203 const uint8_t *data, size_t data_count) {
206 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
207 return INVALID_OPERATION;
209 if ( (res = checkType(tag, TYPE_BYTE)) != OK) {
212 return updateImpl(tag, (const void*)data, data_count);
215 status_t CameraMetadata::update(uint32_t tag,
216 const float *data, size_t data_count) {
219 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
220 return INVALID_OPERATION;
222 if ( (res = checkType(tag, TYPE_FLOAT)) != OK) {
225 return updateImpl(tag, (const void*)data, data_count);
228 status_t CameraMetadata::update(uint32_t tag,
229 const int64_t *data, size_t data_count) {
232 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
233 return INVALID_OPERATION;
235 if ( (res = checkType(tag, TYPE_INT64)) != OK) {
238 return updateImpl(tag, (const void*)data, data_count);
241 status_t CameraMetadata::update(uint32_t tag,
242 const double *data, size_t data_count) {
245 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
246 return INVALID_OPERATION;
248 if ( (res = checkType(tag, TYPE_DOUBLE)) != OK) {
251 return updateImpl(tag, (const void*)data, data_count);
254 status_t CameraMetadata::update(uint32_t tag,
255 const camera_metadata_rational_t *data, size_t data_count) {
258 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
259 return INVALID_OPERATION;
261 if ( (res = checkType(tag, TYPE_RATIONAL)) != OK) {
264 return updateImpl(tag, (const void*)data, data_count);
267 status_t CameraMetadata::update(uint32_t tag,
268 const String8 &string) {
271 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
272 return INVALID_OPERATION;
274 if ( (res = checkType(tag, TYPE_BYTE)) != OK) {
277 // string.size() doesn't count the null termination character.
278 return updateImpl(tag, (const void*)string.string(), string.size() + 1);
281 status_t CameraMetadata::update(const camera_metadata_ro_entry &entry) {
284 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
285 return INVALID_OPERATION;
287 if ( (res = checkType(entry.tag, entry.type)) != OK) {
290 return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count);
293 status_t CameraMetadata::updateImpl(uint32_t tag, const void *data,
297 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
298 return INVALID_OPERATION;
300 int type = get_local_camera_metadata_tag_type(tag, mBuffer);
302 ALOGE("%s: Tag %d not found", __FUNCTION__, tag);
305 // Safety check - ensure that data isn't pointing to this metadata, since
306 // that would get invalidated if a resize is needed
307 size_t bufferSize = get_camera_metadata_size(mBuffer);
308 uintptr_t bufAddr = reinterpret_cast<uintptr_t>(mBuffer);
309 uintptr_t dataAddr = reinterpret_cast<uintptr_t>(data);
310 if (dataAddr > bufAddr && dataAddr < (bufAddr + bufferSize)) {
311 ALOGE("%s: Update attempted with data from the same metadata buffer!",
313 return INVALID_OPERATION;
316 size_t data_size = calculate_camera_metadata_entry_data_size(type,
319 res = resizeIfNeeded(1, data_size);
322 camera_metadata_entry_t entry;
323 res = find_camera_metadata_entry(mBuffer, tag, &entry);
324 if (res == NAME_NOT_FOUND) {
325 res = add_camera_metadata_entry(mBuffer,
326 tag, data, data_count);
327 } else if (res == OK) {
328 res = update_camera_metadata_entry(mBuffer,
329 entry.index, data, data_count, NULL);
334 ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)",
335 __FUNCTION__, get_local_camera_metadata_section_name(tag, mBuffer),
336 get_local_camera_metadata_tag_name(tag, mBuffer), tag,
337 strerror(-res), res);
341 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/NULL) !=
344 "%s: Failed to validate metadata structure after update %p",
345 __FUNCTION__, mBuffer);
351 bool CameraMetadata::exists(uint32_t tag) const {
352 camera_metadata_ro_entry entry;
353 return find_camera_metadata_ro_entry(mBuffer, tag, &entry) == 0;
356 camera_metadata_entry_t CameraMetadata::find(uint32_t tag) {
358 camera_metadata_entry entry;
360 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
364 res = find_camera_metadata_entry(mBuffer, tag, &entry);
365 if (CC_UNLIKELY( res != OK )) {
367 entry.data.u8 = NULL;
372 camera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const {
374 camera_metadata_ro_entry entry;
375 res = find_camera_metadata_ro_entry(mBuffer, tag, &entry);
376 if (CC_UNLIKELY( res != OK )) {
378 entry.data.u8 = NULL;
383 status_t CameraMetadata::erase(uint32_t tag) {
384 camera_metadata_entry_t entry;
387 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
388 return INVALID_OPERATION;
390 res = find_camera_metadata_entry(mBuffer, tag, &entry);
391 if (res == NAME_NOT_FOUND) {
393 } else if (res != OK) {
394 ALOGE("%s: Error looking for entry %s.%s (%x): %s %d",
396 get_local_camera_metadata_section_name(tag, mBuffer),
397 get_local_camera_metadata_tag_name(tag, mBuffer),
398 tag, strerror(-res), res);
401 res = delete_camera_metadata_entry(mBuffer, entry.index);
403 ALOGE("%s: Error deleting entry %s.%s (%x): %s %d",
405 get_local_camera_metadata_section_name(tag, mBuffer),
406 get_local_camera_metadata_tag_name(tag, mBuffer),
407 tag, strerror(-res), res);
412 void CameraMetadata::dump(int fd, int verbosity, int indentation) const {
413 dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation);
416 status_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) {
417 if (mBuffer == NULL) {
418 mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2);
419 if (mBuffer == NULL) {
420 ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
424 size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer);
425 size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer);
426 size_t newEntryCount = currentEntryCount +
428 newEntryCount = (newEntryCount > currentEntryCap) ?
429 newEntryCount * 2 : currentEntryCap;
431 size_t currentDataCount = get_camera_metadata_data_count(mBuffer);
432 size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer);
433 size_t newDataCount = currentDataCount +
435 newDataCount = (newDataCount > currentDataCap) ?
436 newDataCount * 2 : currentDataCap;
438 if (newEntryCount > currentEntryCap ||
439 newDataCount > currentDataCap) {
440 camera_metadata_t *oldBuffer = mBuffer;
441 mBuffer = allocate_camera_metadata(newEntryCount,
443 if (mBuffer == NULL) {
444 ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
447 append_camera_metadata(mBuffer, oldBuffer);
448 free_camera_metadata(oldBuffer);
454 status_t CameraMetadata::readFromParcel(const Parcel& data,
455 camera_metadata_t** out) {
459 camera_metadata_t* metadata = NULL;
465 // See CameraMetadata::writeToParcel for parcel data layout diagram and explanation.
466 // arg0 = blobSize (int32)
467 int32_t blobSizeTmp = -1;
468 if ((err = data.readInt32(&blobSizeTmp)) != OK) {
469 ALOGE("%s: Failed to read metadata size (error %d %s)",
470 __FUNCTION__, err, strerror(-err));
473 const size_t blobSize = static_cast<size_t>(blobSizeTmp);
474 const size_t alignment = get_camera_metadata_alignment();
476 // Special case: zero blob size means zero sized (NULL) metadata.
478 ALOGV("%s: Read 0-sized metadata", __FUNCTION__);
482 if (blobSize <= alignment) {
483 ALOGE("%s: metadata blob is malformed, blobSize(%zu) should be larger than alignment(%zu)",
484 __FUNCTION__, blobSize, alignment);
488 const size_t metadataSize = blobSize - alignment;
490 // NOTE: this doesn't make sense to me. shouldn't the blob
491 // know how big it is? why do we have to specify the size
492 // to Parcel::readBlob ?
494 // arg1 = metadata (blob)
496 if ((err = data.readBlob(blobSize, &blob)) != OK) {
497 ALOGE("%s: Failed to read metadata blob (sized %zu). Possible "
498 " serialization bug. Error %d %s",
499 __FUNCTION__, blobSize, err, strerror(-err));
503 // arg2 = offset (blob)
504 // Must be after blob since we don't know offset until after writeBlob.
506 if ((err = data.readInt32(&offsetTmp)) != OK) {
507 ALOGE("%s: Failed to read metadata offsetTmp (error %d %s)",
508 __FUNCTION__, err, strerror(-err));
511 const size_t offset = static_cast<size_t>(offsetTmp);
512 if (offset >= alignment) {
513 ALOGE("%s: metadata offset(%zu) should be less than alignment(%zu)",
514 __FUNCTION__, blobSize, alignment);
519 const uintptr_t metadataStart = reinterpret_cast<uintptr_t>(blob.data()) + offset;
520 const camera_metadata_t* tmp =
521 reinterpret_cast<const camera_metadata_t*>(metadataStart);
522 ALOGV("%s: alignment is: %zu, metadata start: %p, offset: %zu",
523 __FUNCTION__, alignment, tmp, offset);
524 metadata = allocate_copy_camera_metadata_checked(tmp, metadataSize);
525 if (metadata == NULL) {
526 // We consider that allocation only fails if the validation
527 // also failed, therefore the readFromParcel was a failure.
528 ALOGE("%s: metadata allocation and copy failed", __FUNCTION__);
535 ALOGV("%s: Set out metadata to %p", __FUNCTION__, metadata);
537 } else if (metadata != NULL) {
538 ALOGV("%s: Freed camera metadata at %p", __FUNCTION__, metadata);
539 free_camera_metadata(metadata);
545 status_t CameraMetadata::writeToParcel(Parcel& data,
546 const camera_metadata_t* metadata) {
550 * Below is the camera metadata parcel layout:
552 * |--------------------------------------------|
555 * |--------------------------------------------|<--Skip the rest if blobSize == 0.
559 * | (length = variable, see arg1 layout below) |
562 * |--------------------------------------------|
565 * |--------------------------------------------|
568 // arg0 = blobSize (int32)
569 if (metadata == NULL) {
570 // Write zero blobSize for null metadata.
571 return data.writeInt32(0);
575 * Always make the blob size sufficiently larger, as we need put alignment
576 * padding and metadata into the blob. Since we don't know the alignment
577 * offset before writeBlob. Then write the metadata to aligned offset.
579 const size_t metadataSize = get_camera_metadata_compact_size(metadata);
580 const size_t alignment = get_camera_metadata_alignment();
581 const size_t blobSize = metadataSize + alignment;
582 res = data.writeInt32(static_cast<int32_t>(blobSize));
589 * arg1 = metadata (blob).
591 * The blob size is the sum of front padding size, metadata size and back padding
592 * size, which is equal to metadataSize + alignment.
594 * The blob layout is:
595 * |------------------------------------|<----Start address of the blob (unaligned).
597 * | (size = offset) |
598 * |------------------------------------|<----Aligned start address of metadata.
602 * | (size = metadataSize) |
605 * |------------------------------------|
607 * | (size = alignment - offset) |
608 * |------------------------------------|<----End address of blob.
609 * (Blob start address + blob size).
613 res = data.writeBlob(blobSize, false, &blob);
617 const uintptr_t metadataStart = ALIGN_TO(blob.data(), alignment);
618 offset = metadataStart - reinterpret_cast<uintptr_t>(blob.data());
619 ALOGV("%s: alignment is: %zu, metadata start: %p, offset: %zu",
620 __FUNCTION__, alignment,
621 reinterpret_cast<const void *>(metadataStart), offset);
622 copy_camera_metadata(reinterpret_cast<void*>(metadataStart), metadataSize, metadata);
624 // Not too big of a problem since receiving side does hard validation
625 // Don't check the size since the compact size could be larger
626 if (validate_camera_metadata_structure(metadata, /*size*/NULL) != OK) {
627 ALOGW("%s: Failed to validate metadata %p before writing blob",
628 __FUNCTION__, metadata);
634 // arg2 = offset (int32)
635 res = data.writeInt32(static_cast<int32_t>(offset));
640 status_t CameraMetadata::readFromParcel(const Parcel *parcel) {
642 ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
646 if (parcel == NULL) {
647 ALOGE("%s: parcel is null", __FUNCTION__);
652 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
653 return INVALID_OPERATION;
656 camera_metadata *buffer = NULL;
657 // TODO: reading should return a status code, in case validation fails
658 res = CameraMetadata::readFromParcel(*parcel, &buffer);
660 if (res != NO_ERROR) {
661 ALOGE("%s: Failed to read from parcel. Metadata is unchanged.",
672 status_t CameraMetadata::writeToParcel(Parcel *parcel) const {
674 ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
676 if (parcel == NULL) {
677 ALOGE("%s: parcel is null", __FUNCTION__);
681 return CameraMetadata::writeToParcel(*parcel, mBuffer);
684 void CameraMetadata::swap(CameraMetadata& other) {
686 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
688 } else if (other.mLocked) {
689 ALOGE("%s: Other CameraMetadata is locked", __FUNCTION__);
693 camera_metadata* thisBuf = mBuffer;
694 camera_metadata* otherBuf = other.mBuffer;
696 other.mBuffer = thisBuf;
700 status_t CameraMetadata::getTagFromName(const char *name,
701 const VendorTagDescriptor* vTags, uint32_t *tag) {
703 if (name == nullptr || tag == nullptr) return BAD_VALUE;
705 size_t nameLength = strlen(name);
707 const SortedVector<String8> *vendorSections;
708 size_t vendorSectionCount = 0;
711 vendorSections = vTags->getAllSectionNames();
712 vendorSectionCount = vendorSections->size();
715 // First, find the section by the longest string match
716 const char *section = NULL;
717 size_t sectionIndex = 0;
718 size_t sectionLength = 0;
719 size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount;
720 for (size_t i = 0; i < totalSectionCount; ++i) {
722 const char *str = (i < ANDROID_SECTION_COUNT) ? camera_metadata_section_names[i] :
723 (*vendorSections)[i - ANDROID_SECTION_COUNT].string();
725 ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str);
727 if (strstr(name, str) == name) { // name begins with the section name
728 size_t strLength = strlen(str);
730 ALOGV("%s: Name begins with section name", __FUNCTION__);
732 // section name is the longest we've found so far
733 if (section == NULL || sectionLength < strLength) {
736 sectionLength = strLength;
738 ALOGV("%s: Found new best section (%s)", __FUNCTION__, section);
743 // TODO: Make above get_camera_metadata_section_from_name ?
745 if (section == NULL) {
746 return NAME_NOT_FOUND;
748 ALOGV("%s: Found matched section '%s' (%zu)",
749 __FUNCTION__, section, sectionIndex);
752 // Get the tag name component of the name
753 const char *nameTagName = name + sectionLength + 1; // x.y.z -> z
754 if (sectionLength + 1 >= nameLength) {
758 // Match rest of name against the tag names in that section only
759 uint32_t candidateTag = 0;
760 if (sectionIndex < ANDROID_SECTION_COUNT) {
761 // Match built-in tags (typically android.*)
762 uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd)
763 tagBegin = camera_metadata_section_bounds[sectionIndex][0];
764 tagEnd = camera_metadata_section_bounds[sectionIndex][1];
766 for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) {
767 const char *tagName = get_camera_metadata_tag_name(candidateTag);
769 if (strcmp(nameTagName, tagName) == 0) {
770 ALOGV("%s: Found matched tag '%s' (%d)",
771 __FUNCTION__, tagName, candidateTag);
776 if (candidateTag == tagEnd) {
777 return NAME_NOT_FOUND;
779 } else if (vTags != NULL) {
780 // Match vendor tags (typically com.*)
781 const String8 sectionName(section);
782 const String8 tagName(nameTagName);
785 if ((res = vTags->lookupTag(tagName, sectionName, &candidateTag)) != OK) {
786 return NAME_NOT_FOUND;
795 }; // namespace android