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.

517 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 predictor;
  41. int strips, rps, sstype;
  42. int sot;
  43. const uint8_t* stripdata;
  44. const uint8_t* stripsizes;
  45. int stripsize, stripoff;
  46. LZWState *lzw;
  47. } TiffContext;
  48. static int tget_short(const uint8_t **p, int le){
  49. int v = le ? AV_RL16(*p) : AV_RB16(*p);
  50. *p += 2;
  51. return v;
  52. }
  53. static int tget_long(const uint8_t **p, int le){
  54. int v = le ? AV_RL32(*p) : AV_RB32(*p);
  55. *p += 4;
  56. return v;
  57. }
  58. static int tget(const uint8_t **p, int type, int le){
  59. switch(type){
  60. case TIFF_BYTE : return *(*p)++;
  61. case TIFF_SHORT: return tget_short(p, le);
  62. case TIFF_LONG : return tget_long (p, le);
  63. default : return -1;
  64. }
  65. }
  66. static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
  67. int c, line, pixels, code;
  68. const uint8_t *ssrc = src;
  69. int width = s->width * s->bpp >> 3;
  70. #ifdef CONFIG_ZLIB
  71. uint8_t *zbuf; unsigned long outlen;
  72. if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
  73. outlen = width * lines;
  74. zbuf = av_malloc(outlen);
  75. if(uncompress(zbuf, &outlen, src, size) != Z_OK){
  76. av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
  77. av_free(zbuf);
  78. return -1;
  79. }
  80. src = zbuf;
  81. for(line = 0; line < lines; line++){
  82. memcpy(dst, src, width);
  83. dst += stride;
  84. src += width;
  85. }
  86. av_free(zbuf);
  87. return 0;
  88. }
  89. #endif
  90. if(s->compr == TIFF_LZW){
  91. if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
  92. av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
  93. return -1;
  94. }
  95. }
  96. for(line = 0; line < lines; line++){
  97. if(src - ssrc > size){
  98. av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
  99. return -1;
  100. }
  101. switch(s->compr){
  102. case TIFF_RAW:
  103. memcpy(dst, src, width);
  104. src += width;
  105. break;
  106. case TIFF_PACKBITS:
  107. for(pixels = 0; pixels < width;){
  108. code = (int8_t)*src++;
  109. if(code >= 0){
  110. code++;
  111. if(pixels + code > width){
  112. av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
  113. return -1;
  114. }
  115. memcpy(dst + pixels, src, code);
  116. src += code;
  117. pixels += code;
  118. }else if(code != -128){ // -127..-1
  119. code = (-code) + 1;
  120. if(pixels + code > width){
  121. av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
  122. return -1;
  123. }
  124. c = *src++;
  125. memset(dst + pixels, c, code);
  126. pixels += code;
  127. }
  128. }
  129. break;
  130. case TIFF_LZW:
  131. pixels = ff_lzw_decode(s->lzw, dst, width);
  132. if(pixels < width){
  133. av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
  134. return -1;
  135. }
  136. break;
  137. }
  138. dst += stride;
  139. }
  140. return 0;
  141. }
  142. static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
  143. {
  144. int tag, type, count, off, value = 0;
  145. int i, j;
  146. uint32_t *pal;
  147. const 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. case TIFF_STRING:
  165. if(count <= 4){
  166. buf -= 4;
  167. break;
  168. }
  169. default:
  170. value = -1;
  171. buf = start + off;
  172. }
  173. }else if(type_sizes[type] * count <= 4){
  174. buf -= 4;
  175. }else{
  176. buf = start + off;
  177. }
  178. if(buf && (buf < start || buf > end_buf)){
  179. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  180. return -1;
  181. }
  182. switch(tag){
  183. case TIFF_WIDTH:
  184. s->width = value;
  185. break;
  186. case TIFF_HEIGHT:
  187. s->height = value;
  188. break;
  189. case TIFF_BPP:
  190. if(count == 1) s->bpp = value;
  191. else{
  192. switch(type){
  193. case TIFF_BYTE:
  194. s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
  195. break;
  196. case TIFF_SHORT:
  197. case TIFF_LONG:
  198. s->bpp = 0;
  199. for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
  200. break;
  201. default:
  202. s->bpp = -1;
  203. }
  204. }
  205. switch(s->bpp){
  206. case 1:
  207. s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
  208. break;
  209. case 8:
  210. s->avctx->pix_fmt = PIX_FMT_PAL8;
  211. break;
  212. case 24:
  213. s->avctx->pix_fmt = PIX_FMT_RGB24;
  214. break;
  215. case 16:
  216. if(count == 1){
  217. s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
  218. }else{
  219. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
  220. return -1;
  221. }
  222. break;
  223. default:
  224. av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
  225. return -1;
  226. }
  227. if(s->width != s->avctx->width || s->height != s->avctx->height){
  228. if(avcodec_check_dimensions(s->avctx, s->width, s->height))
  229. return -1;
  230. avcodec_set_dimensions(s->avctx, s->width, s->height);
  231. }
  232. if(s->picture.data[0])
  233. s->avctx->release_buffer(s->avctx, &s->picture);
  234. if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
  235. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  236. return -1;
  237. }
  238. if(s->bpp == 8){
  239. /* make default grayscale pal */
  240. pal = (uint32_t *) s->picture.data[1];
  241. for(i = 0; i < 256; i++)
  242. pal[i] = i * 0x010101;
  243. }
  244. break;
  245. case TIFF_COMPR:
  246. s->compr = value;
  247. s->predictor = 0;
  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. s->sstype = type;
  312. if(s->stripsizes > end_buf){
  313. av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
  314. return -1;
  315. }
  316. break;
  317. case TIFF_PREDICTOR:
  318. s->predictor = value;
  319. break;
  320. case TIFF_INVERT:
  321. switch(value){
  322. case 0:
  323. s->invert = 1;
  324. break;
  325. case 1:
  326. s->invert = 0;
  327. break;
  328. case 2:
  329. case 3:
  330. break;
  331. default:
  332. av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
  333. return -1;
  334. }
  335. break;
  336. case TIFF_PAL:
  337. if(s->avctx->pix_fmt != PIX_FMT_PAL8){
  338. av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
  339. return -1;
  340. }
  341. pal = (uint32_t *) s->picture.data[1];
  342. off = type_sizes[type];
  343. rp = buf;
  344. gp = buf + count / 3 * off;
  345. bp = buf + count / 3 * off * 2;
  346. off = (type_sizes[type] - 1) << 3;
  347. for(i = 0; i < count / 3; i++){
  348. j = (tget(&rp, type, s->le) >> off) << 16;
  349. j |= (tget(&gp, type, s->le) >> off) << 8;
  350. j |= tget(&bp, type, s->le) >> off;
  351. pal[i] = j;
  352. }
  353. break;
  354. case TIFF_PLANAR:
  355. if(value == 2){
  356. av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
  357. return -1;
  358. }
  359. break;
  360. }
  361. return 0;
  362. }
  363. static int decode_frame(AVCodecContext *avctx,
  364. void *data, int *data_size,
  365. const uint8_t *buf, int buf_size)
  366. {
  367. TiffContext * const s = avctx->priv_data;
  368. AVFrame *picture = data;
  369. AVFrame * const p= (AVFrame*)&s->picture;
  370. const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  371. int id, le, off;
  372. int i, j, entries;
  373. int stride, soff, ssize;
  374. uint8_t *dst;
  375. //parse image header
  376. id = AV_RL16(buf); buf += 2;
  377. if(id == 0x4949) le = 1;
  378. else if(id == 0x4D4D) le = 0;
  379. else{
  380. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  381. return -1;
  382. }
  383. s->le = le;
  384. s->invert = 0;
  385. s->compr = TIFF_RAW;
  386. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  387. // that further identifies the file as a TIFF file"
  388. if(tget_short(&buf, le) != 42){
  389. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  390. return -1;
  391. }
  392. /* parse image file directory */
  393. off = tget_long(&buf, le);
  394. if(orig_buf + off + 14 >= end_buf){
  395. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  396. return -1;
  397. }
  398. buf = orig_buf + off;
  399. entries = tget_short(&buf, le);
  400. for(i = 0; i < entries; i++){
  401. if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
  402. return -1;
  403. buf += 12;
  404. }
  405. if(!s->stripdata && !s->stripoff){
  406. av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
  407. return -1;
  408. }
  409. /* now we have the data and may start decoding */
  410. if(!p->data[0]){
  411. av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
  412. return -1;
  413. }
  414. if(s->strips == 1 && !s->stripsize){
  415. av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
  416. s->stripsize = buf_size - s->stripoff;
  417. }
  418. stride = p->linesize[0];
  419. dst = p->data[0];
  420. for(i = 0; i < s->height; i += s->rps){
  421. if(s->stripsizes)
  422. ssize = tget(&s->stripsizes, s->sstype, s->le);
  423. else
  424. ssize = s->stripsize;
  425. if(s->stripdata){
  426. soff = tget(&s->stripdata, s->sot, s->le);
  427. }else
  428. soff = s->stripoff;
  429. if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
  430. break;
  431. dst += s->rps * stride;
  432. }
  433. if(s->predictor == 2){
  434. dst = p->data[0];
  435. soff = s->bpp >> 3;
  436. ssize = s->width * soff;
  437. for(i = 0; i < s->height; i++) {
  438. for(j = soff; j < ssize; j++)
  439. dst[j] += dst[j - soff];
  440. dst += stride;
  441. }
  442. }
  443. if(s->invert){
  444. uint8_t *src;
  445. int j;
  446. src = s->picture.data[0];
  447. for(j = 0; j < s->height; j++){
  448. for(i = 0; i < s->picture.linesize[0]; i++)
  449. src[i] = 255 - src[i];
  450. src += s->picture.linesize[0];
  451. }
  452. }
  453. *picture= *(AVFrame*)&s->picture;
  454. *data_size = sizeof(AVPicture);
  455. return buf_size;
  456. }
  457. static av_cold int tiff_init(AVCodecContext *avctx){
  458. TiffContext *s = avctx->priv_data;
  459. s->width = 0;
  460. s->height = 0;
  461. s->avctx = avctx;
  462. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  463. avctx->coded_frame= (AVFrame*)&s->picture;
  464. s->picture.data[0] = NULL;
  465. ff_lzw_decode_open(&s->lzw);
  466. return 0;
  467. }
  468. static av_cold int tiff_end(AVCodecContext *avctx)
  469. {
  470. TiffContext * const s = avctx->priv_data;
  471. ff_lzw_decode_close(&s->lzw);
  472. if(s->picture.data[0])
  473. avctx->release_buffer(avctx, &s->picture);
  474. return 0;
  475. }
  476. AVCodec tiff_decoder = {
  477. "tiff",
  478. CODEC_TYPE_VIDEO,
  479. CODEC_ID_TIFF,
  480. sizeof(TiffContext),
  481. tiff_init,
  482. NULL,
  483. tiff_end,
  484. decode_frame,
  485. 0,
  486. NULL,
  487. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  488. };