3 * Copyright (c) 2007 Bartlomiej Wolowiec
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "bytestream.h"
31 #define TIFF_MAX_ENTRY 32
33 /** sizes of various TIFF field types (string size = 1)*/
34 static const uint8_t type_sizes2[6] = {
38 typedef struct TiffEncoderContext {
39 AVCodecContext *avctx;
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 invert; ///< 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
59 * Check free space in buffer
60 * @param s Tiff context
61 * @param need Needed bytes
62 * @return 0 - ok, 1 - no free space
64 inline static int check_size(TiffEncoderContext * s, uint64_t need)
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");
75 * Put n values to buffer
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
83 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
87 #ifdef WORDS_BIGENDIAN
88 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
90 for (i = 0; i < n * type_sizes2[type]; i++)
91 *(*p)++ = val[i ^ flip];
95 * Add entry to directory in tiff header.
96 * @param s Tiff context
97 * @param tag Tag that identifies the entry
98 * @param type Entry type
99 * @param count The number of values
100 * @param ptr_val Pointer to values
102 static void add_entry(TiffEncoderContext * s,
103 enum TiffTags tag, enum TiffTypes type, int count,
106 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
108 assert(s->num_entries < TIFF_MAX_ENTRY);
110 bytestream_put_le16(&entries_ptr, tag);
111 bytestream_put_le16(&entries_ptr, type);
112 bytestream_put_le32(&entries_ptr, count);
114 if (type_sizes[type] * count <= 4) {
115 tnput(&entries_ptr, count, ptr_val, type, 0);
117 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
118 check_size(s, count * type_sizes2[type]);
119 tnput(s->buf, count, ptr_val, type, 0);
126 * Encode one strip in tiff file
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
135 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
136 uint8_t * dst, int n, int compr)
142 case TIFF_ADOBE_DEFLATE:
144 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
145 if (compress(dst, &zlen, src, n) != Z_OK) {
146 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
153 if (check_size(s, n))
158 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
164 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
165 int buf_size, void *data)
167 TiffEncoderContext *s = avctx->priv_data;
168 AVFrame *pict = data;
169 AVFrame *const p = (AVFrame *) & s->picture;
175 uint32_t *strip_sizes = NULL;
176 uint32_t *strip_offsets = NULL;
178 uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
179 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
184 s->buf_size = buf_size;
187 p->pict_type = FF_I_TYPE;
190 s->compr = TIFF_PACKBITS;
191 if (avctx->compression_level == 0) {
194 } else if ((avctx->compression_level > 2)) {
195 s->compr = TIFF_DEFLATE;
199 s->width = avctx->width;
200 s->height = avctx->height;
202 switch (avctx->pix_fmt) {
215 case PIX_FMT_MONOBLACK:
219 case PIX_FMT_MONOWHITE:
224 av_log(s->avctx, AV_LOG_ERROR,
225 "This colors format is not supported\n");
228 s->bpp_tab_size = (s->bpp >> 3);
230 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
231 //best choose for DEFLATE
234 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
236 strips = (s->height - 1) / s->rps + 1;
238 if (check_size(s, 8))
242 bytestream_put_le16(&ptr, 0x4949);
243 bytestream_put_le16(&ptr, 42);
246 bytestream_put_le32(&ptr, 0);
248 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
249 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
251 bytes_per_row = (s->width * s->bpp + 7) >> 3;
254 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
259 zlen = bytes_per_row * s->rps;
260 zbuf = av_malloc(zlen);
261 strip_offsets[0] = ptr - buf;
263 for (j = 0; j < s->rps; j++) {
264 memcpy(zbuf + j * bytes_per_row,
265 p->data[0] + j * p->linesize[0], bytes_per_row);
268 n = encode_strip(s, zbuf, ptr, zn, s->compr);
271 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
275 strip_sizes[0] = ptr - buf - strip_offsets[0];
279 for (i = 0; i < s->height; i++) {
280 if (strip_sizes[i / s->rps] == 0) {
281 strip_offsets[i / s->rps] = ptr - buf;
283 if ((n = encode_strip(s, p->data[0] + i * p->linesize[0]
284 , ptr, bytes_per_row, s->compr)) < 0) {
285 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
288 strip_sizes[i / s->rps] += n;
295 add_entry(s, TIFF_SUBFILE, TIFF_LONG, 1, (uint32_t[]) {0});
296 add_entry(s, TIFF_WIDTH, TIFF_LONG, 1, (uint32_t[]) {s->width});
297 add_entry(s, TIFF_HEIGHT, TIFF_LONG, 1, (uint32_t[]) {s->height});
300 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
302 add_entry(s, TIFF_COMPR, TIFF_SHORT, 1, (uint16_t[]) {s->compr});
303 add_entry(s, TIFF_INVERT, TIFF_SHORT, 1, (uint16_t[]) {s->invert});
304 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
307 add_entry(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, 1, (uint16_t[]) {s->bpp_tab_size});
309 add_entry(s, TIFF_ROWSPERSTRIP, TIFF_LONG, 1, (uint32_t[]) {s->rps});
310 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
311 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
312 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
313 add_entry(s, TIFF_RES_UNIT, TIFF_SHORT, 1, (uint16_t[]) {2});
314 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
315 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
317 if (avctx->pix_fmt == PIX_FMT_PAL8) {
318 uint16_t pal[256 * 3];
319 for (i = 0; i < 256; i++) {
320 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
321 pal[i] = ((rgb >> 16) & 0xff) * 257;
322 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
323 pal[i + 512] = ( rgb & 0xff) * 257;
325 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
327 bytestream_put_le32(&offset, ptr - buf); // write offset to dir
329 if (check_size(s, 6 + s->num_entries * 12))
331 bytestream_put_le16(&ptr, s->num_entries); // write tag count
332 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
333 bytestream_put_le32(&ptr, 0);
338 av_free(strip_sizes);
339 av_free(strip_offsets);
343 AVCodec tiff_encoder = {
347 sizeof(TiffEncoderContext),
355 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
356 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,