]> git.sesse.net Git - ffmpeg/blob - libavcodec/tiff.c
lavc: remove disabled FF_API_ALLOC_CONTEXT cruft.
[ffmpeg] / libavcodec / tiff.c
1 /*
2  * TIFF image decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * TIFF image decoder
25  * @author Konstantin Shishkov
26  */
27
28 #include "avcodec.h"
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif
32 #include "lzw.h"
33 #include "tiff.h"
34 #include "faxcompr.h"
35 #include "libavutil/common.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/imgutils.h"
38
39 typedef struct TiffContext {
40     AVCodecContext *avctx;
41     AVFrame picture;
42
43     int width, height;
44     unsigned int bpp, bppcount;
45     uint32_t palette[256];
46     int palette_is_set;
47     int le;
48     enum TiffCompr compr;
49     int invert;
50     int fax_opts;
51     int predictor;
52     int fill_order;
53
54     int strips, rps, sstype;
55     int sot;
56     const uint8_t* stripdata;
57     const uint8_t* stripsizes;
58     int stripsize, stripoff;
59     LZWState *lzw;
60 } TiffContext;
61
62 static int tget_short(const uint8_t **p, int le){
63     int v = le ? AV_RL16(*p) : AV_RB16(*p);
64     *p += 2;
65     return v;
66 }
67
68 static int tget_long(const uint8_t **p, int le){
69     int v = le ? AV_RL32(*p) : AV_RB32(*p);
70     *p += 4;
71     return v;
72 }
73
74 static int tget(const uint8_t **p, int type, int le){
75     switch(type){
76     case TIFF_BYTE : return *(*p)++;
77     case TIFF_SHORT: return tget_short(p, le);
78     case TIFF_LONG : return tget_long (p, le);
79     default        : return -1;
80     }
81 }
82
83 #if CONFIG_ZLIB
84 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
85 {
86     z_stream zstream;
87     int zret;
88
89     memset(&zstream, 0, sizeof(zstream));
90     zstream.next_in = src;
91     zstream.avail_in = size;
92     zstream.next_out = dst;
93     zstream.avail_out = *len;
94     zret = inflateInit(&zstream);
95     if (zret != Z_OK) {
96         av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
97         return zret;
98     }
99     zret = inflate(&zstream, Z_SYNC_FLUSH);
100     inflateEnd(&zstream);
101     *len = zstream.total_out;
102     return zret == Z_STREAM_END ? Z_OK : zret;
103 }
104 #endif
105
106 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
107     int c, line, pixels, code;
108     const uint8_t *ssrc = src;
109     int width = ((s->width * s->bpp) + 7) >> 3;
110 #if CONFIG_ZLIB
111     uint8_t *zbuf; unsigned long outlen;
112
113     if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
114         int ret;
115         outlen = width * lines;
116         zbuf = av_malloc(outlen);
117         ret = tiff_uncompress(zbuf, &outlen, src, size);
118         if(ret != Z_OK){
119             av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
120             av_free(zbuf);
121             return -1;
122         }
123         src = zbuf;
124         for(line = 0; line < lines; line++){
125             memcpy(dst, src, width);
126             dst += stride;
127             src += width;
128         }
129         av_free(zbuf);
130         return 0;
131     }
132 #endif
133     if(s->compr == TIFF_LZW){
134         if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
135             av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
136             return -1;
137         }
138     }
139     if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
140         int i, ret = 0;
141         uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
142
143         if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
144             av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
145             return -1;
146         }
147         if(s->fax_opts & 2){
148             av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
149             av_free(src2);
150             return -1;
151         }
152         if(!s->fill_order){
153             memcpy(src2, src, size);
154         }else{
155             for(i = 0; i < size; i++)
156                 src2[i] = av_reverse[src[i]];
157         }
158         memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
159         switch(s->compr){
160         case TIFF_CCITT_RLE:
161         case TIFF_G3:
162         case TIFF_G4:
163             ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
164             break;
165         }
166         av_free(src2);
167         return ret;
168     }
169     for(line = 0; line < lines; line++){
170         if(src - ssrc > size){
171             av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
172             return -1;
173         }
174         switch(s->compr){
175         case TIFF_RAW:
176             if (ssrc + size - src < width)
177                 return AVERROR_INVALIDDATA;
178             if (!s->fill_order) {
179                 memcpy(dst, src, width);
180             } else {
181                 int i;
182                 for (i = 0; i < width; i++)
183                     dst[i] = av_reverse[src[i]];
184             }
185             src += width;
186             break;
187         case TIFF_PACKBITS:
188             for(pixels = 0; pixels < width;){
189                 code = (int8_t)*src++;
190                 if(code >= 0){
191                     code++;
192                     if(pixels + code > width){
193                         av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
194                         return -1;
195                     }
196                     memcpy(dst + pixels, src, code);
197                     src += code;
198                     pixels += code;
199                 }else if(code != -128){ // -127..-1
200                     code = (-code) + 1;
201                     if(pixels + code > width){
202                         av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
203                         return -1;
204                     }
205                     c = *src++;
206                     memset(dst + pixels, c, code);
207                     pixels += code;
208                 }
209             }
210             break;
211         case TIFF_LZW:
212             pixels = ff_lzw_decode(s->lzw, dst, width);
213             if(pixels < width){
214                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
215                 return -1;
216             }
217             break;
218         }
219         dst += stride;
220     }
221     return 0;
222 }
223
224 static int init_image(TiffContext *s)
225 {
226     int i, ret;
227     uint32_t *pal;
228
229     switch (s->bpp * 10 + s->bppcount) {
230     case 11:
231         s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
232         break;
233     case 81:
234         s->avctx->pix_fmt = PIX_FMT_PAL8;
235         break;
236     case 243:
237         s->avctx->pix_fmt = PIX_FMT_RGB24;
238         break;
239     case 161:
240         s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
241         break;
242     case 324:
243         s->avctx->pix_fmt = PIX_FMT_RGBA;
244         break;
245     case 483:
246         s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
247         break;
248     default:
249         av_log(s->avctx, AV_LOG_ERROR,
250                "This format is not supported (bpp=%d, bppcount=%d)\n",
251                s->bpp, s->bppcount);
252         return AVERROR_INVALIDDATA;
253     }
254     if (s->width != s->avctx->width || s->height != s->avctx->height) {
255         if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
256             return ret;
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 ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
262         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
263         return ret;
264     }
265     if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
266         if (s->palette_is_set) {
267             memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
268         } else {
269             /* make default grayscale pal */
270             pal = (uint32_t *) s->picture.data[1];
271             for (i = 0; i < 256; i++)
272                 pal[i] = i * 0x010101;
273         }
274     }
275     return 0;
276 }
277
278 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
279 {
280     int tag, type, count, off, value = 0;
281     int i, j;
282     uint32_t *pal;
283     const uint8_t *rp, *gp, *bp;
284
285     if (end_buf - buf < 12)
286         return -1;
287     tag = tget_short(&buf, s->le);
288     type = tget_short(&buf, s->le);
289     count = tget_long(&buf, s->le);
290     off = tget_long(&buf, s->le);
291
292     if(count == 1){
293         switch(type){
294         case TIFF_BYTE:
295         case TIFF_SHORT:
296             buf -= 4;
297             value = tget(&buf, type, s->le);
298             buf = NULL;
299             break;
300         case TIFF_LONG:
301             value = off;
302             buf = NULL;
303             break;
304         case TIFF_STRING:
305             if(count <= 4){
306                 buf -= 4;
307                 break;
308             }
309         default:
310             value = -1;
311             buf = start + off;
312         }
313     }else if(type_sizes[type] * count <= 4){
314         buf -= 4;
315     }else{
316         buf = start + off;
317     }
318
319     if(buf && (buf < start || buf > end_buf)){
320         av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
321         return -1;
322     }
323
324     switch(tag){
325     case TIFF_WIDTH:
326         s->width = value;
327         break;
328     case TIFF_HEIGHT:
329         s->height = value;
330         break;
331     case TIFF_BPP:
332         s->bppcount = count;
333         if(count > 4){
334             av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
335             return -1;
336         }
337         if(count == 1) s->bpp = value;
338         else{
339             switch(type){
340             case TIFF_BYTE:
341                 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
342                 break;
343             case TIFF_SHORT:
344             case TIFF_LONG:
345                 s->bpp = 0;
346                 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
347                 break;
348             default:
349                 s->bpp = -1;
350             }
351         }
352         break;
353     case TIFF_SAMPLES_PER_PIXEL:
354         if (count != 1) {
355             av_log(s->avctx, AV_LOG_ERROR,
356                    "Samples per pixel requires a single value, many provided\n");
357             return AVERROR_INVALIDDATA;
358         }
359         if (s->bppcount == 1)
360             s->bpp *= value;
361         s->bppcount = value;
362         break;
363     case TIFF_COMPR:
364         s->compr = value;
365         s->predictor = 0;
366         switch(s->compr){
367         case TIFF_RAW:
368         case TIFF_PACKBITS:
369         case TIFF_LZW:
370         case TIFF_CCITT_RLE:
371             break;
372         case TIFF_G3:
373         case TIFF_G4:
374             s->fax_opts = 0;
375             break;
376         case TIFF_DEFLATE:
377         case TIFF_ADOBE_DEFLATE:
378 #if CONFIG_ZLIB
379             break;
380 #else
381             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
382             return -1;
383 #endif
384         case TIFF_JPEG:
385         case TIFF_NEWJPEG:
386             av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
387             return -1;
388         default:
389             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
390             return -1;
391         }
392         break;
393     case TIFF_ROWSPERSTRIP:
394         if(type == TIFF_LONG && value == -1)
395             value = s->avctx->height;
396         if(value < 1){
397             av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
398             return -1;
399         }
400         s->rps = value;
401         break;
402     case TIFF_STRIP_OFFS:
403         if(count == 1){
404             s->stripdata = NULL;
405             s->stripoff = value;
406         }else
407             s->stripdata = start + off;
408         s->strips = count;
409         if(s->strips == 1) s->rps = s->height;
410         s->sot = type;
411         if(s->stripdata > end_buf){
412             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
413             return -1;
414         }
415         break;
416     case TIFF_STRIP_SIZE:
417         if(count == 1){
418             s->stripsizes = NULL;
419             s->stripsize = value;
420             s->strips = 1;
421         }else{
422             s->stripsizes = start + off;
423         }
424         s->strips = count;
425         s->sstype = type;
426         if(s->stripsizes > end_buf){
427             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
428             return -1;
429         }
430         break;
431     case TIFF_PREDICTOR:
432         s->predictor = value;
433         break;
434     case TIFF_INVERT:
435         switch(value){
436         case 0:
437             s->invert = 1;
438             break;
439         case 1:
440             s->invert = 0;
441             break;
442         case 2:
443         case 3:
444             break;
445         default:
446             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
447             return -1;
448         }
449         break;
450     case TIFF_FILL_ORDER:
451         if(value < 1 || value > 2){
452             av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
453             value = 1;
454         }
455         s->fill_order = value - 1;
456         break;
457     case TIFF_PAL:
458         pal = (uint32_t *) s->palette;
459         off = type_sizes[type];
460         if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
461             return -1;
462         rp = buf;
463         gp = buf + count / 3 * off;
464         bp = buf + count / 3 * off * 2;
465         off = (type_sizes[type] - 1) << 3;
466         for(i = 0; i < count / 3; i++){
467             j = (tget(&rp, type, s->le) >> off) << 16;
468             j |= (tget(&gp, type, s->le) >> off) << 8;
469             j |= tget(&bp, type, s->le) >> off;
470             pal[i] = j;
471         }
472         s->palette_is_set = 1;
473         break;
474     case TIFF_PLANAR:
475         if(value == 2){
476             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
477             return -1;
478         }
479         break;
480     case TIFF_T4OPTIONS:
481         if(s->compr == TIFF_G3)
482             s->fax_opts = value;
483         break;
484     case TIFF_T6OPTIONS:
485         if(s->compr == TIFF_G4)
486             s->fax_opts = value;
487         break;
488     default:
489         av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
490     }
491     return 0;
492 }
493
494 static int decode_frame(AVCodecContext *avctx,
495                         void *data, int *data_size,
496                         AVPacket *avpkt)
497 {
498     const uint8_t *buf = avpkt->data;
499     int buf_size = avpkt->size;
500     TiffContext * const s = avctx->priv_data;
501     AVFrame *picture = data;
502     AVFrame * const p= (AVFrame*)&s->picture;
503     const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
504     unsigned off;
505     int id, le, ret;
506     int i, j, entries;
507     int stride;
508     unsigned soff, ssize;
509     uint8_t *dst;
510
511     //parse image header
512     if (end_buf - buf < 8)
513         return AVERROR_INVALIDDATA;
514     id = AV_RL16(buf); buf += 2;
515     if(id == 0x4949) le = 1;
516     else if(id == 0x4D4D) le = 0;
517     else{
518         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
519         return -1;
520     }
521     s->le = le;
522     s->invert = 0;
523     s->compr = TIFF_RAW;
524     s->fill_order = 0;
525     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
526     // that further identifies the file as a TIFF file"
527     if(tget_short(&buf, le) != 42){
528         av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
529         return -1;
530     }
531     /* parse image file directory */
532     off = tget_long(&buf, le);
533     if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
534         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
535         return AVERROR_INVALIDDATA;
536     }
537     buf = orig_buf + off;
538     entries = tget_short(&buf, le);
539     for(i = 0; i < entries; i++){
540         if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
541             return -1;
542         buf += 12;
543     }
544     if(!s->stripdata && !s->stripoff){
545         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
546         return -1;
547     }
548     /* now we have the data and may start decoding */
549     if ((ret = init_image(s)) < 0)
550         return ret;
551
552     if(s->strips == 1 && !s->stripsize){
553         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
554         s->stripsize = buf_size - s->stripoff;
555     }
556     stride = p->linesize[0];
557     dst = p->data[0];
558     for(i = 0; i < s->height; i += s->rps){
559         if(s->stripsizes) {
560             if (s->stripsizes >= end_buf)
561                 return AVERROR_INVALIDDATA;
562             ssize = tget(&s->stripsizes, s->sstype, s->le);
563         } else
564             ssize = s->stripsize;
565
566         if(s->stripdata){
567             if (s->stripdata >= end_buf)
568                 return AVERROR_INVALIDDATA;
569             soff = tget(&s->stripdata, s->sot, s->le);
570         }else
571             soff = s->stripoff;
572
573         if (soff > buf_size || ssize > buf_size - soff) {
574             av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
575             return -1;
576         }
577         if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
578             break;
579         dst += s->rps * stride;
580     }
581     if(s->predictor == 2){
582         dst = p->data[0];
583         soff = s->bpp >> 3;
584         ssize = s->width * soff;
585         for(i = 0; i < s->height; i++) {
586             for(j = soff; j < ssize; j++)
587                 dst[j] += dst[j - soff];
588             dst += stride;
589         }
590     }
591
592     if(s->invert){
593         uint8_t *src;
594         int j;
595
596         src = s->picture.data[0];
597         for(j = 0; j < s->height; j++){
598             for(i = 0; i < s->picture.linesize[0]; i++)
599                 src[i] = 255 - src[i];
600             src += s->picture.linesize[0];
601         }
602     }
603     *picture= *(AVFrame*)&s->picture;
604     *data_size = sizeof(AVPicture);
605
606     return buf_size;
607 }
608
609 static av_cold int tiff_init(AVCodecContext *avctx){
610     TiffContext *s = avctx->priv_data;
611
612     s->width = 0;
613     s->height = 0;
614     s->avctx = avctx;
615     avcodec_get_frame_defaults((AVFrame*)&s->picture);
616     avctx->coded_frame= (AVFrame*)&s->picture;
617     ff_lzw_decode_open(&s->lzw);
618     ff_ccitt_unpack_init();
619
620     return 0;
621 }
622
623 static av_cold int tiff_end(AVCodecContext *avctx)
624 {
625     TiffContext * const s = avctx->priv_data;
626
627     ff_lzw_decode_close(&s->lzw);
628     if(s->picture.data[0])
629         avctx->release_buffer(avctx, &s->picture);
630     return 0;
631 }
632
633 AVCodec ff_tiff_decoder = {
634     .name           = "tiff",
635     .type           = AVMEDIA_TYPE_VIDEO,
636     .id             = CODEC_ID_TIFF,
637     .priv_data_size = sizeof(TiffContext),
638     .init           = tiff_init,
639     .close          = tiff_end,
640     .decode         = decode_frame,
641     .capabilities   = CODEC_CAP_DR1,
642     .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
643 };