OSDN Git Service

doc: explain __STDC_CONSTANT_MACROS in C++
[coroid/libav_saccubus.git] / 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 (!s->fill_order) {
177                 memcpy(dst, src, width);
178             } else {
179                 int i;
180                 for (i = 0; i < width; i++)
181                     dst[i] = av_reverse[src[i]];
182             }
183             src += width;
184             break;
185         case TIFF_PACKBITS:
186             for(pixels = 0; pixels < width;){
187                 code = (int8_t)*src++;
188                 if(code >= 0){
189                     code++;
190                     if(pixels + code > width){
191                         av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
192                         return -1;
193                     }
194                     memcpy(dst + pixels, src, code);
195                     src += code;
196                     pixels += code;
197                 }else if(code != -128){ // -127..-1
198                     code = (-code) + 1;
199                     if(pixels + code > width){
200                         av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
201                         return -1;
202                     }
203                     c = *src++;
204                     memset(dst + pixels, c, code);
205                     pixels += code;
206                 }
207             }
208             break;
209         case TIFF_LZW:
210             pixels = ff_lzw_decode(s->lzw, dst, width);
211             if(pixels < width){
212                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
213                 return -1;
214             }
215             break;
216         }
217         dst += stride;
218     }
219     return 0;
220 }
221
222 static int init_image(TiffContext *s)
223 {
224     int i, ret;
225     uint32_t *pal;
226
227     switch (s->bpp * 10 + s->bppcount) {
228     case 11:
229         s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
230         break;
231     case 81:
232         s->avctx->pix_fmt = PIX_FMT_PAL8;
233         break;
234     case 243:
235         s->avctx->pix_fmt = PIX_FMT_RGB24;
236         break;
237     case 161:
238         s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
239         break;
240     case 324:
241         s->avctx->pix_fmt = PIX_FMT_RGBA;
242         break;
243     case 483:
244         s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
245         break;
246     default:
247         av_log(s->avctx, AV_LOG_ERROR,
248                "This format is not supported (bpp=%d, bppcount=%d)\n",
249                s->bpp, s->bppcount);
250         return AVERROR_INVALIDDATA;
251     }
252     if (s->width != s->avctx->width || s->height != s->avctx->height) {
253         if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
254             return ret;
255         avcodec_set_dimensions(s->avctx, s->width, s->height);
256     }
257     if (s->picture.data[0])
258         s->avctx->release_buffer(s->avctx, &s->picture);
259     if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
260         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
261         return ret;
262     }
263     if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
264         if (s->palette_is_set) {
265             memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
266         } else {
267             /* make default grayscale pal */
268             pal = (uint32_t *) s->picture.data[1];
269             for (i = 0; i < 256; i++)
270                 pal[i] = i * 0x010101;
271         }
272     }
273     return 0;
274 }
275
276 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
277 {
278     int tag, type, count, off, value = 0;
279     int i, j;
280     uint32_t *pal;
281     const uint8_t *rp, *gp, *bp;
282
283     tag = tget_short(&buf, s->le);
284     type = tget_short(&buf, s->le);
285     count = tget_long(&buf, s->le);
286     off = tget_long(&buf, s->le);
287
288     if(count == 1){
289         switch(type){
290         case TIFF_BYTE:
291         case TIFF_SHORT:
292             buf -= 4;
293             value = tget(&buf, type, s->le);
294             buf = NULL;
295             break;
296         case TIFF_LONG:
297             value = off;
298             buf = NULL;
299             break;
300         case TIFF_STRING:
301             if(count <= 4){
302                 buf -= 4;
303                 break;
304             }
305         default:
306             value = -1;
307             buf = start + off;
308         }
309     }else if(type_sizes[type] * count <= 4){
310         buf -= 4;
311     }else{
312         buf = start + off;
313     }
314
315     if(buf && (buf < start || buf > end_buf)){
316         av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
317         return -1;
318     }
319
320     switch(tag){
321     case TIFF_WIDTH:
322         s->width = value;
323         break;
324     case TIFF_HEIGHT:
325         s->height = value;
326         break;
327     case TIFF_BPP:
328         s->bppcount = count;
329         if(count > 4){
330             av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
331             return -1;
332         }
333         if(count == 1) s->bpp = value;
334         else{
335             switch(type){
336             case TIFF_BYTE:
337                 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
338                 break;
339             case TIFF_SHORT:
340             case TIFF_LONG:
341                 s->bpp = 0;
342                 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
343                 break;
344             default:
345                 s->bpp = -1;
346             }
347         }
348         break;
349     case TIFF_SAMPLES_PER_PIXEL:
350         if (count != 1) {
351             av_log(s->avctx, AV_LOG_ERROR,
352                    "Samples per pixel requires a single value, many provided\n");
353             return AVERROR_INVALIDDATA;
354         }
355         if (s->bppcount == 1)
356             s->bpp *= value;
357         s->bppcount = value;
358         break;
359     case TIFF_COMPR:
360         s->compr = value;
361         s->predictor = 0;
362         switch(s->compr){
363         case TIFF_RAW:
364         case TIFF_PACKBITS:
365         case TIFF_LZW:
366         case TIFF_CCITT_RLE:
367             break;
368         case TIFF_G3:
369         case TIFF_G4:
370             s->fax_opts = 0;
371             break;
372         case TIFF_DEFLATE:
373         case TIFF_ADOBE_DEFLATE:
374 #if CONFIG_ZLIB
375             break;
376 #else
377             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
378             return -1;
379 #endif
380         case TIFF_JPEG:
381         case TIFF_NEWJPEG:
382             av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
383             return -1;
384         default:
385             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
386             return -1;
387         }
388         break;
389     case TIFF_ROWSPERSTRIP:
390         if(type == TIFF_LONG && value == -1)
391             value = s->avctx->height;
392         if(value < 1){
393             av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
394             return -1;
395         }
396         s->rps = value;
397         break;
398     case TIFF_STRIP_OFFS:
399         if(count == 1){
400             s->stripdata = NULL;
401             s->stripoff = value;
402         }else
403             s->stripdata = start + off;
404         s->strips = count;
405         if(s->strips == 1) s->rps = s->height;
406         s->sot = type;
407         if(s->stripdata > end_buf){
408             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
409             return -1;
410         }
411         break;
412     case TIFF_STRIP_SIZE:
413         if(count == 1){
414             s->stripsizes = NULL;
415             s->stripsize = value;
416             s->strips = 1;
417         }else{
418             s->stripsizes = start + off;
419         }
420         s->strips = count;
421         s->sstype = type;
422         if(s->stripsizes > end_buf){
423             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
424             return -1;
425         }
426         break;
427     case TIFF_PREDICTOR:
428         s->predictor = value;
429         break;
430     case TIFF_INVERT:
431         switch(value){
432         case 0:
433             s->invert = 1;
434             break;
435         case 1:
436             s->invert = 0;
437             break;
438         case 2:
439         case 3:
440             break;
441         default:
442             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
443             return -1;
444         }
445         break;
446     case TIFF_FILL_ORDER:
447         if(value < 1 || value > 2){
448             av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
449             value = 1;
450         }
451         s->fill_order = value - 1;
452         break;
453     case TIFF_PAL:
454         pal = (uint32_t *) s->palette;
455         off = type_sizes[type];
456         rp = buf;
457         gp = buf + count / 3 * off;
458         bp = buf + count / 3 * off * 2;
459         off = (type_sizes[type] - 1) << 3;
460         for(i = 0; i < count / 3; i++){
461             j = (tget(&rp, type, s->le) >> off) << 16;
462             j |= (tget(&gp, type, s->le) >> off) << 8;
463             j |= tget(&bp, type, s->le) >> off;
464             pal[i] = j;
465         }
466         s->palette_is_set = 1;
467         break;
468     case TIFF_PLANAR:
469         if(value == 2){
470             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
471             return -1;
472         }
473         break;
474     case TIFF_T4OPTIONS:
475         if(s->compr == TIFF_G3)
476             s->fax_opts = value;
477         break;
478     case TIFF_T6OPTIONS:
479         if(s->compr == TIFF_G4)
480             s->fax_opts = value;
481         break;
482     default:
483         av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
484     }
485     return 0;
486 }
487
488 static int decode_frame(AVCodecContext *avctx,
489                         void *data, int *data_size,
490                         AVPacket *avpkt)
491 {
492     const uint8_t *buf = avpkt->data;
493     int buf_size = avpkt->size;
494     TiffContext * const s = avctx->priv_data;
495     AVFrame *picture = data;
496     AVFrame * const p= (AVFrame*)&s->picture;
497     const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
498     int id, le, off, ret;
499     int i, j, entries;
500     int stride, soff, ssize;
501     uint8_t *dst;
502
503     //parse image header
504     id = AV_RL16(buf); buf += 2;
505     if(id == 0x4949) le = 1;
506     else if(id == 0x4D4D) le = 0;
507     else{
508         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
509         return -1;
510     }
511     s->le = le;
512     s->invert = 0;
513     s->compr = TIFF_RAW;
514     s->fill_order = 0;
515     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
516     // that further identifies the file as a TIFF file"
517     if(tget_short(&buf, le) != 42){
518         av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
519         return -1;
520     }
521     /* parse image file directory */
522     off = tget_long(&buf, le);
523     if(orig_buf + off + 14 >= end_buf){
524         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
525         return -1;
526     }
527     buf = orig_buf + off;
528     entries = tget_short(&buf, le);
529     for(i = 0; i < entries; i++){
530         if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
531             return -1;
532         buf += 12;
533     }
534     if(!s->stripdata && !s->stripoff){
535         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
536         return -1;
537     }
538     /* now we have the data and may start decoding */
539     if ((ret = init_image(s)) < 0)
540         return ret;
541
542     if(s->strips == 1 && !s->stripsize){
543         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
544         s->stripsize = buf_size - s->stripoff;
545     }
546     stride = p->linesize[0];
547     dst = p->data[0];
548     for(i = 0; i < s->height; i += s->rps){
549         if(s->stripsizes)
550             ssize = tget(&s->stripsizes, s->sstype, s->le);
551         else
552             ssize = s->stripsize;
553
554         if (ssize > buf_size) {
555             av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
556             return -1;
557         }
558
559         if(s->stripdata){
560             soff = tget(&s->stripdata, s->sot, s->le);
561         }else
562             soff = s->stripoff;
563         if (soff < 0) {
564             av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
565             return AVERROR(EINVAL);
566         }
567         if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
568             break;
569         dst += s->rps * stride;
570     }
571     if(s->predictor == 2){
572         dst = p->data[0];
573         soff = s->bpp >> 3;
574         ssize = s->width * soff;
575         for(i = 0; i < s->height; i++) {
576             for(j = soff; j < ssize; j++)
577                 dst[j] += dst[j - soff];
578             dst += stride;
579         }
580     }
581
582     if(s->invert){
583         uint8_t *src;
584         int j;
585
586         src = s->picture.data[0];
587         for(j = 0; j < s->height; j++){
588             for(i = 0; i < s->picture.linesize[0]; i++)
589                 src[i] = 255 - src[i];
590             src += s->picture.linesize[0];
591         }
592     }
593     *picture= *(AVFrame*)&s->picture;
594     *data_size = sizeof(AVPicture);
595
596     return buf_size;
597 }
598
599 static av_cold int tiff_init(AVCodecContext *avctx){
600     TiffContext *s = avctx->priv_data;
601
602     s->width = 0;
603     s->height = 0;
604     s->avctx = avctx;
605     avcodec_get_frame_defaults((AVFrame*)&s->picture);
606     avctx->coded_frame= (AVFrame*)&s->picture;
607     ff_lzw_decode_open(&s->lzw);
608     ff_ccitt_unpack_init();
609
610     return 0;
611 }
612
613 static av_cold int tiff_end(AVCodecContext *avctx)
614 {
615     TiffContext * const s = avctx->priv_data;
616
617     ff_lzw_decode_close(&s->lzw);
618     if(s->picture.data[0])
619         avctx->release_buffer(avctx, &s->picture);
620     return 0;
621 }
622
623 AVCodec ff_tiff_decoder = {
624     .name           = "tiff",
625     .type           = AVMEDIA_TYPE_VIDEO,
626     .id             = CODEC_ID_TIFF,
627     .priv_data_size = sizeof(TiffContext),
628     .init           = tiff_init,
629     .close          = tiff_end,
630     .decode         = decode_frame,
631     .capabilities   = CODEC_CAP_DR1,
632     .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
633 };