2 * Copyright (c) 2006 Konstantin Shishkov
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @author Konstantin Shishkov
34 #include "libavutil/common.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/imgutils.h"
38 typedef struct TiffContext {
39 AVCodecContext *avctx;
43 unsigned int bpp, bppcount;
44 uint32_t palette[256];
53 int strips, rps, sstype;
55 const uint8_t* stripdata;
56 const uint8_t* stripsizes;
57 int stripsize, stripoff;
61 static unsigned tget_short(const uint8_t **p, int le) {
62 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
67 static unsigned tget_long(const uint8_t **p, int le) {
68 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
73 static unsigned tget(const uint8_t **p, int type, int le) {
75 case TIFF_BYTE : return *(*p)++;
76 case TIFF_SHORT: return tget_short(p, le);
77 case TIFF_LONG : return tget_long (p, le);
78 default : return UINT_MAX;
83 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
85 z_stream zstream = { 0 };
88 zstream.next_in = src;
89 zstream.avail_in = size;
90 zstream.next_out = dst;
91 zstream.avail_out = *len;
92 zret = inflateInit(&zstream);
94 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
97 zret = inflate(&zstream, Z_SYNC_FLUSH);
99 *len = zstream.total_out;
100 return zret == Z_STREAM_END ? Z_OK : zret;
104 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
105 int usePtr, const uint8_t *src,
106 uint8_t c, int width, int offset)
110 while (--width >= 0) {
111 dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
112 dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
113 dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
114 dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
115 dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
116 dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
117 dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
118 dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
122 while (--width >= 0) {
123 dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
124 dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
125 dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
126 dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
130 while (--width >= 0) {
131 dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
132 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
137 memcpy(dst + offset, src, width);
139 memset(dst + offset, c, width);
144 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
145 int c, line, pixels, code;
146 const uint8_t *ssrc = src;
147 int width = ((s->width * s->bpp) + 7) >> 3;
149 uint8_t *zbuf; unsigned long outlen;
151 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
153 outlen = width * lines;
154 zbuf = av_malloc(outlen);
155 ret = tiff_uncompress(zbuf, &outlen, src, size);
157 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
162 for(line = 0; line < lines; line++){
163 if(s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8){
164 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
166 memcpy(dst, src, width);
175 if(s->compr == TIFF_LZW){
176 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
177 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
181 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
183 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
185 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
186 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
190 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
195 memcpy(src2, src, size);
197 for(i = 0; i < size; i++)
198 src2[i] = av_reverse[src[i]];
200 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
205 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
208 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
209 for (line = 0; line < lines; line++) {
210 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
216 for(line = 0; line < lines; line++){
217 if(src - ssrc > size){
218 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
223 if (ssrc + size - src < width)
224 return AVERROR_INVALIDDATA;
225 if (!s->fill_order) {
226 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
227 dst, 1, src, 0, width, 0);
230 for (i = 0; i < width; i++)
231 dst[i] = av_reverse[src[i]];
236 for(pixels = 0; pixels < width;){
237 code = (int8_t)*src++;
240 if(pixels + code > width){
241 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
244 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
245 dst, 1, src, 0, code, pixels);
248 }else if(code != -128){ // -127..-1
250 if(pixels + code > width){
251 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
255 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
256 dst, 0, NULL, c, code, pixels);
262 pixels = ff_lzw_decode(s->lzw, dst, width);
264 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
267 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
268 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
276 static int init_image(TiffContext *s)
281 switch (s->bpp * 10 + s->bppcount) {
283 if (!s->palette_is_set) {
284 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
290 s->avctx->pix_fmt = PIX_FMT_PAL8;
293 s->avctx->pix_fmt = PIX_FMT_RGB24;
296 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
299 s->avctx->pix_fmt = PIX_FMT_GRAY8A;
302 s->avctx->pix_fmt = PIX_FMT_RGBA;
305 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
308 s->avctx->pix_fmt = s->le ? PIX_FMT_RGBA64LE : PIX_FMT_RGBA64BE;
311 av_log(s->avctx, AV_LOG_ERROR,
312 "This format is not supported (bpp=%d, bppcount=%d)\n",
313 s->bpp, s->bppcount);
314 return AVERROR_INVALIDDATA;
316 if (s->width != s->avctx->width || s->height != s->avctx->height) {
317 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
319 avcodec_set_dimensions(s->avctx, s->width, s->height);
321 if (s->picture.data[0])
322 s->avctx->release_buffer(s->avctx, &s->picture);
323 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
324 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
327 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
328 if (s->palette_is_set) {
329 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
331 /* make default grayscale pal */
332 pal = (uint32_t *) s->picture.data[1];
333 for (i = 0; i < 1<<s->bpp; i++)
334 pal[i] = 0xFF << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
340 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
342 unsigned tag, type, count, off, value = 0;
345 const uint8_t *rp, *gp, *bp;
347 if (end_buf - buf < 12)
349 tag = tget_short(&buf, s->le);
350 type = tget_short(&buf, s->le);
351 count = tget_long(&buf, s->le);
352 off = tget_long(&buf, s->le);
354 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
355 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n", type);
364 value = tget(&buf, type, s->le);
381 if (count <= 4 && type_sizes[type] * count <= 4) {
388 if(buf && (buf < start || buf > end_buf)){
389 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
403 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
406 if(count == 1) s->bpp = value;
410 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
415 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
422 case TIFF_SAMPLES_PER_PIXEL:
424 av_log(s->avctx, AV_LOG_ERROR,
425 "Samples per pixel requires a single value, many provided\n");
426 return AVERROR_INVALIDDATA;
428 if (s->bppcount == 1)
446 case TIFF_ADOBE_DEFLATE:
450 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
455 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
458 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
462 case TIFF_ROWSPERSTRIP:
463 if (type == TIFF_LONG && value == UINT_MAX)
464 value = s->avctx->height;
466 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
471 case TIFF_STRIP_OFFS:
476 s->stripdata = start + off;
478 if(s->strips == 1) s->rps = s->height;
480 if(s->stripdata > end_buf){
481 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
485 case TIFF_STRIP_SIZE:
487 s->stripsizes = NULL;
488 s->stripsize = value;
491 s->stripsizes = start + off;
495 if(s->stripsizes > end_buf){
496 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
500 case TIFF_TILE_BYTE_COUNTS:
501 case TIFF_TILE_LENGTH:
502 case TIFF_TILE_OFFSETS:
503 case TIFF_TILE_WIDTH:
504 av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
505 return AVERROR_PATCHWELCOME;
508 s->predictor = value;
522 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
526 case TIFF_FILL_ORDER:
527 if(value < 1 || value > 2){
528 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
531 s->fill_order = value - 1;
534 pal = (uint32_t *) s->palette;
535 off = type_sizes[type];
536 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
539 gp = buf + count / 3 * off;
540 bp = buf + count / 3 * off * 2;
541 off = (type_sizes[type] - 1) << 3;
542 for(i = 0; i < count / 3; i++){
544 j |= (tget(&rp, type, s->le) >> off) << 16;
545 j |= (tget(&gp, type, s->le) >> off) << 8;
546 j |= tget(&bp, type, s->le) >> off;
549 s->palette_is_set = 1;
553 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
558 if(s->compr == TIFF_G3)
562 if(s->compr == TIFF_G4)
566 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
571 static int decode_frame(AVCodecContext *avctx,
572 void *data, int *data_size,
575 const uint8_t *buf = avpkt->data;
576 int buf_size = avpkt->size;
577 TiffContext * const s = avctx->priv_data;
578 AVFrame *picture = data;
579 AVFrame * const p = &s->picture;
580 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
585 unsigned soff, ssize;
589 if (end_buf - buf < 8)
590 return AVERROR_INVALIDDATA;
591 id = AV_RL16(buf); buf += 2;
592 if(id == 0x4949) le = 1;
593 else if(id == 0x4D4D) le = 0;
595 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
602 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
603 // that further identifies the file as a TIFF file"
604 if(tget_short(&buf, le) != 42){
605 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
608 // Reset these pointers so we can tell if they were set this frame
609 s->stripsizes = s->stripdata = NULL;
610 /* parse image file directory */
611 off = tget_long(&buf, le);
612 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
613 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
614 return AVERROR_INVALIDDATA;
616 buf = orig_buf + off;
617 entries = tget_short(&buf, le);
618 for(i = 0; i < entries; i++){
619 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
623 if(!s->stripdata && !s->stripoff){
624 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
627 /* now we have the data and may start decoding */
628 if ((ret = init_image(s)) < 0)
631 if(s->strips == 1 && !s->stripsize){
632 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
633 s->stripsize = buf_size - s->stripoff;
635 stride = p->linesize[0];
637 for(i = 0; i < s->height; i += s->rps){
639 if (s->stripsizes >= end_buf)
640 return AVERROR_INVALIDDATA;
641 ssize = tget(&s->stripsizes, s->sstype, s->le);
643 ssize = s->stripsize;
646 if (s->stripdata >= end_buf)
647 return AVERROR_INVALIDDATA;
648 soff = tget(&s->stripdata, s->sot, s->le);
652 if (soff > buf_size || ssize > buf_size - soff) {
653 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
656 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
658 dst += s->rps * stride;
660 if(s->predictor == 2){
663 ssize = s->width * soff;
664 if (s->avctx->pix_fmt == PIX_FMT_RGB48LE ||
665 s->avctx->pix_fmt == PIX_FMT_RGBA64LE) {
666 for (i = 0; i < s->height; i++) {
667 for (j = soff; j < ssize; j += 2)
668 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
671 } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE ||
672 s->avctx->pix_fmt == PIX_FMT_RGBA64BE) {
673 for (i = 0; i < s->height; i++) {
674 for (j = soff; j < ssize; j += 2)
675 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
679 for(i = 0; i < s->height; i++) {
680 for(j = soff; j < ssize; j++)
681 dst[j] += dst[j - soff];
688 dst = s->picture.data[0];
689 for(i = 0; i < s->height; i++){
690 for(j = 0; j < s->picture.linesize[0]; j++)
691 dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
692 dst += s->picture.linesize[0];
695 *picture = s->picture;
696 *data_size = sizeof(AVPicture);
701 static av_cold int tiff_init(AVCodecContext *avctx){
702 TiffContext *s = avctx->priv_data;
707 avcodec_get_frame_defaults(&s->picture);
708 avctx->coded_frame = &s->picture;
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);
720 if(s->picture.data[0])
721 avctx->release_buffer(avctx, &s->picture);
725 AVCodec ff_tiff_decoder = {
727 .type = AVMEDIA_TYPE_VIDEO,
729 .priv_data_size = sizeof(TiffContext),
732 .decode = decode_frame,
733 .capabilities = CODEC_CAP_DR1,
734 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),