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