TIFF may omit compression tag.
[libav.git] / libavcodec / tiff.c
CommitLineData
a991b1fe
KS
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
a991b1fe 20 */
ee5c8a9b
DB
21
22/**
23 * TIFF image decoder
24 * @file tiff.c
25 * @author Konstantin Shishkov
26 */
a991b1fe
KS
27#include "avcodec.h"
28#ifdef CONFIG_ZLIB
29#include <zlib.h>
30#endif
ffc5430b 31#include "lzw.h"
716222db 32#include "tiff.h"
a991b1fe 33
339aabd5 34
a991b1fe
KS
35typedef struct TiffContext {
36 AVCodecContext *avctx;
37 AVFrame picture;
38
39 int width, height;
40 unsigned int bpp;
41 int le;
42 int compr;
e2570297 43 int invert;
a991b1fe
KS
44
45 int strips, rps;
46 int sot;
47 uint8_t* stripdata;
48 uint8_t* stripsizes;
49 int stripsize, stripoff;
ffc5430b 50 LZWState *lzw;
a991b1fe
KS
51} TiffContext;
52
53static int tget_short(uint8_t **p, int le){
fead30d4 54 int v = le ? AV_RL16(*p) : AV_RB16(*p);
a991b1fe
KS
55 *p += 2;
56 return v;
57}
58
59static int tget_long(uint8_t **p, int le){
fead30d4 60 int v = le ? AV_RL32(*p) : AV_RB32(*p);
a991b1fe
KS
61 *p += 4;
62 return v;
63}
64
65static int tget(uint8_t **p, int type, int le){
66 switch(type){
67 case TIFF_BYTE : return *(*p)++;
68 case TIFF_SHORT: return tget_short(p, le);
69 case TIFF_LONG : return tget_long (p, le);
70 default : return -1;
71 }
72}
73
74static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
75 int c, line, pixels, code;
76 uint8_t *ssrc = src;
77 int width = s->width * (s->bpp / 8);
78#ifdef CONFIG_ZLIB
79 uint8_t *zbuf; unsigned long outlen;
80
81 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
82 outlen = width * lines;
a991b1fe
KS
83 zbuf = av_malloc(outlen);
84 if(uncompress(zbuf, &outlen, src, size) != Z_OK){
85 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
86 av_free(zbuf);
87 return -1;
88 }
89 src = zbuf;
90 for(line = 0; line < lines; line++){
91 memcpy(dst, src, width);
92 dst += stride;
93 src += width;
94 }
95 av_free(zbuf);
96 return 0;
97 }
98#endif
ffc5430b
KS
99 if(s->compr == TIFF_LZW){
100 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
101 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
102 return -1;
103 }
104 }
a991b1fe
KS
105 for(line = 0; line < lines; line++){
106 if(src - ssrc > size){
107 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
108 return -1;
109 }
110 switch(s->compr){
111 case TIFF_RAW:
112 memcpy(dst, src, s->width * (s->bpp / 8));
113 src += s->width * (s->bpp / 8);
114 break;
115 case TIFF_PACKBITS:
116 for(pixels = 0; pixels < width;){
117 code = (int8_t)*src++;
118 if(code >= 0){
119 code++;
120 if(pixels + code > width){
121 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
122 return -1;
123 }
124 memcpy(dst + pixels, src, code);
125 src += code;
126 pixels += code;
127 }else if(code != -128){ // -127..-1
128 code = (-code) + 1;
129 if(pixels + code > width){
130 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
131 return -1;
132 }
133 c = *src++;
134 memset(dst + pixels, c, code);
135 pixels += code;
136 }
137 }
138 break;
ffc5430b
KS
139 case TIFF_LZW:
140 pixels = ff_lzw_decode(s->lzw, dst, width);
141 if(pixels < width){
142 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
143 return -1;
144 }
145 break;
a991b1fe
KS
146 }
147 dst += stride;
148 }
149 return 0;
150}
151
152
153static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
154{
155 int tag, type, count, off, value = 0;
156 uint8_t *src, *dst;
157 int i, j, ssize, soff, stride;
640b73e1 158 uint32_t *pal;
4a64abdc 159 uint8_t *rp, *gp, *bp;
a991b1fe
KS
160
161 tag = tget_short(&buf, s->le);
162 type = tget_short(&buf, s->le);
163 count = tget_long(&buf, s->le);
164 off = tget_long(&buf, s->le);
165
166 if(count == 1){
167 switch(type){
168 case TIFF_BYTE:
169 case TIFF_SHORT:
170 buf -= 4;
171 value = tget(&buf, type, s->le);
172 buf = NULL;
173 break;
174 case TIFF_LONG:
175 value = off;
176 buf = NULL;
177 break;
178 default:
179 value = -1;
180 buf = start + off;
181 }
339aabd5
KS
182 }else if(type_sizes[type] * count <= 4){
183 buf -= 4;
a991b1fe
KS
184 }else{
185 buf = start + off;
186 }
187
188 if(buf && (buf < start || buf > end_buf)){
189 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
190 return -1;
191 }
192
193 switch(tag){
194 case TIFF_WIDTH:
195 s->width = value;
196 break;
197 case TIFF_HEIGHT:
198 s->height = value;
a991b1fe
KS
199 break;
200 case TIFF_BPP:
201 if(count == 1) s->bpp = value;
202 else{
203 switch(type){
204 case TIFF_BYTE:
ec8a152b 205 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
a991b1fe
KS
206 break;
207 case TIFF_SHORT:
208 case TIFF_LONG:
ec8a152b
KS
209 s->bpp = 0;
210 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
a991b1fe
KS
211 break;
212 default:
213 s->bpp = -1;
214 }
215 }
e2570297
KS
216 switch(s->bpp){
217 case 8:
218 s->avctx->pix_fmt = PIX_FMT_PAL8;
219 break;
220 case 24:
221 s->avctx->pix_fmt = PIX_FMT_RGB24;
222 break;
ee0a0358
KS
223 case 16:
224 if(count == 1){
225 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
226 }else{
227 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
228 return -1;
229 }
230 break;
e2570297 231 default:
b39cf84c 232 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
e2570297
KS
233 return -1;
234 }
235 if(s->width != s->avctx->width || s->height != s->avctx->height){
236 if(avcodec_check_dimensions(s->avctx, s->width, s->height))
237 return -1;
238 avcodec_set_dimensions(s->avctx, s->width, s->height);
239 }
240 if(s->picture.data[0])
241 s->avctx->release_buffer(s->avctx, &s->picture);
242 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
243 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
a991b1fe
KS
244 return -1;
245 }
e2570297
KS
246 if(s->bpp == 8){
247 /* make default grayscale pal */
640b73e1 248 pal = (uint32_t *) s->picture.data[1];
e2570297
KS
249 for(i = 0; i < 256; i++)
250 pal[i] = i * 0x010101;
251 }
a991b1fe
KS
252 break;
253 case TIFF_COMPR:
254 s->compr = value;
255 switch(s->compr){
256 case TIFF_RAW:
257 case TIFF_PACKBITS:
ffc5430b 258 case TIFF_LZW:
a991b1fe
KS
259 break;
260 case TIFF_DEFLATE:
261 case TIFF_ADOBE_DEFLATE:
262#ifdef CONFIG_ZLIB
263 break;
264#else
265 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
266 return -1;
267#endif
a991b1fe
KS
268 case TIFF_G3:
269 av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
270 return -1;
271 case TIFF_G4:
272 av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
273 return -1;
274 case TIFF_CCITT_RLE:
275 av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
276 return -1;
27c18939
KS
277 case TIFF_JPEG:
278 case TIFF_NEWJPEG:
279 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
280 return -1;
a991b1fe
KS
281 default:
282 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
283 return -1;
284 }
285 break;
286 case TIFF_ROWSPERSTRIP:
380fd08f 287 if(value < 1){
a991b1fe
KS
288 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
289 return -1;
290 }
291 s->rps = value;
292 break;
293 case TIFF_STRIP_OFFS:
294 if(count == 1){
295 s->stripdata = NULL;
296 s->stripoff = value;
297 }else
298 s->stripdata = start + off;
299 s->strips = count;
615259a3 300 if(s->strips == 1) s->rps = s->height;
a991b1fe
KS
301 s->sot = type;
302 if(s->stripdata > end_buf){
303 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
304 return -1;
305 }
306 break;
307 case TIFF_STRIP_SIZE:
308 if(count == 1){
309 s->stripsizes = NULL;
310 s->stripsize = value;
311 s->strips = 1;
312 }else{
313 s->stripsizes = start + off;
314 }
315 s->strips = count;
316 if(s->stripsizes > end_buf){
317 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
318 return -1;
319 }
320 if(!pic->data[0]){
321 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
322 return -1;
323 }
324 /* now we have the data and may start decoding */
325 stride = pic->linesize[0];
326 dst = pic->data[0];
327 for(i = 0; i < s->height; i += s->rps){
328 if(s->stripsizes)
329 ssize = tget(&s->stripsizes, type, s->le);
330 else
331 ssize = s->stripsize;
332
333 if(s->stripdata){
334 soff = tget(&s->stripdata, s->sot, s->le);
335 }else
336 soff = s->stripoff;
337 src = start + soff;
338 if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
339 break;
340 dst += s->rps * stride;
341 }
342 break;
343 case TIFF_PREDICTOR:
344 if(!pic->data[0]){
345 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
346 return -1;
347 }
348 if(value == 2){
b0c32a70 349 src = pic->data[0];
a991b1fe
KS
350 stride = pic->linesize[0];
351 soff = s->bpp >> 3;
352 ssize = s->width * soff;
353 for(i = 0; i < s->height; i++) {
354 for(j = soff; j < ssize; j++)
355 src[j] += src[j - soff];
356 src += stride;
357 }
358 }
359 break;
e2570297
KS
360 case TIFF_INVERT:
361 switch(value){
362 case 0:
363 s->invert = 1;
364 break;
365 case 1:
366 s->invert = 0;
367 break;
d00026d0
KS
368 case 2:
369 case 3:
370 break;
371 default:
372 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
373 return -1;
e2570297
KS
374 }
375 break;
376 case TIFF_PAL:
377 if(s->avctx->pix_fmt != PIX_FMT_PAL8){
378 av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
379 return -1;
380 }
640b73e1 381 pal = (uint32_t *) s->picture.data[1];
339aabd5 382 off = type_sizes[type];
e2570297
KS
383 rp = buf;
384 gp = buf + count / 3 * off;
385 bp = buf + count / 3 * off * 2;
339aabd5 386 off = (type_sizes[type] - 1) << 3;
e2570297
KS
387 for(i = 0; i < count / 3; i++){
388 j = (tget(&rp, type, s->le) >> off) << 16;
389 j |= (tget(&gp, type, s->le) >> off) << 8;
390 j |= tget(&bp, type, s->le) >> off;
391 pal[i] = j;
392 }
2878cecd
KS
393 break;
394 case TIFF_PLANAR:
395 if(value == 2){
396 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
397 return -1;
398 }
399 break;
a991b1fe
KS
400 }
401 return 0;
402}
403
404static int decode_frame(AVCodecContext *avctx,
405 void *data, int *data_size,
406 uint8_t *buf, int buf_size)
407{
408 TiffContext * const s = avctx->priv_data;
409 AVFrame *picture = data;
410 AVFrame * const p= (AVFrame*)&s->picture;
411 uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
412 int id, le, off;
413 int i, entries;
414
415 //parse image header
fead30d4 416 id = AV_RL16(buf); buf += 2;
a991b1fe
KS
417 if(id == 0x4949) le = 1;
418 else if(id == 0x4D4D) le = 0;
419 else{
420 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
421 return -1;
422 }
423 s->le = le;
e2570297 424 s->invert = 0;
329851e0 425 s->compr = TIFF_RAW;
a991b1fe
KS
426 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
427 // that further identifies the file as a TIFF file"
428 if(tget_short(&buf, le) != 42){
429 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
430 return -1;
431 }
432 /* parse image file directory */
433 off = tget_long(&buf, le);
434 if(orig_buf + off + 14 >= end_buf){
435 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
436 return -1;
437 }
438 buf = orig_buf + off;
439 entries = tget_short(&buf, le);
440 for(i = 0; i < entries; i++){
441 if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
442 return -1;
443 buf += 12;
444 }
445
e2570297
KS
446 if(s->invert){
447 uint8_t *src;
448 int j;
449
450 src = s->picture.data[0];
451 for(j = 0; j < s->height; j++){
452 for(i = 0; i < s->picture.linesize[0]; i++)
453 src[i] = 255 - src[i];
454 src += s->picture.linesize[0];
455 }
456 }
a991b1fe
KS
457 *picture= *(AVFrame*)&s->picture;
458 *data_size = sizeof(AVPicture);
459
460 return buf_size;
461}
462
463static int tiff_init(AVCodecContext *avctx){
464 TiffContext *s = avctx->priv_data;
465
466 s->width = 0;
467 s->height = 0;
468 s->avctx = avctx;
469 avcodec_get_frame_defaults((AVFrame*)&s->picture);
470 avctx->coded_frame= (AVFrame*)&s->picture;
471 s->picture.data[0] = NULL;
ffc5430b 472 ff_lzw_decode_open(&s->lzw);
a991b1fe
KS
473
474 return 0;
475}
476
477static int tiff_end(AVCodecContext *avctx)
478{
479 TiffContext * const s = avctx->priv_data;
480
ffc5430b 481 ff_lzw_decode_close(&s->lzw);
a991b1fe
KS
482 if(s->picture.data[0])
483 avctx->release_buffer(avctx, &s->picture);
484 return 0;
485}
486
487AVCodec tiff_decoder = {
488 "tiff",
489 CODEC_TYPE_VIDEO,
490 CODEC_ID_TIFF,
491 sizeof(TiffContext),
492 tiff_init,
493 NULL,
494 tiff_end,
495 decode_frame,
496 0,
497 NULL
498};