Path: blob/master/dep/ffmpeg/include/libavcodec/exif.h
10618 views
/*1* EXIF metadata parser2* Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>3* Copyright (c) 2024-2025 Leo Izen <[email protected]>4*5* This file is part of FFmpeg.6*7* FFmpeg is free software; you can redistribute it and/or8* modify it under the terms of the GNU Lesser General Public9* License as published by the Free Software Foundation; either10* 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 of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15* Lesser General Public License for more details.16*17* You should have received a copy of the GNU Lesser General Public18* License along with FFmpeg; if not, write to the Free Software19* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA20*/2122/**23* @file24* EXIF metadata parser25* @author Thilo Borgmann <thilo.borgmann _at_ mail.de>26* @author Leo Izen <[email protected]>27*/2829#ifndef AVCODEC_EXIF_H30#define AVCODEC_EXIF_H3132#include <stddef.h>33#include <stdint.h>3435#include "libavutil/buffer.h"36#include "libavutil/dict.h"37#include "libavutil/rational.h"38#include "version_major.h"3940/** Data type identifiers for TIFF tags */41enum AVTiffDataType {42AV_TIFF_BYTE = 1,43AV_TIFF_STRING,44AV_TIFF_SHORT,45AV_TIFF_LONG,46AV_TIFF_RATIONAL,47AV_TIFF_SBYTE,48AV_TIFF_UNDEFINED,49AV_TIFF_SSHORT,50AV_TIFF_SLONG,51AV_TIFF_SRATIONAL,52AV_TIFF_FLOAT,53AV_TIFF_DOUBLE,54AV_TIFF_IFD,55};5657enum AVExifHeaderMode {58/**59* The TIFF header starts with 0x49492a00, or 0x4d4d002a.60* This one is used internally by FFmpeg.61*/62AV_EXIF_TIFF_HEADER,63/** skip the TIFF header, assume little endian */64AV_EXIF_ASSUME_LE,65/** skip the TIFF header, assume big endian */66AV_EXIF_ASSUME_BE,67/** The first four bytes point to the actual start, then it's AV_EXIF_TIFF_HEADER */68AV_EXIF_T_OFF,69/** The first six bytes contain "Exif\0\0", then it's AV_EXIF_TIFF_HEADER */70AV_EXIF_EXIF00,71};7273typedef struct AVExifEntry AVExifEntry;7475typedef struct AVExifMetadata {76/* array of EXIF metadata entries */77AVExifEntry *entries;78/* number of entries in this array */79unsigned int count;80/* size of the buffer, used for av_fast_realloc */81unsigned int size;82} AVExifMetadata;8384struct AVExifEntry {85uint16_t id;86enum AVTiffDataType type;87uint32_t count;8889/*90* These are for IFD-style MakerNote91* entries which occur after a fixed92* offset rather than at the start of93* the entry. The ifd_lead field contains94* the leading bytes which typically95* identify the type of MakerNote.96*/97uint32_t ifd_offset;98uint8_t *ifd_lead;99100/*101* An array of entries of size count102* Unless it's an IFD, in which case103* it's not an array and count = 1104*/105union {106void *ptr;107int64_t *sint;108uint64_t *uint;109double *dbl;110char *str;111uint8_t *ubytes;112int8_t *sbytes;113AVRational *rat;114AVExifMetadata ifd;115} value;116};117118/**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*/124const char *av_exif_get_tag_name(uint16_t id);125126/**127* Retrieves the tag ID associated with the provided tag string name.128* If the tag name is unknown, a negative number is returned. Otherwise129* it always fits inside a uint16_t integer.130*131* For example, av_exif_get_tag_id("Orientation") returns 274 (0x0112).132*/133int32_t av_exif_get_tag_id(const char *name);134135/**136* Add an entry to the provided EXIF metadata struct. If one already exists with the provided137* ID, it will set the existing one to have the other information provided. Otherwise, it138* will allocate a new entry.139*140* This function reallocates ifd->entries using av_realloc and allocates (using av_malloc)141* a new value member of the entry, then copies the contents of value into that buffer.142*/143int av_exif_set_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, enum AVTiffDataType type,144uint32_t count, const uint8_t *ifd_lead, uint32_t ifd_offset, const void *value);145146/**147* Also check subdirectories.148*/149#define AV_EXIF_FLAG_RECURSIVE (1 << 0)150151/**152* Get an entry with the tagged ID from the EXIF metadata struct. A pointer to the entry153* will be written into *value.154*155* If the entry was present and returned successfully, a positive number is returned.156* If the entry was not found, *value is left untouched and zero is returned.157* If an error occurred, a negative number is returned.158*/159int av_exif_get_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags, AVExifEntry **value);160161/**162* Remove an entry from the provided EXIF metadata struct.163*164* If the entry was present and removed successfully, a positive number is returned.165* If the entry was not found, zero is returned.166* If an error occurred, a negative number is returned.167*/168int av_exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags);169170/**171* Decodes the EXIF data provided in the buffer and writes it into the172* struct *ifd. If this function succeeds, the IFD is owned by the caller173* and must be cleared after use by calling av_exif_free(); If this function174* fails and returns a negative value, it will call av_exif_free(ifd) before175* returning.176*/177int av_exif_parse_buffer(void *logctx, const uint8_t *data, size_t size,178AVExifMetadata *ifd, enum AVExifHeaderMode header_mode);179180/**181* Allocates a buffer using av_malloc of an appropriate size and writes the182* EXIF data represented by ifd into that buffer.183*184* Upon error, *buffer will be NULL. The buffer becomes owned by the caller upon185* success. The *buffer argument must be NULL before calling.186*/187int av_exif_write(void *logctx, const AVExifMetadata *ifd, AVBufferRef **buffer, enum AVExifHeaderMode header_mode);188189/**190* Frees all resources associated with the given EXIF metadata struct.191* Does not free the pointer passed itself, in case it is stack-allocated.192* The pointer passed to this function must be freed by the caller,193* if it is heap-allocated. Passing NULL is permitted.194*/195void av_exif_free(AVExifMetadata *ifd);196197/**198* Recursively reads all tags from the IFD and stores them in the199* provided metadata dictionary.200*/201int av_exif_ifd_to_dict(void *logctx, const AVExifMetadata *ifd, AVDictionary **metadata);202203/**204* Allocates a duplicate of the provided EXIF metadata struct. The caller owns205* the duplicate and must free it with av_exif_free. Returns NULL if the duplication206* process failed.207*/208AVExifMetadata *av_exif_clone_ifd(const AVExifMetadata *ifd);209210/**211* Convert a display matrix used by AV_FRAME_DATA_DISPLAYMATRIX212* into an orientation constant used by EXIF's orientation tag.213*214* Returns an EXIF orientation between 1 and 8 (inclusive) depending215* on the rotation and flip factors. Returns 0 if the matrix is singular.216*/217int av_exif_matrix_to_orientation(const int32_t *matrix);218219/**220* Convert an orientation constant used by EXIF's orientation tag221* into a display matrix used by AV_FRAME_DATA_DISPLAYMATRIX.222*223* Returns 0 on success and negative if the orientation is invalid,224* i.e. not between 1 and 8 (inclusive).225*/226int av_exif_orientation_to_matrix(int32_t *matrix, int orientation);227228#endif /* AVCODEC_EXIF_H */229230231