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