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;
59 int strips, rps, sstype;
61 int stripsizesoff, stripsize, stripoff, strippos;
65 static unsigned tget_short(GetByteContext *gb, int le)
67 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
70 static unsigned tget_long(GetByteContext *gb, int le)
72 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
75 static unsigned tget(GetByteContext *gb, int type, int le)
78 case TIFF_BYTE: return bytestream2_get_byte(gb);
79 case TIFF_SHORT: return tget_short(gb, le);
80 case TIFF_LONG: return tget_long(gb, le);
81 default: return UINT_MAX;
86 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
89 z_stream zstream = { 0 };
92 zstream.next_in = src;
93 zstream.avail_in = size;
94 zstream.next_out = dst;
95 zstream.avail_out = *len;
96 zret = inflateInit(&zstream);
98 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
101 zret = inflate(&zstream, Z_SYNC_FLUSH);
102 inflateEnd(&zstream);
103 *len = zstream.total_out;
104 return zret == Z_STREAM_END ? Z_OK : zret;
107 static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
108 const uint8_t *src, int size,
109 int width, int lines)
112 unsigned long outlen;
114 outlen = width * lines;
115 zbuf = av_malloc(outlen);
117 return AVERROR(ENOMEM);
118 ret = tiff_uncompress(zbuf, &outlen, src, size);
120 av_log(s->avctx, AV_LOG_ERROR,
121 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
122 (unsigned long)width * lines, ret);
124 return AVERROR_UNKNOWN;
127 for (line = 0; line < lines; line++) {
128 memcpy(dst, src, width);
138 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
139 const uint8_t *src, int size, int lines)
142 uint8_t *src2 = av_malloc((unsigned)size +
143 FF_INPUT_BUFFER_PADDING_SIZE);
146 av_log(s->avctx, AV_LOG_ERROR,
147 "Error allocating temporary buffer\n");
148 return AVERROR(ENOMEM);
150 if (s->fax_opts & 2) {
151 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
153 return AVERROR_PATCHWELCOME;
155 if (!s->fill_order) {
156 memcpy(src2, src, size);
158 for (i = 0; i < size; i++)
159 src2[i] = ff_reverse[src[i]];
161 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
162 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
163 s->compr, s->fax_opts);
168 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
169 const uint8_t *src, int size, int lines)
172 int c, line, pixels, code, ret;
173 int width = ((s->width * s->bpp) + 7) >> 3;
176 return AVERROR_INVALIDDATA;
178 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
180 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
182 av_log(s->avctx, AV_LOG_ERROR,
183 "zlib support not enabled, "
184 "deflate compression not supported\n");
185 return AVERROR(ENOSYS);
188 if (s->compr == TIFF_LZW) {
189 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
190 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
193 for (line = 0; line < lines; line++) {
194 pixels = ff_lzw_decode(s->lzw, dst, width);
195 if (pixels < width) {
196 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
198 return AVERROR_INVALIDDATA;
204 if (s->compr == TIFF_CCITT_RLE ||
205 s->compr == TIFF_G3 ||
206 s->compr == TIFF_G4) {
207 return tiff_unpack_fax(s, dst, stride, src, size, lines);
210 bytestream2_init(&s->gb, src, size);
211 bytestream2_init_writer(&pb, dst, stride * lines);
213 for (line = 0; line < lines; line++) {
214 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
216 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
219 if (!s->fill_order) {
220 bytestream2_copy_buffer(&pb, &s->gb, width);
223 for (i = 0; i < width; i++)
224 bytestream2_put_byte(&pb, ff_reverse[bytestream2_get_byte(&s->gb)]);
228 for (pixels = 0; pixels < width;) {
229 code = ff_u8_to_s8(bytestream2_get_byte(&s->gb));
232 bytestream2_copy_buffer(&pb, &s->gb, code);
234 } else if (code != -128) { // -127..-1
236 c = bytestream2_get_byte(&s->gb);
237 bytestream2_set_buffer(&pb, c, code);
247 static int init_image(TiffContext *s, AVFrame *frame)
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_GRAY8A;
269 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
272 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
275 av_log(s->avctx, AV_LOG_ERROR,
276 "This format is not supported (bpp=%d, bppcount=%d)\n",
277 s->bpp, s->bppcount);
278 return AVERROR_INVALIDDATA;
280 if (s->width != s->avctx->width || s->height != s->avctx->height) {
281 ret = ff_set_dimensions(s->avctx, s->width, s->height);
285 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
286 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
289 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
290 if (s->palette_is_set) {
291 memcpy(frame->data[1], s->palette, sizeof(s->palette));
293 /* make default grayscale pal */
294 pal = (uint32_t *) frame->data[1];
295 for (i = 0; i < 256; i++)
296 pal[i] = i * 0x010101;
302 static int tiff_decode_tag(TiffContext *s)
304 unsigned tag, type, count, off, value = 0;
307 if (bytestream2_get_bytes_left(&s->gb) < 12)
308 return AVERROR_INVALIDDATA;
309 tag = tget_short(&s->gb, s->le);
310 type = tget_short(&s->gb, s->le);
311 count = tget_long(&s->gb, s->le);
312 off = tget_long(&s->gb, s->le);
313 start = bytestream2_tell(&s->gb);
315 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
316 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
325 bytestream2_seek(&s->gb, -4, SEEK_CUR);
326 value = tget(&s->gb, type, s->le);
333 bytestream2_seek(&s->gb, -4, SEEK_CUR);
338 bytestream2_seek(&s->gb, off, SEEK_SET);
341 if (count <= 4 && type_sizes[type] * count <= 4)
342 bytestream2_seek(&s->gb, -4, SEEK_CUR);
344 bytestream2_seek(&s->gb, off, SEEK_SET);
357 av_log(s->avctx, AV_LOG_ERROR,
358 "This format is not supported (bpp=%d, %d components)\n",
360 return AVERROR_INVALIDDATA;
367 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
368 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
373 for (i = 0; i < count; i++)
374 s->bpp += tget(&s->gb, type, s->le);
381 case TIFF_SAMPLES_PER_PIXEL:
383 av_log(s->avctx, AV_LOG_ERROR,
384 "Samples per pixel requires a single value, many provided\n");
385 return AVERROR_INVALIDDATA;
387 if (s->bppcount == 1)
405 case TIFF_ADOBE_DEFLATE:
409 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
410 return AVERROR(ENOSYS);
414 avpriv_report_missing_feature(s->avctx, "JPEG compression");
415 return AVERROR_PATCHWELCOME;
417 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
419 return AVERROR_INVALIDDATA;
422 case TIFF_ROWSPERSTRIP:
423 if (!value || (type == TIFF_LONG && value == UINT_MAX))
425 s->rps = FFMIN(value, s->height);
427 case TIFF_STRIP_OFFS:
438 case TIFF_STRIP_SIZE:
440 s->stripsizesoff = 0;
441 s->stripsize = value;
444 s->stripsizesoff = off;
450 s->predictor = value;
452 case TIFF_PHOTOMETRIC:
454 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
455 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
456 case TIFF_PHOTOMETRIC_RGB:
457 case TIFF_PHOTOMETRIC_PALETTE:
458 s->photometric = value;
460 case TIFF_PHOTOMETRIC_ALPHA_MASK:
461 case TIFF_PHOTOMETRIC_SEPARATED:
462 case TIFF_PHOTOMETRIC_YCBCR:
463 case TIFF_PHOTOMETRIC_CIE_LAB:
464 case TIFF_PHOTOMETRIC_ICC_LAB:
465 case TIFF_PHOTOMETRIC_ITU_LAB:
466 case TIFF_PHOTOMETRIC_CFA:
467 case TIFF_PHOTOMETRIC_LOG_L:
468 case TIFF_PHOTOMETRIC_LOG_LUV:
469 case TIFF_PHOTOMETRIC_LINEAR_RAW:
470 avpriv_report_missing_feature(s->avctx,
471 "PhotometricInterpretation 0x%04X",
473 return AVERROR_PATCHWELCOME;
475 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
477 return AVERROR_INVALIDDATA;
480 case TIFF_FILL_ORDER:
481 if (value < 1 || value > 2) {
482 av_log(s->avctx, AV_LOG_ERROR,
483 "Unknown FillOrder value %d, trying default one\n", value);
486 s->fill_order = value - 1;
489 GetByteContext pal_gb[3];
490 off = type_sizes[type];
491 if (count / 3 > 256 ||
492 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
493 return AVERROR_INVALIDDATA;
494 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
495 bytestream2_skip(&pal_gb[1], count / 3 * off);
496 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
497 off = (type_sizes[type] - 1) << 3;
498 for (i = 0; i < count / 3; i++) {
499 uint32_t p = 0xFF000000;
500 p |= (tget(&pal_gb[0], type, s->le) >> off) << 16;
501 p |= (tget(&pal_gb[1], type, s->le) >> off) << 8;
502 p |= tget(&pal_gb[2], type, s->le) >> off;
505 s->palette_is_set = 1;
510 avpriv_report_missing_feature(s->avctx, "Planar format");
511 return AVERROR_PATCHWELCOME;
515 if (s->compr == TIFF_G3)
519 if (s->compr == TIFF_G4)
523 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
524 av_log(s->avctx, AV_LOG_ERROR,
525 "Unknown or unsupported tag %d/0X%0X\n",
527 return AVERROR_INVALIDDATA;
530 bytestream2_seek(&s->gb, start, SEEK_SET);
534 static int decode_frame(AVCodecContext *avctx,
535 void *data, int *got_frame, AVPacket *avpkt)
537 TiffContext *const s = avctx->priv_data;
538 AVFrame *const p = data;
541 int i, j, entries, stride;
542 unsigned soff, ssize;
544 GetByteContext stripsizes;
545 GetByteContext stripdata;
547 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
549 // parse image header
551 return AVERROR_INVALIDDATA;
552 id = bytestream2_get_le16(&s->gb);
555 else if (id == 0x4D4D)
558 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
559 return AVERROR_INVALIDDATA;
562 s->photometric = TIFF_PHOTOMETRIC_NONE;
565 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
566 // that further identifies the file as a TIFF file"
567 if (tget_short(&s->gb, le) != 42) {
568 av_log(avctx, AV_LOG_ERROR,
569 "The answer to life, universe and everything is not correct!\n");
570 return AVERROR_INVALIDDATA;
572 // Reset these offsets so we can tell if they were set this frame
573 s->stripsizesoff = s->strippos = 0;
574 /* parse image file directory */
575 off = tget_long(&s->gb, le);
576 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
577 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
578 return AVERROR_INVALIDDATA;
580 bytestream2_seek(&s->gb, off, SEEK_SET);
581 entries = tget_short(&s->gb, le);
582 for (i = 0; i < entries; i++) {
583 if ((ret = tiff_decode_tag(s)) < 0)
586 if (!s->strippos && !s->stripoff) {
587 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
588 return AVERROR_INVALIDDATA;
590 /* now we have the data and may start decoding */
591 if ((ret = init_image(s, p)) < 0)
594 if (s->strips == 1 && !s->stripsize) {
595 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
596 s->stripsize = avpkt->size - s->stripoff;
598 stride = p->linesize[0];
601 if (s->stripsizesoff) {
602 if (s->stripsizesoff >= avpkt->size)
603 return AVERROR_INVALIDDATA;
604 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
605 avpkt->size - s->stripsizesoff);
608 if (s->strippos >= avpkt->size)
609 return AVERROR_INVALIDDATA;
610 bytestream2_init(&stripdata, avpkt->data + s->strippos,
611 avpkt->size - s->strippos);
614 for (i = 0; i < s->height; i += s->rps) {
615 if (s->stripsizesoff)
616 ssize = tget(&stripsizes, s->sstype, le);
618 ssize = s->stripsize;
621 soff = tget(&stripdata, s->sot, le);
625 if (soff > avpkt->size || ssize > avpkt->size - soff) {
626 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
627 return AVERROR_INVALIDDATA;
629 if ((ret = tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
630 FFMIN(s->rps, s->height - i))) < 0) {
631 if (avctx->err_recognition & AV_EF_EXPLODE)
635 dst += s->rps * stride;
637 if (s->predictor == 2) {
640 ssize = s->width * soff;
641 if (s->avctx->pix_fmt == PIX_FMT_RGB48LE) {
642 for (i = 0; i < s->height; i++) {
643 for (j = soff; j < ssize; j += 2)
644 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
647 } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE) {
648 for (i = 0; i < s->height; i++) {
649 for (j = soff; j < ssize; j += 2)
650 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
654 for (i = 0; i < s->height; i++) {
655 for (j = soff; j < ssize; j++)
656 dst[j] += dst[j - soff];
662 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
664 for (i = 0; i < s->height; i++) {
665 for (j = 0; j < p->linesize[0]; j++)
666 dst[j] = 255 - dst[j];
675 static av_cold int tiff_init(AVCodecContext *avctx)
677 TiffContext *s = avctx->priv_data;
682 ff_lzw_decode_open(&s->lzw);
683 ff_ccitt_unpack_init();
688 static av_cold int tiff_end(AVCodecContext *avctx)
690 TiffContext *const s = avctx->priv_data;
692 ff_lzw_decode_close(&s->lzw);
696 AVCodec ff_tiff_decoder = {
698 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
699 .type = AVMEDIA_TYPE_VIDEO,
700 .id = AV_CODEC_ID_TIFF,
701 .priv_data_size = sizeof(TiffContext),
704 .decode = decode_frame,
705 .capabilities = CODEC_CAP_DR1,