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.

644 lines
19KB

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