lavc: add API for exporting reconstructed frames from encoders

This commit is contained in:
Anton Khirnov 2022-07-16 16:36:23 +02:00
parent eede1d2927
commit e3838b856f
11 changed files with 87 additions and 8 deletions

View File

@ -14,6 +14,11 @@ libavutil: 2021-04-27
API changes, most recent first:
2022-08-xx - xxxxxxxxxx - lavc 59.41.100 - avcodec.h codec.h
Add AV_CODEC_FLAG_RECON_FRAME and AV_CODEC_CAP_ENCODER_RECON_FRAME.
avcodec_receive_frame() may now be used on encoders when
AV_CODEC_FLAG_RECON_FRAME is active.
2022-08-xx - xxxxxxxxxx - lavu 57.31.100 - frame.h
av_frame_make_writable() may now be called on non-refcounted
frames and will make a refcounted copy out of them.

View File

@ -388,6 +388,8 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
}
if (avci->in_frame)
av_frame_unref(avci->in_frame);
if (avci->recon_frame)
av_frame_unref(avci->recon_frame);
} else {
av_packet_unref(avci->last_pkt_props);
while (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) >= 0)
@ -468,6 +470,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
av_packet_free(&avci->in_pkt);
av_frame_free(&avci->in_frame);
av_frame_free(&avci->recon_frame);
av_buffer_unref(&avci->pool);
@ -718,3 +721,12 @@ int avcodec_is_open(AVCodecContext *s)
{
return !!s->internal;
}
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
av_frame_unref(frame);
if (av_codec_is_decoder(avctx->codec))
return ff_decode_receive_frame(avctx, frame);
return ff_encode_receive_frame(avctx, frame);
}

View File

@ -231,6 +231,16 @@ typedef struct RcOverride{
* decoded frame in stream.
*/
#define AV_CODEC_FLAG_DROPCHANGED (1 << 5)
/**
* Request the encoder to output reconstructed frames, i.e. frames that would be
* produced by decoding the encoded bistream. These frames may be retrieved by
* calling avcodec_receive_frame() immediately after a successful call to
* avcodec_receive_packet().
*
* Should only be used with encoders flagged with the
* AV_CODEC_CAP_ENCODER_RECON_FRAME capability.
*/
#define AV_CODEC_FLAG_RECON_FRAME (1 << 6)
/**
* Use internal 2pass ratecontrol in first pass mode.
*/
@ -2601,21 +2611,23 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
/**
* Return decoded output data from a decoder.
* Return decoded output data from a decoder or encoder (when the
* AV_CODEC_FLAG_RECON_FRAME flag is used).
*
* @param avctx codec context
* @param frame This will be set to a reference-counted video or audio
* frame (depending on the decoder type) allocated by the
* decoder. Note that the function will always call
* codec. Note that the function will always call
* av_frame_unref(frame) before doing anything else.
*
* @return
* 0: success, a frame was returned
* AVERROR(EAGAIN): output is not available in this state - user must try
* to send new input
* AVERROR_EOF: the decoder has been fully flushed, and there will be
* AVERROR_EOF: the codec has been fully flushed, and there will be
* no more output frames
* AVERROR(EINVAL): codec not opened, or it is an encoder
* AVERROR(EINVAL): codec not opened, or it is an encoder without
* the AV_CODEC_FLAG_RECON_FRAME flag enabled
* AVERROR_INPUT_CHANGED: current decoded frame has changed parameters
* with respect to first decoded frame. Applicable
* when flag AV_CODEC_FLAG_DROPCHANGED is set.

View File

@ -182,6 +182,14 @@
*/
#define AV_CODEC_CAP_ENCODER_FLUSH (1 << 21)
/**
* The encoder is able to output reconstructed frame data, i.e. raw frames that
* would be produced by decoding the encoded bitstream.
*
* Reconstructed frame output is enabled by the AV_CODEC_FLAG_RECON_FRAME flag.
*/
#define AV_CODEC_CAP_ENCODER_RECON_FRAME (1 << 22)
/**
* AVProfile.
*/

View File

@ -697,13 +697,11 @@ static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
AV_FRAME_CROP_UNALIGNED : 0);
}
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
AVCodecInternal *avci = avctx->internal;
int ret, changed;
av_frame_unref(frame);
if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
return AVERROR(EINVAL);

View File

@ -53,6 +53,11 @@ typedef struct FrameDecodeData {
void (*hwaccel_priv_free)(void *priv);
} FrameDecodeData;
/**
* avcodec_receive_frame() implementation for decoders.
*/
int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame);
/**
* Called by decoders to get the next packet for decoding.
*

View File

@ -656,6 +656,18 @@ int ff_encode_preinit(AVCodecContext *avctx)
return AVERROR(ENOMEM);
}
if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
"from an encoder not supporting it\n");
return AVERROR(ENOSYS);
}
avci->recon_frame = av_frame_alloc();
if (!avci->recon_frame)
return AVERROR(ENOMEM);
}
return 0;
}
@ -692,3 +704,16 @@ int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
return 0;
}
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
AVCodecInternal *avci = avctx->internal;
if (!avci->recon_frame)
return AVERROR(EINVAL);
if (!avci->recon_frame->buf[0])
return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
av_frame_move_ref(frame, avci->recon_frame);
return 0;
}

View File

@ -26,6 +26,11 @@
#include "avcodec.h"
#include "packet.h"
/**
* avcodec_receive_frame() implementation for encoders.
*/
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame);
/**
* Called by encoders to get the next frame for encoding.
*

View File

@ -107,6 +107,14 @@ typedef struct AVCodecInternal {
*/
AVFrame *in_frame;
/**
* When the AV_CODEC_FLAG_RECON_FRAME flag is used. the encoder should store
* here the reconstructed frame corresponding to the last returned packet.
*
* Not allocated in other cases.
*/
AVFrame *recon_frame;
/**
* If this is set, then FFCodec->close (if existing) needs to be called
* for the parent AVCodecContext.

View File

@ -57,6 +57,7 @@ static const AVOption avcodec_options[] = {
{"qpel", "use 1/4-pel motion compensation", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_QPEL }, INT_MIN, INT_MAX, V|E, "flags"},
{"loop", "use loop filter", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_LOOP_FILTER }, INT_MIN, INT_MAX, V|E, "flags"},
{"qscale", "use fixed qscale", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_QSCALE }, INT_MIN, INT_MAX, 0, "flags"},
{"recon_frame", "export reconstructed frames", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_RECON_FRAME}, .unit = "flags"},
{"pass1", "use internal 2-pass ratecontrol in first pass mode", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_PASS1 }, INT_MIN, INT_MAX, 0, "flags"},
{"pass2", "use internal 2-pass ratecontrol in second pass mode", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_PASS2 }, INT_MIN, INT_MAX, 0, "flags"},
{"gray", "only decode/encode grayscale", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_GRAY }, INT_MIN, INT_MAX, V|E|D, "flags"},

View File

@ -29,7 +29,7 @@
#include "version_major.h"
#define LIBAVCODEC_VERSION_MINOR 40
#define LIBAVCODEC_VERSION_MINOR 41
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \