FFmpeg  4.4.9
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 "tiff_common.h"
29 
30 
31 int ff_tis_ifd(unsigned tag)
32 {
33  int i;
34  for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
35  if (ifd_tags[i] == tag) {
36  return i + 1;
37  }
38  }
39  return 0;
40 }
41 
42 
43 unsigned ff_tget_short(GetByteContext *gb, int le)
44 {
45  return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
46 }
47 
48 
49 unsigned ff_tget_long(GetByteContext *gb, int le)
50 {
51  return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
52 }
53 
54 
55 double ff_tget_double(GetByteContext *gb, int le)
56 {
57  av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
58  return i.f64;
59 }
60 
61 
62 unsigned ff_tget(GetByteContext *gb, int type, int le)
63 {
64  switch (type) {
65  case TIFF_BYTE: return bytestream2_get_byte(gb);
66  case TIFF_SHORT: return ff_tget_short(gb, le);
67  case TIFF_LONG: return ff_tget_long(gb, le);
68  default: return UINT_MAX;
69  }
70 }
71 
72 static const char *auto_sep(int count, const char *sep, int i, int columns)
73 {
74  if (sep)
75  return i ? sep : "";
76  if (i && i%columns) {
77  return ", ";
78  } else
79  return columns < count ? "\n" : "";
80 }
81 
82 static int bprint_to_avdict(AVBPrint *bp, const char *name,
83  AVDictionary **metadata)
84 {
85  char *ap;
86  int ret;
87 
88  if (!av_bprint_is_complete(bp)) {
90  return AVERROR(ENOMEM);
91  }
92  if ((ret = av_bprint_finalize(bp, &ap)) < 0)
93  return ret;
94 
95  return av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
96 }
97 
98 int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
99  GetByteContext *gb, int le, AVDictionary **metadata)
100 {
101  AVBPrint bp;
102  int32_t nom, denom;
103  int i;
104 
105  if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
106  return AVERROR_INVALIDDATA;
107  if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
108  return AVERROR_INVALIDDATA;
109 
110  av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
111 
112  for (i = 0; i < count; i++) {
113  nom = ff_tget_long(gb, le);
114  denom = ff_tget_long(gb, le);
115  av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom);
116  }
117 
118  return bprint_to_avdict(&bp, name, metadata);
119 }
120 
121 
122 int ff_tadd_long_metadata(int count, const char *name, const char *sep,
123  GetByteContext *gb, int le, AVDictionary **metadata)
124 {
125  AVBPrint bp;
126  int i;
127 
128  if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
129  return AVERROR_INVALIDDATA;
130  if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
131  return AVERROR_INVALIDDATA;
132 
133  av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
134 
135  for (i = 0; i < count; i++) {
136  av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
137  }
138 
139  return bprint_to_avdict(&bp, name, metadata);
140 }
141 
142 
143 int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
144  GetByteContext *gb, int le, AVDictionary **metadata)
145 {
146  AVBPrint bp;
147  int i;
148 
149  if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
150  return AVERROR_INVALIDDATA;
151  if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
152  return AVERROR_INVALIDDATA;
153 
155 
156  for (i = 0; i < count; i++) {
157  av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
158  }
159 
160  return bprint_to_avdict(&bp, name, metadata);
161 }
162 
163 
164 int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
165  GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
166 {
167  AVBPrint bp;
168  int i;
169 
170  if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
171  return AVERROR_INVALIDDATA;
172  if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
173  return AVERROR_INVALIDDATA;
174 
176 
177  for (i = 0; i < count; i++) {
178  int v = is_signed ? (int16_t)ff_tget_short(gb, le) : ff_tget_short(gb, le);
179  av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
180  }
181 
182  return bprint_to_avdict(&bp, name, metadata);
183 }
184 
185 
186 int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
187  GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
188 {
189  AVBPrint bp;
190  int i;
191 
192  if (count >= INT_MAX / sizeof(int8_t) || count < 0)
193  return AVERROR_INVALIDDATA;
194  if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
195  return AVERROR_INVALIDDATA;
196 
197  av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
198 
199  for (i = 0; i < count; i++) {
200  int v = is_signed ? (int8_t)bytestream2_get_byte(gb) : bytestream2_get_byte(gb);
201  av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v);
202  }
203 
204  return bprint_to_avdict(&bp, name, metadata);
205 }
206 
207 int ff_tadd_string_metadata(int count, const char *name,
208  GetByteContext *gb, int le, AVDictionary **metadata)
209 {
210  char *value;
211 
212  if (bytestream2_get_bytes_left(gb) < count || count < 0)
213  return AVERROR_INVALIDDATA;
214 
215  value = av_malloc(count + 1);
216  if (!value)
217  return AVERROR(ENOMEM);
218 
219  bytestream2_get_bufferu(gb, value, count);
220  value[count] = 0;
221 
223  return 0;
224 }
225 
226 
227 int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
228 {
229  if (bytestream2_get_bytes_left(gb) < 8) {
230  return AVERROR_INVALIDDATA;
231  }
232 
233  *le = bytestream2_get_le16u(gb);
234  if (*le == AV_RB16("II")) {
235  *le = 1;
236  } else if (*le == AV_RB16("MM")) {
237  *le = 0;
238  } else {
239  return AVERROR_INVALIDDATA;
240  }
241 
242  if (ff_tget_short(gb, *le) != 42) {
243  return AVERROR_INVALIDDATA;
244  }
245 
246  *ifd_offset = ff_tget_long(gb, *le);
247 
248  return 0;
249 }
250 
251 
252 int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
253  unsigned *count, int *next)
254 {
255  int ifd_tag;
256  int valid_type;
257 
258  *tag = ff_tget_short(gb, le);
259  *type = ff_tget_short(gb, le);
260  *count = ff_tget_long (gb, le);
261 
262  ifd_tag = ff_tis_ifd(*tag);
263  valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
264 
265  *next = bytestream2_tell(gb) + 4;
266 
267  // check for valid type
268  if (!valid_type) {
269  return AVERROR_INVALIDDATA;
270  }
271 
272  // seek to offset if this is an IFD-tag or
273  // if count values do not fit into the offset value
274  if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
275  bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
276  }
277 
278  return 0;
279 }
int32_t
#define AV_RB16
Definition: intreadwrite.h:53
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:245
#define AV_BPRINT_SIZE_UNLIMITED
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
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(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(GetByteContext *g)
Definition: bytestream.h:192
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
double value
Definition: eval.c:100
#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:74
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:70
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
cl_device_type type
int i
Definition: input.c:407
uint32_t tag
Definition: movenc.c:1611
const char * name
Definition: qsvenc.c:46
#define FF_ARRAY_ELEMS(a)
#define av_malloc(s)
static const char * auto_sep(int count, const char *sep, int i, int columns)
Definition: tiff_common.c:72
unsigned ff_tget(GetByteContext *gb, int type, int le)
Reads a byte from the bytestream using given endianness.
Definition: tiff_common.c:62
int ff_tadd_bytes_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
Adds count bytes converted to a string into the metadata dictionary.
Definition: tiff_common.c:186
int ff_tis_ifd(unsigned tag)
Returns a value > 0 if the tag is a known IFD-tag.
Definition: tiff_common.c:31
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.
Definition: tiff_common.c:164
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:207
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:252
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 ...
Definition: tiff_common.c:227
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianness.
Definition: tiff_common.c:43
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:143
double ff_tget_double(GetByteContext *gb, int le)
Reads a double from the bytestream using given endianness.
Definition: tiff_common.c:55
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
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:98
static int bprint_to_avdict(AVBPrint *bp, const char *name, AVDictionary **metadata)
Definition: tiff_common.c:82
unsigned ff_tget_long(GetByteContext *gb, int le)
Reads a long from the bytestream using given endianness.
Definition: tiff_common.c:49
TIFF Common Routines.
@ TIFF_BYTE
Definition: tiff_common.h:38
@ TIFF_SHORT
Definition: tiff_common.h:40
@ TIFF_LONG
Definition: tiff_common.h:41
@ TIFF_STRING
Definition: tiff_common.h:39
static const uint16_t ifd_tags[]
Definition: tiff_common.h:58
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
Definition: tiff_common.h:54