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.

545 lines
16KB

  1. /*
  2. * TIFF image decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * TIFF image decoder
  23. * @file libavcodec/tiff.c
  24. * @author Konstantin Shishkov
  25. */
  26. #include "avcodec.h"
  27. #if CONFIG_ZLIB
  28. #include <zlib.h>
  29. #endif
  30. #include "lzw.h"
  31. #include "tiff.h"
  32. #include "faxcompr.h"
  33. typedef struct TiffContext {
  34. AVCodecContext *avctx;
  35. AVFrame picture;
  36. int width, height;
  37. unsigned int bpp;
  38. int le;
  39. int compr;
  40. int invert;
  41. int fax_opts;
  42. int predictor;
  43. int strips, rps, sstype;
  44. int sot;
  45. const uint8_t* stripdata;
  46. const uint8_t* stripsizes;
  47. int stripsize, stripoff;
  48. LZWState *lzw;
  49. } TiffContext;
  50. static int tget_short(const uint8_t **p, int le){
  51. int v = le ? AV_RL16(*p) : AV_RB16(*p);
  52. *p += 2;
  53. return v;
  54. }
  55. static int tget_long(const uint8_t **p, int le){
  56. int v = le ? AV_RL32(*p) : AV_RB32(*p);
  57. *p += 4;
  58. return v;
  59. }
  60. static int tget(const uint8_t **p, int type, int le){
  61. switch(type){
  62. case TIFF_BYTE : return *(*p)++;
  63. case TIFF_SHORT: return tget_short(p, le);
  64. case TIFF_LONG : return tget_long (p, le);
  65. default : return -1;
  66. }
  67. }
  68. static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
  69. int c, line, pixels, code;
  70. const uint8_t *ssrc = src;
  71. int width = s->width * s->bpp >> 3;
  72. #if CONFIG_ZLIB
  73. uint8_t *zbuf; unsigned long outlen;
  74. if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
  75. outlen = width * lines;
  76. zbuf = av_malloc(outlen);
  77. if(uncompress(zbuf, &outlen, src, size) != Z_OK){
  78. av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
  79. av_free(zbuf);
  80. return -1;
  81. }
  82. src = zbuf;
  83. for(line = 0; line < lines; line++){
  84. memcpy(dst, src, width);
  85. dst += stride;
  86. src += width;
  87. }
  88. av_free(zbuf);
  89. return 0;
  90. }
  91. #endif
  92. if(s->compr == TIFF_LZW){
  93. if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
  94. av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
  95. return -1;
  96. }
  97. }
  98. if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
  99. int i, ret = 0;
  100. uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  101. if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
  102. av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
  103. return -1;
  104. }
  105. for(i = 0; i < size; i++)
  106. src2[i] = ff_reverse[src[i]];
  107. memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  108. if(s->compr == TIFF_G3 && !(s->fax_opts & 1))
  109. s->compr = TIFF_CCITT_RLE;
  110. switch(s->compr){
  111. case TIFF_CCITT_RLE:
  112. case TIFF_G3:
  113. case TIFF_G4:
  114. ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr);
  115. break;
  116. }
  117. av_free(src2);
  118. return ret;
  119. }
  120. for(line = 0; line < lines; line++){
  121. if(src - ssrc > size){
  122. av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
  123. return -1;
  124. }
  125. switch(s->compr){
  126. case TIFF_RAW:
  127. memcpy(dst, src, width);
  128. src += width;
  129. break;
  130. case TIFF_PACKBITS:
  131. for(pixels = 0; pixels < width;){
  132. code = (int8_t)*src++;
  133. if(code >= 0){
  134. code++;
  135. if(pixels + code > width){
  136. av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
  137. return -1;
  138. }
  139. memcpy(dst + pixels, src, code);
  140. src += code;
  141. pixels += code;
  142. }else if(code != -128){ // -127..-1
  143. code = (-code) + 1;
  144. if(pixels + code > width){
  145. av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  146. return -1;
  147. }
  148. c = *src++;
  149. memset(dst + pixels, c, code);
  150. pixels += code;
  151. }
  152. }
  153. break;
  154. case TIFF_LZW:
  155. pixels = ff_lzw_decode(s->lzw, dst, width);
  156. if(pixels < width){
  157. av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
  158. return -1;
  159. }
  160. break;
  161. }
  162. dst += stride;
  163. }
  164. return 0;
  165. }
  166. static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
  167. {
  168. int tag, type, count, off, value = 0;
  169. int i, j;
  170. uint32_t *pal;
  171. const uint8_t *rp, *gp, *bp;
  172. tag = tget_short(&buf, s->le);
  173. type = tget_short(&buf, s->le);
  174. count = tget_long(&buf, s->le);
  175. off = tget_long(&buf, s->le);
  176. if(count == 1){
  177. switch(type){
  178. case TIFF_BYTE:
  179. case TIFF_SHORT:
  180. buf -= 4;
  181. value = tget(&buf, type, s->le);
  182. buf = NULL;
  183. break;
  184. case TIFF_LONG:
  185. value = off;
  186. buf = NULL;
  187. break;
  188. case TIFF_STRING:
  189. if(count <= 4){
  190. buf -= 4;
  191. break;
  192. }
  193. default:
  194. value = -1;
  195. buf = start + off;
  196. }
  197. }else if(type_sizes[type] * count <= 4){
  198. buf -= 4;
  199. }else{
  200. buf = start + off;
  201. }
  202. if(buf && (buf < start || buf > end_buf)){
  203. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  204. return -1;
  205. }
  206. switch(tag){
  207. case TIFF_WIDTH:
  208. s->width = value;
  209. break;
  210. case TIFF_HEIGHT:
  211. s->height = value;
  212. break;
  213. case TIFF_BPP:
  214. if(count == 1) s->bpp = value;
  215. else{
  216. switch(type){
  217. case TIFF_BYTE:
  218. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  219. break;
  220. case TIFF_SHORT:
  221. case TIFF_LONG:
  222. s->bpp = 0;
  223. for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
  224. break;
  225. default:
  226. s->bpp = -1;
  227. }
  228. }
  229. switch(s->bpp){
  230. case 1:
  231. s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
  232. break;
  233. case 8:
  234. s->avctx->pix_fmt = PIX_FMT_PAL8;
  235. break;
  236. case 24:
  237. s->avctx->pix_fmt = PIX_FMT_RGB24;
  238. break;
  239. case 16:
  240. if(count == 1){
  241. s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
  242. }else{
  243. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
  244. return -1;
  245. }
  246. break;
  247. default:
  248. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
  249. return -1;
  250. }
  251. if(s->width != s->avctx->width || s->height != s->avctx->height){
  252. if(avcodec_check_dimensions(s->avctx, s->width, s->height))
  253. return -1;
  254. avcodec_set_dimensions(s->avctx, s->width, s->height);
  255. }
  256. if(s->picture.data[0])
  257. s->avctx->release_buffer(s->avctx, &s->picture);
  258. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  259. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  260. return -1;
  261. }
  262. if(s->bpp == 8){
  263. /* make default grayscale pal */
  264. pal = (uint32_t *) s->picture.data[1];
  265. for(i = 0; i < 256; i++)
  266. pal[i] = i * 0x010101;
  267. }
  268. break;
  269. case TIFF_COMPR:
  270. s->compr = value;
  271. s->predictor = 0;
  272. switch(s->compr){
  273. case TIFF_RAW:
  274. case TIFF_PACKBITS:
  275. case TIFF_LZW:
  276. case TIFF_CCITT_RLE:
  277. break;
  278. case TIFF_G3:
  279. case TIFF_G4:
  280. s->fax_opts = 0;
  281. break;
  282. case TIFF_DEFLATE:
  283. case TIFF_ADOBE_DEFLATE:
  284. #if CONFIG_ZLIB
  285. break;
  286. #else
  287. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  288. return -1;
  289. #endif
  290. case TIFF_JPEG:
  291. case TIFF_NEWJPEG:
  292. av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
  293. return -1;
  294. default:
  295. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  296. return -1;
  297. }
  298. break;
  299. case TIFF_ROWSPERSTRIP:
  300. if(type == TIFF_LONG && value == -1)
  301. value = s->avctx->height;
  302. if(value < 1){
  303. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  304. return -1;
  305. }
  306. s->rps = value;
  307. break;
  308. case TIFF_STRIP_OFFS:
  309. if(count == 1){
  310. s->stripdata = NULL;
  311. s->stripoff = value;
  312. }else
  313. s->stripdata = start + off;
  314. s->strips = count;
  315. if(s->strips == 1) s->rps = s->height;
  316. s->sot = type;
  317. if(s->stripdata > end_buf){
  318. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  319. return -1;
  320. }
  321. break;
  322. case TIFF_STRIP_SIZE:
  323. if(count == 1){
  324. s->stripsizes = NULL;
  325. s->stripsize = value;
  326. s->strips = 1;
  327. }else{
  328. s->stripsizes = start + off;
  329. }
  330. s->strips = count;
  331. s->sstype = type;
  332. if(s->stripsizes > end_buf){
  333. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  334. return -1;
  335. }
  336. break;
  337. case TIFF_PREDICTOR:
  338. s->predictor = value;
  339. break;
  340. case TIFF_INVERT:
  341. switch(value){
  342. case 0:
  343. s->invert = 1;
  344. break;
  345. case 1:
  346. s->invert = 0;
  347. break;
  348. case 2:
  349. case 3:
  350. break;
  351. default:
  352. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  353. return -1;
  354. }
  355. break;
  356. case TIFF_PAL:
  357. if(s->avctx->pix_fmt != PIX_FMT_PAL8){
  358. av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
  359. return -1;
  360. }
  361. pal = (uint32_t *) s->picture.data[1];
  362. off = type_sizes[type];
  363. rp = buf;
  364. gp = buf + count / 3 * off;
  365. bp = buf + count / 3 * off * 2;
  366. off = (type_sizes[type] - 1) << 3;
  367. for(i = 0; i < count / 3; i++){
  368. j = (tget(&rp, type, s->le) >> off) << 16;
  369. j |= (tget(&gp, type, s->le) >> off) << 8;
  370. j |= tget(&bp, type, s->le) >> off;
  371. pal[i] = j;
  372. }
  373. break;
  374. case TIFF_PLANAR:
  375. if(value == 2){
  376. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  377. return -1;
  378. }
  379. break;
  380. case TIFF_T4OPTIONS:
  381. case TIFF_T6OPTIONS:
  382. s->fax_opts = value;
  383. break;
  384. }
  385. return 0;
  386. }
  387. static int decode_frame(AVCodecContext *avctx,
  388. void *data, int *data_size,
  389. AVPacket *avpkt)
  390. {
  391. const uint8_t *buf = avpkt->data;
  392. int buf_size = avpkt->size;
  393. TiffContext * const s = avctx->priv_data;
  394. AVFrame *picture = data;
  395. AVFrame * const p= (AVFrame*)&s->picture;
  396. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  397. int id, le, off;
  398. int i, j, entries;
  399. int stride, soff, ssize;
  400. uint8_t *dst;
  401. //parse image header
  402. id = AV_RL16(buf); buf += 2;
  403. if(id == 0x4949) le = 1;
  404. else if(id == 0x4D4D) le = 0;
  405. else{
  406. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  407. return -1;
  408. }
  409. s->le = le;
  410. s->invert = 0;
  411. s->compr = TIFF_RAW;
  412. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  413. // that further identifies the file as a TIFF file"
  414. if(tget_short(&buf, le) != 42){
  415. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  416. return -1;
  417. }
  418. /* parse image file directory */
  419. off = tget_long(&buf, le);
  420. if(orig_buf + off + 14 >= end_buf){
  421. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  422. return -1;
  423. }
  424. buf = orig_buf + off;
  425. entries = tget_short(&buf, le);
  426. for(i = 0; i < entries; i++){
  427. if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  428. return -1;
  429. buf += 12;
  430. }
  431. if(!s->stripdata && !s->stripoff){
  432. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  433. return -1;
  434. }
  435. /* now we have the data and may start decoding */
  436. if(!p->data[0]){
  437. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  438. return -1;
  439. }
  440. if(s->strips == 1 && !s->stripsize){
  441. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  442. s->stripsize = buf_size - s->stripoff;
  443. }
  444. stride = p->linesize[0];
  445. dst = p->data[0];
  446. for(i = 0; i < s->height; i += s->rps){
  447. if(s->stripsizes)
  448. ssize = tget(&s->stripsizes, s->sstype, s->le);
  449. else
  450. ssize = s->stripsize;
  451. if(s->stripdata){
  452. soff = tget(&s->stripdata, s->sot, s->le);
  453. }else
  454. soff = s->stripoff;
  455. if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
  456. break;
  457. dst += s->rps * stride;
  458. }
  459. if(s->predictor == 2){
  460. dst = p->data[0];
  461. soff = s->bpp >> 3;
  462. ssize = s->width * soff;
  463. for(i = 0; i < s->height; i++) {
  464. for(j = soff; j < ssize; j++)
  465. dst[j] += dst[j - soff];
  466. dst += stride;
  467. }
  468. }
  469. if(s->invert){
  470. uint8_t *src;
  471. int j;
  472. src = s->picture.data[0];
  473. for(j = 0; j < s->height; j++){
  474. for(i = 0; i < s->picture.linesize[0]; i++)
  475. src[i] = 255 - src[i];
  476. src += s->picture.linesize[0];
  477. }
  478. }
  479. *picture= *(AVFrame*)&s->picture;
  480. *data_size = sizeof(AVPicture);
  481. return buf_size;
  482. }
  483. static av_cold int tiff_init(AVCodecContext *avctx){
  484. TiffContext *s = avctx->priv_data;
  485. s->width = 0;
  486. s->height = 0;
  487. s->avctx = avctx;
  488. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  489. avctx->coded_frame= (AVFrame*)&s->picture;
  490. s->picture.data[0] = NULL;
  491. ff_lzw_decode_open(&s->lzw);
  492. ff_ccitt_unpack_init();
  493. return 0;
  494. }
  495. static av_cold int tiff_end(AVCodecContext *avctx)
  496. {
  497. TiffContext * const s = avctx->priv_data;
  498. ff_lzw_decode_close(&s->lzw);
  499. if(s->picture.data[0])
  500. avctx->release_buffer(avctx, &s->picture);
  501. return 0;
  502. }
  503. AVCodec tiff_decoder = {
  504. "tiff",
  505. CODEC_TYPE_VIDEO,
  506. CODEC_ID_TIFF,
  507. sizeof(TiffContext),
  508. tiff_init,
  509. NULL,
  510. tiff_end,
  511. decode_frame,
  512. 0,
  513. NULL,
  514. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  515. };