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
36 #include "libavutil/common.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/imgutils.h"
40 typedef struct TiffContext {
41 AVCodecContext *avctx;
45 unsigned int bpp, bppcount;
46 uint32_t palette[256];
55 int strips, rps, sstype;
57 const uint8_t *stripdata;
58 const uint8_t *stripsizes;
59 int stripsize, stripoff;
63 static unsigned tget_short(const uint8_t **p, int le)
65 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
70 static unsigned tget_long(const uint8_t **p, int le)
72 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
77 static unsigned tget(const uint8_t **p, int type, int le)
80 case TIFF_BYTE : return *(*p)++;
81 case TIFF_SHORT: return tget_short(p, le);
82 case TIFF_LONG : return tget_long(p, le);
83 default : return UINT_MAX;
88 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
91 z_stream zstream = { 0 };
94 zstream.next_in = src;
95 zstream.avail_in = size;
96 zstream.next_out = dst;
97 zstream.avail_out = *len;
98 zret = inflateInit(&zstream);
100 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
103 zret = inflate(&zstream, Z_SYNC_FLUSH);
104 inflateEnd(&zstream);
105 *len = zstream.total_out;
106 return zret == Z_STREAM_END ? Z_OK : zret;
110 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
111 const uint8_t *src, int size, int lines)
113 int c, line, pixels, code;
114 const uint8_t *ssrc = src;
115 int width = ((s->width * s->bpp) + 7) >> 3;
118 return AVERROR_INVALIDDATA;
121 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
123 unsigned long outlen;
125 outlen = width * lines;
126 zbuf = av_malloc(outlen);
128 return AVERROR(ENOMEM);
129 ret = tiff_uncompress(zbuf, &outlen, src, size);
131 av_log(s->avctx, AV_LOG_ERROR,
132 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
133 (unsigned long)width * lines, ret);
138 for (line = 0; line < lines; line++) {
139 memcpy(dst, src, width);
147 if (s->compr == TIFF_LZW) {
148 if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
149 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
153 if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
154 || s->compr == TIFF_G4) {
156 uint8_t *src2 = av_malloc((unsigned)size +
157 FF_INPUT_BUFFER_PADDING_SIZE);
160 av_log(s->avctx, AV_LOG_ERROR,
161 "Error allocating temporary buffer\n");
162 return AVERROR(ENOMEM);
164 if (s->fax_opts & 2) {
165 av_log(s->avctx, AV_LOG_ERROR,
166 "Uncompressed fax mode is not supported (yet)\n");
170 if (!s->fill_order) {
171 memcpy(src2, src, size);
173 for (i = 0; i < size; i++)
174 src2[i] = av_reverse[src[i]];
176 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
181 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
182 s->compr, s->fax_opts);
188 for (line = 0; line < lines; line++) {
189 if (src - ssrc > size) {
190 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
195 if (ssrc + size - src < width)
196 return AVERROR_INVALIDDATA;
197 if (!s->fill_order) {
198 memcpy(dst, src, width);
201 for (i = 0; i < width; i++)
202 dst[i] = av_reverse[src[i]];
207 for (pixels = 0; pixels < width;) {
208 code = (int8_t) * src++;
211 if (pixels + code > width) {
212 av_log(s->avctx, AV_LOG_ERROR,
213 "Copy went out of bounds\n");
216 memcpy(dst + pixels, src, code);
219 } else if (code != -128) { // -127..-1
221 if (pixels + code > width) {
222 av_log(s->avctx, AV_LOG_ERROR,
223 "Run went out of bounds\n");
227 memset(dst + pixels, c, code);
233 pixels = ff_lzw_decode(s->lzw, dst, width);
234 if (pixels < width) {
235 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
246 static int init_image(TiffContext *s)
251 switch (s->bpp * 10 + s->bppcount) {
253 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
256 s->avctx->pix_fmt = PIX_FMT_PAL8;
259 s->avctx->pix_fmt = PIX_FMT_RGB24;
262 s->avctx->pix_fmt = s->le ? PIX_FMT_GRAY16LE : PIX_FMT_GRAY16BE;
265 s->avctx->pix_fmt = PIX_FMT_RGBA;
268 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
271 av_log(s->avctx, AV_LOG_ERROR,
272 "This format is not supported (bpp=%d, bppcount=%d)\n",
273 s->bpp, s->bppcount);
274 return AVERROR_INVALIDDATA;
276 if (s->width != s->avctx->width || s->height != s->avctx->height) {
277 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
279 avcodec_set_dimensions(s->avctx, s->width, s->height);
281 if (s->picture.data[0])
282 s->avctx->release_buffer(s->avctx, &s->picture);
283 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
284 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
287 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
288 if (s->palette_is_set) {
289 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
291 /* make default grayscale pal */
292 pal = (uint32_t *) s->picture.data[1];
293 for (i = 0; i < 256; i++)
294 pal[i] = i * 0x010101;
300 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
301 const uint8_t *buf, const uint8_t *end_buf)
303 unsigned tag, type, count, off, value = 0;
306 const uint8_t *rp, *gp, *bp;
308 if (end_buf - buf < 12)
310 tag = tget_short(&buf, s->le);
311 type = tget_short(&buf, s->le);
312 count = tget_long(&buf, s->le);
313 off = tget_long(&buf, s->le);
315 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
316 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
326 value = tget(&buf, type, s->le);
343 if (count <= 4 && type_sizes[type] * count <= 4) {
350 if (buf && (buf < start || buf > end_buf)) {
351 av_log(s->avctx, AV_LOG_ERROR,
352 "Tag referencing position outside the image\n");
366 av_log(s->avctx, AV_LOG_ERROR,
367 "This format is not supported (bpp=%d, %d components)\n",
376 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
377 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
382 for (i = 0; i < count && buf < end_buf; i++)
383 s->bpp += tget(&buf, type, s->le);
390 case TIFF_SAMPLES_PER_PIXEL:
392 av_log(s->avctx, AV_LOG_ERROR,
393 "Samples per pixel requires a single value, many provided\n");
394 return AVERROR_INVALIDDATA;
396 if (s->bppcount == 1)
414 case TIFF_ADOBE_DEFLATE:
418 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
423 av_log(s->avctx, AV_LOG_ERROR,
424 "JPEG compression is not supported\n");
427 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
432 case TIFF_ROWSPERSTRIP:
433 if (type == TIFF_LONG && value == UINT_MAX)
434 value = s->avctx->height;
436 av_log(s->avctx, AV_LOG_ERROR,
437 "Incorrect value of rows per strip\n");
442 case TIFF_STRIP_OFFS:
447 s->stripdata = start + off;
452 if (s->stripdata > end_buf) {
453 av_log(s->avctx, AV_LOG_ERROR,
454 "Tag referencing position outside the image\n");
458 case TIFF_STRIP_SIZE:
460 s->stripsizes = NULL;
461 s->stripsize = value;
464 s->stripsizes = start + off;
468 if (s->stripsizes > end_buf) {
469 av_log(s->avctx, AV_LOG_ERROR,
470 "Tag referencing position outside the image\n");
475 s->predictor = value;
489 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
494 case TIFF_FILL_ORDER:
495 if (value < 1 || value > 2) {
496 av_log(s->avctx, AV_LOG_ERROR,
497 "Unknown FillOrder value %d, trying default one\n", value);
500 s->fill_order = value - 1;
503 pal = (uint32_t *) s->palette;
504 off = type_sizes[type];
505 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
508 gp = buf + count / 3 * off;
509 bp = buf + count / 3 * off * 2;
510 off = (type_sizes[type] - 1) << 3;
511 for (i = 0; i < count / 3; i++) {
512 j = (tget(&rp, type, s->le) >> off) << 16;
513 j |= (tget(&gp, type, s->le) >> off) << 8;
514 j |= tget(&bp, type, s->le) >> off;
517 s->palette_is_set = 1;
521 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
526 if (s->compr == TIFF_G3)
530 if (s->compr == TIFF_G4)
534 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
540 static int decode_frame(AVCodecContext *avctx,
541 void *data, int *data_size, AVPacket *avpkt)
543 const uint8_t *buf = avpkt->data;
544 int buf_size = avpkt->size;
545 TiffContext *const s = avctx->priv_data;
546 AVFrame *picture = data;
547 AVFrame *const p = &s->picture;
548 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
553 unsigned soff, ssize;
557 if (end_buf - buf < 8)
558 return AVERROR_INVALIDDATA;
563 else if (id == 0x4D4D)
566 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
573 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
574 // that further identifies the file as a TIFF file"
575 if (tget_short(&buf, le) != 42) {
576 av_log(avctx, AV_LOG_ERROR,
577 "The answer to life, universe and everything is not correct!\n");
580 // Reset these pointers so we can tell if they were set this frame
581 s->stripsizes = s->stripdata = NULL;
582 /* parse image file directory */
583 off = tget_long(&buf, le);
584 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
585 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
586 return AVERROR_INVALIDDATA;
588 buf = orig_buf + off;
589 entries = tget_short(&buf, le);
590 for (i = 0; i < entries; i++) {
591 if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
595 if (!s->stripdata && !s->stripoff) {
596 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
599 /* now we have the data and may start decoding */
600 if ((ret = init_image(s)) < 0)
603 if (s->strips == 1 && !s->stripsize) {
604 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
605 s->stripsize = buf_size - s->stripoff;
607 stride = p->linesize[0];
609 for (i = 0; i < s->height; i += s->rps) {
611 if (s->stripsizes >= end_buf)
612 return AVERROR_INVALIDDATA;
613 ssize = tget(&s->stripsizes, s->sstype, s->le);
615 ssize = s->stripsize;
618 if (s->stripdata >= end_buf)
619 return AVERROR_INVALIDDATA;
620 soff = tget(&s->stripdata, s->sot, s->le);
624 if (soff > buf_size || ssize > buf_size - soff) {
625 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
628 if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
629 FFMIN(s->rps, s->height - i)) < 0)
631 dst += s->rps * stride;
633 if (s->predictor == 2) {
636 ssize = s->width * soff;
637 for (i = 0; i < s->height; i++) {
638 for (j = soff; j < ssize; j++)
639 dst[j] += dst[j - soff];
648 src = s->picture.data[0];
649 for (j = 0; j < s->height; j++) {
650 for (i = 0; i < s->picture.linesize[0]; i++)
651 src[i] = 255 - src[i];
652 src += s->picture.linesize[0];
655 *picture = s->picture;
656 *data_size = sizeof(AVPicture);
661 static av_cold int tiff_init(AVCodecContext *avctx)
663 TiffContext *s = avctx->priv_data;
668 avcodec_get_frame_defaults(&s->picture);
669 avctx->coded_frame = &s->picture;
670 ff_lzw_decode_open(&s->lzw);
671 ff_ccitt_unpack_init();
676 static av_cold int tiff_end(AVCodecContext *avctx)
678 TiffContext *const s = avctx->priv_data;
680 ff_lzw_decode_close(&s->lzw);
681 if (s->picture.data[0])
682 avctx->release_buffer(avctx, &s->picture);
686 AVCodec ff_tiff_decoder = {
688 .type = AVMEDIA_TYPE_VIDEO,
689 .id = AV_CODEC_ID_TIFF,
690 .priv_data_size = sizeof(TiffContext),
693 .decode = decode_frame,
694 .capabilities = CODEC_CAP_DR1,
695 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),