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 int tget_short(const uint8_t **p, int le){
62 int v = le ? AV_RL16(*p) : AV_RB16(*p);
67 static int tget_long(const uint8_t **p, int le){
68 int v = le ? AV_RL32(*p) : AV_RB32(*p);
73 static int 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);
83 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
88 memset(&zstream, 0, sizeof(zstream));
89 zstream.next_in = src;
90 zstream.avail_in = size;
91 zstream.next_out = dst;
92 zstream.avail_out = *len;
93 zret = inflateInit(&zstream);
95 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
98 zret = inflate(&zstream, Z_SYNC_FLUSH);
100 *len = zstream.total_out;
101 return zret == Z_STREAM_END ? Z_OK : zret;
105 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
106 int usePtr, const uint8_t *src,
107 uint8_t c, int width, int offset)
111 while (--width >= 0) {
112 dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
113 dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
114 dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
115 dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
116 dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
117 dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
118 dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
119 dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
123 while (--width >= 0) {
124 dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
125 dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
126 dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
127 dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
131 while (--width >= 0) {
132 dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
133 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
138 memcpy(dst + offset, src, width);
140 memset(dst + offset, c, width);
145 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
146 int c, line, pixels, code;
147 const uint8_t *ssrc = src;
148 int width = ((s->width * s->bpp) + 7) >> 3;
150 uint8_t *zbuf; unsigned long outlen;
152 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
154 outlen = width * lines;
155 zbuf = av_malloc(outlen);
156 ret = tiff_uncompress(zbuf, &outlen, src, size);
158 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
163 for(line = 0; line < lines; line++){
164 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
172 if(s->compr == TIFF_LZW){
173 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
174 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
178 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
180 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
182 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
183 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
187 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
192 memcpy(src2, src, size);
194 for(i = 0; i < size; i++)
195 src2[i] = av_reverse[src[i]];
197 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
202 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
206 for (line = 0; line < lines; line++) {
207 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
213 for(line = 0; line < lines; line++){
214 if(src - ssrc > size){
215 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
220 if (ssrc + size - src < width)
221 return AVERROR_INVALIDDATA;
222 if (!s->fill_order) {
223 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
226 for (i = 0; i < width; i++)
227 dst[i] = av_reverse[src[i]];
232 for(pixels = 0; pixels < width;){
233 code = (int8_t)*src++;
236 if(pixels + code > width){
237 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
240 horizontal_fill(s->bpp, dst, 1, src, 0, code, pixels);
243 }else if(code != -128){ // -127..-1
245 if(pixels + code > width){
246 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
250 horizontal_fill(s->bpp, dst, 0, NULL, c, code, pixels);
256 pixels = ff_lzw_decode(s->lzw, dst, width);
258 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
262 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
270 static int init_image(TiffContext *s)
275 switch (s->bpp * 10 + s->bppcount) {
280 s->avctx->pix_fmt = PIX_FMT_PAL8;
283 s->avctx->pix_fmt = PIX_FMT_RGB24;
286 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
289 s->avctx->pix_fmt = PIX_FMT_RGBA;
292 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
295 av_log(s->avctx, AV_LOG_ERROR,
296 "This format is not supported (bpp=%d, bppcount=%d)\n",
297 s->bpp, s->bppcount);
298 return AVERROR_INVALIDDATA;
300 if (s->width != s->avctx->width || s->height != s->avctx->height) {
301 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
303 avcodec_set_dimensions(s->avctx, s->width, s->height);
305 if (s->picture.data[0])
306 s->avctx->release_buffer(s->avctx, &s->picture);
307 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
308 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
311 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
312 if (s->palette_is_set) {
313 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
315 /* make default grayscale pal */
316 pal = (uint32_t *) s->picture.data[1];
317 for (i = 0; i < 1<<s->bpp; i++)
318 pal[i] = 0xFF << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
324 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
326 int tag, type, count, off, value = 0;
329 const uint8_t *rp, *gp, *bp;
331 if (end_buf - buf < 12)
333 tag = tget_short(&buf, s->le);
334 type = tget_short(&buf, s->le);
335 count = tget_long(&buf, s->le);
336 off = tget_long(&buf, s->le);
343 value = tget(&buf, type, s->le);
359 }else if(type_sizes[type] * count <= 4){
365 if(buf && (buf < start || buf > end_buf)){
366 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
380 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
383 if(count == 1) s->bpp = value;
387 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
392 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
399 case TIFF_SAMPLES_PER_PIXEL:
401 av_log(s->avctx, AV_LOG_ERROR,
402 "Samples per pixel requires a single value, many provided\n");
403 return AVERROR_INVALIDDATA;
405 if (s->bppcount == 1)
423 case TIFF_ADOBE_DEFLATE:
427 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
432 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
435 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
439 case TIFF_ROWSPERSTRIP:
440 if(type == TIFF_LONG && value == -1)
441 value = s->avctx->height;
443 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
448 case TIFF_STRIP_OFFS:
453 s->stripdata = start + off;
455 if(s->strips == 1) s->rps = s->height;
457 if(s->stripdata > end_buf){
458 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
462 case TIFF_STRIP_SIZE:
464 s->stripsizes = NULL;
465 s->stripsize = value;
468 s->stripsizes = start + off;
472 if(s->stripsizes > end_buf){
473 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
478 s->predictor = value;
492 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
496 case TIFF_FILL_ORDER:
497 if(value < 1 || value > 2){
498 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
501 s->fill_order = value - 1;
504 pal = (uint32_t *) s->palette;
505 off = type_sizes[type];
506 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
509 gp = buf + count / 3 * off;
510 bp = buf + count / 3 * off * 2;
511 off = (type_sizes[type] - 1) << 3;
512 for(i = 0; i < count / 3; i++){
514 j |= (tget(&rp, type, s->le) >> off) << 16;
515 j |= (tget(&gp, type, s->le) >> off) << 8;
516 j |= tget(&bp, type, s->le) >> off;
519 s->palette_is_set = 1;
523 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
528 if(s->compr == TIFF_G3)
532 if(s->compr == TIFF_G4)
536 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
541 static int decode_frame(AVCodecContext *avctx,
542 void *data, int *data_size,
545 const uint8_t *buf = avpkt->data;
546 int buf_size = avpkt->size;
547 TiffContext * const s = avctx->priv_data;
548 AVFrame *picture = data;
549 AVFrame * const p= (AVFrame*)&s->picture;
550 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
555 unsigned soff, ssize;
559 if (end_buf - buf < 8)
560 return AVERROR_INVALIDDATA;
561 id = AV_RL16(buf); buf += 2;
562 if(id == 0x4949) le = 1;
563 else if(id == 0x4D4D) le = 0;
565 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
572 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
573 // that further identifies the file as a TIFF file"
574 if(tget_short(&buf, le) != 42){
575 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
578 /* parse image file directory */
579 off = tget_long(&buf, le);
580 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
581 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
582 return AVERROR_INVALIDDATA;
584 buf = orig_buf + off;
585 entries = tget_short(&buf, le);
586 for(i = 0; i < entries; i++){
587 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
591 if(!s->stripdata && !s->stripoff){
592 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
595 /* now we have the data and may start decoding */
596 if ((ret = init_image(s)) < 0)
599 if(s->strips == 1 && !s->stripsize){
600 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
601 s->stripsize = buf_size - s->stripoff;
603 stride = p->linesize[0];
605 for(i = 0; i < s->height; i += s->rps){
607 if (s->stripsizes >= end_buf)
608 return AVERROR_INVALIDDATA;
609 ssize = tget(&s->stripsizes, s->sstype, s->le);
611 ssize = s->stripsize;
614 if (s->stripdata >= end_buf)
615 return AVERROR_INVALIDDATA;
616 soff = tget(&s->stripdata, s->sot, s->le);
620 if (soff > buf_size || ssize > buf_size - soff) {
621 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
624 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
626 dst += s->rps * stride;
628 if(s->predictor == 2){
631 ssize = s->width * soff;
632 if (s->avctx->pix_fmt == PIX_FMT_RGB48LE) {
633 for (i = 0; i < s->height; i++) {
634 for (j = soff; j < ssize; j += 2)
635 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
638 } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE) {
639 for (i = 0; i < s->height; i++) {
640 for (j = soff; j < ssize; j += 2)
641 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
645 for(i = 0; i < s->height; i++) {
646 for(j = soff; j < ssize; j++)
647 dst[j] += dst[j - soff];
654 dst = s->picture.data[0];
655 for(i = 0; i < s->height; i++){
656 for(j = 0; j < s->picture.linesize[0]; j++)
657 dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
658 dst += s->picture.linesize[0];
661 *picture= *(AVFrame*)&s->picture;
662 *data_size = sizeof(AVPicture);
667 static av_cold int tiff_init(AVCodecContext *avctx){
668 TiffContext *s = avctx->priv_data;
673 avcodec_get_frame_defaults((AVFrame*)&s->picture);
674 avctx->coded_frame= (AVFrame*)&s->picture;
675 ff_lzw_decode_open(&s->lzw);
676 ff_ccitt_unpack_init();
681 static av_cold int tiff_end(AVCodecContext *avctx)
683 TiffContext * const s = avctx->priv_data;
685 ff_lzw_decode_close(&s->lzw);
686 if(s->picture.data[0])
687 avctx->release_buffer(avctx, &s->picture);
691 AVCodec ff_tiff_decoder = {
693 .type = AVMEDIA_TYPE_VIDEO,
695 .priv_data_size = sizeof(TiffContext),
698 .decode = decode_frame,
699 .capabilities = CODEC_CAP_DR1,
700 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),