Home | History | Annotate | Download | only in src
      1 /* Copyright (c) 2012-2013, 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 #include "mm_jpeg_dbg.h"
     31 #include "mm_jpeg.h"
     32 
     33 #include <errno.h>
     34 #include <math.h>
     35 
     36 
     37 #define LOWER(a)               ((a) & 0xFFFF)
     38 #define UPPER(a)               (((a)>>16) & 0xFFFF)
     39 #define CHANGE_ENDIAN_16(a)  ((0x00FF & ((a)>>8)) | (0xFF00 & ((a)<<8)))
     40 #define ROUND(a)((a >= 0) ? (long)(a + 0.5) : (long)(a - 0.5))
     41 
     42 
     43 /** addExifEntry:
     44  *
     45  *  Arguments:
     46  *   @exif_info : Exif info struct
     47  *   @p_session: job session
     48  *   @tagid   : exif tag ID
     49  *   @type    : data type
     50  *   @count   : number of data in uint of its type
     51  *   @data    : input data ptr
     52  *
     53  *  Retrun     : int32_t type of status
     54  *               0  -- success
     55  *              none-zero failure code
     56  *
     57  *  Description:
     58  *       Function to add an entry to exif data
     59  *
     60  **/
     61 int32_t addExifEntry(QOMX_EXIF_INFO *p_exif_info, exif_tag_id_t tagid,
     62   exif_tag_type_t type, uint32_t count, void *data)
     63 {
     64     int32_t rc = 0;
     65     int32_t numOfEntries = p_exif_info->numOfEntries;
     66     QEXIF_INFO_DATA *p_info_data = p_exif_info->exif_data;
     67     if(numOfEntries >= MAX_EXIF_TABLE_ENTRIES) {
     68         ALOGE("%s: Number of entries exceeded limit", __func__);
     69         return -1;
     70     }
     71 
     72     p_info_data[numOfEntries].tag_id = tagid;
     73     p_info_data[numOfEntries].tag_entry.type = type;
     74     p_info_data[numOfEntries].tag_entry.count = count;
     75     p_info_data[numOfEntries].tag_entry.copy = 1;
     76     switch (type) {
     77     case EXIF_BYTE: {
     78       if (count > 1) {
     79         uint8_t *values = (uint8_t *)malloc(count);
     80         if (values == NULL) {
     81           ALOGE("%s: No memory for byte array", __func__);
     82           rc = -1;
     83         } else {
     84           memcpy(values, data, count);
     85           p_info_data[numOfEntries].tag_entry.data._bytes = values;
     86         }
     87       } else {
     88         p_info_data[numOfEntries].tag_entry.data._byte = *(uint8_t *)data;
     89       }
     90     }
     91     break;
     92     case EXIF_ASCII: {
     93       char *str = NULL;
     94       str = (char *)malloc(count + 1);
     95       if (str == NULL) {
     96         ALOGE("%s: No memory for ascii string", __func__);
     97         rc = -1;
     98       } else {
     99         memset(str, 0, count + 1);
    100         memcpy(str, data, count);
    101         p_info_data[numOfEntries].tag_entry.data._ascii = str;
    102       }
    103     }
    104     break;
    105     case EXIF_SHORT: {
    106       if (count > 1) {
    107         uint16_t *values = (uint16_t *)malloc(count * sizeof(uint16_t));
    108         if (values == NULL) {
    109           ALOGE("%s: No memory for short array", __func__);
    110           rc = -1;
    111         } else {
    112           memcpy(values, data, count * sizeof(uint16_t));
    113           p_info_data[numOfEntries].tag_entry.data._shorts = values;
    114         }
    115       } else {
    116         p_info_data[numOfEntries].tag_entry.data._short = *(uint16_t *)data;
    117       }
    118     }
    119     break;
    120     case EXIF_LONG: {
    121       if (count > 1) {
    122         uint32_t *values = (uint32_t *)malloc(count * sizeof(uint32_t));
    123         if (values == NULL) {
    124           ALOGE("%s: No memory for long array", __func__);
    125           rc = -1;
    126         } else {
    127           memcpy(values, data, count * sizeof(uint32_t));
    128           p_info_data[numOfEntries].tag_entry.data._longs = values;
    129         }
    130       } else {
    131         p_info_data[numOfEntries].tag_entry.data._long = *(uint32_t *)data;
    132       }
    133     }
    134     break;
    135     case EXIF_RATIONAL: {
    136       if (count > 1) {
    137         rat_t *values = (rat_t *)malloc(count * sizeof(rat_t));
    138         if (values == NULL) {
    139           ALOGE("%s: No memory for rational array", __func__);
    140           rc = -1;
    141         } else {
    142           memcpy(values, data, count * sizeof(rat_t));
    143           p_info_data[numOfEntries].tag_entry.data._rats = values;
    144         }
    145       } else {
    146         p_info_data[numOfEntries].tag_entry.data._rat = *(rat_t *)data;
    147       }
    148     }
    149     break;
    150     case EXIF_UNDEFINED: {
    151       uint8_t *values = (uint8_t *)malloc(count);
    152       if (values == NULL) {
    153         ALOGE("%s: No memory for undefined array", __func__);
    154         rc = -1;
    155       } else {
    156         memcpy(values, data, count);
    157         p_info_data[numOfEntries].tag_entry.data._undefined = values;
    158       }
    159     }
    160     break;
    161     case EXIF_SLONG: {
    162       if (count > 1) {
    163         int32_t *values = (int32_t *)malloc(count * sizeof(int32_t));
    164         if (values == NULL) {
    165           ALOGE("%s: No memory for signed long array", __func__);
    166           rc = -1;
    167         } else {
    168           memcpy(values, data, count * sizeof(int32_t));
    169           p_info_data[numOfEntries].tag_entry.data._slongs = values;
    170         }
    171       } else {
    172         p_info_data[numOfEntries].tag_entry.data._slong = *(int32_t *)data;
    173       }
    174     }
    175     break;
    176     case EXIF_SRATIONAL: {
    177       if (count > 1) {
    178         srat_t *values = (srat_t *)malloc(count * sizeof(srat_t));
    179         if (values == NULL) {
    180           ALOGE("%s: No memory for signed rational array", __func__);
    181           rc = -1;
    182         } else {
    183           memcpy(values, data, count * sizeof(srat_t));
    184           p_info_data[numOfEntries].tag_entry.data._srats = values;
    185         }
    186       } else {
    187         p_info_data[numOfEntries].tag_entry.data._srat = *(srat_t *)data;
    188       }
    189     }
    190     break;
    191     }
    192 
    193     // Increase number of entries
    194     p_exif_info->numOfEntries++;
    195     return rc;
    196 }
    197 
    198 /** releaseExifEntry
    199  *
    200  *  Arguments:
    201  *   @p_exif_data : Exif info struct
    202  *
    203  *  Retrun     : int32_t type of status
    204  *               0  -- success
    205  *              none-zero failure code
    206  *
    207  *  Description:
    208  *       Function to release an entry from exif data
    209  *
    210  **/
    211 int32_t releaseExifEntry(QEXIF_INFO_DATA *p_exif_data)
    212 {
    213  switch (p_exif_data->tag_entry.type) {
    214   case EXIF_BYTE: {
    215     if (p_exif_data->tag_entry.count > 1 &&
    216       p_exif_data->tag_entry.data._bytes != NULL) {
    217       free(p_exif_data->tag_entry.data._bytes);
    218       p_exif_data->tag_entry.data._bytes = NULL;
    219     }
    220   }
    221   break;
    222   case EXIF_ASCII: {
    223     if (p_exif_data->tag_entry.data._ascii != NULL) {
    224       free(p_exif_data->tag_entry.data._ascii);
    225       p_exif_data->tag_entry.data._ascii = NULL;
    226     }
    227   }
    228   break;
    229   case EXIF_SHORT: {
    230     if (p_exif_data->tag_entry.count > 1 &&
    231       p_exif_data->tag_entry.data._shorts != NULL) {
    232       free(p_exif_data->tag_entry.data._shorts);
    233       p_exif_data->tag_entry.data._shorts = NULL;
    234     }
    235   }
    236   break;
    237   case EXIF_LONG: {
    238     if (p_exif_data->tag_entry.count > 1 &&
    239       p_exif_data->tag_entry.data._longs != NULL) {
    240       free(p_exif_data->tag_entry.data._longs);
    241       p_exif_data->tag_entry.data._longs = NULL;
    242     }
    243   }
    244   break;
    245   case EXIF_RATIONAL: {
    246     if (p_exif_data->tag_entry.count > 1 &&
    247       p_exif_data->tag_entry.data._rats != NULL) {
    248       free(p_exif_data->tag_entry.data._rats);
    249       p_exif_data->tag_entry.data._rats = NULL;
    250     }
    251   }
    252   break;
    253   case EXIF_UNDEFINED: {
    254     if (p_exif_data->tag_entry.data._undefined != NULL) {
    255       free(p_exif_data->tag_entry.data._undefined);
    256       p_exif_data->tag_entry.data._undefined = NULL;
    257     }
    258   }
    259   break;
    260   case EXIF_SLONG: {
    261     if (p_exif_data->tag_entry.count > 1 &&
    262       p_exif_data->tag_entry.data._slongs != NULL) {
    263       free(p_exif_data->tag_entry.data._slongs);
    264       p_exif_data->tag_entry.data._slongs = NULL;
    265     }
    266   }
    267   break;
    268   case EXIF_SRATIONAL: {
    269     if (p_exif_data->tag_entry.count > 1 &&
    270       p_exif_data->tag_entry.data._srats != NULL) {
    271       free(p_exif_data->tag_entry.data._srats);
    272       p_exif_data->tag_entry.data._srats = NULL;
    273     }
    274   }
    275   break;
    276   } /*end of switch*/
    277   return 0;
    278 }
    279 /** process_sensor_data:
    280  *
    281  *  Arguments:
    282  *   @p_sensor_params : ptr to sensor data
    283  *
    284  *  Return     : int32_t type of status
    285  *               NO_ERROR  -- success
    286  *              none-zero failure code
    287  *
    288  *  Description:
    289  *       process sensor data
    290  *
    291  *  Notes: this needs to be filled for the metadata
    292  **/
    293 int process_sensor_data(cam_sensor_params_t *p_sensor_params,
    294   QOMX_EXIF_INFO *exif_info)
    295 {
    296   int rc = 0;
    297   rat_t val_rat;
    298 
    299   if (NULL == p_sensor_params) {
    300     ALOGE("%s %d: Sensor params are null", __func__, __LINE__);
    301     return 0;
    302   }
    303 
    304   ALOGD("%s:%d] From metadata aperture = %f ", __func__, __LINE__,
    305     p_sensor_params->aperture_value );
    306 
    307   if (p_sensor_params->aperture_value >= 1.0) {
    308     double apex_value;
    309     apex_value = (double)2.0 * log(p_sensor_params->aperture_value) / log(2.0);
    310     val_rat.num = (uint32_t)(apex_value * 100);
    311     val_rat.denom = 100;
    312     rc = addExifEntry(exif_info, EXIFTAGID_APERTURE, EXIF_RATIONAL, 1, &val_rat);
    313     if (rc) {
    314       ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
    315     }
    316 
    317     val_rat.num = (uint32_t)(p_sensor_params->aperture_value * 100);
    318     val_rat.denom = 100;
    319     rc = addExifEntry(exif_info, EXIFTAGID_F_NUMBER, EXIF_RATIONAL, 1, &val_rat);
    320     if (rc) {
    321       ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
    322     }
    323   }
    324 
    325   /*Flash*/
    326   short val_short;
    327   if (p_sensor_params->flash_state == CAM_FLASH_STATE_FIRED) {
    328     val_short = 1;
    329   } else {
    330     val_short = 0;
    331   }
    332   //val_short =  (p_sensor_params->flash_mode << 3) | val_short;
    333   ALOGE("%s: Flash value %d flash mode %d flash state %d", __func__, val_short,
    334     p_sensor_params->flash_mode, p_sensor_params->flash_state);
    335   rc = addExifEntry(exif_info, EXIFTAGID_FLASH, EXIF_SHORT, 1, &val_short);
    336   if (rc) {
    337     ALOGE("%s %d]: Error adding flash exif entry", __func__, __LINE__);
    338   }
    339   return rc;
    340 }
    341 /** process_3a_data:
    342  *
    343  *  Arguments:
    344  *   @p_ae_params : ptr to aec data
    345  *
    346  *  Return     : int32_t type of status
    347  *               NO_ERROR  -- success
    348  *              none-zero failure code
    349  *
    350  *  Description:
    351  *       process 3a data
    352  *
    353  *  Notes: this needs to be filled for the metadata
    354  **/
    355 int process_3a_data(cam_ae_params_t *p_ae_params, QOMX_EXIF_INFO *exif_info)
    356 {
    357   int rc = 0;
    358   srat_t val_srat;
    359   rat_t val_rat;
    360   double shutter_speed_value;
    361 
    362   if (NULL == p_ae_params) {
    363     ALOGE("%s %d: 3A params are null", __func__, __LINE__);
    364     return 0;
    365   }
    366 
    367   ALOGD("%s:%d] exp_time %f, iso_value %d", __func__, __LINE__,
    368     p_ae_params->exp_time, p_ae_params->iso_value);
    369 
    370   /*Exposure time*/
    371   if (p_ae_params->exp_time == 0) {
    372       val_rat.num = 0;
    373       val_rat.denom = 0;
    374   } else {
    375       val_rat.num = 1;
    376       val_rat.denom = ROUND(1.0/p_ae_params->exp_time);
    377   }
    378   ALOGD("%s: numer %d denom %d", __func__, val_rat.num, val_rat.denom );
    379 
    380   rc = addExifEntry(exif_info, EXIFTAGID_EXPOSURE_TIME, EXIF_RATIONAL,
    381     (sizeof(val_rat)/(8)), &val_rat);
    382   if (rc) {
    383     ALOGE("%s:%d]: Error adding Exif Entry Exposure time",
    384       __func__, __LINE__);
    385   }
    386 
    387   /* Shutter Speed*/
    388   if (p_ae_params->exp_time > 0) {
    389     shutter_speed_value = log10(1/p_ae_params->exp_time)/log10(2);
    390     val_srat.num = shutter_speed_value * 1000;
    391     val_srat.denom = 1000;
    392   } else {
    393     val_srat.num = 0;
    394     val_srat.denom = 0;
    395   }
    396   rc = addExifEntry(exif_info, EXIFTAGID_SHUTTER_SPEED, EXIF_SRATIONAL,
    397     (sizeof(val_srat)/(8)), &val_srat);
    398   if (rc) {
    399     ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
    400   }
    401 
    402   /*ISO*/
    403   short val_short;
    404   val_short = p_ae_params->iso_value;
    405   rc = addExifEntry(exif_info, EXIFTAGID_ISO_SPEED_RATING, EXIF_SHORT,
    406     sizeof(val_short)/2, &val_short);
    407   if (rc) {
    408     ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
    409   }
    410 
    411 
    412  return rc;
    413 
    414 }
    415 
    416 /** process_meta_data_v1:
    417  *
    418  *  Arguments:
    419  *   @p_meta : ptr to metadata
    420  *   @exif_info: Exif info struct
    421  *
    422  *  Return     : int32_t type of status
    423  *               NO_ERROR  -- success
    424  *              none-zero failure code
    425  *
    426  *  Description:
    427  *       process awb debug info
    428  *
    429  **/
    430 int process_meta_data_v1(cam_metadata_info_t *p_meta, QOMX_EXIF_INFO *exif_info,
    431   mm_jpeg_exif_params_t *p_cam_exif_params)
    432 {
    433   int rc = 0;
    434 
    435   if (!p_meta) {
    436     ALOGE("%s %d:Meta data is NULL", __func__, __LINE__);
    437     return 0;
    438   }
    439   cam_ae_params_t *p_ae_params = p_meta->is_ae_params_valid ?
    440     &p_meta->ae_params : NULL;
    441 
    442   if (NULL != p_ae_params) {
    443     rc = process_3a_data(p_ae_params, exif_info);
    444     if (rc) {
    445       ALOGE("%s %d: Failed to extract 3a params", __func__, __LINE__);
    446     }
    447   }
    448   cam_sensor_params_t *p_sensor_params = p_meta->is_sensor_params_valid ?
    449     &p_meta->sensor_params : NULL;
    450 
    451   if (NULL != p_sensor_params) {
    452     rc = process_sensor_data(p_sensor_params, exif_info);
    453     if (rc) {
    454       ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__);
    455     }
    456   }
    457   return rc;
    458 }
    459 
    460 /** process_meta_data_v3:
    461  *
    462  *  Arguments:
    463  *   @p_meta : ptr to metadata
    464  *   @exif_info: Exif info struct
    465  *
    466  *  Return     : int32_t type of status
    467  *               NO_ERROR  -- success
    468  *              none-zero failure code
    469  *
    470  *  Description:
    471  *       Extract exif data from the metadata
    472  **/
    473 int process_meta_data_v3(metadata_buffer_t *p_meta, QOMX_EXIF_INFO *exif_info,
    474   mm_jpeg_exif_params_t *p_cam_exif_params)
    475 {
    476   int rc = 0;
    477   cam_sensor_params_t p_sensor_params;
    478   cam_ae_params_t p_ae_params;
    479 
    480   if (!p_meta) {
    481     ALOGE("%s %d:Meta data is NULL", __func__, __LINE__);
    482     return 0;
    483   }
    484   int32_t *iso =
    485     (int32_t *)POINTER_OF(CAM_INTF_META_SENSOR_SENSITIVITY, p_meta);
    486 
    487   int64_t *sensor_exposure_time =
    488     (int64_t *)POINTER_OF(CAM_INTF_META_SENSOR_EXPOSURE_TIME, p_meta);
    489 
    490   memset(&p_ae_params,  0,  sizeof(cam_ae_params_t));
    491   if (NULL != iso) {
    492     p_ae_params.iso_value= *iso;
    493   } else {
    494     ALOGE("%s: Cannot extract Iso value", __func__);
    495   }
    496 
    497   if (NULL != sensor_exposure_time) {
    498     p_ae_params.exp_time = (double)(*sensor_exposure_time / 1000000000.0);
    499   } else {
    500     ALOGE("%s: Cannot extract Exp time value", __func__);
    501   }
    502 
    503   rc = process_3a_data(&p_ae_params, exif_info);
    504   if (rc) {
    505     ALOGE("%s %d: Failed to add 3a exif params", __func__, __LINE__);
    506   }
    507 
    508   float *aperture = (float *)POINTER_OF(CAM_INTF_META_LENS_APERTURE, p_meta);
    509 
    510   uint8_t *flash_mode = (uint8_t *) POINTER_OF(CAM_INTF_META_FLASH_MODE, p_meta);
    511   uint8_t *flash_state =
    512     (uint8_t *) POINTER_OF(CAM_INTF_META_FLASH_STATE, p_meta);
    513 
    514   memset(&p_sensor_params, 0, sizeof(cam_sensor_params_t));
    515 
    516   if (NULL != aperture) {
    517      p_sensor_params.aperture_value = *aperture;
    518   } else {
    519     ALOGE("%s: Cannot extract Aperture value", __func__);
    520   }
    521 
    522   if (NULL != flash_mode) {
    523      p_sensor_params.flash_mode = *flash_mode;
    524   } else {
    525     ALOGE("%s: Cannot extract flash mode value", __func__);
    526   }
    527 
    528   if (NULL != flash_state) {
    529     p_sensor_params.flash_state = *flash_state;
    530   } else {
    531     ALOGE("%s: Cannot extract flash state value", __func__);
    532   }
    533 
    534   rc = process_sensor_data(&p_sensor_params, exif_info);
    535   if (rc) {
    536       ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__);
    537   }
    538 
    539   return rc;
    540 }
    541