]> git.sesse.net Git - ffmpeg/blob - libavcodec/tiff.c
Drop DCTELEM typedef
[ffmpeg] / libavcodec / tiff.c
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
22 /**
23  * @file
24  * TIFF image decoder
25  * @author Konstantin Shishkov
26  */
27
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 {
43     AVCodecContext *avctx;
44     AVFrame picture;
45
46     int width, height;
47     unsigned int bpp, bppcount;
48     uint32_t palette[256];
49     int palette_is_set;
50     int le;
51     enum TiffCompr compr;
52     int invert;
53     int fax_opts;
54     int predictor;
55     int fill_order;
56
57     int strips, rps, sstype;
58     int sot;
59     const uint8_t *stripdata;
60     const uint8_t *stripsizes;
61     int stripsize, stripoff;
62     LZWState *lzw;
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, ret;
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) {
133             av_log(s->avctx, AV_LOG_ERROR,
134                    "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
135                    (unsigned long)width * lines, ret);
136             av_free(zbuf);
137             return AVERROR_UNKNOWN;
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 ((ret = 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 ret;
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 +
159                                   FF_INPUT_BUFFER_PADDING_SIZE);
160
161         if (!src2) {
162             av_log(s->avctx, AV_LOG_ERROR,
163                    "Error allocating temporary buffer\n");
164             return AVERROR(ENOMEM);
165         }
166         if (s->fax_opts & 2) {
167             av_log(s->avctx, AV_LOG_ERROR,
168                    "Uncompressed fax mode is not supported (yet)\n");
169             av_free(src2);
170             return AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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                 code = (int8_t) * src++;
211                 if (code >= 0) {
212                     code++;
213                     if (pixels + code > width) {
214                         av_log(s->avctx, AV_LOG_ERROR,
215                                "Copy went out of bounds\n");
216                         return AVERROR_INVALIDDATA;
217                     }
218                     memcpy(dst + pixels, src, code);
219                     src += code;
220                     pixels += code;
221                 } else if (code != -128) { // -127..-1
222                     code = (-code) + 1;
223                     if (pixels + code > width) {
224                         av_log(s->avctx, AV_LOG_ERROR,
225                                "Run went out of bounds\n");
226                         return AVERROR_INVALIDDATA;
227                     }
228                     c = *src++;
229                     memset(dst + pixels, c, code);
230                     pixels += code;
231                 }
232             }
233             break;
234         case TIFF_LZW:
235             pixels = ff_lzw_decode(s->lzw, dst, width);
236             if (pixels < width) {
237                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
238                        pixels, width);
239                 return AVERROR_INVALIDDATA;
240             }
241             break;
242         }
243         dst += stride;
244     }
245     return 0;
246 }
247
248 static int init_image(TiffContext *s)
249 {
250     int i, ret;
251     uint32_t *pal;
252
253     switch (s->bpp * 10 + s->bppcount) {
254     case 11:
255         s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
256         break;
257     case 81:
258         s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
259         break;
260     case 243:
261         s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
262         break;
263     case 161:
264         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
265         break;
266     case 324:
267         s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
268         break;
269     case 483:
270         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
271         break;
272     default:
273         av_log(s->avctx, AV_LOG_ERROR,
274                "This format is not supported (bpp=%d, bppcount=%d)\n",
275                s->bpp, s->bppcount);
276         return AVERROR_INVALIDDATA;
277     }
278     if (s->width != s->avctx->width || s->height != s->avctx->height) {
279         if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
280             return ret;
281         avcodec_set_dimensions(s->avctx, s->width, s->height);
282     }
283     if (s->picture.data[0])
284         s->avctx->release_buffer(s->avctx, &s->picture);
285     if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0) {
286         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
287         return ret;
288     }
289     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
290         if (s->palette_is_set) {
291             memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
292         } else {
293             /* make default grayscale pal */
294             pal = (uint32_t *) s->picture.data[1];
295             for (i = 0; i < 256; i++)
296                 pal[i] = i * 0x010101;
297         }
298     }
299     return 0;
300 }
301
302 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
303                            const uint8_t *buf, const uint8_t *end_buf)
304 {
305     unsigned tag, type, count, off, value = 0;
306     int i, j;
307     uint32_t *pal;
308     const uint8_t *rp, *gp, *bp;
309
310     if (end_buf - buf < 12)
311         return AVERROR_INVALIDDATA;
312     tag = tget_short(&buf, s->le);
313     type = tget_short(&buf, s->le);
314     count = tget_long(&buf, s->le);
315     off = tget_long(&buf, s->le);
316
317     if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
318         av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
319                type);
320         return 0;
321     }
322
323     if (count == 1) {
324         switch (type) {
325         case TIFF_BYTE:
326         case TIFF_SHORT:
327             buf -= 4;
328             value = tget(&buf, type, s->le);
329             buf = NULL;
330             break;
331         case TIFF_LONG:
332             value = off;
333             buf = NULL;
334             break;
335         case TIFF_STRING:
336             if (count <= 4) {
337                 buf -= 4;
338                 break;
339             }
340         default:
341             value = UINT_MAX;
342             buf = start + off;
343         }
344     } else {
345         if (count <= 4 && type_sizes[type] * count <= 4) {
346             buf -= 4;
347         } else {
348             buf = start + off;
349         }
350     }
351
352     if (buf && (buf < start || buf > end_buf)) {
353         av_log(s->avctx, AV_LOG_ERROR,
354                "Tag referencing position outside the image\n");
355         return AVERROR_INVALIDDATA;
356     }
357
358     switch (tag) {
359     case TIFF_WIDTH:
360         s->width = value;
361         break;
362     case TIFF_HEIGHT:
363         s->height = value;
364         break;
365     case TIFF_BPP:
366         s->bppcount = count;
367         if (count > 4) {
368             av_log(s->avctx, AV_LOG_ERROR,
369                    "This format is not supported (bpp=%d, %d components)\n",
370                    s->bpp, count);
371             return AVERROR_INVALIDDATA;
372         }
373         if (count == 1)
374             s->bpp = value;
375         else {
376             switch (type) {
377             case TIFF_BYTE:
378                 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
379                          ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
380                 break;
381             case TIFF_SHORT:
382             case TIFF_LONG:
383                 s->bpp = 0;
384                 for (i = 0; i < count && buf < end_buf; i++)
385                     s->bpp += tget(&buf, type, s->le);
386                 break;
387             default:
388                 s->bpp = -1;
389             }
390         }
391         break;
392     case TIFF_SAMPLES_PER_PIXEL:
393         if (count != 1) {
394             av_log(s->avctx, AV_LOG_ERROR,
395                    "Samples per pixel requires a single value, many provided\n");
396             return AVERROR_INVALIDDATA;
397         }
398         if (s->bppcount == 1)
399             s->bpp *= value;
400         s->bppcount = value;
401         break;
402     case TIFF_COMPR:
403         s->compr = value;
404         s->predictor = 0;
405         switch (s->compr) {
406         case TIFF_RAW:
407         case TIFF_PACKBITS:
408         case TIFF_LZW:
409         case TIFF_CCITT_RLE:
410             break;
411         case TIFF_G3:
412         case TIFF_G4:
413             s->fax_opts = 0;
414             break;
415         case TIFF_DEFLATE:
416         case TIFF_ADOBE_DEFLATE:
417 #if CONFIG_ZLIB
418             break;
419 #else
420             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
421             return AVERROR(ENOSYS);
422 #endif
423         case TIFF_JPEG:
424         case TIFF_NEWJPEG:
425             av_log(s->avctx, AV_LOG_ERROR,
426                    "JPEG compression is not supported\n");
427             return AVERROR_PATCHWELCOME;
428         default:
429             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
430                    s->compr);
431             return AVERROR_INVALIDDATA;
432         }
433         break;
434     case TIFF_ROWSPERSTRIP:
435         if (type == TIFF_LONG && value == UINT_MAX)
436             value = s->avctx->height;
437         if (value < 1) {
438             av_log(s->avctx, AV_LOG_ERROR,
439                    "Incorrect value of rows per strip\n");
440             return AVERROR_INVALIDDATA;
441         }
442         s->rps = value;
443         break;
444     case TIFF_STRIP_OFFS:
445         if (count == 1) {
446             s->stripdata = NULL;
447             s->stripoff = value;
448         } else
449             s->stripdata = start + off;
450         s->strips = count;
451         if (s->strips == 1)
452             s->rps = s->height;
453         s->sot = type;
454         if (s->stripdata > end_buf) {
455             av_log(s->avctx, AV_LOG_ERROR,
456                    "Tag referencing position outside the image\n");
457             return AVERROR_INVALIDDATA;
458         }
459         break;
460     case TIFF_STRIP_SIZE:
461         if (count == 1) {
462             s->stripsizes = NULL;
463             s->stripsize = value;
464             s->strips = 1;
465         } else {
466             s->stripsizes = start + off;
467         }
468         s->strips = count;
469         s->sstype = type;
470         if (s->stripsizes > end_buf) {
471             av_log(s->avctx, AV_LOG_ERROR,
472                    "Tag referencing position outside the image\n");
473             return AVERROR_INVALIDDATA;
474         }
475         break;
476     case TIFF_PREDICTOR:
477         s->predictor = value;
478         break;
479     case TIFF_INVERT:
480         switch (value) {
481         case 0:
482             s->invert = 1;
483             break;
484         case 1:
485             s->invert = 0;
486             break;
487         case 2:
488         case 3:
489             break;
490         default:
491             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
492                    value);
493             return AVERROR_INVALIDDATA;
494         }
495         break;
496     case TIFF_FILL_ORDER:
497         if (value < 1 || value > 2) {
498             av_log(s->avctx, AV_LOG_ERROR,
499                    "Unknown FillOrder value %d, trying default one\n", value);
500             value = 1;
501         }
502         s->fill_order = value - 1;
503         break;
504     case TIFF_PAL:
505         pal = (uint32_t *) s->palette;
506         off = type_sizes[type];
507         if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
508             return AVERROR_INVALIDDATA;
509         rp = buf;
510         gp = buf + count / 3 * off;
511         bp = buf + count / 3 * off * 2;
512         off = (type_sizes[type] - 1) << 3;
513         for (i = 0; i < count / 3; i++) {
514             j  = (tget(&rp, type, s->le) >> off) << 16;
515             j |= (tget(&gp, type, s->le) >> off) << 8;
516             j |=  tget(&bp, type, s->le) >> off;
517             pal[i] = j;
518         }
519         s->palette_is_set = 1;
520         break;
521     case TIFF_PLANAR:
522         if (value == 2) {
523             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
524             return AVERROR_PATCHWELCOME;
525         }
526         break;
527     case TIFF_T4OPTIONS:
528         if (s->compr == TIFF_G3)
529             s->fax_opts = value;
530         break;
531     case TIFF_T6OPTIONS:
532         if (s->compr == TIFF_G4)
533             s->fax_opts = value;
534         break;
535     default:
536         av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
537                tag, tag);
538     }
539     return 0;
540 }
541
542 static int decode_frame(AVCodecContext *avctx,
543                         void *data, int *got_frame, AVPacket *avpkt)
544 {
545     const uint8_t *buf = avpkt->data;
546     int buf_size = avpkt->size;
547     TiffContext *const s = avctx->priv_data;
548     AVFrame *picture = data;
549     AVFrame *const p = &s->picture;
550     const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
551     unsigned off;
552     int id, le, ret;
553     int i, j, entries;
554     int stride;
555     unsigned soff, ssize;
556     uint8_t *dst;
557
558     //parse image header
559     if (end_buf - buf < 8)
560         return AVERROR_INVALIDDATA;
561     id = AV_RL16(buf);
562     buf += 2;
563     if (id == 0x4949)
564         le = 1;
565     else if (id == 0x4D4D)
566         le = 0;
567     else {
568         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
569         return AVERROR_INVALIDDATA;
570     }
571     s->le = le;
572     s->invert = 0;
573     s->compr = TIFF_RAW;
574     s->fill_order = 0;
575     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
576     // that further identifies the file as a TIFF file"
577     if (tget_short(&buf, le) != 42) {
578         av_log(avctx, AV_LOG_ERROR,
579                "The answer to life, universe and everything is not correct!\n");
580         return AVERROR_INVALIDDATA;
581     }
582     // Reset these pointers so we can tell if they were set this frame
583     s->stripsizes = s->stripdata = NULL;
584     /* parse image file directory */
585     off = tget_long(&buf, le);
586     if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
587         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
588         return AVERROR_INVALIDDATA;
589     }
590     buf = orig_buf + off;
591     entries = tget_short(&buf, le);
592     for (i = 0; i < entries; i++) {
593         if ((ret = tiff_decode_tag(s, orig_buf, buf, end_buf)) < 0)
594             return ret;
595         buf += 12;
596     }
597     if (!s->stripdata && !s->stripoff) {
598         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
599         return AVERROR_INVALIDDATA;
600     }
601     /* now we have the data and may start decoding */
602     if ((ret = init_image(s)) < 0)
603         return ret;
604
605     if (s->strips == 1 && !s->stripsize) {
606         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
607         s->stripsize = buf_size - s->stripoff;
608     }
609     stride = p->linesize[0];
610     dst = p->data[0];
611     for (i = 0; i < s->height; i += s->rps) {
612         if (s->stripsizes) {
613             if (s->stripsizes >= end_buf)
614                 return AVERROR_INVALIDDATA;
615             ssize = tget(&s->stripsizes, s->sstype, s->le);
616         } else
617             ssize = s->stripsize;
618
619         if (s->stripdata) {
620             if (s->stripdata >= end_buf)
621                 return AVERROR_INVALIDDATA;
622             soff = tget(&s->stripdata, s->sot, s->le);
623         } else
624             soff = s->stripoff;
625
626         if (soff > buf_size || ssize > buf_size - soff) {
627             av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
628             return AVERROR_INVALIDDATA;
629         }
630         if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
631                               FFMIN(s->rps, s->height - i)) < 0)
632             break;
633         dst += s->rps * stride;
634     }
635     if (s->predictor == 2) {
636         dst = p->data[0];
637         soff = s->bpp >> 3;
638         ssize = s->width * soff;
639         for (i = 0; i < s->height; i++) {
640             for (j = soff; j < ssize; j++)
641                 dst[j] += dst[j - soff];
642             dst += stride;
643         }
644     }
645
646     if (s->invert) {
647         uint8_t *src;
648         int j;
649
650         src = s->picture.data[0];
651         for (j = 0; j < s->height; j++) {
652             for (i = 0; i < s->picture.linesize[0]; i++)
653                 src[i] = 255 - src[i];
654             src += s->picture.linesize[0];
655         }
656     }
657     *picture   = s->picture;
658     *got_frame = 1;
659
660     return buf_size;
661 }
662
663 static av_cold int tiff_init(AVCodecContext *avctx)
664 {
665     TiffContext *s = avctx->priv_data;
666
667     s->width = 0;
668     s->height = 0;
669     s->avctx = avctx;
670     avcodec_get_frame_defaults(&s->picture);
671     avctx->coded_frame = &s->picture;
672     ff_lzw_decode_open(&s->lzw);
673     ff_ccitt_unpack_init();
674
675     return 0;
676 }
677
678 static av_cold int tiff_end(AVCodecContext *avctx)
679 {
680     TiffContext *const s = avctx->priv_data;
681
682     ff_lzw_decode_close(&s->lzw);
683     if (s->picture.data[0])
684         avctx->release_buffer(avctx, &s->picture);
685     return 0;
686 }
687
688 AVCodec ff_tiff_decoder = {
689     .name           = "tiff",
690     .type           = AVMEDIA_TYPE_VIDEO,
691     .id             = AV_CODEC_ID_TIFF,
692     .priv_data_size = sizeof(TiffContext),
693     .init           = tiff_init,
694     .close          = tiff_end,
695     .decode         = decode_frame,
696     .capabilities   = CODEC_CAP_DR1,
697     .long_name      = NULL_IF_CONFIG_SMALL("TIFF image"),
698 };