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;
47 unsigned int bpp, bppcount;
48 uint32_t palette[256];
57 int strips, rps, sstype;
59 const uint8_t *stripdata;
60 const uint8_t *stripsizes;
61 int stripsize, stripoff;
65 static unsigned tget_short(const uint8_t **p, int le)
67 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
72 static unsigned tget_long(const uint8_t **p, int le)
74 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
79 static unsigned tget(const uint8_t **p, int type, int le)
82 case TIFF_BYTE : return *(*p)++;
83 case TIFF_SHORT: return tget_short(p, le);
84 case TIFF_LONG : return tget_long(p, le);
85 default : return UINT_MAX;
90 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
93 z_stream zstream = { 0 };
96 zstream.next_in = src;
97 zstream.avail_in = size;
98 zstream.next_out = dst;
99 zstream.avail_out = *len;
100 zret = inflateInit(&zstream);
102 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
105 zret = inflate(&zstream, Z_SYNC_FLUSH);
106 inflateEnd(&zstream);
107 *len = zstream.total_out;
108 return zret == Z_STREAM_END ? Z_OK : zret;
112 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
113 const uint8_t *src, int size, int lines)
115 int c, line, pixels, code, ret;
116 const uint8_t *ssrc = src;
117 int width = ((s->width * s->bpp) + 7) >> 3;
120 return AVERROR_INVALIDDATA;
123 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
125 unsigned long outlen;
127 outlen = width * lines;
128 zbuf = av_malloc(outlen);
130 return AVERROR(ENOMEM);
131 ret = tiff_uncompress(zbuf, &outlen, src, size);
133 av_log(s->avctx, AV_LOG_ERROR,
134 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
135 (unsigned long)width * lines, ret);
137 return AVERROR_UNKNOWN;
140 for (line = 0; line < lines; line++) {
141 memcpy(dst, src, width);
149 if (s->compr == TIFF_LZW) {
150 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
151 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
155 if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
156 || s->compr == TIFF_G4) {
158 uint8_t *src2 = av_malloc((unsigned)size +
159 FF_INPUT_BUFFER_PADDING_SIZE);
162 av_log(s->avctx, AV_LOG_ERROR,
163 "Error allocating temporary buffer\n");
164 return AVERROR(ENOMEM);
166 if (s->fax_opts & 2) {
167 av_log(s->avctx, AV_LOG_ERROR,
168 "Uncompressed fax mode is not supported (yet)\n");
170 return AVERROR_INVALIDDATA;
172 if (!s->fill_order) {
173 memcpy(src2, src, size);
175 for (i = 0; i < size; i++)
176 src2[i] = ff_reverse[src[i]];
178 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
183 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
184 s->compr, s->fax_opts);
190 for (line = 0; line < lines; line++) {
191 if (src - ssrc > size) {
192 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
193 return AVERROR_INVALIDDATA;
197 if (ssrc + size - src < width)
198 return AVERROR_INVALIDDATA;
199 if (!s->fill_order) {
200 memcpy(dst, src, width);
203 for (i = 0; i < width; i++)
204 dst[i] = ff_reverse[src[i]];
209 for (pixels = 0; pixels < width;) {
210 code = (int8_t) * src++;
213 if (pixels + code > width) {
214 av_log(s->avctx, AV_LOG_ERROR,
215 "Copy went out of bounds\n");
216 return AVERROR_INVALIDDATA;
218 memcpy(dst + pixels, src, code);
221 } else if (code != -128) { // -127..-1
223 if (pixels + code > width) {
224 av_log(s->avctx, AV_LOG_ERROR,
225 "Run went out of bounds\n");
226 return AVERROR_INVALIDDATA;
229 memset(dst + pixels, c, code);
235 pixels = ff_lzw_decode(s->lzw, dst, width);
236 if (pixels < width) {
237 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
239 return AVERROR_INVALIDDATA;
248 static int init_image(TiffContext *s)
253 switch (s->bpp * 10 + s->bppcount) {
255 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
258 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
261 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
264 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
267 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
270 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
273 av_log(s->avctx, AV_LOG_ERROR,
274 "This format is not supported (bpp=%d, bppcount=%d)\n",
275 s->bpp, s->bppcount);
276 return AVERROR_INVALIDDATA;
278 if (s->width != s->avctx->width || s->height != s->avctx->height) {
279 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
281 avcodec_set_dimensions(s->avctx, s->width, s->height);
283 if (s->picture.data[0])
284 s->avctx->release_buffer(s->avctx, &s->picture);
285 if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0) {
286 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
289 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
290 if (s->palette_is_set) {
291 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
293 /* make default grayscale pal */
294 pal = (uint32_t *) s->picture.data[1];
295 for (i = 0; i < 256; i++)
296 pal[i] = i * 0x010101;
302 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
303 const uint8_t *buf, const uint8_t *end_buf)
305 unsigned tag, type, count, off, value = 0;
308 const uint8_t *rp, *gp, *bp;
310 if (end_buf - buf < 12)
311 return AVERROR_INVALIDDATA;
312 tag = tget_short(&buf, s->le);
313 type = tget_short(&buf, s->le);
314 count = tget_long(&buf, s->le);
315 off = tget_long(&buf, s->le);
317 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
318 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
328 value = tget(&buf, type, s->le);
345 if (count <= 4 && type_sizes[type] * count <= 4) {
352 if (buf && (buf < start || buf > end_buf)) {
353 av_log(s->avctx, AV_LOG_ERROR,
354 "Tag referencing position outside the image\n");
355 return AVERROR_INVALIDDATA;
368 av_log(s->avctx, AV_LOG_ERROR,
369 "This format is not supported (bpp=%d, %d components)\n",
371 return AVERROR_INVALIDDATA;
378 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
379 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
384 for (i = 0; i < count && buf < end_buf; i++)
385 s->bpp += tget(&buf, type, s->le);
392 case TIFF_SAMPLES_PER_PIXEL:
394 av_log(s->avctx, AV_LOG_ERROR,
395 "Samples per pixel requires a single value, many provided\n");
396 return AVERROR_INVALIDDATA;
398 if (s->bppcount == 1)
416 case TIFF_ADOBE_DEFLATE:
420 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
421 return AVERROR(ENOSYS);
425 av_log(s->avctx, AV_LOG_ERROR,
426 "JPEG compression is not supported\n");
427 return AVERROR_PATCHWELCOME;
429 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
431 return AVERROR_INVALIDDATA;
434 case TIFF_ROWSPERSTRIP:
435 if (type == TIFF_LONG && value == UINT_MAX)
436 value = s->avctx->height;
438 av_log(s->avctx, AV_LOG_ERROR,
439 "Incorrect value of rows per strip\n");
440 return AVERROR_INVALIDDATA;
444 case TIFF_STRIP_OFFS:
449 s->stripdata = start + off;
454 if (s->stripdata > end_buf) {
455 av_log(s->avctx, AV_LOG_ERROR,
456 "Tag referencing position outside the image\n");
457 return AVERROR_INVALIDDATA;
460 case TIFF_STRIP_SIZE:
462 s->stripsizes = NULL;
463 s->stripsize = value;
466 s->stripsizes = start + off;
470 if (s->stripsizes > end_buf) {
471 av_log(s->avctx, AV_LOG_ERROR,
472 "Tag referencing position outside the image\n");
473 return AVERROR_INVALIDDATA;
477 s->predictor = value;
491 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
493 return AVERROR_INVALIDDATA;
496 case TIFF_FILL_ORDER:
497 if (value < 1 || value > 2) {
498 av_log(s->avctx, AV_LOG_ERROR,
499 "Unknown FillOrder value %d, trying default one\n", value);
502 s->fill_order = value - 1;
505 pal = (uint32_t *) s->palette;
506 off = type_sizes[type];
507 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
508 return AVERROR_INVALIDDATA;
510 gp = buf + count / 3 * off;
511 bp = buf + count / 3 * off * 2;
512 off = (type_sizes[type] - 1) << 3;
513 for (i = 0; i < count / 3; i++) {
514 j = (tget(&rp, type, s->le) >> off) << 16;
515 j |= (tget(&gp, type, s->le) >> off) << 8;
516 j |= tget(&bp, type, s->le) >> off;
519 s->palette_is_set = 1;
523 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
524 return AVERROR_PATCHWELCOME;
528 if (s->compr == TIFF_G3)
532 if (s->compr == TIFF_G4)
536 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
542 static int decode_frame(AVCodecContext *avctx,
543 void *data, int *got_frame, AVPacket *avpkt)
545 const uint8_t *buf = avpkt->data;
546 int buf_size = avpkt->size;
547 TiffContext *const s = avctx->priv_data;
548 AVFrame *picture = data;
549 AVFrame *const p = &s->picture;
550 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
555 unsigned soff, ssize;
559 if (end_buf - buf < 8)
560 return AVERROR_INVALIDDATA;
565 else if (id == 0x4D4D)
568 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
569 return AVERROR_INVALIDDATA;
575 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
576 // that further identifies the file as a TIFF file"
577 if (tget_short(&buf, le) != 42) {
578 av_log(avctx, AV_LOG_ERROR,
579 "The answer to life, universe and everything is not correct!\n");
580 return AVERROR_INVALIDDATA;
582 // Reset these pointers so we can tell if they were set this frame
583 s->stripsizes = s->stripdata = NULL;
584 /* parse image file directory */
585 off = tget_long(&buf, le);
586 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
587 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
588 return AVERROR_INVALIDDATA;
590 buf = orig_buf + off;
591 entries = tget_short(&buf, le);
592 for (i = 0; i < entries; i++) {
593 if ((ret = tiff_decode_tag(s, orig_buf, buf, end_buf)) < 0)
597 if (!s->stripdata && !s->stripoff) {
598 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
599 return AVERROR_INVALIDDATA;
601 /* now we have the data and may start decoding */
602 if ((ret = init_image(s)) < 0)
605 if (s->strips == 1 && !s->stripsize) {
606 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
607 s->stripsize = buf_size - s->stripoff;
609 stride = p->linesize[0];
611 for (i = 0; i < s->height; i += s->rps) {
613 if (s->stripsizes >= end_buf)
614 return AVERROR_INVALIDDATA;
615 ssize = tget(&s->stripsizes, s->sstype, s->le);
617 ssize = s->stripsize;
620 if (s->stripdata >= end_buf)
621 return AVERROR_INVALIDDATA;
622 soff = tget(&s->stripdata, s->sot, s->le);
626 if (soff > buf_size || ssize > buf_size - soff) {
627 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
628 return AVERROR_INVALIDDATA;
630 if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
631 FFMIN(s->rps, s->height - i)) < 0)
633 dst += s->rps * stride;
635 if (s->predictor == 2) {
638 ssize = s->width * soff;
639 for (i = 0; i < s->height; i++) {
640 for (j = soff; j < ssize; j++)
641 dst[j] += dst[j - soff];
650 src = s->picture.data[0];
651 for (j = 0; j < s->height; j++) {
652 for (i = 0; i < s->picture.linesize[0]; i++)
653 src[i] = 255 - src[i];
654 src += s->picture.linesize[0];
657 *picture = s->picture;
663 static av_cold int tiff_init(AVCodecContext *avctx)
665 TiffContext *s = avctx->priv_data;
670 avcodec_get_frame_defaults(&s->picture);
671 avctx->coded_frame = &s->picture;
672 ff_lzw_decode_open(&s->lzw);
673 ff_ccitt_unpack_init();
678 static av_cold int tiff_end(AVCodecContext *avctx)
680 TiffContext *const s = avctx->priv_data;
682 ff_lzw_decode_close(&s->lzw);
683 if (s->picture.data[0])
684 avctx->release_buffer(avctx, &s->picture);
688 AVCodec ff_tiff_decoder = {
690 .type = AVMEDIA_TYPE_VIDEO,
691 .id = AV_CODEC_ID_TIFF,
692 .priv_data_size = sizeof(TiffContext),
695 .decode = decode_frame,
696 .capabilities = CODEC_CAP_DR1,
697 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),