Statistics
| Branch: | Revision:

ffmpeg / libavcodec / tiff.c @ a4db272a

History | View | Annotate | Download (17.9 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
/**
23
 * TIFF image decoder
24
 * @file
25
 * @author Konstantin Shishkov
26
 */
27
#include "avcodec.h"
28
#if CONFIG_ZLIB
29
#include <zlib.h>
30
#endif
31
#include "lzw.h"
32
#include "tiff.h"
33
#include "faxcompr.h"
34
#include "libavutil/common.h"
35
#include "libavutil/intreadwrite.h"
36
#include "libavcore/imgutils.h"
37

    
38
typedef struct TiffContext {
39
    AVCodecContext *avctx;
40
    AVFrame picture;
41

    
42
    int width, height;
43
    unsigned int bpp;
44
    int le;
45
    int compr;
46
    int invert;
47
    int fax_opts;
48
    int predictor;
49
    int fill_order;
50

    
51
    int strips, rps, sstype;
52
    int sot;
53
    const uint8_t* stripdata;
54
    const uint8_t* stripsizes;
55
    int stripsize, stripoff;
56
    LZWState *lzw;
57
} TiffContext;
58

    
59
static int tget_short(const uint8_t **p, int le){
60
    int v = le ? AV_RL16(*p) : AV_RB16(*p);
61
    *p += 2;
62
    return v;
63
}
64

    
65
static int tget_long(const uint8_t **p, int le){
66
    int v = le ? AV_RL32(*p) : AV_RB32(*p);
67
    *p += 4;
68
    return v;
69
}
70

    
71
static int tget(const uint8_t **p, int type, int le){
72
    switch(type){
73
    case TIFF_BYTE : return *(*p)++;
74
    case TIFF_SHORT: return tget_short(p, le);
75
    case TIFF_LONG : return tget_long (p, le);
76
    default        : return -1;
77
    }
78
}
79

    
80
#if CONFIG_ZLIB
81
static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
82
{
83
    z_stream zstream;
84
    int zret;
85

    
86
    memset(&zstream, 0, sizeof(zstream));
87
    zstream.next_in = src;
88
    zstream.avail_in = size;
89
    zstream.next_out = dst;
90
    zstream.avail_out = *len;
91
    zret = inflateInit(&zstream);
92
    if (zret != Z_OK) {
93
        av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
94
        return zret;
95
    }
96
    zret = inflate(&zstream, Z_SYNC_FLUSH);
97
    inflateEnd(&zstream);
98
    *len = zstream.total_out;
99
    return zret == Z_STREAM_END ? Z_OK : zret;
100
}
101
#endif
102

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

    
110
    if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
111
        int ret;
112
        outlen = width * lines;
113
        zbuf = av_malloc(outlen);
114
        ret = tiff_uncompress(zbuf, &outlen, src, size);
115
        if(ret != Z_OK){
116
            av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
117
            av_free(zbuf);
118
            return -1;
119
        }
120
        src = zbuf;
121
        for(line = 0; line < lines; line++){
122
            memcpy(dst, src, width);
123
            dst += stride;
124
            src += width;
125
        }
126
        av_free(zbuf);
127
        return 0;
128
    }
129
#endif
130
    if(s->compr == TIFF_LZW){
131
        if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
132
            av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
133
            return -1;
134
        }
135
    }
136
    if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
137
        int i, ret = 0;
138
        uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
139

    
140
        if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
141
            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
142
            return -1;
143
        }
144
        if(s->fax_opts & 2){
145
            av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
146
            av_free(src2);
147
            return -1;
148
        }
149
        if(!s->fill_order){
150
            memcpy(src2, src, size);
151
        }else{
152
            for(i = 0; i < size; i++)
153
                src2[i] = av_reverse[src[i]];
154
        }
155
        memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
156
        switch(s->compr){
157
        case TIFF_CCITT_RLE:
158
        case TIFF_G3:
159
        case TIFF_G4:
160
            ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
161
            break;
162
        }
163
        av_free(src2);
164
        return ret;
165
    }
