From patchwork Mon Apr 8 07:10:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zachary Zhou X-Patchwork-Id: 12638 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id BFB104481D5 for ; Mon, 8 Apr 2019 10:11:51 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A837968AAA5; Mon, 8 Apr 2019 10:11:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9252B68AA67 for ; Mon, 8 Apr 2019 10:11:44 +0300 (EEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Apr 2019 00:11:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,324,1549958400"; d="scan'208";a="132334380" Received: from jiandon-desk.sh.intel.com ([10.239.193.48]) by orsmga008.jf.intel.com with ESMTP; 08 Apr 2019 00:11:38 -0700 From: Zachary Zhou To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Apr 2019 15:10:10 +0800 Message-Id: <20190408071010.12955-3-zachary.zhou@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190408071010.12955-1-zachary.zhou@intel.com> References: <20190408071010.12955-1-zachary.zhou@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v1 3/3] libavutil: Add thumbnail output to vaapi_h264 decoder 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 Cc: zachary.zhou@intel.com Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This is sample code for reference HW support for decode+scaling in a single HW command (VDBOX+SFC). The primary target usage is video analytics, but can be used playback, transcoding, etc. For VAAPI - https://github.com/intel/libva basically, it allows multiple outputs (in different resolutions) using the decode context in a single call (you can search for “additional_outputs” in va.h). VAAPI sample code - https://github.com/intel/libva-utils/commit/957a269f02b00760b7e807643c821ee26abc529b --- libavutil/frame.c | 1 + libavutil/frame.h | 13 +++++++ libavutil/hwcontext_vaapi.c | 73 ++++++++++++++++++++++++++++++++++++- libavutil/hwcontext_vaapi.h | 6 +++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/libavutil/frame.c b/libavutil/frame.c index dcf1fc3d17..aa9cd744ae 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -842,6 +842,7 @@ const char *av_frame_side_data_name(enum AVFrameSideDataType type) #endif case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)"; case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest"; + case AV_FRAME_DATA_SFC_INFO: return "SFC Information"; } return NULL; } diff --git a/libavutil/frame.h b/libavutil/frame.h index 8aa3e88367..616e7d9d4e 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -179,6 +179,11 @@ enum AVFrameSideDataType { * array element is implied by AVFrameSideData.size / AVRegionOfInterest.self_size. */ AV_FRAME_DATA_REGIONS_OF_INTEREST, + + /** + * VDENC SFC information. + */ + AV_FRAME_DATA_SFC_INFO, }; enum AVActiveFormatDescription { @@ -235,6 +240,14 @@ typedef struct AVRegionOfInterest { AVRational qoffset; } AVRegionOfInterest; +/** + * Structure to hold Information of SFC. + * + */ +typedef struct AVSFCInfo { + uint8_t *data; +} AVSFCInfo; + /** * This structure describes decoded (raw) audio or video data. * diff --git a/libavutil/hwcontext_vaapi.c b/libavutil/hwcontext_vaapi.c index 8624369bb9..caa6d02b15 100644 --- a/libavutil/hwcontext_vaapi.c +++ b/libavutil/hwcontext_vaapi.c @@ -642,8 +642,74 @@ static void vaapi_frames_uninit(AVHWFramesContext *hwfc) av_freep(&ctx->attributes); } +static int vaapi_get_sfc_buffer(AVHWFramesContext *hwfc, AVFrame *frame) +{ + AVVAAPIFramesContext *avfc = hwfc->hwctx; + AVFrameSideData *frame_sfc_sd = av_frame_get_side_data(frame, + AV_FRAME_DATA_SFC_INFO); + if (frame_sfc_sd) { + AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx; + VASurfaceID sfc_surface_id; + VASurfaceAttrib attrib; + unsigned int rt_format; + AVBufferRef *ref; + VAStatus vas; + + memset(&attrib, 0, sizeof(attrib)); + attrib.type = VASurfaceAttribPixelFormat; + attrib.flags = VA_SURFACE_ATTRIB_SETTABLE; + attrib.value.type = VAGenericValueTypeInteger; + + switch (avfc->sfc_format) { + case AV_PIX_FMT_NV12: + attrib.value.value.i = VA_FOURCC_NV12; + rt_format = VA_RT_FORMAT_YUV420; + break; + case AV_PIX_FMT_ARGB: + attrib.value.value.i = VA_FOURCC_ARGB; + rt_format = VA_RT_FORMAT_RGB32; + break; + default: + av_log(hwfc, AV_LOG_ERROR, "Unknown sfc format: %d\n", avfc->sfc_format); + return AVERROR(EINVAL); + } + + vas = vaCreateSurfaces(hwctx->display, rt_format, + avfc->sfc_width, avfc->sfc_height, + &sfc_surface_id, 1, + &attrib, 1); + if (vas != VA_STATUS_SUCCESS) { + av_log(hwfc, AV_LOG_ERROR, "Failed to create sfc surface: " + "%d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR(EIO); + } + + av_log(hwfc, AV_LOG_DEBUG, "Created sfc surface %#x. sfc_format(%d), w(%d), h(%d)\n", + sfc_surface_id, avfc->sfc_format, avfc->sfc_width, avfc->sfc_height); + + ref = av_buffer_create((uint8_t*)(uintptr_t)sfc_surface_id, + sizeof(sfc_surface_id), &vaapi_buffer_free, + hwfc, AV_BUFFER_FLAG_READONLY); + if (!ref) { + vaDestroySurfaces(hwctx->display, &sfc_surface_id, 1); + return AVERROR(EIO); + } + + frame_sfc_sd->buf = ref; + + av_log(hwfc, AV_LOG_DEBUG, "Created sfc av buffer ref: %p\n", ref); + + } else { + av_log(hwfc, AV_LOG_DEBUG, "VDSFC frame_sfc_sd is null\n"); + } + + return 0; +} + static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame) { + int err; + frame->buf[0] = av_buffer_pool_get(hwfc->pool); if (!frame->buf[0]) return AVERROR(ENOMEM); @@ -653,6 +719,11 @@ static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame) frame->width = hwfc->width; frame->height = hwfc->height; + //create sfc surface, put it in side data + err = vaapi_get_sfc_buffer(hwfc, frame); + if (err) + return err; + return 0; } @@ -784,7 +855,7 @@ static int vaapi_map_frame(AVHWFramesContext *hwfc, // assume for now that the user is not aware of that and would therefore // prefer not to be given direct-mapped memory if they request read access. if (ctx->derive_works && dst->format == hwfc->sw_format && - ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) { + ((flags & AV_HWFRAME_MAP_DIRECT) || (flags & AV_HWFRAME_MAP_READ))) { vas = vaDeriveImage(hwctx->display, surface_id, &map->image); if (vas != VA_STATUS_SUCCESS) { av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from " diff --git a/libavutil/hwcontext_vaapi.h b/libavutil/hwcontext_vaapi.h index 0b2e071cb3..e2b26ac1da 100644 --- a/libavutil/hwcontext_vaapi.h +++ b/libavutil/hwcontext_vaapi.h @@ -100,6 +100,12 @@ typedef struct AVVAAPIFramesContext { */ VASurfaceID *surface_ids; int nb_surfaces; + + // VSFC info + int sfc_flags; + int sfc_format; + int sfc_width; + int sfc_height; } AVVAAPIFramesContext; /**