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.

442 lines
14KB

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