Mercurial > libavcodec.hg
annotate tiff.c @ 6047:2f9c17454842 libavcodec
Add option for user to scale the amount of dynamic range compression which is
applied by the audio decoder, and use that option in the AC3 decoder.
author | jbr |
---|---|
date | Thu, 20 Dec 2007 00:55:08 +0000 |
parents | a6c9aa23d379 |
children | e7c178d397ea |
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 */ | |
4782
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
21 |
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 * TIFF image decoder |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
24 * @file tiff.c |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
25 * @author Konstantin Shishkov |
bca8924ed36c
Add some Doxygen comments, by Kamil Nowosad, k.nowosad students.mimuw.edu pl.
diego
parents:
4774
diff
changeset
|
26 */ |
4013 | 27 #include "avcodec.h" |
28 #ifdef CONFIG_ZLIB | |
29 #include <zlib.h> | |
30 #endif | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
31 #include "lzw.h" |
4774
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
4405
diff
changeset
|
32 #include "tiff.h" |
4013 | 33 |
4190 | 34 |
4013 | 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; | |
4186 | 43 int invert; |
4013 | 44 |
45 int strips, rps; | |
46 int sot; | |
47 uint8_t* stripdata; | |
48 uint8_t* stripsizes; | |
49 int stripsize, stripoff; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
50 LZWState *lzw; |
4013 | 51 } TiffContext; |
52 | |
53 static int tget_short(uint8_t **p, int le){ | |
4364 | 54 int v = le ? AV_RL16(*p) : AV_RB16(*p); |
4013 | 55 *p += 2; |
56 return v; | |
57 } | |
58 | |
59 static int tget_long(uint8_t **p, int le){ | |
4364 | 60 int v = le ? AV_RL32(*p) : AV_RB32(*p); |
4013 | 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 | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
99 if(s->compr == TIFF_LZW){ |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
100 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
|
101 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
|
102 return -1; |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
103 } |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
104 } |
4013 | 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; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
139 case TIFF_LZW: |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
140 pixels = ff_lzw_decode(s->lzw, dst, width); |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
141 if(pixels < width){ |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
142 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
|
143 return -1; |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
144 } |
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
145 break; |
4013 | 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; | |
5261 | 158 uint32_t *pal; |
5257 | 159 uint8_t *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 */ | |
5261 | 248 pal = (uint32_t *) s->picture.data[1]; |
4186 | 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 } | |
5261 | 381 pal = (uint32_t *) 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; |
5962 | 425 s->compr = TIFF_RAW; |
4013 | 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 | |
4186 | 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 } | |
4013 | 457 *picture= *(AVFrame*)&s->picture; |
458 *data_size = sizeof(AVPicture); | |
459 | |
460 return buf_size; | |
461 } | |
462 | |
463 static 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; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
472 ff_lzw_decode_open(&s->lzw); |
4013 | 473 |
474 return 0; | |
475 } | |
476 | |
477 static int tiff_end(AVCodecContext *avctx) | |
478 { | |
479 TiffContext * const s = avctx->priv_data; | |
480 | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
481 ff_lzw_decode_close(&s->lzw); |
4013 | 482 if(s->picture.data[0]) |
483 avctx->release_buffer(avctx, &s->picture); | |
484 return 0; | |
485 } | |
486 | |
487 AVCodec 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 }; |