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  code = (int8_t)*src++;
190  if(code >= 0){
191  code++;
192  if(pixels + code > width){
193  av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
194  return -1;
195  }
196  memcpy(dst + pixels, src, code);
197  src += code;
198  pixels += code;
199  }else if(code != -128){ // -127..-1
200  code = (-code) + 1;
201  if(pixels + code > width){
202  av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
203  return -1;
204  }
205  c = *src++;
206  memset(dst + pixels, c, code);
207  pixels += code;
208  }
209  }
210  break;
211  case TIFF_LZW:
212  pixels = ff_lzw_decode(s->lzw, dst, width);
213  if(pixels < width){
214  av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
215  return -1;
216  }
217  break;
218  }
219  dst += stride;
220  }
221  return 0;
222 }
223 
224 static int init_image(TiffContext *s)
225 {
226  int i, ret;
227  uint32_t *pal;
228 
229  switch (s->bpp * 10 + s->bppcount) {
230  case 11:
232  break;
233  case 81:
234  s->avctx->pix_fmt = PIX_FMT_PAL8;
235  break;
236  case 243:
238  break;
239  case 161:
241  break;
242  case 324:
243  s->avctx->pix_fmt = PIX_FMT_RGBA;
244  break;
245  case 483:
247  break;
248  default:
250  "This format is not supported (bpp=%d, bppcount=%d)\n",
251  s->bpp, s->bppcount);
252  return AVERROR_INVALIDDATA;
253  }
254  if (s->width != s->avctx->width || s->height != s->avctx->height) {
255  if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
256  return ret;
258  }
259  if (s->picture.data[0])
260  s->avctx->release_buffer(s->avctx, &s->picture);
261  if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
262  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
263  return ret;
264  }
265  if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
266  if (s->palette_is_set) {
267  memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
268  } else {
269  /* make default grayscale pal */
270  pal = (uint32_t *) s->picture.data[1];
271  for (i = 0; i < 256; i++)
272  pal[i] = i * 0x010101;
273  }
274  }
275  return 0;
276 }
277 
278 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
279 {
280  unsigned tag, type, count, off, value = 0;
281  int i, j;
282  uint32_t *pal;
283  const uint8_t *rp, *gp, *bp;
284 
285  if (end_buf - buf < 12)
286  return -1;
287  tag = tget_short(&buf, s->le);
288  type = tget_short(&buf, s->le);
289  count = tget_long(&buf, s->le);
290  off = tget_long(&buf, s->le);
291 
292  if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
293  av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n", type);
294  return 0;
295  }
296 
297  if(count == 1){
298  switch(type){
299  case TIFF_BYTE:
300  case TIFF_SHORT:
301  buf -= 4;
302  value = tget(&buf, type, s->le);
303  buf = NULL;
304  break;
305  case TIFF_LONG:
306  value = off;
307  buf = NULL;
308  break;
309  case TIFF_STRING:
310  if(count <= 4){
311  buf -= 4;
312  break;
313  }
314  default:
315  value = UINT_MAX;
316  buf = start + off;
317  }
318  } else {
319  if (count <= 4 && type_sizes[type] * count <= 4) {
320  buf -= 4;
321  } else {
322  buf = start + off;
323  }
324  }
325 
326  if(buf && (buf < start || buf > end_buf)){
327  av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
328  return -1;
329  }
330 
331  switch(tag){
332  case TIFF_WIDTH:
333  s->width = value;
334  break;
335  case TIFF_HEIGHT:
336  s->height = value;
337  break;
338  case TIFF_BPP:
339  s->bppcount = count;
340  if(count > 4){
341  av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
342  return -1;
343  }
344  if(count == 1) s->bpp = value;
345  else{
346  switch(type){
347  case TIFF_BYTE:
348  s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
349  break;
350  case TIFF_SHORT:
351  case TIFF_LONG:
352  s->bpp = 0;
353  for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
354  break;
355  default:
356  s->bpp = -1;
357  }
358  }
359  break;
361  if (count != 1) {
363  "Samples per pixel requires a single value, many provided\n");
364  return AVERROR_INVALIDDATA;
365  }
366  if (s->bppcount == 1)
367  s->bpp *= value;
368  s->bppcount = value;
369  break;
370  case TIFF_COMPR:
371  s->compr = value;
372  s->predictor = 0;
373  switch(s->compr){
374  case TIFF_RAW:
375  case TIFF_PACKBITS:
376  case TIFF_LZW:
377  case TIFF_CCITT_RLE:
378  break;
379  case TIFF_G3:
380  case TIFF_G4:
381  s->fax_opts = 0;
382  break;
383  case TIFF_DEFLATE:
384  case TIFF_ADOBE_DEFLATE:
385 #if CONFIG_ZLIB
386  break;
387 #else
388  av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
389  return -1;
390 #endif
391  case TIFF_JPEG:
392  case TIFF_NEWJPEG:
393  av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
394  return -1;
395  default:
396  av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
397  return -1;
398  }
399  break;
400  case TIFF_ROWSPERSTRIP:
401  if (type == TIFF_LONG && value == UINT_MAX)
402  value = s->avctx->height;
403  if(value < 1){
404  av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
405  return -1;
406  }
407  s->rps = value;
408  break;
409  case TIFF_STRIP_OFFS:
410  if(count == 1){
411  s->stripdata = NULL;
412  s->stripoff = value;
413  }else
414  s->stripdata = start + off;
415  s->strips = count;
416  if(s->strips == 1) s->rps = s->height;
417  s->sot = type;
418  if(s->stripdata > end_buf){
419  av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
420  return -1;
421  }
422  break;
423  case TIFF_STRIP_SIZE:
424  if(count == 1){
425  s->stripsizes = NULL;
426  s->stripsize = value;
427  s->strips = 1;
428  }else{
429  s->stripsizes = start + off;
430  }
431  s->strips = count;
432  s->sstype = type;
433  if(s->stripsizes > end_buf){
434  av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
435  return -1;
436  }
437  break;
438  case TIFF_PREDICTOR:
439  s->predictor = value;
440  break;
441  case TIFF_INVERT:
442  switch(value){
443  case 0:
444  s->invert = 1;
445  break;
446  case 1:
447  s->invert = 0;
448  break;
449  case 2:
450  case 3:
451  break;
452  default:
453  av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
454  return -1;
455  }
456  break;
457  case TIFF_FILL_ORDER:
458  if(value < 1 || value > 2){
459  av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
460  value = 1;
461  }
462  s->fill_order = value - 1;
463  break;
464  case TIFF_PAL:
465  pal = (uint32_t *) s->palette;
466  off = type_sizes[type];
467  if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
468  return -1;
469  rp = buf;
470  gp = buf + count / 3 * off;
471  bp = buf + count / 3 * off * 2;
472  off = (type_sizes[type] - 1) << 3;
473  for(i = 0; i < count / 3; i++){
474  j = (tget(&rp, type, s->le) >> off) << 16;
475  j |= (tget(&gp, type, s->le) >> off) << 8;
476  j |= tget(&bp, type, s->le) >> off;
477  pal[i] = j;
478  }
479  s->palette_is_set = 1;
480  break;
481  case TIFF_PLANAR:
482  if(value == 2){
483  av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
484  return -1;
485  }
486  break;
487  case TIFF_T4OPTIONS:
488  if(s->compr == TIFF_G3)
489  s->fax_opts = value;
490  break;
491  case TIFF_T6OPTIONS:
492  if(s->compr == TIFF_G4)
493  s->fax_opts = value;
494  break;
495  default:
496  av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
497  }
498  return 0;
499 }
500 
501 static int decode_frame(AVCodecContext *avctx,
502  void *data, int *data_size,
503  AVPacket *avpkt)
504 {
505  const uint8_t *buf = avpkt->data;
506  int buf_size = avpkt->size;
507  TiffContext * const s = avctx->priv_data;
508  AVFrame *picture = data;
509  AVFrame * const p= (AVFrame*)&s->picture;
510  const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
511  unsigned off;
512  int id, le, ret;
513  int i, j, entries;
514  int stride;
515  unsigned soff, ssize;
516  uint8_t *dst;
517 
518  //parse image header
519  if (end_buf - buf < 8)
520  return AVERROR_INVALIDDATA;
521  id = AV_RL16(buf); buf += 2;
522  if(id == 0x4949) le = 1;
523  else if(id == 0x4D4D) le = 0;
524  else{
525  av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
526  return -1;
527  }
528  s->le = le;
529  s->invert = 0;
530  s->compr = TIFF_RAW;
531  s->fill_order = 0;
532  // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
533  // that further identifies the file as a TIFF file"
534  if(tget_short(&buf, le) != 42){
535  av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
536  return -1;
537  }
538  // Reset these pointers so we can tell if they were set this frame
539  s->stripsizes = s->stripdata = NULL;
540  /* parse image file directory */
541  off = tget_long(&buf, le);
542  if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
543  av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
544  return AVERROR_INVALIDDATA;
545  }
546  buf = orig_buf + off;
547  entries = tget_short(&buf, le);
548  for(i = 0; i < entries; i++){
549  if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
550  return -1;
551  buf += 12;
552  }
553  if(!s->stripdata && !s->stripoff){
554  av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
555  return -1;
556  }
557  /* now we have the data and may start decoding */
558  if ((ret = init_image(s)) < 0)
559  return ret;
560 
561  if(s->strips == 1 && !s->stripsize){
562  av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
563  s->stripsize = buf_size - s->stripoff;
564  }
565  stride = p->linesize[0];
566  dst = p->data[0];
567  for(i = 0; i < s->height; i += s->rps){
568  if(s->stripsizes) {
569  if (s->stripsizes >= end_buf)
570  return AVERROR_INVALIDDATA;
571  ssize = tget(&s->stripsizes, s->sstype, s->le);
572  } else
573  ssize = s->stripsize;
574 
575  if(s->stripdata){
576  if (s->stripdata >= end_buf)
577  return AVERROR_INVALIDDATA;
578  soff = tget(&s->stripdata, s->sot, s->le);
579  }else
580  soff = s->stripoff;
581 
582  if (soff > buf_size || ssize > buf_size - soff) {
583  av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
584  return -1;
585  }
586  if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
587  break;
588  dst += s->rps * stride;
589  }
590  if(s->predictor == 2){
591  dst = p->data[0];
592  soff = s->bpp >> 3;
593  ssize = s->width * soff;
594  for(i = 0; i < s->height; i++) {
595  for(j = soff; j < ssize; j++)
596  dst[j] += dst[j - soff];
597  dst += stride;
598  }
599  }
600 
601  if(s->invert){
602  uint8_t *src;
603  int j;
604 
605  src = s->picture.data[0];
606  for(j = 0; j < s->height; j++){
607  for(i = 0; i < s->picture.linesize[0]; i++)
608  src[i] = 255 - src[i];
609  src += s->picture.linesize[0];
610  }
611  }
612  *picture= *(AVFrame*)&s->picture;
613  *data_size = sizeof(AVPicture);
614 
615  return buf_size;
616 }
617 
618 static av_cold int tiff_init(AVCodecContext *avctx){
619  TiffContext *s = avctx->priv_data;
620 
621  s->width = 0;
622  s->height = 0;
623  s->avctx = avctx;
625  avctx->coded_frame= (AVFrame*)&s->picture;
626  ff_lzw_decode_open(&s->lzw);
628 
629  return 0;
630 }
631 
632 static av_cold int tiff_end(AVCodecContext *avctx)
633 {
634  TiffContext * const s = avctx->priv_data;
635 
637  if(s->picture.data[0])
638  avctx->release_buffer(avctx, &s->picture);
639  return 0;
640 }
641 
643  .name = "tiff",
644  .type = AVMEDIA_TYPE_VIDEO,
645  .id = CODEC_ID_TIFF,
646  .priv_data_size = sizeof(TiffContext),
647  .init = tiff_init,
648  .close = tiff_end,
649  .decode = decode_frame,
650  .capabilities = CODEC_CAP_DR1,
651  .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
652 };