00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include "avcodec.h"
00028 #if CONFIG_ZLIB
00029 #include <zlib.h>
00030 #endif
00031 #include "lzw.h"
00032 #include "tiff.h"
00033 #include "faxcompr.h"
00034 #include "libavutil/common.h"
00035 #include "libavutil/intreadwrite.h"
00036 #include "libavutil/imgutils.h"
00037
00038 typedef struct TiffContext {
00039 AVCodecContext *avctx;
00040 AVFrame picture;
00041
00042 int width, height;
00043 unsigned int bpp, bppcount;
00044 uint32_t palette[256];
00045 int palette_is_set;
00046 int le;
00047 enum TiffCompr compr;
00048 int invert;
00049 int fax_opts;
00050 int predictor;
00051 int fill_order;
00052
00053 int strips, rps, sstype;
00054 int sot;
00055 const uint8_t *stripdata;
00056 const uint8_t *stripsizes;
00057 int stripsize, stripoff;
00058 LZWState *lzw;
00059 } TiffContext;
00060
00061 static unsigned tget_short(const uint8_t **p, int le)
00062 {
00063 unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
00064 *p += 2;
00065 return v;
00066 }
00067
00068 static unsigned tget_long(const uint8_t **p, int le)
00069 {
00070 unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
00071 *p += 4;
00072 return v;
00073 }
00074
00075 static unsigned tget(const uint8_t **p, int type, int le)
00076 {
00077 switch (type) {
00078 case TIFF_BYTE : return *(*p)++;
00079 case TIFF_SHORT: return tget_short(p, le);
00080 case TIFF_LONG : return tget_long(p, le);
00081 default : return UINT_MAX;
00082 }
00083 }
00084
00085 #if CONFIG_ZLIB
00086 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
00087 int size)
00088 {
00089 z_stream zstream = { 0 };
00090 int zret;
00091
00092 zstream.next_in = src;
00093 zstream.avail_in = size;
00094 zstream.next_out = dst;
00095 zstream.avail_out = *len;
00096 zret = inflateInit(&zstream);
00097 if (zret != Z_OK) {
00098 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00099 return zret;
00100 }
00101 zret = inflate(&zstream, Z_SYNC_FLUSH);
00102 inflateEnd(&zstream);
00103 *len = zstream.total_out;
00104 return zret == Z_STREAM_END ? Z_OK : zret;
00105 }
00106 #endif
00107
00108 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
00109 int usePtr, const uint8_t *src,
00110 uint8_t c, int width, int offset)
00111 {
00112 switch (bpp) {
00113 case 1:
00114 while (--width >= 0) {
00115 dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
00116 dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
00117 dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
00118 dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
00119 dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
00120 dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
00121 dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
00122 dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
00123 }
00124 break;
00125 case 2:
00126 while (--width >= 0) {
00127 dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
00128 dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
00129 dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
00130 dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
00131 }
00132 break;
00133 case 4:
00134 while (--width >= 0) {
00135 dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
00136 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
00137 }
00138 break;
00139 default:
00140 if (usePtr) {
00141 memcpy(dst + offset, src, width);
00142 } else {
00143 memset(dst + offset, c, width);
00144 }
00145 }
00146 }
00147
00148 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
00149 const uint8_t *src, int size, int lines)
00150 {
00151 int c, line, pixels, code;
00152 const uint8_t *ssrc = src;
00153 int width = ((s->width * s->bpp) + 7) >> 3;
00154
00155 if (size <= 0)
00156 return AVERROR_INVALIDDATA;
00157
00158 #if CONFIG_ZLIB
00159 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00160 uint8_t *zbuf;
00161 unsigned long outlen;
00162 int ret;
00163 outlen = width * lines;
00164 zbuf = av_malloc(outlen);
00165 if (!zbuf)
00166 return AVERROR(ENOMEM);
00167 ret = tiff_uncompress(zbuf, &outlen, src, size);
00168 if (ret != Z_OK) {
00169 av_log(s->avctx, AV_LOG_ERROR,
00170 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
00171 (unsigned long)width * lines, ret);
00172 av_free(zbuf);
00173 return -1;
00174 }
00175 src = zbuf;
00176 for (line = 0; line < lines; line++) {
00177 if(s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8){
00178 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
00179 }else{
00180 memcpy(dst, src, width);
00181 }
00182 dst += stride;
00183 src += width;
00184 }
00185 av_free(zbuf);
00186 return 0;
00187 }
00188 #endif
00189 if (s->compr == TIFF_LZW) {
00190 if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
00191 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
00192 return -1;
00193 }
00194 }
00195 if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
00196 || s->compr == TIFF_G4) {
00197 int i, ret = 0;
00198 uint8_t *src2 = av_malloc((unsigned)size +
00199 FF_INPUT_BUFFER_PADDING_SIZE);
00200
00201 if (!src2) {
00202 av_log(s->avctx, AV_LOG_ERROR,
00203 "Error allocating temporary buffer\n");
00204 return AVERROR(ENOMEM);
00205 }
00206 if (s->fax_opts & 2) {
00207 av_log(s->avctx, AV_LOG_ERROR,
00208 "Uncompressed fax mode is not supported (yet)\n");
00209 av_free(src2);
00210 return -1;
00211 }
00212 if (!s->fill_order) {
00213 memcpy(src2, src, size);
00214 } else {
00215 for (i = 0; i < size; i++)
00216 src2[i] = av_reverse[src[i]];
00217 }
00218 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00219 switch (s->compr) {
00220 case TIFF_CCITT_RLE:
00221 case TIFF_G3:
00222 case TIFF_G4:
00223 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
00224 s->compr, s->fax_opts);
00225 break;
00226 }
00227 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
00228 for (line = 0; line < lines; line++) {
00229 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
00230 dst += stride;
00231 }
00232 av_free(src2);
00233 return ret;
00234 }
00235 for (line = 0; line < lines; line++) {
00236 if (src - ssrc > size) {
00237 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
00238 return -1;
00239 }
00240 switch (s->compr) {
00241 case TIFF_RAW:
00242 if (ssrc + size - src < width)
00243 return AVERROR_INVALIDDATA;
00244 if (!s->fill_order) {
00245 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
00246 dst, 1, src, 0, width, 0);
00247 } else {
00248 int i;
00249 for (i = 0; i < width; i++)
00250 dst[i] = av_reverse[src[i]];
00251 }
00252 src += width;
00253 break;
00254 case TIFF_PACKBITS:
00255 for (pixels = 0; pixels < width;) {
00256 if (ssrc + size - src < 2) {
00257 av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
00258 return AVERROR_INVALIDDATA;
00259 }
00260 code = (int8_t) * src++;
00261 if (code >= 0) {
00262 code++;
00263 if (pixels + code > width) {
00264 av_log(s->avctx, AV_LOG_ERROR,
00265 "Copy went out of bounds\n");
00266 return -1;
00267 }
00268 if (ssrc + size - src < code) {
00269 av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
00270 return AVERROR_INVALIDDATA;
00271 }
00272 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
00273 dst, 1, src, 0, code, pixels);
00274 src += code;
00275 pixels += code;
00276 } else if (code != -128) {
00277 code = (-code) + 1;
00278 if (pixels + code > width) {
00279 av_log(s->avctx, AV_LOG_ERROR,
00280 "Run went out of bounds\n");
00281 return -1;
00282 }
00283 c = *src++;
00284 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
00285 dst, 0, NULL, c, code, pixels);
00286 pixels += code;
00287 }
00288 }
00289 break;
00290 case TIFF_LZW:
00291 pixels = ff_lzw_decode(s->lzw, dst, width);
00292 if (pixels < width) {
00293 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
00294 pixels, width);
00295 return -1;
00296 }
00297 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
00298 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
00299 break;
00300 }
00301 dst += stride;
00302 }
00303 return 0;
00304 }
00305
00306 static int init_image(TiffContext *s)
00307 {
00308 int i, ret;
00309 uint32_t *pal;
00310
00311 switch (s->bpp * 10 + s->bppcount) {
00312 case 11:
00313 if (!s->palette_is_set) {
00314 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
00315 break;
00316 }
00317 case 21:
00318 case 41:
00319 case 81:
00320 s->avctx->pix_fmt = PIX_FMT_PAL8;
00321 break;
00322 case 243:
00323 s->avctx->pix_fmt = PIX_FMT_RGB24;
00324 break;
00325 case 161:
00326 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
00327 break;
00328 case 162:
00329 s->avctx->pix_fmt = PIX_FMT_GRAY8A;
00330 break;
00331 case 324:
00332 s->avctx->pix_fmt = PIX_FMT_RGBA;
00333 break;
00334 case 483:
00335 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
00336 break;
00337 case 644:
00338 s->avctx->pix_fmt = s->le ? PIX_FMT_RGBA64LE : PIX_FMT_RGBA64BE;
00339 break;
00340 default:
00341 av_log(s->avctx, AV_LOG_ERROR,
00342 "This format is not supported (bpp=%d, bppcount=%d)\n",
00343 s->bpp, s->bppcount);
00344 return AVERROR_INVALIDDATA;
00345 }
00346 if (s->width != s->avctx->width || s->height != s->avctx->height) {
00347 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
00348 return ret;
00349 avcodec_set_dimensions(s->avctx, s->width, s->height);
00350 }
00351 if (s->picture.data[0])
00352 s->avctx->release_buffer(s->avctx, &s->picture);
00353 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
00354 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00355 return ret;
00356 }
00357 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
00358 if (s->palette_is_set) {
00359 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
00360 } else {
00361
00362 pal = (uint32_t *) s->picture.data[1];
00363 for (i = 0; i < 1<<s->bpp; i++)
00364 pal[i] = 0xFF << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
00365 }
00366 }
00367 return 0;
00368 }
00369
00370 static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
00371 const uint8_t *buf, const uint8_t *end_buf)
00372 {
00373 unsigned tag, type, count, off, value = 0;
00374 int i, j;
00375 uint32_t *pal;
00376 const uint8_t *rp, *gp, *bp;
00377
00378 if (end_buf - buf < 12)
00379 return -1;
00380 tag = tget_short(&buf, s->le);
00381 type = tget_short(&buf, s->le);
00382 count = tget_long(&buf, s->le);
00383 off = tget_long(&buf, s->le);
00384
00385 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
00386 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
00387 type);
00388 return 0;
00389 }
00390
00391 if (count == 1) {
00392 switch (type) {
00393 case TIFF_BYTE:
00394 case TIFF_SHORT:
00395 buf -= 4;
00396 value = tget(&buf, type, s->le);
00397 buf = NULL;
00398 break;
00399 case TIFF_LONG:
00400 value = off;
00401 buf = NULL;
00402 break;
00403 case TIFF_STRING:
00404 if (count <= 4) {
00405 buf -= 4;
00406 break;
00407 }
00408 default:
00409 value = UINT_MAX;
00410 buf = start + off;
00411 }
00412 } else {
00413 if (count <= 4 && type_sizes[type] * count <= 4) {
00414 buf -= 4;
00415 } else {
00416 buf = start + off;
00417 }
00418 }
00419
00420 if (buf && (buf < start || buf > end_buf)) {
00421 av_log(s->avctx, AV_LOG_ERROR,
00422 "Tag referencing position outside the image\n");
00423 return -1;
00424 }
00425
00426 switch (tag) {
00427 case TIFF_WIDTH:
00428 s->width = value;
00429 break;
00430 case TIFF_HEIGHT:
00431 s->height = value;
00432 break;
00433 case TIFF_BPP:
00434 s->bppcount = count;
00435 if (count > 4) {
00436 av_log(s->avctx, AV_LOG_ERROR,
00437 "This format is not supported (bpp=%d, %d components)\n",
00438 s->bpp, count);
00439 return -1;
00440 }
00441 if (count == 1)
00442 s->bpp = value;
00443 else {
00444 switch (type) {
00445 case TIFF_BYTE:
00446 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
00447 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
00448 break;
00449 case TIFF_SHORT:
00450 case TIFF_LONG:
00451 s->bpp = 0;
00452 for (i = 0; i < count && buf < end_buf; i++)
00453 s->bpp += tget(&buf, type, s->le);
00454 break;
00455 default:
00456 s->bpp = -1;
00457 }
00458 }
00459 break;
00460 case TIFF_SAMPLES_PER_PIXEL:
00461 if (count != 1) {
00462 av_log(s->avctx, AV_LOG_ERROR,
00463 "Samples per pixel requires a single value, many provided\n");
00464 return AVERROR_INVALIDDATA;
00465 }
00466 if (s->bppcount == 1)
00467 s->bpp *= value;
00468 s->bppcount = value;
00469 break;
00470 case TIFF_COMPR:
00471 s->compr = value;
00472 s->predictor = 0;
00473 switch (s->compr) {
00474 case TIFF_RAW:
00475 case TIFF_PACKBITS:
00476 case TIFF_LZW:
00477 case TIFF_CCITT_RLE:
00478 break;
00479 case TIFF_G3:
00480 case TIFF_G4:
00481 s->fax_opts = 0;
00482 break;
00483 case TIFF_DEFLATE:
00484 case TIFF_ADOBE_DEFLATE:
00485 #if CONFIG_ZLIB
00486 break;
00487 #else
00488 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
00489 return -1;
00490 #endif
00491 case TIFF_JPEG:
00492 case TIFF_NEWJPEG:
00493 av_log(s->avctx, AV_LOG_ERROR,
00494 "JPEG compression is not supported\n");
00495 return -1;
00496 default:
00497 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
00498 s->compr);
00499 return -1;
00500 }
00501 break;
00502 case TIFF_ROWSPERSTRIP:
00503 if (type == TIFF_LONG && value == UINT_MAX)
00504 value = s->avctx->height;
00505 if (value < 1) {
00506 av_log(s->avctx, AV_LOG_ERROR,
00507 "Incorrect value of rows per strip\n");
00508 return -1;
00509 }
00510 s->rps = value;
00511 break;
00512 case TIFF_STRIP_OFFS:
00513 if (count == 1) {
00514 s->stripdata = NULL;
00515 s->stripoff = value;
00516 } else
00517 s->stripdata = start + off;
00518 s->strips = count;
00519 if (s->strips == 1)
00520 s->rps = s->height;
00521 s->sot = type;
00522 if (s->stripdata > end_buf) {
00523 av_log(s->avctx, AV_LOG_ERROR,
00524 "Tag referencing position outside the image\n");
00525 return -1;
00526 }
00527 break;
00528 case TIFF_STRIP_SIZE:
00529 if (count == 1) {
00530 s->stripsizes = NULL;
00531 s->stripsize = value;
00532 s->strips = 1;
00533 } else {
00534 s->stripsizes = start + off;
00535 }
00536 s->strips = count;
00537 s->sstype = type;
00538 if (s->stripsizes > end_buf) {
00539 av_log(s->avctx, AV_LOG_ERROR,
00540 "Tag referencing position outside the image\n");
00541 return -1;
00542 }
00543 break;
00544 case TIFF_TILE_BYTE_COUNTS:
00545 case TIFF_TILE_LENGTH:
00546 case TIFF_TILE_OFFSETS:
00547 case TIFF_TILE_WIDTH:
00548 av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
00549 return AVERROR_PATCHWELCOME;
00550 break;
00551 case TIFF_PREDICTOR:
00552 s->predictor = value;
00553 break;
00554 case TIFF_INVERT:
00555 switch (value) {
00556 case 0:
00557 s->invert = 1;
00558 break;
00559 case 1:
00560 s->invert = 0;
00561 break;
00562 case 2:
00563 case 3:
00564 break;
00565 default:
00566 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
00567 value);
00568 return -1;
00569 }
00570 break;
00571 case TIFF_FILL_ORDER:
00572 if (value < 1 || value > 2) {
00573 av_log(s->avctx, AV_LOG_ERROR,
00574 "Unknown FillOrder value %d, trying default one\n", value);
00575 value = 1;
00576 }
00577 s->fill_order = value - 1;
00578 break;
00579 case TIFF_PAL:
00580 pal = (uint32_t *) s->palette;
00581 off = type_sizes[type];
00582 if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
00583 return -1;
00584 rp = buf;
00585 gp = buf + count / 3 * off;
00586 bp = buf + count / 3 * off * 2;
00587 off = (type_sizes[type] - 1) << 3;
00588 for (i = 0; i < count / 3; i++) {
00589 j = 0xff << 24;
00590 j |= (tget(&rp, type, s->le) >> off) << 16;
00591 j |= (tget(&gp, type, s->le) >> off) << 8;
00592 j |= tget(&bp, type, s->le) >> off;
00593 pal[i] = j;
00594 }
00595 s->palette_is_set = 1;
00596 break;
00597 case TIFF_PLANAR:
00598 if (value == 2) {
00599 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
00600 return -1;
00601 }
00602 break;
00603 case TIFF_T4OPTIONS:
00604 if (s->compr == TIFF_G3)
00605 s->fax_opts = value;
00606 break;
00607 case TIFF_T6OPTIONS:
00608 if (s->compr == TIFF_G4)
00609 s->fax_opts = value;
00610 break;
00611 default:
00612 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
00613 tag, tag);
00614 }
00615 return 0;
00616 }
00617
00618 static int decode_frame(AVCodecContext *avctx,
00619 void *data, int *data_size, AVPacket *avpkt)
00620 {
00621 const uint8_t *buf = avpkt->data;
00622 int buf_size = avpkt->size;
00623 TiffContext *const s = avctx->priv_data;
00624 AVFrame *picture = data;
00625 AVFrame *const p = &s->picture;
00626 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
00627 unsigned off;
00628 int id, le, ret;
00629 int i, j, entries;
00630 int stride;
00631 unsigned soff, ssize;
00632 uint8_t *dst;
00633
00634
00635 if (end_buf - buf < 8)
00636 return AVERROR_INVALIDDATA;
00637 id = AV_RL16(buf);
00638 buf += 2;
00639 if (id == 0x4949)
00640 le = 1;
00641 else if (id == 0x4D4D)
00642 le = 0;
00643 else {
00644 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
00645 return -1;
00646 }
00647 s->le = le;
00648 s->invert = 0;
00649 s->compr = TIFF_RAW;
00650 s->fill_order = 0;
00651
00652
00653 if (tget_short(&buf, le) != 42) {
00654 av_log(avctx, AV_LOG_ERROR,
00655 "The answer to life, universe and everything is not correct!\n");
00656 return -1;
00657 }
00658
00659 s->stripsizes = s->stripdata = NULL;
00660
00661 off = tget_long(&buf, le);
00662 if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
00663 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
00664 return AVERROR_INVALIDDATA;
00665 }
00666 buf = orig_buf + off;
00667 entries = tget_short(&buf, le);
00668 for (i = 0; i < entries; i++) {
00669 if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
00670 return -1;
00671 buf += 12;
00672 }
00673 if (!s->stripdata && !s->stripoff) {
00674 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
00675 return -1;
00676 }
00677
00678 if ((ret = init_image(s)) < 0)
00679 return ret;
00680
00681 if (s->strips == 1 && !s->stripsize) {
00682 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
00683 s->stripsize = buf_size - s->stripoff;
00684 }
00685 stride = p->linesize[0];
00686 dst = p->data[0];
00687 for (i = 0; i < s->height; i += s->rps) {
00688 if (s->stripsizes) {
00689 if (s->stripsizes >= end_buf)
00690 return AVERROR_INVALIDDATA;
00691 ssize = tget(&s->stripsizes, s->sstype, s->le);
00692 } else
00693 ssize = s->stripsize;
00694
00695 if (s->stripdata) {
00696 if (s->stripdata >= end_buf)
00697 return AVERROR_INVALIDDATA;
00698 soff = tget(&s->stripdata, s->sot, s->le);
00699 } else
00700 soff = s->stripoff;
00701
00702 if (soff > buf_size || ssize > buf_size - soff) {
00703 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
00704 return -1;
00705 }
00706 if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
00707 FFMIN(s->rps, s->height - i)) < 0)
00708 break;
00709 dst += s->rps * stride;
00710 }
00711 if (s->predictor == 2) {
00712 dst = p->data[0];
00713 soff = s->bpp >> 3;
00714 ssize = s->width * soff;
00715 if (s->avctx->pix_fmt == PIX_FMT_RGB48LE ||
00716 s->avctx->pix_fmt == PIX_FMT_RGBA64LE) {
00717 for (i = 0; i < s->height; i++) {
00718 for (j = soff; j < ssize; j += 2)
00719 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
00720 dst += stride;
00721 }
00722 } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE ||
00723 s->avctx->pix_fmt == PIX_FMT_RGBA64BE) {
00724 for (i = 0; i < s->height; i++) {
00725 for (j = soff; j < ssize; j += 2)
00726 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
00727 dst += stride;
00728 }
00729 } else {
00730 for (i = 0; i < s->height; i++) {
00731 for (j = soff; j < ssize; j++)
00732 dst[j] += dst[j - soff];
00733 dst += stride;
00734 }
00735 }
00736 }
00737
00738 if (s->invert) {
00739 dst = s->picture.data[0];
00740 for (i = 0; i < s->height; i++) {
00741 for (j = 0; j < s->picture.linesize[0]; j++)
00742 dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
00743 dst += s->picture.linesize[0];
00744 }
00745 }
00746 *picture = s->picture;
00747 *data_size = sizeof(AVPicture);
00748
00749 return buf_size;
00750 }
00751
00752 static av_cold int tiff_init(AVCodecContext *avctx)
00753 {
00754 TiffContext *s = avctx->priv_data;
00755
00756 s->width = 0;
00757 s->height = 0;
00758 s->avctx = avctx;
00759 avcodec_get_frame_defaults(&s->picture);
00760 avctx->coded_frame = &s->picture;
00761 ff_lzw_decode_open(&s->lzw);
00762 ff_ccitt_unpack_init();
00763
00764 return 0;
00765 }
00766
00767 static av_cold int tiff_end(AVCodecContext *avctx)
00768 {
00769 TiffContext *const s = avctx->priv_data;
00770
00771 ff_lzw_decode_close(&s->lzw);
00772 if (s->picture.data[0])
00773 avctx->release_buffer(avctx, &s->picture);
00774 return 0;
00775 }
00776
00777 AVCodec ff_tiff_decoder = {
00778 .name = "tiff",
00779 .type = AVMEDIA_TYPE_VIDEO,
00780 .id = CODEC_ID_TIFF,
00781 .priv_data_size = sizeof(TiffContext),
00782 .init = tiff_init,
00783 .close = tiff_end,
00784 .decode = decode_frame,
00785 .capabilities = CODEC_CAP_DR1,
00786 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00787 };