FFmpeg  2.1.1
exif.c
Go to the documentation of this file.
1 /*
2  * EXIF metadata parser
3  * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * EXIF metadata parser
25  * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
26  */
27 
28 #include "exif.h"
29 
30 
31 static const char *exif_get_tag_name(uint16_t id)
32 {
33  int i;
34 
35  for (i = 0; i < FF_ARRAY_ELEMS(tag_list); i++) {
36  if (tag_list[i].id == id)
37  return tag_list[i].name;
38  }
39 
40  return NULL;
41 }
42 
43 
44 static int exif_add_metadata(AVCodecContext *avctx, int count, int type,
45  const char *name, const char *sep,
46  GetByteContext *gb, int le,
47  AVDictionary **metadata)
48 {
49  switch(type) {
50  case TIFF_DOUBLE : return ff_tadd_doubles_metadata(count, name, sep, gb, le, metadata);
51  case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, gb, le, metadata);
52  case TIFF_BYTE :
53  case TIFF_UNDEFINED: return ff_tadd_bytes_metadata(count, name, sep, gb, le, metadata);
54  case TIFF_STRING : return ff_tadd_string_metadata(count, name, gb, le, metadata);
55  case TIFF_SRATIONAL:
56  case TIFF_RATIONAL : return ff_tadd_rational_metadata(count, name, sep, gb, le, metadata);
57  case TIFF_SLONG :
58  case TIFF_LONG : return ff_tadd_long_metadata(count, name, sep, gb, le, metadata);
59  default:
60  avpriv_request_sample(avctx, "TIFF tag type (%u)", type);
61  return 0;
62  };
63 }
64 
65 
66 static int exif_decode_tag(AVCodecContext *avctx, GetByteContext *gbytes, int le,
67  int depth, AVDictionary **metadata)
68 {
69  int ret, cur_pos;
70  unsigned id, count;
71  enum TiffTypes type;
72 
73  if (depth > 2) {
74  return 0;
75  }
76 
77  ff_tread_tag(gbytes, le, &id, &type, &count, &cur_pos);
78 
79  // read count values and add it metadata
80  // store metadata or proceed with next IFD
81  ret = ff_tis_ifd(id);
82  if (ret) {
83  ret = ff_exif_decode_ifd(avctx, gbytes, le, depth + 1, metadata);
84  } else {
85  const char *name = exif_get_tag_name(id);
86  char *use_name = (char*) name;
87 
88  if (!use_name) {
89  use_name = av_malloc(7);
90  if (!use_name) {
91  return AVERROR(ENOMEM);
92  }
93  snprintf(use_name, 7, "0x%04X", id);
94  }
95 
96  ret = exif_add_metadata(avctx, count, type, use_name, NULL,
97  gbytes, le, metadata);
98 
99  if (!name) {
100  av_freep(&use_name);
101  }
102  }
103 
104  bytestream2_seek(gbytes, cur_pos, SEEK_SET);
105 
106  return ret;
107 }
108 
109 
111  int depth, AVDictionary **metadata)
112 {
113  int i, ret;
114  int entries;
115 
116  entries = ff_tget_short(gbytes, le);
117 
118  if (bytestream2_get_bytes_left(gbytes) < entries * 12) {
119  return AVERROR_INVALIDDATA;
120  }
121 
122  for (i = 0; i < entries; i++) {
123  if ((ret = exif_decode_tag(avctx, gbytes, le, depth, metadata)) < 0) {
124  return ret;
125  }
126  }
127 
128  // return next IDF offset or 0x000000000 or a value < 0 for failure
129  return ff_tget_long(gbytes, le);
130 }
const char * name
Definition: avisynth_c.h:675
enum AVCodecID id
Definition: mxfenc.c:90
int ff_tadd_doubles_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count doubles converted to a string into the metadata dictionary.
Definition: tiff_common.c:153
int ff_tadd_shorts_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count shorts converted to a string into the metadata dictionary.
Definition: tiff_common.c:184
int ff_tadd_rational_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count rationals converted to a string into the metadata dictionary.
Definition: tiff_common.c:88
EXIF metadata parser.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static struct exif_tag tag_list[]
Definition: exif.h:43
int ff_exif_decode_ifd(AVCodecContext *avctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Recursively decodes all IFD&#39;s and adds included TAGS into the metadata dictionary.
Definition: exif.c:110
static const char * exif_get_tag_name(uint16_t id)
Definition: exif.c:31
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianess.
Definition: tiff_common.c:43
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:152
int depth
Definition: v4l.c:61
#define FF_ARRAY_ELEMS(a)
Definition: avcodec.h:929
ret
Definition: avfilter.c:961
unsigned ff_tget_long(GetByteContext *gb, int le)
Reads a long from the bytestream using given endianess.
Definition: tiff_common.c:50
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
TiffTypes
data type identifiers for TIFF tags
Definition: tiff_common.h:37
uint8_t le
Definition: crc.c:259
int ff_tis_ifd(unsigned tag)
Returns a value &gt; 0 if the tag is a known IFD-tag.
Definition: tiff_common.c:31
main external API structure.
Definition: avcodec.h:1146
int ff_tadd_long_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count longs converted to a string into the metadata dictionary.
Definition: tiff_common.c:122
#define snprintf
Definition: snprintf.h:34
#define type
static int exif_decode_tag(AVCodecContext *avctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Definition: exif.c:66
#define AVERROR_INVALIDDATA
int ff_tadd_string_metadata(int count, const char *name, GetByteContext *gb, int le, AVDictionary **metadata)
Adds a string of count characters into the metadata dictionary.
Definition: tiff_common.c:245
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:206
#define AVERROR(e)
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, unsigned *count, int *next)
Reads the first 3 fields of a TIFF tag, which are the tag id, the tag type and the count of values fo...
Definition: tiff_common.c:290
void INT64 INT64 count
Definition: avisynth_c.h:594
static int exif_add_metadata(AVCodecContext *avctx, int count, int type, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Definition: exif.c:44
char name[EXIF_TAG_NAME_LENGTH]
Definition: exif.h:39
int ff_tadd_bytes_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count bytes converted to a string into the metadata dictionary.
Definition: tiff_common.c:215