FFmpeg
Loading...
Searching...
No Matches
tiff_common.c
Go to the documentation of this file.
1/*
2 * TIFF Common Routines
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 * TIFF Common Routines
25 * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
26 */
27
28#include "libavutil/bprint.h"
29#include "libavutil/mem.h"
30#include "tiff_common.h"
31
32
33int ff_tis_ifd(unsigned tag)
34{
35 int i;
36 for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
37 if (ifd_tags[i] == tag) {
38 return i + 1;
39 }
40 }
41 return 0;
42}
43
44
45unsigned ff_tget_short(GetByteContext *gb, int le)
46{
47 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
48}
49
50
51unsigned ff_tget_long(GetByteContext *gb, int le)
52{
53 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
54}
55
56
57double ff_tget_double(GetByteContext *gb, int le)
58{
59 av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
60 return i.f64;
61}
62
63
64unsigned ff_tget(GetByteContext *gb, int type, int le)
65{
66 switch (type) {
67 case AV_TIFF_BYTE: return bytestream2_get_byte(gb);
68 case AV_TIFF_SHORT: return ff_tget_short(gb, le);
69 case AV_TIFF_LONG: return ff_tget_long(gb, le);
70 default: return UINT_MAX;
71 }
72}
73
74static const char *auto_sep(int count, const char *sep, int i, int columns)
75{
76 if (sep)
77 return i ? sep : "";
78 if (i && i%columns) {
79 return ", ";
80 } else
81 return columns < count ? "\n" : "";
82}
83
84static int bprint_to_avdict(AVBPrint *bp, const char *name,
86{
87 char *ap;
88 int ret;
89
90 if (!av_bprint_is_complete(bp)) {
92 return AVERROR(ENOMEM);
93 }
94 if ((ret = av_bprint_finalize(bp, &ap)) < 0)
95 return ret;
96
98}
99
100int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
101 GetByteContext *gb, int le, AVDictionary **metadata)
102{
103 AVBPrint bp;
104 int i;
105
106 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
107 return AVERROR_INVALIDDATA;
108 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
109 return AVERROR_INVALIDDATA;
110
111 av_bprint_init(&bp, 10 * count, 100 * count);
112
113 for (i = 0; i < count; i++) {
114 av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
115 }
116
117 return bprint_to_avdict(&bp, name, metadata);
118}
119
120
121int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
122 GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
123{
124 AVBPrint bp;
125 int i;
126
127 if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
128 return AVERROR_INVALIDDATA;
129 if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
130 return AVERROR_INVALIDDATA;
131
133
134 for (i = 0; i < count; i++) {
135 int v = is_signed ? (int16_t)ff_tget_short(gb, le) : ff_tget_short(gb, le);
136 av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
137 }
138
139 return bprint_to_avdict(&bp, name, metadata);
140}
141
142int ff_tadd_string_metadata(int count, const char *name,
143 GetByteContext *gb, int le, AVDictionary **metadata)
144{
145 char *value;
146
147 if (bytestream2_get_bytes_left(gb) < count || count < 0)
148 return AVERROR_INVALIDDATA;
149
150 value = av_malloc(count + 1);
151 if (!value)
152 return AVERROR(ENOMEM);
153
154 bytestream2_get_bufferu(gb, value, count);
155 value[count] = 0;
156
158 return 0;
159}
160
161
162int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
163{
164 if (bytestream2_get_bytes_left(gb) < 8) {
165 return AVERROR_INVALIDDATA;
166 }
167
168 *le = bytestream2_get_le16u(gb);
169 if (*le == AV_RB16("II")) {
170 *le = 1;
171 } else if (*le == AV_RB16("MM")) {
172 *le = 0;
173 } else {
174 return AVERROR_INVALIDDATA;
175 }
176
177 if (ff_tget_short(gb, *le) != 42) {
178 return AVERROR_INVALIDDATA;
179 }
180
181 *ifd_offset = ff_tget_long(gb, *le);
182
183 return 0;
184}
185
186
187int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
188 unsigned *count, int *next)
189{
190 int ifd_tag;
191 int valid_type;
192
193 *tag = ff_tget_short(gb, le);
194 *type = ff_tget_short(gb, le);
195 *count = ff_tget_long (gb, le);
196
197 ifd_tag = ff_tis_ifd(*tag);
198 valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
199
200 *next = bytestream2_tell(gb) + 4;
201
202 // check for valid type
203 if (!valid_type) {
204 return AVERROR_INVALIDDATA;
205 }
206
207 // seek to offset if this is an IFD-tag or
208 // if count values do not fit into the offset value
209 if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == AV_TIFF_STRING))) {
210 bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
211 }
212
213 return 0;
214}
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:69
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
double value
Definition eval.c:102
@ AV_TIFF_STRING
Definition exif.h:43
@ AV_TIFF_SHORT
Definition exif.h:44
@ AV_TIFF_LONG
Definition exif.h:45
@ AV_TIFF_BYTE
Definition exif.h:42
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition bprint.h:218
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:235
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
cl_device_type type
#define AV_RB16(p)
Memory handling functions.
uint32_t tag
Definition movenc.c:2073
#define av_malloc(s)
Definition ops_static.c:52
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
unsigned ff_tget(GetByteContext *gb, int type, int le)
Reads a byte from the bytestream using given endianness.
Definition tiff_common.c:64
int ff_tis_ifd(unsigned tag)
Returns a value > 0 if the tag is a known IFD-tag.
Definition tiff_common.c:33
int ff_tadd_shorts_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
Adds count shorts converted to a string into the metadata dictionary.
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.
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...
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianness in *le and the offset to the ...
static const char * auto_sep(int count, const char *sep, int i, int columns)
Definition tiff_common.c:74
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianness.
Definition tiff_common.c:45
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.
double ff_tget_double(GetByteContext *gb, int le)
Reads a double from the bytestream using given endianness.
Definition tiff_common.c:57
static int bprint_to_avdict(AVBPrint *bp, const char *name, AVDictionary **metadata)
Definition tiff_common.c:84
unsigned ff_tget_long(GetByteContext *gb, int le)
Reads a long from the bytestream using given endianness.
Definition tiff_common.c:51
TIFF Common Routines.
static const uint16_t ifd_tags[]
Definition tiff_common.h:41
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition tiff_common.h:37