From 6886a85f5ca9402c0b589a9af7d53ce53f8a8000 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 1/4] zerocodec: use the AVFrame API properly. --- libavcodec/zerocodec.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/libavcodec/zerocodec.c b/libavcodec/zerocodec.c index 135a809d18..eeba2dedf7 100644 --- a/libavcodec/zerocodec.c +++ b/libavcodec/zerocodec.c @@ -23,7 +23,7 @@ #include "libavutil/common.h" typedef struct { - AVFrame previous_frame; + AVFrame *previous_frame; z_stream zstream; } ZeroCodecContext; @@ -32,7 +32,7 @@ static int zerocodec_decode_frame(AVCodecContext *avctx, void *data, { ZeroCodecContext *zc = avctx->priv_data; AVFrame *pic = data; - AVFrame *prev_pic = &zc->previous_frame; + AVFrame *prev_pic = zc->previous_frame; z_stream *zstream = &zc->zstream; uint8_t *prev = prev_pic->data[0]; uint8_t *dst; @@ -93,8 +93,8 @@ static int zerocodec_decode_frame(AVCodecContext *avctx, void *data, dst -= pic->linesize[0]; } - av_frame_unref(&zc->previous_frame); - if ((ret = av_frame_ref(&zc->previous_frame, pic)) < 0) + av_frame_unref(zc->previous_frame); + if ((ret = av_frame_ref(zc->previous_frame, pic)) < 0) return ret; *got_frame = 1; @@ -106,7 +106,7 @@ static av_cold int zerocodec_decode_close(AVCodecContext *avctx) { ZeroCodecContext *zc = avctx->priv_data; - av_frame_unref(&zc->previous_frame); + av_frame_free(&zc->previous_frame); inflateEnd(&zc->zstream); @@ -132,6 +132,12 @@ static av_cold int zerocodec_decode_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } + zc->previous_frame = av_frame_alloc(); + if (!zc->previous_frame) { + zerocodec_decode_close(avctx); + return AVERROR(ENOMEM); + } + return 0; } From f3cd23fbc849db4da8d5174f8c11a267790cb8d2 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 2/4] xxan: use the AVFrame API properly. --- libavcodec/xxan.c | 72 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/libavcodec/xxan.c b/libavcodec/xxan.c index 74463051e6..d77a50fd3f 100644 --- a/libavcodec/xxan.c +++ b/libavcodec/xxan.c @@ -30,7 +30,7 @@ typedef struct XanContext { AVCodecContext *avctx; - AVFrame pic; + AVFrame *pic; uint8_t *y_buffer; uint8_t *scratch_buffer; @@ -38,6 +38,18 @@ typedef struct XanContext { GetByteContext gb; } XanContext; +static av_cold int xan_decode_end(AVCodecContext *avctx) +{ + XanContext *s = avctx->priv_data; + + av_frame_free(&s->pic); + + av_freep(&s->y_buffer); + av_freep(&s->scratch_buffer); + + return 0; +} + static av_cold int xan_decode_init(AVCodecContext *avctx) { XanContext *s = avctx->priv_data; @@ -65,6 +77,12 @@ static av_cold int xan_decode_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } + s->pic = av_frame_alloc(); + if (!s->pic) { + xan_decode_end(avctx); + return AVERROR(ENOMEM); + } + return 0; } @@ -199,8 +217,8 @@ static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off) return dec_size; } - U = s->pic.data[1]; - V = s->pic.data[2]; + U = s->pic->data[1]; + V = s->pic->data[2]; src = s->scratch_buffer; src_end = src + dec_size; if (mode) { @@ -217,16 +235,16 @@ static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off) if (src == src_end) return 0; } - U += s->pic.linesize[1]; - V += s->pic.linesize[2]; + U += s->pic->linesize[1]; + V += s->pic->linesize[2]; } if (avctx->height & 1) { - memcpy(U, U - s->pic.linesize[1], avctx->width >> 1); - memcpy(V, V - s->pic.linesize[2], avctx->width >> 1); + memcpy(U, U - s->pic->linesize[1], avctx->width >> 1); + memcpy(V, V - s->pic->linesize[2], avctx->width >> 1); } } else { - uint8_t *U2 = U + s->pic.linesize[1]; - uint8_t *V2 = V + s->pic.linesize[2]; + uint8_t *U2 = U + s->pic->linesize[1]; + uint8_t *V2 = V + s->pic->linesize[2]; for (j = 0; j < avctx->height >> 2; j++) { for (i = 0; i < avctx->width >> 1; i += 2) { @@ -239,16 +257,16 @@ static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off) V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5); } } - U += s->pic.linesize[1] * 2; - V += s->pic.linesize[2] * 2; - U2 += s->pic.linesize[1] * 2; - V2 += s->pic.linesize[2] * 2; + U += s->pic->linesize[1] * 2; + V += s->pic->linesize[2] * 2; + U2 += s->pic->linesize[1] * 2; + V2 += s->pic->linesize[2] * 2; } if (avctx->height & 3) { int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2; - memcpy(U, U - lines * s->pic.linesize[1], lines * s->pic.linesize[1]); - memcpy(V, V - lines * s->pic.linesize[2], lines * s->pic.linesize[2]); + memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]); + memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]); } } @@ -320,12 +338,12 @@ static int xan_decode_frame_type0(AVCodecContext *avctx) } src = s->y_buffer; - ybuf = s->pic.data[0]; + ybuf = s->pic->data[0]; for (j = 0; j < avctx->height; j++) { for (i = 0; i < avctx->width; i++) ybuf[i] = (src[i] << 2) | (src[i] >> 3); src += avctx->width; - ybuf += s->pic.linesize[0]; + ybuf += s->pic->linesize[0]; } return 0; @@ -365,12 +383,12 @@ static int xan_decode_frame_type1(AVCodecContext *avctx) } src = s->y_buffer; - ybuf = s->pic.data[0]; + ybuf = s->pic->data[0]; for (j = 0; j < avctx->height; j++) { for (i = 0; i < avctx->width; i++) ybuf[i] = (src[i] << 2) | (src[i] >> 3); src += avctx->width; - ybuf += s->pic.linesize[0]; + ybuf += s->pic->linesize[0]; } return 0; @@ -384,7 +402,7 @@ static int xan_decode_frame(AVCodecContext *avctx, int ftype; int ret; - if ((ret = ff_reget_buffer(avctx, &s->pic))) { + if ((ret = ff_reget_buffer(avctx, s->pic))) { av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return ret; } @@ -405,7 +423,7 @@ static int xan_decode_frame(AVCodecContext *avctx, if (ret) return ret; - if ((ret = av_frame_ref(data, &s->pic)) < 0) + if ((ret = av_frame_ref(data, s->pic)) < 0) return ret; *got_frame = 1; @@ -413,18 +431,6 @@ static int xan_decode_frame(AVCodecContext *avctx, return avpkt->size; } -static av_cold int xan_decode_end(AVCodecContext *avctx) -{ - XanContext *s = avctx->priv_data; - - av_frame_unref(&s->pic); - - av_freep(&s->y_buffer); - av_freep(&s->scratch_buffer); - - return 0; -} - AVCodec ff_xan_wc4_decoder = { .name = "xan_wc4", .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"), From 04f30711d8d28f35f13318a011c9015b3323445d Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 3/4] xan: use the AVFrame API properly. --- libavcodec/xan.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/libavcodec/xan.c b/libavcodec/xan.c index ed2563cc62..4bf1d87f9d 100644 --- a/libavcodec/xan.c +++ b/libavcodec/xan.c @@ -52,7 +52,7 @@ typedef struct XanContext { AVCodecContext *avctx; - AVFrame last_frame; + AVFrame *last_frame; const unsigned char *buf; int size; @@ -71,6 +71,19 @@ typedef struct XanContext { } XanContext; +static av_cold int xan_decode_end(AVCodecContext *avctx) +{ + XanContext *s = avctx->priv_data; + + av_frame_free(&s->last_frame); + + av_freep(&s->buffer1); + av_freep(&s->buffer2); + av_freep(&s->palettes); + + return 0; +} + static av_cold int xan_decode_init(AVCodecContext *avctx) { XanContext *s = avctx->priv_data; @@ -91,6 +104,12 @@ static av_cold int xan_decode_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } + s->last_frame = av_frame_alloc(); + if (!s->last_frame) { + xan_decode_end(avctx); + return AVERROR(ENOMEM); + } + return 0; } @@ -234,7 +253,7 @@ static inline void xan_wc3_copy_pixel_run(XanContext *s, AVFrame *frame, return; palette_plane = frame->data[0]; - prev_palette_plane = s->last_frame.data[0]; + prev_palette_plane = s->last_frame->data[0]; if (!prev_palette_plane) prev_palette_plane = palette_plane; stride = frame->linesize[0]; @@ -582,8 +601,8 @@ static int xan_decode_frame(AVCodecContext *avctx, if (xan_wc3_decode_frame(s, frame) < 0) return AVERROR_INVALIDDATA; - av_frame_unref(&s->last_frame); - if ((ret = av_frame_ref(&s->last_frame, frame)) < 0) + av_frame_unref(s->last_frame); + if ((ret = av_frame_ref(s->last_frame, frame)) < 0) return ret; *got_frame = 1; @@ -592,19 +611,6 @@ static int xan_decode_frame(AVCodecContext *avctx, return buf_size; } -static av_cold int xan_decode_end(AVCodecContext *avctx) -{ - XanContext *s = avctx->priv_data; - - av_frame_unref(&s->last_frame); - - av_freep(&s->buffer1); - av_freep(&s->buffer2); - av_freep(&s->palettes); - - return 0; -} - AVCodec ff_xan_wc3_decoder = { .name = "xan_wc3", .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"), From 3c8ea9d4a74fd4d7493d40c818ca64ee492709f3 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 4/4] vmnc: use the AVFrame API properly. --- libavcodec/vmnc.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/libavcodec/vmnc.c b/libavcodec/vmnc.c index 1073cb202f..16984fbf0f 100644 --- a/libavcodec/vmnc.c +++ b/libavcodec/vmnc.c @@ -57,7 +57,7 @@ enum HexTile_Flags { */ typedef struct VmncContext { AVCodecContext *avctx; - AVFrame pic; + AVFrame *pic; int bpp; int bpp2; @@ -319,15 +319,15 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, uint8_t *outptr; int dx, dy, w, h, depth, enc, chunks, res, size_left, ret; - if ((ret = ff_reget_buffer(avctx, &c->pic)) < 0) { + if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) { av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return ret; } bytestream2_init(gb, buf, buf_size); - c->pic.key_frame = 0; - c->pic.pict_type = AV_PICTURE_TYPE_P; + c->pic->key_frame = 0; + c->pic->pict_type = AV_PICTURE_TYPE_P; // restore screen after cursor if (c->screendta) { @@ -349,11 +349,11 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, dy = 0; } if ((w > 0) && (h > 0)) { - outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0]; + outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0]; for (i = 0; i < h; i++) { memcpy(outptr, c->screendta + i * c->cur_w * c->bpp2, w * c->bpp2); - outptr += c->pic.linesize[0]; + outptr += c->pic->linesize[0]; } } } @@ -365,7 +365,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, w = bytestream2_get_be16(gb); h = bytestream2_get_be16(gb); enc = bytestream2_get_be32(gb); - outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0]; + outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0]; size_left = bytestream2_get_bytes_left(gb); switch (enc) { case MAGIC_WMVd: // cursor @@ -415,8 +415,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, bytestream2_skip(gb, 4); break; case MAGIC_WMVi: // ServerInitialization struct - c->pic.key_frame = 1; - c->pic.pict_type = AV_PICTURE_TYPE_I; + c->pic->key_frame = 1; + c->pic->pict_type = AV_PICTURE_TYPE_I; depth = bytestream2_get_byte(gb); if (depth != c->bpp) { av_log(avctx, AV_LOG_INFO, @@ -451,7 +451,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, return AVERROR_INVALIDDATA; } paint_raw(outptr, w, h, gb, c->bpp2, c->bigendian, - c->pic.linesize[0]); + c->pic->linesize[0]); break; case 0x00000005: // HexTile encoded rectangle if ((dx + w > c->width) || (dy + h > c->height)) { @@ -460,7 +460,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, w, h, dx, dy, c->width, c->height); return AVERROR_INVALIDDATA; } - res = decode_hextile(c, outptr, gb, w, h, c->pic.linesize[0]); + res = decode_hextile(c, outptr, gb, w, h, c->pic->linesize[0]); if (res < 0) return res; break; @@ -489,18 +489,18 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, dy = 0; } if ((w > 0) && (h > 0)) { - outptr = c->pic.data[0] + dx * c->bpp2 + dy * c->pic.linesize[0]; + outptr = c->pic->data[0] + dx * c->bpp2 + dy * c->pic->linesize[0]; for (i = 0; i < h; i++) { memcpy(c->screendta + i * c->cur_w * c->bpp2, outptr, w * c->bpp2); - outptr += c->pic.linesize[0]; + outptr += c->pic->linesize[0]; } - outptr = c->pic.data[0]; - put_cursor(outptr, c->pic.linesize[0], c, c->cur_x, c->cur_y); + outptr = c->pic->data[0]; + put_cursor(outptr, c->pic->linesize[0], c, c->cur_x, c->cur_y); } } *got_frame = 1; - if ((ret = av_frame_ref(data, &c->pic)) < 0) + if ((ret = av_frame_ref(data, c->pic)) < 0) return ret; /* always report that the buffer was completely consumed */ @@ -532,7 +532,9 @@ static av_cold int decode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } - avcodec_get_frame_defaults(&c->pic); + c->pic = av_frame_alloc(); + if (!c->pic) + return AVERROR(ENOMEM); return 0; } @@ -541,7 +543,7 @@ static av_cold int decode_end(AVCodecContext *avctx) { VmncContext * const c = avctx->priv_data; - av_frame_unref(&c->pic); + av_frame_free(&c->pic); av_free(c->curbits); av_free(c->curmask);