• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "metadata_reader.h"
18 
19 // #define LOG_NDEBUG 0
20 #define LOG_TAG "MetadataReader"
21 #include <cutils/log.h>
22 #include <system/camera.h>
23 
24 #include "metadata_common.h"
25 
26 namespace default_camera_hal {
27 
MetadataReader(std::unique_ptr<const android::CameraMetadata> metadata)28 MetadataReader::MetadataReader(
29     std::unique_ptr<const android::CameraMetadata> metadata)
30     : metadata_(std::move(metadata)) {}
31 
~MetadataReader()32 MetadataReader::~MetadataReader() {}
33 
Facing(int * facing) const34 int MetadataReader::Facing(int* facing) const {
35   uint8_t metadata_facing = 0;
36   int res = v4l2_camera_hal::SingleTagValue(
37       *metadata_, ANDROID_LENS_FACING, &metadata_facing);
38   if (res) {
39     ALOGE("%s: Failed to get facing from static metadata.", __func__);
40     return res;
41   }
42 
43   switch (metadata_facing) {
44     case (ANDROID_LENS_FACING_FRONT):
45       *facing = CAMERA_FACING_FRONT;
46       break;
47     case (ANDROID_LENS_FACING_BACK):
48       *facing = CAMERA_FACING_BACK;
49       break;
50     case (ANDROID_LENS_FACING_EXTERNAL):
51       *facing = CAMERA_FACING_EXTERNAL;
52       break;
53     default:
54       ALOGE("%s: Invalid facing from static metadata: %d.",
55             __func__,
56             metadata_facing);
57       return -EINVAL;
58   }
59   return 0;
60 }
61 
Orientation(int * orientation) const62 int MetadataReader::Orientation(int* orientation) const {
63   int32_t metadata_orientation = 0;
64   int res = v4l2_camera_hal::SingleTagValue(
65       *metadata_, ANDROID_SENSOR_ORIENTATION, &metadata_orientation);
66   if (res) {
67     ALOGE("%s: Failed to get orientation from static metadata.", __func__);
68     return res;
69   }
70 
71   // Orientation must be 0, 90, 180, or 270.
72   if (metadata_orientation < 0 || metadata_orientation > 270 ||
73       metadata_orientation % 90 != 0) {
74     ALOGE(
75         "%s: Invalid orientation %d "
76         "(must be a 90-degree increment in [0, 360)).",
77         __func__,
78         metadata_orientation);
79     return -EINVAL;
80   }
81 
82   *orientation = static_cast<int>(metadata_orientation);
83   return 0;
84 }
85 
MaxInputStreams(int32_t * max_input) const86 int MetadataReader::MaxInputStreams(int32_t* max_input) const {
87   int res = v4l2_camera_hal::SingleTagValue(
88       *metadata_, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, max_input);
89   if (res == -ENOENT) {
90     // Not required; default to 0.
91     *max_input = 0;
92   } else if (res) {
93     ALOGE("%s: Failed to get max output streams from static metadata.",
94           __func__);
95     return res;
96   }
97   return 0;
98 }
99 
MaxOutputStreams(int32_t * max_raw,int32_t * max_non_stalling,int32_t * max_stalling) const100 int MetadataReader::MaxOutputStreams(int32_t* max_raw,
101                                      int32_t* max_non_stalling,
102                                      int32_t* max_stalling) const {
103   std::array<int32_t, 3> max_output_streams;
104   int res = v4l2_camera_hal::SingleTagValue(
105       *metadata_, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams);
106   if (res) {
107     ALOGE("%s: Failed to get max output streams from static metadata.",
108           __func__);
109     return res;
110   }
111   *max_raw = max_output_streams[0];
112   *max_non_stalling = max_output_streams[1];
113   *max_stalling = max_output_streams[2];
114   return 0;
115 }
116 
RequestCapabilities(std::set<uint8_t> * capabilities) const117 int MetadataReader::RequestCapabilities(std::set<uint8_t>* capabilities) const {
118   std::vector<uint8_t> raw_capabilities;
119   int res = v4l2_camera_hal::VectorTagValue(
120       *metadata_, ANDROID_REQUEST_AVAILABLE_CAPABILITIES, &raw_capabilities);
121   if (res) {
122     ALOGE("%s: Failed to get request capabilities from static metadata.",
123           __func__);
124     return res;
125   }
126 
127   // Move from vector to set.
128   capabilities->insert(raw_capabilities.begin(), raw_capabilities.end());
129   return 0;
130 }
131 
StreamConfigurations(std::vector<StreamConfiguration> * configs) const132 int MetadataReader::StreamConfigurations(
133     std::vector<StreamConfiguration>* configs) const {
134   std::vector<RawStreamConfiguration> raw_stream_configs;
135   int res = v4l2_camera_hal::VectorTagValue(
136       *metadata_,
137       ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
138       &raw_stream_configs);
139   if (res) {
140     ALOGE("%s: Failed to get stream configs from static metadata.", __func__);
141     return res;
142   }
143 
144   // TODO(b/31384253): check for required configs.
145 
146   // Convert from raw.
147   configs->insert(
148       configs->end(), raw_stream_configs.begin(), raw_stream_configs.end());
149 
150   // Check that all configs are valid.
151   for (const auto& config : *configs) {
152     // Must have positive dimensions.
153     if (config.spec.width < 1 || config.spec.height < 1) {
154       ALOGE("%s: Invalid stream config: non-positive dimensions (%d, %d).",
155             __func__,
156             config.spec.width,
157             config.spec.height);
158       return -EINVAL;
159     }
160     // Must have a known direction enum.
161     switch (config.direction) {
162       case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT:
163       case ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT:
164         break;
165       default:
166         ALOGE("%s: Invalid stream config direction: %d.",
167               __func__,
168               config.direction);
169         return -EINVAL;
170     }
171   }
172   return 0;
173 }
174 
StreamStallDurations(std::vector<StreamStallDuration> * stalls) const175 int MetadataReader::StreamStallDurations(
176     std::vector<StreamStallDuration>* stalls) const {
177   std::vector<RawStreamStallDuration> raw_stream_stall_durations;
178   int res =
179       v4l2_camera_hal::VectorTagValue(*metadata_,
180                                       ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
181                                       &raw_stream_stall_durations);
182   if (res) {
183     ALOGE("%s: Failed to get stall durations from static metadata.", __func__);
184     return res;
185   }
186 
187   // Convert from raw.
188   stalls->insert(stalls->end(),
189                  raw_stream_stall_durations.begin(),
190                  raw_stream_stall_durations.end());
191   // Check that all stalls are valid.
192   for (const auto& stall : *stalls) {
193     // Must have positive dimensions.
194     if (stall.spec.width < 1 || stall.spec.height < 1) {
195       ALOGE("%s: Invalid stall duration: non-positive dimensions (%d, %d).",
196             __func__,
197             stall.spec.width,
198             stall.spec.height);
199       return -EINVAL;
200     }
201     // Must have a non-negative stall.
202     if (stall.duration < 0) {
203       ALOGE("%s: Invalid stall duration: negative stall %d.",
204             __func__,
205             stall.duration);
206       return -EINVAL;
207     }
208     // TODO(b/31384253): YUV_420_888, RAW10, RAW12, RAW_OPAQUE,
209     // and IMPLEMENTATION_DEFINED must have 0 stall duration.
210   }
211 
212   return 0;
213 }
214 
ReprocessFormats(ReprocessFormatMap * reprocess_map) const215 int MetadataReader::ReprocessFormats(ReprocessFormatMap* reprocess_map) const {
216   std::vector<int32_t> input_output_formats;
217   int res = v4l2_camera_hal::VectorTagValue(
218       *metadata_,
219       ANDROID_SCALER_AVAILABLE_INPUT_OUTPUT_FORMATS_MAP,
220       &input_output_formats);
221   if (res) {
222     ALOGE("%s: Failed to get input output format map from static metadata.",
223           __func__);
224     return res;
225   }
226 
227   // Convert from the raw vector.
228   for (size_t i = 0; i < input_output_formats.size();) {
229     // The map is represented as variable-length entries of the format
230     // input, num_outputs, <outputs>.
231 
232     // Get the input format.
233     int32_t input_format = input_output_formats[i++];
234 
235     // Find the output begin and end for this format.
236     int32_t num_output_formats = input_output_formats[i++];
237     if (num_output_formats < 1) {
238       ALOGE(
239           "%s: No output formats for input format %d.", __func__, input_format);
240       return -EINVAL;
241     }
242     size_t outputs_end = i + num_output_formats;
243     if (outputs_end > input_output_formats.size()) {
244       ALOGE("%s: Input format %d requests more data than available.",
245             __func__,
246             input_format);
247       return -EINVAL;
248     }
249 
250     // Copy all the output formats into the map.
251     (*reprocess_map)[input_format].insert(
252         input_output_formats.data() + i,
253         input_output_formats.data() + outputs_end);
254 
255     // Move on to the next entry.
256     i = outputs_end;
257   }
258 
259   // TODO(b/31384253): check for required mappings.
260 
261   return 0;
262 }
263 
264 }  // namespace default_camera_hal
265