tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
[libav.git] / libavcodec / tiffenc.c
CommitLineData
716222db
MN
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#include "avcodec.h"
24#ifdef CONFIG_ZLIB
25#include <zlib.h>
26#endif
27#include "bytestream.h"
28#include "tiff.h"
29#include "rle.h"
30
31#define TIFF_MAX_ENTRY 32
32
33/** sizes of various TIFF field types (string size = 1)*/
34static const uint8_t type_sizes2[6] = {
35 0, 1, 1, 2, 4, 8
36};
37
38typedef struct TiffEncoderContext {
39 AVCodecContext *avctx;
40 AVFrame picture;
41
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
55} TiffEncoderContext;
56
57
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 */
64inline 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/**
75 * Put n values to buffer
76 *
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
82 */
83static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
84 int flip)
85{
86 int i;
87#ifdef WORDS_BIGENDIAN
88 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
89#endif
90 for (i = 0; i < n * type_sizes2[type]; i++)
91 *(*p)++ = val[i ^ flip];
92}
93
94/**
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
101 */
102static void add_entry(TiffEncoderContext * s,
103 enum TiffTags tag, enum TiffTypes type, int count,
104 const void *ptr_val)
105{
106 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
107
108 assert(s->num_entries < TIFF_MAX_ENTRY);
109
110 bytestream_put_le16(&entries_ptr, tag);
111 bytestream_put_le16(&entries_ptr, type);
112 bytestream_put_le32(&entries_ptr, count);
113
114 if (type_sizes[type] * count <= 4) {
115 tnput(&entries_ptr, count, ptr_val, type, 0);
116 } else {
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);
120 }
121
122 s->num_entries++;
123}
124
125/**
126 * Encode one strip in tiff file
127 *
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
134 */
135static int encode_strip(TiffEncoderContext * s, const int8_t * src,
136 uint8_t * dst, int n, int compr)
137{
138
139 switch (compr) {
140#ifdef CONFIG_ZLIB
141 case TIFF_DEFLATE:
142 case TIFF_ADOBE_DEFLATE:
143 {
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");
147 return -1;
148 }
149 return zlen;
150 }
151#endif
152 case TIFF_RAW:
153 if (check_size(s, n))
154 return -1;
155 memcpy(dst, src, n);
156 return n;
157 case TIFF_PACKBITS:
158 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
159 default:
160 return -1;
161 }
162}
163
164static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
165 int buf_size, void *data)
166{
167 TiffEncoderContext *s = avctx->priv_data;
168 AVFrame *pict = data;
169 AVFrame *const p = (AVFrame *) & s->picture;
170 int i;
171 int n;
172 uint8_t *ptr = buf;
173 uint8_t *offset;
174 uint32_t strips;
175 uint32_t *strip_sizes = NULL;
176 uint32_t *strip_offsets = NULL;
177 int bytes_per_row;
178 uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
179 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
180 int ret = -1;
181
182 s->buf_start = buf;
183 s->buf = &ptr;
184 s->buf_size = buf_size;
185
186 *p = *pict;
187 p->pict_type = FF_I_TYPE;
188 p->key_frame = 1;
189
190 s->compr = TIFF_PACKBITS;
191 if (avctx->compression_level == 0) {
192 s->compr = TIFF_RAW;
193#ifdef CONFIG_ZLIB
194 } else if ((avctx->compression_level > 2)) {
195 s->compr = TIFF_DEFLATE;
196#endif
197 }
198
199 s->width = avctx->width;
200 s->height = avctx->height;
201
202 switch (avctx->pix_fmt) {
203 case PIX_FMT_RGB24:
204 s->bpp = 24;
205 s->invert = 2;
206 break;
207 case PIX_FMT_GRAY8:
208 s->bpp = 8;
209 s->invert = 1;
210 break;
211 case PIX_FMT_PAL8:
212 s->bpp = 8;
213 s->invert = 3;
214 break;
215 case PIX_FMT_MONOBLACK:
216 s->bpp = 1;
217 s->invert = 1;
218 break;
219 case PIX_FMT_MONOWHITE:
220 s->bpp = 1;
221 s->invert = 0;
222 break;
223 default:
224 av_log(s->avctx, AV_LOG_ERROR,
225 "This colors format is not supported\n");
226 return -1;
227 }
228 s->bpp_tab_size = (s->bpp >> 3);
229
230 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
231 //best choose for DEFLATE
232 s->rps = s->height;
233 else
234 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
235
236 strips = (s->height - 1) / s->rps + 1;
237
238 if (check_size(s, 8))
239 goto fail;
240
241 // write header
242 bytestream_put_le16(&ptr, 0x4949);
243 bytestream_put_le16(&ptr, 42);
244
245 offset = ptr;
246 bytestream_put_le32(&ptr, 0);
247
248 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
249 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
250
251 bytes_per_row = (s->width * s->bpp + 7) >> 3;
252
253#ifdef CONFIG_ZLIB
254 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
255 uint8_t *zbuf;
256 int zlen, zn;
257 int j;
258
259 zlen = bytes_per_row * s->rps;
260 zbuf = av_malloc(zlen);
261 strip_offsets[0] = ptr - buf;
262 zn = 0;
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);
266 zn += bytes_per_row;
267 }
268 n = encode_strip(s, zbuf, ptr, zn, s->compr);
269 av_free(zbuf);
270 if (n<0) {
271 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
272 goto fail;
273 }
274 ptr += n;
275 strip_sizes[0] = ptr - buf - strip_offsets[0];
276 } else
277#endif
278 {
279 for (i = 0; i < s->height; i++) {
280 if (strip_sizes[i / s->rps] == 0) {
281 strip_offsets[i / s->rps] = ptr - buf;
282 }
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");
286 goto fail;
287 }
288 strip_sizes[i / s->rps] += n;
289 ptr += n;
290 }
291 }
292
293 s->num_entries = 0;
294
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});
298
299 if (s->bpp_tab_size)
300 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
301
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);
305
306 if (s->bpp_tab_size)
307 add_entry(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, 1, (uint16_t[]) {s->bpp_tab_size});
308
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);
316
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;
324 }
325 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
326 }
327 bytestream_put_le32(&offset, ptr - buf); // write offset to dir
328
329 if (check_size(s, 6 + s->num_entries * 12))
330 goto fail;
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);
334
335 ret = ptr - buf;
336
337fail:
338 av_free(strip_sizes);
339 av_free(strip_offsets);
340 return ret;
341}
342
343AVCodec tiff_encoder = {
344 "tiff",
345 CODEC_TYPE_VIDEO,
346 CODEC_ID_TIFF,
347 sizeof(TiffEncoderContext),
348 NULL,
349 encode_frame,
350 NULL,
351 NULL,
352 0,
353 NULL,
354 .pix_fmts =
355 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
356 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
357 -1}
358
359};