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 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
49 unsigned ff_tget_long(GetByteContext *gb, int le)
51 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
55 double ff_tget_double(GetByteContext *gb, int le)
57 av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
62 unsigned ff_tget(GetByteContext *gb, int type, int le)
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;
72 static const char *auto_sep(int count, const char *sep, int i, int columns)
79 return columns < count ? "\n" : "";
82 int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
83 GetByteContext *gb, int le, AVDictionary **metadata)
90 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
91 return AVERROR_INVALIDDATA;
92 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
93 return AVERROR_INVALIDDATA;
95 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
97 for (i = 0; i < count; i++) {
98 nom = ff_tget_long(gb, le);
99 denom = ff_tget_long(gb, le);
100 av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom);
103 if ((i = av_bprint_finalize(&bp, &ap))) {
107 return AVERROR(ENOMEM);
110 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
116 int ff_tadd_long_metadata(int count, const char *name, const char *sep,
117 GetByteContext *gb, int le, AVDictionary **metadata)
123 if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
124 return AVERROR_INVALIDDATA;
125 if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
126 return AVERROR_INVALIDDATA;
128 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
130 for (i = 0; i < count; i++) {
131 av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
134 if ((i = av_bprint_finalize(&bp, &ap))) {
138 return AVERROR(ENOMEM);
141 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
147 int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
148 GetByteContext *gb, int le, AVDictionary **metadata)
154 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
155 return AVERROR_INVALIDDATA;
156 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
157 return AVERROR_INVALIDDATA;
159 av_bprint_init(&bp, 10 * count, 100 * count);
161 for (i = 0; i < count; i++) {
162 av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
165 if ((i = av_bprint_finalize(&bp, &ap))) {
169 return AVERROR(ENOMEM);
172 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
178 int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
179 GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
185 if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
186 return AVERROR_INVALIDDATA;
187 if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
188 return AVERROR_INVALIDDATA;
190 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
192 for (i = 0; i < count; i++) {
193 int v = is_signed ? (int16_t)ff_tget_short(gb, le) : ff_tget_short(gb, le);
194 av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
197 if ((i = av_bprint_finalize(&bp, &ap))) {
201 return AVERROR(ENOMEM);
204 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
210 int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
211 GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
217 if (count >= INT_MAX / sizeof(int8_t) || count < 0)
218 return AVERROR_INVALIDDATA;
219 if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
220 return AVERROR_INVALIDDATA;
222 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
224 for (i = 0; i < count; i++) {
225 int v = is_signed ? (int8_t)bytestream2_get_byte(gb) : bytestream2_get_byte(gb);
226 av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v);
229 if ((i = av_bprint_finalize(&bp, &ap))) {
233 return AVERROR(ENOMEM);
236 av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
241 int ff_tadd_string_metadata(int count, const char *name,
242 GetByteContext *gb, int le, AVDictionary **metadata)
246 if (bytestream2_get_bytes_left(gb) < count || count < 0)
247 return AVERROR_INVALIDDATA;
249 value = av_malloc(count + 1);
251 return AVERROR(ENOMEM);
253 bytestream2_get_bufferu(gb, value, count);
256 av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
261 int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
263 if (bytestream2_get_bytes_left(gb) < 8) {
264 return AVERROR_INVALIDDATA;
267 *le = bytestream2_get_le16u(gb);
268 if (*le == AV_RB16("II")) {
270 } else if (*le == AV_RB16("MM")) {
273 return AVERROR_INVALIDDATA;
276 if (ff_tget_short(gb, *le) != 42) {
277 return AVERROR_INVALIDDATA;
280 *ifd_offset = ff_tget_long(gb, *le);
286 int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
287 unsigned *count, int *next)
292 *tag = ff_tget_short(gb, le);
293 *type = ff_tget_short(gb, le);
294 *count = ff_tget_long (gb, le);
296 ifd_tag = ff_tis_ifd(*tag);
297 valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
299 *next = bytestream2_tell(gb) + 4;
301 // check for valid type
303 return AVERROR_INVALIDDATA;
306 // seek to offset if this is an IFD-tag or
307 // if count values do not fit into the offset value
308 if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
309 bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);