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 int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
106 int c, line, pixels, code;
107 const uint8_t *ssrc = src;
108 int width = ((s->width * s->bpp) + 7) >> 3;
110 uint8_t *zbuf; unsigned long outlen;
112 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
114 outlen = width * lines;
115 zbuf = av_malloc(outlen);
116 ret = tiff_uncompress(zbuf, &outlen, src, size);
118 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
123 for(line = 0; line < lines; line++){
124 memcpy(dst, src, width);
132 if(s->compr == TIFF_LZW){
133 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
134 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
138 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
140 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
142 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
143 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
147 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
152 memcpy(src2, src, size);
154 for(i = 0; i < size; i++)
155 src2[i] = av_reverse[src[i]];
157 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
162 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
168 for(line = 0; line < lines; line++){
169 if(src - ssrc > size){
170 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
175 if (!s->fill_order) {
176 memcpy(dst, src, width);
179 for (i = 0; i < width; i++)
180 dst[i] = av_reverse[src[i]];
185 for(pixels = 0; pixels < width;){
186 code = (int8_t)*src++;
189 if(pixels + code > width){
190 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
193 memcpy(dst + pixels, src, code);
196 }else if(code != -128){ // -127..-1
198 if(pixels + code > width){
199 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
203 memset(dst + pixels, c, code);
209 pixels = ff_lzw_decode(s->lzw, dst, width);
211 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
221 static int init_image(TiffContext *s)
226 switch (s->bpp * 10 + s->bppcount) {
228 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
231 s->avctx->pix_fmt = PIX_FMT_PAL8;
234 s->avctx->pix_fmt = PIX_FMT_RGB24;
237 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
240 s->avctx->pix_fmt = PIX_FMT_RGBA;
243 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
246 av_log(s->avctx, AV_LOG_ERROR,
247 "This format is not supported (bpp=%d, bppcount=%d)\n",
248 s->bpp, s->bppcount);
249 return AVERROR_INVALIDDATA;
251 if (s->width != s->avctx->width || s->height != s->avctx->height) {
252 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
254 avcodec_set_dimensions(s->avctx, s->width, s->height);
256 if (s->picture.data[0])
257 s->avctx->release_buffer(s->avctx, &s->picture);
258 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
259 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
262 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
263 if (s->palette_is_set) {
264 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
266 /* make default grayscale pal */
267 pal = (uint32_t *) s->picture.data[1];
268 for (i = 0; i < 256; i++)
269 pal[i] = i * 0x010101;
275 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
277 int tag, type, count, off, value = 0;
280 const uint8_t *rp, *gp, *bp;
282 tag = tget_short(&buf, s->le);
283 type = tget_short(&buf, s->le);
284 count = tget_long(&buf, s->le);
285 off = tget_long(&buf, s->le);
292 value = tget(&buf, type, s->le);
308 }else if(type_sizes[type] * count <= 4){
314 if(buf && (buf < start || buf > end_buf)){
315 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
329 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
332 if(count == 1) s->bpp = value;
336 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
341 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
348 case TIFF_SAMPLES_PER_PIXEL:
350 av_log(s->avctx, AV_LOG_ERROR,
351 "Samples per pixel requires a single value, many provided\n");
352 return AVERROR_INVALIDDATA;
354 if (s->bppcount == 1)
372 case TIFF_ADOBE_DEFLATE:
376 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
381 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
384 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
388 case TIFF_ROWSPERSTRIP:
389 if(type == TIFF_LONG && value == -1)
390 value = s->avctx->height;
392 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
397 case TIFF_STRIP_OFFS:
402 s->stripdata = start + off;
404 if(s->strips == 1) s->rps = s->height;
406 if(s->stripdata > end_buf){
407 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
411 case TIFF_STRIP_SIZE:
413 s->stripsizes = NULL;
414 s->stripsize = value;
417 s->stripsizes = start + off;
421 if(s->stripsizes > end_buf){
422 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
427 s->predictor = value;
441 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
445 case TIFF_FILL_ORDER:
446 if(value < 1 || value > 2){
447 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
450 s->fill_order = value - 1;
453 pal = (uint32_t *) s->palette;
454 off = type_sizes[type];
456 gp = buf + count / 3 * off;
457 bp = buf + count / 3 * off * 2;
458 off = (type_sizes[type] - 1) << 3;
459 for(i = 0; i < count / 3; i++){
461 j |= (tget(&rp, type, s->le) >> off) << 16;
462 j |= (tget(&gp, type, s->le) >> off) << 8;
463 j |= tget(&bp, type, s->le) >> off;
466 s->palette_is_set = 1;
470 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
475 if(s->compr == TIFF_G3)
479 if(s->compr == TIFF_G4)
483 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
488 static int decode_frame(AVCodecContext *avctx,
489 void *data, int *data_size,
492 const uint8_t *buf = avpkt->data;
493 int buf_size = avpkt->size;
494 TiffContext * const s = avctx->priv_data;
495 AVFrame *picture = data;
496 AVFrame * const p= (AVFrame*)&s->picture;
497 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
498 int id, le, off, ret;
500 int stride, soff, ssize;
504 id = AV_RL16(buf); buf += 2;
505 if(id == 0x4949) le = 1;
506 else if(id == 0x4D4D) le = 0;
508 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
515 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
516 // that further identifies the file as a TIFF file"
517 if(tget_short(&buf, le) != 42){
518 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
521 /* parse image file directory */
522 off = tget_long(&buf, le);
523 if(orig_buf + off + 14 >= end_buf){
524 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
527 buf = orig_buf + off;
528 entries = tget_short(&buf, le);
529 for(i = 0; i < entries; i++){
530 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
534 if(!s->stripdata && !s->stripoff){
535 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
538 /* now we have the data and may start decoding */
539 if ((ret = init_image(s)) < 0)
542 if(s->strips == 1 && !s->stripsize){
543 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
544 s->stripsize = buf_size - s->stripoff;
546 stride = p->linesize[0];
548 for(i = 0; i < s->height; i += s->rps){
550 ssize = tget(&s->stripsizes, s->sstype, s->le);
552 ssize = s->stripsize;
554 if (ssize > buf_size) {
555 av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
560 soff = tget(&s->stripdata, s->sot, s->le);
564 av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
565 return AVERROR(EINVAL);
567 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
569 dst += s->rps * stride;
571 if(s->predictor == 2){
574 ssize = s->width * soff;
575 for(i = 0; i < s->height; i++) {
576 for(j = soff; j < ssize; j++)
577 dst[j] += dst[j - soff];
586 src = s->picture.data[0];
587 for(j = 0; j < s->height; j++){
588 for(i = 0; i < s->picture.linesize[0]; i++)
589 src[i] = 255 - src[i];
590 src += s->picture.linesize[0];
593 *picture= *(AVFrame*)&s->picture;
594 *data_size = sizeof(AVPicture);
599 static av_cold int tiff_init(AVCodecContext *avctx){
600 TiffContext *s = avctx->priv_data;
605 avcodec_get_frame_defaults((AVFrame*)&s->picture);
606 avctx->coded_frame= (AVFrame*)&s->picture;
607 ff_lzw_decode_open(&s->lzw);
608 ff_ccitt_unpack_init();
613 static av_cold int tiff_end(AVCodecContext *avctx)
615 TiffContext * const s = avctx->priv_data;
617 ff_lzw_decode_close(&s->lzw);
618 if(s->picture.data[0])
619 avctx->release_buffer(avctx, &s->picture);
623 AVCodec ff_tiff_decoder = {
634 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),