166
    for(line = 0; line < lines; line++){
167
        if(src - ssrc > size){
168
            av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
169
            return -1;
170
        }
171
        switch(s->compr){
172
        case TIFF_RAW:
173
            memcpy(dst, src, width);
174
            src += width;
175
            break;
176
        case TIFF_PACKBITS:
177
            for(pixels = 0; pixels < width;){
178
                code = (int8_t)*src++;
179
                if(code >= 0){
180
                    code++;
181
                    if(pixels + code > width){
182
                        av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
183
                        return -1;
184
                    }
185
                    memcpy(dst + pixels, src, code);
186
                    src += code;
187
                    pixels += code;
188
                }else if(code != -128){ // -127..-1
189
                    code = (-code) + 1;
190
                    if(pixels + code > width){
191
                        av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
192
                        return -1;
193
                    }
194
                    c = *src++;
195
                    memset(dst + pixels, c, code);
196
                    pixels += code;
197
                }
198
            }
199
            break;
200
        case TIFF_LZW:
201
            pixels = ff_lzw_decode(s->lzw, dst, width);
202
            if(pixels < width){
203
                av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
204
                return -1;
205
            }
206
            break;
207
        }
208
        dst += stride;
209
    }
210
    return 0;
211
}
212

    
213

    
214
static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
215
{
216
    int tag, type, count, off, value = 0;
217
    int i, j;
218
    uint32_t *pal;
219
    const uint8_t *rp, *gp, *bp;
220

    
221
    tag = tget_short(&buf, s->le);
222
    type = tget_short(&buf, s->le);
223
    count = tget_long(&buf, s->le);
224
    off = tget_long(&buf, s->le);
225

    
226
    if(count == 1){
227
        switch(type){
228
        case TIFF_BYTE:
229
        case TIFF_SHORT:
230
            buf -= 4;
231
            value = tget(&buf, type, s->le);
232
            buf = NULL;
233
            break;
234
        case TIFF_LONG:
235
            value = off;
236
            buf = NULL;
237
            break;
238
        case TIFF_STRING:
239
            if(count <= 4){
240
                buf -= 4;
241
                break;
242
            }
243
        default:
244
            value = -1;
245
            buf = start + off;
246
        }
247
    }else if(type_sizes[type] * count <= 4){
248
        buf -= 4;
249
    }else{
250
        buf = start + off;
251
    }
252

    
253
    if(buf && (buf < start || buf > end_buf)){
254
        av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
255
        return -1;
256
    }
257

    
258
    switch(tag){
259
    case TIFF_WIDTH:
260
        s->width = value;
261
        break;
262
    case TIFF_HEIGHT:
263
        s->height = value;
264
        break;
265
    case TIFF_BPP:
266
        if(count > 4){
267
            av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
268
            return -1;
269
        }
270
        if(count == 1) s->bpp = value;
271
        else{
272
            switch(type){
273
            case TIFF_BYTE:
274
                s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
275
                break;
276
            case TIFF_SHORT:
277
            case TIFF_LONG:
278
                s->bpp = 0;
279
                for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
280
                break;
281
            default:
282
                s->bpp = -1;
283
            }
284
        }
285
        switch(s->bpp*10 + count){
286
        case 11:
287
            s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
288
            break;
289
        case 81:
290
            s->avctx->pix_fmt = PIX_FMT_PAL8;
291
            break;
292
        case 243:
293
            s->avctx->pix_fmt = PIX_FMT_RGB24;
294
            break;
295
        case 161:
296
            s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
297
            break;
298
        case 324:
299
            s->avctx->pix_fmt = PIX_FMT_RGBA;
300
            break;
301
        case 483:
302
            s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
303
            break;
304
        default:
305
            av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
306
            return -1;
307
        }
308
        if(s->width != s->avctx->width || s->height != s->avctx->height){
309
            if(av_image_check_size(s->width, s->height, 0, s->avctx))
310
                return -1;
311
            avcodec_set_dimensions(s->avctx, s->width, s->height);
312
        }
313
        if(s->picture.data[0])
314
            s->avctx->release_buffer(s->avctx, &s->picture);
315
        if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
316
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
317
            return -1;
318
        }
319
        if(s->bpp == 8){
320
            /* make default grayscale pal */
321
            pal = (uint32_t *) s->picture.data[1];
322
            for(i = 0; i < 256; i++)
323
                pal[i] = i * 0x010101;
324
        }
325
        break;
326
    case TIFF_COMPR:
327
        s->compr = value;
328
        s->predictor = 0;
329
        switch(s->compr){
330
        case TIFF_RAW:
331
        case TIFF_PACKBITS:
332
        case TIFF_LZW:
333
        case TIFF_CCITT_RLE:
334
            break;
335
        case TIFF_G3:
336
        case TIFF_G4:
337
            s->fax_opts = 0;
338
            break;
339
        case TIFF_DEFLATE:
340
        case TIFF_ADOBE_DEFLATE:
341
#if CONFIG_ZLIB
342
            break;
343
#else
344
            av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
345
            return -1;
346
#endif
347
        case TIFF_JPEG:
348
        case TIFF_NEWJPEG:
349
            av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
350
            return -1;
351
        default:
352
            av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
353
            return -1;
354
        }
355
        break;
356
    case TIFF_ROWSPERSTRIP:
357
        if(type == TIFF_LONG && value == -1)
358
            value = s->avctx->height;
359
        if(value < 1){
360
            av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
361
            return -1;
362
        }
363
        s->rps = value;
364
        break;
365
    case TIFF_STRIP_OFFS:
366
        if(count == 1){
367
            s->stripdata = NULL;
368
            s->stripoff = value;
369
        }else
370
            s->stripdata = start + off;
371
        s->strips = count;
372
        if(s->strips == 1) s->rps = s->height;
373
        s->sot = type;
374
        if(s->stripdata > end_buf){
375
            av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
376
            return -1;
377
        }
378
        break;
379
    case TIFF_STRIP_SIZE:
380
        if(count == 1){
381
            s->stripsizes = NULL;
382
            s->stripsize = value;
383
            s->strips = 1;
384
        }else{
385
            s->stripsizes = start + off;
386
        }
387
        s->strips = count;
388
        s->sstype = type;
389
        if(s->stripsizes > end_buf){
390
            av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
391
            return -1;
392
        }
393
        break;
394
    case TIFF_PREDICTOR:
395
        s->predictor = value;
396
        break;
397
    case TIFF_INVERT:
398
        switch(value){
399
        case 0:
400
            s->invert = 1;
401
            break;
402
        case 1:
403
            s->invert = 0;
404
            break;
405
        case 2:
406
        case 3:
407
            break;
408
        default:
409
            av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
410
            return -1;
411
        }
412
        break;
413
    case TIFF_FILL_ORDER:
414
        if(value < 1 || value > 2){
415
            av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
416
            value = 1;
417
        }
418
        s->fill_order = value - 1;
419
        break;
420
    case TIFF_PAL:
421
        if(s->avctx->pix_fmt != PIX_FMT_PAL8){
422
            av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
423
            return -1;
424
        }
425
        pal = (uint32_t *) s->picture.data[1];
426
        off = type_sizes[type];
427
        rp = buf;
428
        gp = buf + count / 3 * off;
429
        bp = buf + count / 3 * off * 2;
430
        off = (type_sizes[type] - 1) << 3;
431
        for(i = 0; i < count / 3; i++){
432
            j = (tget(&rp, type, s->le) >> off) << 16;
433
            j |= (tget(&gp, type, s->le) >> off) << 8;
434
            j |= tget(&bp, type, s->le) >> off;
435
            pal[i] = j;
436
        }
437
        break;
438
    case TIFF_PLANAR:
439
        if(value == 2){
440
            av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
441
            return -1;
442
        }
443
        break;
444
    case TIFF_T4OPTIONS:
445
        if(s->compr == TIFF_G3)
446
            s->fax_opts = value;
447
        break;
448
    case TIFF_T6OPTIONS:
449
        if(s->compr == TIFF_G4)
450
            s->fax_opts = value;
451
        break;
452
    }
