]> git.sesse.net Git - ffmpeg/blob - libavcodec/tiff_common.c
Merge commit 'ac9d159015a88aa2721b271875d18482f713f354'
[ffmpeg] / libavcodec / tiff_common.c
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     unsigned v = le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
46     return v;
47 }
48
49
50 unsigned ff_tget_long(GetByteContext *gb, int le)
51 {
52     unsigned v = le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
53     return v;
54 }
55
56
57 double 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
64 unsigned ff_tget(GetByteContext *gb, int type, int le)
65 {
66     switch (type) {
67     case TIFF_BYTE:
68         return bytestream2_get_byte(gb);
69     case TIFF_SHORT:
70         return ff_tget_short(gb, le);
71     case TIFF_LONG:
72         return ff_tget_long(gb, le);
73     default:
74         return UINT_MAX;
75     }
76 }
77
78
79 int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
80                               GetByteContext *gb, int le, AVDictionary **metadata)
81 {
82     AVBPrint bp;
83     char *ap;
84     int32_t nom, denom;
85     int i;
86
87     if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
88         return AVERROR_INVALIDDATA;
89     if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
90         return AVERROR_INVALIDDATA;
91     if (!sep) sep = ", ";
92
93     av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC);
94
95     for (i = 0; i < count; i++) {
96         nom   = ff_tget_long(gb, le);
97         denom = ff_tget_long(gb, le);
98         av_bprintf(&bp, "%s%i:%i", (i ? sep : ""), nom, denom);
99     }
100
101     if ((i = av_bprint_finalize(&bp, &ap))) {
102         return i;
103     }
104     if (!ap) {
105         return AVERROR(ENOMEM);
106     }
107
108     av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
109
110     return 0;
111 }
112
113
114 int ff_tadd_long_metadata(int count, const char *name, const char *sep,
115                           GetByteContext *gb, int le, AVDictionary **metadata)
116 {
117     AVBPrint bp;
118     char *ap;
119     int i;
120
121     if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
122         return AVERROR_INVALIDDATA;
123     if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
124         return AVERROR_INVALIDDATA;
125     if (!sep) sep = ", ";
126
127     av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC);
128
129     for (i = 0; i < count; i++) {
130         av_bprintf(&bp, "%s%i", (i ? sep : ""), ff_tget_long(gb, le));
131     }
132
133     if ((i = av_bprint_finalize(&bp, &ap))) {
134         return i;
135     }
136     if (!ap) {
137         return AVERROR(ENOMEM);
138     }
139
140     av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
141
142     return 0;
143 }
144
145
146 int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
147                              GetByteContext *gb, int le, AVDictionary **metadata)
148 {
149     AVBPrint bp;
150     char *ap;
151     int i;
152
153     if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
154         return AVERROR_INVALIDDATA;
155     if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
156         return AVERROR_INVALIDDATA;
157     if (!sep) sep = ", ";
158
159     av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC);
160
161     for (i = 0; i < count; i++) {
162         av_bprintf(&bp, "%s%f", (i ? sep : ""), ff_tget_double(gb, le));
163     }
164
165     if ((i = av_bprint_finalize(&bp, &ap))) {
166         return i;
167     }
168     if (!ap) {
169         return AVERROR(ENOMEM);
170     }
171
172     av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
173
174     return 0;
175 }
176
177
178 int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
179                             GetByteContext *gb, int le, AVDictionary **metadata)
180 {
181     AVBPrint bp;
182     char *ap;
183     int i;
184
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;
189     if (!sep) sep = ", ";
190
191     av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_AUTOMATIC);
192
193     for (i = 0; i < count; i++) {
194         av_bprintf(&bp, "%s%i", (i ? sep : ""), ff_tget_short(gb, le));
195     }
196
197     if ((i = av_bprint_finalize(&bp, &ap))) {
198         return i;
199     }
200     if (!ap) {
201         return AVERROR(ENOMEM);
202     }
203
204     av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
205
206     return 0;
207 }
208
209
210 int ff_tadd_string_metadata(int count, const char *name,
211                             GetByteContext *gb, int le, AVDictionary **metadata)
212 {
213     char *value;
214
215     if (bytestream2_get_bytes_left(gb) < count || count < 0)
216         return AVERROR_INVALIDDATA;
217
218     value = av_malloc(count + 1);
219     if (!value)
220         return AVERROR(ENOMEM);
221
222     bytestream2_get_bufferu(gb, value, count);
223     value[count] = 0;
224
225     av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
226     return 0;
227 }
228
229
230 int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
231 {
232     if (bytestream2_get_bytes_left(gb) < 8) {
233         return AVERROR_INVALIDDATA;
234     }
235
236     *le = bytestream2_get_le16u(gb);
237     if (*le == AV_RB16("II")) {
238         *le = 1;
239     } else if (*le == AV_RB16("MM")) {
240         *le = 0;
241     } else {
242         return AVERROR_INVALIDDATA;
243     }
244
245     if (ff_tget_short(gb, *le) != 42) {
246         return AVERROR_INVALIDDATA;
247     }
248
249     *ifd_offset = ff_tget_long(gb, *le);
250
251     return 0;
252 }
253
254
255 int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
256                  unsigned *count, int *next)
257 {
258     int ifd_tag;
259     int valid_type;
260
261     *tag    = ff_tget_short(gb, le);
262     *type   = ff_tget_short(gb, le);
263     *count  = ff_tget_long (gb, le);
264
265     ifd_tag    = ff_tis_ifd(*tag);
266     valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
267
268     *next = bytestream2_tell(gb) + 4;
269
270     // check for valid type
271     if (!valid_type) {
272         return AVERROR_INVALIDDATA;
273     }
274
275     // seek to offset if this is an IFD-tag or
276     // if count values do not fit into the offset value
277     if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
278         bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
279     }
280
281     return 0;
282 }