FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiffenc.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 128 263 48.7%
Branches: 56 172 32.6%

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