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);
130 static void add_entry1(TiffEncoderContext * s,
131 enum TiffTags tag, enum TiffTypes type, int val){
134 add_entry(s, tag, type, 1, type == TIFF_SHORT ? &w : &dw);
138 * Encode one strip in tiff file
140 * @param s Tiff context
141 * @param src Input buffer
142 * @param dst Output buffer
143 * @param n Size of input buffer
144 * @param compr Compression method
145 * @return Number of output bytes. If an output error is encountered, -1 returned
147 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
148 uint8_t * dst, int n, int compr)
154 case TIFF_ADOBE_DEFLATE:
156 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
157 if (compress(dst, &zlen, src, n) != Z_OK) {
158 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
165 if (check_size(s, n))
170 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
176 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
177 int buf_size, void *data)
179 TiffEncoderContext *s = avctx->priv_data;
180 AVFrame *pict = data;
181 AVFrame *const p = (AVFrame *) & s->picture;
187 uint32_t *strip_sizes = NULL;
188 uint32_t *strip_offsets = NULL;
190 uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
191 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
196 s->buf_size = buf_size;
199 p->pict_type = FF_I_TYPE;
202 s->compr = TIFF_PACKBITS;
203 if (avctx->compression_level == 0) {
206 } else if ((avctx->compression_level > 2)) {
207 s->compr = TIFF_DEFLATE;
211 s->width = avctx->width;
212 s->height = avctx->height;
214 switch (avctx->pix_fmt) {
227 case PIX_FMT_MONOBLACK:
231 case PIX_FMT_MONOWHITE:
236 av_log(s->avctx, AV_LOG_ERROR,
237 "This colors format is not supported\n");
240 s->bpp_tab_size = (s->bpp >> 3);
242 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
243 //best choose for DEFLATE
246 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
248 strips = (s->height - 1) / s->rps + 1;
250 if (check_size(s, 8))
254 bytestream_put_le16(&ptr, 0x4949);
255 bytestream_put_le16(&ptr, 42);
258 bytestream_put_le32(&ptr, 0);
260 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
261 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
263 bytes_per_row = (s->width * s->bpp + 7) >> 3;
266 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
271 zlen = bytes_per_row * s->rps;
272 zbuf = av_malloc(zlen);
273 strip_offsets[0] = ptr - buf;
275 for (j = 0; j < s->rps; j++) {
276 memcpy(zbuf + j * bytes_per_row,
277 p->data[0] + j * p->linesize[0], bytes_per_row);
280 n = encode_strip(s, zbuf, ptr, zn, s->compr);
283 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
287 strip_sizes[0] = ptr - buf - strip_offsets[0];
291 for (i = 0; i < s->height; i++) {
292 if (strip_sizes[i / s->rps] == 0) {
293 strip_offsets[i / s->rps] = ptr - buf;
295 if ((n = encode_strip(s, p->data[0] + i * p->linesize[0]
296 , ptr, bytes_per_row, s->compr)) < 0) {
297 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
300 strip_sizes[i / s->rps] += n;
307 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);
308 add_entry1(s,TIFF_WIDTH, TIFF_LONG, s->width);
309 add_entry1(s,TIFF_HEIGHT, TIFF_LONG, s->height);
312 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
314 add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
315 add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->invert);
316 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
319 add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
321 add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
322 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
323 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
324 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
325 add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
326 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
327 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
329 if (avctx->pix_fmt == PIX_FMT_PAL8) {
330 uint16_t pal[256 * 3];
331 for (i = 0; i < 256; i++) {
332 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
333 pal[i] = ((rgb >> 16) & 0xff) * 257;
334 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
335 pal[i + 512] = ( rgb & 0xff) * 257;
337 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
339 bytestream_put_le32(&offset, ptr - buf); // write offset to dir
341 if (check_size(s, 6 + s->num_entries * 12))
343 bytestream_put_le16(&ptr, s->num_entries); // write tag count
344 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
345 bytestream_put_le32(&ptr, 0);
350 av_free(strip_sizes);
351 av_free(strip_offsets);
355 AVCodec tiff_encoder = {
359 sizeof(TiffEncoderContext),
367 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
368 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,