]> git.sesse.net Git - ffmpeg/blob - libavcodec/tiff.c
d0177fa62bf6655510ffc13b6a7dc85ab022011f
[ffmpeg] / libavcodec / tiff.c
1 /*
2  * TIFF image decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 #include "avcodec.h"
23 #ifdef CONFIG_ZLIB
24 #include <zlib.h>
25 #endif
26 #include "lzw.h"
27
28 /* abridged list of TIFF tags */
29 enum TiffTags{
30     TIFF_WIDTH = 0x100,
31     TIFF_HEIGHT,
32     TIFF_BPP,
33     TIFF_COMPR,
34     TIFF_INVERT = 0x106,
35     TIFF_STRIP_OFFS = 0x111,
36     TIFF_ROWSPERSTRIP = 0x116,
37     TIFF_STRIP_SIZE,
38     TIFF_XPOS = 0x11E,
39     TIFF_YPOS = 0x11F,
40     TIFF_PREDICTOR = 0x13D,
41     TIFF_PAL = 0x140
42 };
43
44 enum TiffCompr{
45     TIFF_RAW = 1,
46     TIFF_CCITT_RLE,
47     TIFF_G3,
48     TIFF_G4,
49     TIFF_LZW,
50     TIFF_JPEG,
51     TIFF_NEWJPEG,
52     TIFF_ADOBE_DEFLATE,
53     TIFF_PACKBITS = 0x8005,
54     TIFF_DEFLATE = 0x80B2
55 };
56
57 enum TiffTypes{
58     TIFF_BYTE = 1,
59     TIFF_STRING,
60     TIFF_SHORT,
61     TIFF_LONG,
62     TIFF_LONGLONG
63 };
64
65 /** sizes of various TIFF field types */
66 static const int type_sizes[6] = {
67     0, 1, 100, 2, 4, 8
68 };
69
70 typedef struct TiffContext {
71     AVCodecContext *avctx;
72     AVFrame picture;
73
74     int width, height;
75     unsigned int bpp;
76     int le;
77     int compr;
78     int invert;
79
80     int strips, rps;
81     int sot;
82     uint8_t* stripdata;
83     uint8_t* stripsizes;
84     int stripsize, stripoff;
85     LZWState *lzw;
86 } TiffContext;
87
88 static int tget_short(uint8_t **p, int le){
89     int v = le ? LE_16(*p) : BE_16(*p);
90     *p += 2;
91     return v;
92 }
93
94 static int tget_long(uint8_t **p, int le){
95     int v = le ? LE_32(*p) : BE_32(*p);
96     *p += 4;
97     return v;
98 }
99
100 static int tget(uint8_t **p, int type, int le){
101     switch(type){
102     case TIFF_BYTE : return *(*p)++;
103     case TIFF_SHORT: return tget_short(p, le);
104     case TIFF_LONG : return tget_long (p, le);
105     default        : return -1;
106     }
107 }
108
109 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
110     int c, line, pixels, code;
111     uint8_t *ssrc = src;
112     int width = s->width * (s->bpp / 8);
113 #ifdef CONFIG_ZLIB
114     uint8_t *zbuf; unsigned long outlen;
115
116     if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
117         outlen = width * lines;
118         zbuf = av_malloc(outlen);
119         if(uncompress(zbuf, &outlen, src, size) != Z_OK){
120             av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
121             av_free(zbuf);
122             return -1;
123         }
124         src = zbuf;
125         for(line = 0; line < lines; line++){
126             memcpy(dst, src, width);
127             dst += stride;
128             src += width;
129         }
130         av_free(zbuf);
131         return 0;
132     }
133 #endif
134     if(s->compr == TIFF_LZW){
135         if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
136             av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
137             return -1;
138         }
139     }
140     for(line = 0; line < lines; line++){
141         if(src - ssrc > size){
142             av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
143             return -1;
144         }
145         switch(s->compr){
146         case TIFF_RAW:
147             memcpy(dst, src, s->width * (s->bpp / 8));
148             src += s->width * (s->bpp / 8);
149             break;
150         case TIFF_PACKBITS:
151             for(pixels = 0; pixels < width;){
152                 code = (int8_t)*src++;
153                 if(code >= 0){
154                     code++;
155                     if(pixels + code > width){
156                         av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
157                         return -1;
158                     }
159                     memcpy(dst + pixels, src, code);
160                     src += code;
161                     pixels += code;
162                 }else if(code != -128){ // -127..-1
163                     code = (-code) + 1;
164                     if(pixels + code > width){
165                         av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
166                         return -1;
167                     }
168                     c = *src++;
169                     memset(dst + pixels, c, code);
170                     pixels += code;
171                 }
172             }
173             break;
174         case TIFF_LZW:
175             pixels = ff_lzw_decode(s->lzw, dst, width);
176             if(pixels < width){
177                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
178                 return -1;
179             }
180             break;
181         }
182         dst += stride;
183     }
184     return 0;
185 }
186
187
188 static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
189 {
190     int tag, type, count, off, value = 0;
191     uint8_t *src, *dst;
192     int i, j, ssize, soff, stride;
193     int *pal, *rp, *gp, *bp;
194
195     tag = tget_short(&buf, s->le);
196     type = tget_short(&buf, s->le);
197     count = tget_long(&buf, s->le);
198     off = tget_long(&buf, s->le);
199
200     if(count == 1){
201         switch(type){
202         case TIFF_BYTE:
203         case TIFF_SHORT:
204             buf -= 4;
205             value = tget(&buf, type, s->le);
206             buf = NULL;
207             break;
208         case TIFF_LONG:
209             value = off;
210             buf = NULL;
211             break;
212         default:
213             value = -1;
214             buf = start + off;
215         }
216     }else if(type_sizes[type] * count <= 4){
217         buf -= 4;
218     }else{
219         buf = start + off;
220     }
221
222     if(buf && (buf < start || buf > end_buf)){
223         av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
224         return -1;
225     }
226
227     switch(tag){
228     case TIFF_WIDTH:
229         s->width = value;
230         break;
231     case TIFF_HEIGHT:
232         s->height = value;
233         break;
234     case TIFF_BPP:
235         if(count == 1) s->bpp = value;
236         else{
237             switch(type){
238             case TIFF_BYTE:
239                 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
240                 break;
241             case TIFF_SHORT:
242             case TIFF_LONG:
243                 s->bpp = 0;
244                 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
245                 break;
246             default:
247                 s->bpp = -1;
248             }
249         }
250         switch(s->bpp){
251         case 8:
252             s->avctx->pix_fmt = PIX_FMT_PAL8;
253             break;
254         case 24:
255             s->avctx->pix_fmt = PIX_FMT_RGB24;
256             break;
257         default:
258             av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported (this bpp=%i)\n", s->bpp);
259             return -1;
260         }
261         if(s->width != s->avctx->width || s->height != s->avctx->height){
262             if(avcodec_check_dimensions(s->avctx, s->width, s->height))
263                 return -1;
264             avcodec_set_dimensions(s->avctx, s->width, s->height);
265         }
266         if(s->picture.data[0])
267             s->avctx->release_buffer(s->avctx, &s->picture);
268         if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
269             av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
270             return -1;
271         }
272         if(s->bpp == 8){
273             /* make default grayscale pal */
274             pal = s->picture.data[1];
275             for(i = 0; i < 256; i++)
276                 pal[i] = i * 0x010101;
277         }
278         break;
279     case TIFF_COMPR:
280         s->compr = value;
281         switch(s->compr){
282         case TIFF_RAW:
283         case TIFF_PACKBITS:
284         case TIFF_LZW:
285             break;
286         case TIFF_DEFLATE:
287         case TIFF_ADOBE_DEFLATE:
288 #ifdef CONFIG_ZLIB
289             break;
290 #else
291             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
292             return -1;
293 #endif
294         case TIFF_G3:
295             av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
296             return -1;
297         case TIFF_G4:
298             av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
299             return -1;
300         case TIFF_CCITT_RLE:
301             av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
302             return -1;
303         case TIFF_JPEG:
304         case TIFF_NEWJPEG:
305             av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
306             return -1;
307         default:
308             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
309             return -1;
310         }
311         break;
312     case TIFF_ROWSPERSTRIP:
313         if(value < 1){
314             av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
315             return -1;
316         }
317         s->rps = value;
318         break;
319     case TIFF_STRIP_OFFS:
320         if(count == 1){
321             s->stripdata = NULL;
322             s->stripoff = value;
323         }else
324             s->stripdata = start + off;
325         s->strips = count;
326         s->sot = type;
327         if(s->stripdata > end_buf){
328             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
329             return -1;
330         }
331         break;
332     case TIFF_STRIP_SIZE:
333         if(count == 1){
334             s->stripsizes = NULL;
335             s->stripsize = value;
336             s->strips = 1;
337         }else{
338             s->stripsizes = start + off;
339         }
340         s->strips = count;
341         if(s->stripsizes > end_buf){
342             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
343             return -1;
344         }
345         if(!pic->data[0]){
346             av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
347             return -1;
348         }
349         /* now we have the data and may start decoding */
350         stride = pic->linesize[0];
351         dst = pic->data[0];
352         for(i = 0; i < s->height; i += s->rps){
353             if(s->stripsizes)
354                 ssize = tget(&s->stripsizes, type, s->le);
355             else
356                 ssize = s->stripsize;
357
358             if(s->stripdata){
359                 soff = tget(&s->stripdata, s->sot, s->le);
360             }else
361                 soff = s->stripoff;
362             src = start + soff;
363             if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
364                 break;
365             dst += s->rps * stride;
366         }
367         break;
368     case TIFF_PREDICTOR:
369         if(!pic->data[0]){
370             av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
371             return -1;
372         }
373         if(value == 2){
374             src = pic->data[0];
375             stride = pic->linesize[0];
376             soff = s->bpp >> 3;
377             ssize = s->width * soff;
378             for(i = 0; i < s->height; i++) {
379                 for(j = soff; j < ssize; j++)
380                     src[j] += src[j - soff];
381                 src += stride;
382             }
383         }
384         break;
385     case TIFF_INVERT:
386         switch(value){
387         case 0:
388             s->invert = 1;
389             break;
390         case 1:
391             s->invert = 0;
392             break;
393         case 2:
394         case 3:
395             break;
396         default:
397             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
398             return -1;
399         }
400         break;
401     case TIFF_PAL:
402         if(s->avctx->pix_fmt != PIX_FMT_PAL8){
403             av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
404             return -1;
405         }
406         pal = s->picture.data[1];
407         off = type_sizes[type];
408         rp = buf;
409         gp = buf + count / 3 * off;
410         bp = buf + count / 3 * off * 2;
411         off = (type_sizes[type] - 1) << 3;
412         for(i = 0; i < count / 3; i++){
413             j = (tget(&rp, type, s->le) >> off) << 16;
414             j |= (tget(&gp, type, s->le) >> off) << 8;
415             j |= tget(&bp, type, s->le) >> off;
416             pal[i] = j;
417         }
418     }
419     return 0;
420 }
421
422 static int decode_frame(AVCodecContext *avctx,
423                         void *data, int *data_size,
424                         uint8_t *buf, int buf_size)
425 {
426     TiffContext * const s = avctx->priv_data;
427     AVFrame *picture = data;
428     AVFrame * const p= (AVFrame*)&s->picture;
429     uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
430     int id, le, off;
431     int i, entries;
432
433     //parse image header
434     id = LE_16(buf); buf += 2;
435     if(id == 0x4949) le = 1;
436     else if(id == 0x4D4D) le = 0;
437     else{
438         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
439         return -1;
440     }
441     s->le = le;
442     s->invert = 0;
443     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
444     // that further identifies the file as a TIFF file"
445     if(tget_short(&buf, le) != 42){
446         av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
447         return -1;
448     }
449     /* parse image file directory */
450     off = tget_long(&buf, le);
451     if(orig_buf + off + 14 >= end_buf){
452         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
453         return -1;
454     }
455     buf = orig_buf + off;
456     entries = tget_short(&buf, le);
457     for(i = 0; i < entries; i++){
458         if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
459             return -1;
460         buf += 12;
461     }
462
463     if(s->invert){
464         uint8_t *src;
465         int j;
466
467         src = s->picture.data[0];
468         for(j = 0; j < s->height; j++){
469             for(i = 0; i < s->picture.linesize[0]; i++)
470                 src[i] = 255 - src[i];
471             src += s->picture.linesize[0];
472         }
473     }
474     *picture= *(AVFrame*)&s->picture;
475     *data_size = sizeof(AVPicture);
476
477     return buf_size;
478 }
479
480 static int tiff_init(AVCodecContext *avctx){
481     TiffContext *s = avctx->priv_data;
482
483     s->width = 0;
484     s->height = 0;
485     s->avctx = avctx;
486     avcodec_get_frame_defaults((AVFrame*)&s->picture);
487     avctx->coded_frame= (AVFrame*)&s->picture;
488     s->picture.data[0] = NULL;
489     ff_lzw_decode_open(&s->lzw);
490
491     return 0;
492 }
493
494 static int tiff_end(AVCodecContext *avctx)
495 {
496     TiffContext * const s = avctx->priv_data;
497
498     ff_lzw_decode_close(&s->lzw);
499     if(s->picture.data[0])
500         avctx->release_buffer(avctx, &s->picture);
501     return 0;
502 }
503
504 AVCodec tiff_decoder = {
505     "tiff",
506     CODEC_TYPE_VIDEO,
507     CODEC_ID_TIFF,
508     sizeof(TiffContext),
509     tiff_init,
510     NULL,
511     tiff_end,
512     decode_frame,
513     0,
514     NULL
515 };