2 * Copyright (c) 2006 Konstantin Shishkov
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @author Konstantin Shishkov
32 #define LZMA_API_STATIC
36 #include "libavutil/attributes.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/imgutils.h"
41 #include "bytestream.h"
47 #include "tiff_data.h"
50 typedef struct TiffContext
{
51 AVCodecContext
*avctx
;
55 unsigned int bpp
, bppcount
;
56 uint32_t palette
[256];
60 enum TiffPhotometric photometric
;
68 int strips
, rps
, sstype
;
70 int stripsizesoff
, stripsize
, stripoff
, strippos
;
73 uint8_t *deinvert_buf
;
74 int deinvert_buf_size
;
76 unsigned int yuv_line_size
;
82 static void free_geotags(TiffContext
*const s
)
85 for (i
= 0; i
< s
->geotag_count
; i
++) {
86 if (s
->geotags
[i
].val
)
87 av_freep(&s
->geotags
[i
].val
);
89 av_freep(&s
->geotags
);
93 #define RET_GEOKEY(TYPE, array, element)\
94 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
95 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
96 return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
98 static const char *get_geokey_name(int key
)
100 RET_GEOKEY(VERT
, vert
, name
);
101 RET_GEOKEY(PROJ
, proj
, name
);
102 RET_GEOKEY(GEOG
, geog
, name
);
103 RET_GEOKEY(CONF
, conf
, name
);
108 static int get_geokey_type(int key
)
110 RET_GEOKEY(VERT
, vert
, type
);
111 RET_GEOKEY(PROJ
, proj
, type
);
112 RET_GEOKEY(GEOG
, geog
, type
);
113 RET_GEOKEY(CONF
, conf
, type
);
115 return AVERROR_INVALIDDATA
;
118 static int cmp_id_key(const void *id
, const void *k
)
120 return *(const int*)id
- ((const TiffGeoTagKeyName
*)k
)->key
;
123 static const char *search_keyval(const TiffGeoTagKeyName
*keys
, int n
, int id
)
125 TiffGeoTagKeyName
*r
= bsearch(&id
, keys
, n
, sizeof(keys
[0]), cmp_id_key
);
132 static char *get_geokey_val(int key
, int val
)
136 if (val
== TIFF_GEO_KEY_UNDEFINED
)
137 return av_strdup("undefined");
138 if (val
== TIFF_GEO_KEY_USER_DEFINED
)
139 return av_strdup("User-Defined");
141 #define RET_GEOKEY_VAL(TYPE, array)\
142 if (val >= TIFF_##TYPE##_OFFSET &&\
143 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
144 return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
147 case TIFF_GT_MODEL_TYPE_GEOKEY
:
148 RET_GEOKEY_VAL(GT_MODEL_TYPE
, gt_model_type
);
150 case TIFF_GT_RASTER_TYPE_GEOKEY
:
151 RET_GEOKEY_VAL(GT_RASTER_TYPE
, gt_raster_type
);
153 case TIFF_GEOG_LINEAR_UNITS_GEOKEY
:
154 case TIFF_PROJ_LINEAR_UNITS_GEOKEY
:
155 case TIFF_VERTICAL_UNITS_GEOKEY
:
156 RET_GEOKEY_VAL(LINEAR_UNIT
, linear_unit
);
158 case TIFF_GEOG_ANGULAR_UNITS_GEOKEY
:
159 case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY
:
160 RET_GEOKEY_VAL(ANGULAR_UNIT
, angular_unit
);
162 case TIFF_GEOGRAPHIC_TYPE_GEOKEY
:
163 RET_GEOKEY_VAL(GCS_TYPE
, gcs_type
);
164 RET_GEOKEY_VAL(GCSE_TYPE
, gcse_type
);
166 case TIFF_GEOG_GEODETIC_DATUM_GEOKEY
:
167 RET_GEOKEY_VAL(GEODETIC_DATUM
, geodetic_datum
);
168 RET_GEOKEY_VAL(GEODETIC_DATUM_E
, geodetic_datum_e
);
170 case TIFF_GEOG_ELLIPSOID_GEOKEY
:
171 RET_GEOKEY_VAL(ELLIPSOID
, ellipsoid
);
173 case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY
:
174 RET_GEOKEY_VAL(PRIME_MERIDIAN
, prime_meridian
);
176 case TIFF_PROJECTED_CS_TYPE_GEOKEY
:
177 ap
= av_strdup(search_keyval(ff_tiff_proj_cs_type_codes
, FF_ARRAY_ELEMS(ff_tiff_proj_cs_type_codes
), val
));
180 case TIFF_PROJECTION_GEOKEY
:
181 ap
= av_strdup(search_keyval(ff_tiff_projection_codes
, FF_ARRAY_ELEMS(ff_tiff_projection_codes
), val
));
184 case TIFF_PROJ_COORD_TRANS_GEOKEY
:
185 RET_GEOKEY_VAL(COORD_TRANS
, coord_trans
);
187 case TIFF_VERTICAL_CS_TYPE_GEOKEY
:
188 RET_GEOKEY_VAL(VERT_CS
, vert_cs
);
189 RET_GEOKEY_VAL(ORTHO_VERT_CS
, ortho_vert_cs
);
196 snprintf(ap
, 14, "Unknown-%d", val
);
200 static char *doubles2str(double *dp
, int count
, const char *sep
)
204 uint64_t component_len
;
205 if (!sep
) sep
= ", ";
206 component_len
= 24LL + strlen(sep
);
207 if (count
>= (INT_MAX
- 1)/component_len
)
209 ap
= av_malloc(component_len
* count
+ 1);
214 for (i
= 0; i
< count
; i
++) {
215 unsigned l
= snprintf(ap
, component_len
, "%.15g%s", dp
[i
], sep
);
216 if(l
>= component_len
) {
222 ap0
[strlen(ap0
) - strlen(sep
)] = '\0';
226 static int add_metadata(int count
, int type
,
227 const char *name
, const char *sep
, TiffContext
*s
, AVFrame
*frame
)
230 case TIFF_DOUBLE
: return ff_tadd_doubles_metadata(count
, name
, sep
, &s
->gb
, s
->le
, avpriv_frame_get_metadatap(frame
));
231 case TIFF_SHORT
: return ff_tadd_shorts_metadata(count
, name
, sep
, &s
->gb
, s
->le
, 0, avpriv_frame_get_metadatap(frame
));
232 case TIFF_STRING
: return ff_tadd_string_metadata(count
, name
, &s
->gb
, s
->le
, avpriv_frame_get_metadatap(frame
));
233 default : return AVERROR_INVALIDDATA
;
237 static void av_always_inline
horizontal_fill(unsigned int bpp
, uint8_t* dst
,
238 int usePtr
, const uint8_t *src
,
239 uint8_t c
, int width
, int offset
)
243 while (--width
>= 0) {
244 dst
[(width
+offset
)*8+7] = (usePtr
? src
[width
] : c
) & 0x1;
245 dst
[(width
+offset
)*8+6] = (usePtr
? src
[width
] : c
) >> 1 & 0x1;
246 dst
[(width
+offset
)*8+5] = (usePtr
? src
[width
] : c
) >> 2 & 0x1;
247 dst
[(width
+offset
)*8+4] = (usePtr
? src
[width
] : c
) >> 3 & 0x1;
248 dst
[(width
+offset
)*8+3] = (usePtr
? src
[width
] : c
) >> 4 & 0x1;
249 dst
[(width
+offset
)*8+2] = (usePtr
? src
[width
] : c
) >> 5 & 0x1;
250 dst
[(width
+offset
)*8+1] = (usePtr
? src
[width
] : c
) >> 6 & 0x1;
251 dst
[(width
+offset
)*8+0] = (usePtr
? src
[width
] : c
) >> 7;
255 while (--width
>= 0) {
256 dst
[(width
+offset
)*4+3] = (usePtr
? src
[width
] : c
) & 0x3;
257 dst
[(width
+offset
)*4+2] = (usePtr
? src
[width
] : c
) >> 2 & 0x3;
258 dst
[(width
+offset
)*4+1] = (usePtr
? src
[width
] : c
) >> 4 & 0x3;
259 dst
[(width
+offset
)*4+0] = (usePtr
? src
[width
] : c
) >> 6;
263 while (--width
>= 0) {
264 dst
[(width
+offset
)*2+1] = (usePtr
? src
[width
] : c
) & 0xF;
265 dst
[(width
+offset
)*2+0] = (usePtr
? src
[width
] : c
) >> 4;
270 memcpy(dst
+ offset
, src
, width
);
272 memset(dst
+ offset
, c
, width
);
277 static int deinvert_buffer(TiffContext
*s
, const uint8_t *src
, int size
)
281 av_fast_padded_malloc(&s
->deinvert_buf
, &s
->deinvert_buf_size
, size
);
282 if (!s
->deinvert_buf
)
283 return AVERROR(ENOMEM
);
284 for (i
= 0; i
< size
; i
++)
285 s
->deinvert_buf
[i
] = ff_reverse
[src
[i
]];
290 static void unpack_yuv(TiffContext
*s
, AVFrame
*p
,
291 const uint8_t *src
, int lnum
)
294 int w
= (s
->width
- 1) / s
->subsampling
[0] + 1;
295 uint8_t *pu
= &p
->data
[1][lnum
/ s
->subsampling
[1] * p
->linesize
[1]];
296 uint8_t *pv
= &p
->data
[2][lnum
/ s
->subsampling
[1] * p
->linesize
[2]];
297 if (s
->width
% s
->subsampling
[0] || s
->height
% s
->subsampling
[1]) {
298 for (i
= 0; i
< w
; i
++) {
299 for (j
= 0; j
< s
->subsampling
[1]; j
++)
300 for (k
= 0; k
< s
->subsampling
[0]; k
++)
301 p
->data
[0][FFMIN(lnum
+ j
, s
->height
-1) * p
->linesize
[0] +
302 FFMIN(i
* s
->subsampling
[0] + k
, s
->width
-1)] = *src
++;
307 for (i
= 0; i
< w
; i
++) {
308 for (j
= 0; j
< s
->subsampling
[1]; j
++)
309 for (k
= 0; k
< s
->subsampling
[0]; k
++)
310 p
->data
[0][(lnum
+ j
) * p
->linesize
[0] +
311 i
* s
->subsampling
[0] + k
] = *src
++;
319 static int tiff_uncompress(uint8_t *dst
, unsigned long *len
, const uint8_t *src
,
322 z_stream zstream
= { 0 };
325 zstream
.next_in
= (uint8_t *)src
;
326 zstream
.avail_in
= size
;
327 zstream
.next_out
= dst
;
328 zstream
.avail_out
= *len
;
329 zret
= inflateInit(&zstream
);
331 av_log(NULL
, AV_LOG_ERROR
, "Inflate init error: %d\n", zret
);
334 zret
= inflate(&zstream
, Z_SYNC_FLUSH
);
335 inflateEnd(&zstream
);
336 *len
= zstream
.total_out
;
337 return zret
== Z_STREAM_END
? Z_OK
: zret
;
340 static int tiff_unpack_zlib(TiffContext
*s
, AVFrame
*p
, uint8_t *dst
, int stride
,
341 const uint8_t *src
, int size
, int width
, int lines
,
342 int strip_start
, int is_yuv
)
345 unsigned long outlen
;
347 outlen
= width
* lines
;
348 zbuf
= av_malloc(outlen
);
350 return AVERROR(ENOMEM
);
352 if ((ret
= deinvert_buffer(s
, src
, size
)) < 0) {
356 src
= s
->deinvert_buf
;
358 ret
= tiff_uncompress(zbuf
, &outlen
, src
, size
);
360 av_log(s
->avctx
, AV_LOG_ERROR
,
361 "Uncompressing failed (%lu of %lu) with error %d\n", outlen
,
362 (unsigned long)width
* lines
, ret
);
364 return AVERROR_UNKNOWN
;
367 for (line
= 0; line
< lines
; line
++) {
368 if (s
->bpp
< 8 && s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
) {
369 horizontal_fill(s
->bpp
, dst
, 1, src
, 0, width
, 0);
371 memcpy(dst
, src
, width
);
374 unpack_yuv(s
, p
, dst
, strip_start
+ line
);
375 line
+= s
->subsampling
[1] - 1;
386 static int tiff_uncompress_lzma(uint8_t *dst
, uint64_t *len
, const uint8_t *src
,
389 lzma_stream stream
= LZMA_STREAM_INIT
;
392 stream
.next_in
= (uint8_t *)src
;
393 stream
.avail_in
= size
;
394 stream
.next_out
= dst
;
395 stream
.avail_out
= *len
;
396 ret
= lzma_stream_decoder(&stream
, UINT64_MAX
, 0);
397 if (ret
!= LZMA_OK
) {
398 av_log(NULL
, AV_LOG_ERROR
, "LZMA init error: %d\n", ret
);
401 ret
= lzma_code(&stream
, LZMA_RUN
);
403 *len
= stream
.total_out
;
404 return ret
== LZMA_STREAM_END
? LZMA_OK
: ret
;
407 static int tiff_unpack_lzma(TiffContext
*s
, AVFrame
*p
, uint8_t *dst
, int stride
,
408 const uint8_t *src
, int size
, int width
, int lines
,
409 int strip_start
, int is_yuv
)
411 uint64_t outlen
= width
* lines
;
413 uint8_t *buf
= av_malloc(outlen
);
415 return AVERROR(ENOMEM
);
417 if ((ret
= deinvert_buffer(s
, src
, size
)) < 0) {
421 src
= s
->deinvert_buf
;
423 ret
= tiff_uncompress_lzma(buf
, &outlen
, src
, size
);
424 if (ret
!= LZMA_OK
) {
425 av_log(s
->avctx
, AV_LOG_ERROR
,
426 "Uncompressing failed (%"PRIu64
" of %"PRIu64
") with error %d\n", outlen
,
427 (uint64_t)width
* lines
, ret
);
429 return AVERROR_UNKNOWN
;
432 for (line
= 0; line
< lines
; line
++) {
433 if (s
->bpp
< 8 && s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
) {
434 horizontal_fill(s
->bpp
, dst
, 1, src
, 0, width
, 0);
436 memcpy(dst
, src
, width
);
439 unpack_yuv(s
, p
, dst
, strip_start
+ line
);
440 line
+= s
->subsampling
[1] - 1;
450 static int tiff_unpack_fax(TiffContext
*s
, uint8_t *dst
, int stride
,
451 const uint8_t *src
, int size
, int width
, int lines
)
455 uint8_t *src2
= av_malloc((unsigned)size
+
456 FF_INPUT_BUFFER_PADDING_SIZE
);
459 av_log(s
->avctx
, AV_LOG_ERROR
,
460 "Error allocating temporary buffer\n");
461 return AVERROR(ENOMEM
);
463 if (s
->fax_opts
& 2) {
464 avpriv_request_sample(s
->avctx
, "Uncompressed fax mode");
466 return AVERROR_PATCHWELCOME
;
468 if (!s
->fill_order
) {
469 memcpy(src2
, src
, size
);
471 for (i
= 0; i
< size
; i
++)
472 src2
[i
] = ff_reverse
[src
[i
]];
474 memset(src2
+ size
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
475 ret
= ff_ccitt_unpack(s
->avctx
, src2
, size
, dst
, lines
, stride
,
476 s
->compr
, s
->fax_opts
);
477 if (s
->bpp
< 8 && s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
)
478 for (line
= 0; line
< lines
; line
++) {
479 horizontal_fill(s
->bpp
, dst
, 1, dst
, 0, width
, 0);
486 static int tiff_unpack_strip(TiffContext
*s
, AVFrame
*p
, uint8_t *dst
, int stride
,
487 const uint8_t *src
, int size
, int strip_start
, int lines
)
490 int c
, line
, pixels
, code
, ret
;
491 const uint8_t *ssrc
= src
;
492 int width
= ((s
->width
* s
->bpp
) + 7) >> 3;
493 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(p
->format
);
494 int is_yuv
= !(desc
->flags
& AV_PIX_FMT_FLAG_RGB
) &&
495 (desc
->flags
& AV_PIX_FMT_FLAG_PLANAR
) &&
496 desc
->nb_components
>= 3;
499 width
/= s
->bppcount
;
502 return AVERROR_INVALIDDATA
;
505 int bytes_per_row
= (((s
->width
- 1) / s
->subsampling
[0] + 1) * s
->bpp
*
506 s
->subsampling
[0] * s
->subsampling
[1] + 7) >> 3;
507 av_fast_padded_malloc(&s
->yuv_line
, &s
->yuv_line_size
, bytes_per_row
);
508 if (s
->yuv_line
== NULL
) {
509 av_log(s
->avctx
, AV_LOG_ERROR
, "Not enough memory\n");
510 return AVERROR(ENOMEM
);
515 width
= (s
->width
- 1) / s
->subsampling
[0] + 1;
516 width
= width
* s
->subsampling
[0] * s
->subsampling
[1] + 2*width
;
517 av_assert0(width
<= bytes_per_row
);
518 av_assert0(s
->bpp
== 24);
521 if (s
->compr
== TIFF_DEFLATE
|| s
->compr
== TIFF_ADOBE_DEFLATE
) {
523 return tiff_unpack_zlib(s
, p
, dst
, stride
, src
, size
, width
, lines
,
524 strip_start
, is_yuv
);
526 av_log(s
->avctx
, AV_LOG_ERROR
,
527 "zlib support not enabled, "
528 "deflate compression not supported\n");
529 return AVERROR(ENOSYS
);
532 if (s
->compr
== TIFF_LZMA
) {
534 return tiff_unpack_lzma(s
, p
, dst
, stride
, src
, size
, width
, lines
,
535 strip_start
, is_yuv
);
537 av_log(s
->avctx
, AV_LOG_ERROR
,
538 "LZMA support not enabled\n");
539 return AVERROR(ENOSYS
);
542 if (s
->compr
== TIFF_LZW
) {
544 if ((ret
= deinvert_buffer(s
, src
, size
)) < 0)
546 ssrc
= src
= s
->deinvert_buf
;
548 if (size
> 1 && !src
[0] && (src
[1]&1)) {
549 av_log(s
->avctx
, AV_LOG_ERROR
, "Old style LZW is unsupported\n");
551 if ((ret
= ff_lzw_decode_init(s
->lzw
, 8, src
, size
, FF_LZW_TIFF
)) < 0) {
552 av_log(s
->avctx
, AV_LOG_ERROR
, "Error initializing LZW decoder\n");
555 for (line
= 0; line
< lines
; line
++) {
556 pixels
= ff_lzw_decode(s
->lzw
, dst
, width
);
557 if (pixels
< width
) {
558 av_log(s
->avctx
, AV_LOG_ERROR
, "Decoded only %i bytes of %i\n",
560 return AVERROR_INVALIDDATA
;
562 if (s
->bpp
< 8 && s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
)
563 horizontal_fill(s
->bpp
, dst
, 1, dst
, 0, width
, 0);
565 unpack_yuv(s
, p
, dst
, strip_start
+ line
);
566 line
+= s
->subsampling
[1] - 1;
572 if (s
->compr
== TIFF_CCITT_RLE
||
573 s
->compr
== TIFF_G3
||
574 s
->compr
== TIFF_G4
) {
576 return AVERROR_INVALIDDATA
;
578 return tiff_unpack_fax(s
, dst
, stride
, src
, size
, width
, lines
);
581 bytestream2_init(&s
->gb
, src
, size
);
582 bytestream2_init_writer(&pb
, dst
, is_yuv
? s
->yuv_line_size
: (stride
* lines
));
584 for (line
= 0; line
< lines
; line
++) {
585 if (src
- ssrc
> size
) {
586 av_log(s
->avctx
, AV_LOG_ERROR
, "Source data overread\n");
587 return AVERROR_INVALIDDATA
;
590 if (bytestream2_get_bytes_left(&s
->gb
) == 0 || bytestream2_get_eof(&pb
))
592 bytestream2_seek_p(&pb
, stride
* line
, SEEK_SET
);
595 if (ssrc
+ size
- src
< width
)
596 return AVERROR_INVALIDDATA
;
598 if (!s
->fill_order
) {
599 horizontal_fill(s
->bpp
* (s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
),
600 dst
, 1, src
, 0, width
, 0);
603 for (i
= 0; i
< width
; i
++)
604 dst
[i
] = ff_reverse
[src
[i
]];
609 for (pixels
= 0; pixels
< width
;) {
610 if (ssrc
+ size
- src
< 2) {
611 av_log(s
->avctx
, AV_LOG_ERROR
, "Read went out of bounds\n");
612 return AVERROR_INVALIDDATA
;
614 code
= s
->fill_order
? (int8_t) ff_reverse
[*src
++]: (int8_t) *src
++;
617 if (pixels
+ code
> width
||
618 ssrc
+ size
- src
< code
) {
619 av_log(s
->avctx
, AV_LOG_ERROR
,
620 "Copy went out of bounds\n");
621 return AVERROR_INVALIDDATA
;
623 horizontal_fill(s
->bpp
* (s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
),
624 dst
, 1, src
, 0, code
, pixels
);
627 } else if (code
!= -128) { // -127..-1
629 if (pixels
+ code
> width
) {
630 av_log(s
->avctx
, AV_LOG_ERROR
,
631 "Run went out of bounds\n");
632 return AVERROR_INVALIDDATA
;
635 horizontal_fill(s
->bpp
* (s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
),
636 dst
, 0, NULL
, c
, code
, pixels
);
642 for (i
= 0; i
< width
; i
++)
643 dst
[i
] = ff_reverse
[dst
[i
]];
648 unpack_yuv(s
, p
, dst
, strip_start
+ line
);
649 line
+= s
->subsampling
[1] - 1;
656 static int init_image(TiffContext
*s
, ThreadFrame
*frame
)
659 int create_gray_palette
= 0;
661 switch (s
->planar
* 1000 + s
->bpp
* 10 + s
->bppcount
) {
663 if (!s
->palette_is_set
) {
664 s
->avctx
->pix_fmt
= AV_PIX_FMT_MONOBLACK
;
669 s
->avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
670 if (!s
->palette_is_set
) {
671 create_gray_palette
= 1;
675 s
->avctx
->pix_fmt
= s
->palette_is_set
? AV_PIX_FMT_PAL8
: AV_PIX_FMT_GRAY8
;
678 if (s
->photometric
== TIFF_PHOTOMETRIC_YCBCR
) {
679 if (s
->subsampling
[0] == 1 && s
->subsampling
[1] == 1) {
680 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV444P
;
681 } else if (s
->subsampling
[0] == 2 && s
->subsampling
[1] == 1) {
682 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV422P
;
683 } else if (s
->subsampling
[0] == 4 && s
->subsampling
[1] == 1) {
684 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV411P
;
685 } else if (s
->subsampling
[0] == 1 && s
->subsampling
[1] == 2) {
686 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV440P
;
687 } else if (s
->subsampling
[0] == 2 && s
->subsampling
[1] == 2) {
688 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
689 } else if (s
->subsampling
[0] == 4 && s
->subsampling
[1] == 4) {
690 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV410P
;
692 av_log(s
->avctx
, AV_LOG_ERROR
, "Unsupported YCbCr subsampling\n");
693 return AVERROR_PATCHWELCOME
;
696 s
->avctx
->pix_fmt
= AV_PIX_FMT_RGB24
;
699 s
->avctx
->pix_fmt
= s
->le
? AV_PIX_FMT_GRAY16LE
: AV_PIX_FMT_GRAY16BE
;
702 s
->avctx
->pix_fmt
= AV_PIX_FMT_YA8
;
705 s
->avctx
->pix_fmt
= s
->le
? AV_PIX_FMT_YA16LE
: AV_PIX_FMT_YA16BE
;
708 s
->avctx
->pix_fmt
= AV_PIX_FMT_RGBA
;
711 s
->avctx
->pix_fmt
= s
->le
? AV_PIX_FMT_RGB48LE
: AV_PIX_FMT_RGB48BE
;
714 s
->avctx
->pix_fmt
= s
->le
? AV_PIX_FMT_RGBA64LE
: AV_PIX_FMT_RGBA64BE
;
717 s
->avctx
->pix_fmt
= AV_PIX_FMT_GBRP
;
720 s
->avctx
->pix_fmt
= AV_PIX_FMT_GBRAP
;
723 s
->avctx
->pix_fmt
= s
->le
? AV_PIX_FMT_GBRP16LE
: AV_PIX_FMT_GBRP16BE
;
726 s
->avctx
->pix_fmt
= s
->le
? AV_PIX_FMT_GBRAP16LE
: AV_PIX_FMT_GBRAP16BE
;
729 av_log(s
->avctx
, AV_LOG_ERROR
,
730 "This format is not supported (bpp=%d, bppcount=%d)\n",
731 s
->bpp
, s
->bppcount
);
732 return AVERROR_INVALIDDATA
;
735 if (s
->photometric
== TIFF_PHOTOMETRIC_YCBCR
) {
736 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(s
->avctx
->pix_fmt
);
737 if((desc
->flags
& AV_PIX_FMT_FLAG_RGB
) ||
738 !(desc
->flags
& AV_PIX_FMT_FLAG_PLANAR
) ||
739 desc
->nb_components
< 3) {
740 av_log(s
->avctx
, AV_LOG_ERROR
, "Unsupported YCbCr variant\n");
741 return AVERROR_INVALIDDATA
;
745 if (s
->width
!= s
->avctx
->width
|| s
->height
!= s
->avctx
->height
) {
746 ret
= ff_set_dimensions(s
->avctx
, s
->width
, s
->height
);
750 if ((ret
= ff_thread_get_buffer(s
->avctx
, frame
, 0)) < 0)
752 if (s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
) {
753 if (!create_gray_palette
)
754 memcpy(frame
->f
->data
[1], s
->palette
, sizeof(s
->palette
));
756 /* make default grayscale pal */
758 uint32_t *pal
= (uint32_t *)frame
->f
->data
[1];
759 for (i
= 0; i
< 1<<s
->bpp
; i
++)
760 pal
[i
] = 0xFFU
<< 24 | i
* 255 / ((1<<s
->bpp
) - 1) * 0x010101;
766 static void set_sar(TiffContext
*s
, unsigned tag
, unsigned num
, unsigned den
)
768 int offset
= tag
== TIFF_YRES
? 2 : 0;
769 s
->res
[offset
++] = num
;
770 s
->res
[offset
] = den
;
771 if (s
->res
[0] && s
->res
[1] && s
->res
[2] && s
->res
[3])
772 av_reduce(&s
->avctx
->sample_aspect_ratio
.num
, &s
->avctx
->sample_aspect_ratio
.den
,
773 s
->res
[2] * (uint64_t)s
->res
[1], s
->res
[0] * (uint64_t)s
->res
[3], INT32_MAX
);
776 static int tiff_decode_tag(TiffContext
*s
, AVFrame
*frame
)
778 unsigned tag
, type
, count
, off
, value
= 0, value2
= 0;
784 ret
= ff_tread_tag(&s
->gb
, s
->le
, &tag
, &type
, &count
, &start
);
789 off
= bytestream2_tell(&s
->gb
);
795 value
= ff_tget(&s
->gb
, type
, s
->le
);
798 value
= ff_tget(&s
->gb
, TIFF_LONG
, s
->le
);
799 value2
= ff_tget(&s
->gb
, TIFF_LONG
, s
->le
);
819 av_log(s
->avctx
, AV_LOG_ERROR
,
820 "This format is not supported (bpp=%d, %d components)\n",
822 return AVERROR_INVALIDDATA
;
833 if (bytestream2_get_bytes_left(&s
->gb
) < type_sizes
[type
] * count
)
834 return AVERROR_INVALIDDATA
;
835 for (i
= 0; i
< count
; i
++)
836 s
->bpp
+= ff_tget(&s
->gb
, type
, s
->le
);
843 av_log(s
->avctx
, AV_LOG_ERROR
,
844 "This format is not supported (bpp=%d, %d components)\n",
847 return AVERROR_INVALIDDATA
;
850 case TIFF_SAMPLES_PER_PIXEL
:
852 av_log(s
->avctx
, AV_LOG_ERROR
,
853 "Samples per pixel requires a single value, many provided\n");
854 return AVERROR_INVALIDDATA
;
857 av_log(s
->avctx
, AV_LOG_ERROR
,
858 "Samples per pixel %d is too large\n", value
);
859 return AVERROR_INVALIDDATA
;
861 if (s
->bppcount
== 1)
879 case TIFF_ADOBE_DEFLATE
:
883 av_log(s
->avctx
, AV_LOG_ERROR
, "Deflate: ZLib not compiled in\n");
884 return AVERROR(ENOSYS
);
888 avpriv_report_missing_feature(s
->avctx
, "JPEG compression");
889 return AVERROR_PATCHWELCOME
;
894 av_log(s
->avctx
, AV_LOG_ERROR
, "LZMA not compiled in\n");
895 return AVERROR(ENOSYS
);
898 av_log(s
->avctx
, AV_LOG_ERROR
, "Unknown compression method %i\n",
900 return AVERROR_INVALIDDATA
;
903 case TIFF_ROWSPERSTRIP
:
904 if (!value
|| (type
== TIFF_LONG
&& value
== UINT_MAX
))
906 s
->rps
= FFMIN(value
, s
->height
);
908 case TIFF_STRIP_OFFS
:
919 case TIFF_STRIP_SIZE
:
921 s
->stripsizesoff
= 0;
922 s
->stripsize
= value
;
925 s
->stripsizesoff
= off
;
932 set_sar(s
, tag
, value
, value2
);
934 case TIFF_TILE_BYTE_COUNTS
:
935 case TIFF_TILE_LENGTH
:
936 case TIFF_TILE_OFFSETS
:
937 case TIFF_TILE_WIDTH
:
938 av_log(s
->avctx
, AV_LOG_ERROR
, "Tiled images are not supported\n");
939 return AVERROR_PATCHWELCOME
;
942 s
->predictor
= value
;
944 case TIFF_PHOTOMETRIC
:
946 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO
:
947 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO
:
948 case TIFF_PHOTOMETRIC_RGB
:
949 case TIFF_PHOTOMETRIC_PALETTE
:
950 case TIFF_PHOTOMETRIC_YCBCR
:
951 s
->photometric
= value
;
953 case TIFF_PHOTOMETRIC_ALPHA_MASK
:
954 case TIFF_PHOTOMETRIC_SEPARATED
:
955 case TIFF_PHOTOMETRIC_CIE_LAB
:
956 case TIFF_PHOTOMETRIC_ICC_LAB
:
957 case TIFF_PHOTOMETRIC_ITU_LAB
:
958 case TIFF_PHOTOMETRIC_CFA
:
959 case TIFF_PHOTOMETRIC_LOG_L
:
960 case TIFF_PHOTOMETRIC_LOG_LUV
:
961 case TIFF_PHOTOMETRIC_LINEAR_RAW
:
962 avpriv_report_missing_feature(s
->avctx
,
963 "PhotometricInterpretation 0x%04X",
965 return AVERROR_PATCHWELCOME
;
967 av_log(s
->avctx
, AV_LOG_ERROR
, "PhotometricInterpretation %u is "
969 return AVERROR_INVALIDDATA
;
972 case TIFF_FILL_ORDER
:
973 if (value
< 1 || value
> 2) {
974 av_log(s
->avctx
, AV_LOG_ERROR
,
975 "Unknown FillOrder value %d, trying default one\n", value
);
978 s
->fill_order
= value
- 1;
981 GetByteContext pal_gb
[3];
982 off
= type_sizes
[type
];
983 if (count
/ 3 > 256 ||
984 bytestream2_get_bytes_left(&s
->gb
) < count
/ 3 * off
* 3)
985 return AVERROR_INVALIDDATA
;
987 pal_gb
[0] = pal_gb
[1] = pal_gb
[2] = s
->gb
;
988 bytestream2_skip(&pal_gb
[1], count
/ 3 * off
);
989 bytestream2_skip(&pal_gb
[2], count
/ 3 * off
* 2);
991 off
= (type_sizes
[type
] - 1) << 3;
992 for (i
= 0; i
< count
/ 3; i
++) {
993 uint32_t p
= 0xFF000000;
994 p
|= (ff_tget(&pal_gb
[0], type
, s
->le
) >> off
) << 16;
995 p
|= (ff_tget(&pal_gb
[1], type
, s
->le
) >> off
) << 8;
996 p
|= ff_tget(&pal_gb
[2], type
, s
->le
) >> off
;
999 s
->palette_is_set
= 1;
1003 s
->planar
= value
== 2;
1005 case TIFF_YCBCR_SUBSAMPLING
:
1007 av_log(s
->avctx
, AV_LOG_ERROR
, "subsample count invalid\n");
1008 return AVERROR_INVALIDDATA
;
1010 for (i
= 0; i
< count
; i
++)
1011 s
->subsampling
[i
] = ff_tget(&s
->gb
, type
, s
->le
);
1013 case TIFF_T4OPTIONS
:
1014 if (s
->compr
== TIFF_G3
)
1015 s
->fax_opts
= value
;
1017 case TIFF_T6OPTIONS
:
1018 if (s
->compr
== TIFF_G4
)
1019 s
->fax_opts
= value
;
1021 #define ADD_METADATA(count, name, sep)\
1022 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1023 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1026 case TIFF_MODEL_PIXEL_SCALE
:
1027 ADD_METADATA(count
, "ModelPixelScaleTag", NULL
);
1029 case TIFF_MODEL_TRANSFORMATION
:
1030 ADD_METADATA(count
, "ModelTransformationTag", NULL
);
1032 case TIFF_MODEL_TIEPOINT
:
1033 ADD_METADATA(count
, "ModelTiepointTag", NULL
);
1035 case TIFF_GEO_KEY_DIRECTORY
:
1036 ADD_METADATA(1, "GeoTIFF_Version", NULL
);
1037 ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1038 s
->geotag_count
= ff_tget_short(&s
->gb
, s
->le
);
1039 if (s
->geotag_count
> count
/ 4 - 1) {
1040 s
->geotag_count
= count
/ 4 - 1;
1041 av_log(s
->avctx
, AV_LOG_WARNING
, "GeoTIFF key directory buffer shorter than specified\n");
1043 if (bytestream2_get_bytes_left(&s
->gb
) < s
->geotag_count
* sizeof(int16_t) * 4) {
1044 s
->geotag_count
= 0;
1047 s
->geotags
= av_mallocz_array(s
->geotag_count
, sizeof(TiffGeoTag
));
1049 av_log(s
->avctx
, AV_LOG_ERROR
, "Error allocating temporary buffer\n");
1050 s
->geotag_count
= 0;
1053 for (i
= 0; i
< s
->geotag_count
; i
++) {
1054 s
->geotags
[i
].key
= ff_tget_short(&s
->gb
, s
->le
);
1055 s
->geotags
[i
].type
= ff_tget_short(&s
->gb
, s
->le
);
1056 s
->geotags
[i
].count
= ff_tget_short(&s
->gb
, s
->le
);
1058 if (!s
->geotags
[i
].type
)
1059 s
->geotags
[i
].val
= get_geokey_val(s
->geotags
[i
].key
, ff_tget_short(&s
->gb
, s
->le
));
1061 s
->geotags
[i
].offset
= ff_tget_short(&s
->gb
, s
->le
);
1064 case TIFF_GEO_DOUBLE_PARAMS
:
1065 if (count
>= INT_MAX
/ sizeof(int64_t))
1066 return AVERROR_INVALIDDATA
;
1067 if (bytestream2_get_bytes_left(&s
->gb
) < count
* sizeof(int64_t))
1068 return AVERROR_INVALIDDATA
;
1069 dp
= av_malloc_array(count
, sizeof(double));
1071 av_log(s
->avctx
, AV_LOG_ERROR
, "Error allocating temporary buffer\n");
1074 for (i
= 0; i
< count
; i
++)
1075 dp
[i
] = ff_tget_double(&s
->gb
, s
->le
);
1076 for (i
= 0; i
< s
->geotag_count
; i
++) {
1077 if (s
->geotags
[i
].type
== TIFF_GEO_DOUBLE_PARAMS
) {
1078 if (s
->geotags
[i
].count
== 0
1079 || s
->geotags
[i
].offset
+ s
->geotags
[i
].count
> count
) {
1080 av_log(s
->avctx
, AV_LOG_WARNING
, "Invalid GeoTIFF key %d\n", s
->geotags
[i
].key
);
1082 char *ap
= doubles2str(&dp
[s
->geotags
[i
].offset
], s
->geotags
[i
].count
, ", ");
1084 av_log(s
->avctx
, AV_LOG_ERROR
, "Error allocating temporary buffer\n");
1086 return AVERROR(ENOMEM
);
1088 s
->geotags
[i
].val
= ap
;
1094 case TIFF_GEO_ASCII_PARAMS
:
1095 pos
= bytestream2_tell(&s
->gb
);
1096 for (i
= 0; i
< s
->geotag_count
; i
++) {
1097 if (s
->geotags
[i
].type
== TIFF_GEO_ASCII_PARAMS
) {
1098 if (s
->geotags
[i
].count
== 0
1099 || s
->geotags
[i
].offset
+ s
->geotags
[i
].count
> count
) {
1100 av_log(s
->avctx
, AV_LOG_WARNING
, "Invalid GeoTIFF key %d\n", s
->geotags
[i
].key
);
1104 bytestream2_seek(&s
->gb
, pos
+ s
->geotags
[i
].offset
, SEEK_SET
);
1105 if (bytestream2_get_bytes_left(&s
->gb
) < s
->geotags
[i
].count
)
1106 return AVERROR_INVALIDDATA
;
1107 ap
= av_malloc(s
->geotags
[i
].count
);
1109 av_log(s
->avctx
, AV_LOG_ERROR
, "Error allocating temporary buffer\n");
1110 return AVERROR(ENOMEM
);
1112 bytestream2_get_bufferu(&s
->gb
, ap
, s
->geotags
[i
].count
);
1113 ap
[s
->geotags
[i
].count
- 1] = '\0'; //replace the "|" delimiter with a 0 byte
1114 s
->geotags
[i
].val
= ap
;
1120 ADD_METADATA(count
, "artist", NULL
);
1122 case TIFF_COPYRIGHT
:
1123 ADD_METADATA(count
, "copyright", NULL
);
1126 ADD_METADATA(count
, "date", NULL
);
1128 case TIFF_DOCUMENT_NAME
:
1129 ADD_METADATA(count
, "document_name", NULL
);
1131 case TIFF_HOST_COMPUTER
:
1132 ADD_METADATA(count
, "computer", NULL
);
1134 case TIFF_IMAGE_DESCRIPTION
:
1135 ADD_METADATA(count
, "description", NULL
);
1138 ADD_METADATA(count
, "make", NULL
);
1141 ADD_METADATA(count
, "model", NULL
);
1143 case TIFF_PAGE_NAME
:
1144 ADD_METADATA(count
, "page_name", NULL
);
1146 case TIFF_PAGE_NUMBER
:
1147 ADD_METADATA(count
, "page_number", " / ");
1149 case TIFF_SOFTWARE_NAME
:
1150 ADD_METADATA(count
, "software", NULL
);
1153 if (s
->avctx
->err_recognition
& AV_EF_EXPLODE
) {
1154 av_log(s
->avctx
, AV_LOG_ERROR
,
1155 "Unknown or unsupported tag %d/0X%0X\n",
1157 return AVERROR_INVALIDDATA
;
1161 bytestream2_seek(&s
->gb
, start
, SEEK_SET
);
1165 static int decode_frame(AVCodecContext
*avctx
,
1166 void *data
, int *got_frame
, AVPacket
*avpkt
)
1168 TiffContext
*const s
= avctx
->priv_data
;
1169 AVFrame
*const p
= data
;
1170 ThreadFrame frame
= { .f
= data
};
1172 int le
, ret
, plane
, planes
;
1173 int i
, j
, entries
, stride
;
1174 unsigned soff
, ssize
;
1176 GetByteContext stripsizes
;
1177 GetByteContext stripdata
;
1179 bytestream2_init(&s
->gb
, avpkt
->data
, avpkt
->size
);
1181 // parse image header
1182 if ((ret
= ff_tdecode_header(&s
->gb
, &le
, &off
))) {
1183 av_log(avctx
, AV_LOG_ERROR
, "Invalid TIFF header\n");
1185 } else if (off
>= UINT_MAX
- 14 || avpkt
->size
< off
+ 14) {
1186 av_log(avctx
, AV_LOG_ERROR
, "IFD offset is greater than image size\n");
1187 return AVERROR_INVALIDDATA
;
1190 // TIFF_BPP is not a required tag and defaults to 1
1191 s
->bppcount
= s
->bpp
= 1;
1192 s
->photometric
= TIFF_PHOTOMETRIC_NONE
;
1193 s
->compr
= TIFF_RAW
;
1197 // Reset these offsets so we can tell if they were set this frame
1198 s
->stripsizesoff
= s
->strippos
= 0;
1199 /* parse image file directory */
1200 bytestream2_seek(&s
->gb
, off
, SEEK_SET
);
1201 entries
= ff_tget_short(&s
->gb
, le
);
1202 if (bytestream2_get_bytes_left(&s
->gb
) < entries
* 12)
1203 return AVERROR_INVALIDDATA
;
1204 for (i
= 0; i
< entries
; i
++) {
1205 if ((ret
= tiff_decode_tag(s
, p
)) < 0)
1209 for (i
= 0; i
<s
->geotag_count
; i
++) {
1210 const char *keyname
= get_geokey_name(s
->geotags
[i
].key
);
1212 av_log(avctx
, AV_LOG_WARNING
, "Unknown or unsupported GeoTIFF key %d\n", s
->geotags
[i
].key
);
1215 if (get_geokey_type(s
->geotags
[i
].key
) != s
->geotags
[i
].type
) {
1216 av_log(avctx
, AV_LOG_WARNING
, "Type of GeoTIFF key %d is wrong\n", s
->geotags
[i
].key
);
1219 ret
= av_dict_set(avpriv_frame_get_metadatap(p
), keyname
, s
->geotags
[i
].val
, 0);
1221 av_log(avctx
, AV_LOG_ERROR
, "Writing metadata with key '%s' failed\n", keyname
);
1226 if (!s
->strippos
&& !s
->stripoff
) {
1227 av_log(avctx
, AV_LOG_ERROR
, "Image data is missing\n");
1228 return AVERROR_INVALIDDATA
;
1230 /* now we have the data and may start decoding */
1231 if ((ret
= init_image(s
, &frame
)) < 0)
1234 if (s
->strips
== 1 && !s
->stripsize
) {
1235 av_log(avctx
, AV_LOG_WARNING
, "Image data size missing\n");
1236 s
->stripsize
= avpkt
->size
- s
->stripoff
;
1239 if (s
->stripsizesoff
) {
1240 if (s
->stripsizesoff
>= (unsigned)avpkt
->size
)
1241 return AVERROR_INVALIDDATA
;
1242 bytestream2_init(&stripsizes
, avpkt
->data
+ s
->stripsizesoff
,
1243 avpkt
->size
- s
->stripsizesoff
);
1246 if (s
->strippos
>= (unsigned)avpkt
->size
)
1247 return AVERROR_INVALIDDATA
;
1248 bytestream2_init(&stripdata
, avpkt
->data
+ s
->strippos
,
1249 avpkt
->size
- s
->strippos
);
1253 av_log(avctx
, AV_LOG_ERROR
, "rps %d invalid\n", s
->rps
);
1254 return AVERROR_INVALIDDATA
;
1257 planes
= s
->planar
? s
->bppcount
: 1;
1258 for (plane
= 0; plane
< planes
; plane
++) {
1259 stride
= p
->linesize
[plane
];
1260 dst
= p
->data
[plane
];
1261 for (i
= 0; i
< s
->height
; i
+= s
->rps
) {
1262 if (s
->stripsizesoff
)
1263 ssize
= ff_tget(&stripsizes
, s
->sstype
, le
);
1265 ssize
= s
->stripsize
;
1268 soff
= ff_tget(&stripdata
, s
->sot
, le
);
1272 if (soff
> avpkt
->size
|| ssize
> avpkt
->size
- soff
) {
1273 av_log(avctx
, AV_LOG_ERROR
, "Invalid strip size/offset\n");
1274 return AVERROR_INVALIDDATA
;
1276 if ((ret
= tiff_unpack_strip(s
, p
, dst
, stride
, avpkt
->data
+ soff
, ssize
, i
,
1277 FFMIN(s
->rps
, s
->height
- i
))) < 0) {
1278 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1282 dst
+= s
->rps
* stride
;
1284 if (s
->predictor
== 2) {
1285 if (s
->photometric
== TIFF_PHOTOMETRIC_YCBCR
) {
1286 av_log(s
->avctx
, AV_LOG_ERROR
, "predictor == 2 with YUV is unsupported");
1287 return AVERROR_PATCHWELCOME
;
1289 dst
= p
->data
[plane
];
1292 soff
= FFMAX(soff
/ s
->bppcount
, 1);
1293 ssize
= s
->width
* soff
;
1294 if (s
->avctx
->pix_fmt
== AV_PIX_FMT_RGB48LE
||
1295 s
->avctx
->pix_fmt
== AV_PIX_FMT_RGBA64LE
||
1296 s
->avctx
->pix_fmt
== AV_PIX_FMT_GBRP16LE
||
1297 s
->avctx
->pix_fmt
== AV_PIX_FMT_GBRAP16LE
) {
1298 for (i
= 0; i
< s
->height
; i
++) {
1299 for (j
= soff
; j
< ssize
; j
+= 2)
1300 AV_WL16(dst
+ j
, AV_RL16(dst
+ j
) + AV_RL16(dst
+ j
- soff
));
1303 } else if (s
->avctx
->pix_fmt
== AV_PIX_FMT_RGB48BE
||
1304 s
->avctx
->pix_fmt
== AV_PIX_FMT_RGBA64BE
||
1305 s
->avctx
->pix_fmt
== AV_PIX_FMT_GBRP16BE
||
1306 s
->avctx
->pix_fmt
== AV_PIX_FMT_GBRAP16BE
) {
1307 for (i
= 0; i
< s
->height
; i
++) {
1308 for (j
= soff
; j
< ssize
; j
+= 2)
1309 AV_WB16(dst
+ j
, AV_RB16(dst
+ j
) + AV_RB16(dst
+ j
- soff
));
1313 for (i
= 0; i
< s
->height
; i
++) {
1314 for (j
= soff
; j
< ssize
; j
++)
1315 dst
[j
] += dst
[j
- soff
];
1321 if (s
->photometric
== TIFF_PHOTOMETRIC_WHITE_IS_ZERO
) {
1322 dst
= p
->data
[plane
];
1323 for (i
= 0; i
< s
->height
; i
++) {
1324 for (j
= 0; j
< p
->linesize
[plane
]; j
++)
1325 dst
[j
] = (s
->avctx
->pix_fmt
== AV_PIX_FMT_PAL8
? (1<<s
->bpp
) - 1 : 255) - dst
[j
];
1331 if (s
->planar
&& s
->bppcount
> 2) {
1332 FFSWAP(uint8_t*, p
->data
[0], p
->data
[2]);
1333 FFSWAP(int, p
->linesize
[0], p
->linesize
[2]);
1334 FFSWAP(uint8_t*, p
->data
[0], p
->data
[1]);
1335 FFSWAP(int, p
->linesize
[0], p
->linesize
[1]);
1343 static av_cold
int tiff_init(AVCodecContext
*avctx
)
1345 TiffContext
*s
= avctx
->priv_data
;
1350 s
->subsampling
[1] = 1;
1352 ff_lzw_decode_open(&s
->lzw
);
1353 ff_ccitt_unpack_init();
1358 static av_cold
int tiff_end(AVCodecContext
*avctx
)
1360 TiffContext
*const s
= avctx
->priv_data
;
1364 ff_lzw_decode_close(&s
->lzw
);
1365 av_freep(&s
->deinvert_buf
);
1369 AVCodec ff_tiff_decoder
= {
1371 .long_name
= NULL_IF_CONFIG_SMALL("TIFF image"),
1372 .type
= AVMEDIA_TYPE_VIDEO
,
1373 .id
= AV_CODEC_ID_TIFF
,
1374 .priv_data_size
= sizeof(TiffContext
),
1377 .decode
= decode_frame
,
1378 .init_thread_copy
= ONLY_IF_THREADS_ENABLED(tiff_init
),
1379 .capabilities
= CODEC_CAP_DR1
| CODEC_CAP_FRAME_THREADS
,