453
    return 0;
454
}
455

    
456
static int decode_frame(AVCodecContext *avctx,
457
                        void *data, int *data_size,
458
                        AVPacket *avpkt)
459
{
460
    const uint8_t *buf = avpkt->data;
461
    int buf_size = avpkt->size;
462
    TiffContext * const s = avctx->priv_data;
463
    AVFrame *picture = data;
464
    AVFrame * const p= (AVFrame*)&s->picture;
465
    const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
466
    int id, le, off;
467
    int i, j, entries;
468
    int stride, soff, ssize;
469
    uint8_t *dst;
470

    
471
    //parse image header
472
    id = AV_RL16(buf); buf += 2;
473
    if(id == 0x4949) le = 1;
474
    else if(id == 0x4D4D) le = 0;
475
    else{
476
        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
477
        return -1;
478
    }
479
    s->le = le;
480
    s->invert = 0;
481
    s->compr = TIFF_RAW;
482
    s->fill_order = 0;
483
    // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
484
    // that further identifies the file as a TIFF file"
485
    if(tget_short(&buf, le) != 42){
486
        av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
487
        return -1;
488
    }
489
    /* parse image file directory */
490
    off = tget_long(&buf, le);
491
    if(orig_buf + off + 14 >= end_buf){
492
        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
493
        return -1;
494
    }
495
    buf = orig_buf + off;
496
    entries = tget_short(&buf, le);
497
    for(i = 0; i < entries; i++){
498
        if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
499
            return -1;
500
        buf += 12;
501
    }
502
    if(!s->stripdata && !s->stripoff){
503
        av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
504
        return -1;
505
    }
506
    /* now we have the data and may start decoding */
507
    if(!p->data[0]){
508
        s->bpp = 1;
509
        avctx->pix_fmt = PIX_FMT_MONOBLACK;
510
        if(s->width != s->avctx->width || s->height != s->avctx->height){
511
            if(av_image_check_size(s->width, s->height, 0, s->avctx))
512
                return -1;
513
            avcodec_set_dimensions(s->avctx, s->width, s->height);
514
        }
515
        if(s->picture.data[0])
516
            s->avctx->release_buffer(s->avctx, &s->picture);
517
        if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
518
            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
519
            return -1;
520
        }
521
    }
