tiff.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. const uint8_t *buf, int buf_size)
  390. {
  391. TiffContext * const s = avctx->priv_data;
  392. AVFrame *picture = data;
  393. AVFrame * const p= (AVFrame*)&s->picture;
  394. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  395. int id, le, off;
  396. int i, j, entries;
  397. int stride, soff, ssize;
  398. uint8_t *dst;
  399. //parse image header
  400. id = AV_RL16(buf); buf += 2;
  401. if(id == 0x4949) le = 1;
  402. else if(id == 0x4D4D) le = 0;
  403. else{
  404. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  405. return -1;
  406. }
  407. s->le = le;
  408. s->invert = 0;
  409. s->compr = TIFF_RAW;
  410. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  411. // that further identifies the file as a TIFF file"
  412. if(tget_short(&buf, le) != 42){
  413. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  414. return -1;
  415. }
  416. /* parse image file directory */
  417. off = tget_long(&buf, le);
  418. if(orig_buf + off + 14 >= end_buf){
  419. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  420. return -1;
  421. }
  422. buf = orig_buf + off;
  423. entries = tget_short(&buf, le);
  424. for(i = 0; i < entries; i++){
  425. if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  426. return -1;
  427. buf += 12;
  428. }
  429. if(!s->stripdata && !s->stripoff){
  430. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  431. return -1;
  432. }
  433. /* now we have the data and may start decoding */
  434. if(!p->data[0]){
  435. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  436. return -1;
  437. }
  438. if(s->strips == 1 && !s->stripsize){
  439. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  440. s->stripsize = buf_size - s->stripoff;
  441. }
  442. stride = p->linesize[0];
  443. dst = p->data[0];
  444. for(i = 0; i < s->height; i += s->rps){
  445. if(s->stripsizes)
  446. ssize = tget(&s->stripsizes, s->sstype, s->le);
  447. else
  448. ssize = s->stripsize;
  449. if(s->stripdata){
  450. soff = tget(&s->stripdata, s->sot, s->le);
  451. }else
  452. soff = s->stripoff;
  453. if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
  454. break;
  455. dst += s->rps * stride;
  456. }
  457. if(s->predictor == 2){
  458. dst = p->data[0];
  459. soff = s->bpp >> 3;
  460. ssize = s->width * soff;
  461. for(i = 0; i < s->height; i++) {
  462. for(j = soff; j < ssize; j++)
  463. dst[j] += dst[j - soff];
  464. dst += stride;
  465. }
  466. }
  467. if(s->invert){
  468. uint8_t *src;
  469. int j;
  470. src = s->picture.data[0];
  471. for(j = 0; j < s->height; j++){
  472. for(i = 0; i < s->picture.linesize[0]; i++)
  473. src[i] = 255 - src[i];
  474. src += s->picture.linesize[0];
  475. }
  476. }
  477. *picture= *(AVFrame*)&s->picture;
  478. *data_size = sizeof(AVPicture);
  479. return buf_size;
  480. }
  481. static av_cold int tiff_init(AVCodecContext *avctx){
  482. TiffContext *s = avctx->priv_data;
  483. s->width = 0;
  484. s->height = 0;
  485. s->avctx = avctx;
  486. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  487. avctx->coded_frame= (AVFrame*)&s->picture;
  488. s->picture.data[0] = NULL;
  489. ff_lzw_decode_open(&s->lzw);
  490. ff_ccitt_unpack_init();
  491. return 0;
  492. }
  493. static av_cold int tiff_end(AVCodecContext *avctx)
  494. {
  495. TiffContext * const s = avctx->priv_data;
  496. ff_lzw_decode_close(&s->lzw);
  497. if(s->picture.data[0])
  498. avctx->release_buffer(avctx, &s->picture);
  499. return 0;
  500. }
  501. AVCodec tiff_decoder = {
  502. "tiff",
  503. CODEC_TYPE_VIDEO,
  504. CODEC_ID_TIFF,
  505. sizeof(TiffContext),
  506. tiff_init,
  507. NULL,
  508. tiff_end,
  509. decode_frame,
  510. 0,
  511. NULL,
  512. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  513. };