1 /*
2 * Copyright (C) 2019 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_TAG "CameraServerExifUtils"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <cutils/log.h>
22
23 #include <inttypes.h>
24 #include <math.h>
25 #include <stdint.h>
26 #include <string>
27 #include <vector>
28
29 #include "ExifUtils.h"
30
31 extern "C" {
32 #include <libexif/exif-data.h>
33 }
34
35 namespace std {
36
37 template <>
38 struct default_delete<ExifEntry> {
operator ()std::default_delete39 inline void operator()(ExifEntry* entry) const { exif_entry_unref(entry); }
40 };
41
42 } // namespace std
43
44
45 namespace android {
46 namespace camera3 {
47
48
49 class ExifUtilsImpl : public ExifUtils {
50 public:
51 ExifUtilsImpl();
52
53 virtual ~ExifUtilsImpl();
54
55 // Initialize() can be called multiple times. The setting of Exif tags will be
56 // cleared.
57 virtual bool initialize(const unsigned char *app1Segment, size_t app1SegmentSize);
58 virtual bool initializeEmpty();
59
60 // set all known fields from a metadata structure
61 virtual bool setFromMetadata(const CameraMetadata& metadata,
62 const CameraMetadata& staticInfo,
63 const size_t imageWidth,
64 const size_t imageHeight);
65
66 // sets the len aperture.
67 // Returns false if memory allocation fails.
68 virtual bool setAperture(float aperture);
69
70 // sets the color space.
71 // Returns false if memory allocation fails.
72 virtual bool setColorSpace(uint16_t color_space);
73
74 // sets the date and time of image last modified. It takes local time. The
75 // name of the tag is DateTime in IFD0.
76 // Returns false if memory allocation fails.
77 virtual bool setDateTime(const struct tm& t);
78
79 // sets the digital zoom ratio. If the numerator is 0, it means digital zoom
80 // was not used.
81 // Returns false if memory allocation fails.
82 virtual bool setDigitalZoomRatio(
83 uint32_t crop_width, uint32_t crop_height,
84 uint32_t sensor_width, uint32_t sensor_height);
85
86 // Sets the exposure bias.
87 // Returns false if memory allocation fails.
88 virtual bool setExposureBias(int32_t ev,
89 uint32_t ev_step_numerator, uint32_t ev_step_denominator);
90
91 // sets the exposure mode set when the image was shot.
92 // Returns false if memory allocation fails.
93 virtual bool setExposureMode(uint8_t exposure_mode);
94
95 // sets the exposure time, given in seconds.
96 // Returns false if memory allocation fails.
97 virtual bool setExposureTime(float exposure_time);
98
99 // sets the status of flash.
100 // Returns false if memory allocation fails.
101 virtual bool setFlash(uint8_t flash_available, uint8_t flash_state, uint8_t ae_mode);
102
103 // sets the F number.
104 // Returns false if memory allocation fails.
105 virtual bool setFNumber(float f_number);
106
107 // sets the focal length of lens used to take the image in millimeters.
108 // Returns false if memory allocation fails.
109 virtual bool setFocalLength(float focal_length);
110
111 // sets the focal length of lens for 35mm film used to take the image in millimeters.
112 // Returns false if memory allocation fails.
113 virtual bool setFocalLengthIn35mmFilm(float focal_length,
114 float sensor_size_x, float sensor_size_y);
115
116 // sets the altitude in meters.
117 // Returns false if memory allocation fails.
118 virtual bool setGpsAltitude(double altitude);
119
120 // sets the latitude with degrees minutes seconds format.
121 // Returns false if memory allocation fails.
122 virtual bool setGpsLatitude(double latitude);
123
124 // sets the longitude with degrees minutes seconds format.
125 // Returns false if memory allocation fails.
126 virtual bool setGpsLongitude(double longitude);
127
128 // sets GPS processing method.
129 // Returns false if memory allocation fails.
130 virtual bool setGpsProcessingMethod(const std::string& method);
131
132 // sets GPS date stamp and time stamp (atomic clock). It takes UTC time.
133 // Returns false if memory allocation fails.
134 virtual bool setGpsTimestamp(const struct tm& t);
135
136 // sets the length (number of rows) of main image.
137 // Returns false if memory allocation fails.
138 virtual bool setImageHeight(uint32_t length);
139
140 // sets the width (number of columes) of main image.
141 // Returns false if memory allocation fails.
142 virtual bool setImageWidth(uint32_t width);
143
144 // sets the ISO speed.
145 // Returns false if memory allocation fails.
146 virtual bool setIsoSpeedRating(uint16_t iso_speed_ratings);
147
148 // sets the smallest F number of the lens.
149 // Returns false if memory allocation fails.
150 virtual bool setMaxAperture(float aperture);
151
152 // sets image orientation.
153 // Returns false if memory allocation fails.
154 virtual bool setOrientation(uint16_t degrees);
155
156 // sets image orientation.
157 // Returns false if memory allocation fails.
158 virtual bool setOrientationValue(ExifOrientation orientationValue);
159
160 // sets the shutter speed.
161 // Returns false if memory allocation fails.
162 virtual bool setShutterSpeed(float exposure_time);
163
164 // sets the distance to the subject, given in meters.
165 // Returns false if memory allocation fails.
166 virtual bool setSubjectDistance(float diopters);
167
168 // sets the fractions of seconds for the <DateTime> tag.
169 // Returns false if memory allocation fails.
170 virtual bool setSubsecTime(const std::string& subsec_time);
171
172 // sets the white balance mode set when the image was shot.
173 // Returns false if memory allocation fails.
174 virtual bool setWhiteBalance(uint8_t white_balance);
175
176 // Generates APP1 segment.
177 // Returns false if generating APP1 segment fails.
178 virtual bool generateApp1();
179
180 // Gets buffer of APP1 segment. This method must be called only after calling
181 // GenerateAPP1().
182 virtual const uint8_t* getApp1Buffer();
183
184 // Gets length of APP1 segment. This method must be called only after calling
185 // GenerateAPP1().
186 virtual unsigned int getApp1Length();
187
188 protected:
189 // sets the version of this standard supported.
190 // Returns false if memory allocation fails.
191 virtual bool setExifVersion(const std::string& exif_version);
192
193 // Resets the pointers and memories.
194 virtual void reset();
195
196 // Adds a variable length tag to |exif_data_|. It will remove the original one
197 // if the tag exists.
198 // Returns the entry of the tag. The reference count of returned ExifEntry is
199 // two.
200 virtual std::unique_ptr<ExifEntry> addVariableLengthEntry(ExifIfd ifd,
201 ExifTag tag, ExifFormat format, uint64_t components, unsigned int size);
202
203 // Adds a entry of |tag| in |exif_data_|. It won't remove the original one if
204 // the tag exists.
205 // Returns the entry of the tag. It adds one reference count to returned
206 // ExifEntry.
207 virtual std::unique_ptr<ExifEntry> addEntry(ExifIfd ifd, ExifTag tag);
208
209 // Helpe functions to add exif data with different types.
210 virtual bool setShort(ExifIfd ifd, ExifTag tag, uint16_t value, const std::string& msg);
211
212 virtual bool setLong(ExifIfd ifd, ExifTag tag, uint32_t value, const std::string& msg);
213
214 virtual bool setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator,
215 uint32_t denominator, const std::string& msg);
216
217 virtual bool setSRational(ExifIfd ifd, ExifTag tag, int32_t numerator,
218 int32_t denominator, const std::string& msg);
219
220 virtual bool setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
221 const std::string& buffer, const std::string& msg);
222
convertToApex(float val)223 float convertToApex(float val) {
224 return 2.0f * log2f(val);
225 }
226
227 // Destroys the buffer of APP1 segment if exists.
228 virtual void destroyApp1();
229
230 // The Exif data (APP1). Owned by this class.
231 ExifData* exif_data_;
232 // The raw data of APP1 segment. It's allocated by ExifMem in |exif_data_| but
233 // owned by this class.
234 uint8_t* app1_buffer_;
235 // The length of |app1_buffer_|.
236 unsigned int app1_length_;
237
238 // How precise the float-to-rational conversion for EXIF tags would be.
239 const static int kRationalPrecision = 10000;
240 };
241
242 #define SET_SHORT(ifd, tag, value) \
243 do { \
244 if (setShort(ifd, tag, value, #tag) == false) \
245 return false; \
246 } while (0);
247
248 #define SET_LONG(ifd, tag, value) \
249 do { \
250 if (setLong(ifd, tag, value, #tag) == false) \
251 return false; \
252 } while (0);
253
254 #define SET_RATIONAL(ifd, tag, numerator, denominator) \
255 do { \
256 if (setRational(ifd, tag, numerator, denominator, #tag) == false) \
257 return false; \
258 } while (0);
259
260 #define SET_SRATIONAL(ifd, tag, numerator, denominator) \
261 do { \
262 if (setSRational(ifd, tag, numerator, denominator, #tag) == false) \
263 return false; \
264 } while (0);
265
266 #define SET_STRING(ifd, tag, format, buffer) \
267 do { \
268 if (setString(ifd, tag, format, buffer, #tag) == false) \
269 return false; \
270 } while (0);
271
272 // This comes from the Exif Version 2.2 standard table 6.
273 const char gExifAsciiPrefix[] = {0x41, 0x53, 0x43, 0x49, 0x49, 0x0, 0x0, 0x0};
274
setLatitudeOrLongitudeData(unsigned char * data,double num)275 static void setLatitudeOrLongitudeData(unsigned char* data, double num) {
276 // Take the integer part of |num|.
277 ExifLong degrees = static_cast<ExifLong>(num);
278 ExifLong minutes = static_cast<ExifLong>(60 * (num - degrees));
279 ExifLong microseconds =
280 static_cast<ExifLong>(3600000000u * (num - degrees - minutes / 60.0));
281 exif_set_rational(data, EXIF_BYTE_ORDER_INTEL, {degrees, 1});
282 exif_set_rational(data + sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL, {minutes, 1});
283 exif_set_rational(data + 2 * sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL,
284 {microseconds, 1000000});
285 }
286
create()287 ExifUtils *ExifUtils::create() {
288 return new ExifUtilsImpl();
289 }
290
~ExifUtils()291 ExifUtils::~ExifUtils() {
292 }
293
ExifUtilsImpl()294 ExifUtilsImpl::ExifUtilsImpl()
295 : exif_data_(nullptr), app1_buffer_(nullptr), app1_length_(0) {}
296
~ExifUtilsImpl()297 ExifUtilsImpl::~ExifUtilsImpl() {
298 reset();
299 }
300
301
initialize(const unsigned char * app1Segment,size_t app1SegmentSize)302 bool ExifUtilsImpl::initialize(const unsigned char *app1Segment, size_t app1SegmentSize) {
303 reset();
304 exif_data_ = exif_data_new_from_data(app1Segment, app1SegmentSize);
305 if (exif_data_ == nullptr) {
306 ALOGE("%s: allocate memory for exif_data_ failed", __FUNCTION__);
307 return false;
308 }
309 // set the image options.
310 exif_data_set_option(exif_data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
311 exif_data_set_data_type(exif_data_, EXIF_DATA_TYPE_COMPRESSED);
312 exif_data_set_byte_order(exif_data_, EXIF_BYTE_ORDER_INTEL);
313
314 // set exif version to 2.2.
315 if (!setExifVersion("0220")) {
316 return false;
317 }
318
319 return true;
320 }
321
initializeEmpty()322 bool ExifUtilsImpl::initializeEmpty() {
323 reset();
324 exif_data_ = exif_data_new();
325 if (exif_data_ == nullptr) {
326 ALOGE("%s: allocate memory for exif_data_ failed", __FUNCTION__);
327 return false;
328 }
329 // set the image options.
330 exif_data_set_option(exif_data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
331 exif_data_set_data_type(exif_data_, EXIF_DATA_TYPE_COMPRESSED);
332 exif_data_set_byte_order(exif_data_, EXIF_BYTE_ORDER_INTEL);
333
334 // set exif version to 2.2.
335 if (!setExifVersion("0220")) {
336 return false;
337 }
338
339 return true;
340 }
341
setAperture(float aperture)342 bool ExifUtilsImpl::setAperture(float aperture) {
343 float apexValue = convertToApex(aperture);
344 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_APERTURE_VALUE,
345 static_cast<uint32_t>(std::round(apexValue * kRationalPrecision)),
346 kRationalPrecision);
347 return true;
348 }
349
setColorSpace(uint16_t color_space)350 bool ExifUtilsImpl::setColorSpace(uint16_t color_space) {
351 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_COLOR_SPACE, color_space);
352 return true;
353 }
354
setDateTime(const struct tm & t)355 bool ExifUtilsImpl::setDateTime(const struct tm& t) {
356 // The length is 20 bytes including NULL for termination in Exif standard.
357 char str[20];
358 int result = snprintf(str, sizeof(str), "%04i:%02i:%02i %02i:%02i:%02i",
359 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
360 if (result != sizeof(str) - 1) {
361 ALOGW("%s: Input time is invalid", __FUNCTION__);
362 return false;
363 }
364 std::string buffer(str);
365 SET_STRING(EXIF_IFD_0, EXIF_TAG_DATE_TIME, EXIF_FORMAT_ASCII, buffer);
366 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_FORMAT_ASCII, buffer);
367 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_DIGITIZED, EXIF_FORMAT_ASCII, buffer);
368 return true;
369 }
370
setDigitalZoomRatio(uint32_t crop_width,uint32_t crop_height,uint32_t sensor_width,uint32_t sensor_height)371 bool ExifUtilsImpl::setDigitalZoomRatio(
372 uint32_t crop_width, uint32_t crop_height,
373 uint32_t sensor_width, uint32_t sensor_height) {
374 float zoomRatioX = (crop_width == 0) ? 1.0 : 1.0 * sensor_width / crop_width;
375 float zoomRatioY = (crop_height == 0) ? 1.0 : 1.0 * sensor_height / crop_height;
376 float zoomRatio = std::max(zoomRatioX, zoomRatioY);
377 const static float noZoomThreshold = 1.02f;
378
379 if (zoomRatio <= noZoomThreshold) {
380 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_DIGITAL_ZOOM_RATIO, 0, 1);
381 } else {
382 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_DIGITAL_ZOOM_RATIO,
383 static_cast<uint32_t>(std::round(zoomRatio * kRationalPrecision)),
384 kRationalPrecision);
385 }
386 return true;
387 }
388
setExposureMode(uint8_t exposure_mode)389 bool ExifUtilsImpl::setExposureMode(uint8_t exposure_mode) {
390 uint16_t exposureMode = (exposure_mode == ANDROID_CONTROL_AE_MODE_OFF) ? 1 : 0;
391 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_MODE, exposureMode);
392 return true;
393 }
394
setExposureTime(float exposure_time)395 bool ExifUtilsImpl::setExposureTime(float exposure_time) {
396 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_TIME,
397 static_cast<uint32_t>(std::round(exposure_time * kRationalPrecision)),
398 kRationalPrecision);
399 return true;
400 }
401
setFlash(uint8_t flash_available,uint8_t flash_state,uint8_t ae_mode)402 bool ExifUtilsImpl::setFlash(uint8_t flash_available, uint8_t flash_state, uint8_t ae_mode) {
403 // EXIF_TAG_FLASH bits layout per EXIF standard:
404 // Bit 0: 0 - did not fire
405 // 1 - fired
406 // Bit 1-2: status of return light
407 // Bit 3-4: 0 - unknown
408 // 1 - compulsory flash firing
409 // 2 - compulsory flash suppression
410 // 3 - auto mode
411 // Bit 5: 0 - flash function present
412 // 1 - no flash function
413 // Bit 6: 0 - no red-eye reduction mode or unknown
414 // 1 - red-eye reduction supported
415 uint16_t flash = 0x20;
416
417 if (flash_available == ANDROID_FLASH_INFO_AVAILABLE_TRUE) {
418 flash = 0x00;
419
420 if (flash_state == ANDROID_FLASH_STATE_FIRED) {
421 flash |= 0x1;
422 }
423 if (ae_mode == ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE) {
424 flash |= 0x40;
425 }
426
427 uint16_t flashMode = 0;
428 switch (ae_mode) {
429 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
430 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE:
431 flashMode = 3; // AUTO
432 break;
433 case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
434 case ANDROID_CONTROL_AE_MODE_ON_EXTERNAL_FLASH:
435 flashMode = 1; // ON
436 break;
437 case ANDROID_CONTROL_AE_MODE_OFF:
438 case ANDROID_CONTROL_AE_MODE_ON:
439 flashMode = 2; // OFF
440 break;
441 default:
442 flashMode = 0; // UNKNOWN
443 break;
444 }
445 flash |= (flashMode << 3);
446 }
447 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_FLASH, flash);
448 return true;
449 }
450
setFNumber(float f_number)451 bool ExifUtilsImpl::setFNumber(float f_number) {
452 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_FNUMBER,
453 static_cast<uint32_t>(std::round(f_number * kRationalPrecision)),
454 kRationalPrecision);
455 return true;
456 }
457
setFocalLength(float focal_length)458 bool ExifUtilsImpl::setFocalLength(float focal_length) {
459 uint32_t numerator = static_cast<uint32_t>(std::round(focal_length * kRationalPrecision));
460 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_FOCAL_LENGTH, numerator, kRationalPrecision);
461 return true;
462 }
463
setFocalLengthIn35mmFilm(float focal_length,float sensor_size_x,float sensor_size_y)464 bool ExifUtilsImpl::setFocalLengthIn35mmFilm(
465 float focal_length, float sensor_size_x, float sensor_size_y) {
466 static const float filmDiagonal = 43.27; // diagonal of 35mm film
467 static const float minSensorDiagonal = 0.01;
468 float sensorDiagonal = std::sqrt(
469 sensor_size_x * sensor_size_x + sensor_size_y * sensor_size_y);
470 sensorDiagonal = std::max(sensorDiagonal, minSensorDiagonal);
471 float focalLength35mmFilm = std::round(focal_length * filmDiagonal / sensorDiagonal);
472 focalLength35mmFilm = std::min(1.0f * 65535, focalLength35mmFilm);
473
474 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM,
475 static_cast<uint16_t>(focalLength35mmFilm));
476 return true;
477 }
478
setGpsAltitude(double altitude)479 bool ExifUtilsImpl::setGpsAltitude(double altitude) {
480 ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE_REF);
481 std::unique_ptr<ExifEntry> refEntry =
482 addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_BYTE, 1, 1);
483 if (!refEntry) {
484 ALOGE("%s: Adding GPSAltitudeRef exif entry failed", __FUNCTION__);
485 return false;
486 }
487 if (altitude >= 0) {
488 *refEntry->data = 0;
489 } else {
490 *refEntry->data = 1;
491 altitude *= -1;
492 }
493
494 ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE);
495 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(
496 EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 1, sizeof(ExifRational));
497 if (!entry) {
498 exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get());
499 ALOGE("%s: Adding GPSAltitude exif entry failed", __FUNCTION__);
500 return false;
501 }
502 exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL,
503 {static_cast<ExifLong>(altitude * 1000), 1000});
504
505 return true;
506 }
507
setGpsLatitude(double latitude)508 bool ExifUtilsImpl::setGpsLatitude(double latitude) {
509 const ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE_REF);
510 std::unique_ptr<ExifEntry> refEntry =
511 addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_ASCII, 2, 2);
512 if (!refEntry) {
513 ALOGE("%s: Adding GPSLatitudeRef exif entry failed", __FUNCTION__);
514 return false;
515 }
516 if (latitude >= 0) {
517 memcpy(refEntry->data, "N", sizeof("N"));
518 } else {
519 memcpy(refEntry->data, "S", sizeof("S"));
520 latitude *= -1;
521 }
522
523 const ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE);
524 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(
525 EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational));
526 if (!entry) {
527 exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get());
528 ALOGE("%s: Adding GPSLatitude exif entry failed", __FUNCTION__);
529 return false;
530 }
531 setLatitudeOrLongitudeData(entry->data, latitude);
532
533 return true;
534 }
535
setGpsLongitude(double longitude)536 bool ExifUtilsImpl::setGpsLongitude(double longitude) {
537 ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE_REF);
538 std::unique_ptr<ExifEntry> refEntry =
539 addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_ASCII, 2, 2);
540 if (!refEntry) {
541 ALOGE("%s: Adding GPSLongitudeRef exif entry failed", __FUNCTION__);
542 return false;
543 }
544 if (longitude >= 0) {
545 memcpy(refEntry->data, "E", sizeof("E"));
546 } else {
547 memcpy(refEntry->data, "W", sizeof("W"));
548 longitude *= -1;
549 }
550
551 ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE);
552 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(
553 EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational));
554 if (!entry) {
555 exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get());
556 ALOGE("%s: Adding GPSLongitude exif entry failed", __FUNCTION__);
557 return false;
558 }
559 setLatitudeOrLongitudeData(entry->data, longitude);
560
561 return true;
562 }
563
setGpsProcessingMethod(const std::string & method)564 bool ExifUtilsImpl::setGpsProcessingMethod(const std::string& method) {
565 std::string buffer =
566 std::string(gExifAsciiPrefix, sizeof(gExifAsciiPrefix)) + method;
567 SET_STRING(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_PROCESSING_METHOD),
568 EXIF_FORMAT_UNDEFINED, buffer);
569 return true;
570 }
571
setGpsTimestamp(const struct tm & t)572 bool ExifUtilsImpl::setGpsTimestamp(const struct tm& t) {
573 const ExifTag dateTag = static_cast<ExifTag>(EXIF_TAG_GPS_DATE_STAMP);
574 const size_t kGpsDateStampSize = 11;
575 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(EXIF_IFD_GPS,
576 dateTag, EXIF_FORMAT_ASCII, kGpsDateStampSize, kGpsDateStampSize);
577 if (!entry) {
578 ALOGE("%s: Adding GPSDateStamp exif entry failed", __FUNCTION__);
579 return false;
580 }
581 int result = snprintf(reinterpret_cast<char*>(entry->data), kGpsDateStampSize,
582 "%04i:%02i:%02i", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
583 if (result != kGpsDateStampSize - 1) {
584 ALOGW("%s: Input time is invalid", __FUNCTION__);
585 return false;
586 }
587
588 const ExifTag timeTag = static_cast<ExifTag>(EXIF_TAG_GPS_TIME_STAMP);
589 entry = addVariableLengthEntry(EXIF_IFD_GPS, timeTag, EXIF_FORMAT_RATIONAL, 3,
590 3 * sizeof(ExifRational));
591 if (!entry) {
592 ALOGE("%s: Adding GPSTimeStamp exif entry failed", __FUNCTION__);
593 return false;
594 }
595 exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL,
596 {static_cast<ExifLong>(t.tm_hour), 1});
597 exif_set_rational(entry->data + sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL,
598 {static_cast<ExifLong>(t.tm_min), 1});
599 exif_set_rational(entry->data + 2 * sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL,
600 {static_cast<ExifLong>(t.tm_sec), 1});
601
602 return true;
603 }
604
setImageHeight(uint32_t length)605 bool ExifUtilsImpl::setImageHeight(uint32_t length) {
606 SET_SHORT(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, length);
607 SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, length);
608 return true;
609 }
610
setImageWidth(uint32_t width)611 bool ExifUtilsImpl::setImageWidth(uint32_t width) {
612 SET_SHORT(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, width);
613 SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, width);
614 return true;
615 }
616
setIsoSpeedRating(uint16_t iso_speed_ratings)617 bool ExifUtilsImpl::setIsoSpeedRating(uint16_t iso_speed_ratings) {
618 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_ISO_SPEED_RATINGS, iso_speed_ratings);
619 return true;
620 }
621
setMaxAperture(float aperture)622 bool ExifUtilsImpl::setMaxAperture(float aperture) {
623 float maxAperture = convertToApex(aperture);
624 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_MAX_APERTURE_VALUE,
625 static_cast<uint32_t>(std::round(maxAperture * kRationalPrecision)),
626 kRationalPrecision);
627 return true;
628 }
629
setExposureBias(int32_t ev,uint32_t ev_step_numerator,uint32_t ev_step_denominator)630 bool ExifUtilsImpl::setExposureBias(int32_t ev,
631 uint32_t ev_step_numerator, uint32_t ev_step_denominator) {
632 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_BIAS_VALUE,
633 ev * ev_step_numerator, ev_step_denominator);
634 return true;
635 }
636
setOrientation(uint16_t degrees)637 bool ExifUtilsImpl::setOrientation(uint16_t degrees) {
638 ExifOrientation value = ExifOrientation::ORIENTATION_0_DEGREES;
639 switch (degrees) {
640 case 90:
641 value = ExifOrientation::ORIENTATION_90_DEGREES;
642 break;
643 case 180:
644 value = ExifOrientation::ORIENTATION_180_DEGREES;
645 break;
646 case 270:
647 value = ExifOrientation::ORIENTATION_270_DEGREES;
648 break;
649 default:
650 break;
651 }
652 return setOrientationValue(value);
653 }
654
setOrientationValue(ExifOrientation orientationValue)655 bool ExifUtilsImpl::setOrientationValue(ExifOrientation orientationValue) {
656 SET_SHORT(EXIF_IFD_0, EXIF_TAG_ORIENTATION, orientationValue);
657 return true;
658 }
659
setShutterSpeed(float exposure_time)660 bool ExifUtilsImpl::setShutterSpeed(float exposure_time) {
661 float shutterSpeed = -log2f(exposure_time);
662 SET_SRATIONAL(EXIF_IFD_EXIF, EXIF_TAG_SHUTTER_SPEED_VALUE,
663 static_cast<uint32_t>(shutterSpeed * kRationalPrecision), kRationalPrecision);
664 return true;
665 }
666
setSubjectDistance(float diopters)667 bool ExifUtilsImpl::setSubjectDistance(float diopters) {
668 const static float kInfinityDiopters = 1.0e-6;
669 uint32_t numerator, denominator;
670 uint16_t distanceRange;
671 if (diopters > kInfinityDiopters) {
672 float focusDistance = 1.0f / diopters;
673 numerator = static_cast<uint32_t>(std::round(focusDistance * kRationalPrecision));
674 denominator = kRationalPrecision;
675
676 if (focusDistance < 1.0f) {
677 distanceRange = 1; // Macro
678 } else if (focusDistance < 3.0f) {
679 distanceRange = 2; // Close
680 } else {
681 distanceRange = 3; // Distant
682 }
683 } else {
684 numerator = 0xFFFFFFFF;
685 denominator = 1;
686 distanceRange = 3; // Distant
687 }
688 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_DISTANCE, numerator, denominator);
689 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_DISTANCE_RANGE, distanceRange);
690 return true;
691 }
692
setSubsecTime(const std::string & subsec_time)693 bool ExifUtilsImpl::setSubsecTime(const std::string& subsec_time) {
694 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME, EXIF_FORMAT_ASCII, subsec_time);
695 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_ORIGINAL, EXIF_FORMAT_ASCII, subsec_time);
696 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_DIGITIZED, EXIF_FORMAT_ASCII, subsec_time);
697 return true;
698 }
699
setWhiteBalance(uint8_t white_balance)700 bool ExifUtilsImpl::setWhiteBalance(uint8_t white_balance) {
701 uint16_t whiteBalance = (white_balance == ANDROID_CONTROL_AWB_MODE_AUTO) ? 0 : 1;
702 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_WHITE_BALANCE, whiteBalance);
703 return true;
704 }
705
generateApp1()706 bool ExifUtilsImpl::generateApp1() {
707 destroyApp1();
708 // Save the result into |app1_buffer_|.
709 exif_data_save_data(exif_data_, &app1_buffer_, &app1_length_);
710 if (!app1_length_) {
711 ALOGE("%s: Allocate memory for app1_buffer_ failed", __FUNCTION__);
712 return false;
713 }
714 /*
715 * The JPEG segment size is 16 bits in spec. The size of APP1 segment should
716 * be smaller than 65533 because there are two bytes for segment size field.
717 */
718 if (app1_length_ > 65533) {
719 destroyApp1();
720 ALOGE("%s: The size of APP1 segment is too large", __FUNCTION__);
721 return false;
722 }
723 return true;
724 }
725
getApp1Buffer()726 const uint8_t* ExifUtilsImpl::getApp1Buffer() {
727 return app1_buffer_;
728 }
729
getApp1Length()730 unsigned int ExifUtilsImpl::getApp1Length() {
731 return app1_length_;
732 }
733
setExifVersion(const std::string & exif_version)734 bool ExifUtilsImpl::setExifVersion(const std::string& exif_version) {
735 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_EXIF_VERSION, EXIF_FORMAT_UNDEFINED, exif_version);
736 return true;
737 }
738
reset()739 void ExifUtilsImpl::reset() {
740 destroyApp1();
741 if (exif_data_) {
742 /*
743 * Since we decided to ignore the original APP1, we are sure that there is
744 * no thumbnail allocated by libexif. |exif_data_->data| is actually
745 * allocated by JpegCompressor. sets |exif_data_->data| to nullptr to
746 * prevent exif_data_unref() destroy it incorrectly.
747 */
748 exif_data_->data = nullptr;
749 exif_data_->size = 0;
750 exif_data_unref(exif_data_);
751 exif_data_ = nullptr;
752 }
753 }
754
addVariableLengthEntry(ExifIfd ifd,ExifTag tag,ExifFormat format,uint64_t components,unsigned int size)755 std::unique_ptr<ExifEntry> ExifUtilsImpl::addVariableLengthEntry(ExifIfd ifd,
756 ExifTag tag, ExifFormat format, uint64_t components, unsigned int size) {
757 // Remove old entry if exists.
758 exif_content_remove_entry(exif_data_->ifd[ifd],
759 exif_content_get_entry(exif_data_->ifd[ifd], tag));
760 ExifMem* mem = exif_mem_new_default();
761 if (!mem) {
762 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
763 return nullptr;
764 }
765 std::unique_ptr<ExifEntry> entry(exif_entry_new_mem(mem));
766 if (!entry) {
767 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
768 exif_mem_unref(mem);
769 return nullptr;
770 }
771 void* tmpBuffer = exif_mem_alloc(mem, size);
772 if (!tmpBuffer) {
773 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
774 exif_mem_unref(mem);
775 return nullptr;
776 }
777
778 entry->data = static_cast<unsigned char*>(tmpBuffer);
779 entry->tag = tag;
780 entry->format = format;
781 entry->components = components;
782 entry->size = size;
783
784 exif_content_add_entry(exif_data_->ifd[ifd], entry.get());
785 exif_mem_unref(mem);
786
787 return entry;
788 }
789
addEntry(ExifIfd ifd,ExifTag tag)790 std::unique_ptr<ExifEntry> ExifUtilsImpl::addEntry(ExifIfd ifd, ExifTag tag) {
791 std::unique_ptr<ExifEntry> entry(exif_content_get_entry(exif_data_->ifd[ifd], tag));
792 if (entry) {
793 // exif_content_get_entry() won't ref the entry, so we ref here.
794 exif_entry_ref(entry.get());
795 return entry;
796 }
797 entry.reset(exif_entry_new());
798 if (!entry) {
799 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
800 return nullptr;
801 }
802 entry->tag = tag;
803 exif_content_add_entry(exif_data_->ifd[ifd], entry.get());
804 exif_entry_initialize(entry.get(), tag);
805 return entry;
806 }
807
setShort(ExifIfd ifd,ExifTag tag,uint16_t value,const std::string & msg)808 bool ExifUtilsImpl::setShort(ExifIfd ifd, ExifTag tag, uint16_t value, const std::string& msg) {
809 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
810 if (!entry) {
811 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
812 return false;
813 }
814 exif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, value);
815 return true;
816 }
817
setLong(ExifIfd ifd,ExifTag tag,uint32_t value,const std::string & msg)818 bool ExifUtilsImpl::setLong(ExifIfd ifd, ExifTag tag, uint32_t value, const std::string& msg) {
819 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
820 if (!entry) {
821 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
822 return false;
823 }
824 exif_set_long(entry->data, EXIF_BYTE_ORDER_INTEL, value);
825 return true;
826 }
827
setRational(ExifIfd ifd,ExifTag tag,uint32_t numerator,uint32_t denominator,const std::string & msg)828 bool ExifUtilsImpl::setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator,
829 uint32_t denominator, const std::string& msg) {
830 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
831 if (!entry) {
832 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
833 return false;
834 }
835 exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, {numerator, denominator});
836 return true;
837 }
838
setSRational(ExifIfd ifd,ExifTag tag,int32_t numerator,int32_t denominator,const std::string & msg)839 bool ExifUtilsImpl::setSRational(ExifIfd ifd, ExifTag tag, int32_t numerator,
840 int32_t denominator, const std::string& msg) {
841 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
842 if (!entry) {
843 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
844 return false;
845 }
846 exif_set_srational(entry->data, EXIF_BYTE_ORDER_INTEL, {numerator, denominator});
847 return true;
848 }
849
setString(ExifIfd ifd,ExifTag tag,ExifFormat format,const std::string & buffer,const std::string & msg)850 bool ExifUtilsImpl::setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
851 const std::string& buffer, const std::string& msg) {
852 size_t entry_size = buffer.length();
853 // Since the exif format is undefined, NULL termination is not necessary.
854 if (format == EXIF_FORMAT_ASCII) {
855 entry_size++;
856 }
857 std::unique_ptr<ExifEntry> entry =
858 addVariableLengthEntry(ifd, tag, format, entry_size, entry_size);
859 if (!entry) {
860 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
861 return false;
862 }
863 memcpy(entry->data, buffer.c_str(), entry_size);
864 return true;
865 }
866
destroyApp1()867 void ExifUtilsImpl::destroyApp1() {
868 /*
869 * Since there is no API to access ExifMem in ExifData->priv, we use free
870 * here, which is the default free function in libexif. See
871 * exif_data_save_data() for detail.
872 */
873 free(app1_buffer_);
874 app1_buffer_ = nullptr;
875 app1_length_ = 0;
876 }
877
setFromMetadata(const CameraMetadata & metadata,const CameraMetadata & staticInfo,const size_t imageWidth,const size_t imageHeight)878 bool ExifUtilsImpl::setFromMetadata(const CameraMetadata& metadata,
879 const CameraMetadata& staticInfo,
880 const size_t imageWidth, const size_t imageHeight) {
881 if (!setImageWidth(imageWidth) ||
882 !setImageHeight(imageHeight)) {
883 ALOGE("%s: setting image resolution failed.", __FUNCTION__);
884 return false;
885 }
886
887 struct timespec tp;
888 struct tm time_info;
889 bool time_available = clock_gettime(CLOCK_REALTIME, &tp) != -1;
890 localtime_r(&tp.tv_sec, &time_info);
891 if (!setDateTime(time_info)) {
892 ALOGE("%s: setting data time failed.", __FUNCTION__);
893 return false;
894 }
895
896 float focal_length;
897 camera_metadata_ro_entry entry = metadata.find(ANDROID_LENS_FOCAL_LENGTH);
898 if (entry.count) {
899 focal_length = entry.data.f[0];
900
901 if (!setFocalLength(focal_length)) {
902 ALOGE("%s: setting focal length failed.", __FUNCTION__);
903 return false;
904 }
905
906 camera_metadata_ro_entry sensorSizeEntry =
907 staticInfo.find(ANDROID_SENSOR_INFO_PHYSICAL_SIZE);
908 if (sensorSizeEntry.count == 2) {
909 if (!setFocalLengthIn35mmFilm(
910 focal_length, sensorSizeEntry.data.f[0], sensorSizeEntry.data.f[1])) {
911 ALOGE("%s: setting focal length in 35mm failed.", __FUNCTION__);
912 return false;
913 }
914 }
915 } else {
916 ALOGV("%s: Cannot find focal length in metadata.", __FUNCTION__);
917 }
918
919 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
920 camera_metadata_ro_entry sensorPixelModeEntry = metadata.find(ANDROID_SENSOR_PIXEL_MODE);
921 if (sensorPixelModeEntry.count != 0) {
922 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
923 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
924 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
925 ALOGE("%s: Request sensor pixel mode is not one of the valid values %d",
926 __FUNCTION__, sensorPixelMode);
927 return false;
928 }
929 }
930 int32_t activeArrayTag = sensorPixelMode == ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION ?
931 ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE_MAXIMUM_RESOLUTION :
932 ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE;
933 if (metadata.exists(ANDROID_SCALER_CROP_REGION) &&
934 staticInfo.exists(activeArrayTag)) {
935 entry = metadata.find(ANDROID_SCALER_CROP_REGION);
936 camera_metadata_ro_entry activeArrayEntry =
937 staticInfo.find(activeArrayTag);
938
939 if (!setDigitalZoomRatio(entry.data.i32[2], entry.data.i32[3],
940 activeArrayEntry.data.i32[2], activeArrayEntry.data.i32[3])) {
941 ALOGE("%s: setting digital zoom ratio failed.", __FUNCTION__);
942 return false;
943 }
944 }
945
946 if (metadata.exists(ANDROID_JPEG_GPS_COORDINATES)) {
947 entry = metadata.find(ANDROID_JPEG_GPS_COORDINATES);
948 if (entry.count < 3) {
949 ALOGE("%s: Gps coordinates in metadata is not complete.", __FUNCTION__);
950 return false;
951 }
952 if (!setGpsLatitude(entry.data.d[0])) {
953 ALOGE("%s: setting gps latitude failed.", __FUNCTION__);
954 return false;
955 }
956 if (!setGpsLongitude(entry.data.d[1])) {
957 ALOGE("%s: setting gps longitude failed.", __FUNCTION__);
958 return false;
959 }
960 if (!setGpsAltitude(entry.data.d[2])) {
961 ALOGE("%s: setting gps altitude failed.", __FUNCTION__);
962 return false;
963 }
964 }
965
966 if (metadata.exists(ANDROID_JPEG_GPS_PROCESSING_METHOD)) {
967 entry = metadata.find(ANDROID_JPEG_GPS_PROCESSING_METHOD);
968 std::string method_str(reinterpret_cast<const char*>(entry.data.u8));
969 if (!setGpsProcessingMethod(method_str)) {
970 ALOGE("%s: setting gps processing method failed.", __FUNCTION__);
971 return false;
972 }
973 }
974
975 if (time_available && metadata.exists(ANDROID_JPEG_GPS_TIMESTAMP)) {
976 entry = metadata.find(ANDROID_JPEG_GPS_TIMESTAMP);
977 time_t timestamp = static_cast<time_t>(entry.data.i64[0]);
978 if (gmtime_r(×tamp, &time_info)) {
979 if (!setGpsTimestamp(time_info)) {
980 ALOGE("%s: setting gps timestamp failed.", __FUNCTION__);
981 return false;
982 }
983 } else {
984 ALOGE("%s: Time tranformation failed.", __FUNCTION__);
985 return false;
986 }
987 }
988
989 if (staticInfo.exists(ANDROID_CONTROL_AE_COMPENSATION_STEP) &&
990 metadata.exists(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION)) {
991 entry = metadata.find(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION);
992 camera_metadata_ro_entry stepEntry =
993 staticInfo.find(ANDROID_CONTROL_AE_COMPENSATION_STEP);
994 if (!setExposureBias(entry.data.i32[0], stepEntry.data.r[0].numerator,
995 stepEntry.data.r[0].denominator)) {
996 ALOGE("%s: setting exposure bias failed.", __FUNCTION__);
997 return false;
998 }
999 }
1000
1001 if (metadata.exists(ANDROID_JPEG_ORIENTATION)) {
1002 entry = metadata.find(ANDROID_JPEG_ORIENTATION);
1003 if (!setOrientation(entry.data.i32[0])) {
1004 ALOGE("%s: setting orientation failed.", __FUNCTION__);
1005 return false;
1006 }
1007 }
1008
1009 if (metadata.exists(ANDROID_SENSOR_EXPOSURE_TIME)) {
1010 entry = metadata.find(ANDROID_SENSOR_EXPOSURE_TIME);
1011 float exposure_time = 1.0f * entry.data.i64[0] / 1e9;
1012 if (!setExposureTime(exposure_time)) {
1013 ALOGE("%s: setting exposure time failed.", __FUNCTION__);
1014 return false;
1015 }
1016
1017 if (!setShutterSpeed(exposure_time)) {
1018 ALOGE("%s: setting shutter speed failed.", __FUNCTION__);
1019 return false;
1020 }
1021 }
1022
1023 if (metadata.exists(ANDROID_LENS_FOCUS_DISTANCE)) {
1024 entry = metadata.find(ANDROID_LENS_FOCUS_DISTANCE);
1025 if (!setSubjectDistance(entry.data.f[0])) {
1026 ALOGE("%s: setting subject distance failed.", __FUNCTION__);
1027 return false;
1028 }
1029 }
1030
1031 if (metadata.exists(ANDROID_SENSOR_SENSITIVITY)) {
1032 entry = metadata.find(ANDROID_SENSOR_SENSITIVITY);
1033 int32_t iso = entry.data.i32[0];
1034 camera_metadata_ro_entry postRawSensEntry =
1035 metadata.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST);
1036 if (postRawSensEntry.count > 0) {
1037 iso = iso * postRawSensEntry.data.i32[0] / 100;
1038 }
1039
1040 if (!setIsoSpeedRating(static_cast<uint16_t>(iso))) {
1041 ALOGE("%s: setting iso rating failed.", __FUNCTION__);
1042 return false;
1043 }
1044 }
1045
1046 if (metadata.exists(ANDROID_LENS_APERTURE)) {
1047 entry = metadata.find(ANDROID_LENS_APERTURE);
1048 if (!setFNumber(entry.data.f[0])) {
1049 ALOGE("%s: setting F number failed.", __FUNCTION__);
1050 return false;
1051 }
1052 if (!setAperture(entry.data.f[0])) {
1053 ALOGE("%s: setting aperture failed.", __FUNCTION__);
1054 return false;
1055 }
1056 }
1057
1058 static const uint16_t kSRGBColorSpace = 1;
1059 if (!setColorSpace(kSRGBColorSpace)) {
1060 ALOGE("%s: setting color space failed.", __FUNCTION__);
1061 return false;
1062 }
1063
1064 if (staticInfo.exists(ANDROID_LENS_INFO_AVAILABLE_APERTURES)) {
1065 entry = staticInfo.find(ANDROID_LENS_INFO_AVAILABLE_APERTURES);
1066 if (!setMaxAperture(entry.data.f[0])) {
1067 ALOGE("%s: setting max aperture failed.", __FUNCTION__);
1068 return false;
1069 }
1070 }
1071
1072 if (staticInfo.exists(ANDROID_FLASH_INFO_AVAILABLE)) {
1073 entry = staticInfo.find(ANDROID_FLASH_INFO_AVAILABLE);
1074 camera_metadata_ro_entry flashStateEntry = metadata.find(ANDROID_FLASH_STATE);
1075 camera_metadata_ro_entry aeModeEntry = metadata.find(ANDROID_CONTROL_AE_MODE);
1076 uint8_t flashState = flashStateEntry.count > 0 ?
1077 flashStateEntry.data.u8[0] : ANDROID_FLASH_STATE_UNAVAILABLE;
1078 uint8_t aeMode = aeModeEntry.count > 0 ?
1079 aeModeEntry.data.u8[0] : ANDROID_CONTROL_AE_MODE_OFF;
1080
1081 if (!setFlash(entry.data.u8[0], flashState, aeMode)) {
1082 ALOGE("%s: setting flash failed.", __FUNCTION__);
1083 return false;
1084 }
1085 }
1086
1087 if (metadata.exists(ANDROID_CONTROL_AWB_MODE)) {
1088 entry = metadata.find(ANDROID_CONTROL_AWB_MODE);
1089 if (!setWhiteBalance(entry.data.u8[0])) {
1090 ALOGE("%s: setting white balance failed.", __FUNCTION__);
1091 return false;
1092 }
1093 }
1094
1095 if (metadata.exists(ANDROID_CONTROL_AE_MODE)) {
1096 entry = metadata.find(ANDROID_CONTROL_AE_MODE);
1097 if (!setExposureMode(entry.data.u8[0])) {
1098 ALOGE("%s: setting exposure mode failed.", __FUNCTION__);
1099 return false;
1100 }
1101 }
1102 if (time_available) {
1103 char str[4];
1104 if (snprintf(str, sizeof(str), "%03ld", tp.tv_nsec / 1000000) < 0) {
1105 ALOGE("%s: Subsec is invalid: %ld", __FUNCTION__, tp.tv_nsec);
1106 return false;
1107 }
1108 if (!setSubsecTime(std::string(str))) {
1109 ALOGE("%s: setting subsec time failed.", __FUNCTION__);
1110 return false;
1111 }
1112 }
1113
1114 return true;
1115 }
1116
1117 } // namespace camera3
1118 } // namespace android
1119