tiffdec: K&R cosmetics

pull/5/head
Alex Converse 13 years ago
parent cbf767a87c
commit f346b6bbeb
  1. 117
      libavcodec/tiff.c

@ -59,19 +59,22 @@ typedef struct TiffContext {
LZWState *lzw; LZWState *lzw;
} TiffContext; } TiffContext;
static unsigned tget_short(const uint8_t **p, int le) { static unsigned tget_short(const uint8_t **p, int le)
{
unsigned v = le ? AV_RL16(*p) : AV_RB16(*p); unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
*p += 2; *p += 2;
return v; return v;
} }
static unsigned tget_long(const uint8_t **p, int le) { static unsigned tget_long(const uint8_t **p, int le)
{
unsigned v = le ? AV_RL32(*p) : AV_RB32(*p); unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
*p += 4; *p += 4;
return v; return v;
} }
static unsigned tget(const uint8_t **p, int type, int le) { static unsigned tget(const uint8_t **p, int type, int le)
{
switch (type) { switch (type) {
case TIFF_BYTE : return *(*p)++; case TIFF_BYTE : return *(*p)++;
case TIFF_SHORT: return tget_short(p, le); case TIFF_SHORT: return tget_short(p, le);
@ -81,7 +84,8 @@ static unsigned tget(const uint8_t **p, int type, int le) {
} }
#if CONFIG_ZLIB #if CONFIG_ZLIB
static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size) static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
int size)
{ {
z_stream zstream = { 0 }; z_stream zstream = { 0 };
int zret; int zret;
@ -102,7 +106,9 @@ static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
} }
#endif #endif
static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
const uint8_t *src, int size, int lines)
{
int c, line, pixels, code; int c, line, pixels, code;
const uint8_t *ssrc = src; const uint8_t *ssrc = src;
int width = ((s->width * s->bpp) + 7) >> 3; int width = ((s->width * s->bpp) + 7) >> 3;
@ -112,7 +118,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
#if CONFIG_ZLIB #if CONFIG_ZLIB
if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) { if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
uint8_t *zbuf; unsigned long outlen; uint8_t *zbuf;
unsigned long outlen;
int ret; int ret;
outlen = width * lines; outlen = width * lines;
zbuf = av_malloc(outlen); zbuf = av_malloc(outlen);
@ -120,7 +127,9 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
ret = tiff_uncompress(zbuf, &outlen, src, size); ret = tiff_uncompress(zbuf, &outlen, src, size);
if (ret != Z_OK) { 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_log(s->avctx, AV_LOG_ERROR,
"Uncompressing failed (%lu of %lu) with error %d\n", outlen,
(unsigned long)width * lines, ret);
av_free(zbuf); av_free(zbuf);
return -1; return -1;
} }
@ -140,16 +149,20 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
return -1; return -1;
} }
} }
if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){ if (s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3
|| s->compr == TIFF_G4) {
int i, ret = 0; int i, ret = 0;
uint8_t *src2 = av_malloc((unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE); uint8_t *src2 = av_malloc((unsigned)size +
FF_INPUT_BUFFER_PADDING_SIZE);
if (!src2) { if (!src2) {
av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); av_log(s->avctx, AV_LOG_ERROR,
"Error allocating temporary buffer\n");
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
if (s->fax_opts & 2) { if (s->fax_opts & 2) {
av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n"); av_log(s->avctx, AV_LOG_ERROR,
"Uncompressed fax mode is not supported (yet)\n");
av_free(src2); av_free(src2);
return -1; return -1;
} }
@ -164,7 +177,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
case TIFF_CCITT_RLE: case TIFF_CCITT_RLE:
case TIFF_G3: case TIFF_G3:
case TIFF_G4: case TIFF_G4:
ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts); ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
s->compr, s->fax_opts);
break; break;
} }
av_free(src2); av_free(src2);
@ -194,7 +208,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
if (code >= 0) { if (code >= 0) {
code++; code++;
if (pixels + code > width) { if (pixels + code > width) {
av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n"); av_log(s->avctx, AV_LOG_ERROR,
"Copy went out of bounds\n");
return -1; return -1;
} }
memcpy(dst + pixels, src, code); memcpy(dst + pixels, src, code);
@ -203,7 +218,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
} else if (code != -128) { // -127..-1 } else if (code != -128) { // -127..-1
code = (-code) + 1; code = (-code) + 1;
if (pixels + code > width) { if (pixels + code > width) {
av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); av_log(s->avctx, AV_LOG_ERROR,
"Run went out of bounds\n");
return -1; return -1;
} }
c = *src++; c = *src++;
@ -215,7 +231,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
case TIFF_LZW: case TIFF_LZW:
pixels = ff_lzw_decode(s->lzw, dst, width); pixels = ff_lzw_decode(s->lzw, dst, width);
if (pixels < width) { if (pixels < width) {
av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width); av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
pixels, width);
return -1; return -1;
} }
break; break;
@ -279,7 +296,8 @@ static int init_image(TiffContext *s)
return 0; return 0;
} }
static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf) static int tiff_decode_tag(TiffContext *s, const uint8_t *start,
const uint8_t *buf, const uint8_t *end_buf)
{ {
unsigned tag, type, count, off, value = 0; unsigned tag, type, count, off, value = 0;
int i, j; int i, j;
@ -294,7 +312,8 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
off = tget_long(&buf, s->le); off = tget_long(&buf, s->le);
if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) { if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n", type); av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
type);
return 0; return 0;
} }
@ -328,7 +347,8 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
} }
if (buf && (buf < start || buf > end_buf)) { if (buf && (buf < start || buf > end_buf)) {
av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n"); av_log(s->avctx, AV_LOG_ERROR,
"Tag referencing position outside the image\n");
return -1; return -1;
} }
@ -342,19 +362,24 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
case TIFF_BPP: case TIFF_BPP:
s->bppcount = count; s->bppcount = count;
if (count > 4) { if (count > 4) {
av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count); av_log(s->avctx, AV_LOG_ERROR,
"This format is not supported (bpp=%d, %d components)\n",
s->bpp, count);
return -1; return -1;
} }
if(count == 1) s->bpp = value; if (count == 1)
s->bpp = value;
else { else {
switch (type) { switch (type) {
case TIFF_BYTE: case TIFF_BYTE:
s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF); s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
break; break;
case TIFF_SHORT: case TIFF_SHORT:
case TIFF_LONG: case TIFF_LONG:
s->bpp = 0; s->bpp = 0;
for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le); for (i = 0; i < count && buf < end_buf; i++)
s->bpp += tget(&buf, type, s->le);
break; break;
default: default:
s->bpp = -1; s->bpp = -1;
@ -394,10 +419,12 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
#endif #endif
case TIFF_JPEG: case TIFF_JPEG:
case TIFF_NEWJPEG: case TIFF_NEWJPEG:
av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n"); av_log(s->avctx, AV_LOG_ERROR,
"JPEG compression is not supported\n");
return -1; return -1;
default: default:
av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr); av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
s->compr);
return -1; return -1;
} }
break; break;
@ -405,7 +432,8 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
if (type == TIFF_LONG && value == UINT_MAX) if (type == TIFF_LONG && value == UINT_MAX)
value = s->avctx->height; value = s->avctx->height;
if (value < 1) { if (value < 1) {
av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n"); av_log(s->avctx, AV_LOG_ERROR,
"Incorrect value of rows per strip\n");
return -1; return -1;
} }
s->rps = value; s->rps = value;
@ -417,10 +445,12 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
} else } else
s->stripdata = start + off; s->stripdata = start + off;
s->strips = count; s->strips = count;
if(s->strips == 1) s->rps = s->height; if (s->strips == 1)
s->rps = s->height;
s->sot = type; s->sot = type;
if (s->stripdata > end_buf) { if (s->stripdata > end_buf) {
av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n"); av_log(s->avctx, AV_LOG_ERROR,
"Tag referencing position outside the image\n");
return -1; return -1;
} }
break; break;
@ -435,7 +465,8 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
s->strips = count; s->strips = count;
s->sstype = type; s->sstype = type;
if (s->stripsizes > end_buf) { if (s->stripsizes > end_buf) {
av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n"); av_log(s->avctx, AV_LOG_ERROR,
"Tag referencing position outside the image\n");
return -1; return -1;
} }
break; break;
@ -454,13 +485,15 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
case 3: case 3:
break; break;
default: default:
av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value); av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n",
value);
return -1; return -1;
} }
break; break;
case TIFF_FILL_ORDER: case TIFF_FILL_ORDER:
if (value < 1 || value > 2) { if (value < 1 || value > 2) {
av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value); av_log(s->avctx, AV_LOG_ERROR,
"Unknown FillOrder value %d, trying default one\n", value);
value = 1; value = 1;
} }
s->fill_order = value - 1; s->fill_order = value - 1;
@ -497,14 +530,14 @@ static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *
s->fax_opts = value; s->fax_opts = value;
break; break;
default: default:
av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag); av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n",
tag, tag);
} }
return 0; return 0;
} }
static int decode_frame(AVCodecContext *avctx, static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size, void *data, int *data_size, AVPacket *avpkt)
AVPacket *avpkt)
{ {
const uint8_t *buf = avpkt->data; const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; int buf_size = avpkt->size;
@ -522,9 +555,12 @@ static int decode_frame(AVCodecContext *avctx,
//parse image header //parse image header
if (end_buf - buf < 8) if (end_buf - buf < 8)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
id = AV_RL16(buf); buf += 2; id = AV_RL16(buf);
if(id == 0x4949) le = 1; buf += 2;
else if(id == 0x4D4D) le = 0; if (id == 0x4949)
le = 1;
else if (id == 0x4D4D)
le = 0;
else { else {
av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n"); av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
return -1; return -1;
@ -536,7 +572,8 @@ static int decode_frame(AVCodecContext *avctx,
// As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
// that further identifies the file as a TIFF file" // that further identifies the file as a TIFF file"
if (tget_short(&buf, le) != 42) { if (tget_short(&buf, le) != 42) {
av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n"); av_log(avctx, AV_LOG_ERROR,
"The answer to life, universe and everything is not correct!\n");
return -1; return -1;
} }
// Reset these pointers so we can tell if they were set this frame // Reset these pointers so we can tell if they were set this frame
@ -587,7 +624,8 @@ static int decode_frame(AVCodecContext *avctx,
av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n"); av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
return -1; return -1;
} }
if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0) if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,
FFMIN(s->rps, s->height - i)) < 0)
break; break;
dst += s->rps * stride; dst += s->rps * stride;
} }
@ -619,7 +657,8 @@ static int decode_frame(AVCodecContext *avctx,
return buf_size; return buf_size;
} }
static av_cold int tiff_init(AVCodecContext *avctx){ static av_cold int tiff_init(AVCodecContext *avctx)
{
TiffContext *s = avctx->priv_data; TiffContext *s = avctx->priv_data;
s->width = 0; s->width = 0;

Loading…
Cancel
Save