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.

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