Imported Debian version 2.5.3~trusty1
[deb_ffmpeg.git] / ffmpeg / libavcodec / tiff.c
1 /*
2 * Copyright (c) 2006 Konstantin Shishkov
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * TIFF image decoder
24 * @author Konstantin Shishkov
25 */
26
27 #include "config.h"
28 #if CONFIG_ZLIB
29 #include <zlib.h>
30 #endif
31 #if CONFIG_LZMA
32 #define LZMA_API_STATIC
33 #include <lzma.h>
34 #endif
35
36 #include "libavutil/attributes.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/imgutils.h"
40 #include "avcodec.h"
41 #include "bytestream.h"
42 #include "faxcompr.h"
43 #include "internal.h"
44 #include "lzw.h"
45 #include "mathops.h"
46 #include "tiff.h"
47 #include "tiff_data.h"
48 #include "thread.h"
49
50 typedef struct TiffContext {
51 AVCodecContext *avctx;
52 GetByteContext gb;
53
54 int width, height;
55 unsigned int bpp, bppcount;
56 uint32_t palette[256];
57 int palette_is_set;
58 int le;
59 enum TiffCompr compr;
60 enum TiffPhotometric photometric;
61 int planar;
62 int subsampling[2];
63 int fax_opts;
64 int predictor;
65 int fill_order;
66 uint32_t res[4];
67
68 int strips, rps, sstype;
69 int sot;
70 int stripsizesoff, stripsize, stripoff, strippos;
71 LZWState *lzw;
72
73 uint8_t *deinvert_buf;
74 int deinvert_buf_size;
75 uint8_t *yuv_line;
76 unsigned int yuv_line_size;
77
78 int geotag_count;
79 TiffGeoTag *geotags;
80 } TiffContext;
81
82 static void free_geotags(TiffContext *const s)
83 {
84 int i;
85 for (i = 0; i < s->geotag_count; i++) {
86 if (s->geotags[i].val)
87 av_freep(&s->geotags[i].val);
88 }
89 av_freep(&s->geotags);
90 s->geotag_count = 0;
91 }
92
93 #define RET_GEOKEY(TYPE, array, element)\
94 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
95 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
96 return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
97
98 static const char *get_geokey_name(int key)
99 {
100 RET_GEOKEY(VERT, vert, name);
101 RET_GEOKEY(PROJ, proj, name);
102 RET_GEOKEY(GEOG, geog, name);
103 RET_GEOKEY(CONF, conf, name);
104
105 return NULL;
106 }
107
108 static int get_geokey_type(int key)
109 {
110 RET_GEOKEY(VERT, vert, type);
111 RET_GEOKEY(PROJ, proj, type);
112 RET_GEOKEY(GEOG, geog, type);
113 RET_GEOKEY(CONF, conf, type);
114
115 return AVERROR_INVALIDDATA;
116 }
117
118 static int cmp_id_key(const void *id, const void *k)
119 {
120 return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
121 }
122
123 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
124 {
125 TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
126 if(r)
127 return r->name;
128
129 return NULL;
130 }
131
132 static char *get_geokey_val(int key, int val)
133 {
134 char *ap;
135
136 if (val == TIFF_GEO_KEY_UNDEFINED)
137 return av_strdup("undefined");
138 if (val == TIFF_GEO_KEY_USER_DEFINED)
139 return av_strdup("User-Defined");
140
141 #define RET_GEOKEY_VAL(TYPE, array)\
142 if (val >= TIFF_##TYPE##_OFFSET &&\
143 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
144 return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
145
146 switch (key) {
147 case TIFF_GT_MODEL_TYPE_GEOKEY:
148 RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
149 break;
150 case TIFF_GT_RASTER_TYPE_GEOKEY:
151 RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
152 break;
153 case TIFF_GEOG_LINEAR_UNITS_GEOKEY:
154 case TIFF_PROJ_LINEAR_UNITS_GEOKEY:
155 case TIFF_VERTICAL_UNITS_GEOKEY:
156 RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
157 break;
158 case TIFF_GEOG_ANGULAR_UNITS_GEOKEY:
159 case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY:
160 RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
161 break;
162 case TIFF_GEOGRAPHIC_TYPE_GEOKEY:
163 RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
164 RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
165 break;
166 case TIFF_GEOG_GEODETIC_DATUM_GEOKEY:
167 RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
168 RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
169 break;
170 case TIFF_GEOG_ELLIPSOID_GEOKEY:
171 RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
172 break;
173 case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY:
174 RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
175 break;
176 case TIFF_PROJECTED_CS_TYPE_GEOKEY:
177 ap = av_strdup(search_keyval(ff_tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(ff_tiff_proj_cs_type_codes), val));
178 if(ap) return ap;
179 break;
180 case TIFF_PROJECTION_GEOKEY:
181 ap = av_strdup(search_keyval(ff_tiff_projection_codes, FF_ARRAY_ELEMS(ff_tiff_projection_codes), val));
182 if(ap) return ap;
183 break;
184 case TIFF_PROJ_COORD_TRANS_GEOKEY:
185 RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
186 break;
187 case TIFF_VERTICAL_CS_TYPE_GEOKEY:
188 RET_GEOKEY_VAL(VERT_CS, vert_cs);
189 RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
190 break;
191
192 }
193
194 ap = av_malloc(14);
195 if (ap)
196 snprintf(ap, 14, "Unknown-%d", val);
197 return ap;
198 }
199
200 static char *doubles2str(double *dp, int count, const char *sep)
201 {
202 int i;
203 char *ap, *ap0;
204 uint64_t component_len;
205 if (!sep) sep = ", ";
206 component_len = 24LL + strlen(sep);
207 if (count >= (INT_MAX - 1)/component_len)
208 return NULL;
209 ap = av_malloc(component_len * count + 1);
210 if (!ap)
211 return NULL;
212 ap0 = ap;
213 ap[0] = '\0';
214 for (i = 0; i < count; i++) {
215 unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
216 if(l >= component_len) {
217 av_free(ap0);
218 return NULL;
219 }
220 ap += l;
221 }
222 ap0[strlen(ap0) - strlen(sep)] = '\0';
223 return ap0;
224 }
225
226 static int add_metadata(int count, int type,
227 const char *name, const char *sep, TiffContext *s, AVFrame *frame)
228 {
229 switch(type) {
230 case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
231 case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, avpriv_frame_get_metadatap(frame));
232 case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, avpriv_frame_get_metadatap(frame));
233 default : return AVERROR_INVALIDDATA;
234 };
235 }
236
237 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
238 int usePtr, const uint8_t *src,
239 uint8_t c, int width, int offset)
240 {
241 switch (bpp) {
242 case 1:
243 while (--width >= 0) {
244 dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
245 dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
246 dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
247 dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
248 dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
249 dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
250 dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
251 dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
252 }
253 break;
254 case 2:
255 while (--width >= 0) {
256 dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
257 dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
258 dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
259 dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
260 }
261 break;
262 case 4:
263 while (--width >= 0) {
264 dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
265 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
266 }
267 break;
268 default:
269 if (usePtr) {
270 memcpy(dst + offset, src, width);
271 } else {
272 memset(dst + offset, c, width);
273 }
274 }
275 }
276
277 static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
278 {
279 int i;
280
281 av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size);
282 if (!s->deinvert_buf)
283 return AVERROR(ENOMEM);
284 for (i = 0; i < size; i++)
285 s->deinvert_buf[i] = ff_reverse[src[i]];
286
287 return 0;
288 }
289
290 static void unpack_yuv(TiffContext *s, AVFrame *p,
291 const uint8_t *src, int lnum)
292 {
293 int i, j, k;
294 int w = (s->width - 1) / s->subsampling[0] + 1;
295 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
296 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
297 if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
298 for (i = 0; i < w; i++) {
299 for (j = 0; j < s->subsampling[1]; j++)
300 for (k = 0; k < s->subsampling[0]; k++)
301 p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
302 FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
303 *pu++ = *src++;
304 *pv++ = *src++;
305 }
306 }else{
307 for (i = 0; i < w; i++) {
308 for (j = 0; j < s->subsampling[1]; j++)
309 for (k = 0; k < s->subsampling[0]; k++)
310 p->data[0][(lnum + j) * p->linesize[0] +
311 i * s->subsampling[0] + k] = *src++;
312 *pu++ = *src++;
313 *pv++ = *src++;
314 }
315 }
316 }
317
318 #if CONFIG_ZLIB
319 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
320 int size)
321 {
322 z_stream zstream = { 0 };
323 int zret;
324
325 zstream.next_in = (uint8_t *)src;
326 zstream.avail_in = size;
327 zstream.next_out = dst;
328 zstream.avail_out = *len;
329 zret = inflateInit(&zstream);
330 if (zret != Z_OK) {
331 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
332 return zret;
333 }
334 zret = inflate(&zstream, Z_SYNC_FLUSH);
335 inflateEnd(&zstream);
336 *len = zstream.total_out;
337 return zret == Z_STREAM_END ? Z_OK : zret;
338 }
339
340 static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
341 const uint8_t *src, int size, int width, int lines,
342 int strip_start, int is_yuv)
343 {
344 uint8_t *zbuf;
345 unsigned long outlen;
346 int ret, line;
347 outlen = width * lines;
348 zbuf = av_malloc(outlen);
349 if (!zbuf)
350 return AVERROR(ENOMEM);
351 if (s->fill_order) {
352 if ((ret = deinvert_buffer(s, src, size)) < 0) {
353 av_free(zbuf);
354 return ret;
355 }
356 src = s->deinvert_buf;
357 }
358 ret = tiff_uncompress(zbuf, &outlen, src, size);
359 if (ret != Z_OK) {
360 av_log(s->avctx, AV_LOG_ERROR,
361 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
362 (unsigned long)width * lines, ret);
363 av_free(zbuf);
364 return AVERROR_UNKNOWN;
365 }
366 src = zbuf;
367 for (line = 0; line < lines; line++) {
368 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
369 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
370 } else {
371 memcpy(dst, src, width);
372 }
373 if (is_yuv) {
374 unpack_yuv(s, p, dst, strip_start + line);
375 line += s->subsampling[1] - 1;
376 }
377 dst += stride;
378 src += width;
379 }
380 av_free(zbuf);
381 return 0;
382 }
383 #endif
384
385 #if CONFIG_LZMA
386 static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
387 int size)
388 {
389 lzma_stream stream = LZMA_STREAM_INIT;
390 lzma_ret ret;
391
392 stream.next_in = (uint8_t *)src;
393 stream.avail_in = size;
394 stream.next_out = dst;
395 stream.avail_out = *len;
396 ret = lzma_stream_decoder(&stream, UINT64_MAX, 0);
397 if (ret != LZMA_OK) {
398 av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
399 return ret;
400 }
401 ret = lzma_code(&stream, LZMA_RUN);
402 lzma_end(&stream);
403 *len = stream.total_out;
404 return ret == LZMA_STREAM_END ? LZMA_OK : ret;
405 }
406
407 static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
408 const uint8_t *src, int size, int width, int lines,
409 int strip_start, int is_yuv)
410 {
411 uint64_t outlen = width * lines;
412 int ret, line;
413 uint8_t *buf = av_malloc(outlen);
414 if (!buf)
415 return AVERROR(ENOMEM);
416 if (s->fill_order) {
417 if ((ret = deinvert_buffer(s, src, size)) < 0) {
418 av_free(buf);
419 return ret;
420 }
421 src = s->deinvert_buf;
422 }
423 ret = tiff_uncompress_lzma(buf, &outlen, src, size);
424 if (ret != LZMA_OK) {
425 av_log(s->avctx, AV_LOG_ERROR,
426 "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
427 (uint64_t)width * lines, ret);
428 av_free(buf);
429 return AVERROR_UNKNOWN;
430 }
431 src = buf;
432 for (line = 0; line < lines; line++) {
433 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
434 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
435 } else {
436 memcpy(dst, src, width);
437 }
438 if (is_yuv) {
439 unpack_yuv(s, p, dst, strip_start + line);
440 line += s->subsampling[1] - 1;
441 }
442 dst += stride;
443 src += width;
444 }
445 av_free(buf);
446 return 0;
447 }
448 #endif
449
450 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
451 const uint8_t *src, int size, int width, int lines)
452 {
453 int i, ret = 0;
454 int line;
455 uint8_t *src2 = av_malloc((unsigned)size +
456 FF_INPUT_BUFFER_PADDING_SIZE);
457
458 if (!src2) {
459 av_log(s->avctx, AV_LOG_ERROR,
460 "Error allocating temporary buffer\n");
461 return AVERROR(ENOMEM);
462 }
463 if (s->fax_opts & 2) {
464 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
465 av_free(src2);
466 return AVERROR_PATCHWELCOME;
467 }
468 if (!s->fill_order) {
469 memcpy(src2, src, size);
470 } else {
471 for (i = 0; i < size; i++)
472 src2[i] = ff_reverse[src[i]];
473 }
474 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
475 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
476 s->compr, s->fax_opts);
477 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
478 for (line = 0; line < lines; line++) {
479 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
480 dst += stride;
481 }
482 av_free(src2);
483 return ret;
484 }
485
486 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
487 const uint8_t *src, int size, int strip_start, int lines)
488 {
489 PutByteContext pb;
490 int c, line, pixels, code, ret;
491 const uint8_t *ssrc = src;
492 int width = ((s->width * s->bpp) + 7) >> 3;
493 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format);
494 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
495 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
496 desc->nb_components >= 3;
497
498 if (s->planar)
499 width /= s->bppcount;
500
501 if (size <= 0)
502 return AVERROR_INVALIDDATA;
503
504 if (is_yuv) {
505 int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
506 s->subsampling[0] * s->subsampling[1] + 7) >> 3;
507 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
508 if (s->yuv_line == NULL) {
509 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
510 return AVERROR(ENOMEM);
511 }
512 dst = s->yuv_line;
513 stride = 0;
514
515 width = (s->width - 1) / s->subsampling[0] + 1;
516 width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
517 av_assert0(width <= bytes_per_row);
518 av_assert0(s->bpp == 24);
519 }
520
521 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
522 #if CONFIG_ZLIB
523 return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
524 strip_start, is_yuv);
525 #else
526 av_log(s->avctx, AV_LOG_ERROR,
527 "zlib support not enabled, "
528 "deflate compression not supported\n");
529 return AVERROR(ENOSYS);
530 #endif
531 }
532 if (s->compr == TIFF_LZMA) {
533 #if CONFIG_LZMA
534 return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
535 strip_start, is_yuv);
536 #else
537 av_log(s->avctx, AV_LOG_ERROR,
538 "LZMA support not enabled\n");
539 return AVERROR(ENOSYS);
540 #endif
541 }
542 if (s->compr == TIFF_LZW) {
543 if (s->fill_order) {
544 if ((ret = deinvert_buffer(s, src, size)) < 0)
545 return ret;
546 ssrc = src = s->deinvert_buf;
547 }
548 if (size > 1 && !src[0] && (src[1]&1)) {
549 av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
550 }
551 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
552 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
553 return ret;
554 }
555 for (line = 0; line < lines; line++) {
556 pixels = ff_lzw_decode(s->lzw, dst, width);
557 if (pixels < width) {
558 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
559 pixels, width);
560 return AVERROR_INVALIDDATA;
561 }
562 if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
563 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
564 if (is_yuv) {
565 unpack_yuv(s, p, dst, strip_start + line);
566 line += s->subsampling[1] - 1;
567 }
568 dst += stride;
569 }
570 return 0;
571 }
572 if (s->compr == TIFF_CCITT_RLE ||
573 s->compr == TIFF_G3 ||
574 s->compr == TIFF_G4) {
575 if (is_yuv)
576 return AVERROR_INVALIDDATA;
577
578 return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
579 }
580
581 bytestream2_init(&s->gb, src, size);
582 bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
583
584 for (line = 0; line < lines; line++) {
585 if (src - ssrc > size) {
586 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
587 return AVERROR_INVALIDDATA;
588 }
589
590 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
591 break;
592 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
593 switch (s->compr) {
594 case TIFF_RAW:
595 if (ssrc + size - src < width)
596 return AVERROR_INVALIDDATA;
597
598 if (!s->fill_order) {
599 horizontal_fill(s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
600 dst, 1, src, 0, width, 0);
601 } else {
602 int i;
603 for (i = 0; i < width; i++)
604 dst[i] = ff_reverse[src[i]];
605 }
606 src += width;
607 break;
608 case TIFF_PACKBITS:
609 for (pixels = 0; pixels < width;) {
610 if (ssrc + size - src < 2) {
611 av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
612 return AVERROR_INVALIDDATA;
613 }
614 code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
615 if (code >= 0) {
616 code++;
617 if (pixels + code > width ||
618 ssrc + size - src < code) {
619 av_log(s->avctx, AV_LOG_ERROR,
620 "Copy went out of bounds\n");
621 return AVERROR_INVALIDDATA;
622 }
623 horizontal_fill(s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
624 dst, 1, src, 0, code, pixels);
625 src += code;
626 pixels += code;
627 } else if (code != -128) { // -127..-1
628 code = (-code) + 1;
629 if (pixels + code > width) {
630 av_log(s->avctx, AV_LOG_ERROR,
631 "Run went out of bounds\n");
632 return AVERROR_INVALIDDATA;
633 }
634 c = *src++;
635 horizontal_fill(s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
636 dst, 0, NULL, c, code, pixels);
637 pixels += code;
638 }
639 }
640 if (s->fill_order) {
641 int i;
642 for (i = 0; i < width; i++)
643 dst[i] = ff_reverse[dst[i]];
644 }
645 break;
646 }
647 if (is_yuv) {
648 unpack_yuv(s, p, dst, strip_start + line);
649 line += s->subsampling[1] - 1;
650 }
651 dst += stride;
652 }
653 return 0;
654 }
655
656 static int init_image(TiffContext *s, ThreadFrame *frame)
657 {
658 int ret;
659 int create_gray_palette = 0;
660
661 switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
662 case 11:
663 if (!s->palette_is_set) {
664 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
665 break;
666 }
667 case 21:
668 case 41:
669 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
670 if (!s->palette_is_set) {
671 create_gray_palette = 1;
672 }
673 break;
674 case 81:
675 s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
676 break;
677 case 243:
678 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
679 if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
680 s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
681 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
682 s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
683 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
684 s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
685 } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
686 s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
687 } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
688 s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
689 } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
690 s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
691 } else {
692 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
693 return AVERROR_PATCHWELCOME;
694 }
695 } else
696 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
697 break;
698 case 161:
699 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
700 break;
701 case 162:
702 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
703 break;
704 case 322:
705 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
706 break;
707 case 324:
708 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
709 break;
710 case 483:
711 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
712 break;
713 case 644:
714 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
715 break;
716 case 1243:
717 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
718 break;
719 case 1324:
720 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
721 break;
722 case 1483:
723 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
724 break;
725 case 1644:
726 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
727 break;
728 default:
729 av_log(s->avctx, AV_LOG_ERROR,
730 "This format is not supported (bpp=%d, bppcount=%d)\n",
731 s->bpp, s->bppcount);
732 return AVERROR_INVALIDDATA;
733 }
734
735 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
736 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
737 if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
738 !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
739 desc->nb_components < 3) {
740 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
741 return AVERROR_INVALIDDATA;
742 }
743 }
744
745 if (s->width != s->avctx->width || s->height != s->avctx->height) {
746 ret = ff_set_dimensions(s->avctx, s->width, s->height);
747 if (ret < 0)
748 return ret;
749 }
750 if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
751 return ret;
752 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
753 if (!create_gray_palette)
754 memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
755 else {
756 /* make default grayscale pal */
757 int i;
758 uint32_t *pal = (uint32_t *)frame->f->data[1];
759 for (i = 0; i < 1<<s->bpp; i++)
760 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
761 }
762 }
763 return 0;
764 }
765
766 static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
767 {
768 int offset = tag == TIFF_YRES ? 2 : 0;
769 s->res[offset++] = num;
770 s->res[offset] = den;
771 if (s->res[0] && s->res[1] && s->res[2] && s->res[3])
772 av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
773 s->res[2] * (uint64_t)s->res[1], s->res[0] * (uint64_t)s->res[3], INT32_MAX);
774 }
775
776 static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
777 {
778 unsigned tag, type, count, off, value = 0, value2 = 0;
779 int i, start;
780 int pos;
781 int ret;
782 double *dp;
783
784 ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
785 if (ret < 0) {
786 goto end;
787 }
788
789 off = bytestream2_tell(&s->gb);
790 if (count == 1) {
791 switch (type) {
792 case TIFF_BYTE:
793 case TIFF_SHORT:
794 case TIFF_LONG:
795 value = ff_tget(&s->gb, type, s->le);
796 break;
797 case TIFF_RATIONAL:
798 value = ff_tget(&s->gb, TIFF_LONG, s->le);
799 value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
800 break;
801 case TIFF_STRING:
802 if (count <= 4) {
803 break;
804 }
805 default:
806 value = UINT_MAX;
807 }
808 }
809
810 switch (tag) {
811 case TIFF_WIDTH:
812 s->width = value;
813 break;
814 case TIFF_HEIGHT:
815 s->height = value;
816 break;
817 case TIFF_BPP:
818 if (count > 4U) {
819 av_log(s->avctx, AV_LOG_ERROR,
820 "This format is not supported (bpp=%d, %d components)\n",
821 value, count);
822 return AVERROR_INVALIDDATA;
823 }
824 s->bppcount = count;
825 if (count == 1)
826 s->bpp = value;
827 else {
828 switch (type) {
829 case TIFF_BYTE:
830 case TIFF_SHORT:
831 case TIFF_LONG:
832 s->bpp = 0;
833 if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
834 return AVERROR_INVALIDDATA;
835 for (i = 0; i < count; i++)
836 s->bpp += ff_tget(&s->gb, type, s->le);
837 break;
838 default:
839 s->bpp = -1;
840 }
841 }
842 if (s->bpp > 64U) {
843 av_log(s->avctx, AV_LOG_ERROR,
844 "This format is not supported (bpp=%d, %d components)\n",
845 s->bpp, count);
846 s->bpp = 0;
847 return AVERROR_INVALIDDATA;
848 }
849 break;
850 case TIFF_SAMPLES_PER_PIXEL:
851 if (count != 1) {
852 av_log(s->avctx, AV_LOG_ERROR,
853 "Samples per pixel requires a single value, many provided\n");
854 return AVERROR_INVALIDDATA;
855 }
856 if (value > 4U) {
857 av_log(s->avctx, AV_LOG_ERROR,
858 "Samples per pixel %d is too large\n", value);
859 return AVERROR_INVALIDDATA;
860 }
861 if (s->bppcount == 1)
862 s->bpp *= value;
863 s->bppcount = value;
864 break;
865 case TIFF_COMPR:
866 s->compr = value;
867 s->predictor = 0;
868 switch (s->compr) {
869 case TIFF_RAW:
870 case TIFF_PACKBITS:
871 case TIFF_LZW:
872 case TIFF_CCITT_RLE:
873 break;
874 case TIFF_G3:
875 case TIFF_G4:
876 s->fax_opts = 0;
877 break;
878 case TIFF_DEFLATE:
879 case TIFF_ADOBE_DEFLATE:
880 #if CONFIG_ZLIB
881 break;
882 #else
883 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
884 return AVERROR(ENOSYS);
885 #endif
886 case TIFF_JPEG:
887 case TIFF_NEWJPEG:
888 avpriv_report_missing_feature(s->avctx, "JPEG compression");
889 return AVERROR_PATCHWELCOME;
890 case TIFF_LZMA:
891 #if CONFIG_LZMA
892 break;
893 #else
894 av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
895 return AVERROR(ENOSYS);
896 #endif
897 default:
898 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
899 s->compr);
900 return AVERROR_INVALIDDATA;
901 }
902 break;
903 case TIFF_ROWSPERSTRIP:
904 if (!value || (type == TIFF_LONG && value == UINT_MAX))
905 value = s->height;
906 s->rps = FFMIN(value, s->height);
907 break;
908 case TIFF_STRIP_OFFS:
909 if (count == 1) {
910 s->strippos = 0;
911 s->stripoff = value;
912 } else
913 s->strippos = off;
914 s->strips = count;
915 if (s->strips == 1)
916 s->rps = s->height;
917 s->sot = type;
918 break;
919 case TIFF_STRIP_SIZE:
920 if (count == 1) {
921 s->stripsizesoff = 0;
922 s->stripsize = value;
923 s->strips = 1;
924 } else {
925 s->stripsizesoff = off;
926 }
927 s->strips = count;
928 s->sstype = type;
929 break;
930 case TIFF_XRES:
931 case TIFF_YRES:
932 set_sar(s, tag, value, value2);
933 break;
934 case TIFF_TILE_BYTE_COUNTS:
935 case TIFF_TILE_LENGTH:
936 case TIFF_TILE_OFFSETS:
937 case TIFF_TILE_WIDTH:
938 av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
939 return AVERROR_PATCHWELCOME;
940 break;
941 case TIFF_PREDICTOR:
942 s->predictor = value;
943 break;
944 case TIFF_PHOTOMETRIC:
945 switch (value) {
946 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
947 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
948 case TIFF_PHOTOMETRIC_RGB:
949 case TIFF_PHOTOMETRIC_PALETTE:
950 case TIFF_PHOTOMETRIC_YCBCR:
951 s->photometric = value;
952 break;
953 case TIFF_PHOTOMETRIC_ALPHA_MASK:
954 case TIFF_PHOTOMETRIC_SEPARATED:
955 case TIFF_PHOTOMETRIC_CIE_LAB:
956 case TIFF_PHOTOMETRIC_ICC_LAB:
957 case TIFF_PHOTOMETRIC_ITU_LAB:
958 case TIFF_PHOTOMETRIC_CFA:
959 case TIFF_PHOTOMETRIC_LOG_L:
960 case TIFF_PHOTOMETRIC_LOG_LUV:
961 case TIFF_PHOTOMETRIC_LINEAR_RAW:
962 avpriv_report_missing_feature(s->avctx,
963 "PhotometricInterpretation 0x%04X",
964 value);
965 return AVERROR_PATCHWELCOME;
966 default:
967 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
968 "unknown\n", value);
969 return AVERROR_INVALIDDATA;
970 }
971 break;
972 case TIFF_FILL_ORDER:
973 if (value < 1 || value > 2) {
974 av_log(s->avctx, AV_LOG_ERROR,
975 "Unknown FillOrder value %d, trying default one\n", value);
976 value = 1;
977 }
978 s->fill_order = value - 1;
979 break;
980 case TIFF_PAL: {
981 GetByteContext pal_gb[3];
982 off = type_sizes[type];
983 if (count / 3 > 256 ||
984 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
985 return AVERROR_INVALIDDATA;
986
987 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
988 bytestream2_skip(&pal_gb[1], count / 3 * off);
989 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
990
991 off = (type_sizes[type] - 1) << 3;
992 for (i = 0; i < count / 3; i++) {
993 uint32_t p = 0xFF000000;
994 p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
995 p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
996 p |= ff_tget(&pal_gb[2], type, s->le) >> off;
997 s->palette[i] = p;
998 }
999 s->palette_is_set = 1;
1000 break;
1001 }
1002 case TIFF_PLANAR:
1003 s->planar = value == 2;
1004 break;
1005 case TIFF_YCBCR_SUBSAMPLING:
1006 if (count != 2) {
1007 av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1008 return AVERROR_INVALIDDATA;
1009 }
1010 for (i = 0; i < count; i++)
1011 s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1012 break;
1013 case TIFF_T4OPTIONS:
1014 if (s->compr == TIFF_G3)
1015 s->fax_opts = value;
1016 break;
1017 case TIFF_T6OPTIONS:
1018 if (s->compr == TIFF_G4)
1019 s->fax_opts = value;
1020 break;
1021 #define ADD_METADATA(count, name, sep)\
1022 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1023 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1024 goto end;\
1025 }
1026 case TIFF_MODEL_PIXEL_SCALE:
1027 ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1028 break;
1029 case TIFF_MODEL_TRANSFORMATION:
1030 ADD_METADATA(count, "ModelTransformationTag", NULL);
1031 break;
1032 case TIFF_MODEL_TIEPOINT:
1033 ADD_METADATA(count, "ModelTiepointTag", NULL);
1034 break;
1035 case TIFF_GEO_KEY_DIRECTORY:
1036 ADD_METADATA(1, "GeoTIFF_Version", NULL);
1037 ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1038 s->geotag_count = ff_tget_short(&s->gb, s->le);
1039 if (s->geotag_count > count / 4 - 1) {
1040 s->geotag_count = count / 4 - 1;
1041 av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1042 }
1043 if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4) {
1044 s->geotag_count = 0;
1045 return -1;
1046 }
1047 s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1048 if (!s->geotags) {
1049 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1050 s->geotag_count = 0;
1051 goto end;
1052 }
1053 for (i = 0; i < s->geotag_count; i++) {
1054 s->geotags[i].key = ff_tget_short(&s->gb, s->le);
1055 s->geotags[i].type = ff_tget_short(&s->gb, s->le);
1056 s->geotags[i].count = ff_tget_short(&s->gb, s->le);
1057
1058 if (!s->geotags[i].type)
1059 s->geotags[i].val = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1060 else
1061 s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1062 }
1063 break;
1064 case TIFF_GEO_DOUBLE_PARAMS:
1065 if (count >= INT_MAX / sizeof(int64_t))
1066 return AVERROR_INVALIDDATA;
1067 if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1068 return AVERROR_INVALIDDATA;
1069 dp = av_malloc_array(count, sizeof(double));
1070 if (!dp) {
1071 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1072 goto end;
1073 }
1074 for (i = 0; i < count; i++)
1075 dp[i] = ff_tget_double(&s->gb, s->le);
1076 for (i = 0; i < s->geotag_count; i++) {
1077 if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1078 if (s->geotags[i].count == 0
1079 || s->geotags[i].offset + s->geotags[i].count > count) {
1080 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1081 } else {
1082 char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1083 if (!ap) {
1084 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1085 av_freep(&dp);
1086 return AVERROR(ENOMEM);
1087 }
1088 s->geotags[i].val = ap;
1089 }
1090 }
1091 }
1092 av_freep(&dp);
1093 break;
1094 case TIFF_GEO_ASCII_PARAMS:
1095 pos = bytestream2_tell(&s->gb);
1096 for (i = 0; i < s->geotag_count; i++) {
1097 if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1098 if (s->geotags[i].count == 0
1099 || s->geotags[i].offset + s->geotags[i].count > count) {
1100 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1101 } else {
1102 char *ap;
1103
1104 bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1105 if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1106 return AVERROR_INVALIDDATA;
1107 ap = av_malloc(s->geotags[i].count);
1108 if (!ap) {
1109 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1110 return AVERROR(ENOMEM);
1111 }
1112 bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1113 ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1114 s->geotags[i].val = ap;
1115 }
1116 }
1117 }
1118 break;
1119 case TIFF_ARTIST:
1120 ADD_METADATA(count, "artist", NULL);
1121 break;
1122 case TIFF_COPYRIGHT:
1123 ADD_METADATA(count, "copyright", NULL);
1124 break;
1125 case TIFF_DATE:
1126 ADD_METADATA(count, "date", NULL);
1127 break;
1128 case TIFF_DOCUMENT_NAME:
1129 ADD_METADATA(count, "document_name", NULL);
1130 break;
1131 case TIFF_HOST_COMPUTER:
1132 ADD_METADATA(count, "computer", NULL);
1133 break;
1134 case TIFF_IMAGE_DESCRIPTION:
1135 ADD_METADATA(count, "description", NULL);
1136 break;
1137 case TIFF_MAKE:
1138 ADD_METADATA(count, "make", NULL);
1139 break;
1140 case TIFF_MODEL:
1141 ADD_METADATA(count, "model", NULL);
1142 break;
1143 case TIFF_PAGE_NAME:
1144 ADD_METADATA(count, "page_name", NULL);
1145 break;
1146 case TIFF_PAGE_NUMBER:
1147 ADD_METADATA(count, "page_number", " / ");
1148 break;
1149 case TIFF_SOFTWARE_NAME:
1150 ADD_METADATA(count, "software", NULL);
1151 break;
1152 default:
1153 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1154 av_log(s->avctx, AV_LOG_ERROR,
1155 "Unknown or unsupported tag %d/0X%0X\n",
1156 tag, tag);
1157 return AVERROR_INVALIDDATA;
1158 }
1159 }
1160 end:
1161 bytestream2_seek(&s->gb, start, SEEK_SET);
1162 return 0;
1163 }
1164
1165 static int decode_frame(AVCodecContext *avctx,
1166 void *data, int *got_frame, AVPacket *avpkt)
1167 {
1168 TiffContext *const s = avctx->priv_data;
1169 AVFrame *const p = data;
1170 ThreadFrame frame = { .f = data };
1171 unsigned off;
1172 int le, ret, plane, planes;
1173 int i, j, entries, stride;
1174 unsigned soff, ssize;
1175 uint8_t *dst;
1176 GetByteContext stripsizes;
1177 GetByteContext stripdata;
1178
1179 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1180
1181 // parse image header
1182 if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1183 av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1184 return ret;
1185 } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1186 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1187 return AVERROR_INVALIDDATA;
1188 }
1189 s->le = le;
1190 // TIFF_BPP is not a required tag and defaults to 1
1191 s->bppcount = s->bpp = 1;
1192 s->photometric = TIFF_PHOTOMETRIC_NONE;
1193 s->compr = TIFF_RAW;
1194 s->fill_order = 0;
1195 free_geotags(s);
1196
1197 // Reset these offsets so we can tell if they were set this frame
1198 s->stripsizesoff = s->strippos = 0;
1199 /* parse image file directory */
1200 bytestream2_seek(&s->gb, off, SEEK_SET);
1201 entries = ff_tget_short(&s->gb, le);
1202 if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1203 return AVERROR_INVALIDDATA;
1204 for (i = 0; i < entries; i++) {
1205 if ((ret = tiff_decode_tag(s, p)) < 0)
1206 return ret;
1207 }
1208
1209 for (i = 0; i<s->geotag_count; i++) {
1210 const char *keyname = get_geokey_name(s->geotags[i].key);
1211 if (!keyname) {
1212 av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1213 continue;
1214 }
1215 if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1216 av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1217 continue;
1218 }
1219 ret = av_dict_set(avpriv_frame_get_metadatap(p), keyname, s->geotags[i].val, 0);
1220 if (ret<0) {
1221 av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1222 return ret;
1223 }
1224 }
1225
1226 if (!s->strippos && !s->stripoff) {
1227 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1228 return AVERROR_INVALIDDATA;
1229 }
1230 /* now we have the data and may start decoding */
1231 if ((ret = init_image(s, &frame)) < 0)
1232 return ret;
1233
1234 if (s->strips == 1 && !s->stripsize) {
1235 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1236 s->stripsize = avpkt->size - s->stripoff;
1237 }
1238
1239 if (s->stripsizesoff) {
1240 if (s->stripsizesoff >= (unsigned)avpkt->size)
1241 return AVERROR_INVALIDDATA;
1242 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1243 avpkt->size - s->stripsizesoff);
1244 }
1245 if (s->strippos) {
1246 if (s->strippos >= (unsigned)avpkt->size)
1247 return AVERROR_INVALIDDATA;
1248 bytestream2_init(&stripdata, avpkt->data + s->strippos,
1249 avpkt->size - s->strippos);
1250 }
1251
1252 if (s->rps <= 0) {
1253 av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1254 return AVERROR_INVALIDDATA;
1255 }
1256
1257 planes = s->planar ? s->bppcount : 1;
1258 for (plane = 0; plane < planes; plane++) {
1259 stride = p->linesize[plane];
1260 dst = p->data[plane];
1261 for (i = 0; i < s->height; i += s->rps) {
1262 if (s->stripsizesoff)
1263 ssize = ff_tget(&stripsizes, s->sstype, le);
1264 else
1265 ssize = s->stripsize;
1266
1267 if (s->strippos)
1268 soff = ff_tget(&stripdata, s->sot, le);
1269 else
1270 soff = s->stripoff;
1271
1272 if (soff > avpkt->size || ssize > avpkt->size - soff) {
1273 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
1274 return AVERROR_INVALIDDATA;
1275 }
1276 if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
1277 FFMIN(s->rps, s->height - i))) < 0) {
1278 if (avctx->err_recognition & AV_EF_EXPLODE)
1279 return ret;
1280 break;
1281 }
1282 dst += s->rps * stride;
1283 }
1284 if (s->predictor == 2) {
1285 if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1286 av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
1287 return AVERROR_PATCHWELCOME;
1288 }
1289 dst = p->data[plane];
1290 soff = s->bpp >> 3;
1291 if (s->planar)
1292 soff = FFMAX(soff / s->bppcount, 1);
1293 ssize = s->width * soff;
1294 if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
1295 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||
1296 s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||
1297 s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) {
1298 for (i = 0; i < s->height; i++) {
1299 for (j = soff; j < ssize; j += 2)
1300 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
1301 dst += stride;
1302 }
1303 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
1304 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||
1305 s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||
1306 s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
1307 for (i = 0; i < s->height; i++) {
1308 for (j = soff; j < ssize; j += 2)
1309 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
1310 dst += stride;
1311 }
1312 } else {
1313 for (i = 0; i < s->height; i++) {
1314 for (j = soff; j < ssize; j++)
1315 dst[j] += dst[j - soff];
1316 dst += stride;
1317 }
1318 }
1319 }
1320
1321 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
1322 dst = p->data[plane];
1323 for (i = 0; i < s->height; i++) {
1324 for (j = 0; j < p->linesize[plane]; j++)
1325 dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
1326 dst += stride;
1327 }
1328 }
1329 }
1330
1331 if (s->planar && s->bppcount > 2) {
1332 FFSWAP(uint8_t*, p->data[0], p->data[2]);
1333 FFSWAP(int, p->linesize[0], p->linesize[2]);
1334 FFSWAP(uint8_t*, p->data[0], p->data[1]);
1335 FFSWAP(int, p->linesize[0], p->linesize[1]);
1336 }
1337
1338 *got_frame = 1;
1339
1340 return avpkt->size;
1341 }
1342
1343 static av_cold int tiff_init(AVCodecContext *avctx)
1344 {
1345 TiffContext *s = avctx->priv_data;
1346
1347 s->width = 0;
1348 s->height = 0;
1349 s->subsampling[0] =
1350 s->subsampling[1] = 1;
1351 s->avctx = avctx;
1352 ff_lzw_decode_open(&s->lzw);
1353 ff_ccitt_unpack_init();
1354
1355 return 0;
1356 }
1357
1358 static av_cold int tiff_end(AVCodecContext *avctx)
1359 {
1360 TiffContext *const s = avctx->priv_data;
1361
1362 free_geotags(s);
1363
1364 ff_lzw_decode_close(&s->lzw);
1365 av_freep(&s->deinvert_buf);
1366 return 0;
1367 }
1368
1369 AVCodec ff_tiff_decoder = {
1370 .name = "tiff",
1371 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
1372 .type = AVMEDIA_TYPE_VIDEO,
1373 .id = AV_CODEC_ID_TIFF,
1374 .priv_data_size = sizeof(TiffContext),
1375 .init = tiff_init,
1376 .close = tiff_end,
1377 .decode = decode_frame,
1378 .init_thread_copy = ONLY_IF_THREADS_ENABLED(tiff_init),
1379 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
1380 };