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.

507 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. if(lines != s->height){
  103. av_log(s->avctx, AV_LOG_ERROR, "This decoder won't decode ZLib-packed TIFF with %i lines per strip\n", lines);
  104. return -1;
  105. }
  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{
  201. buf = start + off;
  202. }
  203. if(buf && (buf < start || buf > end_buf)){
  204. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  205. return -1;
  206. }
  207. switch(tag){
  208. case TIFF_WIDTH:
  209. s->width = value;
  210. break;
  211. case TIFF_HEIGHT:
  212. s->height = value;
  213. break;
  214. case TIFF_BPP:
  215. if(count == 1) s->bpp = value;
  216. else{
  217. switch(type){
  218. case TIFF_BYTE:
  219. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  220. break;
  221. case TIFF_SHORT:
  222. case TIFF_LONG:
  223. s->bpp = 0;
  224. for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
  225. break;
  226. default:
  227. s->bpp = -1;
  228. }
  229. }
  230. switch(s->bpp){
  231. case 8:
  232. s->avctx->pix_fmt = PIX_FMT_PAL8;
  233. break;
  234. case 24:
  235. s->avctx->pix_fmt = PIX_FMT_RGB24;
  236. break;
  237. default:
  238. av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported (this bpp=%i)\n", s->bpp);
  239. return -1;
  240. }
  241. if(s->width != s->avctx->width || s->height != s->avctx->height){
  242. if(avcodec_check_dimensions(s->avctx, s->width, s->height))
  243. return -1;
  244. avcodec_set_dimensions(s->avctx, s->width, s->height);
  245. }
  246. if(s->picture.data[0])
  247. s->avctx->release_buffer(s->avctx, &s->picture);
  248. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  249. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  250. return -1;
  251. }
  252. if(s->bpp == 8){
  253. /* make default grayscale pal */
  254. pal = s->picture.data[1];
  255. for(i = 0; i < 256; i++)
  256. pal[i] = i * 0x010101;
  257. }
  258. break;
  259. case TIFF_COMPR:
  260. s->compr = value;
  261. switch(s->compr){
  262. case TIFF_RAW:
  263. case TIFF_PACKBITS:
  264. case TIFF_LZW:
  265. break;
  266. case TIFF_DEFLATE:
  267. case TIFF_ADOBE_DEFLATE:
  268. #ifdef CONFIG_ZLIB
  269. break;
  270. #else
  271. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  272. return -1;
  273. #endif
  274. case TIFF_G3:
  275. av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
  276. return -1;
  277. case TIFF_G4:
  278. av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
  279. return -1;
  280. case TIFF_CCITT_RLE:
  281. av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
  282. return -1;
  283. case TIFF_JPEG:
  284. case TIFF_NEWJPEG:
  285. av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
  286. return -1;
  287. default:
  288. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  289. return -1;
  290. }
  291. break;
  292. case TIFF_ROWSPERSTRIP:
  293. if(value < 1){
  294. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  295. return -1;
  296. }
  297. s->rps = value;
  298. break;
  299. case TIFF_STRIP_OFFS:
  300. if(count == 1){
  301. s->stripdata = NULL;
  302. s->stripoff = value;
  303. }else
  304. s->stripdata = start + off;
  305. s->strips = count;
  306. s->sot = type;
  307. if(s->stripdata > end_buf){
  308. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  309. return -1;
  310. }
  311. break;
  312. case TIFF_STRIP_SIZE:
  313. if(count == 1){
  314. s->stripsizes = NULL;
  315. s->stripsize = value;
  316. s->strips = 1;
  317. }else{
  318. s->stripsizes = start + off;
  319. }
  320. s->strips = count;
  321. if(s->stripsizes > end_buf){
  322. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  323. return -1;
  324. }
  325. if(!pic->data[0]){
  326. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  327. return -1;
  328. }
  329. /* now we have the data and may start decoding */
  330. stride = pic->linesize[0];
  331. dst = pic->data[0];
  332. for(i = 0; i < s->height; i += s->rps){
  333. if(s->stripsizes)
  334. ssize = tget(&s->stripsizes, type, s->le);
  335. else
  336. ssize = s->stripsize;
  337. if(s->stripdata){
  338. soff = tget(&s->stripdata, s->sot, s->le);
  339. }else
  340. soff = s->stripoff;
  341. src = start + soff;
  342. if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
  343. break;
  344. dst += s->rps * stride;
  345. }
  346. break;
  347. case TIFF_PREDICTOR:
  348. if(!pic->data[0]){
  349. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  350. return -1;
  351. }
  352. if(value == 2){
  353. src = pic->data[0];
  354. stride = pic->linesize[0];
  355. soff = s->bpp >> 3;
  356. ssize = s->width * soff;
  357. for(i = 0; i < s->height; i++) {
  358. for(j = soff; j < ssize; j++)
  359. src[j] += src[j - soff];
  360. src += stride;
  361. }
  362. }
  363. break;
  364. case TIFF_INVERT:
  365. switch(value){
  366. case 0:
  367. s->invert = 1;
  368. break;
  369. case 1:
  370. s->invert = 0;
  371. break;
  372. }
  373. break;
  374. case TIFF_PAL:
  375. if(s->avctx->pix_fmt != PIX_FMT_PAL8){
  376. av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
  377. return -1;
  378. }
  379. pal = s->picture.data[1];
  380. off = (type == TIFF_SHORT) ? 2 : 1;
  381. rp = buf;
  382. gp = buf + count / 3 * off;
  383. bp = buf + count / 3 * off * 2;
  384. off = (type == TIFF_SHORT) ? 8 : 0;
  385. for(i = 0; i < count / 3; i++){
  386. j = (tget(&rp, type, s->le) >> off) << 16;
  387. j |= (tget(&gp, type, s->le) >> off) << 8;
  388. j |= tget(&bp, type, s->le) >> off;
  389. pal[i] = j;
  390. }
  391. }
  392. return 0;
  393. }
  394. static int decode_frame(AVCodecContext *avctx,
  395. void *data, int *data_size,
  396. uint8_t *buf, int buf_size)
  397. {
  398. TiffContext * const s = avctx->priv_data;
  399. AVFrame *picture = data;
  400. AVFrame * const p= (AVFrame*)&s->picture;
  401. uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  402. int id, le, off;
  403. int i, entries;
  404. //parse image header
  405. id = LE_16(buf); buf += 2;
  406. if(id == 0x4949) le = 1;
  407. else if(id == 0x4D4D) le = 0;
  408. else{
  409. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  410. return -1;
  411. }
  412. s->le = le;
  413. s->invert = 0;
  414. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  415. // that further identifies the file as a TIFF file"
  416. if(tget_short(&buf, le) != 42){
  417. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  418. return -1;
  419. }
  420. /* parse image file directory */
  421. off = tget_long(&buf, le);
  422. if(orig_buf + off + 14 >= end_buf){
  423. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  424. return -1;
  425. }
  426. buf = orig_buf + off;
  427. entries = tget_short(&buf, le);
  428. for(i = 0; i < entries; i++){
  429. if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
  430. return -1;
  431. buf += 12;
  432. }
  433. if(s->invert){
  434. uint8_t *src;
  435. int j;
  436. src = s->picture.data[0];
  437. for(j = 0; j < s->height; j++){
  438. for(i = 0; i < s->picture.linesize[0]; i++)
  439. src[i] = 255 - src[i];
  440. src += s->picture.linesize[0];
  441. }
  442. }
  443. *picture= *(AVFrame*)&s->picture;
  444. *data_size = sizeof(AVPicture);
  445. return buf_size;
  446. }
  447. static int tiff_init(AVCodecContext *avctx){
  448. TiffContext *s = avctx->priv_data;
  449. s->width = 0;
  450. s->height = 0;
  451. s->avctx = avctx;
  452. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  453. avctx->coded_frame= (AVFrame*)&s->picture;
  454. s->picture.data[0] = NULL;
  455. ff_lzw_decode_open(&s->lzw);
  456. return 0;
  457. }
  458. static int tiff_end(AVCodecContext *avctx)
  459. {
  460. TiffContext * const s = avctx->priv_data;
  461. ff_lzw_decode_close(&s->lzw);
  462. if(s->picture.data[0])
  463. avctx->release_buffer(avctx, &s->picture);
  464. return 0;
  465. }
  466. AVCodec tiff_decoder = {
  467. "tiff",
  468. CODEC_TYPE_VIDEO,
  469. CODEC_ID_TIFF,
  470. sizeof(TiffContext),
  471. tiff_init,
  472. NULL,
  473. tiff_end,
  474. decode_frame,
  475. 0,
  476. NULL
  477. };