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
37 #include "libavutil/attributes.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/imgutils.h"
41 typedef struct TiffContext {
42 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;
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);
139 for (line = 0; line < lines; line++) {
140 memcpy(dst, src, width);
148 if (s->compr == TIFF_LZW) {
149 if (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");
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");
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");
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");
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",
247 static int init_image(TiffContext *s)
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 (s->picture.data[0])
283 s->avctx->release_buffer(s->avctx, &s->picture);
284 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
285 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
288 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
289 if (s->palette_is_set) {
290 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
292 /* make default grayscale pal */
293 pal = (uint32_t *) s->picture.data[1];
294 for (i = 0; i < 256; i++)
295 pal[i] = i * 0x010101;
301 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
302 const uint8_t *buf, const uint8_t *end_buf)
304 unsigned tag, type, count, off, value = 0;
307 const uint8_t *rp, *gp, *bp;
309 if (end_buf - buf < 12)
311 tag = tget_short(&buf, s->le);
312 type = tget_short(&buf, s->le);
313 count = tget_long(&buf, s->le);
314 off = tget_long(&buf, s->le);
316 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
317 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
327 value = tget(&buf, type, s->le);
344 if (count <= 4 && type_sizes[type] * count <= 4) {
351 if (buf && (buf < start || buf > end_buf)) {
352 av_log(s->avctx, AV_LOG_ERROR,
353 "Tag referencing position outside the image\n");
367 av_log(s->avctx, AV_LOG_ERROR,
368 "This format is not supported (bpp=%d, %d components)\n",
377 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
378 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
383 for (i = 0; i < count && buf < end_buf; i++)
384 s->bpp += tget(&buf, type, s->le);
391 case TIFF_SAMPLES_PER_PIXEL:
393 av_log(s->avctx, AV_LOG_ERROR,
394 "Samples per pixel requires a single value, many provided\n");
395 return AVERROR_INVALIDDATA;
397 if (s->bppcount == 1)
415 case TIFF_ADOBE_DEFLATE:
419 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
424 av_log(s->avctx, AV_LOG_ERROR,
425 "JPEG compression is not supported\n");
428 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
433 case TIFF_ROWSPERSTRIP:
434 if (type == TIFF_LONG && value == UINT_MAX)
435 value = s->avctx->height;
437 av_log(s->avctx, AV_LOG_ERROR,
438 "Incorrect value of rows per strip\n");
443 case TIFF_STRIP_OFFS:
448 s->stripdata = start + off;
453 if (s->stripdata > end_buf) {
454 av_log(s->avctx, AV_LOG_ERROR,
455 "Tag referencing position outside the image\n");
459 case TIFF_STRIP_SIZE:
461 s->stripsizes = NULL;
462 s->stripsize = value;
465 s->stripsizes = start + off;
469 if (s->stripsizes > end_buf) {
470 av_log(s->avctx, AV_LOG_ERROR,
471 "Tag referencing position outside the image\n");
476 s->predictor = value;
490 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
495 case TIFF_FILL_ORDER:
496 if (value < 1 || value > 2) {
497 av_log(s->avctx, AV_LOG_ERROR,
498 "Unknown FillOrder value %d, trying default one\n", value);
501 s->fill_order = value - 1;
504 pal = (uint32_t *) s->palette;
505 off = type_sizes[type];
506 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
509 gp = buf + count / 3 * off;
510 bp = buf + count / 3 * off * 2;
511 off = (type_sizes[type] - 1) << 3;
512 for (i = 0; i < count / 3; i++) {
513 j = (tget(&rp, type, s->le) >> off) << 16;
514 j |= (tget(&gp, type, s->le) >> off) << 8;
515 j |= tget(&bp, type, s->le) >> off;
518 s->palette_is_set = 1;
522 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
527 if (s->compr == TIFF_G3)
531 if (s->compr == TIFF_G4)
535 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
541 static int decode_frame(AVCodecContext *avctx,
542 void *data, int *data_size, AVPacket *avpkt)
544 const uint8_t *buf = avpkt->data;
545 int buf_size = avpkt->size;
546 TiffContext *const s = avctx->priv_data;
547 AVFrame *picture = data;
548 AVFrame *const p = &s->picture;
549 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
554 unsigned soff, ssize;
558 if (end_buf - buf < 8)
559 return AVERROR_INVALIDDATA;
564 else if (id == 0x4D4D)
567 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
574 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
575 // that further identifies the file as a TIFF file"
576 if (tget_short(&buf, le) != 42) {
577 av_log(avctx, AV_LOG_ERROR,
578 "The answer to life, universe and everything is not correct!\n");
581 // Reset these pointers so we can tell if they were set this frame
582 s->stripsizes = s->stripdata = NULL;
583 /* parse image file directory */
584 off = tget_long(&buf, le);
585 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
586 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
587 return AVERROR_INVALIDDATA;
589 buf = orig_buf + off;
590 entries = tget_short(&buf, le);
591 for (i = 0; i < entries; i++) {
592 if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
596 if (!s->stripdata && !s->stripoff) {
597 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
600 /* now we have the data and may start decoding */
601 if ((ret = init_image(s)) < 0)
604 if (s->strips == 1 && !s->stripsize) {
605 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
606 s->stripsize = buf_size - s->stripoff;
608 stride = p->linesize[0];
610 for (i = 0; i < s->height; i += s->rps) {
612 if (s->stripsizes >= end_buf)
613 return AVERROR_INVALIDDATA;
614 ssize = tget(&s->stripsizes, s->sstype, s->le);
616 ssize = s->stripsize;
619 if (s->stripdata >= end_buf)
620 return AVERROR_INVALIDDATA;
621 soff = tget(&s->stripdata, s->sot, s->le);
625 if (soff > buf_size || ssize > buf_size - soff) {
626 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
629 if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
630 FFMIN(s->rps, s->height - i)) < 0)
632 dst += s->rps * stride;
634 if (s->predictor == 2) {
637 ssize = s->width * soff;
638 for (i = 0; i < s->height; i++) {
639 for (j = soff; j < ssize; j++)
640 dst[j] += dst[j - soff];
649 src = s->picture.data[0];
650 for (j = 0; j < s->height; j++) {
651 for (i = 0; i < s->picture.linesize[0]; i++)
652 src[i] = 255 - src[i];
653 src += s->picture.linesize[0];
656 *picture = s->picture;
657 *data_size = sizeof(AVPicture);
662 static av_cold int tiff_init(AVCodecContext *avctx)
664 TiffContext *s = avctx->priv_data;
669 avcodec_get_frame_defaults(&s->picture);
670 avctx->coded_frame = &s->picture;
671 ff_lzw_decode_open(&s->lzw);
672 ff_ccitt_unpack_init();
677 static av_cold int tiff_end(AVCodecContext *avctx)
679 TiffContext *const s = avctx->priv_data;
681 ff_lzw_decode_close(&s->lzw);
682 if (s->picture.data[0])
683 avctx->release_buffer(avctx, &s->picture);
687 AVCodec ff_tiff_decoder = {
689 .type = AVMEDIA_TYPE_VIDEO,
690 .id = AV_CODEC_ID_TIFF,
691 .priv_data_size = sizeof(TiffContext),
694 .decode = decode_frame,
695 .capabilities = CODEC_CAP_DR1,
696 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),