Statistics
| Branch: | Revision:

ffmpeg / libavcodec / tiff.c @ 21330978

History | View | Annotate | Download (14.3 KB)

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
typedef struct TiffContext {
66
    AVCodecContext *avctx;
67
    AVFrame picture;
68

    
69
    int width, height;
70
    unsigned int bpp;
71
    int le;
72
    int compr;
73
    int invert;
74

    
75
    int strips, rps;
76
    int sot;
77
    uint8_t* stripdata;
78
    uint8_t* stripsizes;
79
    int stripsize, stripoff;
80
    LZWState *lzw;
81
} TiffContext;
82

    
83
static int tget_short(uint8_t **p, int le){
84
    int v = le ? LE_16(*p) : BE_16(*p);
85
    *p += 2;
86
    return v;
87
}
88

    
89
static int tget_long(uint8_t **p, int le){
90
    int v = le ? LE_32(*p) : BE_32(*p);
91
    *p += 4;
92
    return v;
93
}
94

    
95
static int tget(uint8_t **p, int type, int le){
96
    switch(type){
97
    case TIFF_BYTE : return *(*p)++;
98
    case TIFF_SHORT: return tget_short(p, le);
99
    case TIFF_LONG : return tget_long (p, le);
100
    default        : return -1;
101
    }
102
}
103

    
104
static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
105
    int c, line, pixels, code;
106
    uint8_t *ssrc = src;
107
    int width = s->width * (s->bpp / 8);
108
#ifdef CONFIG_ZLIB
109
    uint8_t *zbuf; unsigned long outlen;
110

    
111
    if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
112
        outlen = width * lines;
113
        zbuf = av_malloc(outlen);
114
        if(uncompress(zbuf, &outlen, src, size) != Z_OK){
115
            av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
116
            av_free(zbuf);
117
            return -1;
118
        }
119
        src = zbuf;
120
        for(line = 0; line < lines; line++){
121
            memcpy(dst, src, width);
122
            dst += stride;
123
            src += width;
124
        }
125
        av_free(zbuf);
126
        return 0;
127
    }
128
#endif
129
    if(s->compr == TIFF_LZW){
130
        if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
131
            av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
132
            return -1;
133
        }
134
    }
135
    for(line = 0; line < lines; line++){
136
        if(src - ssrc > size){
137
            av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
138
            return -1;
139
        }
140
        switch(s->compr){
141
        case TIFF_RAW:
142
            memcpy(dst, src, s->width * (s->bpp / 8));
143
            src += s->width * (s->bpp / 8);
144
            break;
145
        case TIFF_PACKBITS:
146
            for(pixels = 0; pixels < width;){
147
                code = (int8_t)*src++;
148
                if(code >= 0){
149
                    code++;
150
                    if(pixels + code > width){
151
                        av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
152
                        return -1;
153
                    }
154
                    memcpy(dst + pixels, src, code);
155
                    src += code;
156
                    pixels += code;
157
                }else if(code != -128){ // -127..-1
158
                    code = (-code) + 1;
159
                    if(pixels + code > width){
160
                        av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
161
                        return -1;
162
                    }
163
                    c = *src++;
164
                    memset(dst + pixels, c, code);
165
                    pixels += code;
166
                }
167
            }
168
            break;
169
        case TIFF_LZW:
170
            pixels = ff_lzw_decode(s->lzw, dst, width);
171
            if(pixels < width){
172
                av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
173
                return -1;
174
            }
175
            break;
176
        }
177
        dst += stride;
178
    }
179
    return 0;
