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