00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00037 typedef struct TiffContext {
00038 AVCodecContext *avctx;
00039 AVFrame picture;
00040
00041 int width, height;
00042 unsigned int bpp;
00043 int le;
00044 int compr;
00045 int invert;
00046 int fax_opts;
00047 int predictor;
00048 int fill_order;
00049
00050 int strips, rps, sstype;
00051 int sot;
00052 const uint8_t* stripdata;
00053 const uint8_t* stripsizes;
00054 int stripsize, stripoff;
00055 LZWState *lzw;
00056 } TiffContext;
00057
00058 static int tget_short(const uint8_t **p, int le){
00059 int v = le ? AV_RL16(*p) : AV_RB16(*p);
00060 *p += 2;
00061 return v;
00062 }
00063
00064 static int tget_long(const uint8_t **p, int le){
00065 int v = le ? AV_RL32(*p) : AV_RB32(*p);
00066 *p += 4;
00067 return v;
00068 }
00069
00070 static int tget(const uint8_t **p, int type, int le){
00071 switch(type){
00072 case TIFF_BYTE : return *(*p)++;
00073 case TIFF_SHORT: return tget_short(p, le);
00074 case TIFF_LONG : return tget_long (p, le);
00075 default : return -1;
00076 }
00077 }
00078
00079 #if CONFIG_ZLIB
00080 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
00081 {
00082 z_stream zstream;
00083 int zret;
00084
00085 memset(&zstream, 0, sizeof(zstream));
00086 zstream.next_in = src;
00087 zstream.avail_in = size;
00088 zstream.next_out = dst;
00089 zstream.avail_out = *len;
00090 zret = inflateInit(&zstream);
00091 if (zret != Z_OK) {
00092 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00093 return zret;
00094 }
00095 zret = inflate(&zstream, Z_SYNC_FLUSH);
00096 inflateEnd(&zstream);
00097 *len = zstream.total_out;
00098 return zret == Z_STREAM_END ? Z_OK : zret;
00099 }
00100 #endif
00101
00102 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
00103 int c, line, pixels, code;
00104 const uint8_t *ssrc = src;
00105 int width = s->width * s->bpp >> 3;
00106 #if CONFIG_ZLIB
00107 uint8_t *zbuf; unsigned long outlen;
00108
00109 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
00110 int ret;
00111 outlen = width * lines;
00112 zbuf = av_malloc(outlen);
00113 ret = tiff_uncompress(zbuf, &outlen, src, size);
00114 if(ret != Z_OK){
00115 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
00116 av_free(zbuf);
00117 return -1;
00118 }
00119 src = zbuf;
00120 for(line = 0; line < lines; line++){
00121 memcpy(dst, src, width);
00122 dst += stride;
00123 src += width;
00124 }
00125 av_free(zbuf);
00126 return 0;
00127 }
00128 #endif
00129 if(s->compr == TIFF_LZW){
00130 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
00131 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
00132 return -1;
00133 }
00134 }
00135 if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
00136 int i, ret = 0;
00137 uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00138
00139 if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
00140 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
00141 return -1;
00142 }
00143 if(s->fax_opts & 2){
00144 av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
00145 av_free(src2);
00146 return -1;
00147 }
00148 if(!s->fill_order){
00149 memcpy(src2, src, size);
00150 }else{
00151 for(i = 0; i < size; i++)
00152 src2[i] = av_reverse[src[i]];
00153 }
00154 memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00155 switch(s->compr){
00156 case TIFF_CCITT_RLE:
00157 case TIFF_G3:
00158 case TIFF_G4:
00159 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
00160 break;
00161 }
00162 av_free(src2);
00163 return ret;
00164 }
00165 for(line = 0; line < lines; line++){
00166 if(src - ssrc > size){
00167 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
00168 return -1;
00169 }
00170 switch(s->compr){
00171 case TIFF_RAW:
00172 memcpy(dst, src, width);
00173 src += width;
00174 break;
00175 case TIFF_PACKBITS:
00176 for(pixels = 0; pixels < width;){
00177 code = (int8_t)*src++;
00178 if(code >= 0){
00179 code++;
00180 if(pixels + code > width){
00181 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
00182 return -1;
00183 }
00184 memcpy(dst + pixels, src, code);
00185 src += code;
00186 pixels += code;
00187 }else if(code != -128){
00188 code = (-code) + 1;
00189 if(pixels + code > width){
00190 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
00191 return -1;
00192 }
00193 c = *src++;
00194 memset(dst + pixels, c, code);
00195 pixels += code;
00196 }
00197 }
00198 break;
00199 case TIFF_LZW:
00200 pixels = ff_lzw_decode(s->lzw, dst, width);
00201 if(pixels < width){
00202 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
00203 return -1;
00204 }
00205 break;
00206 }
00207 dst += stride;
00208 }
00209 return 0;
00210 }
00211
00212
00213 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
00214 {
00215 int tag, type, count, off, value = 0;
00216 int i, j;
00217 uint32_t *pal;
00218 const uint8_t *rp, *gp, *bp;
00219
00220 tag = tget_short(&buf, s->le);
00221 type = tget_short(&buf, s->le);
00222 count = tget_long(&buf, s->le);
00223 off = tget_long(&buf, s->le);
00224
00225 if(count == 1){
00226 switch(type){
00227 case TIFF_BYTE:
00228 case TIFF_SHORT:
00229 buf -= 4;
00230 value = tget(&buf, type, s->le);
00231 buf = NULL;
00232 break;
00233 case TIFF_LONG:
00234 value = off;
00235 buf = NULL;
00236 break;
00237 case TIFF_STRING:
00238 if(count <= 4){
00239 buf -= 4;
00240 break;
00241 }
00242 default:
00243 value = -1;
00244 buf = start + off;
00245 }
00246 }else if(type_sizes[type] * count <= 4){
00247 buf -= 4;
00248 }else{
00249 buf = start + off;
00250 }
00251
00252 if(buf && (buf < start || buf > end_buf)){
00253 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00254 return -1;
00255 }
00256
00257 switch(tag){
00258 case TIFF_WIDTH:
00259 s->width = value;
00260 break;
00261 case TIFF_HEIGHT:
00262 s->height = value;
00263 break;
00264 case TIFF_BPP:
00265 if(count == 1) s->bpp = value;
00266 else{
00267 switch(type){
00268 case TIFF_BYTE:
00269 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
00270 break;
00271 case TIFF_SHORT:
00272 case TIFF_LONG:
00273 s->bpp = 0;
00274 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
00275 break;
00276 default:
00277 s->bpp = -1;
00278 }
00279 }
00280 if(count > 4){
00281 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
00282 return -1;
00283 }
00284 switch(s->bpp*10 + count){
00285 case 11:
00286 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
00287 break;
00288 case 81:
00289 s->avctx->pix_fmt = PIX_FMT_PAL8;
00290 break;
00291 case 243:
00292 s->avctx->pix_fmt = PIX_FMT_RGB24;
00293 break;
00294 case 161:
00295 s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
00296 break;
00297 case 324:
00298 s->avctx->pix_fmt = PIX_FMT_RGBA;
00299 break;
00300 case 483:
00301 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
00302 break;
00303 default:
00304 av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
00305 return -1;
00306 }
00307 if(s->width != s->avctx->width || s->height != s->avctx->height){
00308 if(avcodec_check_dimensions(s->avctx, s->width, s->height))
00309 return -1;
00310 avcodec_set_dimensions(s->avctx, s->width, s->height);
00311 }
00312 if(s->picture.data[0])
00313 s->avctx->release_buffer(s->avctx, &s->picture);
00314 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
00315 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00316 return -1;
00317 }
00318 if(s->bpp == 8){
00319
00320 pal = (uint32_t *) s->picture.data[1];
00321 for(i = 0; i < 256; i++)
00322 pal[i] = i * 0x010101;
00323 }
00324 break;
00325 case TIFF_COMPR:
00326 s->compr = value;
00327 s->predictor = 0;
00328 switch(s->compr){
00329 case TIFF_RAW:
00330 case TIFF_PACKBITS:
00331 case TIFF_LZW:
00332 case TIFF_CCITT_RLE:
00333 break;
00334 case TIFF_G3:
00335 case TIFF_G4:
00336 s->fax_opts = 0;
00337 break;
00338 case TIFF_DEFLATE:
00339 case TIFF_ADOBE_DEFLATE:
00340 #if CONFIG_ZLIB
00341 break;
00342 #else
00343 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
00344 return -1;
00345 #endif
00346 case TIFF_JPEG:
00347 case TIFF_NEWJPEG:
00348 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
00349 return -1;
00350 default:
00351 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
00352 return -1;
00353 }
00354 break;
00355 case TIFF_ROWSPERSTRIP:
00356 if(type == TIFF_LONG && value == -1)
00357 value = s->avctx->height;
00358 if(value < 1){
00359 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
00360 return -1;
00361 }
00362 s->rps = value;
00363 break;
00364 case TIFF_STRIP_OFFS:
00365 if(count == 1){
00366 s->stripdata = NULL;
00367 s->stripoff = value;
00368 }else
00369 s->stripdata = start + off;
00370 s->strips = count;
00371 if(s->strips == 1) s->rps = s->height;
00372 s->sot = type;
00373 if(s->stripdata > end_buf){
00374 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00375 return -1;
00376 }
00377 break;
00378 case TIFF_STRIP_SIZE:
00379 if(count == 1){
00380 s->stripsizes = NULL;
00381 s->stripsize = value;
00382 s->strips = 1;
00383 }else{
00384 s->stripsizes = start + off;
00385 }
00386 s->strips = count;
00387 s->sstype = type;
00388 if(s->stripsizes > end_buf){
00389 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00390 return -1;
00391 }
00392 break;
00393 case TIFF_PREDICTOR:
00394 s->predictor = value;
00395 break;
00396 case TIFF_INVERT:
00397 switch(value){
00398 case 0:
00399 s->invert = 1;
00400 break;
00401 case 1:
00402 s->invert = 0;
00403 break;
00404 case 2:
00405 case 3:
00406 break;
00407 default:
00408 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
00409 return -1;
00410 }
00411 break;
00412 case TIFF_FILL_ORDER:
00413 if(value < 1 || value > 2){
00414 av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
00415 value = 1;
00416 }
00417 s->fill_order = value - 1;
00418 break;
00419 case TIFF_PAL:
00420 if(s->avctx->pix_fmt != PIX_FMT_PAL8){
00421 av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
00422 return -1;
00423 }
00424 pal = (uint32_t *) s->picture.data[1];
00425 off = type_sizes[type];
00426 rp = buf;
00427 gp = buf + count / 3 * off;
00428 bp = buf + count / 3 * off * 2;
00429 off = (type_sizes[type] - 1) << 3;
00430 for(i = 0; i < count / 3; i++){
00431 j = (tget(&rp, type, s->le) >> off) << 16;
00432 j |= (tget(&gp, type, s->le) >> off) << 8;
00433 j |= tget(&bp, type, s->le) >> off;
00434 pal[i] = j;
00435 }
00436 break;
00437 case TIFF_PLANAR:
00438 if(value == 2){
00439 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
00440 return -1;
00441 }
00442 break;
00443 case TIFF_T4OPTIONS:
00444 if(s->compr == TIFF_G3)
00445 s->fax_opts = value;
00446 break;
00447 case TIFF_T6OPTIONS:
00448 if(s->compr == TIFF_G4)
00449 s->fax_opts = value;
00450 break;
00451 }
00452 return 0;
00453 }
00454
00455 static int decode_frame(AVCodecContext *avctx,
00456 void *data, int *data_size,
00457 AVPacket *avpkt)
00458 {
00459 const uint8_t *buf = avpkt->data;
00460 int buf_size = avpkt->size;
00461 TiffContext * const s = avctx->priv_data;
00462 AVFrame *picture = data;
00463 AVFrame * const p= (AVFrame*)&s->picture;
00464 const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
00465 int id, le, off;
00466 int i, j, entries;
00467 int stride, soff, ssize;
00468 uint8_t *dst;
00469
00470
00471 id = AV_RL16(buf); buf += 2;
00472 if(id == 0x4949) le = 1;
00473 else if(id == 0x4D4D) le = 0;
00474 else{
00475 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
00476 return -1;
00477 }
00478 s->le = le;
00479 s->invert = 0;
00480 s->compr = TIFF_RAW;
00481 s->fill_order = 0;
00482
00483
00484 if(tget_short(&buf, le) != 42){
00485 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
00486 return -1;
00487 }
00488
00489 off = tget_long(&buf, le);
00490 if(orig_buf + off + 14 >= end_buf){
00491 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
00492 return -1;
00493 }
00494 buf = orig_buf + off;
00495 entries = tget_short(&buf, le);
00496 for(i = 0; i < entries; i++){
00497 if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
00498 return -1;
00499 buf += 12;
00500 }
00501 if(!s->stripdata && !s->stripoff){
00502 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
00503 return -1;
00504 }
00505
00506 if(!p->data[0]){
00507 s->bpp = 1;
00508 avctx->pix_fmt = PIX_FMT_MONOBLACK;
00509 if(s->width != s->avctx->width || s->height != s->avctx->height){
00510 if(avcodec_check_dimensions(s->avctx, s->width, s->height))
00511 return -1;
00512 avcodec_set_dimensions(s->avctx, s->width, s->height);
00513 }
00514 if(s->picture.data[0])
00515 s->avctx->release_buffer(s->avctx, &s->picture);
00516 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
00517 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00518 return -1;
00519 }
00520 }
00521 if(s->strips == 1 && !s->stripsize){
00522 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
00523 s->stripsize = buf_size - s->stripoff;
00524 }
00525 stride = p->linesize[0];
00526 dst = p->data[0];
00527 for(i = 0; i < s->height; i += s->rps){
00528 if(s->stripsizes)
00529 ssize = tget(&s->stripsizes, s->sstype, s->le);
00530 else
00531 ssize = s->stripsize;
00532
00533 if(s->stripdata){
00534 soff = tget(&s->stripdata, s->sot, s->le);
00535 }else
00536 soff = s->stripoff;
00537 if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
00538 break;
00539 dst += s->rps * stride;
00540 }
00541 if(s->predictor == 2){
00542 dst = p->data[0];
00543 soff = s->bpp >> 3;
00544 ssize = s->width * soff;
00545 for(i = 0; i < s->height; i++) {
00546 for(j = soff; j < ssize; j++)
00547 dst[j] += dst[j - soff];
00548 dst += stride;
00549 }
00550 }
00551
00552 if(s->invert){
00553 uint8_t *src;
00554 int j;
00555
00556 src = s->picture.data[0];
00557 for(j = 0; j < s->height; j++){
00558 for(i = 0; i < s->picture.linesize[0]; i++)
00559 src[i] = 255 - src[i];
00560 src += s->picture.linesize[0];
00561 }
00562 }
00563 *picture= *(AVFrame*)&s->picture;
00564 *data_size = sizeof(AVPicture);
00565
00566 return buf_size;
00567 }
00568
00569 static av_cold int tiff_init(AVCodecContext *avctx){
00570 TiffContext *s = avctx->priv_data;
00571
00572 s->width = 0;
00573 s->height = 0;
00574 s->avctx = avctx;
00575 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00576 avctx->coded_frame= (AVFrame*)&s->picture;
00577 ff_lzw_decode_open(&s->lzw);
00578 ff_ccitt_unpack_init();
00579
00580 return 0;
00581 }
00582
00583 static av_cold int tiff_end(AVCodecContext *avctx)
00584 {
00585 TiffContext * const s = avctx->priv_data;
00586
00587 ff_lzw_decode_close(&s->lzw);
00588 if(s->picture.data[0])
00589 avctx->release_buffer(avctx, &s->picture);
00590 return 0;
00591 }
00592
00593 AVCodec tiff_decoder = {
00594 "tiff",
00595 AVMEDIA_TYPE_VIDEO,
00596 CODEC_ID_TIFF,
00597 sizeof(TiffContext),
00598 tiff_init,
00599 NULL,
00600 tiff_end,
00601 decode_frame,
00602 CODEC_CAP_DR1,
00603 NULL,
00604 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00605 };