FFmpeg
exif.h
Go to the documentation of this file.
1 /*
2  * EXIF metadata parser
3  * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>
4  * Copyright (c) 2024-2025 Leo Izen <leo.izen@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * EXIF metadata parser
26  * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
27  * @author Leo Izen <leo.izen@gmail.com>
28  */
29 
30 #ifndef AVCODEC_EXIF_H
31 #define AVCODEC_EXIF_H
32 
33 #include <stddef.h>
34 #include <stdint.h>
35 
36 #include "libavutil/buffer.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/rational.h"
39 
40 /** Data type identifiers for TIFF tags */
55 };
56 
58  /**
59  * The TIFF header starts with 0x49492a00, or 0x4d4d002a.
60  * This one is used internally by FFmpeg.
61  */
63  /** skip the TIFF header, assume little endian */
65  /** skip the TIFF header, assume big endian */
67  /** The first four bytes point to the actual start, then it's AV_EXIF_TIFF_HEADER */
69  /** The first six bytes contain "Exif\0\0", then it's AV_EXIF_TIFF_HEADER */
71 };
72 
73 typedef struct AVExifEntry AVExifEntry;
74 
75 typedef struct AVExifMetadata {
76  /* array of EXIF metadata entries */
78  /* number of entries in this array */
79  unsigned int count;
80  /* size of the buffer, used for av_fast_realloc */
81  unsigned int size;
83 
84 struct AVExifEntry {
85  uint16_t id;
87  uint32_t count;
88 
89  /*
90  * These are for IFD-style MakerNote
91  * entries which occur after a fixed
92  * offset rather than at the start of
93  * the entry. The ifd_lead field contains
94  * the leading bytes which typically
95  * identify the type of MakerNote.
96  */
97  uint32_t ifd_offset;
98  uint8_t *ifd_lead;
99 
100  /*
101  * An array of entries of size count
102  * Unless it's an IFD, in which case
103  * it's not an array and count = 1
104  */
105  union {
106  void *ptr;
108  uint64_t *uint;
109  double *dbl;
110  char *str;
111  uint8_t *ubytes;
112  int8_t *sbytes;
115  } value;
116 };
117 
118 /**
119  * Retrieves the tag name associated with the provided tag ID.
120  * If the tag ID is unknown, NULL is returned.
121  *
122  * For example, av_exif_get_tag_name(0x112) returns "Orientation".
123  */
124 const char *av_exif_get_tag_name(uint16_t id);
125 
126 /**
127  * Retrieves the tag ID associated with the provided tag string name.
128  * If the tag name is unknown, a negative number is returned. Otherwise
129  * it always fits inside a uint16_t integer.
130  *
131  * For example, av_exif_get_tag_id("Orientation") returns 274 (0x0112).
132  */
133 int32_t av_exif_get_tag_id(const char *name);
134 
135 /**
136  * Add an entry to the provided EXIF metadata struct. If one already exists with the provided
137  * ID, it will set the existing one to have the other information provided. Otherwise, it
138  * will allocate a new entry.
139  *
140  * This function reallocates ifd->entries using av_fast_realloc and allocates (using av_malloc)
141  * a new value member of the entry, then copies the contents of value into that buffer.
142  */
143 int av_exif_set_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, enum AVTiffDataType type,
144  uint32_t count, const uint8_t *ifd_lead, uint32_t ifd_offset, const void *value);
145 
146 /**
147  * Also check subdirectories.
148  */
149 #define AV_EXIF_FLAG_RECURSIVE (1 << 0)
150 
151 /**
152  * Get an entry with the tagged ID from the EXIF metadata struct. A pointer to the entry
153  * will be written into *value.
154  *
155  * If the entry was present and returned successfully, a positive number is returned.
156  *
157  * The positive number is equal to 1 plus the offset at which the entry was found. If the
158  * entry was found at the top level, this will be in the range of 1 to the number of entries
159  * in the IFD (inclusive). If the entry was found in a sub-IFD, then it will be a number greater
160  * than the number of entries in the top-level IFD.
161  *
162  * If the entry was not found, *value is left untouched and zero is returned.
163  * If an error occurred, a negative AVERROR is returned.
164  */
165 int av_exif_get_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags, AVExifEntry **value);
166 
167 /**
168  * Remove an entry from the provided EXIF metadata struct.
169  *
170  * If the entry was present and removed successfully, a positive number is returned.
171  *
172  * The positive number is equal to 1 plus the offset at which the entry was found. If the
173  * entry was found at the top level, this will be in the range of 1 to the number of entries
174  * in the IFD (inclusive). If the entry was found in a sub-IFD, then it will be a number greater
175  * than the number of entries in the top-level IFD. This refers to the state of the IFD before
176  * the entry was removed, e.g. if the last entry is removed, a 1 is returned, but the IFD has
177  * no entries anymore.
178  *
179  * If the entry was not found, zero is returned and the IFD is left untouched.
180  * If an error occurred, a negative AVERROR is returned.
181  */
182 int av_exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags);
183 
184 /**
185  * Decodes the EXIF data provided in the buffer and writes it into the
186  * struct *ifd. If this function succeeds, the IFD is owned by the caller
187  * and must be cleared after use by calling av_exif_free(); If this function
188  * fails and returns a negative value, it will call av_exif_free(ifd) before
189  * returning.
190  */
191 int av_exif_parse_buffer(void *logctx, const uint8_t *data, size_t size,
192  AVExifMetadata *ifd, enum AVExifHeaderMode header_mode);
193 
194 /**
195  * Allocates a buffer using av_malloc of an appropriate size and writes the
196  * EXIF data represented by ifd into that buffer.
197  *
198  * Upon error, *buffer will be NULL. The buffer becomes owned by the caller upon
199  * success. The *buffer argument must be NULL before calling.
200  */
201 int av_exif_write(void *logctx, const AVExifMetadata *ifd, AVBufferRef **buffer, enum AVExifHeaderMode header_mode);
202 
203 /**
204  * Frees all resources associated with the given EXIF metadata struct.
205  * Does not free the pointer passed itself, in case it is stack-allocated.
206  * The pointer passed to this function must be freed by the caller,
207  * if it is heap-allocated. Passing NULL is permitted.
208  */
209 void av_exif_free(AVExifMetadata *ifd);
210 
211 /**
212  * Recursively reads all tags from the IFD and stores them in the
213  * provided metadata dictionary.
214  */
215 int av_exif_ifd_to_dict(void *logctx, const AVExifMetadata *ifd, AVDictionary **metadata);
216 
217 /**
218  * Allocates a duplicate of the provided EXIF metadata struct. The caller owns
219  * the duplicate and must free it with av_exif_free. Returns NULL if the duplication
220  * process failed.
221  */
223 
224 /**
225  * Convert a display matrix used by AV_FRAME_DATA_DISPLAYMATRIX
226  * into an orientation constant used by EXIF's orientation tag.
227  *
228  * Returns an EXIF orientation between 1 and 8 (inclusive) depending
229  * on the rotation and flip factors. Returns 0 if the matrix is singular.
230  */
232 
233 /**
234  * Convert an orientation constant used by EXIF's orientation tag
235  * into a display matrix used by AV_FRAME_DATA_DISPLAYMATRIX.
236  *
237  * Returns 0 on success and negative if the orientation is invalid,
238  * i.e. not between 1 and 8 (inclusive).
239  */
240 int av_exif_orientation_to_matrix(int32_t *matrix, int orientation);
241 
242 #endif /* AVCODEC_EXIF_H */
flags
const SwsFlags flags[]
Definition: swscale.c:85
AV_EXIF_T_OFF
@ AV_EXIF_T_OFF
The first four bytes point to the actual start, then it's AV_EXIF_TIFF_HEADER.
Definition: exif.h:68
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
av_exif_get_entry
int av_exif_get_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags, AVExifEntry **value)
Get an entry with the tagged ID from the EXIF metadata struct.
Definition: exif.c:1190
av_exif_get_tag_name
const char * av_exif_get_tag_name(uint16_t id)
Retrieves the tag name associated with the provided tag ID.
Definition: exif.c:234
AVExifEntry
Definition: exif.h:84
AVExifMetadata
Definition: exif.h:75
AVExifEntry::sbytes
int8_t * sbytes
Definition: exif.h:112
matrix
Definition: vc1dsp.c:43
av_exif_parse_buffer
int av_exif_parse_buffer(void *logctx, const uint8_t *data, size_t size, AVExifMetadata *ifd, enum AVExifHeaderMode header_mode)
Decodes the EXIF data provided in the buffer and writes it into the struct *ifd.
Definition: exif.c:882
rational.h
int64_t
long long int64_t
Definition: coverity.c:34
AVExifHeaderMode
AVExifHeaderMode
Definition: exif.h:57
av_exif_set_entry
int av_exif_set_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, enum AVTiffDataType type, uint32_t count, const uint8_t *ifd_lead, uint32_t ifd_offset, const void *value)
Add an entry to the provided EXIF metadata struct.
Definition: exif.c:1195
data
const char data[16]
Definition: mxf.c:149
AVDictionary
Definition: dict.c:32
AVExifEntry::ifd_offset
uint32_t ifd_offset
Definition: exif.h:97
AV_TIFF_SHORT
@ AV_TIFF_SHORT
Definition: exif.h:44
AVExifEntry::dbl
double * dbl
Definition: exif.h:109
AV_TIFF_UNDEFINED
@ AV_TIFF_UNDEFINED
Definition: exif.h:48
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
av_exif_free
void av_exif_free(AVExifMetadata *ifd)
Frees all resources associated with the given EXIF metadata struct.
Definition: exif.c:659
av_exif_ifd_to_dict
int av_exif_ifd_to_dict(void *logctx, const AVExifMetadata *ifd, AVDictionary **metadata)
Recursively reads all tags from the IFD and stores them in the provided metadata dictionary.
Definition: exif.c:1053
AV_TIFF_IFD
@ AV_TIFF_IFD
Definition: exif.h:54
AV_TIFF_RATIONAL
@ AV_TIFF_RATIONAL
Definition: exif.h:46
AV_EXIF_EXIF00
@ AV_EXIF_EXIF00
The first six bytes contain "Exif\0\0", then it's AV_EXIF_TIFF_HEADER.
Definition: exif.h:70
AV_EXIF_ASSUME_BE
@ AV_EXIF_ASSUME_BE
skip the TIFF header, assume big endian
Definition: exif.h:66
AVExifEntry::ifd_lead
uint8_t * ifd_lead
Definition: exif.h:98
metadata
Stream codec metadata
Definition: ogg-flac-chained-meta.txt:2
AV_EXIF_TIFF_HEADER
@ AV_EXIF_TIFF_HEADER
The TIFF header starts with 0x49492a00, or 0x4d4d002a.
Definition: exif.h:62
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_exif_remove_entry
int av_exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags)
Remove an entry from the provided EXIF metadata struct.
Definition: exif.c:1289
av_exif_write
int av_exif_write(void *logctx, const AVExifMetadata *ifd, AVBufferRef **buffer, enum AVExifHeaderMode header_mode)
Allocates a buffer using av_malloc of an appropriate size and writes the EXIF data represented by ifd...
Definition: exif.c:753
av_exif_get_tag_id
int32_t av_exif_get_tag_id(const char *name)
Retrieves the tag ID associated with the provided tag string name.
Definition: exif.c:244
AVExifEntry::count
uint32_t count
Definition: exif.h:87
AV_EXIF_ASSUME_LE
@ AV_EXIF_ASSUME_LE
skip the TIFF header, assume little endian
Definition: exif.h:64
AVExifMetadata::size
unsigned int size
Definition: exif.h:81
av_exif_matrix_to_orientation
int av_exif_matrix_to_orientation(const int32_t *matrix)
Convert a display matrix used by AV_FRAME_DATA_DISPLAYMATRIX into an orientation constant used by EXI...
Definition: exif.c:1330
AV_TIFF_BYTE
@ AV_TIFF_BYTE
Definition: exif.h:42
AVTiffDataType
AVTiffDataType
Data type identifiers for TIFF tags.
Definition: exif.h:41
AVExifEntry::id
uint16_t id
Definition: exif.h:85
size
int size
Definition: twinvq_data.h:10344
AVExifEntry::ptr
void * ptr
Definition: exif.h:106
av_exif_clone_ifd
AVExifMetadata * av_exif_clone_ifd(const AVExifMetadata *ifd)
Allocates a duplicate of the provided EXIF metadata struct.
Definition: exif.c:1294
buffer.h
AV_TIFF_STRING
@ AV_TIFF_STRING
Definition: exif.h:43
AVExifEntry::ifd
AVExifMetadata ifd
Definition: exif.h:114
AVExifEntry::value
union AVExifEntry::@127 value
AV_TIFF_SSHORT
@ AV_TIFF_SSHORT
Definition: exif.h:49
AV_TIFF_SRATIONAL
@ AV_TIFF_SRATIONAL
Definition: exif.h:51
AVExifMetadata::entries
AVExifEntry * entries
Definition: exif.h:77
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVExifMetadata::count
unsigned int count
Definition: exif.h:79
AVExifEntry::uint
uint64_t * uint
Definition: exif.h:108
AV_TIFF_SLONG
@ AV_TIFF_SLONG
Definition: exif.h:50
AV_TIFF_SBYTE
@ AV_TIFF_SBYTE
Definition: exif.h:47
dict.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVExifEntry::sint
int64_t * sint
Definition: exif.h:107
AVExifEntry::type
enum AVTiffDataType type
Definition: exif.h:86
AV_TIFF_DOUBLE
@ AV_TIFF_DOUBLE
Definition: exif.h:53
av_exif_orientation_to_matrix
int av_exif_orientation_to_matrix(int32_t *matrix, int orientation)
Convert an orientation constant used by EXIF's orientation tag into a display matrix used by AV_FRAME...
Definition: exif.c:1343
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVExifEntry::ubytes
uint8_t * ubytes
Definition: exif.h:111
AV_TIFF_FLOAT
@ AV_TIFF_FLOAT
Definition: exif.h:52
AVExifEntry::rat
AVRational * rat
Definition: exif.h:113
int32_t
int32_t
Definition: audioconvert.c:56
AVExifEntry::str
char * str
Definition: exif.h:110
AV_TIFF_LONG
@ AV_TIFF_LONG
Definition: exif.h:45