From 219b35f5d169d9ece408262ba2337dab9d19aafd Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 1/4] gif: use the AVFrame API properly. --- libavcodec/gif.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libavcodec/gif.c b/libavcodec/gif.c index 4dc124f49b..c6c37b6c66 100644 --- a/libavcodec/gif.c +++ b/libavcodec/gif.c @@ -53,7 +53,6 @@ #include "put_bits.h" typedef struct { - AVFrame picture; LZWState *lzw; uint8_t *buf; } GIFContext; @@ -131,7 +130,13 @@ static av_cold int gif_encode_init(AVCodecContext *avctx) { GIFContext *s = avctx->priv_data; - avctx->coded_frame = &s->picture; + avctx->coded_frame = av_frame_alloc(); + if (!avctx->coded_frame) + return AVERROR(ENOMEM); + + avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; + avctx->coded_frame->key_frame = 1; + s->lzw = av_mallocz(ff_lzw_encode_state_size); if (!s->lzw) return AVERROR(ENOMEM); @@ -145,8 +150,6 @@ static av_cold int gif_encode_init(AVCodecContext *avctx) static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet) { - GIFContext *s = avctx->priv_data; - AVFrame *const p = &s->picture; uint8_t *outbuf_ptr, *end; int ret; @@ -157,9 +160,6 @@ static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt, outbuf_ptr = pkt->data; end = pkt->data + pkt->size; - *p = *pict; - p->pict_type = AV_PICTURE_TYPE_I; - p->key_frame = 1; gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]); gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]); @@ -174,6 +174,8 @@ static int gif_encode_close(AVCodecContext *avctx) { GIFContext *s = avctx->priv_data; + av_frame_free(&avctx->coded_frame); + av_freep(&s->lzw); av_freep(&s->buf); return 0; From 394ef4d18f377b357885f95307853b253669de7c Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 2/4] svq1enc: use the AVFrame API properly. --- libavcodec/svq1enc.c | 111 ++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c index fa94f52abb..3cd3a4aaed 100644 --- a/libavcodec/svq1enc.c +++ b/libavcodec/svq1enc.c @@ -46,9 +46,8 @@ typedef struct SVQ1Context { AVCodecContext *avctx; DSPContext dsp; HpelDSPContext hdsp; - AVFrame picture; - AVFrame current_picture; - AVFrame last_picture; + AVFrame *current_picture; + AVFrame *last_picture; PutBitContext pb; GetBitContext gb; @@ -265,13 +264,14 @@ static int svq1_encode_plane(SVQ1Context *s, int plane, unsigned char *decoded_plane, int width, int height, int src_stride, int stride) { + const AVFrame *f = s->avctx->coded_frame; int x, y; int i; int block_width, block_height; int level; int threshold[6]; uint8_t *src = s->scratchbuf + stride * 16; - const int lambda = (s->picture.quality * s->picture.quality) >> + const int lambda = (f->quality * f->quality) >> (2 * FF_LAMBDA_SHIFT); /* figure out the acceptable level thresholds in advance */ @@ -282,7 +282,7 @@ static int svq1_encode_plane(SVQ1Context *s, int plane, block_width = (width + 15) / 16; block_height = (height + 15) / 16; - if (s->picture.pict_type == AV_PICTURE_TYPE_P) { + if (f->pict_type == AV_PICTURE_TYPE_P) { s->m.avctx = s->avctx; s->m.current_picture_ptr = &s->m.current_picture; s->m.last_picture_ptr = &s->m.last_picture; @@ -298,13 +298,13 @@ static int svq1_encode_plane(SVQ1Context *s, int plane, s->m.mb_stride = s->m.mb_width + 1; s->m.b8_stride = 2 * s->m.mb_width + 1; s->m.f_code = 1; - s->m.pict_type = s->picture.pict_type; + s->m.pict_type = f->pict_type; s->m.me_method = s->avctx->me_method; s->m.me.scene_change_score = 0; s->m.flags = s->avctx->flags; // s->m.out_format = FMT_H263; // s->m.unrestricted_mv = 1; - s->m.lambda = s->picture.quality; + s->m.lambda = f->quality; s->m.qscale = s->m.lambda * 139 + FF_LAMBDA_SCALE * 64 >> FF_LAMBDA_SHIFT + 7; @@ -397,13 +397,13 @@ static int svq1_encode_plane(SVQ1Context *s, int plane, ff_init_block_index(&s->m); ff_update_block_index(&s->m); - if (s->picture.pict_type == AV_PICTURE_TYPE_I || + if (f->pict_type == AV_PICTURE_TYPE_I || (s->m.mb_type[x + y * s->m.mb_stride] & CANDIDATE_MB_TYPE_INTRA)) { for (i = 0; i < 6; i++) init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i], 7 * 32); - if (s->picture.pict_type == AV_PICTURE_TYPE_P) { + if (f->pict_type == AV_PICTURE_TYPE_P) { const uint8_t *vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_INTRA]; put_bits(&s->reorder_pb[5], vlc[1], vlc[0]); score[0] = vlc[1] * lambda; @@ -419,7 +419,7 @@ static int svq1_encode_plane(SVQ1Context *s, int plane, best = 0; - if (s->picture.pict_type == AV_PICTURE_TYPE_P) { + if (f->pict_type == AV_PICTURE_TYPE_P) { const uint8_t *vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_INTER]; int mx, my, pred_x, pred_y, dxy; int16_t *motion_ptr; @@ -499,13 +499,48 @@ static int svq1_encode_plane(SVQ1Context *s, int plane, return 0; } +static av_cold int svq1_encode_end(AVCodecContext *avctx) +{ + SVQ1Context *const s = avctx->priv_data; + int i; + + av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", + s->rd_total / (double)(avctx->width * avctx->height * + avctx->frame_number)); + + av_freep(&s->m.me.scratchpad); + av_freep(&s->m.me.map); + av_freep(&s->m.me.score_map); + av_freep(&s->mb_type); + av_freep(&s->dummy); + av_freep(&s->scratchbuf); + + for (i = 0; i < 3; i++) { + av_freep(&s->motion_val8[i]); + av_freep(&s->motion_val16[i]); + } + + av_frame_free(&s->current_picture); + av_frame_free(&s->last_picture); + av_frame_free(&avctx->coded_frame); + + return 0; +} + static av_cold int svq1_encode_init(AVCodecContext *avctx) { SVQ1Context *const s = avctx->priv_data; ff_dsputil_init(&s->dsp, avctx); ff_hpeldsp_init(&s->hdsp, avctx->flags); - avctx->coded_frame = &s->picture; + + avctx->coded_frame = av_frame_alloc(); + s->current_picture = av_frame_alloc(); + s->last_picture = av_frame_alloc(); + if (!avctx->coded_frame || !s->current_picture || !s->last_picture) { + svq1_encode_end(avctx); + return AVERROR(ENOMEM); + } s->frame_width = avctx->width; s->frame_height = avctx->height; @@ -537,8 +572,7 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet) { SVQ1Context *const s = avctx->priv_data; - AVFrame *const p = &s->picture; - AVFrame temp; + AVFrame *const p = avctx->coded_frame; int i, ret; if (!pkt->data && @@ -553,33 +587,31 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, return -1; } - if (!s->current_picture.data[0]) { - ff_get_buffer(avctx, &s->current_picture, 0); - ff_get_buffer(avctx, &s->last_picture, 0); - s->scratchbuf = av_malloc(s->current_picture.linesize[0] * 16 * 2); + if (!s->current_picture->data[0]) { + ff_get_buffer(avctx, s->current_picture, 0); + ff_get_buffer(avctx, s->last_picture, 0); + s->scratchbuf = av_malloc(s->current_picture->linesize[0] * 16 * 2); } - temp = s->current_picture; - s->current_picture = s->last_picture; - s->last_picture = temp; + FFSWAP(AVFrame*, s->current_picture, s->last_picture); init_put_bits(&s->pb, pkt->data, pkt->size); - *p = *pict; p->pict_type = avctx->gop_size && avctx->frame_number % avctx->gop_size ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I; p->key_frame = p->pict_type == AV_PICTURE_TYPE_I; + p->quality = pict->quality; svq1_write_header(s, p->pict_type); for (i = 0; i < 3; i++) if (svq1_encode_plane(s, i, - s->picture.data[i], - s->last_picture.data[i], - s->current_picture.data[i], + pict->data[i], + s->last_picture->data[i], + s->current_picture->data[i], s->frame_width / (i ? 4 : 1), s->frame_height / (i ? 4 : 1), - s->picture.linesize[i], - s->current_picture.linesize[i]) < 0) + pict->linesize[i], + s->current_picture->linesize[i]) < 0) return -1; // avpriv_align_put_bits(&s->pb); @@ -596,33 +628,6 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, return 0; } -static av_cold int svq1_encode_end(AVCodecContext *avctx) -{ - SVQ1Context *const s = avctx->priv_data; - int i; - - av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", - s->rd_total / (double)(avctx->width * avctx->height * - avctx->frame_number)); - - av_freep(&s->m.me.scratchpad); - av_freep(&s->m.me.map); - av_freep(&s->m.me.score_map); - av_freep(&s->mb_type); - av_freep(&s->dummy); - av_freep(&s->scratchbuf); - - for (i = 0; i < 3; i++) { - av_freep(&s->motion_val8[i]); - av_freep(&s->motion_val16[i]); - } - - av_frame_unref(&s->current_picture); - av_frame_unref(&s->last_picture); - - return 0; -} - AVCodec ff_svq1_encoder = { .name = "svq1", .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"), From d2287740d932a79702074fb43d239d368946504a Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 3/4] libx264: use the AVFrame API properly. --- libavcodec/libx264.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index e842419150..08bb509dd8 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -44,7 +44,6 @@ typedef struct X264Context { x264_picture_t pic; uint8_t *sei; int sei_size; - AVFrame out_pic; char *preset; char *tune; char *profile; @@ -181,20 +180,20 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, switch (pic_out.i_type) { case X264_TYPE_IDR: case X264_TYPE_I: - x4->out_pic.pict_type = AV_PICTURE_TYPE_I; + ctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; break; case X264_TYPE_P: - x4->out_pic.pict_type = AV_PICTURE_TYPE_P; + ctx->coded_frame->pict_type = AV_PICTURE_TYPE_P; break; case X264_TYPE_B: case X264_TYPE_BREF: - x4->out_pic.pict_type = AV_PICTURE_TYPE_B; + ctx->coded_frame->pict_type = AV_PICTURE_TYPE_B; break; } pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe; if (ret) - x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; + ctx->coded_frame->quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; *got_packet = ret; return 0; @@ -210,6 +209,8 @@ static av_cold int X264_close(AVCodecContext *avctx) if (x4->enc) x264_encoder_close(x4->enc); + av_frame_free(&avctx->coded_frame); + return 0; } @@ -450,7 +451,9 @@ static av_cold int X264_init(AVCodecContext *avctx) if (!x4->enc) return -1; - avctx->coded_frame = &x4->out_pic; + avctx->coded_frame = av_frame_alloc(); + if (!avctx->coded_frame) + return AVERROR(ENOMEM); if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { x264_nal_t *nal; From 57e7b3a89f5a0879ad039e8f04273b48649799a8 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 9 Nov 2013 10:14:46 +0100 Subject: [PATCH 4/4] dnxhdenc: use the AVFrame API properly. --- libavcodec/dnxhdenc.c | 28 ++++++++++++++-------------- libavcodec/dnxhdenc.h | 1 - 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c index d71186dd06..5eef57e5f1 100644 --- a/libavcodec/dnxhdenc.c +++ b/libavcodec/dnxhdenc.c @@ -307,9 +307,12 @@ static av_cold int dnxhd_encode_init(AVCodecContext *avctx) FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits, ctx->m.mb_num *sizeof(uint16_t), fail); FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale, ctx->m.mb_num *sizeof(uint8_t), fail); - ctx->frame.key_frame = 1; - ctx->frame.pict_type = AV_PICTURE_TYPE_I; - ctx->m.avctx->coded_frame = &ctx->frame; + avctx->coded_frame = av_frame_alloc(); + if (!avctx->coded_frame) + return AVERROR(ENOMEM); + + avctx->coded_frame->key_frame = 1; + avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; if (avctx->thread_count > MAX_THREADS) { av_log(avctx, AV_LOG_ERROR, "too many threads\n"); @@ -908,19 +911,14 @@ static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame) { int i; - for (i = 0; i < 3; i++) { - ctx->frame.data[i] = frame->data[i]; - ctx->frame.linesize[i] = frame->linesize[i]; - } - for (i = 0; i < ctx->m.avctx->thread_count; i++) { - ctx->thread[i]->m.linesize = ctx->frame.linesize[0]<interlaced; - ctx->thread[i]->m.uvlinesize = ctx->frame.linesize[1]<interlaced; + ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced; + ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced; ctx->thread[i]->dct_y_offset = ctx->m.linesize *8; ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8; } - ctx->frame.interlaced_frame = frame->interlaced_frame; + ctx->m.avctx->coded_frame->interlaced_frame = frame->interlaced_frame; ctx->cur_field = frame->interlaced_frame && !frame->top_field_first; } @@ -942,9 +940,9 @@ static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, encode_coding_unit: for (i = 0; i < 3; i++) { - ctx->src[i] = ctx->frame.data[i]; + ctx->src[i] = frame->data[i]; if (ctx->interlaced && ctx->cur_field) - ctx->src[i] += ctx->frame.linesize[i]; + ctx->src[i] += frame->linesize[i]; } dnxhd_write_header(avctx, buf); @@ -982,7 +980,7 @@ static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, goto encode_coding_unit; } - ctx->frame.quality = ctx->qscale*FF_QP2LAMBDA; + avctx->coded_frame->quality = ctx->qscale * FF_QP2LAMBDA; pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; @@ -1015,6 +1013,8 @@ static av_cold int dnxhd_encode_end(AVCodecContext *avctx) for (i = 1; i < avctx->thread_count; i++) av_freep(&ctx->thread[i]); + av_frame_free(&avctx->coded_frame); + return 0; } diff --git a/libavcodec/dnxhdenc.h b/libavcodec/dnxhdenc.h index fed17bed51..232e3348d3 100644 --- a/libavcodec/dnxhdenc.h +++ b/libavcodec/dnxhdenc.h @@ -43,7 +43,6 @@ typedef struct DNXHDEncContext { AVClass *class; MpegEncContext m; ///< Used for quantization dsp functions - AVFrame frame; int cid; const CIDEntry *cid_table; uint8_t *msip; ///< Macroblock Scan Indexes Payload