diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c index af49986c5c..e9b17615c9 100644 --- a/libavcodec/tiff.c +++ b/libavcodec/tiff.c @@ -354,29 +354,6 @@ static int add_metadata(int count, int type, }; } -#if CONFIG_ZLIB -static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, - int size) -{ - z_stream zstream = { 0 }; - int zret; - - zstream.next_in = (uint8_t *)src; - zstream.avail_in = size; - zstream.next_out = dst; - zstream.avail_out = *len; - zret = inflateInit(&zstream); - 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; -} -#endif - 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) @@ -430,6 +407,69 @@ static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size) return 0; } +#if CONFIG_ZLIB +static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, + int size) +{ + z_stream zstream = { 0 }; + int zret; + + zstream.next_in = (uint8_t *)src; + zstream.avail_in = size; + zstream.next_out = dst; + zstream.avail_out = *len; + zret = inflateInit(&zstream); + 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; +} + +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); + if (s->fill_order) { + if ((ret = deinvert_buffer(s, src, size)) < 0) { + av_free(zbuf); + return ret; + } + src = s->deinvert_buf; + } + 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++) { + if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { + horizontal_fill(s->bpp, dst, 1, src, 0, width, 0); + } else { + memcpy(dst, src, width); + } + dst += stride; + src += width; + } + av_free(zbuf); + return 0; +} +#endif + static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride, const uint8_t *src, int size, int lines) { @@ -443,44 +483,16 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride, if (size <= 0) return AVERROR_INVALIDDATA; -#if CONFIG_ZLIB if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) { - uint8_t *zbuf; - unsigned long outlen; - int ret; - outlen = width * lines; - zbuf = av_malloc(outlen); - if (!zbuf) - return AVERROR(ENOMEM); - if (s->fill_order) { - if ((ret = deinvert_buffer(s, src, size)) < 0) { - av_free(zbuf); - return ret; - } - ssrc = src = s->deinvert_buf; - } - 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++) { - if(s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { - horizontal_fill(s->bpp, dst, 1, src, 0, width, 0); - }else{ - memcpy(dst, src, width); - } - dst += stride; - src += width; - } - av_free(zbuf); - return 0; - } +#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); #endif + } if (s->compr == TIFF_LZW) { if (s->fill_order) { if ((ret = deinvert_buffer(s, src, size)) < 0)