Browse code

TIFF decoder

Originally committed as revision 6682 to svn://svn.ffmpeg.org/ffmpeg/trunk

Kostya Shishkov authored on 2006/10/13 19:48:29
Showing 8 changed files
... ...
@@ -63,6 +63,7 @@ version <next>
63 63
 - Delphine Software .cin demuxer/audio and video decoder
64 64
 - Tiertex .seq demuxer/video decoder
65 65
 - MTV demuxer
66
+- TIFF picture decoder
66 67
 
67 68
 version 0.4.9-pre1:
68 69
 
... ...
@@ -156,6 +156,7 @@ Codecs:
156 156
   sonic.c                               Alex Beregszaszi
157 157
   svq3.c                                Michael Niedermayer
158 158
   targa.c                               Kostya Shishkov
159
+  tiff.c                                Kostya Shishkov
159 160
   truemotion1*                          Mike Melanson
160 161
   truemotion2*                          Kostya Shishkov
161 162
   truespeech.c                          Kostya Shishkov
... ...
@@ -761,6 +761,7 @@ following image formats are supported:
761 761
 @item animated GIF @tab X @tab X @tab Only uncompressed GIFs are generated.
762 762
 @item PNG          @tab X @tab X @tab 2 bit and 4 bit/pixel not supported yet.
763 763
 @item Targa        @tab   @tab X @tab Targa (.TGA) image format.
764
+@item TIFF         @tab   @tab X @tab Only 24 bit/pixel images are supported.
764 765
 @item SGI          @tab X @tab X @tab SGI RGB image format
765 766
 @end multitable
766 767
 
... ...
@@ -134,6 +134,7 @@ OBJS-$(CONFIG_SVQ1_ENCODER)            += svq1.o
134 134
 OBJS-$(CONFIG_SVQ3_DECODER)            += h264.o
135 135
 OBJS-$(CONFIG_TARGA_DECODER)           += targa.o
136 136
 OBJS-$(CONFIG_THEORA_DECODER)          += vp3.o
137
+OBJS-$(CONFIG_TIFF_DECODER)            += tiff.o
137 138
 OBJS-$(CONFIG_TRUEMOTION1_DECODER)     += truemotion1.o
138 139
 OBJS-$(CONFIG_TRUEMOTION2_DECODER)     += truemotion2.o
139 140
 OBJS-$(CONFIG_TRUESPEECH_DECODER)      += truespeech.o
... ...
@@ -557,6 +557,9 @@ void avcodec_register_all(void)
557 557
 #ifdef CONFIG_TIERTEXSEQVIDEO_DECODER
558 558
     register_avcodec(&tiertexseqvideo_decoder);
559 559
 #endif //CONFIG_TIERTEXSEQVIDEO_DECODER
560
+#ifdef CONFIG_TIFF_DECODER
561
+    register_avcodec(&tiff_decoder);
562
+#endif //CONFIG_TIFF_DECODER
560 563
 
561 564
 #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
562 565
 #ifdef CONFIG_AMR_NB_DECODER
