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