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.

533 lines
15KB

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