3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Konstantin Shishkov
33 #include "libavutil/attributes.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/imgutils.h"
37 #include "bytestream.h"
44 typedef struct TiffContext {
45 AVCodecContext *avctx;
49 unsigned int bpp, bppcount;
50 uint32_t palette[256];
54 enum TiffPhotometric photometric;
59 int strips, rps, sstype;
61 int stripsizesoff, stripsize, stripoff, strippos;
65 static unsigned tget_short(GetByteContext *gb, int le)
67 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
70 static unsigned tget_long(GetByteContext *gb, int le)
72 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
75 static unsigned tget(GetByteContext *gb, int type, int le)
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;
86 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
89 z_stream zstream = { 0 };
92 zstream.next_in = src;
93 zstream.avail_in = size;
94 zstream.next_out = dst;
95 zstream.avail_out = *len;
96 zret = inflateInit(&zstream);
98 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
101 zret = inflate(&zstream, Z_SYNC_FLUSH);
102 inflateEnd(&zstream);
103 *len = zstream.total_out;
104 return zret == Z_STREAM_END ? Z_OK : zret;
107 static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
108 const uint8_t *src, int size,
109 int width, int lines)
112 unsigned long outlen;
114 outlen = width * lines;
115 zbuf = av_malloc(outlen);
117 return AVERROR(ENOMEM);
118 ret = tiff_uncompress(zbuf, &outlen, src, size);
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);
124 return AVERROR_UNKNOWN;
127 for (line = 0; line < lines; line++) {
128 memcpy(dst, src, width);
138 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
139 const uint8_t *src, int size, int lines)
142 uint8_t *src2 = av_malloc((unsigned)size +
143 FF_INPUT_BUFFER_PADDING_SIZE);
146 av_log(s->avctx, AV_LOG_ERROR,
147 "Error allocating temporary buffer\n");
148 return AVERROR(ENOMEM);
150 if (s->fax_opts & 2) {
151 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
153 return AVERROR_PATCHWELCOME;
155 if (!s->fill_order) {
156 memcpy(src2, src, size);
158 for (i = 0; i < size; i++)
159 src2[i] = ff_reverse[src[i]];
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);
168 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
169 const uint8_t *src, int size, int lines)
172 int c, line, pixels, code, ret;
173 int width = ((s->width * s->bpp) + 7) >> 3;
176 return AVERROR_INVALIDDATA;
178 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
180 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
182 av_log(s->avctx, AV_LOG_ERROR,
183 "zlib support not enabled, "
184 "deflate compression not supported\n");
185 return AVERROR(ENOSYS);
188 if (s->compr == TIFF_LZW) {
189 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
190 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
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",
198 return AVERROR_INVALIDDATA;
204 if (s->compr == TIFF_CCITT_RLE ||
205 s->compr == TIFF_G3 ||
206 s->compr == TIFF_G4) {
207 return tiff_unpack_fax(s, dst, stride, src, size, lines);
210 bytestream2_init(&s->gb, src, size);
211 bytestream2_init_writer(&pb, dst, stride * lines);
213 for (line = 0; line < lines; line++) {
214 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
216 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
219 if (!s->fill_order) {
220 bytestream2_copy_buffer(&pb, &s->gb, width);
223 for (i = 0; i < width; i++)
224 bytestream2_put_byte(&pb, ff_reverse[bytestream2_get_byte(&s->gb)]);
228 for (pixels = 0; pixels < width;) {
229 code = ff_u8_to_s8(bytestream2_get_byte(&s->gb));
232 bytestream2_copy_buffer(&pb, &s->gb, code);
234 } else if (code != -128) { // -127..-1
236 c = bytestream2_get_byte(&s->gb);
237 bytestream2_set_buffer(&pb, c, code);
247 static int init_image(TiffContext *s, AVFrame *frame)
252 switch (s->bpp * 10 + s->bppcount) {
254 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
257 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
260 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
263 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
266 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
269 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
272 av_log(s->avctx, AV_LOG_ERROR,
273 "This format is not supported (bpp=%d, bppcount=%d)\n",
274 s->bpp, s->bppcount);
275 return AVERROR_INVALIDDATA;
277 if (s->width != s->avctx->width || s->height != s->avctx->height) {
278 ret = ff_set_dimensions(s->avctx, s->width, s->height);
282 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
283 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
286 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
287 if (s->palette_is_set) {
288 memcpy(frame->data[1], s->palette, sizeof(s->palette));
290 /* make default grayscale pal */
291 pal = (uint32_t *) frame->data[1];
292 for (i = 0; i < 256; i++)
293 pal[i] = i * 0x010101;
299 static int tiff_decode_tag(TiffContext *s)
301 unsigned tag, type, count, off, value = 0;
304 if (bytestream2_get_bytes_left(&s->gb) < 12)
305 return AVERROR_INVALIDDATA;
306 tag = tget_short(&s->gb, s->le);
307 type = tget_short(&s->gb, s->le);
308 count = tget_long(&s->gb, s->le);
309 off = tget_long(&s->gb, s->le);
310 start = bytestream2_tell(&s->gb);
312 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
313 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
322 bytestream2_seek(&s->gb, -4, SEEK_CUR);
323 value = tget(&s->gb, type, s->le);
330 bytestream2_seek(&s->gb, -4, SEEK_CUR);
335 bytestream2_seek(&s->gb, off, SEEK_SET);
338 if (count <= 4 && type_sizes[type] * count <= 4)
339 bytestream2_seek(&s->gb, -4, SEEK_CUR);
341 bytestream2_seek(&s->gb, off, SEEK_SET);
354 av_log(s->avctx, AV_LOG_ERROR,
355 "This format is not supported (bpp=%d, %d components)\n",
357 return AVERROR_INVALIDDATA;
364 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
365 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
370 for (i = 0; i < count; i++)
371 s->bpp += tget(&s->gb, type, s->le);
378 case TIFF_SAMPLES_PER_PIXEL:
380 av_log(s->avctx, AV_LOG_ERROR,
381 "Samples per pixel requires a single value, many provided\n");
382 return AVERROR_INVALIDDATA;
384 if (s->bppcount == 1)
402 case TIFF_ADOBE_DEFLATE:
406 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
407 return AVERROR(ENOSYS);
411 avpriv_report_missing_feature(s->avctx, "JPEG compression");
412 return AVERROR_PATCHWELCOME;
414 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
416 return AVERROR_INVALIDDATA;
419 case TIFF_ROWSPERSTRIP:
420 if (!value || (type == TIFF_LONG && value == UINT_MAX))
422 s->rps = FFMIN(value, s->height);
424 case TIFF_STRIP_OFFS:
435 case TIFF_STRIP_SIZE:
437 s->stripsizesoff = 0;
438 s->stripsize = value;
441 s->stripsizesoff = off;
447 s->predictor = value;
449 case TIFF_PHOTOMETRIC:
451 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
452 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
453 case TIFF_PHOTOMETRIC_RGB:
454 case TIFF_PHOTOMETRIC_PALETTE:
455 s->photometric = value;
457 case TIFF_PHOTOMETRIC_ALPHA_MASK:
458 case TIFF_PHOTOMETRIC_SEPARATED:
459 case TIFF_PHOTOMETRIC_YCBCR:
460 case TIFF_PHOTOMETRIC_CIE_LAB:
461 case TIFF_PHOTOMETRIC_ICC_LAB:
462 case TIFF_PHOTOMETRIC_ITU_LAB:
463 case TIFF_PHOTOMETRIC_CFA:
464 case TIFF_PHOTOMETRIC_LOG_L:
465 case TIFF_PHOTOMETRIC_LOG_LUV:
466 case TIFF_PHOTOMETRIC_LINEAR_RAW:
467 avpriv_report_missing_feature(s->avctx,
468 "PhotometricInterpretation 0x%04X",
470 return AVERROR_PATCHWELCOME;
472 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
474 return AVERROR_INVALIDDATA;
477 case TIFF_FILL_ORDER:
478 if (value < 1 || value > 2) {
479 av_log(s->avctx, AV_LOG_ERROR,
480 "Unknown FillOrder value %d, trying default one\n", value);
483 s->fill_order = value - 1;
486 GetByteContext pal_gb[3];
487 off = type_sizes[type];
488 if (count / 3 > 256 ||
489 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
490 return AVERROR_INVALIDDATA;
491 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
492 bytestream2_skip(&pal_gb[1], count / 3 * off);
493 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
494 off = (type_sizes[type] - 1) << 3;
495 for (i = 0; i < count / 3; i++) {
496 uint32_t p = 0xFF000000;
497 p |= (tget(&pal_gb[0], type, s->le) >> off) << 16;
498 p |= (tget(&pal_gb[1], type, s->le) >> off) << 8;
499 p |= tget(&pal_gb[2], type, s->le) >> off;
502 s->palette_is_set = 1;
507 avpriv_report_missing_feature(s->avctx, "Planar format");
508 return AVERROR_PATCHWELCOME;
512 if (s->compr == TIFF_G3)
516 if (s->compr == TIFF_G4)
520 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
521 av_log(s->avctx, AV_LOG_ERROR,
522 "Unknown or unsupported tag %d/0X%0X\n",
524 return AVERROR_INVALIDDATA;
527 bytestream2_seek(&s->gb, start, SEEK_SET);
531 static int decode_frame(AVCodecContext *avctx,
532 void *data, int *got_frame, AVPacket *avpkt)
534 TiffContext *const s = avctx->priv_data;
535 AVFrame *const p = data;
538 int i, j, entries, stride;
539 unsigned soff, ssize;
541 GetByteContext stripsizes;
542 GetByteContext stripdata;
544 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
546 // parse image header
548 return AVERROR_INVALIDDATA;
549 id = bytestream2_get_le16(&s->gb);
552 else if (id == 0x4D4D)
555 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
556 return AVERROR_INVALIDDATA;
559 s->photometric = TIFF_PHOTOMETRIC_NONE;
562 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
563 // that further identifies the file as a TIFF file"
564 if (tget_short(&s->gb, le) != 42) {
565 av_log(avctx, AV_LOG_ERROR,
566 "The answer to life, universe and everything is not correct!\n");
567 return AVERROR_INVALIDDATA;
569 // Reset these offsets so we can tell if they were set this frame
570 s->stripsizesoff = s->strippos = 0;
571 /* parse image file directory */
572 off = tget_long(&s->gb, le);
573 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
574 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
575 return AVERROR_INVALIDDATA;
577 bytestream2_seek(&s->gb, off, SEEK_SET);
578 entries = tget_short(&s->gb, le);
579 for (i = 0; i < entries; i++) {
580 if ((ret = tiff_decode_tag(s)) < 0)
583 if (!s->strippos && !s->stripoff) {
584 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
585 return AVERROR_INVALIDDATA;
587 /* now we have the data and may start decoding */
588 if ((ret = init_image(s, p)) < 0)
591 if (s->strips == 1 && !s->stripsize) {
592 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
593 s->stripsize = avpkt->size - s->stripoff;
595 stride = p->linesize[0];
598 if (s->stripsizesoff) {
599 if (s->stripsizesoff >= avpkt->size)
600 return AVERROR_INVALIDDATA;
601 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
602 avpkt->size - s->stripsizesoff);
605 if (s->strippos >= avpkt->size)
606 return AVERROR_INVALIDDATA;
607 bytestream2_init(&stripdata, avpkt->data + s->strippos,
608 avpkt->size - s->strippos);
611 for (i = 0; i < s->height; i += s->rps) {
612 if (s->stripsizesoff)
613 ssize = tget(&stripsizes, s->sstype, le);
615 ssize = s->stripsize;
618 soff = tget(&stripdata, s->sot, le);
622 if (soff > avpkt->size || ssize > avpkt->size - soff) {
623 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
624 return AVERROR_INVALIDDATA;
626 if ((ret = tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
627 FFMIN(s->rps, s->height - i))) < 0) {
628 if (avctx->err_recognition & AV_EF_EXPLODE)
632 dst += s->rps * stride;
634 if (s->predictor == 2) {
637 ssize = s->width * soff;
638 if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE) {
639 for (i = 0; i < s->height; i++) {
640 for (j = soff; j < ssize; j += 2)
641 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
644 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE) {
645 for (i = 0; i < s->height; i++) {
646 for (j = soff; j < ssize; j += 2)
647 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
651 for (i = 0; i < s->height; i++) {
652 for (j = soff; j < ssize; j++)
653 dst[j] += dst[j - soff];
659 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
661 for (i = 0; i < s->height; i++) {
662 for (j = 0; j < p->linesize[0]; j++)
663 dst[j] = 255 - dst[j];
672 static av_cold int tiff_init(AVCodecContext *avctx)
674 TiffContext *s = avctx->priv_data;
679 ff_lzw_decode_open(&s->lzw);
680 ff_ccitt_unpack_init();
685 static av_cold int tiff_end(AVCodecContext *avctx)
687 TiffContext *const s = avctx->priv_data;
689 ff_lzw_decode_close(&s->lzw);
693 AVCodec ff_tiff_decoder = {
695 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
696 .type = AVMEDIA_TYPE_VIDEO,
697 .id = AV_CODEC_ID_TIFF,
698 .priv_data_size = sizeof(TiffContext),
701 .decode = decode_frame,
702 .capabilities = CODEC_CAP_DR1,