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.

549 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. if(count > 4){
  230. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
  231. return -1;
  232. }
  233. switch(s->bpp*10 + count){
  234. case 11:
  235. s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
  236. break;
  237. case 81:
  238. s->avctx->pix_fmt = PIX_FMT_PAL8;
  239. break;
  240. case 243:
  241. s->avctx->pix_fmt = PIX_FMT_RGB24;
  242. break;
  243. case 161:
  244. s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
  245. break;
  246. case 324:
  247. s->avctx->pix_fmt = PIX_FMT_RGBA;
  248. break;
  249. case 483:
  250. s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
  251. break;
  252. default:
  253. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
  254. return -1;
  255. }
  256. if(s->width != s->avctx->width || s->height != s->avctx->height){
  257. if(avcodec_check_dimensions(s->avctx, s->width, s->height))
  258. return -1;
  259. avcodec_set_dimensions(s->avctx, s->width, s->height);
  260. }
  261. if(s->picture.data[0])
  262. s->avctx->release_buffer(s->avctx, &s->picture);
  263. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  264. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  265. return -1;
  266. }
  267. if(s->bpp == 8){
  268. /* make default grayscale pal */
  269. pal = (uint32_t *) s->picture.data[1];
  270. for(i = 0; i < 256; i++)
  271. pal[i] = i * 0x010101;
  272. }
  273. break;
  274. case TIFF_COMPR:
  275. s->compr = value;
  276. s->predictor = 0;
  277. switch(s->compr){
  278. case TIFF_RAW:
  279. case TIFF_PACKBITS:
  280. case TIFF_LZW:
  281. case TIFF_CCITT_RLE:
  282. break;
  283. case TIFF_G3:
  284. case TIFF_G4:
  285. s->fax_opts = 0;
  286. break;
  287. case TIFF_DEFLATE:
  288. case TIFF_ADOBE_DEFLATE:
  289. #if CONFIG_ZLIB
  290. break;
  291. #else
  292. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  293. return -1;
  294. #endif
  295. case TIFF_JPEG:
  296. case TIFF_NEWJPEG:
  297. av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
  298. return -1;
  299. default:
  300. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  301. return -1;
  302. }
  303. break;
  304. case TIFF_ROWSPERSTRIP:
  305. if(type == TIFF_LONG && value == -1)
  306. value = s->avctx->height;
  307. if(value < 1){
  308. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  309. return -1;
  310. }
  311. s->rps = value;
  312. break;
  313. case TIFF_STRIP_OFFS:
  314. if(count == 1){
  315. s->stripdata = NULL;
  316. s->stripoff = value;
  317. }else
  318. s->stripdata = start + off;
  319. s->strips = count;
  320. if(s->strips == 1) s->rps = s->height;
  321. s->sot = type;
  322. if(s->stripdata > end_buf){
  323. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  324. return -1;
  325. }
  326. break;
  327. case TIFF_STRIP_SIZE:
  328. if(count == 1){
  329. s->stripsizes = NULL;
  330. s->stripsize = value;
  331. s->strips = 1;
  332. }else{
  333. s->stripsizes = start + off;
  334. }
  335. s->strips = count;
  336. s->sstype = type;
  337. if(s->stripsizes > end_buf){
  338. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  339. return -1;
  340. }
  341. break;
  342. case TIFF_PREDICTOR:
  343. s->predictor = value;
  344. break;
  345. case TIFF_INVERT:
  346. switch(value){
  347. case 0:
  348. s->invert = 1;
  349. break;
  350. case 1:
  351. s->invert = 0;
  352. break;
  353. case 2:
  354. case 3:
  355. break;
  356. default:
  357. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  358. return -1;
  359. }
  360. break;
  361. case TIFF_PAL:
  362. if(s->avctx->pix_fmt != PIX_FMT_PAL8){
  363. av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
  364. return -1;
  365. }
  366. pal = (uint32_t *) s->picture.data[1];
  367. off = type_sizes[type];
  368. rp = buf;
  369. gp = buf + count / 3 * off;
  370. bp = buf + count / 3 * off * 2;
  371. off = (type_sizes[type] - 1) << 3;
  372. for(i = 0; i < count / 3; i++){
  373. j = (tget(&rp, type, s->le) >> off) << 16;
  374. j |= (tget(&gp, type, s->le) >> off) << 8;
  375. j |= tget(&bp, type, s->le) >> off;
  376. pal[i] = j;
  377. }
  378. break;
  379. case TIFF_PLANAR:
  380. if(value == 2){
  381. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  382. return -1;
  383. }
  384. break;
  385. case TIFF_T4OPTIONS:
  386. case TIFF_T6OPTIONS:
  387. s->fax_opts = value;
  388. break;
  389. }
  390. return 0;
  391. }
  392. static int decode_frame(AVCodecContext *avctx,
  393. void *data, int *data_size,
  394. AVPacket *avpkt)
  395. {
  396. const uint8_t *buf = avpkt->data;
  397. int buf_size = avpkt->size;
  398. TiffContext * const s = avctx->priv_data;
  399. AVFrame *picture = data;
  400. AVFrame * const p= (AVFrame*)&s->picture;
  401. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  402. int id, le, off;
  403. int i, j, entries;
  404. int stride, soff, ssize;
  405. uint8_t *dst;
  406. //parse image header
  407. id = AV_RL16(buf); buf += 2;
  408. if(id == 0x4949) le = 1;
  409. else if(id == 0x4D4D) le = 0;
  410. else{
  411. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  412. return -1;
  413. }
  414. s->le = le;
  415. s->invert = 0;
  416. s->compr = TIFF_RAW;
  417. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  418. // that further identifies the file as a TIFF file"
  419. if(tget_short(&buf, le) != 42){
  420. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  421. return -1;
  422. }
  423. /* parse image file directory */
  424. off = tget_long(&buf, le);
  425. if(orig_buf + off + 14 >= end_buf){
  426. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  427. return -1;
  428. }
  429. buf = orig_buf + off;
  430. entries = tget_short(&buf, le);
  431. for(i = 0; i < entries; i++){
  432. if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  433. return -1;
  434. buf += 12;
  435. }
  436. if(!s->stripdata && !s->stripoff){
  437. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  438. return -1;
  439. }
  440. /* now we have the data and may start decoding */
  441. if(!p->data[0]){
  442. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  443. return -1;
  444. }
  445. if(s->strips == 1 && !s->stripsize){
  446. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  447. s->stripsize = buf_size - s->stripoff;
  448. }
  449. stride = p->linesize[0];
  450. dst = p->data[0];
  451. for(i = 0; i < s->height; i += s->rps){
  452. if(s->stripsizes)
  453. ssize = tget(&s->stripsizes, s->sstype, s->le);
  454. else
  455. ssize = s->stripsize;
  456. if(s->stripdata){
  457. soff = tget(&s->stripdata, s->sot, s->le);
  458. }else
  459. soff = s->stripoff;
  460. if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
  461. break;
  462. dst += s->rps * stride;
  463. }
  464. if(s->predictor == 2){
  465. dst = p->data[0];
  466. soff = s->bpp >> 3;
  467. ssize = s->width * soff;
  468. for(i = 0; i < s->height; i++) {
  469. for(j = soff; j < ssize; j++)
  470. dst[j] += dst[j - soff];
  471. dst += stride;
  472. }
  473. }
  474. if(s->invert){
  475. uint8_t *src;
  476. int j;
  477. src = s->picture.data[0];
  478. for(j = 0; j < s->height; j++){
  479. for(i = 0; i < s->picture.linesize[0]; i++)
  480. src[i] = 255 - src[i];
  481. src += s->picture.linesize[0];
  482. }
  483. }
  484. *picture= *(AVFrame*)&s->picture;
  485. *data_size = sizeof(AVPicture);
  486. return buf_size;
  487. }
  488. static av_cold int tiff_init(AVCodecContext *avctx){
  489. TiffContext *s = avctx->priv_data;
  490. s->width = 0;
  491. s->height = 0;
  492. s->avctx = avctx;
  493. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  494. avctx->coded_frame= (AVFrame*)&s->picture;
  495. ff_lzw_decode_open(&s->lzw);
  496. ff_ccitt_unpack_init();
  497. return 0;
  498. }
  499. static av_cold int tiff_end(AVCodecContext *avctx)
  500. {
  501. TiffContext * const s = avctx->priv_data;
  502. ff_lzw_decode_close(&s->lzw);
  503. if(s->picture.data[0])
  504. avctx->release_buffer(avctx, &s->picture);
  505. return 0;
  506. }
  507. AVCodec tiff_decoder = {
  508. "tiff",
  509. CODEC_TYPE_VIDEO,
  510. CODEC_ID_TIFF,
  511. sizeof(TiffContext),
  512. tiff_init,
  513. NULL,
  514. tiff_end,
  515. decode_frame,
  516. CODEC_CAP_DR1,
  517. NULL,
  518. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  519. };