Mercurial > libavcodec.hg
annotate tiff.c @ 5231:07a97575d0c4 libavcodec
Add support for streams with different chroma_qp_index_offset
for Cr and Cb
Patch by Andreas ªÓman % andreas A olebyn P nu %
Original thread:
Date: Jun 26, 2007 8:48 PM
subject: [FFmpeg-devel] Color corruption and seeking errors with H264 disc sources
author | gpoirier |
---|---|
date | Fri, 06 Jul 2007 14:21:37 +0000 |
parents | 2b72f9bc4f06 |
children | f2eaf5afe6ae |
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; | |
4186 | 158 int *pal, *rp, *gp, *bp; |
4013 | 159 |
160 tag = tget_short(&buf, s->le); | |
161 type = tget_short(&buf, s->le); | |
162 count = tget_long(&buf, s->le); | |
163 off = tget_long(&buf, s->le); | |
164 | |
165 if(count == 1){ | |
166 switch(type){ | |
167 case TIFF_BYTE: | |
168 case TIFF_SHORT: | |
169 buf -= 4; | |
170 value = tget(&buf, type, s->le); | |
171 buf = NULL; | |
172 break; | |
173 case TIFF_LONG: | |
174 value = off; | |
175 buf = NULL; | |
176 break; | |
177 default: | |
178 value = -1; | |
179 buf = start + off; | |
180 } | |
4190 | 181 }else if(type_sizes[type] * count <= 4){ |
182 buf -= 4; | |
4013 | 183 }else{ |
184 buf = start + off; | |
185 } | |
186 | |
187 if(buf && (buf < start || buf > end_buf)){ | |
188 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n"); | |
189 return -1; | |
190 } | |
191 | |
192 switch(tag){ | |
193 case TIFF_WIDTH: | |
194 s->width = value; | |
195 break; | |
196 case TIFF_HEIGHT: | |
197 s->height = value; | |
198 break; | |
199 case TIFF_BPP: | |
200 if(count == 1) s->bpp = value; | |
201 else{ | |
202 switch(type){ | |
203 case TIFF_BYTE: | |
4183 | 204 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF); |
4013 | 205 break; |
206 case TIFF_SHORT: | |
207 case TIFF_LONG: | |
4183 | 208 s->bpp = 0; |
209 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le); | |
4013 | 210 break; |
211 default: | |
212 s->bpp = -1; | |
213 } | |
214 } | |
4186 | 215 switch(s->bpp){ |
216 case 8: | |
217 s->avctx->pix_fmt = PIX_FMT_PAL8; | |
218 break; | |
219 case 24: | |
220 s->avctx->pix_fmt = PIX_FMT_RGB24; | |
221 break; | |
4193 | 222 case 16: |
223 if(count == 1){ | |
224 s->avctx->pix_fmt = PIX_FMT_GRAY16BE; | |
225 }else{ | |
226 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp); | |
227 return -1; | |
228 } | |
229 break; | |
4186 | 230 default: |
4191 | 231 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp); |
4013 | 232 return -1; |
233 } | |
4186 | 234 if(s->width != s->avctx->width || s->height != s->avctx->height){ |
235 if(avcodec_check_dimensions(s->avctx, s->width, s->height)) | |
236 return -1; | |
237 avcodec_set_dimensions(s->avctx, s->width, s->height); | |
238 } | |
239 if(s->picture.data[0]) | |
240 s->avctx->release_buffer(s->avctx, &s->picture); | |
241 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ | |
242 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
243 return -1; | |
244 } | |
245 if(s->bpp == 8){ | |
246 /* make default grayscale pal */ | |
247 pal = s->picture.data[1]; | |
248 for(i = 0; i < 256; i++) | |
249 pal[i] = i * 0x010101; | |
250 } | |
4013 | 251 break; |
252 case TIFF_COMPR: | |
253 s->compr = value; | |
254 switch(s->compr){ | |
255 case TIFF_RAW: | |
256 case TIFF_PACKBITS: | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
257 case TIFF_LZW: |
4013 | 258 break; |
259 case TIFF_DEFLATE: | |
260 case TIFF_ADOBE_DEFLATE: | |
261 #ifdef CONFIG_ZLIB | |
262 break; | |
263 #else | |
264 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n"); | |
265 return -1; | |
266 #endif | |
267 case TIFF_G3: | |
268 av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n"); | |
269 return -1; | |
270 case TIFF_G4: | |
271 av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n"); | |
272 return -1; | |
273 case TIFF_CCITT_RLE: | |
274 av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n"); | |
275 return -1; | |
4184 | 276 case TIFF_JPEG: |
277 case TIFF_NEWJPEG: | |
278 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n"); | |
279 return -1; | |
4013 | 280 default: |
281 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr); | |
282 return -1; | |
283 } | |
284 break; | |
285 case TIFF_ROWSPERSTRIP: | |
4185 | 286 if(value < 1){ |
4013 | 287 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n"); |
288 return -1; | |
289 } | |
290 s->rps = value; | |
291 break; | |
292 case TIFF_STRIP_OFFS: | |
293 if(count == 1){ | |
294 s->stripdata = NULL; | |
295 s->stripoff = value; | |
296 }else | |
297 s->stripdata = start + off; | |
298 s->strips = count; | |
4405
48952197d91f
Some TIFFs do not set rows per strip for single strip.
kostya
parents:
4364
diff
changeset
|
299 if(s->strips == 1) s->rps = s->height; |
4013 | 300 s->sot = type; |
301 if(s->stripdata > end_buf){ | |
302 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n"); | |
303 return -1; | |
304 } | |
305 break; | |
306 case TIFF_STRIP_SIZE: | |
307 if(count == 1){ | |
308 s->stripsizes = NULL; | |
309 s->stripsize = value; | |
310 s->strips = 1; | |
311 }else{ | |
312 s->stripsizes = start + off; | |
313 } | |
314 s->strips = count; | |
315 if(s->stripsizes > end_buf){ | |
316 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n"); | |
317 return -1; | |
318 } | |
319 if(!pic->data[0]){ | |
320 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n"); | |
321 return -1; | |
322 } | |
323 /* now we have the data and may start decoding */ | |
324 stride = pic->linesize[0]; | |
325 dst = pic->data[0]; | |
326 for(i = 0; i < s->height; i += s->rps){ | |
327 if(s->stripsizes) | |
328 ssize = tget(&s->stripsizes, type, s->le); | |
329 else | |
330 ssize = s->stripsize; | |
331 | |
332 if(s->stripdata){ | |
333 soff = tget(&s->stripdata, s->sot, s->le); | |
334 }else | |
335 soff = s->stripoff; | |
336 src = start + soff; | |
337 if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0) | |
338 break; | |
339 dst += s->rps * stride; | |
340 } | |
341 break; | |
342 case TIFF_PREDICTOR: | |
343 if(!pic->data[0]){ | |
344 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n"); | |
345 return -1; | |
346 } | |
347 if(value == 2){ | |
4079 | 348 src = pic->data[0]; |
4013 | 349 stride = pic->linesize[0]; |
350 soff = s->bpp >> 3; | |
351 ssize = s->width * soff; | |
352 for(i = 0; i < s->height; i++) { | |
353 for(j = soff; j < ssize; j++) | |
354 src[j] += src[j - soff]; | |
355 src += stride; | |
356 } | |
357 } | |
358 break; | |
4186 | 359 case TIFF_INVERT: |
360 switch(value){ | |
361 case 0: | |
362 s->invert = 1; | |
363 break; | |
364 case 1: | |
365 s->invert = 0; | |
366 break; | |
4187
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
367 case 2: |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
368 case 3: |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
369 break; |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
370 default: |
46f12596304f
Print error message for unsupported mode (RGB planar,CMYK,YCrCb)
kostya
parents:
4186
diff
changeset
|
371 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
|
372 return -1; |
4186 | 373 } |
374 break; | |
375 case TIFF_PAL: | |
376 if(s->avctx->pix_fmt != PIX_FMT_PAL8){ | |
377 av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n"); | |
378 return -1; | |
379 } | |
380 pal = s->picture.data[1]; | |
4190 | 381 off = type_sizes[type]; |
4186 | 382 rp = buf; |
383 gp = buf + count / 3 * off; | |
384 bp = buf + count / 3 * off * 2; | |
4190 | 385 off = (type_sizes[type] - 1) << 3; |
4186 | 386 for(i = 0; i < count / 3; i++){ |
387 j = (tget(&rp, type, s->le) >> off) << 16; | |
388 j |= (tget(&gp, type, s->le) >> off) << 8; | |
389 j |= tget(&bp, type, s->le) >> off; | |
390 pal[i] = j; | |
391 } | |
4192 | 392 break; |
393 case TIFF_PLANAR: | |
394 if(value == 2){ | |
395 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n"); | |
396 return -1; | |
397 } | |
398 break; | |
4013 | 399 } |
400 return 0; | |
401 } | |
402 | |
403 static int decode_frame(AVCodecContext *avctx, | |
404 void *data, int *data_size, | |
405 uint8_t *buf, int buf_size) | |
406 { | |
407 TiffContext * const s = avctx->priv_data; | |
408 AVFrame *picture = data; | |
409 AVFrame * const p= (AVFrame*)&s->picture; | |
410 uint8_t *orig_buf = buf, *end_buf = buf + buf_size; | |
411 int id, le, off; | |
412 int i, entries; | |
413 | |
414 //parse image header | |
4364 | 415 id = AV_RL16(buf); buf += 2; |
4013 | 416 if(id == 0x4949) le = 1; |
417 else if(id == 0x4D4D) le = 0; | |
418 else{ | |
419 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n"); | |
420 return -1; | |
421 } | |
422 s->le = le; | |
4186 | 423 s->invert = 0; |
4013 | 424 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number |
425 // that further identifies the file as a TIFF file" | |
426 if(tget_short(&buf, le) != 42){ | |
427 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n"); | |
428 return -1; | |
429 } | |
430 /* parse image file directory */ | |
431 off = tget_long(&buf, le); | |
432 if(orig_buf + off + 14 >= end_buf){ | |
433 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n"); | |
434 return -1; | |
435 } | |
436 buf = orig_buf + off; | |
437 entries = tget_short(&buf, le); | |
438 for(i = 0; i < entries; i++){ | |
439 if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0) | |
440 return -1; | |
441 buf += 12; | |
442 } | |
443 | |
4186 | 444 if(s->invert){ |
445 uint8_t *src; | |
446 int j; | |
447 | |
448 src = s->picture.data[0]; | |
449 for(j = 0; j < s->height; j++){ | |
450 for(i = 0; i < s->picture.linesize[0]; i++) | |
451 src[i] = 255 - src[i]; | |
452 src += s->picture.linesize[0]; | |
453 } | |
454 } | |
4013 | 455 *picture= *(AVFrame*)&s->picture; |
456 *data_size = sizeof(AVPicture); | |
457 | |
458 return buf_size; | |
459 } | |
460 | |
461 static int tiff_init(AVCodecContext *avctx){ | |
462 TiffContext *s = avctx->priv_data; | |
463 | |
464 s->width = 0; | |
465 s->height = 0; | |
466 s->avctx = avctx; | |
467 avcodec_get_frame_defaults((AVFrame*)&s->picture); | |
468 avctx->coded_frame= (AVFrame*)&s->picture; | |
469 s->picture.data[0] = NULL; | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
470 ff_lzw_decode_open(&s->lzw); |
4013 | 471 |
472 return 0; | |
473 } | |
474 | |
475 static int tiff_end(AVCodecContext *avctx) | |
476 { | |
477 TiffContext * const s = avctx->priv_data; | |
478 | |
4080
f426c81afc9e
LZW decoder as separate module plus TIFF LZW support
kostya
parents:
4079
diff
changeset
|
479 ff_lzw_decode_close(&s->lzw); |
4013 | 480 if(s->picture.data[0]) |
481 avctx->release_buffer(avctx, &s->picture); | |
482 return 0; | |
483 } | |
484 | |
485 AVCodec tiff_decoder = { | |
486 "tiff", | |
487 CODEC_TYPE_VIDEO, | |
488 CODEC_ID_TIFF, | |
489 sizeof(TiffContext), | |
490 tiff_init, | |
491 NULL, | |
492 tiff_end, | |
493 decode_frame, | |
494 0, | |
495 NULL | |
496 }; |