From patchwork Tue May 23 13:58:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 41796 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c51c:b0:10c:5e6f:955f with SMTP id gm28csp600358pzb; Tue, 23 May 2023 06:59:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5irZbuNMgN9KeUP/H0R5f1x1OP4DPFiHBPRF2RZFOUQdxgolrUZHbq6FnfjLD9fY7UmBx2 X-Received: by 2002:a05:6402:2ce:b0:50b:c4fb:770f with SMTP id b14-20020a05640202ce00b0050bc4fb770fmr11287814edx.34.1684850373937; Tue, 23 May 2023 06:59:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684850373; cv=none; d=google.com; s=arc-20160816; b=Lf+3sHWV4QbmS4KYS1O+4yf/a/EJiNn4ISBl0vDz0qUR4qcge1ONFg0e90IgYN2lNo qLnUbE3AAsDRyrrg3lXsxundcTJrSp//sW6eEYnVm3YIsJ1n/dkWiywFiStSleMIjVMn TJoCuAejsNpw6VuEAnMUaGt/g2it+LLt8zahcyzo574+A2XjPcoPMjWTZ3qG+lo/7Vbh Vdc26SVeJmWznDMfbItaQZ/9a+xuZ3EzsiDo+oEFh5Cao8+PUxM0tHnwpDMaE3AjIoI/ vSAxyxNhTUGS7wgrQV6dsQvSOAil2Kj5ybmKgBPNWMo4yejdWxqrlqYUSJgecGhpTN3U gBcA== 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:mime-version:message-id:date:to:from :delivered-to; bh=0SUijrIXTnGHlerIU7yMLVbbSN63gIDTQw7j3sXYWBw=; b=I4AbFg9MMdmPy63T5iMTNVT19de9FeHRgxIStR/hfyr5DCA4D4IbvuwUXkXbMHvlBA ZcAQKmDKcvaMCjNjbH9vpEX1YdtR+00zFaGa36H6NxWTis3N1F3IXYwotzve2VRqLmd2 iygAow/4jBiUu2uhE+d1QMftduZwcrMhn1UT/hGEGOtcBQqEFqIQDu1xvCSZfyxSqTDq VHQ43mNMLGAqMU1RC6wuwQPYse+jbNtriKG6sRh+3h+l6C+EEpkatt466FctjEfCz8Qu I3FHcbFlj2frWl3kqNALbVAz3DaP7UZjpZVDfFcEKNFamQp9wv+I2BWWUgnVQkwSJD/O 2ZCw== 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o13-20020aa7c50d000000b0050e068438b3si6370623edq.531.2023.05.23.06.59.33; Tue, 23 May 2023 06:59:33 -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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4CF9868C0CB; Tue, 23 May 2023 16:59:06 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4A31268C023 for ; Tue, 23 May 2023 16:58:50 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id CEC4B2404EC for ; Tue, 23 May 2023 15:58:49 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id jgDI9RnacQyL for ; Tue, 23 May 2023 15:58:48 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 5E6FD240177 for ; Tue, 23 May 2023 15:58:48 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 20E683A031B for ; Tue, 23 May 2023 15:58:48 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 May 2023 15:58:28 +0200 Message-Id: <20230523135842.20388-1-anton@khirnov.net> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/15] fftools/ffmpeg_hw: move hw_device_setup_for_decode() to ffmpeg_dec 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: R4J3BOWKGyAH This function is entangled with decoder setup, so it is more decoding code rather than ffmpeg_hw code. This will allow making more decoder state private in the future. --- fftools/ffmpeg.h | 5 +- fftools/ffmpeg_dec.c | 145 ++++++++++++++++++++++++++++++++++++++++ fftools/ffmpeg_hw.c | 153 ++----------------------------------------- 3 files changed, 153 insertions(+), 150 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 45be3b1823..895edbd6d6 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -795,10 +795,13 @@ void enc_stats_write(OutputStream *ost, EncStats *es, uint64_t frame_num); HWDevice *hw_device_get_by_name(const char *name); +HWDevice *hw_device_get_by_type(enum AVHWDeviceType type); int hw_device_init_from_string(const char *arg, HWDevice **dev); +int hw_device_init_from_type(enum AVHWDeviceType type, + const char *device, + HWDevice **dev_out); void hw_device_free_all(void); -int hw_device_setup_for_decode(InputStream *ist); int hw_device_setup_for_encode(OutputStream *ost); /** * Get a hardware device to be used with this filtergraph. diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c index e06747d9c4..0a470c4854 100644 --- a/fftools/ffmpeg_dec.c +++ b/fftools/ffmpeg_dec.c @@ -528,6 +528,151 @@ static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat return *p; } +static HWDevice *hw_device_match_by_codec(const AVCodec *codec) +{ + const AVCodecHWConfig *config; + HWDevice *dev; + int i; + for (i = 0;; i++) { + config = avcodec_get_hw_config(codec, i); + if (!config) + return NULL; + if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) + continue; + dev = hw_device_get_by_type(config->device_type); + if (dev) + return dev; + } +} + +static int hw_device_setup_for_decode(InputStream *ist) +{ + const AVCodecHWConfig *config; + enum AVHWDeviceType type; + HWDevice *dev = NULL; + int err, auto_device = 0; + + if (ist->hwaccel_device) { + dev = hw_device_get_by_name(ist->hwaccel_device); + if (!dev) { + if (ist->hwaccel_id == HWACCEL_AUTO) { + auto_device = 1; + } else if (ist->hwaccel_id == HWACCEL_GENERIC) { + type = ist->hwaccel_device_type; + err = hw_device_init_from_type(type, ist->hwaccel_device, + &dev); + } else { + // This will be dealt with by API-specific initialisation + // (using hwaccel_device), so nothing further needed here. + return 0; + } + } else { + if (ist->hwaccel_id == HWACCEL_AUTO) { + ist->hwaccel_device_type = dev->type; + } else if (ist->hwaccel_device_type != dev->type) { + av_log(NULL, AV_LOG_ERROR, "Invalid hwaccel device " + "specified for decoder: device %s of type %s is not " + "usable with hwaccel %s.\n", dev->name, + av_hwdevice_get_type_name(dev->type), + av_hwdevice_get_type_name(ist->hwaccel_device_type)); + return AVERROR(EINVAL); + } + } + } else { + if (ist->hwaccel_id == HWACCEL_AUTO) { + auto_device = 1; + } 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. Another QSV device is created too + // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices + // if both "-qsv_device device" and "-init_hw_device qsv=name:device" + // 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. + 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 { + dev = hw_device_match_by_codec(ist->dec); + if (!dev) { + // No device for this codec, but not using generic hwaccel + // and therefore may well not need one - ignore. + return 0; + } + } + } + + if (auto_device) { + int i; + if (!avcodec_get_hw_config(ist->dec, 0)) { + // Decoder does not support any hardware devices. + return 0; + } + for (i = 0; !dev; i++) { + config = avcodec_get_hw_config(ist->dec, i); + if (!config) + break; + type = config->device_type; + dev = hw_device_get_by_type(type); + if (dev) { + av_log(NULL, AV_LOG_INFO, "Using auto " + "hwaccel type %s with existing device %s.\n", + av_hwdevice_get_type_name(type), dev->name); + } + } + for (i = 0; !dev; i++) { + config = avcodec_get_hw_config(ist->dec, i); + if (!config) + break; + type = config->device_type; + // Try to make a new device of this type. + err = hw_device_init_from_type(type, ist->hwaccel_device, + &dev); + if (err < 0) { + // Can't make a device of this type. + continue; + } + if (ist->hwaccel_device) { + av_log(NULL, AV_LOG_INFO, "Using auto " + "hwaccel type %s with new device created " + "from %s.\n", av_hwdevice_get_type_name(type), + ist->hwaccel_device); + } else { + av_log(NULL, AV_LOG_INFO, "Using auto " + "hwaccel type %s with new default device.\n", + av_hwdevice_get_type_name(type)); + } + } + if (dev) { + ist->hwaccel_device_type = type; + } else { + av_log(NULL, AV_LOG_INFO, "Auto hwaccel " + "disabled: no device found.\n"); + ist->hwaccel_id = HWACCEL_NONE; + return 0; + } + } + + if (!dev) { + av_log(NULL, AV_LOG_ERROR, "No device available " + "for decoder: device type %s needed for codec %s.\n", + av_hwdevice_get_type_name(type), ist->dec->name); + return err; + } + + ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); + if (!ist->dec_ctx->hw_device_ctx) + return AVERROR(ENOMEM); + + return 0; +} + int dec_open(InputStream *ist) { const AVCodec *codec = ist->dec; diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c index e67145211c..d28257a1d6 100644 --- a/fftools/ffmpeg_hw.c +++ b/fftools/ffmpeg_hw.c @@ -27,7 +27,7 @@ static int nb_hw_devices; static HWDevice **hw_devices; -static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type) +HWDevice *hw_device_get_by_type(enum AVHWDeviceType type) { HWDevice *found = NULL; int i; @@ -242,9 +242,9 @@ fail: goto done; } -static int hw_device_init_from_type(enum AVHWDeviceType type, - const char *device, - HWDevice **dev_out) +int hw_device_init_from_type(enum AVHWDeviceType type, + const char *device, + HWDevice **dev_out) { AVBufferRef *device_ref = NULL; HWDevice *dev; @@ -297,151 +297,6 @@ void hw_device_free_all(void) nb_hw_devices = 0; } -static HWDevice *hw_device_match_by_codec(const AVCodec *codec) -{ - const AVCodecHWConfig *config; - HWDevice *dev; - int i; - for (i = 0;; i++) { - config = avcodec_get_hw_config(codec, i); - if (!config) - return NULL; - if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) - continue; - dev = hw_device_get_by_type(config->device_type); - if (dev) - return dev; - } -} - -int hw_device_setup_for_decode(InputStream *ist) -{ - const AVCodecHWConfig *config; - enum AVHWDeviceType type; - HWDevice *dev = NULL; - int err, auto_device = 0; - - if (ist->hwaccel_device) { - dev = hw_device_get_by_name(ist->hwaccel_device); - if (!dev) { - if (ist->hwaccel_id == HWACCEL_AUTO) { - auto_device = 1; - } else if (ist->hwaccel_id == HWACCEL_GENERIC) { - type = ist->hwaccel_device_type; - err = hw_device_init_from_type(type, ist->hwaccel_device, - &dev); - } else { - // This will be dealt with by API-specific initialisation - // (using hwaccel_device), so nothing further needed here. - return 0; - } - } else { - if (ist->hwaccel_id == HWACCEL_AUTO) { - ist->hwaccel_device_type = dev->type; - } else if (ist->hwaccel_device_type != dev->type) { - av_log(NULL, AV_LOG_ERROR, "Invalid hwaccel device " - "specified for decoder: device %s of type %s is not " - "usable with hwaccel %s.\n", dev->name, - av_hwdevice_get_type_name(dev->type), - av_hwdevice_get_type_name(ist->hwaccel_device_type)); - return AVERROR(EINVAL); - } - } - } else { - if (ist->hwaccel_id == HWACCEL_AUTO) { - auto_device = 1; - } 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. Another QSV device is created too - // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices - // if both "-qsv_device device" and "-init_hw_device qsv=name:device" - // 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. - 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 { - dev = hw_device_match_by_codec(ist->dec); - if (!dev) { - // No device for this codec, but not using generic hwaccel - // and therefore may well not need one - ignore. - return 0; - } - } - } - - if (auto_device) { - int i; - if (!avcodec_get_hw_config(ist->dec, 0)) { - // Decoder does not support any hardware devices. - return 0; - } - for (i = 0; !dev; i++) { - config = avcodec_get_hw_config(ist->dec, i); - if (!config) - break; - type = config->device_type; - dev = hw_device_get_by_type(type); - if (dev) { - av_log(NULL, AV_LOG_INFO, "Using auto " - "hwaccel type %s with existing device %s.\n", - av_hwdevice_get_type_name(type), dev->name); - } - } - for (i = 0; !dev; i++) { - config = avcodec_get_hw_config(ist->dec, i); - if (!config) - break; - type = config->device_type; - // Try to make a new device of this type. - err = hw_device_init_from_type(type, ist->hwaccel_device, - &dev); - if (err < 0) { - // Can't make a device of this type. - continue; - } - if (ist->hwaccel_device) { - av_log(NULL, AV_LOG_INFO, "Using auto " - "hwaccel type %s with new device created " - "from %s.\n", av_hwdevice_get_type_name(type), - ist->hwaccel_device); - } else { - av_log(NULL, AV_LOG_INFO, "Using auto " - "hwaccel type %s with new default device.\n", - av_hwdevice_get_type_name(type)); - } - } - if (dev) { - ist->hwaccel_device_type = type; - } else { - av_log(NULL, AV_LOG_INFO, "Auto hwaccel " - "disabled: no device found.\n"); - ist->hwaccel_id = HWACCEL_NONE; - return 0; - } - } - - if (!dev) { - av_log(NULL, AV_LOG_ERROR, "No device available " - "for decoder: device type %s needed for codec %s.\n", - av_hwdevice_get_type_name(type), ist->dec->name); - return err; - } - - ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); - if (!ist->dec_ctx->hw_device_ctx) - return AVERROR(ENOMEM); - - return 0; -} - int hw_device_setup_for_encode(OutputStream *ost) { const AVCodecHWConfig *config;