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.

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