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