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