... ...
@@ -37,8 +37,8 @@ extern "C" {
37 37
 #define AV_STRINGIFY(s)         AV_TOSTRING(s)
38 38
 #define AV_TOSTRING(s) #s
39 39
 
40
-#define LIBAVCODEC_VERSION_INT  ((51<<16)+(19<<8)+0)
41
-#define LIBAVCODEC_VERSION      51.19.0
40
+#define LIBAVCODEC_VERSION_INT  ((51<<16)+(20<<8)+0)
41
+#define LIBAVCODEC_VERSION      51.20.0
42 42
 #define LIBAVCODEC_BUILD        LIBAVCODEC_VERSION_INT
43 43
 
44 44
 #define LIBAVCODEC_IDENT        "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
... ...
@@ -147,6 +147,7 @@ enum CodecID {
147 147
     CODEC_ID_TARGA,
148 148
     CODEC_ID_DSICINVIDEO,
149 149
     CODEC_ID_TIERTEXSEQVIDEO,
150
+    CODEC_ID_TIFF,
150 151
 
151 152
     /* various pcm "codecs" */
152 153
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2300,6 +2301,7 @@ extern AVCodec targa_decoder;
2300 2300
 extern AVCodec dsicinvideo_decoder;
2301 2301
 extern AVCodec dsicinaudio_decoder;
2302 2302
 extern AVCodec tiertexseqvideo_decoder;
2303
+extern AVCodec tiff_decoder;
2303 2304
 
2304 2305
 /* pcm codecs */
2305 2306
 #define PCM_CODEC(id, name) \
2306 2307
new file mode 100644
... ...
@@ -0,0 +1,432 @@
0
+/*
1
+ * TIFF image decoder
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
+#include "avcodec.h"
22
+#ifdef CONFIG_ZLIB
23
+#include <zlib.h>
24
+#endif
25
+
26
+/* abridged list of TIFF tags */
27
+enum TiffTags{
28
+    TIFF_WIDTH = 0x100,
29
+    TIFF_HEIGHT,
30
+    TIFF_BPP,
31
+    TIFF_COMPR,
32
+    TIFF_INVERT = 0x106,
33
+    TIFF_STRIP_OFFS = 0x111,
34
+    TIFF_ROWSPERSTRIP = 0x116,
35
+    TIFF_STRIP_SIZE,
36
+    TIFF_XPOS = 0x11E,
37
+    TIFF_YPOS = 0x11F,
38
+    TIFF_PREDICTOR = 0x13D
39
+};
40
+
41
+enum TiffCompr{
42
+    TIFF_RAW = 1,
43
+    TIFF_CCITT_RLE,
44
+    TIFF_G3,
45
+    TIFF_G4,
46
+    TIFF_LZW,
47
+    TIFF_JPEG,
48
+    TIFF_NEWJPEG,
49
+    TIFF_ADOBE_DEFLATE,
50
+    TIFF_PACKBITS = 0x8005,
51
+    TIFF_DEFLATE = 0x80B2
52
+};
53
+
54
+enum TiffTypes{
55
+    TIFF_BYTE = 1,
56
+    TIFF_STRING,
57
+    TIFF_SHORT,
58
+    TIFF_LONG,
59
+    TIFF_LONGLONG
60
+};
61
+
62
+typedef struct TiffContext {
63
+    AVCodecContext *avctx;
64
+    AVFrame picture;
65
+
66
+    int width, height;
67
+    unsigned int bpp;
68
+    int le;
69
+    int compr;
70
+
71
+    int strips, rps;
72
+    int sot;
73
+    uint8_t* stripdata;
74
+    uint8_t* stripsizes;
75
+    int stripsize, stripoff;
76
+} TiffContext;
77
+
78
+static int tget_short(uint8_t **p, int le){
79
+    int v = le ? LE_16(*p) : BE_16(*p);
80
+    *p += 2;
81
+    return v;
82
+}
83
+
84
+static int tget_long(uint8_t **p, int le){
85
+    int v = le ? LE_32(*p) : BE_32(*p);
86
+    *p += 4;
87
+    return v;
88
+}
89
+
90
+static int tget(uint8_t **p, int type, int le){
91
+    switch(type){
92
+    case TIFF_BYTE : return *(*p)++;
93
+    case TIFF_SHORT: return tget_short(p, le);
94
+    case TIFF_LONG : return tget_long (p, le);
95
+    default        : return -1;
96
+    }
97
+}
98
+
99
+static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
100
+    int c, line, pixels, code;
101
+    uint8_t *ssrc = src;
102
+    int width = s->width * (s->bpp / 8);
103
+#ifdef CONFIG_ZLIB
104
+    uint8_t *zbuf; unsigned long outlen;
105
+
106
+    if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
107
+        outlen = width * lines;
108
+        if(lines != s->height){
109
+            av_log(s->avctx, AV_LOG_ERROR, "This decoder won't decode ZLib-packed TIFF with %i lines per strip\n", lines);
110
+            return -1;
111
+        }
112
+        zbuf = av_malloc(outlen);
113
+        if(uncompress(zbuf, &outlen, src, size) != Z_OK){
114
+            av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
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
+    for(line = 0; line < lines; line++){
129
+        if(src - ssrc > size){
130
+            av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
131
+            return -1;
132
+        }
133
+        switch(s->compr){
134
+        case TIFF_RAW:
135
+            memcpy(dst, src, s->width * (s->bpp / 8));
136
+            src += s->width * (s->bpp / 8);
137
+            break;
138
+        case TIFF_PACKBITS:
139
+            for(pixels = 0; pixels < width;){
140
+                code = (int8_t)*src++;
141
+                if(code >= 0){
142
+                    code++;
143
+                    if(pixels + code > width){
144
+                        av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
145
+                        return -1;
146
+                    }
147
+                    memcpy(dst + pixels, src, code);
148
+                    src += code;
149
+                    pixels += code;
150
+                }else if(code != -128){ // -127..-1
151
+                    code = (-code) + 1;
152
+                    if(pixels + code > width){
153
+                        av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
154
+                        return -1;
155
+                    }
156
+                    c = *src++;
157
+                    memset(dst + pixels, c, code);
158
+                    pixels += code;
159
+                }
160
+            }
161
+            break;
162
+        }
163
+        dst += stride;
164
+    }
165
+    return 0;
166
+}
167
+
168
+
169
+static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
170
+{
171
+    int tag, type, count, off, value = 0;
172
+    uint8_t *src, *dst;
173
+    int i, j, ssize, soff, stride;
174
+
175
+    tag = tget_short(&buf, s->le);
176
+    type = tget_short(&buf, s->le);
177
+    count = tget_long(&buf, s->le);
178
+    off = tget_long(&buf, s->le);
179
+
180
+    if(count == 1){
181
+        switch(type){
182
+        case TIFF_BYTE:
183
+        case TIFF_SHORT:
184
+            buf -= 4;
185
+            value = tget(&buf, type, s->le);
186
+            buf = NULL;
187
+            break;
188
+        case TIFF_LONG:
189
+            value = off;
190
+            buf = NULL;
191
+            break;
192
+        default:
193
+            value = -1;
194
+            buf = start + off;
195
+        }
196
+    }else{
197
+        buf = start + off;
198
+    }
199
+
200
+    if(buf && (buf < start || buf > end_buf)){
201
+        av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
202
+        return -1;
203
+    }
204
+
205
+    switch(tag){
206
+    case TIFF_WIDTH:
207
+        s->width = value;
208
+        break;
209
+    case TIFF_HEIGHT:
210
+        s->height = value;
211
+        s->avctx->pix_fmt = PIX_FMT_RGB24;
212
+        if(s->width != s->avctx->width || s->height != s->avctx->height){
213
+            if(avcodec_check_dimensions(s->avctx, s->width, s->height))
214
+                return -1;
215
+            avcodec_set_dimensions(s->avctx, s->width, s->height);
216
+        }
217
+        if(s->picture.data[0])
218
+            s->avctx->release_buffer(s->avctx, &s->picture);
219
+        if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
220
+            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
221
+            return -1;
222
+        }
223
+        break;
224
+    case TIFF_BPP:
225
+        if(count == 1) s->bpp = value;
226
+        else{
227
+            switch(type){
228
+            case TIFF_BYTE:
229
+                s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF);
230
+                break;
231
+            case TIFF_SHORT:
232
+            case TIFF_LONG:
233
+                s->bpp = tget(&buf, type, s->le) + tget(&buf, type, s->le) + tget(&buf, type, s->le);
234
+                break;
235
+            default:
236
+                s->bpp = -1;
237
+            }
238
+        }
239
+        if(s->bpp != 24){
240
+            av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported\n");
241
+            return -1;
242
+        }
243
+        break;
244
+    case TIFF_COMPR:
245
+        s->compr = value;
246
+        switch(s->compr){
247
+        case TIFF_RAW:
248
+        case TIFF_PACKBITS:
249
+            break;
250
+        case TIFF_DEFLATE:
251
+        case TIFF_ADOBE_DEFLATE:
252
+#ifdef CONFIG_ZLIB
253
+            break;
254
+#else
255
+            av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
256
+            return -1;
257
+#endif
258
+        case TIFF_LZW:
259
+            av_log(s->avctx, AV_LOG_ERROR, "LZW: not implemented yet\n");
260
+            return -1;
261
+        case TIFF_G3:
262
+            av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
263
+            return -1;
264
+        case TIFF_G4:
265
+            av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
266
+            return -1;
267
+        case TIFF_CCITT_RLE:
268
+            av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
269
+            return -1;
270
+        default:
271
+            av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
272
+            return -1;
273
+        }
274
+        break;
275
+    case TIFF_ROWSPERSTRIP:
276
+        if(value < 1 || value > s->height){
277
+            av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
278
+            return -1;
279
+        }
280
+        s->rps = value;
281
+        break;
282
+    case TIFF_STRIP_OFFS:
283
+        if(count == 1){
284
+            s->stripdata = NULL;
285
+            s->stripoff = value;
286
+        }else
287
+            s->stripdata = start + off;
288
+        s->strips = count;
289
+        s->sot = type;
290
+        if(s->stripdata > end_buf){
291
+            av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
292
+            return -1;
293
+        }
294
+        break;
295
+    case TIFF_STRIP_SIZE:
296
+        if(count == 1){
297
+            s->stripsizes = NULL;
298
+            s->stripsize = value;
299
+            s->strips = 1;
300
+        }else{
301
+            s->stripsizes = start + off;
302
+        }
303
+        s->strips = count;
304
+        if(s->stripsizes > end_buf){
305
+            av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
306
+            return -1;
307
+        }
308
+        if(!pic->data[0]){
309
+            av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
310
+            return -1;
311
+        }
312
+        /* now we have the data and may start decoding */
313
+        stride = pic->linesize[0];
314
+        dst = pic->data[0];
315
+        for(i = 0; i < s->height; i += s->rps){
316
+            if(s->stripsizes)
317
+                ssize = tget(&s->stripsizes, type, s->le);
318
+            else
319
+                ssize = s->stripsize;
320
+
321
+            if(s->stripdata){
322
+                soff = tget(&s->stripdata, s->sot, s->le);
323
+            }else
324
+                soff = s->stripoff;
325
+            src = start + soff;
326
+            if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
327
+                break;
328
+            dst += s->rps * stride;
329
+        }
330
+        break;
331
+    case TIFF_PREDICTOR:
332
+        if(!pic->data[0]){
333
+            av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
334
+            return -1;
335
+        }
336
+        if(value == 2){
337
+            src = pic->data[0] + pic->linesize[0];
338
+            stride = pic->linesize[0];
339
+            soff = s->bpp >> 3;
340
+            ssize = s->width * soff;
341
+            for(i = 0; i < s->height; i++) {
342
+                for(j = soff; j < ssize; j++)
343
+                    src[j] += src[j - soff];
344
+                src += stride;
345
+            }
346
+        }
347
+        break;
348
+    }
349
+    return 0;
350
+}
351
+
352
+static int decode_frame(AVCodecContext *avctx,
353
+                        void *data, int *data_size,
354
+                        uint8_t *buf, int buf_size)
355
+{
356
+    TiffContext * const s = avctx->priv_data;
357
+    AVFrame *picture = data;
358
+    AVFrame * const p= (AVFrame*)&s->picture;
359
+    uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
360
+    int id, le, off;
361
+    int i, entries;
362
+
363
+    //parse image header
364
+    id = LE_16(buf); buf += 2;
365
+    if(id == 0x4949) le = 1;
366
+    else if(id == 0x4D4D) le = 0;
367
+    else{
368
+        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
369
+        return -1;
370
+    }
371
+    s->le = le;
372
+    // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
373
+    // that further identifies the file as a TIFF file"
374
+    if(tget_short(&buf, le) != 42){
375
+        av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
376
+        return -1;
377
+    }
378
+    /* parse image file directory */
379
+    off = tget_long(&buf, le);
380
+    if(orig_buf + off + 14 >= end_buf){
381
+        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
382
+        return -1;
383
+    }
384
+    buf = orig_buf + off;
385
+    entries = tget_short(&buf, le);
386
+    for(i = 0; i < entries; i++){
387
+        if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
388
+            return -1;
389
+        buf += 12;
390
+    }
391
+
392
+    *picture= *(AVFrame*)&s->picture;
393
+    *data_size = sizeof(AVPicture);
394
+
395
+    return buf_size;
396
+}
397
+
398
+static int tiff_init(AVCodecContext *avctx){
399
+    TiffContext *s = avctx->priv_data;
400
+
401
+    s->width = 0;
402
+    s->height = 0;
403
+    s->avctx = avctx;
404
+    avcodec_get_frame_defaults((AVFrame*)&s->picture);
405
+    avctx->coded_frame= (AVFrame*)&s->picture;
406
+    s->picture.data[0] = NULL;
407
+
408
+    return 0;
409
+}
410
+
411
+static int tiff_end(AVCodecContext *avctx)
412
+{
413
+    TiffContext * const s = avctx->priv_data;
414
+
415
+    if(s->picture.data[0])
416
+        avctx->release_buffer(avctx, &s->picture);
417
+    return 0;
418
+}
419
+
420
+AVCodec tiff_decoder = {
421
+    "tiff",
422
+    CODEC_TYPE_VIDEO,
423
+    CODEC_ID_TIFF,
424
+    sizeof(TiffContext),
425
+    tiff_init,
426
+    NULL,
427
+    tiff_end,
428
+    decode_frame,
429
+    0,
430
+    NULL
431
+};
... ...
@@ -118,6 +118,7 @@ static const CodecTag mov_video_tags[] = {
118 118
     { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, /* AVID DV */
119 119
     //{ CODEC_ID_JPEG2000, MKTAG('m', 'j', 'p', '2') }, /* JPEG 2000 produced by FCP */
120 120
     { CODEC_ID_TARGA, MKTAG('t', 'g', 'a', ' ') }, /* Truevision Targa */
121
+    { CODEC_ID_TIFF, MKTAG('t', 'i', 'f', 'f') }, /* TIFF embedded in MOV */
121 122
     { CODEC_ID_RAWVIDEO, MKTAG('2', 'v', 'u', 'y') }, /* UNCOMPRESSED 8BIT 4:2:2 */
122 123
     { CODEC_ID_NONE, 0 },
123 124
 };