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.

615 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;
  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 >> 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. memcpy(dst, src, width);
  160. src += width;
  161. break;
  162. case TIFF_PACKBITS:
  163. for(pixels = 0; pixels < width;){
  164. code = (int8_t)*src++;
  165. if(code >= 0){
  166. code++;
  167. if(pixels + code > width){
  168. av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
  169. return -1;
  170. }
  171. memcpy(dst + pixels, src, code);
  172. src += code;
  173. pixels += code;
  174. }else if(code != -128){ // -127..-1
  175. code = (-code) + 1;
  176. if(pixels + code > width){
  177. av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  178. return -1;
  179. }
  180. c = *src++;
  181. memset(dst + pixels, c, code);
  182. pixels += code;
  183. }
  184. }
  185. break;
  186. case TIFF_LZW:
  187. pixels = ff_lzw_decode(s->lzw, dst, width);
  188. if(pixels < width){
  189. av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
  190. return -1;
  191. }
  192. break;
  193. }
  194. dst += stride;
  195. }
  196. return 0;
  197. }
  198. static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
  199. {
  200. int tag, type, count, off, value = 0;
  201. int i, j;
  202. uint32_t *pal;
  203. const uint8_t *rp, *gp, *bp;
  204. tag = tget_short(&buf, s->le);
  205. type = tget_short(&buf, s->le);
  206. count = tget_long(&buf, s->le);
  207. off = tget_long(&buf, s->le);
  208. if(count == 1){
  209. switch(type){
  210. case TIFF_BYTE:
  211. case TIFF_SHORT:
  212. buf -= 4;
  213. value = tget(&buf, type, s->le);
  214. buf = NULL;
  215. break;
  216. case TIFF_LONG:
  217. value = off;
  218. buf = NULL;
  219. break;
  220. case TIFF_STRING:
  221. if(count <= 4){
  222. buf -= 4;
  223. break;
  224. }
  225. default:
  226. value = -1;
  227. buf = start + off;
  228. }
  229. }else if(type_sizes[type] * count <= 4){
  230. buf -= 4;
  231. }else{
  232. buf = start + off;
  233. }
  234. if(buf && (buf < start || buf > end_buf)){
  235. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  236. return -1;
  237. }
  238. switch(tag){
  239. case TIFF_WIDTH:
  240. s->width = value;
  241. break;
  242. case TIFF_HEIGHT:
  243. s->height = value;
  244. break;
  245. case TIFF_BPP:
  246. if(count > 4){
  247. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
  248. return -1;
  249. }
  250. if(count == 1) s->bpp = value;
  251. else{
  252. switch(type){
  253. case TIFF_BYTE:
  254. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  255. break;
  256. case TIFF_SHORT:
  257. case TIFF_LONG:
  258. s->bpp = 0;
  259. for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
  260. break;
  261. default:
  262. s->bpp = -1;
  263. }
  264. }
  265. switch(s->bpp*10 + count){
  266. case 11:
  267. s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
  268. break;
  269. case 81:
  270. s->avctx->pix_fmt = PIX_FMT_PAL8;
  271. break;
  272. case 243:
  273. s->avctx->pix_fmt = PIX_FMT_RGB24;
  274. break;
  275. case 161:
  276. s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
  277. break;
  278. case 324:
  279. s->avctx->pix_fmt = PIX_FMT_RGBA;
  280. break;
  281. case 483:
  282. s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
  283. break;
  284. default:
  285. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
  286. return -1;
  287. }
  288. if(s->width != s->avctx->width || s->height != s->avctx->height){
  289. if(av_image_check_size(s->width, s->height, 0, s->avctx))
  290. return -1;
  291. avcodec_set_dimensions(s->avctx, s->width, s->height);
  292. }
  293. if(s->picture.data[0])
  294. s->avctx->release_buffer(s->avctx, &s->picture);
  295. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  296. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  297. return -1;
  298. }
  299. if(s->bpp == 8){
  300. /* make default grayscale pal */
  301. pal = (uint32_t *) s->picture.data[1];
  302. for(i = 0; i < 256; i++)
  303. pal[i] = i * 0x010101;
  304. }
  305. break;
  306. case TIFF_COMPR:
  307. s->compr = value;
  308. s->predictor = 0;
  309. switch(s->compr){
  310. case TIFF_RAW:
  311. case TIFF_PACKBITS:
  312. case TIFF_LZW:
  313. case TIFF_CCITT_RLE:
  314. break;
  315. case TIFF_G3:
  316. case TIFF_G4:
  317. s->fax_opts = 0;
  318. break;
  319. case TIFF_DEFLATE:
  320. case TIFF_ADOBE_DEFLATE:
  321. #if CONFIG_ZLIB
  322. break;
  323. #else
  324. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  325. return -1;
  326. #endif
  327. case TIFF_JPEG:
  328. case TIFF_NEWJPEG:
  329. av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
  330. return -1;
  331. default:
  332. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  333. return -1;
  334. }
  335. break;
  336. case TIFF_ROWSPERSTRIP:
  337. if(type == TIFF_LONG && value == -1)
  338. value = s->avctx->height;
  339. if(value < 1){
  340. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  341. return -1;
  342. }
  343. s->rps = value;
  344. break;
  345. case TIFF_STRIP_OFFS:
  346. if(count == 1){
  347. s->stripdata = NULL;
  348. s->stripoff = value;
  349. }else
  350. s->stripdata = start + off;
  351. s->strips = count;
  352. if(s->strips == 1) s->rps = s->height;
  353. s->sot = type;
  354. if(s->stripdata > end_buf){
  355. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  356. return -1;
  357. }
  358. break;
  359. case TIFF_STRIP_SIZE:
  360. if(count == 1){
  361. s->stripsizes = NULL;
  362. s->stripsize = value;
  363. s->strips = 1;
  364. }else{
  365. s->stripsizes = start + off;
  366. }
  367. s->strips = count;
  368. s->sstype = type;
  369. if(s->stripsizes > end_buf){
  370. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  371. return -1;
  372. }
  373. break;
  374. case TIFF_PREDICTOR:
  375. s->predictor = value;
  376. break;
  377. case TIFF_INVERT:
  378. switch(value){
  379. case 0:
  380. s->invert = 1;
  381. break;
  382. case 1:
  383. s->invert = 0;
  384. break;
  385. case 2:
  386. case 3:
  387. break;
  388. default:
  389. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  390. return -1;
  391. }
  392. break;
  393. case TIFF_FILL_ORDER:
  394. if(value < 1 || value > 2){
  395. av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
  396. value = 1;
  397. }
  398. s->fill_order = value - 1;
  399. break;
  400. case TIFF_PAL:
  401. if(s->avctx->pix_fmt != PIX_FMT_PAL8){
  402. av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
  403. return -1;
  404. }
  405. pal = (uint32_t *) s->picture.data[1];
  406. off = type_sizes[type];
  407. rp = buf;
  408. gp = buf + count / 3 * off;
  409. bp = buf + count / 3 * off * 2;
  410. off = (type_sizes[type] - 1) << 3;
  411. for(i = 0; i < count / 3; i++){
  412. j = 0xff << 24;
  413. j |= (tget(&rp, type, s->le) >> off) << 16;
  414. j |= (tget(&gp, type, s->le) >> off) << 8;
  415. j |= tget(&bp, type, s->le) >> off;
  416. pal[i] = j;
  417. }
  418. break;
  419. case TIFF_PLANAR:
  420. if(value == 2){
  421. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  422. return -1;
  423. }
  424. break;
  425. case TIFF_T4OPTIONS:
  426. if(s->compr == TIFF_G3)
  427. s->fax_opts = value;
  428. break;
  429. case TIFF_T6OPTIONS:
  430. if(s->compr == TIFF_G4)
  431. s->fax_opts = value;
  432. break;
  433. }
  434. return 0;
  435. }
  436. static int decode_frame(AVCodecContext *avctx,
  437. void *data, int *data_size,
  438. AVPacket *avpkt)
  439. {
  440. const uint8_t *buf = avpkt->data;
  441. int buf_size = avpkt->size;
  442. TiffContext * const s = avctx->priv_data;
  443. AVFrame *picture = data;
  444. AVFrame * const p= (AVFrame*)&s->picture;
  445. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  446. int id, le, off;
  447. int i, j, entries;
  448. int stride, soff, ssize;
  449. uint8_t *dst;
  450. //parse image header
  451. id = AV_RL16(buf); buf += 2;
  452. if(id == 0x4949) le = 1;
  453. else if(id == 0x4D4D) le = 0;
  454. else{
  455. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  456. return -1;
  457. }
  458. s->le = le;
  459. s->invert = 0;
  460. s->compr = TIFF_RAW;
  461. s->fill_order = 0;
  462. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  463. // that further identifies the file as a TIFF file"
  464. if(tget_short(&buf, le) != 42){
  465. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  466. return -1;
  467. }
  468. /* parse image file directory */
  469. off = tget_long(&buf, le);
  470. if(orig_buf + off + 14 >= end_buf){
  471. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  472. return -1;
  473. }
  474. buf = orig_buf + off;
  475. entries = tget_short(&buf, le);
  476. for(i = 0; i < entries; i++){
  477. if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  478. return -1;
  479. buf += 12;
  480. }
  481. if(!s->stripdata && !s->stripoff){
  482. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  483. return -1;
  484. }
  485. /* now we have the data and may start decoding */
  486. if(!p->data[0]){
  487. s->bpp = 1;
  488. avctx->pix_fmt = PIX_FMT_MONOBLACK;
  489. if(s->width != s->avctx->width || s->height != s->avctx->height){
  490. if(av_image_check_size(s->width, s->height, 0, s->avctx))
  491. return -1;
  492. avcodec_set_dimensions(s->avctx, s->width, s->height);
  493. }
  494. if(s->picture.data[0])
  495. s->avctx->release_buffer(s->avctx, &s->picture);
  496. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  497. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  498. return -1;
  499. }
  500. }
  501. if(s->strips == 1 && !s->stripsize){
  502. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  503. s->stripsize = buf_size - s->stripoff;
  504. }
  505. stride = p->linesize[0];
  506. dst = p->data[0];
  507. for(i = 0; i < s->height; i += s->rps){
  508. if(s->stripsizes)
  509. ssize = tget(&s->stripsizes, s->sstype, s->le);
  510. else
  511. ssize = s->stripsize;
  512. if (ssize > buf_size) {
  513. av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
  514. return -1;
  515. }
  516. if(s->stripdata){
  517. soff = tget(&s->stripdata, s->sot, s->le);
  518. }else
  519. soff = s->stripoff;
  520. if (soff < 0) {
  521. av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
  522. return AVERROR(EINVAL);
  523. }
  524. if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
  525. break;
  526. dst += s->rps * stride;
  527. }
  528. if(s->predictor == 2){
  529. dst = p->data[0];
  530. soff = s->bpp >> 3;
  531. ssize = s->width * soff;
  532. for(i = 0; i < s->height; i++) {
  533. for(j = soff; j < ssize; j++)
  534. dst[j] += dst[j - soff];
  535. dst += stride;
  536. }
  537. }
  538. if(s->invert){
  539. uint8_t *src;
  540. int j;
  541. src = s->picture.data[0];
  542. for(j = 0; j < s->height; j++){
  543. for(i = 0; i < s->picture.linesize[0]; i++)
  544. src[i] = 255 - src[i];
  545. src += s->picture.linesize[0];
  546. }
  547. }
  548. *picture= *(AVFrame*)&s->picture;
  549. *data_size = sizeof(AVPicture);
  550. return buf_size;
  551. }
  552. static av_cold int tiff_init(AVCodecContext *avctx){
  553. TiffContext *s = avctx->priv_data;
  554. s->width = 0;
  555. s->height = 0;
  556. s->avctx = avctx;
  557. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  558. avctx->coded_frame= (AVFrame*)&s->picture;
  559. ff_lzw_decode_open(&s->lzw);
  560. ff_ccitt_unpack_init();
  561. return 0;
  562. }
  563. static av_cold int tiff_end(AVCodecContext *avctx)
  564. {
  565. TiffContext * const s = avctx->priv_data;
  566. ff_lzw_decode_close(&s->lzw);
  567. if(s->picture.data[0])
  568. avctx->release_buffer(avctx, &s->picture);
  569. return 0;
  570. }
  571. AVCodec ff_tiff_decoder = {
  572. "tiff",
  573. AVMEDIA_TYPE_VIDEO,
  574. CODEC_ID_TIFF,
  575. sizeof(TiffContext),
  576. tiff_init,
  577. NULL,
  578. tiff_end,
  579. decode_frame,
  580. CODEC_CAP_DR1,
  581. NULL,
  582. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  583. };