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.

433 lines
12KB

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