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;
115 static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
116 const uint8_t *src, int size,
117 int width, int lines)
120 unsigned long outlen;
122 outlen = width * lines;
123 zbuf = av_malloc(outlen);
125 return AVERROR(ENOMEM);
126 ret = tiff_uncompress(zbuf, &outlen, src, size);
128 av_log(s->avctx, AV_LOG_ERROR,
129 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
130 (unsigned long)width * lines, ret);
132 return AVERROR_UNKNOWN;
135 for (line = 0; line < lines; line++) {
136 memcpy(dst, src, width);
146 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
147 const uint8_t *src, int size, int lines)
150 uint8_t *src2 = av_malloc((unsigned)size +
151 FF_INPUT_BUFFER_PADDING_SIZE);
154 av_log(s->avctx, AV_LOG_ERROR,
155 "Error allocating temporary buffer\n");
156 return AVERROR(ENOMEM);
158 if (s->fax_opts & 2) {
159 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
161 return AVERROR_PATCHWELCOME;
163 if (!s->fill_order) {
164 memcpy(src2, src, size);
166 for (i = 0; i < size; i++)
167 src2[i] = ff_reverse[src[i]];
169 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
170 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
171 s->compr, s->fax_opts);
176 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
177 const uint8_t *src, int size, int lines)
179 int c, line, pixels, code, ret;
180 const uint8_t *ssrc = src;
181 int width = ((s->width * s->bpp) + 7) >> 3;
184 return AVERROR_INVALIDDATA;
186 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
188 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
190 av_log(s->avctx, AV_LOG_ERROR,
191 "zlib support not enabled, "
192 "deflate compression not supported\n");
193 return AVERROR(ENOSYS);
196 if (s->compr == TIFF_LZW) {
197 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
198 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
202 if (s->compr == TIFF_CCITT_RLE ||
203 s->compr == TIFF_G3 ||
204 s->compr == TIFF_G4) {
205 return tiff_unpack_fax(s, dst, stride, src, size, lines);
207 for (line = 0; line < lines; line++) {
208 if (src - ssrc > size) {
209 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
210 return AVERROR_INVALIDDATA;
214 if (ssrc + size - src < width)
215 return AVERROR_INVALIDDATA;
216 if (!s->fill_order) {
217 memcpy(dst, src, width);
220 for (i = 0; i < width; i++)
221 dst[i] = ff_reverse[src[i]];
226 for (pixels = 0; pixels < width;) {
227 if (ssrc + size - src < 2)
228 return AVERROR_INVALIDDATA;
229 code = (int8_t) *src++;
232 if (pixels + code > width ||
233 ssrc + size - src < code) {
234 av_log(s->avctx, AV_LOG_ERROR,
235 "Copy went out of bounds\n");
236 return AVERROR_INVALIDDATA;
238 memcpy(dst + pixels, src, code);
241 } else if (code != -128) { // -127..-1
243 if (pixels + code > width) {
244 av_log(s->avctx, AV_LOG_ERROR,
245 "Run went out of bounds\n");
246 return AVERROR_INVALIDDATA;
249 memset(dst + pixels, c, code);
255 pixels = ff_lzw_decode(s->lzw, dst, width);
256 if (pixels < width) {
257 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
259 return AVERROR_INVALIDDATA;
268 static int init_image(TiffContext *s, AVFrame *frame)
273 switch (s->bpp * 10 + s->bppcount) {
275 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
278 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
281 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
284 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
287 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
290 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
293 av_log(s->avctx, AV_LOG_ERROR,
294 "This format is not supported (bpp=%d, bppcount=%d)\n",
295 s->bpp, s->bppcount);
296 return AVERROR_INVALIDDATA;
298 if (s->width != s->avctx->width || s->height != s->avctx->height) {
299 ret = ff_set_dimensions(s->avctx, s->width, s->height);
303 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
304 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
307 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
308 if (s->palette_is_set) {
309 memcpy(frame->data[1], s->palette, sizeof(s->palette));
311 /* make default grayscale pal */
312 pal = (uint32_t *) frame->data[1];
313 for (i = 0; i < 256; i++)
314 pal[i] = i * 0x010101;
320 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
321 const uint8_t *buf, const uint8_t *end_buf)
323 unsigned tag, type, count, off, value = 0;
326 const uint8_t *rp, *gp, *bp;
328 if (end_buf - buf < 12)
329 return AVERROR_INVALIDDATA;
330 tag = tget_short(&buf, s->le);
331 type = tget_short(&buf, s->le);
332 count = tget_long(&buf, s->le);
333 off = tget_long(&buf, s->le);
335 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
336 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
346 value = tget(&buf, type, s->le);
363 if (count <= 4 && type_sizes[type] * count <= 4)
369 if (buf && (buf < start || buf > end_buf)) {
370 av_log(s->avctx, AV_LOG_ERROR,
371 "Tag referencing position outside the image\n");
372 return AVERROR_INVALIDDATA;
385 av_log(s->avctx, AV_LOG_ERROR,
386 "This format is not supported (bpp=%d, %d components)\n",
388 return AVERROR_INVALIDDATA;
395 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
396 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
401 for (i = 0; i < count && buf < end_buf; i++)
402 s->bpp += tget(&buf, type, s->le);
409 case TIFF_SAMPLES_PER_PIXEL:
411 av_log(s->avctx, AV_LOG_ERROR,
412 "Samples per pixel requires a single value, many provided\n");
413 return AVERROR_INVALIDDATA;
415 if (s->bppcount == 1)
433 case TIFF_ADOBE_DEFLATE:
437 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
438 return AVERROR(ENOSYS);
442 avpriv_report_missing_feature(s->avctx, "JPEG compression");
443 return AVERROR_PATCHWELCOME;
445 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
447 return AVERROR_INVALIDDATA;
450 case TIFF_ROWSPERSTRIP:
451 if (type == TIFF_LONG && value == UINT_MAX)
452 value = s->avctx->height;
454 av_log(s->avctx, AV_LOG_ERROR,
455 "Incorrect value of rows per strip\n");
456 return AVERROR_INVALIDDATA;
460 case TIFF_STRIP_OFFS:
465 s->stripdata = start + off;
470 if (s->stripdata > end_buf) {
471 av_log(s->avctx, AV_LOG_ERROR,
472 "Tag referencing position outside the image\n");
473 return AVERROR_INVALIDDATA;
476 case TIFF_STRIP_SIZE:
478 s->stripsizes = NULL;
479 s->stripsize = value;
482 s->stripsizes = start + off;
486 if (s->stripsizes > end_buf) {
487 av_log(s->avctx, AV_LOG_ERROR,
488 "Tag referencing position outside the image\n");
489 return AVERROR_INVALIDDATA;
493 s->predictor = value;
507 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
509 return AVERROR_INVALIDDATA;
512 case TIFF_FILL_ORDER:
513 if (value < 1 || value > 2) {
514 av_log(s->avctx, AV_LOG_ERROR,
515 "Unknown FillOrder value %d, trying default one\n", value);
518 s->fill_order = value - 1;
521 pal = (uint32_t *) s->palette;
522 off = type_sizes[type];
523 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
524 return AVERROR_INVALIDDATA;
526 gp = buf + count / 3 * off;
527 bp = buf + count / 3 * off * 2;
528 off = (type_sizes[type] - 1) << 3;
529 for (i = 0; i < count / 3; i++) {
530 j = (tget(&rp, type, s->le) >> off) << 16;
531 j |= (tget(&gp, type, s->le) >> off) << 8;
532 j |= tget(&bp, type, s->le) >> off;
535 s->palette_is_set = 1;
539 avpriv_report_missing_feature(s->avctx, "Planar format");
540 return AVERROR_PATCHWELCOME;
544 if (s->compr == TIFF_G3)
548 if (s->compr == TIFF_G4)
552 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
553 av_log(s->avctx, AV_LOG_ERROR,
554 "Unknown or unsupported tag %d/0X%0X\n",
556 return AVERROR_INVALIDDATA;
562 static int decode_frame(AVCodecContext *avctx,
563 void *data, int *got_frame, AVPacket *avpkt)
565 const uint8_t *buf = avpkt->data;
566 int buf_size = avpkt->size;
567 TiffContext *const s = avctx->priv_data;
568 AVFrame *const p = data;
569 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
572 int i, j, entries, stride;
573 unsigned soff, ssize;
576 // parse image header
577 if (end_buf - buf < 8)
578 return AVERROR_INVALIDDATA;
583 else if (id == 0x4D4D)
586 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
587 return AVERROR_INVALIDDATA;
593 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
594 // that further identifies the file as a TIFF file"
595 if (tget_short(&buf, le) != 42) {
596 av_log(avctx, AV_LOG_ERROR,
597 "The answer to life, universe and everything is not correct!\n");
598 return AVERROR_INVALIDDATA;
600 // Reset these pointers so we can tell if they were set this frame
601 s->stripsizes = s->stripdata = NULL;
602 /* parse image file directory */
603 off = tget_long(&buf, le);
604 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
605 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
606 return AVERROR_INVALIDDATA;
608 buf = orig_buf + off;
609 entries = tget_short(&buf, le);
610 for (i = 0; i < entries; i++) {
611 if ((ret = tiff_decode_tag(s, orig_buf, buf, end_buf)) < 0)
615 if (!s->stripdata && !s->stripoff) {
616 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
617 return AVERROR_INVALIDDATA;
619 /* now we have the data and may start decoding */
620 if ((ret = init_image(s, p)) < 0)
623 if (s->strips == 1 && !s->stripsize) {
624 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
625 s->stripsize = buf_size - s->stripoff;
627 stride = p->linesize[0];
629 for (i = 0; i < s->height; i += s->rps) {
631 if (s->stripsizes >= end_buf)
632 return AVERROR_INVALIDDATA;
633 ssize = tget(&s->stripsizes, s->sstype, s->le);
635 ssize = s->stripsize;
638 if (s->stripdata >= end_buf)
639 return AVERROR_INVALIDDATA;
640 soff = tget(&s->stripdata, s->sot, s->le);
644 if (soff > buf_size || ssize > buf_size - soff) {
645 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
646 return AVERROR_INVALIDDATA;
648 if ((ret = tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
649 FFMIN(s->rps, s->height - i))) < 0) {
650 if (avctx->err_recognition & AV_EF_EXPLODE)
654 dst += s->rps * stride;
656 if (s->predictor == 2) {
659 ssize = s->width * soff;
660 for (i = 0; i < s->height; i++) {
661 for (j = soff; j < ssize; j++)
662 dst[j] += dst[j - soff];
672 for (j = 0; j < s->height; j++) {
673 for (i = 0; i < p->linesize[0]; i++)
674 src[i] = 255 - src[i];
675 src += p->linesize[0];
683 static av_cold int tiff_init(AVCodecContext *avctx)
685 TiffContext *s = avctx->priv_data;
690 ff_lzw_decode_open(&s->lzw);
691 ff_ccitt_unpack_init();
696 static av_cold int tiff_end(AVCodecContext *avctx)
698 TiffContext *const s = avctx->priv_data;
700 ff_lzw_decode_close(&s->lzw);
704 AVCodec ff_tiff_decoder = {
706 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
707 .type = AVMEDIA_TYPE_VIDEO,
708 .id = AV_CODEC_ID_TIFF,
709 .priv_data_size = sizeof(TiffContext),
712 .decode = decode_frame,
713 .capabilities = CODEC_CAP_DR1,