tiff: Improve grayscale support
[libav.git] / libavcodec / tiff.c
CommitLineData
a991b1fe
KS
1/*
2 * TIFF image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
a991b1fe 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
a991b1fe
KS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
a991b1fe
KS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
a991b1fe 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a991b1fe 20 */
ee5c8a9b
DB
21
22/**
ba87f080 23 * @file
f6f95d4e 24 * TIFF image decoder
ee5c8a9b
DB
25 * @author Konstantin Shishkov
26 */
f6f95d4e 27
1d9c2dc8 28#include "config.h"
b250f9c6 29#if CONFIG_ZLIB
a991b1fe
KS
30#include <zlib.h>
31#endif
46ce9ded 32
d5c62122 33#include "libavutil/attributes.h"
bdcc13f2 34#include "libavutil/intreadwrite.h"
737eb597 35#include "libavutil/imgutils.h"
46ce9ded 36#include "avcodec.h"
0a467a9b 37#include "bytestream.h"
46ce9ded
DB
38#include "faxcompr.h"
39#include "internal.h"
40#include "lzw.h"
41#include "mathops.h"
42#include "tiff.h"
339aabd5 43
a991b1fe
KS
44typedef struct TiffContext {
45 AVCodecContext *avctx;
0a467a9b 46 GetByteContext gb;
a991b1fe
KS
47
48 int width, height;
bbc572a2 49 unsigned int bpp, bppcount;
7ca5338b
SS
50 uint32_t palette[256];
51 int palette_is_set;
a991b1fe 52 int le;
8102d886 53 enum TiffCompr compr;
a9b046fb 54 enum TiffPhotometric photometric;
92799d32 55 int fax_opts;
108c96d5 56 int predictor;
a58e96be 57 int fill_order;
a991b1fe 58
108c96d5 59 int strips, rps, sstype;
a991b1fe 60 int sot;
0a467a9b 61 int stripsizesoff, stripsize, stripoff, strippos;
ffc5430b 62 LZWState *lzw;
a991b1fe
KS
63} TiffContext;
64
0a467a9b 65static unsigned tget_short(GetByteContext *gb, int le)
f346b6bb 66{
0a467a9b 67 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
a991b1fe
KS
68}
69
0a467a9b 70static unsigned tget_long(GetByteContext *gb, int le)
f346b6bb 71{
0a467a9b 72 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
a991b1fe
KS
73}
74
0a467a9b 75static unsigned tget(GetByteContext *gb, int type, int le)
f346b6bb
AC
76{
77 switch (type) {
0a467a9b
JR
78 case TIFF_BYTE: return bytestream2_get_byte(gb);
79 case TIFF_SHORT: return tget_short(gb, le);
80 case TIFF_LONG: return tget_long(gb, le);
81 default: return UINT_MAX;
a991b1fe
KS
82 }
83}
84
eec69b39 85#if CONFIG_ZLIB
f346b6bb
AC
86static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
87 int size)
e96f885a 88{
a92be9b8 89 z_stream zstream = { 0 };
e96f885a
KS
90 int zret;
91
46ce9ded
DB
92 zstream.next_in = src;
93 zstream.avail_in = size;
94 zstream.next_out = dst;
e96f885a 95 zstream.avail_out = *len;
46ce9ded 96 zret = inflateInit(&zstream);
e96f885a
KS
97 if (zret != Z_OK) {
98 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
99 return zret;
100 }
101 zret = inflate(&zstream, Z_SYNC_FLUSH);
102 inflateEnd(&zstream);
103 *len = zstream.total_out;
104 return zret == Z_STREAM_END ? Z_OK : zret;
105}
10393768
LB
106
107static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
108 const uint8_t *src, int size,
109 int width, int lines)
110{
111 uint8_t *zbuf;
112 unsigned long outlen;
113 int ret, line;
114 outlen = width * lines;
115 zbuf = av_malloc(outlen);
116 if (!zbuf)
117 return AVERROR(ENOMEM);
118 ret = tiff_uncompress(zbuf, &outlen, src, size);
119 if (ret != Z_OK) {
120 av_log(s->avctx, AV_LOG_ERROR,
121 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
122 (unsigned long)width * lines, ret);
123 av_free(zbuf);
124 return AVERROR_UNKNOWN;
125 }
126 src = zbuf;
127 for (line = 0; line < lines; line++) {
128 memcpy(dst, src, width);
129 dst += stride;
130 src += width;
131 }
132 av_free(zbuf);
133 return 0;
134}
eec69b39 135#endif
e96f885a 136
016c5b06
LB
137
138static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
139 const uint8_t *src, int size, int lines)
140{
141 int i, ret = 0;
142 uint8_t *src2 = av_malloc((unsigned)size +
143 FF_INPUT_BUFFER_PADDING_SIZE);
144
145 if (!src2) {
146 av_log(s->avctx, AV_LOG_ERROR,
147 "Error allocating temporary buffer\n");
148 return AVERROR(ENOMEM);
149 }
150 if (s->fax_opts & 2) {
999ccd2d 151 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
016c5b06 152 av_free(src2);
999ccd2d 153 return AVERROR_PATCHWELCOME;
016c5b06
LB
154 }
155 if (!s->fill_order) {
156 memcpy(src2, src, size);
157 } else {
158 for (i = 0; i < size; i++)
159 src2[i] = ff_reverse[src[i]];
160 }
161 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
162 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
163 s->compr, s->fax_opts);
164 av_free(src2);
165 return ret;
166}
167
f346b6bb
AC
168static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
169 const uint8_t *src, int size, int lines)
170{
0a467a9b 171 PutByteContext pb;
b2a7b81b 172 int c, line, pixels, code, ret;
0a467a9b 173 int width = ((s->width * s->bpp) + 7) >> 3;
a991b1fe 174
90a43060
AC
175 if (size <= 0)
176 return AVERROR_INVALIDDATA;
177
f346b6bb 178 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
10393768
LB
179#if CONFIG_ZLIB
180 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
181#else
182 av_log(s->avctx, AV_LOG_ERROR,
183 "zlib support not enabled, "
184 "deflate compression not supported\n");
185 return AVERROR(ENOSYS);
a991b1fe 186#endif
10393768 187 }
f346b6bb 188 if (s->compr == TIFF_LZW) {
b2a7b81b 189 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
ffc5430b 190 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
b2a7b81b 191 return ret;
ffc5430b 192 }
0a467a9b
JR
193 for (line = 0; line < lines; line++) {
194 pixels = ff_lzw_decode(s->lzw, dst, width);
195 if (pixels < width) {
196 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
197 pixels, width);
198 return AVERROR_INVALIDDATA;
199 }
200 dst += stride;
201 }
202 return 0;
ffc5430b 203 }
46ce9ded
DB
204 if (s->compr == TIFF_CCITT_RLE ||
205 s->compr == TIFF_G3 ||
206 s->compr == TIFF_G4) {
016c5b06 207 return tiff_unpack_fax(s, dst, stride, src, size, lines);
92799d32 208 }
0a467a9b
JR
209
210 bytestream2_init(&s->gb, src, size);
211 bytestream2_init_writer(&pb, dst, stride * lines);
212
f346b6bb 213 for (line = 0; line < lines; line++) {
0a467a9b
JR
214 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
215 break;
216 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
f346b6bb 217 switch (s->compr) {
a991b1fe 218 case TIFF_RAW:
b437f5b0 219 if (!s->fill_order) {
0a467a9b 220 bytestream2_copy_buffer(&pb, &s->gb, width);
b437f5b0
SS
221 } else {
222 int i;
223 for (i = 0; i < width; i++)
0a467a9b 224 bytestream2_put_byte(&pb, ff_reverse[bytestream2_get_byte(&s->gb)]);
b437f5b0 225 }
a991b1fe
KS
226 break;
227 case TIFF_PACKBITS:
f346b6bb 228 for (pixels = 0; pixels < width;) {
0a467a9b 229 code = ff_u8_to_s8(bytestream2_get_byte(&s->gb));
f346b6bb 230 if (code >= 0) {
a991b1fe 231 code++;
0a467a9b 232 bytestream2_copy_buffer(&pb, &s->gb, code);
a991b1fe 233 pixels += code;
f346b6bb 234 } else if (code != -128) { // -127..-1
a991b1fe 235 code = (-code) + 1;
0a467a9b
JR
236 c = bytestream2_get_byte(&s->gb);
237 bytestream2_set_buffer(&pb, c, code);
a991b1fe
KS
238 pixels += code;
239 }
240 }
241 break;
242 }
a991b1fe
KS
243 }
244 return 0;
245}
246
759001c5 247static int init_image(TiffContext *s, AVFrame *frame)
bbc572a2 248{
2597a842 249 int ret;
bbc572a2
SS
250
251 switch (s->bpp * 10 + s->bppcount) {
252 case 11:
716d413c 253 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
bbc572a2
SS
254 break;
255 case 81:
2597a842 256 s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
bbc572a2
SS
257 break;
258 case 243:
716d413c 259 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
bbc572a2
SS
260 break;
261 case 161:
716d413c 262 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
bbc572a2 263 break;
60cbd6ad
CEH
264 case 162:
265 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
266 break;
bcc5f69b
VG
267 case 322:
268 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
269 break;
bbc572a2 270 case 324:
716d413c 271 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
bbc572a2
SS
272 break;
273 case 483:
716d413c 274 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
bbc572a2
SS
275 break;
276 default:
277 av_log(s->avctx, AV_LOG_ERROR,
278 "This format is not supported (bpp=%d, bppcount=%d)\n",
279 s->bpp, s->bppcount);
280 return AVERROR_INVALIDDATA;
281 }
282 if (s->width != s->avctx->width || s->height != s->avctx->height) {
c265b8bb
AK
283 ret = ff_set_dimensions(s->avctx, s->width, s->height);
284 if (ret < 0)
bbc572a2 285 return ret;
bbc572a2 286 }
759001c5 287 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
bbc572a2
SS
288 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
289 return ret;
290 }
716d413c 291 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
2597a842 292 memcpy(frame->data[1], s->palette, sizeof(s->palette));
bbc572a2
SS
293 }
294 return 0;
295}
a991b1fe 296
0a467a9b 297static int tiff_decode_tag(TiffContext *s)
a991b1fe 298{
e32548d1 299 unsigned tag, type, count, off, value = 0;
0a467a9b 300 int i, start;
a991b1fe 301
0a467a9b 302 if (bytestream2_get_bytes_left(&s->gb) < 12)
b2a7b81b 303 return AVERROR_INVALIDDATA;
0a467a9b
JR
304 tag = tget_short(&s->gb, s->le);
305 type = tget_short(&s->gb, s->le);
306 count = tget_long(&s->gb, s->le);
307 off = tget_long(&s->gb, s->le);
308 start = bytestream2_tell(&s->gb);
a991b1fe 309
44736387 310 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
f346b6bb
AC
311 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
312 type);
44736387
AC
313 return 0;
314 }
315
f346b6bb
AC
316 if (count == 1) {
317 switch (type) {
a991b1fe
KS
318 case TIFF_BYTE:
319 case TIFF_SHORT:
0a467a9b
JR
320 bytestream2_seek(&s->gb, -4, SEEK_CUR);
321 value = tget(&s->gb, type, s->le);
a991b1fe
KS
322 break;
323 case TIFF_LONG:
324 value = off;
a991b1fe 325 break;
6a4583e9 326 case TIFF_STRING:
f346b6bb 327 if (count <= 4) {
0a467a9b 328 bytestream2_seek(&s->gb, -4, SEEK_CUR);
6a4583e9
KS
329 break;
330 }
a991b1fe 331 default:
e32548d1 332 value = UINT_MAX;
0a467a9b 333 bytestream2_seek(&s->gb, off, SEEK_SET);
a991b1fe 334 }
44736387 335 } else {
46ce9ded 336 if (count <= 4 && type_sizes[type] * count <= 4)
0a467a9b 337 bytestream2_seek(&s->gb, -4, SEEK_CUR);
46ce9ded 338 else
0a467a9b 339 bytestream2_seek(&s->gb, off, SEEK_SET);
a991b1fe
KS
340 }
341
f346b6bb 342 switch (tag) {
a991b1fe
KS
343 case TIFF_WIDTH:
344 s->width = value;
345 break;
346 case TIFF_HEIGHT:
347 s->height = value;
a991b1fe
KS
348 break;
349 case TIFF_BPP:
bbc572a2 350 s->bppcount = count;
f346b6bb
AC
351 if (count > 4) {
352 av_log(s->avctx, AV_LOG_ERROR,
353 "This format is not supported (bpp=%d, %d components)\n",
354 s->bpp, count);
b2a7b81b 355 return AVERROR_INVALIDDATA;
d381249b 356 }
f346b6bb
AC
357 if (count == 1)
358 s->bpp = value;
359 else {
360 switch (type) {
a991b1fe 361 case TIFF_BYTE:
f346b6bb
AC
362 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
363 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
a991b1fe
KS
364 break;
365 case TIFF_SHORT:
366 case TIFF_LONG:
ec8a152b 367 s->bpp = 0;
0a467a9b
JR
368 for (i = 0; i < count; i++)
369 s->bpp += tget(&s->gb, type, s->le);
a991b1fe
KS
370 break;
371 default:
372 s->bpp = -1;
373 }
374 }
bbc572a2
SS
375 break;
376 case TIFF_SAMPLES_PER_PIXEL:
377 if (count != 1) {
378 av_log(s->avctx, AV_LOG_ERROR,
379 "Samples per pixel requires a single value, many provided\n");
380 return AVERROR_INVALIDDATA;
e2570297 381 }
bbc572a2
SS
382 if (s->bppcount == 1)
383 s->bpp *= value;
384 s->bppcount = value;
a991b1fe
KS
385 break;
386 case TIFF_COMPR:
46ce9ded 387 s->compr = value;
108c96d5 388 s->predictor = 0;
f346b6bb 389 switch (s->compr) {
a991b1fe
KS
390 case TIFF_RAW:
391 case TIFF_PACKBITS:
ffc5430b 392 case TIFF_LZW:
92799d32
MN
393 case TIFF_CCITT_RLE:
394 break;
395 case TIFF_G3:
396 case TIFF_G4:
397 s->fax_opts = 0;
a991b1fe
KS
398 break;
399 case TIFF_DEFLATE:
400 case TIFF_ADOBE_DEFLATE:
b250f9c6 401#if CONFIG_ZLIB
a991b1fe
KS
402 break;
403#else
404 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
b2a7b81b 405 return AVERROR(ENOSYS);
a991b1fe 406#endif
27c18939
KS
407 case TIFF_JPEG:
408 case TIFF_NEWJPEG:
999ccd2d 409 avpriv_report_missing_feature(s->avctx, "JPEG compression");
b2a7b81b 410 return AVERROR_PATCHWELCOME;
a991b1fe 411 default:
f346b6bb
AC
412 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
413 s->compr);
b2a7b81b 414 return AVERROR_INVALIDDATA;
a991b1fe
KS
415 }
416 break;
417 case TIFF_ROWSPERSTRIP:
fdbe18b7
JR
418 if (!value || (type == TIFF_LONG && value == UINT_MAX))
419 value = s->height;
420 s->rps = FFMIN(value, s->height);
a991b1fe
KS
421 break;
422 case TIFF_STRIP_OFFS:
f346b6bb 423 if (count == 1) {
0a467a9b
JR
424 s->strippos = 0;
425 s->stripoff = value;
f346b6bb 426 } else
0a467a9b 427 s->strippos = off;
a991b1fe 428 s->strips = count;
f346b6bb
AC
429 if (s->strips == 1)
430 s->rps = s->height;
a991b1fe 431 s->sot = type;
a991b1fe
KS
432 break;
433 case TIFF_STRIP_SIZE:
f346b6bb 434 if (count == 1) {
0a467a9b
JR
435 s->stripsizesoff = 0;
436 s->stripsize = value;
437 s->strips = 1;
f346b6bb 438 } else {
0a467a9b 439 s->stripsizesoff = off;
a991b1fe
KS
440 }
441 s->strips = count;
108c96d5 442 s->sstype = type;
a991b1fe
KS
443 break;
444 case TIFF_PREDICTOR:
108c96d5 445 s->predictor = value;
a991b1fe 446 break;
a9b046fb 447 case TIFF_PHOTOMETRIC:
f346b6bb 448 switch (value) {
a9b046fb
JR
449 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
450 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
451 case TIFF_PHOTOMETRIC_RGB:
452 case TIFF_PHOTOMETRIC_PALETTE:
453 s->photometric = value;
d00026d0 454 break;
a9b046fb
JR
455 case TIFF_PHOTOMETRIC_ALPHA_MASK:
456 case TIFF_PHOTOMETRIC_SEPARATED:
457 case TIFF_PHOTOMETRIC_YCBCR:
458 case TIFF_PHOTOMETRIC_CIE_LAB:
459 case TIFF_PHOTOMETRIC_ICC_LAB:
460 case TIFF_PHOTOMETRIC_ITU_LAB:
461 case TIFF_PHOTOMETRIC_CFA:
462 case TIFF_PHOTOMETRIC_LOG_L:
463 case TIFF_PHOTOMETRIC_LOG_LUV:
464 case TIFF_PHOTOMETRIC_LINEAR_RAW:
465 avpriv_report_missing_feature(s->avctx,
466 "PhotometricInterpretation 0x%04X",
467 value);
468 return AVERROR_PATCHWELCOME;
d00026d0 469 default:
a9b046fb
JR
470 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
471 "unknown\n", value);
b2a7b81b 472 return AVERROR_INVALIDDATA;
e2570297
KS
473 }
474 break;
a58e96be 475 case TIFF_FILL_ORDER:
f346b6bb
AC
476 if (value < 1 || value > 2) {
477 av_log(s->avctx, AV_LOG_ERROR,
478 "Unknown FillOrder value %d, trying default one\n", value);
a58e96be
KS
479 value = 1;
480 }
481 s->fill_order = value - 1;
482 break;
0a467a9b
JR
483 case TIFF_PAL: {
484 GetByteContext pal_gb[3];
339aabd5 485 off = type_sizes[type];
0a467a9b
JR
486 if (count / 3 > 256 ||
487 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
b2a7b81b 488 return AVERROR_INVALIDDATA;
0a467a9b
JR
489 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
490 bytestream2_skip(&pal_gb[1], count / 3 * off);
491 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
339aabd5 492 off = (type_sizes[type] - 1) << 3;
f346b6bb 493 for (i = 0; i < count / 3; i++) {
58bc38a5 494 uint32_t p = 0xFF000000;
0a467a9b
JR
495 p |= (tget(&pal_gb[0], type, s->le) >> off) << 16;
496 p |= (tget(&pal_gb[1], type, s->le) >> off) << 8;
497 p |= tget(&pal_gb[2], type, s->le) >> off;
345a96c3 498 s->palette[i] = p;
e2570297 499 }
7ca5338b 500 s->palette_is_set = 1;
2878cecd 501 break;
0a467a9b 502 }
2878cecd 503 case TIFF_PLANAR:
f346b6bb 504 if (value == 2) {
999ccd2d 505 avpriv_report_missing_feature(s->avctx, "Planar format");
b2a7b81b 506 return AVERROR_PATCHWELCOME;
2878cecd
KS
507 }
508 break;
92799d32 509 case TIFF_T4OPTIONS:
f346b6bb 510 if (s->compr == TIFF_G3)
7c6f5238
KS
511 s->fax_opts = value;
512 break;
92799d32 513 case TIFF_T6OPTIONS:
f346b6bb 514 if (s->compr == TIFF_G4)
7c6f5238 515 s->fax_opts = value;
92799d32 516 break;
1863a3c7 517 default:
999ccd2d
LB
518 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
519 av_log(s->avctx, AV_LOG_ERROR,
520 "Unknown or unsupported tag %d/0X%0X\n",
521 tag, tag);
522 return AVERROR_INVALIDDATA;
523 }
a991b1fe 524 }
0a467a9b 525 bytestream2_seek(&s->gb, start, SEEK_SET);
a991b1fe
KS
526 return 0;
527}
528
529static int decode_frame(AVCodecContext *avctx,
df9b9567 530 void *data, int *got_frame, AVPacket *avpkt)
a991b1fe 531{
f346b6bb 532 TiffContext *const s = avctx->priv_data;
759001c5 533 AVFrame *const p = data;
04a845ca
LA
534 unsigned off;
535 int id, le, ret;
46ce9ded 536 int i, j, entries, stride;
04a845ca 537 unsigned soff, ssize;
108c96d5 538 uint8_t *dst;
0a467a9b
JR
539 GetByteContext stripsizes;
540 GetByteContext stripdata;
541
542 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
a991b1fe 543
46ce9ded 544 // parse image header
0a467a9b 545 if (avpkt->size < 8)
04a845ca 546 return AVERROR_INVALIDDATA;
0a467a9b 547 id = bytestream2_get_le16(&s->gb);
f346b6bb
AC
548 if (id == 0x4949)
549 le = 1;
550 else if (id == 0x4D4D)
551 le = 0;
552 else {
a991b1fe 553 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
b2a7b81b 554 return AVERROR_INVALIDDATA;
a991b1fe 555 }
a9b046fb
JR
556 s->le = le;
557 s->photometric = TIFF_PHOTOMETRIC_NONE;
558 s->compr = TIFF_RAW;
559 s->fill_order = 0;
a991b1fe
KS
560 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
561 // that further identifies the file as a TIFF file"
0a467a9b 562 if (tget_short(&s->gb, le) != 42) {
f346b6bb
AC
563 av_log(avctx, AV_LOG_ERROR,
564 "The answer to life, universe and everything is not correct!\n");
b2a7b81b 565 return AVERROR_INVALIDDATA;
a991b1fe 566 }
0a467a9b
JR
567 // Reset these offsets so we can tell if they were set this frame
568 s->stripsizesoff = s->strippos = 0;
a991b1fe 569 /* parse image file directory */
0a467a9b
JR
570 off = tget_long(&s->gb, le);
571 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
a991b1fe 572 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
04a845ca 573 return AVERROR_INVALIDDATA;
a991b1fe 574 }
0a467a9b
JR
575 bytestream2_seek(&s->gb, off, SEEK_SET);
576 entries = tget_short(&s->gb, le);
f346b6bb 577 for (i = 0; i < entries; i++) {
0a467a9b 578 if ((ret = tiff_decode_tag(s)) < 0)
b2a7b81b 579 return ret;
a991b1fe 580 }
0a467a9b 581 if (!s->strippos && !s->stripoff) {
108c96d5 582 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
b2a7b81b 583 return AVERROR_INVALIDDATA;
108c96d5
KS
584 }
585 /* now we have the data and may start decoding */
759001c5 586 if ((ret = init_image(s, p)) < 0)
bbc572a2
SS
587 return ret;
588
f346b6bb 589 if (s->strips == 1 && !s->stripsize) {
108c96d5 590 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
0a467a9b 591 s->stripsize = avpkt->size - s->stripoff;
108c96d5
KS
592 }
593 stride = p->linesize[0];
46ce9ded 594 dst = p->data[0];
0a467a9b
JR
595
596 if (s->stripsizesoff) {
597 if (s->stripsizesoff >= avpkt->size)
598 return AVERROR_INVALIDDATA;
599 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
600 avpkt->size - s->stripsizesoff);
601 }
602 if (s->strippos) {
603 if (s->strippos >= avpkt->size)
604 return AVERROR_INVALIDDATA;
605 bytestream2_init(&stripdata, avpkt->data + s->strippos,
606 avpkt->size - s->strippos);
607 }
608
f346b6bb 609 for (i = 0; i < s->height; i += s->rps) {
0a467a9b
JR
610 if (s->stripsizesoff)
611 ssize = tget(&stripsizes, s->sstype, le);
612 else
108c96d5
KS
613 ssize = s->stripsize;
614
0a467a9b
JR
615 if (s->strippos)
616 soff = tget(&stripdata, s->sot, le);
617 else
108c96d5 618 soff = s->stripoff;
04a845ca 619
0a467a9b 620 if (soff > avpkt->size || ssize > avpkt->size - soff) {
04a845ca 621 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
b2a7b81b 622 return AVERROR_INVALIDDATA;
a4db272a 623 }
0a467a9b 624 if ((ret = tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
999ccd2d
LB
625 FFMIN(s->rps, s->height - i))) < 0) {
626 if (avctx->err_recognition & AV_EF_EXPLODE)
627 return ret;
108c96d5 628 break;
999ccd2d 629 }
108c96d5
KS
630 dst += s->rps * stride;
631 }
f346b6bb 632 if (s->predictor == 2) {
46ce9ded
DB
633 dst = p->data[0];
634 soff = s->bpp >> 3;
108c96d5 635 ssize = s->width * soff;
df507d5a 636 if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE) {
e75ef2b7
JF
637 for (i = 0; i < s->height; i++) {
638 for (j = soff; j < ssize; j += 2)
639 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
640 dst += stride;
641 }
df507d5a 642 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE) {
e75ef2b7
JF
643 for (i = 0; i < s->height; i++) {
644 for (j = soff; j < ssize; j += 2)
645 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
646 dst += stride;
647 }
648 } else {
649 for (i = 0; i < s->height; i++) {
650 for (j = soff; j < ssize; j++)
651 dst[j] += dst[j - soff];
652 dst += stride;
653 }
108c96d5
KS
654 }
655 }
a991b1fe 656
a9b046fb 657 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
bf2064f0
JF
658 dst = p->data[0];
659 for (i = 0; i < s->height; i++) {
660 for (j = 0; j < p->linesize[0]; j++)
661 dst[j] = 255 - dst[j];
662 dst += stride;
e2570297
KS
663 }
664 }
df9b9567 665 *got_frame = 1;
a991b1fe 666
0a467a9b 667 return avpkt->size;
a991b1fe
KS
668}
669
f346b6bb
AC
670static av_cold int tiff_init(AVCodecContext *avctx)
671{
a991b1fe
KS
672 TiffContext *s = avctx->priv_data;
673
46ce9ded 674 s->width = 0;
a991b1fe 675 s->height = 0;
46ce9ded 676 s->avctx = avctx;
ffc5430b 677 ff_lzw_decode_open(&s->lzw);
92799d32 678 ff_ccitt_unpack_init();
a991b1fe
KS
679
680 return 0;
681}
682
98a6fff9 683static av_cold int tiff_end(AVCodecContext *avctx)
a991b1fe 684{
f346b6bb 685 TiffContext *const s = avctx->priv_data;
a991b1fe 686
ffc5430b 687 ff_lzw_decode_close(&s->lzw);
a991b1fe
KS
688 return 0;
689}
690
d36beb3f 691AVCodec ff_tiff_decoder = {
ec6402b7 692 .name = "tiff",
46ce9ded 693 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
ec6402b7 694 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 695 .id = AV_CODEC_ID_TIFF,
ec6402b7
AK
696 .priv_data_size = sizeof(TiffContext),
697 .init = tiff_init,
698 .close = tiff_end,
699 .decode = decode_frame,
700 .capabilities = CODEC_CAP_DR1,
a991b1fe 701};