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