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