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 unsigned tget_short(const uint8_t **p, int le) {
63 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
68 static unsigned tget_long(const uint8_t **p, int le) {
69 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
74 static unsigned 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);
79 default : return UINT_MAX;
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 unsigned 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);
292 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
293 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n", type);
302 value = tget(&buf, type, s->le);
319 if (count <= 4 && type_sizes[type] * count <= 4) {
326 if(buf && (buf < start || buf > end_buf)){
327 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
341 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
344 if(count == 1) s->bpp = value;
348 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
353 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
360 case TIFF_SAMPLES_PER_PIXEL:
362 av_log(s->avctx, AV_LOG_ERROR,
363 "Samples per pixel requires a single value, many provided\n");
364 return AVERROR_INVALIDDATA;
366 if (s->bppcount == 1)
384 case TIFF_ADOBE_DEFLATE:
388 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
393 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
396 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
400 case TIFF_ROWSPERSTRIP:
401 if (type == TIFF_LONG && value == UINT_MAX)
402 value = s->avctx->height;
404 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
409 case TIFF_STRIP_OFFS:
414 s->stripdata = start + off;
416 if(s->strips == 1) s->rps = s->height;
418 if(s->stripdata > end_buf){
419 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
423 case TIFF_STRIP_SIZE:
425 s->stripsizes = NULL;
426 s->stripsize = value;
429 s->stripsizes = start + off;
433 if(s->stripsizes > end_buf){
434 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
439 s->predictor = value;
453 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
457 case TIFF_FILL_ORDER:
458 if(value < 1 || value > 2){
459 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
462 s->fill_order = value - 1;
465 pal = (uint32_t *) s->palette;
466 off = type_sizes[type];
467 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
470 gp = buf + count / 3 * off;
471 bp = buf + count / 3 * off * 2;
472 off = (type_sizes[type] - 1) << 3;
473 for(i = 0; i < count / 3; i++){
474 j = (tget(&rp, type, s->le) >> off) << 16;
475 j |= (tget(&gp, type, s->le) >> off) << 8;
476 j |= tget(&bp, type, s->le) >> off;
479 s->palette_is_set = 1;
483 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
488 if(s->compr == TIFF_G3)
492 if(s->compr == TIFF_G4)
496 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
501 static int decode_frame(AVCodecContext *avctx,
502 void *data, int *data_size,
505 const uint8_t *buf = avpkt->data;
506 int buf_size = avpkt->size;
507 TiffContext * const s = avctx->priv_data;
508 AVFrame *picture = data;
509 AVFrame * const p = &s->picture;
510 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
515 unsigned soff, ssize;
519 if (end_buf - buf < 8)
520 return AVERROR_INVALIDDATA;
521 id = AV_RL16(buf); buf += 2;
522 if(id == 0x4949) le = 1;
523 else if(id == 0x4D4D) le = 0;
525 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
532 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
533 // that further identifies the file as a TIFF file"
534 if(tget_short(&buf, le) != 42){
535 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
538 /* parse image file directory */
539 off = tget_long(&buf, le);
540 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
541 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
542 return AVERROR_INVALIDDATA;
544 buf = orig_buf + off;
545 entries = tget_short(&buf, le);
546 for(i = 0; i < entries; i++){
547 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
551 if(!s->stripdata && !s->stripoff){
552 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
555 /* now we have the data and may start decoding */
556 if ((ret = init_image(s)) < 0)
559 if(s->strips == 1 && !s->stripsize){
560 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
561 s->stripsize = buf_size - s->stripoff;
563 stride = p->linesize[0];
565 for(i = 0; i < s->height; i += s->rps){
567 if (s->stripsizes >= end_buf)
568 return AVERROR_INVALIDDATA;
569 ssize = tget(&s->stripsizes, s->sstype, s->le);
571 ssize = s->stripsize;
574 if (s->stripdata >= end_buf)
575 return AVERROR_INVALIDDATA;
576 soff = tget(&s->stripdata, s->sot, s->le);
580 if (soff > buf_size || ssize > buf_size - soff) {
581 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
584 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
586 dst += s->rps * stride;
588 if(s->predictor == 2){
591 ssize = s->width * soff;
592 for(i = 0; i < s->height; i++) {
593 for(j = soff; j < ssize; j++)
594 dst[j] += dst[j - soff];
603 src = s->picture.data[0];
604 for(j = 0; j < s->height; j++){
605 for(i = 0; i < s->picture.linesize[0]; i++)
606 src[i] = 255 - src[i];
607 src += s->picture.linesize[0];
610 *picture = s->picture;
611 *data_size = sizeof(AVPicture);
616 static av_cold int tiff_init(AVCodecContext *avctx){
617 TiffContext *s = avctx->priv_data;
622 avcodec_get_frame_defaults(&s->picture);
623 avctx->coded_frame = &s->picture;
624 ff_lzw_decode_open(&s->lzw);
625 ff_ccitt_unpack_init();
630 static av_cold int tiff_end(AVCodecContext *avctx)
632 TiffContext * const s = avctx->priv_data;
634 ff_lzw_decode_close(&s->lzw);
635 if(s->picture.data[0])
636 avctx->release_buffer(avctx, &s->picture);
640 AVCodec ff_tiff_decoder = {
642 .type = AVMEDIA_TYPE_VIDEO,
644 .priv_data_size = sizeof(TiffContext),
647 .decode = decode_frame,
648 .capabilities = CODEC_CAP_DR1,
649 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),