tiff.c
Go to the documentation of this file.
1 /*
2  * TIFF image decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
28 #include "avcodec.h"
29 #include "config.h"
30 #if CONFIG_ZLIB
31 #include <zlib.h>
32 #endif
33 #include "lzw.h"
34 #include "tiff.h"
35 #include "faxcompr.h"
36 #include "internal.h"
37 #include "mathops.h"
38 #include "libavutil/attributes.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/imgutils.h"
41 
42 typedef struct TiffContext {
45 
46  int width, height;
47  unsigned int bpp, bppcount;
48  uint32_t palette[256];
50  int le;
52  int invert;
53  int fax_opts;
54  int predictor;
56 
57  int strips, rps, sstype;
58  int sot;
63 } TiffContext;
64 
65 static unsigned tget_short(const uint8_t **p, int le)
66 {
67  unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
68  *p += 2;
69  return v;
70 }
71 
72 static unsigned tget_long(const uint8_t **p, int le)
73 {
74  unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
75  *p += 4;
76  return v;
77 }
78 
79 static unsigned tget(const uint8_t **p, int type, int le)
80 {
81  switch (type) {
82  case TIFF_BYTE : return *(*p)++;
83  case TIFF_SHORT: return tget_short(p, le);
84  case TIFF_LONG : return tget_long(p, le);
85  default : return UINT_MAX;
86  }
87 }
88 
89 #if CONFIG_ZLIB
90 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
91  int size)
92 {
93  z_stream zstream = { 0 };
94  int zret;
95 
96  zstream.next_in = src;
97  zstream.avail_in = size;
98  zstream.next_out = dst;
99  zstream.avail_out = *len;
100  zret = inflateInit(&zstream);
101  if (zret != Z_OK) {
102  av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
103  return zret;
104  }
105  zret = inflate(&zstream, Z_SYNC_FLUSH);
106  inflateEnd(&zstream);
107  *len = zstream.total_out;
108  return zret == Z_STREAM_END ? Z_OK : zret;
109 }
110 #endif
111 
112 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
113  const uint8_t *src, int size, int lines)
114 {
115  int c, line, pixels, code;
116  const uint8_t *ssrc = src;
117  int width = ((s->width * s->bpp) + 7) >> 3;
118 
119  if (size <= 0)
120  return AVERROR_INVALIDDATA;
121 
122 #if CONFIG_ZLIB
123  if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
124  uint8_t *zbuf;
125  unsigned long outlen;
126  int ret;
127  outlen = width * lines;
128  zbuf = av_malloc(outlen);
129  if (!zbuf)
130  return AVERROR(ENOMEM);
131  ret = tiff_uncompress(zbuf, &outlen, src, size);
132  if (ret != Z_OK) {
134  "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
135  (unsigned long)width * lines, ret);
136  av_free(zbuf);
137  return -1;
138  }
139  src = zbuf;
140  for (line = 0; line < lines; line++) {
141  memcpy(dst, src, width);
142  dst += stride;
143  src += width;
144  }
145  av_free(zbuf);
146  return 0;
147  }
148 #endif
149  if (s->compr == TIFF_LZW) {
150  if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
151  av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
152  return -1;
153  }
154  }
155  if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
156  || s->compr == TIFF_G4) {
157  int i, ret = 0;
158  uint8_t *src2 = av_malloc((unsigned)size +
160 
161  if (!src2) {
163  "Error allocating temporary buffer\n");
164  return AVERROR(ENOMEM);
165  }
166  if (s->fax_opts & 2) {
168  "Uncompressed fax mode is not supported (yet)\n");
169  av_free(src2);
170  return -1;
171  }
172  if (!s->fill_order) {
173  memcpy(src2, src, size);
174  } else {
175  for (i = 0; i < size; i++)
176  src2[i] = ff_reverse[src[i]];
177  }
178  memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
179  switch (s->compr) {
180  case TIFF_CCITT_RLE:
181  case TIFF_G3:
182  case TIFF_G4:
183  ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
184  s->compr, s->fax_opts);
185  break;
186  }
187  av_free(src2);
188  return ret;
189  }
190  for (line = 0; line < lines; line++) {
191  if (src - ssrc > size) {
192  av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
193  return -1;
194  }
195  switch (s->compr) {
196  case TIFF_RAW:
197  if (ssrc + size - src < width)
198  return AVERROR_INVALIDDATA;
199  if (!s->fill_order) {
200  memcpy(dst, src, width);
201  } else {
202  int i;
203  for (i = 0; i < width; i++)
204  dst[i] = ff_reverse[src[i]];
205  }
206  src += width;
207  break;
208  case TIFF_PACKBITS:
209  for (pixels = 0; pixels < width;) {
210  if (ssrc + size - src < 2)
211  return AVERROR_INVALIDDATA;
212  code = (int8_t) * src++;
213  if (code >= 0) {
214  code++;
215  if (pixels + code > width ||
216  ssrc + size - src < code) {
218  "Copy went out of bounds\n");
219  return -1;
220  }
221  memcpy(dst + pixels, src, code);
222  src += code;
223  pixels += code;
224  } else if (code != -128) { // -127..-1
225  code = (-code) + 1;
226  if (pixels + code > width) {
228  "Run went out of bounds\n");
229  return -1;
230  }
231  c = *src++;
232  memset(dst + pixels, c, code);
233  pixels += code;
234  }
235  }
236  break;
237  case TIFF_LZW:
238  pixels = ff_lzw_decode(s->lzw, dst, width);
239  if (pixels < width) {
240  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
241  pixels, width);
242  return -1;
243  }
244  break;
245  }
246  dst += stride;
247  }
248  return 0;
249 }
250 
251 static int init_image(TiffContext *s)
252 {
253  int i, ret;
254  uint32_t *pal;
255 
256  switch (s->bpp * 10 + s->bppcount) {
257  case 11:
259  break;
260  case 81:
262  break;
263  case 243:
265  break;
266  case 161:
268  break;
269  case 324:
271  break;
272  case 483:
274  break;
275  default:
277  "This format is not supported (bpp=%d, bppcount=%d)\n",
278  s->bpp, s->bppcount);
279  return AVERROR_INVALIDDATA;
280  }
281  if (s->width != s->avctx->width || s->height != s->avctx->height) {
282  if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
283  return ret;
285  }
286  if (s->picture.data[0])
287  s->avctx->release_buffer(s->avctx, &s->picture);
288  if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0) {
289  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
290  return ret;
291  }
292  if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
293  if (s->palette_is_set) {
294  memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
295  } else {
296  /* make default grayscale pal */
297  pal = (uint32_t *) s->picture.data[1];
298  for (i = 0; i < 256; i++)
299  pal[i] = i * 0x010101;
300  }
301  }
302  return 0;
303 }
304 
305 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
306  const uint8_t *buf, const uint8_t *end_buf)
307 {
308  unsigned tag, type, count, off, value = 0;
309  int i, j;
310  uint32_t *pal;
311  const uint8_t *rp, *gp, *bp;
312 
313  if (end_buf - buf < 12)
314  return -1;
315  tag = tget_short(&buf, s->le);
316  type = tget_short(&buf, s->le);
317  count = tget_long(&buf, s->le);
318  off = tget_long(&buf, s->le);
319 
320  if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
321  av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
322  type);
323  return 0;
324  }
325 
326  if (count == 1) {
327  switch (type) {
328  case TIFF_BYTE:
329  case TIFF_SHORT:
330  buf -= 4;
331  value = tget(&buf, type, s->le);
332  buf = NULL;
333  break;
334  case TIFF_LONG:
335  value = off;
336  buf = NULL;
337  break;
338  case TIFF_STRING:
339  if (count <= 4) {
340  buf -= 4;
341  break;
342  }
343  default:
344  value = UINT_MAX;
345  buf = start + off;
346  }
347  } else {
348  if (count <= 4 && type_sizes[type] * count <= 4) {
349  buf -= 4;
350  } else {
351  buf = start + off;
352  }
353  }
354 
355  if (buf && (buf < start || buf > end_buf)) {
357  "Tag referencing position outside the image\n");
358  return -1;
359  }
360 
361  switch (tag) {
362  case TIFF_WIDTH:
363  s->width = value;
364  break;
365  case TIFF_HEIGHT:
366  s->height = value;
367  break;
368  case TIFF_BPP:
369  s->bppcount = count;
370  if (count > 4) {
372  "This format is not supported (bpp=%d, %d components)\n",
373  s->bpp, count);
374  return -1;
375  }
376  if (count == 1)
377  s->bpp = value;
378  else {
379  switch (type) {
380  case TIFF_BYTE:
381  s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
382  ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
383  break;
384  case TIFF_SHORT:
385  case TIFF_LONG:
386  s->bpp = 0;
387  for (i = 0; i < count && buf < end_buf; i++)
388  s->bpp += tget(&buf, type, s->le);
389  break;
390  default:
391  s->bpp = -1;
392  }
393  }
394  break;
396  if (count != 1) {
398  "Samples per pixel requires a single value, many provided\n");
399  return AVERROR_INVALIDDATA;
400  }
401  if (s->bppcount == 1)
402  s->bpp *= value;
403  s->bppcount = value;
404  break;
405  case TIFF_COMPR:
406  s->compr = value;
407  s->predictor = 0;
408  switch (s->compr) {
409  case TIFF_RAW:
410  case TIFF_PACKBITS:
411  case TIFF_LZW:
412  case TIFF_CCITT_RLE:
413  break;
414  case TIFF_G3:
415  case TIFF_G4:
416  s->fax_opts = 0;
417  break;
418  case TIFF_DEFLATE:
419  case TIFF_ADOBE_DEFLATE:
420 #if CONFIG_ZLIB
421  break;
422 #else
423  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
424  return -1;
425 #endif
426  case TIFF_JPEG:
427  case TIFF_NEWJPEG:
429  "JPEG compression is not supported\n");
430  return -1;
431  default:
432  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
433  s->compr);
434  return -1;
435  }
436  break;
437  case TIFF_ROWSPERSTRIP:
438  if (type == TIFF_LONG && value == UINT_MAX)
439  value = s->avctx->height;
440  if (value < 1) {
442  "Incorrect value of rows per strip\n");
443  return -1;
444  }
445  s->rps = value;
446  break;
447  case TIFF_STRIP_OFFS:
448  if (count == 1) {
449  s->stripdata = NULL;
450  s->stripoff = value;
451  } else
452  s->stripdata = start + off;
453  s->strips = count;
454  if (s->strips == 1)
455  s->rps = s->height;
456  s->sot = type;
457  if (s->stripdata > end_buf) {
459  "Tag referencing position outside the image\n");
460  return -1;
461  }
462  break;
463  case TIFF_STRIP_SIZE:
464  if (count == 1) {
465  s->stripsizes = NULL;
466  s->stripsize = value;
467  s->strips = 1;
468  } else {
469  s->stripsizes = start + off;
470  }
471  s->strips = count;
472  s->sstype = type;
473  if (s->stripsizes > end_buf) {
475  "Tag referencing position outside the image\n");
476  return -1;
477  }
478  break;
479  case TIFF_PREDICTOR:
480  s->predictor = value;
481  break;
482  case TIFF_INVERT:
483  switch (value) {
484  case 0:
485  s->invert = 1;
486  break;
487  case 1:
488  s->invert = 0;
489  break;
490  case 2:
491  case 3:
492  break;
493  default:
494  av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
495  value);
496  return -1;
497  }
498  break;
499  case TIFF_FILL_ORDER:
500  if (value < 1 || value > 2) {
502  "Unknown FillOrder value %d, trying default one\n", value);
503  value = 1;
504  }
505  s->fill_order = value - 1;
506  break;
507  case TIFF_PAL:
508  pal = (uint32_t *) s->palette;
509  off = type_sizes[type];
510  if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
511  return -1;
512  rp = buf;
513  gp = buf + count / 3 * off;
514  bp = buf + count / 3 * off * 2;
515  off = (type_sizes[type] - 1) << 3;
516  for (i = 0; i < count / 3; i++) {
517  j = (tget(&rp, type, s->le) >> off) << 16;
518  j |= (tget(&gp, type, s->le) >> off) << 8;
519  j |= tget(&bp, type, s->le) >> off;
520  pal[i] = j;
521  }
522  s->palette_is_set = 1;
523  break;
524  case TIFF_PLANAR:
525  if (value == 2) {
526  av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
527  return -1;
528  }
529  break;
530  case TIFF_T4OPTIONS:
531  if (s->compr == TIFF_G3)
532  s->fax_opts = value;
533  break;
534  case TIFF_T6OPTIONS:
535  if (s->compr == TIFF_G4)
536  s->fax_opts = value;
537  break;
538  default:
539  av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
540  tag, tag);
541  }
542  return 0;
543 }
544 
545 static int decode_frame(AVCodecContext *avctx,
546  void *data, int *got_frame, AVPacket *avpkt)
547 {
548  const uint8_t *buf = avpkt->data;
549  int buf_size = avpkt->size;
550  TiffContext *const s = avctx->priv_data;
551  AVFrame *picture = data;
552  AVFrame *const p = &s->picture;
553  const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
554  unsigned off;
555  int id, le, ret;
556  int i, j, entries;
557  int stride;
558  unsigned soff, ssize;
559  uint8_t *dst;
560 
561  //parse image header
562  if (end_buf - buf < 8)
563  return AVERROR_INVALIDDATA;
564  id = AV_RL16(buf);
565  buf += 2;
566  if (id == 0x4949)
567  le = 1;
568  else if (id == 0x4D4D)
569  le = 0;
570  else {
571  av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
572  return -1;
573  }
574  s->le = le;
575  s->invert = 0;
576  s->compr = TIFF_RAW;
577  s->fill_order = 0;
578  // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
579  // that further identifies the file as a TIFF file"
580  if (tget_short(&buf, le) != 42) {
581  av_log(avctx, AV_LOG_ERROR,
582  "The answer to life, universe and everything is not correct!\n");
583  return -1;
584  }
585  // Reset these pointers so we can tell if they were set this frame
586  s->stripsizes = s->stripdata = NULL;
587  /* parse image file directory */
588  off = tget_long(&buf, le);
589  if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
590  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
591  return AVERROR_INVALIDDATA;
592  }
593  buf = orig_buf + off;
594  entries = tget_short(&buf, le);
595  for (i = 0; i < entries; i++) {
596  if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
597  return -1;
598  buf += 12;
599  }
600  if (!s->stripdata && !s->stripoff) {
601  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
602  return -1;
603  }
604  /* now we have the data and may start decoding */
605  if ((ret = init_image(s)) < 0)
606  return ret;
607 
608  if (s->strips == 1 && !s->stripsize) {
609  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
610  s->stripsize = buf_size - s->stripoff;
611  }
612  stride = p->linesize[0];
613  dst = p->data[0];
614  for (i = 0; i < s->height; i += s->rps) {
615  if (s->stripsizes) {
616  if (s->stripsizes >= end_buf)
617  return AVERROR_INVALIDDATA;
618  ssize = tget(&s->stripsizes, s->sstype, s->le);
619  } else
620  ssize = s->stripsize;
621 
622  if (s->stripdata) {
623  if (s->stripdata >= end_buf)
624  return AVERROR_INVALIDDATA;
625  soff = tget(&s->stripdata, s->sot, s->le);
626  } else
627  soff = s->stripoff;
628 
629  if (soff > buf_size || ssize > buf_size - soff) {
630  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
631  return -1;
632  }
633  if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
634  FFMIN(s->rps, s->height - i)) < 0)
635  break;
636  dst += s->rps * stride;
637  }
638  if (s->predictor == 2) {
639  dst = p->data[0];
640  soff = s->bpp >> 3;
641  ssize = s->width * soff;
642  for (i = 0; i < s->height; i++) {
643  for (j = soff; j < ssize; j++)
644  dst[j] += dst[j - soff];
645  dst += stride;
646  }
647  }
648 
649  if (s->invert) {
650  uint8_t *src;
651  int j;
652 
653  src = s->picture.data[0];
654  for (j = 0; j < s->height; j++) {
655  for (i = 0; i < s->picture.linesize[0]; i++)
656  src[i] = 255 - src[i];
657  src += s->picture.linesize[0];
658  }
659  }
660  *picture = s->picture;
661  *got_frame = 1;
662 
663  return buf_size;
664 }
665 
666 static av_cold int tiff_init(AVCodecContext *avctx)
667 {
668  TiffContext *s = avctx->priv_data;
669 
670  s->width = 0;
671  s->height = 0;
672  s->avctx = avctx;
674  avctx->coded_frame = &s->picture;
675  ff_lzw_decode_open(&s->lzw);
677 
678  return 0;
679 }
680 
681 static av_cold int tiff_end(AVCodecContext *avctx)
682 {
683  TiffContext *const s = avctx->priv_data;
684 
686  if (s->picture.data[0])
687  avctx->release_buffer(avctx, &s->picture);
688  return 0;
689 }
690 
692  .name = "tiff",
693  .type = AVMEDIA_TYPE_VIDEO,
694  .id = AV_CODEC_ID_TIFF,
695  .priv_data_size = sizeof(TiffContext),
696  .init = tiff_init,
697  .close = tiff_end,
698  .decode = decode_frame,
699  .capabilities = CODEC_CAP_DR1,
700  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
701 };