Mercurial > libavcodec.hg
annotate tiff.c @ 5153:b985439e3e15 libavcodec
fix some printf format specifiers
author | mru |
---|---|
date | Sat, 16 Jun 2007 14:52:05 +0000 |
parents | bca8924ed36c |
children | 2b72f9bc4f06 |
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 */ | |
4782
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
22 |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
23 /** |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
24 * TIFF image decoder |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
25 * @file tiff.c |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
26 * @author Konstantin Shishkov |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
27 */ |
4013 | 28 #include "avcodec.h" |
29 #ifdef CONFIG_ZLIB | |
30 #include <zlib.h> | |
31 #endif | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
32 #include "lzw.h" |
4774
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
4405
diff
changeset
|
33 #include "tiff.h" |
4013 | 34 |
4190 | 35 |
4013 | 36 typedef struct TiffContext { |
37 AVCodecContext *avctx; | |
38 AVFrame picture; | |
39 | |
40 int width, height; | |
41 unsigned int bpp; | |
42 int le; | |
43 int compr; | |
4186 | 44 int invert; |
4013 | 45 |
46 int strips, rps; | |
47 int sot; | |
48 uint8_t* stripdata; | |
49 uint8_t* stripsizes; | |
50 int stripsize, stripoff; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
51 LZWState *lzw; |
4013 | 52 } TiffContext; |
53 | |
54 static int tget_short(uint8_t **p, int le){ | |
4364 | 55 int v = le ? AV_RL16(*p) : AV_RB16(*p); |
4013 | 56 *p += 2; |
57 return v; | |
58 } | |
59 | |
60 static int tget_long(uint8_t **p, int le){ | |
4364 | 61 int v = le ? AV_RL32(*p) : AV_RB32(*p); |
4013 | 62 *p += 4; |
63 return v; | |
64 } | |
65 | |
66 static int tget(uint8_t **p, int type, int le){ | |
67 switch(type){ | |
68 case TIFF_BYTE : return *(*p)++; | |
69 case TIFF_SHORT: return tget_short(p, le); | |
70 case TIFF_LONG : return tget_long (p, le); | |
71 default : return -1; | |
72 } | |
73 } | |
74 | |
75 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){ | |
76 int c, line, pixels, code; | |
77 uint8_t *ssrc = src; | |
78 int width = s->width * (s->bpp / 8); | |
79 #ifdef CONFIG_ZLIB | |
80 uint8_t *zbuf; unsigned long outlen; | |
81 | |
82 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){ | |
83 outlen = width * lines; | |
84 zbuf = av_malloc(outlen); | |
85 if(uncompress(zbuf, &outlen, src, size) != Z_OK){ | |
86 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines); | |
87 av_free(zbuf); | |
88 return -1; | |
89 } | |
90 src = zbuf; | |
91 for(line = 0; line < lines; line++){ | |
92 memcpy(dst, src, width); | |
93 dst += stride; | |
94 src += width; | |
95 } | |
96 av_free(zbuf); | |
97 return 0; | |
98 } | |
99 #endif | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
100 if(s->compr == TIFF_LZW){ |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
101 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
|
102 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
|
103 return -1; |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
104 } |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
105 } |
4013 | 106 for(line = 0; line < lines; line++){ |
107 if(src - ssrc > size){ | |
108 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n"); | |
109 return -1; | |
110 } | |
111 switch(s->compr){ | |
112 case TIFF_RAW: | |
113 memcpy(dst, src, s->width * (s->bpp / 8)); | |
114 src += s->width * (s->bpp / 8); | |
115 break; | |
116 case TIFF_PACKBITS: | |
117 for(pixels = 0; pixels < width;){ | |
118 code = (int8_t)*src++; | |
119 if(code >= 0){ | |
120 code++; | |
121 if(pixels + code > width){ | |
122 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n"); | |
123 return -1; | |
124 } | |
125 memcpy(dst + pixels, src, code); | |
126 src += code; | |
127 pixels += code; | |
128 }else if(code != -128){ // -127..-1 | |
129 code = (-code) + 1; | |
130 if(pixels + code > width){ | |
131 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); | |
132 return -1; | |
133 } | |
134 c = *src++; | |
135 memset(dst + pixels, c, code); | |
136 pixels += code; | |
137 } | |
138 } | |
139 break; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
140 case TIFF_LZW: |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
141 pixels = ff_lzw_decode(s->lzw, dst, width); |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
142 if(pixels < width){ |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
143 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
|
144 return -1; |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
145 } |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
146 break; |
4013 | 147 } |
148 dst += stride; | |
149 } | |
150 return 0; | |
151 } | |
152 | |
153 | |
154 static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic) | |
155 { | |
156 int tag, type, count, off, value = 0; | |
157 uint8_t *src, *dst; | |
158 int i, j, ssize, soff, stride; | |
4186 | 159 int *pal, *rp, *gp, *bp; |
4013 | 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 } | |
4190 | 182 }else if(type_sizes[type] * count <= 4){ |
183 buf -= 4; | |
4013 | 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: | |
4183 | 205 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF); |
4013 | 206 break; |
207 case TIFF_SHORT: | |
208 case TIFF_LONG: | |
4183 | 209 s->bpp = 0; |
210 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le); | |
4013 | 211 break; |
212 default: | |
213 s->bpp = -1; | |
214 } | |
215 } | |
4186 | 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; | |
4193 | 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; | |
4186 | 231 default: |
4191 | 232 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp); |
4013 | 233 return -1; |
234 } | |
4186 | 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 = s->picture.data[1]; | |
249 for(i = 0; i < 256; i++) | |
250 pal[i] = i * 0x010101; | |
251 } | |
4013 | 252 break; |
253 case TIFF_COMPR: | |
254 s->compr = value; | |
255 switch(s->compr){ | |
256 case TIFF_RAW: | |
257 case TIFF_PACKBITS: | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
258 case TIFF_LZW: |
4013 | 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; | |
4184 | 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; | |
4013 | 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: | |
4185 | 287 if(value < 1){ |
4013 | 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; | |
4405
48952197d91f
Some TIFFs do not set rows per strip for single strip.
kostya
parents:
4364
diff
changeset
|
300 if(s->strips == 1) s->rps = s->height; |
4013 | 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){ | |
4079 | 349 src = pic->data[0]; |
4013 | 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; | |
4186 | 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; | |
4187
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
368 case 2: |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
369 case 3: |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
370 break; |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
371 default: |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
372 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
|
373 return -1; |
4186 | 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 = s->picture.data[1]; | |
4190 | 382 off = type_sizes[type]; |
4186 | 383 rp = buf; |
384 gp = buf + count / 3 * off; | |
385 bp = buf + count / 3 * off * 2; | |
4190 | 386 off = (type_sizes[type] - 1) << 3; |
4186 | 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 } | |
4192 | 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; | |
4013 | 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 | |
4364 | 416 id = AV_RL16(buf); buf += 2; |
4013 | 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; | |
4186 | 424 s->invert = 0; |
4013 | 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 | |
4186 | 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 } | |
4013 | 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; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
471 ff_lzw_decode_open(&s->lzw); |
4013 | 472 |
473 return 0; | |
474 } | |
475 | |
476 static int tiff_end(AVCodecContext *avctx) | |
477 { | |
478 TiffContext * const s = avctx->priv_data; | |
479 | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
480 ff_lzw_decode_close(&s->lzw); |
4013 | 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 }; |