mm_jpeg_exif.c revision 7a737278045be557b6b1b8a1bd53033faec67166
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 **/ 61int32_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 **/ 211int32_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 **/ 293int 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 val_rat.num = (uint32_t)(p_sensor_params->aperture_value * 100); 308 val_rat.denom = 100; 309 rc = addExifEntry(exif_info, EXIFTAGID_APERTURE, EXIF_RATIONAL, 1, &val_rat); 310 if (rc) { 311 ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__); 312 } 313 314 /*Flash*/ 315 short val_short; 316 if (p_sensor_params->flash_state == CAM_FLASH_STATE_FIRED) { 317 val_short = 1; 318 } else { 319 val_short = 0; 320 } 321 //val_short = (p_sensor_params->flash_mode << 3) | val_short; 322 ALOGE("%s: Flash value %d flash mode %d flash state %d", __func__, val_short, 323 p_sensor_params->flash_mode, p_sensor_params->flash_state); 324 rc = addExifEntry(exif_info, EXIFTAGID_FLASH, EXIF_SHORT, 1, &val_short); 325 if (rc) { 326 ALOGE("%s %d]: Error adding flash exif entry", __func__, __LINE__); 327 } 328 return rc; 329} 330/** process_3a_data: 331 * 332 * Arguments: 333 * @p_ae_params : ptr to aec data 334 * 335 * Return : int32_t type of status 336 * NO_ERROR -- success 337 * none-zero failure code 338 * 339 * Description: 340 * process 3a data 341 * 342 * Notes: this needs to be filled for the metadata 343 **/ 344int process_3a_data(cam_ae_params_t *p_ae_params, QOMX_EXIF_INFO *exif_info) 345{ 346 int rc = 0; 347 srat_t val_srat; 348 rat_t val_rat; 349 double shutter_speed_value; 350 351 if (NULL == p_ae_params) { 352 ALOGE("%s %d: 3A params are null", __func__, __LINE__); 353 return 0; 354 } 355 356 ALOGD("%s:%d] exp_time %f, iso_value %d", __func__, __LINE__, 357 p_ae_params->exp_time, p_ae_params->iso_value); 358 359 /*Exposure time*/ 360 if (p_ae_params->exp_time == 0) { 361 val_rat.num = 0; 362 val_rat.denom = 0; 363 } else { 364 val_rat.num = 1; 365 val_rat.denom = ROUND(1.0/p_ae_params->exp_time); 366 } 367 ALOGD("%s: numer %d denom %d", __func__, val_rat.num, val_rat.denom ); 368 369 rc = addExifEntry(exif_info, EXIFTAGID_EXPOSURE_TIME, EXIF_RATIONAL, 370 (sizeof(val_rat)/(8)), &val_rat); 371 if (rc) { 372 ALOGE("%s:%d]: Error adding Exif Entry Exposure time", 373 __func__, __LINE__); 374 } 375 376 /* Shutter Speed*/ 377 if (p_ae_params->exp_time > 0) { 378 shutter_speed_value = log10(1/p_ae_params->exp_time)/log10(2); 379 val_srat.num = shutter_speed_value * 1000; 380 val_srat.denom = 1000; 381 } else { 382 val_srat.num = 0; 383 val_srat.denom = 0; 384 } 385 rc = addExifEntry(exif_info, EXIFTAGID_SHUTTER_SPEED, EXIF_SRATIONAL, 386 (sizeof(val_srat)/(8)), &val_srat); 387 if (rc) { 388 ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__); 389 } 390 391 /*ISO*/ 392 short val_short; 393 val_short = p_ae_params->iso_value; 394 rc = addExifEntry(exif_info, EXIFTAGID_ISO_SPEED_RATING, EXIF_SHORT, 395 sizeof(val_short)/2, &val_short); 396 if (rc) { 397 ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__); 398 } 399 400 401 return rc; 402 403} 404 405/** process_meta_data_v1: 406 * 407 * Arguments: 408 * @p_meta : ptr to metadata 409 * @exif_info: Exif info struct 410 * 411 * Return : int32_t type of status 412 * NO_ERROR -- success 413 * none-zero failure code 414 * 415 * Description: 416 * process awb debug info 417 * 418 **/ 419int process_meta_data_v1(cam_metadata_info_t *p_meta, QOMX_EXIF_INFO *exif_info, 420 mm_jpeg_exif_params_t *p_cam_exif_params) 421{ 422 int rc = 0; 423 424 if (!p_meta) { 425 ALOGE("%s %d:Meta data is NULL", __func__, __LINE__); 426 return 0; 427 } 428 cam_ae_params_t *p_ae_params = p_meta->is_ae_params_valid ? 429 &p_meta->ae_params : NULL; 430 431 if (NULL != p_ae_params) { 432 rc = process_3a_data(p_ae_params, exif_info); 433 if (rc) { 434 ALOGE("%s %d: Failed to extract 3a params", __func__, __LINE__); 435 } 436 } 437 cam_sensor_params_t *p_sensor_params = p_meta->is_sensor_params_valid ? 438 &p_meta->sensor_params : NULL; 439 440 if (NULL != p_sensor_params) { 441 rc = process_sensor_data(p_sensor_params, exif_info); 442 if (rc) { 443 ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__); 444 } 445 } 446 return rc; 447} 448 449/** process_meta_data_v3: 450 * 451 * Arguments: 452 * @p_meta : ptr to metadata 453 * @exif_info: Exif info struct 454 * 455 * Return : int32_t type of status 456 * NO_ERROR -- success 457 * none-zero failure code 458 * 459 * Description: 460 * Extract exif data from the metadata 461 **/ 462int process_meta_data_v3(metadata_buffer_t *p_meta, QOMX_EXIF_INFO *exif_info, 463 mm_jpeg_exif_params_t *p_cam_exif_params) 464{ 465 int rc = 0; 466 cam_sensor_params_t p_sensor_params; 467 cam_ae_params_t p_ae_params; 468 469 if (!p_meta) { 470 ALOGE("%s %d:Meta data is NULL", __func__, __LINE__); 471 return 0; 472 } 473 int32_t *iso = 474 (int32_t *)POINTER_OF(CAM_INTF_META_SENSOR_SENSITIVITY, p_meta); 475 476 int64_t *sensor_exposure_time = 477 (int64_t *)POINTER_OF(CAM_INTF_META_SENSOR_EXPOSURE_TIME, p_meta); 478 479 memset(&p_ae_params, 0, sizeof(cam_ae_params_t)); 480 if (NULL != iso) { 481 p_ae_params.iso_value= *iso; 482 } else { 483 ALOGE("%s: Cannot extract Iso value", __func__); 484 } 485 486 if (NULL != sensor_exposure_time) { 487 p_ae_params.exp_time = (double)(*sensor_exposure_time / 1000000000.0); 488 } else { 489 ALOGE("%s: Cannot extract Exp time value", __func__); 490 } 491 492 rc = process_3a_data(&p_ae_params, exif_info); 493 if (rc) { 494 ALOGE("%s %d: Failed to add 3a exif params", __func__, __LINE__); 495 } 496 497 float *aperture = (float *)POINTER_OF(CAM_INTF_META_LENS_APERTURE, p_meta); 498 499 uint8_t *flash_mode = (uint8_t *) POINTER_OF(CAM_INTF_META_FLASH_MODE, p_meta); 500 uint8_t *flash_state = 501 (uint8_t *) POINTER_OF(CAM_INTF_META_FLASH_STATE, p_meta); 502 503 memset(&p_sensor_params, 0, sizeof(cam_sensor_params_t)); 504 505 if (NULL != aperture) { 506 p_sensor_params.aperture_value = *aperture; 507 } else { 508 ALOGE("%s: Cannot extract Aperture value", __func__); 509 } 510 511 if (NULL != flash_mode) { 512 p_sensor_params.flash_mode = *flash_mode; 513 } else { 514 ALOGE("%s: Cannot extract flash mode value", __func__); 515 } 516 517 if (NULL != flash_state) { 518 p_sensor_params.flash_state = *flash_state; 519 } else { 520 ALOGE("%s: Cannot extract flash state value", __func__); 521 } 522 523 rc = process_sensor_data(&p_sensor_params, exif_info); 524 if (rc) { 525 ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__); 526 } 527 528 return rc; 529} 530