From 8c4b1d8a65e0e40e71dfb4e80f47d80fb21b815e Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 13 Oct 2017 18:59:17 +0200 Subject: [PATCH] decode: add a method for attaching lavc-internal data to frames Use the AVFrame.private_ref field. This new struct will be useful in the following commits. Merges Libav commit 359a8a3e2d1194b52b6c386f94fd0929567dfb67. --- libavcodec/decode.c | 61 ++++++++++++++++++++++++++++++++++-- libavcodec/decode.h | 16 ++++++++++ libavcodec/wrapped_avframe.c | 7 +++++ 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 86fe5aef52a9..c7bd14393a4a 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -613,6 +613,26 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) if (ret == AVERROR_EOF) avci->draining_done = 1; + /* unwrap the per-frame decode data and restore the original private_ref */ + if (!ret) { + /* the only case where decode data is not set should be decoders + * that do not call ff_get_buffer() */ + av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) || + !(avctx->codec->capabilities & AV_CODEC_CAP_DR1)); + + if (frame->private_ref) { + FrameDecodeData *fdd; + AVBufferRef *prev_private_ref; + + fdd = (FrameDecodeData*)frame->private_ref->data; + + prev_private_ref = fdd->prev_private_ref; + fdd->prev_private_ref = NULL; + av_buffer_unref(&frame->private_ref); + frame->private_ref = prev_private_ref; + } + } + return ret; } @@ -1552,6 +1572,37 @@ static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame) } } +static void decode_data_free(void *opaque, uint8_t *data) +{ + FrameDecodeData *fdd = (FrameDecodeData*)data; + + av_buffer_unref(&fdd->prev_private_ref); + + av_freep(&fdd); +} + +int ff_attach_decode_data(AVFrame *frame) +{ + AVBufferRef *fdd_buf; + FrameDecodeData *fdd; + + fdd = av_mallocz(sizeof(*fdd)); + if (!fdd) + return AVERROR(ENOMEM); + + fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free, + NULL, AV_BUFFER_FLAG_READONLY); + if (!fdd_buf) { + av_freep(&fdd); + return AVERROR(ENOMEM); + } + + fdd->prev_private_ref = frame->private_ref; + frame->private_ref = fdd_buf; + + return 0; +} + static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags) { const AVHWAccel *hwaccel = avctx->hwaccel; @@ -1588,8 +1639,14 @@ static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags) avctx->sw_pix_fmt = avctx->pix_fmt; ret = avctx->get_buffer2(avctx, frame, flags); - if (ret >= 0) - validate_avframe_allocation(avctx, frame); + if (ret < 0) + goto end; + + validate_avframe_allocation(avctx, frame); + + ret = ff_attach_decode_data(frame); + if (ret < 0) + goto end; end: if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions && diff --git a/libavcodec/decode.h b/libavcodec/decode.h index c9630228dc5f..0daef1b2eee4 100644 --- a/libavcodec/decode.h +++ b/libavcodec/decode.h @@ -21,8 +21,22 @@ #ifndef AVCODEC_DECODE_H #define AVCODEC_DECODE_H +#include "libavutil/buffer.h" + #include "avcodec.h" +/** + * This struct stores per-frame lavc-internal data and is attached to it via + * private_ref. + */ +typedef struct FrameDecodeData { + /** + * The original private_ref. + * Useful for example with nested de/encoders. + */ + AVBufferRef *prev_private_ref; +} FrameDecodeData; + /** * Called by decoders to get the next packet for decoding. * @@ -36,4 +50,6 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt); void ff_decode_bsfs_uninit(AVCodecContext *avctx); +int ff_attach_decode_data(AVFrame *frame); + #endif /* AVCODEC_DECODE_H */ diff --git a/libavcodec/wrapped_avframe.c b/libavcodec/wrapped_avframe.c index 5f88a668b927..85ff32d13aef 100644 --- a/libavcodec/wrapped_avframe.c +++ b/libavcodec/wrapped_avframe.c @@ -25,6 +25,7 @@ */ #include "avcodec.h" +#include "decode.h" #include "internal.h" #include "libavutil/internal.h" @@ -98,6 +99,12 @@ static int wrapped_avframe_decode(AVCodecContext *avctx, void *data, av_frame_move_ref(out, in); + err = ff_attach_decode_data(out); + if (err < 0) { + av_frame_unref(out); + return err; + } + *got_frame = 1; return 0; }