32 #include "libavutil/attributes.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/imgutils.h"
84 #define RET_GEOKEY(TYPE, array, element)\
85 if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
86 key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
87 return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
132 #define RET_GEOKEY_VAL(TYPE, array)\
133 if (val >= TIFF_##TYPE##_OFFSET &&\
134 val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
135 return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
187 snprintf(ap, 14,
"Unknown-%d", val);
195 uint64_t component_len;
196 if (!sep) sep =
", ";
197 component_len = 15LL + strlen(sep);
198 if (count >= (INT_MAX - 1)/component_len)
200 ap =
av_malloc(component_len * count + 1);
205 for (i = 0; i <
count; i++) {
206 unsigned l =
snprintf(ap, component_len,
"%f%s", dp[i], sep);
207 if(l >= component_len) {
213 ap0[strlen(ap0) - strlen(sep)] =
'\0';
234 while (--width >= 0) {
235 dst[(width+
offset)*8+7] = (usePtr ? src[width] : c) & 0x1;
236 dst[(width+
offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
237 dst[(width+
offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
238 dst[(width+
offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
239 dst[(width+
offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
240 dst[(width+
offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
241 dst[(width+
offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
242 dst[(width+
offset)*8+0] = (usePtr ? src[width] : c) >> 7;
246 while (--width >= 0) {
247 dst[(width+
offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
248 dst[(width+
offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
249 dst[(width+
offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
250 dst[(width+
offset)*4+0] = (usePtr ? src[width] : c) >> 6;
254 while (--width >= 0) {
255 dst[(width+
offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
256 dst[(width+
offset)*2+0] = (usePtr ? src[width] : c) >> 4;
261 memcpy(dst + offset, src, width);
263 memset(dst + offset, c, width);
275 for (i = 0; i <
size; i++)
285 z_stream zstream = { 0 };
288 zstream.next_in = (
uint8_t *)src;
289 zstream.avail_in =
size;
290 zstream.next_out = dst;
291 zstream.avail_out = *
len;
292 zret = inflateInit(&zstream);
297 zret = inflate(&zstream, Z_SYNC_FLUSH);
298 inflateEnd(&zstream);
299 *len = zstream.total_out;
300 return zret == Z_STREAM_END ? Z_OK : zret;
305 int width,
int lines)
308 unsigned long outlen;
310 outlen = width * lines;
321 ret = tiff_uncompress(zbuf, &outlen, src, size);
324 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
325 (
unsigned long)width * lines, ret);
330 for (line = 0; line < lines; line++) {
334 memcpy(dst, src, width);
346 const uint8_t *src,
int size,
int width,
int lines)
355 "Error allocating temporary buffer\n");
364 memcpy(src2, src, size);
366 for (i = 0; i <
size; i++)
373 for (line = 0; line < lines; line++) {
382 const uint8_t *src,
int size,
int lines)
384 int c, line, pixels, code,
ret;
386 int width = ((s->
width * s->
bpp) + 7) >> 3;
396 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
399 "zlib support not enabled, "
400 "deflate compression not supported\n");
410 if (size > 1 && !src[0] && (src[1]&1)) {
423 for (line = 0; line < lines; line++) {
424 if (src - ssrc > size) {
430 if (ssrc + size - src < width)
434 dst, 1, src, 0, width, 0);
437 for (i = 0; i <
width; i++)
443 for (pixels = 0; pixels <
width;) {
444 if (ssrc + size - src < 2) {
451 if (pixels + code > width ||
452 ssrc + size - src < code) {
454 "Copy went out of bounds\n");
458 dst, 1, src, 0, code, pixels);
461 }
else if (code != -128) {
463 if (pixels + code > width) {
465 "Run went out of bounds\n");
470 dst, 0, NULL, c, code, pixels);
476 for (i = 0; i <
width; i++)
482 if (pixels < width) {
544 "This format is not supported (bpp=%d, bppcount=%d)\n",
560 pal = (uint32_t *) frame->
f->
data[1];
561 for (i = 0; i < 1<<s->
bpp; i++)
562 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->
bpp) - 1) * 0x010101;
571 int i, j, k, pos,
start;
609 "This format is not supported (bpp=%d, %d components)\n",
623 for (i = 0; i <
count; i++)
634 "Samples per pixel requires a single value, many provided\n");
639 "Samples per pixel %d is too large\n", value);
678 if (type ==
TIFF_LONG && value == UINT_MAX)
682 "Incorrect value of rows per strip\n");
699 "Tag referencing position outside the image\n");
715 "Tag referencing position outside the image\n");
747 if (value < 1 || value > 2) {
749 "Unknown FillOrder value %d, trying default one\n", value);
760 for (k = 2; k >= 0; k--) {
761 for (i = 0; i < count / 3; i++) {
763 pal[i] = 0xFF
U << 24;
781 #define ADD_METADATA(count, name, sep)\
782 if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
783 av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
825 if (count >= INT_MAX /
sizeof(int64_t))
834 for (i = 0; i <
count; i++)
915 "Unknown or unsupported tag %d/0X%0X\n",
932 int le,
ret, plane, planes;
933 int i, j, entries,
stride;
934 unsigned soff, ssize;
945 }
else if (off >= UINT_MAX - 14 || avpkt->
size < off + 14) {
964 for (i = 0; i < entries; i++) {
1016 for (plane = 0; plane < planes; plane++) {
1018 dst = p->
data[plane];
1019 for (i = 0; i < s->
height; i += s->
rps) {
1030 if (soff > avpkt->
size || ssize > avpkt->
size - soff) {
1043 dst = p->
data[plane];
1047 ssize = s->
width * soff;
1052 for (i = 0; i < s->
height; i++) {
1053 for (j = soff; j < ssize; j += 2)
1061 for (i = 0; i < s->
height; i++) {
1062 for (j = soff; j < ssize; j += 2)
1067 for (i = 0; i < s->
height; i++) {
1068 for (j = soff; j < ssize; j++)
1069 dst[j] += dst[j - soff];
1076 dst = p->
data[plane];
1077 for (i = 0; i < s->
height; i++) {
1078 for (j = 0; j < p->
linesize[plane]; j++)
int ff_lzw_decode(LZWState *p, uint8_t *buf, int len)
Decode given number of bytes NOTE: the algorithm here is inspired from the LZW GIF decoder written by...
const char const char void * val
const TiffGeoTagKeyName ff_tiff_projection_codes[]
#define AVERROR_PATCHWELCOME
This structure describes decoded (raw) audio or video data.
static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride, const uint8_t *src, int size, int lines)
#define AV_LOG_WARNING
Something somehow does not look correct.
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
int ff_tadd_doubles_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count doubles converted to a string into the metadata dictionary.
static av_cold int init(AVCodecContext *avctx)
packed RGB 8:8:8, 24bpp, RGBRGB...
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
av_cold void ff_lzw_decode_close(LZWState **p)
static const char * search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
av_cold void ff_lzw_decode_open(LZWState **p)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static void free_geotags(TiffContext *const s)
int ff_tadd_shorts_metadata(int count, const char *name, const char *sep, GetByteContext *gb, int le, AVDictionary **metadata)
Adds count shorts converted to a string into the metadata dictionary.
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
static const uint8_t type_sizes[14]
sizes of various TIFF field types (string size = 100)
av_cold void ff_ccitt_unpack_init(void)
initialize upacker code
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
#define TIFF_GEO_KEY_USER_DEFINED
static const uint8_t offset[511][2]
static av_cold int end(AVCodecContext *avctx)
Multithreading support functions.
const char * name
Name of the codec implementation.
static av_cold int tiff_init(AVCodecContext *avctx)
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int add_metadata(int count, int type, const char *name, const char *sep, TiffContext *s, AVFrame *frame)
#define FFSWAP(type, a, b)
planar GBRA 4:4:4:4 64bpp, big-endian
static av_always_inline int bytestream2_size(GetByteContext *g)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
planar GBRA 4:4:4:4 32bpp
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_cold int tiff_end(AVCodecContext *avctx)
unsigned ff_tget_short(GetByteContext *gb, int le)
Reads a short from the bytestream using given endianess.
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
unsigned ff_tget(GetByteContext *gb, int type, int le)
Reads a byte from the bytestream using given endianess.
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride, const uint8_t *src, int size, int width, int lines)
int ff_ccitt_unpack(AVCodecContext *avctx, const uint8_t *src, int srcsize, uint8_t *dst, int height, int stride, enum TiffCompr compr, int opts)
unpack data compressed with CCITT Group 3 1/2-D or Group 4 method
Libavcodec external API header.
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
#define FF_ARRAY_ELEMS(a)
const TiffGeoTagKeyName ff_tiff_proj_cs_type_codes[]
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
int width
picture width / height.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
planar GBR 4:4:4 48bpp, big-endian
#define TIFF_GEO_KEY_UNDEFINED
static av_always_inline int bytestream2_tell(GetByteContext *g)
static const char * get_geokey_name(int key)
TiffCompr
list of TIFF compression types
main external API structure.
static void close(AVCodecParserContext *s)
#define AV_EF_EXPLODE
abort decoding on minor error detection
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int get_geokey_type(int key)
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
planar GBRA 4:4:4:4 64bpp, little-endian
Y , 16bpp, little-endian.
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
static char * get_geokey_val(int key, int val)
static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t *dst, int usePtr, const uint8_t *src, uint8_t c, int width, int offset)
static char * doubles2str(double *dp, int count, const char *sep)
int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size, int mode)
Initialize LZW decoder.
#define RET_GEOKEY_VAL(TYPE, array)
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
#define AVERROR_INVALIDDATA
int ff_tadd_string_metadata(int count, const char *name, GetByteContext *gb, int le, AVDictionary **metadata)
Adds a string of count characters into the metadata dictionary.
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, unsigned *count, int *next)
Reads the first 3 fields of a TIFF tag, which are the tag id, the tag type and the count of values fo...
#define RET_GEOKEY(TYPE, array, element)
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianess in *le and the offset to the f...
static int init_image(TiffContext *s, ThreadFrame *frame)
static int init_thread_copy(AVCodecContext *avctx)
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
planar GBR 4:4:4 48bpp, little-endian
const uint8_t ff_reverse[256]
8 bit with PIX_FMT_RGB32 palette
static int cmp_id_key(const void *id, const void *k)
double ff_tget_double(GetByteContext *gb, int le)
Reads a double from the bytestream using given endianess.
#define ADD_METADATA(count, name, sep)
This structure stores compressed data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
CCITT Fax Group 3 and 4 decompression.
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...