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.

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