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"
37 #include "bytestream.h"
44 typedef struct TiffContext {
45 AVCodecContext *avctx;
49 unsigned int bpp, bppcount;
50 uint32_t palette[256];
54 enum TiffPhotometric photometric;
60 int strips, rps, sstype;
62 int stripsizesoff, stripsize, stripoff, strippos;
66 static unsigned tget_short(GetByteContext *gb, int le)
68 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
71 static unsigned tget_long(GetByteContext *gb, int le)
73 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
76 static unsigned tget(GetByteContext *gb, int type, int le)
79 case TIFF_BYTE: return bytestream2_get_byte(gb);
80 case TIFF_SHORT: return tget_short(gb, le);
81 case TIFF_LONG: return tget_long(gb, le);
82 default: return UINT_MAX;
87 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
90 z_stream zstream = { 0 };
93 zstream.next_in = src;
94 zstream.avail_in = size;
95 zstream.next_out = dst;
96 zstream.avail_out = *len;
97 zret = inflateInit(&zstream);
99 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
102 zret = inflate(&zstream, Z_SYNC_FLUSH);
103 inflateEnd(&zstream);
104 *len = zstream.total_out;
105 return zret == Z_STREAM_END ? Z_OK : zret;
108 static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
109 const uint8_t *src, int size,
110 int width, int lines)
113 unsigned long outlen;
115 outlen = width * lines;
116 zbuf = av_malloc(outlen);
118 return AVERROR(ENOMEM);
119 ret = tiff_uncompress(zbuf, &outlen, src, size);
121 av_log(s->avctx, AV_LOG_ERROR,
122 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
123 (unsigned long)width * lines, ret);
125 return AVERROR_UNKNOWN;
128 for (line = 0; line < lines; line++) {
129 memcpy(dst, src, width);
139 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
140 const uint8_t *src, int size, int lines)
143 uint8_t *src2 = av_malloc((unsigned)size +
144 FF_INPUT_BUFFER_PADDING_SIZE);
147 av_log(s->avctx, AV_LOG_ERROR,
148 "Error allocating temporary buffer\n");
149 return AVERROR(ENOMEM);
151 if (s->fax_opts & 2) {
152 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
154 return AVERROR_PATCHWELCOME;
156 if (!s->fill_order) {
157 memcpy(src2, src, size);
159 for (i = 0; i < size; i++)
160 src2[i] = ff_reverse[src[i]];
162 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
163 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
164 s->compr, s->fax_opts);
169 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
170 const uint8_t *src, int size, int lines)
173 int c, line, pixels, code, ret;
174 int width = ((s->width * s->bpp) + 7) >> 3;
177 width /= s->bppcount;
180 return AVERROR_INVALIDDATA;
182 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
184 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
186 av_log(s->avctx, AV_LOG_ERROR,
187 "zlib support not enabled, "
188 "deflate compression not supported\n");
189 return AVERROR(ENOSYS);
192 if (s->compr == TIFF_LZW) {
193 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
194 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
197 for (line = 0; line < lines; line++) {
198 pixels = ff_lzw_decode(s->lzw, dst, width);
199 if (pixels < width) {
200 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
202 return AVERROR_INVALIDDATA;
208 if (s->compr == TIFF_CCITT_RLE ||
209 s->compr == TIFF_G3 ||
210 s->compr == TIFF_G4) {
211 return tiff_unpack_fax(s, dst, stride, src, size, lines);
214 bytestream2_init(&s->gb, src, size);
215 bytestream2_init_writer(&pb, dst, stride * lines);
217 for (line = 0; line < lines; line++) {
218 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
220 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
223 if (!s->fill_order) {
224 bytestream2_copy_buffer(&pb, &s->gb, width);
227 for (i = 0; i < width; i++)
228 bytestream2_put_byte(&pb, ff_reverse[bytestream2_get_byte(&s->gb)]);
232 for (pixels = 0; pixels < width;) {
233 code = ff_u8_to_s8(bytestream2_get_byte(&s->gb));
236 bytestream2_copy_buffer(&pb, &s->gb, code);
238 } else if (code != -128) { // -127..-1
240 c = bytestream2_get_byte(&s->gb);
241 bytestream2_set_buffer(&pb, c, code);
251 static int init_image(TiffContext *s, AVFrame *frame)
255 switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
257 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
260 s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
263 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
266 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
269 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
272 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
275 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
278 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
281 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
284 s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
287 s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
290 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
293 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
296 av_log(s->avctx, AV_LOG_ERROR,
297 "This format is not supported (bpp=%d, bppcount=%d)\n",
298 s->bpp, s->bppcount);
299 return AVERROR_INVALIDDATA;
301 if (s->width != s->avctx->width || s->height != s->avctx->height) {
302 ret = ff_set_dimensions(s->avctx, s->width, s->height);
306 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
307 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
310 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
311 memcpy(frame->data[1], s->palette, sizeof(s->palette));
316 static int tiff_decode_tag(TiffContext *s)
318 unsigned tag, type, count, off, value = 0;
321 if (bytestream2_get_bytes_left(&s->gb) < 12)
322 return AVERROR_INVALIDDATA;
323 tag = tget_short(&s->gb, s->le);
324 type = tget_short(&s->gb, s->le);
325 count = tget_long(&s->gb, s->le);
326 off = tget_long(&s->gb, s->le);
327 start = bytestream2_tell(&s->gb);
329 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
330 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
339 bytestream2_seek(&s->gb, -4, SEEK_CUR);
340 value = tget(&s->gb, type, s->le);
347 bytestream2_seek(&s->gb, -4, SEEK_CUR);
352 bytestream2_seek(&s->gb, off, SEEK_SET);
355 if (count <= 4 && type_sizes[type] * count <= 4)
356 bytestream2_seek(&s->gb, -4, SEEK_CUR);
358 bytestream2_seek(&s->gb, off, SEEK_SET);
371 av_log(s->avctx, AV_LOG_ERROR,
372 "This format is not supported (bpp=%d, %d components)\n",
374 return AVERROR_INVALIDDATA;
381 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
382 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
387 for (i = 0; i < count; i++)
388 s->bpp += tget(&s->gb, type, s->le);
395 case TIFF_SAMPLES_PER_PIXEL:
397 av_log(s->avctx, AV_LOG_ERROR,
398 "Samples per pixel requires a single value, many provided\n");
399 return AVERROR_INVALIDDATA;
401 if (s->bppcount == 1)
419 case TIFF_ADOBE_DEFLATE:
423 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
424 return AVERROR(ENOSYS);
428 avpriv_report_missing_feature(s->avctx, "JPEG compression");
429 return AVERROR_PATCHWELCOME;
431 avpriv_report_missing_feature(s->avctx, "LZMA compression");
432 return AVERROR_PATCHWELCOME;
434 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
436 return AVERROR_INVALIDDATA;
439 case TIFF_ROWSPERSTRIP:
440 if (!value || (type == TIFF_LONG && value == UINT_MAX))
442 s->rps = FFMIN(value, s->height);
444 case TIFF_STRIP_OFFS:
455 case TIFF_STRIP_SIZE:
457 s->stripsizesoff = 0;
458 s->stripsize = value;
461 s->stripsizesoff = off;
467 s->predictor = value;
469 case TIFF_PHOTOMETRIC:
471 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
472 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
473 case TIFF_PHOTOMETRIC_RGB:
474 case TIFF_PHOTOMETRIC_PALETTE:
475 s->photometric = value;
477 case TIFF_PHOTOMETRIC_ALPHA_MASK:
478 case TIFF_PHOTOMETRIC_SEPARATED:
479 case TIFF_PHOTOMETRIC_YCBCR:
480 case TIFF_PHOTOMETRIC_CIE_LAB:
481 case TIFF_PHOTOMETRIC_ICC_LAB:
482 case TIFF_PHOTOMETRIC_ITU_LAB:
483 case TIFF_PHOTOMETRIC_CFA:
484 case TIFF_PHOTOMETRIC_LOG_L:
485 case TIFF_PHOTOMETRIC_LOG_LUV:
486 case TIFF_PHOTOMETRIC_LINEAR_RAW:
487 avpriv_report_missing_feature(s->avctx,
488 "PhotometricInterpretation 0x%04X",
490 return AVERROR_PATCHWELCOME;
492 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
494 return AVERROR_INVALIDDATA;
497 case TIFF_FILL_ORDER:
498 if (value < 1 || value > 2) {
499 av_log(s->avctx, AV_LOG_ERROR,
500 "Unknown FillOrder value %d, trying default one\n", value);
503 s->fill_order = value - 1;
506 GetByteContext pal_gb[3];
507 off = type_sizes[type];
508 if (count / 3 > 256 ||
509 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
510 return AVERROR_INVALIDDATA;
511 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
512 bytestream2_skip(&pal_gb[1], count / 3 * off);
513 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
514 off = (type_sizes[type] - 1) << 3;
515 for (i = 0; i < count / 3; i++) {
516 uint32_t p = 0xFF000000;
517 p |= (tget(&pal_gb[0], type, s->le) >> off) << 16;
518 p |= (tget(&pal_gb[1], type, s->le) >> off) << 8;
519 p |= tget(&pal_gb[2], type, s->le) >> off;
522 s->palette_is_set = 1;
526 s->planar = value == 2;
529 if (s->compr == TIFF_G3)
533 if (s->compr == TIFF_G4)
537 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
538 av_log(s->avctx, AV_LOG_ERROR,
539 "Unknown or unsupported tag %d/0X%0X\n",
541 return AVERROR_INVALIDDATA;
544 bytestream2_seek(&s->gb, start, SEEK_SET);
548 static int decode_frame(AVCodecContext *avctx,
549 void *data, int *got_frame, AVPacket *avpkt)
551 TiffContext *const s = avctx->priv_data;
552 AVFrame *const p = data;
554 int id, le, ret, plane, planes;
555 int i, j, entries, stride;
556 unsigned soff, ssize;
558 GetByteContext stripsizes;
559 GetByteContext stripdata;
561 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
563 // parse image header
565 return AVERROR_INVALIDDATA;
566 id = bytestream2_get_le16(&s->gb);
569 else if (id == 0x4D4D)
572 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
573 return AVERROR_INVALIDDATA;
576 s->photometric = TIFF_PHOTOMETRIC_NONE;
579 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
580 // that further identifies the file as a TIFF file"
581 if (tget_short(&s->gb, le) != 42) {
582 av_log(avctx, AV_LOG_ERROR,
583 "The answer to life, universe and everything is not correct!\n");
584 return AVERROR_INVALIDDATA;
586 // Reset these offsets so we can tell if they were set this frame
587 s->stripsizesoff = s->strippos = 0;
588 /* parse image file directory */
589 off = tget_long(&s->gb, le);
590 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
591 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
592 return AVERROR_INVALIDDATA;
594 bytestream2_seek(&s->gb, off, SEEK_SET);
595 entries = tget_short(&s->gb, le);
596 for (i = 0; i < entries; i++) {
597 if ((ret = tiff_decode_tag(s)) < 0)
600 if (!s->strippos && !s->stripoff) {
601 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
602 return AVERROR_INVALIDDATA;
604 /* now we have the data and may start decoding */
605 if ((ret = init_image(s, p)) < 0)
608 if (s->strips == 1 && !s->stripsize) {
609 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
610 s->stripsize = avpkt->size - s->stripoff;
613 if (s->stripsizesoff) {
614 if (s->stripsizesoff >= avpkt->size)
615 return AVERROR_INVALIDDATA;
616 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
617 avpkt->size - s->stripsizesoff);
620 if (s->strippos >= avpkt->size)
621 return AVERROR_INVALIDDATA;
622 bytestream2_init(&stripdata, avpkt->data + s->strippos,
623 avpkt->size - s->strippos);
626 planes = s->planar ? s->bppcount : 1;
627 for (plane = 0; plane < planes; plane++) {
628 stride = p->linesize[plane];
629 dst = p->data[plane];
630 for (i = 0; i < s->height; i += s->rps) {
631 if (s->stripsizesoff)
632 ssize = tget(&stripsizes, s->sstype, le);
634 ssize = s->stripsize;
637 soff = tget(&stripdata, s->sot, le);
641 if (soff > avpkt->size || ssize > avpkt->size - soff) {
642 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
643 return AVERROR_INVALIDDATA;
645 if ((ret = tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
646 FFMIN(s->rps, s->height - i))) < 0) {
647 if (avctx->err_recognition & AV_EF_EXPLODE)
651 dst += s->rps * stride;
653 if (s->predictor == 2) {
654 dst = p->data[plane];
656 ssize = s->width * soff;
657 if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
658 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE) {
659 for (i = 0; i < s->height; i++) {
660 for (j = soff; j < ssize; j += 2)
661 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
664 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
665 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
666 for (i = 0; i < s->height; i++) {
667 for (j = soff; j < ssize; j += 2)
668 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
672 for (i = 0; i < s->height; i++) {
673 for (j = soff; j < ssize; j++)
674 dst[j] += dst[j - soff];
680 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
681 dst = p->data[plane];
682 for (i = 0; i < s->height; i++) {
683 for (j = 0; j < stride; j++)
684 dst[j] = 255 - dst[j];
690 if (s->planar && s->bppcount > 2) {
691 FFSWAP(uint8_t*, p->data[0], p->data[2]);
692 FFSWAP(int, p->linesize[0], p->linesize[2]);
693 FFSWAP(uint8_t*, p->data[0], p->data[1]);
694 FFSWAP(int, p->linesize[0], p->linesize[1]);
702 static av_cold int tiff_init(AVCodecContext *avctx)
704 TiffContext *s = avctx->priv_data;
709 ff_lzw_decode_open(&s->lzw);
710 ff_ccitt_unpack_init();
715 static av_cold int tiff_end(AVCodecContext *avctx)
717 TiffContext *const s = avctx->priv_data;
719 ff_lzw_decode_close(&s->lzw);
723 AVCodec ff_tiff_decoder = {
725 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
726 .type = AVMEDIA_TYPE_VIDEO,
727 .id = AV_CODEC_ID_TIFF,
728 .priv_data_size = sizeof(TiffContext),
731 .decode = decode_frame,
732 .capabilities = CODEC_CAP_DR1,