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
38 #include "libavutil/attributes.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/imgutils.h"
42 typedef struct TiffContext {
43 AVCodecContext *avctx;
46 unsigned int bpp, bppcount;
47 uint32_t palette[256];
56 int strips, rps, sstype;
58 const uint8_t *stripdata;
59 const uint8_t *stripsizes;
60 int stripsize, stripoff;
64 static unsigned tget_short(const uint8_t **p, int le)
66 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
71 static unsigned tget_long(const uint8_t **p, int le)
73 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
78 static unsigned tget(const uint8_t **p, int type, int le)
81 case TIFF_BYTE : return *(*p)++;
82 case TIFF_SHORT: return tget_short(p, le);
83 case TIFF_LONG : return tget_long(p, le);
84 default : return UINT_MAX;
89 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
92 z_stream zstream = { 0 };
95 zstream.next_in = src;
96 zstream.avail_in = size;
97 zstream.next_out = dst;
98 zstream.avail_out = *len;
99 zret = inflateInit(&zstream);
101 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
104 zret = inflate(&zstream, Z_SYNC_FLUSH);
105 inflateEnd(&zstream);
106 *len = zstream.total_out;
107 return zret == Z_STREAM_END ? Z_OK : zret;
111 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
112 const uint8_t *src, int size, int lines)
114 int c, line, pixels, code, ret;
115 const uint8_t *ssrc = src;
116 int width = ((s->width * s->bpp) + 7) >> 3;
119 return AVERROR_INVALIDDATA;
122 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
124 unsigned long outlen;
126 outlen = width * lines;
127 zbuf = av_malloc(outlen);
129 return AVERROR(ENOMEM);
130 ret = tiff_uncompress(zbuf, &outlen, src, size);
132 av_log(s->avctx, AV_LOG_ERROR,
133 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
134 (unsigned long)width * lines, ret);
136 return AVERROR_UNKNOWN;
139 for (line = 0; line < lines; line++) {
140 memcpy(dst, src, width);
148 if (s->compr == TIFF_LZW) {
149 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
150 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
154 if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
155 || s->compr == TIFF_G4) {
157 uint8_t *src2 = av_malloc((unsigned)size +
158 FF_INPUT_BUFFER_PADDING_SIZE);
161 av_log(s->avctx, AV_LOG_ERROR,
162 "Error allocating temporary buffer\n");
163 return AVERROR(ENOMEM);
165 if (s->fax_opts & 2) {
166 av_log(s->avctx, AV_LOG_ERROR,
167 "Uncompressed fax mode is not supported (yet)\n");
169 return AVERROR_INVALIDDATA;
171 if (!s->fill_order) {
172 memcpy(src2, src, size);
174 for (i = 0; i < size; i++)
175 src2[i] = ff_reverse[src[i]];
177 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
182 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
183 s->compr, s->fax_opts);
189 for (line = 0; line < lines; line++) {
190 if (src - ssrc > size) {
191 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
192 return AVERROR_INVALIDDATA;
196 if (ssrc + size - src < width)
197 return AVERROR_INVALIDDATA;
198 if (!s->fill_order) {
199 memcpy(dst, src, width);
202 for (i = 0; i < width; i++)
203 dst[i] = ff_reverse[src[i]];
208 for (pixels = 0; pixels < width;) {
209 code = (int8_t) * src++;
212 if (pixels + code > width) {
213 av_log(s->avctx, AV_LOG_ERROR,
214 "Copy went out of bounds\n");
215 return AVERROR_INVALIDDATA;
217 memcpy(dst + pixels, src, code);
220 } else if (code != -128) { // -127..-1
222 if (pixels + code > width) {
223 av_log(s->avctx, AV_LOG_ERROR,
224 "Run went out of bounds\n");
225 return AVERROR_INVALIDDATA;
228 memset(dst + pixels, c, code);
234 pixels = ff_lzw_decode(s->lzw, dst, width);
235 if (pixels < width) {
236 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
238 return AVERROR_INVALIDDATA;
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 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
280 avcodec_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, 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)
308 return AVERROR_INVALIDDATA;
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");
352 return AVERROR_INVALIDDATA;
365 av_log(s->avctx, AV_LOG_ERROR,
366 "This format is not supported (bpp=%d, %d components)\n",
368 return AVERROR_INVALIDDATA;
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");
418 return AVERROR(ENOSYS);
422 av_log(s->avctx, AV_LOG_ERROR,
423 "JPEG compression is not supported\n");
424 return AVERROR_PATCHWELCOME;
426 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
428 return AVERROR_INVALIDDATA;
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");
437 return AVERROR_INVALIDDATA;
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");
454 return AVERROR_INVALIDDATA;
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");
470 return AVERROR_INVALIDDATA;
474 s->predictor = value;
488 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
490 return AVERROR_INVALIDDATA;
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)
505 return AVERROR_INVALIDDATA;
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");
521 return AVERROR_PATCHWELCOME;
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 *got_frame, AVPacket *avpkt)
542 const uint8_t *buf = avpkt->data;
543 int buf_size = avpkt->size;
544 TiffContext *const s = avctx->priv_data;
545 AVFrame *const p = data;
546 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
551 unsigned soff, ssize;
555 if (end_buf - buf < 8)
556 return AVERROR_INVALIDDATA;
561 else if (id == 0x4D4D)
564 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
565 return AVERROR_INVALIDDATA;
571 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
572 // that further identifies the file as a TIFF file"
573 if (tget_short(&buf, le) != 42) {
574 av_log(avctx, AV_LOG_ERROR,
575 "The answer to life, universe and everything is not correct!\n");
576 return AVERROR_INVALIDDATA;
578 // Reset these pointers so we can tell if they were set this frame
579 s->stripsizes = s->stripdata = NULL;
580 /* parse image file directory */
581 off = tget_long(&buf, le);
582 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
583 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
584 return AVERROR_INVALIDDATA;
586 buf = orig_buf + off;
587 entries = tget_short(&buf, le);
588 for (i = 0; i < entries; i++) {
589 if ((ret = tiff_decode_tag(s, orig_buf, buf, end_buf)) < 0)
593 if (!s->stripdata && !s->stripoff) {
594 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
595 return AVERROR_INVALIDDATA;
597 /* now we have the data and may start decoding */
598 if ((ret = init_image(s, p)) < 0)
601 if (s->strips == 1 && !s->stripsize) {
602 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
603 s->stripsize = buf_size - s->stripoff;
605 stride = p->linesize[0];
607 for (i = 0; i < s->height; i += s->rps) {
609 if (s->stripsizes >= end_buf)
610 return AVERROR_INVALIDDATA;
611 ssize = tget(&s->stripsizes, s->sstype, s->le);
613 ssize = s->stripsize;
616 if (s->stripdata >= end_buf)
617 return AVERROR_INVALIDDATA;
618 soff = tget(&s->stripdata, s->sot, s->le);
622 if (soff > buf_size || ssize > buf_size - soff) {
623 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
624 return AVERROR_INVALIDDATA;
626 if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
627 FFMIN(s->rps, s->height - i)) < 0)
629 dst += s->rps * stride;
631 if (s->predictor == 2) {
634 ssize = s->width * soff;
635 for (i = 0; i < s->height; i++) {
636 for (j = soff; j < ssize; j++)
637 dst[j] += dst[j - soff];
647 for (j = 0; j < s->height; j++) {
648 for (i = 0; i < p->linesize[0]; i++)
649 src[i] = 255 - src[i];
650 src += p->linesize[0];
658 static av_cold int tiff_init(AVCodecContext *avctx)
660 TiffContext *s = avctx->priv_data;
665 ff_lzw_decode_open(&s->lzw);
666 ff_ccitt_unpack_init();
671 static av_cold int tiff_end(AVCodecContext *avctx)
673 TiffContext *const s = avctx->priv_data;
675 ff_lzw_decode_close(&s->lzw);
679 AVCodec ff_tiff_decoder = {
681 .type = AVMEDIA_TYPE_VIDEO,
682 .id = AV_CODEC_ID_TIFF,
683 .priv_data_size = sizeof(TiffContext),
686 .decode = decode_frame,
687 .capabilities = CODEC_CAP_DR1,
688 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),