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.

463 lines
15KB

  1. /*
  2. * TIFF image encoder
  3. * Copyright (c) 2007 Bartlomiej Wolowiec
  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 encoder
  23. * @file
  24. * @author Bartlomiej Wolowiec
  25. */
  26. #include "avcodec.h"
  27. #if CONFIG_ZLIB
  28. #include <zlib.h>
  29. #endif
  30. #include "bytestream.h"
  31. #include "tiff.h"
  32. #include "rle.h"
  33. #include "lzw.h"
  34. #include "put_bits.h"
  35. #define TIFF_MAX_ENTRY 32
  36. /** sizes of various TIFF field types (string size = 1)*/
  37. static const uint8_t type_sizes2[6] = {
  38. 0, 1, 1, 2, 4, 8
  39. };
  40. typedef struct TiffEncoderContext {
  41. AVCodecContext *avctx;
  42. AVFrame picture;
  43. int width; ///< picture width
  44. int height; ///< picture height
  45. unsigned int bpp; ///< bits per pixel
  46. int compr; ///< compression level
  47. int bpp_tab_size; ///< bpp_tab size
  48. int photometric_interpretation; ///< photometric interpretation
  49. int strips; ///< number of strips
  50. int rps; ///< row per strip
  51. uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header
  52. int num_entries; ///< number of entires
  53. uint8_t **buf; ///< actual position in buffer
  54. uint8_t *buf_start; ///< pointer to first byte in buffer
  55. int buf_size; ///< buffer size
  56. uint16_t subsampling[2]; ///< YUV subsampling factors
  57. struct LZWEncodeState *lzws; ///< LZW Encode state
  58. } TiffEncoderContext;
  59. /**
  60. * Check free space in buffer
  61. * @param s Tiff context
  62. * @param need Needed bytes
  63. * @return 0 - ok, 1 - no free space
  64. */
  65. inline static int check_size(TiffEncoderContext * s, uint64_t need)
  66. {
  67. if (s->buf_size < *s->buf - s->buf_start + need) {
  68. *s->buf = s->buf_start + s->buf_size + 1;
  69. av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. /**
  75. * Put n values to buffer
  76. *
  77. * @param p Pointer to pointer to output buffer
  78. * @param n Number of values
  79. * @param val Pointer to values
  80. * @param type Type of values
  81. * @param flip =0 - normal copy, >0 - flip
  82. */
  83. static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
  84. int flip)
  85. {
  86. int i;
  87. #if HAVE_BIGENDIAN
  88. flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
  89. #endif
  90. for (i = 0; i < n * type_sizes2[type]; i++)
  91. *(*p)++ = val[i ^ flip];
  92. }
  93. /**
  94. * Add entry to directory in tiff header.
  95. * @param s Tiff context
  96. * @param tag Tag that identifies the entry
  97. * @param type Entry type
  98. * @param count The number of values
  99. * @param ptr_val Pointer to values
  100. */
  101. static void add_entry(TiffEncoderContext * s,
  102. enum TiffTags tag, enum TiffTypes type, int count,
  103. const void *ptr_val)
  104. {
  105. uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
  106. assert(s->num_entries < TIFF_MAX_ENTRY);
  107. bytestream_put_le16(&entries_ptr, tag);
  108. bytestream_put_le16(&entries_ptr, type);
  109. bytestream_put_le32(&entries_ptr, count);
  110. if (type_sizes[type] * count <= 4) {
  111. tnput(&entries_ptr, count, ptr_val, type, 0);
  112. } else {
  113. bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
  114. check_size(s, count * type_sizes2[type]);
  115. tnput(s->buf, count, ptr_val, type, 0);
  116. }
  117. s->num_entries++;
  118. }
  119. static void add_entry1(TiffEncoderContext * s,
  120. enum TiffTags tag, enum TiffTypes type, int val){
  121. uint16_t w = val;
  122. uint32_t dw= val;
  123. add_entry(s, tag, type, 1, type == TIFF_SHORT ? (void *)&w : (void *)&dw);
  124. }
  125. /**
  126. * Encode one strip in tiff file
  127. *
  128. * @param s Tiff context
  129. * @param src Input buffer
  130. * @param dst Output buffer
  131. * @param n Size of input buffer
  132. * @param compr Compression method
  133. * @return Number of output bytes. If an output error is encountered, -1 returned
  134. */
  135. static int encode_strip(TiffEncoderContext * s, const int8_t * src,
  136. uint8_t * dst, int n, int compr)
  137. {
  138. switch (compr) {
  139. #if CONFIG_ZLIB
  140. case TIFF_DEFLATE:
  141. case TIFF_ADOBE_DEFLATE:
  142. {
  143. unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
  144. if (compress(dst, &zlen, src, n) != Z_OK) {
  145. av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
  146. return -1;
  147. }
  148. return zlen;
  149. }
  150. #endif
  151. case TIFF_RAW:
  152. if (check_size(s, n))
  153. return -1;
  154. memcpy(dst, src, n);
  155. return n;
  156. case TIFF_PACKBITS:
  157. return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
  158. case TIFF_LZW:
  159. return ff_lzw_encode(s->lzws, src, n);
  160. default:
  161. return -1;
  162. }
  163. }
  164. static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
  165. {
  166. AVFrame *p = &s->picture;
  167. int i, j, k;
  168. int w = (s->width - 1) / s->subsampling[0] + 1;
  169. uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
  170. uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
  171. for (i = 0; i < w; i++){
  172. for (j = 0; j < s->subsampling[1]; j++)
  173. for (k = 0; k < s->subsampling[0]; k++)
  174. *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
  175. i * s->subsampling[0] + k];
  176. *dst++ = *pu++;
  177. *dst++ = *pv++;
  178. }
  179. }
  180. static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
  181. int buf_size, void *data)
  182. {
  183. TiffEncoderContext *s = avctx->priv_data;
  184. AVFrame *pict = data;
  185. AVFrame *const p = (AVFrame *) & s->picture;
  186. int i;
  187. int n;
  188. uint8_t *ptr = buf;
  189. uint8_t *offset;
  190. uint32_t strips;
  191. uint32_t *strip_sizes = NULL;
  192. uint32_t *strip_offsets = NULL;
  193. int bytes_per_row;
  194. uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
  195. uint16_t bpp_tab[] = { 8, 8, 8, 8 };
  196. int ret = -1;
  197. int is_yuv = 0;
  198. uint8_t *yuv_line = NULL;
  199. int shift_h, shift_v;
  200. s->buf_start = buf;
  201. s->buf = &ptr;
  202. s->buf_size = buf_size;
  203. *p = *pict;
  204. p->pict_type = AV_PICTURE_TYPE_I;
  205. p->key_frame = 1;
  206. avctx->coded_frame= &s->picture;
  207. s->compr = TIFF_PACKBITS;
  208. if (avctx->compression_level == 0) {
  209. s->compr = TIFF_RAW;
  210. } else if(avctx->compression_level == 2) {
  211. s->compr = TIFF_LZW;
  212. #if CONFIG_ZLIB
  213. } else if ((avctx->compression_level >= 3)) {
  214. s->compr = TIFF_DEFLATE;
  215. #endif
  216. }
  217. s->width = avctx->width;
  218. s->height = avctx->height;
  219. s->subsampling[0] = 1;
  220. s->subsampling[1] = 1;
  221. switch (avctx->pix_fmt) {
  222. case PIX_FMT_RGB24:
  223. s->bpp = 24;
  224. s->photometric_interpretation = 2;
  225. break;
  226. case PIX_FMT_GRAY8:
  227. s->bpp = 8;
  228. s->photometric_interpretation = 1;
  229. break;
  230. case PIX_FMT_PAL8:
  231. s->bpp = 8;
  232. s->photometric_interpretation = 3;
  233. break;
  234. case PIX_FMT_MONOBLACK:
  235. case PIX_FMT_MONOWHITE:
  236. s->bpp = 1;
  237. s->photometric_interpretation = avctx->pix_fmt == PIX_FMT_MONOBLACK;
  238. bpp_tab[0] = 1;
  239. break;
  240. case PIX_FMT_YUV420P:
  241. case PIX_FMT_YUV422P:
  242. case PIX_FMT_YUV444P:
  243. case PIX_FMT_YUV410P:
  244. case PIX_FMT_YUV411P:
  245. s->photometric_interpretation = 6;
  246. avcodec_get_chroma_sub_sample(avctx->pix_fmt,
  247. &shift_h, &shift_v);
  248. s->bpp = 8 + (16 >> (shift_h + shift_v));
  249. s->subsampling[0] = 1 << shift_h;
  250. s->subsampling[1] = 1 << shift_v;
  251. s->bpp_tab_size = 3;
  252. is_yuv = 1;
  253. break;
  254. default:
  255. av_log(s->avctx, AV_LOG_ERROR,
  256. "This colors format is not supported\n");
  257. return -1;
  258. }
  259. if (!is_yuv)
  260. s->bpp_tab_size = ((s->bpp + 7) >> 3);
  261. if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE || s->compr == TIFF_LZW)
  262. //best choose for DEFLATE
  263. s->rps = s->height;
  264. else
  265. s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
  266. s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1]; // round rps up
  267. strips = (s->height - 1) / s->rps + 1;
  268. if (check_size(s, 8))
  269. goto fail;
  270. // write header
  271. bytestream_put_le16(&ptr, 0x4949);
  272. bytestream_put_le16(&ptr, 42);
  273. offset = ptr;
  274. bytestream_put_le32(&ptr, 0);
  275. strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
  276. strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
  277. bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
  278. * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
  279. if (is_yuv){
  280. yuv_line = av_malloc(bytes_per_row);
  281. if (yuv_line == NULL){
  282. av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
  283. goto fail;
  284. }
  285. }
  286. #if CONFIG_ZLIB
  287. if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
  288. uint8_t *zbuf;
  289. int zlen, zn;
  290. int j;
  291. zlen = bytes_per_row * s->rps;
  292. zbuf = av_malloc(zlen);
  293. strip_offsets[0] = ptr - buf;
  294. zn = 0;
  295. for (j = 0; j < s->rps; j++) {
  296. if (is_yuv){
  297. pack_yuv(s, yuv_line, j);
  298. memcpy(zbuf + zn, yuv_line, bytes_per_row);
  299. j += s->subsampling[1] - 1;
  300. }
  301. else
  302. memcpy(zbuf + j * bytes_per_row,
  303. p->data[0] + j * p->linesize[0], bytes_per_row);
  304. zn += bytes_per_row;
  305. }
  306. n = encode_strip(s, zbuf, ptr, zn, s->compr);
  307. av_free(zbuf);
  308. if (n<0) {
  309. av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
  310. goto fail;
  311. }
  312. ptr += n;
  313. strip_sizes[0] = ptr - buf - strip_offsets[0];
  314. } else
  315. #endif
  316. {
  317. if(s->compr == TIFF_LZW)
  318. s->lzws = av_malloc(ff_lzw_encode_state_size);
  319. for (i = 0; i < s->height; i++) {
  320. if (strip_sizes[i / s->rps] == 0) {
  321. if(s->compr == TIFF_LZW){
  322. ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start),
  323. 12, FF_LZW_TIFF, put_bits);
  324. }
  325. strip_offsets[i / s->rps] = ptr - buf;
  326. }
  327. if (is_yuv){
  328. pack_yuv(s, yuv_line, i);
  329. n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
  330. i += s->subsampling[1] - 1;
  331. }
  332. else
  333. n = encode_strip(s, p->data[0] + i * p->linesize[0],
  334. ptr, bytes_per_row, s->compr);
  335. if (n < 0) {
  336. av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
  337. goto fail;
  338. }
  339. strip_sizes[i / s->rps] += n;
  340. ptr += n;
  341. if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
  342. int ret;
  343. ret = ff_lzw_encode_flush(s->lzws, flush_put_bits);
  344. strip_sizes[(i / s->rps )] += ret ;
  345. ptr += ret;
  346. }
  347. }
  348. if(s->compr == TIFF_LZW)
  349. av_free(s->lzws);
  350. }
  351. s->num_entries = 0;
  352. add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);
  353. add_entry1(s,TIFF_WIDTH, TIFF_LONG, s->width);
  354. add_entry1(s,TIFF_HEIGHT, TIFF_LONG, s->height);
  355. if (s->bpp_tab_size)
  356. add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
  357. add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
  358. add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->photometric_interpretation);
  359. add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
  360. if (s->bpp_tab_size)
  361. add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
  362. add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
  363. add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
  364. add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
  365. add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
  366. add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
  367. if(!(avctx->flags & CODEC_FLAG_BITEXACT))
  368. add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
  369. strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
  370. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  371. uint16_t pal[256 * 3];
  372. for (i = 0; i < 256; i++) {
  373. uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
  374. pal[i] = ((rgb >> 16) & 0xff) * 257;
  375. pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
  376. pal[i + 512] = ( rgb & 0xff) * 257;
  377. }
  378. add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
  379. }
  380. if (is_yuv){
  381. /** according to CCIR Recommendation 601.1 */
  382. uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
  383. add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
  384. add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw);
  385. }
  386. bytestream_put_le32(&offset, ptr - buf); // write offset to dir
  387. if (check_size(s, 6 + s->num_entries * 12))
  388. goto fail;
  389. bytestream_put_le16(&ptr, s->num_entries); // write tag count
  390. bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
  391. bytestream_put_le32(&ptr, 0);
  392. ret = ptr - buf;
  393. fail:
  394. av_free(strip_sizes);
  395. av_free(strip_offsets);
  396. av_free(yuv_line);
  397. return ret;
  398. }
  399. AVCodec ff_tiff_encoder = {
  400. "tiff",
  401. AVMEDIA_TYPE_VIDEO,
  402. CODEC_ID_TIFF,
  403. sizeof(TiffEncoderContext),
  404. NULL,
  405. encode_frame,
  406. NULL,
  407. NULL,
  408. 0,
  409. NULL,
  410. .pix_fmts =
  411. (const enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
  412. PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
  413. PIX_FMT_YUV420P, PIX_FMT_YUV422P,
  414. PIX_FMT_YUV444P, PIX_FMT_YUV410P,
  415. PIX_FMT_YUV411P,
  416. PIX_FMT_NONE},
  417. .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
  418. };