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. * 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. */
  24. #include "avcodec.h"
  25. #if CONFIG_ZLIB
  26. #include <zlib.h>
  27. #endif
  28. #include "lzw.h"
  29. #include "tiff.h"
  30. #include "faxcompr.h"
  31. #include "libavutil/common.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/imgutils.h"
  34. typedef struct TiffContext {
  35. AVCodecContext *avctx;
  36. AVFrame picture;
  37. int width, height;
  38. unsigned int bpp, bppcount;
  39. uint32_t palette[256];
  40. int palette_is_set;
  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) + 7) >> 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->avctx->pix_fmt == PIX_FMT_PAL8) {
  247. if (s->palette_is_set) {
  248. memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
  249. } else {
  250. /* make default grayscale pal */
  251. pal = (uint32_t *) s->picture.data[1];
  252. for (i = 0; i < 256; i++)
  253. pal[i] = i * 0x010101;
  254. }
  255. }
  256. return 0;
  257. }
  258. static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
  259. {
  260. int tag, type, count, off, value = 0;
  261. int i, j;
  262. uint32_t *pal;
  263. const uint8_t *rp, *gp, *bp;
  264. tag = tget_short(&buf, s->le);
  265. type = tget_short(&buf, s->le);
  266. count = tget_long(&buf, s->le);
  267. off = tget_long(&buf, s->le);
  268. if(count == 1){
  269. switch(type){
  270. case TIFF_BYTE:
  271. case TIFF_SHORT:
  272. buf -= 4;
  273. value = tget(&buf, type, s->le);
  274. buf = NULL;
  275. break;
  276. case TIFF_LONG:
  277. value = off;
  278. buf = NULL;
  279. break;
  280. case TIFF_STRING:
  281. if(count <= 4){
  282. buf -= 4;
  283. break;
  284. }
  285. default:
  286. value = -1;
  287. buf = start + off;
  288. }
  289. }else if(type_sizes[type] * count <= 4){
  290. buf -= 4;
  291. }else{
  292. buf = start + off;
  293. }
  294. if(buf && (buf < start || buf > end_buf)){
  295. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  296. return -1;
  297. }
  298. switch(tag){
  299. case TIFF_WIDTH:
  300. s->width = value;
  301. break;
  302. case TIFF_HEIGHT:
  303. s->height = value;
  304. break;
  305. case TIFF_BPP:
  306. s->bppcount = count;
  307. if(count > 4){
  308. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
  309. return -1;
  310. }
  311. if(count == 1) s->bpp = value;
  312. else{
  313. switch(type){
  314. case TIFF_BYTE:
  315. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  316. break;
  317. case TIFF_SHORT:
  318. case TIFF_LONG:
  319. s->bpp = 0;
  320. for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
  321. break;
  322. default:
  323. s->bpp = -1;
  324. }
  325. }
  326. break;
  327. case TIFF_SAMPLES_PER_PIXEL:
  328. if (count != 1) {
  329. av_log(s->avctx, AV_LOG_ERROR,
  330. "Samples per pixel requires a single value, many provided\n");
  331. return AVERROR_INVALIDDATA;
  332. }
  333. if (s->bppcount == 1)
  334. s->bpp *= value;
  335. s->bppcount = value;
  336. break;
  337. case TIFF_COMPR:
  338. s->compr = value;
  339. s->predictor = 0;
  340. switch(s->compr){
  341. case TIFF_RAW:
  342. case TIFF_PACKBITS:
  343. case TIFF_LZW:
  344. case TIFF_CCITT_RLE:
  345. break;
  346. case TIFF_G3:
  347. case TIFF_G4:
  348. s->fax_opts = 0;
  349. break;
  350. case TIFF_DEFLATE:
  351. case TIFF_ADOBE_DEFLATE:
  352. #if CONFIG_ZLIB
  353. break;
  354. #else
  355. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  356. return -1;
  357. #endif
  358. case TIFF_JPEG:
  359. case TIFF_NEWJPEG:
  360. av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
  361. return -1;
  362. default:
  363. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  364. return -1;
  365. }
  366. break;
  367. case TIFF_ROWSPERSTRIP:
  368. if(type == TIFF_LONG && value == -1)
  369. value = s->avctx->height;
  370. if(value < 1){
  371. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  372. return -1;
  373. }
  374. s->rps = value;
  375. break;
  376. case TIFF_STRIP_OFFS:
  377. if(count == 1){
  378. s->stripdata = NULL;
  379. s->stripoff = value;
  380. }else
  381. s->stripdata = start + off;
  382. s->strips = count;
  383. if(s->strips == 1) s->rps = s->height;
  384. s->sot = type;
  385. if(s->stripdata > end_buf){
  386. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  387. return -1;
  388. }
  389. break;
  390. case TIFF_STRIP_SIZE:
  391. if(count == 1){
  392. s->stripsizes = NULL;
  393. s->stripsize = value;
  394. s->strips = 1;
  395. }else{
  396. s->stripsizes = start + off;
  397. }
  398. s->strips = count;
  399. s->sstype = type;
  400. if(s->stripsizes > 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_PREDICTOR:
  406. s->predictor = value;
  407. break;
  408. case TIFF_INVERT:
  409. switch(value){
  410. case 0:
  411. s->invert = 1;
  412. break;
  413. case 1:
  414. s->invert = 0;
  415. break;
  416. case 2:
  417. case 3:
  418. break;
  419. default:
  420. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  421. return -1;
  422. }
  423. break;
  424. case TIFF_FILL_ORDER:
  425. if(value < 1 || value > 2){
  426. av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
  427. value = 1;
  428. }
  429. s->fill_order = value - 1;
  430. break;
  431. case TIFF_PAL:
  432. pal = (uint32_t *) s->palette;
  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 = 0xff << 24;
  440. j |= (tget(&rp, type, s->le) >> off) << 16;
  441. j |= (tget(&gp, type, s->le) >> off) << 8;
  442. j |= tget(&bp, type, s->le) >> off;
  443. pal[i] = j;
  444. }
  445. s->palette_is_set = 1;
  446. break;
  447. case TIFF_PLANAR:
  448. if(value == 2){
  449. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  450. return -1;
  451. }
  452. break;
  453. case TIFF_T4OPTIONS:
  454. if(s->compr == TIFF_G3)
  455. s->fax_opts = value;
  456. break;
  457. case TIFF_T6OPTIONS:
  458. if(s->compr == TIFF_G4)
  459. s->fax_opts = value;
  460. break;
  461. default:
  462. av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
  463. }
  464. return 0;
  465. }
  466. static int decode_frame(AVCodecContext *avctx,
  467. void *data, int *data_size,
  468. AVPacket *avpkt)
  469. {
  470. const uint8_t *buf = avpkt->data;
  471. int buf_size = avpkt->size;
  472. TiffContext * const s = avctx->priv_data;
  473. AVFrame *picture = data;
  474. AVFrame * const p= (AVFrame*)&s->picture;
  475. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  476. int id, le, off, ret;
  477. int i, j, entries;
  478. int stride, soff, ssize;
  479. uint8_t *dst;
  480. //parse image header
  481. id = AV_RL16(buf); buf += 2;
  482. if(id == 0x4949) le = 1;
  483. else if(id == 0x4D4D) le = 0;
  484. else{
  485. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  486. return -1;
  487. }
  488. s->le = le;
  489. s->invert = 0;
  490. s->compr = TIFF_RAW;
  491. s->fill_order = 0;
  492. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  493. // that further identifies the file as a TIFF file"
  494. if(tget_short(&buf, le) != 42){
  495. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  496. return -1;
  497. }
  498. /* parse image file directory */
  499. off = tget_long(&buf, le);
  500. if(orig_buf + off + 14 >= end_buf){
  501. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  502. return -1;
  503. }
  504. buf = orig_buf + off;
  505. entries = tget_short(&buf, le);
  506. for(i = 0; i < entries; i++){
  507. if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  508. return -1;
  509. buf += 12;
  510. }
  511. if(!s->stripdata && !s->stripoff){
  512. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  513. return -1;
  514. }
  515. /* now we have the data and may start decoding */
  516. if ((ret = init_image(s)) < 0)
  517. return ret;
  518. if(s->strips == 1 && !s->stripsize){
  519. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  520. s->stripsize = buf_size - s->stripoff;
  521. }
  522. stride = p->linesize[0];
  523. dst = p->data[0];
  524. for(i = 0; i < s->height; i += s->rps){
  525. if(s->stripsizes)
  526. ssize = tget(&s->stripsizes, s->sstype, s->le);
  527. else
  528. ssize = s->stripsize;
  529. if (ssize > buf_size) {
  530. av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
  531. return -1;
  532. }
  533. if(s->stripdata){
  534. soff = tget(&s->stripdata, s->sot, s->le);
  535. }else
  536. soff = s->stripoff;
  537. if (soff < 0) {
  538. av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
  539. return AVERROR(EINVAL);
  540. }
  541. if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
  542. break;
  543. dst += s->rps * stride;
  544. }
  545. if(s->predictor == 2){
  546. dst = p->data[0];
  547. soff = s->bpp >> 3;
  548. ssize = s->width * soff;
  549. for(i = 0; i < s->height; i++) {
  550. for(j = soff; j < ssize; j++)
  551. dst[j] += dst[j - soff];
  552. dst += stride;
  553. }
  554. }
  555. if(s->invert){
  556. uint8_t *src;
  557. int j;
  558. src = s->picture.data[0];
  559. for(j = 0; j < s->height; j++){
  560. for(i = 0; i < s->picture.linesize[0]; i++)
  561. src[i] = 255 - src[i];
  562. src += s->picture.linesize[0];
  563. }
  564. }
  565. *picture= *(AVFrame*)&s->picture;
  566. *data_size = sizeof(AVPicture);
  567. return buf_size;
  568. }
  569. static av_cold int tiff_init(AVCodecContext *avctx){
  570. TiffContext *s = avctx->priv_data;
  571. s->width = 0;
  572. s->height = 0;
  573. s->avctx = avctx;
  574. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  575. avctx->coded_frame= (AVFrame*)&s->picture;
  576. ff_lzw_decode_open(&s->lzw);
  577. ff_ccitt_unpack_init();
  578. return 0;
  579. }
  580. static av_cold int tiff_end(AVCodecContext *avctx)
  581. {
  582. TiffContext * const s = avctx->priv_data;
  583. ff_lzw_decode_close(&s->lzw);
  584. if(s->picture.data[0])
  585. avctx->release_buffer(avctx, &s->picture);
  586. return 0;
  587. }
  588. AVCodec ff_tiff_decoder = {
  589. "tiff",
  590. AVMEDIA_TYPE_VIDEO,
  591. CODEC_ID_TIFF,
  592. sizeof(TiffContext),
  593. tiff_init,
  594. NULL,
  595. tiff_end,
  596. decode_frame,
  597. CODEC_CAP_DR1,
  598. NULL,
  599. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  600. };