4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jeongmo Yang <jm80.yang@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "mm_camcorder_exifinfo.h"
23 #include "mm_camcorder_exifdef.h"
24 #include "mm_camcorder_internal.h"
26 #include <libexif/exif-loader.h>
27 #include <libexif/exif-utils.h>
28 #include <libexif/exif-data.h>
37 #define MM_EXIFINFO_USE_BINARY_EXIFDATA 1
38 #define JPEG_MAX_SIZE 20000000
39 #define JPEG_THUMBNAIL_MAX_SIZE (128*1024)
40 #define JPEG_DATA_OFFSET 2
41 #define JPEG_EXIF_OFFSET 4
42 #define EXIF_MARKER_SOI_LENGTH 2
43 #define EXIF_MARKER_APP1_LENGTH 2
44 #define EXIF_APP1_LENGTH 2
47 #if MM_EXIFINFO_USE_BINARY_EXIFDATA
52 #define _EXIF_BIN_SIZE_ ((unsigned int)174)
53 unsigned char g_exif_bin [_EXIF_BIN_SIZE_] = {
54 0x45 , 0x78 , 0x69 , 0x66 , 0x00 , 0x00 , 0x49 , 0x49 , 0x2a , 0x00 , 0x08 , 0x00 , 0x00 , 0x00 , 0x05 , 0x00
55 , 0x1a , 0x01 , 0x05 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x4a , 0x00 , 0x00 , 0x00 , 0x1b , 0x01 , 0x05 , 0x00
56 , 0x01 , 0x00 , 0x00 , 0x00 , 0x52 , 0x00 , 0x00 , 0x00 , 0x28 , 0x01 , 0x03 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00
57 , 0x02 , 0x00 , 0x00 , 0x00 , 0x13 , 0x02 , 0x03 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00
58 , 0x69 , 0x87 , 0x04 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x5a , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
59 , 0x48 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x48 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00
60 , 0x06 , 0x00 , 0x00 , 0x90 , 0x07 , 0x00 , 0x04 , 0x00 , 0x00 , 0x00 , 0x30 , 0x32 , 0x31 , 0x30 , 0x01 , 0x91
61 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xa0 , 0x07 , 0x00 , 0x04 , 0x00
62 , 0x00 , 0x00 , 0x30 , 0x31 , 0x30 , 0x30 , 0x01 , 0xa0 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
63 , 0x00 , 0x00 , 0x02 , 0xa0 , 0x04 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 , 0xa0
64 , 0x04 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
69 * Structure for exif entry.
71 typedef struct _mm_exif_entry_t
73 ExifTag tag; /**< exif tag*/
74 ExifFormat format; /**< exif format*/
75 unsigned long components; /**< number of components*/
76 unsigned char *data; /**< data*/
77 unsigned int size; /**< size of data*/
85 _exif_set_uint16 (int is_motorola, void * out, unsigned short in)
88 ((unsigned char *)out)[0] = in & 0x00ff;
89 ((unsigned char *)out)[1] = in >> 8;
91 ((unsigned char *)out)[0] = in >> 8;
92 ((unsigned char *)out)[1] = in & 0x00ff;
97 #ifdef _MMCAMCORDER_EXIF_GET_JPEG_MARKER_OFFSET
99 _exif_get_jpeg_marker_offset (void *jpeg, int jpeg_size, unsigned short marker)
101 unsigned char *p = NULL;
102 unsigned char *src = jpeg;
103 int src_sz = jpeg_size;
109 m[1] = marker & 0x00FF;
111 _mmcam_dbg_log("marker: 0x%02X 0x%02X", m[0], m[1]);
113 if (*src == 0xff && *(src + 1) == 0xd8)
115 p = src + 2; /* SOI(start of image) */
119 _mmcam_dbg_log("invalid JPEG file.");
123 for (i = 0; i < src_sz - (1 + 2); i++, p++)
128 if (*(p + 1) == m[1])
131 _mmcam_dbg_log("marker offset: %lu %p %p.",ret, (p+1), src);
136 _mmcam_dbg_log("Marker not found.");
139 #endif /* _MMCAMCORDER_EXIF_GET_JPEG_MARKER_OFFSET */
143 mm_exif_get_exif_data_from_data (mm_exif_info_t *info)
147 ed = exif_data_new_from_data(info->data, info->size);
150 _mmcam_dbg_log("Null exif data. (ed:%p)", ed);
158 mm_exif_get_exif_from_info (mm_exif_info_t *info)
161 ExifLoader *loader = NULL;
163 unsigned char size[2];
166 /*get ExifData from info*/
167 loader = exif_loader_new ();
169 size[0] = (unsigned char) (info->size);
170 size[1] = (unsigned char) (info->size >> 8);
171 exif_loader_write (loader, size, 2);
173 for (i = 0; i < info->size && exif_loader_write (loader, info->data + i, 1); i++);
175 ed = exif_loader_get_data (loader);
176 exif_loader_unref (loader);
182 mm_exif_set_exif_to_info (mm_exif_info_t *info, ExifData *exif)
184 unsigned char *eb = NULL;
189 _mmcam_dbg_log("exif Null");
190 return MM_ERROR_CAMCORDER_NOT_INITIALIZED;
193 _mmcam_dbg_log("exif(ifd :%p)", exif->ifd);
202 exif_data_save_data (exif, &eb, &ebs);
205 _mmcam_dbg_log("MM_ERROR_CAMCORDER_LOW_MEMORY");
206 return MM_ERROR_CAMCORDER_LOW_MEMORY;
210 return MM_ERROR_NONE;
215 mm_exif_set_add_entry (ExifData *exif, ExifIfd ifd, ExifTag tag,ExifFormat format,unsigned long components, const char* data)
217 ExifData *ed = (ExifData *)exif;
220 if (exif == NULL || format <= 0 || components <= 0 || data == NULL) {
221 _mmcam_dbg_err("invalid argument exif=%p format=%d, components=%lu data=%p!",
222 exif,format,components,data);
223 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
226 /*remove same tag in EXIF*/
227 exif_content_remove_entry(ed->ifd[ifd], exif_content_get_entry(ed->ifd[ifd], tag));
230 e = exif_entry_new();
232 _mmcam_dbg_err("entry create error!");
233 return MM_ERROR_CAMCORDER_LOW_MEMORY;
236 exif_entry_initialize(e, tag);
240 e->components = components;
244 e->data = malloc(exif_format_get_size(format) * e->components);
247 return MM_ERROR_CAMCORDER_LOW_MEMORY;
250 if (format == EXIF_FORMAT_ASCII) {
251 memset(e->data, '\0', exif_format_get_size(format) * e->components);
255 e->size = exif_format_get_size(format) * e->components;
256 memcpy(e->data,data,e->size);
257 exif_content_add_entry(ed->ifd[ifd], e);
260 return MM_ERROR_NONE;
272 mm_exif_create_exif_info (mm_exif_info_t **info)
274 mm_exif_info_t *x = NULL;
275 #if (MM_EXIFINFO_USE_BINARY_EXIFDATA == 0)
277 unsigned char *eb = NULL;
283 _mmcam_dbg_err("NULL pointer");
284 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
287 x = malloc(sizeof(mm_exif_info_t));
289 _mmcam_dbg_err("malloc error");
290 return MM_ERROR_CAMCORDER_LOW_MEMORY;
292 #if MM_EXIFINFO_USE_BINARY_EXIFDATA
294 x->data = malloc(_EXIF_BIN_SIZE_);
296 _mmcam_dbg_err("malloc error");
298 return MM_ERROR_CAMCORDER_LOW_MEMORY;
300 memcpy(x->data, g_exif_bin, _EXIF_BIN_SIZE_);
301 x->size = _EXIF_BIN_SIZE_;
303 ed = exif_data_new();
305 _mmcam_dbg_err("exif data new error");
306 return MM_ERROR_CAMCORDER_LOW_MEMORY;
309 exif_data_set_byte_order(ed, EXIF_BYTE_ORDER_INTEL);
310 exif_data_set_data_type(ed, EXIF_DATA_TYPE_COMPRESSED);
311 exif_data_set_option(ed, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
315 exif_data_save_data(ed, &eb, &ebs);
317 _mmcam_dbg_err("exif_data_save_data error");
321 return MM_ERROR_CAMCORDER_INTERNAL;
330 _mmcam_dbg_log("Data:%p Size:%d", x->data, x->size);
332 return MM_ERROR_NONE;
336 mm_exif_destory_exif_info (mm_exif_info_t *info)
338 //_mmcam_dbg_log( "");
340 #if MM_EXIFINFO_USE_BINARY_EXIFDATA
349 exif_mem_free (info->data);
357 mm_exif_add_thumbnail_info (mm_exif_info_t *info, void *thumbnail, int width, int height, int len)
360 static ExifLong elong[10];
362 unsigned char *p_compressed = NULL;
363 int ret = MM_ERROR_NONE;
366 _mmcam_dbg_log("Thumbnail size:%d, width:%d, height:%d", len, width, height);
368 if( len > JPEG_THUMBNAIL_MAX_SIZE )
370 _mmcam_dbg_err("Thumbnail size[%d] over!!! Skip inserting thumbnail...", len);
371 return MM_ERROR_NONE;
374 /* get ExifData from info*/
375 ed = mm_exif_get_exif_from_info(info);
377 _mmcam_dbg_err("mm_exif_get_exif_from_info failed");
378 return MM_ERROR_CAMCORDER_INTERNAL;
381 ed->data = thumbnail;
384 /* set thumbnail data */
385 p_compressed = (unsigned char *)malloc(sizeof(ExifShort));
386 if (p_compressed != NULL) {
387 exif_set_short(p_compressed, exif_data_get_byte_order(ed), 6);
388 ret = mm_exif_set_add_entry(ed, EXIF_IFD_1, EXIF_TAG_COMPRESSION, EXIF_FORMAT_SHORT, 1, (const char *)p_compressed);
389 if (ret != MM_ERROR_NONE) {
393 ret = MM_ERROR_CAMCORDER_LOW_MEMORY;
397 /* set thumbnail size */
398 exif_set_long ((unsigned char *)&elong[cntl], exif_data_get_byte_order(ed), width);
399 ret = mm_exif_set_add_entry(ed, EXIF_IFD_1, EXIF_TAG_IMAGE_WIDTH, EXIF_FORMAT_LONG, 1, (const char *)&elong[cntl++]);
400 if (ret != MM_ERROR_NONE) {
403 exif_set_long ((unsigned char *)&elong[cntl], exif_data_get_byte_order(ed), height);
404 ret = mm_exif_set_add_entry(ed, EXIF_IFD_1, EXIF_TAG_IMAGE_LENGTH, EXIF_FORMAT_LONG, 1, (const char *)&elong[cntl++]);
405 if (ret != MM_ERROR_NONE) {
409 ret = mm_exif_set_exif_to_info (info, ed);
410 if (ret != MM_ERROR_NONE) {
416 exif_data_unref (ed);
419 if(p_compressed != NULL)
426 mm_exif_write_exif_jpeg_to_file (char *filename, mm_exif_info_t *info, void *jpeg, int jpeg_len)
429 unsigned short head[2] = {0,};
430 unsigned short head_len = 0;
431 unsigned char *eb = NULL;
440 fp = fopen (filename, "wb");
442 _mmcam_dbg_err( "fopen() failed [%s].", filename);
443 return MM_ERROR_IMAGE_FILEOPEN;
447 _exif_set_uint16 (0, &head[0], 0xffd8);
448 _exif_set_uint16 (0, &head[1], 0xffe1);
449 /*set header length*/
450 _exif_set_uint16 (0, &head_len, (unsigned short)(ebs + 2));
452 if(head[0]==0 || head[1]==0 || head_len==0)
454 _mmcam_dbg_err("setting error");
459 fwrite (&head[0], 1, EXIF_MARKER_SOI_LENGTH, fp); /*SOI marker*/
460 fwrite (&head[1], 1, EXIF_MARKER_APP1_LENGTH, fp); /*APP1 marker*/
461 fwrite (&head_len, 1, EXIF_APP1_LENGTH, fp); /*length of APP1*/
462 fwrite (eb, 1, ebs, fp); /*EXIF*/
463 fwrite (jpeg + JPEG_DATA_OFFSET, 1, jpeg_len - JPEG_DATA_OFFSET, fp); /*IMAGE*/
467 return MM_ERROR_NONE;
471 mm_exif_write_exif_jpeg_to_memory (void **mem, unsigned int *length, mm_exif_info_t *info, void *jpeg, unsigned int jpeg_len)
473 unsigned short head[2] = {0,};
474 unsigned short head_len = 0;
475 unsigned char *eb = NULL;
477 int jpeg_offset = JPEG_DATA_OFFSET;
478 mm_exif_info_t *test_exif_info = NULL;
481 unsigned char *m = NULL;
486 if(info==NULL || jpeg==NULL)
488 _mmcam_dbg_err( "MM_ERROR_CAMCORDER_INVALID_ARGUMENT info=%p, jpeg=%p",info,jpeg);
489 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
492 if(jpeg_len>JPEG_MAX_SIZE)
494 _mmcam_dbg_err( "jpeg_len is worng jpeg_len=%d",jpeg_len);
495 return MM_ERROR_CAMCORDER_DEVICE_WRONG_JPEG;
501 /* check EXIF in JPEG */
502 if (mm_exif_load_exif_info(&test_exif_info, jpeg, jpeg_len) == MM_ERROR_NONE) {
503 if (test_exif_info) {
504 jpeg_offset = test_exif_info->size + JPEG_EXIF_OFFSET;
505 if (test_exif_info->data) {
506 free(test_exif_info->data);
507 test_exif_info->data = NULL;
509 free(test_exif_info);
510 test_exif_info = NULL;
512 _mmcam_dbg_err("test_exif_info is NULL");
515 _mmcam_dbg_warn("no EXIF in JPEG");
518 /*length of output image*/
519 /*SOI + APP1 + length of APP1 + length of EXIF + IMAGE*/
520 m_len = EXIF_MARKER_SOI_LENGTH + EXIF_MARKER_APP1_LENGTH + EXIF_APP1_LENGTH + ebs + (jpeg_len - jpeg_offset);
521 /*alloc output image*/
524 _mmcam_dbg_err( "malloc() failed.");
525 return MM_ERROR_CAMCORDER_LOW_MEMORY;
529 _exif_set_uint16 (0, &head[0], 0xffd8);
530 _exif_set_uint16 (0, &head[1], 0xffe1);
531 /*set header length*/
532 _exif_set_uint16 (0, &head_len, (unsigned short)(ebs + 2));
533 if (head[0] == 0 || head[1] == 0 || head_len == 0) {
534 _mmcam_dbg_err("setting error");
536 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
539 /* Complete JPEG+EXIF */
541 memcpy(m, &head[0], EXIF_MARKER_SOI_LENGTH);
543 memcpy(m + EXIF_MARKER_SOI_LENGTH,
544 &head[1], EXIF_MARKER_APP1_LENGTH);
546 memcpy(m + EXIF_MARKER_SOI_LENGTH + EXIF_MARKER_APP1_LENGTH,
547 &head_len, EXIF_APP1_LENGTH);
549 memcpy(m + EXIF_MARKER_SOI_LENGTH + EXIF_MARKER_APP1_LENGTH + EXIF_APP1_LENGTH,
552 memcpy(m + EXIF_MARKER_SOI_LENGTH + EXIF_MARKER_APP1_LENGTH + EXIF_APP1_LENGTH + ebs,
553 jpeg + jpeg_offset, jpeg_len - jpeg_offset);
555 _mmcam_dbg_log("JPEG+EXIF Copy DONE(original:%d, offset:%d, copied:%d)",
556 jpeg_len, jpeg_offset, jpeg_len - jpeg_offset);
562 return MM_ERROR_NONE;
566 int mm_exif_load_exif_info(mm_exif_info_t **info, void *jpeg_data, int jpeg_length)
568 ExifLoader *loader = NULL;
569 const unsigned char *b = NULL;
571 mm_exif_info_t *x = NULL;
572 // TODO : get exif and re-set exif
573 loader = exif_loader_new();
575 exif_loader_write (loader, jpeg_data, jpeg_length);
576 exif_loader_get_buf (loader, &b, &s);
578 x = malloc(sizeof(mm_exif_info_t));
582 memcpy((char*)x->data, b, s);
585 _mmcam_dbg_warn("load EXIF : data %p, size %d", x->data, x->size);
587 _mmcam_dbg_err("mm_exif_info_t malloc failed");
589 exif_loader_unref(loader);
590 return MM_ERROR_CAMCORDER_LOW_MEMORY;
593 _mmcam_dbg_err("mm_exif_info_t malloc failed");
596 _mmcam_dbg_err("exif_loader_get_buf failed");
599 /* The loader is no longer needed--free it */
600 exif_loader_unref(loader);
603 _mmcam_dbg_err("exif_loader_new failed");
607 return MM_ERROR_NONE;
609 return MM_ERROR_CAMCORDER_INTERNAL;