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.

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