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
26 * @author Bartlomiej Wolowiec
32 #include "bytestream.h"
36 #define TIFF_MAX_ENTRY 32
38 /** sizes of various TIFF field types (string size = 1)*/
39 static const uint8_t type_sizes2[6] = {
43 typedef struct TiffEncoderContext {
44 AVCodecContext *avctx;
47 int width; ///< picture width
48 int height; ///< picture height
49 unsigned int bpp; ///< bits per pixel
50 int compr; ///< compression level
51 int bpp_tab_size; ///< bpp_tab size
52 int invert; ///< photometric interpretation
53 int strips; ///< number of strips
54 int rps; ///< row per strip
55 uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header
56 int num_entries; ///< number of entires
57 uint8_t **buf; ///< actual position in buffer
58 uint8_t *buf_start; ///< pointer to first byte in buffer
59 int buf_size; ///< buffer size
64 * Check free space in buffer
65 * @param s Tiff context
66 * @param need Needed bytes
67 * @return 0 - ok, 1 - no free space
69 inline static int check_size(TiffEncoderContext * s, uint64_t need)
71 if (s->buf_size < *s->buf - s->buf_start + need) {
72 *s->buf = s->buf_start + s->buf_size + 1;
73 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
80 * Put n values to buffer
82 * @param p Pointer to pointer to output buffer
83 * @param n Number of values
84 * @param val Pointer to values
85 * @param type Type of values
86 * @param flip =0 - normal copy, >0 - flip
88 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
92 #ifdef WORDS_BIGENDIAN
93 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
95 for (i = 0; i < n * type_sizes2[type]; i++)
96 *(*p)++ = val[i ^ flip];
100 * Add entry to directory in tiff header.
101 * @param s Tiff context
102 * @param tag Tag that identifies the entry
103 * @param type Entry type
104 * @param count The number of values
105 * @param ptr_val Pointer to values
107 static void add_entry(TiffEncoderContext * s,
108 enum TiffTags tag, enum TiffTypes type, int count,
111 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
113 assert(s->num_entries < TIFF_MAX_ENTRY);
115 bytestream_put_le16(&entries_ptr, tag);
116 bytestream_put_le16(&entries_ptr, type);
117 bytestream_put_le32(&entries_ptr, count);
119 if (type_sizes[type] * count <= 4) {
120 tnput(&entries_ptr, count, ptr_val, type, 0);
122 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
123 check_size(s, count * type_sizes2[type]);
124 tnput(s->buf, count, ptr_val, type, 0);
131 * Encode one strip in tiff file
133 * @param s Tiff context
134 * @param src Input buffer
135 * @param dst Output buffer
136 * @param n Size of input buffer
137 * @param compr Compression method
138 * @return Number of output bytes. If an output error is encountered, -1 returned
140 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
141 uint8_t * dst, int n, int compr)
147 case TIFF_ADOBE_DEFLATE:
149 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
150 if (compress(dst, &zlen, src, n) != Z_OK) {
151 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
158 if (check_size(s, n))
163 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
169 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
170 int buf_size, void *data)
172 TiffEncoderContext *s = avctx->priv_data;
173 AVFrame *pict = data;
174 AVFrame *const p = (AVFrame *) & s->picture;
180 uint32_t *strip_sizes = NULL;
181 uint32_t *strip_offsets = NULL;
183 uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
184 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
189 s->buf_size = buf_size;
192 p->pict_type = FF_I_TYPE;
195 s->compr = TIFF_PACKBITS;
196 if (avctx->compression_level == 0) {
199 } else if ((avctx->compression_level > 2)) {
200 s->compr = TIFF_DEFLATE;
204 s->width = avctx->width;
205 s->height = avctx->height;
207 switch (avctx->pix_fmt) {
220 case PIX_FMT_MONOBLACK:
224 case PIX_FMT_MONOWHITE:
229 av_log(s->avctx, AV_LOG_ERROR,
230 "This colors format is not supported\n");
233 s->bpp_tab_size = (s->bpp >> 3);
235 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
236 //best choose for DEFLATE
239 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
241 strips = (s->height - 1) / s->rps + 1;
243 if (check_size(s, 8))
247 bytestream_put_le16(&ptr, 0x4949);
248 bytestream_put_le16(&ptr, 42);
251 bytestream_put_le32(&ptr, 0);
253 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
254 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
256 bytes_per_row = (s->width * s->bpp + 7) >> 3;
259 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
264 zlen = bytes_per_row * s->rps;
265 zbuf = av_malloc(zlen);
266 strip_offsets[0] = ptr - buf;
268 for (j = 0; j < s->rps; j++) {
269 memcpy(zbuf + j * bytes_per_row,
270 p->data[0] + j * p->linesize[0], bytes_per_row);
273 n = encode_strip(s, zbuf, ptr, zn, s->compr);
276 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
280 strip_sizes[0] = ptr - buf - strip_offsets[0];
284 for (i = 0; i < s->height; i++) {
285 if (strip_sizes[i / s->rps] == 0) {
286 strip_offsets[i / s->rps] = ptr - buf;
288 if ((n = encode_strip(s, p->data[0] + i * p->linesize[0]
289 , ptr, bytes_per_row, s->compr)) < 0) {
290 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
293 strip_sizes[i / s->rps] += n;
300 add_entry(s, TIFF_SUBFILE, TIFF_LONG, 1, (uint32_t[]) {0});
301 add_entry(s, TIFF_WIDTH, TIFF_LONG, 1, (uint32_t[]) {s->width});
302 add_entry(s, TIFF_HEIGHT, TIFF_LONG, 1, (uint32_t[]) {s->height});
305 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
307 add_entry(s, TIFF_COMPR, TIFF_SHORT, 1, (uint16_t[]) {s->compr});
308 add_entry(s, TIFF_INVERT, TIFF_SHORT, 1, (uint16_t[]) {s->invert});
309 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
312 add_entry(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, 1, (uint16_t[]) {s->bpp_tab_size});
314 add_entry(s, TIFF_ROWSPERSTRIP, TIFF_LONG, 1, (uint32_t[]) {s->rps});
315 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
316 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
317 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
318 add_entry(s, TIFF_RES_UNIT, TIFF_SHORT, 1, (uint16_t[]) {2});
319 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
320 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
322 if (avctx->pix_fmt == PIX_FMT_PAL8) {
323 uint16_t pal[256 * 3];
324 for (i = 0; i < 256; i++) {
325 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
326 pal[i] = ((rgb >> 16) & 0xff) * 257;
327 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
328 pal[i + 512] = ( rgb & 0xff) * 257;
330 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
332 bytestream_put_le32(&offset, ptr - buf); // write offset to dir
334 if (check_size(s, 6 + s->num_entries * 12))
336 bytestream_put_le16(&ptr, s->num_entries); // write tag count
337 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
338 bytestream_put_le32(&ptr, 0);
343 av_free(strip_sizes);
344 av_free(strip_offsets);
348 AVCodec tiff_encoder = {
352 sizeof(TiffEncoderContext),
360 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
361 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,