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.

715 lines
21KB

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