1 /* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 // System dependencies
31 #include <pthread.h>
32 #include <string.h>
33 #include <math.h>
34 
35 // JPEG dependencies
36 #include "mm_jpeg_dbg.h"
37 #include "mm_jpeg.h"
38 
39 
40 #define LOWER(a)               ((a) & 0xFFFF)
41 #define UPPER(a)               (((a)>>16) & 0xFFFF)
42 #define CHANGE_ENDIAN_16(a)  ((0x00FF & ((a)>>8)) | (0xFF00 & ((a)<<8)))
43 #define ROUND(a) \
44         ((a >= 0) ? (uint32_t)(a + 0.5) : (uint32_t)(a - 0.5))
45 
46 
47 /** addExifEntry:
48  *
49  *  Arguments:
50  *   @exif_info : Exif info struct
51  *   @p_session: job session
52  *   @tagid   : exif tag ID
53  *   @type    : data type
54  *   @count   : number of data in uint of its type
55  *   @data    : input data ptr
56  *
57  *  Retrun     : int32_t type of status
58  *               0  -- success
59  *              none-zero failure code
60  *
61  *  Description:
62  *       Function to add an entry to exif data
63  *
64  **/
addExifEntry(QOMX_EXIF_INFO * p_exif_info,exif_tag_id_t tagid,exif_tag_type_t type,uint32_t count,void * data)65 int32_t addExifEntry(QOMX_EXIF_INFO *p_exif_info, exif_tag_id_t tagid,
66   exif_tag_type_t type, uint32_t count, void *data)
67 {
68     int32_t rc = 0;
69     uint32_t numOfEntries = (uint32_t)p_exif_info->numOfEntries;
70     QEXIF_INFO_DATA *p_info_data = p_exif_info->exif_data;
71     if(numOfEntries >= MAX_EXIF_TABLE_ENTRIES) {
72         LOGE("Number of entries exceeded limit");
73         return -1;
74     }
75 
76     p_info_data[numOfEntries].tag_id = tagid;
77     p_info_data[numOfEntries].tag_entry.type = type;
78     p_info_data[numOfEntries].tag_entry.count = count;
79     p_info_data[numOfEntries].tag_entry.copy = 1;
80     switch (type) {
81     case EXIF_BYTE: {
82       if (count > 1) {
83         uint8_t *values = (uint8_t *)malloc(count);
84         if (values == NULL) {
85           LOGE("No memory for byte array");
86           rc = -1;
87         } else {
88           memcpy(values, data, count);
89           p_info_data[numOfEntries].tag_entry.data._bytes = values;
90         }
91       } else {
92         p_info_data[numOfEntries].tag_entry.data._byte = *(uint8_t *)data;
93       }
94     }
95     break;
96     case EXIF_ASCII: {
97       char *str = NULL;
98       str = (char *)malloc(count + 1);
99       if (str == NULL) {
100         LOGE("No memory for ascii string");
101         rc = -1;
102       } else {
103         memset(str, 0, count + 1);
104         memcpy(str, data, count);
105         p_info_data[numOfEntries].tag_entry.data._ascii = str;
106       }
107     }
108     break;
109     case EXIF_SHORT: {
110       if (count > 1) {
111         uint16_t *values = (uint16_t *)malloc(count * sizeof(uint16_t));
112         if (values == NULL) {
113           LOGE("No memory for short array");
114           rc = -1;
115         } else {
116           memcpy(values, data, count * sizeof(uint16_t));
117           p_info_data[numOfEntries].tag_entry.data._shorts = values;
118         }
119       } else {
120         p_info_data[numOfEntries].tag_entry.data._short = *(uint16_t *)data;
121       }
122     }
123     break;
124     case EXIF_LONG: {
125       if (count > 1) {
126         uint32_t *values = (uint32_t *)malloc(count * sizeof(uint32_t));
127         if (values == NULL) {
128           LOGE("No memory for long array");
129           rc = -1;
130         } else {
131           memcpy(values, data, count * sizeof(uint32_t));
132           p_info_data[numOfEntries].tag_entry.data._longs = values;
133         }
134       } else {
135         p_info_data[numOfEntries].tag_entry.data._long = *(uint32_t *)data;
136       }
137     }
138     break;
139     case EXIF_RATIONAL: {
140       if (count > 1) {
141         rat_t *values = (rat_t *)malloc(count * sizeof(rat_t));
142         if (values == NULL) {
143           LOGE("No memory for rational array");
144           rc = -1;
145         } else {
146           memcpy(values, data, count * sizeof(rat_t));
147           p_info_data[numOfEntries].tag_entry.data._rats = values;
148         }
149       } else {
150         p_info_data[numOfEntries].tag_entry.data._rat = *(rat_t *)data;
151       }
152     }
153     break;
154     case EXIF_UNDEFINED: {
155       uint8_t *values = (uint8_t *)malloc(count);
156       if (values == NULL) {
157         LOGE("No memory for undefined array");
158         rc = -1;
159       } else {
160         memcpy(values, data, count);
161         p_info_data[numOfEntries].tag_entry.data._undefined = values;
162       }
163     }
164     break;
165     case EXIF_SLONG: {
166       if (count > 1) {
167         int32_t *values = (int32_t *)malloc(count * sizeof(int32_t));
168         if (values == NULL) {
169           LOGE("No memory for signed long array");
170           rc = -1;
171         } else {
172           memcpy(values, data, count * sizeof(int32_t));
173           p_info_data[numOfEntries].tag_entry.data._slongs = values;
174         }
175       } else {
176         p_info_data[numOfEntries].tag_entry.data._slong = *(int32_t *)data;
177       }
178     }
179     break;
180     case EXIF_SRATIONAL: {
181       if (count > 1) {
182         srat_t *values = (srat_t *)malloc(count * sizeof(srat_t));
183         if (values == NULL) {
184           LOGE("No memory for signed rational array");
185           rc = -1;
186         } else {
187           memcpy(values, data, count * sizeof(srat_t));
188           p_info_data[numOfEntries].tag_entry.data._srats = values;
189         }
190       } else {
191         p_info_data[numOfEntries].tag_entry.data._srat = *(srat_t *)data;
192       }
193     }
194     break;
195     }
196 
197     // Increase number of entries
198     p_exif_info->numOfEntries++;
199     return rc;
200 }
201 
202 /** releaseExifEntry
203  *
204  *  Arguments:
205  *   @p_exif_data : Exif info struct
206  *
207  *  Retrun     : int32_t type of status
208  *               0  -- success
209  *              none-zero failure code
210  *
211  *  Description:
212  *       Function to release an entry from exif data
213  *
214  **/
releaseExifEntry(QEXIF_INFO_DATA * p_exif_data)215 int32_t releaseExifEntry(QEXIF_INFO_DATA *p_exif_data)
216 {
217  switch (p_exif_data->tag_entry.type) {
218   case EXIF_BYTE: {
219     if (p_exif_data->tag_entry.count > 1 &&
220       p_exif_data->tag_entry.data._bytes != NULL) {
221       free(p_exif_data->tag_entry.data._bytes);
222       p_exif_data->tag_entry.data._bytes = NULL;
223     }
224   }
225   break;
226   case EXIF_ASCII: {
227     if (p_exif_data->tag_entry.data._ascii != NULL) {
228       free(p_exif_data->tag_entry.data._ascii);
229       p_exif_data->tag_entry.data._ascii = NULL;
230     }
231   }
232   break;
233   case EXIF_SHORT: {
234     if (p_exif_data->tag_entry.count > 1 &&
235       p_exif_data->tag_entry.data._shorts != NULL) {
236       free(p_exif_data->tag_entry.data._shorts);
237       p_exif_data->tag_entry.data._shorts = NULL;
238     }
239   }
240   break;
241   case EXIF_LONG: {
242     if (p_exif_data->tag_entry.count > 1 &&
243       p_exif_data->tag_entry.data._longs != NULL) {
244       free(p_exif_data->tag_entry.data._longs);
245       p_exif_data->tag_entry.data._longs = NULL;
246     }
247   }
248   break;
249   case EXIF_RATIONAL: {
250     if (p_exif_data->tag_entry.count > 1 &&
251       p_exif_data->tag_entry.data._rats != NULL) {
252       free(p_exif_data->tag_entry.data._rats);
253       p_exif_data->tag_entry.data._rats = NULL;
254     }
255   }
256   break;
257   case EXIF_UNDEFINED: {
258     if (p_exif_data->tag_entry.data._undefined != NULL) {
259       free(p_exif_data->tag_entry.data._undefined);
260       p_exif_data->tag_entry.data._undefined = NULL;
261     }
262   }
263   break;
264   case EXIF_SLONG: {
265     if (p_exif_data->tag_entry.count > 1 &&
266       p_exif_data->tag_entry.data._slongs != NULL) {
267       free(p_exif_data->tag_entry.data._slongs);
268       p_exif_data->tag_entry.data._slongs = NULL;
269     }
270   }
271   break;
272   case EXIF_SRATIONAL: {
273     if (p_exif_data->tag_entry.count > 1 &&
274       p_exif_data->tag_entry.data._srats != NULL) {
275       free(p_exif_data->tag_entry.data._srats);
276       p_exif_data->tag_entry.data._srats = NULL;
277     }
278   }
279   break;
280   } /*end of switch*/
281 
282   return 0;
283 }
284 
285 /** process_sensor_data:
286  *
287  *  Arguments:
288  *   @p_sensor_params : ptr to sensor data
289  *
290  *  Return     : int32_t type of status
291  *               NO_ERROR  -- success
292  *              none-zero failure code
293  *
294  *  Description:
295  *       process sensor data
296  *
297  *  Notes: this needs to be filled for the metadata
298  **/
process_sensor_data(cam_sensor_params_t * p_sensor_params,QOMX_EXIF_INFO * exif_info)299 int process_sensor_data(cam_sensor_params_t *p_sensor_params,
300   QOMX_EXIF_INFO *exif_info)
301 {
302   int rc = 0;
303   rat_t val_rat;
304 
305   if (NULL == p_sensor_params) {
306     LOGE("Sensor params are null");
307     return 0;
308   }
309 
310   LOGD("From metadata aperture = %f ",
311     p_sensor_params->aperture_value );
312 
313   if (p_sensor_params->aperture_value >= 1.0) {
314     double apex_value;
315     apex_value = (double)2.0 * log(p_sensor_params->aperture_value) / log(2.0);
316     val_rat.num = (uint32_t)(apex_value * 100);
317     val_rat.denom = 100;
318     rc = addExifEntry(exif_info, EXIFTAGID_APERTURE, EXIF_RATIONAL, 1, &val_rat);
319     if (rc) {
320       LOGE(": Error adding Exif Entry");
321     }
322 
323     val_rat.num = (uint32_t)(p_sensor_params->aperture_value * 100);
324     val_rat.denom = 100;
325     rc = addExifEntry(exif_info, EXIFTAGID_F_NUMBER, EXIF_RATIONAL, 1, &val_rat);
326     if (rc) {
327       LOGE(": Error adding Exif Entry");
328     }
329   }
330 
331   /*Flash*/
332   short val_short = 0;
333   int flash_mode_exif, flash_fired;
334   if (p_sensor_params->flash_state == CAM_FLASH_STATE_FIRED) {
335     flash_fired = 1;
336   } else {
337     flash_fired = 0;
338   }
339   LOGD("Flash mode %d flash state %d",
340     p_sensor_params->flash_mode, p_sensor_params->flash_state);
341 
342   switch(p_sensor_params->flash_mode) {
343   case  CAM_FLASH_MODE_OFF:
344     flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_OFF;
345     break;
346   case CAM_FLASH_MODE_ON:
347     flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_ON;
348     break;
349   case CAM_FLASH_MODE_AUTO:
350     flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_AUTO;
351     break;
352   default:
353     flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_AUTO;
354     LOGE(": Unsupported flash mode");
355   }
356   val_short = (short)(flash_fired | (flash_mode_exif << 3));
357 
358   rc = addExifEntry(exif_info, EXIFTAGID_FLASH, EXIF_SHORT, 1, &val_short);
359   if (rc) {
360     LOGE(": Error adding flash exif entry");
361   }
362   /* Sensing Method */
363   val_short = (short) p_sensor_params->sensing_method;
364   rc = addExifEntry(exif_info, EXIFTAGID_SENSING_METHOD, EXIF_SHORT,
365     sizeof(val_short)/2, &val_short);
366   if (rc) {
367     LOGE(": Error adding flash Exif Entry");
368   }
369 
370   /* Focal Length in 35 MM Film */
371   val_short = (short)
372     ((p_sensor_params->focal_length * p_sensor_params->crop_factor) + 0.5f);
373   rc = addExifEntry(exif_info, EXIFTAGID_FOCAL_LENGTH_35MM, EXIF_SHORT,
374     1, &val_short);
375   if (rc) {
376     LOGE(": Error adding Exif Entry");
377   }
378 
379   return rc;
380 }
381 
382 
383 /** process_3a_data:
384  *
385  *  Arguments:
386  *   @p_3a_params : ptr to 3a data
387  *   @exif_info : Exif info struct
388  *
389  *  Return     : int32_t type of status
390  *               NO_ERROR  -- success
391  *               none-zero failure code
392  *
393  *  Description:
394  *       process 3a data
395  *
396  *  Notes: this needs to be filled for the metadata
397  **/
process_3a_data(cam_3a_params_t * p_3a_params,QOMX_EXIF_INFO * exif_info)398 int process_3a_data(cam_3a_params_t *p_3a_params, QOMX_EXIF_INFO *exif_info)
399 {
400   int rc = 0;
401   srat_t val_srat;
402   rat_t val_rat;
403   double shutter_speed_value;
404 
405   if (NULL == p_3a_params) {
406     LOGE("3A params are null");
407     return 0;
408   }
409 
410   LOGD("exp_time %f, iso_value %d, wb_mode %d",
411     p_3a_params->exp_time, p_3a_params->iso_value, p_3a_params->wb_mode);
412 
413   /* Exposure time */
414   if (p_3a_params->exp_time <= 0.0f) {
415     val_rat.num = 0;
416     val_rat.denom = 0;
417   } else if (p_3a_params->exp_time < 1.0f) {
418     val_rat.num = 1;
419     val_rat.denom = ROUND(1.0/p_3a_params->exp_time);
420   } else {
421     val_rat.num = ROUND(p_3a_params->exp_time);
422     val_rat.denom = 1;
423   }
424   LOGD("numer %d denom %d %zd", val_rat.num, val_rat.denom,
425     sizeof(val_rat) / (8));
426 
427   rc = addExifEntry(exif_info, EXIFTAGID_EXPOSURE_TIME, EXIF_RATIONAL,
428     (sizeof(val_rat)/(8)), &val_rat);
429   if (rc) {
430     LOGE(": Error adding Exif Entry Exposure time");
431   }
432 
433   /* Shutter Speed*/
434   if (p_3a_params->exp_time > 0) {
435     shutter_speed_value = log10(1/p_3a_params->exp_time)/log10(2);
436     val_srat.num = (int32_t)(shutter_speed_value * 1000);
437     val_srat.denom = 1000;
438   } else {
439     val_srat.num = 0;
440     val_srat.denom = 0;
441   }
442   rc = addExifEntry(exif_info, EXIFTAGID_SHUTTER_SPEED, EXIF_SRATIONAL,
443     (sizeof(val_srat)/(8)), &val_srat);
444   if (rc) {
445     LOGE(": Error adding Exif Entry");
446   }
447 
448   /*ISO*/
449   short val_short;
450   val_short = (short)p_3a_params->iso_value;
451   rc = addExifEntry(exif_info, EXIFTAGID_ISO_SPEED_RATING, EXIF_SHORT,
452     sizeof(val_short)/2, &val_short);
453   if (rc) {
454      LOGE(": Error adding Exif Entry");
455   }
456 
457   /*WB mode*/
458   if (p_3a_params->wb_mode == CAM_WB_MODE_AUTO)
459     val_short = 0;
460   else
461     val_short = 1;
462   rc = addExifEntry(exif_info, EXIFTAGID_WHITE_BALANCE, EXIF_SHORT,
463     sizeof(val_short)/2, &val_short);
464   if (rc) {
465     LOGE(": Error adding Exif Entry");
466   }
467 
468   /* Metering Mode   */
469   val_short = (short) p_3a_params->metering_mode;
470   rc = addExifEntry(exif_info,EXIFTAGID_METERING_MODE, EXIF_SHORT,
471      sizeof(val_short)/2, &val_short);
472   if (rc) {
473      LOGE(": Error adding Exif Entry");
474    }
475 
476   /*Exposure Program*/
477    val_short = (short) p_3a_params->exposure_program;
478    rc = addExifEntry(exif_info,EXIFTAGID_EXPOSURE_PROGRAM, EXIF_SHORT,
479       sizeof(val_short)/2, &val_short);
480    if (rc) {
481       LOGE(": Error adding Exif Entry");
482     }
483 
484    /*Exposure Mode */
485     val_short = (short) p_3a_params->exposure_mode;
486     rc = addExifEntry(exif_info,EXIFTAGID_EXPOSURE_MODE, EXIF_SHORT,
487        sizeof(val_short)/2, &val_short);
488     if (rc) {
489        LOGE(": Error adding Exif Entry");
490      }
491 
492     /*Scenetype*/
493      uint8_t val_undef;
494      val_undef = (uint8_t) p_3a_params->scenetype;
495      rc = addExifEntry(exif_info,EXIFTAGID_SCENE_TYPE, EXIF_UNDEFINED,
496         sizeof(val_undef), &val_undef);
497      if (rc) {
498         LOGE(": Error adding Exif Entry");
499       }
500 
501      LOGD("brightness %f",
502        p_3a_params->brightness);
503 
504     /* Brightness Value*/
505      val_srat.num = (int32_t) (p_3a_params->brightness * 100.0f);
506      val_srat.denom = 100;
507      rc = addExifEntry(exif_info,EXIFTAGID_BRIGHTNESS, EXIF_SRATIONAL,
508                  (sizeof(val_srat)/(8)), &val_srat);
509      if (rc) {
510         LOGE(": Error adding Exif Entry");
511      }
512 
513   return rc;
514 }
515 
516 /** process_meta_data
517  *
518  *  Arguments:
519  *   @p_meta : ptr to metadata
520  *   @exif_info: Exif info struct
521  *   @mm_jpeg_exif_params: exif params
522  *
523  *  Return     : int32_t type of status
524  *               NO_ERROR  -- success
525  *              none-zero failure code
526  *
527  *  Description:
528  *       Extract exif data from the metadata
529  **/
process_meta_data(metadata_buffer_t * p_meta,QOMX_EXIF_INFO * exif_info,mm_jpeg_exif_params_t * p_cam_exif_params,cam_hal_version_t hal_version)530 int process_meta_data(metadata_buffer_t *p_meta, QOMX_EXIF_INFO *exif_info,
531   mm_jpeg_exif_params_t *p_cam_exif_params, cam_hal_version_t hal_version)
532 {
533   int rc = 0;
534   cam_sensor_params_t p_sensor_params;
535   cam_3a_params_t p_3a_params;
536   bool is_3a_meta_valid = false, is_sensor_meta_valid = false;
537 
538   memset(&p_3a_params,  0,  sizeof(cam_3a_params_t));
539   memset(&p_sensor_params, 0, sizeof(cam_sensor_params_t));
540 
541   if (p_meta) {
542     /* for HAL V1*/
543     if (hal_version == CAM_HAL_V1) {
544 
545       IF_META_AVAILABLE(cam_3a_params_t, l_3a_params, CAM_INTF_META_AEC_INFO,
546           p_meta) {
547         p_3a_params = *l_3a_params;
548         is_3a_meta_valid = true;
549       }
550 
551       IF_META_AVAILABLE(int32_t, wb_mode, CAM_INTF_PARM_WHITE_BALANCE, p_meta) {
552         p_3a_params.wb_mode = *wb_mode;
553       }
554 
555       IF_META_AVAILABLE(cam_sensor_params_t, l_sensor_params,
556           CAM_INTF_META_SENSOR_INFO, p_meta) {
557         p_sensor_params = *l_sensor_params;
558         is_sensor_meta_valid = true;
559       }
560     } else {
561       /* HAL V3 */
562       IF_META_AVAILABLE(cam_3a_params_t, l_3a_params, CAM_INTF_META_AEC_INFO,
563           p_meta) {
564         p_3a_params = *l_3a_params;
565         is_3a_meta_valid = true;
566       }
567 
568       p_3a_params.iso_value = 100;
569       IF_META_AVAILABLE(int32_t, iso, CAM_INTF_META_SENSOR_SENSITIVITY, p_meta) {
570         p_3a_params.iso_value= p_3a_params.iso_value * (*iso) / 100;
571       } else {
572         LOGE("Cannot extract SENSOR_SENSITIVITY value");
573       }
574 
575       int32_t ispSensitivity = 100;
576       IF_META_AVAILABLE(int32_t, isp_iso, CAM_INTF_META_ISP_SENSITIVITY, p_meta) {
577         ispSensitivity = *isp_iso;
578       } else {
579         LOGE("Cannot extract ISP_SENSITIVITY value");
580       }
581 
582       IF_META_AVAILABLE(float, post_stats_iso, CAM_INTF_META_ISP_POST_STATS_SENSITIVITY, p_meta) {
583         ispSensitivity *= *post_stats_iso;
584       } else {
585         /* CAM_INTF_META_ISP_POST_STATS_SENSITIVITY is optional */
586         LOGD("Cannot extract ISP_POST_STATS_SENSITIVITY value");
587       }
588       p_3a_params.iso_value= p_3a_params.iso_value * ispSensitivity / 100;
589 
590       IF_META_AVAILABLE(int64_t, sensor_exposure_time,
591           CAM_INTF_META_SENSOR_EXPOSURE_TIME, p_meta) {
592         p_3a_params.exp_time =
593           (float)((double)(*sensor_exposure_time) / 1000000000.0);
594       } else {
595         LOGE("Cannot extract Exp time value");
596       }
597 
598       IF_META_AVAILABLE(int32_t, wb_mode, CAM_INTF_PARM_WHITE_BALANCE, p_meta) {
599         p_3a_params.wb_mode = *wb_mode;
600       } else {
601         LOGE("Cannot extract white balance mode");
602       }
603 
604       /* Process sensor data */
605       IF_META_AVAILABLE(float, aperture, CAM_INTF_META_LENS_APERTURE, p_meta) {
606         p_sensor_params.aperture_value = *aperture;
607       } else {
608         LOGE("Cannot extract Aperture value");
609       }
610 
611       IF_META_AVAILABLE(uint32_t, flash_mode, CAM_INTF_META_FLASH_MODE, p_meta) {
612         p_sensor_params.flash_mode = *flash_mode;
613       } else {
614         LOGE("Cannot extract flash mode value");
615       }
616 
617       IF_META_AVAILABLE(int32_t, flash_state, CAM_INTF_META_FLASH_STATE, p_meta) {
618         p_sensor_params.flash_state = (cam_flash_state_t) *flash_state;
619       } else {
620         LOGE("Cannot extract flash state value");
621       }
622     }
623   }
624 
625   /* take the cached values if meta is invalid */
626   if ((!is_3a_meta_valid) && (hal_version == CAM_HAL_V1)) {
627     p_3a_params = p_cam_exif_params->cam_3a_params;
628     LOGW("Warning using cached values for 3a");
629   }
630 
631   if ((!is_sensor_meta_valid) && (hal_version == CAM_HAL_V1)) {
632     p_sensor_params = p_cam_exif_params->sensor_params;
633     LOGW("Warning using cached values for sensor");
634   }
635 
636   if ((hal_version != CAM_HAL_V1) || (p_sensor_params.sens_type != CAM_SENSOR_YUV)) {
637     rc = process_3a_data(&p_3a_params, exif_info);
638     if (rc) {
639       LOGE("Failed to add 3a exif params");
640     }
641   }
642 
643   rc = process_sensor_data(&p_sensor_params, exif_info);
644   if (rc) {
645     LOGE("Failed to extract sensor params");
646   }
647 
648   if (p_meta) {
649     short val_short = 0;
650     cam_asd_decision_t *scene_info = NULL;
651 
652     IF_META_AVAILABLE(cam_asd_decision_t, scene_cap_type,
653         CAM_INTF_META_ASD_SCENE_INFO, p_meta) {
654       scene_info = (cam_asd_decision_t*)scene_cap_type;
655       val_short = (short) scene_info->detected_scene;
656     }
657 
658     rc = addExifEntry(exif_info, EXIFTAGID_SCENE_CAPTURE_TYPE, EXIF_SHORT,
659       sizeof(val_short)/2, &val_short);
660     if (rc) {
661       LOGE(": Error adding ASD Exif Entry");
662     }
663 
664     IF_META_AVAILABLE(cam_makernote_t, makernote, CAM_INTF_META_MAKERNOTE, p_meta) {
665       rc = addExifEntry(exif_info, EXIFTAGID_EXIF_MAKER_NOTE, EXIF_UNDEFINED, makernote->length,
666           makernote->data);
667       if (rc) {
668         LOGE(": Error adding makernote");
669       }
670     }
671   } else {
672     LOGE(": Error adding ASD Exif Entry, no meta");
673   }
674   return rc;
675 }
676