FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiffenc.c
Date: 2026-04-30 18:27:06
Exec Total Coverage
Lines: 165 268 61.6%
Functions: 9 9 100.0%
Branches: 79 180 43.9%

Line Branch Exec Source
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 * @file
24 * TIFF image encoder
25 * @author Bartlomiej Wolowiec
26 */
27
28 #include "config.h"
29 #include "libavutil/attributes.h"
30 #if CONFIG_ZLIB
31 #include <zlib.h>
32 #endif
33
34 #include "libavutil/log.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "avcodec.h"
39 #include "bytestream.h"
40 #include "codec_internal.h"
41 #include "encode.h"
42 #include "lzw.h"
43 #include "rle.h"
44 #include "tiff.h"
45 #include "tiff_common.h"
46 #include "version.h"
47
48 #define TIFF_MAX_ENTRY 32
49
50 /** sizes of various TIFF field types (string size = 1)*/
51 static const uint8_t type_sizes2[14] = {
52 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
53 };
54
55 typedef struct TiffEncoderContext {
56 AVClass *class; ///< for private options
57 AVCodecContext *avctx;
58
59 int width; ///< picture width
60 int height; ///< picture height
61 unsigned int bpp; ///< bits per pixel
62 int compr; ///< compression level
63 int bpp_tab_size; ///< bpp_tab size
64 enum TiffPhotometric photometric_interpretation; ///< photometric interpretation
65 int strips; ///< number of strips
66 uint32_t *strip_sizes;
67 unsigned int strip_sizes_size;
68 uint32_t *strip_offsets;
69 unsigned int strip_offsets_size;
70 uint8_t *yuv_line;
71 unsigned int yuv_line_size;
72 int rps; ///< row per strip
73 uint8_t entries[TIFF_MAX_ENTRY * 12]; ///< entries in header
74 int num_entries; ///< number of entries
75 uint8_t **buf; ///< actual position in buffer
76 uint8_t *buf_start; ///< pointer to first byte in buffer
77 int buf_size; ///< buffer size
78 uint16_t subsampling[2]; ///< YUV subsampling factors
79 struct LZWEncodeState *lzws; ///< LZW encode state
80 uint32_t dpi; ///< image resolution in DPI
81 } TiffEncoderContext;
82
83 /**
84 * Check free space in buffer.
85 *
86 * @param s Tiff context
87 * @param need Needed bytes
88 * @return 0 - ok, 1 - no free space
89 */
90 107 static inline int check_size(TiffEncoderContext *s, uint64_t need)
91 {
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (s->buf_size < *s->buf - s->buf_start + need) {
93 *s->buf = s->buf_start + s->buf_size + 1;
94 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
95 return 1;
96 }
97 107 return 0;
98 }
99
100 /**
101 * Put n values to buffer.
102 *
103 * @param p pointer to pointer to output buffer
104 * @param n number of values
105 * @param val pointer to values
106 * @param type type of values
107 * @param flip = 0 - normal copy, >0 - flip
108 */
109 200 static void tnput(uint8_t **p, int n, const uint8_t *val, enum AVTiffDataType type,
110 int flip)
111 {
112 int i;
113 #if HAVE_BIGENDIAN
114 flip ^= ((int[]) { 0, 0, 0, 1, 3, 3 })[type];
115 #endif
116
2/2
✓ Branch 0 taken 5348 times.
✓ Branch 1 taken 200 times.
5548 for (i = 0; i < n * type_sizes2[type]; i++)
117 5348 *(*p)++ = val[i ^ flip];
118 200 }
119
120 /**
121 * Add entry to directory in tiff header.
122 *
123 * @param s Tiff context
124 * @param tag tag that identifies the entry
125 * @param type entry type
126 * @param count the number of values
127 * @param ptr_val pointer to values
128 */
129 200 static int add_entry(TiffEncoderContext *s, enum TiffTags tag,
130 enum AVTiffDataType type, int count, const void *ptr_val)
131 {
132 200 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
133
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 av_assert0(s->num_entries < TIFF_MAX_ENTRY);
135
136 200 bytestream_put_le16(&entries_ptr, tag);
137 200 bytestream_put_le16(&entries_ptr, type);
138 200 bytestream_put_le32(&entries_ptr, count);
139
140
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 77 times.
200 if (type_sizes[type] * (int64_t)count <= 4) {
141 123 tnput(&entries_ptr, count, ptr_val, type, 0);
142 } else {
143 77 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if (check_size(s, count * (int64_t)type_sizes2[type]))
145 return AVERROR_INVALIDDATA;
146 77 tnput(s->buf, count, ptr_val, type, 0);
147 }
148
149 200 s->num_entries++;
150 200 return 0;
151 }
152
153 121 static int add_entry1(TiffEncoderContext *s,
154 enum TiffTags tag, enum AVTiffDataType type, int val)
155 {
156 121 uint16_t w = val;
157 121 uint32_t dw = val;
158
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 60 times.
121 return add_entry(s, tag, type, 1,
159 type == AV_TIFF_SHORT ? (void *)&w : (void *)&dw);
160 }
161
162 /**
163 * Encode one strip in tiff file.
164 *
165 * @param s Tiff context
166 * @param src input buffer
167 * @param dst output buffer
168 * @param n size of input buffer
169 * @param compr compression method
170 * @return number of output bytes. If an output error is encountered, a negative
171 * value corresponding to an AVERROR error code is returned.
172 */
173 3994 static int encode_strip(TiffEncoderContext *s, const int8_t *src,
174 uint8_t *dst, int n, int compr)
175 {
176
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 3994 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3994 switch (compr) {
177 #if CONFIG_ZLIB
178 case TIFF_DEFLATE:
179 case TIFF_ADOBE_DEFLATE:
180 {
181 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
182 if (compress(dst, &zlen, src, n) != Z_OK) {
183 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
184 return AVERROR_EXTERNAL;
185 }
186 return zlen;
187 }
188 #endif
189 case TIFF_RAW:
190 if (check_size(s, n))
191 return AVERROR(EINVAL);
192 memcpy(dst, src, n);
193 return n;
194 3994 case TIFF_PACKBITS:
195 3994 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start),
196 src, 1, n, 2, 0xff, -1, 0);
197 case TIFF_LZW:
198 return ff_lzw_encode(s->lzws, src, n);
199 default:
200 av_log(s->avctx, AV_LOG_ERROR, "Unsupported compression method: %d\n",
201 compr);
202 return AVERROR(EINVAL);
203 }
204 }
205
206 250 static void pack_yuv(TiffEncoderContext *s, const AVFrame *p,
207 uint8_t *dst, int lnum)
208 {
209 int i, j, k;
210 250 int w = (s->width - 1) / s->subsampling[0] + 1;
211 250 const uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
212 250 const uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
213
2/4
✓ Branch 0 taken 250 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 250 times.
250 if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
214 for (i = 0; i < w; i++) {
215 for (j = 0; j < s->subsampling[1]; j++)
216 for (k = 0; k < s->subsampling[0]; k++)
217 *dst++ = p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
218 FFMIN(i * s->subsampling[0] + k, s->width-1)];
219 *dst++ = *pu++;
220 *dst++ = *pv++;
221 }
222 }else{
223
2/2
✓ Branch 0 taken 30000 times.
✓ Branch 1 taken 250 times.
30250 for (i = 0; i < w; i++) {
224
2/2
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 30000 times.
90000 for (j = 0; j < s->subsampling[1]; j++)
225
2/2
✓ Branch 0 taken 120000 times.
✓ Branch 1 taken 60000 times.
180000 for (k = 0; k < s->subsampling[0]; k++)
226 120000 *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
227 120000 i * s->subsampling[0] + k];
228 30000 *dst++ = *pu++;
229 30000 *dst++ = *pv++;
230 }
231 }
232 250 }
233
234 #define ADD_ENTRY(s, tag, type, count, ptr_val) \
235 do { \
236 ret = add_entry(s, tag, type, count, ptr_val); \
237 if (ret < 0) \
238 goto fail; \
239 } while (0)
240
241 #define ADD_ENTRY1(s, tag, type, val) \
242 do { \
243 ret = add_entry1(s, tag, type, val); \
244 if (ret < 0) \
245 goto fail; \
246 } while (0)
247
248 15 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
249 const AVFrame *pict, int *got_packet)
250 {
251 15 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
252 15 TiffEncoderContext *s = avctx->priv_data;
253 15 const AVFrame *const p = pict;
254 int i;
255 uint8_t *ptr;
256 uint8_t *offset;
257 uint32_t strips;
258 int bytes_per_row;
259 15 uint32_t res[2] = { s->dpi, 1 }; // image resolution (72/1)
260 uint16_t bpp_tab[4];
261 15 int ret = 0;
262 15 int is_yuv = 0, alpha = 0;
263 int shift_h, shift_v;
264 int packet_size;
265
266 15 s->width = avctx->width;
267 15 s->height = avctx->height;
268 15 s->subsampling[0] = 1;
269 15 s->subsampling[1] = 1;
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!desc)
272 return AVERROR(EINVAL);
273
274 15 avctx->bits_per_coded_sample =
275 15 s->bpp = av_get_bits_per_pixel(desc);
276 15 s->bpp_tab_size = desc->nb_components;
277
278
2/9
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
15 switch (avctx->pix_fmt) {
279 case AV_PIX_FMT_RGBA64LE:
280 case AV_PIX_FMT_RGBA:
281 alpha = 1;
282 av_fallthrough;
283 13 case AV_PIX_FMT_RGB48LE:
284 case AV_PIX_FMT_RGB24:
285 13 s->photometric_interpretation = TIFF_PHOTOMETRIC_RGB;
286 13 break;
287 case AV_PIX_FMT_GRAY8:
288 avctx->bits_per_coded_sample = 0x28;
289 av_fallthrough;
290 case AV_PIX_FMT_GRAY8A:
291 case AV_PIX_FMT_YA16LE:
292 alpha = avctx->pix_fmt == AV_PIX_FMT_GRAY8A || avctx->pix_fmt == AV_PIX_FMT_YA16LE;
293 av_fallthrough;
294 case AV_PIX_FMT_GRAY16LE:
295 case AV_PIX_FMT_MONOBLACK:
296 s->photometric_interpretation = TIFF_PHOTOMETRIC_BLACK_IS_ZERO;
297 break;
298 case AV_PIX_FMT_PAL8:
299 s->photometric_interpretation = TIFF_PHOTOMETRIC_PALETTE;
300 break;
301 case AV_PIX_FMT_MONOWHITE:
302 s->photometric_interpretation = TIFF_PHOTOMETRIC_WHITE_IS_ZERO;
303 break;
304 2 case AV_PIX_FMT_YUV420P:
305 case AV_PIX_FMT_YUV422P:
306 case AV_PIX_FMT_YUV440P:
307 case AV_PIX_FMT_YUV444P:
308 case AV_PIX_FMT_YUV410P:
309 case AV_PIX_FMT_YUV411P:
310 2 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
311 2 s->photometric_interpretation = TIFF_PHOTOMETRIC_YCBCR;
312 2 s->subsampling[0] = 1 << shift_h;
313 2 s->subsampling[1] = 1 << shift_v;
314 2 is_yuv = 1;
315 2 break;
316 default:
317 av_log(s->avctx, AV_LOG_ERROR,
318 "This colors format is not supported\n");
319 return AVERROR(EINVAL);
320 }
321
322
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 15 times.
60 for (i = 0; i < s->bpp_tab_size; i++)
323 45 bpp_tab[i] = desc->comp[i].depth;
324
325
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (s->compr == TIFF_DEFLATE ||
326
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 s->compr == TIFF_ADOBE_DEFLATE ||
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 s->compr == TIFF_LZW)
328 // best choice for DEFLATE
329 s->rps = s->height;
330 else
331 // suggest size of strip
332 15 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
333 // round rps up
334 15 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
335
336 15 strips = (s->height - 1) / s->rps + 1;
337
338 15 bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
339 15 s->subsampling[0] * s->subsampling[1] + 7) >> 3;
340 15 packet_size = avctx->height * bytes_per_row * 2 +
341 15 avctx->height * 4 + FF_INPUT_BUFFER_MIN_SIZE;
342
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_alloc_packet(avctx, pkt, packet_size)) < 0)
344 return ret;
345 15 ptr = pkt->data;
346 15 s->buf_start = pkt->data;
347 15 s->buf = &ptr;
348 15 s->buf_size = pkt->size;
349
350
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (check_size(s, 8)) {
351 ret = AVERROR(EINVAL);
352 goto fail;
353 }
354
355 // write header
356 15 bytestream_put_le16(&ptr, 0x4949);
357 15 bytestream_put_le16(&ptr, 42);
358
359 15 offset = ptr;
360 15 bytestream_put_le32(&ptr, 0);
361
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (strips > INT_MAX / FFMAX(sizeof(s->strip_sizes[0]), sizeof(s->strip_offsets[0]))) {
363 ret = AVERROR(ENOMEM);
364 goto fail;
365 }
366 15 av_fast_padded_mallocz(&s->strip_sizes , &s->strip_sizes_size , sizeof(s->strip_sizes [0]) * strips);
367 15 av_fast_padded_mallocz(&s->strip_offsets, &s->strip_offsets_size, sizeof(s->strip_offsets[0]) * strips);
368
369
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (!s->strip_sizes || !s->strip_offsets) {
370 ret = AVERROR(ENOMEM);
371 goto fail;
372 }
373
374
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (is_yuv) {
375 2 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->yuv_line == NULL) {
377 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
378 ret = AVERROR(ENOMEM);
379 goto fail;
380 }
381 }
382
383 #if CONFIG_ZLIB
384
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
15 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
385 uint8_t *zbuf;
386 int zlen, zn;
387 int j;
388
389 zlen = bytes_per_row * s->rps;
390 zbuf = av_malloc(zlen);
391 if (!zbuf) {
392 ret = AVERROR(ENOMEM);
393 goto fail;
394 }
395 s->strip_offsets[0] = ptr - pkt->data;
396 zn = 0;
397 for (j = 0; j < s->rps; j++) {
398 if (is_yuv) {
399 pack_yuv(s, p, s->yuv_line, j);
400 memcpy(zbuf + zn, s->yuv_line, bytes_per_row);
401 j += s->subsampling[1] - 1;
402 } else
403 memcpy(zbuf + j * bytes_per_row,
404 p->data[0] + j * p->linesize[0], bytes_per_row);
405 zn += bytes_per_row;
406 }
407 ret = encode_strip(s, zbuf, ptr, zn, s->compr);
408 av_free(zbuf);
409 if (ret < 0) {
410 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
411 goto fail;
412 }
413 ptr += ret;
414 s->strip_sizes[0] = ptr - pkt->data - s->strip_offsets[0];
415 } else
416 #endif
417 {
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->compr == TIFF_LZW) {
419 s->lzws = av_malloc(ff_lzw_encode_state_size);
420 if (!s->lzws) {
421 ret = AVERROR(ENOMEM);
422 goto fail;
423 }
424 }
425
2/2
✓ Branch 0 taken 3994 times.
✓ Branch 1 taken 15 times.
4009 for (i = 0; i < s->height; i++) {
426
2/2
✓ Branch 0 taken 569 times.
✓ Branch 1 taken 3425 times.
3994 if (s->strip_sizes[i / s->rps] == 0) {
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 569 times.
569 if (s->compr == TIFF_LZW) {
428 ff_lzw_encode_init(s->lzws, ptr,
429 s->buf_size - (*s->buf - s->buf_start),
430 12, FF_LZW_TIFF, 0);
431 }
432 569 s->strip_offsets[i / s->rps] = ptr - pkt->data;
433 }
434
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 3744 times.
3994 if (is_yuv) {
435 250 pack_yuv(s, p, s->yuv_line, i);
436 250 ret = encode_strip(s, s->yuv_line, ptr, bytes_per_row, s->compr);
437 250 i += s->subsampling[1] - 1;
438 } else
439 3744 ret = encode_strip(s, p->data[0] + i * p->linesize[0],
440 ptr, bytes_per_row, s->compr);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3994 times.
3994 if (ret < 0) {
442 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
443 goto fail;
444 }
445 3994 s->strip_sizes[i / s->rps] += ret;
446 3994 ptr += ret;
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3994 times.
3994 if (s->compr == TIFF_LZW &&
448 (i == s->height - 1 || i % s->rps == s->rps - 1)) {
449 ret = ff_lzw_encode_flush(s->lzws);
450 s->strip_sizes[(i / s->rps)] += ret;
451 ptr += ret;
452 }
453 }
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->compr == TIFF_LZW)
455 av_freep(&s->lzws);
456 }
457
458 15 s->num_entries = 0;
459
460
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_SUBFILE, AV_TIFF_LONG, 0);
461
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_WIDTH, AV_TIFF_LONG, s->width);
462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_HEIGHT, AV_TIFF_LONG, s->height);
463
464
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (s->bpp_tab_size)
465
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY(s, TIFF_BPP, AV_TIFF_SHORT, s->bpp_tab_size, bpp_tab);
466
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_COMPR, AV_TIFF_SHORT, s->compr);
468
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_PHOTOMETRIC, AV_TIFF_SHORT, s->photometric_interpretation);
469
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY(s, TIFF_STRIP_OFFS, AV_TIFF_LONG, strips, s->strip_offsets);
470
471 15 AVFrameSideData *sd = av_frame_get_side_data(pict, AV_FRAME_DATA_DISPLAYMATRIX);
472
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (sd) {
473 1 int orientation = av_exif_matrix_to_orientation((int32_t *) sd->data);
474
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (orientation >= 1 && orientation <= 8)
475
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ADD_ENTRY1(s, TIFF_ORIENTATION, AV_TIFF_SHORT, orientation);
476 }
477
478
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (s->bpp_tab_size)
479
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_SAMPLES_PER_PIXEL, AV_TIFF_SHORT, s->bpp_tab_size);
480
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_ROWSPERSTRIP, AV_TIFF_LONG, s->rps);
482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY(s, TIFF_STRIP_SIZE, AV_TIFF_LONG, strips, s->strip_sizes);
483
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY(s, TIFF_XRES, AV_TIFF_RATIONAL, 1, res);
484
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (avctx->sample_aspect_ratio.num > 0 &&
485
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->sample_aspect_ratio.den > 0) {
486 2 AVRational y = av_mul_q(av_make_q(s->dpi, 1),
487 avctx->sample_aspect_ratio);
488 2 res[0] = y.num;
489 2 res[1] = y.den;
490 }
491
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY(s, TIFF_YRES, AV_TIFF_RATIONAL, 1, res);
492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 ADD_ENTRY1(s, TIFF_RES_UNIT, AV_TIFF_SHORT, 2);
493
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
495 ADD_ENTRY(s, TIFF_SOFTWARE_NAME, AV_TIFF_STRING,
496 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
497
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
499 uint16_t pal[256 * 3];
500 for (i = 0; i < 256; i++) {
501 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
502 pal[i] = ((rgb >> 16) & 0xff) * 257;
503 pal[i + 256] = ((rgb >> 8) & 0xff) * 257;
504 pal[i + 512] = (rgb & 0xff) * 257;
505 }
506 ADD_ENTRY(s, TIFF_PAL, AV_TIFF_SHORT, 256 * 3, pal);
507 }
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (alpha)
509 ADD_ENTRY1(s,TIFF_EXTRASAMPLES, AV_TIFF_SHORT, 2);
510
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (is_yuv) {
511 /** according to CCIR Recommendation 601.1 */
512 2 uint32_t refbw[12] = { 15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1 };
513
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_ENTRY(s, TIFF_YCBCR_SUBSAMPLING, AV_TIFF_SHORT, 2, s->subsampling);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)
515 ADD_ENTRY1(s, TIFF_YCBCR_POSITIONING, AV_TIFF_SHORT, 2);
516
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ADD_ENTRY(s, TIFF_REFERENCE_BW, AV_TIFF_RATIONAL, 6, refbw);
517 }
518 // write offset to dir
519 15 bytestream_put_le32(&offset, ptr - pkt->data);
520
521
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (check_size(s, 6 + s->num_entries * 12)) {
522 ret = AVERROR(EINVAL);
523 goto fail;
524 }
525 15 bytestream_put_le16(&ptr, s->num_entries); // write tag count
526 15 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
527 15 bytestream_put_le32(&ptr, 0);
528
529 15 pkt->size = ptr - pkt->data;
530 15 *got_packet = 1;
531
532 15 fail:
533 15 return ret < 0 ? ret : 0;
534 }
535
536 3 static av_cold int encode_init(AVCodecContext *avctx)
537 {
538 3 TiffEncoderContext *s = avctx->priv_data;
539
540 #if !CONFIG_ZLIB
541 if (s->compr == TIFF_DEFLATE) {
542 av_log(avctx, AV_LOG_ERROR,
543 "Deflate compression needs zlib compiled in\n");
544 return AVERROR(ENOSYS);
545 }
546 #endif
547
548 3 s->avctx = avctx;
549
550 3 return 0;
551 }
552
553 3 static av_cold int encode_close(AVCodecContext *avctx)
554 {
555 3 TiffEncoderContext *s = avctx->priv_data;
556
557 3 av_freep(&s->strip_sizes);
558 3 av_freep(&s->strip_offsets);
559 3 av_freep(&s->yuv_line);
560
561 3 return 0;
562 }
563
564 #define OFFSET(x) offsetof(TiffEncoderContext, x)
565 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
566 static const AVOption options[] = {
567 {"dpi", "set the image resolution (in dpi)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 72}, 1, 0x10000, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
568 { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT, { .i64 = TIFF_PACKBITS }, TIFF_RAW, TIFF_DEFLATE, VE, .unit = "compression_algo" },
569 { "packbits", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_PACKBITS }, 0, 0, VE, .unit = "compression_algo" },
570 { "raw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_RAW }, 0, 0, VE, .unit = "compression_algo" },
571 { "lzw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_LZW }, 0, 0, VE, .unit = "compression_algo" },
572 { "deflate", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TIFF_DEFLATE }, 0, 0, VE, .unit = "compression_algo" },
573 { NULL },
574 };
575
576 static const AVClass tiffenc_class = {
577 .class_name = "TIFF encoder",
578 .item_name = av_default_item_name,
579 .option = options,
580 .version = LIBAVUTIL_VERSION_INT,
581 };
582
583 const FFCodec ff_tiff_encoder = {
584 .p.name = "tiff",
585 CODEC_LONG_NAME("TIFF image"),
586 .p.type = AVMEDIA_TYPE_VIDEO,
587 .p.id = AV_CODEC_ID_TIFF,
588 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
589 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
590 .priv_data_size = sizeof(TiffEncoderContext),
591 .init = encode_init,
592 .close = encode_close,
593 FF_CODEC_ENCODE_CB(encode_frame),
594 CODEC_PIXFMTS(
595 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48LE, AV_PIX_FMT_PAL8,
596 AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA64LE,
597 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_YA16LE,
598 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_MONOWHITE,
599 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
600 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P),
601 .color_ranges = AVCOL_RANGE_MPEG,
602 .p.priv_class = &tiffenc_class,
603 };
604