tiffenc.c 15 KB

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