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.

448 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);
  229. break;
  230. case TIFF_SHORT:
  231. case TIFF_LONG:
  232. s->bpp = tget(&buf, type, s->le) + tget(&buf, type, s->le) + tget(&buf, type, s->le);
  233. break;
  234. default:
  235. s->bpp = -1;
  236. }
  237. }
  238. if(s->bpp != 24){
  239. av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported\n");
  240. return -1;
  241. }
  242. break;
  243. case TIFF_COMPR:
  244. s->compr = value;
  245. switch(s->compr){
  246. case TIFF_RAW:
  247. case TIFF_PACKBITS:
  248. case TIFF_LZW:
  249. break;
  250. case TIFF_DEFLATE:
  251. case TIFF_ADOBE_DEFLATE:
  252. #ifdef CONFIG_ZLIB
  253. break;
  254. #else
  255. av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
  256. return -1;
  257. #endif
  258. case TIFF_G3:
  259. av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
  260. return -1;
  261. case TIFF_G4:
  262. av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
  263. return -1;
  264. case TIFF_CCITT_RLE:
  265. av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
  266. return -1;
  267. default:
  268. av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
  269. return -1;
  270. }
  271. break;
  272. case TIFF_ROWSPERSTRIP:
  273. if(value < 1 || value > s->height){
  274. av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
  275. return -1;
  276. }
  277. s->rps = value;
  278. break;
  279. case TIFF_STRIP_OFFS:
  280. if(count == 1){
  281. s->stripdata = NULL;
  282. s->stripoff = value;
  283. }else
  284. s->stripdata = start + off;
  285. s->strips = count;
  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. src = 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. src[j] += src[j - soff];
  340. src += stride;
  341. }
  342. }
  343. break;
  344. }
  345. return 0;
  346. }
  347. static int decode_frame(AVCodecContext *avctx,
  348. void *data, int *data_size,
  349. uint8_t *buf, int buf_size)
  350. {
  351. TiffContext * const s = avctx->priv_data;
  352. AVFrame *picture = data;
  353. AVFrame * const p= (AVFrame*)&s->picture;
  354. uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
  355. int id, le, off;
  356. int i, entries;
  357. //parse image header
  358. id = LE_16(buf); buf += 2;
  359. if(id == 0x4949) le = 1;
  360. else if(id == 0x4D4D) le = 0;
  361. else{
  362. av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
  363. return -1;
  364. }
  365. s->le = le;
  366. // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
  367. // that further identifies the file as a TIFF file"
  368. if(tget_short(&buf, le) != 42){
  369. av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
  370. return -1;
  371. }
  372. /* parse image file directory */
  373. off = tget_long(&buf, le);
  374. if(orig_buf + off + 14 >= end_buf){
  375. av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
  376. return -1;
  377. }
  378. buf = orig_buf + off;
  379. entries = tget_short(&buf, le);
  380. for(i = 0; i < entries; i++){
  381. if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
  382. return -1;
  383. buf += 12;
  384. }
  385. *picture= *(AVFrame*)&s->picture;
  386. *data_size = sizeof(AVPicture);
  387. return buf_size;
  388. }
  389. static int tiff_init(AVCodecContext *avctx){
  390. TiffContext *s = avctx->priv_data;
  391. s->width = 0;
  392. s->height = 0;
  393. s->avctx = avctx;
  394. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  395. avctx->coded_frame= (AVFrame*)&s->picture;
  396. s->picture.data[0] = NULL;
  397. ff_lzw_decode_open(&s->lzw);
  398. return 0;
  399. }
  400. static int tiff_end(AVCodecContext *avctx)
  401. {
  402. TiffContext * const s = avctx->priv_data;
  403. ff_lzw_decode_close(&s->lzw);
  404. if(s->picture.data[0])
  405. avctx->release_buffer(avctx, &s->picture);
  406. return 0;
  407. }
  408. AVCodec tiff_decoder = {
  409. "tiff",
  410. CODEC_TYPE_VIDEO,
  411. CODEC_ID_TIFF,
  412. sizeof(TiffContext),
  413. tiff_init,
  414. NULL,
  415. tiff_end,
  416. decode_frame,
  417. 0,
  418. NULL
  419. };