522
    if(s->strips == 1 && !s->stripsize){
523
        av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
524
        s->stripsize = buf_size - s->stripoff;
525
    }
526
    stride = p->linesize[0];
527
    dst = p->data[0];
528
    for(i = 0; i < s->height; i += s->rps){
529
        if(s->stripsizes)
530
            ssize = tget(&s->stripsizes, s->sstype, s->le);
531
        else
532
            ssize = s->stripsize;
533

    
534
        if (ssize > buf_size) {
535
            av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
536
            return -1;
537
        }
538

    
539
        if(s->stripdata){
540
            soff = tget(&s->stripdata, s->sot, s->le);
541
        }else
542
            soff = s->stripoff;
543
        if (soff < 0) {
544
            av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
545
            return AVERROR(EINVAL);
546
        }
547
        if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
548
            break;
549
        dst += s->rps * stride;
550
    }
551
    if(s->predictor == 2){
552
        dst = p->data[0];
553
        soff = s->bpp >> 3;
554
        ssize = s->width * soff;
555
        for(i = 0; i < s->height; i++) {
556
            for(j = soff; j < ssize; j++)
557
                dst[j] += dst[j - soff];
558
            dst += stride;
559
        }
560
    }
561

    
562
    if(s->invert){
563
        uint8_t *src;
564
        int j;
565

    
566
        src = s->picture.data[0];
567
        for(j = 0; j < s->height; j++){
568
            for(i = 0; i < s->picture.linesize[0]; i++)
569
                src[i] = 255 - src[i];
570
            src += s->picture.linesize[0];
571
        }
572
    }
573
    *picture= *(AVFrame*)&s->picture;
574
    *data_size = sizeof(AVPicture);
575

    
576
    return buf_size;
577
}
578

    
579
static av_cold int tiff_init(AVCodecContext *avctx){
580
    TiffContext *s = avctx->priv_data;
581

    
582
    s->width = 0;
583
    s->height = 0;
584
    s->avctx = avctx;
585
    avcodec_get_frame_defaults((AVFrame*)&s->picture);
586
    avctx->coded_frame= (AVFrame*)&s->picture;
587
    ff_lzw_decode_open(&s->lzw);
588
    ff_ccitt_unpack_init();
589

    
590
    return 0;
591
}
592

    
593
static av_cold int tiff_end(AVCodecContext *avctx)
594
{
595
    TiffContext * const s = avctx->priv_data;
596

    
597
    ff_lzw_decode_close(&s->lzw);
598
    if(s->picture.data[0])
599
        avctx->release_buffer(avctx, &s->picture);
600
    return 0;
601
}
602

    
603
AVCodec tiff_decoder = {
604
    "tiff",
605
    AVMEDIA_TYPE_VIDEO,
606
    CODEC_ID_TIFF,
607
    sizeof(TiffContext),
608
    tiff_init,
609
    NULL,
610
    tiff_end,
611
    decode_frame,
612
    CODEC_CAP_DR1,
613
    NULL,
614
    .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
615
};