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.

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