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