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.

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