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.

532 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 ? LE_16(*p) : BE_16(*p);
  83. *p += 2;
  84. return v;
  85. }
  86. static int tget_long(uint8_t **p, int le){
  87. int v = le ? LE_32(*p) : BE_32(*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. s->sot = type;
  318. if(s->stripdata > end_buf){
  319. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  320. return -1;
  321. }
  322. break;
  323. case TIFF_STRIP_SIZE:
  324. if(count == 1){
  325. s->stripsizes = NULL;
  326. s->stripsize = value;
  327. s->strips = 1;
  328. }else{
  329. s->stripsizes = start + off;
  330. }
  331. s->strips = count;
  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. if(!pic->data[0]){
  337. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  338. return -1;
  339. }
  340. /* now we have the data and may start decoding */
  341. stride = pic->linesize[0];
  342. dst = pic->data[0];
  343. for(i = 0; i < s->height; i += s->rps){
  344. if(s->stripsizes)
  345. ssize = tget(&s->stripsizes, type, s->le);
  346. else
  347. ssize = s->stripsize;
  348. if(s->stripdata){
  349. soff = tget(&s->stripdata, s->sot, s->le);
  350. }else
  351. soff = s->stripoff;
  352. src = start + soff;
  353. if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
  354. break;
  355. dst += s->rps * stride;
  356. }
  357. break;
  358. case TIFF_PREDICTOR:
  359. if(!pic->data[0]){
  360. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  361. return -1;
  362. }
  363. if(value == 2){
  364. src = pic->data[0];
  365. stride = pic->linesize[0];
  366. soff = s->bpp >> 3;
  367. ssize = s->width * soff;
  368. for(i = 0; i < s->height; i++) {
  369. for(j = soff; j < ssize; j++)
  370. src[j] += src[j - soff];
  371. src += stride;
  372. }
  373. }
  374. break;
  375. case TIFF_INVERT:
  376. switch(value){
  377. case 0:
  378. s->invert = 1;
  379. break;
  380. case 1:
  381. s->invert = 0;
  382. break;
  383. case 2:
  384. case 3:
  385. break;
  386. default:
  387. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  388. return -1;
  389. }
  390. break;
  391. case TIFF_PAL:
  392. if(s->avctx->pix_fmt != PIX_FMT_PAL8){
  393. av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
  394. return -1;
  395. }
  396. pal = s->picture.data[1];
  397. off = type_sizes[type];
  398. rp = buf;
  399. gp = buf + count / 3 * off;
  400. bp = buf + count / 3 * off * 2;
  401. off = (type_sizes[type] - 1) << 3;
  402. for(i = 0; i < count / 3; i++){
  403. j = (tget(&rp, type, s->le) >> off) << 16;
  404. j |= (tget(&gp, type, s->le) >> off) << 8;
  405. j |= tget(&bp, type, s->le) >> off;
  406. pal[i] = j;
  407. }
  408. break;
  409. case TIFF_PLANAR:
  410. if(value == 2){
  411. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  412. return -1;
  413. }
  414. break;
  415. }
  416. return 0;
  417. }
  418. static int decode_frame(AVCodecContext *avctx,
  419. void *data, int *data_size,
  420. uint8_t *buf, int buf_size)
  421. {
  422. TiffContext * const s = avctx->priv_data;
  423. AVFrame *picture = data;
  424. AVFrame * const p= (AVFrame*)&s->picture;
  425. uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  426. int id, le, off;
  427. int i, entries;
  428. //parse image header
  429. id = LE_16(buf); buf += 2;
  430. if(id == 0x4949) le = 1;
  431. else if(id == 0x4D4D) le = 0;
  432. else{
  433. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  434. return -1;
  435. }
  436. s->le = le;
  437. s->invert = 0;
  438. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  439. // that further identifies the file as a TIFF file"
  440. if(tget_short(&buf, le) != 42){
  441. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  442. return -1;
  443. }
  444. /* parse image file directory */
  445. off = tget_long(&buf, le);
  446. if(orig_buf + off + 14 >= end_buf){
  447. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  448. return -1;
  449. }
  450. buf = orig_buf + off;
  451. entries = tget_short(&buf, le);
  452. for(i = 0; i < entries; i++){
  453. if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
  454. return -1;
  455. buf += 12;
  456. }
  457. if(s->invert){
  458. uint8_t *src;
  459. int j;
  460. src = s->picture.data[0];
  461. for(j = 0; j < s->height; j++){
  462. for(i = 0; i < s->picture.linesize[0]; i++)
  463. src[i] = 255 - src[i];
  464. src += s->picture.linesize[0];
  465. }
  466. }
  467. *picture= *(AVFrame*)&s->picture;
  468. *data_size = sizeof(AVPicture);
  469. return buf_size;
  470. }
  471. static int tiff_init(AVCodecContext *avctx){
  472. TiffContext *s = avctx->priv_data;
  473. s->width = 0;
  474. s->height = 0;
  475. s->avctx = avctx;
  476. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  477. avctx->coded_frame= (AVFrame*)&s->picture;
  478. s->picture.data[0] = NULL;
  479. ff_lzw_decode_open(&s->lzw);
  480. return 0;
  481. }
  482. static int tiff_end(AVCodecContext *avctx)
  483. {
  484. TiffContext * const s = avctx->priv_data;
  485. ff_lzw_decode_close(&s->lzw);
  486. if(s->picture.data[0])
  487. avctx->release_buffer(avctx, &s->picture);
  488. return 0;
  489. }
  490. AVCodec tiff_decoder = {
  491. "tiff",
  492. CODEC_TYPE_VIDEO,
  493. CODEC_ID_TIFF,
  494. sizeof(TiffContext),
  495. tiff_init,
  496. NULL,
  497. tiff_end,
  498. decode_frame,
  499. 0,
  500. NULL
  501. };