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