From patchwork Sat Apr 14 15:53:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 8441 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp1937099jad; Sat, 14 Apr 2018 08:53:57 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+rqr+mLdYbygu+/d3NgM3wt3F0+qJ24JLWw3+OdBMF73z3DLfL5t1fj0HTY7/M8VOgqrxb X-Received: by 10.28.48.149 with SMTP id w143mr6766443wmw.88.1523721237614; Sat, 14 Apr 2018 08:53:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523721237; cv=none; d=google.com; s=arc-20160816; b=X4LHkdVFVOz7DMUsOa+4EQYs1hfaPD3PbIu4ciMkug4uQj2sLRcipCbI2dZHICuXs0 qAfekBI33YuMIDjw97EgxoqzgqtJwj9p9c6S8ag6JjM1gtBlKTodHY7Iqm2pul73UKgn d5ikhZoe/Jm6voZrCf7nKqrULPOz/eyQeKCGOIWhwg2vRHStB57idjUjYTOur/U112EU qC9IFQocbGopPRPrUDilwWG+B393L9QNvQevb3iWPEnv2qEBwEPsL+pH1S4yRB7lOVFJ JWdbSHso10tLmyh2/ldFJDEaqHoB8oHM62zM6TLYmkXY7tnGHOHriCiv82mJaffg3a6N cNxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=9e8MmvHmqjCG9RTjpUr+5Fmtb00QvMQElwSDmXPG3bU=; b=wUdsGXqYNBfi7vSLmSm+qfDx0Ummt2uZAGpSak+9aiU8VcVYdRiY/jX2ti3GjonnDX AiNzXL7VLSIHdi9ld8duCNP5wYiyaMtbxS4L1K6mlwku1pL4PAk1VQ2A2zHKsoyUWs5V gKbAbEHqQuf9WGhcxKj6YUtd1uQ5qzcMCoQwWWnmOSvdFnJ+JS2HrRXKQV5IViKk86VH 4GOn+dppYXc3kjKL1ale5uOoCgoSnOLiYFEj0yI9yATIJ9p6QuSJusrYNH9gq7yXCI9D JSKN//6EAr4+uotYRnFPi6J8pkO+3PcL54MOUw/9iLbJff3OBmzwapUSi8ZyGWU15drX LMGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=JxzUoQG2; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w74si2194551wmf.212.2018.04.14.08.53.56; Sat, 14 Apr 2018 08:53:57 -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=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=JxzUoQG2; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 80F3B689C71; Sat, 14 Apr 2018 18:53:29 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f182.google.com (mail-wr0-f182.google.com [209.85.128.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 16B1368972B for ; Sat, 14 Apr 2018 18:53:23 +0300 (EEST) Received: by mail-wr0-f182.google.com with SMTP id o3so15119903wri.2 for ; Sat, 14 Apr 2018 08:53:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=9Q8q/HC68yqB9BGEnCEtnCaZIXAitRGu/eQxBJa/eVI=; b=JxzUoQG2gFIPWK/vTwO0jHj7o8t8m6F/EGpKbHUVElcv04lMhJZA5Tx+KoJQR8mani ilbRZDkpZ4JZJ3SHJ/Xp9NLQBFQmfJbIKYPIfoJwExh5kmqLB3HUYy5whZ/6KvIiZhQM rlJDU2iDuXYzBbqxkP+ftlaVMorQh7eIKt7HUHtRoBSvo4C6dO/xnCWNjEMhD/RBaIL6 aBWZnufTFWY8ytOVx51l/7s7o7Mc/0SFvcxfpaBkBEzToYN/kbLZ9SRrdZKzKtb4GE1D Df/Tn4TAL9/OLeM/bNH19BPr3MNBvM0QTplEGoropLoviTOnY33fkqzRHbruq29pejdl eG0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=9Q8q/HC68yqB9BGEnCEtnCaZIXAitRGu/eQxBJa/eVI=; b=C9JCRF+0+MlaV8EftaOlAN4TB54DSJ4ZJna+ZmoQjSZxOexesPoJZCdCyuE7E7xSZR HIYY1ac2UhVrUbBk4jw6Atu6AInBMbjNAbW8DdqzdMxpM5HHn1yA5+/kbtG1es53vmN6 SpU5Xp98tpcDKLRYxeYD6SOZCKXC7le0/BWU/FQtzCHW7k5bkqoRKhj7luglV2sg8R+r bvUU72+UdQGj+vZboZXxyNgXC/J3aP8VSIO4MaHgpm/Hrkw+NEl5IdVlP3+vovy5kign gbPbuzR+dp2naB9w/PSzrIJ+BvLl8F6EZbCNVyIEpckl3DHB4ztu1/8KxsvB2+kwYCXX 2mHg== X-Gm-Message-State: ALQs6tBC1HGlgpf2OCxAcgU8QeGNG/ZjMBbaC0lTsk/7KL1pCOsO4tXS YGXNzlkH3G5tOMefN+IwQu8jElhr X-Received: by 10.28.69.68 with SMTP id s65mr5856318wma.25.1523721227587; Sat, 14 Apr 2018 08:53:47 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id n49sm13651823wrn.50.2018.04.14.08.53.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Apr 2018 08:53:46 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sat, 14 Apr 2018 16:53:36 +0100 Message-Id: <20180414155339.22839-2-sw@jkqxz.net> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180414155339.22839-1-sw@jkqxz.net> References: <20180414155339.22839-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH 2/5] amfenc: Do not automatically download/upload unknown hardware input frames 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Supplying a hardware input frame which is not in the input hardware frames context is not allowed by the API, so additional code to handle it is not necessary. Handling it automatically results in very low performance - it is more appropriate to fail immediately so that the user can fix their incorrect setup. --- libavcodec/amfenc.c | 88 +++++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 53 deletions(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 8a9d6884a4..d1a28f13e2 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -71,14 +71,6 @@ static const FormatMap format_map[] = { AV_PIX_FMT_D3D11, AMF_SURFACE_NV12 }, }; - -static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt) -{ - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); - return desc->flags & AV_PIX_FMT_FLAG_HWACCEL; -} - - static enum AMF_SURFACE_FORMAT amf_av_to_amf_format(enum AVPixelFormat fmt) { int i; @@ -337,32 +329,14 @@ int av_cold ff_amf_encode_close(AVCodecContext *avctx) static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame, AMFSurface* surface) { - AVFrame *sw_frame = NULL; AMFPlane *plane = NULL; uint8_t *dst_data[4]; int dst_linesize[4]; - int ret = 0; int planes; int i; - if (frame->hw_frames_ctx && is_hwaccel_pix_fmt(frame->format)) { - if (!(sw_frame = av_frame_alloc())) { - av_log(avctx, AV_LOG_ERROR, "Can not alloc frame\n"); - ret = AVERROR(ENOMEM); - goto fail; - } - if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) { - av_log(avctx, AV_LOG_ERROR, "Error transferring the data to system memory\n"); - goto fail; - } - frame = sw_frame; - } - planes = (int)surface->pVtbl->GetPlanesCount(surface); - if (planes > amf_countof(dst_data)) { - av_log(avctx, AV_LOG_ERROR, "Invalid number of planes %d in surface\n", planes); - ret = AVERROR(EINVAL); - goto fail; - } + planes = surface->pVtbl->GetPlanesCount(surface); + av_assert0(planes < FF_ARRAY_ELEMS(dst_data)); for (i = 0; i < planes; i++) { plane = surface->pVtbl->GetPlaneAt(surface, i); @@ -373,11 +347,7 @@ static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame, (const uint8_t**)frame->data, frame->linesize, frame->format, avctx->width, avctx->height); -fail: - if (sw_frame) { - av_frame_free(&sw_frame); - } - return ret; + return 0; } static inline int timestamp_queue_enqueue(AVCodecContext *avctx, int64_t timestamp) @@ -579,31 +549,46 @@ int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame) return AVERROR_EOF; } } else { // submit frame + int hw_surface = 0; + if (ctx->delayed_surface != NULL) { return AVERROR(EAGAIN); // should not happen when called from ffmpeg, other clients may resubmit } // prepare surface from frame - if (frame->hw_frames_ctx && ( // HW frame detected - // check if the same hw_frames_ctx as used in initialization - (ctx->hw_frames_ctx && frame->hw_frames_ctx->data == ctx->hw_frames_ctx->data) || - // check if the same hw_device_ctx as used in initialization - (ctx->hw_device_ctx && ((AVHWFramesContext*)frame->hw_frames_ctx->data)->device_ctx == - (AVHWDeviceContext*)ctx->hw_device_ctx->data) - )) { - AMFBuffer *frame_ref_storage_buffer; - + switch (frame->format) { #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 - int index = (int)(size_t)frame->data[1]; // index is a slice in texture array is - set to tell AMF which slice to use - texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID, sizeof(index), &index); + case AV_PIX_FMT_D3D11: + { + static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } }; + ID3D11Texture2D *texture = (ID3D11Texture2D*)frame->data[0]; // actual texture + int index = (int)(size_t)frame->data[1]; // index is a slice in texture array is - set to tell AMF which slice to use - res = ctx->context->pVtbl->CreateSurfaceFromDX11Native(ctx->context, texture, &surface, NULL); // wrap to AMF surface - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX11Native() failed with error %d\n", res); + av_assert0(frame->hw_frames_ctx && ctx->hw_frames_ctx && + frame->hw_frames_ctx->data == ctx->hw_frames_ctx->data); + + texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID, sizeof(index), &index); + + res = ctx->context->pVtbl->CreateSurfaceFromDX11Native(ctx->context, texture, &surface, NULL); // wrap to AMF surface + AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX11Native() failed with error %d\n", res); + + hw_surface = 1; + } + break; +#endif + default: + { + 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); + amf_copy_surface(avctx, frame, surface); + } + break; + } + + if (hw_surface) { + AMFBuffer *frame_ref_storage_buffer; // 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 frame_ref_storage_buffer = amf_create_buffer_with_frame_ref(frame, ctx->context); AMF_RETURN_IF_FALSE(ctx, frame_ref_storage_buffer != NULL, AVERROR(ENOMEM), "create_buffer_with_frame_ref() returned NULL\n"); @@ -612,11 +597,8 @@ int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame) AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "SetProperty failed for \"av_frame_ref\" with error %d\n", res); ctx->hwsurfaces_in_queue++; frame_ref_storage_buffer->pVtbl->Release(frame_ref_storage_buffer); - } 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); - amf_copy_surface(avctx, frame, surface); } + surface->pVtbl->SetPts(surface, frame->pts); AMF_ASSIGN_PROPERTY_INT64(res, surface, PTS_PROP, frame->pts);