From patchwork Mon Mar 26 10:41:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexander Kravchenko X-Patchwork-Id: 8176 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp3389303jad; Mon, 26 Mar 2018 03:47:14 -0700 (PDT) X-Google-Smtp-Source: AG47ELu60iHL+/VvViHArViRKeyLibfF7P0gHhc0cB9hegXQ2Hui3OeH2X/whgolQRYRM2SMPfen X-Received: by 10.223.184.219 with SMTP id c27mr32863801wrg.30.1522061234106; Mon, 26 Mar 2018 03:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522061234; cv=none; d=google.com; s=arc-20160816; b=ty+1n186ji4455Ylrp/DI8ifLxTs52lbjE0UoAfCT5ry+55v3rvFxvh/98aBGp9d8o wU//or0pX6vhRKbr1Rk+cbKRfvMCCv3BDVGsyAf8giuuZtHz3t5o20eGNP3O/vSebkTs 4AUh29TTkU8w8ZeYAcYokkcuN2hQxAmsFszWqToHXzXR3vPvRu/PHNX1rcjoimK4GX3F WodBao3NaaYClLIcVOv+KH/TaIehWbdf6fUHCj1Dok+mY6N2EjUW7WSgkgGtcqf0pp9l QO0ZiqVoIpvdEH6fyLzW1UvjPSB4BrPLPt64zYCTQh6SzDgjuP5JOmy1USjcjy/MOMw1 //Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-language:thread-index:mime-version :message-id:date:in-reply-to:references:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=HUiBXj4WECDE2FU1aOfHTDZfMhhkCdrajrLdyB0F70o=; b=kzmIaQuq9uwWdBlnxvpXEo0E3qa+lSn+hXCx5DKfPef5EIfSnjxGnr5BMrdDSS0eZa baPMV7ktGYplazxv1zO5rssYMR1s7cs8dFVFHNu14sDSgJISmS8/tJF3rzIOziNu6w3F APcUAgDP8SdiiK3M1ZK1nt/z+KhHeKxu3LLcuIdPMynNCyKGrR/eJHsHw89rQpmfbJ5z 0uM5dMH/NfXxACS4EDcob+7TBTsroBBUs1mQoAtw6fJYWOt2ZiKZfzO1GE2grVlBp5Mv LOJXa8SXqhaTEuGdNs/vHCw+YsdSPr5aaT/HBENabRP8G9pV42TMWrP/2U6l8WOgoM8R KyAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=BO5xOu5m; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m14si11877177wrb.449.2018.03.26.03.47.13; Mon, 26 Mar 2018 03:47:14 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=BO5xOu5m; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6EE82680841; Mon, 26 Mar 2018 13:46:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf0-f50.google.com (mail-lf0-f50.google.com [209.85.215.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 06434680216 for ; Mon, 26 Mar 2018 13:46:48 +0300 (EEST) Received: by mail-lf0-f50.google.com with SMTP id g203-v6so27373594lfg.11 for ; Mon, 26 Mar 2018 03:47:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-transfer-encoding:thread-index:content-language; bh=Il0Cs9ZGSbFeZmg57awgdRra361cicUIn5BG424xIxY=; b=BO5xOu5m0Xze2BYL5n8bLMU1JLXYWW4BW83onHWF0/OzLOwGH1Iz6xXu69rZ6EDLJi ypCEfyVrZjQeLkHG2qYRG9uKHF9Ll0sciYff0BdFi9yX/sl7Nvn3odbhcLliNaivH5he m/mcMvO/yLwPD2InrO0HXaxWJ1omVXTLhXxjZ6mgzrgUceGVktxVFfeSmOqtL+YrgolZ xX32DxnPRYDJULTS0sbC6CHzPRByEw1HpaL3ZByOt8dlN0AQBCykkvXdUPxlqw/APXCQ +nVyXlknu2LsY8t5xINqTlKXbfGTGsI5cZ5nJBBHumHs/9gSNyU792bwU2caRqA48YBE QH4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:thread-index :content-language; bh=Il0Cs9ZGSbFeZmg57awgdRra361cicUIn5BG424xIxY=; b=moCoN6ctrLc9D/fmQA2mHDxV7jN6ov/cLZCpZV1fPybZmF1uWzJZ+rCimoUBAaXkKT DAYu+ir7+v5Hld9vc8pRpj6sC/HS1k2K3k0bw96hFH0ZSdjJIrM2/8rXZNrZvsJB+FGE 7UYW3avQnGUzgUBtZ1oF7ySCSUuePv89SyH1ieR8bN3r01dHStPVlKNLwNou8eQt++qo nIe6AgIFnkFvb81wJLnJPmzzpEkPW6bnAMtjpDvDQJeIg277cZs+av8V/2el3yBPSH1q 99z7YHnJnXy8KP09ui4Hb6Wp3BpmqvnqFUqA4HhpOOY+e6I4boOjP+XwAnJVOFGRLksk Pa0w== X-Gm-Message-State: AElRT7E1RQcFWe/8OTi5K8WMYnW2ZiT5hMDqqoOTRXPRFDfgJb8JL4qp tCYW2F0tCPAwrm7Z9PzBgtVTvOp6 X-Received: by 2002:a19:7385:: with SMTP id h5-v6mr5930317lfk.67.1522060881096; Mon, 26 Mar 2018 03:41:21 -0700 (PDT) Received: from KAAMBWin10 ([188.227.65.188]) by smtp.gmail.com with ESMTPSA id n62-v6sm3764560lfi.4.2018.03.26.03.41.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Mar 2018 03:41:20 -0700 (PDT) From: "Alexander Kravchenko" To: "'FFmpeg development discussions and patches'" References: <1006FD0F0FE06E479CEE8E19981AE0703680AA52@ORO-MBOX-05.luxoft.com> In-Reply-To: Date: Mon, 26 Mar 2018 13:41:20 +0300 Message-ID: <002401d3c4ee$fada2fe0$f08e8fa0$@gmail.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQEOIRNY0HgZF/j9DGUWfZUmLoStCgIQX00SAgrc4gA= Content-Language: en-us Subject: [FFmpeg-devel] [PATCH] lavc/amfenc: Reference to input AVFrame (hwaccel) is retained during the encoding process X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Hello, I have fixed issues listed in previous patch. > Say what the change is in the title. Something like "amfenc: Retain a reference to D3D11 frames used as input during the encoding > process", maybe? Sure, but I am preparing next patch adding DX9 support, so probably better to write D3D instead D3D11 > > How many frames can end up queued inside the encoder here? 16 > > Is there always an exact 1->1 correspondence between input frames and output packets? That is, is it guaranteed that no frames are > ever dropped, even in the low-latency modes? yes > Put the * in the right place - it's part of the declarator, not the declaration-specifiers. > "if (", and in all places below too. I have fixed these issues in whole file (Hopefully you don’t mind if it put to same commit. There aren't many pf them) From: Alexander Kravchenko --- libavcodec/amfenc.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 89a10ff253..f532a32b7b 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -188,7 +188,7 @@ static int amf_init_context(AVCodecContext *avctx) return AVERROR(ENOMEM); } } else { - if(res == AMF_NOT_SUPPORTED) + if (res == AMF_NOT_SUPPORTED) av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has D3D11 device which doesn't have D3D11VA interface, switching to default\n"); else av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has non-AMD device, switching to default\n"); @@ -298,7 +298,7 @@ int av_cold ff_amf_encode_close(AVCodecContext *avctx) } static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame, - AMFSurface* surface) + AMFSurface *surface) { AVFrame *sw_frame = NULL; AMFPlane *plane = NULL; @@ -371,7 +371,7 @@ static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buff switch (avctx->codec->id) { case AV_CODEC_ID_H264: buffer->pVtbl->GetProperty(buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var); - if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) { + if (var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) { pkt->flags = AV_PKT_FLAG_KEY; } break; @@ -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) { @@ -458,7 +500,7 @@ int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame) if (!ctx->eof) { // submit drain one time only if (ctx->delayed_surface != NULL) { ctx->delayed_drain = 1; // input queue is full: resubmit Drain() in ff_amf_receive_packet - } else if(!ctx->delayed_drain) { + } else if (!ctx->delayed_drain) { res = ctx->encoder->pVtbl->Drain(ctx->encoder); if (res == AMF_INPUT_FULL) { ctx->delayed_drain = 1; // input queue is full: resubmit Drain() in ff_amf_receive_packet @@ -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,20 @@ 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_log(avctx, AV_LOG_WARNING, "failed to attach av_frame_ref to surface\n"); + av_frame_free(&frame_ref); + surface->pVtbl->Release(surface); + } + 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 +612,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);