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.

635 lines
18KB

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