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.

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