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.

634 lines
18KB

  1. /*
  2. * Copyright (c) 2006 Konstantin Shishkov
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * TIFF image decoder
  23. * @author Konstantin Shishkov
  24. */
  25. #include "avcodec.h"
  26. #if CONFIG_ZLIB
  27. #include <zlib.h>
  28. #endif
  29. #include "lzw.h"
  30. #include "tiff.h"
  31. #include "faxcompr.h"
  32. #include "libavutil/common.h"
  33. #include "libavutil/intreadwrite.h"
  34. #include "libavutil/imgutils.h"
  35. typedef struct TiffContext {
  36. AVCodecContext *avctx;
  37. AVFrame picture;
  38. int width, height;
  39. unsigned int bpp, bppcount;
  40. uint32_t palette[256];
  41. int palette_is_set;
  42. int le;
  43. enum TiffCompr compr;
  44. int invert;
  45. int fax_opts;
  46. int predictor;
  47. int fill_order;
  48. int strips, rps, sstype;
  49. int sot;
  50. const uint8_t* stripdata;
  51. const uint8_t* stripsizes;
  52. int stripsize, stripoff;
  53. LZWState *lzw;
  54. } TiffContext;
  55. static int tget_short(const uint8_t **p, int le){
  56. int v = le ? AV_RL16(*p) : AV_RB16(*p);
  57. *p += 2;
  58. return v;
  59. }
  60. static int tget_long(const uint8_t **p, int le){
  61. int v = le ? AV_RL32(*p) : AV_RB32(*p);
  62. *p += 4;
  63. return v;
  64. }
  65. static int tget(const uint8_t **p, int type, int le){
  66. switch(type){
  67. case TIFF_BYTE : return *(*p)++;
  68. case TIFF_SHORT: return tget_short(p, le);
  69. case TIFF_LONG : return tget_long (p, le);
  70. default : return -1;
  71. }
  72. }
  73. #if CONFIG_ZLIB
  74. static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
  75. {
  76. z_stream zstream;
  77. int zret;
  78. memset(&zstream, 0, sizeof(zstream));
  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 CONFIG_ZLIB
  99. uint8_t *zbuf; unsigned long outlen;
  100. if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
  101. int ret;
  102. outlen = width * lines;
  103. zbuf = av_malloc(outlen);
  104. ret = tiff_uncompress(zbuf, &outlen, src, size);
  105. if(ret != Z_OK){
  106. av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
  107. av_free(zbuf);
  108. return -1;
  109. }
  110. src = zbuf;
  111. for(line = 0; line < lines; line++){
  112. memcpy(dst, src, width);
  113. dst += stride;
  114. src += width;
  115. }
  116. av_free(zbuf);
  117. return 0;
  118. }
  119. #endif
  120. if(s->compr == TIFF_LZW){
  121. if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
  122. av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
  123. return -1;
  124. }
  125. }
  126. if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
  127. int i, ret = 0;
  128. uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  129. if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
  130. av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
  131. return -1;
  132. }
  133. if(s->fax_opts & 2){
  134. av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
  135. av_free(src2);
  136. return -1;
  137. }
  138. if(!s->fill_order){
  139. memcpy(src2, src, size);
  140. }else{
  141. for(i = 0; i < size; i++)
  142. src2[i] = av_reverse[src[i]];
  143. }
  144. memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  145. switch(s->compr){
  146. case TIFF_CCITT_RLE:
  147. case TIFF_G3:
  148. case TIFF_G4:
  149. ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
  150. break;
  151. }
  152. av_free(src2);
  153. return ret;
  154. }
  155. for(line = 0; line < lines; line++){
  156. if(src - ssrc > size){
  157. av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
  158. return -1;
  159. }
  160. switch(s->compr){
  161. case TIFF_RAW:
  162. if (!s->fill_order) {
  163. memcpy(dst, src, width);
  164. } else {
  165. int i;
  166. for (i = 0; i < width; i++)
  167. dst[i] = av_reverse[src[i]];
  168. }
  169. src += width;
  170. break;
  171. case TIFF_PACKBITS:
  172. for(pixels = 0; pixels < width;){
  173. code = (int8_t)*src++;
  174. if(code >= 0){
  175. code++;
  176. if(pixels + code > width){
  177. av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
  178. return -1;
  179. }
  180. memcpy(dst + pixels, src, code);
  181. src += code;
  182. pixels += code;
  183. }else if(code != -128){ // -127..-1
  184. code = (-code) + 1;
  185. if(pixels + code > width){
  186. av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  187. return -1;
  188. }
  189. c = *src++;
  190. memset(dst + pixels, c, code);
  191. pixels += code;
  192. }
  193. }
  194. break;
  195. case TIFF_LZW:
  196. pixels = ff_lzw_decode(s->lzw, dst, width);
  197. if(pixels < width){
  198. av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
  199. return -1;
  200. }
  201. break;
  202. }
  203. dst += stride;
  204. }
  205. return 0;
  206. }
  207. static int init_image(TiffContext *s)
  208. {
  209. int i, ret;
  210. uint32_t *pal;
  211. switch (s->bpp * 10 + s->bppcount) {
  212. case 11:
  213. s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
  214. break;
  215. case 81:
  216. s->avctx->pix_fmt = PIX_FMT_PAL8;
  217. break;
  218. case 243:
  219. s->avctx->pix_fmt = PIX_FMT_RGB24;
  220. break;
  221. case 161:
  222. s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
  223. break;
  224. case 324:
  225. s->avctx->pix_fmt = PIX_FMT_RGBA;
  226. break;
  227. case 483:
  228. s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
  229. break;
  230. default:
  231. av_log(s->avctx, AV_LOG_ERROR,
  232. "This format is not supported (bpp=%d, bppcount=%d)\n",
  233. s->bpp, s->bppcount);
  234. return AVERROR_INVALIDDATA;
  235. }
  236. if (s->width != s->avctx->width || s->height != s->avctx->height) {
  237. if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  238. return ret;
  239. avcodec_set_dimensions(s->avctx, s->width, s->height);
  240. }
  241. if (s->picture.data[0])
  242. s->avctx->release_buffer(s->avctx, &s->picture);
  243. if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
  244. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  245. return ret;
  246. }
  247. if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
  248. if (s->palette_is_set) {
  249. memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
  250. } else {
  251. /* make default grayscale pal */
  252. pal = (uint32_t *) s->picture.data[1];
  253. for (i = 0; i < 256; i++)
  254. pal[i] = i * 0x010101;
  255. }
  256. }
  257. return 0;
  258. }
  259. static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
  260. {
  261. int tag, type, count, off, value = 0;
  262. int i, j;
  263. uint32_t *pal;
  264. const uint8_t *rp, *gp, *bp;
  265. tag = tget_short(&buf, s->le);
  266. type = tget_short(&buf, s->le);
  267. count = tget_long(&buf, s->le);
  268. off = tget_long(&buf, s->le);
  269. if(count == 1){
  270. switch(type){
  271. case TIFF_BYTE:
  272. case TIFF_SHORT:
  273. buf -= 4;
  274. value = tget(&buf, type, s->le);
  275. buf = NULL;
  276. break;
  277. case TIFF_LONG:
  278. value = off;
  279. buf = NULL;
  280. break;
  281. case TIFF_STRING:
  282. if(count <= 4){
  283. buf -= 4;
  284. break;
  285. }
  286. default:
  287. value = -1;
  288. buf = start + off;
  289. }
  290. }else if(type_sizes[type] * count <= 4){
  291. buf -= 4;
  292. }else{
  293. buf = start + off;
  294. }
  295. if(buf && (buf < start || buf > end_buf)){
  296. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  297. return -1;
  298. }
  299. switch(tag){
  300. case TIFF_WIDTH:
  301. s->width = value;
  302. break;
  303. case TIFF_HEIGHT:
  304. s->height = value;
  305. break;
  306. case TIFF_BPP:
  307. s->bppcount = count;
  308. if(count > 4){
  309. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
  310. return -1;
  311. }
  312. if(count == 1) s->bpp = value;
  313. else{
  314. switch(type){
  315. case TIFF_BYTE:
  316. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  317. break;
  318. case TIFF_SHORT:
  319. case TIFF_LONG:
  320. s->bpp = 0;
  321. for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
  322. break;
  323. default:
  324. s->bpp = -1;
  325. }
  326. }
  327. break;
  328. case TIFF_SAMPLES_PER_PIXEL:
  329. if (count != 1) {
  330. av_log(s->avctx, AV_LOG_ERROR,
  331. "Samples per pixel requires a single value, many provided\n");
  332. return AVERROR_INVALIDDATA;
  333. }
  334. if (s->bppcount == 1)
  335. s->bpp *= value;
  336. s->bppcount = value;
  337. break;
  338. case TIFF_COMPR:
  339. s->compr = value;
  340. s->predictor = 0;
  341. switch(s->compr){
  342. case TIFF_RAW:
  343. case TIFF_PACKBITS:
  344. case TIFF_LZW:
  345. case TIFF_CCITT_RLE:
  346. break;
  347. case TIFF_G3:
  348. case TIFF_G4:
  349. s->fax_opts = 0;
  350. break;
  351. case TIFF_DEFLATE:
  352. case TIFF_ADOBE_DEFLATE:
  353. #if CONFIG_ZLIB
  354. break;
  355. #else
  356. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  357. return -1;
  358. #endif
  359. case TIFF_JPEG:
  360. case TIFF_NEWJPEG:
  361. av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
  362. return -1;
  363. default:
  364. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  365. return -1;
  366. }
  367. break;
  368. case TIFF_ROWSPERSTRIP:
  369. if(type == TIFF_LONG && value == -1)
  370. value = s->avctx->height;
  371. if(value < 1){
  372. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  373. return -1;
  374. }
  375. s->rps = value;
  376. break;
  377. case TIFF_STRIP_OFFS:
  378. if(count == 1){
  379. s->stripdata = NULL;
  380. s->stripoff = value;
  381. }else
  382. s->stripdata = start + off;
  383. s->strips = count;
  384. if(s->strips == 1) s->rps = s->height;
  385. s->sot = type;
  386. if(s->stripdata > end_buf){
  387. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  388. return -1;
  389. }
  390. break;
  391. case TIFF_STRIP_SIZE:
  392. if(count == 1){
  393. s->stripsizes = NULL;
  394. s->stripsize = value;
  395. s->strips = 1;
  396. }else{
  397. s->stripsizes = start + off;
  398. }
  399. s->strips = count;
  400. s->sstype = type;
  401. if(s->stripsizes > end_buf){
  402. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  403. return -1;
  404. }
  405. break;
  406. case TIFF_PREDICTOR:
  407. s->predictor = value;
  408. break;
  409. case TIFF_INVERT:
  410. switch(value){
  411. case 0:
  412. s->invert = 1;
  413. break;
  414. case 1:
  415. s->invert = 0;
  416. break;
  417. case 2:
  418. case 3:
  419. break;
  420. default:
  421. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  422. return -1;
  423. }
  424. break;
  425. case TIFF_FILL_ORDER:
  426. if(value < 1 || value > 2){
  427. av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
  428. value = 1;
  429. }
  430. s->fill_order = value - 1;
  431. break;
  432. case TIFF_PAL:
  433. pal = (uint32_t *) s->palette;
  434. off = type_sizes[type];
  435. rp = buf;
  436. gp = buf + count / 3 * off;
  437. bp = buf + count / 3 * off * 2;
  438. off = (type_sizes[type] - 1) << 3;
  439. for(i = 0; i < count / 3; i++){
  440. j = 0xff << 24;
  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. .name = "tiff",
  591. .type = AVMEDIA_TYPE_VIDEO,
  592. .id = CODEC_ID_TIFF,
  593. .priv_data_size = sizeof(TiffContext),
  594. .init = tiff_init,
  595. .close = tiff_end,
  596. .decode = decode_frame,
  597. .capabilities = CODEC_CAP_DR1,
  598. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  599. };