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.

511 lines
15KB

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