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.

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