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 #include "bytestream.h"
00029 #include "config.h"
00030 #if CONFIG_ZLIB
00031 #include <zlib.h>
00032 #endif
00033 #include "lzw.h"
00034 #include "tiff.h"
00035 #include "tiff_data.h"
00036 #include "faxcompr.h"
00037 #include "libavutil/common.h"
00038 #include "libavutil/intreadwrite.h"
00039 #include "libavutil/imgutils.h"
00040 #include "libavutil/avstring.h"
00041
00042 typedef struct TiffContext {
00043 AVCodecContext *avctx;
00044 AVFrame picture;
00045 GetByteContext gb;
00046
00047 int width, height;
00048 unsigned int bpp, bppcount;
00049 uint32_t palette[256];
00050 int palette_is_set;
00051 int le;
00052 enum TiffCompr compr;
00053 int invert;
00054 int fax_opts;
00055 int predictor;
00056 int fill_order;
00057
00058 int strips, rps, sstype;
00059 int sot;
00060 int stripsizesoff, stripsize, stripoff, strippos;
00061 LZWState *lzw;
00062
00063 int geotag_count;
00064 TiffGeoTag *geotags;
00065 } TiffContext;
00066
00067 static unsigned tget_short(GetByteContext *gb, int le)
00068 {
00069 unsigned v = le ? bytestream2_get_le16u(gb) : bytestream2_get_be16u(gb);
00070 return v;
00071 }
00072
00073 static unsigned tget_long(GetByteContext *gb, int le)
00074 {
00075 unsigned v = le ? bytestream2_get_le32u(gb) : bytestream2_get_be32u(gb);
00076 return v;
00077 }
00078
00079 static double tget_double(GetByteContext *gb, int le)
00080 {
00081 av_alias64 i = { .u64 = le ? bytestream2_get_le64u(gb) : bytestream2_get_be64u(gb)};
00082 return i.f64;
00083 }
00084
00085 static unsigned tget(GetByteContext *gb, int type, int le)
00086 {
00087 switch (type) {
00088 case TIFF_BYTE : return bytestream2_get_byteu(gb);
00089 case TIFF_SHORT: return tget_short(gb, le);
00090 case TIFF_LONG : return tget_long(gb, le);
00091 default : return UINT_MAX;
00092 }
00093 }
00094
00095 static void free_geotags(TiffContext *const s)
00096 {
00097 int i;
00098 for (i = 0; i < s->geotag_count; i++) {
00099 if (s->geotags[i].val)
00100 av_freep(&s->geotags[i].val);
00101 }
00102 av_freep(&s->geotags);
00103 }
00104
00105 #define RET_GEOKEY(TYPE, array, element)\
00106 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
00107 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
00108 return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
00109
00110 static const char *get_geokey_name(int key)
00111 {
00112 RET_GEOKEY(VERT, vert, name);
00113 RET_GEOKEY(PROJ, proj, name);
00114 RET_GEOKEY(GEOG, geog, name);
00115 RET_GEOKEY(CONF, conf, name);
00116
00117 return NULL;
00118 }
00119
00120 static int get_geokey_type(int key)
00121 {
00122 RET_GEOKEY(VERT, vert, type);
00123 RET_GEOKEY(PROJ, proj, type);
00124 RET_GEOKEY(GEOG, geog, type);
00125 RET_GEOKEY(CONF, conf, type);
00126
00127 return AVERROR_INVALIDDATA;
00128 }
00129
00130 static int cmp_id_key(const void *id, const void *k)
00131 {
00132 return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
00133 }
00134
00135 static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
00136 {
00137 return ((TiffGeoTagKeyName*)bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key))->name;
00138 }
00139
00140 static char *get_geokey_val(int key, int val)
00141 {
00142 char *ap;
00143
00144 if (val == TIFF_GEO_KEY_UNDEFINED)
00145 return av_strdup("undefined");
00146 if (val == TIFF_GEO_KEY_USER_DEFINED)
00147 return av_strdup("User-Defined");
00148
00149 #define RET_GEOKEY_VAL(TYPE, array)\
00150 if (val >= TIFF_##TYPE##_OFFSET &&\
00151 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
00152 return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
00153
00154 switch (key) {
00155 case TIFF_GT_MODEL_TYPE_GEOKEY:
00156 RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
00157 break;
00158 case TIFF_GT_RASTER_TYPE_GEOKEY:
00159 RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
00160 break;
00161 case TIFF_GEOG_LINEAR_UNITS_GEOKEY:
00162 case TIFF_PROJ_LINEAR_UNITS_GEOKEY:
00163 case TIFF_VERTICAL_UNITS_GEOKEY:
00164 RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
00165 break;
00166 case TIFF_GEOG_ANGULAR_UNITS_GEOKEY:
00167 case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY:
00168 RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
00169 break;
00170 case TIFF_GEOGRAPHIC_TYPE_GEOKEY:
00171 RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
00172 RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
00173 break;
00174 case TIFF_GEOG_GEODETIC_DATUM_GEOKEY:
00175 RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
00176 RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
00177 break;
00178 case TIFF_GEOG_ELLIPSOID_GEOKEY:
00179 RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
00180 break;
00181 case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY:
00182 RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
00183 break;
00184 case TIFF_PROJECTED_CS_TYPE_GEOKEY:
00185 return av_strdup(search_keyval(ff_tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(ff_tiff_proj_cs_type_codes), val));
00186 break;
00187 case TIFF_PROJECTION_GEOKEY:
00188 return av_strdup(search_keyval(ff_tiff_projection_codes, FF_ARRAY_ELEMS(ff_tiff_projection_codes), val));
00189 break;
00190 case TIFF_PROJ_COORD_TRANS_GEOKEY:
00191 RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
00192 break;
00193 case TIFF_VERTICAL_CS_TYPE_GEOKEY:
00194 RET_GEOKEY_VAL(VERT_CS, vert_cs);
00195 RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
00196 break;
00197
00198 }
00199
00200 ap = av_malloc(14);
00201 if (ap)
00202 snprintf(ap, 14, "Unknown-%d", val);
00203 return ap;
00204 }
00205
00206 static char *doubles2str(double *dp, int count, const char *sep)
00207 {
00208 int i;
00209 char *ap, *ap0;
00210 int component_len = 15 + strlen(sep);
00211 if (!sep) sep = ", ";
00212 ap = av_malloc(component_len * count);
00213 if (!ap)
00214 return NULL;
00215 ap0 = ap;
00216 ap[0] = '\0';
00217 for (i = 0; i < count; i++) {
00218 unsigned l = snprintf(ap, component_len, "%f%s", dp[i], sep);
00219 if(l >= component_len)
00220 return NULL;
00221 ap += l;
00222 }
00223 ap0[strlen(ap0) - strlen(sep)] = '\0';
00224 return ap0;
00225 }
00226
00227 static char *shorts2str(int16_t *sp, int count, const char *sep)
00228 {
00229 int i;
00230 char *ap, *ap0;
00231 if (!sep) sep = ", ";
00232 ap = av_malloc((5 + strlen(sep)) * count);
00233 if (!ap)
00234 return NULL;
00235 ap0 = ap;
00236 ap[0] = '\0';
00237 for (i = 0; i < count; i++) {
00238 int l = snprintf(ap, 5 + strlen(sep), "%d%s", sp[i], sep);
00239 ap += l;
00240 }
00241 ap0[strlen(ap0) - strlen(sep)] = '\0';
00242 return ap0;
00243 }
00244
00245 static int add_doubles_metadata(int count,
00246 const char *name, const char *sep,
00247 TiffContext *s)
00248 {
00249 char *ap;
00250 int i;
00251 double *dp;
00252
00253 if (count >= INT_MAX / sizeof(int64_t))
00254 return AVERROR_INVALIDDATA;
00255 if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
00256 return AVERROR_INVALIDDATA;
00257
00258 dp = av_malloc(count * sizeof(double));
00259 if (!dp)
00260 return AVERROR(ENOMEM);
00261
00262 for (i = 0; i < count; i++)
00263 dp[i] = tget_double(&s->gb, s->le);
00264 ap = doubles2str(dp, count, sep);
00265 av_freep(&dp);
00266 if (!ap)
00267 return AVERROR(ENOMEM);
00268 av_dict_set(&s->picture.metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
00269 return 0;
00270 }
00271
00272 static int add_shorts_metadata(int count, const char *name,
00273 const char *sep, TiffContext *s)
00274 {
00275 char *ap;
00276 int i;
00277 int16_t *sp;
00278
00279 if (count >= INT_MAX / sizeof(int16_t))
00280 return AVERROR_INVALIDDATA;
00281 if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int16_t))
00282 return AVERROR_INVALIDDATA;
00283
00284 sp = av_malloc(count * sizeof(int16_t));
00285 if (!sp)
00286 return AVERROR(ENOMEM);
00287
00288 for (i = 0; i < count; i++)
00289 sp[i] = tget_short(&s->gb, s->le);
00290 ap = shorts2str(sp, count, sep);
00291 av_freep(&sp);
00292 if (!ap)
00293 return AVERROR(ENOMEM);
00294 av_dict_set(&s->picture.metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
00295 return 0;
00296 }
00297
00298 static int add_string_metadata(int count, const char *name,
00299 TiffContext *s)
00300 {
00301 char *value;
00302
00303 if (bytestream2_get_bytes_left(&s->gb) < count)
00304 return AVERROR_INVALIDDATA;
00305
00306 value = av_malloc(count + 1);
00307 if (!value)
00308 return AVERROR(ENOMEM);
00309
00310 bytestream2_get_bufferu(&s->gb, value, count);
00311 value[count] = 0;
00312
00313 av_dict_set(&s->picture.metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
00314 return 0;
00315 }
00316
00317 static int add_metadata(int count, int type,
00318 const char *name, const char *sep, TiffContext *s)
00319 {
00320 switch(type) {
00321 case TIFF_DOUBLE: return add_doubles_metadata(count, name, sep, s);
00322 case TIFF_SHORT : return add_shorts_metadata(count, name, sep, s);
00323 case TIFF_STRING: return add_string_metadata(count, name, s);
00324 default : return AVERROR_INVALIDDATA;
00325 };
00326 }
00327
00328 #if CONFIG_ZLIB
00329 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
00330 int size)
00331 {
00332 z_stream zstream = { 0 };
00333 int zret;
00334
00335 zstream.next_in = src;
00336 zstream.avail_in = size;
00337 zstream.next_out = dst;
00338 zstream.avail_out = *len;
00339 zret = inflateInit(&zstream);
00340 if (zret != Z_OK) {
00341 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00342 return zret;
00343 }
00344 zret = inflate(&zstream, Z_SYNC_FLUSH);
00345 inflateEnd(&zstream);
00346 *len = zstream.total_out;
00347 return zret == Z_STREAM_END ? Z_OK : zret;
00348 }
00349 #endif
00350
00351 static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
00352 int usePtr, const uint8_t *src,
00353 uint8_t c, int width, int offset)
00354 {
00355 switch (bpp) {
00356 case 1:
00357 while (--width >= 0) {
00358 dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
00359 dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
00360 dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
00361 dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
00362 dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
00363 dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
00364 dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
00365 dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
00366 }
00367 break;
00368 case 2:
00369 while (--width >= 0) {
00370 dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
00371 dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
00372 dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
00373 dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
00374 }
00375 break;
00376 case 4:
00377 while (--width >= 0) {
00378 dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
00379 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
00380 }
00381 break;
00382 default:
00383 if (usePtr) {
00384 memcpy(dst + offset, src, width);
00385 } else {
00386 memset(dst + offset, c, width);
00387 }
00388 }
00389 }
00390
00391 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
00392 const uint8_t *src, int size, int lines)
00393 {
00394 int c, line, pixels, code;
00395 const uint8_t *ssrc = src;
00396 int width = ((s->width * s->bpp) + 7) >> 3;
00397
00398 if (size <= 0)
00399 return AVERROR_INVALIDDATA;
00400
00401 #if CONFIG_ZLIB
00402 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00403 uint8_t *zbuf;
00404 unsigned long outlen;
00405 int ret;
00406 outlen = width * lines;
00407 zbuf = av_malloc(outlen);
00408 if (!zbuf)
00409 return AVERROR(ENOMEM);
00410 ret = tiff_uncompress(zbuf, &outlen, src, size);
00411 if (ret != Z_OK) {
00412 av_log(s->avctx, AV_LOG_ERROR,
00413 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
00414 (unsigned long)width * lines, ret);
00415 av_free(zbuf);
00416 return -1;
00417 }
00418 src = zbuf;
00419 for (line = 0; line < lines; line++) {
00420 if(s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8){
00421 horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
00422 }else{
00423 memcpy(dst, src, width);
00424 }
00425 dst += stride;
00426 src += width;
00427 }
00428 av_free(zbuf);
00429 return 0;
00430 }
00431 #endif
00432 if (s->compr == TIFF_LZW) {
00433 if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
00434 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
00435 return -1;
00436 }
00437 }
00438 if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
00439 || s->compr == TIFF_G4) {
00440 int i, ret = 0;
00441 uint8_t *src2 = av_malloc((unsigned)size +
00442 FF_INPUT_BUFFER_PADDING_SIZE);
00443
00444 if (!src2) {
00445 av_log(s->avctx, AV_LOG_ERROR,
00446 "Error allocating temporary buffer\n");
00447 return AVERROR(ENOMEM);
00448 }
00449 if (s->fax_opts & 2) {
00450 av_log(s->avctx, AV_LOG_ERROR,
00451 "Uncompressed fax mode is not supported (yet)\n");
00452 av_free(src2);
00453 return -1;
00454 }
00455 if (!s->fill_order) {
00456 memcpy(src2, src, size);
00457 } else {
00458 for (i = 0; i < size; i++)
00459 src2[i] = av_reverse[src[i]];
00460 }
00461 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00462 switch (s->compr) {
00463 case TIFF_CCITT_RLE:
00464 case TIFF_G3:
00465 case TIFF_G4:
00466 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
00467 s->compr, s->fax_opts);
00468 break;
00469 }
00470 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
00471 for (line = 0; line < lines; line++) {
00472 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
00473 dst += stride;
00474 }
00475 av_free(src2);
00476 return ret;
00477 }
00478 for (line = 0; line < lines; line++) {
00479 if (src - ssrc > size) {
00480 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
00481 return -1;
00482 }
00483 switch (s->compr) {
00484 case TIFF_RAW:
00485 if (ssrc + size - src < width)
00486 return AVERROR_INVALIDDATA;
00487 if (!s->fill_order) {
00488 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
00489 dst, 1, src, 0, width, 0);
00490 } else {
00491 int i;
00492 for (i = 0; i < width; i++)
00493 dst[i] = av_reverse[src[i]];
00494 }
00495 src += width;
00496 break;
00497 case TIFF_PACKBITS:
00498 for (pixels = 0; pixels < width;) {
00499 if (ssrc + size - src < 2) {
00500 av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
00501 return AVERROR_INVALIDDATA;
00502 }
00503 code = (int8_t) * src++;
00504 if (code >= 0) {
00505 code++;
00506 if (pixels + code > width) {
00507 av_log(s->avctx, AV_LOG_ERROR,
00508 "Copy went out of bounds\n");
00509 return -1;
00510 }
00511 if (ssrc + size - src < code) {
00512 av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
00513 return AVERROR_INVALIDDATA;
00514 }
00515 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
00516 dst, 1, src, 0, code, pixels);
00517 src += code;
00518 pixels += code;
00519 } else if (code != -128) {
00520 code = (-code) + 1;
00521 if (pixels + code > width) {
00522 av_log(s->avctx, AV_LOG_ERROR,
00523 "Run went out of bounds\n");
00524 return -1;
00525 }
00526 c = *src++;
00527 horizontal_fill(s->bpp * (s->avctx->pix_fmt == PIX_FMT_PAL8),
00528 dst, 0, NULL, c, code, pixels);
00529 pixels += code;
00530 }
00531 }
00532 break;
00533 case TIFF_LZW:
00534 pixels = ff_lzw_decode(s->lzw, dst, width);
00535 if (pixels < width) {
00536 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
00537 pixels, width);
00538 return -1;
00539 }
00540 if (s->bpp < 8 && s->avctx->pix_fmt == PIX_FMT_PAL8)
00541 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
00542 break;
00543 }
00544 dst += stride;
00545 }
00546 return 0;
00547 }
00548
00549 static int init_image(TiffContext *s)
00550 {
00551 int i, ret;
00552 uint32_t *pal;
00553
00554 switch (s->bpp * 10 + s->bppcount) {
00555 case 11:
00556 if (!s->palette_is_set) {
00557 s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
00558 break;
00559 }
00560 case 21:
00561 case 41:
00562 case 81:
00563 s->avctx->pix_fmt = PIX_FMT_PAL8;
00564 break;
00565 case 243:
00566 s->avctx->pix_fmt = PIX_FMT_RGB24;
00567 break;
00568 case 161:
00569 s->avctx->pix_fmt = s->le ? PIX_FMT_GRAY16LE : PIX_FMT_GRAY16BE;
00570 break;
00571 case 162:
00572 s->avctx->pix_fmt = PIX_FMT_GRAY8A;
00573 break;
00574 case 324:
00575 s->avctx->pix_fmt = PIX_FMT_RGBA;
00576 break;
00577 case 483:
00578 s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
00579 break;
00580 case 644:
00581 s->avctx->pix_fmt = s->le ? PIX_FMT_RGBA64LE : PIX_FMT_RGBA64BE;
00582 break;
00583 default:
00584 av_log(s->avctx, AV_LOG_ERROR,
00585 "This format is not supported (bpp=%d, bppcount=%d)\n",
00586 s->bpp, s->bppcount);
00587 return AVERROR_INVALIDDATA;
00588 }
00589 if (s->width != s->avctx->width || s->height != s->avctx->height) {
00590 if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
00591 return ret;
00592 avcodec_set_dimensions(s->avctx, s->width, s->height);
00593 }
00594 if (s->picture.data[0])
00595 s->avctx->release_buffer(s->avctx, &s->picture);
00596 if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
00597 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00598 return ret;
00599 }
00600 if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
00601 if (s->palette_is_set) {
00602 memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
00603 } else {
00604
00605 pal = (uint32_t *) s->picture.data[1];
00606 for (i = 0; i < 1<<s->bpp; i++)
00607 pal[i] = 0xFF << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
00608 }
00609 }
00610 return 0;
00611 }
00612
00613 static int tiff_decode_tag(TiffContext *s)
00614 {
00615 unsigned tag, type, count, off, value = 0;
00616 int i, j, k, pos, start;
00617 int ret;
00618 uint32_t *pal;
00619 double *dp;
00620
00621 tag = tget_short(&s->gb, s->le);
00622 type = tget_short(&s->gb, s->le);
00623 count = tget_long(&s->gb, s->le);
00624 off = tget_long(&s->gb, s->le);
00625 start = bytestream2_tell(&s->gb);
00626
00627 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
00628 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
00629 type);
00630 return 0;
00631 }
00632
00633 if (count == 1) {
00634 switch (type) {
00635 case TIFF_BYTE:
00636 case TIFF_SHORT:
00637 bytestream2_seek(&s->gb, -4, SEEK_CUR);
00638 value = tget(&s->gb, type, s->le);
00639 break;
00640 case TIFF_LONG:
00641 value = off;
00642 break;
00643 case TIFF_STRING:
00644 if (count <= 4) {
00645 bytestream2_seek(&s->gb, -4, SEEK_CUR);
00646 break;
00647 }
00648 default:
00649 value = UINT_MAX;
00650 bytestream2_seek(&s->gb, off, SEEK_SET);
00651 }
00652 } else {
00653 if (count <= 4 && type_sizes[type] * count <= 4) {
00654 bytestream2_seek(&s->gb, -4, SEEK_CUR);
00655 } else {
00656 bytestream2_seek(&s->gb, off, SEEK_SET);
00657 }
00658 }
00659
00660 switch (tag) {
00661 case TIFF_WIDTH:
00662 s->width = value;
00663 break;
00664 case TIFF_HEIGHT:
00665 s->height = value;
00666 break;
00667 case TIFF_BPP:
00668 s->bppcount = count;
00669 if (count > 4) {
00670 av_log(s->avctx, AV_LOG_ERROR,
00671 "This format is not supported (bpp=%d, %d components)\n",
00672 s->bpp, count);
00673 return -1;
00674 }
00675 if (count == 1)
00676 s->bpp = value;
00677 else {
00678 switch (type) {
00679 case TIFF_BYTE:
00680 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
00681 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
00682 break;
00683 case TIFF_SHORT:
00684 case TIFF_LONG:
00685 s->bpp = 0;
00686 if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
00687 return -1;
00688 for (i = 0; i < count; i++)
00689 s->bpp += tget(&s->gb, type, s->le);
00690 break;
00691 default:
00692 s->bpp = -1;
00693 }
00694 }
00695 break;
00696 case TIFF_SAMPLES_PER_PIXEL:
00697 if (count != 1) {
00698 av_log(s->avctx, AV_LOG_ERROR,
00699 "Samples per pixel requires a single value, many provided\n");
00700 return AVERROR_INVALIDDATA;
00701 }
00702 if (s->bppcount == 1)
00703 s->bpp *= value;
00704 s->bppcount = value;
00705 break;
00706 case TIFF_COMPR:
00707 s->compr = value;
00708 s->predictor = 0;
00709 switch (s->compr) {
00710 case TIFF_RAW:
00711 case TIFF_PACKBITS:
00712 case TIFF_LZW:
00713 case TIFF_CCITT_RLE:
00714 break;
00715 case TIFF_G3:
00716 case TIFF_G4:
00717 s->fax_opts = 0;
00718 break;
00719 case TIFF_DEFLATE:
00720 case TIFF_ADOBE_DEFLATE:
00721 #if CONFIG_ZLIB
00722 break;
00723 #else
00724 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
00725 return -1;
00726 #endif
00727 case TIFF_JPEG:
00728 case TIFF_NEWJPEG:
00729 av_log(s->avctx, AV_LOG_ERROR,
00730 "JPEG compression is not supported\n");
00731 return -1;
00732 default:
00733 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
00734 s->compr);
00735 return -1;
00736 }
00737 break;
00738 case TIFF_ROWSPERSTRIP:
00739 if (type == TIFF_LONG && value == UINT_MAX)
00740 value = s->avctx->height;
00741 if (value < 1) {
00742 av_log(s->avctx, AV_LOG_ERROR,
00743 "Incorrect value of rows per strip\n");
00744 return -1;
00745 }
00746 s->rps = value;
00747 break;
00748 case TIFF_STRIP_OFFS:
00749 if (count == 1) {
00750 s->strippos = 0;
00751 s->stripoff = value;
00752 } else
00753 s->strippos = off;
00754 s->strips = count;
00755 if (s->strips == 1)
00756 s->rps = s->height;
00757 s->sot = type;
00758 if (s->strippos > bytestream2_size(&s->gb)) {
00759 av_log(s->avctx, AV_LOG_ERROR,
00760 "Tag referencing position outside the image\n");
00761 return -1;
00762 }
00763 break;
00764 case TIFF_STRIP_SIZE:
00765 if (count == 1) {
00766 s->stripsizesoff = 0;
00767 s->stripsize = value;
00768 s->strips = 1;
00769 } else {
00770 s->stripsizesoff = off;
00771 }
00772 s->strips = count;
00773 s->sstype = type;
00774 if (s->stripsizesoff > bytestream2_size(&s->gb)) {
00775 av_log(s->avctx, AV_LOG_ERROR,
00776 "Tag referencing position outside the image\n");
00777 return -1;
00778 }
00779 break;
00780 case TIFF_TILE_BYTE_COUNTS:
00781 case TIFF_TILE_LENGTH:
00782 case TIFF_TILE_OFFSETS:
00783 case TIFF_TILE_WIDTH:
00784 av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
00785 return AVERROR_PATCHWELCOME;
00786 break;
00787 case TIFF_PREDICTOR:
00788 s->predictor = value;
00789 break;
00790 case TIFF_INVERT:
00791 switch (value) {
00792 case 0:
00793 s->invert = 1;
00794 break;
00795 case 1:
00796 s->invert = 0;
00797 break;
00798 case 2:
00799 case 3:
00800 break;
00801 default:
00802 av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
00803 value);
00804 return -1;
00805 }
00806 break;
00807 case TIFF_FILL_ORDER:
00808 if (value < 1 || value > 2) {
00809 av_log(s->avctx, AV_LOG_ERROR,
00810 "Unknown FillOrder value %d, trying default one\n", value);
00811 value = 1;
00812 }
00813 s->fill_order = value - 1;
00814 break;
00815 case TIFF_PAL:
00816 pal = (uint32_t *) s->palette;
00817 off = type_sizes[type];
00818 if (count / 3 > 256 || bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
00819 return -1;
00820 off = (type_sizes[type] - 1) << 3;
00821 for (k = 2; k >= 0; k--) {
00822 for (i = 0; i < count / 3; i++) {
00823 if (k == 2)
00824 pal[i] = 0xff << 24;
00825 j = (tget(&s->gb, type, s->le) >> off) << (k * 8);
00826 pal[i] |= j;
00827 }
00828 }
00829 s->palette_is_set = 1;
00830 break;
00831 case TIFF_PLANAR:
00832 if (value == 2) {
00833 av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
00834 return -1;
00835 }
00836 break;
00837 case TIFF_T4OPTIONS:
00838 if (s->compr == TIFF_G3)
00839 s->fax_opts = value;
00840 break;
00841 case TIFF_T6OPTIONS:
00842 if (s->compr == TIFF_G4)
00843 s->fax_opts = value;
00844 break;
00845 #define ADD_METADATA(count, name, sep)\
00846 if (ret = add_metadata(count, type, name, sep, s) < 0) {\
00847 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
00848 return ret;\
00849 }
00850 case TIFF_MODEL_PIXEL_SCALE:
00851 ADD_METADATA(count, "ModelPixelScaleTag", NULL);
00852 break;
00853 case TIFF_MODEL_TRANSFORMATION:
00854 ADD_METADATA(count, "ModelTransformationTag", NULL);
00855 break;
00856 case TIFF_MODEL_TIEPOINT:
00857 ADD_METADATA(count, "ModelTiepointTag", NULL);
00858 break;
00859 case TIFF_GEO_KEY_DIRECTORY:
00860 ADD_METADATA(1, "GeoTIFF_Version", NULL);
00861 ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
00862 s->geotag_count = tget_short(&s->gb, s->le);
00863 if (s->geotag_count > count / 4 - 1) {
00864 s->geotag_count = count / 4 - 1;
00865 av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
00866 }
00867 if (bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4)
00868 return -1;
00869 s->geotags = av_mallocz(sizeof(TiffGeoTag) * s->geotag_count);
00870 if (!s->geotags) {
00871 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
00872 return AVERROR(ENOMEM);
00873 }
00874 for (i = 0; i < s->geotag_count; i++) {
00875 s->geotags[i].key = tget_short(&s->gb, s->le);
00876 s->geotags[i].type = tget_short(&s->gb, s->le);
00877 s->geotags[i].count = tget_short(&s->gb, s->le);
00878
00879 if (!s->geotags[i].type)
00880 s->geotags[i].val = get_geokey_val(s->geotags[i].key, tget_short(&s->gb, s->le));
00881 else
00882 s->geotags[i].offset = tget_short(&s->gb, s->le);
00883 }
00884 break;
00885 case TIFF_GEO_DOUBLE_PARAMS:
00886 if (count >= INT_MAX / sizeof(int64_t))
00887 return AVERROR_INVALIDDATA;
00888 if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
00889 return AVERROR_INVALIDDATA;
00890 dp = av_malloc(count * sizeof(double));
00891 if (!dp) {
00892 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
00893 return AVERROR(ENOMEM);
00894 }
00895 for (i = 0; i < count; i++)
00896 dp[i] = tget_double(&s->gb, s->le);
00897 for (i = 0; i < s->geotag_count; i++) {
00898 if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
00899 if (s->geotags[i].count == 0
00900 || s->geotags[i].offset + s->geotags[i].count > count) {
00901 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
00902 } else {
00903 char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
00904 if (!ap) {
00905 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
00906 av_freep(&dp);
00907 return AVERROR(ENOMEM);
00908 }
00909 s->geotags[i].val = ap;
00910 }
00911 }
00912 }
00913 av_freep(&dp);
00914 break;
00915 case TIFF_GEO_ASCII_PARAMS:
00916 pos = bytestream2_tell(&s->gb);
00917 for (i = 0; i < s->geotag_count; i++) {
00918 if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
00919 if (s->geotags[i].count == 0
00920 || s->geotags[i].offset + s->geotags[i].count > count) {
00921 av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
00922 } else {
00923 char *ap;
00924
00925 bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
00926 if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
00927 return -1;
00928 ap = av_malloc(s->geotags[i].count);
00929 if (!ap) {
00930 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
00931 return AVERROR(ENOMEM);
00932 }
00933 bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
00934 ap[s->geotags[i].count - 1] = '\0';
00935 s->geotags[i].val = ap;
00936 }
00937 }
00938 }
00939 break;
00940 case TIFF_ARTIST:
00941 ADD_METADATA(count, "artist", NULL);
00942 break;
00943 case TIFF_COPYRIGHT:
00944 ADD_METADATA(count, "copyright", NULL);
00945 break;
00946 case TIFF_DATE:
00947 ADD_METADATA(count, "date", NULL);
00948 break;
00949 case TIFF_DOCUMENT_NAME:
00950 ADD_METADATA(count, "document_name", NULL);
00951 break;
00952 case TIFF_HOST_COMPUTER:
00953 ADD_METADATA(count, "computer", NULL);
00954 break;
00955 case TIFF_IMAGE_DESCRIPTION:
00956 ADD_METADATA(count, "description", NULL);
00957 break;
00958 case TIFF_MAKE:
00959 ADD_METADATA(count, "make", NULL);
00960 break;
00961 case TIFF_MODEL:
00962 ADD_METADATA(count, "model", NULL);
00963 break;
00964 case TIFF_PAGE_NAME:
00965 ADD_METADATA(count, "page_name", NULL);
00966 break;
00967 case TIFF_PAGE_NUMBER:
00968 ADD_METADATA(count, "page_number", " / ");
00969 break;
00970 case TIFF_SOFTWARE_NAME:
00971 ADD_METADATA(count, "software", NULL);
00972 break;
00973 default:
00974 av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
00975 tag, tag);
00976 }
00977 bytestream2_seek(&s->gb, start, SEEK_SET);
00978 return 0;
00979 }
00980
00981 static int decode_frame(AVCodecContext *avctx,
00982 void *data, int *data_size, AVPacket *avpkt)
00983 {
00984 TiffContext *const s = avctx->priv_data;
00985 AVFrame *picture = data;
00986 AVFrame *const p = &s->picture;
00987 unsigned off;
00988 int id, le, ret;
00989 int i, j, entries;
00990 int stride;
00991 unsigned soff, ssize;
00992 uint8_t *dst;
00993 GetByteContext stripsizes;
00994 GetByteContext stripdata;
00995
00996 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
00997
00998
00999 if (avpkt->size < 8)
01000 return AVERROR_INVALIDDATA;
01001 id = bytestream2_get_le16u(&s->gb);
01002 if (id == 0x4949)
01003 le = 1;
01004 else if (id == 0x4D4D)
01005 le = 0;
01006 else {
01007 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
01008 return -1;
01009 }
01010 s->le = le;
01011
01012 s->bppcount = s->bpp = 1;
01013 s->invert = 0;
01014 s->compr = TIFF_RAW;
01015 s->fill_order = 0;
01016 free_geotags(s);
01017
01018 av_dict_free(&s->picture.metadata);
01019
01020
01021
01022 if (tget_short(&s->gb, le) != 42) {
01023 av_log(avctx, AV_LOG_ERROR,
01024 "The answer to life, universe and everything is not correct!\n");
01025 return -1;
01026 }
01027
01028 s->stripsizesoff = s->strippos = 0;
01029
01030 off = tget_long(&s->gb, le);
01031 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
01032 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
01033 return AVERROR_INVALIDDATA;
01034 }
01035 bytestream2_seek(&s->gb, off, SEEK_SET);
01036 entries = tget_short(&s->gb, le);
01037 if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
01038 return AVERROR_INVALIDDATA;
01039 for (i = 0; i < entries; i++) {
01040 if (tiff_decode_tag(s) < 0)
01041 return -1;
01042 }
01043
01044 for (i = 0; i<s->geotag_count; i++) {
01045 const char *keyname = get_geokey_name(s->geotags[i].key);
01046 if (!keyname) {
01047 av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
01048 continue;
01049 }
01050 if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
01051 av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
01052 continue;
01053 }
01054 ret = av_dict_set(&s->picture.metadata, keyname, s->geotags[i].val, 0);
01055 if (ret<0) {
01056 av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
01057 return ret;
01058 }
01059 }
01060
01061 if (!s->strippos && !s->stripoff) {
01062 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
01063 return -1;
01064 }
01065
01066 if ((ret = init_image(s)) < 0)
01067 return ret;
01068
01069 if (s->strips == 1 && !s->stripsize) {
01070 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
01071 s->stripsize = avpkt->size - s->stripoff;
01072 }
01073 stride = p->linesize[0];
01074 dst = p->data[0];
01075
01076 if (s->stripsizesoff) {
01077 if (s->stripsizesoff >= avpkt->size)
01078 return AVERROR_INVALIDDATA;
01079 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff, avpkt->size - s->stripsizesoff);
01080 }
01081 if (s->strippos) {
01082 if (s->strippos >= avpkt->size)
01083 return AVERROR_INVALIDDATA;
01084 bytestream2_init(&stripdata, avpkt->data + s->strippos, avpkt->size - s->strippos);
01085 }
01086
01087 for (i = 0; i < s->height; i += s->rps) {
01088 if (s->stripsizesoff)
01089 ssize = tget(&stripsizes, s->sstype, s->le);
01090 else
01091 ssize = s->stripsize;
01092
01093 if (s->strippos)
01094 soff = tget(&stripdata, s->sot, s->le);
01095 else
01096 soff = s->stripoff;
01097
01098 if (soff > avpkt->size || ssize > avpkt->size - soff) {
01099 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
01100 return -1;
01101 }
01102 if (tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
01103 FFMIN(s->rps, s->height - i)) < 0)
01104 break;
01105 dst += s->rps * stride;
01106 }
01107 if (s->predictor == 2) {
01108 dst = p->data[0];
01109 soff = s->bpp >> 3;
01110 ssize = s->width * soff;
01111 if (s->avctx->pix_fmt == PIX_FMT_RGB48LE ||
01112 s->avctx->pix_fmt == PIX_FMT_RGBA64LE) {
01113 for (i = 0; i < s->height; i++) {
01114 for (j = soff; j < ssize; j += 2)
01115 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
01116 dst += stride;
01117 }
01118 } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE ||
01119 s->avctx->pix_fmt == PIX_FMT_RGBA64BE) {
01120 for (i = 0; i < s->height; i++) {
01121 for (j = soff; j < ssize; j += 2)
01122 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
01123 dst += stride;
01124 }
01125 } else {
01126 for (i = 0; i < s->height; i++) {
01127 for (j = soff; j < ssize; j++)
01128 dst[j] += dst[j - soff];
01129 dst += stride;
01130 }
01131 }
01132 }
01133
01134 if (s->invert) {
01135 dst = s->picture.data[0];
01136 for (i = 0; i < s->height; i++) {
01137 for (j = 0; j < s->picture.linesize[0]; j++)
01138 dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
01139 dst += s->picture.linesize[0];
01140 }
01141 }
01142 *picture = s->picture;
01143 *data_size = sizeof(AVPicture);
01144
01145 return avpkt->size;
01146 }
01147
01148 static av_cold int tiff_init(AVCodecContext *avctx)
01149 {
01150 TiffContext *s = avctx->priv_data;
01151
01152 s->width = 0;
01153 s->height = 0;
01154 s->avctx = avctx;
01155 avcodec_get_frame_defaults(&s->picture);
01156 avctx->coded_frame = &s->picture;
01157 ff_lzw_decode_open(&s->lzw);
01158 ff_ccitt_unpack_init();
01159
01160 return 0;
01161 }
01162
01163 static av_cold int tiff_end(AVCodecContext *avctx)
01164 {
01165 TiffContext *const s = avctx->priv_data;
01166
01167 free_geotags(s);
01168 if (avctx->coded_frame && avctx->coded_frame->metadata)
01169 av_dict_free(&avctx->coded_frame->metadata);
01170
01171 ff_lzw_decode_close(&s->lzw);
01172 if (s->picture.data[0])
01173 avctx->release_buffer(avctx, &s->picture);
01174 return 0;
01175 }
01176
01177 AVCodec ff_tiff_decoder = {
01178 .name = "tiff",
01179 .type = AVMEDIA_TYPE_VIDEO,
01180 .id = AV_CODEC_ID_TIFF,
01181 .priv_data_size = sizeof(TiffContext),
01182 .init = tiff_init,
01183 .close = tiff_end,
01184 .decode = decode_frame,
01185 .capabilities = CODEC_CAP_DR1,
01186 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
01187 };