2006-10-13 13:48:29 +03:00
|
|
|
/*
|
|
|
|
* TIFF image decoder
|
|
|
|
* Copyright (c) 2006 Konstantin Shishkov
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2006-10-13 13:48:29 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2006-10-13 13:48:29 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2006-10-13 13:48:29 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-10-13 13:48:29 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-04-04 14:43:08 +03:00
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2011-07-14 04:46:19 +03:00
|
|
|
* TIFF image decoder
|
2007-04-04 14:43:08 +03:00
|
|
|
* @author Konstantin Shishkov
|
|
|
|
*/
|
2011-07-14 04:46:19 +03:00
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "config.h"
|
2009-01-14 01:44:16 +02:00
|
|
|
#if CONFIG_ZLIB
|
2006-10-13 13:48:29 +03:00
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
2013-06-01 16:35:11 +03:00
|
|
|
|
2012-10-11 19:50:30 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2009-12-03 21:19:57 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2013-06-01 16:35:11 +03:00
|
|
|
#include "avcodec.h"
|
2013-09-30 02:47:55 +03:00
|
|
|
#include "bytestream.h"
|
2013-06-01 16:35:11 +03:00
|
|
|
#include "faxcompr.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "lzw.h"
|
|
|
|
#include "mathops.h"
|
|
|
|
#include "tiff.h"
|
2006-11-13 13:34:46 +02:00
|
|
|
|
2006-10-13 13:48:29 +03:00
|
|
|
typedef struct TiffContext {
|
|
|
|
AVCodecContext *avctx;
|
2013-09-30 02:47:55 +03:00
|
|
|
GetByteContext gb;
|
2006-10-13 13:48:29 +03:00
|
|
|
|
|
|
|
int width, height;
|
2011-05-09 13:51:24 +03:00
|
|
|
unsigned int bpp, bppcount;
|
2011-05-24 02:17:25 +03:00
|
|
|
uint32_t palette[256];
|
|
|
|
int palette_is_set;
|
2006-10-13 13:48:29 +03:00
|
|
|
int le;
|
2011-05-09 22:05:42 +03:00
|
|
|
enum TiffCompr compr;
|
2013-09-30 07:39:51 +03:00
|
|
|
enum TiffPhotometric photometric;
|
2014-12-17 15:54:44 +02:00
|
|
|
int planar;
|
2008-12-26 20:42:26 +02:00
|
|
|
int fax_opts;
|
2008-12-22 08:50:18 +02:00
|
|
|
int predictor;
|
2009-09-24 21:30:28 +03:00
|
|
|
int fill_order;
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2008-12-22 08:50:18 +02:00
|
|
|
int strips, rps, sstype;
|
2006-10-13 13:48:29 +03:00
|
|
|
int sot;
|
2013-09-30 02:47:55 +03:00
|
|
|
int stripsizesoff, stripsize, stripoff, strippos;
|
2006-10-26 07:15:48 +03:00
|
|
|
LZWState *lzw;
|
2006-10-13 13:48:29 +03:00
|
|
|
} TiffContext;
|
|
|
|
|
2013-09-30 02:47:55 +03:00
|
|
|
static unsigned tget_short(GetByteContext *gb, int le)
|
2012-04-13 04:28:37 +03:00
|
|
|
{
|
2013-09-30 02:47:55 +03:00
|
|
|
return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
|
2013-09-30 02:47:55 +03:00
|
|
|
static unsigned tget_long(GetByteContext *gb, int le)
|
2012-04-13 04:28:37 +03:00
|
|
|
{
|
2013-09-30 02:47:55 +03:00
|
|
|
return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
|
2013-09-30 02:47:55 +03:00
|
|
|
static unsigned tget(GetByteContext *gb, int type, int le)
|
2012-04-13 04:28:37 +03:00
|
|
|
{
|
|
|
|
switch (type) {
|
2013-09-30 02:47:55 +03:00
|
|
|
case TIFF_BYTE: return bytestream2_get_byte(gb);
|
|
|
|
case TIFF_SHORT: return tget_short(gb, le);
|
|
|
|
case TIFF_LONG: return tget_long(gb, le);
|
|
|
|
default: return UINT_MAX;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-27 12:43:11 +03:00
|
|
|
#if CONFIG_ZLIB
|
2012-04-13 04:28:37 +03:00
|
|
|
static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
|
|
|
|
int size)
|
2009-09-27 10:01:01 +03:00
|
|
|
{
|
2011-06-06 15:13:02 +03:00
|
|
|
z_stream zstream = { 0 };
|
2009-09-27 10:01:01 +03:00
|
|
|
int zret;
|
|
|
|
|
2013-06-01 16:35:11 +03:00
|
|
|
zstream.next_in = src;
|
|
|
|
zstream.avail_in = size;
|
|
|
|
zstream.next_out = dst;
|
2009-09-27 10:01:01 +03:00
|
|
|
zstream.avail_out = *len;
|
2013-06-01 16:35:11 +03:00
|
|
|
zret = inflateInit(&zstream);
|
2009-09-27 10:01:01 +03:00
|
|
|
if (zret != Z_OK) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
|
|
|
return zret;
|
|
|
|
}
|
|
|
|
zret = inflate(&zstream, Z_SYNC_FLUSH);
|
|
|
|
inflateEnd(&zstream);
|
|
|
|
*len = zstream.total_out;
|
|
|
|
return zret == Z_STREAM_END ? Z_OK : zret;
|
|
|
|
}
|
2013-06-03 04:51:05 +03:00
|
|
|
|
|
|
|
static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
|
|
|
|
const uint8_t *src, int size,
|
|
|
|
int width, int lines)
|
|
|
|
{
|
|
|
|
uint8_t *zbuf;
|
|
|
|
unsigned long outlen;
|
|
|
|
int ret, line;
|
|
|
|
outlen = width * lines;
|
|
|
|
zbuf = av_malloc(outlen);
|
|
|
|
if (!zbuf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = tiff_uncompress(zbuf, &outlen, src, size);
|
|
|
|
if (ret != Z_OK) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Uncompressing failed (%lu of %lu) with error %d\n", outlen,
|
|
|
|
(unsigned long)width * lines, ret);
|
|
|
|
av_free(zbuf);
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
src = zbuf;
|
|
|
|
for (line = 0; line < lines; line++) {
|
|
|
|
memcpy(dst, src, width);
|
|
|
|
dst += stride;
|
|
|
|
src += width;
|
|
|
|
}
|
|
|
|
av_free(zbuf);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-27 12:43:11 +03:00
|
|
|
#endif
|
2009-09-27 10:01:01 +03:00
|
|
|
|
2013-06-03 04:58:17 +03:00
|
|
|
|
|
|
|
static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
|
|
|
|
const uint8_t *src, int size, int lines)
|
|
|
|
{
|
|
|
|
int i, ret = 0;
|
|
|
|
uint8_t *src2 = av_malloc((unsigned)size +
|
|
|
|
FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
|
|
|
|
if (!src2) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Error allocating temporary buffer\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
if (s->fax_opts & 2) {
|
2013-06-03 05:00:43 +03:00
|
|
|
avpriv_request_sample(s->avctx, "Uncompressed fax mode");
|
2013-06-03 04:58:17 +03:00
|
|
|
av_free(src2);
|
2013-06-03 05:00:43 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2013-06-03 04:58:17 +03:00
|
|
|
}
|
|
|
|
if (!s->fill_order) {
|
|
|
|
memcpy(src2, src, size);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
src2[i] = ff_reverse[src[i]];
|
|
|
|
}
|
|
|
|
memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
|
|
|
|
s->compr, s->fax_opts);
|
|
|
|
av_free(src2);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
|
|
|
|
const uint8_t *src, int size, int lines)
|
|
|
|
{
|
2013-09-30 02:47:55 +03:00
|
|
|
PutByteContext pb;
|
2012-11-17 15:40:37 +03:00
|
|
|
int c, line, pixels, code, ret;
|
2013-09-30 02:47:55 +03:00
|
|
|
int width = ((s->width * s->bpp) + 7) >> 3;
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2014-12-17 15:54:44 +02:00
|
|
|
if (s->planar)
|
|
|
|
width /= s->bppcount;
|
|
|
|
|
2012-04-13 04:22:07 +03:00
|
|
|
if (size <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
|
2013-06-03 04:51:05 +03:00
|
|
|
#if CONFIG_ZLIB
|
|
|
|
return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
|
|
|
|
#else
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"zlib support not enabled, "
|
|
|
|
"deflate compression not supported\n");
|
|
|
|
return AVERROR(ENOSYS);
|
2006-10-13 13:48:29 +03:00
|
|
|
#endif
|
2013-06-03 04:51:05 +03:00
|
|
|
}
|
2012-04-13 04:28:37 +03:00
|
|
|
if (s->compr == TIFF_LZW) {
|
2012-11-17 15:40:37 +03:00
|
|
|
if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
|
2006-10-26 07:15:48 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
|
2012-11-17 15:40:37 +03:00
|
|
|
return ret;
|
2006-10-26 07:15:48 +03:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
for (line = 0; line < lines; line++) {
|
|
|
|
pixels = ff_lzw_decode(s->lzw, dst, width);
|
|
|
|
if (pixels < width) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
|
|
|
|
pixels, width);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-10-26 07:15:48 +03:00
|
|
|
}
|
2013-06-01 16:35:11 +03:00
|
|
|
if (s->compr == TIFF_CCITT_RLE ||
|
|
|
|
s->compr == TIFF_G3 ||
|
|
|
|
s->compr == TIFF_G4) {
|
2013-06-03 04:58:17 +03:00
|
|
|
return tiff_unpack_fax(s, dst, stride, src, size, lines);
|
2008-12-26 20:42:26 +02:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
|
|
|
|
bytestream2_init(&s->gb, src, size);
|
|
|
|
bytestream2_init_writer(&pb, dst, stride * lines);
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
for (line = 0; line < lines; line++) {
|
2013-09-30 02:47:55 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
|
|
|
|
break;
|
|
|
|
bytestream2_seek_p(&pb, stride * line, SEEK_SET);
|
2012-04-13 04:28:37 +03:00
|
|
|
switch (s->compr) {
|
2006-10-13 13:48:29 +03:00
|
|
|
case TIFF_RAW:
|
2011-05-09 22:59:20 +03:00
|
|
|
if (!s->fill_order) {
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_copy_buffer(&pb, &s->gb, width);
|
2011-05-09 22:59:20 +03:00
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < width; i++)
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_put_byte(&pb, ff_reverse[bytestream2_get_byte(&s->gb)]);
|
2011-05-09 22:59:20 +03:00
|
|
|
}
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_PACKBITS:
|
2012-04-13 04:28:37 +03:00
|
|
|
for (pixels = 0; pixels < width;) {
|
2013-09-30 02:47:55 +03:00
|
|
|
code = ff_u8_to_s8(bytestream2_get_byte(&s->gb));
|
2012-04-13 04:28:37 +03:00
|
|
|
if (code >= 0) {
|
2006-10-13 13:48:29 +03:00
|
|
|
code++;
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_copy_buffer(&pb, &s->gb, code);
|
2006-10-13 13:48:29 +03:00
|
|
|
pixels += code;
|
2012-04-13 04:28:37 +03:00
|
|
|
} else if (code != -128) { // -127..-1
|
2006-10-13 13:48:29 +03:00
|
|
|
code = (-code) + 1;
|
2013-09-30 02:47:55 +03:00
|
|
|
c = bytestream2_get_byte(&s->gb);
|
|
|
|
bytestream2_set_buffer(&pb, c, code);
|
2006-10-13 13:48:29 +03:00
|
|
|
pixels += code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
static int init_image(TiffContext *s, AVFrame *frame)
|
2011-05-09 13:51:24 +03:00
|
|
|
{
|
2014-08-09 16:19:45 +03:00
|
|
|
int ret;
|
2011-05-09 13:51:24 +03:00
|
|
|
|
2014-12-17 15:54:44 +02:00
|
|
|
switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
|
2011-05-09 13:51:24 +03:00
|
|
|
case 11:
|
2012-10-06 13:10:34 +03:00
|
|
|
s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
|
|
|
case 81:
|
2014-08-09 16:19:45 +03:00
|
|
|
s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
|
|
|
case 243:
|
2012-10-06 13:10:34 +03:00
|
|
|
s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
|
|
|
case 161:
|
2012-10-06 13:10:34 +03:00
|
|
|
s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
2014-07-20 09:34:19 +03:00
|
|
|
case 162:
|
|
|
|
s->avctx->pix_fmt = AV_PIX_FMT_YA8;
|
|
|
|
break;
|
2014-07-21 00:54:35 +03:00
|
|
|
case 322:
|
|
|
|
s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
|
|
|
|
break;
|
2011-05-09 13:51:24 +03:00
|
|
|
case 324:
|
2012-10-06 13:10:34 +03:00
|
|
|
s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
|
|
|
case 483:
|
2012-10-06 13:10:34 +03:00
|
|
|
s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
2014-12-10 20:16:09 +02:00
|
|
|
case 644:
|
|
|
|
s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE;
|
|
|
|
break;
|
2014-12-17 15:54:44 +02:00
|
|
|
case 1243:
|
|
|
|
s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
|
|
|
|
break;
|
|
|
|
case 1324:
|
|
|
|
s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
|
|
|
|
break;
|
|
|
|
case 1483:
|
|
|
|
s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
|
|
|
|
break;
|
|
|
|
case 1644:
|
|
|
|
s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
|
|
|
|
break;
|
2011-05-09 13:51:24 +03:00
|
|
|
default:
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"This format is not supported (bpp=%d, bppcount=%d)\n",
|
|
|
|
s->bpp, s->bppcount);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
if (s->width != s->avctx->width || s->height != s->avctx->height) {
|
2013-10-27 12:02:26 +03:00
|
|
|
ret = ff_set_dimensions(s->avctx, s->width, s->height);
|
|
|
|
if (ret < 0)
|
2011-05-09 13:51:24 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
|
2011-05-09 13:51:24 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-06 13:10:34 +03:00
|
|
|
if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
2014-08-09 16:19:45 +03:00
|
|
|
memcpy(frame->data[1], s->palette, sizeof(s->palette));
|
2011-05-09 13:51:24 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2013-09-30 02:47:55 +03:00
|
|
|
static int tiff_decode_tag(TiffContext *s)
|
2006-10-13 13:48:29 +03:00
|
|
|
{
|
2012-02-23 21:22:51 +03:00
|
|
|
unsigned tag, type, count, off, value = 0;
|
2013-09-30 02:47:55 +03:00
|
|
|
int i, start;
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2013-09-30 02:47:55 +03:00
|
|
|
if (bytestream2_get_bytes_left(&s->gb) < 12)
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-09-30 02:47:55 +03:00
|
|
|
tag = tget_short(&s->gb, s->le);
|
|
|
|
type = tget_short(&s->gb, s->le);
|
|
|
|
count = tget_long(&s->gb, s->le);
|
|
|
|
off = tget_long(&s->gb, s->le);
|
|
|
|
start = bytestream2_tell(&s->gb);
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2012-02-23 21:47:50 +03:00
|
|
|
if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
|
2012-04-13 04:28:37 +03:00
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
|
|
|
|
type);
|
2012-02-23 21:47:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
if (count == 1) {
|
|
|
|
switch (type) {
|
2006-10-13 13:48:29 +03:00
|
|
|
case TIFF_BYTE:
|
|
|
|
case TIFF_SHORT:
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, -4, SEEK_CUR);
|
|
|
|
value = tget(&s->gb, type, s->le);
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_LONG:
|
|
|
|
value = off;
|
|
|
|
break;
|
2008-12-17 19:39:35 +02:00
|
|
|
case TIFF_STRING:
|
2012-04-13 04:28:37 +03:00
|
|
|
if (count <= 4) {
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, -4, SEEK_CUR);
|
2008-12-17 19:39:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2006-10-13 13:48:29 +03:00
|
|
|
default:
|
2012-02-23 21:22:51 +03:00
|
|
|
value = UINT_MAX;
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, off, SEEK_SET);
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
2012-02-23 21:47:50 +03:00
|
|
|
} else {
|
2013-06-01 16:35:11 +03:00
|
|
|
if (count <= 4 && type_sizes[type] * count <= 4)
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, -4, SEEK_CUR);
|
2013-06-01 16:35:11 +03:00
|
|
|
else
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, off, SEEK_SET);
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
switch (tag) {
|
2006-10-13 13:48:29 +03:00
|
|
|
case TIFF_WIDTH:
|
|
|
|
s->width = value;
|
|
|
|
break;
|
|
|
|
case TIFF_HEIGHT:
|
|
|
|
s->height = value;
|
|
|
|
break;
|
|
|
|
case TIFF_BPP:
|
2011-05-09 13:51:24 +03:00
|
|
|
s->bppcount = count;
|
2012-04-13 04:28:37 +03:00
|
|
|
if (count > 4) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"This format is not supported (bpp=%d, %d components)\n",
|
|
|
|
s->bpp, count);
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-01-07 00:12:55 +02:00
|
|
|
}
|
2012-04-13 04:28:37 +03:00
|
|
|
if (count == 1)
|
|
|
|
s->bpp = value;
|
|
|
|
else {
|
|
|
|
switch (type) {
|
2006-10-13 13:48:29 +03:00
|
|
|
case TIFF_BYTE:
|
2012-04-13 04:28:37 +03:00
|
|
|
s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
|
|
|
|
((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_SHORT:
|
|
|
|
case TIFF_LONG:
|
2006-11-13 07:28:55 +02:00
|
|
|
s->bpp = 0;
|
2013-09-30 02:47:55 +03:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
s->bpp += tget(&s->gb, type, s->le);
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s->bpp = -1;
|
|
|
|
}
|
|
|
|
}
|
2011-05-09 13:51:24 +03:00
|
|
|
break;
|
|
|
|
case TIFF_SAMPLES_PER_PIXEL:
|
|
|
|
if (count != 1) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Samples per pixel requires a single value, many provided\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-13 07:32:10 +02:00
|
|
|
}
|
2011-05-09 13:51:24 +03:00
|
|
|
if (s->bppcount == 1)
|
|
|
|
s->bpp *= value;
|
|
|
|
s->bppcount = value;
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_COMPR:
|
2013-06-01 16:35:11 +03:00
|
|
|
s->compr = value;
|
2008-12-22 08:50:18 +02:00
|
|
|
s->predictor = 0;
|
2012-04-13 04:28:37 +03:00
|
|
|
switch (s->compr) {
|
2006-10-13 13:48:29 +03:00
|
|
|
case TIFF_RAW:
|
|
|
|
case TIFF_PACKBITS:
|
2006-10-26 07:15:48 +03:00
|
|
|
case TIFF_LZW:
|
2008-12-26 20:42:26 +02:00
|
|
|
case TIFF_CCITT_RLE:
|
|
|
|
break;
|
|
|
|
case TIFF_G3:
|
|
|
|
case TIFF_G4:
|
|
|
|
s->fax_opts = 0;
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_DEFLATE:
|
|
|
|
case TIFF_ADOBE_DEFLATE:
|
2009-01-14 01:44:16 +02:00
|
|
|
#if CONFIG_ZLIB
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
#else
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR(ENOSYS);
|
2006-10-13 13:48:29 +03:00
|
|
|
#endif
|
2006-11-13 07:29:45 +02:00
|
|
|
case TIFF_JPEG:
|
|
|
|
case TIFF_NEWJPEG:
|
2013-06-03 05:00:43 +03:00
|
|
|
avpriv_report_missing_feature(s->avctx, "JPEG compression");
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2014-08-18 11:27:49 +03:00
|
|
|
case TIFF_LZMA:
|
|
|
|
avpriv_report_missing_feature(s->avctx, "LZMA compression");
|
|
|
|
return AVERROR_PATCHWELCOME;
|
2006-10-13 13:48:29 +03:00
|
|
|
default:
|
2012-04-13 04:28:37 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
|
|
|
|
s->compr);
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIFF_ROWSPERSTRIP:
|
2013-09-30 07:11:12 +03:00
|
|
|
if (!value || (type == TIFF_LONG && value == UINT_MAX))
|
|
|
|
value = s->height;
|
|
|
|
s->rps = FFMIN(value, s->height);
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_STRIP_OFFS:
|
2012-04-13 04:28:37 +03:00
|
|
|
if (count == 1) {
|
2013-09-30 02:47:55 +03:00
|
|
|
s->strippos = 0;
|
|
|
|
s->stripoff = value;
|
2012-04-13 04:28:37 +03:00
|
|
|
} else
|
2013-09-30 02:47:55 +03:00
|
|
|
s->strippos = off;
|
2006-10-13 13:48:29 +03:00
|
|
|
s->strips = count;
|
2012-04-13 04:28:37 +03:00
|
|
|
if (s->strips == 1)
|
|
|
|
s->rps = s->height;
|
2006-10-13 13:48:29 +03:00
|
|
|
s->sot = type;
|
|
|
|
break;
|
|
|
|
case TIFF_STRIP_SIZE:
|
2012-04-13 04:28:37 +03:00
|
|
|
if (count == 1) {
|
2013-09-30 02:47:55 +03:00
|
|
|
s->stripsizesoff = 0;
|
|
|
|
s->stripsize = value;
|
|
|
|
s->strips = 1;
|
2012-04-13 04:28:37 +03:00
|
|
|
} else {
|
2013-09-30 02:47:55 +03:00
|
|
|
s->stripsizesoff = off;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
s->strips = count;
|
2008-12-22 08:50:18 +02:00
|
|
|
s->sstype = type;
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
|
|
|
case TIFF_PREDICTOR:
|
2008-12-22 08:50:18 +02:00
|
|
|
s->predictor = value;
|
2006-10-13 13:48:29 +03:00
|
|
|
break;
|
2013-09-30 07:39:51 +03:00
|
|
|
case TIFF_PHOTOMETRIC:
|
2012-04-13 04:28:37 +03:00
|
|
|
switch (value) {
|
2013-09-30 07:39:51 +03:00
|
|
|
case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
|
|
|
|
case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
|
|
|
|
case TIFF_PHOTOMETRIC_RGB:
|
|
|
|
case TIFF_PHOTOMETRIC_PALETTE:
|
|
|
|
s->photometric = value;
|
2006-11-13 13:20:50 +02:00
|
|
|
break;
|
2013-09-30 07:39:51 +03:00
|
|
|
case TIFF_PHOTOMETRIC_ALPHA_MASK:
|
|
|
|
case TIFF_PHOTOMETRIC_SEPARATED:
|
|
|
|
case TIFF_PHOTOMETRIC_YCBCR:
|
|
|
|
case TIFF_PHOTOMETRIC_CIE_LAB:
|
|
|
|
case TIFF_PHOTOMETRIC_ICC_LAB:
|
|
|
|
case TIFF_PHOTOMETRIC_ITU_LAB:
|
|
|
|
case TIFF_PHOTOMETRIC_CFA:
|
|
|
|
case TIFF_PHOTOMETRIC_LOG_L:
|
|
|
|
case TIFF_PHOTOMETRIC_LOG_LUV:
|
|
|
|
case TIFF_PHOTOMETRIC_LINEAR_RAW:
|
|
|
|
avpriv_report_missing_feature(s->avctx,
|
|
|
|
"PhotometricInterpretation 0x%04X",
|
|
|
|
value);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
2006-11-13 13:20:50 +02:00
|
|
|
default:
|
2013-09-30 07:39:51 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
|
|
|
|
"unknown\n", value);
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-11-13 07:32:10 +02:00
|
|
|
}
|
|
|
|
break;
|
2009-09-24 21:30:28 +03:00
|
|
|
case TIFF_FILL_ORDER:
|
2012-04-13 04:28:37 +03:00
|
|
|
if (value < 1 || value > 2) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Unknown FillOrder value %d, trying default one\n", value);
|
2009-09-24 21:30:28 +03:00
|
|
|
value = 1;
|
|
|
|
}
|
|
|
|
s->fill_order = value - 1;
|
|
|
|
break;
|
2013-09-30 02:47:55 +03:00
|
|
|
case TIFF_PAL: {
|
|
|
|
GetByteContext pal_gb[3];
|
2006-11-13 13:34:46 +02:00
|
|
|
off = type_sizes[type];
|
2013-09-30 02:47:55 +03:00
|
|
|
if (count / 3 > 256 ||
|
|
|
|
bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-09-30 02:47:55 +03:00
|
|
|
pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
|
|
|
|
bytestream2_skip(&pal_gb[1], count / 3 * off);
|
|
|
|
bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
|
2006-11-13 13:34:46 +02:00
|
|
|
off = (type_sizes[type] - 1) << 3;
|
2012-04-13 04:28:37 +03:00
|
|
|
for (i = 0; i < count / 3; i++) {
|
2013-09-28 19:20:19 +03:00
|
|
|
uint32_t p = 0xFF000000;
|
2013-09-30 02:47:55 +03:00
|
|
|
p |= (tget(&pal_gb[0], type, s->le) >> off) << 16;
|
|
|
|
p |= (tget(&pal_gb[1], type, s->le) >> off) << 8;
|
|
|
|
p |= tget(&pal_gb[2], type, s->le) >> off;
|
2013-09-30 08:00:20 +03:00
|
|
|
s->palette[i] = p;
|
2006-11-13 07:32:10 +02:00
|
|
|
}
|
2011-05-24 02:17:25 +03:00
|
|
|
s->palette_is_set = 1;
|
2006-11-13 13:38:42 +02:00
|
|
|
break;
|
2013-09-30 02:47:55 +03:00
|
|
|
}
|
2006-11-13 13:38:42 +02:00
|
|
|
case TIFF_PLANAR:
|
2014-12-17 15:54:44 +02:00
|
|
|
s->planar = value == 2;
|
2006-11-13 13:38:42 +02:00
|
|
|
break;
|
2008-12-26 20:42:26 +02:00
|
|
|
case TIFF_T4OPTIONS:
|
2012-04-13 04:28:37 +03:00
|
|
|
if (s->compr == TIFF_G3)
|
2009-09-28 08:15:27 +03:00
|
|
|
s->fax_opts = value;
|
|
|
|
break;
|
2008-12-26 20:42:26 +02:00
|
|
|
case TIFF_T6OPTIONS:
|
2012-04-13 04:28:37 +03:00
|
|
|
if (s->compr == TIFF_G4)
|
2009-09-28 08:15:27 +03:00
|
|
|
s->fax_opts = value;
|
2008-12-26 20:42:26 +02:00
|
|
|
break;
|
2011-05-09 22:26:39 +03:00
|
|
|
default:
|
2013-06-03 05:00:43 +03:00
|
|
|
if (s->avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Unknown or unsupported tag %d/0X%0X\n",
|
|
|
|
tag, tag);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, start, SEEK_SET);
|
2006-10-13 13:48:29 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_frame(AVCodecContext *avctx,
|
2012-11-13 21:35:22 +03:00
|
|
|
void *data, int *got_frame, AVPacket *avpkt)
|
2006-10-13 13:48:29 +03:00
|
|
|
{
|
2012-04-13 04:28:37 +03:00
|
|
|
TiffContext *const s = avctx->priv_data;
|
2012-11-21 23:34:46 +03:00
|
|
|
AVFrame *const p = data;
|
2011-10-01 02:42:33 +03:00
|
|
|
unsigned off;
|
2014-12-17 15:54:44 +02:00
|
|
|
int id, le, ret, plane, planes;
|
2013-06-01 16:35:11 +03:00
|
|
|
int i, j, entries, stride;
|
2011-10-01 02:42:33 +03:00
|
|
|
unsigned soff, ssize;
|
2008-12-22 08:50:18 +02:00
|
|
|
uint8_t *dst;
|
2013-09-30 02:47:55 +03:00
|
|
|
GetByteContext stripsizes;
|
|
|
|
GetByteContext stripdata;
|
|
|
|
|
|
|
|
bytestream2_init(&s->gb, avpkt->data, avpkt->size);
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2013-06-01 16:35:11 +03:00
|
|
|
// parse image header
|
2013-09-30 02:47:55 +03:00
|
|
|
if (avpkt->size < 8)
|
2011-10-01 02:42:33 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-09-30 02:47:55 +03:00
|
|
|
id = bytestream2_get_le16(&s->gb);
|
2012-04-13 04:28:37 +03:00
|
|
|
if (id == 0x4949)
|
|
|
|
le = 1;
|
|
|
|
else if (id == 0x4D4D)
|
|
|
|
le = 0;
|
|
|
|
else {
|
2006-10-13 13:48:29 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
2013-09-30 07:39:51 +03:00
|
|
|
s->le = le;
|
|
|
|
s->photometric = TIFF_PHOTOMETRIC_NONE;
|
|
|
|
s->compr = TIFF_RAW;
|
|
|
|
s->fill_order = 0;
|
2006-10-13 13:48:29 +03:00
|
|
|
// As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
|
|
|
|
// that further identifies the file as a TIFF file"
|
2013-09-30 02:47:55 +03:00
|
|
|
if (tget_short(&s->gb, le) != 42) {
|
2012-04-13 04:28:37 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"The answer to life, universe and everything is not correct!\n");
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
// Reset these offsets so we can tell if they were set this frame
|
|
|
|
s->stripsizesoff = s->strippos = 0;
|
2006-10-13 13:48:29 +03:00
|
|
|
/* parse image file directory */
|
2013-09-30 02:47:55 +03:00
|
|
|
off = tget_long(&s->gb, le);
|
|
|
|
if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
|
2006-10-13 13:48:29 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
|
2011-10-01 02:42:33 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
bytestream2_seek(&s->gb, off, SEEK_SET);
|
|
|
|
entries = tget_short(&s->gb, le);
|
2012-04-13 04:28:37 +03:00
|
|
|
for (i = 0; i < entries; i++) {
|
2013-09-30 02:47:55 +03:00
|
|
|
if ((ret = tiff_decode_tag(s)) < 0)
|
2012-11-17 15:40:37 +03:00
|
|
|
return ret;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
if (!s->strippos && !s->stripoff) {
|
2008-12-22 08:50:18 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
|
2012-11-17 15:40:37 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-12-22 08:50:18 +02:00
|
|
|
}
|
|
|
|
/* now we have the data and may start decoding */
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = init_image(s, p)) < 0)
|
2011-05-09 13:51:24 +03:00
|
|
|
return ret;
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
if (s->strips == 1 && !s->stripsize) {
|
2008-12-22 08:50:18 +02:00
|
|
|
av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
|
2013-09-30 02:47:55 +03:00
|
|
|
s->stripsize = avpkt->size - s->stripoff;
|
2008-12-22 08:50:18 +02:00
|
|
|
}
|
2013-09-30 02:47:55 +03:00
|
|
|
|
|
|
|
if (s->stripsizesoff) {
|
|
|
|
if (s->stripsizesoff >= avpkt->size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
|
|
|
|
avpkt->size - s->stripsizesoff);
|
|
|
|
}
|
|
|
|
if (s->strippos) {
|
|
|
|
if (s->strippos >= avpkt->size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
bytestream2_init(&stripdata, avpkt->data + s->strippos,
|
|
|
|
avpkt->size - s->strippos);
|
|
|
|
}
|
|
|
|
|
2014-12-17 15:54:44 +02:00
|
|
|
planes = s->planar ? s->bppcount : 1;
|
|
|
|
for (plane = 0; plane < planes; plane++) {
|
|
|
|
stride = p->linesize[plane];
|
|
|
|
dst = p->data[plane];
|
|
|
|
for (i = 0; i < s->height; i += s->rps) {
|
|
|
|
if (s->stripsizesoff)
|
|
|
|
ssize = tget(&stripsizes, s->sstype, le);
|
|
|
|
else
|
|
|
|
ssize = s->stripsize;
|
|
|
|
|
|
|
|
if (s->strippos)
|
|
|
|
soff = tget(&stripdata, s->sot, le);
|
|
|
|
else
|
|
|
|
soff = s->stripoff;
|
|
|
|
|
|
|
|
if (soff > avpkt->size || ssize > avpkt->size - soff) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
2011-09-25 17:00:04 +03:00
|
|
|
}
|
2014-12-17 15:54:44 +02:00
|
|
|
if ((ret = tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
|
|
|
|
FFMIN(s->rps, s->height - i))) < 0) {
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return ret;
|
|
|
|
break;
|
2011-09-25 17:00:04 +03:00
|
|
|
}
|
2014-12-17 15:54:44 +02:00
|
|
|
dst += s->rps * stride;
|
|
|
|
}
|
|
|
|
if (s->predictor == 2) {
|
|
|
|
dst = p->data[plane];
|
|
|
|
soff = s->bpp >> 3;
|
|
|
|
ssize = s->width * soff;
|
|
|
|
if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
|
|
|
|
s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE) {
|
|
|
|
for (i = 0; i < s->height; i++) {
|
|
|
|
for (j = soff; j < ssize; j += 2)
|
|
|
|
AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
} else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
|
|
|
|
s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
|
|
|
|
for (i = 0; i < s->height; i++) {
|
|
|
|
for (j = soff; j < ssize; j += 2)
|
|
|
|
AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < s->height; i++) {
|
|
|
|
for (j = soff; j < ssize; j++)
|
|
|
|
dst[j] += dst[j - soff];
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
|
|
|
|
dst = p->data[plane];
|
2011-09-25 17:00:04 +03:00
|
|
|
for (i = 0; i < s->height; i++) {
|
2014-12-17 15:54:44 +02:00
|
|
|
for (j = 0; j < stride; j++)
|
|
|
|
dst[j] = 255 - dst[j];
|
2011-09-25 17:00:04 +03:00
|
|
|
dst += stride;
|
|
|
|
}
|
2008-12-22 08:50:18 +02:00
|
|
|
}
|
|
|
|
}
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2014-12-17 15:54:44 +02:00
|
|
|
if (s->planar && s->bppcount > 2) {
|
|
|
|
FFSWAP(uint8_t*, p->data[0], p->data[2]);
|
|
|
|
FFSWAP(int, p->linesize[0], p->linesize[2]);
|
|
|
|
FFSWAP(uint8_t*, p->data[0], p->data[1]);
|
|
|
|
FFSWAP(int, p->linesize[0], p->linesize[1]);
|
2006-11-13 07:32:10 +02:00
|
|
|
}
|
2014-12-17 15:54:44 +02:00
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2013-09-30 02:47:55 +03:00
|
|
|
return avpkt->size;
|
2006-10-13 13:48:29 +03:00
|
|
|
}
|
|
|
|
|
2012-04-13 04:28:37 +03:00
|
|
|
static av_cold int tiff_init(AVCodecContext *avctx)
|
|
|
|
{
|
2006-10-13 13:48:29 +03:00
|
|
|
TiffContext *s = avctx->priv_data;
|
|
|
|
|
2013-06-01 16:35:11 +03:00
|
|
|
s->width = 0;
|
2006-10-13 13:48:29 +03:00
|
|
|
s->height = 0;
|
2013-06-01 16:35:11 +03:00
|
|
|
s->avctx = avctx;
|
2006-10-26 07:15:48 +03:00
|
|
|
ff_lzw_decode_open(&s->lzw);
|
2008-12-26 20:42:26 +02:00
|
|
|
ff_ccitt_unpack_init();
|
2006-10-13 13:48:29 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int tiff_end(AVCodecContext *avctx)
|
2006-10-13 13:48:29 +03:00
|
|
|
{
|
2012-04-13 04:28:37 +03:00
|
|
|
TiffContext *const s = avctx->priv_data;
|
2006-10-13 13:48:29 +03:00
|
|
|
|
2006-10-26 07:15:48 +03:00
|
|
|
ff_lzw_decode_close(&s->lzw);
|
2006-10-13 13:48:29 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_tiff_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "tiff",
|
2013-06-01 16:35:11 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
|
2011-07-17 13:54:31 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_TIFF,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(TiffContext),
|
|
|
|
.init = tiff_init,
|
|
|
|
.close = tiff_end,
|
|
|
|
.decode = decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2006-10-13 13:48:29 +03:00
|
|
|
};
|