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
35 #include "libavutil/common.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/imgutils.h"
39 typedef struct TiffContext {
40 AVCodecContext *avctx;
44 unsigned int bpp, bppcount;
45 uint32_t palette[256];
54 int strips, rps, sstype;
56 const uint8_t* stripdata;
57 const uint8_t* stripsizes;
58 int stripsize, stripoff;
62 static int tget_short(const uint8_t **p, int le){
63 int v = le ? AV_RL16(*p) : AV_RB16(*p);
68 static int tget_long(const uint8_t **p, int le){
69 int v = le ? AV_RL32(*p) : AV_RB32(*p);
74 static int tget(const uint8_t **p, int type, int le){
76 case TIFF_BYTE : return *(*p)++;
77 case TIFF_SHORT: return tget_short(p, le);
78 case TIFF_LONG : return tget_long (p, le);
84 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
89 memset(&zstream, 0, sizeof(zstream));
90 zstream.next_in = src;
91 zstream.avail_in = size;
92 zstream.next_out = dst;
93 zstream.avail_out = *len;
94 zret = inflateInit(&zstream);
96 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
99 zret = inflate(&zstream, Z_SYNC_FLUSH);
100 inflateEnd(&zstream);
101 *len = zstream.total_out;
102 return zret == Z_STREAM_END ? Z_OK : zret;
106 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
107 int c, line, pixels, code;
108 const uint8_t *ssrc = src;
109 int width = ((s->width * s->bpp) + 7) >> 3;
111 uint8_t *zbuf; unsigned long outlen;
113 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
115 outlen = width * lines;
116 zbuf = av_malloc(outlen);
117 ret = tiff_uncompress(zbuf, &outlen, src, size);
119 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
124 for(line = 0; line < lines; line++){
125 memcpy(dst, src, width);
133 if(s->compr == TIFF_LZW){
134 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
135 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
139 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
141 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
143 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
144 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
148 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
153 memcpy(src2, src, size);
155 for(i = 0; i < size; i++)
156 src2[i] = av_reverse[src[i]];
158 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
163 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
169 for(line = 0; line < lines; line++){
170 if(src - ssrc > size){
171 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
176 if (ssrc + size - src < width)
177 return AVERROR_INVALIDDATA;
178 if (!s->fill_order) {
179 memcpy(dst, src, width);
182 for (i = 0; i < width; i++)
183 dst[i] = av_reverse[src[i]];
188 for(pixels = 0; pixels < width;){
189 code = (int8_t)*src++;
192 if(pixels + code > width){
193 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
196 memcpy(dst + pixels, src, code);
199 }else if(code != -128){ // -127..-1
201 if(pixels + code > width){
202 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
206 memset(dst + pixels, c, code);
212 pixels = ff_lzw_decode(s->lzw, dst, width);
214 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
224 static int init_image(TiffContext *s)
229 switch (s->bpp * 10 + s->bppcount) {
231 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
234 s->avctx->pix_fmt = PIX_FMT_PAL8;
237 s->avctx->pix_fmt = PIX_FMT_RGB24;
240 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
243 s->avctx->pix_fmt = PIX_FMT_RGBA;
246 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
249 av_log(s->avctx, AV_LOG_ERROR,
250 "This format is not supported (bpp=%d, bppcount=%d)\n",
251 s->bpp, s->bppcount);
252 return AVERROR_INVALIDDATA;
254 if (s->width != s->avctx->width || s->height != s->avctx->height) {
255 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
257 avcodec_set_dimensions(s->avctx, s->width, s->height);
259 if (s->picture.data[0])
260 s->avctx->release_buffer(s->avctx, &s->picture);
261 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
262 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
265 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
266 if (s->palette_is_set) {
267 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
269 /* make default grayscale pal */
270 pal = (uint32_t *) s->picture.data[1];
271 for (i = 0; i < 256; i++)
272 pal[i] = i * 0x010101;
278 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
280 int tag, type, count, off, value = 0;
283 const uint8_t *rp, *gp, *bp;
285 if (end_buf - buf < 12)
287 tag = tget_short(&buf, s->le);
288 type = tget_short(&buf, s->le);
289 count = tget_long(&buf, s->le);
290 off = tget_long(&buf, s->le);
297 value = tget(&buf, type, s->le);
313 }else if(type_sizes[type] * count <= 4){
319 if(buf && (buf < start || buf > end_buf)){
320 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
334 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
337 if(count == 1) s->bpp = value;
341 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
346 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
353 case TIFF_SAMPLES_PER_PIXEL:
355 av_log(s->avctx, AV_LOG_ERROR,
356 "Samples per pixel requires a single value, many provided\n");
357 return AVERROR_INVALIDDATA;
359 if (s->bppcount == 1)
377 case TIFF_ADOBE_DEFLATE:
381 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
386 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
389 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
393 case TIFF_ROWSPERSTRIP:
394 if(type == TIFF_LONG && value == -1)
395 value = s->avctx->height;
397 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
402 case TIFF_STRIP_OFFS:
407 s->stripdata = start + off;
409 if(s->strips == 1) s->rps = s->height;
411 if(s->stripdata > end_buf){
412 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
416 case TIFF_STRIP_SIZE:
418 s->stripsizes = NULL;
419 s->stripsize = value;
422 s->stripsizes = start + off;
426 if(s->stripsizes > end_buf){
427 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
432 s->predictor = value;
446 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
450 case TIFF_FILL_ORDER:
451 if(value < 1 || value > 2){
452 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
455 s->fill_order = value - 1;
458 pal = (uint32_t *) s->palette;
459 off = type_sizes[type];
460 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
463 gp = buf + count / 3 * off;
464 bp = buf + count / 3 * off * 2;
465 off = (type_sizes[type] - 1) << 3;
466 for(i = 0; i < count / 3; i++){
467 j = (tget(&rp, type, s->le) >> off) << 16;
468 j |= (tget(&gp, type, s->le) >> off) << 8;
469 j |= tget(&bp, type, s->le) >> off;
472 s->palette_is_set = 1;
476 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
481 if(s->compr == TIFF_G3)
485 if(s->compr == TIFF_G4)
489 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
494 static int decode_frame(AVCodecContext *avctx,
495 void *data, int *data_size,
498 const uint8_t *buf = avpkt->data;
499 int buf_size = avpkt->size;
500 TiffContext * const s = avctx->priv_data;
501 AVFrame *picture = data;
502 AVFrame * const p= (AVFrame*)&s->picture;
503 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
508 unsigned soff, ssize;
512 if (end_buf - buf < 8)
513 return AVERROR_INVALIDDATA;
514 id = AV_RL16(buf); buf += 2;
515 if(id == 0x4949) le = 1;
516 else if(id == 0x4D4D) le = 0;
518 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
525 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
526 // that further identifies the file as a TIFF file"
527 if(tget_short(&buf, le) != 42){
528 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
531 /* parse image file directory */
532 off = tget_long(&buf, le);
533 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
534 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
535 return AVERROR_INVALIDDATA;
537 buf = orig_buf + off;
538 entries = tget_short(&buf, le);
539 for(i = 0; i < entries; i++){
540 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
544 if(!s->stripdata && !s->stripoff){
545 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
548 /* now we have the data and may start decoding */
549 if ((ret = init_image(s)) < 0)
552 if(s->strips == 1 && !s->stripsize){
553 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
554 s->stripsize = buf_size - s->stripoff;
556 stride = p->linesize[0];
558 for(i = 0; i < s->height; i += s->rps){
560 if (s->stripsizes >= end_buf)
561 return AVERROR_INVALIDDATA;
562 ssize = tget(&s->stripsizes, s->sstype, s->le);
564 ssize = s->stripsize;
567 if (s->stripdata >= end_buf)
568 return AVERROR_INVALIDDATA;
569 soff = tget(&s->stripdata, s->sot, s->le);
573 if (soff > buf_size || ssize > buf_size - soff) {
574 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
577 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
579 dst += s->rps * stride;
581 if(s->predictor == 2){
584 ssize = s->width * soff;
585 for(i = 0; i < s->height; i++) {
586 for(j = soff; j < ssize; j++)
587 dst[j] += dst[j - soff];
596 src = s->picture.data[0];
597 for(j = 0; j < s->height; j++){
598 for(i = 0; i < s->picture.linesize[0]; i++)
599 src[i] = 255 - src[i];
600 src += s->picture.linesize[0];
603 *picture= *(AVFrame*)&s->picture;
604 *data_size = sizeof(AVPicture);
609 static av_cold int tiff_init(AVCodecContext *avctx){
610 TiffContext *s = avctx->priv_data;
615 avcodec_get_frame_defaults((AVFrame*)&s->picture);
616 avctx->coded_frame= (AVFrame*)&s->picture;
617 ff_lzw_decode_open(&s->lzw);
618 ff_ccitt_unpack_init();
623 static av_cold int tiff_end(AVCodecContext *avctx)
625 TiffContext * const s = avctx->priv_data;
627 ff_lzw_decode_close(&s->lzw);
628 if(s->picture.data[0])
629 avctx->release_buffer(avctx, &s->picture);
633 AVCodec ff_tiff_decoder = {
635 .type = AVMEDIA_TYPE_VIDEO,
637 .priv_data_size = sizeof(TiffContext),
640 .decode = decode_frame,
641 .capabilities = CODEC_CAP_DR1,
642 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),