From patchwork Wed Jun 23 03:03:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 28863 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:bbc9:0:0:0:0:0 with SMTP id c9csp5480133ybk; Tue, 22 Jun 2021 20:05:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSundIKEQBsMcX6b8SxA/GmHJOOfFm8h9vCMqNUARL7cmHmTvIN9ZPHH1+Uf09FjB6fmQX X-Received: by 2002:a05:6402:27ce:: with SMTP id c14mr9215863ede.118.1624417507320; Tue, 22 Jun 2021 20:05:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624417507; cv=none; d=google.com; s=arc-20160816; b=V+BUTkXE+EeMCKKCzoaeGbx9uuYq7vV3yUMNlDaIFBdpawWsz4aP2MEzLNSqiXCMwo QxfMq+a7RJtxfmpVaRaBsDF1ng4+t3so6YECOo7pto6MjQiv/8hrkX+/oQmlMhl6fXR0 T/UoZYZG8Y3MZbUemuOzJ19GUL7TD2svlJGxtVwy78MRPekWaryWdM7yYaPvPQwAzD+C IfOJA8IAinLIaGkMdUM4gCN3wb4Z1xZLeSPJ41b9kQ/C6SFnsTdQWT7IWq+VTbCAOqFU e9kk110QM6crbhBhdmW2b0fZNVBifWRqyuQRdYgCIwywsJLJ14IU0EQ22fYTBfWSElJh CKRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :ironport-sdr:ironport-sdr:delivered-to; bh=UkZtqYq5E9WXH7NY+MW3y2FgtuT7G/v2ChIAIbpOBXA=; b=rdzJDwYFO+5Z9d0DtWmf6nNVzgfgommjhjWmf5OtoNah3FA3KIPSLqmVo3AkCqhc1D Uilx2zY+UaUQQuCa1rfQQHHmgdXsPDwoYTe1+GVzyxUBvHxtrHrPjmA1gJYgM7JZUlUU cYusnPgTAJNUNb4AezB0kcs++y+hBV/oiPGb2d7trf3lX8qvFHdqL0wWWunk4TqW+vX/ 4YJKJMKvGtQc0JIhjjbNQO2d940eiMwChmiewtDxfwzR3rqQ6xsyvwOMen5GFfxfRlM9 kVc2PRre2p0AtfuIVouoNsihUe6Ayn8PuRjkhfY09WY/xbBqzQOvwWHCbaTLpQy8NFL6 8LdA== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id kl18si17109668ejc.160.2021.06.22.20.05.06; Tue, 22 Jun 2021 20:05:07 -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; 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=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4C3AC680549; Wed, 23 Jun 2021 06:04:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 30F05680549 for ; Wed, 23 Jun 2021 06:04:48 +0300 (EEST) IronPort-SDR: e4vkDzTQ3tBOfO2mDgKKH/ysFhno7rHKUEynGqAgV8TSYdRajp3LP4vSSNu+YYoHCedMoPtzJE YAizbGXdwCzA== X-IronPort-AV: E=McAfee;i="6200,9189,10023"; a="205355664" X-IronPort-AV: E=Sophos;i="5.83,293,1616482800"; d="scan'208";a="205355664" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2021 20:04:02 -0700 IronPort-SDR: g7YDxuAv2OI4KS2LKhq9vbaq35mYVx+HFhLtBjrHlCEswqNMbuZ6/++m74vkU4gn8RoQ5CFqHt 1QLWqGOKgnkg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,293,1616482800"; d="scan'208";a="641844708" Received: from xhh-tgl64.sh.intel.com ([10.239.159.53]) by fmsmga005.fm.intel.com with ESMTP; 22 Jun 2021 20:03:51 -0700 From: Haihao Xiang To: ffmpeg-devel@ffmpeg.org Date: Wed, 23 Jun 2021 11:03:45 +0800 Message-Id: <20210623030346.2025933-1-haihao.xiang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] qsvdec: add support for HW_DEVICE_CTX method X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Haihao Xiang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 19klz9JbrP1b Content-Length: 13204 This allows user set hw_device_ctx instead of hw_frames_ctx for QSV decoders, hence we may remove the ad-hoc libmfx setup code from FFmpeg. "-hwaccel_output_format format" is applied to QSV decoders after removing the ad-hoc libmfx code. In order to keep compatibility with old commandlines, the default format is set to AV_PIX_FMT_QSV, but this behavior will be removed in the future. Please set "-hwaccel_output_format qsv" explicitly if AV_PIX_FMT_QSV is expected. The normal device stuff works for QSV decoders now, user may use "-init_hw_device args" to initialise device and "-hwaccel_device devicename" to select a device for QSV decoders. "-qsv_device device" which was added for workarounding device selection in the ad-hoc libmfx code is deprecated and will be removed from FFmpeg. For example: $> ffmpeg -init_hw_device vaapi=va:/dev/dri/card0 -init_hw_device qsv=hw@va -hwaccel qsv -c:v h264_qsv -i input.h264 -f null - /dev/dri/renderD128 is actually open for h264_qsv decoder in the above command without this patch. After applying this patch, /dev/dri/card0 is used. $> ffmpeg -init_hw_device vaapi=va:/dev/dri/card0 -init_hw_device qsv=hw@va -hwaccel_device hw -hwaccel qsv -c:v h264_qsv -i input.h264 -f null - device hw of type qsv is not usable in the above command without this patch. After applying this patch, this command works as expected. --- fftools/Makefile | 1 - fftools/ffmpeg.h | 1 - fftools/ffmpeg_hw.c | 12 +++++ fftools/ffmpeg_opt.c | 56 ++++++++++++++++++++-- fftools/ffmpeg_qsv.c | 110 ------------------------------------------- libavcodec/qsvdec.c | 31 +++++++++++- 6 files changed, 93 insertions(+), 118 deletions(-) delete mode 100644 fftools/ffmpeg_qsv.c diff --git a/fftools/Makefile b/fftools/Makefile index 5affaa3f56..5234932ab0 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -10,7 +10,6 @@ ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF)) ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF)) OBJS-ffmpeg += fftools/ffmpeg_opt.o fftools/ffmpeg_filter.o fftools/ffmpeg_hw.o -OBJS-ffmpeg-$(CONFIG_LIBMFX) += fftools/ffmpeg_qsv.o ifndef CONFIG_VIDEOTOOLBOX OBJS-ffmpeg-$(CONFIG_VDA) += fftools/ffmpeg_videotoolbox.o endif diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index e9d30fbd67..27856f95f4 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -60,7 +60,6 @@ enum HWAccelID { HWACCEL_AUTO, HWACCEL_GENERIC, HWACCEL_VIDEOTOOLBOX, - HWACCEL_QSV, }; typedef struct HWAccel { diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c index fc4a5d31d6..043e4c5863 100644 --- a/fftools/ffmpeg_hw.c +++ b/fftools/ffmpeg_hw.c @@ -339,6 +339,18 @@ int hw_device_setup_for_decode(InputStream *ist) } else if (ist->hwaccel_id == HWACCEL_GENERIC) { type = ist->hwaccel_device_type; dev = hw_device_get_by_type(type); + + // When "-qsv_device device" is used, an internal QSV device named + // as "__qsv_device" is created and another QSV device is created + // if "-init_hw_device qsv=name@name" is used. There are 2 QSV devices + // if both "-qsv_device device" and "-init_hw_device qsv=name@name" + // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL. + // To keep back-compatibility with the removed ad-hoc libmfx setup code, + // call hw_device_get_by_name("__qsv_device") to select the internal QSV + // device. This will be removed once -qsv_device is no longer supported. + if (!dev && type == AV_HWDEVICE_TYPE_QSV) + dev = hw_device_get_by_name("__qsv_device"); + if (!dev) err = hw_device_init_from_type(type, NULL, &dev); } else { diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index a63bed54cf..1f4d9dbcfa 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -136,9 +136,6 @@ static const char *const opt_name_enc_time_bases[] = {"enc_time_base" const HWAccel hwaccels[] = { #if CONFIG_VIDEOTOOLBOX { "videotoolbox", videotoolbox_init, HWACCEL_VIDEOTOOLBOX, AV_PIX_FMT_VIDEOTOOLBOX }, -#endif -#if CONFIG_LIBMFX - { "qsv", qsv_init, HWACCEL_QSV, AV_PIX_FMT_QSV }, #endif { 0 }, }; @@ -569,6 +566,49 @@ static int opt_vaapi_device(void *optctx, const char *opt, const char *arg) } #endif +#if CONFIG_QSV +static int opt_qsv_device(void *optctx, const char *opt, const char *arg) +{ +#if CONFIG_VAAPI + const char *prefix = "vaapi=__qsv_child_device:"; +#elif CONFIG_DXVA2 + const char *prefix = "dxva2=__qsv_child_device:"; +#else + const char *prefix = NULL; +#endif + int err = 0; + char *tmp = NULL; + + if (prefix) { + av_log(NULL, AV_LOG_WARNING, + "WARNING: Please use \"-init_hw_device args\" to initialise QSV " + "device and \"-hwaccel_device devicename\" to select QSV device " + "for QSV decoders. \"-qsv_device device\" is DEPRECATED and will " + "be removed in the future. Please do not use \"__qsv_child_device\" " + "or \"__qsv_device\" as device name when both \"-qsv_device device\" " + "and \"-init_hw_device args\" are used.\n"); + + tmp = av_asprintf("%s%s", prefix, arg); + + if (!tmp) + return AVERROR(ENOMEM); + + err = hw_device_init_from_string(tmp, NULL); + + if (err) + goto error; + + err = hw_device_init_from_string("qsv=__qsv_device@__qsv_child_device", NULL); + } else + err = AVERROR(ENOSYS); + +error: + av_free(tmp); + return err; +} + +#endif + static int opt_init_hw_device(void *optctx, const char *opt, const char *arg) { if (!strcmp(arg, "list")) { @@ -894,6 +934,12 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) "with old commandlines. This behaviour is DEPRECATED and will be removed " "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n"); ist->hwaccel_output_format = AV_PIX_FMT_CUDA; + } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) { + av_log(NULL, AV_LOG_WARNING, + "WARNING: defaulting hwaccel_output_format to qsv for compatibility " + "with old commandlines. This behaviour is DEPRECATED and will be removed " + "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n"); + ist->hwaccel_output_format = AV_PIX_FMT_QSV; } else if (hwaccel_output_format) { ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format); if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) { @@ -3805,8 +3851,8 @@ const OptionDef options[] = { #endif #if CONFIG_QSV - { "qsv_device", HAS_ARG | OPT_STRING | OPT_EXPERT, { &qsv_device }, - "set QSV hardware device (DirectX adapter index, DRM path or X11 display name)", "device"}, + { "qsv_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_qsv_device }, + "set QSV hardware device (deprecated, use the normal device stuff; DirectX adapter index, DRM path or X11 display name)", "device"}, #endif { "init_hw_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_init_hw_device }, diff --git a/fftools/ffmpeg_qsv.c b/fftools/ffmpeg_qsv.c deleted file mode 100644 index 960c88b69d..0000000000 --- a/fftools/ffmpeg_qsv.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include - -#include "libavutil/dict.h" -#include "libavutil/hwcontext.h" -#include "libavutil/hwcontext_qsv.h" -#include "libavutil/mem.h" -#include "libavutil/opt.h" -#include "libavcodec/qsv.h" - -#include "ffmpeg.h" - -static AVBufferRef *hw_device_ctx; -char *qsv_device = NULL; - -static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags) -{ - InputStream *ist = s->opaque; - - return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0); -} - -static void qsv_uninit(AVCodecContext *s) -{ - InputStream *ist = s->opaque; - av_buffer_unref(&ist->hw_frames_ctx); -} - -static int qsv_device_init(InputStream *ist) -{ - int err; - AVDictionary *dict = NULL; - - if (qsv_device) { - err = av_dict_set(&dict, "child_device", qsv_device, 0); - if (err < 0) - return err; - } - - err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV, - ist->hwaccel_device, dict, 0); - if (err < 0) { - av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n"); - goto err_out; - } - -err_out: - if (dict) - av_dict_free(&dict); - - return err; -} - -int qsv_init(AVCodecContext *s) -{ - InputStream *ist = s->opaque; - AVHWFramesContext *frames_ctx; - AVQSVFramesContext *frames_hwctx; - int ret; - - if (!hw_device_ctx) { - ret = qsv_device_init(ist); - if (ret < 0) - return ret; - } - - av_buffer_unref(&ist->hw_frames_ctx); - ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx); - if (!ist->hw_frames_ctx) - return AVERROR(ENOMEM); - - frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data; - frames_hwctx = frames_ctx->hwctx; - - frames_ctx->width = FFALIGN(s->coded_width, 32); - frames_ctx->height = FFALIGN(s->coded_height, 32); - frames_ctx->format = AV_PIX_FMT_QSV; - frames_ctx->sw_format = s->sw_pix_fmt; - frames_ctx->initial_pool_size = 64 + s->extra_hw_frames; - frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET; - - ret = av_hwframe_ctx_init(ist->hw_frames_ctx); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n"); - return ret; - } - - ist->hwaccel_get_buffer = qsv_get_buffer; - ist->hwaccel_uninit = qsv_uninit; - - return 0; -} diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c index 622750927c..1ee9944aa2 100644 --- a/libavcodec/qsvdec.c +++ b/libavcodec/qsvdec.c @@ -99,7 +99,7 @@ static const AVCodecHWConfigInternal *const qsv_hw_configs[] = { .public = { .pix_fmt = AV_PIX_FMT_QSV, .methods = AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | - AV_CODEC_HW_CONFIG_METHOD_AD_HOC, + AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX, .device_type = AV_HWDEVICE_TYPE_QSV, }, .hwaccel = NULL, @@ -248,6 +248,35 @@ static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixel q->nb_ext_buffers = user_ctx->nb_ext_buffers; } + if (avctx->hw_device_ctx && !avctx->hw_frames_ctx && ret == AV_PIX_FMT_QSV) { + AVHWFramesContext *hwframes_ctx; + AVQSVFramesContext *frames_hwctx; + + avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx); + + if (!avctx->hw_frames_ctx) { + av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n"); + return AVERROR(ENOMEM); + } + + hwframes_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; + frames_hwctx = hwframes_ctx->hwctx; + hwframes_ctx->width = FFALIGN(avctx->coded_width, 32); + hwframes_ctx->height = FFALIGN(avctx->coded_height, 32); + hwframes_ctx->format = AV_PIX_FMT_QSV; + hwframes_ctx->sw_format = avctx->sw_pix_fmt; + hwframes_ctx->initial_pool_size = 64 + avctx->extra_hw_frames; + frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET; + + ret = av_hwframe_ctx_init(avctx->hw_frames_ctx); + + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n"); + av_buffer_unref(&avctx->hw_frames_ctx); + return ret; + } + } + if (avctx->hw_frames_ctx) { AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx; From patchwork Wed Jun 23 03:03:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 28864 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:bbc9:0:0:0:0:0 with SMTP id c9csp5480006ybk; Tue, 22 Jun 2021 20:04:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVwM7j0gKG4x16ktBmN6/UygtL3llQUv6T2hCR4fY9muiS5kXc3ikZaPU9gJqfgLTCBcCZ X-Received: by 2002:a17:906:28c2:: with SMTP id p2mr7492258ejd.41.1624417495460; Tue, 22 Jun 2021 20:04:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624417495; cv=none; d=google.com; s=arc-20160816; b=ogmqWr+QrzV9HwC31tEijv251ykmIqteHTNobNNfEDB+jiW0wQlfOLsk4iVQ51r0kg +AZ0MDmTG0PD/K5sAGa9/QuF0Fta6uRCaYV5BZM/VmM2AFGKgWIMZRIX/nQH5ocScnPE gQT4IRYaeUwxlGuPbeq+LdHf9Ad3mSQypSMnkPjirLowkRpTmAn9x1qKtpIK7C5t/Xw6 GnDkhh4By0Yl2qkhMKju3sBTSnsaWNacCpE3l1rbY3yk2Dr8cPuyVXoTW0o+z1PJZ/L4 /7j/ZTkkNbYYMzfte/EB8HFTByPxO73pgHXnZlxI8iYd9s+Qx54wUYYxJGjnFY/04xhD eCzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:ironport-sdr:ironport-sdr:delivered-to; bh=wPCxLanyOsXHi449lpz/SzdVFft6Bzyr7xpmS3NCmVw=; b=nepseaPt+De6ZmSlI7CrbKMSdMa8emT2P3/esruml54OtVduyJEkLrU32M+VoJI7G0 sHTbIKdho4xonKjipSv0oliqI1fnZSuq7ga6cvzqahlobCGiqZRHK8+pNcHbiq5HsN7v FR9ryAL1EwIEKoZnFnsbWfuH4gf2y9psn9tc03Rrml/c0MCWZ5DPDBRYGY7zmrJeeizd nG5D6c4x5TEPefHwLz1yiOPkXoMj0aKe+H4u1TKdSSSn3Nhq55tF8f48dAfrSA0ZnaZ2 5q4wm6D/hbShtABug1BG/mIHf4jQ8NbKjF1d54HpZwfgioxYmlVRSQC2eUv72A1d0UrT 0RSg== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 24si6743928edv.35.2021.06.22.20.04.54; Tue, 22 Jun 2021 20:04:55 -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; 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=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 20C22689E5B; Wed, 23 Jun 2021 06:04:50 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 490BD680BBB for ; Wed, 23 Jun 2021 06:04:41 +0300 (EEST) IronPort-SDR: 384OE+Qx1zURa7QN9tLT2mbqwNd5p/Y0tCPWPBfJS2hFS9X6A2g16SLMg98QO6V0pmEd9pl5Se Gv3wyqzvFUAg== X-IronPort-AV: E=McAfee;i="6200,9189,10023"; a="205355674" X-IronPort-AV: E=Sophos;i="5.83,293,1616482800"; d="scan'208";a="205355674" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2021 20:04:17 -0700 IronPort-SDR: Ma3hzLymZipNidGzj2zzTloGNJFkjWxLQkVFN29ZvFZhBh5FcuuCA8yc8dbo51yw45Aq4lgH+L kq0nL19AUMOw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,293,1616482800"; d="scan'208";a="641844718" Received: from xhh-tgl64.sh.intel.com ([10.239.159.53]) by fmsmga005.fm.intel.com with ESMTP; 22 Jun 2021 20:03:54 -0700 From: Haihao Xiang To: ffmpeg-devel@ffmpeg.org Date: Wed, 23 Jun 2021 11:03:46 +0800 Message-Id: <20210623030346.2025933-2-haihao.xiang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210623030346.2025933-1-haihao.xiang@intel.com> References: <20210623030346.2025933-1-haihao.xiang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] doc/examples/qsvdec: simplify this example via hw_device_ctx interface X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Haihao Xiang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: wkJi9wnbLdl8 Content-Length: 4340 --- doc/examples/qsvdec.c | 45 +++++++++---------------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/doc/examples/qsvdec.c b/doc/examples/qsvdec.c index 7415eefca5..571d868f93 100644 --- a/doc/examples/qsvdec.c +++ b/doc/examples/qsvdec.c @@ -44,38 +44,10 @@ #include "libavutil/hwcontext_qsv.h" #include "libavutil/mem.h" -typedef struct DecodeContext { - AVBufferRef *hw_device_ref; -} DecodeContext; - static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts) { while (*pix_fmts != AV_PIX_FMT_NONE) { if (*pix_fmts == AV_PIX_FMT_QSV) { - DecodeContext *decode = avctx->opaque; - AVHWFramesContext *frames_ctx; - AVQSVFramesContext *frames_hwctx; - int ret; - - /* create a pool of surfaces to be used by the decoder */ - avctx->hw_frames_ctx = av_hwframe_ctx_alloc(decode->hw_device_ref); - if (!avctx->hw_frames_ctx) - return AV_PIX_FMT_NONE; - frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; - frames_hwctx = frames_ctx->hwctx; - - frames_ctx->format = AV_PIX_FMT_QSV; - frames_ctx->sw_format = avctx->sw_pix_fmt; - frames_ctx->width = FFALIGN(avctx->coded_width, 32); - frames_ctx->height = FFALIGN(avctx->coded_height, 32); - frames_ctx->initial_pool_size = 32; - - frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET; - - ret = av_hwframe_ctx_init(avctx->hw_frames_ctx); - if (ret < 0) - return AV_PIX_FMT_NONE; - return AV_PIX_FMT_QSV; } @@ -87,7 +59,7 @@ static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts) return AV_PIX_FMT_NONE; } -static int decode_packet(DecodeContext *decode, AVCodecContext *decoder_ctx, +static int decode_packet(AVCodecContext *decoder_ctx, AVFrame *frame, AVFrame *sw_frame, AVPacket *pkt, AVIOContext *output_ctx) { @@ -144,12 +116,12 @@ int main(int argc, char **argv) AVPacket pkt = { 0 }; AVFrame *frame = NULL, *sw_frame = NULL; - DecodeContext decode = { NULL }; - AVIOContext *output_ctx = NULL; int ret, i; + AVBufferRef *device_ref = NULL; + if (argc < 3) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; @@ -177,7 +149,7 @@ int main(int argc, char **argv) } /* open the hardware device */ - ret = av_hwdevice_ctx_create(&decode.hw_device_ref, AV_HWDEVICE_TYPE_QSV, + ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_QSV, "auto", NULL, 0); if (ret < 0) { fprintf(stderr, "Cannot open the hardware device\n"); @@ -209,7 +181,8 @@ int main(int argc, char **argv) decoder_ctx->extradata_size = video_st->codecpar->extradata_size; } - decoder_ctx->opaque = &decode; + + decoder_ctx->hw_device_ctx = av_buffer_ref(device_ref); decoder_ctx->get_format = get_format; ret = avcodec_open2(decoder_ctx, NULL, NULL); @@ -239,7 +212,7 @@ int main(int argc, char **argv) break; if (pkt.stream_index == video_st->index) - ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx); + ret = decode_packet(decoder_ctx, frame, sw_frame, &pkt, output_ctx); av_packet_unref(&pkt); } @@ -247,7 +220,7 @@ int main(int argc, char **argv) /* flush the decoder */ pkt.data = NULL; pkt.size = 0; - ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx); + ret = decode_packet(decoder_ctx, frame, sw_frame, &pkt, output_ctx); finish: if (ret < 0) { @@ -263,7 +236,7 @@ finish: avcodec_free_context(&decoder_ctx); - av_buffer_unref(&decode.hw_device_ref); + av_buffer_unref(&device_ref); avio_close(output_ctx);