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.

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