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.

453 lines
13KB

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