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.

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