6c1c346e61d7c728412194ddbc062123e2f4c830
[libav.git] / libavcodec / tiff.c
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 tiff.c
25 * @author Konstantin Shishkov
26 */
27 #include "avcodec.h"
28 #ifdef CONFIG_ZLIB
29 #include <zlib.h>
30 #endif
31 #include "lzw.h"
32 #include "tiff.h"
33
34
35 typedef struct TiffContext {
36 AVCodecContext *avctx;
37 AVFrame picture;
38
39 int width, height;
40 unsigned int bpp;
41 int le;
42 int compr;
43 int invert;
44
45 int strips, rps;
46 int sot;
47 uint8_t* stripdata;
48 uint8_t* stripsizes;
49 int stripsize, stripoff;
50 LZWState *lzw;
51 } TiffContext;
52
53 static int tget_short(uint8_t **p, int le){
54 int v = le ? AV_RL16(*p) : AV_RB16(*p);
55 *p += 2;
56 return v;
57 }
58
59 static int tget_long(uint8_t **p, int le){
60 int v = le ? AV_RL32(*p) : AV_RB32(*p);
61 *p += 4;
62 return v;
63 }
64
65 static 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
74 static 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;
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
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 }
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;
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;
146 }
147 dst += stride;
148 }
149 return 0;
150 }
151
152
153 static 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;
158 uint32_t *pal;
159 uint8_t *rp, *gp, *bp;
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 }
182 }else if(type_sizes[type] * count <= 4){
183 buf -= 4;
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;
199 break;
200 case TIFF_BPP:
201 if(count == 1) s->bpp = value;
202 else{
203 switch(type){
204 case TIFF_BYTE:
205 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
206 break;
207 case TIFF_SHORT:
208 case TIFF_LONG:
209 s->bpp = 0;
210 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
211 break;
212 default:
213 s->bpp = -1;
214 }
215 }
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;
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;
231 default:
232 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
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");
244 return -1;
245 }
246 if(s->bpp == 8){
247 /* make default grayscale pal */
248 pal = (uint32_t *) s->picture.data[1];
249 for(i = 0; i < 256; i++)
250 pal[i] = i * 0x010101;
251 }
252 break;
253 case TIFF_COMPR:
254 s->compr = value;
255 switch(s->compr){
256 case TIFF_RAW:
257 case TIFF_PACKBITS:
258 case TIFF_LZW:
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
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;
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;
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:
287 if(value < 1){
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;
300 if(s->strips == 1) s->rps = s->height;
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){
349 src = pic->data[0];
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;
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;
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;
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 }
381 pal = (uint32_t *) s->picture.data[1];
382 off = type_sizes[type];
383 rp = buf;
384 gp = buf + count / 3 * off;
385 bp = buf + count / 3 * off * 2;
386 off = (type_sizes[type] - 1) << 3;
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 }
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;
400 }
401 return 0;
402 }
403
404 static 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
416 id = AV_RL16(buf); buf += 2;
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;
424 s->invert = 0;
425 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
426 // that further identifies the file as a TIFF file"
427 if(tget_short(&buf, le) != 42){
428 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
429 return -1;
430 }
431 /* parse image file directory */
432 off = tget_long(&buf, le);
433 if(orig_buf + off + 14 >= end_buf){
434 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
435 return -1;
436 }
437 buf = orig_buf + off;
438 entries = tget_short(&buf, le);
439 for(i = 0; i < entries; i++){
440 if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
441 return -1;
442 buf += 12;
443 }
444
445 if(s->invert){
446 uint8_t *src;
447 int j;
448
449 src = s->picture.data[0];
450 for(j = 0; j < s->height; j++){
451 for(i = 0; i < s->picture.linesize[0]; i++)
452 src[i] = 255 - src[i];
453 src += s->picture.linesize[0];
454 }
455 }
456 *picture= *(AVFrame*)&s->picture;
457 *data_size = sizeof(AVPicture);
458
459 return buf_size;
460 }
461
462 static int tiff_init(AVCodecContext *avctx){
463 TiffContext *s = avctx->priv_data;
464
465 s->width = 0;
466 s->height = 0;
467 s->avctx = avctx;
468 avcodec_get_frame_defaults((AVFrame*)&s->picture);
469 avctx->coded_frame= (AVFrame*)&s->picture;
470 s->picture.data[0] = NULL;
471 ff_lzw_decode_open(&s->lzw);
472
473 return 0;
474 }
475
476 static int tiff_end(AVCodecContext *avctx)
477 {
478 TiffContext * const s = avctx->priv_data;
479
480 ff_lzw_decode_close(&s->lzw);
481 if(s->picture.data[0])
482 avctx->release_buffer(avctx, &s->picture);
483 return 0;
484 }
485
486 AVCodec tiff_decoder = {
487 "tiff",
488 CODEC_TYPE_VIDEO,
489 CODEC_ID_TIFF,
490 sizeof(TiffContext),
491 tiff_init,
492 NULL,
493 tiff_end,
494 decode_frame,
495 0,
496 NULL
497 };