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.

657 lines
19KB

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