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
32 #include "libavutil/common.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/imgutils.h"
36 typedef struct TiffContext {
37 AVCodecContext *avctx;
41 unsigned int bpp, bppcount;
42 uint32_t palette[256];
51 int strips, rps, sstype;
53 const uint8_t* stripdata;
54 const uint8_t* stripsizes;
55 int stripsize, stripoff;
59 static int tget_short(const uint8_t **p, int le){
60 int v = le ? AV_RL16(*p) : AV_RB16(*p);
65 static int tget_long(const uint8_t **p, int le){
66 int v = le ? AV_RL32(*p) : AV_RB32(*p);
71 static int tget(const uint8_t **p, int type, int le){
73 case TIFF_BYTE : return *(*p)++;
74 case TIFF_SHORT: return tget_short(p, le);
75 case TIFF_LONG : return tget_long (p, le);
81 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
86 memset(&zstream, 0, sizeof(zstream));
87 zstream.next_in = src;
88 zstream.avail_in = size;
89 zstream.next_out = dst;
90 zstream.avail_out = *len;
91 zret = inflateInit(&zstream);
93 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
96 zret = inflate(&zstream, Z_SYNC_FLUSH);
98 *len = zstream.total_out;
99 return zret == Z_STREAM_END ? Z_OK : zret;
103 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
104 int c, line, pixels, code;
105 const uint8_t *ssrc = src;
106 int width = ((s->width * s->bpp) + 7) >> 3;
108 uint8_t *zbuf; unsigned long outlen;
110 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
112 outlen = width * lines;
113 zbuf = av_malloc(outlen);
114 ret = tiff_uncompress(zbuf, &outlen, src, size);
116 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
121 for(line = 0; line < lines; line++){
122 memcpy(dst, src, width);
130 if(s->compr == TIFF_LZW){
131 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
132 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
136 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
138 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
140 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
141 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
145 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
150 memcpy(src2, src, size);
152 for(i = 0; i < size; i++)
153 src2[i] = av_reverse[src[i]];
155 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
160 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
166 for(line = 0; line < lines; line++){
167 if(src - ssrc > size){
168 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
173 if (!s->fill_order) {
174 memcpy(dst, src, width);
177 for (i = 0; i < width; i++)
178 dst[i] = av_reverse[src[i]];
183 for(pixels = 0; pixels < width;){
184 code = (int8_t)*src++;
187 if(pixels + code > width){
188 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
191 memcpy(dst + pixels, src, code);
194 }else if(code != -128){ // -127..-1
196 if(pixels + code > width){
197 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
201 memset(dst + pixels, c, code);
207 pixels = ff_lzw_decode(s->lzw, dst, width);
209 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
219 static int init_image(TiffContext *s)
224 switch (s->bpp * 10 + s->bppcount) {
226 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
229 s->avctx->pix_fmt = PIX_FMT_PAL8;
232 s->avctx->pix_fmt = PIX_FMT_RGB24;
235 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
238 s->avctx->pix_fmt = PIX_FMT_RGBA;
241 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
244 av_log(s->avctx, AV_LOG_ERROR,
245 "This format is not supported (bpp=%d, bppcount=%d)\n",
246 s->bpp, s->bppcount);
247 return AVERROR_INVALIDDATA;
249 if (s->width != s->avctx->width || s->height != s->avctx->height) {
250 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
252 avcodec_set_dimensions(s->avctx, s->width, s->height);
254 if (s->picture.data[0])
255 s->avctx->release_buffer(s->avctx, &s->picture);
256 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
257 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
260 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
261 if (s->palette_is_set) {
262 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
264 /* make default grayscale pal */
265 pal = (uint32_t *) s->picture.data[1];
266 for (i = 0; i < 256; i++)
267 pal[i] = i * 0x010101;
273 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
275 int tag, type, count, off, value = 0;
278 const uint8_t *rp, *gp, *bp;
280 tag = tget_short(&buf, s->le);
281 type = tget_short(&buf, s->le);
282 count = tget_long(&buf, s->le);
283 off = tget_long(&buf, s->le);
290 value = tget(&buf, type, s->le);
306 }else if(type_sizes[type] * count <= 4){
312 if(buf && (buf < start || buf > end_buf)){
313 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
327 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
330 if(count == 1) s->bpp = value;
334 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
339 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
346 case TIFF_SAMPLES_PER_PIXEL:
348 av_log(s->avctx, AV_LOG_ERROR,
349 "Samples per pixel requires a single value, many provided\n");
350 return AVERROR_INVALIDDATA;
352 if (s->bppcount == 1)
370 case TIFF_ADOBE_DEFLATE:
374 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
379 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
382 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
386 case TIFF_ROWSPERSTRIP:
387 if(type == TIFF_LONG && value == -1)
388 value = s->avctx->height;
390 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
395 case TIFF_STRIP_OFFS:
400 s->stripdata = start + off;
402 if(s->strips == 1) s->rps = s->height;
404 if(s->stripdata > end_buf){
405 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
409 case TIFF_STRIP_SIZE:
411 s->stripsizes = NULL;
412 s->stripsize = value;
415 s->stripsizes = start + off;
419 if(s->stripsizes > end_buf){
420 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
425 s->predictor = value;
439 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
443 case TIFF_FILL_ORDER:
444 if(value < 1 || value > 2){
445 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
448 s->fill_order = value - 1;
451 pal = (uint32_t *) s->palette;
452 off = type_sizes[type];
454 gp = buf + count / 3 * off;
455 bp = buf + count / 3 * off * 2;
456 off = (type_sizes[type] - 1) << 3;
457 for(i = 0; i < count / 3; i++){
459 j |= (tget(&rp, type, s->le) >> off) << 16;
460 j |= (tget(&gp, type, s->le) >> off) << 8;
461 j |= tget(&bp, type, s->le) >> off;
464 s->palette_is_set = 1;
468 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
473 if(s->compr == TIFF_G3)
477 if(s->compr == TIFF_G4)
482 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
487 static int decode_frame(AVCodecContext *avctx,
488 void *data, int *data_size,
491 const uint8_t *buf = avpkt->data;
492 int buf_size = avpkt->size;
493 TiffContext * const s = avctx->priv_data;
494 AVFrame *picture = data;
495 AVFrame * const p= (AVFrame*)&s->picture;
496 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
497 int id, le, off, ret;
499 int stride, soff, ssize;
503 id = AV_RL16(buf); buf += 2;
504 if(id == 0x4949) le = 1;
505 else if(id == 0x4D4D) le = 0;
507 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
514 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
515 // that further identifies the file as a TIFF file"
516 if(tget_short(&buf, le) != 42){
517 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
520 /* parse image file directory */
521 off = tget_long(&buf, le);
522 if(orig_buf + off + 14 >= end_buf){
523 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
526 buf = orig_buf + off;
527 entries = tget_short(&buf, le);
528 for(i = 0; i < entries; i++){
529 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
533 if(!s->stripdata && !s->stripoff){
534 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
537 /* now we have the data and may start decoding */
538 if ((ret = init_image(s)) < 0)
541 if(s->strips == 1 && !s->stripsize){
542 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
543 s->stripsize = buf_size - s->stripoff;
545 stride = p->linesize[0];
547 for(i = 0; i < s->height; i += s->rps){
549 ssize = tget(&s->stripsizes, s->sstype, s->le);
551 ssize = s->stripsize;
553 if (ssize > buf_size) {
554 av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
559 soff = tget(&s->stripdata, s->sot, s->le);
563 av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
564 return AVERROR(EINVAL);
566 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
568 dst += s->rps * stride;
570 if(s->predictor == 2){
573 ssize = s->width * soff;
574 for(i = 0; i < s->height; i++) {
575 for(j = soff; j < ssize; j++)
576 dst[j] += dst[j - soff];
585 src = s->picture.data[0];
586 for(j = 0; j < s->height; j++){
587 for(i = 0; i < s->picture.linesize[0]; i++)
588 src[i] = 255 - src[i];
589 src += s->picture.linesize[0];
592 *picture= *(AVFrame*)&s->picture;
593 *data_size = sizeof(AVPicture);
598 static av_cold int tiff_init(AVCodecContext *avctx){
599 TiffContext *s = avctx->priv_data;
604 avcodec_get_frame_defaults((AVFrame*)&s->picture);
605 avctx->coded_frame= (AVFrame*)&s->picture;
606 ff_lzw_decode_open(&s->lzw);
607 ff_ccitt_unpack_init();
612 static av_cold int tiff_end(AVCodecContext *avctx)
614 TiffContext * const s = avctx->priv_data;
616 ff_lzw_decode_close(&s->lzw);
617 if(s->picture.data[0])
618 avctx->release_buffer(avctx, &s->picture);
622 AVCodec ff_tiff_decoder = {
633 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),