180
}
181

    
182

    
183
static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
184
{
185
    int tag, type, count, off, value = 0;
186
    uint8_t *src, *dst;
187
    int i, j, ssize, soff, stride;
188
    int *pal, *rp, *gp, *bp;
189

    
190
    tag = tget_short(&buf, s->le);
191
    type = tget_short(&buf, s->le);
192
    count = tget_long(&buf, s->le);
193
    off = tget_long(&buf, s->le);
194

    
195
    if(count == 1){
196
        switch(type){
197
        case TIFF_BYTE:
198
        case TIFF_SHORT:
199
            buf -= 4;
200
            value = tget(&buf, type, s->le);
201
            buf = NULL;
202
            break;
203
        case TIFF_LONG:
204
            value = off;
205
            buf = NULL;
206
            break;
207
        default:
208
            value = -1;
209
            buf = start + off;
210
        }
211
    }else{
212
        buf = start + off;
213
    }
214

    
215
    if(buf && (buf < start || buf > end_buf)){
216
        av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
217
        return -1;
218
    }
219

    
220
    switch(tag){
221
    case TIFF_WIDTH:
222
        s->width = value;
223
        break;
224
    case TIFF_HEIGHT:
225
        s->height = value;
226
        break;
227
    case TIFF_BPP:
228
        if(count == 1) s->bpp = value;
229
        else{
230
            switch(type){
231
            case TIFF_BYTE:
232
                s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
233
                break;
234
            case TIFF_SHORT:
235
            case TIFF_LONG:
236
                s->bpp = 0;
237
                for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
238
                break;
239
            default:
240
                s->bpp = -1;
241
            }
242
        }
243
        switch(s->bpp){
244
        case 8:
245
            s->avctx->pix_fmt = PIX_FMT_PAL8;
246
            break;
247
        case 24:
248
            s->avctx->pix_fmt = PIX_FMT_RGB24;
249
            break;
250
        default:
251
            av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported (this bpp=%i)\n", s->bpp);
252
            return -1;
253
        }
254
        if(s->width != s->avctx->width || s->height != s->avctx->height){
255
            if(avcodec_check_dimensions(s->avctx, s->width, s->height))
256
                return -1;
257
            avcodec_set_dimensions(s->avctx, s->width, s->height);
258
        }
259
        if(s->picture.data[0])
260
            s->avctx->release_buffer(s->avctx, &s->picture);
261
        if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
262
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
263
            return -1;
264
        }
265
        if(s->bpp == 8){
266
            /* make default grayscale pal */
267
            pal = s->picture.data[1];
268
            for(i = 0; i < 256; i++)
269
                pal[i] = i * 0x010101;
270
        }
271
        break;
272
    case TIFF_COMPR:
273
        s->compr = value;
274
        switch(s->compr){
275
        case TIFF_RAW:
276
        case TIFF_PACKBITS:
277
        case TIFF_LZW:
278
            break;
279
        case TIFF_DEFLATE:
280
        case TIFF_ADOBE_DEFLATE:
281
#ifdef CONFIG_ZLIB
282
            break;
283
#else
284
            av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
285
            return -1;
286
#endif
287
        case TIFF_G3:
288
            av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
289
            return -1;
290
        case TIFF_G4:
291
            av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
292
            return -1;
293
        case TIFF_CCITT_RLE:
294
            av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
295
            return -1;
296
        case TIFF_JPEG:
297
        case TIFF_NEWJPEG:
298
            av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
299
            return -1;
300
        default:
301
            av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
302
            return -1;
303
        }
304
        break;
305
    case TIFF_ROWSPERSTRIP:
306
        if(value < 1){
307
            av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
308
            return -1;
309
        }
310
        s->rps = value;
311
        break;
312
    case TIFF_STRIP_OFFS:
313
        if(count == 1){
314
            s->stripdata = NULL;
315
            s->stripoff = value;
316
        }else
317
            s->stripdata = start + off;
318
        s->strips = count;
319
        s->sot = type;
320
        if(s->stripdata > end_buf){
321
            av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
322
            return -1;
323
        }
324
        break;
325
    case TIFF_STRIP_SIZE:
326
        if(count == 1){
327
            s->stripsizes = NULL;
328
            s->stripsize = value;
329
            s->strips = 1;
330
        }else{
331
            s->stripsizes = start + off;
332
        }
333
        s->strips = count;
334
        if(s->stripsizes > end_buf){
335
            av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
336
            return -1;
337
        }
338
        if(!pic->data[0]){
339
            av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
340
            return -1;
341
        }
342
        /* now we have the data and may start decoding */
343
        stride = pic->linesize[0];
344
        dst = pic->data[0];
345
        for(i = 0; i < s->height; i += s->rps){
346
            if(s->stripsizes)
347
                ssize = tget(&s->stripsizes, type, s->le);
348
            else
349
                ssize = s->stripsize;
350

    
351
            if(s->stripdata){
352
                soff = tget(&s->stripdata, s->sot, s->le);
353
            }else
354
                soff = s->stripoff;
355
            src = start + soff;
356
            if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
357
                break;
358
            dst += s->rps * stride;
359
        }
360
        break;
361
    case TIFF_PREDICTOR:
362
        if(!pic->data[0]){
363
            av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
364
            return -1;
365
        }
366
        if(value == 2){
367
            src = pic->data[0];
368
            stride = pic->linesize[0];
369
            soff = s->bpp >> 3;
370
            ssize = s->width * soff;
371
            for(i = 0; i < s->height; i++) {
372
                for(j = soff; j < ssize; j++)
373
                    src[j] += src[j - soff];
374
                src += stride;
375
            }
376
        }
377
        break;
378
    case TIFF_INVERT:
379
        switch(value){
380
        case 0:
381
            s->invert = 1;
382
            break;
383
        case 1:
384
            s->invert = 0;
385
            break;
386
        case 2:
387
        case 3:
388
            break;
389
        default:
390
            av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
391
            return -1;
392
        }
393
        break;
394
    case TIFF_PAL:
395
        if(s->avctx->pix_fmt != PIX_FMT_PAL8){
396
            av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
397
            return -1;
398
        }
399
        pal = s->picture.data[1];
400
        off = (type == TIFF_SHORT) ? 2 : 1;
401
        rp = buf;
402
        gp = buf + count / 3 * off;
403
        bp = buf + count / 3 * off * 2;
404
        off = (type == TIFF_SHORT) ? 8 : 0;
405
        for(i = 0; i < count / 3; i++){
406
            j = (tget(&rp, type, s->le) >> off) << 16;
407
            j |= (tget(&gp, type, s->le) >> off) << 8;
408
            j |= tget(&bp, type, s->le) >> off;
409
            pal[i] = j;
410
        }
411
    }
