From 5c96f02901d8a701a47024970f353a239f49ff5c Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 1/4] mmvideo: use the AVFrame API properly. --- libavcodec/mmvideo.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/libavcodec/mmvideo.c b/libavcodec/mmvideo.c index edf602bfba..abec2e8150 100644 --- a/libavcodec/mmvideo.c +++ b/libavcodec/mmvideo.c @@ -48,7 +48,7 @@ typedef struct MmContext { AVCodecContext *avctx; - AVFrame frame; + AVFrame *frame; int palette[AVPALETTE_COUNT]; GetByteContext gb; } MmContext; @@ -61,7 +61,9 @@ static av_cold int mm_decode_init(AVCodecContext *avctx) avctx->pix_fmt = AV_PIX_FMT_PAL8; - avcodec_get_frame_defaults(&s->frame); + s->frame = av_frame_alloc(); + if (!s->frame) + return AVERROR(ENOMEM); return 0; } @@ -105,9 +107,9 @@ static int mm_decode_intra(MmContext * s, int half_horiz, int half_vert) run_length *=2; if (color) { - memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length); + memset(s->frame->data[0] + y*s->frame->linesize[0] + x, color, run_length); if (half_vert) - memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length); + memset(s->frame->data[0] + (y+1)*s->frame->linesize[0] + x, color, run_length); } x+= run_length; @@ -154,13 +156,13 @@ static int mm_decode_inter(MmContext * s, int half_horiz, int half_vert) int replace = (replace_array >> (7-j)) & 1; if (replace) { int color = bytestream2_get_byte(&data_ptr); - s->frame.data[0][y*s->frame.linesize[0] + x] = color; + s->frame->data[0][y*s->frame->linesize[0] + x] = color; if (half_horiz) - s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color; + s->frame->data[0][y*s->frame->linesize[0] + x + 1] = color; if (half_vert) { - s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color; + s->frame->data[0][(y+1)*s->frame->linesize[0] + x] = color; if (half_horiz) - s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color; + s->frame->data[0][(y+1)*s->frame->linesize[0] + x + 1] = color; } } x += 1 + half_horiz; @@ -189,7 +191,7 @@ static int mm_decode_frame(AVCodecContext *avctx, buf_size -= MM_PREAMBLE_SIZE; bytestream2_init(&s->gb, buf, buf_size); - if ((res = ff_reget_buffer(avctx, &s->frame)) < 0) { + if ((res = ff_reget_buffer(avctx, s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return res; } @@ -209,9 +211,9 @@ static int mm_decode_frame(AVCodecContext *avctx, if (res < 0) return res; - memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE); + memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE); - if ((res = av_frame_ref(data, &s->frame)) < 0) + if ((res = av_frame_ref(data, s->frame)) < 0) return res; *got_frame = 1; @@ -223,7 +225,7 @@ static av_cold int mm_decode_end(AVCodecContext *avctx) { MmContext *s = avctx->priv_data; - av_frame_unref(&s->frame); + av_frame_free(&s->frame); return 0; } From 84099f51f397fb1105c543193785c959f9d3901d Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 2/4] smacker: use the AVFrame API properly. --- libavcodec/smacker.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c index ffd816f721..ba693034e5 100644 --- a/libavcodec/smacker.c +++ b/libavcodec/smacker.c @@ -48,7 +48,7 @@ */ typedef struct SmackVContext { AVCodecContext *avctx; - AVFrame pic; + AVFrame *pic; int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl; int mmap_last[3], mclr_last[3], full_last[3], type_last[3]; @@ -391,21 +391,21 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, if (avpkt->size <= 769) return 0; - if ((ret = ff_reget_buffer(avctx, &smk->pic)) < 0) { + if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } /* make the palette available on the way out */ - pal = (uint32_t*)smk->pic.data[1]; + pal = (uint32_t*)smk->pic->data[1]; bytestream2_init(&gb2, avpkt->data, avpkt->size); flags = bytestream2_get_byteu(&gb2); - smk->pic.palette_has_changed = flags & 1; - smk->pic.key_frame = !!(flags & 2); - if(smk->pic.key_frame) - smk->pic.pict_type = AV_PICTURE_TYPE_I; + smk->pic->palette_has_changed = flags & 1; + smk->pic->key_frame = !!(flags & 2); + if(smk->pic->key_frame) + smk->pic->pict_type = AV_PICTURE_TYPE_I; else - smk->pic.pict_type = AV_PICTURE_TYPE_P; + smk->pic->pict_type = AV_PICTURE_TYPE_P; for(i = 0; i < 256; i++) *pal++ = bytestream2_get_be24u(&gb2); @@ -420,8 +420,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, bw = avctx->width >> 2; bh = avctx->height >> 2; blocks = bw * bh; - out = smk->pic.data[0]; - stride = smk->pic.linesize[0]; + out = smk->pic->data[0]; + stride = smk->pic->linesize[0]; while(blk < blocks) { int type, run, mode; uint16_t pix; @@ -435,7 +435,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, int hi, lo; clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last); map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last); - out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; + out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; hi = clr >> 8; lo = clr & 0xFF; for(i = 0; i < 4; i++) { @@ -456,7 +456,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, else if(get_bits1(&gb)) mode = 2; } while(run-- && blk < blocks){ - out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; + out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; switch(mode){ case 0: for(i = 0; i < 4; i++) { @@ -508,7 +508,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, mode = type >> 8; while(run-- && blk < blocks){ uint32_t col; - out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; + out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; col = mode * 0x01010101; for(i = 0; i < 4; i++) { *((uint32_t*)out) = col; @@ -521,7 +521,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, } - if ((ret = av_frame_ref(data, &smk->pic)) < 0) + if ((ret = av_frame_ref(data, smk->pic)) < 0) return ret; *got_frame = 1; @@ -546,7 +546,7 @@ static av_cold int decode_end(AVCodecContext *avctx) av_freep(&smk->full_tbl); av_freep(&smk->type_tbl); - av_frame_unref(&smk->pic); + av_frame_free(&smk->pic); return 0; } @@ -564,7 +564,10 @@ static av_cold int decode_init(AVCodecContext *avctx) c->avctx = avctx; avctx->pix_fmt = AV_PIX_FMT_PAL8; - avcodec_get_frame_defaults(&c->pic); + + c->pic = av_frame_alloc(); + if (!c->pic) + return AVERROR(ENOMEM); /* decode huffman trees from extradata */ if(avctx->extradata_size < 16){ From d100f9e7cd6d3508359c76bdcffcab4dff4d6bc6 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 3/4] flicvideo: use the AVFrame API properly. --- libavcodec/flicvideo.c | 61 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/libavcodec/flicvideo.c b/libavcodec/flicvideo.c index ff1d8201ac..68f45b4c65 100644 --- a/libavcodec/flicvideo.c +++ b/libavcodec/flicvideo.c @@ -71,7 +71,7 @@ typedef struct FlicDecodeContext { AVCodecContext *avctx; - AVFrame frame; + AVFrame *frame; unsigned int palette[256]; int new_palette; @@ -123,7 +123,10 @@ static av_cold int flic_decode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } - avcodec_get_frame_defaults(&s->frame); + s->frame = av_frame_alloc(); + if (!s->frame) + return AVERROR(ENOMEM); + s->new_palette = 0; return 0; @@ -168,13 +171,13 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, bytestream2_init(&g2, buf, buf_size); - if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0) { + if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return ret; } - pixels = s->frame.data[0]; - pixel_limit = s->avctx->height * s->frame.linesize[0]; + pixels = s->frame->data[0]; + pixel_limit = s->avctx->height * s->frame->linesize[0]; frame_size = bytestream2_get_le32(&g2); bytestream2_skip(&g2, 2); /* skip the magic number */ num_chunks = bytestream2_get_le16(&g2); @@ -248,12 +251,12 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, if ((line_packets & 0xC000) == 0xC000) { // line skip opcode line_packets = -line_packets; - y_ptr += line_packets * s->frame.linesize[0]; + y_ptr += line_packets * s->frame->linesize[0]; } else if ((line_packets & 0xC000) == 0x4000) { av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets); } else if ((line_packets & 0xC000) == 0x8000) { // "last byte" opcode - pixel_ptr= y_ptr + s->frame.linesize[0] - 1; + pixel_ptr= y_ptr + s->frame->linesize[0] - 1; CHECK_PIXEL_PTR(0); pixels[pixel_ptr] = line_packets & 0xff; } else { @@ -284,7 +287,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, } } - y_ptr += s->frame.linesize[0]; + y_ptr += s->frame->linesize[0]; } } break; @@ -293,7 +296,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, /* line compressed */ starting_line = bytestream2_get_le16(&g2); y_ptr = 0; - y_ptr += starting_line * s->frame.linesize[0]; + y_ptr += starting_line * s->frame->linesize[0]; compressed_lines = bytestream2_get_le16(&g2); while (compressed_lines > 0) { @@ -324,7 +327,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, } } - y_ptr += s->frame.linesize[0]; + y_ptr += s->frame->linesize[0]; compressed_lines--; } break; @@ -332,7 +335,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, case FLI_BLACK: /* set the whole frame to color 0 (which is usually black) */ memset(pixels, 0, - s->frame.linesize[0] * s->avctx->height); + s->frame->linesize[0] * s->avctx->height); break; case FLI_BRUN: @@ -375,7 +378,7 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, } } - y_ptr += s->frame.linesize[0]; + y_ptr += s->frame->linesize[0]; } break; @@ -386,8 +389,8 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, "bigger than image, skipping chunk\n", chunk_size - 6); bytestream2_skip(&g2, chunk_size - 6); } else { - for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height; - y_ptr += s->frame.linesize[0]) { + for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height; + y_ptr += s->frame->linesize[0]) { bytestream2_get_buffer(&g2, &pixels[y_ptr], s->avctx->width); } @@ -417,13 +420,13 @@ static int flic_decode_frame_8BPP(AVCodecContext *avctx, buf_size - bytestream2_get_bytes_left(&g2)); /* make the palette available on the way out */ - memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE); + memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE); if (s->new_palette) { - s->frame.palette_has_changed = 1; + s->frame->palette_has_changed = 1; s->new_palette = 0; } - if ((ret = av_frame_ref(data, &s->frame)) < 0) + if ((ret = av_frame_ref(data, s->frame)) < 0) return ret; *got_frame = 1; @@ -464,13 +467,13 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, bytestream2_init(&g2, buf, buf_size); - if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0) { + if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return ret; } - pixels = s->frame.data[0]; - pixel_limit = s->avctx->height * s->frame.linesize[0]; + pixels = s->frame->data[0]; + pixel_limit = s->avctx->height * s->frame->linesize[0]; frame_size = bytestream2_get_le32(&g2); bytestream2_skip(&g2, 2); /* skip the magic number */ @@ -504,7 +507,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, line_packets = bytestream2_get_le16(&g2); if (line_packets < 0) { line_packets = -line_packets; - y_ptr += line_packets * s->frame.linesize[0]; + y_ptr += line_packets * s->frame->linesize[0]; } else { compressed_lines--; pixel_ptr = y_ptr; @@ -533,7 +536,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, } } - y_ptr += s->frame.linesize[0]; + y_ptr += s->frame->linesize[0]; } } break; @@ -546,7 +549,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, case FLI_BLACK: /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */ memset(pixels, 0x0000, - s->frame.linesize[0] * s->avctx->height); + s->frame->linesize[0] * s->avctx->height); break; case FLI_BRUN: @@ -597,7 +600,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, pixel_ptr += 2; } #endif - y_ptr += s->frame.linesize[0]; + y_ptr += s->frame->linesize[0]; } break; @@ -637,7 +640,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, } } - y_ptr += s->frame.linesize[0]; + y_ptr += s->frame->linesize[0]; } break; @@ -650,8 +653,8 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, bytestream2_skip(&g2, chunk_size - 6); } else { - for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height; - y_ptr += s->frame.linesize[0]) { + for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height; + y_ptr += s->frame->linesize[0]) { pixel_countdown = s->avctx->width; pixel_ptr = 0; @@ -684,7 +687,7 @@ static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \ "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2)); - if ((ret = av_frame_ref(data, &s->frame)) < 0) + if ((ret = av_frame_ref(data, s->frame)) < 0) return ret; *got_frame = 1; @@ -733,7 +736,7 @@ static av_cold int flic_decode_end(AVCodecContext *avctx) { FlicDecodeContext *s = avctx->priv_data; - av_frame_unref(&s->frame); + av_frame_free(&s->frame); return 0; } From a837c4f2df96a30bf9aa4115b426d608487c7101 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 4/4] zmbvenc: use the AVFrame API properly. --- libavcodec/zmbvenc.c | 46 +++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/libavcodec/zmbvenc.c b/libavcodec/zmbvenc.c index c039383df5..785ee0a409 100644 --- a/libavcodec/zmbvenc.c +++ b/libavcodec/zmbvenc.c @@ -44,7 +44,6 @@ */ typedef struct ZmbvEncContext { AVCodecContext *avctx; - AVFrame pic; int range; uint8_t *comp_buf, *work_buf; @@ -121,7 +120,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet) { ZmbvEncContext * const c = avctx->priv_data; - AVFrame * const p = &c->pic; + const AVFrame * const p = pict; uint8_t *src, *prev, *buf; uint32_t *palptr; int keyframe, chpal; @@ -134,9 +133,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, c->curfrm++; if(c->curfrm == c->keyint) c->curfrm = 0; - *p = *pict; - p->pict_type= keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; - p->key_frame= keyframe; + avctx->coded_frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; + avctx->coded_frame->key_frame = keyframe; chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024); palptr = (uint32_t*)p->data[1]; @@ -253,6 +251,20 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, return 0; } +static av_cold int encode_end(AVCodecContext *avctx) +{ + ZmbvEncContext * const c = avctx->priv_data; + + av_freep(&c->comp_buf); + av_freep(&c->work_buf); + + deflateEnd(&c->zstream); + av_freep(&c->prev); + + av_frame_free(&avctx->coded_frame); + + return 0; +} /** * Init zmbv encoder @@ -314,25 +326,11 @@ static av_cold int encode_init(AVCodecContext *avctx) return -1; } - avctx->coded_frame = &c->pic; - - return 0; -} - - - -/** - * Uninit zmbv encoder - */ -static av_cold int encode_end(AVCodecContext *avctx) -{ - ZmbvEncContext * const c = avctx->priv_data; - - av_freep(&c->comp_buf); - av_freep(&c->work_buf); - - deflateEnd(&c->zstream); - av_freep(&c->prev); + avctx->coded_frame = av_frame_alloc(); + if (!avctx->coded_frame) { + encode_end(avctx); + return AVERROR(ENOMEM); + } return 0; }