3 * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>
5 * This file is part of FFmpeg.
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.
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.
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
24 * TIFF Common Routines
25 * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
28 #include "tiff_common.h"
31 int ff_tis_ifd(unsigned tag)
34 for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
35 if (ifd_tags[i] == tag) {
43 unsigned ff_tget_short(GetByteContext *gb, int le)
45 unsigned v = le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
50 unsigned ff_tget_long(GetByteContext *gb, int le)
52 unsigned v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
57 double ff_tget_double(GetByteContext *gb, int le)
59 av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
64 unsigned ff_tget(GetByteContext *gb, int type, int le)
68 return bytestream2_get_byte(gb);
70 return ff_tget_short(gb, le);
72 return ff_tget_long(gb, le);
78 static const char *auto_sep(int count, const char *sep, int i, int columns)
85 return columns < count ? "\n" : "";
88 int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
89 GetByteContext *gb, int le, AVDictionary **metadata)
96 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
97 return AVERROR_INVALIDDATA;
98 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
99 return AVERROR_INVALIDDATA;
101 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
103 for (i = 0; i < count; i++) {
104 nom = ff_tget_long(gb, le);
105 denom = ff_tget_long(gb, le);
106 av_bprintf(&bp, "%s%7i:%-7i", auto_sep(count, sep, i, 4), nom, denom);
109 if ((i = av_bprint_finalize(&bp, &ap))) {
113 return AVERROR(ENOMEM);
116 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
122 int ff_tadd_long_metadata(int count, const char *name, const char *sep,
123 GetByteContext *gb, int le, AVDictionary **metadata)
129 if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
130 return AVERROR_INVALIDDATA;
131 if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
132 return AVERROR_INVALIDDATA;
134 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
136 for (i = 0; i < count; i++) {
137 av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
140 if ((i = av_bprint_finalize(&bp, &ap))) {
144 return AVERROR(ENOMEM);
147 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
153 int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
154 GetByteContext *gb, int le, AVDictionary **metadata)
160 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
161 return AVERROR_INVALIDDATA;
162 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
163 return AVERROR_INVALIDDATA;
165 av_bprint_init(&bp, 10 * count, 100 * count);
167 for (i = 0; i < count; i++) {
168 av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
171 if ((i = av_bprint_finalize(&bp, &ap))) {
175 return AVERROR(ENOMEM);
178 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
184 int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
185 GetByteContext *gb, int le, AVDictionary **metadata)
191 if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
192 return AVERROR_INVALIDDATA;
193 if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
194 return AVERROR_INVALIDDATA;
196 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
198 for (i = 0; i < count; i++) {
199 av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), ff_tget_short(gb, le));
202 if ((i = av_bprint_finalize(&bp, &ap))) {
206 return AVERROR(ENOMEM);
209 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
215 int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
216 GetByteContext *gb, int le, AVDictionary **metadata)
222 if (count >= INT_MAX / sizeof(int8_t) || count < 0)
223 return AVERROR_INVALIDDATA;
224 if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
225 return AVERROR_INVALIDDATA;
227 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
229 for (i = 0; i < count; i++) {
230 av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), bytestream2_get_byte(gb));
233 if ((i = av_bprint_finalize(&bp, &ap))) {
237 return AVERROR(ENOMEM);
240 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
245 int ff_tadd_string_metadata(int count, const char *name,
246 GetByteContext *gb, int le, AVDictionary **metadata)
250 if (bytestream2_get_bytes_left(gb) < count || count < 0)
251 return AVERROR_INVALIDDATA;
253 value = av_malloc(count + 1);
255 return AVERROR(ENOMEM);
257 bytestream2_get_bufferu(gb, value, count);
260 av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
265 int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
267 if (bytestream2_get_bytes_left(gb) < 8) {
268 return AVERROR_INVALIDDATA;
271 *le = bytestream2_get_le16u(gb);
272 if (*le == AV_RB16("II")) {
274 } else if (*le == AV_RB16("MM")) {
277 return AVERROR_INVALIDDATA;
280 if (ff_tget_short(gb, *le) != 42) {
281 return AVERROR_INVALIDDATA;
284 *ifd_offset = ff_tget_long(gb, *le);
290 int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
291 unsigned *count, int *next)
296 *tag = ff_tget_short(gb, le);
297 *type = ff_tget_short(gb, le);
298 *count = ff_tget_long (gb, le);
300 ifd_tag = ff_tis_ifd(*tag);
301 valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
303 *next = bytestream2_tell(gb) + 4;
305 // check for valid type
307 return AVERROR_INVALIDDATA;
310 // seek to offset if this is an IFD-tag or
311 // if count values do not fit into the offset value
312 if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
313 bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);