libavcodec/exif.c
ad0f7574
 /*
  * EXIF metadata parser
6a64b23d
  * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>
ad0f7574
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * EXIF metadata parser
6a64b23d
  * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
ad0f7574
  */
 
 #include "exif.h"
 
 
 static const char *exif_get_tag_name(uint16_t id)
 {
     int i;
 
     for (i = 0; i < FF_ARRAY_ELEMS(tag_list); i++) {
         if (tag_list[i].id == id)
             return tag_list[i].name;
     }
 
     return NULL;
 }
 
 
ce87711d
 static int exif_add_metadata(void *logctx, int count, int type,
ad0f7574
                              const char *name, const char *sep,
                              GetByteContext *gb, int le,
                              AVDictionary **metadata)
 {
     switch(type) {
badcd3da
     case 0:
ce87711d
         av_log(logctx, AV_LOG_WARNING,
badcd3da
                "Invalid TIFF tag type 0 found for %s with size %d\n",
                name, count);
         return 0;
ad0f7574
     case TIFF_DOUBLE   : return ff_tadd_doubles_metadata(count, name, sep, gb, le, metadata);
a94de50b
     case TIFF_SSHORT   : return ff_tadd_shorts_metadata(count, name, sep, gb, le, 1, metadata);
     case TIFF_SHORT    : return ff_tadd_shorts_metadata(count, name, sep, gb, le, 0, metadata);
     case TIFF_SBYTE    : return ff_tadd_bytes_metadata(count, name, sep, gb, le, 1, metadata);
ad0f7574
     case TIFF_BYTE     :
a94de50b
     case TIFF_UNDEFINED: return ff_tadd_bytes_metadata(count, name, sep, gb, le, 0, metadata);
ad0f7574
     case TIFF_STRING   : return ff_tadd_string_metadata(count, name, gb, le, metadata);
     case TIFF_SRATIONAL:
     case TIFF_RATIONAL : return ff_tadd_rational_metadata(count, name, sep, gb, le, metadata);
     case TIFF_SLONG    :
     case TIFF_LONG     : return ff_tadd_long_metadata(count, name, sep, gb, le, metadata);
     default:
ce87711d
         avpriv_request_sample(logctx, "TIFF tag type (%u)", type);
ad0f7574
         return 0;
     };
 }
 
 
ce87711d
 static int exif_decode_tag(void *logctx, GetByteContext *gbytes, int le,
ad0f7574
                            int depth, AVDictionary **metadata)
 {
     int ret, cur_pos;
     unsigned id, count;
     enum TiffTypes type;
 
     if (depth > 2) {
         return 0;
     }
 
     ff_tread_tag(gbytes, le, &id, &type, &count, &cur_pos);
 
e70b9b32
     if (!bytestream2_tell(gbytes)) {
         bytestream2_seek(gbytes, cur_pos, SEEK_SET);
         return 0;
     }
 
ad0f7574
     // read count values and add it metadata
     // store metadata or proceed with next IFD
     ret = ff_tis_ifd(id);
     if (ret) {
ae100046
         ret = ff_exif_decode_ifd(logctx, gbytes, le, depth + 1, metadata);
ad0f7574
     } else {
         const char *name = exif_get_tag_name(id);
         char *use_name   = (char*) name;
 
         if (!use_name) {
             use_name = av_malloc(7);
             if (!use_name) {
                 return AVERROR(ENOMEM);
             }
             snprintf(use_name, 7, "0x%04X", id);
         }
 
ce87711d
         ret = exif_add_metadata(logctx, count, type, use_name, NULL,
ad0f7574
                                 gbytes, le, metadata);
 
         if (!name) {
             av_freep(&use_name);
         }
     }
 
     bytestream2_seek(gbytes, cur_pos, SEEK_SET);
 
     return ret;
 }
 
 
ae100046
 int ff_exif_decode_ifd(void *logctx, GetByteContext *gbytes,
                        int le, int depth, AVDictionary **metadata)
ad0f7574
 {
     int i, ret;
     int entries;
 
     entries = ff_tget_short(gbytes, le);
 
     if (bytestream2_get_bytes_left(gbytes) < entries * 12) {
         return AVERROR_INVALIDDATA;
     }
 
     for (i = 0; i < entries; i++) {
ce87711d
         if ((ret = exif_decode_tag(logctx, gbytes, le, depth, metadata)) < 0) {
ad0f7574
             return ret;
         }
     }
 
     // return next IDF offset or 0x000000000 or a value < 0 for failure
     return ff_tget_long(gbytes, le);
 }
ae100046
 
 int avpriv_exif_decode_ifd(void *logctx, const uint8_t *buf, int size,
                            int le, int depth, AVDictionary **metadata)
 {
     GetByteContext gb;
 
     bytestream2_init(&gb, buf, size);
 
     return ff_exif_decode_ifd(logctx, &gb, le, depth, metadata);
 }