Home | History | Annotate | Download | only in 3_4
      1 /*
      2  * Copyright 2016 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 "V4L2MetadataFactory"
     19 
     20 #include "v4l2_metadata_factory.h"
     21 
     22 #include <camera/CameraMetadata.h>
     23 #include "common.h"
     24 #include "format_metadata_factory.h"
     25 #include "metadata/boottime_state_delegate.h"
     26 #include "metadata/control.h"
     27 #include "metadata/enum_converter.h"
     28 #include "metadata/partial_metadata_factory.h"
     29 #include "metadata/property.h"
     30 #include "metadata/scaling_converter.h"
     31 
     32 namespace v4l2_camera_hal {
     33 
     34 // According to spec, each unit of V4L2_CID_AUTO_EXPOSURE_BIAS is 0.001 EV.
     35 const camera_metadata_rational_t kAeCompensationUnit = {1, 1000};
     36 // According to spec, each unit of V4L2_CID_EXPOSURE_ABSOLUTE is 100 us.
     37 const int64_t kV4L2ExposureTimeStepNs = 100000;
     38 // According to spec, each unit of V4L2_CID_ISO_SENSITIVITY is ISO/1000.
     39 const int32_t kV4L2SensitivityDenominator = 1000;
     40 // Generously allow up to 6MB (the largest size on the RPi Camera is about 5MB).
     41 const size_t kV4L2MaxJpegSize = 6000000;
     42 
     43 int GetV4L2Metadata(std::shared_ptr<V4L2Wrapper> device,
     44                     std::unique_ptr<Metadata>* result) {
     45   HAL_LOG_ENTER();
     46 
     47   // Open a temporary connection to the device for all the V4L2 querying
     48   // that will be happening (this could be done for each component individually,
     49   // but doing it here prevents connecting and disconnecting for each one).
     50   V4L2Wrapper::Connection temp_connection = V4L2Wrapper::Connection(device);
     51   if (temp_connection.status()) {
     52     HAL_LOGE("Failed to connect to device: %d.", temp_connection.status());
     53     return temp_connection.status();
     54   }
     55 
     56   // TODO(b/30035628): Add states.
     57 
     58   PartialMetadataSet components;
     59 
     60   components.insert(NoEffectMenuControl<uint8_t>(
     61       ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
     62       ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
     63       {ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
     64        ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY},
     65       {{CAMERA3_TEMPLATE_STILL_CAPTURE,
     66         ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY},
     67        {OTHER_TEMPLATES, ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST}}));
     68 
     69   // TODO(b/30510395): subcomponents of 3A.
     70   // In general, default to ON/AUTO since they imply pretty much nothing,
     71   // while OFF implies guarantees about not hindering performance.
     72   components.insert(std::unique_ptr<PartialMetadataInterface>(
     73       new Property<std::array<int32_t, 3>>(ANDROID_CONTROL_MAX_REGIONS,
     74                                            {{/*AE*/ 0, /*AWB*/ 0, /*AF*/ 0}})));
     75   // TODO(b/30921166): V4L2_CID_AUTO_EXPOSURE_BIAS is an int menu, so
     76   // this will be falling back to NoEffect until int menu support is added.
     77   components.insert(V4L2ControlOrDefault<int32_t>(
     78       ControlType::kSlider,
     79       ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
     80       ANDROID_CONTROL_AE_COMPENSATION_RANGE,
     81       device,
     82       V4L2_CID_AUTO_EXPOSURE_BIAS,
     83       // No scaling necessary, AE_COMPENSATION_STEP handles this.
     84       std::make_shared<ScalingConverter<int32_t, int32_t>>(1, 1),
     85       0,
     86       {{OTHER_TEMPLATES, 0}}));
     87   components.insert(std::unique_ptr<PartialMetadataInterface>(
     88       new Property<camera_metadata_rational_t>(
     89           ANDROID_CONTROL_AE_COMPENSATION_STEP, kAeCompensationUnit)));
     90   // TODO(b/31021522): Autofocus subcomponent.
     91   components.insert(
     92       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AF_MODE,
     93                                    ANDROID_CONTROL_AF_AVAILABLE_MODES,
     94                                    {ANDROID_CONTROL_AF_MODE_OFF}));
     95   // TODO(b/31021522): Should read autofocus state from
     96   // V4L2_CID_AUTO_FOCUS_STATUS bitmask. The framework gets a little more
     97   // complex than that does; there's a whole state-machine table in
     98   // the docs (system/media/camera/docs/docs.html).
     99   components.insert(FixedState<uint8_t>(ANDROID_CONTROL_AF_STATE,
    100                                         ANDROID_CONTROL_AF_STATE_INACTIVE));
    101   // TODO(b/31022735): Correctly implement AE & AF triggers that
    102   // actually do something. These no effect triggers are even worse than most
    103   // of the useless controls in this class, since technically they should
    104   // revert back to IDLE eventually after START/CANCEL, but for now they won't
    105   // unless IDLE is requested.
    106   components.insert(
    107       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AF_TRIGGER,
    108                                    DO_NOT_REPORT_OPTIONS,
    109                                    {ANDROID_CONTROL_AF_TRIGGER_IDLE,
    110                                     ANDROID_CONTROL_AF_TRIGGER_START,
    111                                     ANDROID_CONTROL_AF_TRIGGER_CANCEL}));
    112   components.insert(NoEffectMenuControl<uint8_t>(
    113       ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
    114       DO_NOT_REPORT_OPTIONS,
    115       {ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE,
    116        ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START,
    117        ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL}));
    118   components.insert(V4L2ControlOrDefault<uint8_t>(
    119       ControlType::kMenu,
    120       ANDROID_CONTROL_AE_ANTIBANDING_MODE,
    121       ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
    122       device,
    123       V4L2_CID_POWER_LINE_FREQUENCY,
    124       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(
    125           new EnumConverter({{V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
    126                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF},
    127                              {V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
    128                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ},
    129                              {V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
    130                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ},
    131                              {V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
    132                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO}})),
    133       ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO,
    134       {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF},
    135        {OTHER_TEMPLATES, ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO}}));
    136   std::unique_ptr<PartialMetadataInterface> exposure_time =
    137       V4L2Control<int64_t>(ControlType::kSlider,
    138                            ANDROID_SENSOR_EXPOSURE_TIME,
    139                            ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,
    140                            device,
    141                            V4L2_CID_EXPOSURE_ABSOLUTE,
    142                            std::make_shared<ScalingConverter<int64_t, int32_t>>(
    143                                kV4L2ExposureTimeStepNs, 1));
    144   // TODO(b/31037072): Sensitivity has additional V4L2 controls
    145   // (V4L2_CID_ISO_SENSITIVITY_AUTO), so this control currently has
    146   // undefined behavior.
    147   // TODO(b/30921166): V4L2_CID_ISO_SENSITIVITY is an int menu, so
    148   // this will return nullptr until that is added.
    149   std::unique_ptr<PartialMetadataInterface> sensitivity =
    150       V4L2Control<int32_t>(ControlType::kSlider,
    151                            ANDROID_SENSOR_SENSITIVITY,
    152                            ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
    153                            device,
    154                            V4L2_CID_ISO_SENSITIVITY,
    155                            std::make_shared<ScalingConverter<int32_t, int32_t>>(
    156                                1, kV4L2SensitivityDenominator));
    157   std::multimap<int32_t, uint8_t> ae_mode_mapping = {
    158       {V4L2_EXPOSURE_AUTO, ANDROID_CONTROL_AE_MODE_ON}};
    159   if (exposure_time && sensitivity) {
    160     // TODO(b/30510395): as part of coordinated 3A component,
    161     // if these aren't available don't advertise AE mode OFF, only AUTO.
    162     components.insert(std::move(exposure_time));
    163     components.insert(std::move(sensitivity));
    164     ae_mode_mapping.emplace(V4L2_EXPOSURE_MANUAL, ANDROID_CONTROL_AE_MODE_OFF);
    165   }
    166   components.insert(V4L2ControlOrDefault<uint8_t>(
    167       ControlType::kMenu,
    168       ANDROID_CONTROL_AE_MODE,
    169       ANDROID_CONTROL_AE_AVAILABLE_MODES,
    170       device,
    171       V4L2_CID_EXPOSURE_AUTO,
    172       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(
    173           new EnumConverter(ae_mode_mapping)),
    174       ANDROID_CONTROL_AE_MODE_ON,
    175       {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AE_MODE_OFF},
    176        {OTHER_TEMPLATES, ANDROID_CONTROL_AE_MODE_ON}}));
    177   // Can't get AE status from V4L2.
    178   // TODO(b/30510395): If AE mode is OFF, this should switch to INACTIVE.
    179   components.insert(FixedState<uint8_t>(ANDROID_CONTROL_AE_STATE,
    180                                         ANDROID_CONTROL_AE_STATE_CONVERGED));
    181   // V4L2 offers multiple white balance interfaces. Try the advanced one before
    182   // falling
    183   // back to the simpler version.
    184   // Modes from each API that don't match up:
    185   // Android: WARM_FLUORESCENT, TWILIGHT.
    186   // V4L2: FLUORESCENT_H, HORIZON, FLASH.
    187   std::unique_ptr<PartialMetadataInterface> awb(V4L2Control<uint8_t>(
    188       ControlType::kMenu,
    189       ANDROID_CONTROL_AWB_MODE,
    190       ANDROID_CONTROL_AWB_AVAILABLE_MODES,
    191       device,
    192       V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
    193       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
    194           {{V4L2_WHITE_BALANCE_MANUAL, ANDROID_CONTROL_AWB_MODE_OFF},
    195            {V4L2_WHITE_BALANCE_AUTO, ANDROID_CONTROL_AWB_MODE_AUTO},
    196            {V4L2_WHITE_BALANCE_INCANDESCENT,
    197             ANDROID_CONTROL_AWB_MODE_INCANDESCENT},
    198            {V4L2_WHITE_BALANCE_FLUORESCENT,
    199             ANDROID_CONTROL_AWB_MODE_FLUORESCENT},
    200            {V4L2_WHITE_BALANCE_DAYLIGHT, ANDROID_CONTROL_AWB_MODE_DAYLIGHT},
    201            {V4L2_WHITE_BALANCE_CLOUDY,
    202             ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT},
    203            {V4L2_WHITE_BALANCE_SHADE, ANDROID_CONTROL_AWB_MODE_SHADE}})),
    204       {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AWB_MODE_OFF},
    205        {OTHER_TEMPLATES, ANDROID_CONTROL_AWB_MODE_AUTO}}));
    206   if (awb) {
    207     components.insert(std::move(awb));
    208   } else {
    209     // Fall back to simpler AWB or even just an ignored control.
    210     components.insert(V4L2ControlOrDefault<uint8_t>(
    211         ControlType::kMenu,
    212         ANDROID_CONTROL_AWB_MODE,
    213         ANDROID_CONTROL_AWB_AVAILABLE_MODES,
    214         device,
    215         V4L2_CID_AUTO_WHITE_BALANCE,
    216         std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(
    217             new EnumConverter({{0, ANDROID_CONTROL_AWB_MODE_OFF},
    218                                {1, ANDROID_CONTROL_AWB_MODE_AUTO}})),
    219         ANDROID_CONTROL_AWB_MODE_AUTO,
    220         {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AWB_MODE_OFF},
    221          {OTHER_TEMPLATES, ANDROID_CONTROL_AWB_MODE_AUTO}}));
    222   }
    223   // TODO(b/31041577): Handle AWB state machine correctly.
    224   components.insert(FixedState<uint8_t>(ANDROID_CONTROL_AWB_STATE,
    225                                         ANDROID_CONTROL_AWB_STATE_CONVERGED));
    226   // TODO(b/31022153): 3A locks.
    227   components.insert(std::unique_ptr<PartialMetadataInterface>(
    228       new Property<uint8_t>(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
    229                             ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE)));
    230   components.insert(
    231       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AE_LOCK,
    232                                    DO_NOT_REPORT_OPTIONS,
    233                                    {ANDROID_CONTROL_AE_LOCK_OFF}));
    234   components.insert(std::unique_ptr<PartialMetadataInterface>(
    235       new Property<uint8_t>(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
    236                             ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE)));
    237   components.insert(
    238       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AWB_LOCK,
    239                                    DO_NOT_REPORT_OPTIONS,
    240                                    {ANDROID_CONTROL_AWB_LOCK_OFF}));
    241   // TODO(b/30510395): subcomponents of scene modes
    242   // (may itself be a subcomponent of 3A).
    243   // Modes from each API that don't match up:
    244   // Android: FACE_PRIORITY, ACTION, NIGHT_PORTRAIT, THEATRE, STEADYPHOTO,
    245   // BARCODE, HIGH_SPEED_VIDEO.
    246   // V4L2: BACKLIGHT, DAWN_DUSK, FALL_COLORS, TEXT.
    247   components.insert(V4L2ControlOrDefault<uint8_t>(
    248       ControlType::kMenu,
    249       ANDROID_CONTROL_SCENE_MODE,
    250       ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
    251       device,
    252       V4L2_CID_SCENE_MODE,
    253       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
    254           {{V4L2_SCENE_MODE_NONE, ANDROID_CONTROL_SCENE_MODE_DISABLED},
    255            {V4L2_SCENE_MODE_BEACH_SNOW, ANDROID_CONTROL_SCENE_MODE_BEACH},
    256            {V4L2_SCENE_MODE_BEACH_SNOW, ANDROID_CONTROL_SCENE_MODE_SNOW},
    257            {V4L2_SCENE_MODE_CANDLE_LIGHT,
    258             ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT},
    259            {V4L2_SCENE_MODE_FIREWORKS, ANDROID_CONTROL_SCENE_MODE_FIREWORKS},
    260            {V4L2_SCENE_MODE_LANDSCAPE, ANDROID_CONTROL_SCENE_MODE_LANDSCAPE},
    261            {V4L2_SCENE_MODE_NIGHT, ANDROID_CONTROL_SCENE_MODE_NIGHT},
    262            {V4L2_SCENE_MODE_PARTY_INDOOR, ANDROID_CONTROL_SCENE_MODE_PARTY},
    263            {V4L2_SCENE_MODE_SPORTS, ANDROID_CONTROL_SCENE_MODE_SPORTS},
    264            {V4L2_SCENE_MODE_SUNSET, ANDROID_CONTROL_SCENE_MODE_SUNSET}})),
    265       ANDROID_CONTROL_SCENE_MODE_DISABLED));
    266   // TODO(b/31022612): Scene mode overrides.
    267   // Modes from each API that don't match up:
    268   // Android: POSTERIZE, WHITEBOARD, BLACKBOARD.
    269   // V4L2: ANTIQUE, ART_FREEZE, EMBOSS, GRASS_GREEN, SKETCH, SKIN_WHITEN,
    270   // SKY_BLUE, SILHOUETTE, VIVID, SET_CBCR.
    271   components.insert(V4L2ControlOrDefault<uint8_t>(
    272       ControlType::kMenu,
    273       ANDROID_CONTROL_EFFECT_MODE,
    274       ANDROID_CONTROL_AVAILABLE_EFFECTS,
    275       device,
    276       V4L2_CID_COLORFX,
    277       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
    278           {{V4L2_COLORFX_NONE, ANDROID_CONTROL_EFFECT_MODE_OFF},
    279            {V4L2_COLORFX_BW, ANDROID_CONTROL_EFFECT_MODE_MONO},
    280            {V4L2_COLORFX_NEGATIVE, ANDROID_CONTROL_EFFECT_MODE_NEGATIVE},
    281            {V4L2_COLORFX_SOLARIZATION, ANDROID_CONTROL_EFFECT_MODE_SOLARIZE},
    282            {V4L2_COLORFX_SEPIA, ANDROID_CONTROL_EFFECT_MODE_SEPIA},
    283            {V4L2_COLORFX_AQUA, ANDROID_CONTROL_EFFECT_MODE_AQUA}})),
    284       ANDROID_CONTROL_EFFECT_MODE_OFF));
    285   // TODO(b/31021654): This should behave as a top level switch, not no effect.
    286   // Should enforce being set to USE_SCENE_MODE when a scene mode is requested.
    287   components.insert(NoEffectMenuControl<uint8_t>(
    288       ANDROID_CONTROL_MODE,
    289       ANDROID_CONTROL_AVAILABLE_MODES,
    290       {ANDROID_CONTROL_MODE_AUTO, ANDROID_CONTROL_MODE_USE_SCENE_MODE}));
    291 
    292   // Not sure if V4L2 does or doesn't do this, but HAL documentation says
    293   // all devices must support FAST, and FAST can be equivalent to OFF, so
    294   // either way it's fine to list. And if FAST is included, HIGH_QUALITY
    295   // is supposed to be included as well.
    296   components.insert(NoEffectMenuControl<uint8_t>(
    297       ANDROID_EDGE_MODE,
    298       ANDROID_EDGE_AVAILABLE_EDGE_MODES,
    299       {ANDROID_EDGE_MODE_FAST, ANDROID_EDGE_MODE_HIGH_QUALITY},
    300       {{CAMERA3_TEMPLATE_STILL_CAPTURE, ANDROID_EDGE_MODE_HIGH_QUALITY},
    301        {OTHER_TEMPLATES, ANDROID_EDGE_MODE_FAST}}));
    302 
    303   // TODO(b/31023454): subcomponents of flash.
    304   components.insert(
    305       std::unique_ptr<PartialMetadataInterface>(new Property<uint8_t>(
    306           ANDROID_FLASH_INFO_AVAILABLE, ANDROID_FLASH_INFO_AVAILABLE_FALSE)));
    307   components.insert(FixedState<uint8_t>(ANDROID_FLASH_STATE,
    308                                         ANDROID_FLASH_STATE_UNAVAILABLE));
    309   components.insert(NoEffectMenuControl<uint8_t>(
    310       ANDROID_FLASH_MODE, DO_NOT_REPORT_OPTIONS, {ANDROID_FLASH_MODE_OFF}));
    311 
    312   // TODO(30510395): subcomponents of hotpixel.
    313   // No known V4L2 hot pixel correction. But it might be happening,
    314   // so we report FAST/HIGH_QUALITY.
    315   components.insert(NoEffectMenuControl<uint8_t>(
    316       ANDROID_HOT_PIXEL_MODE,
    317       ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
    318       {ANDROID_HOT_PIXEL_MODE_FAST, ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY}));
    319   // ON only needs to be supported for RAW capable devices.
    320   components.insert(NoEffectMenuControl<uint8_t>(
    321       ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
    322       ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
    323       {ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF}));
    324 
    325   // TODO(30510395): subcomponents focus/lens.
    326   // No way to actually get the aperture and focal length
    327   // in V4L2, but they're required keys, so fake them.
    328   components.insert(
    329       NoEffectMenuControl<float>(ANDROID_LENS_APERTURE,
    330                                  ANDROID_LENS_INFO_AVAILABLE_APERTURES,
    331                                  {2.0}));  // RPi camera v2 is f/2.0.
    332   // Always assume external-facing.
    333   components.insert(
    334       std::unique_ptr<PartialMetadataInterface>(new Property<uint8_t>(
    335           ANDROID_LENS_FACING, ANDROID_LENS_FACING_EXTERNAL)));
    336   components.insert(
    337       NoEffectMenuControl<float>(ANDROID_LENS_FOCAL_LENGTH,
    338                                  ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
    339                                  {3.04}));  // RPi camera v2 is 3.04mm.
    340   // No known way to get filter densities from V4L2,
    341   // report 0 to indicate this control is not supported.
    342   components.insert(
    343       NoEffectMenuControl<float>(ANDROID_LENS_FILTER_DENSITY,
    344                                  ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
    345                                  {0.0}));
    346   // V4L2 focal units do not correspond to a particular physical unit.
    347   components.insert(
    348       std::unique_ptr<PartialMetadataInterface>(new Property<uint8_t>(
    349           ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
    350           ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED)));
    351   // TODO(b/31022711): Focus distance component.
    352   // Using a NoEffectMenuControl for now because for
    353   // fixed-focus it meets expectations. Framework may allow
    354   // setting any value and expect it to be clamped to 0, in which
    355   // case this will have unexpected behavior (failing on non-0 settings).
    356   components.insert(
    357       NoEffectMenuControl<float>(ANDROID_LENS_FOCUS_DISTANCE,
    358                                  ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
    359                                  {0}));
    360   // Hypefocal distance doesn't mean much for a fixed-focus uncalibrated device.
    361   components.insert(std::make_unique<Property<float>>(
    362       ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, 0));
    363 
    364   // No way to know when the lens is moving or not in V4L2.
    365   components.insert(
    366       FixedState<uint8_t>(ANDROID_LENS_STATE, ANDROID_LENS_STATE_STATIONARY));
    367   // No known V4L2 lens shading. But it might be happening,
    368   // so report FAST/HIGH_QUALITY.
    369   components.insert(NoEffectMenuControl<uint8_t>(
    370       ANDROID_SHADING_MODE,
    371       ANDROID_SHADING_AVAILABLE_MODES,
    372       {ANDROID_SHADING_MODE_FAST, ANDROID_SHADING_MODE_HIGH_QUALITY}));
    373   // ON only needs to be supported for RAW capable devices.
    374   components.insert(NoEffectMenuControl<uint8_t>(
    375       ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
    376       ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
    377       {ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF}));
    378   // V4L2 doesn't differentiate between OPTICAL and VIDEO stabilization,
    379   // so only report one (and report the other as OFF).
    380   components.insert(V4L2ControlOrDefault<uint8_t>(
    381       ControlType::kMenu,
    382       ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
    383       ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
    384       device,
    385       V4L2_CID_IMAGE_STABILIZATION,
    386       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
    387           {{0, ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF},
    388            {1, ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_ON}})),
    389       ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF));
    390   components.insert(NoEffectMenuControl<uint8_t>(
    391       ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
    392       ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
    393       {ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF}));
    394   // TODO(b/31017806): This should definitely have a different default depending
    395   // on template.
    396   components.insert(NoEffectMenuControl<uint8_t>(
    397       ANDROID_CONTROL_CAPTURE_INTENT,
    398       DO_NOT_REPORT_OPTIONS,
    399       {ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM,
    400        ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW,
    401        ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE,
    402        ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD,
    403        ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT,
    404        ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG,
    405        ANDROID_CONTROL_CAPTURE_INTENT_MANUAL},
    406       {{CAMERA3_TEMPLATE_PREVIEW, ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW},
    407        {CAMERA3_TEMPLATE_STILL_CAPTURE,
    408         ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE},
    409        {CAMERA3_TEMPLATE_VIDEO_RECORD,
    410         ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD},
    411        {CAMERA3_TEMPLATE_VIDEO_SNAPSHOT,
    412         ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT},
    413        {CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG,
    414         ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG},
    415        {CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_CAPTURE_INTENT_MANUAL},
    416        {OTHER_TEMPLATES, ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM}}));
    417 
    418   // Unable to control noise reduction in V4L2 devices,
    419   // but FAST is allowed to be the same as OFF,
    420   // and HIGH_QUALITY can be the same as FAST.
    421   components.insert(NoEffectMenuControl<uint8_t>(
    422       ANDROID_NOISE_REDUCTION_MODE,
    423       ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
    424       {ANDROID_NOISE_REDUCTION_MODE_FAST,
    425        ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY},
    426       {{CAMERA3_TEMPLATE_STILL_CAPTURE,
    427         ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY},
    428        {OTHER_TEMPLATES, ANDROID_NOISE_REDUCTION_MODE_FAST}}));
    429 
    430   // TODO(30510395): subcomponents of formats/streams.
    431   // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
    432   // TODO(b/29580107): Could end up with a mismatch between request & result,
    433   // since V4L2 doesn't actually allow for thumbnail size control.
    434   components.insert(NoEffectMenuControl<std::array<int32_t, 2>>(
    435       ANDROID_JPEG_THUMBNAIL_SIZE,
    436       ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
    437       {{{0, 0}}}));
    438   // TODO(b/31022752): Get this from the device, not constant.
    439   components.insert(std::unique_ptr<PartialMetadataInterface>(
    440       new Property<int32_t>(ANDROID_JPEG_MAX_SIZE, kV4L2MaxJpegSize)));
    441   // TODO(b/31021672): Other JPEG controls (GPS, quality, orientation).
    442   // TODO(b/29939583): V4L2 can only support 1 stream at a time.
    443   // For now, just reporting minimum allowable for LIMITED devices.
    444   components.insert(std::unique_ptr<PartialMetadataInterface>(
    445       new Property<std::array<int32_t, 3>>(
    446           ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
    447           {{/* Raw */ 0, /* Non-stalling */ 2, /* Stalling */ 1}})));
    448   // Reprocessing not supported.
    449   components.insert(std::unique_ptr<PartialMetadataInterface>(
    450       new Property<int32_t>(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, 0)));
    451   // No way to know pipeline depth for V4L2, so fake with max allowable latency.
    452   // Doesn't mean much without per-frame controls anyways.
    453   components.insert(std::unique_ptr<PartialMetadataInterface>(
    454       new Property<uint8_t>(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, 4)));
    455   components.insert(FixedState<uint8_t>(ANDROID_REQUEST_PIPELINE_DEPTH, 4));
    456   // "LIMITED devices are strongly encouraged to use a non-negative value.
    457   // If UNKNOWN is used here then app developers do not have a way to know
    458   // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
    459   // really help here either. Could even be that adjusting settings mid-stream
    460   // blocks in V4L2, and should be avoided.
    461   components.insert(
    462       std::unique_ptr<PartialMetadataInterface>(new Property<int32_t>(
    463           ANDROID_SYNC_MAX_LATENCY, ANDROID_SYNC_MAX_LATENCY_UNKNOWN)));
    464   // Never know when controls are synced.
    465   components.insert(FixedState<int64_t>(ANDROID_SYNC_FRAME_NUMBER,
    466                                         ANDROID_SYNC_FRAME_NUMBER_UNKNOWN));
    467 
    468   // TODO(b/31022480): subcomponents of cropping/sensors.
    469   // Need ANDROID_SCALER_CROP_REGION control support.
    470   // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
    471   // it's driver dependent. For now, assume freeform, and
    472   // some cameras may just behave badly.
    473   // TODO(b/29579652): Figure out a way to determine this.
    474   components.insert(std::unique_ptr<PartialMetadataInterface>(
    475       new Property<float>(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, 1)));
    476   components.insert(std::unique_ptr<PartialMetadataInterface>(
    477       new Property<uint8_t>(ANDROID_SCALER_CROPPING_TYPE,
    478                             ANDROID_SCALER_CROPPING_TYPE_FREEFORM)));
    479   // Spoof pixel array size for now, eventually get from CROPCAP.
    480   std::array<int32_t, 2> pixel_array_size = {{3280, 2464}};
    481   components.insert(std::unique_ptr<PartialMetadataInterface>(
    482       new Property<std::array<int32_t, 2>>(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
    483                                            pixel_array_size)));
    484   // Active array size is {x-offset, y-offset, width, height}, relative to
    485   // the pixel array size, with {0, 0} being the top left. Since there's no way
    486   // to get this in V4L2, assume the full pixel array is the active array.
    487   std::array<int32_t, 4> active_array_size = {
    488       {0, 0, pixel_array_size[0], pixel_array_size[1]}};
    489   components.insert(std::unique_ptr<PartialMetadataInterface>(
    490       new Property<std::array<int32_t, 4>>(
    491           ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, active_array_size)));
    492   // This is really more freeform than a menu control, but since we're
    493   // restricting it to not being used anyways this works for now.
    494   components.insert(NoEffectMenuControl<std::array<int32_t, 4>>(
    495       ANDROID_SCALER_CROP_REGION, DO_NOT_REPORT_OPTIONS, {active_array_size}));
    496   // No way to get in V4L2, so faked. RPi camera v2 is 3.674 x 2.760 mm.
    497   // Physical size is used in framework calculations (field of view,
    498   // pixel pitch, etc.), so faking it may have unexpected results.
    499   components.insert(std::unique_ptr<PartialMetadataInterface>(
    500       new Property<std::array<float, 2>>(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
    501                                          {{3.674, 2.760}})));
    502   // HAL uses BOOTTIME timestamps.
    503   // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
    504   components.insert(std::unique_ptr<PartialMetadataInterface>(
    505       new Property<uint8_t>(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
    506                             ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN)));
    507   components.insert(std::make_unique<State<int64_t>>(
    508       ANDROID_SENSOR_TIMESTAMP, std::make_unique<BoottimeStateDelegate>()));
    509   // No way to actually get shutter skew from V4L2.
    510   components.insert(
    511       FixedState<int64_t>(ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, 0));
    512   // No way to actually get orientation from V4L2.
    513   components.insert(std::unique_ptr<PartialMetadataInterface>(
    514       new Property<int32_t>(ANDROID_SENSOR_ORIENTATION, 0)));
    515   // TODO(b/31023611): Sensor frame duration. Range should
    516   // be dependent on the stream configuration being used.
    517   // No test patterns supported.
    518   components.insert(
    519       NoEffectMenuControl<int32_t>(ANDROID_SENSOR_TEST_PATTERN_MODE,
    520                                    ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES,
    521                                    {ANDROID_SENSOR_TEST_PATTERN_MODE_OFF}));
    522 
    523   // TODO(b/30510395): subcomponents of face detection.
    524   // Face detection not supported.
    525   components.insert(NoEffectMenuControl<uint8_t>(
    526       ANDROID_STATISTICS_FACE_DETECT_MODE,
    527       ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
    528       {ANDROID_STATISTICS_FACE_DETECT_MODE_OFF}));
    529   components.insert(std::unique_ptr<PartialMetadataInterface>(
    530       new Property<int32_t>(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, 0)));
    531 
    532   // No way to get detected scene flicker from V4L2.
    533   components.insert(FixedState<uint8_t>(ANDROID_STATISTICS_SCENE_FLICKER,
    534                                         ANDROID_STATISTICS_SCENE_FLICKER_NONE));
    535 
    536   // TOOD(b/31023265): V4L2_CID_FLASH_INDICATOR_INTENSITY could be queried
    537   // to see if there's a transmit LED. Would need to translate HAL off/on
    538   // enum to slider min/max value. For now, no LEDs available.
    539   components.insert(std::unique_ptr<PartialMetadataInterface>(
    540       new Property<uint8_t>(ANDROID_LED_AVAILABLE_LEDS, {})));
    541 
    542   /* Capabilities. */
    543   // The V4L2Metadata pretends to at least meet the
    544   // "LIMITED" and "BACKWARD_COMPATIBLE" functionality requirements.
    545   components.insert(std::unique_ptr<PartialMetadataInterface>(
    546       new Property<uint8_t>(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
    547                             ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED)));
    548   components.insert(std::unique_ptr<PartialMetadataInterface>(
    549       new Property<std::vector<uint8_t>>(
    550           ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
    551           {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})));
    552 
    553   // Request is unused, and can be any value,
    554   // but that value needs to be propagated.
    555   components.insert(NoEffectOptionlessControl<int32_t>(ANDROID_REQUEST_ID, 0));
    556 
    557   // Metadata is returned in a single result; not multiple pieces.
    558   components.insert(std::make_unique<Property<int32_t>>(
    559       ANDROID_REQUEST_PARTIAL_RESULT_COUNT, 1));
    560 
    561   int res =
    562       AddFormatComponents(device, std::inserter(components, components.end()));
    563   if (res) {
    564     HAL_LOGE("Failed to initialize format components.");
    565     return res;
    566   }
    567 
    568   *result = std::make_unique<Metadata>(std::move(components));
    569   return 0;
    570 }
    571 
    572 }  // namespace v4l2_camera_hal
    573