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 if(s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8){
165 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
167 memcpy(dst, src, width);
176 if(s->compr == TIFF_LZW){
177 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
178 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
182 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
184 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
186 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
187 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
191 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
196 memcpy(src2, src, size);
198 for(i = 0; i < size; i++)
199 src2[i] = av_reverse[src[i]];
201 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
206 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
209 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
210 for (line = 0; line < lines; line++) {
211 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
217 for(line = 0; line < lines; line++){
218 if(src - ssrc > size){
219 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
224 if (ssrc + size - src < width)
225 return AVERROR_INVALIDDATA;
226 if (!s->fill_order) {
227 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
228 dst, 1, src, 0, width, 0);
231 for (i = 0; i < width; i++)
232 dst[i] = av_reverse[src[i]];
237 for(pixels = 0; pixels < width;){
238 code = (int8_t)*src++;
241 if(pixels + code > width){
242 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
245 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
246 dst, 1, src, 0, code, pixels);
249 }else if(code != -128){ // -127..-1
251 if(pixels + code > width){
252 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
256 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
257 dst, 0, NULL, c, code, pixels);
263 pixels = ff_lzw_decode(s->lzw, dst, width);
265 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
268 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
269 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
277 static int init_image(TiffContext *s)
282 switch (s->bpp * 10 + s->bppcount) {
284 if (!s->palette_is_set) {
285 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
291 s->avctx->pix_fmt = PIX_FMT_PAL8;
294 s->avctx->pix_fmt = PIX_FMT_RGB24;
297 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
300 s->avctx->pix_fmt = PIX_FMT_RGBA;
303 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
306 av_log(s->avctx, AV_LOG_ERROR,
307 "This format is not supported (bpp=%d, bppcount=%d)\n",
308 s->bpp, s->bppcount);
309 return AVERROR_INVALIDDATA;
311 if (s->width != s->avctx->width || s->height != s->avctx->height) {
312 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
314 avcodec_set_dimensions(s->avctx, s->width, s->height);
316 if (s->picture.data[0])
317 s->avctx->release_buffer(s->avctx, &s->picture);
318 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
319 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
322 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
323 if (s->palette_is_set) {
324 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
326 /* make default grayscale pal */
327 pal = (uint32_t *) s->picture.data[1];
328 for (i = 0; i < 1<<s->bpp; i++)
329 pal[i] = 0xFF << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
335 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
337 int tag, type, count, off, value = 0;
340 const uint8_t *rp, *gp, *bp;
342 if (end_buf - buf < 12)
344 tag = tget_short(&buf, s->le);
345 type = tget_short(&buf, s->le);
346 count = tget_long(&buf, s->le);
347 off = tget_long(&buf, s->le);
354 value = tget(&buf, type, s->le);
370 }else if(type_sizes[type] * count <= 4){
376 if(buf && (buf < start || buf > end_buf)){
377 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
391 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
394 if(count == 1) s->bpp = value;
398 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
403 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
410 case TIFF_SAMPLES_PER_PIXEL:
412 av_log(s->avctx, AV_LOG_ERROR,
413 "Samples per pixel requires a single value, many provided\n");
414 return AVERROR_INVALIDDATA;
416 if (s->bppcount == 1)
434 case TIFF_ADOBE_DEFLATE:
438 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
443 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
446 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
450 case TIFF_ROWSPERSTRIP:
451 if(type == TIFF_LONG && value == -1)
452 value = s->avctx->height;
454 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
459 case TIFF_STRIP_OFFS:
464 s->stripdata = start + off;
466 if(s->strips == 1) s->rps = s->height;
468 if(s->stripdata > end_buf){
469 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
473 case TIFF_STRIP_SIZE:
475 s->stripsizes = NULL;
476 s->stripsize = value;
479 s->stripsizes = start + off;
483 if(s->stripsizes > end_buf){
484 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
488 case TIFF_TILE_BYTE_COUNTS:
489 case TIFF_TILE_LENGTH:
490 case TIFF_TILE_OFFSETS:
491 case TIFF_TILE_WIDTH:
492 av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
493 return AVERROR_PATCHWELCOME;
496 s->predictor = value;
510 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
514 case TIFF_FILL_ORDER:
515 if(value < 1 || value > 2){
516 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
519 s->fill_order = value - 1;
522 pal = (uint32_t *) s->palette;
523 off = type_sizes[type];
524 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
527 gp = buf + count / 3 * off;
528 bp = buf + count / 3 * off * 2;
529 off = (type_sizes[type] - 1) << 3;
530 for(i = 0; i < count / 3; i++){
532 j |= (tget(&rp, type, s->le) >> off) << 16;
533 j |= (tget(&gp, type, s->le) >> off) << 8;
534 j |= tget(&bp, type, s->le) >> off;
537 s->palette_is_set = 1;
541 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
546 if(s->compr == TIFF_G3)
550 if(s->compr == TIFF_G4)
554 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
559 static int decode_frame(AVCodecContext *avctx,
560 void *data, int *data_size,
563 const uint8_t *buf = avpkt->data;
564 int buf_size = avpkt->size;
565 TiffContext * const s = avctx->priv_data;
566 AVFrame *picture = data;
567 AVFrame * const p= (AVFrame*)&s->picture;
568 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
573 unsigned soff, ssize;
577 if (end_buf - buf < 8)
578 return AVERROR_INVALIDDATA;
579 id = AV_RL16(buf); buf += 2;
580 if(id == 0x4949) le = 1;
581 else if(id == 0x4D4D) le = 0;
583 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
590 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
591 // that further identifies the file as a TIFF file"
592 if(tget_short(&buf, le) != 42){
593 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
596 /* parse image file directory */
597 off = tget_long(&buf, le);
598 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
599 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
600 return AVERROR_INVALIDDATA;
602 buf = orig_buf + off;
603 entries = tget_short(&buf, le);
604 for(i = 0; i < entries; i++){
605 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
609 if(!s->stripdata && !s->stripoff){
610 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
613 /* now we have the data and may start decoding */
614 if ((ret = init_image(s)) < 0)
617 if(s->strips == 1 && !s->stripsize){
618 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
619 s->stripsize = buf_size - s->stripoff;
621 stride = p->linesize[0];
623 for(i = 0; i < s->height; i += s->rps){
625 if (s->stripsizes >= end_buf)
626 return AVERROR_INVALIDDATA;
627 ssize = tget(&s->stripsizes, s->sstype, s->le);
629 ssize = s->stripsize;
632 if (s->stripdata >= end_buf)
633 return AVERROR_INVALIDDATA;
634 soff = tget(&s->stripdata, s->sot, s->le);
638 if (soff > buf_size || ssize > buf_size - soff) {
639 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
642 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
644 dst += s->rps * stride;
646 if(s->predictor == 2){
649 ssize = s->width * soff;
650 if (s->avctx->pix_fmt == PIX_FMT_RGB48LE) {
651 for (i = 0; i < s->height; i++) {
652 for (j = soff; j < ssize; j += 2)
653 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
656 } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE) {
657 for (i = 0; i < s->height; i++) {
658 for (j = soff; j < ssize; j += 2)
659 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
663 for(i = 0; i < s->height; i++) {
664 for(j = soff; j < ssize; j++)
665 dst[j] += dst[j - soff];
672 dst = s->picture.data[0];
673 for(i = 0; i < s->height; i++){
674 for(j = 0; j < s->picture.linesize[0]; j++)
675 dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
676 dst += s->picture.linesize[0];
679 *picture= *(AVFrame*)&s->picture;
680 *data_size = sizeof(AVPicture);
685 static av_cold int tiff_init(AVCodecContext *avctx){
686 TiffContext *s = avctx->priv_data;
691 avcodec_get_frame_defaults((AVFrame*)&s->picture);
692 avctx->coded_frame= (AVFrame*)&s->picture;
693 ff_lzw_decode_open(&s->lzw);
694 ff_ccitt_unpack_init();
699 static av_cold int tiff_end(AVCodecContext *avctx)
701 TiffContext * const s = avctx->priv_data;
703 ff_lzw_decode_close(&s->lzw);
704 if(s->picture.data[0])
705 avctx->release_buffer(avctx, &s->picture);
709 AVCodec ff_tiff_decoder = {
711 .type = AVMEDIA_TYPE_VIDEO,
713 .priv_data_size = sizeof(TiffContext),
716 .decode = decode_frame,
717 .capabilities = CODEC_CAP_DR1,
718 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),