avcodec/wcmv: Use ff_inflate_init/end()

This fixes the problem of potentially closing a z_stream
that has never been successfully initialized.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-03-15 11:33:26 +01:00
parent 93c70b8555
commit c78d39b641
2 changed files with 20 additions and 28 deletions
configure
libavcodec

2
configure vendored

@ -2972,7 +2972,7 @@ vp6f_decoder_select="vp6_decoder"
vp7_decoder_select="h264pred videodsp vp8dsp" vp7_decoder_select="h264pred videodsp vp8dsp"
vp8_decoder_select="h264pred videodsp vp8dsp" vp8_decoder_select="h264pred videodsp vp8dsp"
vp9_decoder_select="videodsp vp9_parser vp9_superframe_split_bsf" vp9_decoder_select="videodsp vp9_parser vp9_superframe_split_bsf"
wcmv_decoder_deps="zlib" wcmv_decoder_select="inflate_wrapper"
webp_decoder_select="vp8_decoder exif" webp_decoder_select="vp8_decoder exif"
wmalossless_decoder_select="llauddsp" wmalossless_decoder_select="llauddsp"
wmapro_decoder_select="mdct sinewin wma_freqs" wmapro_decoder_select="mdct sinewin wma_freqs"

@ -29,12 +29,13 @@
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "internal.h" #include "internal.h"
#include "zlib_wrapper.h"
#include <zlib.h> #include <zlib.h>
typedef struct WCMVContext { typedef struct WCMVContext {
int bpp; int bpp;
z_stream zstream; FFZStream zstream;
AVFrame *prev_frame; AVFrame *prev_frame;
uint8_t block_data[65536*8]; uint8_t block_data[65536*8];
} WCMVContext; } WCMVContext;
@ -44,12 +45,13 @@ static int decode_frame(AVCodecContext *avctx,
AVPacket *avpkt) AVPacket *avpkt)
{ {
WCMVContext *s = avctx->priv_data; WCMVContext *s = avctx->priv_data;
z_stream *const zstream = &s->zstream.zstream;
AVFrame *frame = data; AVFrame *frame = data;
int skip, blocks, zret, ret, intra = 0, flags = 0, bpp = s->bpp; int skip, blocks, zret, ret, intra = 0, flags = 0, bpp = s->bpp;
GetByteContext gb; GetByteContext gb;
uint8_t *dst; uint8_t *dst;
ret = inflateReset(&s->zstream); ret = inflateReset(zstream);
if (ret != Z_OK) { if (ret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret); av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
@ -78,19 +80,19 @@ static int decode_frame(AVCodecContext *avctx,
if (size > avpkt->size - skip) if (size > avpkt->size - skip)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
s->zstream.next_in = avpkt->data + skip; zstream->next_in = avpkt->data + skip;
s->zstream.avail_in = size; zstream->avail_in = size;
s->zstream.next_out = s->block_data; zstream->next_out = s->block_data;
s->zstream.avail_out = sizeof(s->block_data); zstream->avail_out = sizeof(s->block_data);
zret = inflate(&s->zstream, Z_FINISH); zret = inflate(zstream, Z_FINISH);
if (zret != Z_STREAM_END) { if (zret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR, av_log(avctx, AV_LOG_ERROR,
"Inflate failed with return code: %d.\n", zret); "Inflate failed with return code: %d.\n", zret);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
ret = inflateReset(&s->zstream); ret = inflateReset(zstream);
if (ret != Z_OK) { if (ret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret); av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
@ -119,8 +121,8 @@ static int decode_frame(AVCodecContext *avctx,
skip = bytestream2_tell(&gb); skip = bytestream2_tell(&gb);
s->zstream.next_in = avpkt->data + skip; zstream->next_in = avpkt->data + skip;
s->zstream.avail_in = avpkt->size - skip; zstream->avail_in = avpkt->size - skip;
bytestream2_init(&gb, s->block_data, blocks * 8); bytestream2_init(&gb, s->block_data, blocks * 8);
} else if (blocks) { } else if (blocks) {
@ -148,8 +150,8 @@ static int decode_frame(AVCodecContext *avctx,
skip = bytestream2_tell(&gb); skip = bytestream2_tell(&gb);
s->zstream.next_in = avpkt->data + skip; zstream->next_in = avpkt->data + skip;
s->zstream.avail_in = avpkt->size - skip; zstream->avail_in = avpkt->size - skip;
bytestream2_seek(&gb, 2, SEEK_SET); bytestream2_seek(&gb, 2, SEEK_SET);
} }
@ -182,10 +184,10 @@ static int decode_frame(AVCodecContext *avctx,
dst = s->prev_frame->data[0] + (avctx->height - y - 1) * s->prev_frame->linesize[0] + x * bpp; dst = s->prev_frame->data[0] + (avctx->height - y - 1) * s->prev_frame->linesize[0] + x * bpp;
for (int i = 0; i < h; i++) { for (int i = 0; i < h; i++) {
s->zstream.next_out = dst; zstream->next_out = dst;
s->zstream.avail_out = w * bpp; zstream->avail_out = w * bpp;
zret = inflate(&s->zstream, Z_SYNC_FLUSH); zret = inflate(zstream, Z_SYNC_FLUSH);
if (zret != Z_OK && zret != Z_STREAM_END) { if (zret != Z_OK && zret != Z_STREAM_END) {
av_log(avctx, AV_LOG_ERROR, av_log(avctx, AV_LOG_ERROR,
"Inflate failed with return code: %d.\n", zret); "Inflate failed with return code: %d.\n", zret);
@ -210,7 +212,6 @@ static int decode_frame(AVCodecContext *avctx,
static av_cold int decode_init(AVCodecContext *avctx) static av_cold int decode_init(AVCodecContext *avctx)
{ {
WCMVContext *s = avctx->priv_data; WCMVContext *s = avctx->priv_data;
int zret;
switch (avctx->bits_per_coded_sample) { switch (avctx->bits_per_coded_sample) {
case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break; case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
@ -223,20 +224,11 @@ static av_cold int decode_init(AVCodecContext *avctx)
s->bpp = avctx->bits_per_coded_sample >> 3; s->bpp = avctx->bits_per_coded_sample >> 3;
s->zstream.zalloc = Z_NULL;
s->zstream.zfree = Z_NULL;
s->zstream.opaque = Z_NULL;
zret = inflateInit(&s->zstream);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
return AVERROR_EXTERNAL;
}
s->prev_frame = av_frame_alloc(); s->prev_frame = av_frame_alloc();
if (!s->prev_frame) if (!s->prev_frame)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
return 0; return ff_inflate_init(&s->zstream, avctx);
} }
static av_cold int decode_close(AVCodecContext *avctx) static av_cold int decode_close(AVCodecContext *avctx)
@ -244,7 +236,7 @@ static av_cold int decode_close(AVCodecContext *avctx)
WCMVContext *s = avctx->priv_data; WCMVContext *s = avctx->priv_data;
av_frame_free(&s->prev_frame); av_frame_free(&s->prev_frame);
inflateEnd(&s->zstream); ff_inflate_end(&s->zstream);
return 0; return 0;
} }