FFmpeg coverage


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