1cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala/*
2cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * Copyright (C) 2012 The Android Open Source Project
3cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala *
4cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * you may not use this file except in compliance with the License.
6cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * You may obtain a copy of the License at
7cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala *
8cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala *
10cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * See the License for the specific language governing permissions and
14cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala * limitations under the License.
15cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala */
16cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
174bb8118816874c696d9f1adab48490df1da365f7Eino-Ville Talvala#define LOG_TAG "Camera2-Metadata"
18cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala#include <utils/Log.h>
19cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala#include <utils/Errors.h>
20cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
21cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala#include "CameraMetadata.h"
22cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
23cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalanamespace android {
24cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
252e19c3c02957208371cdd491e6342ea7ddb440d9Eino-Ville Talvalanamespace camera2 {
26cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville TalvalaCameraMetadata::CameraMetadata() :
27cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        mBuffer(NULL) {
28cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
29cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
30cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville TalvalaCameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity)
31cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala{
32cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity);
33cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
34cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
35cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville TalvalaCameraMetadata::CameraMetadata(const CameraMetadata &other) {
36cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    mBuffer = clone_camera_metadata(other.mBuffer);
37cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
38cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
39cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville TalvalaCameraMetadata &CameraMetadata::operator=(const CameraMetadata &other) {
40cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return operator=(other.mBuffer);
41cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
42cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
43cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville TalvalaCameraMetadata &CameraMetadata::operator=(const camera_metadata_t *buffer) {
44cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (CC_LIKELY(buffer != mBuffer)) {
45cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        camera_metadata_t *newBuffer = clone_camera_metadata(buffer);
46cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        clear();
47cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        mBuffer = newBuffer;
48cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
49cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return *this;
50cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
51cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
52cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville TalvalaCameraMetadata::~CameraMetadata() {
53cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    clear();
54cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
55cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
56cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalacamera_metadata_t* CameraMetadata::release() {
57cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    camera_metadata_t *released = mBuffer;
58cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    mBuffer = NULL;
59cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return released;
60cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
61cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
62cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalavoid CameraMetadata::clear() {
63cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (mBuffer) {
64cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        free_camera_metadata(mBuffer);
65cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        mBuffer = NULL;
66cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
67cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
68cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
69cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalavoid CameraMetadata::acquire(camera_metadata_t *buffer) {
70cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    clear();
71cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    mBuffer = buffer;
72cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
73cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
74cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalavoid CameraMetadata::acquire(CameraMetadata &other) {
75cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    acquire(other.release());
76cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
77cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
78cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::append(const CameraMetadata &other) {
79cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return append_camera_metadata(mBuffer, other.mBuffer);
80cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
81cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
82cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalasize_t CameraMetadata::entryCount() const {
83cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return (mBuffer == NULL) ? 0 :
84cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            get_camera_metadata_entry_count(mBuffer);
85cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
86cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
87da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalabool CameraMetadata::isEmpty() const {
88da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return entryCount() == 0;
89da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
90da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
91cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::sort() {
92cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return sort_camera_metadata(mBuffer);
93cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
94cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
95cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) {
96cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    int tagType = get_camera_metadata_tag_type(tag);
97cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( CC_UNLIKELY(tagType == -1)) {
98cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        ALOGE("Update metadata entry: Unknown tag %d", tag);
99cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return INVALID_OPERATION;
100cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
101cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( CC_UNLIKELY(tagType != expectedType) ) {
102cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; "
103cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                "got type %s data instead ",
104cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                get_camera_metadata_tag_name(tag), tag,
105cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                camera_metadata_type_names[tagType],
106cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                camera_metadata_type_names[expectedType]);
107cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return INVALID_OPERATION;
108cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
109cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return OK;
110cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
111cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
112cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
113cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const int32_t *data, size_t data_count) {
114cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
115cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_INT32)) != OK) {
116cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
117cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
118cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)data, data_count);
119cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
120cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
121cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
122cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const uint8_t *data, size_t data_count) {
123cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
124cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_BYTE)) != OK) {
125cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
126cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
127cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)data, data_count);
128cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
129cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
130cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
131cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const float *data, size_t data_count) {
132cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
133cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_FLOAT)) != OK) {
134cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
135cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
136cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)data, data_count);
137cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
138cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
139cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
140cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const int64_t *data, size_t data_count) {
141cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
142cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_INT64)) != OK) {
143cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
144cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
145cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)data, data_count);
146cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
147cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
148cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
149cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const double *data, size_t data_count) {
150cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
151cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_DOUBLE)) != OK) {
152cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
153cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
154cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)data, data_count);
155cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
156cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
157cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
158cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const camera_metadata_rational_t *data, size_t data_count) {
159cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
160cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_RATIONAL)) != OK) {
161cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
162cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
163cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)data, data_count);
164cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
165cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
166cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag,
167cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        const String8 &string) {
168cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
169cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if ( (res = checkType(tag, TYPE_BYTE)) != OK) {
170cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
171cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
172cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return update(tag, (const void*)string.string(), string.size());
173cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
174cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
175cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::update(uint32_t tag, const void *data,
176cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t data_count) {
177cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
178cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    int type = get_camera_metadata_tag_type(tag);
179cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (type == -1) {
180cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        ALOGE("%s: Tag %d not found", __FUNCTION__, tag);
181cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return BAD_VALUE;
182cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
183cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    size_t data_size = calculate_camera_metadata_entry_data_size(type,
184cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            data_count);
185cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
186cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    res = resizeIfNeeded(1, data_size);
187cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
188cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (res == OK) {
189cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        camera_metadata_entry_t entry;
190cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        res = find_camera_metadata_entry(mBuffer, tag, &entry);
191cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        if (res == NAME_NOT_FOUND) {
192cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            res = add_camera_metadata_entry(mBuffer,
193cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                    tag, data, data_count);
194cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        } else if (res == OK) {
195cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            res = update_camera_metadata_entry(mBuffer,
196cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                    entry.index, data, data_count, NULL);
197cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        }
198cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
199cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
200cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (res != OK) {
201cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)",
202cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                __FUNCTION__, get_camera_metadata_section_name(tag),
203cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                get_camera_metadata_tag_name(tag), tag, strerror(-res), res);
204cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
205cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return res;
206cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
207cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
208cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalacamera_metadata_entry_t CameraMetadata::find(uint32_t tag) {
209cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
210cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    camera_metadata_entry entry;
211cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    res = find_camera_metadata_entry(mBuffer, tag, &entry);
212cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (CC_UNLIKELY( res != OK )) {
213cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        entry.count = 0;
214cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        entry.data.u8 = NULL;
215cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
216cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return entry;
217cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
218cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
219cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalacamera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const {
220cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
221cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    camera_metadata_ro_entry entry;
222cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    res = find_camera_metadata_ro_entry(mBuffer, tag, &entry);
223cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (CC_UNLIKELY( res != OK )) {
224cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        entry.count = 0;
225cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        entry.data.u8 = NULL;
226cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
227cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return entry;
228cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
229cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
230cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::erase(uint32_t tag) {
231cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    camera_metadata_entry_t entry;
232cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    status_t res;
233cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    res = find_camera_metadata_entry(mBuffer, tag, &entry);
234cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (res == NAME_NOT_FOUND) {
235cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return OK;
236cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    } else if (res != OK) {
237cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        ALOGE("%s: Error looking for entry %s.%s (%x): %s %d",
238cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                __FUNCTION__,
239cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                get_camera_metadata_section_name(tag),
240cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                get_camera_metadata_tag_name(tag), tag, strerror(-res), res);
241cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        return res;
242cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
243cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    res = delete_camera_metadata_entry(mBuffer, entry.index);
244cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (res != OK) {
245cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        ALOGE("%s: Error deleting entry %s.%s (%x): %s %d",
246cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                __FUNCTION__,
247cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                get_camera_metadata_section_name(tag),
248cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                get_camera_metadata_tag_name(tag), tag, strerror(-res), res);
249cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
250cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return res;
251cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
252cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
253cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalavoid CameraMetadata::dump(int fd, int verbosity, int indentation) const {
254cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation);
255cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
256cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
257cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvalastatus_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) {
258cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    if (mBuffer == NULL) {
259cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2);
260cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        if (mBuffer == NULL) {
261cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
262cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            return NO_MEMORY;
263cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        }
264cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    } else {
265cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer);
266cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer);
267cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t newEntryCount = currentEntryCount +
268cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                extraEntries;
269cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        newEntryCount = (newEntryCount > currentEntryCap) ?
270cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                newEntryCount * 2 : currentEntryCap;
271cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
272cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t currentDataCount = get_camera_metadata_data_count(mBuffer);
273cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer);
274cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        size_t newDataCount = currentDataCount +
275cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                extraData;
276cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        newDataCount = (newDataCount > currentDataCap) ?
277cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                newDataCount * 2 : currentDataCap;
278cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
279cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        if (newEntryCount > currentEntryCap ||
280cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                newDataCount > currentDataCap) {
281cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            camera_metadata_t *oldBuffer = mBuffer;
282cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            mBuffer = allocate_camera_metadata(newEntryCount,
283cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                    newDataCount);
284cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            if (mBuffer == NULL) {
285cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
286cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala                return NO_MEMORY;
287cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            }
288cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            append_camera_metadata(mBuffer, oldBuffer);
289cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala            free_camera_metadata(oldBuffer);
290cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala        }
291cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    }
292cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala    return OK;
293cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}
294cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala
2952e19c3c02957208371cdd491e6342ea7ddb440d9Eino-Ville Talvala}; // namespace camera2
296cab96a4a7a2900fa991ab77ae45ba8d4be97e054Eino-Ville Talvala}; // namespace android
297