2 * Copyright (C) 2007 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
24 * Reads EXIF tags from images.
26 * @todo: get GPS data.
27 * @todo: check if worth using mmap().
34 #define _XOPEN_SOURCE 600
35 #include <lightmediascanner_plugin.h>
36 #include <lightmediascanner_utils.h>
37 #include <lightmediascanner_db.h>
38 #include <sys/types.h>
49 JPEG_MARKER_SOI = 0xd8,
50 JPEG_MARKER_DQT = 0xdb,
51 JPEG_MARKER_JFIF = 0xe0,
52 JPEG_MARKER_EXIF = 0xe1,
53 JPEG_MARKER_COMM = 0xfe,
54 JPEG_MARKER_SOF0 = 0xc0,
55 JPEG_MARKER_SOF1 = 0xc1,
56 JPEG_MARKER_SOF2 = 0xc2,
57 JPEG_MARKER_SOF9 = 0xc9,
58 JPEG_MARKER_SOF10 = 0xca,
59 JPEG_MARKER_SOS = 0xda
63 * Process SOF JPEG, this contains width and height.
66 _jpeg_sof_process(int fd, unsigned short *width, unsigned short *height)
70 if (read(fd, buf, 6) != 6) {
71 perror("could not read() SOF data");
75 *height = (buf[1] << 8) | buf[2];
76 *width = (buf[3] << 8) | buf[4];
82 * Process COM JPEG, this contains user comment.
85 _jpeg_com_process(int fd, int len, struct lms_string_size *comment)
93 comment->str = malloc(len + 1);
98 if (read(fd, comment->str, len) != len) {
105 if (comment->str[len - 1] == '\0')
108 comment->str[len] = '\0';
111 lms_string_size_strip_and_free(comment);
117 * Walk JPEG markers in order to get useful information.
120 _jpeg_info_get(int fd, int len, struct lms_image_info *info)
122 unsigned char buf[4];
126 found = info->title.str ? 1 : 0;
127 offset = lseek(fd, len - 2, SEEK_CUR);
130 offset = lseek(fd, offset + len, SEEK_SET);
136 if (read(fd, buf, 4) != 4) {
141 len = ((buf[2] << 8) | buf[3]) - 2;
143 if (buf[0] != 0xff) {
144 fprintf(stderr, "ERROR: expected 0xff marker, got %#x\n", buf[0]);
148 if (buf[1] == JPEG_MARKER_SOF0 ||
149 buf[1] == JPEG_MARKER_SOF1 ||
150 buf[1] == JPEG_MARKER_SOF2 ||
151 buf[1] == JPEG_MARKER_SOF9 ||
152 buf[1] == JPEG_MARKER_SOF10) {
153 if (_jpeg_sof_process(fd, &info->width, &info->height) != 0)
156 } else if (buf[1] == JPEG_MARKER_COMM && !info->title.str) {
157 if (_jpeg_com_process(fd, len, &info->title) != 0)
160 } else if (buf[1] == JPEG_MARKER_SOS)
163 len += 4; /* add read size */
170 * Read JPEG file start (0xffd8 marker) and return the next
171 * marker type and its length.
174 _jpeg_data_get(int fd, int *type, int *len)
176 unsigned char buf[6];
178 if (lseek(fd, 0, SEEK_SET) != 0) {
183 if (read(fd, buf, 6) != 6) {
188 if (buf[0] != 0xff || buf[1] != JPEG_MARKER_SOI || buf[2] != 0xff) {
189 fprintf(stderr, "ERROR: not JPEG file (magic=%#x %#x %#x)\n",
190 buf[0], buf[1], buf[2]);
195 *len = (buf[4] << 8) | buf[5];
200 #define LE_4BYTE(a) ((a)[0] | ((a)[1] << 8) | ((a)[2] << 16) | ((a)[3] << 24))
201 #define BE_4BYTE(a) (((a)[0] << 24) | ((a)[1] << 16) | ((a)[2] << 8) | (a)[3])
203 #define LE_2BYTE(a) ((a)[0] | ((a)[1] << 8))
204 #define BE_2BYTE(a) (((a)[0] << 8) | (a)[1])
206 #define E_2BTYE(little_endian, a) ((little_endian) ? LE_2BYTE(a) : BE_2BYTE(a))
207 #define E_4BTYE(little_endian, a) ((little_endian) ? LE_4BYTE(a) : BE_4BYTE(a))
210 EXIF_TYPE_BYTE = 1, /* 8 bit unsigned */
211 EXIF_TYPE_ASCII = 2, /* 8 bit byte with 7-bit ASCII code, NULL terminated */
212 EXIF_TYPE_SHORT = 3, /* 2-byte unsigned integer */
213 EXIF_TYPE_LONG = 4, /* 4-byte unsigned integer */
214 EXIF_TYPE_RATIONAL = 5, /* 2 4-byte unsigned integer, 1st = numerator */
215 EXIF_TYPE_UNDEFINED = 7, /* 8-bit byte */
216 EXIF_TYPE_SLONG = 9, /* 4-byte signed integer (2'complement) */
217 EXIF_TYPE_SRATIONAL = 10 /* 2 4-byte signed integer, 1st = numerator */
221 EXIF_TAG_ORIENTATION = 0x0112,
222 EXIF_TAG_ARTIST = 0x013b,
223 EXIF_TAG_USER_COMMENT = 0x9286,
224 EXIF_TAG_IMAGE_DESCRIPTION = 0x010e,
225 EXIF_TAG_DATE_TIME = 0x0132,
226 EXIF_TAG_DATE_TIME_ORIGINAL = 0x9003,
227 EXIF_TAG_DATE_TIME_DIGITIZED = 0x9004,
228 EXIF_TAG_EXIF_IFD_POINTER = 0x8769
240 * Read IFD from stream.
243 _exif_ifd_get(int fd, int little_endian, struct exif_ifd *ifd)
245 unsigned char buf[12];
247 if (read(fd, buf, 12) != 12) {
253 ifd->tag = LE_2BYTE(buf);
254 ifd->type = LE_2BYTE(buf + 2);
255 ifd->count = LE_4BYTE(buf + 4);
256 ifd->offset = LE_4BYTE(buf + 8);
258 ifd->tag = BE_2BYTE(buf);
259 ifd->type = BE_2BYTE(buf + 2);
260 ifd->count = BE_4BYTE(buf + 4);
261 ifd->offset = BE_4BYTE(buf + 8);
267 * Get non-exif data based on Exif tag offset.
269 * This will setup the file description position and call _jpeg_info_get().
272 _exif_extra_get(int fd, int abs_offset, int len, struct lms_image_info *info)
274 if (lseek(fd, abs_offset, SEEK_SET) == -1) {
279 if (_jpeg_info_get(fd, len, info) != 0) {
280 fprintf(stderr, "ERROR: could not get image size.\n");
287 _exif_text_encoding_get(int fd, unsigned int count, int offset, struct lms_string_size *s)
292 count -= 8; /* XXX don't just ignore character code, handle it. */
295 if (lseek(fd, offset, SEEK_SET) == -1) {
300 s->str = malloc(count + 1);
302 if (read(fd, s->str, count) != count) {
309 s->str[count] = '\0';
312 lms_string_size_strip_and_free(s);
318 _exif_text_ascii_get(int fd, unsigned int count, int offset, struct lms_string_size *s)
326 if (lseek(fd, offset, SEEK_SET) == -1) {
331 s->str = malloc(count);
333 if (read(fd, s->str, count) != count) {
340 s->str[count - 1] = '\0';
343 lms_string_size_strip_and_free(s);
349 _exif_datetime_get(int fd, int offset)
354 if (lseek(fd, offset, SEEK_SET) == -1) {
359 if (read(fd, buf, 20) != 20) {
365 if (strptime(buf, "%Y:%m:%d %H:%M:%S", &tm)) {
371 static int _exif_private_ifd_get(int fd, int base_offset, int offset, int little_endian, struct lms_image_info *info);
374 * Process IFD contents.
377 _exif_ifd_process(int fd, int count, int ifd_offset, int tiff_base, int little_endian, struct lms_image_info *info)
379 int i, torig, tdig, tlast;
381 torig = tdig = tlast = 0;
383 for (i = 0; i < count; i++) {
386 lseek(fd, tiff_base + ifd_offset + i * 12, SEEK_SET);
387 if (_exif_ifd_get(fd, little_endian, &ifd) != 0) {
388 fprintf(stderr, "ERROR: could not read Exif IFD.\n");
393 case EXIF_TAG_ORIENTATION:
394 info->orientation = ifd.offset >> 16;
396 case EXIF_TAG_ARTIST:
397 if (!info->artist.str)
398 _exif_text_ascii_get(fd, ifd.count, tiff_base + ifd.offset,
401 case EXIF_TAG_USER_COMMENT:
402 if (!info->title.str)
403 _exif_text_encoding_get(fd, ifd.count, tiff_base + ifd.offset,
406 case EXIF_TAG_IMAGE_DESCRIPTION:
407 if (!info->title.str)
408 _exif_text_ascii_get(fd, ifd.count, tiff_base + ifd.offset,
411 case EXIF_TAG_DATE_TIME:
412 if (torig == 0 && info->date == 0)
413 tlast = _exif_datetime_get(fd, tiff_base + ifd.offset);
415 case EXIF_TAG_DATE_TIME_ORIGINAL:
416 if (torig == 0 && info->date == 0)
417 torig = _exif_datetime_get(fd, tiff_base + ifd.offset);
419 case EXIF_TAG_DATE_TIME_DIGITIZED:
420 if (torig == 0 && info->date == 0)
421 tdig = _exif_datetime_get(fd, tiff_base + ifd.offset);
423 case EXIF_TAG_EXIF_IFD_POINTER:
424 if (ifd.count == 1 && ifd.type == EXIF_TYPE_LONG)
425 _exif_private_ifd_get(fd, ifd.offset, tiff_base,
426 little_endian, info);
434 if (info->date == 0) {
447 * Process Exif IFD (Exif Private Tag), with more specific info.
450 _exif_private_ifd_get(int fd, int ifd_offset, int tiff_base, int little_endian, struct lms_image_info *info)
455 if (lseek(fd, tiff_base + ifd_offset, SEEK_SET) == -1) {
460 if (read(fd, buf, 2) != 2) {
465 count = E_2BTYE(little_endian, buf);
466 return _exif_ifd_process(fd, count, ifd_offset + 2, tiff_base,
467 little_endian, info);
471 * Process file as it being Exif, will extract Exif as well as other
472 * JPEG markers (comment, size).
475 _exif_data_get(int fd, int len, struct lms_image_info *info)
477 const unsigned char exif_hdr[6] = "Exif\0";
478 unsigned char buf[8];
479 unsigned int little_endian, offset, count;
480 off_t abs_offset, tiff_base;
482 abs_offset = lseek(fd, 0, SEEK_CUR);
483 if (abs_offset == -1) {
488 if (read(fd, buf, 6) != 6) {
493 memset(info, 0, sizeof(*info));
494 info->orientation = 1;
496 if (memcmp(buf, exif_hdr, 6) != 0)
497 return _exif_extra_get(fd, abs_offset, len, info);
499 if (read(fd, buf, 8) != 8) {
504 if (buf[0] == 'I' && buf[1] == 'I') {
506 offset = LE_4BYTE(buf + 4);
507 } else if (buf[0] == 'M' && buf[1] == 'M') {
509 offset = BE_4BYTE(buf + 4);
511 fprintf(stderr, "ERROR: undefined byte sex \"%2.2s\".\n", buf);
516 if (offset > 0 && lseek(fd, offset, SEEK_CUR) == -1) {
521 tiff_base = abs_offset + 6; /* offsets are relative to TIFF base */
523 if (read(fd, buf, 2) != 2) {
527 count = E_2BTYE(little_endian, buf);
529 _exif_ifd_process(fd, count, 8 + 2, tiff_base,
530 little_endian, info);
532 return _exif_extra_get(fd, abs_offset, len, info);
536 * Process file as it being JFIF
539 _jfif_data_get(int fd, int len, struct lms_image_info *info)
541 unsigned char buf[4];
544 memset(info, 0, sizeof(*info));
545 info->orientation = 1;
547 /* JFIF provides no useful information, try to find out Exif */
548 if (lseek(fd, len - 2, SEEK_CUR) == -1) {
553 if (read(fd, buf, 4) != 4) {
558 new_len = ((buf[2] << 8) | buf[3]);
559 if (buf[0] != 0xff) {
560 fprintf(stderr, "ERROR: expected 0xff marker, got %#x\n", buf[0]);
564 if (buf[1] == JPEG_MARKER_EXIF)
565 return _exif_data_get(fd, new_len, info);
567 /* rollback to avoid losing initial frame */
568 if (lseek(fd, - len - 2, SEEK_CUR) == -1) {
572 return _jpeg_info_get(fd, len, info);
576 static const char _name[] = "jpeg";
577 static const struct lms_string_size _exts[] = {
578 LMS_STATIC_STRING_SIZE(".jpg"),
579 LMS_STATIC_STRING_SIZE(".jpeg"),
580 LMS_STATIC_STRING_SIZE(".jpe")
584 struct lms_plugin plugin;
585 lms_db_image_t *img_db;
589 _match(struct plugin *p, const char *path, int len, int base)
593 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
597 return (void*)(i + 1);
601 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
603 struct lms_image_info info = {0};
604 int fd, type, len, r;
606 fd = open(finfo->path, O_RDONLY);
612 if (_jpeg_data_get(fd, &type, &len) != 0) {
617 if (type == JPEG_MARKER_EXIF) {
618 if (_exif_data_get(fd, len, &info) != 0) {
619 fprintf(stderr, "ERROR: could not get EXIF info (%s).\n",
624 } else if (type == JPEG_MARKER_JFIF || type == JPEG_MARKER_DQT) {
625 if (_jfif_data_get(fd, len, &info) != 0) {
626 fprintf(stderr, "ERROR: could not get JPEG size (%s).\n",
632 fprintf(stderr, "ERROR: unsupported JPEG marker %#x (%s)\n", type,
639 info.date = finfo->mtime;
641 if (!info.title.str) {
644 ext_idx = ((int)match) - 1;
645 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
646 info.title.str = malloc((info.title.len + 1) * sizeof(char));
647 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
648 info.title.str[info.title.len] = '\0';
652 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
654 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
657 r = lms_db_image_add(plugin->img_db, &info);
661 free(info.title.str);
663 free(info.artist.str);
665 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
672 _setup(struct plugin *plugin, struct lms_context *ctxt)
674 plugin->img_db = lms_db_image_new(ctxt->db);
682 _start(struct plugin *plugin, struct lms_context *ctxt)
684 return lms_db_image_start(plugin->img_db);
688 _finish(struct plugin *plugin, struct lms_context *ctxt)
691 return lms_db_image_free(plugin->img_db);
698 _close(struct plugin *plugin)
704 API struct lms_plugin *
705 lms_plugin_open(void)
707 struct plugin *plugin;
709 plugin = malloc(sizeof(*plugin));
710 plugin->plugin.name = _name;
711 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
712 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
713 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
714 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
715 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
716 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
718 return (struct lms_plugin *)plugin;