X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fzmbv.c;h=79e0892070412b4a54d6748032dfe8d1dd651191;hb=5ccf296e74725bc8bdfbfe500d0482daa200b6f3;hp=f91d2e393179d4a025ba1fef28ed5c73955aff35;hpb=ce47f1589e9f5a6cf8372a269bdd862ff0cc3f91;p=ffmpeg diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c index f91d2e39317..79e08920704 100644 --- a/libavcodec/zmbv.c +++ b/libavcodec/zmbv.c @@ -57,6 +57,7 @@ typedef struct ZmbvContext { AVCodecContext *avctx; int bpp; + int alloc_bpp; unsigned int decomp_size; uint8_t* decomp_buf; uint8_t pal[768]; @@ -408,6 +409,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac int zret = Z_OK; // Zlib return code int len = buf_size; int hi_ver, lo_ver, ret; + int expected_size; /* parse header */ if (len < 1) @@ -494,16 +496,29 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac return AVERROR_UNKNOWN; } - c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8)); - c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8)); + if (c->alloc_bpp < c->bpp) { + c->cur = av_realloc_f(c->cur, avctx->width * avctx->height, (c->bpp / 8)); + c->prev = av_realloc_f(c->prev, avctx->width * avctx->height, (c->bpp / 8)); + c->alloc_bpp = c->bpp; + } c->bx = (c->width + c->bw - 1) / c->bw; c->by = (c->height+ c->bh - 1) / c->bh; - if (!c->cur || !c->prev) + if (!c->cur || !c->prev) { + c->alloc_bpp = 0; return AVERROR(ENOMEM); + } memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8)); memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8)); c->decode_intra= decode_intra; } + if (c->flags & ZMBV_KEYFRAME) { + expected_size = avctx->width * avctx->height * (c->bpp / 8); + } else { + expected_size = (c->bx * c->by * 2 + 3) & ~3; + } + if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && + (c->flags & (ZMBV_DELTAPAL | ZMBV_KEYFRAME))) + expected_size += 768; if (!c->decode_intra) { av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n"); @@ -519,6 +534,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac return AVERROR_INVALIDDATA; } memcpy(c->decomp_buf, buf, len); + c->decomp_len = len; } else { // ZLIB-compressed data c->zstream.total_in = c->zstream.total_out = 0; c->zstream.next_in = (uint8_t*)buf; @@ -532,6 +548,11 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac } c->decomp_len = c->zstream.total_out; } + if (expected_size > c->decomp_len || + (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) { + av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, expected %d\n", c->decomp_len, expected_size); + return AVERROR_INVALIDDATA; + } if (c->flags & ZMBV_KEYFRAME) { frame->key_frame = 1; frame->pict_type = AV_PICTURE_TYPE_I; @@ -599,12 +620,11 @@ static av_cold int decode_init(AVCodecContext *avctx) c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64); /* Allocate decompression buffer */ - if (c->decomp_size) { - if (!(c->decomp_buf = av_mallocz(c->decomp_size))) { - av_log(avctx, AV_LOG_ERROR, - "Can't allocate decompression buffer.\n"); - return AVERROR(ENOMEM); - } + c->decomp_buf = av_mallocz(c->decomp_size); + if (!c->decomp_buf) { + av_log(avctx, AV_LOG_ERROR, + "Can't allocate decompression buffer.\n"); + return AVERROR(ENOMEM); } c->zstream.zalloc = Z_NULL; @@ -642,4 +662,5 @@ AVCodec ff_zmbv_decoder = { .close = decode_end, .decode = decode_frame, .capabilities = AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, };