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.

360 lines
11KB

  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. #include "avcodec.h"
  23. #ifdef CONFIG_ZLIB
  24. #include <zlib.h>
  25. #endif
  26. #include "bytestream.h"
  27. #include "tiff.h"
  28. #include "rle.h"
  29. #define TIFF_MAX_ENTRY 32
  30. /** sizes of various TIFF field types (string size = 1)*/
  31. static const uint8_t type_sizes2[6] = {
  32. 0, 1, 1, 2, 4, 8
  33. };
  34. typedef struct TiffEncoderContext {
  35. AVCodecContext *avctx;
  36. AVFrame picture;
  37. int width; ///< picture width
  38. int height; ///< picture height
  39. unsigned int bpp; ///< bits per pixel
  40. int compr; ///< compression level
  41. int bpp_tab_size; ///< bpp_tab size
  42. int invert; ///< photometric interpretation
  43. int strips; ///< number of strips
  44. int rps; ///< row per strip
  45. uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header
  46. int num_entries; ///< number of entires
  47. uint8_t **buf; ///< actual position in buffer
  48. uint8_t *buf_start; ///< pointer to first byte in buffer
  49. int buf_size; ///< buffer size
  50. } TiffEncoderContext;
  51. /**
  52. * Check free space in buffer
  53. * @param s Tiff context
  54. * @param need Needed bytes
  55. * @return 0 - ok, 1 - no free space
  56. */
  57. inline static int check_size(TiffEncoderContext * s, uint64_t need)
  58. {
  59. if (s->buf_size < *s->buf - s->buf_start + need) {
  60. *s->buf = s->buf_start + s->buf_size + 1;
  61. av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * Put n values to buffer
  68. *
  69. * @param p Pointer to pointer to output buffer
  70. * @param n Number of values
  71. * @param val Pointer to values
  72. * @param type Type of values
  73. * @param flip =0 - normal copy, >0 - flip
  74. */
  75. static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
  76. int flip)
  77. {
  78. int i;
  79. #ifdef WORDS_BIGENDIAN
  80. flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
  81. #endif
  82. for (i = 0; i < n * type_sizes2[type]; i++)
  83. *(*p)++ = val[i ^ flip];
  84. }
  85. /**
  86. * Add entry to directory in tiff header.
  87. * @param s Tiff context
  88. * @param tag Tag that identifies the entry
  89. * @param type Entry type
  90. * @param count The number of values
  91. * @param ptr_val Pointer to values
  92. */
  93. static void add_entry(TiffEncoderContext * s,
  94. enum TiffTags tag, enum TiffTypes type, int count,
  95. const void *ptr_val)
  96. {
  97. uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
  98. assert(s->num_entries < TIFF_MAX_ENTRY);
  99. bytestream_put_le16(&entries_ptr, tag);
  100. bytestream_put_le16(&entries_ptr, type);
  101. bytestream_put_le32(&entries_ptr, count);
  102. if (type_sizes[type] * count <= 4) {
  103. tnput(&entries_ptr, count, ptr_val, type, 0);
  104. } else {
  105. bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
  106. check_size(s, count * type_sizes2[type]);
  107. tnput(s->buf, count, ptr_val, type, 0);
  108. }
  109. s->num_entries++;
  110. }
  111. /**
  112. * Encode one strip in tiff file
  113. *
  114. * @param s Tiff context
  115. * @param src Input buffer
  116. * @param dst Output buffer
  117. * @param n Size of input buffer
  118. * @param compr Compression method
  119. * @return Number of output bytes. If an output error is encountered, -1 returned
  120. */
  121. static int encode_strip(TiffEncoderContext * s, const int8_t * src,
  122. uint8_t * dst, int n, int compr)
  123. {
  124. switch (compr) {
  125. #ifdef CONFIG_ZLIB
  126. case TIFF_DEFLATE:
  127. case TIFF_ADOBE_DEFLATE:
  128. {
  129. unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
  130. if (compress(dst, &zlen, src, n) != Z_OK) {
  131. av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
  132. return -1;
  133. }
  134. return zlen;
  135. }
  136. #endif
  137. case TIFF_RAW:
  138. if (check_size(s, n))
  139. return -1;
  140. memcpy(dst, src, n);
  141. return n;
  142. case TIFF_PACKBITS:
  143. return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
  144. default:
  145. return -1;
  146. }
  147. }
  148. static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
  149. int buf_size, void *data)
  150. {
  151. TiffEncoderContext *s = avctx->priv_data;
  152. AVFrame *pict = data;
  153. AVFrame *const p = (AVFrame *) & s->picture;
  154. int i;
  155. int n;
  156. uint8_t *ptr = buf;
  157. uint8_t *offset;
  158. uint32_t strips;
  159. uint32_t *strip_sizes = NULL;
  160. uint32_t *strip_offsets = NULL;
  161. int bytes_per_row;
  162. uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
  163. static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
  164. int ret = -1;
  165. s->buf_start = buf;
  166. s->buf = &ptr;
  167. s->buf_size = buf_size;
  168. *p = *pict;
  169. p->pict_type = FF_I_TYPE;
  170. p->key_frame = 1;
  171. s->compr = TIFF_PACKBITS;
  172. if (avctx->compression_level == 0) {
  173. s->compr = TIFF_RAW;
  174. #ifdef CONFIG_ZLIB
  175. } else if ((avctx->compression_level > 2)) {
  176. s->compr = TIFF_DEFLATE;
  177. #endif
  178. }
  179. s->width = avctx->width;
  180. s->height = avctx->height;
  181. switch (avctx->pix_fmt) {
  182. case PIX_FMT_RGB24:
  183. s->bpp = 24;
  184. s->invert = 2;
  185. break;
  186. case PIX_FMT_GRAY8:
  187. s->bpp = 8;
  188. s->invert = 1;
  189. break;
  190. case PIX_FMT_PAL8:
  191. s->bpp = 8;
  192. s->invert = 3;
  193. break;
  194. case PIX_FMT_MONOBLACK:
  195. s->bpp = 1;
  196. s->invert = 1;
  197. break;
  198. case PIX_FMT_MONOWHITE:
  199. s->bpp = 1;
  200. s->invert = 0;
  201. break;
  202. default:
  203. av_log(s->avctx, AV_LOG_ERROR,
  204. "This colors format is not supported\n");
  205. return -1;
  206. }
  207. s->bpp_tab_size = (s->bpp >> 3);
  208. if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
  209. //best choose for DEFLATE
  210. s->rps = s->height;
  211. else
  212. s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
  213. strips = (s->height - 1) / s->rps + 1;
  214. if (check_size(s, 8))
  215. goto fail;
  216. // write header
  217. bytestream_put_le16(&ptr, 0x4949);
  218. bytestream_put_le16(&ptr, 42);
  219. offset = ptr;
  220. bytestream_put_le32(&ptr, 0);
  221. strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
  222. strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
  223. bytes_per_row = (s->width * s->bpp + 7) >> 3;
  224. #ifdef CONFIG_ZLIB
  225. if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
  226. uint8_t *zbuf;
  227. int zlen, zn;
  228. int j;
  229. zlen = bytes_per_row * s->rps;
  230. zbuf = av_malloc(zlen);
  231. strip_offsets[0] = ptr - buf;
  232. zn = 0;
  233. for (j = 0; j < s->rps; j++) {
  234. memcpy(zbuf + j * bytes_per_row,
  235. p->data[0] + j * p->linesize[0], bytes_per_row);
  236. zn += bytes_per_row;
  237. }
  238. n = encode_strip(s, zbuf, ptr, zn, s->compr);
  239. av_free(zbuf);
  240. if (n<0) {
  241. av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
  242. goto fail;
  243. }
  244. ptr += n;
  245. strip_sizes[0] = ptr - buf - strip_offsets[0];
  246. } else
  247. #endif
  248. {
  249. for (i = 0; i < s->height; i++) {
  250. if (strip_sizes[i / s->rps] == 0) {
  251. strip_offsets[i / s->rps] = ptr - buf;
  252. }
  253. if ((n = encode_strip(s, p->data[0] + i * p->linesize[0]
  254. , ptr, bytes_per_row, s->compr)) < 0) {
  255. av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
  256. goto fail;
  257. }
  258. strip_sizes[i / s->rps] += n;
  259. ptr += n;
  260. }
  261. }
  262. s->num_entries = 0;
  263. add_entry(s, TIFF_SUBFILE, TIFF_LONG, 1, (uint32_t[]) {0});
  264. add_entry(s, TIFF_WIDTH, TIFF_LONG, 1, (uint32_t[]) {s->width});
  265. add_entry(s, TIFF_HEIGHT, TIFF_LONG, 1, (uint32_t[]) {s->height});
  266. if (s->bpp_tab_size)
  267. add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
  268. add_entry(s, TIFF_COMPR, TIFF_SHORT, 1, (uint16_t[]) {s->compr});
  269. add_entry(s, TIFF_INVERT, TIFF_SHORT, 1, (uint16_t[]) {s->invert});
  270. add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
  271. if (s->bpp_tab_size)
  272. add_entry(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, 1, (uint16_t[]) {s->bpp_tab_size});
  273. add_entry(s, TIFF_ROWSPERSTRIP, TIFF_LONG, 1, (uint32_t[]) {s->rps});
  274. add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
  275. add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
  276. add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
  277. add_entry(s, TIFF_RES_UNIT, TIFF_SHORT, 1, (uint16_t[]) {2});
  278. add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
  279. strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
  280. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  281. uint16_t pal[256 * 3];
  282. for (i = 0; i < 256; i++) {
  283. uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
  284. pal[i] = ((rgb >> 16) & 0xff) * 257;
  285. pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
  286. pal[i + 512] = ( rgb & 0xff) * 257;
  287. }
  288. add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
  289. }
  290. bytestream_put_le32(&offset, ptr - buf); // write offset to dir
  291. if (check_size(s, 6 + s->num_entries * 12))
  292. goto fail;
  293. bytestream_put_le16(&ptr, s->num_entries); // write tag count
  294. bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
  295. bytestream_put_le32(&ptr, 0);
  296. ret = ptr - buf;
  297. fail:
  298. av_free(strip_sizes);
  299. av_free(strip_offsets);
  300. return ret;
  301. }
  302. AVCodec tiff_encoder = {
  303. "tiff",
  304. CODEC_TYPE_VIDEO,
  305. CODEC_ID_TIFF,
  306. sizeof(TiffEncoderContext),
  307. NULL,
  308. encode_frame,
  309. NULL,
  310. NULL,
  311. 0,
  312. NULL,
  313. .pix_fmts =
  314. (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
  315. PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
  316. -1}
  317. };