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.

509 lines
14KB

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