1 /* Copyright (c) 2012-2014, 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 **/
addExifEntry(QOMX_EXIF_INFO * p_exif_info,exif_tag_id_t tagid,exif_tag_type_t type,uint32_t count,void * data)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 **/
releaseExifEntry(QEXIF_INFO_DATA * p_exif_data)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 **/
process_sensor_data(cam_sensor_params_t * p_sensor_params,QOMX_EXIF_INFO * exif_info)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 ALOGI("%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_3a_params : ptr to 3a 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 **/
process_3a_data(cam_3a_params_t * p_3a_params,QOMX_EXIF_INFO * exif_info)355 int process_3a_data(cam_3a_params_t *p_3a_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_3a_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, wb_mode %d", __func__, __LINE__,
368 p_3a_params->exp_time, p_3a_params->iso_value, p_3a_params->wb_mode);
369
370 /*Exposure time*/
371 if (p_3a_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_3a_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_3a_params->exp_time > 0) {
389 shutter_speed_value = log10(1/p_3a_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_3a_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 /*WB mode*/
412 if (p_3a_params->wb_mode == CAM_WB_MODE_AUTO)
413 val_short = 0;
414 else
415 val_short = 1;
416 rc = addExifEntry(exif_info, EXIFTAGID_WHITE_BALANCE, EXIF_SHORT,
417 sizeof(val_short)/2, &val_short);
418 if (rc) {
419 ALOGE("%s:%d]: Error adding Exif Entry", __func__, __LINE__);
420 }
421
422 return rc;
423 }
424
425 /** process_meta_data_v1:
426 *
427 * Arguments:
428 * @p_meta : ptr to metadata
429 * @exif_info: Exif info struct
430 *
431 * Return : int32_t type of status
432 * NO_ERROR -- success
433 * none-zero failure code
434 *
435 * Description:
436 * process awb debug info
437 *
438 **/
process_meta_data_v1(cam_metadata_info_t * p_meta,QOMX_EXIF_INFO * exif_info,mm_jpeg_exif_params_t * p_cam_exif_params)439 int process_meta_data_v1(cam_metadata_info_t *p_meta, QOMX_EXIF_INFO *exif_info,
440 mm_jpeg_exif_params_t *p_cam_exif_params)
441 {
442 int rc = 0;
443
444 if (!p_meta) {
445 ALOGE("%s %d:Meta data is NULL", __func__, __LINE__);
446 return 0;
447 }
448 cam_3a_params_t *p_3a_params = p_meta->is_3a_params_valid ?
449 &p_meta->cam_3a_params : NULL;
450
451 if (NULL != p_3a_params) {
452 rc = process_3a_data(p_3a_params, exif_info);
453 if (rc) {
454 ALOGE("%s %d: Failed to extract 3a params", __func__, __LINE__);
455 }
456 }
457 cam_sensor_params_t *p_sensor_params = p_meta->is_sensor_params_valid ?
458 &p_meta->sensor_params : NULL;
459
460 if (NULL != p_sensor_params) {
461 rc = process_sensor_data(p_sensor_params, exif_info);
462 if (rc) {
463 ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__);
464 }
465 }
466 return rc;
467 }
468
469 /** process_meta_data_v3:
470 *
471 * Arguments:
472 * @p_meta : ptr to metadata
473 * @exif_info: Exif info struct
474 *
475 * Return : int32_t type of status
476 * NO_ERROR -- success
477 * none-zero failure code
478 *
479 * Description:
480 * Extract exif data from the metadata
481 **/
process_meta_data_v3(metadata_buffer_t * p_meta,QOMX_EXIF_INFO * exif_info,mm_jpeg_exif_params_t * p_cam_exif_params)482 int process_meta_data_v3(metadata_buffer_t *p_meta, QOMX_EXIF_INFO *exif_info,
483 mm_jpeg_exif_params_t *p_cam_exif_params)
484 {
485 int rc = 0;
486 cam_sensor_params_t p_sensor_params;
487 cam_3a_params_t p_3a_params;
488
489 if (!p_meta) {
490 ALOGE("%s %d:Meta data is NULL", __func__, __LINE__);
491 return 0;
492 }
493
494 /* Process 3a data */
495 int32_t *iso =
496 (int32_t *)POINTER_OF(CAM_INTF_META_SENSOR_SENSITIVITY, p_meta);
497
498 int64_t *sensor_exposure_time =
499 (int64_t *)POINTER_OF(CAM_INTF_META_SENSOR_EXPOSURE_TIME, p_meta);
500
501 cam_wb_mode_type *wb_mode =
502 (cam_wb_mode_type *)POINTER_OF(CAM_INTF_PARM_WHITE_BALANCE, p_meta);
503
504 memset(&p_3a_params, 0, sizeof(cam_3a_params_t));
505 if (NULL != iso) {
506 p_3a_params.iso_value= *iso;
507 } else {
508 ALOGE("%s: Cannot extract Iso value", __func__);
509 }
510
511 if (NULL != sensor_exposure_time) {
512 p_3a_params.exp_time = (double)(*sensor_exposure_time / 1000000000.0);
513 } else {
514 ALOGE("%s: Cannot extract Exp time value", __func__);
515 }
516
517 if (NULL != wb_mode) {
518 p_3a_params.wb_mode = *wb_mode;
519 } else {
520 ALOGE("%s: Cannot extract white balance mode", __func__);
521 }
522
523 rc = process_3a_data(&p_3a_params, exif_info);
524 if (rc) {
525 ALOGE("%s %d: Failed to add 3a exif params", __func__, __LINE__);
526 }
527
528 /* Process sensor data */
529 float *aperture = (float *)POINTER_OF(CAM_INTF_META_LENS_APERTURE, p_meta);
530 uint8_t *flash_mode = (uint8_t *) POINTER_OF(CAM_INTF_META_FLASH_MODE, p_meta);
531 uint8_t *flash_state =
532 (uint8_t *) POINTER_OF(CAM_INTF_META_FLASH_STATE, p_meta);
533
534 memset(&p_sensor_params, 0, sizeof(cam_sensor_params_t));
535
536 if (NULL != aperture) {
537 p_sensor_params.aperture_value = *aperture;
538 } else {
539 ALOGE("%s: Cannot extract Aperture value", __func__);
540 }
541
542 if (NULL != flash_mode) {
543 p_sensor_params.flash_mode = *flash_mode;
544 } else {
545 ALOGE("%s: Cannot extract flash mode value", __func__);
546 }
547
548 if (NULL != flash_state) {
549 p_sensor_params.flash_state = *flash_state;
550 } else {
551 ALOGE("%s: Cannot extract flash state value", __func__);
552 }
553
554 rc = process_sensor_data(&p_sensor_params, exif_info);
555 if (rc) {
556 ALOGE("%s %d: Failed to extract sensor params", __func__, __LINE__);
557 }
558
559 return rc;
560 }
561