You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

697 lines
20KB

  1. /*
  2. * TIFF image decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * TIFF image decoder
  24. * @author Konstantin Shishkov
  25. */
  26. #include "avcodec.h"
  27. #include "config.h"
  28. #if CONFIG_ZLIB
  29. #include <zlib.h>
  30. #endif
  31. #include "lzw.h"
  32. #include "tiff.h"
  33. #include "faxcompr.h"
  34. #include "libavutil/common.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/imgutils.h"
  37. typedef struct TiffContext {
  38. AVCodecContext *avctx;
  39. AVFrame picture;
  40. int width, height;
  41. unsigned int bpp, bppcount;
  42. uint32_t palette[256];
  43. int palette_is_set;
  44. int le;
  45. enum TiffCompr compr;
  46. int invert;
  47. int fax_opts;
  48. int predictor;
  49. int fill_order;
  50. int strips, rps, sstype;
  51. int sot;
  52. const uint8_t *stripdata;
  53. const uint8_t *stripsizes;
  54. int stripsize, stripoff;
  55. LZWState *lzw;
  56. } TiffContext;
  57. static unsigned tget_short(const uint8_t **p, int le)
  58. {
  59. unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
  60. *p += 2;
  61. return v;
  62. }
  63. static unsigned tget_long(const uint8_t **p, int le)
  64. {
  65. unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
  66. *p += 4;
  67. return v;
  68. }
  69. static unsigned tget(const uint8_t **p, int type, int le)
  70. {
  71. switch (type) {
  72. case TIFF_BYTE : return *(*p)++;
  73. case TIFF_SHORT: return tget_short(p, le);
  74. case TIFF_LONG : return tget_long(p, le);
  75. default : return UINT_MAX;
  76. }
  77. }
  78. #if CONFIG_ZLIB
  79. static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
  80. int size)
  81. {
  82. z_stream zstream = { 0 };
  83. int zret;
  84. zstream.next_in = src;
  85. zstream.avail_in = size;
  86. zstream.next_out = dst;
  87. zstream.avail_out = *len;
  88. zret = inflateInit(&zstream);
  89. if (zret != Z_OK) {
  90. av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  91. return zret;
  92. }
  93. zret = inflate(&zstream, Z_SYNC_FLUSH);
  94. inflateEnd(&zstream);
  95. *len = zstream.total_out;
  96. return zret == Z_STREAM_END ? Z_OK : zret;
  97. }
  98. #endif
  99. static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
  100. const uint8_t *src, int size, int lines)
  101. {
  102. int c, line, pixels, code;
  103. const uint8_t *ssrc = src;
  104. int width = ((s->width * s->bpp) + 7) >> 3;
  105. if (size <= 0)
  106. return AVERROR_INVALIDDATA;
  107. #if CONFIG_ZLIB
  108. if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
  109. uint8_t *zbuf;
  110. unsigned long outlen;
  111. int ret;
  112. outlen = width * lines;
  113. zbuf = av_malloc(outlen);
  114. if (!zbuf)
  115. return AVERROR(ENOMEM);
  116. ret = tiff_uncompress(zbuf, &outlen, src, size);
  117. if (ret != Z_OK) {
  118. av_log(s->avctx, AV_LOG_ERROR,
  119. "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
  120. (unsigned long)width * lines, ret);
  121. av_free(zbuf);
  122. return -1;
  123. }
  124. src = zbuf;
  125. for (line = 0; line < lines; line++) {
  126. memcpy(dst, src, width);
  127. dst += stride;
  128. src += width;
  129. }
  130. av_free(zbuf);
  131. return 0;
  132. }
  133. #endif
  134. if (s->compr == TIFF_LZW) {
  135. if (ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0) {
  136. av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
  137. return -1;
  138. }
  139. }
  140. if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
  141. || s->compr == TIFF_G4) {
  142. int i, ret = 0;
  143. uint8_t *src2 = av_malloc((unsigned)size +
  144. FF_INPUT_BUFFER_PADDING_SIZE);
  145. if (!src2) {
  146. av_log(s->avctx, AV_LOG_ERROR,
  147. "Error allocating temporary buffer\n");
  148. return AVERROR(ENOMEM);
  149. }
  150. if (s->fax_opts & 2) {
  151. av_log(s->avctx, AV_LOG_ERROR,
  152. "Uncompressed fax mode is not supported (yet)\n");
  153. av_free(src2);
  154. return -1;
  155. }
  156. if (!s->fill_order) {
  157. memcpy(src2, src, size);
  158. } else {
  159. for (i = 0; i < size; i++)
  160. src2[i] = av_reverse[src[i]];
  161. }
  162. memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  163. switch (s->compr) {
  164. case TIFF_CCITT_RLE:
  165. case TIFF_G3:
  166. case TIFF_G4:
  167. ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
  168. s->compr, s->fax_opts);
  169. break;
  170. }
  171. av_free(src2);
  172. return ret;
  173. }
  174. for (line = 0; line < lines; line++) {
  175. if (src - ssrc > size) {
  176. av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
  177. return -1;
  178. }
  179. switch (s->compr) {
  180. case TIFF_RAW:
  181. if (ssrc + size - src < width)
  182. return AVERROR_INVALIDDATA;
  183. if (!s->fill_order) {
  184. memcpy(dst, src, width);
  185. } else {
  186. int i;
  187. for (i = 0; i < width; i++)
  188. dst[i] = av_reverse[src[i]];
  189. }
  190. src += width;
  191. break;
  192. case TIFF_PACKBITS:
  193. for (pixels = 0; pixels < width;) {
  194. code = (int8_t) * src++;
  195. if (code >= 0) {
  196. code++;
  197. if (pixels + code > width) {
  198. av_log(s->avctx, AV_LOG_ERROR,
  199. "Copy went out of bounds\n");
  200. return -1;
  201. }
  202. memcpy(dst + pixels, src, code);
  203. src += code;
  204. pixels += code;
  205. } else if (code != -128) { // -127..-1
  206. code = (-code) + 1;
  207. if (pixels + code > width) {
  208. av_log(s->avctx, AV_LOG_ERROR,
  209. "Run went out of bounds\n");
  210. return -1;
  211. }
  212. c = *src++;
  213. memset(dst + pixels, c, code);
  214. pixels += code;
  215. }
  216. }
  217. break;
  218. case TIFF_LZW:
  219. pixels = ff_lzw_decode(s->lzw, dst, width);
  220. if (pixels < width) {
  221. av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
  222. pixels, width);
  223. return -1;
  224. }
  225. break;
  226. }
  227. dst += stride;
  228. }
  229. return 0;
  230. }
  231. static int init_image(TiffContext *s)
  232. {
  233. int i, ret;
  234. uint32_t *pal;
  235. switch (s->bpp * 10 + s->bppcount) {
  236. case 11:
  237. s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
  238. break;
  239. case 81:
  240. s->avctx->pix_fmt = PIX_FMT_PAL8;
  241. break;
  242. case 243:
  243. s->avctx->pix_fmt = PIX_FMT_RGB24;
  244. break;
  245. case 161:
  246. s->avctx->pix_fmt = s->le ? PIX_FMT_GRAY16LE : PIX_FMT_GRAY16BE;
  247. break;
  248. case 324:
  249. s->avctx->pix_fmt = PIX_FMT_RGBA;
  250. break;
  251. case 483:
  252. s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
  253. break;
  254. default:
  255. av_log(s->avctx, AV_LOG_ERROR,
  256. "This format is not supported (bpp=%d, bppcount=%d)\n",
  257. s->bpp, s->bppcount);
  258. return AVERROR_INVALIDDATA;
  259. }
  260. if (s->width != s->avctx->width || s->height != s->avctx->height) {
  261. if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  262. return ret;
  263. avcodec_set_dimensions(s->avctx, s->width, s->height);
  264. }
  265. if (s->picture.data[0])
  266. s->avctx->release_buffer(s->avctx, &s->picture);
  267. if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
  268. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  269. return ret;
  270. }
  271. if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
  272. if (s->palette_is_set) {
  273. memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
  274. } else {
  275. /* make default grayscale pal */
  276. pal = (uint32_t *) s->picture.data[1];
  277. for (i = 0; i < 256; i++)
  278. pal[i] = i * 0x010101;
  279. }
  280. }
  281. return 0;
  282. }
  283. static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
  284. const uint8_t *buf, const uint8_t *end_buf)
  285. {
  286. unsigned tag, type, count, off, value = 0;
  287. int i, j;
  288. uint32_t *pal;
  289. const uint8_t *rp, *gp, *bp;
  290. if (end_buf - buf < 12)
  291. return -1;
  292. tag = tget_short(&buf, s->le);
  293. type = tget_short(&buf, s->le);
  294. count = tget_long(&buf, s->le);
  295. off = tget_long(&buf, s->le);
  296. if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
  297. av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
  298. type);
  299. return 0;
  300. }
  301. if (count == 1) {
  302. switch (type) {
  303. case TIFF_BYTE:
  304. case TIFF_SHORT:
  305. buf -= 4;
  306. value = tget(&buf, type, s->le);
  307. buf = NULL;
  308. break;
  309. case TIFF_LONG:
  310. value = off;
  311. buf = NULL;
  312. break;
  313. case TIFF_STRING:
  314. if (count <= 4) {
  315. buf -= 4;
  316. break;
  317. }
  318. default:
  319. value = UINT_MAX;
  320. buf = start + off;
  321. }
  322. } else {
  323. if (count <= 4 && type_sizes[type] * count <= 4) {
  324. buf -= 4;
  325. } else {
  326. buf = start + off;
  327. }
  328. }
  329. if (buf && (buf < start || buf > end_buf)) {
  330. av_log(s->avctx, AV_LOG_ERROR,
  331. "Tag referencing position outside the image\n");
  332. return -1;
  333. }
  334. switch (tag) {
  335. case TIFF_WIDTH:
  336. s->width = value;
  337. break;
  338. case TIFF_HEIGHT:
  339. s->height = value;
  340. break;
  341. case TIFF_BPP:
  342. s->bppcount = count;
  343. if (count > 4) {
  344. av_log(s->avctx, AV_LOG_ERROR,
  345. "This format is not supported (bpp=%d, %d components)\n",
  346. s->bpp, count);
  347. return -1;
  348. }
  349. if (count == 1)
  350. s->bpp = value;
  351. else {
  352. switch (type) {
  353. case TIFF_BYTE:
  354. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
  355. ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  356. break;
  357. case TIFF_SHORT:
  358. case TIFF_LONG:
  359. s->bpp = 0;
  360. for (i = 0; i < count && buf < end_buf; i++)
  361. s->bpp += tget(&buf, type, s->le);
  362. break;
  363. default:
  364. s->bpp = -1;
  365. }
  366. }
  367. break;
  368. case TIFF_SAMPLES_PER_PIXEL:
  369. if (count != 1) {
  370. av_log(s->avctx, AV_LOG_ERROR,
  371. "Samples per pixel requires a single value, many provided\n");
  372. return AVERROR_INVALIDDATA;
  373. }
  374. if (s->bppcount == 1)
  375. s->bpp *= value;
  376. s->bppcount = value;
  377. break;
  378. case TIFF_COMPR:
  379. s->compr = value;
  380. s->predictor = 0;
  381. switch (s->compr) {
  382. case TIFF_RAW:
  383. case TIFF_PACKBITS:
  384. case TIFF_LZW:
  385. case TIFF_CCITT_RLE:
  386. break;
  387. case TIFF_G3:
  388. case TIFF_G4:
  389. s->fax_opts = 0;
  390. break;
  391. case TIFF_DEFLATE:
  392. case TIFF_ADOBE_DEFLATE:
  393. #if CONFIG_ZLIB
  394. break;
  395. #else
  396. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  397. return -1;
  398. #endif
  399. case TIFF_JPEG:
  400. case TIFF_NEWJPEG:
  401. av_log(s->avctx, AV_LOG_ERROR,
  402. "JPEG compression is not supported\n");
  403. return -1;
  404. default:
  405. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
  406. s->compr);
  407. return -1;
  408. }
  409. break;
  410. case TIFF_ROWSPERSTRIP:
  411. if (type == TIFF_LONG && value == UINT_MAX)
  412. value = s->avctx->height;
  413. if (value < 1) {
  414. av_log(s->avctx, AV_LOG_ERROR,
  415. "Incorrect value of rows per strip\n");
  416. return -1;
  417. }
  418. s->rps = value;
  419. break;
  420. case TIFF_STRIP_OFFS:
  421. if (count == 1) {
  422. s->stripdata = NULL;
  423. s->stripoff = value;
  424. } else
  425. s->stripdata = start + off;
  426. s->strips = count;
  427. if (s->strips == 1)
  428. s->rps = s->height;
  429. s->sot = type;
  430. if (s->stripdata > end_buf) {
  431. av_log(s->avctx, AV_LOG_ERROR,
  432. "Tag referencing position outside the image\n");
  433. return -1;
  434. }
  435. break;
  436. case TIFF_STRIP_SIZE:
  437. if (count == 1) {
  438. s->stripsizes = NULL;
  439. s->stripsize = value;
  440. s->strips = 1;
  441. } else {
  442. s->stripsizes = start + off;
  443. }
  444. s->strips = count;
  445. s->sstype = type;
  446. if (s->stripsizes > end_buf) {
  447. av_log(s->avctx, AV_LOG_ERROR,
  448. "Tag referencing position outside the image\n");
  449. return -1;
  450. }
  451. break;
  452. case TIFF_PREDICTOR:
  453. s->predictor = value;
  454. break;
  455. case TIFF_INVERT:
  456. switch (value) {
  457. case 0:
  458. s->invert = 1;
  459. break;
  460. case 1:
  461. s->invert = 0;
  462. break;
  463. case 2:
  464. case 3:
  465. break;
  466. default:
  467. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
  468. value);
  469. return -1;
  470. }
  471. break;
  472. case TIFF_FILL_ORDER:
  473. if (value < 1 || value > 2) {
  474. av_log(s->avctx, AV_LOG_ERROR,
  475. "Unknown FillOrder value %d, trying default one\n", value);
  476. value = 1;
  477. }
  478. s->fill_order = value - 1;
  479. break;
  480. case TIFF_PAL:
  481. pal = (uint32_t *) s->palette;
  482. off = type_sizes[type];
  483. if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
  484. return -1;
  485. rp = buf;
  486. gp = buf + count / 3 * off;
  487. bp = buf + count / 3 * off * 2;
  488. off = (type_sizes[type] - 1) << 3;
  489. for (i = 0; i < count / 3; i++) {
  490. j = (tget(&rp, type, s->le) >> off) << 16;
  491. j |= (tget(&gp, type, s->le) >> off) << 8;
  492. j |= tget(&bp, type, s->le) >> off;
  493. pal[i] = j;
  494. }
  495. s->palette_is_set = 1;
  496. break;
  497. case TIFF_PLANAR:
  498. if (value == 2) {
  499. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  500. return -1;
  501. }
  502. break;
  503. case TIFF_T4OPTIONS:
  504. if (s->compr == TIFF_G3)
  505. s->fax_opts = value;
  506. break;
  507. case TIFF_T6OPTIONS:
  508. if (s->compr == TIFF_G4)
  509. s->fax_opts = value;
  510. break;
  511. default:
  512. av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
  513. tag, tag);
  514. }
  515. return 0;
  516. }
  517. static int decode_frame(AVCodecContext *avctx,
  518. void *data, int *data_size, AVPacket *avpkt)
  519. {
  520. const uint8_t *buf = avpkt->data;
  521. int buf_size = avpkt->size;
  522. TiffContext *const s = avctx->priv_data;
  523. AVFrame *picture = data;
  524. AVFrame *const p = &s->picture;
  525. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  526. unsigned off;
  527. int id, le, ret;
  528. int i, j, entries;
  529. int stride;
  530. unsigned soff, ssize;
  531. uint8_t *dst;
  532. //parse image header
  533. if (end_buf - buf < 8)
  534. return AVERROR_INVALIDDATA;
  535. id = AV_RL16(buf);
  536. buf += 2;
  537. if (id == 0x4949)
  538. le = 1;
  539. else if (id == 0x4D4D)
  540. le = 0;
  541. else {
  542. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  543. return -1;
  544. }
  545. s->le = le;
  546. s->invert = 0;
  547. s->compr = TIFF_RAW;
  548. s->fill_order = 0;
  549. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  550. // that further identifies the file as a TIFF file"
  551. if (tget_short(&buf, le) != 42) {
  552. av_log(avctx, AV_LOG_ERROR,
  553. "The answer to life, universe and everything is not correct!\n");
  554. return -1;
  555. }
  556. // Reset these pointers so we can tell if they were set this frame
  557. s->stripsizes = s->stripdata = NULL;
  558. /* parse image file directory */
  559. off = tget_long(&buf, le);
  560. if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
  561. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  562. return AVERROR_INVALIDDATA;
  563. }
  564. buf = orig_buf + off;
  565. entries = tget_short(&buf, le);
  566. for (i = 0; i < entries; i++) {
  567. if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  568. return -1;
  569. buf += 12;
  570. }
  571. if (!s->stripdata && !s->stripoff) {
  572. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  573. return -1;
  574. }
  575. /* now we have the data and may start decoding */
  576. if ((ret = init_image(s)) < 0)
  577. return ret;
  578. if (s->strips == 1 && !s->stripsize) {
  579. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  580. s->stripsize = buf_size - s->stripoff;
  581. }
  582. stride = p->linesize[0];
  583. dst = p->data[0];
  584. for (i = 0; i < s->height; i += s->rps) {
  585. if (s->stripsizes) {
  586. if (s->stripsizes >= end_buf)
  587. return AVERROR_INVALIDDATA;
  588. ssize = tget(&s->stripsizes, s->sstype, s->le);
  589. } else
  590. ssize = s->stripsize;
  591. if (s->stripdata) {
  592. if (s->stripdata >= end_buf)
  593. return AVERROR_INVALIDDATA;
  594. soff = tget(&s->stripdata, s->sot, s->le);
  595. } else
  596. soff = s->stripoff;
  597. if (soff > buf_size || ssize > buf_size - soff) {
  598. av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
  599. return -1;
  600. }
  601. if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
  602. FFMIN(s->rps, s->height - i)) < 0)
  603. break;
  604. dst += s->rps * stride;
  605. }
  606. if (s->predictor == 2) {
  607. dst = p->data[0];
  608. soff = s->bpp >> 3;
  609. ssize = s->width * soff;
  610. for (i = 0; i < s->height; i++) {
  611. for (j = soff; j < ssize; j++)
  612. dst[j] += dst[j - soff];
  613. dst += stride;
  614. }
  615. }
  616. if (s->invert) {
  617. uint8_t *src;
  618. int j;
  619. src = s->picture.data[0];
  620. for (j = 0; j < s->height; j++) {
  621. for (i = 0; i < s->picture.linesize[0]; i++)
  622. src[i] = 255 - src[i];
  623. src += s->picture.linesize[0];
  624. }
  625. }
  626. *picture = s->picture;
  627. *data_size = sizeof(AVPicture);
  628. return buf_size;
  629. }
  630. static av_cold int tiff_init(AVCodecContext *avctx)
  631. {
  632. TiffContext *s = avctx->priv_data;
  633. s->width = 0;
  634. s->height = 0;
  635. s->avctx = avctx;
  636. avcodec_get_frame_defaults(&s->picture);
  637. avctx->coded_frame = &s->picture;
  638. ff_lzw_decode_open(&s->lzw);
  639. ff_ccitt_unpack_init();
  640. return 0;
  641. }
  642. static av_cold int tiff_end(AVCodecContext *avctx)
  643. {
  644. TiffContext *const s = avctx->priv_data;
  645. ff_lzw_decode_close(&s->lzw);
  646. if (s->picture.data[0])
  647. avctx->release_buffer(avctx, &s->picture);
  648. return 0;
  649. }
  650. AVCodec ff_tiff_decoder = {
  651. .name = "tiff",
  652. .type = AVMEDIA_TYPE_VIDEO,
  653. .id = AV_CODEC_ID_TIFF,
  654. .priv_data_size = sizeof(TiffContext),
  655. .init = tiff_init,
  656. .close = tiff_end,
  657. .decode = decode_frame,
  658. .capabilities = CODEC_CAP_DR1,
  659. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  660. };