Update doc to show FLAC encoding
[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
130/**
131 * Encode one strip in tiff file
132 *
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
139 */
140static int encode_strip(TiffEncoderContext * s, const int8_t * src,
141 uint8_t * dst, int n, int compr)
142{
143
144 switch (compr) {
145#ifdef CONFIG_ZLIB
146 case TIFF_DEFLATE:
147 case TIFF_ADOBE_DEFLATE:
148 {
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");
152 return -1;
153 }
154 return zlen;
155 }
156#endif
157 case TIFF_RAW:
158 if (check_size(s, n))
159 return -1;
160 memcpy(dst, src, n);
161 return n;
162 case TIFF_PACKBITS:
163 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
164 default:
165 return -1;
166 }
167}
168
169static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
170 int buf_size, void *data)
171{
172 TiffEncoderContext *s = avctx->priv_data;
173 AVFrame *pict = data;
174 AVFrame *const p = (AVFrame *) & s->picture;
175 int i;
176 int n;
177 uint8_t *ptr = buf;
178 uint8_t *offset;
179 uint32_t strips;
180 uint32_t *strip_sizes = NULL;
181 uint32_t *strip_offsets = NULL;
182 int bytes_per_row;
183 uint32_t res[2] = { 72, 1 }; // image resolution (72/1)
184 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
185 int ret = -1;
186
187 s->buf_start = buf;
188 s->buf = &ptr;
189 s->buf_size = buf_size;
190
191 *p = *pict;
192 p->pict_type = FF_I_TYPE;
193 p->key_frame = 1;
194
195 s->compr = TIFF_PACKBITS;
196 if (avctx->compression_level == 0) {
197 s->compr = TIFF_RAW;
198#ifdef CONFIG_ZLIB
199 } else if ((avctx->compression_level > 2)) {
200 s->compr = TIFF_DEFLATE;
201#endif
202 }
203
204 s->width = avctx->width;
205 s->height = avctx->height;
206
207 switch (avctx->pix_fmt) {
208 case PIX_FMT_RGB24:
209 s->bpp = 24;
210 s->invert = 2;
211 break;
212 case PIX_FMT_GRAY8:
213 s->bpp = 8;
214 s->invert = 1;
215 break;
216 case PIX_FMT_PAL8:
217 s->bpp = 8;
218 s->invert = 3;
219 break;
220 case PIX_FMT_MONOBLACK:
221 s->bpp = 1;
222 s->invert = 1;
223 break;
224 case PIX_FMT_MONOWHITE:
225 s->bpp = 1;
226 s->invert = 0;
227 break;
228 default:
229 av_log(s->avctx, AV_LOG_ERROR,
230 "This colors format is not supported\n");
231 return -1;
232 }
233 s->bpp_tab_size = (s->bpp >> 3);
234
235 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
236 //best choose for DEFLATE
237 s->rps = s->height;
238 else
239 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
240
241 strips = (s->height - 1) / s->rps + 1;
242
243 if (check_size(s, 8))
244 goto fail;
245
246 // write header
247 bytestream_put_le16(&ptr, 0x4949);
248 bytestream_put_le16(&ptr, 42);
249
250 offset = ptr;
251 bytestream_put_le32(&ptr, 0);
252
253 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
254 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
255
256 bytes_per_row = (s->width * s->bpp + 7) >> 3;
257
258#ifdef CONFIG_ZLIB
259 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
260 uint8_t *zbuf;
261 int zlen, zn;
262 int j;
263
264 zlen = bytes_per_row * s->rps;
265 zbuf = av_malloc(zlen);
266 strip_offsets[0] = ptr - buf;
267 zn = 0;
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);
271 zn += bytes_per_row;
272 }
273 n = encode_strip(s, zbuf, ptr, zn, s->compr);
274 av_free(zbuf);
275 if (n<0) {
276 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
277 goto fail;
278 }
279 ptr += n;
280 strip_sizes[0] = ptr - buf - strip_offsets[0];
281 } else
282#endif
283 {
284 for (i = 0; i < s->height; i++) {
285 if (strip_sizes[i / s->rps] == 0) {
286 strip_offsets[i / s->rps] = ptr - buf;
287 }
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");
291 goto fail;
292 }
293 strip_sizes[i / s->rps] += n;
294 ptr += n;
295 }
296 }
297
298 s->num_entries = 0;
299
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});
303
304 if (s->bpp_tab_size)
305 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
306
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);
310
311 if (s->bpp_tab_size)
312 add_entry(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, 1, (uint16_t[]) {s->bpp_tab_size});
313
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);
321
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;
329 }
330 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
331 }
332 bytestream_put_le32(&offset, ptr - buf); // write offset to dir
333
334 if (check_size(s, 6 + s->num_entries * 12))
335 goto fail;
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);
339
340 ret = ptr - buf;
341
342fail:
343 av_free(strip_sizes);
344 av_free(strip_offsets);
345 return ret;
346}
347
348AVCodec tiff_encoder = {
349 "tiff",
350 CODEC_TYPE_VIDEO,
351 CODEC_ID_TIFF,
352 sizeof(TiffEncoderContext),
353 NULL,
354 encode_frame,
355 NULL,
356 NULL,
357 0,
358 NULL,
359 .pix_fmts =
360 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
361 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
362 -1}
363
364};