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