[FFmpeg-devel] [PATCH] lavc/amfenc: Reference to input AVFrame (hwaccel) is retained during the encoding process
Alexander Kravchenko
akravchenko188 at gmail.com
Tue Mar 27 01:51:09 EEST 2018
Fixed issues of previous patch
---
libavcodec/amfenc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 78 insertions(+), 4 deletions(-)
diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index 89a10ff253..5f3440a9f3 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -443,6 +443,48 @@ int ff_amf_encode_init(AVCodecContext *avctx)
return ret;
}
+#define AMF_AV_QUERY_INTERFACE(res, from, InterfaceTypeTo, to) \
+ { \
+ AMFGuid guid_##InterfaceTypeTo = IID_##InterfaceTypeTo(); \
+ res = from->pVtbl->QueryInterface(from, &guid_##InterfaceTypeTo, (void**)&to); \
+ }
+
+#define AMF_AV_ASSIGN_PROPERTY_INTERFACE(res, pThis, name, val) \
+ { \
+ AMFInterface *amf_interface; \
+ AMFVariantStruct var; \
+ res = AMFVariantInit(&var); \
+ if (res != AMF_OK) \
+ return res; \
+ if (res == AMF_OK) { \
+ AMF_AV_QUERY_INTERFACE(res, val, AMFInterface, amf_interface)\
+ } \
+ if (res == AMF_OK) { \
+ res = AMFVariantAssignInterface(&var, amf_interface); \
+ amf_interface->pVtbl->Release(amf_interface); \
+ } \
+ if (res == AMF_OK) { \
+ res = pThis->pVtbl->SetProperty(pThis, name, var); \
+ } \
+ res = AMFVariantClear(&var); \
+ }
+
+#define AMF_AV_GET_PROPERTY_INTERFACE(res, pThis, name, TargetType, val) \
+ { \
+ AMFVariantStruct var; \
+ res = AMFVariantInit(&var); \
+ if (res != AMF_OK) \
+ return res; \
+ res = pThis->pVtbl->GetProperty(pThis, name, &var); \
+ if (res == AMF_OK) { \
+ if (var.type == AMF_VARIANT_INTERFACE && AMFVariantInterface(&var)) { \
+ AMF_AV_QUERY_INTERFACE(res, AMFVariantInterface(&var), TargetType, val); \
+ } else { \
+ res = AMF_INVALID_DATA_TYPE; \
+ } \
+ } \
+ AMFVariantClear(&var); \
+ }
int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame)
{
@@ -484,6 +526,8 @@ int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame)
(ctx->hw_device_ctx && ((AVHWFramesContext*)frame->hw_frames_ctx->data)->device_ctx ==
(AVHWDeviceContext*)ctx->hw_device_ctx->data)
)) {
+ AVFrame *frame_ref;
+ AMFBuffer *frame_ref_storage_buffer;
#if CONFIG_D3D11VA
static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } };
ID3D11Texture2D *texture = (ID3D11Texture2D*)frame->data[0]; // actual texture
@@ -496,6 +540,21 @@ int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame)
// input HW surfaces can be vertically aligned by 16; tell AMF the real size
surface->pVtbl->SetCrop(surface, 0, 0, frame->width, frame->height);
#endif
+ res = ctx->context->pVtbl->AllocBuffer(ctx->context, AMF_MEMORY_HOST, sizeof(frame_ref), &frame_ref_storage_buffer);
+ AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "AllocBuffer() failed with error %d\n", res);
+ frame_ref = av_frame_clone(frame);
+ AMF_RETURN_IF_FALSE(ctx, frame_ref != NULL, AVERROR(ENOMEM), "av_frame_clone() returned NULL\n");
+ memcpy(frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), &frame_ref, sizeof(frame_ref));
+ AMF_AV_ASSIGN_PROPERTY_INTERFACE(res, surface, L"av_frame_ref", frame_ref_storage_buffer);
+ if (res != AMF_OK)
+ {
+ av_frame_free(&frame_ref);
+ surface->pVtbl->Release(surface);
+ av_log(avctx, AV_LOG_WARNING, "failed to attach av_frame_ref to surface\n");
+ return AVERROR(ENOMEM);
+ }
+ frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
+ AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "SetProperty failed for \"frame_ref\" with error %d\n", res);
} else {
res = ctx->context->pVtbl->AllocSurface(ctx->context, AMF_MEMORY_HOST, ctx->format, avctx->width, avctx->height, &surface);
AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "AllocSurface() failed with error %d\n", res);
@@ -554,12 +613,27 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
res_query = ctx->encoder->pVtbl->QueryOutput(ctx->encoder, &data);
if (data) {
// copy data to packet
- AMFBuffer* buffer;
- AMFGuid guid = IID_AMFBuffer();
- data->pVtbl->QueryInterface(data, &guid, (void**)&buffer); // query for buffer interface
+ AMFBuffer *buffer;
+ AMF_AV_QUERY_INTERFACE(res, data, AMFBuffer, buffer);
+ AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Invalid data type from encoder->QueryOutput, should be AMFBuffer, error %d\n", res);
ret = amf_copy_buffer(avctx, avpkt, buffer);
-
buffer->pVtbl->Release(buffer);
+
+ //try to get attached av_frame_ref and unref
+ if (data->pVtbl->HasProperty(data, L"av_frame_ref")) {
+ AMFBuffer *frame_ref_storage_buffer = NULL;
+ AVFrame *av_frame_ref;
+
+ AMF_AV_GET_PROPERTY_INTERFACE(res, data, L"av_frame_ref", AMFBuffer, frame_ref_storage_buffer);
+ if (res == AMF_OK) {
+ memcpy(&av_frame_ref, frame_ref_storage_buffer->pVtbl->GetNative(frame_ref_storage_buffer), sizeof(av_frame_ref));
+ av_frame_free(&av_frame_ref);
+ frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer);
+ } else {
+ av_log(avctx, AV_LOG_WARNING, "av_frame_ref data attached to frame is corrupted\n");
+ }
+ }
+
data->pVtbl->Release(data);
AMF_RETURN_IF_FALSE(ctx, ret >= 0, ret, "amf_copy_buffer() failed with error %d\n", ret);
--
2.16.2.windows.1
More information about the ffmpeg-devel
mailing list