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.

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