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
35 #include "libavutil/common.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/imgutils.h"
39 typedef struct TiffContext {
40 AVCodecContext *avctx;
44 unsigned int bpp, bppcount;
45 uint32_t palette[256];
54 int strips, rps, sstype;
56 const uint8_t *stripdata;
57 const uint8_t *stripsizes;
58 int stripsize, stripoff;
62 static unsigned tget_short(const uint8_t **p, int le)
64 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
69 static unsigned tget_long(const uint8_t **p, int le)
71 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
76 static unsigned tget(const uint8_t **p, int type, int le)
79 case TIFF_BYTE : return *(*p)++;
80 case TIFF_SHORT: return tget_short(p, le);
81 case TIFF_LONG : return tget_long(p, le);
82 default : return UINT_MAX;
87 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
90 z_stream zstream = { 0 };
93 zstream.next_in = src;
94 zstream.avail_in = size;
95 zstream.next_out = dst;
96 zstream.avail_out = *len;
97 zret = inflateInit(&zstream);
99 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
102 zret = inflate(&zstream, Z_SYNC_FLUSH);
103 inflateEnd(&zstream);
104 *len = zstream.total_out;
105 return zret == Z_STREAM_END ? Z_OK : zret;
109 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
110 const uint8_t *src, int size, int lines)
112 int c, line, pixels, code;
113 const uint8_t *ssrc = src;
114 int width = ((s->width * s->bpp) + 7) >> 3;
117 return AVERROR_INVALIDDATA;
120 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
122 unsigned long outlen;
124 outlen = width * lines;
125 zbuf = av_malloc(outlen);
127 return AVERROR(ENOMEM);
128 ret = tiff_uncompress(zbuf, &outlen, src, size);
130 av_log(s->avctx, AV_LOG_ERROR,
131 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
132 (unsigned long)width * lines, ret);
137 for (line = 0; line < lines; line++) {
138 memcpy(dst, src, width);
146 if (s->compr == TIFF_LZW) {
147 if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
148 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
152 if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
153 || s->compr == TIFF_G4) {
155 uint8_t *src2 = av_malloc((unsigned)size +
156 FF_INPUT_BUFFER_PADDING_SIZE);
159 av_log(s->avctx, AV_LOG_ERROR,
160 "Error allocating temporary buffer\n");
161 return AVERROR(ENOMEM);
163 if (s->fax_opts & 2) {
164 av_log(s->avctx, AV_LOG_ERROR,
165 "Uncompressed fax mode is not supported (yet)\n");
169 if (!s->fill_order) {
170 memcpy(src2, src, size);
172 for (i = 0; i < size; i++)
173 src2[i] = av_reverse[src[i]];
175 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
180 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
181 s->compr, s->fax_opts);
187 for (line = 0; line < lines; line++) {
188 if (src - ssrc > size) {
189 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
194 if (ssrc + size - src < width)
195 return AVERROR_INVALIDDATA;
196 if (!s->fill_order) {
197 memcpy(dst, src, width);
200 for (i = 0; i < width; i++)
201 dst[i] = av_reverse[src[i]];
206 for (pixels = 0; pixels < width;) {
207 code = (int8_t) * src++;
210 if (pixels + code > width) {
211 av_log(s->avctx, AV_LOG_ERROR,
212 "Copy went out of bounds\n");
215 memcpy(dst + pixels, src, code);
218 } else if (code != -128) { // -127..-1
220 if (pixels + code > width) {
221 av_log(s->avctx, AV_LOG_ERROR,
222 "Run went out of bounds\n");
226 memset(dst + pixels, c, code);
232 pixels = ff_lzw_decode(s->lzw, dst, width);
233 if (pixels < width) {
234 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
245 static int init_image(TiffContext *s)
250 switch (s->bpp * 10 + s->bppcount) {
252 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
255 s->avctx->pix_fmt = PIX_FMT_PAL8;
258 s->avctx->pix_fmt = PIX_FMT_RGB24;
261 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
264 s->avctx->pix_fmt = PIX_FMT_RGBA;
267 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
270 av_log(s->avctx, AV_LOG_ERROR,
271 "This format is not supported (bpp=%d, bppcount=%d)\n",
272 s->bpp, s->bppcount);
273 return AVERROR_INVALIDDATA;
275 if (s->width != s->avctx->width || s->height != s->avctx->height) {
276 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
278 avcodec_set_dimensions(s->avctx, s->width, s->height);
280 if (s->picture.data[0])
281 s->avctx->release_buffer(s->avctx, &s->picture);
282 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
283 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
286 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
287 if (s->palette_is_set) {
288 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
290 /* make default grayscale pal */
291 pal = (uint32_t *) s->picture.data[1];
292 for (i = 0; i < 256; i++)
293 pal[i] = i * 0x010101;
299 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
300 const uint8_t *buf, const uint8_t *end_buf)
302 unsigned tag, type, count, off, value = 0;
305 const uint8_t *rp, *gp, *bp;
307 if (end_buf - buf < 12)
309 tag = tget_short(&buf, s->le);
310 type = tget_short(&buf, s->le);
311 count = tget_long(&buf, s->le);
312 off = tget_long(&buf, s->le);
314 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
315 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
325 value = tget(&buf, type, s->le);
342 if (count <= 4 && type_sizes[type] * count <= 4) {
349 if (buf && (buf < start || buf > end_buf)) {
350 av_log(s->avctx, AV_LOG_ERROR,
351 "Tag referencing position outside the image\n");
365 av_log(s->avctx, AV_LOG_ERROR,
366 "This format is not supported (bpp=%d, %d components)\n",
375 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
376 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
381 for (i = 0; i < count && buf < end_buf; i++)
382 s->bpp += tget(&buf, type, s->le);
389 case TIFF_SAMPLES_PER_PIXEL:
391 av_log(s->avctx, AV_LOG_ERROR,
392 "Samples per pixel requires a single value, many provided\n");
393 return AVERROR_INVALIDDATA;
395 if (s->bppcount == 1)
413 case TIFF_ADOBE_DEFLATE:
417 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
422 av_log(s->avctx, AV_LOG_ERROR,
423 "JPEG compression is not supported\n");
426 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
431 case TIFF_ROWSPERSTRIP:
432 if (type == TIFF_LONG && value == UINT_MAX)
433 value = s->avctx->height;
435 av_log(s->avctx, AV_LOG_ERROR,
436 "Incorrect value of rows per strip\n");
441 case TIFF_STRIP_OFFS:
446 s->stripdata = start + off;
451 if (s->stripdata > end_buf) {
452 av_log(s->avctx, AV_LOG_ERROR,
453 "Tag referencing position outside the image\n");
457 case TIFF_STRIP_SIZE:
459 s->stripsizes = NULL;
460 s->stripsize = value;
463 s->stripsizes = start + off;
467 if (s->stripsizes > end_buf) {
468 av_log(s->avctx, AV_LOG_ERROR,
469 "Tag referencing position outside the image\n");
474 s->predictor = value;
488 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
493 case TIFF_FILL_ORDER:
494 if (value < 1 || value > 2) {
495 av_log(s->avctx, AV_LOG_ERROR,
496 "Unknown FillOrder value %d, trying default one\n", value);
499 s->fill_order = value - 1;
502 pal = (uint32_t *) s->palette;
503 off = type_sizes[type];
504 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
507 gp = buf + count / 3 * off;
508 bp = buf + count / 3 * off * 2;
509 off = (type_sizes[type] - 1) << 3;
510 for (i = 0; i < count / 3; i++) {
511 j = (tget(&rp, type, s->le) >> off) << 16;
512 j |= (tget(&gp, type, s->le) >> off) << 8;
513 j |= tget(&bp, type, s->le) >> off;
516 s->palette_is_set = 1;
520 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
525 if (s->compr == TIFF_G3)
529 if (s->compr == TIFF_G4)
533 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
539 static int decode_frame(AVCodecContext *avctx,
540 void *data, int *data_size, AVPacket *avpkt)
542 const uint8_t *buf = avpkt->data;
543 int buf_size = avpkt->size;
544 TiffContext *const s = avctx->priv_data;
545 AVFrame *picture = data;
546 AVFrame *const p = &s->picture;
547 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
552 unsigned soff, ssize;
556 if (end_buf - buf < 8)
557 return AVERROR_INVALIDDATA;
562 else if (id == 0x4D4D)
565 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
572 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
573 // that further identifies the file as a TIFF file"
574 if (tget_short(&buf, le) != 42) {
575 av_log(avctx, AV_LOG_ERROR,
576 "The answer to life, universe and everything is not correct!\n");
579 // Reset these pointers so we can tell if they were set this frame
580 s->stripsizes = s->stripdata = NULL;
581 /* parse image file directory */
582 off = tget_long(&buf, le);
583 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
584 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
585 return AVERROR_INVALIDDATA;
587 buf = orig_buf + off;
588 entries = tget_short(&buf, le);
589 for (i = 0; i < entries; i++) {
590 if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
594 if (!s->stripdata && !s->stripoff) {
595 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
598 /* now we have the data and may start decoding */
599 if ((ret = init_image(s)) < 0)
602 if (s->strips == 1 && !s->stripsize) {
603 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
604 s->stripsize = buf_size - s->stripoff;
606 stride = p->linesize[0];
608 for (i = 0; i < s->height; i += s->rps) {
610 if (s->stripsizes >= end_buf)
611 return AVERROR_INVALIDDATA;
612 ssize = tget(&s->stripsizes, s->sstype, s->le);
614 ssize = s->stripsize;
617 if (s->stripdata >= end_buf)
618 return AVERROR_INVALIDDATA;
619 soff = tget(&s->stripdata, s->sot, s->le);
623 if (soff > buf_size || ssize > buf_size - soff) {
624 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
627 if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
628 FFMIN(s->rps, s->height - i)) < 0)
630 dst += s->rps * stride;
632 if (s->predictor == 2) {
635 ssize = s->width * soff;
636 for (i = 0; i < s->height; i++) {
637 for (j = soff; j < ssize; j++)
638 dst[j] += dst[j - soff];
647 src = s->picture.data[0];
648 for (j = 0; j < s->height; j++) {
649 for (i = 0; i < s->picture.linesize[0]; i++)
650 src[i] = 255 - src[i];
651 src += s->picture.linesize[0];
654 *picture = s->picture;
655 *data_size = sizeof(AVPicture);
660 static av_cold int tiff_init(AVCodecContext *avctx)
662 TiffContext *s = avctx->priv_data;
667 avcodec_get_frame_defaults(&s->picture);
668 avctx->coded_frame = &s->picture;
669 ff_lzw_decode_open(&s->lzw);
670 ff_ccitt_unpack_init();
675 static av_cold int tiff_end(AVCodecContext *avctx)
677 TiffContext *const s = avctx->priv_data;
679 ff_lzw_decode_close(&s->lzw);
680 if (s->picture.data[0])
681 avctx->release_buffer(avctx, &s->picture);
685 AVCodec ff_tiff_decoder = {
687 .type = AVMEDIA_TYPE_VIDEO,
689 .priv_data_size = sizeof(TiffContext),
692 .decode = decode_frame,
693 .capabilities = CODEC_CAP_DR1,
694 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),