412
    return 0;
413
}
414

    
415
static int decode_frame(AVCodecContext *avctx,
416
                        void *data, int *data_size,
417
                        uint8_t *buf, int buf_size)
418
{
419
    TiffContext * const s = avctx->priv_data;
420
    AVFrame *picture = data;
421
    AVFrame * const p= (AVFrame*)&s->picture;
422
    uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
423
    int id, le, off;
424
    int i, entries;
425

    
426
    //parse image header
427
    id = LE_16(buf); buf += 2;
428
    if(id == 0x4949) le = 1;
429
    else if(id == 0x4D4D) le = 0;
430
    else{
431
        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
432
        return -1;
433
    }
434
    s->le = le;
435
    s->invert = 0;
436
    // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
437
    // that further identifies the file as a TIFF file"
438
    if(tget_short(&buf, le) != 42){
439
        av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
440
        return -1;
441
    }
442
    /* parse image file directory */
443
    off = tget_long(&buf, le);
444
    if(orig_buf + off + 14 >= end_buf){
445
        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
446
        return -1;
447
    }
448
    buf = orig_buf + off;
449
    entries = tget_short(&buf, le);
450
    for(i = 0; i < entries; i++){
451
        if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
452
            return -1;
453
        buf += 12;
454
    }
455

    
456
    if(s->invert){
457
        uint8_t *src;
458
        int j;
459

    
460
        src = s->picture.data[0];
461
        for(j = 0; j < s->height; j++){
462
            for(i = 0; i < s->picture.linesize[0]; i++)
463
                src[i] = 255 - src[i];
464
            src += s->picture.linesize[0];
465
        }
466
    }
467
    *picture= *(AVFrame*)&s->picture;
468
    *data_size = sizeof(AVPicture);
469

    
470
    return buf_size;
471
}
472

    
473
static int tiff_init(AVCodecContext *avctx){
474
    TiffContext *s = avctx->priv_data;
475

    
476
    s->width = 0;
477
    s->height = 0;
478
    s->avctx = avctx;
479
    avcodec_get_frame_defaults((AVFrame*)&s->picture);
480
    avctx->coded_frame= (AVFrame*)&s->picture;
481
    s->picture.data[0] = NULL;
482
    ff_lzw_decode_open(&s->lzw);
483

    
484
    return 0;
485
}
486

    
487
static int tiff_end(AVCodecContext *avctx)
488
{
489
    TiffContext * const s = avctx->priv_data;
490

    
491
    ff_lzw_decode_close(&s->lzw);
492
    if(s->picture.data[0])
493
        avctx->release_buffer(avctx, &s->picture);
494
    return 0;
495
}
496

    
497
AVCodec tiff_decoder = {
498
    "tiff",
499
    CODEC_TYPE_VIDEO,
500
    CODEC_ID_TIFF,
501
    sizeof(TiffContext),
502
    tiff_init,
503
    NULL,
504
    tiff_end,
505
    decode_frame,
506
    0,
507
    NULL
508
};