Home | History | Annotate | Download | only in impl
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "ACameraMetadata"
     19 
     20 #include "ACameraMetadata.h"
     21 #include <utils/Vector.h>
     22 #include <system/graphics.h>
     23 #include "NdkImage.h"
     24 
     25 using namespace android;
     26 
     27 /**
     28  * ACameraMetadata Implementation
     29  */
     30 ACameraMetadata::ACameraMetadata(camera_metadata_t* buffer, ACAMERA_METADATA_TYPE type) :
     31         mData(buffer), mType(type) {
     32     if (mType == ACM_CHARACTERISTICS) {
     33         filterUnsupportedFeatures();
     34         filterStreamConfigurations();
     35     }
     36     // TODO: filter request/result keys
     37 }
     38 
     39 bool
     40 ACameraMetadata::isNdkSupportedCapability(int32_t capability) {
     41     switch (capability) {
     42         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE:
     43         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR:
     44         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_POST_PROCESSING:
     45         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW:
     46         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_READ_SENSOR_SETTINGS:
     47         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE:
     48         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT:
     49             return true;
     50         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_YUV_REPROCESSING:
     51         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING:
     52         case ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO:
     53             return false;
     54         default:
     55             // Newly defined capabilities will be unsupported by default (blacklist)
     56             // TODO: Should we do whitelist or blacklist here?
     57             ALOGE("%s: Unknonwn capability %d", __FUNCTION__, capability);
     58             return false;
     59     }
     60 }
     61 
     62 void
     63 ACameraMetadata::filterUnsupportedFeatures() {
     64     // Hide unsupported capabilities (reprocessing)
     65     camera_metadata_entry entry = mData.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
     66     if (entry.count == 0 || entry.type != TYPE_BYTE) {
     67         ALOGE("%s: malformed available capability key! count %zu, type %d",
     68                 __FUNCTION__, entry.count, entry.type);
     69         return;
     70     }
     71 
     72     Vector<uint8_t> capabilities;
     73     capabilities.setCapacity(entry.count);
     74     for (size_t i = 0; i < entry.count; i++) {
     75         uint8_t capability = entry.data.u8[i];
     76         if (isNdkSupportedCapability(capability)) {
     77             capabilities.push(capability);
     78         }
     79     }
     80     mData.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, capabilities);
     81 }
     82 
     83 
     84 void
     85 ACameraMetadata::filterStreamConfigurations() {
     86     const int STREAM_CONFIGURATION_SIZE = 4;
     87     const int STREAM_FORMAT_OFFSET = 0;
     88     const int STREAM_WIDTH_OFFSET = 1;
     89     const int STREAM_HEIGHT_OFFSET = 2;
     90     const int STREAM_IS_INPUT_OFFSET = 3;
     91     camera_metadata_entry entry = mData.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
     92     if (entry.count == 0 || entry.count % 4 || entry.type != TYPE_INT32) {
     93         ALOGE("%s: malformed available stream configuration key! count %zu, type %d",
     94                 __FUNCTION__, entry.count, entry.type);
     95         return;
     96     }
     97 
     98     Vector<int32_t> filteredStreamConfigs;
     99     filteredStreamConfigs.setCapacity(entry.count);
    100 
    101     for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) {
    102         int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET];
    103         int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET];
    104         int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET];
    105         int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET];
    106         if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
    107             // Hide input streams
    108             continue;
    109         }
    110         // Translate HAL formats to NDK format
    111         if (format == HAL_PIXEL_FORMAT_BLOB) {
    112             format = AIMAGE_FORMAT_JPEG;
    113         }
    114         filteredStreamConfigs.push_back(format);
    115         filteredStreamConfigs.push_back(width);
    116         filteredStreamConfigs.push_back(height);
    117         filteredStreamConfigs.push_back(isInput);
    118     }
    119 
    120     mData.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, filteredStreamConfigs);
    121 
    122     entry = mData.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS);
    123     Vector<int32_t> filteredDepthStreamConfigs;
    124     filteredDepthStreamConfigs.setCapacity(entry.count);
    125 
    126     for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) {
    127         int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET];
    128         int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET];
    129         int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET];
    130         int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET];
    131         if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
    132             // Hide input streams
    133             continue;
    134         }
    135         // Translate HAL formats to NDK format
    136         if (format == HAL_PIXEL_FORMAT_BLOB) {
    137             format = AIMAGE_FORMAT_DEPTH_POINT_CLOUD;
    138         } else if (format == HAL_PIXEL_FORMAT_Y16) {
    139             format = AIMAGE_FORMAT_DEPTH16;
    140         }
    141 
    142         filteredDepthStreamConfigs.push_back(format);
    143         filteredDepthStreamConfigs.push_back(width);
    144         filteredDepthStreamConfigs.push_back(height);
    145         filteredDepthStreamConfigs.push_back(isInput);
    146     }
    147     mData.update(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS, filteredDepthStreamConfigs);
    148 }
    149 
    150 bool
    151 ACameraMetadata::isVendorTag(const uint32_t tag) {
    152     uint32_t tag_section = tag >> 16;
    153     if (tag_section >= VENDOR_SECTION) {
    154         return true;
    155     }
    156     return false;
    157 }
    158 
    159 camera_status_t
    160 ACameraMetadata::getConstEntry(uint32_t tag, ACameraMetadata_const_entry* entry) const {
    161     if (entry == nullptr) {
    162         return ACAMERA_ERROR_INVALID_PARAMETER;
    163     }
    164 
    165     Mutex::Autolock _l(mLock);
    166 
    167     camera_metadata_ro_entry rawEntry = mData.find(tag);
    168     if (rawEntry.count == 0) {
    169         ALOGE("%s: cannot find metadata tag %d", __FUNCTION__, tag);
    170         return ACAMERA_ERROR_METADATA_NOT_FOUND;
    171     }
    172     entry->tag = tag;
    173     entry->type = rawEntry.type;
    174     entry->count = rawEntry.count;
    175     entry->data.u8 = rawEntry.data.u8;
    176     return ACAMERA_OK;
    177 }
    178 
    179 camera_status_t
    180 ACameraMetadata::update(uint32_t tag, uint32_t count, const uint8_t* data) {
    181     return updateImpl<uint8_t>(tag, count, data);
    182 }
    183 
    184 camera_status_t
    185 ACameraMetadata::update(uint32_t tag, uint32_t count, const int32_t* data) {
    186     return updateImpl<int32_t>(tag, count, data);
    187 }
    188 
    189 camera_status_t
    190 ACameraMetadata::update(uint32_t tag, uint32_t count, const float* data) {
    191     return updateImpl<float>(tag, count, data);
    192 }
    193 
    194 camera_status_t
    195 ACameraMetadata::update(uint32_t tag, uint32_t count, const double* data) {
    196     return updateImpl<double>(tag, count, data);
    197 }
    198 
    199 camera_status_t
    200 ACameraMetadata::update(uint32_t tag, uint32_t count, const int64_t* data) {
    201     return updateImpl<int64_t>(tag, count, data);
    202 }
    203 
    204 camera_status_t
    205 ACameraMetadata::update(uint32_t tag, uint32_t count, const ACameraMetadata_rational* data) {
    206     return updateImpl<camera_metadata_rational_t>(tag, count, data);
    207 }
    208 
    209 camera_status_t
    210 ACameraMetadata::getTags(/*out*/int32_t* numTags,
    211                          /*out*/const uint32_t** tags) const {
    212     Mutex::Autolock _l(mLock);
    213     if (mTags.size() == 0) {
    214         size_t entry_count = mData.entryCount();
    215         mTags.setCapacity(entry_count);
    216         const camera_metadata_t* rawMetadata = mData.getAndLock();
    217         for (size_t i = 0; i < entry_count; i++) {
    218             camera_metadata_ro_entry_t entry;
    219             int ret = get_camera_metadata_ro_entry(rawMetadata, i, &entry);
    220             if (ret != 0) {
    221                 ALOGE("%s: error reading metadata index %zu", __FUNCTION__, i);
    222                 return ACAMERA_ERROR_UNKNOWN;
    223             }
    224             // Hide system key from users
    225             if (sSystemTags.count(entry.tag) == 0) {
    226                 mTags.push_back(entry.tag);
    227             }
    228         }
    229         mData.unlock(rawMetadata);
    230     }
    231 
    232     *numTags = mTags.size();
    233     *tags = mTags.array();
    234     return ACAMERA_OK;
    235 }
    236 
    237 const CameraMetadata&
    238 ACameraMetadata::getInternalData() {
    239     return mData;
    240 }
    241 
    242 // TODO: some of key below should be hidden from user
    243 // ex: ACAMERA_REQUEST_ID and ACAMERA_REPROCESS_EFFECTIVE_EXPOSURE_FACTOR
    244 /*@O~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~
    245  * The key entries below this point are generated from metadata
    246  * definitions in /system/media/camera/docs. Do not modify by hand or
    247  * modify the comment blocks at the start or end.
    248  *~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~*/
    249 
    250 bool
    251 ACameraMetadata::isCaptureRequestTag(const uint32_t tag) {
    252     // Skip check for vendor keys
    253     if (isVendorTag(tag)) {
    254         return true;
    255     }
    256 
    257     switch (tag) {
    258         case ACAMERA_COLOR_CORRECTION_MODE:
    259         case ACAMERA_COLOR_CORRECTION_TRANSFORM:
    260         case ACAMERA_COLOR_CORRECTION_GAINS:
    261         case ACAMERA_COLOR_CORRECTION_ABERRATION_MODE:
    262         case ACAMERA_CONTROL_AE_ANTIBANDING_MODE:
    263         case ACAMERA_CONTROL_AE_EXPOSURE_COMPENSATION:
    264         case ACAMERA_CONTROL_AE_LOCK:
    265         case ACAMERA_CONTROL_AE_MODE:
    266         case ACAMERA_CONTROL_AE_REGIONS:
    267         case ACAMERA_CONTROL_AE_TARGET_FPS_RANGE:
    268         case ACAMERA_CONTROL_AE_PRECAPTURE_TRIGGER:
    269         case ACAMERA_CONTROL_AF_MODE:
    270         case ACAMERA_CONTROL_AF_REGIONS:
    271         case ACAMERA_CONTROL_AF_TRIGGER:
    272         case ACAMERA_CONTROL_AWB_LOCK:
    273         case ACAMERA_CONTROL_AWB_MODE:
    274         case ACAMERA_CONTROL_AWB_REGIONS:
    275         case ACAMERA_CONTROL_CAPTURE_INTENT:
    276         case ACAMERA_CONTROL_EFFECT_MODE:
    277         case ACAMERA_CONTROL_MODE:
    278         case ACAMERA_CONTROL_SCENE_MODE:
    279         case ACAMERA_CONTROL_VIDEO_STABILIZATION_MODE:
    280         case ACAMERA_CONTROL_POST_RAW_SENSITIVITY_BOOST:
    281         case ACAMERA_EDGE_MODE:
    282         case ACAMERA_FLASH_MODE:
    283         case ACAMERA_HOT_PIXEL_MODE:
    284         case ACAMERA_JPEG_GPS_COORDINATES:
    285         case ACAMERA_JPEG_GPS_PROCESSING_METHOD:
    286         case ACAMERA_JPEG_GPS_TIMESTAMP:
    287         case ACAMERA_JPEG_ORIENTATION:
    288         case ACAMERA_JPEG_QUALITY:
    289         case ACAMERA_JPEG_THUMBNAIL_QUALITY:
    290         case ACAMERA_JPEG_THUMBNAIL_SIZE:
    291         case ACAMERA_LENS_APERTURE:
    292         case ACAMERA_LENS_FILTER_DENSITY:
    293         case ACAMERA_LENS_FOCAL_LENGTH:
    294         case ACAMERA_LENS_FOCUS_DISTANCE:
    295         case ACAMERA_LENS_OPTICAL_STABILIZATION_MODE:
    296         case ACAMERA_NOISE_REDUCTION_MODE:
    297         case ACAMERA_SCALER_CROP_REGION:
    298         case ACAMERA_SENSOR_EXPOSURE_TIME:
    299         case ACAMERA_SENSOR_FRAME_DURATION:
    300         case ACAMERA_SENSOR_SENSITIVITY:
    301         case ACAMERA_SENSOR_TEST_PATTERN_DATA:
    302         case ACAMERA_SENSOR_TEST_PATTERN_MODE:
    303         case ACAMERA_SHADING_MODE:
    304         case ACAMERA_STATISTICS_FACE_DETECT_MODE:
    305         case ACAMERA_STATISTICS_HOT_PIXEL_MAP_MODE:
    306         case ACAMERA_STATISTICS_LENS_SHADING_MAP_MODE:
    307         case ACAMERA_TONEMAP_CURVE_BLUE:
    308         case ACAMERA_TONEMAP_CURVE_GREEN:
    309         case ACAMERA_TONEMAP_CURVE_RED:
    310         case ACAMERA_TONEMAP_MODE:
    311         case ACAMERA_TONEMAP_GAMMA:
    312         case ACAMERA_TONEMAP_PRESET_CURVE:
    313         case ACAMERA_BLACK_LEVEL_LOCK:
    314             return true;
    315         default:
    316             return false;
    317     }
    318 }
    319 
    320 // System tags that should be hidden from users
    321 std::unordered_set<uint32_t> ACameraMetadata::sSystemTags ({
    322     ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
    323     ANDROID_CONTROL_AE_PRECAPTURE_ID,
    324     ANDROID_CONTROL_AF_TRIGGER_ID,
    325     ANDROID_DEMOSAIC_MODE,
    326     ANDROID_EDGE_STRENGTH,
    327     ANDROID_FLASH_FIRING_POWER,
    328     ANDROID_FLASH_FIRING_TIME,
    329     ANDROID_FLASH_COLOR_TEMPERATURE,
    330     ANDROID_FLASH_MAX_ENERGY,
    331     ANDROID_FLASH_INFO_CHARGE_DURATION,
    332     ANDROID_JPEG_MAX_SIZE,
    333     ANDROID_JPEG_SIZE,
    334     ANDROID_NOISE_REDUCTION_STRENGTH,
    335     ANDROID_QUIRKS_METERING_CROP_REGION,
    336     ANDROID_QUIRKS_TRIGGER_AF_WITH_AUTO,
    337     ANDROID_QUIRKS_USE_ZSL_FORMAT,
    338     ANDROID_REQUEST_INPUT_STREAMS,
    339     ANDROID_REQUEST_METADATA_MODE,
    340     ANDROID_REQUEST_OUTPUT_STREAMS,
    341     ANDROID_REQUEST_TYPE,
    342     ANDROID_REQUEST_MAX_NUM_REPROCESS_STREAMS,
    343     ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
    344     ANDROID_SCALER_AVAILABLE_RAW_SIZES,
    345     ANDROID_SENSOR_BASE_GAIN_FACTOR,
    346     ANDROID_SENSOR_PROFILE_HUE_SAT_MAP_DIMENSIONS,
    347     ANDROID_SENSOR_TEMPERATURE,
    348     ANDROID_SENSOR_PROFILE_HUE_SAT_MAP,
    349     ANDROID_SENSOR_PROFILE_TONE_CURVE,
    350     ANDROID_SENSOR_OPAQUE_RAW_SIZE,
    351     ANDROID_SHADING_STRENGTH,
    352     ANDROID_STATISTICS_HISTOGRAM_MODE,
    353     ANDROID_STATISTICS_SHARPNESS_MAP_MODE,
    354     ANDROID_STATISTICS_HISTOGRAM,
    355     ANDROID_STATISTICS_SHARPNESS_MAP,
    356     ANDROID_STATISTICS_INFO_HISTOGRAM_BUCKET_COUNT,
    357     ANDROID_STATISTICS_INFO_MAX_HISTOGRAM_COUNT,
    358     ANDROID_STATISTICS_INFO_MAX_SHARPNESS_MAP_VALUE,
    359     ANDROID_STATISTICS_INFO_SHARPNESS_MAP_SIZE,
    360     ANDROID_DEPTH_MAX_DEPTH_SAMPLES,
    361 });
    362 
    363 /*~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~
    364  * End generated code
    365  *~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~@~O@*/
    366