mm_jpeg_exif.c revision 1237194c260eaedcb6384c400b7add966e143c96
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#include <errno.h>
33#include <math.h>
34
35
36#define LOWER(a)               ((a) & 0xFFFF)
37#define UPPER(a)               (((a)>>16) & 0xFFFF)
38#define CHANGE_ENDIAN_16(a)  ((0x00FF & ((a)>>8)) | (0xFF00 & ((a)<<8)))
39#define ROUND(a)((a >= 0) ? (long)(a + 0.5) : (long)(a - 0.5))
40
41
42/** addExifEntry:
43 *
44 *  Arguments:
45 *   @exif_info : Exif info struct
46 *   @p_session: job session
47 *   @tagid   : exif tag ID
48 *   @type    : data type
49 *   @count   : number of data in uint of its type
50 *   @data    : input data ptr
51 *
52 *  Retrun     : int32_t type of status
53 *               0  -- success
54 *              none-zero failure code
55 *
56 *  Description:
57 *       Function to add an entry to exif data
58 *
59 **/
60int32_t addExifEntry(QOMX_EXIF_INFO *p_exif_info, exif_tag_id_t tagid,
61  exif_tag_type_t type, uint32_t count, void *data)
62{
63    int32_t rc = 0;
64    int32_t numOfEntries = p_exif_info->numOfEntries;
65    QEXIF_INFO_DATA *p_info_data = p_exif_info->exif_data;
66    if(numOfEntries >= MAX_EXIF_TABLE_ENTRIES) {
67        ALOGE("%s: Number of entries exceeded limit", __func__);
68        return -1;
69    }
70
71    p_info_data[numOfEntries].tag_id = tagid;
72    p_info_data[numOfEntries].tag_entry.type = type;
73    p_info_data[numOfEntries].tag_entry.count = count;
74    p_info_data[numOfEntries].tag_entry.copy = 1;
75    switch (type) {
76    case EXIF_BYTE: {
77      if (count > 1) {
78        uint8_t *values = (uint8_t *)malloc(count);
79        if (values == NULL) {
80          ALOGE("%s: No memory for byte array", __func__);
81          rc = -1;
82        } else {
83          memcpy(values, data, count);
84          p_info_data[numOfEntries].tag_entry.data._bytes = values;
85        }
86      } else {
87        p_info_data[numOfEntries].tag_entry.data._byte = *(uint8_t *)data;
88      }
89    }
90    break;
91    case EXIF_ASCII: {
92      char *str = NULL;
93      str = (char *)malloc(count + 1);
94      if (str == NULL) {
95        ALOGE("%s: No memory for ascii string", __func__);
96        rc = -1;
97      } else {
98        memset(str, 0, count + 1);
99        memcpy(str, data, count);
100        p_info_data[numOfEntries].tag_entry.data._ascii = str;
101      }
102    }
103    break;
104    case EXIF_SHORT: {
105      if (count > 1) {
106        uint16_t *values = (uint16_t *)malloc(count * sizeof(uint16_t));
107        if (values == NULL) {
108          ALOGE("%s: No memory for short array", __func__);
109          rc = -1;
110        } else {
111          memcpy(values, data, count * sizeof(uint16_t));
112          p_info_data[numOfEntries].tag_entry.data._shorts = values;
113        }
114      } else {
115        p_info_data[numOfEntries].tag_entry.data._short = *(uint16_t *)data;
116      }
117    }
118    break;
119    case EXIF_LONG: {
120      if (count > 1) {
121        uint32_t *values = (uint32_t *)malloc(count * sizeof(uint32_t));
122        if (values == NULL) {
123          ALOGE("%s: No memory for long array", __func__);
124          rc = -1;
125        } else {
126          memcpy(values, data, count * sizeof(uint32_t));
127          p_info_data[numOfEntries].tag_entry.data._longs = values;
128        }
129      } else {
130        p_info_data[numOfEntries].tag_entry.data._long = *(uint32_t *)data;
131      }
132    }
133    break;
134    case EXIF_RATIONAL: {
135      if (count > 1) {
136        rat_t *values = (rat_t *)malloc(count * sizeof(rat_t));
137        if (values == NULL) {
138          ALOGE("%s: No memory for rational array", __func__);
139          rc = -1;
140        } else {
141          memcpy(values, data, count * sizeof(rat_t));
142          p_info_data[numOfEntries].tag_entry.data._rats = values;
143        }
144      } else {
145        p_info_data[numOfEntries].tag_entry.data._rat = *(rat_t *)data;
146      }
147    }
148    break;
149    case EXIF_UNDEFINED: {
150      uint8_t *values = (uint8_t *)malloc(count);
151      if (values == NULL) {
152        ALOGE("%s: No memory for undefined array", __func__);
153        rc = -1;
154      } else {
155        memcpy(values, data, count);
156        p_info_data[numOfEntries].tag_entry.data._undefined = values;
157      }
158    }
159    break;
160    case EXIF_SLONG: {
161      if (count > 1) {
162        int32_t *values = (int32_t *)malloc(count * sizeof(int32_t));
163        if (values == NULL) {
164          ALOGE("%s: No memory for signed long array", __func__);
165          rc = -1;
166        } else {
167          memcpy(values, data, count * sizeof(int32_t));
168          p_info_data[numOfEntries].tag_entry.data._slongs = values;
169        }
170      } else {
171        p_info_data[numOfEntries].tag_entry.data._slong = *(int32_t *)data;
172      }
173    }
174    break;
175    case EXIF_SRATIONAL: {
176      if (count > 1) {
177        srat_t *values = (srat_t *)malloc(count * sizeof(srat_t));
178        if (values == NULL) {
179          ALOGE("%s: No memory for signed rational array", __func__);
180          rc = -1;
181        } else {
182          memcpy(values, data, count * sizeof(srat_t));
183          p_info_data[numOfEntries].tag_entry.data._srats = values;
184        }
185      } else {
186        p_info_data[numOfEntries].tag_entry.data._srat = *(srat_t *)data;
187      }
188    }
189    break;
190    }
191
192    // Increase number of entries
193    p_exif_info->numOfEntries++;
194    return rc;
195}
196
197/** releaseExifEntry
198 *
199 *  Arguments:
200 *   @p_exif_data : Exif info struct
201 *
202 *  Retrun     : int32_t type of status
203 *               0  -- success
204 *              none-zero failure code
205 *
206 *  Description:
207 *       Function to release an entry from exif data
208 *
209 **/
210int32_t releaseExifEntry(QEXIF_INFO_DATA *p_exif_data)
211{
212 switch (p_exif_data->tag_entry.type) {
213  case EXIF_BYTE: {
214    if (p_exif_data->tag_entry.count > 1 &&
215      p_exif_data->tag_entry.data._bytes != NULL) {
216      free(p_exif_data->tag_entry.data._bytes);
217      p_exif_data->tag_entry.data._bytes = NULL;
218    }
219  }
220  break;
221  case EXIF_ASCII: {
222    if (p_exif_data->tag_entry.data._ascii != NULL) {
223      free(p_exif_data->tag_entry.data._ascii);
224      p_exif_data->tag_entry.data._ascii = NULL;
225    }
226  }
227  break;
228  case EXIF_SHORT: {
229    if (p_exif_data->tag_entry.count > 1 &&
230      p_exif_data->tag_entry.data._shorts != NULL) {
231      free(p_exif_data->tag_entry.data._shorts);
232      p_exif_data->tag_entry.data._shorts = NULL;
233    }
234  }
235  break;
236  case EXIF_LONG: {
237    if (p_exif_data->tag_entry.count > 1 &&
238      p_exif_data->tag_entry.data._longs != NULL) {
239      free(p_exif_data->tag_entry.data._longs);
240      p_exif_data->tag_entry.data._longs = NULL;
241    }
242  }
243  break;
244  case EXIF_RATIONAL: {
245    if (p_exif_data->tag_entry.count > 1 &&
246      p_exif_data->tag_entry.data._rats != NULL) {
247      free(p_exif_data->tag_entry.data._rats);
248      p_exif_data->tag_entry.data._rats = NULL;
249    }
250  }
251  break;
252  case EXIF_UNDEFINED: {
253    if (p_exif_data->tag_entry.data._undefined != NULL) {
254      free(p_exif_data->tag_entry.data._undefined);
255      p_exif_data->tag_entry.data._undefined = NULL;
256    }
257  }
258  break;
259  case EXIF_SLONG: {
260    if (p_exif_data->tag_entry.count > 1 &&
261      p_exif_data->tag_entry.data._slongs != NULL) {
262      free(p_exif_data->tag_entry.data._slongs);
263      p_exif_data->tag_entry.data._slongs = NULL;
264    }
265  }
266  break;
267  case EXIF_SRATIONAL: {
268    if (p_exif_data->tag_entry.count > 1 &&
269      p_exif_data->tag_entry.data._srats != NULL) {
270      free(p_exif_data->tag_entry.data._srats);
271      p_exif_data->tag_entry.data._srats = NULL;
272    }
273  }
274  break;
275  } /*end of switch*/
276  return 0;
277}
278/** process_sensor_data:
279 *
280 *  Arguments:
281 *   @p_sensor_params : ptr to sensor data
282 *
283 *  Return     : int32_t type of status
284 *               NO_ERROR  -- success
285 *              none-zero failure code
286 *
287 *  Description:
288 *       process sensor data
289 *
290 *  Notes: this needs to be filled for the metadata
291 **/
292int process_sensor_data(cam_sensor_params_t *p_sensor_params,
293  QOMX_EXIF_INFO *exif_info)
294{
295  int rc = 0;
296  rat_t val_rat;
297
298  if (NULL == p_sensor_params) {
299    ALOGE("%s %d: Sensor params are null", __func__, __LINE__);
300    return 0;
301  }
302
303  ALOGD("%s:%d] From metadata aperture = %f ", __func__, __LINE__,
304    p_sensor_params->aperture_value );
305
306  val_rat.num = (uint32_t)(p_sensor_params->aperture_value * 100);
307  val_rat.denom = 100;
308  rc = addExifEntry(exif_info, EXIFTAGID_APERTURE, EXIF_RATIONAL, 1, &val_rat);
309  if (rc) {
310    ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
311  }
312
313  return rc;
314}
315/** process_3a_data:
316 *
317 *  Arguments:
318 *   @p_ae_params : ptr to aec data
319 *
320 *  Return     : int32_t type of status
321 *               NO_ERROR  -- success
322 *              none-zero failure code
323 *
324 *  Description:
325 *       process 3a data
326 *
327 *  Notes: this needs to be filled for the metadata
328 **/
329int process_3a_data(cam_ae_params_t *p_ae_params, QOMX_EXIF_INFO *exif_info)
330{
331  int rc = 0;
332  srat_t val_srat;
333  rat_t val_rat;
334  double shutter_speed_value;
335
336  if (NULL == p_ae_params) {
337    ALOGE("%s %d: 3A params are null", __func__, __LINE__);
338    return 0;
339  }
340
341  ALOGD("%s:%d] exp_time %f, iso_value %d", __func__, __LINE__,
342    p_ae_params->exp_time, p_ae_params->iso_value);
343
344  /*Exposure time*/
345  if (p_ae_params->exp_time == 0) {
346      val_rat.num = 0;
347      val_rat.denom = 0;
348  } else {
349      val_rat.num = 1;
350      val_rat.denom = ROUND((double)p_ae_params->exp_time * 1000);
351  }
352  ALOGE("%s: numer %d denom %d", __func__, val_rat.num, val_rat.denom );
353
354  rc = addExifEntry(exif_info, EXIFTAGID_EXPOSURE_TIME, EXIF_RATIONAL,
355    (sizeof(val_rat)/(8)), &val_rat);
356  if (rc) {
357    ALOGE("%s:%d]: Error adding Exif Entry Exposure time",
358      __func__, __LINE__);
359  }
360
361  /* Shutter Speed*/
362  if (p_ae_params->exp_time > 0) {
363    shutter_speed_value = log10(1/p_ae_params->exp_time)/log10(2);
364    val_srat.num = shutter_speed_value * 1000;
365    val_srat.denom = 1000;
366  } else {
367    val_srat.num = 0;
368    val_srat.denom = 0;
369  }
370  rc = addExifEntry(exif_info, EXIFTAGID_SHUTTER_SPEED, EXIF_SRATIONAL,
371    (sizeof(val_srat)/(8)), &val_srat);
372  if (rc) {
373    ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
374  }
375
376  /*ISO*/
377  short val_short;
378  val_short = p_ae_params->iso_value;
379  rc = addExifEntry(exif_info, EXIFTAGID_ISO_SPEED_RATING, EXIF_SHORT,
380    sizeof(val_short)/2, &val_short);
381  if (rc) {
382    ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
383  }
384
385 return rc;
386
387}
388/** processMetaData:
389 *
390 *  Arguments:
391 *   @p_meta : ptr to metadata
392 *   @exif_info: Exif info struct
393 *
394 *  Return     : int32_t type of status
395 *               NO_ERROR  -- success
396 *              none-zero failure code
397 *
398 *  Description:
399 *       process awb debug info
400 *
401 *  Notes: this needs to be filled for the metadata
402 **/
403int process_meta_data(cam_metadata_info_t *p_meta, QOMX_EXIF_INFO *exif_info,
404  mm_jpeg_exif_params_t *p_cam_exif_params)
405{
406  int rc = 0;
407
408  if (!p_meta) {
409    ALOGE("%s %d:Meta data is NULL", __func__, __LINE__);
410    return 0;
411  }
412  cam_ae_params_t *p_ae_params = p_meta->is_ae_params_valid ?
413    &p_meta->ae_params : &p_cam_exif_params->ae_params;
414
415  if (NULL != p_ae_params) {
416    rc = process_3a_data(p_ae_params, exif_info);
417    if (rc) {
418      ALOGE("%s %d: Failed to extract 3a params", __func__, __LINE__);
419    }
420  }
421  cam_sensor_params_t *p_sensor_params = p_meta->is_sensor_params_valid ?
422    &p_meta->sensor_params : &p_cam_exif_params->sensor_params;
423
424  if (NULL != p_sensor_params) {
425    rc = process_sensor_data(p_sensor_params, exif_info);
426    if (rc) {
427      ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__);
428    }
429  }
430  return rc;
431}
432