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"
43 typedef struct TiffContext {
44 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)
85 return tget_short(p, le);
87 return tget_long(p, le);
94 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
97 z_stream zstream = { 0 };
100 zstream.next_in = src;
101 zstream.avail_in = size;
102 zstream.next_out = dst;
103 zstream.avail_out = *len;
104 zret = inflateInit(&zstream);
106 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
109 zret = inflate(&zstream, Z_SYNC_FLUSH);
110 inflateEnd(&zstream);
111 *len = zstream.total_out;
112 return zret == Z_STREAM_END ? Z_OK : zret;
116 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
117 const uint8_t *src, int size, int lines)
119 int c, line, pixels, code, ret;
120 const uint8_t *ssrc = src;
121 int width = ((s->width * s->bpp) + 7) >> 3;
124 return AVERROR_INVALIDDATA;
127 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
129 unsigned long outlen;
131 outlen = width * lines;
132 zbuf = av_malloc(outlen);
134 return AVERROR(ENOMEM);
135 ret = tiff_uncompress(zbuf, &outlen, src, size);
137 av_log(s->avctx, AV_LOG_ERROR,
138 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
139 (unsigned long)width * lines, ret);
141 return AVERROR_UNKNOWN;
144 for (line = 0; line < lines; line++) {
145 memcpy(dst, src, width);
153 if (s->compr == TIFF_LZW) {
154 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
155 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
159 if (s->compr == TIFF_CCITT_RLE ||
160 s->compr == TIFF_G3 ||
161 s->compr == TIFF_G4) {
163 uint8_t *src2 = av_malloc((unsigned)size +
164 FF_INPUT_BUFFER_PADDING_SIZE);
167 av_log(s->avctx, AV_LOG_ERROR,
168 "Error allocating temporary buffer\n");
169 return AVERROR(ENOMEM);
171 if (s->fax_opts & 2) {
172 av_log(s->avctx, AV_LOG_ERROR,
173 "Uncompressed fax mode is not supported (yet)\n");
175 return AVERROR_INVALIDDATA;
177 if (!s->fill_order) {
178 memcpy(src2, src, size);
180 for (i = 0; i < size; i++)
181 src2[i] = ff_reverse[src[i]];
183 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
188 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
189 s->compr, s->fax_opts);
195 for (line = 0; line < lines; line++) {
196 if (src - ssrc > size) {
197 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
198 return AVERROR_INVALIDDATA;
202 if (ssrc + size - src < width)
203 return AVERROR_INVALIDDATA;
204 if (!s->fill_order) {
205 memcpy(dst, src, width);
208 for (i = 0; i < width; i++)
209 dst[i] = ff_reverse[src[i]];
214 for (pixels = 0; pixels < width;) {
215 code = (int8_t) *src++;
218 if (pixels + code > width) {
219 av_log(s->avctx, AV_LOG_ERROR,
220 "Copy went out of bounds\n");
221 return AVERROR_INVALIDDATA;
223 memcpy(dst + pixels, src, code);
226 } else if (code != -128) { // -127..-1
228 if (pixels + code > width) {
229 av_log(s->avctx, AV_LOG_ERROR,
230 "Run went out of bounds\n");
231 return AVERROR_INVALIDDATA;
234 memset(dst + pixels, c, code);
240 pixels = ff_lzw_decode(s->lzw, dst, width);
241 if (pixels < width) {
242 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
244 return AVERROR_INVALIDDATA;
253 static int init_image(TiffContext *s, AVFrame *frame)
258 switch (s->bpp * 10 + s->bppcount) {
260 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
263 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
266 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
269 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
272 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
275 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
278 av_log(s->avctx, AV_LOG_ERROR,
279 "This format is not supported (bpp=%d, bppcount=%d)\n",
280 s->bpp, s->bppcount);
281 return AVERROR_INVALIDDATA;
283 if (s->width != s->avctx->width || s->height != s->avctx->height) {
284 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
286 avcodec_set_dimensions(s->avctx, s->width, s->height);
288 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
289 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
292 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
293 if (s->palette_is_set) {
294 memcpy(frame->data[1], s->palette, sizeof(s->palette));
296 /* make default grayscale pal */
297 pal = (uint32_t *) frame->data[1];
298 for (i = 0; i < 256; i++)
299 pal[i] = i * 0x010101;
305 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
306 const uint8_t *buf, const uint8_t *end_buf)
308 unsigned tag, type, count, off, value = 0;
311 const uint8_t *rp, *gp, *bp;
313 if (end_buf - buf < 12)
314 return AVERROR_INVALIDDATA;
315 tag = tget_short(&buf, s->le);
316 type = tget_short(&buf, s->le);
317 count = tget_long(&buf, s->le);
318 off = tget_long(&buf, s->le);
320 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
321 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
331 value = tget(&buf, type, s->le);
348 if (count <= 4 && type_sizes[type] * count <= 4)
354 if (buf && (buf < start || buf > end_buf)) {
355 av_log(s->avctx, AV_LOG_ERROR,
356 "Tag referencing position outside the image\n");
357 return AVERROR_INVALIDDATA;
370 av_log(s->avctx, AV_LOG_ERROR,
371 "This format is not supported (bpp=%d, %d components)\n",
373 return AVERROR_INVALIDDATA;
380 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
381 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
386 for (i = 0; i < count && buf < end_buf; i++)
387 s->bpp += tget(&buf, type, s->le);
394 case TIFF_SAMPLES_PER_PIXEL:
396 av_log(s->avctx, AV_LOG_ERROR,
397 "Samples per pixel requires a single value, many provided\n");
398 return AVERROR_INVALIDDATA;
400 if (s->bppcount == 1)
418 case TIFF_ADOBE_DEFLATE:
422 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
423 return AVERROR(ENOSYS);
427 av_log(s->avctx, AV_LOG_ERROR,
428 "JPEG compression is not supported\n");
429 return AVERROR_PATCHWELCOME;
431 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
433 return AVERROR_INVALIDDATA;
436 case TIFF_ROWSPERSTRIP:
437 if (type == TIFF_LONG && value == UINT_MAX)
438 value = s->avctx->height;
440 av_log(s->avctx, AV_LOG_ERROR,
441 "Incorrect value of rows per strip\n");
442 return AVERROR_INVALIDDATA;
446 case TIFF_STRIP_OFFS:
451 s->stripdata = start + off;
456 if (s->stripdata > end_buf) {
457 av_log(s->avctx, AV_LOG_ERROR,
458 "Tag referencing position outside the image\n");
459 return AVERROR_INVALIDDATA;
462 case TIFF_STRIP_SIZE:
464 s->stripsizes = NULL;
465 s->stripsize = value;
468 s->stripsizes = start + off;
472 if (s->stripsizes > end_buf) {
473 av_log(s->avctx, AV_LOG_ERROR,
474 "Tag referencing position outside the image\n");
475 return AVERROR_INVALIDDATA;
479 s->predictor = value;
493 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
495 return AVERROR_INVALIDDATA;
498 case TIFF_FILL_ORDER:
499 if (value < 1 || value > 2) {
500 av_log(s->avctx, AV_LOG_ERROR,
501 "Unknown FillOrder value %d, trying default one\n", value);
504 s->fill_order = value - 1;
507 pal = (uint32_t *) s->palette;
508 off = type_sizes[type];
509 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
510 return AVERROR_INVALIDDATA;
512 gp = buf + count / 3 * off;
513 bp = buf + count / 3 * off * 2;
514 off = (type_sizes[type] - 1) << 3;
515 for (i = 0; i < count / 3; i++) {
516 j = (tget(&rp, type, s->le) >> off) << 16;
517 j |= (tget(&gp, type, s->le) >> off) << 8;
518 j |= tget(&bp, type, s->le) >> off;
521 s->palette_is_set = 1;
525 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
526 return AVERROR_PATCHWELCOME;
530 if (s->compr == TIFF_G3)
534 if (s->compr == TIFF_G4)
538 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
544 static int decode_frame(AVCodecContext *avctx,
545 void *data, int *got_frame, AVPacket *avpkt)
547 const uint8_t *buf = avpkt->data;
548 int buf_size = avpkt->size;
549 TiffContext *const s = avctx->priv_data;
550 AVFrame *const p = data;
551 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
554 int i, j, entries, stride;
555 unsigned soff, ssize;
558 // parse image header
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, p)) < 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];
651 for (j = 0; j < s->height; j++) {
652 for (i = 0; i < p->linesize[0]; i++)
653 src[i] = 255 - src[i];
654 src += p->linesize[0];
662 static av_cold int tiff_init(AVCodecContext *avctx)
664 TiffContext *s = avctx->priv_data;
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);
683 AVCodec ff_tiff_decoder = {
685 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
686 .type = AVMEDIA_TYPE_VIDEO,
687 .id = AV_CODEC_ID_TIFF,
688 .priv_data_size = sizeof(TiffContext),
691 .decode = decode_frame,
692 .capabilities = CODEC_CAP_DR1,