tiff & sgi regression test
[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
ee5c8a9b
DB
23/**
24 * TIFF image encoder
25 * @file tiffenc.c
26 * @author Bartlomiej Wolowiec
27 */
716222db
MN
28#include "avcodec.h"
29#ifdef CONFIG_ZLIB
30#include <zlib.h>
31#endif
32#include "bytestream.h"
33#include "tiff.h"
34#include "rle.h"
35
36#define TIFF_MAX_ENTRY 32
37
38/** sizes of various TIFF field types (string size = 1)*/
39static const uint8_t type_sizes2[6] = {
40 0, 1, 1, 2, 4, 8
41};
42
43typedef struct TiffEncoderContext {
44 AVCodecContext *avctx;
45 AVFrame picture;
46
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
60} TiffEncoderContext;
61
62
63/**
64 * Check free space in buffer
65 * @param s Tiff context
66 * @param need Needed bytes
67 * @return 0 - ok, 1 - no free space
68 */
69inline static int check_size(TiffEncoderContext * s, uint64_t need)
70{
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");
74 return 1;
75 }
76 return 0;
77}
78
79/**
80 * Put n values to buffer
81 *
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
87 */
88static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
89 int flip)
90{
91 int i;
92#ifdef WORDS_BIGENDIAN
93 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
94#endif
95 for (i = 0; i < n * type_sizes2[type]; i++)
96 *(*p)++ = val[i ^ flip];
97}
98
99/**
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
106 */
107static void add_entry(TiffEncoderContext * s,
108 enum TiffTags tag, enum TiffTypes type, int count,
109 const void *ptr_val)
110{
111 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
112
113 assert(s->num_entries < TIFF_MAX_ENTRY);
114
115 bytestream_put_le16(&entries_ptr, tag);
116 bytestream_put_le16(&entries_ptr, type);
117 bytestream_put_le32(&entries_ptr, count);
118
119 if (type_sizes[type] * count <= 4) {
120 tnput(&entries_ptr, count, ptr_val, type, 0);
121 } else {
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);
125 }
126
127 s->num_entries++;
128}
129
0a7a33d2
MN
130static void add_entry1(TiffEncoderContext * s,
131 enum TiffTags tag, enum TiffTypes type, int val){
132 uint16_t w = val;
133 uint32_t dw= val;
134 add_entry(s, tag, type, 1, type == TIFF_SHORT ? &w : &dw);
135}
136
716222db
MN
137/**
138 * Encode one strip in tiff file
139 *
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
146 */
147static int encode_strip(TiffEncoderContext * s, const int8_t * src,
148 uint8_t * dst, int n, int compr)
149{
150
151 switch (compr) {
152#ifdef CONFIG_ZLIB
153 case TIFF_DEFLATE:
154 case TIFF_ADOBE_DEFLATE:
155 {
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");
159 return -1;
160 }
161 return zlen;
162 }
163#endif
164 case TIFF_RAW:
165 if (check_size(s, n))
166 return -1;
167 memcpy(dst, src, n);
168 return n;
169 case TIFF_PACKBITS:
170 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
171 default:
172 return -1;
173 }
174}
175
176static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
177 int buf_size, void *data)
178{
179 TiffEncoderContext *s = avctx->priv_data;
180 AVFrame *pict = data;
181 AVFrame *const p = (AVFrame *) & s->picture;
182 int i;
183 int n;
184 uint8_t *ptr = buf;
185 uint8_t *offset;
186 uint32_t strips;
187 uint32_t *strip_sizes = NULL;
188 uint32_t *strip_offsets = NULL;
189 int bytes_per_row;
190 uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
191 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
192 int ret = -1;
193
194 s->buf_start = buf;
195 s->buf = &ptr;
196 s->buf_size = buf_size;
197
198 *p = *pict;
199 p->pict_type = FF_I_TYPE;
200 p->key_frame = 1;
201
202 s->compr = TIFF_PACKBITS;
203 if (avctx->compression_level == 0) {
204 s->compr = TIFF_RAW;
205#ifdef CONFIG_ZLIB
206 } else if ((avctx->compression_level > 2)) {
207 s->compr = TIFF_DEFLATE;
208#endif
209 }
210
211 s->width = avctx->width;
212 s->height = avctx->height;
213
214 switch (avctx->pix_fmt) {
215 case PIX_FMT_RGB24:
216 s->bpp = 24;
217 s->invert = 2;
218 break;
219 case PIX_FMT_GRAY8:
220 s->bpp = 8;
221 s->invert = 1;
222 break;
223 case PIX_FMT_PAL8:
224 s->bpp = 8;
225 s->invert = 3;
226 break;
227 case PIX_FMT_MONOBLACK:
228 s->bpp = 1;
229 s->invert = 1;
230 break;
231 case PIX_FMT_MONOWHITE:
232 s->bpp = 1;
233 s->invert = 0;
234 break;
235 default:
236 av_log(s->avctx, AV_LOG_ERROR,
237 "This colors format is not supported\n");
238 return -1;
239 }
240 s->bpp_tab_size = (s->bpp >> 3);
241
242 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
243 //best choose for DEFLATE
244 s->rps = s->height;
245 else
246 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
247
248 strips = (s->height - 1) / s->rps + 1;
249
250 if (check_size(s, 8))
251 goto fail;
252
253 // write header
254 bytestream_put_le16(&ptr, 0x4949);
255 bytestream_put_le16(&ptr, 42);
256
257 offset = ptr;
258 bytestream_put_le32(&ptr, 0);
259
260 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
261 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
262
263 bytes_per_row = (s->width * s->bpp + 7) >> 3;
264
265#ifdef CONFIG_ZLIB
266 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
267 uint8_t *zbuf;
268 int zlen, zn;
269 int j;
270
271 zlen = bytes_per_row * s->rps;
272 zbuf = av_malloc(zlen);
273 strip_offsets[0] = ptr - buf;
274 zn = 0;
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);
278 zn += bytes_per_row;
279 }
280 n = encode_strip(s, zbuf, ptr, zn, s->compr);
281 av_free(zbuf);
282 if (n<0) {
283 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
284 goto fail;
285 }
286 ptr += n;
287 strip_sizes[0] = ptr - buf - strip_offsets[0];
288 } else
289#endif
290 {
291 for (i = 0; i < s->height; i++) {
292 if (strip_sizes[i / s->rps] == 0) {
293 strip_offsets[i / s->rps] = ptr - buf;
294 }
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");
298 goto fail;
299 }
300 strip_sizes[i / s->rps] += n;
301 ptr += n;
302 }
303 }
304
305 s->num_entries = 0;
306
0a7a33d2
MN
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);
716222db
MN
310
311 if (s->bpp_tab_size)
312 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
313
0a7a33d2
MN
314 add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
315 add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->invert);
716222db
MN
316 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
317
318 if (s->bpp_tab_size)
0a7a33d2 319 add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
716222db 320
0a7a33d2 321 add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
716222db
MN
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);
0a7a33d2 325 add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
716222db
MN
326 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
327 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
328
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;
336 }
337 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
338 }
339 bytestream_put_le32(&offset, ptr - buf); // write offset to dir
340
341 if (check_size(s, 6 + s->num_entries * 12))
342 goto fail;
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);
346
347 ret = ptr - buf;
348
349fail:
350 av_free(strip_sizes);
351 av_free(strip_offsets);
352 return ret;
353}
354
355AVCodec tiff_encoder = {
356 "tiff",
357 CODEC_TYPE_VIDEO,
358 CODEC_ID_TIFF,
359 sizeof(TiffEncoderContext),
360 NULL,
361 encode_frame,
362 NULL,
363 NULL,
364 0,
365 NULL,
366 .pix_fmts =
367 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
368 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
369 -1}
370
371};